PR90545, gcc.target/powerpc/fold-vec-splats-floatdouble.c fails
[official-gcc.git] / gcc / config / rs6000 / rs6000.c
blob693f2e1c91ff4815987a4f2dcd3c63912068a95e
1 /* Subroutines used for code generation on IBM RS/6000.
2 Copyright (C) 1991-2019 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 #define IN_TARGET_CODE 1
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "backend.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "memmodel.h"
30 #include "gimple.h"
31 #include "cfghooks.h"
32 #include "cfgloop.h"
33 #include "df.h"
34 #include "tm_p.h"
35 #include "stringpool.h"
36 #include "expmed.h"
37 #include "optabs.h"
38 #include "regs.h"
39 #include "ira.h"
40 #include "recog.h"
41 #include "cgraph.h"
42 #include "diagnostic-core.h"
43 #include "insn-attr.h"
44 #include "flags.h"
45 #include "alias.h"
46 #include "fold-const.h"
47 #include "attribs.h"
48 #include "stor-layout.h"
49 #include "calls.h"
50 #include "print-tree.h"
51 #include "varasm.h"
52 #include "explow.h"
53 #include "expr.h"
54 #include "output.h"
55 #include "dbxout.h"
56 #include "common/common-target.h"
57 #include "langhooks.h"
58 #include "reload.h"
59 #include "sched-int.h"
60 #include "gimplify.h"
61 #include "gimple-fold.h"
62 #include "gimple-iterator.h"
63 #include "gimple-ssa.h"
64 #include "gimple-walk.h"
65 #include "intl.h"
66 #include "params.h"
67 #include "tm-constrs.h"
68 #include "tree-vectorizer.h"
69 #include "target-globals.h"
70 #include "builtins.h"
71 #include "tree-vector-builder.h"
72 #include "context.h"
73 #include "tree-pass.h"
74 #include "except.h"
75 #if TARGET_XCOFF
76 #include "xcoffout.h" /* get declarations of xcoff_*_section_name */
77 #endif
78 #if TARGET_MACHO
79 #include "gstab.h" /* for N_SLINE */
80 #endif
81 #include "case-cfn-macros.h"
82 #include "ppc-auxv.h"
83 #include "tree-ssa-propagate.h"
84 #include "tree-vrp.h"
85 #include "tree-ssanames.h"
87 /* This file should be included last. */
88 #include "target-def.h"
90 #ifndef TARGET_NO_PROTOTYPE
91 #define TARGET_NO_PROTOTYPE 0
92 #endif
94 /* Set -mabi=ieeelongdouble on some old targets. In the future, power server
95 systems will also set long double to be IEEE 128-bit. AIX and Darwin
96 explicitly redefine TARGET_IEEEQUAD and TARGET_IEEEQUAD_DEFAULT to 0, so
97 those systems will not pick up this default. This needs to be after all
98 of the include files, so that POWERPC_LINUX and POWERPC_FREEBSD are
99 properly defined. */
100 #ifndef TARGET_IEEEQUAD_DEFAULT
101 #if !defined (POWERPC_LINUX) && !defined (POWERPC_FREEBSD)
102 #define TARGET_IEEEQUAD_DEFAULT 1
103 #else
104 #define TARGET_IEEEQUAD_DEFAULT 0
105 #endif
106 #endif
108 static pad_direction rs6000_function_arg_padding (machine_mode, const_tree);
110 /* Structure used to define the rs6000 stack */
111 typedef struct rs6000_stack {
112 int reload_completed; /* stack info won't change from here on */
113 int first_gp_reg_save; /* first callee saved GP register used */
114 int first_fp_reg_save; /* first callee saved FP register used */
115 int first_altivec_reg_save; /* first callee saved AltiVec register used */
116 int lr_save_p; /* true if the link reg needs to be saved */
117 int cr_save_p; /* true if the CR reg needs to be saved */
118 unsigned int vrsave_mask; /* mask of vec registers to save */
119 int push_p; /* true if we need to allocate stack space */
120 int calls_p; /* true if the function makes any calls */
121 int world_save_p; /* true if we're saving *everything*:
122 r13-r31, cr, f14-f31, vrsave, v20-v31 */
123 enum rs6000_abi abi; /* which ABI to use */
124 int gp_save_offset; /* offset to save GP regs from initial SP */
125 int fp_save_offset; /* offset to save FP regs from initial SP */
126 int altivec_save_offset; /* offset to save AltiVec regs from initial SP */
127 int lr_save_offset; /* offset to save LR from initial SP */
128 int cr_save_offset; /* offset to save CR from initial SP */
129 int vrsave_save_offset; /* offset to save VRSAVE from initial SP */
130 int varargs_save_offset; /* offset to save the varargs registers */
131 int ehrd_offset; /* offset to EH return data */
132 int ehcr_offset; /* offset to EH CR field data */
133 int reg_size; /* register size (4 or 8) */
134 HOST_WIDE_INT vars_size; /* variable save area size */
135 int parm_size; /* outgoing parameter size */
136 int save_size; /* save area size */
137 int fixed_size; /* fixed size of stack frame */
138 int gp_size; /* size of saved GP registers */
139 int fp_size; /* size of saved FP registers */
140 int altivec_size; /* size of saved AltiVec registers */
141 int cr_size; /* size to hold CR if not in fixed area */
142 int vrsave_size; /* size to hold VRSAVE */
143 int altivec_padding_size; /* size of altivec alignment padding */
144 HOST_WIDE_INT total_size; /* total bytes allocated for stack */
145 int savres_strategy;
146 } rs6000_stack_t;
148 /* A C structure for machine-specific, per-function data.
149 This is added to the cfun structure. */
150 typedef struct GTY(()) machine_function
152 /* Flags if __builtin_return_address (n) with n >= 1 was used. */
153 int ra_needs_full_frame;
154 /* Flags if __builtin_return_address (0) was used. */
155 int ra_need_lr;
156 /* Cache lr_save_p after expansion of builtin_eh_return. */
157 int lr_save_state;
158 /* Whether we need to save the TOC to the reserved stack location in the
159 function prologue. */
160 bool save_toc_in_prologue;
161 /* Offset from virtual_stack_vars_rtx to the start of the ABI_V4
162 varargs save area. */
163 HOST_WIDE_INT varargs_save_offset;
164 /* Alternative internal arg pointer for -fsplit-stack. */
165 rtx split_stack_arg_pointer;
166 bool split_stack_argp_used;
167 /* Flag if r2 setup is needed with ELFv2 ABI. */
168 bool r2_setup_needed;
169 /* The number of components we use for separate shrink-wrapping. */
170 int n_components;
171 /* The components already handled by separate shrink-wrapping, which should
172 not be considered by the prologue and epilogue. */
173 bool gpr_is_wrapped_separately[32];
174 bool fpr_is_wrapped_separately[32];
175 bool lr_is_wrapped_separately;
176 bool toc_is_wrapped_separately;
177 } machine_function;
179 /* Support targetm.vectorize.builtin_mask_for_load. */
180 static GTY(()) tree altivec_builtin_mask_for_load;
182 /* Set to nonzero once AIX common-mode calls have been defined. */
183 static GTY(()) int common_mode_defined;
185 /* Label number of label created for -mrelocatable, to call to so we can
186 get the address of the GOT section */
187 static int rs6000_pic_labelno;
189 #ifdef USING_ELFOS_H
190 /* Counter for labels which are to be placed in .fixup. */
191 int fixuplabelno = 0;
192 #endif
194 /* Whether to use variant of AIX ABI for PowerPC64 Linux. */
195 int dot_symbols;
197 /* Specify the machine mode that pointers have. After generation of rtl, the
198 compiler makes no further distinction between pointers and any other objects
199 of this machine mode. */
200 scalar_int_mode rs6000_pmode;
202 #if TARGET_ELF
203 /* Note whether IEEE 128-bit floating point was passed or returned, either as
204 the __float128/_Float128 explicit type, or when long double is IEEE 128-bit
205 floating point. We changed the default C++ mangling for these types and we
206 may want to generate a weak alias of the old mangling (U10__float128) to the
207 new mangling (u9__ieee128). */
208 static bool rs6000_passes_ieee128;
209 #endif
211 /* Generate the manged name (i.e. U10__float128) used in GCC 8.1, and not the
212 name used in current releases (i.e. u9__ieee128). */
213 static bool ieee128_mangling_gcc_8_1;
215 /* Width in bits of a pointer. */
216 unsigned rs6000_pointer_size;
218 #ifdef HAVE_AS_GNU_ATTRIBUTE
219 # ifndef HAVE_LD_PPC_GNU_ATTR_LONG_DOUBLE
220 # define HAVE_LD_PPC_GNU_ATTR_LONG_DOUBLE 0
221 # endif
222 /* Flag whether floating point values have been passed/returned.
223 Note that this doesn't say whether fprs are used, since the
224 Tag_GNU_Power_ABI_FP .gnu.attributes value this flag controls
225 should be set for soft-float values passed in gprs and ieee128
226 values passed in vsx registers. */
227 static bool rs6000_passes_float;
228 static bool rs6000_passes_long_double;
229 /* Flag whether vector values have been passed/returned. */
230 static bool rs6000_passes_vector;
231 /* Flag whether small (<= 8 byte) structures have been returned. */
232 static bool rs6000_returns_struct;
233 #endif
235 /* Value is TRUE if register/mode pair is acceptable. */
236 static bool rs6000_hard_regno_mode_ok_p
237 [NUM_MACHINE_MODES][FIRST_PSEUDO_REGISTER];
239 /* Maximum number of registers needed for a given register class and mode. */
240 unsigned char rs6000_class_max_nregs[NUM_MACHINE_MODES][LIM_REG_CLASSES];
242 /* How many registers are needed for a given register and mode. */
243 unsigned char rs6000_hard_regno_nregs[NUM_MACHINE_MODES][FIRST_PSEUDO_REGISTER];
245 /* Map register number to register class. */
246 enum reg_class rs6000_regno_regclass[FIRST_PSEUDO_REGISTER];
248 static int dbg_cost_ctrl;
250 /* Built in types. */
251 tree rs6000_builtin_types[RS6000_BTI_MAX];
252 tree rs6000_builtin_decls[RS6000_BUILTIN_COUNT];
254 /* Flag to say the TOC is initialized */
255 int toc_initialized, need_toc_init;
256 char toc_label_name[10];
258 /* Cached value of rs6000_variable_issue. This is cached in
259 rs6000_variable_issue hook and returned from rs6000_sched_reorder2. */
260 static short cached_can_issue_more;
262 static GTY(()) section *read_only_data_section;
263 static GTY(()) section *private_data_section;
264 static GTY(()) section *tls_data_section;
265 static GTY(()) section *tls_private_data_section;
266 static GTY(()) section *read_only_private_data_section;
267 static GTY(()) section *sdata2_section;
268 static GTY(()) section *toc_section;
270 struct builtin_description
272 const HOST_WIDE_INT mask;
273 const enum insn_code icode;
274 const char *const name;
275 const enum rs6000_builtins code;
278 /* Describe the vector unit used for modes. */
279 enum rs6000_vector rs6000_vector_unit[NUM_MACHINE_MODES];
280 enum rs6000_vector rs6000_vector_mem[NUM_MACHINE_MODES];
282 /* Register classes for various constraints that are based on the target
283 switches. */
284 enum reg_class rs6000_constraints[RS6000_CONSTRAINT_MAX];
286 /* Describe the alignment of a vector. */
287 int rs6000_vector_align[NUM_MACHINE_MODES];
289 /* Map selected modes to types for builtins. */
290 static GTY(()) tree builtin_mode_to_type[MAX_MACHINE_MODE][2];
292 /* What modes to automatically generate reciprocal divide estimate (fre) and
293 reciprocal sqrt (frsqrte) for. */
294 unsigned char rs6000_recip_bits[MAX_MACHINE_MODE];
296 /* Masks to determine which reciprocal esitmate instructions to generate
297 automatically. */
298 enum rs6000_recip_mask {
299 RECIP_SF_DIV = 0x001, /* Use divide estimate */
300 RECIP_DF_DIV = 0x002,
301 RECIP_V4SF_DIV = 0x004,
302 RECIP_V2DF_DIV = 0x008,
304 RECIP_SF_RSQRT = 0x010, /* Use reciprocal sqrt estimate. */
305 RECIP_DF_RSQRT = 0x020,
306 RECIP_V4SF_RSQRT = 0x040,
307 RECIP_V2DF_RSQRT = 0x080,
309 /* Various combination of flags for -mrecip=xxx. */
310 RECIP_NONE = 0,
311 RECIP_ALL = (RECIP_SF_DIV | RECIP_DF_DIV | RECIP_V4SF_DIV
312 | RECIP_V2DF_DIV | RECIP_SF_RSQRT | RECIP_DF_RSQRT
313 | RECIP_V4SF_RSQRT | RECIP_V2DF_RSQRT),
315 RECIP_HIGH_PRECISION = RECIP_ALL,
317 /* On low precision machines like the power5, don't enable double precision
318 reciprocal square root estimate, since it isn't accurate enough. */
319 RECIP_LOW_PRECISION = (RECIP_ALL & ~(RECIP_DF_RSQRT | RECIP_V2DF_RSQRT))
322 /* -mrecip options. */
323 static struct
325 const char *string; /* option name */
326 unsigned int mask; /* mask bits to set */
327 } recip_options[] = {
328 { "all", RECIP_ALL },
329 { "none", RECIP_NONE },
330 { "div", (RECIP_SF_DIV | RECIP_DF_DIV | RECIP_V4SF_DIV
331 | RECIP_V2DF_DIV) },
332 { "divf", (RECIP_SF_DIV | RECIP_V4SF_DIV) },
333 { "divd", (RECIP_DF_DIV | RECIP_V2DF_DIV) },
334 { "rsqrt", (RECIP_SF_RSQRT | RECIP_DF_RSQRT | RECIP_V4SF_RSQRT
335 | RECIP_V2DF_RSQRT) },
336 { "rsqrtf", (RECIP_SF_RSQRT | RECIP_V4SF_RSQRT) },
337 { "rsqrtd", (RECIP_DF_RSQRT | RECIP_V2DF_RSQRT) },
340 /* Used by __builtin_cpu_is(), mapping from PLATFORM names to values. */
341 static const struct
343 const char *cpu;
344 unsigned int cpuid;
345 } cpu_is_info[] = {
346 { "power9", PPC_PLATFORM_POWER9 },
347 { "power8", PPC_PLATFORM_POWER8 },
348 { "power7", PPC_PLATFORM_POWER7 },
349 { "power6x", PPC_PLATFORM_POWER6X },
350 { "power6", PPC_PLATFORM_POWER6 },
351 { "power5+", PPC_PLATFORM_POWER5_PLUS },
352 { "power5", PPC_PLATFORM_POWER5 },
353 { "ppc970", PPC_PLATFORM_PPC970 },
354 { "power4", PPC_PLATFORM_POWER4 },
355 { "ppca2", PPC_PLATFORM_PPCA2 },
356 { "ppc476", PPC_PLATFORM_PPC476 },
357 { "ppc464", PPC_PLATFORM_PPC464 },
358 { "ppc440", PPC_PLATFORM_PPC440 },
359 { "ppc405", PPC_PLATFORM_PPC405 },
360 { "ppc-cell-be", PPC_PLATFORM_CELL_BE }
363 /* Used by __builtin_cpu_supports(), mapping from HWCAP names to masks. */
364 static const struct
366 const char *hwcap;
367 int mask;
368 unsigned int id;
369 } cpu_supports_info[] = {
370 /* AT_HWCAP masks. */
371 { "4xxmac", PPC_FEATURE_HAS_4xxMAC, 0 },
372 { "altivec", PPC_FEATURE_HAS_ALTIVEC, 0 },
373 { "arch_2_05", PPC_FEATURE_ARCH_2_05, 0 },
374 { "arch_2_06", PPC_FEATURE_ARCH_2_06, 0 },
375 { "archpmu", PPC_FEATURE_PERFMON_COMPAT, 0 },
376 { "booke", PPC_FEATURE_BOOKE, 0 },
377 { "cellbe", PPC_FEATURE_CELL_BE, 0 },
378 { "dfp", PPC_FEATURE_HAS_DFP, 0 },
379 { "efpdouble", PPC_FEATURE_HAS_EFP_DOUBLE, 0 },
380 { "efpsingle", PPC_FEATURE_HAS_EFP_SINGLE, 0 },
381 { "fpu", PPC_FEATURE_HAS_FPU, 0 },
382 { "ic_snoop", PPC_FEATURE_ICACHE_SNOOP, 0 },
383 { "mmu", PPC_FEATURE_HAS_MMU, 0 },
384 { "notb", PPC_FEATURE_NO_TB, 0 },
385 { "pa6t", PPC_FEATURE_PA6T, 0 },
386 { "power4", PPC_FEATURE_POWER4, 0 },
387 { "power5", PPC_FEATURE_POWER5, 0 },
388 { "power5+", PPC_FEATURE_POWER5_PLUS, 0 },
389 { "power6x", PPC_FEATURE_POWER6_EXT, 0 },
390 { "ppc32", PPC_FEATURE_32, 0 },
391 { "ppc601", PPC_FEATURE_601_INSTR, 0 },
392 { "ppc64", PPC_FEATURE_64, 0 },
393 { "ppcle", PPC_FEATURE_PPC_LE, 0 },
394 { "smt", PPC_FEATURE_SMT, 0 },
395 { "spe", PPC_FEATURE_HAS_SPE, 0 },
396 { "true_le", PPC_FEATURE_TRUE_LE, 0 },
397 { "ucache", PPC_FEATURE_UNIFIED_CACHE, 0 },
398 { "vsx", PPC_FEATURE_HAS_VSX, 0 },
400 /* AT_HWCAP2 masks. */
401 { "arch_2_07", PPC_FEATURE2_ARCH_2_07, 1 },
402 { "dscr", PPC_FEATURE2_HAS_DSCR, 1 },
403 { "ebb", PPC_FEATURE2_HAS_EBB, 1 },
404 { "htm", PPC_FEATURE2_HAS_HTM, 1 },
405 { "htm-nosc", PPC_FEATURE2_HTM_NOSC, 1 },
406 { "htm-no-suspend", PPC_FEATURE2_HTM_NO_SUSPEND, 1 },
407 { "isel", PPC_FEATURE2_HAS_ISEL, 1 },
408 { "tar", PPC_FEATURE2_HAS_TAR, 1 },
409 { "vcrypto", PPC_FEATURE2_HAS_VEC_CRYPTO, 1 },
410 { "arch_3_00", PPC_FEATURE2_ARCH_3_00, 1 },
411 { "ieee128", PPC_FEATURE2_HAS_IEEE128, 1 },
412 { "darn", PPC_FEATURE2_DARN, 1 },
413 { "scv", PPC_FEATURE2_SCV, 1 }
416 /* On PowerPC, we have a limited number of target clones that we care about
417 which means we can use an array to hold the options, rather than having more
418 elaborate data structures to identify each possible variation. Order the
419 clones from the default to the highest ISA. */
420 enum {
421 CLONE_DEFAULT = 0, /* default clone. */
422 CLONE_ISA_2_05, /* ISA 2.05 (power6). */
423 CLONE_ISA_2_06, /* ISA 2.06 (power7). */
424 CLONE_ISA_2_07, /* ISA 2.07 (power8). */
425 CLONE_ISA_3_00, /* ISA 3.00 (power9). */
426 CLONE_MAX
429 /* Map compiler ISA bits into HWCAP names. */
430 struct clone_map {
431 HOST_WIDE_INT isa_mask; /* rs6000_isa mask */
432 const char *name; /* name to use in __builtin_cpu_supports. */
435 static const struct clone_map rs6000_clone_map[CLONE_MAX] = {
436 { 0, "" }, /* Default options. */
437 { OPTION_MASK_CMPB, "arch_2_05" }, /* ISA 2.05 (power6). */
438 { OPTION_MASK_POPCNTD, "arch_2_06" }, /* ISA 2.06 (power7). */
439 { OPTION_MASK_P8_VECTOR, "arch_2_07" }, /* ISA 2.07 (power8). */
440 { OPTION_MASK_P9_VECTOR, "arch_3_00" }, /* ISA 3.00 (power9). */
444 /* Newer LIBCs explicitly export this symbol to declare that they provide
445 the AT_PLATFORM and AT_HWCAP/AT_HWCAP2 values in the TCB. We emit a
446 reference to this symbol whenever we expand a CPU builtin, so that
447 we never link against an old LIBC. */
448 const char *tcb_verification_symbol = "__parse_hwcap_and_convert_at_platform";
450 /* True if we have expanded a CPU builtin. */
451 bool cpu_builtin_p;
453 /* Pointer to function (in rs6000-c.c) that can define or undefine target
454 macros that have changed. Languages that don't support the preprocessor
455 don't link in rs6000-c.c, so we can't call it directly. */
456 void (*rs6000_target_modify_macros_ptr) (bool, HOST_WIDE_INT, HOST_WIDE_INT);
458 /* Simplfy register classes into simpler classifications. We assume
459 GPR_REG_TYPE - FPR_REG_TYPE are ordered so that we can use a simple range
460 check for standard register classes (gpr/floating/altivec/vsx) and
461 floating/vector classes (float/altivec/vsx). */
463 enum rs6000_reg_type {
464 NO_REG_TYPE,
465 PSEUDO_REG_TYPE,
466 GPR_REG_TYPE,
467 VSX_REG_TYPE,
468 ALTIVEC_REG_TYPE,
469 FPR_REG_TYPE,
470 SPR_REG_TYPE,
471 CR_REG_TYPE
474 /* Map register class to register type. */
475 static enum rs6000_reg_type reg_class_to_reg_type[N_REG_CLASSES];
477 /* First/last register type for the 'normal' register types (i.e. general
478 purpose, floating point, altivec, and VSX registers). */
479 #define IS_STD_REG_TYPE(RTYPE) IN_RANGE(RTYPE, GPR_REG_TYPE, FPR_REG_TYPE)
481 #define IS_FP_VECT_REG_TYPE(RTYPE) IN_RANGE(RTYPE, VSX_REG_TYPE, FPR_REG_TYPE)
484 /* Register classes we care about in secondary reload or go if legitimate
485 address. We only need to worry about GPR, FPR, and Altivec registers here,
486 along an ANY field that is the OR of the 3 register classes. */
488 enum rs6000_reload_reg_type {
489 RELOAD_REG_GPR, /* General purpose registers. */
490 RELOAD_REG_FPR, /* Traditional floating point regs. */
491 RELOAD_REG_VMX, /* Altivec (VMX) registers. */
492 RELOAD_REG_ANY, /* OR of GPR, FPR, Altivec masks. */
493 N_RELOAD_REG
496 /* For setting up register classes, loop through the 3 register classes mapping
497 into real registers, and skip the ANY class, which is just an OR of the
498 bits. */
499 #define FIRST_RELOAD_REG_CLASS RELOAD_REG_GPR
500 #define LAST_RELOAD_REG_CLASS RELOAD_REG_VMX
502 /* Map reload register type to a register in the register class. */
503 struct reload_reg_map_type {
504 const char *name; /* Register class name. */
505 int reg; /* Register in the register class. */
508 static const struct reload_reg_map_type reload_reg_map[N_RELOAD_REG] = {
509 { "Gpr", FIRST_GPR_REGNO }, /* RELOAD_REG_GPR. */
510 { "Fpr", FIRST_FPR_REGNO }, /* RELOAD_REG_FPR. */
511 { "VMX", FIRST_ALTIVEC_REGNO }, /* RELOAD_REG_VMX. */
512 { "Any", -1 }, /* RELOAD_REG_ANY. */
515 /* Mask bits for each register class, indexed per mode. Historically the
516 compiler has been more restrictive which types can do PRE_MODIFY instead of
517 PRE_INC and PRE_DEC, so keep track of sepaate bits for these two. */
518 typedef unsigned char addr_mask_type;
520 #define RELOAD_REG_VALID 0x01 /* Mode valid in register.. */
521 #define RELOAD_REG_MULTIPLE 0x02 /* Mode takes multiple registers. */
522 #define RELOAD_REG_INDEXED 0x04 /* Reg+reg addressing. */
523 #define RELOAD_REG_OFFSET 0x08 /* Reg+offset addressing. */
524 #define RELOAD_REG_PRE_INCDEC 0x10 /* PRE_INC/PRE_DEC valid. */
525 #define RELOAD_REG_PRE_MODIFY 0x20 /* PRE_MODIFY valid. */
526 #define RELOAD_REG_AND_M16 0x40 /* AND -16 addressing. */
527 #define RELOAD_REG_QUAD_OFFSET 0x80 /* quad offset is limited. */
529 /* Register type masks based on the type, of valid addressing modes. */
530 struct rs6000_reg_addr {
531 enum insn_code reload_load; /* INSN to reload for loading. */
532 enum insn_code reload_store; /* INSN to reload for storing. */
533 enum insn_code reload_fpr_gpr; /* INSN to move from FPR to GPR. */
534 enum insn_code reload_gpr_vsx; /* INSN to move from GPR to VSX. */
535 enum insn_code reload_vsx_gpr; /* INSN to move from VSX to GPR. */
536 addr_mask_type addr_mask[(int)N_RELOAD_REG]; /* Valid address masks. */
537 bool scalar_in_vmx_p; /* Scalar value can go in VMX. */
540 static struct rs6000_reg_addr reg_addr[NUM_MACHINE_MODES];
542 /* Helper function to say whether a mode supports PRE_INC or PRE_DEC. */
543 static inline bool
544 mode_supports_pre_incdec_p (machine_mode mode)
546 return ((reg_addr[mode].addr_mask[RELOAD_REG_ANY] & RELOAD_REG_PRE_INCDEC)
547 != 0);
550 /* Helper function to say whether a mode supports PRE_MODIFY. */
551 static inline bool
552 mode_supports_pre_modify_p (machine_mode mode)
554 return ((reg_addr[mode].addr_mask[RELOAD_REG_ANY] & RELOAD_REG_PRE_MODIFY)
555 != 0);
558 /* Return true if we have D-form addressing in altivec registers. */
559 static inline bool
560 mode_supports_vmx_dform (machine_mode mode)
562 return ((reg_addr[mode].addr_mask[RELOAD_REG_VMX] & RELOAD_REG_OFFSET) != 0);
565 /* Return true if we have D-form addressing in VSX registers. This addressing
566 is more limited than normal d-form addressing in that the offset must be
567 aligned on a 16-byte boundary. */
568 static inline bool
569 mode_supports_dq_form (machine_mode mode)
571 return ((reg_addr[mode].addr_mask[RELOAD_REG_ANY] & RELOAD_REG_QUAD_OFFSET)
572 != 0);
575 /* Given that there exists at least one variable that is set (produced)
576 by OUT_INSN and read (consumed) by IN_INSN, return true iff
577 IN_INSN represents one or more memory store operations and none of
578 the variables set by OUT_INSN is used by IN_INSN as the address of a
579 store operation. If either IN_INSN or OUT_INSN does not represent
580 a "single" RTL SET expression (as loosely defined by the
581 implementation of the single_set function) or a PARALLEL with only
582 SETs, CLOBBERs, and USEs inside, this function returns false.
584 This rs6000-specific version of store_data_bypass_p checks for
585 certain conditions that result in assertion failures (and internal
586 compiler errors) in the generic store_data_bypass_p function and
587 returns false rather than calling store_data_bypass_p if one of the
588 problematic conditions is detected. */
591 rs6000_store_data_bypass_p (rtx_insn *out_insn, rtx_insn *in_insn)
593 rtx out_set, in_set;
594 rtx out_pat, in_pat;
595 rtx out_exp, in_exp;
596 int i, j;
598 in_set = single_set (in_insn);
599 if (in_set)
601 if (MEM_P (SET_DEST (in_set)))
603 out_set = single_set (out_insn);
604 if (!out_set)
606 out_pat = PATTERN (out_insn);
607 if (GET_CODE (out_pat) == PARALLEL)
609 for (i = 0; i < XVECLEN (out_pat, 0); i++)
611 out_exp = XVECEXP (out_pat, 0, i);
612 if ((GET_CODE (out_exp) == CLOBBER)
613 || (GET_CODE (out_exp) == USE))
614 continue;
615 else if (GET_CODE (out_exp) != SET)
616 return false;
622 else
624 in_pat = PATTERN (in_insn);
625 if (GET_CODE (in_pat) != PARALLEL)
626 return false;
628 for (i = 0; i < XVECLEN (in_pat, 0); i++)
630 in_exp = XVECEXP (in_pat, 0, i);
631 if ((GET_CODE (in_exp) == CLOBBER) || (GET_CODE (in_exp) == USE))
632 continue;
633 else if (GET_CODE (in_exp) != SET)
634 return false;
636 if (MEM_P (SET_DEST (in_exp)))
638 out_set = single_set (out_insn);
639 if (!out_set)
641 out_pat = PATTERN (out_insn);
642 if (GET_CODE (out_pat) != PARALLEL)
643 return false;
644 for (j = 0; j < XVECLEN (out_pat, 0); j++)
646 out_exp = XVECEXP (out_pat, 0, j);
647 if ((GET_CODE (out_exp) == CLOBBER)
648 || (GET_CODE (out_exp) == USE))
649 continue;
650 else if (GET_CODE (out_exp) != SET)
651 return false;
657 return store_data_bypass_p (out_insn, in_insn);
661 /* Processor costs (relative to an add) */
663 const struct processor_costs *rs6000_cost;
665 /* Instruction size costs on 32bit processors. */
666 static const
667 struct processor_costs size32_cost = {
668 COSTS_N_INSNS (1), /* mulsi */
669 COSTS_N_INSNS (1), /* mulsi_const */
670 COSTS_N_INSNS (1), /* mulsi_const9 */
671 COSTS_N_INSNS (1), /* muldi */
672 COSTS_N_INSNS (1), /* divsi */
673 COSTS_N_INSNS (1), /* divdi */
674 COSTS_N_INSNS (1), /* fp */
675 COSTS_N_INSNS (1), /* dmul */
676 COSTS_N_INSNS (1), /* sdiv */
677 COSTS_N_INSNS (1), /* ddiv */
678 32, /* cache line size */
679 0, /* l1 cache */
680 0, /* l2 cache */
681 0, /* streams */
682 0, /* SF->DF convert */
685 /* Instruction size costs on 64bit processors. */
686 static const
687 struct processor_costs size64_cost = {
688 COSTS_N_INSNS (1), /* mulsi */
689 COSTS_N_INSNS (1), /* mulsi_const */
690 COSTS_N_INSNS (1), /* mulsi_const9 */
691 COSTS_N_INSNS (1), /* muldi */
692 COSTS_N_INSNS (1), /* divsi */
693 COSTS_N_INSNS (1), /* divdi */
694 COSTS_N_INSNS (1), /* fp */
695 COSTS_N_INSNS (1), /* dmul */
696 COSTS_N_INSNS (1), /* sdiv */
697 COSTS_N_INSNS (1), /* ddiv */
698 128, /* cache line size */
699 0, /* l1 cache */
700 0, /* l2 cache */
701 0, /* streams */
702 0, /* SF->DF convert */
705 /* Instruction costs on RS64A processors. */
706 static const
707 struct processor_costs rs64a_cost = {
708 COSTS_N_INSNS (20), /* mulsi */
709 COSTS_N_INSNS (12), /* mulsi_const */
710 COSTS_N_INSNS (8), /* mulsi_const9 */
711 COSTS_N_INSNS (34), /* muldi */
712 COSTS_N_INSNS (65), /* divsi */
713 COSTS_N_INSNS (67), /* divdi */
714 COSTS_N_INSNS (4), /* fp */
715 COSTS_N_INSNS (4), /* dmul */
716 COSTS_N_INSNS (31), /* sdiv */
717 COSTS_N_INSNS (31), /* ddiv */
718 128, /* cache line size */
719 128, /* l1 cache */
720 2048, /* l2 cache */
721 1, /* streams */
722 0, /* SF->DF convert */
725 /* Instruction costs on MPCCORE processors. */
726 static const
727 struct processor_costs mpccore_cost = {
728 COSTS_N_INSNS (2), /* mulsi */
729 COSTS_N_INSNS (2), /* mulsi_const */
730 COSTS_N_INSNS (2), /* mulsi_const9 */
731 COSTS_N_INSNS (2), /* muldi */
732 COSTS_N_INSNS (6), /* divsi */
733 COSTS_N_INSNS (6), /* divdi */
734 COSTS_N_INSNS (4), /* fp */
735 COSTS_N_INSNS (5), /* dmul */
736 COSTS_N_INSNS (10), /* sdiv */
737 COSTS_N_INSNS (17), /* ddiv */
738 32, /* cache line size */
739 4, /* l1 cache */
740 16, /* l2 cache */
741 1, /* streams */
742 0, /* SF->DF convert */
745 /* Instruction costs on PPC403 processors. */
746 static const
747 struct processor_costs ppc403_cost = {
748 COSTS_N_INSNS (4), /* mulsi */
749 COSTS_N_INSNS (4), /* mulsi_const */
750 COSTS_N_INSNS (4), /* mulsi_const9 */
751 COSTS_N_INSNS (4), /* muldi */
752 COSTS_N_INSNS (33), /* divsi */
753 COSTS_N_INSNS (33), /* divdi */
754 COSTS_N_INSNS (11), /* fp */
755 COSTS_N_INSNS (11), /* dmul */
756 COSTS_N_INSNS (11), /* sdiv */
757 COSTS_N_INSNS (11), /* ddiv */
758 32, /* cache line size */
759 4, /* l1 cache */
760 16, /* l2 cache */
761 1, /* streams */
762 0, /* SF->DF convert */
765 /* Instruction costs on PPC405 processors. */
766 static const
767 struct processor_costs ppc405_cost = {
768 COSTS_N_INSNS (5), /* mulsi */
769 COSTS_N_INSNS (4), /* mulsi_const */
770 COSTS_N_INSNS (3), /* mulsi_const9 */
771 COSTS_N_INSNS (5), /* muldi */
772 COSTS_N_INSNS (35), /* divsi */
773 COSTS_N_INSNS (35), /* divdi */
774 COSTS_N_INSNS (11), /* fp */
775 COSTS_N_INSNS (11), /* dmul */
776 COSTS_N_INSNS (11), /* sdiv */
777 COSTS_N_INSNS (11), /* ddiv */
778 32, /* cache line size */
779 16, /* l1 cache */
780 128, /* l2 cache */
781 1, /* streams */
782 0, /* SF->DF convert */
785 /* Instruction costs on PPC440 processors. */
786 static const
787 struct processor_costs ppc440_cost = {
788 COSTS_N_INSNS (3), /* mulsi */
789 COSTS_N_INSNS (2), /* mulsi_const */
790 COSTS_N_INSNS (2), /* mulsi_const9 */
791 COSTS_N_INSNS (3), /* muldi */
792 COSTS_N_INSNS (34), /* divsi */
793 COSTS_N_INSNS (34), /* divdi */
794 COSTS_N_INSNS (5), /* fp */
795 COSTS_N_INSNS (5), /* dmul */
796 COSTS_N_INSNS (19), /* sdiv */
797 COSTS_N_INSNS (33), /* ddiv */
798 32, /* cache line size */
799 32, /* l1 cache */
800 256, /* l2 cache */
801 1, /* streams */
802 0, /* SF->DF convert */
805 /* Instruction costs on PPC476 processors. */
806 static const
807 struct processor_costs ppc476_cost = {
808 COSTS_N_INSNS (4), /* mulsi */
809 COSTS_N_INSNS (4), /* mulsi_const */
810 COSTS_N_INSNS (4), /* mulsi_const9 */
811 COSTS_N_INSNS (4), /* muldi */
812 COSTS_N_INSNS (11), /* divsi */
813 COSTS_N_INSNS (11), /* divdi */
814 COSTS_N_INSNS (6), /* fp */
815 COSTS_N_INSNS (6), /* dmul */
816 COSTS_N_INSNS (19), /* sdiv */
817 COSTS_N_INSNS (33), /* ddiv */
818 32, /* l1 cache line size */
819 32, /* l1 cache */
820 512, /* l2 cache */
821 1, /* streams */
822 0, /* SF->DF convert */
825 /* Instruction costs on PPC601 processors. */
826 static const
827 struct processor_costs ppc601_cost = {
828 COSTS_N_INSNS (5), /* mulsi */
829 COSTS_N_INSNS (5), /* mulsi_const */
830 COSTS_N_INSNS (5), /* mulsi_const9 */
831 COSTS_N_INSNS (5), /* muldi */
832 COSTS_N_INSNS (36), /* divsi */
833 COSTS_N_INSNS (36), /* divdi */
834 COSTS_N_INSNS (4), /* fp */
835 COSTS_N_INSNS (5), /* dmul */
836 COSTS_N_INSNS (17), /* sdiv */
837 COSTS_N_INSNS (31), /* ddiv */
838 32, /* cache line size */
839 32, /* l1 cache */
840 256, /* l2 cache */
841 1, /* streams */
842 0, /* SF->DF convert */
845 /* Instruction costs on PPC603 processors. */
846 static const
847 struct processor_costs ppc603_cost = {
848 COSTS_N_INSNS (5), /* mulsi */
849 COSTS_N_INSNS (3), /* mulsi_const */
850 COSTS_N_INSNS (2), /* mulsi_const9 */
851 COSTS_N_INSNS (5), /* muldi */
852 COSTS_N_INSNS (37), /* divsi */
853 COSTS_N_INSNS (37), /* divdi */
854 COSTS_N_INSNS (3), /* fp */
855 COSTS_N_INSNS (4), /* dmul */
856 COSTS_N_INSNS (18), /* sdiv */
857 COSTS_N_INSNS (33), /* ddiv */
858 32, /* cache line size */
859 8, /* l1 cache */
860 64, /* l2 cache */
861 1, /* streams */
862 0, /* SF->DF convert */
865 /* Instruction costs on PPC604 processors. */
866 static const
867 struct processor_costs ppc604_cost = {
868 COSTS_N_INSNS (4), /* mulsi */
869 COSTS_N_INSNS (4), /* mulsi_const */
870 COSTS_N_INSNS (4), /* mulsi_const9 */
871 COSTS_N_INSNS (4), /* muldi */
872 COSTS_N_INSNS (20), /* divsi */
873 COSTS_N_INSNS (20), /* divdi */
874 COSTS_N_INSNS (3), /* fp */
875 COSTS_N_INSNS (3), /* dmul */
876 COSTS_N_INSNS (18), /* sdiv */
877 COSTS_N_INSNS (32), /* ddiv */
878 32, /* cache line size */
879 16, /* l1 cache */
880 512, /* l2 cache */
881 1, /* streams */
882 0, /* SF->DF convert */
885 /* Instruction costs on PPC604e processors. */
886 static const
887 struct processor_costs ppc604e_cost = {
888 COSTS_N_INSNS (2), /* mulsi */
889 COSTS_N_INSNS (2), /* mulsi_const */
890 COSTS_N_INSNS (2), /* mulsi_const9 */
891 COSTS_N_INSNS (2), /* muldi */
892 COSTS_N_INSNS (20), /* divsi */
893 COSTS_N_INSNS (20), /* divdi */
894 COSTS_N_INSNS (3), /* fp */
895 COSTS_N_INSNS (3), /* dmul */
896 COSTS_N_INSNS (18), /* sdiv */
897 COSTS_N_INSNS (32), /* ddiv */
898 32, /* cache line size */
899 32, /* l1 cache */
900 1024, /* l2 cache */
901 1, /* streams */
902 0, /* SF->DF convert */
905 /* Instruction costs on PPC620 processors. */
906 static const
907 struct processor_costs ppc620_cost = {
908 COSTS_N_INSNS (5), /* mulsi */
909 COSTS_N_INSNS (4), /* mulsi_const */
910 COSTS_N_INSNS (3), /* mulsi_const9 */
911 COSTS_N_INSNS (7), /* muldi */
912 COSTS_N_INSNS (21), /* divsi */
913 COSTS_N_INSNS (37), /* divdi */
914 COSTS_N_INSNS (3), /* fp */
915 COSTS_N_INSNS (3), /* dmul */
916 COSTS_N_INSNS (18), /* sdiv */
917 COSTS_N_INSNS (32), /* ddiv */
918 128, /* cache line size */
919 32, /* l1 cache */
920 1024, /* l2 cache */
921 1, /* streams */
922 0, /* SF->DF convert */
925 /* Instruction costs on PPC630 processors. */
926 static const
927 struct processor_costs ppc630_cost = {
928 COSTS_N_INSNS (5), /* mulsi */
929 COSTS_N_INSNS (4), /* mulsi_const */
930 COSTS_N_INSNS (3), /* mulsi_const9 */
931 COSTS_N_INSNS (7), /* muldi */
932 COSTS_N_INSNS (21), /* divsi */
933 COSTS_N_INSNS (37), /* divdi */
934 COSTS_N_INSNS (3), /* fp */
935 COSTS_N_INSNS (3), /* dmul */
936 COSTS_N_INSNS (17), /* sdiv */
937 COSTS_N_INSNS (21), /* ddiv */
938 128, /* cache line size */
939 64, /* l1 cache */
940 1024, /* l2 cache */
941 1, /* streams */
942 0, /* SF->DF convert */
945 /* Instruction costs on Cell processor. */
946 /* COSTS_N_INSNS (1) ~ one add. */
947 static const
948 struct processor_costs ppccell_cost = {
949 COSTS_N_INSNS (9/2)+2, /* mulsi */
950 COSTS_N_INSNS (6/2), /* mulsi_const */
951 COSTS_N_INSNS (6/2), /* mulsi_const9 */
952 COSTS_N_INSNS (15/2)+2, /* muldi */
953 COSTS_N_INSNS (38/2), /* divsi */
954 COSTS_N_INSNS (70/2), /* divdi */
955 COSTS_N_INSNS (10/2), /* fp */
956 COSTS_N_INSNS (10/2), /* dmul */
957 COSTS_N_INSNS (74/2), /* sdiv */
958 COSTS_N_INSNS (74/2), /* ddiv */
959 128, /* cache line size */
960 32, /* l1 cache */
961 512, /* l2 cache */
962 6, /* streams */
963 0, /* SF->DF convert */
966 /* Instruction costs on PPC750 and PPC7400 processors. */
967 static const
968 struct processor_costs ppc750_cost = {
969 COSTS_N_INSNS (5), /* mulsi */
970 COSTS_N_INSNS (3), /* mulsi_const */
971 COSTS_N_INSNS (2), /* mulsi_const9 */
972 COSTS_N_INSNS (5), /* muldi */
973 COSTS_N_INSNS (17), /* divsi */
974 COSTS_N_INSNS (17), /* divdi */
975 COSTS_N_INSNS (3), /* fp */
976 COSTS_N_INSNS (3), /* dmul */
977 COSTS_N_INSNS (17), /* sdiv */
978 COSTS_N_INSNS (31), /* ddiv */
979 32, /* cache line size */
980 32, /* l1 cache */
981 512, /* l2 cache */
982 1, /* streams */
983 0, /* SF->DF convert */
986 /* Instruction costs on PPC7450 processors. */
987 static const
988 struct processor_costs ppc7450_cost = {
989 COSTS_N_INSNS (4), /* mulsi */
990 COSTS_N_INSNS (3), /* mulsi_const */
991 COSTS_N_INSNS (3), /* mulsi_const9 */
992 COSTS_N_INSNS (4), /* muldi */
993 COSTS_N_INSNS (23), /* divsi */
994 COSTS_N_INSNS (23), /* divdi */
995 COSTS_N_INSNS (5), /* fp */
996 COSTS_N_INSNS (5), /* dmul */
997 COSTS_N_INSNS (21), /* sdiv */
998 COSTS_N_INSNS (35), /* ddiv */
999 32, /* cache line size */
1000 32, /* l1 cache */
1001 1024, /* l2 cache */
1002 1, /* streams */
1003 0, /* SF->DF convert */
1006 /* Instruction costs on PPC8540 processors. */
1007 static const
1008 struct processor_costs ppc8540_cost = {
1009 COSTS_N_INSNS (4), /* mulsi */
1010 COSTS_N_INSNS (4), /* mulsi_const */
1011 COSTS_N_INSNS (4), /* mulsi_const9 */
1012 COSTS_N_INSNS (4), /* muldi */
1013 COSTS_N_INSNS (19), /* divsi */
1014 COSTS_N_INSNS (19), /* divdi */
1015 COSTS_N_INSNS (4), /* fp */
1016 COSTS_N_INSNS (4), /* dmul */
1017 COSTS_N_INSNS (29), /* sdiv */
1018 COSTS_N_INSNS (29), /* ddiv */
1019 32, /* cache line size */
1020 32, /* l1 cache */
1021 256, /* l2 cache */
1022 1, /* prefetch streams /*/
1023 0, /* SF->DF convert */
1026 /* Instruction costs on E300C2 and E300C3 cores. */
1027 static const
1028 struct processor_costs ppce300c2c3_cost = {
1029 COSTS_N_INSNS (4), /* mulsi */
1030 COSTS_N_INSNS (4), /* mulsi_const */
1031 COSTS_N_INSNS (4), /* mulsi_const9 */
1032 COSTS_N_INSNS (4), /* muldi */
1033 COSTS_N_INSNS (19), /* divsi */
1034 COSTS_N_INSNS (19), /* divdi */
1035 COSTS_N_INSNS (3), /* fp */
1036 COSTS_N_INSNS (4), /* dmul */
1037 COSTS_N_INSNS (18), /* sdiv */
1038 COSTS_N_INSNS (33), /* ddiv */
1040 16, /* l1 cache */
1041 16, /* l2 cache */
1042 1, /* prefetch streams /*/
1043 0, /* SF->DF convert */
1046 /* Instruction costs on PPCE500MC processors. */
1047 static const
1048 struct processor_costs ppce500mc_cost = {
1049 COSTS_N_INSNS (4), /* mulsi */
1050 COSTS_N_INSNS (4), /* mulsi_const */
1051 COSTS_N_INSNS (4), /* mulsi_const9 */
1052 COSTS_N_INSNS (4), /* muldi */
1053 COSTS_N_INSNS (14), /* divsi */
1054 COSTS_N_INSNS (14), /* divdi */
1055 COSTS_N_INSNS (8), /* fp */
1056 COSTS_N_INSNS (10), /* dmul */
1057 COSTS_N_INSNS (36), /* sdiv */
1058 COSTS_N_INSNS (66), /* ddiv */
1059 64, /* cache line size */
1060 32, /* l1 cache */
1061 128, /* l2 cache */
1062 1, /* prefetch streams /*/
1063 0, /* SF->DF convert */
1066 /* Instruction costs on PPCE500MC64 processors. */
1067 static const
1068 struct processor_costs ppce500mc64_cost = {
1069 COSTS_N_INSNS (4), /* mulsi */
1070 COSTS_N_INSNS (4), /* mulsi_const */
1071 COSTS_N_INSNS (4), /* mulsi_const9 */
1072 COSTS_N_INSNS (4), /* muldi */
1073 COSTS_N_INSNS (14), /* divsi */
1074 COSTS_N_INSNS (14), /* divdi */
1075 COSTS_N_INSNS (4), /* fp */
1076 COSTS_N_INSNS (10), /* dmul */
1077 COSTS_N_INSNS (36), /* sdiv */
1078 COSTS_N_INSNS (66), /* ddiv */
1079 64, /* cache line size */
1080 32, /* l1 cache */
1081 128, /* l2 cache */
1082 1, /* prefetch streams /*/
1083 0, /* SF->DF convert */
1086 /* Instruction costs on PPCE5500 processors. */
1087 static const
1088 struct processor_costs ppce5500_cost = {
1089 COSTS_N_INSNS (5), /* mulsi */
1090 COSTS_N_INSNS (5), /* mulsi_const */
1091 COSTS_N_INSNS (4), /* mulsi_const9 */
1092 COSTS_N_INSNS (5), /* muldi */
1093 COSTS_N_INSNS (14), /* divsi */
1094 COSTS_N_INSNS (14), /* divdi */
1095 COSTS_N_INSNS (7), /* fp */
1096 COSTS_N_INSNS (10), /* dmul */
1097 COSTS_N_INSNS (36), /* sdiv */
1098 COSTS_N_INSNS (66), /* ddiv */
1099 64, /* cache line size */
1100 32, /* l1 cache */
1101 128, /* l2 cache */
1102 1, /* prefetch streams /*/
1103 0, /* SF->DF convert */
1106 /* Instruction costs on PPCE6500 processors. */
1107 static const
1108 struct processor_costs ppce6500_cost = {
1109 COSTS_N_INSNS (5), /* mulsi */
1110 COSTS_N_INSNS (5), /* mulsi_const */
1111 COSTS_N_INSNS (4), /* mulsi_const9 */
1112 COSTS_N_INSNS (5), /* muldi */
1113 COSTS_N_INSNS (14), /* divsi */
1114 COSTS_N_INSNS (14), /* divdi */
1115 COSTS_N_INSNS (7), /* fp */
1116 COSTS_N_INSNS (10), /* dmul */
1117 COSTS_N_INSNS (36), /* sdiv */
1118 COSTS_N_INSNS (66), /* ddiv */
1119 64, /* cache line size */
1120 32, /* l1 cache */
1121 128, /* l2 cache */
1122 1, /* prefetch streams /*/
1123 0, /* SF->DF convert */
1126 /* Instruction costs on AppliedMicro Titan processors. */
1127 static const
1128 struct processor_costs titan_cost = {
1129 COSTS_N_INSNS (5), /* mulsi */
1130 COSTS_N_INSNS (5), /* mulsi_const */
1131 COSTS_N_INSNS (5), /* mulsi_const9 */
1132 COSTS_N_INSNS (5), /* muldi */
1133 COSTS_N_INSNS (18), /* divsi */
1134 COSTS_N_INSNS (18), /* divdi */
1135 COSTS_N_INSNS (10), /* fp */
1136 COSTS_N_INSNS (10), /* dmul */
1137 COSTS_N_INSNS (46), /* sdiv */
1138 COSTS_N_INSNS (72), /* ddiv */
1139 32, /* cache line size */
1140 32, /* l1 cache */
1141 512, /* l2 cache */
1142 1, /* prefetch streams /*/
1143 0, /* SF->DF convert */
1146 /* Instruction costs on POWER4 and POWER5 processors. */
1147 static const
1148 struct processor_costs power4_cost = {
1149 COSTS_N_INSNS (3), /* mulsi */
1150 COSTS_N_INSNS (2), /* mulsi_const */
1151 COSTS_N_INSNS (2), /* mulsi_const9 */
1152 COSTS_N_INSNS (4), /* muldi */
1153 COSTS_N_INSNS (18), /* divsi */
1154 COSTS_N_INSNS (34), /* divdi */
1155 COSTS_N_INSNS (3), /* fp */
1156 COSTS_N_INSNS (3), /* dmul */
1157 COSTS_N_INSNS (17), /* sdiv */
1158 COSTS_N_INSNS (17), /* ddiv */
1159 128, /* cache line size */
1160 32, /* l1 cache */
1161 1024, /* l2 cache */
1162 8, /* prefetch streams /*/
1163 0, /* SF->DF convert */
1166 /* Instruction costs on POWER6 processors. */
1167 static const
1168 struct processor_costs power6_cost = {
1169 COSTS_N_INSNS (8), /* mulsi */
1170 COSTS_N_INSNS (8), /* mulsi_const */
1171 COSTS_N_INSNS (8), /* mulsi_const9 */
1172 COSTS_N_INSNS (8), /* muldi */
1173 COSTS_N_INSNS (22), /* divsi */
1174 COSTS_N_INSNS (28), /* divdi */
1175 COSTS_N_INSNS (3), /* fp */
1176 COSTS_N_INSNS (3), /* dmul */
1177 COSTS_N_INSNS (13), /* sdiv */
1178 COSTS_N_INSNS (16), /* ddiv */
1179 128, /* cache line size */
1180 64, /* l1 cache */
1181 2048, /* l2 cache */
1182 16, /* prefetch streams */
1183 0, /* SF->DF convert */
1186 /* Instruction costs on POWER7 processors. */
1187 static const
1188 struct processor_costs power7_cost = {
1189 COSTS_N_INSNS (2), /* mulsi */
1190 COSTS_N_INSNS (2), /* mulsi_const */
1191 COSTS_N_INSNS (2), /* mulsi_const9 */
1192 COSTS_N_INSNS (2), /* muldi */
1193 COSTS_N_INSNS (18), /* divsi */
1194 COSTS_N_INSNS (34), /* divdi */
1195 COSTS_N_INSNS (3), /* fp */
1196 COSTS_N_INSNS (3), /* dmul */
1197 COSTS_N_INSNS (13), /* sdiv */
1198 COSTS_N_INSNS (16), /* ddiv */
1199 128, /* cache line size */
1200 32, /* l1 cache */
1201 256, /* l2 cache */
1202 12, /* prefetch streams */
1203 COSTS_N_INSNS (3), /* SF->DF convert */
1206 /* Instruction costs on POWER8 processors. */
1207 static const
1208 struct processor_costs power8_cost = {
1209 COSTS_N_INSNS (3), /* mulsi */
1210 COSTS_N_INSNS (3), /* mulsi_const */
1211 COSTS_N_INSNS (3), /* mulsi_const9 */
1212 COSTS_N_INSNS (3), /* muldi */
1213 COSTS_N_INSNS (19), /* divsi */
1214 COSTS_N_INSNS (35), /* divdi */
1215 COSTS_N_INSNS (3), /* fp */
1216 COSTS_N_INSNS (3), /* dmul */
1217 COSTS_N_INSNS (14), /* sdiv */
1218 COSTS_N_INSNS (17), /* ddiv */
1219 128, /* cache line size */
1220 32, /* l1 cache */
1221 256, /* l2 cache */
1222 12, /* prefetch streams */
1223 COSTS_N_INSNS (3), /* SF->DF convert */
1226 /* Instruction costs on POWER9 processors. */
1227 static const
1228 struct processor_costs power9_cost = {
1229 COSTS_N_INSNS (3), /* mulsi */
1230 COSTS_N_INSNS (3), /* mulsi_const */
1231 COSTS_N_INSNS (3), /* mulsi_const9 */
1232 COSTS_N_INSNS (3), /* muldi */
1233 COSTS_N_INSNS (8), /* divsi */
1234 COSTS_N_INSNS (12), /* divdi */
1235 COSTS_N_INSNS (3), /* fp */
1236 COSTS_N_INSNS (3), /* dmul */
1237 COSTS_N_INSNS (13), /* sdiv */
1238 COSTS_N_INSNS (18), /* ddiv */
1239 128, /* cache line size */
1240 32, /* l1 cache */
1241 512, /* l2 cache */
1242 8, /* prefetch streams */
1243 COSTS_N_INSNS (3), /* SF->DF convert */
1246 /* Instruction costs on POWER A2 processors. */
1247 static const
1248 struct processor_costs ppca2_cost = {
1249 COSTS_N_INSNS (16), /* mulsi */
1250 COSTS_N_INSNS (16), /* mulsi_const */
1251 COSTS_N_INSNS (16), /* mulsi_const9 */
1252 COSTS_N_INSNS (16), /* muldi */
1253 COSTS_N_INSNS (22), /* divsi */
1254 COSTS_N_INSNS (28), /* divdi */
1255 COSTS_N_INSNS (3), /* fp */
1256 COSTS_N_INSNS (3), /* dmul */
1257 COSTS_N_INSNS (59), /* sdiv */
1258 COSTS_N_INSNS (72), /* ddiv */
1260 16, /* l1 cache */
1261 2048, /* l2 cache */
1262 16, /* prefetch streams */
1263 0, /* SF->DF convert */
1267 /* Table that classifies rs6000 builtin functions (pure, const, etc.). */
1268 #undef RS6000_BUILTIN_0
1269 #undef RS6000_BUILTIN_1
1270 #undef RS6000_BUILTIN_2
1271 #undef RS6000_BUILTIN_3
1272 #undef RS6000_BUILTIN_A
1273 #undef RS6000_BUILTIN_D
1274 #undef RS6000_BUILTIN_H
1275 #undef RS6000_BUILTIN_P
1276 #undef RS6000_BUILTIN_X
1278 #define RS6000_BUILTIN_0(ENUM, NAME, MASK, ATTR, ICODE) \
1279 { NAME, ICODE, MASK, ATTR },
1281 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE) \
1282 { NAME, ICODE, MASK, ATTR },
1284 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE) \
1285 { NAME, ICODE, MASK, ATTR },
1287 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE) \
1288 { NAME, ICODE, MASK, ATTR },
1290 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE) \
1291 { NAME, ICODE, MASK, ATTR },
1293 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE) \
1294 { NAME, ICODE, MASK, ATTR },
1296 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE) \
1297 { NAME, ICODE, MASK, ATTR },
1299 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE) \
1300 { NAME, ICODE, MASK, ATTR },
1302 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE) \
1303 { NAME, ICODE, MASK, ATTR },
1305 struct rs6000_builtin_info_type {
1306 const char *name;
1307 const enum insn_code icode;
1308 const HOST_WIDE_INT mask;
1309 const unsigned attr;
1312 static const struct rs6000_builtin_info_type rs6000_builtin_info[] =
1314 #include "rs6000-builtin.def"
1317 #undef RS6000_BUILTIN_0
1318 #undef RS6000_BUILTIN_1
1319 #undef RS6000_BUILTIN_2
1320 #undef RS6000_BUILTIN_3
1321 #undef RS6000_BUILTIN_A
1322 #undef RS6000_BUILTIN_D
1323 #undef RS6000_BUILTIN_H
1324 #undef RS6000_BUILTIN_P
1325 #undef RS6000_BUILTIN_X
1327 /* Support for -mveclibabi=<xxx> to control which vector library to use. */
1328 static tree (*rs6000_veclib_handler) (combined_fn, tree, tree);
1331 static bool rs6000_debug_legitimate_address_p (machine_mode, rtx, bool);
1332 static struct machine_function * rs6000_init_machine_status (void);
1333 static int rs6000_ra_ever_killed (void);
1334 static tree rs6000_handle_longcall_attribute (tree *, tree, tree, int, bool *);
1335 static tree rs6000_handle_altivec_attribute (tree *, tree, tree, int, bool *);
1336 static tree rs6000_handle_struct_attribute (tree *, tree, tree, int, bool *);
1337 static tree rs6000_builtin_vectorized_libmass (combined_fn, tree, tree);
1338 static void rs6000_emit_set_long_const (rtx, HOST_WIDE_INT);
1339 static int rs6000_memory_move_cost (machine_mode, reg_class_t, bool);
1340 static bool rs6000_debug_rtx_costs (rtx, machine_mode, int, int, int *, bool);
1341 static int rs6000_debug_address_cost (rtx, machine_mode, addr_space_t,
1342 bool);
1343 static int rs6000_debug_adjust_cost (rtx_insn *, int, rtx_insn *, int,
1344 unsigned int);
1345 static bool is_microcoded_insn (rtx_insn *);
1346 static bool is_nonpipeline_insn (rtx_insn *);
1347 static bool is_cracked_insn (rtx_insn *);
1348 static bool is_load_insn (rtx, rtx *);
1349 static bool is_store_insn (rtx, rtx *);
1350 static bool set_to_load_agen (rtx_insn *,rtx_insn *);
1351 static bool insn_terminates_group_p (rtx_insn *, enum group_termination);
1352 static bool insn_must_be_first_in_group (rtx_insn *);
1353 static bool insn_must_be_last_in_group (rtx_insn *);
1354 static void altivec_init_builtins (void);
1355 static tree builtin_function_type (machine_mode, machine_mode,
1356 machine_mode, machine_mode,
1357 enum rs6000_builtins, const char *name);
1358 static void rs6000_common_init_builtins (void);
1359 static void htm_init_builtins (void);
1360 static rs6000_stack_t *rs6000_stack_info (void);
1361 static void is_altivec_return_reg (rtx, void *);
1362 int easy_vector_constant (rtx, machine_mode);
1363 static rtx rs6000_debug_legitimize_address (rtx, rtx, machine_mode);
1364 static rtx rs6000_legitimize_tls_address (rtx, enum tls_model);
1365 static rtx rs6000_darwin64_record_arg (CUMULATIVE_ARGS *, const_tree,
1366 bool, bool);
1367 #if TARGET_MACHO
1368 static void macho_branch_islands (void);
1369 static tree get_prev_label (tree);
1370 #endif
1371 static bool rs6000_mode_dependent_address (const_rtx);
1372 static bool rs6000_debug_mode_dependent_address (const_rtx);
1373 static bool rs6000_offsettable_memref_p (rtx, machine_mode, bool);
1374 static enum reg_class rs6000_secondary_reload_class (enum reg_class,
1375 machine_mode, rtx);
1376 static enum reg_class rs6000_debug_secondary_reload_class (enum reg_class,
1377 machine_mode,
1378 rtx);
1379 static enum reg_class rs6000_preferred_reload_class (rtx, enum reg_class);
1380 static enum reg_class rs6000_debug_preferred_reload_class (rtx,
1381 enum reg_class);
1382 static bool rs6000_debug_secondary_memory_needed (machine_mode,
1383 reg_class_t,
1384 reg_class_t);
1385 static bool rs6000_debug_can_change_mode_class (machine_mode,
1386 machine_mode,
1387 reg_class_t);
1388 static bool rs6000_save_toc_in_prologue_p (void);
1389 static rtx rs6000_internal_arg_pointer (void);
1391 static bool (*rs6000_mode_dependent_address_ptr) (const_rtx)
1392 = rs6000_mode_dependent_address;
1394 enum reg_class (*rs6000_secondary_reload_class_ptr) (enum reg_class,
1395 machine_mode, rtx)
1396 = rs6000_secondary_reload_class;
1398 enum reg_class (*rs6000_preferred_reload_class_ptr) (rtx, enum reg_class)
1399 = rs6000_preferred_reload_class;
1401 const int INSN_NOT_AVAILABLE = -1;
1403 static void rs6000_print_isa_options (FILE *, int, const char *,
1404 HOST_WIDE_INT);
1405 static void rs6000_print_builtin_options (FILE *, int, const char *,
1406 HOST_WIDE_INT);
1407 static HOST_WIDE_INT rs6000_disable_incompatible_switches (void);
1409 static enum rs6000_reg_type register_to_reg_type (rtx, bool *);
1410 static bool rs6000_secondary_reload_move (enum rs6000_reg_type,
1411 enum rs6000_reg_type,
1412 machine_mode,
1413 secondary_reload_info *,
1414 bool);
1415 rtl_opt_pass *make_pass_analyze_swaps (gcc::context*);
1416 static bool rs6000_keep_leaf_when_profiled () __attribute__ ((unused));
1417 static tree rs6000_fold_builtin (tree, int, tree *, bool);
1419 /* Hash table stuff for keeping track of TOC entries. */
1421 struct GTY((for_user)) toc_hash_struct
1423 /* `key' will satisfy CONSTANT_P; in fact, it will satisfy
1424 ASM_OUTPUT_SPECIAL_POOL_ENTRY_P. */
1425 rtx key;
1426 machine_mode key_mode;
1427 int labelno;
1430 struct toc_hasher : ggc_ptr_hash<toc_hash_struct>
1432 static hashval_t hash (toc_hash_struct *);
1433 static bool equal (toc_hash_struct *, toc_hash_struct *);
1436 static GTY (()) hash_table<toc_hasher> *toc_hash_table;
1438 /* Hash table to keep track of the argument types for builtin functions. */
1440 struct GTY((for_user)) builtin_hash_struct
1442 tree type;
1443 machine_mode mode[4]; /* return value + 3 arguments. */
1444 unsigned char uns_p[4]; /* and whether the types are unsigned. */
1447 struct builtin_hasher : ggc_ptr_hash<builtin_hash_struct>
1449 static hashval_t hash (builtin_hash_struct *);
1450 static bool equal (builtin_hash_struct *, builtin_hash_struct *);
1453 static GTY (()) hash_table<builtin_hasher> *builtin_hash_table;
1456 /* Default register names. */
1457 char rs6000_reg_names[][8] =
1459 /* GPRs */
1460 "0", "1", "2", "3", "4", "5", "6", "7",
1461 "8", "9", "10", "11", "12", "13", "14", "15",
1462 "16", "17", "18", "19", "20", "21", "22", "23",
1463 "24", "25", "26", "27", "28", "29", "30", "31",
1464 /* FPRs */
1465 "0", "1", "2", "3", "4", "5", "6", "7",
1466 "8", "9", "10", "11", "12", "13", "14", "15",
1467 "16", "17", "18", "19", "20", "21", "22", "23",
1468 "24", "25", "26", "27", "28", "29", "30", "31",
1469 /* VRs */
1470 "0", "1", "2", "3", "4", "5", "6", "7",
1471 "8", "9", "10", "11", "12", "13", "14", "15",
1472 "16", "17", "18", "19", "20", "21", "22", "23",
1473 "24", "25", "26", "27", "28", "29", "30", "31",
1474 /* lr ctr ca ap */
1475 "lr", "ctr", "ca", "ap",
1476 /* cr0..cr7 */
1477 "0", "1", "2", "3", "4", "5", "6", "7",
1478 /* vrsave vscr sfp */
1479 "vrsave", "vscr", "sfp",
1482 #ifdef TARGET_REGNAMES
1483 static const char alt_reg_names[][8] =
1485 /* GPRs */
1486 "%r0", "%r1", "%r2", "%r3", "%r4", "%r5", "%r6", "%r7",
1487 "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15",
1488 "%r16", "%r17", "%r18", "%r19", "%r20", "%r21", "%r22", "%r23",
1489 "%r24", "%r25", "%r26", "%r27", "%r28", "%r29", "%r30", "%r31",
1490 /* FPRs */
1491 "%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7",
1492 "%f8", "%f9", "%f10", "%f11", "%f12", "%f13", "%f14", "%f15",
1493 "%f16", "%f17", "%f18", "%f19", "%f20", "%f21", "%f22", "%f23",
1494 "%f24", "%f25", "%f26", "%f27", "%f28", "%f29", "%f30", "%f31",
1495 /* VRs */
1496 "%v0", "%v1", "%v2", "%v3", "%v4", "%v5", "%v6", "%v7",
1497 "%v8", "%v9", "%v10", "%v11", "%v12", "%v13", "%v14", "%v15",
1498 "%v16", "%v17", "%v18", "%v19", "%v20", "%v21", "%v22", "%v23",
1499 "%v24", "%v25", "%v26", "%v27", "%v28", "%v29", "%v30", "%v31",
1500 /* lr ctr ca ap */
1501 "lr", "ctr", "ca", "ap",
1502 /* cr0..cr7 */
1503 "%cr0", "%cr1", "%cr2", "%cr3", "%cr4", "%cr5", "%cr6", "%cr7",
1504 /* vrsave vscr sfp */
1505 "vrsave", "vscr", "sfp",
1507 #endif
1509 /* Table of valid machine attributes. */
1511 static const struct attribute_spec rs6000_attribute_table[] =
1513 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
1514 affects_type_identity, handler, exclude } */
1515 { "altivec", 1, 1, false, true, false, false,
1516 rs6000_handle_altivec_attribute, NULL },
1517 { "longcall", 0, 0, false, true, true, false,
1518 rs6000_handle_longcall_attribute, NULL },
1519 { "shortcall", 0, 0, false, true, true, false,
1520 rs6000_handle_longcall_attribute, NULL },
1521 { "ms_struct", 0, 0, false, false, false, false,
1522 rs6000_handle_struct_attribute, NULL },
1523 { "gcc_struct", 0, 0, false, false, false, false,
1524 rs6000_handle_struct_attribute, NULL },
1525 #ifdef SUBTARGET_ATTRIBUTE_TABLE
1526 SUBTARGET_ATTRIBUTE_TABLE,
1527 #endif
1528 { NULL, 0, 0, false, false, false, false, NULL, NULL }
1531 #ifndef TARGET_PROFILE_KERNEL
1532 #define TARGET_PROFILE_KERNEL 0
1533 #endif
1535 /* The VRSAVE bitmask puts bit %v0 as the most significant bit. */
1536 #define ALTIVEC_REG_BIT(REGNO) (0x80000000 >> ((REGNO) - FIRST_ALTIVEC_REGNO))
1538 /* Initialize the GCC target structure. */
1539 #undef TARGET_ATTRIBUTE_TABLE
1540 #define TARGET_ATTRIBUTE_TABLE rs6000_attribute_table
1541 #undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
1542 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES rs6000_set_default_type_attributes
1543 #undef TARGET_ATTRIBUTE_TAKES_IDENTIFIER_P
1544 #define TARGET_ATTRIBUTE_TAKES_IDENTIFIER_P rs6000_attribute_takes_identifier_p
1546 #undef TARGET_ASM_ALIGNED_DI_OP
1547 #define TARGET_ASM_ALIGNED_DI_OP DOUBLE_INT_ASM_OP
1549 /* Default unaligned ops are only provided for ELF. Find the ops needed
1550 for non-ELF systems. */
1551 #ifndef OBJECT_FORMAT_ELF
1552 #if TARGET_XCOFF
1553 /* For XCOFF. rs6000_assemble_integer will handle unaligned DIs on
1554 64-bit targets. */
1555 #undef TARGET_ASM_UNALIGNED_HI_OP
1556 #define TARGET_ASM_UNALIGNED_HI_OP "\t.vbyte\t2,"
1557 #undef TARGET_ASM_UNALIGNED_SI_OP
1558 #define TARGET_ASM_UNALIGNED_SI_OP "\t.vbyte\t4,"
1559 #undef TARGET_ASM_UNALIGNED_DI_OP
1560 #define TARGET_ASM_UNALIGNED_DI_OP "\t.vbyte\t8,"
1561 #else
1562 /* For Darwin. */
1563 #undef TARGET_ASM_UNALIGNED_HI_OP
1564 #define TARGET_ASM_UNALIGNED_HI_OP "\t.short\t"
1565 #undef TARGET_ASM_UNALIGNED_SI_OP
1566 #define TARGET_ASM_UNALIGNED_SI_OP "\t.long\t"
1567 #undef TARGET_ASM_UNALIGNED_DI_OP
1568 #define TARGET_ASM_UNALIGNED_DI_OP "\t.quad\t"
1569 #undef TARGET_ASM_ALIGNED_DI_OP
1570 #define TARGET_ASM_ALIGNED_DI_OP "\t.quad\t"
1571 #endif
1572 #endif
1574 /* This hook deals with fixups for relocatable code and DI-mode objects
1575 in 64-bit code. */
1576 #undef TARGET_ASM_INTEGER
1577 #define TARGET_ASM_INTEGER rs6000_assemble_integer
1579 #if defined (HAVE_GAS_HIDDEN) && !TARGET_MACHO
1580 #undef TARGET_ASM_ASSEMBLE_VISIBILITY
1581 #define TARGET_ASM_ASSEMBLE_VISIBILITY rs6000_assemble_visibility
1582 #endif
1584 #undef TARGET_SET_UP_BY_PROLOGUE
1585 #define TARGET_SET_UP_BY_PROLOGUE rs6000_set_up_by_prologue
1587 #undef TARGET_SHRINK_WRAP_GET_SEPARATE_COMPONENTS
1588 #define TARGET_SHRINK_WRAP_GET_SEPARATE_COMPONENTS rs6000_get_separate_components
1589 #undef TARGET_SHRINK_WRAP_COMPONENTS_FOR_BB
1590 #define TARGET_SHRINK_WRAP_COMPONENTS_FOR_BB rs6000_components_for_bb
1591 #undef TARGET_SHRINK_WRAP_DISQUALIFY_COMPONENTS
1592 #define TARGET_SHRINK_WRAP_DISQUALIFY_COMPONENTS rs6000_disqualify_components
1593 #undef TARGET_SHRINK_WRAP_EMIT_PROLOGUE_COMPONENTS
1594 #define TARGET_SHRINK_WRAP_EMIT_PROLOGUE_COMPONENTS rs6000_emit_prologue_components
1595 #undef TARGET_SHRINK_WRAP_EMIT_EPILOGUE_COMPONENTS
1596 #define TARGET_SHRINK_WRAP_EMIT_EPILOGUE_COMPONENTS rs6000_emit_epilogue_components
1597 #undef TARGET_SHRINK_WRAP_SET_HANDLED_COMPONENTS
1598 #define TARGET_SHRINK_WRAP_SET_HANDLED_COMPONENTS rs6000_set_handled_components
1600 #undef TARGET_EXTRA_LIVE_ON_ENTRY
1601 #define TARGET_EXTRA_LIVE_ON_ENTRY rs6000_live_on_entry
1603 #undef TARGET_INTERNAL_ARG_POINTER
1604 #define TARGET_INTERNAL_ARG_POINTER rs6000_internal_arg_pointer
1606 #undef TARGET_HAVE_TLS
1607 #define TARGET_HAVE_TLS HAVE_AS_TLS
1609 #undef TARGET_CANNOT_FORCE_CONST_MEM
1610 #define TARGET_CANNOT_FORCE_CONST_MEM rs6000_cannot_force_const_mem
1612 #undef TARGET_DELEGITIMIZE_ADDRESS
1613 #define TARGET_DELEGITIMIZE_ADDRESS rs6000_delegitimize_address
1615 #undef TARGET_CONST_NOT_OK_FOR_DEBUG_P
1616 #define TARGET_CONST_NOT_OK_FOR_DEBUG_P rs6000_const_not_ok_for_debug_p
1618 #undef TARGET_LEGITIMATE_COMBINED_INSN
1619 #define TARGET_LEGITIMATE_COMBINED_INSN rs6000_legitimate_combined_insn
1621 #undef TARGET_ASM_FUNCTION_PROLOGUE
1622 #define TARGET_ASM_FUNCTION_PROLOGUE rs6000_output_function_prologue
1623 #undef TARGET_ASM_FUNCTION_EPILOGUE
1624 #define TARGET_ASM_FUNCTION_EPILOGUE rs6000_output_function_epilogue
1626 #undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
1627 #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA rs6000_output_addr_const_extra
1629 #undef TARGET_LEGITIMIZE_ADDRESS
1630 #define TARGET_LEGITIMIZE_ADDRESS rs6000_legitimize_address
1632 #undef TARGET_SCHED_VARIABLE_ISSUE
1633 #define TARGET_SCHED_VARIABLE_ISSUE rs6000_variable_issue
1635 #undef TARGET_SCHED_ISSUE_RATE
1636 #define TARGET_SCHED_ISSUE_RATE rs6000_issue_rate
1637 #undef TARGET_SCHED_ADJUST_COST
1638 #define TARGET_SCHED_ADJUST_COST rs6000_adjust_cost
1639 #undef TARGET_SCHED_ADJUST_PRIORITY
1640 #define TARGET_SCHED_ADJUST_PRIORITY rs6000_adjust_priority
1641 #undef TARGET_SCHED_IS_COSTLY_DEPENDENCE
1642 #define TARGET_SCHED_IS_COSTLY_DEPENDENCE rs6000_is_costly_dependence
1643 #undef TARGET_SCHED_INIT
1644 #define TARGET_SCHED_INIT rs6000_sched_init
1645 #undef TARGET_SCHED_FINISH
1646 #define TARGET_SCHED_FINISH rs6000_sched_finish
1647 #undef TARGET_SCHED_REORDER
1648 #define TARGET_SCHED_REORDER rs6000_sched_reorder
1649 #undef TARGET_SCHED_REORDER2
1650 #define TARGET_SCHED_REORDER2 rs6000_sched_reorder2
1652 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
1653 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD rs6000_use_sched_lookahead
1655 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD
1656 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD rs6000_use_sched_lookahead_guard
1658 #undef TARGET_SCHED_ALLOC_SCHED_CONTEXT
1659 #define TARGET_SCHED_ALLOC_SCHED_CONTEXT rs6000_alloc_sched_context
1660 #undef TARGET_SCHED_INIT_SCHED_CONTEXT
1661 #define TARGET_SCHED_INIT_SCHED_CONTEXT rs6000_init_sched_context
1662 #undef TARGET_SCHED_SET_SCHED_CONTEXT
1663 #define TARGET_SCHED_SET_SCHED_CONTEXT rs6000_set_sched_context
1664 #undef TARGET_SCHED_FREE_SCHED_CONTEXT
1665 #define TARGET_SCHED_FREE_SCHED_CONTEXT rs6000_free_sched_context
1667 #undef TARGET_SCHED_CAN_SPECULATE_INSN
1668 #define TARGET_SCHED_CAN_SPECULATE_INSN rs6000_sched_can_speculate_insn
1670 #undef TARGET_VECTORIZE_BUILTIN_MASK_FOR_LOAD
1671 #define TARGET_VECTORIZE_BUILTIN_MASK_FOR_LOAD rs6000_builtin_mask_for_load
1672 #undef TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT
1673 #define TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT \
1674 rs6000_builtin_support_vector_misalignment
1675 #undef TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE
1676 #define TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE rs6000_vector_alignment_reachable
1677 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST
1678 #define TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST \
1679 rs6000_builtin_vectorization_cost
1680 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
1681 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE \
1682 rs6000_preferred_simd_mode
1683 #undef TARGET_VECTORIZE_INIT_COST
1684 #define TARGET_VECTORIZE_INIT_COST rs6000_init_cost
1685 #undef TARGET_VECTORIZE_ADD_STMT_COST
1686 #define TARGET_VECTORIZE_ADD_STMT_COST rs6000_add_stmt_cost
1687 #undef TARGET_VECTORIZE_FINISH_COST
1688 #define TARGET_VECTORIZE_FINISH_COST rs6000_finish_cost
1689 #undef TARGET_VECTORIZE_DESTROY_COST_DATA
1690 #define TARGET_VECTORIZE_DESTROY_COST_DATA rs6000_destroy_cost_data
1692 #undef TARGET_INIT_BUILTINS
1693 #define TARGET_INIT_BUILTINS rs6000_init_builtins
1694 #undef TARGET_BUILTIN_DECL
1695 #define TARGET_BUILTIN_DECL rs6000_builtin_decl
1697 #undef TARGET_FOLD_BUILTIN
1698 #define TARGET_FOLD_BUILTIN rs6000_fold_builtin
1699 #undef TARGET_GIMPLE_FOLD_BUILTIN
1700 #define TARGET_GIMPLE_FOLD_BUILTIN rs6000_gimple_fold_builtin
1702 #undef TARGET_EXPAND_BUILTIN
1703 #define TARGET_EXPAND_BUILTIN rs6000_expand_builtin
1705 #undef TARGET_MANGLE_TYPE
1706 #define TARGET_MANGLE_TYPE rs6000_mangle_type
1708 #undef TARGET_INIT_LIBFUNCS
1709 #define TARGET_INIT_LIBFUNCS rs6000_init_libfuncs
1711 #if TARGET_MACHO
1712 #undef TARGET_BINDS_LOCAL_P
1713 #define TARGET_BINDS_LOCAL_P darwin_binds_local_p
1714 #endif
1716 #undef TARGET_MS_BITFIELD_LAYOUT_P
1717 #define TARGET_MS_BITFIELD_LAYOUT_P rs6000_ms_bitfield_layout_p
1719 #undef TARGET_ASM_OUTPUT_MI_THUNK
1720 #define TARGET_ASM_OUTPUT_MI_THUNK rs6000_output_mi_thunk
1722 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
1723 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
1725 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
1726 #define TARGET_FUNCTION_OK_FOR_SIBCALL rs6000_function_ok_for_sibcall
1728 #undef TARGET_REGISTER_MOVE_COST
1729 #define TARGET_REGISTER_MOVE_COST rs6000_register_move_cost
1730 #undef TARGET_MEMORY_MOVE_COST
1731 #define TARGET_MEMORY_MOVE_COST rs6000_memory_move_cost
1732 #undef TARGET_IRA_CHANGE_PSEUDO_ALLOCNO_CLASS
1733 #define TARGET_IRA_CHANGE_PSEUDO_ALLOCNO_CLASS \
1734 rs6000_ira_change_pseudo_allocno_class
1735 #undef TARGET_CANNOT_COPY_INSN_P
1736 #define TARGET_CANNOT_COPY_INSN_P rs6000_cannot_copy_insn_p
1737 #undef TARGET_RTX_COSTS
1738 #define TARGET_RTX_COSTS rs6000_rtx_costs
1739 #undef TARGET_ADDRESS_COST
1740 #define TARGET_ADDRESS_COST hook_int_rtx_mode_as_bool_0
1741 #undef TARGET_INSN_COST
1742 #define TARGET_INSN_COST rs6000_insn_cost
1744 #undef TARGET_INIT_DWARF_REG_SIZES_EXTRA
1745 #define TARGET_INIT_DWARF_REG_SIZES_EXTRA rs6000_init_dwarf_reg_sizes_extra
1747 #undef TARGET_PROMOTE_FUNCTION_MODE
1748 #define TARGET_PROMOTE_FUNCTION_MODE rs6000_promote_function_mode
1750 #undef TARGET_RETURN_IN_MEMORY
1751 #define TARGET_RETURN_IN_MEMORY rs6000_return_in_memory
1753 #undef TARGET_RETURN_IN_MSB
1754 #define TARGET_RETURN_IN_MSB rs6000_return_in_msb
1756 #undef TARGET_SETUP_INCOMING_VARARGS
1757 #define TARGET_SETUP_INCOMING_VARARGS setup_incoming_varargs
1759 /* Always strict argument naming on rs6000. */
1760 #undef TARGET_STRICT_ARGUMENT_NAMING
1761 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
1762 #undef TARGET_PRETEND_OUTGOING_VARARGS_NAMED
1763 #define TARGET_PRETEND_OUTGOING_VARARGS_NAMED hook_bool_CUMULATIVE_ARGS_true
1764 #undef TARGET_SPLIT_COMPLEX_ARG
1765 #define TARGET_SPLIT_COMPLEX_ARG hook_bool_const_tree_true
1766 #undef TARGET_MUST_PASS_IN_STACK
1767 #define TARGET_MUST_PASS_IN_STACK rs6000_must_pass_in_stack
1768 #undef TARGET_PASS_BY_REFERENCE
1769 #define TARGET_PASS_BY_REFERENCE rs6000_pass_by_reference
1770 #undef TARGET_ARG_PARTIAL_BYTES
1771 #define TARGET_ARG_PARTIAL_BYTES rs6000_arg_partial_bytes
1772 #undef TARGET_FUNCTION_ARG_ADVANCE
1773 #define TARGET_FUNCTION_ARG_ADVANCE rs6000_function_arg_advance
1774 #undef TARGET_FUNCTION_ARG
1775 #define TARGET_FUNCTION_ARG rs6000_function_arg
1776 #undef TARGET_FUNCTION_ARG_PADDING
1777 #define TARGET_FUNCTION_ARG_PADDING rs6000_function_arg_padding
1778 #undef TARGET_FUNCTION_ARG_BOUNDARY
1779 #define TARGET_FUNCTION_ARG_BOUNDARY rs6000_function_arg_boundary
1781 #undef TARGET_BUILD_BUILTIN_VA_LIST
1782 #define TARGET_BUILD_BUILTIN_VA_LIST rs6000_build_builtin_va_list
1784 #undef TARGET_EXPAND_BUILTIN_VA_START
1785 #define TARGET_EXPAND_BUILTIN_VA_START rs6000_va_start
1787 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
1788 #define TARGET_GIMPLIFY_VA_ARG_EXPR rs6000_gimplify_va_arg
1790 #undef TARGET_EH_RETURN_FILTER_MODE
1791 #define TARGET_EH_RETURN_FILTER_MODE rs6000_eh_return_filter_mode
1793 #undef TARGET_TRANSLATE_MODE_ATTRIBUTE
1794 #define TARGET_TRANSLATE_MODE_ATTRIBUTE rs6000_translate_mode_attribute
1796 #undef TARGET_SCALAR_MODE_SUPPORTED_P
1797 #define TARGET_SCALAR_MODE_SUPPORTED_P rs6000_scalar_mode_supported_p
1799 #undef TARGET_VECTOR_MODE_SUPPORTED_P
1800 #define TARGET_VECTOR_MODE_SUPPORTED_P rs6000_vector_mode_supported_p
1802 #undef TARGET_FLOATN_MODE
1803 #define TARGET_FLOATN_MODE rs6000_floatn_mode
1805 #undef TARGET_INVALID_ARG_FOR_UNPROTOTYPED_FN
1806 #define TARGET_INVALID_ARG_FOR_UNPROTOTYPED_FN invalid_arg_for_unprototyped_fn
1808 #undef TARGET_MD_ASM_ADJUST
1809 #define TARGET_MD_ASM_ADJUST rs6000_md_asm_adjust
1811 #undef TARGET_OPTION_OVERRIDE
1812 #define TARGET_OPTION_OVERRIDE rs6000_option_override
1814 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION
1815 #define TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION \
1816 rs6000_builtin_vectorized_function
1818 #undef TARGET_VECTORIZE_BUILTIN_MD_VECTORIZED_FUNCTION
1819 #define TARGET_VECTORIZE_BUILTIN_MD_VECTORIZED_FUNCTION \
1820 rs6000_builtin_md_vectorized_function
1822 #undef TARGET_STACK_PROTECT_GUARD
1823 #define TARGET_STACK_PROTECT_GUARD rs6000_init_stack_protect_guard
1825 #if !TARGET_MACHO
1826 #undef TARGET_STACK_PROTECT_FAIL
1827 #define TARGET_STACK_PROTECT_FAIL rs6000_stack_protect_fail
1828 #endif
1830 #ifdef HAVE_AS_TLS
1831 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
1832 #define TARGET_ASM_OUTPUT_DWARF_DTPREL rs6000_output_dwarf_dtprel
1833 #endif
1835 /* Use a 32-bit anchor range. This leads to sequences like:
1837 addis tmp,anchor,high
1838 add dest,tmp,low
1840 where tmp itself acts as an anchor, and can be shared between
1841 accesses to the same 64k page. */
1842 #undef TARGET_MIN_ANCHOR_OFFSET
1843 #define TARGET_MIN_ANCHOR_OFFSET -0x7fffffff - 1
1844 #undef TARGET_MAX_ANCHOR_OFFSET
1845 #define TARGET_MAX_ANCHOR_OFFSET 0x7fffffff
1846 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
1847 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P rs6000_use_blocks_for_constant_p
1848 #undef TARGET_USE_BLOCKS_FOR_DECL_P
1849 #define TARGET_USE_BLOCKS_FOR_DECL_P rs6000_use_blocks_for_decl_p
1851 #undef TARGET_BUILTIN_RECIPROCAL
1852 #define TARGET_BUILTIN_RECIPROCAL rs6000_builtin_reciprocal
1854 #undef TARGET_SECONDARY_RELOAD
1855 #define TARGET_SECONDARY_RELOAD rs6000_secondary_reload
1856 #undef TARGET_SECONDARY_MEMORY_NEEDED
1857 #define TARGET_SECONDARY_MEMORY_NEEDED rs6000_secondary_memory_needed
1858 #undef TARGET_SECONDARY_MEMORY_NEEDED_MODE
1859 #define TARGET_SECONDARY_MEMORY_NEEDED_MODE rs6000_secondary_memory_needed_mode
1861 #undef TARGET_LEGITIMATE_ADDRESS_P
1862 #define TARGET_LEGITIMATE_ADDRESS_P rs6000_legitimate_address_p
1864 #undef TARGET_MODE_DEPENDENT_ADDRESS_P
1865 #define TARGET_MODE_DEPENDENT_ADDRESS_P rs6000_mode_dependent_address_p
1867 #undef TARGET_COMPUTE_PRESSURE_CLASSES
1868 #define TARGET_COMPUTE_PRESSURE_CLASSES rs6000_compute_pressure_classes
1870 #undef TARGET_CAN_ELIMINATE
1871 #define TARGET_CAN_ELIMINATE rs6000_can_eliminate
1873 #undef TARGET_CONDITIONAL_REGISTER_USAGE
1874 #define TARGET_CONDITIONAL_REGISTER_USAGE rs6000_conditional_register_usage
1876 #undef TARGET_SCHED_REASSOCIATION_WIDTH
1877 #define TARGET_SCHED_REASSOCIATION_WIDTH rs6000_reassociation_width
1879 #undef TARGET_TRAMPOLINE_INIT
1880 #define TARGET_TRAMPOLINE_INIT rs6000_trampoline_init
1882 #undef TARGET_FUNCTION_VALUE
1883 #define TARGET_FUNCTION_VALUE rs6000_function_value
1885 #undef TARGET_OPTION_VALID_ATTRIBUTE_P
1886 #define TARGET_OPTION_VALID_ATTRIBUTE_P rs6000_valid_attribute_p
1888 #undef TARGET_OPTION_SAVE
1889 #define TARGET_OPTION_SAVE rs6000_function_specific_save
1891 #undef TARGET_OPTION_RESTORE
1892 #define TARGET_OPTION_RESTORE rs6000_function_specific_restore
1894 #undef TARGET_OPTION_PRINT
1895 #define TARGET_OPTION_PRINT rs6000_function_specific_print
1897 #undef TARGET_CAN_INLINE_P
1898 #define TARGET_CAN_INLINE_P rs6000_can_inline_p
1900 #undef TARGET_SET_CURRENT_FUNCTION
1901 #define TARGET_SET_CURRENT_FUNCTION rs6000_set_current_function
1903 #undef TARGET_LEGITIMATE_CONSTANT_P
1904 #define TARGET_LEGITIMATE_CONSTANT_P rs6000_legitimate_constant_p
1906 #undef TARGET_VECTORIZE_VEC_PERM_CONST
1907 #define TARGET_VECTORIZE_VEC_PERM_CONST rs6000_vectorize_vec_perm_const
1909 #undef TARGET_CAN_USE_DOLOOP_P
1910 #define TARGET_CAN_USE_DOLOOP_P can_use_doloop_if_innermost
1912 #undef TARGET_ATOMIC_ASSIGN_EXPAND_FENV
1913 #define TARGET_ATOMIC_ASSIGN_EXPAND_FENV rs6000_atomic_assign_expand_fenv
1915 #undef TARGET_LIBGCC_CMP_RETURN_MODE
1916 #define TARGET_LIBGCC_CMP_RETURN_MODE rs6000_abi_word_mode
1917 #undef TARGET_LIBGCC_SHIFT_COUNT_MODE
1918 #define TARGET_LIBGCC_SHIFT_COUNT_MODE rs6000_abi_word_mode
1919 #undef TARGET_UNWIND_WORD_MODE
1920 #define TARGET_UNWIND_WORD_MODE rs6000_abi_word_mode
1922 #undef TARGET_OFFLOAD_OPTIONS
1923 #define TARGET_OFFLOAD_OPTIONS rs6000_offload_options
1925 #undef TARGET_C_MODE_FOR_SUFFIX
1926 #define TARGET_C_MODE_FOR_SUFFIX rs6000_c_mode_for_suffix
1928 #undef TARGET_INVALID_BINARY_OP
1929 #define TARGET_INVALID_BINARY_OP rs6000_invalid_binary_op
1931 #undef TARGET_OPTAB_SUPPORTED_P
1932 #define TARGET_OPTAB_SUPPORTED_P rs6000_optab_supported_p
1934 #undef TARGET_CUSTOM_FUNCTION_DESCRIPTORS
1935 #define TARGET_CUSTOM_FUNCTION_DESCRIPTORS 1
1937 #undef TARGET_COMPARE_VERSION_PRIORITY
1938 #define TARGET_COMPARE_VERSION_PRIORITY rs6000_compare_version_priority
1940 #undef TARGET_GENERATE_VERSION_DISPATCHER_BODY
1941 #define TARGET_GENERATE_VERSION_DISPATCHER_BODY \
1942 rs6000_generate_version_dispatcher_body
1944 #undef TARGET_GET_FUNCTION_VERSIONS_DISPATCHER
1945 #define TARGET_GET_FUNCTION_VERSIONS_DISPATCHER \
1946 rs6000_get_function_versions_dispatcher
1948 #undef TARGET_OPTION_FUNCTION_VERSIONS
1949 #define TARGET_OPTION_FUNCTION_VERSIONS common_function_versions
1951 #undef TARGET_HARD_REGNO_NREGS
1952 #define TARGET_HARD_REGNO_NREGS rs6000_hard_regno_nregs_hook
1953 #undef TARGET_HARD_REGNO_MODE_OK
1954 #define TARGET_HARD_REGNO_MODE_OK rs6000_hard_regno_mode_ok
1956 #undef TARGET_MODES_TIEABLE_P
1957 #define TARGET_MODES_TIEABLE_P rs6000_modes_tieable_p
1959 #undef TARGET_HARD_REGNO_CALL_PART_CLOBBERED
1960 #define TARGET_HARD_REGNO_CALL_PART_CLOBBERED \
1961 rs6000_hard_regno_call_part_clobbered
1963 #undef TARGET_SLOW_UNALIGNED_ACCESS
1964 #define TARGET_SLOW_UNALIGNED_ACCESS rs6000_slow_unaligned_access
1966 #undef TARGET_CAN_CHANGE_MODE_CLASS
1967 #define TARGET_CAN_CHANGE_MODE_CLASS rs6000_can_change_mode_class
1969 #undef TARGET_CONSTANT_ALIGNMENT
1970 #define TARGET_CONSTANT_ALIGNMENT rs6000_constant_alignment
1972 #undef TARGET_STARTING_FRAME_OFFSET
1973 #define TARGET_STARTING_FRAME_OFFSET rs6000_starting_frame_offset
1975 #if TARGET_ELF && RS6000_WEAK
1976 #undef TARGET_ASM_GLOBALIZE_DECL_NAME
1977 #define TARGET_ASM_GLOBALIZE_DECL_NAME rs6000_globalize_decl_name
1978 #endif
1980 #undef TARGET_SETJMP_PRESERVES_NONVOLATILE_REGS_P
1981 #define TARGET_SETJMP_PRESERVES_NONVOLATILE_REGS_P hook_bool_void_true
1983 #undef TARGET_MANGLE_DECL_ASSEMBLER_NAME
1984 #define TARGET_MANGLE_DECL_ASSEMBLER_NAME rs6000_mangle_decl_assembler_name
1987 /* Processor table. */
1988 struct rs6000_ptt
1990 const char *const name; /* Canonical processor name. */
1991 const enum processor_type processor; /* Processor type enum value. */
1992 const HOST_WIDE_INT target_enable; /* Target flags to enable. */
1995 static struct rs6000_ptt const processor_target_table[] =
1997 #define RS6000_CPU(NAME, CPU, FLAGS) { NAME, CPU, FLAGS },
1998 #include "rs6000-cpus.def"
1999 #undef RS6000_CPU
2002 /* Look up a processor name for -mcpu=xxx and -mtune=xxx. Return -1 if the
2003 name is invalid. */
2005 static int
2006 rs6000_cpu_name_lookup (const char *name)
2008 size_t i;
2010 if (name != NULL)
2012 for (i = 0; i < ARRAY_SIZE (processor_target_table); i++)
2013 if (! strcmp (name, processor_target_table[i].name))
2014 return (int)i;
2017 return -1;
2021 /* Return number of consecutive hard regs needed starting at reg REGNO
2022 to hold something of mode MODE.
2023 This is ordinarily the length in words of a value of mode MODE
2024 but can be less for certain modes in special long registers.
2026 POWER and PowerPC GPRs hold 32 bits worth;
2027 PowerPC64 GPRs and FPRs point register holds 64 bits worth. */
2029 static int
2030 rs6000_hard_regno_nregs_internal (int regno, machine_mode mode)
2032 unsigned HOST_WIDE_INT reg_size;
2034 /* 128-bit floating point usually takes 2 registers, unless it is IEEE
2035 128-bit floating point that can go in vector registers, which has VSX
2036 memory addressing. */
2037 if (FP_REGNO_P (regno))
2038 reg_size = (VECTOR_MEM_VSX_P (mode) || FLOAT128_VECTOR_P (mode)
2039 ? UNITS_PER_VSX_WORD
2040 : UNITS_PER_FP_WORD);
2042 else if (ALTIVEC_REGNO_P (regno))
2043 reg_size = UNITS_PER_ALTIVEC_WORD;
2045 else
2046 reg_size = UNITS_PER_WORD;
2048 return (GET_MODE_SIZE (mode) + reg_size - 1) / reg_size;
2051 /* Value is 1 if hard register REGNO can hold a value of machine-mode
2052 MODE. */
2053 static int
2054 rs6000_hard_regno_mode_ok_uncached (int regno, machine_mode mode)
2056 int last_regno = regno + rs6000_hard_regno_nregs[mode][regno] - 1;
2058 if (COMPLEX_MODE_P (mode))
2059 mode = GET_MODE_INNER (mode);
2061 /* PTImode can only go in GPRs. Quad word memory operations require even/odd
2062 register combinations, and use PTImode where we need to deal with quad
2063 word memory operations. Don't allow quad words in the argument or frame
2064 pointer registers, just registers 0..31. */
2065 if (mode == PTImode)
2066 return (IN_RANGE (regno, FIRST_GPR_REGNO, LAST_GPR_REGNO)
2067 && IN_RANGE (last_regno, FIRST_GPR_REGNO, LAST_GPR_REGNO)
2068 && ((regno & 1) == 0));
2070 /* VSX registers that overlap the FPR registers are larger than for non-VSX
2071 implementations. Don't allow an item to be split between a FP register
2072 and an Altivec register. Allow TImode in all VSX registers if the user
2073 asked for it. */
2074 if (TARGET_VSX && VSX_REGNO_P (regno)
2075 && (VECTOR_MEM_VSX_P (mode)
2076 || FLOAT128_VECTOR_P (mode)
2077 || reg_addr[mode].scalar_in_vmx_p
2078 || mode == TImode
2079 || (TARGET_VADDUQM && mode == V1TImode)))
2081 if (FP_REGNO_P (regno))
2082 return FP_REGNO_P (last_regno);
2084 if (ALTIVEC_REGNO_P (regno))
2086 if (GET_MODE_SIZE (mode) != 16 && !reg_addr[mode].scalar_in_vmx_p)
2087 return 0;
2089 return ALTIVEC_REGNO_P (last_regno);
2093 /* The GPRs can hold any mode, but values bigger than one register
2094 cannot go past R31. */
2095 if (INT_REGNO_P (regno))
2096 return INT_REGNO_P (last_regno);
2098 /* The float registers (except for VSX vector modes) can only hold floating
2099 modes and DImode. */
2100 if (FP_REGNO_P (regno))
2102 if (FLOAT128_VECTOR_P (mode))
2103 return false;
2105 if (SCALAR_FLOAT_MODE_P (mode)
2106 && (mode != TDmode || (regno % 2) == 0)
2107 && FP_REGNO_P (last_regno))
2108 return 1;
2110 if (GET_MODE_CLASS (mode) == MODE_INT)
2112 if(GET_MODE_SIZE (mode) == UNITS_PER_FP_WORD)
2113 return 1;
2115 if (TARGET_P8_VECTOR && (mode == SImode))
2116 return 1;
2118 if (TARGET_P9_VECTOR && (mode == QImode || mode == HImode))
2119 return 1;
2122 return 0;
2125 /* The CR register can only hold CC modes. */
2126 if (CR_REGNO_P (regno))
2127 return GET_MODE_CLASS (mode) == MODE_CC;
2129 if (CA_REGNO_P (regno))
2130 return mode == Pmode || mode == SImode;
2132 /* AltiVec only in AldyVec registers. */
2133 if (ALTIVEC_REGNO_P (regno))
2134 return (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode)
2135 || mode == V1TImode);
2137 /* We cannot put non-VSX TImode or PTImode anywhere except general register
2138 and it must be able to fit within the register set. */
2140 return GET_MODE_SIZE (mode) <= UNITS_PER_WORD;
2143 /* Implement TARGET_HARD_REGNO_NREGS. */
2145 static unsigned int
2146 rs6000_hard_regno_nregs_hook (unsigned int regno, machine_mode mode)
2148 return rs6000_hard_regno_nregs[mode][regno];
2151 /* Implement TARGET_HARD_REGNO_MODE_OK. */
2153 static bool
2154 rs6000_hard_regno_mode_ok (unsigned int regno, machine_mode mode)
2156 return rs6000_hard_regno_mode_ok_p[mode][regno];
2159 /* Implement TARGET_MODES_TIEABLE_P.
2161 PTImode cannot tie with other modes because PTImode is restricted to even
2162 GPR registers, and TImode can go in any GPR as well as VSX registers (PR
2163 57744).
2165 Altivec/VSX vector tests were moved ahead of scalar float mode, so that IEEE
2166 128-bit floating point on VSX systems ties with other vectors. */
2168 static bool
2169 rs6000_modes_tieable_p (machine_mode mode1, machine_mode mode2)
2171 if (mode1 == PTImode)
2172 return mode2 == PTImode;
2173 if (mode2 == PTImode)
2174 return false;
2176 if (ALTIVEC_OR_VSX_VECTOR_MODE (mode1))
2177 return ALTIVEC_OR_VSX_VECTOR_MODE (mode2);
2178 if (ALTIVEC_OR_VSX_VECTOR_MODE (mode2))
2179 return false;
2181 if (SCALAR_FLOAT_MODE_P (mode1))
2182 return SCALAR_FLOAT_MODE_P (mode2);
2183 if (SCALAR_FLOAT_MODE_P (mode2))
2184 return false;
2186 if (GET_MODE_CLASS (mode1) == MODE_CC)
2187 return GET_MODE_CLASS (mode2) == MODE_CC;
2188 if (GET_MODE_CLASS (mode2) == MODE_CC)
2189 return false;
2191 return true;
2194 /* Implement TARGET_HARD_REGNO_CALL_PART_CLOBBERED. */
2196 static bool
2197 rs6000_hard_regno_call_part_clobbered (rtx_insn *insn ATTRIBUTE_UNUSED,
2198 unsigned int regno, machine_mode mode)
2200 if (TARGET_32BIT
2201 && TARGET_POWERPC64
2202 && GET_MODE_SIZE (mode) > 4
2203 && INT_REGNO_P (regno))
2204 return true;
2206 if (TARGET_VSX
2207 && FP_REGNO_P (regno)
2208 && GET_MODE_SIZE (mode) > 8
2209 && !FLOAT128_2REG_P (mode))
2210 return true;
2212 return false;
2215 /* Print interesting facts about registers. */
2216 static void
2217 rs6000_debug_reg_print (int first_regno, int last_regno, const char *reg_name)
2219 int r, m;
2221 for (r = first_regno; r <= last_regno; ++r)
2223 const char *comma = "";
2224 int len;
2226 if (first_regno == last_regno)
2227 fprintf (stderr, "%s:\t", reg_name);
2228 else
2229 fprintf (stderr, "%s%d:\t", reg_name, r - first_regno);
2231 len = 8;
2232 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2233 if (rs6000_hard_regno_mode_ok_p[m][r] && rs6000_hard_regno_nregs[m][r])
2235 if (len > 70)
2237 fprintf (stderr, ",\n\t");
2238 len = 8;
2239 comma = "";
2242 if (rs6000_hard_regno_nregs[m][r] > 1)
2243 len += fprintf (stderr, "%s%s/%d", comma, GET_MODE_NAME (m),
2244 rs6000_hard_regno_nregs[m][r]);
2245 else
2246 len += fprintf (stderr, "%s%s", comma, GET_MODE_NAME (m));
2248 comma = ", ";
2251 if (call_used_regs[r])
2253 if (len > 70)
2255 fprintf (stderr, ",\n\t");
2256 len = 8;
2257 comma = "";
2260 len += fprintf (stderr, "%s%s", comma, "call-used");
2261 comma = ", ";
2264 if (fixed_regs[r])
2266 if (len > 70)
2268 fprintf (stderr, ",\n\t");
2269 len = 8;
2270 comma = "";
2273 len += fprintf (stderr, "%s%s", comma, "fixed");
2274 comma = ", ";
2277 if (len > 70)
2279 fprintf (stderr, ",\n\t");
2280 comma = "";
2283 len += fprintf (stderr, "%sreg-class = %s", comma,
2284 reg_class_names[(int)rs6000_regno_regclass[r]]);
2285 comma = ", ";
2287 if (len > 70)
2289 fprintf (stderr, ",\n\t");
2290 comma = "";
2293 fprintf (stderr, "%sregno = %d\n", comma, r);
2297 static const char *
2298 rs6000_debug_vector_unit (enum rs6000_vector v)
2300 const char *ret;
2302 switch (v)
2304 case VECTOR_NONE: ret = "none"; break;
2305 case VECTOR_ALTIVEC: ret = "altivec"; break;
2306 case VECTOR_VSX: ret = "vsx"; break;
2307 case VECTOR_P8_VECTOR: ret = "p8_vector"; break;
2308 default: ret = "unknown"; break;
2311 return ret;
2314 /* Inner function printing just the address mask for a particular reload
2315 register class. */
2316 DEBUG_FUNCTION char *
2317 rs6000_debug_addr_mask (addr_mask_type mask, bool keep_spaces)
2319 static char ret[8];
2320 char *p = ret;
2322 if ((mask & RELOAD_REG_VALID) != 0)
2323 *p++ = 'v';
2324 else if (keep_spaces)
2325 *p++ = ' ';
2327 if ((mask & RELOAD_REG_MULTIPLE) != 0)
2328 *p++ = 'm';
2329 else if (keep_spaces)
2330 *p++ = ' ';
2332 if ((mask & RELOAD_REG_INDEXED) != 0)
2333 *p++ = 'i';
2334 else if (keep_spaces)
2335 *p++ = ' ';
2337 if ((mask & RELOAD_REG_QUAD_OFFSET) != 0)
2338 *p++ = 'O';
2339 else if ((mask & RELOAD_REG_OFFSET) != 0)
2340 *p++ = 'o';
2341 else if (keep_spaces)
2342 *p++ = ' ';
2344 if ((mask & RELOAD_REG_PRE_INCDEC) != 0)
2345 *p++ = '+';
2346 else if (keep_spaces)
2347 *p++ = ' ';
2349 if ((mask & RELOAD_REG_PRE_MODIFY) != 0)
2350 *p++ = '+';
2351 else if (keep_spaces)
2352 *p++ = ' ';
2354 if ((mask & RELOAD_REG_AND_M16) != 0)
2355 *p++ = '&';
2356 else if (keep_spaces)
2357 *p++ = ' ';
2359 *p = '\0';
2361 return ret;
2364 /* Print the address masks in a human readble fashion. */
2365 DEBUG_FUNCTION void
2366 rs6000_debug_print_mode (ssize_t m)
2368 ssize_t rc;
2369 int spaces = 0;
2371 fprintf (stderr, "Mode: %-5s", GET_MODE_NAME (m));
2372 for (rc = 0; rc < N_RELOAD_REG; rc++)
2373 fprintf (stderr, " %s: %s", reload_reg_map[rc].name,
2374 rs6000_debug_addr_mask (reg_addr[m].addr_mask[rc], true));
2376 if ((reg_addr[m].reload_store != CODE_FOR_nothing)
2377 || (reg_addr[m].reload_load != CODE_FOR_nothing))
2379 fprintf (stderr, "%*s Reload=%c%c", spaces, "",
2380 (reg_addr[m].reload_store != CODE_FOR_nothing) ? 's' : '*',
2381 (reg_addr[m].reload_load != CODE_FOR_nothing) ? 'l' : '*');
2382 spaces = 0;
2384 else
2385 spaces += sizeof (" Reload=sl") - 1;
2387 if (reg_addr[m].scalar_in_vmx_p)
2389 fprintf (stderr, "%*s Upper=y", spaces, "");
2390 spaces = 0;
2392 else
2393 spaces += sizeof (" Upper=y") - 1;
2395 if (rs6000_vector_unit[m] != VECTOR_NONE
2396 || rs6000_vector_mem[m] != VECTOR_NONE)
2398 fprintf (stderr, "%*s vector: arith=%-10s mem=%s",
2399 spaces, "",
2400 rs6000_debug_vector_unit (rs6000_vector_unit[m]),
2401 rs6000_debug_vector_unit (rs6000_vector_mem[m]));
2404 fputs ("\n", stderr);
2407 #define DEBUG_FMT_ID "%-32s= "
2408 #define DEBUG_FMT_D DEBUG_FMT_ID "%d\n"
2409 #define DEBUG_FMT_WX DEBUG_FMT_ID "%#.12" HOST_WIDE_INT_PRINT "x: "
2410 #define DEBUG_FMT_S DEBUG_FMT_ID "%s\n"
2412 /* Print various interesting information with -mdebug=reg. */
2413 static void
2414 rs6000_debug_reg_global (void)
2416 static const char *const tf[2] = { "false", "true" };
2417 const char *nl = (const char *)0;
2418 int m;
2419 size_t m1, m2, v;
2420 char costly_num[20];
2421 char nop_num[20];
2422 char flags_buffer[40];
2423 const char *costly_str;
2424 const char *nop_str;
2425 const char *trace_str;
2426 const char *abi_str;
2427 const char *cmodel_str;
2428 struct cl_target_option cl_opts;
2430 /* Modes we want tieable information on. */
2431 static const machine_mode print_tieable_modes[] = {
2432 QImode,
2433 HImode,
2434 SImode,
2435 DImode,
2436 TImode,
2437 PTImode,
2438 SFmode,
2439 DFmode,
2440 TFmode,
2441 IFmode,
2442 KFmode,
2443 SDmode,
2444 DDmode,
2445 TDmode,
2446 V16QImode,
2447 V8HImode,
2448 V4SImode,
2449 V2DImode,
2450 V1TImode,
2451 V32QImode,
2452 V16HImode,
2453 V8SImode,
2454 V4DImode,
2455 V2TImode,
2456 V4SFmode,
2457 V2DFmode,
2458 V8SFmode,
2459 V4DFmode,
2460 CCmode,
2461 CCUNSmode,
2462 CCEQmode,
2465 /* Virtual regs we are interested in. */
2466 const static struct {
2467 int regno; /* register number. */
2468 const char *name; /* register name. */
2469 } virtual_regs[] = {
2470 { STACK_POINTER_REGNUM, "stack pointer:" },
2471 { TOC_REGNUM, "toc: " },
2472 { STATIC_CHAIN_REGNUM, "static chain: " },
2473 { RS6000_PIC_OFFSET_TABLE_REGNUM, "pic offset: " },
2474 { HARD_FRAME_POINTER_REGNUM, "hard frame: " },
2475 { ARG_POINTER_REGNUM, "arg pointer: " },
2476 { FRAME_POINTER_REGNUM, "frame pointer:" },
2477 { FIRST_PSEUDO_REGISTER, "first pseudo: " },
2478 { FIRST_VIRTUAL_REGISTER, "first virtual:" },
2479 { VIRTUAL_INCOMING_ARGS_REGNUM, "incoming_args:" },
2480 { VIRTUAL_STACK_VARS_REGNUM, "stack_vars: " },
2481 { VIRTUAL_STACK_DYNAMIC_REGNUM, "stack_dynamic:" },
2482 { VIRTUAL_OUTGOING_ARGS_REGNUM, "outgoing_args:" },
2483 { VIRTUAL_CFA_REGNUM, "cfa (frame): " },
2484 { VIRTUAL_PREFERRED_STACK_BOUNDARY_REGNUM, "stack boundry:" },
2485 { LAST_VIRTUAL_REGISTER, "last virtual: " },
2488 fputs ("\nHard register information:\n", stderr);
2489 rs6000_debug_reg_print (FIRST_GPR_REGNO, LAST_GPR_REGNO, "gr");
2490 rs6000_debug_reg_print (FIRST_FPR_REGNO, LAST_FPR_REGNO, "fp");
2491 rs6000_debug_reg_print (FIRST_ALTIVEC_REGNO,
2492 LAST_ALTIVEC_REGNO,
2493 "vs");
2494 rs6000_debug_reg_print (LR_REGNO, LR_REGNO, "lr");
2495 rs6000_debug_reg_print (CTR_REGNO, CTR_REGNO, "ctr");
2496 rs6000_debug_reg_print (CR0_REGNO, CR7_REGNO, "cr");
2497 rs6000_debug_reg_print (CA_REGNO, CA_REGNO, "ca");
2498 rs6000_debug_reg_print (VRSAVE_REGNO, VRSAVE_REGNO, "vrsave");
2499 rs6000_debug_reg_print (VSCR_REGNO, VSCR_REGNO, "vscr");
2501 fputs ("\nVirtual/stack/frame registers:\n", stderr);
2502 for (v = 0; v < ARRAY_SIZE (virtual_regs); v++)
2503 fprintf (stderr, "%s regno = %3d\n", virtual_regs[v].name, virtual_regs[v].regno);
2505 fprintf (stderr,
2506 "\n"
2507 "d reg_class = %s\n"
2508 "f reg_class = %s\n"
2509 "v reg_class = %s\n"
2510 "wa reg_class = %s\n"
2511 "wd reg_class = %s\n"
2512 "we reg_class = %s\n"
2513 "wf reg_class = %s\n"
2514 "wg reg_class = %s\n"
2515 "wh reg_class = %s\n"
2516 "wi reg_class = %s\n"
2517 "wj reg_class = %s\n"
2518 "wk reg_class = %s\n"
2519 "wl reg_class = %s\n"
2520 "wm reg_class = %s\n"
2521 "wp reg_class = %s\n"
2522 "wq reg_class = %s\n"
2523 "wr reg_class = %s\n"
2524 "ws reg_class = %s\n"
2525 "wt reg_class = %s\n"
2526 "wv reg_class = %s\n"
2527 "ww reg_class = %s\n"
2528 "wx reg_class = %s\n"
2529 "wz reg_class = %s\n"
2530 "wA reg_class = %s\n"
2531 "\n",
2532 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_d]],
2533 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_f]],
2534 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_v]],
2535 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wa]],
2536 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wd]],
2537 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_we]],
2538 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wf]],
2539 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wg]],
2540 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wh]],
2541 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wi]],
2542 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wj]],
2543 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wk]],
2544 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wl]],
2545 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wm]],
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_wv]],
2552 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_ww]],
2553 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wx]],
2554 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wz]],
2555 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wA]]);
2557 nl = "\n";
2558 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2559 rs6000_debug_print_mode (m);
2561 fputs ("\n", stderr);
2563 for (m1 = 0; m1 < ARRAY_SIZE (print_tieable_modes); m1++)
2565 machine_mode mode1 = print_tieable_modes[m1];
2566 bool first_time = true;
2568 nl = (const char *)0;
2569 for (m2 = 0; m2 < ARRAY_SIZE (print_tieable_modes); m2++)
2571 machine_mode mode2 = print_tieable_modes[m2];
2572 if (mode1 != mode2 && rs6000_modes_tieable_p (mode1, mode2))
2574 if (first_time)
2576 fprintf (stderr, "Tieable modes %s:", GET_MODE_NAME (mode1));
2577 nl = "\n";
2578 first_time = false;
2581 fprintf (stderr, " %s", GET_MODE_NAME (mode2));
2585 if (!first_time)
2586 fputs ("\n", stderr);
2589 if (nl)
2590 fputs (nl, stderr);
2592 if (rs6000_recip_control)
2594 fprintf (stderr, "\nReciprocal mask = 0x%x\n", rs6000_recip_control);
2596 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2597 if (rs6000_recip_bits[m])
2599 fprintf (stderr,
2600 "Reciprocal estimate mode: %-5s divide: %s rsqrt: %s\n",
2601 GET_MODE_NAME (m),
2602 (RS6000_RECIP_AUTO_RE_P (m)
2603 ? "auto"
2604 : (RS6000_RECIP_HAVE_RE_P (m) ? "have" : "none")),
2605 (RS6000_RECIP_AUTO_RSQRTE_P (m)
2606 ? "auto"
2607 : (RS6000_RECIP_HAVE_RSQRTE_P (m) ? "have" : "none")));
2610 fputs ("\n", stderr);
2613 if (rs6000_cpu_index >= 0)
2615 const char *name = processor_target_table[rs6000_cpu_index].name;
2616 HOST_WIDE_INT flags
2617 = processor_target_table[rs6000_cpu_index].target_enable;
2619 sprintf (flags_buffer, "-mcpu=%s flags", name);
2620 rs6000_print_isa_options (stderr, 0, flags_buffer, flags);
2622 else
2623 fprintf (stderr, DEBUG_FMT_S, "cpu", "<none>");
2625 if (rs6000_tune_index >= 0)
2627 const char *name = processor_target_table[rs6000_tune_index].name;
2628 HOST_WIDE_INT flags
2629 = processor_target_table[rs6000_tune_index].target_enable;
2631 sprintf (flags_buffer, "-mtune=%s flags", name);
2632 rs6000_print_isa_options (stderr, 0, flags_buffer, flags);
2634 else
2635 fprintf (stderr, DEBUG_FMT_S, "tune", "<none>");
2637 cl_target_option_save (&cl_opts, &global_options);
2638 rs6000_print_isa_options (stderr, 0, "rs6000_isa_flags",
2639 rs6000_isa_flags);
2641 rs6000_print_isa_options (stderr, 0, "rs6000_isa_flags_explicit",
2642 rs6000_isa_flags_explicit);
2644 rs6000_print_builtin_options (stderr, 0, "rs6000_builtin_mask",
2645 rs6000_builtin_mask);
2647 rs6000_print_isa_options (stderr, 0, "TARGET_DEFAULT", TARGET_DEFAULT);
2649 fprintf (stderr, DEBUG_FMT_S, "--with-cpu default",
2650 OPTION_TARGET_CPU_DEFAULT ? OPTION_TARGET_CPU_DEFAULT : "<none>");
2652 switch (rs6000_sched_costly_dep)
2654 case max_dep_latency:
2655 costly_str = "max_dep_latency";
2656 break;
2658 case no_dep_costly:
2659 costly_str = "no_dep_costly";
2660 break;
2662 case all_deps_costly:
2663 costly_str = "all_deps_costly";
2664 break;
2666 case true_store_to_load_dep_costly:
2667 costly_str = "true_store_to_load_dep_costly";
2668 break;
2670 case store_to_load_dep_costly:
2671 costly_str = "store_to_load_dep_costly";
2672 break;
2674 default:
2675 costly_str = costly_num;
2676 sprintf (costly_num, "%d", (int)rs6000_sched_costly_dep);
2677 break;
2680 fprintf (stderr, DEBUG_FMT_S, "sched_costly_dep", costly_str);
2682 switch (rs6000_sched_insert_nops)
2684 case sched_finish_regroup_exact:
2685 nop_str = "sched_finish_regroup_exact";
2686 break;
2688 case sched_finish_pad_groups:
2689 nop_str = "sched_finish_pad_groups";
2690 break;
2692 case sched_finish_none:
2693 nop_str = "sched_finish_none";
2694 break;
2696 default:
2697 nop_str = nop_num;
2698 sprintf (nop_num, "%d", (int)rs6000_sched_insert_nops);
2699 break;
2702 fprintf (stderr, DEBUG_FMT_S, "sched_insert_nops", nop_str);
2704 switch (rs6000_sdata)
2706 default:
2707 case SDATA_NONE:
2708 break;
2710 case SDATA_DATA:
2711 fprintf (stderr, DEBUG_FMT_S, "sdata", "data");
2712 break;
2714 case SDATA_SYSV:
2715 fprintf (stderr, DEBUG_FMT_S, "sdata", "sysv");
2716 break;
2718 case SDATA_EABI:
2719 fprintf (stderr, DEBUG_FMT_S, "sdata", "eabi");
2720 break;
2724 switch (rs6000_traceback)
2726 case traceback_default: trace_str = "default"; break;
2727 case traceback_none: trace_str = "none"; break;
2728 case traceback_part: trace_str = "part"; break;
2729 case traceback_full: trace_str = "full"; break;
2730 default: trace_str = "unknown"; break;
2733 fprintf (stderr, DEBUG_FMT_S, "traceback", trace_str);
2735 switch (rs6000_current_cmodel)
2737 case CMODEL_SMALL: cmodel_str = "small"; break;
2738 case CMODEL_MEDIUM: cmodel_str = "medium"; break;
2739 case CMODEL_LARGE: cmodel_str = "large"; break;
2740 default: cmodel_str = "unknown"; break;
2743 fprintf (stderr, DEBUG_FMT_S, "cmodel", cmodel_str);
2745 switch (rs6000_current_abi)
2747 case ABI_NONE: abi_str = "none"; break;
2748 case ABI_AIX: abi_str = "aix"; break;
2749 case ABI_ELFv2: abi_str = "ELFv2"; break;
2750 case ABI_V4: abi_str = "V4"; break;
2751 case ABI_DARWIN: abi_str = "darwin"; break;
2752 default: abi_str = "unknown"; break;
2755 fprintf (stderr, DEBUG_FMT_S, "abi", abi_str);
2757 if (rs6000_altivec_abi)
2758 fprintf (stderr, DEBUG_FMT_S, "altivec_abi", "true");
2760 if (rs6000_darwin64_abi)
2761 fprintf (stderr, DEBUG_FMT_S, "darwin64_abi", "true");
2763 fprintf (stderr, DEBUG_FMT_S, "soft_float",
2764 (TARGET_SOFT_FLOAT ? "true" : "false"));
2766 if (TARGET_LINK_STACK)
2767 fprintf (stderr, DEBUG_FMT_S, "link_stack", "true");
2769 if (TARGET_P8_FUSION)
2771 char options[80];
2773 strcpy (options, "power8");
2774 if (TARGET_P8_FUSION_SIGN)
2775 strcat (options, ", sign");
2777 fprintf (stderr, DEBUG_FMT_S, "fusion", options);
2780 fprintf (stderr, DEBUG_FMT_S, "plt-format",
2781 TARGET_SECURE_PLT ? "secure" : "bss");
2782 fprintf (stderr, DEBUG_FMT_S, "struct-return",
2783 aix_struct_return ? "aix" : "sysv");
2784 fprintf (stderr, DEBUG_FMT_S, "always_hint", tf[!!rs6000_always_hint]);
2785 fprintf (stderr, DEBUG_FMT_S, "sched_groups", tf[!!rs6000_sched_groups]);
2786 fprintf (stderr, DEBUG_FMT_S, "align_branch",
2787 tf[!!rs6000_align_branch_targets]);
2788 fprintf (stderr, DEBUG_FMT_D, "tls_size", rs6000_tls_size);
2789 fprintf (stderr, DEBUG_FMT_D, "long_double_size",
2790 rs6000_long_double_type_size);
2791 if (rs6000_long_double_type_size > 64)
2793 fprintf (stderr, DEBUG_FMT_S, "long double type",
2794 TARGET_IEEEQUAD ? "IEEE" : "IBM");
2795 fprintf (stderr, DEBUG_FMT_S, "default long double type",
2796 TARGET_IEEEQUAD_DEFAULT ? "IEEE" : "IBM");
2798 fprintf (stderr, DEBUG_FMT_D, "sched_restricted_insns_priority",
2799 (int)rs6000_sched_restricted_insns_priority);
2800 fprintf (stderr, DEBUG_FMT_D, "Number of standard builtins",
2801 (int)END_BUILTINS);
2802 fprintf (stderr, DEBUG_FMT_D, "Number of rs6000 builtins",
2803 (int)RS6000_BUILTIN_COUNT);
2805 fprintf (stderr, DEBUG_FMT_D, "Enable float128 on VSX",
2806 (int)TARGET_FLOAT128_ENABLE_TYPE);
2808 if (TARGET_VSX)
2809 fprintf (stderr, DEBUG_FMT_D, "VSX easy 64-bit scalar element",
2810 (int)VECTOR_ELEMENT_SCALAR_64BIT);
2812 if (TARGET_DIRECT_MOVE_128)
2813 fprintf (stderr, DEBUG_FMT_D, "VSX easy 64-bit mfvsrld element",
2814 (int)VECTOR_ELEMENT_MFVSRLD_64BIT);
2818 /* Update the addr mask bits in reg_addr to help secondary reload and go if
2819 legitimate address support to figure out the appropriate addressing to
2820 use. */
2822 static void
2823 rs6000_setup_reg_addr_masks (void)
2825 ssize_t rc, reg, m, nregs;
2826 addr_mask_type any_addr_mask, addr_mask;
2828 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2830 machine_mode m2 = (machine_mode) m;
2831 bool complex_p = false;
2832 bool small_int_p = (m2 == QImode || m2 == HImode || m2 == SImode);
2833 size_t msize;
2835 if (COMPLEX_MODE_P (m2))
2837 complex_p = true;
2838 m2 = GET_MODE_INNER (m2);
2841 msize = GET_MODE_SIZE (m2);
2843 /* SDmode is special in that we want to access it only via REG+REG
2844 addressing on power7 and above, since we want to use the LFIWZX and
2845 STFIWZX instructions to load it. */
2846 bool indexed_only_p = (m == SDmode && TARGET_NO_SDMODE_STACK);
2848 any_addr_mask = 0;
2849 for (rc = FIRST_RELOAD_REG_CLASS; rc <= LAST_RELOAD_REG_CLASS; rc++)
2851 addr_mask = 0;
2852 reg = reload_reg_map[rc].reg;
2854 /* Can mode values go in the GPR/FPR/Altivec registers? */
2855 if (reg >= 0 && rs6000_hard_regno_mode_ok_p[m][reg])
2857 bool small_int_vsx_p = (small_int_p
2858 && (rc == RELOAD_REG_FPR
2859 || rc == RELOAD_REG_VMX));
2861 nregs = rs6000_hard_regno_nregs[m][reg];
2862 addr_mask |= RELOAD_REG_VALID;
2864 /* Indicate if the mode takes more than 1 physical register. If
2865 it takes a single register, indicate it can do REG+REG
2866 addressing. Small integers in VSX registers can only do
2867 REG+REG addressing. */
2868 if (small_int_vsx_p)
2869 addr_mask |= RELOAD_REG_INDEXED;
2870 else if (nregs > 1 || m == BLKmode || complex_p)
2871 addr_mask |= RELOAD_REG_MULTIPLE;
2872 else
2873 addr_mask |= RELOAD_REG_INDEXED;
2875 /* Figure out if we can do PRE_INC, PRE_DEC, or PRE_MODIFY
2876 addressing. If we allow scalars into Altivec registers,
2877 don't allow PRE_INC, PRE_DEC, or PRE_MODIFY.
2879 For VSX systems, we don't allow update addressing for
2880 DFmode/SFmode if those registers can go in both the
2881 traditional floating point registers and Altivec registers.
2882 The load/store instructions for the Altivec registers do not
2883 have update forms. If we allowed update addressing, it seems
2884 to break IV-OPT code using floating point if the index type is
2885 int instead of long (PR target/81550 and target/84042). */
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 && (m != E_DFmode || !TARGET_VSX)
2894 && (m != E_SFmode || !TARGET_P8_VECTOR)
2895 && !small_int_vsx_p)
2897 addr_mask |= RELOAD_REG_PRE_INCDEC;
2899 /* PRE_MODIFY is more restricted than PRE_INC/PRE_DEC in that
2900 we don't allow PRE_MODIFY for some multi-register
2901 operations. */
2902 switch (m)
2904 default:
2905 addr_mask |= RELOAD_REG_PRE_MODIFY;
2906 break;
2908 case E_DImode:
2909 if (TARGET_POWERPC64)
2910 addr_mask |= RELOAD_REG_PRE_MODIFY;
2911 break;
2913 case E_DFmode:
2914 case E_DDmode:
2915 if (TARGET_HARD_FLOAT)
2916 addr_mask |= RELOAD_REG_PRE_MODIFY;
2917 break;
2922 /* GPR and FPR registers can do REG+OFFSET addressing, except
2923 possibly for SDmode. ISA 3.0 (i.e. power9) adds D-form addressing
2924 for 64-bit scalars and 32-bit SFmode to altivec registers. */
2925 if ((addr_mask != 0) && !indexed_only_p
2926 && msize <= 8
2927 && (rc == RELOAD_REG_GPR
2928 || ((msize == 8 || m2 == SFmode)
2929 && (rc == RELOAD_REG_FPR
2930 || (rc == RELOAD_REG_VMX && TARGET_P9_VECTOR)))))
2931 addr_mask |= RELOAD_REG_OFFSET;
2933 /* VSX registers can do REG+OFFSET addresssing if ISA 3.0
2934 instructions are enabled. The offset for 128-bit VSX registers is
2935 only 12-bits. While GPRs can handle the full offset range, VSX
2936 registers can only handle the restricted range. */
2937 else if ((addr_mask != 0) && !indexed_only_p
2938 && msize == 16 && TARGET_P9_VECTOR
2939 && (ALTIVEC_OR_VSX_VECTOR_MODE (m2)
2940 || (m2 == TImode && TARGET_VSX)))
2942 addr_mask |= RELOAD_REG_OFFSET;
2943 if (rc == RELOAD_REG_FPR || rc == RELOAD_REG_VMX)
2944 addr_mask |= RELOAD_REG_QUAD_OFFSET;
2947 /* VMX registers can do (REG & -16) and ((REG+REG) & -16)
2948 addressing on 128-bit types. */
2949 if (rc == RELOAD_REG_VMX && msize == 16
2950 && (addr_mask & RELOAD_REG_VALID) != 0)
2951 addr_mask |= RELOAD_REG_AND_M16;
2953 reg_addr[m].addr_mask[rc] = addr_mask;
2954 any_addr_mask |= addr_mask;
2957 reg_addr[m].addr_mask[RELOAD_REG_ANY] = any_addr_mask;
2962 /* Initialize the various global tables that are based on register size. */
2963 static void
2964 rs6000_init_hard_regno_mode_ok (bool global_init_p)
2966 ssize_t r, m, c;
2967 int align64;
2968 int align32;
2970 /* Precalculate REGNO_REG_CLASS. */
2971 rs6000_regno_regclass[0] = GENERAL_REGS;
2972 for (r = 1; r < 32; ++r)
2973 rs6000_regno_regclass[r] = BASE_REGS;
2975 for (r = 32; r < 64; ++r)
2976 rs6000_regno_regclass[r] = FLOAT_REGS;
2978 for (r = 64; HARD_REGISTER_NUM_P (r); ++r)
2979 rs6000_regno_regclass[r] = NO_REGS;
2981 for (r = FIRST_ALTIVEC_REGNO; r <= LAST_ALTIVEC_REGNO; ++r)
2982 rs6000_regno_regclass[r] = ALTIVEC_REGS;
2984 rs6000_regno_regclass[CR0_REGNO] = CR0_REGS;
2985 for (r = CR1_REGNO; r <= CR7_REGNO; ++r)
2986 rs6000_regno_regclass[r] = CR_REGS;
2988 rs6000_regno_regclass[LR_REGNO] = LINK_REGS;
2989 rs6000_regno_regclass[CTR_REGNO] = CTR_REGS;
2990 rs6000_regno_regclass[CA_REGNO] = NO_REGS;
2991 rs6000_regno_regclass[VRSAVE_REGNO] = VRSAVE_REGS;
2992 rs6000_regno_regclass[VSCR_REGNO] = VRSAVE_REGS;
2993 rs6000_regno_regclass[ARG_POINTER_REGNUM] = BASE_REGS;
2994 rs6000_regno_regclass[FRAME_POINTER_REGNUM] = BASE_REGS;
2996 /* Precalculate register class to simpler reload register class. We don't
2997 need all of the register classes that are combinations of different
2998 classes, just the simple ones that have constraint letters. */
2999 for (c = 0; c < N_REG_CLASSES; c++)
3000 reg_class_to_reg_type[c] = NO_REG_TYPE;
3002 reg_class_to_reg_type[(int)GENERAL_REGS] = GPR_REG_TYPE;
3003 reg_class_to_reg_type[(int)BASE_REGS] = GPR_REG_TYPE;
3004 reg_class_to_reg_type[(int)VSX_REGS] = VSX_REG_TYPE;
3005 reg_class_to_reg_type[(int)VRSAVE_REGS] = SPR_REG_TYPE;
3006 reg_class_to_reg_type[(int)VSCR_REGS] = SPR_REG_TYPE;
3007 reg_class_to_reg_type[(int)LINK_REGS] = SPR_REG_TYPE;
3008 reg_class_to_reg_type[(int)CTR_REGS] = SPR_REG_TYPE;
3009 reg_class_to_reg_type[(int)LINK_OR_CTR_REGS] = SPR_REG_TYPE;
3010 reg_class_to_reg_type[(int)CR_REGS] = CR_REG_TYPE;
3011 reg_class_to_reg_type[(int)CR0_REGS] = CR_REG_TYPE;
3013 if (TARGET_VSX)
3015 reg_class_to_reg_type[(int)FLOAT_REGS] = VSX_REG_TYPE;
3016 reg_class_to_reg_type[(int)ALTIVEC_REGS] = VSX_REG_TYPE;
3018 else
3020 reg_class_to_reg_type[(int)FLOAT_REGS] = FPR_REG_TYPE;
3021 reg_class_to_reg_type[(int)ALTIVEC_REGS] = ALTIVEC_REG_TYPE;
3024 /* Precalculate the valid memory formats as well as the vector information,
3025 this must be set up before the rs6000_hard_regno_nregs_internal calls
3026 below. */
3027 gcc_assert ((int)VECTOR_NONE == 0);
3028 memset ((void *) &rs6000_vector_unit[0], '\0', sizeof (rs6000_vector_unit));
3029 memset ((void *) &rs6000_vector_mem[0], '\0', sizeof (rs6000_vector_mem));
3031 gcc_assert ((int)CODE_FOR_nothing == 0);
3032 memset ((void *) &reg_addr[0], '\0', sizeof (reg_addr));
3034 gcc_assert ((int)NO_REGS == 0);
3035 memset ((void *) &rs6000_constraints[0], '\0', sizeof (rs6000_constraints));
3037 /* The VSX hardware allows native alignment for vectors, but control whether the compiler
3038 believes it can use native alignment or still uses 128-bit alignment. */
3039 if (TARGET_VSX && !TARGET_VSX_ALIGN_128)
3041 align64 = 64;
3042 align32 = 32;
3044 else
3046 align64 = 128;
3047 align32 = 128;
3050 /* KF mode (IEEE 128-bit in VSX registers). We do not have arithmetic, so
3051 only set the memory modes. Include TFmode if -mabi=ieeelongdouble. */
3052 if (TARGET_FLOAT128_TYPE)
3054 rs6000_vector_mem[KFmode] = VECTOR_VSX;
3055 rs6000_vector_align[KFmode] = 128;
3057 if (FLOAT128_IEEE_P (TFmode))
3059 rs6000_vector_mem[TFmode] = VECTOR_VSX;
3060 rs6000_vector_align[TFmode] = 128;
3064 /* V2DF mode, VSX only. */
3065 if (TARGET_VSX)
3067 rs6000_vector_unit[V2DFmode] = VECTOR_VSX;
3068 rs6000_vector_mem[V2DFmode] = VECTOR_VSX;
3069 rs6000_vector_align[V2DFmode] = align64;
3072 /* V4SF mode, either VSX or Altivec. */
3073 if (TARGET_VSX)
3075 rs6000_vector_unit[V4SFmode] = VECTOR_VSX;
3076 rs6000_vector_mem[V4SFmode] = VECTOR_VSX;
3077 rs6000_vector_align[V4SFmode] = align32;
3079 else if (TARGET_ALTIVEC)
3081 rs6000_vector_unit[V4SFmode] = VECTOR_ALTIVEC;
3082 rs6000_vector_mem[V4SFmode] = VECTOR_ALTIVEC;
3083 rs6000_vector_align[V4SFmode] = align32;
3086 /* V16QImode, V8HImode, V4SImode are Altivec only, but possibly do VSX loads
3087 and stores. */
3088 if (TARGET_ALTIVEC)
3090 rs6000_vector_unit[V4SImode] = VECTOR_ALTIVEC;
3091 rs6000_vector_unit[V8HImode] = VECTOR_ALTIVEC;
3092 rs6000_vector_unit[V16QImode] = VECTOR_ALTIVEC;
3093 rs6000_vector_align[V4SImode] = align32;
3094 rs6000_vector_align[V8HImode] = align32;
3095 rs6000_vector_align[V16QImode] = align32;
3097 if (TARGET_VSX)
3099 rs6000_vector_mem[V4SImode] = VECTOR_VSX;
3100 rs6000_vector_mem[V8HImode] = VECTOR_VSX;
3101 rs6000_vector_mem[V16QImode] = VECTOR_VSX;
3103 else
3105 rs6000_vector_mem[V4SImode] = VECTOR_ALTIVEC;
3106 rs6000_vector_mem[V8HImode] = VECTOR_ALTIVEC;
3107 rs6000_vector_mem[V16QImode] = VECTOR_ALTIVEC;
3111 /* V2DImode, full mode depends on ISA 2.07 vector mode. Allow under VSX to
3112 do insert/splat/extract. Altivec doesn't have 64-bit integer support. */
3113 if (TARGET_VSX)
3115 rs6000_vector_mem[V2DImode] = VECTOR_VSX;
3116 rs6000_vector_unit[V2DImode]
3117 = (TARGET_P8_VECTOR) ? VECTOR_P8_VECTOR : VECTOR_NONE;
3118 rs6000_vector_align[V2DImode] = align64;
3120 rs6000_vector_mem[V1TImode] = VECTOR_VSX;
3121 rs6000_vector_unit[V1TImode]
3122 = (TARGET_P8_VECTOR) ? VECTOR_P8_VECTOR : VECTOR_NONE;
3123 rs6000_vector_align[V1TImode] = 128;
3126 /* DFmode, see if we want to use the VSX unit. Memory is handled
3127 differently, so don't set rs6000_vector_mem. */
3128 if (TARGET_VSX)
3130 rs6000_vector_unit[DFmode] = VECTOR_VSX;
3131 rs6000_vector_align[DFmode] = 64;
3134 /* SFmode, see if we want to use the VSX unit. */
3135 if (TARGET_P8_VECTOR)
3137 rs6000_vector_unit[SFmode] = VECTOR_VSX;
3138 rs6000_vector_align[SFmode] = 32;
3141 /* Allow TImode in VSX register and set the VSX memory macros. */
3142 if (TARGET_VSX)
3144 rs6000_vector_mem[TImode] = VECTOR_VSX;
3145 rs6000_vector_align[TImode] = align64;
3148 /* Register class constraints for the constraints that depend on compile
3149 switches. When the VSX code was added, different constraints were added
3150 based on the type (DFmode, V2DFmode, V4SFmode). For the vector types, all
3151 of the VSX registers are used. The register classes for scalar floating
3152 point types is set, based on whether we allow that type into the upper
3153 (Altivec) registers. GCC has register classes to target the Altivec
3154 registers for load/store operations, to select using a VSX memory
3155 operation instead of the traditional floating point operation. The
3156 constraints are:
3158 d - Register class to use with traditional DFmode instructions.
3159 f - Register class to use with traditional SFmode instructions.
3160 v - Altivec register.
3161 wa - Any VSX register.
3162 wc - Reserved to represent individual CR bits (used in LLVM).
3163 wd - Preferred register class for V2DFmode.
3164 wf - Preferred register class for V4SFmode.
3165 wg - Float register for power6x move insns.
3166 wh - FP register for direct move instructions.
3167 wi - FP or VSX register to hold 64-bit integers for VSX insns.
3168 wj - FP or VSX register to hold 64-bit integers for direct moves.
3169 wk - FP or VSX register to hold 64-bit doubles for direct moves.
3170 wl - Float register if we can do 32-bit signed int loads.
3171 wm - VSX register for ISA 2.07 direct move operations.
3172 wn - always NO_REGS.
3173 wr - GPR if 64-bit mode is permitted.
3174 ws - Register class to do ISA 2.06 DF operations.
3175 wt - VSX register for TImode in VSX registers.
3176 wv - Altivec register for ISA 2.06 VSX DF/DI load/stores.
3177 ww - Register class to do SF conversions in with VSX operations.
3178 wx - Float register if we can do 32-bit int stores.
3179 wz - Float register if we can do 32-bit unsigned int loads. */
3181 if (TARGET_HARD_FLOAT)
3183 rs6000_constraints[RS6000_CONSTRAINT_f] = FLOAT_REGS; /* SFmode */
3184 rs6000_constraints[RS6000_CONSTRAINT_d] = FLOAT_REGS; /* DFmode */
3187 if (TARGET_VSX)
3189 rs6000_constraints[RS6000_CONSTRAINT_wa] = VSX_REGS;
3190 rs6000_constraints[RS6000_CONSTRAINT_wd] = VSX_REGS; /* V2DFmode */
3191 rs6000_constraints[RS6000_CONSTRAINT_wf] = VSX_REGS; /* V4SFmode */
3192 rs6000_constraints[RS6000_CONSTRAINT_ws] = VSX_REGS; /* DFmode */
3193 rs6000_constraints[RS6000_CONSTRAINT_wv] = ALTIVEC_REGS; /* DFmode */
3194 rs6000_constraints[RS6000_CONSTRAINT_wi] = VSX_REGS; /* DImode */
3195 rs6000_constraints[RS6000_CONSTRAINT_wt] = VSX_REGS; /* TImode */
3198 /* Add conditional constraints based on various options, to allow us to
3199 collapse multiple insn patterns. */
3200 if (TARGET_ALTIVEC)
3201 rs6000_constraints[RS6000_CONSTRAINT_v] = ALTIVEC_REGS;
3203 if (TARGET_MFPGPR) /* DFmode */
3204 rs6000_constraints[RS6000_CONSTRAINT_wg] = FLOAT_REGS;
3206 if (TARGET_LFIWAX)
3207 rs6000_constraints[RS6000_CONSTRAINT_wl] = FLOAT_REGS; /* DImode */
3209 if (TARGET_DIRECT_MOVE)
3211 rs6000_constraints[RS6000_CONSTRAINT_wh] = FLOAT_REGS;
3212 rs6000_constraints[RS6000_CONSTRAINT_wj] /* DImode */
3213 = rs6000_constraints[RS6000_CONSTRAINT_wi];
3214 rs6000_constraints[RS6000_CONSTRAINT_wk] /* DFmode */
3215 = rs6000_constraints[RS6000_CONSTRAINT_ws];
3216 rs6000_constraints[RS6000_CONSTRAINT_wm] = VSX_REGS;
3219 if (TARGET_POWERPC64)
3221 rs6000_constraints[RS6000_CONSTRAINT_wr] = GENERAL_REGS;
3222 rs6000_constraints[RS6000_CONSTRAINT_wA] = BASE_REGS;
3225 if (TARGET_P8_VECTOR) /* SFmode */
3226 rs6000_constraints[RS6000_CONSTRAINT_ww] = VSX_REGS;
3227 else if (TARGET_VSX)
3228 rs6000_constraints[RS6000_CONSTRAINT_ww] = FLOAT_REGS;
3230 if (TARGET_STFIWX)
3231 rs6000_constraints[RS6000_CONSTRAINT_wx] = FLOAT_REGS; /* DImode */
3233 if (TARGET_LFIWZX)
3234 rs6000_constraints[RS6000_CONSTRAINT_wz] = FLOAT_REGS; /* DImode */
3236 if (TARGET_FLOAT128_TYPE)
3238 rs6000_constraints[RS6000_CONSTRAINT_wq] = VSX_REGS; /* KFmode */
3239 if (FLOAT128_IEEE_P (TFmode))
3240 rs6000_constraints[RS6000_CONSTRAINT_wp] = VSX_REGS; /* TFmode */
3243 /* Support for new direct moves (ISA 3.0 + 64bit). */
3244 if (TARGET_DIRECT_MOVE_128)
3245 rs6000_constraints[RS6000_CONSTRAINT_we] = VSX_REGS;
3247 /* Set up the reload helper and direct move functions. */
3248 if (TARGET_VSX || TARGET_ALTIVEC)
3250 if (TARGET_64BIT)
3252 reg_addr[V16QImode].reload_store = CODE_FOR_reload_v16qi_di_store;
3253 reg_addr[V16QImode].reload_load = CODE_FOR_reload_v16qi_di_load;
3254 reg_addr[V8HImode].reload_store = CODE_FOR_reload_v8hi_di_store;
3255 reg_addr[V8HImode].reload_load = CODE_FOR_reload_v8hi_di_load;
3256 reg_addr[V4SImode].reload_store = CODE_FOR_reload_v4si_di_store;
3257 reg_addr[V4SImode].reload_load = CODE_FOR_reload_v4si_di_load;
3258 reg_addr[V2DImode].reload_store = CODE_FOR_reload_v2di_di_store;
3259 reg_addr[V2DImode].reload_load = CODE_FOR_reload_v2di_di_load;
3260 reg_addr[V1TImode].reload_store = CODE_FOR_reload_v1ti_di_store;
3261 reg_addr[V1TImode].reload_load = CODE_FOR_reload_v1ti_di_load;
3262 reg_addr[V4SFmode].reload_store = CODE_FOR_reload_v4sf_di_store;
3263 reg_addr[V4SFmode].reload_load = CODE_FOR_reload_v4sf_di_load;
3264 reg_addr[V2DFmode].reload_store = CODE_FOR_reload_v2df_di_store;
3265 reg_addr[V2DFmode].reload_load = CODE_FOR_reload_v2df_di_load;
3266 reg_addr[DFmode].reload_store = CODE_FOR_reload_df_di_store;
3267 reg_addr[DFmode].reload_load = CODE_FOR_reload_df_di_load;
3268 reg_addr[DDmode].reload_store = CODE_FOR_reload_dd_di_store;
3269 reg_addr[DDmode].reload_load = CODE_FOR_reload_dd_di_load;
3270 reg_addr[SFmode].reload_store = CODE_FOR_reload_sf_di_store;
3271 reg_addr[SFmode].reload_load = CODE_FOR_reload_sf_di_load;
3273 if (FLOAT128_VECTOR_P (KFmode))
3275 reg_addr[KFmode].reload_store = CODE_FOR_reload_kf_di_store;
3276 reg_addr[KFmode].reload_load = CODE_FOR_reload_kf_di_load;
3279 if (FLOAT128_VECTOR_P (TFmode))
3281 reg_addr[TFmode].reload_store = CODE_FOR_reload_tf_di_store;
3282 reg_addr[TFmode].reload_load = CODE_FOR_reload_tf_di_load;
3285 /* Only provide a reload handler for SDmode if lfiwzx/stfiwx are
3286 available. */
3287 if (TARGET_NO_SDMODE_STACK)
3289 reg_addr[SDmode].reload_store = CODE_FOR_reload_sd_di_store;
3290 reg_addr[SDmode].reload_load = CODE_FOR_reload_sd_di_load;
3293 if (TARGET_VSX)
3295 reg_addr[TImode].reload_store = CODE_FOR_reload_ti_di_store;
3296 reg_addr[TImode].reload_load = CODE_FOR_reload_ti_di_load;
3299 if (TARGET_DIRECT_MOVE && !TARGET_DIRECT_MOVE_128)
3301 reg_addr[TImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxti;
3302 reg_addr[V1TImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv1ti;
3303 reg_addr[V2DFmode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv2df;
3304 reg_addr[V2DImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv2di;
3305 reg_addr[V4SFmode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv4sf;
3306 reg_addr[V4SImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv4si;
3307 reg_addr[V8HImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv8hi;
3308 reg_addr[V16QImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv16qi;
3309 reg_addr[SFmode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxsf;
3311 reg_addr[TImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprti;
3312 reg_addr[V1TImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv1ti;
3313 reg_addr[V2DFmode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv2df;
3314 reg_addr[V2DImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv2di;
3315 reg_addr[V4SFmode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv4sf;
3316 reg_addr[V4SImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv4si;
3317 reg_addr[V8HImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv8hi;
3318 reg_addr[V16QImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv16qi;
3319 reg_addr[SFmode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprsf;
3321 if (FLOAT128_VECTOR_P (KFmode))
3323 reg_addr[KFmode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxkf;
3324 reg_addr[KFmode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprkf;
3327 if (FLOAT128_VECTOR_P (TFmode))
3329 reg_addr[TFmode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxtf;
3330 reg_addr[TFmode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprtf;
3334 else
3336 reg_addr[V16QImode].reload_store = CODE_FOR_reload_v16qi_si_store;
3337 reg_addr[V16QImode].reload_load = CODE_FOR_reload_v16qi_si_load;
3338 reg_addr[V8HImode].reload_store = CODE_FOR_reload_v8hi_si_store;
3339 reg_addr[V8HImode].reload_load = CODE_FOR_reload_v8hi_si_load;
3340 reg_addr[V4SImode].reload_store = CODE_FOR_reload_v4si_si_store;
3341 reg_addr[V4SImode].reload_load = CODE_FOR_reload_v4si_si_load;
3342 reg_addr[V2DImode].reload_store = CODE_FOR_reload_v2di_si_store;
3343 reg_addr[V2DImode].reload_load = CODE_FOR_reload_v2di_si_load;
3344 reg_addr[V1TImode].reload_store = CODE_FOR_reload_v1ti_si_store;
3345 reg_addr[V1TImode].reload_load = CODE_FOR_reload_v1ti_si_load;
3346 reg_addr[V4SFmode].reload_store = CODE_FOR_reload_v4sf_si_store;
3347 reg_addr[V4SFmode].reload_load = CODE_FOR_reload_v4sf_si_load;
3348 reg_addr[V2DFmode].reload_store = CODE_FOR_reload_v2df_si_store;
3349 reg_addr[V2DFmode].reload_load = CODE_FOR_reload_v2df_si_load;
3350 reg_addr[DFmode].reload_store = CODE_FOR_reload_df_si_store;
3351 reg_addr[DFmode].reload_load = CODE_FOR_reload_df_si_load;
3352 reg_addr[DDmode].reload_store = CODE_FOR_reload_dd_si_store;
3353 reg_addr[DDmode].reload_load = CODE_FOR_reload_dd_si_load;
3354 reg_addr[SFmode].reload_store = CODE_FOR_reload_sf_si_store;
3355 reg_addr[SFmode].reload_load = CODE_FOR_reload_sf_si_load;
3357 if (FLOAT128_VECTOR_P (KFmode))
3359 reg_addr[KFmode].reload_store = CODE_FOR_reload_kf_si_store;
3360 reg_addr[KFmode].reload_load = CODE_FOR_reload_kf_si_load;
3363 if (FLOAT128_IEEE_P (TFmode))
3365 reg_addr[TFmode].reload_store = CODE_FOR_reload_tf_si_store;
3366 reg_addr[TFmode].reload_load = CODE_FOR_reload_tf_si_load;
3369 /* Only provide a reload handler for SDmode if lfiwzx/stfiwx are
3370 available. */
3371 if (TARGET_NO_SDMODE_STACK)
3373 reg_addr[SDmode].reload_store = CODE_FOR_reload_sd_si_store;
3374 reg_addr[SDmode].reload_load = CODE_FOR_reload_sd_si_load;
3377 if (TARGET_VSX)
3379 reg_addr[TImode].reload_store = CODE_FOR_reload_ti_si_store;
3380 reg_addr[TImode].reload_load = CODE_FOR_reload_ti_si_load;
3383 if (TARGET_DIRECT_MOVE)
3385 reg_addr[DImode].reload_fpr_gpr = CODE_FOR_reload_fpr_from_gprdi;
3386 reg_addr[DDmode].reload_fpr_gpr = CODE_FOR_reload_fpr_from_gprdd;
3387 reg_addr[DFmode].reload_fpr_gpr = CODE_FOR_reload_fpr_from_gprdf;
3391 reg_addr[DFmode].scalar_in_vmx_p = true;
3392 reg_addr[DImode].scalar_in_vmx_p = true;
3394 if (TARGET_P8_VECTOR)
3396 reg_addr[SFmode].scalar_in_vmx_p = true;
3397 reg_addr[SImode].scalar_in_vmx_p = true;
3399 if (TARGET_P9_VECTOR)
3401 reg_addr[HImode].scalar_in_vmx_p = true;
3402 reg_addr[QImode].scalar_in_vmx_p = true;
3407 /* Precalculate HARD_REGNO_NREGS. */
3408 for (r = 0; HARD_REGISTER_NUM_P (r); ++r)
3409 for (m = 0; m < NUM_MACHINE_MODES; ++m)
3410 rs6000_hard_regno_nregs[m][r]
3411 = rs6000_hard_regno_nregs_internal (r, (machine_mode) m);
3413 /* Precalculate TARGET_HARD_REGNO_MODE_OK. */
3414 for (r = 0; HARD_REGISTER_NUM_P (r); ++r)
3415 for (m = 0; m < NUM_MACHINE_MODES; ++m)
3416 rs6000_hard_regno_mode_ok_p[m][r]
3417 = rs6000_hard_regno_mode_ok_uncached (r, (machine_mode) m);
3419 /* Precalculate CLASS_MAX_NREGS sizes. */
3420 for (c = 0; c < LIM_REG_CLASSES; ++c)
3422 int reg_size;
3424 if (TARGET_VSX && VSX_REG_CLASS_P (c))
3425 reg_size = UNITS_PER_VSX_WORD;
3427 else if (c == ALTIVEC_REGS)
3428 reg_size = UNITS_PER_ALTIVEC_WORD;
3430 else if (c == FLOAT_REGS)
3431 reg_size = UNITS_PER_FP_WORD;
3433 else
3434 reg_size = UNITS_PER_WORD;
3436 for (m = 0; m < NUM_MACHINE_MODES; ++m)
3438 machine_mode m2 = (machine_mode)m;
3439 int reg_size2 = reg_size;
3441 /* TDmode & IBM 128-bit floating point always takes 2 registers, even
3442 in VSX. */
3443 if (TARGET_VSX && VSX_REG_CLASS_P (c) && FLOAT128_2REG_P (m))
3444 reg_size2 = UNITS_PER_FP_WORD;
3446 rs6000_class_max_nregs[m][c]
3447 = (GET_MODE_SIZE (m2) + reg_size2 - 1) / reg_size2;
3451 /* Calculate which modes to automatically generate code to use a the
3452 reciprocal divide and square root instructions. In the future, possibly
3453 automatically generate the instructions even if the user did not specify
3454 -mrecip. The older machines double precision reciprocal sqrt estimate is
3455 not accurate enough. */
3456 memset (rs6000_recip_bits, 0, sizeof (rs6000_recip_bits));
3457 if (TARGET_FRES)
3458 rs6000_recip_bits[SFmode] = RS6000_RECIP_MASK_HAVE_RE;
3459 if (TARGET_FRE)
3460 rs6000_recip_bits[DFmode] = RS6000_RECIP_MASK_HAVE_RE;
3461 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode))
3462 rs6000_recip_bits[V4SFmode] = RS6000_RECIP_MASK_HAVE_RE;
3463 if (VECTOR_UNIT_VSX_P (V2DFmode))
3464 rs6000_recip_bits[V2DFmode] = RS6000_RECIP_MASK_HAVE_RE;
3466 if (TARGET_FRSQRTES)
3467 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
3468 if (TARGET_FRSQRTE)
3469 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
3470 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode))
3471 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
3472 if (VECTOR_UNIT_VSX_P (V2DFmode))
3473 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
3475 if (rs6000_recip_control)
3477 if (!flag_finite_math_only)
3478 warning (0, "%qs requires %qs or %qs", "-mrecip", "-ffinite-math",
3479 "-ffast-math");
3480 if (flag_trapping_math)
3481 warning (0, "%qs requires %qs or %qs", "-mrecip",
3482 "-fno-trapping-math", "-ffast-math");
3483 if (!flag_reciprocal_math)
3484 warning (0, "%qs requires %qs or %qs", "-mrecip", "-freciprocal-math",
3485 "-ffast-math");
3486 if (flag_finite_math_only && !flag_trapping_math && flag_reciprocal_math)
3488 if (RS6000_RECIP_HAVE_RE_P (SFmode)
3489 && (rs6000_recip_control & RECIP_SF_DIV) != 0)
3490 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_AUTO_RE;
3492 if (RS6000_RECIP_HAVE_RE_P (DFmode)
3493 && (rs6000_recip_control & RECIP_DF_DIV) != 0)
3494 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_AUTO_RE;
3496 if (RS6000_RECIP_HAVE_RE_P (V4SFmode)
3497 && (rs6000_recip_control & RECIP_V4SF_DIV) != 0)
3498 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_AUTO_RE;
3500 if (RS6000_RECIP_HAVE_RE_P (V2DFmode)
3501 && (rs6000_recip_control & RECIP_V2DF_DIV) != 0)
3502 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_AUTO_RE;
3504 if (RS6000_RECIP_HAVE_RSQRTE_P (SFmode)
3505 && (rs6000_recip_control & RECIP_SF_RSQRT) != 0)
3506 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
3508 if (RS6000_RECIP_HAVE_RSQRTE_P (DFmode)
3509 && (rs6000_recip_control & RECIP_DF_RSQRT) != 0)
3510 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
3512 if (RS6000_RECIP_HAVE_RSQRTE_P (V4SFmode)
3513 && (rs6000_recip_control & RECIP_V4SF_RSQRT) != 0)
3514 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
3516 if (RS6000_RECIP_HAVE_RSQRTE_P (V2DFmode)
3517 && (rs6000_recip_control & RECIP_V2DF_RSQRT) != 0)
3518 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
3522 /* Update the addr mask bits in reg_addr to help secondary reload and go if
3523 legitimate address support to figure out the appropriate addressing to
3524 use. */
3525 rs6000_setup_reg_addr_masks ();
3527 if (global_init_p || TARGET_DEBUG_TARGET)
3529 if (TARGET_DEBUG_REG)
3530 rs6000_debug_reg_global ();
3532 if (TARGET_DEBUG_COST || TARGET_DEBUG_REG)
3533 fprintf (stderr,
3534 "SImode variable mult cost = %d\n"
3535 "SImode constant mult cost = %d\n"
3536 "SImode short constant mult cost = %d\n"
3537 "DImode multipliciation cost = %d\n"
3538 "SImode division cost = %d\n"
3539 "DImode division cost = %d\n"
3540 "Simple fp operation cost = %d\n"
3541 "DFmode multiplication cost = %d\n"
3542 "SFmode division cost = %d\n"
3543 "DFmode division cost = %d\n"
3544 "cache line size = %d\n"
3545 "l1 cache size = %d\n"
3546 "l2 cache size = %d\n"
3547 "simultaneous prefetches = %d\n"
3548 "\n",
3549 rs6000_cost->mulsi,
3550 rs6000_cost->mulsi_const,
3551 rs6000_cost->mulsi_const9,
3552 rs6000_cost->muldi,
3553 rs6000_cost->divsi,
3554 rs6000_cost->divdi,
3555 rs6000_cost->fp,
3556 rs6000_cost->dmul,
3557 rs6000_cost->sdiv,
3558 rs6000_cost->ddiv,
3559 rs6000_cost->cache_line_size,
3560 rs6000_cost->l1_cache_size,
3561 rs6000_cost->l2_cache_size,
3562 rs6000_cost->simultaneous_prefetches);
3566 #if TARGET_MACHO
3567 /* The Darwin version of SUBTARGET_OVERRIDE_OPTIONS. */
3569 static void
3570 darwin_rs6000_override_options (void)
3572 /* The Darwin ABI always includes AltiVec, can't be (validly) turned
3573 off. */
3574 rs6000_altivec_abi = 1;
3575 TARGET_ALTIVEC_VRSAVE = 1;
3576 rs6000_current_abi = ABI_DARWIN;
3578 if (DEFAULT_ABI == ABI_DARWIN
3579 && TARGET_64BIT)
3580 darwin_one_byte_bool = 1;
3582 if (TARGET_64BIT && ! TARGET_POWERPC64)
3584 rs6000_isa_flags |= OPTION_MASK_POWERPC64;
3585 warning (0, "%qs requires PowerPC64 architecture, enabling", "-m64");
3587 if (flag_mkernel)
3589 rs6000_default_long_calls = 1;
3590 rs6000_isa_flags |= OPTION_MASK_SOFT_FLOAT;
3593 /* Make -m64 imply -maltivec. Darwin's 64-bit ABI includes
3594 Altivec. */
3595 if (!flag_mkernel && !flag_apple_kext
3596 && TARGET_64BIT
3597 && ! (rs6000_isa_flags_explicit & OPTION_MASK_ALTIVEC))
3598 rs6000_isa_flags |= OPTION_MASK_ALTIVEC;
3600 /* Unless the user (not the configurer) has explicitly overridden
3601 it with -mcpu=G3 or -mno-altivec, then 10.5+ targets default to
3602 G4 unless targeting the kernel. */
3603 if (!flag_mkernel
3604 && !flag_apple_kext
3605 && strverscmp (darwin_macosx_version_min, "10.5") >= 0
3606 && ! (rs6000_isa_flags_explicit & OPTION_MASK_ALTIVEC)
3607 && ! global_options_set.x_rs6000_cpu_index)
3609 rs6000_isa_flags |= OPTION_MASK_ALTIVEC;
3612 #endif
3614 /* If not otherwise specified by a target, make 'long double' equivalent to
3615 'double'. */
3617 #ifndef RS6000_DEFAULT_LONG_DOUBLE_SIZE
3618 #define RS6000_DEFAULT_LONG_DOUBLE_SIZE 64
3619 #endif
3621 /* Return the builtin mask of the various options used that could affect which
3622 builtins were used. In the past we used target_flags, but we've run out of
3623 bits, and some options are no longer in target_flags. */
3625 HOST_WIDE_INT
3626 rs6000_builtin_mask_calculate (void)
3628 return (((TARGET_ALTIVEC) ? RS6000_BTM_ALTIVEC : 0)
3629 | ((TARGET_CMPB) ? RS6000_BTM_CMPB : 0)
3630 | ((TARGET_VSX) ? RS6000_BTM_VSX : 0)
3631 | ((TARGET_FRE) ? RS6000_BTM_FRE : 0)
3632 | ((TARGET_FRES) ? RS6000_BTM_FRES : 0)
3633 | ((TARGET_FRSQRTE) ? RS6000_BTM_FRSQRTE : 0)
3634 | ((TARGET_FRSQRTES) ? RS6000_BTM_FRSQRTES : 0)
3635 | ((TARGET_POPCNTD) ? RS6000_BTM_POPCNTD : 0)
3636 | ((rs6000_cpu == PROCESSOR_CELL) ? RS6000_BTM_CELL : 0)
3637 | ((TARGET_P8_VECTOR) ? RS6000_BTM_P8_VECTOR : 0)
3638 | ((TARGET_P9_VECTOR) ? RS6000_BTM_P9_VECTOR : 0)
3639 | ((TARGET_P9_MISC) ? RS6000_BTM_P9_MISC : 0)
3640 | ((TARGET_MODULO) ? RS6000_BTM_MODULO : 0)
3641 | ((TARGET_64BIT) ? RS6000_BTM_64BIT : 0)
3642 | ((TARGET_POWERPC64) ? RS6000_BTM_POWERPC64 : 0)
3643 | ((TARGET_CRYPTO) ? RS6000_BTM_CRYPTO : 0)
3644 | ((TARGET_HTM) ? RS6000_BTM_HTM : 0)
3645 | ((TARGET_DFP) ? RS6000_BTM_DFP : 0)
3646 | ((TARGET_HARD_FLOAT) ? RS6000_BTM_HARD_FLOAT : 0)
3647 | ((TARGET_LONG_DOUBLE_128
3648 && TARGET_HARD_FLOAT
3649 && !TARGET_IEEEQUAD) ? RS6000_BTM_LDBL128 : 0)
3650 | ((TARGET_FLOAT128_TYPE) ? RS6000_BTM_FLOAT128 : 0)
3651 | ((TARGET_FLOAT128_HW) ? RS6000_BTM_FLOAT128_HW : 0));
3654 /* Implement TARGET_MD_ASM_ADJUST. All asm statements are considered
3655 to clobber the XER[CA] bit because clobbering that bit without telling
3656 the compiler worked just fine with versions of GCC before GCC 5, and
3657 breaking a lot of older code in ways that are hard to track down is
3658 not such a great idea. */
3660 static rtx_insn *
3661 rs6000_md_asm_adjust (vec<rtx> &/*outputs*/, vec<rtx> &/*inputs*/,
3662 vec<const char *> &/*constraints*/,
3663 vec<rtx> &clobbers, HARD_REG_SET &clobbered_regs)
3665 clobbers.safe_push (gen_rtx_REG (SImode, CA_REGNO));
3666 SET_HARD_REG_BIT (clobbered_regs, CA_REGNO);
3667 return NULL;
3670 /* Override command line options.
3672 Combine build-specific configuration information with options
3673 specified on the command line to set various state variables which
3674 influence code generation, optimization, and expansion of built-in
3675 functions. Assure that command-line configuration preferences are
3676 compatible with each other and with the build configuration; issue
3677 warnings while adjusting configuration or error messages while
3678 rejecting configuration.
3680 Upon entry to this function:
3682 This function is called once at the beginning of
3683 compilation, and then again at the start and end of compiling
3684 each section of code that has a different configuration, as
3685 indicated, for example, by adding the
3687 __attribute__((__target__("cpu=power9")))
3689 qualifier to a function definition or, for example, by bracketing
3690 code between
3692 #pragma GCC target("altivec")
3696 #pragma GCC reset_options
3698 directives. Parameter global_init_p is true for the initial
3699 invocation, which initializes global variables, and false for all
3700 subsequent invocations.
3703 Various global state information is assumed to be valid. This
3704 includes OPTION_TARGET_CPU_DEFAULT, representing the name of the
3705 default CPU specified at build configure time, TARGET_DEFAULT,
3706 representing the default set of option flags for the default
3707 target, and global_options_set.x_rs6000_isa_flags, representing
3708 which options were requested on the command line.
3710 Upon return from this function:
3712 rs6000_isa_flags_explicit has a non-zero bit for each flag that
3713 was set by name on the command line. Additionally, if certain
3714 attributes are automatically enabled or disabled by this function
3715 in order to assure compatibility between options and
3716 configuration, the flags associated with those attributes are
3717 also set. By setting these "explicit bits", we avoid the risk
3718 that other code might accidentally overwrite these particular
3719 attributes with "default values".
3721 The various bits of rs6000_isa_flags are set to indicate the
3722 target options that have been selected for the most current
3723 compilation efforts. This has the effect of also turning on the
3724 associated TARGET_XXX values since these are macros which are
3725 generally defined to test the corresponding bit of the
3726 rs6000_isa_flags variable.
3728 The variable rs6000_builtin_mask is set to represent the target
3729 options for the most current compilation efforts, consistent with
3730 the current contents of rs6000_isa_flags. This variable controls
3731 expansion of built-in functions.
3733 Various other global variables and fields of global structures
3734 (over 50 in all) are initialized to reflect the desired options
3735 for the most current compilation efforts. */
3737 static bool
3738 rs6000_option_override_internal (bool global_init_p)
3740 bool ret = true;
3742 HOST_WIDE_INT set_masks;
3743 HOST_WIDE_INT ignore_masks;
3744 int cpu_index = -1;
3745 int tune_index;
3746 struct cl_target_option *main_target_opt
3747 = ((global_init_p || target_option_default_node == NULL)
3748 ? NULL : TREE_TARGET_OPTION (target_option_default_node));
3750 /* Print defaults. */
3751 if ((TARGET_DEBUG_REG || TARGET_DEBUG_TARGET) && global_init_p)
3752 rs6000_print_isa_options (stderr, 0, "TARGET_DEFAULT", TARGET_DEFAULT);
3754 /* Remember the explicit arguments. */
3755 if (global_init_p)
3756 rs6000_isa_flags_explicit = global_options_set.x_rs6000_isa_flags;
3758 /* On 64-bit Darwin, power alignment is ABI-incompatible with some C
3759 library functions, so warn about it. The flag may be useful for
3760 performance studies from time to time though, so don't disable it
3761 entirely. */
3762 if (global_options_set.x_rs6000_alignment_flags
3763 && rs6000_alignment_flags == MASK_ALIGN_POWER
3764 && DEFAULT_ABI == ABI_DARWIN
3765 && TARGET_64BIT)
3766 warning (0, "%qs is not supported for 64-bit Darwin;"
3767 " it is incompatible with the installed C and C++ libraries",
3768 "-malign-power");
3770 /* Numerous experiment shows that IRA based loop pressure
3771 calculation works better for RTL loop invariant motion on targets
3772 with enough (>= 32) registers. It is an expensive optimization.
3773 So it is on only for peak performance. */
3774 if (optimize >= 3 && global_init_p
3775 && !global_options_set.x_flag_ira_loop_pressure)
3776 flag_ira_loop_pressure = 1;
3778 /* -fsanitize=address needs to turn on -fasynchronous-unwind-tables in order
3779 for tracebacks to be complete but not if any -fasynchronous-unwind-tables
3780 options were already specified. */
3781 if (flag_sanitize & SANITIZE_USER_ADDRESS
3782 && !global_options_set.x_flag_asynchronous_unwind_tables)
3783 flag_asynchronous_unwind_tables = 1;
3785 /* Set the pointer size. */
3786 if (TARGET_64BIT)
3788 rs6000_pmode = DImode;
3789 rs6000_pointer_size = 64;
3791 else
3793 rs6000_pmode = SImode;
3794 rs6000_pointer_size = 32;
3797 /* Some OSs don't support saving the high part of 64-bit registers on context
3798 switch. Other OSs don't support saving Altivec registers. On those OSs,
3799 we don't touch the OPTION_MASK_POWERPC64 or OPTION_MASK_ALTIVEC settings;
3800 if the user wants either, the user must explicitly specify them and we
3801 won't interfere with the user's specification. */
3803 set_masks = POWERPC_MASKS;
3804 #ifdef OS_MISSING_POWERPC64
3805 if (OS_MISSING_POWERPC64)
3806 set_masks &= ~OPTION_MASK_POWERPC64;
3807 #endif
3808 #ifdef OS_MISSING_ALTIVEC
3809 if (OS_MISSING_ALTIVEC)
3810 set_masks &= ~(OPTION_MASK_ALTIVEC | OPTION_MASK_VSX
3811 | OTHER_VSX_VECTOR_MASKS);
3812 #endif
3814 /* Don't override by the processor default if given explicitly. */
3815 set_masks &= ~rs6000_isa_flags_explicit;
3817 if (global_init_p && rs6000_dejagnu_cpu_index >= 0)
3818 rs6000_cpu_index = rs6000_dejagnu_cpu_index;
3820 /* Process the -mcpu=<xxx> and -mtune=<xxx> argument. If the user changed
3821 the cpu in a target attribute or pragma, but did not specify a tuning
3822 option, use the cpu for the tuning option rather than the option specified
3823 with -mtune on the command line. Process a '--with-cpu' configuration
3824 request as an implicit --cpu. */
3825 if (rs6000_cpu_index >= 0)
3826 cpu_index = rs6000_cpu_index;
3827 else if (main_target_opt != NULL && main_target_opt->x_rs6000_cpu_index >= 0)
3828 cpu_index = main_target_opt->x_rs6000_cpu_index;
3829 else if (OPTION_TARGET_CPU_DEFAULT)
3830 cpu_index = rs6000_cpu_name_lookup (OPTION_TARGET_CPU_DEFAULT);
3832 /* If we have a cpu, either through an explicit -mcpu=<xxx> or if the
3833 compiler was configured with --with-cpu=<xxx>, replace all of the ISA bits
3834 with those from the cpu, except for options that were explicitly set. If
3835 we don't have a cpu, do not override the target bits set in
3836 TARGET_DEFAULT. */
3837 if (cpu_index >= 0)
3839 rs6000_cpu_index = cpu_index;
3840 rs6000_isa_flags &= ~set_masks;
3841 rs6000_isa_flags |= (processor_target_table[cpu_index].target_enable
3842 & set_masks);
3844 else
3846 /* If no -mcpu=<xxx>, inherit any default options that were cleared via
3847 POWERPC_MASKS. Originally, TARGET_DEFAULT was used to initialize
3848 target_flags via the TARGET_DEFAULT_TARGET_FLAGS hook. When we switched
3849 to using rs6000_isa_flags, we need to do the initialization here.
3851 If there is a TARGET_DEFAULT, use that. Otherwise fall back to using
3852 -mcpu=powerpc, -mcpu=powerpc64, or -mcpu=powerpc64le defaults. */
3853 HOST_WIDE_INT flags;
3854 if (TARGET_DEFAULT)
3855 flags = TARGET_DEFAULT;
3856 else
3858 /* PowerPC 64-bit LE requires at least ISA 2.07. */
3859 const char *default_cpu = (!TARGET_POWERPC64
3860 ? "powerpc"
3861 : (BYTES_BIG_ENDIAN
3862 ? "powerpc64"
3863 : "powerpc64le"));
3864 int default_cpu_index = rs6000_cpu_name_lookup (default_cpu);
3865 flags = processor_target_table[default_cpu_index].target_enable;
3867 rs6000_isa_flags |= (flags & ~rs6000_isa_flags_explicit);
3870 if (rs6000_tune_index >= 0)
3871 tune_index = rs6000_tune_index;
3872 else if (cpu_index >= 0)
3873 rs6000_tune_index = tune_index = cpu_index;
3874 else
3876 size_t i;
3877 enum processor_type tune_proc
3878 = (TARGET_POWERPC64 ? PROCESSOR_DEFAULT64 : PROCESSOR_DEFAULT);
3880 tune_index = -1;
3881 for (i = 0; i < ARRAY_SIZE (processor_target_table); i++)
3882 if (processor_target_table[i].processor == tune_proc)
3884 tune_index = i;
3885 break;
3889 if (cpu_index >= 0)
3890 rs6000_cpu = processor_target_table[cpu_index].processor;
3891 else
3892 rs6000_cpu = TARGET_POWERPC64 ? PROCESSOR_DEFAULT64 : PROCESSOR_DEFAULT;
3894 gcc_assert (tune_index >= 0);
3895 rs6000_tune = processor_target_table[tune_index].processor;
3897 if (rs6000_cpu == PROCESSOR_PPCE300C2 || rs6000_cpu == PROCESSOR_PPCE300C3
3898 || rs6000_cpu == PROCESSOR_PPCE500MC || rs6000_cpu == PROCESSOR_PPCE500MC64
3899 || rs6000_cpu == PROCESSOR_PPCE5500)
3901 if (TARGET_ALTIVEC)
3902 error ("AltiVec not supported in this target");
3905 /* If we are optimizing big endian systems for space, use the load/store
3906 multiple instructions. */
3907 if (BYTES_BIG_ENDIAN && optimize_size)
3908 rs6000_isa_flags |= ~rs6000_isa_flags_explicit & OPTION_MASK_MULTIPLE;
3910 /* Don't allow -mmultiple on little endian systems unless the cpu is a 750,
3911 because the hardware doesn't support the instructions used in little
3912 endian mode, and causes an alignment trap. The 750 does not cause an
3913 alignment trap (except when the target is unaligned). */
3915 if (!BYTES_BIG_ENDIAN && rs6000_cpu != PROCESSOR_PPC750 && TARGET_MULTIPLE)
3917 rs6000_isa_flags &= ~OPTION_MASK_MULTIPLE;
3918 if ((rs6000_isa_flags_explicit & OPTION_MASK_MULTIPLE) != 0)
3919 warning (0, "%qs is not supported on little endian systems",
3920 "-mmultiple");
3923 /* If little-endian, default to -mstrict-align on older processors.
3924 Testing for htm matches power8 and later. */
3925 if (!BYTES_BIG_ENDIAN
3926 && !(processor_target_table[tune_index].target_enable & OPTION_MASK_HTM))
3927 rs6000_isa_flags |= ~rs6000_isa_flags_explicit & OPTION_MASK_STRICT_ALIGN;
3929 if (!rs6000_fold_gimple)
3930 fprintf (stderr,
3931 "gimple folding of rs6000 builtins has been disabled.\n");
3933 /* Add some warnings for VSX. */
3934 if (TARGET_VSX)
3936 const char *msg = NULL;
3937 if (!TARGET_HARD_FLOAT)
3939 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX)
3940 msg = N_("%<-mvsx%> requires hardware floating point");
3941 else
3943 rs6000_isa_flags &= ~ OPTION_MASK_VSX;
3944 rs6000_isa_flags_explicit |= OPTION_MASK_VSX;
3947 else if (TARGET_AVOID_XFORM > 0)
3948 msg = N_("%<-mvsx%> needs indexed addressing");
3949 else if (!TARGET_ALTIVEC && (rs6000_isa_flags_explicit
3950 & OPTION_MASK_ALTIVEC))
3952 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX)
3953 msg = N_("%<-mvsx%> and %<-mno-altivec%> are incompatible");
3954 else
3955 msg = N_("%<-mno-altivec%> disables vsx");
3958 if (msg)
3960 warning (0, msg);
3961 rs6000_isa_flags &= ~ OPTION_MASK_VSX;
3962 rs6000_isa_flags_explicit |= OPTION_MASK_VSX;
3966 /* If hard-float/altivec/vsx were explicitly turned off then don't allow
3967 the -mcpu setting to enable options that conflict. */
3968 if ((!TARGET_HARD_FLOAT || !TARGET_ALTIVEC || !TARGET_VSX)
3969 && (rs6000_isa_flags_explicit & (OPTION_MASK_SOFT_FLOAT
3970 | OPTION_MASK_ALTIVEC
3971 | OPTION_MASK_VSX)) != 0)
3972 rs6000_isa_flags &= ~((OPTION_MASK_P8_VECTOR | OPTION_MASK_CRYPTO
3973 | OPTION_MASK_DIRECT_MOVE)
3974 & ~rs6000_isa_flags_explicit);
3976 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
3977 rs6000_print_isa_options (stderr, 0, "before defaults", rs6000_isa_flags);
3979 /* Handle explicit -mno-{altivec,vsx,power8-vector,power9-vector} and turn
3980 off all of the options that depend on those flags. */
3981 ignore_masks = rs6000_disable_incompatible_switches ();
3983 /* For the newer switches (vsx, dfp, etc.) set some of the older options,
3984 unless the user explicitly used the -mno-<option> to disable the code. */
3985 if (TARGET_P9_VECTOR || TARGET_MODULO || TARGET_P9_MISC)
3986 rs6000_isa_flags |= (ISA_3_0_MASKS_SERVER & ~ignore_masks);
3987 else if (TARGET_P9_MINMAX)
3989 if (cpu_index >= 0)
3991 if (cpu_index == PROCESSOR_POWER9)
3993 /* legacy behavior: allow -mcpu=power9 with certain
3994 capabilities explicitly disabled. */
3995 rs6000_isa_flags |= (ISA_3_0_MASKS_SERVER & ~ignore_masks);
3997 else
3998 error ("power9 target option is incompatible with %<%s=<xxx>%> "
3999 "for <xxx> less than power9", "-mcpu");
4001 else if ((ISA_3_0_MASKS_SERVER & rs6000_isa_flags_explicit)
4002 != (ISA_3_0_MASKS_SERVER & rs6000_isa_flags
4003 & rs6000_isa_flags_explicit))
4004 /* Enforce that none of the ISA_3_0_MASKS_SERVER flags
4005 were explicitly cleared. */
4006 error ("%qs incompatible with explicitly disabled options",
4007 "-mpower9-minmax");
4008 else
4009 rs6000_isa_flags |= ISA_3_0_MASKS_SERVER;
4011 else if (TARGET_P8_VECTOR || TARGET_DIRECT_MOVE || TARGET_CRYPTO)
4012 rs6000_isa_flags |= (ISA_2_7_MASKS_SERVER & ~ignore_masks);
4013 else if (TARGET_VSX)
4014 rs6000_isa_flags |= (ISA_2_6_MASKS_SERVER & ~ignore_masks);
4015 else if (TARGET_POPCNTD)
4016 rs6000_isa_flags |= (ISA_2_6_MASKS_EMBEDDED & ~ignore_masks);
4017 else if (TARGET_DFP)
4018 rs6000_isa_flags |= (ISA_2_5_MASKS_SERVER & ~ignore_masks);
4019 else if (TARGET_CMPB)
4020 rs6000_isa_flags |= (ISA_2_5_MASKS_EMBEDDED & ~ignore_masks);
4021 else if (TARGET_FPRND)
4022 rs6000_isa_flags |= (ISA_2_4_MASKS & ~ignore_masks);
4023 else if (TARGET_POPCNTB)
4024 rs6000_isa_flags |= (ISA_2_2_MASKS & ~ignore_masks);
4025 else if (TARGET_ALTIVEC)
4026 rs6000_isa_flags |= (OPTION_MASK_PPC_GFXOPT & ~ignore_masks);
4028 if (TARGET_CRYPTO && !TARGET_ALTIVEC)
4030 if (rs6000_isa_flags_explicit & OPTION_MASK_CRYPTO)
4031 error ("%qs requires %qs", "-mcrypto", "-maltivec");
4032 rs6000_isa_flags &= ~OPTION_MASK_CRYPTO;
4035 if (TARGET_DIRECT_MOVE && !TARGET_VSX)
4037 if (rs6000_isa_flags_explicit & OPTION_MASK_DIRECT_MOVE)
4038 error ("%qs requires %qs", "-mdirect-move", "-mvsx");
4039 rs6000_isa_flags &= ~OPTION_MASK_DIRECT_MOVE;
4042 if (TARGET_P8_VECTOR && !TARGET_ALTIVEC)
4044 if (rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR)
4045 error ("%qs requires %qs", "-mpower8-vector", "-maltivec");
4046 rs6000_isa_flags &= ~OPTION_MASK_P8_VECTOR;
4049 if (TARGET_P8_VECTOR && !TARGET_VSX)
4051 if ((rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR)
4052 && (rs6000_isa_flags_explicit & OPTION_MASK_VSX))
4053 error ("%qs requires %qs", "-mpower8-vector", "-mvsx");
4054 else if ((rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR) == 0)
4056 rs6000_isa_flags &= ~OPTION_MASK_P8_VECTOR;
4057 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX)
4058 rs6000_isa_flags_explicit |= OPTION_MASK_P8_VECTOR;
4060 else
4062 /* OPTION_MASK_P8_VECTOR is explicit, and OPTION_MASK_VSX is
4063 not explicit. */
4064 rs6000_isa_flags |= OPTION_MASK_VSX;
4065 rs6000_isa_flags_explicit |= OPTION_MASK_VSX;
4069 if (TARGET_DFP && !TARGET_HARD_FLOAT)
4071 if (rs6000_isa_flags_explicit & OPTION_MASK_DFP)
4072 error ("%qs requires %qs", "-mhard-dfp", "-mhard-float");
4073 rs6000_isa_flags &= ~OPTION_MASK_DFP;
4076 /* The quad memory instructions only works in 64-bit mode. In 32-bit mode,
4077 silently turn off quad memory mode. */
4078 if ((TARGET_QUAD_MEMORY || TARGET_QUAD_MEMORY_ATOMIC) && !TARGET_POWERPC64)
4080 if ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY) != 0)
4081 warning (0, N_("%<-mquad-memory%> requires 64-bit mode"));
4083 if ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY_ATOMIC) != 0)
4084 warning (0, N_("%<-mquad-memory-atomic%> requires 64-bit mode"));
4086 rs6000_isa_flags &= ~(OPTION_MASK_QUAD_MEMORY
4087 | OPTION_MASK_QUAD_MEMORY_ATOMIC);
4090 /* Non-atomic quad memory load/store are disabled for little endian, since
4091 the words are reversed, but atomic operations can still be done by
4092 swapping the words. */
4093 if (TARGET_QUAD_MEMORY && !WORDS_BIG_ENDIAN)
4095 if ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY) != 0)
4096 warning (0, N_("%<-mquad-memory%> is not available in little endian "
4097 "mode"));
4099 rs6000_isa_flags &= ~OPTION_MASK_QUAD_MEMORY;
4102 /* Assume if the user asked for normal quad memory instructions, they want
4103 the atomic versions as well, unless they explicity told us not to use quad
4104 word atomic instructions. */
4105 if (TARGET_QUAD_MEMORY
4106 && !TARGET_QUAD_MEMORY_ATOMIC
4107 && ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY_ATOMIC) == 0))
4108 rs6000_isa_flags |= OPTION_MASK_QUAD_MEMORY_ATOMIC;
4110 /* If we can shrink-wrap the TOC register save separately, then use
4111 -msave-toc-indirect unless explicitly disabled. */
4112 if ((rs6000_isa_flags_explicit & OPTION_MASK_SAVE_TOC_INDIRECT) == 0
4113 && flag_shrink_wrap_separate
4114 && optimize_function_for_speed_p (cfun))
4115 rs6000_isa_flags |= OPTION_MASK_SAVE_TOC_INDIRECT;
4117 /* Enable power8 fusion if we are tuning for power8, even if we aren't
4118 generating power8 instructions. Power9 does not optimize power8 fusion
4119 cases. */
4120 if (!(rs6000_isa_flags_explicit & OPTION_MASK_P8_FUSION))
4122 if (processor_target_table[tune_index].processor == PROCESSOR_POWER8)
4123 rs6000_isa_flags |= OPTION_MASK_P8_FUSION;
4124 else
4125 rs6000_isa_flags &= ~OPTION_MASK_P8_FUSION;
4128 /* Setting additional fusion flags turns on base fusion. */
4129 if (!TARGET_P8_FUSION && TARGET_P8_FUSION_SIGN)
4131 if (rs6000_isa_flags_explicit & OPTION_MASK_P8_FUSION)
4133 if (TARGET_P8_FUSION_SIGN)
4134 error ("%qs requires %qs", "-mpower8-fusion-sign",
4135 "-mpower8-fusion");
4137 rs6000_isa_flags &= ~OPTION_MASK_P8_FUSION;
4139 else
4140 rs6000_isa_flags |= OPTION_MASK_P8_FUSION;
4143 /* Power8 does not fuse sign extended loads with the addis. If we are
4144 optimizing at high levels for speed, convert a sign extended load into a
4145 zero extending load, and an explicit sign extension. */
4146 if (TARGET_P8_FUSION
4147 && !(rs6000_isa_flags_explicit & OPTION_MASK_P8_FUSION_SIGN)
4148 && optimize_function_for_speed_p (cfun)
4149 && optimize >= 3)
4150 rs6000_isa_flags |= OPTION_MASK_P8_FUSION_SIGN;
4152 /* ISA 3.0 vector instructions include ISA 2.07. */
4153 if (TARGET_P9_VECTOR && !TARGET_P8_VECTOR)
4155 /* We prefer to not mention undocumented options in
4156 error messages. However, if users have managed to select
4157 power9-vector without selecting power8-vector, they
4158 already know about undocumented flags. */
4159 if ((rs6000_isa_flags_explicit & OPTION_MASK_P9_VECTOR) &&
4160 (rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR))
4161 error ("%qs requires %qs", "-mpower9-vector", "-mpower8-vector");
4162 else if ((rs6000_isa_flags_explicit & OPTION_MASK_P9_VECTOR) == 0)
4164 rs6000_isa_flags &= ~OPTION_MASK_P9_VECTOR;
4165 if (rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR)
4166 rs6000_isa_flags_explicit |= OPTION_MASK_P9_VECTOR;
4168 else
4170 /* OPTION_MASK_P9_VECTOR is explicit and
4171 OPTION_MASK_P8_VECTOR is not explicit. */
4172 rs6000_isa_flags |= OPTION_MASK_P8_VECTOR;
4173 rs6000_isa_flags_explicit |= OPTION_MASK_P8_VECTOR;
4177 /* Set -mallow-movmisalign to explicitly on if we have full ISA 2.07
4178 support. If we only have ISA 2.06 support, and the user did not specify
4179 the switch, leave it set to -1 so the movmisalign patterns are enabled,
4180 but we don't enable the full vectorization support */
4181 if (TARGET_ALLOW_MOVMISALIGN == -1 && TARGET_P8_VECTOR && TARGET_DIRECT_MOVE)
4182 TARGET_ALLOW_MOVMISALIGN = 1;
4184 else if (TARGET_ALLOW_MOVMISALIGN && !TARGET_VSX)
4186 if (TARGET_ALLOW_MOVMISALIGN > 0
4187 && global_options_set.x_TARGET_ALLOW_MOVMISALIGN)
4188 error ("%qs requires %qs", "-mallow-movmisalign", "-mvsx");
4190 TARGET_ALLOW_MOVMISALIGN = 0;
4193 /* Determine when unaligned vector accesses are permitted, and when
4194 they are preferred over masked Altivec loads. Note that if
4195 TARGET_ALLOW_MOVMISALIGN has been disabled by the user, then
4196 TARGET_EFFICIENT_UNALIGNED_VSX must be as well. The converse is
4197 not true. */
4198 if (TARGET_EFFICIENT_UNALIGNED_VSX)
4200 if (!TARGET_VSX)
4202 if (rs6000_isa_flags_explicit & OPTION_MASK_EFFICIENT_UNALIGNED_VSX)
4203 error ("%qs requires %qs", "-mefficient-unaligned-vsx", "-mvsx");
4205 rs6000_isa_flags &= ~OPTION_MASK_EFFICIENT_UNALIGNED_VSX;
4208 else if (!TARGET_ALLOW_MOVMISALIGN)
4210 if (rs6000_isa_flags_explicit & OPTION_MASK_EFFICIENT_UNALIGNED_VSX)
4211 error ("%qs requires %qs", "-munefficient-unaligned-vsx",
4212 "-mallow-movmisalign");
4214 rs6000_isa_flags &= ~OPTION_MASK_EFFICIENT_UNALIGNED_VSX;
4218 /* Use long double size to select the appropriate long double. We use
4219 TYPE_PRECISION to differentiate the 3 different long double types. We map
4220 128 into the precision used for TFmode. */
4221 int default_long_double_size = (RS6000_DEFAULT_LONG_DOUBLE_SIZE == 64
4222 ? 64
4223 : FLOAT_PRECISION_TFmode);
4225 /* Set long double size before the IEEE 128-bit tests. */
4226 if (!global_options_set.x_rs6000_long_double_type_size)
4228 if (main_target_opt != NULL
4229 && (main_target_opt->x_rs6000_long_double_type_size
4230 != default_long_double_size))
4231 error ("target attribute or pragma changes %<long double%> size");
4232 else
4233 rs6000_long_double_type_size = default_long_double_size;
4235 else if (rs6000_long_double_type_size == 128)
4236 rs6000_long_double_type_size = FLOAT_PRECISION_TFmode;
4237 else if (global_options_set.x_rs6000_ieeequad)
4239 if (global_options.x_rs6000_ieeequad)
4240 error ("%qs requires %qs", "-mabi=ieeelongdouble", "-mlong-double-128");
4241 else
4242 error ("%qs requires %qs", "-mabi=ibmlongdouble", "-mlong-double-128");
4245 /* Set -mabi=ieeelongdouble on some old targets. In the future, power server
4246 systems will also set long double to be IEEE 128-bit. AIX and Darwin
4247 explicitly redefine TARGET_IEEEQUAD and TARGET_IEEEQUAD_DEFAULT to 0, so
4248 those systems will not pick up this default. Warn if the user changes the
4249 default unless -Wno-psabi. */
4250 if (!global_options_set.x_rs6000_ieeequad)
4251 rs6000_ieeequad = TARGET_IEEEQUAD_DEFAULT;
4253 else
4255 if (global_options.x_rs6000_ieeequad
4256 && (!TARGET_POPCNTD || !TARGET_VSX))
4257 error ("%qs requires full ISA 2.06 support", "-mabi=ieeelongdouble");
4259 if (rs6000_ieeequad != TARGET_IEEEQUAD_DEFAULT && TARGET_LONG_DOUBLE_128)
4261 static bool warned_change_long_double;
4262 if (!warned_change_long_double)
4264 warned_change_long_double = true;
4265 if (TARGET_IEEEQUAD)
4266 warning (OPT_Wpsabi, "Using IEEE extended precision "
4267 "%<long double%>");
4268 else
4269 warning (OPT_Wpsabi, "Using IBM extended precision "
4270 "%<long double%>");
4275 /* Enable the default support for IEEE 128-bit floating point on Linux VSX
4276 sytems. In GCC 7, we would enable the the IEEE 128-bit floating point
4277 infrastructure (-mfloat128-type) but not enable the actual __float128 type
4278 unless the user used the explicit -mfloat128. In GCC 8, we enable both
4279 the keyword as well as the type. */
4280 TARGET_FLOAT128_TYPE = TARGET_FLOAT128_ENABLE_TYPE && TARGET_VSX;
4282 /* IEEE 128-bit floating point requires VSX support. */
4283 if (TARGET_FLOAT128_KEYWORD)
4285 if (!TARGET_VSX)
4287 if ((rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_KEYWORD) != 0)
4288 error ("%qs requires VSX support", "%<-mfloat128%>");
4290 TARGET_FLOAT128_TYPE = 0;
4291 rs6000_isa_flags &= ~(OPTION_MASK_FLOAT128_KEYWORD
4292 | OPTION_MASK_FLOAT128_HW);
4294 else if (!TARGET_FLOAT128_TYPE)
4296 TARGET_FLOAT128_TYPE = 1;
4297 warning (0, "The %<-mfloat128%> option may not be fully supported");
4301 /* Enable the __float128 keyword under Linux by default. */
4302 if (TARGET_FLOAT128_TYPE && !TARGET_FLOAT128_KEYWORD
4303 && (rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_KEYWORD) == 0)
4304 rs6000_isa_flags |= OPTION_MASK_FLOAT128_KEYWORD;
4306 /* If we have are supporting the float128 type and full ISA 3.0 support,
4307 enable -mfloat128-hardware by default. However, don't enable the
4308 __float128 keyword if it was explicitly turned off. 64-bit mode is needed
4309 because sometimes the compiler wants to put things in an integer
4310 container, and if we don't have __int128 support, it is impossible. */
4311 if (TARGET_FLOAT128_TYPE && !TARGET_FLOAT128_HW && TARGET_64BIT
4312 && (rs6000_isa_flags & ISA_3_0_MASKS_IEEE) == ISA_3_0_MASKS_IEEE
4313 && !(rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_HW))
4314 rs6000_isa_flags |= OPTION_MASK_FLOAT128_HW;
4316 if (TARGET_FLOAT128_HW
4317 && (rs6000_isa_flags & ISA_3_0_MASKS_IEEE) != ISA_3_0_MASKS_IEEE)
4319 if ((rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_HW) != 0)
4320 error ("%qs requires full ISA 3.0 support", "%<-mfloat128-hardware%>");
4322 rs6000_isa_flags &= ~OPTION_MASK_FLOAT128_HW;
4325 if (TARGET_FLOAT128_HW && !TARGET_64BIT)
4327 if ((rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_HW) != 0)
4328 error ("%qs requires %qs", "%<-mfloat128-hardware%>", "-m64");
4330 rs6000_isa_flags &= ~OPTION_MASK_FLOAT128_HW;
4333 /* Print the options after updating the defaults. */
4334 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
4335 rs6000_print_isa_options (stderr, 0, "after defaults", rs6000_isa_flags);
4337 /* E500mc does "better" if we inline more aggressively. Respect the
4338 user's opinion, though. */
4339 if (rs6000_block_move_inline_limit == 0
4340 && (rs6000_tune == PROCESSOR_PPCE500MC
4341 || rs6000_tune == PROCESSOR_PPCE500MC64
4342 || rs6000_tune == PROCESSOR_PPCE5500
4343 || rs6000_tune == PROCESSOR_PPCE6500))
4344 rs6000_block_move_inline_limit = 128;
4346 /* store_one_arg depends on expand_block_move to handle at least the
4347 size of reg_parm_stack_space. */
4348 if (rs6000_block_move_inline_limit < (TARGET_POWERPC64 ? 64 : 32))
4349 rs6000_block_move_inline_limit = (TARGET_POWERPC64 ? 64 : 32);
4351 if (global_init_p)
4353 /* If the appropriate debug option is enabled, replace the target hooks
4354 with debug versions that call the real version and then prints
4355 debugging information. */
4356 if (TARGET_DEBUG_COST)
4358 targetm.rtx_costs = rs6000_debug_rtx_costs;
4359 targetm.address_cost = rs6000_debug_address_cost;
4360 targetm.sched.adjust_cost = rs6000_debug_adjust_cost;
4363 if (TARGET_DEBUG_ADDR)
4365 targetm.legitimate_address_p = rs6000_debug_legitimate_address_p;
4366 targetm.legitimize_address = rs6000_debug_legitimize_address;
4367 rs6000_secondary_reload_class_ptr
4368 = rs6000_debug_secondary_reload_class;
4369 targetm.secondary_memory_needed
4370 = rs6000_debug_secondary_memory_needed;
4371 targetm.can_change_mode_class
4372 = rs6000_debug_can_change_mode_class;
4373 rs6000_preferred_reload_class_ptr
4374 = rs6000_debug_preferred_reload_class;
4375 rs6000_mode_dependent_address_ptr
4376 = rs6000_debug_mode_dependent_address;
4379 if (rs6000_veclibabi_name)
4381 if (strcmp (rs6000_veclibabi_name, "mass") == 0)
4382 rs6000_veclib_handler = rs6000_builtin_vectorized_libmass;
4383 else
4385 error ("unknown vectorization library ABI type (%qs) for "
4386 "%qs switch", rs6000_veclibabi_name, "-mveclibabi=");
4387 ret = false;
4392 /* Disable VSX and Altivec silently if the user switched cpus to power7 in a
4393 target attribute or pragma which automatically enables both options,
4394 unless the altivec ABI was set. This is set by default for 64-bit, but
4395 not for 32-bit. */
4396 if (main_target_opt != NULL && !main_target_opt->x_rs6000_altivec_abi)
4398 TARGET_FLOAT128_TYPE = 0;
4399 rs6000_isa_flags &= ~((OPTION_MASK_VSX | OPTION_MASK_ALTIVEC
4400 | OPTION_MASK_FLOAT128_KEYWORD)
4401 & ~rs6000_isa_flags_explicit);
4404 /* Enable Altivec ABI for AIX -maltivec. */
4405 if (TARGET_XCOFF && (TARGET_ALTIVEC || TARGET_VSX))
4407 if (main_target_opt != NULL && !main_target_opt->x_rs6000_altivec_abi)
4408 error ("target attribute or pragma changes AltiVec ABI");
4409 else
4410 rs6000_altivec_abi = 1;
4413 /* The AltiVec ABI is the default for PowerPC-64 GNU/Linux. For
4414 PowerPC-32 GNU/Linux, -maltivec implies the AltiVec ABI. It can
4415 be explicitly overridden in either case. */
4416 if (TARGET_ELF)
4418 if (!global_options_set.x_rs6000_altivec_abi
4419 && (TARGET_64BIT || TARGET_ALTIVEC || TARGET_VSX))
4421 if (main_target_opt != NULL &&
4422 !main_target_opt->x_rs6000_altivec_abi)
4423 error ("target attribute or pragma changes AltiVec ABI");
4424 else
4425 rs6000_altivec_abi = 1;
4429 /* Set the Darwin64 ABI as default for 64-bit Darwin.
4430 So far, the only darwin64 targets are also MACH-O. */
4431 if (TARGET_MACHO
4432 && DEFAULT_ABI == ABI_DARWIN
4433 && TARGET_64BIT)
4435 if (main_target_opt != NULL && !main_target_opt->x_rs6000_darwin64_abi)
4436 error ("target attribute or pragma changes darwin64 ABI");
4437 else
4439 rs6000_darwin64_abi = 1;
4440 /* Default to natural alignment, for better performance. */
4441 rs6000_alignment_flags = MASK_ALIGN_NATURAL;
4445 /* Place FP constants in the constant pool instead of TOC
4446 if section anchors enabled. */
4447 if (flag_section_anchors
4448 && !global_options_set.x_TARGET_NO_FP_IN_TOC)
4449 TARGET_NO_FP_IN_TOC = 1;
4451 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
4452 rs6000_print_isa_options (stderr, 0, "before subtarget", rs6000_isa_flags);
4454 #ifdef SUBTARGET_OVERRIDE_OPTIONS
4455 SUBTARGET_OVERRIDE_OPTIONS;
4456 #endif
4457 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
4458 SUBSUBTARGET_OVERRIDE_OPTIONS;
4459 #endif
4460 #ifdef SUB3TARGET_OVERRIDE_OPTIONS
4461 SUB3TARGET_OVERRIDE_OPTIONS;
4462 #endif
4464 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
4465 rs6000_print_isa_options (stderr, 0, "after subtarget", rs6000_isa_flags);
4467 rs6000_always_hint = (rs6000_tune != PROCESSOR_POWER4
4468 && rs6000_tune != PROCESSOR_POWER5
4469 && rs6000_tune != PROCESSOR_POWER6
4470 && rs6000_tune != PROCESSOR_POWER7
4471 && rs6000_tune != PROCESSOR_POWER8
4472 && rs6000_tune != PROCESSOR_POWER9
4473 && rs6000_tune != PROCESSOR_PPCA2
4474 && rs6000_tune != PROCESSOR_CELL
4475 && rs6000_tune != PROCESSOR_PPC476);
4476 rs6000_sched_groups = (rs6000_tune == PROCESSOR_POWER4
4477 || rs6000_tune == PROCESSOR_POWER5
4478 || rs6000_tune == PROCESSOR_POWER7
4479 || rs6000_tune == PROCESSOR_POWER8);
4480 rs6000_align_branch_targets = (rs6000_tune == PROCESSOR_POWER4
4481 || rs6000_tune == PROCESSOR_POWER5
4482 || rs6000_tune == PROCESSOR_POWER6
4483 || rs6000_tune == PROCESSOR_POWER7
4484 || rs6000_tune == PROCESSOR_POWER8
4485 || rs6000_tune == PROCESSOR_POWER9
4486 || rs6000_tune == PROCESSOR_PPCE500MC
4487 || rs6000_tune == PROCESSOR_PPCE500MC64
4488 || rs6000_tune == PROCESSOR_PPCE5500
4489 || rs6000_tune == PROCESSOR_PPCE6500);
4491 /* Allow debug switches to override the above settings. These are set to -1
4492 in rs6000.opt to indicate the user hasn't directly set the switch. */
4493 if (TARGET_ALWAYS_HINT >= 0)
4494 rs6000_always_hint = TARGET_ALWAYS_HINT;
4496 if (TARGET_SCHED_GROUPS >= 0)
4497 rs6000_sched_groups = TARGET_SCHED_GROUPS;
4499 if (TARGET_ALIGN_BRANCH_TARGETS >= 0)
4500 rs6000_align_branch_targets = TARGET_ALIGN_BRANCH_TARGETS;
4502 rs6000_sched_restricted_insns_priority
4503 = (rs6000_sched_groups ? 1 : 0);
4505 /* Handle -msched-costly-dep option. */
4506 rs6000_sched_costly_dep
4507 = (rs6000_sched_groups ? true_store_to_load_dep_costly : no_dep_costly);
4509 if (rs6000_sched_costly_dep_str)
4511 if (! strcmp (rs6000_sched_costly_dep_str, "no"))
4512 rs6000_sched_costly_dep = no_dep_costly;
4513 else if (! strcmp (rs6000_sched_costly_dep_str, "all"))
4514 rs6000_sched_costly_dep = all_deps_costly;
4515 else if (! strcmp (rs6000_sched_costly_dep_str, "true_store_to_load"))
4516 rs6000_sched_costly_dep = true_store_to_load_dep_costly;
4517 else if (! strcmp (rs6000_sched_costly_dep_str, "store_to_load"))
4518 rs6000_sched_costly_dep = store_to_load_dep_costly;
4519 else
4520 rs6000_sched_costly_dep = ((enum rs6000_dependence_cost)
4521 atoi (rs6000_sched_costly_dep_str));
4524 /* Handle -minsert-sched-nops option. */
4525 rs6000_sched_insert_nops
4526 = (rs6000_sched_groups ? sched_finish_regroup_exact : sched_finish_none);
4528 if (rs6000_sched_insert_nops_str)
4530 if (! strcmp (rs6000_sched_insert_nops_str, "no"))
4531 rs6000_sched_insert_nops = sched_finish_none;
4532 else if (! strcmp (rs6000_sched_insert_nops_str, "pad"))
4533 rs6000_sched_insert_nops = sched_finish_pad_groups;
4534 else if (! strcmp (rs6000_sched_insert_nops_str, "regroup_exact"))
4535 rs6000_sched_insert_nops = sched_finish_regroup_exact;
4536 else
4537 rs6000_sched_insert_nops = ((enum rs6000_nop_insertion)
4538 atoi (rs6000_sched_insert_nops_str));
4541 /* Handle stack protector */
4542 if (!global_options_set.x_rs6000_stack_protector_guard)
4543 #ifdef TARGET_THREAD_SSP_OFFSET
4544 rs6000_stack_protector_guard = SSP_TLS;
4545 #else
4546 rs6000_stack_protector_guard = SSP_GLOBAL;
4547 #endif
4549 #ifdef TARGET_THREAD_SSP_OFFSET
4550 rs6000_stack_protector_guard_offset = TARGET_THREAD_SSP_OFFSET;
4551 rs6000_stack_protector_guard_reg = TARGET_64BIT ? 13 : 2;
4552 #endif
4554 if (global_options_set.x_rs6000_stack_protector_guard_offset_str)
4556 char *endp;
4557 const char *str = rs6000_stack_protector_guard_offset_str;
4559 errno = 0;
4560 long offset = strtol (str, &endp, 0);
4561 if (!*str || *endp || errno)
4562 error ("%qs is not a valid number in %qs", str,
4563 "-mstack-protector-guard-offset=");
4565 if (!IN_RANGE (offset, -0x8000, 0x7fff)
4566 || (TARGET_64BIT && (offset & 3)))
4567 error ("%qs is not a valid offset in %qs", str,
4568 "-mstack-protector-guard-offset=");
4570 rs6000_stack_protector_guard_offset = offset;
4573 if (global_options_set.x_rs6000_stack_protector_guard_reg_str)
4575 const char *str = rs6000_stack_protector_guard_reg_str;
4576 int reg = decode_reg_name (str);
4578 if (!IN_RANGE (reg, 1, 31))
4579 error ("%qs is not a valid base register in %qs", str,
4580 "-mstack-protector-guard-reg=");
4582 rs6000_stack_protector_guard_reg = reg;
4585 if (rs6000_stack_protector_guard == SSP_TLS
4586 && !IN_RANGE (rs6000_stack_protector_guard_reg, 1, 31))
4587 error ("%qs needs a valid base register", "-mstack-protector-guard=tls");
4589 if (global_init_p)
4591 #ifdef TARGET_REGNAMES
4592 /* If the user desires alternate register names, copy in the
4593 alternate names now. */
4594 if (TARGET_REGNAMES)
4595 memcpy (rs6000_reg_names, alt_reg_names, sizeof (rs6000_reg_names));
4596 #endif
4598 /* Set aix_struct_return last, after the ABI is determined.
4599 If -maix-struct-return or -msvr4-struct-return was explicitly
4600 used, don't override with the ABI default. */
4601 if (!global_options_set.x_aix_struct_return)
4602 aix_struct_return = (DEFAULT_ABI != ABI_V4 || DRAFT_V4_STRUCT_RET);
4604 #if 0
4605 /* IBM XL compiler defaults to unsigned bitfields. */
4606 if (TARGET_XL_COMPAT)
4607 flag_signed_bitfields = 0;
4608 #endif
4610 if (TARGET_LONG_DOUBLE_128 && !TARGET_IEEEQUAD)
4611 REAL_MODE_FORMAT (TFmode) = &ibm_extended_format;
4613 ASM_GENERATE_INTERNAL_LABEL (toc_label_name, "LCTOC", 1);
4615 /* We can only guarantee the availability of DI pseudo-ops when
4616 assembling for 64-bit targets. */
4617 if (!TARGET_64BIT)
4619 targetm.asm_out.aligned_op.di = NULL;
4620 targetm.asm_out.unaligned_op.di = NULL;
4624 /* Set branch target alignment, if not optimizing for size. */
4625 if (!optimize_size)
4627 /* Cell wants to be aligned 8byte for dual issue. Titan wants to be
4628 aligned 8byte to avoid misprediction by the branch predictor. */
4629 if (rs6000_tune == PROCESSOR_TITAN
4630 || rs6000_tune == PROCESSOR_CELL)
4632 if (flag_align_functions && !str_align_functions)
4633 str_align_functions = "8";
4634 if (flag_align_jumps && !str_align_jumps)
4635 str_align_jumps = "8";
4636 if (flag_align_loops && !str_align_loops)
4637 str_align_loops = "8";
4639 if (rs6000_align_branch_targets)
4641 if (flag_align_functions && !str_align_functions)
4642 str_align_functions = "16";
4643 if (flag_align_jumps && !str_align_jumps)
4644 str_align_jumps = "16";
4645 if (flag_align_loops && !str_align_loops)
4647 can_override_loop_align = 1;
4648 str_align_loops = "16";
4652 if (flag_align_jumps && !str_align_jumps)
4653 str_align_jumps = "16";
4654 if (flag_align_loops && !str_align_loops)
4655 str_align_loops = "16";
4658 /* Arrange to save and restore machine status around nested functions. */
4659 init_machine_status = rs6000_init_machine_status;
4661 /* We should always be splitting complex arguments, but we can't break
4662 Linux and Darwin ABIs at the moment. For now, only AIX is fixed. */
4663 if (DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN)
4664 targetm.calls.split_complex_arg = NULL;
4666 /* The AIX and ELFv1 ABIs define standard function descriptors. */
4667 if (DEFAULT_ABI == ABI_AIX)
4668 targetm.calls.custom_function_descriptors = 0;
4671 /* Initialize rs6000_cost with the appropriate target costs. */
4672 if (optimize_size)
4673 rs6000_cost = TARGET_POWERPC64 ? &size64_cost : &size32_cost;
4674 else
4675 switch (rs6000_tune)
4677 case PROCESSOR_RS64A:
4678 rs6000_cost = &rs64a_cost;
4679 break;
4681 case PROCESSOR_MPCCORE:
4682 rs6000_cost = &mpccore_cost;
4683 break;
4685 case PROCESSOR_PPC403:
4686 rs6000_cost = &ppc403_cost;
4687 break;
4689 case PROCESSOR_PPC405:
4690 rs6000_cost = &ppc405_cost;
4691 break;
4693 case PROCESSOR_PPC440:
4694 rs6000_cost = &ppc440_cost;
4695 break;
4697 case PROCESSOR_PPC476:
4698 rs6000_cost = &ppc476_cost;
4699 break;
4701 case PROCESSOR_PPC601:
4702 rs6000_cost = &ppc601_cost;
4703 break;
4705 case PROCESSOR_PPC603:
4706 rs6000_cost = &ppc603_cost;
4707 break;
4709 case PROCESSOR_PPC604:
4710 rs6000_cost = &ppc604_cost;
4711 break;
4713 case PROCESSOR_PPC604e:
4714 rs6000_cost = &ppc604e_cost;
4715 break;
4717 case PROCESSOR_PPC620:
4718 rs6000_cost = &ppc620_cost;
4719 break;
4721 case PROCESSOR_PPC630:
4722 rs6000_cost = &ppc630_cost;
4723 break;
4725 case PROCESSOR_CELL:
4726 rs6000_cost = &ppccell_cost;
4727 break;
4729 case PROCESSOR_PPC750:
4730 case PROCESSOR_PPC7400:
4731 rs6000_cost = &ppc750_cost;
4732 break;
4734 case PROCESSOR_PPC7450:
4735 rs6000_cost = &ppc7450_cost;
4736 break;
4738 case PROCESSOR_PPC8540:
4739 case PROCESSOR_PPC8548:
4740 rs6000_cost = &ppc8540_cost;
4741 break;
4743 case PROCESSOR_PPCE300C2:
4744 case PROCESSOR_PPCE300C3:
4745 rs6000_cost = &ppce300c2c3_cost;
4746 break;
4748 case PROCESSOR_PPCE500MC:
4749 rs6000_cost = &ppce500mc_cost;
4750 break;
4752 case PROCESSOR_PPCE500MC64:
4753 rs6000_cost = &ppce500mc64_cost;
4754 break;
4756 case PROCESSOR_PPCE5500:
4757 rs6000_cost = &ppce5500_cost;
4758 break;
4760 case PROCESSOR_PPCE6500:
4761 rs6000_cost = &ppce6500_cost;
4762 break;
4764 case PROCESSOR_TITAN:
4765 rs6000_cost = &titan_cost;
4766 break;
4768 case PROCESSOR_POWER4:
4769 case PROCESSOR_POWER5:
4770 rs6000_cost = &power4_cost;
4771 break;
4773 case PROCESSOR_POWER6:
4774 rs6000_cost = &power6_cost;
4775 break;
4777 case PROCESSOR_POWER7:
4778 rs6000_cost = &power7_cost;
4779 break;
4781 case PROCESSOR_POWER8:
4782 rs6000_cost = &power8_cost;
4783 break;
4785 case PROCESSOR_POWER9:
4786 rs6000_cost = &power9_cost;
4787 break;
4789 case PROCESSOR_PPCA2:
4790 rs6000_cost = &ppca2_cost;
4791 break;
4793 default:
4794 gcc_unreachable ();
4797 if (global_init_p)
4799 maybe_set_param_value (PARAM_SIMULTANEOUS_PREFETCHES,
4800 rs6000_cost->simultaneous_prefetches,
4801 global_options.x_param_values,
4802 global_options_set.x_param_values);
4803 maybe_set_param_value (PARAM_L1_CACHE_SIZE, rs6000_cost->l1_cache_size,
4804 global_options.x_param_values,
4805 global_options_set.x_param_values);
4806 maybe_set_param_value (PARAM_L1_CACHE_LINE_SIZE,
4807 rs6000_cost->cache_line_size,
4808 global_options.x_param_values,
4809 global_options_set.x_param_values);
4810 maybe_set_param_value (PARAM_L2_CACHE_SIZE, rs6000_cost->l2_cache_size,
4811 global_options.x_param_values,
4812 global_options_set.x_param_values);
4814 /* Increase loop peeling limits based on performance analysis. */
4815 maybe_set_param_value (PARAM_MAX_PEELED_INSNS, 400,
4816 global_options.x_param_values,
4817 global_options_set.x_param_values);
4818 maybe_set_param_value (PARAM_MAX_COMPLETELY_PEELED_INSNS, 400,
4819 global_options.x_param_values,
4820 global_options_set.x_param_values);
4822 /* Use the 'model' -fsched-pressure algorithm by default. */
4823 maybe_set_param_value (PARAM_SCHED_PRESSURE_ALGORITHM,
4824 SCHED_PRESSURE_MODEL,
4825 global_options.x_param_values,
4826 global_options_set.x_param_values);
4828 /* If using typedef char *va_list, signal that
4829 __builtin_va_start (&ap, 0) can be optimized to
4830 ap = __builtin_next_arg (0). */
4831 if (DEFAULT_ABI != ABI_V4)
4832 targetm.expand_builtin_va_start = NULL;
4835 /* If not explicitly specified via option, decide whether to generate indexed
4836 load/store instructions. A value of -1 indicates that the
4837 initial value of this variable has not been overwritten. During
4838 compilation, TARGET_AVOID_XFORM is either 0 or 1. */
4839 if (TARGET_AVOID_XFORM == -1)
4840 /* Avoid indexed addressing when targeting Power6 in order to avoid the
4841 DERAT mispredict penalty. However the LVE and STVE altivec instructions
4842 need indexed accesses and the type used is the scalar type of the element
4843 being loaded or stored. */
4844 TARGET_AVOID_XFORM = (rs6000_tune == PROCESSOR_POWER6 && TARGET_CMPB
4845 && !TARGET_ALTIVEC);
4847 /* Set the -mrecip options. */
4848 if (rs6000_recip_name)
4850 char *p = ASTRDUP (rs6000_recip_name);
4851 char *q;
4852 unsigned int mask, i;
4853 bool invert;
4855 while ((q = strtok (p, ",")) != NULL)
4857 p = NULL;
4858 if (*q == '!')
4860 invert = true;
4861 q++;
4863 else
4864 invert = false;
4866 if (!strcmp (q, "default"))
4867 mask = ((TARGET_RECIP_PRECISION)
4868 ? RECIP_HIGH_PRECISION : RECIP_LOW_PRECISION);
4869 else
4871 for (i = 0; i < ARRAY_SIZE (recip_options); i++)
4872 if (!strcmp (q, recip_options[i].string))
4874 mask = recip_options[i].mask;
4875 break;
4878 if (i == ARRAY_SIZE (recip_options))
4880 error ("unknown option for %<%s=%s%>", "-mrecip", q);
4881 invert = false;
4882 mask = 0;
4883 ret = false;
4887 if (invert)
4888 rs6000_recip_control &= ~mask;
4889 else
4890 rs6000_recip_control |= mask;
4894 /* Set the builtin mask of the various options used that could affect which
4895 builtins were used. In the past we used target_flags, but we've run out
4896 of bits, and some options are no longer in target_flags. */
4897 rs6000_builtin_mask = rs6000_builtin_mask_calculate ();
4898 if (TARGET_DEBUG_BUILTIN || TARGET_DEBUG_TARGET)
4899 rs6000_print_builtin_options (stderr, 0, "builtin mask",
4900 rs6000_builtin_mask);
4902 /* Initialize all of the registers. */
4903 rs6000_init_hard_regno_mode_ok (global_init_p);
4905 /* Save the initial options in case the user does function specific options */
4906 if (global_init_p)
4907 target_option_default_node = target_option_current_node
4908 = build_target_option_node (&global_options);
4910 /* If not explicitly specified via option, decide whether to generate the
4911 extra blr's required to preserve the link stack on some cpus (eg, 476). */
4912 if (TARGET_LINK_STACK == -1)
4913 SET_TARGET_LINK_STACK (rs6000_tune == PROCESSOR_PPC476 && flag_pic);
4915 /* Deprecate use of -mno-speculate-indirect-jumps. */
4916 if (!rs6000_speculate_indirect_jumps)
4917 warning (0, "%qs is deprecated and not recommended in any circumstances",
4918 "-mno-speculate-indirect-jumps");
4920 return ret;
4923 /* Implement TARGET_OPTION_OVERRIDE. On the RS/6000 this is used to
4924 define the target cpu type. */
4926 static void
4927 rs6000_option_override (void)
4929 (void) rs6000_option_override_internal (true);
4933 /* Implement targetm.vectorize.builtin_mask_for_load. */
4934 static tree
4935 rs6000_builtin_mask_for_load (void)
4937 /* Don't use lvsl/vperm for P8 and similarly efficient machines. */
4938 if ((TARGET_ALTIVEC && !TARGET_VSX)
4939 || (TARGET_VSX && !TARGET_EFFICIENT_UNALIGNED_VSX))
4940 return altivec_builtin_mask_for_load;
4941 else
4942 return 0;
4945 /* Implement LOOP_ALIGN. */
4946 align_flags
4947 rs6000_loop_align (rtx label)
4949 basic_block bb;
4950 int ninsns;
4952 /* Don't override loop alignment if -falign-loops was specified. */
4953 if (!can_override_loop_align)
4954 return align_loops;
4956 bb = BLOCK_FOR_INSN (label);
4957 ninsns = num_loop_insns(bb->loop_father);
4959 /* Align small loops to 32 bytes to fit in an icache sector, otherwise return default. */
4960 if (ninsns > 4 && ninsns <= 8
4961 && (rs6000_tune == PROCESSOR_POWER4
4962 || rs6000_tune == PROCESSOR_POWER5
4963 || rs6000_tune == PROCESSOR_POWER6
4964 || rs6000_tune == PROCESSOR_POWER7
4965 || rs6000_tune == PROCESSOR_POWER8))
4966 return align_flags (5);
4967 else
4968 return align_loops;
4971 /* Return true iff, data reference of TYPE can reach vector alignment (16)
4972 after applying N number of iterations. This routine does not determine
4973 how may iterations are required to reach desired alignment. */
4975 static bool
4976 rs6000_vector_alignment_reachable (const_tree type ATTRIBUTE_UNUSED, bool is_packed)
4978 if (is_packed)
4979 return false;
4981 if (TARGET_32BIT)
4983 if (rs6000_alignment_flags == MASK_ALIGN_NATURAL)
4984 return true;
4986 if (rs6000_alignment_flags == MASK_ALIGN_POWER)
4987 return true;
4989 return false;
4991 else
4993 if (TARGET_MACHO)
4994 return false;
4996 /* Assuming that all other types are naturally aligned. CHECKME! */
4997 return true;
5001 /* Return true if the vector misalignment factor is supported by the
5002 target. */
5003 static bool
5004 rs6000_builtin_support_vector_misalignment (machine_mode mode,
5005 const_tree type,
5006 int misalignment,
5007 bool is_packed)
5009 if (TARGET_VSX)
5011 if (TARGET_EFFICIENT_UNALIGNED_VSX)
5012 return true;
5014 /* Return if movmisalign pattern is not supported for this mode. */
5015 if (optab_handler (movmisalign_optab, mode) == CODE_FOR_nothing)
5016 return false;
5018 if (misalignment == -1)
5020 /* Misalignment factor is unknown at compile time but we know
5021 it's word aligned. */
5022 if (rs6000_vector_alignment_reachable (type, is_packed))
5024 int element_size = TREE_INT_CST_LOW (TYPE_SIZE (type));
5026 if (element_size == 64 || element_size == 32)
5027 return true;
5030 return false;
5033 /* VSX supports word-aligned vector. */
5034 if (misalignment % 4 == 0)
5035 return true;
5037 return false;
5040 /* Implement targetm.vectorize.builtin_vectorization_cost. */
5041 static int
5042 rs6000_builtin_vectorization_cost (enum vect_cost_for_stmt type_of_cost,
5043 tree vectype, int misalign)
5045 unsigned elements;
5046 tree elem_type;
5048 switch (type_of_cost)
5050 case scalar_stmt:
5051 case scalar_load:
5052 case scalar_store:
5053 case vector_stmt:
5054 case vector_load:
5055 case vector_store:
5056 case vec_to_scalar:
5057 case scalar_to_vec:
5058 case cond_branch_not_taken:
5059 return 1;
5061 case vec_perm:
5062 if (TARGET_VSX)
5063 return 3;
5064 else
5065 return 1;
5067 case vec_promote_demote:
5068 if (TARGET_VSX)
5069 return 4;
5070 else
5071 return 1;
5073 case cond_branch_taken:
5074 return 3;
5076 case unaligned_load:
5077 case vector_gather_load:
5078 if (TARGET_EFFICIENT_UNALIGNED_VSX)
5079 return 1;
5081 if (TARGET_VSX && TARGET_ALLOW_MOVMISALIGN)
5083 elements = TYPE_VECTOR_SUBPARTS (vectype);
5084 if (elements == 2)
5085 /* Double word aligned. */
5086 return 2;
5088 if (elements == 4)
5090 switch (misalign)
5092 case 8:
5093 /* Double word aligned. */
5094 return 2;
5096 case -1:
5097 /* Unknown misalignment. */
5098 case 4:
5099 case 12:
5100 /* Word aligned. */
5101 return 22;
5103 default:
5104 gcc_unreachable ();
5109 if (TARGET_ALTIVEC)
5110 /* Misaligned loads are not supported. */
5111 gcc_unreachable ();
5113 return 2;
5115 case unaligned_store:
5116 case vector_scatter_store:
5117 if (TARGET_EFFICIENT_UNALIGNED_VSX)
5118 return 1;
5120 if (TARGET_VSX && TARGET_ALLOW_MOVMISALIGN)
5122 elements = TYPE_VECTOR_SUBPARTS (vectype);
5123 if (elements == 2)
5124 /* Double word aligned. */
5125 return 2;
5127 if (elements == 4)
5129 switch (misalign)
5131 case 8:
5132 /* Double word aligned. */
5133 return 2;
5135 case -1:
5136 /* Unknown misalignment. */
5137 case 4:
5138 case 12:
5139 /* Word aligned. */
5140 return 23;
5142 default:
5143 gcc_unreachable ();
5148 if (TARGET_ALTIVEC)
5149 /* Misaligned stores are not supported. */
5150 gcc_unreachable ();
5152 return 2;
5154 case vec_construct:
5155 /* This is a rough approximation assuming non-constant elements
5156 constructed into a vector via element insertion. FIXME:
5157 vec_construct is not granular enough for uniformly good
5158 decisions. If the initialization is a splat, this is
5159 cheaper than we estimate. Improve this someday. */
5160 elem_type = TREE_TYPE (vectype);
5161 /* 32-bit vectors loaded into registers are stored as double
5162 precision, so we need 2 permutes, 2 converts, and 1 merge
5163 to construct a vector of short floats from them. */
5164 if (SCALAR_FLOAT_TYPE_P (elem_type)
5165 && TYPE_PRECISION (elem_type) == 32)
5166 return 5;
5167 /* On POWER9, integer vector types are built up in GPRs and then
5168 use a direct move (2 cycles). For POWER8 this is even worse,
5169 as we need two direct moves and a merge, and the direct moves
5170 are five cycles. */
5171 else if (INTEGRAL_TYPE_P (elem_type))
5173 if (TARGET_P9_VECTOR)
5174 return TYPE_VECTOR_SUBPARTS (vectype) - 1 + 2;
5175 else
5176 return TYPE_VECTOR_SUBPARTS (vectype) - 1 + 5;
5178 else
5179 /* V2DFmode doesn't need a direct move. */
5180 return 2;
5182 default:
5183 gcc_unreachable ();
5187 /* Implement targetm.vectorize.preferred_simd_mode. */
5189 static machine_mode
5190 rs6000_preferred_simd_mode (scalar_mode mode)
5192 if (TARGET_VSX)
5193 switch (mode)
5195 case E_DFmode:
5196 return V2DFmode;
5197 default:;
5199 if (TARGET_ALTIVEC || TARGET_VSX)
5200 switch (mode)
5202 case E_SFmode:
5203 return V4SFmode;
5204 case E_TImode:
5205 return V1TImode;
5206 case E_DImode:
5207 return V2DImode;
5208 case E_SImode:
5209 return V4SImode;
5210 case E_HImode:
5211 return V8HImode;
5212 case E_QImode:
5213 return V16QImode;
5214 default:;
5216 return word_mode;
5219 typedef struct _rs6000_cost_data
5221 struct loop *loop_info;
5222 unsigned cost[3];
5223 } rs6000_cost_data;
5225 /* Test for likely overcommitment of vector hardware resources. If a
5226 loop iteration is relatively large, and too large a percentage of
5227 instructions in the loop are vectorized, the cost model may not
5228 adequately reflect delays from unavailable vector resources.
5229 Penalize the loop body cost for this case. */
5231 static void
5232 rs6000_density_test (rs6000_cost_data *data)
5234 const int DENSITY_PCT_THRESHOLD = 85;
5235 const int DENSITY_SIZE_THRESHOLD = 70;
5236 const int DENSITY_PENALTY = 10;
5237 struct loop *loop = data->loop_info;
5238 basic_block *bbs = get_loop_body (loop);
5239 int nbbs = loop->num_nodes;
5240 loop_vec_info loop_vinfo = loop_vec_info_for_loop (data->loop_info);
5241 int vec_cost = data->cost[vect_body], not_vec_cost = 0;
5242 int i, density_pct;
5244 for (i = 0; i < nbbs; i++)
5246 basic_block bb = bbs[i];
5247 gimple_stmt_iterator gsi;
5249 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5251 gimple *stmt = gsi_stmt (gsi);
5252 stmt_vec_info stmt_info = loop_vinfo->lookup_stmt (stmt);
5254 if (!STMT_VINFO_RELEVANT_P (stmt_info)
5255 && !STMT_VINFO_IN_PATTERN_P (stmt_info))
5256 not_vec_cost++;
5260 free (bbs);
5261 density_pct = (vec_cost * 100) / (vec_cost + not_vec_cost);
5263 if (density_pct > DENSITY_PCT_THRESHOLD
5264 && vec_cost + not_vec_cost > DENSITY_SIZE_THRESHOLD)
5266 data->cost[vect_body] = vec_cost * (100 + DENSITY_PENALTY) / 100;
5267 if (dump_enabled_p ())
5268 dump_printf_loc (MSG_NOTE, vect_location,
5269 "density %d%%, cost %d exceeds threshold, penalizing "
5270 "loop body cost by %d%%", density_pct,
5271 vec_cost + not_vec_cost, DENSITY_PENALTY);
5275 /* Implement targetm.vectorize.init_cost. */
5277 /* For each vectorized loop, this var holds TRUE iff a non-memory vector
5278 instruction is needed by the vectorization. */
5279 static bool rs6000_vect_nonmem;
5281 static void *
5282 rs6000_init_cost (struct loop *loop_info)
5284 rs6000_cost_data *data = XNEW (struct _rs6000_cost_data);
5285 data->loop_info = loop_info;
5286 data->cost[vect_prologue] = 0;
5287 data->cost[vect_body] = 0;
5288 data->cost[vect_epilogue] = 0;
5289 rs6000_vect_nonmem = false;
5290 return data;
5293 /* Implement targetm.vectorize.add_stmt_cost. */
5295 static unsigned
5296 rs6000_add_stmt_cost (void *data, int count, enum vect_cost_for_stmt kind,
5297 struct _stmt_vec_info *stmt_info, int misalign,
5298 enum vect_cost_model_location where)
5300 rs6000_cost_data *cost_data = (rs6000_cost_data*) data;
5301 unsigned retval = 0;
5303 if (flag_vect_cost_model)
5305 tree vectype = stmt_info ? stmt_vectype (stmt_info) : NULL_TREE;
5306 int stmt_cost = rs6000_builtin_vectorization_cost (kind, vectype,
5307 misalign);
5308 /* Statements in an inner loop relative to the loop being
5309 vectorized are weighted more heavily. The value here is
5310 arbitrary and could potentially be improved with analysis. */
5311 if (where == vect_body && stmt_info && stmt_in_inner_loop_p (stmt_info))
5312 count *= 50; /* FIXME. */
5314 retval = (unsigned) (count * stmt_cost);
5315 cost_data->cost[where] += retval;
5317 /* Check whether we're doing something other than just a copy loop.
5318 Not all such loops may be profitably vectorized; see
5319 rs6000_finish_cost. */
5320 if ((kind == vec_to_scalar || kind == vec_perm
5321 || kind == vec_promote_demote || kind == vec_construct
5322 || kind == scalar_to_vec)
5323 || (where == vect_body && kind == vector_stmt))
5324 rs6000_vect_nonmem = true;
5327 return retval;
5330 /* Implement targetm.vectorize.finish_cost. */
5332 static void
5333 rs6000_finish_cost (void *data, unsigned *prologue_cost,
5334 unsigned *body_cost, unsigned *epilogue_cost)
5336 rs6000_cost_data *cost_data = (rs6000_cost_data*) data;
5338 if (cost_data->loop_info)
5339 rs6000_density_test (cost_data);
5341 /* Don't vectorize minimum-vectorization-factor, simple copy loops
5342 that require versioning for any reason. The vectorization is at
5343 best a wash inside the loop, and the versioning checks make
5344 profitability highly unlikely and potentially quite harmful. */
5345 if (cost_data->loop_info)
5347 loop_vec_info vec_info = loop_vec_info_for_loop (cost_data->loop_info);
5348 if (!rs6000_vect_nonmem
5349 && LOOP_VINFO_VECT_FACTOR (vec_info) == 2
5350 && LOOP_REQUIRES_VERSIONING (vec_info))
5351 cost_data->cost[vect_body] += 10000;
5354 *prologue_cost = cost_data->cost[vect_prologue];
5355 *body_cost = cost_data->cost[vect_body];
5356 *epilogue_cost = cost_data->cost[vect_epilogue];
5359 /* Implement targetm.vectorize.destroy_cost_data. */
5361 static void
5362 rs6000_destroy_cost_data (void *data)
5364 free (data);
5367 /* Handler for the Mathematical Acceleration Subsystem (mass) interface to a
5368 library with vectorized intrinsics. */
5370 static tree
5371 rs6000_builtin_vectorized_libmass (combined_fn fn, tree type_out,
5372 tree type_in)
5374 char name[32];
5375 const char *suffix = NULL;
5376 tree fntype, new_fndecl, bdecl = NULL_TREE;
5377 int n_args = 1;
5378 const char *bname;
5379 machine_mode el_mode, in_mode;
5380 int n, in_n;
5382 /* Libmass is suitable for unsafe math only as it does not correctly support
5383 parts of IEEE with the required precision such as denormals. Only support
5384 it if we have VSX to use the simd d2 or f4 functions.
5385 XXX: Add variable length support. */
5386 if (!flag_unsafe_math_optimizations || !TARGET_VSX)
5387 return NULL_TREE;
5389 el_mode = TYPE_MODE (TREE_TYPE (type_out));
5390 n = TYPE_VECTOR_SUBPARTS (type_out);
5391 in_mode = TYPE_MODE (TREE_TYPE (type_in));
5392 in_n = TYPE_VECTOR_SUBPARTS (type_in);
5393 if (el_mode != in_mode
5394 || n != in_n)
5395 return NULL_TREE;
5397 switch (fn)
5399 CASE_CFN_ATAN2:
5400 CASE_CFN_HYPOT:
5401 CASE_CFN_POW:
5402 n_args = 2;
5403 gcc_fallthrough ();
5405 CASE_CFN_ACOS:
5406 CASE_CFN_ACOSH:
5407 CASE_CFN_ASIN:
5408 CASE_CFN_ASINH:
5409 CASE_CFN_ATAN:
5410 CASE_CFN_ATANH:
5411 CASE_CFN_CBRT:
5412 CASE_CFN_COS:
5413 CASE_CFN_COSH:
5414 CASE_CFN_ERF:
5415 CASE_CFN_ERFC:
5416 CASE_CFN_EXP2:
5417 CASE_CFN_EXP:
5418 CASE_CFN_EXPM1:
5419 CASE_CFN_LGAMMA:
5420 CASE_CFN_LOG10:
5421 CASE_CFN_LOG1P:
5422 CASE_CFN_LOG2:
5423 CASE_CFN_LOG:
5424 CASE_CFN_SIN:
5425 CASE_CFN_SINH:
5426 CASE_CFN_SQRT:
5427 CASE_CFN_TAN:
5428 CASE_CFN_TANH:
5429 if (el_mode == DFmode && n == 2)
5431 bdecl = mathfn_built_in (double_type_node, fn);
5432 suffix = "d2"; /* pow -> powd2 */
5434 else if (el_mode == SFmode && n == 4)
5436 bdecl = mathfn_built_in (float_type_node, fn);
5437 suffix = "4"; /* powf -> powf4 */
5439 else
5440 return NULL_TREE;
5441 if (!bdecl)
5442 return NULL_TREE;
5443 break;
5445 default:
5446 return NULL_TREE;
5449 gcc_assert (suffix != NULL);
5450 bname = IDENTIFIER_POINTER (DECL_NAME (bdecl));
5451 if (!bname)
5452 return NULL_TREE;
5454 strcpy (name, bname + sizeof ("__builtin_") - 1);
5455 strcat (name, suffix);
5457 if (n_args == 1)
5458 fntype = build_function_type_list (type_out, type_in, NULL);
5459 else if (n_args == 2)
5460 fntype = build_function_type_list (type_out, type_in, type_in, NULL);
5461 else
5462 gcc_unreachable ();
5464 /* Build a function declaration for the vectorized function. */
5465 new_fndecl = build_decl (BUILTINS_LOCATION,
5466 FUNCTION_DECL, get_identifier (name), fntype);
5467 TREE_PUBLIC (new_fndecl) = 1;
5468 DECL_EXTERNAL (new_fndecl) = 1;
5469 DECL_IS_NOVOPS (new_fndecl) = 1;
5470 TREE_READONLY (new_fndecl) = 1;
5472 return new_fndecl;
5475 /* Returns a function decl for a vectorized version of the builtin function
5476 with builtin function code FN and the result vector type TYPE, or NULL_TREE
5477 if it is not available. */
5479 static tree
5480 rs6000_builtin_vectorized_function (unsigned int fn, tree type_out,
5481 tree type_in)
5483 machine_mode in_mode, out_mode;
5484 int in_n, out_n;
5486 if (TARGET_DEBUG_BUILTIN)
5487 fprintf (stderr, "rs6000_builtin_vectorized_function (%s, %s, %s)\n",
5488 combined_fn_name (combined_fn (fn)),
5489 GET_MODE_NAME (TYPE_MODE (type_out)),
5490 GET_MODE_NAME (TYPE_MODE (type_in)));
5492 if (TREE_CODE (type_out) != VECTOR_TYPE
5493 || TREE_CODE (type_in) != VECTOR_TYPE)
5494 return NULL_TREE;
5496 out_mode = TYPE_MODE (TREE_TYPE (type_out));
5497 out_n = TYPE_VECTOR_SUBPARTS (type_out);
5498 in_mode = TYPE_MODE (TREE_TYPE (type_in));
5499 in_n = TYPE_VECTOR_SUBPARTS (type_in);
5501 switch (fn)
5503 CASE_CFN_COPYSIGN:
5504 if (VECTOR_UNIT_VSX_P (V2DFmode)
5505 && out_mode == DFmode && out_n == 2
5506 && in_mode == DFmode && in_n == 2)
5507 return rs6000_builtin_decls[VSX_BUILTIN_CPSGNDP];
5508 if (VECTOR_UNIT_VSX_P (V4SFmode)
5509 && out_mode == SFmode && out_n == 4
5510 && in_mode == SFmode && in_n == 4)
5511 return rs6000_builtin_decls[VSX_BUILTIN_CPSGNSP];
5512 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode)
5513 && out_mode == SFmode && out_n == 4
5514 && in_mode == SFmode && in_n == 4)
5515 return rs6000_builtin_decls[ALTIVEC_BUILTIN_COPYSIGN_V4SF];
5516 break;
5517 CASE_CFN_CEIL:
5518 if (VECTOR_UNIT_VSX_P (V2DFmode)
5519 && out_mode == DFmode && out_n == 2
5520 && in_mode == DFmode && in_n == 2)
5521 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIP];
5522 if (VECTOR_UNIT_VSX_P (V4SFmode)
5523 && out_mode == SFmode && out_n == 4
5524 && in_mode == SFmode && in_n == 4)
5525 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIP];
5526 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode)
5527 && out_mode == SFmode && out_n == 4
5528 && in_mode == SFmode && in_n == 4)
5529 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIP];
5530 break;
5531 CASE_CFN_FLOOR:
5532 if (VECTOR_UNIT_VSX_P (V2DFmode)
5533 && out_mode == DFmode && out_n == 2
5534 && in_mode == DFmode && in_n == 2)
5535 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIM];
5536 if (VECTOR_UNIT_VSX_P (V4SFmode)
5537 && out_mode == SFmode && out_n == 4
5538 && in_mode == SFmode && in_n == 4)
5539 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIM];
5540 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode)
5541 && out_mode == SFmode && out_n == 4
5542 && in_mode == SFmode && in_n == 4)
5543 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIM];
5544 break;
5545 CASE_CFN_FMA:
5546 if (VECTOR_UNIT_VSX_P (V2DFmode)
5547 && out_mode == DFmode && out_n == 2
5548 && in_mode == DFmode && in_n == 2)
5549 return rs6000_builtin_decls[VSX_BUILTIN_XVMADDDP];
5550 if (VECTOR_UNIT_VSX_P (V4SFmode)
5551 && out_mode == SFmode && out_n == 4
5552 && in_mode == SFmode && in_n == 4)
5553 return rs6000_builtin_decls[VSX_BUILTIN_XVMADDSP];
5554 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode)
5555 && out_mode == SFmode && out_n == 4
5556 && in_mode == SFmode && in_n == 4)
5557 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VMADDFP];
5558 break;
5559 CASE_CFN_TRUNC:
5560 if (VECTOR_UNIT_VSX_P (V2DFmode)
5561 && out_mode == DFmode && out_n == 2
5562 && in_mode == DFmode && in_n == 2)
5563 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIZ];
5564 if (VECTOR_UNIT_VSX_P (V4SFmode)
5565 && out_mode == SFmode && out_n == 4
5566 && in_mode == SFmode && in_n == 4)
5567 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIZ];
5568 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode)
5569 && out_mode == SFmode && out_n == 4
5570 && in_mode == SFmode && in_n == 4)
5571 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIZ];
5572 break;
5573 CASE_CFN_NEARBYINT:
5574 if (VECTOR_UNIT_VSX_P (V2DFmode)
5575 && flag_unsafe_math_optimizations
5576 && out_mode == DFmode && out_n == 2
5577 && in_mode == DFmode && in_n == 2)
5578 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPI];
5579 if (VECTOR_UNIT_VSX_P (V4SFmode)
5580 && flag_unsafe_math_optimizations
5581 && out_mode == SFmode && out_n == 4
5582 && in_mode == SFmode && in_n == 4)
5583 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPI];
5584 break;
5585 CASE_CFN_RINT:
5586 if (VECTOR_UNIT_VSX_P (V2DFmode)
5587 && !flag_trapping_math
5588 && out_mode == DFmode && out_n == 2
5589 && in_mode == DFmode && in_n == 2)
5590 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIC];
5591 if (VECTOR_UNIT_VSX_P (V4SFmode)
5592 && !flag_trapping_math
5593 && out_mode == SFmode && out_n == 4
5594 && in_mode == SFmode && in_n == 4)
5595 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIC];
5596 break;
5597 default:
5598 break;
5601 /* Generate calls to libmass if appropriate. */
5602 if (rs6000_veclib_handler)
5603 return rs6000_veclib_handler (combined_fn (fn), type_out, type_in);
5605 return NULL_TREE;
5608 /* Implement TARGET_VECTORIZE_BUILTIN_MD_VECTORIZED_FUNCTION. */
5610 static tree
5611 rs6000_builtin_md_vectorized_function (tree fndecl, tree type_out,
5612 tree type_in)
5614 machine_mode in_mode, out_mode;
5615 int in_n, out_n;
5617 if (TARGET_DEBUG_BUILTIN)
5618 fprintf (stderr, "rs6000_builtin_md_vectorized_function (%s, %s, %s)\n",
5619 IDENTIFIER_POINTER (DECL_NAME (fndecl)),
5620 GET_MODE_NAME (TYPE_MODE (type_out)),
5621 GET_MODE_NAME (TYPE_MODE (type_in)));
5623 if (TREE_CODE (type_out) != VECTOR_TYPE
5624 || TREE_CODE (type_in) != VECTOR_TYPE)
5625 return NULL_TREE;
5627 out_mode = TYPE_MODE (TREE_TYPE (type_out));
5628 out_n = TYPE_VECTOR_SUBPARTS (type_out);
5629 in_mode = TYPE_MODE (TREE_TYPE (type_in));
5630 in_n = TYPE_VECTOR_SUBPARTS (type_in);
5632 enum rs6000_builtins fn
5633 = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
5634 switch (fn)
5636 case RS6000_BUILTIN_RSQRTF:
5637 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode)
5638 && out_mode == SFmode && out_n == 4
5639 && in_mode == SFmode && in_n == 4)
5640 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRSQRTFP];
5641 break;
5642 case RS6000_BUILTIN_RSQRT:
5643 if (VECTOR_UNIT_VSX_P (V2DFmode)
5644 && out_mode == DFmode && out_n == 2
5645 && in_mode == DFmode && in_n == 2)
5646 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_2DF];
5647 break;
5648 case RS6000_BUILTIN_RECIPF:
5649 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode)
5650 && out_mode == SFmode && out_n == 4
5651 && in_mode == SFmode && in_n == 4)
5652 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRECIPFP];
5653 break;
5654 case RS6000_BUILTIN_RECIP:
5655 if (VECTOR_UNIT_VSX_P (V2DFmode)
5656 && out_mode == DFmode && out_n == 2
5657 && in_mode == DFmode && in_n == 2)
5658 return rs6000_builtin_decls[VSX_BUILTIN_RECIP_V2DF];
5659 break;
5660 default:
5661 break;
5663 return NULL_TREE;
5666 /* Default CPU string for rs6000*_file_start functions. */
5667 static const char *rs6000_default_cpu;
5669 /* Do anything needed at the start of the asm file. */
5671 static void
5672 rs6000_file_start (void)
5674 char buffer[80];
5675 const char *start = buffer;
5676 FILE *file = asm_out_file;
5678 rs6000_default_cpu = TARGET_CPU_DEFAULT;
5680 default_file_start ();
5682 if (flag_verbose_asm)
5684 sprintf (buffer, "\n%s rs6000/powerpc options:", ASM_COMMENT_START);
5686 if (rs6000_default_cpu != 0 && rs6000_default_cpu[0] != '\0')
5688 fprintf (file, "%s --with-cpu=%s", start, rs6000_default_cpu);
5689 start = "";
5692 if (global_options_set.x_rs6000_cpu_index)
5694 fprintf (file, "%s -mcpu=%s", start,
5695 processor_target_table[rs6000_cpu_index].name);
5696 start = "";
5699 if (global_options_set.x_rs6000_tune_index)
5701 fprintf (file, "%s -mtune=%s", start,
5702 processor_target_table[rs6000_tune_index].name);
5703 start = "";
5706 if (PPC405_ERRATUM77)
5708 fprintf (file, "%s PPC405CR_ERRATUM77", start);
5709 start = "";
5712 #ifdef USING_ELFOS_H
5713 switch (rs6000_sdata)
5715 case SDATA_NONE: fprintf (file, "%s -msdata=none", start); start = ""; break;
5716 case SDATA_DATA: fprintf (file, "%s -msdata=data", start); start = ""; break;
5717 case SDATA_SYSV: fprintf (file, "%s -msdata=sysv", start); start = ""; break;
5718 case SDATA_EABI: fprintf (file, "%s -msdata=eabi", start); start = ""; break;
5721 if (rs6000_sdata && g_switch_value)
5723 fprintf (file, "%s -G %d", start,
5724 g_switch_value);
5725 start = "";
5727 #endif
5729 if (*start == '\0')
5730 putc ('\n', file);
5733 #ifdef USING_ELFOS_H
5734 if (!(rs6000_default_cpu && rs6000_default_cpu[0])
5735 && !global_options_set.x_rs6000_cpu_index)
5737 fputs ("\t.machine ", asm_out_file);
5738 if ((rs6000_isa_flags & OPTION_MASK_MODULO) != 0)
5739 fputs ("power9\n", asm_out_file);
5740 else if ((rs6000_isa_flags & OPTION_MASK_DIRECT_MOVE) != 0)
5741 fputs ("power8\n", asm_out_file);
5742 else if ((rs6000_isa_flags & OPTION_MASK_POPCNTD) != 0)
5743 fputs ("power7\n", asm_out_file);
5744 else if ((rs6000_isa_flags & OPTION_MASK_CMPB) != 0)
5745 fputs ("power6\n", asm_out_file);
5746 else if ((rs6000_isa_flags & OPTION_MASK_POPCNTB) != 0)
5747 fputs ("power5\n", asm_out_file);
5748 else if ((rs6000_isa_flags & OPTION_MASK_MFCRF) != 0)
5749 fputs ("power4\n", asm_out_file);
5750 else if ((rs6000_isa_flags & OPTION_MASK_POWERPC64) != 0)
5751 fputs ("ppc64\n", asm_out_file);
5752 else
5753 fputs ("ppc\n", asm_out_file);
5755 #endif
5757 if (DEFAULT_ABI == ABI_ELFv2)
5758 fprintf (file, "\t.abiversion 2\n");
5762 /* Return nonzero if this function is known to have a null epilogue. */
5765 direct_return (void)
5767 if (reload_completed)
5769 rs6000_stack_t *info = rs6000_stack_info ();
5771 if (info->first_gp_reg_save == 32
5772 && info->first_fp_reg_save == 64
5773 && info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1
5774 && ! info->lr_save_p
5775 && ! info->cr_save_p
5776 && info->vrsave_size == 0
5777 && ! info->push_p)
5778 return 1;
5781 return 0;
5784 /* Helper for num_insns_constant. Calculate number of instructions to
5785 load VALUE to a single gpr using combinations of addi, addis, ori,
5786 oris and sldi instructions. */
5788 static int
5789 num_insns_constant_gpr (HOST_WIDE_INT value)
5791 /* signed constant loadable with addi */
5792 if (((unsigned HOST_WIDE_INT) value + 0x8000) < 0x10000)
5793 return 1;
5795 /* constant loadable with addis */
5796 else if ((value & 0xffff) == 0
5797 && (value >> 31 == -1 || value >> 31 == 0))
5798 return 1;
5800 else if (TARGET_POWERPC64)
5802 HOST_WIDE_INT low = ((value & 0xffffffff) ^ 0x80000000) - 0x80000000;
5803 HOST_WIDE_INT high = value >> 31;
5805 if (high == 0 || high == -1)
5806 return 2;
5808 high >>= 1;
5810 if (low == 0)
5811 return num_insns_constant_gpr (high) + 1;
5812 else if (high == 0)
5813 return num_insns_constant_gpr (low) + 1;
5814 else
5815 return (num_insns_constant_gpr (high)
5816 + num_insns_constant_gpr (low) + 1);
5819 else
5820 return 2;
5823 /* Helper for num_insns_constant. Allow constants formed by the
5824 num_insns_constant_gpr sequences, plus li -1, rldicl/rldicr/rlwinm,
5825 and handle modes that require multiple gprs. */
5827 static int
5828 num_insns_constant_multi (HOST_WIDE_INT value, machine_mode mode)
5830 int nregs = (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
5831 int total = 0;
5832 while (nregs-- > 0)
5834 HOST_WIDE_INT low = sext_hwi (value, BITS_PER_WORD);
5835 int insns = num_insns_constant_gpr (low);
5836 if (insns > 2
5837 /* We won't get more than 2 from num_insns_constant_gpr
5838 except when TARGET_POWERPC64 and mode is DImode or
5839 wider, so the register mode must be DImode. */
5840 && rs6000_is_valid_and_mask (GEN_INT (low), DImode))
5841 insns = 2;
5842 total += insns;
5843 value >>= BITS_PER_WORD;
5845 return total;
5848 /* Return the number of instructions it takes to form a constant in as
5849 many gprs are needed for MODE. */
5852 num_insns_constant (rtx op, machine_mode mode)
5854 HOST_WIDE_INT val;
5856 switch (GET_CODE (op))
5858 case CONST_INT:
5859 val = INTVAL (op);
5860 break;
5862 case CONST_WIDE_INT:
5864 int insns = 0;
5865 for (int i = 0; i < CONST_WIDE_INT_NUNITS (op); i++)
5866 insns += num_insns_constant_multi (CONST_WIDE_INT_ELT (op, i),
5867 DImode);
5868 return insns;
5871 case CONST_DOUBLE:
5873 const struct real_value *rv = CONST_DOUBLE_REAL_VALUE (op);
5875 if (mode == SFmode || mode == SDmode)
5877 long l;
5879 if (mode == SDmode)
5880 REAL_VALUE_TO_TARGET_DECIMAL32 (*rv, l);
5881 else
5882 REAL_VALUE_TO_TARGET_SINGLE (*rv, l);
5883 /* See the first define_split in rs6000.md handling a
5884 const_double_operand. */
5885 val = l;
5886 mode = SImode;
5888 else if (mode == DFmode || mode == DDmode)
5890 long l[2];
5892 if (mode == DDmode)
5893 REAL_VALUE_TO_TARGET_DECIMAL64 (*rv, l);
5894 else
5895 REAL_VALUE_TO_TARGET_DOUBLE (*rv, l);
5897 /* See the second (32-bit) and third (64-bit) define_split
5898 in rs6000.md handling a const_double_operand. */
5899 val = (unsigned HOST_WIDE_INT) l[WORDS_BIG_ENDIAN ? 0 : 1] << 32;
5900 val |= l[WORDS_BIG_ENDIAN ? 1 : 0] & 0xffffffffUL;
5901 mode = DImode;
5903 else if (mode == TFmode || mode == TDmode
5904 || mode == KFmode || mode == IFmode)
5906 long l[4];
5907 int insns;
5909 if (mode == TDmode)
5910 REAL_VALUE_TO_TARGET_DECIMAL128 (*rv, l);
5911 else
5912 REAL_VALUE_TO_TARGET_LONG_DOUBLE (*rv, l);
5914 val = (unsigned HOST_WIDE_INT) l[WORDS_BIG_ENDIAN ? 0 : 3] << 32;
5915 val |= l[WORDS_BIG_ENDIAN ? 1 : 2] & 0xffffffffUL;
5916 insns = num_insns_constant_multi (val, DImode);
5917 val = (unsigned HOST_WIDE_INT) l[WORDS_BIG_ENDIAN ? 2 : 1] << 32;
5918 val |= l[WORDS_BIG_ENDIAN ? 3 : 0] & 0xffffffffUL;
5919 insns += num_insns_constant_multi (val, DImode);
5920 return insns;
5922 else
5923 gcc_unreachable ();
5925 break;
5927 default:
5928 gcc_unreachable ();
5931 return num_insns_constant_multi (val, mode);
5934 /* Interpret element ELT of the CONST_VECTOR OP as an integer value.
5935 If the mode of OP is MODE_VECTOR_INT, this simply returns the
5936 corresponding element of the vector, but for V4SFmode, the
5937 corresponding "float" is interpreted as an SImode integer. */
5939 HOST_WIDE_INT
5940 const_vector_elt_as_int (rtx op, unsigned int elt)
5942 rtx tmp;
5944 /* We can't handle V2DImode and V2DFmode vector constants here yet. */
5945 gcc_assert (GET_MODE (op) != V2DImode
5946 && GET_MODE (op) != V2DFmode);
5948 tmp = CONST_VECTOR_ELT (op, elt);
5949 if (GET_MODE (op) == V4SFmode)
5950 tmp = gen_lowpart (SImode, tmp);
5951 return INTVAL (tmp);
5954 /* Return true if OP can be synthesized with a particular vspltisb, vspltish
5955 or vspltisw instruction. OP is a CONST_VECTOR. Which instruction is used
5956 depends on STEP and COPIES, one of which will be 1. If COPIES > 1,
5957 all items are set to the same value and contain COPIES replicas of the
5958 vsplt's operand; if STEP > 1, one in STEP elements is set to the vsplt's
5959 operand and the others are set to the value of the operand's msb. */
5961 static bool
5962 vspltis_constant (rtx op, unsigned step, unsigned copies)
5964 machine_mode mode = GET_MODE (op);
5965 machine_mode inner = GET_MODE_INNER (mode);
5967 unsigned i;
5968 unsigned nunits;
5969 unsigned bitsize;
5970 unsigned mask;
5972 HOST_WIDE_INT val;
5973 HOST_WIDE_INT splat_val;
5974 HOST_WIDE_INT msb_val;
5976 if (mode == V2DImode || mode == V2DFmode || mode == V1TImode)
5977 return false;
5979 nunits = GET_MODE_NUNITS (mode);
5980 bitsize = GET_MODE_BITSIZE (inner);
5981 mask = GET_MODE_MASK (inner);
5983 val = const_vector_elt_as_int (op, BYTES_BIG_ENDIAN ? nunits - 1 : 0);
5984 splat_val = val;
5985 msb_val = val >= 0 ? 0 : -1;
5987 /* Construct the value to be splatted, if possible. If not, return 0. */
5988 for (i = 2; i <= copies; i *= 2)
5990 HOST_WIDE_INT small_val;
5991 bitsize /= 2;
5992 small_val = splat_val >> bitsize;
5993 mask >>= bitsize;
5994 if (splat_val != ((HOST_WIDE_INT)
5995 ((unsigned HOST_WIDE_INT) small_val << bitsize)
5996 | (small_val & mask)))
5997 return false;
5998 splat_val = small_val;
6001 /* Check if SPLAT_VAL can really be the operand of a vspltis[bhw]. */
6002 if (EASY_VECTOR_15 (splat_val))
6005 /* Also check if we can splat, and then add the result to itself. Do so if
6006 the value is positive, of if the splat instruction is using OP's mode;
6007 for splat_val < 0, the splat and the add should use the same mode. */
6008 else if (EASY_VECTOR_15_ADD_SELF (splat_val)
6009 && (splat_val >= 0 || (step == 1 && copies == 1)))
6012 /* Also check if are loading up the most significant bit which can be done by
6013 loading up -1 and shifting the value left by -1. */
6014 else if (EASY_VECTOR_MSB (splat_val, inner))
6017 else
6018 return false;
6020 /* Check if VAL is present in every STEP-th element, and the
6021 other elements are filled with its most significant bit. */
6022 for (i = 1; i < nunits; ++i)
6024 HOST_WIDE_INT desired_val;
6025 unsigned elt = BYTES_BIG_ENDIAN ? nunits - 1 - i : i;
6026 if ((i & (step - 1)) == 0)
6027 desired_val = val;
6028 else
6029 desired_val = msb_val;
6031 if (desired_val != const_vector_elt_as_int (op, elt))
6032 return false;
6035 return true;
6038 /* Like vsplitis_constant, but allow the value to be shifted left with a VSLDOI
6039 instruction, filling in the bottom elements with 0 or -1.
6041 Return 0 if the constant cannot be generated with VSLDOI. Return positive
6042 for the number of zeroes to shift in, or negative for the number of 0xff
6043 bytes to shift in.
6045 OP is a CONST_VECTOR. */
6048 vspltis_shifted (rtx op)
6050 machine_mode mode = GET_MODE (op);
6051 machine_mode inner = GET_MODE_INNER (mode);
6053 unsigned i, j;
6054 unsigned nunits;
6055 unsigned mask;
6057 HOST_WIDE_INT val;
6059 if (mode != V16QImode && mode != V8HImode && mode != V4SImode)
6060 return false;
6062 /* We need to create pseudo registers to do the shift, so don't recognize
6063 shift vector constants after reload. */
6064 if (!can_create_pseudo_p ())
6065 return false;
6067 nunits = GET_MODE_NUNITS (mode);
6068 mask = GET_MODE_MASK (inner);
6070 val = const_vector_elt_as_int (op, BYTES_BIG_ENDIAN ? 0 : nunits - 1);
6072 /* Check if the value can really be the operand of a vspltis[bhw]. */
6073 if (EASY_VECTOR_15 (val))
6076 /* Also check if we are loading up the most significant bit which can be done
6077 by loading up -1 and shifting the value left by -1. */
6078 else if (EASY_VECTOR_MSB (val, inner))
6081 else
6082 return 0;
6084 /* Check if VAL is present in every STEP-th element until we find elements
6085 that are 0 or all 1 bits. */
6086 for (i = 1; i < nunits; ++i)
6088 unsigned elt = BYTES_BIG_ENDIAN ? i : nunits - 1 - i;
6089 HOST_WIDE_INT elt_val = const_vector_elt_as_int (op, elt);
6091 /* If the value isn't the splat value, check for the remaining elements
6092 being 0/-1. */
6093 if (val != elt_val)
6095 if (elt_val == 0)
6097 for (j = i+1; j < nunits; ++j)
6099 unsigned elt2 = BYTES_BIG_ENDIAN ? j : nunits - 1 - j;
6100 if (const_vector_elt_as_int (op, elt2) != 0)
6101 return 0;
6104 return (nunits - i) * GET_MODE_SIZE (inner);
6107 else if ((elt_val & mask) == mask)
6109 for (j = i+1; j < nunits; ++j)
6111 unsigned elt2 = BYTES_BIG_ENDIAN ? j : nunits - 1 - j;
6112 if ((const_vector_elt_as_int (op, elt2) & mask) != mask)
6113 return 0;
6116 return -((nunits - i) * GET_MODE_SIZE (inner));
6119 else
6120 return 0;
6124 /* If all elements are equal, we don't need to do VLSDOI. */
6125 return 0;
6129 /* Return true if OP is of the given MODE and can be synthesized
6130 with a vspltisb, vspltish or vspltisw. */
6132 bool
6133 easy_altivec_constant (rtx op, machine_mode mode)
6135 unsigned step, copies;
6137 if (mode == VOIDmode)
6138 mode = GET_MODE (op);
6139 else if (mode != GET_MODE (op))
6140 return false;
6142 /* V2DI/V2DF was added with VSX. Only allow 0 and all 1's as easy
6143 constants. */
6144 if (mode == V2DFmode)
6145 return zero_constant (op, mode);
6147 else if (mode == V2DImode)
6149 if (!CONST_INT_P (CONST_VECTOR_ELT (op, 0))
6150 || !CONST_INT_P (CONST_VECTOR_ELT (op, 1)))
6151 return false;
6153 if (zero_constant (op, mode))
6154 return true;
6156 if (INTVAL (CONST_VECTOR_ELT (op, 0)) == -1
6157 && INTVAL (CONST_VECTOR_ELT (op, 1)) == -1)
6158 return true;
6160 return false;
6163 /* V1TImode is a special container for TImode. Ignore for now. */
6164 else if (mode == V1TImode)
6165 return false;
6167 /* Start with a vspltisw. */
6168 step = GET_MODE_NUNITS (mode) / 4;
6169 copies = 1;
6171 if (vspltis_constant (op, step, copies))
6172 return true;
6174 /* Then try with a vspltish. */
6175 if (step == 1)
6176 copies <<= 1;
6177 else
6178 step >>= 1;
6180 if (vspltis_constant (op, step, copies))
6181 return true;
6183 /* And finally a vspltisb. */
6184 if (step == 1)
6185 copies <<= 1;
6186 else
6187 step >>= 1;
6189 if (vspltis_constant (op, step, copies))
6190 return true;
6192 if (vspltis_shifted (op) != 0)
6193 return true;
6195 return false;
6198 /* Generate a VEC_DUPLICATE representing a vspltis[bhw] instruction whose
6199 result is OP. Abort if it is not possible. */
6202 gen_easy_altivec_constant (rtx op)
6204 machine_mode mode = GET_MODE (op);
6205 int nunits = GET_MODE_NUNITS (mode);
6206 rtx val = CONST_VECTOR_ELT (op, BYTES_BIG_ENDIAN ? nunits - 1 : 0);
6207 unsigned step = nunits / 4;
6208 unsigned copies = 1;
6210 /* Start with a vspltisw. */
6211 if (vspltis_constant (op, step, copies))
6212 return gen_rtx_VEC_DUPLICATE (V4SImode, gen_lowpart (SImode, val));
6214 /* Then try with a vspltish. */
6215 if (step == 1)
6216 copies <<= 1;
6217 else
6218 step >>= 1;
6220 if (vspltis_constant (op, step, copies))
6221 return gen_rtx_VEC_DUPLICATE (V8HImode, gen_lowpart (HImode, val));
6223 /* And finally a vspltisb. */
6224 if (step == 1)
6225 copies <<= 1;
6226 else
6227 step >>= 1;
6229 if (vspltis_constant (op, step, copies))
6230 return gen_rtx_VEC_DUPLICATE (V16QImode, gen_lowpart (QImode, val));
6232 gcc_unreachable ();
6235 /* Return true if OP is of the given MODE and can be synthesized with ISA 3.0
6236 instructions (xxspltib, vupkhsb/vextsb2w/vextb2d).
6238 Return the number of instructions needed (1 or 2) into the address pointed
6239 via NUM_INSNS_PTR.
6241 Return the constant that is being split via CONSTANT_PTR. */
6243 bool
6244 xxspltib_constant_p (rtx op,
6245 machine_mode mode,
6246 int *num_insns_ptr,
6247 int *constant_ptr)
6249 size_t nunits = GET_MODE_NUNITS (mode);
6250 size_t i;
6251 HOST_WIDE_INT value;
6252 rtx element;
6254 /* Set the returned values to out of bound values. */
6255 *num_insns_ptr = -1;
6256 *constant_ptr = 256;
6258 if (!TARGET_P9_VECTOR)
6259 return false;
6261 if (mode == VOIDmode)
6262 mode = GET_MODE (op);
6264 else if (mode != GET_MODE (op) && GET_MODE (op) != VOIDmode)
6265 return false;
6267 /* Handle (vec_duplicate <constant>). */
6268 if (GET_CODE (op) == VEC_DUPLICATE)
6270 if (mode != V16QImode && mode != V8HImode && mode != V4SImode
6271 && mode != V2DImode)
6272 return false;
6274 element = XEXP (op, 0);
6275 if (!CONST_INT_P (element))
6276 return false;
6278 value = INTVAL (element);
6279 if (!IN_RANGE (value, -128, 127))
6280 return false;
6283 /* Handle (const_vector [...]). */
6284 else if (GET_CODE (op) == CONST_VECTOR)
6286 if (mode != V16QImode && mode != V8HImode && mode != V4SImode
6287 && mode != V2DImode)
6288 return false;
6290 element = CONST_VECTOR_ELT (op, 0);
6291 if (!CONST_INT_P (element))
6292 return false;
6294 value = INTVAL (element);
6295 if (!IN_RANGE (value, -128, 127))
6296 return false;
6298 for (i = 1; i < nunits; i++)
6300 element = CONST_VECTOR_ELT (op, i);
6301 if (!CONST_INT_P (element))
6302 return false;
6304 if (value != INTVAL (element))
6305 return false;
6309 /* Handle integer constants being loaded into the upper part of the VSX
6310 register as a scalar. If the value isn't 0/-1, only allow it if the mode
6311 can go in Altivec registers. Prefer VSPLTISW/VUPKHSW over XXSPLITIB. */
6312 else if (CONST_INT_P (op))
6314 if (!SCALAR_INT_MODE_P (mode))
6315 return false;
6317 value = INTVAL (op);
6318 if (!IN_RANGE (value, -128, 127))
6319 return false;
6321 if (!IN_RANGE (value, -1, 0))
6323 if (!(reg_addr[mode].addr_mask[RELOAD_REG_VMX] & RELOAD_REG_VALID))
6324 return false;
6326 if (EASY_VECTOR_15 (value))
6327 return false;
6331 else
6332 return false;
6334 /* See if we could generate vspltisw/vspltish directly instead of xxspltib +
6335 sign extend. Special case 0/-1 to allow getting any VSX register instead
6336 of an Altivec register. */
6337 if ((mode == V4SImode || mode == V8HImode) && !IN_RANGE (value, -1, 0)
6338 && EASY_VECTOR_15 (value))
6339 return false;
6341 /* Return # of instructions and the constant byte for XXSPLTIB. */
6342 if (mode == V16QImode)
6343 *num_insns_ptr = 1;
6345 else if (IN_RANGE (value, -1, 0))
6346 *num_insns_ptr = 1;
6348 else
6349 *num_insns_ptr = 2;
6351 *constant_ptr = (int) value;
6352 return true;
6355 const char *
6356 output_vec_const_move (rtx *operands)
6358 int shift;
6359 machine_mode mode;
6360 rtx dest, vec;
6362 dest = operands[0];
6363 vec = operands[1];
6364 mode = GET_MODE (dest);
6366 if (TARGET_VSX)
6368 bool dest_vmx_p = ALTIVEC_REGNO_P (REGNO (dest));
6369 int xxspltib_value = 256;
6370 int num_insns = -1;
6372 if (zero_constant (vec, mode))
6374 if (TARGET_P9_VECTOR)
6375 return "xxspltib %x0,0";
6377 else if (dest_vmx_p)
6378 return "vspltisw %0,0";
6380 else
6381 return "xxlxor %x0,%x0,%x0";
6384 if (all_ones_constant (vec, mode))
6386 if (TARGET_P9_VECTOR)
6387 return "xxspltib %x0,255";
6389 else if (dest_vmx_p)
6390 return "vspltisw %0,-1";
6392 else if (TARGET_P8_VECTOR)
6393 return "xxlorc %x0,%x0,%x0";
6395 else
6396 gcc_unreachable ();
6399 if (TARGET_P9_VECTOR
6400 && xxspltib_constant_p (vec, mode, &num_insns, &xxspltib_value))
6402 if (num_insns == 1)
6404 operands[2] = GEN_INT (xxspltib_value & 0xff);
6405 return "xxspltib %x0,%2";
6408 return "#";
6412 if (TARGET_ALTIVEC)
6414 rtx splat_vec;
6416 gcc_assert (ALTIVEC_REGNO_P (REGNO (dest)));
6417 if (zero_constant (vec, mode))
6418 return "vspltisw %0,0";
6420 if (all_ones_constant (vec, mode))
6421 return "vspltisw %0,-1";
6423 /* Do we need to construct a value using VSLDOI? */
6424 shift = vspltis_shifted (vec);
6425 if (shift != 0)
6426 return "#";
6428 splat_vec = gen_easy_altivec_constant (vec);
6429 gcc_assert (GET_CODE (splat_vec) == VEC_DUPLICATE);
6430 operands[1] = XEXP (splat_vec, 0);
6431 if (!EASY_VECTOR_15 (INTVAL (operands[1])))
6432 return "#";
6434 switch (GET_MODE (splat_vec))
6436 case E_V4SImode:
6437 return "vspltisw %0,%1";
6439 case E_V8HImode:
6440 return "vspltish %0,%1";
6442 case E_V16QImode:
6443 return "vspltisb %0,%1";
6445 default:
6446 gcc_unreachable ();
6450 gcc_unreachable ();
6453 /* Initialize vector TARGET to VALS. */
6455 void
6456 rs6000_expand_vector_init (rtx target, rtx vals)
6458 machine_mode mode = GET_MODE (target);
6459 machine_mode inner_mode = GET_MODE_INNER (mode);
6460 int n_elts = GET_MODE_NUNITS (mode);
6461 int n_var = 0, one_var = -1;
6462 bool all_same = true, all_const_zero = true;
6463 rtx x, mem;
6464 int i;
6466 for (i = 0; i < n_elts; ++i)
6468 x = XVECEXP (vals, 0, i);
6469 if (!(CONST_SCALAR_INT_P (x) || CONST_DOUBLE_P (x) || CONST_FIXED_P (x)))
6470 ++n_var, one_var = i;
6471 else if (x != CONST0_RTX (inner_mode))
6472 all_const_zero = false;
6474 if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
6475 all_same = false;
6478 if (n_var == 0)
6480 rtx const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0));
6481 bool int_vector_p = (GET_MODE_CLASS (mode) == MODE_VECTOR_INT);
6482 if ((int_vector_p || TARGET_VSX) && all_const_zero)
6484 /* Zero register. */
6485 emit_move_insn (target, CONST0_RTX (mode));
6486 return;
6488 else if (int_vector_p && easy_vector_constant (const_vec, mode))
6490 /* Splat immediate. */
6491 emit_insn (gen_rtx_SET (target, const_vec));
6492 return;
6494 else
6496 /* Load from constant pool. */
6497 emit_move_insn (target, const_vec);
6498 return;
6502 /* Double word values on VSX can use xxpermdi or lxvdsx. */
6503 if (VECTOR_MEM_VSX_P (mode) && (mode == V2DFmode || mode == V2DImode))
6505 rtx op[2];
6506 size_t i;
6507 size_t num_elements = all_same ? 1 : 2;
6508 for (i = 0; i < num_elements; i++)
6510 op[i] = XVECEXP (vals, 0, i);
6511 /* Just in case there is a SUBREG with a smaller mode, do a
6512 conversion. */
6513 if (GET_MODE (op[i]) != inner_mode)
6515 rtx tmp = gen_reg_rtx (inner_mode);
6516 convert_move (tmp, op[i], 0);
6517 op[i] = tmp;
6519 /* Allow load with splat double word. */
6520 else if (MEM_P (op[i]))
6522 if (!all_same)
6523 op[i] = force_reg (inner_mode, op[i]);
6525 else if (!REG_P (op[i]))
6526 op[i] = force_reg (inner_mode, op[i]);
6529 if (all_same)
6531 if (mode == V2DFmode)
6532 emit_insn (gen_vsx_splat_v2df (target, op[0]));
6533 else
6534 emit_insn (gen_vsx_splat_v2di (target, op[0]));
6536 else
6538 if (mode == V2DFmode)
6539 emit_insn (gen_vsx_concat_v2df (target, op[0], op[1]));
6540 else
6541 emit_insn (gen_vsx_concat_v2di (target, op[0], op[1]));
6543 return;
6546 /* Special case initializing vector int if we are on 64-bit systems with
6547 direct move or we have the ISA 3.0 instructions. */
6548 if (mode == V4SImode && VECTOR_MEM_VSX_P (V4SImode)
6549 && TARGET_DIRECT_MOVE_64BIT)
6551 if (all_same)
6553 rtx element0 = XVECEXP (vals, 0, 0);
6554 if (MEM_P (element0))
6555 element0 = rs6000_force_indexed_or_indirect_mem (element0);
6556 else
6557 element0 = force_reg (SImode, element0);
6559 if (TARGET_P9_VECTOR)
6560 emit_insn (gen_vsx_splat_v4si (target, element0));
6561 else
6563 rtx tmp = gen_reg_rtx (DImode);
6564 emit_insn (gen_zero_extendsidi2 (tmp, element0));
6565 emit_insn (gen_vsx_splat_v4si_di (target, tmp));
6567 return;
6569 else
6571 rtx elements[4];
6572 size_t i;
6574 for (i = 0; i < 4; i++)
6575 elements[i] = force_reg (SImode, XVECEXP (vals, 0, i));
6577 emit_insn (gen_vsx_init_v4si (target, elements[0], elements[1],
6578 elements[2], elements[3]));
6579 return;
6583 /* With single precision floating point on VSX, know that internally single
6584 precision is actually represented as a double, and either make 2 V2DF
6585 vectors, and convert these vectors to single precision, or do one
6586 conversion, and splat the result to the other elements. */
6587 if (mode == V4SFmode && VECTOR_MEM_VSX_P (V4SFmode))
6589 if (all_same)
6591 rtx element0 = XVECEXP (vals, 0, 0);
6593 if (TARGET_P9_VECTOR)
6595 if (MEM_P (element0))
6596 element0 = rs6000_force_indexed_or_indirect_mem (element0);
6598 emit_insn (gen_vsx_splat_v4sf (target, element0));
6601 else
6603 rtx freg = gen_reg_rtx (V4SFmode);
6604 rtx sreg = force_reg (SFmode, element0);
6605 rtx cvt = (TARGET_XSCVDPSPN
6606 ? gen_vsx_xscvdpspn_scalar (freg, sreg)
6607 : gen_vsx_xscvdpsp_scalar (freg, sreg));
6609 emit_insn (cvt);
6610 emit_insn (gen_vsx_xxspltw_v4sf_direct (target, freg,
6611 const0_rtx));
6614 else
6616 rtx dbl_even = gen_reg_rtx (V2DFmode);
6617 rtx dbl_odd = gen_reg_rtx (V2DFmode);
6618 rtx flt_even = gen_reg_rtx (V4SFmode);
6619 rtx flt_odd = gen_reg_rtx (V4SFmode);
6620 rtx op0 = force_reg (SFmode, XVECEXP (vals, 0, 0));
6621 rtx op1 = force_reg (SFmode, XVECEXP (vals, 0, 1));
6622 rtx op2 = force_reg (SFmode, XVECEXP (vals, 0, 2));
6623 rtx op3 = force_reg (SFmode, XVECEXP (vals, 0, 3));
6625 /* Use VMRGEW if we can instead of doing a permute. */
6626 if (TARGET_P8_VECTOR)
6628 emit_insn (gen_vsx_concat_v2sf (dbl_even, op0, op2));
6629 emit_insn (gen_vsx_concat_v2sf (dbl_odd, op1, op3));
6630 emit_insn (gen_vsx_xvcvdpsp (flt_even, dbl_even));
6631 emit_insn (gen_vsx_xvcvdpsp (flt_odd, dbl_odd));
6632 if (BYTES_BIG_ENDIAN)
6633 emit_insn (gen_p8_vmrgew_v4sf_direct (target, flt_even, flt_odd));
6634 else
6635 emit_insn (gen_p8_vmrgew_v4sf_direct (target, flt_odd, flt_even));
6637 else
6639 emit_insn (gen_vsx_concat_v2sf (dbl_even, op0, op1));
6640 emit_insn (gen_vsx_concat_v2sf (dbl_odd, op2, op3));
6641 emit_insn (gen_vsx_xvcvdpsp (flt_even, dbl_even));
6642 emit_insn (gen_vsx_xvcvdpsp (flt_odd, dbl_odd));
6643 rs6000_expand_extract_even (target, flt_even, flt_odd);
6646 return;
6649 /* Special case initializing vector short/char that are splats if we are on
6650 64-bit systems with direct move. */
6651 if (all_same && TARGET_DIRECT_MOVE_64BIT
6652 && (mode == V16QImode || mode == V8HImode))
6654 rtx op0 = XVECEXP (vals, 0, 0);
6655 rtx di_tmp = gen_reg_rtx (DImode);
6657 if (!REG_P (op0))
6658 op0 = force_reg (GET_MODE_INNER (mode), op0);
6660 if (mode == V16QImode)
6662 emit_insn (gen_zero_extendqidi2 (di_tmp, op0));
6663 emit_insn (gen_vsx_vspltb_di (target, di_tmp));
6664 return;
6667 if (mode == V8HImode)
6669 emit_insn (gen_zero_extendhidi2 (di_tmp, op0));
6670 emit_insn (gen_vsx_vsplth_di (target, di_tmp));
6671 return;
6675 /* Store value to stack temp. Load vector element. Splat. However, splat
6676 of 64-bit items is not supported on Altivec. */
6677 if (all_same && GET_MODE_SIZE (inner_mode) <= 4)
6679 mem = assign_stack_temp (mode, GET_MODE_SIZE (inner_mode));
6680 emit_move_insn (adjust_address_nv (mem, inner_mode, 0),
6681 XVECEXP (vals, 0, 0));
6682 x = gen_rtx_UNSPEC (VOIDmode,
6683 gen_rtvec (1, const0_rtx), UNSPEC_LVE);
6684 emit_insn (gen_rtx_PARALLEL (VOIDmode,
6685 gen_rtvec (2,
6686 gen_rtx_SET (target, mem),
6687 x)));
6688 x = gen_rtx_VEC_SELECT (inner_mode, target,
6689 gen_rtx_PARALLEL (VOIDmode,
6690 gen_rtvec (1, const0_rtx)));
6691 emit_insn (gen_rtx_SET (target, gen_rtx_VEC_DUPLICATE (mode, x)));
6692 return;
6695 /* One field is non-constant. Load constant then overwrite
6696 varying field. */
6697 if (n_var == 1)
6699 rtx copy = copy_rtx (vals);
6701 /* Load constant part of vector, substitute neighboring value for
6702 varying element. */
6703 XVECEXP (copy, 0, one_var) = XVECEXP (vals, 0, (one_var + 1) % n_elts);
6704 rs6000_expand_vector_init (target, copy);
6706 /* Insert variable. */
6707 rs6000_expand_vector_set (target, XVECEXP (vals, 0, one_var), one_var);
6708 return;
6711 /* Construct the vector in memory one field at a time
6712 and load the whole vector. */
6713 mem = assign_stack_temp (mode, GET_MODE_SIZE (mode));
6714 for (i = 0; i < n_elts; i++)
6715 emit_move_insn (adjust_address_nv (mem, inner_mode,
6716 i * GET_MODE_SIZE (inner_mode)),
6717 XVECEXP (vals, 0, i));
6718 emit_move_insn (target, mem);
6721 /* Set field ELT of TARGET to VAL. */
6723 void
6724 rs6000_expand_vector_set (rtx target, rtx val, int elt)
6726 machine_mode mode = GET_MODE (target);
6727 machine_mode inner_mode = GET_MODE_INNER (mode);
6728 rtx reg = gen_reg_rtx (mode);
6729 rtx mask, mem, x;
6730 int width = GET_MODE_SIZE (inner_mode);
6731 int i;
6733 val = force_reg (GET_MODE (val), val);
6735 if (VECTOR_MEM_VSX_P (mode))
6737 rtx insn = NULL_RTX;
6738 rtx elt_rtx = GEN_INT (elt);
6740 if (mode == V2DFmode)
6741 insn = gen_vsx_set_v2df (target, target, val, elt_rtx);
6743 else if (mode == V2DImode)
6744 insn = gen_vsx_set_v2di (target, target, val, elt_rtx);
6746 else if (TARGET_P9_VECTOR && TARGET_POWERPC64)
6748 if (mode == V4SImode)
6749 insn = gen_vsx_set_v4si_p9 (target, target, val, elt_rtx);
6750 else if (mode == V8HImode)
6751 insn = gen_vsx_set_v8hi_p9 (target, target, val, elt_rtx);
6752 else if (mode == V16QImode)
6753 insn = gen_vsx_set_v16qi_p9 (target, target, val, elt_rtx);
6754 else if (mode == V4SFmode)
6755 insn = gen_vsx_set_v4sf_p9 (target, target, val, elt_rtx);
6758 if (insn)
6760 emit_insn (insn);
6761 return;
6765 /* Simplify setting single element vectors like V1TImode. */
6766 if (GET_MODE_SIZE (mode) == GET_MODE_SIZE (inner_mode) && elt == 0)
6768 emit_move_insn (target, gen_lowpart (mode, val));
6769 return;
6772 /* Load single variable value. */
6773 mem = assign_stack_temp (mode, GET_MODE_SIZE (inner_mode));
6774 emit_move_insn (adjust_address_nv (mem, inner_mode, 0), val);
6775 x = gen_rtx_UNSPEC (VOIDmode,
6776 gen_rtvec (1, const0_rtx), UNSPEC_LVE);
6777 emit_insn (gen_rtx_PARALLEL (VOIDmode,
6778 gen_rtvec (2,
6779 gen_rtx_SET (reg, mem),
6780 x)));
6782 /* Linear sequence. */
6783 mask = gen_rtx_PARALLEL (V16QImode, rtvec_alloc (16));
6784 for (i = 0; i < 16; ++i)
6785 XVECEXP (mask, 0, i) = GEN_INT (i);
6787 /* Set permute mask to insert element into target. */
6788 for (i = 0; i < width; ++i)
6789 XVECEXP (mask, 0, elt*width + i)
6790 = GEN_INT (i + 0x10);
6791 x = gen_rtx_CONST_VECTOR (V16QImode, XVEC (mask, 0));
6793 if (BYTES_BIG_ENDIAN)
6794 x = gen_rtx_UNSPEC (mode,
6795 gen_rtvec (3, target, reg,
6796 force_reg (V16QImode, x)),
6797 UNSPEC_VPERM);
6798 else
6800 if (TARGET_P9_VECTOR)
6801 x = gen_rtx_UNSPEC (mode,
6802 gen_rtvec (3, reg, target,
6803 force_reg (V16QImode, x)),
6804 UNSPEC_VPERMR);
6805 else
6807 /* Invert selector. We prefer to generate VNAND on P8 so
6808 that future fusion opportunities can kick in, but must
6809 generate VNOR elsewhere. */
6810 rtx notx = gen_rtx_NOT (V16QImode, force_reg (V16QImode, x));
6811 rtx iorx = (TARGET_P8_VECTOR
6812 ? gen_rtx_IOR (V16QImode, notx, notx)
6813 : gen_rtx_AND (V16QImode, notx, notx));
6814 rtx tmp = gen_reg_rtx (V16QImode);
6815 emit_insn (gen_rtx_SET (tmp, iorx));
6817 /* Permute with operands reversed and adjusted selector. */
6818 x = gen_rtx_UNSPEC (mode, gen_rtvec (3, reg, target, tmp),
6819 UNSPEC_VPERM);
6823 emit_insn (gen_rtx_SET (target, x));
6826 /* Extract field ELT from VEC into TARGET. */
6828 void
6829 rs6000_expand_vector_extract (rtx target, rtx vec, rtx elt)
6831 machine_mode mode = GET_MODE (vec);
6832 machine_mode inner_mode = GET_MODE_INNER (mode);
6833 rtx mem;
6835 if (VECTOR_MEM_VSX_P (mode) && CONST_INT_P (elt))
6837 switch (mode)
6839 default:
6840 break;
6841 case E_V1TImode:
6842 emit_move_insn (target, gen_lowpart (TImode, vec));
6843 break;
6844 case E_V2DFmode:
6845 emit_insn (gen_vsx_extract_v2df (target, vec, elt));
6846 return;
6847 case E_V2DImode:
6848 emit_insn (gen_vsx_extract_v2di (target, vec, elt));
6849 return;
6850 case E_V4SFmode:
6851 emit_insn (gen_vsx_extract_v4sf (target, vec, elt));
6852 return;
6853 case E_V16QImode:
6854 if (TARGET_DIRECT_MOVE_64BIT)
6856 emit_insn (gen_vsx_extract_v16qi (target, vec, elt));
6857 return;
6859 else
6860 break;
6861 case E_V8HImode:
6862 if (TARGET_DIRECT_MOVE_64BIT)
6864 emit_insn (gen_vsx_extract_v8hi (target, vec, elt));
6865 return;
6867 else
6868 break;
6869 case E_V4SImode:
6870 if (TARGET_DIRECT_MOVE_64BIT)
6872 emit_insn (gen_vsx_extract_v4si (target, vec, elt));
6873 return;
6875 break;
6878 else if (VECTOR_MEM_VSX_P (mode) && !CONST_INT_P (elt)
6879 && TARGET_DIRECT_MOVE_64BIT)
6881 if (GET_MODE (elt) != DImode)
6883 rtx tmp = gen_reg_rtx (DImode);
6884 convert_move (tmp, elt, 0);
6885 elt = tmp;
6887 else if (!REG_P (elt))
6888 elt = force_reg (DImode, elt);
6890 switch (mode)
6892 case E_V1TImode:
6893 emit_move_insn (target, gen_lowpart (TImode, vec));
6894 return;
6896 case E_V2DFmode:
6897 emit_insn (gen_vsx_extract_v2df_var (target, vec, elt));
6898 return;
6900 case E_V2DImode:
6901 emit_insn (gen_vsx_extract_v2di_var (target, vec, elt));
6902 return;
6904 case E_V4SFmode:
6905 emit_insn (gen_vsx_extract_v4sf_var (target, vec, elt));
6906 return;
6908 case E_V4SImode:
6909 emit_insn (gen_vsx_extract_v4si_var (target, vec, elt));
6910 return;
6912 case E_V8HImode:
6913 emit_insn (gen_vsx_extract_v8hi_var (target, vec, elt));
6914 return;
6916 case E_V16QImode:
6917 emit_insn (gen_vsx_extract_v16qi_var (target, vec, elt));
6918 return;
6920 default:
6921 gcc_unreachable ();
6925 /* Allocate mode-sized buffer. */
6926 mem = assign_stack_temp (mode, GET_MODE_SIZE (mode));
6928 emit_move_insn (mem, vec);
6929 if (CONST_INT_P (elt))
6931 int modulo_elt = INTVAL (elt) % GET_MODE_NUNITS (mode);
6933 /* Add offset to field within buffer matching vector element. */
6934 mem = adjust_address_nv (mem, inner_mode,
6935 modulo_elt * GET_MODE_SIZE (inner_mode));
6936 emit_move_insn (target, adjust_address_nv (mem, inner_mode, 0));
6938 else
6940 unsigned int ele_size = GET_MODE_SIZE (inner_mode);
6941 rtx num_ele_m1 = GEN_INT (GET_MODE_NUNITS (mode) - 1);
6942 rtx new_addr = gen_reg_rtx (Pmode);
6944 elt = gen_rtx_AND (Pmode, elt, num_ele_m1);
6945 if (ele_size > 1)
6946 elt = gen_rtx_MULT (Pmode, elt, GEN_INT (ele_size));
6947 new_addr = gen_rtx_PLUS (Pmode, XEXP (mem, 0), elt);
6948 new_addr = change_address (mem, inner_mode, new_addr);
6949 emit_move_insn (target, new_addr);
6953 /* Adjust a memory address (MEM) of a vector type to point to a scalar field
6954 within the vector (ELEMENT) with a mode (SCALAR_MODE). Use a base register
6955 temporary (BASE_TMP) to fixup the address. Return the new memory address
6956 that is valid for reads or writes to a given register (SCALAR_REG). */
6959 rs6000_adjust_vec_address (rtx scalar_reg,
6960 rtx mem,
6961 rtx element,
6962 rtx base_tmp,
6963 machine_mode scalar_mode)
6965 unsigned scalar_size = GET_MODE_SIZE (scalar_mode);
6966 rtx addr = XEXP (mem, 0);
6967 rtx element_offset;
6968 rtx new_addr;
6969 bool valid_addr_p;
6971 /* Vector addresses should not have PRE_INC, PRE_DEC, or PRE_MODIFY. */
6972 gcc_assert (GET_RTX_CLASS (GET_CODE (addr)) != RTX_AUTOINC);
6974 /* Calculate what we need to add to the address to get the element
6975 address. */
6976 if (CONST_INT_P (element))
6977 element_offset = GEN_INT (INTVAL (element) * scalar_size);
6978 else
6980 int byte_shift = exact_log2 (scalar_size);
6981 gcc_assert (byte_shift >= 0);
6983 if (byte_shift == 0)
6984 element_offset = element;
6986 else
6988 if (TARGET_POWERPC64)
6989 emit_insn (gen_ashldi3 (base_tmp, element, GEN_INT (byte_shift)));
6990 else
6991 emit_insn (gen_ashlsi3 (base_tmp, element, GEN_INT (byte_shift)));
6993 element_offset = base_tmp;
6997 /* Create the new address pointing to the element within the vector. If we
6998 are adding 0, we don't have to change the address. */
6999 if (element_offset == const0_rtx)
7000 new_addr = addr;
7002 /* A simple indirect address can be converted into a reg + offset
7003 address. */
7004 else if (REG_P (addr) || SUBREG_P (addr))
7005 new_addr = gen_rtx_PLUS (Pmode, addr, element_offset);
7007 /* Optimize D-FORM addresses with constant offset with a constant element, to
7008 include the element offset in the address directly. */
7009 else if (GET_CODE (addr) == PLUS)
7011 rtx op0 = XEXP (addr, 0);
7012 rtx op1 = XEXP (addr, 1);
7013 rtx insn;
7015 gcc_assert (REG_P (op0) || SUBREG_P (op0));
7016 if (CONST_INT_P (op1) && CONST_INT_P (element_offset))
7018 HOST_WIDE_INT offset = INTVAL (op1) + INTVAL (element_offset);
7019 rtx offset_rtx = GEN_INT (offset);
7021 if (IN_RANGE (offset, -32768, 32767)
7022 && (scalar_size < 8 || (offset & 0x3) == 0))
7023 new_addr = gen_rtx_PLUS (Pmode, op0, offset_rtx);
7024 else
7026 emit_move_insn (base_tmp, offset_rtx);
7027 new_addr = gen_rtx_PLUS (Pmode, op0, base_tmp);
7030 else
7032 bool op1_reg_p = (REG_P (op1) || SUBREG_P (op1));
7033 bool ele_reg_p = (REG_P (element_offset) || SUBREG_P (element_offset));
7035 /* Note, ADDI requires the register being added to be a base
7036 register. If the register was R0, load it up into the temporary
7037 and do the add. */
7038 if (op1_reg_p
7039 && (ele_reg_p || reg_or_subregno (op1) != FIRST_GPR_REGNO))
7041 insn = gen_add3_insn (base_tmp, op1, element_offset);
7042 gcc_assert (insn != NULL_RTX);
7043 emit_insn (insn);
7046 else if (ele_reg_p
7047 && reg_or_subregno (element_offset) != FIRST_GPR_REGNO)
7049 insn = gen_add3_insn (base_tmp, element_offset, op1);
7050 gcc_assert (insn != NULL_RTX);
7051 emit_insn (insn);
7054 else
7056 emit_move_insn (base_tmp, op1);
7057 emit_insn (gen_add2_insn (base_tmp, element_offset));
7060 new_addr = gen_rtx_PLUS (Pmode, op0, base_tmp);
7064 else
7066 emit_move_insn (base_tmp, addr);
7067 new_addr = gen_rtx_PLUS (Pmode, base_tmp, element_offset);
7070 /* If we have a PLUS, we need to see whether the particular register class
7071 allows for D-FORM or X-FORM addressing. */
7072 if (GET_CODE (new_addr) == PLUS)
7074 rtx op1 = XEXP (new_addr, 1);
7075 addr_mask_type addr_mask;
7076 unsigned int scalar_regno = reg_or_subregno (scalar_reg);
7078 gcc_assert (HARD_REGISTER_NUM_P (scalar_regno));
7079 if (INT_REGNO_P (scalar_regno))
7080 addr_mask = reg_addr[scalar_mode].addr_mask[RELOAD_REG_GPR];
7082 else if (FP_REGNO_P (scalar_regno))
7083 addr_mask = reg_addr[scalar_mode].addr_mask[RELOAD_REG_FPR];
7085 else if (ALTIVEC_REGNO_P (scalar_regno))
7086 addr_mask = reg_addr[scalar_mode].addr_mask[RELOAD_REG_VMX];
7088 else
7089 gcc_unreachable ();
7091 if (REG_P (op1) || SUBREG_P (op1))
7092 valid_addr_p = (addr_mask & RELOAD_REG_INDEXED) != 0;
7093 else
7094 valid_addr_p = (addr_mask & RELOAD_REG_OFFSET) != 0;
7097 else if (REG_P (new_addr) || SUBREG_P (new_addr))
7098 valid_addr_p = true;
7100 else
7101 valid_addr_p = false;
7103 if (!valid_addr_p)
7105 emit_move_insn (base_tmp, new_addr);
7106 new_addr = base_tmp;
7109 return change_address (mem, scalar_mode, new_addr);
7112 /* Split a variable vec_extract operation into the component instructions. */
7114 void
7115 rs6000_split_vec_extract_var (rtx dest, rtx src, rtx element, rtx tmp_gpr,
7116 rtx tmp_altivec)
7118 machine_mode mode = GET_MODE (src);
7119 machine_mode scalar_mode = GET_MODE_INNER (GET_MODE (src));
7120 unsigned scalar_size = GET_MODE_SIZE (scalar_mode);
7121 int byte_shift = exact_log2 (scalar_size);
7123 gcc_assert (byte_shift >= 0);
7125 /* If we are given a memory address, optimize to load just the element. We
7126 don't have to adjust the vector element number on little endian
7127 systems. */
7128 if (MEM_P (src))
7130 int num_elements = GET_MODE_NUNITS (mode);
7131 rtx num_ele_m1 = GEN_INT (num_elements - 1);
7133 emit_insn (gen_anddi3 (element, element, num_ele_m1));
7134 gcc_assert (REG_P (tmp_gpr));
7135 emit_move_insn (dest, rs6000_adjust_vec_address (dest, src, element,
7136 tmp_gpr, scalar_mode));
7137 return;
7140 else if (REG_P (src) || SUBREG_P (src))
7142 int num_elements = GET_MODE_NUNITS (mode);
7143 int bits_in_element = mode_to_bits (GET_MODE_INNER (mode));
7144 int bit_shift = 7 - exact_log2 (num_elements);
7145 rtx element2;
7146 unsigned int dest_regno = reg_or_subregno (dest);
7147 unsigned int src_regno = reg_or_subregno (src);
7148 unsigned int element_regno = reg_or_subregno (element);
7150 gcc_assert (REG_P (tmp_gpr));
7152 /* See if we want to generate VEXTU{B,H,W}{L,R}X if the destination is in
7153 a general purpose register. */
7154 if (TARGET_P9_VECTOR
7155 && (mode == V16QImode || mode == V8HImode || mode == V4SImode)
7156 && INT_REGNO_P (dest_regno)
7157 && ALTIVEC_REGNO_P (src_regno)
7158 && INT_REGNO_P (element_regno))
7160 rtx dest_si = gen_rtx_REG (SImode, dest_regno);
7161 rtx element_si = gen_rtx_REG (SImode, element_regno);
7163 if (mode == V16QImode)
7164 emit_insn (BYTES_BIG_ENDIAN
7165 ? gen_vextublx (dest_si, element_si, src)
7166 : gen_vextubrx (dest_si, element_si, src));
7168 else if (mode == V8HImode)
7170 rtx tmp_gpr_si = gen_rtx_REG (SImode, REGNO (tmp_gpr));
7171 emit_insn (gen_ashlsi3 (tmp_gpr_si, element_si, const1_rtx));
7172 emit_insn (BYTES_BIG_ENDIAN
7173 ? gen_vextuhlx (dest_si, tmp_gpr_si, src)
7174 : gen_vextuhrx (dest_si, tmp_gpr_si, src));
7178 else
7180 rtx tmp_gpr_si = gen_rtx_REG (SImode, REGNO (tmp_gpr));
7181 emit_insn (gen_ashlsi3 (tmp_gpr_si, element_si, const2_rtx));
7182 emit_insn (BYTES_BIG_ENDIAN
7183 ? gen_vextuwlx (dest_si, tmp_gpr_si, src)
7184 : gen_vextuwrx (dest_si, tmp_gpr_si, src));
7187 return;
7191 gcc_assert (REG_P (tmp_altivec));
7193 /* For little endian, adjust element ordering. For V2DI/V2DF, we can use
7194 an XOR, otherwise we need to subtract. The shift amount is so VSLO
7195 will shift the element into the upper position (adding 3 to convert a
7196 byte shift into a bit shift). */
7197 if (scalar_size == 8)
7199 if (!BYTES_BIG_ENDIAN)
7201 emit_insn (gen_xordi3 (tmp_gpr, element, const1_rtx));
7202 element2 = tmp_gpr;
7204 else
7205 element2 = element;
7207 /* Generate RLDIC directly to shift left 6 bits and retrieve 1
7208 bit. */
7209 emit_insn (gen_rtx_SET (tmp_gpr,
7210 gen_rtx_AND (DImode,
7211 gen_rtx_ASHIFT (DImode,
7212 element2,
7213 GEN_INT (6)),
7214 GEN_INT (64))));
7216 else
7218 if (!BYTES_BIG_ENDIAN)
7220 rtx num_ele_m1 = GEN_INT (num_elements - 1);
7222 emit_insn (gen_anddi3 (tmp_gpr, element, num_ele_m1));
7223 emit_insn (gen_subdi3 (tmp_gpr, num_ele_m1, tmp_gpr));
7224 element2 = tmp_gpr;
7226 else
7227 element2 = element;
7229 emit_insn (gen_ashldi3 (tmp_gpr, element2, GEN_INT (bit_shift)));
7232 /* Get the value into the lower byte of the Altivec register where VSLO
7233 expects it. */
7234 if (TARGET_P9_VECTOR)
7235 emit_insn (gen_vsx_splat_v2di (tmp_altivec, tmp_gpr));
7236 else if (can_create_pseudo_p ())
7237 emit_insn (gen_vsx_concat_v2di (tmp_altivec, tmp_gpr, tmp_gpr));
7238 else
7240 rtx tmp_di = gen_rtx_REG (DImode, REGNO (tmp_altivec));
7241 emit_move_insn (tmp_di, tmp_gpr);
7242 emit_insn (gen_vsx_concat_v2di (tmp_altivec, tmp_di, tmp_di));
7245 /* Do the VSLO to get the value into the final location. */
7246 switch (mode)
7248 case E_V2DFmode:
7249 emit_insn (gen_vsx_vslo_v2df (dest, src, tmp_altivec));
7250 return;
7252 case E_V2DImode:
7253 emit_insn (gen_vsx_vslo_v2di (dest, src, tmp_altivec));
7254 return;
7256 case E_V4SFmode:
7258 rtx tmp_altivec_di = gen_rtx_REG (DImode, REGNO (tmp_altivec));
7259 rtx tmp_altivec_v4sf = gen_rtx_REG (V4SFmode, REGNO (tmp_altivec));
7260 rtx src_v2di = gen_rtx_REG (V2DImode, REGNO (src));
7261 emit_insn (gen_vsx_vslo_v2di (tmp_altivec_di, src_v2di,
7262 tmp_altivec));
7264 emit_insn (gen_vsx_xscvspdp_scalar2 (dest, tmp_altivec_v4sf));
7265 return;
7268 case E_V4SImode:
7269 case E_V8HImode:
7270 case E_V16QImode:
7272 rtx tmp_altivec_di = gen_rtx_REG (DImode, REGNO (tmp_altivec));
7273 rtx src_v2di = gen_rtx_REG (V2DImode, REGNO (src));
7274 rtx tmp_gpr_di = gen_rtx_REG (DImode, REGNO (dest));
7275 emit_insn (gen_vsx_vslo_v2di (tmp_altivec_di, src_v2di,
7276 tmp_altivec));
7277 emit_move_insn (tmp_gpr_di, tmp_altivec_di);
7278 emit_insn (gen_lshrdi3 (tmp_gpr_di, tmp_gpr_di,
7279 GEN_INT (64 - bits_in_element)));
7280 return;
7283 default:
7284 gcc_unreachable ();
7287 return;
7289 else
7290 gcc_unreachable ();
7293 /* Return alignment of TYPE. Existing alignment is ALIGN. HOW
7294 selects whether the alignment is abi mandated, optional, or
7295 both abi and optional alignment. */
7297 unsigned int
7298 rs6000_data_alignment (tree type, unsigned int align, enum data_align how)
7300 if (how != align_opt)
7302 if (TREE_CODE (type) == VECTOR_TYPE && align < 128)
7303 align = 128;
7306 if (how != align_abi)
7308 if (TREE_CODE (type) == ARRAY_TYPE
7309 && TYPE_MODE (TREE_TYPE (type)) == QImode)
7311 if (align < BITS_PER_WORD)
7312 align = BITS_PER_WORD;
7316 return align;
7319 /* Implement TARGET_SLOW_UNALIGNED_ACCESS. Altivec vector memory
7320 instructions simply ignore the low bits; VSX memory instructions
7321 are aligned to 4 or 8 bytes. */
7323 static bool
7324 rs6000_slow_unaligned_access (machine_mode mode, unsigned int align)
7326 return (STRICT_ALIGNMENT
7327 || (!TARGET_EFFICIENT_UNALIGNED_VSX
7328 && ((SCALAR_FLOAT_MODE_NOT_VECTOR_P (mode) && align < 32)
7329 || ((VECTOR_MODE_P (mode) || FLOAT128_VECTOR_P (mode))
7330 && (int) align < VECTOR_ALIGN (mode)))));
7333 /* Previous GCC releases forced all vector types to have 16-byte alignment. */
7335 bool
7336 rs6000_special_adjust_field_align_p (tree type, unsigned int computed)
7338 if (TARGET_ALTIVEC && TREE_CODE (type) == VECTOR_TYPE)
7340 if (computed != 128)
7342 static bool warned;
7343 if (!warned && warn_psabi)
7345 warned = true;
7346 inform (input_location,
7347 "the layout of aggregates containing vectors with"
7348 " %d-byte alignment has changed in GCC 5",
7349 computed / BITS_PER_UNIT);
7352 /* In current GCC there is no special case. */
7353 return false;
7356 return false;
7359 /* AIX increases natural record alignment to doubleword if the first
7360 field is an FP double while the FP fields remain word aligned. */
7362 unsigned int
7363 rs6000_special_round_type_align (tree type, unsigned int computed,
7364 unsigned int specified)
7366 unsigned int align = MAX (computed, specified);
7367 tree field = TYPE_FIELDS (type);
7369 /* Skip all non field decls */
7370 while (field != NULL && TREE_CODE (field) != FIELD_DECL)
7371 field = DECL_CHAIN (field);
7373 if (field != NULL && field != type)
7375 type = TREE_TYPE (field);
7376 while (TREE_CODE (type) == ARRAY_TYPE)
7377 type = TREE_TYPE (type);
7379 if (type != error_mark_node && TYPE_MODE (type) == DFmode)
7380 align = MAX (align, 64);
7383 return align;
7386 /* Darwin increases record alignment to the natural alignment of
7387 the first field. */
7389 unsigned int
7390 darwin_rs6000_special_round_type_align (tree type, unsigned int computed,
7391 unsigned int specified)
7393 unsigned int align = MAX (computed, specified);
7395 if (TYPE_PACKED (type))
7396 return align;
7398 /* Find the first field, looking down into aggregates. */
7399 do {
7400 tree field = TYPE_FIELDS (type);
7401 /* Skip all non field decls */
7402 while (field != NULL && TREE_CODE (field) != FIELD_DECL)
7403 field = DECL_CHAIN (field);
7404 if (! field)
7405 break;
7406 /* A packed field does not contribute any extra alignment. */
7407 if (DECL_PACKED (field))
7408 return align;
7409 type = TREE_TYPE (field);
7410 while (TREE_CODE (type) == ARRAY_TYPE)
7411 type = TREE_TYPE (type);
7412 } while (AGGREGATE_TYPE_P (type));
7414 if (! AGGREGATE_TYPE_P (type) && type != error_mark_node)
7415 align = MAX (align, TYPE_ALIGN (type));
7417 return align;
7420 /* Return 1 for an operand in small memory on V.4/eabi. */
7423 small_data_operand (rtx op ATTRIBUTE_UNUSED,
7424 machine_mode mode ATTRIBUTE_UNUSED)
7426 #if TARGET_ELF
7427 rtx sym_ref;
7429 if (rs6000_sdata == SDATA_NONE || rs6000_sdata == SDATA_DATA)
7430 return 0;
7432 if (DEFAULT_ABI != ABI_V4)
7433 return 0;
7435 if (SYMBOL_REF_P (op))
7436 sym_ref = op;
7438 else if (GET_CODE (op) != CONST
7439 || GET_CODE (XEXP (op, 0)) != PLUS
7440 || !SYMBOL_REF_P (XEXP (XEXP (op, 0), 0))
7441 || !CONST_INT_P (XEXP (XEXP (op, 0), 1)))
7442 return 0;
7444 else
7446 rtx sum = XEXP (op, 0);
7447 HOST_WIDE_INT summand;
7449 /* We have to be careful here, because it is the referenced address
7450 that must be 32k from _SDA_BASE_, not just the symbol. */
7451 summand = INTVAL (XEXP (sum, 1));
7452 if (summand < 0 || summand > g_switch_value)
7453 return 0;
7455 sym_ref = XEXP (sum, 0);
7458 return SYMBOL_REF_SMALL_P (sym_ref);
7459 #else
7460 return 0;
7461 #endif
7464 /* Return true if either operand is a general purpose register. */
7466 bool
7467 gpr_or_gpr_p (rtx op0, rtx op1)
7469 return ((REG_P (op0) && INT_REGNO_P (REGNO (op0)))
7470 || (REG_P (op1) && INT_REGNO_P (REGNO (op1))));
7473 /* Return true if this is a move direct operation between GPR registers and
7474 floating point/VSX registers. */
7476 bool
7477 direct_move_p (rtx op0, rtx op1)
7479 int regno0, regno1;
7481 if (!REG_P (op0) || !REG_P (op1))
7482 return false;
7484 if (!TARGET_DIRECT_MOVE && !TARGET_MFPGPR)
7485 return false;
7487 regno0 = REGNO (op0);
7488 regno1 = REGNO (op1);
7489 if (!HARD_REGISTER_NUM_P (regno0) || !HARD_REGISTER_NUM_P (regno1))
7490 return false;
7492 if (INT_REGNO_P (regno0))
7493 return (TARGET_DIRECT_MOVE) ? VSX_REGNO_P (regno1) : FP_REGNO_P (regno1);
7495 else if (INT_REGNO_P (regno1))
7497 if (TARGET_MFPGPR && FP_REGNO_P (regno0))
7498 return true;
7500 else if (TARGET_DIRECT_MOVE && VSX_REGNO_P (regno0))
7501 return true;
7504 return false;
7507 /* Return true if the OFFSET is valid for the quad address instructions that
7508 use d-form (register + offset) addressing. */
7510 static inline bool
7511 quad_address_offset_p (HOST_WIDE_INT offset)
7513 return (IN_RANGE (offset, -32768, 32767) && ((offset) & 0xf) == 0);
7516 /* Return true if the ADDR is an acceptable address for a quad memory
7517 operation of mode MODE (either LQ/STQ for general purpose registers, or
7518 LXV/STXV for vector registers under ISA 3.0. GPR_P is true if this address
7519 is intended for LQ/STQ. If it is false, the address is intended for the ISA
7520 3.0 LXV/STXV instruction. */
7522 bool
7523 quad_address_p (rtx addr, machine_mode mode, bool strict)
7525 rtx op0, op1;
7527 if (GET_MODE_SIZE (mode) != 16)
7528 return false;
7530 if (legitimate_indirect_address_p (addr, strict))
7531 return true;
7533 if (VECTOR_MODE_P (mode) && !mode_supports_dq_form (mode))
7534 return false;
7536 if (GET_CODE (addr) != PLUS)
7537 return false;
7539 op0 = XEXP (addr, 0);
7540 if (!REG_P (op0) || !INT_REG_OK_FOR_BASE_P (op0, strict))
7541 return false;
7543 op1 = XEXP (addr, 1);
7544 if (!CONST_INT_P (op1))
7545 return false;
7547 return quad_address_offset_p (INTVAL (op1));
7550 /* Return true if this is a load or store quad operation. This function does
7551 not handle the atomic quad memory instructions. */
7553 bool
7554 quad_load_store_p (rtx op0, rtx op1)
7556 bool ret;
7558 if (!TARGET_QUAD_MEMORY)
7559 ret = false;
7561 else if (REG_P (op0) && MEM_P (op1))
7562 ret = (quad_int_reg_operand (op0, GET_MODE (op0))
7563 && quad_memory_operand (op1, GET_MODE (op1))
7564 && !reg_overlap_mentioned_p (op0, op1));
7566 else if (MEM_P (op0) && REG_P (op1))
7567 ret = (quad_memory_operand (op0, GET_MODE (op0))
7568 && quad_int_reg_operand (op1, GET_MODE (op1)));
7570 else
7571 ret = false;
7573 if (TARGET_DEBUG_ADDR)
7575 fprintf (stderr, "\n========== quad_load_store, return %s\n",
7576 ret ? "true" : "false");
7577 debug_rtx (gen_rtx_SET (op0, op1));
7580 return ret;
7583 /* Given an address, return a constant offset term if one exists. */
7585 static rtx
7586 address_offset (rtx op)
7588 if (GET_CODE (op) == PRE_INC
7589 || GET_CODE (op) == PRE_DEC)
7590 op = XEXP (op, 0);
7591 else if (GET_CODE (op) == PRE_MODIFY
7592 || GET_CODE (op) == LO_SUM)
7593 op = XEXP (op, 1);
7595 if (GET_CODE (op) == CONST)
7596 op = XEXP (op, 0);
7598 if (GET_CODE (op) == PLUS)
7599 op = XEXP (op, 1);
7601 if (CONST_INT_P (op))
7602 return op;
7604 return NULL_RTX;
7607 /* Return true if the MEM operand is a memory operand suitable for use
7608 with a (full width, possibly multiple) gpr load/store. On
7609 powerpc64 this means the offset must be divisible by 4.
7610 Implements 'Y' constraint.
7612 Accept direct, indexed, offset, lo_sum and tocref. Since this is
7613 a constraint function we know the operand has satisfied a suitable
7614 memory predicate.
7616 Offsetting a lo_sum should not be allowed, except where we know by
7617 alignment that a 32k boundary is not crossed. Note that by
7618 "offsetting" here we mean a further offset to access parts of the
7619 MEM. It's fine to have a lo_sum where the inner address is offset
7620 from a sym, since the same sym+offset will appear in the high part
7621 of the address calculation. */
7623 bool
7624 mem_operand_gpr (rtx op, machine_mode mode)
7626 unsigned HOST_WIDE_INT offset;
7627 int extra;
7628 rtx addr = XEXP (op, 0);
7630 /* PR85755: Allow PRE_INC and PRE_DEC addresses. */
7631 if (TARGET_UPDATE
7632 && (GET_CODE (addr) == PRE_INC || GET_CODE (addr) == PRE_DEC)
7633 && mode_supports_pre_incdec_p (mode)
7634 && legitimate_indirect_address_p (XEXP (addr, 0), false))
7635 return true;
7637 /* Don't allow non-offsettable addresses. See PRs 83969 and 84279. */
7638 if (!rs6000_offsettable_memref_p (op, mode, false))
7639 return false;
7641 op = address_offset (addr);
7642 if (op == NULL_RTX)
7643 return true;
7645 offset = INTVAL (op);
7646 if (TARGET_POWERPC64 && (offset & 3) != 0)
7647 return false;
7649 extra = GET_MODE_SIZE (mode) - UNITS_PER_WORD;
7650 if (extra < 0)
7651 extra = 0;
7653 if (GET_CODE (addr) == LO_SUM)
7654 /* For lo_sum addresses, we must allow any offset except one that
7655 causes a wrap, so test only the low 16 bits. */
7656 offset = ((offset & 0xffff) ^ 0x8000) - 0x8000;
7658 return offset + 0x8000 < 0x10000u - extra;
7661 /* As above, but for DS-FORM VSX insns. Unlike mem_operand_gpr,
7662 enforce an offset divisible by 4 even for 32-bit. */
7664 bool
7665 mem_operand_ds_form (rtx op, machine_mode mode)
7667 unsigned HOST_WIDE_INT offset;
7668 int extra;
7669 rtx addr = XEXP (op, 0);
7671 if (!offsettable_address_p (false, mode, addr))
7672 return false;
7674 op = address_offset (addr);
7675 if (op == NULL_RTX)
7676 return true;
7678 offset = INTVAL (op);
7679 if ((offset & 3) != 0)
7680 return false;
7682 extra = GET_MODE_SIZE (mode) - UNITS_PER_WORD;
7683 if (extra < 0)
7684 extra = 0;
7686 if (GET_CODE (addr) == LO_SUM)
7687 /* For lo_sum addresses, we must allow any offset except one that
7688 causes a wrap, so test only the low 16 bits. */
7689 offset = ((offset & 0xffff) ^ 0x8000) - 0x8000;
7691 return offset + 0x8000 < 0x10000u - extra;
7694 /* Subroutines of rs6000_legitimize_address and rs6000_legitimate_address_p. */
7696 static bool
7697 reg_offset_addressing_ok_p (machine_mode mode)
7699 switch (mode)
7701 case E_V16QImode:
7702 case E_V8HImode:
7703 case E_V4SFmode:
7704 case E_V4SImode:
7705 case E_V2DFmode:
7706 case E_V2DImode:
7707 case E_V1TImode:
7708 case E_TImode:
7709 case E_TFmode:
7710 case E_KFmode:
7711 /* AltiVec/VSX vector modes. Only reg+reg addressing was valid until the
7712 ISA 3.0 vector d-form addressing mode was added. While TImode is not
7713 a vector mode, if we want to use the VSX registers to move it around,
7714 we need to restrict ourselves to reg+reg addressing. Similarly for
7715 IEEE 128-bit floating point that is passed in a single vector
7716 register. */
7717 if (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode))
7718 return mode_supports_dq_form (mode);
7719 break;
7721 case E_SDmode:
7722 /* If we can do direct load/stores of SDmode, restrict it to reg+reg
7723 addressing for the LFIWZX and STFIWX instructions. */
7724 if (TARGET_NO_SDMODE_STACK)
7725 return false;
7726 break;
7728 default:
7729 break;
7732 return true;
7735 static bool
7736 virtual_stack_registers_memory_p (rtx op)
7738 int regnum;
7740 if (REG_P (op))
7741 regnum = REGNO (op);
7743 else if (GET_CODE (op) == PLUS
7744 && REG_P (XEXP (op, 0))
7745 && CONST_INT_P (XEXP (op, 1)))
7746 regnum = REGNO (XEXP (op, 0));
7748 else
7749 return false;
7751 return (regnum >= FIRST_VIRTUAL_REGISTER
7752 && regnum <= LAST_VIRTUAL_POINTER_REGISTER);
7755 /* Return true if a MODE sized memory accesses to OP plus OFFSET
7756 is known to not straddle a 32k boundary. This function is used
7757 to determine whether -mcmodel=medium code can use TOC pointer
7758 relative addressing for OP. This means the alignment of the TOC
7759 pointer must also be taken into account, and unfortunately that is
7760 only 8 bytes. */
7762 #ifndef POWERPC64_TOC_POINTER_ALIGNMENT
7763 #define POWERPC64_TOC_POINTER_ALIGNMENT 8
7764 #endif
7766 static bool
7767 offsettable_ok_by_alignment (rtx op, HOST_WIDE_INT offset,
7768 machine_mode mode)
7770 tree decl;
7771 unsigned HOST_WIDE_INT dsize, dalign, lsb, mask;
7773 if (!SYMBOL_REF_P (op))
7774 return false;
7776 /* ISA 3.0 vector d-form addressing is restricted, don't allow
7777 SYMBOL_REF. */
7778 if (mode_supports_dq_form (mode))
7779 return false;
7781 dsize = GET_MODE_SIZE (mode);
7782 decl = SYMBOL_REF_DECL (op);
7783 if (!decl)
7785 if (dsize == 0)
7786 return false;
7788 /* -fsection-anchors loses the original SYMBOL_REF_DECL when
7789 replacing memory addresses with an anchor plus offset. We
7790 could find the decl by rummaging around in the block->objects
7791 VEC for the given offset but that seems like too much work. */
7792 dalign = BITS_PER_UNIT;
7793 if (SYMBOL_REF_HAS_BLOCK_INFO_P (op)
7794 && SYMBOL_REF_ANCHOR_P (op)
7795 && SYMBOL_REF_BLOCK (op) != NULL)
7797 struct object_block *block = SYMBOL_REF_BLOCK (op);
7799 dalign = block->alignment;
7800 offset += SYMBOL_REF_BLOCK_OFFSET (op);
7802 else if (CONSTANT_POOL_ADDRESS_P (op))
7804 /* It would be nice to have get_pool_align().. */
7805 machine_mode cmode = get_pool_mode (op);
7807 dalign = GET_MODE_ALIGNMENT (cmode);
7810 else if (DECL_P (decl))
7812 dalign = DECL_ALIGN (decl);
7814 if (dsize == 0)
7816 /* Allow BLKmode when the entire object is known to not
7817 cross a 32k boundary. */
7818 if (!DECL_SIZE_UNIT (decl))
7819 return false;
7821 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (decl)))
7822 return false;
7824 dsize = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7825 if (dsize > 32768)
7826 return false;
7828 dalign /= BITS_PER_UNIT;
7829 if (dalign > POWERPC64_TOC_POINTER_ALIGNMENT)
7830 dalign = POWERPC64_TOC_POINTER_ALIGNMENT;
7831 return dalign >= dsize;
7834 else
7835 gcc_unreachable ();
7837 /* Find how many bits of the alignment we know for this access. */
7838 dalign /= BITS_PER_UNIT;
7839 if (dalign > POWERPC64_TOC_POINTER_ALIGNMENT)
7840 dalign = POWERPC64_TOC_POINTER_ALIGNMENT;
7841 mask = dalign - 1;
7842 lsb = offset & -offset;
7843 mask &= lsb - 1;
7844 dalign = mask + 1;
7846 return dalign >= dsize;
7849 static bool
7850 constant_pool_expr_p (rtx op)
7852 rtx base, offset;
7854 split_const (op, &base, &offset);
7855 return (SYMBOL_REF_P (base)
7856 && CONSTANT_POOL_ADDRESS_P (base)
7857 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (base), Pmode));
7860 /* These are only used to pass through from print_operand/print_operand_address
7861 to rs6000_output_addr_const_extra over the intervening function
7862 output_addr_const which is not target code. */
7863 static const_rtx tocrel_base_oac, tocrel_offset_oac;
7865 /* Return true if OP is a toc pointer relative address (the output
7866 of create_TOC_reference). If STRICT, do not match non-split
7867 -mcmodel=large/medium toc pointer relative addresses. If the pointers
7868 are non-NULL, place base and offset pieces in TOCREL_BASE_RET and
7869 TOCREL_OFFSET_RET respectively. */
7871 bool
7872 toc_relative_expr_p (const_rtx op, bool strict, const_rtx *tocrel_base_ret,
7873 const_rtx *tocrel_offset_ret)
7875 if (!TARGET_TOC)
7876 return false;
7878 if (TARGET_CMODEL != CMODEL_SMALL)
7880 /* When strict ensure we have everything tidy. */
7881 if (strict
7882 && !(GET_CODE (op) == LO_SUM
7883 && REG_P (XEXP (op, 0))
7884 && INT_REG_OK_FOR_BASE_P (XEXP (op, 0), strict)))
7885 return false;
7887 /* When not strict, allow non-split TOC addresses and also allow
7888 (lo_sum (high ..)) TOC addresses created during reload. */
7889 if (GET_CODE (op) == LO_SUM)
7890 op = XEXP (op, 1);
7893 const_rtx tocrel_base = op;
7894 const_rtx tocrel_offset = const0_rtx;
7896 if (GET_CODE (op) == PLUS && add_cint_operand (XEXP (op, 1), GET_MODE (op)))
7898 tocrel_base = XEXP (op, 0);
7899 tocrel_offset = XEXP (op, 1);
7902 if (tocrel_base_ret)
7903 *tocrel_base_ret = tocrel_base;
7904 if (tocrel_offset_ret)
7905 *tocrel_offset_ret = tocrel_offset;
7907 return (GET_CODE (tocrel_base) == UNSPEC
7908 && XINT (tocrel_base, 1) == UNSPEC_TOCREL
7909 && REG_P (XVECEXP (tocrel_base, 0, 1))
7910 && REGNO (XVECEXP (tocrel_base, 0, 1)) == TOC_REGISTER);
7913 /* Return true if X is a constant pool address, and also for cmodel=medium
7914 if X is a toc-relative address known to be offsettable within MODE. */
7916 bool
7917 legitimate_constant_pool_address_p (const_rtx x, machine_mode mode,
7918 bool strict)
7920 const_rtx tocrel_base, tocrel_offset;
7921 return (toc_relative_expr_p (x, strict, &tocrel_base, &tocrel_offset)
7922 && (TARGET_CMODEL != CMODEL_MEDIUM
7923 || constant_pool_expr_p (XVECEXP (tocrel_base, 0, 0))
7924 || mode == QImode
7925 || offsettable_ok_by_alignment (XVECEXP (tocrel_base, 0, 0),
7926 INTVAL (tocrel_offset), mode)));
7929 static bool
7930 legitimate_small_data_p (machine_mode mode, rtx x)
7932 return (DEFAULT_ABI == ABI_V4
7933 && !flag_pic && !TARGET_TOC
7934 && (SYMBOL_REF_P (x) || GET_CODE (x) == CONST)
7935 && small_data_operand (x, mode));
7938 bool
7939 rs6000_legitimate_offset_address_p (machine_mode mode, rtx x,
7940 bool strict, bool worst_case)
7942 unsigned HOST_WIDE_INT offset;
7943 unsigned int extra;
7945 if (GET_CODE (x) != PLUS)
7946 return false;
7947 if (!REG_P (XEXP (x, 0)))
7948 return false;
7949 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
7950 return false;
7951 if (mode_supports_dq_form (mode))
7952 return quad_address_p (x, mode, strict);
7953 if (!reg_offset_addressing_ok_p (mode))
7954 return virtual_stack_registers_memory_p (x);
7955 if (legitimate_constant_pool_address_p (x, mode, strict || lra_in_progress))
7956 return true;
7957 if (!CONST_INT_P (XEXP (x, 1)))
7958 return false;
7960 offset = INTVAL (XEXP (x, 1));
7961 extra = 0;
7962 switch (mode)
7964 case E_DFmode:
7965 case E_DDmode:
7966 case E_DImode:
7967 /* If we are using VSX scalar loads, restrict ourselves to reg+reg
7968 addressing. */
7969 if (VECTOR_MEM_VSX_P (mode))
7970 return false;
7972 if (!worst_case)
7973 break;
7974 if (!TARGET_POWERPC64)
7975 extra = 4;
7976 else if (offset & 3)
7977 return false;
7978 break;
7980 case E_TFmode:
7981 case E_IFmode:
7982 case E_KFmode:
7983 case E_TDmode:
7984 case E_TImode:
7985 case E_PTImode:
7986 extra = 8;
7987 if (!worst_case)
7988 break;
7989 if (!TARGET_POWERPC64)
7990 extra = 12;
7991 else if (offset & 3)
7992 return false;
7993 break;
7995 default:
7996 break;
7999 offset += 0x8000;
8000 return offset < 0x10000 - extra;
8003 bool
8004 legitimate_indexed_address_p (rtx x, int strict)
8006 rtx op0, op1;
8008 if (GET_CODE (x) != PLUS)
8009 return false;
8011 op0 = XEXP (x, 0);
8012 op1 = XEXP (x, 1);
8014 return (REG_P (op0) && REG_P (op1)
8015 && ((INT_REG_OK_FOR_BASE_P (op0, strict)
8016 && INT_REG_OK_FOR_INDEX_P (op1, strict))
8017 || (INT_REG_OK_FOR_BASE_P (op1, strict)
8018 && INT_REG_OK_FOR_INDEX_P (op0, strict))));
8021 bool
8022 avoiding_indexed_address_p (machine_mode mode)
8024 /* Avoid indexed addressing for modes that have non-indexed
8025 load/store instruction forms. */
8026 return (TARGET_AVOID_XFORM && VECTOR_MEM_NONE_P (mode));
8029 bool
8030 legitimate_indirect_address_p (rtx x, int strict)
8032 return REG_P (x) && INT_REG_OK_FOR_BASE_P (x, strict);
8035 bool
8036 macho_lo_sum_memory_operand (rtx x, machine_mode mode)
8038 if (!TARGET_MACHO || !flag_pic
8039 || mode != SImode || !MEM_P (x))
8040 return false;
8041 x = XEXP (x, 0);
8043 if (GET_CODE (x) != LO_SUM)
8044 return false;
8045 if (!REG_P (XEXP (x, 0)))
8046 return false;
8047 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), 0))
8048 return false;
8049 x = XEXP (x, 1);
8051 return CONSTANT_P (x);
8054 static bool
8055 legitimate_lo_sum_address_p (machine_mode mode, rtx x, int strict)
8057 if (GET_CODE (x) != LO_SUM)
8058 return false;
8059 if (!REG_P (XEXP (x, 0)))
8060 return false;
8061 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
8062 return false;
8063 /* quad word addresses are restricted, and we can't use LO_SUM. */
8064 if (mode_supports_dq_form (mode))
8065 return false;
8066 x = XEXP (x, 1);
8068 if (TARGET_ELF || TARGET_MACHO)
8070 bool large_toc_ok;
8072 if (DEFAULT_ABI == ABI_V4 && flag_pic)
8073 return false;
8074 /* LRA doesn't use LEGITIMIZE_RELOAD_ADDRESS as it usually calls
8075 push_reload from reload pass code. LEGITIMIZE_RELOAD_ADDRESS
8076 recognizes some LO_SUM addresses as valid although this
8077 function says opposite. In most cases, LRA through different
8078 transformations can generate correct code for address reloads.
8079 It cannot manage only some LO_SUM cases. So we need to add
8080 code here saying that some addresses are still valid. */
8081 large_toc_ok = (lra_in_progress && TARGET_CMODEL != CMODEL_SMALL
8082 && small_toc_ref (x, VOIDmode));
8083 if (TARGET_TOC && ! large_toc_ok)
8084 return false;
8085 if (GET_MODE_NUNITS (mode) != 1)
8086 return false;
8087 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
8088 && !(/* ??? Assume floating point reg based on mode? */
8089 TARGET_HARD_FLOAT && (mode == DFmode || mode == DDmode)))
8090 return false;
8092 return CONSTANT_P (x) || large_toc_ok;
8095 return false;
8099 /* Try machine-dependent ways of modifying an illegitimate address
8100 to be legitimate. If we find one, return the new, valid address.
8101 This is used from only one place: `memory_address' in explow.c.
8103 OLDX is the address as it was before break_out_memory_refs was
8104 called. In some cases it is useful to look at this to decide what
8105 needs to be done.
8107 It is always safe for this function to do nothing. It exists to
8108 recognize opportunities to optimize the output.
8110 On RS/6000, first check for the sum of a register with a constant
8111 integer that is out of range. If so, generate code to add the
8112 constant with the low-order 16 bits masked to the register and force
8113 this result into another register (this can be done with `cau').
8114 Then generate an address of REG+(CONST&0xffff), allowing for the
8115 possibility of bit 16 being a one.
8117 Then check for the sum of a register and something not constant, try to
8118 load the other things into a register and return the sum. */
8120 static rtx
8121 rs6000_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
8122 machine_mode mode)
8124 unsigned int extra;
8126 if (!reg_offset_addressing_ok_p (mode)
8127 || mode_supports_dq_form (mode))
8129 if (virtual_stack_registers_memory_p (x))
8130 return x;
8132 /* In theory we should not be seeing addresses of the form reg+0,
8133 but just in case it is generated, optimize it away. */
8134 if (GET_CODE (x) == PLUS && XEXP (x, 1) == const0_rtx)
8135 return force_reg (Pmode, XEXP (x, 0));
8137 /* For TImode with load/store quad, restrict addresses to just a single
8138 pointer, so it works with both GPRs and VSX registers. */
8139 /* Make sure both operands are registers. */
8140 else if (GET_CODE (x) == PLUS
8141 && (mode != TImode || !TARGET_VSX))
8142 return gen_rtx_PLUS (Pmode,
8143 force_reg (Pmode, XEXP (x, 0)),
8144 force_reg (Pmode, XEXP (x, 1)));
8145 else
8146 return force_reg (Pmode, x);
8148 if (SYMBOL_REF_P (x))
8150 enum tls_model model = SYMBOL_REF_TLS_MODEL (x);
8151 if (model != 0)
8152 return rs6000_legitimize_tls_address (x, model);
8155 extra = 0;
8156 switch (mode)
8158 case E_TFmode:
8159 case E_TDmode:
8160 case E_TImode:
8161 case E_PTImode:
8162 case E_IFmode:
8163 case E_KFmode:
8164 /* As in legitimate_offset_address_p we do not assume
8165 worst-case. The mode here is just a hint as to the registers
8166 used. A TImode is usually in gprs, but may actually be in
8167 fprs. Leave worst-case scenario for reload to handle via
8168 insn constraints. PTImode is only GPRs. */
8169 extra = 8;
8170 break;
8171 default:
8172 break;
8175 if (GET_CODE (x) == PLUS
8176 && REG_P (XEXP (x, 0))
8177 && CONST_INT_P (XEXP (x, 1))
8178 && ((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 1)) + 0x8000)
8179 >= 0x10000 - extra))
8181 HOST_WIDE_INT high_int, low_int;
8182 rtx sum;
8183 low_int = ((INTVAL (XEXP (x, 1)) & 0xffff) ^ 0x8000) - 0x8000;
8184 if (low_int >= 0x8000 - extra)
8185 low_int = 0;
8186 high_int = INTVAL (XEXP (x, 1)) - low_int;
8187 sum = force_operand (gen_rtx_PLUS (Pmode, XEXP (x, 0),
8188 GEN_INT (high_int)), 0);
8189 return plus_constant (Pmode, sum, low_int);
8191 else if (GET_CODE (x) == PLUS
8192 && REG_P (XEXP (x, 0))
8193 && !CONST_INT_P (XEXP (x, 1))
8194 && GET_MODE_NUNITS (mode) == 1
8195 && (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
8196 || (/* ??? Assume floating point reg based on mode? */
8197 TARGET_HARD_FLOAT && (mode == DFmode || mode == DDmode)))
8198 && !avoiding_indexed_address_p (mode))
8200 return gen_rtx_PLUS (Pmode, XEXP (x, 0),
8201 force_reg (Pmode, force_operand (XEXP (x, 1), 0)));
8203 else if ((TARGET_ELF
8204 #if TARGET_MACHO
8205 || !MACHO_DYNAMIC_NO_PIC_P
8206 #endif
8208 && TARGET_32BIT
8209 && TARGET_NO_TOC
8210 && !flag_pic
8211 && !CONST_INT_P (x)
8212 && !CONST_WIDE_INT_P (x)
8213 && !CONST_DOUBLE_P (x)
8214 && CONSTANT_P (x)
8215 && GET_MODE_NUNITS (mode) == 1
8216 && (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
8217 || (/* ??? Assume floating point reg based on mode? */
8218 TARGET_HARD_FLOAT && (mode == DFmode || mode == DDmode))))
8220 rtx reg = gen_reg_rtx (Pmode);
8221 if (TARGET_ELF)
8222 emit_insn (gen_elf_high (reg, x));
8223 else
8224 emit_insn (gen_macho_high (reg, x));
8225 return gen_rtx_LO_SUM (Pmode, reg, x);
8227 else if (TARGET_TOC
8228 && SYMBOL_REF_P (x)
8229 && constant_pool_expr_p (x)
8230 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (x), Pmode))
8231 return create_TOC_reference (x, NULL_RTX);
8232 else
8233 return x;
8236 /* Debug version of rs6000_legitimize_address. */
8237 static rtx
8238 rs6000_debug_legitimize_address (rtx x, rtx oldx, machine_mode mode)
8240 rtx ret;
8241 rtx_insn *insns;
8243 start_sequence ();
8244 ret = rs6000_legitimize_address (x, oldx, mode);
8245 insns = get_insns ();
8246 end_sequence ();
8248 if (ret != x)
8250 fprintf (stderr,
8251 "\nrs6000_legitimize_address: mode %s, old code %s, "
8252 "new code %s, modified\n",
8253 GET_MODE_NAME (mode), GET_RTX_NAME (GET_CODE (x)),
8254 GET_RTX_NAME (GET_CODE (ret)));
8256 fprintf (stderr, "Original address:\n");
8257 debug_rtx (x);
8259 fprintf (stderr, "oldx:\n");
8260 debug_rtx (oldx);
8262 fprintf (stderr, "New address:\n");
8263 debug_rtx (ret);
8265 if (insns)
8267 fprintf (stderr, "Insns added:\n");
8268 debug_rtx_list (insns, 20);
8271 else
8273 fprintf (stderr,
8274 "\nrs6000_legitimize_address: mode %s, code %s, no change:\n",
8275 GET_MODE_NAME (mode), GET_RTX_NAME (GET_CODE (x)));
8277 debug_rtx (x);
8280 if (insns)
8281 emit_insn (insns);
8283 return ret;
8286 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
8287 We need to emit DTP-relative relocations. */
8289 static void rs6000_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
8290 static void
8291 rs6000_output_dwarf_dtprel (FILE *file, int size, rtx x)
8293 switch (size)
8295 case 4:
8296 fputs ("\t.long\t", file);
8297 break;
8298 case 8:
8299 fputs (DOUBLE_INT_ASM_OP, file);
8300 break;
8301 default:
8302 gcc_unreachable ();
8304 output_addr_const (file, x);
8305 if (TARGET_ELF)
8306 fputs ("@dtprel+0x8000", file);
8307 else if (TARGET_XCOFF && SYMBOL_REF_P (x))
8309 switch (SYMBOL_REF_TLS_MODEL (x))
8311 case 0:
8312 break;
8313 case TLS_MODEL_LOCAL_EXEC:
8314 fputs ("@le", file);
8315 break;
8316 case TLS_MODEL_INITIAL_EXEC:
8317 fputs ("@ie", file);
8318 break;
8319 case TLS_MODEL_GLOBAL_DYNAMIC:
8320 case TLS_MODEL_LOCAL_DYNAMIC:
8321 fputs ("@m", file);
8322 break;
8323 default:
8324 gcc_unreachable ();
8329 /* Return true if X is a symbol that refers to real (rather than emulated)
8330 TLS. */
8332 static bool
8333 rs6000_real_tls_symbol_ref_p (rtx x)
8335 return (SYMBOL_REF_P (x)
8336 && SYMBOL_REF_TLS_MODEL (x) >= TLS_MODEL_REAL);
8339 /* In the name of slightly smaller debug output, and to cater to
8340 general assembler lossage, recognize various UNSPEC sequences
8341 and turn them back into a direct symbol reference. */
8343 static rtx
8344 rs6000_delegitimize_address (rtx orig_x)
8346 rtx x, y, offset;
8348 if (GET_CODE (orig_x) == UNSPEC && XINT (orig_x, 1) == UNSPEC_FUSION_GPR)
8349 orig_x = XVECEXP (orig_x, 0, 0);
8351 orig_x = delegitimize_mem_from_attrs (orig_x);
8353 x = orig_x;
8354 if (MEM_P (x))
8355 x = XEXP (x, 0);
8357 y = x;
8358 if (TARGET_CMODEL != CMODEL_SMALL && GET_CODE (y) == LO_SUM)
8359 y = XEXP (y, 1);
8361 offset = NULL_RTX;
8362 if (GET_CODE (y) == PLUS
8363 && GET_MODE (y) == Pmode
8364 && CONST_INT_P (XEXP (y, 1)))
8366 offset = XEXP (y, 1);
8367 y = XEXP (y, 0);
8370 if (GET_CODE (y) == UNSPEC && XINT (y, 1) == UNSPEC_TOCREL)
8372 y = XVECEXP (y, 0, 0);
8374 #ifdef HAVE_AS_TLS
8375 /* Do not associate thread-local symbols with the original
8376 constant pool symbol. */
8377 if (TARGET_XCOFF
8378 && SYMBOL_REF_P (y)
8379 && CONSTANT_POOL_ADDRESS_P (y)
8380 && rs6000_real_tls_symbol_ref_p (get_pool_constant (y)))
8381 return orig_x;
8382 #endif
8384 if (offset != NULL_RTX)
8385 y = gen_rtx_PLUS (Pmode, y, offset);
8386 if (!MEM_P (orig_x))
8387 return y;
8388 else
8389 return replace_equiv_address_nv (orig_x, y);
8392 if (TARGET_MACHO
8393 && GET_CODE (orig_x) == LO_SUM
8394 && GET_CODE (XEXP (orig_x, 1)) == CONST)
8396 y = XEXP (XEXP (orig_x, 1), 0);
8397 if (GET_CODE (y) == UNSPEC && XINT (y, 1) == UNSPEC_MACHOPIC_OFFSET)
8398 return XVECEXP (y, 0, 0);
8401 return orig_x;
8404 /* Return true if X shouldn't be emitted into the debug info.
8405 The linker doesn't like .toc section references from
8406 .debug_* sections, so reject .toc section symbols. */
8408 static bool
8409 rs6000_const_not_ok_for_debug_p (rtx x)
8411 if (GET_CODE (x) == UNSPEC)
8412 return true;
8413 if (SYMBOL_REF_P (x)
8414 && CONSTANT_POOL_ADDRESS_P (x))
8416 rtx c = get_pool_constant (x);
8417 machine_mode cmode = get_pool_mode (x);
8418 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (c, cmode))
8419 return true;
8422 return false;
8425 /* Implement the TARGET_LEGITIMATE_COMBINED_INSN hook. */
8427 static bool
8428 rs6000_legitimate_combined_insn (rtx_insn *insn)
8430 int icode = INSN_CODE (insn);
8432 /* Reject creating doloop insns. Combine should not be allowed
8433 to create these for a number of reasons:
8434 1) In a nested loop, if combine creates one of these in an
8435 outer loop and the register allocator happens to allocate ctr
8436 to the outer loop insn, then the inner loop can't use ctr.
8437 Inner loops ought to be more highly optimized.
8438 2) Combine often wants to create one of these from what was
8439 originally a three insn sequence, first combining the three
8440 insns to two, then to ctrsi/ctrdi. When ctrsi/ctrdi is not
8441 allocated ctr, the splitter takes use back to the three insn
8442 sequence. It's better to stop combine at the two insn
8443 sequence.
8444 3) Faced with not being able to allocate ctr for ctrsi/crtdi
8445 insns, the register allocator sometimes uses floating point
8446 or vector registers for the pseudo. Since ctrsi/ctrdi is a
8447 jump insn and output reloads are not implemented for jumps,
8448 the ctrsi/ctrdi splitters need to handle all possible cases.
8449 That's a pain, and it gets to be seriously difficult when a
8450 splitter that runs after reload needs memory to transfer from
8451 a gpr to fpr. See PR70098 and PR71763 which are not fixed
8452 for the difficult case. It's better to not create problems
8453 in the first place. */
8454 if (icode != CODE_FOR_nothing
8455 && (icode == CODE_FOR_bdz_si
8456 || icode == CODE_FOR_bdz_di
8457 || icode == CODE_FOR_bdnz_si
8458 || icode == CODE_FOR_bdnz_di
8459 || icode == CODE_FOR_bdztf_si
8460 || icode == CODE_FOR_bdztf_di
8461 || icode == CODE_FOR_bdnztf_si
8462 || icode == CODE_FOR_bdnztf_di))
8463 return false;
8465 return true;
8468 /* Construct the SYMBOL_REF for the tls_get_addr function. */
8470 static GTY(()) rtx rs6000_tls_symbol;
8471 static rtx
8472 rs6000_tls_get_addr (void)
8474 if (!rs6000_tls_symbol)
8475 rs6000_tls_symbol = init_one_libfunc ("__tls_get_addr");
8477 return rs6000_tls_symbol;
8480 /* Construct the SYMBOL_REF for TLS GOT references. */
8482 static GTY(()) rtx rs6000_got_symbol;
8483 static rtx
8484 rs6000_got_sym (void)
8486 if (!rs6000_got_symbol)
8488 rs6000_got_symbol = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
8489 SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_LOCAL;
8490 SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_EXTERNAL;
8493 return rs6000_got_symbol;
8496 /* AIX Thread-Local Address support. */
8498 static rtx
8499 rs6000_legitimize_tls_address_aix (rtx addr, enum tls_model model)
8501 rtx sym, mem, tocref, tlsreg, tmpreg, dest, tlsaddr;
8502 const char *name;
8503 char *tlsname;
8505 name = XSTR (addr, 0);
8506 /* Append TLS CSECT qualifier, unless the symbol already is qualified
8507 or the symbol will be in TLS private data section. */
8508 if (name[strlen (name) - 1] != ']'
8509 && (TREE_PUBLIC (SYMBOL_REF_DECL (addr))
8510 || bss_initializer_p (SYMBOL_REF_DECL (addr))))
8512 tlsname = XALLOCAVEC (char, strlen (name) + 4);
8513 strcpy (tlsname, name);
8514 strcat (tlsname,
8515 bss_initializer_p (SYMBOL_REF_DECL (addr)) ? "[UL]" : "[TL]");
8516 tlsaddr = copy_rtx (addr);
8517 XSTR (tlsaddr, 0) = ggc_strdup (tlsname);
8519 else
8520 tlsaddr = addr;
8522 /* Place addr into TOC constant pool. */
8523 sym = force_const_mem (GET_MODE (tlsaddr), tlsaddr);
8525 /* Output the TOC entry and create the MEM referencing the value. */
8526 if (constant_pool_expr_p (XEXP (sym, 0))
8527 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (XEXP (sym, 0)), Pmode))
8529 tocref = create_TOC_reference (XEXP (sym, 0), NULL_RTX);
8530 mem = gen_const_mem (Pmode, tocref);
8531 set_mem_alias_set (mem, get_TOC_alias_set ());
8533 else
8534 return sym;
8536 /* Use global-dynamic for local-dynamic. */
8537 if (model == TLS_MODEL_GLOBAL_DYNAMIC
8538 || model == TLS_MODEL_LOCAL_DYNAMIC)
8540 /* Create new TOC reference for @m symbol. */
8541 name = XSTR (XVECEXP (XEXP (mem, 0), 0, 0), 0);
8542 tlsname = XALLOCAVEC (char, strlen (name) + 1);
8543 strcpy (tlsname, "*LCM");
8544 strcat (tlsname, name + 3);
8545 rtx modaddr = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (tlsname));
8546 SYMBOL_REF_FLAGS (modaddr) |= SYMBOL_FLAG_LOCAL;
8547 tocref = create_TOC_reference (modaddr, NULL_RTX);
8548 rtx modmem = gen_const_mem (Pmode, tocref);
8549 set_mem_alias_set (modmem, get_TOC_alias_set ());
8551 rtx modreg = gen_reg_rtx (Pmode);
8552 emit_insn (gen_rtx_SET (modreg, modmem));
8554 tmpreg = gen_reg_rtx (Pmode);
8555 emit_insn (gen_rtx_SET (tmpreg, mem));
8557 dest = gen_reg_rtx (Pmode);
8558 if (TARGET_32BIT)
8559 emit_insn (gen_tls_get_addrsi (dest, modreg, tmpreg));
8560 else
8561 emit_insn (gen_tls_get_addrdi (dest, modreg, tmpreg));
8562 return dest;
8564 /* Obtain TLS pointer: 32 bit call or 64 bit GPR 13. */
8565 else if (TARGET_32BIT)
8567 tlsreg = gen_reg_rtx (SImode);
8568 emit_insn (gen_tls_get_tpointer (tlsreg));
8570 else
8571 tlsreg = gen_rtx_REG (DImode, 13);
8573 /* Load the TOC value into temporary register. */
8574 tmpreg = gen_reg_rtx (Pmode);
8575 emit_insn (gen_rtx_SET (tmpreg, mem));
8576 set_unique_reg_note (get_last_insn (), REG_EQUAL,
8577 gen_rtx_MINUS (Pmode, addr, tlsreg));
8579 /* Add TOC symbol value to TLS pointer. */
8580 dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, tmpreg, tlsreg));
8582 return dest;
8585 /* Output arg setup instructions for a !TARGET_TLS_MARKERS
8586 __tls_get_addr call. */
8588 void
8589 rs6000_output_tlsargs (rtx *operands)
8591 /* Set up operands for output_asm_insn, without modifying OPERANDS. */
8592 rtx op[3];
8594 /* The set dest of the call, ie. r3, which is also the first arg reg. */
8595 op[0] = operands[0];
8596 /* The TLS symbol from global_tlsarg stashed as CALL operand 2. */
8597 op[1] = XVECEXP (operands[2], 0, 0);
8598 if (XINT (operands[2], 1) == UNSPEC_TLSGD)
8600 /* The GOT register. */
8601 op[2] = XVECEXP (operands[2], 0, 1);
8602 if (TARGET_CMODEL != CMODEL_SMALL)
8603 output_asm_insn ("addis %0,%2,%1@got@tlsgd@ha\n\t"
8604 "addi %0,%0,%1@got@tlsgd@l", op);
8605 else
8606 output_asm_insn ("addi %0,%2,%1@got@tlsgd", op);
8608 else if (XINT (operands[2], 1) == UNSPEC_TLSLD)
8610 if (TARGET_CMODEL != CMODEL_SMALL)
8611 output_asm_insn ("addis %0,%1,%&@got@tlsld@ha\n\t"
8612 "addi %0,%0,%&@got@tlsld@l", op);
8613 else
8614 output_asm_insn ("addi %0,%1,%&@got@tlsld", op);
8616 else
8617 gcc_unreachable ();
8620 /* Passes the tls arg value for global dynamic and local dynamic
8621 emit_library_call_value in rs6000_legitimize_tls_address to
8622 rs6000_call_aix and rs6000_call_sysv. This is used to emit the
8623 marker relocs put on __tls_get_addr calls. */
8624 static rtx global_tlsarg;
8626 /* ADDR contains a thread-local SYMBOL_REF. Generate code to compute
8627 this (thread-local) address. */
8629 static rtx
8630 rs6000_legitimize_tls_address (rtx addr, enum tls_model model)
8632 rtx dest, insn;
8634 if (TARGET_XCOFF)
8635 return rs6000_legitimize_tls_address_aix (addr, model);
8637 dest = gen_reg_rtx (Pmode);
8638 if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 16)
8640 rtx tlsreg;
8642 if (TARGET_64BIT)
8644 tlsreg = gen_rtx_REG (Pmode, 13);
8645 insn = gen_tls_tprel_64 (dest, tlsreg, addr);
8647 else
8649 tlsreg = gen_rtx_REG (Pmode, 2);
8650 insn = gen_tls_tprel_32 (dest, tlsreg, addr);
8652 emit_insn (insn);
8654 else if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 32)
8656 rtx tlsreg, tmp;
8658 tmp = gen_reg_rtx (Pmode);
8659 if (TARGET_64BIT)
8661 tlsreg = gen_rtx_REG (Pmode, 13);
8662 insn = gen_tls_tprel_ha_64 (tmp, tlsreg, addr);
8664 else
8666 tlsreg = gen_rtx_REG (Pmode, 2);
8667 insn = gen_tls_tprel_ha_32 (tmp, tlsreg, addr);
8669 emit_insn (insn);
8670 if (TARGET_64BIT)
8671 insn = gen_tls_tprel_lo_64 (dest, tmp, addr);
8672 else
8673 insn = gen_tls_tprel_lo_32 (dest, tmp, addr);
8674 emit_insn (insn);
8676 else
8678 rtx got, tga, tmp1, tmp2;
8680 /* We currently use relocations like @got@tlsgd for tls, which
8681 means the linker will handle allocation of tls entries, placing
8682 them in the .got section. So use a pointer to the .got section,
8683 not one to secondary TOC sections used by 64-bit -mminimal-toc,
8684 or to secondary GOT sections used by 32-bit -fPIC. */
8685 if (TARGET_64BIT)
8686 got = gen_rtx_REG (Pmode, 2);
8687 else
8689 if (flag_pic == 1)
8690 got = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
8691 else
8693 rtx gsym = rs6000_got_sym ();
8694 got = gen_reg_rtx (Pmode);
8695 if (flag_pic == 0)
8696 rs6000_emit_move (got, gsym, Pmode);
8697 else
8699 rtx mem, lab;
8701 tmp1 = gen_reg_rtx (Pmode);
8702 tmp2 = gen_reg_rtx (Pmode);
8703 mem = gen_const_mem (Pmode, tmp1);
8704 lab = gen_label_rtx ();
8705 emit_insn (gen_load_toc_v4_PIC_1b (gsym, lab));
8706 emit_move_insn (tmp1, gen_rtx_REG (Pmode, LR_REGNO));
8707 if (TARGET_LINK_STACK)
8708 emit_insn (gen_addsi3 (tmp1, tmp1, GEN_INT (4)));
8709 emit_move_insn (tmp2, mem);
8710 rtx_insn *last = emit_insn (gen_addsi3 (got, tmp1, tmp2));
8711 set_unique_reg_note (last, REG_EQUAL, gsym);
8716 if (model == TLS_MODEL_GLOBAL_DYNAMIC)
8718 rtx arg = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, addr, got),
8719 UNSPEC_TLSGD);
8720 tga = rs6000_tls_get_addr ();
8721 global_tlsarg = arg;
8722 if (TARGET_TLS_MARKERS)
8724 rtx argreg = gen_rtx_REG (Pmode, 3);
8725 emit_insn (gen_rtx_SET (argreg, arg));
8726 emit_library_call_value (tga, dest, LCT_CONST, Pmode,
8727 argreg, Pmode);
8729 else
8730 emit_library_call_value (tga, dest, LCT_CONST, Pmode);
8731 global_tlsarg = NULL_RTX;
8733 /* Make a note so that the result of this call can be CSEd. */
8734 rtvec vec = gen_rtvec (1, copy_rtx (arg));
8735 rtx uns = gen_rtx_UNSPEC (Pmode, vec, UNSPEC_TLS_GET_ADDR);
8736 set_unique_reg_note (get_last_insn (), REG_EQUAL, uns);
8738 else if (model == TLS_MODEL_LOCAL_DYNAMIC)
8740 rtx arg = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, got), UNSPEC_TLSLD);
8741 tga = rs6000_tls_get_addr ();
8742 tmp1 = gen_reg_rtx (Pmode);
8743 global_tlsarg = arg;
8744 if (TARGET_TLS_MARKERS)
8746 rtx argreg = gen_rtx_REG (Pmode, 3);
8747 emit_insn (gen_rtx_SET (argreg, arg));
8748 emit_library_call_value (tga, tmp1, LCT_CONST, Pmode,
8749 argreg, Pmode);
8751 else
8752 emit_library_call_value (tga, tmp1, LCT_CONST, Pmode);
8753 global_tlsarg = NULL_RTX;
8755 /* Make a note so that the result of this call can be CSEd. */
8756 rtvec vec = gen_rtvec (1, copy_rtx (arg));
8757 rtx uns = gen_rtx_UNSPEC (Pmode, vec, UNSPEC_TLS_GET_ADDR);
8758 set_unique_reg_note (get_last_insn (), REG_EQUAL, uns);
8760 if (rs6000_tls_size == 16)
8762 if (TARGET_64BIT)
8763 insn = gen_tls_dtprel_64 (dest, tmp1, addr);
8764 else
8765 insn = gen_tls_dtprel_32 (dest, tmp1, addr);
8767 else if (rs6000_tls_size == 32)
8769 tmp2 = gen_reg_rtx (Pmode);
8770 if (TARGET_64BIT)
8771 insn = gen_tls_dtprel_ha_64 (tmp2, tmp1, addr);
8772 else
8773 insn = gen_tls_dtprel_ha_32 (tmp2, tmp1, addr);
8774 emit_insn (insn);
8775 if (TARGET_64BIT)
8776 insn = gen_tls_dtprel_lo_64 (dest, tmp2, addr);
8777 else
8778 insn = gen_tls_dtprel_lo_32 (dest, tmp2, addr);
8780 else
8782 tmp2 = gen_reg_rtx (Pmode);
8783 if (TARGET_64BIT)
8784 insn = gen_tls_got_dtprel_64 (tmp2, got, addr);
8785 else
8786 insn = gen_tls_got_dtprel_32 (tmp2, got, addr);
8787 emit_insn (insn);
8788 insn = gen_rtx_SET (dest, gen_rtx_PLUS (Pmode, tmp2, tmp1));
8790 emit_insn (insn);
8792 else
8794 /* IE, or 64-bit offset LE. */
8795 tmp2 = gen_reg_rtx (Pmode);
8796 if (TARGET_64BIT)
8797 insn = gen_tls_got_tprel_64 (tmp2, got, addr);
8798 else
8799 insn = gen_tls_got_tprel_32 (tmp2, got, addr);
8800 emit_insn (insn);
8801 if (TARGET_64BIT)
8802 insn = gen_tls_tls_64 (dest, tmp2, addr);
8803 else
8804 insn = gen_tls_tls_32 (dest, tmp2, addr);
8805 emit_insn (insn);
8809 return dest;
8812 /* Only create the global variable for the stack protect guard if we are using
8813 the global flavor of that guard. */
8814 static tree
8815 rs6000_init_stack_protect_guard (void)
8817 if (rs6000_stack_protector_guard == SSP_GLOBAL)
8818 return default_stack_protect_guard ();
8820 return NULL_TREE;
8823 /* Implement TARGET_CANNOT_FORCE_CONST_MEM. */
8825 static bool
8826 rs6000_cannot_force_const_mem (machine_mode mode ATTRIBUTE_UNUSED, rtx x)
8828 if (GET_CODE (x) == HIGH
8829 && GET_CODE (XEXP (x, 0)) == UNSPEC)
8830 return true;
8832 /* A TLS symbol in the TOC cannot contain a sum. */
8833 if (GET_CODE (x) == CONST
8834 && GET_CODE (XEXP (x, 0)) == PLUS
8835 && SYMBOL_REF_P (XEXP (XEXP (x, 0), 0))
8836 && SYMBOL_REF_TLS_MODEL (XEXP (XEXP (x, 0), 0)) != 0)
8837 return true;
8839 /* Do not place an ELF TLS symbol in the constant pool. */
8840 return TARGET_ELF && tls_referenced_p (x);
8843 /* Return true iff the given SYMBOL_REF refers to a constant pool entry
8844 that we have put in the TOC, or for cmodel=medium, if the SYMBOL_REF
8845 can be addressed relative to the toc pointer. */
8847 static bool
8848 use_toc_relative_ref (rtx sym, machine_mode mode)
8850 return ((constant_pool_expr_p (sym)
8851 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (sym),
8852 get_pool_mode (sym)))
8853 || (TARGET_CMODEL == CMODEL_MEDIUM
8854 && SYMBOL_REF_LOCAL_P (sym)
8855 && GET_MODE_SIZE (mode) <= POWERPC64_TOC_POINTER_ALIGNMENT));
8858 /* TARGET_LEGITIMATE_ADDRESS_P recognizes an RTL expression
8859 that is a valid memory address for an instruction.
8860 The MODE argument is the machine mode for the MEM expression
8861 that wants to use this address.
8863 On the RS/6000, there are four valid address: a SYMBOL_REF that
8864 refers to a constant pool entry of an address (or the sum of it
8865 plus a constant), a short (16-bit signed) constant plus a register,
8866 the sum of two registers, or a register indirect, possibly with an
8867 auto-increment. For DFmode, DDmode and DImode with a constant plus
8868 register, we must ensure that both words are addressable or PowerPC64
8869 with offset word aligned.
8871 For modes spanning multiple registers (DFmode and DDmode in 32-bit GPRs,
8872 32-bit DImode, TImode, TFmode, TDmode), indexed addressing cannot be used
8873 because adjacent memory cells are accessed by adding word-sized offsets
8874 during assembly output. */
8875 static bool
8876 rs6000_legitimate_address_p (machine_mode mode, rtx x, bool reg_ok_strict)
8878 bool reg_offset_p = reg_offset_addressing_ok_p (mode);
8879 bool quad_offset_p = mode_supports_dq_form (mode);
8881 /* If this is an unaligned stvx/ldvx type address, discard the outer AND. */
8882 if (VECTOR_MEM_ALTIVEC_P (mode)
8883 && GET_CODE (x) == AND
8884 && CONST_INT_P (XEXP (x, 1))
8885 && INTVAL (XEXP (x, 1)) == -16)
8886 x = XEXP (x, 0);
8888 if (TARGET_ELF && RS6000_SYMBOL_REF_TLS_P (x))
8889 return 0;
8890 if (legitimate_indirect_address_p (x, reg_ok_strict))
8891 return 1;
8892 if (TARGET_UPDATE
8893 && (GET_CODE (x) == PRE_INC || GET_CODE (x) == PRE_DEC)
8894 && mode_supports_pre_incdec_p (mode)
8895 && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict))
8896 return 1;
8897 /* Handle restricted vector d-form offsets in ISA 3.0. */
8898 if (quad_offset_p)
8900 if (quad_address_p (x, mode, reg_ok_strict))
8901 return 1;
8903 else if (virtual_stack_registers_memory_p (x))
8904 return 1;
8906 else if (reg_offset_p)
8908 if (legitimate_small_data_p (mode, x))
8909 return 1;
8910 if (legitimate_constant_pool_address_p (x, mode,
8911 reg_ok_strict || lra_in_progress))
8912 return 1;
8915 /* For TImode, if we have TImode in VSX registers, only allow register
8916 indirect addresses. This will allow the values to go in either GPRs
8917 or VSX registers without reloading. The vector types would tend to
8918 go into VSX registers, so we allow REG+REG, while TImode seems
8919 somewhat split, in that some uses are GPR based, and some VSX based. */
8920 /* FIXME: We could loosen this by changing the following to
8921 if (mode == TImode && TARGET_QUAD_MEMORY && TARGET_VSX)
8922 but currently we cannot allow REG+REG addressing for TImode. See
8923 PR72827 for complete details on how this ends up hoodwinking DSE. */
8924 if (mode == TImode && TARGET_VSX)
8925 return 0;
8926 /* If not REG_OK_STRICT (before reload) let pass any stack offset. */
8927 if (! reg_ok_strict
8928 && reg_offset_p
8929 && GET_CODE (x) == PLUS
8930 && REG_P (XEXP (x, 0))
8931 && (XEXP (x, 0) == virtual_stack_vars_rtx
8932 || XEXP (x, 0) == arg_pointer_rtx)
8933 && CONST_INT_P (XEXP (x, 1)))
8934 return 1;
8935 if (rs6000_legitimate_offset_address_p (mode, x, reg_ok_strict, false))
8936 return 1;
8937 if (!FLOAT128_2REG_P (mode)
8938 && (TARGET_HARD_FLOAT
8939 || TARGET_POWERPC64
8940 || (mode != DFmode && mode != DDmode))
8941 && (TARGET_POWERPC64 || mode != DImode)
8942 && (mode != TImode || VECTOR_MEM_VSX_P (TImode))
8943 && mode != PTImode
8944 && !avoiding_indexed_address_p (mode)
8945 && legitimate_indexed_address_p (x, reg_ok_strict))
8946 return 1;
8947 if (TARGET_UPDATE && GET_CODE (x) == PRE_MODIFY
8948 && mode_supports_pre_modify_p (mode)
8949 && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict)
8950 && (rs6000_legitimate_offset_address_p (mode, XEXP (x, 1),
8951 reg_ok_strict, false)
8952 || (!avoiding_indexed_address_p (mode)
8953 && legitimate_indexed_address_p (XEXP (x, 1), reg_ok_strict)))
8954 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
8955 return 1;
8956 if (reg_offset_p && !quad_offset_p
8957 && legitimate_lo_sum_address_p (mode, x, reg_ok_strict))
8958 return 1;
8959 return 0;
8962 /* Debug version of rs6000_legitimate_address_p. */
8963 static bool
8964 rs6000_debug_legitimate_address_p (machine_mode mode, rtx x,
8965 bool reg_ok_strict)
8967 bool ret = rs6000_legitimate_address_p (mode, x, reg_ok_strict);
8968 fprintf (stderr,
8969 "\nrs6000_legitimate_address_p: return = %s, mode = %s, "
8970 "strict = %d, reload = %s, code = %s\n",
8971 ret ? "true" : "false",
8972 GET_MODE_NAME (mode),
8973 reg_ok_strict,
8974 (reload_completed ? "after" : "before"),
8975 GET_RTX_NAME (GET_CODE (x)));
8976 debug_rtx (x);
8978 return ret;
8981 /* Implement TARGET_MODE_DEPENDENT_ADDRESS_P. */
8983 static bool
8984 rs6000_mode_dependent_address_p (const_rtx addr,
8985 addr_space_t as ATTRIBUTE_UNUSED)
8987 return rs6000_mode_dependent_address_ptr (addr);
8990 /* Go to LABEL if ADDR (a legitimate address expression)
8991 has an effect that depends on the machine mode it is used for.
8993 On the RS/6000 this is true of all integral offsets (since AltiVec
8994 and VSX modes don't allow them) or is a pre-increment or decrement.
8996 ??? Except that due to conceptual problems in offsettable_address_p
8997 we can't really report the problems of integral offsets. So leave
8998 this assuming that the adjustable offset must be valid for the
8999 sub-words of a TFmode operand, which is what we had before. */
9001 static bool
9002 rs6000_mode_dependent_address (const_rtx addr)
9004 switch (GET_CODE (addr))
9006 case PLUS:
9007 /* Any offset from virtual_stack_vars_rtx and arg_pointer_rtx
9008 is considered a legitimate address before reload, so there
9009 are no offset restrictions in that case. Note that this
9010 condition is safe in strict mode because any address involving
9011 virtual_stack_vars_rtx or arg_pointer_rtx would already have
9012 been rejected as illegitimate. */
9013 if (XEXP (addr, 0) != virtual_stack_vars_rtx
9014 && XEXP (addr, 0) != arg_pointer_rtx
9015 && CONST_INT_P (XEXP (addr, 1)))
9017 unsigned HOST_WIDE_INT val = INTVAL (XEXP (addr, 1));
9018 return val + 0x8000 >= 0x10000 - (TARGET_POWERPC64 ? 8 : 12);
9020 break;
9022 case LO_SUM:
9023 /* Anything in the constant pool is sufficiently aligned that
9024 all bytes have the same high part address. */
9025 return !legitimate_constant_pool_address_p (addr, QImode, false);
9027 /* Auto-increment cases are now treated generically in recog.c. */
9028 case PRE_MODIFY:
9029 return TARGET_UPDATE;
9031 /* AND is only allowed in Altivec loads. */
9032 case AND:
9033 return true;
9035 default:
9036 break;
9039 return false;
9042 /* Debug version of rs6000_mode_dependent_address. */
9043 static bool
9044 rs6000_debug_mode_dependent_address (const_rtx addr)
9046 bool ret = rs6000_mode_dependent_address (addr);
9048 fprintf (stderr, "\nrs6000_mode_dependent_address: ret = %s\n",
9049 ret ? "true" : "false");
9050 debug_rtx (addr);
9052 return ret;
9055 /* Implement FIND_BASE_TERM. */
9058 rs6000_find_base_term (rtx op)
9060 rtx base;
9062 base = op;
9063 if (GET_CODE (base) == CONST)
9064 base = XEXP (base, 0);
9065 if (GET_CODE (base) == PLUS)
9066 base = XEXP (base, 0);
9067 if (GET_CODE (base) == UNSPEC)
9068 switch (XINT (base, 1))
9070 case UNSPEC_TOCREL:
9071 case UNSPEC_MACHOPIC_OFFSET:
9072 /* OP represents SYM [+ OFFSET] - ANCHOR. SYM is the base term
9073 for aliasing purposes. */
9074 return XVECEXP (base, 0, 0);
9077 return op;
9080 /* More elaborate version of recog's offsettable_memref_p predicate
9081 that works around the ??? note of rs6000_mode_dependent_address.
9082 In particular it accepts
9084 (mem:DI (plus:SI (reg/f:SI 31 31) (const_int 32760 [0x7ff8])))
9086 in 32-bit mode, that the recog predicate rejects. */
9088 static bool
9089 rs6000_offsettable_memref_p (rtx op, machine_mode reg_mode, bool strict)
9091 bool worst_case;
9093 if (!MEM_P (op))
9094 return false;
9096 /* First mimic offsettable_memref_p. */
9097 if (offsettable_address_p (strict, GET_MODE (op), XEXP (op, 0)))
9098 return true;
9100 /* offsettable_address_p invokes rs6000_mode_dependent_address, but
9101 the latter predicate knows nothing about the mode of the memory
9102 reference and, therefore, assumes that it is the largest supported
9103 mode (TFmode). As a consequence, legitimate offsettable memory
9104 references are rejected. rs6000_legitimate_offset_address_p contains
9105 the correct logic for the PLUS case of rs6000_mode_dependent_address,
9106 at least with a little bit of help here given that we know the
9107 actual registers used. */
9108 worst_case = ((TARGET_POWERPC64 && GET_MODE_CLASS (reg_mode) == MODE_INT)
9109 || GET_MODE_SIZE (reg_mode) == 4);
9110 return rs6000_legitimate_offset_address_p (GET_MODE (op), XEXP (op, 0),
9111 strict, worst_case);
9114 /* Determine the reassociation width to be used in reassociate_bb.
9115 This takes into account how many parallel operations we
9116 can actually do of a given type, and also the latency.
9118 int add/sub 6/cycle
9119 mul 2/cycle
9120 vect add/sub/mul 2/cycle
9121 fp add/sub/mul 2/cycle
9122 dfp 1/cycle
9125 static int
9126 rs6000_reassociation_width (unsigned int opc ATTRIBUTE_UNUSED,
9127 machine_mode mode)
9129 switch (rs6000_tune)
9131 case PROCESSOR_POWER8:
9132 case PROCESSOR_POWER9:
9133 if (DECIMAL_FLOAT_MODE_P (mode))
9134 return 1;
9135 if (VECTOR_MODE_P (mode))
9136 return 4;
9137 if (INTEGRAL_MODE_P (mode))
9138 return 1;
9139 if (FLOAT_MODE_P (mode))
9140 return 4;
9141 break;
9142 default:
9143 break;
9145 return 1;
9148 /* Change register usage conditional on target flags. */
9149 static void
9150 rs6000_conditional_register_usage (void)
9152 int i;
9154 if (TARGET_DEBUG_TARGET)
9155 fprintf (stderr, "rs6000_conditional_register_usage called\n");
9157 /* 64-bit AIX and Linux reserve GPR13 for thread-private data. */
9158 if (TARGET_64BIT)
9159 fixed_regs[13] = call_used_regs[13]
9160 = call_really_used_regs[13] = 1;
9162 /* Conditionally disable FPRs. */
9163 if (TARGET_SOFT_FLOAT)
9164 for (i = 32; i < 64; i++)
9165 fixed_regs[i] = call_used_regs[i]
9166 = call_really_used_regs[i] = 1;
9168 /* The TOC register is not killed across calls in a way that is
9169 visible to the compiler. */
9170 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
9171 call_really_used_regs[2] = 0;
9173 if (DEFAULT_ABI == ABI_V4 && flag_pic == 2)
9174 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
9176 if (DEFAULT_ABI == ABI_V4 && flag_pic == 1)
9177 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
9178 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
9179 = call_really_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
9181 if (DEFAULT_ABI == ABI_DARWIN && flag_pic)
9182 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
9183 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
9184 = call_really_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
9186 if (TARGET_TOC && TARGET_MINIMAL_TOC)
9187 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
9188 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
9190 if (!TARGET_ALTIVEC && !TARGET_VSX)
9192 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
9193 fixed_regs[i] = call_used_regs[i] = call_really_used_regs[i] = 1;
9194 call_really_used_regs[VRSAVE_REGNO] = 1;
9197 if (TARGET_ALTIVEC || TARGET_VSX)
9198 global_regs[VSCR_REGNO] = 1;
9200 if (TARGET_ALTIVEC_ABI)
9202 for (i = FIRST_ALTIVEC_REGNO; i < FIRST_ALTIVEC_REGNO + 20; ++i)
9203 call_used_regs[i] = call_really_used_regs[i] = 1;
9205 /* AIX reserves VR20:31 in non-extended ABI mode. */
9206 if (TARGET_XCOFF)
9207 for (i = FIRST_ALTIVEC_REGNO + 20; i < FIRST_ALTIVEC_REGNO + 32; ++i)
9208 fixed_regs[i] = call_used_regs[i] = call_really_used_regs[i] = 1;
9213 /* Output insns to set DEST equal to the constant SOURCE as a series of
9214 lis, ori and shl instructions and return TRUE. */
9216 bool
9217 rs6000_emit_set_const (rtx dest, rtx source)
9219 machine_mode mode = GET_MODE (dest);
9220 rtx temp, set;
9221 rtx_insn *insn;
9222 HOST_WIDE_INT c;
9224 gcc_checking_assert (CONST_INT_P (source));
9225 c = INTVAL (source);
9226 switch (mode)
9228 case E_QImode:
9229 case E_HImode:
9230 emit_insn (gen_rtx_SET (dest, source));
9231 return true;
9233 case E_SImode:
9234 temp = !can_create_pseudo_p () ? dest : gen_reg_rtx (SImode);
9236 emit_insn (gen_rtx_SET (copy_rtx (temp),
9237 GEN_INT (c & ~(HOST_WIDE_INT) 0xffff)));
9238 emit_insn (gen_rtx_SET (dest,
9239 gen_rtx_IOR (SImode, copy_rtx (temp),
9240 GEN_INT (c & 0xffff))));
9241 break;
9243 case E_DImode:
9244 if (!TARGET_POWERPC64)
9246 rtx hi, lo;
9248 hi = operand_subword_force (copy_rtx (dest), WORDS_BIG_ENDIAN == 0,
9249 DImode);
9250 lo = operand_subword_force (dest, WORDS_BIG_ENDIAN != 0,
9251 DImode);
9252 emit_move_insn (hi, GEN_INT (c >> 32));
9253 c = ((c & 0xffffffff) ^ 0x80000000) - 0x80000000;
9254 emit_move_insn (lo, GEN_INT (c));
9256 else
9257 rs6000_emit_set_long_const (dest, c);
9258 break;
9260 default:
9261 gcc_unreachable ();
9264 insn = get_last_insn ();
9265 set = single_set (insn);
9266 if (! CONSTANT_P (SET_SRC (set)))
9267 set_unique_reg_note (insn, REG_EQUAL, GEN_INT (c));
9269 return true;
9272 /* Subroutine of rs6000_emit_set_const, handling PowerPC64 DImode.
9273 Output insns to set DEST equal to the constant C as a series of
9274 lis, ori and shl instructions. */
9276 static void
9277 rs6000_emit_set_long_const (rtx dest, HOST_WIDE_INT c)
9279 rtx temp;
9280 HOST_WIDE_INT ud1, ud2, ud3, ud4;
9282 ud1 = c & 0xffff;
9283 c = c >> 16;
9284 ud2 = c & 0xffff;
9285 c = c >> 16;
9286 ud3 = c & 0xffff;
9287 c = c >> 16;
9288 ud4 = c & 0xffff;
9290 if ((ud4 == 0xffff && ud3 == 0xffff && ud2 == 0xffff && (ud1 & 0x8000))
9291 || (ud4 == 0 && ud3 == 0 && ud2 == 0 && ! (ud1 & 0x8000)))
9292 emit_move_insn (dest, GEN_INT ((ud1 ^ 0x8000) - 0x8000));
9294 else if ((ud4 == 0xffff && ud3 == 0xffff && (ud2 & 0x8000))
9295 || (ud4 == 0 && ud3 == 0 && ! (ud2 & 0x8000)))
9297 temp = !can_create_pseudo_p () ? dest : gen_reg_rtx (DImode);
9299 emit_move_insn (ud1 != 0 ? copy_rtx (temp) : dest,
9300 GEN_INT (((ud2 << 16) ^ 0x80000000) - 0x80000000));
9301 if (ud1 != 0)
9302 emit_move_insn (dest,
9303 gen_rtx_IOR (DImode, copy_rtx (temp),
9304 GEN_INT (ud1)));
9306 else if (ud3 == 0 && ud4 == 0)
9308 temp = !can_create_pseudo_p () ? dest : gen_reg_rtx (DImode);
9310 gcc_assert (ud2 & 0x8000);
9311 emit_move_insn (copy_rtx (temp),
9312 GEN_INT (((ud2 << 16) ^ 0x80000000) - 0x80000000));
9313 if (ud1 != 0)
9314 emit_move_insn (copy_rtx (temp),
9315 gen_rtx_IOR (DImode, copy_rtx (temp),
9316 GEN_INT (ud1)));
9317 emit_move_insn (dest,
9318 gen_rtx_ZERO_EXTEND (DImode,
9319 gen_lowpart (SImode,
9320 copy_rtx (temp))));
9322 else if ((ud4 == 0xffff && (ud3 & 0x8000))
9323 || (ud4 == 0 && ! (ud3 & 0x8000)))
9325 temp = !can_create_pseudo_p () ? dest : gen_reg_rtx (DImode);
9327 emit_move_insn (copy_rtx (temp),
9328 GEN_INT (((ud3 << 16) ^ 0x80000000) - 0x80000000));
9329 if (ud2 != 0)
9330 emit_move_insn (copy_rtx (temp),
9331 gen_rtx_IOR (DImode, copy_rtx (temp),
9332 GEN_INT (ud2)));
9333 emit_move_insn (ud1 != 0 ? copy_rtx (temp) : dest,
9334 gen_rtx_ASHIFT (DImode, copy_rtx (temp),
9335 GEN_INT (16)));
9336 if (ud1 != 0)
9337 emit_move_insn (dest,
9338 gen_rtx_IOR (DImode, copy_rtx (temp),
9339 GEN_INT (ud1)));
9341 else
9343 temp = !can_create_pseudo_p () ? dest : gen_reg_rtx (DImode);
9345 emit_move_insn (copy_rtx (temp),
9346 GEN_INT (((ud4 << 16) ^ 0x80000000) - 0x80000000));
9347 if (ud3 != 0)
9348 emit_move_insn (copy_rtx (temp),
9349 gen_rtx_IOR (DImode, copy_rtx (temp),
9350 GEN_INT (ud3)));
9352 emit_move_insn (ud2 != 0 || ud1 != 0 ? copy_rtx (temp) : dest,
9353 gen_rtx_ASHIFT (DImode, copy_rtx (temp),
9354 GEN_INT (32)));
9355 if (ud2 != 0)
9356 emit_move_insn (ud1 != 0 ? copy_rtx (temp) : dest,
9357 gen_rtx_IOR (DImode, copy_rtx (temp),
9358 GEN_INT (ud2 << 16)));
9359 if (ud1 != 0)
9360 emit_move_insn (dest,
9361 gen_rtx_IOR (DImode, copy_rtx (temp),
9362 GEN_INT (ud1)));
9366 /* Helper for the following. Get rid of [r+r] memory refs
9367 in cases where it won't work (TImode, TFmode, TDmode, PTImode). */
9369 static void
9370 rs6000_eliminate_indexed_memrefs (rtx operands[2])
9372 if (MEM_P (operands[0])
9373 && !REG_P (XEXP (operands[0], 0))
9374 && ! legitimate_constant_pool_address_p (XEXP (operands[0], 0),
9375 GET_MODE (operands[0]), false))
9376 operands[0]
9377 = replace_equiv_address (operands[0],
9378 copy_addr_to_reg (XEXP (operands[0], 0)));
9380 if (MEM_P (operands[1])
9381 && !REG_P (XEXP (operands[1], 0))
9382 && ! legitimate_constant_pool_address_p (XEXP (operands[1], 0),
9383 GET_MODE (operands[1]), false))
9384 operands[1]
9385 = replace_equiv_address (operands[1],
9386 copy_addr_to_reg (XEXP (operands[1], 0)));
9389 /* Generate a vector of constants to permute MODE for a little-endian
9390 storage operation by swapping the two halves of a vector. */
9391 static rtvec
9392 rs6000_const_vec (machine_mode mode)
9394 int i, subparts;
9395 rtvec v;
9397 switch (mode)
9399 case E_V1TImode:
9400 subparts = 1;
9401 break;
9402 case E_V2DFmode:
9403 case E_V2DImode:
9404 subparts = 2;
9405 break;
9406 case E_V4SFmode:
9407 case E_V4SImode:
9408 subparts = 4;
9409 break;
9410 case E_V8HImode:
9411 subparts = 8;
9412 break;
9413 case E_V16QImode:
9414 subparts = 16;
9415 break;
9416 default:
9417 gcc_unreachable();
9420 v = rtvec_alloc (subparts);
9422 for (i = 0; i < subparts / 2; ++i)
9423 RTVEC_ELT (v, i) = gen_rtx_CONST_INT (DImode, i + subparts / 2);
9424 for (i = subparts / 2; i < subparts; ++i)
9425 RTVEC_ELT (v, i) = gen_rtx_CONST_INT (DImode, i - subparts / 2);
9427 return v;
9430 /* Emit an lxvd2x, stxvd2x, or xxpermdi instruction for a VSX load or
9431 store operation. */
9432 void
9433 rs6000_emit_le_vsx_permute (rtx dest, rtx source, machine_mode mode)
9435 /* Scalar permutations are easier to express in integer modes rather than
9436 floating-point modes, so cast them here. We use V1TImode instead
9437 of TImode to ensure that the values don't go through GPRs. */
9438 if (FLOAT128_VECTOR_P (mode))
9440 dest = gen_lowpart (V1TImode, dest);
9441 source = gen_lowpart (V1TImode, source);
9442 mode = V1TImode;
9445 /* Use ROTATE instead of VEC_SELECT if the mode contains only a single
9446 scalar. */
9447 if (mode == TImode || mode == V1TImode)
9448 emit_insn (gen_rtx_SET (dest, gen_rtx_ROTATE (mode, source,
9449 GEN_INT (64))));
9450 else
9452 rtx par = gen_rtx_PARALLEL (VOIDmode, rs6000_const_vec (mode));
9453 emit_insn (gen_rtx_SET (dest, gen_rtx_VEC_SELECT (mode, source, par)));
9457 /* Emit a little-endian load from vector memory location SOURCE to VSX
9458 register DEST in mode MODE. The load is done with two permuting
9459 insn's that represent an lxvd2x and xxpermdi. */
9460 void
9461 rs6000_emit_le_vsx_load (rtx dest, rtx source, machine_mode mode)
9463 /* Use V2DImode to do swaps of types with 128-bit scalare parts (TImode,
9464 V1TImode). */
9465 if (mode == TImode || mode == V1TImode)
9467 mode = V2DImode;
9468 dest = gen_lowpart (V2DImode, dest);
9469 source = adjust_address (source, V2DImode, 0);
9472 rtx tmp = can_create_pseudo_p () ? gen_reg_rtx_and_attrs (dest) : dest;
9473 rs6000_emit_le_vsx_permute (tmp, source, mode);
9474 rs6000_emit_le_vsx_permute (dest, tmp, mode);
9477 /* Emit a little-endian store to vector memory location DEST from VSX
9478 register SOURCE in mode MODE. The store is done with two permuting
9479 insn's that represent an xxpermdi and an stxvd2x. */
9480 void
9481 rs6000_emit_le_vsx_store (rtx dest, rtx source, machine_mode mode)
9483 /* This should never be called during or after LRA, because it does
9484 not re-permute the source register. It is intended only for use
9485 during expand. */
9486 gcc_assert (!lra_in_progress && !reload_completed);
9488 /* Use V2DImode to do swaps of types with 128-bit scalar parts (TImode,
9489 V1TImode). */
9490 if (mode == TImode || mode == V1TImode)
9492 mode = V2DImode;
9493 dest = adjust_address (dest, V2DImode, 0);
9494 source = gen_lowpart (V2DImode, source);
9497 rtx tmp = can_create_pseudo_p () ? gen_reg_rtx_and_attrs (source) : source;
9498 rs6000_emit_le_vsx_permute (tmp, source, mode);
9499 rs6000_emit_le_vsx_permute (dest, tmp, mode);
9502 /* Emit a sequence representing a little-endian VSX load or store,
9503 moving data from SOURCE to DEST in mode MODE. This is done
9504 separately from rs6000_emit_move to ensure it is called only
9505 during expand. LE VSX loads and stores introduced later are
9506 handled with a split. The expand-time RTL generation allows
9507 us to optimize away redundant pairs of register-permutes. */
9508 void
9509 rs6000_emit_le_vsx_move (rtx dest, rtx source, machine_mode mode)
9511 gcc_assert (!BYTES_BIG_ENDIAN
9512 && VECTOR_MEM_VSX_P (mode)
9513 && !TARGET_P9_VECTOR
9514 && !gpr_or_gpr_p (dest, source)
9515 && (MEM_P (source) ^ MEM_P (dest)));
9517 if (MEM_P (source))
9519 gcc_assert (REG_P (dest) || SUBREG_P (dest));
9520 rs6000_emit_le_vsx_load (dest, source, mode);
9522 else
9524 if (!REG_P (source))
9525 source = force_reg (mode, source);
9526 rs6000_emit_le_vsx_store (dest, source, mode);
9530 /* Return whether a SFmode or SImode move can be done without converting one
9531 mode to another. This arrises when we have:
9533 (SUBREG:SF (REG:SI ...))
9534 (SUBREG:SI (REG:SF ...))
9536 and one of the values is in a floating point/vector register, where SFmode
9537 scalars are stored in DFmode format. */
9539 bool
9540 valid_sf_si_move (rtx dest, rtx src, machine_mode mode)
9542 if (TARGET_ALLOW_SF_SUBREG)
9543 return true;
9545 if (mode != SFmode && GET_MODE_CLASS (mode) != MODE_INT)
9546 return true;
9548 if (!SUBREG_P (src) || !sf_subreg_operand (src, mode))
9549 return true;
9551 /*. Allow (set (SUBREG:SI (REG:SF)) (SUBREG:SI (REG:SF))). */
9552 if (SUBREG_P (dest))
9554 rtx dest_subreg = SUBREG_REG (dest);
9555 rtx src_subreg = SUBREG_REG (src);
9556 return GET_MODE (dest_subreg) == GET_MODE (src_subreg);
9559 return false;
9563 /* Helper function to change moves with:
9565 (SUBREG:SF (REG:SI)) and
9566 (SUBREG:SI (REG:SF))
9568 into separate UNSPEC insns. In the PowerPC architecture, scalar SFmode
9569 values are stored as DFmode values in the VSX registers. We need to convert
9570 the bits before we can use a direct move or operate on the bits in the
9571 vector register as an integer type.
9573 Skip things like (set (SUBREG:SI (...) (SUBREG:SI (...)). */
9575 static bool
9576 rs6000_emit_move_si_sf_subreg (rtx dest, rtx source, machine_mode mode)
9578 if (TARGET_DIRECT_MOVE_64BIT && !reload_completed
9579 && (!SUBREG_P (dest) || !sf_subreg_operand (dest, mode))
9580 && SUBREG_P (source) && sf_subreg_operand (source, mode))
9582 rtx inner_source = SUBREG_REG (source);
9583 machine_mode inner_mode = GET_MODE (inner_source);
9585 if (mode == SImode && inner_mode == SFmode)
9587 emit_insn (gen_movsi_from_sf (dest, inner_source));
9588 return true;
9591 if (mode == SFmode && inner_mode == SImode)
9593 emit_insn (gen_movsf_from_si (dest, inner_source));
9594 return true;
9598 return false;
9601 /* Emit a move from SOURCE to DEST in mode MODE. */
9602 void
9603 rs6000_emit_move (rtx dest, rtx source, machine_mode mode)
9605 rtx operands[2];
9606 operands[0] = dest;
9607 operands[1] = source;
9609 if (TARGET_DEBUG_ADDR)
9611 fprintf (stderr,
9612 "\nrs6000_emit_move: mode = %s, lra_in_progress = %d, "
9613 "reload_completed = %d, can_create_pseudos = %d.\ndest:\n",
9614 GET_MODE_NAME (mode),
9615 lra_in_progress,
9616 reload_completed,
9617 can_create_pseudo_p ());
9618 debug_rtx (dest);
9619 fprintf (stderr, "source:\n");
9620 debug_rtx (source);
9623 /* Check that we get CONST_WIDE_INT only when we should. */
9624 if (CONST_WIDE_INT_P (operands[1])
9625 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
9626 gcc_unreachable ();
9628 #ifdef HAVE_AS_GNU_ATTRIBUTE
9629 /* If we use a long double type, set the flags in .gnu_attribute that say
9630 what the long double type is. This is to allow the linker's warning
9631 message for the wrong long double to be useful, even if the function does
9632 not do a call (for example, doing a 128-bit add on power9 if the long
9633 double type is IEEE 128-bit. Do not set this if __ibm128 or __floa128 are
9634 used if they aren't the default long dobule type. */
9635 if (rs6000_gnu_attr && (HAVE_LD_PPC_GNU_ATTR_LONG_DOUBLE || TARGET_64BIT))
9637 if (TARGET_LONG_DOUBLE_128 && (mode == TFmode || mode == TCmode))
9638 rs6000_passes_float = rs6000_passes_long_double = true;
9640 else if (!TARGET_LONG_DOUBLE_128 && (mode == DFmode || mode == DCmode))
9641 rs6000_passes_float = rs6000_passes_long_double = true;
9643 #endif
9645 /* See if we need to special case SImode/SFmode SUBREG moves. */
9646 if ((mode == SImode || mode == SFmode) && SUBREG_P (source)
9647 && rs6000_emit_move_si_sf_subreg (dest, source, mode))
9648 return;
9650 /* Check if GCC is setting up a block move that will end up using FP
9651 registers as temporaries. We must make sure this is acceptable. */
9652 if (MEM_P (operands[0])
9653 && MEM_P (operands[1])
9654 && mode == DImode
9655 && (rs6000_slow_unaligned_access (DImode, MEM_ALIGN (operands[0]))
9656 || rs6000_slow_unaligned_access (DImode, MEM_ALIGN (operands[1])))
9657 && ! (rs6000_slow_unaligned_access (SImode,
9658 (MEM_ALIGN (operands[0]) > 32
9659 ? 32 : MEM_ALIGN (operands[0])))
9660 || rs6000_slow_unaligned_access (SImode,
9661 (MEM_ALIGN (operands[1]) > 32
9662 ? 32 : MEM_ALIGN (operands[1]))))
9663 && ! MEM_VOLATILE_P (operands [0])
9664 && ! MEM_VOLATILE_P (operands [1]))
9666 emit_move_insn (adjust_address (operands[0], SImode, 0),
9667 adjust_address (operands[1], SImode, 0));
9668 emit_move_insn (adjust_address (copy_rtx (operands[0]), SImode, 4),
9669 adjust_address (copy_rtx (operands[1]), SImode, 4));
9670 return;
9673 if (can_create_pseudo_p () && MEM_P (operands[0])
9674 && !gpc_reg_operand (operands[1], mode))
9675 operands[1] = force_reg (mode, operands[1]);
9677 /* Recognize the case where operand[1] is a reference to thread-local
9678 data and load its address to a register. */
9679 if (tls_referenced_p (operands[1]))
9681 enum tls_model model;
9682 rtx tmp = operands[1];
9683 rtx addend = NULL;
9685 if (GET_CODE (tmp) == CONST && GET_CODE (XEXP (tmp, 0)) == PLUS)
9687 addend = XEXP (XEXP (tmp, 0), 1);
9688 tmp = XEXP (XEXP (tmp, 0), 0);
9691 gcc_assert (SYMBOL_REF_P (tmp));
9692 model = SYMBOL_REF_TLS_MODEL (tmp);
9693 gcc_assert (model != 0);
9695 tmp = rs6000_legitimize_tls_address (tmp, model);
9696 if (addend)
9698 tmp = gen_rtx_PLUS (mode, tmp, addend);
9699 tmp = force_operand (tmp, operands[0]);
9701 operands[1] = tmp;
9704 /* 128-bit constant floating-point values on Darwin should really be loaded
9705 as two parts. However, this premature splitting is a problem when DFmode
9706 values can go into Altivec registers. */
9707 if (TARGET_MACHO && CONST_DOUBLE_P (operands[1]) && FLOAT128_IBM_P (mode)
9708 && !reg_addr[DFmode].scalar_in_vmx_p)
9710 rs6000_emit_move (simplify_gen_subreg (DFmode, operands[0], mode, 0),
9711 simplify_gen_subreg (DFmode, operands[1], mode, 0),
9712 DFmode);
9713 rs6000_emit_move (simplify_gen_subreg (DFmode, operands[0], mode,
9714 GET_MODE_SIZE (DFmode)),
9715 simplify_gen_subreg (DFmode, operands[1], mode,
9716 GET_MODE_SIZE (DFmode)),
9717 DFmode);
9718 return;
9721 /* Transform (p0:DD, (SUBREG:DD p1:SD)) to ((SUBREG:SD p0:DD),
9722 p1:SD) if p1 is not of floating point class and p0 is spilled as
9723 we can have no analogous movsd_store for this. */
9724 if (lra_in_progress && mode == DDmode
9725 && REG_P (operands[0]) && !HARD_REGISTER_P (operands[0])
9726 && reg_preferred_class (REGNO (operands[0])) == NO_REGS
9727 && SUBREG_P (operands[1]) && REG_P (SUBREG_REG (operands[1]))
9728 && GET_MODE (SUBREG_REG (operands[1])) == SDmode)
9730 enum reg_class cl;
9731 int regno = REGNO (SUBREG_REG (operands[1]));
9733 if (!HARD_REGISTER_NUM_P (regno))
9735 cl = reg_preferred_class (regno);
9736 regno = reg_renumber[regno];
9737 if (regno < 0)
9738 regno = cl == NO_REGS ? -1 : ira_class_hard_regs[cl][1];
9740 if (regno >= 0 && ! FP_REGNO_P (regno))
9742 mode = SDmode;
9743 operands[0] = gen_lowpart_SUBREG (SDmode, operands[0]);
9744 operands[1] = SUBREG_REG (operands[1]);
9747 if (lra_in_progress
9748 && mode == SDmode
9749 && REG_P (operands[0]) && !HARD_REGISTER_P (operands[0])
9750 && reg_preferred_class (REGNO (operands[0])) == NO_REGS
9751 && (REG_P (operands[1])
9752 || (SUBREG_P (operands[1]) && REG_P (SUBREG_REG (operands[1])))))
9754 int regno = reg_or_subregno (operands[1]);
9755 enum reg_class cl;
9757 if (!HARD_REGISTER_NUM_P (regno))
9759 cl = reg_preferred_class (regno);
9760 gcc_assert (cl != NO_REGS);
9761 regno = reg_renumber[regno];
9762 if (regno < 0)
9763 regno = ira_class_hard_regs[cl][0];
9765 if (FP_REGNO_P (regno))
9767 if (GET_MODE (operands[0]) != DDmode)
9768 operands[0] = gen_rtx_SUBREG (DDmode, operands[0], 0);
9769 emit_insn (gen_movsd_store (operands[0], operands[1]));
9771 else if (INT_REGNO_P (regno))
9772 emit_insn (gen_movsd_hardfloat (operands[0], operands[1]));
9773 else
9774 gcc_unreachable();
9775 return;
9777 /* Transform ((SUBREG:DD p0:SD), p1:DD) to (p0:SD, (SUBREG:SD
9778 p:DD)) if p0 is not of floating point class and p1 is spilled as
9779 we can have no analogous movsd_load for this. */
9780 if (lra_in_progress && mode == DDmode
9781 && SUBREG_P (operands[0]) && REG_P (SUBREG_REG (operands[0]))
9782 && GET_MODE (SUBREG_REG (operands[0])) == SDmode
9783 && REG_P (operands[1]) && !HARD_REGISTER_P (operands[1])
9784 && reg_preferred_class (REGNO (operands[1])) == NO_REGS)
9786 enum reg_class cl;
9787 int regno = REGNO (SUBREG_REG (operands[0]));
9789 if (!HARD_REGISTER_NUM_P (regno))
9791 cl = reg_preferred_class (regno);
9792 regno = reg_renumber[regno];
9793 if (regno < 0)
9794 regno = cl == NO_REGS ? -1 : ira_class_hard_regs[cl][0];
9796 if (regno >= 0 && ! FP_REGNO_P (regno))
9798 mode = SDmode;
9799 operands[0] = SUBREG_REG (operands[0]);
9800 operands[1] = gen_lowpart_SUBREG (SDmode, operands[1]);
9803 if (lra_in_progress
9804 && mode == SDmode
9805 && (REG_P (operands[0])
9806 || (SUBREG_P (operands[0]) && REG_P (SUBREG_REG (operands[0]))))
9807 && REG_P (operands[1]) && !HARD_REGISTER_P (operands[1])
9808 && reg_preferred_class (REGNO (operands[1])) == NO_REGS)
9810 int regno = reg_or_subregno (operands[0]);
9811 enum reg_class cl;
9813 if (!HARD_REGISTER_NUM_P (regno))
9815 cl = reg_preferred_class (regno);
9816 gcc_assert (cl != NO_REGS);
9817 regno = reg_renumber[regno];
9818 if (regno < 0)
9819 regno = ira_class_hard_regs[cl][0];
9821 if (FP_REGNO_P (regno))
9823 if (GET_MODE (operands[1]) != DDmode)
9824 operands[1] = gen_rtx_SUBREG (DDmode, operands[1], 0);
9825 emit_insn (gen_movsd_load (operands[0], operands[1]));
9827 else if (INT_REGNO_P (regno))
9828 emit_insn (gen_movsd_hardfloat (operands[0], operands[1]));
9829 else
9830 gcc_unreachable();
9831 return;
9834 /* FIXME: In the long term, this switch statement should go away
9835 and be replaced by a sequence of tests based on things like
9836 mode == Pmode. */
9837 switch (mode)
9839 case E_HImode:
9840 case E_QImode:
9841 if (CONSTANT_P (operands[1])
9842 && !CONST_INT_P (operands[1]))
9843 operands[1] = force_const_mem (mode, operands[1]);
9844 break;
9846 case E_TFmode:
9847 case E_TDmode:
9848 case E_IFmode:
9849 case E_KFmode:
9850 if (FLOAT128_2REG_P (mode))
9851 rs6000_eliminate_indexed_memrefs (operands);
9852 /* fall through */
9854 case E_DFmode:
9855 case E_DDmode:
9856 case E_SFmode:
9857 case E_SDmode:
9858 if (CONSTANT_P (operands[1])
9859 && ! easy_fp_constant (operands[1], mode))
9860 operands[1] = force_const_mem (mode, operands[1]);
9861 break;
9863 case E_V16QImode:
9864 case E_V8HImode:
9865 case E_V4SFmode:
9866 case E_V4SImode:
9867 case E_V2DFmode:
9868 case E_V2DImode:
9869 case E_V1TImode:
9870 if (CONSTANT_P (operands[1])
9871 && !easy_vector_constant (operands[1], mode))
9872 operands[1] = force_const_mem (mode, operands[1]);
9873 break;
9875 case E_SImode:
9876 case E_DImode:
9877 /* Use default pattern for address of ELF small data */
9878 if (TARGET_ELF
9879 && mode == Pmode
9880 && DEFAULT_ABI == ABI_V4
9881 && (SYMBOL_REF_P (operands[1])
9882 || GET_CODE (operands[1]) == CONST)
9883 && small_data_operand (operands[1], mode))
9885 emit_insn (gen_rtx_SET (operands[0], operands[1]));
9886 return;
9889 if (DEFAULT_ABI == ABI_V4
9890 && mode == Pmode && mode == SImode
9891 && flag_pic == 1 && got_operand (operands[1], mode))
9893 emit_insn (gen_movsi_got (operands[0], operands[1]));
9894 return;
9897 if ((TARGET_ELF || DEFAULT_ABI == ABI_DARWIN)
9898 && TARGET_NO_TOC
9899 && ! flag_pic
9900 && mode == Pmode
9901 && CONSTANT_P (operands[1])
9902 && GET_CODE (operands[1]) != HIGH
9903 && !CONST_INT_P (operands[1]))
9905 rtx target = (!can_create_pseudo_p ()
9906 ? operands[0]
9907 : gen_reg_rtx (mode));
9909 /* If this is a function address on -mcall-aixdesc,
9910 convert it to the address of the descriptor. */
9911 if (DEFAULT_ABI == ABI_AIX
9912 && SYMBOL_REF_P (operands[1])
9913 && XSTR (operands[1], 0)[0] == '.')
9915 const char *name = XSTR (operands[1], 0);
9916 rtx new_ref;
9917 while (*name == '.')
9918 name++;
9919 new_ref = gen_rtx_SYMBOL_REF (Pmode, name);
9920 CONSTANT_POOL_ADDRESS_P (new_ref)
9921 = CONSTANT_POOL_ADDRESS_P (operands[1]);
9922 SYMBOL_REF_FLAGS (new_ref) = SYMBOL_REF_FLAGS (operands[1]);
9923 SYMBOL_REF_USED (new_ref) = SYMBOL_REF_USED (operands[1]);
9924 SYMBOL_REF_DATA (new_ref) = SYMBOL_REF_DATA (operands[1]);
9925 operands[1] = new_ref;
9928 if (DEFAULT_ABI == ABI_DARWIN)
9930 #if TARGET_MACHO
9931 if (MACHO_DYNAMIC_NO_PIC_P)
9933 /* Take care of any required data indirection. */
9934 operands[1] = rs6000_machopic_legitimize_pic_address (
9935 operands[1], mode, operands[0]);
9936 if (operands[0] != operands[1])
9937 emit_insn (gen_rtx_SET (operands[0], operands[1]));
9938 return;
9940 #endif
9941 emit_insn (gen_macho_high (target, operands[1]));
9942 emit_insn (gen_macho_low (operands[0], target, operands[1]));
9943 return;
9946 emit_insn (gen_elf_high (target, operands[1]));
9947 emit_insn (gen_elf_low (operands[0], target, operands[1]));
9948 return;
9951 /* If this is a SYMBOL_REF that refers to a constant pool entry,
9952 and we have put it in the TOC, we just need to make a TOC-relative
9953 reference to it. */
9954 if (TARGET_TOC
9955 && SYMBOL_REF_P (operands[1])
9956 && use_toc_relative_ref (operands[1], mode))
9957 operands[1] = create_TOC_reference (operands[1], operands[0]);
9958 else if (mode == Pmode
9959 && CONSTANT_P (operands[1])
9960 && GET_CODE (operands[1]) != HIGH
9961 && ((REG_P (operands[0])
9962 && FP_REGNO_P (REGNO (operands[0])))
9963 || !CONST_INT_P (operands[1])
9964 || (num_insns_constant (operands[1], mode)
9965 > (TARGET_CMODEL != CMODEL_SMALL ? 3 : 2)))
9966 && !toc_relative_expr_p (operands[1], false, NULL, NULL)
9967 && (TARGET_CMODEL == CMODEL_SMALL
9968 || can_create_pseudo_p ()
9969 || (REG_P (operands[0])
9970 && INT_REG_OK_FOR_BASE_P (operands[0], true))))
9973 #if TARGET_MACHO
9974 /* Darwin uses a special PIC legitimizer. */
9975 if (DEFAULT_ABI == ABI_DARWIN && MACHOPIC_INDIRECT)
9977 operands[1] =
9978 rs6000_machopic_legitimize_pic_address (operands[1], mode,
9979 operands[0]);
9980 if (operands[0] != operands[1])
9981 emit_insn (gen_rtx_SET (operands[0], operands[1]));
9982 return;
9984 #endif
9986 /* If we are to limit the number of things we put in the TOC and
9987 this is a symbol plus a constant we can add in one insn,
9988 just put the symbol in the TOC and add the constant. */
9989 if (GET_CODE (operands[1]) == CONST
9990 && TARGET_NO_SUM_IN_TOC
9991 && GET_CODE (XEXP (operands[1], 0)) == PLUS
9992 && add_operand (XEXP (XEXP (operands[1], 0), 1), mode)
9993 && (GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == LABEL_REF
9994 || SYMBOL_REF_P (XEXP (XEXP (operands[1], 0), 0)))
9995 && ! side_effects_p (operands[0]))
9997 rtx sym =
9998 force_const_mem (mode, XEXP (XEXP (operands[1], 0), 0));
9999 rtx other = XEXP (XEXP (operands[1], 0), 1);
10001 sym = force_reg (mode, sym);
10002 emit_insn (gen_add3_insn (operands[0], sym, other));
10003 return;
10006 operands[1] = force_const_mem (mode, operands[1]);
10008 if (TARGET_TOC
10009 && SYMBOL_REF_P (XEXP (operands[1], 0))
10010 && use_toc_relative_ref (XEXP (operands[1], 0), mode))
10012 rtx tocref = create_TOC_reference (XEXP (operands[1], 0),
10013 operands[0]);
10014 operands[1] = gen_const_mem (mode, tocref);
10015 set_mem_alias_set (operands[1], get_TOC_alias_set ());
10018 break;
10020 case E_TImode:
10021 if (!VECTOR_MEM_VSX_P (TImode))
10022 rs6000_eliminate_indexed_memrefs (operands);
10023 break;
10025 case E_PTImode:
10026 rs6000_eliminate_indexed_memrefs (operands);
10027 break;
10029 default:
10030 fatal_insn ("bad move", gen_rtx_SET (dest, source));
10033 /* Above, we may have called force_const_mem which may have returned
10034 an invalid address. If we can, fix this up; otherwise, reload will
10035 have to deal with it. */
10036 if (MEM_P (operands[1]))
10037 operands[1] = validize_mem (operands[1]);
10039 emit_insn (gen_rtx_SET (operands[0], operands[1]));
10042 /* Nonzero if we can use a floating-point register to pass this arg. */
10043 #define USE_FP_FOR_ARG_P(CUM,MODE) \
10044 (SCALAR_FLOAT_MODE_NOT_VECTOR_P (MODE) \
10045 && (CUM)->fregno <= FP_ARG_MAX_REG \
10046 && TARGET_HARD_FLOAT)
10048 /* Nonzero if we can use an AltiVec register to pass this arg. */
10049 #define USE_ALTIVEC_FOR_ARG_P(CUM,MODE,NAMED) \
10050 (ALTIVEC_OR_VSX_VECTOR_MODE (MODE) \
10051 && (CUM)->vregno <= ALTIVEC_ARG_MAX_REG \
10052 && TARGET_ALTIVEC_ABI \
10053 && (NAMED))
10055 /* Walk down the type tree of TYPE counting consecutive base elements.
10056 If *MODEP is VOIDmode, then set it to the first valid floating point
10057 or vector type. If a non-floating point or vector type is found, or
10058 if a floating point or vector type that doesn't match a non-VOIDmode
10059 *MODEP is found, then return -1, otherwise return the count in the
10060 sub-tree. */
10062 static int
10063 rs6000_aggregate_candidate (const_tree type, machine_mode *modep)
10065 machine_mode mode;
10066 HOST_WIDE_INT size;
10068 switch (TREE_CODE (type))
10070 case REAL_TYPE:
10071 mode = TYPE_MODE (type);
10072 if (!SCALAR_FLOAT_MODE_P (mode))
10073 return -1;
10075 if (*modep == VOIDmode)
10076 *modep = mode;
10078 if (*modep == mode)
10079 return 1;
10081 break;
10083 case COMPLEX_TYPE:
10084 mode = TYPE_MODE (TREE_TYPE (type));
10085 if (!SCALAR_FLOAT_MODE_P (mode))
10086 return -1;
10088 if (*modep == VOIDmode)
10089 *modep = mode;
10091 if (*modep == mode)
10092 return 2;
10094 break;
10096 case VECTOR_TYPE:
10097 if (!TARGET_ALTIVEC_ABI || !TARGET_ALTIVEC)
10098 return -1;
10100 /* Use V4SImode as representative of all 128-bit vector types. */
10101 size = int_size_in_bytes (type);
10102 switch (size)
10104 case 16:
10105 mode = V4SImode;
10106 break;
10107 default:
10108 return -1;
10111 if (*modep == VOIDmode)
10112 *modep = mode;
10114 /* Vector modes are considered to be opaque: two vectors are
10115 equivalent for the purposes of being homogeneous aggregates
10116 if they are the same size. */
10117 if (*modep == mode)
10118 return 1;
10120 break;
10122 case ARRAY_TYPE:
10124 int count;
10125 tree index = TYPE_DOMAIN (type);
10127 /* Can't handle incomplete types nor sizes that are not
10128 fixed. */
10129 if (!COMPLETE_TYPE_P (type)
10130 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
10131 return -1;
10133 count = rs6000_aggregate_candidate (TREE_TYPE (type), modep);
10134 if (count == -1
10135 || !index
10136 || !TYPE_MAX_VALUE (index)
10137 || !tree_fits_uhwi_p (TYPE_MAX_VALUE (index))
10138 || !TYPE_MIN_VALUE (index)
10139 || !tree_fits_uhwi_p (TYPE_MIN_VALUE (index))
10140 || count < 0)
10141 return -1;
10143 count *= (1 + tree_to_uhwi (TYPE_MAX_VALUE (index))
10144 - tree_to_uhwi (TYPE_MIN_VALUE (index)));
10146 /* There must be no padding. */
10147 if (wi::to_wide (TYPE_SIZE (type))
10148 != count * GET_MODE_BITSIZE (*modep))
10149 return -1;
10151 return count;
10154 case RECORD_TYPE:
10156 int count = 0;
10157 int sub_count;
10158 tree field;
10160 /* Can't handle incomplete types nor sizes that are not
10161 fixed. */
10162 if (!COMPLETE_TYPE_P (type)
10163 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
10164 return -1;
10166 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
10168 if (TREE_CODE (field) != FIELD_DECL)
10169 continue;
10171 sub_count = rs6000_aggregate_candidate (TREE_TYPE (field), modep);
10172 if (sub_count < 0)
10173 return -1;
10174 count += sub_count;
10177 /* There must be no padding. */
10178 if (wi::to_wide (TYPE_SIZE (type))
10179 != count * GET_MODE_BITSIZE (*modep))
10180 return -1;
10182 return count;
10185 case UNION_TYPE:
10186 case QUAL_UNION_TYPE:
10188 /* These aren't very interesting except in a degenerate case. */
10189 int count = 0;
10190 int sub_count;
10191 tree field;
10193 /* Can't handle incomplete types nor sizes that are not
10194 fixed. */
10195 if (!COMPLETE_TYPE_P (type)
10196 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
10197 return -1;
10199 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
10201 if (TREE_CODE (field) != FIELD_DECL)
10202 continue;
10204 sub_count = rs6000_aggregate_candidate (TREE_TYPE (field), modep);
10205 if (sub_count < 0)
10206 return -1;
10207 count = count > sub_count ? count : sub_count;
10210 /* There must be no padding. */
10211 if (wi::to_wide (TYPE_SIZE (type))
10212 != count * GET_MODE_BITSIZE (*modep))
10213 return -1;
10215 return count;
10218 default:
10219 break;
10222 return -1;
10225 /* If an argument, whose type is described by TYPE and MODE, is a homogeneous
10226 float or vector aggregate that shall be passed in FP/vector registers
10227 according to the ELFv2 ABI, return the homogeneous element mode in
10228 *ELT_MODE and the number of elements in *N_ELTS, and return TRUE.
10230 Otherwise, set *ELT_MODE to MODE and *N_ELTS to 1, and return FALSE. */
10232 static bool
10233 rs6000_discover_homogeneous_aggregate (machine_mode mode, const_tree type,
10234 machine_mode *elt_mode,
10235 int *n_elts)
10237 /* Note that we do not accept complex types at the top level as
10238 homogeneous aggregates; these types are handled via the
10239 targetm.calls.split_complex_arg mechanism. Complex types
10240 can be elements of homogeneous aggregates, however. */
10241 if (TARGET_HARD_FLOAT && DEFAULT_ABI == ABI_ELFv2 && type
10242 && AGGREGATE_TYPE_P (type))
10244 machine_mode field_mode = VOIDmode;
10245 int field_count = rs6000_aggregate_candidate (type, &field_mode);
10247 if (field_count > 0)
10249 int reg_size = ALTIVEC_OR_VSX_VECTOR_MODE (field_mode) ? 16 : 8;
10250 int field_size = ROUND_UP (GET_MODE_SIZE (field_mode), reg_size);
10252 /* The ELFv2 ABI allows homogeneous aggregates to occupy
10253 up to AGGR_ARG_NUM_REG registers. */
10254 if (field_count * field_size <= AGGR_ARG_NUM_REG * reg_size)
10256 if (elt_mode)
10257 *elt_mode = field_mode;
10258 if (n_elts)
10259 *n_elts = field_count;
10260 return true;
10265 if (elt_mode)
10266 *elt_mode = mode;
10267 if (n_elts)
10268 *n_elts = 1;
10269 return false;
10272 /* Return a nonzero value to say to return the function value in
10273 memory, just as large structures are always returned. TYPE will be
10274 the data type of the value, and FNTYPE will be the type of the
10275 function doing the returning, or @code{NULL} for libcalls.
10277 The AIX ABI for the RS/6000 specifies that all structures are
10278 returned in memory. The Darwin ABI does the same.
10280 For the Darwin 64 Bit ABI, a function result can be returned in
10281 registers or in memory, depending on the size of the return data
10282 type. If it is returned in registers, the value occupies the same
10283 registers as it would if it were the first and only function
10284 argument. Otherwise, the function places its result in memory at
10285 the location pointed to by GPR3.
10287 The SVR4 ABI specifies that structures <= 8 bytes are returned in r3/r4,
10288 but a draft put them in memory, and GCC used to implement the draft
10289 instead of the final standard. Therefore, aix_struct_return
10290 controls this instead of DEFAULT_ABI; V.4 targets needing backward
10291 compatibility can change DRAFT_V4_STRUCT_RET to override the
10292 default, and -m switches get the final word. See
10293 rs6000_option_override_internal for more details.
10295 The PPC32 SVR4 ABI uses IEEE double extended for long double, if 128-bit
10296 long double support is enabled. These values are returned in memory.
10298 int_size_in_bytes returns -1 for variable size objects, which go in
10299 memory always. The cast to unsigned makes -1 > 8. */
10301 static bool
10302 rs6000_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
10304 /* For the Darwin64 ABI, test if we can fit the return value in regs. */
10305 if (TARGET_MACHO
10306 && rs6000_darwin64_abi
10307 && TREE_CODE (type) == RECORD_TYPE
10308 && int_size_in_bytes (type) > 0)
10310 CUMULATIVE_ARGS valcum;
10311 rtx valret;
10313 valcum.words = 0;
10314 valcum.fregno = FP_ARG_MIN_REG;
10315 valcum.vregno = ALTIVEC_ARG_MIN_REG;
10316 /* Do a trial code generation as if this were going to be passed
10317 as an argument; if any part goes in memory, we return NULL. */
10318 valret = rs6000_darwin64_record_arg (&valcum, type, true, true);
10319 if (valret)
10320 return false;
10321 /* Otherwise fall through to more conventional ABI rules. */
10324 /* The ELFv2 ABI returns homogeneous VFP aggregates in registers */
10325 if (rs6000_discover_homogeneous_aggregate (TYPE_MODE (type), type,
10326 NULL, NULL))
10327 return false;
10329 /* The ELFv2 ABI returns aggregates up to 16B in registers */
10330 if (DEFAULT_ABI == ABI_ELFv2 && AGGREGATE_TYPE_P (type)
10331 && (unsigned HOST_WIDE_INT) int_size_in_bytes (type) <= 16)
10332 return false;
10334 if (AGGREGATE_TYPE_P (type)
10335 && (aix_struct_return
10336 || (unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 8))
10337 return true;
10339 /* Allow -maltivec -mabi=no-altivec without warning. Altivec vector
10340 modes only exist for GCC vector types if -maltivec. */
10341 if (TARGET_32BIT && !TARGET_ALTIVEC_ABI
10342 && ALTIVEC_VECTOR_MODE (TYPE_MODE (type)))
10343 return false;
10345 /* Return synthetic vectors in memory. */
10346 if (TREE_CODE (type) == VECTOR_TYPE
10347 && int_size_in_bytes (type) > (TARGET_ALTIVEC_ABI ? 16 : 8))
10349 static bool warned_for_return_big_vectors = false;
10350 if (!warned_for_return_big_vectors)
10352 warning (OPT_Wpsabi, "GCC vector returned by reference: "
10353 "non-standard ABI extension with no compatibility "
10354 "guarantee");
10355 warned_for_return_big_vectors = true;
10357 return true;
10360 if (DEFAULT_ABI == ABI_V4 && TARGET_IEEEQUAD
10361 && FLOAT128_IEEE_P (TYPE_MODE (type)))
10362 return true;
10364 return false;
10367 /* Specify whether values returned in registers should be at the most
10368 significant end of a register. We want aggregates returned by
10369 value to match the way aggregates are passed to functions. */
10371 static bool
10372 rs6000_return_in_msb (const_tree valtype)
10374 return (DEFAULT_ABI == ABI_ELFv2
10375 && BYTES_BIG_ENDIAN
10376 && AGGREGATE_TYPE_P (valtype)
10377 && (rs6000_function_arg_padding (TYPE_MODE (valtype), valtype)
10378 == PAD_UPWARD));
10381 #ifdef HAVE_AS_GNU_ATTRIBUTE
10382 /* Return TRUE if a call to function FNDECL may be one that
10383 potentially affects the function calling ABI of the object file. */
10385 static bool
10386 call_ABI_of_interest (tree fndecl)
10388 if (rs6000_gnu_attr && symtab->state == EXPANSION)
10390 struct cgraph_node *c_node;
10392 /* Libcalls are always interesting. */
10393 if (fndecl == NULL_TREE)
10394 return true;
10396 /* Any call to an external function is interesting. */
10397 if (DECL_EXTERNAL (fndecl))
10398 return true;
10400 /* Interesting functions that we are emitting in this object file. */
10401 c_node = cgraph_node::get (fndecl);
10402 c_node = c_node->ultimate_alias_target ();
10403 return !c_node->only_called_directly_p ();
10405 return false;
10407 #endif
10409 /* Initialize a variable CUM of type CUMULATIVE_ARGS
10410 for a call to a function whose data type is FNTYPE.
10411 For a library call, FNTYPE is 0 and RETURN_MODE the return value mode.
10413 For incoming args we set the number of arguments in the prototype large
10414 so we never return a PARALLEL. */
10416 void
10417 init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype,
10418 rtx libname ATTRIBUTE_UNUSED, int incoming,
10419 int libcall, int n_named_args,
10420 tree fndecl,
10421 machine_mode return_mode ATTRIBUTE_UNUSED)
10423 static CUMULATIVE_ARGS zero_cumulative;
10425 *cum = zero_cumulative;
10426 cum->words = 0;
10427 cum->fregno = FP_ARG_MIN_REG;
10428 cum->vregno = ALTIVEC_ARG_MIN_REG;
10429 cum->prototype = (fntype && prototype_p (fntype));
10430 cum->call_cookie = ((DEFAULT_ABI == ABI_V4 && libcall)
10431 ? CALL_LIBCALL : CALL_NORMAL);
10432 cum->sysv_gregno = GP_ARG_MIN_REG;
10433 cum->stdarg = stdarg_p (fntype);
10434 cum->libcall = libcall;
10436 cum->nargs_prototype = 0;
10437 if (incoming || cum->prototype)
10438 cum->nargs_prototype = n_named_args;
10440 /* Check for a longcall attribute. */
10441 if ((!fntype && rs6000_default_long_calls)
10442 || (fntype
10443 && lookup_attribute ("longcall", TYPE_ATTRIBUTES (fntype))
10444 && !lookup_attribute ("shortcall", TYPE_ATTRIBUTES (fntype))))
10445 cum->call_cookie |= CALL_LONG;
10446 else if (DEFAULT_ABI != ABI_DARWIN)
10448 bool is_local = (fndecl
10449 && !DECL_EXTERNAL (fndecl)
10450 && !DECL_WEAK (fndecl)
10451 && (*targetm.binds_local_p) (fndecl));
10452 if (is_local)
10454 else if (flag_plt)
10456 if (fntype
10457 && lookup_attribute ("noplt", TYPE_ATTRIBUTES (fntype)))
10458 cum->call_cookie |= CALL_LONG;
10460 else
10462 if (!(fntype
10463 && lookup_attribute ("plt", TYPE_ATTRIBUTES (fntype))))
10464 cum->call_cookie |= CALL_LONG;
10468 if (TARGET_DEBUG_ARG)
10470 fprintf (stderr, "\ninit_cumulative_args:");
10471 if (fntype)
10473 tree ret_type = TREE_TYPE (fntype);
10474 fprintf (stderr, " ret code = %s,",
10475 get_tree_code_name (TREE_CODE (ret_type)));
10478 if (cum->call_cookie & CALL_LONG)
10479 fprintf (stderr, " longcall,");
10481 fprintf (stderr, " proto = %d, nargs = %d\n",
10482 cum->prototype, cum->nargs_prototype);
10485 #ifdef HAVE_AS_GNU_ATTRIBUTE
10486 if (TARGET_ELF && (TARGET_64BIT || DEFAULT_ABI == ABI_V4))
10488 cum->escapes = call_ABI_of_interest (fndecl);
10489 if (cum->escapes)
10491 tree return_type;
10493 if (fntype)
10495 return_type = TREE_TYPE (fntype);
10496 return_mode = TYPE_MODE (return_type);
10498 else
10499 return_type = lang_hooks.types.type_for_mode (return_mode, 0);
10501 if (return_type != NULL)
10503 if (TREE_CODE (return_type) == RECORD_TYPE
10504 && TYPE_TRANSPARENT_AGGR (return_type))
10506 return_type = TREE_TYPE (first_field (return_type));
10507 return_mode = TYPE_MODE (return_type);
10509 if (AGGREGATE_TYPE_P (return_type)
10510 && ((unsigned HOST_WIDE_INT) int_size_in_bytes (return_type)
10511 <= 8))
10512 rs6000_returns_struct = true;
10514 if (SCALAR_FLOAT_MODE_P (return_mode))
10516 rs6000_passes_float = true;
10517 if ((HAVE_LD_PPC_GNU_ATTR_LONG_DOUBLE || TARGET_64BIT)
10518 && (FLOAT128_IBM_P (return_mode)
10519 || FLOAT128_IEEE_P (return_mode)
10520 || (return_type != NULL
10521 && (TYPE_MAIN_VARIANT (return_type)
10522 == long_double_type_node))))
10523 rs6000_passes_long_double = true;
10525 /* Note if we passed or return a IEEE 128-bit type. We changed
10526 the mangling for these types, and we may need to make an alias
10527 with the old mangling. */
10528 if (FLOAT128_IEEE_P (return_mode))
10529 rs6000_passes_ieee128 = true;
10531 if (ALTIVEC_OR_VSX_VECTOR_MODE (return_mode))
10532 rs6000_passes_vector = true;
10535 #endif
10537 if (fntype
10538 && !TARGET_ALTIVEC
10539 && TARGET_ALTIVEC_ABI
10540 && ALTIVEC_VECTOR_MODE (TYPE_MODE (TREE_TYPE (fntype))))
10542 error ("cannot return value in vector register because"
10543 " altivec instructions are disabled, use %qs"
10544 " to enable them", "-maltivec");
10548 /* The mode the ABI uses for a word. This is not the same as word_mode
10549 for -m32 -mpowerpc64. This is used to implement various target hooks. */
10551 static scalar_int_mode
10552 rs6000_abi_word_mode (void)
10554 return TARGET_32BIT ? SImode : DImode;
10557 /* Implement the TARGET_OFFLOAD_OPTIONS hook. */
10558 static char *
10559 rs6000_offload_options (void)
10561 if (TARGET_64BIT)
10562 return xstrdup ("-foffload-abi=lp64");
10563 else
10564 return xstrdup ("-foffload-abi=ilp32");
10567 /* On rs6000, function arguments are promoted, as are function return
10568 values. */
10570 static machine_mode
10571 rs6000_promote_function_mode (const_tree type ATTRIBUTE_UNUSED,
10572 machine_mode mode,
10573 int *punsignedp ATTRIBUTE_UNUSED,
10574 const_tree, int)
10576 PROMOTE_MODE (mode, *punsignedp, type);
10578 return mode;
10581 /* Return true if TYPE must be passed on the stack and not in registers. */
10583 static bool
10584 rs6000_must_pass_in_stack (machine_mode mode, const_tree type)
10586 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2 || TARGET_64BIT)
10587 return must_pass_in_stack_var_size (mode, type);
10588 else
10589 return must_pass_in_stack_var_size_or_pad (mode, type);
10592 static inline bool
10593 is_complex_IBM_long_double (machine_mode mode)
10595 return mode == ICmode || (mode == TCmode && FLOAT128_IBM_P (TCmode));
10598 /* Whether ABI_V4 passes MODE args to a function in floating point
10599 registers. */
10601 static bool
10602 abi_v4_pass_in_fpr (machine_mode mode, bool named)
10604 if (!TARGET_HARD_FLOAT)
10605 return false;
10606 if (mode == DFmode)
10607 return true;
10608 if (mode == SFmode && named)
10609 return true;
10610 /* ABI_V4 passes complex IBM long double in 8 gprs.
10611 Stupid, but we can't change the ABI now. */
10612 if (is_complex_IBM_long_double (mode))
10613 return false;
10614 if (FLOAT128_2REG_P (mode))
10615 return true;
10616 if (DECIMAL_FLOAT_MODE_P (mode))
10617 return true;
10618 return false;
10621 /* Implement TARGET_FUNCTION_ARG_PADDING.
10623 For the AIX ABI structs are always stored left shifted in their
10624 argument slot. */
10626 static pad_direction
10627 rs6000_function_arg_padding (machine_mode mode, const_tree type)
10629 #ifndef AGGREGATE_PADDING_FIXED
10630 #define AGGREGATE_PADDING_FIXED 0
10631 #endif
10632 #ifndef AGGREGATES_PAD_UPWARD_ALWAYS
10633 #define AGGREGATES_PAD_UPWARD_ALWAYS 0
10634 #endif
10636 if (!AGGREGATE_PADDING_FIXED)
10638 /* GCC used to pass structures of the same size as integer types as
10639 if they were in fact integers, ignoring TARGET_FUNCTION_ARG_PADDING.
10640 i.e. Structures of size 1 or 2 (or 4 when TARGET_64BIT) were
10641 passed padded downward, except that -mstrict-align further
10642 muddied the water in that multi-component structures of 2 and 4
10643 bytes in size were passed padded upward.
10645 The following arranges for best compatibility with previous
10646 versions of gcc, but removes the -mstrict-align dependency. */
10647 if (BYTES_BIG_ENDIAN)
10649 HOST_WIDE_INT size = 0;
10651 if (mode == BLKmode)
10653 if (type && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10654 size = int_size_in_bytes (type);
10656 else
10657 size = GET_MODE_SIZE (mode);
10659 if (size == 1 || size == 2 || size == 4)
10660 return PAD_DOWNWARD;
10662 return PAD_UPWARD;
10665 if (AGGREGATES_PAD_UPWARD_ALWAYS)
10667 if (type != 0 && AGGREGATE_TYPE_P (type))
10668 return PAD_UPWARD;
10671 /* Fall back to the default. */
10672 return default_function_arg_padding (mode, type);
10675 /* If defined, a C expression that gives the alignment boundary, in bits,
10676 of an argument with the specified mode and type. If it is not defined,
10677 PARM_BOUNDARY is used for all arguments.
10679 V.4 wants long longs and doubles to be double word aligned. Just
10680 testing the mode size is a boneheaded way to do this as it means
10681 that other types such as complex int are also double word aligned.
10682 However, we're stuck with this because changing the ABI might break
10683 existing library interfaces.
10685 Quadword align Altivec/VSX vectors.
10686 Quadword align large synthetic vector types. */
10688 static unsigned int
10689 rs6000_function_arg_boundary (machine_mode mode, const_tree type)
10691 machine_mode elt_mode;
10692 int n_elts;
10694 rs6000_discover_homogeneous_aggregate (mode, type, &elt_mode, &n_elts);
10696 if (DEFAULT_ABI == ABI_V4
10697 && (GET_MODE_SIZE (mode) == 8
10698 || (TARGET_HARD_FLOAT
10699 && !is_complex_IBM_long_double (mode)
10700 && FLOAT128_2REG_P (mode))))
10701 return 64;
10702 else if (FLOAT128_VECTOR_P (mode))
10703 return 128;
10704 else if (type && TREE_CODE (type) == VECTOR_TYPE
10705 && int_size_in_bytes (type) >= 8
10706 && int_size_in_bytes (type) < 16)
10707 return 64;
10708 else if (ALTIVEC_OR_VSX_VECTOR_MODE (elt_mode)
10709 || (type && TREE_CODE (type) == VECTOR_TYPE
10710 && int_size_in_bytes (type) >= 16))
10711 return 128;
10713 /* Aggregate types that need > 8 byte alignment are quadword-aligned
10714 in the parameter area in the ELFv2 ABI, and in the AIX ABI unless
10715 -mcompat-align-parm is used. */
10716 if (((DEFAULT_ABI == ABI_AIX && !rs6000_compat_align_parm)
10717 || DEFAULT_ABI == ABI_ELFv2)
10718 && type && TYPE_ALIGN (type) > 64)
10720 /* "Aggregate" means any AGGREGATE_TYPE except for single-element
10721 or homogeneous float/vector aggregates here. We already handled
10722 vector aggregates above, but still need to check for float here. */
10723 bool aggregate_p = (AGGREGATE_TYPE_P (type)
10724 && !SCALAR_FLOAT_MODE_P (elt_mode));
10726 /* We used to check for BLKmode instead of the above aggregate type
10727 check. Warn when this results in any difference to the ABI. */
10728 if (aggregate_p != (mode == BLKmode))
10730 static bool warned;
10731 if (!warned && warn_psabi)
10733 warned = true;
10734 inform (input_location,
10735 "the ABI of passing aggregates with %d-byte alignment"
10736 " has changed in GCC 5",
10737 (int) TYPE_ALIGN (type) / BITS_PER_UNIT);
10741 if (aggregate_p)
10742 return 128;
10745 /* Similar for the Darwin64 ABI. Note that for historical reasons we
10746 implement the "aggregate type" check as a BLKmode check here; this
10747 means certain aggregate types are in fact not aligned. */
10748 if (TARGET_MACHO && rs6000_darwin64_abi
10749 && mode == BLKmode
10750 && type && TYPE_ALIGN (type) > 64)
10751 return 128;
10753 return PARM_BOUNDARY;
10756 /* The offset in words to the start of the parameter save area. */
10758 static unsigned int
10759 rs6000_parm_offset (void)
10761 return (DEFAULT_ABI == ABI_V4 ? 2
10762 : DEFAULT_ABI == ABI_ELFv2 ? 4
10763 : 6);
10766 /* For a function parm of MODE and TYPE, return the starting word in
10767 the parameter area. NWORDS of the parameter area are already used. */
10769 static unsigned int
10770 rs6000_parm_start (machine_mode mode, const_tree type,
10771 unsigned int nwords)
10773 unsigned int align;
10775 align = rs6000_function_arg_boundary (mode, type) / PARM_BOUNDARY - 1;
10776 return nwords + (-(rs6000_parm_offset () + nwords) & align);
10779 /* Compute the size (in words) of a function argument. */
10781 static unsigned long
10782 rs6000_arg_size (machine_mode mode, const_tree type)
10784 unsigned long size;
10786 if (mode != BLKmode)
10787 size = GET_MODE_SIZE (mode);
10788 else
10789 size = int_size_in_bytes (type);
10791 if (TARGET_32BIT)
10792 return (size + 3) >> 2;
10793 else
10794 return (size + 7) >> 3;
10797 /* Use this to flush pending int fields. */
10799 static void
10800 rs6000_darwin64_record_arg_advance_flush (CUMULATIVE_ARGS *cum,
10801 HOST_WIDE_INT bitpos, int final)
10803 unsigned int startbit, endbit;
10804 int intregs, intoffset;
10806 /* Handle the situations where a float is taking up the first half
10807 of the GPR, and the other half is empty (typically due to
10808 alignment restrictions). We can detect this by a 8-byte-aligned
10809 int field, or by seeing that this is the final flush for this
10810 argument. Count the word and continue on. */
10811 if (cum->floats_in_gpr == 1
10812 && (cum->intoffset % 64 == 0
10813 || (cum->intoffset == -1 && final)))
10815 cum->words++;
10816 cum->floats_in_gpr = 0;
10819 if (cum->intoffset == -1)
10820 return;
10822 intoffset = cum->intoffset;
10823 cum->intoffset = -1;
10824 cum->floats_in_gpr = 0;
10826 if (intoffset % BITS_PER_WORD != 0)
10828 unsigned int bits = BITS_PER_WORD - intoffset % BITS_PER_WORD;
10829 if (!int_mode_for_size (bits, 0).exists ())
10831 /* We couldn't find an appropriate mode, which happens,
10832 e.g., in packed structs when there are 3 bytes to load.
10833 Back intoffset back to the beginning of the word in this
10834 case. */
10835 intoffset = ROUND_DOWN (intoffset, BITS_PER_WORD);
10839 startbit = ROUND_DOWN (intoffset, BITS_PER_WORD);
10840 endbit = ROUND_UP (bitpos, BITS_PER_WORD);
10841 intregs = (endbit - startbit) / BITS_PER_WORD;
10842 cum->words += intregs;
10843 /* words should be unsigned. */
10844 if ((unsigned)cum->words < (endbit/BITS_PER_WORD))
10846 int pad = (endbit/BITS_PER_WORD) - cum->words;
10847 cum->words += pad;
10851 /* The darwin64 ABI calls for us to recurse down through structs,
10852 looking for elements passed in registers. Unfortunately, we have
10853 to track int register count here also because of misalignments
10854 in powerpc alignment mode. */
10856 static void
10857 rs6000_darwin64_record_arg_advance_recurse (CUMULATIVE_ARGS *cum,
10858 const_tree type,
10859 HOST_WIDE_INT startbitpos)
10861 tree f;
10863 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
10864 if (TREE_CODE (f) == FIELD_DECL)
10866 HOST_WIDE_INT bitpos = startbitpos;
10867 tree ftype = TREE_TYPE (f);
10868 machine_mode mode;
10869 if (ftype == error_mark_node)
10870 continue;
10871 mode = TYPE_MODE (ftype);
10873 if (DECL_SIZE (f) != 0
10874 && tree_fits_uhwi_p (bit_position (f)))
10875 bitpos += int_bit_position (f);
10877 /* ??? FIXME: else assume zero offset. */
10879 if (TREE_CODE (ftype) == RECORD_TYPE)
10880 rs6000_darwin64_record_arg_advance_recurse (cum, ftype, bitpos);
10881 else if (USE_FP_FOR_ARG_P (cum, mode))
10883 unsigned n_fpregs = (GET_MODE_SIZE (mode) + 7) >> 3;
10884 rs6000_darwin64_record_arg_advance_flush (cum, bitpos, 0);
10885 cum->fregno += n_fpregs;
10886 /* Single-precision floats present a special problem for
10887 us, because they are smaller than an 8-byte GPR, and so
10888 the structure-packing rules combined with the standard
10889 varargs behavior mean that we want to pack float/float
10890 and float/int combinations into a single register's
10891 space. This is complicated by the arg advance flushing,
10892 which works on arbitrarily large groups of int-type
10893 fields. */
10894 if (mode == SFmode)
10896 if (cum->floats_in_gpr == 1)
10898 /* Two floats in a word; count the word and reset
10899 the float count. */
10900 cum->words++;
10901 cum->floats_in_gpr = 0;
10903 else if (bitpos % 64 == 0)
10905 /* A float at the beginning of an 8-byte word;
10906 count it and put off adjusting cum->words until
10907 we see if a arg advance flush is going to do it
10908 for us. */
10909 cum->floats_in_gpr++;
10911 else
10913 /* The float is at the end of a word, preceded
10914 by integer fields, so the arg advance flush
10915 just above has already set cum->words and
10916 everything is taken care of. */
10919 else
10920 cum->words += n_fpregs;
10922 else if (USE_ALTIVEC_FOR_ARG_P (cum, mode, 1))
10924 rs6000_darwin64_record_arg_advance_flush (cum, bitpos, 0);
10925 cum->vregno++;
10926 cum->words += 2;
10928 else if (cum->intoffset == -1)
10929 cum->intoffset = bitpos;
10933 /* Check for an item that needs to be considered specially under the darwin 64
10934 bit ABI. These are record types where the mode is BLK or the structure is
10935 8 bytes in size. */
10936 static int
10937 rs6000_darwin64_struct_check_p (machine_mode mode, const_tree type)
10939 return rs6000_darwin64_abi
10940 && ((mode == BLKmode
10941 && TREE_CODE (type) == RECORD_TYPE
10942 && int_size_in_bytes (type) > 0)
10943 || (type && TREE_CODE (type) == RECORD_TYPE
10944 && int_size_in_bytes (type) == 8)) ? 1 : 0;
10947 /* Update the data in CUM to advance over an argument
10948 of mode MODE and data type TYPE.
10949 (TYPE is null for libcalls where that information may not be available.)
10951 Note that for args passed by reference, function_arg will be called
10952 with MODE and TYPE set to that of the pointer to the arg, not the arg
10953 itself. */
10955 static void
10956 rs6000_function_arg_advance_1 (CUMULATIVE_ARGS *cum, machine_mode mode,
10957 const_tree type, bool named, int depth)
10959 machine_mode elt_mode;
10960 int n_elts;
10962 rs6000_discover_homogeneous_aggregate (mode, type, &elt_mode, &n_elts);
10964 /* Only tick off an argument if we're not recursing. */
10965 if (depth == 0)
10966 cum->nargs_prototype--;
10968 #ifdef HAVE_AS_GNU_ATTRIBUTE
10969 if (TARGET_ELF && (TARGET_64BIT || DEFAULT_ABI == ABI_V4)
10970 && cum->escapes)
10972 if (SCALAR_FLOAT_MODE_P (mode))
10974 rs6000_passes_float = true;
10975 if ((HAVE_LD_PPC_GNU_ATTR_LONG_DOUBLE || TARGET_64BIT)
10976 && (FLOAT128_IBM_P (mode)
10977 || FLOAT128_IEEE_P (mode)
10978 || (type != NULL
10979 && TYPE_MAIN_VARIANT (type) == long_double_type_node)))
10980 rs6000_passes_long_double = true;
10982 /* Note if we passed or return a IEEE 128-bit type. We changed the
10983 mangling for these types, and we may need to make an alias with
10984 the old mangling. */
10985 if (FLOAT128_IEEE_P (mode))
10986 rs6000_passes_ieee128 = true;
10988 if (named && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
10989 rs6000_passes_vector = true;
10991 #endif
10993 if (TARGET_ALTIVEC_ABI
10994 && (ALTIVEC_OR_VSX_VECTOR_MODE (elt_mode)
10995 || (type && TREE_CODE (type) == VECTOR_TYPE
10996 && int_size_in_bytes (type) == 16)))
10998 bool stack = false;
11000 if (USE_ALTIVEC_FOR_ARG_P (cum, elt_mode, named))
11002 cum->vregno += n_elts;
11004 if (!TARGET_ALTIVEC)
11005 error ("cannot pass argument in vector register because"
11006 " altivec instructions are disabled, use %qs"
11007 " to enable them", "-maltivec");
11009 /* PowerPC64 Linux and AIX allocate GPRs for a vector argument
11010 even if it is going to be passed in a vector register.
11011 Darwin does the same for variable-argument functions. */
11012 if (((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
11013 && TARGET_64BIT)
11014 || (cum->stdarg && DEFAULT_ABI != ABI_V4))
11015 stack = true;
11017 else
11018 stack = true;
11020 if (stack)
11022 int align;
11024 /* Vector parameters must be 16-byte aligned. In 32-bit
11025 mode this means we need to take into account the offset
11026 to the parameter save area. In 64-bit mode, they just
11027 have to start on an even word, since the parameter save
11028 area is 16-byte aligned. */
11029 if (TARGET_32BIT)
11030 align = -(rs6000_parm_offset () + cum->words) & 3;
11031 else
11032 align = cum->words & 1;
11033 cum->words += align + rs6000_arg_size (mode, type);
11035 if (TARGET_DEBUG_ARG)
11037 fprintf (stderr, "function_adv: words = %2d, align=%d, ",
11038 cum->words, align);
11039 fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s\n",
11040 cum->nargs_prototype, cum->prototype,
11041 GET_MODE_NAME (mode));
11045 else if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
11047 int size = int_size_in_bytes (type);
11048 /* Variable sized types have size == -1 and are
11049 treated as if consisting entirely of ints.
11050 Pad to 16 byte boundary if needed. */
11051 if (TYPE_ALIGN (type) >= 2 * BITS_PER_WORD
11052 && (cum->words % 2) != 0)
11053 cum->words++;
11054 /* For varargs, we can just go up by the size of the struct. */
11055 if (!named)
11056 cum->words += (size + 7) / 8;
11057 else
11059 /* It is tempting to say int register count just goes up by
11060 sizeof(type)/8, but this is wrong in a case such as
11061 { int; double; int; } [powerpc alignment]. We have to
11062 grovel through the fields for these too. */
11063 cum->intoffset = 0;
11064 cum->floats_in_gpr = 0;
11065 rs6000_darwin64_record_arg_advance_recurse (cum, type, 0);
11066 rs6000_darwin64_record_arg_advance_flush (cum,
11067 size * BITS_PER_UNIT, 1);
11069 if (TARGET_DEBUG_ARG)
11071 fprintf (stderr, "function_adv: words = %2d, align=%d, size=%d",
11072 cum->words, TYPE_ALIGN (type), size);
11073 fprintf (stderr,
11074 "nargs = %4d, proto = %d, mode = %4s (darwin64 abi)\n",
11075 cum->nargs_prototype, cum->prototype,
11076 GET_MODE_NAME (mode));
11079 else if (DEFAULT_ABI == ABI_V4)
11081 if (abi_v4_pass_in_fpr (mode, named))
11083 /* _Decimal128 must use an even/odd register pair. This assumes
11084 that the register number is odd when fregno is odd. */
11085 if (mode == TDmode && (cum->fregno % 2) == 1)
11086 cum->fregno++;
11088 if (cum->fregno + (FLOAT128_2REG_P (mode) ? 1 : 0)
11089 <= FP_ARG_V4_MAX_REG)
11090 cum->fregno += (GET_MODE_SIZE (mode) + 7) >> 3;
11091 else
11093 cum->fregno = FP_ARG_V4_MAX_REG + 1;
11094 if (mode == DFmode || FLOAT128_IBM_P (mode)
11095 || mode == DDmode || mode == TDmode)
11096 cum->words += cum->words & 1;
11097 cum->words += rs6000_arg_size (mode, type);
11100 else
11102 int n_words = rs6000_arg_size (mode, type);
11103 int gregno = cum->sysv_gregno;
11105 /* Long long is put in (r3,r4), (r5,r6), (r7,r8) or (r9,r10).
11106 As does any other 2 word item such as complex int due to a
11107 historical mistake. */
11108 if (n_words == 2)
11109 gregno += (1 - gregno) & 1;
11111 /* Multi-reg args are not split between registers and stack. */
11112 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
11114 /* Long long is aligned on the stack. So are other 2 word
11115 items such as complex int due to a historical mistake. */
11116 if (n_words == 2)
11117 cum->words += cum->words & 1;
11118 cum->words += n_words;
11121 /* Note: continuing to accumulate gregno past when we've started
11122 spilling to the stack indicates the fact that we've started
11123 spilling to the stack to expand_builtin_saveregs. */
11124 cum->sysv_gregno = gregno + n_words;
11127 if (TARGET_DEBUG_ARG)
11129 fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
11130 cum->words, cum->fregno);
11131 fprintf (stderr, "gregno = %2d, nargs = %4d, proto = %d, ",
11132 cum->sysv_gregno, cum->nargs_prototype, cum->prototype);
11133 fprintf (stderr, "mode = %4s, named = %d\n",
11134 GET_MODE_NAME (mode), named);
11137 else
11139 int n_words = rs6000_arg_size (mode, type);
11140 int start_words = cum->words;
11141 int align_words = rs6000_parm_start (mode, type, start_words);
11143 cum->words = align_words + n_words;
11145 if (SCALAR_FLOAT_MODE_P (elt_mode) && TARGET_HARD_FLOAT)
11147 /* _Decimal128 must be passed in an even/odd float register pair.
11148 This assumes that the register number is odd when fregno is
11149 odd. */
11150 if (elt_mode == TDmode && (cum->fregno % 2) == 1)
11151 cum->fregno++;
11152 cum->fregno += n_elts * ((GET_MODE_SIZE (elt_mode) + 7) >> 3);
11155 if (TARGET_DEBUG_ARG)
11157 fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
11158 cum->words, cum->fregno);
11159 fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s, ",
11160 cum->nargs_prototype, cum->prototype, GET_MODE_NAME (mode));
11161 fprintf (stderr, "named = %d, align = %d, depth = %d\n",
11162 named, align_words - start_words, depth);
11167 static void
11168 rs6000_function_arg_advance (cumulative_args_t cum, machine_mode mode,
11169 const_tree type, bool named)
11171 rs6000_function_arg_advance_1 (get_cumulative_args (cum), mode, type, named,
11175 /* A subroutine of rs6000_darwin64_record_arg. Assign the bits of the
11176 structure between cum->intoffset and bitpos to integer registers. */
11178 static void
11179 rs6000_darwin64_record_arg_flush (CUMULATIVE_ARGS *cum,
11180 HOST_WIDE_INT bitpos, rtx rvec[], int *k)
11182 machine_mode mode;
11183 unsigned int regno;
11184 unsigned int startbit, endbit;
11185 int this_regno, intregs, intoffset;
11186 rtx reg;
11188 if (cum->intoffset == -1)
11189 return;
11191 intoffset = cum->intoffset;
11192 cum->intoffset = -1;
11194 /* If this is the trailing part of a word, try to only load that
11195 much into the register. Otherwise load the whole register. Note
11196 that in the latter case we may pick up unwanted bits. It's not a
11197 problem at the moment but may wish to revisit. */
11199 if (intoffset % BITS_PER_WORD != 0)
11201 unsigned int bits = BITS_PER_WORD - intoffset % BITS_PER_WORD;
11202 if (!int_mode_for_size (bits, 0).exists (&mode))
11204 /* We couldn't find an appropriate mode, which happens,
11205 e.g., in packed structs when there are 3 bytes to load.
11206 Back intoffset back to the beginning of the word in this
11207 case. */
11208 intoffset = ROUND_DOWN (intoffset, BITS_PER_WORD);
11209 mode = word_mode;
11212 else
11213 mode = word_mode;
11215 startbit = ROUND_DOWN (intoffset, BITS_PER_WORD);
11216 endbit = ROUND_UP (bitpos, BITS_PER_WORD);
11217 intregs = (endbit - startbit) / BITS_PER_WORD;
11218 this_regno = cum->words + intoffset / BITS_PER_WORD;
11220 if (intregs > 0 && intregs > GP_ARG_NUM_REG - this_regno)
11221 cum->use_stack = 1;
11223 intregs = MIN (intregs, GP_ARG_NUM_REG - this_regno);
11224 if (intregs <= 0)
11225 return;
11227 intoffset /= BITS_PER_UNIT;
11230 regno = GP_ARG_MIN_REG + this_regno;
11231 reg = gen_rtx_REG (mode, regno);
11232 rvec[(*k)++] =
11233 gen_rtx_EXPR_LIST (VOIDmode, reg, GEN_INT (intoffset));
11235 this_regno += 1;
11236 intoffset = (intoffset | (UNITS_PER_WORD-1)) + 1;
11237 mode = word_mode;
11238 intregs -= 1;
11240 while (intregs > 0);
11243 /* Recursive workhorse for the following. */
11245 static void
11246 rs6000_darwin64_record_arg_recurse (CUMULATIVE_ARGS *cum, const_tree type,
11247 HOST_WIDE_INT startbitpos, rtx rvec[],
11248 int *k)
11250 tree f;
11252 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
11253 if (TREE_CODE (f) == FIELD_DECL)
11255 HOST_WIDE_INT bitpos = startbitpos;
11256 tree ftype = TREE_TYPE (f);
11257 machine_mode mode;
11258 if (ftype == error_mark_node)
11259 continue;
11260 mode = TYPE_MODE (ftype);
11262 if (DECL_SIZE (f) != 0
11263 && tree_fits_uhwi_p (bit_position (f)))
11264 bitpos += int_bit_position (f);
11266 /* ??? FIXME: else assume zero offset. */
11268 if (TREE_CODE (ftype) == RECORD_TYPE)
11269 rs6000_darwin64_record_arg_recurse (cum, ftype, bitpos, rvec, k);
11270 else if (cum->named && USE_FP_FOR_ARG_P (cum, mode))
11272 unsigned n_fpreg = (GET_MODE_SIZE (mode) + 7) >> 3;
11273 #if 0
11274 switch (mode)
11276 case E_SCmode: mode = SFmode; break;
11277 case E_DCmode: mode = DFmode; break;
11278 case E_TCmode: mode = TFmode; break;
11279 default: break;
11281 #endif
11282 rs6000_darwin64_record_arg_flush (cum, bitpos, rvec, k);
11283 if (cum->fregno + n_fpreg > FP_ARG_MAX_REG + 1)
11285 gcc_assert (cum->fregno == FP_ARG_MAX_REG
11286 && (mode == TFmode || mode == TDmode));
11287 /* Long double or _Decimal128 split over regs and memory. */
11288 mode = DECIMAL_FLOAT_MODE_P (mode) ? DDmode : DFmode;
11289 cum->use_stack=1;
11291 rvec[(*k)++]
11292 = gen_rtx_EXPR_LIST (VOIDmode,
11293 gen_rtx_REG (mode, cum->fregno++),
11294 GEN_INT (bitpos / BITS_PER_UNIT));
11295 if (FLOAT128_2REG_P (mode))
11296 cum->fregno++;
11298 else if (cum->named && USE_ALTIVEC_FOR_ARG_P (cum, mode, 1))
11300 rs6000_darwin64_record_arg_flush (cum, bitpos, rvec, k);
11301 rvec[(*k)++]
11302 = gen_rtx_EXPR_LIST (VOIDmode,
11303 gen_rtx_REG (mode, cum->vregno++),
11304 GEN_INT (bitpos / BITS_PER_UNIT));
11306 else if (cum->intoffset == -1)
11307 cum->intoffset = bitpos;
11311 /* For the darwin64 ABI, we want to construct a PARALLEL consisting of
11312 the register(s) to be used for each field and subfield of a struct
11313 being passed by value, along with the offset of where the
11314 register's value may be found in the block. FP fields go in FP
11315 register, vector fields go in vector registers, and everything
11316 else goes in int registers, packed as in memory.
11318 This code is also used for function return values. RETVAL indicates
11319 whether this is the case.
11321 Much of this is taken from the SPARC V9 port, which has a similar
11322 calling convention. */
11324 static rtx
11325 rs6000_darwin64_record_arg (CUMULATIVE_ARGS *orig_cum, const_tree type,
11326 bool named, bool retval)
11328 rtx rvec[FIRST_PSEUDO_REGISTER];
11329 int k = 1, kbase = 1;
11330 HOST_WIDE_INT typesize = int_size_in_bytes (type);
11331 /* This is a copy; modifications are not visible to our caller. */
11332 CUMULATIVE_ARGS copy_cum = *orig_cum;
11333 CUMULATIVE_ARGS *cum = &copy_cum;
11335 /* Pad to 16 byte boundary if needed. */
11336 if (!retval && TYPE_ALIGN (type) >= 2 * BITS_PER_WORD
11337 && (cum->words % 2) != 0)
11338 cum->words++;
11340 cum->intoffset = 0;
11341 cum->use_stack = 0;
11342 cum->named = named;
11344 /* Put entries into rvec[] for individual FP and vector fields, and
11345 for the chunks of memory that go in int regs. Note we start at
11346 element 1; 0 is reserved for an indication of using memory, and
11347 may or may not be filled in below. */
11348 rs6000_darwin64_record_arg_recurse (cum, type, /* startbit pos= */ 0, rvec, &k);
11349 rs6000_darwin64_record_arg_flush (cum, typesize * BITS_PER_UNIT, rvec, &k);
11351 /* If any part of the struct went on the stack put all of it there.
11352 This hack is because the generic code for
11353 FUNCTION_ARG_PARTIAL_NREGS cannot handle cases where the register
11354 parts of the struct are not at the beginning. */
11355 if (cum->use_stack)
11357 if (retval)
11358 return NULL_RTX; /* doesn't go in registers at all */
11359 kbase = 0;
11360 rvec[0] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
11362 if (k > 1 || cum->use_stack)
11363 return gen_rtx_PARALLEL (BLKmode, gen_rtvec_v (k - kbase, &rvec[kbase]));
11364 else
11365 return NULL_RTX;
11368 /* Determine where to place an argument in 64-bit mode with 32-bit ABI. */
11370 static rtx
11371 rs6000_mixed_function_arg (machine_mode mode, const_tree type,
11372 int align_words)
11374 int n_units;
11375 int i, k;
11376 rtx rvec[GP_ARG_NUM_REG + 1];
11378 if (align_words >= GP_ARG_NUM_REG)
11379 return NULL_RTX;
11381 n_units = rs6000_arg_size (mode, type);
11383 /* Optimize the simple case where the arg fits in one gpr, except in
11384 the case of BLKmode due to assign_parms assuming that registers are
11385 BITS_PER_WORD wide. */
11386 if (n_units == 0
11387 || (n_units == 1 && mode != BLKmode))
11388 return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
11390 k = 0;
11391 if (align_words + n_units > GP_ARG_NUM_REG)
11392 /* Not all of the arg fits in gprs. Say that it goes in memory too,
11393 using a magic NULL_RTX component.
11394 This is not strictly correct. Only some of the arg belongs in
11395 memory, not all of it. However, the normal scheme using
11396 function_arg_partial_nregs can result in unusual subregs, eg.
11397 (subreg:SI (reg:DF) 4), which are not handled well. The code to
11398 store the whole arg to memory is often more efficient than code
11399 to store pieces, and we know that space is available in the right
11400 place for the whole arg. */
11401 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
11403 i = 0;
11406 rtx r = gen_rtx_REG (SImode, GP_ARG_MIN_REG + align_words);
11407 rtx off = GEN_INT (i++ * 4);
11408 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
11410 while (++align_words < GP_ARG_NUM_REG && --n_units != 0);
11412 return gen_rtx_PARALLEL (mode, gen_rtvec_v (k, rvec));
11415 /* We have an argument of MODE and TYPE that goes into FPRs or VRs,
11416 but must also be copied into the parameter save area starting at
11417 offset ALIGN_WORDS. Fill in RVEC with the elements corresponding
11418 to the GPRs and/or memory. Return the number of elements used. */
11420 static int
11421 rs6000_psave_function_arg (machine_mode mode, const_tree type,
11422 int align_words, rtx *rvec)
11424 int k = 0;
11426 if (align_words < GP_ARG_NUM_REG)
11428 int n_words = rs6000_arg_size (mode, type);
11430 if (align_words + n_words > GP_ARG_NUM_REG
11431 || mode == BLKmode
11432 || (TARGET_32BIT && TARGET_POWERPC64))
11434 /* If this is partially on the stack, then we only
11435 include the portion actually in registers here. */
11436 machine_mode rmode = TARGET_32BIT ? SImode : DImode;
11437 int i = 0;
11439 if (align_words + n_words > GP_ARG_NUM_REG)
11441 /* Not all of the arg fits in gprs. Say that it goes in memory
11442 too, using a magic NULL_RTX component. Also see comment in
11443 rs6000_mixed_function_arg for why the normal
11444 function_arg_partial_nregs scheme doesn't work in this case. */
11445 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
11450 rtx r = gen_rtx_REG (rmode, GP_ARG_MIN_REG + align_words);
11451 rtx off = GEN_INT (i++ * GET_MODE_SIZE (rmode));
11452 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
11454 while (++align_words < GP_ARG_NUM_REG && --n_words != 0);
11456 else
11458 /* The whole arg fits in gprs. */
11459 rtx r = gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
11460 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, const0_rtx);
11463 else
11465 /* It's entirely in memory. */
11466 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
11469 return k;
11472 /* RVEC is a vector of K components of an argument of mode MODE.
11473 Construct the final function_arg return value from it. */
11475 static rtx
11476 rs6000_finish_function_arg (machine_mode mode, rtx *rvec, int k)
11478 gcc_assert (k >= 1);
11480 /* Avoid returning a PARALLEL in the trivial cases. */
11481 if (k == 1)
11483 if (XEXP (rvec[0], 0) == NULL_RTX)
11484 return NULL_RTX;
11486 if (GET_MODE (XEXP (rvec[0], 0)) == mode)
11487 return XEXP (rvec[0], 0);
11490 return gen_rtx_PARALLEL (mode, gen_rtvec_v (k, rvec));
11493 /* Determine where to put an argument to a function.
11494 Value is zero to push the argument on the stack,
11495 or a hard register in which to store the argument.
11497 MODE is the argument's machine mode.
11498 TYPE is the data type of the argument (as a tree).
11499 This is null for libcalls where that information may
11500 not be available.
11501 CUM is a variable of type CUMULATIVE_ARGS which gives info about
11502 the preceding args and about the function being called. It is
11503 not modified in this routine.
11504 NAMED is nonzero if this argument is a named parameter
11505 (otherwise it is an extra parameter matching an ellipsis).
11507 On RS/6000 the first eight words of non-FP are normally in registers
11508 and the rest are pushed. Under AIX, the first 13 FP args are in registers.
11509 Under V.4, the first 8 FP args are in registers.
11511 If this is floating-point and no prototype is specified, we use
11512 both an FP and integer register (or possibly FP reg and stack). Library
11513 functions (when CALL_LIBCALL is set) always have the proper types for args,
11514 so we can pass the FP value just in one register. emit_library_function
11515 doesn't support PARALLEL anyway.
11517 Note that for args passed by reference, function_arg will be called
11518 with MODE and TYPE set to that of the pointer to the arg, not the arg
11519 itself. */
11521 static rtx
11522 rs6000_function_arg (cumulative_args_t cum_v, machine_mode mode,
11523 const_tree type, bool named)
11525 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
11526 enum rs6000_abi abi = DEFAULT_ABI;
11527 machine_mode elt_mode;
11528 int n_elts;
11530 /* Return a marker to indicate whether CR1 needs to set or clear the
11531 bit that V.4 uses to say fp args were passed in registers.
11532 Assume that we don't need the marker for software floating point,
11533 or compiler generated library calls. */
11534 if (mode == VOIDmode)
11536 if (abi == ABI_V4
11537 && (cum->call_cookie & CALL_LIBCALL) == 0
11538 && (cum->stdarg
11539 || (cum->nargs_prototype < 0
11540 && (cum->prototype || TARGET_NO_PROTOTYPE)))
11541 && TARGET_HARD_FLOAT)
11542 return GEN_INT (cum->call_cookie
11543 | ((cum->fregno == FP_ARG_MIN_REG)
11544 ? CALL_V4_SET_FP_ARGS
11545 : CALL_V4_CLEAR_FP_ARGS));
11547 return GEN_INT (cum->call_cookie & ~CALL_LIBCALL);
11550 rs6000_discover_homogeneous_aggregate (mode, type, &elt_mode, &n_elts);
11552 if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
11554 rtx rslt = rs6000_darwin64_record_arg (cum, type, named, /*retval= */false);
11555 if (rslt != NULL_RTX)
11556 return rslt;
11557 /* Else fall through to usual handling. */
11560 if (USE_ALTIVEC_FOR_ARG_P (cum, elt_mode, named))
11562 rtx rvec[GP_ARG_NUM_REG + AGGR_ARG_NUM_REG + 1];
11563 rtx r, off;
11564 int i, k = 0;
11566 /* Do we also need to pass this argument in the parameter save area?
11567 Library support functions for IEEE 128-bit are assumed to not need the
11568 value passed both in GPRs and in vector registers. */
11569 if (TARGET_64BIT && !cum->prototype
11570 && (!cum->libcall || !FLOAT128_VECTOR_P (elt_mode)))
11572 int align_words = ROUND_UP (cum->words, 2);
11573 k = rs6000_psave_function_arg (mode, type, align_words, rvec);
11576 /* Describe where this argument goes in the vector registers. */
11577 for (i = 0; i < n_elts && cum->vregno + i <= ALTIVEC_ARG_MAX_REG; i++)
11579 r = gen_rtx_REG (elt_mode, cum->vregno + i);
11580 off = GEN_INT (i * GET_MODE_SIZE (elt_mode));
11581 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
11584 return rs6000_finish_function_arg (mode, rvec, k);
11586 else if (TARGET_ALTIVEC_ABI
11587 && (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
11588 || (type && TREE_CODE (type) == VECTOR_TYPE
11589 && int_size_in_bytes (type) == 16)))
11591 if (named || abi == ABI_V4)
11592 return NULL_RTX;
11593 else
11595 /* Vector parameters to varargs functions under AIX or Darwin
11596 get passed in memory and possibly also in GPRs. */
11597 int align, align_words, n_words;
11598 machine_mode part_mode;
11600 /* Vector parameters must be 16-byte aligned. In 32-bit
11601 mode this means we need to take into account the offset
11602 to the parameter save area. In 64-bit mode, they just
11603 have to start on an even word, since the parameter save
11604 area is 16-byte aligned. */
11605 if (TARGET_32BIT)
11606 align = -(rs6000_parm_offset () + cum->words) & 3;
11607 else
11608 align = cum->words & 1;
11609 align_words = cum->words + align;
11611 /* Out of registers? Memory, then. */
11612 if (align_words >= GP_ARG_NUM_REG)
11613 return NULL_RTX;
11615 if (TARGET_32BIT && TARGET_POWERPC64)
11616 return rs6000_mixed_function_arg (mode, type, align_words);
11618 /* The vector value goes in GPRs. Only the part of the
11619 value in GPRs is reported here. */
11620 part_mode = mode;
11621 n_words = rs6000_arg_size (mode, type);
11622 if (align_words + n_words > GP_ARG_NUM_REG)
11623 /* Fortunately, there are only two possibilities, the value
11624 is either wholly in GPRs or half in GPRs and half not. */
11625 part_mode = DImode;
11627 return gen_rtx_REG (part_mode, GP_ARG_MIN_REG + align_words);
11631 else if (abi == ABI_V4)
11633 if (abi_v4_pass_in_fpr (mode, named))
11635 /* _Decimal128 must use an even/odd register pair. This assumes
11636 that the register number is odd when fregno is odd. */
11637 if (mode == TDmode && (cum->fregno % 2) == 1)
11638 cum->fregno++;
11640 if (cum->fregno + (FLOAT128_2REG_P (mode) ? 1 : 0)
11641 <= FP_ARG_V4_MAX_REG)
11642 return gen_rtx_REG (mode, cum->fregno);
11643 else
11644 return NULL_RTX;
11646 else
11648 int n_words = rs6000_arg_size (mode, type);
11649 int gregno = cum->sysv_gregno;
11651 /* Long long is put in (r3,r4), (r5,r6), (r7,r8) or (r9,r10).
11652 As does any other 2 word item such as complex int due to a
11653 historical mistake. */
11654 if (n_words == 2)
11655 gregno += (1 - gregno) & 1;
11657 /* Multi-reg args are not split between registers and stack. */
11658 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
11659 return NULL_RTX;
11661 if (TARGET_32BIT && TARGET_POWERPC64)
11662 return rs6000_mixed_function_arg (mode, type,
11663 gregno - GP_ARG_MIN_REG);
11664 return gen_rtx_REG (mode, gregno);
11667 else
11669 int align_words = rs6000_parm_start (mode, type, cum->words);
11671 /* _Decimal128 must be passed in an even/odd float register pair.
11672 This assumes that the register number is odd when fregno is odd. */
11673 if (elt_mode == TDmode && (cum->fregno % 2) == 1)
11674 cum->fregno++;
11676 if (USE_FP_FOR_ARG_P (cum, elt_mode)
11677 && !(TARGET_AIX && !TARGET_ELF
11678 && type != NULL && AGGREGATE_TYPE_P (type)))
11680 rtx rvec[GP_ARG_NUM_REG + AGGR_ARG_NUM_REG + 1];
11681 rtx r, off;
11682 int i, k = 0;
11683 unsigned long n_fpreg = (GET_MODE_SIZE (elt_mode) + 7) >> 3;
11684 int fpr_words;
11686 /* Do we also need to pass this argument in the parameter
11687 save area? */
11688 if (type && (cum->nargs_prototype <= 0
11689 || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
11690 && TARGET_XL_COMPAT
11691 && align_words >= GP_ARG_NUM_REG)))
11692 k = rs6000_psave_function_arg (mode, type, align_words, rvec);
11694 /* Describe where this argument goes in the fprs. */
11695 for (i = 0; i < n_elts
11696 && cum->fregno + i * n_fpreg <= FP_ARG_MAX_REG; i++)
11698 /* Check if the argument is split over registers and memory.
11699 This can only ever happen for long double or _Decimal128;
11700 complex types are handled via split_complex_arg. */
11701 machine_mode fmode = elt_mode;
11702 if (cum->fregno + (i + 1) * n_fpreg > FP_ARG_MAX_REG + 1)
11704 gcc_assert (FLOAT128_2REG_P (fmode));
11705 fmode = DECIMAL_FLOAT_MODE_P (fmode) ? DDmode : DFmode;
11708 r = gen_rtx_REG (fmode, cum->fregno + i * n_fpreg);
11709 off = GEN_INT (i * GET_MODE_SIZE (elt_mode));
11710 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
11713 /* If there were not enough FPRs to hold the argument, the rest
11714 usually goes into memory. However, if the current position
11715 is still within the register parameter area, a portion may
11716 actually have to go into GPRs.
11718 Note that it may happen that the portion of the argument
11719 passed in the first "half" of the first GPR was already
11720 passed in the last FPR as well.
11722 For unnamed arguments, we already set up GPRs to cover the
11723 whole argument in rs6000_psave_function_arg, so there is
11724 nothing further to do at this point. */
11725 fpr_words = (i * GET_MODE_SIZE (elt_mode)) / (TARGET_32BIT ? 4 : 8);
11726 if (i < n_elts && align_words + fpr_words < GP_ARG_NUM_REG
11727 && cum->nargs_prototype > 0)
11729 static bool warned;
11731 machine_mode rmode = TARGET_32BIT ? SImode : DImode;
11732 int n_words = rs6000_arg_size (mode, type);
11734 align_words += fpr_words;
11735 n_words -= fpr_words;
11739 r = gen_rtx_REG (rmode, GP_ARG_MIN_REG + align_words);
11740 off = GEN_INT (fpr_words++ * GET_MODE_SIZE (rmode));
11741 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
11743 while (++align_words < GP_ARG_NUM_REG && --n_words != 0);
11745 if (!warned && warn_psabi)
11747 warned = true;
11748 inform (input_location,
11749 "the ABI of passing homogeneous %<float%> aggregates"
11750 " has changed in GCC 5");
11754 return rs6000_finish_function_arg (mode, rvec, k);
11756 else if (align_words < GP_ARG_NUM_REG)
11758 if (TARGET_32BIT && TARGET_POWERPC64)
11759 return rs6000_mixed_function_arg (mode, type, align_words);
11761 return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
11763 else
11764 return NULL_RTX;
11768 /* For an arg passed partly in registers and partly in memory, this is
11769 the number of bytes passed in registers. For args passed entirely in
11770 registers or entirely in memory, zero. When an arg is described by a
11771 PARALLEL, perhaps using more than one register type, this function
11772 returns the number of bytes used by the first element of the PARALLEL. */
11774 static int
11775 rs6000_arg_partial_bytes (cumulative_args_t cum_v, machine_mode mode,
11776 tree type, bool named)
11778 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
11779 bool passed_in_gprs = true;
11780 int ret = 0;
11781 int align_words;
11782 machine_mode elt_mode;
11783 int n_elts;
11785 rs6000_discover_homogeneous_aggregate (mode, type, &elt_mode, &n_elts);
11787 if (DEFAULT_ABI == ABI_V4)
11788 return 0;
11790 if (USE_ALTIVEC_FOR_ARG_P (cum, elt_mode, named))
11792 /* If we are passing this arg in the fixed parameter save area (gprs or
11793 memory) as well as VRs, we do not use the partial bytes mechanism;
11794 instead, rs6000_function_arg will return a PARALLEL including a memory
11795 element as necessary. Library support functions for IEEE 128-bit are
11796 assumed to not need the value passed both in GPRs and in vector
11797 registers. */
11798 if (TARGET_64BIT && !cum->prototype
11799 && (!cum->libcall || !FLOAT128_VECTOR_P (elt_mode)))
11800 return 0;
11802 /* Otherwise, we pass in VRs only. Check for partial copies. */
11803 passed_in_gprs = false;
11804 if (cum->vregno + n_elts > ALTIVEC_ARG_MAX_REG + 1)
11805 ret = (ALTIVEC_ARG_MAX_REG + 1 - cum->vregno) * 16;
11808 /* In this complicated case we just disable the partial_nregs code. */
11809 if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
11810 return 0;
11812 align_words = rs6000_parm_start (mode, type, cum->words);
11814 if (USE_FP_FOR_ARG_P (cum, elt_mode)
11815 && !(TARGET_AIX && !TARGET_ELF
11816 && type != NULL && AGGREGATE_TYPE_P (type)))
11818 unsigned long n_fpreg = (GET_MODE_SIZE (elt_mode) + 7) >> 3;
11820 /* If we are passing this arg in the fixed parameter save area
11821 (gprs or memory) as well as FPRs, we do not use the partial
11822 bytes mechanism; instead, rs6000_function_arg will return a
11823 PARALLEL including a memory element as necessary. */
11824 if (type
11825 && (cum->nargs_prototype <= 0
11826 || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
11827 && TARGET_XL_COMPAT
11828 && align_words >= GP_ARG_NUM_REG)))
11829 return 0;
11831 /* Otherwise, we pass in FPRs only. Check for partial copies. */
11832 passed_in_gprs = false;
11833 if (cum->fregno + n_elts * n_fpreg > FP_ARG_MAX_REG + 1)
11835 /* Compute number of bytes / words passed in FPRs. If there
11836 is still space available in the register parameter area
11837 *after* that amount, a part of the argument will be passed
11838 in GPRs. In that case, the total amount passed in any
11839 registers is equal to the amount that would have been passed
11840 in GPRs if everything were passed there, so we fall back to
11841 the GPR code below to compute the appropriate value. */
11842 int fpr = ((FP_ARG_MAX_REG + 1 - cum->fregno)
11843 * MIN (8, GET_MODE_SIZE (elt_mode)));
11844 int fpr_words = fpr / (TARGET_32BIT ? 4 : 8);
11846 if (align_words + fpr_words < GP_ARG_NUM_REG)
11847 passed_in_gprs = true;
11848 else
11849 ret = fpr;
11853 if (passed_in_gprs
11854 && align_words < GP_ARG_NUM_REG
11855 && GP_ARG_NUM_REG < align_words + rs6000_arg_size (mode, type))
11856 ret = (GP_ARG_NUM_REG - align_words) * (TARGET_32BIT ? 4 : 8);
11858 if (ret != 0 && TARGET_DEBUG_ARG)
11859 fprintf (stderr, "rs6000_arg_partial_bytes: %d\n", ret);
11861 return ret;
11864 /* A C expression that indicates when an argument must be passed by
11865 reference. If nonzero for an argument, a copy of that argument is
11866 made in memory and a pointer to the argument is passed instead of
11867 the argument itself. The pointer is passed in whatever way is
11868 appropriate for passing a pointer to that type.
11870 Under V.4, aggregates and long double are passed by reference.
11872 As an extension to all 32-bit ABIs, AltiVec vectors are passed by
11873 reference unless the AltiVec vector extension ABI is in force.
11875 As an extension to all ABIs, variable sized types are passed by
11876 reference. */
11878 static bool
11879 rs6000_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED,
11880 machine_mode mode, const_tree type,
11881 bool named ATTRIBUTE_UNUSED)
11883 if (!type)
11884 return 0;
11886 if (DEFAULT_ABI == ABI_V4 && TARGET_IEEEQUAD
11887 && FLOAT128_IEEE_P (TYPE_MODE (type)))
11889 if (TARGET_DEBUG_ARG)
11890 fprintf (stderr, "function_arg_pass_by_reference: V4 IEEE 128-bit\n");
11891 return 1;
11894 if (DEFAULT_ABI == ABI_V4 && AGGREGATE_TYPE_P (type))
11896 if (TARGET_DEBUG_ARG)
11897 fprintf (stderr, "function_arg_pass_by_reference: V4 aggregate\n");
11898 return 1;
11901 if (int_size_in_bytes (type) < 0)
11903 if (TARGET_DEBUG_ARG)
11904 fprintf (stderr, "function_arg_pass_by_reference: variable size\n");
11905 return 1;
11908 /* Allow -maltivec -mabi=no-altivec without warning. Altivec vector
11909 modes only exist for GCC vector types if -maltivec. */
11910 if (TARGET_32BIT && !TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
11912 if (TARGET_DEBUG_ARG)
11913 fprintf (stderr, "function_arg_pass_by_reference: AltiVec\n");
11914 return 1;
11917 /* Pass synthetic vectors in memory. */
11918 if (TREE_CODE (type) == VECTOR_TYPE
11919 && int_size_in_bytes (type) > (TARGET_ALTIVEC_ABI ? 16 : 8))
11921 static bool warned_for_pass_big_vectors = false;
11922 if (TARGET_DEBUG_ARG)
11923 fprintf (stderr, "function_arg_pass_by_reference: synthetic vector\n");
11924 if (!warned_for_pass_big_vectors)
11926 warning (OPT_Wpsabi, "GCC vector passed by reference: "
11927 "non-standard ABI extension with no compatibility "
11928 "guarantee");
11929 warned_for_pass_big_vectors = true;
11931 return 1;
11934 return 0;
11937 /* Process parameter of type TYPE after ARGS_SO_FAR parameters were
11938 already processes. Return true if the parameter must be passed
11939 (fully or partially) on the stack. */
11941 static bool
11942 rs6000_parm_needs_stack (cumulative_args_t args_so_far, tree type)
11944 machine_mode mode;
11945 int unsignedp;
11946 rtx entry_parm;
11948 /* Catch errors. */
11949 if (type == NULL || type == error_mark_node)
11950 return true;
11952 /* Handle types with no storage requirement. */
11953 if (TYPE_MODE (type) == VOIDmode)
11954 return false;
11956 /* Handle complex types. */
11957 if (TREE_CODE (type) == COMPLEX_TYPE)
11958 return (rs6000_parm_needs_stack (args_so_far, TREE_TYPE (type))
11959 || rs6000_parm_needs_stack (args_so_far, TREE_TYPE (type)));
11961 /* Handle transparent aggregates. */
11962 if ((TREE_CODE (type) == UNION_TYPE || TREE_CODE (type) == RECORD_TYPE)
11963 && TYPE_TRANSPARENT_AGGR (type))
11964 type = TREE_TYPE (first_field (type));
11966 /* See if this arg was passed by invisible reference. */
11967 if (pass_by_reference (get_cumulative_args (args_so_far),
11968 TYPE_MODE (type), type, true))
11969 type = build_pointer_type (type);
11971 /* Find mode as it is passed by the ABI. */
11972 unsignedp = TYPE_UNSIGNED (type);
11973 mode = promote_mode (type, TYPE_MODE (type), &unsignedp);
11975 /* If we must pass in stack, we need a stack. */
11976 if (rs6000_must_pass_in_stack (mode, type))
11977 return true;
11979 /* If there is no incoming register, we need a stack. */
11980 entry_parm = rs6000_function_arg (args_so_far, mode, type, true);
11981 if (entry_parm == NULL)
11982 return true;
11984 /* Likewise if we need to pass both in registers and on the stack. */
11985 if (GET_CODE (entry_parm) == PARALLEL
11986 && XEXP (XVECEXP (entry_parm, 0, 0), 0) == NULL_RTX)
11987 return true;
11989 /* Also true if we're partially in registers and partially not. */
11990 if (rs6000_arg_partial_bytes (args_so_far, mode, type, true) != 0)
11991 return true;
11993 /* Update info on where next arg arrives in registers. */
11994 rs6000_function_arg_advance (args_so_far, mode, type, true);
11995 return false;
11998 /* Return true if FUN has no prototype, has a variable argument
11999 list, or passes any parameter in memory. */
12001 static bool
12002 rs6000_function_parms_need_stack (tree fun, bool incoming)
12004 tree fntype, result;
12005 CUMULATIVE_ARGS args_so_far_v;
12006 cumulative_args_t args_so_far;
12008 if (!fun)
12009 /* Must be a libcall, all of which only use reg parms. */
12010 return false;
12012 fntype = fun;
12013 if (!TYPE_P (fun))
12014 fntype = TREE_TYPE (fun);
12016 /* Varargs functions need the parameter save area. */
12017 if ((!incoming && !prototype_p (fntype)) || stdarg_p (fntype))
12018 return true;
12020 INIT_CUMULATIVE_INCOMING_ARGS (args_so_far_v, fntype, NULL_RTX);
12021 args_so_far = pack_cumulative_args (&args_so_far_v);
12023 /* When incoming, we will have been passed the function decl.
12024 It is necessary to use the decl to handle K&R style functions,
12025 where TYPE_ARG_TYPES may not be available. */
12026 if (incoming)
12028 gcc_assert (DECL_P (fun));
12029 result = DECL_RESULT (fun);
12031 else
12032 result = TREE_TYPE (fntype);
12034 if (result && aggregate_value_p (result, fntype))
12036 if (!TYPE_P (result))
12037 result = TREE_TYPE (result);
12038 result = build_pointer_type (result);
12039 rs6000_parm_needs_stack (args_so_far, result);
12042 if (incoming)
12044 tree parm;
12046 for (parm = DECL_ARGUMENTS (fun);
12047 parm && parm != void_list_node;
12048 parm = TREE_CHAIN (parm))
12049 if (rs6000_parm_needs_stack (args_so_far, TREE_TYPE (parm)))
12050 return true;
12052 else
12054 function_args_iterator args_iter;
12055 tree arg_type;
12057 FOREACH_FUNCTION_ARGS (fntype, arg_type, args_iter)
12058 if (rs6000_parm_needs_stack (args_so_far, arg_type))
12059 return true;
12062 return false;
12065 /* Return the size of the REG_PARM_STACK_SPACE are for FUN. This is
12066 usually a constant depending on the ABI. However, in the ELFv2 ABI
12067 the register parameter area is optional when calling a function that
12068 has a prototype is scope, has no variable argument list, and passes
12069 all parameters in registers. */
12072 rs6000_reg_parm_stack_space (tree fun, bool incoming)
12074 int reg_parm_stack_space;
12076 switch (DEFAULT_ABI)
12078 default:
12079 reg_parm_stack_space = 0;
12080 break;
12082 case ABI_AIX:
12083 case ABI_DARWIN:
12084 reg_parm_stack_space = TARGET_64BIT ? 64 : 32;
12085 break;
12087 case ABI_ELFv2:
12088 /* ??? Recomputing this every time is a bit expensive. Is there
12089 a place to cache this information? */
12090 if (rs6000_function_parms_need_stack (fun, incoming))
12091 reg_parm_stack_space = TARGET_64BIT ? 64 : 32;
12092 else
12093 reg_parm_stack_space = 0;
12094 break;
12097 return reg_parm_stack_space;
12100 static void
12101 rs6000_move_block_from_reg (int regno, rtx x, int nregs)
12103 int i;
12104 machine_mode reg_mode = TARGET_32BIT ? SImode : DImode;
12106 if (nregs == 0)
12107 return;
12109 for (i = 0; i < nregs; i++)
12111 rtx tem = adjust_address_nv (x, reg_mode, i * GET_MODE_SIZE (reg_mode));
12112 if (reload_completed)
12114 if (! strict_memory_address_p (reg_mode, XEXP (tem, 0)))
12115 tem = NULL_RTX;
12116 else
12117 tem = simplify_gen_subreg (reg_mode, x, BLKmode,
12118 i * GET_MODE_SIZE (reg_mode));
12120 else
12121 tem = replace_equiv_address (tem, XEXP (tem, 0));
12123 gcc_assert (tem);
12125 emit_move_insn (tem, gen_rtx_REG (reg_mode, regno + i));
12129 /* Perform any needed actions needed for a function that is receiving a
12130 variable number of arguments.
12132 CUM is as above.
12134 MODE and TYPE are the mode and type of the current parameter.
12136 PRETEND_SIZE is a variable that should be set to the amount of stack
12137 that must be pushed by the prolog to pretend that our caller pushed
12140 Normally, this macro will push all remaining incoming registers on the
12141 stack and set PRETEND_SIZE to the length of the registers pushed. */
12143 static void
12144 setup_incoming_varargs (cumulative_args_t cum, machine_mode mode,
12145 tree type, int *pretend_size ATTRIBUTE_UNUSED,
12146 int no_rtl)
12148 CUMULATIVE_ARGS next_cum;
12149 int reg_size = TARGET_32BIT ? 4 : 8;
12150 rtx save_area = NULL_RTX, mem;
12151 int first_reg_offset;
12152 alias_set_type set;
12154 /* Skip the last named argument. */
12155 next_cum = *get_cumulative_args (cum);
12156 rs6000_function_arg_advance_1 (&next_cum, mode, type, true, 0);
12158 if (DEFAULT_ABI == ABI_V4)
12160 first_reg_offset = next_cum.sysv_gregno - GP_ARG_MIN_REG;
12162 if (! no_rtl)
12164 int gpr_reg_num = 0, gpr_size = 0, fpr_size = 0;
12165 HOST_WIDE_INT offset = 0;
12167 /* Try to optimize the size of the varargs save area.
12168 The ABI requires that ap.reg_save_area is doubleword
12169 aligned, but we don't need to allocate space for all
12170 the bytes, only those to which we actually will save
12171 anything. */
12172 if (cfun->va_list_gpr_size && first_reg_offset < GP_ARG_NUM_REG)
12173 gpr_reg_num = GP_ARG_NUM_REG - first_reg_offset;
12174 if (TARGET_HARD_FLOAT
12175 && next_cum.fregno <= FP_ARG_V4_MAX_REG
12176 && cfun->va_list_fpr_size)
12178 if (gpr_reg_num)
12179 fpr_size = (next_cum.fregno - FP_ARG_MIN_REG)
12180 * UNITS_PER_FP_WORD;
12181 if (cfun->va_list_fpr_size
12182 < FP_ARG_V4_MAX_REG + 1 - next_cum.fregno)
12183 fpr_size += cfun->va_list_fpr_size * UNITS_PER_FP_WORD;
12184 else
12185 fpr_size += (FP_ARG_V4_MAX_REG + 1 - next_cum.fregno)
12186 * UNITS_PER_FP_WORD;
12188 if (gpr_reg_num)
12190 offset = -((first_reg_offset * reg_size) & ~7);
12191 if (!fpr_size && gpr_reg_num > cfun->va_list_gpr_size)
12193 gpr_reg_num = cfun->va_list_gpr_size;
12194 if (reg_size == 4 && (first_reg_offset & 1))
12195 gpr_reg_num++;
12197 gpr_size = (gpr_reg_num * reg_size + 7) & ~7;
12199 else if (fpr_size)
12200 offset = - (int) (next_cum.fregno - FP_ARG_MIN_REG)
12201 * UNITS_PER_FP_WORD
12202 - (int) (GP_ARG_NUM_REG * reg_size);
12204 if (gpr_size + fpr_size)
12206 rtx reg_save_area
12207 = assign_stack_local (BLKmode, gpr_size + fpr_size, 64);
12208 gcc_assert (MEM_P (reg_save_area));
12209 reg_save_area = XEXP (reg_save_area, 0);
12210 if (GET_CODE (reg_save_area) == PLUS)
12212 gcc_assert (XEXP (reg_save_area, 0)
12213 == virtual_stack_vars_rtx);
12214 gcc_assert (CONST_INT_P (XEXP (reg_save_area, 1)));
12215 offset += INTVAL (XEXP (reg_save_area, 1));
12217 else
12218 gcc_assert (reg_save_area == virtual_stack_vars_rtx);
12221 cfun->machine->varargs_save_offset = offset;
12222 save_area = plus_constant (Pmode, virtual_stack_vars_rtx, offset);
12225 else
12227 first_reg_offset = next_cum.words;
12228 save_area = crtl->args.internal_arg_pointer;
12230 if (targetm.calls.must_pass_in_stack (mode, type))
12231 first_reg_offset += rs6000_arg_size (TYPE_MODE (type), type);
12234 set = get_varargs_alias_set ();
12235 if (! no_rtl && first_reg_offset < GP_ARG_NUM_REG
12236 && cfun->va_list_gpr_size)
12238 int n_gpr, nregs = GP_ARG_NUM_REG - first_reg_offset;
12240 if (va_list_gpr_counter_field)
12241 /* V4 va_list_gpr_size counts number of registers needed. */
12242 n_gpr = cfun->va_list_gpr_size;
12243 else
12244 /* char * va_list instead counts number of bytes needed. */
12245 n_gpr = (cfun->va_list_gpr_size + reg_size - 1) / reg_size;
12247 if (nregs > n_gpr)
12248 nregs = n_gpr;
12250 mem = gen_rtx_MEM (BLKmode,
12251 plus_constant (Pmode, save_area,
12252 first_reg_offset * reg_size));
12253 MEM_NOTRAP_P (mem) = 1;
12254 set_mem_alias_set (mem, set);
12255 set_mem_align (mem, BITS_PER_WORD);
12257 rs6000_move_block_from_reg (GP_ARG_MIN_REG + first_reg_offset, mem,
12258 nregs);
12261 /* Save FP registers if needed. */
12262 if (DEFAULT_ABI == ABI_V4
12263 && TARGET_HARD_FLOAT
12264 && ! no_rtl
12265 && next_cum.fregno <= FP_ARG_V4_MAX_REG
12266 && cfun->va_list_fpr_size)
12268 int fregno = next_cum.fregno, nregs;
12269 rtx cr1 = gen_rtx_REG (CCmode, CR1_REGNO);
12270 rtx lab = gen_label_rtx ();
12271 int off = (GP_ARG_NUM_REG * reg_size) + ((fregno - FP_ARG_MIN_REG)
12272 * UNITS_PER_FP_WORD);
12274 emit_jump_insn
12275 (gen_rtx_SET (pc_rtx,
12276 gen_rtx_IF_THEN_ELSE (VOIDmode,
12277 gen_rtx_NE (VOIDmode, cr1,
12278 const0_rtx),
12279 gen_rtx_LABEL_REF (VOIDmode, lab),
12280 pc_rtx)));
12282 for (nregs = 0;
12283 fregno <= FP_ARG_V4_MAX_REG && nregs < cfun->va_list_fpr_size;
12284 fregno++, off += UNITS_PER_FP_WORD, nregs++)
12286 mem = gen_rtx_MEM (TARGET_HARD_FLOAT ? DFmode : SFmode,
12287 plus_constant (Pmode, save_area, off));
12288 MEM_NOTRAP_P (mem) = 1;
12289 set_mem_alias_set (mem, set);
12290 set_mem_align (mem, GET_MODE_ALIGNMENT (
12291 TARGET_HARD_FLOAT ? DFmode : SFmode));
12292 emit_move_insn (mem, gen_rtx_REG (
12293 TARGET_HARD_FLOAT ? DFmode : SFmode, fregno));
12296 emit_label (lab);
12300 /* Create the va_list data type. */
12302 static tree
12303 rs6000_build_builtin_va_list (void)
12305 tree f_gpr, f_fpr, f_res, f_ovf, f_sav, record, type_decl;
12307 /* For AIX, prefer 'char *' because that's what the system
12308 header files like. */
12309 if (DEFAULT_ABI != ABI_V4)
12310 return build_pointer_type (char_type_node);
12312 record = (*lang_hooks.types.make_type) (RECORD_TYPE);
12313 type_decl = build_decl (BUILTINS_LOCATION, TYPE_DECL,
12314 get_identifier ("__va_list_tag"), record);
12316 f_gpr = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("gpr"),
12317 unsigned_char_type_node);
12318 f_fpr = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("fpr"),
12319 unsigned_char_type_node);
12320 /* Give the two bytes of padding a name, so that -Wpadded won't warn on
12321 every user file. */
12322 f_res = build_decl (BUILTINS_LOCATION, FIELD_DECL,
12323 get_identifier ("reserved"), short_unsigned_type_node);
12324 f_ovf = build_decl (BUILTINS_LOCATION, FIELD_DECL,
12325 get_identifier ("overflow_arg_area"),
12326 ptr_type_node);
12327 f_sav = build_decl (BUILTINS_LOCATION, FIELD_DECL,
12328 get_identifier ("reg_save_area"),
12329 ptr_type_node);
12331 va_list_gpr_counter_field = f_gpr;
12332 va_list_fpr_counter_field = f_fpr;
12334 DECL_FIELD_CONTEXT (f_gpr) = record;
12335 DECL_FIELD_CONTEXT (f_fpr) = record;
12336 DECL_FIELD_CONTEXT (f_res) = record;
12337 DECL_FIELD_CONTEXT (f_ovf) = record;
12338 DECL_FIELD_CONTEXT (f_sav) = record;
12340 TYPE_STUB_DECL (record) = type_decl;
12341 TYPE_NAME (record) = type_decl;
12342 TYPE_FIELDS (record) = f_gpr;
12343 DECL_CHAIN (f_gpr) = f_fpr;
12344 DECL_CHAIN (f_fpr) = f_res;
12345 DECL_CHAIN (f_res) = f_ovf;
12346 DECL_CHAIN (f_ovf) = f_sav;
12348 layout_type (record);
12350 /* The correct type is an array type of one element. */
12351 return build_array_type (record, build_index_type (size_zero_node));
12354 /* Implement va_start. */
12356 static void
12357 rs6000_va_start (tree valist, rtx nextarg)
12359 HOST_WIDE_INT words, n_gpr, n_fpr;
12360 tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
12361 tree gpr, fpr, ovf, sav, t;
12363 /* Only SVR4 needs something special. */
12364 if (DEFAULT_ABI != ABI_V4)
12366 std_expand_builtin_va_start (valist, nextarg);
12367 return;
12370 f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
12371 f_fpr = DECL_CHAIN (f_gpr);
12372 f_res = DECL_CHAIN (f_fpr);
12373 f_ovf = DECL_CHAIN (f_res);
12374 f_sav = DECL_CHAIN (f_ovf);
12376 valist = build_simple_mem_ref (valist);
12377 gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
12378 fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
12379 f_fpr, NULL_TREE);
12380 ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
12381 f_ovf, NULL_TREE);
12382 sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
12383 f_sav, NULL_TREE);
12385 /* Count number of gp and fp argument registers used. */
12386 words = crtl->args.info.words;
12387 n_gpr = MIN (crtl->args.info.sysv_gregno - GP_ARG_MIN_REG,
12388 GP_ARG_NUM_REG);
12389 n_fpr = MIN (crtl->args.info.fregno - FP_ARG_MIN_REG,
12390 FP_ARG_NUM_REG);
12392 if (TARGET_DEBUG_ARG)
12393 fprintf (stderr, "va_start: words = " HOST_WIDE_INT_PRINT_DEC", n_gpr = "
12394 HOST_WIDE_INT_PRINT_DEC", n_fpr = " HOST_WIDE_INT_PRINT_DEC"\n",
12395 words, n_gpr, n_fpr);
12397 if (cfun->va_list_gpr_size)
12399 t = build2 (MODIFY_EXPR, TREE_TYPE (gpr), gpr,
12400 build_int_cst (NULL_TREE, n_gpr));
12401 TREE_SIDE_EFFECTS (t) = 1;
12402 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
12405 if (cfun->va_list_fpr_size)
12407 t = build2 (MODIFY_EXPR, TREE_TYPE (fpr), fpr,
12408 build_int_cst (NULL_TREE, n_fpr));
12409 TREE_SIDE_EFFECTS (t) = 1;
12410 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
12412 #ifdef HAVE_AS_GNU_ATTRIBUTE
12413 if (call_ABI_of_interest (cfun->decl))
12414 rs6000_passes_float = true;
12415 #endif
12418 /* Find the overflow area. */
12419 t = make_tree (TREE_TYPE (ovf), crtl->args.internal_arg_pointer);
12420 if (words != 0)
12421 t = fold_build_pointer_plus_hwi (t, words * MIN_UNITS_PER_WORD);
12422 t = build2 (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
12423 TREE_SIDE_EFFECTS (t) = 1;
12424 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
12426 /* If there were no va_arg invocations, don't set up the register
12427 save area. */
12428 if (!cfun->va_list_gpr_size
12429 && !cfun->va_list_fpr_size
12430 && n_gpr < GP_ARG_NUM_REG
12431 && n_fpr < FP_ARG_V4_MAX_REG)
12432 return;
12434 /* Find the register save area. */
12435 t = make_tree (TREE_TYPE (sav), virtual_stack_vars_rtx);
12436 if (cfun->machine->varargs_save_offset)
12437 t = fold_build_pointer_plus_hwi (t, cfun->machine->varargs_save_offset);
12438 t = build2 (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
12439 TREE_SIDE_EFFECTS (t) = 1;
12440 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
12443 /* Implement va_arg. */
12445 static tree
12446 rs6000_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p,
12447 gimple_seq *post_p)
12449 tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
12450 tree gpr, fpr, ovf, sav, reg, t, u;
12451 int size, rsize, n_reg, sav_ofs, sav_scale;
12452 tree lab_false, lab_over, addr;
12453 int align;
12454 tree ptrtype = build_pointer_type_for_mode (type, ptr_mode, true);
12455 int regalign = 0;
12456 gimple *stmt;
12458 if (pass_by_reference (NULL, TYPE_MODE (type), type, false))
12460 t = rs6000_gimplify_va_arg (valist, ptrtype, pre_p, post_p);
12461 return build_va_arg_indirect_ref (t);
12464 /* We need to deal with the fact that the darwin ppc64 ABI is defined by an
12465 earlier version of gcc, with the property that it always applied alignment
12466 adjustments to the va-args (even for zero-sized types). The cheapest way
12467 to deal with this is to replicate the effect of the part of
12468 std_gimplify_va_arg_expr that carries out the align adjust, for the case
12469 of relevance.
12470 We don't need to check for pass-by-reference because of the test above.
12471 We can return a simplifed answer, since we know there's no offset to add. */
12473 if (((TARGET_MACHO
12474 && rs6000_darwin64_abi)
12475 || DEFAULT_ABI == ABI_ELFv2
12476 || (DEFAULT_ABI == ABI_AIX && !rs6000_compat_align_parm))
12477 && integer_zerop (TYPE_SIZE (type)))
12479 unsigned HOST_WIDE_INT align, boundary;
12480 tree valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
12481 align = PARM_BOUNDARY / BITS_PER_UNIT;
12482 boundary = rs6000_function_arg_boundary (TYPE_MODE (type), type);
12483 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
12484 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
12485 boundary /= BITS_PER_UNIT;
12486 if (boundary > align)
12488 tree t ;
12489 /* This updates arg ptr by the amount that would be necessary
12490 to align the zero-sized (but not zero-alignment) item. */
12491 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
12492 fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
12493 gimplify_and_add (t, pre_p);
12495 t = fold_convert (sizetype, valist_tmp);
12496 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
12497 fold_convert (TREE_TYPE (valist),
12498 fold_build2 (BIT_AND_EXPR, sizetype, t,
12499 size_int (-boundary))));
12500 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
12501 gimplify_and_add (t, pre_p);
12503 /* Since it is zero-sized there's no increment for the item itself. */
12504 valist_tmp = fold_convert (build_pointer_type (type), valist_tmp);
12505 return build_va_arg_indirect_ref (valist_tmp);
12508 if (DEFAULT_ABI != ABI_V4)
12510 if (targetm.calls.split_complex_arg && TREE_CODE (type) == COMPLEX_TYPE)
12512 tree elem_type = TREE_TYPE (type);
12513 machine_mode elem_mode = TYPE_MODE (elem_type);
12514 int elem_size = GET_MODE_SIZE (elem_mode);
12516 if (elem_size < UNITS_PER_WORD)
12518 tree real_part, imag_part;
12519 gimple_seq post = NULL;
12521 real_part = rs6000_gimplify_va_arg (valist, elem_type, pre_p,
12522 &post);
12523 /* Copy the value into a temporary, lest the formal temporary
12524 be reused out from under us. */
12525 real_part = get_initialized_tmp_var (real_part, pre_p, &post);
12526 gimple_seq_add_seq (pre_p, post);
12528 imag_part = rs6000_gimplify_va_arg (valist, elem_type, pre_p,
12529 post_p);
12531 return build2 (COMPLEX_EXPR, type, real_part, imag_part);
12535 return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
12538 f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
12539 f_fpr = DECL_CHAIN (f_gpr);
12540 f_res = DECL_CHAIN (f_fpr);
12541 f_ovf = DECL_CHAIN (f_res);
12542 f_sav = DECL_CHAIN (f_ovf);
12544 gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
12545 fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
12546 f_fpr, NULL_TREE);
12547 ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
12548 f_ovf, NULL_TREE);
12549 sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
12550 f_sav, NULL_TREE);
12552 size = int_size_in_bytes (type);
12553 rsize = (size + 3) / 4;
12554 int pad = 4 * rsize - size;
12555 align = 1;
12557 machine_mode mode = TYPE_MODE (type);
12558 if (abi_v4_pass_in_fpr (mode, false))
12560 /* FP args go in FP registers, if present. */
12561 reg = fpr;
12562 n_reg = (size + 7) / 8;
12563 sav_ofs = (TARGET_HARD_FLOAT ? 8 : 4) * 4;
12564 sav_scale = (TARGET_HARD_FLOAT ? 8 : 4);
12565 if (mode != SFmode && mode != SDmode)
12566 align = 8;
12568 else
12570 /* Otherwise into GP registers. */
12571 reg = gpr;
12572 n_reg = rsize;
12573 sav_ofs = 0;
12574 sav_scale = 4;
12575 if (n_reg == 2)
12576 align = 8;
12579 /* Pull the value out of the saved registers.... */
12581 lab_over = NULL;
12582 addr = create_tmp_var (ptr_type_node, "addr");
12584 /* AltiVec vectors never go in registers when -mabi=altivec. */
12585 if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
12586 align = 16;
12587 else
12589 lab_false = create_artificial_label (input_location);
12590 lab_over = create_artificial_label (input_location);
12592 /* Long long is aligned in the registers. As are any other 2 gpr
12593 item such as complex int due to a historical mistake. */
12594 u = reg;
12595 if (n_reg == 2 && reg == gpr)
12597 regalign = 1;
12598 u = build2 (BIT_AND_EXPR, TREE_TYPE (reg), unshare_expr (reg),
12599 build_int_cst (TREE_TYPE (reg), n_reg - 1));
12600 u = build2 (POSTINCREMENT_EXPR, TREE_TYPE (reg),
12601 unshare_expr (reg), u);
12603 /* _Decimal128 is passed in even/odd fpr pairs; the stored
12604 reg number is 0 for f1, so we want to make it odd. */
12605 else if (reg == fpr && mode == TDmode)
12607 t = build2 (BIT_IOR_EXPR, TREE_TYPE (reg), unshare_expr (reg),
12608 build_int_cst (TREE_TYPE (reg), 1));
12609 u = build2 (MODIFY_EXPR, void_type_node, unshare_expr (reg), t);
12612 t = fold_convert (TREE_TYPE (reg), size_int (8 - n_reg + 1));
12613 t = build2 (GE_EXPR, boolean_type_node, u, t);
12614 u = build1 (GOTO_EXPR, void_type_node, lab_false);
12615 t = build3 (COND_EXPR, void_type_node, t, u, NULL_TREE);
12616 gimplify_and_add (t, pre_p);
12618 t = sav;
12619 if (sav_ofs)
12620 t = fold_build_pointer_plus_hwi (sav, sav_ofs);
12622 u = build2 (POSTINCREMENT_EXPR, TREE_TYPE (reg), unshare_expr (reg),
12623 build_int_cst (TREE_TYPE (reg), n_reg));
12624 u = fold_convert (sizetype, u);
12625 u = build2 (MULT_EXPR, sizetype, u, size_int (sav_scale));
12626 t = fold_build_pointer_plus (t, u);
12628 /* _Decimal32 varargs are located in the second word of the 64-bit
12629 FP register for 32-bit binaries. */
12630 if (TARGET_32BIT && TARGET_HARD_FLOAT && mode == SDmode)
12631 t = fold_build_pointer_plus_hwi (t, size);
12633 /* Args are passed right-aligned. */
12634 if (BYTES_BIG_ENDIAN)
12635 t = fold_build_pointer_plus_hwi (t, pad);
12637 gimplify_assign (addr, t, pre_p);
12639 gimple_seq_add_stmt (pre_p, gimple_build_goto (lab_over));
12641 stmt = gimple_build_label (lab_false);
12642 gimple_seq_add_stmt (pre_p, stmt);
12644 if ((n_reg == 2 && !regalign) || n_reg > 2)
12646 /* Ensure that we don't find any more args in regs.
12647 Alignment has taken care of for special cases. */
12648 gimplify_assign (reg, build_int_cst (TREE_TYPE (reg), 8), pre_p);
12652 /* ... otherwise out of the overflow area. */
12654 /* Care for on-stack alignment if needed. */
12655 t = ovf;
12656 if (align != 1)
12658 t = fold_build_pointer_plus_hwi (t, align - 1);
12659 t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t,
12660 build_int_cst (TREE_TYPE (t), -align));
12663 /* Args are passed right-aligned. */
12664 if (BYTES_BIG_ENDIAN)
12665 t = fold_build_pointer_plus_hwi (t, pad);
12667 gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
12669 gimplify_assign (unshare_expr (addr), t, pre_p);
12671 t = fold_build_pointer_plus_hwi (t, size);
12672 gimplify_assign (unshare_expr (ovf), t, pre_p);
12674 if (lab_over)
12676 stmt = gimple_build_label (lab_over);
12677 gimple_seq_add_stmt (pre_p, stmt);
12680 if (STRICT_ALIGNMENT
12681 && (TYPE_ALIGN (type)
12682 > (unsigned) BITS_PER_UNIT * (align < 4 ? 4 : align)))
12684 /* The value (of type complex double, for example) may not be
12685 aligned in memory in the saved registers, so copy via a
12686 temporary. (This is the same code as used for SPARC.) */
12687 tree tmp = create_tmp_var (type, "va_arg_tmp");
12688 tree dest_addr = build_fold_addr_expr (tmp);
12690 tree copy = build_call_expr (builtin_decl_implicit (BUILT_IN_MEMCPY),
12691 3, dest_addr, addr, size_int (rsize * 4));
12692 TREE_ADDRESSABLE (tmp) = 1;
12694 gimplify_and_add (copy, pre_p);
12695 addr = dest_addr;
12698 addr = fold_convert (ptrtype, addr);
12699 return build_va_arg_indirect_ref (addr);
12702 /* Builtins. */
12704 static void
12705 def_builtin (const char *name, tree type, enum rs6000_builtins code)
12707 tree t;
12708 unsigned classify = rs6000_builtin_info[(int)code].attr;
12709 const char *attr_string = "";
12711 gcc_assert (name != NULL);
12712 gcc_assert (IN_RANGE ((int)code, 0, (int)RS6000_BUILTIN_COUNT));
12714 if (rs6000_builtin_decls[(int)code])
12715 fatal_error (input_location,
12716 "internal error: builtin function %qs already processed",
12717 name);
12719 rs6000_builtin_decls[(int)code] = t =
12720 add_builtin_function (name, type, (int)code, BUILT_IN_MD, NULL, NULL_TREE);
12722 /* Set any special attributes. */
12723 if ((classify & RS6000_BTC_CONST) != 0)
12725 /* const function, function only depends on the inputs. */
12726 TREE_READONLY (t) = 1;
12727 TREE_NOTHROW (t) = 1;
12728 attr_string = ", const";
12730 else if ((classify & RS6000_BTC_PURE) != 0)
12732 /* pure function, function can read global memory, but does not set any
12733 external state. */
12734 DECL_PURE_P (t) = 1;
12735 TREE_NOTHROW (t) = 1;
12736 attr_string = ", pure";
12738 else if ((classify & RS6000_BTC_FP) != 0)
12740 /* Function is a math function. If rounding mode is on, then treat the
12741 function as not reading global memory, but it can have arbitrary side
12742 effects. If it is off, then assume the function is a const function.
12743 This mimics the ATTR_MATHFN_FPROUNDING attribute in
12744 builtin-attribute.def that is used for the math functions. */
12745 TREE_NOTHROW (t) = 1;
12746 if (flag_rounding_math)
12748 DECL_PURE_P (t) = 1;
12749 DECL_IS_NOVOPS (t) = 1;
12750 attr_string = ", fp, pure";
12752 else
12754 TREE_READONLY (t) = 1;
12755 attr_string = ", fp, const";
12758 else if ((classify & RS6000_BTC_ATTR_MASK) != 0)
12759 gcc_unreachable ();
12761 if (TARGET_DEBUG_BUILTIN)
12762 fprintf (stderr, "rs6000_builtin, code = %4d, %s%s\n",
12763 (int)code, name, attr_string);
12766 /* Simple ternary operations: VECd = foo (VECa, VECb, VECc). */
12768 #undef RS6000_BUILTIN_0
12769 #undef RS6000_BUILTIN_1
12770 #undef RS6000_BUILTIN_2
12771 #undef RS6000_BUILTIN_3
12772 #undef RS6000_BUILTIN_A
12773 #undef RS6000_BUILTIN_D
12774 #undef RS6000_BUILTIN_H
12775 #undef RS6000_BUILTIN_P
12776 #undef RS6000_BUILTIN_X
12778 #define RS6000_BUILTIN_0(ENUM, NAME, MASK, ATTR, ICODE)
12779 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
12780 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
12781 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE) \
12782 { MASK, ICODE, NAME, ENUM },
12784 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
12785 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
12786 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
12787 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
12788 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
12790 static const struct builtin_description bdesc_3arg[] =
12792 #include "rs6000-builtin.def"
12795 /* DST operations: void foo (void *, const int, const char). */
12797 #undef RS6000_BUILTIN_0
12798 #undef RS6000_BUILTIN_1
12799 #undef RS6000_BUILTIN_2
12800 #undef RS6000_BUILTIN_3
12801 #undef RS6000_BUILTIN_A
12802 #undef RS6000_BUILTIN_D
12803 #undef RS6000_BUILTIN_H
12804 #undef RS6000_BUILTIN_P
12805 #undef RS6000_BUILTIN_X
12807 #define RS6000_BUILTIN_0(ENUM, NAME, MASK, ATTR, ICODE)
12808 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
12809 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
12810 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
12811 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
12812 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE) \
12813 { MASK, ICODE, NAME, ENUM },
12815 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
12816 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
12817 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
12819 static const struct builtin_description bdesc_dst[] =
12821 #include "rs6000-builtin.def"
12824 /* Simple binary operations: VECc = foo (VECa, VECb). */
12826 #undef RS6000_BUILTIN_0
12827 #undef RS6000_BUILTIN_1
12828 #undef RS6000_BUILTIN_2
12829 #undef RS6000_BUILTIN_3
12830 #undef RS6000_BUILTIN_A
12831 #undef RS6000_BUILTIN_D
12832 #undef RS6000_BUILTIN_H
12833 #undef RS6000_BUILTIN_P
12834 #undef RS6000_BUILTIN_X
12836 #define RS6000_BUILTIN_0(ENUM, NAME, MASK, ATTR, ICODE)
12837 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
12838 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE) \
12839 { MASK, ICODE, NAME, ENUM },
12841 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
12842 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
12843 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
12844 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
12845 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
12846 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
12848 static const struct builtin_description bdesc_2arg[] =
12850 #include "rs6000-builtin.def"
12853 #undef RS6000_BUILTIN_0
12854 #undef RS6000_BUILTIN_1
12855 #undef RS6000_BUILTIN_2
12856 #undef RS6000_BUILTIN_3
12857 #undef RS6000_BUILTIN_A
12858 #undef RS6000_BUILTIN_D
12859 #undef RS6000_BUILTIN_H
12860 #undef RS6000_BUILTIN_P
12861 #undef RS6000_BUILTIN_X
12863 #define RS6000_BUILTIN_0(ENUM, NAME, MASK, ATTR, ICODE)
12864 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
12865 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
12866 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
12867 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
12868 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
12869 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
12870 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE) \
12871 { MASK, ICODE, NAME, ENUM },
12873 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
12875 /* AltiVec predicates. */
12877 static const struct builtin_description bdesc_altivec_preds[] =
12879 #include "rs6000-builtin.def"
12882 /* ABS* operations. */
12884 #undef RS6000_BUILTIN_0
12885 #undef RS6000_BUILTIN_1
12886 #undef RS6000_BUILTIN_2
12887 #undef RS6000_BUILTIN_3
12888 #undef RS6000_BUILTIN_A
12889 #undef RS6000_BUILTIN_D
12890 #undef RS6000_BUILTIN_H
12891 #undef RS6000_BUILTIN_P
12892 #undef RS6000_BUILTIN_X
12894 #define RS6000_BUILTIN_0(ENUM, NAME, MASK, ATTR, ICODE)
12895 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
12896 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
12897 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
12898 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE) \
12899 { MASK, ICODE, NAME, ENUM },
12901 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
12902 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
12903 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
12904 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
12906 static const struct builtin_description bdesc_abs[] =
12908 #include "rs6000-builtin.def"
12911 /* Simple unary operations: VECb = foo (unsigned literal) or VECb =
12912 foo (VECa). */
12914 #undef RS6000_BUILTIN_0
12915 #undef RS6000_BUILTIN_1
12916 #undef RS6000_BUILTIN_2
12917 #undef RS6000_BUILTIN_3
12918 #undef RS6000_BUILTIN_A
12919 #undef RS6000_BUILTIN_D
12920 #undef RS6000_BUILTIN_H
12921 #undef RS6000_BUILTIN_P
12922 #undef RS6000_BUILTIN_X
12924 #define RS6000_BUILTIN_0(ENUM, NAME, MASK, ATTR, ICODE)
12925 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE) \
12926 { MASK, ICODE, NAME, ENUM },
12928 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
12929 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
12930 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
12931 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
12932 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
12933 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
12934 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
12936 static const struct builtin_description bdesc_1arg[] =
12938 #include "rs6000-builtin.def"
12941 /* Simple no-argument operations: result = __builtin_darn_32 () */
12943 #undef RS6000_BUILTIN_0
12944 #undef RS6000_BUILTIN_1
12945 #undef RS6000_BUILTIN_2
12946 #undef RS6000_BUILTIN_3
12947 #undef RS6000_BUILTIN_A
12948 #undef RS6000_BUILTIN_D
12949 #undef RS6000_BUILTIN_H
12950 #undef RS6000_BUILTIN_P
12951 #undef RS6000_BUILTIN_X
12953 #define RS6000_BUILTIN_0(ENUM, NAME, MASK, ATTR, ICODE) \
12954 { MASK, ICODE, NAME, ENUM },
12956 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
12957 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
12958 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
12959 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
12960 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
12961 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
12962 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
12963 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
12965 static const struct builtin_description bdesc_0arg[] =
12967 #include "rs6000-builtin.def"
12970 /* HTM builtins. */
12971 #undef RS6000_BUILTIN_0
12972 #undef RS6000_BUILTIN_1
12973 #undef RS6000_BUILTIN_2
12974 #undef RS6000_BUILTIN_3
12975 #undef RS6000_BUILTIN_A
12976 #undef RS6000_BUILTIN_D
12977 #undef RS6000_BUILTIN_H
12978 #undef RS6000_BUILTIN_P
12979 #undef RS6000_BUILTIN_X
12981 #define RS6000_BUILTIN_0(ENUM, NAME, MASK, ATTR, ICODE)
12982 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
12983 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
12984 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
12985 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
12986 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
12987 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE) \
12988 { MASK, ICODE, NAME, ENUM },
12990 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
12991 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
12993 static const struct builtin_description bdesc_htm[] =
12995 #include "rs6000-builtin.def"
12998 #undef RS6000_BUILTIN_0
12999 #undef RS6000_BUILTIN_1
13000 #undef RS6000_BUILTIN_2
13001 #undef RS6000_BUILTIN_3
13002 #undef RS6000_BUILTIN_A
13003 #undef RS6000_BUILTIN_D
13004 #undef RS6000_BUILTIN_H
13005 #undef RS6000_BUILTIN_P
13007 /* Return true if a builtin function is overloaded. */
13008 bool
13009 rs6000_overloaded_builtin_p (enum rs6000_builtins fncode)
13011 return (rs6000_builtin_info[(int)fncode].attr & RS6000_BTC_OVERLOADED) != 0;
13014 const char *
13015 rs6000_overloaded_builtin_name (enum rs6000_builtins fncode)
13017 return rs6000_builtin_info[(int)fncode].name;
13020 /* Expand an expression EXP that calls a builtin without arguments. */
13021 static rtx
13022 rs6000_expand_zeroop_builtin (enum insn_code icode, rtx target)
13024 rtx pat;
13025 machine_mode tmode = insn_data[icode].operand[0].mode;
13027 if (icode == CODE_FOR_nothing)
13028 /* Builtin not supported on this processor. */
13029 return 0;
13031 if (icode == CODE_FOR_rs6000_mffsl
13032 && rs6000_isa_flags & OPTION_MASK_SOFT_FLOAT)
13034 error ("%<__builtin_mffsl%> not supported with %<-msoft-float%>");
13035 return const0_rtx;
13038 if (target == 0
13039 || GET_MODE (target) != tmode
13040 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13041 target = gen_reg_rtx (tmode);
13043 pat = GEN_FCN (icode) (target);
13044 if (! pat)
13045 return 0;
13046 emit_insn (pat);
13048 return target;
13052 static rtx
13053 rs6000_expand_mtfsf_builtin (enum insn_code icode, tree exp)
13055 rtx pat;
13056 tree arg0 = CALL_EXPR_ARG (exp, 0);
13057 tree arg1 = CALL_EXPR_ARG (exp, 1);
13058 rtx op0 = expand_normal (arg0);
13059 rtx op1 = expand_normal (arg1);
13060 machine_mode mode0 = insn_data[icode].operand[0].mode;
13061 machine_mode mode1 = insn_data[icode].operand[1].mode;
13063 if (icode == CODE_FOR_nothing)
13064 /* Builtin not supported on this processor. */
13065 return 0;
13067 /* If we got invalid arguments bail out before generating bad rtl. */
13068 if (arg0 == error_mark_node || arg1 == error_mark_node)
13069 return const0_rtx;
13071 if (!CONST_INT_P (op0)
13072 || INTVAL (op0) > 255
13073 || INTVAL (op0) < 0)
13075 error ("argument 1 must be an 8-bit field value");
13076 return const0_rtx;
13079 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
13080 op0 = copy_to_mode_reg (mode0, op0);
13082 if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
13083 op1 = copy_to_mode_reg (mode1, op1);
13085 pat = GEN_FCN (icode) (op0, op1);
13086 if (!pat)
13087 return const0_rtx;
13088 emit_insn (pat);
13090 return NULL_RTX;
13093 static rtx
13094 rs6000_expand_mtfsb_builtin (enum insn_code icode, tree exp)
13096 rtx pat;
13097 tree arg0 = CALL_EXPR_ARG (exp, 0);
13098 rtx op0 = expand_normal (arg0);
13100 if (icode == CODE_FOR_nothing)
13101 /* Builtin not supported on this processor. */
13102 return 0;
13104 if (rs6000_isa_flags & OPTION_MASK_SOFT_FLOAT)
13106 error ("%<__builtin_mtfsb0%> and %<__builtin_mtfsb1%> not supported with "
13107 "%<-msoft-float%>");
13108 return const0_rtx;
13111 /* If we got invalid arguments bail out before generating bad rtl. */
13112 if (arg0 == error_mark_node)
13113 return const0_rtx;
13115 /* Only allow bit numbers 0 to 31. */
13116 if (!u5bit_cint_operand (op0, VOIDmode))
13118 error ("Argument must be a constant between 0 and 31.");
13119 return const0_rtx;
13122 pat = GEN_FCN (icode) (op0);
13123 if (!pat)
13124 return const0_rtx;
13125 emit_insn (pat);
13127 return NULL_RTX;
13130 static rtx
13131 rs6000_expand_set_fpscr_rn_builtin (enum insn_code icode, tree exp)
13133 rtx pat;
13134 tree arg0 = CALL_EXPR_ARG (exp, 0);
13135 rtx op0 = expand_normal (arg0);
13136 machine_mode mode0 = insn_data[icode].operand[0].mode;
13138 if (icode == CODE_FOR_nothing)
13139 /* Builtin not supported on this processor. */
13140 return 0;
13142 if (rs6000_isa_flags & OPTION_MASK_SOFT_FLOAT)
13144 error ("%<__builtin_set_fpscr_rn%> not supported with %<-msoft-float%>");
13145 return const0_rtx;
13148 /* If we got invalid arguments bail out before generating bad rtl. */
13149 if (arg0 == error_mark_node)
13150 return const0_rtx;
13152 /* If the argument is a constant, check the range. Argument can only be a
13153 2-bit value. Unfortunately, can't check the range of the value at
13154 compile time if the argument is a variable. The least significant two
13155 bits of the argument, regardless of type, are used to set the rounding
13156 mode. All other bits are ignored. */
13157 if (CONST_INT_P (op0) && !const_0_to_3_operand(op0, VOIDmode))
13159 error ("Argument must be a value between 0 and 3.");
13160 return const0_rtx;
13163 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
13164 op0 = copy_to_mode_reg (mode0, op0);
13166 pat = GEN_FCN (icode) (op0);
13167 if (!pat)
13168 return const0_rtx;
13169 emit_insn (pat);
13171 return NULL_RTX;
13173 static rtx
13174 rs6000_expand_set_fpscr_drn_builtin (enum insn_code icode, tree exp)
13176 rtx pat;
13177 tree arg0 = CALL_EXPR_ARG (exp, 0);
13178 rtx op0 = expand_normal (arg0);
13179 machine_mode mode0 = insn_data[icode].operand[0].mode;
13181 if (TARGET_32BIT)
13182 /* Builtin not supported in 32-bit mode. */
13183 fatal_error (input_location,
13184 "%<__builtin_set_fpscr_drn%> is not supported "
13185 "in 32-bit mode");
13187 if (rs6000_isa_flags & OPTION_MASK_SOFT_FLOAT)
13189 error ("%<__builtin_set_fpscr_drn%> not supported with %<-msoft-float%>");
13190 return const0_rtx;
13193 if (icode == CODE_FOR_nothing)
13194 /* Builtin not supported on this processor. */
13195 return 0;
13197 /* If we got invalid arguments bail out before generating bad rtl. */
13198 if (arg0 == error_mark_node)
13199 return const0_rtx;
13201 /* If the argument is a constant, check the range. Agrument can only be a
13202 3-bit value. Unfortunately, can't check the range of the value at
13203 compile time if the argument is a variable. The least significant two
13204 bits of the argument, regardless of type, are used to set the rounding
13205 mode. All other bits are ignored. */
13206 if (CONST_INT_P (op0) && !const_0_to_7_operand(op0, VOIDmode))
13208 error ("Argument must be a value between 0 and 7.");
13209 return const0_rtx;
13212 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
13213 op0 = copy_to_mode_reg (mode0, op0);
13215 pat = GEN_FCN (icode) (op0);
13216 if (! pat)
13217 return const0_rtx;
13218 emit_insn (pat);
13220 return NULL_RTX;
13223 static rtx
13224 rs6000_expand_unop_builtin (enum insn_code icode, tree exp, rtx target)
13226 rtx pat;
13227 tree arg0 = CALL_EXPR_ARG (exp, 0);
13228 rtx op0 = expand_normal (arg0);
13229 machine_mode tmode = insn_data[icode].operand[0].mode;
13230 machine_mode mode0 = insn_data[icode].operand[1].mode;
13232 if (icode == CODE_FOR_nothing)
13233 /* Builtin not supported on this processor. */
13234 return 0;
13236 /* If we got invalid arguments bail out before generating bad rtl. */
13237 if (arg0 == error_mark_node)
13238 return const0_rtx;
13240 if (icode == CODE_FOR_altivec_vspltisb
13241 || icode == CODE_FOR_altivec_vspltish
13242 || icode == CODE_FOR_altivec_vspltisw)
13244 /* Only allow 5-bit *signed* literals. */
13245 if (!CONST_INT_P (op0)
13246 || INTVAL (op0) > 15
13247 || INTVAL (op0) < -16)
13249 error ("argument 1 must be a 5-bit signed literal");
13250 return CONST0_RTX (tmode);
13254 if (target == 0
13255 || GET_MODE (target) != tmode
13256 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13257 target = gen_reg_rtx (tmode);
13259 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13260 op0 = copy_to_mode_reg (mode0, op0);
13262 pat = GEN_FCN (icode) (target, op0);
13263 if (! pat)
13264 return 0;
13265 emit_insn (pat);
13267 return target;
13270 static rtx
13271 altivec_expand_abs_builtin (enum insn_code icode, tree exp, rtx target)
13273 rtx pat, scratch1, scratch2;
13274 tree arg0 = CALL_EXPR_ARG (exp, 0);
13275 rtx op0 = expand_normal (arg0);
13276 machine_mode tmode = insn_data[icode].operand[0].mode;
13277 machine_mode mode0 = insn_data[icode].operand[1].mode;
13279 /* If we have invalid arguments, bail out before generating bad rtl. */
13280 if (arg0 == error_mark_node)
13281 return const0_rtx;
13283 if (target == 0
13284 || GET_MODE (target) != tmode
13285 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13286 target = gen_reg_rtx (tmode);
13288 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13289 op0 = copy_to_mode_reg (mode0, op0);
13291 scratch1 = gen_reg_rtx (mode0);
13292 scratch2 = gen_reg_rtx (mode0);
13294 pat = GEN_FCN (icode) (target, op0, scratch1, scratch2);
13295 if (! pat)
13296 return 0;
13297 emit_insn (pat);
13299 return target;
13302 static rtx
13303 rs6000_expand_binop_builtin (enum insn_code icode, tree exp, rtx target)
13305 rtx pat;
13306 tree arg0 = CALL_EXPR_ARG (exp, 0);
13307 tree arg1 = CALL_EXPR_ARG (exp, 1);
13308 rtx op0 = expand_normal (arg0);
13309 rtx op1 = expand_normal (arg1);
13310 machine_mode tmode = insn_data[icode].operand[0].mode;
13311 machine_mode mode0 = insn_data[icode].operand[1].mode;
13312 machine_mode mode1 = insn_data[icode].operand[2].mode;
13314 if (icode == CODE_FOR_nothing)
13315 /* Builtin not supported on this processor. */
13316 return 0;
13318 /* If we got invalid arguments bail out before generating bad rtl. */
13319 if (arg0 == error_mark_node || arg1 == error_mark_node)
13320 return const0_rtx;
13322 if (icode == CODE_FOR_unpackv1ti
13323 || icode == CODE_FOR_unpackkf
13324 || icode == CODE_FOR_unpacktf
13325 || icode == CODE_FOR_unpackif
13326 || icode == CODE_FOR_unpacktd)
13328 /* Only allow 1-bit unsigned literals. */
13329 STRIP_NOPS (arg1);
13330 if (TREE_CODE (arg1) != INTEGER_CST
13331 || !IN_RANGE (TREE_INT_CST_LOW (arg1), 0, 1))
13333 error ("argument 2 must be a 1-bit unsigned literal");
13334 return CONST0_RTX (tmode);
13337 else if (icode == CODE_FOR_altivec_vspltw)
13339 /* Only allow 2-bit unsigned literals. */
13340 STRIP_NOPS (arg1);
13341 if (TREE_CODE (arg1) != INTEGER_CST
13342 || TREE_INT_CST_LOW (arg1) & ~3)
13344 error ("argument 2 must be a 2-bit unsigned literal");
13345 return CONST0_RTX (tmode);
13348 else if (icode == CODE_FOR_altivec_vsplth)
13350 /* Only allow 3-bit unsigned literals. */
13351 STRIP_NOPS (arg1);
13352 if (TREE_CODE (arg1) != INTEGER_CST
13353 || TREE_INT_CST_LOW (arg1) & ~7)
13355 error ("argument 2 must be a 3-bit unsigned literal");
13356 return CONST0_RTX (tmode);
13359 else if (icode == CODE_FOR_altivec_vspltb)
13361 /* Only allow 4-bit unsigned literals. */
13362 STRIP_NOPS (arg1);
13363 if (TREE_CODE (arg1) != INTEGER_CST
13364 || TREE_INT_CST_LOW (arg1) & ~15)
13366 error ("argument 2 must be a 4-bit unsigned literal");
13367 return CONST0_RTX (tmode);
13370 else if (icode == CODE_FOR_altivec_vcfux
13371 || icode == CODE_FOR_altivec_vcfsx
13372 || icode == CODE_FOR_altivec_vctsxs
13373 || icode == CODE_FOR_altivec_vctuxs)
13375 /* Only allow 5-bit unsigned literals. */
13376 STRIP_NOPS (arg1);
13377 if (TREE_CODE (arg1) != INTEGER_CST
13378 || TREE_INT_CST_LOW (arg1) & ~0x1f)
13380 error ("argument 2 must be a 5-bit unsigned literal");
13381 return CONST0_RTX (tmode);
13384 else if (icode == CODE_FOR_dfptstsfi_eq_dd
13385 || icode == CODE_FOR_dfptstsfi_lt_dd
13386 || icode == CODE_FOR_dfptstsfi_gt_dd
13387 || icode == CODE_FOR_dfptstsfi_unordered_dd
13388 || icode == CODE_FOR_dfptstsfi_eq_td
13389 || icode == CODE_FOR_dfptstsfi_lt_td
13390 || icode == CODE_FOR_dfptstsfi_gt_td
13391 || icode == CODE_FOR_dfptstsfi_unordered_td)
13393 /* Only allow 6-bit unsigned literals. */
13394 STRIP_NOPS (arg0);
13395 if (TREE_CODE (arg0) != INTEGER_CST
13396 || !IN_RANGE (TREE_INT_CST_LOW (arg0), 0, 63))
13398 error ("argument 1 must be a 6-bit unsigned literal");
13399 return CONST0_RTX (tmode);
13402 else if (icode == CODE_FOR_xststdcqp_kf
13403 || icode == CODE_FOR_xststdcqp_tf
13404 || icode == CODE_FOR_xststdcdp
13405 || icode == CODE_FOR_xststdcsp
13406 || icode == CODE_FOR_xvtstdcdp
13407 || icode == CODE_FOR_xvtstdcsp)
13409 /* Only allow 7-bit unsigned literals. */
13410 STRIP_NOPS (arg1);
13411 if (TREE_CODE (arg1) != INTEGER_CST
13412 || !IN_RANGE (TREE_INT_CST_LOW (arg1), 0, 127))
13414 error ("argument 2 must be a 7-bit unsigned literal");
13415 return CONST0_RTX (tmode);
13419 if (target == 0
13420 || GET_MODE (target) != tmode
13421 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13422 target = gen_reg_rtx (tmode);
13424 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13425 op0 = copy_to_mode_reg (mode0, op0);
13426 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
13427 op1 = copy_to_mode_reg (mode1, op1);
13429 pat = GEN_FCN (icode) (target, op0, op1);
13430 if (! pat)
13431 return 0;
13432 emit_insn (pat);
13434 return target;
13437 static rtx
13438 altivec_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
13440 rtx pat, scratch;
13441 tree cr6_form = CALL_EXPR_ARG (exp, 0);
13442 tree arg0 = CALL_EXPR_ARG (exp, 1);
13443 tree arg1 = CALL_EXPR_ARG (exp, 2);
13444 rtx op0 = expand_normal (arg0);
13445 rtx op1 = expand_normal (arg1);
13446 machine_mode tmode = SImode;
13447 machine_mode mode0 = insn_data[icode].operand[1].mode;
13448 machine_mode mode1 = insn_data[icode].operand[2].mode;
13449 int cr6_form_int;
13451 if (TREE_CODE (cr6_form) != INTEGER_CST)
13453 error ("argument 1 of %qs must be a constant",
13454 "__builtin_altivec_predicate");
13455 return const0_rtx;
13457 else
13458 cr6_form_int = TREE_INT_CST_LOW (cr6_form);
13460 gcc_assert (mode0 == mode1);
13462 /* If we have invalid arguments, bail out before generating bad rtl. */
13463 if (arg0 == error_mark_node || arg1 == error_mark_node)
13464 return const0_rtx;
13466 if (target == 0
13467 || GET_MODE (target) != tmode
13468 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13469 target = gen_reg_rtx (tmode);
13471 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13472 op0 = copy_to_mode_reg (mode0, op0);
13473 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
13474 op1 = copy_to_mode_reg (mode1, op1);
13476 /* Note that for many of the relevant operations (e.g. cmpne or
13477 cmpeq) with float or double operands, it makes more sense for the
13478 mode of the allocated scratch register to select a vector of
13479 integer. But the choice to copy the mode of operand 0 was made
13480 long ago and there are no plans to change it. */
13481 scratch = gen_reg_rtx (mode0);
13483 pat = GEN_FCN (icode) (scratch, op0, op1);
13484 if (! pat)
13485 return 0;
13486 emit_insn (pat);
13488 /* The vec_any* and vec_all* predicates use the same opcodes for two
13489 different operations, but the bits in CR6 will be different
13490 depending on what information we want. So we have to play tricks
13491 with CR6 to get the right bits out.
13493 If you think this is disgusting, look at the specs for the
13494 AltiVec predicates. */
13496 switch (cr6_form_int)
13498 case 0:
13499 emit_insn (gen_cr6_test_for_zero (target));
13500 break;
13501 case 1:
13502 emit_insn (gen_cr6_test_for_zero_reverse (target));
13503 break;
13504 case 2:
13505 emit_insn (gen_cr6_test_for_lt (target));
13506 break;
13507 case 3:
13508 emit_insn (gen_cr6_test_for_lt_reverse (target));
13509 break;
13510 default:
13511 error ("argument 1 of %qs is out of range",
13512 "__builtin_altivec_predicate");
13513 break;
13516 return target;
13520 swap_endian_selector_for_mode (machine_mode mode)
13522 unsigned int swap1[16] = {15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0};
13523 unsigned int swap2[16] = {7,6,5,4,3,2,1,0,15,14,13,12,11,10,9,8};
13524 unsigned int swap4[16] = {3,2,1,0,7,6,5,4,11,10,9,8,15,14,13,12};
13525 unsigned int swap8[16] = {1,0,3,2,5,4,7,6,9,8,11,10,13,12,15,14};
13527 unsigned int *swaparray, i;
13528 rtx perm[16];
13530 switch (mode)
13532 case E_V1TImode:
13533 swaparray = swap1;
13534 break;
13535 case E_V2DFmode:
13536 case E_V2DImode:
13537 swaparray = swap2;
13538 break;
13539 case E_V4SFmode:
13540 case E_V4SImode:
13541 swaparray = swap4;
13542 break;
13543 case E_V8HImode:
13544 swaparray = swap8;
13545 break;
13546 default:
13547 gcc_unreachable ();
13550 for (i = 0; i < 16; ++i)
13551 perm[i] = GEN_INT (swaparray[i]);
13553 return force_reg (V16QImode, gen_rtx_CONST_VECTOR (V16QImode,
13554 gen_rtvec_v (16, perm)));
13557 static rtx
13558 altivec_expand_lv_builtin (enum insn_code icode, tree exp, rtx target, bool blk)
13560 rtx pat, addr;
13561 tree arg0 = CALL_EXPR_ARG (exp, 0);
13562 tree arg1 = CALL_EXPR_ARG (exp, 1);
13563 machine_mode tmode = insn_data[icode].operand[0].mode;
13564 machine_mode mode0 = Pmode;
13565 machine_mode mode1 = Pmode;
13566 rtx op0 = expand_normal (arg0);
13567 rtx op1 = expand_normal (arg1);
13569 if (icode == CODE_FOR_nothing)
13570 /* Builtin not supported on this processor. */
13571 return 0;
13573 /* If we got invalid arguments bail out before generating bad rtl. */
13574 if (arg0 == error_mark_node || arg1 == error_mark_node)
13575 return const0_rtx;
13577 if (target == 0
13578 || GET_MODE (target) != tmode
13579 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13580 target = gen_reg_rtx (tmode);
13582 op1 = copy_to_mode_reg (mode1, op1);
13584 /* For LVX, express the RTL accurately by ANDing the address with -16.
13585 LVXL and LVE*X expand to use UNSPECs to hide their special behavior,
13586 so the raw address is fine. */
13587 if (icode == CODE_FOR_altivec_lvx_v1ti
13588 || icode == CODE_FOR_altivec_lvx_v2df
13589 || icode == CODE_FOR_altivec_lvx_v2di
13590 || icode == CODE_FOR_altivec_lvx_v4sf
13591 || icode == CODE_FOR_altivec_lvx_v4si
13592 || icode == CODE_FOR_altivec_lvx_v8hi
13593 || icode == CODE_FOR_altivec_lvx_v16qi)
13595 rtx rawaddr;
13596 if (op0 == const0_rtx)
13597 rawaddr = op1;
13598 else
13600 op0 = copy_to_mode_reg (mode0, op0);
13601 rawaddr = gen_rtx_PLUS (Pmode, op1, op0);
13603 addr = gen_rtx_AND (Pmode, rawaddr, gen_rtx_CONST_INT (Pmode, -16));
13604 addr = gen_rtx_MEM (blk ? BLKmode : tmode, addr);
13606 emit_insn (gen_rtx_SET (target, addr));
13608 else
13610 if (op0 == const0_rtx)
13611 addr = gen_rtx_MEM (blk ? BLKmode : tmode, op1);
13612 else
13614 op0 = copy_to_mode_reg (mode0, op0);
13615 addr = gen_rtx_MEM (blk ? BLKmode : tmode,
13616 gen_rtx_PLUS (Pmode, op1, op0));
13619 pat = GEN_FCN (icode) (target, addr);
13620 if (! pat)
13621 return 0;
13622 emit_insn (pat);
13625 return target;
13628 static rtx
13629 altivec_expand_stxvl_builtin (enum insn_code icode, tree exp)
13631 rtx pat;
13632 tree arg0 = CALL_EXPR_ARG (exp, 0);
13633 tree arg1 = CALL_EXPR_ARG (exp, 1);
13634 tree arg2 = CALL_EXPR_ARG (exp, 2);
13635 rtx op0 = expand_normal (arg0);
13636 rtx op1 = expand_normal (arg1);
13637 rtx op2 = expand_normal (arg2);
13638 machine_mode mode0 = insn_data[icode].operand[0].mode;
13639 machine_mode mode1 = insn_data[icode].operand[1].mode;
13640 machine_mode mode2 = insn_data[icode].operand[2].mode;
13642 if (icode == CODE_FOR_nothing)
13643 /* Builtin not supported on this processor. */
13644 return NULL_RTX;
13646 /* If we got invalid arguments bail out before generating bad rtl. */
13647 if (arg0 == error_mark_node
13648 || arg1 == error_mark_node
13649 || arg2 == error_mark_node)
13650 return NULL_RTX;
13652 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13653 op0 = copy_to_mode_reg (mode0, op0);
13654 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
13655 op1 = copy_to_mode_reg (mode1, op1);
13656 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
13657 op2 = copy_to_mode_reg (mode2, op2);
13659 pat = GEN_FCN (icode) (op0, op1, op2);
13660 if (pat)
13661 emit_insn (pat);
13663 return NULL_RTX;
13666 static rtx
13667 altivec_expand_stv_builtin (enum insn_code icode, tree exp)
13669 tree arg0 = CALL_EXPR_ARG (exp, 0);
13670 tree arg1 = CALL_EXPR_ARG (exp, 1);
13671 tree arg2 = CALL_EXPR_ARG (exp, 2);
13672 rtx op0 = expand_normal (arg0);
13673 rtx op1 = expand_normal (arg1);
13674 rtx op2 = expand_normal (arg2);
13675 rtx pat, addr, rawaddr;
13676 machine_mode tmode = insn_data[icode].operand[0].mode;
13677 machine_mode smode = insn_data[icode].operand[1].mode;
13678 machine_mode mode1 = Pmode;
13679 machine_mode mode2 = Pmode;
13681 /* Invalid arguments. Bail before doing anything stoopid! */
13682 if (arg0 == error_mark_node
13683 || arg1 == error_mark_node
13684 || arg2 == error_mark_node)
13685 return const0_rtx;
13687 op2 = copy_to_mode_reg (mode2, op2);
13689 /* For STVX, express the RTL accurately by ANDing the address with -16.
13690 STVXL and STVE*X expand to use UNSPECs to hide their special behavior,
13691 so the raw address is fine. */
13692 if (icode == CODE_FOR_altivec_stvx_v2df
13693 || icode == CODE_FOR_altivec_stvx_v2di
13694 || icode == CODE_FOR_altivec_stvx_v4sf
13695 || icode == CODE_FOR_altivec_stvx_v4si
13696 || icode == CODE_FOR_altivec_stvx_v8hi
13697 || icode == CODE_FOR_altivec_stvx_v16qi)
13699 if (op1 == const0_rtx)
13700 rawaddr = op2;
13701 else
13703 op1 = copy_to_mode_reg (mode1, op1);
13704 rawaddr = gen_rtx_PLUS (Pmode, op2, op1);
13707 addr = gen_rtx_AND (Pmode, rawaddr, gen_rtx_CONST_INT (Pmode, -16));
13708 addr = gen_rtx_MEM (tmode, addr);
13710 op0 = copy_to_mode_reg (tmode, op0);
13712 emit_insn (gen_rtx_SET (addr, op0));
13714 else
13716 if (! (*insn_data[icode].operand[1].predicate) (op0, smode))
13717 op0 = copy_to_mode_reg (smode, op0);
13719 if (op1 == const0_rtx)
13720 addr = gen_rtx_MEM (tmode, op2);
13721 else
13723 op1 = copy_to_mode_reg (mode1, op1);
13724 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op2, op1));
13727 pat = GEN_FCN (icode) (addr, op0);
13728 if (pat)
13729 emit_insn (pat);
13732 return NULL_RTX;
13735 /* Return the appropriate SPR number associated with the given builtin. */
13736 static inline HOST_WIDE_INT
13737 htm_spr_num (enum rs6000_builtins code)
13739 if (code == HTM_BUILTIN_GET_TFHAR
13740 || code == HTM_BUILTIN_SET_TFHAR)
13741 return TFHAR_SPR;
13742 else if (code == HTM_BUILTIN_GET_TFIAR
13743 || code == HTM_BUILTIN_SET_TFIAR)
13744 return TFIAR_SPR;
13745 else if (code == HTM_BUILTIN_GET_TEXASR
13746 || code == HTM_BUILTIN_SET_TEXASR)
13747 return TEXASR_SPR;
13748 gcc_assert (code == HTM_BUILTIN_GET_TEXASRU
13749 || code == HTM_BUILTIN_SET_TEXASRU);
13750 return TEXASRU_SPR;
13753 /* Return the correct ICODE value depending on whether we are
13754 setting or reading the HTM SPRs. */
13755 static inline enum insn_code
13756 rs6000_htm_spr_icode (bool nonvoid)
13758 if (nonvoid)
13759 return (TARGET_POWERPC64) ? CODE_FOR_htm_mfspr_di : CODE_FOR_htm_mfspr_si;
13760 else
13761 return (TARGET_POWERPC64) ? CODE_FOR_htm_mtspr_di : CODE_FOR_htm_mtspr_si;
13764 /* Expand the HTM builtin in EXP and store the result in TARGET.
13765 Store true in *EXPANDEDP if we found a builtin to expand. */
13766 static rtx
13767 htm_expand_builtin (tree exp, rtx target, bool * expandedp)
13769 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
13770 bool nonvoid = TREE_TYPE (TREE_TYPE (fndecl)) != void_type_node;
13771 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
13772 const struct builtin_description *d;
13773 size_t i;
13775 *expandedp = true;
13777 if (!TARGET_POWERPC64
13778 && (fcode == HTM_BUILTIN_TABORTDC
13779 || fcode == HTM_BUILTIN_TABORTDCI))
13781 size_t uns_fcode = (size_t)fcode;
13782 const char *name = rs6000_builtin_info[uns_fcode].name;
13783 error ("builtin %qs is only valid in 64-bit mode", name);
13784 return const0_rtx;
13787 /* Expand the HTM builtins. */
13788 d = bdesc_htm;
13789 for (i = 0; i < ARRAY_SIZE (bdesc_htm); i++, d++)
13790 if (d->code == fcode)
13792 rtx op[MAX_HTM_OPERANDS], pat;
13793 int nopnds = 0;
13794 tree arg;
13795 call_expr_arg_iterator iter;
13796 unsigned attr = rs6000_builtin_info[fcode].attr;
13797 enum insn_code icode = d->icode;
13798 const struct insn_operand_data *insn_op;
13799 bool uses_spr = (attr & RS6000_BTC_SPR);
13800 rtx cr = NULL_RTX;
13802 if (uses_spr)
13803 icode = rs6000_htm_spr_icode (nonvoid);
13804 insn_op = &insn_data[icode].operand[0];
13806 if (nonvoid)
13808 machine_mode tmode = (uses_spr) ? insn_op->mode : E_SImode;
13809 if (!target
13810 || GET_MODE (target) != tmode
13811 || (uses_spr && !(*insn_op->predicate) (target, tmode)))
13812 target = gen_reg_rtx (tmode);
13813 if (uses_spr)
13814 op[nopnds++] = target;
13817 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
13819 if (arg == error_mark_node || nopnds >= MAX_HTM_OPERANDS)
13820 return const0_rtx;
13822 insn_op = &insn_data[icode].operand[nopnds];
13824 op[nopnds] = expand_normal (arg);
13826 if (!(*insn_op->predicate) (op[nopnds], insn_op->mode))
13828 if (!strcmp (insn_op->constraint, "n"))
13830 int arg_num = (nonvoid) ? nopnds : nopnds + 1;
13831 if (!CONST_INT_P (op[nopnds]))
13832 error ("argument %d must be an unsigned literal", arg_num);
13833 else
13834 error ("argument %d is an unsigned literal that is "
13835 "out of range", arg_num);
13836 return const0_rtx;
13838 op[nopnds] = copy_to_mode_reg (insn_op->mode, op[nopnds]);
13841 nopnds++;
13844 /* Handle the builtins for extended mnemonics. These accept
13845 no arguments, but map to builtins that take arguments. */
13846 switch (fcode)
13848 case HTM_BUILTIN_TENDALL: /* Alias for: tend. 1 */
13849 case HTM_BUILTIN_TRESUME: /* Alias for: tsr. 1 */
13850 op[nopnds++] = GEN_INT (1);
13851 if (flag_checking)
13852 attr |= RS6000_BTC_UNARY;
13853 break;
13854 case HTM_BUILTIN_TSUSPEND: /* Alias for: tsr. 0 */
13855 op[nopnds++] = GEN_INT (0);
13856 if (flag_checking)
13857 attr |= RS6000_BTC_UNARY;
13858 break;
13859 default:
13860 break;
13863 /* If this builtin accesses SPRs, then pass in the appropriate
13864 SPR number and SPR regno as the last two operands. */
13865 if (uses_spr)
13867 machine_mode mode = (TARGET_POWERPC64) ? DImode : SImode;
13868 op[nopnds++] = gen_rtx_CONST_INT (mode, htm_spr_num (fcode));
13870 /* If this builtin accesses a CR, then pass in a scratch
13871 CR as the last operand. */
13872 else if (attr & RS6000_BTC_CR)
13873 { cr = gen_reg_rtx (CCmode);
13874 op[nopnds++] = cr;
13877 if (flag_checking)
13879 int expected_nopnds = 0;
13880 if ((attr & RS6000_BTC_TYPE_MASK) == RS6000_BTC_UNARY)
13881 expected_nopnds = 1;
13882 else if ((attr & RS6000_BTC_TYPE_MASK) == RS6000_BTC_BINARY)
13883 expected_nopnds = 2;
13884 else if ((attr & RS6000_BTC_TYPE_MASK) == RS6000_BTC_TERNARY)
13885 expected_nopnds = 3;
13886 if (!(attr & RS6000_BTC_VOID))
13887 expected_nopnds += 1;
13888 if (uses_spr)
13889 expected_nopnds += 1;
13891 gcc_assert (nopnds == expected_nopnds
13892 && nopnds <= MAX_HTM_OPERANDS);
13895 switch (nopnds)
13897 case 1:
13898 pat = GEN_FCN (icode) (op[0]);
13899 break;
13900 case 2:
13901 pat = GEN_FCN (icode) (op[0], op[1]);
13902 break;
13903 case 3:
13904 pat = GEN_FCN (icode) (op[0], op[1], op[2]);
13905 break;
13906 case 4:
13907 pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3]);
13908 break;
13909 default:
13910 gcc_unreachable ();
13912 if (!pat)
13913 return NULL_RTX;
13914 emit_insn (pat);
13916 if (attr & RS6000_BTC_CR)
13918 if (fcode == HTM_BUILTIN_TBEGIN)
13920 /* Emit code to set TARGET to true or false depending on
13921 whether the tbegin. instruction successfully or failed
13922 to start a transaction. We do this by placing the 1's
13923 complement of CR's EQ bit into TARGET. */
13924 rtx scratch = gen_reg_rtx (SImode);
13925 emit_insn (gen_rtx_SET (scratch,
13926 gen_rtx_EQ (SImode, cr,
13927 const0_rtx)));
13928 emit_insn (gen_rtx_SET (target,
13929 gen_rtx_XOR (SImode, scratch,
13930 GEN_INT (1))));
13932 else
13934 /* Emit code to copy the 4-bit condition register field
13935 CR into the least significant end of register TARGET. */
13936 rtx scratch1 = gen_reg_rtx (SImode);
13937 rtx scratch2 = gen_reg_rtx (SImode);
13938 rtx subreg = simplify_gen_subreg (CCmode, scratch1, SImode, 0);
13939 emit_insn (gen_movcc (subreg, cr));
13940 emit_insn (gen_lshrsi3 (scratch2, scratch1, GEN_INT (28)));
13941 emit_insn (gen_andsi3 (target, scratch2, GEN_INT (0xf)));
13945 if (nonvoid)
13946 return target;
13947 return const0_rtx;
13950 *expandedp = false;
13951 return NULL_RTX;
13954 /* Expand the CPU builtin in FCODE and store the result in TARGET. */
13956 static rtx
13957 cpu_expand_builtin (enum rs6000_builtins fcode, tree exp ATTRIBUTE_UNUSED,
13958 rtx target)
13960 /* __builtin_cpu_init () is a nop, so expand to nothing. */
13961 if (fcode == RS6000_BUILTIN_CPU_INIT)
13962 return const0_rtx;
13964 if (target == 0 || GET_MODE (target) != SImode)
13965 target = gen_reg_rtx (SImode);
13967 #ifdef TARGET_LIBC_PROVIDES_HWCAP_IN_TCB
13968 tree arg = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
13969 /* Target clones creates an ARRAY_REF instead of STRING_CST, convert it back
13970 to a STRING_CST. */
13971 if (TREE_CODE (arg) == ARRAY_REF
13972 && TREE_CODE (TREE_OPERAND (arg, 0)) == STRING_CST
13973 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST
13974 && compare_tree_int (TREE_OPERAND (arg, 1), 0) == 0)
13975 arg = TREE_OPERAND (arg, 0);
13977 if (TREE_CODE (arg) != STRING_CST)
13979 error ("builtin %qs only accepts a string argument",
13980 rs6000_builtin_info[(size_t) fcode].name);
13981 return const0_rtx;
13984 if (fcode == RS6000_BUILTIN_CPU_IS)
13986 const char *cpu = TREE_STRING_POINTER (arg);
13987 rtx cpuid = NULL_RTX;
13988 for (size_t i = 0; i < ARRAY_SIZE (cpu_is_info); i++)
13989 if (strcmp (cpu, cpu_is_info[i].cpu) == 0)
13991 /* The CPUID value in the TCB is offset by _DL_FIRST_PLATFORM. */
13992 cpuid = GEN_INT (cpu_is_info[i].cpuid + _DL_FIRST_PLATFORM);
13993 break;
13995 if (cpuid == NULL_RTX)
13997 /* Invalid CPU argument. */
13998 error ("cpu %qs is an invalid argument to builtin %qs",
13999 cpu, rs6000_builtin_info[(size_t) fcode].name);
14000 return const0_rtx;
14003 rtx platform = gen_reg_rtx (SImode);
14004 rtx tcbmem = gen_const_mem (SImode,
14005 gen_rtx_PLUS (Pmode,
14006 gen_rtx_REG (Pmode, TLS_REGNUM),
14007 GEN_INT (TCB_PLATFORM_OFFSET)));
14008 emit_move_insn (platform, tcbmem);
14009 emit_insn (gen_eqsi3 (target, platform, cpuid));
14011 else if (fcode == RS6000_BUILTIN_CPU_SUPPORTS)
14013 const char *hwcap = TREE_STRING_POINTER (arg);
14014 rtx mask = NULL_RTX;
14015 int hwcap_offset;
14016 for (size_t i = 0; i < ARRAY_SIZE (cpu_supports_info); i++)
14017 if (strcmp (hwcap, cpu_supports_info[i].hwcap) == 0)
14019 mask = GEN_INT (cpu_supports_info[i].mask);
14020 hwcap_offset = TCB_HWCAP_OFFSET (cpu_supports_info[i].id);
14021 break;
14023 if (mask == NULL_RTX)
14025 /* Invalid HWCAP argument. */
14026 error ("%s %qs is an invalid argument to builtin %qs",
14027 "hwcap", hwcap, rs6000_builtin_info[(size_t) fcode].name);
14028 return const0_rtx;
14031 rtx tcb_hwcap = gen_reg_rtx (SImode);
14032 rtx tcbmem = gen_const_mem (SImode,
14033 gen_rtx_PLUS (Pmode,
14034 gen_rtx_REG (Pmode, TLS_REGNUM),
14035 GEN_INT (hwcap_offset)));
14036 emit_move_insn (tcb_hwcap, tcbmem);
14037 rtx scratch1 = gen_reg_rtx (SImode);
14038 emit_insn (gen_rtx_SET (scratch1, gen_rtx_AND (SImode, tcb_hwcap, mask)));
14039 rtx scratch2 = gen_reg_rtx (SImode);
14040 emit_insn (gen_eqsi3 (scratch2, scratch1, const0_rtx));
14041 emit_insn (gen_rtx_SET (target, gen_rtx_XOR (SImode, scratch2, const1_rtx)));
14043 else
14044 gcc_unreachable ();
14046 /* Record that we have expanded a CPU builtin, so that we can later
14047 emit a reference to the special symbol exported by LIBC to ensure we
14048 do not link against an old LIBC that doesn't support this feature. */
14049 cpu_builtin_p = true;
14051 #else
14052 warning (0, "builtin %qs needs GLIBC (2.23 and newer) that exports hardware "
14053 "capability bits", rs6000_builtin_info[(size_t) fcode].name);
14055 /* For old LIBCs, always return FALSE. */
14056 emit_move_insn (target, GEN_INT (0));
14057 #endif /* TARGET_LIBC_PROVIDES_HWCAP_IN_TCB */
14059 return target;
14062 static rtx
14063 rs6000_expand_ternop_builtin (enum insn_code icode, tree exp, rtx target)
14065 rtx pat;
14066 tree arg0 = CALL_EXPR_ARG (exp, 0);
14067 tree arg1 = CALL_EXPR_ARG (exp, 1);
14068 tree arg2 = CALL_EXPR_ARG (exp, 2);
14069 rtx op0 = expand_normal (arg0);
14070 rtx op1 = expand_normal (arg1);
14071 rtx op2 = expand_normal (arg2);
14072 machine_mode tmode = insn_data[icode].operand[0].mode;
14073 machine_mode mode0 = insn_data[icode].operand[1].mode;
14074 machine_mode mode1 = insn_data[icode].operand[2].mode;
14075 machine_mode mode2 = insn_data[icode].operand[3].mode;
14077 if (icode == CODE_FOR_nothing)
14078 /* Builtin not supported on this processor. */
14079 return 0;
14081 /* If we got invalid arguments bail out before generating bad rtl. */
14082 if (arg0 == error_mark_node
14083 || arg1 == error_mark_node
14084 || arg2 == error_mark_node)
14085 return const0_rtx;
14087 /* Check and prepare argument depending on the instruction code.
14089 Note that a switch statement instead of the sequence of tests
14090 would be incorrect as many of the CODE_FOR values could be
14091 CODE_FOR_nothing and that would yield multiple alternatives
14092 with identical values. We'd never reach here at runtime in
14093 this case. */
14094 if (icode == CODE_FOR_altivec_vsldoi_v4sf
14095 || icode == CODE_FOR_altivec_vsldoi_v2df
14096 || icode == CODE_FOR_altivec_vsldoi_v4si
14097 || icode == CODE_FOR_altivec_vsldoi_v8hi
14098 || icode == CODE_FOR_altivec_vsldoi_v16qi)
14100 /* Only allow 4-bit unsigned literals. */
14101 STRIP_NOPS (arg2);
14102 if (TREE_CODE (arg2) != INTEGER_CST
14103 || TREE_INT_CST_LOW (arg2) & ~0xf)
14105 error ("argument 3 must be a 4-bit unsigned literal");
14106 return CONST0_RTX (tmode);
14109 else if (icode == CODE_FOR_vsx_xxpermdi_v2df
14110 || icode == CODE_FOR_vsx_xxpermdi_v2di
14111 || icode == CODE_FOR_vsx_xxpermdi_v2df_be
14112 || icode == CODE_FOR_vsx_xxpermdi_v2di_be
14113 || icode == CODE_FOR_vsx_xxpermdi_v1ti
14114 || icode == CODE_FOR_vsx_xxpermdi_v4sf
14115 || icode == CODE_FOR_vsx_xxpermdi_v4si
14116 || icode == CODE_FOR_vsx_xxpermdi_v8hi
14117 || icode == CODE_FOR_vsx_xxpermdi_v16qi
14118 || icode == CODE_FOR_vsx_xxsldwi_v16qi
14119 || icode == CODE_FOR_vsx_xxsldwi_v8hi
14120 || icode == CODE_FOR_vsx_xxsldwi_v4si
14121 || icode == CODE_FOR_vsx_xxsldwi_v4sf
14122 || icode == CODE_FOR_vsx_xxsldwi_v2di
14123 || icode == CODE_FOR_vsx_xxsldwi_v2df)
14125 /* Only allow 2-bit unsigned literals. */
14126 STRIP_NOPS (arg2);
14127 if (TREE_CODE (arg2) != INTEGER_CST
14128 || TREE_INT_CST_LOW (arg2) & ~0x3)
14130 error ("argument 3 must be a 2-bit unsigned literal");
14131 return CONST0_RTX (tmode);
14134 else if (icode == CODE_FOR_vsx_set_v2df
14135 || icode == CODE_FOR_vsx_set_v2di
14136 || icode == CODE_FOR_bcdadd
14137 || icode == CODE_FOR_bcdadd_lt
14138 || icode == CODE_FOR_bcdadd_eq
14139 || icode == CODE_FOR_bcdadd_gt
14140 || icode == CODE_FOR_bcdsub
14141 || icode == CODE_FOR_bcdsub_lt
14142 || icode == CODE_FOR_bcdsub_eq
14143 || icode == CODE_FOR_bcdsub_gt)
14145 /* Only allow 1-bit unsigned literals. */
14146 STRIP_NOPS (arg2);
14147 if (TREE_CODE (arg2) != INTEGER_CST
14148 || TREE_INT_CST_LOW (arg2) & ~0x1)
14150 error ("argument 3 must be a 1-bit unsigned literal");
14151 return CONST0_RTX (tmode);
14154 else if (icode == CODE_FOR_dfp_ddedpd_dd
14155 || icode == CODE_FOR_dfp_ddedpd_td)
14157 /* Only allow 2-bit unsigned literals where the value is 0 or 2. */
14158 STRIP_NOPS (arg0);
14159 if (TREE_CODE (arg0) != INTEGER_CST
14160 || TREE_INT_CST_LOW (arg2) & ~0x3)
14162 error ("argument 1 must be 0 or 2");
14163 return CONST0_RTX (tmode);
14166 else if (icode == CODE_FOR_dfp_denbcd_dd
14167 || icode == CODE_FOR_dfp_denbcd_td)
14169 /* Only allow 1-bit unsigned literals. */
14170 STRIP_NOPS (arg0);
14171 if (TREE_CODE (arg0) != INTEGER_CST
14172 || TREE_INT_CST_LOW (arg0) & ~0x1)
14174 error ("argument 1 must be a 1-bit unsigned literal");
14175 return CONST0_RTX (tmode);
14178 else if (icode == CODE_FOR_dfp_dscli_dd
14179 || icode == CODE_FOR_dfp_dscli_td
14180 || icode == CODE_FOR_dfp_dscri_dd
14181 || icode == CODE_FOR_dfp_dscri_td)
14183 /* Only allow 6-bit unsigned literals. */
14184 STRIP_NOPS (arg1);
14185 if (TREE_CODE (arg1) != INTEGER_CST
14186 || TREE_INT_CST_LOW (arg1) & ~0x3f)
14188 error ("argument 2 must be a 6-bit unsigned literal");
14189 return CONST0_RTX (tmode);
14192 else if (icode == CODE_FOR_crypto_vshasigmaw
14193 || icode == CODE_FOR_crypto_vshasigmad)
14195 /* Check whether the 2nd and 3rd arguments are integer constants and in
14196 range and prepare arguments. */
14197 STRIP_NOPS (arg1);
14198 if (TREE_CODE (arg1) != INTEGER_CST || wi::geu_p (wi::to_wide (arg1), 2))
14200 error ("argument 2 must be 0 or 1");
14201 return CONST0_RTX (tmode);
14204 STRIP_NOPS (arg2);
14205 if (TREE_CODE (arg2) != INTEGER_CST
14206 || wi::geu_p (wi::to_wide (arg2), 16))
14208 error ("argument 3 must be in the range [0, 15]");
14209 return CONST0_RTX (tmode);
14213 if (target == 0
14214 || GET_MODE (target) != tmode
14215 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
14216 target = gen_reg_rtx (tmode);
14218 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
14219 op0 = copy_to_mode_reg (mode0, op0);
14220 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
14221 op1 = copy_to_mode_reg (mode1, op1);
14222 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
14223 op2 = copy_to_mode_reg (mode2, op2);
14225 pat = GEN_FCN (icode) (target, op0, op1, op2);
14226 if (! pat)
14227 return 0;
14228 emit_insn (pat);
14230 return target;
14234 /* Expand the dst builtins. */
14235 static rtx
14236 altivec_expand_dst_builtin (tree exp, rtx target ATTRIBUTE_UNUSED,
14237 bool *expandedp)
14239 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
14240 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
14241 tree arg0, arg1, arg2;
14242 machine_mode mode0, mode1;
14243 rtx pat, op0, op1, op2;
14244 const struct builtin_description *d;
14245 size_t i;
14247 *expandedp = false;
14249 /* Handle DST variants. */
14250 d = bdesc_dst;
14251 for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
14252 if (d->code == fcode)
14254 arg0 = CALL_EXPR_ARG (exp, 0);
14255 arg1 = CALL_EXPR_ARG (exp, 1);
14256 arg2 = CALL_EXPR_ARG (exp, 2);
14257 op0 = expand_normal (arg0);
14258 op1 = expand_normal (arg1);
14259 op2 = expand_normal (arg2);
14260 mode0 = insn_data[d->icode].operand[0].mode;
14261 mode1 = insn_data[d->icode].operand[1].mode;
14263 /* Invalid arguments, bail out before generating bad rtl. */
14264 if (arg0 == error_mark_node
14265 || arg1 == error_mark_node
14266 || arg2 == error_mark_node)
14267 return const0_rtx;
14269 *expandedp = true;
14270 STRIP_NOPS (arg2);
14271 if (TREE_CODE (arg2) != INTEGER_CST
14272 || TREE_INT_CST_LOW (arg2) & ~0x3)
14274 error ("argument to %qs must be a 2-bit unsigned literal", d->name);
14275 return const0_rtx;
14278 if (! (*insn_data[d->icode].operand[0].predicate) (op0, mode0))
14279 op0 = copy_to_mode_reg (Pmode, op0);
14280 if (! (*insn_data[d->icode].operand[1].predicate) (op1, mode1))
14281 op1 = copy_to_mode_reg (mode1, op1);
14283 pat = GEN_FCN (d->icode) (op0, op1, op2);
14284 if (pat != 0)
14285 emit_insn (pat);
14287 return NULL_RTX;
14290 return NULL_RTX;
14293 /* Expand vec_init builtin. */
14294 static rtx
14295 altivec_expand_vec_init_builtin (tree type, tree exp, rtx target)
14297 machine_mode tmode = TYPE_MODE (type);
14298 machine_mode inner_mode = GET_MODE_INNER (tmode);
14299 int i, n_elt = GET_MODE_NUNITS (tmode);
14301 gcc_assert (VECTOR_MODE_P (tmode));
14302 gcc_assert (n_elt == call_expr_nargs (exp));
14304 if (!target || !register_operand (target, tmode))
14305 target = gen_reg_rtx (tmode);
14307 /* If we have a vector compromised of a single element, such as V1TImode, do
14308 the initialization directly. */
14309 if (n_elt == 1 && GET_MODE_SIZE (tmode) == GET_MODE_SIZE (inner_mode))
14311 rtx x = expand_normal (CALL_EXPR_ARG (exp, 0));
14312 emit_move_insn (target, gen_lowpart (tmode, x));
14314 else
14316 rtvec v = rtvec_alloc (n_elt);
14318 for (i = 0; i < n_elt; ++i)
14320 rtx x = expand_normal (CALL_EXPR_ARG (exp, i));
14321 RTVEC_ELT (v, i) = gen_lowpart (inner_mode, x);
14324 rs6000_expand_vector_init (target, gen_rtx_PARALLEL (tmode, v));
14327 return target;
14330 /* Return the integer constant in ARG. Constrain it to be in the range
14331 of the subparts of VEC_TYPE; issue an error if not. */
14333 static int
14334 get_element_number (tree vec_type, tree arg)
14336 unsigned HOST_WIDE_INT elt, max = TYPE_VECTOR_SUBPARTS (vec_type) - 1;
14338 if (!tree_fits_uhwi_p (arg)
14339 || (elt = tree_to_uhwi (arg), elt > max))
14341 error ("selector must be an integer constant in the range [0, %wi]", max);
14342 return 0;
14345 return elt;
14348 /* Expand vec_set builtin. */
14349 static rtx
14350 altivec_expand_vec_set_builtin (tree exp)
14352 machine_mode tmode, mode1;
14353 tree arg0, arg1, arg2;
14354 int elt;
14355 rtx op0, op1;
14357 arg0 = CALL_EXPR_ARG (exp, 0);
14358 arg1 = CALL_EXPR_ARG (exp, 1);
14359 arg2 = CALL_EXPR_ARG (exp, 2);
14361 tmode = TYPE_MODE (TREE_TYPE (arg0));
14362 mode1 = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
14363 gcc_assert (VECTOR_MODE_P (tmode));
14365 op0 = expand_expr (arg0, NULL_RTX, tmode, EXPAND_NORMAL);
14366 op1 = expand_expr (arg1, NULL_RTX, mode1, EXPAND_NORMAL);
14367 elt = get_element_number (TREE_TYPE (arg0), arg2);
14369 if (GET_MODE (op1) != mode1 && GET_MODE (op1) != VOIDmode)
14370 op1 = convert_modes (mode1, GET_MODE (op1), op1, true);
14372 op0 = force_reg (tmode, op0);
14373 op1 = force_reg (mode1, op1);
14375 rs6000_expand_vector_set (op0, op1, elt);
14377 return op0;
14380 /* Expand vec_ext builtin. */
14381 static rtx
14382 altivec_expand_vec_ext_builtin (tree exp, rtx target)
14384 machine_mode tmode, mode0;
14385 tree arg0, arg1;
14386 rtx op0;
14387 rtx op1;
14389 arg0 = CALL_EXPR_ARG (exp, 0);
14390 arg1 = CALL_EXPR_ARG (exp, 1);
14392 op0 = expand_normal (arg0);
14393 op1 = expand_normal (arg1);
14395 if (TREE_CODE (arg1) == INTEGER_CST)
14397 unsigned HOST_WIDE_INT elt;
14398 unsigned HOST_WIDE_INT size = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
14399 unsigned int truncated_selector;
14400 /* Even if !tree_fits_uhwi_p (arg1)), TREE_INT_CST_LOW (arg0)
14401 returns low-order bits of INTEGER_CST for modulo indexing. */
14402 elt = TREE_INT_CST_LOW (arg1);
14403 truncated_selector = elt % size;
14404 op1 = GEN_INT (truncated_selector);
14407 tmode = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
14408 mode0 = TYPE_MODE (TREE_TYPE (arg0));
14409 gcc_assert (VECTOR_MODE_P (mode0));
14411 op0 = force_reg (mode0, op0);
14413 if (optimize || !target || !register_operand (target, tmode))
14414 target = gen_reg_rtx (tmode);
14416 rs6000_expand_vector_extract (target, op0, op1);
14418 return target;
14421 /* Expand the builtin in EXP and store the result in TARGET. Store
14422 true in *EXPANDEDP if we found a builtin to expand. */
14423 static rtx
14424 altivec_expand_builtin (tree exp, rtx target, bool *expandedp)
14426 const struct builtin_description *d;
14427 size_t i;
14428 enum insn_code icode;
14429 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
14430 tree arg0, arg1, arg2;
14431 rtx op0, pat;
14432 machine_mode tmode, mode0;
14433 enum rs6000_builtins fcode
14434 = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
14436 if (rs6000_overloaded_builtin_p (fcode))
14438 *expandedp = true;
14439 error ("unresolved overload for Altivec builtin %qF", fndecl);
14441 /* Given it is invalid, just generate a normal call. */
14442 return expand_call (exp, target, false);
14445 target = altivec_expand_dst_builtin (exp, target, expandedp);
14446 if (*expandedp)
14447 return target;
14449 *expandedp = true;
14451 switch (fcode)
14453 case ALTIVEC_BUILTIN_STVX_V2DF:
14454 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v2df, exp);
14455 case ALTIVEC_BUILTIN_STVX_V2DI:
14456 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v2di, exp);
14457 case ALTIVEC_BUILTIN_STVX_V4SF:
14458 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v4sf, exp);
14459 case ALTIVEC_BUILTIN_STVX:
14460 case ALTIVEC_BUILTIN_STVX_V4SI:
14461 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v4si, exp);
14462 case ALTIVEC_BUILTIN_STVX_V8HI:
14463 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v8hi, exp);
14464 case ALTIVEC_BUILTIN_STVX_V16QI:
14465 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v16qi, exp);
14466 case ALTIVEC_BUILTIN_STVEBX:
14467 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvebx, exp);
14468 case ALTIVEC_BUILTIN_STVEHX:
14469 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvehx, exp);
14470 case ALTIVEC_BUILTIN_STVEWX:
14471 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvewx, exp);
14472 case ALTIVEC_BUILTIN_STVXL_V2DF:
14473 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v2df, exp);
14474 case ALTIVEC_BUILTIN_STVXL_V2DI:
14475 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v2di, exp);
14476 case ALTIVEC_BUILTIN_STVXL_V4SF:
14477 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v4sf, exp);
14478 case ALTIVEC_BUILTIN_STVXL:
14479 case ALTIVEC_BUILTIN_STVXL_V4SI:
14480 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v4si, exp);
14481 case ALTIVEC_BUILTIN_STVXL_V8HI:
14482 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v8hi, exp);
14483 case ALTIVEC_BUILTIN_STVXL_V16QI:
14484 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v16qi, exp);
14486 case ALTIVEC_BUILTIN_STVLX:
14487 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvlx, exp);
14488 case ALTIVEC_BUILTIN_STVLXL:
14489 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvlxl, exp);
14490 case ALTIVEC_BUILTIN_STVRX:
14491 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvrx, exp);
14492 case ALTIVEC_BUILTIN_STVRXL:
14493 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvrxl, exp);
14495 case P9V_BUILTIN_STXVL:
14496 return altivec_expand_stxvl_builtin (CODE_FOR_stxvl, exp);
14498 case P9V_BUILTIN_XST_LEN_R:
14499 return altivec_expand_stxvl_builtin (CODE_FOR_xst_len_r, exp);
14501 case VSX_BUILTIN_STXVD2X_V1TI:
14502 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v1ti, exp);
14503 case VSX_BUILTIN_STXVD2X_V2DF:
14504 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v2df, exp);
14505 case VSX_BUILTIN_STXVD2X_V2DI:
14506 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v2di, exp);
14507 case VSX_BUILTIN_STXVW4X_V4SF:
14508 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v4sf, exp);
14509 case VSX_BUILTIN_STXVW4X_V4SI:
14510 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v4si, exp);
14511 case VSX_BUILTIN_STXVW4X_V8HI:
14512 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v8hi, exp);
14513 case VSX_BUILTIN_STXVW4X_V16QI:
14514 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v16qi, exp);
14516 /* For the following on big endian, it's ok to use any appropriate
14517 unaligned-supporting store, so use a generic expander. For
14518 little-endian, the exact element-reversing instruction must
14519 be used. */
14520 case VSX_BUILTIN_ST_ELEMREV_V1TI:
14522 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_store_v1ti
14523 : CODE_FOR_vsx_st_elemrev_v1ti);
14524 return altivec_expand_stv_builtin (code, exp);
14526 case VSX_BUILTIN_ST_ELEMREV_V2DF:
14528 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_store_v2df
14529 : CODE_FOR_vsx_st_elemrev_v2df);
14530 return altivec_expand_stv_builtin (code, exp);
14532 case VSX_BUILTIN_ST_ELEMREV_V2DI:
14534 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_store_v2di
14535 : CODE_FOR_vsx_st_elemrev_v2di);
14536 return altivec_expand_stv_builtin (code, exp);
14538 case VSX_BUILTIN_ST_ELEMREV_V4SF:
14540 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_store_v4sf
14541 : CODE_FOR_vsx_st_elemrev_v4sf);
14542 return altivec_expand_stv_builtin (code, exp);
14544 case VSX_BUILTIN_ST_ELEMREV_V4SI:
14546 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_store_v4si
14547 : CODE_FOR_vsx_st_elemrev_v4si);
14548 return altivec_expand_stv_builtin (code, exp);
14550 case VSX_BUILTIN_ST_ELEMREV_V8HI:
14552 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_store_v8hi
14553 : CODE_FOR_vsx_st_elemrev_v8hi);
14554 return altivec_expand_stv_builtin (code, exp);
14556 case VSX_BUILTIN_ST_ELEMREV_V16QI:
14558 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_store_v16qi
14559 : CODE_FOR_vsx_st_elemrev_v16qi);
14560 return altivec_expand_stv_builtin (code, exp);
14563 case ALTIVEC_BUILTIN_MFVSCR:
14564 icode = CODE_FOR_altivec_mfvscr;
14565 tmode = insn_data[icode].operand[0].mode;
14567 if (target == 0
14568 || GET_MODE (target) != tmode
14569 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
14570 target = gen_reg_rtx (tmode);
14572 pat = GEN_FCN (icode) (target);
14573 if (! pat)
14574 return 0;
14575 emit_insn (pat);
14576 return target;
14578 case ALTIVEC_BUILTIN_MTVSCR:
14579 icode = CODE_FOR_altivec_mtvscr;
14580 arg0 = CALL_EXPR_ARG (exp, 0);
14581 op0 = expand_normal (arg0);
14582 mode0 = insn_data[icode].operand[0].mode;
14584 /* If we got invalid arguments bail out before generating bad rtl. */
14585 if (arg0 == error_mark_node)
14586 return const0_rtx;
14588 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
14589 op0 = copy_to_mode_reg (mode0, op0);
14591 pat = GEN_FCN (icode) (op0);
14592 if (pat)
14593 emit_insn (pat);
14594 return NULL_RTX;
14596 case ALTIVEC_BUILTIN_DSSALL:
14597 emit_insn (gen_altivec_dssall ());
14598 return NULL_RTX;
14600 case ALTIVEC_BUILTIN_DSS:
14601 icode = CODE_FOR_altivec_dss;
14602 arg0 = CALL_EXPR_ARG (exp, 0);
14603 STRIP_NOPS (arg0);
14604 op0 = expand_normal (arg0);
14605 mode0 = insn_data[icode].operand[0].mode;
14607 /* If we got invalid arguments bail out before generating bad rtl. */
14608 if (arg0 == error_mark_node)
14609 return const0_rtx;
14611 if (TREE_CODE (arg0) != INTEGER_CST
14612 || TREE_INT_CST_LOW (arg0) & ~0x3)
14614 error ("argument to %qs must be a 2-bit unsigned literal", "dss");
14615 return const0_rtx;
14618 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
14619 op0 = copy_to_mode_reg (mode0, op0);
14621 emit_insn (gen_altivec_dss (op0));
14622 return NULL_RTX;
14624 case ALTIVEC_BUILTIN_VEC_INIT_V4SI:
14625 case ALTIVEC_BUILTIN_VEC_INIT_V8HI:
14626 case ALTIVEC_BUILTIN_VEC_INIT_V16QI:
14627 case ALTIVEC_BUILTIN_VEC_INIT_V4SF:
14628 case VSX_BUILTIN_VEC_INIT_V2DF:
14629 case VSX_BUILTIN_VEC_INIT_V2DI:
14630 case VSX_BUILTIN_VEC_INIT_V1TI:
14631 return altivec_expand_vec_init_builtin (TREE_TYPE (exp), exp, target);
14633 case ALTIVEC_BUILTIN_VEC_SET_V4SI:
14634 case ALTIVEC_BUILTIN_VEC_SET_V8HI:
14635 case ALTIVEC_BUILTIN_VEC_SET_V16QI:
14636 case ALTIVEC_BUILTIN_VEC_SET_V4SF:
14637 case VSX_BUILTIN_VEC_SET_V2DF:
14638 case VSX_BUILTIN_VEC_SET_V2DI:
14639 case VSX_BUILTIN_VEC_SET_V1TI:
14640 return altivec_expand_vec_set_builtin (exp);
14642 case ALTIVEC_BUILTIN_VEC_EXT_V4SI:
14643 case ALTIVEC_BUILTIN_VEC_EXT_V8HI:
14644 case ALTIVEC_BUILTIN_VEC_EXT_V16QI:
14645 case ALTIVEC_BUILTIN_VEC_EXT_V4SF:
14646 case VSX_BUILTIN_VEC_EXT_V2DF:
14647 case VSX_BUILTIN_VEC_EXT_V2DI:
14648 case VSX_BUILTIN_VEC_EXT_V1TI:
14649 return altivec_expand_vec_ext_builtin (exp, target);
14651 case P9V_BUILTIN_VEC_EXTRACT4B:
14652 arg1 = CALL_EXPR_ARG (exp, 1);
14653 STRIP_NOPS (arg1);
14655 /* Generate a normal call if it is invalid. */
14656 if (arg1 == error_mark_node)
14657 return expand_call (exp, target, false);
14659 if (TREE_CODE (arg1) != INTEGER_CST || TREE_INT_CST_LOW (arg1) > 12)
14661 error ("second argument to %qs must be [0, 12]", "vec_vextract4b");
14662 return expand_call (exp, target, false);
14664 break;
14666 case P9V_BUILTIN_VEC_INSERT4B:
14667 arg2 = CALL_EXPR_ARG (exp, 2);
14668 STRIP_NOPS (arg2);
14670 /* Generate a normal call if it is invalid. */
14671 if (arg2 == error_mark_node)
14672 return expand_call (exp, target, false);
14674 if (TREE_CODE (arg2) != INTEGER_CST || TREE_INT_CST_LOW (arg2) > 12)
14676 error ("third argument to %qs must be [0, 12]", "vec_vinsert4b");
14677 return expand_call (exp, target, false);
14679 break;
14681 default:
14682 break;
14683 /* Fall through. */
14686 /* Expand abs* operations. */
14687 d = bdesc_abs;
14688 for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
14689 if (d->code == fcode)
14690 return altivec_expand_abs_builtin (d->icode, exp, target);
14692 /* Expand the AltiVec predicates. */
14693 d = bdesc_altivec_preds;
14694 for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, d++)
14695 if (d->code == fcode)
14696 return altivec_expand_predicate_builtin (d->icode, exp, target);
14698 /* LV* are funky. We initialized them differently. */
14699 switch (fcode)
14701 case ALTIVEC_BUILTIN_LVSL:
14702 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsl,
14703 exp, target, false);
14704 case ALTIVEC_BUILTIN_LVSR:
14705 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsr,
14706 exp, target, false);
14707 case ALTIVEC_BUILTIN_LVEBX:
14708 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvebx,
14709 exp, target, false);
14710 case ALTIVEC_BUILTIN_LVEHX:
14711 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvehx,
14712 exp, target, false);
14713 case ALTIVEC_BUILTIN_LVEWX:
14714 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvewx,
14715 exp, target, false);
14716 case ALTIVEC_BUILTIN_LVXL_V2DF:
14717 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v2df,
14718 exp, target, false);
14719 case ALTIVEC_BUILTIN_LVXL_V2DI:
14720 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v2di,
14721 exp, target, false);
14722 case ALTIVEC_BUILTIN_LVXL_V4SF:
14723 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v4sf,
14724 exp, target, false);
14725 case ALTIVEC_BUILTIN_LVXL:
14726 case ALTIVEC_BUILTIN_LVXL_V4SI:
14727 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v4si,
14728 exp, target, false);
14729 case ALTIVEC_BUILTIN_LVXL_V8HI:
14730 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v8hi,
14731 exp, target, false);
14732 case ALTIVEC_BUILTIN_LVXL_V16QI:
14733 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v16qi,
14734 exp, target, false);
14735 case ALTIVEC_BUILTIN_LVX_V1TI:
14736 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v1ti,
14737 exp, target, false);
14738 case ALTIVEC_BUILTIN_LVX_V2DF:
14739 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v2df,
14740 exp, target, false);
14741 case ALTIVEC_BUILTIN_LVX_V2DI:
14742 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v2di,
14743 exp, target, false);
14744 case ALTIVEC_BUILTIN_LVX_V4SF:
14745 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v4sf,
14746 exp, target, false);
14747 case ALTIVEC_BUILTIN_LVX:
14748 case ALTIVEC_BUILTIN_LVX_V4SI:
14749 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v4si,
14750 exp, target, false);
14751 case ALTIVEC_BUILTIN_LVX_V8HI:
14752 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v8hi,
14753 exp, target, false);
14754 case ALTIVEC_BUILTIN_LVX_V16QI:
14755 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v16qi,
14756 exp, target, false);
14757 case ALTIVEC_BUILTIN_LVLX:
14758 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvlx,
14759 exp, target, true);
14760 case ALTIVEC_BUILTIN_LVLXL:
14761 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvlxl,
14762 exp, target, true);
14763 case ALTIVEC_BUILTIN_LVRX:
14764 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvrx,
14765 exp, target, true);
14766 case ALTIVEC_BUILTIN_LVRXL:
14767 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvrxl,
14768 exp, target, true);
14769 case VSX_BUILTIN_LXVD2X_V1TI:
14770 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v1ti,
14771 exp, target, false);
14772 case VSX_BUILTIN_LXVD2X_V2DF:
14773 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v2df,
14774 exp, target, false);
14775 case VSX_BUILTIN_LXVD2X_V2DI:
14776 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v2di,
14777 exp, target, false);
14778 case VSX_BUILTIN_LXVW4X_V4SF:
14779 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v4sf,
14780 exp, target, false);
14781 case VSX_BUILTIN_LXVW4X_V4SI:
14782 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v4si,
14783 exp, target, false);
14784 case VSX_BUILTIN_LXVW4X_V8HI:
14785 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v8hi,
14786 exp, target, false);
14787 case VSX_BUILTIN_LXVW4X_V16QI:
14788 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v16qi,
14789 exp, target, false);
14790 /* For the following on big endian, it's ok to use any appropriate
14791 unaligned-supporting load, so use a generic expander. For
14792 little-endian, the exact element-reversing instruction must
14793 be used. */
14794 case VSX_BUILTIN_LD_ELEMREV_V2DF:
14796 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_load_v2df
14797 : CODE_FOR_vsx_ld_elemrev_v2df);
14798 return altivec_expand_lv_builtin (code, exp, target, false);
14800 case VSX_BUILTIN_LD_ELEMREV_V1TI:
14802 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_load_v1ti
14803 : CODE_FOR_vsx_ld_elemrev_v1ti);
14804 return altivec_expand_lv_builtin (code, exp, target, false);
14806 case VSX_BUILTIN_LD_ELEMREV_V2DI:
14808 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_load_v2di
14809 : CODE_FOR_vsx_ld_elemrev_v2di);
14810 return altivec_expand_lv_builtin (code, exp, target, false);
14812 case VSX_BUILTIN_LD_ELEMREV_V4SF:
14814 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_load_v4sf
14815 : CODE_FOR_vsx_ld_elemrev_v4sf);
14816 return altivec_expand_lv_builtin (code, exp, target, false);
14818 case VSX_BUILTIN_LD_ELEMREV_V4SI:
14820 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_load_v4si
14821 : CODE_FOR_vsx_ld_elemrev_v4si);
14822 return altivec_expand_lv_builtin (code, exp, target, false);
14824 case VSX_BUILTIN_LD_ELEMREV_V8HI:
14826 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_load_v8hi
14827 : CODE_FOR_vsx_ld_elemrev_v8hi);
14828 return altivec_expand_lv_builtin (code, exp, target, false);
14830 case VSX_BUILTIN_LD_ELEMREV_V16QI:
14832 enum insn_code code = (BYTES_BIG_ENDIAN ? CODE_FOR_vsx_load_v16qi
14833 : CODE_FOR_vsx_ld_elemrev_v16qi);
14834 return altivec_expand_lv_builtin (code, exp, target, false);
14836 break;
14837 default:
14838 break;
14839 /* Fall through. */
14842 *expandedp = false;
14843 return NULL_RTX;
14846 /* Check whether a builtin function is supported in this target
14847 configuration. */
14848 bool
14849 rs6000_builtin_is_supported_p (enum rs6000_builtins fncode)
14851 HOST_WIDE_INT fnmask = rs6000_builtin_info[fncode].mask;
14852 if ((fnmask & rs6000_builtin_mask) != fnmask)
14853 return false;
14854 else
14855 return true;
14858 /* Raise an error message for a builtin function that is called without the
14859 appropriate target options being set. */
14861 static void
14862 rs6000_invalid_builtin (enum rs6000_builtins fncode)
14864 size_t uns_fncode = (size_t) fncode;
14865 const char *name = rs6000_builtin_info[uns_fncode].name;
14866 HOST_WIDE_INT fnmask = rs6000_builtin_info[uns_fncode].mask;
14868 gcc_assert (name != NULL);
14869 if ((fnmask & RS6000_BTM_CELL) != 0)
14870 error ("builtin function %qs is only valid for the cell processor", name);
14871 else if ((fnmask & RS6000_BTM_VSX) != 0)
14872 error ("builtin function %qs requires the %qs option", name, "-mvsx");
14873 else if ((fnmask & RS6000_BTM_HTM) != 0)
14874 error ("builtin function %qs requires the %qs option", name, "-mhtm");
14875 else if ((fnmask & RS6000_BTM_ALTIVEC) != 0)
14876 error ("builtin function %qs requires the %qs option", name, "-maltivec");
14877 else if ((fnmask & (RS6000_BTM_DFP | RS6000_BTM_P8_VECTOR))
14878 == (RS6000_BTM_DFP | RS6000_BTM_P8_VECTOR))
14879 error ("builtin function %qs requires the %qs and %qs options",
14880 name, "-mhard-dfp", "-mpower8-vector");
14881 else if ((fnmask & RS6000_BTM_DFP) != 0)
14882 error ("builtin function %qs requires the %qs option", name, "-mhard-dfp");
14883 else if ((fnmask & RS6000_BTM_P8_VECTOR) != 0)
14884 error ("builtin function %qs requires the %qs option", name,
14885 "-mpower8-vector");
14886 else if ((fnmask & (RS6000_BTM_P9_VECTOR | RS6000_BTM_64BIT))
14887 == (RS6000_BTM_P9_VECTOR | RS6000_BTM_64BIT))
14888 error ("builtin function %qs requires the %qs and %qs options",
14889 name, "-mcpu=power9", "-m64");
14890 else if ((fnmask & RS6000_BTM_P9_VECTOR) != 0)
14891 error ("builtin function %qs requires the %qs option", name,
14892 "-mcpu=power9");
14893 else if ((fnmask & (RS6000_BTM_P9_MISC | RS6000_BTM_64BIT))
14894 == (RS6000_BTM_P9_MISC | RS6000_BTM_64BIT))
14895 error ("builtin function %qs requires the %qs and %qs options",
14896 name, "-mcpu=power9", "-m64");
14897 else if ((fnmask & RS6000_BTM_P9_MISC) == RS6000_BTM_P9_MISC)
14898 error ("builtin function %qs requires the %qs option", name,
14899 "-mcpu=power9");
14900 else if ((fnmask & RS6000_BTM_LDBL128) == RS6000_BTM_LDBL128)
14902 if (!TARGET_HARD_FLOAT)
14903 error ("builtin function %qs requires the %qs option", name,
14904 "-mhard-float");
14905 else
14906 error ("builtin function %qs requires the %qs option", name,
14907 TARGET_IEEEQUAD ? "-mabi=ibmlongdouble" : "-mlong-double-128");
14909 else if ((fnmask & RS6000_BTM_HARD_FLOAT) != 0)
14910 error ("builtin function %qs requires the %qs option", name,
14911 "-mhard-float");
14912 else if ((fnmask & RS6000_BTM_FLOAT128_HW) != 0)
14913 error ("builtin function %qs requires ISA 3.0 IEEE 128-bit floating point",
14914 name);
14915 else if ((fnmask & RS6000_BTM_FLOAT128) != 0)
14916 error ("builtin function %qs requires the %qs option", name,
14917 "%<-mfloat128%>");
14918 else if ((fnmask & (RS6000_BTM_POPCNTD | RS6000_BTM_POWERPC64))
14919 == (RS6000_BTM_POPCNTD | RS6000_BTM_POWERPC64))
14920 error ("builtin function %qs requires the %qs (or newer), and "
14921 "%qs or %qs options",
14922 name, "-mcpu=power7", "-m64", "-mpowerpc64");
14923 else
14924 error ("builtin function %qs is not supported with the current options",
14925 name);
14928 /* Target hook for early folding of built-ins, shamelessly stolen
14929 from ia64.c. */
14931 static tree
14932 rs6000_fold_builtin (tree fndecl ATTRIBUTE_UNUSED,
14933 int n_args ATTRIBUTE_UNUSED,
14934 tree *args ATTRIBUTE_UNUSED,
14935 bool ignore ATTRIBUTE_UNUSED)
14937 #ifdef SUBTARGET_FOLD_BUILTIN
14938 return SUBTARGET_FOLD_BUILTIN (fndecl, n_args, args, ignore);
14939 #else
14940 return NULL_TREE;
14941 #endif
14944 /* Helper function to sort out which built-ins may be valid without having
14945 a LHS. */
14946 static bool
14947 rs6000_builtin_valid_without_lhs (enum rs6000_builtins fn_code)
14949 switch (fn_code)
14951 case ALTIVEC_BUILTIN_STVX_V16QI:
14952 case ALTIVEC_BUILTIN_STVX_V8HI:
14953 case ALTIVEC_BUILTIN_STVX_V4SI:
14954 case ALTIVEC_BUILTIN_STVX_V4SF:
14955 case ALTIVEC_BUILTIN_STVX_V2DI:
14956 case ALTIVEC_BUILTIN_STVX_V2DF:
14957 case VSX_BUILTIN_STXVW4X_V16QI:
14958 case VSX_BUILTIN_STXVW4X_V8HI:
14959 case VSX_BUILTIN_STXVW4X_V4SF:
14960 case VSX_BUILTIN_STXVW4X_V4SI:
14961 case VSX_BUILTIN_STXVD2X_V2DF:
14962 case VSX_BUILTIN_STXVD2X_V2DI:
14963 return true;
14964 default:
14965 return false;
14969 /* Helper function to handle the gimple folding of a vector compare
14970 operation. This sets up true/false vectors, and uses the
14971 VEC_COND_EXPR operation.
14972 CODE indicates which comparison is to be made. (EQ, GT, ...).
14973 TYPE indicates the type of the result. */
14974 static tree
14975 fold_build_vec_cmp (tree_code code, tree type,
14976 tree arg0, tree arg1)
14978 tree cmp_type = build_same_sized_truth_vector_type (type);
14979 tree zero_vec = build_zero_cst (type);
14980 tree minus_one_vec = build_minus_one_cst (type);
14981 tree cmp = fold_build2 (code, cmp_type, arg0, arg1);
14982 return fold_build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
14985 /* Helper function to handle the in-between steps for the
14986 vector compare built-ins. */
14987 static void
14988 fold_compare_helper (gimple_stmt_iterator *gsi, tree_code code, gimple *stmt)
14990 tree arg0 = gimple_call_arg (stmt, 0);
14991 tree arg1 = gimple_call_arg (stmt, 1);
14992 tree lhs = gimple_call_lhs (stmt);
14993 tree cmp = fold_build_vec_cmp (code, TREE_TYPE (lhs), arg0, arg1);
14994 gimple *g = gimple_build_assign (lhs, cmp);
14995 gimple_set_location (g, gimple_location (stmt));
14996 gsi_replace (gsi, g, true);
14999 /* Helper function to map V2DF and V4SF types to their
15000 integral equivalents (V2DI and V4SI). */
15001 tree map_to_integral_tree_type (tree input_tree_type)
15003 if (INTEGRAL_TYPE_P (TREE_TYPE (input_tree_type)))
15004 return input_tree_type;
15005 else
15007 if (types_compatible_p (TREE_TYPE (input_tree_type),
15008 TREE_TYPE (V2DF_type_node)))
15009 return V2DI_type_node;
15010 else if (types_compatible_p (TREE_TYPE (input_tree_type),
15011 TREE_TYPE (V4SF_type_node)))
15012 return V4SI_type_node;
15013 else
15014 gcc_unreachable ();
15018 /* Helper function to handle the vector merge[hl] built-ins. The
15019 implementation difference between h and l versions for this code are in
15020 the values used when building of the permute vector for high word versus
15021 low word merge. The variance is keyed off the use_high parameter. */
15022 static void
15023 fold_mergehl_helper (gimple_stmt_iterator *gsi, gimple *stmt, int use_high)
15025 tree arg0 = gimple_call_arg (stmt, 0);
15026 tree arg1 = gimple_call_arg (stmt, 1);
15027 tree lhs = gimple_call_lhs (stmt);
15028 tree lhs_type = TREE_TYPE (lhs);
15029 int n_elts = TYPE_VECTOR_SUBPARTS (lhs_type);
15030 int midpoint = n_elts / 2;
15031 int offset = 0;
15033 if (use_high == 1)
15034 offset = midpoint;
15036 /* The permute_type will match the lhs for integral types. For double and
15037 float types, the permute type needs to map to the V2 or V4 type that
15038 matches size. */
15039 tree permute_type;
15040 permute_type = map_to_integral_tree_type (lhs_type);
15041 tree_vector_builder elts (permute_type, VECTOR_CST_NELTS (arg0), 1);
15043 for (int i = 0; i < midpoint; i++)
15045 elts.safe_push (build_int_cst (TREE_TYPE (permute_type),
15046 offset + i));
15047 elts.safe_push (build_int_cst (TREE_TYPE (permute_type),
15048 offset + n_elts + i));
15051 tree permute = elts.build ();
15053 gimple *g = gimple_build_assign (lhs, VEC_PERM_EXPR, arg0, arg1, permute);
15054 gimple_set_location (g, gimple_location (stmt));
15055 gsi_replace (gsi, g, true);
15058 /* Helper function to handle the vector merge[eo] built-ins. */
15059 static void
15060 fold_mergeeo_helper (gimple_stmt_iterator *gsi, gimple *stmt, int use_odd)
15062 tree arg0 = gimple_call_arg (stmt, 0);
15063 tree arg1 = gimple_call_arg (stmt, 1);
15064 tree lhs = gimple_call_lhs (stmt);
15065 tree lhs_type = TREE_TYPE (lhs);
15066 int n_elts = TYPE_VECTOR_SUBPARTS (lhs_type);
15068 /* The permute_type will match the lhs for integral types. For double and
15069 float types, the permute type needs to map to the V2 or V4 type that
15070 matches size. */
15071 tree permute_type;
15072 permute_type = map_to_integral_tree_type (lhs_type);
15074 tree_vector_builder elts (permute_type, VECTOR_CST_NELTS (arg0), 1);
15076 /* Build the permute vector. */
15077 for (int i = 0; i < n_elts / 2; i++)
15079 elts.safe_push (build_int_cst (TREE_TYPE (permute_type),
15080 2*i + use_odd));
15081 elts.safe_push (build_int_cst (TREE_TYPE (permute_type),
15082 2*i + use_odd + n_elts));
15085 tree permute = elts.build ();
15087 gimple *g = gimple_build_assign (lhs, VEC_PERM_EXPR, arg0, arg1, permute);
15088 gimple_set_location (g, gimple_location (stmt));
15089 gsi_replace (gsi, g, true);
15092 /* Fold a machine-dependent built-in in GIMPLE. (For folding into
15093 a constant, use rs6000_fold_builtin.) */
15095 bool
15096 rs6000_gimple_fold_builtin (gimple_stmt_iterator *gsi)
15098 gimple *stmt = gsi_stmt (*gsi);
15099 tree fndecl = gimple_call_fndecl (stmt);
15100 gcc_checking_assert (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD);
15101 enum rs6000_builtins fn_code
15102 = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
15103 tree arg0, arg1, lhs, temp;
15104 enum tree_code bcode;
15105 gimple *g;
15107 size_t uns_fncode = (size_t) fn_code;
15108 enum insn_code icode = rs6000_builtin_info[uns_fncode].icode;
15109 const char *fn_name1 = rs6000_builtin_info[uns_fncode].name;
15110 const char *fn_name2 = (icode != CODE_FOR_nothing)
15111 ? get_insn_name ((int) icode)
15112 : "nothing";
15114 if (TARGET_DEBUG_BUILTIN)
15115 fprintf (stderr, "rs6000_gimple_fold_builtin %d %s %s\n",
15116 fn_code, fn_name1, fn_name2);
15118 if (!rs6000_fold_gimple)
15119 return false;
15121 /* Prevent gimple folding for code that does not have a LHS, unless it is
15122 allowed per the rs6000_builtin_valid_without_lhs helper function. */
15123 if (!gimple_call_lhs (stmt) && !rs6000_builtin_valid_without_lhs (fn_code))
15124 return false;
15126 /* Don't fold invalid builtins, let rs6000_expand_builtin diagnose it. */
15127 HOST_WIDE_INT mask = rs6000_builtin_info[uns_fncode].mask;
15128 bool func_valid_p = (rs6000_builtin_mask & mask) == mask;
15129 if (!func_valid_p)
15130 return false;
15132 switch (fn_code)
15134 /* Flavors of vec_add. We deliberately don't expand
15135 P8V_BUILTIN_VADDUQM as it gets lowered from V1TImode to
15136 TImode, resulting in much poorer code generation. */
15137 case ALTIVEC_BUILTIN_VADDUBM:
15138 case ALTIVEC_BUILTIN_VADDUHM:
15139 case ALTIVEC_BUILTIN_VADDUWM:
15140 case P8V_BUILTIN_VADDUDM:
15141 case ALTIVEC_BUILTIN_VADDFP:
15142 case VSX_BUILTIN_XVADDDP:
15143 bcode = PLUS_EXPR;
15144 do_binary:
15145 arg0 = gimple_call_arg (stmt, 0);
15146 arg1 = gimple_call_arg (stmt, 1);
15147 lhs = gimple_call_lhs (stmt);
15148 if (INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (lhs)))
15149 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (TREE_TYPE (lhs))))
15151 /* Ensure the binary operation is performed in a type
15152 that wraps if it is integral type. */
15153 gimple_seq stmts = NULL;
15154 tree type = unsigned_type_for (TREE_TYPE (lhs));
15155 tree uarg0 = gimple_build (&stmts, VIEW_CONVERT_EXPR,
15156 type, arg0);
15157 tree uarg1 = gimple_build (&stmts, VIEW_CONVERT_EXPR,
15158 type, arg1);
15159 tree res = gimple_build (&stmts, gimple_location (stmt), bcode,
15160 type, uarg0, uarg1);
15161 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
15162 g = gimple_build_assign (lhs, VIEW_CONVERT_EXPR,
15163 build1 (VIEW_CONVERT_EXPR,
15164 TREE_TYPE (lhs), res));
15165 gsi_replace (gsi, g, true);
15166 return true;
15168 g = gimple_build_assign (lhs, bcode, arg0, arg1);
15169 gimple_set_location (g, gimple_location (stmt));
15170 gsi_replace (gsi, g, true);
15171 return true;
15172 /* Flavors of vec_sub. We deliberately don't expand
15173 P8V_BUILTIN_VSUBUQM. */
15174 case ALTIVEC_BUILTIN_VSUBUBM:
15175 case ALTIVEC_BUILTIN_VSUBUHM:
15176 case ALTIVEC_BUILTIN_VSUBUWM:
15177 case P8V_BUILTIN_VSUBUDM:
15178 case ALTIVEC_BUILTIN_VSUBFP:
15179 case VSX_BUILTIN_XVSUBDP:
15180 bcode = MINUS_EXPR;
15181 goto do_binary;
15182 case VSX_BUILTIN_XVMULSP:
15183 case VSX_BUILTIN_XVMULDP:
15184 arg0 = gimple_call_arg (stmt, 0);
15185 arg1 = gimple_call_arg (stmt, 1);
15186 lhs = gimple_call_lhs (stmt);
15187 g = gimple_build_assign (lhs, MULT_EXPR, arg0, arg1);
15188 gimple_set_location (g, gimple_location (stmt));
15189 gsi_replace (gsi, g, true);
15190 return true;
15191 /* Even element flavors of vec_mul (signed). */
15192 case ALTIVEC_BUILTIN_VMULESB:
15193 case ALTIVEC_BUILTIN_VMULESH:
15194 case P8V_BUILTIN_VMULESW:
15195 /* Even element flavors of vec_mul (unsigned). */
15196 case ALTIVEC_BUILTIN_VMULEUB:
15197 case ALTIVEC_BUILTIN_VMULEUH:
15198 case P8V_BUILTIN_VMULEUW:
15199 arg0 = gimple_call_arg (stmt, 0);
15200 arg1 = gimple_call_arg (stmt, 1);
15201 lhs = gimple_call_lhs (stmt);
15202 g = gimple_build_assign (lhs, VEC_WIDEN_MULT_EVEN_EXPR, arg0, arg1);
15203 gimple_set_location (g, gimple_location (stmt));
15204 gsi_replace (gsi, g, true);
15205 return true;
15206 /* Odd element flavors of vec_mul (signed). */
15207 case ALTIVEC_BUILTIN_VMULOSB:
15208 case ALTIVEC_BUILTIN_VMULOSH:
15209 case P8V_BUILTIN_VMULOSW:
15210 /* Odd element flavors of vec_mul (unsigned). */
15211 case ALTIVEC_BUILTIN_VMULOUB:
15212 case ALTIVEC_BUILTIN_VMULOUH:
15213 case P8V_BUILTIN_VMULOUW:
15214 arg0 = gimple_call_arg (stmt, 0);
15215 arg1 = gimple_call_arg (stmt, 1);
15216 lhs = gimple_call_lhs (stmt);
15217 g = gimple_build_assign (lhs, VEC_WIDEN_MULT_ODD_EXPR, arg0, arg1);
15218 gimple_set_location (g, gimple_location (stmt));
15219 gsi_replace (gsi, g, true);
15220 return true;
15221 /* Flavors of vec_div (Integer). */
15222 case VSX_BUILTIN_DIV_V2DI:
15223 case VSX_BUILTIN_UDIV_V2DI:
15224 arg0 = gimple_call_arg (stmt, 0);
15225 arg1 = gimple_call_arg (stmt, 1);
15226 lhs = gimple_call_lhs (stmt);
15227 g = gimple_build_assign (lhs, TRUNC_DIV_EXPR, arg0, arg1);
15228 gimple_set_location (g, gimple_location (stmt));
15229 gsi_replace (gsi, g, true);
15230 return true;
15231 /* Flavors of vec_div (Float). */
15232 case VSX_BUILTIN_XVDIVSP:
15233 case VSX_BUILTIN_XVDIVDP:
15234 arg0 = gimple_call_arg (stmt, 0);
15235 arg1 = gimple_call_arg (stmt, 1);
15236 lhs = gimple_call_lhs (stmt);
15237 g = gimple_build_assign (lhs, RDIV_EXPR, arg0, arg1);
15238 gimple_set_location (g, gimple_location (stmt));
15239 gsi_replace (gsi, g, true);
15240 return true;
15241 /* Flavors of vec_and. */
15242 case ALTIVEC_BUILTIN_VAND:
15243 arg0 = gimple_call_arg (stmt, 0);
15244 arg1 = gimple_call_arg (stmt, 1);
15245 lhs = gimple_call_lhs (stmt);
15246 g = gimple_build_assign (lhs, BIT_AND_EXPR, arg0, arg1);
15247 gimple_set_location (g, gimple_location (stmt));
15248 gsi_replace (gsi, g, true);
15249 return true;
15250 /* Flavors of vec_andc. */
15251 case ALTIVEC_BUILTIN_VANDC:
15252 arg0 = gimple_call_arg (stmt, 0);
15253 arg1 = gimple_call_arg (stmt, 1);
15254 lhs = gimple_call_lhs (stmt);
15255 temp = create_tmp_reg_or_ssa_name (TREE_TYPE (arg1));
15256 g = gimple_build_assign (temp, BIT_NOT_EXPR, arg1);
15257 gimple_set_location (g, gimple_location (stmt));
15258 gsi_insert_before (gsi, g, GSI_SAME_STMT);
15259 g = gimple_build_assign (lhs, BIT_AND_EXPR, arg0, temp);
15260 gimple_set_location (g, gimple_location (stmt));
15261 gsi_replace (gsi, g, true);
15262 return true;
15263 /* Flavors of vec_nand. */
15264 case P8V_BUILTIN_VEC_NAND:
15265 case P8V_BUILTIN_NAND_V16QI:
15266 case P8V_BUILTIN_NAND_V8HI:
15267 case P8V_BUILTIN_NAND_V4SI:
15268 case P8V_BUILTIN_NAND_V4SF:
15269 case P8V_BUILTIN_NAND_V2DF:
15270 case P8V_BUILTIN_NAND_V2DI:
15271 arg0 = gimple_call_arg (stmt, 0);
15272 arg1 = gimple_call_arg (stmt, 1);
15273 lhs = gimple_call_lhs (stmt);
15274 temp = create_tmp_reg_or_ssa_name (TREE_TYPE (arg1));
15275 g = gimple_build_assign (temp, BIT_AND_EXPR, arg0, arg1);
15276 gimple_set_location (g, gimple_location (stmt));
15277 gsi_insert_before (gsi, g, GSI_SAME_STMT);
15278 g = gimple_build_assign (lhs, BIT_NOT_EXPR, temp);
15279 gimple_set_location (g, gimple_location (stmt));
15280 gsi_replace (gsi, g, true);
15281 return true;
15282 /* Flavors of vec_or. */
15283 case ALTIVEC_BUILTIN_VOR:
15284 arg0 = gimple_call_arg (stmt, 0);
15285 arg1 = gimple_call_arg (stmt, 1);
15286 lhs = gimple_call_lhs (stmt);
15287 g = gimple_build_assign (lhs, BIT_IOR_EXPR, arg0, arg1);
15288 gimple_set_location (g, gimple_location (stmt));
15289 gsi_replace (gsi, g, true);
15290 return true;
15291 /* flavors of vec_orc. */
15292 case P8V_BUILTIN_ORC_V16QI:
15293 case P8V_BUILTIN_ORC_V8HI:
15294 case P8V_BUILTIN_ORC_V4SI:
15295 case P8V_BUILTIN_ORC_V4SF:
15296 case P8V_BUILTIN_ORC_V2DF:
15297 case P8V_BUILTIN_ORC_V2DI:
15298 arg0 = gimple_call_arg (stmt, 0);
15299 arg1 = gimple_call_arg (stmt, 1);
15300 lhs = gimple_call_lhs (stmt);
15301 temp = create_tmp_reg_or_ssa_name (TREE_TYPE (arg1));
15302 g = gimple_build_assign (temp, BIT_NOT_EXPR, arg1);
15303 gimple_set_location (g, gimple_location (stmt));
15304 gsi_insert_before (gsi, g, GSI_SAME_STMT);
15305 g = gimple_build_assign (lhs, BIT_IOR_EXPR, arg0, temp);
15306 gimple_set_location (g, gimple_location (stmt));
15307 gsi_replace (gsi, g, true);
15308 return true;
15309 /* Flavors of vec_xor. */
15310 case ALTIVEC_BUILTIN_VXOR:
15311 arg0 = gimple_call_arg (stmt, 0);
15312 arg1 = gimple_call_arg (stmt, 1);
15313 lhs = gimple_call_lhs (stmt);
15314 g = gimple_build_assign (lhs, BIT_XOR_EXPR, arg0, arg1);
15315 gimple_set_location (g, gimple_location (stmt));
15316 gsi_replace (gsi, g, true);
15317 return true;
15318 /* Flavors of vec_nor. */
15319 case ALTIVEC_BUILTIN_VNOR:
15320 arg0 = gimple_call_arg (stmt, 0);
15321 arg1 = gimple_call_arg (stmt, 1);
15322 lhs = gimple_call_lhs (stmt);
15323 temp = create_tmp_reg_or_ssa_name (TREE_TYPE (arg1));
15324 g = gimple_build_assign (temp, BIT_IOR_EXPR, arg0, arg1);
15325 gimple_set_location (g, gimple_location (stmt));
15326 gsi_insert_before (gsi, g, GSI_SAME_STMT);
15327 g = gimple_build_assign (lhs, BIT_NOT_EXPR, temp);
15328 gimple_set_location (g, gimple_location (stmt));
15329 gsi_replace (gsi, g, true);
15330 return true;
15331 /* flavors of vec_abs. */
15332 case ALTIVEC_BUILTIN_ABS_V16QI:
15333 case ALTIVEC_BUILTIN_ABS_V8HI:
15334 case ALTIVEC_BUILTIN_ABS_V4SI:
15335 case ALTIVEC_BUILTIN_ABS_V4SF:
15336 case P8V_BUILTIN_ABS_V2DI:
15337 case VSX_BUILTIN_XVABSDP:
15338 arg0 = gimple_call_arg (stmt, 0);
15339 if (INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (arg0)))
15340 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (TREE_TYPE (arg0))))
15341 return false;
15342 lhs = gimple_call_lhs (stmt);
15343 g = gimple_build_assign (lhs, ABS_EXPR, arg0);
15344 gimple_set_location (g, gimple_location (stmt));
15345 gsi_replace (gsi, g, true);
15346 return true;
15347 /* flavors of vec_min. */
15348 case VSX_BUILTIN_XVMINDP:
15349 case P8V_BUILTIN_VMINSD:
15350 case P8V_BUILTIN_VMINUD:
15351 case ALTIVEC_BUILTIN_VMINSB:
15352 case ALTIVEC_BUILTIN_VMINSH:
15353 case ALTIVEC_BUILTIN_VMINSW:
15354 case ALTIVEC_BUILTIN_VMINUB:
15355 case ALTIVEC_BUILTIN_VMINUH:
15356 case ALTIVEC_BUILTIN_VMINUW:
15357 case ALTIVEC_BUILTIN_VMINFP:
15358 arg0 = gimple_call_arg (stmt, 0);
15359 arg1 = gimple_call_arg (stmt, 1);
15360 lhs = gimple_call_lhs (stmt);
15361 g = gimple_build_assign (lhs, MIN_EXPR, arg0, arg1);
15362 gimple_set_location (g, gimple_location (stmt));
15363 gsi_replace (gsi, g, true);
15364 return true;
15365 /* flavors of vec_max. */
15366 case VSX_BUILTIN_XVMAXDP:
15367 case P8V_BUILTIN_VMAXSD:
15368 case P8V_BUILTIN_VMAXUD:
15369 case ALTIVEC_BUILTIN_VMAXSB:
15370 case ALTIVEC_BUILTIN_VMAXSH:
15371 case ALTIVEC_BUILTIN_VMAXSW:
15372 case ALTIVEC_BUILTIN_VMAXUB:
15373 case ALTIVEC_BUILTIN_VMAXUH:
15374 case ALTIVEC_BUILTIN_VMAXUW:
15375 case ALTIVEC_BUILTIN_VMAXFP:
15376 arg0 = gimple_call_arg (stmt, 0);
15377 arg1 = gimple_call_arg (stmt, 1);
15378 lhs = gimple_call_lhs (stmt);
15379 g = gimple_build_assign (lhs, MAX_EXPR, arg0, arg1);
15380 gimple_set_location (g, gimple_location (stmt));
15381 gsi_replace (gsi, g, true);
15382 return true;
15383 /* Flavors of vec_eqv. */
15384 case P8V_BUILTIN_EQV_V16QI:
15385 case P8V_BUILTIN_EQV_V8HI:
15386 case P8V_BUILTIN_EQV_V4SI:
15387 case P8V_BUILTIN_EQV_V4SF:
15388 case P8V_BUILTIN_EQV_V2DF:
15389 case P8V_BUILTIN_EQV_V2DI:
15390 arg0 = gimple_call_arg (stmt, 0);
15391 arg1 = gimple_call_arg (stmt, 1);
15392 lhs = gimple_call_lhs (stmt);
15393 temp = create_tmp_reg_or_ssa_name (TREE_TYPE (arg1));
15394 g = gimple_build_assign (temp, BIT_XOR_EXPR, arg0, arg1);
15395 gimple_set_location (g, gimple_location (stmt));
15396 gsi_insert_before (gsi, g, GSI_SAME_STMT);
15397 g = gimple_build_assign (lhs, BIT_NOT_EXPR, temp);
15398 gimple_set_location (g, gimple_location (stmt));
15399 gsi_replace (gsi, g, true);
15400 return true;
15401 /* Flavors of vec_rotate_left. */
15402 case ALTIVEC_BUILTIN_VRLB:
15403 case ALTIVEC_BUILTIN_VRLH:
15404 case ALTIVEC_BUILTIN_VRLW:
15405 case P8V_BUILTIN_VRLD:
15406 arg0 = gimple_call_arg (stmt, 0);
15407 arg1 = gimple_call_arg (stmt, 1);
15408 lhs = gimple_call_lhs (stmt);
15409 g = gimple_build_assign (lhs, LROTATE_EXPR, arg0, arg1);
15410 gimple_set_location (g, gimple_location (stmt));
15411 gsi_replace (gsi, g, true);
15412 return true;
15413 /* Flavors of vector shift right algebraic.
15414 vec_sra{b,h,w} -> vsra{b,h,w}. */
15415 case ALTIVEC_BUILTIN_VSRAB:
15416 case ALTIVEC_BUILTIN_VSRAH:
15417 case ALTIVEC_BUILTIN_VSRAW:
15418 case P8V_BUILTIN_VSRAD:
15420 arg0 = gimple_call_arg (stmt, 0);
15421 arg1 = gimple_call_arg (stmt, 1);
15422 lhs = gimple_call_lhs (stmt);
15423 tree arg1_type = TREE_TYPE (arg1);
15424 tree unsigned_arg1_type = unsigned_type_for (TREE_TYPE (arg1));
15425 tree unsigned_element_type = unsigned_type_for (TREE_TYPE (arg1_type));
15426 location_t loc = gimple_location (stmt);
15427 /* Force arg1 into the range valid matching the arg0 type. */
15428 /* Build a vector consisting of the max valid bit-size values. */
15429 int n_elts = VECTOR_CST_NELTS (arg1);
15430 tree element_size = build_int_cst (unsigned_element_type,
15431 128 / n_elts);
15432 tree_vector_builder elts (unsigned_arg1_type, n_elts, 1);
15433 for (int i = 0; i < n_elts; i++)
15434 elts.safe_push (element_size);
15435 tree modulo_tree = elts.build ();
15436 /* Modulo the provided shift value against that vector. */
15437 gimple_seq stmts = NULL;
15438 tree unsigned_arg1 = gimple_build (&stmts, VIEW_CONVERT_EXPR,
15439 unsigned_arg1_type, arg1);
15440 tree new_arg1 = gimple_build (&stmts, loc, TRUNC_MOD_EXPR,
15441 unsigned_arg1_type, unsigned_arg1,
15442 modulo_tree);
15443 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
15444 /* And finally, do the shift. */
15445 g = gimple_build_assign (lhs, RSHIFT_EXPR, arg0, new_arg1);
15446 gimple_set_location (g, loc);
15447 gsi_replace (gsi, g, true);
15448 return true;
15450 /* Flavors of vector shift left.
15451 builtin_altivec_vsl{b,h,w} -> vsl{b,h,w}. */
15452 case ALTIVEC_BUILTIN_VSLB:
15453 case ALTIVEC_BUILTIN_VSLH:
15454 case ALTIVEC_BUILTIN_VSLW:
15455 case P8V_BUILTIN_VSLD:
15457 location_t loc;
15458 gimple_seq stmts = NULL;
15459 arg0 = gimple_call_arg (stmt, 0);
15460 tree arg0_type = TREE_TYPE (arg0);
15461 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0_type))
15462 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0_type)))
15463 return false;
15464 arg1 = gimple_call_arg (stmt, 1);
15465 tree arg1_type = TREE_TYPE (arg1);
15466 tree unsigned_arg1_type = unsigned_type_for (TREE_TYPE (arg1));
15467 tree unsigned_element_type = unsigned_type_for (TREE_TYPE (arg1_type));
15468 loc = gimple_location (stmt);
15469 lhs = gimple_call_lhs (stmt);
15470 /* Force arg1 into the range valid matching the arg0 type. */
15471 /* Build a vector consisting of the max valid bit-size values. */
15472 int n_elts = VECTOR_CST_NELTS (arg1);
15473 int tree_size_in_bits = TREE_INT_CST_LOW (size_in_bytes (arg1_type))
15474 * BITS_PER_UNIT;
15475 tree element_size = build_int_cst (unsigned_element_type,
15476 tree_size_in_bits / n_elts);
15477 tree_vector_builder elts (unsigned_type_for (arg1_type), n_elts, 1);
15478 for (int i = 0; i < n_elts; i++)
15479 elts.safe_push (element_size);
15480 tree modulo_tree = elts.build ();
15481 /* Modulo the provided shift value against that vector. */
15482 tree unsigned_arg1 = gimple_build (&stmts, VIEW_CONVERT_EXPR,
15483 unsigned_arg1_type, arg1);
15484 tree new_arg1 = gimple_build (&stmts, loc, TRUNC_MOD_EXPR,
15485 unsigned_arg1_type, unsigned_arg1,
15486 modulo_tree);
15487 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
15488 /* And finally, do the shift. */
15489 g = gimple_build_assign (lhs, LSHIFT_EXPR, arg0, new_arg1);
15490 gimple_set_location (g, gimple_location (stmt));
15491 gsi_replace (gsi, g, true);
15492 return true;
15494 /* Flavors of vector shift right. */
15495 case ALTIVEC_BUILTIN_VSRB:
15496 case ALTIVEC_BUILTIN_VSRH:
15497 case ALTIVEC_BUILTIN_VSRW:
15498 case P8V_BUILTIN_VSRD:
15500 arg0 = gimple_call_arg (stmt, 0);
15501 arg1 = gimple_call_arg (stmt, 1);
15502 lhs = gimple_call_lhs (stmt);
15503 tree arg1_type = TREE_TYPE (arg1);
15504 tree unsigned_arg1_type = unsigned_type_for (TREE_TYPE (arg1));
15505 tree unsigned_element_type = unsigned_type_for (TREE_TYPE (arg1_type));
15506 location_t loc = gimple_location (stmt);
15507 gimple_seq stmts = NULL;
15508 /* Convert arg0 to unsigned. */
15509 tree arg0_unsigned
15510 = gimple_build (&stmts, VIEW_CONVERT_EXPR,
15511 unsigned_type_for (TREE_TYPE (arg0)), arg0);
15512 /* Force arg1 into the range valid matching the arg0 type. */
15513 /* Build a vector consisting of the max valid bit-size values. */
15514 int n_elts = VECTOR_CST_NELTS (arg1);
15515 tree element_size = build_int_cst (unsigned_element_type,
15516 128 / n_elts);
15517 tree_vector_builder elts (unsigned_arg1_type, n_elts, 1);
15518 for (int i = 0; i < n_elts; i++)
15519 elts.safe_push (element_size);
15520 tree modulo_tree = elts.build ();
15521 /* Modulo the provided shift value against that vector. */
15522 tree unsigned_arg1 = gimple_build (&stmts, VIEW_CONVERT_EXPR,
15523 unsigned_arg1_type, arg1);
15524 tree new_arg1 = gimple_build (&stmts, loc, TRUNC_MOD_EXPR,
15525 unsigned_arg1_type, unsigned_arg1,
15526 modulo_tree);
15527 /* Do the shift. */
15528 tree res
15529 = gimple_build (&stmts, RSHIFT_EXPR,
15530 TREE_TYPE (arg0_unsigned), arg0_unsigned, new_arg1);
15531 /* Convert result back to the lhs type. */
15532 res = gimple_build (&stmts, VIEW_CONVERT_EXPR, TREE_TYPE (lhs), res);
15533 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
15534 update_call_from_tree (gsi, res);
15535 return true;
15537 /* Vector loads. */
15538 case ALTIVEC_BUILTIN_LVX_V16QI:
15539 case ALTIVEC_BUILTIN_LVX_V8HI:
15540 case ALTIVEC_BUILTIN_LVX_V4SI:
15541 case ALTIVEC_BUILTIN_LVX_V4SF:
15542 case ALTIVEC_BUILTIN_LVX_V2DI:
15543 case ALTIVEC_BUILTIN_LVX_V2DF:
15544 case ALTIVEC_BUILTIN_LVX_V1TI:
15546 arg0 = gimple_call_arg (stmt, 0); // offset
15547 arg1 = gimple_call_arg (stmt, 1); // address
15548 lhs = gimple_call_lhs (stmt);
15549 location_t loc = gimple_location (stmt);
15550 /* Since arg1 may be cast to a different type, just use ptr_type_node
15551 here instead of trying to enforce TBAA on pointer types. */
15552 tree arg1_type = ptr_type_node;
15553 tree lhs_type = TREE_TYPE (lhs);
15554 /* POINTER_PLUS_EXPR wants the offset to be of type 'sizetype'. Create
15555 the tree using the value from arg0. The resulting type will match
15556 the type of arg1. */
15557 gimple_seq stmts = NULL;
15558 tree temp_offset = gimple_convert (&stmts, loc, sizetype, arg0);
15559 tree temp_addr = gimple_build (&stmts, loc, POINTER_PLUS_EXPR,
15560 arg1_type, arg1, temp_offset);
15561 /* Mask off any lower bits from the address. */
15562 tree aligned_addr = gimple_build (&stmts, loc, BIT_AND_EXPR,
15563 arg1_type, temp_addr,
15564 build_int_cst (arg1_type, -16));
15565 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
15566 if (!is_gimple_mem_ref_addr (aligned_addr))
15568 tree t = make_ssa_name (TREE_TYPE (aligned_addr));
15569 gimple *g = gimple_build_assign (t, aligned_addr);
15570 gsi_insert_before (gsi, g, GSI_SAME_STMT);
15571 aligned_addr = t;
15573 /* Use the build2 helper to set up the mem_ref. The MEM_REF could also
15574 take an offset, but since we've already incorporated the offset
15575 above, here we just pass in a zero. */
15576 gimple *g
15577 = gimple_build_assign (lhs, build2 (MEM_REF, lhs_type, aligned_addr,
15578 build_int_cst (arg1_type, 0)));
15579 gimple_set_location (g, loc);
15580 gsi_replace (gsi, g, true);
15581 return true;
15583 /* Vector stores. */
15584 case ALTIVEC_BUILTIN_STVX_V16QI:
15585 case ALTIVEC_BUILTIN_STVX_V8HI:
15586 case ALTIVEC_BUILTIN_STVX_V4SI:
15587 case ALTIVEC_BUILTIN_STVX_V4SF:
15588 case ALTIVEC_BUILTIN_STVX_V2DI:
15589 case ALTIVEC_BUILTIN_STVX_V2DF:
15591 arg0 = gimple_call_arg (stmt, 0); /* Value to be stored. */
15592 arg1 = gimple_call_arg (stmt, 1); /* Offset. */
15593 tree arg2 = gimple_call_arg (stmt, 2); /* Store-to address. */
15594 location_t loc = gimple_location (stmt);
15595 tree arg0_type = TREE_TYPE (arg0);
15596 /* Use ptr_type_node (no TBAA) for the arg2_type.
15597 FIXME: (Richard) "A proper fix would be to transition this type as
15598 seen from the frontend to GIMPLE, for example in a similar way we
15599 do for MEM_REFs by piggy-backing that on an extra argument, a
15600 constant zero pointer of the alias pointer type to use (which would
15601 also serve as a type indicator of the store itself). I'd use a
15602 target specific internal function for this (not sure if we can have
15603 those target specific, but I guess if it's folded away then that's
15604 fine) and get away with the overload set." */
15605 tree arg2_type = ptr_type_node;
15606 /* POINTER_PLUS_EXPR wants the offset to be of type 'sizetype'. Create
15607 the tree using the value from arg0. The resulting type will match
15608 the type of arg2. */
15609 gimple_seq stmts = NULL;
15610 tree temp_offset = gimple_convert (&stmts, loc, sizetype, arg1);
15611 tree temp_addr = gimple_build (&stmts, loc, POINTER_PLUS_EXPR,
15612 arg2_type, arg2, temp_offset);
15613 /* Mask off any lower bits from the address. */
15614 tree aligned_addr = gimple_build (&stmts, loc, BIT_AND_EXPR,
15615 arg2_type, temp_addr,
15616 build_int_cst (arg2_type, -16));
15617 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
15618 if (!is_gimple_mem_ref_addr (aligned_addr))
15620 tree t = make_ssa_name (TREE_TYPE (aligned_addr));
15621 gimple *g = gimple_build_assign (t, aligned_addr);
15622 gsi_insert_before (gsi, g, GSI_SAME_STMT);
15623 aligned_addr = t;
15625 /* The desired gimple result should be similar to:
15626 MEM[(__vector floatD.1407 *)_1] = vf1D.2697; */
15627 gimple *g
15628 = gimple_build_assign (build2 (MEM_REF, arg0_type, aligned_addr,
15629 build_int_cst (arg2_type, 0)), arg0);
15630 gimple_set_location (g, loc);
15631 gsi_replace (gsi, g, true);
15632 return true;
15635 /* unaligned Vector loads. */
15636 case VSX_BUILTIN_LXVW4X_V16QI:
15637 case VSX_BUILTIN_LXVW4X_V8HI:
15638 case VSX_BUILTIN_LXVW4X_V4SF:
15639 case VSX_BUILTIN_LXVW4X_V4SI:
15640 case VSX_BUILTIN_LXVD2X_V2DF:
15641 case VSX_BUILTIN_LXVD2X_V2DI:
15643 arg0 = gimple_call_arg (stmt, 0); // offset
15644 arg1 = gimple_call_arg (stmt, 1); // address
15645 lhs = gimple_call_lhs (stmt);
15646 location_t loc = gimple_location (stmt);
15647 /* Since arg1 may be cast to a different type, just use ptr_type_node
15648 here instead of trying to enforce TBAA on pointer types. */
15649 tree arg1_type = ptr_type_node;
15650 tree lhs_type = TREE_TYPE (lhs);
15651 /* In GIMPLE the type of the MEM_REF specifies the alignment. The
15652 required alignment (power) is 4 bytes regardless of data type. */
15653 tree align_ltype = build_aligned_type (lhs_type, 4);
15654 /* POINTER_PLUS_EXPR wants the offset to be of type 'sizetype'. Create
15655 the tree using the value from arg0. The resulting type will match
15656 the type of arg1. */
15657 gimple_seq stmts = NULL;
15658 tree temp_offset = gimple_convert (&stmts, loc, sizetype, arg0);
15659 tree temp_addr = gimple_build (&stmts, loc, POINTER_PLUS_EXPR,
15660 arg1_type, arg1, temp_offset);
15661 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
15662 if (!is_gimple_mem_ref_addr (temp_addr))
15664 tree t = make_ssa_name (TREE_TYPE (temp_addr));
15665 gimple *g = gimple_build_assign (t, temp_addr);
15666 gsi_insert_before (gsi, g, GSI_SAME_STMT);
15667 temp_addr = t;
15669 /* Use the build2 helper to set up the mem_ref. The MEM_REF could also
15670 take an offset, but since we've already incorporated the offset
15671 above, here we just pass in a zero. */
15672 gimple *g;
15673 g = gimple_build_assign (lhs, build2 (MEM_REF, align_ltype, temp_addr,
15674 build_int_cst (arg1_type, 0)));
15675 gimple_set_location (g, loc);
15676 gsi_replace (gsi, g, true);
15677 return true;
15680 /* unaligned Vector stores. */
15681 case VSX_BUILTIN_STXVW4X_V16QI:
15682 case VSX_BUILTIN_STXVW4X_V8HI:
15683 case VSX_BUILTIN_STXVW4X_V4SF:
15684 case VSX_BUILTIN_STXVW4X_V4SI:
15685 case VSX_BUILTIN_STXVD2X_V2DF:
15686 case VSX_BUILTIN_STXVD2X_V2DI:
15688 arg0 = gimple_call_arg (stmt, 0); /* Value to be stored. */
15689 arg1 = gimple_call_arg (stmt, 1); /* Offset. */
15690 tree arg2 = gimple_call_arg (stmt, 2); /* Store-to address. */
15691 location_t loc = gimple_location (stmt);
15692 tree arg0_type = TREE_TYPE (arg0);
15693 /* Use ptr_type_node (no TBAA) for the arg2_type. */
15694 tree arg2_type = ptr_type_node;
15695 /* In GIMPLE the type of the MEM_REF specifies the alignment. The
15696 required alignment (power) is 4 bytes regardless of data type. */
15697 tree align_stype = build_aligned_type (arg0_type, 4);
15698 /* POINTER_PLUS_EXPR wants the offset to be of type 'sizetype'. Create
15699 the tree using the value from arg1. */
15700 gimple_seq stmts = NULL;
15701 tree temp_offset = gimple_convert (&stmts, loc, sizetype, arg1);
15702 tree temp_addr = gimple_build (&stmts, loc, POINTER_PLUS_EXPR,
15703 arg2_type, arg2, temp_offset);
15704 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
15705 if (!is_gimple_mem_ref_addr (temp_addr))
15707 tree t = make_ssa_name (TREE_TYPE (temp_addr));
15708 gimple *g = gimple_build_assign (t, temp_addr);
15709 gsi_insert_before (gsi, g, GSI_SAME_STMT);
15710 temp_addr = t;
15712 gimple *g;
15713 g = gimple_build_assign (build2 (MEM_REF, align_stype, temp_addr,
15714 build_int_cst (arg2_type, 0)), arg0);
15715 gimple_set_location (g, loc);
15716 gsi_replace (gsi, g, true);
15717 return true;
15720 /* Vector Fused multiply-add (fma). */
15721 case ALTIVEC_BUILTIN_VMADDFP:
15722 case VSX_BUILTIN_XVMADDDP:
15723 case ALTIVEC_BUILTIN_VMLADDUHM:
15725 arg0 = gimple_call_arg (stmt, 0);
15726 arg1 = gimple_call_arg (stmt, 1);
15727 tree arg2 = gimple_call_arg (stmt, 2);
15728 lhs = gimple_call_lhs (stmt);
15729 gcall *g = gimple_build_call_internal (IFN_FMA, 3, arg0, arg1, arg2);
15730 gimple_call_set_lhs (g, lhs);
15731 gimple_call_set_nothrow (g, true);
15732 gimple_set_location (g, gimple_location (stmt));
15733 gsi_replace (gsi, g, true);
15734 return true;
15737 /* Vector compares; EQ, NE, GE, GT, LE. */
15738 case ALTIVEC_BUILTIN_VCMPEQUB:
15739 case ALTIVEC_BUILTIN_VCMPEQUH:
15740 case ALTIVEC_BUILTIN_VCMPEQUW:
15741 case P8V_BUILTIN_VCMPEQUD:
15742 fold_compare_helper (gsi, EQ_EXPR, stmt);
15743 return true;
15745 case P9V_BUILTIN_CMPNEB:
15746 case P9V_BUILTIN_CMPNEH:
15747 case P9V_BUILTIN_CMPNEW:
15748 fold_compare_helper (gsi, NE_EXPR, stmt);
15749 return true;
15751 case VSX_BUILTIN_CMPGE_16QI:
15752 case VSX_BUILTIN_CMPGE_U16QI:
15753 case VSX_BUILTIN_CMPGE_8HI:
15754 case VSX_BUILTIN_CMPGE_U8HI:
15755 case VSX_BUILTIN_CMPGE_4SI:
15756 case VSX_BUILTIN_CMPGE_U4SI:
15757 case VSX_BUILTIN_CMPGE_2DI:
15758 case VSX_BUILTIN_CMPGE_U2DI:
15759 fold_compare_helper (gsi, GE_EXPR, stmt);
15760 return true;
15762 case ALTIVEC_BUILTIN_VCMPGTSB:
15763 case ALTIVEC_BUILTIN_VCMPGTUB:
15764 case ALTIVEC_BUILTIN_VCMPGTSH:
15765 case ALTIVEC_BUILTIN_VCMPGTUH:
15766 case ALTIVEC_BUILTIN_VCMPGTSW:
15767 case ALTIVEC_BUILTIN_VCMPGTUW:
15768 case P8V_BUILTIN_VCMPGTUD:
15769 case P8V_BUILTIN_VCMPGTSD:
15770 fold_compare_helper (gsi, GT_EXPR, stmt);
15771 return true;
15773 case VSX_BUILTIN_CMPLE_16QI:
15774 case VSX_BUILTIN_CMPLE_U16QI:
15775 case VSX_BUILTIN_CMPLE_8HI:
15776 case VSX_BUILTIN_CMPLE_U8HI:
15777 case VSX_BUILTIN_CMPLE_4SI:
15778 case VSX_BUILTIN_CMPLE_U4SI:
15779 case VSX_BUILTIN_CMPLE_2DI:
15780 case VSX_BUILTIN_CMPLE_U2DI:
15781 fold_compare_helper (gsi, LE_EXPR, stmt);
15782 return true;
15784 /* flavors of vec_splat_[us]{8,16,32}. */
15785 case ALTIVEC_BUILTIN_VSPLTISB:
15786 case ALTIVEC_BUILTIN_VSPLTISH:
15787 case ALTIVEC_BUILTIN_VSPLTISW:
15789 arg0 = gimple_call_arg (stmt, 0);
15790 lhs = gimple_call_lhs (stmt);
15792 /* Only fold the vec_splat_*() if the lower bits of arg 0 is a
15793 5-bit signed constant in range -16 to +15. */
15794 if (TREE_CODE (arg0) != INTEGER_CST
15795 || !IN_RANGE (TREE_INT_CST_LOW (arg0), -16, 15))
15796 return false;
15797 gimple_seq stmts = NULL;
15798 location_t loc = gimple_location (stmt);
15799 tree splat_value = gimple_convert (&stmts, loc,
15800 TREE_TYPE (TREE_TYPE (lhs)), arg0);
15801 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
15802 tree splat_tree = build_vector_from_val (TREE_TYPE (lhs), splat_value);
15803 g = gimple_build_assign (lhs, splat_tree);
15804 gimple_set_location (g, gimple_location (stmt));
15805 gsi_replace (gsi, g, true);
15806 return true;
15809 /* Flavors of vec_splat. */
15810 /* a = vec_splat (b, 0x3) becomes a = { b[3],b[3],b[3],...}; */
15811 case ALTIVEC_BUILTIN_VSPLTB:
15812 case ALTIVEC_BUILTIN_VSPLTH:
15813 case ALTIVEC_BUILTIN_VSPLTW:
15814 case VSX_BUILTIN_XXSPLTD_V2DI:
15815 case VSX_BUILTIN_XXSPLTD_V2DF:
15817 arg0 = gimple_call_arg (stmt, 0); /* input vector. */
15818 arg1 = gimple_call_arg (stmt, 1); /* index into arg0. */
15819 /* Only fold the vec_splat_*() if arg1 is both a constant value and
15820 is a valid index into the arg0 vector. */
15821 unsigned int n_elts = VECTOR_CST_NELTS (arg0);
15822 if (TREE_CODE (arg1) != INTEGER_CST
15823 || TREE_INT_CST_LOW (arg1) > (n_elts -1))
15824 return false;
15825 lhs = gimple_call_lhs (stmt);
15826 tree lhs_type = TREE_TYPE (lhs);
15827 tree arg0_type = TREE_TYPE (arg0);
15828 tree splat;
15829 if (TREE_CODE (arg0) == VECTOR_CST)
15830 splat = VECTOR_CST_ELT (arg0, TREE_INT_CST_LOW (arg1));
15831 else
15833 /* Determine (in bits) the length and start location of the
15834 splat value for a call to the tree_vec_extract helper. */
15835 int splat_elem_size = TREE_INT_CST_LOW (size_in_bytes (arg0_type))
15836 * BITS_PER_UNIT / n_elts;
15837 int splat_start_bit = TREE_INT_CST_LOW (arg1) * splat_elem_size;
15838 tree len = build_int_cst (bitsizetype, splat_elem_size);
15839 tree start = build_int_cst (bitsizetype, splat_start_bit);
15840 splat = tree_vec_extract (gsi, TREE_TYPE (lhs_type), arg0,
15841 len, start);
15843 /* And finally, build the new vector. */
15844 tree splat_tree = build_vector_from_val (lhs_type, splat);
15845 g = gimple_build_assign (lhs, splat_tree);
15846 gimple_set_location (g, gimple_location (stmt));
15847 gsi_replace (gsi, g, true);
15848 return true;
15851 /* vec_mergel (integrals). */
15852 case ALTIVEC_BUILTIN_VMRGLH:
15853 case ALTIVEC_BUILTIN_VMRGLW:
15854 case VSX_BUILTIN_XXMRGLW_4SI:
15855 case ALTIVEC_BUILTIN_VMRGLB:
15856 case VSX_BUILTIN_VEC_MERGEL_V2DI:
15857 case VSX_BUILTIN_XXMRGLW_4SF:
15858 case VSX_BUILTIN_VEC_MERGEL_V2DF:
15859 fold_mergehl_helper (gsi, stmt, 1);
15860 return true;
15861 /* vec_mergeh (integrals). */
15862 case ALTIVEC_BUILTIN_VMRGHH:
15863 case ALTIVEC_BUILTIN_VMRGHW:
15864 case VSX_BUILTIN_XXMRGHW_4SI:
15865 case ALTIVEC_BUILTIN_VMRGHB:
15866 case VSX_BUILTIN_VEC_MERGEH_V2DI:
15867 case VSX_BUILTIN_XXMRGHW_4SF:
15868 case VSX_BUILTIN_VEC_MERGEH_V2DF:
15869 fold_mergehl_helper (gsi, stmt, 0);
15870 return true;
15872 /* Flavors of vec_mergee. */
15873 case P8V_BUILTIN_VMRGEW_V4SI:
15874 case P8V_BUILTIN_VMRGEW_V2DI:
15875 case P8V_BUILTIN_VMRGEW_V4SF:
15876 case P8V_BUILTIN_VMRGEW_V2DF:
15877 fold_mergeeo_helper (gsi, stmt, 0);
15878 return true;
15879 /* Flavors of vec_mergeo. */
15880 case P8V_BUILTIN_VMRGOW_V4SI:
15881 case P8V_BUILTIN_VMRGOW_V2DI:
15882 case P8V_BUILTIN_VMRGOW_V4SF:
15883 case P8V_BUILTIN_VMRGOW_V2DF:
15884 fold_mergeeo_helper (gsi, stmt, 1);
15885 return true;
15887 /* d = vec_pack (a, b) */
15888 case P8V_BUILTIN_VPKUDUM:
15889 case ALTIVEC_BUILTIN_VPKUHUM:
15890 case ALTIVEC_BUILTIN_VPKUWUM:
15892 arg0 = gimple_call_arg (stmt, 0);
15893 arg1 = gimple_call_arg (stmt, 1);
15894 lhs = gimple_call_lhs (stmt);
15895 gimple *g = gimple_build_assign (lhs, VEC_PACK_TRUNC_EXPR, arg0, arg1);
15896 gimple_set_location (g, gimple_location (stmt));
15897 gsi_replace (gsi, g, true);
15898 return true;
15901 /* d = vec_unpackh (a) */
15902 /* Note that the UNPACK_{HI,LO}_EXPR used in the gimple_build_assign call
15903 in this code is sensitive to endian-ness, and needs to be inverted to
15904 handle both LE and BE targets. */
15905 case ALTIVEC_BUILTIN_VUPKHSB:
15906 case ALTIVEC_BUILTIN_VUPKHSH:
15907 case P8V_BUILTIN_VUPKHSW:
15909 arg0 = gimple_call_arg (stmt, 0);
15910 lhs = gimple_call_lhs (stmt);
15911 if (BYTES_BIG_ENDIAN)
15912 g = gimple_build_assign (lhs, VEC_UNPACK_HI_EXPR, arg0);
15913 else
15914 g = gimple_build_assign (lhs, VEC_UNPACK_LO_EXPR, arg0);
15915 gimple_set_location (g, gimple_location (stmt));
15916 gsi_replace (gsi, g, true);
15917 return true;
15919 /* d = vec_unpackl (a) */
15920 case ALTIVEC_BUILTIN_VUPKLSB:
15921 case ALTIVEC_BUILTIN_VUPKLSH:
15922 case P8V_BUILTIN_VUPKLSW:
15924 arg0 = gimple_call_arg (stmt, 0);
15925 lhs = gimple_call_lhs (stmt);
15926 if (BYTES_BIG_ENDIAN)
15927 g = gimple_build_assign (lhs, VEC_UNPACK_LO_EXPR, arg0);
15928 else
15929 g = gimple_build_assign (lhs, VEC_UNPACK_HI_EXPR, arg0);
15930 gimple_set_location (g, gimple_location (stmt));
15931 gsi_replace (gsi, g, true);
15932 return true;
15934 /* There is no gimple type corresponding with pixel, so just return. */
15935 case ALTIVEC_BUILTIN_VUPKHPX:
15936 case ALTIVEC_BUILTIN_VUPKLPX:
15937 return false;
15939 /* vec_perm. */
15940 case ALTIVEC_BUILTIN_VPERM_16QI:
15941 case ALTIVEC_BUILTIN_VPERM_8HI:
15942 case ALTIVEC_BUILTIN_VPERM_4SI:
15943 case ALTIVEC_BUILTIN_VPERM_2DI:
15944 case ALTIVEC_BUILTIN_VPERM_4SF:
15945 case ALTIVEC_BUILTIN_VPERM_2DF:
15947 arg0 = gimple_call_arg (stmt, 0);
15948 arg1 = gimple_call_arg (stmt, 1);
15949 tree permute = gimple_call_arg (stmt, 2);
15950 lhs = gimple_call_lhs (stmt);
15951 location_t loc = gimple_location (stmt);
15952 gimple_seq stmts = NULL;
15953 // convert arg0 and arg1 to match the type of the permute
15954 // for the VEC_PERM_EXPR operation.
15955 tree permute_type = (TREE_TYPE (permute));
15956 tree arg0_ptype = gimple_convert (&stmts, loc, permute_type, arg0);
15957 tree arg1_ptype = gimple_convert (&stmts, loc, permute_type, arg1);
15958 tree lhs_ptype = gimple_build (&stmts, loc, VEC_PERM_EXPR,
15959 permute_type, arg0_ptype, arg1_ptype,
15960 permute);
15961 // Convert the result back to the desired lhs type upon completion.
15962 tree temp = gimple_convert (&stmts, loc, TREE_TYPE (lhs), lhs_ptype);
15963 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
15964 g = gimple_build_assign (lhs, temp);
15965 gimple_set_location (g, loc);
15966 gsi_replace (gsi, g, true);
15967 return true;
15970 default:
15971 if (TARGET_DEBUG_BUILTIN)
15972 fprintf (stderr, "gimple builtin intrinsic not matched:%d %s %s\n",
15973 fn_code, fn_name1, fn_name2);
15974 break;
15977 return false;
15980 /* Expand an expression EXP that calls a built-in function,
15981 with result going to TARGET if that's convenient
15982 (and in mode MODE if that's convenient).
15983 SUBTARGET may be used as the target for computing one of EXP's operands.
15984 IGNORE is nonzero if the value is to be ignored. */
15986 static rtx
15987 rs6000_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
15988 machine_mode mode ATTRIBUTE_UNUSED,
15989 int ignore ATTRIBUTE_UNUSED)
15991 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
15992 enum rs6000_builtins fcode
15993 = (enum rs6000_builtins)DECL_FUNCTION_CODE (fndecl);
15994 size_t uns_fcode = (size_t)fcode;
15995 const struct builtin_description *d;
15996 size_t i;
15997 rtx ret;
15998 bool success;
15999 HOST_WIDE_INT mask = rs6000_builtin_info[uns_fcode].mask;
16000 bool func_valid_p = ((rs6000_builtin_mask & mask) == mask);
16001 enum insn_code icode = rs6000_builtin_info[uns_fcode].icode;
16003 /* We have two different modes (KFmode, TFmode) that are the IEEE 128-bit
16004 floating point type, depending on whether long double is the IBM extended
16005 double (KFmode) or long double is IEEE 128-bit (TFmode). It is simpler if
16006 we only define one variant of the built-in function, and switch the code
16007 when defining it, rather than defining two built-ins and using the
16008 overload table in rs6000-c.c to switch between the two. If we don't have
16009 the proper assembler, don't do this switch because CODE_FOR_*kf* and
16010 CODE_FOR_*tf* will be CODE_FOR_nothing. */
16011 if (FLOAT128_IEEE_P (TFmode))
16012 switch (icode)
16014 default:
16015 break;
16017 case CODE_FOR_sqrtkf2_odd: icode = CODE_FOR_sqrttf2_odd; break;
16018 case CODE_FOR_trunckfdf2_odd: icode = CODE_FOR_trunctfdf2_odd; break;
16019 case CODE_FOR_addkf3_odd: icode = CODE_FOR_addtf3_odd; break;
16020 case CODE_FOR_subkf3_odd: icode = CODE_FOR_subtf3_odd; break;
16021 case CODE_FOR_mulkf3_odd: icode = CODE_FOR_multf3_odd; break;
16022 case CODE_FOR_divkf3_odd: icode = CODE_FOR_divtf3_odd; break;
16023 case CODE_FOR_fmakf4_odd: icode = CODE_FOR_fmatf4_odd; break;
16024 case CODE_FOR_xsxexpqp_kf: icode = CODE_FOR_xsxexpqp_tf; break;
16025 case CODE_FOR_xsxsigqp_kf: icode = CODE_FOR_xsxsigqp_tf; break;
16026 case CODE_FOR_xststdcnegqp_kf: icode = CODE_FOR_xststdcnegqp_tf; break;
16027 case CODE_FOR_xsiexpqp_kf: icode = CODE_FOR_xsiexpqp_tf; break;
16028 case CODE_FOR_xsiexpqpf_kf: icode = CODE_FOR_xsiexpqpf_tf; break;
16029 case CODE_FOR_xststdcqp_kf: icode = CODE_FOR_xststdcqp_tf; break;
16032 if (TARGET_DEBUG_BUILTIN)
16034 const char *name1 = rs6000_builtin_info[uns_fcode].name;
16035 const char *name2 = (icode != CODE_FOR_nothing)
16036 ? get_insn_name ((int) icode)
16037 : "nothing";
16038 const char *name3;
16040 switch (rs6000_builtin_info[uns_fcode].attr & RS6000_BTC_TYPE_MASK)
16042 default: name3 = "unknown"; break;
16043 case RS6000_BTC_SPECIAL: name3 = "special"; break;
16044 case RS6000_BTC_UNARY: name3 = "unary"; break;
16045 case RS6000_BTC_BINARY: name3 = "binary"; break;
16046 case RS6000_BTC_TERNARY: name3 = "ternary"; break;
16047 case RS6000_BTC_PREDICATE: name3 = "predicate"; break;
16048 case RS6000_BTC_ABS: name3 = "abs"; break;
16049 case RS6000_BTC_DST: name3 = "dst"; break;
16053 fprintf (stderr,
16054 "rs6000_expand_builtin, %s (%d), insn = %s (%d), type=%s%s\n",
16055 (name1) ? name1 : "---", fcode,
16056 (name2) ? name2 : "---", (int) icode,
16057 name3,
16058 func_valid_p ? "" : ", not valid");
16061 if (!func_valid_p)
16063 rs6000_invalid_builtin (fcode);
16065 /* Given it is invalid, just generate a normal call. */
16066 return expand_call (exp, target, ignore);
16069 switch (fcode)
16071 case RS6000_BUILTIN_RECIP:
16072 return rs6000_expand_binop_builtin (CODE_FOR_recipdf3, exp, target);
16074 case RS6000_BUILTIN_RECIPF:
16075 return rs6000_expand_binop_builtin (CODE_FOR_recipsf3, exp, target);
16077 case RS6000_BUILTIN_RSQRTF:
16078 return rs6000_expand_unop_builtin (CODE_FOR_rsqrtsf2, exp, target);
16080 case RS6000_BUILTIN_RSQRT:
16081 return rs6000_expand_unop_builtin (CODE_FOR_rsqrtdf2, exp, target);
16083 case POWER7_BUILTIN_BPERMD:
16084 return rs6000_expand_binop_builtin (((TARGET_64BIT)
16085 ? CODE_FOR_bpermd_di
16086 : CODE_FOR_bpermd_si), exp, target);
16088 case RS6000_BUILTIN_GET_TB:
16089 return rs6000_expand_zeroop_builtin (CODE_FOR_rs6000_get_timebase,
16090 target);
16092 case RS6000_BUILTIN_MFTB:
16093 return rs6000_expand_zeroop_builtin (((TARGET_64BIT)
16094 ? CODE_FOR_rs6000_mftb_di
16095 : CODE_FOR_rs6000_mftb_si),
16096 target);
16098 case RS6000_BUILTIN_MFFS:
16099 return rs6000_expand_zeroop_builtin (CODE_FOR_rs6000_mffs, target);
16101 case RS6000_BUILTIN_MTFSB0:
16102 return rs6000_expand_mtfsb_builtin (CODE_FOR_rs6000_mtfsb0, exp);
16104 case RS6000_BUILTIN_MTFSB1:
16105 return rs6000_expand_mtfsb_builtin (CODE_FOR_rs6000_mtfsb1, exp);
16107 case RS6000_BUILTIN_SET_FPSCR_RN:
16108 return rs6000_expand_set_fpscr_rn_builtin (CODE_FOR_rs6000_set_fpscr_rn,
16109 exp);
16111 case RS6000_BUILTIN_SET_FPSCR_DRN:
16112 return
16113 rs6000_expand_set_fpscr_drn_builtin (CODE_FOR_rs6000_set_fpscr_drn,
16114 exp);
16116 case RS6000_BUILTIN_MFFSL:
16117 return rs6000_expand_zeroop_builtin (CODE_FOR_rs6000_mffsl, target);
16119 case RS6000_BUILTIN_MTFSF:
16120 return rs6000_expand_mtfsf_builtin (CODE_FOR_rs6000_mtfsf, exp);
16122 case RS6000_BUILTIN_CPU_INIT:
16123 case RS6000_BUILTIN_CPU_IS:
16124 case RS6000_BUILTIN_CPU_SUPPORTS:
16125 return cpu_expand_builtin (fcode, exp, target);
16127 case MISC_BUILTIN_SPEC_BARRIER:
16129 emit_insn (gen_speculation_barrier ());
16130 return NULL_RTX;
16133 case ALTIVEC_BUILTIN_MASK_FOR_LOAD:
16134 case ALTIVEC_BUILTIN_MASK_FOR_STORE:
16136 int icode2 = (BYTES_BIG_ENDIAN ? (int) CODE_FOR_altivec_lvsr_direct
16137 : (int) CODE_FOR_altivec_lvsl_direct);
16138 machine_mode tmode = insn_data[icode2].operand[0].mode;
16139 machine_mode mode = insn_data[icode2].operand[1].mode;
16140 tree arg;
16141 rtx op, addr, pat;
16143 gcc_assert (TARGET_ALTIVEC);
16145 arg = CALL_EXPR_ARG (exp, 0);
16146 gcc_assert (POINTER_TYPE_P (TREE_TYPE (arg)));
16147 op = expand_expr (arg, NULL_RTX, Pmode, EXPAND_NORMAL);
16148 addr = memory_address (mode, op);
16149 if (fcode == ALTIVEC_BUILTIN_MASK_FOR_STORE)
16150 op = addr;
16151 else
16153 /* For the load case need to negate the address. */
16154 op = gen_reg_rtx (GET_MODE (addr));
16155 emit_insn (gen_rtx_SET (op, gen_rtx_NEG (GET_MODE (addr), addr)));
16157 op = gen_rtx_MEM (mode, op);
16159 if (target == 0
16160 || GET_MODE (target) != tmode
16161 || ! (*insn_data[icode2].operand[0].predicate) (target, tmode))
16162 target = gen_reg_rtx (tmode);
16164 pat = GEN_FCN (icode2) (target, op);
16165 if (!pat)
16166 return 0;
16167 emit_insn (pat);
16169 return target;
16172 case ALTIVEC_BUILTIN_VCFUX:
16173 case ALTIVEC_BUILTIN_VCFSX:
16174 case ALTIVEC_BUILTIN_VCTUXS:
16175 case ALTIVEC_BUILTIN_VCTSXS:
16176 /* FIXME: There's got to be a nicer way to handle this case than
16177 constructing a new CALL_EXPR. */
16178 if (call_expr_nargs (exp) == 1)
16180 exp = build_call_nary (TREE_TYPE (exp), CALL_EXPR_FN (exp),
16181 2, CALL_EXPR_ARG (exp, 0), integer_zero_node);
16183 break;
16185 /* For the pack and unpack int128 routines, fix up the builtin so it
16186 uses the correct IBM128 type. */
16187 case MISC_BUILTIN_PACK_IF:
16188 if (TARGET_LONG_DOUBLE_128 && !TARGET_IEEEQUAD)
16190 icode = CODE_FOR_packtf;
16191 fcode = MISC_BUILTIN_PACK_TF;
16192 uns_fcode = (size_t)fcode;
16194 break;
16196 case MISC_BUILTIN_UNPACK_IF:
16197 if (TARGET_LONG_DOUBLE_128 && !TARGET_IEEEQUAD)
16199 icode = CODE_FOR_unpacktf;
16200 fcode = MISC_BUILTIN_UNPACK_TF;
16201 uns_fcode = (size_t)fcode;
16203 break;
16205 default:
16206 break;
16209 if (TARGET_ALTIVEC)
16211 ret = altivec_expand_builtin (exp, target, &success);
16213 if (success)
16214 return ret;
16216 if (TARGET_HTM)
16218 ret = htm_expand_builtin (exp, target, &success);
16220 if (success)
16221 return ret;
16224 unsigned attr = rs6000_builtin_info[uns_fcode].attr & RS6000_BTC_TYPE_MASK;
16225 /* RS6000_BTC_SPECIAL represents no-operand operators. */
16226 gcc_assert (attr == RS6000_BTC_UNARY
16227 || attr == RS6000_BTC_BINARY
16228 || attr == RS6000_BTC_TERNARY
16229 || attr == RS6000_BTC_SPECIAL);
16231 /* Handle simple unary operations. */
16232 d = bdesc_1arg;
16233 for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
16234 if (d->code == fcode)
16235 return rs6000_expand_unop_builtin (icode, exp, target);
16237 /* Handle simple binary operations. */
16238 d = bdesc_2arg;
16239 for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
16240 if (d->code == fcode)
16241 return rs6000_expand_binop_builtin (icode, exp, target);
16243 /* Handle simple ternary operations. */
16244 d = bdesc_3arg;
16245 for (i = 0; i < ARRAY_SIZE (bdesc_3arg); i++, d++)
16246 if (d->code == fcode)
16247 return rs6000_expand_ternop_builtin (icode, exp, target);
16249 /* Handle simple no-argument operations. */
16250 d = bdesc_0arg;
16251 for (i = 0; i < ARRAY_SIZE (bdesc_0arg); i++, d++)
16252 if (d->code == fcode)
16253 return rs6000_expand_zeroop_builtin (icode, target);
16255 gcc_unreachable ();
16258 /* Create a builtin vector type with a name. Taking care not to give
16259 the canonical type a name. */
16261 static tree
16262 rs6000_vector_type (const char *name, tree elt_type, unsigned num_elts)
16264 tree result = build_vector_type (elt_type, num_elts);
16266 /* Copy so we don't give the canonical type a name. */
16267 result = build_variant_type_copy (result);
16269 add_builtin_type (name, result);
16271 return result;
16274 static void
16275 rs6000_init_builtins (void)
16277 tree tdecl;
16278 tree ftype;
16279 machine_mode mode;
16281 if (TARGET_DEBUG_BUILTIN)
16282 fprintf (stderr, "rs6000_init_builtins%s%s\n",
16283 (TARGET_ALTIVEC) ? ", altivec" : "",
16284 (TARGET_VSX) ? ", vsx" : "");
16286 V2DI_type_node = rs6000_vector_type (TARGET_POWERPC64 ? "__vector long"
16287 : "__vector long long",
16288 intDI_type_node, 2);
16289 V2DF_type_node = rs6000_vector_type ("__vector double", double_type_node, 2);
16290 V4SI_type_node = rs6000_vector_type ("__vector signed int",
16291 intSI_type_node, 4);
16292 V4SF_type_node = rs6000_vector_type ("__vector float", float_type_node, 4);
16293 V8HI_type_node = rs6000_vector_type ("__vector signed short",
16294 intHI_type_node, 8);
16295 V16QI_type_node = rs6000_vector_type ("__vector signed char",
16296 intQI_type_node, 16);
16298 unsigned_V16QI_type_node = rs6000_vector_type ("__vector unsigned char",
16299 unsigned_intQI_type_node, 16);
16300 unsigned_V8HI_type_node = rs6000_vector_type ("__vector unsigned short",
16301 unsigned_intHI_type_node, 8);
16302 unsigned_V4SI_type_node = rs6000_vector_type ("__vector unsigned int",
16303 unsigned_intSI_type_node, 4);
16304 unsigned_V2DI_type_node = rs6000_vector_type (TARGET_POWERPC64
16305 ? "__vector unsigned long"
16306 : "__vector unsigned long long",
16307 unsigned_intDI_type_node, 2);
16309 opaque_V4SI_type_node = build_opaque_vector_type (intSI_type_node, 4);
16311 const_str_type_node
16312 = build_pointer_type (build_qualified_type (char_type_node,
16313 TYPE_QUAL_CONST));
16315 /* We use V1TI mode as a special container to hold __int128_t items that
16316 must live in VSX registers. */
16317 if (intTI_type_node)
16319 V1TI_type_node = rs6000_vector_type ("__vector __int128",
16320 intTI_type_node, 1);
16321 unsigned_V1TI_type_node
16322 = rs6000_vector_type ("__vector unsigned __int128",
16323 unsigned_intTI_type_node, 1);
16326 /* The 'vector bool ...' types must be kept distinct from 'vector unsigned ...'
16327 types, especially in C++ land. Similarly, 'vector pixel' is distinct from
16328 'vector unsigned short'. */
16330 bool_char_type_node = build_distinct_type_copy (unsigned_intQI_type_node);
16331 bool_short_type_node = build_distinct_type_copy (unsigned_intHI_type_node);
16332 bool_int_type_node = build_distinct_type_copy (unsigned_intSI_type_node);
16333 bool_long_long_type_node = build_distinct_type_copy (unsigned_intDI_type_node);
16334 pixel_type_node = build_distinct_type_copy (unsigned_intHI_type_node);
16336 long_integer_type_internal_node = long_integer_type_node;
16337 long_unsigned_type_internal_node = long_unsigned_type_node;
16338 long_long_integer_type_internal_node = long_long_integer_type_node;
16339 long_long_unsigned_type_internal_node = long_long_unsigned_type_node;
16340 intQI_type_internal_node = intQI_type_node;
16341 uintQI_type_internal_node = unsigned_intQI_type_node;
16342 intHI_type_internal_node = intHI_type_node;
16343 uintHI_type_internal_node = unsigned_intHI_type_node;
16344 intSI_type_internal_node = intSI_type_node;
16345 uintSI_type_internal_node = unsigned_intSI_type_node;
16346 intDI_type_internal_node = intDI_type_node;
16347 uintDI_type_internal_node = unsigned_intDI_type_node;
16348 intTI_type_internal_node = intTI_type_node;
16349 uintTI_type_internal_node = unsigned_intTI_type_node;
16350 float_type_internal_node = float_type_node;
16351 double_type_internal_node = double_type_node;
16352 long_double_type_internal_node = long_double_type_node;
16353 dfloat64_type_internal_node = dfloat64_type_node;
16354 dfloat128_type_internal_node = dfloat128_type_node;
16355 void_type_internal_node = void_type_node;
16357 /* 128-bit floating point support. KFmode is IEEE 128-bit floating point.
16358 IFmode is the IBM extended 128-bit format that is a pair of doubles.
16359 TFmode will be either IEEE 128-bit floating point or the IBM double-double
16360 format that uses a pair of doubles, depending on the switches and
16361 defaults.
16363 If we don't support for either 128-bit IBM double double or IEEE 128-bit
16364 floating point, we need make sure the type is non-zero or else self-test
16365 fails during bootstrap.
16367 Always create __ibm128 as a separate type, even if the current long double
16368 format is IBM extended double.
16370 For IEEE 128-bit floating point, always create the type __ieee128. If the
16371 user used -mfloat128, rs6000-c.c will create a define from __float128 to
16372 __ieee128. */
16373 if (TARGET_FLOAT128_TYPE)
16375 if (!TARGET_IEEEQUAD && TARGET_LONG_DOUBLE_128)
16376 ibm128_float_type_node = long_double_type_node;
16377 else
16379 ibm128_float_type_node = make_node (REAL_TYPE);
16380 TYPE_PRECISION (ibm128_float_type_node) = 128;
16381 SET_TYPE_MODE (ibm128_float_type_node, IFmode);
16382 layout_type (ibm128_float_type_node);
16385 lang_hooks.types.register_builtin_type (ibm128_float_type_node,
16386 "__ibm128");
16388 if (TARGET_IEEEQUAD && TARGET_LONG_DOUBLE_128)
16389 ieee128_float_type_node = long_double_type_node;
16390 else
16391 ieee128_float_type_node = float128_type_node;
16393 lang_hooks.types.register_builtin_type (ieee128_float_type_node,
16394 "__ieee128");
16397 else
16398 ieee128_float_type_node = ibm128_float_type_node = long_double_type_node;
16400 /* Initialize the modes for builtin_function_type, mapping a machine mode to
16401 tree type node. */
16402 builtin_mode_to_type[QImode][0] = integer_type_node;
16403 builtin_mode_to_type[HImode][0] = integer_type_node;
16404 builtin_mode_to_type[SImode][0] = intSI_type_node;
16405 builtin_mode_to_type[SImode][1] = unsigned_intSI_type_node;
16406 builtin_mode_to_type[DImode][0] = intDI_type_node;
16407 builtin_mode_to_type[DImode][1] = unsigned_intDI_type_node;
16408 builtin_mode_to_type[TImode][0] = intTI_type_node;
16409 builtin_mode_to_type[TImode][1] = unsigned_intTI_type_node;
16410 builtin_mode_to_type[SFmode][0] = float_type_node;
16411 builtin_mode_to_type[DFmode][0] = double_type_node;
16412 builtin_mode_to_type[IFmode][0] = ibm128_float_type_node;
16413 builtin_mode_to_type[KFmode][0] = ieee128_float_type_node;
16414 builtin_mode_to_type[TFmode][0] = long_double_type_node;
16415 builtin_mode_to_type[DDmode][0] = dfloat64_type_node;
16416 builtin_mode_to_type[TDmode][0] = dfloat128_type_node;
16417 builtin_mode_to_type[V1TImode][0] = V1TI_type_node;
16418 builtin_mode_to_type[V1TImode][1] = unsigned_V1TI_type_node;
16419 builtin_mode_to_type[V2DImode][0] = V2DI_type_node;
16420 builtin_mode_to_type[V2DImode][1] = unsigned_V2DI_type_node;
16421 builtin_mode_to_type[V2DFmode][0] = V2DF_type_node;
16422 builtin_mode_to_type[V4SImode][0] = V4SI_type_node;
16423 builtin_mode_to_type[V4SImode][1] = unsigned_V4SI_type_node;
16424 builtin_mode_to_type[V4SFmode][0] = V4SF_type_node;
16425 builtin_mode_to_type[V8HImode][0] = V8HI_type_node;
16426 builtin_mode_to_type[V8HImode][1] = unsigned_V8HI_type_node;
16427 builtin_mode_to_type[V16QImode][0] = V16QI_type_node;
16428 builtin_mode_to_type[V16QImode][1] = unsigned_V16QI_type_node;
16430 tdecl = add_builtin_type ("__bool char", bool_char_type_node);
16431 TYPE_NAME (bool_char_type_node) = tdecl;
16433 tdecl = add_builtin_type ("__bool short", bool_short_type_node);
16434 TYPE_NAME (bool_short_type_node) = tdecl;
16436 tdecl = add_builtin_type ("__bool int", bool_int_type_node);
16437 TYPE_NAME (bool_int_type_node) = tdecl;
16439 tdecl = add_builtin_type ("__pixel", pixel_type_node);
16440 TYPE_NAME (pixel_type_node) = tdecl;
16442 bool_V16QI_type_node = rs6000_vector_type ("__vector __bool char",
16443 bool_char_type_node, 16);
16444 bool_V8HI_type_node = rs6000_vector_type ("__vector __bool short",
16445 bool_short_type_node, 8);
16446 bool_V4SI_type_node = rs6000_vector_type ("__vector __bool int",
16447 bool_int_type_node, 4);
16448 bool_V2DI_type_node = rs6000_vector_type (TARGET_POWERPC64
16449 ? "__vector __bool long"
16450 : "__vector __bool long long",
16451 bool_long_long_type_node, 2);
16452 pixel_V8HI_type_node = rs6000_vector_type ("__vector __pixel",
16453 pixel_type_node, 8);
16455 /* Create Altivec and VSX builtins on machines with at least the
16456 general purpose extensions (970 and newer) to allow the use of
16457 the target attribute. */
16458 if (TARGET_EXTRA_BUILTINS)
16459 altivec_init_builtins ();
16460 if (TARGET_HTM)
16461 htm_init_builtins ();
16463 if (TARGET_EXTRA_BUILTINS)
16464 rs6000_common_init_builtins ();
16466 ftype = builtin_function_type (DFmode, DFmode, DFmode, VOIDmode,
16467 RS6000_BUILTIN_RECIP, "__builtin_recipdiv");
16468 def_builtin ("__builtin_recipdiv", ftype, RS6000_BUILTIN_RECIP);
16470 ftype = builtin_function_type (SFmode, SFmode, SFmode, VOIDmode,
16471 RS6000_BUILTIN_RECIPF, "__builtin_recipdivf");
16472 def_builtin ("__builtin_recipdivf", ftype, RS6000_BUILTIN_RECIPF);
16474 ftype = builtin_function_type (DFmode, DFmode, VOIDmode, VOIDmode,
16475 RS6000_BUILTIN_RSQRT, "__builtin_rsqrt");
16476 def_builtin ("__builtin_rsqrt", ftype, RS6000_BUILTIN_RSQRT);
16478 ftype = builtin_function_type (SFmode, SFmode, VOIDmode, VOIDmode,
16479 RS6000_BUILTIN_RSQRTF, "__builtin_rsqrtf");
16480 def_builtin ("__builtin_rsqrtf", ftype, RS6000_BUILTIN_RSQRTF);
16482 mode = (TARGET_64BIT) ? DImode : SImode;
16483 ftype = builtin_function_type (mode, mode, mode, VOIDmode,
16484 POWER7_BUILTIN_BPERMD, "__builtin_bpermd");
16485 def_builtin ("__builtin_bpermd", ftype, POWER7_BUILTIN_BPERMD);
16487 ftype = build_function_type_list (unsigned_intDI_type_node,
16488 NULL_TREE);
16489 def_builtin ("__builtin_ppc_get_timebase", ftype, RS6000_BUILTIN_GET_TB);
16491 if (TARGET_64BIT)
16492 ftype = build_function_type_list (unsigned_intDI_type_node,
16493 NULL_TREE);
16494 else
16495 ftype = build_function_type_list (unsigned_intSI_type_node,
16496 NULL_TREE);
16497 def_builtin ("__builtin_ppc_mftb", ftype, RS6000_BUILTIN_MFTB);
16499 ftype = build_function_type_list (double_type_node, NULL_TREE);
16500 def_builtin ("__builtin_mffs", ftype, RS6000_BUILTIN_MFFS);
16502 ftype = build_function_type_list (double_type_node, NULL_TREE);
16503 def_builtin ("__builtin_mffsl", ftype, RS6000_BUILTIN_MFFSL);
16505 ftype = build_function_type_list (void_type_node,
16506 intSI_type_node,
16507 NULL_TREE);
16508 def_builtin ("__builtin_mtfsb0", ftype, RS6000_BUILTIN_MTFSB0);
16510 ftype = build_function_type_list (void_type_node,
16511 intSI_type_node,
16512 NULL_TREE);
16513 def_builtin ("__builtin_mtfsb1", ftype, RS6000_BUILTIN_MTFSB1);
16515 ftype = build_function_type_list (void_type_node,
16516 intDI_type_node,
16517 NULL_TREE);
16518 def_builtin ("__builtin_set_fpscr_rn", ftype, RS6000_BUILTIN_SET_FPSCR_RN);
16520 ftype = build_function_type_list (void_type_node,
16521 intDI_type_node,
16522 NULL_TREE);
16523 def_builtin ("__builtin_set_fpscr_drn", ftype, RS6000_BUILTIN_SET_FPSCR_DRN);
16525 ftype = build_function_type_list (void_type_node,
16526 intSI_type_node, double_type_node,
16527 NULL_TREE);
16528 def_builtin ("__builtin_mtfsf", ftype, RS6000_BUILTIN_MTFSF);
16530 ftype = build_function_type_list (void_type_node, NULL_TREE);
16531 def_builtin ("__builtin_cpu_init", ftype, RS6000_BUILTIN_CPU_INIT);
16532 def_builtin ("__builtin_ppc_speculation_barrier", ftype,
16533 MISC_BUILTIN_SPEC_BARRIER);
16535 ftype = build_function_type_list (bool_int_type_node, const_ptr_type_node,
16536 NULL_TREE);
16537 def_builtin ("__builtin_cpu_is", ftype, RS6000_BUILTIN_CPU_IS);
16538 def_builtin ("__builtin_cpu_supports", ftype, RS6000_BUILTIN_CPU_SUPPORTS);
16540 /* AIX libm provides clog as __clog. */
16541 if (TARGET_XCOFF &&
16542 (tdecl = builtin_decl_explicit (BUILT_IN_CLOG)) != NULL_TREE)
16543 set_user_assembler_name (tdecl, "__clog");
16545 #ifdef SUBTARGET_INIT_BUILTINS
16546 SUBTARGET_INIT_BUILTINS;
16547 #endif
16550 /* Returns the rs6000 builtin decl for CODE. */
16552 static tree
16553 rs6000_builtin_decl (unsigned code, bool initialize_p ATTRIBUTE_UNUSED)
16555 HOST_WIDE_INT fnmask;
16557 if (code >= RS6000_BUILTIN_COUNT)
16558 return error_mark_node;
16560 fnmask = rs6000_builtin_info[code].mask;
16561 if ((fnmask & rs6000_builtin_mask) != fnmask)
16563 rs6000_invalid_builtin ((enum rs6000_builtins)code);
16564 return error_mark_node;
16567 return rs6000_builtin_decls[code];
16570 static void
16571 altivec_init_builtins (void)
16573 const struct builtin_description *d;
16574 size_t i;
16575 tree ftype;
16576 tree decl;
16577 HOST_WIDE_INT builtin_mask = rs6000_builtin_mask;
16579 tree pvoid_type_node = build_pointer_type (void_type_node);
16581 tree pcvoid_type_node
16582 = build_pointer_type (build_qualified_type (void_type_node,
16583 TYPE_QUAL_CONST));
16585 tree int_ftype_opaque
16586 = build_function_type_list (integer_type_node,
16587 opaque_V4SI_type_node, NULL_TREE);
16588 tree opaque_ftype_opaque
16589 = build_function_type_list (integer_type_node, NULL_TREE);
16590 tree opaque_ftype_opaque_int
16591 = build_function_type_list (opaque_V4SI_type_node,
16592 opaque_V4SI_type_node, integer_type_node, NULL_TREE);
16593 tree opaque_ftype_opaque_opaque_int
16594 = build_function_type_list (opaque_V4SI_type_node,
16595 opaque_V4SI_type_node, opaque_V4SI_type_node,
16596 integer_type_node, NULL_TREE);
16597 tree opaque_ftype_opaque_opaque_opaque
16598 = build_function_type_list (opaque_V4SI_type_node,
16599 opaque_V4SI_type_node, opaque_V4SI_type_node,
16600 opaque_V4SI_type_node, NULL_TREE);
16601 tree opaque_ftype_opaque_opaque
16602 = build_function_type_list (opaque_V4SI_type_node,
16603 opaque_V4SI_type_node, opaque_V4SI_type_node,
16604 NULL_TREE);
16605 tree int_ftype_int_opaque_opaque
16606 = build_function_type_list (integer_type_node,
16607 integer_type_node, opaque_V4SI_type_node,
16608 opaque_V4SI_type_node, NULL_TREE);
16609 tree int_ftype_int_v4si_v4si
16610 = build_function_type_list (integer_type_node,
16611 integer_type_node, V4SI_type_node,
16612 V4SI_type_node, NULL_TREE);
16613 tree int_ftype_int_v2di_v2di
16614 = build_function_type_list (integer_type_node,
16615 integer_type_node, V2DI_type_node,
16616 V2DI_type_node, NULL_TREE);
16617 tree void_ftype_v4si
16618 = build_function_type_list (void_type_node, V4SI_type_node, NULL_TREE);
16619 tree v8hi_ftype_void
16620 = build_function_type_list (V8HI_type_node, NULL_TREE);
16621 tree void_ftype_void
16622 = build_function_type_list (void_type_node, NULL_TREE);
16623 tree void_ftype_int
16624 = build_function_type_list (void_type_node, integer_type_node, NULL_TREE);
16626 tree opaque_ftype_long_pcvoid
16627 = build_function_type_list (opaque_V4SI_type_node,
16628 long_integer_type_node, pcvoid_type_node,
16629 NULL_TREE);
16630 tree v16qi_ftype_long_pcvoid
16631 = build_function_type_list (V16QI_type_node,
16632 long_integer_type_node, pcvoid_type_node,
16633 NULL_TREE);
16634 tree v8hi_ftype_long_pcvoid
16635 = build_function_type_list (V8HI_type_node,
16636 long_integer_type_node, pcvoid_type_node,
16637 NULL_TREE);
16638 tree v4si_ftype_long_pcvoid
16639 = build_function_type_list (V4SI_type_node,
16640 long_integer_type_node, pcvoid_type_node,
16641 NULL_TREE);
16642 tree v4sf_ftype_long_pcvoid
16643 = build_function_type_list (V4SF_type_node,
16644 long_integer_type_node, pcvoid_type_node,
16645 NULL_TREE);
16646 tree v2df_ftype_long_pcvoid
16647 = build_function_type_list (V2DF_type_node,
16648 long_integer_type_node, pcvoid_type_node,
16649 NULL_TREE);
16650 tree v2di_ftype_long_pcvoid
16651 = build_function_type_list (V2DI_type_node,
16652 long_integer_type_node, pcvoid_type_node,
16653 NULL_TREE);
16654 tree v1ti_ftype_long_pcvoid
16655 = build_function_type_list (V1TI_type_node,
16656 long_integer_type_node, pcvoid_type_node,
16657 NULL_TREE);
16659 tree void_ftype_opaque_long_pvoid
16660 = build_function_type_list (void_type_node,
16661 opaque_V4SI_type_node, long_integer_type_node,
16662 pvoid_type_node, NULL_TREE);
16663 tree void_ftype_v4si_long_pvoid
16664 = build_function_type_list (void_type_node,
16665 V4SI_type_node, long_integer_type_node,
16666 pvoid_type_node, NULL_TREE);
16667 tree void_ftype_v16qi_long_pvoid
16668 = build_function_type_list (void_type_node,
16669 V16QI_type_node, long_integer_type_node,
16670 pvoid_type_node, NULL_TREE);
16672 tree void_ftype_v16qi_pvoid_long
16673 = build_function_type_list (void_type_node,
16674 V16QI_type_node, pvoid_type_node,
16675 long_integer_type_node, NULL_TREE);
16677 tree void_ftype_v8hi_long_pvoid
16678 = build_function_type_list (void_type_node,
16679 V8HI_type_node, long_integer_type_node,
16680 pvoid_type_node, NULL_TREE);
16681 tree void_ftype_v4sf_long_pvoid
16682 = build_function_type_list (void_type_node,
16683 V4SF_type_node, long_integer_type_node,
16684 pvoid_type_node, NULL_TREE);
16685 tree void_ftype_v2df_long_pvoid
16686 = build_function_type_list (void_type_node,
16687 V2DF_type_node, long_integer_type_node,
16688 pvoid_type_node, NULL_TREE);
16689 tree void_ftype_v1ti_long_pvoid
16690 = build_function_type_list (void_type_node,
16691 V1TI_type_node, long_integer_type_node,
16692 pvoid_type_node, NULL_TREE);
16693 tree void_ftype_v2di_long_pvoid
16694 = build_function_type_list (void_type_node,
16695 V2DI_type_node, long_integer_type_node,
16696 pvoid_type_node, NULL_TREE);
16697 tree int_ftype_int_v8hi_v8hi
16698 = build_function_type_list (integer_type_node,
16699 integer_type_node, V8HI_type_node,
16700 V8HI_type_node, NULL_TREE);
16701 tree int_ftype_int_v16qi_v16qi
16702 = build_function_type_list (integer_type_node,
16703 integer_type_node, V16QI_type_node,
16704 V16QI_type_node, NULL_TREE);
16705 tree int_ftype_int_v4sf_v4sf
16706 = build_function_type_list (integer_type_node,
16707 integer_type_node, V4SF_type_node,
16708 V4SF_type_node, NULL_TREE);
16709 tree int_ftype_int_v2df_v2df
16710 = build_function_type_list (integer_type_node,
16711 integer_type_node, V2DF_type_node,
16712 V2DF_type_node, NULL_TREE);
16713 tree v2di_ftype_v2di
16714 = build_function_type_list (V2DI_type_node, V2DI_type_node, NULL_TREE);
16715 tree v4si_ftype_v4si
16716 = build_function_type_list (V4SI_type_node, V4SI_type_node, NULL_TREE);
16717 tree v8hi_ftype_v8hi
16718 = build_function_type_list (V8HI_type_node, V8HI_type_node, NULL_TREE);
16719 tree v16qi_ftype_v16qi
16720 = build_function_type_list (V16QI_type_node, V16QI_type_node, NULL_TREE);
16721 tree v4sf_ftype_v4sf
16722 = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
16723 tree v2df_ftype_v2df
16724 = build_function_type_list (V2DF_type_node, V2DF_type_node, NULL_TREE);
16725 tree void_ftype_pcvoid_int_int
16726 = build_function_type_list (void_type_node,
16727 pcvoid_type_node, integer_type_node,
16728 integer_type_node, NULL_TREE);
16730 def_builtin ("__builtin_altivec_mtvscr", void_ftype_v4si, ALTIVEC_BUILTIN_MTVSCR);
16731 def_builtin ("__builtin_altivec_mfvscr", v8hi_ftype_void, ALTIVEC_BUILTIN_MFVSCR);
16732 def_builtin ("__builtin_altivec_dssall", void_ftype_void, ALTIVEC_BUILTIN_DSSALL);
16733 def_builtin ("__builtin_altivec_dss", void_ftype_int, ALTIVEC_BUILTIN_DSS);
16734 def_builtin ("__builtin_altivec_lvsl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSL);
16735 def_builtin ("__builtin_altivec_lvsr", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSR);
16736 def_builtin ("__builtin_altivec_lvebx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEBX);
16737 def_builtin ("__builtin_altivec_lvehx", v8hi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEHX);
16738 def_builtin ("__builtin_altivec_lvewx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEWX);
16739 def_builtin ("__builtin_altivec_lvxl", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVXL);
16740 def_builtin ("__builtin_altivec_lvxl_v2df", v2df_ftype_long_pcvoid,
16741 ALTIVEC_BUILTIN_LVXL_V2DF);
16742 def_builtin ("__builtin_altivec_lvxl_v2di", v2di_ftype_long_pcvoid,
16743 ALTIVEC_BUILTIN_LVXL_V2DI);
16744 def_builtin ("__builtin_altivec_lvxl_v4sf", v4sf_ftype_long_pcvoid,
16745 ALTIVEC_BUILTIN_LVXL_V4SF);
16746 def_builtin ("__builtin_altivec_lvxl_v4si", v4si_ftype_long_pcvoid,
16747 ALTIVEC_BUILTIN_LVXL_V4SI);
16748 def_builtin ("__builtin_altivec_lvxl_v8hi", v8hi_ftype_long_pcvoid,
16749 ALTIVEC_BUILTIN_LVXL_V8HI);
16750 def_builtin ("__builtin_altivec_lvxl_v16qi", v16qi_ftype_long_pcvoid,
16751 ALTIVEC_BUILTIN_LVXL_V16QI);
16752 def_builtin ("__builtin_altivec_lvx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVX);
16753 def_builtin ("__builtin_altivec_lvx_v1ti", v1ti_ftype_long_pcvoid,
16754 ALTIVEC_BUILTIN_LVX_V1TI);
16755 def_builtin ("__builtin_altivec_lvx_v2df", v2df_ftype_long_pcvoid,
16756 ALTIVEC_BUILTIN_LVX_V2DF);
16757 def_builtin ("__builtin_altivec_lvx_v2di", v2di_ftype_long_pcvoid,
16758 ALTIVEC_BUILTIN_LVX_V2DI);
16759 def_builtin ("__builtin_altivec_lvx_v4sf", v4sf_ftype_long_pcvoid,
16760 ALTIVEC_BUILTIN_LVX_V4SF);
16761 def_builtin ("__builtin_altivec_lvx_v4si", v4si_ftype_long_pcvoid,
16762 ALTIVEC_BUILTIN_LVX_V4SI);
16763 def_builtin ("__builtin_altivec_lvx_v8hi", v8hi_ftype_long_pcvoid,
16764 ALTIVEC_BUILTIN_LVX_V8HI);
16765 def_builtin ("__builtin_altivec_lvx_v16qi", v16qi_ftype_long_pcvoid,
16766 ALTIVEC_BUILTIN_LVX_V16QI);
16767 def_builtin ("__builtin_altivec_stvx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVX);
16768 def_builtin ("__builtin_altivec_stvx_v2df", void_ftype_v2df_long_pvoid,
16769 ALTIVEC_BUILTIN_STVX_V2DF);
16770 def_builtin ("__builtin_altivec_stvx_v2di", void_ftype_v2di_long_pvoid,
16771 ALTIVEC_BUILTIN_STVX_V2DI);
16772 def_builtin ("__builtin_altivec_stvx_v4sf", void_ftype_v4sf_long_pvoid,
16773 ALTIVEC_BUILTIN_STVX_V4SF);
16774 def_builtin ("__builtin_altivec_stvx_v4si", void_ftype_v4si_long_pvoid,
16775 ALTIVEC_BUILTIN_STVX_V4SI);
16776 def_builtin ("__builtin_altivec_stvx_v8hi", void_ftype_v8hi_long_pvoid,
16777 ALTIVEC_BUILTIN_STVX_V8HI);
16778 def_builtin ("__builtin_altivec_stvx_v16qi", void_ftype_v16qi_long_pvoid,
16779 ALTIVEC_BUILTIN_STVX_V16QI);
16780 def_builtin ("__builtin_altivec_stvewx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVEWX);
16781 def_builtin ("__builtin_altivec_stvxl", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVXL);
16782 def_builtin ("__builtin_altivec_stvxl_v2df", void_ftype_v2df_long_pvoid,
16783 ALTIVEC_BUILTIN_STVXL_V2DF);
16784 def_builtin ("__builtin_altivec_stvxl_v2di", void_ftype_v2di_long_pvoid,
16785 ALTIVEC_BUILTIN_STVXL_V2DI);
16786 def_builtin ("__builtin_altivec_stvxl_v4sf", void_ftype_v4sf_long_pvoid,
16787 ALTIVEC_BUILTIN_STVXL_V4SF);
16788 def_builtin ("__builtin_altivec_stvxl_v4si", void_ftype_v4si_long_pvoid,
16789 ALTIVEC_BUILTIN_STVXL_V4SI);
16790 def_builtin ("__builtin_altivec_stvxl_v8hi", void_ftype_v8hi_long_pvoid,
16791 ALTIVEC_BUILTIN_STVXL_V8HI);
16792 def_builtin ("__builtin_altivec_stvxl_v16qi", void_ftype_v16qi_long_pvoid,
16793 ALTIVEC_BUILTIN_STVXL_V16QI);
16794 def_builtin ("__builtin_altivec_stvebx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVEBX);
16795 def_builtin ("__builtin_altivec_stvehx", void_ftype_v8hi_long_pvoid, ALTIVEC_BUILTIN_STVEHX);
16796 def_builtin ("__builtin_vec_ld", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LD);
16797 def_builtin ("__builtin_vec_lde", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LDE);
16798 def_builtin ("__builtin_vec_ldl", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LDL);
16799 def_builtin ("__builtin_vec_lvsl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVSL);
16800 def_builtin ("__builtin_vec_lvsr", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVSR);
16801 def_builtin ("__builtin_vec_lvebx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEBX);
16802 def_builtin ("__builtin_vec_lvehx", v8hi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEHX);
16803 def_builtin ("__builtin_vec_lvewx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEWX);
16804 def_builtin ("__builtin_vec_st", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_ST);
16805 def_builtin ("__builtin_vec_ste", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STE);
16806 def_builtin ("__builtin_vec_stl", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STL);
16807 def_builtin ("__builtin_vec_stvewx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEWX);
16808 def_builtin ("__builtin_vec_stvebx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEBX);
16809 def_builtin ("__builtin_vec_stvehx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEHX);
16811 def_builtin ("__builtin_vsx_lxvd2x_v2df", v2df_ftype_long_pcvoid,
16812 VSX_BUILTIN_LXVD2X_V2DF);
16813 def_builtin ("__builtin_vsx_lxvd2x_v2di", v2di_ftype_long_pcvoid,
16814 VSX_BUILTIN_LXVD2X_V2DI);
16815 def_builtin ("__builtin_vsx_lxvw4x_v4sf", v4sf_ftype_long_pcvoid,
16816 VSX_BUILTIN_LXVW4X_V4SF);
16817 def_builtin ("__builtin_vsx_lxvw4x_v4si", v4si_ftype_long_pcvoid,
16818 VSX_BUILTIN_LXVW4X_V4SI);
16819 def_builtin ("__builtin_vsx_lxvw4x_v8hi", v8hi_ftype_long_pcvoid,
16820 VSX_BUILTIN_LXVW4X_V8HI);
16821 def_builtin ("__builtin_vsx_lxvw4x_v16qi", v16qi_ftype_long_pcvoid,
16822 VSX_BUILTIN_LXVW4X_V16QI);
16823 def_builtin ("__builtin_vsx_stxvd2x_v2df", void_ftype_v2df_long_pvoid,
16824 VSX_BUILTIN_STXVD2X_V2DF);
16825 def_builtin ("__builtin_vsx_stxvd2x_v2di", void_ftype_v2di_long_pvoid,
16826 VSX_BUILTIN_STXVD2X_V2DI);
16827 def_builtin ("__builtin_vsx_stxvw4x_v4sf", void_ftype_v4sf_long_pvoid,
16828 VSX_BUILTIN_STXVW4X_V4SF);
16829 def_builtin ("__builtin_vsx_stxvw4x_v4si", void_ftype_v4si_long_pvoid,
16830 VSX_BUILTIN_STXVW4X_V4SI);
16831 def_builtin ("__builtin_vsx_stxvw4x_v8hi", void_ftype_v8hi_long_pvoid,
16832 VSX_BUILTIN_STXVW4X_V8HI);
16833 def_builtin ("__builtin_vsx_stxvw4x_v16qi", void_ftype_v16qi_long_pvoid,
16834 VSX_BUILTIN_STXVW4X_V16QI);
16836 def_builtin ("__builtin_vsx_ld_elemrev_v2df", v2df_ftype_long_pcvoid,
16837 VSX_BUILTIN_LD_ELEMREV_V2DF);
16838 def_builtin ("__builtin_vsx_ld_elemrev_v2di", v2di_ftype_long_pcvoid,
16839 VSX_BUILTIN_LD_ELEMREV_V2DI);
16840 def_builtin ("__builtin_vsx_ld_elemrev_v4sf", v4sf_ftype_long_pcvoid,
16841 VSX_BUILTIN_LD_ELEMREV_V4SF);
16842 def_builtin ("__builtin_vsx_ld_elemrev_v4si", v4si_ftype_long_pcvoid,
16843 VSX_BUILTIN_LD_ELEMREV_V4SI);
16844 def_builtin ("__builtin_vsx_ld_elemrev_v8hi", v8hi_ftype_long_pcvoid,
16845 VSX_BUILTIN_LD_ELEMREV_V8HI);
16846 def_builtin ("__builtin_vsx_ld_elemrev_v16qi", v16qi_ftype_long_pcvoid,
16847 VSX_BUILTIN_LD_ELEMREV_V16QI);
16848 def_builtin ("__builtin_vsx_st_elemrev_v2df", void_ftype_v2df_long_pvoid,
16849 VSX_BUILTIN_ST_ELEMREV_V2DF);
16850 def_builtin ("__builtin_vsx_st_elemrev_v1ti", void_ftype_v1ti_long_pvoid,
16851 VSX_BUILTIN_ST_ELEMREV_V1TI);
16852 def_builtin ("__builtin_vsx_st_elemrev_v2di", void_ftype_v2di_long_pvoid,
16853 VSX_BUILTIN_ST_ELEMREV_V2DI);
16854 def_builtin ("__builtin_vsx_st_elemrev_v4sf", void_ftype_v4sf_long_pvoid,
16855 VSX_BUILTIN_ST_ELEMREV_V4SF);
16856 def_builtin ("__builtin_vsx_st_elemrev_v4si", void_ftype_v4si_long_pvoid,
16857 VSX_BUILTIN_ST_ELEMREV_V4SI);
16858 def_builtin ("__builtin_vsx_st_elemrev_v8hi", void_ftype_v8hi_long_pvoid,
16859 VSX_BUILTIN_ST_ELEMREV_V8HI);
16860 def_builtin ("__builtin_vsx_st_elemrev_v16qi", void_ftype_v16qi_long_pvoid,
16861 VSX_BUILTIN_ST_ELEMREV_V16QI);
16863 def_builtin ("__builtin_vec_vsx_ld", opaque_ftype_long_pcvoid,
16864 VSX_BUILTIN_VEC_LD);
16865 def_builtin ("__builtin_vec_vsx_st", void_ftype_opaque_long_pvoid,
16866 VSX_BUILTIN_VEC_ST);
16867 def_builtin ("__builtin_vec_xl", opaque_ftype_long_pcvoid,
16868 VSX_BUILTIN_VEC_XL);
16869 def_builtin ("__builtin_vec_xl_be", opaque_ftype_long_pcvoid,
16870 VSX_BUILTIN_VEC_XL_BE);
16871 def_builtin ("__builtin_vec_xst", void_ftype_opaque_long_pvoid,
16872 VSX_BUILTIN_VEC_XST);
16873 def_builtin ("__builtin_vec_xst_be", void_ftype_opaque_long_pvoid,
16874 VSX_BUILTIN_VEC_XST_BE);
16876 def_builtin ("__builtin_vec_step", int_ftype_opaque, ALTIVEC_BUILTIN_VEC_STEP);
16877 def_builtin ("__builtin_vec_splats", opaque_ftype_opaque, ALTIVEC_BUILTIN_VEC_SPLATS);
16878 def_builtin ("__builtin_vec_promote", opaque_ftype_opaque, ALTIVEC_BUILTIN_VEC_PROMOTE);
16880 def_builtin ("__builtin_vec_sld", opaque_ftype_opaque_opaque_int, ALTIVEC_BUILTIN_VEC_SLD);
16881 def_builtin ("__builtin_vec_splat", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_SPLAT);
16882 def_builtin ("__builtin_vec_extract", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_EXTRACT);
16883 def_builtin ("__builtin_vec_insert", opaque_ftype_opaque_opaque_int, ALTIVEC_BUILTIN_VEC_INSERT);
16884 def_builtin ("__builtin_vec_vspltw", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTW);
16885 def_builtin ("__builtin_vec_vsplth", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTH);
16886 def_builtin ("__builtin_vec_vspltb", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTB);
16887 def_builtin ("__builtin_vec_ctf", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTF);
16888 def_builtin ("__builtin_vec_vcfsx", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VCFSX);
16889 def_builtin ("__builtin_vec_vcfux", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VCFUX);
16890 def_builtin ("__builtin_vec_cts", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTS);
16891 def_builtin ("__builtin_vec_ctu", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTU);
16893 def_builtin ("__builtin_vec_adde", opaque_ftype_opaque_opaque_opaque,
16894 ALTIVEC_BUILTIN_VEC_ADDE);
16895 def_builtin ("__builtin_vec_addec", opaque_ftype_opaque_opaque_opaque,
16896 ALTIVEC_BUILTIN_VEC_ADDEC);
16897 def_builtin ("__builtin_vec_cmpne", opaque_ftype_opaque_opaque,
16898 ALTIVEC_BUILTIN_VEC_CMPNE);
16899 def_builtin ("__builtin_vec_mul", opaque_ftype_opaque_opaque,
16900 ALTIVEC_BUILTIN_VEC_MUL);
16901 def_builtin ("__builtin_vec_sube", opaque_ftype_opaque_opaque_opaque,
16902 ALTIVEC_BUILTIN_VEC_SUBE);
16903 def_builtin ("__builtin_vec_subec", opaque_ftype_opaque_opaque_opaque,
16904 ALTIVEC_BUILTIN_VEC_SUBEC);
16906 /* Cell builtins. */
16907 def_builtin ("__builtin_altivec_lvlx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVLX);
16908 def_builtin ("__builtin_altivec_lvlxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVLXL);
16909 def_builtin ("__builtin_altivec_lvrx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVRX);
16910 def_builtin ("__builtin_altivec_lvrxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVRXL);
16912 def_builtin ("__builtin_vec_lvlx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVLX);
16913 def_builtin ("__builtin_vec_lvlxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVLXL);
16914 def_builtin ("__builtin_vec_lvrx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVRX);
16915 def_builtin ("__builtin_vec_lvrxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVRXL);
16917 def_builtin ("__builtin_altivec_stvlx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVLX);
16918 def_builtin ("__builtin_altivec_stvlxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVLXL);
16919 def_builtin ("__builtin_altivec_stvrx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVRX);
16920 def_builtin ("__builtin_altivec_stvrxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVRXL);
16922 def_builtin ("__builtin_vec_stvlx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVLX);
16923 def_builtin ("__builtin_vec_stvlxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVLXL);
16924 def_builtin ("__builtin_vec_stvrx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVRX);
16925 def_builtin ("__builtin_vec_stvrxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVRXL);
16927 if (TARGET_P9_VECTOR)
16929 def_builtin ("__builtin_altivec_stxvl", void_ftype_v16qi_pvoid_long,
16930 P9V_BUILTIN_STXVL);
16931 def_builtin ("__builtin_xst_len_r", void_ftype_v16qi_pvoid_long,
16932 P9V_BUILTIN_XST_LEN_R);
16935 /* Add the DST variants. */
16936 d = bdesc_dst;
16937 for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
16939 HOST_WIDE_INT mask = d->mask;
16941 /* It is expected that these dst built-in functions may have
16942 d->icode equal to CODE_FOR_nothing. */
16943 if ((mask & builtin_mask) != mask)
16945 if (TARGET_DEBUG_BUILTIN)
16946 fprintf (stderr, "altivec_init_builtins, skip dst %s\n",
16947 d->name);
16948 continue;
16950 def_builtin (d->name, void_ftype_pcvoid_int_int, d->code);
16953 /* Initialize the predicates. */
16954 d = bdesc_altivec_preds;
16955 for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, d++)
16957 machine_mode mode1;
16958 tree type;
16959 HOST_WIDE_INT mask = d->mask;
16961 if ((mask & builtin_mask) != mask)
16963 if (TARGET_DEBUG_BUILTIN)
16964 fprintf (stderr, "altivec_init_builtins, skip predicate %s\n",
16965 d->name);
16966 continue;
16969 if (rs6000_overloaded_builtin_p (d->code))
16970 mode1 = VOIDmode;
16971 else
16973 /* Cannot define builtin if the instruction is disabled. */
16974 gcc_assert (d->icode != CODE_FOR_nothing);
16975 mode1 = insn_data[d->icode].operand[1].mode;
16978 switch (mode1)
16980 case E_VOIDmode:
16981 type = int_ftype_int_opaque_opaque;
16982 break;
16983 case E_V2DImode:
16984 type = int_ftype_int_v2di_v2di;
16985 break;
16986 case E_V4SImode:
16987 type = int_ftype_int_v4si_v4si;
16988 break;
16989 case E_V8HImode:
16990 type = int_ftype_int_v8hi_v8hi;
16991 break;
16992 case E_V16QImode:
16993 type = int_ftype_int_v16qi_v16qi;
16994 break;
16995 case E_V4SFmode:
16996 type = int_ftype_int_v4sf_v4sf;
16997 break;
16998 case E_V2DFmode:
16999 type = int_ftype_int_v2df_v2df;
17000 break;
17001 default:
17002 gcc_unreachable ();
17005 def_builtin (d->name, type, d->code);
17008 /* Initialize the abs* operators. */
17009 d = bdesc_abs;
17010 for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
17012 machine_mode mode0;
17013 tree type;
17014 HOST_WIDE_INT mask = d->mask;
17016 if ((mask & builtin_mask) != mask)
17018 if (TARGET_DEBUG_BUILTIN)
17019 fprintf (stderr, "altivec_init_builtins, skip abs %s\n",
17020 d->name);
17021 continue;
17024 /* Cannot define builtin if the instruction is disabled. */
17025 gcc_assert (d->icode != CODE_FOR_nothing);
17026 mode0 = insn_data[d->icode].operand[0].mode;
17028 switch (mode0)
17030 case E_V2DImode:
17031 type = v2di_ftype_v2di;
17032 break;
17033 case E_V4SImode:
17034 type = v4si_ftype_v4si;
17035 break;
17036 case E_V8HImode:
17037 type = v8hi_ftype_v8hi;
17038 break;
17039 case E_V16QImode:
17040 type = v16qi_ftype_v16qi;
17041 break;
17042 case E_V4SFmode:
17043 type = v4sf_ftype_v4sf;
17044 break;
17045 case E_V2DFmode:
17046 type = v2df_ftype_v2df;
17047 break;
17048 default:
17049 gcc_unreachable ();
17052 def_builtin (d->name, type, d->code);
17055 /* Initialize target builtin that implements
17056 targetm.vectorize.builtin_mask_for_load. */
17058 decl = add_builtin_function ("__builtin_altivec_mask_for_load",
17059 v16qi_ftype_long_pcvoid,
17060 ALTIVEC_BUILTIN_MASK_FOR_LOAD,
17061 BUILT_IN_MD, NULL, NULL_TREE);
17062 TREE_READONLY (decl) = 1;
17063 /* Record the decl. Will be used by rs6000_builtin_mask_for_load. */
17064 altivec_builtin_mask_for_load = decl;
17066 /* Access to the vec_init patterns. */
17067 ftype = build_function_type_list (V4SI_type_node, integer_type_node,
17068 integer_type_node, integer_type_node,
17069 integer_type_node, NULL_TREE);
17070 def_builtin ("__builtin_vec_init_v4si", ftype, ALTIVEC_BUILTIN_VEC_INIT_V4SI);
17072 ftype = build_function_type_list (V8HI_type_node, short_integer_type_node,
17073 short_integer_type_node,
17074 short_integer_type_node,
17075 short_integer_type_node,
17076 short_integer_type_node,
17077 short_integer_type_node,
17078 short_integer_type_node,
17079 short_integer_type_node, NULL_TREE);
17080 def_builtin ("__builtin_vec_init_v8hi", ftype, ALTIVEC_BUILTIN_VEC_INIT_V8HI);
17082 ftype = build_function_type_list (V16QI_type_node, char_type_node,
17083 char_type_node, char_type_node,
17084 char_type_node, char_type_node,
17085 char_type_node, char_type_node,
17086 char_type_node, char_type_node,
17087 char_type_node, char_type_node,
17088 char_type_node, char_type_node,
17089 char_type_node, char_type_node,
17090 char_type_node, NULL_TREE);
17091 def_builtin ("__builtin_vec_init_v16qi", ftype,
17092 ALTIVEC_BUILTIN_VEC_INIT_V16QI);
17094 ftype = build_function_type_list (V4SF_type_node, float_type_node,
17095 float_type_node, float_type_node,
17096 float_type_node, NULL_TREE);
17097 def_builtin ("__builtin_vec_init_v4sf", ftype, ALTIVEC_BUILTIN_VEC_INIT_V4SF);
17099 /* VSX builtins. */
17100 ftype = build_function_type_list (V2DF_type_node, double_type_node,
17101 double_type_node, NULL_TREE);
17102 def_builtin ("__builtin_vec_init_v2df", ftype, VSX_BUILTIN_VEC_INIT_V2DF);
17104 ftype = build_function_type_list (V2DI_type_node, intDI_type_node,
17105 intDI_type_node, NULL_TREE);
17106 def_builtin ("__builtin_vec_init_v2di", ftype, VSX_BUILTIN_VEC_INIT_V2DI);
17108 /* Access to the vec_set patterns. */
17109 ftype = build_function_type_list (V4SI_type_node, V4SI_type_node,
17110 intSI_type_node,
17111 integer_type_node, NULL_TREE);
17112 def_builtin ("__builtin_vec_set_v4si", ftype, ALTIVEC_BUILTIN_VEC_SET_V4SI);
17114 ftype = build_function_type_list (V8HI_type_node, V8HI_type_node,
17115 intHI_type_node,
17116 integer_type_node, NULL_TREE);
17117 def_builtin ("__builtin_vec_set_v8hi", ftype, ALTIVEC_BUILTIN_VEC_SET_V8HI);
17119 ftype = build_function_type_list (V16QI_type_node, V16QI_type_node,
17120 intQI_type_node,
17121 integer_type_node, NULL_TREE);
17122 def_builtin ("__builtin_vec_set_v16qi", ftype, ALTIVEC_BUILTIN_VEC_SET_V16QI);
17124 ftype = build_function_type_list (V4SF_type_node, V4SF_type_node,
17125 float_type_node,
17126 integer_type_node, NULL_TREE);
17127 def_builtin ("__builtin_vec_set_v4sf", ftype, ALTIVEC_BUILTIN_VEC_SET_V4SF);
17129 ftype = build_function_type_list (V2DF_type_node, V2DF_type_node,
17130 double_type_node,
17131 integer_type_node, NULL_TREE);
17132 def_builtin ("__builtin_vec_set_v2df", ftype, VSX_BUILTIN_VEC_SET_V2DF);
17134 ftype = build_function_type_list (V2DI_type_node, V2DI_type_node,
17135 intDI_type_node,
17136 integer_type_node, NULL_TREE);
17137 def_builtin ("__builtin_vec_set_v2di", ftype, VSX_BUILTIN_VEC_SET_V2DI);
17139 /* Access to the vec_extract patterns. */
17140 ftype = build_function_type_list (intSI_type_node, V4SI_type_node,
17141 integer_type_node, NULL_TREE);
17142 def_builtin ("__builtin_vec_ext_v4si", ftype, ALTIVEC_BUILTIN_VEC_EXT_V4SI);
17144 ftype = build_function_type_list (intHI_type_node, V8HI_type_node,
17145 integer_type_node, NULL_TREE);
17146 def_builtin ("__builtin_vec_ext_v8hi", ftype, ALTIVEC_BUILTIN_VEC_EXT_V8HI);
17148 ftype = build_function_type_list (intQI_type_node, V16QI_type_node,
17149 integer_type_node, NULL_TREE);
17150 def_builtin ("__builtin_vec_ext_v16qi", ftype, ALTIVEC_BUILTIN_VEC_EXT_V16QI);
17152 ftype = build_function_type_list (float_type_node, V4SF_type_node,
17153 integer_type_node, NULL_TREE);
17154 def_builtin ("__builtin_vec_ext_v4sf", ftype, ALTIVEC_BUILTIN_VEC_EXT_V4SF);
17156 ftype = build_function_type_list (double_type_node, V2DF_type_node,
17157 integer_type_node, NULL_TREE);
17158 def_builtin ("__builtin_vec_ext_v2df", ftype, VSX_BUILTIN_VEC_EXT_V2DF);
17160 ftype = build_function_type_list (intDI_type_node, V2DI_type_node,
17161 integer_type_node, NULL_TREE);
17162 def_builtin ("__builtin_vec_ext_v2di", ftype, VSX_BUILTIN_VEC_EXT_V2DI);
17165 if (V1TI_type_node)
17167 tree v1ti_ftype_long_pcvoid
17168 = build_function_type_list (V1TI_type_node,
17169 long_integer_type_node, pcvoid_type_node,
17170 NULL_TREE);
17171 tree void_ftype_v1ti_long_pvoid
17172 = build_function_type_list (void_type_node,
17173 V1TI_type_node, long_integer_type_node,
17174 pvoid_type_node, NULL_TREE);
17175 def_builtin ("__builtin_vsx_ld_elemrev_v1ti", v1ti_ftype_long_pcvoid,
17176 VSX_BUILTIN_LD_ELEMREV_V1TI);
17177 def_builtin ("__builtin_vsx_lxvd2x_v1ti", v1ti_ftype_long_pcvoid,
17178 VSX_BUILTIN_LXVD2X_V1TI);
17179 def_builtin ("__builtin_vsx_stxvd2x_v1ti", void_ftype_v1ti_long_pvoid,
17180 VSX_BUILTIN_STXVD2X_V1TI);
17181 ftype = build_function_type_list (V1TI_type_node, intTI_type_node,
17182 NULL_TREE, NULL_TREE);
17183 def_builtin ("__builtin_vec_init_v1ti", ftype, VSX_BUILTIN_VEC_INIT_V1TI);
17184 ftype = build_function_type_list (V1TI_type_node, V1TI_type_node,
17185 intTI_type_node,
17186 integer_type_node, NULL_TREE);
17187 def_builtin ("__builtin_vec_set_v1ti", ftype, VSX_BUILTIN_VEC_SET_V1TI);
17188 ftype = build_function_type_list (intTI_type_node, V1TI_type_node,
17189 integer_type_node, NULL_TREE);
17190 def_builtin ("__builtin_vec_ext_v1ti", ftype, VSX_BUILTIN_VEC_EXT_V1TI);
17195 static void
17196 htm_init_builtins (void)
17198 HOST_WIDE_INT builtin_mask = rs6000_builtin_mask;
17199 const struct builtin_description *d;
17200 size_t i;
17202 d = bdesc_htm;
17203 for (i = 0; i < ARRAY_SIZE (bdesc_htm); i++, d++)
17205 tree op[MAX_HTM_OPERANDS], type;
17206 HOST_WIDE_INT mask = d->mask;
17207 unsigned attr = rs6000_builtin_info[d->code].attr;
17208 bool void_func = (attr & RS6000_BTC_VOID);
17209 int attr_args = (attr & RS6000_BTC_TYPE_MASK);
17210 int nopnds = 0;
17211 tree gpr_type_node;
17212 tree rettype;
17213 tree argtype;
17215 /* It is expected that these htm built-in functions may have
17216 d->icode equal to CODE_FOR_nothing. */
17218 if (TARGET_32BIT && TARGET_POWERPC64)
17219 gpr_type_node = long_long_unsigned_type_node;
17220 else
17221 gpr_type_node = long_unsigned_type_node;
17223 if (attr & RS6000_BTC_SPR)
17225 rettype = gpr_type_node;
17226 argtype = gpr_type_node;
17228 else if (d->code == HTM_BUILTIN_TABORTDC
17229 || d->code == HTM_BUILTIN_TABORTDCI)
17231 rettype = unsigned_type_node;
17232 argtype = gpr_type_node;
17234 else
17236 rettype = unsigned_type_node;
17237 argtype = unsigned_type_node;
17240 if ((mask & builtin_mask) != mask)
17242 if (TARGET_DEBUG_BUILTIN)
17243 fprintf (stderr, "htm_builtin, skip binary %s\n", d->name);
17244 continue;
17247 if (d->name == 0)
17249 if (TARGET_DEBUG_BUILTIN)
17250 fprintf (stderr, "htm_builtin, bdesc_htm[%ld] no name\n",
17251 (long unsigned) i);
17252 continue;
17255 op[nopnds++] = (void_func) ? void_type_node : rettype;
17257 if (attr_args == RS6000_BTC_UNARY)
17258 op[nopnds++] = argtype;
17259 else if (attr_args == RS6000_BTC_BINARY)
17261 op[nopnds++] = argtype;
17262 op[nopnds++] = argtype;
17264 else if (attr_args == RS6000_BTC_TERNARY)
17266 op[nopnds++] = argtype;
17267 op[nopnds++] = argtype;
17268 op[nopnds++] = argtype;
17271 switch (nopnds)
17273 case 1:
17274 type = build_function_type_list (op[0], NULL_TREE);
17275 break;
17276 case 2:
17277 type = build_function_type_list (op[0], op[1], NULL_TREE);
17278 break;
17279 case 3:
17280 type = build_function_type_list (op[0], op[1], op[2], NULL_TREE);
17281 break;
17282 case 4:
17283 type = build_function_type_list (op[0], op[1], op[2], op[3],
17284 NULL_TREE);
17285 break;
17286 default:
17287 gcc_unreachable ();
17290 def_builtin (d->name, type, d->code);
17294 /* Hash function for builtin functions with up to 3 arguments and a return
17295 type. */
17296 hashval_t
17297 builtin_hasher::hash (builtin_hash_struct *bh)
17299 unsigned ret = 0;
17300 int i;
17302 for (i = 0; i < 4; i++)
17304 ret = (ret * (unsigned)MAX_MACHINE_MODE) + ((unsigned)bh->mode[i]);
17305 ret = (ret * 2) + bh->uns_p[i];
17308 return ret;
17311 /* Compare builtin hash entries H1 and H2 for equivalence. */
17312 bool
17313 builtin_hasher::equal (builtin_hash_struct *p1, builtin_hash_struct *p2)
17315 return ((p1->mode[0] == p2->mode[0])
17316 && (p1->mode[1] == p2->mode[1])
17317 && (p1->mode[2] == p2->mode[2])
17318 && (p1->mode[3] == p2->mode[3])
17319 && (p1->uns_p[0] == p2->uns_p[0])
17320 && (p1->uns_p[1] == p2->uns_p[1])
17321 && (p1->uns_p[2] == p2->uns_p[2])
17322 && (p1->uns_p[3] == p2->uns_p[3]));
17325 /* Map types for builtin functions with an explicit return type and up to 3
17326 arguments. Functions with fewer than 3 arguments use VOIDmode as the type
17327 of the argument. */
17328 static tree
17329 builtin_function_type (machine_mode mode_ret, machine_mode mode_arg0,
17330 machine_mode mode_arg1, machine_mode mode_arg2,
17331 enum rs6000_builtins builtin, const char *name)
17333 struct builtin_hash_struct h;
17334 struct builtin_hash_struct *h2;
17335 int num_args = 3;
17336 int i;
17337 tree ret_type = NULL_TREE;
17338 tree arg_type[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
17340 /* Create builtin_hash_table. */
17341 if (builtin_hash_table == NULL)
17342 builtin_hash_table = hash_table<builtin_hasher>::create_ggc (1500);
17344 h.type = NULL_TREE;
17345 h.mode[0] = mode_ret;
17346 h.mode[1] = mode_arg0;
17347 h.mode[2] = mode_arg1;
17348 h.mode[3] = mode_arg2;
17349 h.uns_p[0] = 0;
17350 h.uns_p[1] = 0;
17351 h.uns_p[2] = 0;
17352 h.uns_p[3] = 0;
17354 /* If the builtin is a type that produces unsigned results or takes unsigned
17355 arguments, and it is returned as a decl for the vectorizer (such as
17356 widening multiplies, permute), make sure the arguments and return value
17357 are type correct. */
17358 switch (builtin)
17360 /* unsigned 1 argument functions. */
17361 case CRYPTO_BUILTIN_VSBOX:
17362 case CRYPTO_BUILTIN_VSBOX_BE:
17363 case P8V_BUILTIN_VGBBD:
17364 case MISC_BUILTIN_CDTBCD:
17365 case MISC_BUILTIN_CBCDTD:
17366 h.uns_p[0] = 1;
17367 h.uns_p[1] = 1;
17368 break;
17370 /* unsigned 2 argument functions. */
17371 case ALTIVEC_BUILTIN_VMULEUB:
17372 case ALTIVEC_BUILTIN_VMULEUH:
17373 case P8V_BUILTIN_VMULEUW:
17374 case ALTIVEC_BUILTIN_VMULOUB:
17375 case ALTIVEC_BUILTIN_VMULOUH:
17376 case P8V_BUILTIN_VMULOUW:
17377 case CRYPTO_BUILTIN_VCIPHER:
17378 case CRYPTO_BUILTIN_VCIPHER_BE:
17379 case CRYPTO_BUILTIN_VCIPHERLAST:
17380 case CRYPTO_BUILTIN_VCIPHERLAST_BE:
17381 case CRYPTO_BUILTIN_VNCIPHER:
17382 case CRYPTO_BUILTIN_VNCIPHER_BE:
17383 case CRYPTO_BUILTIN_VNCIPHERLAST:
17384 case CRYPTO_BUILTIN_VNCIPHERLAST_BE:
17385 case CRYPTO_BUILTIN_VPMSUMB:
17386 case CRYPTO_BUILTIN_VPMSUMH:
17387 case CRYPTO_BUILTIN_VPMSUMW:
17388 case CRYPTO_BUILTIN_VPMSUMD:
17389 case CRYPTO_BUILTIN_VPMSUM:
17390 case MISC_BUILTIN_ADDG6S:
17391 case MISC_BUILTIN_DIVWEU:
17392 case MISC_BUILTIN_DIVDEU:
17393 case VSX_BUILTIN_UDIV_V2DI:
17394 case ALTIVEC_BUILTIN_VMAXUB:
17395 case ALTIVEC_BUILTIN_VMINUB:
17396 case ALTIVEC_BUILTIN_VMAXUH:
17397 case ALTIVEC_BUILTIN_VMINUH:
17398 case ALTIVEC_BUILTIN_VMAXUW:
17399 case ALTIVEC_BUILTIN_VMINUW:
17400 case P8V_BUILTIN_VMAXUD:
17401 case P8V_BUILTIN_VMINUD:
17402 h.uns_p[0] = 1;
17403 h.uns_p[1] = 1;
17404 h.uns_p[2] = 1;
17405 break;
17407 /* unsigned 3 argument functions. */
17408 case ALTIVEC_BUILTIN_VPERM_16QI_UNS:
17409 case ALTIVEC_BUILTIN_VPERM_8HI_UNS:
17410 case ALTIVEC_BUILTIN_VPERM_4SI_UNS:
17411 case ALTIVEC_BUILTIN_VPERM_2DI_UNS:
17412 case ALTIVEC_BUILTIN_VSEL_16QI_UNS:
17413 case ALTIVEC_BUILTIN_VSEL_8HI_UNS:
17414 case ALTIVEC_BUILTIN_VSEL_4SI_UNS:
17415 case ALTIVEC_BUILTIN_VSEL_2DI_UNS:
17416 case VSX_BUILTIN_VPERM_16QI_UNS:
17417 case VSX_BUILTIN_VPERM_8HI_UNS:
17418 case VSX_BUILTIN_VPERM_4SI_UNS:
17419 case VSX_BUILTIN_VPERM_2DI_UNS:
17420 case VSX_BUILTIN_XXSEL_16QI_UNS:
17421 case VSX_BUILTIN_XXSEL_8HI_UNS:
17422 case VSX_BUILTIN_XXSEL_4SI_UNS:
17423 case VSX_BUILTIN_XXSEL_2DI_UNS:
17424 case CRYPTO_BUILTIN_VPERMXOR:
17425 case CRYPTO_BUILTIN_VPERMXOR_V2DI:
17426 case CRYPTO_BUILTIN_VPERMXOR_V4SI:
17427 case CRYPTO_BUILTIN_VPERMXOR_V8HI:
17428 case CRYPTO_BUILTIN_VPERMXOR_V16QI:
17429 case CRYPTO_BUILTIN_VSHASIGMAW:
17430 case CRYPTO_BUILTIN_VSHASIGMAD:
17431 case CRYPTO_BUILTIN_VSHASIGMA:
17432 h.uns_p[0] = 1;
17433 h.uns_p[1] = 1;
17434 h.uns_p[2] = 1;
17435 h.uns_p[3] = 1;
17436 break;
17438 /* signed permute functions with unsigned char mask. */
17439 case ALTIVEC_BUILTIN_VPERM_16QI:
17440 case ALTIVEC_BUILTIN_VPERM_8HI:
17441 case ALTIVEC_BUILTIN_VPERM_4SI:
17442 case ALTIVEC_BUILTIN_VPERM_4SF:
17443 case ALTIVEC_BUILTIN_VPERM_2DI:
17444 case ALTIVEC_BUILTIN_VPERM_2DF:
17445 case VSX_BUILTIN_VPERM_16QI:
17446 case VSX_BUILTIN_VPERM_8HI:
17447 case VSX_BUILTIN_VPERM_4SI:
17448 case VSX_BUILTIN_VPERM_4SF:
17449 case VSX_BUILTIN_VPERM_2DI:
17450 case VSX_BUILTIN_VPERM_2DF:
17451 h.uns_p[3] = 1;
17452 break;
17454 /* unsigned args, signed return. */
17455 case VSX_BUILTIN_XVCVUXDSP:
17456 case VSX_BUILTIN_XVCVUXDDP_UNS:
17457 case ALTIVEC_BUILTIN_UNSFLOAT_V4SI_V4SF:
17458 h.uns_p[1] = 1;
17459 break;
17461 /* signed args, unsigned return. */
17462 case VSX_BUILTIN_XVCVDPUXDS_UNS:
17463 case ALTIVEC_BUILTIN_FIXUNS_V4SF_V4SI:
17464 case MISC_BUILTIN_UNPACK_TD:
17465 case MISC_BUILTIN_UNPACK_V1TI:
17466 h.uns_p[0] = 1;
17467 break;
17469 /* unsigned arguments, bool return (compares). */
17470 case ALTIVEC_BUILTIN_VCMPEQUB:
17471 case ALTIVEC_BUILTIN_VCMPEQUH:
17472 case ALTIVEC_BUILTIN_VCMPEQUW:
17473 case P8V_BUILTIN_VCMPEQUD:
17474 case VSX_BUILTIN_CMPGE_U16QI:
17475 case VSX_BUILTIN_CMPGE_U8HI:
17476 case VSX_BUILTIN_CMPGE_U4SI:
17477 case VSX_BUILTIN_CMPGE_U2DI:
17478 case ALTIVEC_BUILTIN_VCMPGTUB:
17479 case ALTIVEC_BUILTIN_VCMPGTUH:
17480 case ALTIVEC_BUILTIN_VCMPGTUW:
17481 case P8V_BUILTIN_VCMPGTUD:
17482 h.uns_p[1] = 1;
17483 h.uns_p[2] = 1;
17484 break;
17486 /* unsigned arguments for 128-bit pack instructions. */
17487 case MISC_BUILTIN_PACK_TD:
17488 case MISC_BUILTIN_PACK_V1TI:
17489 h.uns_p[1] = 1;
17490 h.uns_p[2] = 1;
17491 break;
17493 /* unsigned second arguments (vector shift right). */
17494 case ALTIVEC_BUILTIN_VSRB:
17495 case ALTIVEC_BUILTIN_VSRH:
17496 case ALTIVEC_BUILTIN_VSRW:
17497 case P8V_BUILTIN_VSRD:
17498 h.uns_p[2] = 1;
17499 break;
17501 default:
17502 break;
17505 /* Figure out how many args are present. */
17506 while (num_args > 0 && h.mode[num_args] == VOIDmode)
17507 num_args--;
17509 ret_type = builtin_mode_to_type[h.mode[0]][h.uns_p[0]];
17510 if (!ret_type && h.uns_p[0])
17511 ret_type = builtin_mode_to_type[h.mode[0]][0];
17513 if (!ret_type)
17514 fatal_error (input_location,
17515 "internal error: builtin function %qs had an unexpected "
17516 "return type %qs", name, GET_MODE_NAME (h.mode[0]));
17518 for (i = 0; i < (int) ARRAY_SIZE (arg_type); i++)
17519 arg_type[i] = NULL_TREE;
17521 for (i = 0; i < num_args; i++)
17523 int m = (int) h.mode[i+1];
17524 int uns_p = h.uns_p[i+1];
17526 arg_type[i] = builtin_mode_to_type[m][uns_p];
17527 if (!arg_type[i] && uns_p)
17528 arg_type[i] = builtin_mode_to_type[m][0];
17530 if (!arg_type[i])
17531 fatal_error (input_location,
17532 "internal error: builtin function %qs, argument %d "
17533 "had unexpected argument type %qs", name, i,
17534 GET_MODE_NAME (m));
17537 builtin_hash_struct **found = builtin_hash_table->find_slot (&h, INSERT);
17538 if (*found == NULL)
17540 h2 = ggc_alloc<builtin_hash_struct> ();
17541 *h2 = h;
17542 *found = h2;
17544 h2->type = build_function_type_list (ret_type, arg_type[0], arg_type[1],
17545 arg_type[2], NULL_TREE);
17548 return (*found)->type;
17551 static void
17552 rs6000_common_init_builtins (void)
17554 const struct builtin_description *d;
17555 size_t i;
17557 tree opaque_ftype_opaque = NULL_TREE;
17558 tree opaque_ftype_opaque_opaque = NULL_TREE;
17559 tree opaque_ftype_opaque_opaque_opaque = NULL_TREE;
17560 HOST_WIDE_INT builtin_mask = rs6000_builtin_mask;
17562 /* Create Altivec and VSX builtins on machines with at least the
17563 general purpose extensions (970 and newer) to allow the use of
17564 the target attribute. */
17566 if (TARGET_EXTRA_BUILTINS)
17567 builtin_mask |= RS6000_BTM_COMMON;
17569 /* Add the ternary operators. */
17570 d = bdesc_3arg;
17571 for (i = 0; i < ARRAY_SIZE (bdesc_3arg); i++, d++)
17573 tree type;
17574 HOST_WIDE_INT mask = d->mask;
17576 if ((mask & builtin_mask) != mask)
17578 if (TARGET_DEBUG_BUILTIN)
17579 fprintf (stderr, "rs6000_builtin, skip ternary %s\n", d->name);
17580 continue;
17583 if (rs6000_overloaded_builtin_p (d->code))
17585 if (! (type = opaque_ftype_opaque_opaque_opaque))
17586 type = opaque_ftype_opaque_opaque_opaque
17587 = build_function_type_list (opaque_V4SI_type_node,
17588 opaque_V4SI_type_node,
17589 opaque_V4SI_type_node,
17590 opaque_V4SI_type_node,
17591 NULL_TREE);
17593 else
17595 enum insn_code icode = d->icode;
17596 if (d->name == 0)
17598 if (TARGET_DEBUG_BUILTIN)
17599 fprintf (stderr, "rs6000_builtin, bdesc_3arg[%ld] no name\n",
17600 (long unsigned)i);
17602 continue;
17605 if (icode == CODE_FOR_nothing)
17607 if (TARGET_DEBUG_BUILTIN)
17608 fprintf (stderr, "rs6000_builtin, skip ternary %s (no code)\n",
17609 d->name);
17611 continue;
17614 type = builtin_function_type (insn_data[icode].operand[0].mode,
17615 insn_data[icode].operand[1].mode,
17616 insn_data[icode].operand[2].mode,
17617 insn_data[icode].operand[3].mode,
17618 d->code, d->name);
17621 def_builtin (d->name, type, d->code);
17624 /* Add the binary operators. */
17625 d = bdesc_2arg;
17626 for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
17628 machine_mode mode0, mode1, mode2;
17629 tree type;
17630 HOST_WIDE_INT mask = d->mask;
17632 if ((mask & builtin_mask) != mask)
17634 if (TARGET_DEBUG_BUILTIN)
17635 fprintf (stderr, "rs6000_builtin, skip binary %s\n", d->name);
17636 continue;
17639 if (rs6000_overloaded_builtin_p (d->code))
17641 if (! (type = opaque_ftype_opaque_opaque))
17642 type = opaque_ftype_opaque_opaque
17643 = build_function_type_list (opaque_V4SI_type_node,
17644 opaque_V4SI_type_node,
17645 opaque_V4SI_type_node,
17646 NULL_TREE);
17648 else
17650 enum insn_code icode = d->icode;
17651 if (d->name == 0)
17653 if (TARGET_DEBUG_BUILTIN)
17654 fprintf (stderr, "rs6000_builtin, bdesc_2arg[%ld] no name\n",
17655 (long unsigned)i);
17657 continue;
17660 if (icode == CODE_FOR_nothing)
17662 if (TARGET_DEBUG_BUILTIN)
17663 fprintf (stderr, "rs6000_builtin, skip binary %s (no code)\n",
17664 d->name);
17666 continue;
17669 mode0 = insn_data[icode].operand[0].mode;
17670 mode1 = insn_data[icode].operand[1].mode;
17671 mode2 = insn_data[icode].operand[2].mode;
17673 type = builtin_function_type (mode0, mode1, mode2, VOIDmode,
17674 d->code, d->name);
17677 def_builtin (d->name, type, d->code);
17680 /* Add the simple unary operators. */
17681 d = bdesc_1arg;
17682 for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
17684 machine_mode mode0, mode1;
17685 tree type;
17686 HOST_WIDE_INT mask = d->mask;
17688 if ((mask & builtin_mask) != mask)
17690 if (TARGET_DEBUG_BUILTIN)
17691 fprintf (stderr, "rs6000_builtin, skip unary %s\n", d->name);
17692 continue;
17695 if (rs6000_overloaded_builtin_p (d->code))
17697 if (! (type = opaque_ftype_opaque))
17698 type = opaque_ftype_opaque
17699 = build_function_type_list (opaque_V4SI_type_node,
17700 opaque_V4SI_type_node,
17701 NULL_TREE);
17703 else
17705 enum insn_code icode = d->icode;
17706 if (d->name == 0)
17708 if (TARGET_DEBUG_BUILTIN)
17709 fprintf (stderr, "rs6000_builtin, bdesc_1arg[%ld] no name\n",
17710 (long unsigned)i);
17712 continue;
17715 if (icode == CODE_FOR_nothing)
17717 if (TARGET_DEBUG_BUILTIN)
17718 fprintf (stderr, "rs6000_builtin, skip unary %s (no code)\n",
17719 d->name);
17721 continue;
17724 mode0 = insn_data[icode].operand[0].mode;
17725 mode1 = insn_data[icode].operand[1].mode;
17727 type = builtin_function_type (mode0, mode1, VOIDmode, VOIDmode,
17728 d->code, d->name);
17731 def_builtin (d->name, type, d->code);
17734 /* Add the simple no-argument operators. */
17735 d = bdesc_0arg;
17736 for (i = 0; i < ARRAY_SIZE (bdesc_0arg); i++, d++)
17738 machine_mode mode0;
17739 tree type;
17740 HOST_WIDE_INT mask = d->mask;
17742 if ((mask & builtin_mask) != mask)
17744 if (TARGET_DEBUG_BUILTIN)
17745 fprintf (stderr, "rs6000_builtin, skip no-argument %s\n", d->name);
17746 continue;
17748 if (rs6000_overloaded_builtin_p (d->code))
17750 if (!opaque_ftype_opaque)
17751 opaque_ftype_opaque
17752 = build_function_type_list (opaque_V4SI_type_node, NULL_TREE);
17753 type = opaque_ftype_opaque;
17755 else
17757 enum insn_code icode = d->icode;
17758 if (d->name == 0)
17760 if (TARGET_DEBUG_BUILTIN)
17761 fprintf (stderr, "rs6000_builtin, bdesc_0arg[%lu] no name\n",
17762 (long unsigned) i);
17763 continue;
17765 if (icode == CODE_FOR_nothing)
17767 if (TARGET_DEBUG_BUILTIN)
17768 fprintf (stderr,
17769 "rs6000_builtin, skip no-argument %s (no code)\n",
17770 d->name);
17771 continue;
17773 mode0 = insn_data[icode].operand[0].mode;
17774 type = builtin_function_type (mode0, VOIDmode, VOIDmode, VOIDmode,
17775 d->code, d->name);
17777 def_builtin (d->name, type, d->code);
17781 /* Set up AIX/Darwin/64-bit Linux quad floating point routines. */
17782 static void
17783 init_float128_ibm (machine_mode mode)
17785 if (!TARGET_XL_COMPAT)
17787 set_optab_libfunc (add_optab, mode, "__gcc_qadd");
17788 set_optab_libfunc (sub_optab, mode, "__gcc_qsub");
17789 set_optab_libfunc (smul_optab, mode, "__gcc_qmul");
17790 set_optab_libfunc (sdiv_optab, mode, "__gcc_qdiv");
17792 if (!TARGET_HARD_FLOAT)
17794 set_optab_libfunc (neg_optab, mode, "__gcc_qneg");
17795 set_optab_libfunc (eq_optab, mode, "__gcc_qeq");
17796 set_optab_libfunc (ne_optab, mode, "__gcc_qne");
17797 set_optab_libfunc (gt_optab, mode, "__gcc_qgt");
17798 set_optab_libfunc (ge_optab, mode, "__gcc_qge");
17799 set_optab_libfunc (lt_optab, mode, "__gcc_qlt");
17800 set_optab_libfunc (le_optab, mode, "__gcc_qle");
17801 set_optab_libfunc (unord_optab, mode, "__gcc_qunord");
17803 set_conv_libfunc (sext_optab, mode, SFmode, "__gcc_stoq");
17804 set_conv_libfunc (sext_optab, mode, DFmode, "__gcc_dtoq");
17805 set_conv_libfunc (trunc_optab, SFmode, mode, "__gcc_qtos");
17806 set_conv_libfunc (trunc_optab, DFmode, mode, "__gcc_qtod");
17807 set_conv_libfunc (sfix_optab, SImode, mode, "__gcc_qtoi");
17808 set_conv_libfunc (ufix_optab, SImode, mode, "__gcc_qtou");
17809 set_conv_libfunc (sfloat_optab, mode, SImode, "__gcc_itoq");
17810 set_conv_libfunc (ufloat_optab, mode, SImode, "__gcc_utoq");
17813 else
17815 set_optab_libfunc (add_optab, mode, "_xlqadd");
17816 set_optab_libfunc (sub_optab, mode, "_xlqsub");
17817 set_optab_libfunc (smul_optab, mode, "_xlqmul");
17818 set_optab_libfunc (sdiv_optab, mode, "_xlqdiv");
17821 /* Add various conversions for IFmode to use the traditional TFmode
17822 names. */
17823 if (mode == IFmode)
17825 set_conv_libfunc (sext_optab, mode, SDmode, "__dpd_extendsdtf");
17826 set_conv_libfunc (sext_optab, mode, DDmode, "__dpd_extendddtf");
17827 set_conv_libfunc (trunc_optab, mode, TDmode, "__dpd_trunctdtf");
17828 set_conv_libfunc (trunc_optab, SDmode, mode, "__dpd_trunctfsd");
17829 set_conv_libfunc (trunc_optab, DDmode, mode, "__dpd_trunctfdd");
17830 set_conv_libfunc (sext_optab, TDmode, mode, "__dpd_extendtftd");
17832 if (TARGET_POWERPC64)
17834 set_conv_libfunc (sfix_optab, TImode, mode, "__fixtfti");
17835 set_conv_libfunc (ufix_optab, TImode, mode, "__fixunstfti");
17836 set_conv_libfunc (sfloat_optab, mode, TImode, "__floattitf");
17837 set_conv_libfunc (ufloat_optab, mode, TImode, "__floatuntitf");
17842 /* Create a decl for either complex long double multiply or complex long double
17843 divide when long double is IEEE 128-bit floating point. We can't use
17844 __multc3 and __divtc3 because the original long double using IBM extended
17845 double used those names. The complex multiply/divide functions are encoded
17846 as builtin functions with a complex result and 4 scalar inputs. */
17848 static void
17849 create_complex_muldiv (const char *name, built_in_function fncode, tree fntype)
17851 tree fndecl = add_builtin_function (name, fntype, fncode, BUILT_IN_NORMAL,
17852 name, NULL_TREE);
17854 set_builtin_decl (fncode, fndecl, true);
17856 if (TARGET_DEBUG_BUILTIN)
17857 fprintf (stderr, "create complex %s, fncode: %d\n", name, (int) fncode);
17859 return;
17862 /* Set up IEEE 128-bit floating point routines. Use different names if the
17863 arguments can be passed in a vector register. The historical PowerPC
17864 implementation of IEEE 128-bit floating point used _q_<op> for the names, so
17865 continue to use that if we aren't using vector registers to pass IEEE
17866 128-bit floating point. */
17868 static void
17869 init_float128_ieee (machine_mode mode)
17871 if (FLOAT128_VECTOR_P (mode))
17873 static bool complex_muldiv_init_p = false;
17875 /* Set up to call __mulkc3 and __divkc3 under -mabi=ieeelongdouble. If
17876 we have clone or target attributes, this will be called a second
17877 time. We want to create the built-in function only once. */
17878 if (mode == TFmode && TARGET_IEEEQUAD && !complex_muldiv_init_p)
17880 complex_muldiv_init_p = true;
17881 built_in_function fncode_mul =
17882 (built_in_function) (BUILT_IN_COMPLEX_MUL_MIN + TCmode
17883 - MIN_MODE_COMPLEX_FLOAT);
17884 built_in_function fncode_div =
17885 (built_in_function) (BUILT_IN_COMPLEX_DIV_MIN + TCmode
17886 - MIN_MODE_COMPLEX_FLOAT);
17888 tree fntype = build_function_type_list (complex_long_double_type_node,
17889 long_double_type_node,
17890 long_double_type_node,
17891 long_double_type_node,
17892 long_double_type_node,
17893 NULL_TREE);
17895 create_complex_muldiv ("__mulkc3", fncode_mul, fntype);
17896 create_complex_muldiv ("__divkc3", fncode_div, fntype);
17899 set_optab_libfunc (add_optab, mode, "__addkf3");
17900 set_optab_libfunc (sub_optab, mode, "__subkf3");
17901 set_optab_libfunc (neg_optab, mode, "__negkf2");
17902 set_optab_libfunc (smul_optab, mode, "__mulkf3");
17903 set_optab_libfunc (sdiv_optab, mode, "__divkf3");
17904 set_optab_libfunc (sqrt_optab, mode, "__sqrtkf2");
17905 set_optab_libfunc (abs_optab, mode, "__abskf2");
17906 set_optab_libfunc (powi_optab, mode, "__powikf2");
17908 set_optab_libfunc (eq_optab, mode, "__eqkf2");
17909 set_optab_libfunc (ne_optab, mode, "__nekf2");
17910 set_optab_libfunc (gt_optab, mode, "__gtkf2");
17911 set_optab_libfunc (ge_optab, mode, "__gekf2");
17912 set_optab_libfunc (lt_optab, mode, "__ltkf2");
17913 set_optab_libfunc (le_optab, mode, "__lekf2");
17914 set_optab_libfunc (unord_optab, mode, "__unordkf2");
17916 set_conv_libfunc (sext_optab, mode, SFmode, "__extendsfkf2");
17917 set_conv_libfunc (sext_optab, mode, DFmode, "__extenddfkf2");
17918 set_conv_libfunc (trunc_optab, SFmode, mode, "__trunckfsf2");
17919 set_conv_libfunc (trunc_optab, DFmode, mode, "__trunckfdf2");
17921 set_conv_libfunc (sext_optab, mode, IFmode, "__trunctfkf2");
17922 if (mode != TFmode && FLOAT128_IBM_P (TFmode))
17923 set_conv_libfunc (sext_optab, mode, TFmode, "__trunctfkf2");
17925 set_conv_libfunc (trunc_optab, IFmode, mode, "__extendkftf2");
17926 if (mode != TFmode && FLOAT128_IBM_P (TFmode))
17927 set_conv_libfunc (trunc_optab, TFmode, mode, "__extendkftf2");
17929 set_conv_libfunc (sext_optab, mode, SDmode, "__dpd_extendsdkf");
17930 set_conv_libfunc (sext_optab, mode, DDmode, "__dpd_extendddkf");
17931 set_conv_libfunc (trunc_optab, mode, TDmode, "__dpd_trunctdkf");
17932 set_conv_libfunc (trunc_optab, SDmode, mode, "__dpd_trunckfsd");
17933 set_conv_libfunc (trunc_optab, DDmode, mode, "__dpd_trunckfdd");
17934 set_conv_libfunc (sext_optab, TDmode, mode, "__dpd_extendkftd");
17936 set_conv_libfunc (sfix_optab, SImode, mode, "__fixkfsi");
17937 set_conv_libfunc (ufix_optab, SImode, mode, "__fixunskfsi");
17938 set_conv_libfunc (sfix_optab, DImode, mode, "__fixkfdi");
17939 set_conv_libfunc (ufix_optab, DImode, mode, "__fixunskfdi");
17941 set_conv_libfunc (sfloat_optab, mode, SImode, "__floatsikf");
17942 set_conv_libfunc (ufloat_optab, mode, SImode, "__floatunsikf");
17943 set_conv_libfunc (sfloat_optab, mode, DImode, "__floatdikf");
17944 set_conv_libfunc (ufloat_optab, mode, DImode, "__floatundikf");
17946 if (TARGET_POWERPC64)
17948 set_conv_libfunc (sfix_optab, TImode, mode, "__fixkfti");
17949 set_conv_libfunc (ufix_optab, TImode, mode, "__fixunskfti");
17950 set_conv_libfunc (sfloat_optab, mode, TImode, "__floattikf");
17951 set_conv_libfunc (ufloat_optab, mode, TImode, "__floatuntikf");
17955 else
17957 set_optab_libfunc (add_optab, mode, "_q_add");
17958 set_optab_libfunc (sub_optab, mode, "_q_sub");
17959 set_optab_libfunc (neg_optab, mode, "_q_neg");
17960 set_optab_libfunc (smul_optab, mode, "_q_mul");
17961 set_optab_libfunc (sdiv_optab, mode, "_q_div");
17962 if (TARGET_PPC_GPOPT)
17963 set_optab_libfunc (sqrt_optab, mode, "_q_sqrt");
17965 set_optab_libfunc (eq_optab, mode, "_q_feq");
17966 set_optab_libfunc (ne_optab, mode, "_q_fne");
17967 set_optab_libfunc (gt_optab, mode, "_q_fgt");
17968 set_optab_libfunc (ge_optab, mode, "_q_fge");
17969 set_optab_libfunc (lt_optab, mode, "_q_flt");
17970 set_optab_libfunc (le_optab, mode, "_q_fle");
17972 set_conv_libfunc (sext_optab, mode, SFmode, "_q_stoq");
17973 set_conv_libfunc (sext_optab, mode, DFmode, "_q_dtoq");
17974 set_conv_libfunc (trunc_optab, SFmode, mode, "_q_qtos");
17975 set_conv_libfunc (trunc_optab, DFmode, mode, "_q_qtod");
17976 set_conv_libfunc (sfix_optab, SImode, mode, "_q_qtoi");
17977 set_conv_libfunc (ufix_optab, SImode, mode, "_q_qtou");
17978 set_conv_libfunc (sfloat_optab, mode, SImode, "_q_itoq");
17979 set_conv_libfunc (ufloat_optab, mode, SImode, "_q_utoq");
17983 static void
17984 rs6000_init_libfuncs (void)
17986 /* __float128 support. */
17987 if (TARGET_FLOAT128_TYPE)
17989 init_float128_ibm (IFmode);
17990 init_float128_ieee (KFmode);
17993 /* AIX/Darwin/64-bit Linux quad floating point routines. */
17994 if (TARGET_LONG_DOUBLE_128)
17996 if (!TARGET_IEEEQUAD)
17997 init_float128_ibm (TFmode);
17999 /* IEEE 128-bit including 32-bit SVR4 quad floating point routines. */
18000 else
18001 init_float128_ieee (TFmode);
18005 /* Emit a potentially record-form instruction, setting DST from SRC.
18006 If DOT is 0, that is all; otherwise, set CCREG to the result of the
18007 signed comparison of DST with zero. If DOT is 1, the generated RTL
18008 doesn't care about the DST result; if DOT is 2, it does. If CCREG
18009 is CR0 do a single dot insn (as a PARALLEL); otherwise, do a SET and
18010 a separate COMPARE. */
18012 void
18013 rs6000_emit_dot_insn (rtx dst, rtx src, int dot, rtx ccreg)
18015 if (dot == 0)
18017 emit_move_insn (dst, src);
18018 return;
18021 if (cc_reg_not_cr0_operand (ccreg, CCmode))
18023 emit_move_insn (dst, src);
18024 emit_move_insn (ccreg, gen_rtx_COMPARE (CCmode, dst, const0_rtx));
18025 return;
18028 rtx ccset = gen_rtx_SET (ccreg, gen_rtx_COMPARE (CCmode, src, const0_rtx));
18029 if (dot == 1)
18031 rtx clobber = gen_rtx_CLOBBER (VOIDmode, dst);
18032 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, ccset, clobber)));
18034 else
18036 rtx set = gen_rtx_SET (dst, src);
18037 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, ccset, set)));
18042 /* A validation routine: say whether CODE, a condition code, and MODE
18043 match. The other alternatives either don't make sense or should
18044 never be generated. */
18046 void
18047 validate_condition_mode (enum rtx_code code, machine_mode mode)
18049 gcc_assert ((GET_RTX_CLASS (code) == RTX_COMPARE
18050 || GET_RTX_CLASS (code) == RTX_COMM_COMPARE)
18051 && GET_MODE_CLASS (mode) == MODE_CC);
18053 /* These don't make sense. */
18054 gcc_assert ((code != GT && code != LT && code != GE && code != LE)
18055 || mode != CCUNSmode);
18057 gcc_assert ((code != GTU && code != LTU && code != GEU && code != LEU)
18058 || mode == CCUNSmode);
18060 gcc_assert (mode == CCFPmode
18061 || (code != ORDERED && code != UNORDERED
18062 && code != UNEQ && code != LTGT
18063 && code != UNGT && code != UNLT
18064 && code != UNGE && code != UNLE));
18066 /* These should never be generated except for
18067 flag_finite_math_only. */
18068 gcc_assert (mode != CCFPmode
18069 || flag_finite_math_only
18070 || (code != LE && code != GE
18071 && code != UNEQ && code != LTGT
18072 && code != UNGT && code != UNLT));
18074 /* These are invalid; the information is not there. */
18075 gcc_assert (mode != CCEQmode || code == EQ || code == NE);
18079 /* Return whether MASK (a CONST_INT) is a valid mask for any rlwinm,
18080 rldicl, rldicr, or rldic instruction in mode MODE. If so, if E is
18081 not zero, store there the bit offset (counted from the right) where
18082 the single stretch of 1 bits begins; and similarly for B, the bit
18083 offset where it ends. */
18085 bool
18086 rs6000_is_valid_mask (rtx mask, int *b, int *e, machine_mode mode)
18088 unsigned HOST_WIDE_INT val = INTVAL (mask);
18089 unsigned HOST_WIDE_INT bit;
18090 int nb, ne;
18091 int n = GET_MODE_PRECISION (mode);
18093 if (mode != DImode && mode != SImode)
18094 return false;
18096 if (INTVAL (mask) >= 0)
18098 bit = val & -val;
18099 ne = exact_log2 (bit);
18100 nb = exact_log2 (val + bit);
18102 else if (val + 1 == 0)
18104 nb = n;
18105 ne = 0;
18107 else if (val & 1)
18109 val = ~val;
18110 bit = val & -val;
18111 nb = exact_log2 (bit);
18112 ne = exact_log2 (val + bit);
18114 else
18116 bit = val & -val;
18117 ne = exact_log2 (bit);
18118 if (val + bit == 0)
18119 nb = n;
18120 else
18121 nb = 0;
18124 nb--;
18126 if (nb < 0 || ne < 0 || nb >= n || ne >= n)
18127 return false;
18129 if (b)
18130 *b = nb;
18131 if (e)
18132 *e = ne;
18134 return true;
18137 /* Return whether MASK (a CONST_INT) is a valid mask for any rlwinm, rldicl,
18138 or rldicr instruction, to implement an AND with it in mode MODE. */
18140 bool
18141 rs6000_is_valid_and_mask (rtx mask, machine_mode mode)
18143 int nb, ne;
18145 if (!rs6000_is_valid_mask (mask, &nb, &ne, mode))
18146 return false;
18148 /* For DImode, we need a rldicl, rldicr, or a rlwinm with mask that
18149 does not wrap. */
18150 if (mode == DImode)
18151 return (ne == 0 || nb == 63 || (nb < 32 && ne <= nb));
18153 /* For SImode, rlwinm can do everything. */
18154 if (mode == SImode)
18155 return (nb < 32 && ne < 32);
18157 return false;
18160 /* Return the instruction template for an AND with mask in mode MODE, with
18161 operands OPERANDS. If DOT is true, make it a record-form instruction. */
18163 const char *
18164 rs6000_insn_for_and_mask (machine_mode mode, rtx *operands, bool dot)
18166 int nb, ne;
18168 if (!rs6000_is_valid_mask (operands[2], &nb, &ne, mode))
18169 gcc_unreachable ();
18171 if (mode == DImode && ne == 0)
18173 operands[3] = GEN_INT (63 - nb);
18174 if (dot)
18175 return "rldicl. %0,%1,0,%3";
18176 return "rldicl %0,%1,0,%3";
18179 if (mode == DImode && nb == 63)
18181 operands[3] = GEN_INT (63 - ne);
18182 if (dot)
18183 return "rldicr. %0,%1,0,%3";
18184 return "rldicr %0,%1,0,%3";
18187 if (nb < 32 && ne < 32)
18189 operands[3] = GEN_INT (31 - nb);
18190 operands[4] = GEN_INT (31 - ne);
18191 if (dot)
18192 return "rlwinm. %0,%1,0,%3,%4";
18193 return "rlwinm %0,%1,0,%3,%4";
18196 gcc_unreachable ();
18199 /* Return whether MASK (a CONST_INT) is a valid mask for any rlw[i]nm,
18200 rld[i]cl, rld[i]cr, or rld[i]c instruction, to implement an AND with
18201 shift SHIFT (a ROTATE, ASHIFT, or LSHIFTRT) in mode MODE. */
18203 bool
18204 rs6000_is_valid_shift_mask (rtx mask, rtx shift, machine_mode mode)
18206 int nb, ne;
18208 if (!rs6000_is_valid_mask (mask, &nb, &ne, mode))
18209 return false;
18211 int n = GET_MODE_PRECISION (mode);
18212 int sh = -1;
18214 if (CONST_INT_P (XEXP (shift, 1)))
18216 sh = INTVAL (XEXP (shift, 1));
18217 if (sh < 0 || sh >= n)
18218 return false;
18221 rtx_code code = GET_CODE (shift);
18223 /* Convert any shift by 0 to a rotate, to simplify below code. */
18224 if (sh == 0)
18225 code = ROTATE;
18227 /* Convert rotate to simple shift if we can, to make analysis simpler. */
18228 if (code == ROTATE && sh >= 0 && nb >= ne && ne >= sh)
18229 code = ASHIFT;
18230 if (code == ROTATE && sh >= 0 && nb >= ne && nb < sh)
18232 code = LSHIFTRT;
18233 sh = n - sh;
18236 /* DImode rotates need rld*. */
18237 if (mode == DImode && code == ROTATE)
18238 return (nb == 63 || ne == 0 || ne == sh);
18240 /* SImode rotates need rlw*. */
18241 if (mode == SImode && code == ROTATE)
18242 return (nb < 32 && ne < 32 && sh < 32);
18244 /* Wrap-around masks are only okay for rotates. */
18245 if (ne > nb)
18246 return false;
18248 /* Variable shifts are only okay for rotates. */
18249 if (sh < 0)
18250 return false;
18252 /* Don't allow ASHIFT if the mask is wrong for that. */
18253 if (code == ASHIFT && ne < sh)
18254 return false;
18256 /* If we can do it with an rlw*, we can do it. Don't allow LSHIFTRT
18257 if the mask is wrong for that. */
18258 if (nb < 32 && ne < 32 && sh < 32
18259 && !(code == LSHIFTRT && nb >= 32 - sh))
18260 return true;
18262 /* If we can do it with an rld*, we can do it. Don't allow LSHIFTRT
18263 if the mask is wrong for that. */
18264 if (code == LSHIFTRT)
18265 sh = 64 - sh;
18266 if (nb == 63 || ne == 0 || ne == sh)
18267 return !(code == LSHIFTRT && nb >= sh);
18269 return false;
18272 /* Return the instruction template for a shift with mask in mode MODE, with
18273 operands OPERANDS. If DOT is true, make it a record-form instruction. */
18275 const char *
18276 rs6000_insn_for_shift_mask (machine_mode mode, rtx *operands, bool dot)
18278 int nb, ne;
18280 if (!rs6000_is_valid_mask (operands[3], &nb, &ne, mode))
18281 gcc_unreachable ();
18283 if (mode == DImode && ne == 0)
18285 if (GET_CODE (operands[4]) == LSHIFTRT && INTVAL (operands[2]))
18286 operands[2] = GEN_INT (64 - INTVAL (operands[2]));
18287 operands[3] = GEN_INT (63 - nb);
18288 if (dot)
18289 return "rld%I2cl. %0,%1,%2,%3";
18290 return "rld%I2cl %0,%1,%2,%3";
18293 if (mode == DImode && nb == 63)
18295 operands[3] = GEN_INT (63 - ne);
18296 if (dot)
18297 return "rld%I2cr. %0,%1,%2,%3";
18298 return "rld%I2cr %0,%1,%2,%3";
18301 if (mode == DImode
18302 && GET_CODE (operands[4]) != LSHIFTRT
18303 && CONST_INT_P (operands[2])
18304 && ne == INTVAL (operands[2]))
18306 operands[3] = GEN_INT (63 - nb);
18307 if (dot)
18308 return "rld%I2c. %0,%1,%2,%3";
18309 return "rld%I2c %0,%1,%2,%3";
18312 if (nb < 32 && ne < 32)
18314 if (GET_CODE (operands[4]) == LSHIFTRT && INTVAL (operands[2]))
18315 operands[2] = GEN_INT (32 - INTVAL (operands[2]));
18316 operands[3] = GEN_INT (31 - nb);
18317 operands[4] = GEN_INT (31 - ne);
18318 /* This insn can also be a 64-bit rotate with mask that really makes
18319 it just a shift right (with mask); the %h below are to adjust for
18320 that situation (shift count is >= 32 in that case). */
18321 if (dot)
18322 return "rlw%I2nm. %0,%1,%h2,%3,%4";
18323 return "rlw%I2nm %0,%1,%h2,%3,%4";
18326 gcc_unreachable ();
18329 /* Return whether MASK (a CONST_INT) is a valid mask for any rlwimi or
18330 rldimi instruction, to implement an insert with shift SHIFT (a ROTATE,
18331 ASHIFT, or LSHIFTRT) in mode MODE. */
18333 bool
18334 rs6000_is_valid_insert_mask (rtx mask, rtx shift, machine_mode mode)
18336 int nb, ne;
18338 if (!rs6000_is_valid_mask (mask, &nb, &ne, mode))
18339 return false;
18341 int n = GET_MODE_PRECISION (mode);
18343 int sh = INTVAL (XEXP (shift, 1));
18344 if (sh < 0 || sh >= n)
18345 return false;
18347 rtx_code code = GET_CODE (shift);
18349 /* Convert any shift by 0 to a rotate, to simplify below code. */
18350 if (sh == 0)
18351 code = ROTATE;
18353 /* Convert rotate to simple shift if we can, to make analysis simpler. */
18354 if (code == ROTATE && sh >= 0 && nb >= ne && ne >= sh)
18355 code = ASHIFT;
18356 if (code == ROTATE && sh >= 0 && nb >= ne && nb < sh)
18358 code = LSHIFTRT;
18359 sh = n - sh;
18362 /* DImode rotates need rldimi. */
18363 if (mode == DImode && code == ROTATE)
18364 return (ne == sh);
18366 /* SImode rotates need rlwimi. */
18367 if (mode == SImode && code == ROTATE)
18368 return (nb < 32 && ne < 32 && sh < 32);
18370 /* Wrap-around masks are only okay for rotates. */
18371 if (ne > nb)
18372 return false;
18374 /* Don't allow ASHIFT if the mask is wrong for that. */
18375 if (code == ASHIFT && ne < sh)
18376 return false;
18378 /* If we can do it with an rlwimi, we can do it. Don't allow LSHIFTRT
18379 if the mask is wrong for that. */
18380 if (nb < 32 && ne < 32 && sh < 32
18381 && !(code == LSHIFTRT && nb >= 32 - sh))
18382 return true;
18384 /* If we can do it with an rldimi, we can do it. Don't allow LSHIFTRT
18385 if the mask is wrong for that. */
18386 if (code == LSHIFTRT)
18387 sh = 64 - sh;
18388 if (ne == sh)
18389 return !(code == LSHIFTRT && nb >= sh);
18391 return false;
18394 /* Return the instruction template for an insert with mask in mode MODE, with
18395 operands OPERANDS. If DOT is true, make it a record-form instruction. */
18397 const char *
18398 rs6000_insn_for_insert_mask (machine_mode mode, rtx *operands, bool dot)
18400 int nb, ne;
18402 if (!rs6000_is_valid_mask (operands[3], &nb, &ne, mode))
18403 gcc_unreachable ();
18405 /* Prefer rldimi because rlwimi is cracked. */
18406 if (TARGET_POWERPC64
18407 && (!dot || mode == DImode)
18408 && GET_CODE (operands[4]) != LSHIFTRT
18409 && ne == INTVAL (operands[2]))
18411 operands[3] = GEN_INT (63 - nb);
18412 if (dot)
18413 return "rldimi. %0,%1,%2,%3";
18414 return "rldimi %0,%1,%2,%3";
18417 if (nb < 32 && ne < 32)
18419 if (GET_CODE (operands[4]) == LSHIFTRT && INTVAL (operands[2]))
18420 operands[2] = GEN_INT (32 - INTVAL (operands[2]));
18421 operands[3] = GEN_INT (31 - nb);
18422 operands[4] = GEN_INT (31 - ne);
18423 if (dot)
18424 return "rlwimi. %0,%1,%2,%3,%4";
18425 return "rlwimi %0,%1,%2,%3,%4";
18428 gcc_unreachable ();
18431 /* Return whether an AND with C (a CONST_INT) in mode MODE can be done
18432 using two machine instructions. */
18434 bool
18435 rs6000_is_valid_2insn_and (rtx c, machine_mode mode)
18437 /* There are two kinds of AND we can handle with two insns:
18438 1) those we can do with two rl* insn;
18439 2) ori[s];xori[s].
18441 We do not handle that last case yet. */
18443 /* If there is just one stretch of ones, we can do it. */
18444 if (rs6000_is_valid_mask (c, NULL, NULL, mode))
18445 return true;
18447 /* Otherwise, fill in the lowest "hole"; if we can do the result with
18448 one insn, we can do the whole thing with two. */
18449 unsigned HOST_WIDE_INT val = INTVAL (c);
18450 unsigned HOST_WIDE_INT bit1 = val & -val;
18451 unsigned HOST_WIDE_INT bit2 = (val + bit1) & ~val;
18452 unsigned HOST_WIDE_INT val1 = (val + bit1) & val;
18453 unsigned HOST_WIDE_INT bit3 = val1 & -val1;
18454 return rs6000_is_valid_and_mask (GEN_INT (val + bit3 - bit2), mode);
18457 /* Emit the two insns to do an AND in mode MODE, with operands OPERANDS.
18458 If EXPAND is true, split rotate-and-mask instructions we generate to
18459 their constituent parts as well (this is used during expand); if DOT
18460 is 1, make the last insn a record-form instruction clobbering the
18461 destination GPR and setting the CC reg (from operands[3]); if 2, set
18462 that GPR as well as the CC reg. */
18464 void
18465 rs6000_emit_2insn_and (machine_mode mode, rtx *operands, bool expand, int dot)
18467 gcc_assert (!(expand && dot));
18469 unsigned HOST_WIDE_INT val = INTVAL (operands[2]);
18471 /* If it is one stretch of ones, it is DImode; shift left, mask, then
18472 shift right. This generates better code than doing the masks without
18473 shifts, or shifting first right and then left. */
18474 int nb, ne;
18475 if (rs6000_is_valid_mask (operands[2], &nb, &ne, mode) && nb >= ne)
18477 gcc_assert (mode == DImode);
18479 int shift = 63 - nb;
18480 if (expand)
18482 rtx tmp1 = gen_reg_rtx (DImode);
18483 rtx tmp2 = gen_reg_rtx (DImode);
18484 emit_insn (gen_ashldi3 (tmp1, operands[1], GEN_INT (shift)));
18485 emit_insn (gen_anddi3 (tmp2, tmp1, GEN_INT (val << shift)));
18486 emit_insn (gen_lshrdi3 (operands[0], tmp2, GEN_INT (shift)));
18488 else
18490 rtx tmp = gen_rtx_ASHIFT (mode, operands[1], GEN_INT (shift));
18491 tmp = gen_rtx_AND (mode, tmp, GEN_INT (val << shift));
18492 emit_move_insn (operands[0], tmp);
18493 tmp = gen_rtx_LSHIFTRT (mode, operands[0], GEN_INT (shift));
18494 rs6000_emit_dot_insn (operands[0], tmp, dot, dot ? operands[3] : 0);
18496 return;
18499 /* Otherwise, make a mask2 that cuts out the lowest "hole", and a mask1
18500 that does the rest. */
18501 unsigned HOST_WIDE_INT bit1 = val & -val;
18502 unsigned HOST_WIDE_INT bit2 = (val + bit1) & ~val;
18503 unsigned HOST_WIDE_INT val1 = (val + bit1) & val;
18504 unsigned HOST_WIDE_INT bit3 = val1 & -val1;
18506 unsigned HOST_WIDE_INT mask1 = -bit3 + bit2 - 1;
18507 unsigned HOST_WIDE_INT mask2 = val + bit3 - bit2;
18509 gcc_assert (rs6000_is_valid_and_mask (GEN_INT (mask2), mode));
18511 /* Two "no-rotate"-and-mask instructions, for SImode. */
18512 if (rs6000_is_valid_and_mask (GEN_INT (mask1), mode))
18514 gcc_assert (mode == SImode);
18516 rtx reg = expand ? gen_reg_rtx (mode) : operands[0];
18517 rtx tmp = gen_rtx_AND (mode, operands[1], GEN_INT (mask1));
18518 emit_move_insn (reg, tmp);
18519 tmp = gen_rtx_AND (mode, reg, GEN_INT (mask2));
18520 rs6000_emit_dot_insn (operands[0], tmp, dot, dot ? operands[3] : 0);
18521 return;
18524 gcc_assert (mode == DImode);
18526 /* Two "no-rotate"-and-mask instructions, for DImode: both are rlwinm
18527 insns; we have to do the first in SImode, because it wraps. */
18528 if (mask2 <= 0xffffffff
18529 && rs6000_is_valid_and_mask (GEN_INT (mask1), SImode))
18531 rtx reg = expand ? gen_reg_rtx (mode) : operands[0];
18532 rtx tmp = gen_rtx_AND (SImode, gen_lowpart (SImode, operands[1]),
18533 GEN_INT (mask1));
18534 rtx reg_low = gen_lowpart (SImode, reg);
18535 emit_move_insn (reg_low, tmp);
18536 tmp = gen_rtx_AND (mode, reg, GEN_INT (mask2));
18537 rs6000_emit_dot_insn (operands[0], tmp, dot, dot ? operands[3] : 0);
18538 return;
18541 /* Two rld* insns: rotate, clear the hole in the middle (which now is
18542 at the top end), rotate back and clear the other hole. */
18543 int right = exact_log2 (bit3);
18544 int left = 64 - right;
18546 /* Rotate the mask too. */
18547 mask1 = (mask1 >> right) | ((bit2 - 1) << left);
18549 if (expand)
18551 rtx tmp1 = gen_reg_rtx (DImode);
18552 rtx tmp2 = gen_reg_rtx (DImode);
18553 rtx tmp3 = gen_reg_rtx (DImode);
18554 emit_insn (gen_rotldi3 (tmp1, operands[1], GEN_INT (left)));
18555 emit_insn (gen_anddi3 (tmp2, tmp1, GEN_INT (mask1)));
18556 emit_insn (gen_rotldi3 (tmp3, tmp2, GEN_INT (right)));
18557 emit_insn (gen_anddi3 (operands[0], tmp3, GEN_INT (mask2)));
18559 else
18561 rtx tmp = gen_rtx_ROTATE (mode, operands[1], GEN_INT (left));
18562 tmp = gen_rtx_AND (mode, tmp, GEN_INT (mask1));
18563 emit_move_insn (operands[0], tmp);
18564 tmp = gen_rtx_ROTATE (mode, operands[0], GEN_INT (right));
18565 tmp = gen_rtx_AND (mode, tmp, GEN_INT (mask2));
18566 rs6000_emit_dot_insn (operands[0], tmp, dot, dot ? operands[3] : 0);
18570 /* Return 1 if REGNO (reg1) == REGNO (reg2) - 1 making them candidates
18571 for lfq and stfq insns iff the registers are hard registers. */
18574 registers_ok_for_quad_peep (rtx reg1, rtx reg2)
18576 /* We might have been passed a SUBREG. */
18577 if (!REG_P (reg1) || !REG_P (reg2))
18578 return 0;
18580 /* We might have been passed non floating point registers. */
18581 if (!FP_REGNO_P (REGNO (reg1))
18582 || !FP_REGNO_P (REGNO (reg2)))
18583 return 0;
18585 return (REGNO (reg1) == REGNO (reg2) - 1);
18588 /* Return 1 if addr1 and addr2 are suitable for lfq or stfq insn.
18589 addr1 and addr2 must be in consecutive memory locations
18590 (addr2 == addr1 + 8). */
18593 mems_ok_for_quad_peep (rtx mem1, rtx mem2)
18595 rtx addr1, addr2;
18596 unsigned int reg1, reg2;
18597 int offset1, offset2;
18599 /* The mems cannot be volatile. */
18600 if (MEM_VOLATILE_P (mem1) || MEM_VOLATILE_P (mem2))
18601 return 0;
18603 addr1 = XEXP (mem1, 0);
18604 addr2 = XEXP (mem2, 0);
18606 /* Extract an offset (if used) from the first addr. */
18607 if (GET_CODE (addr1) == PLUS)
18609 /* If not a REG, return zero. */
18610 if (!REG_P (XEXP (addr1, 0)))
18611 return 0;
18612 else
18614 reg1 = REGNO (XEXP (addr1, 0));
18615 /* The offset must be constant! */
18616 if (!CONST_INT_P (XEXP (addr1, 1)))
18617 return 0;
18618 offset1 = INTVAL (XEXP (addr1, 1));
18621 else if (!REG_P (addr1))
18622 return 0;
18623 else
18625 reg1 = REGNO (addr1);
18626 /* This was a simple (mem (reg)) expression. Offset is 0. */
18627 offset1 = 0;
18630 /* And now for the second addr. */
18631 if (GET_CODE (addr2) == PLUS)
18633 /* If not a REG, return zero. */
18634 if (!REG_P (XEXP (addr2, 0)))
18635 return 0;
18636 else
18638 reg2 = REGNO (XEXP (addr2, 0));
18639 /* The offset must be constant. */
18640 if (!CONST_INT_P (XEXP (addr2, 1)))
18641 return 0;
18642 offset2 = INTVAL (XEXP (addr2, 1));
18645 else if (!REG_P (addr2))
18646 return 0;
18647 else
18649 reg2 = REGNO (addr2);
18650 /* This was a simple (mem (reg)) expression. Offset is 0. */
18651 offset2 = 0;
18654 /* Both of these must have the same base register. */
18655 if (reg1 != reg2)
18656 return 0;
18658 /* The offset for the second addr must be 8 more than the first addr. */
18659 if (offset2 != offset1 + 8)
18660 return 0;
18662 /* All the tests passed. addr1 and addr2 are valid for lfq or stfq
18663 instructions. */
18664 return 1;
18667 /* Implement TARGET_SECONDARY_RELOAD_NEEDED_MODE. For SDmode values we
18668 need to use DDmode, in all other cases we can use the same mode. */
18669 static machine_mode
18670 rs6000_secondary_memory_needed_mode (machine_mode mode)
18672 if (lra_in_progress && mode == SDmode)
18673 return DDmode;
18674 return mode;
18677 /* Classify a register type. Because the FMRGOW/FMRGEW instructions only work
18678 on traditional floating point registers, and the VMRGOW/VMRGEW instructions
18679 only work on the traditional altivec registers, note if an altivec register
18680 was chosen. */
18682 static enum rs6000_reg_type
18683 register_to_reg_type (rtx reg, bool *is_altivec)
18685 HOST_WIDE_INT regno;
18686 enum reg_class rclass;
18688 if (SUBREG_P (reg))
18689 reg = SUBREG_REG (reg);
18691 if (!REG_P (reg))
18692 return NO_REG_TYPE;
18694 regno = REGNO (reg);
18695 if (!HARD_REGISTER_NUM_P (regno))
18697 if (!lra_in_progress && !reload_completed)
18698 return PSEUDO_REG_TYPE;
18700 regno = true_regnum (reg);
18701 if (regno < 0 || !HARD_REGISTER_NUM_P (regno))
18702 return PSEUDO_REG_TYPE;
18705 gcc_assert (regno >= 0);
18707 if (is_altivec && ALTIVEC_REGNO_P (regno))
18708 *is_altivec = true;
18710 rclass = rs6000_regno_regclass[regno];
18711 return reg_class_to_reg_type[(int)rclass];
18714 /* Helper function to return the cost of adding a TOC entry address. */
18716 static inline int
18717 rs6000_secondary_reload_toc_costs (addr_mask_type addr_mask)
18719 int ret;
18721 if (TARGET_CMODEL != CMODEL_SMALL)
18722 ret = ((addr_mask & RELOAD_REG_OFFSET) == 0) ? 1 : 2;
18724 else
18725 ret = (TARGET_MINIMAL_TOC) ? 6 : 3;
18727 return ret;
18730 /* Helper function for rs6000_secondary_reload to determine whether the memory
18731 address (ADDR) with a given register class (RCLASS) and machine mode (MODE)
18732 needs reloading. Return negative if the memory is not handled by the memory
18733 helper functions and to try a different reload method, 0 if no additional
18734 instructions are need, and positive to give the extra cost for the
18735 memory. */
18737 static int
18738 rs6000_secondary_reload_memory (rtx addr,
18739 enum reg_class rclass,
18740 machine_mode mode)
18742 int extra_cost = 0;
18743 rtx reg, and_arg, plus_arg0, plus_arg1;
18744 addr_mask_type addr_mask;
18745 const char *type = NULL;
18746 const char *fail_msg = NULL;
18748 if (GPR_REG_CLASS_P (rclass))
18749 addr_mask = reg_addr[mode].addr_mask[RELOAD_REG_GPR];
18751 else if (rclass == FLOAT_REGS)
18752 addr_mask = reg_addr[mode].addr_mask[RELOAD_REG_FPR];
18754 else if (rclass == ALTIVEC_REGS)
18755 addr_mask = reg_addr[mode].addr_mask[RELOAD_REG_VMX];
18757 /* For the combined VSX_REGS, turn off Altivec AND -16. */
18758 else if (rclass == VSX_REGS)
18759 addr_mask = (reg_addr[mode].addr_mask[RELOAD_REG_VMX]
18760 & ~RELOAD_REG_AND_M16);
18762 /* If the register allocator hasn't made up its mind yet on the register
18763 class to use, settle on defaults to use. */
18764 else if (rclass == NO_REGS)
18766 addr_mask = (reg_addr[mode].addr_mask[RELOAD_REG_ANY]
18767 & ~RELOAD_REG_AND_M16);
18769 if ((addr_mask & RELOAD_REG_MULTIPLE) != 0)
18770 addr_mask &= ~(RELOAD_REG_INDEXED
18771 | RELOAD_REG_PRE_INCDEC
18772 | RELOAD_REG_PRE_MODIFY);
18775 else
18776 addr_mask = 0;
18778 /* If the register isn't valid in this register class, just return now. */
18779 if ((addr_mask & RELOAD_REG_VALID) == 0)
18781 if (TARGET_DEBUG_ADDR)
18783 fprintf (stderr,
18784 "rs6000_secondary_reload_memory: mode = %s, class = %s, "
18785 "not valid in class\n",
18786 GET_MODE_NAME (mode), reg_class_names[rclass]);
18787 debug_rtx (addr);
18790 return -1;
18793 switch (GET_CODE (addr))
18795 /* Does the register class supports auto update forms for this mode? We
18796 don't need a scratch register, since the powerpc only supports
18797 PRE_INC, PRE_DEC, and PRE_MODIFY. */
18798 case PRE_INC:
18799 case PRE_DEC:
18800 reg = XEXP (addr, 0);
18801 if (!base_reg_operand (addr, GET_MODE (reg)))
18803 fail_msg = "no base register #1";
18804 extra_cost = -1;
18807 else if ((addr_mask & RELOAD_REG_PRE_INCDEC) == 0)
18809 extra_cost = 1;
18810 type = "update";
18812 break;
18814 case PRE_MODIFY:
18815 reg = XEXP (addr, 0);
18816 plus_arg1 = XEXP (addr, 1);
18817 if (!base_reg_operand (reg, GET_MODE (reg))
18818 || GET_CODE (plus_arg1) != PLUS
18819 || !rtx_equal_p (reg, XEXP (plus_arg1, 0)))
18821 fail_msg = "bad PRE_MODIFY";
18822 extra_cost = -1;
18825 else if ((addr_mask & RELOAD_REG_PRE_MODIFY) == 0)
18827 extra_cost = 1;
18828 type = "update";
18830 break;
18832 /* Do we need to simulate AND -16 to clear the bottom address bits used
18833 in VMX load/stores? Only allow the AND for vector sizes. */
18834 case AND:
18835 and_arg = XEXP (addr, 0);
18836 if (GET_MODE_SIZE (mode) != 16
18837 || !CONST_INT_P (XEXP (addr, 1))
18838 || INTVAL (XEXP (addr, 1)) != -16)
18840 fail_msg = "bad Altivec AND #1";
18841 extra_cost = -1;
18844 if (rclass != ALTIVEC_REGS)
18846 if (legitimate_indirect_address_p (and_arg, false))
18847 extra_cost = 1;
18849 else if (legitimate_indexed_address_p (and_arg, false))
18850 extra_cost = 2;
18852 else
18854 fail_msg = "bad Altivec AND #2";
18855 extra_cost = -1;
18858 type = "and";
18860 break;
18862 /* If this is an indirect address, make sure it is a base register. */
18863 case REG:
18864 case SUBREG:
18865 if (!legitimate_indirect_address_p (addr, false))
18867 extra_cost = 1;
18868 type = "move";
18870 break;
18872 /* If this is an indexed address, make sure the register class can handle
18873 indexed addresses for this mode. */
18874 case PLUS:
18875 plus_arg0 = XEXP (addr, 0);
18876 plus_arg1 = XEXP (addr, 1);
18878 /* (plus (plus (reg) (constant)) (constant)) is generated during
18879 push_reload processing, so handle it now. */
18880 if (GET_CODE (plus_arg0) == PLUS && CONST_INT_P (plus_arg1))
18882 if ((addr_mask & RELOAD_REG_OFFSET) == 0)
18884 extra_cost = 1;
18885 type = "offset";
18889 /* (plus (plus (reg) (constant)) (reg)) is also generated during
18890 push_reload processing, so handle it now. */
18891 else if (GET_CODE (plus_arg0) == PLUS && REG_P (plus_arg1))
18893 if ((addr_mask & RELOAD_REG_INDEXED) == 0)
18895 extra_cost = 1;
18896 type = "indexed #2";
18900 else if (!base_reg_operand (plus_arg0, GET_MODE (plus_arg0)))
18902 fail_msg = "no base register #2";
18903 extra_cost = -1;
18906 else if (int_reg_operand (plus_arg1, GET_MODE (plus_arg1)))
18908 if ((addr_mask & RELOAD_REG_INDEXED) == 0
18909 || !legitimate_indexed_address_p (addr, false))
18911 extra_cost = 1;
18912 type = "indexed";
18916 else if ((addr_mask & RELOAD_REG_QUAD_OFFSET) != 0
18917 && CONST_INT_P (plus_arg1))
18919 if (!quad_address_offset_p (INTVAL (plus_arg1)))
18921 extra_cost = 1;
18922 type = "vector d-form offset";
18926 /* Make sure the register class can handle offset addresses. */
18927 else if (rs6000_legitimate_offset_address_p (mode, addr, false, true))
18929 if ((addr_mask & RELOAD_REG_OFFSET) == 0)
18931 extra_cost = 1;
18932 type = "offset #2";
18936 else
18938 fail_msg = "bad PLUS";
18939 extra_cost = -1;
18942 break;
18944 case LO_SUM:
18945 /* Quad offsets are restricted and can't handle normal addresses. */
18946 if ((addr_mask & RELOAD_REG_QUAD_OFFSET) != 0)
18948 extra_cost = -1;
18949 type = "vector d-form lo_sum";
18952 else if (!legitimate_lo_sum_address_p (mode, addr, false))
18954 fail_msg = "bad LO_SUM";
18955 extra_cost = -1;
18958 if ((addr_mask & RELOAD_REG_OFFSET) == 0)
18960 extra_cost = 1;
18961 type = "lo_sum";
18963 break;
18965 /* Static addresses need to create a TOC entry. */
18966 case CONST:
18967 case SYMBOL_REF:
18968 case LABEL_REF:
18969 if ((addr_mask & RELOAD_REG_QUAD_OFFSET) != 0)
18971 extra_cost = -1;
18972 type = "vector d-form lo_sum #2";
18975 else
18977 type = "address";
18978 extra_cost = rs6000_secondary_reload_toc_costs (addr_mask);
18980 break;
18982 /* TOC references look like offsetable memory. */
18983 case UNSPEC:
18984 if (TARGET_CMODEL == CMODEL_SMALL || XINT (addr, 1) != UNSPEC_TOCREL)
18986 fail_msg = "bad UNSPEC";
18987 extra_cost = -1;
18990 else if ((addr_mask & RELOAD_REG_QUAD_OFFSET) != 0)
18992 extra_cost = -1;
18993 type = "vector d-form lo_sum #3";
18996 else if ((addr_mask & RELOAD_REG_OFFSET) == 0)
18998 extra_cost = 1;
18999 type = "toc reference";
19001 break;
19003 default:
19005 fail_msg = "bad address";
19006 extra_cost = -1;
19010 if (TARGET_DEBUG_ADDR /* && extra_cost != 0 */)
19012 if (extra_cost < 0)
19013 fprintf (stderr,
19014 "rs6000_secondary_reload_memory error: mode = %s, "
19015 "class = %s, addr_mask = '%s', %s\n",
19016 GET_MODE_NAME (mode),
19017 reg_class_names[rclass],
19018 rs6000_debug_addr_mask (addr_mask, false),
19019 (fail_msg != NULL) ? fail_msg : "<bad address>");
19021 else
19022 fprintf (stderr,
19023 "rs6000_secondary_reload_memory: mode = %s, class = %s, "
19024 "addr_mask = '%s', extra cost = %d, %s\n",
19025 GET_MODE_NAME (mode),
19026 reg_class_names[rclass],
19027 rs6000_debug_addr_mask (addr_mask, false),
19028 extra_cost,
19029 (type) ? type : "<none>");
19031 debug_rtx (addr);
19034 return extra_cost;
19037 /* Helper function for rs6000_secondary_reload to return true if a move to a
19038 different register classe is really a simple move. */
19040 static bool
19041 rs6000_secondary_reload_simple_move (enum rs6000_reg_type to_type,
19042 enum rs6000_reg_type from_type,
19043 machine_mode mode)
19045 int size = GET_MODE_SIZE (mode);
19047 /* Add support for various direct moves available. In this function, we only
19048 look at cases where we don't need any extra registers, and one or more
19049 simple move insns are issued. Originally small integers are not allowed
19050 in FPR/VSX registers. Single precision binary floating is not a simple
19051 move because we need to convert to the single precision memory layout.
19052 The 4-byte SDmode can be moved. TDmode values are disallowed since they
19053 need special direct move handling, which we do not support yet. */
19054 if (TARGET_DIRECT_MOVE
19055 && ((to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
19056 || (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)))
19058 if (TARGET_POWERPC64)
19060 /* ISA 2.07: MTVSRD or MVFVSRD. */
19061 if (size == 8)
19062 return true;
19064 /* ISA 3.0: MTVSRDD or MFVSRD + MFVSRLD. */
19065 if (size == 16 && TARGET_P9_VECTOR && mode != TDmode)
19066 return true;
19069 /* ISA 2.07: MTVSRWZ or MFVSRWZ. */
19070 if (TARGET_P8_VECTOR)
19072 if (mode == SImode)
19073 return true;
19075 if (TARGET_P9_VECTOR && (mode == HImode || mode == QImode))
19076 return true;
19079 /* ISA 2.07: MTVSRWZ or MFVSRWZ. */
19080 if (mode == SDmode)
19081 return true;
19084 /* Power6+: MFTGPR or MFFGPR. */
19085 else if (TARGET_MFPGPR && TARGET_POWERPC64 && size == 8
19086 && ((to_type == GPR_REG_TYPE && from_type == FPR_REG_TYPE)
19087 || (to_type == FPR_REG_TYPE && from_type == GPR_REG_TYPE)))
19088 return true;
19090 /* Move to/from SPR. */
19091 else if ((size == 4 || (TARGET_POWERPC64 && size == 8))
19092 && ((to_type == GPR_REG_TYPE && from_type == SPR_REG_TYPE)
19093 || (to_type == SPR_REG_TYPE && from_type == GPR_REG_TYPE)))
19094 return true;
19096 return false;
19099 /* Direct move helper function for rs6000_secondary_reload, handle all of the
19100 special direct moves that involve allocating an extra register, return the
19101 insn code of the helper function if there is such a function or
19102 CODE_FOR_nothing if not. */
19104 static bool
19105 rs6000_secondary_reload_direct_move (enum rs6000_reg_type to_type,
19106 enum rs6000_reg_type from_type,
19107 machine_mode mode,
19108 secondary_reload_info *sri,
19109 bool altivec_p)
19111 bool ret = false;
19112 enum insn_code icode = CODE_FOR_nothing;
19113 int cost = 0;
19114 int size = GET_MODE_SIZE (mode);
19116 if (TARGET_POWERPC64 && size == 16)
19118 /* Handle moving 128-bit values from GPRs to VSX point registers on
19119 ISA 2.07 (power8, power9) when running in 64-bit mode using
19120 XXPERMDI to glue the two 64-bit values back together. */
19121 if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)
19123 cost = 3; /* 2 mtvsrd's, 1 xxpermdi. */
19124 icode = reg_addr[mode].reload_vsx_gpr;
19127 /* Handle moving 128-bit values from VSX point registers to GPRs on
19128 ISA 2.07 when running in 64-bit mode using XXPERMDI to get access to the
19129 bottom 64-bit value. */
19130 else if (to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
19132 cost = 3; /* 2 mfvsrd's, 1 xxpermdi. */
19133 icode = reg_addr[mode].reload_gpr_vsx;
19137 else if (TARGET_POWERPC64 && mode == SFmode)
19139 if (to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
19141 cost = 3; /* xscvdpspn, mfvsrd, and. */
19142 icode = reg_addr[mode].reload_gpr_vsx;
19145 else if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)
19147 cost = 2; /* mtvsrz, xscvspdpn. */
19148 icode = reg_addr[mode].reload_vsx_gpr;
19152 else if (!TARGET_POWERPC64 && size == 8)
19154 /* Handle moving 64-bit values from GPRs to floating point registers on
19155 ISA 2.07 when running in 32-bit mode using FMRGOW to glue the two
19156 32-bit values back together. Altivec register classes must be handled
19157 specially since a different instruction is used, and the secondary
19158 reload support requires a single instruction class in the scratch
19159 register constraint. However, right now TFmode is not allowed in
19160 Altivec registers, so the pattern will never match. */
19161 if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE && !altivec_p)
19163 cost = 3; /* 2 mtvsrwz's, 1 fmrgow. */
19164 icode = reg_addr[mode].reload_fpr_gpr;
19168 if (icode != CODE_FOR_nothing)
19170 ret = true;
19171 if (sri)
19173 sri->icode = icode;
19174 sri->extra_cost = cost;
19178 return ret;
19181 /* Return whether a move between two register classes can be done either
19182 directly (simple move) or via a pattern that uses a single extra temporary
19183 (using ISA 2.07's direct move in this case. */
19185 static bool
19186 rs6000_secondary_reload_move (enum rs6000_reg_type to_type,
19187 enum rs6000_reg_type from_type,
19188 machine_mode mode,
19189 secondary_reload_info *sri,
19190 bool altivec_p)
19192 /* Fall back to load/store reloads if either type is not a register. */
19193 if (to_type == NO_REG_TYPE || from_type == NO_REG_TYPE)
19194 return false;
19196 /* If we haven't allocated registers yet, assume the move can be done for the
19197 standard register types. */
19198 if ((to_type == PSEUDO_REG_TYPE && from_type == PSEUDO_REG_TYPE)
19199 || (to_type == PSEUDO_REG_TYPE && IS_STD_REG_TYPE (from_type))
19200 || (from_type == PSEUDO_REG_TYPE && IS_STD_REG_TYPE (to_type)))
19201 return true;
19203 /* Moves to the same set of registers is a simple move for non-specialized
19204 registers. */
19205 if (to_type == from_type && IS_STD_REG_TYPE (to_type))
19206 return true;
19208 /* Check whether a simple move can be done directly. */
19209 if (rs6000_secondary_reload_simple_move (to_type, from_type, mode))
19211 if (sri)
19213 sri->icode = CODE_FOR_nothing;
19214 sri->extra_cost = 0;
19216 return true;
19219 /* Now check if we can do it in a few steps. */
19220 return rs6000_secondary_reload_direct_move (to_type, from_type, mode, sri,
19221 altivec_p);
19224 /* Inform reload about cases where moving X with a mode MODE to a register in
19225 RCLASS requires an extra scratch or immediate register. Return the class
19226 needed for the immediate register.
19228 For VSX and Altivec, we may need a register to convert sp+offset into
19229 reg+sp.
19231 For misaligned 64-bit gpr loads and stores we need a register to
19232 convert an offset address to indirect. */
19234 static reg_class_t
19235 rs6000_secondary_reload (bool in_p,
19236 rtx x,
19237 reg_class_t rclass_i,
19238 machine_mode mode,
19239 secondary_reload_info *sri)
19241 enum reg_class rclass = (enum reg_class) rclass_i;
19242 reg_class_t ret = ALL_REGS;
19243 enum insn_code icode;
19244 bool default_p = false;
19245 bool done_p = false;
19247 /* Allow subreg of memory before/during reload. */
19248 bool memory_p = (MEM_P (x)
19249 || (!reload_completed && SUBREG_P (x)
19250 && MEM_P (SUBREG_REG (x))));
19252 sri->icode = CODE_FOR_nothing;
19253 sri->t_icode = CODE_FOR_nothing;
19254 sri->extra_cost = 0;
19255 icode = ((in_p)
19256 ? reg_addr[mode].reload_load
19257 : reg_addr[mode].reload_store);
19259 if (REG_P (x) || register_operand (x, mode))
19261 enum rs6000_reg_type to_type = reg_class_to_reg_type[(int)rclass];
19262 bool altivec_p = (rclass == ALTIVEC_REGS);
19263 enum rs6000_reg_type from_type = register_to_reg_type (x, &altivec_p);
19265 if (!in_p)
19266 std::swap (to_type, from_type);
19268 /* Can we do a direct move of some sort? */
19269 if (rs6000_secondary_reload_move (to_type, from_type, mode, sri,
19270 altivec_p))
19272 icode = (enum insn_code)sri->icode;
19273 default_p = false;
19274 done_p = true;
19275 ret = NO_REGS;
19279 /* Make sure 0.0 is not reloaded or forced into memory. */
19280 if (x == CONST0_RTX (mode) && VSX_REG_CLASS_P (rclass))
19282 ret = NO_REGS;
19283 default_p = false;
19284 done_p = true;
19287 /* If this is a scalar floating point value and we want to load it into the
19288 traditional Altivec registers, do it via a move via a traditional floating
19289 point register, unless we have D-form addressing. Also make sure that
19290 non-zero constants use a FPR. */
19291 if (!done_p && reg_addr[mode].scalar_in_vmx_p
19292 && !mode_supports_vmx_dform (mode)
19293 && (rclass == VSX_REGS || rclass == ALTIVEC_REGS)
19294 && (memory_p || CONST_DOUBLE_P (x)))
19296 ret = FLOAT_REGS;
19297 default_p = false;
19298 done_p = true;
19301 /* Handle reload of load/stores if we have reload helper functions. */
19302 if (!done_p && icode != CODE_FOR_nothing && memory_p)
19304 int extra_cost = rs6000_secondary_reload_memory (XEXP (x, 0), rclass,
19305 mode);
19307 if (extra_cost >= 0)
19309 done_p = true;
19310 ret = NO_REGS;
19311 if (extra_cost > 0)
19313 sri->extra_cost = extra_cost;
19314 sri->icode = icode;
19319 /* Handle unaligned loads and stores of integer registers. */
19320 if (!done_p && TARGET_POWERPC64
19321 && reg_class_to_reg_type[(int)rclass] == GPR_REG_TYPE
19322 && memory_p
19323 && GET_MODE_SIZE (GET_MODE (x)) >= UNITS_PER_WORD)
19325 rtx addr = XEXP (x, 0);
19326 rtx off = address_offset (addr);
19328 if (off != NULL_RTX)
19330 unsigned int extra = GET_MODE_SIZE (GET_MODE (x)) - UNITS_PER_WORD;
19331 unsigned HOST_WIDE_INT offset = INTVAL (off);
19333 /* We need a secondary reload when our legitimate_address_p
19334 says the address is good (as otherwise the entire address
19335 will be reloaded), and the offset is not a multiple of
19336 four or we have an address wrap. Address wrap will only
19337 occur for LO_SUMs since legitimate_offset_address_p
19338 rejects addresses for 16-byte mems that will wrap. */
19339 if (GET_CODE (addr) == LO_SUM
19340 ? (1 /* legitimate_address_p allows any offset for lo_sum */
19341 && ((offset & 3) != 0
19342 || ((offset & 0xffff) ^ 0x8000) >= 0x10000 - extra))
19343 : (offset + 0x8000 < 0x10000 - extra /* legitimate_address_p */
19344 && (offset & 3) != 0))
19346 /* -m32 -mpowerpc64 needs to use a 32-bit scratch register. */
19347 if (in_p)
19348 sri->icode = ((TARGET_32BIT) ? CODE_FOR_reload_si_load
19349 : CODE_FOR_reload_di_load);
19350 else
19351 sri->icode = ((TARGET_32BIT) ? CODE_FOR_reload_si_store
19352 : CODE_FOR_reload_di_store);
19353 sri->extra_cost = 2;
19354 ret = NO_REGS;
19355 done_p = true;
19357 else
19358 default_p = true;
19360 else
19361 default_p = true;
19364 if (!done_p && !TARGET_POWERPC64
19365 && reg_class_to_reg_type[(int)rclass] == GPR_REG_TYPE
19366 && memory_p
19367 && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
19369 rtx addr = XEXP (x, 0);
19370 rtx off = address_offset (addr);
19372 if (off != NULL_RTX)
19374 unsigned int extra = GET_MODE_SIZE (GET_MODE (x)) - UNITS_PER_WORD;
19375 unsigned HOST_WIDE_INT offset = INTVAL (off);
19377 /* We need a secondary reload when our legitimate_address_p
19378 says the address is good (as otherwise the entire address
19379 will be reloaded), and we have a wrap.
19381 legitimate_lo_sum_address_p allows LO_SUM addresses to
19382 have any offset so test for wrap in the low 16 bits.
19384 legitimate_offset_address_p checks for the range
19385 [-0x8000,0x7fff] for mode size of 8 and [-0x8000,0x7ff7]
19386 for mode size of 16. We wrap at [0x7ffc,0x7fff] and
19387 [0x7ff4,0x7fff] respectively, so test for the
19388 intersection of these ranges, [0x7ffc,0x7fff] and
19389 [0x7ff4,0x7ff7] respectively.
19391 Note that the address we see here may have been
19392 manipulated by legitimize_reload_address. */
19393 if (GET_CODE (addr) == LO_SUM
19394 ? ((offset & 0xffff) ^ 0x8000) >= 0x10000 - extra
19395 : offset - (0x8000 - extra) < UNITS_PER_WORD)
19397 if (in_p)
19398 sri->icode = CODE_FOR_reload_si_load;
19399 else
19400 sri->icode = CODE_FOR_reload_si_store;
19401 sri->extra_cost = 2;
19402 ret = NO_REGS;
19403 done_p = true;
19405 else
19406 default_p = true;
19408 else
19409 default_p = true;
19412 if (!done_p)
19413 default_p = true;
19415 if (default_p)
19416 ret = default_secondary_reload (in_p, x, rclass, mode, sri);
19418 gcc_assert (ret != ALL_REGS);
19420 if (TARGET_DEBUG_ADDR)
19422 fprintf (stderr,
19423 "\nrs6000_secondary_reload, return %s, in_p = %s, rclass = %s, "
19424 "mode = %s",
19425 reg_class_names[ret],
19426 in_p ? "true" : "false",
19427 reg_class_names[rclass],
19428 GET_MODE_NAME (mode));
19430 if (reload_completed)
19431 fputs (", after reload", stderr);
19433 if (!done_p)
19434 fputs (", done_p not set", stderr);
19436 if (default_p)
19437 fputs (", default secondary reload", stderr);
19439 if (sri->icode != CODE_FOR_nothing)
19440 fprintf (stderr, ", reload func = %s, extra cost = %d",
19441 insn_data[sri->icode].name, sri->extra_cost);
19443 else if (sri->extra_cost > 0)
19444 fprintf (stderr, ", extra cost = %d", sri->extra_cost);
19446 fputs ("\n", stderr);
19447 debug_rtx (x);
19450 return ret;
19453 /* Better tracing for rs6000_secondary_reload_inner. */
19455 static void
19456 rs6000_secondary_reload_trace (int line, rtx reg, rtx mem, rtx scratch,
19457 bool store_p)
19459 rtx set, clobber;
19461 gcc_assert (reg != NULL_RTX && mem != NULL_RTX && scratch != NULL_RTX);
19463 fprintf (stderr, "rs6000_secondary_reload_inner:%d, type = %s\n", line,
19464 store_p ? "store" : "load");
19466 if (store_p)
19467 set = gen_rtx_SET (mem, reg);
19468 else
19469 set = gen_rtx_SET (reg, mem);
19471 clobber = gen_rtx_CLOBBER (VOIDmode, scratch);
19472 debug_rtx (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set, clobber)));
19475 static void rs6000_secondary_reload_fail (int, rtx, rtx, rtx, bool)
19476 ATTRIBUTE_NORETURN;
19478 static void
19479 rs6000_secondary_reload_fail (int line, rtx reg, rtx mem, rtx scratch,
19480 bool store_p)
19482 rs6000_secondary_reload_trace (line, reg, mem, scratch, store_p);
19483 gcc_unreachable ();
19486 /* Fixup reload addresses for values in GPR, FPR, and VMX registers that have
19487 reload helper functions. These were identified in
19488 rs6000_secondary_reload_memory, and if reload decided to use the secondary
19489 reload, it calls the insns:
19490 reload_<RELOAD:mode>_<P:mptrsize>_store
19491 reload_<RELOAD:mode>_<P:mptrsize>_load
19493 which in turn calls this function, to do whatever is necessary to create
19494 valid addresses. */
19496 void
19497 rs6000_secondary_reload_inner (rtx reg, rtx mem, rtx scratch, bool store_p)
19499 int regno = true_regnum (reg);
19500 machine_mode mode = GET_MODE (reg);
19501 addr_mask_type addr_mask;
19502 rtx addr;
19503 rtx new_addr;
19504 rtx op_reg, op0, op1;
19505 rtx and_op;
19506 rtx cc_clobber;
19507 rtvec rv;
19509 if (regno < 0 || !HARD_REGISTER_NUM_P (regno) || !MEM_P (mem)
19510 || !base_reg_operand (scratch, GET_MODE (scratch)))
19511 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
19513 if (IN_RANGE (regno, FIRST_GPR_REGNO, LAST_GPR_REGNO))
19514 addr_mask = reg_addr[mode].addr_mask[RELOAD_REG_GPR];
19516 else if (IN_RANGE (regno, FIRST_FPR_REGNO, LAST_FPR_REGNO))
19517 addr_mask = reg_addr[mode].addr_mask[RELOAD_REG_FPR];
19519 else if (IN_RANGE (regno, FIRST_ALTIVEC_REGNO, LAST_ALTIVEC_REGNO))
19520 addr_mask = reg_addr[mode].addr_mask[RELOAD_REG_VMX];
19522 else
19523 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
19525 /* Make sure the mode is valid in this register class. */
19526 if ((addr_mask & RELOAD_REG_VALID) == 0)
19527 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
19529 if (TARGET_DEBUG_ADDR)
19530 rs6000_secondary_reload_trace (__LINE__, reg, mem, scratch, store_p);
19532 new_addr = addr = XEXP (mem, 0);
19533 switch (GET_CODE (addr))
19535 /* Does the register class support auto update forms for this mode? If
19536 not, do the update now. We don't need a scratch register, since the
19537 powerpc only supports PRE_INC, PRE_DEC, and PRE_MODIFY. */
19538 case PRE_INC:
19539 case PRE_DEC:
19540 op_reg = XEXP (addr, 0);
19541 if (!base_reg_operand (op_reg, Pmode))
19542 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
19544 if ((addr_mask & RELOAD_REG_PRE_INCDEC) == 0)
19546 int delta = GET_MODE_SIZE (mode);
19547 if (GET_CODE (addr) == PRE_DEC)
19548 delta = -delta;
19549 emit_insn (gen_add2_insn (op_reg, GEN_INT (delta)));
19550 new_addr = op_reg;
19552 break;
19554 case PRE_MODIFY:
19555 op0 = XEXP (addr, 0);
19556 op1 = XEXP (addr, 1);
19557 if (!base_reg_operand (op0, Pmode)
19558 || GET_CODE (op1) != PLUS
19559 || !rtx_equal_p (op0, XEXP (op1, 0)))
19560 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
19562 if ((addr_mask & RELOAD_REG_PRE_MODIFY) == 0)
19564 emit_insn (gen_rtx_SET (op0, op1));
19565 new_addr = reg;
19567 break;
19569 /* Do we need to simulate AND -16 to clear the bottom address bits used
19570 in VMX load/stores? */
19571 case AND:
19572 op0 = XEXP (addr, 0);
19573 op1 = XEXP (addr, 1);
19574 if ((addr_mask & RELOAD_REG_AND_M16) == 0)
19576 if (REG_P (op0) || SUBREG_P (op0))
19577 op_reg = op0;
19579 else if (GET_CODE (op1) == PLUS)
19581 emit_insn (gen_rtx_SET (scratch, op1));
19582 op_reg = scratch;
19585 else
19586 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
19588 and_op = gen_rtx_AND (GET_MODE (scratch), op_reg, op1);
19589 cc_clobber = gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (CCmode));
19590 rv = gen_rtvec (2, gen_rtx_SET (scratch, and_op), cc_clobber);
19591 emit_insn (gen_rtx_PARALLEL (VOIDmode, rv));
19592 new_addr = scratch;
19594 break;
19596 /* If this is an indirect address, make sure it is a base register. */
19597 case REG:
19598 case SUBREG:
19599 if (!base_reg_operand (addr, GET_MODE (addr)))
19601 emit_insn (gen_rtx_SET (scratch, addr));
19602 new_addr = scratch;
19604 break;
19606 /* If this is an indexed address, make sure the register class can handle
19607 indexed addresses for this mode. */
19608 case PLUS:
19609 op0 = XEXP (addr, 0);
19610 op1 = XEXP (addr, 1);
19611 if (!base_reg_operand (op0, Pmode))
19612 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
19614 else if (int_reg_operand (op1, Pmode))
19616 if ((addr_mask & RELOAD_REG_INDEXED) == 0)
19618 emit_insn (gen_rtx_SET (scratch, addr));
19619 new_addr = scratch;
19623 else if (mode_supports_dq_form (mode) && CONST_INT_P (op1))
19625 if (((addr_mask & RELOAD_REG_QUAD_OFFSET) == 0)
19626 || !quad_address_p (addr, mode, false))
19628 emit_insn (gen_rtx_SET (scratch, addr));
19629 new_addr = scratch;
19633 /* Make sure the register class can handle offset addresses. */
19634 else if (rs6000_legitimate_offset_address_p (mode, addr, false, true))
19636 if ((addr_mask & RELOAD_REG_OFFSET) == 0)
19638 emit_insn (gen_rtx_SET (scratch, addr));
19639 new_addr = scratch;
19643 else
19644 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
19646 break;
19648 case LO_SUM:
19649 op0 = XEXP (addr, 0);
19650 op1 = XEXP (addr, 1);
19651 if (!base_reg_operand (op0, Pmode))
19652 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
19654 else if (int_reg_operand (op1, Pmode))
19656 if ((addr_mask & RELOAD_REG_INDEXED) == 0)
19658 emit_insn (gen_rtx_SET (scratch, addr));
19659 new_addr = scratch;
19663 /* Quad offsets are restricted and can't handle normal addresses. */
19664 else if (mode_supports_dq_form (mode))
19666 emit_insn (gen_rtx_SET (scratch, addr));
19667 new_addr = scratch;
19670 /* Make sure the register class can handle offset addresses. */
19671 else if (legitimate_lo_sum_address_p (mode, addr, false))
19673 if ((addr_mask & RELOAD_REG_OFFSET) == 0)
19675 emit_insn (gen_rtx_SET (scratch, addr));
19676 new_addr = scratch;
19680 else
19681 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
19683 break;
19685 case SYMBOL_REF:
19686 case CONST:
19687 case LABEL_REF:
19688 rs6000_emit_move (scratch, addr, Pmode);
19689 new_addr = scratch;
19690 break;
19692 default:
19693 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
19696 /* Adjust the address if it changed. */
19697 if (addr != new_addr)
19699 mem = replace_equiv_address_nv (mem, new_addr);
19700 if (TARGET_DEBUG_ADDR)
19701 fprintf (stderr, "\nrs6000_secondary_reload_inner, mem adjusted.\n");
19704 /* Now create the move. */
19705 if (store_p)
19706 emit_insn (gen_rtx_SET (mem, reg));
19707 else
19708 emit_insn (gen_rtx_SET (reg, mem));
19710 return;
19713 /* Convert reloads involving 64-bit gprs and misaligned offset
19714 addressing, or multiple 32-bit gprs and offsets that are too large,
19715 to use indirect addressing. */
19717 void
19718 rs6000_secondary_reload_gpr (rtx reg, rtx mem, rtx scratch, bool store_p)
19720 int regno = true_regnum (reg);
19721 enum reg_class rclass;
19722 rtx addr;
19723 rtx scratch_or_premodify = scratch;
19725 if (TARGET_DEBUG_ADDR)
19727 fprintf (stderr, "\nrs6000_secondary_reload_gpr, type = %s\n",
19728 store_p ? "store" : "load");
19729 fprintf (stderr, "reg:\n");
19730 debug_rtx (reg);
19731 fprintf (stderr, "mem:\n");
19732 debug_rtx (mem);
19733 fprintf (stderr, "scratch:\n");
19734 debug_rtx (scratch);
19737 gcc_assert (regno >= 0 && HARD_REGISTER_NUM_P (regno));
19738 gcc_assert (MEM_P (mem));
19739 rclass = REGNO_REG_CLASS (regno);
19740 gcc_assert (rclass == GENERAL_REGS || rclass == BASE_REGS);
19741 addr = XEXP (mem, 0);
19743 if (GET_CODE (addr) == PRE_MODIFY)
19745 gcc_assert (REG_P (XEXP (addr, 0))
19746 && GET_CODE (XEXP (addr, 1)) == PLUS
19747 && XEXP (XEXP (addr, 1), 0) == XEXP (addr, 0));
19748 scratch_or_premodify = XEXP (addr, 0);
19749 addr = XEXP (addr, 1);
19751 gcc_assert (GET_CODE (addr) == PLUS || GET_CODE (addr) == LO_SUM);
19753 rs6000_emit_move (scratch_or_premodify, addr, Pmode);
19755 mem = replace_equiv_address_nv (mem, scratch_or_premodify);
19757 /* Now create the move. */
19758 if (store_p)
19759 emit_insn (gen_rtx_SET (mem, reg));
19760 else
19761 emit_insn (gen_rtx_SET (reg, mem));
19763 return;
19766 /* Given an rtx X being reloaded into a reg required to be
19767 in class CLASS, return the class of reg to actually use.
19768 In general this is just CLASS; but on some machines
19769 in some cases it is preferable to use a more restrictive class.
19771 On the RS/6000, we have to return NO_REGS when we want to reload a
19772 floating-point CONST_DOUBLE to force it to be copied to memory.
19774 We also don't want to reload integer values into floating-point
19775 registers if we can at all help it. In fact, this can
19776 cause reload to die, if it tries to generate a reload of CTR
19777 into a FP register and discovers it doesn't have the memory location
19778 required.
19780 ??? Would it be a good idea to have reload do the converse, that is
19781 try to reload floating modes into FP registers if possible?
19784 static enum reg_class
19785 rs6000_preferred_reload_class (rtx x, enum reg_class rclass)
19787 machine_mode mode = GET_MODE (x);
19788 bool is_constant = CONSTANT_P (x);
19790 /* If a mode can't go in FPR/ALTIVEC/VSX registers, don't return a preferred
19791 reload class for it. */
19792 if ((rclass == ALTIVEC_REGS || rclass == VSX_REGS)
19793 && (reg_addr[mode].addr_mask[RELOAD_REG_VMX] & RELOAD_REG_VALID) == 0)
19794 return NO_REGS;
19796 if ((rclass == FLOAT_REGS || rclass == VSX_REGS)
19797 && (reg_addr[mode].addr_mask[RELOAD_REG_FPR] & RELOAD_REG_VALID) == 0)
19798 return NO_REGS;
19800 /* For VSX, see if we should prefer FLOAT_REGS or ALTIVEC_REGS. Do not allow
19801 the reloading of address expressions using PLUS into floating point
19802 registers. */
19803 if (TARGET_VSX && VSX_REG_CLASS_P (rclass) && GET_CODE (x) != PLUS)
19805 if (is_constant)
19807 /* Zero is always allowed in all VSX registers. */
19808 if (x == CONST0_RTX (mode))
19809 return rclass;
19811 /* If this is a vector constant that can be formed with a few Altivec
19812 instructions, we want altivec registers. */
19813 if (GET_CODE (x) == CONST_VECTOR && easy_vector_constant (x, mode))
19814 return ALTIVEC_REGS;
19816 /* If this is an integer constant that can easily be loaded into
19817 vector registers, allow it. */
19818 if (CONST_INT_P (x))
19820 HOST_WIDE_INT value = INTVAL (x);
19822 /* ISA 2.07 can generate -1 in all registers with XXLORC. ISA
19823 2.06 can generate it in the Altivec registers with
19824 VSPLTI<x>. */
19825 if (value == -1)
19827 if (TARGET_P8_VECTOR)
19828 return rclass;
19829 else if (rclass == ALTIVEC_REGS || rclass == VSX_REGS)
19830 return ALTIVEC_REGS;
19831 else
19832 return NO_REGS;
19835 /* ISA 3.0 can load -128..127 using the XXSPLTIB instruction and
19836 a sign extend in the Altivec registers. */
19837 if (IN_RANGE (value, -128, 127) && TARGET_P9_VECTOR
19838 && (rclass == ALTIVEC_REGS || rclass == VSX_REGS))
19839 return ALTIVEC_REGS;
19842 /* Force constant to memory. */
19843 return NO_REGS;
19846 /* D-form addressing can easily reload the value. */
19847 if (mode_supports_vmx_dform (mode)
19848 || mode_supports_dq_form (mode))
19849 return rclass;
19851 /* If this is a scalar floating point value and we don't have D-form
19852 addressing, prefer the traditional floating point registers so that we
19853 can use D-form (register+offset) addressing. */
19854 if (rclass == VSX_REGS
19855 && (mode == SFmode || GET_MODE_SIZE (mode) == 8))
19856 return FLOAT_REGS;
19858 /* Prefer the Altivec registers if Altivec is handling the vector
19859 operations (i.e. V16QI, V8HI, and V4SI), or if we prefer Altivec
19860 loads. */
19861 if (VECTOR_UNIT_ALTIVEC_P (mode) || VECTOR_MEM_ALTIVEC_P (mode)
19862 || mode == V1TImode)
19863 return ALTIVEC_REGS;
19865 return rclass;
19868 if (is_constant || GET_CODE (x) == PLUS)
19870 if (reg_class_subset_p (GENERAL_REGS, rclass))
19871 return GENERAL_REGS;
19872 if (reg_class_subset_p (BASE_REGS, rclass))
19873 return BASE_REGS;
19874 return NO_REGS;
19877 if (GET_MODE_CLASS (mode) == MODE_INT && rclass == GEN_OR_FLOAT_REGS)
19878 return GENERAL_REGS;
19880 return rclass;
19883 /* Debug version of rs6000_preferred_reload_class. */
19884 static enum reg_class
19885 rs6000_debug_preferred_reload_class (rtx x, enum reg_class rclass)
19887 enum reg_class ret = rs6000_preferred_reload_class (x, rclass);
19889 fprintf (stderr,
19890 "\nrs6000_preferred_reload_class, return %s, rclass = %s, "
19891 "mode = %s, x:\n",
19892 reg_class_names[ret], reg_class_names[rclass],
19893 GET_MODE_NAME (GET_MODE (x)));
19894 debug_rtx (x);
19896 return ret;
19899 /* If we are copying between FP or AltiVec registers and anything else, we need
19900 a memory location. The exception is when we are targeting ppc64 and the
19901 move to/from fpr to gpr instructions are available. Also, under VSX, you
19902 can copy vector registers from the FP register set to the Altivec register
19903 set and vice versa. */
19905 static bool
19906 rs6000_secondary_memory_needed (machine_mode mode,
19907 reg_class_t from_class,
19908 reg_class_t to_class)
19910 enum rs6000_reg_type from_type, to_type;
19911 bool altivec_p = ((from_class == ALTIVEC_REGS)
19912 || (to_class == ALTIVEC_REGS));
19914 /* If a simple/direct move is available, we don't need secondary memory */
19915 from_type = reg_class_to_reg_type[(int)from_class];
19916 to_type = reg_class_to_reg_type[(int)to_class];
19918 if (rs6000_secondary_reload_move (to_type, from_type, mode,
19919 (secondary_reload_info *)0, altivec_p))
19920 return false;
19922 /* If we have a floating point or vector register class, we need to use
19923 memory to transfer the data. */
19924 if (IS_FP_VECT_REG_TYPE (from_type) || IS_FP_VECT_REG_TYPE (to_type))
19925 return true;
19927 return false;
19930 /* Debug version of rs6000_secondary_memory_needed. */
19931 static bool
19932 rs6000_debug_secondary_memory_needed (machine_mode mode,
19933 reg_class_t from_class,
19934 reg_class_t to_class)
19936 bool ret = rs6000_secondary_memory_needed (mode, from_class, to_class);
19938 fprintf (stderr,
19939 "rs6000_secondary_memory_needed, return: %s, from_class = %s, "
19940 "to_class = %s, mode = %s\n",
19941 ret ? "true" : "false",
19942 reg_class_names[from_class],
19943 reg_class_names[to_class],
19944 GET_MODE_NAME (mode));
19946 return ret;
19949 /* Return the register class of a scratch register needed to copy IN into
19950 or out of a register in RCLASS in MODE. If it can be done directly,
19951 NO_REGS is returned. */
19953 static enum reg_class
19954 rs6000_secondary_reload_class (enum reg_class rclass, machine_mode mode,
19955 rtx in)
19957 int regno;
19959 if (TARGET_ELF || (DEFAULT_ABI == ABI_DARWIN
19960 #if TARGET_MACHO
19961 && MACHOPIC_INDIRECT
19962 #endif
19965 /* We cannot copy a symbolic operand directly into anything
19966 other than BASE_REGS for TARGET_ELF. So indicate that a
19967 register from BASE_REGS is needed as an intermediate
19968 register.
19970 On Darwin, pic addresses require a load from memory, which
19971 needs a base register. */
19972 if (rclass != BASE_REGS
19973 && (SYMBOL_REF_P (in)
19974 || GET_CODE (in) == HIGH
19975 || GET_CODE (in) == LABEL_REF
19976 || GET_CODE (in) == CONST))
19977 return BASE_REGS;
19980 if (REG_P (in))
19982 regno = REGNO (in);
19983 if (!HARD_REGISTER_NUM_P (regno))
19985 regno = true_regnum (in);
19986 if (!HARD_REGISTER_NUM_P (regno))
19987 regno = -1;
19990 else if (SUBREG_P (in))
19992 regno = true_regnum (in);
19993 if (!HARD_REGISTER_NUM_P (regno))
19994 regno = -1;
19996 else
19997 regno = -1;
19999 /* If we have VSX register moves, prefer moving scalar values between
20000 Altivec registers and GPR by going via an FPR (and then via memory)
20001 instead of reloading the secondary memory address for Altivec moves. */
20002 if (TARGET_VSX
20003 && GET_MODE_SIZE (mode) < 16
20004 && !mode_supports_vmx_dform (mode)
20005 && (((rclass == GENERAL_REGS || rclass == BASE_REGS)
20006 && (regno >= 0 && ALTIVEC_REGNO_P (regno)))
20007 || ((rclass == VSX_REGS || rclass == ALTIVEC_REGS)
20008 && (regno >= 0 && INT_REGNO_P (regno)))))
20009 return FLOAT_REGS;
20011 /* We can place anything into GENERAL_REGS and can put GENERAL_REGS
20012 into anything. */
20013 if (rclass == GENERAL_REGS || rclass == BASE_REGS
20014 || (regno >= 0 && INT_REGNO_P (regno)))
20015 return NO_REGS;
20017 /* Constants, memory, and VSX registers can go into VSX registers (both the
20018 traditional floating point and the altivec registers). */
20019 if (rclass == VSX_REGS
20020 && (regno == -1 || VSX_REGNO_P (regno)))
20021 return NO_REGS;
20023 /* Constants, memory, and FP registers can go into FP registers. */
20024 if ((regno == -1 || FP_REGNO_P (regno))
20025 && (rclass == FLOAT_REGS || rclass == GEN_OR_FLOAT_REGS))
20026 return (mode != SDmode || lra_in_progress) ? NO_REGS : GENERAL_REGS;
20028 /* Memory, and AltiVec registers can go into AltiVec registers. */
20029 if ((regno == -1 || ALTIVEC_REGNO_P (regno))
20030 && rclass == ALTIVEC_REGS)
20031 return NO_REGS;
20033 /* We can copy among the CR registers. */
20034 if ((rclass == CR_REGS || rclass == CR0_REGS)
20035 && regno >= 0 && CR_REGNO_P (regno))
20036 return NO_REGS;
20038 /* Otherwise, we need GENERAL_REGS. */
20039 return GENERAL_REGS;
20042 /* Debug version of rs6000_secondary_reload_class. */
20043 static enum reg_class
20044 rs6000_debug_secondary_reload_class (enum reg_class rclass,
20045 machine_mode mode, rtx in)
20047 enum reg_class ret = rs6000_secondary_reload_class (rclass, mode, in);
20048 fprintf (stderr,
20049 "\nrs6000_secondary_reload_class, return %s, rclass = %s, "
20050 "mode = %s, input rtx:\n",
20051 reg_class_names[ret], reg_class_names[rclass],
20052 GET_MODE_NAME (mode));
20053 debug_rtx (in);
20055 return ret;
20058 /* Implement TARGET_CAN_CHANGE_MODE_CLASS. */
20060 static bool
20061 rs6000_can_change_mode_class (machine_mode from,
20062 machine_mode to,
20063 reg_class_t rclass)
20065 unsigned from_size = GET_MODE_SIZE (from);
20066 unsigned to_size = GET_MODE_SIZE (to);
20068 if (from_size != to_size)
20070 enum reg_class xclass = (TARGET_VSX) ? VSX_REGS : FLOAT_REGS;
20072 if (reg_classes_intersect_p (xclass, rclass))
20074 unsigned to_nregs = hard_regno_nregs (FIRST_FPR_REGNO, to);
20075 unsigned from_nregs = hard_regno_nregs (FIRST_FPR_REGNO, from);
20076 bool to_float128_vector_p = FLOAT128_VECTOR_P (to);
20077 bool from_float128_vector_p = FLOAT128_VECTOR_P (from);
20079 /* Don't allow 64-bit types to overlap with 128-bit types that take a
20080 single register under VSX because the scalar part of the register
20081 is in the upper 64-bits, and not the lower 64-bits. Types like
20082 TFmode/TDmode that take 2 scalar register can overlap. 128-bit
20083 IEEE floating point can't overlap, and neither can small
20084 values. */
20086 if (to_float128_vector_p && from_float128_vector_p)
20087 return true;
20089 else if (to_float128_vector_p || from_float128_vector_p)
20090 return false;
20092 /* TDmode in floating-mode registers must always go into a register
20093 pair with the most significant word in the even-numbered register
20094 to match ISA requirements. In little-endian mode, this does not
20095 match subreg numbering, so we cannot allow subregs. */
20096 if (!BYTES_BIG_ENDIAN && (to == TDmode || from == TDmode))
20097 return false;
20099 if (from_size < 8 || to_size < 8)
20100 return false;
20102 if (from_size == 8 && (8 * to_nregs) != to_size)
20103 return false;
20105 if (to_size == 8 && (8 * from_nregs) != from_size)
20106 return false;
20108 return true;
20110 else
20111 return true;
20114 /* Since the VSX register set includes traditional floating point registers
20115 and altivec registers, just check for the size being different instead of
20116 trying to check whether the modes are vector modes. Otherwise it won't
20117 allow say DF and DI to change classes. For types like TFmode and TDmode
20118 that take 2 64-bit registers, rather than a single 128-bit register, don't
20119 allow subregs of those types to other 128 bit types. */
20120 if (TARGET_VSX && VSX_REG_CLASS_P (rclass))
20122 unsigned num_regs = (from_size + 15) / 16;
20123 if (hard_regno_nregs (FIRST_FPR_REGNO, to) > num_regs
20124 || hard_regno_nregs (FIRST_FPR_REGNO, from) > num_regs)
20125 return false;
20127 return (from_size == 8 || from_size == 16);
20130 if (TARGET_ALTIVEC && rclass == ALTIVEC_REGS
20131 && (ALTIVEC_VECTOR_MODE (from) + ALTIVEC_VECTOR_MODE (to)) == 1)
20132 return false;
20134 return true;
20137 /* Debug version of rs6000_can_change_mode_class. */
20138 static bool
20139 rs6000_debug_can_change_mode_class (machine_mode from,
20140 machine_mode to,
20141 reg_class_t rclass)
20143 bool ret = rs6000_can_change_mode_class (from, to, rclass);
20145 fprintf (stderr,
20146 "rs6000_can_change_mode_class, return %s, from = %s, "
20147 "to = %s, rclass = %s\n",
20148 ret ? "true" : "false",
20149 GET_MODE_NAME (from), GET_MODE_NAME (to),
20150 reg_class_names[rclass]);
20152 return ret;
20155 /* Return a string to do a move operation of 128 bits of data. */
20157 const char *
20158 rs6000_output_move_128bit (rtx operands[])
20160 rtx dest = operands[0];
20161 rtx src = operands[1];
20162 machine_mode mode = GET_MODE (dest);
20163 int dest_regno;
20164 int src_regno;
20165 bool dest_gpr_p, dest_fp_p, dest_vmx_p, dest_vsx_p;
20166 bool src_gpr_p, src_fp_p, src_vmx_p, src_vsx_p;
20168 if (REG_P (dest))
20170 dest_regno = REGNO (dest);
20171 dest_gpr_p = INT_REGNO_P (dest_regno);
20172 dest_fp_p = FP_REGNO_P (dest_regno);
20173 dest_vmx_p = ALTIVEC_REGNO_P (dest_regno);
20174 dest_vsx_p = dest_fp_p | dest_vmx_p;
20176 else
20178 dest_regno = -1;
20179 dest_gpr_p = dest_fp_p = dest_vmx_p = dest_vsx_p = false;
20182 if (REG_P (src))
20184 src_regno = REGNO (src);
20185 src_gpr_p = INT_REGNO_P (src_regno);
20186 src_fp_p = FP_REGNO_P (src_regno);
20187 src_vmx_p = ALTIVEC_REGNO_P (src_regno);
20188 src_vsx_p = src_fp_p | src_vmx_p;
20190 else
20192 src_regno = -1;
20193 src_gpr_p = src_fp_p = src_vmx_p = src_vsx_p = false;
20196 /* Register moves. */
20197 if (dest_regno >= 0 && src_regno >= 0)
20199 if (dest_gpr_p)
20201 if (src_gpr_p)
20202 return "#";
20204 if (TARGET_DIRECT_MOVE_128 && src_vsx_p)
20205 return (WORDS_BIG_ENDIAN
20206 ? "mfvsrd %0,%x1\n\tmfvsrld %L0,%x1"
20207 : "mfvsrd %L0,%x1\n\tmfvsrld %0,%x1");
20209 else if (TARGET_VSX && TARGET_DIRECT_MOVE && src_vsx_p)
20210 return "#";
20213 else if (TARGET_VSX && dest_vsx_p)
20215 if (src_vsx_p)
20216 return "xxlor %x0,%x1,%x1";
20218 else if (TARGET_DIRECT_MOVE_128 && src_gpr_p)
20219 return (WORDS_BIG_ENDIAN
20220 ? "mtvsrdd %x0,%1,%L1"
20221 : "mtvsrdd %x0,%L1,%1");
20223 else if (TARGET_DIRECT_MOVE && src_gpr_p)
20224 return "#";
20227 else if (TARGET_ALTIVEC && dest_vmx_p && src_vmx_p)
20228 return "vor %0,%1,%1";
20230 else if (dest_fp_p && src_fp_p)
20231 return "#";
20234 /* Loads. */
20235 else if (dest_regno >= 0 && MEM_P (src))
20237 if (dest_gpr_p)
20239 if (TARGET_QUAD_MEMORY && quad_load_store_p (dest, src))
20240 return "lq %0,%1";
20241 else
20242 return "#";
20245 else if (TARGET_ALTIVEC && dest_vmx_p
20246 && altivec_indexed_or_indirect_operand (src, mode))
20247 return "lvx %0,%y1";
20249 else if (TARGET_VSX && dest_vsx_p)
20251 if (mode_supports_dq_form (mode)
20252 && quad_address_p (XEXP (src, 0), mode, true))
20253 return "lxv %x0,%1";
20255 else if (TARGET_P9_VECTOR)
20256 return "lxvx %x0,%y1";
20258 else if (mode == V16QImode || mode == V8HImode || mode == V4SImode)
20259 return "lxvw4x %x0,%y1";
20261 else
20262 return "lxvd2x %x0,%y1";
20265 else if (TARGET_ALTIVEC && dest_vmx_p)
20266 return "lvx %0,%y1";
20268 else if (dest_fp_p)
20269 return "#";
20272 /* Stores. */
20273 else if (src_regno >= 0 && MEM_P (dest))
20275 if (src_gpr_p)
20277 if (TARGET_QUAD_MEMORY && quad_load_store_p (dest, src))
20278 return "stq %1,%0";
20279 else
20280 return "#";
20283 else if (TARGET_ALTIVEC && src_vmx_p
20284 && altivec_indexed_or_indirect_operand (dest, mode))
20285 return "stvx %1,%y0";
20287 else if (TARGET_VSX && src_vsx_p)
20289 if (mode_supports_dq_form (mode)
20290 && quad_address_p (XEXP (dest, 0), mode, true))
20291 return "stxv %x1,%0";
20293 else if (TARGET_P9_VECTOR)
20294 return "stxvx %x1,%y0";
20296 else if (mode == V16QImode || mode == V8HImode || mode == V4SImode)
20297 return "stxvw4x %x1,%y0";
20299 else
20300 return "stxvd2x %x1,%y0";
20303 else if (TARGET_ALTIVEC && src_vmx_p)
20304 return "stvx %1,%y0";
20306 else if (src_fp_p)
20307 return "#";
20310 /* Constants. */
20311 else if (dest_regno >= 0
20312 && (CONST_INT_P (src)
20313 || CONST_WIDE_INT_P (src)
20314 || CONST_DOUBLE_P (src)
20315 || GET_CODE (src) == CONST_VECTOR))
20317 if (dest_gpr_p)
20318 return "#";
20320 else if ((dest_vmx_p && TARGET_ALTIVEC)
20321 || (dest_vsx_p && TARGET_VSX))
20322 return output_vec_const_move (operands);
20325 fatal_insn ("Bad 128-bit move", gen_rtx_SET (dest, src));
20328 /* Validate a 128-bit move. */
20329 bool
20330 rs6000_move_128bit_ok_p (rtx operands[])
20332 machine_mode mode = GET_MODE (operands[0]);
20333 return (gpc_reg_operand (operands[0], mode)
20334 || gpc_reg_operand (operands[1], mode));
20337 /* Return true if a 128-bit move needs to be split. */
20338 bool
20339 rs6000_split_128bit_ok_p (rtx operands[])
20341 if (!reload_completed)
20342 return false;
20344 if (!gpr_or_gpr_p (operands[0], operands[1]))
20345 return false;
20347 if (quad_load_store_p (operands[0], operands[1]))
20348 return false;
20350 return true;
20354 /* Given a comparison operation, return the bit number in CCR to test. We
20355 know this is a valid comparison.
20357 SCC_P is 1 if this is for an scc. That means that %D will have been
20358 used instead of %C, so the bits will be in different places.
20360 Return -1 if OP isn't a valid comparison for some reason. */
20363 ccr_bit (rtx op, int scc_p)
20365 enum rtx_code code = GET_CODE (op);
20366 machine_mode cc_mode;
20367 int cc_regnum;
20368 int base_bit;
20369 rtx reg;
20371 if (!COMPARISON_P (op))
20372 return -1;
20374 reg = XEXP (op, 0);
20376 if (!REG_P (reg) || !CR_REGNO_P (REGNO (reg)))
20377 return -1;
20379 cc_mode = GET_MODE (reg);
20380 cc_regnum = REGNO (reg);
20381 base_bit = 4 * (cc_regnum - CR0_REGNO);
20383 validate_condition_mode (code, cc_mode);
20385 /* When generating a sCOND operation, only positive conditions are
20386 allowed. */
20387 if (scc_p)
20388 switch (code)
20390 case EQ:
20391 case GT:
20392 case LT:
20393 case UNORDERED:
20394 case GTU:
20395 case LTU:
20396 break;
20397 default:
20398 return -1;
20401 switch (code)
20403 case NE:
20404 return scc_p ? base_bit + 3 : base_bit + 2;
20405 case EQ:
20406 return base_bit + 2;
20407 case GT: case GTU: case UNLE:
20408 return base_bit + 1;
20409 case LT: case LTU: case UNGE:
20410 return base_bit;
20411 case ORDERED: case UNORDERED:
20412 return base_bit + 3;
20414 case GE: case GEU:
20415 /* If scc, we will have done a cror to put the bit in the
20416 unordered position. So test that bit. For integer, this is ! LT
20417 unless this is an scc insn. */
20418 return scc_p ? base_bit + 3 : base_bit;
20420 case LE: case LEU:
20421 return scc_p ? base_bit + 3 : base_bit + 1;
20423 default:
20424 return -1;
20428 /* Return the GOT register. */
20431 rs6000_got_register (rtx value ATTRIBUTE_UNUSED)
20433 /* The second flow pass currently (June 1999) can't update
20434 regs_ever_live without disturbing other parts of the compiler, so
20435 update it here to make the prolog/epilogue code happy. */
20436 if (!can_create_pseudo_p ()
20437 && !df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))
20438 df_set_regs_ever_live (RS6000_PIC_OFFSET_TABLE_REGNUM, true);
20440 crtl->uses_pic_offset_table = 1;
20442 return pic_offset_table_rtx;
20445 static rs6000_stack_t stack_info;
20447 /* Function to init struct machine_function.
20448 This will be called, via a pointer variable,
20449 from push_function_context. */
20451 static struct machine_function *
20452 rs6000_init_machine_status (void)
20454 stack_info.reload_completed = 0;
20455 return ggc_cleared_alloc<machine_function> ();
20458 #define INT_P(X) (CONST_INT_P (X) && GET_MODE (X) == VOIDmode)
20460 /* Write out a function code label. */
20462 void
20463 rs6000_output_function_entry (FILE *file, const char *fname)
20465 if (fname[0] != '.')
20467 switch (DEFAULT_ABI)
20469 default:
20470 gcc_unreachable ();
20472 case ABI_AIX:
20473 if (DOT_SYMBOLS)
20474 putc ('.', file);
20475 else
20476 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "L.");
20477 break;
20479 case ABI_ELFv2:
20480 case ABI_V4:
20481 case ABI_DARWIN:
20482 break;
20486 RS6000_OUTPUT_BASENAME (file, fname);
20489 /* Print an operand. Recognize special options, documented below. */
20491 #if TARGET_ELF
20492 /* Access to .sdata2 through r2 (see -msdata=eabi in invoke.texi) is
20493 only introduced by the linker, when applying the sda21
20494 relocation. */
20495 #define SMALL_DATA_RELOC ((rs6000_sdata == SDATA_EABI) ? "sda21" : "sdarel")
20496 #define SMALL_DATA_REG ((rs6000_sdata == SDATA_EABI) ? 0 : 13)
20497 #else
20498 #define SMALL_DATA_RELOC "sda21"
20499 #define SMALL_DATA_REG 0
20500 #endif
20502 void
20503 print_operand (FILE *file, rtx x, int code)
20505 int i;
20506 unsigned HOST_WIDE_INT uval;
20508 switch (code)
20510 /* %a is output_address. */
20512 /* %c is output_addr_const if a CONSTANT_ADDRESS_P, otherwise
20513 output_operand. */
20515 case 'D':
20516 /* Like 'J' but get to the GT bit only. */
20517 if (!REG_P (x) || !CR_REGNO_P (REGNO (x)))
20519 output_operand_lossage ("invalid %%D value");
20520 return;
20523 /* Bit 1 is GT bit. */
20524 i = 4 * (REGNO (x) - CR0_REGNO) + 1;
20526 /* Add one for shift count in rlinm for scc. */
20527 fprintf (file, "%d", i + 1);
20528 return;
20530 case 'e':
20531 /* If the low 16 bits are 0, but some other bit is set, write 's'. */
20532 if (! INT_P (x))
20534 output_operand_lossage ("invalid %%e value");
20535 return;
20538 uval = INTVAL (x);
20539 if ((uval & 0xffff) == 0 && uval != 0)
20540 putc ('s', file);
20541 return;
20543 case 'E':
20544 /* X is a CR register. Print the number of the EQ bit of the CR */
20545 if (!REG_P (x) || !CR_REGNO_P (REGNO (x)))
20546 output_operand_lossage ("invalid %%E value");
20547 else
20548 fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO) + 2);
20549 return;
20551 case 'f':
20552 /* X is a CR register. Print the shift count needed to move it
20553 to the high-order four bits. */
20554 if (!REG_P (x) || !CR_REGNO_P (REGNO (x)))
20555 output_operand_lossage ("invalid %%f value");
20556 else
20557 fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO));
20558 return;
20560 case 'F':
20561 /* Similar, but print the count for the rotate in the opposite
20562 direction. */
20563 if (!REG_P (x) || !CR_REGNO_P (REGNO (x)))
20564 output_operand_lossage ("invalid %%F value");
20565 else
20566 fprintf (file, "%d", 32 - 4 * (REGNO (x) - CR0_REGNO));
20567 return;
20569 case 'G':
20570 /* X is a constant integer. If it is negative, print "m",
20571 otherwise print "z". This is to make an aze or ame insn. */
20572 if (!CONST_INT_P (x))
20573 output_operand_lossage ("invalid %%G value");
20574 else if (INTVAL (x) >= 0)
20575 putc ('z', file);
20576 else
20577 putc ('m', file);
20578 return;
20580 case 'h':
20581 /* If constant, output low-order five bits. Otherwise, write
20582 normally. */
20583 if (INT_P (x))
20584 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 31);
20585 else
20586 print_operand (file, x, 0);
20587 return;
20589 case 'H':
20590 /* If constant, output low-order six bits. Otherwise, write
20591 normally. */
20592 if (INT_P (x))
20593 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 63);
20594 else
20595 print_operand (file, x, 0);
20596 return;
20598 case 'I':
20599 /* Print `i' if this is a constant, else nothing. */
20600 if (INT_P (x))
20601 putc ('i', file);
20602 return;
20604 case 'j':
20605 /* Write the bit number in CCR for jump. */
20606 i = ccr_bit (x, 0);
20607 if (i == -1)
20608 output_operand_lossage ("invalid %%j code");
20609 else
20610 fprintf (file, "%d", i);
20611 return;
20613 case 'J':
20614 /* Similar, but add one for shift count in rlinm for scc and pass
20615 scc flag to `ccr_bit'. */
20616 i = ccr_bit (x, 1);
20617 if (i == -1)
20618 output_operand_lossage ("invalid %%J code");
20619 else
20620 /* If we want bit 31, write a shift count of zero, not 32. */
20621 fprintf (file, "%d", i == 31 ? 0 : i + 1);
20622 return;
20624 case 'k':
20625 /* X must be a constant. Write the 1's complement of the
20626 constant. */
20627 if (! INT_P (x))
20628 output_operand_lossage ("invalid %%k value");
20629 else
20630 fprintf (file, HOST_WIDE_INT_PRINT_DEC, ~ INTVAL (x));
20631 return;
20633 case 'K':
20634 /* X must be a symbolic constant on ELF. Write an
20635 expression suitable for an 'addi' that adds in the low 16
20636 bits of the MEM. */
20637 if (GET_CODE (x) == CONST)
20639 if (GET_CODE (XEXP (x, 0)) != PLUS
20640 || (!SYMBOL_REF_P (XEXP (XEXP (x, 0), 0))
20641 && GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
20642 || !CONST_INT_P (XEXP (XEXP (x, 0), 1)))
20643 output_operand_lossage ("invalid %%K value");
20645 print_operand_address (file, x);
20646 fputs ("@l", file);
20647 return;
20649 /* %l is output_asm_label. */
20651 case 'L':
20652 /* Write second word of DImode or DFmode reference. Works on register
20653 or non-indexed memory only. */
20654 if (REG_P (x))
20655 fputs (reg_names[REGNO (x) + 1], file);
20656 else if (MEM_P (x))
20658 machine_mode mode = GET_MODE (x);
20659 /* Handle possible auto-increment. Since it is pre-increment and
20660 we have already done it, we can just use an offset of word. */
20661 if (GET_CODE (XEXP (x, 0)) == PRE_INC
20662 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
20663 output_address (mode, plus_constant (Pmode, XEXP (XEXP (x, 0), 0),
20664 UNITS_PER_WORD));
20665 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
20666 output_address (mode, plus_constant (Pmode, XEXP (XEXP (x, 0), 0),
20667 UNITS_PER_WORD));
20668 else
20669 output_address (mode, XEXP (adjust_address_nv (x, SImode,
20670 UNITS_PER_WORD),
20671 0));
20673 if (small_data_operand (x, GET_MODE (x)))
20674 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
20675 reg_names[SMALL_DATA_REG]);
20677 return;
20679 case 'N': /* Unused */
20680 /* Write the number of elements in the vector times 4. */
20681 if (GET_CODE (x) != PARALLEL)
20682 output_operand_lossage ("invalid %%N value");
20683 else
20684 fprintf (file, "%d", XVECLEN (x, 0) * 4);
20685 return;
20687 case 'O': /* Unused */
20688 /* Similar, but subtract 1 first. */
20689 if (GET_CODE (x) != PARALLEL)
20690 output_operand_lossage ("invalid %%O value");
20691 else
20692 fprintf (file, "%d", (XVECLEN (x, 0) - 1) * 4);
20693 return;
20695 case 'p':
20696 /* X is a CONST_INT that is a power of two. Output the logarithm. */
20697 if (! INT_P (x)
20698 || INTVAL (x) < 0
20699 || (i = exact_log2 (INTVAL (x))) < 0)
20700 output_operand_lossage ("invalid %%p value");
20701 else
20702 fprintf (file, "%d", i);
20703 return;
20705 case 'P':
20706 /* The operand must be an indirect memory reference. The result
20707 is the register name. */
20708 if (!MEM_P (x) || !REG_P (XEXP (x, 0))
20709 || REGNO (XEXP (x, 0)) >= 32)
20710 output_operand_lossage ("invalid %%P value");
20711 else
20712 fputs (reg_names[REGNO (XEXP (x, 0))], file);
20713 return;
20715 case 'q':
20716 /* This outputs the logical code corresponding to a boolean
20717 expression. The expression may have one or both operands
20718 negated (if one, only the first one). For condition register
20719 logical operations, it will also treat the negated
20720 CR codes as NOTs, but not handle NOTs of them. */
20722 const char *const *t = 0;
20723 const char *s;
20724 enum rtx_code code = GET_CODE (x);
20725 static const char * const tbl[3][3] = {
20726 { "and", "andc", "nor" },
20727 { "or", "orc", "nand" },
20728 { "xor", "eqv", "xor" } };
20730 if (code == AND)
20731 t = tbl[0];
20732 else if (code == IOR)
20733 t = tbl[1];
20734 else if (code == XOR)
20735 t = tbl[2];
20736 else
20737 output_operand_lossage ("invalid %%q value");
20739 if (GET_CODE (XEXP (x, 0)) != NOT)
20740 s = t[0];
20741 else
20743 if (GET_CODE (XEXP (x, 1)) == NOT)
20744 s = t[2];
20745 else
20746 s = t[1];
20749 fputs (s, file);
20751 return;
20753 case 'Q':
20754 if (! TARGET_MFCRF)
20755 return;
20756 fputc (',', file);
20757 /* FALLTHRU */
20759 case 'R':
20760 /* X is a CR register. Print the mask for `mtcrf'. */
20761 if (!REG_P (x) || !CR_REGNO_P (REGNO (x)))
20762 output_operand_lossage ("invalid %%R value");
20763 else
20764 fprintf (file, "%d", 128 >> (REGNO (x) - CR0_REGNO));
20765 return;
20767 case 's':
20768 /* Low 5 bits of 32 - value */
20769 if (! INT_P (x))
20770 output_operand_lossage ("invalid %%s value");
20771 else
20772 fprintf (file, HOST_WIDE_INT_PRINT_DEC, (32 - INTVAL (x)) & 31);
20773 return;
20775 case 't':
20776 /* Like 'J' but get to the OVERFLOW/UNORDERED bit. */
20777 if (!REG_P (x) || !CR_REGNO_P (REGNO (x)))
20779 output_operand_lossage ("invalid %%t value");
20780 return;
20783 /* Bit 3 is OV bit. */
20784 i = 4 * (REGNO (x) - CR0_REGNO) + 3;
20786 /* If we want bit 31, write a shift count of zero, not 32. */
20787 fprintf (file, "%d", i == 31 ? 0 : i + 1);
20788 return;
20790 case 'T':
20791 /* Print the symbolic name of a branch target register. */
20792 if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_PLTSEQ)
20793 x = XVECEXP (x, 0, 0);
20794 if (!REG_P (x) || (REGNO (x) != LR_REGNO
20795 && REGNO (x) != CTR_REGNO))
20796 output_operand_lossage ("invalid %%T value");
20797 else if (REGNO (x) == LR_REGNO)
20798 fputs ("lr", file);
20799 else
20800 fputs ("ctr", file);
20801 return;
20803 case 'u':
20804 /* High-order or low-order 16 bits of constant, whichever is non-zero,
20805 for use in unsigned operand. */
20806 if (! INT_P (x))
20808 output_operand_lossage ("invalid %%u value");
20809 return;
20812 uval = INTVAL (x);
20813 if ((uval & 0xffff) == 0)
20814 uval >>= 16;
20816 fprintf (file, HOST_WIDE_INT_PRINT_HEX, uval & 0xffff);
20817 return;
20819 case 'v':
20820 /* High-order 16 bits of constant for use in signed operand. */
20821 if (! INT_P (x))
20822 output_operand_lossage ("invalid %%v value");
20823 else
20824 fprintf (file, HOST_WIDE_INT_PRINT_HEX,
20825 (INTVAL (x) >> 16) & 0xffff);
20826 return;
20828 case 'U':
20829 /* Print `u' if this has an auto-increment or auto-decrement. */
20830 if (MEM_P (x)
20831 && (GET_CODE (XEXP (x, 0)) == PRE_INC
20832 || GET_CODE (XEXP (x, 0)) == PRE_DEC
20833 || GET_CODE (XEXP (x, 0)) == PRE_MODIFY))
20834 putc ('u', file);
20835 return;
20837 case 'V':
20838 /* Print the trap code for this operand. */
20839 switch (GET_CODE (x))
20841 case EQ:
20842 fputs ("eq", file); /* 4 */
20843 break;
20844 case NE:
20845 fputs ("ne", file); /* 24 */
20846 break;
20847 case LT:
20848 fputs ("lt", file); /* 16 */
20849 break;
20850 case LE:
20851 fputs ("le", file); /* 20 */
20852 break;
20853 case GT:
20854 fputs ("gt", file); /* 8 */
20855 break;
20856 case GE:
20857 fputs ("ge", file); /* 12 */
20858 break;
20859 case LTU:
20860 fputs ("llt", file); /* 2 */
20861 break;
20862 case LEU:
20863 fputs ("lle", file); /* 6 */
20864 break;
20865 case GTU:
20866 fputs ("lgt", file); /* 1 */
20867 break;
20868 case GEU:
20869 fputs ("lge", file); /* 5 */
20870 break;
20871 default:
20872 output_operand_lossage ("invalid %%V value");
20874 break;
20876 case 'w':
20877 /* If constant, low-order 16 bits of constant, signed. Otherwise, write
20878 normally. */
20879 if (INT_P (x))
20880 fprintf (file, HOST_WIDE_INT_PRINT_DEC,
20881 ((INTVAL (x) & 0xffff) ^ 0x8000) - 0x8000);
20882 else
20883 print_operand (file, x, 0);
20884 return;
20886 case 'x':
20887 /* X is a FPR or Altivec register used in a VSX context. */
20888 if (!REG_P (x) || !VSX_REGNO_P (REGNO (x)))
20889 output_operand_lossage ("invalid %%x value");
20890 else
20892 int reg = REGNO (x);
20893 int vsx_reg = (FP_REGNO_P (reg)
20894 ? reg - 32
20895 : reg - FIRST_ALTIVEC_REGNO + 32);
20897 #ifdef TARGET_REGNAMES
20898 if (TARGET_REGNAMES)
20899 fprintf (file, "%%vs%d", vsx_reg);
20900 else
20901 #endif
20902 fprintf (file, "%d", vsx_reg);
20904 return;
20906 case 'X':
20907 if (MEM_P (x)
20908 && (legitimate_indexed_address_p (XEXP (x, 0), 0)
20909 || (GET_CODE (XEXP (x, 0)) == PRE_MODIFY
20910 && legitimate_indexed_address_p (XEXP (XEXP (x, 0), 1), 0))))
20911 putc ('x', file);
20912 return;
20914 case 'Y':
20915 /* Like 'L', for third word of TImode/PTImode */
20916 if (REG_P (x))
20917 fputs (reg_names[REGNO (x) + 2], file);
20918 else if (MEM_P (x))
20920 machine_mode mode = GET_MODE (x);
20921 if (GET_CODE (XEXP (x, 0)) == PRE_INC
20922 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
20923 output_address (mode, plus_constant (Pmode,
20924 XEXP (XEXP (x, 0), 0), 8));
20925 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
20926 output_address (mode, plus_constant (Pmode,
20927 XEXP (XEXP (x, 0), 0), 8));
20928 else
20929 output_address (mode, XEXP (adjust_address_nv (x, SImode, 8), 0));
20930 if (small_data_operand (x, GET_MODE (x)))
20931 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
20932 reg_names[SMALL_DATA_REG]);
20934 return;
20936 case 'z':
20937 if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_PLTSEQ)
20938 x = XVECEXP (x, 0, 1);
20939 /* X is a SYMBOL_REF. Write out the name preceded by a
20940 period and without any trailing data in brackets. Used for function
20941 names. If we are configured for System V (or the embedded ABI) on
20942 the PowerPC, do not emit the period, since those systems do not use
20943 TOCs and the like. */
20944 if (!SYMBOL_REF_P (x))
20946 output_operand_lossage ("invalid %%z value");
20947 return;
20950 /* For macho, check to see if we need a stub. */
20951 if (TARGET_MACHO)
20953 const char *name = XSTR (x, 0);
20954 #if TARGET_MACHO
20955 if (darwin_emit_branch_islands
20956 && MACHOPIC_INDIRECT
20957 && machopic_classify_symbol (x) == MACHOPIC_UNDEFINED_FUNCTION)
20958 name = machopic_indirection_name (x, /*stub_p=*/true);
20959 #endif
20960 assemble_name (file, name);
20962 else if (!DOT_SYMBOLS)
20963 assemble_name (file, XSTR (x, 0));
20964 else
20965 rs6000_output_function_entry (file, XSTR (x, 0));
20966 return;
20968 case 'Z':
20969 /* Like 'L', for last word of TImode/PTImode. */
20970 if (REG_P (x))
20971 fputs (reg_names[REGNO (x) + 3], file);
20972 else if (MEM_P (x))
20974 machine_mode mode = GET_MODE (x);
20975 if (GET_CODE (XEXP (x, 0)) == PRE_INC
20976 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
20977 output_address (mode, plus_constant (Pmode,
20978 XEXP (XEXP (x, 0), 0), 12));
20979 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
20980 output_address (mode, plus_constant (Pmode,
20981 XEXP (XEXP (x, 0), 0), 12));
20982 else
20983 output_address (mode, XEXP (adjust_address_nv (x, SImode, 12), 0));
20984 if (small_data_operand (x, GET_MODE (x)))
20985 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
20986 reg_names[SMALL_DATA_REG]);
20988 return;
20990 /* Print AltiVec memory operand. */
20991 case 'y':
20993 rtx tmp;
20995 gcc_assert (MEM_P (x));
20997 tmp = XEXP (x, 0);
20999 if (VECTOR_MEM_ALTIVEC_OR_VSX_P (GET_MODE (x))
21000 && GET_CODE (tmp) == AND
21001 && CONST_INT_P (XEXP (tmp, 1))
21002 && INTVAL (XEXP (tmp, 1)) == -16)
21003 tmp = XEXP (tmp, 0);
21004 else if (VECTOR_MEM_VSX_P (GET_MODE (x))
21005 && GET_CODE (tmp) == PRE_MODIFY)
21006 tmp = XEXP (tmp, 1);
21007 if (REG_P (tmp))
21008 fprintf (file, "0,%s", reg_names[REGNO (tmp)]);
21009 else
21011 if (GET_CODE (tmp) != PLUS
21012 || !REG_P (XEXP (tmp, 0))
21013 || !REG_P (XEXP (tmp, 1)))
21015 output_operand_lossage ("invalid %%y value, try using the 'Z' constraint");
21016 break;
21019 if (REGNO (XEXP (tmp, 0)) == 0)
21020 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 1)) ],
21021 reg_names[ REGNO (XEXP (tmp, 0)) ]);
21022 else
21023 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 0)) ],
21024 reg_names[ REGNO (XEXP (tmp, 1)) ]);
21026 break;
21029 case 0:
21030 if (REG_P (x))
21031 fprintf (file, "%s", reg_names[REGNO (x)]);
21032 else if (MEM_P (x))
21034 /* We need to handle PRE_INC and PRE_DEC here, since we need to
21035 know the width from the mode. */
21036 if (GET_CODE (XEXP (x, 0)) == PRE_INC)
21037 fprintf (file, "%d(%s)", GET_MODE_SIZE (GET_MODE (x)),
21038 reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
21039 else if (GET_CODE (XEXP (x, 0)) == PRE_DEC)
21040 fprintf (file, "%d(%s)", - GET_MODE_SIZE (GET_MODE (x)),
21041 reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
21042 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
21043 output_address (GET_MODE (x), XEXP (XEXP (x, 0), 1));
21044 else
21045 output_address (GET_MODE (x), XEXP (x, 0));
21047 else if (toc_relative_expr_p (x, false,
21048 &tocrel_base_oac, &tocrel_offset_oac))
21049 /* This hack along with a corresponding hack in
21050 rs6000_output_addr_const_extra arranges to output addends
21051 where the assembler expects to find them. eg.
21052 (plus (unspec [(symbol_ref ("x")) (reg 2)] tocrel) 4)
21053 without this hack would be output as "x@toc+4". We
21054 want "x+4@toc". */
21055 output_addr_const (file, CONST_CAST_RTX (tocrel_base_oac));
21056 else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLSGD)
21057 output_addr_const (file, XVECEXP (x, 0, 0));
21058 else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_PLTSEQ)
21059 output_addr_const (file, XVECEXP (x, 0, 1));
21060 else
21061 output_addr_const (file, x);
21062 return;
21064 case '&':
21065 if (const char *name = get_some_local_dynamic_name ())
21066 assemble_name (file, name);
21067 else
21068 output_operand_lossage ("'%%&' used without any "
21069 "local dynamic TLS references");
21070 return;
21072 default:
21073 output_operand_lossage ("invalid %%xn code");
21077 /* Print the address of an operand. */
21079 void
21080 print_operand_address (FILE *file, rtx x)
21082 if (REG_P (x))
21083 fprintf (file, "0(%s)", reg_names[ REGNO (x) ]);
21084 else if (SYMBOL_REF_P (x) || GET_CODE (x) == CONST
21085 || GET_CODE (x) == LABEL_REF)
21087 output_addr_const (file, x);
21088 if (small_data_operand (x, GET_MODE (x)))
21089 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
21090 reg_names[SMALL_DATA_REG]);
21091 else
21092 gcc_assert (!TARGET_TOC);
21094 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
21095 && REG_P (XEXP (x, 1)))
21097 if (REGNO (XEXP (x, 0)) == 0)
21098 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 1)) ],
21099 reg_names[ REGNO (XEXP (x, 0)) ]);
21100 else
21101 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 0)) ],
21102 reg_names[ REGNO (XEXP (x, 1)) ]);
21104 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
21105 && CONST_INT_P (XEXP (x, 1)))
21106 fprintf (file, HOST_WIDE_INT_PRINT_DEC "(%s)",
21107 INTVAL (XEXP (x, 1)), reg_names[ REGNO (XEXP (x, 0)) ]);
21108 #if TARGET_MACHO
21109 else if (GET_CODE (x) == LO_SUM && REG_P (XEXP (x, 0))
21110 && CONSTANT_P (XEXP (x, 1)))
21112 fprintf (file, "lo16(");
21113 output_addr_const (file, XEXP (x, 1));
21114 fprintf (file, ")(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
21116 #endif
21117 #if TARGET_ELF
21118 else if (GET_CODE (x) == LO_SUM && REG_P (XEXP (x, 0))
21119 && CONSTANT_P (XEXP (x, 1)))
21121 output_addr_const (file, XEXP (x, 1));
21122 fprintf (file, "@l(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
21124 #endif
21125 else if (toc_relative_expr_p (x, false, &tocrel_base_oac, &tocrel_offset_oac))
21127 /* This hack along with a corresponding hack in
21128 rs6000_output_addr_const_extra arranges to output addends
21129 where the assembler expects to find them. eg.
21130 (lo_sum (reg 9)
21131 . (plus (unspec [(symbol_ref ("x")) (reg 2)] tocrel) 8))
21132 without this hack would be output as "x@toc+8@l(9)". We
21133 want "x+8@toc@l(9)". */
21134 output_addr_const (file, CONST_CAST_RTX (tocrel_base_oac));
21135 if (GET_CODE (x) == LO_SUM)
21136 fprintf (file, "@l(%s)", reg_names[REGNO (XEXP (x, 0))]);
21137 else
21138 fprintf (file, "(%s)", reg_names[REGNO (XVECEXP (tocrel_base_oac, 0, 1))]);
21140 else
21141 output_addr_const (file, x);
21144 /* Implement TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA. */
21146 static bool
21147 rs6000_output_addr_const_extra (FILE *file, rtx x)
21149 if (GET_CODE (x) == UNSPEC)
21150 switch (XINT (x, 1))
21152 case UNSPEC_TOCREL:
21153 gcc_checking_assert (SYMBOL_REF_P (XVECEXP (x, 0, 0))
21154 && REG_P (XVECEXP (x, 0, 1))
21155 && REGNO (XVECEXP (x, 0, 1)) == TOC_REGISTER);
21156 output_addr_const (file, XVECEXP (x, 0, 0));
21157 if (x == tocrel_base_oac && tocrel_offset_oac != const0_rtx)
21159 if (INTVAL (tocrel_offset_oac) >= 0)
21160 fprintf (file, "+");
21161 output_addr_const (file, CONST_CAST_RTX (tocrel_offset_oac));
21163 if (!TARGET_AIX || (TARGET_ELF && TARGET_MINIMAL_TOC))
21165 putc ('-', file);
21166 assemble_name (file, toc_label_name);
21167 need_toc_init = 1;
21169 else if (TARGET_ELF)
21170 fputs ("@toc", file);
21171 return true;
21173 #if TARGET_MACHO
21174 case UNSPEC_MACHOPIC_OFFSET:
21175 output_addr_const (file, XVECEXP (x, 0, 0));
21176 putc ('-', file);
21177 machopic_output_function_base_name (file);
21178 return true;
21179 #endif
21181 return false;
21184 /* Target hook for assembling integer objects. The PowerPC version has
21185 to handle fixup entries for relocatable code if RELOCATABLE_NEEDS_FIXUP
21186 is defined. It also needs to handle DI-mode objects on 64-bit
21187 targets. */
21189 static bool
21190 rs6000_assemble_integer (rtx x, unsigned int size, int aligned_p)
21192 #ifdef RELOCATABLE_NEEDS_FIXUP
21193 /* Special handling for SI values. */
21194 if (RELOCATABLE_NEEDS_FIXUP && size == 4 && aligned_p)
21196 static int recurse = 0;
21198 /* For -mrelocatable, we mark all addresses that need to be fixed up in
21199 the .fixup section. Since the TOC section is already relocated, we
21200 don't need to mark it here. We used to skip the text section, but it
21201 should never be valid for relocated addresses to be placed in the text
21202 section. */
21203 if (DEFAULT_ABI == ABI_V4
21204 && (TARGET_RELOCATABLE || flag_pic > 1)
21205 && in_section != toc_section
21206 && !recurse
21207 && !CONST_SCALAR_INT_P (x)
21208 && CONSTANT_P (x))
21210 char buf[256];
21212 recurse = 1;
21213 ASM_GENERATE_INTERNAL_LABEL (buf, "LCP", fixuplabelno);
21214 fixuplabelno++;
21215 ASM_OUTPUT_LABEL (asm_out_file, buf);
21216 fprintf (asm_out_file, "\t.long\t(");
21217 output_addr_const (asm_out_file, x);
21218 fprintf (asm_out_file, ")@fixup\n");
21219 fprintf (asm_out_file, "\t.section\t\".fixup\",\"aw\"\n");
21220 ASM_OUTPUT_ALIGN (asm_out_file, 2);
21221 fprintf (asm_out_file, "\t.long\t");
21222 assemble_name (asm_out_file, buf);
21223 fprintf (asm_out_file, "\n\t.previous\n");
21224 recurse = 0;
21225 return true;
21227 /* Remove initial .'s to turn a -mcall-aixdesc function
21228 address into the address of the descriptor, not the function
21229 itself. */
21230 else if (SYMBOL_REF_P (x)
21231 && XSTR (x, 0)[0] == '.'
21232 && DEFAULT_ABI == ABI_AIX)
21234 const char *name = XSTR (x, 0);
21235 while (*name == '.')
21236 name++;
21238 fprintf (asm_out_file, "\t.long\t%s\n", name);
21239 return true;
21242 #endif /* RELOCATABLE_NEEDS_FIXUP */
21243 return default_assemble_integer (x, size, aligned_p);
21246 /* Return a template string for assembly to emit when making an
21247 external call. FUNOP is the call mem argument operand number. */
21249 static const char *
21250 rs6000_call_template_1 (rtx *operands, unsigned int funop, bool sibcall)
21252 /* -Wformat-overflow workaround, without which gcc thinks that %u
21253 might produce 10 digits. */
21254 gcc_assert (funop <= MAX_RECOG_OPERANDS);
21256 char arg[12];
21257 arg[0] = 0;
21258 if (TARGET_TLS_MARKERS && GET_CODE (operands[funop + 1]) == UNSPEC)
21260 if (XINT (operands[funop + 1], 1) == UNSPEC_TLSGD)
21261 sprintf (arg, "(%%%u@tlsgd)", funop + 1);
21262 else if (XINT (operands[funop + 1], 1) == UNSPEC_TLSLD)
21263 sprintf (arg, "(%%&@tlsld)");
21264 else
21265 gcc_unreachable ();
21268 /* The magic 32768 offset here corresponds to the offset of
21269 r30 in .got2, as given by LCTOC1. See sysv4.h:toc_section. */
21270 char z[11];
21271 sprintf (z, "%%z%u%s", funop,
21272 (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic == 2
21273 ? "+32768" : ""));
21275 static char str[32]; /* 2 spare */
21276 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
21277 sprintf (str, "b%s %s%s%s", sibcall ? "" : "l", z, arg,
21278 sibcall ? "" : "\n\tnop");
21279 else if (DEFAULT_ABI == ABI_V4)
21280 sprintf (str, "b%s %s%s%s", sibcall ? "" : "l", z, arg,
21281 flag_pic ? "@plt" : "");
21282 #if TARGET_MACHO
21283 /* If/when we remove the mlongcall opt, we can share the AIX/ELGv2 case. */
21284 else if (DEFAULT_ABI == ABI_DARWIN)
21286 /* The cookie is in operand func+2. */
21287 gcc_checking_assert (GET_CODE (operands[funop + 2]) == CONST_INT);
21288 int cookie = INTVAL (operands[funop + 2]);
21289 if (cookie & CALL_LONG)
21291 tree funname = get_identifier (XSTR (operands[funop], 0));
21292 tree labelname = get_prev_label (funname);
21293 gcc_checking_assert (labelname && !sibcall);
21295 /* "jbsr foo, L42" is Mach-O for "Link as 'bl foo' if a 'bl'
21296 instruction will reach 'foo', otherwise link as 'bl L42'".
21297 "L42" should be a 'branch island', that will do a far jump to
21298 'foo'. Branch islands are generated in
21299 macho_branch_islands(). */
21300 sprintf (str, "jbsr %%z%u,%.10s", funop,
21301 IDENTIFIER_POINTER (labelname));
21303 else
21304 /* Same as AIX or ELFv2, except to keep backwards compat, no nop
21305 after the call. */
21306 sprintf (str, "b%s %s%s", sibcall ? "" : "l", z, arg);
21308 #endif
21309 else
21310 gcc_unreachable ();
21311 return str;
21314 const char *
21315 rs6000_call_template (rtx *operands, unsigned int funop)
21317 return rs6000_call_template_1 (operands, funop, false);
21320 const char *
21321 rs6000_sibcall_template (rtx *operands, unsigned int funop)
21323 return rs6000_call_template_1 (operands, funop, true);
21326 /* As above, for indirect calls. */
21328 static const char *
21329 rs6000_indirect_call_template_1 (rtx *operands, unsigned int funop,
21330 bool sibcall)
21332 /* -Wformat-overflow workaround, without which gcc thinks that %u
21333 might produce 10 digits. Note that -Wformat-overflow will not
21334 currently warn here for str[], so do not rely on a warning to
21335 ensure str[] is correctly sized. */
21336 gcc_assert (funop <= MAX_RECOG_OPERANDS);
21338 /* Currently, funop is either 0 or 1. The maximum string is always
21339 a !speculate 64-bit __tls_get_addr call.
21341 ABI_AIX:
21342 . 9 ld 2,%3\n\t
21343 . 27 .reloc .,R_PPC64_TLSGD,%2\n\t
21344 . 29 .reloc .,R_PPC64_PLTSEQ,%z1\n\t
21345 . 9 crset 2\n\t
21346 . 27 .reloc .,R_PPC64_TLSGD,%2\n\t
21347 . 30 .reloc .,R_PPC64_PLTCALL,%z1\n\t
21348 . 10 beq%T1l-\n\t
21349 . 10 ld 2,%4(1)
21350 .---
21351 .151
21353 ABI_ELFv2:
21354 . 27 .reloc .,R_PPC64_TLSGD,%2\n\t
21355 . 29 .reloc .,R_PPC64_PLTSEQ,%z1\n\t
21356 . 9 crset 2\n\t
21357 . 27 .reloc .,R_PPC64_TLSGD,%2\n\t
21358 . 30 .reloc .,R_PPC64_PLTCALL,%z1\n\t
21359 . 10 beq%T1l-\n\t
21360 . 10 ld 2,%3(1)
21361 .---
21362 .142
21364 ABI_V4:
21365 . 27 .reloc .,R_PPC64_TLSGD,%2\n\t
21366 . 35 .reloc .,R_PPC64_PLTSEQ,%z1+32768\n\t
21367 . 9 crset 2\n\t
21368 . 27 .reloc .,R_PPC64_TLSGD,%2\n\t
21369 . 36 .reloc .,R_PPC64_PLTCALL,%z1+32768\n\t
21370 . 8 beq%T1l-
21371 .---
21372 .141 */
21373 static char str[160]; /* 8 spare */
21374 char *s = str;
21375 const char *ptrload = TARGET_64BIT ? "d" : "wz";
21377 if (DEFAULT_ABI == ABI_AIX)
21378 s += sprintf (s,
21379 "l%s 2,%%%u\n\t",
21380 ptrload, funop + 2);
21382 /* We don't need the extra code to stop indirect call speculation if
21383 calling via LR. */
21384 bool speculate = (TARGET_MACHO
21385 || rs6000_speculate_indirect_jumps
21386 || (REG_P (operands[funop])
21387 && REGNO (operands[funop]) == LR_REGNO));
21389 if (TARGET_PLTSEQ && GET_CODE (operands[funop]) == UNSPEC)
21391 const char *rel64 = TARGET_64BIT ? "64" : "";
21392 char tls[29];
21393 tls[0] = 0;
21394 if (TARGET_TLS_MARKERS && GET_CODE (operands[funop + 1]) == UNSPEC)
21396 if (XINT (operands[funop + 1], 1) == UNSPEC_TLSGD)
21397 sprintf (tls, ".reloc .,R_PPC%s_TLSGD,%%%u\n\t",
21398 rel64, funop + 1);
21399 else if (XINT (operands[funop + 1], 1) == UNSPEC_TLSLD)
21400 sprintf (tls, ".reloc .,R_PPC%s_TLSLD,%%&\n\t",
21401 rel64);
21402 else
21403 gcc_unreachable ();
21406 const char *addend = (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT
21407 && flag_pic == 2 ? "+32768" : "");
21408 if (!speculate)
21410 s += sprintf (s,
21411 "%s.reloc .,R_PPC%s_PLTSEQ,%%z%u%s\n\t",
21412 tls, rel64, funop, addend);
21413 s += sprintf (s, "crset 2\n\t");
21415 s += sprintf (s,
21416 "%s.reloc .,R_PPC%s_PLTCALL,%%z%u%s\n\t",
21417 tls, rel64, funop, addend);
21419 else if (!speculate)
21420 s += sprintf (s, "crset 2\n\t");
21422 if (DEFAULT_ABI == ABI_AIX)
21424 if (speculate)
21425 sprintf (s,
21426 "b%%T%ul\n\t"
21427 "l%s 2,%%%u(1)",
21428 funop, ptrload, funop + 3);
21429 else
21430 sprintf (s,
21431 "beq%%T%ul-\n\t"
21432 "l%s 2,%%%u(1)",
21433 funop, ptrload, funop + 3);
21435 else if (DEFAULT_ABI == ABI_ELFv2)
21437 if (speculate)
21438 sprintf (s,
21439 "b%%T%ul\n\t"
21440 "l%s 2,%%%u(1)",
21441 funop, ptrload, funop + 2);
21442 else
21443 sprintf (s,
21444 "beq%%T%ul-\n\t"
21445 "l%s 2,%%%u(1)",
21446 funop, ptrload, funop + 2);
21448 else
21450 if (speculate)
21451 sprintf (s,
21452 "b%%T%u%s",
21453 funop, sibcall ? "" : "l");
21454 else
21455 sprintf (s,
21456 "beq%%T%u%s-%s",
21457 funop, sibcall ? "" : "l", sibcall ? "\n\tb $" : "");
21459 return str;
21462 const char *
21463 rs6000_indirect_call_template (rtx *operands, unsigned int funop)
21465 return rs6000_indirect_call_template_1 (operands, funop, false);
21468 const char *
21469 rs6000_indirect_sibcall_template (rtx *operands, unsigned int funop)
21471 return rs6000_indirect_call_template_1 (operands, funop, true);
21474 #if HAVE_AS_PLTSEQ
21475 /* Output indirect call insns.
21476 WHICH is 0 for tocsave, 1 for plt16_ha, 2 for plt16_lo, 3 for mtctr. */
21477 const char *
21478 rs6000_pltseq_template (rtx *operands, int which)
21480 const char *rel64 = TARGET_64BIT ? "64" : "";
21481 char tls[28];
21482 tls[0] = 0;
21483 if (TARGET_TLS_MARKERS && GET_CODE (operands[3]) == UNSPEC)
21485 if (XINT (operands[3], 1) == UNSPEC_TLSGD)
21486 sprintf (tls, ".reloc .,R_PPC%s_TLSGD,%%3\n\t",
21487 rel64);
21488 else if (XINT (operands[3], 1) == UNSPEC_TLSLD)
21489 sprintf (tls, ".reloc .,R_PPC%s_TLSLD,%%&\n\t",
21490 rel64);
21491 else
21492 gcc_unreachable ();
21495 gcc_assert (DEFAULT_ABI == ABI_ELFv2 || DEFAULT_ABI == ABI_V4);
21496 static char str[96]; /* 15 spare */
21497 const char *off = WORDS_BIG_ENDIAN ? "+2" : "";
21498 const char *addend = (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT
21499 && flag_pic == 2 ? "+32768" : "");
21500 switch (which)
21502 case 0:
21503 sprintf (str,
21504 "%s.reloc .,R_PPC%s_PLTSEQ,%%z2\n\t"
21505 "st%s",
21506 tls, rel64, TARGET_64BIT ? "d 2,24(1)" : "w 2,12(1)");
21507 break;
21508 case 1:
21509 if (DEFAULT_ABI == ABI_V4 && !flag_pic)
21510 sprintf (str,
21511 "%s.reloc .%s,R_PPC%s_PLT16_HA,%%z2\n\t"
21512 "lis %%0,0",
21513 tls, off, rel64);
21514 else
21515 sprintf (str,
21516 "%s.reloc .%s,R_PPC%s_PLT16_HA,%%z2%s\n\t"
21517 "addis %%0,%%1,0",
21518 tls, off, rel64, addend);
21519 break;
21520 case 2:
21521 sprintf (str,
21522 "%s.reloc .%s,R_PPC%s_PLT16_LO%s,%%z2%s\n\t"
21523 "l%s %%0,0(%%1)",
21524 tls, off, rel64, TARGET_64BIT ? "_DS" : "", addend,
21525 TARGET_64BIT ? "d" : "wz");
21526 break;
21527 case 3:
21528 sprintf (str,
21529 "%s.reloc .,R_PPC%s_PLTSEQ,%%z2%s\n\t"
21530 "mtctr %%1",
21531 tls, rel64, addend);
21532 break;
21533 default:
21534 gcc_unreachable ();
21536 return str;
21538 #endif
21540 #if defined (HAVE_GAS_HIDDEN) && !TARGET_MACHO
21541 /* Emit an assembler directive to set symbol visibility for DECL to
21542 VISIBILITY_TYPE. */
21544 static void
21545 rs6000_assemble_visibility (tree decl, int vis)
21547 if (TARGET_XCOFF)
21548 return;
21550 /* Functions need to have their entry point symbol visibility set as
21551 well as their descriptor symbol visibility. */
21552 if (DEFAULT_ABI == ABI_AIX
21553 && DOT_SYMBOLS
21554 && TREE_CODE (decl) == FUNCTION_DECL)
21556 static const char * const visibility_types[] = {
21557 NULL, "protected", "hidden", "internal"
21560 const char *name, *type;
21562 name = ((* targetm.strip_name_encoding)
21563 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
21564 type = visibility_types[vis];
21566 fprintf (asm_out_file, "\t.%s\t%s\n", type, name);
21567 fprintf (asm_out_file, "\t.%s\t.%s\n", type, name);
21569 else
21570 default_assemble_visibility (decl, vis);
21572 #endif
21574 enum rtx_code
21575 rs6000_reverse_condition (machine_mode mode, enum rtx_code code)
21577 /* Reversal of FP compares takes care -- an ordered compare
21578 becomes an unordered compare and vice versa. */
21579 if (mode == CCFPmode
21580 && (!flag_finite_math_only
21581 || code == UNLT || code == UNLE || code == UNGT || code == UNGE
21582 || code == UNEQ || code == LTGT))
21583 return reverse_condition_maybe_unordered (code);
21584 else
21585 return reverse_condition (code);
21588 /* Generate a compare for CODE. Return a brand-new rtx that
21589 represents the result of the compare. */
21591 static rtx
21592 rs6000_generate_compare (rtx cmp, machine_mode mode)
21594 machine_mode comp_mode;
21595 rtx compare_result;
21596 enum rtx_code code = GET_CODE (cmp);
21597 rtx op0 = XEXP (cmp, 0);
21598 rtx op1 = XEXP (cmp, 1);
21600 if (!TARGET_FLOAT128_HW && FLOAT128_VECTOR_P (mode))
21601 comp_mode = CCmode;
21602 else if (FLOAT_MODE_P (mode))
21603 comp_mode = CCFPmode;
21604 else if (code == GTU || code == LTU
21605 || code == GEU || code == LEU)
21606 comp_mode = CCUNSmode;
21607 else if ((code == EQ || code == NE)
21608 && unsigned_reg_p (op0)
21609 && (unsigned_reg_p (op1)
21610 || (CONST_INT_P (op1) && INTVAL (op1) != 0)))
21611 /* These are unsigned values, perhaps there will be a later
21612 ordering compare that can be shared with this one. */
21613 comp_mode = CCUNSmode;
21614 else
21615 comp_mode = CCmode;
21617 /* If we have an unsigned compare, make sure we don't have a signed value as
21618 an immediate. */
21619 if (comp_mode == CCUNSmode && CONST_INT_P (op1)
21620 && INTVAL (op1) < 0)
21622 op0 = copy_rtx_if_shared (op0);
21623 op1 = force_reg (GET_MODE (op0), op1);
21624 cmp = gen_rtx_fmt_ee (code, GET_MODE (cmp), op0, op1);
21627 /* First, the compare. */
21628 compare_result = gen_reg_rtx (comp_mode);
21630 /* IEEE 128-bit support in VSX registers when we do not have hardware
21631 support. */
21632 if (!TARGET_FLOAT128_HW && FLOAT128_VECTOR_P (mode))
21634 rtx libfunc = NULL_RTX;
21635 bool check_nan = false;
21636 rtx dest;
21638 switch (code)
21640 case EQ:
21641 case NE:
21642 libfunc = optab_libfunc (eq_optab, mode);
21643 break;
21645 case GT:
21646 case GE:
21647 libfunc = optab_libfunc (ge_optab, mode);
21648 break;
21650 case LT:
21651 case LE:
21652 libfunc = optab_libfunc (le_optab, mode);
21653 break;
21655 case UNORDERED:
21656 case ORDERED:
21657 libfunc = optab_libfunc (unord_optab, mode);
21658 code = (code == UNORDERED) ? NE : EQ;
21659 break;
21661 case UNGE:
21662 case UNGT:
21663 check_nan = true;
21664 libfunc = optab_libfunc (ge_optab, mode);
21665 code = (code == UNGE) ? GE : GT;
21666 break;
21668 case UNLE:
21669 case UNLT:
21670 check_nan = true;
21671 libfunc = optab_libfunc (le_optab, mode);
21672 code = (code == UNLE) ? LE : LT;
21673 break;
21675 case UNEQ:
21676 case LTGT:
21677 check_nan = true;
21678 libfunc = optab_libfunc (eq_optab, mode);
21679 code = (code = UNEQ) ? EQ : NE;
21680 break;
21682 default:
21683 gcc_unreachable ();
21686 gcc_assert (libfunc);
21688 if (!check_nan)
21689 dest = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
21690 SImode, op0, mode, op1, mode);
21692 /* The library signals an exception for signalling NaNs, so we need to
21693 handle isgreater, etc. by first checking isordered. */
21694 else
21696 rtx ne_rtx, normal_dest, unord_dest;
21697 rtx unord_func = optab_libfunc (unord_optab, mode);
21698 rtx join_label = gen_label_rtx ();
21699 rtx join_ref = gen_rtx_LABEL_REF (VOIDmode, join_label);
21700 rtx unord_cmp = gen_reg_rtx (comp_mode);
21703 /* Test for either value being a NaN. */
21704 gcc_assert (unord_func);
21705 unord_dest = emit_library_call_value (unord_func, NULL_RTX, LCT_CONST,
21706 SImode, op0, mode, op1, mode);
21708 /* Set value (0) if either value is a NaN, and jump to the join
21709 label. */
21710 dest = gen_reg_rtx (SImode);
21711 emit_move_insn (dest, const1_rtx);
21712 emit_insn (gen_rtx_SET (unord_cmp,
21713 gen_rtx_COMPARE (comp_mode, unord_dest,
21714 const0_rtx)));
21716 ne_rtx = gen_rtx_NE (comp_mode, unord_cmp, const0_rtx);
21717 emit_jump_insn (gen_rtx_SET (pc_rtx,
21718 gen_rtx_IF_THEN_ELSE (VOIDmode, ne_rtx,
21719 join_ref,
21720 pc_rtx)));
21722 /* Do the normal comparison, knowing that the values are not
21723 NaNs. */
21724 normal_dest = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
21725 SImode, op0, mode, op1, mode);
21727 emit_insn (gen_cstoresi4 (dest,
21728 gen_rtx_fmt_ee (code, SImode, normal_dest,
21729 const0_rtx),
21730 normal_dest, const0_rtx));
21732 /* Join NaN and non-Nan paths. Compare dest against 0. */
21733 emit_label (join_label);
21734 code = NE;
21737 emit_insn (gen_rtx_SET (compare_result,
21738 gen_rtx_COMPARE (comp_mode, dest, const0_rtx)));
21741 else
21743 /* Generate XLC-compatible TFmode compare as PARALLEL with extra
21744 CLOBBERs to match cmptf_internal2 pattern. */
21745 if (comp_mode == CCFPmode && TARGET_XL_COMPAT
21746 && FLOAT128_IBM_P (GET_MODE (op0))
21747 && TARGET_HARD_FLOAT)
21748 emit_insn (gen_rtx_PARALLEL (VOIDmode,
21749 gen_rtvec (10,
21750 gen_rtx_SET (compare_result,
21751 gen_rtx_COMPARE (comp_mode, op0, op1)),
21752 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
21753 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
21754 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
21755 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
21756 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
21757 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
21758 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
21759 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
21760 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (Pmode)))));
21761 else if (GET_CODE (op1) == UNSPEC
21762 && XINT (op1, 1) == UNSPEC_SP_TEST)
21764 rtx op1b = XVECEXP (op1, 0, 0);
21765 comp_mode = CCEQmode;
21766 compare_result = gen_reg_rtx (CCEQmode);
21767 if (TARGET_64BIT)
21768 emit_insn (gen_stack_protect_testdi (compare_result, op0, op1b));
21769 else
21770 emit_insn (gen_stack_protect_testsi (compare_result, op0, op1b));
21772 else
21773 emit_insn (gen_rtx_SET (compare_result,
21774 gen_rtx_COMPARE (comp_mode, op0, op1)));
21777 /* Some kinds of FP comparisons need an OR operation;
21778 under flag_finite_math_only we don't bother. */
21779 if (FLOAT_MODE_P (mode)
21780 && (!FLOAT128_IEEE_P (mode) || TARGET_FLOAT128_HW)
21781 && !flag_finite_math_only
21782 && (code == LE || code == GE
21783 || code == UNEQ || code == LTGT
21784 || code == UNGT || code == UNLT))
21786 enum rtx_code or1, or2;
21787 rtx or1_rtx, or2_rtx, compare2_rtx;
21788 rtx or_result = gen_reg_rtx (CCEQmode);
21790 switch (code)
21792 case LE: or1 = LT; or2 = EQ; break;
21793 case GE: or1 = GT; or2 = EQ; break;
21794 case UNEQ: or1 = UNORDERED; or2 = EQ; break;
21795 case LTGT: or1 = LT; or2 = GT; break;
21796 case UNGT: or1 = UNORDERED; or2 = GT; break;
21797 case UNLT: or1 = UNORDERED; or2 = LT; break;
21798 default: gcc_unreachable ();
21800 validate_condition_mode (or1, comp_mode);
21801 validate_condition_mode (or2, comp_mode);
21802 or1_rtx = gen_rtx_fmt_ee (or1, SImode, compare_result, const0_rtx);
21803 or2_rtx = gen_rtx_fmt_ee (or2, SImode, compare_result, const0_rtx);
21804 compare2_rtx = gen_rtx_COMPARE (CCEQmode,
21805 gen_rtx_IOR (SImode, or1_rtx, or2_rtx),
21806 const_true_rtx);
21807 emit_insn (gen_rtx_SET (or_result, compare2_rtx));
21809 compare_result = or_result;
21810 code = EQ;
21813 validate_condition_mode (code, GET_MODE (compare_result));
21815 return gen_rtx_fmt_ee (code, VOIDmode, compare_result, const0_rtx);
21819 /* Return the diagnostic message string if the binary operation OP is
21820 not permitted on TYPE1 and TYPE2, NULL otherwise. */
21822 static const char*
21823 rs6000_invalid_binary_op (int op ATTRIBUTE_UNUSED,
21824 const_tree type1,
21825 const_tree type2)
21827 machine_mode mode1 = TYPE_MODE (type1);
21828 machine_mode mode2 = TYPE_MODE (type2);
21830 /* For complex modes, use the inner type. */
21831 if (COMPLEX_MODE_P (mode1))
21832 mode1 = GET_MODE_INNER (mode1);
21834 if (COMPLEX_MODE_P (mode2))
21835 mode2 = GET_MODE_INNER (mode2);
21837 /* Don't allow IEEE 754R 128-bit binary floating point and IBM extended
21838 double to intermix unless -mfloat128-convert. */
21839 if (mode1 == mode2)
21840 return NULL;
21842 if (!TARGET_FLOAT128_CVT)
21844 if ((mode1 == KFmode && mode2 == IFmode)
21845 || (mode1 == IFmode && mode2 == KFmode))
21846 return N_("__float128 and __ibm128 cannot be used in the same "
21847 "expression");
21849 if (TARGET_IEEEQUAD
21850 && ((mode1 == IFmode && mode2 == TFmode)
21851 || (mode1 == TFmode && mode2 == IFmode)))
21852 return N_("__ibm128 and long double cannot be used in the same "
21853 "expression");
21855 if (!TARGET_IEEEQUAD
21856 && ((mode1 == KFmode && mode2 == TFmode)
21857 || (mode1 == TFmode && mode2 == KFmode)))
21858 return N_("__float128 and long double cannot be used in the same "
21859 "expression");
21862 return NULL;
21866 /* Expand floating point conversion to/from __float128 and __ibm128. */
21868 void
21869 rs6000_expand_float128_convert (rtx dest, rtx src, bool unsigned_p)
21871 machine_mode dest_mode = GET_MODE (dest);
21872 machine_mode src_mode = GET_MODE (src);
21873 convert_optab cvt = unknown_optab;
21874 bool do_move = false;
21875 rtx libfunc = NULL_RTX;
21876 rtx dest2;
21877 typedef rtx (*rtx_2func_t) (rtx, rtx);
21878 rtx_2func_t hw_convert = (rtx_2func_t)0;
21879 size_t kf_or_tf;
21881 struct hw_conv_t {
21882 rtx_2func_t from_df;
21883 rtx_2func_t from_sf;
21884 rtx_2func_t from_si_sign;
21885 rtx_2func_t from_si_uns;
21886 rtx_2func_t from_di_sign;
21887 rtx_2func_t from_di_uns;
21888 rtx_2func_t to_df;
21889 rtx_2func_t to_sf;
21890 rtx_2func_t to_si_sign;
21891 rtx_2func_t to_si_uns;
21892 rtx_2func_t to_di_sign;
21893 rtx_2func_t to_di_uns;
21894 } hw_conversions[2] = {
21895 /* convertions to/from KFmode */
21897 gen_extenddfkf2_hw, /* KFmode <- DFmode. */
21898 gen_extendsfkf2_hw, /* KFmode <- SFmode. */
21899 gen_float_kfsi2_hw, /* KFmode <- SImode (signed). */
21900 gen_floatuns_kfsi2_hw, /* KFmode <- SImode (unsigned). */
21901 gen_float_kfdi2_hw, /* KFmode <- DImode (signed). */
21902 gen_floatuns_kfdi2_hw, /* KFmode <- DImode (unsigned). */
21903 gen_trunckfdf2_hw, /* DFmode <- KFmode. */
21904 gen_trunckfsf2_hw, /* SFmode <- KFmode. */
21905 gen_fix_kfsi2_hw, /* SImode <- KFmode (signed). */
21906 gen_fixuns_kfsi2_hw, /* SImode <- KFmode (unsigned). */
21907 gen_fix_kfdi2_hw, /* DImode <- KFmode (signed). */
21908 gen_fixuns_kfdi2_hw, /* DImode <- KFmode (unsigned). */
21911 /* convertions to/from TFmode */
21913 gen_extenddftf2_hw, /* TFmode <- DFmode. */
21914 gen_extendsftf2_hw, /* TFmode <- SFmode. */
21915 gen_float_tfsi2_hw, /* TFmode <- SImode (signed). */
21916 gen_floatuns_tfsi2_hw, /* TFmode <- SImode (unsigned). */
21917 gen_float_tfdi2_hw, /* TFmode <- DImode (signed). */
21918 gen_floatuns_tfdi2_hw, /* TFmode <- DImode (unsigned). */
21919 gen_trunctfdf2_hw, /* DFmode <- TFmode. */
21920 gen_trunctfsf2_hw, /* SFmode <- TFmode. */
21921 gen_fix_tfsi2_hw, /* SImode <- TFmode (signed). */
21922 gen_fixuns_tfsi2_hw, /* SImode <- TFmode (unsigned). */
21923 gen_fix_tfdi2_hw, /* DImode <- TFmode (signed). */
21924 gen_fixuns_tfdi2_hw, /* DImode <- TFmode (unsigned). */
21928 if (dest_mode == src_mode)
21929 gcc_unreachable ();
21931 /* Eliminate memory operations. */
21932 if (MEM_P (src))
21933 src = force_reg (src_mode, src);
21935 if (MEM_P (dest))
21937 rtx tmp = gen_reg_rtx (dest_mode);
21938 rs6000_expand_float128_convert (tmp, src, unsigned_p);
21939 rs6000_emit_move (dest, tmp, dest_mode);
21940 return;
21943 /* Convert to IEEE 128-bit floating point. */
21944 if (FLOAT128_IEEE_P (dest_mode))
21946 if (dest_mode == KFmode)
21947 kf_or_tf = 0;
21948 else if (dest_mode == TFmode)
21949 kf_or_tf = 1;
21950 else
21951 gcc_unreachable ();
21953 switch (src_mode)
21955 case E_DFmode:
21956 cvt = sext_optab;
21957 hw_convert = hw_conversions[kf_or_tf].from_df;
21958 break;
21960 case E_SFmode:
21961 cvt = sext_optab;
21962 hw_convert = hw_conversions[kf_or_tf].from_sf;
21963 break;
21965 case E_KFmode:
21966 case E_IFmode:
21967 case E_TFmode:
21968 if (FLOAT128_IBM_P (src_mode))
21969 cvt = sext_optab;
21970 else
21971 do_move = true;
21972 break;
21974 case E_SImode:
21975 if (unsigned_p)
21977 cvt = ufloat_optab;
21978 hw_convert = hw_conversions[kf_or_tf].from_si_uns;
21980 else
21982 cvt = sfloat_optab;
21983 hw_convert = hw_conversions[kf_or_tf].from_si_sign;
21985 break;
21987 case E_DImode:
21988 if (unsigned_p)
21990 cvt = ufloat_optab;
21991 hw_convert = hw_conversions[kf_or_tf].from_di_uns;
21993 else
21995 cvt = sfloat_optab;
21996 hw_convert = hw_conversions[kf_or_tf].from_di_sign;
21998 break;
22000 default:
22001 gcc_unreachable ();
22005 /* Convert from IEEE 128-bit floating point. */
22006 else if (FLOAT128_IEEE_P (src_mode))
22008 if (src_mode == KFmode)
22009 kf_or_tf = 0;
22010 else if (src_mode == TFmode)
22011 kf_or_tf = 1;
22012 else
22013 gcc_unreachable ();
22015 switch (dest_mode)
22017 case E_DFmode:
22018 cvt = trunc_optab;
22019 hw_convert = hw_conversions[kf_or_tf].to_df;
22020 break;
22022 case E_SFmode:
22023 cvt = trunc_optab;
22024 hw_convert = hw_conversions[kf_or_tf].to_sf;
22025 break;
22027 case E_KFmode:
22028 case E_IFmode:
22029 case E_TFmode:
22030 if (FLOAT128_IBM_P (dest_mode))
22031 cvt = trunc_optab;
22032 else
22033 do_move = true;
22034 break;
22036 case E_SImode:
22037 if (unsigned_p)
22039 cvt = ufix_optab;
22040 hw_convert = hw_conversions[kf_or_tf].to_si_uns;
22042 else
22044 cvt = sfix_optab;
22045 hw_convert = hw_conversions[kf_or_tf].to_si_sign;
22047 break;
22049 case E_DImode:
22050 if (unsigned_p)
22052 cvt = ufix_optab;
22053 hw_convert = hw_conversions[kf_or_tf].to_di_uns;
22055 else
22057 cvt = sfix_optab;
22058 hw_convert = hw_conversions[kf_or_tf].to_di_sign;
22060 break;
22062 default:
22063 gcc_unreachable ();
22067 /* Both IBM format. */
22068 else if (FLOAT128_IBM_P (dest_mode) && FLOAT128_IBM_P (src_mode))
22069 do_move = true;
22071 else
22072 gcc_unreachable ();
22074 /* Handle conversion between TFmode/KFmode/IFmode. */
22075 if (do_move)
22076 emit_insn (gen_rtx_SET (dest, gen_rtx_FLOAT_EXTEND (dest_mode, src)));
22078 /* Handle conversion if we have hardware support. */
22079 else if (TARGET_FLOAT128_HW && hw_convert)
22080 emit_insn ((hw_convert) (dest, src));
22082 /* Call an external function to do the conversion. */
22083 else if (cvt != unknown_optab)
22085 libfunc = convert_optab_libfunc (cvt, dest_mode, src_mode);
22086 gcc_assert (libfunc != NULL_RTX);
22088 dest2 = emit_library_call_value (libfunc, dest, LCT_CONST, dest_mode,
22089 src, src_mode);
22091 gcc_assert (dest2 != NULL_RTX);
22092 if (!rtx_equal_p (dest, dest2))
22093 emit_move_insn (dest, dest2);
22096 else
22097 gcc_unreachable ();
22099 return;
22103 /* Emit RTL that sets a register to zero if OP1 and OP2 are equal. SCRATCH
22104 can be used as that dest register. Return the dest register. */
22107 rs6000_emit_eqne (machine_mode mode, rtx op1, rtx op2, rtx scratch)
22109 if (op2 == const0_rtx)
22110 return op1;
22112 if (GET_CODE (scratch) == SCRATCH)
22113 scratch = gen_reg_rtx (mode);
22115 if (logical_operand (op2, mode))
22116 emit_insn (gen_rtx_SET (scratch, gen_rtx_XOR (mode, op1, op2)));
22117 else
22118 emit_insn (gen_rtx_SET (scratch,
22119 gen_rtx_PLUS (mode, op1, negate_rtx (mode, op2))));
22121 return scratch;
22124 void
22125 rs6000_emit_sCOND (machine_mode mode, rtx operands[])
22127 rtx condition_rtx;
22128 machine_mode op_mode;
22129 enum rtx_code cond_code;
22130 rtx result = operands[0];
22132 condition_rtx = rs6000_generate_compare (operands[1], mode);
22133 cond_code = GET_CODE (condition_rtx);
22135 if (cond_code == NE
22136 || cond_code == GE || cond_code == LE
22137 || cond_code == GEU || cond_code == LEU
22138 || cond_code == ORDERED || cond_code == UNGE || cond_code == UNLE)
22140 rtx not_result = gen_reg_rtx (CCEQmode);
22141 rtx not_op, rev_cond_rtx;
22142 machine_mode cc_mode;
22144 cc_mode = GET_MODE (XEXP (condition_rtx, 0));
22146 rev_cond_rtx = gen_rtx_fmt_ee (rs6000_reverse_condition (cc_mode, cond_code),
22147 SImode, XEXP (condition_rtx, 0), const0_rtx);
22148 not_op = gen_rtx_COMPARE (CCEQmode, rev_cond_rtx, const0_rtx);
22149 emit_insn (gen_rtx_SET (not_result, not_op));
22150 condition_rtx = gen_rtx_EQ (VOIDmode, not_result, const0_rtx);
22153 op_mode = GET_MODE (XEXP (operands[1], 0));
22154 if (op_mode == VOIDmode)
22155 op_mode = GET_MODE (XEXP (operands[1], 1));
22157 if (TARGET_POWERPC64 && (op_mode == DImode || FLOAT_MODE_P (mode)))
22159 PUT_MODE (condition_rtx, DImode);
22160 convert_move (result, condition_rtx, 0);
22162 else
22164 PUT_MODE (condition_rtx, SImode);
22165 emit_insn (gen_rtx_SET (result, condition_rtx));
22169 /* Emit a branch of kind CODE to location LOC. */
22171 void
22172 rs6000_emit_cbranch (machine_mode mode, rtx operands[])
22174 rtx condition_rtx, loc_ref;
22176 condition_rtx = rs6000_generate_compare (operands[0], mode);
22177 loc_ref = gen_rtx_LABEL_REF (VOIDmode, operands[3]);
22178 emit_jump_insn (gen_rtx_SET (pc_rtx,
22179 gen_rtx_IF_THEN_ELSE (VOIDmode, condition_rtx,
22180 loc_ref, pc_rtx)));
22183 /* Return the string to output a conditional branch to LABEL, which is
22184 the operand template of the label, or NULL if the branch is really a
22185 conditional return.
22187 OP is the conditional expression. XEXP (OP, 0) is assumed to be a
22188 condition code register and its mode specifies what kind of
22189 comparison we made.
22191 REVERSED is nonzero if we should reverse the sense of the comparison.
22193 INSN is the insn. */
22195 char *
22196 output_cbranch (rtx op, const char *label, int reversed, rtx_insn *insn)
22198 static char string[64];
22199 enum rtx_code code = GET_CODE (op);
22200 rtx cc_reg = XEXP (op, 0);
22201 machine_mode mode = GET_MODE (cc_reg);
22202 int cc_regno = REGNO (cc_reg) - CR0_REGNO;
22203 int need_longbranch = label != NULL && get_attr_length (insn) == 8;
22204 int really_reversed = reversed ^ need_longbranch;
22205 char *s = string;
22206 const char *ccode;
22207 const char *pred;
22208 rtx note;
22210 validate_condition_mode (code, mode);
22212 /* Work out which way this really branches. We could use
22213 reverse_condition_maybe_unordered here always but this
22214 makes the resulting assembler clearer. */
22215 if (really_reversed)
22217 /* Reversal of FP compares takes care -- an ordered compare
22218 becomes an unordered compare and vice versa. */
22219 if (mode == CCFPmode)
22220 code = reverse_condition_maybe_unordered (code);
22221 else
22222 code = reverse_condition (code);
22225 switch (code)
22227 /* Not all of these are actually distinct opcodes, but
22228 we distinguish them for clarity of the resulting assembler. */
22229 case NE: case LTGT:
22230 ccode = "ne"; break;
22231 case EQ: case UNEQ:
22232 ccode = "eq"; break;
22233 case GE: case GEU:
22234 ccode = "ge"; break;
22235 case GT: case GTU: case UNGT:
22236 ccode = "gt"; break;
22237 case LE: case LEU:
22238 ccode = "le"; break;
22239 case LT: case LTU: case UNLT:
22240 ccode = "lt"; break;
22241 case UNORDERED: ccode = "un"; break;
22242 case ORDERED: ccode = "nu"; break;
22243 case UNGE: ccode = "nl"; break;
22244 case UNLE: ccode = "ng"; break;
22245 default:
22246 gcc_unreachable ();
22249 /* Maybe we have a guess as to how likely the branch is. */
22250 pred = "";
22251 note = find_reg_note (insn, REG_BR_PROB, NULL_RTX);
22252 if (note != NULL_RTX)
22254 /* PROB is the difference from 50%. */
22255 int prob = profile_probability::from_reg_br_prob_note (XINT (note, 0))
22256 .to_reg_br_prob_base () - REG_BR_PROB_BASE / 2;
22258 /* Only hint for highly probable/improbable branches on newer cpus when
22259 we have real profile data, as static prediction overrides processor
22260 dynamic prediction. For older cpus we may as well always hint, but
22261 assume not taken for branches that are very close to 50% as a
22262 mispredicted taken branch is more expensive than a
22263 mispredicted not-taken branch. */
22264 if (rs6000_always_hint
22265 || (abs (prob) > REG_BR_PROB_BASE / 100 * 48
22266 && (profile_status_for_fn (cfun) != PROFILE_GUESSED)
22267 && br_prob_note_reliable_p (note)))
22269 if (abs (prob) > REG_BR_PROB_BASE / 20
22270 && ((prob > 0) ^ need_longbranch))
22271 pred = "+";
22272 else
22273 pred = "-";
22277 if (label == NULL)
22278 s += sprintf (s, "b%slr%s ", ccode, pred);
22279 else
22280 s += sprintf (s, "b%s%s ", ccode, pred);
22282 /* We need to escape any '%' characters in the reg_names string.
22283 Assume they'd only be the first character.... */
22284 if (reg_names[cc_regno + CR0_REGNO][0] == '%')
22285 *s++ = '%';
22286 s += sprintf (s, "%s", reg_names[cc_regno + CR0_REGNO]);
22288 if (label != NULL)
22290 /* If the branch distance was too far, we may have to use an
22291 unconditional branch to go the distance. */
22292 if (need_longbranch)
22293 s += sprintf (s, ",$+8\n\tb %s", label);
22294 else
22295 s += sprintf (s, ",%s", label);
22298 return string;
22301 /* Return insn for VSX or Altivec comparisons. */
22303 static rtx
22304 rs6000_emit_vector_compare_inner (enum rtx_code code, rtx op0, rtx op1)
22306 rtx mask;
22307 machine_mode mode = GET_MODE (op0);
22309 switch (code)
22311 default:
22312 break;
22314 case GE:
22315 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
22316 return NULL_RTX;
22317 /* FALLTHRU */
22319 case EQ:
22320 case GT:
22321 case GTU:
22322 case ORDERED:
22323 case UNORDERED:
22324 case UNEQ:
22325 case LTGT:
22326 mask = gen_reg_rtx (mode);
22327 emit_insn (gen_rtx_SET (mask, gen_rtx_fmt_ee (code, mode, op0, op1)));
22328 return mask;
22331 return NULL_RTX;
22334 /* Emit vector compare for operands OP0 and OP1 using code RCODE.
22335 DMODE is expected destination mode. This is a recursive function. */
22337 static rtx
22338 rs6000_emit_vector_compare (enum rtx_code rcode,
22339 rtx op0, rtx op1,
22340 machine_mode dmode)
22342 rtx mask;
22343 bool swap_operands = false;
22344 bool try_again = false;
22346 gcc_assert (VECTOR_UNIT_ALTIVEC_OR_VSX_P (dmode));
22347 gcc_assert (GET_MODE (op0) == GET_MODE (op1));
22349 /* See if the comparison works as is. */
22350 mask = rs6000_emit_vector_compare_inner (rcode, op0, op1);
22351 if (mask)
22352 return mask;
22354 switch (rcode)
22356 case LT:
22357 rcode = GT;
22358 swap_operands = true;
22359 try_again = true;
22360 break;
22361 case LTU:
22362 rcode = GTU;
22363 swap_operands = true;
22364 try_again = true;
22365 break;
22366 case NE:
22367 case UNLE:
22368 case UNLT:
22369 case UNGE:
22370 case UNGT:
22371 /* Invert condition and try again.
22372 e.g., A != B becomes ~(A==B). */
22374 enum rtx_code rev_code;
22375 enum insn_code nor_code;
22376 rtx mask2;
22378 rev_code = reverse_condition_maybe_unordered (rcode);
22379 if (rev_code == UNKNOWN)
22380 return NULL_RTX;
22382 nor_code = optab_handler (one_cmpl_optab, dmode);
22383 if (nor_code == CODE_FOR_nothing)
22384 return NULL_RTX;
22386 mask2 = rs6000_emit_vector_compare (rev_code, op0, op1, dmode);
22387 if (!mask2)
22388 return NULL_RTX;
22390 mask = gen_reg_rtx (dmode);
22391 emit_insn (GEN_FCN (nor_code) (mask, mask2));
22392 return mask;
22394 break;
22395 case GE:
22396 case GEU:
22397 case LE:
22398 case LEU:
22399 /* Try GT/GTU/LT/LTU OR EQ */
22401 rtx c_rtx, eq_rtx;
22402 enum insn_code ior_code;
22403 enum rtx_code new_code;
22405 switch (rcode)
22407 case GE:
22408 new_code = GT;
22409 break;
22411 case GEU:
22412 new_code = GTU;
22413 break;
22415 case LE:
22416 new_code = LT;
22417 break;
22419 case LEU:
22420 new_code = LTU;
22421 break;
22423 default:
22424 gcc_unreachable ();
22427 ior_code = optab_handler (ior_optab, dmode);
22428 if (ior_code == CODE_FOR_nothing)
22429 return NULL_RTX;
22431 c_rtx = rs6000_emit_vector_compare (new_code, op0, op1, dmode);
22432 if (!c_rtx)
22433 return NULL_RTX;
22435 eq_rtx = rs6000_emit_vector_compare (EQ, op0, op1, dmode);
22436 if (!eq_rtx)
22437 return NULL_RTX;
22439 mask = gen_reg_rtx (dmode);
22440 emit_insn (GEN_FCN (ior_code) (mask, c_rtx, eq_rtx));
22441 return mask;
22443 break;
22444 default:
22445 return NULL_RTX;
22448 if (try_again)
22450 if (swap_operands)
22451 std::swap (op0, op1);
22453 mask = rs6000_emit_vector_compare_inner (rcode, op0, op1);
22454 if (mask)
22455 return mask;
22458 /* You only get two chances. */
22459 return NULL_RTX;
22462 /* Emit vector conditional expression. DEST is destination. OP_TRUE and
22463 OP_FALSE are two VEC_COND_EXPR operands. CC_OP0 and CC_OP1 are the two
22464 operands for the relation operation COND. */
22467 rs6000_emit_vector_cond_expr (rtx dest, rtx op_true, rtx op_false,
22468 rtx cond, rtx cc_op0, rtx cc_op1)
22470 machine_mode dest_mode = GET_MODE (dest);
22471 machine_mode mask_mode = GET_MODE (cc_op0);
22472 enum rtx_code rcode = GET_CODE (cond);
22473 machine_mode cc_mode = CCmode;
22474 rtx mask;
22475 rtx cond2;
22476 bool invert_move = false;
22478 if (VECTOR_UNIT_NONE_P (dest_mode))
22479 return 0;
22481 gcc_assert (GET_MODE_SIZE (dest_mode) == GET_MODE_SIZE (mask_mode)
22482 && GET_MODE_NUNITS (dest_mode) == GET_MODE_NUNITS (mask_mode));
22484 switch (rcode)
22486 /* Swap operands if we can, and fall back to doing the operation as
22487 specified, and doing a NOR to invert the test. */
22488 case NE:
22489 case UNLE:
22490 case UNLT:
22491 case UNGE:
22492 case UNGT:
22493 /* Invert condition and try again.
22494 e.g., A = (B != C) ? D : E becomes A = (B == C) ? E : D. */
22495 invert_move = true;
22496 rcode = reverse_condition_maybe_unordered (rcode);
22497 if (rcode == UNKNOWN)
22498 return 0;
22499 break;
22501 case GE:
22502 case LE:
22503 if (GET_MODE_CLASS (mask_mode) == MODE_VECTOR_INT)
22505 /* Invert condition to avoid compound test. */
22506 invert_move = true;
22507 rcode = reverse_condition (rcode);
22509 break;
22511 case GTU:
22512 case GEU:
22513 case LTU:
22514 case LEU:
22515 /* Mark unsigned tests with CCUNSmode. */
22516 cc_mode = CCUNSmode;
22518 /* Invert condition to avoid compound test if necessary. */
22519 if (rcode == GEU || rcode == LEU)
22521 invert_move = true;
22522 rcode = reverse_condition (rcode);
22524 break;
22526 default:
22527 break;
22530 /* Get the vector mask for the given relational operations. */
22531 mask = rs6000_emit_vector_compare (rcode, cc_op0, cc_op1, mask_mode);
22533 if (!mask)
22534 return 0;
22536 if (invert_move)
22537 std::swap (op_true, op_false);
22539 /* Optimize vec1 == vec2, to know the mask generates -1/0. */
22540 if (GET_MODE_CLASS (dest_mode) == MODE_VECTOR_INT
22541 && (GET_CODE (op_true) == CONST_VECTOR
22542 || GET_CODE (op_false) == CONST_VECTOR))
22544 rtx constant_0 = CONST0_RTX (dest_mode);
22545 rtx constant_m1 = CONSTM1_RTX (dest_mode);
22547 if (op_true == constant_m1 && op_false == constant_0)
22549 emit_move_insn (dest, mask);
22550 return 1;
22553 else if (op_true == constant_0 && op_false == constant_m1)
22555 emit_insn (gen_rtx_SET (dest, gen_rtx_NOT (dest_mode, mask)));
22556 return 1;
22559 /* If we can't use the vector comparison directly, perhaps we can use
22560 the mask for the true or false fields, instead of loading up a
22561 constant. */
22562 if (op_true == constant_m1)
22563 op_true = mask;
22565 if (op_false == constant_0)
22566 op_false = mask;
22569 if (!REG_P (op_true) && !SUBREG_P (op_true))
22570 op_true = force_reg (dest_mode, op_true);
22572 if (!REG_P (op_false) && !SUBREG_P (op_false))
22573 op_false = force_reg (dest_mode, op_false);
22575 cond2 = gen_rtx_fmt_ee (NE, cc_mode, gen_lowpart (dest_mode, mask),
22576 CONST0_RTX (dest_mode));
22577 emit_insn (gen_rtx_SET (dest,
22578 gen_rtx_IF_THEN_ELSE (dest_mode,
22579 cond2,
22580 op_true,
22581 op_false)));
22582 return 1;
22585 /* ISA 3.0 (power9) minmax subcase to emit a XSMAXCDP or XSMINCDP instruction
22586 for SF/DF scalars. Move TRUE_COND to DEST if OP of the operands of the last
22587 comparison is nonzero/true, FALSE_COND if it is zero/false. Return 0 if the
22588 hardware has no such operation. */
22590 static int
22591 rs6000_emit_p9_fp_minmax (rtx dest, rtx op, rtx true_cond, rtx false_cond)
22593 enum rtx_code code = GET_CODE (op);
22594 rtx op0 = XEXP (op, 0);
22595 rtx op1 = XEXP (op, 1);
22596 machine_mode compare_mode = GET_MODE (op0);
22597 machine_mode result_mode = GET_MODE (dest);
22598 bool max_p = false;
22600 if (result_mode != compare_mode)
22601 return 0;
22603 if (code == GE || code == GT)
22604 max_p = true;
22605 else if (code == LE || code == LT)
22606 max_p = false;
22607 else
22608 return 0;
22610 if (rtx_equal_p (op0, true_cond) && rtx_equal_p (op1, false_cond))
22613 else if (rtx_equal_p (op1, true_cond) && rtx_equal_p (op0, false_cond))
22614 max_p = !max_p;
22616 else
22617 return 0;
22619 rs6000_emit_minmax (dest, max_p ? SMAX : SMIN, op0, op1);
22620 return 1;
22623 /* ISA 3.0 (power9) conditional move subcase to emit XSCMP{EQ,GE,GT,NE}DP and
22624 XXSEL instructions for SF/DF scalars. Move TRUE_COND to DEST if OP of the
22625 operands of the last comparison is nonzero/true, FALSE_COND if it is
22626 zero/false. Return 0 if the hardware has no such operation. */
22628 static int
22629 rs6000_emit_p9_fp_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
22631 enum rtx_code code = GET_CODE (op);
22632 rtx op0 = XEXP (op, 0);
22633 rtx op1 = XEXP (op, 1);
22634 machine_mode result_mode = GET_MODE (dest);
22635 rtx compare_rtx;
22636 rtx cmove_rtx;
22637 rtx clobber_rtx;
22639 if (!can_create_pseudo_p ())
22640 return 0;
22642 switch (code)
22644 case EQ:
22645 case GE:
22646 case GT:
22647 break;
22649 case NE:
22650 case LT:
22651 case LE:
22652 code = swap_condition (code);
22653 std::swap (op0, op1);
22654 break;
22656 default:
22657 return 0;
22660 /* Generate: [(parallel [(set (dest)
22661 (if_then_else (op (cmp1) (cmp2))
22662 (true)
22663 (false)))
22664 (clobber (scratch))])]. */
22666 compare_rtx = gen_rtx_fmt_ee (code, CCFPmode, op0, op1);
22667 cmove_rtx = gen_rtx_SET (dest,
22668 gen_rtx_IF_THEN_ELSE (result_mode,
22669 compare_rtx,
22670 true_cond,
22671 false_cond));
22673 clobber_rtx = gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (V2DImode));
22674 emit_insn (gen_rtx_PARALLEL (VOIDmode,
22675 gen_rtvec (2, cmove_rtx, clobber_rtx)));
22677 return 1;
22680 /* Emit a conditional move: move TRUE_COND to DEST if OP of the
22681 operands of the last comparison is nonzero/true, FALSE_COND if it
22682 is zero/false. Return 0 if the hardware has no such operation. */
22685 rs6000_emit_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
22687 enum rtx_code code = GET_CODE (op);
22688 rtx op0 = XEXP (op, 0);
22689 rtx op1 = XEXP (op, 1);
22690 machine_mode compare_mode = GET_MODE (op0);
22691 machine_mode result_mode = GET_MODE (dest);
22692 rtx temp;
22693 bool is_against_zero;
22695 /* These modes should always match. */
22696 if (GET_MODE (op1) != compare_mode
22697 /* In the isel case however, we can use a compare immediate, so
22698 op1 may be a small constant. */
22699 && (!TARGET_ISEL || !short_cint_operand (op1, VOIDmode)))
22700 return 0;
22701 if (GET_MODE (true_cond) != result_mode)
22702 return 0;
22703 if (GET_MODE (false_cond) != result_mode)
22704 return 0;
22706 /* See if we can use the ISA 3.0 (power9) min/max/compare functions. */
22707 if (TARGET_P9_MINMAX
22708 && (compare_mode == SFmode || compare_mode == DFmode)
22709 && (result_mode == SFmode || result_mode == DFmode))
22711 if (rs6000_emit_p9_fp_minmax (dest, op, true_cond, false_cond))
22712 return 1;
22714 if (rs6000_emit_p9_fp_cmove (dest, op, true_cond, false_cond))
22715 return 1;
22718 /* Don't allow using floating point comparisons for integer results for
22719 now. */
22720 if (FLOAT_MODE_P (compare_mode) && !FLOAT_MODE_P (result_mode))
22721 return 0;
22723 /* First, work out if the hardware can do this at all, or
22724 if it's too slow.... */
22725 if (!FLOAT_MODE_P (compare_mode))
22727 if (TARGET_ISEL)
22728 return rs6000_emit_int_cmove (dest, op, true_cond, false_cond);
22729 return 0;
22732 is_against_zero = op1 == CONST0_RTX (compare_mode);
22734 /* A floating-point subtract might overflow, underflow, or produce
22735 an inexact result, thus changing the floating-point flags, so it
22736 can't be generated if we care about that. It's safe if one side
22737 of the construct is zero, since then no subtract will be
22738 generated. */
22739 if (SCALAR_FLOAT_MODE_P (compare_mode)
22740 && flag_trapping_math && ! is_against_zero)
22741 return 0;
22743 /* Eliminate half of the comparisons by switching operands, this
22744 makes the remaining code simpler. */
22745 if (code == UNLT || code == UNGT || code == UNORDERED || code == NE
22746 || code == LTGT || code == LT || code == UNLE)
22748 code = reverse_condition_maybe_unordered (code);
22749 temp = true_cond;
22750 true_cond = false_cond;
22751 false_cond = temp;
22754 /* UNEQ and LTGT take four instructions for a comparison with zero,
22755 it'll probably be faster to use a branch here too. */
22756 if (code == UNEQ && HONOR_NANS (compare_mode))
22757 return 0;
22759 /* We're going to try to implement comparisons by performing
22760 a subtract, then comparing against zero. Unfortunately,
22761 Inf - Inf is NaN which is not zero, and so if we don't
22762 know that the operand is finite and the comparison
22763 would treat EQ different to UNORDERED, we can't do it. */
22764 if (HONOR_INFINITIES (compare_mode)
22765 && code != GT && code != UNGE
22766 && (!CONST_DOUBLE_P (op1)
22767 || real_isinf (CONST_DOUBLE_REAL_VALUE (op1)))
22768 /* Constructs of the form (a OP b ? a : b) are safe. */
22769 && ((! rtx_equal_p (op0, false_cond) && ! rtx_equal_p (op1, false_cond))
22770 || (! rtx_equal_p (op0, true_cond)
22771 && ! rtx_equal_p (op1, true_cond))))
22772 return 0;
22774 /* At this point we know we can use fsel. */
22776 /* Reduce the comparison to a comparison against zero. */
22777 if (! is_against_zero)
22779 temp = gen_reg_rtx (compare_mode);
22780 emit_insn (gen_rtx_SET (temp, gen_rtx_MINUS (compare_mode, op0, op1)));
22781 op0 = temp;
22782 op1 = CONST0_RTX (compare_mode);
22785 /* If we don't care about NaNs we can reduce some of the comparisons
22786 down to faster ones. */
22787 if (! HONOR_NANS (compare_mode))
22788 switch (code)
22790 case GT:
22791 code = LE;
22792 temp = true_cond;
22793 true_cond = false_cond;
22794 false_cond = temp;
22795 break;
22796 case UNGE:
22797 code = GE;
22798 break;
22799 case UNEQ:
22800 code = EQ;
22801 break;
22802 default:
22803 break;
22806 /* Now, reduce everything down to a GE. */
22807 switch (code)
22809 case GE:
22810 break;
22812 case LE:
22813 temp = gen_reg_rtx (compare_mode);
22814 emit_insn (gen_rtx_SET (temp, gen_rtx_NEG (compare_mode, op0)));
22815 op0 = temp;
22816 break;
22818 case ORDERED:
22819 temp = gen_reg_rtx (compare_mode);
22820 emit_insn (gen_rtx_SET (temp, gen_rtx_ABS (compare_mode, op0)));
22821 op0 = temp;
22822 break;
22824 case EQ:
22825 temp = gen_reg_rtx (compare_mode);
22826 emit_insn (gen_rtx_SET (temp,
22827 gen_rtx_NEG (compare_mode,
22828 gen_rtx_ABS (compare_mode, op0))));
22829 op0 = temp;
22830 break;
22832 case UNGE:
22833 /* a UNGE 0 <-> (a GE 0 || -a UNLT 0) */
22834 temp = gen_reg_rtx (result_mode);
22835 emit_insn (gen_rtx_SET (temp,
22836 gen_rtx_IF_THEN_ELSE (result_mode,
22837 gen_rtx_GE (VOIDmode,
22838 op0, op1),
22839 true_cond, false_cond)));
22840 false_cond = true_cond;
22841 true_cond = temp;
22843 temp = gen_reg_rtx (compare_mode);
22844 emit_insn (gen_rtx_SET (temp, gen_rtx_NEG (compare_mode, op0)));
22845 op0 = temp;
22846 break;
22848 case GT:
22849 /* a GT 0 <-> (a GE 0 && -a UNLT 0) */
22850 temp = gen_reg_rtx (result_mode);
22851 emit_insn (gen_rtx_SET (temp,
22852 gen_rtx_IF_THEN_ELSE (result_mode,
22853 gen_rtx_GE (VOIDmode,
22854 op0, op1),
22855 true_cond, false_cond)));
22856 true_cond = false_cond;
22857 false_cond = temp;
22859 temp = gen_reg_rtx (compare_mode);
22860 emit_insn (gen_rtx_SET (temp, gen_rtx_NEG (compare_mode, op0)));
22861 op0 = temp;
22862 break;
22864 default:
22865 gcc_unreachable ();
22868 emit_insn (gen_rtx_SET (dest,
22869 gen_rtx_IF_THEN_ELSE (result_mode,
22870 gen_rtx_GE (VOIDmode,
22871 op0, op1),
22872 true_cond, false_cond)));
22873 return 1;
22876 /* Same as above, but for ints (isel). */
22879 rs6000_emit_int_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
22881 rtx condition_rtx, cr;
22882 machine_mode mode = GET_MODE (dest);
22883 enum rtx_code cond_code;
22884 rtx (*isel_func) (rtx, rtx, rtx, rtx, rtx);
22885 bool signedp;
22887 if (mode != SImode && (!TARGET_POWERPC64 || mode != DImode))
22888 return 0;
22890 /* We still have to do the compare, because isel doesn't do a
22891 compare, it just looks at the CRx bits set by a previous compare
22892 instruction. */
22893 condition_rtx = rs6000_generate_compare (op, mode);
22894 cond_code = GET_CODE (condition_rtx);
22895 cr = XEXP (condition_rtx, 0);
22896 signedp = GET_MODE (cr) == CCmode;
22898 isel_func = (mode == SImode
22899 ? (signedp ? gen_isel_signed_si : gen_isel_unsigned_si)
22900 : (signedp ? gen_isel_signed_di : gen_isel_unsigned_di));
22902 switch (cond_code)
22904 case LT: case GT: case LTU: case GTU: case EQ:
22905 /* isel handles these directly. */
22906 break;
22908 default:
22909 /* We need to swap the sense of the comparison. */
22911 std::swap (false_cond, true_cond);
22912 PUT_CODE (condition_rtx, reverse_condition (cond_code));
22914 break;
22917 false_cond = force_reg (mode, false_cond);
22918 if (true_cond != const0_rtx)
22919 true_cond = force_reg (mode, true_cond);
22921 emit_insn (isel_func (dest, condition_rtx, true_cond, false_cond, cr));
22923 return 1;
22926 void
22927 rs6000_emit_minmax (rtx dest, enum rtx_code code, rtx op0, rtx op1)
22929 machine_mode mode = GET_MODE (op0);
22930 enum rtx_code c;
22931 rtx target;
22933 /* VSX/altivec have direct min/max insns. */
22934 if ((code == SMAX || code == SMIN)
22935 && (VECTOR_UNIT_ALTIVEC_OR_VSX_P (mode)
22936 || (mode == SFmode && VECTOR_UNIT_VSX_P (DFmode))))
22938 emit_insn (gen_rtx_SET (dest, gen_rtx_fmt_ee (code, mode, op0, op1)));
22939 return;
22942 if (code == SMAX || code == SMIN)
22943 c = GE;
22944 else
22945 c = GEU;
22947 if (code == SMAX || code == UMAX)
22948 target = emit_conditional_move (dest, c, op0, op1, mode,
22949 op0, op1, mode, 0);
22950 else
22951 target = emit_conditional_move (dest, c, op0, op1, mode,
22952 op1, op0, mode, 0);
22953 gcc_assert (target);
22954 if (target != dest)
22955 emit_move_insn (dest, target);
22958 /* A subroutine of the atomic operation splitters. Jump to LABEL if
22959 COND is true. Mark the jump as unlikely to be taken. */
22961 static void
22962 emit_unlikely_jump (rtx cond, rtx label)
22964 rtx x = gen_rtx_IF_THEN_ELSE (VOIDmode, cond, label, pc_rtx);
22965 rtx_insn *insn = emit_jump_insn (gen_rtx_SET (pc_rtx, x));
22966 add_reg_br_prob_note (insn, profile_probability::very_unlikely ());
22969 /* A subroutine of the atomic operation splitters. Emit a load-locked
22970 instruction in MODE. For QI/HImode, possibly use a pattern than includes
22971 the zero_extend operation. */
22973 static void
22974 emit_load_locked (machine_mode mode, rtx reg, rtx mem)
22976 rtx (*fn) (rtx, rtx) = NULL;
22978 switch (mode)
22980 case E_QImode:
22981 fn = gen_load_lockedqi;
22982 break;
22983 case E_HImode:
22984 fn = gen_load_lockedhi;
22985 break;
22986 case E_SImode:
22987 if (GET_MODE (mem) == QImode)
22988 fn = gen_load_lockedqi_si;
22989 else if (GET_MODE (mem) == HImode)
22990 fn = gen_load_lockedhi_si;
22991 else
22992 fn = gen_load_lockedsi;
22993 break;
22994 case E_DImode:
22995 fn = gen_load_lockeddi;
22996 break;
22997 case E_TImode:
22998 fn = gen_load_lockedti;
22999 break;
23000 default:
23001 gcc_unreachable ();
23003 emit_insn (fn (reg, mem));
23006 /* A subroutine of the atomic operation splitters. Emit a store-conditional
23007 instruction in MODE. */
23009 static void
23010 emit_store_conditional (machine_mode mode, rtx res, rtx mem, rtx val)
23012 rtx (*fn) (rtx, rtx, rtx) = NULL;
23014 switch (mode)
23016 case E_QImode:
23017 fn = gen_store_conditionalqi;
23018 break;
23019 case E_HImode:
23020 fn = gen_store_conditionalhi;
23021 break;
23022 case E_SImode:
23023 fn = gen_store_conditionalsi;
23024 break;
23025 case E_DImode:
23026 fn = gen_store_conditionaldi;
23027 break;
23028 case E_TImode:
23029 fn = gen_store_conditionalti;
23030 break;
23031 default:
23032 gcc_unreachable ();
23035 /* Emit sync before stwcx. to address PPC405 Erratum. */
23036 if (PPC405_ERRATUM77)
23037 emit_insn (gen_hwsync ());
23039 emit_insn (fn (res, mem, val));
23042 /* Expand barriers before and after a load_locked/store_cond sequence. */
23044 static rtx
23045 rs6000_pre_atomic_barrier (rtx mem, enum memmodel model)
23047 rtx addr = XEXP (mem, 0);
23049 if (!legitimate_indirect_address_p (addr, reload_completed)
23050 && !legitimate_indexed_address_p (addr, reload_completed))
23052 addr = force_reg (Pmode, addr);
23053 mem = replace_equiv_address_nv (mem, addr);
23056 switch (model)
23058 case MEMMODEL_RELAXED:
23059 case MEMMODEL_CONSUME:
23060 case MEMMODEL_ACQUIRE:
23061 break;
23062 case MEMMODEL_RELEASE:
23063 case MEMMODEL_ACQ_REL:
23064 emit_insn (gen_lwsync ());
23065 break;
23066 case MEMMODEL_SEQ_CST:
23067 emit_insn (gen_hwsync ());
23068 break;
23069 default:
23070 gcc_unreachable ();
23072 return mem;
23075 static void
23076 rs6000_post_atomic_barrier (enum memmodel model)
23078 switch (model)
23080 case MEMMODEL_RELAXED:
23081 case MEMMODEL_CONSUME:
23082 case MEMMODEL_RELEASE:
23083 break;
23084 case MEMMODEL_ACQUIRE:
23085 case MEMMODEL_ACQ_REL:
23086 case MEMMODEL_SEQ_CST:
23087 emit_insn (gen_isync ());
23088 break;
23089 default:
23090 gcc_unreachable ();
23094 /* A subroutine of the various atomic expanders. For sub-word operations,
23095 we must adjust things to operate on SImode. Given the original MEM,
23096 return a new aligned memory. Also build and return the quantities by
23097 which to shift and mask. */
23099 static rtx
23100 rs6000_adjust_atomic_subword (rtx orig_mem, rtx *pshift, rtx *pmask)
23102 rtx addr, align, shift, mask, mem;
23103 HOST_WIDE_INT shift_mask;
23104 machine_mode mode = GET_MODE (orig_mem);
23106 /* For smaller modes, we have to implement this via SImode. */
23107 shift_mask = (mode == QImode ? 0x18 : 0x10);
23109 addr = XEXP (orig_mem, 0);
23110 addr = force_reg (GET_MODE (addr), addr);
23112 /* Aligned memory containing subword. Generate a new memory. We
23113 do not want any of the existing MEM_ATTR data, as we're now
23114 accessing memory outside the original object. */
23115 align = expand_simple_binop (Pmode, AND, addr, GEN_INT (-4),
23116 NULL_RTX, 1, OPTAB_LIB_WIDEN);
23117 mem = gen_rtx_MEM (SImode, align);
23118 MEM_VOLATILE_P (mem) = MEM_VOLATILE_P (orig_mem);
23119 if (MEM_ALIAS_SET (orig_mem) == ALIAS_SET_MEMORY_BARRIER)
23120 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
23122 /* Shift amount for subword relative to aligned word. */
23123 shift = gen_reg_rtx (SImode);
23124 addr = gen_lowpart (SImode, addr);
23125 rtx tmp = gen_reg_rtx (SImode);
23126 emit_insn (gen_ashlsi3 (tmp, addr, GEN_INT (3)));
23127 emit_insn (gen_andsi3 (shift, tmp, GEN_INT (shift_mask)));
23128 if (BYTES_BIG_ENDIAN)
23129 shift = expand_simple_binop (SImode, XOR, shift, GEN_INT (shift_mask),
23130 shift, 1, OPTAB_LIB_WIDEN);
23131 *pshift = shift;
23133 /* Mask for insertion. */
23134 mask = expand_simple_binop (SImode, ASHIFT, GEN_INT (GET_MODE_MASK (mode)),
23135 shift, NULL_RTX, 1, OPTAB_LIB_WIDEN);
23136 *pmask = mask;
23138 return mem;
23141 /* A subroutine of the various atomic expanders. For sub-word operands,
23142 combine OLDVAL and NEWVAL via MASK. Returns a new pseduo. */
23144 static rtx
23145 rs6000_mask_atomic_subword (rtx oldval, rtx newval, rtx mask)
23147 rtx x;
23149 x = gen_reg_rtx (SImode);
23150 emit_insn (gen_rtx_SET (x, gen_rtx_AND (SImode,
23151 gen_rtx_NOT (SImode, mask),
23152 oldval)));
23154 x = expand_simple_binop (SImode, IOR, newval, x, x, 1, OPTAB_LIB_WIDEN);
23156 return x;
23159 /* A subroutine of the various atomic expanders. For sub-word operands,
23160 extract WIDE to NARROW via SHIFT. */
23162 static void
23163 rs6000_finish_atomic_subword (rtx narrow, rtx wide, rtx shift)
23165 wide = expand_simple_binop (SImode, LSHIFTRT, wide, shift,
23166 wide, 1, OPTAB_LIB_WIDEN);
23167 emit_move_insn (narrow, gen_lowpart (GET_MODE (narrow), wide));
23170 /* Expand an atomic compare and swap operation. */
23172 void
23173 rs6000_expand_atomic_compare_and_swap (rtx operands[])
23175 rtx boolval, retval, mem, oldval, newval, cond;
23176 rtx label1, label2, x, mask, shift;
23177 machine_mode mode, orig_mode;
23178 enum memmodel mod_s, mod_f;
23179 bool is_weak;
23181 boolval = operands[0];
23182 retval = operands[1];
23183 mem = operands[2];
23184 oldval = operands[3];
23185 newval = operands[4];
23186 is_weak = (INTVAL (operands[5]) != 0);
23187 mod_s = memmodel_base (INTVAL (operands[6]));
23188 mod_f = memmodel_base (INTVAL (operands[7]));
23189 orig_mode = mode = GET_MODE (mem);
23191 mask = shift = NULL_RTX;
23192 if (mode == QImode || mode == HImode)
23194 /* Before power8, we didn't have access to lbarx/lharx, so generate a
23195 lwarx and shift/mask operations. With power8, we need to do the
23196 comparison in SImode, but the store is still done in QI/HImode. */
23197 oldval = convert_modes (SImode, mode, oldval, 1);
23199 if (!TARGET_SYNC_HI_QI)
23201 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
23203 /* Shift and mask OLDVAL into position with the word. */
23204 oldval = expand_simple_binop (SImode, ASHIFT, oldval, shift,
23205 NULL_RTX, 1, OPTAB_LIB_WIDEN);
23207 /* Shift and mask NEWVAL into position within the word. */
23208 newval = convert_modes (SImode, mode, newval, 1);
23209 newval = expand_simple_binop (SImode, ASHIFT, newval, shift,
23210 NULL_RTX, 1, OPTAB_LIB_WIDEN);
23213 /* Prepare to adjust the return value. */
23214 retval = gen_reg_rtx (SImode);
23215 mode = SImode;
23217 else if (reg_overlap_mentioned_p (retval, oldval))
23218 oldval = copy_to_reg (oldval);
23220 if (mode != TImode && !reg_or_short_operand (oldval, mode))
23221 oldval = copy_to_mode_reg (mode, oldval);
23223 if (reg_overlap_mentioned_p (retval, newval))
23224 newval = copy_to_reg (newval);
23226 mem = rs6000_pre_atomic_barrier (mem, mod_s);
23228 label1 = NULL_RTX;
23229 if (!is_weak)
23231 label1 = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
23232 emit_label (XEXP (label1, 0));
23234 label2 = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
23236 emit_load_locked (mode, retval, mem);
23238 x = retval;
23239 if (mask)
23240 x = expand_simple_binop (SImode, AND, retval, mask,
23241 NULL_RTX, 1, OPTAB_LIB_WIDEN);
23243 cond = gen_reg_rtx (CCmode);
23244 /* If we have TImode, synthesize a comparison. */
23245 if (mode != TImode)
23246 x = gen_rtx_COMPARE (CCmode, x, oldval);
23247 else
23249 rtx xor1_result = gen_reg_rtx (DImode);
23250 rtx xor2_result = gen_reg_rtx (DImode);
23251 rtx or_result = gen_reg_rtx (DImode);
23252 rtx new_word0 = simplify_gen_subreg (DImode, x, TImode, 0);
23253 rtx new_word1 = simplify_gen_subreg (DImode, x, TImode, 8);
23254 rtx old_word0 = simplify_gen_subreg (DImode, oldval, TImode, 0);
23255 rtx old_word1 = simplify_gen_subreg (DImode, oldval, TImode, 8);
23257 emit_insn (gen_xordi3 (xor1_result, new_word0, old_word0));
23258 emit_insn (gen_xordi3 (xor2_result, new_word1, old_word1));
23259 emit_insn (gen_iordi3 (or_result, xor1_result, xor2_result));
23260 x = gen_rtx_COMPARE (CCmode, or_result, const0_rtx);
23263 emit_insn (gen_rtx_SET (cond, x));
23265 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
23266 emit_unlikely_jump (x, label2);
23268 x = newval;
23269 if (mask)
23270 x = rs6000_mask_atomic_subword (retval, newval, mask);
23272 emit_store_conditional (orig_mode, cond, mem, x);
23274 if (!is_weak)
23276 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
23277 emit_unlikely_jump (x, label1);
23280 if (!is_mm_relaxed (mod_f))
23281 emit_label (XEXP (label2, 0));
23283 rs6000_post_atomic_barrier (mod_s);
23285 if (is_mm_relaxed (mod_f))
23286 emit_label (XEXP (label2, 0));
23288 if (shift)
23289 rs6000_finish_atomic_subword (operands[1], retval, shift);
23290 else if (mode != GET_MODE (operands[1]))
23291 convert_move (operands[1], retval, 1);
23293 /* In all cases, CR0 contains EQ on success, and NE on failure. */
23294 x = gen_rtx_EQ (SImode, cond, const0_rtx);
23295 emit_insn (gen_rtx_SET (boolval, x));
23298 /* Expand an atomic exchange operation. */
23300 void
23301 rs6000_expand_atomic_exchange (rtx operands[])
23303 rtx retval, mem, val, cond;
23304 machine_mode mode;
23305 enum memmodel model;
23306 rtx label, x, mask, shift;
23308 retval = operands[0];
23309 mem = operands[1];
23310 val = operands[2];
23311 model = memmodel_base (INTVAL (operands[3]));
23312 mode = GET_MODE (mem);
23314 mask = shift = NULL_RTX;
23315 if (!TARGET_SYNC_HI_QI && (mode == QImode || mode == HImode))
23317 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
23319 /* Shift and mask VAL into position with the word. */
23320 val = convert_modes (SImode, mode, val, 1);
23321 val = expand_simple_binop (SImode, ASHIFT, val, shift,
23322 NULL_RTX, 1, OPTAB_LIB_WIDEN);
23324 /* Prepare to adjust the return value. */
23325 retval = gen_reg_rtx (SImode);
23326 mode = SImode;
23329 mem = rs6000_pre_atomic_barrier (mem, model);
23331 label = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
23332 emit_label (XEXP (label, 0));
23334 emit_load_locked (mode, retval, mem);
23336 x = val;
23337 if (mask)
23338 x = rs6000_mask_atomic_subword (retval, val, mask);
23340 cond = gen_reg_rtx (CCmode);
23341 emit_store_conditional (mode, cond, mem, x);
23343 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
23344 emit_unlikely_jump (x, label);
23346 rs6000_post_atomic_barrier (model);
23348 if (shift)
23349 rs6000_finish_atomic_subword (operands[0], retval, shift);
23352 /* Expand an atomic fetch-and-operate pattern. CODE is the binary operation
23353 to perform. MEM is the memory on which to operate. VAL is the second
23354 operand of the binary operator. BEFORE and AFTER are optional locations to
23355 return the value of MEM either before of after the operation. MODEL_RTX
23356 is a CONST_INT containing the memory model to use. */
23358 void
23359 rs6000_expand_atomic_op (enum rtx_code code, rtx mem, rtx val,
23360 rtx orig_before, rtx orig_after, rtx model_rtx)
23362 enum memmodel model = memmodel_base (INTVAL (model_rtx));
23363 machine_mode mode = GET_MODE (mem);
23364 machine_mode store_mode = mode;
23365 rtx label, x, cond, mask, shift;
23366 rtx before = orig_before, after = orig_after;
23368 mask = shift = NULL_RTX;
23369 /* On power8, we want to use SImode for the operation. On previous systems,
23370 use the operation in a subword and shift/mask to get the proper byte or
23371 halfword. */
23372 if (mode == QImode || mode == HImode)
23374 if (TARGET_SYNC_HI_QI)
23376 val = convert_modes (SImode, mode, val, 1);
23378 /* Prepare to adjust the return value. */
23379 before = gen_reg_rtx (SImode);
23380 if (after)
23381 after = gen_reg_rtx (SImode);
23382 mode = SImode;
23384 else
23386 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
23388 /* Shift and mask VAL into position with the word. */
23389 val = convert_modes (SImode, mode, val, 1);
23390 val = expand_simple_binop (SImode, ASHIFT, val, shift,
23391 NULL_RTX, 1, OPTAB_LIB_WIDEN);
23393 switch (code)
23395 case IOR:
23396 case XOR:
23397 /* We've already zero-extended VAL. That is sufficient to
23398 make certain that it does not affect other bits. */
23399 mask = NULL;
23400 break;
23402 case AND:
23403 /* If we make certain that all of the other bits in VAL are
23404 set, that will be sufficient to not affect other bits. */
23405 x = gen_rtx_NOT (SImode, mask);
23406 x = gen_rtx_IOR (SImode, x, val);
23407 emit_insn (gen_rtx_SET (val, x));
23408 mask = NULL;
23409 break;
23411 case NOT:
23412 case PLUS:
23413 case MINUS:
23414 /* These will all affect bits outside the field and need
23415 adjustment via MASK within the loop. */
23416 break;
23418 default:
23419 gcc_unreachable ();
23422 /* Prepare to adjust the return value. */
23423 before = gen_reg_rtx (SImode);
23424 if (after)
23425 after = gen_reg_rtx (SImode);
23426 store_mode = mode = SImode;
23430 mem = rs6000_pre_atomic_barrier (mem, model);
23432 label = gen_label_rtx ();
23433 emit_label (label);
23434 label = gen_rtx_LABEL_REF (VOIDmode, label);
23436 if (before == NULL_RTX)
23437 before = gen_reg_rtx (mode);
23439 emit_load_locked (mode, before, mem);
23441 if (code == NOT)
23443 x = expand_simple_binop (mode, AND, before, val,
23444 NULL_RTX, 1, OPTAB_LIB_WIDEN);
23445 after = expand_simple_unop (mode, NOT, x, after, 1);
23447 else
23449 after = expand_simple_binop (mode, code, before, val,
23450 after, 1, OPTAB_LIB_WIDEN);
23453 x = after;
23454 if (mask)
23456 x = expand_simple_binop (SImode, AND, after, mask,
23457 NULL_RTX, 1, OPTAB_LIB_WIDEN);
23458 x = rs6000_mask_atomic_subword (before, x, mask);
23460 else if (store_mode != mode)
23461 x = convert_modes (store_mode, mode, x, 1);
23463 cond = gen_reg_rtx (CCmode);
23464 emit_store_conditional (store_mode, cond, mem, x);
23466 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
23467 emit_unlikely_jump (x, label);
23469 rs6000_post_atomic_barrier (model);
23471 if (shift)
23473 /* QImode/HImode on machines without lbarx/lharx where we do a lwarx and
23474 then do the calcuations in a SImode register. */
23475 if (orig_before)
23476 rs6000_finish_atomic_subword (orig_before, before, shift);
23477 if (orig_after)
23478 rs6000_finish_atomic_subword (orig_after, after, shift);
23480 else if (store_mode != mode)
23482 /* QImode/HImode on machines with lbarx/lharx where we do the native
23483 operation and then do the calcuations in a SImode register. */
23484 if (orig_before)
23485 convert_move (orig_before, before, 1);
23486 if (orig_after)
23487 convert_move (orig_after, after, 1);
23489 else if (orig_after && after != orig_after)
23490 emit_move_insn (orig_after, after);
23493 /* Emit instructions to move SRC to DST. Called by splitters for
23494 multi-register moves. It will emit at most one instruction for
23495 each register that is accessed; that is, it won't emit li/lis pairs
23496 (or equivalent for 64-bit code). One of SRC or DST must be a hard
23497 register. */
23499 void
23500 rs6000_split_multireg_move (rtx dst, rtx src)
23502 /* The register number of the first register being moved. */
23503 int reg;
23504 /* The mode that is to be moved. */
23505 machine_mode mode;
23506 /* The mode that the move is being done in, and its size. */
23507 machine_mode reg_mode;
23508 int reg_mode_size;
23509 /* The number of registers that will be moved. */
23510 int nregs;
23512 reg = REG_P (dst) ? REGNO (dst) : REGNO (src);
23513 mode = GET_MODE (dst);
23514 nregs = hard_regno_nregs (reg, mode);
23515 if (FP_REGNO_P (reg))
23516 reg_mode = DECIMAL_FLOAT_MODE_P (mode) ? DDmode :
23517 (TARGET_HARD_FLOAT ? DFmode : SFmode);
23518 else if (ALTIVEC_REGNO_P (reg))
23519 reg_mode = V16QImode;
23520 else
23521 reg_mode = word_mode;
23522 reg_mode_size = GET_MODE_SIZE (reg_mode);
23524 gcc_assert (reg_mode_size * nregs == GET_MODE_SIZE (mode));
23526 /* TDmode residing in FP registers is special, since the ISA requires that
23527 the lower-numbered word of a register pair is always the most significant
23528 word, even in little-endian mode. This does not match the usual subreg
23529 semantics, so we cannnot use simplify_gen_subreg in those cases. Access
23530 the appropriate constituent registers "by hand" in little-endian mode.
23532 Note we do not need to check for destructive overlap here since TDmode
23533 can only reside in even/odd register pairs. */
23534 if (FP_REGNO_P (reg) && DECIMAL_FLOAT_MODE_P (mode) && !BYTES_BIG_ENDIAN)
23536 rtx p_src, p_dst;
23537 int i;
23539 for (i = 0; i < nregs; i++)
23541 if (REG_P (src) && FP_REGNO_P (REGNO (src)))
23542 p_src = gen_rtx_REG (reg_mode, REGNO (src) + nregs - 1 - i);
23543 else
23544 p_src = simplify_gen_subreg (reg_mode, src, mode,
23545 i * reg_mode_size);
23547 if (REG_P (dst) && FP_REGNO_P (REGNO (dst)))
23548 p_dst = gen_rtx_REG (reg_mode, REGNO (dst) + nregs - 1 - i);
23549 else
23550 p_dst = simplify_gen_subreg (reg_mode, dst, mode,
23551 i * reg_mode_size);
23553 emit_insn (gen_rtx_SET (p_dst, p_src));
23556 return;
23559 if (REG_P (src) && REG_P (dst) && (REGNO (src) < REGNO (dst)))
23561 /* Move register range backwards, if we might have destructive
23562 overlap. */
23563 int i;
23564 for (i = nregs - 1; i >= 0; i--)
23565 emit_insn (gen_rtx_SET (simplify_gen_subreg (reg_mode, dst, mode,
23566 i * reg_mode_size),
23567 simplify_gen_subreg (reg_mode, src, mode,
23568 i * reg_mode_size)));
23570 else
23572 int i;
23573 int j = -1;
23574 bool used_update = false;
23575 rtx restore_basereg = NULL_RTX;
23577 if (MEM_P (src) && INT_REGNO_P (reg))
23579 rtx breg;
23581 if (GET_CODE (XEXP (src, 0)) == PRE_INC
23582 || GET_CODE (XEXP (src, 0)) == PRE_DEC)
23584 rtx delta_rtx;
23585 breg = XEXP (XEXP (src, 0), 0);
23586 delta_rtx = (GET_CODE (XEXP (src, 0)) == PRE_INC
23587 ? GEN_INT (GET_MODE_SIZE (GET_MODE (src)))
23588 : GEN_INT (-GET_MODE_SIZE (GET_MODE (src))));
23589 emit_insn (gen_add3_insn (breg, breg, delta_rtx));
23590 src = replace_equiv_address (src, breg);
23592 else if (! rs6000_offsettable_memref_p (src, reg_mode, true))
23594 if (GET_CODE (XEXP (src, 0)) == PRE_MODIFY)
23596 rtx basereg = XEXP (XEXP (src, 0), 0);
23597 if (TARGET_UPDATE)
23599 rtx ndst = simplify_gen_subreg (reg_mode, dst, mode, 0);
23600 emit_insn (gen_rtx_SET (ndst,
23601 gen_rtx_MEM (reg_mode,
23602 XEXP (src, 0))));
23603 used_update = true;
23605 else
23606 emit_insn (gen_rtx_SET (basereg,
23607 XEXP (XEXP (src, 0), 1)));
23608 src = replace_equiv_address (src, basereg);
23610 else
23612 rtx basereg = gen_rtx_REG (Pmode, reg);
23613 emit_insn (gen_rtx_SET (basereg, XEXP (src, 0)));
23614 src = replace_equiv_address (src, basereg);
23618 breg = XEXP (src, 0);
23619 if (GET_CODE (breg) == PLUS || GET_CODE (breg) == LO_SUM)
23620 breg = XEXP (breg, 0);
23622 /* If the base register we are using to address memory is
23623 also a destination reg, then change that register last. */
23624 if (REG_P (breg)
23625 && REGNO (breg) >= REGNO (dst)
23626 && REGNO (breg) < REGNO (dst) + nregs)
23627 j = REGNO (breg) - REGNO (dst);
23629 else if (MEM_P (dst) && INT_REGNO_P (reg))
23631 rtx breg;
23633 if (GET_CODE (XEXP (dst, 0)) == PRE_INC
23634 || GET_CODE (XEXP (dst, 0)) == PRE_DEC)
23636 rtx delta_rtx;
23637 breg = XEXP (XEXP (dst, 0), 0);
23638 delta_rtx = (GET_CODE (XEXP (dst, 0)) == PRE_INC
23639 ? GEN_INT (GET_MODE_SIZE (GET_MODE (dst)))
23640 : GEN_INT (-GET_MODE_SIZE (GET_MODE (dst))));
23642 /* We have to update the breg before doing the store.
23643 Use store with update, if available. */
23645 if (TARGET_UPDATE)
23647 rtx nsrc = simplify_gen_subreg (reg_mode, src, mode, 0);
23648 emit_insn (TARGET_32BIT
23649 ? (TARGET_POWERPC64
23650 ? gen_movdi_si_update (breg, breg, delta_rtx, nsrc)
23651 : gen_movsi_si_update (breg, breg, delta_rtx, nsrc))
23652 : gen_movdi_di_update (breg, breg, delta_rtx, nsrc));
23653 used_update = true;
23655 else
23656 emit_insn (gen_add3_insn (breg, breg, delta_rtx));
23657 dst = replace_equiv_address (dst, breg);
23659 else if (!rs6000_offsettable_memref_p (dst, reg_mode, true)
23660 && GET_CODE (XEXP (dst, 0)) != LO_SUM)
23662 if (GET_CODE (XEXP (dst, 0)) == PRE_MODIFY)
23664 rtx basereg = XEXP (XEXP (dst, 0), 0);
23665 if (TARGET_UPDATE)
23667 rtx nsrc = simplify_gen_subreg (reg_mode, src, mode, 0);
23668 emit_insn (gen_rtx_SET (gen_rtx_MEM (reg_mode,
23669 XEXP (dst, 0)),
23670 nsrc));
23671 used_update = true;
23673 else
23674 emit_insn (gen_rtx_SET (basereg,
23675 XEXP (XEXP (dst, 0), 1)));
23676 dst = replace_equiv_address (dst, basereg);
23678 else
23680 rtx basereg = XEXP (XEXP (dst, 0), 0);
23681 rtx offsetreg = XEXP (XEXP (dst, 0), 1);
23682 gcc_assert (GET_CODE (XEXP (dst, 0)) == PLUS
23683 && REG_P (basereg)
23684 && REG_P (offsetreg)
23685 && REGNO (basereg) != REGNO (offsetreg));
23686 if (REGNO (basereg) == 0)
23688 rtx tmp = offsetreg;
23689 offsetreg = basereg;
23690 basereg = tmp;
23692 emit_insn (gen_add3_insn (basereg, basereg, offsetreg));
23693 restore_basereg = gen_sub3_insn (basereg, basereg, offsetreg);
23694 dst = replace_equiv_address (dst, basereg);
23697 else if (GET_CODE (XEXP (dst, 0)) != LO_SUM)
23698 gcc_assert (rs6000_offsettable_memref_p (dst, reg_mode, true));
23701 for (i = 0; i < nregs; i++)
23703 /* Calculate index to next subword. */
23704 ++j;
23705 if (j == nregs)
23706 j = 0;
23708 /* If compiler already emitted move of first word by
23709 store with update, no need to do anything. */
23710 if (j == 0 && used_update)
23711 continue;
23713 emit_insn (gen_rtx_SET (simplify_gen_subreg (reg_mode, dst, mode,
23714 j * reg_mode_size),
23715 simplify_gen_subreg (reg_mode, src, mode,
23716 j * reg_mode_size)));
23718 if (restore_basereg != NULL_RTX)
23719 emit_insn (restore_basereg);
23724 /* This page contains routines that are used to determine what the
23725 function prologue and epilogue code will do and write them out. */
23727 /* Determine whether the REG is really used. */
23729 static bool
23730 save_reg_p (int reg)
23732 if (reg == RS6000_PIC_OFFSET_TABLE_REGNUM && !TARGET_SINGLE_PIC_BASE)
23734 /* When calling eh_return, we must return true for all the cases
23735 where conditional_register_usage marks the PIC offset reg
23736 call used or fixed. */
23737 if (crtl->calls_eh_return
23738 && ((DEFAULT_ABI == ABI_V4 && flag_pic)
23739 || (DEFAULT_ABI == ABI_DARWIN && flag_pic)
23740 || (TARGET_TOC && TARGET_MINIMAL_TOC)))
23741 return true;
23743 /* We need to mark the PIC offset register live for the same
23744 conditions as it is set up in rs6000_emit_prologue, or
23745 otherwise it won't be saved before we clobber it. */
23746 if (TARGET_TOC && TARGET_MINIMAL_TOC
23747 && !constant_pool_empty_p ())
23748 return true;
23750 if (DEFAULT_ABI == ABI_V4
23751 && (flag_pic == 1 || (flag_pic && TARGET_SECURE_PLT))
23752 && df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))
23753 return true;
23755 if (DEFAULT_ABI == ABI_DARWIN
23756 && flag_pic && crtl->uses_pic_offset_table)
23757 return true;
23760 return !call_used_regs[reg] && df_regs_ever_live_p (reg);
23763 /* Return the first fixed-point register that is required to be
23764 saved. 32 if none. */
23767 first_reg_to_save (void)
23769 int first_reg;
23771 /* Find lowest numbered live register. */
23772 for (first_reg = 13; first_reg <= 31; first_reg++)
23773 if (save_reg_p (first_reg))
23774 break;
23776 return first_reg;
23779 /* Similar, for FP regs. */
23782 first_fp_reg_to_save (void)
23784 int first_reg;
23786 /* Find lowest numbered live register. */
23787 for (first_reg = 14 + 32; first_reg <= 63; first_reg++)
23788 if (save_reg_p (first_reg))
23789 break;
23791 return first_reg;
23794 /* Similar, for AltiVec regs. */
23796 static int
23797 first_altivec_reg_to_save (void)
23799 int i;
23801 /* Stack frame remains as is unless we are in AltiVec ABI. */
23802 if (! TARGET_ALTIVEC_ABI)
23803 return LAST_ALTIVEC_REGNO + 1;
23805 /* On Darwin, the unwind routines are compiled without
23806 TARGET_ALTIVEC, and use save_world to save/restore the
23807 altivec registers when necessary. */
23808 if (DEFAULT_ABI == ABI_DARWIN && crtl->calls_eh_return
23809 && ! TARGET_ALTIVEC)
23810 return FIRST_ALTIVEC_REGNO + 20;
23812 /* Find lowest numbered live register. */
23813 for (i = FIRST_ALTIVEC_REGNO + 20; i <= LAST_ALTIVEC_REGNO; ++i)
23814 if (save_reg_p (i))
23815 break;
23817 return i;
23820 /* Return a 32-bit mask of the AltiVec registers we need to set in
23821 VRSAVE. Bit n of the return value is 1 if Vn is live. The MSB in
23822 the 32-bit word is 0. */
23824 static unsigned int
23825 compute_vrsave_mask (void)
23827 unsigned int i, mask = 0;
23829 /* On Darwin, the unwind routines are compiled without
23830 TARGET_ALTIVEC, and use save_world to save/restore the
23831 call-saved altivec registers when necessary. */
23832 if (DEFAULT_ABI == ABI_DARWIN && crtl->calls_eh_return
23833 && ! TARGET_ALTIVEC)
23834 mask |= 0xFFF;
23836 /* First, find out if we use _any_ altivec registers. */
23837 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
23838 if (df_regs_ever_live_p (i))
23839 mask |= ALTIVEC_REG_BIT (i);
23841 if (mask == 0)
23842 return mask;
23844 /* Next, remove the argument registers from the set. These must
23845 be in the VRSAVE mask set by the caller, so we don't need to add
23846 them in again. More importantly, the mask we compute here is
23847 used to generate CLOBBERs in the set_vrsave insn, and we do not
23848 wish the argument registers to die. */
23849 for (i = ALTIVEC_ARG_MIN_REG; i < (unsigned) crtl->args.info.vregno; i++)
23850 mask &= ~ALTIVEC_REG_BIT (i);
23852 /* Similarly, remove the return value from the set. */
23854 bool yes = false;
23855 diddle_return_value (is_altivec_return_reg, &yes);
23856 if (yes)
23857 mask &= ~ALTIVEC_REG_BIT (ALTIVEC_ARG_RETURN);
23860 return mask;
23863 /* For a very restricted set of circumstances, we can cut down the
23864 size of prologues/epilogues by calling our own save/restore-the-world
23865 routines. */
23867 static void
23868 compute_save_world_info (rs6000_stack_t *info)
23870 info->world_save_p = 1;
23871 info->world_save_p
23872 = (WORLD_SAVE_P (info)
23873 && DEFAULT_ABI == ABI_DARWIN
23874 && !cfun->has_nonlocal_label
23875 && info->first_fp_reg_save == FIRST_SAVED_FP_REGNO
23876 && info->first_gp_reg_save == FIRST_SAVED_GP_REGNO
23877 && info->first_altivec_reg_save == FIRST_SAVED_ALTIVEC_REGNO
23878 && info->cr_save_p);
23880 /* This will not work in conjunction with sibcalls. Make sure there
23881 are none. (This check is expensive, but seldom executed.) */
23882 if (WORLD_SAVE_P (info))
23884 rtx_insn *insn;
23885 for (insn = get_last_insn_anywhere (); insn; insn = PREV_INSN (insn))
23886 if (CALL_P (insn) && SIBLING_CALL_P (insn))
23888 info->world_save_p = 0;
23889 break;
23893 if (WORLD_SAVE_P (info))
23895 /* Even if we're not touching VRsave, make sure there's room on the
23896 stack for it, if it looks like we're calling SAVE_WORLD, which
23897 will attempt to save it. */
23898 info->vrsave_size = 4;
23900 /* If we are going to save the world, we need to save the link register too. */
23901 info->lr_save_p = 1;
23903 /* "Save" the VRsave register too if we're saving the world. */
23904 if (info->vrsave_mask == 0)
23905 info->vrsave_mask = compute_vrsave_mask ();
23907 /* Because the Darwin register save/restore routines only handle
23908 F14 .. F31 and V20 .. V31 as per the ABI, perform a consistency
23909 check. */
23910 gcc_assert (info->first_fp_reg_save >= FIRST_SAVED_FP_REGNO
23911 && (info->first_altivec_reg_save
23912 >= FIRST_SAVED_ALTIVEC_REGNO));
23915 return;
23919 static void
23920 is_altivec_return_reg (rtx reg, void *xyes)
23922 bool *yes = (bool *) xyes;
23923 if (REGNO (reg) == ALTIVEC_ARG_RETURN)
23924 *yes = true;
23928 /* Return whether REG is a global user reg or has been specifed by
23929 -ffixed-REG. We should not restore these, and so cannot use
23930 lmw or out-of-line restore functions if there are any. We also
23931 can't save them (well, emit frame notes for them), because frame
23932 unwinding during exception handling will restore saved registers. */
23934 static bool
23935 fixed_reg_p (int reg)
23937 /* Ignore fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] when the
23938 backend sets it, overriding anything the user might have given. */
23939 if (reg == RS6000_PIC_OFFSET_TABLE_REGNUM
23940 && ((DEFAULT_ABI == ABI_V4 && flag_pic)
23941 || (DEFAULT_ABI == ABI_DARWIN && flag_pic)
23942 || (TARGET_TOC && TARGET_MINIMAL_TOC)))
23943 return false;
23945 return fixed_regs[reg];
23948 /* Determine the strategy for savings/restoring registers. */
23950 enum {
23951 SAVE_MULTIPLE = 0x1,
23952 SAVE_INLINE_GPRS = 0x2,
23953 SAVE_INLINE_FPRS = 0x4,
23954 SAVE_NOINLINE_GPRS_SAVES_LR = 0x8,
23955 SAVE_NOINLINE_FPRS_SAVES_LR = 0x10,
23956 SAVE_INLINE_VRS = 0x20,
23957 REST_MULTIPLE = 0x100,
23958 REST_INLINE_GPRS = 0x200,
23959 REST_INLINE_FPRS = 0x400,
23960 REST_NOINLINE_FPRS_DOESNT_RESTORE_LR = 0x800,
23961 REST_INLINE_VRS = 0x1000
23964 static int
23965 rs6000_savres_strategy (rs6000_stack_t *info,
23966 bool using_static_chain_p)
23968 int strategy = 0;
23970 /* Select between in-line and out-of-line save and restore of regs.
23971 First, all the obvious cases where we don't use out-of-line. */
23972 if (crtl->calls_eh_return
23973 || cfun->machine->ra_need_lr)
23974 strategy |= (SAVE_INLINE_FPRS | REST_INLINE_FPRS
23975 | SAVE_INLINE_GPRS | REST_INLINE_GPRS
23976 | SAVE_INLINE_VRS | REST_INLINE_VRS);
23978 if (info->first_gp_reg_save == 32)
23979 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
23981 if (info->first_fp_reg_save == 64)
23982 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
23984 if (info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1)
23985 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
23987 /* Define cutoff for using out-of-line functions to save registers. */
23988 if (DEFAULT_ABI == ABI_V4 || TARGET_ELF)
23990 if (!optimize_size)
23992 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
23993 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
23994 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
23996 else
23998 /* Prefer out-of-line restore if it will exit. */
23999 if (info->first_fp_reg_save > 61)
24000 strategy |= SAVE_INLINE_FPRS;
24001 if (info->first_gp_reg_save > 29)
24003 if (info->first_fp_reg_save == 64)
24004 strategy |= SAVE_INLINE_GPRS;
24005 else
24006 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
24008 if (info->first_altivec_reg_save == LAST_ALTIVEC_REGNO)
24009 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
24012 else if (DEFAULT_ABI == ABI_DARWIN)
24014 if (info->first_fp_reg_save > 60)
24015 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
24016 if (info->first_gp_reg_save > 29)
24017 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
24018 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
24020 else
24022 gcc_checking_assert (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2);
24023 if ((flag_shrink_wrap_separate && optimize_function_for_speed_p (cfun))
24024 || info->first_fp_reg_save > 61)
24025 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
24026 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
24027 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
24030 /* Don't bother to try to save things out-of-line if r11 is occupied
24031 by the static chain. It would require too much fiddling and the
24032 static chain is rarely used anyway. FPRs are saved w.r.t the stack
24033 pointer on Darwin, and AIX uses r1 or r12. */
24034 if (using_static_chain_p
24035 && (DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN))
24036 strategy |= ((DEFAULT_ABI == ABI_DARWIN ? 0 : SAVE_INLINE_FPRS)
24037 | SAVE_INLINE_GPRS
24038 | SAVE_INLINE_VRS);
24040 /* Don't ever restore fixed regs. That means we can't use the
24041 out-of-line register restore functions if a fixed reg is in the
24042 range of regs restored. */
24043 if (!(strategy & REST_INLINE_FPRS))
24044 for (int i = info->first_fp_reg_save; i < 64; i++)
24045 if (fixed_regs[i])
24047 strategy |= REST_INLINE_FPRS;
24048 break;
24051 /* We can only use the out-of-line routines to restore fprs if we've
24052 saved all the registers from first_fp_reg_save in the prologue.
24053 Otherwise, we risk loading garbage. Of course, if we have saved
24054 out-of-line then we know we haven't skipped any fprs. */
24055 if ((strategy & SAVE_INLINE_FPRS)
24056 && !(strategy & REST_INLINE_FPRS))
24057 for (int i = info->first_fp_reg_save; i < 64; i++)
24058 if (!save_reg_p (i))
24060 strategy |= REST_INLINE_FPRS;
24061 break;
24064 /* Similarly, for altivec regs. */
24065 if (!(strategy & REST_INLINE_VRS))
24066 for (int i = info->first_altivec_reg_save; i < LAST_ALTIVEC_REGNO + 1; i++)
24067 if (fixed_regs[i])
24069 strategy |= REST_INLINE_VRS;
24070 break;
24073 if ((strategy & SAVE_INLINE_VRS)
24074 && !(strategy & REST_INLINE_VRS))
24075 for (int i = info->first_altivec_reg_save; i < LAST_ALTIVEC_REGNO + 1; i++)
24076 if (!save_reg_p (i))
24078 strategy |= REST_INLINE_VRS;
24079 break;
24082 /* info->lr_save_p isn't yet set if the only reason lr needs to be
24083 saved is an out-of-line save or restore. Set up the value for
24084 the next test (excluding out-of-line gprs). */
24085 bool lr_save_p = (info->lr_save_p
24086 || !(strategy & SAVE_INLINE_FPRS)
24087 || !(strategy & SAVE_INLINE_VRS)
24088 || !(strategy & REST_INLINE_FPRS)
24089 || !(strategy & REST_INLINE_VRS));
24091 if (TARGET_MULTIPLE
24092 && !TARGET_POWERPC64
24093 && info->first_gp_reg_save < 31
24094 && !(flag_shrink_wrap
24095 && flag_shrink_wrap_separate
24096 && optimize_function_for_speed_p (cfun)))
24098 int count = 0;
24099 for (int i = info->first_gp_reg_save; i < 32; i++)
24100 if (save_reg_p (i))
24101 count++;
24103 if (count <= 1)
24104 /* Don't use store multiple if only one reg needs to be
24105 saved. This can occur for example when the ABI_V4 pic reg
24106 (r30) needs to be saved to make calls, but r31 is not
24107 used. */
24108 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
24109 else
24111 /* Prefer store multiple for saves over out-of-line
24112 routines, since the store-multiple instruction will
24113 always be smaller. */
24114 strategy |= SAVE_INLINE_GPRS | SAVE_MULTIPLE;
24116 /* The situation is more complicated with load multiple.
24117 We'd prefer to use the out-of-line routines for restores,
24118 since the "exit" out-of-line routines can handle the
24119 restore of LR and the frame teardown. However if doesn't
24120 make sense to use the out-of-line routine if that is the
24121 only reason we'd need to save LR, and we can't use the
24122 "exit" out-of-line gpr restore if we have saved some
24123 fprs; In those cases it is advantageous to use load
24124 multiple when available. */
24125 if (info->first_fp_reg_save != 64 || !lr_save_p)
24126 strategy |= REST_INLINE_GPRS | REST_MULTIPLE;
24130 /* Using the "exit" out-of-line routine does not improve code size
24131 if using it would require lr to be saved and if only saving one
24132 or two gprs. */
24133 else if (!lr_save_p && info->first_gp_reg_save > 29)
24134 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
24136 /* Don't ever restore fixed regs. */
24137 if ((strategy & (REST_INLINE_GPRS | REST_MULTIPLE)) != REST_INLINE_GPRS)
24138 for (int i = info->first_gp_reg_save; i < 32; i++)
24139 if (fixed_reg_p (i))
24141 strategy |= REST_INLINE_GPRS;
24142 strategy &= ~REST_MULTIPLE;
24143 break;
24146 /* We can only use load multiple or the out-of-line routines to
24147 restore gprs if we've saved all the registers from
24148 first_gp_reg_save. Otherwise, we risk loading garbage.
24149 Of course, if we have saved out-of-line or used stmw then we know
24150 we haven't skipped any gprs. */
24151 if ((strategy & (SAVE_INLINE_GPRS | SAVE_MULTIPLE)) == SAVE_INLINE_GPRS
24152 && (strategy & (REST_INLINE_GPRS | REST_MULTIPLE)) != REST_INLINE_GPRS)
24153 for (int i = info->first_gp_reg_save; i < 32; i++)
24154 if (!save_reg_p (i))
24156 strategy |= REST_INLINE_GPRS;
24157 strategy &= ~REST_MULTIPLE;
24158 break;
24161 if (TARGET_ELF && TARGET_64BIT)
24163 if (!(strategy & SAVE_INLINE_FPRS))
24164 strategy |= SAVE_NOINLINE_FPRS_SAVES_LR;
24165 else if (!(strategy & SAVE_INLINE_GPRS)
24166 && info->first_fp_reg_save == 64)
24167 strategy |= SAVE_NOINLINE_GPRS_SAVES_LR;
24169 else if (TARGET_AIX && !(strategy & REST_INLINE_FPRS))
24170 strategy |= REST_NOINLINE_FPRS_DOESNT_RESTORE_LR;
24172 if (TARGET_MACHO && !(strategy & SAVE_INLINE_FPRS))
24173 strategy |= SAVE_NOINLINE_FPRS_SAVES_LR;
24175 return strategy;
24178 /* Calculate the stack information for the current function. This is
24179 complicated by having two separate calling sequences, the AIX calling
24180 sequence and the V.4 calling sequence.
24182 AIX (and Darwin/Mac OS X) stack frames look like:
24183 32-bit 64-bit
24184 SP----> +---------------------------------------+
24185 | back chain to caller | 0 0
24186 +---------------------------------------+
24187 | saved CR | 4 8 (8-11)
24188 +---------------------------------------+
24189 | saved LR | 8 16
24190 +---------------------------------------+
24191 | reserved for compilers | 12 24
24192 +---------------------------------------+
24193 | reserved for binders | 16 32
24194 +---------------------------------------+
24195 | saved TOC pointer | 20 40
24196 +---------------------------------------+
24197 | Parameter save area (+padding*) (P) | 24 48
24198 +---------------------------------------+
24199 | Alloca space (A) | 24+P etc.
24200 +---------------------------------------+
24201 | Local variable space (L) | 24+P+A
24202 +---------------------------------------+
24203 | Float/int conversion temporary (X) | 24+P+A+L
24204 +---------------------------------------+
24205 | Save area for AltiVec registers (W) | 24+P+A+L+X
24206 +---------------------------------------+
24207 | AltiVec alignment padding (Y) | 24+P+A+L+X+W
24208 +---------------------------------------+
24209 | Save area for VRSAVE register (Z) | 24+P+A+L+X+W+Y
24210 +---------------------------------------+
24211 | Save area for GP registers (G) | 24+P+A+X+L+X+W+Y+Z
24212 +---------------------------------------+
24213 | Save area for FP registers (F) | 24+P+A+X+L+X+W+Y+Z+G
24214 +---------------------------------------+
24215 old SP->| back chain to caller's caller |
24216 +---------------------------------------+
24218 * If the alloca area is present, the parameter save area is
24219 padded so that the former starts 16-byte aligned.
24221 The required alignment for AIX configurations is two words (i.e., 8
24222 or 16 bytes).
24224 The ELFv2 ABI is a variant of the AIX ABI. Stack frames look like:
24226 SP----> +---------------------------------------+
24227 | Back chain to caller | 0
24228 +---------------------------------------+
24229 | Save area for CR | 8
24230 +---------------------------------------+
24231 | Saved LR | 16
24232 +---------------------------------------+
24233 | Saved TOC pointer | 24
24234 +---------------------------------------+
24235 | Parameter save area (+padding*) (P) | 32
24236 +---------------------------------------+
24237 | Alloca space (A) | 32+P
24238 +---------------------------------------+
24239 | Local variable space (L) | 32+P+A
24240 +---------------------------------------+
24241 | Save area for AltiVec registers (W) | 32+P+A+L
24242 +---------------------------------------+
24243 | AltiVec alignment padding (Y) | 32+P+A+L+W
24244 +---------------------------------------+
24245 | Save area for GP registers (G) | 32+P+A+L+W+Y
24246 +---------------------------------------+
24247 | Save area for FP registers (F) | 32+P+A+L+W+Y+G
24248 +---------------------------------------+
24249 old SP->| back chain to caller's caller | 32+P+A+L+W+Y+G+F
24250 +---------------------------------------+
24252 * If the alloca area is present, the parameter save area is
24253 padded so that the former starts 16-byte aligned.
24255 V.4 stack frames look like:
24257 SP----> +---------------------------------------+
24258 | back chain to caller | 0
24259 +---------------------------------------+
24260 | caller's saved LR | 4
24261 +---------------------------------------+
24262 | Parameter save area (+padding*) (P) | 8
24263 +---------------------------------------+
24264 | Alloca space (A) | 8+P
24265 +---------------------------------------+
24266 | Varargs save area (V) | 8+P+A
24267 +---------------------------------------+
24268 | Local variable space (L) | 8+P+A+V
24269 +---------------------------------------+
24270 | Float/int conversion temporary (X) | 8+P+A+V+L
24271 +---------------------------------------+
24272 | Save area for AltiVec registers (W) | 8+P+A+V+L+X
24273 +---------------------------------------+
24274 | AltiVec alignment padding (Y) | 8+P+A+V+L+X+W
24275 +---------------------------------------+
24276 | Save area for VRSAVE register (Z) | 8+P+A+V+L+X+W+Y
24277 +---------------------------------------+
24278 | saved CR (C) | 8+P+A+V+L+X+W+Y+Z
24279 +---------------------------------------+
24280 | Save area for GP registers (G) | 8+P+A+V+L+X+W+Y+Z+C
24281 +---------------------------------------+
24282 | Save area for FP registers (F) | 8+P+A+V+L+X+W+Y+Z+C+G
24283 +---------------------------------------+
24284 old SP->| back chain to caller's caller |
24285 +---------------------------------------+
24287 * If the alloca area is present and the required alignment is
24288 16 bytes, the parameter save area is padded so that the
24289 alloca area starts 16-byte aligned.
24291 The required alignment for V.4 is 16 bytes, or 8 bytes if -meabi is
24292 given. (But note below and in sysv4.h that we require only 8 and
24293 may round up the size of our stack frame anyways. The historical
24294 reason is early versions of powerpc-linux which didn't properly
24295 align the stack at program startup. A happy side-effect is that
24296 -mno-eabi libraries can be used with -meabi programs.)
24298 The EABI configuration defaults to the V.4 layout. However,
24299 the stack alignment requirements may differ. If -mno-eabi is not
24300 given, the required stack alignment is 8 bytes; if -mno-eabi is
24301 given, the required alignment is 16 bytes. (But see V.4 comment
24302 above.) */
24304 #ifndef ABI_STACK_BOUNDARY
24305 #define ABI_STACK_BOUNDARY STACK_BOUNDARY
24306 #endif
24308 static rs6000_stack_t *
24309 rs6000_stack_info (void)
24311 /* We should never be called for thunks, we are not set up for that. */
24312 gcc_assert (!cfun->is_thunk);
24314 rs6000_stack_t *info = &stack_info;
24315 int reg_size = TARGET_32BIT ? 4 : 8;
24316 int ehrd_size;
24317 int ehcr_size;
24318 int save_align;
24319 int first_gp;
24320 HOST_WIDE_INT non_fixed_size;
24321 bool using_static_chain_p;
24323 if (reload_completed && info->reload_completed)
24324 return info;
24326 memset (info, 0, sizeof (*info));
24327 info->reload_completed = reload_completed;
24329 /* Select which calling sequence. */
24330 info->abi = DEFAULT_ABI;
24332 /* Calculate which registers need to be saved & save area size. */
24333 info->first_gp_reg_save = first_reg_to_save ();
24334 /* Assume that we will have to save RS6000_PIC_OFFSET_TABLE_REGNUM,
24335 even if it currently looks like we won't. Reload may need it to
24336 get at a constant; if so, it will have already created a constant
24337 pool entry for it. */
24338 if (((TARGET_TOC && TARGET_MINIMAL_TOC)
24339 || (flag_pic == 1 && DEFAULT_ABI == ABI_V4)
24340 || (flag_pic && DEFAULT_ABI == ABI_DARWIN))
24341 && crtl->uses_const_pool
24342 && info->first_gp_reg_save > RS6000_PIC_OFFSET_TABLE_REGNUM)
24343 first_gp = RS6000_PIC_OFFSET_TABLE_REGNUM;
24344 else
24345 first_gp = info->first_gp_reg_save;
24347 info->gp_size = reg_size * (32 - first_gp);
24349 info->first_fp_reg_save = first_fp_reg_to_save ();
24350 info->fp_size = 8 * (64 - info->first_fp_reg_save);
24352 info->first_altivec_reg_save = first_altivec_reg_to_save ();
24353 info->altivec_size = 16 * (LAST_ALTIVEC_REGNO + 1
24354 - info->first_altivec_reg_save);
24356 /* Does this function call anything? */
24357 info->calls_p = (!crtl->is_leaf || cfun->machine->ra_needs_full_frame);
24359 /* Determine if we need to save the condition code registers. */
24360 if (save_reg_p (CR2_REGNO)
24361 || save_reg_p (CR3_REGNO)
24362 || save_reg_p (CR4_REGNO))
24364 info->cr_save_p = 1;
24365 if (DEFAULT_ABI == ABI_V4)
24366 info->cr_size = reg_size;
24369 /* If the current function calls __builtin_eh_return, then we need
24370 to allocate stack space for registers that will hold data for
24371 the exception handler. */
24372 if (crtl->calls_eh_return)
24374 unsigned int i;
24375 for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; ++i)
24376 continue;
24378 ehrd_size = i * UNITS_PER_WORD;
24380 else
24381 ehrd_size = 0;
24383 /* In the ELFv2 ABI, we also need to allocate space for separate
24384 CR field save areas if the function calls __builtin_eh_return. */
24385 if (DEFAULT_ABI == ABI_ELFv2 && crtl->calls_eh_return)
24387 /* This hard-codes that we have three call-saved CR fields. */
24388 ehcr_size = 3 * reg_size;
24389 /* We do *not* use the regular CR save mechanism. */
24390 info->cr_save_p = 0;
24392 else
24393 ehcr_size = 0;
24395 /* Determine various sizes. */
24396 info->reg_size = reg_size;
24397 info->fixed_size = RS6000_SAVE_AREA;
24398 info->vars_size = RS6000_ALIGN (get_frame_size (), 8);
24399 if (cfun->calls_alloca)
24400 info->parm_size =
24401 RS6000_ALIGN (crtl->outgoing_args_size + info->fixed_size,
24402 STACK_BOUNDARY / BITS_PER_UNIT) - info->fixed_size;
24403 else
24404 info->parm_size = RS6000_ALIGN (crtl->outgoing_args_size,
24405 TARGET_ALTIVEC ? 16 : 8);
24406 if (FRAME_GROWS_DOWNWARD)
24407 info->vars_size
24408 += RS6000_ALIGN (info->fixed_size + info->vars_size + info->parm_size,
24409 ABI_STACK_BOUNDARY / BITS_PER_UNIT)
24410 - (info->fixed_size + info->vars_size + info->parm_size);
24412 if (TARGET_ALTIVEC_ABI)
24413 info->vrsave_mask = compute_vrsave_mask ();
24415 if (TARGET_ALTIVEC_VRSAVE && info->vrsave_mask)
24416 info->vrsave_size = 4;
24418 compute_save_world_info (info);
24420 /* Calculate the offsets. */
24421 switch (DEFAULT_ABI)
24423 case ABI_NONE:
24424 default:
24425 gcc_unreachable ();
24427 case ABI_AIX:
24428 case ABI_ELFv2:
24429 case ABI_DARWIN:
24430 info->fp_save_offset = -info->fp_size;
24431 info->gp_save_offset = info->fp_save_offset - info->gp_size;
24433 if (TARGET_ALTIVEC_ABI)
24435 info->vrsave_save_offset = info->gp_save_offset - info->vrsave_size;
24437 /* Align stack so vector save area is on a quadword boundary.
24438 The padding goes above the vectors. */
24439 if (info->altivec_size != 0)
24440 info->altivec_padding_size = info->vrsave_save_offset & 0xF;
24442 info->altivec_save_offset = info->vrsave_save_offset
24443 - info->altivec_padding_size
24444 - info->altivec_size;
24445 gcc_assert (info->altivec_size == 0
24446 || info->altivec_save_offset % 16 == 0);
24448 /* Adjust for AltiVec case. */
24449 info->ehrd_offset = info->altivec_save_offset - ehrd_size;
24451 else
24452 info->ehrd_offset = info->gp_save_offset - ehrd_size;
24454 info->ehcr_offset = info->ehrd_offset - ehcr_size;
24455 info->cr_save_offset = reg_size; /* first word when 64-bit. */
24456 info->lr_save_offset = 2*reg_size;
24457 break;
24459 case ABI_V4:
24460 info->fp_save_offset = -info->fp_size;
24461 info->gp_save_offset = info->fp_save_offset - info->gp_size;
24462 info->cr_save_offset = info->gp_save_offset - info->cr_size;
24464 if (TARGET_ALTIVEC_ABI)
24466 info->vrsave_save_offset = info->cr_save_offset - info->vrsave_size;
24468 /* Align stack so vector save area is on a quadword boundary. */
24469 if (info->altivec_size != 0)
24470 info->altivec_padding_size = 16 - (-info->vrsave_save_offset % 16);
24472 info->altivec_save_offset = info->vrsave_save_offset
24473 - info->altivec_padding_size
24474 - info->altivec_size;
24476 /* Adjust for AltiVec case. */
24477 info->ehrd_offset = info->altivec_save_offset;
24479 else
24480 info->ehrd_offset = info->cr_save_offset;
24482 info->ehrd_offset -= ehrd_size;
24483 info->lr_save_offset = reg_size;
24486 save_align = (TARGET_ALTIVEC_ABI || DEFAULT_ABI == ABI_DARWIN) ? 16 : 8;
24487 info->save_size = RS6000_ALIGN (info->fp_size
24488 + info->gp_size
24489 + info->altivec_size
24490 + info->altivec_padding_size
24491 + ehrd_size
24492 + ehcr_size
24493 + info->cr_size
24494 + info->vrsave_size,
24495 save_align);
24497 non_fixed_size = info->vars_size + info->parm_size + info->save_size;
24499 info->total_size = RS6000_ALIGN (non_fixed_size + info->fixed_size,
24500 ABI_STACK_BOUNDARY / BITS_PER_UNIT);
24502 /* Determine if we need to save the link register. */
24503 if (info->calls_p
24504 || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
24505 && crtl->profile
24506 && !TARGET_PROFILE_KERNEL)
24507 || (DEFAULT_ABI == ABI_V4 && cfun->calls_alloca)
24508 #ifdef TARGET_RELOCATABLE
24509 || (DEFAULT_ABI == ABI_V4
24510 && (TARGET_RELOCATABLE || flag_pic > 1)
24511 && !constant_pool_empty_p ())
24512 #endif
24513 || rs6000_ra_ever_killed ())
24514 info->lr_save_p = 1;
24516 using_static_chain_p = (cfun->static_chain_decl != NULL_TREE
24517 && df_regs_ever_live_p (STATIC_CHAIN_REGNUM)
24518 && call_used_regs[STATIC_CHAIN_REGNUM]);
24519 info->savres_strategy = rs6000_savres_strategy (info, using_static_chain_p);
24521 if (!(info->savres_strategy & SAVE_INLINE_GPRS)
24522 || !(info->savres_strategy & SAVE_INLINE_FPRS)
24523 || !(info->savres_strategy & SAVE_INLINE_VRS)
24524 || !(info->savres_strategy & REST_INLINE_GPRS)
24525 || !(info->savres_strategy & REST_INLINE_FPRS)
24526 || !(info->savres_strategy & REST_INLINE_VRS))
24527 info->lr_save_p = 1;
24529 if (info->lr_save_p)
24530 df_set_regs_ever_live (LR_REGNO, true);
24532 /* Determine if we need to allocate any stack frame:
24534 For AIX we need to push the stack if a frame pointer is needed
24535 (because the stack might be dynamically adjusted), if we are
24536 debugging, if we make calls, or if the sum of fp_save, gp_save,
24537 and local variables are more than the space needed to save all
24538 non-volatile registers: 32-bit: 18*8 + 19*4 = 220 or 64-bit: 18*8
24539 + 18*8 = 288 (GPR13 reserved).
24541 For V.4 we don't have the stack cushion that AIX uses, but assume
24542 that the debugger can handle stackless frames. */
24544 if (info->calls_p)
24545 info->push_p = 1;
24547 else if (DEFAULT_ABI == ABI_V4)
24548 info->push_p = non_fixed_size != 0;
24550 else if (frame_pointer_needed)
24551 info->push_p = 1;
24553 else if (TARGET_XCOFF && write_symbols != NO_DEBUG)
24554 info->push_p = 1;
24556 else
24557 info->push_p = non_fixed_size > (TARGET_32BIT ? 220 : 288);
24559 return info;
24562 static void
24563 debug_stack_info (rs6000_stack_t *info)
24565 const char *abi_string;
24567 if (! info)
24568 info = rs6000_stack_info ();
24570 fprintf (stderr, "\nStack information for function %s:\n",
24571 ((current_function_decl && DECL_NAME (current_function_decl))
24572 ? IDENTIFIER_POINTER (DECL_NAME (current_function_decl))
24573 : "<unknown>"));
24575 switch (info->abi)
24577 default: abi_string = "Unknown"; break;
24578 case ABI_NONE: abi_string = "NONE"; break;
24579 case ABI_AIX: abi_string = "AIX"; break;
24580 case ABI_ELFv2: abi_string = "ELFv2"; break;
24581 case ABI_DARWIN: abi_string = "Darwin"; break;
24582 case ABI_V4: abi_string = "V.4"; break;
24585 fprintf (stderr, "\tABI = %5s\n", abi_string);
24587 if (TARGET_ALTIVEC_ABI)
24588 fprintf (stderr, "\tALTIVEC ABI extensions enabled.\n");
24590 if (info->first_gp_reg_save != 32)
24591 fprintf (stderr, "\tfirst_gp_reg_save = %5d\n", info->first_gp_reg_save);
24593 if (info->first_fp_reg_save != 64)
24594 fprintf (stderr, "\tfirst_fp_reg_save = %5d\n", info->first_fp_reg_save);
24596 if (info->first_altivec_reg_save <= LAST_ALTIVEC_REGNO)
24597 fprintf (stderr, "\tfirst_altivec_reg_save = %5d\n",
24598 info->first_altivec_reg_save);
24600 if (info->lr_save_p)
24601 fprintf (stderr, "\tlr_save_p = %5d\n", info->lr_save_p);
24603 if (info->cr_save_p)
24604 fprintf (stderr, "\tcr_save_p = %5d\n", info->cr_save_p);
24606 if (info->vrsave_mask)
24607 fprintf (stderr, "\tvrsave_mask = 0x%x\n", info->vrsave_mask);
24609 if (info->push_p)
24610 fprintf (stderr, "\tpush_p = %5d\n", info->push_p);
24612 if (info->calls_p)
24613 fprintf (stderr, "\tcalls_p = %5d\n", info->calls_p);
24615 if (info->gp_size)
24616 fprintf (stderr, "\tgp_save_offset = %5d\n", info->gp_save_offset);
24618 if (info->fp_size)
24619 fprintf (stderr, "\tfp_save_offset = %5d\n", info->fp_save_offset);
24621 if (info->altivec_size)
24622 fprintf (stderr, "\taltivec_save_offset = %5d\n",
24623 info->altivec_save_offset);
24625 if (info->vrsave_size)
24626 fprintf (stderr, "\tvrsave_save_offset = %5d\n",
24627 info->vrsave_save_offset);
24629 if (info->lr_save_p)
24630 fprintf (stderr, "\tlr_save_offset = %5d\n", info->lr_save_offset);
24632 if (info->cr_save_p)
24633 fprintf (stderr, "\tcr_save_offset = %5d\n", info->cr_save_offset);
24635 if (info->varargs_save_offset)
24636 fprintf (stderr, "\tvarargs_save_offset = %5d\n", info->varargs_save_offset);
24638 if (info->total_size)
24639 fprintf (stderr, "\ttotal_size = " HOST_WIDE_INT_PRINT_DEC"\n",
24640 info->total_size);
24642 if (info->vars_size)
24643 fprintf (stderr, "\tvars_size = " HOST_WIDE_INT_PRINT_DEC"\n",
24644 info->vars_size);
24646 if (info->parm_size)
24647 fprintf (stderr, "\tparm_size = %5d\n", info->parm_size);
24649 if (info->fixed_size)
24650 fprintf (stderr, "\tfixed_size = %5d\n", info->fixed_size);
24652 if (info->gp_size)
24653 fprintf (stderr, "\tgp_size = %5d\n", info->gp_size);
24655 if (info->fp_size)
24656 fprintf (stderr, "\tfp_size = %5d\n", info->fp_size);
24658 if (info->altivec_size)
24659 fprintf (stderr, "\taltivec_size = %5d\n", info->altivec_size);
24661 if (info->vrsave_size)
24662 fprintf (stderr, "\tvrsave_size = %5d\n", info->vrsave_size);
24664 if (info->altivec_padding_size)
24665 fprintf (stderr, "\taltivec_padding_size= %5d\n",
24666 info->altivec_padding_size);
24668 if (info->cr_size)
24669 fprintf (stderr, "\tcr_size = %5d\n", info->cr_size);
24671 if (info->save_size)
24672 fprintf (stderr, "\tsave_size = %5d\n", info->save_size);
24674 if (info->reg_size != 4)
24675 fprintf (stderr, "\treg_size = %5d\n", info->reg_size);
24677 fprintf (stderr, "\tsave-strategy = %04x\n", info->savres_strategy);
24679 if (info->abi == ABI_DARWIN)
24680 fprintf (stderr, "\tWORLD_SAVE_P = %5d\n", WORLD_SAVE_P(info));
24682 fprintf (stderr, "\n");
24686 rs6000_return_addr (int count, rtx frame)
24688 /* We can't use get_hard_reg_initial_val for LR when count == 0 if LR
24689 is trashed by the prologue, as it is for PIC on ABI_V4 and Darwin. */
24690 if (count != 0
24691 || ((DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN) && flag_pic))
24693 cfun->machine->ra_needs_full_frame = 1;
24695 if (count == 0)
24696 /* FRAME is set to frame_pointer_rtx by the generic code, but that
24697 is good for loading 0(r1) only when !FRAME_GROWS_DOWNWARD. */
24698 frame = stack_pointer_rtx;
24699 rtx prev_frame_addr = memory_address (Pmode, frame);
24700 rtx prev_frame = copy_to_reg (gen_rtx_MEM (Pmode, prev_frame_addr));
24701 rtx lr_save_off = plus_constant (Pmode,
24702 prev_frame, RETURN_ADDRESS_OFFSET);
24703 rtx lr_save_addr = memory_address (Pmode, lr_save_off);
24704 return gen_rtx_MEM (Pmode, lr_save_addr);
24707 cfun->machine->ra_need_lr = 1;
24708 return get_hard_reg_initial_val (Pmode, LR_REGNO);
24711 /* Say whether a function is a candidate for sibcall handling or not. */
24713 static bool
24714 rs6000_function_ok_for_sibcall (tree decl, tree exp)
24716 tree fntype;
24718 /* The sibcall epilogue may clobber the static chain register.
24719 ??? We could work harder and avoid that, but it's probably
24720 not worth the hassle in practice. */
24721 if (CALL_EXPR_STATIC_CHAIN (exp))
24722 return false;
24724 if (decl)
24725 fntype = TREE_TYPE (decl);
24726 else
24727 fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (exp)));
24729 /* We can't do it if the called function has more vector parameters
24730 than the current function; there's nowhere to put the VRsave code. */
24731 if (TARGET_ALTIVEC_ABI
24732 && TARGET_ALTIVEC_VRSAVE
24733 && !(decl && decl == current_function_decl))
24735 function_args_iterator args_iter;
24736 tree type;
24737 int nvreg = 0;
24739 /* Functions with vector parameters are required to have a
24740 prototype, so the argument type info must be available
24741 here. */
24742 FOREACH_FUNCTION_ARGS(fntype, type, args_iter)
24743 if (TREE_CODE (type) == VECTOR_TYPE
24744 && ALTIVEC_OR_VSX_VECTOR_MODE (TYPE_MODE (type)))
24745 nvreg++;
24747 FOREACH_FUNCTION_ARGS(TREE_TYPE (current_function_decl), type, args_iter)
24748 if (TREE_CODE (type) == VECTOR_TYPE
24749 && ALTIVEC_OR_VSX_VECTOR_MODE (TYPE_MODE (type)))
24750 nvreg--;
24752 if (nvreg > 0)
24753 return false;
24756 /* Under the AIX or ELFv2 ABIs we can't allow calls to non-local
24757 functions, because the callee may have a different TOC pointer to
24758 the caller and there's no way to ensure we restore the TOC when
24759 we return. With the secure-plt SYSV ABI we can't make non-local
24760 calls when -fpic/PIC because the plt call stubs use r30. */
24761 if (DEFAULT_ABI == ABI_DARWIN
24762 || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
24763 && decl
24764 && !DECL_EXTERNAL (decl)
24765 && !DECL_WEAK (decl)
24766 && (*targetm.binds_local_p) (decl))
24767 || (DEFAULT_ABI == ABI_V4
24768 && (!TARGET_SECURE_PLT
24769 || !flag_pic
24770 || (decl
24771 && (*targetm.binds_local_p) (decl)))))
24773 tree attr_list = TYPE_ATTRIBUTES (fntype);
24775 if (!lookup_attribute ("longcall", attr_list)
24776 || lookup_attribute ("shortcall", attr_list))
24777 return true;
24780 return false;
24783 static int
24784 rs6000_ra_ever_killed (void)
24786 rtx_insn *top;
24787 rtx reg;
24788 rtx_insn *insn;
24790 if (cfun->is_thunk)
24791 return 0;
24793 if (cfun->machine->lr_save_state)
24794 return cfun->machine->lr_save_state - 1;
24796 /* regs_ever_live has LR marked as used if any sibcalls are present,
24797 but this should not force saving and restoring in the
24798 pro/epilogue. Likewise, reg_set_between_p thinks a sibcall
24799 clobbers LR, so that is inappropriate. */
24801 /* Also, the prologue can generate a store into LR that
24802 doesn't really count, like this:
24804 move LR->R0
24805 bcl to set PIC register
24806 move LR->R31
24807 move R0->LR
24809 When we're called from the epilogue, we need to avoid counting
24810 this as a store. */
24812 push_topmost_sequence ();
24813 top = get_insns ();
24814 pop_topmost_sequence ();
24815 reg = gen_rtx_REG (Pmode, LR_REGNO);
24817 for (insn = NEXT_INSN (top); insn != NULL_RTX; insn = NEXT_INSN (insn))
24819 if (INSN_P (insn))
24821 if (CALL_P (insn))
24823 if (!SIBLING_CALL_P (insn))
24824 return 1;
24826 else if (find_regno_note (insn, REG_INC, LR_REGNO))
24827 return 1;
24828 else if (set_of (reg, insn) != NULL_RTX
24829 && !prologue_epilogue_contains (insn))
24830 return 1;
24833 return 0;
24836 /* Emit instructions needed to load the TOC register.
24837 This is only needed when TARGET_TOC, TARGET_MINIMAL_TOC, and there is
24838 a constant pool; or for SVR4 -fpic. */
24840 void
24841 rs6000_emit_load_toc_table (int fromprolog)
24843 rtx dest;
24844 dest = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
24846 if (TARGET_ELF && TARGET_SECURE_PLT && DEFAULT_ABI == ABI_V4 && flag_pic)
24848 char buf[30];
24849 rtx lab, tmp1, tmp2, got;
24851 lab = gen_label_rtx ();
24852 ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (lab));
24853 lab = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
24854 if (flag_pic == 2)
24856 got = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (toc_label_name));
24857 need_toc_init = 1;
24859 else
24860 got = rs6000_got_sym ();
24861 tmp1 = tmp2 = dest;
24862 if (!fromprolog)
24864 tmp1 = gen_reg_rtx (Pmode);
24865 tmp2 = gen_reg_rtx (Pmode);
24867 emit_insn (gen_load_toc_v4_PIC_1 (lab));
24868 emit_move_insn (tmp1, gen_rtx_REG (Pmode, LR_REGNO));
24869 emit_insn (gen_load_toc_v4_PIC_3b (tmp2, tmp1, got, lab));
24870 emit_insn (gen_load_toc_v4_PIC_3c (dest, tmp2, got, lab));
24872 else if (TARGET_ELF && DEFAULT_ABI == ABI_V4 && flag_pic == 1)
24874 emit_insn (gen_load_toc_v4_pic_si ());
24875 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
24877 else if (TARGET_ELF && DEFAULT_ABI == ABI_V4 && flag_pic == 2)
24879 char buf[30];
24880 rtx temp0 = (fromprolog
24881 ? gen_rtx_REG (Pmode, 0)
24882 : gen_reg_rtx (Pmode));
24884 if (fromprolog)
24886 rtx symF, symL;
24888 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
24889 symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
24891 ASM_GENERATE_INTERNAL_LABEL (buf, "LCL", rs6000_pic_labelno);
24892 symL = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
24894 emit_insn (gen_load_toc_v4_PIC_1 (symF));
24895 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
24896 emit_insn (gen_load_toc_v4_PIC_2 (temp0, dest, symL, symF));
24898 else
24900 rtx tocsym, lab;
24902 tocsym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (toc_label_name));
24903 need_toc_init = 1;
24904 lab = gen_label_rtx ();
24905 emit_insn (gen_load_toc_v4_PIC_1b (tocsym, lab));
24906 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
24907 if (TARGET_LINK_STACK)
24908 emit_insn (gen_addsi3 (dest, dest, GEN_INT (4)));
24909 emit_move_insn (temp0, gen_rtx_MEM (Pmode, dest));
24911 emit_insn (gen_addsi3 (dest, temp0, dest));
24913 else if (TARGET_ELF && !TARGET_AIX && flag_pic == 0 && TARGET_MINIMAL_TOC)
24915 /* This is for AIX code running in non-PIC ELF32. */
24916 rtx realsym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (toc_label_name));
24918 need_toc_init = 1;
24919 emit_insn (gen_elf_high (dest, realsym));
24920 emit_insn (gen_elf_low (dest, dest, realsym));
24922 else
24924 gcc_assert (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2);
24926 if (TARGET_32BIT)
24927 emit_insn (gen_load_toc_aix_si (dest));
24928 else
24929 emit_insn (gen_load_toc_aix_di (dest));
24933 /* Emit instructions to restore the link register after determining where
24934 its value has been stored. */
24936 void
24937 rs6000_emit_eh_reg_restore (rtx source, rtx scratch)
24939 rs6000_stack_t *info = rs6000_stack_info ();
24940 rtx operands[2];
24942 operands[0] = source;
24943 operands[1] = scratch;
24945 if (info->lr_save_p)
24947 rtx frame_rtx = stack_pointer_rtx;
24948 HOST_WIDE_INT sp_offset = 0;
24949 rtx tmp;
24951 if (frame_pointer_needed
24952 || cfun->calls_alloca
24953 || info->total_size > 32767)
24955 tmp = gen_frame_mem (Pmode, frame_rtx);
24956 emit_move_insn (operands[1], tmp);
24957 frame_rtx = operands[1];
24959 else if (info->push_p)
24960 sp_offset = info->total_size;
24962 tmp = plus_constant (Pmode, frame_rtx,
24963 info->lr_save_offset + sp_offset);
24964 tmp = gen_frame_mem (Pmode, tmp);
24965 emit_move_insn (tmp, operands[0]);
24967 else
24968 emit_move_insn (gen_rtx_REG (Pmode, LR_REGNO), operands[0]);
24970 /* Freeze lr_save_p. We've just emitted rtl that depends on the
24971 state of lr_save_p so any change from here on would be a bug. In
24972 particular, stop rs6000_ra_ever_killed from considering the SET
24973 of lr we may have added just above. */
24974 cfun->machine->lr_save_state = info->lr_save_p + 1;
24977 static GTY(()) alias_set_type set = -1;
24979 alias_set_type
24980 get_TOC_alias_set (void)
24982 if (set == -1)
24983 set = new_alias_set ();
24984 return set;
24987 /* This returns nonzero if the current function uses the TOC. This is
24988 determined by the presence of (use (unspec ... UNSPEC_TOC)), which
24989 is generated by the ABI_V4 load_toc_* patterns.
24990 Return 2 instead of 1 if the load_toc_* pattern is in the function
24991 partition that doesn't start the function. */
24992 #if TARGET_ELF
24993 static int
24994 uses_TOC (void)
24996 rtx_insn *insn;
24997 int ret = 1;
24999 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
25001 if (INSN_P (insn))
25003 rtx pat = PATTERN (insn);
25004 int i;
25006 if (GET_CODE (pat) == PARALLEL)
25007 for (i = 0; i < XVECLEN (pat, 0); i++)
25009 rtx sub = XVECEXP (pat, 0, i);
25010 if (GET_CODE (sub) == USE)
25012 sub = XEXP (sub, 0);
25013 if (GET_CODE (sub) == UNSPEC
25014 && XINT (sub, 1) == UNSPEC_TOC)
25015 return ret;
25019 else if (crtl->has_bb_partition
25020 && NOTE_P (insn)
25021 && NOTE_KIND (insn) == NOTE_INSN_SWITCH_TEXT_SECTIONS)
25022 ret = 2;
25024 return 0;
25026 #endif
25029 create_TOC_reference (rtx symbol, rtx largetoc_reg)
25031 rtx tocrel, tocreg, hi;
25033 if (TARGET_DEBUG_ADDR)
25035 if (SYMBOL_REF_P (symbol))
25036 fprintf (stderr, "\ncreate_TOC_reference, (symbol_ref %s)\n",
25037 XSTR (symbol, 0));
25038 else
25040 fprintf (stderr, "\ncreate_TOC_reference, code %s:\n",
25041 GET_RTX_NAME (GET_CODE (symbol)));
25042 debug_rtx (symbol);
25046 if (!can_create_pseudo_p ())
25047 df_set_regs_ever_live (TOC_REGISTER, true);
25049 tocreg = gen_rtx_REG (Pmode, TOC_REGISTER);
25050 tocrel = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, symbol, tocreg), UNSPEC_TOCREL);
25051 if (TARGET_CMODEL == CMODEL_SMALL || can_create_pseudo_p ())
25052 return tocrel;
25054 hi = gen_rtx_HIGH (Pmode, copy_rtx (tocrel));
25055 if (largetoc_reg != NULL)
25057 emit_move_insn (largetoc_reg, hi);
25058 hi = largetoc_reg;
25060 return gen_rtx_LO_SUM (Pmode, hi, tocrel);
25063 /* Issue assembly directives that create a reference to the given DWARF
25064 FRAME_TABLE_LABEL from the current function section. */
25065 void
25066 rs6000_aix_asm_output_dwarf_table_ref (char * frame_table_label)
25068 fprintf (asm_out_file, "\t.ref %s\n",
25069 (* targetm.strip_name_encoding) (frame_table_label));
25072 /* This ties together stack memory (MEM with an alias set of frame_alias_set)
25073 and the change to the stack pointer. */
25075 static void
25076 rs6000_emit_stack_tie (rtx fp, bool hard_frame_needed)
25078 rtvec p;
25079 int i;
25080 rtx regs[3];
25082 i = 0;
25083 regs[i++] = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
25084 if (hard_frame_needed)
25085 regs[i++] = gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM);
25086 if (!(REGNO (fp) == STACK_POINTER_REGNUM
25087 || (hard_frame_needed
25088 && REGNO (fp) == HARD_FRAME_POINTER_REGNUM)))
25089 regs[i++] = fp;
25091 p = rtvec_alloc (i);
25092 while (--i >= 0)
25094 rtx mem = gen_frame_mem (BLKmode, regs[i]);
25095 RTVEC_ELT (p, i) = gen_rtx_SET (mem, const0_rtx);
25098 emit_insn (gen_stack_tie (gen_rtx_PARALLEL (VOIDmode, p)));
25101 /* Allocate SIZE_INT bytes on the stack using a store with update style insn
25102 and set the appropriate attributes for the generated insn. Return the
25103 first insn which adjusts the stack pointer or the last insn before
25104 the stack adjustment loop.
25106 SIZE_INT is used to create the CFI note for the allocation.
25108 SIZE_RTX is an rtx containing the size of the adjustment. Note that
25109 since stacks grow to lower addresses its runtime value is -SIZE_INT.
25111 ORIG_SP contains the backchain value that must be stored at *sp. */
25113 static rtx_insn *
25114 rs6000_emit_allocate_stack_1 (HOST_WIDE_INT size_int, rtx orig_sp)
25116 rtx_insn *insn;
25118 rtx size_rtx = GEN_INT (-size_int);
25119 if (size_int > 32767)
25121 rtx tmp_reg = gen_rtx_REG (Pmode, 0);
25122 /* Need a note here so that try_split doesn't get confused. */
25123 if (get_last_insn () == NULL_RTX)
25124 emit_note (NOTE_INSN_DELETED);
25125 insn = emit_move_insn (tmp_reg, size_rtx);
25126 try_split (PATTERN (insn), insn, 0);
25127 size_rtx = tmp_reg;
25130 if (TARGET_32BIT)
25131 insn = emit_insn (gen_movsi_update_stack (stack_pointer_rtx,
25132 stack_pointer_rtx,
25133 size_rtx,
25134 orig_sp));
25135 else
25136 insn = emit_insn (gen_movdi_update_stack (stack_pointer_rtx,
25137 stack_pointer_rtx,
25138 size_rtx,
25139 orig_sp));
25140 rtx par = PATTERN (insn);
25141 gcc_assert (GET_CODE (par) == PARALLEL);
25142 rtx set = XVECEXP (par, 0, 0);
25143 gcc_assert (GET_CODE (set) == SET);
25144 rtx mem = SET_DEST (set);
25145 gcc_assert (MEM_P (mem));
25146 MEM_NOTRAP_P (mem) = 1;
25147 set_mem_alias_set (mem, get_frame_alias_set ());
25149 RTX_FRAME_RELATED_P (insn) = 1;
25150 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
25151 gen_rtx_SET (stack_pointer_rtx,
25152 gen_rtx_PLUS (Pmode,
25153 stack_pointer_rtx,
25154 GEN_INT (-size_int))));
25156 /* Emit a blockage to ensure the allocation/probing insns are
25157 not optimized, combined, removed, etc. Add REG_STACK_CHECK
25158 note for similar reasons. */
25159 if (flag_stack_clash_protection)
25161 add_reg_note (insn, REG_STACK_CHECK, const0_rtx);
25162 emit_insn (gen_blockage ());
25165 return insn;
25168 static HOST_WIDE_INT
25169 get_stack_clash_protection_probe_interval (void)
25171 return (HOST_WIDE_INT_1U
25172 << PARAM_VALUE (PARAM_STACK_CLASH_PROTECTION_PROBE_INTERVAL));
25175 static HOST_WIDE_INT
25176 get_stack_clash_protection_guard_size (void)
25178 return (HOST_WIDE_INT_1U
25179 << PARAM_VALUE (PARAM_STACK_CLASH_PROTECTION_GUARD_SIZE));
25182 /* Allocate ORIG_SIZE bytes on the stack and probe the newly
25183 allocated space every STACK_CLASH_PROTECTION_PROBE_INTERVAL bytes.
25185 COPY_REG, if non-null, should contain a copy of the original
25186 stack pointer at exit from this function.
25188 This is subtly different than the Ada probing in that it tries hard to
25189 prevent attacks that jump the stack guard. Thus it is never allowed to
25190 allocate more than STACK_CLASH_PROTECTION_PROBE_INTERVAL bytes of stack
25191 space without a suitable probe. */
25192 static rtx_insn *
25193 rs6000_emit_probe_stack_range_stack_clash (HOST_WIDE_INT orig_size,
25194 rtx copy_reg)
25196 rtx orig_sp = copy_reg;
25198 HOST_WIDE_INT probe_interval = get_stack_clash_protection_probe_interval ();
25200 /* Round the size down to a multiple of PROBE_INTERVAL. */
25201 HOST_WIDE_INT rounded_size = ROUND_DOWN (orig_size, probe_interval);
25203 /* If explicitly requested,
25204 or the rounded size is not the same as the original size
25205 or the the rounded size is greater than a page,
25206 then we will need a copy of the original stack pointer. */
25207 if (rounded_size != orig_size
25208 || rounded_size > probe_interval
25209 || copy_reg)
25211 /* If the caller did not request a copy of the incoming stack
25212 pointer, then we use r0 to hold the copy. */
25213 if (!copy_reg)
25214 orig_sp = gen_rtx_REG (Pmode, 0);
25215 emit_move_insn (orig_sp, stack_pointer_rtx);
25218 /* There's three cases here.
25220 One is a single probe which is the most common and most efficiently
25221 implemented as it does not have to have a copy of the original
25222 stack pointer if there are no residuals.
25224 Second is unrolled allocation/probes which we use if there's just
25225 a few of them. It needs to save the original stack pointer into a
25226 temporary for use as a source register in the allocation/probe.
25228 Last is a loop. This is the most uncommon case and least efficient. */
25229 rtx_insn *retval = NULL;
25230 if (rounded_size == probe_interval)
25232 retval = rs6000_emit_allocate_stack_1 (probe_interval, stack_pointer_rtx);
25234 dump_stack_clash_frame_info (PROBE_INLINE, rounded_size != orig_size);
25236 else if (rounded_size <= 8 * probe_interval)
25238 /* The ABI requires using the store with update insns to allocate
25239 space and store the backchain into the stack
25241 So we save the current stack pointer into a temporary, then
25242 emit the store-with-update insns to store the saved stack pointer
25243 into the right location in each new page. */
25244 for (int i = 0; i < rounded_size; i += probe_interval)
25246 rtx_insn *insn
25247 = rs6000_emit_allocate_stack_1 (probe_interval, orig_sp);
25249 /* Save the first stack adjustment in RETVAL. */
25250 if (i == 0)
25251 retval = insn;
25254 dump_stack_clash_frame_info (PROBE_INLINE, rounded_size != orig_size);
25256 else
25258 /* Compute the ending address. */
25259 rtx end_addr
25260 = copy_reg ? gen_rtx_REG (Pmode, 0) : gen_rtx_REG (Pmode, 12);
25261 rtx rs = GEN_INT (-rounded_size);
25262 rtx_insn *insn;
25263 if (add_operand (rs, Pmode))
25264 insn = emit_insn (gen_add3_insn (end_addr, stack_pointer_rtx, rs));
25265 else
25267 emit_move_insn (end_addr, GEN_INT (-rounded_size));
25268 insn = emit_insn (gen_add3_insn (end_addr, end_addr,
25269 stack_pointer_rtx));
25270 /* Describe the effect of INSN to the CFI engine. */
25271 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
25272 gen_rtx_SET (end_addr,
25273 gen_rtx_PLUS (Pmode, stack_pointer_rtx,
25274 rs)));
25276 RTX_FRAME_RELATED_P (insn) = 1;
25278 /* Emit the loop. */
25279 if (TARGET_64BIT)
25280 retval = emit_insn (gen_probe_stack_rangedi (stack_pointer_rtx,
25281 stack_pointer_rtx, orig_sp,
25282 end_addr));
25283 else
25284 retval = emit_insn (gen_probe_stack_rangesi (stack_pointer_rtx,
25285 stack_pointer_rtx, orig_sp,
25286 end_addr));
25287 RTX_FRAME_RELATED_P (retval) = 1;
25288 /* Describe the effect of INSN to the CFI engine. */
25289 add_reg_note (retval, REG_FRAME_RELATED_EXPR,
25290 gen_rtx_SET (stack_pointer_rtx, end_addr));
25292 /* Emit a blockage to ensure the allocation/probing insns are
25293 not optimized, combined, removed, etc. Other cases handle this
25294 within their call to rs6000_emit_allocate_stack_1. */
25295 emit_insn (gen_blockage ());
25297 dump_stack_clash_frame_info (PROBE_LOOP, rounded_size != orig_size);
25300 if (orig_size != rounded_size)
25302 /* Allocate (and implicitly probe) any residual space. */
25303 HOST_WIDE_INT residual = orig_size - rounded_size;
25305 rtx_insn *insn = rs6000_emit_allocate_stack_1 (residual, orig_sp);
25307 /* If the residual was the only allocation, then we can return the
25308 allocating insn. */
25309 if (!retval)
25310 retval = insn;
25313 return retval;
25316 /* Emit the correct code for allocating stack space, as insns.
25317 If COPY_REG, make sure a copy of the old frame is left there.
25318 The generated code may use hard register 0 as a temporary. */
25320 static rtx_insn *
25321 rs6000_emit_allocate_stack (HOST_WIDE_INT size, rtx copy_reg, int copy_off)
25323 rtx_insn *insn;
25324 rtx stack_reg = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
25325 rtx tmp_reg = gen_rtx_REG (Pmode, 0);
25326 rtx todec = gen_int_mode (-size, Pmode);
25328 if (INTVAL (todec) != -size)
25330 warning (0, "stack frame too large");
25331 emit_insn (gen_trap ());
25332 return 0;
25335 if (crtl->limit_stack)
25337 if (REG_P (stack_limit_rtx)
25338 && REGNO (stack_limit_rtx) > 1
25339 && REGNO (stack_limit_rtx) <= 31)
25341 rtx_insn *insn
25342 = gen_add3_insn (tmp_reg, stack_limit_rtx, GEN_INT (size));
25343 gcc_assert (insn);
25344 emit_insn (insn);
25345 emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg, const0_rtx));
25347 else if (SYMBOL_REF_P (stack_limit_rtx)
25348 && TARGET_32BIT
25349 && DEFAULT_ABI == ABI_V4
25350 && !flag_pic)
25352 rtx toload = gen_rtx_CONST (VOIDmode,
25353 gen_rtx_PLUS (Pmode,
25354 stack_limit_rtx,
25355 GEN_INT (size)));
25357 emit_insn (gen_elf_high (tmp_reg, toload));
25358 emit_insn (gen_elf_low (tmp_reg, tmp_reg, toload));
25359 emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
25360 const0_rtx));
25362 else
25363 warning (0, "stack limit expression is not supported");
25366 if (flag_stack_clash_protection)
25368 if (size < get_stack_clash_protection_guard_size ())
25369 dump_stack_clash_frame_info (NO_PROBE_SMALL_FRAME, true);
25370 else
25372 rtx_insn *insn = rs6000_emit_probe_stack_range_stack_clash (size,
25373 copy_reg);
25375 /* If we asked for a copy with an offset, then we still need add in
25376 the offset. */
25377 if (copy_reg && copy_off)
25378 emit_insn (gen_add3_insn (copy_reg, copy_reg, GEN_INT (copy_off)));
25379 return insn;
25383 if (copy_reg)
25385 if (copy_off != 0)
25386 emit_insn (gen_add3_insn (copy_reg, stack_reg, GEN_INT (copy_off)));
25387 else
25388 emit_move_insn (copy_reg, stack_reg);
25391 /* Since we didn't use gen_frame_mem to generate the MEM, grab
25392 it now and set the alias set/attributes. The above gen_*_update
25393 calls will generate a PARALLEL with the MEM set being the first
25394 operation. */
25395 insn = rs6000_emit_allocate_stack_1 (size, stack_reg);
25396 return insn;
25399 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
25401 #if PROBE_INTERVAL > 32768
25402 #error Cannot use indexed addressing mode for stack probing
25403 #endif
25405 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
25406 inclusive. These are offsets from the current stack pointer. */
25408 static void
25409 rs6000_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
25411 /* See if we have a constant small number of probes to generate. If so,
25412 that's the easy case. */
25413 if (first + size <= 32768)
25415 HOST_WIDE_INT i;
25417 /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
25418 it exceeds SIZE. If only one probe is needed, this will not
25419 generate any code. Then probe at FIRST + SIZE. */
25420 for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
25421 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
25422 -(first + i)));
25424 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
25425 -(first + size)));
25428 /* Otherwise, do the same as above, but in a loop. Note that we must be
25429 extra careful with variables wrapping around because we might be at
25430 the very top (or the very bottom) of the address space and we have
25431 to be able to handle this case properly; in particular, we use an
25432 equality test for the loop condition. */
25433 else
25435 HOST_WIDE_INT rounded_size;
25436 rtx r12 = gen_rtx_REG (Pmode, 12);
25437 rtx r0 = gen_rtx_REG (Pmode, 0);
25439 /* Sanity check for the addressing mode we're going to use. */
25440 gcc_assert (first <= 32768);
25442 /* Step 1: round SIZE to the previous multiple of the interval. */
25444 rounded_size = ROUND_DOWN (size, PROBE_INTERVAL);
25447 /* Step 2: compute initial and final value of the loop counter. */
25449 /* TEST_ADDR = SP + FIRST. */
25450 emit_insn (gen_rtx_SET (r12, plus_constant (Pmode, stack_pointer_rtx,
25451 -first)));
25453 /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE. */
25454 if (rounded_size > 32768)
25456 emit_move_insn (r0, GEN_INT (-rounded_size));
25457 emit_insn (gen_rtx_SET (r0, gen_rtx_PLUS (Pmode, r12, r0)));
25459 else
25460 emit_insn (gen_rtx_SET (r0, plus_constant (Pmode, r12,
25461 -rounded_size)));
25464 /* Step 3: the loop
25468 TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
25469 probe at TEST_ADDR
25471 while (TEST_ADDR != LAST_ADDR)
25473 probes at FIRST + N * PROBE_INTERVAL for values of N from 1
25474 until it is equal to ROUNDED_SIZE. */
25476 if (TARGET_64BIT)
25477 emit_insn (gen_probe_stack_rangedi (r12, r12, stack_pointer_rtx, r0));
25478 else
25479 emit_insn (gen_probe_stack_rangesi (r12, r12, stack_pointer_rtx, r0));
25482 /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
25483 that SIZE is equal to ROUNDED_SIZE. */
25485 if (size != rounded_size)
25486 emit_stack_probe (plus_constant (Pmode, r12, rounded_size - size));
25490 /* Probe a range of stack addresses from REG1 to REG2 inclusive. These are
25491 addresses, not offsets. */
25493 static const char *
25494 output_probe_stack_range_1 (rtx reg1, rtx reg2)
25496 static int labelno = 0;
25497 char loop_lab[32];
25498 rtx xops[2];
25500 ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno++);
25502 /* Loop. */
25503 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
25505 /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL. */
25506 xops[0] = reg1;
25507 xops[1] = GEN_INT (-PROBE_INTERVAL);
25508 output_asm_insn ("addi %0,%0,%1", xops);
25510 /* Probe at TEST_ADDR. */
25511 xops[1] = gen_rtx_REG (Pmode, 0);
25512 output_asm_insn ("stw %1,0(%0)", xops);
25514 /* Test if TEST_ADDR == LAST_ADDR. */
25515 xops[1] = reg2;
25516 if (TARGET_64BIT)
25517 output_asm_insn ("cmpd 0,%0,%1", xops);
25518 else
25519 output_asm_insn ("cmpw 0,%0,%1", xops);
25521 /* Branch. */
25522 fputs ("\tbne 0,", asm_out_file);
25523 assemble_name_raw (asm_out_file, loop_lab);
25524 fputc ('\n', asm_out_file);
25526 return "";
25529 /* This function is called when rs6000_frame_related is processing
25530 SETs within a PARALLEL, and returns whether the REGNO save ought to
25531 be marked RTX_FRAME_RELATED_P. The PARALLELs involved are those
25532 for out-of-line register save functions, store multiple, and the
25533 Darwin world_save. They may contain registers that don't really
25534 need saving. */
25536 static bool
25537 interesting_frame_related_regno (unsigned int regno)
25539 /* Saves apparently of r0 are actually saving LR. It doesn't make
25540 sense to substitute the regno here to test save_reg_p (LR_REGNO).
25541 We *know* LR needs saving, and dwarf2cfi.c is able to deduce that
25542 (set (mem) (r0)) is saving LR from a prior (set (r0) (lr)) marked
25543 as frame related. */
25544 if (regno == 0)
25545 return true;
25546 /* If we see CR2 then we are here on a Darwin world save. Saves of
25547 CR2 signify the whole CR is being saved. This is a long-standing
25548 ABI wart fixed by ELFv2. As for r0/lr there is no need to check
25549 that CR needs to be saved. */
25550 if (regno == CR2_REGNO)
25551 return true;
25552 /* Omit frame info for any user-defined global regs. If frame info
25553 is supplied for them, frame unwinding will restore a user reg.
25554 Also omit frame info for any reg we don't need to save, as that
25555 bloats frame info and can cause problems with shrink wrapping.
25556 Since global regs won't be seen as needing to be saved, both of
25557 these conditions are covered by save_reg_p. */
25558 return save_reg_p (regno);
25561 /* Probe a range of stack addresses from REG1 to REG3 inclusive. These are
25562 addresses, not offsets.
25564 REG2 contains the backchain that must be stored into *sp at each allocation.
25566 This is subtly different than the Ada probing above in that it tries hard
25567 to prevent attacks that jump the stack guard. Thus, it is never allowed
25568 to allocate more than PROBE_INTERVAL bytes of stack space without a
25569 suitable probe. */
25571 static const char *
25572 output_probe_stack_range_stack_clash (rtx reg1, rtx reg2, rtx reg3)
25574 static int labelno = 0;
25575 char loop_lab[32];
25576 rtx xops[3];
25578 HOST_WIDE_INT probe_interval = get_stack_clash_protection_probe_interval ();
25580 ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno++);
25582 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
25584 /* This allocates and probes. */
25585 xops[0] = reg1;
25586 xops[1] = reg2;
25587 xops[2] = GEN_INT (-probe_interval);
25588 if (TARGET_64BIT)
25589 output_asm_insn ("stdu %1,%2(%0)", xops);
25590 else
25591 output_asm_insn ("stwu %1,%2(%0)", xops);
25593 /* Jump to LOOP_LAB if TEST_ADDR != LAST_ADDR. */
25594 xops[0] = reg1;
25595 xops[1] = reg3;
25596 if (TARGET_64BIT)
25597 output_asm_insn ("cmpd 0,%0,%1", xops);
25598 else
25599 output_asm_insn ("cmpw 0,%0,%1", xops);
25601 fputs ("\tbne 0,", asm_out_file);
25602 assemble_name_raw (asm_out_file, loop_lab);
25603 fputc ('\n', asm_out_file);
25605 return "";
25608 /* Wrapper around the output_probe_stack_range routines. */
25609 const char *
25610 output_probe_stack_range (rtx reg1, rtx reg2, rtx reg3)
25612 if (flag_stack_clash_protection)
25613 return output_probe_stack_range_stack_clash (reg1, reg2, reg3);
25614 else
25615 return output_probe_stack_range_1 (reg1, reg3);
25618 /* Add to 'insn' a note which is PATTERN (INSN) but with REG replaced
25619 with (plus:P (reg 1) VAL), and with REG2 replaced with REPL2 if REG2
25620 is not NULL. It would be nice if dwarf2out_frame_debug_expr could
25621 deduce these equivalences by itself so it wasn't necessary to hold
25622 its hand so much. Don't be tempted to always supply d2_f_d_e with
25623 the actual cfa register, ie. r31 when we are using a hard frame
25624 pointer. That fails when saving regs off r1, and sched moves the
25625 r31 setup past the reg saves. */
25627 static rtx_insn *
25628 rs6000_frame_related (rtx_insn *insn, rtx reg, HOST_WIDE_INT val,
25629 rtx reg2, rtx repl2)
25631 rtx repl;
25633 if (REGNO (reg) == STACK_POINTER_REGNUM)
25635 gcc_checking_assert (val == 0);
25636 repl = NULL_RTX;
25638 else
25639 repl = gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode, STACK_POINTER_REGNUM),
25640 GEN_INT (val));
25642 rtx pat = PATTERN (insn);
25643 if (!repl && !reg2)
25645 /* No need for any replacement. Just set RTX_FRAME_RELATED_P. */
25646 if (GET_CODE (pat) == PARALLEL)
25647 for (int i = 0; i < XVECLEN (pat, 0); i++)
25648 if (GET_CODE (XVECEXP (pat, 0, i)) == SET)
25650 rtx set = XVECEXP (pat, 0, i);
25652 if (!REG_P (SET_SRC (set))
25653 || interesting_frame_related_regno (REGNO (SET_SRC (set))))
25654 RTX_FRAME_RELATED_P (set) = 1;
25656 RTX_FRAME_RELATED_P (insn) = 1;
25657 return insn;
25660 /* We expect that 'pat' is either a SET or a PARALLEL containing
25661 SETs (and possibly other stuff). In a PARALLEL, all the SETs
25662 are important so they all have to be marked RTX_FRAME_RELATED_P.
25663 Call simplify_replace_rtx on the SETs rather than the whole insn
25664 so as to leave the other stuff alone (for example USE of r12). */
25666 set_used_flags (pat);
25667 if (GET_CODE (pat) == SET)
25669 if (repl)
25670 pat = simplify_replace_rtx (pat, reg, repl);
25671 if (reg2)
25672 pat = simplify_replace_rtx (pat, reg2, repl2);
25674 else if (GET_CODE (pat) == PARALLEL)
25676 pat = shallow_copy_rtx (pat);
25677 XVEC (pat, 0) = shallow_copy_rtvec (XVEC (pat, 0));
25679 for (int i = 0; i < XVECLEN (pat, 0); i++)
25680 if (GET_CODE (XVECEXP (pat, 0, i)) == SET)
25682 rtx set = XVECEXP (pat, 0, i);
25684 if (repl)
25685 set = simplify_replace_rtx (set, reg, repl);
25686 if (reg2)
25687 set = simplify_replace_rtx (set, reg2, repl2);
25688 XVECEXP (pat, 0, i) = set;
25690 if (!REG_P (SET_SRC (set))
25691 || interesting_frame_related_regno (REGNO (SET_SRC (set))))
25692 RTX_FRAME_RELATED_P (set) = 1;
25695 else
25696 gcc_unreachable ();
25698 RTX_FRAME_RELATED_P (insn) = 1;
25699 add_reg_note (insn, REG_FRAME_RELATED_EXPR, copy_rtx_if_shared (pat));
25701 return insn;
25704 /* Returns an insn that has a vrsave set operation with the
25705 appropriate CLOBBERs. */
25707 static rtx
25708 generate_set_vrsave (rtx reg, rs6000_stack_t *info, int epiloguep)
25710 int nclobs, i;
25711 rtx insn, clobs[TOTAL_ALTIVEC_REGS + 1];
25712 rtx vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
25714 clobs[0]
25715 = gen_rtx_SET (vrsave,
25716 gen_rtx_UNSPEC_VOLATILE (SImode,
25717 gen_rtvec (2, reg, vrsave),
25718 UNSPECV_SET_VRSAVE));
25720 nclobs = 1;
25722 /* We need to clobber the registers in the mask so the scheduler
25723 does not move sets to VRSAVE before sets of AltiVec registers.
25725 However, if the function receives nonlocal gotos, reload will set
25726 all call saved registers live. We will end up with:
25728 (set (reg 999) (mem))
25729 (parallel [ (set (reg vrsave) (unspec blah))
25730 (clobber (reg 999))])
25732 The clobber will cause the store into reg 999 to be dead, and
25733 flow will attempt to delete an epilogue insn. In this case, we
25734 need an unspec use/set of the register. */
25736 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
25737 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
25739 if (!epiloguep || call_used_regs [i])
25740 clobs[nclobs++] = gen_hard_reg_clobber (V4SImode, i);
25741 else
25743 rtx reg = gen_rtx_REG (V4SImode, i);
25745 clobs[nclobs++]
25746 = gen_rtx_SET (reg,
25747 gen_rtx_UNSPEC (V4SImode,
25748 gen_rtvec (1, reg), 27));
25752 insn = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nclobs));
25754 for (i = 0; i < nclobs; ++i)
25755 XVECEXP (insn, 0, i) = clobs[i];
25757 return insn;
25760 static rtx
25761 gen_frame_set (rtx reg, rtx frame_reg, int offset, bool store)
25763 rtx addr, mem;
25765 addr = gen_rtx_PLUS (Pmode, frame_reg, GEN_INT (offset));
25766 mem = gen_frame_mem (GET_MODE (reg), addr);
25767 return gen_rtx_SET (store ? mem : reg, store ? reg : mem);
25770 static rtx
25771 gen_frame_load (rtx reg, rtx frame_reg, int offset)
25773 return gen_frame_set (reg, frame_reg, offset, false);
25776 static rtx
25777 gen_frame_store (rtx reg, rtx frame_reg, int offset)
25779 return gen_frame_set (reg, frame_reg, offset, true);
25782 /* Save a register into the frame, and emit RTX_FRAME_RELATED_P notes.
25783 Save REGNO into [FRAME_REG + OFFSET] in mode MODE. */
25785 static rtx_insn *
25786 emit_frame_save (rtx frame_reg, machine_mode mode,
25787 unsigned int regno, int offset, HOST_WIDE_INT frame_reg_to_sp)
25789 rtx reg;
25791 /* Some cases that need register indexed addressing. */
25792 gcc_checking_assert (!(TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
25793 || (TARGET_VSX && ALTIVEC_OR_VSX_VECTOR_MODE (mode)));
25795 reg = gen_rtx_REG (mode, regno);
25796 rtx_insn *insn = emit_insn (gen_frame_store (reg, frame_reg, offset));
25797 return rs6000_frame_related (insn, frame_reg, frame_reg_to_sp,
25798 NULL_RTX, NULL_RTX);
25801 /* Emit an offset memory reference suitable for a frame store, while
25802 converting to a valid addressing mode. */
25804 static rtx
25805 gen_frame_mem_offset (machine_mode mode, rtx reg, int offset)
25807 return gen_frame_mem (mode, gen_rtx_PLUS (Pmode, reg, GEN_INT (offset)));
25810 #ifndef TARGET_FIX_AND_CONTINUE
25811 #define TARGET_FIX_AND_CONTINUE 0
25812 #endif
25814 /* It's really GPR 13 or 14, FPR 14 and VR 20. We need the smallest. */
25815 #define FIRST_SAVRES_REGISTER FIRST_SAVED_GP_REGNO
25816 #define LAST_SAVRES_REGISTER 31
25817 #define N_SAVRES_REGISTERS (LAST_SAVRES_REGISTER - FIRST_SAVRES_REGISTER + 1)
25819 enum {
25820 SAVRES_LR = 0x1,
25821 SAVRES_SAVE = 0x2,
25822 SAVRES_REG = 0x0c,
25823 SAVRES_GPR = 0,
25824 SAVRES_FPR = 4,
25825 SAVRES_VR = 8
25828 static GTY(()) rtx savres_routine_syms[N_SAVRES_REGISTERS][12];
25830 /* Temporary holding space for an out-of-line register save/restore
25831 routine name. */
25832 static char savres_routine_name[30];
25834 /* Return the name for an out-of-line register save/restore routine.
25835 We are saving/restoring GPRs if GPR is true. */
25837 static char *
25838 rs6000_savres_routine_name (int regno, int sel)
25840 const char *prefix = "";
25841 const char *suffix = "";
25843 /* Different targets are supposed to define
25844 {SAVE,RESTORE}_FP_{PREFIX,SUFFIX} with the idea that the needed
25845 routine name could be defined with:
25847 sprintf (name, "%s%d%s", SAVE_FP_PREFIX, regno, SAVE_FP_SUFFIX)
25849 This is a nice idea in practice, but in reality, things are
25850 complicated in several ways:
25852 - ELF targets have save/restore routines for GPRs.
25854 - PPC64 ELF targets have routines for save/restore of GPRs that
25855 differ in what they do with the link register, so having a set
25856 prefix doesn't work. (We only use one of the save routines at
25857 the moment, though.)
25859 - PPC32 elf targets have "exit" versions of the restore routines
25860 that restore the link register and can save some extra space.
25861 These require an extra suffix. (There are also "tail" versions
25862 of the restore routines and "GOT" versions of the save routines,
25863 but we don't generate those at present. Same problems apply,
25864 though.)
25866 We deal with all this by synthesizing our own prefix/suffix and
25867 using that for the simple sprintf call shown above. */
25868 if (DEFAULT_ABI == ABI_V4)
25870 if (TARGET_64BIT)
25871 goto aix_names;
25873 if ((sel & SAVRES_REG) == SAVRES_GPR)
25874 prefix = (sel & SAVRES_SAVE) ? "_savegpr_" : "_restgpr_";
25875 else if ((sel & SAVRES_REG) == SAVRES_FPR)
25876 prefix = (sel & SAVRES_SAVE) ? "_savefpr_" : "_restfpr_";
25877 else if ((sel & SAVRES_REG) == SAVRES_VR)
25878 prefix = (sel & SAVRES_SAVE) ? "_savevr_" : "_restvr_";
25879 else
25880 abort ();
25882 if ((sel & SAVRES_LR))
25883 suffix = "_x";
25885 else if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
25887 #if !defined (POWERPC_LINUX) && !defined (POWERPC_FREEBSD)
25888 /* No out-of-line save/restore routines for GPRs on AIX. */
25889 gcc_assert (!TARGET_AIX || (sel & SAVRES_REG) != SAVRES_GPR);
25890 #endif
25892 aix_names:
25893 if ((sel & SAVRES_REG) == SAVRES_GPR)
25894 prefix = ((sel & SAVRES_SAVE)
25895 ? ((sel & SAVRES_LR) ? "_savegpr0_" : "_savegpr1_")
25896 : ((sel & SAVRES_LR) ? "_restgpr0_" : "_restgpr1_"));
25897 else if ((sel & SAVRES_REG) == SAVRES_FPR)
25899 #if defined (POWERPC_LINUX) || defined (POWERPC_FREEBSD)
25900 if ((sel & SAVRES_LR))
25901 prefix = ((sel & SAVRES_SAVE) ? "_savefpr_" : "_restfpr_");
25902 else
25903 #endif
25905 prefix = (sel & SAVRES_SAVE) ? SAVE_FP_PREFIX : RESTORE_FP_PREFIX;
25906 suffix = (sel & SAVRES_SAVE) ? SAVE_FP_SUFFIX : RESTORE_FP_SUFFIX;
25909 else if ((sel & SAVRES_REG) == SAVRES_VR)
25910 prefix = (sel & SAVRES_SAVE) ? "_savevr_" : "_restvr_";
25911 else
25912 abort ();
25915 if (DEFAULT_ABI == ABI_DARWIN)
25917 /* The Darwin approach is (slightly) different, in order to be
25918 compatible with code generated by the system toolchain. There is a
25919 single symbol for the start of save sequence, and the code here
25920 embeds an offset into that code on the basis of the first register
25921 to be saved. */
25922 prefix = (sel & SAVRES_SAVE) ? "save" : "rest" ;
25923 if ((sel & SAVRES_REG) == SAVRES_GPR)
25924 sprintf (savres_routine_name, "*%sGPR%s%s%.0d ; %s r%d-r31", prefix,
25925 ((sel & SAVRES_LR) ? "x" : ""), (regno == 13 ? "" : "+"),
25926 (regno - 13) * 4, prefix, regno);
25927 else if ((sel & SAVRES_REG) == SAVRES_FPR)
25928 sprintf (savres_routine_name, "*%sFP%s%.0d ; %s f%d-f31", prefix,
25929 (regno == 14 ? "" : "+"), (regno - 14) * 4, prefix, regno);
25930 else if ((sel & SAVRES_REG) == SAVRES_VR)
25931 sprintf (savres_routine_name, "*%sVEC%s%.0d ; %s v%d-v31", prefix,
25932 (regno == 20 ? "" : "+"), (regno - 20) * 8, prefix, regno);
25933 else
25934 abort ();
25936 else
25937 sprintf (savres_routine_name, "%s%d%s", prefix, regno, suffix);
25939 return savres_routine_name;
25942 /* Return an RTL SYMBOL_REF for an out-of-line register save/restore routine.
25943 We are saving/restoring GPRs if GPR is true. */
25945 static rtx
25946 rs6000_savres_routine_sym (rs6000_stack_t *info, int sel)
25948 int regno = ((sel & SAVRES_REG) == SAVRES_GPR
25949 ? info->first_gp_reg_save
25950 : (sel & SAVRES_REG) == SAVRES_FPR
25951 ? info->first_fp_reg_save - 32
25952 : (sel & SAVRES_REG) == SAVRES_VR
25953 ? info->first_altivec_reg_save - FIRST_ALTIVEC_REGNO
25954 : -1);
25955 rtx sym;
25956 int select = sel;
25958 /* Don't generate bogus routine names. */
25959 gcc_assert (FIRST_SAVRES_REGISTER <= regno
25960 && regno <= LAST_SAVRES_REGISTER
25961 && select >= 0 && select <= 12);
25963 sym = savres_routine_syms[regno-FIRST_SAVRES_REGISTER][select];
25965 if (sym == NULL)
25967 char *name;
25969 name = rs6000_savres_routine_name (regno, sel);
25971 sym = savres_routine_syms[regno-FIRST_SAVRES_REGISTER][select]
25972 = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
25973 SYMBOL_REF_FLAGS (sym) |= SYMBOL_FLAG_FUNCTION;
25976 return sym;
25979 /* Emit a sequence of insns, including a stack tie if needed, for
25980 resetting the stack pointer. If UPDT_REGNO is not 1, then don't
25981 reset the stack pointer, but move the base of the frame into
25982 reg UPDT_REGNO for use by out-of-line register restore routines. */
25984 static rtx
25985 rs6000_emit_stack_reset (rtx frame_reg_rtx, HOST_WIDE_INT frame_off,
25986 unsigned updt_regno)
25988 /* If there is nothing to do, don't do anything. */
25989 if (frame_off == 0 && REGNO (frame_reg_rtx) == updt_regno)
25990 return NULL_RTX;
25992 rtx updt_reg_rtx = gen_rtx_REG (Pmode, updt_regno);
25994 /* This blockage is needed so that sched doesn't decide to move
25995 the sp change before the register restores. */
25996 if (DEFAULT_ABI == ABI_V4)
25997 return emit_insn (gen_stack_restore_tie (updt_reg_rtx, frame_reg_rtx,
25998 GEN_INT (frame_off)));
26000 /* If we are restoring registers out-of-line, we will be using the
26001 "exit" variants of the restore routines, which will reset the
26002 stack for us. But we do need to point updt_reg into the
26003 right place for those routines. */
26004 if (frame_off != 0)
26005 return emit_insn (gen_add3_insn (updt_reg_rtx,
26006 frame_reg_rtx, GEN_INT (frame_off)));
26007 else
26008 return emit_move_insn (updt_reg_rtx, frame_reg_rtx);
26010 return NULL_RTX;
26013 /* Return the register number used as a pointer by out-of-line
26014 save/restore functions. */
26016 static inline unsigned
26017 ptr_regno_for_savres (int sel)
26019 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
26020 return (sel & SAVRES_REG) == SAVRES_FPR || (sel & SAVRES_LR) ? 1 : 12;
26021 return DEFAULT_ABI == ABI_DARWIN && (sel & SAVRES_REG) == SAVRES_FPR ? 1 : 11;
26024 /* Construct a parallel rtx describing the effect of a call to an
26025 out-of-line register save/restore routine, and emit the insn
26026 or jump_insn as appropriate. */
26028 static rtx_insn *
26029 rs6000_emit_savres_rtx (rs6000_stack_t *info,
26030 rtx frame_reg_rtx, int save_area_offset, int lr_offset,
26031 machine_mode reg_mode, int sel)
26033 int i;
26034 int offset, start_reg, end_reg, n_regs, use_reg;
26035 int reg_size = GET_MODE_SIZE (reg_mode);
26036 rtx sym;
26037 rtvec p;
26038 rtx par;
26039 rtx_insn *insn;
26041 offset = 0;
26042 start_reg = ((sel & SAVRES_REG) == SAVRES_GPR
26043 ? info->first_gp_reg_save
26044 : (sel & SAVRES_REG) == SAVRES_FPR
26045 ? info->first_fp_reg_save
26046 : (sel & SAVRES_REG) == SAVRES_VR
26047 ? info->first_altivec_reg_save
26048 : -1);
26049 end_reg = ((sel & SAVRES_REG) == SAVRES_GPR
26050 ? 32
26051 : (sel & SAVRES_REG) == SAVRES_FPR
26052 ? 64
26053 : (sel & SAVRES_REG) == SAVRES_VR
26054 ? LAST_ALTIVEC_REGNO + 1
26055 : -1);
26056 n_regs = end_reg - start_reg;
26057 p = rtvec_alloc (3 + ((sel & SAVRES_LR) ? 1 : 0)
26058 + ((sel & SAVRES_REG) == SAVRES_VR ? 1 : 0)
26059 + n_regs);
26061 if (!(sel & SAVRES_SAVE) && (sel & SAVRES_LR))
26062 RTVEC_ELT (p, offset++) = ret_rtx;
26064 RTVEC_ELT (p, offset++) = gen_hard_reg_clobber (Pmode, LR_REGNO);
26066 sym = rs6000_savres_routine_sym (info, sel);
26067 RTVEC_ELT (p, offset++) = gen_rtx_USE (VOIDmode, sym);
26069 use_reg = ptr_regno_for_savres (sel);
26070 if ((sel & SAVRES_REG) == SAVRES_VR)
26072 /* Vector regs are saved/restored using [reg+reg] addressing. */
26073 RTVEC_ELT (p, offset++) = gen_hard_reg_clobber (Pmode, use_reg);
26074 RTVEC_ELT (p, offset++)
26075 = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, 0));
26077 else
26078 RTVEC_ELT (p, offset++)
26079 = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, use_reg));
26081 for (i = 0; i < end_reg - start_reg; i++)
26082 RTVEC_ELT (p, i + offset)
26083 = gen_frame_set (gen_rtx_REG (reg_mode, start_reg + i),
26084 frame_reg_rtx, save_area_offset + reg_size * i,
26085 (sel & SAVRES_SAVE) != 0);
26087 if ((sel & SAVRES_SAVE) && (sel & SAVRES_LR))
26088 RTVEC_ELT (p, i + offset)
26089 = gen_frame_store (gen_rtx_REG (Pmode, 0), frame_reg_rtx, lr_offset);
26091 par = gen_rtx_PARALLEL (VOIDmode, p);
26093 if (!(sel & SAVRES_SAVE) && (sel & SAVRES_LR))
26095 insn = emit_jump_insn (par);
26096 JUMP_LABEL (insn) = ret_rtx;
26098 else
26099 insn = emit_insn (par);
26100 return insn;
26103 /* Emit prologue code to store CR fields that need to be saved into REG. This
26104 function should only be called when moving the non-volatile CRs to REG, it
26105 is not a general purpose routine to move the entire set of CRs to REG.
26106 Specifically, gen_prologue_movesi_from_cr() does not contain uses of the
26107 volatile CRs. */
26109 static void
26110 rs6000_emit_prologue_move_from_cr (rtx reg)
26112 /* Only the ELFv2 ABI allows storing only selected fields. */
26113 if (DEFAULT_ABI == ABI_ELFv2 && TARGET_MFCRF)
26115 int i, cr_reg[8], count = 0;
26117 /* Collect CR fields that must be saved. */
26118 for (i = 0; i < 8; i++)
26119 if (save_reg_p (CR0_REGNO + i))
26120 cr_reg[count++] = i;
26122 /* If it's just a single one, use mfcrf. */
26123 if (count == 1)
26125 rtvec p = rtvec_alloc (1);
26126 rtvec r = rtvec_alloc (2);
26127 RTVEC_ELT (r, 0) = gen_rtx_REG (CCmode, CR0_REGNO + cr_reg[0]);
26128 RTVEC_ELT (r, 1) = GEN_INT (1 << (7 - cr_reg[0]));
26129 RTVEC_ELT (p, 0)
26130 = gen_rtx_SET (reg,
26131 gen_rtx_UNSPEC (SImode, r, UNSPEC_MOVESI_FROM_CR));
26133 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
26134 return;
26137 /* ??? It might be better to handle count == 2 / 3 cases here
26138 as well, using logical operations to combine the values. */
26141 emit_insn (gen_prologue_movesi_from_cr (reg));
26144 /* Return whether the split-stack arg pointer (r12) is used. */
26146 static bool
26147 split_stack_arg_pointer_used_p (void)
26149 /* If the pseudo holding the arg pointer is no longer a pseudo,
26150 then the arg pointer is used. */
26151 if (cfun->machine->split_stack_arg_pointer != NULL_RTX
26152 && (!REG_P (cfun->machine->split_stack_arg_pointer)
26153 || HARD_REGISTER_P (cfun->machine->split_stack_arg_pointer)))
26154 return true;
26156 /* Unfortunately we also need to do some code scanning, since
26157 r12 may have been substituted for the pseudo. */
26158 rtx_insn *insn;
26159 basic_block bb = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb;
26160 FOR_BB_INSNS (bb, insn)
26161 if (NONDEBUG_INSN_P (insn))
26163 /* A call destroys r12. */
26164 if (CALL_P (insn))
26165 return false;
26167 df_ref use;
26168 FOR_EACH_INSN_USE (use, insn)
26170 rtx x = DF_REF_REG (use);
26171 if (REG_P (x) && REGNO (x) == 12)
26172 return true;
26174 df_ref def;
26175 FOR_EACH_INSN_DEF (def, insn)
26177 rtx x = DF_REF_REG (def);
26178 if (REG_P (x) && REGNO (x) == 12)
26179 return false;
26182 return bitmap_bit_p (DF_LR_OUT (bb), 12);
26185 /* Return whether we need to emit an ELFv2 global entry point prologue. */
26187 static bool
26188 rs6000_global_entry_point_needed_p (void)
26190 /* Only needed for the ELFv2 ABI. */
26191 if (DEFAULT_ABI != ABI_ELFv2)
26192 return false;
26194 /* With -msingle-pic-base, we assume the whole program shares the same
26195 TOC, so no global entry point prologues are needed anywhere. */
26196 if (TARGET_SINGLE_PIC_BASE)
26197 return false;
26199 /* Ensure we have a global entry point for thunks. ??? We could
26200 avoid that if the target routine doesn't need a global entry point,
26201 but we do not know whether this is the case at this point. */
26202 if (cfun->is_thunk)
26203 return true;
26205 /* For regular functions, rs6000_emit_prologue sets this flag if the
26206 routine ever uses the TOC pointer. */
26207 return cfun->machine->r2_setup_needed;
26210 /* Implement TARGET_SHRINK_WRAP_GET_SEPARATE_COMPONENTS. */
26211 static sbitmap
26212 rs6000_get_separate_components (void)
26214 rs6000_stack_t *info = rs6000_stack_info ();
26216 if (WORLD_SAVE_P (info))
26217 return NULL;
26219 gcc_assert (!(info->savres_strategy & SAVE_MULTIPLE)
26220 && !(info->savres_strategy & REST_MULTIPLE));
26222 /* Component 0 is the save/restore of LR (done via GPR0).
26223 Component 2 is the save of the TOC (GPR2).
26224 Components 13..31 are the save/restore of GPR13..GPR31.
26225 Components 46..63 are the save/restore of FPR14..FPR31. */
26227 cfun->machine->n_components = 64;
26229 sbitmap components = sbitmap_alloc (cfun->machine->n_components);
26230 bitmap_clear (components);
26232 int reg_size = TARGET_32BIT ? 4 : 8;
26233 int fp_reg_size = 8;
26235 /* The GPRs we need saved to the frame. */
26236 if ((info->savres_strategy & SAVE_INLINE_GPRS)
26237 && (info->savres_strategy & REST_INLINE_GPRS))
26239 int offset = info->gp_save_offset;
26240 if (info->push_p)
26241 offset += info->total_size;
26243 for (unsigned regno = info->first_gp_reg_save; regno < 32; regno++)
26245 if (IN_RANGE (offset, -0x8000, 0x7fff)
26246 && save_reg_p (regno))
26247 bitmap_set_bit (components, regno);
26249 offset += reg_size;
26253 /* Don't mess with the hard frame pointer. */
26254 if (frame_pointer_needed)
26255 bitmap_clear_bit (components, HARD_FRAME_POINTER_REGNUM);
26257 /* Don't mess with the fixed TOC register. */
26258 if ((TARGET_TOC && TARGET_MINIMAL_TOC)
26259 || (flag_pic == 1 && DEFAULT_ABI == ABI_V4)
26260 || (flag_pic && DEFAULT_ABI == ABI_DARWIN))
26261 bitmap_clear_bit (components, RS6000_PIC_OFFSET_TABLE_REGNUM);
26263 /* The FPRs we need saved to the frame. */
26264 if ((info->savres_strategy & SAVE_INLINE_FPRS)
26265 && (info->savres_strategy & REST_INLINE_FPRS))
26267 int offset = info->fp_save_offset;
26268 if (info->push_p)
26269 offset += info->total_size;
26271 for (unsigned regno = info->first_fp_reg_save; regno < 64; regno++)
26273 if (IN_RANGE (offset, -0x8000, 0x7fff) && save_reg_p (regno))
26274 bitmap_set_bit (components, regno);
26276 offset += fp_reg_size;
26280 /* Optimize LR save and restore if we can. This is component 0. Any
26281 out-of-line register save/restore routines need LR. */
26282 if (info->lr_save_p
26283 && !(flag_pic && (DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN))
26284 && (info->savres_strategy & SAVE_INLINE_GPRS)
26285 && (info->savres_strategy & REST_INLINE_GPRS)
26286 && (info->savres_strategy & SAVE_INLINE_FPRS)
26287 && (info->savres_strategy & REST_INLINE_FPRS)
26288 && (info->savres_strategy & SAVE_INLINE_VRS)
26289 && (info->savres_strategy & REST_INLINE_VRS))
26291 int offset = info->lr_save_offset;
26292 if (info->push_p)
26293 offset += info->total_size;
26294 if (IN_RANGE (offset, -0x8000, 0x7fff))
26295 bitmap_set_bit (components, 0);
26298 /* Optimize saving the TOC. This is component 2. */
26299 if (cfun->machine->save_toc_in_prologue)
26300 bitmap_set_bit (components, 2);
26302 return components;
26305 /* Implement TARGET_SHRINK_WRAP_COMPONENTS_FOR_BB. */
26306 static sbitmap
26307 rs6000_components_for_bb (basic_block bb)
26309 rs6000_stack_t *info = rs6000_stack_info ();
26311 bitmap in = DF_LIVE_IN (bb);
26312 bitmap gen = &DF_LIVE_BB_INFO (bb)->gen;
26313 bitmap kill = &DF_LIVE_BB_INFO (bb)->kill;
26315 sbitmap components = sbitmap_alloc (cfun->machine->n_components);
26316 bitmap_clear (components);
26318 /* A register is used in a bb if it is in the IN, GEN, or KILL sets. */
26320 /* GPRs. */
26321 for (unsigned regno = info->first_gp_reg_save; regno < 32; regno++)
26322 if (bitmap_bit_p (in, regno)
26323 || bitmap_bit_p (gen, regno)
26324 || bitmap_bit_p (kill, regno))
26325 bitmap_set_bit (components, regno);
26327 /* FPRs. */
26328 for (unsigned regno = info->first_fp_reg_save; regno < 64; regno++)
26329 if (bitmap_bit_p (in, regno)
26330 || bitmap_bit_p (gen, regno)
26331 || bitmap_bit_p (kill, regno))
26332 bitmap_set_bit (components, regno);
26334 /* The link register. */
26335 if (bitmap_bit_p (in, LR_REGNO)
26336 || bitmap_bit_p (gen, LR_REGNO)
26337 || bitmap_bit_p (kill, LR_REGNO))
26338 bitmap_set_bit (components, 0);
26340 /* The TOC save. */
26341 if (bitmap_bit_p (in, TOC_REGNUM)
26342 || bitmap_bit_p (gen, TOC_REGNUM)
26343 || bitmap_bit_p (kill, TOC_REGNUM))
26344 bitmap_set_bit (components, 2);
26346 return components;
26349 /* Implement TARGET_SHRINK_WRAP_DISQUALIFY_COMPONENTS. */
26350 static void
26351 rs6000_disqualify_components (sbitmap components, edge e,
26352 sbitmap edge_components, bool /*is_prologue*/)
26354 /* Our LR pro/epilogue code moves LR via R0, so R0 had better not be
26355 live where we want to place that code. */
26356 if (bitmap_bit_p (edge_components, 0)
26357 && bitmap_bit_p (DF_LIVE_IN (e->dest), 0))
26359 if (dump_file)
26360 fprintf (dump_file, "Disqualifying LR because GPR0 is live "
26361 "on entry to bb %d\n", e->dest->index);
26362 bitmap_clear_bit (components, 0);
26366 /* Implement TARGET_SHRINK_WRAP_EMIT_PROLOGUE_COMPONENTS. */
26367 static void
26368 rs6000_emit_prologue_components (sbitmap components)
26370 rs6000_stack_t *info = rs6000_stack_info ();
26371 rtx ptr_reg = gen_rtx_REG (Pmode, frame_pointer_needed
26372 ? HARD_FRAME_POINTER_REGNUM
26373 : STACK_POINTER_REGNUM);
26375 machine_mode reg_mode = Pmode;
26376 int reg_size = TARGET_32BIT ? 4 : 8;
26377 machine_mode fp_reg_mode = TARGET_HARD_FLOAT ? DFmode : SFmode;
26378 int fp_reg_size = 8;
26380 /* Prologue for LR. */
26381 if (bitmap_bit_p (components, 0))
26383 rtx lr = gen_rtx_REG (reg_mode, LR_REGNO);
26384 rtx reg = gen_rtx_REG (reg_mode, 0);
26385 rtx_insn *insn = emit_move_insn (reg, lr);
26386 RTX_FRAME_RELATED_P (insn) = 1;
26387 add_reg_note (insn, REG_CFA_REGISTER, gen_rtx_SET (reg, lr));
26389 int offset = info->lr_save_offset;
26390 if (info->push_p)
26391 offset += info->total_size;
26393 insn = emit_insn (gen_frame_store (reg, ptr_reg, offset));
26394 RTX_FRAME_RELATED_P (insn) = 1;
26395 rtx mem = copy_rtx (SET_DEST (single_set (insn)));
26396 add_reg_note (insn, REG_CFA_OFFSET, gen_rtx_SET (mem, lr));
26399 /* Prologue for TOC. */
26400 if (bitmap_bit_p (components, 2))
26402 rtx reg = gen_rtx_REG (reg_mode, TOC_REGNUM);
26403 rtx sp_reg = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
26404 emit_insn (gen_frame_store (reg, sp_reg, RS6000_TOC_SAVE_SLOT));
26407 /* Prologue for the GPRs. */
26408 int offset = info->gp_save_offset;
26409 if (info->push_p)
26410 offset += info->total_size;
26412 for (int i = info->first_gp_reg_save; i < 32; i++)
26414 if (bitmap_bit_p (components, i))
26416 rtx reg = gen_rtx_REG (reg_mode, i);
26417 rtx_insn *insn = emit_insn (gen_frame_store (reg, ptr_reg, offset));
26418 RTX_FRAME_RELATED_P (insn) = 1;
26419 rtx set = copy_rtx (single_set (insn));
26420 add_reg_note (insn, REG_CFA_OFFSET, set);
26423 offset += reg_size;
26426 /* Prologue for the FPRs. */
26427 offset = info->fp_save_offset;
26428 if (info->push_p)
26429 offset += info->total_size;
26431 for (int i = info->first_fp_reg_save; i < 64; i++)
26433 if (bitmap_bit_p (components, i))
26435 rtx reg = gen_rtx_REG (fp_reg_mode, i);
26436 rtx_insn *insn = emit_insn (gen_frame_store (reg, ptr_reg, offset));
26437 RTX_FRAME_RELATED_P (insn) = 1;
26438 rtx set = copy_rtx (single_set (insn));
26439 add_reg_note (insn, REG_CFA_OFFSET, set);
26442 offset += fp_reg_size;
26446 /* Implement TARGET_SHRINK_WRAP_EMIT_EPILOGUE_COMPONENTS. */
26447 static void
26448 rs6000_emit_epilogue_components (sbitmap components)
26450 rs6000_stack_t *info = rs6000_stack_info ();
26451 rtx ptr_reg = gen_rtx_REG (Pmode, frame_pointer_needed
26452 ? HARD_FRAME_POINTER_REGNUM
26453 : STACK_POINTER_REGNUM);
26455 machine_mode reg_mode = Pmode;
26456 int reg_size = TARGET_32BIT ? 4 : 8;
26458 machine_mode fp_reg_mode = TARGET_HARD_FLOAT ? DFmode : SFmode;
26459 int fp_reg_size = 8;
26461 /* Epilogue for the FPRs. */
26462 int offset = info->fp_save_offset;
26463 if (info->push_p)
26464 offset += info->total_size;
26466 for (int i = info->first_fp_reg_save; i < 64; i++)
26468 if (bitmap_bit_p (components, i))
26470 rtx reg = gen_rtx_REG (fp_reg_mode, i);
26471 rtx_insn *insn = emit_insn (gen_frame_load (reg, ptr_reg, offset));
26472 RTX_FRAME_RELATED_P (insn) = 1;
26473 add_reg_note (insn, REG_CFA_RESTORE, reg);
26476 offset += fp_reg_size;
26479 /* Epilogue for the GPRs. */
26480 offset = info->gp_save_offset;
26481 if (info->push_p)
26482 offset += info->total_size;
26484 for (int i = info->first_gp_reg_save; i < 32; i++)
26486 if (bitmap_bit_p (components, i))
26488 rtx reg = gen_rtx_REG (reg_mode, i);
26489 rtx_insn *insn = emit_insn (gen_frame_load (reg, ptr_reg, offset));
26490 RTX_FRAME_RELATED_P (insn) = 1;
26491 add_reg_note (insn, REG_CFA_RESTORE, reg);
26494 offset += reg_size;
26497 /* Epilogue for LR. */
26498 if (bitmap_bit_p (components, 0))
26500 int offset = info->lr_save_offset;
26501 if (info->push_p)
26502 offset += info->total_size;
26504 rtx reg = gen_rtx_REG (reg_mode, 0);
26505 rtx_insn *insn = emit_insn (gen_frame_load (reg, ptr_reg, offset));
26507 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
26508 insn = emit_move_insn (lr, reg);
26509 RTX_FRAME_RELATED_P (insn) = 1;
26510 add_reg_note (insn, REG_CFA_RESTORE, lr);
26514 /* Implement TARGET_SHRINK_WRAP_SET_HANDLED_COMPONENTS. */
26515 static void
26516 rs6000_set_handled_components (sbitmap components)
26518 rs6000_stack_t *info = rs6000_stack_info ();
26520 for (int i = info->first_gp_reg_save; i < 32; i++)
26521 if (bitmap_bit_p (components, i))
26522 cfun->machine->gpr_is_wrapped_separately[i] = true;
26524 for (int i = info->first_fp_reg_save; i < 64; i++)
26525 if (bitmap_bit_p (components, i))
26526 cfun->machine->fpr_is_wrapped_separately[i - 32] = true;
26528 if (bitmap_bit_p (components, 0))
26529 cfun->machine->lr_is_wrapped_separately = true;
26531 if (bitmap_bit_p (components, 2))
26532 cfun->machine->toc_is_wrapped_separately = true;
26535 /* VRSAVE is a bit vector representing which AltiVec registers
26536 are used. The OS uses this to determine which vector
26537 registers to save on a context switch. We need to save
26538 VRSAVE on the stack frame, add whatever AltiVec registers we
26539 used in this function, and do the corresponding magic in the
26540 epilogue. */
26541 static void
26542 emit_vrsave_prologue (rs6000_stack_t *info, int save_regno,
26543 HOST_WIDE_INT frame_off, rtx frame_reg_rtx)
26545 /* Get VRSAVE into a GPR. */
26546 rtx reg = gen_rtx_REG (SImode, save_regno);
26547 rtx vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
26548 if (TARGET_MACHO)
26549 emit_insn (gen_get_vrsave_internal (reg));
26550 else
26551 emit_insn (gen_rtx_SET (reg, vrsave));
26553 /* Save VRSAVE. */
26554 int offset = info->vrsave_save_offset + frame_off;
26555 emit_insn (gen_frame_store (reg, frame_reg_rtx, offset));
26557 /* Include the registers in the mask. */
26558 emit_insn (gen_iorsi3 (reg, reg, GEN_INT (info->vrsave_mask)));
26560 emit_insn (generate_set_vrsave (reg, info, 0));
26563 /* Set up the arg pointer (r12) for -fsplit-stack code. If __morestack was
26564 called, it left the arg pointer to the old stack in r29. Otherwise, the
26565 arg pointer is the top of the current frame. */
26566 static void
26567 emit_split_stack_prologue (rs6000_stack_t *info, rtx_insn *sp_adjust,
26568 HOST_WIDE_INT frame_off, rtx frame_reg_rtx)
26570 cfun->machine->split_stack_argp_used = true;
26572 if (sp_adjust)
26574 rtx r12 = gen_rtx_REG (Pmode, 12);
26575 rtx sp_reg_rtx = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
26576 rtx set_r12 = gen_rtx_SET (r12, sp_reg_rtx);
26577 emit_insn_before (set_r12, sp_adjust);
26579 else if (frame_off != 0 || REGNO (frame_reg_rtx) != 12)
26581 rtx r12 = gen_rtx_REG (Pmode, 12);
26582 if (frame_off == 0)
26583 emit_move_insn (r12, frame_reg_rtx);
26584 else
26585 emit_insn (gen_add3_insn (r12, frame_reg_rtx, GEN_INT (frame_off)));
26588 if (info->push_p)
26590 rtx r12 = gen_rtx_REG (Pmode, 12);
26591 rtx r29 = gen_rtx_REG (Pmode, 29);
26592 rtx cr7 = gen_rtx_REG (CCUNSmode, CR7_REGNO);
26593 rtx not_more = gen_label_rtx ();
26594 rtx jump;
26596 jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
26597 gen_rtx_GEU (VOIDmode, cr7, const0_rtx),
26598 gen_rtx_LABEL_REF (VOIDmode, not_more),
26599 pc_rtx);
26600 jump = emit_jump_insn (gen_rtx_SET (pc_rtx, jump));
26601 JUMP_LABEL (jump) = not_more;
26602 LABEL_NUSES (not_more) += 1;
26603 emit_move_insn (r12, r29);
26604 emit_label (not_more);
26608 /* Emit function prologue as insns. */
26610 void
26611 rs6000_emit_prologue (void)
26613 rs6000_stack_t *info = rs6000_stack_info ();
26614 machine_mode reg_mode = Pmode;
26615 int reg_size = TARGET_32BIT ? 4 : 8;
26616 machine_mode fp_reg_mode = TARGET_HARD_FLOAT ? DFmode : SFmode;
26617 int fp_reg_size = 8;
26618 rtx sp_reg_rtx = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
26619 rtx frame_reg_rtx = sp_reg_rtx;
26620 unsigned int cr_save_regno;
26621 rtx cr_save_rtx = NULL_RTX;
26622 rtx_insn *insn;
26623 int strategy;
26624 int using_static_chain_p = (cfun->static_chain_decl != NULL_TREE
26625 && df_regs_ever_live_p (STATIC_CHAIN_REGNUM)
26626 && call_used_regs[STATIC_CHAIN_REGNUM]);
26627 int using_split_stack = (flag_split_stack
26628 && (lookup_attribute ("no_split_stack",
26629 DECL_ATTRIBUTES (cfun->decl))
26630 == NULL));
26632 /* Offset to top of frame for frame_reg and sp respectively. */
26633 HOST_WIDE_INT frame_off = 0;
26634 HOST_WIDE_INT sp_off = 0;
26635 /* sp_adjust is the stack adjusting instruction, tracked so that the
26636 insn setting up the split-stack arg pointer can be emitted just
26637 prior to it, when r12 is not used here for other purposes. */
26638 rtx_insn *sp_adjust = 0;
26640 #if CHECKING_P
26641 /* Track and check usage of r0, r11, r12. */
26642 int reg_inuse = using_static_chain_p ? 1 << 11 : 0;
26643 #define START_USE(R) do \
26645 gcc_assert ((reg_inuse & (1 << (R))) == 0); \
26646 reg_inuse |= 1 << (R); \
26647 } while (0)
26648 #define END_USE(R) do \
26650 gcc_assert ((reg_inuse & (1 << (R))) != 0); \
26651 reg_inuse &= ~(1 << (R)); \
26652 } while (0)
26653 #define NOT_INUSE(R) do \
26655 gcc_assert ((reg_inuse & (1 << (R))) == 0); \
26656 } while (0)
26657 #else
26658 #define START_USE(R) do {} while (0)
26659 #define END_USE(R) do {} while (0)
26660 #define NOT_INUSE(R) do {} while (0)
26661 #endif
26663 if (DEFAULT_ABI == ABI_ELFv2
26664 && !TARGET_SINGLE_PIC_BASE)
26666 cfun->machine->r2_setup_needed = df_regs_ever_live_p (TOC_REGNUM);
26668 /* With -mminimal-toc we may generate an extra use of r2 below. */
26669 if (TARGET_TOC && TARGET_MINIMAL_TOC
26670 && !constant_pool_empty_p ())
26671 cfun->machine->r2_setup_needed = true;
26675 if (flag_stack_usage_info)
26676 current_function_static_stack_size = info->total_size;
26678 if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
26680 HOST_WIDE_INT size = info->total_size;
26682 if (crtl->is_leaf && !cfun->calls_alloca)
26684 if (size > PROBE_INTERVAL && size > get_stack_check_protect ())
26685 rs6000_emit_probe_stack_range (get_stack_check_protect (),
26686 size - get_stack_check_protect ());
26688 else if (size > 0)
26689 rs6000_emit_probe_stack_range (get_stack_check_protect (), size);
26692 if (TARGET_FIX_AND_CONTINUE)
26694 /* gdb on darwin arranges to forward a function from the old
26695 address by modifying the first 5 instructions of the function
26696 to branch to the overriding function. This is necessary to
26697 permit function pointers that point to the old function to
26698 actually forward to the new function. */
26699 emit_insn (gen_nop ());
26700 emit_insn (gen_nop ());
26701 emit_insn (gen_nop ());
26702 emit_insn (gen_nop ());
26703 emit_insn (gen_nop ());
26706 /* Handle world saves specially here. */
26707 if (WORLD_SAVE_P (info))
26709 int i, j, sz;
26710 rtx treg;
26711 rtvec p;
26712 rtx reg0;
26714 /* save_world expects lr in r0. */
26715 reg0 = gen_rtx_REG (Pmode, 0);
26716 if (info->lr_save_p)
26718 insn = emit_move_insn (reg0,
26719 gen_rtx_REG (Pmode, LR_REGNO));
26720 RTX_FRAME_RELATED_P (insn) = 1;
26723 /* The SAVE_WORLD and RESTORE_WORLD routines make a number of
26724 assumptions about the offsets of various bits of the stack
26725 frame. */
26726 gcc_assert (info->gp_save_offset == -220
26727 && info->fp_save_offset == -144
26728 && info->lr_save_offset == 8
26729 && info->cr_save_offset == 4
26730 && info->push_p
26731 && info->lr_save_p
26732 && (!crtl->calls_eh_return
26733 || info->ehrd_offset == -432)
26734 && info->vrsave_save_offset == -224
26735 && info->altivec_save_offset == -416);
26737 treg = gen_rtx_REG (SImode, 11);
26738 emit_move_insn (treg, GEN_INT (-info->total_size));
26740 /* SAVE_WORLD takes the caller's LR in R0 and the frame size
26741 in R11. It also clobbers R12, so beware! */
26743 /* Preserve CR2 for save_world prologues */
26744 sz = 5;
26745 sz += 32 - info->first_gp_reg_save;
26746 sz += 64 - info->first_fp_reg_save;
26747 sz += LAST_ALTIVEC_REGNO - info->first_altivec_reg_save + 1;
26748 p = rtvec_alloc (sz);
26749 j = 0;
26750 RTVEC_ELT (p, j++) = gen_hard_reg_clobber (SImode, LR_REGNO);
26751 RTVEC_ELT (p, j++) = gen_rtx_USE (VOIDmode,
26752 gen_rtx_SYMBOL_REF (Pmode,
26753 "*save_world"));
26754 /* We do floats first so that the instruction pattern matches
26755 properly. */
26756 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
26757 RTVEC_ELT (p, j++)
26758 = gen_frame_store (gen_rtx_REG (TARGET_HARD_FLOAT ? DFmode : SFmode,
26759 info->first_fp_reg_save + i),
26760 frame_reg_rtx,
26761 info->fp_save_offset + frame_off + 8 * i);
26762 for (i = 0; info->first_altivec_reg_save + i <= LAST_ALTIVEC_REGNO; i++)
26763 RTVEC_ELT (p, j++)
26764 = gen_frame_store (gen_rtx_REG (V4SImode,
26765 info->first_altivec_reg_save + i),
26766 frame_reg_rtx,
26767 info->altivec_save_offset + frame_off + 16 * i);
26768 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
26769 RTVEC_ELT (p, j++)
26770 = gen_frame_store (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
26771 frame_reg_rtx,
26772 info->gp_save_offset + frame_off + reg_size * i);
26774 /* CR register traditionally saved as CR2. */
26775 RTVEC_ELT (p, j++)
26776 = gen_frame_store (gen_rtx_REG (SImode, CR2_REGNO),
26777 frame_reg_rtx, info->cr_save_offset + frame_off);
26778 /* Explain about use of R0. */
26779 if (info->lr_save_p)
26780 RTVEC_ELT (p, j++)
26781 = gen_frame_store (reg0,
26782 frame_reg_rtx, info->lr_save_offset + frame_off);
26783 /* Explain what happens to the stack pointer. */
26785 rtx newval = gen_rtx_PLUS (Pmode, sp_reg_rtx, treg);
26786 RTVEC_ELT (p, j++) = gen_rtx_SET (sp_reg_rtx, newval);
26789 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
26790 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
26791 treg, GEN_INT (-info->total_size));
26792 sp_off = frame_off = info->total_size;
26795 strategy = info->savres_strategy;
26797 /* For V.4, update stack before we do any saving and set back pointer. */
26798 if (! WORLD_SAVE_P (info)
26799 && info->push_p
26800 && (DEFAULT_ABI == ABI_V4
26801 || crtl->calls_eh_return))
26803 bool need_r11 = (!(strategy & SAVE_INLINE_FPRS)
26804 || !(strategy & SAVE_INLINE_GPRS)
26805 || !(strategy & SAVE_INLINE_VRS));
26806 int ptr_regno = -1;
26807 rtx ptr_reg = NULL_RTX;
26808 int ptr_off = 0;
26810 if (info->total_size < 32767)
26811 frame_off = info->total_size;
26812 else if (need_r11)
26813 ptr_regno = 11;
26814 else if (info->cr_save_p
26815 || info->lr_save_p
26816 || info->first_fp_reg_save < 64
26817 || info->first_gp_reg_save < 32
26818 || info->altivec_size != 0
26819 || info->vrsave_size != 0
26820 || crtl->calls_eh_return)
26821 ptr_regno = 12;
26822 else
26824 /* The prologue won't be saving any regs so there is no need
26825 to set up a frame register to access any frame save area.
26826 We also won't be using frame_off anywhere below, but set
26827 the correct value anyway to protect against future
26828 changes to this function. */
26829 frame_off = info->total_size;
26831 if (ptr_regno != -1)
26833 /* Set up the frame offset to that needed by the first
26834 out-of-line save function. */
26835 START_USE (ptr_regno);
26836 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
26837 frame_reg_rtx = ptr_reg;
26838 if (!(strategy & SAVE_INLINE_FPRS) && info->fp_size != 0)
26839 gcc_checking_assert (info->fp_save_offset + info->fp_size == 0);
26840 else if (!(strategy & SAVE_INLINE_GPRS) && info->first_gp_reg_save < 32)
26841 ptr_off = info->gp_save_offset + info->gp_size;
26842 else if (!(strategy & SAVE_INLINE_VRS) && info->altivec_size != 0)
26843 ptr_off = info->altivec_save_offset + info->altivec_size;
26844 frame_off = -ptr_off;
26846 sp_adjust = rs6000_emit_allocate_stack (info->total_size,
26847 ptr_reg, ptr_off);
26848 if (REGNO (frame_reg_rtx) == 12)
26849 sp_adjust = 0;
26850 sp_off = info->total_size;
26851 if (frame_reg_rtx != sp_reg_rtx)
26852 rs6000_emit_stack_tie (frame_reg_rtx, false);
26855 /* If we use the link register, get it into r0. */
26856 if (!WORLD_SAVE_P (info) && info->lr_save_p
26857 && !cfun->machine->lr_is_wrapped_separately)
26859 rtx addr, reg, mem;
26861 reg = gen_rtx_REG (Pmode, 0);
26862 START_USE (0);
26863 insn = emit_move_insn (reg, gen_rtx_REG (Pmode, LR_REGNO));
26864 RTX_FRAME_RELATED_P (insn) = 1;
26866 if (!(strategy & (SAVE_NOINLINE_GPRS_SAVES_LR
26867 | SAVE_NOINLINE_FPRS_SAVES_LR)))
26869 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
26870 GEN_INT (info->lr_save_offset + frame_off));
26871 mem = gen_rtx_MEM (Pmode, addr);
26872 /* This should not be of rs6000_sr_alias_set, because of
26873 __builtin_return_address. */
26875 insn = emit_move_insn (mem, reg);
26876 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
26877 NULL_RTX, NULL_RTX);
26878 END_USE (0);
26882 /* If we need to save CR, put it into r12 or r11. Choose r12 except when
26883 r12 will be needed by out-of-line gpr save. */
26884 cr_save_regno = ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
26885 && !(strategy & (SAVE_INLINE_GPRS
26886 | SAVE_NOINLINE_GPRS_SAVES_LR))
26887 ? 11 : 12);
26888 if (!WORLD_SAVE_P (info)
26889 && info->cr_save_p
26890 && REGNO (frame_reg_rtx) != cr_save_regno
26891 && !(using_static_chain_p && cr_save_regno == 11)
26892 && !(using_split_stack && cr_save_regno == 12 && sp_adjust))
26894 cr_save_rtx = gen_rtx_REG (SImode, cr_save_regno);
26895 START_USE (cr_save_regno);
26896 rs6000_emit_prologue_move_from_cr (cr_save_rtx);
26899 /* Do any required saving of fpr's. If only one or two to save, do
26900 it ourselves. Otherwise, call function. */
26901 if (!WORLD_SAVE_P (info) && (strategy & SAVE_INLINE_FPRS))
26903 int offset = info->fp_save_offset + frame_off;
26904 for (int i = info->first_fp_reg_save; i < 64; i++)
26906 if (save_reg_p (i)
26907 && !cfun->machine->fpr_is_wrapped_separately[i - 32])
26908 emit_frame_save (frame_reg_rtx, fp_reg_mode, i, offset,
26909 sp_off - frame_off);
26911 offset += fp_reg_size;
26914 else if (!WORLD_SAVE_P (info) && info->first_fp_reg_save != 64)
26916 bool lr = (strategy & SAVE_NOINLINE_FPRS_SAVES_LR) != 0;
26917 int sel = SAVRES_SAVE | SAVRES_FPR | (lr ? SAVRES_LR : 0);
26918 unsigned ptr_regno = ptr_regno_for_savres (sel);
26919 rtx ptr_reg = frame_reg_rtx;
26921 if (REGNO (frame_reg_rtx) == ptr_regno)
26922 gcc_checking_assert (frame_off == 0);
26923 else
26925 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
26926 NOT_INUSE (ptr_regno);
26927 emit_insn (gen_add3_insn (ptr_reg,
26928 frame_reg_rtx, GEN_INT (frame_off)));
26930 insn = rs6000_emit_savres_rtx (info, ptr_reg,
26931 info->fp_save_offset,
26932 info->lr_save_offset,
26933 DFmode, sel);
26934 rs6000_frame_related (insn, ptr_reg, sp_off,
26935 NULL_RTX, NULL_RTX);
26936 if (lr)
26937 END_USE (0);
26940 /* Save GPRs. This is done as a PARALLEL if we are using
26941 the store-multiple instructions. */
26942 if (!WORLD_SAVE_P (info) && !(strategy & SAVE_INLINE_GPRS))
26944 bool lr = (strategy & SAVE_NOINLINE_GPRS_SAVES_LR) != 0;
26945 int sel = SAVRES_SAVE | SAVRES_GPR | (lr ? SAVRES_LR : 0);
26946 unsigned ptr_regno = ptr_regno_for_savres (sel);
26947 rtx ptr_reg = frame_reg_rtx;
26948 bool ptr_set_up = REGNO (ptr_reg) == ptr_regno;
26949 int end_save = info->gp_save_offset + info->gp_size;
26950 int ptr_off;
26952 if (ptr_regno == 12)
26953 sp_adjust = 0;
26954 if (!ptr_set_up)
26955 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
26957 /* Need to adjust r11 (r12) if we saved any FPRs. */
26958 if (end_save + frame_off != 0)
26960 rtx offset = GEN_INT (end_save + frame_off);
26962 if (ptr_set_up)
26963 frame_off = -end_save;
26964 else
26965 NOT_INUSE (ptr_regno);
26966 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
26968 else if (!ptr_set_up)
26970 NOT_INUSE (ptr_regno);
26971 emit_move_insn (ptr_reg, frame_reg_rtx);
26973 ptr_off = -end_save;
26974 insn = rs6000_emit_savres_rtx (info, ptr_reg,
26975 info->gp_save_offset + ptr_off,
26976 info->lr_save_offset + ptr_off,
26977 reg_mode, sel);
26978 rs6000_frame_related (insn, ptr_reg, sp_off - ptr_off,
26979 NULL_RTX, NULL_RTX);
26980 if (lr)
26981 END_USE (0);
26983 else if (!WORLD_SAVE_P (info) && (strategy & SAVE_MULTIPLE))
26985 rtvec p;
26986 int i;
26987 p = rtvec_alloc (32 - info->first_gp_reg_save);
26988 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
26989 RTVEC_ELT (p, i)
26990 = gen_frame_store (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
26991 frame_reg_rtx,
26992 info->gp_save_offset + frame_off + reg_size * i);
26993 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
26994 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
26995 NULL_RTX, NULL_RTX);
26997 else if (!WORLD_SAVE_P (info))
26999 int offset = info->gp_save_offset + frame_off;
27000 for (int i = info->first_gp_reg_save; i < 32; i++)
27002 if (save_reg_p (i)
27003 && !cfun->machine->gpr_is_wrapped_separately[i])
27004 emit_frame_save (frame_reg_rtx, reg_mode, i, offset,
27005 sp_off - frame_off);
27007 offset += reg_size;
27011 if (crtl->calls_eh_return)
27013 unsigned int i;
27014 rtvec p;
27016 for (i = 0; ; ++i)
27018 unsigned int regno = EH_RETURN_DATA_REGNO (i);
27019 if (regno == INVALID_REGNUM)
27020 break;
27023 p = rtvec_alloc (i);
27025 for (i = 0; ; ++i)
27027 unsigned int regno = EH_RETURN_DATA_REGNO (i);
27028 if (regno == INVALID_REGNUM)
27029 break;
27031 rtx set
27032 = gen_frame_store (gen_rtx_REG (reg_mode, regno),
27033 sp_reg_rtx,
27034 info->ehrd_offset + sp_off + reg_size * (int) i);
27035 RTVEC_ELT (p, i) = set;
27036 RTX_FRAME_RELATED_P (set) = 1;
27039 insn = emit_insn (gen_blockage ());
27040 RTX_FRAME_RELATED_P (insn) = 1;
27041 add_reg_note (insn, REG_FRAME_RELATED_EXPR, gen_rtx_PARALLEL (VOIDmode, p));
27044 /* In AIX ABI we need to make sure r2 is really saved. */
27045 if (TARGET_AIX && crtl->calls_eh_return)
27047 rtx tmp_reg, tmp_reg_si, hi, lo, compare_result, toc_save_done, jump;
27048 rtx join_insn, note;
27049 rtx_insn *save_insn;
27050 long toc_restore_insn;
27052 tmp_reg = gen_rtx_REG (Pmode, 11);
27053 tmp_reg_si = gen_rtx_REG (SImode, 11);
27054 if (using_static_chain_p)
27056 START_USE (0);
27057 emit_move_insn (gen_rtx_REG (Pmode, 0), tmp_reg);
27059 else
27060 START_USE (11);
27061 emit_move_insn (tmp_reg, gen_rtx_REG (Pmode, LR_REGNO));
27062 /* Peek at instruction to which this function returns. If it's
27063 restoring r2, then we know we've already saved r2. We can't
27064 unconditionally save r2 because the value we have will already
27065 be updated if we arrived at this function via a plt call or
27066 toc adjusting stub. */
27067 emit_move_insn (tmp_reg_si, gen_rtx_MEM (SImode, tmp_reg));
27068 toc_restore_insn = ((TARGET_32BIT ? 0x80410000 : 0xE8410000)
27069 + RS6000_TOC_SAVE_SLOT);
27070 hi = gen_int_mode (toc_restore_insn & ~0xffff, SImode);
27071 emit_insn (gen_xorsi3 (tmp_reg_si, tmp_reg_si, hi));
27072 compare_result = gen_rtx_REG (CCUNSmode, CR0_REGNO);
27073 validate_condition_mode (EQ, CCUNSmode);
27074 lo = gen_int_mode (toc_restore_insn & 0xffff, SImode);
27075 emit_insn (gen_rtx_SET (compare_result,
27076 gen_rtx_COMPARE (CCUNSmode, tmp_reg_si, lo)));
27077 toc_save_done = gen_label_rtx ();
27078 jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
27079 gen_rtx_EQ (VOIDmode, compare_result,
27080 const0_rtx),
27081 gen_rtx_LABEL_REF (VOIDmode, toc_save_done),
27082 pc_rtx);
27083 jump = emit_jump_insn (gen_rtx_SET (pc_rtx, jump));
27084 JUMP_LABEL (jump) = toc_save_done;
27085 LABEL_NUSES (toc_save_done) += 1;
27087 save_insn = emit_frame_save (frame_reg_rtx, reg_mode,
27088 TOC_REGNUM, frame_off + RS6000_TOC_SAVE_SLOT,
27089 sp_off - frame_off);
27091 emit_label (toc_save_done);
27093 /* ??? If we leave SAVE_INSN as marked as saving R2, then we'll
27094 have a CFG that has different saves along different paths.
27095 Move the note to a dummy blockage insn, which describes that
27096 R2 is unconditionally saved after the label. */
27097 /* ??? An alternate representation might be a special insn pattern
27098 containing both the branch and the store. That might let the
27099 code that minimizes the number of DW_CFA_advance opcodes better
27100 freedom in placing the annotations. */
27101 note = find_reg_note (save_insn, REG_FRAME_RELATED_EXPR, NULL);
27102 if (note)
27103 remove_note (save_insn, note);
27104 else
27105 note = alloc_reg_note (REG_FRAME_RELATED_EXPR,
27106 copy_rtx (PATTERN (save_insn)), NULL_RTX);
27107 RTX_FRAME_RELATED_P (save_insn) = 0;
27109 join_insn = emit_insn (gen_blockage ());
27110 REG_NOTES (join_insn) = note;
27111 RTX_FRAME_RELATED_P (join_insn) = 1;
27113 if (using_static_chain_p)
27115 emit_move_insn (tmp_reg, gen_rtx_REG (Pmode, 0));
27116 END_USE (0);
27118 else
27119 END_USE (11);
27122 /* Save CR if we use any that must be preserved. */
27123 if (!WORLD_SAVE_P (info) && info->cr_save_p)
27125 rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
27126 GEN_INT (info->cr_save_offset + frame_off));
27127 rtx mem = gen_frame_mem (SImode, addr);
27129 /* If we didn't copy cr before, do so now using r0. */
27130 if (cr_save_rtx == NULL_RTX)
27132 START_USE (0);
27133 cr_save_rtx = gen_rtx_REG (SImode, 0);
27134 rs6000_emit_prologue_move_from_cr (cr_save_rtx);
27137 /* Saving CR requires a two-instruction sequence: one instruction
27138 to move the CR to a general-purpose register, and a second
27139 instruction that stores the GPR to memory.
27141 We do not emit any DWARF CFI records for the first of these,
27142 because we cannot properly represent the fact that CR is saved in
27143 a register. One reason is that we cannot express that multiple
27144 CR fields are saved; another reason is that on 64-bit, the size
27145 of the CR register in DWARF (4 bytes) differs from the size of
27146 a general-purpose register.
27148 This means if any intervening instruction were to clobber one of
27149 the call-saved CR fields, we'd have incorrect CFI. To prevent
27150 this from happening, we mark the store to memory as a use of
27151 those CR fields, which prevents any such instruction from being
27152 scheduled in between the two instructions. */
27153 rtx crsave_v[9];
27154 int n_crsave = 0;
27155 int i;
27157 crsave_v[n_crsave++] = gen_rtx_SET (mem, cr_save_rtx);
27158 for (i = 0; i < 8; i++)
27159 if (save_reg_p (CR0_REGNO + i))
27160 crsave_v[n_crsave++]
27161 = gen_rtx_USE (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO + i));
27163 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode,
27164 gen_rtvec_v (n_crsave, crsave_v)));
27165 END_USE (REGNO (cr_save_rtx));
27167 /* Now, there's no way that dwarf2out_frame_debug_expr is going to
27168 understand '(unspec:SI [(reg:CC 68) ...] UNSPEC_MOVESI_FROM_CR)',
27169 so we need to construct a frame expression manually. */
27170 RTX_FRAME_RELATED_P (insn) = 1;
27172 /* Update address to be stack-pointer relative, like
27173 rs6000_frame_related would do. */
27174 addr = gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode, STACK_POINTER_REGNUM),
27175 GEN_INT (info->cr_save_offset + sp_off));
27176 mem = gen_frame_mem (SImode, addr);
27178 if (DEFAULT_ABI == ABI_ELFv2)
27180 /* In the ELFv2 ABI we generate separate CFI records for each
27181 CR field that was actually saved. They all point to the
27182 same 32-bit stack slot. */
27183 rtx crframe[8];
27184 int n_crframe = 0;
27186 for (i = 0; i < 8; i++)
27187 if (save_reg_p (CR0_REGNO + i))
27189 crframe[n_crframe]
27190 = gen_rtx_SET (mem, gen_rtx_REG (SImode, CR0_REGNO + i));
27192 RTX_FRAME_RELATED_P (crframe[n_crframe]) = 1;
27193 n_crframe++;
27196 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
27197 gen_rtx_PARALLEL (VOIDmode,
27198 gen_rtvec_v (n_crframe, crframe)));
27200 else
27202 /* In other ABIs, by convention, we use a single CR regnum to
27203 represent the fact that all call-saved CR fields are saved.
27204 We use CR2_REGNO to be compatible with gcc-2.95 on Linux. */
27205 rtx set = gen_rtx_SET (mem, gen_rtx_REG (SImode, CR2_REGNO));
27206 add_reg_note (insn, REG_FRAME_RELATED_EXPR, set);
27210 /* In the ELFv2 ABI we need to save all call-saved CR fields into
27211 *separate* slots if the routine calls __builtin_eh_return, so
27212 that they can be independently restored by the unwinder. */
27213 if (DEFAULT_ABI == ABI_ELFv2 && crtl->calls_eh_return)
27215 int i, cr_off = info->ehcr_offset;
27216 rtx crsave;
27218 /* ??? We might get better performance by using multiple mfocrf
27219 instructions. */
27220 crsave = gen_rtx_REG (SImode, 0);
27221 emit_insn (gen_prologue_movesi_from_cr (crsave));
27223 for (i = 0; i < 8; i++)
27224 if (!call_used_regs[CR0_REGNO + i])
27226 rtvec p = rtvec_alloc (2);
27227 RTVEC_ELT (p, 0)
27228 = gen_frame_store (crsave, frame_reg_rtx, cr_off + frame_off);
27229 RTVEC_ELT (p, 1)
27230 = gen_rtx_USE (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO + i));
27232 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
27234 RTX_FRAME_RELATED_P (insn) = 1;
27235 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
27236 gen_frame_store (gen_rtx_REG (SImode, CR0_REGNO + i),
27237 sp_reg_rtx, cr_off + sp_off));
27239 cr_off += reg_size;
27243 /* If we are emitting stack probes, but allocate no stack, then
27244 just note that in the dump file. */
27245 if (flag_stack_clash_protection
27246 && dump_file
27247 && !info->push_p)
27248 dump_stack_clash_frame_info (NO_PROBE_NO_FRAME, false);
27250 /* Update stack and set back pointer unless this is V.4,
27251 for which it was done previously. */
27252 if (!WORLD_SAVE_P (info) && info->push_p
27253 && !(DEFAULT_ABI == ABI_V4 || crtl->calls_eh_return))
27255 rtx ptr_reg = NULL;
27256 int ptr_off = 0;
27258 /* If saving altivec regs we need to be able to address all save
27259 locations using a 16-bit offset. */
27260 if ((strategy & SAVE_INLINE_VRS) == 0
27261 || (info->altivec_size != 0
27262 && (info->altivec_save_offset + info->altivec_size - 16
27263 + info->total_size - frame_off) > 32767)
27264 || (info->vrsave_size != 0
27265 && (info->vrsave_save_offset
27266 + info->total_size - frame_off) > 32767))
27268 int sel = SAVRES_SAVE | SAVRES_VR;
27269 unsigned ptr_regno = ptr_regno_for_savres (sel);
27271 if (using_static_chain_p
27272 && ptr_regno == STATIC_CHAIN_REGNUM)
27273 ptr_regno = 12;
27274 if (REGNO (frame_reg_rtx) != ptr_regno)
27275 START_USE (ptr_regno);
27276 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
27277 frame_reg_rtx = ptr_reg;
27278 ptr_off = info->altivec_save_offset + info->altivec_size;
27279 frame_off = -ptr_off;
27281 else if (REGNO (frame_reg_rtx) == 1)
27282 frame_off = info->total_size;
27283 sp_adjust = rs6000_emit_allocate_stack (info->total_size,
27284 ptr_reg, ptr_off);
27285 if (REGNO (frame_reg_rtx) == 12)
27286 sp_adjust = 0;
27287 sp_off = info->total_size;
27288 if (frame_reg_rtx != sp_reg_rtx)
27289 rs6000_emit_stack_tie (frame_reg_rtx, false);
27292 /* Set frame pointer, if needed. */
27293 if (frame_pointer_needed)
27295 insn = emit_move_insn (gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
27296 sp_reg_rtx);
27297 RTX_FRAME_RELATED_P (insn) = 1;
27300 /* Save AltiVec registers if needed. Save here because the red zone does
27301 not always include AltiVec registers. */
27302 if (!WORLD_SAVE_P (info)
27303 && info->altivec_size != 0 && (strategy & SAVE_INLINE_VRS) == 0)
27305 int end_save = info->altivec_save_offset + info->altivec_size;
27306 int ptr_off;
27307 /* Oddly, the vector save/restore functions point r0 at the end
27308 of the save area, then use r11 or r12 to load offsets for
27309 [reg+reg] addressing. */
27310 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
27311 int scratch_regno = ptr_regno_for_savres (SAVRES_SAVE | SAVRES_VR);
27312 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
27314 gcc_checking_assert (scratch_regno == 11 || scratch_regno == 12);
27315 NOT_INUSE (0);
27316 if (scratch_regno == 12)
27317 sp_adjust = 0;
27318 if (end_save + frame_off != 0)
27320 rtx offset = GEN_INT (end_save + frame_off);
27322 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
27324 else
27325 emit_move_insn (ptr_reg, frame_reg_rtx);
27327 ptr_off = -end_save;
27328 insn = rs6000_emit_savres_rtx (info, scratch_reg,
27329 info->altivec_save_offset + ptr_off,
27330 0, V4SImode, SAVRES_SAVE | SAVRES_VR);
27331 rs6000_frame_related (insn, scratch_reg, sp_off - ptr_off,
27332 NULL_RTX, NULL_RTX);
27333 if (REGNO (frame_reg_rtx) == REGNO (scratch_reg))
27335 /* The oddity mentioned above clobbered our frame reg. */
27336 emit_move_insn (frame_reg_rtx, ptr_reg);
27337 frame_off = ptr_off;
27340 else if (!WORLD_SAVE_P (info)
27341 && info->altivec_size != 0)
27343 int i;
27345 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
27346 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
27348 rtx areg, savereg, mem;
27349 HOST_WIDE_INT offset;
27351 offset = (info->altivec_save_offset + frame_off
27352 + 16 * (i - info->first_altivec_reg_save));
27354 savereg = gen_rtx_REG (V4SImode, i);
27356 if (TARGET_P9_VECTOR && quad_address_offset_p (offset))
27358 mem = gen_frame_mem (V4SImode,
27359 gen_rtx_PLUS (Pmode, frame_reg_rtx,
27360 GEN_INT (offset)));
27361 insn = emit_insn (gen_rtx_SET (mem, savereg));
27362 areg = NULL_RTX;
27364 else
27366 NOT_INUSE (0);
27367 areg = gen_rtx_REG (Pmode, 0);
27368 emit_move_insn (areg, GEN_INT (offset));
27370 /* AltiVec addressing mode is [reg+reg]. */
27371 mem = gen_frame_mem (V4SImode,
27372 gen_rtx_PLUS (Pmode, frame_reg_rtx, areg));
27374 /* Rather than emitting a generic move, force use of the stvx
27375 instruction, which we always want on ISA 2.07 (power8) systems.
27376 In particular we don't want xxpermdi/stxvd2x for little
27377 endian. */
27378 insn = emit_insn (gen_altivec_stvx_v4si_internal (mem, savereg));
27381 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
27382 areg, GEN_INT (offset));
27386 /* VRSAVE is a bit vector representing which AltiVec registers
27387 are used. The OS uses this to determine which vector
27388 registers to save on a context switch. We need to save
27389 VRSAVE on the stack frame, add whatever AltiVec registers we
27390 used in this function, and do the corresponding magic in the
27391 epilogue. */
27393 if (!WORLD_SAVE_P (info) && info->vrsave_size != 0)
27395 /* Get VRSAVE into a GPR. Note that ABI_V4 and ABI_DARWIN might
27396 be using r12 as frame_reg_rtx and r11 as the static chain
27397 pointer for nested functions. */
27398 int save_regno = 12;
27399 if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
27400 && !using_static_chain_p)
27401 save_regno = 11;
27402 else if (using_split_stack || REGNO (frame_reg_rtx) == 12)
27404 save_regno = 11;
27405 if (using_static_chain_p)
27406 save_regno = 0;
27408 NOT_INUSE (save_regno);
27410 emit_vrsave_prologue (info, save_regno, frame_off, frame_reg_rtx);
27413 /* If we are using RS6000_PIC_OFFSET_TABLE_REGNUM, we need to set it up. */
27414 if (!TARGET_SINGLE_PIC_BASE
27415 && ((TARGET_TOC && TARGET_MINIMAL_TOC
27416 && !constant_pool_empty_p ())
27417 || (DEFAULT_ABI == ABI_V4
27418 && (flag_pic == 1 || (flag_pic && TARGET_SECURE_PLT))
27419 && df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))))
27421 /* If emit_load_toc_table will use the link register, we need to save
27422 it. We use R12 for this purpose because emit_load_toc_table
27423 can use register 0. This allows us to use a plain 'blr' to return
27424 from the procedure more often. */
27425 int save_LR_around_toc_setup = (TARGET_ELF
27426 && DEFAULT_ABI == ABI_V4
27427 && flag_pic
27428 && ! info->lr_save_p
27429 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds) > 0);
27430 if (save_LR_around_toc_setup)
27432 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
27433 rtx tmp = gen_rtx_REG (Pmode, 12);
27435 sp_adjust = 0;
27436 insn = emit_move_insn (tmp, lr);
27437 RTX_FRAME_RELATED_P (insn) = 1;
27439 rs6000_emit_load_toc_table (TRUE);
27441 insn = emit_move_insn (lr, tmp);
27442 add_reg_note (insn, REG_CFA_RESTORE, lr);
27443 RTX_FRAME_RELATED_P (insn) = 1;
27445 else
27446 rs6000_emit_load_toc_table (TRUE);
27449 #if TARGET_MACHO
27450 if (!TARGET_SINGLE_PIC_BASE
27451 && DEFAULT_ABI == ABI_DARWIN
27452 && flag_pic && crtl->uses_pic_offset_table)
27454 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
27455 rtx src = gen_rtx_SYMBOL_REF (Pmode, MACHOPIC_FUNCTION_BASE_NAME);
27457 /* Save and restore LR locally around this call (in R0). */
27458 if (!info->lr_save_p)
27459 emit_move_insn (gen_rtx_REG (Pmode, 0), lr);
27461 emit_insn (gen_load_macho_picbase (src));
27463 emit_move_insn (gen_rtx_REG (Pmode,
27464 RS6000_PIC_OFFSET_TABLE_REGNUM),
27465 lr);
27467 if (!info->lr_save_p)
27468 emit_move_insn (lr, gen_rtx_REG (Pmode, 0));
27470 #endif
27472 /* If we need to, save the TOC register after doing the stack setup.
27473 Do not emit eh frame info for this save. The unwinder wants info,
27474 conceptually attached to instructions in this function, about
27475 register values in the caller of this function. This R2 may have
27476 already been changed from the value in the caller.
27477 We don't attempt to write accurate DWARF EH frame info for R2
27478 because code emitted by gcc for a (non-pointer) function call
27479 doesn't save and restore R2. Instead, R2 is managed out-of-line
27480 by a linker generated plt call stub when the function resides in
27481 a shared library. This behavior is costly to describe in DWARF,
27482 both in terms of the size of DWARF info and the time taken in the
27483 unwinder to interpret it. R2 changes, apart from the
27484 calls_eh_return case earlier in this function, are handled by
27485 linux-unwind.h frob_update_context. */
27486 if (rs6000_save_toc_in_prologue_p ()
27487 && !cfun->machine->toc_is_wrapped_separately)
27489 rtx reg = gen_rtx_REG (reg_mode, TOC_REGNUM);
27490 emit_insn (gen_frame_store (reg, sp_reg_rtx, RS6000_TOC_SAVE_SLOT));
27493 /* Set up the arg pointer (r12) for -fsplit-stack code. */
27494 if (using_split_stack && split_stack_arg_pointer_used_p ())
27495 emit_split_stack_prologue (info, sp_adjust, frame_off, frame_reg_rtx);
27498 /* Output .extern statements for the save/restore routines we use. */
27500 static void
27501 rs6000_output_savres_externs (FILE *file)
27503 rs6000_stack_t *info = rs6000_stack_info ();
27505 if (TARGET_DEBUG_STACK)
27506 debug_stack_info (info);
27508 /* Write .extern for any function we will call to save and restore
27509 fp values. */
27510 if (info->first_fp_reg_save < 64
27511 && !TARGET_MACHO
27512 && !TARGET_ELF)
27514 char *name;
27515 int regno = info->first_fp_reg_save - 32;
27517 if ((info->savres_strategy & SAVE_INLINE_FPRS) == 0)
27519 bool lr = (info->savres_strategy & SAVE_NOINLINE_FPRS_SAVES_LR) != 0;
27520 int sel = SAVRES_SAVE | SAVRES_FPR | (lr ? SAVRES_LR : 0);
27521 name = rs6000_savres_routine_name (regno, sel);
27522 fprintf (file, "\t.extern %s\n", name);
27524 if ((info->savres_strategy & REST_INLINE_FPRS) == 0)
27526 bool lr = (info->savres_strategy
27527 & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
27528 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
27529 name = rs6000_savres_routine_name (regno, sel);
27530 fprintf (file, "\t.extern %s\n", name);
27535 /* Write function prologue. */
27537 static void
27538 rs6000_output_function_prologue (FILE *file)
27540 if (!cfun->is_thunk)
27541 rs6000_output_savres_externs (file);
27543 /* ELFv2 ABI r2 setup code and local entry point. This must follow
27544 immediately after the global entry point label. */
27545 if (rs6000_global_entry_point_needed_p ())
27547 const char *name = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
27549 (*targetm.asm_out.internal_label) (file, "LCF", rs6000_pic_labelno);
27551 if (TARGET_CMODEL != CMODEL_LARGE)
27553 /* In the small and medium code models, we assume the TOC is less
27554 2 GB away from the text section, so it can be computed via the
27555 following two-instruction sequence. */
27556 char buf[256];
27558 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
27559 fprintf (file, "0:\taddis 2,12,.TOC.-");
27560 assemble_name (file, buf);
27561 fprintf (file, "@ha\n");
27562 fprintf (file, "\taddi 2,2,.TOC.-");
27563 assemble_name (file, buf);
27564 fprintf (file, "@l\n");
27566 else
27568 /* In the large code model, we allow arbitrary offsets between the
27569 TOC and the text section, so we have to load the offset from
27570 memory. The data field is emitted directly before the global
27571 entry point in rs6000_elf_declare_function_name. */
27572 char buf[256];
27574 #ifdef HAVE_AS_ENTRY_MARKERS
27575 /* If supported by the linker, emit a marker relocation. If the
27576 total code size of the final executable or shared library
27577 happens to fit into 2 GB after all, the linker will replace
27578 this code sequence with the sequence for the small or medium
27579 code model. */
27580 fprintf (file, "\t.reloc .,R_PPC64_ENTRY\n");
27581 #endif
27582 fprintf (file, "\tld 2,");
27583 ASM_GENERATE_INTERNAL_LABEL (buf, "LCL", rs6000_pic_labelno);
27584 assemble_name (file, buf);
27585 fprintf (file, "-");
27586 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
27587 assemble_name (file, buf);
27588 fprintf (file, "(12)\n");
27589 fprintf (file, "\tadd 2,2,12\n");
27592 fputs ("\t.localentry\t", file);
27593 assemble_name (file, name);
27594 fputs (",.-", file);
27595 assemble_name (file, name);
27596 fputs ("\n", file);
27599 /* Output -mprofile-kernel code. This needs to be done here instead of
27600 in output_function_profile since it must go after the ELFv2 ABI
27601 local entry point. */
27602 if (TARGET_PROFILE_KERNEL && crtl->profile)
27604 gcc_assert (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2);
27605 gcc_assert (!TARGET_32BIT);
27607 asm_fprintf (file, "\tmflr %s\n", reg_names[0]);
27609 /* In the ELFv2 ABI we have no compiler stack word. It must be
27610 the resposibility of _mcount to preserve the static chain
27611 register if required. */
27612 if (DEFAULT_ABI != ABI_ELFv2
27613 && cfun->static_chain_decl != NULL)
27615 asm_fprintf (file, "\tstd %s,24(%s)\n",
27616 reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
27617 fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
27618 asm_fprintf (file, "\tld %s,24(%s)\n",
27619 reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
27621 else
27622 fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
27625 rs6000_pic_labelno++;
27628 /* -mprofile-kernel code calls mcount before the function prolog,
27629 so a profiled leaf function should stay a leaf function. */
27630 static bool
27631 rs6000_keep_leaf_when_profiled ()
27633 return TARGET_PROFILE_KERNEL;
27636 /* Non-zero if vmx regs are restored before the frame pop, zero if
27637 we restore after the pop when possible. */
27638 #define ALWAYS_RESTORE_ALTIVEC_BEFORE_POP 0
27640 /* Restoring cr is a two step process: loading a reg from the frame
27641 save, then moving the reg to cr. For ABI_V4 we must let the
27642 unwinder know that the stack location is no longer valid at or
27643 before the stack deallocation, but we can't emit a cfa_restore for
27644 cr at the stack deallocation like we do for other registers.
27645 The trouble is that it is possible for the move to cr to be
27646 scheduled after the stack deallocation. So say exactly where cr
27647 is located on each of the two insns. */
27649 static rtx
27650 load_cr_save (int regno, rtx frame_reg_rtx, int offset, bool exit_func)
27652 rtx mem = gen_frame_mem_offset (SImode, frame_reg_rtx, offset);
27653 rtx reg = gen_rtx_REG (SImode, regno);
27654 rtx_insn *insn = emit_move_insn (reg, mem);
27656 if (!exit_func && DEFAULT_ABI == ABI_V4)
27658 rtx cr = gen_rtx_REG (SImode, CR2_REGNO);
27659 rtx set = gen_rtx_SET (reg, cr);
27661 add_reg_note (insn, REG_CFA_REGISTER, set);
27662 RTX_FRAME_RELATED_P (insn) = 1;
27664 return reg;
27667 /* Reload CR from REG. */
27669 static void
27670 restore_saved_cr (rtx reg, bool using_mfcr_multiple, bool exit_func)
27672 int count = 0;
27673 int i;
27675 if (using_mfcr_multiple)
27677 for (i = 0; i < 8; i++)
27678 if (save_reg_p (CR0_REGNO + i))
27679 count++;
27680 gcc_assert (count);
27683 if (using_mfcr_multiple && count > 1)
27685 rtx_insn *insn;
27686 rtvec p;
27687 int ndx;
27689 p = rtvec_alloc (count);
27691 ndx = 0;
27692 for (i = 0; i < 8; i++)
27693 if (save_reg_p (CR0_REGNO + i))
27695 rtvec r = rtvec_alloc (2);
27696 RTVEC_ELT (r, 0) = reg;
27697 RTVEC_ELT (r, 1) = GEN_INT (1 << (7-i));
27698 RTVEC_ELT (p, ndx) =
27699 gen_rtx_SET (gen_rtx_REG (CCmode, CR0_REGNO + i),
27700 gen_rtx_UNSPEC (CCmode, r, UNSPEC_MOVESI_TO_CR));
27701 ndx++;
27703 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
27704 gcc_assert (ndx == count);
27706 /* For the ELFv2 ABI we generate a CFA_RESTORE for each
27707 CR field separately. */
27708 if (!exit_func && DEFAULT_ABI == ABI_ELFv2 && flag_shrink_wrap)
27710 for (i = 0; i < 8; i++)
27711 if (save_reg_p (CR0_REGNO + i))
27712 add_reg_note (insn, REG_CFA_RESTORE,
27713 gen_rtx_REG (SImode, CR0_REGNO + i));
27715 RTX_FRAME_RELATED_P (insn) = 1;
27718 else
27719 for (i = 0; i < 8; i++)
27720 if (save_reg_p (CR0_REGNO + i))
27722 rtx insn = emit_insn (gen_movsi_to_cr_one
27723 (gen_rtx_REG (CCmode, CR0_REGNO + i), reg));
27725 /* For the ELFv2 ABI we generate a CFA_RESTORE for each
27726 CR field separately, attached to the insn that in fact
27727 restores this particular CR field. */
27728 if (!exit_func && DEFAULT_ABI == ABI_ELFv2 && flag_shrink_wrap)
27730 add_reg_note (insn, REG_CFA_RESTORE,
27731 gen_rtx_REG (SImode, CR0_REGNO + i));
27733 RTX_FRAME_RELATED_P (insn) = 1;
27737 /* For other ABIs, we just generate a single CFA_RESTORE for CR2. */
27738 if (!exit_func && DEFAULT_ABI != ABI_ELFv2
27739 && (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap))
27741 rtx_insn *insn = get_last_insn ();
27742 rtx cr = gen_rtx_REG (SImode, CR2_REGNO);
27744 add_reg_note (insn, REG_CFA_RESTORE, cr);
27745 RTX_FRAME_RELATED_P (insn) = 1;
27749 /* Like cr, the move to lr instruction can be scheduled after the
27750 stack deallocation, but unlike cr, its stack frame save is still
27751 valid. So we only need to emit the cfa_restore on the correct
27752 instruction. */
27754 static void
27755 load_lr_save (int regno, rtx frame_reg_rtx, int offset)
27757 rtx mem = gen_frame_mem_offset (Pmode, frame_reg_rtx, offset);
27758 rtx reg = gen_rtx_REG (Pmode, regno);
27760 emit_move_insn (reg, mem);
27763 static void
27764 restore_saved_lr (int regno, bool exit_func)
27766 rtx reg = gen_rtx_REG (Pmode, regno);
27767 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
27768 rtx_insn *insn = emit_move_insn (lr, reg);
27770 if (!exit_func && flag_shrink_wrap)
27772 add_reg_note (insn, REG_CFA_RESTORE, lr);
27773 RTX_FRAME_RELATED_P (insn) = 1;
27777 static rtx
27778 add_crlr_cfa_restore (const rs6000_stack_t *info, rtx cfa_restores)
27780 if (DEFAULT_ABI == ABI_ELFv2)
27782 int i;
27783 for (i = 0; i < 8; i++)
27784 if (save_reg_p (CR0_REGNO + i))
27786 rtx cr = gen_rtx_REG (SImode, CR0_REGNO + i);
27787 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, cr,
27788 cfa_restores);
27791 else if (info->cr_save_p)
27792 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
27793 gen_rtx_REG (SImode, CR2_REGNO),
27794 cfa_restores);
27796 if (info->lr_save_p)
27797 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
27798 gen_rtx_REG (Pmode, LR_REGNO),
27799 cfa_restores);
27800 return cfa_restores;
27803 /* Return true if OFFSET from stack pointer can be clobbered by signals.
27804 V.4 doesn't have any stack cushion, AIX ABIs have 220 or 288 bytes
27805 below stack pointer not cloberred by signals. */
27807 static inline bool
27808 offset_below_red_zone_p (HOST_WIDE_INT offset)
27810 return offset < (DEFAULT_ABI == ABI_V4
27812 : TARGET_32BIT ? -220 : -288);
27815 /* Append CFA_RESTORES to any existing REG_NOTES on the last insn. */
27817 static void
27818 emit_cfa_restores (rtx cfa_restores)
27820 rtx_insn *insn = get_last_insn ();
27821 rtx *loc = &REG_NOTES (insn);
27823 while (*loc)
27824 loc = &XEXP (*loc, 1);
27825 *loc = cfa_restores;
27826 RTX_FRAME_RELATED_P (insn) = 1;
27829 /* Emit function epilogue as insns. */
27831 void
27832 rs6000_emit_epilogue (enum epilogue_type epilogue_type)
27834 HOST_WIDE_INT frame_off = 0;
27835 rtx sp_reg_rtx = gen_rtx_REG (Pmode, 1);
27836 rtx frame_reg_rtx = sp_reg_rtx;
27837 rtx cfa_restores = NULL_RTX;
27838 rtx insn;
27839 rtx cr_save_reg = NULL_RTX;
27840 machine_mode reg_mode = Pmode;
27841 int reg_size = TARGET_32BIT ? 4 : 8;
27842 machine_mode fp_reg_mode = TARGET_HARD_FLOAT ? DFmode : SFmode;
27843 int fp_reg_size = 8;
27844 int i;
27845 unsigned ptr_regno;
27847 rs6000_stack_t *info = rs6000_stack_info ();
27849 if (epilogue_type == EPILOGUE_TYPE_NORMAL && crtl->calls_eh_return)
27850 epilogue_type = EPILOGUE_TYPE_EH_RETURN;
27852 int strategy = info->savres_strategy;
27853 bool using_load_multiple = !!(strategy & REST_MULTIPLE);
27854 bool restoring_GPRs_inline = !!(strategy & REST_INLINE_GPRS);
27855 bool restoring_FPRs_inline = !!(strategy & REST_INLINE_FPRS);
27856 if (epilogue_type == EPILOGUE_TYPE_SIBCALL)
27858 restoring_GPRs_inline = true;
27859 restoring_FPRs_inline = true;
27862 bool using_mtcr_multiple = (rs6000_tune == PROCESSOR_PPC601
27863 || rs6000_tune == PROCESSOR_PPC603
27864 || rs6000_tune == PROCESSOR_PPC750
27865 || optimize_size);
27867 /* Restore via the backchain when we have a large frame, since this
27868 is more efficient than an addis, addi pair. The second condition
27869 here will not trigger at the moment; We don't actually need a
27870 frame pointer for alloca, but the generic parts of the compiler
27871 give us one anyway. */
27872 bool use_backchain_to_restore_sp
27873 = (info->total_size + (info->lr_save_p ? info->lr_save_offset : 0) > 32767
27874 || (cfun->calls_alloca && !frame_pointer_needed));
27876 bool restore_lr = (info->lr_save_p
27877 && (restoring_FPRs_inline
27878 || (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR))
27879 && (restoring_GPRs_inline
27880 || info->first_fp_reg_save < 64)
27881 && !cfun->machine->lr_is_wrapped_separately);
27884 if (WORLD_SAVE_P (info))
27886 gcc_assert (epilogue_type != EPILOGUE_TYPE_SIBCALL);
27888 /* eh_rest_world_r10 will return to the location saved in the LR
27889 stack slot (which is not likely to be our caller.)
27890 Input: R10 -- stack adjustment. Clobbers R0, R11, R12, R7, R8.
27891 rest_world is similar, except any R10 parameter is ignored.
27892 The exception-handling stuff that was here in 2.95 is no
27893 longer necessary. */
27895 rtvec p;
27896 p = rtvec_alloc (9
27897 + 32 - info->first_gp_reg_save
27898 + LAST_ALTIVEC_REGNO + 1 - info->first_altivec_reg_save
27899 + 63 + 1 - info->first_fp_reg_save);
27901 const char *rname;
27902 switch (epilogue_type)
27904 case EPILOGUE_TYPE_NORMAL:
27905 rname = ggc_strdup ("*rest_world");
27906 break;
27908 case EPILOGUE_TYPE_EH_RETURN:
27909 rname = ggc_strdup ("*eh_rest_world_r10");
27910 break;
27912 default:
27913 gcc_unreachable ();
27916 int j = 0;
27917 RTVEC_ELT (p, j++) = ret_rtx;
27918 RTVEC_ELT (p, j++)
27919 = gen_rtx_USE (VOIDmode, gen_rtx_SYMBOL_REF (Pmode, rname));
27920 /* The instruction pattern requires a clobber here;
27921 it is shared with the restVEC helper. */
27922 RTVEC_ELT (p, j++) = gen_hard_reg_clobber (Pmode, 11);
27925 /* CR register traditionally saved as CR2. */
27926 rtx reg = gen_rtx_REG (SImode, CR2_REGNO);
27927 RTVEC_ELT (p, j++)
27928 = gen_frame_load (reg, frame_reg_rtx, info->cr_save_offset);
27929 if (flag_shrink_wrap)
27931 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
27932 gen_rtx_REG (Pmode, LR_REGNO),
27933 cfa_restores);
27934 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
27938 int i;
27939 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
27941 rtx reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
27942 RTVEC_ELT (p, j++)
27943 = gen_frame_load (reg,
27944 frame_reg_rtx, info->gp_save_offset + reg_size * i);
27945 if (flag_shrink_wrap
27946 && save_reg_p (info->first_gp_reg_save + i))
27947 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
27949 for (i = 0; info->first_altivec_reg_save + i <= LAST_ALTIVEC_REGNO; i++)
27951 rtx reg = gen_rtx_REG (V4SImode, info->first_altivec_reg_save + i);
27952 RTVEC_ELT (p, j++)
27953 = gen_frame_load (reg,
27954 frame_reg_rtx, info->altivec_save_offset + 16 * i);
27955 if (flag_shrink_wrap
27956 && save_reg_p (info->first_altivec_reg_save + i))
27957 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
27959 for (i = 0; info->first_fp_reg_save + i <= 63; i++)
27961 rtx reg = gen_rtx_REG (TARGET_HARD_FLOAT ? DFmode : SFmode,
27962 info->first_fp_reg_save + i);
27963 RTVEC_ELT (p, j++)
27964 = gen_frame_load (reg, frame_reg_rtx, info->fp_save_offset + 8 * i);
27965 if (flag_shrink_wrap
27966 && save_reg_p (info->first_fp_reg_save + i))
27967 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
27969 RTVEC_ELT (p, j++) = gen_hard_reg_clobber (Pmode, 0);
27970 RTVEC_ELT (p, j++) = gen_hard_reg_clobber (SImode, 12);
27971 RTVEC_ELT (p, j++) = gen_hard_reg_clobber (SImode, 7);
27972 RTVEC_ELT (p, j++) = gen_hard_reg_clobber (SImode, 8);
27973 RTVEC_ELT (p, j++)
27974 = gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, 10));
27975 insn = emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
27977 if (flag_shrink_wrap)
27979 REG_NOTES (insn) = cfa_restores;
27980 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
27981 RTX_FRAME_RELATED_P (insn) = 1;
27983 return;
27986 /* frame_reg_rtx + frame_off points to the top of this stack frame. */
27987 if (info->push_p)
27988 frame_off = info->total_size;
27990 /* Restore AltiVec registers if we must do so before adjusting the
27991 stack. */
27992 if (info->altivec_size != 0
27993 && (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
27994 || (DEFAULT_ABI != ABI_V4
27995 && offset_below_red_zone_p (info->altivec_save_offset))))
27997 int i;
27998 int scratch_regno = ptr_regno_for_savres (SAVRES_VR);
28000 gcc_checking_assert (scratch_regno == 11 || scratch_regno == 12);
28001 if (use_backchain_to_restore_sp)
28003 int frame_regno = 11;
28005 if ((strategy & REST_INLINE_VRS) == 0)
28007 /* Of r11 and r12, select the one not clobbered by an
28008 out-of-line restore function for the frame register. */
28009 frame_regno = 11 + 12 - scratch_regno;
28011 frame_reg_rtx = gen_rtx_REG (Pmode, frame_regno);
28012 emit_move_insn (frame_reg_rtx,
28013 gen_rtx_MEM (Pmode, sp_reg_rtx));
28014 frame_off = 0;
28016 else if (frame_pointer_needed)
28017 frame_reg_rtx = hard_frame_pointer_rtx;
28019 if ((strategy & REST_INLINE_VRS) == 0)
28021 int end_save = info->altivec_save_offset + info->altivec_size;
28022 int ptr_off;
28023 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
28024 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
28026 if (end_save + frame_off != 0)
28028 rtx offset = GEN_INT (end_save + frame_off);
28030 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
28032 else
28033 emit_move_insn (ptr_reg, frame_reg_rtx);
28035 ptr_off = -end_save;
28036 insn = rs6000_emit_savres_rtx (info, scratch_reg,
28037 info->altivec_save_offset + ptr_off,
28038 0, V4SImode, SAVRES_VR);
28040 else
28042 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
28043 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
28045 rtx addr, areg, mem, insn;
28046 rtx reg = gen_rtx_REG (V4SImode, i);
28047 HOST_WIDE_INT offset
28048 = (info->altivec_save_offset + frame_off
28049 + 16 * (i - info->first_altivec_reg_save));
28051 if (TARGET_P9_VECTOR && quad_address_offset_p (offset))
28053 mem = gen_frame_mem (V4SImode,
28054 gen_rtx_PLUS (Pmode, frame_reg_rtx,
28055 GEN_INT (offset)));
28056 insn = gen_rtx_SET (reg, mem);
28058 else
28060 areg = gen_rtx_REG (Pmode, 0);
28061 emit_move_insn (areg, GEN_INT (offset));
28063 /* AltiVec addressing mode is [reg+reg]. */
28064 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
28065 mem = gen_frame_mem (V4SImode, addr);
28067 /* Rather than emitting a generic move, force use of the
28068 lvx instruction, which we always want. In particular we
28069 don't want lxvd2x/xxpermdi for little endian. */
28070 insn = gen_altivec_lvx_v4si_internal (reg, mem);
28073 (void) emit_insn (insn);
28077 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
28078 if (((strategy & REST_INLINE_VRS) == 0
28079 || (info->vrsave_mask & ALTIVEC_REG_BIT (i)) != 0)
28080 && (flag_shrink_wrap
28081 || (offset_below_red_zone_p
28082 (info->altivec_save_offset
28083 + 16 * (i - info->first_altivec_reg_save))))
28084 && save_reg_p (i))
28086 rtx reg = gen_rtx_REG (V4SImode, i);
28087 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
28091 /* Restore VRSAVE if we must do so before adjusting the stack. */
28092 if (info->vrsave_size != 0
28093 && (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
28094 || (DEFAULT_ABI != ABI_V4
28095 && offset_below_red_zone_p (info->vrsave_save_offset))))
28097 rtx reg;
28099 if (frame_reg_rtx == sp_reg_rtx)
28101 if (use_backchain_to_restore_sp)
28103 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
28104 emit_move_insn (frame_reg_rtx,
28105 gen_rtx_MEM (Pmode, sp_reg_rtx));
28106 frame_off = 0;
28108 else if (frame_pointer_needed)
28109 frame_reg_rtx = hard_frame_pointer_rtx;
28112 reg = gen_rtx_REG (SImode, 12);
28113 emit_insn (gen_frame_load (reg, frame_reg_rtx,
28114 info->vrsave_save_offset + frame_off));
28116 emit_insn (generate_set_vrsave (reg, info, 1));
28119 insn = NULL_RTX;
28120 /* If we have a large stack frame, restore the old stack pointer
28121 using the backchain. */
28122 if (use_backchain_to_restore_sp)
28124 if (frame_reg_rtx == sp_reg_rtx)
28126 /* Under V.4, don't reset the stack pointer until after we're done
28127 loading the saved registers. */
28128 if (DEFAULT_ABI == ABI_V4)
28129 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
28131 insn = emit_move_insn (frame_reg_rtx,
28132 gen_rtx_MEM (Pmode, sp_reg_rtx));
28133 frame_off = 0;
28135 else if (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
28136 && DEFAULT_ABI == ABI_V4)
28137 /* frame_reg_rtx has been set up by the altivec restore. */
28139 else
28141 insn = emit_move_insn (sp_reg_rtx, frame_reg_rtx);
28142 frame_reg_rtx = sp_reg_rtx;
28145 /* If we have a frame pointer, we can restore the old stack pointer
28146 from it. */
28147 else if (frame_pointer_needed)
28149 frame_reg_rtx = sp_reg_rtx;
28150 if (DEFAULT_ABI == ABI_V4)
28151 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
28152 /* Prevent reordering memory accesses against stack pointer restore. */
28153 else if (cfun->calls_alloca
28154 || offset_below_red_zone_p (-info->total_size))
28155 rs6000_emit_stack_tie (frame_reg_rtx, true);
28157 insn = emit_insn (gen_add3_insn (frame_reg_rtx, hard_frame_pointer_rtx,
28158 GEN_INT (info->total_size)));
28159 frame_off = 0;
28161 else if (info->push_p
28162 && DEFAULT_ABI != ABI_V4
28163 && epilogue_type != EPILOGUE_TYPE_EH_RETURN)
28165 /* Prevent reordering memory accesses against stack pointer restore. */
28166 if (cfun->calls_alloca
28167 || offset_below_red_zone_p (-info->total_size))
28168 rs6000_emit_stack_tie (frame_reg_rtx, false);
28169 insn = emit_insn (gen_add3_insn (sp_reg_rtx, sp_reg_rtx,
28170 GEN_INT (info->total_size)));
28171 frame_off = 0;
28173 if (insn && frame_reg_rtx == sp_reg_rtx)
28175 if (cfa_restores)
28177 REG_NOTES (insn) = cfa_restores;
28178 cfa_restores = NULL_RTX;
28180 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
28181 RTX_FRAME_RELATED_P (insn) = 1;
28184 /* Restore AltiVec registers if we have not done so already. */
28185 if (!ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
28186 && info->altivec_size != 0
28187 && (DEFAULT_ABI == ABI_V4
28188 || !offset_below_red_zone_p (info->altivec_save_offset)))
28190 int i;
28192 if ((strategy & REST_INLINE_VRS) == 0)
28194 int end_save = info->altivec_save_offset + info->altivec_size;
28195 int ptr_off;
28196 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
28197 int scratch_regno = ptr_regno_for_savres (SAVRES_VR);
28198 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
28200 if (end_save + frame_off != 0)
28202 rtx offset = GEN_INT (end_save + frame_off);
28204 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
28206 else
28207 emit_move_insn (ptr_reg, frame_reg_rtx);
28209 ptr_off = -end_save;
28210 insn = rs6000_emit_savres_rtx (info, scratch_reg,
28211 info->altivec_save_offset + ptr_off,
28212 0, V4SImode, SAVRES_VR);
28213 if (REGNO (frame_reg_rtx) == REGNO (scratch_reg))
28215 /* Frame reg was clobbered by out-of-line save. Restore it
28216 from ptr_reg, and if we are calling out-of-line gpr or
28217 fpr restore set up the correct pointer and offset. */
28218 unsigned newptr_regno = 1;
28219 if (!restoring_GPRs_inline)
28221 bool lr = info->gp_save_offset + info->gp_size == 0;
28222 int sel = SAVRES_GPR | (lr ? SAVRES_LR : 0);
28223 newptr_regno = ptr_regno_for_savres (sel);
28224 end_save = info->gp_save_offset + info->gp_size;
28226 else if (!restoring_FPRs_inline)
28228 bool lr = !(strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR);
28229 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
28230 newptr_regno = ptr_regno_for_savres (sel);
28231 end_save = info->fp_save_offset + info->fp_size;
28234 if (newptr_regno != 1 && REGNO (frame_reg_rtx) != newptr_regno)
28235 frame_reg_rtx = gen_rtx_REG (Pmode, newptr_regno);
28237 if (end_save + ptr_off != 0)
28239 rtx offset = GEN_INT (end_save + ptr_off);
28241 frame_off = -end_save;
28242 if (TARGET_32BIT)
28243 emit_insn (gen_addsi3_carry (frame_reg_rtx,
28244 ptr_reg, offset));
28245 else
28246 emit_insn (gen_adddi3_carry (frame_reg_rtx,
28247 ptr_reg, offset));
28249 else
28251 frame_off = ptr_off;
28252 emit_move_insn (frame_reg_rtx, ptr_reg);
28256 else
28258 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
28259 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
28261 rtx addr, areg, mem, insn;
28262 rtx reg = gen_rtx_REG (V4SImode, i);
28263 HOST_WIDE_INT offset
28264 = (info->altivec_save_offset + frame_off
28265 + 16 * (i - info->first_altivec_reg_save));
28267 if (TARGET_P9_VECTOR && quad_address_offset_p (offset))
28269 mem = gen_frame_mem (V4SImode,
28270 gen_rtx_PLUS (Pmode, frame_reg_rtx,
28271 GEN_INT (offset)));
28272 insn = gen_rtx_SET (reg, mem);
28274 else
28276 areg = gen_rtx_REG (Pmode, 0);
28277 emit_move_insn (areg, GEN_INT (offset));
28279 /* AltiVec addressing mode is [reg+reg]. */
28280 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
28281 mem = gen_frame_mem (V4SImode, addr);
28283 /* Rather than emitting a generic move, force use of the
28284 lvx instruction, which we always want. In particular we
28285 don't want lxvd2x/xxpermdi for little endian. */
28286 insn = gen_altivec_lvx_v4si_internal (reg, mem);
28289 (void) emit_insn (insn);
28293 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
28294 if (((strategy & REST_INLINE_VRS) == 0
28295 || (info->vrsave_mask & ALTIVEC_REG_BIT (i)) != 0)
28296 && (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
28297 && save_reg_p (i))
28299 rtx reg = gen_rtx_REG (V4SImode, i);
28300 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
28304 /* Restore VRSAVE if we have not done so already. */
28305 if (!ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
28306 && info->vrsave_size != 0
28307 && (DEFAULT_ABI == ABI_V4
28308 || !offset_below_red_zone_p (info->vrsave_save_offset)))
28310 rtx reg;
28312 reg = gen_rtx_REG (SImode, 12);
28313 emit_insn (gen_frame_load (reg, frame_reg_rtx,
28314 info->vrsave_save_offset + frame_off));
28316 emit_insn (generate_set_vrsave (reg, info, 1));
28319 /* If we exit by an out-of-line restore function on ABI_V4 then that
28320 function will deallocate the stack, so we don't need to worry
28321 about the unwinder restoring cr from an invalid stack frame
28322 location. */
28323 bool exit_func = (!restoring_FPRs_inline
28324 || (!restoring_GPRs_inline
28325 && info->first_fp_reg_save == 64));
28327 /* In the ELFv2 ABI we need to restore all call-saved CR fields from
28328 *separate* slots if the routine calls __builtin_eh_return, so
28329 that they can be independently restored by the unwinder. */
28330 if (DEFAULT_ABI == ABI_ELFv2 && crtl->calls_eh_return)
28332 int i, cr_off = info->ehcr_offset;
28334 for (i = 0; i < 8; i++)
28335 if (!call_used_regs[CR0_REGNO + i])
28337 rtx reg = gen_rtx_REG (SImode, 0);
28338 emit_insn (gen_frame_load (reg, frame_reg_rtx,
28339 cr_off + frame_off));
28341 insn = emit_insn (gen_movsi_to_cr_one
28342 (gen_rtx_REG (CCmode, CR0_REGNO + i), reg));
28344 if (!exit_func && flag_shrink_wrap)
28346 add_reg_note (insn, REG_CFA_RESTORE,
28347 gen_rtx_REG (SImode, CR0_REGNO + i));
28349 RTX_FRAME_RELATED_P (insn) = 1;
28352 cr_off += reg_size;
28356 /* Get the old lr if we saved it. If we are restoring registers
28357 out-of-line, then the out-of-line routines can do this for us. */
28358 if (restore_lr && restoring_GPRs_inline)
28359 load_lr_save (0, frame_reg_rtx, info->lr_save_offset + frame_off);
28361 /* Get the old cr if we saved it. */
28362 if (info->cr_save_p)
28364 unsigned cr_save_regno = 12;
28366 if (!restoring_GPRs_inline)
28368 /* Ensure we don't use the register used by the out-of-line
28369 gpr register restore below. */
28370 bool lr = info->gp_save_offset + info->gp_size == 0;
28371 int sel = SAVRES_GPR | (lr ? SAVRES_LR : 0);
28372 int gpr_ptr_regno = ptr_regno_for_savres (sel);
28374 if (gpr_ptr_regno == 12)
28375 cr_save_regno = 11;
28376 gcc_checking_assert (REGNO (frame_reg_rtx) != cr_save_regno);
28378 else if (REGNO (frame_reg_rtx) == 12)
28379 cr_save_regno = 11;
28381 cr_save_reg = load_cr_save (cr_save_regno, frame_reg_rtx,
28382 info->cr_save_offset + frame_off,
28383 exit_func);
28386 /* Set LR here to try to overlap restores below. */
28387 if (restore_lr && restoring_GPRs_inline)
28388 restore_saved_lr (0, exit_func);
28390 /* Load exception handler data registers, if needed. */
28391 if (epilogue_type == EPILOGUE_TYPE_EH_RETURN)
28393 unsigned int i, regno;
28395 if (TARGET_AIX)
28397 rtx reg = gen_rtx_REG (reg_mode, 2);
28398 emit_insn (gen_frame_load (reg, frame_reg_rtx,
28399 frame_off + RS6000_TOC_SAVE_SLOT));
28402 for (i = 0; ; ++i)
28404 rtx mem;
28406 regno = EH_RETURN_DATA_REGNO (i);
28407 if (regno == INVALID_REGNUM)
28408 break;
28410 mem = gen_frame_mem_offset (reg_mode, frame_reg_rtx,
28411 info->ehrd_offset + frame_off
28412 + reg_size * (int) i);
28414 emit_move_insn (gen_rtx_REG (reg_mode, regno), mem);
28418 /* Restore GPRs. This is done as a PARALLEL if we are using
28419 the load-multiple instructions. */
28420 if (!restoring_GPRs_inline)
28422 /* We are jumping to an out-of-line function. */
28423 rtx ptr_reg;
28424 int end_save = info->gp_save_offset + info->gp_size;
28425 bool can_use_exit = end_save == 0;
28426 int sel = SAVRES_GPR | (can_use_exit ? SAVRES_LR : 0);
28427 int ptr_off;
28429 /* Emit stack reset code if we need it. */
28430 ptr_regno = ptr_regno_for_savres (sel);
28431 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
28432 if (can_use_exit)
28433 rs6000_emit_stack_reset (frame_reg_rtx, frame_off, ptr_regno);
28434 else if (end_save + frame_off != 0)
28435 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx,
28436 GEN_INT (end_save + frame_off)));
28437 else if (REGNO (frame_reg_rtx) != ptr_regno)
28438 emit_move_insn (ptr_reg, frame_reg_rtx);
28439 if (REGNO (frame_reg_rtx) == ptr_regno)
28440 frame_off = -end_save;
28442 if (can_use_exit && info->cr_save_p)
28443 restore_saved_cr (cr_save_reg, using_mtcr_multiple, true);
28445 ptr_off = -end_save;
28446 rs6000_emit_savres_rtx (info, ptr_reg,
28447 info->gp_save_offset + ptr_off,
28448 info->lr_save_offset + ptr_off,
28449 reg_mode, sel);
28451 else if (using_load_multiple)
28453 rtvec p;
28454 p = rtvec_alloc (32 - info->first_gp_reg_save);
28455 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
28456 RTVEC_ELT (p, i)
28457 = gen_frame_load (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
28458 frame_reg_rtx,
28459 info->gp_save_offset + frame_off + reg_size * i);
28460 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
28462 else
28464 int offset = info->gp_save_offset + frame_off;
28465 for (i = info->first_gp_reg_save; i < 32; i++)
28467 if (save_reg_p (i)
28468 && !cfun->machine->gpr_is_wrapped_separately[i])
28470 rtx reg = gen_rtx_REG (reg_mode, i);
28471 emit_insn (gen_frame_load (reg, frame_reg_rtx, offset));
28474 offset += reg_size;
28478 if (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
28480 /* If the frame pointer was used then we can't delay emitting
28481 a REG_CFA_DEF_CFA note. This must happen on the insn that
28482 restores the frame pointer, r31. We may have already emitted
28483 a REG_CFA_DEF_CFA note, but that's OK; A duplicate is
28484 discarded by dwarf2cfi.c/dwarf2out.c, and in any case would
28485 be harmless if emitted. */
28486 if (frame_pointer_needed)
28488 insn = get_last_insn ();
28489 add_reg_note (insn, REG_CFA_DEF_CFA,
28490 plus_constant (Pmode, frame_reg_rtx, frame_off));
28491 RTX_FRAME_RELATED_P (insn) = 1;
28494 /* Set up cfa_restores. We always need these when
28495 shrink-wrapping. If not shrink-wrapping then we only need
28496 the cfa_restore when the stack location is no longer valid.
28497 The cfa_restores must be emitted on or before the insn that
28498 invalidates the stack, and of course must not be emitted
28499 before the insn that actually does the restore. The latter
28500 is why it is a bad idea to emit the cfa_restores as a group
28501 on the last instruction here that actually does a restore:
28502 That insn may be reordered with respect to others doing
28503 restores. */
28504 if (flag_shrink_wrap
28505 && !restoring_GPRs_inline
28506 && info->first_fp_reg_save == 64)
28507 cfa_restores = add_crlr_cfa_restore (info, cfa_restores);
28509 for (i = info->first_gp_reg_save; i < 32; i++)
28510 if (save_reg_p (i)
28511 && !cfun->machine->gpr_is_wrapped_separately[i])
28513 rtx reg = gen_rtx_REG (reg_mode, i);
28514 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
28518 if (!restoring_GPRs_inline
28519 && info->first_fp_reg_save == 64)
28521 /* We are jumping to an out-of-line function. */
28522 if (cfa_restores)
28523 emit_cfa_restores (cfa_restores);
28524 return;
28527 if (restore_lr && !restoring_GPRs_inline)
28529 load_lr_save (0, frame_reg_rtx, info->lr_save_offset + frame_off);
28530 restore_saved_lr (0, exit_func);
28533 /* Restore fpr's if we need to do it without calling a function. */
28534 if (restoring_FPRs_inline)
28536 int offset = info->fp_save_offset + frame_off;
28537 for (i = info->first_fp_reg_save; i < 64; i++)
28539 if (save_reg_p (i)
28540 && !cfun->machine->fpr_is_wrapped_separately[i - 32])
28542 rtx reg = gen_rtx_REG (fp_reg_mode, i);
28543 emit_insn (gen_frame_load (reg, frame_reg_rtx, offset));
28544 if (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
28545 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg,
28546 cfa_restores);
28549 offset += fp_reg_size;
28553 /* If we saved cr, restore it here. Just those that were used. */
28554 if (info->cr_save_p)
28555 restore_saved_cr (cr_save_reg, using_mtcr_multiple, exit_func);
28557 /* If this is V.4, unwind the stack pointer after all of the loads
28558 have been done, or set up r11 if we are restoring fp out of line. */
28559 ptr_regno = 1;
28560 if (!restoring_FPRs_inline)
28562 bool lr = (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
28563 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
28564 ptr_regno = ptr_regno_for_savres (sel);
28567 insn = rs6000_emit_stack_reset (frame_reg_rtx, frame_off, ptr_regno);
28568 if (REGNO (frame_reg_rtx) == ptr_regno)
28569 frame_off = 0;
28571 if (insn && restoring_FPRs_inline)
28573 if (cfa_restores)
28575 REG_NOTES (insn) = cfa_restores;
28576 cfa_restores = NULL_RTX;
28578 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
28579 RTX_FRAME_RELATED_P (insn) = 1;
28582 if (epilogue_type == EPILOGUE_TYPE_EH_RETURN)
28584 rtx sa = EH_RETURN_STACKADJ_RTX;
28585 emit_insn (gen_add3_insn (sp_reg_rtx, sp_reg_rtx, sa));
28588 if (epilogue_type != EPILOGUE_TYPE_SIBCALL && restoring_FPRs_inline)
28590 if (cfa_restores)
28592 /* We can't hang the cfa_restores off a simple return,
28593 since the shrink-wrap code sometimes uses an existing
28594 return. This means there might be a path from
28595 pre-prologue code to this return, and dwarf2cfi code
28596 wants the eh_frame unwinder state to be the same on
28597 all paths to any point. So we need to emit the
28598 cfa_restores before the return. For -m64 we really
28599 don't need epilogue cfa_restores at all, except for
28600 this irritating dwarf2cfi with shrink-wrap
28601 requirement; The stack red-zone means eh_frame info
28602 from the prologue telling the unwinder to restore
28603 from the stack is perfectly good right to the end of
28604 the function. */
28605 emit_insn (gen_blockage ());
28606 emit_cfa_restores (cfa_restores);
28607 cfa_restores = NULL_RTX;
28610 emit_jump_insn (targetm.gen_simple_return ());
28613 if (epilogue_type != EPILOGUE_TYPE_SIBCALL && !restoring_FPRs_inline)
28615 bool lr = (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
28616 rtvec p = rtvec_alloc (3 + !!lr + 64 - info->first_fp_reg_save);
28617 int elt = 0;
28618 RTVEC_ELT (p, elt++) = ret_rtx;
28619 if (lr)
28620 RTVEC_ELT (p, elt++) = gen_hard_reg_clobber (Pmode, LR_REGNO);
28622 /* We have to restore more than two FP registers, so branch to the
28623 restore function. It will return to our caller. */
28624 int i;
28625 int reg;
28626 rtx sym;
28628 if (flag_shrink_wrap)
28629 cfa_restores = add_crlr_cfa_restore (info, cfa_restores);
28631 sym = rs6000_savres_routine_sym (info, SAVRES_FPR | (lr ? SAVRES_LR : 0));
28632 RTVEC_ELT (p, elt++) = gen_rtx_USE (VOIDmode, sym);
28633 reg = (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)? 1 : 11;
28634 RTVEC_ELT (p, elt++) = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, reg));
28636 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
28638 rtx reg = gen_rtx_REG (DFmode, info->first_fp_reg_save + i);
28640 RTVEC_ELT (p, elt++)
28641 = gen_frame_load (reg, sp_reg_rtx, info->fp_save_offset + 8 * i);
28642 if (flag_shrink_wrap
28643 && save_reg_p (info->first_fp_reg_save + i))
28644 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
28647 emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
28650 if (cfa_restores)
28652 if (epilogue_type == EPILOGUE_TYPE_SIBCALL)
28653 /* Ensure the cfa_restores are hung off an insn that won't
28654 be reordered above other restores. */
28655 emit_insn (gen_blockage ());
28657 emit_cfa_restores (cfa_restores);
28661 /* Write function epilogue. */
28663 static void
28664 rs6000_output_function_epilogue (FILE *file)
28666 #if TARGET_MACHO
28667 macho_branch_islands ();
28670 rtx_insn *insn = get_last_insn ();
28671 rtx_insn *deleted_debug_label = NULL;
28673 /* Mach-O doesn't support labels at the end of objects, so if
28674 it looks like we might want one, take special action.
28676 First, collect any sequence of deleted debug labels. */
28677 while (insn
28678 && NOTE_P (insn)
28679 && NOTE_KIND (insn) != NOTE_INSN_DELETED_LABEL)
28681 /* Don't insert a nop for NOTE_INSN_DELETED_DEBUG_LABEL
28682 notes only, instead set their CODE_LABEL_NUMBER to -1,
28683 otherwise there would be code generation differences
28684 in between -g and -g0. */
28685 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
28686 deleted_debug_label = insn;
28687 insn = PREV_INSN (insn);
28690 /* Second, if we have:
28691 label:
28692 barrier
28693 then this needs to be detected, so skip past the barrier. */
28695 if (insn && BARRIER_P (insn))
28696 insn = PREV_INSN (insn);
28698 /* Up to now we've only seen notes or barriers. */
28699 if (insn)
28701 if (LABEL_P (insn)
28702 || (NOTE_P (insn)
28703 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))
28704 /* Trailing label: <barrier>. */
28705 fputs ("\tnop\n", file);
28706 else
28708 /* Lastly, see if we have a completely empty function body. */
28709 while (insn && ! INSN_P (insn))
28710 insn = PREV_INSN (insn);
28711 /* If we don't find any insns, we've got an empty function body;
28712 I.e. completely empty - without a return or branch. This is
28713 taken as the case where a function body has been removed
28714 because it contains an inline __builtin_unreachable(). GCC
28715 states that reaching __builtin_unreachable() means UB so we're
28716 not obliged to do anything special; however, we want
28717 non-zero-sized function bodies. To meet this, and help the
28718 user out, let's trap the case. */
28719 if (insn == NULL)
28720 fputs ("\ttrap\n", file);
28723 else if (deleted_debug_label)
28724 for (insn = deleted_debug_label; insn; insn = NEXT_INSN (insn))
28725 if (NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
28726 CODE_LABEL_NUMBER (insn) = -1;
28728 #endif
28730 /* Output a traceback table here. See /usr/include/sys/debug.h for info
28731 on its format.
28733 We don't output a traceback table if -finhibit-size-directive was
28734 used. The documentation for -finhibit-size-directive reads
28735 ``don't output a @code{.size} assembler directive, or anything
28736 else that would cause trouble if the function is split in the
28737 middle, and the two halves are placed at locations far apart in
28738 memory.'' The traceback table has this property, since it
28739 includes the offset from the start of the function to the
28740 traceback table itself.
28742 System V.4 Powerpc's (and the embedded ABI derived from it) use a
28743 different traceback table. */
28744 if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
28745 && ! flag_inhibit_size_directive
28746 && rs6000_traceback != traceback_none && !cfun->is_thunk)
28748 const char *fname = NULL;
28749 const char *language_string = lang_hooks.name;
28750 int fixed_parms = 0, float_parms = 0, parm_info = 0;
28751 int i;
28752 int optional_tbtab;
28753 rs6000_stack_t *info = rs6000_stack_info ();
28755 if (rs6000_traceback == traceback_full)
28756 optional_tbtab = 1;
28757 else if (rs6000_traceback == traceback_part)
28758 optional_tbtab = 0;
28759 else
28760 optional_tbtab = !optimize_size && !TARGET_ELF;
28762 if (optional_tbtab)
28764 fname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
28765 while (*fname == '.') /* V.4 encodes . in the name */
28766 fname++;
28768 /* Need label immediately before tbtab, so we can compute
28769 its offset from the function start. */
28770 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
28771 ASM_OUTPUT_LABEL (file, fname);
28774 /* The .tbtab pseudo-op can only be used for the first eight
28775 expressions, since it can't handle the possibly variable
28776 length fields that follow. However, if you omit the optional
28777 fields, the assembler outputs zeros for all optional fields
28778 anyways, giving each variable length field is minimum length
28779 (as defined in sys/debug.h). Thus we cannot use the .tbtab
28780 pseudo-op at all. */
28782 /* An all-zero word flags the start of the tbtab, for debuggers
28783 that have to find it by searching forward from the entry
28784 point or from the current pc. */
28785 fputs ("\t.long 0\n", file);
28787 /* Tbtab format type. Use format type 0. */
28788 fputs ("\t.byte 0,", file);
28790 /* Language type. Unfortunately, there does not seem to be any
28791 official way to discover the language being compiled, so we
28792 use language_string.
28793 C is 0. Fortran is 1. Ada is 3. C++ is 9.
28794 Java is 13. Objective-C is 14. Objective-C++ isn't assigned
28795 a number, so for now use 9. LTO, Go, D, and JIT aren't assigned
28796 numbers either, so for now use 0. */
28797 if (lang_GNU_C ()
28798 || ! strcmp (language_string, "GNU GIMPLE")
28799 || ! strcmp (language_string, "GNU Go")
28800 || ! strcmp (language_string, "GNU D")
28801 || ! strcmp (language_string, "libgccjit"))
28802 i = 0;
28803 else if (! strcmp (language_string, "GNU F77")
28804 || lang_GNU_Fortran ())
28805 i = 1;
28806 else if (! strcmp (language_string, "GNU Ada"))
28807 i = 3;
28808 else if (lang_GNU_CXX ()
28809 || ! strcmp (language_string, "GNU Objective-C++"))
28810 i = 9;
28811 else if (! strcmp (language_string, "GNU Java"))
28812 i = 13;
28813 else if (! strcmp (language_string, "GNU Objective-C"))
28814 i = 14;
28815 else
28816 gcc_unreachable ();
28817 fprintf (file, "%d,", i);
28819 /* 8 single bit fields: global linkage (not set for C extern linkage,
28820 apparently a PL/I convention?), out-of-line epilogue/prologue, offset
28821 from start of procedure stored in tbtab, internal function, function
28822 has controlled storage, function has no toc, function uses fp,
28823 function logs/aborts fp operations. */
28824 /* Assume that fp operations are used if any fp reg must be saved. */
28825 fprintf (file, "%d,",
28826 (optional_tbtab << 5) | ((info->first_fp_reg_save != 64) << 1));
28828 /* 6 bitfields: function is interrupt handler, name present in
28829 proc table, function calls alloca, on condition directives
28830 (controls stack walks, 3 bits), saves condition reg, saves
28831 link reg. */
28832 /* The `function calls alloca' bit seems to be set whenever reg 31 is
28833 set up as a frame pointer, even when there is no alloca call. */
28834 fprintf (file, "%d,",
28835 ((optional_tbtab << 6)
28836 | ((optional_tbtab & frame_pointer_needed) << 5)
28837 | (info->cr_save_p << 1)
28838 | (info->lr_save_p)));
28840 /* 3 bitfields: saves backchain, fixup code, number of fpr saved
28841 (6 bits). */
28842 fprintf (file, "%d,",
28843 (info->push_p << 7) | (64 - info->first_fp_reg_save));
28845 /* 2 bitfields: spare bits (2 bits), number of gpr saved (6 bits). */
28846 fprintf (file, "%d,", (32 - first_reg_to_save ()));
28848 if (optional_tbtab)
28850 /* Compute the parameter info from the function decl argument
28851 list. */
28852 tree decl;
28853 int next_parm_info_bit = 31;
28855 for (decl = DECL_ARGUMENTS (current_function_decl);
28856 decl; decl = DECL_CHAIN (decl))
28858 rtx parameter = DECL_INCOMING_RTL (decl);
28859 machine_mode mode = GET_MODE (parameter);
28861 if (REG_P (parameter))
28863 if (SCALAR_FLOAT_MODE_P (mode))
28865 int bits;
28867 float_parms++;
28869 switch (mode)
28871 case E_SFmode:
28872 case E_SDmode:
28873 bits = 0x2;
28874 break;
28876 case E_DFmode:
28877 case E_DDmode:
28878 case E_TFmode:
28879 case E_TDmode:
28880 case E_IFmode:
28881 case E_KFmode:
28882 bits = 0x3;
28883 break;
28885 default:
28886 gcc_unreachable ();
28889 /* If only one bit will fit, don't or in this entry. */
28890 if (next_parm_info_bit > 0)
28891 parm_info |= (bits << (next_parm_info_bit - 1));
28892 next_parm_info_bit -= 2;
28894 else
28896 fixed_parms += ((GET_MODE_SIZE (mode)
28897 + (UNITS_PER_WORD - 1))
28898 / UNITS_PER_WORD);
28899 next_parm_info_bit -= 1;
28905 /* Number of fixed point parameters. */
28906 /* This is actually the number of words of fixed point parameters; thus
28907 an 8 byte struct counts as 2; and thus the maximum value is 8. */
28908 fprintf (file, "%d,", fixed_parms);
28910 /* 2 bitfields: number of floating point parameters (7 bits), parameters
28911 all on stack. */
28912 /* This is actually the number of fp registers that hold parameters;
28913 and thus the maximum value is 13. */
28914 /* Set parameters on stack bit if parameters are not in their original
28915 registers, regardless of whether they are on the stack? Xlc
28916 seems to set the bit when not optimizing. */
28917 fprintf (file, "%d\n", ((float_parms << 1) | (! optimize)));
28919 if (optional_tbtab)
28921 /* Optional fields follow. Some are variable length. */
28923 /* Parameter types, left adjusted bit fields: 0 fixed, 10 single
28924 float, 11 double float. */
28925 /* There is an entry for each parameter in a register, in the order
28926 that they occur in the parameter list. Any intervening arguments
28927 on the stack are ignored. If the list overflows a long (max
28928 possible length 34 bits) then completely leave off all elements
28929 that don't fit. */
28930 /* Only emit this long if there was at least one parameter. */
28931 if (fixed_parms || float_parms)
28932 fprintf (file, "\t.long %d\n", parm_info);
28934 /* Offset from start of code to tb table. */
28935 fputs ("\t.long ", file);
28936 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
28937 RS6000_OUTPUT_BASENAME (file, fname);
28938 putc ('-', file);
28939 rs6000_output_function_entry (file, fname);
28940 putc ('\n', file);
28942 /* Interrupt handler mask. */
28943 /* Omit this long, since we never set the interrupt handler bit
28944 above. */
28946 /* Number of CTL (controlled storage) anchors. */
28947 /* Omit this long, since the has_ctl bit is never set above. */
28949 /* Displacement into stack of each CTL anchor. */
28950 /* Omit this list of longs, because there are no CTL anchors. */
28952 /* Length of function name. */
28953 if (*fname == '*')
28954 ++fname;
28955 fprintf (file, "\t.short %d\n", (int) strlen (fname));
28957 /* Function name. */
28958 assemble_string (fname, strlen (fname));
28960 /* Register for alloca automatic storage; this is always reg 31.
28961 Only emit this if the alloca bit was set above. */
28962 if (frame_pointer_needed)
28963 fputs ("\t.byte 31\n", file);
28965 fputs ("\t.align 2\n", file);
28969 /* Arrange to define .LCTOC1 label, if not already done. */
28970 if (need_toc_init)
28972 need_toc_init = 0;
28973 if (!toc_initialized)
28975 switch_to_section (toc_section);
28976 switch_to_section (current_function_section ());
28981 /* -fsplit-stack support. */
28983 /* A SYMBOL_REF for __morestack. */
28984 static GTY(()) rtx morestack_ref;
28986 static rtx
28987 gen_add3_const (rtx rt, rtx ra, long c)
28989 if (TARGET_64BIT)
28990 return gen_adddi3 (rt, ra, GEN_INT (c));
28991 else
28992 return gen_addsi3 (rt, ra, GEN_INT (c));
28995 /* Emit -fsplit-stack prologue, which goes before the regular function
28996 prologue (at local entry point in the case of ELFv2). */
28998 void
28999 rs6000_expand_split_stack_prologue (void)
29001 rs6000_stack_t *info = rs6000_stack_info ();
29002 unsigned HOST_WIDE_INT allocate;
29003 long alloc_hi, alloc_lo;
29004 rtx r0, r1, r12, lr, ok_label, compare, jump, call_fusage;
29005 rtx_insn *insn;
29007 gcc_assert (flag_split_stack && reload_completed);
29009 if (!info->push_p)
29010 return;
29012 if (global_regs[29])
29014 error ("%qs uses register r29", "%<-fsplit-stack%>");
29015 inform (DECL_SOURCE_LOCATION (global_regs_decl[29]),
29016 "conflicts with %qD", global_regs_decl[29]);
29019 allocate = info->total_size;
29020 if (allocate > (unsigned HOST_WIDE_INT) 1 << 31)
29022 sorry ("Stack frame larger than 2G is not supported for "
29023 "%<-fsplit-stack%>");
29024 return;
29026 if (morestack_ref == NULL_RTX)
29028 morestack_ref = gen_rtx_SYMBOL_REF (Pmode, "__morestack");
29029 SYMBOL_REF_FLAGS (morestack_ref) |= (SYMBOL_FLAG_LOCAL
29030 | SYMBOL_FLAG_FUNCTION);
29033 r0 = gen_rtx_REG (Pmode, 0);
29034 r1 = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
29035 r12 = gen_rtx_REG (Pmode, 12);
29036 emit_insn (gen_load_split_stack_limit (r0));
29037 /* Always emit two insns here to calculate the requested stack,
29038 so that the linker can edit them when adjusting size for calling
29039 non-split-stack code. */
29040 alloc_hi = (-allocate + 0x8000) & ~0xffffL;
29041 alloc_lo = -allocate - alloc_hi;
29042 if (alloc_hi != 0)
29044 emit_insn (gen_add3_const (r12, r1, alloc_hi));
29045 if (alloc_lo != 0)
29046 emit_insn (gen_add3_const (r12, r12, alloc_lo));
29047 else
29048 emit_insn (gen_nop ());
29050 else
29052 emit_insn (gen_add3_const (r12, r1, alloc_lo));
29053 emit_insn (gen_nop ());
29056 compare = gen_rtx_REG (CCUNSmode, CR7_REGNO);
29057 emit_insn (gen_rtx_SET (compare, gen_rtx_COMPARE (CCUNSmode, r12, r0)));
29058 ok_label = gen_label_rtx ();
29059 jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
29060 gen_rtx_GEU (VOIDmode, compare, const0_rtx),
29061 gen_rtx_LABEL_REF (VOIDmode, ok_label),
29062 pc_rtx);
29063 insn = emit_jump_insn (gen_rtx_SET (pc_rtx, jump));
29064 JUMP_LABEL (insn) = ok_label;
29065 /* Mark the jump as very likely to be taken. */
29066 add_reg_br_prob_note (insn, profile_probability::very_likely ());
29068 lr = gen_rtx_REG (Pmode, LR_REGNO);
29069 insn = emit_move_insn (r0, lr);
29070 RTX_FRAME_RELATED_P (insn) = 1;
29071 insn = emit_insn (gen_frame_store (r0, r1, info->lr_save_offset));
29072 RTX_FRAME_RELATED_P (insn) = 1;
29074 insn = emit_call_insn (gen_call (gen_rtx_MEM (SImode, morestack_ref),
29075 const0_rtx, const0_rtx));
29076 call_fusage = NULL_RTX;
29077 use_reg (&call_fusage, r12);
29078 /* Say the call uses r0, even though it doesn't, to stop regrename
29079 from twiddling with the insns saving lr, trashing args for cfun.
29080 The insns restoring lr are similarly protected by making
29081 split_stack_return use r0. */
29082 use_reg (&call_fusage, r0);
29083 add_function_usage_to (insn, call_fusage);
29084 /* Indicate that this function can't jump to non-local gotos. */
29085 make_reg_eh_region_note_nothrow_nononlocal (insn);
29086 emit_insn (gen_frame_load (r0, r1, info->lr_save_offset));
29087 insn = emit_move_insn (lr, r0);
29088 add_reg_note (insn, REG_CFA_RESTORE, lr);
29089 RTX_FRAME_RELATED_P (insn) = 1;
29090 emit_insn (gen_split_stack_return ());
29092 emit_label (ok_label);
29093 LABEL_NUSES (ok_label) = 1;
29096 /* Return the internal arg pointer used for function incoming
29097 arguments. When -fsplit-stack, the arg pointer is r12 so we need
29098 to copy it to a pseudo in order for it to be preserved over calls
29099 and suchlike. We'd really like to use a pseudo here for the
29100 internal arg pointer but data-flow analysis is not prepared to
29101 accept pseudos as live at the beginning of a function. */
29103 static rtx
29104 rs6000_internal_arg_pointer (void)
29106 if (flag_split_stack
29107 && (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (cfun->decl))
29108 == NULL))
29111 if (cfun->machine->split_stack_arg_pointer == NULL_RTX)
29113 rtx pat;
29115 cfun->machine->split_stack_arg_pointer = gen_reg_rtx (Pmode);
29116 REG_POINTER (cfun->machine->split_stack_arg_pointer) = 1;
29118 /* Put the pseudo initialization right after the note at the
29119 beginning of the function. */
29120 pat = gen_rtx_SET (cfun->machine->split_stack_arg_pointer,
29121 gen_rtx_REG (Pmode, 12));
29122 push_topmost_sequence ();
29123 emit_insn_after (pat, get_insns ());
29124 pop_topmost_sequence ();
29126 rtx ret = plus_constant (Pmode, cfun->machine->split_stack_arg_pointer,
29127 FIRST_PARM_OFFSET (current_function_decl));
29128 return copy_to_reg (ret);
29130 return virtual_incoming_args_rtx;
29133 /* We may have to tell the dataflow pass that the split stack prologue
29134 is initializing a register. */
29136 static void
29137 rs6000_live_on_entry (bitmap regs)
29139 if (flag_split_stack)
29140 bitmap_set_bit (regs, 12);
29143 /* Emit -fsplit-stack dynamic stack allocation space check. */
29145 void
29146 rs6000_split_stack_space_check (rtx size, rtx label)
29148 rtx sp = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
29149 rtx limit = gen_reg_rtx (Pmode);
29150 rtx requested = gen_reg_rtx (Pmode);
29151 rtx cmp = gen_reg_rtx (CCUNSmode);
29152 rtx jump;
29154 emit_insn (gen_load_split_stack_limit (limit));
29155 if (CONST_INT_P (size))
29156 emit_insn (gen_add3_insn (requested, sp, GEN_INT (-INTVAL (size))));
29157 else
29159 size = force_reg (Pmode, size);
29160 emit_move_insn (requested, gen_rtx_MINUS (Pmode, sp, size));
29162 emit_insn (gen_rtx_SET (cmp, gen_rtx_COMPARE (CCUNSmode, requested, limit)));
29163 jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
29164 gen_rtx_GEU (VOIDmode, cmp, const0_rtx),
29165 gen_rtx_LABEL_REF (VOIDmode, label),
29166 pc_rtx);
29167 jump = emit_jump_insn (gen_rtx_SET (pc_rtx, jump));
29168 JUMP_LABEL (jump) = label;
29171 /* A C compound statement that outputs the assembler code for a thunk
29172 function, used to implement C++ virtual function calls with
29173 multiple inheritance. The thunk acts as a wrapper around a virtual
29174 function, adjusting the implicit object parameter before handing
29175 control off to the real function.
29177 First, emit code to add the integer DELTA to the location that
29178 contains the incoming first argument. Assume that this argument
29179 contains a pointer, and is the one used to pass the `this' pointer
29180 in C++. This is the incoming argument *before* the function
29181 prologue, e.g. `%o0' on a sparc. The addition must preserve the
29182 values of all other incoming arguments.
29184 After the addition, emit code to jump to FUNCTION, which is a
29185 `FUNCTION_DECL'. This is a direct pure jump, not a call, and does
29186 not touch the return address. Hence returning from FUNCTION will
29187 return to whoever called the current `thunk'.
29189 The effect must be as if FUNCTION had been called directly with the
29190 adjusted first argument. This macro is responsible for emitting
29191 all of the code for a thunk function; output_function_prologue()
29192 and output_function_epilogue() are not invoked.
29194 The THUNK_FNDECL is redundant. (DELTA and FUNCTION have already
29195 been extracted from it.) It might possibly be useful on some
29196 targets, but probably not.
29198 If you do not define this macro, the target-independent code in the
29199 C++ frontend will generate a less efficient heavyweight thunk that
29200 calls FUNCTION instead of jumping to it. The generic approach does
29201 not support varargs. */
29203 static void
29204 rs6000_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
29205 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
29206 tree function)
29208 const char *fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl));
29209 rtx this_rtx, funexp;
29210 rtx_insn *insn;
29212 reload_completed = 1;
29213 epilogue_completed = 1;
29215 /* Mark the end of the (empty) prologue. */
29216 emit_note (NOTE_INSN_PROLOGUE_END);
29218 /* Find the "this" pointer. If the function returns a structure,
29219 the structure return pointer is in r3. */
29220 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
29221 this_rtx = gen_rtx_REG (Pmode, 4);
29222 else
29223 this_rtx = gen_rtx_REG (Pmode, 3);
29225 /* Apply the constant offset, if required. */
29226 if (delta)
29227 emit_insn (gen_add3_insn (this_rtx, this_rtx, GEN_INT (delta)));
29229 /* Apply the offset from the vtable, if required. */
29230 if (vcall_offset)
29232 rtx vcall_offset_rtx = GEN_INT (vcall_offset);
29233 rtx tmp = gen_rtx_REG (Pmode, 12);
29235 emit_move_insn (tmp, gen_rtx_MEM (Pmode, this_rtx));
29236 if (((unsigned HOST_WIDE_INT) vcall_offset) + 0x8000 >= 0x10000)
29238 emit_insn (gen_add3_insn (tmp, tmp, vcall_offset_rtx));
29239 emit_move_insn (tmp, gen_rtx_MEM (Pmode, tmp));
29241 else
29243 rtx loc = gen_rtx_PLUS (Pmode, tmp, vcall_offset_rtx);
29245 emit_move_insn (tmp, gen_rtx_MEM (Pmode, loc));
29247 emit_insn (gen_add3_insn (this_rtx, this_rtx, tmp));
29250 /* Generate a tail call to the target function. */
29251 if (!TREE_USED (function))
29253 assemble_external (function);
29254 TREE_USED (function) = 1;
29256 funexp = XEXP (DECL_RTL (function), 0);
29257 funexp = gen_rtx_MEM (FUNCTION_MODE, funexp);
29259 #if TARGET_MACHO
29260 if (MACHOPIC_INDIRECT)
29261 funexp = machopic_indirect_call_target (funexp);
29262 #endif
29264 /* gen_sibcall expects reload to convert scratch pseudo to LR so we must
29265 generate sibcall RTL explicitly. */
29266 insn = emit_call_insn (
29267 gen_rtx_PARALLEL (VOIDmode,
29268 gen_rtvec (3,
29269 gen_rtx_CALL (VOIDmode,
29270 funexp, const0_rtx),
29271 gen_rtx_USE (VOIDmode, const0_rtx),
29272 simple_return_rtx)));
29273 SIBLING_CALL_P (insn) = 1;
29274 emit_barrier ();
29276 /* Run just enough of rest_of_compilation to get the insns emitted.
29277 There's not really enough bulk here to make other passes such as
29278 instruction scheduling worth while. Note that use_thunk calls
29279 assemble_start_function and assemble_end_function. */
29280 insn = get_insns ();
29281 shorten_branches (insn);
29282 assemble_start_function (thunk_fndecl, fnname);
29283 final_start_function (insn, file, 1);
29284 final (insn, file, 1);
29285 final_end_function ();
29286 assemble_end_function (thunk_fndecl, fnname);
29288 reload_completed = 0;
29289 epilogue_completed = 0;
29292 /* A quick summary of the various types of 'constant-pool tables'
29293 under PowerPC:
29295 Target Flags Name One table per
29296 AIX (none) AIX TOC object file
29297 AIX -mfull-toc AIX TOC object file
29298 AIX -mminimal-toc AIX minimal TOC translation unit
29299 SVR4/EABI (none) SVR4 SDATA object file
29300 SVR4/EABI -fpic SVR4 pic object file
29301 SVR4/EABI -fPIC SVR4 PIC translation unit
29302 SVR4/EABI -mrelocatable EABI TOC function
29303 SVR4/EABI -maix AIX TOC object file
29304 SVR4/EABI -maix -mminimal-toc
29305 AIX minimal TOC translation unit
29307 Name Reg. Set by entries contains:
29308 made by addrs? fp? sum?
29310 AIX TOC 2 crt0 as Y option option
29311 AIX minimal TOC 30 prolog gcc Y Y option
29312 SVR4 SDATA 13 crt0 gcc N Y N
29313 SVR4 pic 30 prolog ld Y not yet N
29314 SVR4 PIC 30 prolog gcc Y option option
29315 EABI TOC 30 prolog gcc Y option option
29319 /* Hash functions for the hash table. */
29321 static unsigned
29322 rs6000_hash_constant (rtx k)
29324 enum rtx_code code = GET_CODE (k);
29325 machine_mode mode = GET_MODE (k);
29326 unsigned result = (code << 3) ^ mode;
29327 const char *format;
29328 int flen, fidx;
29330 format = GET_RTX_FORMAT (code);
29331 flen = strlen (format);
29332 fidx = 0;
29334 switch (code)
29336 case LABEL_REF:
29337 return result * 1231 + (unsigned) INSN_UID (XEXP (k, 0));
29339 case CONST_WIDE_INT:
29341 int i;
29342 flen = CONST_WIDE_INT_NUNITS (k);
29343 for (i = 0; i < flen; i++)
29344 result = result * 613 + CONST_WIDE_INT_ELT (k, i);
29345 return result;
29348 case CONST_DOUBLE:
29349 return real_hash (CONST_DOUBLE_REAL_VALUE (k)) * result;
29351 case CODE_LABEL:
29352 fidx = 3;
29353 break;
29355 default:
29356 break;
29359 for (; fidx < flen; fidx++)
29360 switch (format[fidx])
29362 case 's':
29364 unsigned i, len;
29365 const char *str = XSTR (k, fidx);
29366 len = strlen (str);
29367 result = result * 613 + len;
29368 for (i = 0; i < len; i++)
29369 result = result * 613 + (unsigned) str[i];
29370 break;
29372 case 'u':
29373 case 'e':
29374 result = result * 1231 + rs6000_hash_constant (XEXP (k, fidx));
29375 break;
29376 case 'i':
29377 case 'n':
29378 result = result * 613 + (unsigned) XINT (k, fidx);
29379 break;
29380 case 'w':
29381 if (sizeof (unsigned) >= sizeof (HOST_WIDE_INT))
29382 result = result * 613 + (unsigned) XWINT (k, fidx);
29383 else
29385 size_t i;
29386 for (i = 0; i < sizeof (HOST_WIDE_INT) / sizeof (unsigned); i++)
29387 result = result * 613 + (unsigned) (XWINT (k, fidx)
29388 >> CHAR_BIT * i);
29390 break;
29391 case '0':
29392 break;
29393 default:
29394 gcc_unreachable ();
29397 return result;
29400 hashval_t
29401 toc_hasher::hash (toc_hash_struct *thc)
29403 return rs6000_hash_constant (thc->key) ^ thc->key_mode;
29406 /* Compare H1 and H2 for equivalence. */
29408 bool
29409 toc_hasher::equal (toc_hash_struct *h1, toc_hash_struct *h2)
29411 rtx r1 = h1->key;
29412 rtx r2 = h2->key;
29414 if (h1->key_mode != h2->key_mode)
29415 return 0;
29417 return rtx_equal_p (r1, r2);
29420 /* These are the names given by the C++ front-end to vtables, and
29421 vtable-like objects. Ideally, this logic should not be here;
29422 instead, there should be some programmatic way of inquiring as
29423 to whether or not an object is a vtable. */
29425 #define VTABLE_NAME_P(NAME) \
29426 (strncmp ("_vt.", name, strlen ("_vt.")) == 0 \
29427 || strncmp ("_ZTV", name, strlen ("_ZTV")) == 0 \
29428 || strncmp ("_ZTT", name, strlen ("_ZTT")) == 0 \
29429 || strncmp ("_ZTI", name, strlen ("_ZTI")) == 0 \
29430 || strncmp ("_ZTC", name, strlen ("_ZTC")) == 0)
29432 #ifdef NO_DOLLAR_IN_LABEL
29433 /* Return a GGC-allocated character string translating dollar signs in
29434 input NAME to underscores. Used by XCOFF ASM_OUTPUT_LABELREF. */
29436 const char *
29437 rs6000_xcoff_strip_dollar (const char *name)
29439 char *strip, *p;
29440 const char *q;
29441 size_t len;
29443 q = (const char *) strchr (name, '$');
29445 if (q == 0 || q == name)
29446 return name;
29448 len = strlen (name);
29449 strip = XALLOCAVEC (char, len + 1);
29450 strcpy (strip, name);
29451 p = strip + (q - name);
29452 while (p)
29454 *p = '_';
29455 p = strchr (p + 1, '$');
29458 return ggc_alloc_string (strip, len);
29460 #endif
29462 void
29463 rs6000_output_symbol_ref (FILE *file, rtx x)
29465 const char *name = XSTR (x, 0);
29467 /* Currently C++ toc references to vtables can be emitted before it
29468 is decided whether the vtable is public or private. If this is
29469 the case, then the linker will eventually complain that there is
29470 a reference to an unknown section. Thus, for vtables only,
29471 we emit the TOC reference to reference the identifier and not the
29472 symbol. */
29473 if (VTABLE_NAME_P (name))
29475 RS6000_OUTPUT_BASENAME (file, name);
29477 else
29478 assemble_name (file, name);
29481 /* Output a TOC entry. We derive the entry name from what is being
29482 written. */
29484 void
29485 output_toc (FILE *file, rtx x, int labelno, machine_mode mode)
29487 char buf[256];
29488 const char *name = buf;
29489 rtx base = x;
29490 HOST_WIDE_INT offset = 0;
29492 gcc_assert (!TARGET_NO_TOC);
29494 /* When the linker won't eliminate them, don't output duplicate
29495 TOC entries (this happens on AIX if there is any kind of TOC,
29496 and on SVR4 under -fPIC or -mrelocatable). Don't do this for
29497 CODE_LABELs. */
29498 if (TARGET_TOC && GET_CODE (x) != LABEL_REF)
29500 struct toc_hash_struct *h;
29502 /* Create toc_hash_table. This can't be done at TARGET_OPTION_OVERRIDE
29503 time because GGC is not initialized at that point. */
29504 if (toc_hash_table == NULL)
29505 toc_hash_table = hash_table<toc_hasher>::create_ggc (1021);
29507 h = ggc_alloc<toc_hash_struct> ();
29508 h->key = x;
29509 h->key_mode = mode;
29510 h->labelno = labelno;
29512 toc_hash_struct **found = toc_hash_table->find_slot (h, INSERT);
29513 if (*found == NULL)
29514 *found = h;
29515 else /* This is indeed a duplicate.
29516 Set this label equal to that label. */
29518 fputs ("\t.set ", file);
29519 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
29520 fprintf (file, "%d,", labelno);
29521 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
29522 fprintf (file, "%d\n", ((*found)->labelno));
29524 #ifdef HAVE_AS_TLS
29525 if (TARGET_XCOFF && SYMBOL_REF_P (x)
29526 && (SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_GLOBAL_DYNAMIC
29527 || SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC))
29529 fputs ("\t.set ", file);
29530 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LCM");
29531 fprintf (file, "%d,", labelno);
29532 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LCM");
29533 fprintf (file, "%d\n", ((*found)->labelno));
29535 #endif
29536 return;
29540 /* If we're going to put a double constant in the TOC, make sure it's
29541 aligned properly when strict alignment is on. */
29542 if ((CONST_DOUBLE_P (x) || CONST_WIDE_INT_P (x))
29543 && STRICT_ALIGNMENT
29544 && GET_MODE_BITSIZE (mode) >= 64
29545 && ! (TARGET_NO_FP_IN_TOC && ! TARGET_MINIMAL_TOC)) {
29546 ASM_OUTPUT_ALIGN (file, 3);
29549 (*targetm.asm_out.internal_label) (file, "LC", labelno);
29551 /* Handle FP constants specially. Note that if we have a minimal
29552 TOC, things we put here aren't actually in the TOC, so we can allow
29553 FP constants. */
29554 if (CONST_DOUBLE_P (x)
29555 && (GET_MODE (x) == TFmode || GET_MODE (x) == TDmode
29556 || GET_MODE (x) == IFmode || GET_MODE (x) == KFmode))
29558 long k[4];
29560 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
29561 REAL_VALUE_TO_TARGET_DECIMAL128 (*CONST_DOUBLE_REAL_VALUE (x), k);
29562 else
29563 REAL_VALUE_TO_TARGET_LONG_DOUBLE (*CONST_DOUBLE_REAL_VALUE (x), k);
29565 if (TARGET_64BIT)
29567 if (TARGET_ELF || TARGET_MINIMAL_TOC)
29568 fputs (DOUBLE_INT_ASM_OP, file);
29569 else
29570 fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
29571 k[0] & 0xffffffff, k[1] & 0xffffffff,
29572 k[2] & 0xffffffff, k[3] & 0xffffffff);
29573 fprintf (file, "0x%lx%08lx,0x%lx%08lx\n",
29574 k[WORDS_BIG_ENDIAN ? 0 : 1] & 0xffffffff,
29575 k[WORDS_BIG_ENDIAN ? 1 : 0] & 0xffffffff,
29576 k[WORDS_BIG_ENDIAN ? 2 : 3] & 0xffffffff,
29577 k[WORDS_BIG_ENDIAN ? 3 : 2] & 0xffffffff);
29578 return;
29580 else
29582 if (TARGET_ELF || TARGET_MINIMAL_TOC)
29583 fputs ("\t.long ", file);
29584 else
29585 fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
29586 k[0] & 0xffffffff, k[1] & 0xffffffff,
29587 k[2] & 0xffffffff, k[3] & 0xffffffff);
29588 fprintf (file, "0x%lx,0x%lx,0x%lx,0x%lx\n",
29589 k[0] & 0xffffffff, k[1] & 0xffffffff,
29590 k[2] & 0xffffffff, k[3] & 0xffffffff);
29591 return;
29594 else if (CONST_DOUBLE_P (x)
29595 && (GET_MODE (x) == DFmode || GET_MODE (x) == DDmode))
29597 long k[2];
29599 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
29600 REAL_VALUE_TO_TARGET_DECIMAL64 (*CONST_DOUBLE_REAL_VALUE (x), k);
29601 else
29602 REAL_VALUE_TO_TARGET_DOUBLE (*CONST_DOUBLE_REAL_VALUE (x), k);
29604 if (TARGET_64BIT)
29606 if (TARGET_ELF || TARGET_MINIMAL_TOC)
29607 fputs (DOUBLE_INT_ASM_OP, file);
29608 else
29609 fprintf (file, "\t.tc FD_%lx_%lx[TC],",
29610 k[0] & 0xffffffff, k[1] & 0xffffffff);
29611 fprintf (file, "0x%lx%08lx\n",
29612 k[WORDS_BIG_ENDIAN ? 0 : 1] & 0xffffffff,
29613 k[WORDS_BIG_ENDIAN ? 1 : 0] & 0xffffffff);
29614 return;
29616 else
29618 if (TARGET_ELF || TARGET_MINIMAL_TOC)
29619 fputs ("\t.long ", file);
29620 else
29621 fprintf (file, "\t.tc FD_%lx_%lx[TC],",
29622 k[0] & 0xffffffff, k[1] & 0xffffffff);
29623 fprintf (file, "0x%lx,0x%lx\n",
29624 k[0] & 0xffffffff, k[1] & 0xffffffff);
29625 return;
29628 else if (CONST_DOUBLE_P (x)
29629 && (GET_MODE (x) == SFmode || GET_MODE (x) == SDmode))
29631 long l;
29633 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
29634 REAL_VALUE_TO_TARGET_DECIMAL32 (*CONST_DOUBLE_REAL_VALUE (x), l);
29635 else
29636 REAL_VALUE_TO_TARGET_SINGLE (*CONST_DOUBLE_REAL_VALUE (x), l);
29638 if (TARGET_64BIT)
29640 if (TARGET_ELF || TARGET_MINIMAL_TOC)
29641 fputs (DOUBLE_INT_ASM_OP, file);
29642 else
29643 fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
29644 if (WORDS_BIG_ENDIAN)
29645 fprintf (file, "0x%lx00000000\n", l & 0xffffffff);
29646 else
29647 fprintf (file, "0x%lx\n", l & 0xffffffff);
29648 return;
29650 else
29652 if (TARGET_ELF || TARGET_MINIMAL_TOC)
29653 fputs ("\t.long ", file);
29654 else
29655 fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
29656 fprintf (file, "0x%lx\n", l & 0xffffffff);
29657 return;
29660 else if (GET_MODE (x) == VOIDmode && CONST_INT_P (x))
29662 unsigned HOST_WIDE_INT low;
29663 HOST_WIDE_INT high;
29665 low = INTVAL (x) & 0xffffffff;
29666 high = (HOST_WIDE_INT) INTVAL (x) >> 32;
29668 /* TOC entries are always Pmode-sized, so when big-endian
29669 smaller integer constants in the TOC need to be padded.
29670 (This is still a win over putting the constants in
29671 a separate constant pool, because then we'd have
29672 to have both a TOC entry _and_ the actual constant.)
29674 For a 32-bit target, CONST_INT values are loaded and shifted
29675 entirely within `low' and can be stored in one TOC entry. */
29677 /* It would be easy to make this work, but it doesn't now. */
29678 gcc_assert (!TARGET_64BIT || POINTER_SIZE >= GET_MODE_BITSIZE (mode));
29680 if (WORDS_BIG_ENDIAN && POINTER_SIZE > GET_MODE_BITSIZE (mode))
29682 low |= high << 32;
29683 low <<= POINTER_SIZE - GET_MODE_BITSIZE (mode);
29684 high = (HOST_WIDE_INT) low >> 32;
29685 low &= 0xffffffff;
29688 if (TARGET_64BIT)
29690 if (TARGET_ELF || TARGET_MINIMAL_TOC)
29691 fputs (DOUBLE_INT_ASM_OP, file);
29692 else
29693 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
29694 (long) high & 0xffffffff, (long) low & 0xffffffff);
29695 fprintf (file, "0x%lx%08lx\n",
29696 (long) high & 0xffffffff, (long) low & 0xffffffff);
29697 return;
29699 else
29701 if (POINTER_SIZE < GET_MODE_BITSIZE (mode))
29703 if (TARGET_ELF || TARGET_MINIMAL_TOC)
29704 fputs ("\t.long ", file);
29705 else
29706 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
29707 (long) high & 0xffffffff, (long) low & 0xffffffff);
29708 fprintf (file, "0x%lx,0x%lx\n",
29709 (long) high & 0xffffffff, (long) low & 0xffffffff);
29711 else
29713 if (TARGET_ELF || TARGET_MINIMAL_TOC)
29714 fputs ("\t.long ", file);
29715 else
29716 fprintf (file, "\t.tc IS_%lx[TC],", (long) low & 0xffffffff);
29717 fprintf (file, "0x%lx\n", (long) low & 0xffffffff);
29719 return;
29723 if (GET_CODE (x) == CONST)
29725 gcc_assert (GET_CODE (XEXP (x, 0)) == PLUS
29726 && CONST_INT_P (XEXP (XEXP (x, 0), 1)));
29728 base = XEXP (XEXP (x, 0), 0);
29729 offset = INTVAL (XEXP (XEXP (x, 0), 1));
29732 switch (GET_CODE (base))
29734 case SYMBOL_REF:
29735 name = XSTR (base, 0);
29736 break;
29738 case LABEL_REF:
29739 ASM_GENERATE_INTERNAL_LABEL (buf, "L",
29740 CODE_LABEL_NUMBER (XEXP (base, 0)));
29741 break;
29743 case CODE_LABEL:
29744 ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (base));
29745 break;
29747 default:
29748 gcc_unreachable ();
29751 if (TARGET_ELF || TARGET_MINIMAL_TOC)
29752 fputs (TARGET_32BIT ? "\t.long " : DOUBLE_INT_ASM_OP, file);
29753 else
29755 fputs ("\t.tc ", file);
29756 RS6000_OUTPUT_BASENAME (file, name);
29758 if (offset < 0)
29759 fprintf (file, ".N" HOST_WIDE_INT_PRINT_UNSIGNED, - offset);
29760 else if (offset)
29761 fprintf (file, ".P" HOST_WIDE_INT_PRINT_UNSIGNED, offset);
29763 /* Mark large TOC symbols on AIX with [TE] so they are mapped
29764 after other TOC symbols, reducing overflow of small TOC access
29765 to [TC] symbols. */
29766 fputs (TARGET_XCOFF && TARGET_CMODEL != CMODEL_SMALL
29767 ? "[TE]," : "[TC],", file);
29770 /* Currently C++ toc references to vtables can be emitted before it
29771 is decided whether the vtable is public or private. If this is
29772 the case, then the linker will eventually complain that there is
29773 a TOC reference to an unknown section. Thus, for vtables only,
29774 we emit the TOC reference to reference the symbol and not the
29775 section. */
29776 if (VTABLE_NAME_P (name))
29778 RS6000_OUTPUT_BASENAME (file, name);
29779 if (offset < 0)
29780 fprintf (file, HOST_WIDE_INT_PRINT_DEC, offset);
29781 else if (offset > 0)
29782 fprintf (file, "+" HOST_WIDE_INT_PRINT_DEC, offset);
29784 else
29785 output_addr_const (file, x);
29787 #if HAVE_AS_TLS
29788 if (TARGET_XCOFF && SYMBOL_REF_P (base))
29790 switch (SYMBOL_REF_TLS_MODEL (base))
29792 case 0:
29793 break;
29794 case TLS_MODEL_LOCAL_EXEC:
29795 fputs ("@le", file);
29796 break;
29797 case TLS_MODEL_INITIAL_EXEC:
29798 fputs ("@ie", file);
29799 break;
29800 /* Use global-dynamic for local-dynamic. */
29801 case TLS_MODEL_GLOBAL_DYNAMIC:
29802 case TLS_MODEL_LOCAL_DYNAMIC:
29803 putc ('\n', file);
29804 (*targetm.asm_out.internal_label) (file, "LCM", labelno);
29805 fputs ("\t.tc .", file);
29806 RS6000_OUTPUT_BASENAME (file, name);
29807 fputs ("[TC],", file);
29808 output_addr_const (file, x);
29809 fputs ("@m", file);
29810 break;
29811 default:
29812 gcc_unreachable ();
29815 #endif
29817 putc ('\n', file);
29820 /* Output an assembler pseudo-op to write an ASCII string of N characters
29821 starting at P to FILE.
29823 On the RS/6000, we have to do this using the .byte operation and
29824 write out special characters outside the quoted string.
29825 Also, the assembler is broken; very long strings are truncated,
29826 so we must artificially break them up early. */
29828 void
29829 output_ascii (FILE *file, const char *p, int n)
29831 char c;
29832 int i, count_string;
29833 const char *for_string = "\t.byte \"";
29834 const char *for_decimal = "\t.byte ";
29835 const char *to_close = NULL;
29837 count_string = 0;
29838 for (i = 0; i < n; i++)
29840 c = *p++;
29841 if (c >= ' ' && c < 0177)
29843 if (for_string)
29844 fputs (for_string, file);
29845 putc (c, file);
29847 /* Write two quotes to get one. */
29848 if (c == '"')
29850 putc (c, file);
29851 ++count_string;
29854 for_string = NULL;
29855 for_decimal = "\"\n\t.byte ";
29856 to_close = "\"\n";
29857 ++count_string;
29859 if (count_string >= 512)
29861 fputs (to_close, file);
29863 for_string = "\t.byte \"";
29864 for_decimal = "\t.byte ";
29865 to_close = NULL;
29866 count_string = 0;
29869 else
29871 if (for_decimal)
29872 fputs (for_decimal, file);
29873 fprintf (file, "%d", c);
29875 for_string = "\n\t.byte \"";
29876 for_decimal = ", ";
29877 to_close = "\n";
29878 count_string = 0;
29882 /* Now close the string if we have written one. Then end the line. */
29883 if (to_close)
29884 fputs (to_close, file);
29887 /* Generate a unique section name for FILENAME for a section type
29888 represented by SECTION_DESC. Output goes into BUF.
29890 SECTION_DESC can be any string, as long as it is different for each
29891 possible section type.
29893 We name the section in the same manner as xlc. The name begins with an
29894 underscore followed by the filename (after stripping any leading directory
29895 names) with the last period replaced by the string SECTION_DESC. If
29896 FILENAME does not contain a period, SECTION_DESC is appended to the end of
29897 the name. */
29899 void
29900 rs6000_gen_section_name (char **buf, const char *filename,
29901 const char *section_desc)
29903 const char *q, *after_last_slash, *last_period = 0;
29904 char *p;
29905 int len;
29907 after_last_slash = filename;
29908 for (q = filename; *q; q++)
29910 if (*q == '/')
29911 after_last_slash = q + 1;
29912 else if (*q == '.')
29913 last_period = q;
29916 len = strlen (after_last_slash) + strlen (section_desc) + 2;
29917 *buf = (char *) xmalloc (len);
29919 p = *buf;
29920 *p++ = '_';
29922 for (q = after_last_slash; *q; q++)
29924 if (q == last_period)
29926 strcpy (p, section_desc);
29927 p += strlen (section_desc);
29928 break;
29931 else if (ISALNUM (*q))
29932 *p++ = *q;
29935 if (last_period == 0)
29936 strcpy (p, section_desc);
29937 else
29938 *p = '\0';
29941 /* Emit profile function. */
29943 void
29944 output_profile_hook (int labelno ATTRIBUTE_UNUSED)
29946 /* Non-standard profiling for kernels, which just saves LR then calls
29947 _mcount without worrying about arg saves. The idea is to change
29948 the function prologue as little as possible as it isn't easy to
29949 account for arg save/restore code added just for _mcount. */
29950 if (TARGET_PROFILE_KERNEL)
29951 return;
29953 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
29955 #ifndef NO_PROFILE_COUNTERS
29956 # define NO_PROFILE_COUNTERS 0
29957 #endif
29958 if (NO_PROFILE_COUNTERS)
29959 emit_library_call (init_one_libfunc (RS6000_MCOUNT),
29960 LCT_NORMAL, VOIDmode);
29961 else
29963 char buf[30];
29964 const char *label_name;
29965 rtx fun;
29967 ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
29968 label_name = ggc_strdup ((*targetm.strip_name_encoding) (buf));
29969 fun = gen_rtx_SYMBOL_REF (Pmode, label_name);
29971 emit_library_call (init_one_libfunc (RS6000_MCOUNT),
29972 LCT_NORMAL, VOIDmode, fun, Pmode);
29975 else if (DEFAULT_ABI == ABI_DARWIN)
29977 const char *mcount_name = RS6000_MCOUNT;
29978 int caller_addr_regno = LR_REGNO;
29980 /* Be conservative and always set this, at least for now. */
29981 crtl->uses_pic_offset_table = 1;
29983 #if TARGET_MACHO
29984 /* For PIC code, set up a stub and collect the caller's address
29985 from r0, which is where the prologue puts it. */
29986 if (MACHOPIC_INDIRECT
29987 && crtl->uses_pic_offset_table)
29988 caller_addr_regno = 0;
29989 #endif
29990 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, mcount_name),
29991 LCT_NORMAL, VOIDmode,
29992 gen_rtx_REG (Pmode, caller_addr_regno), Pmode);
29996 /* Write function profiler code. */
29998 void
29999 output_function_profiler (FILE *file, int labelno)
30001 char buf[100];
30003 switch (DEFAULT_ABI)
30005 default:
30006 gcc_unreachable ();
30008 case ABI_V4:
30009 if (!TARGET_32BIT)
30011 warning (0, "no profiling of 64-bit code for this ABI");
30012 return;
30014 ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
30015 fprintf (file, "\tmflr %s\n", reg_names[0]);
30016 if (NO_PROFILE_COUNTERS)
30018 asm_fprintf (file, "\tstw %s,4(%s)\n",
30019 reg_names[0], reg_names[1]);
30021 else if (TARGET_SECURE_PLT && flag_pic)
30023 if (TARGET_LINK_STACK)
30025 char name[32];
30026 get_ppc476_thunk_name (name);
30027 asm_fprintf (file, "\tbl %s\n", name);
30029 else
30030 asm_fprintf (file, "\tbcl 20,31,1f\n1:\n");
30031 asm_fprintf (file, "\tstw %s,4(%s)\n",
30032 reg_names[0], reg_names[1]);
30033 asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
30034 asm_fprintf (file, "\taddis %s,%s,",
30035 reg_names[12], reg_names[12]);
30036 assemble_name (file, buf);
30037 asm_fprintf (file, "-1b@ha\n\tla %s,", reg_names[0]);
30038 assemble_name (file, buf);
30039 asm_fprintf (file, "-1b@l(%s)\n", reg_names[12]);
30041 else if (flag_pic == 1)
30043 fputs ("\tbl _GLOBAL_OFFSET_TABLE_@local-4\n", file);
30044 asm_fprintf (file, "\tstw %s,4(%s)\n",
30045 reg_names[0], reg_names[1]);
30046 asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
30047 asm_fprintf (file, "\tlwz %s,", reg_names[0]);
30048 assemble_name (file, buf);
30049 asm_fprintf (file, "@got(%s)\n", reg_names[12]);
30051 else if (flag_pic > 1)
30053 asm_fprintf (file, "\tstw %s,4(%s)\n",
30054 reg_names[0], reg_names[1]);
30055 /* Now, we need to get the address of the label. */
30056 if (TARGET_LINK_STACK)
30058 char name[32];
30059 get_ppc476_thunk_name (name);
30060 asm_fprintf (file, "\tbl %s\n\tb 1f\n\t.long ", name);
30061 assemble_name (file, buf);
30062 fputs ("-.\n1:", file);
30063 asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
30064 asm_fprintf (file, "\taddi %s,%s,4\n",
30065 reg_names[11], reg_names[11]);
30067 else
30069 fputs ("\tbcl 20,31,1f\n\t.long ", file);
30070 assemble_name (file, buf);
30071 fputs ("-.\n1:", file);
30072 asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
30074 asm_fprintf (file, "\tlwz %s,0(%s)\n",
30075 reg_names[0], reg_names[11]);
30076 asm_fprintf (file, "\tadd %s,%s,%s\n",
30077 reg_names[0], reg_names[0], reg_names[11]);
30079 else
30081 asm_fprintf (file, "\tlis %s,", reg_names[12]);
30082 assemble_name (file, buf);
30083 fputs ("@ha\n", file);
30084 asm_fprintf (file, "\tstw %s,4(%s)\n",
30085 reg_names[0], reg_names[1]);
30086 asm_fprintf (file, "\tla %s,", reg_names[0]);
30087 assemble_name (file, buf);
30088 asm_fprintf (file, "@l(%s)\n", reg_names[12]);
30091 /* ABI_V4 saves the static chain reg with ASM_OUTPUT_REG_PUSH. */
30092 fprintf (file, "\tbl %s%s\n",
30093 RS6000_MCOUNT, flag_pic ? "@plt" : "");
30094 break;
30096 case ABI_AIX:
30097 case ABI_ELFv2:
30098 case ABI_DARWIN:
30099 /* Don't do anything, done in output_profile_hook (). */
30100 break;
30106 /* The following variable value is the last issued insn. */
30108 static rtx_insn *last_scheduled_insn;
30110 /* The following variable helps to balance issuing of load and
30111 store instructions */
30113 static int load_store_pendulum;
30115 /* The following variable helps pair divide insns during scheduling. */
30116 static int divide_cnt;
30117 /* The following variable helps pair and alternate vector and vector load
30118 insns during scheduling. */
30119 static int vec_pairing;
30122 /* Power4 load update and store update instructions are cracked into a
30123 load or store and an integer insn which are executed in the same cycle.
30124 Branches have their own dispatch slot which does not count against the
30125 GCC issue rate, but it changes the program flow so there are no other
30126 instructions to issue in this cycle. */
30128 static int
30129 rs6000_variable_issue_1 (rtx_insn *insn, int more)
30131 last_scheduled_insn = insn;
30132 if (GET_CODE (PATTERN (insn)) == USE
30133 || GET_CODE (PATTERN (insn)) == CLOBBER)
30135 cached_can_issue_more = more;
30136 return cached_can_issue_more;
30139 if (insn_terminates_group_p (insn, current_group))
30141 cached_can_issue_more = 0;
30142 return cached_can_issue_more;
30145 /* If no reservation, but reach here */
30146 if (recog_memoized (insn) < 0)
30147 return more;
30149 if (rs6000_sched_groups)
30151 if (is_microcoded_insn (insn))
30152 cached_can_issue_more = 0;
30153 else if (is_cracked_insn (insn))
30154 cached_can_issue_more = more > 2 ? more - 2 : 0;
30155 else
30156 cached_can_issue_more = more - 1;
30158 return cached_can_issue_more;
30161 if (rs6000_tune == PROCESSOR_CELL && is_nonpipeline_insn (insn))
30162 return 0;
30164 cached_can_issue_more = more - 1;
30165 return cached_can_issue_more;
30168 static int
30169 rs6000_variable_issue (FILE *stream, int verbose, rtx_insn *insn, int more)
30171 int r = rs6000_variable_issue_1 (insn, more);
30172 if (verbose)
30173 fprintf (stream, "// rs6000_variable_issue (more = %d) = %d\n", more, r);
30174 return r;
30177 /* Adjust the cost of a scheduling dependency. Return the new cost of
30178 a dependency LINK or INSN on DEP_INSN. COST is the current cost. */
30180 static int
30181 rs6000_adjust_cost (rtx_insn *insn, int dep_type, rtx_insn *dep_insn, int cost,
30182 unsigned int)
30184 enum attr_type attr_type;
30186 if (recog_memoized (insn) < 0 || recog_memoized (dep_insn) < 0)
30187 return cost;
30189 switch (dep_type)
30191 case REG_DEP_TRUE:
30193 /* Data dependency; DEP_INSN writes a register that INSN reads
30194 some cycles later. */
30196 /* Separate a load from a narrower, dependent store. */
30197 if ((rs6000_sched_groups || rs6000_tune == PROCESSOR_POWER9)
30198 && GET_CODE (PATTERN (insn)) == SET
30199 && GET_CODE (PATTERN (dep_insn)) == SET
30200 && MEM_P (XEXP (PATTERN (insn), 1))
30201 && MEM_P (XEXP (PATTERN (dep_insn), 0))
30202 && (GET_MODE_SIZE (GET_MODE (XEXP (PATTERN (insn), 1)))
30203 > GET_MODE_SIZE (GET_MODE (XEXP (PATTERN (dep_insn), 0)))))
30204 return cost + 14;
30206 attr_type = get_attr_type (insn);
30208 switch (attr_type)
30210 case TYPE_JMPREG:
30211 /* Tell the first scheduling pass about the latency between
30212 a mtctr and bctr (and mtlr and br/blr). The first
30213 scheduling pass will not know about this latency since
30214 the mtctr instruction, which has the latency associated
30215 to it, will be generated by reload. */
30216 return 4;
30217 case TYPE_BRANCH:
30218 /* Leave some extra cycles between a compare and its
30219 dependent branch, to inhibit expensive mispredicts. */
30220 if ((rs6000_tune == PROCESSOR_PPC603
30221 || rs6000_tune == PROCESSOR_PPC604
30222 || rs6000_tune == PROCESSOR_PPC604e
30223 || rs6000_tune == PROCESSOR_PPC620
30224 || rs6000_tune == PROCESSOR_PPC630
30225 || rs6000_tune == PROCESSOR_PPC750
30226 || rs6000_tune == PROCESSOR_PPC7400
30227 || rs6000_tune == PROCESSOR_PPC7450
30228 || rs6000_tune == PROCESSOR_PPCE5500
30229 || rs6000_tune == PROCESSOR_PPCE6500
30230 || rs6000_tune == PROCESSOR_POWER4
30231 || rs6000_tune == PROCESSOR_POWER5
30232 || rs6000_tune == PROCESSOR_POWER7
30233 || rs6000_tune == PROCESSOR_POWER8
30234 || rs6000_tune == PROCESSOR_POWER9
30235 || rs6000_tune == PROCESSOR_CELL)
30236 && recog_memoized (dep_insn)
30237 && (INSN_CODE (dep_insn) >= 0))
30239 switch (get_attr_type (dep_insn))
30241 case TYPE_CMP:
30242 case TYPE_FPCOMPARE:
30243 case TYPE_CR_LOGICAL:
30244 return cost + 2;
30245 case TYPE_EXTS:
30246 case TYPE_MUL:
30247 if (get_attr_dot (dep_insn) == DOT_YES)
30248 return cost + 2;
30249 else
30250 break;
30251 case TYPE_SHIFT:
30252 if (get_attr_dot (dep_insn) == DOT_YES
30253 && get_attr_var_shift (dep_insn) == VAR_SHIFT_NO)
30254 return cost + 2;
30255 else
30256 break;
30257 default:
30258 break;
30260 break;
30262 case TYPE_STORE:
30263 case TYPE_FPSTORE:
30264 if ((rs6000_tune == PROCESSOR_POWER6)
30265 && recog_memoized (dep_insn)
30266 && (INSN_CODE (dep_insn) >= 0))
30269 if (GET_CODE (PATTERN (insn)) != SET)
30270 /* If this happens, we have to extend this to schedule
30271 optimally. Return default for now. */
30272 return cost;
30274 /* Adjust the cost for the case where the value written
30275 by a fixed point operation is used as the address
30276 gen value on a store. */
30277 switch (get_attr_type (dep_insn))
30279 case TYPE_LOAD:
30280 case TYPE_CNTLZ:
30282 if (! rs6000_store_data_bypass_p (dep_insn, insn))
30283 return get_attr_sign_extend (dep_insn)
30284 == SIGN_EXTEND_YES ? 6 : 4;
30285 break;
30287 case TYPE_SHIFT:
30289 if (! rs6000_store_data_bypass_p (dep_insn, insn))
30290 return get_attr_var_shift (dep_insn) == VAR_SHIFT_YES ?
30291 6 : 3;
30292 break;
30294 case TYPE_INTEGER:
30295 case TYPE_ADD:
30296 case TYPE_LOGICAL:
30297 case TYPE_EXTS:
30298 case TYPE_INSERT:
30300 if (! rs6000_store_data_bypass_p (dep_insn, insn))
30301 return 3;
30302 break;
30304 case TYPE_STORE:
30305 case TYPE_FPLOAD:
30306 case TYPE_FPSTORE:
30308 if (get_attr_update (dep_insn) == UPDATE_YES
30309 && ! rs6000_store_data_bypass_p (dep_insn, insn))
30310 return 3;
30311 break;
30313 case TYPE_MUL:
30315 if (! rs6000_store_data_bypass_p (dep_insn, insn))
30316 return 17;
30317 break;
30319 case TYPE_DIV:
30321 if (! rs6000_store_data_bypass_p (dep_insn, insn))
30322 return get_attr_size (dep_insn) == SIZE_32 ? 45 : 57;
30323 break;
30325 default:
30326 break;
30329 break;
30331 case TYPE_LOAD:
30332 if ((rs6000_tune == PROCESSOR_POWER6)
30333 && recog_memoized (dep_insn)
30334 && (INSN_CODE (dep_insn) >= 0))
30337 /* Adjust the cost for the case where the value written
30338 by a fixed point instruction is used within the address
30339 gen portion of a subsequent load(u)(x) */
30340 switch (get_attr_type (dep_insn))
30342 case TYPE_LOAD:
30343 case TYPE_CNTLZ:
30345 if (set_to_load_agen (dep_insn, insn))
30346 return get_attr_sign_extend (dep_insn)
30347 == SIGN_EXTEND_YES ? 6 : 4;
30348 break;
30350 case TYPE_SHIFT:
30352 if (set_to_load_agen (dep_insn, insn))
30353 return get_attr_var_shift (dep_insn) == VAR_SHIFT_YES ?
30354 6 : 3;
30355 break;
30357 case TYPE_INTEGER:
30358 case TYPE_ADD:
30359 case TYPE_LOGICAL:
30360 case TYPE_EXTS:
30361 case TYPE_INSERT:
30363 if (set_to_load_agen (dep_insn, insn))
30364 return 3;
30365 break;
30367 case TYPE_STORE:
30368 case TYPE_FPLOAD:
30369 case TYPE_FPSTORE:
30371 if (get_attr_update (dep_insn) == UPDATE_YES
30372 && set_to_load_agen (dep_insn, insn))
30373 return 3;
30374 break;
30376 case TYPE_MUL:
30378 if (set_to_load_agen (dep_insn, insn))
30379 return 17;
30380 break;
30382 case TYPE_DIV:
30384 if (set_to_load_agen (dep_insn, insn))
30385 return get_attr_size (dep_insn) == SIZE_32 ? 45 : 57;
30386 break;
30388 default:
30389 break;
30392 break;
30394 case TYPE_FPLOAD:
30395 if ((rs6000_tune == PROCESSOR_POWER6)
30396 && get_attr_update (insn) == UPDATE_NO
30397 && recog_memoized (dep_insn)
30398 && (INSN_CODE (dep_insn) >= 0)
30399 && (get_attr_type (dep_insn) == TYPE_MFFGPR))
30400 return 2;
30402 default:
30403 break;
30406 /* Fall out to return default cost. */
30408 break;
30410 case REG_DEP_OUTPUT:
30411 /* Output dependency; DEP_INSN writes a register that INSN writes some
30412 cycles later. */
30413 if ((rs6000_tune == PROCESSOR_POWER6)
30414 && recog_memoized (dep_insn)
30415 && (INSN_CODE (dep_insn) >= 0))
30417 attr_type = get_attr_type (insn);
30419 switch (attr_type)
30421 case TYPE_FP:
30422 case TYPE_FPSIMPLE:
30423 if (get_attr_type (dep_insn) == TYPE_FP
30424 || get_attr_type (dep_insn) == TYPE_FPSIMPLE)
30425 return 1;
30426 break;
30427 case TYPE_FPLOAD:
30428 if (get_attr_update (insn) == UPDATE_NO
30429 && get_attr_type (dep_insn) == TYPE_MFFGPR)
30430 return 2;
30431 break;
30432 default:
30433 break;
30436 /* Fall through, no cost for output dependency. */
30437 /* FALLTHRU */
30439 case REG_DEP_ANTI:
30440 /* Anti dependency; DEP_INSN reads a register that INSN writes some
30441 cycles later. */
30442 return 0;
30444 default:
30445 gcc_unreachable ();
30448 return cost;
30451 /* Debug version of rs6000_adjust_cost. */
30453 static int
30454 rs6000_debug_adjust_cost (rtx_insn *insn, int dep_type, rtx_insn *dep_insn,
30455 int cost, unsigned int dw)
30457 int ret = rs6000_adjust_cost (insn, dep_type, dep_insn, cost, dw);
30459 if (ret != cost)
30461 const char *dep;
30463 switch (dep_type)
30465 default: dep = "unknown depencency"; break;
30466 case REG_DEP_TRUE: dep = "data dependency"; break;
30467 case REG_DEP_OUTPUT: dep = "output dependency"; break;
30468 case REG_DEP_ANTI: dep = "anti depencency"; break;
30471 fprintf (stderr,
30472 "\nrs6000_adjust_cost, final cost = %d, orig cost = %d, "
30473 "%s, insn:\n", ret, cost, dep);
30475 debug_rtx (insn);
30478 return ret;
30481 /* The function returns a true if INSN is microcoded.
30482 Return false otherwise. */
30484 static bool
30485 is_microcoded_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_tune == PROCESSOR_CELL)
30493 return get_attr_cell_micro (insn) == CELL_MICRO_ALWAYS;
30495 if (rs6000_sched_groups
30496 && (rs6000_tune == PROCESSOR_POWER4 || rs6000_tune == PROCESSOR_POWER5))
30498 enum attr_type type = get_attr_type (insn);
30499 if ((type == TYPE_LOAD
30500 && get_attr_update (insn) == UPDATE_YES
30501 && get_attr_sign_extend (insn) == SIGN_EXTEND_YES)
30502 || ((type == TYPE_LOAD || type == TYPE_STORE)
30503 && get_attr_update (insn) == UPDATE_YES
30504 && get_attr_indexed (insn) == INDEXED_YES)
30505 || type == TYPE_MFCR)
30506 return true;
30509 return false;
30512 /* The function returns true if INSN is cracked into 2 instructions
30513 by the processor (and therefore occupies 2 issue slots). */
30515 static bool
30516 is_cracked_insn (rtx_insn *insn)
30518 if (!insn || !NONDEBUG_INSN_P (insn)
30519 || GET_CODE (PATTERN (insn)) == USE
30520 || GET_CODE (PATTERN (insn)) == CLOBBER)
30521 return false;
30523 if (rs6000_sched_groups
30524 && (rs6000_tune == PROCESSOR_POWER4 || rs6000_tune == PROCESSOR_POWER5))
30526 enum attr_type type = get_attr_type (insn);
30527 if ((type == TYPE_LOAD
30528 && get_attr_sign_extend (insn) == SIGN_EXTEND_YES
30529 && get_attr_update (insn) == UPDATE_NO)
30530 || (type == TYPE_LOAD
30531 && get_attr_sign_extend (insn) == SIGN_EXTEND_NO
30532 && get_attr_update (insn) == UPDATE_YES
30533 && get_attr_indexed (insn) == INDEXED_NO)
30534 || (type == TYPE_STORE
30535 && get_attr_update (insn) == UPDATE_YES
30536 && get_attr_indexed (insn) == INDEXED_NO)
30537 || ((type == TYPE_FPLOAD || type == TYPE_FPSTORE)
30538 && get_attr_update (insn) == UPDATE_YES)
30539 || (type == TYPE_CR_LOGICAL
30540 && get_attr_cr_logical_3op (insn) == CR_LOGICAL_3OP_YES)
30541 || (type == TYPE_EXTS
30542 && get_attr_dot (insn) == DOT_YES)
30543 || (type == TYPE_SHIFT
30544 && get_attr_dot (insn) == DOT_YES
30545 && get_attr_var_shift (insn) == VAR_SHIFT_NO)
30546 || (type == TYPE_MUL
30547 && get_attr_dot (insn) == DOT_YES)
30548 || type == TYPE_DIV
30549 || (type == TYPE_INSERT
30550 && get_attr_size (insn) == SIZE_32))
30551 return true;
30554 return false;
30557 /* The function returns true if INSN can be issued only from
30558 the branch slot. */
30560 static bool
30561 is_branch_slot_insn (rtx_insn *insn)
30563 if (!insn || !NONDEBUG_INSN_P (insn)
30564 || GET_CODE (PATTERN (insn)) == USE
30565 || GET_CODE (PATTERN (insn)) == CLOBBER)
30566 return false;
30568 if (rs6000_sched_groups)
30570 enum attr_type type = get_attr_type (insn);
30571 if (type == TYPE_BRANCH || type == TYPE_JMPREG)
30572 return true;
30573 return false;
30576 return false;
30579 /* The function returns true if out_inst sets a value that is
30580 used in the address generation computation of in_insn */
30581 static bool
30582 set_to_load_agen (rtx_insn *out_insn, rtx_insn *in_insn)
30584 rtx out_set, in_set;
30586 /* For performance reasons, only handle the simple case where
30587 both loads are a single_set. */
30588 out_set = single_set (out_insn);
30589 if (out_set)
30591 in_set = single_set (in_insn);
30592 if (in_set)
30593 return reg_mentioned_p (SET_DEST (out_set), SET_SRC (in_set));
30596 return false;
30599 /* Try to determine base/offset/size parts of the given MEM.
30600 Return true if successful, false if all the values couldn't
30601 be determined.
30603 This function only looks for REG or REG+CONST address forms.
30604 REG+REG address form will return false. */
30606 static bool
30607 get_memref_parts (rtx mem, rtx *base, HOST_WIDE_INT *offset,
30608 HOST_WIDE_INT *size)
30610 rtx addr_rtx;
30611 if MEM_SIZE_KNOWN_P (mem)
30612 *size = MEM_SIZE (mem);
30613 else
30614 return false;
30616 addr_rtx = (XEXP (mem, 0));
30617 if (GET_CODE (addr_rtx) == PRE_MODIFY)
30618 addr_rtx = XEXP (addr_rtx, 1);
30620 *offset = 0;
30621 while (GET_CODE (addr_rtx) == PLUS
30622 && CONST_INT_P (XEXP (addr_rtx, 1)))
30624 *offset += INTVAL (XEXP (addr_rtx, 1));
30625 addr_rtx = XEXP (addr_rtx, 0);
30627 if (!REG_P (addr_rtx))
30628 return false;
30630 *base = addr_rtx;
30631 return true;
30634 /* The function returns true if the target storage location of
30635 mem1 is adjacent to the target storage location of mem2 */
30636 /* Return 1 if memory locations are adjacent. */
30638 static bool
30639 adjacent_mem_locations (rtx mem1, rtx mem2)
30641 rtx reg1, reg2;
30642 HOST_WIDE_INT off1, size1, off2, size2;
30644 if (get_memref_parts (mem1, &reg1, &off1, &size1)
30645 && get_memref_parts (mem2, &reg2, &off2, &size2))
30646 return ((REGNO (reg1) == REGNO (reg2))
30647 && ((off1 + size1 == off2)
30648 || (off2 + size2 == off1)));
30650 return false;
30653 /* This function returns true if it can be determined that the two MEM
30654 locations overlap by at least 1 byte based on base reg/offset/size. */
30656 static bool
30657 mem_locations_overlap (rtx mem1, rtx mem2)
30659 rtx reg1, reg2;
30660 HOST_WIDE_INT off1, size1, off2, size2;
30662 if (get_memref_parts (mem1, &reg1, &off1, &size1)
30663 && get_memref_parts (mem2, &reg2, &off2, &size2))
30664 return ((REGNO (reg1) == REGNO (reg2))
30665 && (((off1 <= off2) && (off1 + size1 > off2))
30666 || ((off2 <= off1) && (off2 + size2 > off1))));
30668 return false;
30671 /* A C statement (sans semicolon) to update the integer scheduling
30672 priority INSN_PRIORITY (INSN). Increase the priority to execute the
30673 INSN earlier, reduce the priority to execute INSN later. Do not
30674 define this macro if you do not need to adjust the scheduling
30675 priorities of insns. */
30677 static int
30678 rs6000_adjust_priority (rtx_insn *insn ATTRIBUTE_UNUSED, int priority)
30680 rtx load_mem, str_mem;
30681 /* On machines (like the 750) which have asymmetric integer units,
30682 where one integer unit can do multiply and divides and the other
30683 can't, reduce the priority of multiply/divide so it is scheduled
30684 before other integer operations. */
30686 #if 0
30687 if (! INSN_P (insn))
30688 return priority;
30690 if (GET_CODE (PATTERN (insn)) == USE)
30691 return priority;
30693 switch (rs6000_tune) {
30694 case PROCESSOR_PPC750:
30695 switch (get_attr_type (insn))
30697 default:
30698 break;
30700 case TYPE_MUL:
30701 case TYPE_DIV:
30702 fprintf (stderr, "priority was %#x (%d) before adjustment\n",
30703 priority, priority);
30704 if (priority >= 0 && priority < 0x01000000)
30705 priority >>= 3;
30706 break;
30709 #endif
30711 if (insn_must_be_first_in_group (insn)
30712 && reload_completed
30713 && current_sched_info->sched_max_insns_priority
30714 && rs6000_sched_restricted_insns_priority)
30717 /* Prioritize insns that can be dispatched only in the first
30718 dispatch slot. */
30719 if (rs6000_sched_restricted_insns_priority == 1)
30720 /* Attach highest priority to insn. This means that in
30721 haifa-sched.c:ready_sort(), dispatch-slot restriction considerations
30722 precede 'priority' (critical path) considerations. */
30723 return current_sched_info->sched_max_insns_priority;
30724 else if (rs6000_sched_restricted_insns_priority == 2)
30725 /* Increase priority of insn by a minimal amount. This means that in
30726 haifa-sched.c:ready_sort(), only 'priority' (critical path)
30727 considerations precede dispatch-slot restriction considerations. */
30728 return (priority + 1);
30731 if (rs6000_tune == PROCESSOR_POWER6
30732 && ((load_store_pendulum == -2 && is_load_insn (insn, &load_mem))
30733 || (load_store_pendulum == 2 && is_store_insn (insn, &str_mem))))
30734 /* Attach highest priority to insn if the scheduler has just issued two
30735 stores and this instruction is a load, or two loads and this instruction
30736 is a store. Power6 wants loads and stores scheduled alternately
30737 when possible */
30738 return current_sched_info->sched_max_insns_priority;
30740 return priority;
30743 /* Return true if the instruction is nonpipelined on the Cell. */
30744 static bool
30745 is_nonpipeline_insn (rtx_insn *insn)
30747 enum attr_type type;
30748 if (!insn || !NONDEBUG_INSN_P (insn)
30749 || GET_CODE (PATTERN (insn)) == USE
30750 || GET_CODE (PATTERN (insn)) == CLOBBER)
30751 return false;
30753 type = get_attr_type (insn);
30754 if (type == TYPE_MUL
30755 || type == TYPE_DIV
30756 || type == TYPE_SDIV
30757 || type == TYPE_DDIV
30758 || type == TYPE_SSQRT
30759 || type == TYPE_DSQRT
30760 || type == TYPE_MFCR
30761 || type == TYPE_MFCRF
30762 || type == TYPE_MFJMPR)
30764 return true;
30766 return false;
30770 /* Return how many instructions the machine can issue per cycle. */
30772 static int
30773 rs6000_issue_rate (void)
30775 /* Unless scheduling for register pressure, use issue rate of 1 for
30776 first scheduling pass to decrease degradation. */
30777 if (!reload_completed && !flag_sched_pressure)
30778 return 1;
30780 switch (rs6000_tune) {
30781 case PROCESSOR_RS64A:
30782 case PROCESSOR_PPC601: /* ? */
30783 case PROCESSOR_PPC7450:
30784 return 3;
30785 case PROCESSOR_PPC440:
30786 case PROCESSOR_PPC603:
30787 case PROCESSOR_PPC750:
30788 case PROCESSOR_PPC7400:
30789 case PROCESSOR_PPC8540:
30790 case PROCESSOR_PPC8548:
30791 case PROCESSOR_CELL:
30792 case PROCESSOR_PPCE300C2:
30793 case PROCESSOR_PPCE300C3:
30794 case PROCESSOR_PPCE500MC:
30795 case PROCESSOR_PPCE500MC64:
30796 case PROCESSOR_PPCE5500:
30797 case PROCESSOR_PPCE6500:
30798 case PROCESSOR_TITAN:
30799 return 2;
30800 case PROCESSOR_PPC476:
30801 case PROCESSOR_PPC604:
30802 case PROCESSOR_PPC604e:
30803 case PROCESSOR_PPC620:
30804 case PROCESSOR_PPC630:
30805 return 4;
30806 case PROCESSOR_POWER4:
30807 case PROCESSOR_POWER5:
30808 case PROCESSOR_POWER6:
30809 case PROCESSOR_POWER7:
30810 return 5;
30811 case PROCESSOR_POWER8:
30812 return 7;
30813 case PROCESSOR_POWER9:
30814 return 6;
30815 default:
30816 return 1;
30820 /* Return how many instructions to look ahead for better insn
30821 scheduling. */
30823 static int
30824 rs6000_use_sched_lookahead (void)
30826 switch (rs6000_tune)
30828 case PROCESSOR_PPC8540:
30829 case PROCESSOR_PPC8548:
30830 return 4;
30832 case PROCESSOR_CELL:
30833 return (reload_completed ? 8 : 0);
30835 default:
30836 return 0;
30840 /* We are choosing insn from the ready queue. Return zero if INSN can be
30841 chosen. */
30842 static int
30843 rs6000_use_sched_lookahead_guard (rtx_insn *insn, int ready_index)
30845 if (ready_index == 0)
30846 return 0;
30848 if (rs6000_tune != PROCESSOR_CELL)
30849 return 0;
30851 gcc_assert (insn != NULL_RTX && INSN_P (insn));
30853 if (!reload_completed
30854 || is_nonpipeline_insn (insn)
30855 || is_microcoded_insn (insn))
30856 return 1;
30858 return 0;
30861 /* Determine if PAT refers to memory. If so, set MEM_REF to the MEM rtx
30862 and return true. */
30864 static bool
30865 find_mem_ref (rtx pat, rtx *mem_ref)
30867 const char * fmt;
30868 int i, j;
30870 /* stack_tie does not produce any real memory traffic. */
30871 if (tie_operand (pat, VOIDmode))
30872 return false;
30874 if (MEM_P (pat))
30876 *mem_ref = pat;
30877 return true;
30880 /* Recursively process the pattern. */
30881 fmt = GET_RTX_FORMAT (GET_CODE (pat));
30883 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
30885 if (fmt[i] == 'e')
30887 if (find_mem_ref (XEXP (pat, i), mem_ref))
30888 return true;
30890 else if (fmt[i] == 'E')
30891 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
30893 if (find_mem_ref (XVECEXP (pat, i, j), mem_ref))
30894 return true;
30898 return false;
30901 /* Determine if PAT is a PATTERN of a load insn. */
30903 static bool
30904 is_load_insn1 (rtx pat, rtx *load_mem)
30906 if (!pat || pat == NULL_RTX)
30907 return false;
30909 if (GET_CODE (pat) == SET)
30910 return find_mem_ref (SET_SRC (pat), load_mem);
30912 if (GET_CODE (pat) == PARALLEL)
30914 int i;
30916 for (i = 0; i < XVECLEN (pat, 0); i++)
30917 if (is_load_insn1 (XVECEXP (pat, 0, i), load_mem))
30918 return true;
30921 return false;
30924 /* Determine if INSN loads from memory. */
30926 static bool
30927 is_load_insn (rtx insn, rtx *load_mem)
30929 if (!insn || !INSN_P (insn))
30930 return false;
30932 if (CALL_P (insn))
30933 return false;
30935 return is_load_insn1 (PATTERN (insn), load_mem);
30938 /* Determine if PAT is a PATTERN of a store insn. */
30940 static bool
30941 is_store_insn1 (rtx pat, rtx *str_mem)
30943 if (!pat || pat == NULL_RTX)
30944 return false;
30946 if (GET_CODE (pat) == SET)
30947 return find_mem_ref (SET_DEST (pat), str_mem);
30949 if (GET_CODE (pat) == PARALLEL)
30951 int i;
30953 for (i = 0; i < XVECLEN (pat, 0); i++)
30954 if (is_store_insn1 (XVECEXP (pat, 0, i), str_mem))
30955 return true;
30958 return false;
30961 /* Determine if INSN stores to memory. */
30963 static bool
30964 is_store_insn (rtx insn, rtx *str_mem)
30966 if (!insn || !INSN_P (insn))
30967 return false;
30969 return is_store_insn1 (PATTERN (insn), str_mem);
30972 /* Return whether TYPE is a Power9 pairable vector instruction type. */
30974 static bool
30975 is_power9_pairable_vec_type (enum attr_type type)
30977 switch (type)
30979 case TYPE_VECSIMPLE:
30980 case TYPE_VECCOMPLEX:
30981 case TYPE_VECDIV:
30982 case TYPE_VECCMP:
30983 case TYPE_VECPERM:
30984 case TYPE_VECFLOAT:
30985 case TYPE_VECFDIV:
30986 case TYPE_VECDOUBLE:
30987 return true;
30988 default:
30989 break;
30991 return false;
30994 /* Returns whether the dependence between INSN and NEXT is considered
30995 costly by the given target. */
30997 static bool
30998 rs6000_is_costly_dependence (dep_t dep, int cost, int distance)
31000 rtx insn;
31001 rtx next;
31002 rtx load_mem, str_mem;
31004 /* If the flag is not enabled - no dependence is considered costly;
31005 allow all dependent insns in the same group.
31006 This is the most aggressive option. */
31007 if (rs6000_sched_costly_dep == no_dep_costly)
31008 return false;
31010 /* If the flag is set to 1 - a dependence is always considered costly;
31011 do not allow dependent instructions in the same group.
31012 This is the most conservative option. */
31013 if (rs6000_sched_costly_dep == all_deps_costly)
31014 return true;
31016 insn = DEP_PRO (dep);
31017 next = DEP_CON (dep);
31019 if (rs6000_sched_costly_dep == store_to_load_dep_costly
31020 && is_load_insn (next, &load_mem)
31021 && is_store_insn (insn, &str_mem))
31022 /* Prevent load after store in the same group. */
31023 return true;
31025 if (rs6000_sched_costly_dep == true_store_to_load_dep_costly
31026 && is_load_insn (next, &load_mem)
31027 && is_store_insn (insn, &str_mem)
31028 && DEP_TYPE (dep) == REG_DEP_TRUE
31029 && mem_locations_overlap(str_mem, load_mem))
31030 /* Prevent load after store in the same group if it is a true
31031 dependence. */
31032 return true;
31034 /* The flag is set to X; dependences with latency >= X are considered costly,
31035 and will not be scheduled in the same group. */
31036 if (rs6000_sched_costly_dep <= max_dep_latency
31037 && ((cost - distance) >= (int)rs6000_sched_costly_dep))
31038 return true;
31040 return false;
31043 /* Return the next insn after INSN that is found before TAIL is reached,
31044 skipping any "non-active" insns - insns that will not actually occupy
31045 an issue slot. Return NULL_RTX if such an insn is not found. */
31047 static rtx_insn *
31048 get_next_active_insn (rtx_insn *insn, rtx_insn *tail)
31050 if (insn == NULL_RTX || insn == tail)
31051 return NULL;
31053 while (1)
31055 insn = NEXT_INSN (insn);
31056 if (insn == NULL_RTX || insn == tail)
31057 return NULL;
31059 if (CALL_P (insn)
31060 || JUMP_P (insn) || JUMP_TABLE_DATA_P (insn)
31061 || (NONJUMP_INSN_P (insn)
31062 && GET_CODE (PATTERN (insn)) != USE
31063 && GET_CODE (PATTERN (insn)) != CLOBBER
31064 && INSN_CODE (insn) != CODE_FOR_stack_tie))
31065 break;
31067 return insn;
31070 /* Do Power9 specific sched_reorder2 reordering of ready list. */
31072 static int
31073 power9_sched_reorder2 (rtx_insn **ready, int lastpos)
31075 int pos;
31076 int i;
31077 rtx_insn *tmp;
31078 enum attr_type type, type2;
31080 type = get_attr_type (last_scheduled_insn);
31082 /* Try to issue fixed point divides back-to-back in pairs so they will be
31083 routed to separate execution units and execute in parallel. */
31084 if (type == TYPE_DIV && divide_cnt == 0)
31086 /* First divide has been scheduled. */
31087 divide_cnt = 1;
31089 /* Scan the ready list looking for another divide, if found move it
31090 to the end of the list so it is chosen next. */
31091 pos = lastpos;
31092 while (pos >= 0)
31094 if (recog_memoized (ready[pos]) >= 0
31095 && get_attr_type (ready[pos]) == TYPE_DIV)
31097 tmp = ready[pos];
31098 for (i = pos; i < lastpos; i++)
31099 ready[i] = ready[i + 1];
31100 ready[lastpos] = tmp;
31101 break;
31103 pos--;
31106 else
31108 /* Last insn was the 2nd divide or not a divide, reset the counter. */
31109 divide_cnt = 0;
31111 /* The best dispatch throughput for vector and vector load insns can be
31112 achieved by interleaving a vector and vector load such that they'll
31113 dispatch to the same superslice. If this pairing cannot be achieved
31114 then it is best to pair vector insns together and vector load insns
31115 together.
31117 To aid in this pairing, vec_pairing maintains the current state with
31118 the following values:
31120 0 : Initial state, no vecload/vector pairing has been started.
31122 1 : A vecload or vector insn has been issued and a candidate for
31123 pairing has been found and moved to the end of the ready
31124 list. */
31125 if (type == TYPE_VECLOAD)
31127 /* Issued a vecload. */
31128 if (vec_pairing == 0)
31130 int vecload_pos = -1;
31131 /* We issued a single vecload, look for a vector insn to pair it
31132 with. If one isn't found, try to pair another vecload. */
31133 pos = lastpos;
31134 while (pos >= 0)
31136 if (recog_memoized (ready[pos]) >= 0)
31138 type2 = get_attr_type (ready[pos]);
31139 if (is_power9_pairable_vec_type (type2))
31141 /* Found a vector insn to pair with, move it to the
31142 end of the ready list so it is scheduled next. */
31143 tmp = ready[pos];
31144 for (i = pos; i < lastpos; i++)
31145 ready[i] = ready[i + 1];
31146 ready[lastpos] = tmp;
31147 vec_pairing = 1;
31148 return cached_can_issue_more;
31150 else if (type2 == TYPE_VECLOAD && vecload_pos == -1)
31151 /* Remember position of first vecload seen. */
31152 vecload_pos = pos;
31154 pos--;
31156 if (vecload_pos >= 0)
31158 /* Didn't find a vector to pair with but did find a vecload,
31159 move it to the end of the ready list. */
31160 tmp = ready[vecload_pos];
31161 for (i = vecload_pos; i < lastpos; i++)
31162 ready[i] = ready[i + 1];
31163 ready[lastpos] = tmp;
31164 vec_pairing = 1;
31165 return cached_can_issue_more;
31169 else if (is_power9_pairable_vec_type (type))
31171 /* Issued a vector operation. */
31172 if (vec_pairing == 0)
31174 int vec_pos = -1;
31175 /* We issued a single vector insn, look for a vecload to pair it
31176 with. If one isn't found, try to pair another vector. */
31177 pos = lastpos;
31178 while (pos >= 0)
31180 if (recog_memoized (ready[pos]) >= 0)
31182 type2 = get_attr_type (ready[pos]);
31183 if (type2 == TYPE_VECLOAD)
31185 /* Found a vecload insn to pair with, move it to the
31186 end of the ready list so it is scheduled next. */
31187 tmp = ready[pos];
31188 for (i = pos; i < lastpos; i++)
31189 ready[i] = ready[i + 1];
31190 ready[lastpos] = tmp;
31191 vec_pairing = 1;
31192 return cached_can_issue_more;
31194 else if (is_power9_pairable_vec_type (type2)
31195 && vec_pos == -1)
31196 /* Remember position of first vector insn seen. */
31197 vec_pos = pos;
31199 pos--;
31201 if (vec_pos >= 0)
31203 /* Didn't find a vecload to pair with but did find a vector
31204 insn, move it to the end of the ready list. */
31205 tmp = ready[vec_pos];
31206 for (i = vec_pos; i < lastpos; i++)
31207 ready[i] = ready[i + 1];
31208 ready[lastpos] = tmp;
31209 vec_pairing = 1;
31210 return cached_can_issue_more;
31215 /* We've either finished a vec/vecload pair, couldn't find an insn to
31216 continue the current pair, or the last insn had nothing to do with
31217 with pairing. In any case, reset the state. */
31218 vec_pairing = 0;
31221 return cached_can_issue_more;
31224 /* We are about to begin issuing insns for this clock cycle. */
31226 static int
31227 rs6000_sched_reorder (FILE *dump ATTRIBUTE_UNUSED, int sched_verbose,
31228 rtx_insn **ready ATTRIBUTE_UNUSED,
31229 int *pn_ready ATTRIBUTE_UNUSED,
31230 int clock_var ATTRIBUTE_UNUSED)
31232 int n_ready = *pn_ready;
31234 if (sched_verbose)
31235 fprintf (dump, "// rs6000_sched_reorder :\n");
31237 /* Reorder the ready list, if the second to last ready insn
31238 is a nonepipeline insn. */
31239 if (rs6000_tune == PROCESSOR_CELL && n_ready > 1)
31241 if (is_nonpipeline_insn (ready[n_ready - 1])
31242 && (recog_memoized (ready[n_ready - 2]) > 0))
31243 /* Simply swap first two insns. */
31244 std::swap (ready[n_ready - 1], ready[n_ready - 2]);
31247 if (rs6000_tune == PROCESSOR_POWER6)
31248 load_store_pendulum = 0;
31250 return rs6000_issue_rate ();
31253 /* Like rs6000_sched_reorder, but called after issuing each insn. */
31255 static int
31256 rs6000_sched_reorder2 (FILE *dump, int sched_verbose, rtx_insn **ready,
31257 int *pn_ready, int clock_var ATTRIBUTE_UNUSED)
31259 if (sched_verbose)
31260 fprintf (dump, "// rs6000_sched_reorder2 :\n");
31262 /* For Power6, we need to handle some special cases to try and keep the
31263 store queue from overflowing and triggering expensive flushes.
31265 This code monitors how load and store instructions are being issued
31266 and skews the ready list one way or the other to increase the likelihood
31267 that a desired instruction is issued at the proper time.
31269 A couple of things are done. First, we maintain a "load_store_pendulum"
31270 to track the current state of load/store issue.
31272 - If the pendulum is at zero, then no loads or stores have been
31273 issued in the current cycle so we do nothing.
31275 - If the pendulum is 1, then a single load has been issued in this
31276 cycle and we attempt to locate another load in the ready list to
31277 issue with it.
31279 - If the pendulum is -2, then two stores have already been
31280 issued in this cycle, so we increase the priority of the first load
31281 in the ready list to increase it's likelihood of being chosen first
31282 in the next cycle.
31284 - If the pendulum is -1, then a single store has been issued in this
31285 cycle and we attempt to locate another store in the ready list to
31286 issue with it, preferring a store to an adjacent memory location to
31287 facilitate store pairing in the store queue.
31289 - If the pendulum is 2, then two loads have already been
31290 issued in this cycle, so we increase the priority of the first store
31291 in the ready list to increase it's likelihood of being chosen first
31292 in the next cycle.
31294 - If the pendulum < -2 or > 2, then do nothing.
31296 Note: This code covers the most common scenarios. There exist non
31297 load/store instructions which make use of the LSU and which
31298 would need to be accounted for to strictly model the behavior
31299 of the machine. Those instructions are currently unaccounted
31300 for to help minimize compile time overhead of this code.
31302 if (rs6000_tune == PROCESSOR_POWER6 && last_scheduled_insn)
31304 int pos;
31305 int i;
31306 rtx_insn *tmp;
31307 rtx load_mem, str_mem;
31309 if (is_store_insn (last_scheduled_insn, &str_mem))
31310 /* Issuing a store, swing the load_store_pendulum to the left */
31311 load_store_pendulum--;
31312 else if (is_load_insn (last_scheduled_insn, &load_mem))
31313 /* Issuing a load, swing the load_store_pendulum to the right */
31314 load_store_pendulum++;
31315 else
31316 return cached_can_issue_more;
31318 /* If the pendulum is balanced, or there is only one instruction on
31319 the ready list, then all is well, so return. */
31320 if ((load_store_pendulum == 0) || (*pn_ready <= 1))
31321 return cached_can_issue_more;
31323 if (load_store_pendulum == 1)
31325 /* A load has been issued in this cycle. Scan the ready list
31326 for another load to issue with it */
31327 pos = *pn_ready-1;
31329 while (pos >= 0)
31331 if (is_load_insn (ready[pos], &load_mem))
31333 /* Found a load. Move it to the head of the ready list,
31334 and adjust it's priority so that it is more likely to
31335 stay there */
31336 tmp = ready[pos];
31337 for (i=pos; i<*pn_ready-1; i++)
31338 ready[i] = ready[i + 1];
31339 ready[*pn_ready-1] = tmp;
31341 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
31342 INSN_PRIORITY (tmp)++;
31343 break;
31345 pos--;
31348 else if (load_store_pendulum == -2)
31350 /* Two stores have been issued in this cycle. Increase the
31351 priority of the first load in the ready list to favor it for
31352 issuing in the next cycle. */
31353 pos = *pn_ready-1;
31355 while (pos >= 0)
31357 if (is_load_insn (ready[pos], &load_mem)
31358 && !sel_sched_p ()
31359 && INSN_PRIORITY_KNOWN (ready[pos]))
31361 INSN_PRIORITY (ready[pos])++;
31363 /* Adjust the pendulum to account for the fact that a load
31364 was found and increased in priority. This is to prevent
31365 increasing the priority of multiple loads */
31366 load_store_pendulum--;
31368 break;
31370 pos--;
31373 else if (load_store_pendulum == -1)
31375 /* A store has been issued in this cycle. Scan the ready list for
31376 another store to issue with it, preferring a store to an adjacent
31377 memory location */
31378 int first_store_pos = -1;
31380 pos = *pn_ready-1;
31382 while (pos >= 0)
31384 if (is_store_insn (ready[pos], &str_mem))
31386 rtx str_mem2;
31387 /* Maintain the index of the first store found on the
31388 list */
31389 if (first_store_pos == -1)
31390 first_store_pos = pos;
31392 if (is_store_insn (last_scheduled_insn, &str_mem2)
31393 && adjacent_mem_locations (str_mem, str_mem2))
31395 /* Found an adjacent store. Move it to the head of the
31396 ready list, and adjust it's priority so that it is
31397 more likely to stay there */
31398 tmp = ready[pos];
31399 for (i=pos; i<*pn_ready-1; i++)
31400 ready[i] = ready[i + 1];
31401 ready[*pn_ready-1] = tmp;
31403 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
31404 INSN_PRIORITY (tmp)++;
31406 first_store_pos = -1;
31408 break;
31411 pos--;
31414 if (first_store_pos >= 0)
31416 /* An adjacent store wasn't found, but a non-adjacent store was,
31417 so move the non-adjacent store to the front of the ready
31418 list, and adjust its priority so that it is more likely to
31419 stay there. */
31420 tmp = ready[first_store_pos];
31421 for (i=first_store_pos; i<*pn_ready-1; i++)
31422 ready[i] = ready[i + 1];
31423 ready[*pn_ready-1] = tmp;
31424 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
31425 INSN_PRIORITY (tmp)++;
31428 else if (load_store_pendulum == 2)
31430 /* Two loads have been issued in this cycle. Increase the priority
31431 of the first store in the ready list to favor it for issuing in
31432 the next cycle. */
31433 pos = *pn_ready-1;
31435 while (pos >= 0)
31437 if (is_store_insn (ready[pos], &str_mem)
31438 && !sel_sched_p ()
31439 && INSN_PRIORITY_KNOWN (ready[pos]))
31441 INSN_PRIORITY (ready[pos])++;
31443 /* Adjust the pendulum to account for the fact that a store
31444 was found and increased in priority. This is to prevent
31445 increasing the priority of multiple stores */
31446 load_store_pendulum++;
31448 break;
31450 pos--;
31455 /* Do Power9 dependent reordering if necessary. */
31456 if (rs6000_tune == PROCESSOR_POWER9 && last_scheduled_insn
31457 && recog_memoized (last_scheduled_insn) >= 0)
31458 return power9_sched_reorder2 (ready, *pn_ready - 1);
31460 return cached_can_issue_more;
31463 /* Return whether the presence of INSN causes a dispatch group termination
31464 of group WHICH_GROUP.
31466 If WHICH_GROUP == current_group, this function will return true if INSN
31467 causes the termination of the current group (i.e, the dispatch group to
31468 which INSN belongs). This means that INSN will be the last insn in the
31469 group it belongs to.
31471 If WHICH_GROUP == previous_group, this function will return true if INSN
31472 causes the termination of the previous group (i.e, the dispatch group that
31473 precedes the group to which INSN belongs). This means that INSN will be
31474 the first insn in the group it belongs to). */
31476 static bool
31477 insn_terminates_group_p (rtx_insn *insn, enum group_termination which_group)
31479 bool first, last;
31481 if (! insn)
31482 return false;
31484 first = insn_must_be_first_in_group (insn);
31485 last = insn_must_be_last_in_group (insn);
31487 if (first && last)
31488 return true;
31490 if (which_group == current_group)
31491 return last;
31492 else if (which_group == previous_group)
31493 return first;
31495 return false;
31499 static bool
31500 insn_must_be_first_in_group (rtx_insn *insn)
31502 enum attr_type type;
31504 if (!insn
31505 || NOTE_P (insn)
31506 || DEBUG_INSN_P (insn)
31507 || GET_CODE (PATTERN (insn)) == USE
31508 || GET_CODE (PATTERN (insn)) == CLOBBER)
31509 return false;
31511 switch (rs6000_tune)
31513 case PROCESSOR_POWER5:
31514 if (is_cracked_insn (insn))
31515 return true;
31516 /* FALLTHRU */
31517 case PROCESSOR_POWER4:
31518 if (is_microcoded_insn (insn))
31519 return true;
31521 if (!rs6000_sched_groups)
31522 return false;
31524 type = get_attr_type (insn);
31526 switch (type)
31528 case TYPE_MFCR:
31529 case TYPE_MFCRF:
31530 case TYPE_MTCR:
31531 case TYPE_CR_LOGICAL:
31532 case TYPE_MTJMPR:
31533 case TYPE_MFJMPR:
31534 case TYPE_DIV:
31535 case TYPE_LOAD_L:
31536 case TYPE_STORE_C:
31537 case TYPE_ISYNC:
31538 case TYPE_SYNC:
31539 return true;
31540 default:
31541 break;
31543 break;
31544 case PROCESSOR_POWER6:
31545 type = get_attr_type (insn);
31547 switch (type)
31549 case TYPE_EXTS:
31550 case TYPE_CNTLZ:
31551 case TYPE_TRAP:
31552 case TYPE_MUL:
31553 case TYPE_INSERT:
31554 case TYPE_FPCOMPARE:
31555 case TYPE_MFCR:
31556 case TYPE_MTCR:
31557 case TYPE_MFJMPR:
31558 case TYPE_MTJMPR:
31559 case TYPE_ISYNC:
31560 case TYPE_SYNC:
31561 case TYPE_LOAD_L:
31562 case TYPE_STORE_C:
31563 return true;
31564 case TYPE_SHIFT:
31565 if (get_attr_dot (insn) == DOT_NO
31566 || get_attr_var_shift (insn) == VAR_SHIFT_NO)
31567 return true;
31568 else
31569 break;
31570 case TYPE_DIV:
31571 if (get_attr_size (insn) == SIZE_32)
31572 return true;
31573 else
31574 break;
31575 case TYPE_LOAD:
31576 case TYPE_STORE:
31577 case TYPE_FPLOAD:
31578 case TYPE_FPSTORE:
31579 if (get_attr_update (insn) == UPDATE_YES)
31580 return true;
31581 else
31582 break;
31583 default:
31584 break;
31586 break;
31587 case PROCESSOR_POWER7:
31588 type = get_attr_type (insn);
31590 switch (type)
31592 case TYPE_CR_LOGICAL:
31593 case TYPE_MFCR:
31594 case TYPE_MFCRF:
31595 case TYPE_MTCR:
31596 case TYPE_DIV:
31597 case TYPE_ISYNC:
31598 case TYPE_LOAD_L:
31599 case TYPE_STORE_C:
31600 case TYPE_MFJMPR:
31601 case TYPE_MTJMPR:
31602 return true;
31603 case TYPE_MUL:
31604 case TYPE_SHIFT:
31605 case TYPE_EXTS:
31606 if (get_attr_dot (insn) == DOT_YES)
31607 return true;
31608 else
31609 break;
31610 case TYPE_LOAD:
31611 if (get_attr_sign_extend (insn) == SIGN_EXTEND_YES
31612 || get_attr_update (insn) == UPDATE_YES)
31613 return true;
31614 else
31615 break;
31616 case TYPE_STORE:
31617 case TYPE_FPLOAD:
31618 case TYPE_FPSTORE:
31619 if (get_attr_update (insn) == UPDATE_YES)
31620 return true;
31621 else
31622 break;
31623 default:
31624 break;
31626 break;
31627 case PROCESSOR_POWER8:
31628 type = get_attr_type (insn);
31630 switch (type)
31632 case TYPE_CR_LOGICAL:
31633 case TYPE_MFCR:
31634 case TYPE_MFCRF:
31635 case TYPE_MTCR:
31636 case TYPE_SYNC:
31637 case TYPE_ISYNC:
31638 case TYPE_LOAD_L:
31639 case TYPE_STORE_C:
31640 case TYPE_VECSTORE:
31641 case TYPE_MFJMPR:
31642 case TYPE_MTJMPR:
31643 return true;
31644 case TYPE_SHIFT:
31645 case TYPE_EXTS:
31646 case TYPE_MUL:
31647 if (get_attr_dot (insn) == DOT_YES)
31648 return true;
31649 else
31650 break;
31651 case TYPE_LOAD:
31652 if (get_attr_sign_extend (insn) == SIGN_EXTEND_YES
31653 || get_attr_update (insn) == UPDATE_YES)
31654 return true;
31655 else
31656 break;
31657 case TYPE_STORE:
31658 if (get_attr_update (insn) == UPDATE_YES
31659 && get_attr_indexed (insn) == INDEXED_YES)
31660 return true;
31661 else
31662 break;
31663 default:
31664 break;
31666 break;
31667 default:
31668 break;
31671 return false;
31674 static bool
31675 insn_must_be_last_in_group (rtx_insn *insn)
31677 enum attr_type type;
31679 if (!insn
31680 || NOTE_P (insn)
31681 || DEBUG_INSN_P (insn)
31682 || GET_CODE (PATTERN (insn)) == USE
31683 || GET_CODE (PATTERN (insn)) == CLOBBER)
31684 return false;
31686 switch (rs6000_tune) {
31687 case PROCESSOR_POWER4:
31688 case PROCESSOR_POWER5:
31689 if (is_microcoded_insn (insn))
31690 return true;
31692 if (is_branch_slot_insn (insn))
31693 return true;
31695 break;
31696 case PROCESSOR_POWER6:
31697 type = get_attr_type (insn);
31699 switch (type)
31701 case TYPE_EXTS:
31702 case TYPE_CNTLZ:
31703 case TYPE_TRAP:
31704 case TYPE_MUL:
31705 case TYPE_FPCOMPARE:
31706 case TYPE_MFCR:
31707 case TYPE_MTCR:
31708 case TYPE_MFJMPR:
31709 case TYPE_MTJMPR:
31710 case TYPE_ISYNC:
31711 case TYPE_SYNC:
31712 case TYPE_LOAD_L:
31713 case TYPE_STORE_C:
31714 return true;
31715 case TYPE_SHIFT:
31716 if (get_attr_dot (insn) == DOT_NO
31717 || get_attr_var_shift (insn) == VAR_SHIFT_NO)
31718 return true;
31719 else
31720 break;
31721 case TYPE_DIV:
31722 if (get_attr_size (insn) == SIZE_32)
31723 return true;
31724 else
31725 break;
31726 default:
31727 break;
31729 break;
31730 case PROCESSOR_POWER7:
31731 type = get_attr_type (insn);
31733 switch (type)
31735 case TYPE_ISYNC:
31736 case TYPE_SYNC:
31737 case TYPE_LOAD_L:
31738 case TYPE_STORE_C:
31739 return true;
31740 case TYPE_LOAD:
31741 if (get_attr_sign_extend (insn) == SIGN_EXTEND_YES
31742 && get_attr_update (insn) == UPDATE_YES)
31743 return true;
31744 else
31745 break;
31746 case TYPE_STORE:
31747 if (get_attr_update (insn) == UPDATE_YES
31748 && get_attr_indexed (insn) == INDEXED_YES)
31749 return true;
31750 else
31751 break;
31752 default:
31753 break;
31755 break;
31756 case PROCESSOR_POWER8:
31757 type = get_attr_type (insn);
31759 switch (type)
31761 case TYPE_MFCR:
31762 case TYPE_MTCR:
31763 case TYPE_ISYNC:
31764 case TYPE_SYNC:
31765 case TYPE_LOAD_L:
31766 case TYPE_STORE_C:
31767 return true;
31768 case TYPE_LOAD:
31769 if (get_attr_sign_extend (insn) == SIGN_EXTEND_YES
31770 && get_attr_update (insn) == UPDATE_YES)
31771 return true;
31772 else
31773 break;
31774 case TYPE_STORE:
31775 if (get_attr_update (insn) == UPDATE_YES
31776 && get_attr_indexed (insn) == INDEXED_YES)
31777 return true;
31778 else
31779 break;
31780 default:
31781 break;
31783 break;
31784 default:
31785 break;
31788 return false;
31791 /* Return true if it is recommended to keep NEXT_INSN "far" (in a separate
31792 dispatch group) from the insns in GROUP_INSNS. Return false otherwise. */
31794 static bool
31795 is_costly_group (rtx *group_insns, rtx next_insn)
31797 int i;
31798 int issue_rate = rs6000_issue_rate ();
31800 for (i = 0; i < issue_rate; i++)
31802 sd_iterator_def sd_it;
31803 dep_t dep;
31804 rtx insn = group_insns[i];
31806 if (!insn)
31807 continue;
31809 FOR_EACH_DEP (insn, SD_LIST_RES_FORW, sd_it, dep)
31811 rtx next = DEP_CON (dep);
31813 if (next == next_insn
31814 && rs6000_is_costly_dependence (dep, dep_cost (dep), 0))
31815 return true;
31819 return false;
31822 /* Utility of the function redefine_groups.
31823 Check if it is too costly to schedule NEXT_INSN together with GROUP_INSNS
31824 in the same dispatch group. If so, insert nops before NEXT_INSN, in order
31825 to keep it "far" (in a separate group) from GROUP_INSNS, following
31826 one of the following schemes, depending on the value of the flag
31827 -minsert_sched_nops = X:
31828 (1) X == sched_finish_regroup_exact: insert exactly as many nops as needed
31829 in order to force NEXT_INSN into a separate group.
31830 (2) X < sched_finish_regroup_exact: insert exactly X nops.
31831 GROUP_END, CAN_ISSUE_MORE and GROUP_COUNT record the state after nop
31832 insertion (has a group just ended, how many vacant issue slots remain in the
31833 last group, and how many dispatch groups were encountered so far). */
31835 static int
31836 force_new_group (int sched_verbose, FILE *dump, rtx *group_insns,
31837 rtx_insn *next_insn, bool *group_end, int can_issue_more,
31838 int *group_count)
31840 rtx nop;
31841 bool force;
31842 int issue_rate = rs6000_issue_rate ();
31843 bool end = *group_end;
31844 int i;
31846 if (next_insn == NULL_RTX || DEBUG_INSN_P (next_insn))
31847 return can_issue_more;
31849 if (rs6000_sched_insert_nops > sched_finish_regroup_exact)
31850 return can_issue_more;
31852 force = is_costly_group (group_insns, next_insn);
31853 if (!force)
31854 return can_issue_more;
31856 if (sched_verbose > 6)
31857 fprintf (dump,"force: group count = %d, can_issue_more = %d\n",
31858 *group_count ,can_issue_more);
31860 if (rs6000_sched_insert_nops == sched_finish_regroup_exact)
31862 if (*group_end)
31863 can_issue_more = 0;
31865 /* Since only a branch can be issued in the last issue_slot, it is
31866 sufficient to insert 'can_issue_more - 1' nops if next_insn is not
31867 a branch. If next_insn is a branch, we insert 'can_issue_more' nops;
31868 in this case the last nop will start a new group and the branch
31869 will be forced to the new group. */
31870 if (can_issue_more && !is_branch_slot_insn (next_insn))
31871 can_issue_more--;
31873 /* Do we have a special group ending nop? */
31874 if (rs6000_tune == PROCESSOR_POWER6 || rs6000_tune == PROCESSOR_POWER7
31875 || rs6000_tune == PROCESSOR_POWER8)
31877 nop = gen_group_ending_nop ();
31878 emit_insn_before (nop, next_insn);
31879 can_issue_more = 0;
31881 else
31882 while (can_issue_more > 0)
31884 nop = gen_nop ();
31885 emit_insn_before (nop, next_insn);
31886 can_issue_more--;
31889 *group_end = true;
31890 return 0;
31893 if (rs6000_sched_insert_nops < sched_finish_regroup_exact)
31895 int n_nops = rs6000_sched_insert_nops;
31897 /* Nops can't be issued from the branch slot, so the effective
31898 issue_rate for nops is 'issue_rate - 1'. */
31899 if (can_issue_more == 0)
31900 can_issue_more = issue_rate;
31901 can_issue_more--;
31902 if (can_issue_more == 0)
31904 can_issue_more = issue_rate - 1;
31905 (*group_count)++;
31906 end = true;
31907 for (i = 0; i < issue_rate; i++)
31909 group_insns[i] = 0;
31913 while (n_nops > 0)
31915 nop = gen_nop ();
31916 emit_insn_before (nop, next_insn);
31917 if (can_issue_more == issue_rate - 1) /* new group begins */
31918 end = false;
31919 can_issue_more--;
31920 if (can_issue_more == 0)
31922 can_issue_more = issue_rate - 1;
31923 (*group_count)++;
31924 end = true;
31925 for (i = 0; i < issue_rate; i++)
31927 group_insns[i] = 0;
31930 n_nops--;
31933 /* Scale back relative to 'issue_rate' (instead of 'issue_rate - 1'). */
31934 can_issue_more++;
31936 /* Is next_insn going to start a new group? */
31937 *group_end
31938 = (end
31939 || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
31940 || (can_issue_more <= 2 && is_cracked_insn (next_insn))
31941 || (can_issue_more < issue_rate &&
31942 insn_terminates_group_p (next_insn, previous_group)));
31943 if (*group_end && end)
31944 (*group_count)--;
31946 if (sched_verbose > 6)
31947 fprintf (dump, "done force: group count = %d, can_issue_more = %d\n",
31948 *group_count, can_issue_more);
31949 return can_issue_more;
31952 return can_issue_more;
31955 /* This function tries to synch the dispatch groups that the compiler "sees"
31956 with the dispatch groups that the processor dispatcher is expected to
31957 form in practice. It tries to achieve this synchronization by forcing the
31958 estimated processor grouping on the compiler (as opposed to the function
31959 'pad_goups' which tries to force the scheduler's grouping on the processor).
31961 The function scans the insn sequence between PREV_HEAD_INSN and TAIL and
31962 examines the (estimated) dispatch groups that will be formed by the processor
31963 dispatcher. It marks these group boundaries to reflect the estimated
31964 processor grouping, overriding the grouping that the scheduler had marked.
31965 Depending on the value of the flag '-minsert-sched-nops' this function can
31966 force certain insns into separate groups or force a certain distance between
31967 them by inserting nops, for example, if there exists a "costly dependence"
31968 between the insns.
31970 The function estimates the group boundaries that the processor will form as
31971 follows: It keeps track of how many vacant issue slots are available after
31972 each insn. A subsequent insn will start a new group if one of the following
31973 4 cases applies:
31974 - no more vacant issue slots remain in the current dispatch group.
31975 - only the last issue slot, which is the branch slot, is vacant, but the next
31976 insn is not a branch.
31977 - only the last 2 or less issue slots, including the branch slot, are vacant,
31978 which means that a cracked insn (which occupies two issue slots) can't be
31979 issued in this group.
31980 - less than 'issue_rate' slots are vacant, and the next insn always needs to
31981 start a new group. */
31983 static int
31984 redefine_groups (FILE *dump, int sched_verbose, rtx_insn *prev_head_insn,
31985 rtx_insn *tail)
31987 rtx_insn *insn, *next_insn;
31988 int issue_rate;
31989 int can_issue_more;
31990 int slot, i;
31991 bool group_end;
31992 int group_count = 0;
31993 rtx *group_insns;
31995 /* Initialize. */
31996 issue_rate = rs6000_issue_rate ();
31997 group_insns = XALLOCAVEC (rtx, issue_rate);
31998 for (i = 0; i < issue_rate; i++)
32000 group_insns[i] = 0;
32002 can_issue_more = issue_rate;
32003 slot = 0;
32004 insn = get_next_active_insn (prev_head_insn, tail);
32005 group_end = false;
32007 while (insn != NULL_RTX)
32009 slot = (issue_rate - can_issue_more);
32010 group_insns[slot] = insn;
32011 can_issue_more =
32012 rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
32013 if (insn_terminates_group_p (insn, current_group))
32014 can_issue_more = 0;
32016 next_insn = get_next_active_insn (insn, tail);
32017 if (next_insn == NULL_RTX)
32018 return group_count + 1;
32020 /* Is next_insn going to start a new group? */
32021 group_end
32022 = (can_issue_more == 0
32023 || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
32024 || (can_issue_more <= 2 && is_cracked_insn (next_insn))
32025 || (can_issue_more < issue_rate &&
32026 insn_terminates_group_p (next_insn, previous_group)));
32028 can_issue_more = force_new_group (sched_verbose, dump, group_insns,
32029 next_insn, &group_end, can_issue_more,
32030 &group_count);
32032 if (group_end)
32034 group_count++;
32035 can_issue_more = 0;
32036 for (i = 0; i < issue_rate; i++)
32038 group_insns[i] = 0;
32042 if (GET_MODE (next_insn) == TImode && can_issue_more)
32043 PUT_MODE (next_insn, VOIDmode);
32044 else if (!can_issue_more && GET_MODE (next_insn) != TImode)
32045 PUT_MODE (next_insn, TImode);
32047 insn = next_insn;
32048 if (can_issue_more == 0)
32049 can_issue_more = issue_rate;
32050 } /* while */
32052 return group_count;
32055 /* Scan the insn sequence between PREV_HEAD_INSN and TAIL and examine the
32056 dispatch group boundaries that the scheduler had marked. Pad with nops
32057 any dispatch groups which have vacant issue slots, in order to force the
32058 scheduler's grouping on the processor dispatcher. The function
32059 returns the number of dispatch groups found. */
32061 static int
32062 pad_groups (FILE *dump, int sched_verbose, rtx_insn *prev_head_insn,
32063 rtx_insn *tail)
32065 rtx_insn *insn, *next_insn;
32066 rtx nop;
32067 int issue_rate;
32068 int can_issue_more;
32069 int group_end;
32070 int group_count = 0;
32072 /* Initialize issue_rate. */
32073 issue_rate = rs6000_issue_rate ();
32074 can_issue_more = issue_rate;
32076 insn = get_next_active_insn (prev_head_insn, tail);
32077 next_insn = get_next_active_insn (insn, tail);
32079 while (insn != NULL_RTX)
32081 can_issue_more =
32082 rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
32084 group_end = (next_insn == NULL_RTX || GET_MODE (next_insn) == TImode);
32086 if (next_insn == NULL_RTX)
32087 break;
32089 if (group_end)
32091 /* If the scheduler had marked group termination at this location
32092 (between insn and next_insn), and neither insn nor next_insn will
32093 force group termination, pad the group with nops to force group
32094 termination. */
32095 if (can_issue_more
32096 && (rs6000_sched_insert_nops == sched_finish_pad_groups)
32097 && !insn_terminates_group_p (insn, current_group)
32098 && !insn_terminates_group_p (next_insn, previous_group))
32100 if (!is_branch_slot_insn (next_insn))
32101 can_issue_more--;
32103 while (can_issue_more)
32105 nop = gen_nop ();
32106 emit_insn_before (nop, next_insn);
32107 can_issue_more--;
32111 can_issue_more = issue_rate;
32112 group_count++;
32115 insn = next_insn;
32116 next_insn = get_next_active_insn (insn, tail);
32119 return group_count;
32122 /* We're beginning a new block. Initialize data structures as necessary. */
32124 static void
32125 rs6000_sched_init (FILE *dump ATTRIBUTE_UNUSED,
32126 int sched_verbose ATTRIBUTE_UNUSED,
32127 int max_ready ATTRIBUTE_UNUSED)
32129 last_scheduled_insn = NULL;
32130 load_store_pendulum = 0;
32131 divide_cnt = 0;
32132 vec_pairing = 0;
32135 /* The following function is called at the end of scheduling BB.
32136 After reload, it inserts nops at insn group bundling. */
32138 static void
32139 rs6000_sched_finish (FILE *dump, int sched_verbose)
32141 int n_groups;
32143 if (sched_verbose)
32144 fprintf (dump, "=== Finishing schedule.\n");
32146 if (reload_completed && rs6000_sched_groups)
32148 /* Do not run sched_finish hook when selective scheduling enabled. */
32149 if (sel_sched_p ())
32150 return;
32152 if (rs6000_sched_insert_nops == sched_finish_none)
32153 return;
32155 if (rs6000_sched_insert_nops == sched_finish_pad_groups)
32156 n_groups = pad_groups (dump, sched_verbose,
32157 current_sched_info->prev_head,
32158 current_sched_info->next_tail);
32159 else
32160 n_groups = redefine_groups (dump, sched_verbose,
32161 current_sched_info->prev_head,
32162 current_sched_info->next_tail);
32164 if (sched_verbose >= 6)
32166 fprintf (dump, "ngroups = %d\n", n_groups);
32167 print_rtl (dump, current_sched_info->prev_head);
32168 fprintf (dump, "Done finish_sched\n");
32173 struct rs6000_sched_context
32175 short cached_can_issue_more;
32176 rtx_insn *last_scheduled_insn;
32177 int load_store_pendulum;
32178 int divide_cnt;
32179 int vec_pairing;
32182 typedef struct rs6000_sched_context rs6000_sched_context_def;
32183 typedef rs6000_sched_context_def *rs6000_sched_context_t;
32185 /* Allocate store for new scheduling context. */
32186 static void *
32187 rs6000_alloc_sched_context (void)
32189 return xmalloc (sizeof (rs6000_sched_context_def));
32192 /* If CLEAN_P is true then initializes _SC with clean data,
32193 and from the global context otherwise. */
32194 static void
32195 rs6000_init_sched_context (void *_sc, bool clean_p)
32197 rs6000_sched_context_t sc = (rs6000_sched_context_t) _sc;
32199 if (clean_p)
32201 sc->cached_can_issue_more = 0;
32202 sc->last_scheduled_insn = NULL;
32203 sc->load_store_pendulum = 0;
32204 sc->divide_cnt = 0;
32205 sc->vec_pairing = 0;
32207 else
32209 sc->cached_can_issue_more = cached_can_issue_more;
32210 sc->last_scheduled_insn = last_scheduled_insn;
32211 sc->load_store_pendulum = load_store_pendulum;
32212 sc->divide_cnt = divide_cnt;
32213 sc->vec_pairing = vec_pairing;
32217 /* Sets the global scheduling context to the one pointed to by _SC. */
32218 static void
32219 rs6000_set_sched_context (void *_sc)
32221 rs6000_sched_context_t sc = (rs6000_sched_context_t) _sc;
32223 gcc_assert (sc != NULL);
32225 cached_can_issue_more = sc->cached_can_issue_more;
32226 last_scheduled_insn = sc->last_scheduled_insn;
32227 load_store_pendulum = sc->load_store_pendulum;
32228 divide_cnt = sc->divide_cnt;
32229 vec_pairing = sc->vec_pairing;
32232 /* Free _SC. */
32233 static void
32234 rs6000_free_sched_context (void *_sc)
32236 gcc_assert (_sc != NULL);
32238 free (_sc);
32241 static bool
32242 rs6000_sched_can_speculate_insn (rtx_insn *insn)
32244 switch (get_attr_type (insn))
32246 case TYPE_DIV:
32247 case TYPE_SDIV:
32248 case TYPE_DDIV:
32249 case TYPE_VECDIV:
32250 case TYPE_SSQRT:
32251 case TYPE_DSQRT:
32252 return false;
32254 default:
32255 return true;
32259 /* Length in units of the trampoline for entering a nested function. */
32262 rs6000_trampoline_size (void)
32264 int ret = 0;
32266 switch (DEFAULT_ABI)
32268 default:
32269 gcc_unreachable ();
32271 case ABI_AIX:
32272 ret = (TARGET_32BIT) ? 12 : 24;
32273 break;
32275 case ABI_ELFv2:
32276 gcc_assert (!TARGET_32BIT);
32277 ret = 32;
32278 break;
32280 case ABI_DARWIN:
32281 case ABI_V4:
32282 ret = (TARGET_32BIT) ? 40 : 48;
32283 break;
32286 return ret;
32289 /* Emit RTL insns to initialize the variable parts of a trampoline.
32290 FNADDR is an RTX for the address of the function's pure code.
32291 CXT is an RTX for the static chain value for the function. */
32293 static void
32294 rs6000_trampoline_init (rtx m_tramp, tree fndecl, rtx cxt)
32296 int regsize = (TARGET_32BIT) ? 4 : 8;
32297 rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
32298 rtx ctx_reg = force_reg (Pmode, cxt);
32299 rtx addr = force_reg (Pmode, XEXP (m_tramp, 0));
32301 switch (DEFAULT_ABI)
32303 default:
32304 gcc_unreachable ();
32306 /* Under AIX, just build the 3 word function descriptor */
32307 case ABI_AIX:
32309 rtx fnmem, fn_reg, toc_reg;
32311 if (!TARGET_POINTERS_TO_NESTED_FUNCTIONS)
32312 error ("you cannot take the address of a nested function if you use "
32313 "the %qs option", "-mno-pointers-to-nested-functions");
32315 fnmem = gen_const_mem (Pmode, force_reg (Pmode, fnaddr));
32316 fn_reg = gen_reg_rtx (Pmode);
32317 toc_reg = gen_reg_rtx (Pmode);
32319 /* Macro to shorten the code expansions below. */
32320 # define MEM_PLUS(MEM, OFFSET) adjust_address (MEM, Pmode, OFFSET)
32322 m_tramp = replace_equiv_address (m_tramp, addr);
32324 emit_move_insn (fn_reg, MEM_PLUS (fnmem, 0));
32325 emit_move_insn (toc_reg, MEM_PLUS (fnmem, regsize));
32326 emit_move_insn (MEM_PLUS (m_tramp, 0), fn_reg);
32327 emit_move_insn (MEM_PLUS (m_tramp, regsize), toc_reg);
32328 emit_move_insn (MEM_PLUS (m_tramp, 2*regsize), ctx_reg);
32330 # undef MEM_PLUS
32332 break;
32334 /* Under V.4/eabi/darwin, __trampoline_setup does the real work. */
32335 case ABI_ELFv2:
32336 case ABI_DARWIN:
32337 case ABI_V4:
32338 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__trampoline_setup"),
32339 LCT_NORMAL, VOIDmode,
32340 addr, Pmode,
32341 GEN_INT (rs6000_trampoline_size ()), SImode,
32342 fnaddr, Pmode,
32343 ctx_reg, Pmode);
32344 break;
32349 /* Returns TRUE iff the target attribute indicated by ATTR_ID takes a plain
32350 identifier as an argument, so the front end shouldn't look it up. */
32352 static bool
32353 rs6000_attribute_takes_identifier_p (const_tree attr_id)
32355 return is_attribute_p ("altivec", attr_id);
32358 /* Handle the "altivec" attribute. The attribute may have
32359 arguments as follows:
32361 __attribute__((altivec(vector__)))
32362 __attribute__((altivec(pixel__))) (always followed by 'unsigned short')
32363 __attribute__((altivec(bool__))) (always followed by 'unsigned')
32365 and may appear more than once (e.g., 'vector bool char') in a
32366 given declaration. */
32368 static tree
32369 rs6000_handle_altivec_attribute (tree *node,
32370 tree name ATTRIBUTE_UNUSED,
32371 tree args,
32372 int flags ATTRIBUTE_UNUSED,
32373 bool *no_add_attrs)
32375 tree type = *node, result = NULL_TREE;
32376 machine_mode mode;
32377 int unsigned_p;
32378 char altivec_type
32379 = ((args && TREE_CODE (args) == TREE_LIST && TREE_VALUE (args)
32380 && TREE_CODE (TREE_VALUE (args)) == IDENTIFIER_NODE)
32381 ? *IDENTIFIER_POINTER (TREE_VALUE (args))
32382 : '?');
32384 while (POINTER_TYPE_P (type)
32385 || TREE_CODE (type) == FUNCTION_TYPE
32386 || TREE_CODE (type) == METHOD_TYPE
32387 || TREE_CODE (type) == ARRAY_TYPE)
32388 type = TREE_TYPE (type);
32390 mode = TYPE_MODE (type);
32392 /* Check for invalid AltiVec type qualifiers. */
32393 if (type == long_double_type_node)
32394 error ("use of %<long double%> in AltiVec types is invalid");
32395 else if (type == boolean_type_node)
32396 error ("use of boolean types in AltiVec types is invalid");
32397 else if (TREE_CODE (type) == COMPLEX_TYPE)
32398 error ("use of %<complex%> in AltiVec types is invalid");
32399 else if (DECIMAL_FLOAT_MODE_P (mode))
32400 error ("use of decimal floating point types in AltiVec types is invalid");
32401 else if (!TARGET_VSX)
32403 if (type == long_unsigned_type_node || type == long_integer_type_node)
32405 if (TARGET_64BIT)
32406 error ("use of %<long%> in AltiVec types is invalid for "
32407 "64-bit code without %qs", "-mvsx");
32408 else if (rs6000_warn_altivec_long)
32409 warning (0, "use of %<long%> in AltiVec types is deprecated; "
32410 "use %<int%>");
32412 else if (type == long_long_unsigned_type_node
32413 || type == long_long_integer_type_node)
32414 error ("use of %<long long%> in AltiVec types is invalid without %qs",
32415 "-mvsx");
32416 else if (type == double_type_node)
32417 error ("use of %<double%> in AltiVec types is invalid without %qs",
32418 "-mvsx");
32421 switch (altivec_type)
32423 case 'v':
32424 unsigned_p = TYPE_UNSIGNED (type);
32425 switch (mode)
32427 case E_TImode:
32428 result = (unsigned_p ? unsigned_V1TI_type_node : V1TI_type_node);
32429 break;
32430 case E_DImode:
32431 result = (unsigned_p ? unsigned_V2DI_type_node : V2DI_type_node);
32432 break;
32433 case E_SImode:
32434 result = (unsigned_p ? unsigned_V4SI_type_node : V4SI_type_node);
32435 break;
32436 case E_HImode:
32437 result = (unsigned_p ? unsigned_V8HI_type_node : V8HI_type_node);
32438 break;
32439 case E_QImode:
32440 result = (unsigned_p ? unsigned_V16QI_type_node : V16QI_type_node);
32441 break;
32442 case E_SFmode: result = V4SF_type_node; break;
32443 case E_DFmode: result = V2DF_type_node; break;
32444 /* If the user says 'vector int bool', we may be handed the 'bool'
32445 attribute _before_ the 'vector' attribute, and so select the
32446 proper type in the 'b' case below. */
32447 case E_V4SImode: case E_V8HImode: case E_V16QImode: case E_V4SFmode:
32448 case E_V2DImode: case E_V2DFmode:
32449 result = type;
32450 default: break;
32452 break;
32453 case 'b':
32454 switch (mode)
32456 case E_DImode: case E_V2DImode: result = bool_V2DI_type_node; break;
32457 case E_SImode: case E_V4SImode: result = bool_V4SI_type_node; break;
32458 case E_HImode: case E_V8HImode: result = bool_V8HI_type_node; break;
32459 case E_QImode: case E_V16QImode: result = bool_V16QI_type_node;
32460 default: break;
32462 break;
32463 case 'p':
32464 switch (mode)
32466 case E_V8HImode: result = pixel_V8HI_type_node;
32467 default: break;
32469 default: break;
32472 /* Propagate qualifiers attached to the element type
32473 onto the vector type. */
32474 if (result && result != type && TYPE_QUALS (type))
32475 result = build_qualified_type (result, TYPE_QUALS (type));
32477 *no_add_attrs = true; /* No need to hang on to the attribute. */
32479 if (result)
32480 *node = lang_hooks.types.reconstruct_complex_type (*node, result);
32482 return NULL_TREE;
32485 /* AltiVec defines five built-in scalar types that serve as vector
32486 elements; we must teach the compiler how to mangle them. The 128-bit
32487 floating point mangling is target-specific as well. */
32489 static const char *
32490 rs6000_mangle_type (const_tree type)
32492 type = TYPE_MAIN_VARIANT (type);
32494 if (TREE_CODE (type) != VOID_TYPE && TREE_CODE (type) != BOOLEAN_TYPE
32495 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
32496 return NULL;
32498 if (type == bool_char_type_node) return "U6__boolc";
32499 if (type == bool_short_type_node) return "U6__bools";
32500 if (type == pixel_type_node) return "u7__pixel";
32501 if (type == bool_int_type_node) return "U6__booli";
32502 if (type == bool_long_long_type_node) return "U6__boolx";
32504 if (SCALAR_FLOAT_TYPE_P (type) && FLOAT128_IBM_P (TYPE_MODE (type)))
32505 return "g";
32506 if (SCALAR_FLOAT_TYPE_P (type) && FLOAT128_IEEE_P (TYPE_MODE (type)))
32507 return ieee128_mangling_gcc_8_1 ? "U10__float128" : "u9__ieee128";
32509 /* For all other types, use the default mangling. */
32510 return NULL;
32513 /* Handle a "longcall" or "shortcall" attribute; arguments as in
32514 struct attribute_spec.handler. */
32516 static tree
32517 rs6000_handle_longcall_attribute (tree *node, tree name,
32518 tree args ATTRIBUTE_UNUSED,
32519 int flags ATTRIBUTE_UNUSED,
32520 bool *no_add_attrs)
32522 if (TREE_CODE (*node) != FUNCTION_TYPE
32523 && TREE_CODE (*node) != FIELD_DECL
32524 && TREE_CODE (*node) != TYPE_DECL)
32526 warning (OPT_Wattributes, "%qE attribute only applies to functions",
32527 name);
32528 *no_add_attrs = true;
32531 return NULL_TREE;
32534 /* Set longcall attributes on all functions declared when
32535 rs6000_default_long_calls is true. */
32536 static void
32537 rs6000_set_default_type_attributes (tree type)
32539 if (rs6000_default_long_calls
32540 && (TREE_CODE (type) == FUNCTION_TYPE
32541 || TREE_CODE (type) == METHOD_TYPE))
32542 TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("longcall"),
32543 NULL_TREE,
32544 TYPE_ATTRIBUTES (type));
32546 #if TARGET_MACHO
32547 darwin_set_default_type_attributes (type);
32548 #endif
32551 /* Return a reference suitable for calling a function with the
32552 longcall attribute. */
32554 static rtx
32555 rs6000_longcall_ref (rtx call_ref, rtx arg)
32557 /* System V adds '.' to the internal name, so skip them. */
32558 const char *call_name = XSTR (call_ref, 0);
32559 if (*call_name == '.')
32561 while (*call_name == '.')
32562 call_name++;
32564 tree node = get_identifier (call_name);
32565 call_ref = gen_rtx_SYMBOL_REF (VOIDmode, IDENTIFIER_POINTER (node));
32568 if (TARGET_PLTSEQ)
32570 rtx base = const0_rtx;
32571 int regno;
32572 if (DEFAULT_ABI == ABI_ELFv2)
32574 base = gen_rtx_REG (Pmode, TOC_REGISTER);
32575 regno = 12;
32577 else
32579 if (flag_pic)
32580 base = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
32581 regno = 11;
32583 /* Reg must match that used by linker PLT stubs. For ELFv2, r12
32584 may be used by a function global entry point. For SysV4, r11
32585 is used by __glink_PLTresolve lazy resolver entry. */
32586 rtx reg = gen_rtx_REG (Pmode, regno);
32587 rtx hi = gen_rtx_UNSPEC (Pmode, gen_rtvec (3, base, call_ref, arg),
32588 UNSPEC_PLT16_HA);
32589 rtx lo = gen_rtx_UNSPEC (Pmode, gen_rtvec (3, reg, call_ref, arg),
32590 UNSPEC_PLT16_LO);
32591 emit_insn (gen_rtx_SET (reg, hi));
32592 emit_insn (gen_rtx_SET (reg, lo));
32593 return reg;
32596 return force_reg (Pmode, call_ref);
32599 #ifndef TARGET_USE_MS_BITFIELD_LAYOUT
32600 #define TARGET_USE_MS_BITFIELD_LAYOUT 0
32601 #endif
32603 /* Handle a "ms_struct" or "gcc_struct" attribute; arguments as in
32604 struct attribute_spec.handler. */
32605 static tree
32606 rs6000_handle_struct_attribute (tree *node, tree name,
32607 tree args ATTRIBUTE_UNUSED,
32608 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
32610 tree *type = NULL;
32611 if (DECL_P (*node))
32613 if (TREE_CODE (*node) == TYPE_DECL)
32614 type = &TREE_TYPE (*node);
32616 else
32617 type = node;
32619 if (!(type && (TREE_CODE (*type) == RECORD_TYPE
32620 || TREE_CODE (*type) == UNION_TYPE)))
32622 warning (OPT_Wattributes, "%qE attribute ignored", name);
32623 *no_add_attrs = true;
32626 else if ((is_attribute_p ("ms_struct", name)
32627 && lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (*type)))
32628 || ((is_attribute_p ("gcc_struct", name)
32629 && lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (*type)))))
32631 warning (OPT_Wattributes, "%qE incompatible attribute ignored",
32632 name);
32633 *no_add_attrs = true;
32636 return NULL_TREE;
32639 static bool
32640 rs6000_ms_bitfield_layout_p (const_tree record_type)
32642 return (TARGET_USE_MS_BITFIELD_LAYOUT &&
32643 !lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (record_type)))
32644 || lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (record_type));
32647 #ifdef USING_ELFOS_H
32649 /* A get_unnamed_section callback, used for switching to toc_section. */
32651 static void
32652 rs6000_elf_output_toc_section_asm_op (const void *data ATTRIBUTE_UNUSED)
32654 if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
32655 && TARGET_MINIMAL_TOC)
32657 if (!toc_initialized)
32659 fprintf (asm_out_file, "%s\n", TOC_SECTION_ASM_OP);
32660 ASM_OUTPUT_ALIGN (asm_out_file, TARGET_64BIT ? 3 : 2);
32661 (*targetm.asm_out.internal_label) (asm_out_file, "LCTOC", 0);
32662 fprintf (asm_out_file, "\t.tc ");
32663 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1[TC],");
32664 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
32665 fprintf (asm_out_file, "\n");
32667 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
32668 ASM_OUTPUT_ALIGN (asm_out_file, TARGET_64BIT ? 3 : 2);
32669 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
32670 fprintf (asm_out_file, " = .+32768\n");
32671 toc_initialized = 1;
32673 else
32674 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
32676 else if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
32678 fprintf (asm_out_file, "%s\n", TOC_SECTION_ASM_OP);
32679 if (!toc_initialized)
32681 ASM_OUTPUT_ALIGN (asm_out_file, TARGET_64BIT ? 3 : 2);
32682 toc_initialized = 1;
32685 else
32687 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
32688 if (!toc_initialized)
32690 ASM_OUTPUT_ALIGN (asm_out_file, TARGET_64BIT ? 3 : 2);
32691 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
32692 fprintf (asm_out_file, " = .+32768\n");
32693 toc_initialized = 1;
32698 /* Implement TARGET_ASM_INIT_SECTIONS. */
32700 static void
32701 rs6000_elf_asm_init_sections (void)
32703 toc_section
32704 = get_unnamed_section (0, rs6000_elf_output_toc_section_asm_op, NULL);
32706 sdata2_section
32707 = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
32708 SDATA2_SECTION_ASM_OP);
32711 /* Implement TARGET_SELECT_RTX_SECTION. */
32713 static section *
32714 rs6000_elf_select_rtx_section (machine_mode mode, rtx x,
32715 unsigned HOST_WIDE_INT align)
32717 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
32718 return toc_section;
32719 else
32720 return default_elf_select_rtx_section (mode, x, align);
32723 /* For a SYMBOL_REF, set generic flags and then perform some
32724 target-specific processing.
32726 When the AIX ABI is requested on a non-AIX system, replace the
32727 function name with the real name (with a leading .) rather than the
32728 function descriptor name. This saves a lot of overriding code to
32729 read the prefixes. */
32731 static void rs6000_elf_encode_section_info (tree, rtx, int) ATTRIBUTE_UNUSED;
32732 static void
32733 rs6000_elf_encode_section_info (tree decl, rtx rtl, int first)
32735 default_encode_section_info (decl, rtl, first);
32737 if (first
32738 && TREE_CODE (decl) == FUNCTION_DECL
32739 && !TARGET_AIX
32740 && DEFAULT_ABI == ABI_AIX)
32742 rtx sym_ref = XEXP (rtl, 0);
32743 size_t len = strlen (XSTR (sym_ref, 0));
32744 char *str = XALLOCAVEC (char, len + 2);
32745 str[0] = '.';
32746 memcpy (str + 1, XSTR (sym_ref, 0), len + 1);
32747 XSTR (sym_ref, 0) = ggc_alloc_string (str, len + 1);
32751 static inline bool
32752 compare_section_name (const char *section, const char *templ)
32754 int len;
32756 len = strlen (templ);
32757 return (strncmp (section, templ, len) == 0
32758 && (section[len] == 0 || section[len] == '.'));
32761 bool
32762 rs6000_elf_in_small_data_p (const_tree decl)
32764 if (rs6000_sdata == SDATA_NONE)
32765 return false;
32767 /* We want to merge strings, so we never consider them small data. */
32768 if (TREE_CODE (decl) == STRING_CST)
32769 return false;
32771 /* Functions are never in the small data area. */
32772 if (TREE_CODE (decl) == FUNCTION_DECL)
32773 return false;
32775 if (TREE_CODE (decl) == VAR_DECL && DECL_SECTION_NAME (decl))
32777 const char *section = DECL_SECTION_NAME (decl);
32778 if (compare_section_name (section, ".sdata")
32779 || compare_section_name (section, ".sdata2")
32780 || compare_section_name (section, ".gnu.linkonce.s")
32781 || compare_section_name (section, ".sbss")
32782 || compare_section_name (section, ".sbss2")
32783 || compare_section_name (section, ".gnu.linkonce.sb")
32784 || strcmp (section, ".PPC.EMB.sdata0") == 0
32785 || strcmp (section, ".PPC.EMB.sbss0") == 0)
32786 return true;
32788 else
32790 /* If we are told not to put readonly data in sdata, then don't. */
32791 if (TREE_READONLY (decl) && rs6000_sdata != SDATA_EABI
32792 && !rs6000_readonly_in_sdata)
32793 return false;
32795 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (decl));
32797 if (size > 0
32798 && size <= g_switch_value
32799 /* If it's not public, and we're not going to reference it there,
32800 there's no need to put it in the small data section. */
32801 && (rs6000_sdata != SDATA_DATA || TREE_PUBLIC (decl)))
32802 return true;
32805 return false;
32808 #endif /* USING_ELFOS_H */
32810 /* Implement TARGET_USE_BLOCKS_FOR_CONSTANT_P. */
32812 static bool
32813 rs6000_use_blocks_for_constant_p (machine_mode mode, const_rtx x)
32815 return !ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode);
32818 /* Do not place thread-local symbols refs in the object blocks. */
32820 static bool
32821 rs6000_use_blocks_for_decl_p (const_tree decl)
32823 return !DECL_THREAD_LOCAL_P (decl);
32826 /* Return a REG that occurs in ADDR with coefficient 1.
32827 ADDR can be effectively incremented by incrementing REG.
32829 r0 is special and we must not select it as an address
32830 register by this routine since our caller will try to
32831 increment the returned register via an "la" instruction. */
32834 find_addr_reg (rtx addr)
32836 while (GET_CODE (addr) == PLUS)
32838 if (REG_P (XEXP (addr, 0))
32839 && REGNO (XEXP (addr, 0)) != 0)
32840 addr = XEXP (addr, 0);
32841 else if (REG_P (XEXP (addr, 1))
32842 && REGNO (XEXP (addr, 1)) != 0)
32843 addr = XEXP (addr, 1);
32844 else if (CONSTANT_P (XEXP (addr, 0)))
32845 addr = XEXP (addr, 1);
32846 else if (CONSTANT_P (XEXP (addr, 1)))
32847 addr = XEXP (addr, 0);
32848 else
32849 gcc_unreachable ();
32851 gcc_assert (REG_P (addr) && REGNO (addr) != 0);
32852 return addr;
32855 void
32856 rs6000_fatal_bad_address (rtx op)
32858 fatal_insn ("bad address", op);
32861 #if TARGET_MACHO
32863 typedef struct branch_island_d {
32864 tree function_name;
32865 tree label_name;
32866 int line_number;
32867 } branch_island;
32870 static vec<branch_island, va_gc> *branch_islands;
32872 /* Remember to generate a branch island for far calls to the given
32873 function. */
32875 static void
32876 add_compiler_branch_island (tree label_name, tree function_name,
32877 int line_number)
32879 branch_island bi = {function_name, label_name, line_number};
32880 vec_safe_push (branch_islands, bi);
32883 /* Generate far-jump branch islands for everything recorded in
32884 branch_islands. Invoked immediately after the last instruction of
32885 the epilogue has been emitted; the branch islands must be appended
32886 to, and contiguous with, the function body. Mach-O stubs are
32887 generated in machopic_output_stub(). */
32889 static void
32890 macho_branch_islands (void)
32892 char tmp_buf[512];
32894 while (!vec_safe_is_empty (branch_islands))
32896 branch_island *bi = &branch_islands->last ();
32897 const char *label = IDENTIFIER_POINTER (bi->label_name);
32898 const char *name = IDENTIFIER_POINTER (bi->function_name);
32899 char name_buf[512];
32900 /* Cheap copy of the details from the Darwin ASM_OUTPUT_LABELREF(). */
32901 if (name[0] == '*' || name[0] == '&')
32902 strcpy (name_buf, name+1);
32903 else
32905 name_buf[0] = '_';
32906 strcpy (name_buf+1, name);
32908 strcpy (tmp_buf, "\n");
32909 strcat (tmp_buf, label);
32910 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
32911 if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
32912 dbxout_stabd (N_SLINE, bi->line_number);
32913 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
32914 if (flag_pic)
32916 if (TARGET_LINK_STACK)
32918 char name[32];
32919 get_ppc476_thunk_name (name);
32920 strcat (tmp_buf, ":\n\tmflr r0\n\tbl ");
32921 strcat (tmp_buf, name);
32922 strcat (tmp_buf, "\n");
32923 strcat (tmp_buf, label);
32924 strcat (tmp_buf, "_pic:\n\tmflr r11\n");
32926 else
32928 strcat (tmp_buf, ":\n\tmflr r0\n\tbcl 20,31,");
32929 strcat (tmp_buf, label);
32930 strcat (tmp_buf, "_pic\n");
32931 strcat (tmp_buf, label);
32932 strcat (tmp_buf, "_pic:\n\tmflr r11\n");
32935 strcat (tmp_buf, "\taddis r11,r11,ha16(");
32936 strcat (tmp_buf, name_buf);
32937 strcat (tmp_buf, " - ");
32938 strcat (tmp_buf, label);
32939 strcat (tmp_buf, "_pic)\n");
32941 strcat (tmp_buf, "\tmtlr r0\n");
32943 strcat (tmp_buf, "\taddi r12,r11,lo16(");
32944 strcat (tmp_buf, name_buf);
32945 strcat (tmp_buf, " - ");
32946 strcat (tmp_buf, label);
32947 strcat (tmp_buf, "_pic)\n");
32949 strcat (tmp_buf, "\tmtctr r12\n\tbctr\n");
32951 else
32953 strcat (tmp_buf, ":\n\tlis r12,hi16(");
32954 strcat (tmp_buf, name_buf);
32955 strcat (tmp_buf, ")\n\tori r12,r12,lo16(");
32956 strcat (tmp_buf, name_buf);
32957 strcat (tmp_buf, ")\n\tmtctr r12\n\tbctr");
32959 output_asm_insn (tmp_buf, 0);
32960 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
32961 if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
32962 dbxout_stabd (N_SLINE, bi->line_number);
32963 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
32964 branch_islands->pop ();
32968 /* NO_PREVIOUS_DEF checks in the link list whether the function name is
32969 already there or not. */
32971 static int
32972 no_previous_def (tree function_name)
32974 branch_island *bi;
32975 unsigned ix;
32977 FOR_EACH_VEC_SAFE_ELT (branch_islands, ix, bi)
32978 if (function_name == bi->function_name)
32979 return 0;
32980 return 1;
32983 /* GET_PREV_LABEL gets the label name from the previous definition of
32984 the function. */
32986 static tree
32987 get_prev_label (tree function_name)
32989 branch_island *bi;
32990 unsigned ix;
32992 FOR_EACH_VEC_SAFE_ELT (branch_islands, ix, bi)
32993 if (function_name == bi->function_name)
32994 return bi->label_name;
32995 return NULL_TREE;
32998 /* Generate PIC and indirect symbol stubs. */
33000 void
33001 machopic_output_stub (FILE *file, const char *symb, const char *stub)
33003 unsigned int length;
33004 char *symbol_name, *lazy_ptr_name;
33005 char *local_label_0;
33006 static unsigned label = 0;
33008 /* Lose our funky encoding stuff so it doesn't contaminate the stub. */
33009 symb = (*targetm.strip_name_encoding) (symb);
33012 length = strlen (symb);
33013 symbol_name = XALLOCAVEC (char, length + 32);
33014 GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
33016 lazy_ptr_name = XALLOCAVEC (char, length + 32);
33017 GEN_LAZY_PTR_NAME_FOR_SYMBOL (lazy_ptr_name, symb, length);
33019 if (flag_pic == 2)
33020 switch_to_section (darwin_sections[machopic_picsymbol_stub1_section]);
33021 else
33022 switch_to_section (darwin_sections[machopic_symbol_stub1_section]);
33024 if (flag_pic == 2)
33026 fprintf (file, "\t.align 5\n");
33028 fprintf (file, "%s:\n", stub);
33029 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
33031 label++;
33032 local_label_0 = XALLOCAVEC (char, 16);
33033 sprintf (local_label_0, "L%u$spb", label);
33035 fprintf (file, "\tmflr r0\n");
33036 if (TARGET_LINK_STACK)
33038 char name[32];
33039 get_ppc476_thunk_name (name);
33040 fprintf (file, "\tbl %s\n", name);
33041 fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
33043 else
33045 fprintf (file, "\tbcl 20,31,%s\n", local_label_0);
33046 fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
33048 fprintf (file, "\taddis r11,r11,ha16(%s-%s)\n",
33049 lazy_ptr_name, local_label_0);
33050 fprintf (file, "\tmtlr r0\n");
33051 fprintf (file, "\t%s r12,lo16(%s-%s)(r11)\n",
33052 (TARGET_64BIT ? "ldu" : "lwzu"),
33053 lazy_ptr_name, local_label_0);
33054 fprintf (file, "\tmtctr r12\n");
33055 fprintf (file, "\tbctr\n");
33057 else
33059 fprintf (file, "\t.align 4\n");
33061 fprintf (file, "%s:\n", stub);
33062 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
33064 fprintf (file, "\tlis r11,ha16(%s)\n", lazy_ptr_name);
33065 fprintf (file, "\t%s r12,lo16(%s)(r11)\n",
33066 (TARGET_64BIT ? "ldu" : "lwzu"),
33067 lazy_ptr_name);
33068 fprintf (file, "\tmtctr r12\n");
33069 fprintf (file, "\tbctr\n");
33072 switch_to_section (darwin_sections[machopic_lazy_symbol_ptr_section]);
33073 fprintf (file, "%s:\n", lazy_ptr_name);
33074 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
33075 fprintf (file, "%sdyld_stub_binding_helper\n",
33076 (TARGET_64BIT ? DOUBLE_INT_ASM_OP : "\t.long\t"));
33079 /* Legitimize PIC addresses. If the address is already
33080 position-independent, we return ORIG. Newly generated
33081 position-independent addresses go into a reg. This is REG if non
33082 zero, otherwise we allocate register(s) as necessary. */
33084 #define SMALL_INT(X) ((UINTVAL (X) + 0x8000) < 0x10000)
33087 rs6000_machopic_legitimize_pic_address (rtx orig, machine_mode mode,
33088 rtx reg)
33090 rtx base, offset;
33092 if (reg == NULL && !reload_completed)
33093 reg = gen_reg_rtx (Pmode);
33095 if (GET_CODE (orig) == CONST)
33097 rtx reg_temp;
33099 if (GET_CODE (XEXP (orig, 0)) == PLUS
33100 && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
33101 return orig;
33103 gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
33105 /* Use a different reg for the intermediate value, as
33106 it will be marked UNCHANGING. */
33107 reg_temp = !can_create_pseudo_p () ? reg : gen_reg_rtx (Pmode);
33108 base = rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 0),
33109 Pmode, reg_temp);
33110 offset =
33111 rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 1),
33112 Pmode, reg);
33114 if (CONST_INT_P (offset))
33116 if (SMALL_INT (offset))
33117 return plus_constant (Pmode, base, INTVAL (offset));
33118 else if (!reload_completed)
33119 offset = force_reg (Pmode, offset);
33120 else
33122 rtx mem = force_const_mem (Pmode, orig);
33123 return machopic_legitimize_pic_address (mem, Pmode, reg);
33126 return gen_rtx_PLUS (Pmode, base, offset);
33129 /* Fall back on generic machopic code. */
33130 return machopic_legitimize_pic_address (orig, mode, reg);
33133 /* Output a .machine directive for the Darwin assembler, and call
33134 the generic start_file routine. */
33136 static void
33137 rs6000_darwin_file_start (void)
33139 static const struct
33141 const char *arg;
33142 const char *name;
33143 HOST_WIDE_INT if_set;
33144 } mapping[] = {
33145 { "ppc64", "ppc64", MASK_64BIT },
33146 { "970", "ppc970", MASK_PPC_GPOPT | MASK_MFCRF | MASK_POWERPC64 },
33147 { "power4", "ppc970", 0 },
33148 { "G5", "ppc970", 0 },
33149 { "7450", "ppc7450", 0 },
33150 { "7400", "ppc7400", MASK_ALTIVEC },
33151 { "G4", "ppc7400", 0 },
33152 { "750", "ppc750", 0 },
33153 { "740", "ppc750", 0 },
33154 { "G3", "ppc750", 0 },
33155 { "604e", "ppc604e", 0 },
33156 { "604", "ppc604", 0 },
33157 { "603e", "ppc603", 0 },
33158 { "603", "ppc603", 0 },
33159 { "601", "ppc601", 0 },
33160 { NULL, "ppc", 0 } };
33161 const char *cpu_id = "";
33162 size_t i;
33164 rs6000_file_start ();
33165 darwin_file_start ();
33167 /* Determine the argument to -mcpu=. Default to G3 if not specified. */
33169 if (rs6000_default_cpu != 0 && rs6000_default_cpu[0] != '\0')
33170 cpu_id = rs6000_default_cpu;
33172 if (global_options_set.x_rs6000_cpu_index)
33173 cpu_id = processor_target_table[rs6000_cpu_index].name;
33175 /* Look through the mapping array. Pick the first name that either
33176 matches the argument, has a bit set in IF_SET that is also set
33177 in the target flags, or has a NULL name. */
33179 i = 0;
33180 while (mapping[i].arg != NULL
33181 && strcmp (mapping[i].arg, cpu_id) != 0
33182 && (mapping[i].if_set & rs6000_isa_flags) == 0)
33183 i++;
33185 fprintf (asm_out_file, "\t.machine %s\n", mapping[i].name);
33188 #endif /* TARGET_MACHO */
33190 #if TARGET_ELF
33191 static int
33192 rs6000_elf_reloc_rw_mask (void)
33194 if (flag_pic)
33195 return 3;
33196 else if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
33197 return 2;
33198 else
33199 return 0;
33202 /* Record an element in the table of global constructors. SYMBOL is
33203 a SYMBOL_REF of the function to be called; PRIORITY is a number
33204 between 0 and MAX_INIT_PRIORITY.
33206 This differs from default_named_section_asm_out_constructor in
33207 that we have special handling for -mrelocatable. */
33209 static void rs6000_elf_asm_out_constructor (rtx, int) ATTRIBUTE_UNUSED;
33210 static void
33211 rs6000_elf_asm_out_constructor (rtx symbol, int priority)
33213 const char *section = ".ctors";
33214 char buf[18];
33216 if (priority != DEFAULT_INIT_PRIORITY)
33218 sprintf (buf, ".ctors.%.5u",
33219 /* Invert the numbering so the linker puts us in the proper
33220 order; constructors are run from right to left, and the
33221 linker sorts in increasing order. */
33222 MAX_INIT_PRIORITY - priority);
33223 section = buf;
33226 switch_to_section (get_section (section, SECTION_WRITE, NULL));
33227 assemble_align (POINTER_SIZE);
33229 if (DEFAULT_ABI == ABI_V4
33230 && (TARGET_RELOCATABLE || flag_pic > 1))
33232 fputs ("\t.long (", asm_out_file);
33233 output_addr_const (asm_out_file, symbol);
33234 fputs (")@fixup\n", asm_out_file);
33236 else
33237 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
33240 static void rs6000_elf_asm_out_destructor (rtx, int) ATTRIBUTE_UNUSED;
33241 static void
33242 rs6000_elf_asm_out_destructor (rtx symbol, int priority)
33244 const char *section = ".dtors";
33245 char buf[18];
33247 if (priority != DEFAULT_INIT_PRIORITY)
33249 sprintf (buf, ".dtors.%.5u",
33250 /* Invert the numbering so the linker puts us in the proper
33251 order; constructors are run from right to left, and the
33252 linker sorts in increasing order. */
33253 MAX_INIT_PRIORITY - priority);
33254 section = buf;
33257 switch_to_section (get_section (section, SECTION_WRITE, NULL));
33258 assemble_align (POINTER_SIZE);
33260 if (DEFAULT_ABI == ABI_V4
33261 && (TARGET_RELOCATABLE || flag_pic > 1))
33263 fputs ("\t.long (", asm_out_file);
33264 output_addr_const (asm_out_file, symbol);
33265 fputs (")@fixup\n", asm_out_file);
33267 else
33268 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
33271 void
33272 rs6000_elf_declare_function_name (FILE *file, const char *name, tree decl)
33274 if (TARGET_64BIT && DEFAULT_ABI != ABI_ELFv2)
33276 fputs ("\t.section\t\".opd\",\"aw\"\n\t.align 3\n", file);
33277 ASM_OUTPUT_LABEL (file, name);
33278 fputs (DOUBLE_INT_ASM_OP, file);
33279 rs6000_output_function_entry (file, name);
33280 fputs (",.TOC.@tocbase,0\n\t.previous\n", file);
33281 if (DOT_SYMBOLS)
33283 fputs ("\t.size\t", file);
33284 assemble_name (file, name);
33285 fputs (",24\n\t.type\t.", file);
33286 assemble_name (file, name);
33287 fputs (",@function\n", file);
33288 if (TREE_PUBLIC (decl) && ! DECL_WEAK (decl))
33290 fputs ("\t.globl\t.", file);
33291 assemble_name (file, name);
33292 putc ('\n', file);
33295 else
33296 ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
33297 ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
33298 rs6000_output_function_entry (file, name);
33299 fputs (":\n", file);
33300 return;
33303 int uses_toc;
33304 if (DEFAULT_ABI == ABI_V4
33305 && (TARGET_RELOCATABLE || flag_pic > 1)
33306 && !TARGET_SECURE_PLT
33307 && (!constant_pool_empty_p () || crtl->profile)
33308 && (uses_toc = uses_TOC ()))
33310 char buf[256];
33312 if (uses_toc == 2)
33313 switch_to_other_text_partition ();
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);
33323 if (uses_toc == 2)
33324 switch_to_other_text_partition ();
33327 ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
33328 ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
33330 if (TARGET_CMODEL == CMODEL_LARGE && rs6000_global_entry_point_needed_p ())
33332 char buf[256];
33334 (*targetm.asm_out.internal_label) (file, "LCL", rs6000_pic_labelno);
33336 fprintf (file, "\t.quad .TOC.-");
33337 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
33338 assemble_name (file, buf);
33339 putc ('\n', file);
33342 if (DEFAULT_ABI == ABI_AIX)
33344 const char *desc_name, *orig_name;
33346 orig_name = (*targetm.strip_name_encoding) (name);
33347 desc_name = orig_name;
33348 while (*desc_name == '.')
33349 desc_name++;
33351 if (TREE_PUBLIC (decl))
33352 fprintf (file, "\t.globl %s\n", desc_name);
33354 fprintf (file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
33355 fprintf (file, "%s:\n", desc_name);
33356 fprintf (file, "\t.long %s\n", orig_name);
33357 fputs ("\t.long _GLOBAL_OFFSET_TABLE_\n", file);
33358 fputs ("\t.long 0\n", file);
33359 fprintf (file, "\t.previous\n");
33361 ASM_OUTPUT_LABEL (file, name);
33364 static void rs6000_elf_file_end (void) ATTRIBUTE_UNUSED;
33365 static void
33366 rs6000_elf_file_end (void)
33368 #ifdef HAVE_AS_GNU_ATTRIBUTE
33369 /* ??? The value emitted depends on options active at file end.
33370 Assume anyone using #pragma or attributes that might change
33371 options knows what they are doing. */
33372 if ((TARGET_64BIT || DEFAULT_ABI == ABI_V4)
33373 && rs6000_passes_float)
33375 int fp;
33377 if (TARGET_HARD_FLOAT)
33378 fp = 1;
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 read_only_private_data_section
33524 = get_unnamed_section (0, rs6000_xcoff_output_readonly_section_asm_op,
33525 &xcoff_private_rodata_section_name);
33527 tls_data_section
33528 = get_unnamed_section (SECTION_TLS,
33529 rs6000_xcoff_output_tls_section_asm_op,
33530 &xcoff_tls_data_section_name);
33532 tls_private_data_section
33533 = get_unnamed_section (SECTION_TLS,
33534 rs6000_xcoff_output_tls_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_private_rodata_section_name,
33718 main_input_filename, ".rop_");
33719 rs6000_gen_section_name (&xcoff_read_only_section_name,
33720 main_input_filename, ".ro_");
33721 rs6000_gen_section_name (&xcoff_tls_data_section_name,
33722 main_input_filename, ".tls_");
33723 rs6000_gen_section_name (&xcoff_tbss_section_name,
33724 main_input_filename, ".tbss_[UL]");
33726 fputs ("\t.file\t", asm_out_file);
33727 output_quoted_string (asm_out_file, main_input_filename);
33728 fputc ('\n', asm_out_file);
33729 if (write_symbols != NO_DEBUG)
33730 switch_to_section (private_data_section);
33731 switch_to_section (toc_section);
33732 switch_to_section (text_section);
33733 if (profile_flag)
33734 fprintf (asm_out_file, "\t.extern %s\n", RS6000_MCOUNT);
33735 rs6000_file_start ();
33738 /* Output at end of assembler file.
33739 On the RS/6000, referencing data should automatically pull in text. */
33741 static void
33742 rs6000_xcoff_file_end (void)
33744 switch_to_section (text_section);
33745 fputs ("_section_.text:\n", asm_out_file);
33746 switch_to_section (data_section);
33747 fputs (TARGET_32BIT
33748 ? "\t.long _section_.text\n" : "\t.llong _section_.text\n",
33749 asm_out_file);
33752 struct declare_alias_data
33754 FILE *file;
33755 bool function_descriptor;
33758 /* Declare alias N. A helper function for for_node_and_aliases. */
33760 static bool
33761 rs6000_declare_alias (struct symtab_node *n, void *d)
33763 struct declare_alias_data *data = (struct declare_alias_data *)d;
33764 /* Main symbol is output specially, because varasm machinery does part of
33765 the job for us - we do not need to declare .globl/lglobs and such. */
33766 if (!n->alias || n->weakref)
33767 return false;
33769 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (n->decl)))
33770 return false;
33772 /* Prevent assemble_alias from trying to use .set pseudo operation
33773 that does not behave as expected by the middle-end. */
33774 TREE_ASM_WRITTEN (n->decl) = true;
33776 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n->decl));
33777 char *buffer = (char *) alloca (strlen (name) + 2);
33778 char *p;
33779 int dollar_inside = 0;
33781 strcpy (buffer, name);
33782 p = strchr (buffer, '$');
33783 while (p) {
33784 *p = '_';
33785 dollar_inside++;
33786 p = strchr (p + 1, '$');
33788 if (TREE_PUBLIC (n->decl))
33790 if (!RS6000_WEAK || !DECL_WEAK (n->decl))
33792 if (dollar_inside) {
33793 if (data->function_descriptor)
33794 fprintf(data->file, "\t.rename .%s,\".%s\"\n", buffer, name);
33795 fprintf(data->file, "\t.rename %s,\"%s\"\n", buffer, name);
33797 if (data->function_descriptor)
33799 fputs ("\t.globl .", data->file);
33800 RS6000_OUTPUT_BASENAME (data->file, buffer);
33801 putc ('\n', data->file);
33803 fputs ("\t.globl ", data->file);
33804 RS6000_OUTPUT_BASENAME (data->file, buffer);
33805 putc ('\n', data->file);
33807 #ifdef ASM_WEAKEN_DECL
33808 else if (DECL_WEAK (n->decl) && !data->function_descriptor)
33809 ASM_WEAKEN_DECL (data->file, n->decl, name, NULL);
33810 #endif
33812 else
33814 if (dollar_inside)
33816 if (data->function_descriptor)
33817 fprintf(data->file, "\t.rename .%s,\".%s\"\n", buffer, name);
33818 fprintf(data->file, "\t.rename %s,\"%s\"\n", buffer, name);
33820 if (data->function_descriptor)
33822 fputs ("\t.lglobl .", data->file);
33823 RS6000_OUTPUT_BASENAME (data->file, buffer);
33824 putc ('\n', data->file);
33826 fputs ("\t.lglobl ", data->file);
33827 RS6000_OUTPUT_BASENAME (data->file, buffer);
33828 putc ('\n', data->file);
33830 if (data->function_descriptor)
33831 fputs (".", data->file);
33832 RS6000_OUTPUT_BASENAME (data->file, buffer);
33833 fputs (":\n", data->file);
33834 return false;
33838 #ifdef HAVE_GAS_HIDDEN
33839 /* Helper function to calculate visibility of a DECL
33840 and return the value as a const string. */
33842 static const char *
33843 rs6000_xcoff_visibility (tree decl)
33845 static const char * const visibility_types[] = {
33846 "", ",protected", ",hidden", ",internal"
33849 enum symbol_visibility vis = DECL_VISIBILITY (decl);
33850 return visibility_types[vis];
33852 #endif
33855 /* This macro produces the initial definition of a function name.
33856 On the RS/6000, we need to place an extra '.' in the function name and
33857 output the function descriptor.
33858 Dollar signs are converted to underscores.
33860 The csect for the function will have already been created when
33861 text_section was selected. We do have to go back to that csect, however.
33863 The third and fourth parameters to the .function pseudo-op (16 and 044)
33864 are placeholders which no longer have any use.
33866 Because AIX assembler's .set command has unexpected semantics, we output
33867 all aliases as alternative labels in front of the definition. */
33869 void
33870 rs6000_xcoff_declare_function_name (FILE *file, const char *name, tree decl)
33872 char *buffer = (char *) alloca (strlen (name) + 1);
33873 char *p;
33874 int dollar_inside = 0;
33875 struct declare_alias_data data = {file, false};
33877 strcpy (buffer, name);
33878 p = strchr (buffer, '$');
33879 while (p) {
33880 *p = '_';
33881 dollar_inside++;
33882 p = strchr (p + 1, '$');
33884 if (TREE_PUBLIC (decl))
33886 if (!RS6000_WEAK || !DECL_WEAK (decl))
33888 if (dollar_inside) {
33889 fprintf(file, "\t.rename .%s,\".%s\"\n", buffer, name);
33890 fprintf(file, "\t.rename %s,\"%s\"\n", buffer, name);
33892 fputs ("\t.globl .", file);
33893 RS6000_OUTPUT_BASENAME (file, buffer);
33894 #ifdef HAVE_GAS_HIDDEN
33895 fputs (rs6000_xcoff_visibility (decl), file);
33896 #endif
33897 putc ('\n', file);
33900 else
33902 if (dollar_inside) {
33903 fprintf(file, "\t.rename .%s,\".%s\"\n", buffer, name);
33904 fprintf(file, "\t.rename %s,\"%s\"\n", buffer, name);
33906 fputs ("\t.lglobl .", file);
33907 RS6000_OUTPUT_BASENAME (file, buffer);
33908 putc ('\n', file);
33910 fputs ("\t.csect ", file);
33911 RS6000_OUTPUT_BASENAME (file, buffer);
33912 fputs (TARGET_32BIT ? "[DS]\n" : "[DS],3\n", file);
33913 RS6000_OUTPUT_BASENAME (file, buffer);
33914 fputs (":\n", file);
33915 symtab_node::get (decl)->call_for_symbol_and_aliases (rs6000_declare_alias,
33916 &data, true);
33917 fputs (TARGET_32BIT ? "\t.long ." : "\t.llong .", file);
33918 RS6000_OUTPUT_BASENAME (file, buffer);
33919 fputs (", TOC[tc0], 0\n", file);
33920 in_section = NULL;
33921 switch_to_section (function_section (decl));
33922 putc ('.', file);
33923 RS6000_OUTPUT_BASENAME (file, buffer);
33924 fputs (":\n", file);
33925 data.function_descriptor = true;
33926 symtab_node::get (decl)->call_for_symbol_and_aliases (rs6000_declare_alias,
33927 &data, true);
33928 if (!DECL_IGNORED_P (decl))
33930 if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
33931 xcoffout_declare_function (file, decl, buffer);
33932 else if (write_symbols == DWARF2_DEBUG)
33934 name = (*targetm.strip_name_encoding) (name);
33935 fprintf (file, "\t.function .%s,.%s,2,0\n", name, name);
33938 return;
33942 /* Output assembly language to globalize a symbol from a DECL,
33943 possibly with visibility. */
33945 void
33946 rs6000_xcoff_asm_globalize_decl_name (FILE *stream, tree decl)
33948 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
33949 fputs (GLOBAL_ASM_OP, stream);
33950 RS6000_OUTPUT_BASENAME (stream, name);
33951 #ifdef HAVE_GAS_HIDDEN
33952 fputs (rs6000_xcoff_visibility (decl), stream);
33953 #endif
33954 putc ('\n', stream);
33957 /* Output assembly language to define a symbol as COMMON from a DECL,
33958 possibly with visibility. */
33960 void
33961 rs6000_xcoff_asm_output_aligned_decl_common (FILE *stream,
33962 tree decl ATTRIBUTE_UNUSED,
33963 const char *name,
33964 unsigned HOST_WIDE_INT size,
33965 unsigned HOST_WIDE_INT align)
33967 unsigned HOST_WIDE_INT align2 = 2;
33969 if (align > 32)
33970 align2 = floor_log2 (align / BITS_PER_UNIT);
33971 else if (size > 4)
33972 align2 = 3;
33974 fputs (COMMON_ASM_OP, stream);
33975 RS6000_OUTPUT_BASENAME (stream, name);
33977 fprintf (stream,
33978 "," HOST_WIDE_INT_PRINT_UNSIGNED "," HOST_WIDE_INT_PRINT_UNSIGNED,
33979 size, align2);
33981 #ifdef HAVE_GAS_HIDDEN
33982 if (decl != NULL)
33983 fputs (rs6000_xcoff_visibility (decl), stream);
33984 #endif
33985 putc ('\n', stream);
33988 /* This macro produces the initial definition of a object (variable) name.
33989 Because AIX assembler's .set command has unexpected semantics, we output
33990 all aliases as alternative labels in front of the definition. */
33992 void
33993 rs6000_xcoff_declare_object_name (FILE *file, const char *name, tree decl)
33995 struct declare_alias_data data = {file, false};
33996 RS6000_OUTPUT_BASENAME (file, name);
33997 fputs (":\n", file);
33998 symtab_node::get_create (decl)->call_for_symbol_and_aliases (rs6000_declare_alias,
33999 &data, true);
34002 /* Overide the default 'SYMBOL-.' syntax with AIX compatible 'SYMBOL-$'. */
34004 void
34005 rs6000_asm_output_dwarf_pcrel (FILE *file, int size, const char *label)
34007 fputs (integer_asm_op (size, FALSE), file);
34008 assemble_name (file, label);
34009 fputs ("-$", file);
34012 /* Output a symbol offset relative to the dbase for the current object.
34013 We use __gcc_unwind_dbase as an arbitrary base for dbase and assume
34014 signed offsets.
34016 __gcc_unwind_dbase is embedded in all executables/libraries through
34017 libgcc/config/rs6000/crtdbase.S. */
34019 void
34020 rs6000_asm_output_dwarf_datarel (FILE *file, int size, const char *label)
34022 fputs (integer_asm_op (size, FALSE), file);
34023 assemble_name (file, label);
34024 fputs("-__gcc_unwind_dbase", file);
34027 #ifdef HAVE_AS_TLS
34028 static void
34029 rs6000_xcoff_encode_section_info (tree decl, rtx rtl, int first)
34031 rtx symbol;
34032 int flags;
34033 const char *symname;
34035 default_encode_section_info (decl, rtl, first);
34037 /* Careful not to prod global register variables. */
34038 if (!MEM_P (rtl))
34039 return;
34040 symbol = XEXP (rtl, 0);
34041 if (!SYMBOL_REF_P (symbol))
34042 return;
34044 flags = SYMBOL_REF_FLAGS (symbol);
34046 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
34047 flags &= ~SYMBOL_FLAG_HAS_BLOCK_INFO;
34049 SYMBOL_REF_FLAGS (symbol) = flags;
34051 /* Append mapping class to extern decls. */
34052 symname = XSTR (symbol, 0);
34053 if (decl /* sync condition with assemble_external () */
34054 && DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl)
34055 && ((TREE_CODE (decl) == VAR_DECL && !DECL_THREAD_LOCAL_P (decl))
34056 || TREE_CODE (decl) == FUNCTION_DECL)
34057 && symname[strlen (symname) - 1] != ']')
34059 char *newname = (char *) alloca (strlen (symname) + 5);
34060 strcpy (newname, symname);
34061 strcat (newname, (TREE_CODE (decl) == FUNCTION_DECL
34062 ? "[DS]" : "[UA]"));
34063 XSTR (symbol, 0) = ggc_strdup (newname);
34066 #endif /* HAVE_AS_TLS */
34067 #endif /* TARGET_XCOFF */
34069 void
34070 rs6000_asm_weaken_decl (FILE *stream, tree decl,
34071 const char *name, const char *val)
34073 fputs ("\t.weak\t", stream);
34074 RS6000_OUTPUT_BASENAME (stream, name);
34075 if (decl && TREE_CODE (decl) == FUNCTION_DECL
34076 && DEFAULT_ABI == ABI_AIX && DOT_SYMBOLS)
34078 if (TARGET_XCOFF)
34079 fputs ("[DS]", stream);
34080 #if TARGET_XCOFF && HAVE_GAS_HIDDEN
34081 if (TARGET_XCOFF)
34082 fputs (rs6000_xcoff_visibility (decl), stream);
34083 #endif
34084 fputs ("\n\t.weak\t.", stream);
34085 RS6000_OUTPUT_BASENAME (stream, name);
34087 #if TARGET_XCOFF && HAVE_GAS_HIDDEN
34088 if (TARGET_XCOFF)
34089 fputs (rs6000_xcoff_visibility (decl), stream);
34090 #endif
34091 fputc ('\n', stream);
34092 if (val)
34094 #ifdef ASM_OUTPUT_DEF
34095 ASM_OUTPUT_DEF (stream, name, val);
34096 #endif
34097 if (decl && TREE_CODE (decl) == FUNCTION_DECL
34098 && DEFAULT_ABI == ABI_AIX && DOT_SYMBOLS)
34100 fputs ("\t.set\t.", stream);
34101 RS6000_OUTPUT_BASENAME (stream, name);
34102 fputs (",.", stream);
34103 RS6000_OUTPUT_BASENAME (stream, val);
34104 fputc ('\n', stream);
34110 /* Return true if INSN should not be copied. */
34112 static bool
34113 rs6000_cannot_copy_insn_p (rtx_insn *insn)
34115 return recog_memoized (insn) >= 0
34116 && get_attr_cannot_copy (insn);
34119 /* Compute a (partial) cost for rtx X. Return true if the complete
34120 cost has been computed, and false if subexpressions should be
34121 scanned. In either case, *TOTAL contains the cost result. */
34123 static bool
34124 rs6000_rtx_costs (rtx x, machine_mode mode, int outer_code,
34125 int opno ATTRIBUTE_UNUSED, int *total, bool speed)
34127 int code = GET_CODE (x);
34129 switch (code)
34131 /* On the RS/6000, if it is valid in the insn, it is free. */
34132 case CONST_INT:
34133 if (((outer_code == SET
34134 || outer_code == PLUS
34135 || outer_code == MINUS)
34136 && (satisfies_constraint_I (x)
34137 || satisfies_constraint_L (x)))
34138 || (outer_code == AND
34139 && (satisfies_constraint_K (x)
34140 || (mode == SImode
34141 ? satisfies_constraint_L (x)
34142 : satisfies_constraint_J (x))))
34143 || ((outer_code == IOR || outer_code == XOR)
34144 && (satisfies_constraint_K (x)
34145 || (mode == SImode
34146 ? satisfies_constraint_L (x)
34147 : satisfies_constraint_J (x))))
34148 || outer_code == ASHIFT
34149 || outer_code == ASHIFTRT
34150 || outer_code == LSHIFTRT
34151 || outer_code == ROTATE
34152 || outer_code == ROTATERT
34153 || outer_code == ZERO_EXTRACT
34154 || (outer_code == MULT
34155 && satisfies_constraint_I (x))
34156 || ((outer_code == DIV || outer_code == UDIV
34157 || outer_code == MOD || outer_code == UMOD)
34158 && exact_log2 (INTVAL (x)) >= 0)
34159 || (outer_code == COMPARE
34160 && (satisfies_constraint_I (x)
34161 || satisfies_constraint_K (x)))
34162 || ((outer_code == EQ || outer_code == NE)
34163 && (satisfies_constraint_I (x)
34164 || satisfies_constraint_K (x)
34165 || (mode == SImode
34166 ? satisfies_constraint_L (x)
34167 : satisfies_constraint_J (x))))
34168 || (outer_code == GTU
34169 && satisfies_constraint_I (x))
34170 || (outer_code == LTU
34171 && satisfies_constraint_P (x)))
34173 *total = 0;
34174 return true;
34176 else if ((outer_code == PLUS
34177 && reg_or_add_cint_operand (x, VOIDmode))
34178 || (outer_code == MINUS
34179 && reg_or_sub_cint_operand (x, VOIDmode))
34180 || ((outer_code == SET
34181 || outer_code == IOR
34182 || outer_code == XOR)
34183 && (INTVAL (x)
34184 & ~ (unsigned HOST_WIDE_INT) 0xffffffff) == 0))
34186 *total = COSTS_N_INSNS (1);
34187 return true;
34189 /* FALLTHRU */
34191 case CONST_DOUBLE:
34192 case CONST_WIDE_INT:
34193 case CONST:
34194 case HIGH:
34195 case SYMBOL_REF:
34196 *total = !speed ? COSTS_N_INSNS (1) + 1 : COSTS_N_INSNS (2);
34197 return true;
34199 case MEM:
34200 /* When optimizing for size, MEM should be slightly more expensive
34201 than generating address, e.g., (plus (reg) (const)).
34202 L1 cache latency is about two instructions. */
34203 *total = !speed ? COSTS_N_INSNS (1) + 1 : COSTS_N_INSNS (2);
34204 if (rs6000_slow_unaligned_access (mode, MEM_ALIGN (x)))
34205 *total += COSTS_N_INSNS (100);
34206 return true;
34208 case LABEL_REF:
34209 *total = 0;
34210 return true;
34212 case PLUS:
34213 case MINUS:
34214 if (FLOAT_MODE_P (mode))
34215 *total = rs6000_cost->fp;
34216 else
34217 *total = COSTS_N_INSNS (1);
34218 return false;
34220 case MULT:
34221 if (CONST_INT_P (XEXP (x, 1))
34222 && satisfies_constraint_I (XEXP (x, 1)))
34224 if (INTVAL (XEXP (x, 1)) >= -256
34225 && INTVAL (XEXP (x, 1)) <= 255)
34226 *total = rs6000_cost->mulsi_const9;
34227 else
34228 *total = rs6000_cost->mulsi_const;
34230 else if (mode == SFmode)
34231 *total = rs6000_cost->fp;
34232 else if (FLOAT_MODE_P (mode))
34233 *total = rs6000_cost->dmul;
34234 else if (mode == DImode)
34235 *total = rs6000_cost->muldi;
34236 else
34237 *total = rs6000_cost->mulsi;
34238 return false;
34240 case FMA:
34241 if (mode == SFmode)
34242 *total = rs6000_cost->fp;
34243 else
34244 *total = rs6000_cost->dmul;
34245 break;
34247 case DIV:
34248 case MOD:
34249 if (FLOAT_MODE_P (mode))
34251 *total = mode == DFmode ? rs6000_cost->ddiv
34252 : rs6000_cost->sdiv;
34253 return false;
34255 /* FALLTHRU */
34257 case UDIV:
34258 case UMOD:
34259 if (CONST_INT_P (XEXP (x, 1))
34260 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0)
34262 if (code == DIV || code == MOD)
34263 /* Shift, addze */
34264 *total = COSTS_N_INSNS (2);
34265 else
34266 /* Shift */
34267 *total = COSTS_N_INSNS (1);
34269 else
34271 if (GET_MODE (XEXP (x, 1)) == DImode)
34272 *total = rs6000_cost->divdi;
34273 else
34274 *total = rs6000_cost->divsi;
34276 /* Add in shift and subtract for MOD unless we have a mod instruction. */
34277 if (!TARGET_MODULO && (code == MOD || code == UMOD))
34278 *total += COSTS_N_INSNS (2);
34279 return false;
34281 case CTZ:
34282 *total = COSTS_N_INSNS (TARGET_CTZ ? 1 : 4);
34283 return false;
34285 case FFS:
34286 *total = COSTS_N_INSNS (4);
34287 return false;
34289 case POPCOUNT:
34290 *total = COSTS_N_INSNS (TARGET_POPCNTD ? 1 : 6);
34291 return false;
34293 case PARITY:
34294 *total = COSTS_N_INSNS (TARGET_CMPB ? 2 : 6);
34295 return false;
34297 case NOT:
34298 if (outer_code == AND || outer_code == IOR || outer_code == XOR)
34299 *total = 0;
34300 else
34301 *total = COSTS_N_INSNS (1);
34302 return false;
34304 case AND:
34305 if (CONST_INT_P (XEXP (x, 1)))
34307 rtx left = XEXP (x, 0);
34308 rtx_code left_code = GET_CODE (left);
34310 /* rotate-and-mask: 1 insn. */
34311 if ((left_code == ROTATE
34312 || left_code == ASHIFT
34313 || left_code == LSHIFTRT)
34314 && rs6000_is_valid_shift_mask (XEXP (x, 1), left, mode))
34316 *total = rtx_cost (XEXP (left, 0), mode, left_code, 0, speed);
34317 if (!CONST_INT_P (XEXP (left, 1)))
34318 *total += rtx_cost (XEXP (left, 1), SImode, left_code, 1, speed);
34319 *total += COSTS_N_INSNS (1);
34320 return true;
34323 /* rotate-and-mask (no rotate), andi., andis.: 1 insn. */
34324 HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
34325 if (rs6000_is_valid_and_mask (XEXP (x, 1), mode)
34326 || (val & 0xffff) == val
34327 || (val & 0xffff0000) == val
34328 || ((val & 0xffff) == 0 && mode == SImode))
34330 *total = rtx_cost (left, mode, AND, 0, speed);
34331 *total += COSTS_N_INSNS (1);
34332 return true;
34335 /* 2 insns. */
34336 if (rs6000_is_valid_2insn_and (XEXP (x, 1), mode))
34338 *total = rtx_cost (left, mode, AND, 0, speed);
34339 *total += COSTS_N_INSNS (2);
34340 return true;
34344 *total = COSTS_N_INSNS (1);
34345 return false;
34347 case IOR:
34348 /* FIXME */
34349 *total = COSTS_N_INSNS (1);
34350 return true;
34352 case CLZ:
34353 case XOR:
34354 case ZERO_EXTRACT:
34355 *total = COSTS_N_INSNS (1);
34356 return false;
34358 case ASHIFT:
34359 /* The EXTSWSLI instruction is a combined instruction. Don't count both
34360 the sign extend and shift separately within the insn. */
34361 if (TARGET_EXTSWSLI && mode == DImode
34362 && GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
34363 && GET_MODE (XEXP (XEXP (x, 0), 0)) == SImode)
34365 *total = 0;
34366 return false;
34368 /* fall through */
34370 case ASHIFTRT:
34371 case LSHIFTRT:
34372 case ROTATE:
34373 case ROTATERT:
34374 /* Handle mul_highpart. */
34375 if (outer_code == TRUNCATE
34376 && GET_CODE (XEXP (x, 0)) == MULT)
34378 if (mode == DImode)
34379 *total = rs6000_cost->muldi;
34380 else
34381 *total = rs6000_cost->mulsi;
34382 return true;
34384 else if (outer_code == AND)
34385 *total = 0;
34386 else
34387 *total = COSTS_N_INSNS (1);
34388 return false;
34390 case SIGN_EXTEND:
34391 case ZERO_EXTEND:
34392 if (MEM_P (XEXP (x, 0)))
34393 *total = 0;
34394 else
34395 *total = COSTS_N_INSNS (1);
34396 return false;
34398 case COMPARE:
34399 case NEG:
34400 case ABS:
34401 if (!FLOAT_MODE_P (mode))
34403 *total = COSTS_N_INSNS (1);
34404 return false;
34406 /* FALLTHRU */
34408 case FLOAT:
34409 case UNSIGNED_FLOAT:
34410 case FIX:
34411 case UNSIGNED_FIX:
34412 case FLOAT_TRUNCATE:
34413 *total = rs6000_cost->fp;
34414 return false;
34416 case FLOAT_EXTEND:
34417 if (mode == DFmode)
34418 *total = rs6000_cost->sfdf_convert;
34419 else
34420 *total = rs6000_cost->fp;
34421 return false;
34423 case UNSPEC:
34424 switch (XINT (x, 1))
34426 case UNSPEC_FRSP:
34427 *total = rs6000_cost->fp;
34428 return true;
34430 default:
34431 break;
34433 break;
34435 case CALL:
34436 case IF_THEN_ELSE:
34437 if (!speed)
34439 *total = COSTS_N_INSNS (1);
34440 return true;
34442 else if (FLOAT_MODE_P (mode) && TARGET_PPC_GFXOPT && TARGET_HARD_FLOAT)
34444 *total = rs6000_cost->fp;
34445 return false;
34447 break;
34449 case NE:
34450 case EQ:
34451 case GTU:
34452 case LTU:
34453 /* Carry bit requires mode == Pmode.
34454 NEG or PLUS already counted so only add one. */
34455 if (mode == Pmode
34456 && (outer_code == NEG || outer_code == PLUS))
34458 *total = COSTS_N_INSNS (1);
34459 return true;
34461 /* FALLTHRU */
34463 case GT:
34464 case LT:
34465 case UNORDERED:
34466 if (outer_code == SET)
34468 if (XEXP (x, 1) == const0_rtx)
34470 *total = COSTS_N_INSNS (2);
34471 return true;
34473 else
34475 *total = COSTS_N_INSNS (3);
34476 return false;
34479 /* CC COMPARE. */
34480 if (outer_code == COMPARE)
34482 *total = 0;
34483 return true;
34485 break;
34487 default:
34488 break;
34491 return false;
34494 /* Debug form of r6000_rtx_costs that is selected if -mdebug=cost. */
34496 static bool
34497 rs6000_debug_rtx_costs (rtx x, machine_mode mode, int outer_code,
34498 int opno, int *total, bool speed)
34500 bool ret = rs6000_rtx_costs (x, mode, outer_code, opno, total, speed);
34502 fprintf (stderr,
34503 "\nrs6000_rtx_costs, return = %s, mode = %s, outer_code = %s, "
34504 "opno = %d, total = %d, speed = %s, x:\n",
34505 ret ? "complete" : "scan inner",
34506 GET_MODE_NAME (mode),
34507 GET_RTX_NAME (outer_code),
34508 opno,
34509 *total,
34510 speed ? "true" : "false");
34512 debug_rtx (x);
34514 return ret;
34517 static int
34518 rs6000_insn_cost (rtx_insn *insn, bool speed)
34520 if (recog_memoized (insn) < 0)
34521 return 0;
34523 if (!speed)
34524 return get_attr_length (insn);
34526 int cost = get_attr_cost (insn);
34527 if (cost > 0)
34528 return cost;
34530 int n = get_attr_length (insn) / 4;
34531 enum attr_type type = get_attr_type (insn);
34533 switch (type)
34535 case TYPE_LOAD:
34536 case TYPE_FPLOAD:
34537 case TYPE_VECLOAD:
34538 cost = COSTS_N_INSNS (n + 1);
34539 break;
34541 case TYPE_MUL:
34542 switch (get_attr_size (insn))
34544 case SIZE_8:
34545 cost = COSTS_N_INSNS (n - 1) + rs6000_cost->mulsi_const9;
34546 break;
34547 case SIZE_16:
34548 cost = COSTS_N_INSNS (n - 1) + rs6000_cost->mulsi_const;
34549 break;
34550 case SIZE_32:
34551 cost = COSTS_N_INSNS (n - 1) + rs6000_cost->mulsi;
34552 break;
34553 case SIZE_64:
34554 cost = COSTS_N_INSNS (n - 1) + rs6000_cost->muldi;
34555 break;
34556 default:
34557 gcc_unreachable ();
34559 break;
34560 case TYPE_DIV:
34561 switch (get_attr_size (insn))
34563 case SIZE_32:
34564 cost = COSTS_N_INSNS (n - 1) + rs6000_cost->divsi;
34565 break;
34566 case SIZE_64:
34567 cost = COSTS_N_INSNS (n - 1) + rs6000_cost->divdi;
34568 break;
34569 default:
34570 gcc_unreachable ();
34572 break;
34574 case TYPE_FP:
34575 cost = n * rs6000_cost->fp;
34576 break;
34577 case TYPE_DMUL:
34578 cost = n * rs6000_cost->dmul;
34579 break;
34580 case TYPE_SDIV:
34581 cost = n * rs6000_cost->sdiv;
34582 break;
34583 case TYPE_DDIV:
34584 cost = n * rs6000_cost->ddiv;
34585 break;
34587 case TYPE_SYNC:
34588 case TYPE_LOAD_L:
34589 case TYPE_MFCR:
34590 case TYPE_MFCRF:
34591 cost = COSTS_N_INSNS (n + 2);
34592 break;
34594 default:
34595 cost = COSTS_N_INSNS (n);
34598 return cost;
34601 /* Debug form of ADDRESS_COST that is selected if -mdebug=cost. */
34603 static int
34604 rs6000_debug_address_cost (rtx x, machine_mode mode,
34605 addr_space_t as, bool speed)
34607 int ret = TARGET_ADDRESS_COST (x, mode, as, speed);
34609 fprintf (stderr, "\nrs6000_address_cost, return = %d, speed = %s, x:\n",
34610 ret, speed ? "true" : "false");
34611 debug_rtx (x);
34613 return ret;
34617 /* A C expression returning the cost of moving data from a register of class
34618 CLASS1 to one of CLASS2. */
34620 static int
34621 rs6000_register_move_cost (machine_mode mode,
34622 reg_class_t from, reg_class_t to)
34624 int ret;
34625 reg_class_t rclass;
34627 if (TARGET_DEBUG_COST)
34628 dbg_cost_ctrl++;
34630 /* If we have VSX, we can easily move between FPR or Altivec registers,
34631 otherwise we can only easily move within classes.
34632 Do this first so we give best-case answers for union classes
34633 containing both gprs and vsx regs. */
34634 HARD_REG_SET to_vsx, from_vsx;
34635 COPY_HARD_REG_SET (to_vsx, reg_class_contents[to]);
34636 AND_HARD_REG_SET (to_vsx, reg_class_contents[VSX_REGS]);
34637 COPY_HARD_REG_SET (from_vsx, reg_class_contents[from]);
34638 AND_HARD_REG_SET (from_vsx, reg_class_contents[VSX_REGS]);
34639 if (!hard_reg_set_empty_p (to_vsx)
34640 && !hard_reg_set_empty_p (from_vsx)
34641 && (TARGET_VSX
34642 || hard_reg_set_intersect_p (to_vsx, from_vsx)))
34644 int reg = FIRST_FPR_REGNO;
34645 if (TARGET_VSX
34646 || (TEST_HARD_REG_BIT (to_vsx, FIRST_ALTIVEC_REGNO)
34647 && TEST_HARD_REG_BIT (from_vsx, FIRST_ALTIVEC_REGNO)))
34648 reg = FIRST_ALTIVEC_REGNO;
34649 ret = 2 * hard_regno_nregs (reg, mode);
34652 /* Moves from/to GENERAL_REGS. */
34653 else if ((rclass = from, reg_classes_intersect_p (to, GENERAL_REGS))
34654 || (rclass = to, reg_classes_intersect_p (from, GENERAL_REGS)))
34656 if (rclass == FLOAT_REGS || rclass == ALTIVEC_REGS || rclass == VSX_REGS)
34658 if (TARGET_DIRECT_MOVE)
34660 /* Keep the cost for direct moves above that for within
34661 a register class even if the actual processor cost is
34662 comparable. We do this because a direct move insn
34663 can't be a nop, whereas with ideal register
34664 allocation a move within the same class might turn
34665 out to be a nop. */
34666 if (rs6000_tune == PROCESSOR_POWER9)
34667 ret = 3 * hard_regno_nregs (FIRST_GPR_REGNO, mode);
34668 else
34669 ret = 4 * hard_regno_nregs (FIRST_GPR_REGNO, mode);
34670 /* SFmode requires a conversion when moving between gprs
34671 and vsx. */
34672 if (mode == SFmode)
34673 ret += 2;
34675 else
34676 ret = (rs6000_memory_move_cost (mode, rclass, false)
34677 + rs6000_memory_move_cost (mode, GENERAL_REGS, false));
34680 /* It's more expensive to move CR_REGS than CR0_REGS because of the
34681 shift. */
34682 else if (rclass == CR_REGS)
34683 ret = 4;
34685 /* For those processors that have slow LR/CTR moves, make them more
34686 expensive than memory in order to bias spills to memory .*/
34687 else if ((rs6000_tune == PROCESSOR_POWER6
34688 || rs6000_tune == PROCESSOR_POWER7
34689 || rs6000_tune == PROCESSOR_POWER8
34690 || rs6000_tune == PROCESSOR_POWER9)
34691 && reg_class_subset_p (rclass, SPECIAL_REGS))
34692 ret = 6 * hard_regno_nregs (FIRST_GPR_REGNO, mode);
34694 else
34695 /* A move will cost one instruction per GPR moved. */
34696 ret = 2 * hard_regno_nregs (FIRST_GPR_REGNO, mode);
34699 /* Everything else has to go through GENERAL_REGS. */
34700 else
34701 ret = (rs6000_register_move_cost (mode, GENERAL_REGS, to)
34702 + rs6000_register_move_cost (mode, from, GENERAL_REGS));
34704 if (TARGET_DEBUG_COST)
34706 if (dbg_cost_ctrl == 1)
34707 fprintf (stderr,
34708 "rs6000_register_move_cost: ret=%d, mode=%s, from=%s, to=%s\n",
34709 ret, GET_MODE_NAME (mode), reg_class_names[from],
34710 reg_class_names[to]);
34711 dbg_cost_ctrl--;
34714 return ret;
34717 /* A C expressions returning the cost of moving data of MODE from a register to
34718 or from memory. */
34720 static int
34721 rs6000_memory_move_cost (machine_mode mode, reg_class_t rclass,
34722 bool in ATTRIBUTE_UNUSED)
34724 int ret;
34726 if (TARGET_DEBUG_COST)
34727 dbg_cost_ctrl++;
34729 if (reg_classes_intersect_p (rclass, GENERAL_REGS))
34730 ret = 4 * hard_regno_nregs (0, mode);
34731 else if ((reg_classes_intersect_p (rclass, FLOAT_REGS)
34732 || reg_classes_intersect_p (rclass, VSX_REGS)))
34733 ret = 4 * hard_regno_nregs (32, mode);
34734 else if (reg_classes_intersect_p (rclass, ALTIVEC_REGS))
34735 ret = 4 * hard_regno_nregs (FIRST_ALTIVEC_REGNO, mode);
34736 else
34737 ret = 4 + rs6000_register_move_cost (mode, rclass, GENERAL_REGS);
34739 if (TARGET_DEBUG_COST)
34741 if (dbg_cost_ctrl == 1)
34742 fprintf (stderr,
34743 "rs6000_memory_move_cost: ret=%d, mode=%s, rclass=%s, in=%d\n",
34744 ret, GET_MODE_NAME (mode), reg_class_names[rclass], in);
34745 dbg_cost_ctrl--;
34748 return ret;
34751 /* Implement TARGET_IRA_CHANGE_PSEUDO_ALLOCNO_CLASS.
34753 The register allocator chooses GEN_OR_VSX_REGS for the allocno
34754 class if GENERAL_REGS and VSX_REGS cost is lower than the memory
34755 cost. This happens a lot when TARGET_DIRECT_MOVE makes the register
34756 move cost between GENERAL_REGS and VSX_REGS low.
34758 It might seem reasonable to use a union class. After all, if usage
34759 of vsr is low and gpr high, it might make sense to spill gpr to vsr
34760 rather than memory. However, in cases where register pressure of
34761 both is high, like the cactus_adm spec test, allowing
34762 GEN_OR_VSX_REGS as the allocno class results in bad decisions in
34763 the first scheduling pass. This is partly due to an allocno of
34764 GEN_OR_VSX_REGS wrongly contributing to the GENERAL_REGS pressure
34765 class, which gives too high a pressure for GENERAL_REGS and too low
34766 for VSX_REGS. So, force a choice of the subclass here.
34768 The best class is also the union if GENERAL_REGS and VSX_REGS have
34769 the same cost. In that case we do use GEN_OR_VSX_REGS as the
34770 allocno class, since trying to narrow down the class by regno mode
34771 is prone to error. For example, SImode is allowed in VSX regs and
34772 in some cases (eg. gcc.target/powerpc/p9-xxbr-3.c do_bswap32_vect)
34773 it would be wrong to choose an allocno of GENERAL_REGS based on
34774 SImode. */
34776 static reg_class_t
34777 rs6000_ira_change_pseudo_allocno_class (int regno ATTRIBUTE_UNUSED,
34778 reg_class_t allocno_class,
34779 reg_class_t best_class)
34781 switch (allocno_class)
34783 case GEN_OR_VSX_REGS:
34784 /* best_class must be a subset of allocno_class. */
34785 gcc_checking_assert (best_class == GEN_OR_VSX_REGS
34786 || best_class == GEN_OR_FLOAT_REGS
34787 || best_class == VSX_REGS
34788 || best_class == ALTIVEC_REGS
34789 || best_class == FLOAT_REGS
34790 || best_class == GENERAL_REGS
34791 || best_class == BASE_REGS);
34792 /* Use best_class but choose wider classes when copying from the
34793 wider class to best_class is cheap. This mimics IRA choice
34794 of allocno class. */
34795 if (best_class == BASE_REGS)
34796 return GENERAL_REGS;
34797 if (TARGET_VSX
34798 && (best_class == FLOAT_REGS || best_class == ALTIVEC_REGS))
34799 return VSX_REGS;
34800 return best_class;
34802 default:
34803 break;
34806 return allocno_class;
34809 /* Returns a code for a target-specific builtin that implements
34810 reciprocal of the function, or NULL_TREE if not available. */
34812 static tree
34813 rs6000_builtin_reciprocal (tree fndecl)
34815 switch (DECL_FUNCTION_CODE (fndecl))
34817 case VSX_BUILTIN_XVSQRTDP:
34818 if (!RS6000_RECIP_AUTO_RSQRTE_P (V2DFmode))
34819 return NULL_TREE;
34821 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_2DF];
34823 case VSX_BUILTIN_XVSQRTSP:
34824 if (!RS6000_RECIP_AUTO_RSQRTE_P (V4SFmode))
34825 return NULL_TREE;
34827 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_4SF];
34829 default:
34830 return NULL_TREE;
34834 /* Load up a constant. If the mode is a vector mode, splat the value across
34835 all of the vector elements. */
34837 static rtx
34838 rs6000_load_constant_and_splat (machine_mode mode, REAL_VALUE_TYPE dconst)
34840 rtx reg;
34842 if (mode == SFmode || mode == DFmode)
34844 rtx d = const_double_from_real_value (dconst, mode);
34845 reg = force_reg (mode, d);
34847 else if (mode == V4SFmode)
34849 rtx d = const_double_from_real_value (dconst, SFmode);
34850 rtvec v = gen_rtvec (4, d, d, d, d);
34851 reg = gen_reg_rtx (mode);
34852 rs6000_expand_vector_init (reg, gen_rtx_PARALLEL (mode, v));
34854 else if (mode == V2DFmode)
34856 rtx d = const_double_from_real_value (dconst, DFmode);
34857 rtvec v = gen_rtvec (2, d, d);
34858 reg = gen_reg_rtx (mode);
34859 rs6000_expand_vector_init (reg, gen_rtx_PARALLEL (mode, v));
34861 else
34862 gcc_unreachable ();
34864 return reg;
34867 /* Generate an FMA instruction. */
34869 static void
34870 rs6000_emit_madd (rtx target, rtx m1, rtx m2, rtx a)
34872 machine_mode mode = GET_MODE (target);
34873 rtx dst;
34875 dst = expand_ternary_op (mode, fma_optab, m1, m2, a, target, 0);
34876 gcc_assert (dst != NULL);
34878 if (dst != target)
34879 emit_move_insn (target, dst);
34882 /* Generate a FNMSUB instruction: dst = -fma(m1, m2, -a). */
34884 static void
34885 rs6000_emit_nmsub (rtx dst, rtx m1, rtx m2, rtx a)
34887 machine_mode mode = GET_MODE (dst);
34888 rtx r;
34890 /* This is a tad more complicated, since the fnma_optab is for
34891 a different expression: fma(-m1, m2, a), which is the same
34892 thing except in the case of signed zeros.
34894 Fortunately we know that if FMA is supported that FNMSUB is
34895 also supported in the ISA. Just expand it directly. */
34897 gcc_assert (optab_handler (fma_optab, mode) != CODE_FOR_nothing);
34899 r = gen_rtx_NEG (mode, a);
34900 r = gen_rtx_FMA (mode, m1, m2, r);
34901 r = gen_rtx_NEG (mode, r);
34902 emit_insn (gen_rtx_SET (dst, r));
34905 /* Newton-Raphson approximation of floating point divide DST = N/D. If NOTE_P,
34906 add a reg_note saying that this was a division. Support both scalar and
34907 vector divide. Assumes no trapping math and finite arguments. */
34909 void
34910 rs6000_emit_swdiv (rtx dst, rtx n, rtx d, bool note_p)
34912 machine_mode mode = GET_MODE (dst);
34913 rtx one, x0, e0, x1, xprev, eprev, xnext, enext, u, v;
34914 int i;
34916 /* Low precision estimates guarantee 5 bits of accuracy. High
34917 precision estimates guarantee 14 bits of accuracy. SFmode
34918 requires 23 bits of accuracy. DFmode requires 52 bits of
34919 accuracy. Each pass at least doubles the accuracy, leading
34920 to the following. */
34921 int passes = (TARGET_RECIP_PRECISION) ? 1 : 3;
34922 if (mode == DFmode || mode == V2DFmode)
34923 passes++;
34925 enum insn_code code = optab_handler (smul_optab, mode);
34926 insn_gen_fn gen_mul = GEN_FCN (code);
34928 gcc_assert (code != CODE_FOR_nothing);
34930 one = rs6000_load_constant_and_splat (mode, dconst1);
34932 /* x0 = 1./d estimate */
34933 x0 = gen_reg_rtx (mode);
34934 emit_insn (gen_rtx_SET (x0, gen_rtx_UNSPEC (mode, gen_rtvec (1, d),
34935 UNSPEC_FRES)));
34937 /* Each iteration but the last calculates x_(i+1) = x_i * (2 - d * x_i). */
34938 if (passes > 1) {
34940 /* e0 = 1. - d * x0 */
34941 e0 = gen_reg_rtx (mode);
34942 rs6000_emit_nmsub (e0, d, x0, one);
34944 /* x1 = x0 + e0 * x0 */
34945 x1 = gen_reg_rtx (mode);
34946 rs6000_emit_madd (x1, e0, x0, x0);
34948 for (i = 0, xprev = x1, eprev = e0; i < passes - 2;
34949 ++i, xprev = xnext, eprev = enext) {
34951 /* enext = eprev * eprev */
34952 enext = gen_reg_rtx (mode);
34953 emit_insn (gen_mul (enext, eprev, eprev));
34955 /* xnext = xprev + enext * xprev */
34956 xnext = gen_reg_rtx (mode);
34957 rs6000_emit_madd (xnext, enext, xprev, xprev);
34960 } else
34961 xprev = x0;
34963 /* The last iteration calculates x_(i+1) = n * x_i * (2 - d * x_i). */
34965 /* u = n * xprev */
34966 u = gen_reg_rtx (mode);
34967 emit_insn (gen_mul (u, n, xprev));
34969 /* v = n - (d * u) */
34970 v = gen_reg_rtx (mode);
34971 rs6000_emit_nmsub (v, d, u, n);
34973 /* dst = (v * xprev) + u */
34974 rs6000_emit_madd (dst, v, xprev, u);
34976 if (note_p)
34977 add_reg_note (get_last_insn (), REG_EQUAL, gen_rtx_DIV (mode, n, d));
34980 /* Goldschmidt's Algorithm for single/double-precision floating point
34981 sqrt and rsqrt. Assumes no trapping math and finite arguments. */
34983 void
34984 rs6000_emit_swsqrt (rtx dst, rtx src, bool recip)
34986 machine_mode mode = GET_MODE (src);
34987 rtx e = gen_reg_rtx (mode);
34988 rtx g = gen_reg_rtx (mode);
34989 rtx h = gen_reg_rtx (mode);
34991 /* Low precision estimates guarantee 5 bits of accuracy. High
34992 precision estimates guarantee 14 bits of accuracy. SFmode
34993 requires 23 bits of accuracy. DFmode requires 52 bits of
34994 accuracy. Each pass at least doubles the accuracy, leading
34995 to the following. */
34996 int passes = (TARGET_RECIP_PRECISION) ? 1 : 3;
34997 if (mode == DFmode || mode == V2DFmode)
34998 passes++;
35000 int i;
35001 rtx mhalf;
35002 enum insn_code code = optab_handler (smul_optab, mode);
35003 insn_gen_fn gen_mul = GEN_FCN (code);
35005 gcc_assert (code != CODE_FOR_nothing);
35007 mhalf = rs6000_load_constant_and_splat (mode, dconsthalf);
35009 /* e = rsqrt estimate */
35010 emit_insn (gen_rtx_SET (e, gen_rtx_UNSPEC (mode, gen_rtvec (1, src),
35011 UNSPEC_RSQRT)));
35013 /* If (src == 0.0) filter infinity to prevent NaN for sqrt(0.0). */
35014 if (!recip)
35016 rtx zero = force_reg (mode, CONST0_RTX (mode));
35018 if (mode == SFmode)
35020 rtx target = emit_conditional_move (e, GT, src, zero, mode,
35021 e, zero, mode, 0);
35022 if (target != e)
35023 emit_move_insn (e, target);
35025 else
35027 rtx cond = gen_rtx_GT (VOIDmode, e, zero);
35028 rs6000_emit_vector_cond_expr (e, e, zero, cond, src, zero);
35032 /* g = sqrt estimate. */
35033 emit_insn (gen_mul (g, e, src));
35034 /* h = 1/(2*sqrt) estimate. */
35035 emit_insn (gen_mul (h, e, mhalf));
35037 if (recip)
35039 if (passes == 1)
35041 rtx t = gen_reg_rtx (mode);
35042 rs6000_emit_nmsub (t, g, h, mhalf);
35043 /* Apply correction directly to 1/rsqrt estimate. */
35044 rs6000_emit_madd (dst, e, t, e);
35046 else
35048 for (i = 0; i < passes; i++)
35050 rtx t1 = gen_reg_rtx (mode);
35051 rtx g1 = gen_reg_rtx (mode);
35052 rtx h1 = gen_reg_rtx (mode);
35054 rs6000_emit_nmsub (t1, g, h, mhalf);
35055 rs6000_emit_madd (g1, g, t1, g);
35056 rs6000_emit_madd (h1, h, t1, h);
35058 g = g1;
35059 h = h1;
35061 /* Multiply by 2 for 1/rsqrt. */
35062 emit_insn (gen_add3_insn (dst, h, h));
35065 else
35067 rtx t = gen_reg_rtx (mode);
35068 rs6000_emit_nmsub (t, g, h, mhalf);
35069 rs6000_emit_madd (dst, g, t, g);
35072 return;
35075 /* Emit popcount intrinsic on TARGET_POPCNTB (Power5) and TARGET_POPCNTD
35076 (Power7) targets. DST is the target, and SRC is the argument operand. */
35078 void
35079 rs6000_emit_popcount (rtx dst, rtx src)
35081 machine_mode mode = GET_MODE (dst);
35082 rtx tmp1, tmp2;
35084 /* Use the PPC ISA 2.06 popcnt{w,d} instruction if we can. */
35085 if (TARGET_POPCNTD)
35087 if (mode == SImode)
35088 emit_insn (gen_popcntdsi2 (dst, src));
35089 else
35090 emit_insn (gen_popcntddi2 (dst, src));
35091 return;
35094 tmp1 = gen_reg_rtx (mode);
35096 if (mode == SImode)
35098 emit_insn (gen_popcntbsi2 (tmp1, src));
35099 tmp2 = expand_mult (SImode, tmp1, GEN_INT (0x01010101),
35100 NULL_RTX, 0);
35101 tmp2 = force_reg (SImode, tmp2);
35102 emit_insn (gen_lshrsi3 (dst, tmp2, GEN_INT (24)));
35104 else
35106 emit_insn (gen_popcntbdi2 (tmp1, src));
35107 tmp2 = expand_mult (DImode, tmp1,
35108 GEN_INT ((HOST_WIDE_INT)
35109 0x01010101 << 32 | 0x01010101),
35110 NULL_RTX, 0);
35111 tmp2 = force_reg (DImode, tmp2);
35112 emit_insn (gen_lshrdi3 (dst, tmp2, GEN_INT (56)));
35117 /* Emit parity intrinsic on TARGET_POPCNTB targets. DST is the
35118 target, and SRC is the argument operand. */
35120 void
35121 rs6000_emit_parity (rtx dst, rtx src)
35123 machine_mode mode = GET_MODE (dst);
35124 rtx tmp;
35126 tmp = gen_reg_rtx (mode);
35128 /* Use the PPC ISA 2.05 prtyw/prtyd instruction if we can. */
35129 if (TARGET_CMPB)
35131 if (mode == SImode)
35133 emit_insn (gen_popcntbsi2 (tmp, src));
35134 emit_insn (gen_paritysi2_cmpb (dst, tmp));
35136 else
35138 emit_insn (gen_popcntbdi2 (tmp, src));
35139 emit_insn (gen_paritydi2_cmpb (dst, tmp));
35141 return;
35144 if (mode == SImode)
35146 /* Is mult+shift >= shift+xor+shift+xor? */
35147 if (rs6000_cost->mulsi_const >= COSTS_N_INSNS (3))
35149 rtx tmp1, tmp2, tmp3, tmp4;
35151 tmp1 = gen_reg_rtx (SImode);
35152 emit_insn (gen_popcntbsi2 (tmp1, src));
35154 tmp2 = gen_reg_rtx (SImode);
35155 emit_insn (gen_lshrsi3 (tmp2, tmp1, GEN_INT (16)));
35156 tmp3 = gen_reg_rtx (SImode);
35157 emit_insn (gen_xorsi3 (tmp3, tmp1, tmp2));
35159 tmp4 = gen_reg_rtx (SImode);
35160 emit_insn (gen_lshrsi3 (tmp4, tmp3, GEN_INT (8)));
35161 emit_insn (gen_xorsi3 (tmp, tmp3, tmp4));
35163 else
35164 rs6000_emit_popcount (tmp, src);
35165 emit_insn (gen_andsi3 (dst, tmp, const1_rtx));
35167 else
35169 /* Is mult+shift >= shift+xor+shift+xor+shift+xor? */
35170 if (rs6000_cost->muldi >= COSTS_N_INSNS (5))
35172 rtx tmp1, tmp2, tmp3, tmp4, tmp5, tmp6;
35174 tmp1 = gen_reg_rtx (DImode);
35175 emit_insn (gen_popcntbdi2 (tmp1, src));
35177 tmp2 = gen_reg_rtx (DImode);
35178 emit_insn (gen_lshrdi3 (tmp2, tmp1, GEN_INT (32)));
35179 tmp3 = gen_reg_rtx (DImode);
35180 emit_insn (gen_xordi3 (tmp3, tmp1, tmp2));
35182 tmp4 = gen_reg_rtx (DImode);
35183 emit_insn (gen_lshrdi3 (tmp4, tmp3, GEN_INT (16)));
35184 tmp5 = gen_reg_rtx (DImode);
35185 emit_insn (gen_xordi3 (tmp5, tmp3, tmp4));
35187 tmp6 = gen_reg_rtx (DImode);
35188 emit_insn (gen_lshrdi3 (tmp6, tmp5, GEN_INT (8)));
35189 emit_insn (gen_xordi3 (tmp, tmp5, tmp6));
35191 else
35192 rs6000_emit_popcount (tmp, src);
35193 emit_insn (gen_anddi3 (dst, tmp, const1_rtx));
35197 /* Expand an Altivec constant permutation for little endian mode.
35198 OP0 and OP1 are the input vectors and TARGET is the output vector.
35199 SEL specifies the constant permutation vector.
35201 There are two issues: First, the two input operands must be
35202 swapped so that together they form a double-wide array in LE
35203 order. Second, the vperm instruction has surprising behavior
35204 in LE mode: it interprets the elements of the source vectors
35205 in BE mode ("left to right") and interprets the elements of
35206 the destination vector in LE mode ("right to left"). To
35207 correct for this, we must subtract each element of the permute
35208 control vector from 31.
35210 For example, suppose we want to concatenate vr10 = {0, 1, 2, 3}
35211 with vr11 = {4, 5, 6, 7} and extract {0, 2, 4, 6} using a vperm.
35212 We place {0,1,2,3,8,9,10,11,16,17,18,19,24,25,26,27} in vr12 to
35213 serve as the permute control vector. Then, in BE mode,
35215 vperm 9,10,11,12
35217 places the desired result in vr9. However, in LE mode the
35218 vector contents will be
35220 vr10 = 00000003 00000002 00000001 00000000
35221 vr11 = 00000007 00000006 00000005 00000004
35223 The result of the vperm using the same permute control vector is
35225 vr9 = 05000000 07000000 01000000 03000000
35227 That is, the leftmost 4 bytes of vr10 are interpreted as the
35228 source for the rightmost 4 bytes of vr9, and so on.
35230 If we change the permute control vector to
35232 vr12 = {31,20,29,28,23,22,21,20,15,14,13,12,7,6,5,4}
35234 and issue
35236 vperm 9,11,10,12
35238 we get the desired
35240 vr9 = 00000006 00000004 00000002 00000000. */
35242 static void
35243 altivec_expand_vec_perm_const_le (rtx target, rtx op0, rtx op1,
35244 const vec_perm_indices &sel)
35246 unsigned int i;
35247 rtx perm[16];
35248 rtx constv, unspec;
35250 /* Unpack and adjust the constant selector. */
35251 for (i = 0; i < 16; ++i)
35253 unsigned int elt = 31 - (sel[i] & 31);
35254 perm[i] = GEN_INT (elt);
35257 /* Expand to a permute, swapping the inputs and using the
35258 adjusted selector. */
35259 if (!REG_P (op0))
35260 op0 = force_reg (V16QImode, op0);
35261 if (!REG_P (op1))
35262 op1 = force_reg (V16QImode, op1);
35264 constv = gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, perm));
35265 constv = force_reg (V16QImode, constv);
35266 unspec = gen_rtx_UNSPEC (V16QImode, gen_rtvec (3, op1, op0, constv),
35267 UNSPEC_VPERM);
35268 if (!REG_P (target))
35270 rtx tmp = gen_reg_rtx (V16QImode);
35271 emit_move_insn (tmp, unspec);
35272 unspec = tmp;
35275 emit_move_insn (target, unspec);
35278 /* Similarly to altivec_expand_vec_perm_const_le, we must adjust the
35279 permute control vector. But here it's not a constant, so we must
35280 generate a vector NAND or NOR to do the adjustment. */
35282 void
35283 altivec_expand_vec_perm_le (rtx operands[4])
35285 rtx notx, iorx, unspec;
35286 rtx target = operands[0];
35287 rtx op0 = operands[1];
35288 rtx op1 = operands[2];
35289 rtx sel = operands[3];
35290 rtx tmp = target;
35291 rtx norreg = gen_reg_rtx (V16QImode);
35292 machine_mode mode = GET_MODE (target);
35294 /* Get everything in regs so the pattern matches. */
35295 if (!REG_P (op0))
35296 op0 = force_reg (mode, op0);
35297 if (!REG_P (op1))
35298 op1 = force_reg (mode, op1);
35299 if (!REG_P (sel))
35300 sel = force_reg (V16QImode, sel);
35301 if (!REG_P (target))
35302 tmp = gen_reg_rtx (mode);
35304 if (TARGET_P9_VECTOR)
35306 unspec = gen_rtx_UNSPEC (mode, gen_rtvec (3, op1, op0, sel),
35307 UNSPEC_VPERMR);
35309 else
35311 /* Invert the selector with a VNAND if available, else a VNOR.
35312 The VNAND is preferred for future fusion opportunities. */
35313 notx = gen_rtx_NOT (V16QImode, sel);
35314 iorx = (TARGET_P8_VECTOR
35315 ? gen_rtx_IOR (V16QImode, notx, notx)
35316 : gen_rtx_AND (V16QImode, notx, notx));
35317 emit_insn (gen_rtx_SET (norreg, iorx));
35319 /* Permute with operands reversed and adjusted selector. */
35320 unspec = gen_rtx_UNSPEC (mode, gen_rtvec (3, op1, op0, norreg),
35321 UNSPEC_VPERM);
35324 /* Copy into target, possibly by way of a register. */
35325 if (!REG_P (target))
35327 emit_move_insn (tmp, unspec);
35328 unspec = tmp;
35331 emit_move_insn (target, unspec);
35334 /* Expand an Altivec constant permutation. Return true if we match
35335 an efficient implementation; false to fall back to VPERM.
35337 OP0 and OP1 are the input vectors and TARGET is the output vector.
35338 SEL specifies the constant permutation vector. */
35340 static bool
35341 altivec_expand_vec_perm_const (rtx target, rtx op0, rtx op1,
35342 const vec_perm_indices &sel)
35344 struct altivec_perm_insn {
35345 HOST_WIDE_INT mask;
35346 enum insn_code impl;
35347 unsigned char perm[16];
35349 static const struct altivec_perm_insn patterns[] = {
35350 { OPTION_MASK_ALTIVEC, CODE_FOR_altivec_vpkuhum_direct,
35351 { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } },
35352 { OPTION_MASK_ALTIVEC, CODE_FOR_altivec_vpkuwum_direct,
35353 { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } },
35354 { OPTION_MASK_ALTIVEC,
35355 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrghb_direct
35356 : CODE_FOR_altivec_vmrglb_direct),
35357 { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 } },
35358 { OPTION_MASK_ALTIVEC,
35359 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrghh_direct
35360 : CODE_FOR_altivec_vmrglh_direct),
35361 { 0, 1, 16, 17, 2, 3, 18, 19, 4, 5, 20, 21, 6, 7, 22, 23 } },
35362 { OPTION_MASK_ALTIVEC,
35363 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrghw_direct
35364 : CODE_FOR_altivec_vmrglw_direct),
35365 { 0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23 } },
35366 { OPTION_MASK_ALTIVEC,
35367 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrglb_direct
35368 : CODE_FOR_altivec_vmrghb_direct),
35369 { 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } },
35370 { OPTION_MASK_ALTIVEC,
35371 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrglh_direct
35372 : CODE_FOR_altivec_vmrghh_direct),
35373 { 8, 9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } },
35374 { OPTION_MASK_ALTIVEC,
35375 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrglw_direct
35376 : CODE_FOR_altivec_vmrghw_direct),
35377 { 8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } },
35378 { OPTION_MASK_P8_VECTOR,
35379 (BYTES_BIG_ENDIAN ? CODE_FOR_p8_vmrgew_v4sf_direct
35380 : CODE_FOR_p8_vmrgow_v4sf_direct),
35381 { 0, 1, 2, 3, 16, 17, 18, 19, 8, 9, 10, 11, 24, 25, 26, 27 } },
35382 { OPTION_MASK_P8_VECTOR,
35383 (BYTES_BIG_ENDIAN ? CODE_FOR_p8_vmrgow_v4sf_direct
35384 : CODE_FOR_p8_vmrgew_v4sf_direct),
35385 { 4, 5, 6, 7, 20, 21, 22, 23, 12, 13, 14, 15, 28, 29, 30, 31 } }
35388 unsigned int i, j, elt, which;
35389 unsigned char perm[16];
35390 rtx x;
35391 bool one_vec;
35393 /* Unpack the constant selector. */
35394 for (i = which = 0; i < 16; ++i)
35396 elt = sel[i] & 31;
35397 which |= (elt < 16 ? 1 : 2);
35398 perm[i] = elt;
35401 /* Simplify the constant selector based on operands. */
35402 switch (which)
35404 default:
35405 gcc_unreachable ();
35407 case 3:
35408 one_vec = false;
35409 if (!rtx_equal_p (op0, op1))
35410 break;
35411 /* FALLTHRU */
35413 case 2:
35414 for (i = 0; i < 16; ++i)
35415 perm[i] &= 15;
35416 op0 = op1;
35417 one_vec = true;
35418 break;
35420 case 1:
35421 op1 = op0;
35422 one_vec = true;
35423 break;
35426 /* Look for splat patterns. */
35427 if (one_vec)
35429 elt = perm[0];
35431 for (i = 0; i < 16; ++i)
35432 if (perm[i] != elt)
35433 break;
35434 if (i == 16)
35436 if (!BYTES_BIG_ENDIAN)
35437 elt = 15 - elt;
35438 emit_insn (gen_altivec_vspltb_direct (target, op0, GEN_INT (elt)));
35439 return true;
35442 if (elt % 2 == 0)
35444 for (i = 0; i < 16; i += 2)
35445 if (perm[i] != elt || perm[i + 1] != elt + 1)
35446 break;
35447 if (i == 16)
35449 int field = BYTES_BIG_ENDIAN ? elt / 2 : 7 - elt / 2;
35450 x = gen_reg_rtx (V8HImode);
35451 emit_insn (gen_altivec_vsplth_direct (x, gen_lowpart (V8HImode, op0),
35452 GEN_INT (field)));
35453 emit_move_insn (target, gen_lowpart (V16QImode, x));
35454 return true;
35458 if (elt % 4 == 0)
35460 for (i = 0; i < 16; i += 4)
35461 if (perm[i] != elt
35462 || perm[i + 1] != elt + 1
35463 || perm[i + 2] != elt + 2
35464 || perm[i + 3] != elt + 3)
35465 break;
35466 if (i == 16)
35468 int field = BYTES_BIG_ENDIAN ? elt / 4 : 3 - elt / 4;
35469 x = gen_reg_rtx (V4SImode);
35470 emit_insn (gen_altivec_vspltw_direct (x, gen_lowpart (V4SImode, op0),
35471 GEN_INT (field)));
35472 emit_move_insn (target, gen_lowpart (V16QImode, x));
35473 return true;
35478 /* Look for merge and pack patterns. */
35479 for (j = 0; j < ARRAY_SIZE (patterns); ++j)
35481 bool swapped;
35483 if ((patterns[j].mask & rs6000_isa_flags) == 0)
35484 continue;
35486 elt = patterns[j].perm[0];
35487 if (perm[0] == elt)
35488 swapped = false;
35489 else if (perm[0] == elt + 16)
35490 swapped = true;
35491 else
35492 continue;
35493 for (i = 1; i < 16; ++i)
35495 elt = patterns[j].perm[i];
35496 if (swapped)
35497 elt = (elt >= 16 ? elt - 16 : elt + 16);
35498 else if (one_vec && elt >= 16)
35499 elt -= 16;
35500 if (perm[i] != elt)
35501 break;
35503 if (i == 16)
35505 enum insn_code icode = patterns[j].impl;
35506 machine_mode omode = insn_data[icode].operand[0].mode;
35507 machine_mode imode = insn_data[icode].operand[1].mode;
35509 /* For little-endian, don't use vpkuwum and vpkuhum if the
35510 underlying vector type is not V4SI and V8HI, respectively.
35511 For example, using vpkuwum with a V8HI picks up the even
35512 halfwords (BE numbering) when the even halfwords (LE
35513 numbering) are what we need. */
35514 if (!BYTES_BIG_ENDIAN
35515 && icode == CODE_FOR_altivec_vpkuwum_direct
35516 && ((REG_P (op0)
35517 && GET_MODE (op0) != V4SImode)
35518 || (SUBREG_P (op0)
35519 && GET_MODE (XEXP (op0, 0)) != V4SImode)))
35520 continue;
35521 if (!BYTES_BIG_ENDIAN
35522 && icode == CODE_FOR_altivec_vpkuhum_direct
35523 && ((REG_P (op0)
35524 && GET_MODE (op0) != V8HImode)
35525 || (SUBREG_P (op0)
35526 && GET_MODE (XEXP (op0, 0)) != V8HImode)))
35527 continue;
35529 /* For little-endian, the two input operands must be swapped
35530 (or swapped back) to ensure proper right-to-left numbering
35531 from 0 to 2N-1. */
35532 if (swapped ^ !BYTES_BIG_ENDIAN)
35533 std::swap (op0, op1);
35534 if (imode != V16QImode)
35536 op0 = gen_lowpart (imode, op0);
35537 op1 = gen_lowpart (imode, op1);
35539 if (omode == V16QImode)
35540 x = target;
35541 else
35542 x = gen_reg_rtx (omode);
35543 emit_insn (GEN_FCN (icode) (x, op0, op1));
35544 if (omode != V16QImode)
35545 emit_move_insn (target, gen_lowpart (V16QImode, x));
35546 return true;
35550 if (!BYTES_BIG_ENDIAN)
35552 altivec_expand_vec_perm_const_le (target, op0, op1, sel);
35553 return true;
35556 return false;
35559 /* Expand a VSX Permute Doubleword constant permutation.
35560 Return true if we match an efficient implementation. */
35562 static bool
35563 rs6000_expand_vec_perm_const_1 (rtx target, rtx op0, rtx op1,
35564 unsigned char perm0, unsigned char perm1)
35566 rtx x;
35568 /* If both selectors come from the same operand, fold to single op. */
35569 if ((perm0 & 2) == (perm1 & 2))
35571 if (perm0 & 2)
35572 op0 = op1;
35573 else
35574 op1 = op0;
35576 /* If both operands are equal, fold to simpler permutation. */
35577 if (rtx_equal_p (op0, op1))
35579 perm0 = perm0 & 1;
35580 perm1 = (perm1 & 1) + 2;
35582 /* If the first selector comes from the second operand, swap. */
35583 else if (perm0 & 2)
35585 if (perm1 & 2)
35586 return false;
35587 perm0 -= 2;
35588 perm1 += 2;
35589 std::swap (op0, op1);
35591 /* If the second selector does not come from the second operand, fail. */
35592 else if ((perm1 & 2) == 0)
35593 return false;
35595 /* Success! */
35596 if (target != NULL)
35598 machine_mode vmode, dmode;
35599 rtvec v;
35601 vmode = GET_MODE (target);
35602 gcc_assert (GET_MODE_NUNITS (vmode) == 2);
35603 dmode = mode_for_vector (GET_MODE_INNER (vmode), 4).require ();
35604 x = gen_rtx_VEC_CONCAT (dmode, op0, op1);
35605 v = gen_rtvec (2, GEN_INT (perm0), GEN_INT (perm1));
35606 x = gen_rtx_VEC_SELECT (vmode, x, gen_rtx_PARALLEL (VOIDmode, v));
35607 emit_insn (gen_rtx_SET (target, x));
35609 return true;
35612 /* Implement TARGET_VECTORIZE_VEC_PERM_CONST. */
35614 static bool
35615 rs6000_vectorize_vec_perm_const (machine_mode vmode, rtx target, rtx op0,
35616 rtx op1, const vec_perm_indices &sel)
35618 bool testing_p = !target;
35620 /* AltiVec (and thus VSX) can handle arbitrary permutations. */
35621 if (TARGET_ALTIVEC && testing_p)
35622 return true;
35624 /* Check for ps_merge* or xxpermdi insns. */
35625 if ((vmode == V2DFmode || vmode == V2DImode) && VECTOR_MEM_VSX_P (vmode))
35627 if (testing_p)
35629 op0 = gen_raw_REG (vmode, LAST_VIRTUAL_REGISTER + 1);
35630 op1 = gen_raw_REG (vmode, LAST_VIRTUAL_REGISTER + 2);
35632 if (rs6000_expand_vec_perm_const_1 (target, op0, op1, sel[0], sel[1]))
35633 return true;
35636 if (TARGET_ALTIVEC)
35638 /* Force the target-independent code to lower to V16QImode. */
35639 if (vmode != V16QImode)
35640 return false;
35641 if (altivec_expand_vec_perm_const (target, op0, op1, sel))
35642 return true;
35645 return false;
35648 /* A subroutine for rs6000_expand_extract_even & rs6000_expand_interleave.
35649 OP0 and OP1 are the input vectors and TARGET is the output vector.
35650 PERM specifies the constant permutation vector. */
35652 static void
35653 rs6000_do_expand_vec_perm (rtx target, rtx op0, rtx op1,
35654 machine_mode vmode, const vec_perm_builder &perm)
35656 rtx x = expand_vec_perm_const (vmode, op0, op1, perm, BLKmode, target);
35657 if (x != target)
35658 emit_move_insn (target, x);
35661 /* Expand an extract even operation. */
35663 void
35664 rs6000_expand_extract_even (rtx target, rtx op0, rtx op1)
35666 machine_mode vmode = GET_MODE (target);
35667 unsigned i, nelt = GET_MODE_NUNITS (vmode);
35668 vec_perm_builder perm (nelt, nelt, 1);
35670 for (i = 0; i < nelt; i++)
35671 perm.quick_push (i * 2);
35673 rs6000_do_expand_vec_perm (target, op0, op1, vmode, perm);
35676 /* Expand a vector interleave operation. */
35678 void
35679 rs6000_expand_interleave (rtx target, rtx op0, rtx op1, bool highp)
35681 machine_mode vmode = GET_MODE (target);
35682 unsigned i, high, nelt = GET_MODE_NUNITS (vmode);
35683 vec_perm_builder perm (nelt, nelt, 1);
35685 high = (highp ? 0 : nelt / 2);
35686 for (i = 0; i < nelt / 2; i++)
35688 perm.quick_push (i + high);
35689 perm.quick_push (i + nelt + high);
35692 rs6000_do_expand_vec_perm (target, op0, op1, vmode, perm);
35695 /* Scale a V2DF vector SRC by two to the SCALE and place in TGT. */
35696 void
35697 rs6000_scale_v2df (rtx tgt, rtx src, int scale)
35699 HOST_WIDE_INT hwi_scale (scale);
35700 REAL_VALUE_TYPE r_pow;
35701 rtvec v = rtvec_alloc (2);
35702 rtx elt;
35703 rtx scale_vec = gen_reg_rtx (V2DFmode);
35704 (void)real_powi (&r_pow, DFmode, &dconst2, hwi_scale);
35705 elt = const_double_from_real_value (r_pow, DFmode);
35706 RTVEC_ELT (v, 0) = elt;
35707 RTVEC_ELT (v, 1) = elt;
35708 rs6000_expand_vector_init (scale_vec, gen_rtx_PARALLEL (V2DFmode, v));
35709 emit_insn (gen_mulv2df3 (tgt, src, scale_vec));
35712 /* Return an RTX representing where to find the function value of a
35713 function returning MODE. */
35714 static rtx
35715 rs6000_complex_function_value (machine_mode mode)
35717 unsigned int regno;
35718 rtx r1, r2;
35719 machine_mode inner = GET_MODE_INNER (mode);
35720 unsigned int inner_bytes = GET_MODE_UNIT_SIZE (mode);
35722 if (TARGET_FLOAT128_TYPE
35723 && (mode == KCmode
35724 || (mode == TCmode && TARGET_IEEEQUAD)))
35725 regno = ALTIVEC_ARG_RETURN;
35727 else if (FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT)
35728 regno = FP_ARG_RETURN;
35730 else
35732 regno = GP_ARG_RETURN;
35734 /* 32-bit is OK since it'll go in r3/r4. */
35735 if (TARGET_32BIT && inner_bytes >= 4)
35736 return gen_rtx_REG (mode, regno);
35739 if (inner_bytes >= 8)
35740 return gen_rtx_REG (mode, regno);
35742 r1 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno),
35743 const0_rtx);
35744 r2 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno + 1),
35745 GEN_INT (inner_bytes));
35746 return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
35749 /* Return an rtx describing a return value of MODE as a PARALLEL
35750 in N_ELTS registers, each of mode ELT_MODE, starting at REGNO,
35751 stride REG_STRIDE. */
35753 static rtx
35754 rs6000_parallel_return (machine_mode mode,
35755 int n_elts, machine_mode elt_mode,
35756 unsigned int regno, unsigned int reg_stride)
35758 rtx par = gen_rtx_PARALLEL (mode, rtvec_alloc (n_elts));
35760 int i;
35761 for (i = 0; i < n_elts; i++)
35763 rtx r = gen_rtx_REG (elt_mode, regno);
35764 rtx off = GEN_INT (i * GET_MODE_SIZE (elt_mode));
35765 XVECEXP (par, 0, i) = gen_rtx_EXPR_LIST (VOIDmode, r, off);
35766 regno += reg_stride;
35769 return par;
35772 /* Target hook for TARGET_FUNCTION_VALUE.
35774 An integer value is in r3 and a floating-point value is in fp1,
35775 unless -msoft-float. */
35777 static rtx
35778 rs6000_function_value (const_tree valtype,
35779 const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
35780 bool outgoing ATTRIBUTE_UNUSED)
35782 machine_mode mode;
35783 unsigned int regno;
35784 machine_mode elt_mode;
35785 int n_elts;
35787 /* Special handling for structs in darwin64. */
35788 if (TARGET_MACHO
35789 && rs6000_darwin64_struct_check_p (TYPE_MODE (valtype), valtype))
35791 CUMULATIVE_ARGS valcum;
35792 rtx valret;
35794 valcum.words = 0;
35795 valcum.fregno = FP_ARG_MIN_REG;
35796 valcum.vregno = ALTIVEC_ARG_MIN_REG;
35797 /* Do a trial code generation as if this were going to be passed as
35798 an argument; if any part goes in memory, we return NULL. */
35799 valret = rs6000_darwin64_record_arg (&valcum, valtype, true, /* retval= */ true);
35800 if (valret)
35801 return valret;
35802 /* Otherwise fall through to standard ABI rules. */
35805 mode = TYPE_MODE (valtype);
35807 /* The ELFv2 ABI returns homogeneous VFP aggregates in registers. */
35808 if (rs6000_discover_homogeneous_aggregate (mode, valtype, &elt_mode, &n_elts))
35810 int first_reg, n_regs;
35812 if (SCALAR_FLOAT_MODE_NOT_VECTOR_P (elt_mode))
35814 /* _Decimal128 must use even/odd register pairs. */
35815 first_reg = (elt_mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
35816 n_regs = (GET_MODE_SIZE (elt_mode) + 7) >> 3;
35818 else
35820 first_reg = ALTIVEC_ARG_RETURN;
35821 n_regs = 1;
35824 return rs6000_parallel_return (mode, n_elts, elt_mode, first_reg, n_regs);
35827 /* Some return value types need be split in -mpowerpc64, 32bit ABI. */
35828 if (TARGET_32BIT && TARGET_POWERPC64)
35829 switch (mode)
35831 default:
35832 break;
35833 case E_DImode:
35834 case E_SCmode:
35835 case E_DCmode:
35836 case E_TCmode:
35837 int count = GET_MODE_SIZE (mode) / 4;
35838 return rs6000_parallel_return (mode, count, SImode, GP_ARG_RETURN, 1);
35841 if ((INTEGRAL_TYPE_P (valtype)
35842 && GET_MODE_BITSIZE (mode) < (TARGET_32BIT ? 32 : 64))
35843 || POINTER_TYPE_P (valtype))
35844 mode = TARGET_32BIT ? SImode : DImode;
35846 if (DECIMAL_FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT)
35847 /* _Decimal128 must use an even/odd register pair. */
35848 regno = (mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
35849 else if (SCALAR_FLOAT_TYPE_P (valtype) && TARGET_HARD_FLOAT
35850 && !FLOAT128_VECTOR_P (mode))
35851 regno = FP_ARG_RETURN;
35852 else if (TREE_CODE (valtype) == COMPLEX_TYPE
35853 && targetm.calls.split_complex_arg)
35854 return rs6000_complex_function_value (mode);
35855 /* VSX is a superset of Altivec and adds V2DImode/V2DFmode. Since the same
35856 return register is used in both cases, and we won't see V2DImode/V2DFmode
35857 for pure altivec, combine the two cases. */
35858 else if ((TREE_CODE (valtype) == VECTOR_TYPE || FLOAT128_VECTOR_P (mode))
35859 && TARGET_ALTIVEC && TARGET_ALTIVEC_ABI
35860 && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
35861 regno = ALTIVEC_ARG_RETURN;
35862 else
35863 regno = GP_ARG_RETURN;
35865 return gen_rtx_REG (mode, regno);
35868 /* Define how to find the value returned by a library function
35869 assuming the value has mode MODE. */
35871 rs6000_libcall_value (machine_mode mode)
35873 unsigned int regno;
35875 /* Long long return value need be split in -mpowerpc64, 32bit ABI. */
35876 if (TARGET_32BIT && TARGET_POWERPC64 && mode == DImode)
35877 return rs6000_parallel_return (mode, 2, SImode, GP_ARG_RETURN, 1);
35879 if (DECIMAL_FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT)
35880 /* _Decimal128 must use an even/odd register pair. */
35881 regno = (mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
35882 else if (SCALAR_FLOAT_MODE_NOT_VECTOR_P (mode) && TARGET_HARD_FLOAT)
35883 regno = FP_ARG_RETURN;
35884 /* VSX is a superset of Altivec and adds V2DImode/V2DFmode. Since the same
35885 return register is used in both cases, and we won't see V2DImode/V2DFmode
35886 for pure altivec, combine the two cases. */
35887 else if (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
35888 && TARGET_ALTIVEC && TARGET_ALTIVEC_ABI)
35889 regno = ALTIVEC_ARG_RETURN;
35890 else if (COMPLEX_MODE_P (mode) && targetm.calls.split_complex_arg)
35891 return rs6000_complex_function_value (mode);
35892 else
35893 regno = GP_ARG_RETURN;
35895 return gen_rtx_REG (mode, regno);
35898 /* Compute register pressure classes. We implement the target hook to avoid
35899 IRA picking something like GEN_OR_FLOAT_REGS as a pressure class, which can
35900 lead to incorrect estimates of number of available registers and therefor
35901 increased register pressure/spill. */
35902 static int
35903 rs6000_compute_pressure_classes (enum reg_class *pressure_classes)
35905 int n;
35907 n = 0;
35908 pressure_classes[n++] = GENERAL_REGS;
35909 if (TARGET_VSX)
35910 pressure_classes[n++] = VSX_REGS;
35911 else
35913 if (TARGET_ALTIVEC)
35914 pressure_classes[n++] = ALTIVEC_REGS;
35915 if (TARGET_HARD_FLOAT)
35916 pressure_classes[n++] = FLOAT_REGS;
35918 pressure_classes[n++] = CR_REGS;
35919 pressure_classes[n++] = SPECIAL_REGS;
35921 return n;
35924 /* Given FROM and TO register numbers, say whether this elimination is allowed.
35925 Frame pointer elimination is automatically handled.
35927 For the RS/6000, if frame pointer elimination is being done, we would like
35928 to convert ap into fp, not sp.
35930 We need r30 if -mminimal-toc was specified, and there are constant pool
35931 references. */
35933 static bool
35934 rs6000_can_eliminate (const int from, const int to)
35936 return (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM
35937 ? ! frame_pointer_needed
35938 : from == RS6000_PIC_OFFSET_TABLE_REGNUM
35939 ? ! TARGET_MINIMAL_TOC || TARGET_NO_TOC
35940 || constant_pool_empty_p ()
35941 : true);
35944 /* Define the offset between two registers, FROM to be eliminated and its
35945 replacement TO, at the start of a routine. */
35946 HOST_WIDE_INT
35947 rs6000_initial_elimination_offset (int from, int to)
35949 rs6000_stack_t *info = rs6000_stack_info ();
35950 HOST_WIDE_INT offset;
35952 if (from == HARD_FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
35953 offset = info->push_p ? 0 : -info->total_size;
35954 else if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
35956 offset = info->push_p ? 0 : -info->total_size;
35957 if (FRAME_GROWS_DOWNWARD)
35958 offset += info->fixed_size + info->vars_size + info->parm_size;
35960 else if (from == FRAME_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
35961 offset = FRAME_GROWS_DOWNWARD
35962 ? info->fixed_size + info->vars_size + info->parm_size
35963 : 0;
35964 else if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
35965 offset = info->total_size;
35966 else if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
35967 offset = info->push_p ? info->total_size : 0;
35968 else if (from == RS6000_PIC_OFFSET_TABLE_REGNUM)
35969 offset = 0;
35970 else
35971 gcc_unreachable ();
35973 return offset;
35976 /* Fill in sizes of registers used by unwinder. */
35978 static void
35979 rs6000_init_dwarf_reg_sizes_extra (tree address)
35981 if (TARGET_MACHO && ! TARGET_ALTIVEC)
35983 int i;
35984 machine_mode mode = TYPE_MODE (char_type_node);
35985 rtx addr = expand_expr (address, NULL_RTX, VOIDmode, EXPAND_NORMAL);
35986 rtx mem = gen_rtx_MEM (BLKmode, addr);
35987 rtx value = gen_int_mode (16, mode);
35989 /* On Darwin, libgcc may be built to run on both G3 and G4/5.
35990 The unwinder still needs to know the size of Altivec registers. */
35992 for (i = FIRST_ALTIVEC_REGNO; i < LAST_ALTIVEC_REGNO+1; i++)
35994 int column = DWARF_REG_TO_UNWIND_COLUMN
35995 (DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), true));
35996 HOST_WIDE_INT offset = column * GET_MODE_SIZE (mode);
35998 emit_move_insn (adjust_address (mem, mode, offset), value);
36003 /* Map internal gcc register numbers to debug format register numbers.
36004 FORMAT specifies the type of debug register number to use:
36005 0 -- debug information, except for frame-related sections
36006 1 -- DWARF .debug_frame section
36007 2 -- DWARF .eh_frame section */
36009 unsigned int
36010 rs6000_dbx_register_number (unsigned int regno, unsigned int format)
36012 /* On some platforms, we use the standard DWARF register
36013 numbering for .debug_info and .debug_frame. */
36014 if ((format == 0 && write_symbols == DWARF2_DEBUG) || format == 1)
36016 #ifdef RS6000_USE_DWARF_NUMBERING
36017 if (regno <= 31)
36018 return regno;
36019 if (FP_REGNO_P (regno))
36020 return regno - FIRST_FPR_REGNO + 32;
36021 if (ALTIVEC_REGNO_P (regno))
36022 return regno - FIRST_ALTIVEC_REGNO + 1124;
36023 if (regno == LR_REGNO)
36024 return 108;
36025 if (regno == CTR_REGNO)
36026 return 109;
36027 if (regno == CA_REGNO)
36028 return 101; /* XER */
36029 /* Special handling for CR for .debug_frame: rs6000_emit_prologue has
36030 translated any combination of CR2, CR3, CR4 saves to a save of CR2.
36031 The actual code emitted saves the whole of CR, so we map CR2_REGNO
36032 to the DWARF reg for CR. */
36033 if (format == 1 && regno == CR2_REGNO)
36034 return 64;
36035 if (CR_REGNO_P (regno))
36036 return regno - CR0_REGNO + 86;
36037 if (regno == VRSAVE_REGNO)
36038 return 356;
36039 if (regno == VSCR_REGNO)
36040 return 67;
36042 /* These do not make much sense. */
36043 if (regno == FRAME_POINTER_REGNUM)
36044 return 111;
36045 if (regno == ARG_POINTER_REGNUM)
36046 return 67;
36047 if (regno == 64)
36048 return 100;
36050 gcc_unreachable ();
36051 #endif
36054 /* We use the GCC 7 (and before) internal number for non-DWARF debug
36055 information, and also for .eh_frame. */
36056 /* Translate the regnos to their numbers in GCC 7 (and before). */
36057 if (regno <= 31)
36058 return regno;
36059 if (FP_REGNO_P (regno))
36060 return regno - FIRST_FPR_REGNO + 32;
36061 if (ALTIVEC_REGNO_P (regno))
36062 return regno - FIRST_ALTIVEC_REGNO + 77;
36063 if (regno == LR_REGNO)
36064 return 65;
36065 if (regno == CTR_REGNO)
36066 return 66;
36067 if (regno == CA_REGNO)
36068 return 76; /* XER */
36069 if (CR_REGNO_P (regno))
36070 return regno - CR0_REGNO + 68;
36071 if (regno == VRSAVE_REGNO)
36072 return 109;
36073 if (regno == VSCR_REGNO)
36074 return 110;
36076 if (regno == FRAME_POINTER_REGNUM)
36077 return 111;
36078 if (regno == ARG_POINTER_REGNUM)
36079 return 67;
36080 if (regno == 64)
36081 return 64;
36083 gcc_unreachable ();
36086 /* target hook eh_return_filter_mode */
36087 static scalar_int_mode
36088 rs6000_eh_return_filter_mode (void)
36090 return TARGET_32BIT ? SImode : word_mode;
36093 /* Target hook for translate_mode_attribute. */
36094 static machine_mode
36095 rs6000_translate_mode_attribute (machine_mode mode)
36097 if ((FLOAT128_IEEE_P (mode)
36098 && ieee128_float_type_node == long_double_type_node)
36099 || (FLOAT128_IBM_P (mode)
36100 && ibm128_float_type_node == long_double_type_node))
36101 return COMPLEX_MODE_P (mode) ? E_TCmode : E_TFmode;
36102 return mode;
36105 /* Target hook for scalar_mode_supported_p. */
36106 static bool
36107 rs6000_scalar_mode_supported_p (scalar_mode mode)
36109 /* -m32 does not support TImode. This is the default, from
36110 default_scalar_mode_supported_p. For -m32 -mpowerpc64 we want the
36111 same ABI as for -m32. But default_scalar_mode_supported_p allows
36112 integer modes of precision 2 * BITS_PER_WORD, which matches TImode
36113 for -mpowerpc64. */
36114 if (TARGET_32BIT && mode == TImode)
36115 return false;
36117 if (DECIMAL_FLOAT_MODE_P (mode))
36118 return default_decimal_float_supported_p ();
36119 else if (TARGET_FLOAT128_TYPE && (mode == KFmode || mode == IFmode))
36120 return true;
36121 else
36122 return default_scalar_mode_supported_p (mode);
36125 /* Target hook for vector_mode_supported_p. */
36126 static bool
36127 rs6000_vector_mode_supported_p (machine_mode mode)
36129 /* There is no vector form for IEEE 128-bit. If we return true for IEEE
36130 128-bit, the compiler might try to widen IEEE 128-bit to IBM
36131 double-double. */
36132 if (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode) && !FLOAT128_IEEE_P (mode))
36133 return true;
36135 else
36136 return false;
36139 /* Target hook for floatn_mode. */
36140 static opt_scalar_float_mode
36141 rs6000_floatn_mode (int n, bool extended)
36143 if (extended)
36145 switch (n)
36147 case 32:
36148 return DFmode;
36150 case 64:
36151 if (TARGET_FLOAT128_TYPE)
36152 return (FLOAT128_IEEE_P (TFmode)) ? TFmode : KFmode;
36153 else
36154 return opt_scalar_float_mode ();
36156 case 128:
36157 return opt_scalar_float_mode ();
36159 default:
36160 /* Those are the only valid _FloatNx types. */
36161 gcc_unreachable ();
36164 else
36166 switch (n)
36168 case 32:
36169 return SFmode;
36171 case 64:
36172 return DFmode;
36174 case 128:
36175 if (TARGET_FLOAT128_TYPE)
36176 return (FLOAT128_IEEE_P (TFmode)) ? TFmode : KFmode;
36177 else
36178 return opt_scalar_float_mode ();
36180 default:
36181 return opt_scalar_float_mode ();
36187 /* Target hook for c_mode_for_suffix. */
36188 static machine_mode
36189 rs6000_c_mode_for_suffix (char suffix)
36191 if (TARGET_FLOAT128_TYPE)
36193 if (suffix == 'q' || suffix == 'Q')
36194 return (FLOAT128_IEEE_P (TFmode)) ? TFmode : KFmode;
36196 /* At the moment, we are not defining a suffix for IBM extended double.
36197 If/when the default for -mabi=ieeelongdouble is changed, and we want
36198 to support __ibm128 constants in legacy library code, we may need to
36199 re-evalaute this decision. Currently, c-lex.c only supports 'w' and
36200 'q' as machine dependent suffixes. The x86_64 port uses 'w' for
36201 __float80 constants. */
36204 return VOIDmode;
36207 /* Target hook for invalid_arg_for_unprototyped_fn. */
36208 static const char *
36209 invalid_arg_for_unprototyped_fn (const_tree typelist, const_tree funcdecl, const_tree val)
36211 return (!rs6000_darwin64_abi
36212 && typelist == 0
36213 && TREE_CODE (TREE_TYPE (val)) == VECTOR_TYPE
36214 && (funcdecl == NULL_TREE
36215 || (TREE_CODE (funcdecl) == FUNCTION_DECL
36216 && DECL_BUILT_IN_CLASS (funcdecl) != BUILT_IN_MD)))
36217 ? N_("AltiVec argument passed to unprototyped function")
36218 : NULL;
36221 /* For TARGET_SECURE_PLT 32-bit PIC code we can save PIC register
36222 setup by using __stack_chk_fail_local hidden function instead of
36223 calling __stack_chk_fail directly. Otherwise it is better to call
36224 __stack_chk_fail directly. */
36226 static tree ATTRIBUTE_UNUSED
36227 rs6000_stack_protect_fail (void)
36229 return (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
36230 ? default_hidden_stack_protect_fail ()
36231 : default_external_stack_protect_fail ();
36234 /* Implement the TARGET_ASAN_SHADOW_OFFSET hook. */
36236 #if TARGET_ELF
36237 static unsigned HOST_WIDE_INT
36238 rs6000_asan_shadow_offset (void)
36240 return (unsigned HOST_WIDE_INT) 1 << (TARGET_64BIT ? 41 : 29);
36242 #endif
36244 /* Mask options that we want to support inside of attribute((target)) and
36245 #pragma GCC target operations. Note, we do not include things like
36246 64/32-bit, endianness, hard/soft floating point, etc. that would have
36247 different calling sequences. */
36249 struct rs6000_opt_mask {
36250 const char *name; /* option name */
36251 HOST_WIDE_INT mask; /* mask to set */
36252 bool invert; /* invert sense of mask */
36253 bool valid_target; /* option is a target option */
36256 static struct rs6000_opt_mask const rs6000_opt_masks[] =
36258 { "altivec", OPTION_MASK_ALTIVEC, false, true },
36259 { "cmpb", OPTION_MASK_CMPB, false, true },
36260 { "crypto", OPTION_MASK_CRYPTO, false, true },
36261 { "direct-move", OPTION_MASK_DIRECT_MOVE, false, true },
36262 { "dlmzb", OPTION_MASK_DLMZB, false, true },
36263 { "efficient-unaligned-vsx", OPTION_MASK_EFFICIENT_UNALIGNED_VSX,
36264 false, true },
36265 { "float128", OPTION_MASK_FLOAT128_KEYWORD, false, true },
36266 { "float128-hardware", OPTION_MASK_FLOAT128_HW, false, true },
36267 { "fprnd", OPTION_MASK_FPRND, false, true },
36268 { "hard-dfp", OPTION_MASK_DFP, false, true },
36269 { "htm", OPTION_MASK_HTM, false, true },
36270 { "isel", OPTION_MASK_ISEL, false, true },
36271 { "mfcrf", OPTION_MASK_MFCRF, false, true },
36272 { "mfpgpr", OPTION_MASK_MFPGPR, false, true },
36273 { "modulo", OPTION_MASK_MODULO, false, true },
36274 { "mulhw", OPTION_MASK_MULHW, false, true },
36275 { "multiple", OPTION_MASK_MULTIPLE, false, true },
36276 { "popcntb", OPTION_MASK_POPCNTB, false, true },
36277 { "popcntd", OPTION_MASK_POPCNTD, false, true },
36278 { "power8-fusion", OPTION_MASK_P8_FUSION, false, true },
36279 { "power8-fusion-sign", OPTION_MASK_P8_FUSION_SIGN, false, true },
36280 { "power8-vector", OPTION_MASK_P8_VECTOR, false, true },
36281 { "power9-minmax", OPTION_MASK_P9_MINMAX, false, true },
36282 { "power9-misc", OPTION_MASK_P9_MISC, false, true },
36283 { "power9-vector", OPTION_MASK_P9_VECTOR, false, true },
36284 { "powerpc-gfxopt", OPTION_MASK_PPC_GFXOPT, false, true },
36285 { "powerpc-gpopt", OPTION_MASK_PPC_GPOPT, false, true },
36286 { "quad-memory", OPTION_MASK_QUAD_MEMORY, false, true },
36287 { "quad-memory-atomic", OPTION_MASK_QUAD_MEMORY_ATOMIC, false, true },
36288 { "recip-precision", OPTION_MASK_RECIP_PRECISION, false, true },
36289 { "save-toc-indirect", OPTION_MASK_SAVE_TOC_INDIRECT, false, true },
36290 { "string", 0, false, true },
36291 { "update", OPTION_MASK_NO_UPDATE, true , true },
36292 { "vsx", OPTION_MASK_VSX, false, true },
36293 #ifdef OPTION_MASK_64BIT
36294 #if TARGET_AIX_OS
36295 { "aix64", OPTION_MASK_64BIT, false, false },
36296 { "aix32", OPTION_MASK_64BIT, true, false },
36297 #else
36298 { "64", OPTION_MASK_64BIT, false, false },
36299 { "32", OPTION_MASK_64BIT, true, false },
36300 #endif
36301 #endif
36302 #ifdef OPTION_MASK_EABI
36303 { "eabi", OPTION_MASK_EABI, false, false },
36304 #endif
36305 #ifdef OPTION_MASK_LITTLE_ENDIAN
36306 { "little", OPTION_MASK_LITTLE_ENDIAN, false, false },
36307 { "big", OPTION_MASK_LITTLE_ENDIAN, true, false },
36308 #endif
36309 #ifdef OPTION_MASK_RELOCATABLE
36310 { "relocatable", OPTION_MASK_RELOCATABLE, false, false },
36311 #endif
36312 #ifdef OPTION_MASK_STRICT_ALIGN
36313 { "strict-align", OPTION_MASK_STRICT_ALIGN, false, false },
36314 #endif
36315 { "soft-float", OPTION_MASK_SOFT_FLOAT, false, false },
36316 { "string", 0, false, false },
36319 /* Builtin mask mapping for printing the flags. */
36320 static struct rs6000_opt_mask const rs6000_builtin_mask_names[] =
36322 { "altivec", RS6000_BTM_ALTIVEC, false, false },
36323 { "vsx", RS6000_BTM_VSX, false, false },
36324 { "fre", RS6000_BTM_FRE, false, false },
36325 { "fres", RS6000_BTM_FRES, false, false },
36326 { "frsqrte", RS6000_BTM_FRSQRTE, false, false },
36327 { "frsqrtes", RS6000_BTM_FRSQRTES, false, false },
36328 { "popcntd", RS6000_BTM_POPCNTD, false, false },
36329 { "cell", RS6000_BTM_CELL, false, false },
36330 { "power8-vector", RS6000_BTM_P8_VECTOR, false, false },
36331 { "power9-vector", RS6000_BTM_P9_VECTOR, false, false },
36332 { "power9-misc", RS6000_BTM_P9_MISC, false, false },
36333 { "crypto", RS6000_BTM_CRYPTO, false, false },
36334 { "htm", RS6000_BTM_HTM, false, false },
36335 { "hard-dfp", RS6000_BTM_DFP, false, false },
36336 { "hard-float", RS6000_BTM_HARD_FLOAT, false, false },
36337 { "long-double-128", RS6000_BTM_LDBL128, false, false },
36338 { "powerpc64", RS6000_BTM_POWERPC64, false, false },
36339 { "float128", RS6000_BTM_FLOAT128, false, false },
36340 { "float128-hw", RS6000_BTM_FLOAT128_HW,false, false },
36343 /* Option variables that we want to support inside attribute((target)) and
36344 #pragma GCC target operations. */
36346 struct rs6000_opt_var {
36347 const char *name; /* option name */
36348 size_t global_offset; /* offset of the option in global_options. */
36349 size_t target_offset; /* offset of the option in target options. */
36352 static struct rs6000_opt_var const rs6000_opt_vars[] =
36354 { "friz",
36355 offsetof (struct gcc_options, x_TARGET_FRIZ),
36356 offsetof (struct cl_target_option, x_TARGET_FRIZ), },
36357 { "avoid-indexed-addresses",
36358 offsetof (struct gcc_options, x_TARGET_AVOID_XFORM),
36359 offsetof (struct cl_target_option, x_TARGET_AVOID_XFORM) },
36360 { "longcall",
36361 offsetof (struct gcc_options, x_rs6000_default_long_calls),
36362 offsetof (struct cl_target_option, x_rs6000_default_long_calls), },
36363 { "optimize-swaps",
36364 offsetof (struct gcc_options, x_rs6000_optimize_swaps),
36365 offsetof (struct cl_target_option, x_rs6000_optimize_swaps), },
36366 { "allow-movmisalign",
36367 offsetof (struct gcc_options, x_TARGET_ALLOW_MOVMISALIGN),
36368 offsetof (struct cl_target_option, x_TARGET_ALLOW_MOVMISALIGN), },
36369 { "sched-groups",
36370 offsetof (struct gcc_options, x_TARGET_SCHED_GROUPS),
36371 offsetof (struct cl_target_option, x_TARGET_SCHED_GROUPS), },
36372 { "always-hint",
36373 offsetof (struct gcc_options, x_TARGET_ALWAYS_HINT),
36374 offsetof (struct cl_target_option, x_TARGET_ALWAYS_HINT), },
36375 { "align-branch-targets",
36376 offsetof (struct gcc_options, x_TARGET_ALIGN_BRANCH_TARGETS),
36377 offsetof (struct cl_target_option, x_TARGET_ALIGN_BRANCH_TARGETS), },
36378 { "tls-markers",
36379 offsetof (struct gcc_options, x_tls_markers),
36380 offsetof (struct cl_target_option, x_tls_markers), },
36381 { "sched-prolog",
36382 offsetof (struct gcc_options, x_TARGET_SCHED_PROLOG),
36383 offsetof (struct cl_target_option, x_TARGET_SCHED_PROLOG), },
36384 { "sched-epilog",
36385 offsetof (struct gcc_options, x_TARGET_SCHED_PROLOG),
36386 offsetof (struct cl_target_option, x_TARGET_SCHED_PROLOG), },
36387 { "speculate-indirect-jumps",
36388 offsetof (struct gcc_options, x_rs6000_speculate_indirect_jumps),
36389 offsetof (struct cl_target_option, x_rs6000_speculate_indirect_jumps), },
36392 /* Inner function to handle attribute((target("..."))) and #pragma GCC target
36393 parsing. Return true if there were no errors. */
36395 static bool
36396 rs6000_inner_target_options (tree args, bool attr_p)
36398 bool ret = true;
36400 if (args == NULL_TREE)
36403 else if (TREE_CODE (args) == STRING_CST)
36405 char *p = ASTRDUP (TREE_STRING_POINTER (args));
36406 char *q;
36408 while ((q = strtok (p, ",")) != NULL)
36410 bool error_p = false;
36411 bool not_valid_p = false;
36412 const char *cpu_opt = NULL;
36414 p = NULL;
36415 if (strncmp (q, "cpu=", 4) == 0)
36417 int cpu_index = rs6000_cpu_name_lookup (q+4);
36418 if (cpu_index >= 0)
36419 rs6000_cpu_index = cpu_index;
36420 else
36422 error_p = true;
36423 cpu_opt = q+4;
36426 else if (strncmp (q, "tune=", 5) == 0)
36428 int tune_index = rs6000_cpu_name_lookup (q+5);
36429 if (tune_index >= 0)
36430 rs6000_tune_index = tune_index;
36431 else
36433 error_p = true;
36434 cpu_opt = q+5;
36437 else
36439 size_t i;
36440 bool invert = false;
36441 char *r = q;
36443 error_p = true;
36444 if (strncmp (r, "no-", 3) == 0)
36446 invert = true;
36447 r += 3;
36450 for (i = 0; i < ARRAY_SIZE (rs6000_opt_masks); i++)
36451 if (strcmp (r, rs6000_opt_masks[i].name) == 0)
36453 HOST_WIDE_INT mask = rs6000_opt_masks[i].mask;
36455 if (!rs6000_opt_masks[i].valid_target)
36456 not_valid_p = true;
36457 else
36459 error_p = false;
36460 rs6000_isa_flags_explicit |= mask;
36462 /* VSX needs altivec, so -mvsx automagically sets
36463 altivec and disables -mavoid-indexed-addresses. */
36464 if (!invert)
36466 if (mask == OPTION_MASK_VSX)
36468 mask |= OPTION_MASK_ALTIVEC;
36469 TARGET_AVOID_XFORM = 0;
36473 if (rs6000_opt_masks[i].invert)
36474 invert = !invert;
36476 if (invert)
36477 rs6000_isa_flags &= ~mask;
36478 else
36479 rs6000_isa_flags |= mask;
36481 break;
36484 if (error_p && !not_valid_p)
36486 for (i = 0; i < ARRAY_SIZE (rs6000_opt_vars); i++)
36487 if (strcmp (r, rs6000_opt_vars[i].name) == 0)
36489 size_t j = rs6000_opt_vars[i].global_offset;
36490 *((int *) ((char *)&global_options + j)) = !invert;
36491 error_p = false;
36492 not_valid_p = false;
36493 break;
36498 if (error_p)
36500 const char *eprefix, *esuffix;
36502 ret = false;
36503 if (attr_p)
36505 eprefix = "__attribute__((__target__(";
36506 esuffix = ")))";
36508 else
36510 eprefix = "#pragma GCC target ";
36511 esuffix = "";
36514 if (cpu_opt)
36515 error ("invalid cpu %qs for %s%qs%s", cpu_opt, eprefix,
36516 q, esuffix);
36517 else if (not_valid_p)
36518 error ("%s%qs%s is not allowed", eprefix, q, esuffix);
36519 else
36520 error ("%s%qs%s is invalid", eprefix, q, esuffix);
36525 else if (TREE_CODE (args) == TREE_LIST)
36529 tree value = TREE_VALUE (args);
36530 if (value)
36532 bool ret2 = rs6000_inner_target_options (value, attr_p);
36533 if (!ret2)
36534 ret = false;
36536 args = TREE_CHAIN (args);
36538 while (args != NULL_TREE);
36541 else
36543 error ("attribute %<target%> argument not a string");
36544 return false;
36547 return ret;
36550 /* Print out the target options as a list for -mdebug=target. */
36552 static void
36553 rs6000_debug_target_options (tree args, const char *prefix)
36555 if (args == NULL_TREE)
36556 fprintf (stderr, "%s<NULL>", prefix);
36558 else if (TREE_CODE (args) == STRING_CST)
36560 char *p = ASTRDUP (TREE_STRING_POINTER (args));
36561 char *q;
36563 while ((q = strtok (p, ",")) != NULL)
36565 p = NULL;
36566 fprintf (stderr, "%s\"%s\"", prefix, q);
36567 prefix = ", ";
36571 else if (TREE_CODE (args) == TREE_LIST)
36575 tree value = TREE_VALUE (args);
36576 if (value)
36578 rs6000_debug_target_options (value, prefix);
36579 prefix = ", ";
36581 args = TREE_CHAIN (args);
36583 while (args != NULL_TREE);
36586 else
36587 gcc_unreachable ();
36589 return;
36593 /* Hook to validate attribute((target("..."))). */
36595 static bool
36596 rs6000_valid_attribute_p (tree fndecl,
36597 tree ARG_UNUSED (name),
36598 tree args,
36599 int flags)
36601 struct cl_target_option cur_target;
36602 bool ret;
36603 tree old_optimize;
36604 tree new_target, new_optimize;
36605 tree func_optimize;
36607 gcc_assert ((fndecl != NULL_TREE) && (args != NULL_TREE));
36609 if (TARGET_DEBUG_TARGET)
36611 tree tname = DECL_NAME (fndecl);
36612 fprintf (stderr, "\n==================== rs6000_valid_attribute_p:\n");
36613 if (tname)
36614 fprintf (stderr, "function: %.*s\n",
36615 (int) IDENTIFIER_LENGTH (tname),
36616 IDENTIFIER_POINTER (tname));
36617 else
36618 fprintf (stderr, "function: unknown\n");
36620 fprintf (stderr, "args:");
36621 rs6000_debug_target_options (args, " ");
36622 fprintf (stderr, "\n");
36624 if (flags)
36625 fprintf (stderr, "flags: 0x%x\n", flags);
36627 fprintf (stderr, "--------------------\n");
36630 /* attribute((target("default"))) does nothing, beyond
36631 affecting multi-versioning. */
36632 if (TREE_VALUE (args)
36633 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
36634 && TREE_CHAIN (args) == NULL_TREE
36635 && strcmp (TREE_STRING_POINTER (TREE_VALUE (args)), "default") == 0)
36636 return true;
36638 old_optimize = build_optimization_node (&global_options);
36639 func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
36641 /* If the function changed the optimization levels as well as setting target
36642 options, start with the optimizations specified. */
36643 if (func_optimize && func_optimize != old_optimize)
36644 cl_optimization_restore (&global_options,
36645 TREE_OPTIMIZATION (func_optimize));
36647 /* The target attributes may also change some optimization flags, so update
36648 the optimization options if necessary. */
36649 cl_target_option_save (&cur_target, &global_options);
36650 rs6000_cpu_index = rs6000_tune_index = -1;
36651 ret = rs6000_inner_target_options (args, true);
36653 /* Set up any additional state. */
36654 if (ret)
36656 ret = rs6000_option_override_internal (false);
36657 new_target = build_target_option_node (&global_options);
36659 else
36660 new_target = NULL;
36662 new_optimize = build_optimization_node (&global_options);
36664 if (!new_target)
36665 ret = false;
36667 else if (fndecl)
36669 DECL_FUNCTION_SPECIFIC_TARGET (fndecl) = new_target;
36671 if (old_optimize != new_optimize)
36672 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl) = new_optimize;
36675 cl_target_option_restore (&global_options, &cur_target);
36677 if (old_optimize != new_optimize)
36678 cl_optimization_restore (&global_options,
36679 TREE_OPTIMIZATION (old_optimize));
36681 return ret;
36685 /* Hook to validate the current #pragma GCC target and set the state, and
36686 update the macros based on what was changed. If ARGS is NULL, then
36687 POP_TARGET is used to reset the options. */
36689 bool
36690 rs6000_pragma_target_parse (tree args, tree pop_target)
36692 tree prev_tree = build_target_option_node (&global_options);
36693 tree cur_tree;
36694 struct cl_target_option *prev_opt, *cur_opt;
36695 HOST_WIDE_INT prev_flags, cur_flags, diff_flags;
36696 HOST_WIDE_INT prev_bumask, cur_bumask, diff_bumask;
36698 if (TARGET_DEBUG_TARGET)
36700 fprintf (stderr, "\n==================== rs6000_pragma_target_parse\n");
36701 fprintf (stderr, "args:");
36702 rs6000_debug_target_options (args, " ");
36703 fprintf (stderr, "\n");
36705 if (pop_target)
36707 fprintf (stderr, "pop_target:\n");
36708 debug_tree (pop_target);
36710 else
36711 fprintf (stderr, "pop_target: <NULL>\n");
36713 fprintf (stderr, "--------------------\n");
36716 if (! args)
36718 cur_tree = ((pop_target)
36719 ? pop_target
36720 : target_option_default_node);
36721 cl_target_option_restore (&global_options,
36722 TREE_TARGET_OPTION (cur_tree));
36724 else
36726 rs6000_cpu_index = rs6000_tune_index = -1;
36727 if (!rs6000_inner_target_options (args, false)
36728 || !rs6000_option_override_internal (false)
36729 || (cur_tree = build_target_option_node (&global_options))
36730 == NULL_TREE)
36732 if (TARGET_DEBUG_BUILTIN || TARGET_DEBUG_TARGET)
36733 fprintf (stderr, "invalid pragma\n");
36735 return false;
36739 target_option_current_node = cur_tree;
36740 rs6000_activate_target_options (target_option_current_node);
36742 /* If we have the preprocessor linked in (i.e. C or C++ languages), possibly
36743 change the macros that are defined. */
36744 if (rs6000_target_modify_macros_ptr)
36746 prev_opt = TREE_TARGET_OPTION (prev_tree);
36747 prev_bumask = prev_opt->x_rs6000_builtin_mask;
36748 prev_flags = prev_opt->x_rs6000_isa_flags;
36750 cur_opt = TREE_TARGET_OPTION (cur_tree);
36751 cur_flags = cur_opt->x_rs6000_isa_flags;
36752 cur_bumask = cur_opt->x_rs6000_builtin_mask;
36754 diff_bumask = (prev_bumask ^ cur_bumask);
36755 diff_flags = (prev_flags ^ cur_flags);
36757 if ((diff_flags != 0) || (diff_bumask != 0))
36759 /* Delete old macros. */
36760 rs6000_target_modify_macros_ptr (false,
36761 prev_flags & diff_flags,
36762 prev_bumask & diff_bumask);
36764 /* Define new macros. */
36765 rs6000_target_modify_macros_ptr (true,
36766 cur_flags & diff_flags,
36767 cur_bumask & diff_bumask);
36771 return true;
36775 /* Remember the last target of rs6000_set_current_function. */
36776 static GTY(()) tree rs6000_previous_fndecl;
36778 /* Restore target's globals from NEW_TREE and invalidate the
36779 rs6000_previous_fndecl cache. */
36781 void
36782 rs6000_activate_target_options (tree new_tree)
36784 cl_target_option_restore (&global_options, TREE_TARGET_OPTION (new_tree));
36785 if (TREE_TARGET_GLOBALS (new_tree))
36786 restore_target_globals (TREE_TARGET_GLOBALS (new_tree));
36787 else if (new_tree == target_option_default_node)
36788 restore_target_globals (&default_target_globals);
36789 else
36790 TREE_TARGET_GLOBALS (new_tree) = save_target_globals_default_opts ();
36791 rs6000_previous_fndecl = NULL_TREE;
36794 /* Establish appropriate back-end context for processing the function
36795 FNDECL. The argument might be NULL to indicate processing at top
36796 level, outside of any function scope. */
36797 static void
36798 rs6000_set_current_function (tree fndecl)
36800 if (TARGET_DEBUG_TARGET)
36802 fprintf (stderr, "\n==================== rs6000_set_current_function");
36804 if (fndecl)
36805 fprintf (stderr, ", fndecl %s (%p)",
36806 (DECL_NAME (fndecl)
36807 ? IDENTIFIER_POINTER (DECL_NAME (fndecl))
36808 : "<unknown>"), (void *)fndecl);
36810 if (rs6000_previous_fndecl)
36811 fprintf (stderr, ", prev_fndecl (%p)", (void *)rs6000_previous_fndecl);
36813 fprintf (stderr, "\n");
36816 /* Only change the context if the function changes. This hook is called
36817 several times in the course of compiling a function, and we don't want to
36818 slow things down too much or call target_reinit when it isn't safe. */
36819 if (fndecl == rs6000_previous_fndecl)
36820 return;
36822 tree old_tree;
36823 if (rs6000_previous_fndecl == NULL_TREE)
36824 old_tree = target_option_current_node;
36825 else if (DECL_FUNCTION_SPECIFIC_TARGET (rs6000_previous_fndecl))
36826 old_tree = DECL_FUNCTION_SPECIFIC_TARGET (rs6000_previous_fndecl);
36827 else
36828 old_tree = target_option_default_node;
36830 tree new_tree;
36831 if (fndecl == NULL_TREE)
36833 if (old_tree != target_option_current_node)
36834 new_tree = target_option_current_node;
36835 else
36836 new_tree = NULL_TREE;
36838 else
36840 new_tree = DECL_FUNCTION_SPECIFIC_TARGET (fndecl);
36841 if (new_tree == NULL_TREE)
36842 new_tree = target_option_default_node;
36845 if (TARGET_DEBUG_TARGET)
36847 if (new_tree)
36849 fprintf (stderr, "\nnew fndecl target specific options:\n");
36850 debug_tree (new_tree);
36853 if (old_tree)
36855 fprintf (stderr, "\nold fndecl target specific options:\n");
36856 debug_tree (old_tree);
36859 if (old_tree != NULL_TREE || new_tree != NULL_TREE)
36860 fprintf (stderr, "--------------------\n");
36863 if (new_tree && old_tree != new_tree)
36864 rs6000_activate_target_options (new_tree);
36866 if (fndecl)
36867 rs6000_previous_fndecl = fndecl;
36871 /* Save the current options */
36873 static void
36874 rs6000_function_specific_save (struct cl_target_option *ptr,
36875 struct gcc_options *opts)
36877 ptr->x_rs6000_isa_flags = opts->x_rs6000_isa_flags;
36878 ptr->x_rs6000_isa_flags_explicit = opts->x_rs6000_isa_flags_explicit;
36881 /* Restore the current options */
36883 static void
36884 rs6000_function_specific_restore (struct gcc_options *opts,
36885 struct cl_target_option *ptr)
36888 opts->x_rs6000_isa_flags = ptr->x_rs6000_isa_flags;
36889 opts->x_rs6000_isa_flags_explicit = ptr->x_rs6000_isa_flags_explicit;
36890 (void) rs6000_option_override_internal (false);
36893 /* Print the current options */
36895 static void
36896 rs6000_function_specific_print (FILE *file, int indent,
36897 struct cl_target_option *ptr)
36899 rs6000_print_isa_options (file, indent, "Isa options set",
36900 ptr->x_rs6000_isa_flags);
36902 rs6000_print_isa_options (file, indent, "Isa options explicit",
36903 ptr->x_rs6000_isa_flags_explicit);
36906 /* Helper function to print the current isa or misc options on a line. */
36908 static void
36909 rs6000_print_options_internal (FILE *file,
36910 int indent,
36911 const char *string,
36912 HOST_WIDE_INT flags,
36913 const char *prefix,
36914 const struct rs6000_opt_mask *opts,
36915 size_t num_elements)
36917 size_t i;
36918 size_t start_column = 0;
36919 size_t cur_column;
36920 size_t max_column = 120;
36921 size_t prefix_len = strlen (prefix);
36922 size_t comma_len = 0;
36923 const char *comma = "";
36925 if (indent)
36926 start_column += fprintf (file, "%*s", indent, "");
36928 if (!flags)
36930 fprintf (stderr, DEBUG_FMT_S, string, "<none>");
36931 return;
36934 start_column += fprintf (stderr, DEBUG_FMT_WX, string, flags);
36936 /* Print the various mask options. */
36937 cur_column = start_column;
36938 for (i = 0; i < num_elements; i++)
36940 bool invert = opts[i].invert;
36941 const char *name = opts[i].name;
36942 const char *no_str = "";
36943 HOST_WIDE_INT mask = opts[i].mask;
36944 size_t len = comma_len + prefix_len + strlen (name);
36946 if (!invert)
36948 if ((flags & mask) == 0)
36950 no_str = "no-";
36951 len += sizeof ("no-") - 1;
36954 flags &= ~mask;
36957 else
36959 if ((flags & mask) != 0)
36961 no_str = "no-";
36962 len += sizeof ("no-") - 1;
36965 flags |= mask;
36968 cur_column += len;
36969 if (cur_column > max_column)
36971 fprintf (stderr, ", \\\n%*s", (int)start_column, "");
36972 cur_column = start_column + len;
36973 comma = "";
36976 fprintf (file, "%s%s%s%s", comma, prefix, no_str, name);
36977 comma = ", ";
36978 comma_len = sizeof (", ") - 1;
36981 fputs ("\n", file);
36984 /* Helper function to print the current isa options on a line. */
36986 static void
36987 rs6000_print_isa_options (FILE *file, int indent, const char *string,
36988 HOST_WIDE_INT flags)
36990 rs6000_print_options_internal (file, indent, string, flags, "-m",
36991 &rs6000_opt_masks[0],
36992 ARRAY_SIZE (rs6000_opt_masks));
36995 static void
36996 rs6000_print_builtin_options (FILE *file, int indent, const char *string,
36997 HOST_WIDE_INT flags)
36999 rs6000_print_options_internal (file, indent, string, flags, "",
37000 &rs6000_builtin_mask_names[0],
37001 ARRAY_SIZE (rs6000_builtin_mask_names));
37004 /* If the user used -mno-vsx, we need turn off all of the implicit ISA 2.06,
37005 2.07, and 3.0 options that relate to the vector unit (-mdirect-move,
37006 -mupper-regs-df, etc.).
37008 If the user used -mno-power8-vector, we need to turn off all of the implicit
37009 ISA 2.07 and 3.0 options that relate to the vector unit.
37011 If the user used -mno-power9-vector, we need to turn off all of the implicit
37012 ISA 3.0 options that relate to the vector unit.
37014 This function does not handle explicit options such as the user specifying
37015 -mdirect-move. These are handled in rs6000_option_override_internal, and
37016 the appropriate error is given if needed.
37018 We return a mask of all of the implicit options that should not be enabled
37019 by default. */
37021 static HOST_WIDE_INT
37022 rs6000_disable_incompatible_switches (void)
37024 HOST_WIDE_INT ignore_masks = rs6000_isa_flags_explicit;
37025 size_t i, j;
37027 static const struct {
37028 const HOST_WIDE_INT no_flag; /* flag explicitly turned off. */
37029 const HOST_WIDE_INT dep_flags; /* flags that depend on this option. */
37030 const char *const name; /* name of the switch. */
37031 } flags[] = {
37032 { OPTION_MASK_P9_VECTOR, OTHER_P9_VECTOR_MASKS, "power9-vector" },
37033 { OPTION_MASK_P8_VECTOR, OTHER_P8_VECTOR_MASKS, "power8-vector" },
37034 { OPTION_MASK_VSX, OTHER_VSX_VECTOR_MASKS, "vsx" },
37037 for (i = 0; i < ARRAY_SIZE (flags); i++)
37039 HOST_WIDE_INT no_flag = flags[i].no_flag;
37041 if ((rs6000_isa_flags & no_flag) == 0
37042 && (rs6000_isa_flags_explicit & no_flag) != 0)
37044 HOST_WIDE_INT dep_flags = flags[i].dep_flags;
37045 HOST_WIDE_INT set_flags = (rs6000_isa_flags_explicit
37046 & rs6000_isa_flags
37047 & dep_flags);
37049 if (set_flags)
37051 for (j = 0; j < ARRAY_SIZE (rs6000_opt_masks); j++)
37052 if ((set_flags & rs6000_opt_masks[j].mask) != 0)
37054 set_flags &= ~rs6000_opt_masks[j].mask;
37055 error ("%<-mno-%s%> turns off %<-m%s%>",
37056 flags[i].name,
37057 rs6000_opt_masks[j].name);
37060 gcc_assert (!set_flags);
37063 rs6000_isa_flags &= ~dep_flags;
37064 ignore_masks |= no_flag | dep_flags;
37068 return ignore_masks;
37072 /* Helper function for printing the function name when debugging. */
37074 static const char *
37075 get_decl_name (tree fn)
37077 tree name;
37079 if (!fn)
37080 return "<null>";
37082 name = DECL_NAME (fn);
37083 if (!name)
37084 return "<no-name>";
37086 return IDENTIFIER_POINTER (name);
37089 /* Return the clone id of the target we are compiling code for in a target
37090 clone. The clone id is ordered from 0 (default) to CLONE_MAX-1 and gives
37091 the priority list for the target clones (ordered from lowest to
37092 highest). */
37094 static int
37095 rs6000_clone_priority (tree fndecl)
37097 tree fn_opts = DECL_FUNCTION_SPECIFIC_TARGET (fndecl);
37098 HOST_WIDE_INT isa_masks;
37099 int ret = CLONE_DEFAULT;
37100 tree attrs = lookup_attribute ("target", DECL_ATTRIBUTES (fndecl));
37101 const char *attrs_str = NULL;
37103 attrs = TREE_VALUE (TREE_VALUE (attrs));
37104 attrs_str = TREE_STRING_POINTER (attrs);
37106 /* Return priority zero for default function. Return the ISA needed for the
37107 function if it is not the default. */
37108 if (strcmp (attrs_str, "default") != 0)
37110 if (fn_opts == NULL_TREE)
37111 fn_opts = target_option_default_node;
37113 if (!fn_opts || !TREE_TARGET_OPTION (fn_opts))
37114 isa_masks = rs6000_isa_flags;
37115 else
37116 isa_masks = TREE_TARGET_OPTION (fn_opts)->x_rs6000_isa_flags;
37118 for (ret = CLONE_MAX - 1; ret != 0; ret--)
37119 if ((rs6000_clone_map[ret].isa_mask & isa_masks) != 0)
37120 break;
37123 if (TARGET_DEBUG_TARGET)
37124 fprintf (stderr, "rs6000_get_function_version_priority (%s) => %d\n",
37125 get_decl_name (fndecl), ret);
37127 return ret;
37130 /* This compares the priority of target features in function DECL1 and DECL2.
37131 It returns positive value if DECL1 is higher priority, negative value if
37132 DECL2 is higher priority and 0 if they are the same. Note, priorities are
37133 ordered from lowest (CLONE_DEFAULT) to highest (currently CLONE_ISA_3_0). */
37135 static int
37136 rs6000_compare_version_priority (tree decl1, tree decl2)
37138 int priority1 = rs6000_clone_priority (decl1);
37139 int priority2 = rs6000_clone_priority (decl2);
37140 int ret = priority1 - priority2;
37142 if (TARGET_DEBUG_TARGET)
37143 fprintf (stderr, "rs6000_compare_version_priority (%s, %s) => %d\n",
37144 get_decl_name (decl1), get_decl_name (decl2), ret);
37146 return ret;
37149 /* Make a dispatcher declaration for the multi-versioned function DECL.
37150 Calls to DECL function will be replaced with calls to the dispatcher
37151 by the front-end. Returns the decl of the dispatcher function. */
37153 static tree
37154 rs6000_get_function_versions_dispatcher (void *decl)
37156 tree fn = (tree) decl;
37157 struct cgraph_node *node = NULL;
37158 struct cgraph_node *default_node = NULL;
37159 struct cgraph_function_version_info *node_v = NULL;
37160 struct cgraph_function_version_info *first_v = NULL;
37162 tree dispatch_decl = NULL;
37164 struct cgraph_function_version_info *default_version_info = NULL;
37165 gcc_assert (fn != NULL && DECL_FUNCTION_VERSIONED (fn));
37167 if (TARGET_DEBUG_TARGET)
37168 fprintf (stderr, "rs6000_get_function_versions_dispatcher (%s)\n",
37169 get_decl_name (fn));
37171 node = cgraph_node::get (fn);
37172 gcc_assert (node != NULL);
37174 node_v = node->function_version ();
37175 gcc_assert (node_v != NULL);
37177 if (node_v->dispatcher_resolver != NULL)
37178 return node_v->dispatcher_resolver;
37180 /* Find the default version and make it the first node. */
37181 first_v = node_v;
37182 /* Go to the beginning of the chain. */
37183 while (first_v->prev != NULL)
37184 first_v = first_v->prev;
37186 default_version_info = first_v;
37187 while (default_version_info != NULL)
37189 const tree decl2 = default_version_info->this_node->decl;
37190 if (is_function_default_version (decl2))
37191 break;
37192 default_version_info = default_version_info->next;
37195 /* If there is no default node, just return NULL. */
37196 if (default_version_info == NULL)
37197 return NULL;
37199 /* Make default info the first node. */
37200 if (first_v != default_version_info)
37202 default_version_info->prev->next = default_version_info->next;
37203 if (default_version_info->next)
37204 default_version_info->next->prev = default_version_info->prev;
37205 first_v->prev = default_version_info;
37206 default_version_info->next = first_v;
37207 default_version_info->prev = NULL;
37210 default_node = default_version_info->this_node;
37212 #ifndef TARGET_LIBC_PROVIDES_HWCAP_IN_TCB
37213 error_at (DECL_SOURCE_LOCATION (default_node->decl),
37214 "%<target_clones%> attribute needs GLIBC (2.23 and newer) that "
37215 "exports hardware capability bits");
37216 #else
37218 if (targetm.has_ifunc_p ())
37220 struct cgraph_function_version_info *it_v = NULL;
37221 struct cgraph_node *dispatcher_node = NULL;
37222 struct cgraph_function_version_info *dispatcher_version_info = NULL;
37224 /* Right now, the dispatching is done via ifunc. */
37225 dispatch_decl = make_dispatcher_decl (default_node->decl);
37227 dispatcher_node = cgraph_node::get_create (dispatch_decl);
37228 gcc_assert (dispatcher_node != NULL);
37229 dispatcher_node->dispatcher_function = 1;
37230 dispatcher_version_info
37231 = dispatcher_node->insert_new_function_version ();
37232 dispatcher_version_info->next = default_version_info;
37233 dispatcher_node->definition = 1;
37235 /* Set the dispatcher for all the versions. */
37236 it_v = default_version_info;
37237 while (it_v != NULL)
37239 it_v->dispatcher_resolver = dispatch_decl;
37240 it_v = it_v->next;
37243 else
37245 error_at (DECL_SOURCE_LOCATION (default_node->decl),
37246 "multiversioning needs ifunc which is not supported "
37247 "on this target");
37249 #endif
37251 return dispatch_decl;
37254 /* Make the resolver function decl to dispatch the versions of a multi-
37255 versioned function, DEFAULT_DECL. Create an empty basic block in the
37256 resolver and store the pointer in EMPTY_BB. Return the decl of the resolver
37257 function. */
37259 static tree
37260 make_resolver_func (const tree default_decl,
37261 const tree dispatch_decl,
37262 basic_block *empty_bb)
37264 /* Make the resolver function static. The resolver function returns
37265 void *. */
37266 tree decl_name = clone_function_name (default_decl, "resolver");
37267 const char *resolver_name = IDENTIFIER_POINTER (decl_name);
37268 tree type = build_function_type_list (ptr_type_node, NULL_TREE);
37269 tree decl = build_fn_decl (resolver_name, type);
37270 SET_DECL_ASSEMBLER_NAME (decl, decl_name);
37272 DECL_NAME (decl) = decl_name;
37273 TREE_USED (decl) = 1;
37274 DECL_ARTIFICIAL (decl) = 1;
37275 DECL_IGNORED_P (decl) = 0;
37276 TREE_PUBLIC (decl) = 0;
37277 DECL_UNINLINABLE (decl) = 1;
37279 /* Resolver is not external, body is generated. */
37280 DECL_EXTERNAL (decl) = 0;
37281 DECL_EXTERNAL (dispatch_decl) = 0;
37283 DECL_CONTEXT (decl) = NULL_TREE;
37284 DECL_INITIAL (decl) = make_node (BLOCK);
37285 DECL_STATIC_CONSTRUCTOR (decl) = 0;
37287 /* Build result decl and add to function_decl. */
37288 tree t = build_decl (UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE, ptr_type_node);
37289 DECL_CONTEXT (t) = decl;
37290 DECL_ARTIFICIAL (t) = 1;
37291 DECL_IGNORED_P (t) = 1;
37292 DECL_RESULT (decl) = t;
37294 gimplify_function_tree (decl);
37295 push_cfun (DECL_STRUCT_FUNCTION (decl));
37296 *empty_bb = init_lowered_empty_function (decl, false,
37297 profile_count::uninitialized ());
37299 cgraph_node::add_new_function (decl, true);
37300 symtab->call_cgraph_insertion_hooks (cgraph_node::get_create (decl));
37302 pop_cfun ();
37304 /* Mark dispatch_decl as "ifunc" with resolver as resolver_name. */
37305 DECL_ATTRIBUTES (dispatch_decl)
37306 = make_attribute ("ifunc", resolver_name, DECL_ATTRIBUTES (dispatch_decl));
37308 cgraph_node::create_same_body_alias (dispatch_decl, decl);
37310 return decl;
37313 /* This adds a condition to the basic_block NEW_BB in function FUNCTION_DECL to
37314 return a pointer to VERSION_DECL if we are running on a machine that
37315 supports the index CLONE_ISA hardware architecture bits. This function will
37316 be called during version dispatch to decide which function version to
37317 execute. It returns the basic block at the end, to which more conditions
37318 can be added. */
37320 static basic_block
37321 add_condition_to_bb (tree function_decl, tree version_decl,
37322 int clone_isa, basic_block new_bb)
37324 push_cfun (DECL_STRUCT_FUNCTION (function_decl));
37326 gcc_assert (new_bb != NULL);
37327 gimple_seq gseq = bb_seq (new_bb);
37330 tree convert_expr = build1 (CONVERT_EXPR, ptr_type_node,
37331 build_fold_addr_expr (version_decl));
37332 tree result_var = create_tmp_var (ptr_type_node);
37333 gimple *convert_stmt = gimple_build_assign (result_var, convert_expr);
37334 gimple *return_stmt = gimple_build_return (result_var);
37336 if (clone_isa == CLONE_DEFAULT)
37338 gimple_seq_add_stmt (&gseq, convert_stmt);
37339 gimple_seq_add_stmt (&gseq, return_stmt);
37340 set_bb_seq (new_bb, gseq);
37341 gimple_set_bb (convert_stmt, new_bb);
37342 gimple_set_bb (return_stmt, new_bb);
37343 pop_cfun ();
37344 return new_bb;
37347 tree bool_zero = build_int_cst (bool_int_type_node, 0);
37348 tree cond_var = create_tmp_var (bool_int_type_node);
37349 tree predicate_decl = rs6000_builtin_decls [(int) RS6000_BUILTIN_CPU_SUPPORTS];
37350 const char *arg_str = rs6000_clone_map[clone_isa].name;
37351 tree predicate_arg = build_string_literal (strlen (arg_str) + 1, arg_str);
37352 gimple *call_cond_stmt = gimple_build_call (predicate_decl, 1, predicate_arg);
37353 gimple_call_set_lhs (call_cond_stmt, cond_var);
37355 gimple_set_block (call_cond_stmt, DECL_INITIAL (function_decl));
37356 gimple_set_bb (call_cond_stmt, new_bb);
37357 gimple_seq_add_stmt (&gseq, call_cond_stmt);
37359 gimple *if_else_stmt = gimple_build_cond (NE_EXPR, cond_var, bool_zero,
37360 NULL_TREE, NULL_TREE);
37361 gimple_set_block (if_else_stmt, DECL_INITIAL (function_decl));
37362 gimple_set_bb (if_else_stmt, new_bb);
37363 gimple_seq_add_stmt (&gseq, if_else_stmt);
37365 gimple_seq_add_stmt (&gseq, convert_stmt);
37366 gimple_seq_add_stmt (&gseq, return_stmt);
37367 set_bb_seq (new_bb, gseq);
37369 basic_block bb1 = new_bb;
37370 edge e12 = split_block (bb1, if_else_stmt);
37371 basic_block bb2 = e12->dest;
37372 e12->flags &= ~EDGE_FALLTHRU;
37373 e12->flags |= EDGE_TRUE_VALUE;
37375 edge e23 = split_block (bb2, return_stmt);
37376 gimple_set_bb (convert_stmt, bb2);
37377 gimple_set_bb (return_stmt, bb2);
37379 basic_block bb3 = e23->dest;
37380 make_edge (bb1, bb3, EDGE_FALSE_VALUE);
37382 remove_edge (e23);
37383 make_edge (bb2, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
37385 pop_cfun ();
37386 return bb3;
37389 /* This function generates the dispatch function for multi-versioned functions.
37390 DISPATCH_DECL is the function which will contain the dispatch logic.
37391 FNDECLS are the function choices for dispatch, and is a tree chain.
37392 EMPTY_BB is the basic block pointer in DISPATCH_DECL in which the dispatch
37393 code is generated. */
37395 static int
37396 dispatch_function_versions (tree dispatch_decl,
37397 void *fndecls_p,
37398 basic_block *empty_bb)
37400 int ix;
37401 tree ele;
37402 vec<tree> *fndecls;
37403 tree clones[CLONE_MAX];
37405 if (TARGET_DEBUG_TARGET)
37406 fputs ("dispatch_function_versions, top\n", stderr);
37408 gcc_assert (dispatch_decl != NULL
37409 && fndecls_p != NULL
37410 && empty_bb != NULL);
37412 /* fndecls_p is actually a vector. */
37413 fndecls = static_cast<vec<tree> *> (fndecls_p);
37415 /* At least one more version other than the default. */
37416 gcc_assert (fndecls->length () >= 2);
37418 /* The first version in the vector is the default decl. */
37419 memset ((void *) clones, '\0', sizeof (clones));
37420 clones[CLONE_DEFAULT] = (*fndecls)[0];
37422 /* On the PowerPC, we do not need to call __builtin_cpu_init, which is a NOP
37423 on the PowerPC (on the x86_64, it is not a NOP). The builtin function
37424 __builtin_cpu_support ensures that the TOC fields are setup by requiring a
37425 recent glibc. If we ever need to call __builtin_cpu_init, we would need
37426 to insert the code here to do the call. */
37428 for (ix = 1; fndecls->iterate (ix, &ele); ++ix)
37430 int priority = rs6000_clone_priority (ele);
37431 if (!clones[priority])
37432 clones[priority] = ele;
37435 for (ix = CLONE_MAX - 1; ix >= 0; ix--)
37436 if (clones[ix])
37438 if (TARGET_DEBUG_TARGET)
37439 fprintf (stderr, "dispatch_function_versions, clone %d, %s\n",
37440 ix, get_decl_name (clones[ix]));
37442 *empty_bb = add_condition_to_bb (dispatch_decl, clones[ix], ix,
37443 *empty_bb);
37446 return 0;
37449 /* Generate the dispatching code body to dispatch multi-versioned function
37450 DECL. The target hook is called to process the "target" attributes and
37451 provide the code to dispatch the right function at run-time. NODE points
37452 to the dispatcher decl whose body will be created. */
37454 static tree
37455 rs6000_generate_version_dispatcher_body (void *node_p)
37457 tree resolver;
37458 basic_block empty_bb;
37459 struct cgraph_node *node = (cgraph_node *) node_p;
37460 struct cgraph_function_version_info *ninfo = node->function_version ();
37462 if (ninfo->dispatcher_resolver)
37463 return ninfo->dispatcher_resolver;
37465 /* node is going to be an alias, so remove the finalized bit. */
37466 node->definition = false;
37468 /* The first version in the chain corresponds to the default version. */
37469 ninfo->dispatcher_resolver = resolver
37470 = make_resolver_func (ninfo->next->this_node->decl, node->decl, &empty_bb);
37472 if (TARGET_DEBUG_TARGET)
37473 fprintf (stderr, "rs6000_get_function_versions_dispatcher, %s\n",
37474 get_decl_name (resolver));
37476 push_cfun (DECL_STRUCT_FUNCTION (resolver));
37477 auto_vec<tree, 2> fn_ver_vec;
37479 for (struct cgraph_function_version_info *vinfo = ninfo->next;
37480 vinfo;
37481 vinfo = vinfo->next)
37483 struct cgraph_node *version = vinfo->this_node;
37484 /* Check for virtual functions here again, as by this time it should
37485 have been determined if this function needs a vtable index or
37486 not. This happens for methods in derived classes that override
37487 virtual methods in base classes but are not explicitly marked as
37488 virtual. */
37489 if (DECL_VINDEX (version->decl))
37490 sorry ("Virtual function multiversioning not supported");
37492 fn_ver_vec.safe_push (version->decl);
37495 dispatch_function_versions (resolver, &fn_ver_vec, &empty_bb);
37496 cgraph_edge::rebuild_edges ();
37497 pop_cfun ();
37498 return resolver;
37502 /* Hook to determine if one function can safely inline another. */
37504 static bool
37505 rs6000_can_inline_p (tree caller, tree callee)
37507 bool ret = false;
37508 tree caller_tree = DECL_FUNCTION_SPECIFIC_TARGET (caller);
37509 tree callee_tree = DECL_FUNCTION_SPECIFIC_TARGET (callee);
37511 /* If callee has no option attributes, then it is ok to inline. */
37512 if (!callee_tree)
37513 ret = true;
37515 /* If caller has no option attributes, but callee does then it is not ok to
37516 inline. */
37517 else if (!caller_tree)
37518 ret = false;
37520 else
37522 struct cl_target_option *caller_opts = TREE_TARGET_OPTION (caller_tree);
37523 struct cl_target_option *callee_opts = TREE_TARGET_OPTION (callee_tree);
37525 /* Callee's options should a subset of the caller's, i.e. a vsx function
37526 can inline an altivec function but a non-vsx function can't inline a
37527 vsx function. */
37528 if ((caller_opts->x_rs6000_isa_flags & callee_opts->x_rs6000_isa_flags)
37529 == callee_opts->x_rs6000_isa_flags)
37530 ret = true;
37533 if (TARGET_DEBUG_TARGET)
37534 fprintf (stderr, "rs6000_can_inline_p:, caller %s, callee %s, %s inline\n",
37535 get_decl_name (caller), get_decl_name (callee),
37536 (ret ? "can" : "cannot"));
37538 return ret;
37541 /* Allocate a stack temp and fixup the address so it meets the particular
37542 memory requirements (either offetable or REG+REG addressing). */
37545 rs6000_allocate_stack_temp (machine_mode mode,
37546 bool offsettable_p,
37547 bool reg_reg_p)
37549 rtx stack = assign_stack_temp (mode, GET_MODE_SIZE (mode));
37550 rtx addr = XEXP (stack, 0);
37551 int strict_p = reload_completed;
37553 if (!legitimate_indirect_address_p (addr, strict_p))
37555 if (offsettable_p
37556 && !rs6000_legitimate_offset_address_p (mode, addr, strict_p, true))
37557 stack = replace_equiv_address (stack, copy_addr_to_reg (addr));
37559 else if (reg_reg_p && !legitimate_indexed_address_p (addr, strict_p))
37560 stack = replace_equiv_address (stack, copy_addr_to_reg (addr));
37563 return stack;
37566 /* Given a memory reference, if it is not a reg or reg+reg addressing,
37567 convert to such a form to deal with memory reference instructions
37568 like STFIWX and LDBRX that only take reg+reg addressing. */
37571 rs6000_force_indexed_or_indirect_mem (rtx x)
37573 machine_mode mode = GET_MODE (x);
37575 gcc_assert (MEM_P (x));
37576 if (can_create_pseudo_p () && !indexed_or_indirect_operand (x, mode))
37578 rtx addr = XEXP (x, 0);
37579 if (GET_CODE (addr) == PRE_INC || GET_CODE (addr) == PRE_DEC)
37581 rtx reg = XEXP (addr, 0);
37582 HOST_WIDE_INT size = GET_MODE_SIZE (GET_MODE (x));
37583 rtx size_rtx = GEN_INT ((GET_CODE (addr) == PRE_DEC) ? -size : size);
37584 gcc_assert (REG_P (reg));
37585 emit_insn (gen_add3_insn (reg, reg, size_rtx));
37586 addr = reg;
37588 else if (GET_CODE (addr) == PRE_MODIFY)
37590 rtx reg = XEXP (addr, 0);
37591 rtx expr = XEXP (addr, 1);
37592 gcc_assert (REG_P (reg));
37593 gcc_assert (GET_CODE (expr) == PLUS);
37594 emit_insn (gen_add3_insn (reg, XEXP (expr, 0), XEXP (expr, 1)));
37595 addr = reg;
37598 x = replace_equiv_address (x, force_reg (Pmode, addr));
37601 return x;
37604 /* Implement TARGET_LEGITIMATE_CONSTANT_P.
37606 On the RS/6000, all integer constants are acceptable, most won't be valid
37607 for particular insns, though. Only easy FP constants are acceptable. */
37609 static bool
37610 rs6000_legitimate_constant_p (machine_mode mode, rtx x)
37612 if (TARGET_ELF && tls_referenced_p (x))
37613 return false;
37615 if (CONST_DOUBLE_P (x))
37616 return easy_fp_constant (x, mode);
37618 if (GET_CODE (x) == CONST_VECTOR)
37619 return easy_vector_constant (x, mode);
37621 return true;
37625 /* Return TRUE iff the sequence ending in LAST sets the static chain. */
37627 static bool
37628 chain_already_loaded (rtx_insn *last)
37630 for (; last != NULL; last = PREV_INSN (last))
37632 if (NONJUMP_INSN_P (last))
37634 rtx patt = PATTERN (last);
37636 if (GET_CODE (patt) == SET)
37638 rtx lhs = XEXP (patt, 0);
37640 if (REG_P (lhs) && REGNO (lhs) == STATIC_CHAIN_REGNUM)
37641 return true;
37645 return false;
37648 /* Expand code to perform a call under the AIX or ELFv2 ABI. */
37650 void
37651 rs6000_call_aix (rtx value, rtx func_desc, rtx tlsarg, rtx cookie)
37653 rtx func = func_desc;
37654 rtx toc_reg = gen_rtx_REG (Pmode, TOC_REGNUM);
37655 rtx toc_load = NULL_RTX;
37656 rtx toc_restore = NULL_RTX;
37657 rtx func_addr;
37658 rtx abi_reg = NULL_RTX;
37659 rtx call[4];
37660 int n_call;
37661 rtx insn;
37662 bool is_pltseq_longcall;
37664 if (global_tlsarg)
37665 tlsarg = global_tlsarg;
37667 /* Handle longcall attributes. */
37668 is_pltseq_longcall = false;
37669 if ((INTVAL (cookie) & CALL_LONG) != 0
37670 && GET_CODE (func_desc) == SYMBOL_REF)
37672 func = rs6000_longcall_ref (func_desc, tlsarg);
37673 if (TARGET_PLTSEQ)
37674 is_pltseq_longcall = true;
37677 /* Handle indirect calls. */
37678 if (!SYMBOL_REF_P (func)
37679 || (DEFAULT_ABI == ABI_AIX && !SYMBOL_REF_FUNCTION_P (func)))
37681 /* Save the TOC into its reserved slot before the call,
37682 and prepare to restore it after the call. */
37683 rtx stack_toc_offset = GEN_INT (RS6000_TOC_SAVE_SLOT);
37684 rtx stack_toc_unspec = gen_rtx_UNSPEC (Pmode,
37685 gen_rtvec (1, stack_toc_offset),
37686 UNSPEC_TOCSLOT);
37687 toc_restore = gen_rtx_SET (toc_reg, stack_toc_unspec);
37689 /* Can we optimize saving the TOC in the prologue or
37690 do we need to do it at every call? */
37691 if (TARGET_SAVE_TOC_INDIRECT && !cfun->calls_alloca)
37692 cfun->machine->save_toc_in_prologue = true;
37693 else
37695 rtx stack_ptr = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
37696 rtx stack_toc_mem = gen_frame_mem (Pmode,
37697 gen_rtx_PLUS (Pmode, stack_ptr,
37698 stack_toc_offset));
37699 MEM_VOLATILE_P (stack_toc_mem) = 1;
37700 if (is_pltseq_longcall)
37702 /* Use USPEC_PLTSEQ here to emit every instruction in an
37703 inline PLT call sequence with a reloc, enabling the
37704 linker to edit the sequence back to a direct call
37705 when that makes sense. */
37706 rtvec v = gen_rtvec (3, toc_reg, func_desc, tlsarg);
37707 rtx mark_toc_reg = gen_rtx_UNSPEC (Pmode, v, UNSPEC_PLTSEQ);
37708 emit_insn (gen_rtx_SET (stack_toc_mem, mark_toc_reg));
37710 else
37711 emit_move_insn (stack_toc_mem, toc_reg);
37714 if (DEFAULT_ABI == ABI_ELFv2)
37716 /* A function pointer in the ELFv2 ABI is just a plain address, but
37717 the ABI requires it to be loaded into r12 before the call. */
37718 func_addr = gen_rtx_REG (Pmode, 12);
37719 if (!rtx_equal_p (func_addr, func))
37720 emit_move_insn (func_addr, func);
37721 abi_reg = func_addr;
37722 /* Indirect calls via CTR are strongly preferred over indirect
37723 calls via LR, so move the address there. Needed to mark
37724 this insn for linker plt sequence editing too. */
37725 func_addr = gen_rtx_REG (Pmode, CTR_REGNO);
37726 if (is_pltseq_longcall)
37728 rtvec v = gen_rtvec (3, abi_reg, func_desc, tlsarg);
37729 rtx mark_func = gen_rtx_UNSPEC (Pmode, v, UNSPEC_PLTSEQ);
37730 emit_insn (gen_rtx_SET (func_addr, mark_func));
37731 v = gen_rtvec (2, func_addr, func_desc);
37732 func_addr = gen_rtx_UNSPEC (Pmode, v, UNSPEC_PLTSEQ);
37734 else
37735 emit_move_insn (func_addr, abi_reg);
37737 else
37739 /* A function pointer under AIX is a pointer to a data area whose
37740 first word contains the actual address of the function, whose
37741 second word contains a pointer to its TOC, and whose third word
37742 contains a value to place in the static chain register (r11).
37743 Note that if we load the static chain, our "trampoline" need
37744 not have any executable code. */
37746 /* Load up address of the actual function. */
37747 func = force_reg (Pmode, func);
37748 func_addr = gen_reg_rtx (Pmode);
37749 emit_move_insn (func_addr, gen_rtx_MEM (Pmode, func));
37751 /* Indirect calls via CTR are strongly preferred over indirect
37752 calls via LR, so move the address there. */
37753 rtx ctr_reg = gen_rtx_REG (Pmode, CTR_REGNO);
37754 emit_move_insn (ctr_reg, func_addr);
37755 func_addr = ctr_reg;
37757 /* Prepare to load the TOC of the called function. Note that the
37758 TOC load must happen immediately before the actual call so
37759 that unwinding the TOC registers works correctly. See the
37760 comment in frob_update_context. */
37761 rtx func_toc_offset = GEN_INT (GET_MODE_SIZE (Pmode));
37762 rtx func_toc_mem = gen_rtx_MEM (Pmode,
37763 gen_rtx_PLUS (Pmode, func,
37764 func_toc_offset));
37765 toc_load = gen_rtx_USE (VOIDmode, func_toc_mem);
37767 /* If we have a static chain, load it up. But, if the call was
37768 originally direct, the 3rd word has not been written since no
37769 trampoline has been built, so we ought not to load it, lest we
37770 override a static chain value. */
37771 if (!(GET_CODE (func_desc) == SYMBOL_REF
37772 && SYMBOL_REF_FUNCTION_P (func_desc))
37773 && TARGET_POINTERS_TO_NESTED_FUNCTIONS
37774 && !chain_already_loaded (get_current_sequence ()->next->last))
37776 rtx sc_reg = gen_rtx_REG (Pmode, STATIC_CHAIN_REGNUM);
37777 rtx func_sc_offset = GEN_INT (2 * GET_MODE_SIZE (Pmode));
37778 rtx func_sc_mem = gen_rtx_MEM (Pmode,
37779 gen_rtx_PLUS (Pmode, func,
37780 func_sc_offset));
37781 emit_move_insn (sc_reg, func_sc_mem);
37782 abi_reg = sc_reg;
37786 else
37788 /* Direct calls use the TOC: for local calls, the callee will
37789 assume the TOC register is set; for non-local calls, the
37790 PLT stub needs the TOC register. */
37791 abi_reg = toc_reg;
37792 func_addr = func;
37795 /* Create the call. */
37796 call[0] = gen_rtx_CALL (VOIDmode, gen_rtx_MEM (SImode, func_addr), tlsarg);
37797 if (value != NULL_RTX)
37798 call[0] = gen_rtx_SET (value, call[0]);
37799 n_call = 1;
37801 if (toc_load)
37802 call[n_call++] = toc_load;
37803 if (toc_restore)
37804 call[n_call++] = toc_restore;
37806 call[n_call++] = gen_hard_reg_clobber (Pmode, LR_REGNO);
37808 insn = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (n_call, call));
37809 insn = emit_call_insn (insn);
37811 /* Mention all registers defined by the ABI to hold information
37812 as uses in CALL_INSN_FUNCTION_USAGE. */
37813 if (abi_reg)
37814 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), abi_reg);
37817 /* Expand code to perform a sibling call under the AIX or ELFv2 ABI. */
37819 void
37820 rs6000_sibcall_aix (rtx value, rtx func_desc, rtx tlsarg, rtx cookie)
37822 rtx call[2];
37823 rtx insn;
37825 gcc_assert (INTVAL (cookie) == 0);
37827 if (global_tlsarg)
37828 tlsarg = global_tlsarg;
37830 /* Create the call. */
37831 call[0] = gen_rtx_CALL (VOIDmode, gen_rtx_MEM (SImode, func_desc), tlsarg);
37832 if (value != NULL_RTX)
37833 call[0] = gen_rtx_SET (value, call[0]);
37835 call[1] = simple_return_rtx;
37837 insn = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (2, call));
37838 insn = emit_call_insn (insn);
37840 /* Note use of the TOC register. */
37841 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), gen_rtx_REG (Pmode, TOC_REGNUM));
37844 /* Expand code to perform a call under the SYSV4 ABI. */
37846 void
37847 rs6000_call_sysv (rtx value, rtx func_desc, rtx tlsarg, rtx cookie)
37849 rtx func = func_desc;
37850 rtx func_addr;
37851 rtx call[4];
37852 rtx insn;
37853 rtx abi_reg = NULL_RTX;
37854 int n;
37856 if (global_tlsarg)
37857 tlsarg = global_tlsarg;
37859 /* Handle longcall attributes. */
37860 if ((INTVAL (cookie) & CALL_LONG) != 0
37861 && GET_CODE (func_desc) == SYMBOL_REF)
37863 func = rs6000_longcall_ref (func_desc, tlsarg);
37864 /* If the longcall was implemented as an inline PLT call using
37865 PLT unspecs then func will be REG:r11. If not, func will be
37866 a pseudo reg. The inline PLT call sequence supports lazy
37867 linking (and longcalls to functions in dlopen'd libraries).
37868 The other style of longcalls don't. The lazy linking entry
37869 to the dynamic symbol resolver requires r11 be the function
37870 address (as it is for linker generated PLT stubs). Ensure
37871 r11 stays valid to the bctrl by marking r11 used by the call. */
37872 if (TARGET_PLTSEQ)
37873 abi_reg = func;
37876 /* Handle indirect calls. */
37877 if (GET_CODE (func) != SYMBOL_REF)
37879 func = force_reg (Pmode, func);
37881 /* Indirect calls via CTR are strongly preferred over indirect
37882 calls via LR, so move the address there. That can't be left
37883 to reload because we want to mark every instruction in an
37884 inline PLT call sequence with a reloc, enabling the linker to
37885 edit the sequence back to a direct call when that makes sense. */
37886 func_addr = gen_rtx_REG (Pmode, CTR_REGNO);
37887 if (abi_reg)
37889 rtvec v = gen_rtvec (3, func, func_desc, tlsarg);
37890 rtx mark_func = gen_rtx_UNSPEC (Pmode, v, UNSPEC_PLTSEQ);
37891 emit_insn (gen_rtx_SET (func_addr, mark_func));
37892 v = gen_rtvec (2, func_addr, func_desc);
37893 func_addr = gen_rtx_UNSPEC (Pmode, v, UNSPEC_PLTSEQ);
37895 else
37896 emit_move_insn (func_addr, func);
37898 else
37899 func_addr = func;
37901 /* Create the call. */
37902 call[0] = gen_rtx_CALL (VOIDmode, gen_rtx_MEM (SImode, func_addr), tlsarg);
37903 if (value != NULL_RTX)
37904 call[0] = gen_rtx_SET (value, call[0]);
37906 call[1] = gen_rtx_USE (VOIDmode, cookie);
37907 n = 2;
37908 if (TARGET_SECURE_PLT
37909 && flag_pic
37910 && GET_CODE (func_addr) == SYMBOL_REF
37911 && !SYMBOL_REF_LOCAL_P (func_addr))
37912 call[n++] = gen_rtx_USE (VOIDmode, pic_offset_table_rtx);
37914 call[n++] = gen_hard_reg_clobber (Pmode, LR_REGNO);
37916 insn = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (n, call));
37917 insn = emit_call_insn (insn);
37918 if (abi_reg)
37919 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), abi_reg);
37922 /* Expand code to perform a sibling call under the SysV4 ABI. */
37924 void
37925 rs6000_sibcall_sysv (rtx value, rtx func_desc, rtx tlsarg, rtx cookie)
37927 rtx func = func_desc;
37928 rtx func_addr;
37929 rtx call[3];
37930 rtx insn;
37931 rtx abi_reg = NULL_RTX;
37933 if (global_tlsarg)
37934 tlsarg = global_tlsarg;
37936 /* Handle longcall attributes. */
37937 if ((INTVAL (cookie) & CALL_LONG) != 0
37938 && GET_CODE (func_desc) == SYMBOL_REF)
37940 func = rs6000_longcall_ref (func_desc, tlsarg);
37941 /* If the longcall was implemented as an inline PLT call using
37942 PLT unspecs then func will be REG:r11. If not, func will be
37943 a pseudo reg. The inline PLT call sequence supports lazy
37944 linking (and longcalls to functions in dlopen'd libraries).
37945 The other style of longcalls don't. The lazy linking entry
37946 to the dynamic symbol resolver requires r11 be the function
37947 address (as it is for linker generated PLT stubs). Ensure
37948 r11 stays valid to the bctr by marking r11 used by the call. */
37949 if (TARGET_PLTSEQ)
37950 abi_reg = func;
37953 /* Handle indirect calls. */
37954 if (GET_CODE (func) != SYMBOL_REF)
37956 func = force_reg (Pmode, func);
37958 /* Indirect sibcalls must go via CTR. That can't be left to
37959 reload because we want to mark every instruction in an inline
37960 PLT call sequence with a reloc, enabling the linker to edit
37961 the sequence back to a direct call when that makes sense. */
37962 func_addr = gen_rtx_REG (Pmode, CTR_REGNO);
37963 if (abi_reg)
37965 rtvec v = gen_rtvec (3, func, func_desc, tlsarg);
37966 rtx mark_func = gen_rtx_UNSPEC (Pmode, v, UNSPEC_PLTSEQ);
37967 emit_insn (gen_rtx_SET (func_addr, mark_func));
37968 v = gen_rtvec (2, func_addr, func_desc);
37969 func_addr = gen_rtx_UNSPEC (Pmode, v, UNSPEC_PLTSEQ);
37971 else
37972 emit_move_insn (func_addr, func);
37974 else
37975 func_addr = func;
37977 /* Create the call. */
37978 call[0] = gen_rtx_CALL (VOIDmode, gen_rtx_MEM (SImode, func_addr), tlsarg);
37979 if (value != NULL_RTX)
37980 call[0] = gen_rtx_SET (value, call[0]);
37982 call[1] = gen_rtx_USE (VOIDmode, cookie);
37983 call[2] = simple_return_rtx;
37985 insn = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (3, call));
37986 insn = emit_call_insn (insn);
37987 if (abi_reg)
37988 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), abi_reg);
37991 #if TARGET_MACHO
37993 /* Expand code to perform a call under the Darwin ABI.
37994 Modulo handling of mlongcall, this is much the same as sysv.
37995 if/when the longcall optimisation is removed, we could drop this
37996 code and use the sysv case (taking care to avoid the tls stuff).
37998 We can use this for sibcalls too, if needed. */
38000 void
38001 rs6000_call_darwin_1 (rtx value, rtx func_desc, rtx tlsarg,
38002 rtx cookie, bool sibcall)
38004 rtx func = func_desc;
38005 rtx func_addr;
38006 rtx call[3];
38007 rtx insn;
38008 int cookie_val = INTVAL (cookie);
38009 bool make_island = false;
38011 /* Handle longcall attributes, there are two cases for Darwin:
38012 1) Newer linkers are capable of synthesising any branch islands needed.
38013 2) We need a helper branch island synthesised by the compiler.
38014 The second case has mostly been retired and we don't use it for m64.
38015 In fact, it's is an optimisation, we could just indirect as sysv does..
38016 ... however, backwards compatibility for now.
38017 If we're going to use this, then we need to keep the CALL_LONG bit set,
38018 so that we can pick up the special insn form later. */
38019 if ((cookie_val & CALL_LONG) != 0
38020 && GET_CODE (func_desc) == SYMBOL_REF)
38022 if (darwin_emit_branch_islands && TARGET_32BIT)
38023 make_island = true; /* Do nothing yet, retain the CALL_LONG flag. */
38024 else
38026 /* The linker is capable of doing this, but the user explicitly
38027 asked for -mlongcall, so we'll do the 'normal' version. */
38028 func = rs6000_longcall_ref (func_desc, NULL_RTX);
38029 cookie_val &= ~CALL_LONG; /* Handled, zap it. */
38033 /* Handle indirect calls. */
38034 if (GET_CODE (func) != SYMBOL_REF)
38036 func = force_reg (Pmode, func);
38038 /* Indirect calls via CTR are strongly preferred over indirect
38039 calls via LR, and are required for indirect sibcalls, so move
38040 the address there. */
38041 func_addr = gen_rtx_REG (Pmode, CTR_REGNO);
38042 emit_move_insn (func_addr, func);
38044 else
38045 func_addr = func;
38047 /* Create the call. */
38048 call[0] = gen_rtx_CALL (VOIDmode, gen_rtx_MEM (SImode, func_addr), tlsarg);
38049 if (value != NULL_RTX)
38050 call[0] = gen_rtx_SET (value, call[0]);
38052 call[1] = gen_rtx_USE (VOIDmode, GEN_INT (cookie_val));
38054 if (sibcall)
38055 call[2] = simple_return_rtx;
38056 else
38057 call[2] = gen_hard_reg_clobber (Pmode, LR_REGNO);
38059 insn = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (3, call));
38060 insn = emit_call_insn (insn);
38061 /* Now we have the debug info in the insn, we can set up the branch island
38062 if we're using one. */
38063 if (make_island)
38065 tree funname = get_identifier (XSTR (func_desc, 0));
38067 if (no_previous_def (funname))
38069 rtx label_rtx = gen_label_rtx ();
38070 char *label_buf, temp_buf[256];
38071 ASM_GENERATE_INTERNAL_LABEL (temp_buf, "L",
38072 CODE_LABEL_NUMBER (label_rtx));
38073 label_buf = temp_buf[0] == '*' ? temp_buf + 1 : temp_buf;
38074 tree labelname = get_identifier (label_buf);
38075 add_compiler_branch_island (labelname, funname,
38076 insn_line ((const rtx_insn*)insn));
38080 #endif
38082 void
38083 rs6000_call_darwin (rtx value ATTRIBUTE_UNUSED, rtx func_desc ATTRIBUTE_UNUSED,
38084 rtx tlsarg ATTRIBUTE_UNUSED, rtx cookie ATTRIBUTE_UNUSED)
38086 #if TARGET_MACHO
38087 rs6000_call_darwin_1 (value, func_desc, tlsarg, cookie, false);
38088 #else
38089 gcc_unreachable();
38090 #endif
38094 void
38095 rs6000_sibcall_darwin (rtx value ATTRIBUTE_UNUSED, rtx func_desc ATTRIBUTE_UNUSED,
38096 rtx tlsarg ATTRIBUTE_UNUSED, rtx cookie ATTRIBUTE_UNUSED)
38098 #if TARGET_MACHO
38099 rs6000_call_darwin_1 (value, func_desc, tlsarg, cookie, true);
38100 #else
38101 gcc_unreachable();
38102 #endif
38106 /* Return whether we need to always update the saved TOC pointer when we update
38107 the stack pointer. */
38109 static bool
38110 rs6000_save_toc_in_prologue_p (void)
38112 return (cfun && cfun->machine && cfun->machine->save_toc_in_prologue);
38115 #ifdef HAVE_GAS_HIDDEN
38116 # define USE_HIDDEN_LINKONCE 1
38117 #else
38118 # define USE_HIDDEN_LINKONCE 0
38119 #endif
38121 /* Fills in the label name that should be used for a 476 link stack thunk. */
38123 void
38124 get_ppc476_thunk_name (char name[32])
38126 gcc_assert (TARGET_LINK_STACK);
38128 if (USE_HIDDEN_LINKONCE)
38129 sprintf (name, "__ppc476.get_thunk");
38130 else
38131 ASM_GENERATE_INTERNAL_LABEL (name, "LPPC476_", 0);
38134 /* This function emits the simple thunk routine that is used to preserve
38135 the link stack on the 476 cpu. */
38137 static void rs6000_code_end (void) ATTRIBUTE_UNUSED;
38138 static void
38139 rs6000_code_end (void)
38141 char name[32];
38142 tree decl;
38144 if (!TARGET_LINK_STACK)
38145 return;
38147 get_ppc476_thunk_name (name);
38149 decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL, get_identifier (name),
38150 build_function_type_list (void_type_node, NULL_TREE));
38151 DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
38152 NULL_TREE, void_type_node);
38153 TREE_PUBLIC (decl) = 1;
38154 TREE_STATIC (decl) = 1;
38156 #if RS6000_WEAK
38157 if (USE_HIDDEN_LINKONCE && !TARGET_XCOFF)
38159 cgraph_node::create (decl)->set_comdat_group (DECL_ASSEMBLER_NAME (decl));
38160 targetm.asm_out.unique_section (decl, 0);
38161 switch_to_section (get_named_section (decl, NULL, 0));
38162 DECL_WEAK (decl) = 1;
38163 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
38164 targetm.asm_out.globalize_label (asm_out_file, name);
38165 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
38166 ASM_DECLARE_FUNCTION_NAME (asm_out_file, name, decl);
38168 else
38169 #endif
38171 switch_to_section (text_section);
38172 ASM_OUTPUT_LABEL (asm_out_file, name);
38175 DECL_INITIAL (decl) = make_node (BLOCK);
38176 current_function_decl = decl;
38177 allocate_struct_function (decl, false);
38178 init_function_start (decl);
38179 first_function_block_is_cold = false;
38180 /* Make sure unwind info is emitted for the thunk if needed. */
38181 final_start_function (emit_barrier (), asm_out_file, 1);
38183 fputs ("\tblr\n", asm_out_file);
38185 final_end_function ();
38186 init_insn_lengths ();
38187 free_after_compilation (cfun);
38188 set_cfun (NULL);
38189 current_function_decl = NULL;
38192 /* Add r30 to hard reg set if the prologue sets it up and it is not
38193 pic_offset_table_rtx. */
38195 static void
38196 rs6000_set_up_by_prologue (struct hard_reg_set_container *set)
38198 if (!TARGET_SINGLE_PIC_BASE
38199 && TARGET_TOC
38200 && TARGET_MINIMAL_TOC
38201 && !constant_pool_empty_p ())
38202 add_to_hard_reg_set (&set->set, Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
38203 if (cfun->machine->split_stack_argp_used)
38204 add_to_hard_reg_set (&set->set, Pmode, 12);
38206 /* Make sure the hard reg set doesn't include r2, which was possibly added
38207 via PIC_OFFSET_TABLE_REGNUM. */
38208 if (TARGET_TOC)
38209 remove_from_hard_reg_set (&set->set, Pmode, TOC_REGNUM);
38213 /* Helper function for rs6000_split_logical to emit a logical instruction after
38214 spliting the operation to single GPR registers.
38216 DEST is the destination register.
38217 OP1 and OP2 are the input source registers.
38218 CODE is the base operation (AND, IOR, XOR, NOT).
38219 MODE is the machine mode.
38220 If COMPLEMENT_FINAL_P is true, wrap the whole operation with NOT.
38221 If COMPLEMENT_OP1_P is true, wrap operand1 with NOT.
38222 If COMPLEMENT_OP2_P is true, wrap operand2 with NOT. */
38224 static void
38225 rs6000_split_logical_inner (rtx dest,
38226 rtx op1,
38227 rtx op2,
38228 enum rtx_code code,
38229 machine_mode mode,
38230 bool complement_final_p,
38231 bool complement_op1_p,
38232 bool complement_op2_p)
38234 rtx bool_rtx;
38236 /* Optimize AND of 0/0xffffffff and IOR/XOR of 0. */
38237 if (op2 && CONST_INT_P (op2)
38238 && (mode == SImode || (mode == DImode && TARGET_POWERPC64))
38239 && !complement_final_p && !complement_op1_p && !complement_op2_p)
38241 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
38242 HOST_WIDE_INT value = INTVAL (op2) & mask;
38244 /* Optimize AND of 0 to just set 0. Optimize AND of -1 to be a move. */
38245 if (code == AND)
38247 if (value == 0)
38249 emit_insn (gen_rtx_SET (dest, const0_rtx));
38250 return;
38253 else if (value == mask)
38255 if (!rtx_equal_p (dest, op1))
38256 emit_insn (gen_rtx_SET (dest, op1));
38257 return;
38261 /* Optimize IOR/XOR of 0 to be a simple move. Split large operations
38262 into separate ORI/ORIS or XORI/XORIS instrucitons. */
38263 else if (code == IOR || code == XOR)
38265 if (value == 0)
38267 if (!rtx_equal_p (dest, op1))
38268 emit_insn (gen_rtx_SET (dest, op1));
38269 return;
38274 if (code == AND && mode == SImode
38275 && !complement_final_p && !complement_op1_p && !complement_op2_p)
38277 emit_insn (gen_andsi3 (dest, op1, op2));
38278 return;
38281 if (complement_op1_p)
38282 op1 = gen_rtx_NOT (mode, op1);
38284 if (complement_op2_p)
38285 op2 = gen_rtx_NOT (mode, op2);
38287 /* For canonical RTL, if only one arm is inverted it is the first. */
38288 if (!complement_op1_p && complement_op2_p)
38289 std::swap (op1, op2);
38291 bool_rtx = ((code == NOT)
38292 ? gen_rtx_NOT (mode, op1)
38293 : gen_rtx_fmt_ee (code, mode, op1, op2));
38295 if (complement_final_p)
38296 bool_rtx = gen_rtx_NOT (mode, bool_rtx);
38298 emit_insn (gen_rtx_SET (dest, bool_rtx));
38301 /* Split a DImode AND/IOR/XOR with a constant on a 32-bit system. These
38302 operations are split immediately during RTL generation to allow for more
38303 optimizations of the AND/IOR/XOR.
38305 OPERANDS is an array containing the destination and two input operands.
38306 CODE is the base operation (AND, IOR, XOR, NOT).
38307 MODE is the machine mode.
38308 If COMPLEMENT_FINAL_P is true, wrap the whole operation with NOT.
38309 If COMPLEMENT_OP1_P is true, wrap operand1 with NOT.
38310 If COMPLEMENT_OP2_P is true, wrap operand2 with NOT.
38311 CLOBBER_REG is either NULL or a scratch register of type CC to allow
38312 formation of the AND instructions. */
38314 static void
38315 rs6000_split_logical_di (rtx operands[3],
38316 enum rtx_code code,
38317 bool complement_final_p,
38318 bool complement_op1_p,
38319 bool complement_op2_p)
38321 const HOST_WIDE_INT lower_32bits = HOST_WIDE_INT_C(0xffffffff);
38322 const HOST_WIDE_INT upper_32bits = ~ lower_32bits;
38323 const HOST_WIDE_INT sign_bit = HOST_WIDE_INT_C(0x80000000);
38324 enum hi_lo { hi = 0, lo = 1 };
38325 rtx op0_hi_lo[2], op1_hi_lo[2], op2_hi_lo[2];
38326 size_t i;
38328 op0_hi_lo[hi] = gen_highpart (SImode, operands[0]);
38329 op1_hi_lo[hi] = gen_highpart (SImode, operands[1]);
38330 op0_hi_lo[lo] = gen_lowpart (SImode, operands[0]);
38331 op1_hi_lo[lo] = gen_lowpart (SImode, operands[1]);
38333 if (code == NOT)
38334 op2_hi_lo[hi] = op2_hi_lo[lo] = NULL_RTX;
38335 else
38337 if (!CONST_INT_P (operands[2]))
38339 op2_hi_lo[hi] = gen_highpart_mode (SImode, DImode, operands[2]);
38340 op2_hi_lo[lo] = gen_lowpart (SImode, operands[2]);
38342 else
38344 HOST_WIDE_INT value = INTVAL (operands[2]);
38345 HOST_WIDE_INT value_hi_lo[2];
38347 gcc_assert (!complement_final_p);
38348 gcc_assert (!complement_op1_p);
38349 gcc_assert (!complement_op2_p);
38351 value_hi_lo[hi] = value >> 32;
38352 value_hi_lo[lo] = value & lower_32bits;
38354 for (i = 0; i < 2; i++)
38356 HOST_WIDE_INT sub_value = value_hi_lo[i];
38358 if (sub_value & sign_bit)
38359 sub_value |= upper_32bits;
38361 op2_hi_lo[i] = GEN_INT (sub_value);
38363 /* If this is an AND instruction, check to see if we need to load
38364 the value in a register. */
38365 if (code == AND && sub_value != -1 && sub_value != 0
38366 && !and_operand (op2_hi_lo[i], SImode))
38367 op2_hi_lo[i] = force_reg (SImode, op2_hi_lo[i]);
38372 for (i = 0; i < 2; i++)
38374 /* Split large IOR/XOR operations. */
38375 if ((code == IOR || code == XOR)
38376 && CONST_INT_P (op2_hi_lo[i])
38377 && !complement_final_p
38378 && !complement_op1_p
38379 && !complement_op2_p
38380 && !logical_const_operand (op2_hi_lo[i], SImode))
38382 HOST_WIDE_INT value = INTVAL (op2_hi_lo[i]);
38383 HOST_WIDE_INT hi_16bits = value & HOST_WIDE_INT_C(0xffff0000);
38384 HOST_WIDE_INT lo_16bits = value & HOST_WIDE_INT_C(0x0000ffff);
38385 rtx tmp = gen_reg_rtx (SImode);
38387 /* Make sure the constant is sign extended. */
38388 if ((hi_16bits & sign_bit) != 0)
38389 hi_16bits |= upper_32bits;
38391 rs6000_split_logical_inner (tmp, op1_hi_lo[i], GEN_INT (hi_16bits),
38392 code, SImode, false, false, false);
38394 rs6000_split_logical_inner (op0_hi_lo[i], tmp, GEN_INT (lo_16bits),
38395 code, SImode, false, false, false);
38397 else
38398 rs6000_split_logical_inner (op0_hi_lo[i], op1_hi_lo[i], op2_hi_lo[i],
38399 code, SImode, complement_final_p,
38400 complement_op1_p, complement_op2_p);
38403 return;
38406 /* Split the insns that make up boolean operations operating on multiple GPR
38407 registers. The boolean MD patterns ensure that the inputs either are
38408 exactly the same as the output registers, or there is no overlap.
38410 OPERANDS is an array containing the destination and two input operands.
38411 CODE is the base operation (AND, IOR, XOR, NOT).
38412 If COMPLEMENT_FINAL_P is true, wrap the whole operation with NOT.
38413 If COMPLEMENT_OP1_P is true, wrap operand1 with NOT.
38414 If COMPLEMENT_OP2_P is true, wrap operand2 with NOT. */
38416 void
38417 rs6000_split_logical (rtx operands[3],
38418 enum rtx_code code,
38419 bool complement_final_p,
38420 bool complement_op1_p,
38421 bool complement_op2_p)
38423 machine_mode mode = GET_MODE (operands[0]);
38424 machine_mode sub_mode;
38425 rtx op0, op1, op2;
38426 int sub_size, regno0, regno1, nregs, i;
38428 /* If this is DImode, use the specialized version that can run before
38429 register allocation. */
38430 if (mode == DImode && !TARGET_POWERPC64)
38432 rs6000_split_logical_di (operands, code, complement_final_p,
38433 complement_op1_p, complement_op2_p);
38434 return;
38437 op0 = operands[0];
38438 op1 = operands[1];
38439 op2 = (code == NOT) ? NULL_RTX : operands[2];
38440 sub_mode = (TARGET_POWERPC64) ? DImode : SImode;
38441 sub_size = GET_MODE_SIZE (sub_mode);
38442 regno0 = REGNO (op0);
38443 regno1 = REGNO (op1);
38445 gcc_assert (reload_completed);
38446 gcc_assert (IN_RANGE (regno0, FIRST_GPR_REGNO, LAST_GPR_REGNO));
38447 gcc_assert (IN_RANGE (regno1, FIRST_GPR_REGNO, LAST_GPR_REGNO));
38449 nregs = rs6000_hard_regno_nregs[(int)mode][regno0];
38450 gcc_assert (nregs > 1);
38452 if (op2 && REG_P (op2))
38453 gcc_assert (IN_RANGE (REGNO (op2), FIRST_GPR_REGNO, LAST_GPR_REGNO));
38455 for (i = 0; i < nregs; i++)
38457 int offset = i * sub_size;
38458 rtx sub_op0 = simplify_subreg (sub_mode, op0, mode, offset);
38459 rtx sub_op1 = simplify_subreg (sub_mode, op1, mode, offset);
38460 rtx sub_op2 = ((code == NOT)
38461 ? NULL_RTX
38462 : simplify_subreg (sub_mode, op2, mode, offset));
38464 rs6000_split_logical_inner (sub_op0, sub_op1, sub_op2, code, sub_mode,
38465 complement_final_p, complement_op1_p,
38466 complement_op2_p);
38469 return;
38473 /* Return true if the peephole2 can combine a load involving a combination of
38474 an addis instruction and a load with an offset that can be fused together on
38475 a power8. */
38477 bool
38478 fusion_gpr_load_p (rtx addis_reg, /* register set via addis. */
38479 rtx addis_value, /* addis value. */
38480 rtx target, /* target register that is loaded. */
38481 rtx mem) /* bottom part of the memory addr. */
38483 rtx addr;
38484 rtx base_reg;
38486 /* Validate arguments. */
38487 if (!base_reg_operand (addis_reg, GET_MODE (addis_reg)))
38488 return false;
38490 if (!base_reg_operand (target, GET_MODE (target)))
38491 return false;
38493 if (!fusion_gpr_addis (addis_value, GET_MODE (addis_value)))
38494 return false;
38496 /* Allow sign/zero extension. */
38497 if (GET_CODE (mem) == ZERO_EXTEND
38498 || (GET_CODE (mem) == SIGN_EXTEND && TARGET_P8_FUSION_SIGN))
38499 mem = XEXP (mem, 0);
38501 if (!MEM_P (mem))
38502 return false;
38504 if (!fusion_gpr_mem_load (mem, GET_MODE (mem)))
38505 return false;
38507 addr = XEXP (mem, 0); /* either PLUS or LO_SUM. */
38508 if (GET_CODE (addr) != PLUS && GET_CODE (addr) != LO_SUM)
38509 return false;
38511 /* Validate that the register used to load the high value is either the
38512 register being loaded, or we can safely replace its use.
38514 This function is only called from the peephole2 pass and we assume that
38515 there are 2 instructions in the peephole (addis and load), so we want to
38516 check if the target register was not used in the memory address and the
38517 register to hold the addis result is dead after the peephole. */
38518 if (REGNO (addis_reg) != REGNO (target))
38520 if (reg_mentioned_p (target, mem))
38521 return false;
38523 if (!peep2_reg_dead_p (2, addis_reg))
38524 return false;
38526 /* If the target register being loaded is the stack pointer, we must
38527 avoid loading any other value into it, even temporarily. */
38528 if (REG_P (target) && REGNO (target) == STACK_POINTER_REGNUM)
38529 return false;
38532 base_reg = XEXP (addr, 0);
38533 return REGNO (addis_reg) == REGNO (base_reg);
38536 /* During the peephole2 pass, adjust and expand the insns for a load fusion
38537 sequence. We adjust the addis register to use the target register. If the
38538 load sign extends, we adjust the code to do the zero extending load, and an
38539 explicit sign extension later since the fusion only covers zero extending
38540 loads.
38542 The operands are:
38543 operands[0] register set with addis (to be replaced with target)
38544 operands[1] value set via addis
38545 operands[2] target register being loaded
38546 operands[3] D-form memory reference using operands[0]. */
38548 void
38549 expand_fusion_gpr_load (rtx *operands)
38551 rtx addis_value = operands[1];
38552 rtx target = operands[2];
38553 rtx orig_mem = operands[3];
38554 rtx new_addr, new_mem, orig_addr, offset;
38555 enum rtx_code plus_or_lo_sum;
38556 machine_mode target_mode = GET_MODE (target);
38557 machine_mode extend_mode = target_mode;
38558 machine_mode ptr_mode = Pmode;
38559 enum rtx_code extend = UNKNOWN;
38561 if (GET_CODE (orig_mem) == ZERO_EXTEND
38562 || (TARGET_P8_FUSION_SIGN && GET_CODE (orig_mem) == SIGN_EXTEND))
38564 extend = GET_CODE (orig_mem);
38565 orig_mem = XEXP (orig_mem, 0);
38566 target_mode = GET_MODE (orig_mem);
38569 gcc_assert (MEM_P (orig_mem));
38571 orig_addr = XEXP (orig_mem, 0);
38572 plus_or_lo_sum = GET_CODE (orig_addr);
38573 gcc_assert (plus_or_lo_sum == PLUS || plus_or_lo_sum == LO_SUM);
38575 offset = XEXP (orig_addr, 1);
38576 new_addr = gen_rtx_fmt_ee (plus_or_lo_sum, ptr_mode, addis_value, offset);
38577 new_mem = replace_equiv_address_nv (orig_mem, new_addr, false);
38579 if (extend != UNKNOWN)
38580 new_mem = gen_rtx_fmt_e (ZERO_EXTEND, extend_mode, new_mem);
38582 new_mem = gen_rtx_UNSPEC (extend_mode, gen_rtvec (1, new_mem),
38583 UNSPEC_FUSION_GPR);
38584 emit_insn (gen_rtx_SET (target, new_mem));
38586 if (extend == SIGN_EXTEND)
38588 int sub_off = ((BYTES_BIG_ENDIAN)
38589 ? GET_MODE_SIZE (extend_mode) - GET_MODE_SIZE (target_mode)
38590 : 0);
38591 rtx sign_reg
38592 = simplify_subreg (target_mode, target, extend_mode, sub_off);
38594 emit_insn (gen_rtx_SET (target,
38595 gen_rtx_SIGN_EXTEND (extend_mode, sign_reg)));
38598 return;
38601 /* Emit the addis instruction that will be part of a fused instruction
38602 sequence. */
38604 void
38605 emit_fusion_addis (rtx target, rtx addis_value)
38607 rtx fuse_ops[10];
38608 const char *addis_str = NULL;
38610 /* Emit the addis instruction. */
38611 fuse_ops[0] = target;
38612 if (satisfies_constraint_L (addis_value))
38614 fuse_ops[1] = addis_value;
38615 addis_str = "lis %0,%v1";
38618 else if (GET_CODE (addis_value) == PLUS)
38620 rtx op0 = XEXP (addis_value, 0);
38621 rtx op1 = XEXP (addis_value, 1);
38623 if (REG_P (op0) && CONST_INT_P (op1)
38624 && satisfies_constraint_L (op1))
38626 fuse_ops[1] = op0;
38627 fuse_ops[2] = op1;
38628 addis_str = "addis %0,%1,%v2";
38632 else if (GET_CODE (addis_value) == HIGH)
38634 rtx value = XEXP (addis_value, 0);
38635 if (GET_CODE (value) == UNSPEC && XINT (value, 1) == UNSPEC_TOCREL)
38637 fuse_ops[1] = XVECEXP (value, 0, 0); /* symbol ref. */
38638 fuse_ops[2] = XVECEXP (value, 0, 1); /* TOC register. */
38639 if (TARGET_ELF)
38640 addis_str = "addis %0,%2,%1@toc@ha";
38642 else if (TARGET_XCOFF)
38643 addis_str = "addis %0,%1@u(%2)";
38645 else
38646 gcc_unreachable ();
38649 else if (GET_CODE (value) == PLUS)
38651 rtx op0 = XEXP (value, 0);
38652 rtx op1 = XEXP (value, 1);
38654 if (GET_CODE (op0) == UNSPEC
38655 && XINT (op0, 1) == UNSPEC_TOCREL
38656 && CONST_INT_P (op1))
38658 fuse_ops[1] = XVECEXP (op0, 0, 0); /* symbol ref. */
38659 fuse_ops[2] = XVECEXP (op0, 0, 1); /* TOC register. */
38660 fuse_ops[3] = op1;
38661 if (TARGET_ELF)
38662 addis_str = "addis %0,%2,%1+%3@toc@ha";
38664 else if (TARGET_XCOFF)
38665 addis_str = "addis %0,%1+%3@u(%2)";
38667 else
38668 gcc_unreachable ();
38672 else if (satisfies_constraint_L (value))
38674 fuse_ops[1] = value;
38675 addis_str = "lis %0,%v1";
38678 else if (TARGET_ELF && !TARGET_POWERPC64 && CONSTANT_P (value))
38680 fuse_ops[1] = value;
38681 addis_str = "lis %0,%1@ha";
38685 if (!addis_str)
38686 fatal_insn ("Could not generate addis value for fusion", addis_value);
38688 output_asm_insn (addis_str, fuse_ops);
38691 /* Emit a D-form load or store instruction that is the second instruction
38692 of a fusion sequence. */
38694 static void
38695 emit_fusion_load (rtx load_reg, rtx addis_reg, rtx offset, const char *insn_str)
38697 rtx fuse_ops[10];
38698 char insn_template[80];
38700 fuse_ops[0] = load_reg;
38701 fuse_ops[1] = addis_reg;
38703 if (CONST_INT_P (offset) && satisfies_constraint_I (offset))
38705 sprintf (insn_template, "%s %%0,%%2(%%1)", insn_str);
38706 fuse_ops[2] = offset;
38707 output_asm_insn (insn_template, fuse_ops);
38710 else if (GET_CODE (offset) == UNSPEC
38711 && XINT (offset, 1) == UNSPEC_TOCREL)
38713 if (TARGET_ELF)
38714 sprintf (insn_template, "%s %%0,%%2@toc@l(%%1)", insn_str);
38716 else if (TARGET_XCOFF)
38717 sprintf (insn_template, "%s %%0,%%2@l(%%1)", insn_str);
38719 else
38720 gcc_unreachable ();
38722 fuse_ops[2] = XVECEXP (offset, 0, 0);
38723 output_asm_insn (insn_template, fuse_ops);
38726 else if (GET_CODE (offset) == PLUS
38727 && GET_CODE (XEXP (offset, 0)) == UNSPEC
38728 && XINT (XEXP (offset, 0), 1) == UNSPEC_TOCREL
38729 && CONST_INT_P (XEXP (offset, 1)))
38731 rtx tocrel_unspec = XEXP (offset, 0);
38732 if (TARGET_ELF)
38733 sprintf (insn_template, "%s %%0,%%2+%%3@toc@l(%%1)", insn_str);
38735 else if (TARGET_XCOFF)
38736 sprintf (insn_template, "%s %%0,%%2+%%3@l(%%1)", insn_str);
38738 else
38739 gcc_unreachable ();
38741 fuse_ops[2] = XVECEXP (tocrel_unspec, 0, 0);
38742 fuse_ops[3] = XEXP (offset, 1);
38743 output_asm_insn (insn_template, fuse_ops);
38746 else if (TARGET_ELF && !TARGET_POWERPC64 && CONSTANT_P (offset))
38748 sprintf (insn_template, "%s %%0,%%2@l(%%1)", insn_str);
38750 fuse_ops[2] = offset;
38751 output_asm_insn (insn_template, fuse_ops);
38754 else
38755 fatal_insn ("Unable to generate load/store offset for fusion", offset);
38757 return;
38760 /* Given an address, convert it into the addis and load offset parts. Addresses
38761 created during the peephole2 process look like:
38762 (lo_sum (high (unspec [(sym)] UNSPEC_TOCREL))
38763 (unspec [(...)] UNSPEC_TOCREL)) */
38765 static void
38766 fusion_split_address (rtx addr, rtx *p_hi, rtx *p_lo)
38768 rtx hi, lo;
38770 if (GET_CODE (addr) == PLUS || GET_CODE (addr) == LO_SUM)
38772 hi = XEXP (addr, 0);
38773 lo = XEXP (addr, 1);
38775 else
38776 gcc_unreachable ();
38778 *p_hi = hi;
38779 *p_lo = lo;
38782 /* Return a string to fuse an addis instruction with a gpr load to the same
38783 register that we loaded up the addis instruction. The address that is used
38784 is the logical address that was formed during peephole2:
38785 (lo_sum (high) (low-part))
38787 The code is complicated, so we call output_asm_insn directly, and just
38788 return "". */
38790 const char *
38791 emit_fusion_gpr_load (rtx target, rtx mem)
38793 rtx addis_value;
38794 rtx addr;
38795 rtx load_offset;
38796 const char *load_str = NULL;
38797 machine_mode mode;
38799 if (GET_CODE (mem) == ZERO_EXTEND)
38800 mem = XEXP (mem, 0);
38802 gcc_assert (REG_P (target) && MEM_P (mem));
38804 addr = XEXP (mem, 0);
38805 fusion_split_address (addr, &addis_value, &load_offset);
38807 /* Now emit the load instruction to the same register. */
38808 mode = GET_MODE (mem);
38809 switch (mode)
38811 case E_QImode:
38812 load_str = "lbz";
38813 break;
38815 case E_HImode:
38816 load_str = "lhz";
38817 break;
38819 case E_SImode:
38820 case E_SFmode:
38821 load_str = "lwz";
38822 break;
38824 case E_DImode:
38825 case E_DFmode:
38826 gcc_assert (TARGET_POWERPC64);
38827 load_str = "ld";
38828 break;
38830 default:
38831 fatal_insn ("Bad GPR fusion", gen_rtx_SET (target, mem));
38834 /* Emit the addis instruction. */
38835 emit_fusion_addis (target, addis_value);
38837 /* Emit the D-form load instruction. */
38838 emit_fusion_load (target, target, load_offset, load_str);
38840 return "";
38844 #ifdef RS6000_GLIBC_ATOMIC_FENV
38845 /* Function declarations for rs6000_atomic_assign_expand_fenv. */
38846 static tree atomic_hold_decl, atomic_clear_decl, atomic_update_decl;
38847 #endif
38849 /* Implement TARGET_ATOMIC_ASSIGN_EXPAND_FENV hook. */
38851 static void
38852 rs6000_atomic_assign_expand_fenv (tree *hold, tree *clear, tree *update)
38854 if (!TARGET_HARD_FLOAT)
38856 #ifdef RS6000_GLIBC_ATOMIC_FENV
38857 if (atomic_hold_decl == NULL_TREE)
38859 atomic_hold_decl
38860 = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
38861 get_identifier ("__atomic_feholdexcept"),
38862 build_function_type_list (void_type_node,
38863 double_ptr_type_node,
38864 NULL_TREE));
38865 TREE_PUBLIC (atomic_hold_decl) = 1;
38866 DECL_EXTERNAL (atomic_hold_decl) = 1;
38869 if (atomic_clear_decl == NULL_TREE)
38871 atomic_clear_decl
38872 = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
38873 get_identifier ("__atomic_feclearexcept"),
38874 build_function_type_list (void_type_node,
38875 NULL_TREE));
38876 TREE_PUBLIC (atomic_clear_decl) = 1;
38877 DECL_EXTERNAL (atomic_clear_decl) = 1;
38880 tree const_double = build_qualified_type (double_type_node,
38881 TYPE_QUAL_CONST);
38882 tree const_double_ptr = build_pointer_type (const_double);
38883 if (atomic_update_decl == NULL_TREE)
38885 atomic_update_decl
38886 = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
38887 get_identifier ("__atomic_feupdateenv"),
38888 build_function_type_list (void_type_node,
38889 const_double_ptr,
38890 NULL_TREE));
38891 TREE_PUBLIC (atomic_update_decl) = 1;
38892 DECL_EXTERNAL (atomic_update_decl) = 1;
38895 tree fenv_var = create_tmp_var_raw (double_type_node);
38896 TREE_ADDRESSABLE (fenv_var) = 1;
38897 tree fenv_addr = build1 (ADDR_EXPR, double_ptr_type_node, fenv_var);
38899 *hold = build_call_expr (atomic_hold_decl, 1, fenv_addr);
38900 *clear = build_call_expr (atomic_clear_decl, 0);
38901 *update = build_call_expr (atomic_update_decl, 1,
38902 fold_convert (const_double_ptr, fenv_addr));
38903 #endif
38904 return;
38907 tree mffs = rs6000_builtin_decls[RS6000_BUILTIN_MFFS];
38908 tree mtfsf = rs6000_builtin_decls[RS6000_BUILTIN_MTFSF];
38909 tree call_mffs = build_call_expr (mffs, 0);
38911 /* Generates the equivalent of feholdexcept (&fenv_var)
38913 *fenv_var = __builtin_mffs ();
38914 double fenv_hold;
38915 *(uint64_t*)&fenv_hold = *(uint64_t*)fenv_var & 0xffffffff00000007LL;
38916 __builtin_mtfsf (0xff, fenv_hold); */
38918 /* Mask to clear everything except for the rounding modes and non-IEEE
38919 arithmetic flag. */
38920 const unsigned HOST_WIDE_INT hold_exception_mask =
38921 HOST_WIDE_INT_C (0xffffffff00000007);
38923 tree fenv_var = create_tmp_var_raw (double_type_node);
38925 tree hold_mffs = build2 (MODIFY_EXPR, void_type_node, fenv_var, call_mffs);
38927 tree fenv_llu = build1 (VIEW_CONVERT_EXPR, uint64_type_node, fenv_var);
38928 tree fenv_llu_and = build2 (BIT_AND_EXPR, uint64_type_node, fenv_llu,
38929 build_int_cst (uint64_type_node,
38930 hold_exception_mask));
38932 tree fenv_hold_mtfsf = build1 (VIEW_CONVERT_EXPR, double_type_node,
38933 fenv_llu_and);
38935 tree hold_mtfsf = build_call_expr (mtfsf, 2,
38936 build_int_cst (unsigned_type_node, 0xff),
38937 fenv_hold_mtfsf);
38939 *hold = build2 (COMPOUND_EXPR, void_type_node, hold_mffs, hold_mtfsf);
38941 /* Generates the equivalent of feclearexcept (FE_ALL_EXCEPT):
38943 double fenv_clear = __builtin_mffs ();
38944 *(uint64_t)&fenv_clear &= 0xffffffff00000000LL;
38945 __builtin_mtfsf (0xff, fenv_clear); */
38947 /* Mask to clear everything except for the rounding modes and non-IEEE
38948 arithmetic flag. */
38949 const unsigned HOST_WIDE_INT clear_exception_mask =
38950 HOST_WIDE_INT_C (0xffffffff00000000);
38952 tree fenv_clear = create_tmp_var_raw (double_type_node);
38954 tree clear_mffs = build2 (MODIFY_EXPR, void_type_node, fenv_clear, call_mffs);
38956 tree fenv_clean_llu = build1 (VIEW_CONVERT_EXPR, uint64_type_node, fenv_clear);
38957 tree fenv_clear_llu_and = build2 (BIT_AND_EXPR, uint64_type_node,
38958 fenv_clean_llu,
38959 build_int_cst (uint64_type_node,
38960 clear_exception_mask));
38962 tree fenv_clear_mtfsf = build1 (VIEW_CONVERT_EXPR, double_type_node,
38963 fenv_clear_llu_and);
38965 tree clear_mtfsf = build_call_expr (mtfsf, 2,
38966 build_int_cst (unsigned_type_node, 0xff),
38967 fenv_clear_mtfsf);
38969 *clear = build2 (COMPOUND_EXPR, void_type_node, clear_mffs, clear_mtfsf);
38971 /* Generates the equivalent of feupdateenv (&fenv_var)
38973 double old_fenv = __builtin_mffs ();
38974 double fenv_update;
38975 *(uint64_t*)&fenv_update = (*(uint64_t*)&old & 0xffffffff1fffff00LL) |
38976 (*(uint64_t*)fenv_var 0x1ff80fff);
38977 __builtin_mtfsf (0xff, fenv_update); */
38979 const unsigned HOST_WIDE_INT update_exception_mask =
38980 HOST_WIDE_INT_C (0xffffffff1fffff00);
38981 const unsigned HOST_WIDE_INT new_exception_mask =
38982 HOST_WIDE_INT_C (0x1ff80fff);
38984 tree old_fenv = create_tmp_var_raw (double_type_node);
38985 tree update_mffs = build2 (MODIFY_EXPR, void_type_node, old_fenv, call_mffs);
38987 tree old_llu = build1 (VIEW_CONVERT_EXPR, uint64_type_node, old_fenv);
38988 tree old_llu_and = build2 (BIT_AND_EXPR, uint64_type_node, old_llu,
38989 build_int_cst (uint64_type_node,
38990 update_exception_mask));
38992 tree new_llu_and = build2 (BIT_AND_EXPR, uint64_type_node, fenv_llu,
38993 build_int_cst (uint64_type_node,
38994 new_exception_mask));
38996 tree new_llu_mask = build2 (BIT_IOR_EXPR, uint64_type_node,
38997 old_llu_and, new_llu_and);
38999 tree fenv_update_mtfsf = build1 (VIEW_CONVERT_EXPR, double_type_node,
39000 new_llu_mask);
39002 tree update_mtfsf = build_call_expr (mtfsf, 2,
39003 build_int_cst (unsigned_type_node, 0xff),
39004 fenv_update_mtfsf);
39006 *update = build2 (COMPOUND_EXPR, void_type_node, update_mffs, update_mtfsf);
39009 void
39010 rs6000_generate_float2_double_code (rtx dst, rtx src1, rtx src2)
39012 rtx rtx_tmp0, rtx_tmp1, rtx_tmp2, rtx_tmp3;
39014 rtx_tmp0 = gen_reg_rtx (V2DFmode);
39015 rtx_tmp1 = gen_reg_rtx (V2DFmode);
39017 /* The destination of the vmrgew instruction layout is:
39018 rtx_tmp2[0] rtx_tmp3[0] rtx_tmp2[1] rtx_tmp3[0].
39019 Setup rtx_tmp0 and rtx_tmp1 to ensure the order of the elements after the
39020 vmrgew instruction will be correct. */
39021 if (BYTES_BIG_ENDIAN)
39023 emit_insn (gen_vsx_xxpermdi_v2df_be (rtx_tmp0, src1, src2,
39024 GEN_INT (0)));
39025 emit_insn (gen_vsx_xxpermdi_v2df_be (rtx_tmp1, src1, src2,
39026 GEN_INT (3)));
39028 else
39030 emit_insn (gen_vsx_xxpermdi_v2df (rtx_tmp0, src1, src2, GEN_INT (3)));
39031 emit_insn (gen_vsx_xxpermdi_v2df (rtx_tmp1, src1, src2, GEN_INT (0)));
39034 rtx_tmp2 = gen_reg_rtx (V4SFmode);
39035 rtx_tmp3 = gen_reg_rtx (V4SFmode);
39037 emit_insn (gen_vsx_xvcdpsp (rtx_tmp2, rtx_tmp0));
39038 emit_insn (gen_vsx_xvcdpsp (rtx_tmp3, rtx_tmp1));
39040 if (BYTES_BIG_ENDIAN)
39041 emit_insn (gen_p8_vmrgew_v4sf (dst, rtx_tmp2, rtx_tmp3));
39042 else
39043 emit_insn (gen_p8_vmrgew_v4sf (dst, rtx_tmp3, rtx_tmp2));
39046 void
39047 rs6000_generate_float2_code (bool signed_convert, rtx dst, rtx src1, rtx src2)
39049 rtx rtx_tmp0, rtx_tmp1, rtx_tmp2, rtx_tmp3;
39051 rtx_tmp0 = gen_reg_rtx (V2DImode);
39052 rtx_tmp1 = gen_reg_rtx (V2DImode);
39054 /* The destination of the vmrgew instruction layout is:
39055 rtx_tmp2[0] rtx_tmp3[0] rtx_tmp2[1] rtx_tmp3[0].
39056 Setup rtx_tmp0 and rtx_tmp1 to ensure the order of the elements after the
39057 vmrgew instruction will be correct. */
39058 if (BYTES_BIG_ENDIAN)
39060 emit_insn (gen_vsx_xxpermdi_v2di_be (rtx_tmp0, src1, src2, GEN_INT (0)));
39061 emit_insn (gen_vsx_xxpermdi_v2di_be (rtx_tmp1, src1, src2, GEN_INT (3)));
39063 else
39065 emit_insn (gen_vsx_xxpermdi_v2di (rtx_tmp0, src1, src2, GEN_INT (3)));
39066 emit_insn (gen_vsx_xxpermdi_v2di (rtx_tmp1, src1, src2, GEN_INT (0)));
39069 rtx_tmp2 = gen_reg_rtx (V4SFmode);
39070 rtx_tmp3 = gen_reg_rtx (V4SFmode);
39072 if (signed_convert)
39074 emit_insn (gen_vsx_xvcvsxdsp (rtx_tmp2, rtx_tmp0));
39075 emit_insn (gen_vsx_xvcvsxdsp (rtx_tmp3, rtx_tmp1));
39077 else
39079 emit_insn (gen_vsx_xvcvuxdsp (rtx_tmp2, rtx_tmp0));
39080 emit_insn (gen_vsx_xvcvuxdsp (rtx_tmp3, rtx_tmp1));
39083 if (BYTES_BIG_ENDIAN)
39084 emit_insn (gen_p8_vmrgew_v4sf (dst, rtx_tmp2, rtx_tmp3));
39085 else
39086 emit_insn (gen_p8_vmrgew_v4sf (dst, rtx_tmp3, rtx_tmp2));
39089 void
39090 rs6000_generate_vsigned2_code (bool signed_convert, rtx dst, rtx src1,
39091 rtx src2)
39093 rtx rtx_tmp0, rtx_tmp1, rtx_tmp2, rtx_tmp3;
39095 rtx_tmp0 = gen_reg_rtx (V2DFmode);
39096 rtx_tmp1 = gen_reg_rtx (V2DFmode);
39098 emit_insn (gen_vsx_xxpermdi_v2df (rtx_tmp0, src1, src2, GEN_INT (0)));
39099 emit_insn (gen_vsx_xxpermdi_v2df (rtx_tmp1, src1, src2, GEN_INT (3)));
39101 rtx_tmp2 = gen_reg_rtx (V4SImode);
39102 rtx_tmp3 = gen_reg_rtx (V4SImode);
39104 if (signed_convert)
39106 emit_insn (gen_vsx_xvcvdpsxws (rtx_tmp2, rtx_tmp0));
39107 emit_insn (gen_vsx_xvcvdpsxws (rtx_tmp3, rtx_tmp1));
39109 else
39111 emit_insn (gen_vsx_xvcvdpuxws (rtx_tmp2, rtx_tmp0));
39112 emit_insn (gen_vsx_xvcvdpuxws (rtx_tmp3, rtx_tmp1));
39115 emit_insn (gen_p8_vmrgew_v4si (dst, rtx_tmp2, rtx_tmp3));
39118 /* Implement the TARGET_OPTAB_SUPPORTED_P hook. */
39120 static bool
39121 rs6000_optab_supported_p (int op, machine_mode mode1, machine_mode,
39122 optimization_type opt_type)
39124 switch (op)
39126 case rsqrt_optab:
39127 return (opt_type == OPTIMIZE_FOR_SPEED
39128 && RS6000_RECIP_AUTO_RSQRTE_P (mode1));
39130 default:
39131 return true;
39135 /* Implement TARGET_CONSTANT_ALIGNMENT. */
39137 static HOST_WIDE_INT
39138 rs6000_constant_alignment (const_tree exp, HOST_WIDE_INT align)
39140 if (TREE_CODE (exp) == STRING_CST
39141 && (STRICT_ALIGNMENT || !optimize_size))
39142 return MAX (align, BITS_PER_WORD);
39143 return align;
39146 /* Implement TARGET_STARTING_FRAME_OFFSET. */
39148 static HOST_WIDE_INT
39149 rs6000_starting_frame_offset (void)
39151 if (FRAME_GROWS_DOWNWARD)
39152 return 0;
39153 return RS6000_STARTING_FRAME_OFFSET;
39157 /* Create an alias for a mangled name where we have changed the mangling (in
39158 GCC 8.1, we used U10__float128, and now we use u9__ieee128). This is called
39159 via the target hook TARGET_ASM_GLOBALIZE_DECL_NAME. */
39161 #if TARGET_ELF && RS6000_WEAK
39162 static void
39163 rs6000_globalize_decl_name (FILE * stream, tree decl)
39165 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
39167 targetm.asm_out.globalize_label (stream, name);
39169 if (rs6000_passes_ieee128 && name[0] == '_' && name[1] == 'Z')
39171 tree save_asm_name = DECL_ASSEMBLER_NAME (decl);
39172 const char *old_name;
39174 ieee128_mangling_gcc_8_1 = true;
39175 lang_hooks.set_decl_assembler_name (decl);
39176 old_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
39177 SET_DECL_ASSEMBLER_NAME (decl, save_asm_name);
39178 ieee128_mangling_gcc_8_1 = false;
39180 if (strcmp (name, old_name) != 0)
39182 fprintf (stream, "\t.weak %s\n", old_name);
39183 fprintf (stream, "\t.set %s,%s\n", old_name, name);
39187 #endif
39190 /* On 64-bit Linux and Freebsd systems, possibly switch the long double library
39191 function names from <foo>l to <foo>f128 if the default long double type is
39192 IEEE 128-bit. Typically, with the C and C++ languages, the standard math.h
39193 include file switches the names on systems that support long double as IEEE
39194 128-bit, but that doesn't work if the user uses __builtin_<foo>l directly.
39195 In the future, glibc will export names like __ieee128_sinf128 and we can
39196 switch to using those instead of using sinf128, which pollutes the user's
39197 namespace.
39199 This will switch the names for Fortran math functions as well (which doesn't
39200 use math.h). However, Fortran needs other changes to the compiler and
39201 library before you can switch the real*16 type at compile time.
39203 We use the TARGET_MANGLE_DECL_ASSEMBLER_NAME hook to change this name. We
39204 only do this if the default is that long double is IBM extended double, and
39205 the user asked for IEEE 128-bit. */
39207 static tree
39208 rs6000_mangle_decl_assembler_name (tree decl, tree id)
39210 if (!TARGET_IEEEQUAD_DEFAULT && TARGET_IEEEQUAD && TARGET_LONG_DOUBLE_128
39211 && TREE_CODE (decl) == FUNCTION_DECL && DECL_IS_BUILTIN (decl) )
39213 size_t len = IDENTIFIER_LENGTH (id);
39214 const char *name = IDENTIFIER_POINTER (id);
39216 if (name[len - 1] == 'l')
39218 bool uses_ieee128_p = false;
39219 tree type = TREE_TYPE (decl);
39220 machine_mode ret_mode = TYPE_MODE (type);
39222 /* See if the function returns a IEEE 128-bit floating point type or
39223 complex type. */
39224 if (ret_mode == TFmode || ret_mode == TCmode)
39225 uses_ieee128_p = true;
39226 else
39228 function_args_iterator args_iter;
39229 tree arg;
39231 /* See if the function passes a IEEE 128-bit floating point type
39232 or complex type. */
39233 FOREACH_FUNCTION_ARGS (type, arg, args_iter)
39235 machine_mode arg_mode = TYPE_MODE (arg);
39236 if (arg_mode == TFmode || arg_mode == TCmode)
39238 uses_ieee128_p = true;
39239 break;
39244 /* If we passed or returned an IEEE 128-bit floating point type,
39245 change the name. */
39246 if (uses_ieee128_p)
39248 char *name2 = (char *) alloca (len + 4);
39249 memcpy (name2, name, len - 1);
39250 strcpy (name2 + len - 1, "f128");
39251 id = get_identifier (name2);
39256 return id;
39260 struct gcc_target targetm = TARGET_INITIALIZER;
39262 #include "gt-rs6000.h"