* cgraph.h: Flatten. Remove all include files.
[official-gcc.git] / gcc / config / rs6000 / rs6000.c
blobcd13e77c854f6c1b10576486e79385222f632d28
1 /* Subroutines used for code generation on IBM RS/6000.
2 Copyright (C) 1991-2014 Free Software Foundation, Inc.
3 Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published
9 by the Free Software Foundation; either version 3, or (at your
10 option) any later version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "regs.h"
27 #include "hard-reg-set.h"
28 #include "insn-config.h"
29 #include "conditions.h"
30 #include "insn-attr.h"
31 #include "flags.h"
32 #include "recog.h"
33 #include "obstack.h"
34 #include "tree.h"
35 #include "stringpool.h"
36 #include "stor-layout.h"
37 #include "calls.h"
38 #include "print-tree.h"
39 #include "varasm.h"
40 #include "expr.h"
41 #include "optabs.h"
42 #include "except.h"
43 #include "hashtab.h"
44 #include "hash-set.h"
45 #include "vec.h"
46 #include "machmode.h"
47 #include "input.h"
48 #include "function.h"
49 #include "output.h"
50 #include "dbxout.h"
51 #include "predict.h"
52 #include "dominance.h"
53 #include "cfg.h"
54 #include "cfgrtl.h"
55 #include "cfganal.h"
56 #include "lcm.h"
57 #include "cfgbuild.h"
58 #include "cfgcleanup.h"
59 #include "basic-block.h"
60 #include "diagnostic-core.h"
61 #include "toplev.h"
62 #include "ggc.h"
63 #include "tm_p.h"
64 #include "target.h"
65 #include "target-def.h"
66 #include "common/common-target.h"
67 #include "langhooks.h"
68 #include "reload.h"
69 #include "cfgloop.h"
70 #include "sched-int.h"
71 #include "hash-table.h"
72 #include "tree-ssa-alias.h"
73 #include "internal-fn.h"
74 #include "gimple-fold.h"
75 #include "tree-eh.h"
76 #include "gimple-expr.h"
77 #include "is-a.h"
78 #include "gimple.h"
79 #include "gimplify.h"
80 #include "gimple-iterator.h"
81 #include "gimple-walk.h"
82 #include "intl.h"
83 #include "params.h"
84 #include "tm-constrs.h"
85 #include "ira.h"
86 #include "opts.h"
87 #include "tree-vectorizer.h"
88 #include "dumpfile.h"
89 #include "hash-map.h"
90 #include "plugin-api.h"
91 #include "ipa-ref.h"
92 #include "cgraph.h"
93 #include "target-globals.h"
94 #include "builtins.h"
95 #include "context.h"
96 #include "tree-pass.h"
97 #include "real.h"
98 #if TARGET_XCOFF
99 #include "xcoffout.h" /* get declarations of xcoff_*_section_name */
100 #endif
101 #if TARGET_MACHO
102 #include "gstab.h" /* for N_SLINE */
103 #endif
105 #ifndef TARGET_NO_PROTOTYPE
106 #define TARGET_NO_PROTOTYPE 0
107 #endif
109 #define min(A,B) ((A) < (B) ? (A) : (B))
110 #define max(A,B) ((A) > (B) ? (A) : (B))
112 /* Structure used to define the rs6000 stack */
113 typedef struct rs6000_stack {
114 int reload_completed; /* stack info won't change from here on */
115 int first_gp_reg_save; /* first callee saved GP register used */
116 int first_fp_reg_save; /* first callee saved FP register used */
117 int first_altivec_reg_save; /* first callee saved AltiVec register used */
118 int lr_save_p; /* true if the link reg needs to be saved */
119 int cr_save_p; /* true if the CR reg needs to be saved */
120 unsigned int vrsave_mask; /* mask of vec registers to save */
121 int push_p; /* true if we need to allocate stack space */
122 int calls_p; /* true if the function makes any calls */
123 int world_save_p; /* true if we're saving *everything*:
124 r13-r31, cr, f14-f31, vrsave, v20-v31 */
125 enum rs6000_abi abi; /* which ABI to use */
126 int gp_save_offset; /* offset to save GP regs from initial SP */
127 int fp_save_offset; /* offset to save FP regs from initial SP */
128 int altivec_save_offset; /* offset to save AltiVec regs from initial SP */
129 int lr_save_offset; /* offset to save LR from initial SP */
130 int cr_save_offset; /* offset to save CR from initial SP */
131 int vrsave_save_offset; /* offset to save VRSAVE from initial SP */
132 int spe_gp_save_offset; /* offset to save spe 64-bit gprs */
133 int varargs_save_offset; /* offset to save the varargs registers */
134 int ehrd_offset; /* offset to EH return data */
135 int ehcr_offset; /* offset to EH CR field data */
136 int reg_size; /* register size (4 or 8) */
137 HOST_WIDE_INT vars_size; /* variable save area size */
138 int parm_size; /* outgoing parameter size */
139 int save_size; /* save area size */
140 int fixed_size; /* fixed size of stack frame */
141 int gp_size; /* size of saved GP registers */
142 int fp_size; /* size of saved FP registers */
143 int altivec_size; /* size of saved AltiVec registers */
144 int cr_size; /* size to hold CR if not in save_size */
145 int vrsave_size; /* size to hold VRSAVE if not in save_size */
146 int altivec_padding_size; /* size of altivec alignment padding if
147 not in save_size */
148 int spe_gp_size; /* size of 64-bit GPR save size for SPE */
149 int spe_padding_size;
150 HOST_WIDE_INT total_size; /* total bytes allocated for stack */
151 int spe_64bit_regs_used;
152 int savres_strategy;
153 } rs6000_stack_t;
155 /* A C structure for machine-specific, per-function data.
156 This is added to the cfun structure. */
157 typedef struct GTY(()) machine_function
159 /* Whether the instruction chain has been scanned already. */
160 int insn_chain_scanned_p;
161 /* Flags if __builtin_return_address (n) with n >= 1 was used. */
162 int ra_needs_full_frame;
163 /* Flags if __builtin_return_address (0) was used. */
164 int ra_need_lr;
165 /* Cache lr_save_p after expansion of builtin_eh_return. */
166 int lr_save_state;
167 /* Whether we need to save the TOC to the reserved stack location in the
168 function prologue. */
169 bool save_toc_in_prologue;
170 /* Offset from virtual_stack_vars_rtx to the start of the ABI_V4
171 varargs save area. */
172 HOST_WIDE_INT varargs_save_offset;
173 /* Temporary stack slot to use for SDmode copies. This slot is
174 64-bits wide and is allocated early enough so that the offset
175 does not overflow the 16-bit load/store offset field. */
176 rtx sdmode_stack_slot;
177 /* Flag if r2 setup is needed with ELFv2 ABI. */
178 bool r2_setup_needed;
179 } machine_function;
181 /* Support targetm.vectorize.builtin_mask_for_load. */
182 static GTY(()) tree altivec_builtin_mask_for_load;
184 /* Set to nonzero once AIX common-mode calls have been defined. */
185 static GTY(()) int common_mode_defined;
187 /* Label number of label created for -mrelocatable, to call to so we can
188 get the address of the GOT section */
189 static int rs6000_pic_labelno;
191 #ifdef USING_ELFOS_H
192 /* Counter for labels which are to be placed in .fixup. */
193 int fixuplabelno = 0;
194 #endif
196 /* Whether to use variant of AIX ABI for PowerPC64 Linux. */
197 int dot_symbols;
199 /* Specify the machine mode that pointers have. After generation of rtl, the
200 compiler makes no further distinction between pointers and any other objects
201 of this machine mode. The type is unsigned since not all things that
202 include rs6000.h also include machmode.h. */
203 unsigned rs6000_pmode;
205 /* Width in bits of a pointer. */
206 unsigned rs6000_pointer_size;
208 #ifdef HAVE_AS_GNU_ATTRIBUTE
209 /* Flag whether floating point values have been passed/returned. */
210 static bool rs6000_passes_float;
211 /* Flag whether vector values have been passed/returned. */
212 static bool rs6000_passes_vector;
213 /* Flag whether small (<= 8 byte) structures have been returned. */
214 static bool rs6000_returns_struct;
215 #endif
217 /* Value is TRUE if register/mode pair is acceptable. */
218 bool rs6000_hard_regno_mode_ok_p[NUM_MACHINE_MODES][FIRST_PSEUDO_REGISTER];
220 /* Maximum number of registers needed for a given register class and mode. */
221 unsigned char rs6000_class_max_nregs[NUM_MACHINE_MODES][LIM_REG_CLASSES];
223 /* How many registers are needed for a given register and mode. */
224 unsigned char rs6000_hard_regno_nregs[NUM_MACHINE_MODES][FIRST_PSEUDO_REGISTER];
226 /* Map register number to register class. */
227 enum reg_class rs6000_regno_regclass[FIRST_PSEUDO_REGISTER];
229 static int dbg_cost_ctrl;
231 /* Built in types. */
232 tree rs6000_builtin_types[RS6000_BTI_MAX];
233 tree rs6000_builtin_decls[RS6000_BUILTIN_COUNT];
235 /* Flag to say the TOC is initialized */
236 int toc_initialized;
237 char toc_label_name[10];
239 /* Cached value of rs6000_variable_issue. This is cached in
240 rs6000_variable_issue hook and returned from rs6000_sched_reorder2. */
241 static short cached_can_issue_more;
243 static GTY(()) section *read_only_data_section;
244 static GTY(()) section *private_data_section;
245 static GTY(()) section *tls_data_section;
246 static GTY(()) section *tls_private_data_section;
247 static GTY(()) section *read_only_private_data_section;
248 static GTY(()) section *sdata2_section;
249 static GTY(()) section *toc_section;
251 struct builtin_description
253 const HOST_WIDE_INT mask;
254 const enum insn_code icode;
255 const char *const name;
256 const enum rs6000_builtins code;
259 /* Describe the vector unit used for modes. */
260 enum rs6000_vector rs6000_vector_unit[NUM_MACHINE_MODES];
261 enum rs6000_vector rs6000_vector_mem[NUM_MACHINE_MODES];
263 /* Register classes for various constraints that are based on the target
264 switches. */
265 enum reg_class rs6000_constraints[RS6000_CONSTRAINT_MAX];
267 /* Describe the alignment of a vector. */
268 int rs6000_vector_align[NUM_MACHINE_MODES];
270 /* Map selected modes to types for builtins. */
271 static GTY(()) tree builtin_mode_to_type[MAX_MACHINE_MODE][2];
273 /* What modes to automatically generate reciprocal divide estimate (fre) and
274 reciprocal sqrt (frsqrte) for. */
275 unsigned char rs6000_recip_bits[MAX_MACHINE_MODE];
277 /* Masks to determine which reciprocal esitmate instructions to generate
278 automatically. */
279 enum rs6000_recip_mask {
280 RECIP_SF_DIV = 0x001, /* Use divide estimate */
281 RECIP_DF_DIV = 0x002,
282 RECIP_V4SF_DIV = 0x004,
283 RECIP_V2DF_DIV = 0x008,
285 RECIP_SF_RSQRT = 0x010, /* Use reciprocal sqrt estimate. */
286 RECIP_DF_RSQRT = 0x020,
287 RECIP_V4SF_RSQRT = 0x040,
288 RECIP_V2DF_RSQRT = 0x080,
290 /* Various combination of flags for -mrecip=xxx. */
291 RECIP_NONE = 0,
292 RECIP_ALL = (RECIP_SF_DIV | RECIP_DF_DIV | RECIP_V4SF_DIV
293 | RECIP_V2DF_DIV | RECIP_SF_RSQRT | RECIP_DF_RSQRT
294 | RECIP_V4SF_RSQRT | RECIP_V2DF_RSQRT),
296 RECIP_HIGH_PRECISION = RECIP_ALL,
298 /* On low precision machines like the power5, don't enable double precision
299 reciprocal square root estimate, since it isn't accurate enough. */
300 RECIP_LOW_PRECISION = (RECIP_ALL & ~(RECIP_DF_RSQRT | RECIP_V2DF_RSQRT))
303 /* -mrecip options. */
304 static struct
306 const char *string; /* option name */
307 unsigned int mask; /* mask bits to set */
308 } recip_options[] = {
309 { "all", RECIP_ALL },
310 { "none", RECIP_NONE },
311 { "div", (RECIP_SF_DIV | RECIP_DF_DIV | RECIP_V4SF_DIV
312 | RECIP_V2DF_DIV) },
313 { "divf", (RECIP_SF_DIV | RECIP_V4SF_DIV) },
314 { "divd", (RECIP_DF_DIV | RECIP_V2DF_DIV) },
315 { "rsqrt", (RECIP_SF_RSQRT | RECIP_DF_RSQRT | RECIP_V4SF_RSQRT
316 | RECIP_V2DF_RSQRT) },
317 { "rsqrtf", (RECIP_SF_RSQRT | RECIP_V4SF_RSQRT) },
318 { "rsqrtd", (RECIP_DF_RSQRT | RECIP_V2DF_RSQRT) },
321 /* Pointer to function (in rs6000-c.c) that can define or undefine target
322 macros that have changed. Languages that don't support the preprocessor
323 don't link in rs6000-c.c, so we can't call it directly. */
324 void (*rs6000_target_modify_macros_ptr) (bool, HOST_WIDE_INT, HOST_WIDE_INT);
326 /* Simplfy register classes into simpler classifications. We assume
327 GPR_REG_TYPE - FPR_REG_TYPE are ordered so that we can use a simple range
328 check for standard register classes (gpr/floating/altivec/vsx) and
329 floating/vector classes (float/altivec/vsx). */
331 enum rs6000_reg_type {
332 NO_REG_TYPE,
333 PSEUDO_REG_TYPE,
334 GPR_REG_TYPE,
335 VSX_REG_TYPE,
336 ALTIVEC_REG_TYPE,
337 FPR_REG_TYPE,
338 SPR_REG_TYPE,
339 CR_REG_TYPE,
340 SPE_ACC_TYPE,
341 SPEFSCR_REG_TYPE
344 /* Map register class to register type. */
345 static enum rs6000_reg_type reg_class_to_reg_type[N_REG_CLASSES];
347 /* First/last register type for the 'normal' register types (i.e. general
348 purpose, floating point, altivec, and VSX registers). */
349 #define IS_STD_REG_TYPE(RTYPE) IN_RANGE(RTYPE, GPR_REG_TYPE, FPR_REG_TYPE)
351 #define IS_FP_VECT_REG_TYPE(RTYPE) IN_RANGE(RTYPE, VSX_REG_TYPE, FPR_REG_TYPE)
354 /* Register classes we care about in secondary reload or go if legitimate
355 address. We only need to worry about GPR, FPR, and Altivec registers here,
356 along an ANY field that is the OR of the 3 register classes. */
358 enum rs6000_reload_reg_type {
359 RELOAD_REG_GPR, /* General purpose registers. */
360 RELOAD_REG_FPR, /* Traditional floating point regs. */
361 RELOAD_REG_VMX, /* Altivec (VMX) registers. */
362 RELOAD_REG_ANY, /* OR of GPR, FPR, Altivec masks. */
363 N_RELOAD_REG
366 /* For setting up register classes, loop through the 3 register classes mapping
367 into real registers, and skip the ANY class, which is just an OR of the
368 bits. */
369 #define FIRST_RELOAD_REG_CLASS RELOAD_REG_GPR
370 #define LAST_RELOAD_REG_CLASS RELOAD_REG_VMX
372 /* Map reload register type to a register in the register class. */
373 struct reload_reg_map_type {
374 const char *name; /* Register class name. */
375 int reg; /* Register in the register class. */
378 static const struct reload_reg_map_type reload_reg_map[N_RELOAD_REG] = {
379 { "Gpr", FIRST_GPR_REGNO }, /* RELOAD_REG_GPR. */
380 { "Fpr", FIRST_FPR_REGNO }, /* RELOAD_REG_FPR. */
381 { "VMX", FIRST_ALTIVEC_REGNO }, /* RELOAD_REG_VMX. */
382 { "Any", -1 }, /* RELOAD_REG_ANY. */
385 /* Mask bits for each register class, indexed per mode. Historically the
386 compiler has been more restrictive which types can do PRE_MODIFY instead of
387 PRE_INC and PRE_DEC, so keep track of sepaate bits for these two. */
388 typedef unsigned char addr_mask_type;
390 #define RELOAD_REG_VALID 0x01 /* Mode valid in register.. */
391 #define RELOAD_REG_MULTIPLE 0x02 /* Mode takes multiple registers. */
392 #define RELOAD_REG_INDEXED 0x04 /* Reg+reg addressing. */
393 #define RELOAD_REG_OFFSET 0x08 /* Reg+offset addressing. */
394 #define RELOAD_REG_PRE_INCDEC 0x10 /* PRE_INC/PRE_DEC valid. */
395 #define RELOAD_REG_PRE_MODIFY 0x20 /* PRE_MODIFY valid. */
397 /* Register type masks based on the type, of valid addressing modes. */
398 struct rs6000_reg_addr {
399 enum insn_code reload_load; /* INSN to reload for loading. */
400 enum insn_code reload_store; /* INSN to reload for storing. */
401 enum insn_code reload_fpr_gpr; /* INSN to move from FPR to GPR. */
402 enum insn_code reload_gpr_vsx; /* INSN to move from GPR to VSX. */
403 enum insn_code reload_vsx_gpr; /* INSN to move from VSX to GPR. */
404 addr_mask_type addr_mask[(int)N_RELOAD_REG]; /* Valid address masks. */
405 bool scalar_in_vmx_p; /* Scalar value can go in VMX. */
408 static struct rs6000_reg_addr reg_addr[NUM_MACHINE_MODES];
410 /* Helper function to say whether a mode supports PRE_INC or PRE_DEC. */
411 static inline bool
412 mode_supports_pre_incdec_p (enum machine_mode mode)
414 return ((reg_addr[mode].addr_mask[RELOAD_REG_ANY] & RELOAD_REG_PRE_INCDEC)
415 != 0);
418 /* Helper function to say whether a mode supports PRE_MODIFY. */
419 static inline bool
420 mode_supports_pre_modify_p (enum machine_mode mode)
422 return ((reg_addr[mode].addr_mask[RELOAD_REG_ANY] & RELOAD_REG_PRE_MODIFY)
423 != 0);
427 /* Target cpu costs. */
429 struct processor_costs {
430 const int mulsi; /* cost of SImode multiplication. */
431 const int mulsi_const; /* cost of SImode multiplication by constant. */
432 const int mulsi_const9; /* cost of SImode mult by short constant. */
433 const int muldi; /* cost of DImode multiplication. */
434 const int divsi; /* cost of SImode division. */
435 const int divdi; /* cost of DImode division. */
436 const int fp; /* cost of simple SFmode and DFmode insns. */
437 const int dmul; /* cost of DFmode multiplication (and fmadd). */
438 const int sdiv; /* cost of SFmode division (fdivs). */
439 const int ddiv; /* cost of DFmode division (fdiv). */
440 const int cache_line_size; /* cache line size in bytes. */
441 const int l1_cache_size; /* size of l1 cache, in kilobytes. */
442 const int l2_cache_size; /* size of l2 cache, in kilobytes. */
443 const int simultaneous_prefetches; /* number of parallel prefetch
444 operations. */
447 const struct processor_costs *rs6000_cost;
449 /* Processor costs (relative to an add) */
451 /* Instruction size costs on 32bit processors. */
452 static const
453 struct processor_costs size32_cost = {
454 COSTS_N_INSNS (1), /* mulsi */
455 COSTS_N_INSNS (1), /* mulsi_const */
456 COSTS_N_INSNS (1), /* mulsi_const9 */
457 COSTS_N_INSNS (1), /* muldi */
458 COSTS_N_INSNS (1), /* divsi */
459 COSTS_N_INSNS (1), /* divdi */
460 COSTS_N_INSNS (1), /* fp */
461 COSTS_N_INSNS (1), /* dmul */
462 COSTS_N_INSNS (1), /* sdiv */
463 COSTS_N_INSNS (1), /* ddiv */
470 /* Instruction size costs on 64bit processors. */
471 static const
472 struct processor_costs size64_cost = {
473 COSTS_N_INSNS (1), /* mulsi */
474 COSTS_N_INSNS (1), /* mulsi_const */
475 COSTS_N_INSNS (1), /* mulsi_const9 */
476 COSTS_N_INSNS (1), /* muldi */
477 COSTS_N_INSNS (1), /* divsi */
478 COSTS_N_INSNS (1), /* divdi */
479 COSTS_N_INSNS (1), /* fp */
480 COSTS_N_INSNS (1), /* dmul */
481 COSTS_N_INSNS (1), /* sdiv */
482 COSTS_N_INSNS (1), /* ddiv */
483 128,
489 /* Instruction costs on RS64A processors. */
490 static const
491 struct processor_costs rs64a_cost = {
492 COSTS_N_INSNS (20), /* mulsi */
493 COSTS_N_INSNS (12), /* mulsi_const */
494 COSTS_N_INSNS (8), /* mulsi_const9 */
495 COSTS_N_INSNS (34), /* muldi */
496 COSTS_N_INSNS (65), /* divsi */
497 COSTS_N_INSNS (67), /* divdi */
498 COSTS_N_INSNS (4), /* fp */
499 COSTS_N_INSNS (4), /* dmul */
500 COSTS_N_INSNS (31), /* sdiv */
501 COSTS_N_INSNS (31), /* ddiv */
502 128, /* cache line size */
503 128, /* l1 cache */
504 2048, /* l2 cache */
505 1, /* streams */
508 /* Instruction costs on MPCCORE processors. */
509 static const
510 struct processor_costs mpccore_cost = {
511 COSTS_N_INSNS (2), /* mulsi */
512 COSTS_N_INSNS (2), /* mulsi_const */
513 COSTS_N_INSNS (2), /* mulsi_const9 */
514 COSTS_N_INSNS (2), /* muldi */
515 COSTS_N_INSNS (6), /* divsi */
516 COSTS_N_INSNS (6), /* divdi */
517 COSTS_N_INSNS (4), /* fp */
518 COSTS_N_INSNS (5), /* dmul */
519 COSTS_N_INSNS (10), /* sdiv */
520 COSTS_N_INSNS (17), /* ddiv */
521 32, /* cache line size */
522 4, /* l1 cache */
523 16, /* l2 cache */
524 1, /* streams */
527 /* Instruction costs on PPC403 processors. */
528 static const
529 struct processor_costs ppc403_cost = {
530 COSTS_N_INSNS (4), /* mulsi */
531 COSTS_N_INSNS (4), /* mulsi_const */
532 COSTS_N_INSNS (4), /* mulsi_const9 */
533 COSTS_N_INSNS (4), /* muldi */
534 COSTS_N_INSNS (33), /* divsi */
535 COSTS_N_INSNS (33), /* divdi */
536 COSTS_N_INSNS (11), /* fp */
537 COSTS_N_INSNS (11), /* dmul */
538 COSTS_N_INSNS (11), /* sdiv */
539 COSTS_N_INSNS (11), /* ddiv */
540 32, /* cache line size */
541 4, /* l1 cache */
542 16, /* l2 cache */
543 1, /* streams */
546 /* Instruction costs on PPC405 processors. */
547 static const
548 struct processor_costs ppc405_cost = {
549 COSTS_N_INSNS (5), /* mulsi */
550 COSTS_N_INSNS (4), /* mulsi_const */
551 COSTS_N_INSNS (3), /* mulsi_const9 */
552 COSTS_N_INSNS (5), /* muldi */
553 COSTS_N_INSNS (35), /* divsi */
554 COSTS_N_INSNS (35), /* divdi */
555 COSTS_N_INSNS (11), /* fp */
556 COSTS_N_INSNS (11), /* dmul */
557 COSTS_N_INSNS (11), /* sdiv */
558 COSTS_N_INSNS (11), /* ddiv */
559 32, /* cache line size */
560 16, /* l1 cache */
561 128, /* l2 cache */
562 1, /* streams */
565 /* Instruction costs on PPC440 processors. */
566 static const
567 struct processor_costs ppc440_cost = {
568 COSTS_N_INSNS (3), /* mulsi */
569 COSTS_N_INSNS (2), /* mulsi_const */
570 COSTS_N_INSNS (2), /* mulsi_const9 */
571 COSTS_N_INSNS (3), /* muldi */
572 COSTS_N_INSNS (34), /* divsi */
573 COSTS_N_INSNS (34), /* divdi */
574 COSTS_N_INSNS (5), /* fp */
575 COSTS_N_INSNS (5), /* dmul */
576 COSTS_N_INSNS (19), /* sdiv */
577 COSTS_N_INSNS (33), /* ddiv */
578 32, /* cache line size */
579 32, /* l1 cache */
580 256, /* l2 cache */
581 1, /* streams */
584 /* Instruction costs on PPC476 processors. */
585 static const
586 struct processor_costs ppc476_cost = {
587 COSTS_N_INSNS (4), /* mulsi */
588 COSTS_N_INSNS (4), /* mulsi_const */
589 COSTS_N_INSNS (4), /* mulsi_const9 */
590 COSTS_N_INSNS (4), /* muldi */
591 COSTS_N_INSNS (11), /* divsi */
592 COSTS_N_INSNS (11), /* divdi */
593 COSTS_N_INSNS (6), /* fp */
594 COSTS_N_INSNS (6), /* dmul */
595 COSTS_N_INSNS (19), /* sdiv */
596 COSTS_N_INSNS (33), /* ddiv */
597 32, /* l1 cache line size */
598 32, /* l1 cache */
599 512, /* l2 cache */
600 1, /* streams */
603 /* Instruction costs on PPC601 processors. */
604 static const
605 struct processor_costs ppc601_cost = {
606 COSTS_N_INSNS (5), /* mulsi */
607 COSTS_N_INSNS (5), /* mulsi_const */
608 COSTS_N_INSNS (5), /* mulsi_const9 */
609 COSTS_N_INSNS (5), /* muldi */
610 COSTS_N_INSNS (36), /* divsi */
611 COSTS_N_INSNS (36), /* divdi */
612 COSTS_N_INSNS (4), /* fp */
613 COSTS_N_INSNS (5), /* dmul */
614 COSTS_N_INSNS (17), /* sdiv */
615 COSTS_N_INSNS (31), /* ddiv */
616 32, /* cache line size */
617 32, /* l1 cache */
618 256, /* l2 cache */
619 1, /* streams */
622 /* Instruction costs on PPC603 processors. */
623 static const
624 struct processor_costs ppc603_cost = {
625 COSTS_N_INSNS (5), /* mulsi */
626 COSTS_N_INSNS (3), /* mulsi_const */
627 COSTS_N_INSNS (2), /* mulsi_const9 */
628 COSTS_N_INSNS (5), /* muldi */
629 COSTS_N_INSNS (37), /* divsi */
630 COSTS_N_INSNS (37), /* divdi */
631 COSTS_N_INSNS (3), /* fp */
632 COSTS_N_INSNS (4), /* dmul */
633 COSTS_N_INSNS (18), /* sdiv */
634 COSTS_N_INSNS (33), /* ddiv */
635 32, /* cache line size */
636 8, /* l1 cache */
637 64, /* l2 cache */
638 1, /* streams */
641 /* Instruction costs on PPC604 processors. */
642 static const
643 struct processor_costs ppc604_cost = {
644 COSTS_N_INSNS (4), /* mulsi */
645 COSTS_N_INSNS (4), /* mulsi_const */
646 COSTS_N_INSNS (4), /* mulsi_const9 */
647 COSTS_N_INSNS (4), /* muldi */
648 COSTS_N_INSNS (20), /* divsi */
649 COSTS_N_INSNS (20), /* divdi */
650 COSTS_N_INSNS (3), /* fp */
651 COSTS_N_INSNS (3), /* dmul */
652 COSTS_N_INSNS (18), /* sdiv */
653 COSTS_N_INSNS (32), /* ddiv */
654 32, /* cache line size */
655 16, /* l1 cache */
656 512, /* l2 cache */
657 1, /* streams */
660 /* Instruction costs on PPC604e processors. */
661 static const
662 struct processor_costs ppc604e_cost = {
663 COSTS_N_INSNS (2), /* mulsi */
664 COSTS_N_INSNS (2), /* mulsi_const */
665 COSTS_N_INSNS (2), /* mulsi_const9 */
666 COSTS_N_INSNS (2), /* muldi */
667 COSTS_N_INSNS (20), /* divsi */
668 COSTS_N_INSNS (20), /* divdi */
669 COSTS_N_INSNS (3), /* fp */
670 COSTS_N_INSNS (3), /* dmul */
671 COSTS_N_INSNS (18), /* sdiv */
672 COSTS_N_INSNS (32), /* ddiv */
673 32, /* cache line size */
674 32, /* l1 cache */
675 1024, /* l2 cache */
676 1, /* streams */
679 /* Instruction costs on PPC620 processors. */
680 static const
681 struct processor_costs ppc620_cost = {
682 COSTS_N_INSNS (5), /* mulsi */
683 COSTS_N_INSNS (4), /* mulsi_const */
684 COSTS_N_INSNS (3), /* mulsi_const9 */
685 COSTS_N_INSNS (7), /* muldi */
686 COSTS_N_INSNS (21), /* divsi */
687 COSTS_N_INSNS (37), /* divdi */
688 COSTS_N_INSNS (3), /* fp */
689 COSTS_N_INSNS (3), /* dmul */
690 COSTS_N_INSNS (18), /* sdiv */
691 COSTS_N_INSNS (32), /* ddiv */
692 128, /* cache line size */
693 32, /* l1 cache */
694 1024, /* l2 cache */
695 1, /* streams */
698 /* Instruction costs on PPC630 processors. */
699 static const
700 struct processor_costs ppc630_cost = {
701 COSTS_N_INSNS (5), /* mulsi */
702 COSTS_N_INSNS (4), /* mulsi_const */
703 COSTS_N_INSNS (3), /* mulsi_const9 */
704 COSTS_N_INSNS (7), /* muldi */
705 COSTS_N_INSNS (21), /* divsi */
706 COSTS_N_INSNS (37), /* divdi */
707 COSTS_N_INSNS (3), /* fp */
708 COSTS_N_INSNS (3), /* dmul */
709 COSTS_N_INSNS (17), /* sdiv */
710 COSTS_N_INSNS (21), /* ddiv */
711 128, /* cache line size */
712 64, /* l1 cache */
713 1024, /* l2 cache */
714 1, /* streams */
717 /* Instruction costs on Cell processor. */
718 /* COSTS_N_INSNS (1) ~ one add. */
719 static const
720 struct processor_costs ppccell_cost = {
721 COSTS_N_INSNS (9/2)+2, /* mulsi */
722 COSTS_N_INSNS (6/2), /* mulsi_const */
723 COSTS_N_INSNS (6/2), /* mulsi_const9 */
724 COSTS_N_INSNS (15/2)+2, /* muldi */
725 COSTS_N_INSNS (38/2), /* divsi */
726 COSTS_N_INSNS (70/2), /* divdi */
727 COSTS_N_INSNS (10/2), /* fp */
728 COSTS_N_INSNS (10/2), /* dmul */
729 COSTS_N_INSNS (74/2), /* sdiv */
730 COSTS_N_INSNS (74/2), /* ddiv */
731 128, /* cache line size */
732 32, /* l1 cache */
733 512, /* l2 cache */
734 6, /* streams */
737 /* Instruction costs on PPC750 and PPC7400 processors. */
738 static const
739 struct processor_costs ppc750_cost = {
740 COSTS_N_INSNS (5), /* mulsi */
741 COSTS_N_INSNS (3), /* mulsi_const */
742 COSTS_N_INSNS (2), /* mulsi_const9 */
743 COSTS_N_INSNS (5), /* muldi */
744 COSTS_N_INSNS (17), /* divsi */
745 COSTS_N_INSNS (17), /* divdi */
746 COSTS_N_INSNS (3), /* fp */
747 COSTS_N_INSNS (3), /* dmul */
748 COSTS_N_INSNS (17), /* sdiv */
749 COSTS_N_INSNS (31), /* ddiv */
750 32, /* cache line size */
751 32, /* l1 cache */
752 512, /* l2 cache */
753 1, /* streams */
756 /* Instruction costs on PPC7450 processors. */
757 static const
758 struct processor_costs ppc7450_cost = {
759 COSTS_N_INSNS (4), /* mulsi */
760 COSTS_N_INSNS (3), /* mulsi_const */
761 COSTS_N_INSNS (3), /* mulsi_const9 */
762 COSTS_N_INSNS (4), /* muldi */
763 COSTS_N_INSNS (23), /* divsi */
764 COSTS_N_INSNS (23), /* divdi */
765 COSTS_N_INSNS (5), /* fp */
766 COSTS_N_INSNS (5), /* dmul */
767 COSTS_N_INSNS (21), /* sdiv */
768 COSTS_N_INSNS (35), /* ddiv */
769 32, /* cache line size */
770 32, /* l1 cache */
771 1024, /* l2 cache */
772 1, /* streams */
775 /* Instruction costs on PPC8540 processors. */
776 static const
777 struct processor_costs ppc8540_cost = {
778 COSTS_N_INSNS (4), /* mulsi */
779 COSTS_N_INSNS (4), /* mulsi_const */
780 COSTS_N_INSNS (4), /* mulsi_const9 */
781 COSTS_N_INSNS (4), /* muldi */
782 COSTS_N_INSNS (19), /* divsi */
783 COSTS_N_INSNS (19), /* divdi */
784 COSTS_N_INSNS (4), /* fp */
785 COSTS_N_INSNS (4), /* dmul */
786 COSTS_N_INSNS (29), /* sdiv */
787 COSTS_N_INSNS (29), /* ddiv */
788 32, /* cache line size */
789 32, /* l1 cache */
790 256, /* l2 cache */
791 1, /* prefetch streams /*/
794 /* Instruction costs on E300C2 and E300C3 cores. */
795 static const
796 struct processor_costs ppce300c2c3_cost = {
797 COSTS_N_INSNS (4), /* mulsi */
798 COSTS_N_INSNS (4), /* mulsi_const */
799 COSTS_N_INSNS (4), /* mulsi_const9 */
800 COSTS_N_INSNS (4), /* muldi */
801 COSTS_N_INSNS (19), /* divsi */
802 COSTS_N_INSNS (19), /* divdi */
803 COSTS_N_INSNS (3), /* fp */
804 COSTS_N_INSNS (4), /* dmul */
805 COSTS_N_INSNS (18), /* sdiv */
806 COSTS_N_INSNS (33), /* ddiv */
808 16, /* l1 cache */
809 16, /* l2 cache */
810 1, /* prefetch streams /*/
813 /* Instruction costs on PPCE500MC processors. */
814 static const
815 struct processor_costs ppce500mc_cost = {
816 COSTS_N_INSNS (4), /* mulsi */
817 COSTS_N_INSNS (4), /* mulsi_const */
818 COSTS_N_INSNS (4), /* mulsi_const9 */
819 COSTS_N_INSNS (4), /* muldi */
820 COSTS_N_INSNS (14), /* divsi */
821 COSTS_N_INSNS (14), /* divdi */
822 COSTS_N_INSNS (8), /* fp */
823 COSTS_N_INSNS (10), /* dmul */
824 COSTS_N_INSNS (36), /* sdiv */
825 COSTS_N_INSNS (66), /* ddiv */
826 64, /* cache line size */
827 32, /* l1 cache */
828 128, /* l2 cache */
829 1, /* prefetch streams /*/
832 /* Instruction costs on PPCE500MC64 processors. */
833 static const
834 struct processor_costs ppce500mc64_cost = {
835 COSTS_N_INSNS (4), /* mulsi */
836 COSTS_N_INSNS (4), /* mulsi_const */
837 COSTS_N_INSNS (4), /* mulsi_const9 */
838 COSTS_N_INSNS (4), /* muldi */
839 COSTS_N_INSNS (14), /* divsi */
840 COSTS_N_INSNS (14), /* divdi */
841 COSTS_N_INSNS (4), /* fp */
842 COSTS_N_INSNS (10), /* dmul */
843 COSTS_N_INSNS (36), /* sdiv */
844 COSTS_N_INSNS (66), /* ddiv */
845 64, /* cache line size */
846 32, /* l1 cache */
847 128, /* l2 cache */
848 1, /* prefetch streams /*/
851 /* Instruction costs on PPCE5500 processors. */
852 static const
853 struct processor_costs ppce5500_cost = {
854 COSTS_N_INSNS (5), /* mulsi */
855 COSTS_N_INSNS (5), /* mulsi_const */
856 COSTS_N_INSNS (4), /* mulsi_const9 */
857 COSTS_N_INSNS (5), /* muldi */
858 COSTS_N_INSNS (14), /* divsi */
859 COSTS_N_INSNS (14), /* divdi */
860 COSTS_N_INSNS (7), /* fp */
861 COSTS_N_INSNS (10), /* dmul */
862 COSTS_N_INSNS (36), /* sdiv */
863 COSTS_N_INSNS (66), /* ddiv */
864 64, /* cache line size */
865 32, /* l1 cache */
866 128, /* l2 cache */
867 1, /* prefetch streams /*/
870 /* Instruction costs on PPCE6500 processors. */
871 static const
872 struct processor_costs ppce6500_cost = {
873 COSTS_N_INSNS (5), /* mulsi */
874 COSTS_N_INSNS (5), /* mulsi_const */
875 COSTS_N_INSNS (4), /* mulsi_const9 */
876 COSTS_N_INSNS (5), /* muldi */
877 COSTS_N_INSNS (14), /* divsi */
878 COSTS_N_INSNS (14), /* divdi */
879 COSTS_N_INSNS (7), /* fp */
880 COSTS_N_INSNS (10), /* dmul */
881 COSTS_N_INSNS (36), /* sdiv */
882 COSTS_N_INSNS (66), /* ddiv */
883 64, /* cache line size */
884 32, /* l1 cache */
885 128, /* l2 cache */
886 1, /* prefetch streams /*/
889 /* Instruction costs on AppliedMicro Titan processors. */
890 static const
891 struct processor_costs titan_cost = {
892 COSTS_N_INSNS (5), /* mulsi */
893 COSTS_N_INSNS (5), /* mulsi_const */
894 COSTS_N_INSNS (5), /* mulsi_const9 */
895 COSTS_N_INSNS (5), /* muldi */
896 COSTS_N_INSNS (18), /* divsi */
897 COSTS_N_INSNS (18), /* divdi */
898 COSTS_N_INSNS (10), /* fp */
899 COSTS_N_INSNS (10), /* dmul */
900 COSTS_N_INSNS (46), /* sdiv */
901 COSTS_N_INSNS (72), /* ddiv */
902 32, /* cache line size */
903 32, /* l1 cache */
904 512, /* l2 cache */
905 1, /* prefetch streams /*/
908 /* Instruction costs on POWER4 and POWER5 processors. */
909 static const
910 struct processor_costs power4_cost = {
911 COSTS_N_INSNS (3), /* mulsi */
912 COSTS_N_INSNS (2), /* mulsi_const */
913 COSTS_N_INSNS (2), /* mulsi_const9 */
914 COSTS_N_INSNS (4), /* muldi */
915 COSTS_N_INSNS (18), /* divsi */
916 COSTS_N_INSNS (34), /* divdi */
917 COSTS_N_INSNS (3), /* fp */
918 COSTS_N_INSNS (3), /* dmul */
919 COSTS_N_INSNS (17), /* sdiv */
920 COSTS_N_INSNS (17), /* ddiv */
921 128, /* cache line size */
922 32, /* l1 cache */
923 1024, /* l2 cache */
924 8, /* prefetch streams /*/
927 /* Instruction costs on POWER6 processors. */
928 static const
929 struct processor_costs power6_cost = {
930 COSTS_N_INSNS (8), /* mulsi */
931 COSTS_N_INSNS (8), /* mulsi_const */
932 COSTS_N_INSNS (8), /* mulsi_const9 */
933 COSTS_N_INSNS (8), /* muldi */
934 COSTS_N_INSNS (22), /* divsi */
935 COSTS_N_INSNS (28), /* divdi */
936 COSTS_N_INSNS (3), /* fp */
937 COSTS_N_INSNS (3), /* dmul */
938 COSTS_N_INSNS (13), /* sdiv */
939 COSTS_N_INSNS (16), /* ddiv */
940 128, /* cache line size */
941 64, /* l1 cache */
942 2048, /* l2 cache */
943 16, /* prefetch streams */
946 /* Instruction costs on POWER7 processors. */
947 static const
948 struct processor_costs power7_cost = {
949 COSTS_N_INSNS (2), /* mulsi */
950 COSTS_N_INSNS (2), /* mulsi_const */
951 COSTS_N_INSNS (2), /* mulsi_const9 */
952 COSTS_N_INSNS (2), /* muldi */
953 COSTS_N_INSNS (18), /* divsi */
954 COSTS_N_INSNS (34), /* divdi */
955 COSTS_N_INSNS (3), /* fp */
956 COSTS_N_INSNS (3), /* dmul */
957 COSTS_N_INSNS (13), /* sdiv */
958 COSTS_N_INSNS (16), /* ddiv */
959 128, /* cache line size */
960 32, /* l1 cache */
961 256, /* l2 cache */
962 12, /* prefetch streams */
965 /* Instruction costs on POWER8 processors. */
966 static const
967 struct processor_costs power8_cost = {
968 COSTS_N_INSNS (3), /* mulsi */
969 COSTS_N_INSNS (3), /* mulsi_const */
970 COSTS_N_INSNS (3), /* mulsi_const9 */
971 COSTS_N_INSNS (3), /* muldi */
972 COSTS_N_INSNS (19), /* divsi */
973 COSTS_N_INSNS (35), /* divdi */
974 COSTS_N_INSNS (3), /* fp */
975 COSTS_N_INSNS (3), /* dmul */
976 COSTS_N_INSNS (14), /* sdiv */
977 COSTS_N_INSNS (17), /* ddiv */
978 128, /* cache line size */
979 32, /* l1 cache */
980 256, /* l2 cache */
981 12, /* prefetch streams */
984 /* Instruction costs on POWER A2 processors. */
985 static const
986 struct processor_costs ppca2_cost = {
987 COSTS_N_INSNS (16), /* mulsi */
988 COSTS_N_INSNS (16), /* mulsi_const */
989 COSTS_N_INSNS (16), /* mulsi_const9 */
990 COSTS_N_INSNS (16), /* muldi */
991 COSTS_N_INSNS (22), /* divsi */
992 COSTS_N_INSNS (28), /* divdi */
993 COSTS_N_INSNS (3), /* fp */
994 COSTS_N_INSNS (3), /* dmul */
995 COSTS_N_INSNS (59), /* sdiv */
996 COSTS_N_INSNS (72), /* ddiv */
998 16, /* l1 cache */
999 2048, /* l2 cache */
1000 16, /* prefetch streams */
1004 /* Table that classifies rs6000 builtin functions (pure, const, etc.). */
1005 #undef RS6000_BUILTIN_1
1006 #undef RS6000_BUILTIN_2
1007 #undef RS6000_BUILTIN_3
1008 #undef RS6000_BUILTIN_A
1009 #undef RS6000_BUILTIN_D
1010 #undef RS6000_BUILTIN_E
1011 #undef RS6000_BUILTIN_H
1012 #undef RS6000_BUILTIN_P
1013 #undef RS6000_BUILTIN_Q
1014 #undef RS6000_BUILTIN_S
1015 #undef RS6000_BUILTIN_X
1017 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE) \
1018 { NAME, ICODE, MASK, ATTR },
1020 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE) \
1021 { NAME, ICODE, MASK, ATTR },
1023 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE) \
1024 { NAME, ICODE, MASK, ATTR },
1026 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE) \
1027 { NAME, ICODE, MASK, ATTR },
1029 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE) \
1030 { NAME, ICODE, MASK, ATTR },
1032 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE) \
1033 { NAME, ICODE, MASK, ATTR },
1035 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE) \
1036 { NAME, ICODE, MASK, ATTR },
1038 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE) \
1039 { NAME, ICODE, MASK, ATTR },
1041 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE) \
1042 { NAME, ICODE, MASK, ATTR },
1044 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE) \
1045 { NAME, ICODE, MASK, ATTR },
1047 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE) \
1048 { NAME, ICODE, MASK, ATTR },
1050 struct rs6000_builtin_info_type {
1051 const char *name;
1052 const enum insn_code icode;
1053 const HOST_WIDE_INT mask;
1054 const unsigned attr;
1057 static const struct rs6000_builtin_info_type rs6000_builtin_info[] =
1059 #include "rs6000-builtin.def"
1062 #undef RS6000_BUILTIN_1
1063 #undef RS6000_BUILTIN_2
1064 #undef RS6000_BUILTIN_3
1065 #undef RS6000_BUILTIN_A
1066 #undef RS6000_BUILTIN_D
1067 #undef RS6000_BUILTIN_E
1068 #undef RS6000_BUILTIN_H
1069 #undef RS6000_BUILTIN_P
1070 #undef RS6000_BUILTIN_Q
1071 #undef RS6000_BUILTIN_S
1072 #undef RS6000_BUILTIN_X
1074 /* Support for -mveclibabi=<xxx> to control which vector library to use. */
1075 static tree (*rs6000_veclib_handler) (tree, tree, tree);
1078 static bool rs6000_debug_legitimate_address_p (enum machine_mode, rtx, bool);
1079 static bool spe_func_has_64bit_regs_p (void);
1080 static struct machine_function * rs6000_init_machine_status (void);
1081 static int rs6000_ra_ever_killed (void);
1082 static tree rs6000_handle_longcall_attribute (tree *, tree, tree, int, bool *);
1083 static tree rs6000_handle_altivec_attribute (tree *, tree, tree, int, bool *);
1084 static tree rs6000_handle_struct_attribute (tree *, tree, tree, int, bool *);
1085 static tree rs6000_builtin_vectorized_libmass (tree, tree, tree);
1086 static void rs6000_emit_set_long_const (rtx, HOST_WIDE_INT);
1087 static int rs6000_memory_move_cost (enum machine_mode, reg_class_t, bool);
1088 static bool rs6000_debug_rtx_costs (rtx, int, int, int, int *, bool);
1089 static int rs6000_debug_address_cost (rtx, enum machine_mode, addr_space_t,
1090 bool);
1091 static int rs6000_debug_adjust_cost (rtx_insn *, rtx, rtx_insn *, int);
1092 static bool is_microcoded_insn (rtx_insn *);
1093 static bool is_nonpipeline_insn (rtx_insn *);
1094 static bool is_cracked_insn (rtx_insn *);
1095 static bool is_load_insn (rtx, rtx *);
1096 static bool is_store_insn (rtx, rtx *);
1097 static bool set_to_load_agen (rtx_insn *,rtx_insn *);
1098 static bool insn_terminates_group_p (rtx_insn *, enum group_termination);
1099 static bool insn_must_be_first_in_group (rtx_insn *);
1100 static bool insn_must_be_last_in_group (rtx_insn *);
1101 static void altivec_init_builtins (void);
1102 static tree builtin_function_type (enum machine_mode, enum machine_mode,
1103 enum machine_mode, enum machine_mode,
1104 enum rs6000_builtins, const char *name);
1105 static void rs6000_common_init_builtins (void);
1106 static void paired_init_builtins (void);
1107 static rtx paired_expand_predicate_builtin (enum insn_code, tree, rtx);
1108 static void spe_init_builtins (void);
1109 static void htm_init_builtins (void);
1110 static rtx spe_expand_predicate_builtin (enum insn_code, tree, rtx);
1111 static rtx spe_expand_evsel_builtin (enum insn_code, tree, rtx);
1112 static int rs6000_emit_int_cmove (rtx, rtx, rtx, rtx);
1113 static rs6000_stack_t *rs6000_stack_info (void);
1114 static void is_altivec_return_reg (rtx, void *);
1115 int easy_vector_constant (rtx, enum machine_mode);
1116 static rtx rs6000_debug_legitimize_address (rtx, rtx, enum machine_mode);
1117 static rtx rs6000_legitimize_tls_address (rtx, enum tls_model);
1118 static rtx rs6000_darwin64_record_arg (CUMULATIVE_ARGS *, const_tree,
1119 bool, bool);
1120 #if TARGET_MACHO
1121 static void macho_branch_islands (void);
1122 #endif
1123 static rtx rs6000_legitimize_reload_address (rtx, enum machine_mode, int, int,
1124 int, int *);
1125 static rtx rs6000_debug_legitimize_reload_address (rtx, enum machine_mode, int,
1126 int, int, int *);
1127 static bool rs6000_mode_dependent_address (const_rtx);
1128 static bool rs6000_debug_mode_dependent_address (const_rtx);
1129 static enum reg_class rs6000_secondary_reload_class (enum reg_class,
1130 enum machine_mode, rtx);
1131 static enum reg_class rs6000_debug_secondary_reload_class (enum reg_class,
1132 enum machine_mode,
1133 rtx);
1134 static enum reg_class rs6000_preferred_reload_class (rtx, enum reg_class);
1135 static enum reg_class rs6000_debug_preferred_reload_class (rtx,
1136 enum reg_class);
1137 static bool rs6000_secondary_memory_needed (enum reg_class, enum reg_class,
1138 enum machine_mode);
1139 static bool rs6000_debug_secondary_memory_needed (enum reg_class,
1140 enum reg_class,
1141 enum machine_mode);
1142 static bool rs6000_cannot_change_mode_class (enum machine_mode,
1143 enum machine_mode,
1144 enum reg_class);
1145 static bool rs6000_debug_cannot_change_mode_class (enum machine_mode,
1146 enum machine_mode,
1147 enum reg_class);
1148 static bool rs6000_save_toc_in_prologue_p (void);
1150 rtx (*rs6000_legitimize_reload_address_ptr) (rtx, enum machine_mode, int, int,
1151 int, int *)
1152 = rs6000_legitimize_reload_address;
1154 static bool (*rs6000_mode_dependent_address_ptr) (const_rtx)
1155 = rs6000_mode_dependent_address;
1157 enum reg_class (*rs6000_secondary_reload_class_ptr) (enum reg_class,
1158 enum machine_mode, rtx)
1159 = rs6000_secondary_reload_class;
1161 enum reg_class (*rs6000_preferred_reload_class_ptr) (rtx, enum reg_class)
1162 = rs6000_preferred_reload_class;
1164 bool (*rs6000_secondary_memory_needed_ptr) (enum reg_class, enum reg_class,
1165 enum machine_mode)
1166 = rs6000_secondary_memory_needed;
1168 bool (*rs6000_cannot_change_mode_class_ptr) (enum machine_mode,
1169 enum machine_mode,
1170 enum reg_class)
1171 = rs6000_cannot_change_mode_class;
1173 const int INSN_NOT_AVAILABLE = -1;
1175 static void rs6000_print_isa_options (FILE *, int, const char *,
1176 HOST_WIDE_INT);
1177 static void rs6000_print_builtin_options (FILE *, int, const char *,
1178 HOST_WIDE_INT);
1180 static enum rs6000_reg_type register_to_reg_type (rtx, bool *);
1181 static bool rs6000_secondary_reload_move (enum rs6000_reg_type,
1182 enum rs6000_reg_type,
1183 enum machine_mode,
1184 secondary_reload_info *,
1185 bool);
1186 rtl_opt_pass *make_pass_analyze_swaps (gcc::context*);
1188 /* Hash table stuff for keeping track of TOC entries. */
1190 struct GTY((for_user)) toc_hash_struct
1192 /* `key' will satisfy CONSTANT_P; in fact, it will satisfy
1193 ASM_OUTPUT_SPECIAL_POOL_ENTRY_P. */
1194 rtx key;
1195 enum machine_mode key_mode;
1196 int labelno;
1199 struct toc_hasher : ggc_hasher<toc_hash_struct *>
1201 static hashval_t hash (toc_hash_struct *);
1202 static bool equal (toc_hash_struct *, toc_hash_struct *);
1205 static GTY (()) hash_table<toc_hasher> *toc_hash_table;
1207 /* Hash table to keep track of the argument types for builtin functions. */
1209 struct GTY((for_user)) builtin_hash_struct
1211 tree type;
1212 enum machine_mode mode[4]; /* return value + 3 arguments. */
1213 unsigned char uns_p[4]; /* and whether the types are unsigned. */
1216 struct builtin_hasher : ggc_hasher<builtin_hash_struct *>
1218 static hashval_t hash (builtin_hash_struct *);
1219 static bool equal (builtin_hash_struct *, builtin_hash_struct *);
1222 static GTY (()) hash_table<builtin_hasher> *builtin_hash_table;
1225 /* Default register names. */
1226 char rs6000_reg_names[][8] =
1228 "0", "1", "2", "3", "4", "5", "6", "7",
1229 "8", "9", "10", "11", "12", "13", "14", "15",
1230 "16", "17", "18", "19", "20", "21", "22", "23",
1231 "24", "25", "26", "27", "28", "29", "30", "31",
1232 "0", "1", "2", "3", "4", "5", "6", "7",
1233 "8", "9", "10", "11", "12", "13", "14", "15",
1234 "16", "17", "18", "19", "20", "21", "22", "23",
1235 "24", "25", "26", "27", "28", "29", "30", "31",
1236 "mq", "lr", "ctr","ap",
1237 "0", "1", "2", "3", "4", "5", "6", "7",
1238 "ca",
1239 /* AltiVec registers. */
1240 "0", "1", "2", "3", "4", "5", "6", "7",
1241 "8", "9", "10", "11", "12", "13", "14", "15",
1242 "16", "17", "18", "19", "20", "21", "22", "23",
1243 "24", "25", "26", "27", "28", "29", "30", "31",
1244 "vrsave", "vscr",
1245 /* SPE registers. */
1246 "spe_acc", "spefscr",
1247 /* Soft frame pointer. */
1248 "sfp",
1249 /* HTM SPR registers. */
1250 "tfhar", "tfiar", "texasr",
1251 /* SPE High registers. */
1252 "0", "1", "2", "3", "4", "5", "6", "7",
1253 "8", "9", "10", "11", "12", "13", "14", "15",
1254 "16", "17", "18", "19", "20", "21", "22", "23",
1255 "24", "25", "26", "27", "28", "29", "30", "31"
1258 #ifdef TARGET_REGNAMES
1259 static const char alt_reg_names[][8] =
1261 "%r0", "%r1", "%r2", "%r3", "%r4", "%r5", "%r6", "%r7",
1262 "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15",
1263 "%r16", "%r17", "%r18", "%r19", "%r20", "%r21", "%r22", "%r23",
1264 "%r24", "%r25", "%r26", "%r27", "%r28", "%r29", "%r30", "%r31",
1265 "%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7",
1266 "%f8", "%f9", "%f10", "%f11", "%f12", "%f13", "%f14", "%f15",
1267 "%f16", "%f17", "%f18", "%f19", "%f20", "%f21", "%f22", "%f23",
1268 "%f24", "%f25", "%f26", "%f27", "%f28", "%f29", "%f30", "%f31",
1269 "mq", "lr", "ctr", "ap",
1270 "%cr0", "%cr1", "%cr2", "%cr3", "%cr4", "%cr5", "%cr6", "%cr7",
1271 "ca",
1272 /* AltiVec registers. */
1273 "%v0", "%v1", "%v2", "%v3", "%v4", "%v5", "%v6", "%v7",
1274 "%v8", "%v9", "%v10", "%v11", "%v12", "%v13", "%v14", "%v15",
1275 "%v16", "%v17", "%v18", "%v19", "%v20", "%v21", "%v22", "%v23",
1276 "%v24", "%v25", "%v26", "%v27", "%v28", "%v29", "%v30", "%v31",
1277 "vrsave", "vscr",
1278 /* SPE registers. */
1279 "spe_acc", "spefscr",
1280 /* Soft frame pointer. */
1281 "sfp",
1282 /* HTM SPR registers. */
1283 "tfhar", "tfiar", "texasr",
1284 /* SPE High registers. */
1285 "%rh0", "%rh1", "%rh2", "%rh3", "%rh4", "%rh5", "%rh6", "%rh7",
1286 "%rh8", "%rh9", "%rh10", "%r11", "%rh12", "%rh13", "%rh14", "%rh15",
1287 "%rh16", "%rh17", "%rh18", "%rh19", "%rh20", "%rh21", "%rh22", "%rh23",
1288 "%rh24", "%rh25", "%rh26", "%rh27", "%rh28", "%rh29", "%rh30", "%rh31"
1290 #endif
1292 /* Table of valid machine attributes. */
1294 static const struct attribute_spec rs6000_attribute_table[] =
1296 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
1297 affects_type_identity } */
1298 { "altivec", 1, 1, false, true, false, rs6000_handle_altivec_attribute,
1299 false },
1300 { "longcall", 0, 0, false, true, true, rs6000_handle_longcall_attribute,
1301 false },
1302 { "shortcall", 0, 0, false, true, true, rs6000_handle_longcall_attribute,
1303 false },
1304 { "ms_struct", 0, 0, false, false, false, rs6000_handle_struct_attribute,
1305 false },
1306 { "gcc_struct", 0, 0, false, false, false, rs6000_handle_struct_attribute,
1307 false },
1308 #ifdef SUBTARGET_ATTRIBUTE_TABLE
1309 SUBTARGET_ATTRIBUTE_TABLE,
1310 #endif
1311 { NULL, 0, 0, false, false, false, NULL, false }
1314 #ifndef TARGET_PROFILE_KERNEL
1315 #define TARGET_PROFILE_KERNEL 0
1316 #endif
1318 /* The VRSAVE bitmask puts bit %v0 as the most significant bit. */
1319 #define ALTIVEC_REG_BIT(REGNO) (0x80000000 >> ((REGNO) - FIRST_ALTIVEC_REGNO))
1321 /* Initialize the GCC target structure. */
1322 #undef TARGET_ATTRIBUTE_TABLE
1323 #define TARGET_ATTRIBUTE_TABLE rs6000_attribute_table
1324 #undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
1325 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES rs6000_set_default_type_attributes
1326 #undef TARGET_ATTRIBUTE_TAKES_IDENTIFIER_P
1327 #define TARGET_ATTRIBUTE_TAKES_IDENTIFIER_P rs6000_attribute_takes_identifier_p
1329 #undef TARGET_ASM_ALIGNED_DI_OP
1330 #define TARGET_ASM_ALIGNED_DI_OP DOUBLE_INT_ASM_OP
1332 /* Default unaligned ops are only provided for ELF. Find the ops needed
1333 for non-ELF systems. */
1334 #ifndef OBJECT_FORMAT_ELF
1335 #if TARGET_XCOFF
1336 /* For XCOFF. rs6000_assemble_integer will handle unaligned DIs on
1337 64-bit targets. */
1338 #undef TARGET_ASM_UNALIGNED_HI_OP
1339 #define TARGET_ASM_UNALIGNED_HI_OP "\t.vbyte\t2,"
1340 #undef TARGET_ASM_UNALIGNED_SI_OP
1341 #define TARGET_ASM_UNALIGNED_SI_OP "\t.vbyte\t4,"
1342 #undef TARGET_ASM_UNALIGNED_DI_OP
1343 #define TARGET_ASM_UNALIGNED_DI_OP "\t.vbyte\t8,"
1344 #else
1345 /* For Darwin. */
1346 #undef TARGET_ASM_UNALIGNED_HI_OP
1347 #define TARGET_ASM_UNALIGNED_HI_OP "\t.short\t"
1348 #undef TARGET_ASM_UNALIGNED_SI_OP
1349 #define TARGET_ASM_UNALIGNED_SI_OP "\t.long\t"
1350 #undef TARGET_ASM_UNALIGNED_DI_OP
1351 #define TARGET_ASM_UNALIGNED_DI_OP "\t.quad\t"
1352 #undef TARGET_ASM_ALIGNED_DI_OP
1353 #define TARGET_ASM_ALIGNED_DI_OP "\t.quad\t"
1354 #endif
1355 #endif
1357 /* This hook deals with fixups for relocatable code and DI-mode objects
1358 in 64-bit code. */
1359 #undef TARGET_ASM_INTEGER
1360 #define TARGET_ASM_INTEGER rs6000_assemble_integer
1362 #if defined (HAVE_GAS_HIDDEN) && !TARGET_MACHO
1363 #undef TARGET_ASM_ASSEMBLE_VISIBILITY
1364 #define TARGET_ASM_ASSEMBLE_VISIBILITY rs6000_assemble_visibility
1365 #endif
1367 #undef TARGET_SET_UP_BY_PROLOGUE
1368 #define TARGET_SET_UP_BY_PROLOGUE rs6000_set_up_by_prologue
1370 #undef TARGET_HAVE_TLS
1371 #define TARGET_HAVE_TLS HAVE_AS_TLS
1373 #undef TARGET_CANNOT_FORCE_CONST_MEM
1374 #define TARGET_CANNOT_FORCE_CONST_MEM rs6000_cannot_force_const_mem
1376 #undef TARGET_DELEGITIMIZE_ADDRESS
1377 #define TARGET_DELEGITIMIZE_ADDRESS rs6000_delegitimize_address
1379 #undef TARGET_CONST_NOT_OK_FOR_DEBUG_P
1380 #define TARGET_CONST_NOT_OK_FOR_DEBUG_P rs6000_const_not_ok_for_debug_p
1382 #undef TARGET_ASM_FUNCTION_PROLOGUE
1383 #define TARGET_ASM_FUNCTION_PROLOGUE rs6000_output_function_prologue
1384 #undef TARGET_ASM_FUNCTION_EPILOGUE
1385 #define TARGET_ASM_FUNCTION_EPILOGUE rs6000_output_function_epilogue
1387 #undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
1388 #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA rs6000_output_addr_const_extra
1390 #undef TARGET_LEGITIMIZE_ADDRESS
1391 #define TARGET_LEGITIMIZE_ADDRESS rs6000_legitimize_address
1393 #undef TARGET_SCHED_VARIABLE_ISSUE
1394 #define TARGET_SCHED_VARIABLE_ISSUE rs6000_variable_issue
1396 #undef TARGET_SCHED_ISSUE_RATE
1397 #define TARGET_SCHED_ISSUE_RATE rs6000_issue_rate
1398 #undef TARGET_SCHED_ADJUST_COST
1399 #define TARGET_SCHED_ADJUST_COST rs6000_adjust_cost
1400 #undef TARGET_SCHED_ADJUST_PRIORITY
1401 #define TARGET_SCHED_ADJUST_PRIORITY rs6000_adjust_priority
1402 #undef TARGET_SCHED_IS_COSTLY_DEPENDENCE
1403 #define TARGET_SCHED_IS_COSTLY_DEPENDENCE rs6000_is_costly_dependence
1404 #undef TARGET_SCHED_INIT
1405 #define TARGET_SCHED_INIT rs6000_sched_init
1406 #undef TARGET_SCHED_FINISH
1407 #define TARGET_SCHED_FINISH rs6000_sched_finish
1408 #undef TARGET_SCHED_REORDER
1409 #define TARGET_SCHED_REORDER rs6000_sched_reorder
1410 #undef TARGET_SCHED_REORDER2
1411 #define TARGET_SCHED_REORDER2 rs6000_sched_reorder2
1413 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
1414 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD rs6000_use_sched_lookahead
1416 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD
1417 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD rs6000_use_sched_lookahead_guard
1419 #undef TARGET_SCHED_ALLOC_SCHED_CONTEXT
1420 #define TARGET_SCHED_ALLOC_SCHED_CONTEXT rs6000_alloc_sched_context
1421 #undef TARGET_SCHED_INIT_SCHED_CONTEXT
1422 #define TARGET_SCHED_INIT_SCHED_CONTEXT rs6000_init_sched_context
1423 #undef TARGET_SCHED_SET_SCHED_CONTEXT
1424 #define TARGET_SCHED_SET_SCHED_CONTEXT rs6000_set_sched_context
1425 #undef TARGET_SCHED_FREE_SCHED_CONTEXT
1426 #define TARGET_SCHED_FREE_SCHED_CONTEXT rs6000_free_sched_context
1428 #undef TARGET_VECTORIZE_BUILTIN_MASK_FOR_LOAD
1429 #define TARGET_VECTORIZE_BUILTIN_MASK_FOR_LOAD rs6000_builtin_mask_for_load
1430 #undef TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT
1431 #define TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT \
1432 rs6000_builtin_support_vector_misalignment
1433 #undef TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE
1434 #define TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE rs6000_vector_alignment_reachable
1435 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST
1436 #define TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST \
1437 rs6000_builtin_vectorization_cost
1438 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
1439 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE \
1440 rs6000_preferred_simd_mode
1441 #undef TARGET_VECTORIZE_INIT_COST
1442 #define TARGET_VECTORIZE_INIT_COST rs6000_init_cost
1443 #undef TARGET_VECTORIZE_ADD_STMT_COST
1444 #define TARGET_VECTORIZE_ADD_STMT_COST rs6000_add_stmt_cost
1445 #undef TARGET_VECTORIZE_FINISH_COST
1446 #define TARGET_VECTORIZE_FINISH_COST rs6000_finish_cost
1447 #undef TARGET_VECTORIZE_DESTROY_COST_DATA
1448 #define TARGET_VECTORIZE_DESTROY_COST_DATA rs6000_destroy_cost_data
1450 #undef TARGET_INIT_BUILTINS
1451 #define TARGET_INIT_BUILTINS rs6000_init_builtins
1452 #undef TARGET_BUILTIN_DECL
1453 #define TARGET_BUILTIN_DECL rs6000_builtin_decl
1455 #undef TARGET_EXPAND_BUILTIN
1456 #define TARGET_EXPAND_BUILTIN rs6000_expand_builtin
1458 #undef TARGET_MANGLE_TYPE
1459 #define TARGET_MANGLE_TYPE rs6000_mangle_type
1461 #undef TARGET_INIT_LIBFUNCS
1462 #define TARGET_INIT_LIBFUNCS rs6000_init_libfuncs
1464 #if TARGET_MACHO
1465 #undef TARGET_BINDS_LOCAL_P
1466 #define TARGET_BINDS_LOCAL_P darwin_binds_local_p
1467 #endif
1469 #undef TARGET_MS_BITFIELD_LAYOUT_P
1470 #define TARGET_MS_BITFIELD_LAYOUT_P rs6000_ms_bitfield_layout_p
1472 #undef TARGET_ASM_OUTPUT_MI_THUNK
1473 #define TARGET_ASM_OUTPUT_MI_THUNK rs6000_output_mi_thunk
1475 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
1476 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
1478 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
1479 #define TARGET_FUNCTION_OK_FOR_SIBCALL rs6000_function_ok_for_sibcall
1481 #undef TARGET_REGISTER_MOVE_COST
1482 #define TARGET_REGISTER_MOVE_COST rs6000_register_move_cost
1483 #undef TARGET_MEMORY_MOVE_COST
1484 #define TARGET_MEMORY_MOVE_COST rs6000_memory_move_cost
1485 #undef TARGET_RTX_COSTS
1486 #define TARGET_RTX_COSTS rs6000_rtx_costs
1487 #undef TARGET_ADDRESS_COST
1488 #define TARGET_ADDRESS_COST hook_int_rtx_mode_as_bool_0
1490 #undef TARGET_DWARF_REGISTER_SPAN
1491 #define TARGET_DWARF_REGISTER_SPAN rs6000_dwarf_register_span
1493 #undef TARGET_INIT_DWARF_REG_SIZES_EXTRA
1494 #define TARGET_INIT_DWARF_REG_SIZES_EXTRA rs6000_init_dwarf_reg_sizes_extra
1496 #undef TARGET_MEMBER_TYPE_FORCES_BLK
1497 #define TARGET_MEMBER_TYPE_FORCES_BLK rs6000_member_type_forces_blk
1499 /* On rs6000, function arguments are promoted, as are function return
1500 values. */
1501 #undef TARGET_PROMOTE_FUNCTION_MODE
1502 #define TARGET_PROMOTE_FUNCTION_MODE default_promote_function_mode_always_promote
1504 #undef TARGET_RETURN_IN_MEMORY
1505 #define TARGET_RETURN_IN_MEMORY rs6000_return_in_memory
1507 #undef TARGET_RETURN_IN_MSB
1508 #define TARGET_RETURN_IN_MSB rs6000_return_in_msb
1510 #undef TARGET_SETUP_INCOMING_VARARGS
1511 #define TARGET_SETUP_INCOMING_VARARGS setup_incoming_varargs
1513 /* Always strict argument naming on rs6000. */
1514 #undef TARGET_STRICT_ARGUMENT_NAMING
1515 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
1516 #undef TARGET_PRETEND_OUTGOING_VARARGS_NAMED
1517 #define TARGET_PRETEND_OUTGOING_VARARGS_NAMED hook_bool_CUMULATIVE_ARGS_true
1518 #undef TARGET_SPLIT_COMPLEX_ARG
1519 #define TARGET_SPLIT_COMPLEX_ARG hook_bool_const_tree_true
1520 #undef TARGET_MUST_PASS_IN_STACK
1521 #define TARGET_MUST_PASS_IN_STACK rs6000_must_pass_in_stack
1522 #undef TARGET_PASS_BY_REFERENCE
1523 #define TARGET_PASS_BY_REFERENCE rs6000_pass_by_reference
1524 #undef TARGET_ARG_PARTIAL_BYTES
1525 #define TARGET_ARG_PARTIAL_BYTES rs6000_arg_partial_bytes
1526 #undef TARGET_FUNCTION_ARG_ADVANCE
1527 #define TARGET_FUNCTION_ARG_ADVANCE rs6000_function_arg_advance
1528 #undef TARGET_FUNCTION_ARG
1529 #define TARGET_FUNCTION_ARG rs6000_function_arg
1530 #undef TARGET_FUNCTION_ARG_BOUNDARY
1531 #define TARGET_FUNCTION_ARG_BOUNDARY rs6000_function_arg_boundary
1533 #undef TARGET_BUILD_BUILTIN_VA_LIST
1534 #define TARGET_BUILD_BUILTIN_VA_LIST rs6000_build_builtin_va_list
1536 #undef TARGET_EXPAND_BUILTIN_VA_START
1537 #define TARGET_EXPAND_BUILTIN_VA_START rs6000_va_start
1539 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
1540 #define TARGET_GIMPLIFY_VA_ARG_EXPR rs6000_gimplify_va_arg
1542 #undef TARGET_EH_RETURN_FILTER_MODE
1543 #define TARGET_EH_RETURN_FILTER_MODE rs6000_eh_return_filter_mode
1545 #undef TARGET_SCALAR_MODE_SUPPORTED_P
1546 #define TARGET_SCALAR_MODE_SUPPORTED_P rs6000_scalar_mode_supported_p
1548 #undef TARGET_VECTOR_MODE_SUPPORTED_P
1549 #define TARGET_VECTOR_MODE_SUPPORTED_P rs6000_vector_mode_supported_p
1551 #undef TARGET_INVALID_ARG_FOR_UNPROTOTYPED_FN
1552 #define TARGET_INVALID_ARG_FOR_UNPROTOTYPED_FN invalid_arg_for_unprototyped_fn
1554 #undef TARGET_ASM_LOOP_ALIGN_MAX_SKIP
1555 #define TARGET_ASM_LOOP_ALIGN_MAX_SKIP rs6000_loop_align_max_skip
1557 #undef TARGET_OPTION_OVERRIDE
1558 #define TARGET_OPTION_OVERRIDE rs6000_option_override
1560 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION
1561 #define TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION \
1562 rs6000_builtin_vectorized_function
1564 #if !TARGET_MACHO
1565 #undef TARGET_STACK_PROTECT_FAIL
1566 #define TARGET_STACK_PROTECT_FAIL rs6000_stack_protect_fail
1567 #endif
1569 /* MPC604EUM 3.5.2 Weak Consistency between Multiple Processors
1570 The PowerPC architecture requires only weak consistency among
1571 processors--that is, memory accesses between processors need not be
1572 sequentially consistent and memory accesses among processors can occur
1573 in any order. The ability to order memory accesses weakly provides
1574 opportunities for more efficient use of the system bus. Unless a
1575 dependency exists, the 604e allows read operations to precede store
1576 operations. */
1577 #undef TARGET_RELAXED_ORDERING
1578 #define TARGET_RELAXED_ORDERING true
1580 #ifdef HAVE_AS_TLS
1581 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
1582 #define TARGET_ASM_OUTPUT_DWARF_DTPREL rs6000_output_dwarf_dtprel
1583 #endif
1585 /* Use a 32-bit anchor range. This leads to sequences like:
1587 addis tmp,anchor,high
1588 add dest,tmp,low
1590 where tmp itself acts as an anchor, and can be shared between
1591 accesses to the same 64k page. */
1592 #undef TARGET_MIN_ANCHOR_OFFSET
1593 #define TARGET_MIN_ANCHOR_OFFSET -0x7fffffff - 1
1594 #undef TARGET_MAX_ANCHOR_OFFSET
1595 #define TARGET_MAX_ANCHOR_OFFSET 0x7fffffff
1596 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
1597 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P rs6000_use_blocks_for_constant_p
1598 #undef TARGET_USE_BLOCKS_FOR_DECL_P
1599 #define TARGET_USE_BLOCKS_FOR_DECL_P rs6000_use_blocks_for_decl_p
1601 #undef TARGET_BUILTIN_RECIPROCAL
1602 #define TARGET_BUILTIN_RECIPROCAL rs6000_builtin_reciprocal
1604 #undef TARGET_EXPAND_TO_RTL_HOOK
1605 #define TARGET_EXPAND_TO_RTL_HOOK rs6000_alloc_sdmode_stack_slot
1607 #undef TARGET_INSTANTIATE_DECLS
1608 #define TARGET_INSTANTIATE_DECLS rs6000_instantiate_decls
1610 #undef TARGET_SECONDARY_RELOAD
1611 #define TARGET_SECONDARY_RELOAD rs6000_secondary_reload
1613 #undef TARGET_LEGITIMATE_ADDRESS_P
1614 #define TARGET_LEGITIMATE_ADDRESS_P rs6000_legitimate_address_p
1616 #undef TARGET_MODE_DEPENDENT_ADDRESS_P
1617 #define TARGET_MODE_DEPENDENT_ADDRESS_P rs6000_mode_dependent_address_p
1619 #undef TARGET_LRA_P
1620 #define TARGET_LRA_P rs6000_lra_p
1622 #undef TARGET_CAN_ELIMINATE
1623 #define TARGET_CAN_ELIMINATE rs6000_can_eliminate
1625 #undef TARGET_CONDITIONAL_REGISTER_USAGE
1626 #define TARGET_CONDITIONAL_REGISTER_USAGE rs6000_conditional_register_usage
1628 #undef TARGET_TRAMPOLINE_INIT
1629 #define TARGET_TRAMPOLINE_INIT rs6000_trampoline_init
1631 #undef TARGET_FUNCTION_VALUE
1632 #define TARGET_FUNCTION_VALUE rs6000_function_value
1634 #undef TARGET_OPTION_VALID_ATTRIBUTE_P
1635 #define TARGET_OPTION_VALID_ATTRIBUTE_P rs6000_valid_attribute_p
1637 #undef TARGET_OPTION_SAVE
1638 #define TARGET_OPTION_SAVE rs6000_function_specific_save
1640 #undef TARGET_OPTION_RESTORE
1641 #define TARGET_OPTION_RESTORE rs6000_function_specific_restore
1643 #undef TARGET_OPTION_PRINT
1644 #define TARGET_OPTION_PRINT rs6000_function_specific_print
1646 #undef TARGET_CAN_INLINE_P
1647 #define TARGET_CAN_INLINE_P rs6000_can_inline_p
1649 #undef TARGET_SET_CURRENT_FUNCTION
1650 #define TARGET_SET_CURRENT_FUNCTION rs6000_set_current_function
1652 #undef TARGET_LEGITIMATE_CONSTANT_P
1653 #define TARGET_LEGITIMATE_CONSTANT_P rs6000_legitimate_constant_p
1655 #undef TARGET_VECTORIZE_VEC_PERM_CONST_OK
1656 #define TARGET_VECTORIZE_VEC_PERM_CONST_OK rs6000_vectorize_vec_perm_const_ok
1658 #undef TARGET_CAN_USE_DOLOOP_P
1659 #define TARGET_CAN_USE_DOLOOP_P can_use_doloop_if_innermost
1661 #undef TARGET_ATOMIC_ASSIGN_EXPAND_FENV
1662 #define TARGET_ATOMIC_ASSIGN_EXPAND_FENV rs6000_atomic_assign_expand_fenv
1665 /* Processor table. */
1666 struct rs6000_ptt
1668 const char *const name; /* Canonical processor name. */
1669 const enum processor_type processor; /* Processor type enum value. */
1670 const HOST_WIDE_INT target_enable; /* Target flags to enable. */
1673 static struct rs6000_ptt const processor_target_table[] =
1675 #define RS6000_CPU(NAME, CPU, FLAGS) { NAME, CPU, FLAGS },
1676 #include "rs6000-cpus.def"
1677 #undef RS6000_CPU
1680 /* Look up a processor name for -mcpu=xxx and -mtune=xxx. Return -1 if the
1681 name is invalid. */
1683 static int
1684 rs6000_cpu_name_lookup (const char *name)
1686 size_t i;
1688 if (name != NULL)
1690 for (i = 0; i < ARRAY_SIZE (processor_target_table); i++)
1691 if (! strcmp (name, processor_target_table[i].name))
1692 return (int)i;
1695 return -1;
1699 /* Return number of consecutive hard regs needed starting at reg REGNO
1700 to hold something of mode MODE.
1701 This is ordinarily the length in words of a value of mode MODE
1702 but can be less for certain modes in special long registers.
1704 For the SPE, GPRs are 64 bits but only 32 bits are visible in
1705 scalar instructions. The upper 32 bits are only available to the
1706 SIMD instructions.
1708 POWER and PowerPC GPRs hold 32 bits worth;
1709 PowerPC64 GPRs and FPRs point register holds 64 bits worth. */
1711 static int
1712 rs6000_hard_regno_nregs_internal (int regno, enum machine_mode mode)
1714 unsigned HOST_WIDE_INT reg_size;
1716 /* TF/TD modes are special in that they always take 2 registers. */
1717 if (FP_REGNO_P (regno))
1718 reg_size = ((VECTOR_MEM_VSX_P (mode) && mode != TDmode && mode != TFmode)
1719 ? UNITS_PER_VSX_WORD
1720 : UNITS_PER_FP_WORD);
1722 else if (SPE_SIMD_REGNO_P (regno) && TARGET_SPE && SPE_VECTOR_MODE (mode))
1723 reg_size = UNITS_PER_SPE_WORD;
1725 else if (ALTIVEC_REGNO_P (regno))
1726 reg_size = UNITS_PER_ALTIVEC_WORD;
1728 /* The value returned for SCmode in the E500 double case is 2 for
1729 ABI compatibility; storing an SCmode value in a single register
1730 would require function_arg and rs6000_spe_function_arg to handle
1731 SCmode so as to pass the value correctly in a pair of
1732 registers. */
1733 else if (TARGET_E500_DOUBLE && FLOAT_MODE_P (mode) && mode != SCmode
1734 && !DECIMAL_FLOAT_MODE_P (mode) && SPE_SIMD_REGNO_P (regno))
1735 reg_size = UNITS_PER_FP_WORD;
1737 else
1738 reg_size = UNITS_PER_WORD;
1740 return (GET_MODE_SIZE (mode) + reg_size - 1) / reg_size;
1743 /* Value is 1 if hard register REGNO can hold a value of machine-mode
1744 MODE. */
1745 static int
1746 rs6000_hard_regno_mode_ok (int regno, enum machine_mode mode)
1748 int last_regno = regno + rs6000_hard_regno_nregs[mode][regno] - 1;
1750 /* PTImode can only go in GPRs. Quad word memory operations require even/odd
1751 register combinations, and use PTImode where we need to deal with quad
1752 word memory operations. Don't allow quad words in the argument or frame
1753 pointer registers, just registers 0..31. */
1754 if (mode == PTImode)
1755 return (IN_RANGE (regno, FIRST_GPR_REGNO, LAST_GPR_REGNO)
1756 && IN_RANGE (last_regno, FIRST_GPR_REGNO, LAST_GPR_REGNO)
1757 && ((regno & 1) == 0));
1759 /* VSX registers that overlap the FPR registers are larger than for non-VSX
1760 implementations. Don't allow an item to be split between a FP register
1761 and an Altivec register. Allow TImode in all VSX registers if the user
1762 asked for it. */
1763 if (TARGET_VSX && VSX_REGNO_P (regno)
1764 && (VECTOR_MEM_VSX_P (mode)
1765 || reg_addr[mode].scalar_in_vmx_p
1766 || (TARGET_VSX_TIMODE && mode == TImode)
1767 || (TARGET_VADDUQM && mode == V1TImode)))
1769 if (FP_REGNO_P (regno))
1770 return FP_REGNO_P (last_regno);
1772 if (ALTIVEC_REGNO_P (regno))
1774 if (GET_MODE_SIZE (mode) != 16 && !reg_addr[mode].scalar_in_vmx_p)
1775 return 0;
1777 return ALTIVEC_REGNO_P (last_regno);
1781 /* The GPRs can hold any mode, but values bigger than one register
1782 cannot go past R31. */
1783 if (INT_REGNO_P (regno))
1784 return INT_REGNO_P (last_regno);
1786 /* The float registers (except for VSX vector modes) can only hold floating
1787 modes and DImode. */
1788 if (FP_REGNO_P (regno))
1790 if (SCALAR_FLOAT_MODE_P (mode)
1791 && (mode != TDmode || (regno % 2) == 0)
1792 && FP_REGNO_P (last_regno))
1793 return 1;
1795 if (GET_MODE_CLASS (mode) == MODE_INT
1796 && GET_MODE_SIZE (mode) == UNITS_PER_FP_WORD)
1797 return 1;
1799 if (PAIRED_SIMD_REGNO_P (regno) && TARGET_PAIRED_FLOAT
1800 && PAIRED_VECTOR_MODE (mode))
1801 return 1;
1803 return 0;
1806 /* The CR register can only hold CC modes. */
1807 if (CR_REGNO_P (regno))
1808 return GET_MODE_CLASS (mode) == MODE_CC;
1810 if (CA_REGNO_P (regno))
1811 return mode == Pmode || mode == SImode;
1813 /* AltiVec only in AldyVec registers. */
1814 if (ALTIVEC_REGNO_P (regno))
1815 return (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode)
1816 || mode == V1TImode);
1818 /* ...but GPRs can hold SIMD data on the SPE in one register. */
1819 if (SPE_SIMD_REGNO_P (regno) && TARGET_SPE && SPE_VECTOR_MODE (mode))
1820 return 1;
1822 /* We cannot put non-VSX TImode or PTImode anywhere except general register
1823 and it must be able to fit within the register set. */
1825 return GET_MODE_SIZE (mode) <= UNITS_PER_WORD;
1828 /* Print interesting facts about registers. */
1829 static void
1830 rs6000_debug_reg_print (int first_regno, int last_regno, const char *reg_name)
1832 int r, m;
1834 for (r = first_regno; r <= last_regno; ++r)
1836 const char *comma = "";
1837 int len;
1839 if (first_regno == last_regno)
1840 fprintf (stderr, "%s:\t", reg_name);
1841 else
1842 fprintf (stderr, "%s%d:\t", reg_name, r - first_regno);
1844 len = 8;
1845 for (m = 0; m < NUM_MACHINE_MODES; ++m)
1846 if (rs6000_hard_regno_mode_ok_p[m][r] && rs6000_hard_regno_nregs[m][r])
1848 if (len > 70)
1850 fprintf (stderr, ",\n\t");
1851 len = 8;
1852 comma = "";
1855 if (rs6000_hard_regno_nregs[m][r] > 1)
1856 len += fprintf (stderr, "%s%s/%d", comma, GET_MODE_NAME (m),
1857 rs6000_hard_regno_nregs[m][r]);
1858 else
1859 len += fprintf (stderr, "%s%s", comma, GET_MODE_NAME (m));
1861 comma = ", ";
1864 if (call_used_regs[r])
1866 if (len > 70)
1868 fprintf (stderr, ",\n\t");
1869 len = 8;
1870 comma = "";
1873 len += fprintf (stderr, "%s%s", comma, "call-used");
1874 comma = ", ";
1877 if (fixed_regs[r])
1879 if (len > 70)
1881 fprintf (stderr, ",\n\t");
1882 len = 8;
1883 comma = "";
1886 len += fprintf (stderr, "%s%s", comma, "fixed");
1887 comma = ", ";
1890 if (len > 70)
1892 fprintf (stderr, ",\n\t");
1893 comma = "";
1896 len += fprintf (stderr, "%sreg-class = %s", comma,
1897 reg_class_names[(int)rs6000_regno_regclass[r]]);
1898 comma = ", ";
1900 if (len > 70)
1902 fprintf (stderr, ",\n\t");
1903 comma = "";
1906 fprintf (stderr, "%sregno = %d\n", comma, r);
1910 static const char *
1911 rs6000_debug_vector_unit (enum rs6000_vector v)
1913 const char *ret;
1915 switch (v)
1917 case VECTOR_NONE: ret = "none"; break;
1918 case VECTOR_ALTIVEC: ret = "altivec"; break;
1919 case VECTOR_VSX: ret = "vsx"; break;
1920 case VECTOR_P8_VECTOR: ret = "p8_vector"; break;
1921 case VECTOR_PAIRED: ret = "paired"; break;
1922 case VECTOR_SPE: ret = "spe"; break;
1923 case VECTOR_OTHER: ret = "other"; break;
1924 default: ret = "unknown"; break;
1927 return ret;
1930 /* Print the address masks in a human readble fashion. */
1931 DEBUG_FUNCTION void
1932 rs6000_debug_print_mode (ssize_t m)
1934 ssize_t rc;
1936 fprintf (stderr, "Mode: %-5s", GET_MODE_NAME (m));
1937 for (rc = 0; rc < N_RELOAD_REG; rc++)
1939 addr_mask_type mask = reg_addr[m].addr_mask[rc];
1940 fprintf (stderr,
1941 " %s: %c%c%c%c%c%c",
1942 reload_reg_map[rc].name,
1943 (mask & RELOAD_REG_VALID) != 0 ? 'v' : ' ',
1944 (mask & RELOAD_REG_MULTIPLE) != 0 ? 'm' : ' ',
1945 (mask & RELOAD_REG_INDEXED) != 0 ? 'i' : ' ',
1946 (mask & RELOAD_REG_OFFSET) != 0 ? 'o' : ' ',
1947 (mask & RELOAD_REG_PRE_INCDEC) != 0 ? '+' : ' ',
1948 (mask & RELOAD_REG_PRE_MODIFY) != 0 ? '+' : ' ');
1951 if (rs6000_vector_unit[m] != VECTOR_NONE
1952 || rs6000_vector_mem[m] != VECTOR_NONE
1953 || (reg_addr[m].reload_store != CODE_FOR_nothing)
1954 || (reg_addr[m].reload_load != CODE_FOR_nothing)
1955 || reg_addr[m].scalar_in_vmx_p)
1957 fprintf (stderr,
1958 " Vector-arith=%-10s Vector-mem=%-10s Reload=%c%c Upper=%c",
1959 rs6000_debug_vector_unit (rs6000_vector_unit[m]),
1960 rs6000_debug_vector_unit (rs6000_vector_mem[m]),
1961 (reg_addr[m].reload_store != CODE_FOR_nothing) ? 's' : '*',
1962 (reg_addr[m].reload_load != CODE_FOR_nothing) ? 'l' : '*',
1963 (reg_addr[m].scalar_in_vmx_p) ? 'y' : 'n');
1966 fputs ("\n", stderr);
1969 #define DEBUG_FMT_ID "%-32s= "
1970 #define DEBUG_FMT_D DEBUG_FMT_ID "%d\n"
1971 #define DEBUG_FMT_WX DEBUG_FMT_ID "%#.12" HOST_WIDE_INT_PRINT "x: "
1972 #define DEBUG_FMT_S DEBUG_FMT_ID "%s\n"
1974 /* Print various interesting information with -mdebug=reg. */
1975 static void
1976 rs6000_debug_reg_global (void)
1978 static const char *const tf[2] = { "false", "true" };
1979 const char *nl = (const char *)0;
1980 int m;
1981 size_t m1, m2, v;
1982 char costly_num[20];
1983 char nop_num[20];
1984 char flags_buffer[40];
1985 const char *costly_str;
1986 const char *nop_str;
1987 const char *trace_str;
1988 const char *abi_str;
1989 const char *cmodel_str;
1990 struct cl_target_option cl_opts;
1992 /* Modes we want tieable information on. */
1993 static const enum machine_mode print_tieable_modes[] = {
1994 QImode,
1995 HImode,
1996 SImode,
1997 DImode,
1998 TImode,
1999 PTImode,
2000 SFmode,
2001 DFmode,
2002 TFmode,
2003 SDmode,
2004 DDmode,
2005 TDmode,
2006 V8QImode,
2007 V4HImode,
2008 V2SImode,
2009 V16QImode,
2010 V8HImode,
2011 V4SImode,
2012 V2DImode,
2013 V1TImode,
2014 V32QImode,
2015 V16HImode,
2016 V8SImode,
2017 V4DImode,
2018 V2TImode,
2019 V2SFmode,
2020 V4SFmode,
2021 V2DFmode,
2022 V8SFmode,
2023 V4DFmode,
2024 CCmode,
2025 CCUNSmode,
2026 CCEQmode,
2029 /* Virtual regs we are interested in. */
2030 const static struct {
2031 int regno; /* register number. */
2032 const char *name; /* register name. */
2033 } virtual_regs[] = {
2034 { STACK_POINTER_REGNUM, "stack pointer:" },
2035 { TOC_REGNUM, "toc: " },
2036 { STATIC_CHAIN_REGNUM, "static chain: " },
2037 { RS6000_PIC_OFFSET_TABLE_REGNUM, "pic offset: " },
2038 { HARD_FRAME_POINTER_REGNUM, "hard frame: " },
2039 { ARG_POINTER_REGNUM, "arg pointer: " },
2040 { FRAME_POINTER_REGNUM, "frame pointer:" },
2041 { FIRST_PSEUDO_REGISTER, "first pseudo: " },
2042 { FIRST_VIRTUAL_REGISTER, "first virtual:" },
2043 { VIRTUAL_INCOMING_ARGS_REGNUM, "incoming_args:" },
2044 { VIRTUAL_STACK_VARS_REGNUM, "stack_vars: " },
2045 { VIRTUAL_STACK_DYNAMIC_REGNUM, "stack_dynamic:" },
2046 { VIRTUAL_OUTGOING_ARGS_REGNUM, "outgoing_args:" },
2047 { VIRTUAL_CFA_REGNUM, "cfa (frame): " },
2048 { VIRTUAL_PREFERRED_STACK_BOUNDARY_REGNUM, "stack boundry:" },
2049 { LAST_VIRTUAL_REGISTER, "last virtual: " },
2052 fputs ("\nHard register information:\n", stderr);
2053 rs6000_debug_reg_print (FIRST_GPR_REGNO, LAST_GPR_REGNO, "gr");
2054 rs6000_debug_reg_print (FIRST_FPR_REGNO, LAST_FPR_REGNO, "fp");
2055 rs6000_debug_reg_print (FIRST_ALTIVEC_REGNO,
2056 LAST_ALTIVEC_REGNO,
2057 "vs");
2058 rs6000_debug_reg_print (LR_REGNO, LR_REGNO, "lr");
2059 rs6000_debug_reg_print (CTR_REGNO, CTR_REGNO, "ctr");
2060 rs6000_debug_reg_print (CR0_REGNO, CR7_REGNO, "cr");
2061 rs6000_debug_reg_print (CA_REGNO, CA_REGNO, "ca");
2062 rs6000_debug_reg_print (VRSAVE_REGNO, VRSAVE_REGNO, "vrsave");
2063 rs6000_debug_reg_print (VSCR_REGNO, VSCR_REGNO, "vscr");
2064 rs6000_debug_reg_print (SPE_ACC_REGNO, SPE_ACC_REGNO, "spe_a");
2065 rs6000_debug_reg_print (SPEFSCR_REGNO, SPEFSCR_REGNO, "spe_f");
2067 fputs ("\nVirtual/stack/frame registers:\n", stderr);
2068 for (v = 0; v < ARRAY_SIZE (virtual_regs); v++)
2069 fprintf (stderr, "%s regno = %3d\n", virtual_regs[v].name, virtual_regs[v].regno);
2071 fprintf (stderr,
2072 "\n"
2073 "d reg_class = %s\n"
2074 "f reg_class = %s\n"
2075 "v reg_class = %s\n"
2076 "wa reg_class = %s\n"
2077 "wd reg_class = %s\n"
2078 "wf reg_class = %s\n"
2079 "wg reg_class = %s\n"
2080 "wh reg_class = %s\n"
2081 "wi reg_class = %s\n"
2082 "wj reg_class = %s\n"
2083 "wk reg_class = %s\n"
2084 "wl reg_class = %s\n"
2085 "wm reg_class = %s\n"
2086 "wr reg_class = %s\n"
2087 "ws reg_class = %s\n"
2088 "wt reg_class = %s\n"
2089 "wu reg_class = %s\n"
2090 "wv reg_class = %s\n"
2091 "ww reg_class = %s\n"
2092 "wx reg_class = %s\n"
2093 "wy reg_class = %s\n"
2094 "wz reg_class = %s\n"
2095 "\n",
2096 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_d]],
2097 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_f]],
2098 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_v]],
2099 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wa]],
2100 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wd]],
2101 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wf]],
2102 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wg]],
2103 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wh]],
2104 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wi]],
2105 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wj]],
2106 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wk]],
2107 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wl]],
2108 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wm]],
2109 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wr]],
2110 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_ws]],
2111 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wt]],
2112 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wu]],
2113 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wv]],
2114 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_ww]],
2115 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wx]],
2116 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wy]],
2117 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wz]]);
2119 nl = "\n";
2120 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2121 rs6000_debug_print_mode (m);
2123 fputs ("\n", stderr);
2125 for (m1 = 0; m1 < ARRAY_SIZE (print_tieable_modes); m1++)
2127 enum machine_mode mode1 = print_tieable_modes[m1];
2128 bool first_time = true;
2130 nl = (const char *)0;
2131 for (m2 = 0; m2 < ARRAY_SIZE (print_tieable_modes); m2++)
2133 enum machine_mode mode2 = print_tieable_modes[m2];
2134 if (mode1 != mode2 && MODES_TIEABLE_P (mode1, mode2))
2136 if (first_time)
2138 fprintf (stderr, "Tieable modes %s:", GET_MODE_NAME (mode1));
2139 nl = "\n";
2140 first_time = false;
2143 fprintf (stderr, " %s", GET_MODE_NAME (mode2));
2147 if (!first_time)
2148 fputs ("\n", stderr);
2151 if (nl)
2152 fputs (nl, stderr);
2154 if (rs6000_recip_control)
2156 fprintf (stderr, "\nReciprocal mask = 0x%x\n", rs6000_recip_control);
2158 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2159 if (rs6000_recip_bits[m])
2161 fprintf (stderr,
2162 "Reciprocal estimate mode: %-5s divide: %s rsqrt: %s\n",
2163 GET_MODE_NAME (m),
2164 (RS6000_RECIP_AUTO_RE_P (m)
2165 ? "auto"
2166 : (RS6000_RECIP_HAVE_RE_P (m) ? "have" : "none")),
2167 (RS6000_RECIP_AUTO_RSQRTE_P (m)
2168 ? "auto"
2169 : (RS6000_RECIP_HAVE_RSQRTE_P (m) ? "have" : "none")));
2172 fputs ("\n", stderr);
2175 if (rs6000_cpu_index >= 0)
2177 const char *name = processor_target_table[rs6000_cpu_index].name;
2178 HOST_WIDE_INT flags
2179 = processor_target_table[rs6000_cpu_index].target_enable;
2181 sprintf (flags_buffer, "-mcpu=%s flags", name);
2182 rs6000_print_isa_options (stderr, 0, flags_buffer, flags);
2184 else
2185 fprintf (stderr, DEBUG_FMT_S, "cpu", "<none>");
2187 if (rs6000_tune_index >= 0)
2189 const char *name = processor_target_table[rs6000_tune_index].name;
2190 HOST_WIDE_INT flags
2191 = processor_target_table[rs6000_tune_index].target_enable;
2193 sprintf (flags_buffer, "-mtune=%s flags", name);
2194 rs6000_print_isa_options (stderr, 0, flags_buffer, flags);
2196 else
2197 fprintf (stderr, DEBUG_FMT_S, "tune", "<none>");
2199 cl_target_option_save (&cl_opts, &global_options);
2200 rs6000_print_isa_options (stderr, 0, "rs6000_isa_flags",
2201 rs6000_isa_flags);
2203 rs6000_print_isa_options (stderr, 0, "rs6000_isa_flags_explicit",
2204 rs6000_isa_flags_explicit);
2206 rs6000_print_builtin_options (stderr, 0, "rs6000_builtin_mask",
2207 rs6000_builtin_mask);
2209 rs6000_print_isa_options (stderr, 0, "TARGET_DEFAULT", TARGET_DEFAULT);
2211 fprintf (stderr, DEBUG_FMT_S, "--with-cpu default",
2212 OPTION_TARGET_CPU_DEFAULT ? OPTION_TARGET_CPU_DEFAULT : "<none>");
2214 switch (rs6000_sched_costly_dep)
2216 case max_dep_latency:
2217 costly_str = "max_dep_latency";
2218 break;
2220 case no_dep_costly:
2221 costly_str = "no_dep_costly";
2222 break;
2224 case all_deps_costly:
2225 costly_str = "all_deps_costly";
2226 break;
2228 case true_store_to_load_dep_costly:
2229 costly_str = "true_store_to_load_dep_costly";
2230 break;
2232 case store_to_load_dep_costly:
2233 costly_str = "store_to_load_dep_costly";
2234 break;
2236 default:
2237 costly_str = costly_num;
2238 sprintf (costly_num, "%d", (int)rs6000_sched_costly_dep);
2239 break;
2242 fprintf (stderr, DEBUG_FMT_S, "sched_costly_dep", costly_str);
2244 switch (rs6000_sched_insert_nops)
2246 case sched_finish_regroup_exact:
2247 nop_str = "sched_finish_regroup_exact";
2248 break;
2250 case sched_finish_pad_groups:
2251 nop_str = "sched_finish_pad_groups";
2252 break;
2254 case sched_finish_none:
2255 nop_str = "sched_finish_none";
2256 break;
2258 default:
2259 nop_str = nop_num;
2260 sprintf (nop_num, "%d", (int)rs6000_sched_insert_nops);
2261 break;
2264 fprintf (stderr, DEBUG_FMT_S, "sched_insert_nops", nop_str);
2266 switch (rs6000_sdata)
2268 default:
2269 case SDATA_NONE:
2270 break;
2272 case SDATA_DATA:
2273 fprintf (stderr, DEBUG_FMT_S, "sdata", "data");
2274 break;
2276 case SDATA_SYSV:
2277 fprintf (stderr, DEBUG_FMT_S, "sdata", "sysv");
2278 break;
2280 case SDATA_EABI:
2281 fprintf (stderr, DEBUG_FMT_S, "sdata", "eabi");
2282 break;
2286 switch (rs6000_traceback)
2288 case traceback_default: trace_str = "default"; break;
2289 case traceback_none: trace_str = "none"; break;
2290 case traceback_part: trace_str = "part"; break;
2291 case traceback_full: trace_str = "full"; break;
2292 default: trace_str = "unknown"; break;
2295 fprintf (stderr, DEBUG_FMT_S, "traceback", trace_str);
2297 switch (rs6000_current_cmodel)
2299 case CMODEL_SMALL: cmodel_str = "small"; break;
2300 case CMODEL_MEDIUM: cmodel_str = "medium"; break;
2301 case CMODEL_LARGE: cmodel_str = "large"; break;
2302 default: cmodel_str = "unknown"; break;
2305 fprintf (stderr, DEBUG_FMT_S, "cmodel", cmodel_str);
2307 switch (rs6000_current_abi)
2309 case ABI_NONE: abi_str = "none"; break;
2310 case ABI_AIX: abi_str = "aix"; break;
2311 case ABI_ELFv2: abi_str = "ELFv2"; break;
2312 case ABI_V4: abi_str = "V4"; break;
2313 case ABI_DARWIN: abi_str = "darwin"; break;
2314 default: abi_str = "unknown"; break;
2317 fprintf (stderr, DEBUG_FMT_S, "abi", abi_str);
2319 if (rs6000_altivec_abi)
2320 fprintf (stderr, DEBUG_FMT_S, "altivec_abi", "true");
2322 if (rs6000_spe_abi)
2323 fprintf (stderr, DEBUG_FMT_S, "spe_abi", "true");
2325 if (rs6000_darwin64_abi)
2326 fprintf (stderr, DEBUG_FMT_S, "darwin64_abi", "true");
2328 if (rs6000_float_gprs)
2329 fprintf (stderr, DEBUG_FMT_S, "float_gprs", "true");
2331 fprintf (stderr, DEBUG_FMT_S, "fprs",
2332 (TARGET_FPRS ? "true" : "false"));
2334 fprintf (stderr, DEBUG_FMT_S, "single_float",
2335 (TARGET_SINGLE_FLOAT ? "true" : "false"));
2337 fprintf (stderr, DEBUG_FMT_S, "double_float",
2338 (TARGET_DOUBLE_FLOAT ? "true" : "false"));
2340 fprintf (stderr, DEBUG_FMT_S, "soft_float",
2341 (TARGET_SOFT_FLOAT ? "true" : "false"));
2343 fprintf (stderr, DEBUG_FMT_S, "e500_single",
2344 (TARGET_E500_SINGLE ? "true" : "false"));
2346 fprintf (stderr, DEBUG_FMT_S, "e500_double",
2347 (TARGET_E500_DOUBLE ? "true" : "false"));
2349 if (TARGET_LINK_STACK)
2350 fprintf (stderr, DEBUG_FMT_S, "link_stack", "true");
2352 if (targetm.lra_p ())
2353 fprintf (stderr, DEBUG_FMT_S, "lra", "true");
2355 if (TARGET_P8_FUSION)
2356 fprintf (stderr, DEBUG_FMT_S, "p8 fusion",
2357 (TARGET_P8_FUSION_SIGN) ? "zero+sign" : "zero");
2359 fprintf (stderr, DEBUG_FMT_S, "plt-format",
2360 TARGET_SECURE_PLT ? "secure" : "bss");
2361 fprintf (stderr, DEBUG_FMT_S, "struct-return",
2362 aix_struct_return ? "aix" : "sysv");
2363 fprintf (stderr, DEBUG_FMT_S, "always_hint", tf[!!rs6000_always_hint]);
2364 fprintf (stderr, DEBUG_FMT_S, "sched_groups", tf[!!rs6000_sched_groups]);
2365 fprintf (stderr, DEBUG_FMT_S, "align_branch",
2366 tf[!!rs6000_align_branch_targets]);
2367 fprintf (stderr, DEBUG_FMT_D, "tls_size", rs6000_tls_size);
2368 fprintf (stderr, DEBUG_FMT_D, "long_double_size",
2369 rs6000_long_double_type_size);
2370 fprintf (stderr, DEBUG_FMT_D, "sched_restricted_insns_priority",
2371 (int)rs6000_sched_restricted_insns_priority);
2372 fprintf (stderr, DEBUG_FMT_D, "Number of standard builtins",
2373 (int)END_BUILTINS);
2374 fprintf (stderr, DEBUG_FMT_D, "Number of rs6000 builtins",
2375 (int)RS6000_BUILTIN_COUNT);
2377 if (TARGET_VSX)
2378 fprintf (stderr, DEBUG_FMT_D, "VSX easy 64-bit scalar element",
2379 (int)VECTOR_ELEMENT_SCALAR_64BIT);
2383 /* Update the addr mask bits in reg_addr to help secondary reload and go if
2384 legitimate address support to figure out the appropriate addressing to
2385 use. */
2387 static void
2388 rs6000_setup_reg_addr_masks (void)
2390 ssize_t rc, reg, m, nregs;
2391 addr_mask_type any_addr_mask, addr_mask;
2393 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2395 enum machine_mode m2 = (enum machine_mode)m;
2397 /* SDmode is special in that we want to access it only via REG+REG
2398 addressing on power7 and above, since we want to use the LFIWZX and
2399 STFIWZX instructions to load it. */
2400 bool indexed_only_p = (m == SDmode && TARGET_NO_SDMODE_STACK);
2402 any_addr_mask = 0;
2403 for (rc = FIRST_RELOAD_REG_CLASS; rc <= LAST_RELOAD_REG_CLASS; rc++)
2405 addr_mask = 0;
2406 reg = reload_reg_map[rc].reg;
2408 /* Can mode values go in the GPR/FPR/Altivec registers? */
2409 if (reg >= 0 && rs6000_hard_regno_mode_ok_p[m][reg])
2411 nregs = rs6000_hard_regno_nregs[m][reg];
2412 addr_mask |= RELOAD_REG_VALID;
2414 /* Indicate if the mode takes more than 1 physical register. If
2415 it takes a single register, indicate it can do REG+REG
2416 addressing. */
2417 if (nregs > 1 || m == BLKmode)
2418 addr_mask |= RELOAD_REG_MULTIPLE;
2419 else
2420 addr_mask |= RELOAD_REG_INDEXED;
2422 /* Figure out if we can do PRE_INC, PRE_DEC, or PRE_MODIFY
2423 addressing. Restrict addressing on SPE for 64-bit types
2424 because of the SUBREG hackery used to address 64-bit floats in
2425 '32-bit' GPRs. To simplify secondary reload, don't allow
2426 update forms on scalar floating point types that can go in the
2427 upper registers. */
2429 if (TARGET_UPDATE
2430 && (rc == RELOAD_REG_GPR || rc == RELOAD_REG_FPR)
2431 && GET_MODE_SIZE (m2) <= 8
2432 && !VECTOR_MODE_P (m2)
2433 && !COMPLEX_MODE_P (m2)
2434 && !indexed_only_p
2435 && !(TARGET_E500_DOUBLE && GET_MODE_SIZE (m2) == 8)
2436 && !reg_addr[m2].scalar_in_vmx_p)
2438 addr_mask |= RELOAD_REG_PRE_INCDEC;
2440 /* PRE_MODIFY is more restricted than PRE_INC/PRE_DEC in that
2441 we don't allow PRE_MODIFY for some multi-register
2442 operations. */
2443 switch (m)
2445 default:
2446 addr_mask |= RELOAD_REG_PRE_MODIFY;
2447 break;
2449 case DImode:
2450 if (TARGET_POWERPC64)
2451 addr_mask |= RELOAD_REG_PRE_MODIFY;
2452 break;
2454 case DFmode:
2455 case DDmode:
2456 if (TARGET_DF_INSN)
2457 addr_mask |= RELOAD_REG_PRE_MODIFY;
2458 break;
2463 /* GPR and FPR registers can do REG+OFFSET addressing, except
2464 possibly for SDmode. */
2465 if ((addr_mask != 0) && !indexed_only_p
2466 && (rc == RELOAD_REG_GPR || rc == RELOAD_REG_FPR))
2467 addr_mask |= RELOAD_REG_OFFSET;
2469 reg_addr[m].addr_mask[rc] = addr_mask;
2470 any_addr_mask |= addr_mask;
2473 reg_addr[m].addr_mask[RELOAD_REG_ANY] = any_addr_mask;
2478 /* Initialize the various global tables that are based on register size. */
2479 static void
2480 rs6000_init_hard_regno_mode_ok (bool global_init_p)
2482 ssize_t r, m, c;
2483 int align64;
2484 int align32;
2486 /* Precalculate REGNO_REG_CLASS. */
2487 rs6000_regno_regclass[0] = GENERAL_REGS;
2488 for (r = 1; r < 32; ++r)
2489 rs6000_regno_regclass[r] = BASE_REGS;
2491 for (r = 32; r < 64; ++r)
2492 rs6000_regno_regclass[r] = FLOAT_REGS;
2494 for (r = 64; r < FIRST_PSEUDO_REGISTER; ++r)
2495 rs6000_regno_regclass[r] = NO_REGS;
2497 for (r = FIRST_ALTIVEC_REGNO; r <= LAST_ALTIVEC_REGNO; ++r)
2498 rs6000_regno_regclass[r] = ALTIVEC_REGS;
2500 rs6000_regno_regclass[CR0_REGNO] = CR0_REGS;
2501 for (r = CR1_REGNO; r <= CR7_REGNO; ++r)
2502 rs6000_regno_regclass[r] = CR_REGS;
2504 rs6000_regno_regclass[LR_REGNO] = LINK_REGS;
2505 rs6000_regno_regclass[CTR_REGNO] = CTR_REGS;
2506 rs6000_regno_regclass[CA_REGNO] = NO_REGS;
2507 rs6000_regno_regclass[VRSAVE_REGNO] = VRSAVE_REGS;
2508 rs6000_regno_regclass[VSCR_REGNO] = VRSAVE_REGS;
2509 rs6000_regno_regclass[SPE_ACC_REGNO] = SPE_ACC_REGS;
2510 rs6000_regno_regclass[SPEFSCR_REGNO] = SPEFSCR_REGS;
2511 rs6000_regno_regclass[TFHAR_REGNO] = SPR_REGS;
2512 rs6000_regno_regclass[TFIAR_REGNO] = SPR_REGS;
2513 rs6000_regno_regclass[TEXASR_REGNO] = SPR_REGS;
2514 rs6000_regno_regclass[ARG_POINTER_REGNUM] = BASE_REGS;
2515 rs6000_regno_regclass[FRAME_POINTER_REGNUM] = BASE_REGS;
2517 /* Precalculate register class to simpler reload register class. We don't
2518 need all of the register classes that are combinations of different
2519 classes, just the simple ones that have constraint letters. */
2520 for (c = 0; c < N_REG_CLASSES; c++)
2521 reg_class_to_reg_type[c] = NO_REG_TYPE;
2523 reg_class_to_reg_type[(int)GENERAL_REGS] = GPR_REG_TYPE;
2524 reg_class_to_reg_type[(int)BASE_REGS] = GPR_REG_TYPE;
2525 reg_class_to_reg_type[(int)VSX_REGS] = VSX_REG_TYPE;
2526 reg_class_to_reg_type[(int)VRSAVE_REGS] = SPR_REG_TYPE;
2527 reg_class_to_reg_type[(int)VSCR_REGS] = SPR_REG_TYPE;
2528 reg_class_to_reg_type[(int)LINK_REGS] = SPR_REG_TYPE;
2529 reg_class_to_reg_type[(int)CTR_REGS] = SPR_REG_TYPE;
2530 reg_class_to_reg_type[(int)LINK_OR_CTR_REGS] = SPR_REG_TYPE;
2531 reg_class_to_reg_type[(int)CR_REGS] = CR_REG_TYPE;
2532 reg_class_to_reg_type[(int)CR0_REGS] = CR_REG_TYPE;
2533 reg_class_to_reg_type[(int)SPE_ACC_REGS] = SPE_ACC_TYPE;
2534 reg_class_to_reg_type[(int)SPEFSCR_REGS] = SPEFSCR_REG_TYPE;
2536 if (TARGET_VSX)
2538 reg_class_to_reg_type[(int)FLOAT_REGS] = VSX_REG_TYPE;
2539 reg_class_to_reg_type[(int)ALTIVEC_REGS] = VSX_REG_TYPE;
2541 else
2543 reg_class_to_reg_type[(int)FLOAT_REGS] = FPR_REG_TYPE;
2544 reg_class_to_reg_type[(int)ALTIVEC_REGS] = ALTIVEC_REG_TYPE;
2547 /* Precalculate the valid memory formats as well as the vector information,
2548 this must be set up before the rs6000_hard_regno_nregs_internal calls
2549 below. */
2550 gcc_assert ((int)VECTOR_NONE == 0);
2551 memset ((void *) &rs6000_vector_unit[0], '\0', sizeof (rs6000_vector_unit));
2552 memset ((void *) &rs6000_vector_mem[0], '\0', sizeof (rs6000_vector_unit));
2554 gcc_assert ((int)CODE_FOR_nothing == 0);
2555 memset ((void *) &reg_addr[0], '\0', sizeof (reg_addr));
2557 gcc_assert ((int)NO_REGS == 0);
2558 memset ((void *) &rs6000_constraints[0], '\0', sizeof (rs6000_constraints));
2560 /* The VSX hardware allows native alignment for vectors, but control whether the compiler
2561 believes it can use native alignment or still uses 128-bit alignment. */
2562 if (TARGET_VSX && !TARGET_VSX_ALIGN_128)
2564 align64 = 64;
2565 align32 = 32;
2567 else
2569 align64 = 128;
2570 align32 = 128;
2573 /* V2DF mode, VSX only. */
2574 if (TARGET_VSX)
2576 rs6000_vector_unit[V2DFmode] = VECTOR_VSX;
2577 rs6000_vector_mem[V2DFmode] = VECTOR_VSX;
2578 rs6000_vector_align[V2DFmode] = align64;
2581 /* V4SF mode, either VSX or Altivec. */
2582 if (TARGET_VSX)
2584 rs6000_vector_unit[V4SFmode] = VECTOR_VSX;
2585 rs6000_vector_mem[V4SFmode] = VECTOR_VSX;
2586 rs6000_vector_align[V4SFmode] = align32;
2588 else if (TARGET_ALTIVEC)
2590 rs6000_vector_unit[V4SFmode] = VECTOR_ALTIVEC;
2591 rs6000_vector_mem[V4SFmode] = VECTOR_ALTIVEC;
2592 rs6000_vector_align[V4SFmode] = align32;
2595 /* V16QImode, V8HImode, V4SImode are Altivec only, but possibly do VSX loads
2596 and stores. */
2597 if (TARGET_ALTIVEC)
2599 rs6000_vector_unit[V4SImode] = VECTOR_ALTIVEC;
2600 rs6000_vector_unit[V8HImode] = VECTOR_ALTIVEC;
2601 rs6000_vector_unit[V16QImode] = VECTOR_ALTIVEC;
2602 rs6000_vector_align[V4SImode] = align32;
2603 rs6000_vector_align[V8HImode] = align32;
2604 rs6000_vector_align[V16QImode] = align32;
2606 if (TARGET_VSX)
2608 rs6000_vector_mem[V4SImode] = VECTOR_VSX;
2609 rs6000_vector_mem[V8HImode] = VECTOR_VSX;
2610 rs6000_vector_mem[V16QImode] = VECTOR_VSX;
2612 else
2614 rs6000_vector_mem[V4SImode] = VECTOR_ALTIVEC;
2615 rs6000_vector_mem[V8HImode] = VECTOR_ALTIVEC;
2616 rs6000_vector_mem[V16QImode] = VECTOR_ALTIVEC;
2620 /* V2DImode, full mode depends on ISA 2.07 vector mode. Allow under VSX to
2621 do insert/splat/extract. Altivec doesn't have 64-bit integer support. */
2622 if (TARGET_VSX)
2624 rs6000_vector_mem[V2DImode] = VECTOR_VSX;
2625 rs6000_vector_unit[V2DImode]
2626 = (TARGET_P8_VECTOR) ? VECTOR_P8_VECTOR : VECTOR_NONE;
2627 rs6000_vector_align[V2DImode] = align64;
2629 rs6000_vector_mem[V1TImode] = VECTOR_VSX;
2630 rs6000_vector_unit[V1TImode]
2631 = (TARGET_P8_VECTOR) ? VECTOR_P8_VECTOR : VECTOR_NONE;
2632 rs6000_vector_align[V1TImode] = 128;
2635 /* DFmode, see if we want to use the VSX unit. */
2636 if (TARGET_VSX && TARGET_VSX_SCALAR_DOUBLE)
2638 rs6000_vector_unit[DFmode] = VECTOR_VSX;
2639 rs6000_vector_mem[DFmode]
2640 = (TARGET_UPPER_REGS_DF ? VECTOR_VSX : VECTOR_NONE);
2641 rs6000_vector_align[DFmode] = align64;
2644 /* Allow TImode in VSX register and set the VSX memory macros. */
2645 if (TARGET_VSX && TARGET_VSX_TIMODE)
2647 rs6000_vector_mem[TImode] = VECTOR_VSX;
2648 rs6000_vector_align[TImode] = align64;
2651 /* TODO add SPE and paired floating point vector support. */
2653 /* Register class constraints for the constraints that depend on compile
2654 switches. When the VSX code was added, different constraints were added
2655 based on the type (DFmode, V2DFmode, V4SFmode). For the vector types, all
2656 of the VSX registers are used. The register classes for scalar floating
2657 point types is set, based on whether we allow that type into the upper
2658 (Altivec) registers. GCC has register classes to target the Altivec
2659 registers for load/store operations, to select using a VSX memory
2660 operation instead of the traditional floating point operation. The
2661 constraints are:
2663 d - Register class to use with traditional DFmode instructions.
2664 f - Register class to use with traditional SFmode instructions.
2665 v - Altivec register.
2666 wa - Any VSX register.
2667 wc - Reserved to represent individual CR bits (used in LLVM).
2668 wd - Preferred register class for V2DFmode.
2669 wf - Preferred register class for V4SFmode.
2670 wg - Float register for power6x move insns.
2671 wh - FP register for direct move instructions.
2672 wi - FP or VSX register to hold 64-bit integers for VSX insns.
2673 wj - FP or VSX register to hold 64-bit integers for direct moves.
2674 wk - FP or VSX register to hold 64-bit doubles for direct moves.
2675 wl - Float register if we can do 32-bit signed int loads.
2676 wm - VSX register for ISA 2.07 direct move operations.
2677 wn - always NO_REGS.
2678 wr - GPR if 64-bit mode is permitted.
2679 ws - Register class to do ISA 2.06 DF operations.
2680 wt - VSX register for TImode in VSX registers.
2681 wu - Altivec register for ISA 2.07 VSX SF/SI load/stores.
2682 wv - Altivec register for ISA 2.06 VSX DF/DI load/stores.
2683 ww - Register class to do SF conversions in with VSX operations.
2684 wx - Float register if we can do 32-bit int stores.
2685 wy - Register class to do ISA 2.07 SF operations.
2686 wz - Float register if we can do 32-bit unsigned int loads. */
2688 if (TARGET_HARD_FLOAT && TARGET_FPRS)
2689 rs6000_constraints[RS6000_CONSTRAINT_f] = FLOAT_REGS; /* SFmode */
2691 if (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
2692 rs6000_constraints[RS6000_CONSTRAINT_d] = FLOAT_REGS; /* DFmode */
2694 if (TARGET_VSX)
2696 rs6000_constraints[RS6000_CONSTRAINT_wa] = VSX_REGS;
2697 rs6000_constraints[RS6000_CONSTRAINT_wd] = VSX_REGS; /* V2DFmode */
2698 rs6000_constraints[RS6000_CONSTRAINT_wf] = VSX_REGS; /* V4SFmode */
2699 rs6000_constraints[RS6000_CONSTRAINT_wi] = FLOAT_REGS; /* DImode */
2701 if (TARGET_VSX_TIMODE)
2702 rs6000_constraints[RS6000_CONSTRAINT_wt] = VSX_REGS; /* TImode */
2704 if (TARGET_UPPER_REGS_DF) /* DFmode */
2706 rs6000_constraints[RS6000_CONSTRAINT_ws] = VSX_REGS;
2707 rs6000_constraints[RS6000_CONSTRAINT_wv] = ALTIVEC_REGS;
2709 else
2710 rs6000_constraints[RS6000_CONSTRAINT_ws] = FLOAT_REGS;
2713 /* Add conditional constraints based on various options, to allow us to
2714 collapse multiple insn patterns. */
2715 if (TARGET_ALTIVEC)
2716 rs6000_constraints[RS6000_CONSTRAINT_v] = ALTIVEC_REGS;
2718 if (TARGET_MFPGPR) /* DFmode */
2719 rs6000_constraints[RS6000_CONSTRAINT_wg] = FLOAT_REGS;
2721 if (TARGET_LFIWAX)
2722 rs6000_constraints[RS6000_CONSTRAINT_wl] = FLOAT_REGS; /* DImode */
2724 if (TARGET_DIRECT_MOVE)
2726 rs6000_constraints[RS6000_CONSTRAINT_wh] = FLOAT_REGS;
2727 rs6000_constraints[RS6000_CONSTRAINT_wj] /* DImode */
2728 = rs6000_constraints[RS6000_CONSTRAINT_wi];
2729 rs6000_constraints[RS6000_CONSTRAINT_wk] /* DFmode */
2730 = rs6000_constraints[RS6000_CONSTRAINT_ws];
2731 rs6000_constraints[RS6000_CONSTRAINT_wm] = VSX_REGS;
2734 if (TARGET_POWERPC64)
2735 rs6000_constraints[RS6000_CONSTRAINT_wr] = GENERAL_REGS;
2737 if (TARGET_P8_VECTOR && TARGET_UPPER_REGS_SF) /* SFmode */
2739 rs6000_constraints[RS6000_CONSTRAINT_wu] = ALTIVEC_REGS;
2740 rs6000_constraints[RS6000_CONSTRAINT_wy] = VSX_REGS;
2741 rs6000_constraints[RS6000_CONSTRAINT_ww] = VSX_REGS;
2743 else if (TARGET_P8_VECTOR)
2745 rs6000_constraints[RS6000_CONSTRAINT_wy] = FLOAT_REGS;
2746 rs6000_constraints[RS6000_CONSTRAINT_ww] = FLOAT_REGS;
2748 else if (TARGET_VSX)
2749 rs6000_constraints[RS6000_CONSTRAINT_ww] = FLOAT_REGS;
2751 if (TARGET_STFIWX)
2752 rs6000_constraints[RS6000_CONSTRAINT_wx] = FLOAT_REGS; /* DImode */
2754 if (TARGET_LFIWZX)
2755 rs6000_constraints[RS6000_CONSTRAINT_wz] = FLOAT_REGS; /* DImode */
2757 /* Set up the reload helper and direct move functions. */
2758 if (TARGET_VSX || TARGET_ALTIVEC)
2760 if (TARGET_64BIT)
2762 reg_addr[V16QImode].reload_store = CODE_FOR_reload_v16qi_di_store;
2763 reg_addr[V16QImode].reload_load = CODE_FOR_reload_v16qi_di_load;
2764 reg_addr[V8HImode].reload_store = CODE_FOR_reload_v8hi_di_store;
2765 reg_addr[V8HImode].reload_load = CODE_FOR_reload_v8hi_di_load;
2766 reg_addr[V4SImode].reload_store = CODE_FOR_reload_v4si_di_store;
2767 reg_addr[V4SImode].reload_load = CODE_FOR_reload_v4si_di_load;
2768 reg_addr[V2DImode].reload_store = CODE_FOR_reload_v2di_di_store;
2769 reg_addr[V2DImode].reload_load = CODE_FOR_reload_v2di_di_load;
2770 reg_addr[V1TImode].reload_store = CODE_FOR_reload_v1ti_di_store;
2771 reg_addr[V1TImode].reload_load = CODE_FOR_reload_v1ti_di_load;
2772 reg_addr[V4SFmode].reload_store = CODE_FOR_reload_v4sf_di_store;
2773 reg_addr[V4SFmode].reload_load = CODE_FOR_reload_v4sf_di_load;
2774 reg_addr[V2DFmode].reload_store = CODE_FOR_reload_v2df_di_store;
2775 reg_addr[V2DFmode].reload_load = CODE_FOR_reload_v2df_di_load;
2776 if (TARGET_VSX && TARGET_UPPER_REGS_DF)
2778 reg_addr[DFmode].reload_store = CODE_FOR_reload_df_di_store;
2779 reg_addr[DFmode].reload_load = CODE_FOR_reload_df_di_load;
2780 reg_addr[DFmode].scalar_in_vmx_p = true;
2781 reg_addr[DDmode].reload_store = CODE_FOR_reload_dd_di_store;
2782 reg_addr[DDmode].reload_load = CODE_FOR_reload_dd_di_load;
2784 if (TARGET_P8_VECTOR)
2786 reg_addr[SFmode].reload_store = CODE_FOR_reload_sf_di_store;
2787 reg_addr[SFmode].reload_load = CODE_FOR_reload_sf_di_load;
2788 reg_addr[SDmode].reload_store = CODE_FOR_reload_sd_di_store;
2789 reg_addr[SDmode].reload_load = CODE_FOR_reload_sd_di_load;
2790 if (TARGET_UPPER_REGS_SF)
2791 reg_addr[SFmode].scalar_in_vmx_p = true;
2793 if (TARGET_VSX_TIMODE)
2795 reg_addr[TImode].reload_store = CODE_FOR_reload_ti_di_store;
2796 reg_addr[TImode].reload_load = CODE_FOR_reload_ti_di_load;
2798 if (TARGET_DIRECT_MOVE)
2800 if (TARGET_POWERPC64)
2802 reg_addr[TImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxti;
2803 reg_addr[V1TImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv1ti;
2804 reg_addr[V2DFmode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv2df;
2805 reg_addr[V2DImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv2di;
2806 reg_addr[V4SFmode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv4sf;
2807 reg_addr[V4SImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv4si;
2808 reg_addr[V8HImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv8hi;
2809 reg_addr[V16QImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv16qi;
2810 reg_addr[SFmode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxsf;
2812 reg_addr[TImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprti;
2813 reg_addr[V1TImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv1ti;
2814 reg_addr[V2DFmode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv2df;
2815 reg_addr[V2DImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv2di;
2816 reg_addr[V4SFmode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv4sf;
2817 reg_addr[V4SImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv4si;
2818 reg_addr[V8HImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv8hi;
2819 reg_addr[V16QImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv16qi;
2820 reg_addr[SFmode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprsf;
2822 else
2824 reg_addr[DImode].reload_fpr_gpr = CODE_FOR_reload_fpr_from_gprdi;
2825 reg_addr[DDmode].reload_fpr_gpr = CODE_FOR_reload_fpr_from_gprdd;
2826 reg_addr[DFmode].reload_fpr_gpr = CODE_FOR_reload_fpr_from_gprdf;
2830 else
2832 reg_addr[V16QImode].reload_store = CODE_FOR_reload_v16qi_si_store;
2833 reg_addr[V16QImode].reload_load = CODE_FOR_reload_v16qi_si_load;
2834 reg_addr[V8HImode].reload_store = CODE_FOR_reload_v8hi_si_store;
2835 reg_addr[V8HImode].reload_load = CODE_FOR_reload_v8hi_si_load;
2836 reg_addr[V4SImode].reload_store = CODE_FOR_reload_v4si_si_store;
2837 reg_addr[V4SImode].reload_load = CODE_FOR_reload_v4si_si_load;
2838 reg_addr[V2DImode].reload_store = CODE_FOR_reload_v2di_si_store;
2839 reg_addr[V2DImode].reload_load = CODE_FOR_reload_v2di_si_load;
2840 reg_addr[V1TImode].reload_store = CODE_FOR_reload_v1ti_si_store;
2841 reg_addr[V1TImode].reload_load = CODE_FOR_reload_v1ti_si_load;
2842 reg_addr[V4SFmode].reload_store = CODE_FOR_reload_v4sf_si_store;
2843 reg_addr[V4SFmode].reload_load = CODE_FOR_reload_v4sf_si_load;
2844 reg_addr[V2DFmode].reload_store = CODE_FOR_reload_v2df_si_store;
2845 reg_addr[V2DFmode].reload_load = CODE_FOR_reload_v2df_si_load;
2846 if (TARGET_VSX && TARGET_UPPER_REGS_DF)
2848 reg_addr[DFmode].reload_store = CODE_FOR_reload_df_si_store;
2849 reg_addr[DFmode].reload_load = CODE_FOR_reload_df_si_load;
2850 reg_addr[DFmode].scalar_in_vmx_p = true;
2851 reg_addr[DDmode].reload_store = CODE_FOR_reload_dd_si_store;
2852 reg_addr[DDmode].reload_load = CODE_FOR_reload_dd_si_load;
2854 if (TARGET_P8_VECTOR)
2856 reg_addr[SFmode].reload_store = CODE_FOR_reload_sf_si_store;
2857 reg_addr[SFmode].reload_load = CODE_FOR_reload_sf_si_load;
2858 reg_addr[SDmode].reload_store = CODE_FOR_reload_sd_si_store;
2859 reg_addr[SDmode].reload_load = CODE_FOR_reload_sd_si_load;
2860 if (TARGET_UPPER_REGS_SF)
2861 reg_addr[SFmode].scalar_in_vmx_p = true;
2863 if (TARGET_VSX_TIMODE)
2865 reg_addr[TImode].reload_store = CODE_FOR_reload_ti_si_store;
2866 reg_addr[TImode].reload_load = CODE_FOR_reload_ti_si_load;
2871 /* Precalculate HARD_REGNO_NREGS. */
2872 for (r = 0; r < FIRST_PSEUDO_REGISTER; ++r)
2873 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2874 rs6000_hard_regno_nregs[m][r]
2875 = rs6000_hard_regno_nregs_internal (r, (enum machine_mode)m);
2877 /* Precalculate HARD_REGNO_MODE_OK. */
2878 for (r = 0; r < FIRST_PSEUDO_REGISTER; ++r)
2879 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2880 if (rs6000_hard_regno_mode_ok (r, (enum machine_mode)m))
2881 rs6000_hard_regno_mode_ok_p[m][r] = true;
2883 /* Precalculate CLASS_MAX_NREGS sizes. */
2884 for (c = 0; c < LIM_REG_CLASSES; ++c)
2886 int reg_size;
2888 if (TARGET_VSX && VSX_REG_CLASS_P (c))
2889 reg_size = UNITS_PER_VSX_WORD;
2891 else if (c == ALTIVEC_REGS)
2892 reg_size = UNITS_PER_ALTIVEC_WORD;
2894 else if (c == FLOAT_REGS)
2895 reg_size = UNITS_PER_FP_WORD;
2897 else
2898 reg_size = UNITS_PER_WORD;
2900 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2902 enum machine_mode m2 = (enum machine_mode)m;
2903 int reg_size2 = reg_size;
2905 /* TFmode/TDmode always takes 2 registers, even in VSX. */
2906 if (TARGET_VSX && VSX_REG_CLASS_P (c)
2907 && (m == TDmode || m == TFmode))
2908 reg_size2 = UNITS_PER_FP_WORD;
2910 rs6000_class_max_nregs[m][c]
2911 = (GET_MODE_SIZE (m2) + reg_size2 - 1) / reg_size2;
2915 if (TARGET_E500_DOUBLE)
2916 rs6000_class_max_nregs[DFmode][GENERAL_REGS] = 1;
2918 /* Calculate which modes to automatically generate code to use a the
2919 reciprocal divide and square root instructions. In the future, possibly
2920 automatically generate the instructions even if the user did not specify
2921 -mrecip. The older machines double precision reciprocal sqrt estimate is
2922 not accurate enough. */
2923 memset (rs6000_recip_bits, 0, sizeof (rs6000_recip_bits));
2924 if (TARGET_FRES)
2925 rs6000_recip_bits[SFmode] = RS6000_RECIP_MASK_HAVE_RE;
2926 if (TARGET_FRE)
2927 rs6000_recip_bits[DFmode] = RS6000_RECIP_MASK_HAVE_RE;
2928 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode))
2929 rs6000_recip_bits[V4SFmode] = RS6000_RECIP_MASK_HAVE_RE;
2930 if (VECTOR_UNIT_VSX_P (V2DFmode))
2931 rs6000_recip_bits[V2DFmode] = RS6000_RECIP_MASK_HAVE_RE;
2933 if (TARGET_FRSQRTES)
2934 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2935 if (TARGET_FRSQRTE)
2936 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2937 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode))
2938 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2939 if (VECTOR_UNIT_VSX_P (V2DFmode))
2940 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2942 if (rs6000_recip_control)
2944 if (!flag_finite_math_only)
2945 warning (0, "-mrecip requires -ffinite-math or -ffast-math");
2946 if (flag_trapping_math)
2947 warning (0, "-mrecip requires -fno-trapping-math or -ffast-math");
2948 if (!flag_reciprocal_math)
2949 warning (0, "-mrecip requires -freciprocal-math or -ffast-math");
2950 if (flag_finite_math_only && !flag_trapping_math && flag_reciprocal_math)
2952 if (RS6000_RECIP_HAVE_RE_P (SFmode)
2953 && (rs6000_recip_control & RECIP_SF_DIV) != 0)
2954 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2956 if (RS6000_RECIP_HAVE_RE_P (DFmode)
2957 && (rs6000_recip_control & RECIP_DF_DIV) != 0)
2958 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2960 if (RS6000_RECIP_HAVE_RE_P (V4SFmode)
2961 && (rs6000_recip_control & RECIP_V4SF_DIV) != 0)
2962 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2964 if (RS6000_RECIP_HAVE_RE_P (V2DFmode)
2965 && (rs6000_recip_control & RECIP_V2DF_DIV) != 0)
2966 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2968 if (RS6000_RECIP_HAVE_RSQRTE_P (SFmode)
2969 && (rs6000_recip_control & RECIP_SF_RSQRT) != 0)
2970 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2972 if (RS6000_RECIP_HAVE_RSQRTE_P (DFmode)
2973 && (rs6000_recip_control & RECIP_DF_RSQRT) != 0)
2974 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2976 if (RS6000_RECIP_HAVE_RSQRTE_P (V4SFmode)
2977 && (rs6000_recip_control & RECIP_V4SF_RSQRT) != 0)
2978 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2980 if (RS6000_RECIP_HAVE_RSQRTE_P (V2DFmode)
2981 && (rs6000_recip_control & RECIP_V2DF_RSQRT) != 0)
2982 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2986 /* Update the addr mask bits in reg_addr to help secondary reload and go if
2987 legitimate address support to figure out the appropriate addressing to
2988 use. */
2989 rs6000_setup_reg_addr_masks ();
2991 if (global_init_p || TARGET_DEBUG_TARGET)
2993 if (TARGET_DEBUG_REG)
2994 rs6000_debug_reg_global ();
2996 if (TARGET_DEBUG_COST || TARGET_DEBUG_REG)
2997 fprintf (stderr,
2998 "SImode variable mult cost = %d\n"
2999 "SImode constant mult cost = %d\n"
3000 "SImode short constant mult cost = %d\n"
3001 "DImode multipliciation cost = %d\n"
3002 "SImode division cost = %d\n"
3003 "DImode division cost = %d\n"
3004 "Simple fp operation cost = %d\n"
3005 "DFmode multiplication cost = %d\n"
3006 "SFmode division cost = %d\n"
3007 "DFmode division cost = %d\n"
3008 "cache line size = %d\n"
3009 "l1 cache size = %d\n"
3010 "l2 cache size = %d\n"
3011 "simultaneous prefetches = %d\n"
3012 "\n",
3013 rs6000_cost->mulsi,
3014 rs6000_cost->mulsi_const,
3015 rs6000_cost->mulsi_const9,
3016 rs6000_cost->muldi,
3017 rs6000_cost->divsi,
3018 rs6000_cost->divdi,
3019 rs6000_cost->fp,
3020 rs6000_cost->dmul,
3021 rs6000_cost->sdiv,
3022 rs6000_cost->ddiv,
3023 rs6000_cost->cache_line_size,
3024 rs6000_cost->l1_cache_size,
3025 rs6000_cost->l2_cache_size,
3026 rs6000_cost->simultaneous_prefetches);
3030 #if TARGET_MACHO
3031 /* The Darwin version of SUBTARGET_OVERRIDE_OPTIONS. */
3033 static void
3034 darwin_rs6000_override_options (void)
3036 /* The Darwin ABI always includes AltiVec, can't be (validly) turned
3037 off. */
3038 rs6000_altivec_abi = 1;
3039 TARGET_ALTIVEC_VRSAVE = 1;
3040 rs6000_current_abi = ABI_DARWIN;
3042 if (DEFAULT_ABI == ABI_DARWIN
3043 && TARGET_64BIT)
3044 darwin_one_byte_bool = 1;
3046 if (TARGET_64BIT && ! TARGET_POWERPC64)
3048 rs6000_isa_flags |= OPTION_MASK_POWERPC64;
3049 warning (0, "-m64 requires PowerPC64 architecture, enabling");
3051 if (flag_mkernel)
3053 rs6000_default_long_calls = 1;
3054 rs6000_isa_flags |= OPTION_MASK_SOFT_FLOAT;
3057 /* Make -m64 imply -maltivec. Darwin's 64-bit ABI includes
3058 Altivec. */
3059 if (!flag_mkernel && !flag_apple_kext
3060 && TARGET_64BIT
3061 && ! (rs6000_isa_flags_explicit & OPTION_MASK_ALTIVEC))
3062 rs6000_isa_flags |= OPTION_MASK_ALTIVEC;
3064 /* Unless the user (not the configurer) has explicitly overridden
3065 it with -mcpu=G3 or -mno-altivec, then 10.5+ targets default to
3066 G4 unless targeting the kernel. */
3067 if (!flag_mkernel
3068 && !flag_apple_kext
3069 && strverscmp (darwin_macosx_version_min, "10.5") >= 0
3070 && ! (rs6000_isa_flags_explicit & OPTION_MASK_ALTIVEC)
3071 && ! global_options_set.x_rs6000_cpu_index)
3073 rs6000_isa_flags |= OPTION_MASK_ALTIVEC;
3076 #endif
3078 /* If not otherwise specified by a target, make 'long double' equivalent to
3079 'double'. */
3081 #ifndef RS6000_DEFAULT_LONG_DOUBLE_SIZE
3082 #define RS6000_DEFAULT_LONG_DOUBLE_SIZE 64
3083 #endif
3085 /* Return the builtin mask of the various options used that could affect which
3086 builtins were used. In the past we used target_flags, but we've run out of
3087 bits, and some options like SPE and PAIRED are no longer in
3088 target_flags. */
3090 HOST_WIDE_INT
3091 rs6000_builtin_mask_calculate (void)
3093 return (((TARGET_ALTIVEC) ? RS6000_BTM_ALTIVEC : 0)
3094 | ((TARGET_VSX) ? RS6000_BTM_VSX : 0)
3095 | ((TARGET_SPE) ? RS6000_BTM_SPE : 0)
3096 | ((TARGET_PAIRED_FLOAT) ? RS6000_BTM_PAIRED : 0)
3097 | ((TARGET_FRE) ? RS6000_BTM_FRE : 0)
3098 | ((TARGET_FRES) ? RS6000_BTM_FRES : 0)
3099 | ((TARGET_FRSQRTE) ? RS6000_BTM_FRSQRTE : 0)
3100 | ((TARGET_FRSQRTES) ? RS6000_BTM_FRSQRTES : 0)
3101 | ((TARGET_POPCNTD) ? RS6000_BTM_POPCNTD : 0)
3102 | ((rs6000_cpu == PROCESSOR_CELL) ? RS6000_BTM_CELL : 0)
3103 | ((TARGET_P8_VECTOR) ? RS6000_BTM_P8_VECTOR : 0)
3104 | ((TARGET_CRYPTO) ? RS6000_BTM_CRYPTO : 0)
3105 | ((TARGET_HTM) ? RS6000_BTM_HTM : 0)
3106 | ((TARGET_DFP) ? RS6000_BTM_DFP : 0)
3107 | ((TARGET_HARD_FLOAT) ? RS6000_BTM_HARD_FLOAT : 0)
3108 | ((TARGET_LONG_DOUBLE_128) ? RS6000_BTM_LDBL128 : 0));
3111 /* Override command line options. Mostly we process the processor type and
3112 sometimes adjust other TARGET_ options. */
3114 static bool
3115 rs6000_option_override_internal (bool global_init_p)
3117 bool ret = true;
3118 bool have_cpu = false;
3120 /* The default cpu requested at configure time, if any. */
3121 const char *implicit_cpu = OPTION_TARGET_CPU_DEFAULT;
3123 HOST_WIDE_INT set_masks;
3124 int cpu_index;
3125 int tune_index;
3126 struct cl_target_option *main_target_opt
3127 = ((global_init_p || target_option_default_node == NULL)
3128 ? NULL : TREE_TARGET_OPTION (target_option_default_node));
3130 /* Remember the explicit arguments. */
3131 if (global_init_p)
3132 rs6000_isa_flags_explicit = global_options_set.x_rs6000_isa_flags;
3134 /* On 64-bit Darwin, power alignment is ABI-incompatible with some C
3135 library functions, so warn about it. The flag may be useful for
3136 performance studies from time to time though, so don't disable it
3137 entirely. */
3138 if (global_options_set.x_rs6000_alignment_flags
3139 && rs6000_alignment_flags == MASK_ALIGN_POWER
3140 && DEFAULT_ABI == ABI_DARWIN
3141 && TARGET_64BIT)
3142 warning (0, "-malign-power is not supported for 64-bit Darwin;"
3143 " it is incompatible with the installed C and C++ libraries");
3145 /* Numerous experiment shows that IRA based loop pressure
3146 calculation works better for RTL loop invariant motion on targets
3147 with enough (>= 32) registers. It is an expensive optimization.
3148 So it is on only for peak performance. */
3149 if (optimize >= 3 && global_init_p
3150 && !global_options_set.x_flag_ira_loop_pressure)
3151 flag_ira_loop_pressure = 1;
3153 /* Set the pointer size. */
3154 if (TARGET_64BIT)
3156 rs6000_pmode = (int)DImode;
3157 rs6000_pointer_size = 64;
3159 else
3161 rs6000_pmode = (int)SImode;
3162 rs6000_pointer_size = 32;
3165 /* Some OSs don't support saving the high part of 64-bit registers on context
3166 switch. Other OSs don't support saving Altivec registers. On those OSs,
3167 we don't touch the OPTION_MASK_POWERPC64 or OPTION_MASK_ALTIVEC settings;
3168 if the user wants either, the user must explicitly specify them and we
3169 won't interfere with the user's specification. */
3171 set_masks = POWERPC_MASKS;
3172 #ifdef OS_MISSING_POWERPC64
3173 if (OS_MISSING_POWERPC64)
3174 set_masks &= ~OPTION_MASK_POWERPC64;
3175 #endif
3176 #ifdef OS_MISSING_ALTIVEC
3177 if (OS_MISSING_ALTIVEC)
3178 set_masks &= ~(OPTION_MASK_ALTIVEC | OPTION_MASK_VSX);
3179 #endif
3181 /* Don't override by the processor default if given explicitly. */
3182 set_masks &= ~rs6000_isa_flags_explicit;
3184 /* Process the -mcpu=<xxx> and -mtune=<xxx> argument. If the user changed
3185 the cpu in a target attribute or pragma, but did not specify a tuning
3186 option, use the cpu for the tuning option rather than the option specified
3187 with -mtune on the command line. Process a '--with-cpu' configuration
3188 request as an implicit --cpu. */
3189 if (rs6000_cpu_index >= 0)
3191 cpu_index = rs6000_cpu_index;
3192 have_cpu = true;
3194 else if (main_target_opt != NULL && main_target_opt->x_rs6000_cpu_index >= 0)
3196 rs6000_cpu_index = cpu_index = main_target_opt->x_rs6000_cpu_index;
3197 have_cpu = true;
3199 else if (implicit_cpu)
3201 rs6000_cpu_index = cpu_index = rs6000_cpu_name_lookup (implicit_cpu);
3202 have_cpu = true;
3204 else
3206 const char *default_cpu = (TARGET_POWERPC64 ? "powerpc64" : "powerpc");
3207 rs6000_cpu_index = cpu_index = rs6000_cpu_name_lookup (default_cpu);
3208 have_cpu = false;
3211 gcc_assert (cpu_index >= 0);
3213 /* If we have a cpu, either through an explicit -mcpu=<xxx> or if the
3214 compiler was configured with --with-cpu=<xxx>, replace all of the ISA bits
3215 with those from the cpu, except for options that were explicitly set. If
3216 we don't have a cpu, do not override the target bits set in
3217 TARGET_DEFAULT. */
3218 if (have_cpu)
3220 rs6000_isa_flags &= ~set_masks;
3221 rs6000_isa_flags |= (processor_target_table[cpu_index].target_enable
3222 & set_masks);
3224 else
3225 rs6000_isa_flags |= (processor_target_table[cpu_index].target_enable
3226 & ~rs6000_isa_flags_explicit);
3228 /* If no -mcpu=<xxx>, inherit any default options that were cleared via
3229 POWERPC_MASKS. Originally, TARGET_DEFAULT was used to initialize
3230 target_flags via the TARGET_DEFAULT_TARGET_FLAGS hook. When we switched
3231 to using rs6000_isa_flags, we need to do the initialization here. */
3232 if (!have_cpu)
3233 rs6000_isa_flags |= (TARGET_DEFAULT & ~rs6000_isa_flags_explicit);
3235 if (rs6000_tune_index >= 0)
3236 tune_index = rs6000_tune_index;
3237 else if (have_cpu)
3238 rs6000_tune_index = tune_index = cpu_index;
3239 else
3241 size_t i;
3242 enum processor_type tune_proc
3243 = (TARGET_POWERPC64 ? PROCESSOR_DEFAULT64 : PROCESSOR_DEFAULT);
3245 tune_index = -1;
3246 for (i = 0; i < ARRAY_SIZE (processor_target_table); i++)
3247 if (processor_target_table[i].processor == tune_proc)
3249 rs6000_tune_index = tune_index = i;
3250 break;
3254 gcc_assert (tune_index >= 0);
3255 rs6000_cpu = processor_target_table[tune_index].processor;
3257 /* Pick defaults for SPE related control flags. Do this early to make sure
3258 that the TARGET_ macros are representative ASAP. */
3260 int spe_capable_cpu =
3261 (rs6000_cpu == PROCESSOR_PPC8540
3262 || rs6000_cpu == PROCESSOR_PPC8548);
3264 if (!global_options_set.x_rs6000_spe_abi)
3265 rs6000_spe_abi = spe_capable_cpu;
3267 if (!global_options_set.x_rs6000_spe)
3268 rs6000_spe = spe_capable_cpu;
3270 if (!global_options_set.x_rs6000_float_gprs)
3271 rs6000_float_gprs =
3272 (rs6000_cpu == PROCESSOR_PPC8540 ? 1
3273 : rs6000_cpu == PROCESSOR_PPC8548 ? 2
3274 : 0);
3277 if (global_options_set.x_rs6000_spe_abi
3278 && rs6000_spe_abi
3279 && !TARGET_SPE_ABI)
3280 error ("not configured for SPE ABI");
3282 if (global_options_set.x_rs6000_spe
3283 && rs6000_spe
3284 && !TARGET_SPE)
3285 error ("not configured for SPE instruction set");
3287 if (main_target_opt != NULL
3288 && ((main_target_opt->x_rs6000_spe_abi != rs6000_spe_abi)
3289 || (main_target_opt->x_rs6000_spe != rs6000_spe)
3290 || (main_target_opt->x_rs6000_float_gprs != rs6000_float_gprs)))
3291 error ("target attribute or pragma changes SPE ABI");
3293 if (rs6000_cpu == PROCESSOR_PPCE300C2 || rs6000_cpu == PROCESSOR_PPCE300C3
3294 || rs6000_cpu == PROCESSOR_PPCE500MC || rs6000_cpu == PROCESSOR_PPCE500MC64
3295 || rs6000_cpu == PROCESSOR_PPCE5500)
3297 if (TARGET_ALTIVEC)
3298 error ("AltiVec not supported in this target");
3299 if (TARGET_SPE)
3300 error ("SPE not supported in this target");
3302 if (rs6000_cpu == PROCESSOR_PPCE6500)
3304 if (TARGET_SPE)
3305 error ("SPE not supported in this target");
3308 /* Disable Cell microcode if we are optimizing for the Cell
3309 and not optimizing for size. */
3310 if (rs6000_gen_cell_microcode == -1)
3311 rs6000_gen_cell_microcode = !(rs6000_cpu == PROCESSOR_CELL
3312 && !optimize_size);
3314 /* If we are optimizing big endian systems for space and it's OK to
3315 use instructions that would be microcoded on the Cell, use the
3316 load/store multiple and string instructions. */
3317 if (BYTES_BIG_ENDIAN && optimize_size && rs6000_gen_cell_microcode)
3318 rs6000_isa_flags |= ~rs6000_isa_flags_explicit & (OPTION_MASK_MULTIPLE
3319 | OPTION_MASK_STRING);
3321 /* Don't allow -mmultiple or -mstring on little endian systems
3322 unless the cpu is a 750, because the hardware doesn't support the
3323 instructions used in little endian mode, and causes an alignment
3324 trap. The 750 does not cause an alignment trap (except when the
3325 target is unaligned). */
3327 if (!BYTES_BIG_ENDIAN && rs6000_cpu != PROCESSOR_PPC750)
3329 if (TARGET_MULTIPLE)
3331 rs6000_isa_flags &= ~OPTION_MASK_MULTIPLE;
3332 if ((rs6000_isa_flags_explicit & OPTION_MASK_MULTIPLE) != 0)
3333 warning (0, "-mmultiple is not supported on little endian systems");
3336 if (TARGET_STRING)
3338 rs6000_isa_flags &= ~OPTION_MASK_STRING;
3339 if ((rs6000_isa_flags_explicit & OPTION_MASK_STRING) != 0)
3340 warning (0, "-mstring is not supported on little endian systems");
3344 /* If little-endian, default to -mstrict-align on older processors.
3345 Testing for htm matches power8 and later. */
3346 if (!BYTES_BIG_ENDIAN
3347 && !(processor_target_table[tune_index].target_enable & OPTION_MASK_HTM))
3348 rs6000_isa_flags |= ~rs6000_isa_flags_explicit & OPTION_MASK_STRICT_ALIGN;
3350 /* -maltivec={le,be} implies -maltivec. */
3351 if (rs6000_altivec_element_order != 0)
3352 rs6000_isa_flags |= OPTION_MASK_ALTIVEC;
3354 /* Disallow -maltivec=le in big endian mode for now. This is not
3355 known to be useful for anyone. */
3356 if (BYTES_BIG_ENDIAN && rs6000_altivec_element_order == 1)
3358 warning (0, N_("-maltivec=le not allowed for big-endian targets"));
3359 rs6000_altivec_element_order = 0;
3362 /* Add some warnings for VSX. */
3363 if (TARGET_VSX)
3365 const char *msg = NULL;
3366 if (!TARGET_HARD_FLOAT || !TARGET_FPRS
3367 || !TARGET_SINGLE_FLOAT || !TARGET_DOUBLE_FLOAT)
3369 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX)
3370 msg = N_("-mvsx requires hardware floating point");
3371 else
3373 rs6000_isa_flags &= ~ OPTION_MASK_VSX;
3374 rs6000_isa_flags_explicit |= OPTION_MASK_VSX;
3377 else if (TARGET_PAIRED_FLOAT)
3378 msg = N_("-mvsx and -mpaired are incompatible");
3379 else if (TARGET_AVOID_XFORM > 0)
3380 msg = N_("-mvsx needs indexed addressing");
3381 else if (!TARGET_ALTIVEC && (rs6000_isa_flags_explicit
3382 & OPTION_MASK_ALTIVEC))
3384 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX)
3385 msg = N_("-mvsx and -mno-altivec are incompatible");
3386 else
3387 msg = N_("-mno-altivec disables vsx");
3390 if (msg)
3392 warning (0, msg);
3393 rs6000_isa_flags &= ~ OPTION_MASK_VSX;
3394 rs6000_isa_flags_explicit |= OPTION_MASK_VSX;
3398 /* If hard-float/altivec/vsx were explicitly turned off then don't allow
3399 the -mcpu setting to enable options that conflict. */
3400 if ((!TARGET_HARD_FLOAT || !TARGET_ALTIVEC || !TARGET_VSX)
3401 && (rs6000_isa_flags_explicit & (OPTION_MASK_SOFT_FLOAT
3402 | OPTION_MASK_ALTIVEC
3403 | OPTION_MASK_VSX)) != 0)
3404 rs6000_isa_flags &= ~((OPTION_MASK_P8_VECTOR | OPTION_MASK_CRYPTO
3405 | OPTION_MASK_DIRECT_MOVE)
3406 & ~rs6000_isa_flags_explicit);
3408 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
3409 rs6000_print_isa_options (stderr, 0, "before defaults", rs6000_isa_flags);
3411 /* For the newer switches (vsx, dfp, etc.) set some of the older options,
3412 unless the user explicitly used the -mno-<option> to disable the code. */
3413 if (TARGET_P8_VECTOR || TARGET_DIRECT_MOVE || TARGET_CRYPTO)
3414 rs6000_isa_flags |= (ISA_2_7_MASKS_SERVER & ~rs6000_isa_flags_explicit);
3415 else if (TARGET_VSX)
3416 rs6000_isa_flags |= (ISA_2_6_MASKS_SERVER & ~rs6000_isa_flags_explicit);
3417 else if (TARGET_POPCNTD)
3418 rs6000_isa_flags |= (ISA_2_6_MASKS_EMBEDDED & ~rs6000_isa_flags_explicit);
3419 else if (TARGET_DFP)
3420 rs6000_isa_flags |= (ISA_2_5_MASKS_SERVER & ~rs6000_isa_flags_explicit);
3421 else if (TARGET_CMPB)
3422 rs6000_isa_flags |= (ISA_2_5_MASKS_EMBEDDED & ~rs6000_isa_flags_explicit);
3423 else if (TARGET_FPRND)
3424 rs6000_isa_flags |= (ISA_2_4_MASKS & ~rs6000_isa_flags_explicit);
3425 else if (TARGET_POPCNTB)
3426 rs6000_isa_flags |= (ISA_2_2_MASKS & ~rs6000_isa_flags_explicit);
3427 else if (TARGET_ALTIVEC)
3428 rs6000_isa_flags |= (OPTION_MASK_PPC_GFXOPT & ~rs6000_isa_flags_explicit);
3430 if (TARGET_CRYPTO && !TARGET_ALTIVEC)
3432 if (rs6000_isa_flags_explicit & OPTION_MASK_CRYPTO)
3433 error ("-mcrypto requires -maltivec");
3434 rs6000_isa_flags &= ~OPTION_MASK_CRYPTO;
3437 if (TARGET_DIRECT_MOVE && !TARGET_VSX)
3439 if (rs6000_isa_flags_explicit & OPTION_MASK_DIRECT_MOVE)
3440 error ("-mdirect-move requires -mvsx");
3441 rs6000_isa_flags &= ~OPTION_MASK_DIRECT_MOVE;
3444 if (TARGET_P8_VECTOR && !TARGET_ALTIVEC)
3446 if (rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR)
3447 error ("-mpower8-vector requires -maltivec");
3448 rs6000_isa_flags &= ~OPTION_MASK_P8_VECTOR;
3451 if (TARGET_P8_VECTOR && !TARGET_VSX)
3453 if (rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR)
3454 error ("-mpower8-vector requires -mvsx");
3455 rs6000_isa_flags &= ~OPTION_MASK_P8_VECTOR;
3458 if (TARGET_VSX_TIMODE && !TARGET_VSX)
3460 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX_TIMODE)
3461 error ("-mvsx-timode requires -mvsx");
3462 rs6000_isa_flags &= ~OPTION_MASK_VSX_TIMODE;
3465 if (TARGET_DFP && !TARGET_HARD_FLOAT)
3467 if (rs6000_isa_flags_explicit & OPTION_MASK_DFP)
3468 error ("-mhard-dfp requires -mhard-float");
3469 rs6000_isa_flags &= ~OPTION_MASK_DFP;
3472 /* The quad memory instructions only works in 64-bit mode. In 32-bit mode,
3473 silently turn off quad memory mode. */
3474 if ((TARGET_QUAD_MEMORY || TARGET_QUAD_MEMORY_ATOMIC) && !TARGET_POWERPC64)
3476 if ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY) != 0)
3477 warning (0, N_("-mquad-memory requires 64-bit mode"));
3479 if ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY_ATOMIC) != 0)
3480 warning (0, N_("-mquad-memory-atomic requires 64-bit mode"));
3482 rs6000_isa_flags &= ~(OPTION_MASK_QUAD_MEMORY
3483 | OPTION_MASK_QUAD_MEMORY_ATOMIC);
3486 /* Non-atomic quad memory load/store are disabled for little endian, since
3487 the words are reversed, but atomic operations can still be done by
3488 swapping the words. */
3489 if (TARGET_QUAD_MEMORY && !WORDS_BIG_ENDIAN)
3491 if ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY) != 0)
3492 warning (0, N_("-mquad-memory is not available in little endian mode"));
3494 rs6000_isa_flags &= ~OPTION_MASK_QUAD_MEMORY;
3497 /* Assume if the user asked for normal quad memory instructions, they want
3498 the atomic versions as well, unless they explicity told us not to use quad
3499 word atomic instructions. */
3500 if (TARGET_QUAD_MEMORY
3501 && !TARGET_QUAD_MEMORY_ATOMIC
3502 && ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY_ATOMIC) == 0))
3503 rs6000_isa_flags |= OPTION_MASK_QUAD_MEMORY_ATOMIC;
3505 /* Enable power8 fusion if we are tuning for power8, even if we aren't
3506 generating power8 instructions. */
3507 if (!(rs6000_isa_flags_explicit & OPTION_MASK_P8_FUSION))
3508 rs6000_isa_flags |= (processor_target_table[tune_index].target_enable
3509 & OPTION_MASK_P8_FUSION);
3511 /* Power8 does not fuse sign extended loads with the addis. If we are
3512 optimizing at high levels for speed, convert a sign extended load into a
3513 zero extending load, and an explicit sign extension. */
3514 if (TARGET_P8_FUSION
3515 && !(rs6000_isa_flags_explicit & OPTION_MASK_P8_FUSION_SIGN)
3516 && optimize_function_for_speed_p (cfun)
3517 && optimize >= 3)
3518 rs6000_isa_flags |= OPTION_MASK_P8_FUSION_SIGN;
3520 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
3521 rs6000_print_isa_options (stderr, 0, "after defaults", rs6000_isa_flags);
3523 /* E500mc does "better" if we inline more aggressively. Respect the
3524 user's opinion, though. */
3525 if (rs6000_block_move_inline_limit == 0
3526 && (rs6000_cpu == PROCESSOR_PPCE500MC
3527 || rs6000_cpu == PROCESSOR_PPCE500MC64
3528 || rs6000_cpu == PROCESSOR_PPCE5500
3529 || rs6000_cpu == PROCESSOR_PPCE6500))
3530 rs6000_block_move_inline_limit = 128;
3532 /* store_one_arg depends on expand_block_move to handle at least the
3533 size of reg_parm_stack_space. */
3534 if (rs6000_block_move_inline_limit < (TARGET_POWERPC64 ? 64 : 32))
3535 rs6000_block_move_inline_limit = (TARGET_POWERPC64 ? 64 : 32);
3537 if (global_init_p)
3539 /* If the appropriate debug option is enabled, replace the target hooks
3540 with debug versions that call the real version and then prints
3541 debugging information. */
3542 if (TARGET_DEBUG_COST)
3544 targetm.rtx_costs = rs6000_debug_rtx_costs;
3545 targetm.address_cost = rs6000_debug_address_cost;
3546 targetm.sched.adjust_cost = rs6000_debug_adjust_cost;
3549 if (TARGET_DEBUG_ADDR)
3551 targetm.legitimate_address_p = rs6000_debug_legitimate_address_p;
3552 targetm.legitimize_address = rs6000_debug_legitimize_address;
3553 rs6000_secondary_reload_class_ptr
3554 = rs6000_debug_secondary_reload_class;
3555 rs6000_secondary_memory_needed_ptr
3556 = rs6000_debug_secondary_memory_needed;
3557 rs6000_cannot_change_mode_class_ptr
3558 = rs6000_debug_cannot_change_mode_class;
3559 rs6000_preferred_reload_class_ptr
3560 = rs6000_debug_preferred_reload_class;
3561 rs6000_legitimize_reload_address_ptr
3562 = rs6000_debug_legitimize_reload_address;
3563 rs6000_mode_dependent_address_ptr
3564 = rs6000_debug_mode_dependent_address;
3567 if (rs6000_veclibabi_name)
3569 if (strcmp (rs6000_veclibabi_name, "mass") == 0)
3570 rs6000_veclib_handler = rs6000_builtin_vectorized_libmass;
3571 else
3573 error ("unknown vectorization library ABI type (%s) for "
3574 "-mveclibabi= switch", rs6000_veclibabi_name);
3575 ret = false;
3580 if (!global_options_set.x_rs6000_long_double_type_size)
3582 if (main_target_opt != NULL
3583 && (main_target_opt->x_rs6000_long_double_type_size
3584 != RS6000_DEFAULT_LONG_DOUBLE_SIZE))
3585 error ("target attribute or pragma changes long double size");
3586 else
3587 rs6000_long_double_type_size = RS6000_DEFAULT_LONG_DOUBLE_SIZE;
3590 #if !defined (POWERPC_LINUX) && !defined (POWERPC_FREEBSD)
3591 if (!global_options_set.x_rs6000_ieeequad)
3592 rs6000_ieeequad = 1;
3593 #endif
3595 /* Disable VSX and Altivec silently if the user switched cpus to power7 in a
3596 target attribute or pragma which automatically enables both options,
3597 unless the altivec ABI was set. This is set by default for 64-bit, but
3598 not for 32-bit. */
3599 if (main_target_opt != NULL && !main_target_opt->x_rs6000_altivec_abi)
3600 rs6000_isa_flags &= ~((OPTION_MASK_VSX | OPTION_MASK_ALTIVEC)
3601 & ~rs6000_isa_flags_explicit);
3603 /* Enable Altivec ABI for AIX -maltivec. */
3604 if (TARGET_XCOFF && (TARGET_ALTIVEC || TARGET_VSX))
3606 if (main_target_opt != NULL && !main_target_opt->x_rs6000_altivec_abi)
3607 error ("target attribute or pragma changes AltiVec ABI");
3608 else
3609 rs6000_altivec_abi = 1;
3612 /* The AltiVec ABI is the default for PowerPC-64 GNU/Linux. For
3613 PowerPC-32 GNU/Linux, -maltivec implies the AltiVec ABI. It can
3614 be explicitly overridden in either case. */
3615 if (TARGET_ELF)
3617 if (!global_options_set.x_rs6000_altivec_abi
3618 && (TARGET_64BIT || TARGET_ALTIVEC || TARGET_VSX))
3620 if (main_target_opt != NULL &&
3621 !main_target_opt->x_rs6000_altivec_abi)
3622 error ("target attribute or pragma changes AltiVec ABI");
3623 else
3624 rs6000_altivec_abi = 1;
3628 /* Set the Darwin64 ABI as default for 64-bit Darwin.
3629 So far, the only darwin64 targets are also MACH-O. */
3630 if (TARGET_MACHO
3631 && DEFAULT_ABI == ABI_DARWIN
3632 && TARGET_64BIT)
3634 if (main_target_opt != NULL && !main_target_opt->x_rs6000_darwin64_abi)
3635 error ("target attribute or pragma changes darwin64 ABI");
3636 else
3638 rs6000_darwin64_abi = 1;
3639 /* Default to natural alignment, for better performance. */
3640 rs6000_alignment_flags = MASK_ALIGN_NATURAL;
3644 /* Place FP constants in the constant pool instead of TOC
3645 if section anchors enabled. */
3646 if (flag_section_anchors
3647 && !global_options_set.x_TARGET_NO_FP_IN_TOC)
3648 TARGET_NO_FP_IN_TOC = 1;
3650 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
3651 rs6000_print_isa_options (stderr, 0, "before subtarget", rs6000_isa_flags);
3653 #ifdef SUBTARGET_OVERRIDE_OPTIONS
3654 SUBTARGET_OVERRIDE_OPTIONS;
3655 #endif
3656 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
3657 SUBSUBTARGET_OVERRIDE_OPTIONS;
3658 #endif
3659 #ifdef SUB3TARGET_OVERRIDE_OPTIONS
3660 SUB3TARGET_OVERRIDE_OPTIONS;
3661 #endif
3663 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
3664 rs6000_print_isa_options (stderr, 0, "after subtarget", rs6000_isa_flags);
3666 /* For the E500 family of cores, reset the single/double FP flags to let us
3667 check that they remain constant across attributes or pragmas. Also,
3668 clear a possible request for string instructions, not supported and which
3669 we might have silently queried above for -Os.
3671 For other families, clear ISEL in case it was set implicitly.
3674 switch (rs6000_cpu)
3676 case PROCESSOR_PPC8540:
3677 case PROCESSOR_PPC8548:
3678 case PROCESSOR_PPCE500MC:
3679 case PROCESSOR_PPCE500MC64:
3680 case PROCESSOR_PPCE5500:
3681 case PROCESSOR_PPCE6500:
3683 rs6000_single_float = TARGET_E500_SINGLE || TARGET_E500_DOUBLE;
3684 rs6000_double_float = TARGET_E500_DOUBLE;
3686 rs6000_isa_flags &= ~OPTION_MASK_STRING;
3688 break;
3690 default:
3692 if (have_cpu && !(rs6000_isa_flags_explicit & OPTION_MASK_ISEL))
3693 rs6000_isa_flags &= ~OPTION_MASK_ISEL;
3695 break;
3698 if (main_target_opt)
3700 if (main_target_opt->x_rs6000_single_float != rs6000_single_float)
3701 error ("target attribute or pragma changes single precision floating "
3702 "point");
3703 if (main_target_opt->x_rs6000_double_float != rs6000_double_float)
3704 error ("target attribute or pragma changes double precision floating "
3705 "point");
3708 /* Detect invalid option combinations with E500. */
3709 CHECK_E500_OPTIONS;
3711 rs6000_always_hint = (rs6000_cpu != PROCESSOR_POWER4
3712 && rs6000_cpu != PROCESSOR_POWER5
3713 && rs6000_cpu != PROCESSOR_POWER6
3714 && rs6000_cpu != PROCESSOR_POWER7
3715 && rs6000_cpu != PROCESSOR_POWER8
3716 && rs6000_cpu != PROCESSOR_PPCA2
3717 && rs6000_cpu != PROCESSOR_CELL
3718 && rs6000_cpu != PROCESSOR_PPC476);
3719 rs6000_sched_groups = (rs6000_cpu == PROCESSOR_POWER4
3720 || rs6000_cpu == PROCESSOR_POWER5
3721 || rs6000_cpu == PROCESSOR_POWER7
3722 || rs6000_cpu == PROCESSOR_POWER8);
3723 rs6000_align_branch_targets = (rs6000_cpu == PROCESSOR_POWER4
3724 || rs6000_cpu == PROCESSOR_POWER5
3725 || rs6000_cpu == PROCESSOR_POWER6
3726 || rs6000_cpu == PROCESSOR_POWER7
3727 || rs6000_cpu == PROCESSOR_POWER8
3728 || rs6000_cpu == PROCESSOR_PPCE500MC
3729 || rs6000_cpu == PROCESSOR_PPCE500MC64
3730 || rs6000_cpu == PROCESSOR_PPCE5500
3731 || rs6000_cpu == PROCESSOR_PPCE6500);
3733 /* Allow debug switches to override the above settings. These are set to -1
3734 in rs6000.opt to indicate the user hasn't directly set the switch. */
3735 if (TARGET_ALWAYS_HINT >= 0)
3736 rs6000_always_hint = TARGET_ALWAYS_HINT;
3738 if (TARGET_SCHED_GROUPS >= 0)
3739 rs6000_sched_groups = TARGET_SCHED_GROUPS;
3741 if (TARGET_ALIGN_BRANCH_TARGETS >= 0)
3742 rs6000_align_branch_targets = TARGET_ALIGN_BRANCH_TARGETS;
3744 rs6000_sched_restricted_insns_priority
3745 = (rs6000_sched_groups ? 1 : 0);
3747 /* Handle -msched-costly-dep option. */
3748 rs6000_sched_costly_dep
3749 = (rs6000_sched_groups ? true_store_to_load_dep_costly : no_dep_costly);
3751 if (rs6000_sched_costly_dep_str)
3753 if (! strcmp (rs6000_sched_costly_dep_str, "no"))
3754 rs6000_sched_costly_dep = no_dep_costly;
3755 else if (! strcmp (rs6000_sched_costly_dep_str, "all"))
3756 rs6000_sched_costly_dep = all_deps_costly;
3757 else if (! strcmp (rs6000_sched_costly_dep_str, "true_store_to_load"))
3758 rs6000_sched_costly_dep = true_store_to_load_dep_costly;
3759 else if (! strcmp (rs6000_sched_costly_dep_str, "store_to_load"))
3760 rs6000_sched_costly_dep = store_to_load_dep_costly;
3761 else
3762 rs6000_sched_costly_dep = ((enum rs6000_dependence_cost)
3763 atoi (rs6000_sched_costly_dep_str));
3766 /* Handle -minsert-sched-nops option. */
3767 rs6000_sched_insert_nops
3768 = (rs6000_sched_groups ? sched_finish_regroup_exact : sched_finish_none);
3770 if (rs6000_sched_insert_nops_str)
3772 if (! strcmp (rs6000_sched_insert_nops_str, "no"))
3773 rs6000_sched_insert_nops = sched_finish_none;
3774 else if (! strcmp (rs6000_sched_insert_nops_str, "pad"))
3775 rs6000_sched_insert_nops = sched_finish_pad_groups;
3776 else if (! strcmp (rs6000_sched_insert_nops_str, "regroup_exact"))
3777 rs6000_sched_insert_nops = sched_finish_regroup_exact;
3778 else
3779 rs6000_sched_insert_nops = ((enum rs6000_nop_insertion)
3780 atoi (rs6000_sched_insert_nops_str));
3783 if (global_init_p)
3785 #ifdef TARGET_REGNAMES
3786 /* If the user desires alternate register names, copy in the
3787 alternate names now. */
3788 if (TARGET_REGNAMES)
3789 memcpy (rs6000_reg_names, alt_reg_names, sizeof (rs6000_reg_names));
3790 #endif
3792 /* Set aix_struct_return last, after the ABI is determined.
3793 If -maix-struct-return or -msvr4-struct-return was explicitly
3794 used, don't override with the ABI default. */
3795 if (!global_options_set.x_aix_struct_return)
3796 aix_struct_return = (DEFAULT_ABI != ABI_V4 || DRAFT_V4_STRUCT_RET);
3798 #if 0
3799 /* IBM XL compiler defaults to unsigned bitfields. */
3800 if (TARGET_XL_COMPAT)
3801 flag_signed_bitfields = 0;
3802 #endif
3804 if (TARGET_LONG_DOUBLE_128 && !TARGET_IEEEQUAD)
3805 REAL_MODE_FORMAT (TFmode) = &ibm_extended_format;
3807 if (TARGET_TOC)
3808 ASM_GENERATE_INTERNAL_LABEL (toc_label_name, "LCTOC", 1);
3810 /* We can only guarantee the availability of DI pseudo-ops when
3811 assembling for 64-bit targets. */
3812 if (!TARGET_64BIT)
3814 targetm.asm_out.aligned_op.di = NULL;
3815 targetm.asm_out.unaligned_op.di = NULL;
3819 /* Set branch target alignment, if not optimizing for size. */
3820 if (!optimize_size)
3822 /* Cell wants to be aligned 8byte for dual issue. Titan wants to be
3823 aligned 8byte to avoid misprediction by the branch predictor. */
3824 if (rs6000_cpu == PROCESSOR_TITAN
3825 || rs6000_cpu == PROCESSOR_CELL)
3827 if (align_functions <= 0)
3828 align_functions = 8;
3829 if (align_jumps <= 0)
3830 align_jumps = 8;
3831 if (align_loops <= 0)
3832 align_loops = 8;
3834 if (rs6000_align_branch_targets)
3836 if (align_functions <= 0)
3837 align_functions = 16;
3838 if (align_jumps <= 0)
3839 align_jumps = 16;
3840 if (align_loops <= 0)
3842 can_override_loop_align = 1;
3843 align_loops = 16;
3846 if (align_jumps_max_skip <= 0)
3847 align_jumps_max_skip = 15;
3848 if (align_loops_max_skip <= 0)
3849 align_loops_max_skip = 15;
3852 /* Arrange to save and restore machine status around nested functions. */
3853 init_machine_status = rs6000_init_machine_status;
3855 /* We should always be splitting complex arguments, but we can't break
3856 Linux and Darwin ABIs at the moment. For now, only AIX is fixed. */
3857 if (DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN)
3858 targetm.calls.split_complex_arg = NULL;
3861 /* Initialize rs6000_cost with the appropriate target costs. */
3862 if (optimize_size)
3863 rs6000_cost = TARGET_POWERPC64 ? &size64_cost : &size32_cost;
3864 else
3865 switch (rs6000_cpu)
3867 case PROCESSOR_RS64A:
3868 rs6000_cost = &rs64a_cost;
3869 break;
3871 case PROCESSOR_MPCCORE:
3872 rs6000_cost = &mpccore_cost;
3873 break;
3875 case PROCESSOR_PPC403:
3876 rs6000_cost = &ppc403_cost;
3877 break;
3879 case PROCESSOR_PPC405:
3880 rs6000_cost = &ppc405_cost;
3881 break;
3883 case PROCESSOR_PPC440:
3884 rs6000_cost = &ppc440_cost;
3885 break;
3887 case PROCESSOR_PPC476:
3888 rs6000_cost = &ppc476_cost;
3889 break;
3891 case PROCESSOR_PPC601:
3892 rs6000_cost = &ppc601_cost;
3893 break;
3895 case PROCESSOR_PPC603:
3896 rs6000_cost = &ppc603_cost;
3897 break;
3899 case PROCESSOR_PPC604:
3900 rs6000_cost = &ppc604_cost;
3901 break;
3903 case PROCESSOR_PPC604e:
3904 rs6000_cost = &ppc604e_cost;
3905 break;
3907 case PROCESSOR_PPC620:
3908 rs6000_cost = &ppc620_cost;
3909 break;
3911 case PROCESSOR_PPC630:
3912 rs6000_cost = &ppc630_cost;
3913 break;
3915 case PROCESSOR_CELL:
3916 rs6000_cost = &ppccell_cost;
3917 break;
3919 case PROCESSOR_PPC750:
3920 case PROCESSOR_PPC7400:
3921 rs6000_cost = &ppc750_cost;
3922 break;
3924 case PROCESSOR_PPC7450:
3925 rs6000_cost = &ppc7450_cost;
3926 break;
3928 case PROCESSOR_PPC8540:
3929 case PROCESSOR_PPC8548:
3930 rs6000_cost = &ppc8540_cost;
3931 break;
3933 case PROCESSOR_PPCE300C2:
3934 case PROCESSOR_PPCE300C3:
3935 rs6000_cost = &ppce300c2c3_cost;
3936 break;
3938 case PROCESSOR_PPCE500MC:
3939 rs6000_cost = &ppce500mc_cost;
3940 break;
3942 case PROCESSOR_PPCE500MC64:
3943 rs6000_cost = &ppce500mc64_cost;
3944 break;
3946 case PROCESSOR_PPCE5500:
3947 rs6000_cost = &ppce5500_cost;
3948 break;
3950 case PROCESSOR_PPCE6500:
3951 rs6000_cost = &ppce6500_cost;
3952 break;
3954 case PROCESSOR_TITAN:
3955 rs6000_cost = &titan_cost;
3956 break;
3958 case PROCESSOR_POWER4:
3959 case PROCESSOR_POWER5:
3960 rs6000_cost = &power4_cost;
3961 break;
3963 case PROCESSOR_POWER6:
3964 rs6000_cost = &power6_cost;
3965 break;
3967 case PROCESSOR_POWER7:
3968 rs6000_cost = &power7_cost;
3969 break;
3971 case PROCESSOR_POWER8:
3972 rs6000_cost = &power8_cost;
3973 break;
3975 case PROCESSOR_PPCA2:
3976 rs6000_cost = &ppca2_cost;
3977 break;
3979 default:
3980 gcc_unreachable ();
3983 if (global_init_p)
3985 maybe_set_param_value (PARAM_SIMULTANEOUS_PREFETCHES,
3986 rs6000_cost->simultaneous_prefetches,
3987 global_options.x_param_values,
3988 global_options_set.x_param_values);
3989 maybe_set_param_value (PARAM_L1_CACHE_SIZE, rs6000_cost->l1_cache_size,
3990 global_options.x_param_values,
3991 global_options_set.x_param_values);
3992 maybe_set_param_value (PARAM_L1_CACHE_LINE_SIZE,
3993 rs6000_cost->cache_line_size,
3994 global_options.x_param_values,
3995 global_options_set.x_param_values);
3996 maybe_set_param_value (PARAM_L2_CACHE_SIZE, rs6000_cost->l2_cache_size,
3997 global_options.x_param_values,
3998 global_options_set.x_param_values);
4000 /* Increase loop peeling limits based on performance analysis. */
4001 maybe_set_param_value (PARAM_MAX_PEELED_INSNS, 400,
4002 global_options.x_param_values,
4003 global_options_set.x_param_values);
4004 maybe_set_param_value (PARAM_MAX_COMPLETELY_PEELED_INSNS, 400,
4005 global_options.x_param_values,
4006 global_options_set.x_param_values);
4008 /* If using typedef char *va_list, signal that
4009 __builtin_va_start (&ap, 0) can be optimized to
4010 ap = __builtin_next_arg (0). */
4011 if (DEFAULT_ABI != ABI_V4)
4012 targetm.expand_builtin_va_start = NULL;
4015 /* Set up single/double float flags.
4016 If TARGET_HARD_FLOAT is set, but neither single or double is set,
4017 then set both flags. */
4018 if (TARGET_HARD_FLOAT && TARGET_FPRS
4019 && rs6000_single_float == 0 && rs6000_double_float == 0)
4020 rs6000_single_float = rs6000_double_float = 1;
4022 /* If not explicitly specified via option, decide whether to generate indexed
4023 load/store instructions. */
4024 if (TARGET_AVOID_XFORM == -1)
4025 /* Avoid indexed addressing when targeting Power6 in order to avoid the
4026 DERAT mispredict penalty. However the LVE and STVE altivec instructions
4027 need indexed accesses and the type used is the scalar type of the element
4028 being loaded or stored. */
4029 TARGET_AVOID_XFORM = (rs6000_cpu == PROCESSOR_POWER6 && TARGET_CMPB
4030 && !TARGET_ALTIVEC);
4032 /* Set the -mrecip options. */
4033 if (rs6000_recip_name)
4035 char *p = ASTRDUP (rs6000_recip_name);
4036 char *q;
4037 unsigned int mask, i;
4038 bool invert;
4040 while ((q = strtok (p, ",")) != NULL)
4042 p = NULL;
4043 if (*q == '!')
4045 invert = true;
4046 q++;
4048 else
4049 invert = false;
4051 if (!strcmp (q, "default"))
4052 mask = ((TARGET_RECIP_PRECISION)
4053 ? RECIP_HIGH_PRECISION : RECIP_LOW_PRECISION);
4054 else
4056 for (i = 0; i < ARRAY_SIZE (recip_options); i++)
4057 if (!strcmp (q, recip_options[i].string))
4059 mask = recip_options[i].mask;
4060 break;
4063 if (i == ARRAY_SIZE (recip_options))
4065 error ("unknown option for -mrecip=%s", q);
4066 invert = false;
4067 mask = 0;
4068 ret = false;
4072 if (invert)
4073 rs6000_recip_control &= ~mask;
4074 else
4075 rs6000_recip_control |= mask;
4079 /* Set the builtin mask of the various options used that could affect which
4080 builtins were used. In the past we used target_flags, but we've run out
4081 of bits, and some options like SPE and PAIRED are no longer in
4082 target_flags. */
4083 rs6000_builtin_mask = rs6000_builtin_mask_calculate ();
4084 if (TARGET_DEBUG_BUILTIN || TARGET_DEBUG_TARGET)
4086 fprintf (stderr,
4087 "new builtin mask = " HOST_WIDE_INT_PRINT_HEX ", ",
4088 rs6000_builtin_mask);
4089 rs6000_print_builtin_options (stderr, 0, NULL, rs6000_builtin_mask);
4092 /* Initialize all of the registers. */
4093 rs6000_init_hard_regno_mode_ok (global_init_p);
4095 /* Save the initial options in case the user does function specific options */
4096 if (global_init_p)
4097 target_option_default_node = target_option_current_node
4098 = build_target_option_node (&global_options);
4100 /* If not explicitly specified via option, decide whether to generate the
4101 extra blr's required to preserve the link stack on some cpus (eg, 476). */
4102 if (TARGET_LINK_STACK == -1)
4103 SET_TARGET_LINK_STACK (rs6000_cpu == PROCESSOR_PPC476 && flag_pic);
4105 return ret;
4108 /* Implement TARGET_OPTION_OVERRIDE. On the RS/6000 this is used to
4109 define the target cpu type. */
4111 static void
4112 rs6000_option_override (void)
4114 (void) rs6000_option_override_internal (true);
4116 /* Register machine-specific passes. This needs to be done at start-up.
4117 It's convenient to do it here (like i386 does). */
4118 opt_pass *pass_analyze_swaps = make_pass_analyze_swaps (g);
4120 static struct register_pass_info analyze_swaps_info
4121 = { pass_analyze_swaps, "cse1", 1, PASS_POS_INSERT_BEFORE };
4123 register_pass (&analyze_swaps_info);
4127 /* Implement targetm.vectorize.builtin_mask_for_load. */
4128 static tree
4129 rs6000_builtin_mask_for_load (void)
4131 if (TARGET_ALTIVEC || TARGET_VSX)
4132 return altivec_builtin_mask_for_load;
4133 else
4134 return 0;
4137 /* Implement LOOP_ALIGN. */
4139 rs6000_loop_align (rtx label)
4141 basic_block bb;
4142 int ninsns;
4144 /* Don't override loop alignment if -falign-loops was specified. */
4145 if (!can_override_loop_align)
4146 return align_loops_log;
4148 bb = BLOCK_FOR_INSN (label);
4149 ninsns = num_loop_insns(bb->loop_father);
4151 /* Align small loops to 32 bytes to fit in an icache sector, otherwise return default. */
4152 if (ninsns > 4 && ninsns <= 8
4153 && (rs6000_cpu == PROCESSOR_POWER4
4154 || rs6000_cpu == PROCESSOR_POWER5
4155 || rs6000_cpu == PROCESSOR_POWER6
4156 || rs6000_cpu == PROCESSOR_POWER7
4157 || rs6000_cpu == PROCESSOR_POWER8))
4158 return 5;
4159 else
4160 return align_loops_log;
4163 /* Implement TARGET_LOOP_ALIGN_MAX_SKIP. */
4164 static int
4165 rs6000_loop_align_max_skip (rtx_insn *label)
4167 return (1 << rs6000_loop_align (label)) - 1;
4170 /* Return true iff, data reference of TYPE can reach vector alignment (16)
4171 after applying N number of iterations. This routine does not determine
4172 how may iterations are required to reach desired alignment. */
4174 static bool
4175 rs6000_vector_alignment_reachable (const_tree type ATTRIBUTE_UNUSED, bool is_packed)
4177 if (is_packed)
4178 return false;
4180 if (TARGET_32BIT)
4182 if (rs6000_alignment_flags == MASK_ALIGN_NATURAL)
4183 return true;
4185 if (rs6000_alignment_flags == MASK_ALIGN_POWER)
4186 return true;
4188 return false;
4190 else
4192 if (TARGET_MACHO)
4193 return false;
4195 /* Assuming that all other types are naturally aligned. CHECKME! */
4196 return true;
4200 /* Return true if the vector misalignment factor is supported by the
4201 target. */
4202 static bool
4203 rs6000_builtin_support_vector_misalignment (enum machine_mode mode,
4204 const_tree type,
4205 int misalignment,
4206 bool is_packed)
4208 if (TARGET_VSX)
4210 /* Return if movmisalign pattern is not supported for this mode. */
4211 if (optab_handler (movmisalign_optab, mode) == CODE_FOR_nothing)
4212 return false;
4214 if (misalignment == -1)
4216 /* Misalignment factor is unknown at compile time but we know
4217 it's word aligned. */
4218 if (rs6000_vector_alignment_reachable (type, is_packed))
4220 int element_size = TREE_INT_CST_LOW (TYPE_SIZE (type));
4222 if (element_size == 64 || element_size == 32)
4223 return true;
4226 return false;
4229 /* VSX supports word-aligned vector. */
4230 if (misalignment % 4 == 0)
4231 return true;
4233 return false;
4236 /* Implement targetm.vectorize.builtin_vectorization_cost. */
4237 static int
4238 rs6000_builtin_vectorization_cost (enum vect_cost_for_stmt type_of_cost,
4239 tree vectype, int misalign)
4241 unsigned elements;
4242 tree elem_type;
4244 switch (type_of_cost)
4246 case scalar_stmt:
4247 case scalar_load:
4248 case scalar_store:
4249 case vector_stmt:
4250 case vector_load:
4251 case vector_store:
4252 case vec_to_scalar:
4253 case scalar_to_vec:
4254 case cond_branch_not_taken:
4255 return 1;
4257 case vec_perm:
4258 if (TARGET_VSX)
4259 return 3;
4260 else
4261 return 1;
4263 case vec_promote_demote:
4264 if (TARGET_VSX)
4265 return 4;
4266 else
4267 return 1;
4269 case cond_branch_taken:
4270 return 3;
4272 case unaligned_load:
4273 if (TARGET_VSX && TARGET_ALLOW_MOVMISALIGN)
4275 elements = TYPE_VECTOR_SUBPARTS (vectype);
4276 if (elements == 2)
4277 /* Double word aligned. */
4278 return 2;
4280 if (elements == 4)
4282 switch (misalign)
4284 case 8:
4285 /* Double word aligned. */
4286 return 2;
4288 case -1:
4289 /* Unknown misalignment. */
4290 case 4:
4291 case 12:
4292 /* Word aligned. */
4293 return 22;
4295 default:
4296 gcc_unreachable ();
4301 if (TARGET_ALTIVEC)
4302 /* Misaligned loads are not supported. */
4303 gcc_unreachable ();
4305 return 2;
4307 case unaligned_store:
4308 if (TARGET_VSX && TARGET_ALLOW_MOVMISALIGN)
4310 elements = TYPE_VECTOR_SUBPARTS (vectype);
4311 if (elements == 2)
4312 /* Double word aligned. */
4313 return 2;
4315 if (elements == 4)
4317 switch (misalign)
4319 case 8:
4320 /* Double word aligned. */
4321 return 2;
4323 case -1:
4324 /* Unknown misalignment. */
4325 case 4:
4326 case 12:
4327 /* Word aligned. */
4328 return 23;
4330 default:
4331 gcc_unreachable ();
4336 if (TARGET_ALTIVEC)
4337 /* Misaligned stores are not supported. */
4338 gcc_unreachable ();
4340 return 2;
4342 case vec_construct:
4343 elements = TYPE_VECTOR_SUBPARTS (vectype);
4344 elem_type = TREE_TYPE (vectype);
4345 /* 32-bit vectors loaded into registers are stored as double
4346 precision, so we need n/2 converts in addition to the usual
4347 n/2 merges to construct a vector of short floats from them. */
4348 if (SCALAR_FLOAT_TYPE_P (elem_type)
4349 && TYPE_PRECISION (elem_type) == 32)
4350 return elements + 1;
4351 else
4352 return elements / 2 + 1;
4354 default:
4355 gcc_unreachable ();
4359 /* Implement targetm.vectorize.preferred_simd_mode. */
4361 static enum machine_mode
4362 rs6000_preferred_simd_mode (enum machine_mode mode)
4364 if (TARGET_VSX)
4365 switch (mode)
4367 case DFmode:
4368 return V2DFmode;
4369 default:;
4371 if (TARGET_ALTIVEC || TARGET_VSX)
4372 switch (mode)
4374 case SFmode:
4375 return V4SFmode;
4376 case TImode:
4377 return V1TImode;
4378 case DImode:
4379 return V2DImode;
4380 case SImode:
4381 return V4SImode;
4382 case HImode:
4383 return V8HImode;
4384 case QImode:
4385 return V16QImode;
4386 default:;
4388 if (TARGET_SPE)
4389 switch (mode)
4391 case SFmode:
4392 return V2SFmode;
4393 case SImode:
4394 return V2SImode;
4395 default:;
4397 if (TARGET_PAIRED_FLOAT
4398 && mode == SFmode)
4399 return V2SFmode;
4400 return word_mode;
4403 typedef struct _rs6000_cost_data
4405 struct loop *loop_info;
4406 unsigned cost[3];
4407 } rs6000_cost_data;
4409 /* Test for likely overcommitment of vector hardware resources. If a
4410 loop iteration is relatively large, and too large a percentage of
4411 instructions in the loop are vectorized, the cost model may not
4412 adequately reflect delays from unavailable vector resources.
4413 Penalize the loop body cost for this case. */
4415 static void
4416 rs6000_density_test (rs6000_cost_data *data)
4418 const int DENSITY_PCT_THRESHOLD = 85;
4419 const int DENSITY_SIZE_THRESHOLD = 70;
4420 const int DENSITY_PENALTY = 10;
4421 struct loop *loop = data->loop_info;
4422 basic_block *bbs = get_loop_body (loop);
4423 int nbbs = loop->num_nodes;
4424 int vec_cost = data->cost[vect_body], not_vec_cost = 0;
4425 int i, density_pct;
4427 for (i = 0; i < nbbs; i++)
4429 basic_block bb = bbs[i];
4430 gimple_stmt_iterator gsi;
4432 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4434 gimple stmt = gsi_stmt (gsi);
4435 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4437 if (!STMT_VINFO_RELEVANT_P (stmt_info)
4438 && !STMT_VINFO_IN_PATTERN_P (stmt_info))
4439 not_vec_cost++;
4443 free (bbs);
4444 density_pct = (vec_cost * 100) / (vec_cost + not_vec_cost);
4446 if (density_pct > DENSITY_PCT_THRESHOLD
4447 && vec_cost + not_vec_cost > DENSITY_SIZE_THRESHOLD)
4449 data->cost[vect_body] = vec_cost * (100 + DENSITY_PENALTY) / 100;
4450 if (dump_enabled_p ())
4451 dump_printf_loc (MSG_NOTE, vect_location,
4452 "density %d%%, cost %d exceeds threshold, penalizing "
4453 "loop body cost by %d%%", density_pct,
4454 vec_cost + not_vec_cost, DENSITY_PENALTY);
4458 /* Implement targetm.vectorize.init_cost. */
4460 static void *
4461 rs6000_init_cost (struct loop *loop_info)
4463 rs6000_cost_data *data = XNEW (struct _rs6000_cost_data);
4464 data->loop_info = loop_info;
4465 data->cost[vect_prologue] = 0;
4466 data->cost[vect_body] = 0;
4467 data->cost[vect_epilogue] = 0;
4468 return data;
4471 /* Implement targetm.vectorize.add_stmt_cost. */
4473 static unsigned
4474 rs6000_add_stmt_cost (void *data, int count, enum vect_cost_for_stmt kind,
4475 struct _stmt_vec_info *stmt_info, int misalign,
4476 enum vect_cost_model_location where)
4478 rs6000_cost_data *cost_data = (rs6000_cost_data*) data;
4479 unsigned retval = 0;
4481 if (flag_vect_cost_model)
4483 tree vectype = stmt_info ? stmt_vectype (stmt_info) : NULL_TREE;
4484 int stmt_cost = rs6000_builtin_vectorization_cost (kind, vectype,
4485 misalign);
4486 /* Statements in an inner loop relative to the loop being
4487 vectorized are weighted more heavily. The value here is
4488 arbitrary and could potentially be improved with analysis. */
4489 if (where == vect_body && stmt_info && stmt_in_inner_loop_p (stmt_info))
4490 count *= 50; /* FIXME. */
4492 retval = (unsigned) (count * stmt_cost);
4493 cost_data->cost[where] += retval;
4496 return retval;
4499 /* Implement targetm.vectorize.finish_cost. */
4501 static void
4502 rs6000_finish_cost (void *data, unsigned *prologue_cost,
4503 unsigned *body_cost, unsigned *epilogue_cost)
4505 rs6000_cost_data *cost_data = (rs6000_cost_data*) data;
4507 if (cost_data->loop_info)
4508 rs6000_density_test (cost_data);
4510 *prologue_cost = cost_data->cost[vect_prologue];
4511 *body_cost = cost_data->cost[vect_body];
4512 *epilogue_cost = cost_data->cost[vect_epilogue];
4515 /* Implement targetm.vectorize.destroy_cost_data. */
4517 static void
4518 rs6000_destroy_cost_data (void *data)
4520 free (data);
4523 /* Handler for the Mathematical Acceleration Subsystem (mass) interface to a
4524 library with vectorized intrinsics. */
4526 static tree
4527 rs6000_builtin_vectorized_libmass (tree fndecl, tree type_out, tree type_in)
4529 char name[32];
4530 const char *suffix = NULL;
4531 tree fntype, new_fndecl, bdecl = NULL_TREE;
4532 int n_args = 1;
4533 const char *bname;
4534 enum machine_mode el_mode, in_mode;
4535 int n, in_n;
4537 /* Libmass is suitable for unsafe math only as it does not correctly support
4538 parts of IEEE with the required precision such as denormals. Only support
4539 it if we have VSX to use the simd d2 or f4 functions.
4540 XXX: Add variable length support. */
4541 if (!flag_unsafe_math_optimizations || !TARGET_VSX)
4542 return NULL_TREE;
4544 el_mode = TYPE_MODE (TREE_TYPE (type_out));
4545 n = TYPE_VECTOR_SUBPARTS (type_out);
4546 in_mode = TYPE_MODE (TREE_TYPE (type_in));
4547 in_n = TYPE_VECTOR_SUBPARTS (type_in);
4548 if (el_mode != in_mode
4549 || n != in_n)
4550 return NULL_TREE;
4552 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
4554 enum built_in_function fn = DECL_FUNCTION_CODE (fndecl);
4555 switch (fn)
4557 case BUILT_IN_ATAN2:
4558 case BUILT_IN_HYPOT:
4559 case BUILT_IN_POW:
4560 n_args = 2;
4561 /* fall through */
4563 case BUILT_IN_ACOS:
4564 case BUILT_IN_ACOSH:
4565 case BUILT_IN_ASIN:
4566 case BUILT_IN_ASINH:
4567 case BUILT_IN_ATAN:
4568 case BUILT_IN_ATANH:
4569 case BUILT_IN_CBRT:
4570 case BUILT_IN_COS:
4571 case BUILT_IN_COSH:
4572 case BUILT_IN_ERF:
4573 case BUILT_IN_ERFC:
4574 case BUILT_IN_EXP2:
4575 case BUILT_IN_EXP:
4576 case BUILT_IN_EXPM1:
4577 case BUILT_IN_LGAMMA:
4578 case BUILT_IN_LOG10:
4579 case BUILT_IN_LOG1P:
4580 case BUILT_IN_LOG2:
4581 case BUILT_IN_LOG:
4582 case BUILT_IN_SIN:
4583 case BUILT_IN_SINH:
4584 case BUILT_IN_SQRT:
4585 case BUILT_IN_TAN:
4586 case BUILT_IN_TANH:
4587 bdecl = builtin_decl_implicit (fn);
4588 suffix = "d2"; /* pow -> powd2 */
4589 if (el_mode != DFmode
4590 || n != 2
4591 || !bdecl)
4592 return NULL_TREE;
4593 break;
4595 case BUILT_IN_ATAN2F:
4596 case BUILT_IN_HYPOTF:
4597 case BUILT_IN_POWF:
4598 n_args = 2;
4599 /* fall through */
4601 case BUILT_IN_ACOSF:
4602 case BUILT_IN_ACOSHF:
4603 case BUILT_IN_ASINF:
4604 case BUILT_IN_ASINHF:
4605 case BUILT_IN_ATANF:
4606 case BUILT_IN_ATANHF:
4607 case BUILT_IN_CBRTF:
4608 case BUILT_IN_COSF:
4609 case BUILT_IN_COSHF:
4610 case BUILT_IN_ERFF:
4611 case BUILT_IN_ERFCF:
4612 case BUILT_IN_EXP2F:
4613 case BUILT_IN_EXPF:
4614 case BUILT_IN_EXPM1F:
4615 case BUILT_IN_LGAMMAF:
4616 case BUILT_IN_LOG10F:
4617 case BUILT_IN_LOG1PF:
4618 case BUILT_IN_LOG2F:
4619 case BUILT_IN_LOGF:
4620 case BUILT_IN_SINF:
4621 case BUILT_IN_SINHF:
4622 case BUILT_IN_SQRTF:
4623 case BUILT_IN_TANF:
4624 case BUILT_IN_TANHF:
4625 bdecl = builtin_decl_implicit (fn);
4626 suffix = "4"; /* powf -> powf4 */
4627 if (el_mode != SFmode
4628 || n != 4
4629 || !bdecl)
4630 return NULL_TREE;
4631 break;
4633 default:
4634 return NULL_TREE;
4637 else
4638 return NULL_TREE;
4640 gcc_assert (suffix != NULL);
4641 bname = IDENTIFIER_POINTER (DECL_NAME (bdecl));
4642 if (!bname)
4643 return NULL_TREE;
4645 strcpy (name, bname + sizeof ("__builtin_") - 1);
4646 strcat (name, suffix);
4648 if (n_args == 1)
4649 fntype = build_function_type_list (type_out, type_in, NULL);
4650 else if (n_args == 2)
4651 fntype = build_function_type_list (type_out, type_in, type_in, NULL);
4652 else
4653 gcc_unreachable ();
4655 /* Build a function declaration for the vectorized function. */
4656 new_fndecl = build_decl (BUILTINS_LOCATION,
4657 FUNCTION_DECL, get_identifier (name), fntype);
4658 TREE_PUBLIC (new_fndecl) = 1;
4659 DECL_EXTERNAL (new_fndecl) = 1;
4660 DECL_IS_NOVOPS (new_fndecl) = 1;
4661 TREE_READONLY (new_fndecl) = 1;
4663 return new_fndecl;
4666 /* Returns a function decl for a vectorized version of the builtin function
4667 with builtin function code FN and the result vector type TYPE, or NULL_TREE
4668 if it is not available. */
4670 static tree
4671 rs6000_builtin_vectorized_function (tree fndecl, tree type_out,
4672 tree type_in)
4674 enum machine_mode in_mode, out_mode;
4675 int in_n, out_n;
4677 if (TARGET_DEBUG_BUILTIN)
4678 fprintf (stderr, "rs6000_builtin_vectorized_function (%s, %s, %s)\n",
4679 IDENTIFIER_POINTER (DECL_NAME (fndecl)),
4680 GET_MODE_NAME (TYPE_MODE (type_out)),
4681 GET_MODE_NAME (TYPE_MODE (type_in)));
4683 if (TREE_CODE (type_out) != VECTOR_TYPE
4684 || TREE_CODE (type_in) != VECTOR_TYPE
4685 || !TARGET_VECTORIZE_BUILTINS)
4686 return NULL_TREE;
4688 out_mode = TYPE_MODE (TREE_TYPE (type_out));
4689 out_n = TYPE_VECTOR_SUBPARTS (type_out);
4690 in_mode = TYPE_MODE (TREE_TYPE (type_in));
4691 in_n = TYPE_VECTOR_SUBPARTS (type_in);
4693 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
4695 enum built_in_function fn = DECL_FUNCTION_CODE (fndecl);
4696 switch (fn)
4698 case BUILT_IN_CLZIMAX:
4699 case BUILT_IN_CLZLL:
4700 case BUILT_IN_CLZL:
4701 case BUILT_IN_CLZ:
4702 if (TARGET_P8_VECTOR && in_mode == out_mode && out_n == in_n)
4704 if (out_mode == QImode && out_n == 16)
4705 return rs6000_builtin_decls[P8V_BUILTIN_VCLZB];
4706 else if (out_mode == HImode && out_n == 8)
4707 return rs6000_builtin_decls[P8V_BUILTIN_VCLZH];
4708 else if (out_mode == SImode && out_n == 4)
4709 return rs6000_builtin_decls[P8V_BUILTIN_VCLZW];
4710 else if (out_mode == DImode && out_n == 2)
4711 return rs6000_builtin_decls[P8V_BUILTIN_VCLZD];
4713 break;
4714 case BUILT_IN_COPYSIGN:
4715 if (VECTOR_UNIT_VSX_P (V2DFmode)
4716 && out_mode == DFmode && out_n == 2
4717 && in_mode == DFmode && in_n == 2)
4718 return rs6000_builtin_decls[VSX_BUILTIN_CPSGNDP];
4719 break;
4720 case BUILT_IN_COPYSIGNF:
4721 if (out_mode != SFmode || out_n != 4
4722 || in_mode != SFmode || in_n != 4)
4723 break;
4724 if (VECTOR_UNIT_VSX_P (V4SFmode))
4725 return rs6000_builtin_decls[VSX_BUILTIN_CPSGNSP];
4726 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
4727 return rs6000_builtin_decls[ALTIVEC_BUILTIN_COPYSIGN_V4SF];
4728 break;
4729 case BUILT_IN_POPCOUNTIMAX:
4730 case BUILT_IN_POPCOUNTLL:
4731 case BUILT_IN_POPCOUNTL:
4732 case BUILT_IN_POPCOUNT:
4733 if (TARGET_P8_VECTOR && in_mode == out_mode && out_n == in_n)
4735 if (out_mode == QImode && out_n == 16)
4736 return rs6000_builtin_decls[P8V_BUILTIN_VPOPCNTB];
4737 else if (out_mode == HImode && out_n == 8)
4738 return rs6000_builtin_decls[P8V_BUILTIN_VPOPCNTH];
4739 else if (out_mode == SImode && out_n == 4)
4740 return rs6000_builtin_decls[P8V_BUILTIN_VPOPCNTW];
4741 else if (out_mode == DImode && out_n == 2)
4742 return rs6000_builtin_decls[P8V_BUILTIN_VPOPCNTD];
4744 break;
4745 case BUILT_IN_SQRT:
4746 if (VECTOR_UNIT_VSX_P (V2DFmode)
4747 && out_mode == DFmode && out_n == 2
4748 && in_mode == DFmode && in_n == 2)
4749 return rs6000_builtin_decls[VSX_BUILTIN_XVSQRTDP];
4750 break;
4751 case BUILT_IN_SQRTF:
4752 if (VECTOR_UNIT_VSX_P (V4SFmode)
4753 && out_mode == SFmode && out_n == 4
4754 && in_mode == SFmode && in_n == 4)
4755 return rs6000_builtin_decls[VSX_BUILTIN_XVSQRTSP];
4756 break;
4757 case BUILT_IN_CEIL:
4758 if (VECTOR_UNIT_VSX_P (V2DFmode)
4759 && out_mode == DFmode && out_n == 2
4760 && in_mode == DFmode && in_n == 2)
4761 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIP];
4762 break;
4763 case BUILT_IN_CEILF:
4764 if (out_mode != SFmode || out_n != 4
4765 || in_mode != SFmode || in_n != 4)
4766 break;
4767 if (VECTOR_UNIT_VSX_P (V4SFmode))
4768 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIP];
4769 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
4770 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIP];
4771 break;
4772 case BUILT_IN_FLOOR:
4773 if (VECTOR_UNIT_VSX_P (V2DFmode)
4774 && out_mode == DFmode && out_n == 2
4775 && in_mode == DFmode && in_n == 2)
4776 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIM];
4777 break;
4778 case BUILT_IN_FLOORF:
4779 if (out_mode != SFmode || out_n != 4
4780 || in_mode != SFmode || in_n != 4)
4781 break;
4782 if (VECTOR_UNIT_VSX_P (V4SFmode))
4783 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIM];
4784 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
4785 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIM];
4786 break;
4787 case BUILT_IN_FMA:
4788 if (VECTOR_UNIT_VSX_P (V2DFmode)
4789 && out_mode == DFmode && out_n == 2
4790 && in_mode == DFmode && in_n == 2)
4791 return rs6000_builtin_decls[VSX_BUILTIN_XVMADDDP];
4792 break;
4793 case BUILT_IN_FMAF:
4794 if (VECTOR_UNIT_VSX_P (V4SFmode)
4795 && out_mode == SFmode && out_n == 4
4796 && in_mode == SFmode && in_n == 4)
4797 return rs6000_builtin_decls[VSX_BUILTIN_XVMADDSP];
4798 else if (VECTOR_UNIT_ALTIVEC_P (V4SFmode)
4799 && out_mode == SFmode && out_n == 4
4800 && in_mode == SFmode && in_n == 4)
4801 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VMADDFP];
4802 break;
4803 case BUILT_IN_TRUNC:
4804 if (VECTOR_UNIT_VSX_P (V2DFmode)
4805 && out_mode == DFmode && out_n == 2
4806 && in_mode == DFmode && in_n == 2)
4807 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIZ];
4808 break;
4809 case BUILT_IN_TRUNCF:
4810 if (out_mode != SFmode || out_n != 4
4811 || in_mode != SFmode || in_n != 4)
4812 break;
4813 if (VECTOR_UNIT_VSX_P (V4SFmode))
4814 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIZ];
4815 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
4816 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIZ];
4817 break;
4818 case BUILT_IN_NEARBYINT:
4819 if (VECTOR_UNIT_VSX_P (V2DFmode)
4820 && flag_unsafe_math_optimizations
4821 && out_mode == DFmode && out_n == 2
4822 && in_mode == DFmode && in_n == 2)
4823 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPI];
4824 break;
4825 case BUILT_IN_NEARBYINTF:
4826 if (VECTOR_UNIT_VSX_P (V4SFmode)
4827 && flag_unsafe_math_optimizations
4828 && out_mode == SFmode && out_n == 4
4829 && in_mode == SFmode && in_n == 4)
4830 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPI];
4831 break;
4832 case BUILT_IN_RINT:
4833 if (VECTOR_UNIT_VSX_P (V2DFmode)
4834 && !flag_trapping_math
4835 && out_mode == DFmode && out_n == 2
4836 && in_mode == DFmode && in_n == 2)
4837 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIC];
4838 break;
4839 case BUILT_IN_RINTF:
4840 if (VECTOR_UNIT_VSX_P (V4SFmode)
4841 && !flag_trapping_math
4842 && out_mode == SFmode && out_n == 4
4843 && in_mode == SFmode && in_n == 4)
4844 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIC];
4845 break;
4846 default:
4847 break;
4851 else if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
4853 enum rs6000_builtins fn
4854 = (enum rs6000_builtins)DECL_FUNCTION_CODE (fndecl);
4855 switch (fn)
4857 case RS6000_BUILTIN_RSQRTF:
4858 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode)
4859 && out_mode == SFmode && out_n == 4
4860 && in_mode == SFmode && in_n == 4)
4861 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRSQRTFP];
4862 break;
4863 case RS6000_BUILTIN_RSQRT:
4864 if (VECTOR_UNIT_VSX_P (V2DFmode)
4865 && out_mode == DFmode && out_n == 2
4866 && in_mode == DFmode && in_n == 2)
4867 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_2DF];
4868 break;
4869 case RS6000_BUILTIN_RECIPF:
4870 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode)
4871 && out_mode == SFmode && out_n == 4
4872 && in_mode == SFmode && in_n == 4)
4873 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRECIPFP];
4874 break;
4875 case RS6000_BUILTIN_RECIP:
4876 if (VECTOR_UNIT_VSX_P (V2DFmode)
4877 && out_mode == DFmode && out_n == 2
4878 && in_mode == DFmode && in_n == 2)
4879 return rs6000_builtin_decls[VSX_BUILTIN_RECIP_V2DF];
4880 break;
4881 default:
4882 break;
4886 /* Generate calls to libmass if appropriate. */
4887 if (rs6000_veclib_handler)
4888 return rs6000_veclib_handler (fndecl, type_out, type_in);
4890 return NULL_TREE;
4893 /* Default CPU string for rs6000*_file_start functions. */
4894 static const char *rs6000_default_cpu;
4896 /* Do anything needed at the start of the asm file. */
4898 static void
4899 rs6000_file_start (void)
4901 char buffer[80];
4902 const char *start = buffer;
4903 FILE *file = asm_out_file;
4905 rs6000_default_cpu = TARGET_CPU_DEFAULT;
4907 default_file_start ();
4909 if (flag_verbose_asm)
4911 sprintf (buffer, "\n%s rs6000/powerpc options:", ASM_COMMENT_START);
4913 if (rs6000_default_cpu != 0 && rs6000_default_cpu[0] != '\0')
4915 fprintf (file, "%s --with-cpu=%s", start, rs6000_default_cpu);
4916 start = "";
4919 if (global_options_set.x_rs6000_cpu_index)
4921 fprintf (file, "%s -mcpu=%s", start,
4922 processor_target_table[rs6000_cpu_index].name);
4923 start = "";
4926 if (global_options_set.x_rs6000_tune_index)
4928 fprintf (file, "%s -mtune=%s", start,
4929 processor_target_table[rs6000_tune_index].name);
4930 start = "";
4933 if (PPC405_ERRATUM77)
4935 fprintf (file, "%s PPC405CR_ERRATUM77", start);
4936 start = "";
4939 #ifdef USING_ELFOS_H
4940 switch (rs6000_sdata)
4942 case SDATA_NONE: fprintf (file, "%s -msdata=none", start); start = ""; break;
4943 case SDATA_DATA: fprintf (file, "%s -msdata=data", start); start = ""; break;
4944 case SDATA_SYSV: fprintf (file, "%s -msdata=sysv", start); start = ""; break;
4945 case SDATA_EABI: fprintf (file, "%s -msdata=eabi", start); start = ""; break;
4948 if (rs6000_sdata && g_switch_value)
4950 fprintf (file, "%s -G %d", start,
4951 g_switch_value);
4952 start = "";
4954 #endif
4956 if (*start == '\0')
4957 putc ('\n', file);
4960 if (DEFAULT_ABI == ABI_ELFv2)
4961 fprintf (file, "\t.abiversion 2\n");
4963 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2
4964 || (TARGET_ELF && flag_pic == 2))
4966 switch_to_section (toc_section);
4967 switch_to_section (text_section);
4972 /* Return nonzero if this function is known to have a null epilogue. */
4975 direct_return (void)
4977 if (reload_completed)
4979 rs6000_stack_t *info = rs6000_stack_info ();
4981 if (info->first_gp_reg_save == 32
4982 && info->first_fp_reg_save == 64
4983 && info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1
4984 && ! info->lr_save_p
4985 && ! info->cr_save_p
4986 && info->vrsave_mask == 0
4987 && ! info->push_p)
4988 return 1;
4991 return 0;
4994 /* Return the number of instructions it takes to form a constant in an
4995 integer register. */
4998 num_insns_constant_wide (HOST_WIDE_INT value)
5000 /* signed constant loadable with addi */
5001 if ((unsigned HOST_WIDE_INT) (value + 0x8000) < 0x10000)
5002 return 1;
5004 /* constant loadable with addis */
5005 else if ((value & 0xffff) == 0
5006 && (value >> 31 == -1 || value >> 31 == 0))
5007 return 1;
5009 else if (TARGET_POWERPC64)
5011 HOST_WIDE_INT low = ((value & 0xffffffff) ^ 0x80000000) - 0x80000000;
5012 HOST_WIDE_INT high = value >> 31;
5014 if (high == 0 || high == -1)
5015 return 2;
5017 high >>= 1;
5019 if (low == 0)
5020 return num_insns_constant_wide (high) + 1;
5021 else if (high == 0)
5022 return num_insns_constant_wide (low) + 1;
5023 else
5024 return (num_insns_constant_wide (high)
5025 + num_insns_constant_wide (low) + 1);
5028 else
5029 return 2;
5033 num_insns_constant (rtx op, enum machine_mode mode)
5035 HOST_WIDE_INT low, high;
5037 switch (GET_CODE (op))
5039 case CONST_INT:
5040 if ((INTVAL (op) >> 31) != 0 && (INTVAL (op) >> 31) != -1
5041 && mask64_operand (op, mode))
5042 return 2;
5043 else
5044 return num_insns_constant_wide (INTVAL (op));
5046 case CONST_WIDE_INT:
5048 int i;
5049 int ins = CONST_WIDE_INT_NUNITS (op) - 1;
5050 for (i = 0; i < CONST_WIDE_INT_NUNITS (op); i++)
5051 ins += num_insns_constant_wide (CONST_WIDE_INT_ELT (op, i));
5052 return ins;
5055 case CONST_DOUBLE:
5056 if (mode == SFmode || mode == SDmode)
5058 long l;
5059 REAL_VALUE_TYPE rv;
5061 REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
5062 if (DECIMAL_FLOAT_MODE_P (mode))
5063 REAL_VALUE_TO_TARGET_DECIMAL32 (rv, l);
5064 else
5065 REAL_VALUE_TO_TARGET_SINGLE (rv, l);
5066 return num_insns_constant_wide ((HOST_WIDE_INT) l);
5069 long l[2];
5070 REAL_VALUE_TYPE rv;
5072 REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
5073 if (DECIMAL_FLOAT_MODE_P (mode))
5074 REAL_VALUE_TO_TARGET_DECIMAL64 (rv, l);
5075 else
5076 REAL_VALUE_TO_TARGET_DOUBLE (rv, l);
5077 high = l[WORDS_BIG_ENDIAN == 0];
5078 low = l[WORDS_BIG_ENDIAN != 0];
5080 if (TARGET_32BIT)
5081 return (num_insns_constant_wide (low)
5082 + num_insns_constant_wide (high));
5083 else
5085 if ((high == 0 && low >= 0)
5086 || (high == -1 && low < 0))
5087 return num_insns_constant_wide (low);
5089 else if (mask64_operand (op, mode))
5090 return 2;
5092 else if (low == 0)
5093 return num_insns_constant_wide (high) + 1;
5095 else
5096 return (num_insns_constant_wide (high)
5097 + num_insns_constant_wide (low) + 1);
5100 default:
5101 gcc_unreachable ();
5105 /* Interpret element ELT of the CONST_VECTOR OP as an integer value.
5106 If the mode of OP is MODE_VECTOR_INT, this simply returns the
5107 corresponding element of the vector, but for V4SFmode and V2SFmode,
5108 the corresponding "float" is interpreted as an SImode integer. */
5110 HOST_WIDE_INT
5111 const_vector_elt_as_int (rtx op, unsigned int elt)
5113 rtx tmp;
5115 /* We can't handle V2DImode and V2DFmode vector constants here yet. */
5116 gcc_assert (GET_MODE (op) != V2DImode
5117 && GET_MODE (op) != V2DFmode);
5119 tmp = CONST_VECTOR_ELT (op, elt);
5120 if (GET_MODE (op) == V4SFmode
5121 || GET_MODE (op) == V2SFmode)
5122 tmp = gen_lowpart (SImode, tmp);
5123 return INTVAL (tmp);
5126 /* Return true if OP can be synthesized with a particular vspltisb, vspltish
5127 or vspltisw instruction. OP is a CONST_VECTOR. Which instruction is used
5128 depends on STEP and COPIES, one of which will be 1. If COPIES > 1,
5129 all items are set to the same value and contain COPIES replicas of the
5130 vsplt's operand; if STEP > 1, one in STEP elements is set to the vsplt's
5131 operand and the others are set to the value of the operand's msb. */
5133 static bool
5134 vspltis_constant (rtx op, unsigned step, unsigned copies)
5136 enum machine_mode mode = GET_MODE (op);
5137 enum machine_mode inner = GET_MODE_INNER (mode);
5139 unsigned i;
5140 unsigned nunits;
5141 unsigned bitsize;
5142 unsigned mask;
5144 HOST_WIDE_INT val;
5145 HOST_WIDE_INT splat_val;
5146 HOST_WIDE_INT msb_val;
5148 if (mode == V2DImode || mode == V2DFmode || mode == V1TImode)
5149 return false;
5151 nunits = GET_MODE_NUNITS (mode);
5152 bitsize = GET_MODE_BITSIZE (inner);
5153 mask = GET_MODE_MASK (inner);
5155 val = const_vector_elt_as_int (op, BYTES_BIG_ENDIAN ? nunits - 1 : 0);
5156 splat_val = val;
5157 msb_val = val >= 0 ? 0 : -1;
5159 /* Construct the value to be splatted, if possible. If not, return 0. */
5160 for (i = 2; i <= copies; i *= 2)
5162 HOST_WIDE_INT small_val;
5163 bitsize /= 2;
5164 small_val = splat_val >> bitsize;
5165 mask >>= bitsize;
5166 if (splat_val != ((small_val << bitsize) | (small_val & mask)))
5167 return false;
5168 splat_val = small_val;
5171 /* Check if SPLAT_VAL can really be the operand of a vspltis[bhw]. */
5172 if (EASY_VECTOR_15 (splat_val))
5175 /* Also check if we can splat, and then add the result to itself. Do so if
5176 the value is positive, of if the splat instruction is using OP's mode;
5177 for splat_val < 0, the splat and the add should use the same mode. */
5178 else if (EASY_VECTOR_15_ADD_SELF (splat_val)
5179 && (splat_val >= 0 || (step == 1 && copies == 1)))
5182 /* Also check if are loading up the most significant bit which can be done by
5183 loading up -1 and shifting the value left by -1. */
5184 else if (EASY_VECTOR_MSB (splat_val, inner))
5187 else
5188 return false;
5190 /* Check if VAL is present in every STEP-th element, and the
5191 other elements are filled with its most significant bit. */
5192 for (i = 1; i < nunits; ++i)
5194 HOST_WIDE_INT desired_val;
5195 unsigned elt = BYTES_BIG_ENDIAN ? nunits - 1 - i : i;
5196 if ((i & (step - 1)) == 0)
5197 desired_val = val;
5198 else
5199 desired_val = msb_val;
5201 if (desired_val != const_vector_elt_as_int (op, elt))
5202 return false;
5205 return true;
5209 /* Return true if OP is of the given MODE and can be synthesized
5210 with a vspltisb, vspltish or vspltisw. */
5212 bool
5213 easy_altivec_constant (rtx op, enum machine_mode mode)
5215 unsigned step, copies;
5217 if (mode == VOIDmode)
5218 mode = GET_MODE (op);
5219 else if (mode != GET_MODE (op))
5220 return false;
5222 /* V2DI/V2DF was added with VSX. Only allow 0 and all 1's as easy
5223 constants. */
5224 if (mode == V2DFmode)
5225 return zero_constant (op, mode);
5227 else if (mode == V2DImode)
5229 if (GET_CODE (CONST_VECTOR_ELT (op, 0)) != CONST_INT
5230 || GET_CODE (CONST_VECTOR_ELT (op, 1)) != CONST_INT)
5231 return false;
5233 if (zero_constant (op, mode))
5234 return true;
5236 if (INTVAL (CONST_VECTOR_ELT (op, 0)) == -1
5237 && INTVAL (CONST_VECTOR_ELT (op, 1)) == -1)
5238 return true;
5240 return false;
5243 /* V1TImode is a special container for TImode. Ignore for now. */
5244 else if (mode == V1TImode)
5245 return false;
5247 /* Start with a vspltisw. */
5248 step = GET_MODE_NUNITS (mode) / 4;
5249 copies = 1;
5251 if (vspltis_constant (op, step, copies))
5252 return true;
5254 /* Then try with a vspltish. */
5255 if (step == 1)
5256 copies <<= 1;
5257 else
5258 step >>= 1;
5260 if (vspltis_constant (op, step, copies))
5261 return true;
5263 /* And finally a vspltisb. */
5264 if (step == 1)
5265 copies <<= 1;
5266 else
5267 step >>= 1;
5269 if (vspltis_constant (op, step, copies))
5270 return true;
5272 return false;
5275 /* Generate a VEC_DUPLICATE representing a vspltis[bhw] instruction whose
5276 result is OP. Abort if it is not possible. */
5279 gen_easy_altivec_constant (rtx op)
5281 enum machine_mode mode = GET_MODE (op);
5282 int nunits = GET_MODE_NUNITS (mode);
5283 rtx val = CONST_VECTOR_ELT (op, BYTES_BIG_ENDIAN ? nunits - 1 : 0);
5284 unsigned step = nunits / 4;
5285 unsigned copies = 1;
5287 /* Start with a vspltisw. */
5288 if (vspltis_constant (op, step, copies))
5289 return gen_rtx_VEC_DUPLICATE (V4SImode, gen_lowpart (SImode, val));
5291 /* Then try with a vspltish. */
5292 if (step == 1)
5293 copies <<= 1;
5294 else
5295 step >>= 1;
5297 if (vspltis_constant (op, step, copies))
5298 return gen_rtx_VEC_DUPLICATE (V8HImode, gen_lowpart (HImode, val));
5300 /* And finally a vspltisb. */
5301 if (step == 1)
5302 copies <<= 1;
5303 else
5304 step >>= 1;
5306 if (vspltis_constant (op, step, copies))
5307 return gen_rtx_VEC_DUPLICATE (V16QImode, gen_lowpart (QImode, val));
5309 gcc_unreachable ();
5312 const char *
5313 output_vec_const_move (rtx *operands)
5315 int cst, cst2;
5316 enum machine_mode mode;
5317 rtx dest, vec;
5319 dest = operands[0];
5320 vec = operands[1];
5321 mode = GET_MODE (dest);
5323 if (TARGET_VSX)
5325 if (zero_constant (vec, mode))
5326 return "xxlxor %x0,%x0,%x0";
5328 if ((mode == V2DImode || mode == V1TImode)
5329 && INTVAL (CONST_VECTOR_ELT (vec, 0)) == -1
5330 && INTVAL (CONST_VECTOR_ELT (vec, 1)) == -1)
5331 return "vspltisw %0,-1";
5334 if (TARGET_ALTIVEC)
5336 rtx splat_vec;
5337 if (zero_constant (vec, mode))
5338 return "vxor %0,%0,%0";
5340 splat_vec = gen_easy_altivec_constant (vec);
5341 gcc_assert (GET_CODE (splat_vec) == VEC_DUPLICATE);
5342 operands[1] = XEXP (splat_vec, 0);
5343 if (!EASY_VECTOR_15 (INTVAL (operands[1])))
5344 return "#";
5346 switch (GET_MODE (splat_vec))
5348 case V4SImode:
5349 return "vspltisw %0,%1";
5351 case V8HImode:
5352 return "vspltish %0,%1";
5354 case V16QImode:
5355 return "vspltisb %0,%1";
5357 default:
5358 gcc_unreachable ();
5362 gcc_assert (TARGET_SPE);
5364 /* Vector constant 0 is handled as a splitter of V2SI, and in the
5365 pattern of V1DI, V4HI, and V2SF.
5367 FIXME: We should probably return # and add post reload
5368 splitters for these, but this way is so easy ;-). */
5369 cst = INTVAL (CONST_VECTOR_ELT (vec, 0));
5370 cst2 = INTVAL (CONST_VECTOR_ELT (vec, 1));
5371 operands[1] = CONST_VECTOR_ELT (vec, 0);
5372 operands[2] = CONST_VECTOR_ELT (vec, 1);
5373 if (cst == cst2)
5374 return "li %0,%1\n\tevmergelo %0,%0,%0";
5375 else if (WORDS_BIG_ENDIAN)
5376 return "li %0,%1\n\tevmergelo %0,%0,%0\n\tli %0,%2";
5377 else
5378 return "li %0,%2\n\tevmergelo %0,%0,%0\n\tli %0,%1";
5381 /* Initialize TARGET of vector PAIRED to VALS. */
5383 void
5384 paired_expand_vector_init (rtx target, rtx vals)
5386 enum machine_mode mode = GET_MODE (target);
5387 int n_elts = GET_MODE_NUNITS (mode);
5388 int n_var = 0;
5389 rtx x, new_rtx, tmp, constant_op, op1, op2;
5390 int i;
5392 for (i = 0; i < n_elts; ++i)
5394 x = XVECEXP (vals, 0, i);
5395 if (!(CONST_SCALAR_INT_P (x) || CONST_DOUBLE_P (x) || CONST_FIXED_P (x)))
5396 ++n_var;
5398 if (n_var == 0)
5400 /* Load from constant pool. */
5401 emit_move_insn (target, gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)));
5402 return;
5405 if (n_var == 2)
5407 /* The vector is initialized only with non-constants. */
5408 new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, XVECEXP (vals, 0, 0),
5409 XVECEXP (vals, 0, 1));
5411 emit_move_insn (target, new_rtx);
5412 return;
5415 /* One field is non-constant and the other one is a constant. Load the
5416 constant from the constant pool and use ps_merge instruction to
5417 construct the whole vector. */
5418 op1 = XVECEXP (vals, 0, 0);
5419 op2 = XVECEXP (vals, 0, 1);
5421 constant_op = (CONSTANT_P (op1)) ? op1 : op2;
5423 tmp = gen_reg_rtx (GET_MODE (constant_op));
5424 emit_move_insn (tmp, constant_op);
5426 if (CONSTANT_P (op1))
5427 new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, tmp, op2);
5428 else
5429 new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, op1, tmp);
5431 emit_move_insn (target, new_rtx);
5434 void
5435 paired_expand_vector_move (rtx operands[])
5437 rtx op0 = operands[0], op1 = operands[1];
5439 emit_move_insn (op0, op1);
5442 /* Emit vector compare for code RCODE. DEST is destination, OP1 and
5443 OP2 are two VEC_COND_EXPR operands, CC_OP0 and CC_OP1 are the two
5444 operands for the relation operation COND. This is a recursive
5445 function. */
5447 static void
5448 paired_emit_vector_compare (enum rtx_code rcode,
5449 rtx dest, rtx op0, rtx op1,
5450 rtx cc_op0, rtx cc_op1)
5452 rtx tmp = gen_reg_rtx (V2SFmode);
5453 rtx tmp1, max, min;
5455 gcc_assert (TARGET_PAIRED_FLOAT);
5456 gcc_assert (GET_MODE (op0) == GET_MODE (op1));
5458 switch (rcode)
5460 case LT:
5461 case LTU:
5462 paired_emit_vector_compare (GE, dest, op1, op0, cc_op0, cc_op1);
5463 return;
5464 case GE:
5465 case GEU:
5466 emit_insn (gen_subv2sf3 (tmp, cc_op0, cc_op1));
5467 emit_insn (gen_selv2sf4 (dest, tmp, op0, op1, CONST0_RTX (SFmode)));
5468 return;
5469 case LE:
5470 case LEU:
5471 paired_emit_vector_compare (GE, dest, op0, op1, cc_op1, cc_op0);
5472 return;
5473 case GT:
5474 paired_emit_vector_compare (LE, dest, op1, op0, cc_op0, cc_op1);
5475 return;
5476 case EQ:
5477 tmp1 = gen_reg_rtx (V2SFmode);
5478 max = gen_reg_rtx (V2SFmode);
5479 min = gen_reg_rtx (V2SFmode);
5480 gen_reg_rtx (V2SFmode);
5482 emit_insn (gen_subv2sf3 (tmp, cc_op0, cc_op1));
5483 emit_insn (gen_selv2sf4
5484 (max, tmp, cc_op0, cc_op1, CONST0_RTX (SFmode)));
5485 emit_insn (gen_subv2sf3 (tmp, cc_op1, cc_op0));
5486 emit_insn (gen_selv2sf4
5487 (min, tmp, cc_op0, cc_op1, CONST0_RTX (SFmode)));
5488 emit_insn (gen_subv2sf3 (tmp1, min, max));
5489 emit_insn (gen_selv2sf4 (dest, tmp1, op0, op1, CONST0_RTX (SFmode)));
5490 return;
5491 case NE:
5492 paired_emit_vector_compare (EQ, dest, op1, op0, cc_op0, cc_op1);
5493 return;
5494 case UNLE:
5495 paired_emit_vector_compare (LE, dest, op1, op0, cc_op0, cc_op1);
5496 return;
5497 case UNLT:
5498 paired_emit_vector_compare (LT, dest, op1, op0, cc_op0, cc_op1);
5499 return;
5500 case UNGE:
5501 paired_emit_vector_compare (GE, dest, op1, op0, cc_op0, cc_op1);
5502 return;
5503 case UNGT:
5504 paired_emit_vector_compare (GT, dest, op1, op0, cc_op0, cc_op1);
5505 return;
5506 default:
5507 gcc_unreachable ();
5510 return;
5513 /* Emit vector conditional expression.
5514 DEST is destination. OP1 and OP2 are two VEC_COND_EXPR operands.
5515 CC_OP0 and CC_OP1 are the two operands for the relation operation COND. */
5518 paired_emit_vector_cond_expr (rtx dest, rtx op1, rtx op2,
5519 rtx cond, rtx cc_op0, rtx cc_op1)
5521 enum rtx_code rcode = GET_CODE (cond);
5523 if (!TARGET_PAIRED_FLOAT)
5524 return 0;
5526 paired_emit_vector_compare (rcode, dest, op1, op2, cc_op0, cc_op1);
5528 return 1;
5531 /* Initialize vector TARGET to VALS. */
5533 void
5534 rs6000_expand_vector_init (rtx target, rtx vals)
5536 enum machine_mode mode = GET_MODE (target);
5537 enum machine_mode inner_mode = GET_MODE_INNER (mode);
5538 int n_elts = GET_MODE_NUNITS (mode);
5539 int n_var = 0, one_var = -1;
5540 bool all_same = true, all_const_zero = true;
5541 rtx x, mem;
5542 int i;
5544 for (i = 0; i < n_elts; ++i)
5546 x = XVECEXP (vals, 0, i);
5547 if (!(CONST_SCALAR_INT_P (x) || CONST_DOUBLE_P (x) || CONST_FIXED_P (x)))
5548 ++n_var, one_var = i;
5549 else if (x != CONST0_RTX (inner_mode))
5550 all_const_zero = false;
5552 if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
5553 all_same = false;
5556 if (n_var == 0)
5558 rtx const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0));
5559 bool int_vector_p = (GET_MODE_CLASS (mode) == MODE_VECTOR_INT);
5560 if ((int_vector_p || TARGET_VSX) && all_const_zero)
5562 /* Zero register. */
5563 emit_insn (gen_rtx_SET (VOIDmode, target,
5564 gen_rtx_XOR (mode, target, target)));
5565 return;
5567 else if (int_vector_p && easy_vector_constant (const_vec, mode))
5569 /* Splat immediate. */
5570 emit_insn (gen_rtx_SET (VOIDmode, target, const_vec));
5571 return;
5573 else
5575 /* Load from constant pool. */
5576 emit_move_insn (target, const_vec);
5577 return;
5581 /* Double word values on VSX can use xxpermdi or lxvdsx. */
5582 if (VECTOR_MEM_VSX_P (mode) && (mode == V2DFmode || mode == V2DImode))
5584 rtx op0 = XVECEXP (vals, 0, 0);
5585 rtx op1 = XVECEXP (vals, 0, 1);
5586 if (all_same)
5588 if (!MEM_P (op0) && !REG_P (op0))
5589 op0 = force_reg (inner_mode, op0);
5590 if (mode == V2DFmode)
5591 emit_insn (gen_vsx_splat_v2df (target, op0));
5592 else
5593 emit_insn (gen_vsx_splat_v2di (target, op0));
5595 else
5597 op0 = force_reg (inner_mode, op0);
5598 op1 = force_reg (inner_mode, op1);
5599 if (mode == V2DFmode)
5600 emit_insn (gen_vsx_concat_v2df (target, op0, op1));
5601 else
5602 emit_insn (gen_vsx_concat_v2di (target, op0, op1));
5604 return;
5607 /* With single precision floating point on VSX, know that internally single
5608 precision is actually represented as a double, and either make 2 V2DF
5609 vectors, and convert these vectors to single precision, or do one
5610 conversion, and splat the result to the other elements. */
5611 if (mode == V4SFmode && VECTOR_MEM_VSX_P (mode))
5613 if (all_same)
5615 rtx freg = gen_reg_rtx (V4SFmode);
5616 rtx sreg = force_reg (SFmode, XVECEXP (vals, 0, 0));
5617 rtx cvt = ((TARGET_XSCVDPSPN)
5618 ? gen_vsx_xscvdpspn_scalar (freg, sreg)
5619 : gen_vsx_xscvdpsp_scalar (freg, sreg));
5621 emit_insn (cvt);
5622 emit_insn (gen_vsx_xxspltw_v4sf_direct (target, freg, const0_rtx));
5624 else
5626 rtx dbl_even = gen_reg_rtx (V2DFmode);
5627 rtx dbl_odd = gen_reg_rtx (V2DFmode);
5628 rtx flt_even = gen_reg_rtx (V4SFmode);
5629 rtx flt_odd = gen_reg_rtx (V4SFmode);
5630 rtx op0 = force_reg (SFmode, XVECEXP (vals, 0, 0));
5631 rtx op1 = force_reg (SFmode, XVECEXP (vals, 0, 1));
5632 rtx op2 = force_reg (SFmode, XVECEXP (vals, 0, 2));
5633 rtx op3 = force_reg (SFmode, XVECEXP (vals, 0, 3));
5635 emit_insn (gen_vsx_concat_v2sf (dbl_even, op0, op1));
5636 emit_insn (gen_vsx_concat_v2sf (dbl_odd, op2, op3));
5637 emit_insn (gen_vsx_xvcvdpsp (flt_even, dbl_even));
5638 emit_insn (gen_vsx_xvcvdpsp (flt_odd, dbl_odd));
5639 rs6000_expand_extract_even (target, flt_even, flt_odd);
5641 return;
5644 /* Store value to stack temp. Load vector element. Splat. However, splat
5645 of 64-bit items is not supported on Altivec. */
5646 if (all_same && GET_MODE_SIZE (inner_mode) <= 4)
5648 mem = assign_stack_temp (mode, GET_MODE_SIZE (inner_mode));
5649 emit_move_insn (adjust_address_nv (mem, inner_mode, 0),
5650 XVECEXP (vals, 0, 0));
5651 x = gen_rtx_UNSPEC (VOIDmode,
5652 gen_rtvec (1, const0_rtx), UNSPEC_LVE);
5653 emit_insn (gen_rtx_PARALLEL (VOIDmode,
5654 gen_rtvec (2,
5655 gen_rtx_SET (VOIDmode,
5656 target, mem),
5657 x)));
5658 x = gen_rtx_VEC_SELECT (inner_mode, target,
5659 gen_rtx_PARALLEL (VOIDmode,
5660 gen_rtvec (1, const0_rtx)));
5661 emit_insn (gen_rtx_SET (VOIDmode, target,
5662 gen_rtx_VEC_DUPLICATE (mode, x)));
5663 return;
5666 /* One field is non-constant. Load constant then overwrite
5667 varying field. */
5668 if (n_var == 1)
5670 rtx copy = copy_rtx (vals);
5672 /* Load constant part of vector, substitute neighboring value for
5673 varying element. */
5674 XVECEXP (copy, 0, one_var) = XVECEXP (vals, 0, (one_var + 1) % n_elts);
5675 rs6000_expand_vector_init (target, copy);
5677 /* Insert variable. */
5678 rs6000_expand_vector_set (target, XVECEXP (vals, 0, one_var), one_var);
5679 return;
5682 /* Construct the vector in memory one field at a time
5683 and load the whole vector. */
5684 mem = assign_stack_temp (mode, GET_MODE_SIZE (mode));
5685 for (i = 0; i < n_elts; i++)
5686 emit_move_insn (adjust_address_nv (mem, inner_mode,
5687 i * GET_MODE_SIZE (inner_mode)),
5688 XVECEXP (vals, 0, i));
5689 emit_move_insn (target, mem);
5692 /* Set field ELT of TARGET to VAL. */
5694 void
5695 rs6000_expand_vector_set (rtx target, rtx val, int elt)
5697 enum machine_mode mode = GET_MODE (target);
5698 enum machine_mode inner_mode = GET_MODE_INNER (mode);
5699 rtx reg = gen_reg_rtx (mode);
5700 rtx mask, mem, x;
5701 int width = GET_MODE_SIZE (inner_mode);
5702 int i;
5704 if (VECTOR_MEM_VSX_P (mode) && (mode == V2DFmode || mode == V2DImode))
5706 rtx (*set_func) (rtx, rtx, rtx, rtx)
5707 = ((mode == V2DFmode) ? gen_vsx_set_v2df : gen_vsx_set_v2di);
5708 emit_insn (set_func (target, target, val, GEN_INT (elt)));
5709 return;
5712 /* Simplify setting single element vectors like V1TImode. */
5713 if (GET_MODE_SIZE (mode) == GET_MODE_SIZE (inner_mode) && elt == 0)
5715 emit_move_insn (target, gen_lowpart (mode, val));
5716 return;
5719 /* Load single variable value. */
5720 mem = assign_stack_temp (mode, GET_MODE_SIZE (inner_mode));
5721 emit_move_insn (adjust_address_nv (mem, inner_mode, 0), val);
5722 x = gen_rtx_UNSPEC (VOIDmode,
5723 gen_rtvec (1, const0_rtx), UNSPEC_LVE);
5724 emit_insn (gen_rtx_PARALLEL (VOIDmode,
5725 gen_rtvec (2,
5726 gen_rtx_SET (VOIDmode,
5727 reg, mem),
5728 x)));
5730 /* Linear sequence. */
5731 mask = gen_rtx_PARALLEL (V16QImode, rtvec_alloc (16));
5732 for (i = 0; i < 16; ++i)
5733 XVECEXP (mask, 0, i) = GEN_INT (i);
5735 /* Set permute mask to insert element into target. */
5736 for (i = 0; i < width; ++i)
5737 XVECEXP (mask, 0, elt*width + i)
5738 = GEN_INT (i + 0x10);
5739 x = gen_rtx_CONST_VECTOR (V16QImode, XVEC (mask, 0));
5741 if (BYTES_BIG_ENDIAN)
5742 x = gen_rtx_UNSPEC (mode,
5743 gen_rtvec (3, target, reg,
5744 force_reg (V16QImode, x)),
5745 UNSPEC_VPERM);
5746 else
5748 /* Invert selector. We prefer to generate VNAND on P8 so
5749 that future fusion opportunities can kick in, but must
5750 generate VNOR elsewhere. */
5751 rtx notx = gen_rtx_NOT (V16QImode, force_reg (V16QImode, x));
5752 rtx iorx = (TARGET_P8_VECTOR
5753 ? gen_rtx_IOR (V16QImode, notx, notx)
5754 : gen_rtx_AND (V16QImode, notx, notx));
5755 rtx tmp = gen_reg_rtx (V16QImode);
5756 emit_insn (gen_rtx_SET (VOIDmode, tmp, iorx));
5758 /* Permute with operands reversed and adjusted selector. */
5759 x = gen_rtx_UNSPEC (mode, gen_rtvec (3, reg, target, tmp),
5760 UNSPEC_VPERM);
5763 emit_insn (gen_rtx_SET (VOIDmode, target, x));
5766 /* Extract field ELT from VEC into TARGET. */
5768 void
5769 rs6000_expand_vector_extract (rtx target, rtx vec, int elt)
5771 enum machine_mode mode = GET_MODE (vec);
5772 enum machine_mode inner_mode = GET_MODE_INNER (mode);
5773 rtx mem;
5775 if (VECTOR_MEM_VSX_P (mode))
5777 switch (mode)
5779 default:
5780 break;
5781 case V1TImode:
5782 gcc_assert (elt == 0 && inner_mode == TImode);
5783 emit_move_insn (target, gen_lowpart (TImode, vec));
5784 break;
5785 case V2DFmode:
5786 emit_insn (gen_vsx_extract_v2df (target, vec, GEN_INT (elt)));
5787 return;
5788 case V2DImode:
5789 emit_insn (gen_vsx_extract_v2di (target, vec, GEN_INT (elt)));
5790 return;
5791 case V4SFmode:
5792 emit_insn (gen_vsx_extract_v4sf (target, vec, GEN_INT (elt)));
5793 return;
5797 /* Allocate mode-sized buffer. */
5798 mem = assign_stack_temp (mode, GET_MODE_SIZE (mode));
5800 emit_move_insn (mem, vec);
5802 /* Add offset to field within buffer matching vector element. */
5803 mem = adjust_address_nv (mem, inner_mode, elt * GET_MODE_SIZE (inner_mode));
5805 emit_move_insn (target, adjust_address_nv (mem, inner_mode, 0));
5808 /* Generates shifts and masks for a pair of rldicl or rldicr insns to
5809 implement ANDing by the mask IN. */
5810 void
5811 build_mask64_2_operands (rtx in, rtx *out)
5813 unsigned HOST_WIDE_INT c, lsb, m1, m2;
5814 int shift;
5816 gcc_assert (GET_CODE (in) == CONST_INT);
5818 c = INTVAL (in);
5819 if (c & 1)
5821 /* Assume c initially something like 0x00fff000000fffff. The idea
5822 is to rotate the word so that the middle ^^^^^^ group of zeros
5823 is at the MS end and can be cleared with an rldicl mask. We then
5824 rotate back and clear off the MS ^^ group of zeros with a
5825 second rldicl. */
5826 c = ~c; /* c == 0xff000ffffff00000 */
5827 lsb = c & -c; /* lsb == 0x0000000000100000 */
5828 m1 = -lsb; /* m1 == 0xfffffffffff00000 */
5829 c = ~c; /* c == 0x00fff000000fffff */
5830 c &= -lsb; /* c == 0x00fff00000000000 */
5831 lsb = c & -c; /* lsb == 0x0000100000000000 */
5832 c = ~c; /* c == 0xff000fffffffffff */
5833 c &= -lsb; /* c == 0xff00000000000000 */
5834 shift = 0;
5835 while ((lsb >>= 1) != 0)
5836 shift++; /* shift == 44 on exit from loop */
5837 m1 <<= 64 - shift; /* m1 == 0xffffff0000000000 */
5838 m1 = ~m1; /* m1 == 0x000000ffffffffff */
5839 m2 = ~c; /* m2 == 0x00ffffffffffffff */
5841 else
5843 /* Assume c initially something like 0xff000f0000000000. The idea
5844 is to rotate the word so that the ^^^ middle group of zeros
5845 is at the LS end and can be cleared with an rldicr mask. We then
5846 rotate back and clear off the LS group of ^^^^^^^^^^ zeros with
5847 a second rldicr. */
5848 lsb = c & -c; /* lsb == 0x0000010000000000 */
5849 m2 = -lsb; /* m2 == 0xffffff0000000000 */
5850 c = ~c; /* c == 0x00fff0ffffffffff */
5851 c &= -lsb; /* c == 0x00fff00000000000 */
5852 lsb = c & -c; /* lsb == 0x0000100000000000 */
5853 c = ~c; /* c == 0xff000fffffffffff */
5854 c &= -lsb; /* c == 0xff00000000000000 */
5855 shift = 0;
5856 while ((lsb >>= 1) != 0)
5857 shift++; /* shift == 44 on exit from loop */
5858 m1 = ~c; /* m1 == 0x00ffffffffffffff */
5859 m1 >>= shift; /* m1 == 0x0000000000000fff */
5860 m1 = ~m1; /* m1 == 0xfffffffffffff000 */
5863 /* Note that when we only have two 0->1 and 1->0 transitions, one of the
5864 masks will be all 1's. We are guaranteed more than one transition. */
5865 out[0] = GEN_INT (64 - shift);
5866 out[1] = GEN_INT (m1);
5867 out[2] = GEN_INT (shift);
5868 out[3] = GEN_INT (m2);
5871 /* Return TRUE if OP is an invalid SUBREG operation on the e500. */
5873 bool
5874 invalid_e500_subreg (rtx op, enum machine_mode mode)
5876 if (TARGET_E500_DOUBLE)
5878 /* Reject (subreg:SI (reg:DF)); likewise with subreg:DI or
5879 subreg:TI and reg:TF. Decimal float modes are like integer
5880 modes (only low part of each register used) for this
5881 purpose. */
5882 if (GET_CODE (op) == SUBREG
5883 && (mode == SImode || mode == DImode || mode == TImode
5884 || mode == DDmode || mode == TDmode || mode == PTImode)
5885 && REG_P (SUBREG_REG (op))
5886 && (GET_MODE (SUBREG_REG (op)) == DFmode
5887 || GET_MODE (SUBREG_REG (op)) == TFmode))
5888 return true;
5890 /* Reject (subreg:DF (reg:DI)); likewise with subreg:TF and
5891 reg:TI. */
5892 if (GET_CODE (op) == SUBREG
5893 && (mode == DFmode || mode == TFmode)
5894 && REG_P (SUBREG_REG (op))
5895 && (GET_MODE (SUBREG_REG (op)) == DImode
5896 || GET_MODE (SUBREG_REG (op)) == TImode
5897 || GET_MODE (SUBREG_REG (op)) == PTImode
5898 || GET_MODE (SUBREG_REG (op)) == DDmode
5899 || GET_MODE (SUBREG_REG (op)) == TDmode))
5900 return true;
5903 if (TARGET_SPE
5904 && GET_CODE (op) == SUBREG
5905 && mode == SImode
5906 && REG_P (SUBREG_REG (op))
5907 && SPE_VECTOR_MODE (GET_MODE (SUBREG_REG (op))))
5908 return true;
5910 return false;
5913 /* Return alignment of TYPE. Existing alignment is ALIGN. HOW
5914 selects whether the alignment is abi mandated, optional, or
5915 both abi and optional alignment. */
5917 unsigned int
5918 rs6000_data_alignment (tree type, unsigned int align, enum data_align how)
5920 if (how != align_opt)
5922 if (TREE_CODE (type) == VECTOR_TYPE)
5924 if ((TARGET_SPE && SPE_VECTOR_MODE (TYPE_MODE (type)))
5925 || (TARGET_PAIRED_FLOAT && PAIRED_VECTOR_MODE (TYPE_MODE (type))))
5927 if (align < 64)
5928 align = 64;
5930 else if (align < 128)
5931 align = 128;
5933 else if (TARGET_E500_DOUBLE
5934 && TREE_CODE (type) == REAL_TYPE
5935 && TYPE_MODE (type) == DFmode)
5937 if (align < 64)
5938 align = 64;
5942 if (how != align_abi)
5944 if (TREE_CODE (type) == ARRAY_TYPE
5945 && TYPE_MODE (TREE_TYPE (type)) == QImode)
5947 if (align < BITS_PER_WORD)
5948 align = BITS_PER_WORD;
5952 return align;
5955 /* Previous GCC releases forced all vector types to have 16-byte alignment. */
5957 bool
5958 rs6000_special_adjust_field_align_p (tree field, unsigned int computed)
5960 if (TARGET_ALTIVEC && TREE_CODE (TREE_TYPE (field)) == VECTOR_TYPE)
5962 if (computed != 128)
5964 static bool warned;
5965 if (!warned && warn_psabi)
5967 warned = true;
5968 inform (input_location,
5969 "the layout of aggregates containing vectors with"
5970 " %d-byte alignment has changed in GCC 5",
5971 computed / BITS_PER_UNIT);
5974 /* In current GCC there is no special case. */
5975 return false;
5978 return false;
5981 /* AIX increases natural record alignment to doubleword if the first
5982 field is an FP double while the FP fields remain word aligned. */
5984 unsigned int
5985 rs6000_special_round_type_align (tree type, unsigned int computed,
5986 unsigned int specified)
5988 unsigned int align = MAX (computed, specified);
5989 tree field = TYPE_FIELDS (type);
5991 /* Skip all non field decls */
5992 while (field != NULL && TREE_CODE (field) != FIELD_DECL)
5993 field = DECL_CHAIN (field);
5995 if (field != NULL && field != type)
5997 type = TREE_TYPE (field);
5998 while (TREE_CODE (type) == ARRAY_TYPE)
5999 type = TREE_TYPE (type);
6001 if (type != error_mark_node && TYPE_MODE (type) == DFmode)
6002 align = MAX (align, 64);
6005 return align;
6008 /* Darwin increases record alignment to the natural alignment of
6009 the first field. */
6011 unsigned int
6012 darwin_rs6000_special_round_type_align (tree type, unsigned int computed,
6013 unsigned int specified)
6015 unsigned int align = MAX (computed, specified);
6017 if (TYPE_PACKED (type))
6018 return align;
6020 /* Find the first field, looking down into aggregates. */
6021 do {
6022 tree field = TYPE_FIELDS (type);
6023 /* Skip all non field decls */
6024 while (field != NULL && TREE_CODE (field) != FIELD_DECL)
6025 field = DECL_CHAIN (field);
6026 if (! field)
6027 break;
6028 /* A packed field does not contribute any extra alignment. */
6029 if (DECL_PACKED (field))
6030 return align;
6031 type = TREE_TYPE (field);
6032 while (TREE_CODE (type) == ARRAY_TYPE)
6033 type = TREE_TYPE (type);
6034 } while (AGGREGATE_TYPE_P (type));
6036 if (! AGGREGATE_TYPE_P (type) && type != error_mark_node)
6037 align = MAX (align, TYPE_ALIGN (type));
6039 return align;
6042 /* Return 1 for an operand in small memory on V.4/eabi. */
6045 small_data_operand (rtx op ATTRIBUTE_UNUSED,
6046 enum machine_mode mode ATTRIBUTE_UNUSED)
6048 #if TARGET_ELF
6049 rtx sym_ref;
6051 if (rs6000_sdata == SDATA_NONE || rs6000_sdata == SDATA_DATA)
6052 return 0;
6054 if (DEFAULT_ABI != ABI_V4)
6055 return 0;
6057 /* Vector and float memory instructions have a limited offset on the
6058 SPE, so using a vector or float variable directly as an operand is
6059 not useful. */
6060 if (TARGET_SPE
6061 && (SPE_VECTOR_MODE (mode) || FLOAT_MODE_P (mode)))
6062 return 0;
6064 if (GET_CODE (op) == SYMBOL_REF)
6065 sym_ref = op;
6067 else if (GET_CODE (op) != CONST
6068 || GET_CODE (XEXP (op, 0)) != PLUS
6069 || GET_CODE (XEXP (XEXP (op, 0), 0)) != SYMBOL_REF
6070 || GET_CODE (XEXP (XEXP (op, 0), 1)) != CONST_INT)
6071 return 0;
6073 else
6075 rtx sum = XEXP (op, 0);
6076 HOST_WIDE_INT summand;
6078 /* We have to be careful here, because it is the referenced address
6079 that must be 32k from _SDA_BASE_, not just the symbol. */
6080 summand = INTVAL (XEXP (sum, 1));
6081 if (summand < 0 || summand > g_switch_value)
6082 return 0;
6084 sym_ref = XEXP (sum, 0);
6087 return SYMBOL_REF_SMALL_P (sym_ref);
6088 #else
6089 return 0;
6090 #endif
6093 /* Return true if either operand is a general purpose register. */
6095 bool
6096 gpr_or_gpr_p (rtx op0, rtx op1)
6098 return ((REG_P (op0) && INT_REGNO_P (REGNO (op0)))
6099 || (REG_P (op1) && INT_REGNO_P (REGNO (op1))));
6102 /* Return true if this is a move direct operation between GPR registers and
6103 floating point/VSX registers. */
6105 bool
6106 direct_move_p (rtx op0, rtx op1)
6108 int regno0, regno1;
6110 if (!REG_P (op0) || !REG_P (op1))
6111 return false;
6113 if (!TARGET_DIRECT_MOVE && !TARGET_MFPGPR)
6114 return false;
6116 regno0 = REGNO (op0);
6117 regno1 = REGNO (op1);
6118 if (regno0 >= FIRST_PSEUDO_REGISTER || regno1 >= FIRST_PSEUDO_REGISTER)
6119 return false;
6121 if (INT_REGNO_P (regno0))
6122 return (TARGET_DIRECT_MOVE) ? VSX_REGNO_P (regno1) : FP_REGNO_P (regno1);
6124 else if (INT_REGNO_P (regno1))
6126 if (TARGET_MFPGPR && FP_REGNO_P (regno0))
6127 return true;
6129 else if (TARGET_DIRECT_MOVE && VSX_REGNO_P (regno0))
6130 return true;
6133 return false;
6136 /* Return true if this is a load or store quad operation. This function does
6137 not handle the atomic quad memory instructions. */
6139 bool
6140 quad_load_store_p (rtx op0, rtx op1)
6142 bool ret;
6144 if (!TARGET_QUAD_MEMORY)
6145 ret = false;
6147 else if (REG_P (op0) && MEM_P (op1))
6148 ret = (quad_int_reg_operand (op0, GET_MODE (op0))
6149 && quad_memory_operand (op1, GET_MODE (op1))
6150 && !reg_overlap_mentioned_p (op0, op1));
6152 else if (MEM_P (op0) && REG_P (op1))
6153 ret = (quad_memory_operand (op0, GET_MODE (op0))
6154 && quad_int_reg_operand (op1, GET_MODE (op1)));
6156 else
6157 ret = false;
6159 if (TARGET_DEBUG_ADDR)
6161 fprintf (stderr, "\n========== quad_load_store, return %s\n",
6162 ret ? "true" : "false");
6163 debug_rtx (gen_rtx_SET (VOIDmode, op0, op1));
6166 return ret;
6169 /* Given an address, return a constant offset term if one exists. */
6171 static rtx
6172 address_offset (rtx op)
6174 if (GET_CODE (op) == PRE_INC
6175 || GET_CODE (op) == PRE_DEC)
6176 op = XEXP (op, 0);
6177 else if (GET_CODE (op) == PRE_MODIFY
6178 || GET_CODE (op) == LO_SUM)
6179 op = XEXP (op, 1);
6181 if (GET_CODE (op) == CONST)
6182 op = XEXP (op, 0);
6184 if (GET_CODE (op) == PLUS)
6185 op = XEXP (op, 1);
6187 if (CONST_INT_P (op))
6188 return op;
6190 return NULL_RTX;
6193 /* Return true if the MEM operand is a memory operand suitable for use
6194 with a (full width, possibly multiple) gpr load/store. On
6195 powerpc64 this means the offset must be divisible by 4.
6196 Implements 'Y' constraint.
6198 Accept direct, indexed, offset, lo_sum and tocref. Since this is
6199 a constraint function we know the operand has satisfied a suitable
6200 memory predicate. Also accept some odd rtl generated by reload
6201 (see rs6000_legitimize_reload_address for various forms). It is
6202 important that reload rtl be accepted by appropriate constraints
6203 but not by the operand predicate.
6205 Offsetting a lo_sum should not be allowed, except where we know by
6206 alignment that a 32k boundary is not crossed, but see the ???
6207 comment in rs6000_legitimize_reload_address. Note that by
6208 "offsetting" here we mean a further offset to access parts of the
6209 MEM. It's fine to have a lo_sum where the inner address is offset
6210 from a sym, since the same sym+offset will appear in the high part
6211 of the address calculation. */
6213 bool
6214 mem_operand_gpr (rtx op, enum machine_mode mode)
6216 unsigned HOST_WIDE_INT offset;
6217 int extra;
6218 rtx addr = XEXP (op, 0);
6220 op = address_offset (addr);
6221 if (op == NULL_RTX)
6222 return true;
6224 offset = INTVAL (op);
6225 if (TARGET_POWERPC64 && (offset & 3) != 0)
6226 return false;
6228 extra = GET_MODE_SIZE (mode) - UNITS_PER_WORD;
6229 if (extra < 0)
6230 extra = 0;
6232 if (GET_CODE (addr) == LO_SUM)
6233 /* For lo_sum addresses, we must allow any offset except one that
6234 causes a wrap, so test only the low 16 bits. */
6235 offset = ((offset & 0xffff) ^ 0x8000) - 0x8000;
6237 return offset + 0x8000 < 0x10000u - extra;
6240 /* Subroutines of rs6000_legitimize_address and rs6000_legitimate_address_p. */
6242 static bool
6243 reg_offset_addressing_ok_p (enum machine_mode mode)
6245 switch (mode)
6247 case V16QImode:
6248 case V8HImode:
6249 case V4SFmode:
6250 case V4SImode:
6251 case V2DFmode:
6252 case V2DImode:
6253 case V1TImode:
6254 case TImode:
6255 /* AltiVec/VSX vector modes. Only reg+reg addressing is valid. While
6256 TImode is not a vector mode, if we want to use the VSX registers to
6257 move it around, we need to restrict ourselves to reg+reg
6258 addressing. */
6259 if (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode))
6260 return false;
6261 break;
6263 case V4HImode:
6264 case V2SImode:
6265 case V1DImode:
6266 case V2SFmode:
6267 /* Paired vector modes. Only reg+reg addressing is valid. */
6268 if (TARGET_PAIRED_FLOAT)
6269 return false;
6270 break;
6272 case SDmode:
6273 /* If we can do direct load/stores of SDmode, restrict it to reg+reg
6274 addressing for the LFIWZX and STFIWX instructions. */
6275 if (TARGET_NO_SDMODE_STACK)
6276 return false;
6277 break;
6279 default:
6280 break;
6283 return true;
6286 static bool
6287 virtual_stack_registers_memory_p (rtx op)
6289 int regnum;
6291 if (GET_CODE (op) == REG)
6292 regnum = REGNO (op);
6294 else if (GET_CODE (op) == PLUS
6295 && GET_CODE (XEXP (op, 0)) == REG
6296 && GET_CODE (XEXP (op, 1)) == CONST_INT)
6297 regnum = REGNO (XEXP (op, 0));
6299 else
6300 return false;
6302 return (regnum >= FIRST_VIRTUAL_REGISTER
6303 && regnum <= LAST_VIRTUAL_POINTER_REGISTER);
6306 /* Return true if a MODE sized memory accesses to OP plus OFFSET
6307 is known to not straddle a 32k boundary. */
6309 static bool
6310 offsettable_ok_by_alignment (rtx op, HOST_WIDE_INT offset,
6311 enum machine_mode mode)
6313 tree decl, type;
6314 unsigned HOST_WIDE_INT dsize, dalign, lsb, mask;
6316 if (GET_CODE (op) != SYMBOL_REF)
6317 return false;
6319 dsize = GET_MODE_SIZE (mode);
6320 decl = SYMBOL_REF_DECL (op);
6321 if (!decl)
6323 if (dsize == 0)
6324 return false;
6326 /* -fsection-anchors loses the original SYMBOL_REF_DECL when
6327 replacing memory addresses with an anchor plus offset. We
6328 could find the decl by rummaging around in the block->objects
6329 VEC for the given offset but that seems like too much work. */
6330 dalign = BITS_PER_UNIT;
6331 if (SYMBOL_REF_HAS_BLOCK_INFO_P (op)
6332 && SYMBOL_REF_ANCHOR_P (op)
6333 && SYMBOL_REF_BLOCK (op) != NULL)
6335 struct object_block *block = SYMBOL_REF_BLOCK (op);
6337 dalign = block->alignment;
6338 offset += SYMBOL_REF_BLOCK_OFFSET (op);
6340 else if (CONSTANT_POOL_ADDRESS_P (op))
6342 /* It would be nice to have get_pool_align().. */
6343 enum machine_mode cmode = get_pool_mode (op);
6345 dalign = GET_MODE_ALIGNMENT (cmode);
6348 else if (DECL_P (decl))
6350 dalign = DECL_ALIGN (decl);
6352 if (dsize == 0)
6354 /* Allow BLKmode when the entire object is known to not
6355 cross a 32k boundary. */
6356 if (!DECL_SIZE_UNIT (decl))
6357 return false;
6359 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (decl)))
6360 return false;
6362 dsize = tree_to_uhwi (DECL_SIZE_UNIT (decl));
6363 if (dsize > 32768)
6364 return false;
6366 return dalign / BITS_PER_UNIT >= dsize;
6369 else
6371 type = TREE_TYPE (decl);
6373 dalign = TYPE_ALIGN (type);
6374 if (CONSTANT_CLASS_P (decl))
6375 dalign = CONSTANT_ALIGNMENT (decl, dalign);
6376 else
6377 dalign = DATA_ALIGNMENT (decl, dalign);
6379 if (dsize == 0)
6381 /* BLKmode, check the entire object. */
6382 if (TREE_CODE (decl) == STRING_CST)
6383 dsize = TREE_STRING_LENGTH (decl);
6384 else if (TYPE_SIZE_UNIT (type)
6385 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
6386 dsize = tree_to_uhwi (TYPE_SIZE_UNIT (type));
6387 else
6388 return false;
6389 if (dsize > 32768)
6390 return false;
6392 return dalign / BITS_PER_UNIT >= dsize;
6396 /* Find how many bits of the alignment we know for this access. */
6397 mask = dalign / BITS_PER_UNIT - 1;
6398 lsb = offset & -offset;
6399 mask &= lsb - 1;
6400 dalign = mask + 1;
6402 return dalign >= dsize;
6405 static bool
6406 constant_pool_expr_p (rtx op)
6408 rtx base, offset;
6410 split_const (op, &base, &offset);
6411 return (GET_CODE (base) == SYMBOL_REF
6412 && CONSTANT_POOL_ADDRESS_P (base)
6413 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (base), Pmode));
6416 static const_rtx tocrel_base, tocrel_offset;
6418 /* Return true if OP is a toc pointer relative address (the output
6419 of create_TOC_reference). If STRICT, do not match high part or
6420 non-split -mcmodel=large/medium toc pointer relative addresses. */
6422 bool
6423 toc_relative_expr_p (const_rtx op, bool strict)
6425 if (!TARGET_TOC)
6426 return false;
6428 if (TARGET_CMODEL != CMODEL_SMALL)
6430 /* Only match the low part. */
6431 if (GET_CODE (op) == LO_SUM
6432 && REG_P (XEXP (op, 0))
6433 && INT_REG_OK_FOR_BASE_P (XEXP (op, 0), strict))
6434 op = XEXP (op, 1);
6435 else if (strict)
6436 return false;
6439 tocrel_base = op;
6440 tocrel_offset = const0_rtx;
6441 if (GET_CODE (op) == PLUS && add_cint_operand (XEXP (op, 1), GET_MODE (op)))
6443 tocrel_base = XEXP (op, 0);
6444 tocrel_offset = XEXP (op, 1);
6447 return (GET_CODE (tocrel_base) == UNSPEC
6448 && XINT (tocrel_base, 1) == UNSPEC_TOCREL);
6451 /* Return true if X is a constant pool address, and also for cmodel=medium
6452 if X is a toc-relative address known to be offsettable within MODE. */
6454 bool
6455 legitimate_constant_pool_address_p (const_rtx x, enum machine_mode mode,
6456 bool strict)
6458 return (toc_relative_expr_p (x, strict)
6459 && (TARGET_CMODEL != CMODEL_MEDIUM
6460 || constant_pool_expr_p (XVECEXP (tocrel_base, 0, 0))
6461 || mode == QImode
6462 || offsettable_ok_by_alignment (XVECEXP (tocrel_base, 0, 0),
6463 INTVAL (tocrel_offset), mode)));
6466 static bool
6467 legitimate_small_data_p (enum machine_mode mode, rtx x)
6469 return (DEFAULT_ABI == ABI_V4
6470 && !flag_pic && !TARGET_TOC
6471 && (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST)
6472 && small_data_operand (x, mode));
6475 /* SPE offset addressing is limited to 5-bits worth of double words. */
6476 #define SPE_CONST_OFFSET_OK(x) (((x) & ~0xf8) == 0)
6478 bool
6479 rs6000_legitimate_offset_address_p (enum machine_mode mode, rtx x,
6480 bool strict, bool worst_case)
6482 unsigned HOST_WIDE_INT offset;
6483 unsigned int extra;
6485 if (GET_CODE (x) != PLUS)
6486 return false;
6487 if (!REG_P (XEXP (x, 0)))
6488 return false;
6489 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
6490 return false;
6491 if (!reg_offset_addressing_ok_p (mode))
6492 return virtual_stack_registers_memory_p (x);
6493 if (legitimate_constant_pool_address_p (x, mode, strict || lra_in_progress))
6494 return true;
6495 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6496 return false;
6498 offset = INTVAL (XEXP (x, 1));
6499 extra = 0;
6500 switch (mode)
6502 case V4HImode:
6503 case V2SImode:
6504 case V1DImode:
6505 case V2SFmode:
6506 /* SPE vector modes. */
6507 return SPE_CONST_OFFSET_OK (offset);
6509 case DFmode:
6510 case DDmode:
6511 case DImode:
6512 /* On e500v2, we may have:
6514 (subreg:DF (mem:DI (plus (reg) (const_int))) 0).
6516 Which gets addressed with evldd instructions. */
6517 if (TARGET_E500_DOUBLE)
6518 return SPE_CONST_OFFSET_OK (offset);
6520 /* If we are using VSX scalar loads, restrict ourselves to reg+reg
6521 addressing. */
6522 if (VECTOR_MEM_VSX_P (mode))
6523 return false;
6525 if (!worst_case)
6526 break;
6527 if (!TARGET_POWERPC64)
6528 extra = 4;
6529 else if (offset & 3)
6530 return false;
6531 break;
6533 case TFmode:
6534 if (TARGET_E500_DOUBLE)
6535 return (SPE_CONST_OFFSET_OK (offset)
6536 && SPE_CONST_OFFSET_OK (offset + 8));
6537 /* fall through */
6539 case TDmode:
6540 case TImode:
6541 case PTImode:
6542 extra = 8;
6543 if (!worst_case)
6544 break;
6545 if (!TARGET_POWERPC64)
6546 extra = 12;
6547 else if (offset & 3)
6548 return false;
6549 break;
6551 default:
6552 break;
6555 offset += 0x8000;
6556 return offset < 0x10000 - extra;
6559 bool
6560 legitimate_indexed_address_p (rtx x, int strict)
6562 rtx op0, op1;
6564 if (GET_CODE (x) != PLUS)
6565 return false;
6567 op0 = XEXP (x, 0);
6568 op1 = XEXP (x, 1);
6570 /* Recognize the rtl generated by reload which we know will later be
6571 replaced with proper base and index regs. */
6572 if (!strict
6573 && reload_in_progress
6574 && (REG_P (op0) || GET_CODE (op0) == PLUS)
6575 && REG_P (op1))
6576 return true;
6578 return (REG_P (op0) && REG_P (op1)
6579 && ((INT_REG_OK_FOR_BASE_P (op0, strict)
6580 && INT_REG_OK_FOR_INDEX_P (op1, strict))
6581 || (INT_REG_OK_FOR_BASE_P (op1, strict)
6582 && INT_REG_OK_FOR_INDEX_P (op0, strict))));
6585 bool
6586 avoiding_indexed_address_p (enum machine_mode mode)
6588 /* Avoid indexed addressing for modes that have non-indexed
6589 load/store instruction forms. */
6590 return (TARGET_AVOID_XFORM && VECTOR_MEM_NONE_P (mode));
6593 bool
6594 legitimate_indirect_address_p (rtx x, int strict)
6596 return GET_CODE (x) == REG && INT_REG_OK_FOR_BASE_P (x, strict);
6599 bool
6600 macho_lo_sum_memory_operand (rtx x, enum machine_mode mode)
6602 if (!TARGET_MACHO || !flag_pic
6603 || mode != SImode || GET_CODE (x) != MEM)
6604 return false;
6605 x = XEXP (x, 0);
6607 if (GET_CODE (x) != LO_SUM)
6608 return false;
6609 if (GET_CODE (XEXP (x, 0)) != REG)
6610 return false;
6611 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), 0))
6612 return false;
6613 x = XEXP (x, 1);
6615 return CONSTANT_P (x);
6618 static bool
6619 legitimate_lo_sum_address_p (enum machine_mode mode, rtx x, int strict)
6621 if (GET_CODE (x) != LO_SUM)
6622 return false;
6623 if (GET_CODE (XEXP (x, 0)) != REG)
6624 return false;
6625 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
6626 return false;
6627 /* Restrict addressing for DI because of our SUBREG hackery. */
6628 if (TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
6629 return false;
6630 x = XEXP (x, 1);
6632 if (TARGET_ELF || TARGET_MACHO)
6634 bool large_toc_ok;
6636 if (DEFAULT_ABI == ABI_V4 && flag_pic)
6637 return false;
6638 /* LRA don't use LEGITIMIZE_RELOAD_ADDRESS as it usually calls
6639 push_reload from reload pass code. LEGITIMIZE_RELOAD_ADDRESS
6640 recognizes some LO_SUM addresses as valid although this
6641 function says opposite. In most cases, LRA through different
6642 transformations can generate correct code for address reloads.
6643 It can not manage only some LO_SUM cases. So we need to add
6644 code analogous to one in rs6000_legitimize_reload_address for
6645 LOW_SUM here saying that some addresses are still valid. */
6646 large_toc_ok = (lra_in_progress && TARGET_CMODEL != CMODEL_SMALL
6647 && small_toc_ref (x, VOIDmode));
6648 if (TARGET_TOC && ! large_toc_ok)
6649 return false;
6650 if (GET_MODE_NUNITS (mode) != 1)
6651 return false;
6652 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
6653 && !(/* ??? Assume floating point reg based on mode? */
6654 TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT
6655 && (mode == DFmode || mode == DDmode)))
6656 return false;
6658 return CONSTANT_P (x) || large_toc_ok;
6661 return false;
6665 /* Try machine-dependent ways of modifying an illegitimate address
6666 to be legitimate. If we find one, return the new, valid address.
6667 This is used from only one place: `memory_address' in explow.c.
6669 OLDX is the address as it was before break_out_memory_refs was
6670 called. In some cases it is useful to look at this to decide what
6671 needs to be done.
6673 It is always safe for this function to do nothing. It exists to
6674 recognize opportunities to optimize the output.
6676 On RS/6000, first check for the sum of a register with a constant
6677 integer that is out of range. If so, generate code to add the
6678 constant with the low-order 16 bits masked to the register and force
6679 this result into another register (this can be done with `cau').
6680 Then generate an address of REG+(CONST&0xffff), allowing for the
6681 possibility of bit 16 being a one.
6683 Then check for the sum of a register and something not constant, try to
6684 load the other things into a register and return the sum. */
6686 static rtx
6687 rs6000_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
6688 enum machine_mode mode)
6690 unsigned int extra;
6692 if (!reg_offset_addressing_ok_p (mode))
6694 if (virtual_stack_registers_memory_p (x))
6695 return x;
6697 /* In theory we should not be seeing addresses of the form reg+0,
6698 but just in case it is generated, optimize it away. */
6699 if (GET_CODE (x) == PLUS && XEXP (x, 1) == const0_rtx)
6700 return force_reg (Pmode, XEXP (x, 0));
6702 /* For TImode with load/store quad, restrict addresses to just a single
6703 pointer, so it works with both GPRs and VSX registers. */
6704 /* Make sure both operands are registers. */
6705 else if (GET_CODE (x) == PLUS
6706 && (mode != TImode || !TARGET_QUAD_MEMORY))
6707 return gen_rtx_PLUS (Pmode,
6708 force_reg (Pmode, XEXP (x, 0)),
6709 force_reg (Pmode, XEXP (x, 1)));
6710 else
6711 return force_reg (Pmode, x);
6713 if (GET_CODE (x) == SYMBOL_REF)
6715 enum tls_model model = SYMBOL_REF_TLS_MODEL (x);
6716 if (model != 0)
6717 return rs6000_legitimize_tls_address (x, model);
6720 extra = 0;
6721 switch (mode)
6723 case TFmode:
6724 case TDmode:
6725 case TImode:
6726 case PTImode:
6727 /* As in legitimate_offset_address_p we do not assume
6728 worst-case. The mode here is just a hint as to the registers
6729 used. A TImode is usually in gprs, but may actually be in
6730 fprs. Leave worst-case scenario for reload to handle via
6731 insn constraints. PTImode is only GPRs. */
6732 extra = 8;
6733 break;
6734 default:
6735 break;
6738 if (GET_CODE (x) == PLUS
6739 && GET_CODE (XEXP (x, 0)) == REG
6740 && GET_CODE (XEXP (x, 1)) == CONST_INT
6741 && ((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 1)) + 0x8000)
6742 >= 0x10000 - extra)
6743 && !(SPE_VECTOR_MODE (mode)
6744 || (TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD)))
6746 HOST_WIDE_INT high_int, low_int;
6747 rtx sum;
6748 low_int = ((INTVAL (XEXP (x, 1)) & 0xffff) ^ 0x8000) - 0x8000;
6749 if (low_int >= 0x8000 - extra)
6750 low_int = 0;
6751 high_int = INTVAL (XEXP (x, 1)) - low_int;
6752 sum = force_operand (gen_rtx_PLUS (Pmode, XEXP (x, 0),
6753 GEN_INT (high_int)), 0);
6754 return plus_constant (Pmode, sum, low_int);
6756 else if (GET_CODE (x) == PLUS
6757 && GET_CODE (XEXP (x, 0)) == REG
6758 && GET_CODE (XEXP (x, 1)) != CONST_INT
6759 && GET_MODE_NUNITS (mode) == 1
6760 && (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
6761 || (/* ??? Assume floating point reg based on mode? */
6762 (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
6763 && (mode == DFmode || mode == DDmode)))
6764 && !avoiding_indexed_address_p (mode))
6766 return gen_rtx_PLUS (Pmode, XEXP (x, 0),
6767 force_reg (Pmode, force_operand (XEXP (x, 1), 0)));
6769 else if (SPE_VECTOR_MODE (mode)
6770 || (TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD))
6772 if (mode == DImode)
6773 return x;
6774 /* We accept [reg + reg] and [reg + OFFSET]. */
6776 if (GET_CODE (x) == PLUS)
6778 rtx op1 = XEXP (x, 0);
6779 rtx op2 = XEXP (x, 1);
6780 rtx y;
6782 op1 = force_reg (Pmode, op1);
6784 if (GET_CODE (op2) != REG
6785 && (GET_CODE (op2) != CONST_INT
6786 || !SPE_CONST_OFFSET_OK (INTVAL (op2))
6787 || (GET_MODE_SIZE (mode) > 8
6788 && !SPE_CONST_OFFSET_OK (INTVAL (op2) + 8))))
6789 op2 = force_reg (Pmode, op2);
6791 /* We can't always do [reg + reg] for these, because [reg +
6792 reg + offset] is not a legitimate addressing mode. */
6793 y = gen_rtx_PLUS (Pmode, op1, op2);
6795 if ((GET_MODE_SIZE (mode) > 8 || mode == DDmode) && REG_P (op2))
6796 return force_reg (Pmode, y);
6797 else
6798 return y;
6801 return force_reg (Pmode, x);
6803 else if ((TARGET_ELF
6804 #if TARGET_MACHO
6805 || !MACHO_DYNAMIC_NO_PIC_P
6806 #endif
6808 && TARGET_32BIT
6809 && TARGET_NO_TOC
6810 && ! flag_pic
6811 && GET_CODE (x) != CONST_INT
6812 && GET_CODE (x) != CONST_WIDE_INT
6813 && GET_CODE (x) != CONST_DOUBLE
6814 && CONSTANT_P (x)
6815 && GET_MODE_NUNITS (mode) == 1
6816 && (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
6817 || (/* ??? Assume floating point reg based on mode? */
6818 (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
6819 && (mode == DFmode || mode == DDmode))))
6821 rtx reg = gen_reg_rtx (Pmode);
6822 if (TARGET_ELF)
6823 emit_insn (gen_elf_high (reg, x));
6824 else
6825 emit_insn (gen_macho_high (reg, x));
6826 return gen_rtx_LO_SUM (Pmode, reg, x);
6828 else if (TARGET_TOC
6829 && GET_CODE (x) == SYMBOL_REF
6830 && constant_pool_expr_p (x)
6831 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (x), Pmode))
6832 return create_TOC_reference (x, NULL_RTX);
6833 else
6834 return x;
6837 /* Debug version of rs6000_legitimize_address. */
6838 static rtx
6839 rs6000_debug_legitimize_address (rtx x, rtx oldx, enum machine_mode mode)
6841 rtx ret;
6842 rtx_insn *insns;
6844 start_sequence ();
6845 ret = rs6000_legitimize_address (x, oldx, mode);
6846 insns = get_insns ();
6847 end_sequence ();
6849 if (ret != x)
6851 fprintf (stderr,
6852 "\nrs6000_legitimize_address: mode %s, old code %s, "
6853 "new code %s, modified\n",
6854 GET_MODE_NAME (mode), GET_RTX_NAME (GET_CODE (x)),
6855 GET_RTX_NAME (GET_CODE (ret)));
6857 fprintf (stderr, "Original address:\n");
6858 debug_rtx (x);
6860 fprintf (stderr, "oldx:\n");
6861 debug_rtx (oldx);
6863 fprintf (stderr, "New address:\n");
6864 debug_rtx (ret);
6866 if (insns)
6868 fprintf (stderr, "Insns added:\n");
6869 debug_rtx_list (insns, 20);
6872 else
6874 fprintf (stderr,
6875 "\nrs6000_legitimize_address: mode %s, code %s, no change:\n",
6876 GET_MODE_NAME (mode), GET_RTX_NAME (GET_CODE (x)));
6878 debug_rtx (x);
6881 if (insns)
6882 emit_insn (insns);
6884 return ret;
6887 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
6888 We need to emit DTP-relative relocations. */
6890 static void rs6000_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
6891 static void
6892 rs6000_output_dwarf_dtprel (FILE *file, int size, rtx x)
6894 switch (size)
6896 case 4:
6897 fputs ("\t.long\t", file);
6898 break;
6899 case 8:
6900 fputs (DOUBLE_INT_ASM_OP, file);
6901 break;
6902 default:
6903 gcc_unreachable ();
6905 output_addr_const (file, x);
6906 fputs ("@dtprel+0x8000", file);
6909 /* Return true if X is a symbol that refers to real (rather than emulated)
6910 TLS. */
6912 static bool
6913 rs6000_real_tls_symbol_ref_p (rtx x)
6915 return (GET_CODE (x) == SYMBOL_REF
6916 && SYMBOL_REF_TLS_MODEL (x) >= TLS_MODEL_REAL);
6919 /* In the name of slightly smaller debug output, and to cater to
6920 general assembler lossage, recognize various UNSPEC sequences
6921 and turn them back into a direct symbol reference. */
6923 static rtx
6924 rs6000_delegitimize_address (rtx orig_x)
6926 rtx x, y, offset;
6928 orig_x = delegitimize_mem_from_attrs (orig_x);
6929 x = orig_x;
6930 if (MEM_P (x))
6931 x = XEXP (x, 0);
6933 y = x;
6934 if (TARGET_CMODEL != CMODEL_SMALL
6935 && GET_CODE (y) == LO_SUM)
6936 y = XEXP (y, 1);
6938 offset = NULL_RTX;
6939 if (GET_CODE (y) == PLUS
6940 && GET_MODE (y) == Pmode
6941 && CONST_INT_P (XEXP (y, 1)))
6943 offset = XEXP (y, 1);
6944 y = XEXP (y, 0);
6947 if (GET_CODE (y) == UNSPEC
6948 && XINT (y, 1) == UNSPEC_TOCREL)
6950 #ifdef ENABLE_CHECKING
6951 if (REG_P (XVECEXP (y, 0, 1))
6952 && REGNO (XVECEXP (y, 0, 1)) == TOC_REGISTER)
6954 /* All good. */
6956 else if (GET_CODE (XVECEXP (y, 0, 1)) == DEBUG_EXPR)
6958 /* Weirdness alert. df_note_compute can replace r2 with a
6959 debug_expr when this unspec is in a debug_insn.
6960 Seen in gcc.dg/pr51957-1.c */
6962 else
6964 debug_rtx (orig_x);
6965 abort ();
6967 #endif
6968 y = XVECEXP (y, 0, 0);
6970 #ifdef HAVE_AS_TLS
6971 /* Do not associate thread-local symbols with the original
6972 constant pool symbol. */
6973 if (TARGET_XCOFF
6974 && GET_CODE (y) == SYMBOL_REF
6975 && CONSTANT_POOL_ADDRESS_P (y)
6976 && rs6000_real_tls_symbol_ref_p (get_pool_constant (y)))
6977 return orig_x;
6978 #endif
6980 if (offset != NULL_RTX)
6981 y = gen_rtx_PLUS (Pmode, y, offset);
6982 if (!MEM_P (orig_x))
6983 return y;
6984 else
6985 return replace_equiv_address_nv (orig_x, y);
6988 if (TARGET_MACHO
6989 && GET_CODE (orig_x) == LO_SUM
6990 && GET_CODE (XEXP (orig_x, 1)) == CONST)
6992 y = XEXP (XEXP (orig_x, 1), 0);
6993 if (GET_CODE (y) == UNSPEC
6994 && XINT (y, 1) == UNSPEC_MACHOPIC_OFFSET)
6995 return XVECEXP (y, 0, 0);
6998 return orig_x;
7001 /* Return true if X shouldn't be emitted into the debug info.
7002 The linker doesn't like .toc section references from
7003 .debug_* sections, so reject .toc section symbols. */
7005 static bool
7006 rs6000_const_not_ok_for_debug_p (rtx x)
7008 if (GET_CODE (x) == SYMBOL_REF
7009 && CONSTANT_POOL_ADDRESS_P (x))
7011 rtx c = get_pool_constant (x);
7012 enum machine_mode cmode = get_pool_mode (x);
7013 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (c, cmode))
7014 return true;
7017 return false;
7020 /* Construct the SYMBOL_REF for the tls_get_addr function. */
7022 static GTY(()) rtx rs6000_tls_symbol;
7023 static rtx
7024 rs6000_tls_get_addr (void)
7026 if (!rs6000_tls_symbol)
7027 rs6000_tls_symbol = init_one_libfunc ("__tls_get_addr");
7029 return rs6000_tls_symbol;
7032 /* Construct the SYMBOL_REF for TLS GOT references. */
7034 static GTY(()) rtx rs6000_got_symbol;
7035 static rtx
7036 rs6000_got_sym (void)
7038 if (!rs6000_got_symbol)
7040 rs6000_got_symbol = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
7041 SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_LOCAL;
7042 SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_EXTERNAL;
7045 return rs6000_got_symbol;
7048 /* AIX Thread-Local Address support. */
7050 static rtx
7051 rs6000_legitimize_tls_address_aix (rtx addr, enum tls_model model)
7053 rtx sym, mem, tocref, tlsreg, tmpreg, dest, tlsaddr;
7054 const char *name;
7055 char *tlsname;
7057 name = XSTR (addr, 0);
7058 /* Append TLS CSECT qualifier, unless the symbol already is qualified
7059 or the symbol will be in TLS private data section. */
7060 if (name[strlen (name) - 1] != ']'
7061 && (TREE_PUBLIC (SYMBOL_REF_DECL (addr))
7062 || bss_initializer_p (SYMBOL_REF_DECL (addr))))
7064 tlsname = XALLOCAVEC (char, strlen (name) + 4);
7065 strcpy (tlsname, name);
7066 strcat (tlsname,
7067 bss_initializer_p (SYMBOL_REF_DECL (addr)) ? "[UL]" : "[TL]");
7068 tlsaddr = copy_rtx (addr);
7069 XSTR (tlsaddr, 0) = ggc_strdup (tlsname);
7071 else
7072 tlsaddr = addr;
7074 /* Place addr into TOC constant pool. */
7075 sym = force_const_mem (GET_MODE (tlsaddr), tlsaddr);
7077 /* Output the TOC entry and create the MEM referencing the value. */
7078 if (constant_pool_expr_p (XEXP (sym, 0))
7079 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (XEXP (sym, 0)), Pmode))
7081 tocref = create_TOC_reference (XEXP (sym, 0), NULL_RTX);
7082 mem = gen_const_mem (Pmode, tocref);
7083 set_mem_alias_set (mem, get_TOC_alias_set ());
7085 else
7086 return sym;
7088 /* Use global-dynamic for local-dynamic. */
7089 if (model == TLS_MODEL_GLOBAL_DYNAMIC
7090 || model == TLS_MODEL_LOCAL_DYNAMIC)
7092 /* Create new TOC reference for @m symbol. */
7093 name = XSTR (XVECEXP (XEXP (mem, 0), 0, 0), 0);
7094 tlsname = XALLOCAVEC (char, strlen (name) + 1);
7095 strcpy (tlsname, "*LCM");
7096 strcat (tlsname, name + 3);
7097 rtx modaddr = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (tlsname));
7098 SYMBOL_REF_FLAGS (modaddr) |= SYMBOL_FLAG_LOCAL;
7099 tocref = create_TOC_reference (modaddr, NULL_RTX);
7100 rtx modmem = gen_const_mem (Pmode, tocref);
7101 set_mem_alias_set (modmem, get_TOC_alias_set ());
7103 rtx modreg = gen_reg_rtx (Pmode);
7104 emit_insn (gen_rtx_SET (VOIDmode, modreg, modmem));
7106 tmpreg = gen_reg_rtx (Pmode);
7107 emit_insn (gen_rtx_SET (VOIDmode, tmpreg, mem));
7109 dest = gen_reg_rtx (Pmode);
7110 if (TARGET_32BIT)
7111 emit_insn (gen_tls_get_addrsi (dest, modreg, tmpreg));
7112 else
7113 emit_insn (gen_tls_get_addrdi (dest, modreg, tmpreg));
7114 return dest;
7116 /* Obtain TLS pointer: 32 bit call or 64 bit GPR 13. */
7117 else if (TARGET_32BIT)
7119 tlsreg = gen_reg_rtx (SImode);
7120 emit_insn (gen_tls_get_tpointer (tlsreg));
7122 else
7123 tlsreg = gen_rtx_REG (DImode, 13);
7125 /* Load the TOC value into temporary register. */
7126 tmpreg = gen_reg_rtx (Pmode);
7127 emit_insn (gen_rtx_SET (VOIDmode, tmpreg, mem));
7128 set_unique_reg_note (get_last_insn (), REG_EQUAL,
7129 gen_rtx_MINUS (Pmode, addr, tlsreg));
7131 /* Add TOC symbol value to TLS pointer. */
7132 dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, tmpreg, tlsreg));
7134 return dest;
7137 /* ADDR contains a thread-local SYMBOL_REF. Generate code to compute
7138 this (thread-local) address. */
7140 static rtx
7141 rs6000_legitimize_tls_address (rtx addr, enum tls_model model)
7143 rtx dest, insn;
7145 if (TARGET_XCOFF)
7146 return rs6000_legitimize_tls_address_aix (addr, model);
7148 dest = gen_reg_rtx (Pmode);
7149 if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 16)
7151 rtx tlsreg;
7153 if (TARGET_64BIT)
7155 tlsreg = gen_rtx_REG (Pmode, 13);
7156 insn = gen_tls_tprel_64 (dest, tlsreg, addr);
7158 else
7160 tlsreg = gen_rtx_REG (Pmode, 2);
7161 insn = gen_tls_tprel_32 (dest, tlsreg, addr);
7163 emit_insn (insn);
7165 else if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 32)
7167 rtx tlsreg, tmp;
7169 tmp = gen_reg_rtx (Pmode);
7170 if (TARGET_64BIT)
7172 tlsreg = gen_rtx_REG (Pmode, 13);
7173 insn = gen_tls_tprel_ha_64 (tmp, tlsreg, addr);
7175 else
7177 tlsreg = gen_rtx_REG (Pmode, 2);
7178 insn = gen_tls_tprel_ha_32 (tmp, tlsreg, addr);
7180 emit_insn (insn);
7181 if (TARGET_64BIT)
7182 insn = gen_tls_tprel_lo_64 (dest, tmp, addr);
7183 else
7184 insn = gen_tls_tprel_lo_32 (dest, tmp, addr);
7185 emit_insn (insn);
7187 else
7189 rtx r3, got, tga, tmp1, tmp2, call_insn;
7191 /* We currently use relocations like @got@tlsgd for tls, which
7192 means the linker will handle allocation of tls entries, placing
7193 them in the .got section. So use a pointer to the .got section,
7194 not one to secondary TOC sections used by 64-bit -mminimal-toc,
7195 or to secondary GOT sections used by 32-bit -fPIC. */
7196 if (TARGET_64BIT)
7197 got = gen_rtx_REG (Pmode, 2);
7198 else
7200 if (flag_pic == 1)
7201 got = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
7202 else
7204 rtx gsym = rs6000_got_sym ();
7205 got = gen_reg_rtx (Pmode);
7206 if (flag_pic == 0)
7207 rs6000_emit_move (got, gsym, Pmode);
7208 else
7210 rtx mem, lab, last;
7212 tmp1 = gen_reg_rtx (Pmode);
7213 tmp2 = gen_reg_rtx (Pmode);
7214 mem = gen_const_mem (Pmode, tmp1);
7215 lab = gen_label_rtx ();
7216 emit_insn (gen_load_toc_v4_PIC_1b (gsym, lab));
7217 emit_move_insn (tmp1, gen_rtx_REG (Pmode, LR_REGNO));
7218 if (TARGET_LINK_STACK)
7219 emit_insn (gen_addsi3 (tmp1, tmp1, GEN_INT (4)));
7220 emit_move_insn (tmp2, mem);
7221 last = emit_insn (gen_addsi3 (got, tmp1, tmp2));
7222 set_unique_reg_note (last, REG_EQUAL, gsym);
7227 if (model == TLS_MODEL_GLOBAL_DYNAMIC)
7229 tga = rs6000_tls_get_addr ();
7230 emit_library_call_value (tga, dest, LCT_CONST, Pmode,
7231 1, const0_rtx, Pmode);
7233 r3 = gen_rtx_REG (Pmode, 3);
7234 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
7236 if (TARGET_64BIT)
7237 insn = gen_tls_gd_aix64 (r3, got, addr, tga, const0_rtx);
7238 else
7239 insn = gen_tls_gd_aix32 (r3, got, addr, tga, const0_rtx);
7241 else if (DEFAULT_ABI == ABI_V4)
7242 insn = gen_tls_gd_sysvsi (r3, got, addr, tga, const0_rtx);
7243 else
7244 gcc_unreachable ();
7245 call_insn = last_call_insn ();
7246 PATTERN (call_insn) = insn;
7247 if (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
7248 use_reg (&CALL_INSN_FUNCTION_USAGE (call_insn),
7249 pic_offset_table_rtx);
7251 else if (model == TLS_MODEL_LOCAL_DYNAMIC)
7253 tga = rs6000_tls_get_addr ();
7254 tmp1 = gen_reg_rtx (Pmode);
7255 emit_library_call_value (tga, tmp1, LCT_CONST, Pmode,
7256 1, const0_rtx, Pmode);
7258 r3 = gen_rtx_REG (Pmode, 3);
7259 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
7261 if (TARGET_64BIT)
7262 insn = gen_tls_ld_aix64 (r3, got, tga, const0_rtx);
7263 else
7264 insn = gen_tls_ld_aix32 (r3, got, tga, const0_rtx);
7266 else if (DEFAULT_ABI == ABI_V4)
7267 insn = gen_tls_ld_sysvsi (r3, got, tga, const0_rtx);
7268 else
7269 gcc_unreachable ();
7270 call_insn = last_call_insn ();
7271 PATTERN (call_insn) = insn;
7272 if (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
7273 use_reg (&CALL_INSN_FUNCTION_USAGE (call_insn),
7274 pic_offset_table_rtx);
7276 if (rs6000_tls_size == 16)
7278 if (TARGET_64BIT)
7279 insn = gen_tls_dtprel_64 (dest, tmp1, addr);
7280 else
7281 insn = gen_tls_dtprel_32 (dest, tmp1, addr);
7283 else if (rs6000_tls_size == 32)
7285 tmp2 = gen_reg_rtx (Pmode);
7286 if (TARGET_64BIT)
7287 insn = gen_tls_dtprel_ha_64 (tmp2, tmp1, addr);
7288 else
7289 insn = gen_tls_dtprel_ha_32 (tmp2, tmp1, addr);
7290 emit_insn (insn);
7291 if (TARGET_64BIT)
7292 insn = gen_tls_dtprel_lo_64 (dest, tmp2, addr);
7293 else
7294 insn = gen_tls_dtprel_lo_32 (dest, tmp2, addr);
7296 else
7298 tmp2 = gen_reg_rtx (Pmode);
7299 if (TARGET_64BIT)
7300 insn = gen_tls_got_dtprel_64 (tmp2, got, addr);
7301 else
7302 insn = gen_tls_got_dtprel_32 (tmp2, got, addr);
7303 emit_insn (insn);
7304 insn = gen_rtx_SET (Pmode, dest,
7305 gen_rtx_PLUS (Pmode, tmp2, tmp1));
7307 emit_insn (insn);
7309 else
7311 /* IE, or 64-bit offset LE. */
7312 tmp2 = gen_reg_rtx (Pmode);
7313 if (TARGET_64BIT)
7314 insn = gen_tls_got_tprel_64 (tmp2, got, addr);
7315 else
7316 insn = gen_tls_got_tprel_32 (tmp2, got, addr);
7317 emit_insn (insn);
7318 if (TARGET_64BIT)
7319 insn = gen_tls_tls_64 (dest, tmp2, addr);
7320 else
7321 insn = gen_tls_tls_32 (dest, tmp2, addr);
7322 emit_insn (insn);
7326 return dest;
7329 /* Implement TARGET_CANNOT_FORCE_CONST_MEM. */
7331 static bool
7332 rs6000_cannot_force_const_mem (enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
7334 if (GET_CODE (x) == HIGH
7335 && GET_CODE (XEXP (x, 0)) == UNSPEC)
7336 return true;
7338 /* A TLS symbol in the TOC cannot contain a sum. */
7339 if (GET_CODE (x) == CONST
7340 && GET_CODE (XEXP (x, 0)) == PLUS
7341 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
7342 && SYMBOL_REF_TLS_MODEL (XEXP (XEXP (x, 0), 0)) != 0)
7343 return true;
7345 /* Do not place an ELF TLS symbol in the constant pool. */
7346 return TARGET_ELF && tls_referenced_p (x);
7349 /* Return true iff the given SYMBOL_REF refers to a constant pool entry
7350 that we have put in the TOC, or for cmodel=medium, if the SYMBOL_REF
7351 can be addressed relative to the toc pointer. */
7353 static bool
7354 use_toc_relative_ref (rtx sym)
7356 return ((constant_pool_expr_p (sym)
7357 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (sym),
7358 get_pool_mode (sym)))
7359 || (TARGET_CMODEL == CMODEL_MEDIUM
7360 && SYMBOL_REF_LOCAL_P (sym)));
7363 /* Our implementation of LEGITIMIZE_RELOAD_ADDRESS. Returns a value to
7364 replace the input X, or the original X if no replacement is called for.
7365 The output parameter *WIN is 1 if the calling macro should goto WIN,
7366 0 if it should not.
7368 For RS/6000, we wish to handle large displacements off a base
7369 register by splitting the addend across an addiu/addis and the mem insn.
7370 This cuts number of extra insns needed from 3 to 1.
7372 On Darwin, we use this to generate code for floating point constants.
7373 A movsf_low is generated so we wind up with 2 instructions rather than 3.
7374 The Darwin code is inside #if TARGET_MACHO because only then are the
7375 machopic_* functions defined. */
7376 static rtx
7377 rs6000_legitimize_reload_address (rtx x, enum machine_mode mode,
7378 int opnum, int type,
7379 int ind_levels ATTRIBUTE_UNUSED, int *win)
7381 bool reg_offset_p = reg_offset_addressing_ok_p (mode);
7383 /* Nasty hack for vsx_splat_V2DF/V2DI load from mem, which takes a
7384 DFmode/DImode MEM. */
7385 if (reg_offset_p
7386 && opnum == 1
7387 && ((mode == DFmode && recog_data.operand_mode[0] == V2DFmode)
7388 || (mode == DImode && recog_data.operand_mode[0] == V2DImode)))
7389 reg_offset_p = false;
7391 /* We must recognize output that we have already generated ourselves. */
7392 if (GET_CODE (x) == PLUS
7393 && GET_CODE (XEXP (x, 0)) == PLUS
7394 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
7395 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7396 && GET_CODE (XEXP (x, 1)) == CONST_INT)
7398 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7399 BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
7400 opnum, (enum reload_type) type);
7401 *win = 1;
7402 return x;
7405 /* Likewise for (lo_sum (high ...) ...) output we have generated. */
7406 if (GET_CODE (x) == LO_SUM
7407 && GET_CODE (XEXP (x, 0)) == HIGH)
7409 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7410 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
7411 opnum, (enum reload_type) type);
7412 *win = 1;
7413 return x;
7416 #if TARGET_MACHO
7417 if (DEFAULT_ABI == ABI_DARWIN && flag_pic
7418 && GET_CODE (x) == LO_SUM
7419 && GET_CODE (XEXP (x, 0)) == PLUS
7420 && XEXP (XEXP (x, 0), 0) == pic_offset_table_rtx
7421 && GET_CODE (XEXP (XEXP (x, 0), 1)) == HIGH
7422 && XEXP (XEXP (XEXP (x, 0), 1), 0) == XEXP (x, 1)
7423 && machopic_operand_p (XEXP (x, 1)))
7425 /* Result of previous invocation of this function on Darwin
7426 floating point constant. */
7427 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7428 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
7429 opnum, (enum reload_type) type);
7430 *win = 1;
7431 return x;
7433 #endif
7435 if (TARGET_CMODEL != CMODEL_SMALL
7436 && reg_offset_p
7437 && small_toc_ref (x, VOIDmode))
7439 rtx hi = gen_rtx_HIGH (Pmode, copy_rtx (x));
7440 x = gen_rtx_LO_SUM (Pmode, hi, x);
7441 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7442 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
7443 opnum, (enum reload_type) type);
7444 *win = 1;
7445 return x;
7448 if (GET_CODE (x) == PLUS
7449 && GET_CODE (XEXP (x, 0)) == REG
7450 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
7451 && INT_REG_OK_FOR_BASE_P (XEXP (x, 0), 1)
7452 && GET_CODE (XEXP (x, 1)) == CONST_INT
7453 && reg_offset_p
7454 && !SPE_VECTOR_MODE (mode)
7455 && !(TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
7456 && (!VECTOR_MODE_P (mode) || VECTOR_MEM_NONE_P (mode)))
7458 HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
7459 HOST_WIDE_INT low = ((val & 0xffff) ^ 0x8000) - 0x8000;
7460 HOST_WIDE_INT high
7461 = (((val - low) & 0xffffffff) ^ 0x80000000) - 0x80000000;
7463 /* Check for 32-bit overflow. */
7464 if (high + low != val)
7466 *win = 0;
7467 return x;
7470 /* Reload the high part into a base reg; leave the low part
7471 in the mem directly. */
7473 x = gen_rtx_PLUS (GET_MODE (x),
7474 gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0),
7475 GEN_INT (high)),
7476 GEN_INT (low));
7478 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7479 BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
7480 opnum, (enum reload_type) type);
7481 *win = 1;
7482 return x;
7485 if (GET_CODE (x) == SYMBOL_REF
7486 && reg_offset_p
7487 && (!VECTOR_MODE_P (mode) || VECTOR_MEM_NONE_P (mode))
7488 && !SPE_VECTOR_MODE (mode)
7489 #if TARGET_MACHO
7490 && DEFAULT_ABI == ABI_DARWIN
7491 && (flag_pic || MACHO_DYNAMIC_NO_PIC_P)
7492 && machopic_symbol_defined_p (x)
7493 #else
7494 && DEFAULT_ABI == ABI_V4
7495 && !flag_pic
7496 #endif
7497 /* Don't do this for TFmode or TDmode, since the result isn't offsettable.
7498 The same goes for DImode without 64-bit gprs and DFmode and DDmode
7499 without fprs.
7500 ??? Assume floating point reg based on mode? This assumption is
7501 violated by eg. powerpc-linux -m32 compile of gcc.dg/pr28796-2.c
7502 where reload ends up doing a DFmode load of a constant from
7503 mem using two gprs. Unfortunately, at this point reload
7504 hasn't yet selected regs so poking around in reload data
7505 won't help and even if we could figure out the regs reliably,
7506 we'd still want to allow this transformation when the mem is
7507 naturally aligned. Since we say the address is good here, we
7508 can't disable offsets from LO_SUMs in mem_operand_gpr.
7509 FIXME: Allow offset from lo_sum for other modes too, when
7510 mem is sufficiently aligned. */
7511 && mode != TFmode
7512 && mode != TDmode
7513 && (mode != TImode || !TARGET_VSX_TIMODE)
7514 && mode != PTImode
7515 && (mode != DImode || TARGET_POWERPC64)
7516 && ((mode != DFmode && mode != DDmode) || TARGET_POWERPC64
7517 || (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)))
7519 #if TARGET_MACHO
7520 if (flag_pic)
7522 rtx offset = machopic_gen_offset (x);
7523 x = gen_rtx_LO_SUM (GET_MODE (x),
7524 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
7525 gen_rtx_HIGH (Pmode, offset)), offset);
7527 else
7528 #endif
7529 x = gen_rtx_LO_SUM (GET_MODE (x),
7530 gen_rtx_HIGH (Pmode, x), x);
7532 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7533 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
7534 opnum, (enum reload_type) type);
7535 *win = 1;
7536 return x;
7539 /* Reload an offset address wrapped by an AND that represents the
7540 masking of the lower bits. Strip the outer AND and let reload
7541 convert the offset address into an indirect address. For VSX,
7542 force reload to create the address with an AND in a separate
7543 register, because we can't guarantee an altivec register will
7544 be used. */
7545 if (VECTOR_MEM_ALTIVEC_P (mode)
7546 && GET_CODE (x) == AND
7547 && GET_CODE (XEXP (x, 0)) == PLUS
7548 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
7549 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7550 && GET_CODE (XEXP (x, 1)) == CONST_INT
7551 && INTVAL (XEXP (x, 1)) == -16)
7553 x = XEXP (x, 0);
7554 *win = 1;
7555 return x;
7558 if (TARGET_TOC
7559 && reg_offset_p
7560 && GET_CODE (x) == SYMBOL_REF
7561 && use_toc_relative_ref (x))
7563 x = create_TOC_reference (x, NULL_RTX);
7564 if (TARGET_CMODEL != CMODEL_SMALL)
7565 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7566 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
7567 opnum, (enum reload_type) type);
7568 *win = 1;
7569 return x;
7571 *win = 0;
7572 return x;
7575 /* Debug version of rs6000_legitimize_reload_address. */
7576 static rtx
7577 rs6000_debug_legitimize_reload_address (rtx x, enum machine_mode mode,
7578 int opnum, int type,
7579 int ind_levels, int *win)
7581 rtx ret = rs6000_legitimize_reload_address (x, mode, opnum, type,
7582 ind_levels, win);
7583 fprintf (stderr,
7584 "\nrs6000_legitimize_reload_address: mode = %s, opnum = %d, "
7585 "type = %d, ind_levels = %d, win = %d, original addr:\n",
7586 GET_MODE_NAME (mode), opnum, type, ind_levels, *win);
7587 debug_rtx (x);
7589 if (x == ret)
7590 fprintf (stderr, "Same address returned\n");
7591 else if (!ret)
7592 fprintf (stderr, "NULL returned\n");
7593 else
7595 fprintf (stderr, "New address:\n");
7596 debug_rtx (ret);
7599 return ret;
7602 /* TARGET_LEGITIMATE_ADDRESS_P recognizes an RTL expression
7603 that is a valid memory address for an instruction.
7604 The MODE argument is the machine mode for the MEM expression
7605 that wants to use this address.
7607 On the RS/6000, there are four valid address: a SYMBOL_REF that
7608 refers to a constant pool entry of an address (or the sum of it
7609 plus a constant), a short (16-bit signed) constant plus a register,
7610 the sum of two registers, or a register indirect, possibly with an
7611 auto-increment. For DFmode, DDmode and DImode with a constant plus
7612 register, we must ensure that both words are addressable or PowerPC64
7613 with offset word aligned.
7615 For modes spanning multiple registers (DFmode and DDmode in 32-bit GPRs,
7616 32-bit DImode, TImode, TFmode, TDmode), indexed addressing cannot be used
7617 because adjacent memory cells are accessed by adding word-sized offsets
7618 during assembly output. */
7619 static bool
7620 rs6000_legitimate_address_p (enum machine_mode mode, rtx x, bool reg_ok_strict)
7622 bool reg_offset_p = reg_offset_addressing_ok_p (mode);
7624 /* If this is an unaligned stvx/ldvx type address, discard the outer AND. */
7625 if (VECTOR_MEM_ALTIVEC_P (mode)
7626 && GET_CODE (x) == AND
7627 && GET_CODE (XEXP (x, 1)) == CONST_INT
7628 && INTVAL (XEXP (x, 1)) == -16)
7629 x = XEXP (x, 0);
7631 if (TARGET_ELF && RS6000_SYMBOL_REF_TLS_P (x))
7632 return 0;
7633 if (legitimate_indirect_address_p (x, reg_ok_strict))
7634 return 1;
7635 if (TARGET_UPDATE
7636 && (GET_CODE (x) == PRE_INC || GET_CODE (x) == PRE_DEC)
7637 && mode_supports_pre_incdec_p (mode)
7638 && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict))
7639 return 1;
7640 if (virtual_stack_registers_memory_p (x))
7641 return 1;
7642 if (reg_offset_p && legitimate_small_data_p (mode, x))
7643 return 1;
7644 if (reg_offset_p
7645 && legitimate_constant_pool_address_p (x, mode,
7646 reg_ok_strict || lra_in_progress))
7647 return 1;
7648 /* For TImode, if we have load/store quad and TImode in VSX registers, only
7649 allow register indirect addresses. This will allow the values to go in
7650 either GPRs or VSX registers without reloading. The vector types would
7651 tend to go into VSX registers, so we allow REG+REG, while TImode seems
7652 somewhat split, in that some uses are GPR based, and some VSX based. */
7653 if (mode == TImode && TARGET_QUAD_MEMORY && TARGET_VSX_TIMODE)
7654 return 0;
7655 /* If not REG_OK_STRICT (before reload) let pass any stack offset. */
7656 if (! reg_ok_strict
7657 && reg_offset_p
7658 && GET_CODE (x) == PLUS
7659 && GET_CODE (XEXP (x, 0)) == REG
7660 && (XEXP (x, 0) == virtual_stack_vars_rtx
7661 || XEXP (x, 0) == arg_pointer_rtx)
7662 && GET_CODE (XEXP (x, 1)) == CONST_INT)
7663 return 1;
7664 if (rs6000_legitimate_offset_address_p (mode, x, reg_ok_strict, false))
7665 return 1;
7666 if (mode != TFmode
7667 && mode != TDmode
7668 && ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
7669 || TARGET_POWERPC64
7670 || (mode != DFmode && mode != DDmode)
7671 || (TARGET_E500_DOUBLE && mode != DDmode))
7672 && (TARGET_POWERPC64 || mode != DImode)
7673 && (mode != TImode || VECTOR_MEM_VSX_P (TImode))
7674 && mode != PTImode
7675 && !avoiding_indexed_address_p (mode)
7676 && legitimate_indexed_address_p (x, reg_ok_strict))
7677 return 1;
7678 if (TARGET_UPDATE && GET_CODE (x) == PRE_MODIFY
7679 && mode_supports_pre_modify_p (mode)
7680 && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict)
7681 && (rs6000_legitimate_offset_address_p (mode, XEXP (x, 1),
7682 reg_ok_strict, false)
7683 || (!avoiding_indexed_address_p (mode)
7684 && legitimate_indexed_address_p (XEXP (x, 1), reg_ok_strict)))
7685 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
7686 return 1;
7687 if (reg_offset_p && legitimate_lo_sum_address_p (mode, x, reg_ok_strict))
7688 return 1;
7689 return 0;
7692 /* Debug version of rs6000_legitimate_address_p. */
7693 static bool
7694 rs6000_debug_legitimate_address_p (enum machine_mode mode, rtx x,
7695 bool reg_ok_strict)
7697 bool ret = rs6000_legitimate_address_p (mode, x, reg_ok_strict);
7698 fprintf (stderr,
7699 "\nrs6000_legitimate_address_p: return = %s, mode = %s, "
7700 "strict = %d, reload = %s, code = %s\n",
7701 ret ? "true" : "false",
7702 GET_MODE_NAME (mode),
7703 reg_ok_strict,
7704 (reload_completed
7705 ? "after"
7706 : (reload_in_progress ? "progress" : "before")),
7707 GET_RTX_NAME (GET_CODE (x)));
7708 debug_rtx (x);
7710 return ret;
7713 /* Implement TARGET_MODE_DEPENDENT_ADDRESS_P. */
7715 static bool
7716 rs6000_mode_dependent_address_p (const_rtx addr,
7717 addr_space_t as ATTRIBUTE_UNUSED)
7719 return rs6000_mode_dependent_address_ptr (addr);
7722 /* Go to LABEL if ADDR (a legitimate address expression)
7723 has an effect that depends on the machine mode it is used for.
7725 On the RS/6000 this is true of all integral offsets (since AltiVec
7726 and VSX modes don't allow them) or is a pre-increment or decrement.
7728 ??? Except that due to conceptual problems in offsettable_address_p
7729 we can't really report the problems of integral offsets. So leave
7730 this assuming that the adjustable offset must be valid for the
7731 sub-words of a TFmode operand, which is what we had before. */
7733 static bool
7734 rs6000_mode_dependent_address (const_rtx addr)
7736 switch (GET_CODE (addr))
7738 case PLUS:
7739 /* Any offset from virtual_stack_vars_rtx and arg_pointer_rtx
7740 is considered a legitimate address before reload, so there
7741 are no offset restrictions in that case. Note that this
7742 condition is safe in strict mode because any address involving
7743 virtual_stack_vars_rtx or arg_pointer_rtx would already have
7744 been rejected as illegitimate. */
7745 if (XEXP (addr, 0) != virtual_stack_vars_rtx
7746 && XEXP (addr, 0) != arg_pointer_rtx
7747 && GET_CODE (XEXP (addr, 1)) == CONST_INT)
7749 unsigned HOST_WIDE_INT val = INTVAL (XEXP (addr, 1));
7750 return val + 0x8000 >= 0x10000 - (TARGET_POWERPC64 ? 8 : 12);
7752 break;
7754 case LO_SUM:
7755 /* Anything in the constant pool is sufficiently aligned that
7756 all bytes have the same high part address. */
7757 return !legitimate_constant_pool_address_p (addr, QImode, false);
7759 /* Auto-increment cases are now treated generically in recog.c. */
7760 case PRE_MODIFY:
7761 return TARGET_UPDATE;
7763 /* AND is only allowed in Altivec loads. */
7764 case AND:
7765 return true;
7767 default:
7768 break;
7771 return false;
7774 /* Debug version of rs6000_mode_dependent_address. */
7775 static bool
7776 rs6000_debug_mode_dependent_address (const_rtx addr)
7778 bool ret = rs6000_mode_dependent_address (addr);
7780 fprintf (stderr, "\nrs6000_mode_dependent_address: ret = %s\n",
7781 ret ? "true" : "false");
7782 debug_rtx (addr);
7784 return ret;
7787 /* Implement FIND_BASE_TERM. */
7790 rs6000_find_base_term (rtx op)
7792 rtx base;
7794 base = op;
7795 if (GET_CODE (base) == CONST)
7796 base = XEXP (base, 0);
7797 if (GET_CODE (base) == PLUS)
7798 base = XEXP (base, 0);
7799 if (GET_CODE (base) == UNSPEC)
7800 switch (XINT (base, 1))
7802 case UNSPEC_TOCREL:
7803 case UNSPEC_MACHOPIC_OFFSET:
7804 /* OP represents SYM [+ OFFSET] - ANCHOR. SYM is the base term
7805 for aliasing purposes. */
7806 return XVECEXP (base, 0, 0);
7809 return op;
7812 /* More elaborate version of recog's offsettable_memref_p predicate
7813 that works around the ??? note of rs6000_mode_dependent_address.
7814 In particular it accepts
7816 (mem:DI (plus:SI (reg/f:SI 31 31) (const_int 32760 [0x7ff8])))
7818 in 32-bit mode, that the recog predicate rejects. */
7820 static bool
7821 rs6000_offsettable_memref_p (rtx op, enum machine_mode reg_mode)
7823 bool worst_case;
7825 if (!MEM_P (op))
7826 return false;
7828 /* First mimic offsettable_memref_p. */
7829 if (offsettable_address_p (true, GET_MODE (op), XEXP (op, 0)))
7830 return true;
7832 /* offsettable_address_p invokes rs6000_mode_dependent_address, but
7833 the latter predicate knows nothing about the mode of the memory
7834 reference and, therefore, assumes that it is the largest supported
7835 mode (TFmode). As a consequence, legitimate offsettable memory
7836 references are rejected. rs6000_legitimate_offset_address_p contains
7837 the correct logic for the PLUS case of rs6000_mode_dependent_address,
7838 at least with a little bit of help here given that we know the
7839 actual registers used. */
7840 worst_case = ((TARGET_POWERPC64 && GET_MODE_CLASS (reg_mode) == MODE_INT)
7841 || GET_MODE_SIZE (reg_mode) == 4);
7842 return rs6000_legitimate_offset_address_p (GET_MODE (op), XEXP (op, 0),
7843 true, worst_case);
7846 /* Change register usage conditional on target flags. */
7847 static void
7848 rs6000_conditional_register_usage (void)
7850 int i;
7852 if (TARGET_DEBUG_TARGET)
7853 fprintf (stderr, "rs6000_conditional_register_usage called\n");
7855 /* Set MQ register fixed (already call_used) so that it will not be
7856 allocated. */
7857 fixed_regs[64] = 1;
7859 /* 64-bit AIX and Linux reserve GPR13 for thread-private data. */
7860 if (TARGET_64BIT)
7861 fixed_regs[13] = call_used_regs[13]
7862 = call_really_used_regs[13] = 1;
7864 /* Conditionally disable FPRs. */
7865 if (TARGET_SOFT_FLOAT || !TARGET_FPRS)
7866 for (i = 32; i < 64; i++)
7867 fixed_regs[i] = call_used_regs[i]
7868 = call_really_used_regs[i] = 1;
7870 /* The TOC register is not killed across calls in a way that is
7871 visible to the compiler. */
7872 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
7873 call_really_used_regs[2] = 0;
7875 if (DEFAULT_ABI == ABI_V4
7876 && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
7877 && flag_pic == 2)
7878 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
7880 if (DEFAULT_ABI == ABI_V4
7881 && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
7882 && flag_pic == 1)
7883 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
7884 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
7885 = call_really_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
7887 if (DEFAULT_ABI == ABI_DARWIN
7888 && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM)
7889 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
7890 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
7891 = call_really_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
7893 if (TARGET_TOC && TARGET_MINIMAL_TOC)
7894 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
7895 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
7897 if (TARGET_SPE)
7899 global_regs[SPEFSCR_REGNO] = 1;
7900 /* We used to use r14 as FIXED_SCRATCH to address SPE 64-bit
7901 registers in prologues and epilogues. We no longer use r14
7902 for FIXED_SCRATCH, but we're keeping r14 out of the allocation
7903 pool for link-compatibility with older versions of GCC. Once
7904 "old" code has died out, we can return r14 to the allocation
7905 pool. */
7906 fixed_regs[14]
7907 = call_used_regs[14]
7908 = call_really_used_regs[14] = 1;
7911 if (!TARGET_ALTIVEC && !TARGET_VSX)
7913 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
7914 fixed_regs[i] = call_used_regs[i] = call_really_used_regs[i] = 1;
7915 call_really_used_regs[VRSAVE_REGNO] = 1;
7918 if (TARGET_ALTIVEC || TARGET_VSX)
7919 global_regs[VSCR_REGNO] = 1;
7921 if (TARGET_ALTIVEC_ABI)
7923 for (i = FIRST_ALTIVEC_REGNO; i < FIRST_ALTIVEC_REGNO + 20; ++i)
7924 call_used_regs[i] = call_really_used_regs[i] = 1;
7926 /* AIX reserves VR20:31 in non-extended ABI mode. */
7927 if (TARGET_XCOFF)
7928 for (i = FIRST_ALTIVEC_REGNO + 20; i < FIRST_ALTIVEC_REGNO + 32; ++i)
7929 fixed_regs[i] = call_used_regs[i] = call_really_used_regs[i] = 1;
7934 /* Output insns to set DEST equal to the constant SOURCE as a series of
7935 lis, ori and shl instructions and return TRUE. */
7937 bool
7938 rs6000_emit_set_const (rtx dest, rtx source)
7940 enum machine_mode mode = GET_MODE (dest);
7941 rtx temp, set;
7942 rtx_insn *insn;
7943 HOST_WIDE_INT c;
7945 gcc_checking_assert (CONST_INT_P (source));
7946 c = INTVAL (source);
7947 switch (mode)
7949 case QImode:
7950 case HImode:
7951 emit_insn (gen_rtx_SET (VOIDmode, dest, source));
7952 return true;
7954 case SImode:
7955 temp = !can_create_pseudo_p () ? dest : gen_reg_rtx (SImode);
7957 emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (temp),
7958 GEN_INT (c & ~(HOST_WIDE_INT) 0xffff)));
7959 emit_insn (gen_rtx_SET (VOIDmode, dest,
7960 gen_rtx_IOR (SImode, copy_rtx (temp),
7961 GEN_INT (c & 0xffff))));
7962 break;
7964 case DImode:
7965 if (!TARGET_POWERPC64)
7967 rtx hi, lo;
7969 hi = operand_subword_force (copy_rtx (dest), WORDS_BIG_ENDIAN == 0,
7970 DImode);
7971 lo = operand_subword_force (dest, WORDS_BIG_ENDIAN != 0,
7972 DImode);
7973 emit_move_insn (hi, GEN_INT (c >> 32));
7974 c = ((c & 0xffffffff) ^ 0x80000000) - 0x80000000;
7975 emit_move_insn (lo, GEN_INT (c));
7977 else
7978 rs6000_emit_set_long_const (dest, c);
7979 break;
7981 default:
7982 gcc_unreachable ();
7985 insn = get_last_insn ();
7986 set = single_set (insn);
7987 if (! CONSTANT_P (SET_SRC (set)))
7988 set_unique_reg_note (insn, REG_EQUAL, GEN_INT (c));
7990 return true;
7993 /* Subroutine of rs6000_emit_set_const, handling PowerPC64 DImode.
7994 Output insns to set DEST equal to the constant C as a series of
7995 lis, ori and shl instructions. */
7997 static void
7998 rs6000_emit_set_long_const (rtx dest, HOST_WIDE_INT c)
8000 rtx temp;
8001 HOST_WIDE_INT ud1, ud2, ud3, ud4;
8003 ud1 = c & 0xffff;
8004 c = c >> 16;
8005 ud2 = c & 0xffff;
8006 c = c >> 16;
8007 ud3 = c & 0xffff;
8008 c = c >> 16;
8009 ud4 = c & 0xffff;
8011 if ((ud4 == 0xffff && ud3 == 0xffff && ud2 == 0xffff && (ud1 & 0x8000))
8012 || (ud4 == 0 && ud3 == 0 && ud2 == 0 && ! (ud1 & 0x8000)))
8013 emit_move_insn (dest, GEN_INT ((ud1 ^ 0x8000) - 0x8000));
8015 else if ((ud4 == 0xffff && ud3 == 0xffff && (ud2 & 0x8000))
8016 || (ud4 == 0 && ud3 == 0 && ! (ud2 & 0x8000)))
8018 temp = !can_create_pseudo_p () ? dest : gen_reg_rtx (DImode);
8020 emit_move_insn (ud1 != 0 ? copy_rtx (temp) : dest,
8021 GEN_INT (((ud2 << 16) ^ 0x80000000) - 0x80000000));
8022 if (ud1 != 0)
8023 emit_move_insn (dest,
8024 gen_rtx_IOR (DImode, copy_rtx (temp),
8025 GEN_INT (ud1)));
8027 else if (ud3 == 0 && ud4 == 0)
8029 temp = !can_create_pseudo_p () ? dest : gen_reg_rtx (DImode);
8031 gcc_assert (ud2 & 0x8000);
8032 emit_move_insn (copy_rtx (temp),
8033 GEN_INT (((ud2 << 16) ^ 0x80000000) - 0x80000000));
8034 if (ud1 != 0)
8035 emit_move_insn (copy_rtx (temp),
8036 gen_rtx_IOR (DImode, copy_rtx (temp),
8037 GEN_INT (ud1)));
8038 emit_move_insn (dest,
8039 gen_rtx_ZERO_EXTEND (DImode,
8040 gen_lowpart (SImode,
8041 copy_rtx (temp))));
8043 else if ((ud4 == 0xffff && (ud3 & 0x8000))
8044 || (ud4 == 0 && ! (ud3 & 0x8000)))
8046 temp = !can_create_pseudo_p () ? dest : gen_reg_rtx (DImode);
8048 emit_move_insn (copy_rtx (temp),
8049 GEN_INT (((ud3 << 16) ^ 0x80000000) - 0x80000000));
8050 if (ud2 != 0)
8051 emit_move_insn (copy_rtx (temp),
8052 gen_rtx_IOR (DImode, copy_rtx (temp),
8053 GEN_INT (ud2)));
8054 emit_move_insn (ud1 != 0 ? copy_rtx (temp) : dest,
8055 gen_rtx_ASHIFT (DImode, copy_rtx (temp),
8056 GEN_INT (16)));
8057 if (ud1 != 0)
8058 emit_move_insn (dest,
8059 gen_rtx_IOR (DImode, copy_rtx (temp),
8060 GEN_INT (ud1)));
8062 else
8064 temp = !can_create_pseudo_p () ? dest : gen_reg_rtx (DImode);
8066 emit_move_insn (copy_rtx (temp),
8067 GEN_INT (((ud4 << 16) ^ 0x80000000) - 0x80000000));
8068 if (ud3 != 0)
8069 emit_move_insn (copy_rtx (temp),
8070 gen_rtx_IOR (DImode, copy_rtx (temp),
8071 GEN_INT (ud3)));
8073 emit_move_insn (ud2 != 0 || ud1 != 0 ? copy_rtx (temp) : dest,
8074 gen_rtx_ASHIFT (DImode, copy_rtx (temp),
8075 GEN_INT (32)));
8076 if (ud2 != 0)
8077 emit_move_insn (ud1 != 0 ? copy_rtx (temp) : dest,
8078 gen_rtx_IOR (DImode, copy_rtx (temp),
8079 GEN_INT (ud2 << 16)));
8080 if (ud1 != 0)
8081 emit_move_insn (dest,
8082 gen_rtx_IOR (DImode, copy_rtx (temp),
8083 GEN_INT (ud1)));
8087 /* Helper for the following. Get rid of [r+r] memory refs
8088 in cases where it won't work (TImode, TFmode, TDmode, PTImode). */
8090 static void
8091 rs6000_eliminate_indexed_memrefs (rtx operands[2])
8093 if (reload_in_progress)
8094 return;
8096 if (GET_CODE (operands[0]) == MEM
8097 && GET_CODE (XEXP (operands[0], 0)) != REG
8098 && ! legitimate_constant_pool_address_p (XEXP (operands[0], 0),
8099 GET_MODE (operands[0]), false))
8100 operands[0]
8101 = replace_equiv_address (operands[0],
8102 copy_addr_to_reg (XEXP (operands[0], 0)));
8104 if (GET_CODE (operands[1]) == MEM
8105 && GET_CODE (XEXP (operands[1], 0)) != REG
8106 && ! legitimate_constant_pool_address_p (XEXP (operands[1], 0),
8107 GET_MODE (operands[1]), false))
8108 operands[1]
8109 = replace_equiv_address (operands[1],
8110 copy_addr_to_reg (XEXP (operands[1], 0)));
8113 /* Generate a vector of constants to permute MODE for a little-endian
8114 storage operation by swapping the two halves of a vector. */
8115 static rtvec
8116 rs6000_const_vec (enum machine_mode mode)
8118 int i, subparts;
8119 rtvec v;
8121 switch (mode)
8123 case V1TImode:
8124 subparts = 1;
8125 break;
8126 case V2DFmode:
8127 case V2DImode:
8128 subparts = 2;
8129 break;
8130 case V4SFmode:
8131 case V4SImode:
8132 subparts = 4;
8133 break;
8134 case V8HImode:
8135 subparts = 8;
8136 break;
8137 case V16QImode:
8138 subparts = 16;
8139 break;
8140 default:
8141 gcc_unreachable();
8144 v = rtvec_alloc (subparts);
8146 for (i = 0; i < subparts / 2; ++i)
8147 RTVEC_ELT (v, i) = gen_rtx_CONST_INT (DImode, i + subparts / 2);
8148 for (i = subparts / 2; i < subparts; ++i)
8149 RTVEC_ELT (v, i) = gen_rtx_CONST_INT (DImode, i - subparts / 2);
8151 return v;
8154 /* Generate a permute rtx that represents an lxvd2x, stxvd2x, or xxpermdi
8155 for a VSX load or store operation. */
8157 rs6000_gen_le_vsx_permute (rtx source, enum machine_mode mode)
8159 rtx par = gen_rtx_PARALLEL (VOIDmode, rs6000_const_vec (mode));
8160 return gen_rtx_VEC_SELECT (mode, source, par);
8163 /* Emit a little-endian load from vector memory location SOURCE to VSX
8164 register DEST in mode MODE. The load is done with two permuting
8165 insn's that represent an lxvd2x and xxpermdi. */
8166 void
8167 rs6000_emit_le_vsx_load (rtx dest, rtx source, enum machine_mode mode)
8169 rtx tmp, permute_mem, permute_reg;
8171 /* Use V2DImode to do swaps of types with 128-bit scalare parts (TImode,
8172 V1TImode). */
8173 if (mode == TImode || mode == V1TImode)
8175 mode = V2DImode;
8176 dest = gen_lowpart (V2DImode, dest);
8177 source = adjust_address (source, V2DImode, 0);
8180 tmp = can_create_pseudo_p () ? gen_reg_rtx_and_attrs (dest) : dest;
8181 permute_mem = rs6000_gen_le_vsx_permute (source, mode);
8182 permute_reg = rs6000_gen_le_vsx_permute (tmp, mode);
8183 emit_insn (gen_rtx_SET (VOIDmode, tmp, permute_mem));
8184 emit_insn (gen_rtx_SET (VOIDmode, dest, permute_reg));
8187 /* Emit a little-endian store to vector memory location DEST from VSX
8188 register SOURCE in mode MODE. The store is done with two permuting
8189 insn's that represent an xxpermdi and an stxvd2x. */
8190 void
8191 rs6000_emit_le_vsx_store (rtx dest, rtx source, enum machine_mode mode)
8193 rtx tmp, permute_src, permute_tmp;
8195 /* Use V2DImode to do swaps of types with 128-bit scalare parts (TImode,
8196 V1TImode). */
8197 if (mode == TImode || mode == V1TImode)
8199 mode = V2DImode;
8200 dest = adjust_address (dest, V2DImode, 0);
8201 source = gen_lowpart (V2DImode, source);
8204 tmp = can_create_pseudo_p () ? gen_reg_rtx_and_attrs (source) : source;
8205 permute_src = rs6000_gen_le_vsx_permute (source, mode);
8206 permute_tmp = rs6000_gen_le_vsx_permute (tmp, mode);
8207 emit_insn (gen_rtx_SET (VOIDmode, tmp, permute_src));
8208 emit_insn (gen_rtx_SET (VOIDmode, dest, permute_tmp));
8211 /* Emit a sequence representing a little-endian VSX load or store,
8212 moving data from SOURCE to DEST in mode MODE. This is done
8213 separately from rs6000_emit_move to ensure it is called only
8214 during expand. LE VSX loads and stores introduced later are
8215 handled with a split. The expand-time RTL generation allows
8216 us to optimize away redundant pairs of register-permutes. */
8217 void
8218 rs6000_emit_le_vsx_move (rtx dest, rtx source, enum machine_mode mode)
8220 gcc_assert (!BYTES_BIG_ENDIAN
8221 && VECTOR_MEM_VSX_P (mode)
8222 && !gpr_or_gpr_p (dest, source)
8223 && (MEM_P (source) ^ MEM_P (dest)));
8225 if (MEM_P (source))
8227 gcc_assert (REG_P (dest) || GET_CODE (dest) == SUBREG);
8228 rs6000_emit_le_vsx_load (dest, source, mode);
8230 else
8232 if (!REG_P (source))
8233 source = force_reg (mode, source);
8234 rs6000_emit_le_vsx_store (dest, source, mode);
8238 /* Emit a move from SOURCE to DEST in mode MODE. */
8239 void
8240 rs6000_emit_move (rtx dest, rtx source, enum machine_mode mode)
8242 rtx operands[2];
8243 operands[0] = dest;
8244 operands[1] = source;
8246 if (TARGET_DEBUG_ADDR)
8248 fprintf (stderr,
8249 "\nrs6000_emit_move: mode = %s, reload_in_progress = %d, "
8250 "reload_completed = %d, can_create_pseudos = %d.\ndest:\n",
8251 GET_MODE_NAME (mode),
8252 reload_in_progress,
8253 reload_completed,
8254 can_create_pseudo_p ());
8255 debug_rtx (dest);
8256 fprintf (stderr, "source:\n");
8257 debug_rtx (source);
8260 /* Sanity checks. Check that we get CONST_DOUBLE only when we should. */
8261 if (CONST_WIDE_INT_P (operands[1])
8262 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
8264 /* This should be fixed with the introduction of CONST_WIDE_INT. */
8265 gcc_unreachable ();
8268 /* Check if GCC is setting up a block move that will end up using FP
8269 registers as temporaries. We must make sure this is acceptable. */
8270 if (GET_CODE (operands[0]) == MEM
8271 && GET_CODE (operands[1]) == MEM
8272 && mode == DImode
8273 && (SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[0]))
8274 || SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[1])))
8275 && ! (SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[0]) > 32
8276 ? 32 : MEM_ALIGN (operands[0])))
8277 || SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[1]) > 32
8278 ? 32
8279 : MEM_ALIGN (operands[1]))))
8280 && ! MEM_VOLATILE_P (operands [0])
8281 && ! MEM_VOLATILE_P (operands [1]))
8283 emit_move_insn (adjust_address (operands[0], SImode, 0),
8284 adjust_address (operands[1], SImode, 0));
8285 emit_move_insn (adjust_address (copy_rtx (operands[0]), SImode, 4),
8286 adjust_address (copy_rtx (operands[1]), SImode, 4));
8287 return;
8290 if (can_create_pseudo_p () && GET_CODE (operands[0]) == MEM
8291 && !gpc_reg_operand (operands[1], mode))
8292 operands[1] = force_reg (mode, operands[1]);
8294 /* Recognize the case where operand[1] is a reference to thread-local
8295 data and load its address to a register. */
8296 if (tls_referenced_p (operands[1]))
8298 enum tls_model model;
8299 rtx tmp = operands[1];
8300 rtx addend = NULL;
8302 if (GET_CODE (tmp) == CONST && GET_CODE (XEXP (tmp, 0)) == PLUS)
8304 addend = XEXP (XEXP (tmp, 0), 1);
8305 tmp = XEXP (XEXP (tmp, 0), 0);
8308 gcc_assert (GET_CODE (tmp) == SYMBOL_REF);
8309 model = SYMBOL_REF_TLS_MODEL (tmp);
8310 gcc_assert (model != 0);
8312 tmp = rs6000_legitimize_tls_address (tmp, model);
8313 if (addend)
8315 tmp = gen_rtx_PLUS (mode, tmp, addend);
8316 tmp = force_operand (tmp, operands[0]);
8318 operands[1] = tmp;
8321 /* Handle the case where reload calls us with an invalid address. */
8322 if (reload_in_progress && mode == Pmode
8323 && (! general_operand (operands[1], mode)
8324 || ! nonimmediate_operand (operands[0], mode)))
8325 goto emit_set;
8327 /* 128-bit constant floating-point values on Darwin should really be
8328 loaded as two parts. */
8329 if (!TARGET_IEEEQUAD && TARGET_LONG_DOUBLE_128
8330 && mode == TFmode && GET_CODE (operands[1]) == CONST_DOUBLE)
8332 rs6000_emit_move (simplify_gen_subreg (DFmode, operands[0], mode, 0),
8333 simplify_gen_subreg (DFmode, operands[1], mode, 0),
8334 DFmode);
8335 rs6000_emit_move (simplify_gen_subreg (DFmode, operands[0], mode,
8336 GET_MODE_SIZE (DFmode)),
8337 simplify_gen_subreg (DFmode, operands[1], mode,
8338 GET_MODE_SIZE (DFmode)),
8339 DFmode);
8340 return;
8343 if (reload_in_progress && cfun->machine->sdmode_stack_slot != NULL_RTX)
8344 cfun->machine->sdmode_stack_slot =
8345 eliminate_regs (cfun->machine->sdmode_stack_slot, VOIDmode, NULL_RTX);
8348 /* Transform (p0:DD, (SUBREG:DD p1:SD)) to ((SUBREG:SD p0:DD),
8349 p1:SD) if p1 is not of floating point class and p0 is spilled as
8350 we can have no analogous movsd_store for this. */
8351 if (lra_in_progress && mode == DDmode
8352 && REG_P (operands[0]) && REGNO (operands[0]) >= FIRST_PSEUDO_REGISTER
8353 && reg_preferred_class (REGNO (operands[0])) == NO_REGS
8354 && GET_CODE (operands[1]) == SUBREG && REG_P (SUBREG_REG (operands[1]))
8355 && GET_MODE (SUBREG_REG (operands[1])) == SDmode)
8357 enum reg_class cl;
8358 int regno = REGNO (SUBREG_REG (operands[1]));
8360 if (regno >= FIRST_PSEUDO_REGISTER)
8362 cl = reg_preferred_class (regno);
8363 regno = cl == NO_REGS ? -1 : ira_class_hard_regs[cl][1];
8365 if (regno >= 0 && ! FP_REGNO_P (regno))
8367 mode = SDmode;
8368 operands[0] = gen_lowpart_SUBREG (SDmode, operands[0]);
8369 operands[1] = SUBREG_REG (operands[1]);
8372 if (lra_in_progress
8373 && mode == SDmode
8374 && REG_P (operands[0]) && REGNO (operands[0]) >= FIRST_PSEUDO_REGISTER
8375 && reg_preferred_class (REGNO (operands[0])) == NO_REGS
8376 && (REG_P (operands[1])
8377 || (GET_CODE (operands[1]) == SUBREG
8378 && REG_P (SUBREG_REG (operands[1])))))
8380 int regno = REGNO (GET_CODE (operands[1]) == SUBREG
8381 ? SUBREG_REG (operands[1]) : operands[1]);
8382 enum reg_class cl;
8384 if (regno >= FIRST_PSEUDO_REGISTER)
8386 cl = reg_preferred_class (regno);
8387 gcc_assert (cl != NO_REGS);
8388 regno = ira_class_hard_regs[cl][0];
8390 if (FP_REGNO_P (regno))
8392 if (GET_MODE (operands[0]) != DDmode)
8393 operands[0] = gen_rtx_SUBREG (DDmode, operands[0], 0);
8394 emit_insn (gen_movsd_store (operands[0], operands[1]));
8396 else if (INT_REGNO_P (regno))
8397 emit_insn (gen_movsd_hardfloat (operands[0], operands[1]));
8398 else
8399 gcc_unreachable();
8400 return;
8402 /* Transform ((SUBREG:DD p0:SD), p1:DD) to (p0:SD, (SUBREG:SD
8403 p:DD)) if p0 is not of floating point class and p1 is spilled as
8404 we can have no analogous movsd_load for this. */
8405 if (lra_in_progress && mode == DDmode
8406 && GET_CODE (operands[0]) == SUBREG && REG_P (SUBREG_REG (operands[0]))
8407 && GET_MODE (SUBREG_REG (operands[0])) == SDmode
8408 && REG_P (operands[1]) && REGNO (operands[1]) >= FIRST_PSEUDO_REGISTER
8409 && reg_preferred_class (REGNO (operands[1])) == NO_REGS)
8411 enum reg_class cl;
8412 int regno = REGNO (SUBREG_REG (operands[0]));
8414 if (regno >= FIRST_PSEUDO_REGISTER)
8416 cl = reg_preferred_class (regno);
8417 regno = cl == NO_REGS ? -1 : ira_class_hard_regs[cl][0];
8419 if (regno >= 0 && ! FP_REGNO_P (regno))
8421 mode = SDmode;
8422 operands[0] = SUBREG_REG (operands[0]);
8423 operands[1] = gen_lowpart_SUBREG (SDmode, operands[1]);
8426 if (lra_in_progress
8427 && mode == SDmode
8428 && (REG_P (operands[0])
8429 || (GET_CODE (operands[0]) == SUBREG
8430 && REG_P (SUBREG_REG (operands[0]))))
8431 && REG_P (operands[1]) && REGNO (operands[1]) >= FIRST_PSEUDO_REGISTER
8432 && reg_preferred_class (REGNO (operands[1])) == NO_REGS)
8434 int regno = REGNO (GET_CODE (operands[0]) == SUBREG
8435 ? SUBREG_REG (operands[0]) : operands[0]);
8436 enum reg_class cl;
8438 if (regno >= FIRST_PSEUDO_REGISTER)
8440 cl = reg_preferred_class (regno);
8441 gcc_assert (cl != NO_REGS);
8442 regno = ira_class_hard_regs[cl][0];
8444 if (FP_REGNO_P (regno))
8446 if (GET_MODE (operands[1]) != DDmode)
8447 operands[1] = gen_rtx_SUBREG (DDmode, operands[1], 0);
8448 emit_insn (gen_movsd_load (operands[0], operands[1]));
8450 else if (INT_REGNO_P (regno))
8451 emit_insn (gen_movsd_hardfloat (operands[0], operands[1]));
8452 else
8453 gcc_unreachable();
8454 return;
8457 if (reload_in_progress
8458 && mode == SDmode
8459 && cfun->machine->sdmode_stack_slot != NULL_RTX
8460 && MEM_P (operands[0])
8461 && rtx_equal_p (operands[0], cfun->machine->sdmode_stack_slot)
8462 && REG_P (operands[1]))
8464 if (FP_REGNO_P (REGNO (operands[1])))
8466 rtx mem = adjust_address_nv (operands[0], DDmode, 0);
8467 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
8468 emit_insn (gen_movsd_store (mem, operands[1]));
8470 else if (INT_REGNO_P (REGNO (operands[1])))
8472 rtx mem = operands[0];
8473 if (BYTES_BIG_ENDIAN)
8474 mem = adjust_address_nv (mem, mode, 4);
8475 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
8476 emit_insn (gen_movsd_hardfloat (mem, operands[1]));
8478 else
8479 gcc_unreachable();
8480 return;
8482 if (reload_in_progress
8483 && mode == SDmode
8484 && REG_P (operands[0])
8485 && MEM_P (operands[1])
8486 && cfun->machine->sdmode_stack_slot != NULL_RTX
8487 && rtx_equal_p (operands[1], cfun->machine->sdmode_stack_slot))
8489 if (FP_REGNO_P (REGNO (operands[0])))
8491 rtx mem = adjust_address_nv (operands[1], DDmode, 0);
8492 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
8493 emit_insn (gen_movsd_load (operands[0], mem));
8495 else if (INT_REGNO_P (REGNO (operands[0])))
8497 rtx mem = operands[1];
8498 if (BYTES_BIG_ENDIAN)
8499 mem = adjust_address_nv (mem, mode, 4);
8500 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
8501 emit_insn (gen_movsd_hardfloat (operands[0], mem));
8503 else
8504 gcc_unreachable();
8505 return;
8508 /* FIXME: In the long term, this switch statement should go away
8509 and be replaced by a sequence of tests based on things like
8510 mode == Pmode. */
8511 switch (mode)
8513 case HImode:
8514 case QImode:
8515 if (CONSTANT_P (operands[1])
8516 && GET_CODE (operands[1]) != CONST_INT)
8517 operands[1] = force_const_mem (mode, operands[1]);
8518 break;
8520 case TFmode:
8521 case TDmode:
8522 rs6000_eliminate_indexed_memrefs (operands);
8523 /* fall through */
8525 case DFmode:
8526 case DDmode:
8527 case SFmode:
8528 case SDmode:
8529 if (CONSTANT_P (operands[1])
8530 && ! easy_fp_constant (operands[1], mode))
8531 operands[1] = force_const_mem (mode, operands[1]);
8532 break;
8534 case V16QImode:
8535 case V8HImode:
8536 case V4SFmode:
8537 case V4SImode:
8538 case V4HImode:
8539 case V2SFmode:
8540 case V2SImode:
8541 case V1DImode:
8542 case V2DFmode:
8543 case V2DImode:
8544 case V1TImode:
8545 if (CONSTANT_P (operands[1])
8546 && !easy_vector_constant (operands[1], mode))
8547 operands[1] = force_const_mem (mode, operands[1]);
8548 break;
8550 case SImode:
8551 case DImode:
8552 /* Use default pattern for address of ELF small data */
8553 if (TARGET_ELF
8554 && mode == Pmode
8555 && DEFAULT_ABI == ABI_V4
8556 && (GET_CODE (operands[1]) == SYMBOL_REF
8557 || GET_CODE (operands[1]) == CONST)
8558 && small_data_operand (operands[1], mode))
8560 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
8561 return;
8564 if (DEFAULT_ABI == ABI_V4
8565 && mode == Pmode && mode == SImode
8566 && flag_pic == 1 && got_operand (operands[1], mode))
8568 emit_insn (gen_movsi_got (operands[0], operands[1]));
8569 return;
8572 if ((TARGET_ELF || DEFAULT_ABI == ABI_DARWIN)
8573 && TARGET_NO_TOC
8574 && ! flag_pic
8575 && mode == Pmode
8576 && CONSTANT_P (operands[1])
8577 && GET_CODE (operands[1]) != HIGH
8578 && GET_CODE (operands[1]) != CONST_INT)
8580 rtx target = (!can_create_pseudo_p ()
8581 ? operands[0]
8582 : gen_reg_rtx (mode));
8584 /* If this is a function address on -mcall-aixdesc,
8585 convert it to the address of the descriptor. */
8586 if (DEFAULT_ABI == ABI_AIX
8587 && GET_CODE (operands[1]) == SYMBOL_REF
8588 && XSTR (operands[1], 0)[0] == '.')
8590 const char *name = XSTR (operands[1], 0);
8591 rtx new_ref;
8592 while (*name == '.')
8593 name++;
8594 new_ref = gen_rtx_SYMBOL_REF (Pmode, name);
8595 CONSTANT_POOL_ADDRESS_P (new_ref)
8596 = CONSTANT_POOL_ADDRESS_P (operands[1]);
8597 SYMBOL_REF_FLAGS (new_ref) = SYMBOL_REF_FLAGS (operands[1]);
8598 SYMBOL_REF_USED (new_ref) = SYMBOL_REF_USED (operands[1]);
8599 SYMBOL_REF_DATA (new_ref) = SYMBOL_REF_DATA (operands[1]);
8600 operands[1] = new_ref;
8603 if (DEFAULT_ABI == ABI_DARWIN)
8605 #if TARGET_MACHO
8606 if (MACHO_DYNAMIC_NO_PIC_P)
8608 /* Take care of any required data indirection. */
8609 operands[1] = rs6000_machopic_legitimize_pic_address (
8610 operands[1], mode, operands[0]);
8611 if (operands[0] != operands[1])
8612 emit_insn (gen_rtx_SET (VOIDmode,
8613 operands[0], operands[1]));
8614 return;
8616 #endif
8617 emit_insn (gen_macho_high (target, operands[1]));
8618 emit_insn (gen_macho_low (operands[0], target, operands[1]));
8619 return;
8622 emit_insn (gen_elf_high (target, operands[1]));
8623 emit_insn (gen_elf_low (operands[0], target, operands[1]));
8624 return;
8627 /* If this is a SYMBOL_REF that refers to a constant pool entry,
8628 and we have put it in the TOC, we just need to make a TOC-relative
8629 reference to it. */
8630 if (TARGET_TOC
8631 && GET_CODE (operands[1]) == SYMBOL_REF
8632 && use_toc_relative_ref (operands[1]))
8633 operands[1] = create_TOC_reference (operands[1], operands[0]);
8634 else if (mode == Pmode
8635 && CONSTANT_P (operands[1])
8636 && GET_CODE (operands[1]) != HIGH
8637 && ((GET_CODE (operands[1]) != CONST_INT
8638 && ! easy_fp_constant (operands[1], mode))
8639 || (GET_CODE (operands[1]) == CONST_INT
8640 && (num_insns_constant (operands[1], mode)
8641 > (TARGET_CMODEL != CMODEL_SMALL ? 3 : 2)))
8642 || (GET_CODE (operands[0]) == REG
8643 && FP_REGNO_P (REGNO (operands[0]))))
8644 && !toc_relative_expr_p (operands[1], false)
8645 && (TARGET_CMODEL == CMODEL_SMALL
8646 || can_create_pseudo_p ()
8647 || (REG_P (operands[0])
8648 && INT_REG_OK_FOR_BASE_P (operands[0], true))))
8651 #if TARGET_MACHO
8652 /* Darwin uses a special PIC legitimizer. */
8653 if (DEFAULT_ABI == ABI_DARWIN && MACHOPIC_INDIRECT)
8655 operands[1] =
8656 rs6000_machopic_legitimize_pic_address (operands[1], mode,
8657 operands[0]);
8658 if (operands[0] != operands[1])
8659 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
8660 return;
8662 #endif
8664 /* If we are to limit the number of things we put in the TOC and
8665 this is a symbol plus a constant we can add in one insn,
8666 just put the symbol in the TOC and add the constant. Don't do
8667 this if reload is in progress. */
8668 if (GET_CODE (operands[1]) == CONST
8669 && TARGET_NO_SUM_IN_TOC && ! reload_in_progress
8670 && GET_CODE (XEXP (operands[1], 0)) == PLUS
8671 && add_operand (XEXP (XEXP (operands[1], 0), 1), mode)
8672 && (GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == LABEL_REF
8673 || GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == SYMBOL_REF)
8674 && ! side_effects_p (operands[0]))
8676 rtx sym =
8677 force_const_mem (mode, XEXP (XEXP (operands[1], 0), 0));
8678 rtx other = XEXP (XEXP (operands[1], 0), 1);
8680 sym = force_reg (mode, sym);
8681 emit_insn (gen_add3_insn (operands[0], sym, other));
8682 return;
8685 operands[1] = force_const_mem (mode, operands[1]);
8687 if (TARGET_TOC
8688 && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
8689 && constant_pool_expr_p (XEXP (operands[1], 0))
8690 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (
8691 get_pool_constant (XEXP (operands[1], 0)),
8692 get_pool_mode (XEXP (operands[1], 0))))
8694 rtx tocref = create_TOC_reference (XEXP (operands[1], 0),
8695 operands[0]);
8696 operands[1] = gen_const_mem (mode, tocref);
8697 set_mem_alias_set (operands[1], get_TOC_alias_set ());
8700 break;
8702 case TImode:
8703 if (!VECTOR_MEM_VSX_P (TImode))
8704 rs6000_eliminate_indexed_memrefs (operands);
8705 break;
8707 case PTImode:
8708 rs6000_eliminate_indexed_memrefs (operands);
8709 break;
8711 default:
8712 fatal_insn ("bad move", gen_rtx_SET (VOIDmode, dest, source));
8715 /* Above, we may have called force_const_mem which may have returned
8716 an invalid address. If we can, fix this up; otherwise, reload will
8717 have to deal with it. */
8718 if (GET_CODE (operands[1]) == MEM && ! reload_in_progress)
8719 operands[1] = validize_mem (operands[1]);
8721 emit_set:
8722 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
8725 /* Return true if a structure, union or array containing FIELD should be
8726 accessed using `BLKMODE'.
8728 For the SPE, simd types are V2SI, and gcc can be tempted to put the
8729 entire thing in a DI and use subregs to access the internals.
8730 store_bit_field() will force (subreg:DI (reg:V2SI x))'s to the
8731 back-end. Because a single GPR can hold a V2SI, but not a DI, the
8732 best thing to do is set structs to BLKmode and avoid Severe Tire
8733 Damage.
8735 On e500 v2, DF and DI modes suffer from the same anomaly. DF can
8736 fit into 1, whereas DI still needs two. */
8738 static bool
8739 rs6000_member_type_forces_blk (const_tree field, enum machine_mode mode)
8741 return ((TARGET_SPE && TREE_CODE (TREE_TYPE (field)) == VECTOR_TYPE)
8742 || (TARGET_E500_DOUBLE && mode == DFmode));
8745 /* Nonzero if we can use a floating-point register to pass this arg. */
8746 #define USE_FP_FOR_ARG_P(CUM,MODE) \
8747 (SCALAR_FLOAT_MODE_P (MODE) \
8748 && (CUM)->fregno <= FP_ARG_MAX_REG \
8749 && TARGET_HARD_FLOAT && TARGET_FPRS)
8751 /* Nonzero if we can use an AltiVec register to pass this arg. */
8752 #define USE_ALTIVEC_FOR_ARG_P(CUM,MODE,NAMED) \
8753 (ALTIVEC_OR_VSX_VECTOR_MODE (MODE) \
8754 && (CUM)->vregno <= ALTIVEC_ARG_MAX_REG \
8755 && TARGET_ALTIVEC_ABI \
8756 && (NAMED))
8758 /* Walk down the type tree of TYPE counting consecutive base elements.
8759 If *MODEP is VOIDmode, then set it to the first valid floating point
8760 or vector type. If a non-floating point or vector type is found, or
8761 if a floating point or vector type that doesn't match a non-VOIDmode
8762 *MODEP is found, then return -1, otherwise return the count in the
8763 sub-tree. */
8765 static int
8766 rs6000_aggregate_candidate (const_tree type, enum machine_mode *modep)
8768 enum machine_mode mode;
8769 HOST_WIDE_INT size;
8771 switch (TREE_CODE (type))
8773 case REAL_TYPE:
8774 mode = TYPE_MODE (type);
8775 if (!SCALAR_FLOAT_MODE_P (mode))
8776 return -1;
8778 if (*modep == VOIDmode)
8779 *modep = mode;
8781 if (*modep == mode)
8782 return 1;
8784 break;
8786 case COMPLEX_TYPE:
8787 mode = TYPE_MODE (TREE_TYPE (type));
8788 if (!SCALAR_FLOAT_MODE_P (mode))
8789 return -1;
8791 if (*modep == VOIDmode)
8792 *modep = mode;
8794 if (*modep == mode)
8795 return 2;
8797 break;
8799 case VECTOR_TYPE:
8800 if (!TARGET_ALTIVEC_ABI || !TARGET_ALTIVEC)
8801 return -1;
8803 /* Use V4SImode as representative of all 128-bit vector types. */
8804 size = int_size_in_bytes (type);
8805 switch (size)
8807 case 16:
8808 mode = V4SImode;
8809 break;
8810 default:
8811 return -1;
8814 if (*modep == VOIDmode)
8815 *modep = mode;
8817 /* Vector modes are considered to be opaque: two vectors are
8818 equivalent for the purposes of being homogeneous aggregates
8819 if they are the same size. */
8820 if (*modep == mode)
8821 return 1;
8823 break;
8825 case ARRAY_TYPE:
8827 int count;
8828 tree index = TYPE_DOMAIN (type);
8830 /* Can't handle incomplete types nor sizes that are not
8831 fixed. */
8832 if (!COMPLETE_TYPE_P (type)
8833 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
8834 return -1;
8836 count = rs6000_aggregate_candidate (TREE_TYPE (type), modep);
8837 if (count == -1
8838 || !index
8839 || !TYPE_MAX_VALUE (index)
8840 || !tree_fits_uhwi_p (TYPE_MAX_VALUE (index))
8841 || !TYPE_MIN_VALUE (index)
8842 || !tree_fits_uhwi_p (TYPE_MIN_VALUE (index))
8843 || count < 0)
8844 return -1;
8846 count *= (1 + tree_to_uhwi (TYPE_MAX_VALUE (index))
8847 - tree_to_uhwi (TYPE_MIN_VALUE (index)));
8849 /* There must be no padding. */
8850 if (wi::ne_p (TYPE_SIZE (type), count * GET_MODE_BITSIZE (*modep)))
8851 return -1;
8853 return count;
8856 case RECORD_TYPE:
8858 int count = 0;
8859 int sub_count;
8860 tree field;
8862 /* Can't handle incomplete types nor sizes that are not
8863 fixed. */
8864 if (!COMPLETE_TYPE_P (type)
8865 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
8866 return -1;
8868 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
8870 if (TREE_CODE (field) != FIELD_DECL)
8871 continue;
8873 sub_count = rs6000_aggregate_candidate (TREE_TYPE (field), modep);
8874 if (sub_count < 0)
8875 return -1;
8876 count += sub_count;
8879 /* There must be no padding. */
8880 if (wi::ne_p (TYPE_SIZE (type), count * GET_MODE_BITSIZE (*modep)))
8881 return -1;
8883 return count;
8886 case UNION_TYPE:
8887 case QUAL_UNION_TYPE:
8889 /* These aren't very interesting except in a degenerate case. */
8890 int count = 0;
8891 int sub_count;
8892 tree field;
8894 /* Can't handle incomplete types nor sizes that are not
8895 fixed. */
8896 if (!COMPLETE_TYPE_P (type)
8897 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
8898 return -1;
8900 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
8902 if (TREE_CODE (field) != FIELD_DECL)
8903 continue;
8905 sub_count = rs6000_aggregate_candidate (TREE_TYPE (field), modep);
8906 if (sub_count < 0)
8907 return -1;
8908 count = count > sub_count ? count : sub_count;
8911 /* There must be no padding. */
8912 if (wi::ne_p (TYPE_SIZE (type), count * GET_MODE_BITSIZE (*modep)))
8913 return -1;
8915 return count;
8918 default:
8919 break;
8922 return -1;
8925 /* If an argument, whose type is described by TYPE and MODE, is a homogeneous
8926 float or vector aggregate that shall be passed in FP/vector registers
8927 according to the ELFv2 ABI, return the homogeneous element mode in
8928 *ELT_MODE and the number of elements in *N_ELTS, and return TRUE.
8930 Otherwise, set *ELT_MODE to MODE and *N_ELTS to 1, and return FALSE. */
8932 static bool
8933 rs6000_discover_homogeneous_aggregate (enum machine_mode mode, const_tree type,
8934 enum machine_mode *elt_mode,
8935 int *n_elts)
8937 /* Note that we do not accept complex types at the top level as
8938 homogeneous aggregates; these types are handled via the
8939 targetm.calls.split_complex_arg mechanism. Complex types
8940 can be elements of homogeneous aggregates, however. */
8941 if (DEFAULT_ABI == ABI_ELFv2 && type && AGGREGATE_TYPE_P (type))
8943 enum machine_mode field_mode = VOIDmode;
8944 int field_count = rs6000_aggregate_candidate (type, &field_mode);
8946 if (field_count > 0)
8948 int n_regs = (SCALAR_FLOAT_MODE_P (field_mode)?
8949 (GET_MODE_SIZE (field_mode) + 7) >> 3 : 1);
8951 /* The ELFv2 ABI allows homogeneous aggregates to occupy
8952 up to AGGR_ARG_NUM_REG registers. */
8953 if (field_count * n_regs <= AGGR_ARG_NUM_REG)
8955 if (elt_mode)
8956 *elt_mode = field_mode;
8957 if (n_elts)
8958 *n_elts = field_count;
8959 return true;
8964 if (elt_mode)
8965 *elt_mode = mode;
8966 if (n_elts)
8967 *n_elts = 1;
8968 return false;
8971 /* Return a nonzero value to say to return the function value in
8972 memory, just as large structures are always returned. TYPE will be
8973 the data type of the value, and FNTYPE will be the type of the
8974 function doing the returning, or @code{NULL} for libcalls.
8976 The AIX ABI for the RS/6000 specifies that all structures are
8977 returned in memory. The Darwin ABI does the same.
8979 For the Darwin 64 Bit ABI, a function result can be returned in
8980 registers or in memory, depending on the size of the return data
8981 type. If it is returned in registers, the value occupies the same
8982 registers as it would if it were the first and only function
8983 argument. Otherwise, the function places its result in memory at
8984 the location pointed to by GPR3.
8986 The SVR4 ABI specifies that structures <= 8 bytes are returned in r3/r4,
8987 but a draft put them in memory, and GCC used to implement the draft
8988 instead of the final standard. Therefore, aix_struct_return
8989 controls this instead of DEFAULT_ABI; V.4 targets needing backward
8990 compatibility can change DRAFT_V4_STRUCT_RET to override the
8991 default, and -m switches get the final word. See
8992 rs6000_option_override_internal for more details.
8994 The PPC32 SVR4 ABI uses IEEE double extended for long double, if 128-bit
8995 long double support is enabled. These values are returned in memory.
8997 int_size_in_bytes returns -1 for variable size objects, which go in
8998 memory always. The cast to unsigned makes -1 > 8. */
9000 static bool
9001 rs6000_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
9003 /* For the Darwin64 ABI, test if we can fit the return value in regs. */
9004 if (TARGET_MACHO
9005 && rs6000_darwin64_abi
9006 && TREE_CODE (type) == RECORD_TYPE
9007 && int_size_in_bytes (type) > 0)
9009 CUMULATIVE_ARGS valcum;
9010 rtx valret;
9012 valcum.words = 0;
9013 valcum.fregno = FP_ARG_MIN_REG;
9014 valcum.vregno = ALTIVEC_ARG_MIN_REG;
9015 /* Do a trial code generation as if this were going to be passed
9016 as an argument; if any part goes in memory, we return NULL. */
9017 valret = rs6000_darwin64_record_arg (&valcum, type, true, true);
9018 if (valret)
9019 return false;
9020 /* Otherwise fall through to more conventional ABI rules. */
9023 /* The ELFv2 ABI returns homogeneous VFP aggregates in registers */
9024 if (rs6000_discover_homogeneous_aggregate (TYPE_MODE (type), type,
9025 NULL, NULL))
9026 return false;
9028 /* The ELFv2 ABI returns aggregates up to 16B in registers */
9029 if (DEFAULT_ABI == ABI_ELFv2 && AGGREGATE_TYPE_P (type)
9030 && (unsigned HOST_WIDE_INT) int_size_in_bytes (type) <= 16)
9031 return false;
9033 if (AGGREGATE_TYPE_P (type)
9034 && (aix_struct_return
9035 || (unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 8))
9036 return true;
9038 /* Allow -maltivec -mabi=no-altivec without warning. Altivec vector
9039 modes only exist for GCC vector types if -maltivec. */
9040 if (TARGET_32BIT && !TARGET_ALTIVEC_ABI
9041 && ALTIVEC_VECTOR_MODE (TYPE_MODE (type)))
9042 return false;
9044 /* Return synthetic vectors in memory. */
9045 if (TREE_CODE (type) == VECTOR_TYPE
9046 && int_size_in_bytes (type) > (TARGET_ALTIVEC_ABI ? 16 : 8))
9048 static bool warned_for_return_big_vectors = false;
9049 if (!warned_for_return_big_vectors)
9051 warning (0, "GCC vector returned by reference: "
9052 "non-standard ABI extension with no compatibility guarantee");
9053 warned_for_return_big_vectors = true;
9055 return true;
9058 if (DEFAULT_ABI == ABI_V4 && TARGET_IEEEQUAD && TYPE_MODE (type) == TFmode)
9059 return true;
9061 return false;
9064 /* Specify whether values returned in registers should be at the most
9065 significant end of a register. We want aggregates returned by
9066 value to match the way aggregates are passed to functions. */
9068 static bool
9069 rs6000_return_in_msb (const_tree valtype)
9071 return (DEFAULT_ABI == ABI_ELFv2
9072 && BYTES_BIG_ENDIAN
9073 && AGGREGATE_TYPE_P (valtype)
9074 && FUNCTION_ARG_PADDING (TYPE_MODE (valtype), valtype) == upward);
9077 #ifdef HAVE_AS_GNU_ATTRIBUTE
9078 /* Return TRUE if a call to function FNDECL may be one that
9079 potentially affects the function calling ABI of the object file. */
9081 static bool
9082 call_ABI_of_interest (tree fndecl)
9084 if (symtab->state == EXPANSION)
9086 struct cgraph_node *c_node;
9088 /* Libcalls are always interesting. */
9089 if (fndecl == NULL_TREE)
9090 return true;
9092 /* Any call to an external function is interesting. */
9093 if (DECL_EXTERNAL (fndecl))
9094 return true;
9096 /* Interesting functions that we are emitting in this object file. */
9097 c_node = cgraph_node::get (fndecl);
9098 c_node = c_node->ultimate_alias_target ();
9099 return !c_node->only_called_directly_p ();
9101 return false;
9103 #endif
9105 /* Initialize a variable CUM of type CUMULATIVE_ARGS
9106 for a call to a function whose data type is FNTYPE.
9107 For a library call, FNTYPE is 0 and RETURN_MODE the return value mode.
9109 For incoming args we set the number of arguments in the prototype large
9110 so we never return a PARALLEL. */
9112 void
9113 init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype,
9114 rtx libname ATTRIBUTE_UNUSED, int incoming,
9115 int libcall, int n_named_args,
9116 tree fndecl ATTRIBUTE_UNUSED,
9117 enum machine_mode return_mode ATTRIBUTE_UNUSED)
9119 static CUMULATIVE_ARGS zero_cumulative;
9121 *cum = zero_cumulative;
9122 cum->words = 0;
9123 cum->fregno = FP_ARG_MIN_REG;
9124 cum->vregno = ALTIVEC_ARG_MIN_REG;
9125 cum->prototype = (fntype && prototype_p (fntype));
9126 cum->call_cookie = ((DEFAULT_ABI == ABI_V4 && libcall)
9127 ? CALL_LIBCALL : CALL_NORMAL);
9128 cum->sysv_gregno = GP_ARG_MIN_REG;
9129 cum->stdarg = stdarg_p (fntype);
9131 cum->nargs_prototype = 0;
9132 if (incoming || cum->prototype)
9133 cum->nargs_prototype = n_named_args;
9135 /* Check for a longcall attribute. */
9136 if ((!fntype && rs6000_default_long_calls)
9137 || (fntype
9138 && lookup_attribute ("longcall", TYPE_ATTRIBUTES (fntype))
9139 && !lookup_attribute ("shortcall", TYPE_ATTRIBUTES (fntype))))
9140 cum->call_cookie |= CALL_LONG;
9142 if (TARGET_DEBUG_ARG)
9144 fprintf (stderr, "\ninit_cumulative_args:");
9145 if (fntype)
9147 tree ret_type = TREE_TYPE (fntype);
9148 fprintf (stderr, " ret code = %s,",
9149 get_tree_code_name (TREE_CODE (ret_type)));
9152 if (cum->call_cookie & CALL_LONG)
9153 fprintf (stderr, " longcall,");
9155 fprintf (stderr, " proto = %d, nargs = %d\n",
9156 cum->prototype, cum->nargs_prototype);
9159 #ifdef HAVE_AS_GNU_ATTRIBUTE
9160 if (DEFAULT_ABI == ABI_V4)
9162 cum->escapes = call_ABI_of_interest (fndecl);
9163 if (cum->escapes)
9165 tree return_type;
9167 if (fntype)
9169 return_type = TREE_TYPE (fntype);
9170 return_mode = TYPE_MODE (return_type);
9172 else
9173 return_type = lang_hooks.types.type_for_mode (return_mode, 0);
9175 if (return_type != NULL)
9177 if (TREE_CODE (return_type) == RECORD_TYPE
9178 && TYPE_TRANSPARENT_AGGR (return_type))
9180 return_type = TREE_TYPE (first_field (return_type));
9181 return_mode = TYPE_MODE (return_type);
9183 if (AGGREGATE_TYPE_P (return_type)
9184 && ((unsigned HOST_WIDE_INT) int_size_in_bytes (return_type)
9185 <= 8))
9186 rs6000_returns_struct = true;
9188 if (SCALAR_FLOAT_MODE_P (return_mode))
9189 rs6000_passes_float = true;
9190 else if (ALTIVEC_OR_VSX_VECTOR_MODE (return_mode)
9191 || SPE_VECTOR_MODE (return_mode))
9192 rs6000_passes_vector = true;
9195 #endif
9197 if (fntype
9198 && !TARGET_ALTIVEC
9199 && TARGET_ALTIVEC_ABI
9200 && ALTIVEC_VECTOR_MODE (TYPE_MODE (TREE_TYPE (fntype))))
9202 error ("cannot return value in vector register because"
9203 " altivec instructions are disabled, use -maltivec"
9204 " to enable them");
9208 /* Return true if TYPE must be passed on the stack and not in registers. */
9210 static bool
9211 rs6000_must_pass_in_stack (enum machine_mode mode, const_tree type)
9213 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2 || TARGET_64BIT)
9214 return must_pass_in_stack_var_size (mode, type);
9215 else
9216 return must_pass_in_stack_var_size_or_pad (mode, type);
9219 /* If defined, a C expression which determines whether, and in which
9220 direction, to pad out an argument with extra space. The value
9221 should be of type `enum direction': either `upward' to pad above
9222 the argument, `downward' to pad below, or `none' to inhibit
9223 padding.
9225 For the AIX ABI structs are always stored left shifted in their
9226 argument slot. */
9228 enum direction
9229 function_arg_padding (enum machine_mode mode, const_tree type)
9231 #ifndef AGGREGATE_PADDING_FIXED
9232 #define AGGREGATE_PADDING_FIXED 0
9233 #endif
9234 #ifndef AGGREGATES_PAD_UPWARD_ALWAYS
9235 #define AGGREGATES_PAD_UPWARD_ALWAYS 0
9236 #endif
9238 if (!AGGREGATE_PADDING_FIXED)
9240 /* GCC used to pass structures of the same size as integer types as
9241 if they were in fact integers, ignoring FUNCTION_ARG_PADDING.
9242 i.e. Structures of size 1 or 2 (or 4 when TARGET_64BIT) were
9243 passed padded downward, except that -mstrict-align further
9244 muddied the water in that multi-component structures of 2 and 4
9245 bytes in size were passed padded upward.
9247 The following arranges for best compatibility with previous
9248 versions of gcc, but removes the -mstrict-align dependency. */
9249 if (BYTES_BIG_ENDIAN)
9251 HOST_WIDE_INT size = 0;
9253 if (mode == BLKmode)
9255 if (type && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
9256 size = int_size_in_bytes (type);
9258 else
9259 size = GET_MODE_SIZE (mode);
9261 if (size == 1 || size == 2 || size == 4)
9262 return downward;
9264 return upward;
9267 if (AGGREGATES_PAD_UPWARD_ALWAYS)
9269 if (type != 0 && AGGREGATE_TYPE_P (type))
9270 return upward;
9273 /* Fall back to the default. */
9274 return DEFAULT_FUNCTION_ARG_PADDING (mode, type);
9277 /* If defined, a C expression that gives the alignment boundary, in bits,
9278 of an argument with the specified mode and type. If it is not defined,
9279 PARM_BOUNDARY is used for all arguments.
9281 V.4 wants long longs and doubles to be double word aligned. Just
9282 testing the mode size is a boneheaded way to do this as it means
9283 that other types such as complex int are also double word aligned.
9284 However, we're stuck with this because changing the ABI might break
9285 existing library interfaces.
9287 Doubleword align SPE vectors.
9288 Quadword align Altivec/VSX vectors.
9289 Quadword align large synthetic vector types. */
9291 static unsigned int
9292 rs6000_function_arg_boundary (enum machine_mode mode, const_tree type)
9294 enum machine_mode elt_mode;
9295 int n_elts;
9297 rs6000_discover_homogeneous_aggregate (mode, type, &elt_mode, &n_elts);
9299 if (DEFAULT_ABI == ABI_V4
9300 && (GET_MODE_SIZE (mode) == 8
9301 || (TARGET_HARD_FLOAT
9302 && TARGET_FPRS
9303 && (mode == TFmode || mode == TDmode))))
9304 return 64;
9305 else if (SPE_VECTOR_MODE (mode)
9306 || (type && TREE_CODE (type) == VECTOR_TYPE
9307 && int_size_in_bytes (type) >= 8
9308 && int_size_in_bytes (type) < 16))
9309 return 64;
9310 else if (ALTIVEC_OR_VSX_VECTOR_MODE (elt_mode)
9311 || (type && TREE_CODE (type) == VECTOR_TYPE
9312 && int_size_in_bytes (type) >= 16))
9313 return 128;
9315 /* Aggregate types that need > 8 byte alignment are quadword-aligned
9316 in the parameter area in the ELFv2 ABI, and in the AIX ABI unless
9317 -mcompat-align-parm is used. */
9318 if (((DEFAULT_ABI == ABI_AIX && !rs6000_compat_align_parm)
9319 || DEFAULT_ABI == ABI_ELFv2)
9320 && type && TYPE_ALIGN (type) > 64)
9322 /* "Aggregate" means any AGGREGATE_TYPE except for single-element
9323 or homogeneous float/vector aggregates here. We already handled
9324 vector aggregates above, but still need to check for float here. */
9325 bool aggregate_p = (AGGREGATE_TYPE_P (type)
9326 && !SCALAR_FLOAT_MODE_P (elt_mode));
9328 /* We used to check for BLKmode instead of the above aggregate type
9329 check. Warn when this results in any difference to the ABI. */
9330 if (aggregate_p != (mode == BLKmode))
9332 static bool warned;
9333 if (!warned && warn_psabi)
9335 warned = true;
9336 inform (input_location,
9337 "the ABI of passing aggregates with %d-byte alignment"
9338 " has changed in GCC 5",
9339 (int) TYPE_ALIGN (type) / BITS_PER_UNIT);
9343 if (aggregate_p)
9344 return 128;
9347 /* Similar for the Darwin64 ABI. Note that for historical reasons we
9348 implement the "aggregate type" check as a BLKmode check here; this
9349 means certain aggregate types are in fact not aligned. */
9350 if (TARGET_MACHO && rs6000_darwin64_abi
9351 && mode == BLKmode
9352 && type && TYPE_ALIGN (type) > 64)
9353 return 128;
9355 return PARM_BOUNDARY;
9358 /* The offset in words to the start of the parameter save area. */
9360 static unsigned int
9361 rs6000_parm_offset (void)
9363 return (DEFAULT_ABI == ABI_V4 ? 2
9364 : DEFAULT_ABI == ABI_ELFv2 ? 4
9365 : 6);
9368 /* For a function parm of MODE and TYPE, return the starting word in
9369 the parameter area. NWORDS of the parameter area are already used. */
9371 static unsigned int
9372 rs6000_parm_start (enum machine_mode mode, const_tree type,
9373 unsigned int nwords)
9375 unsigned int align;
9377 align = rs6000_function_arg_boundary (mode, type) / PARM_BOUNDARY - 1;
9378 return nwords + (-(rs6000_parm_offset () + nwords) & align);
9381 /* Compute the size (in words) of a function argument. */
9383 static unsigned long
9384 rs6000_arg_size (enum machine_mode mode, const_tree type)
9386 unsigned long size;
9388 if (mode != BLKmode)
9389 size = GET_MODE_SIZE (mode);
9390 else
9391 size = int_size_in_bytes (type);
9393 if (TARGET_32BIT)
9394 return (size + 3) >> 2;
9395 else
9396 return (size + 7) >> 3;
9399 /* Use this to flush pending int fields. */
9401 static void
9402 rs6000_darwin64_record_arg_advance_flush (CUMULATIVE_ARGS *cum,
9403 HOST_WIDE_INT bitpos, int final)
9405 unsigned int startbit, endbit;
9406 int intregs, intoffset;
9407 enum machine_mode mode;
9409 /* Handle the situations where a float is taking up the first half
9410 of the GPR, and the other half is empty (typically due to
9411 alignment restrictions). We can detect this by a 8-byte-aligned
9412 int field, or by seeing that this is the final flush for this
9413 argument. Count the word and continue on. */
9414 if (cum->floats_in_gpr == 1
9415 && (cum->intoffset % 64 == 0
9416 || (cum->intoffset == -1 && final)))
9418 cum->words++;
9419 cum->floats_in_gpr = 0;
9422 if (cum->intoffset == -1)
9423 return;
9425 intoffset = cum->intoffset;
9426 cum->intoffset = -1;
9427 cum->floats_in_gpr = 0;
9429 if (intoffset % BITS_PER_WORD != 0)
9431 mode = mode_for_size (BITS_PER_WORD - intoffset % BITS_PER_WORD,
9432 MODE_INT, 0);
9433 if (mode == BLKmode)
9435 /* We couldn't find an appropriate mode, which happens,
9436 e.g., in packed structs when there are 3 bytes to load.
9437 Back intoffset back to the beginning of the word in this
9438 case. */
9439 intoffset = intoffset & -BITS_PER_WORD;
9443 startbit = intoffset & -BITS_PER_WORD;
9444 endbit = (bitpos + BITS_PER_WORD - 1) & -BITS_PER_WORD;
9445 intregs = (endbit - startbit) / BITS_PER_WORD;
9446 cum->words += intregs;
9447 /* words should be unsigned. */
9448 if ((unsigned)cum->words < (endbit/BITS_PER_WORD))
9450 int pad = (endbit/BITS_PER_WORD) - cum->words;
9451 cum->words += pad;
9455 /* The darwin64 ABI calls for us to recurse down through structs,
9456 looking for elements passed in registers. Unfortunately, we have
9457 to track int register count here also because of misalignments
9458 in powerpc alignment mode. */
9460 static void
9461 rs6000_darwin64_record_arg_advance_recurse (CUMULATIVE_ARGS *cum,
9462 const_tree type,
9463 HOST_WIDE_INT startbitpos)
9465 tree f;
9467 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
9468 if (TREE_CODE (f) == FIELD_DECL)
9470 HOST_WIDE_INT bitpos = startbitpos;
9471 tree ftype = TREE_TYPE (f);
9472 enum machine_mode mode;
9473 if (ftype == error_mark_node)
9474 continue;
9475 mode = TYPE_MODE (ftype);
9477 if (DECL_SIZE (f) != 0
9478 && tree_fits_uhwi_p (bit_position (f)))
9479 bitpos += int_bit_position (f);
9481 /* ??? FIXME: else assume zero offset. */
9483 if (TREE_CODE (ftype) == RECORD_TYPE)
9484 rs6000_darwin64_record_arg_advance_recurse (cum, ftype, bitpos);
9485 else if (USE_FP_FOR_ARG_P (cum, mode))
9487 unsigned n_fpregs = (GET_MODE_SIZE (mode) + 7) >> 3;
9488 rs6000_darwin64_record_arg_advance_flush (cum, bitpos, 0);
9489 cum->fregno += n_fpregs;
9490 /* Single-precision floats present a special problem for
9491 us, because they are smaller than an 8-byte GPR, and so
9492 the structure-packing rules combined with the standard
9493 varargs behavior mean that we want to pack float/float
9494 and float/int combinations into a single register's
9495 space. This is complicated by the arg advance flushing,
9496 which works on arbitrarily large groups of int-type
9497 fields. */
9498 if (mode == SFmode)
9500 if (cum->floats_in_gpr == 1)
9502 /* Two floats in a word; count the word and reset
9503 the float count. */
9504 cum->words++;
9505 cum->floats_in_gpr = 0;
9507 else if (bitpos % 64 == 0)
9509 /* A float at the beginning of an 8-byte word;
9510 count it and put off adjusting cum->words until
9511 we see if a arg advance flush is going to do it
9512 for us. */
9513 cum->floats_in_gpr++;
9515 else
9517 /* The float is at the end of a word, preceded
9518 by integer fields, so the arg advance flush
9519 just above has already set cum->words and
9520 everything is taken care of. */
9523 else
9524 cum->words += n_fpregs;
9526 else if (USE_ALTIVEC_FOR_ARG_P (cum, mode, 1))
9528 rs6000_darwin64_record_arg_advance_flush (cum, bitpos, 0);
9529 cum->vregno++;
9530 cum->words += 2;
9532 else if (cum->intoffset == -1)
9533 cum->intoffset = bitpos;
9537 /* Check for an item that needs to be considered specially under the darwin 64
9538 bit ABI. These are record types where the mode is BLK or the structure is
9539 8 bytes in size. */
9540 static int
9541 rs6000_darwin64_struct_check_p (enum machine_mode mode, const_tree type)
9543 return rs6000_darwin64_abi
9544 && ((mode == BLKmode
9545 && TREE_CODE (type) == RECORD_TYPE
9546 && int_size_in_bytes (type) > 0)
9547 || (type && TREE_CODE (type) == RECORD_TYPE
9548 && int_size_in_bytes (type) == 8)) ? 1 : 0;
9551 /* Update the data in CUM to advance over an argument
9552 of mode MODE and data type TYPE.
9553 (TYPE is null for libcalls where that information may not be available.)
9555 Note that for args passed by reference, function_arg will be called
9556 with MODE and TYPE set to that of the pointer to the arg, not the arg
9557 itself. */
9559 static void
9560 rs6000_function_arg_advance_1 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
9561 const_tree type, bool named, int depth)
9563 enum machine_mode elt_mode;
9564 int n_elts;
9566 rs6000_discover_homogeneous_aggregate (mode, type, &elt_mode, &n_elts);
9568 /* Only tick off an argument if we're not recursing. */
9569 if (depth == 0)
9570 cum->nargs_prototype--;
9572 #ifdef HAVE_AS_GNU_ATTRIBUTE
9573 if (DEFAULT_ABI == ABI_V4
9574 && cum->escapes)
9576 if (SCALAR_FLOAT_MODE_P (mode))
9577 rs6000_passes_float = true;
9578 else if (named && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
9579 rs6000_passes_vector = true;
9580 else if (SPE_VECTOR_MODE (mode)
9581 && !cum->stdarg
9582 && cum->sysv_gregno <= GP_ARG_MAX_REG)
9583 rs6000_passes_vector = true;
9585 #endif
9587 if (TARGET_ALTIVEC_ABI
9588 && (ALTIVEC_OR_VSX_VECTOR_MODE (elt_mode)
9589 || (type && TREE_CODE (type) == VECTOR_TYPE
9590 && int_size_in_bytes (type) == 16)))
9592 bool stack = false;
9594 if (USE_ALTIVEC_FOR_ARG_P (cum, elt_mode, named))
9596 cum->vregno += n_elts;
9598 if (!TARGET_ALTIVEC)
9599 error ("cannot pass argument in vector register because"
9600 " altivec instructions are disabled, use -maltivec"
9601 " to enable them");
9603 /* PowerPC64 Linux and AIX allocate GPRs for a vector argument
9604 even if it is going to be passed in a vector register.
9605 Darwin does the same for variable-argument functions. */
9606 if (((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
9607 && TARGET_64BIT)
9608 || (cum->stdarg && DEFAULT_ABI != ABI_V4))
9609 stack = true;
9611 else
9612 stack = true;
9614 if (stack)
9616 int align;
9618 /* Vector parameters must be 16-byte aligned. In 32-bit
9619 mode this means we need to take into account the offset
9620 to the parameter save area. In 64-bit mode, they just
9621 have to start on an even word, since the parameter save
9622 area is 16-byte aligned. */
9623 if (TARGET_32BIT)
9624 align = -(rs6000_parm_offset () + cum->words) & 3;
9625 else
9626 align = cum->words & 1;
9627 cum->words += align + rs6000_arg_size (mode, type);
9629 if (TARGET_DEBUG_ARG)
9631 fprintf (stderr, "function_adv: words = %2d, align=%d, ",
9632 cum->words, align);
9633 fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s\n",
9634 cum->nargs_prototype, cum->prototype,
9635 GET_MODE_NAME (mode));
9639 else if (TARGET_SPE_ABI && TARGET_SPE && SPE_VECTOR_MODE (mode)
9640 && !cum->stdarg
9641 && cum->sysv_gregno <= GP_ARG_MAX_REG)
9642 cum->sysv_gregno++;
9644 else if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
9646 int size = int_size_in_bytes (type);
9647 /* Variable sized types have size == -1 and are
9648 treated as if consisting entirely of ints.
9649 Pad to 16 byte boundary if needed. */
9650 if (TYPE_ALIGN (type) >= 2 * BITS_PER_WORD
9651 && (cum->words % 2) != 0)
9652 cum->words++;
9653 /* For varargs, we can just go up by the size of the struct. */
9654 if (!named)
9655 cum->words += (size + 7) / 8;
9656 else
9658 /* It is tempting to say int register count just goes up by
9659 sizeof(type)/8, but this is wrong in a case such as
9660 { int; double; int; } [powerpc alignment]. We have to
9661 grovel through the fields for these too. */
9662 cum->intoffset = 0;
9663 cum->floats_in_gpr = 0;
9664 rs6000_darwin64_record_arg_advance_recurse (cum, type, 0);
9665 rs6000_darwin64_record_arg_advance_flush (cum,
9666 size * BITS_PER_UNIT, 1);
9668 if (TARGET_DEBUG_ARG)
9670 fprintf (stderr, "function_adv: words = %2d, align=%d, size=%d",
9671 cum->words, TYPE_ALIGN (type), size);
9672 fprintf (stderr,
9673 "nargs = %4d, proto = %d, mode = %4s (darwin64 abi)\n",
9674 cum->nargs_prototype, cum->prototype,
9675 GET_MODE_NAME (mode));
9678 else if (DEFAULT_ABI == ABI_V4)
9680 if (TARGET_HARD_FLOAT && TARGET_FPRS
9681 && ((TARGET_SINGLE_FLOAT && mode == SFmode)
9682 || (TARGET_DOUBLE_FLOAT && mode == DFmode)
9683 || (mode == TFmode && !TARGET_IEEEQUAD)
9684 || mode == SDmode || mode == DDmode || mode == TDmode))
9686 /* _Decimal128 must use an even/odd register pair. This assumes
9687 that the register number is odd when fregno is odd. */
9688 if (mode == TDmode && (cum->fregno % 2) == 1)
9689 cum->fregno++;
9691 if (cum->fregno + (mode == TFmode || mode == TDmode ? 1 : 0)
9692 <= FP_ARG_V4_MAX_REG)
9693 cum->fregno += (GET_MODE_SIZE (mode) + 7) >> 3;
9694 else
9696 cum->fregno = FP_ARG_V4_MAX_REG + 1;
9697 if (mode == DFmode || mode == TFmode
9698 || mode == DDmode || mode == TDmode)
9699 cum->words += cum->words & 1;
9700 cum->words += rs6000_arg_size (mode, type);
9703 else
9705 int n_words = rs6000_arg_size (mode, type);
9706 int gregno = cum->sysv_gregno;
9708 /* Long long and SPE vectors are put in (r3,r4), (r5,r6),
9709 (r7,r8) or (r9,r10). As does any other 2 word item such
9710 as complex int due to a historical mistake. */
9711 if (n_words == 2)
9712 gregno += (1 - gregno) & 1;
9714 /* Multi-reg args are not split between registers and stack. */
9715 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
9717 /* Long long and SPE vectors are aligned on the stack.
9718 So are other 2 word items such as complex int due to
9719 a historical mistake. */
9720 if (n_words == 2)
9721 cum->words += cum->words & 1;
9722 cum->words += n_words;
9725 /* Note: continuing to accumulate gregno past when we've started
9726 spilling to the stack indicates the fact that we've started
9727 spilling to the stack to expand_builtin_saveregs. */
9728 cum->sysv_gregno = gregno + n_words;
9731 if (TARGET_DEBUG_ARG)
9733 fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
9734 cum->words, cum->fregno);
9735 fprintf (stderr, "gregno = %2d, nargs = %4d, proto = %d, ",
9736 cum->sysv_gregno, cum->nargs_prototype, cum->prototype);
9737 fprintf (stderr, "mode = %4s, named = %d\n",
9738 GET_MODE_NAME (mode), named);
9741 else
9743 int n_words = rs6000_arg_size (mode, type);
9744 int start_words = cum->words;
9745 int align_words = rs6000_parm_start (mode, type, start_words);
9747 cum->words = align_words + n_words;
9749 if (SCALAR_FLOAT_MODE_P (elt_mode)
9750 && TARGET_HARD_FLOAT && TARGET_FPRS)
9752 /* _Decimal128 must be passed in an even/odd float register pair.
9753 This assumes that the register number is odd when fregno is
9754 odd. */
9755 if (elt_mode == TDmode && (cum->fregno % 2) == 1)
9756 cum->fregno++;
9757 cum->fregno += n_elts * ((GET_MODE_SIZE (elt_mode) + 7) >> 3);
9760 if (TARGET_DEBUG_ARG)
9762 fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
9763 cum->words, cum->fregno);
9764 fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s, ",
9765 cum->nargs_prototype, cum->prototype, GET_MODE_NAME (mode));
9766 fprintf (stderr, "named = %d, align = %d, depth = %d\n",
9767 named, align_words - start_words, depth);
9772 static void
9773 rs6000_function_arg_advance (cumulative_args_t cum, enum machine_mode mode,
9774 const_tree type, bool named)
9776 rs6000_function_arg_advance_1 (get_cumulative_args (cum), mode, type, named,
9780 static rtx
9781 spe_build_register_parallel (enum machine_mode mode, int gregno)
9783 rtx r1, r3, r5, r7;
9785 switch (mode)
9787 case DFmode:
9788 r1 = gen_rtx_REG (DImode, gregno);
9789 r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
9790 return gen_rtx_PARALLEL (mode, gen_rtvec (1, r1));
9792 case DCmode:
9793 case TFmode:
9794 r1 = gen_rtx_REG (DImode, gregno);
9795 r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
9796 r3 = gen_rtx_REG (DImode, gregno + 2);
9797 r3 = gen_rtx_EXPR_LIST (VOIDmode, r3, GEN_INT (8));
9798 return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r3));
9800 case TCmode:
9801 r1 = gen_rtx_REG (DImode, gregno);
9802 r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
9803 r3 = gen_rtx_REG (DImode, gregno + 2);
9804 r3 = gen_rtx_EXPR_LIST (VOIDmode, r3, GEN_INT (8));
9805 r5 = gen_rtx_REG (DImode, gregno + 4);
9806 r5 = gen_rtx_EXPR_LIST (VOIDmode, r5, GEN_INT (16));
9807 r7 = gen_rtx_REG (DImode, gregno + 6);
9808 r7 = gen_rtx_EXPR_LIST (VOIDmode, r7, GEN_INT (24));
9809 return gen_rtx_PARALLEL (mode, gen_rtvec (4, r1, r3, r5, r7));
9811 default:
9812 gcc_unreachable ();
9816 /* Determine where to put a SIMD argument on the SPE. */
9817 static rtx
9818 rs6000_spe_function_arg (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
9819 const_tree type)
9821 int gregno = cum->sysv_gregno;
9823 /* On E500 v2, double arithmetic is done on the full 64-bit GPR, but
9824 are passed and returned in a pair of GPRs for ABI compatibility. */
9825 if (TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode
9826 || mode == DCmode || mode == TCmode))
9828 int n_words = rs6000_arg_size (mode, type);
9830 /* Doubles go in an odd/even register pair (r5/r6, etc). */
9831 if (mode == DFmode)
9832 gregno += (1 - gregno) & 1;
9834 /* Multi-reg args are not split between registers and stack. */
9835 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
9836 return NULL_RTX;
9838 return spe_build_register_parallel (mode, gregno);
9840 if (cum->stdarg)
9842 int n_words = rs6000_arg_size (mode, type);
9844 /* SPE vectors are put in odd registers. */
9845 if (n_words == 2 && (gregno & 1) == 0)
9846 gregno += 1;
9848 if (gregno + n_words - 1 <= GP_ARG_MAX_REG)
9850 rtx r1, r2;
9851 enum machine_mode m = SImode;
9853 r1 = gen_rtx_REG (m, gregno);
9854 r1 = gen_rtx_EXPR_LIST (m, r1, const0_rtx);
9855 r2 = gen_rtx_REG (m, gregno + 1);
9856 r2 = gen_rtx_EXPR_LIST (m, r2, GEN_INT (4));
9857 return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
9859 else
9860 return NULL_RTX;
9862 else
9864 if (gregno <= GP_ARG_MAX_REG)
9865 return gen_rtx_REG (mode, gregno);
9866 else
9867 return NULL_RTX;
9871 /* A subroutine of rs6000_darwin64_record_arg. Assign the bits of the
9872 structure between cum->intoffset and bitpos to integer registers. */
9874 static void
9875 rs6000_darwin64_record_arg_flush (CUMULATIVE_ARGS *cum,
9876 HOST_WIDE_INT bitpos, rtx rvec[], int *k)
9878 enum machine_mode mode;
9879 unsigned int regno;
9880 unsigned int startbit, endbit;
9881 int this_regno, intregs, intoffset;
9882 rtx reg;
9884 if (cum->intoffset == -1)
9885 return;
9887 intoffset = cum->intoffset;
9888 cum->intoffset = -1;
9890 /* If this is the trailing part of a word, try to only load that
9891 much into the register. Otherwise load the whole register. Note
9892 that in the latter case we may pick up unwanted bits. It's not a
9893 problem at the moment but may wish to revisit. */
9895 if (intoffset % BITS_PER_WORD != 0)
9897 mode = mode_for_size (BITS_PER_WORD - intoffset % BITS_PER_WORD,
9898 MODE_INT, 0);
9899 if (mode == BLKmode)
9901 /* We couldn't find an appropriate mode, which happens,
9902 e.g., in packed structs when there are 3 bytes to load.
9903 Back intoffset back to the beginning of the word in this
9904 case. */
9905 intoffset = intoffset & -BITS_PER_WORD;
9906 mode = word_mode;
9909 else
9910 mode = word_mode;
9912 startbit = intoffset & -BITS_PER_WORD;
9913 endbit = (bitpos + BITS_PER_WORD - 1) & -BITS_PER_WORD;
9914 intregs = (endbit - startbit) / BITS_PER_WORD;
9915 this_regno = cum->words + intoffset / BITS_PER_WORD;
9917 if (intregs > 0 && intregs > GP_ARG_NUM_REG - this_regno)
9918 cum->use_stack = 1;
9920 intregs = MIN (intregs, GP_ARG_NUM_REG - this_regno);
9921 if (intregs <= 0)
9922 return;
9924 intoffset /= BITS_PER_UNIT;
9927 regno = GP_ARG_MIN_REG + this_regno;
9928 reg = gen_rtx_REG (mode, regno);
9929 rvec[(*k)++] =
9930 gen_rtx_EXPR_LIST (VOIDmode, reg, GEN_INT (intoffset));
9932 this_regno += 1;
9933 intoffset = (intoffset | (UNITS_PER_WORD-1)) + 1;
9934 mode = word_mode;
9935 intregs -= 1;
9937 while (intregs > 0);
9940 /* Recursive workhorse for the following. */
9942 static void
9943 rs6000_darwin64_record_arg_recurse (CUMULATIVE_ARGS *cum, const_tree type,
9944 HOST_WIDE_INT startbitpos, rtx rvec[],
9945 int *k)
9947 tree f;
9949 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
9950 if (TREE_CODE (f) == FIELD_DECL)
9952 HOST_WIDE_INT bitpos = startbitpos;
9953 tree ftype = TREE_TYPE (f);
9954 enum machine_mode mode;
9955 if (ftype == error_mark_node)
9956 continue;
9957 mode = TYPE_MODE (ftype);
9959 if (DECL_SIZE (f) != 0
9960 && tree_fits_uhwi_p (bit_position (f)))
9961 bitpos += int_bit_position (f);
9963 /* ??? FIXME: else assume zero offset. */
9965 if (TREE_CODE (ftype) == RECORD_TYPE)
9966 rs6000_darwin64_record_arg_recurse (cum, ftype, bitpos, rvec, k);
9967 else if (cum->named && USE_FP_FOR_ARG_P (cum, mode))
9969 unsigned n_fpreg = (GET_MODE_SIZE (mode) + 7) >> 3;
9970 #if 0
9971 switch (mode)
9973 case SCmode: mode = SFmode; break;
9974 case DCmode: mode = DFmode; break;
9975 case TCmode: mode = TFmode; break;
9976 default: break;
9978 #endif
9979 rs6000_darwin64_record_arg_flush (cum, bitpos, rvec, k);
9980 if (cum->fregno + n_fpreg > FP_ARG_MAX_REG + 1)
9982 gcc_assert (cum->fregno == FP_ARG_MAX_REG
9983 && (mode == TFmode || mode == TDmode));
9984 /* Long double or _Decimal128 split over regs and memory. */
9985 mode = DECIMAL_FLOAT_MODE_P (mode) ? DDmode : DFmode;
9986 cum->use_stack=1;
9988 rvec[(*k)++]
9989 = gen_rtx_EXPR_LIST (VOIDmode,
9990 gen_rtx_REG (mode, cum->fregno++),
9991 GEN_INT (bitpos / BITS_PER_UNIT));
9992 if (mode == TFmode || mode == TDmode)
9993 cum->fregno++;
9995 else if (cum->named && USE_ALTIVEC_FOR_ARG_P (cum, mode, 1))
9997 rs6000_darwin64_record_arg_flush (cum, bitpos, rvec, k);
9998 rvec[(*k)++]
9999 = gen_rtx_EXPR_LIST (VOIDmode,
10000 gen_rtx_REG (mode, cum->vregno++),
10001 GEN_INT (bitpos / BITS_PER_UNIT));
10003 else if (cum->intoffset == -1)
10004 cum->intoffset = bitpos;
10008 /* For the darwin64 ABI, we want to construct a PARALLEL consisting of
10009 the register(s) to be used for each field and subfield of a struct
10010 being passed by value, along with the offset of where the
10011 register's value may be found in the block. FP fields go in FP
10012 register, vector fields go in vector registers, and everything
10013 else goes in int registers, packed as in memory.
10015 This code is also used for function return values. RETVAL indicates
10016 whether this is the case.
10018 Much of this is taken from the SPARC V9 port, which has a similar
10019 calling convention. */
10021 static rtx
10022 rs6000_darwin64_record_arg (CUMULATIVE_ARGS *orig_cum, const_tree type,
10023 bool named, bool retval)
10025 rtx rvec[FIRST_PSEUDO_REGISTER];
10026 int k = 1, kbase = 1;
10027 HOST_WIDE_INT typesize = int_size_in_bytes (type);
10028 /* This is a copy; modifications are not visible to our caller. */
10029 CUMULATIVE_ARGS copy_cum = *orig_cum;
10030 CUMULATIVE_ARGS *cum = &copy_cum;
10032 /* Pad to 16 byte boundary if needed. */
10033 if (!retval && TYPE_ALIGN (type) >= 2 * BITS_PER_WORD
10034 && (cum->words % 2) != 0)
10035 cum->words++;
10037 cum->intoffset = 0;
10038 cum->use_stack = 0;
10039 cum->named = named;
10041 /* Put entries into rvec[] for individual FP and vector fields, and
10042 for the chunks of memory that go in int regs. Note we start at
10043 element 1; 0 is reserved for an indication of using memory, and
10044 may or may not be filled in below. */
10045 rs6000_darwin64_record_arg_recurse (cum, type, /* startbit pos= */ 0, rvec, &k);
10046 rs6000_darwin64_record_arg_flush (cum, typesize * BITS_PER_UNIT, rvec, &k);
10048 /* If any part of the struct went on the stack put all of it there.
10049 This hack is because the generic code for
10050 FUNCTION_ARG_PARTIAL_NREGS cannot handle cases where the register
10051 parts of the struct are not at the beginning. */
10052 if (cum->use_stack)
10054 if (retval)
10055 return NULL_RTX; /* doesn't go in registers at all */
10056 kbase = 0;
10057 rvec[0] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
10059 if (k > 1 || cum->use_stack)
10060 return gen_rtx_PARALLEL (BLKmode, gen_rtvec_v (k - kbase, &rvec[kbase]));
10061 else
10062 return NULL_RTX;
10065 /* Determine where to place an argument in 64-bit mode with 32-bit ABI. */
10067 static rtx
10068 rs6000_mixed_function_arg (enum machine_mode mode, const_tree type,
10069 int align_words)
10071 int n_units;
10072 int i, k;
10073 rtx rvec[GP_ARG_NUM_REG + 1];
10075 if (align_words >= GP_ARG_NUM_REG)
10076 return NULL_RTX;
10078 n_units = rs6000_arg_size (mode, type);
10080 /* Optimize the simple case where the arg fits in one gpr, except in
10081 the case of BLKmode due to assign_parms assuming that registers are
10082 BITS_PER_WORD wide. */
10083 if (n_units == 0
10084 || (n_units == 1 && mode != BLKmode))
10085 return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
10087 k = 0;
10088 if (align_words + n_units > GP_ARG_NUM_REG)
10089 /* Not all of the arg fits in gprs. Say that it goes in memory too,
10090 using a magic NULL_RTX component.
10091 This is not strictly correct. Only some of the arg belongs in
10092 memory, not all of it. However, the normal scheme using
10093 function_arg_partial_nregs can result in unusual subregs, eg.
10094 (subreg:SI (reg:DF) 4), which are not handled well. The code to
10095 store the whole arg to memory is often more efficient than code
10096 to store pieces, and we know that space is available in the right
10097 place for the whole arg. */
10098 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
10100 i = 0;
10103 rtx r = gen_rtx_REG (SImode, GP_ARG_MIN_REG + align_words);
10104 rtx off = GEN_INT (i++ * 4);
10105 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
10107 while (++align_words < GP_ARG_NUM_REG && --n_units != 0);
10109 return gen_rtx_PARALLEL (mode, gen_rtvec_v (k, rvec));
10112 /* We have an argument of MODE and TYPE that goes into FPRs or VRs,
10113 but must also be copied into the parameter save area starting at
10114 offset ALIGN_WORDS. Fill in RVEC with the elements corresponding
10115 to the GPRs and/or memory. Return the number of elements used. */
10117 static int
10118 rs6000_psave_function_arg (enum machine_mode mode, const_tree type,
10119 int align_words, rtx *rvec)
10121 int k = 0;
10123 if (align_words < GP_ARG_NUM_REG)
10125 int n_words = rs6000_arg_size (mode, type);
10127 if (align_words + n_words > GP_ARG_NUM_REG
10128 || mode == BLKmode
10129 || (TARGET_32BIT && TARGET_POWERPC64))
10131 /* If this is partially on the stack, then we only
10132 include the portion actually in registers here. */
10133 enum machine_mode rmode = TARGET_32BIT ? SImode : DImode;
10134 int i = 0;
10136 if (align_words + n_words > GP_ARG_NUM_REG)
10138 /* Not all of the arg fits in gprs. Say that it goes in memory
10139 too, using a magic NULL_RTX component. Also see comment in
10140 rs6000_mixed_function_arg for why the normal
10141 function_arg_partial_nregs scheme doesn't work in this case. */
10142 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
10147 rtx r = gen_rtx_REG (rmode, GP_ARG_MIN_REG + align_words);
10148 rtx off = GEN_INT (i++ * GET_MODE_SIZE (rmode));
10149 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
10151 while (++align_words < GP_ARG_NUM_REG && --n_words != 0);
10153 else
10155 /* The whole arg fits in gprs. */
10156 rtx r = gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
10157 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, const0_rtx);
10160 else
10162 /* It's entirely in memory. */
10163 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
10166 return k;
10169 /* RVEC is a vector of K components of an argument of mode MODE.
10170 Construct the final function_arg return value from it. */
10172 static rtx
10173 rs6000_finish_function_arg (enum machine_mode mode, rtx *rvec, int k)
10175 gcc_assert (k >= 1);
10177 /* Avoid returning a PARALLEL in the trivial cases. */
10178 if (k == 1)
10180 if (XEXP (rvec[0], 0) == NULL_RTX)
10181 return NULL_RTX;
10183 if (GET_MODE (XEXP (rvec[0], 0)) == mode)
10184 return XEXP (rvec[0], 0);
10187 return gen_rtx_PARALLEL (mode, gen_rtvec_v (k, rvec));
10190 /* Determine where to put an argument to a function.
10191 Value is zero to push the argument on the stack,
10192 or a hard register in which to store the argument.
10194 MODE is the argument's machine mode.
10195 TYPE is the data type of the argument (as a tree).
10196 This is null for libcalls where that information may
10197 not be available.
10198 CUM is a variable of type CUMULATIVE_ARGS which gives info about
10199 the preceding args and about the function being called. It is
10200 not modified in this routine.
10201 NAMED is nonzero if this argument is a named parameter
10202 (otherwise it is an extra parameter matching an ellipsis).
10204 On RS/6000 the first eight words of non-FP are normally in registers
10205 and the rest are pushed. Under AIX, the first 13 FP args are in registers.
10206 Under V.4, the first 8 FP args are in registers.
10208 If this is floating-point and no prototype is specified, we use
10209 both an FP and integer register (or possibly FP reg and stack). Library
10210 functions (when CALL_LIBCALL is set) always have the proper types for args,
10211 so we can pass the FP value just in one register. emit_library_function
10212 doesn't support PARALLEL anyway.
10214 Note that for args passed by reference, function_arg will be called
10215 with MODE and TYPE set to that of the pointer to the arg, not the arg
10216 itself. */
10218 static rtx
10219 rs6000_function_arg (cumulative_args_t cum_v, enum machine_mode mode,
10220 const_tree type, bool named)
10222 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
10223 enum rs6000_abi abi = DEFAULT_ABI;
10224 enum machine_mode elt_mode;
10225 int n_elts;
10227 /* Return a marker to indicate whether CR1 needs to set or clear the
10228 bit that V.4 uses to say fp args were passed in registers.
10229 Assume that we don't need the marker for software floating point,
10230 or compiler generated library calls. */
10231 if (mode == VOIDmode)
10233 if (abi == ABI_V4
10234 && (cum->call_cookie & CALL_LIBCALL) == 0
10235 && (cum->stdarg
10236 || (cum->nargs_prototype < 0
10237 && (cum->prototype || TARGET_NO_PROTOTYPE))))
10239 /* For the SPE, we need to crxor CR6 always. */
10240 if (TARGET_SPE_ABI)
10241 return GEN_INT (cum->call_cookie | CALL_V4_SET_FP_ARGS);
10242 else if (TARGET_HARD_FLOAT && TARGET_FPRS)
10243 return GEN_INT (cum->call_cookie
10244 | ((cum->fregno == FP_ARG_MIN_REG)
10245 ? CALL_V4_SET_FP_ARGS
10246 : CALL_V4_CLEAR_FP_ARGS));
10249 return GEN_INT (cum->call_cookie & ~CALL_LIBCALL);
10252 rs6000_discover_homogeneous_aggregate (mode, type, &elt_mode, &n_elts);
10254 if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
10256 rtx rslt = rs6000_darwin64_record_arg (cum, type, named, /*retval= */false);
10257 if (rslt != NULL_RTX)
10258 return rslt;
10259 /* Else fall through to usual handling. */
10262 if (USE_ALTIVEC_FOR_ARG_P (cum, elt_mode, named))
10264 rtx rvec[GP_ARG_NUM_REG + AGGR_ARG_NUM_REG + 1];
10265 rtx r, off;
10266 int i, k = 0;
10268 /* Do we also need to pass this argument in the parameter
10269 save area? */
10270 if (TARGET_64BIT && ! cum->prototype)
10272 int align_words = (cum->words + 1) & ~1;
10273 k = rs6000_psave_function_arg (mode, type, align_words, rvec);
10276 /* Describe where this argument goes in the vector registers. */
10277 for (i = 0; i < n_elts && cum->vregno + i <= ALTIVEC_ARG_MAX_REG; i++)
10279 r = gen_rtx_REG (elt_mode, cum->vregno + i);
10280 off = GEN_INT (i * GET_MODE_SIZE (elt_mode));
10281 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
10284 return rs6000_finish_function_arg (mode, rvec, k);
10286 else if (TARGET_ALTIVEC_ABI
10287 && (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
10288 || (type && TREE_CODE (type) == VECTOR_TYPE
10289 && int_size_in_bytes (type) == 16)))
10291 if (named || abi == ABI_V4)
10292 return NULL_RTX;
10293 else
10295 /* Vector parameters to varargs functions under AIX or Darwin
10296 get passed in memory and possibly also in GPRs. */
10297 int align, align_words, n_words;
10298 enum machine_mode part_mode;
10300 /* Vector parameters must be 16-byte aligned. In 32-bit
10301 mode this means we need to take into account the offset
10302 to the parameter save area. In 64-bit mode, they just
10303 have to start on an even word, since the parameter save
10304 area is 16-byte aligned. */
10305 if (TARGET_32BIT)
10306 align = -(rs6000_parm_offset () + cum->words) & 3;
10307 else
10308 align = cum->words & 1;
10309 align_words = cum->words + align;
10311 /* Out of registers? Memory, then. */
10312 if (align_words >= GP_ARG_NUM_REG)
10313 return NULL_RTX;
10315 if (TARGET_32BIT && TARGET_POWERPC64)
10316 return rs6000_mixed_function_arg (mode, type, align_words);
10318 /* The vector value goes in GPRs. Only the part of the
10319 value in GPRs is reported here. */
10320 part_mode = mode;
10321 n_words = rs6000_arg_size (mode, type);
10322 if (align_words + n_words > GP_ARG_NUM_REG)
10323 /* Fortunately, there are only two possibilities, the value
10324 is either wholly in GPRs or half in GPRs and half not. */
10325 part_mode = DImode;
10327 return gen_rtx_REG (part_mode, GP_ARG_MIN_REG + align_words);
10330 else if (TARGET_SPE_ABI && TARGET_SPE
10331 && (SPE_VECTOR_MODE (mode)
10332 || (TARGET_E500_DOUBLE && (mode == DFmode
10333 || mode == DCmode
10334 || mode == TFmode
10335 || mode == TCmode))))
10336 return rs6000_spe_function_arg (cum, mode, type);
10338 else if (abi == ABI_V4)
10340 if (TARGET_HARD_FLOAT && TARGET_FPRS
10341 && ((TARGET_SINGLE_FLOAT && mode == SFmode)
10342 || (TARGET_DOUBLE_FLOAT && mode == DFmode)
10343 || (mode == TFmode && !TARGET_IEEEQUAD)
10344 || mode == SDmode || mode == DDmode || mode == TDmode))
10346 /* _Decimal128 must use an even/odd register pair. This assumes
10347 that the register number is odd when fregno is odd. */
10348 if (mode == TDmode && (cum->fregno % 2) == 1)
10349 cum->fregno++;
10351 if (cum->fregno + (mode == TFmode || mode == TDmode ? 1 : 0)
10352 <= FP_ARG_V4_MAX_REG)
10353 return gen_rtx_REG (mode, cum->fregno);
10354 else
10355 return NULL_RTX;
10357 else
10359 int n_words = rs6000_arg_size (mode, type);
10360 int gregno = cum->sysv_gregno;
10362 /* Long long and SPE vectors are put in (r3,r4), (r5,r6),
10363 (r7,r8) or (r9,r10). As does any other 2 word item such
10364 as complex int due to a historical mistake. */
10365 if (n_words == 2)
10366 gregno += (1 - gregno) & 1;
10368 /* Multi-reg args are not split between registers and stack. */
10369 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
10370 return NULL_RTX;
10372 if (TARGET_32BIT && TARGET_POWERPC64)
10373 return rs6000_mixed_function_arg (mode, type,
10374 gregno - GP_ARG_MIN_REG);
10375 return gen_rtx_REG (mode, gregno);
10378 else
10380 int align_words = rs6000_parm_start (mode, type, cum->words);
10382 /* _Decimal128 must be passed in an even/odd float register pair.
10383 This assumes that the register number is odd when fregno is odd. */
10384 if (elt_mode == TDmode && (cum->fregno % 2) == 1)
10385 cum->fregno++;
10387 if (USE_FP_FOR_ARG_P (cum, elt_mode))
10389 rtx rvec[GP_ARG_NUM_REG + AGGR_ARG_NUM_REG + 1];
10390 rtx r, off;
10391 int i, k = 0;
10392 unsigned long n_fpreg = (GET_MODE_SIZE (elt_mode) + 7) >> 3;
10393 int fpr_words;
10395 /* Do we also need to pass this argument in the parameter
10396 save area? */
10397 if (type && (cum->nargs_prototype <= 0
10398 || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
10399 && TARGET_XL_COMPAT
10400 && align_words >= GP_ARG_NUM_REG)))
10401 k = rs6000_psave_function_arg (mode, type, align_words, rvec);
10403 /* Describe where this argument goes in the fprs. */
10404 for (i = 0; i < n_elts
10405 && cum->fregno + i * n_fpreg <= FP_ARG_MAX_REG; i++)
10407 /* Check if the argument is split over registers and memory.
10408 This can only ever happen for long double or _Decimal128;
10409 complex types are handled via split_complex_arg. */
10410 enum machine_mode fmode = elt_mode;
10411 if (cum->fregno + (i + 1) * n_fpreg > FP_ARG_MAX_REG + 1)
10413 gcc_assert (fmode == TFmode || fmode == TDmode);
10414 fmode = DECIMAL_FLOAT_MODE_P (fmode) ? DDmode : DFmode;
10417 r = gen_rtx_REG (fmode, cum->fregno + i * n_fpreg);
10418 off = GEN_INT (i * GET_MODE_SIZE (elt_mode));
10419 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
10422 /* If there were not enough FPRs to hold the argument, the rest
10423 usually goes into memory. However, if the current position
10424 is still within the register parameter area, a portion may
10425 actually have to go into GPRs.
10427 Note that it may happen that the portion of the argument
10428 passed in the first "half" of the first GPR was already
10429 passed in the last FPR as well.
10431 For unnamed arguments, we already set up GPRs to cover the
10432 whole argument in rs6000_psave_function_arg, so there is
10433 nothing further to do at this point. */
10434 fpr_words = (i * GET_MODE_SIZE (elt_mode)) / (TARGET_32BIT ? 4 : 8);
10435 if (i < n_elts && align_words + fpr_words < GP_ARG_NUM_REG
10436 && cum->nargs_prototype > 0)
10438 static bool warned;
10440 enum machine_mode rmode = TARGET_32BIT ? SImode : DImode;
10441 int n_words = rs6000_arg_size (mode, type);
10443 align_words += fpr_words;
10444 n_words -= fpr_words;
10448 r = gen_rtx_REG (rmode, GP_ARG_MIN_REG + align_words);
10449 off = GEN_INT (fpr_words++ * GET_MODE_SIZE (rmode));
10450 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
10452 while (++align_words < GP_ARG_NUM_REG && --n_words != 0);
10454 if (!warned && warn_psabi)
10456 warned = true;
10457 inform (input_location,
10458 "the ABI of passing homogeneous float aggregates"
10459 " has changed in GCC 5");
10463 return rs6000_finish_function_arg (mode, rvec, k);
10465 else if (align_words < GP_ARG_NUM_REG)
10467 if (TARGET_32BIT && TARGET_POWERPC64)
10468 return rs6000_mixed_function_arg (mode, type, align_words);
10470 return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
10472 else
10473 return NULL_RTX;
10477 /* For an arg passed partly in registers and partly in memory, this is
10478 the number of bytes passed in registers. For args passed entirely in
10479 registers or entirely in memory, zero. When an arg is described by a
10480 PARALLEL, perhaps using more than one register type, this function
10481 returns the number of bytes used by the first element of the PARALLEL. */
10483 static int
10484 rs6000_arg_partial_bytes (cumulative_args_t cum_v, enum machine_mode mode,
10485 tree type, bool named)
10487 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
10488 bool passed_in_gprs = true;
10489 int ret = 0;
10490 int align_words;
10491 enum machine_mode elt_mode;
10492 int n_elts;
10494 rs6000_discover_homogeneous_aggregate (mode, type, &elt_mode, &n_elts);
10496 if (DEFAULT_ABI == ABI_V4)
10497 return 0;
10499 if (USE_ALTIVEC_FOR_ARG_P (cum, elt_mode, named))
10501 /* If we are passing this arg in the fixed parameter save area
10502 (gprs or memory) as well as VRs, we do not use the partial
10503 bytes mechanism; instead, rs6000_function_arg will return a
10504 PARALLEL including a memory element as necessary. */
10505 if (TARGET_64BIT && ! cum->prototype)
10506 return 0;
10508 /* Otherwise, we pass in VRs only. Check for partial copies. */
10509 passed_in_gprs = false;
10510 if (cum->vregno + n_elts > ALTIVEC_ARG_MAX_REG + 1)
10511 ret = (ALTIVEC_ARG_MAX_REG + 1 - cum->vregno) * 16;
10514 /* In this complicated case we just disable the partial_nregs code. */
10515 if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
10516 return 0;
10518 align_words = rs6000_parm_start (mode, type, cum->words);
10520 if (USE_FP_FOR_ARG_P (cum, elt_mode))
10522 unsigned long n_fpreg = (GET_MODE_SIZE (elt_mode) + 7) >> 3;
10524 /* If we are passing this arg in the fixed parameter save area
10525 (gprs or memory) as well as FPRs, we do not use the partial
10526 bytes mechanism; instead, rs6000_function_arg will return a
10527 PARALLEL including a memory element as necessary. */
10528 if (type
10529 && (cum->nargs_prototype <= 0
10530 || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
10531 && TARGET_XL_COMPAT
10532 && align_words >= GP_ARG_NUM_REG)))
10533 return 0;
10535 /* Otherwise, we pass in FPRs only. Check for partial copies. */
10536 passed_in_gprs = false;
10537 if (cum->fregno + n_elts * n_fpreg > FP_ARG_MAX_REG + 1)
10539 /* Compute number of bytes / words passed in FPRs. If there
10540 is still space available in the register parameter area
10541 *after* that amount, a part of the argument will be passed
10542 in GPRs. In that case, the total amount passed in any
10543 registers is equal to the amount that would have been passed
10544 in GPRs if everything were passed there, so we fall back to
10545 the GPR code below to compute the appropriate value. */
10546 int fpr = ((FP_ARG_MAX_REG + 1 - cum->fregno)
10547 * MIN (8, GET_MODE_SIZE (elt_mode)));
10548 int fpr_words = fpr / (TARGET_32BIT ? 4 : 8);
10550 if (align_words + fpr_words < GP_ARG_NUM_REG)
10551 passed_in_gprs = true;
10552 else
10553 ret = fpr;
10557 if (passed_in_gprs
10558 && align_words < GP_ARG_NUM_REG
10559 && GP_ARG_NUM_REG < align_words + rs6000_arg_size (mode, type))
10560 ret = (GP_ARG_NUM_REG - align_words) * (TARGET_32BIT ? 4 : 8);
10562 if (ret != 0 && TARGET_DEBUG_ARG)
10563 fprintf (stderr, "rs6000_arg_partial_bytes: %d\n", ret);
10565 return ret;
10568 /* A C expression that indicates when an argument must be passed by
10569 reference. If nonzero for an argument, a copy of that argument is
10570 made in memory and a pointer to the argument is passed instead of
10571 the argument itself. The pointer is passed in whatever way is
10572 appropriate for passing a pointer to that type.
10574 Under V.4, aggregates and long double are passed by reference.
10576 As an extension to all 32-bit ABIs, AltiVec vectors are passed by
10577 reference unless the AltiVec vector extension ABI is in force.
10579 As an extension to all ABIs, variable sized types are passed by
10580 reference. */
10582 static bool
10583 rs6000_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED,
10584 enum machine_mode mode, const_tree type,
10585 bool named ATTRIBUTE_UNUSED)
10587 if (DEFAULT_ABI == ABI_V4 && TARGET_IEEEQUAD && mode == TFmode)
10589 if (TARGET_DEBUG_ARG)
10590 fprintf (stderr, "function_arg_pass_by_reference: V4 long double\n");
10591 return 1;
10594 if (!type)
10595 return 0;
10597 if (DEFAULT_ABI == ABI_V4 && AGGREGATE_TYPE_P (type))
10599 if (TARGET_DEBUG_ARG)
10600 fprintf (stderr, "function_arg_pass_by_reference: V4 aggregate\n");
10601 return 1;
10604 if (int_size_in_bytes (type) < 0)
10606 if (TARGET_DEBUG_ARG)
10607 fprintf (stderr, "function_arg_pass_by_reference: variable size\n");
10608 return 1;
10611 /* Allow -maltivec -mabi=no-altivec without warning. Altivec vector
10612 modes only exist for GCC vector types if -maltivec. */
10613 if (TARGET_32BIT && !TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
10615 if (TARGET_DEBUG_ARG)
10616 fprintf (stderr, "function_arg_pass_by_reference: AltiVec\n");
10617 return 1;
10620 /* Pass synthetic vectors in memory. */
10621 if (TREE_CODE (type) == VECTOR_TYPE
10622 && int_size_in_bytes (type) > (TARGET_ALTIVEC_ABI ? 16 : 8))
10624 static bool warned_for_pass_big_vectors = false;
10625 if (TARGET_DEBUG_ARG)
10626 fprintf (stderr, "function_arg_pass_by_reference: synthetic vector\n");
10627 if (!warned_for_pass_big_vectors)
10629 warning (0, "GCC vector passed by reference: "
10630 "non-standard ABI extension with no compatibility guarantee");
10631 warned_for_pass_big_vectors = true;
10633 return 1;
10636 return 0;
10639 /* Process parameter of type TYPE after ARGS_SO_FAR parameters were
10640 already processes. Return true if the parameter must be passed
10641 (fully or partially) on the stack. */
10643 static bool
10644 rs6000_parm_needs_stack (cumulative_args_t args_so_far, tree type)
10646 enum machine_mode mode;
10647 int unsignedp;
10648 rtx entry_parm;
10650 /* Catch errors. */
10651 if (type == NULL || type == error_mark_node)
10652 return true;
10654 /* Handle types with no storage requirement. */
10655 if (TYPE_MODE (type) == VOIDmode)
10656 return false;
10658 /* Handle complex types. */
10659 if (TREE_CODE (type) == COMPLEX_TYPE)
10660 return (rs6000_parm_needs_stack (args_so_far, TREE_TYPE (type))
10661 || rs6000_parm_needs_stack (args_so_far, TREE_TYPE (type)));
10663 /* Handle transparent aggregates. */
10664 if ((TREE_CODE (type) == UNION_TYPE || TREE_CODE (type) == RECORD_TYPE)
10665 && TYPE_TRANSPARENT_AGGR (type))
10666 type = TREE_TYPE (first_field (type));
10668 /* See if this arg was passed by invisible reference. */
10669 if (pass_by_reference (get_cumulative_args (args_so_far),
10670 TYPE_MODE (type), type, true))
10671 type = build_pointer_type (type);
10673 /* Find mode as it is passed by the ABI. */
10674 unsignedp = TYPE_UNSIGNED (type);
10675 mode = promote_mode (type, TYPE_MODE (type), &unsignedp);
10677 /* If we must pass in stack, we need a stack. */
10678 if (rs6000_must_pass_in_stack (mode, type))
10679 return true;
10681 /* If there is no incoming register, we need a stack. */
10682 entry_parm = rs6000_function_arg (args_so_far, mode, type, true);
10683 if (entry_parm == NULL)
10684 return true;
10686 /* Likewise if we need to pass both in registers and on the stack. */
10687 if (GET_CODE (entry_parm) == PARALLEL
10688 && XEXP (XVECEXP (entry_parm, 0, 0), 0) == NULL_RTX)
10689 return true;
10691 /* Also true if we're partially in registers and partially not. */
10692 if (rs6000_arg_partial_bytes (args_so_far, mode, type, true) != 0)
10693 return true;
10695 /* Update info on where next arg arrives in registers. */
10696 rs6000_function_arg_advance (args_so_far, mode, type, true);
10697 return false;
10700 /* Return true if FUN has no prototype, has a variable argument
10701 list, or passes any parameter in memory. */
10703 static bool
10704 rs6000_function_parms_need_stack (tree fun, bool incoming)
10706 tree fntype, result;
10707 CUMULATIVE_ARGS args_so_far_v;
10708 cumulative_args_t args_so_far;
10710 if (!fun)
10711 /* Must be a libcall, all of which only use reg parms. */
10712 return false;
10714 fntype = fun;
10715 if (!TYPE_P (fun))
10716 fntype = TREE_TYPE (fun);
10718 /* Varargs functions need the parameter save area. */
10719 if ((!incoming && !prototype_p (fntype)) || stdarg_p (fntype))
10720 return true;
10722 INIT_CUMULATIVE_INCOMING_ARGS (args_so_far_v, fntype, NULL_RTX);
10723 args_so_far = pack_cumulative_args (&args_so_far_v);
10725 /* When incoming, we will have been passed the function decl.
10726 It is necessary to use the decl to handle K&R style functions,
10727 where TYPE_ARG_TYPES may not be available. */
10728 if (incoming)
10730 gcc_assert (DECL_P (fun));
10731 result = DECL_RESULT (fun);
10733 else
10734 result = TREE_TYPE (fntype);
10736 if (result && aggregate_value_p (result, fntype))
10738 if (!TYPE_P (result))
10739 result = TREE_TYPE (result);
10740 result = build_pointer_type (result);
10741 rs6000_parm_needs_stack (args_so_far, result);
10744 if (incoming)
10746 tree parm;
10748 for (parm = DECL_ARGUMENTS (fun);
10749 parm && parm != void_list_node;
10750 parm = TREE_CHAIN (parm))
10751 if (rs6000_parm_needs_stack (args_so_far, TREE_TYPE (parm)))
10752 return true;
10754 else
10756 function_args_iterator args_iter;
10757 tree arg_type;
10759 FOREACH_FUNCTION_ARGS (fntype, arg_type, args_iter)
10760 if (rs6000_parm_needs_stack (args_so_far, arg_type))
10761 return true;
10764 return false;
10767 /* Return the size of the REG_PARM_STACK_SPACE are for FUN. This is
10768 usually a constant depending on the ABI. However, in the ELFv2 ABI
10769 the register parameter area is optional when calling a function that
10770 has a prototype is scope, has no variable argument list, and passes
10771 all parameters in registers. */
10774 rs6000_reg_parm_stack_space (tree fun, bool incoming)
10776 int reg_parm_stack_space;
10778 switch (DEFAULT_ABI)
10780 default:
10781 reg_parm_stack_space = 0;
10782 break;
10784 case ABI_AIX:
10785 case ABI_DARWIN:
10786 reg_parm_stack_space = TARGET_64BIT ? 64 : 32;
10787 break;
10789 case ABI_ELFv2:
10790 /* ??? Recomputing this every time is a bit expensive. Is there
10791 a place to cache this information? */
10792 if (rs6000_function_parms_need_stack (fun, incoming))
10793 reg_parm_stack_space = TARGET_64BIT ? 64 : 32;
10794 else
10795 reg_parm_stack_space = 0;
10796 break;
10799 return reg_parm_stack_space;
10802 static void
10803 rs6000_move_block_from_reg (int regno, rtx x, int nregs)
10805 int i;
10806 enum machine_mode reg_mode = TARGET_32BIT ? SImode : DImode;
10808 if (nregs == 0)
10809 return;
10811 for (i = 0; i < nregs; i++)
10813 rtx tem = adjust_address_nv (x, reg_mode, i * GET_MODE_SIZE (reg_mode));
10814 if (reload_completed)
10816 if (! strict_memory_address_p (reg_mode, XEXP (tem, 0)))
10817 tem = NULL_RTX;
10818 else
10819 tem = simplify_gen_subreg (reg_mode, x, BLKmode,
10820 i * GET_MODE_SIZE (reg_mode));
10822 else
10823 tem = replace_equiv_address (tem, XEXP (tem, 0));
10825 gcc_assert (tem);
10827 emit_move_insn (tem, gen_rtx_REG (reg_mode, regno + i));
10831 /* Perform any needed actions needed for a function that is receiving a
10832 variable number of arguments.
10834 CUM is as above.
10836 MODE and TYPE are the mode and type of the current parameter.
10838 PRETEND_SIZE is a variable that should be set to the amount of stack
10839 that must be pushed by the prolog to pretend that our caller pushed
10842 Normally, this macro will push all remaining incoming registers on the
10843 stack and set PRETEND_SIZE to the length of the registers pushed. */
10845 static void
10846 setup_incoming_varargs (cumulative_args_t cum, enum machine_mode mode,
10847 tree type, int *pretend_size ATTRIBUTE_UNUSED,
10848 int no_rtl)
10850 CUMULATIVE_ARGS next_cum;
10851 int reg_size = TARGET_32BIT ? 4 : 8;
10852 rtx save_area = NULL_RTX, mem;
10853 int first_reg_offset;
10854 alias_set_type set;
10856 /* Skip the last named argument. */
10857 next_cum = *get_cumulative_args (cum);
10858 rs6000_function_arg_advance_1 (&next_cum, mode, type, true, 0);
10860 if (DEFAULT_ABI == ABI_V4)
10862 first_reg_offset = next_cum.sysv_gregno - GP_ARG_MIN_REG;
10864 if (! no_rtl)
10866 int gpr_reg_num = 0, gpr_size = 0, fpr_size = 0;
10867 HOST_WIDE_INT offset = 0;
10869 /* Try to optimize the size of the varargs save area.
10870 The ABI requires that ap.reg_save_area is doubleword
10871 aligned, but we don't need to allocate space for all
10872 the bytes, only those to which we actually will save
10873 anything. */
10874 if (cfun->va_list_gpr_size && first_reg_offset < GP_ARG_NUM_REG)
10875 gpr_reg_num = GP_ARG_NUM_REG - first_reg_offset;
10876 if (TARGET_HARD_FLOAT && TARGET_FPRS
10877 && next_cum.fregno <= FP_ARG_V4_MAX_REG
10878 && cfun->va_list_fpr_size)
10880 if (gpr_reg_num)
10881 fpr_size = (next_cum.fregno - FP_ARG_MIN_REG)
10882 * UNITS_PER_FP_WORD;
10883 if (cfun->va_list_fpr_size
10884 < FP_ARG_V4_MAX_REG + 1 - next_cum.fregno)
10885 fpr_size += cfun->va_list_fpr_size * UNITS_PER_FP_WORD;
10886 else
10887 fpr_size += (FP_ARG_V4_MAX_REG + 1 - next_cum.fregno)
10888 * UNITS_PER_FP_WORD;
10890 if (gpr_reg_num)
10892 offset = -((first_reg_offset * reg_size) & ~7);
10893 if (!fpr_size && gpr_reg_num > cfun->va_list_gpr_size)
10895 gpr_reg_num = cfun->va_list_gpr_size;
10896 if (reg_size == 4 && (first_reg_offset & 1))
10897 gpr_reg_num++;
10899 gpr_size = (gpr_reg_num * reg_size + 7) & ~7;
10901 else if (fpr_size)
10902 offset = - (int) (next_cum.fregno - FP_ARG_MIN_REG)
10903 * UNITS_PER_FP_WORD
10904 - (int) (GP_ARG_NUM_REG * reg_size);
10906 if (gpr_size + fpr_size)
10908 rtx reg_save_area
10909 = assign_stack_local (BLKmode, gpr_size + fpr_size, 64);
10910 gcc_assert (GET_CODE (reg_save_area) == MEM);
10911 reg_save_area = XEXP (reg_save_area, 0);
10912 if (GET_CODE (reg_save_area) == PLUS)
10914 gcc_assert (XEXP (reg_save_area, 0)
10915 == virtual_stack_vars_rtx);
10916 gcc_assert (GET_CODE (XEXP (reg_save_area, 1)) == CONST_INT);
10917 offset += INTVAL (XEXP (reg_save_area, 1));
10919 else
10920 gcc_assert (reg_save_area == virtual_stack_vars_rtx);
10923 cfun->machine->varargs_save_offset = offset;
10924 save_area = plus_constant (Pmode, virtual_stack_vars_rtx, offset);
10927 else
10929 first_reg_offset = next_cum.words;
10930 save_area = virtual_incoming_args_rtx;
10932 if (targetm.calls.must_pass_in_stack (mode, type))
10933 first_reg_offset += rs6000_arg_size (TYPE_MODE (type), type);
10936 set = get_varargs_alias_set ();
10937 if (! no_rtl && first_reg_offset < GP_ARG_NUM_REG
10938 && cfun->va_list_gpr_size)
10940 int n_gpr, nregs = GP_ARG_NUM_REG - first_reg_offset;
10942 if (va_list_gpr_counter_field)
10943 /* V4 va_list_gpr_size counts number of registers needed. */
10944 n_gpr = cfun->va_list_gpr_size;
10945 else
10946 /* char * va_list instead counts number of bytes needed. */
10947 n_gpr = (cfun->va_list_gpr_size + reg_size - 1) / reg_size;
10949 if (nregs > n_gpr)
10950 nregs = n_gpr;
10952 mem = gen_rtx_MEM (BLKmode,
10953 plus_constant (Pmode, save_area,
10954 first_reg_offset * reg_size));
10955 MEM_NOTRAP_P (mem) = 1;
10956 set_mem_alias_set (mem, set);
10957 set_mem_align (mem, BITS_PER_WORD);
10959 rs6000_move_block_from_reg (GP_ARG_MIN_REG + first_reg_offset, mem,
10960 nregs);
10963 /* Save FP registers if needed. */
10964 if (DEFAULT_ABI == ABI_V4
10965 && TARGET_HARD_FLOAT && TARGET_FPRS
10966 && ! no_rtl
10967 && next_cum.fregno <= FP_ARG_V4_MAX_REG
10968 && cfun->va_list_fpr_size)
10970 int fregno = next_cum.fregno, nregs;
10971 rtx cr1 = gen_rtx_REG (CCmode, CR1_REGNO);
10972 rtx lab = gen_label_rtx ();
10973 int off = (GP_ARG_NUM_REG * reg_size) + ((fregno - FP_ARG_MIN_REG)
10974 * UNITS_PER_FP_WORD);
10976 emit_jump_insn
10977 (gen_rtx_SET (VOIDmode,
10978 pc_rtx,
10979 gen_rtx_IF_THEN_ELSE (VOIDmode,
10980 gen_rtx_NE (VOIDmode, cr1,
10981 const0_rtx),
10982 gen_rtx_LABEL_REF (VOIDmode, lab),
10983 pc_rtx)));
10985 for (nregs = 0;
10986 fregno <= FP_ARG_V4_MAX_REG && nregs < cfun->va_list_fpr_size;
10987 fregno++, off += UNITS_PER_FP_WORD, nregs++)
10989 mem = gen_rtx_MEM ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
10990 ? DFmode : SFmode,
10991 plus_constant (Pmode, save_area, off));
10992 MEM_NOTRAP_P (mem) = 1;
10993 set_mem_alias_set (mem, set);
10994 set_mem_align (mem, GET_MODE_ALIGNMENT (
10995 (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
10996 ? DFmode : SFmode));
10997 emit_move_insn (mem, gen_rtx_REG (
10998 (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
10999 ? DFmode : SFmode, fregno));
11002 emit_label (lab);
11006 /* Create the va_list data type. */
11008 static tree
11009 rs6000_build_builtin_va_list (void)
11011 tree f_gpr, f_fpr, f_res, f_ovf, f_sav, record, type_decl;
11013 /* For AIX, prefer 'char *' because that's what the system
11014 header files like. */
11015 if (DEFAULT_ABI != ABI_V4)
11016 return build_pointer_type (char_type_node);
11018 record = (*lang_hooks.types.make_type) (RECORD_TYPE);
11019 type_decl = build_decl (BUILTINS_LOCATION, TYPE_DECL,
11020 get_identifier ("__va_list_tag"), record);
11022 f_gpr = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("gpr"),
11023 unsigned_char_type_node);
11024 f_fpr = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("fpr"),
11025 unsigned_char_type_node);
11026 /* Give the two bytes of padding a name, so that -Wpadded won't warn on
11027 every user file. */
11028 f_res = build_decl (BUILTINS_LOCATION, FIELD_DECL,
11029 get_identifier ("reserved"), short_unsigned_type_node);
11030 f_ovf = build_decl (BUILTINS_LOCATION, FIELD_DECL,
11031 get_identifier ("overflow_arg_area"),
11032 ptr_type_node);
11033 f_sav = build_decl (BUILTINS_LOCATION, FIELD_DECL,
11034 get_identifier ("reg_save_area"),
11035 ptr_type_node);
11037 va_list_gpr_counter_field = f_gpr;
11038 va_list_fpr_counter_field = f_fpr;
11040 DECL_FIELD_CONTEXT (f_gpr) = record;
11041 DECL_FIELD_CONTEXT (f_fpr) = record;
11042 DECL_FIELD_CONTEXT (f_res) = record;
11043 DECL_FIELD_CONTEXT (f_ovf) = record;
11044 DECL_FIELD_CONTEXT (f_sav) = record;
11046 TYPE_STUB_DECL (record) = type_decl;
11047 TYPE_NAME (record) = type_decl;
11048 TYPE_FIELDS (record) = f_gpr;
11049 DECL_CHAIN (f_gpr) = f_fpr;
11050 DECL_CHAIN (f_fpr) = f_res;
11051 DECL_CHAIN (f_res) = f_ovf;
11052 DECL_CHAIN (f_ovf) = f_sav;
11054 layout_type (record);
11056 /* The correct type is an array type of one element. */
11057 return build_array_type (record, build_index_type (size_zero_node));
11060 /* Implement va_start. */
11062 static void
11063 rs6000_va_start (tree valist, rtx nextarg)
11065 HOST_WIDE_INT words, n_gpr, n_fpr;
11066 tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
11067 tree gpr, fpr, ovf, sav, t;
11069 /* Only SVR4 needs something special. */
11070 if (DEFAULT_ABI != ABI_V4)
11072 std_expand_builtin_va_start (valist, nextarg);
11073 return;
11076 f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
11077 f_fpr = DECL_CHAIN (f_gpr);
11078 f_res = DECL_CHAIN (f_fpr);
11079 f_ovf = DECL_CHAIN (f_res);
11080 f_sav = DECL_CHAIN (f_ovf);
11082 valist = build_simple_mem_ref (valist);
11083 gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
11084 fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
11085 f_fpr, NULL_TREE);
11086 ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
11087 f_ovf, NULL_TREE);
11088 sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
11089 f_sav, NULL_TREE);
11091 /* Count number of gp and fp argument registers used. */
11092 words = crtl->args.info.words;
11093 n_gpr = MIN (crtl->args.info.sysv_gregno - GP_ARG_MIN_REG,
11094 GP_ARG_NUM_REG);
11095 n_fpr = MIN (crtl->args.info.fregno - FP_ARG_MIN_REG,
11096 FP_ARG_NUM_REG);
11098 if (TARGET_DEBUG_ARG)
11099 fprintf (stderr, "va_start: words = "HOST_WIDE_INT_PRINT_DEC", n_gpr = "
11100 HOST_WIDE_INT_PRINT_DEC", n_fpr = "HOST_WIDE_INT_PRINT_DEC"\n",
11101 words, n_gpr, n_fpr);
11103 if (cfun->va_list_gpr_size)
11105 t = build2 (MODIFY_EXPR, TREE_TYPE (gpr), gpr,
11106 build_int_cst (NULL_TREE, n_gpr));
11107 TREE_SIDE_EFFECTS (t) = 1;
11108 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
11111 if (cfun->va_list_fpr_size)
11113 t = build2 (MODIFY_EXPR, TREE_TYPE (fpr), fpr,
11114 build_int_cst (NULL_TREE, n_fpr));
11115 TREE_SIDE_EFFECTS (t) = 1;
11116 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
11118 #ifdef HAVE_AS_GNU_ATTRIBUTE
11119 if (call_ABI_of_interest (cfun->decl))
11120 rs6000_passes_float = true;
11121 #endif
11124 /* Find the overflow area. */
11125 t = make_tree (TREE_TYPE (ovf), virtual_incoming_args_rtx);
11126 if (words != 0)
11127 t = fold_build_pointer_plus_hwi (t, words * UNITS_PER_WORD);
11128 t = build2 (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
11129 TREE_SIDE_EFFECTS (t) = 1;
11130 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
11132 /* If there were no va_arg invocations, don't set up the register
11133 save area. */
11134 if (!cfun->va_list_gpr_size
11135 && !cfun->va_list_fpr_size
11136 && n_gpr < GP_ARG_NUM_REG
11137 && n_fpr < FP_ARG_V4_MAX_REG)
11138 return;
11140 /* Find the register save area. */
11141 t = make_tree (TREE_TYPE (sav), virtual_stack_vars_rtx);
11142 if (cfun->machine->varargs_save_offset)
11143 t = fold_build_pointer_plus_hwi (t, cfun->machine->varargs_save_offset);
11144 t = build2 (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
11145 TREE_SIDE_EFFECTS (t) = 1;
11146 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
11149 /* Implement va_arg. */
11151 static tree
11152 rs6000_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p,
11153 gimple_seq *post_p)
11155 tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
11156 tree gpr, fpr, ovf, sav, reg, t, u;
11157 int size, rsize, n_reg, sav_ofs, sav_scale;
11158 tree lab_false, lab_over, addr;
11159 int align;
11160 tree ptrtype = build_pointer_type_for_mode (type, ptr_mode, true);
11161 int regalign = 0;
11162 gimple stmt;
11164 if (pass_by_reference (NULL, TYPE_MODE (type), type, false))
11166 t = rs6000_gimplify_va_arg (valist, ptrtype, pre_p, post_p);
11167 return build_va_arg_indirect_ref (t);
11170 /* We need to deal with the fact that the darwin ppc64 ABI is defined by an
11171 earlier version of gcc, with the property that it always applied alignment
11172 adjustments to the va-args (even for zero-sized types). The cheapest way
11173 to deal with this is to replicate the effect of the part of
11174 std_gimplify_va_arg_expr that carries out the align adjust, for the case
11175 of relevance.
11176 We don't need to check for pass-by-reference because of the test above.
11177 We can return a simplifed answer, since we know there's no offset to add. */
11179 if (((TARGET_MACHO
11180 && rs6000_darwin64_abi)
11181 || DEFAULT_ABI == ABI_ELFv2
11182 || (DEFAULT_ABI == ABI_AIX && !rs6000_compat_align_parm))
11183 && integer_zerop (TYPE_SIZE (type)))
11185 unsigned HOST_WIDE_INT align, boundary;
11186 tree valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
11187 align = PARM_BOUNDARY / BITS_PER_UNIT;
11188 boundary = rs6000_function_arg_boundary (TYPE_MODE (type), type);
11189 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
11190 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
11191 boundary /= BITS_PER_UNIT;
11192 if (boundary > align)
11194 tree t ;
11195 /* This updates arg ptr by the amount that would be necessary
11196 to align the zero-sized (but not zero-alignment) item. */
11197 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
11198 fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
11199 gimplify_and_add (t, pre_p);
11201 t = fold_convert (sizetype, valist_tmp);
11202 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
11203 fold_convert (TREE_TYPE (valist),
11204 fold_build2 (BIT_AND_EXPR, sizetype, t,
11205 size_int (-boundary))));
11206 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
11207 gimplify_and_add (t, pre_p);
11209 /* Since it is zero-sized there's no increment for the item itself. */
11210 valist_tmp = fold_convert (build_pointer_type (type), valist_tmp);
11211 return build_va_arg_indirect_ref (valist_tmp);
11214 if (DEFAULT_ABI != ABI_V4)
11216 if (targetm.calls.split_complex_arg && TREE_CODE (type) == COMPLEX_TYPE)
11218 tree elem_type = TREE_TYPE (type);
11219 enum machine_mode elem_mode = TYPE_MODE (elem_type);
11220 int elem_size = GET_MODE_SIZE (elem_mode);
11222 if (elem_size < UNITS_PER_WORD)
11224 tree real_part, imag_part;
11225 gimple_seq post = NULL;
11227 real_part = rs6000_gimplify_va_arg (valist, elem_type, pre_p,
11228 &post);
11229 /* Copy the value into a temporary, lest the formal temporary
11230 be reused out from under us. */
11231 real_part = get_initialized_tmp_var (real_part, pre_p, &post);
11232 gimple_seq_add_seq (pre_p, post);
11234 imag_part = rs6000_gimplify_va_arg (valist, elem_type, pre_p,
11235 post_p);
11237 return build2 (COMPLEX_EXPR, type, real_part, imag_part);
11241 return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
11244 f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
11245 f_fpr = DECL_CHAIN (f_gpr);
11246 f_res = DECL_CHAIN (f_fpr);
11247 f_ovf = DECL_CHAIN (f_res);
11248 f_sav = DECL_CHAIN (f_ovf);
11250 valist = build_va_arg_indirect_ref (valist);
11251 gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
11252 fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
11253 f_fpr, NULL_TREE);
11254 ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
11255 f_ovf, NULL_TREE);
11256 sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
11257 f_sav, NULL_TREE);
11259 size = int_size_in_bytes (type);
11260 rsize = (size + 3) / 4;
11261 align = 1;
11263 if (TARGET_HARD_FLOAT && TARGET_FPRS
11264 && ((TARGET_SINGLE_FLOAT && TYPE_MODE (type) == SFmode)
11265 || (TARGET_DOUBLE_FLOAT
11266 && (TYPE_MODE (type) == DFmode
11267 || TYPE_MODE (type) == TFmode
11268 || TYPE_MODE (type) == SDmode
11269 || TYPE_MODE (type) == DDmode
11270 || TYPE_MODE (type) == TDmode))))
11272 /* FP args go in FP registers, if present. */
11273 reg = fpr;
11274 n_reg = (size + 7) / 8;
11275 sav_ofs = ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? 8 : 4) * 4;
11276 sav_scale = ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? 8 : 4);
11277 if (TYPE_MODE (type) != SFmode && TYPE_MODE (type) != SDmode)
11278 align = 8;
11280 else
11282 /* Otherwise into GP registers. */
11283 reg = gpr;
11284 n_reg = rsize;
11285 sav_ofs = 0;
11286 sav_scale = 4;
11287 if (n_reg == 2)
11288 align = 8;
11291 /* Pull the value out of the saved registers.... */
11293 lab_over = NULL;
11294 addr = create_tmp_var (ptr_type_node, "addr");
11296 /* AltiVec vectors never go in registers when -mabi=altivec. */
11297 if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (TYPE_MODE (type)))
11298 align = 16;
11299 else
11301 lab_false = create_artificial_label (input_location);
11302 lab_over = create_artificial_label (input_location);
11304 /* Long long and SPE vectors are aligned in the registers.
11305 As are any other 2 gpr item such as complex int due to a
11306 historical mistake. */
11307 u = reg;
11308 if (n_reg == 2 && reg == gpr)
11310 regalign = 1;
11311 u = build2 (BIT_AND_EXPR, TREE_TYPE (reg), unshare_expr (reg),
11312 build_int_cst (TREE_TYPE (reg), n_reg - 1));
11313 u = build2 (POSTINCREMENT_EXPR, TREE_TYPE (reg),
11314 unshare_expr (reg), u);
11316 /* _Decimal128 is passed in even/odd fpr pairs; the stored
11317 reg number is 0 for f1, so we want to make it odd. */
11318 else if (reg == fpr && TYPE_MODE (type) == TDmode)
11320 t = build2 (BIT_IOR_EXPR, TREE_TYPE (reg), unshare_expr (reg),
11321 build_int_cst (TREE_TYPE (reg), 1));
11322 u = build2 (MODIFY_EXPR, void_type_node, unshare_expr (reg), t);
11325 t = fold_convert (TREE_TYPE (reg), size_int (8 - n_reg + 1));
11326 t = build2 (GE_EXPR, boolean_type_node, u, t);
11327 u = build1 (GOTO_EXPR, void_type_node, lab_false);
11328 t = build3 (COND_EXPR, void_type_node, t, u, NULL_TREE);
11329 gimplify_and_add (t, pre_p);
11331 t = sav;
11332 if (sav_ofs)
11333 t = fold_build_pointer_plus_hwi (sav, sav_ofs);
11335 u = build2 (POSTINCREMENT_EXPR, TREE_TYPE (reg), unshare_expr (reg),
11336 build_int_cst (TREE_TYPE (reg), n_reg));
11337 u = fold_convert (sizetype, u);
11338 u = build2 (MULT_EXPR, sizetype, u, size_int (sav_scale));
11339 t = fold_build_pointer_plus (t, u);
11341 /* _Decimal32 varargs are located in the second word of the 64-bit
11342 FP register for 32-bit binaries. */
11343 if (!TARGET_POWERPC64
11344 && TARGET_HARD_FLOAT && TARGET_FPRS
11345 && TYPE_MODE (type) == SDmode)
11346 t = fold_build_pointer_plus_hwi (t, size);
11348 gimplify_assign (addr, t, pre_p);
11350 gimple_seq_add_stmt (pre_p, gimple_build_goto (lab_over));
11352 stmt = gimple_build_label (lab_false);
11353 gimple_seq_add_stmt (pre_p, stmt);
11355 if ((n_reg == 2 && !regalign) || n_reg > 2)
11357 /* Ensure that we don't find any more args in regs.
11358 Alignment has taken care of for special cases. */
11359 gimplify_assign (reg, build_int_cst (TREE_TYPE (reg), 8), pre_p);
11363 /* ... otherwise out of the overflow area. */
11365 /* Care for on-stack alignment if needed. */
11366 t = ovf;
11367 if (align != 1)
11369 t = fold_build_pointer_plus_hwi (t, align - 1);
11370 t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t,
11371 build_int_cst (TREE_TYPE (t), -align));
11373 gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
11375 gimplify_assign (unshare_expr (addr), t, pre_p);
11377 t = fold_build_pointer_plus_hwi (t, size);
11378 gimplify_assign (unshare_expr (ovf), t, pre_p);
11380 if (lab_over)
11382 stmt = gimple_build_label (lab_over);
11383 gimple_seq_add_stmt (pre_p, stmt);
11386 if (STRICT_ALIGNMENT
11387 && (TYPE_ALIGN (type)
11388 > (unsigned) BITS_PER_UNIT * (align < 4 ? 4 : align)))
11390 /* The value (of type complex double, for example) may not be
11391 aligned in memory in the saved registers, so copy via a
11392 temporary. (This is the same code as used for SPARC.) */
11393 tree tmp = create_tmp_var (type, "va_arg_tmp");
11394 tree dest_addr = build_fold_addr_expr (tmp);
11396 tree copy = build_call_expr (builtin_decl_implicit (BUILT_IN_MEMCPY),
11397 3, dest_addr, addr, size_int (rsize * 4));
11399 gimplify_and_add (copy, pre_p);
11400 addr = dest_addr;
11403 addr = fold_convert (ptrtype, addr);
11404 return build_va_arg_indirect_ref (addr);
11407 /* Builtins. */
11409 static void
11410 def_builtin (const char *name, tree type, enum rs6000_builtins code)
11412 tree t;
11413 unsigned classify = rs6000_builtin_info[(int)code].attr;
11414 const char *attr_string = "";
11416 gcc_assert (name != NULL);
11417 gcc_assert (IN_RANGE ((int)code, 0, (int)RS6000_BUILTIN_COUNT));
11419 if (rs6000_builtin_decls[(int)code])
11420 fatal_error ("internal error: builtin function %s already processed", name);
11422 rs6000_builtin_decls[(int)code] = t =
11423 add_builtin_function (name, type, (int)code, BUILT_IN_MD, NULL, NULL_TREE);
11425 /* Set any special attributes. */
11426 if ((classify & RS6000_BTC_CONST) != 0)
11428 /* const function, function only depends on the inputs. */
11429 TREE_READONLY (t) = 1;
11430 TREE_NOTHROW (t) = 1;
11431 attr_string = ", pure";
11433 else if ((classify & RS6000_BTC_PURE) != 0)
11435 /* pure function, function can read global memory, but does not set any
11436 external state. */
11437 DECL_PURE_P (t) = 1;
11438 TREE_NOTHROW (t) = 1;
11439 attr_string = ", const";
11441 else if ((classify & RS6000_BTC_FP) != 0)
11443 /* Function is a math function. If rounding mode is on, then treat the
11444 function as not reading global memory, but it can have arbitrary side
11445 effects. If it is off, then assume the function is a const function.
11446 This mimics the ATTR_MATHFN_FPROUNDING attribute in
11447 builtin-attribute.def that is used for the math functions. */
11448 TREE_NOTHROW (t) = 1;
11449 if (flag_rounding_math)
11451 DECL_PURE_P (t) = 1;
11452 DECL_IS_NOVOPS (t) = 1;
11453 attr_string = ", fp, pure";
11455 else
11457 TREE_READONLY (t) = 1;
11458 attr_string = ", fp, const";
11461 else if ((classify & RS6000_BTC_ATTR_MASK) != 0)
11462 gcc_unreachable ();
11464 if (TARGET_DEBUG_BUILTIN)
11465 fprintf (stderr, "rs6000_builtin, code = %4d, %s%s\n",
11466 (int)code, name, attr_string);
11469 /* Simple ternary operations: VECd = foo (VECa, VECb, VECc). */
11471 #undef RS6000_BUILTIN_1
11472 #undef RS6000_BUILTIN_2
11473 #undef RS6000_BUILTIN_3
11474 #undef RS6000_BUILTIN_A
11475 #undef RS6000_BUILTIN_D
11476 #undef RS6000_BUILTIN_E
11477 #undef RS6000_BUILTIN_H
11478 #undef RS6000_BUILTIN_P
11479 #undef RS6000_BUILTIN_Q
11480 #undef RS6000_BUILTIN_S
11481 #undef RS6000_BUILTIN_X
11483 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
11484 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
11485 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE) \
11486 { MASK, ICODE, NAME, ENUM },
11488 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
11489 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
11490 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
11491 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
11492 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
11493 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
11494 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
11495 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11497 static const struct builtin_description bdesc_3arg[] =
11499 #include "rs6000-builtin.def"
11502 /* DST operations: void foo (void *, const int, const char). */
11504 #undef RS6000_BUILTIN_1
11505 #undef RS6000_BUILTIN_2
11506 #undef RS6000_BUILTIN_3
11507 #undef RS6000_BUILTIN_A
11508 #undef RS6000_BUILTIN_D
11509 #undef RS6000_BUILTIN_E
11510 #undef RS6000_BUILTIN_H
11511 #undef RS6000_BUILTIN_P
11512 #undef RS6000_BUILTIN_Q
11513 #undef RS6000_BUILTIN_S
11514 #undef RS6000_BUILTIN_X
11516 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
11517 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
11518 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
11519 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
11520 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE) \
11521 { MASK, ICODE, NAME, ENUM },
11523 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
11524 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
11525 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
11526 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
11527 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
11528 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11530 static const struct builtin_description bdesc_dst[] =
11532 #include "rs6000-builtin.def"
11535 /* Simple binary operations: VECc = foo (VECa, VECb). */
11537 #undef RS6000_BUILTIN_1
11538 #undef RS6000_BUILTIN_2
11539 #undef RS6000_BUILTIN_3
11540 #undef RS6000_BUILTIN_A
11541 #undef RS6000_BUILTIN_D
11542 #undef RS6000_BUILTIN_E
11543 #undef RS6000_BUILTIN_H
11544 #undef RS6000_BUILTIN_P
11545 #undef RS6000_BUILTIN_Q
11546 #undef RS6000_BUILTIN_S
11547 #undef RS6000_BUILTIN_X
11549 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
11550 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE) \
11551 { MASK, ICODE, NAME, ENUM },
11553 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
11554 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
11555 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
11556 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
11557 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
11558 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
11559 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
11560 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
11561 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11563 static const struct builtin_description bdesc_2arg[] =
11565 #include "rs6000-builtin.def"
11568 #undef RS6000_BUILTIN_1
11569 #undef RS6000_BUILTIN_2
11570 #undef RS6000_BUILTIN_3
11571 #undef RS6000_BUILTIN_A
11572 #undef RS6000_BUILTIN_D
11573 #undef RS6000_BUILTIN_E
11574 #undef RS6000_BUILTIN_H
11575 #undef RS6000_BUILTIN_P
11576 #undef RS6000_BUILTIN_Q
11577 #undef RS6000_BUILTIN_S
11578 #undef RS6000_BUILTIN_X
11580 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
11581 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
11582 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
11583 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
11584 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
11585 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
11586 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
11587 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE) \
11588 { MASK, ICODE, NAME, ENUM },
11590 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
11591 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
11592 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11594 /* AltiVec predicates. */
11596 static const struct builtin_description bdesc_altivec_preds[] =
11598 #include "rs6000-builtin.def"
11601 /* SPE predicates. */
11602 #undef RS6000_BUILTIN_1
11603 #undef RS6000_BUILTIN_2
11604 #undef RS6000_BUILTIN_3
11605 #undef RS6000_BUILTIN_A
11606 #undef RS6000_BUILTIN_D
11607 #undef RS6000_BUILTIN_E
11608 #undef RS6000_BUILTIN_H
11609 #undef RS6000_BUILTIN_P
11610 #undef RS6000_BUILTIN_Q
11611 #undef RS6000_BUILTIN_S
11612 #undef RS6000_BUILTIN_X
11614 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
11615 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
11616 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
11617 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
11618 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
11619 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
11620 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
11621 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
11622 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
11623 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE) \
11624 { MASK, ICODE, NAME, ENUM },
11626 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11628 static const struct builtin_description bdesc_spe_predicates[] =
11630 #include "rs6000-builtin.def"
11633 /* SPE evsel predicates. */
11634 #undef RS6000_BUILTIN_1
11635 #undef RS6000_BUILTIN_2
11636 #undef RS6000_BUILTIN_3
11637 #undef RS6000_BUILTIN_A
11638 #undef RS6000_BUILTIN_D
11639 #undef RS6000_BUILTIN_E
11640 #undef RS6000_BUILTIN_H
11641 #undef RS6000_BUILTIN_P
11642 #undef RS6000_BUILTIN_Q
11643 #undef RS6000_BUILTIN_S
11644 #undef RS6000_BUILTIN_X
11646 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
11647 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
11648 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
11649 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
11650 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
11651 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE) \
11652 { MASK, ICODE, NAME, ENUM },
11654 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
11655 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
11656 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
11657 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
11658 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11660 static const struct builtin_description bdesc_spe_evsel[] =
11662 #include "rs6000-builtin.def"
11665 /* PAIRED predicates. */
11666 #undef RS6000_BUILTIN_1
11667 #undef RS6000_BUILTIN_2
11668 #undef RS6000_BUILTIN_3
11669 #undef RS6000_BUILTIN_A
11670 #undef RS6000_BUILTIN_D
11671 #undef RS6000_BUILTIN_E
11672 #undef RS6000_BUILTIN_H
11673 #undef RS6000_BUILTIN_P
11674 #undef RS6000_BUILTIN_Q
11675 #undef RS6000_BUILTIN_S
11676 #undef RS6000_BUILTIN_X
11678 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
11679 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
11680 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
11681 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
11682 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
11683 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
11684 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
11685 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
11686 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE) \
11687 { MASK, ICODE, NAME, ENUM },
11689 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
11690 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11692 static const struct builtin_description bdesc_paired_preds[] =
11694 #include "rs6000-builtin.def"
11697 /* ABS* operations. */
11699 #undef RS6000_BUILTIN_1
11700 #undef RS6000_BUILTIN_2
11701 #undef RS6000_BUILTIN_3
11702 #undef RS6000_BUILTIN_A
11703 #undef RS6000_BUILTIN_D
11704 #undef RS6000_BUILTIN_E
11705 #undef RS6000_BUILTIN_H
11706 #undef RS6000_BUILTIN_P
11707 #undef RS6000_BUILTIN_Q
11708 #undef RS6000_BUILTIN_S
11709 #undef RS6000_BUILTIN_X
11711 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
11712 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
11713 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
11714 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE) \
11715 { MASK, ICODE, NAME, ENUM },
11717 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
11718 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
11719 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
11720 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
11721 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
11722 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
11723 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11725 static const struct builtin_description bdesc_abs[] =
11727 #include "rs6000-builtin.def"
11730 /* Simple unary operations: VECb = foo (unsigned literal) or VECb =
11731 foo (VECa). */
11733 #undef RS6000_BUILTIN_1
11734 #undef RS6000_BUILTIN_2
11735 #undef RS6000_BUILTIN_3
11736 #undef RS6000_BUILTIN_A
11737 #undef RS6000_BUILTIN_D
11738 #undef RS6000_BUILTIN_E
11739 #undef RS6000_BUILTIN_H
11740 #undef RS6000_BUILTIN_P
11741 #undef RS6000_BUILTIN_Q
11742 #undef RS6000_BUILTIN_S
11743 #undef RS6000_BUILTIN_X
11745 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE) \
11746 { MASK, ICODE, NAME, ENUM },
11748 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
11749 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
11750 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
11751 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
11752 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
11753 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
11754 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
11755 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
11756 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
11757 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11759 static const struct builtin_description bdesc_1arg[] =
11761 #include "rs6000-builtin.def"
11764 /* HTM builtins. */
11765 #undef RS6000_BUILTIN_1
11766 #undef RS6000_BUILTIN_2
11767 #undef RS6000_BUILTIN_3
11768 #undef RS6000_BUILTIN_A
11769 #undef RS6000_BUILTIN_D
11770 #undef RS6000_BUILTIN_E
11771 #undef RS6000_BUILTIN_H
11772 #undef RS6000_BUILTIN_P
11773 #undef RS6000_BUILTIN_Q
11774 #undef RS6000_BUILTIN_S
11775 #undef RS6000_BUILTIN_X
11777 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
11778 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
11779 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
11780 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
11781 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
11782 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
11783 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE) \
11784 { MASK, ICODE, NAME, ENUM },
11786 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
11787 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
11788 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
11789 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11791 static const struct builtin_description bdesc_htm[] =
11793 #include "rs6000-builtin.def"
11796 #undef RS6000_BUILTIN_1
11797 #undef RS6000_BUILTIN_2
11798 #undef RS6000_BUILTIN_3
11799 #undef RS6000_BUILTIN_A
11800 #undef RS6000_BUILTIN_D
11801 #undef RS6000_BUILTIN_E
11802 #undef RS6000_BUILTIN_H
11803 #undef RS6000_BUILTIN_P
11804 #undef RS6000_BUILTIN_Q
11805 #undef RS6000_BUILTIN_S
11807 /* Return true if a builtin function is overloaded. */
11808 bool
11809 rs6000_overloaded_builtin_p (enum rs6000_builtins fncode)
11811 return (rs6000_builtin_info[(int)fncode].attr & RS6000_BTC_OVERLOADED) != 0;
11814 /* Expand an expression EXP that calls a builtin without arguments. */
11815 static rtx
11816 rs6000_expand_zeroop_builtin (enum insn_code icode, rtx target)
11818 rtx pat;
11819 enum machine_mode tmode = insn_data[icode].operand[0].mode;
11821 if (icode == CODE_FOR_nothing)
11822 /* Builtin not supported on this processor. */
11823 return 0;
11825 if (target == 0
11826 || GET_MODE (target) != tmode
11827 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11828 target = gen_reg_rtx (tmode);
11830 pat = GEN_FCN (icode) (target);
11831 if (! pat)
11832 return 0;
11833 emit_insn (pat);
11835 return target;
11839 static rtx
11840 rs6000_expand_mtfsf_builtin (enum insn_code icode, tree exp)
11842 rtx pat;
11843 tree arg0 = CALL_EXPR_ARG (exp, 0);
11844 tree arg1 = CALL_EXPR_ARG (exp, 1);
11845 rtx op0 = expand_normal (arg0);
11846 rtx op1 = expand_normal (arg1);
11847 enum machine_mode mode0 = insn_data[icode].operand[0].mode;
11848 enum machine_mode mode1 = insn_data[icode].operand[1].mode;
11850 if (icode == CODE_FOR_nothing)
11851 /* Builtin not supported on this processor. */
11852 return 0;
11854 /* If we got invalid arguments bail out before generating bad rtl. */
11855 if (arg0 == error_mark_node || arg1 == error_mark_node)
11856 return const0_rtx;
11858 if (GET_CODE (op0) != CONST_INT
11859 || INTVAL (op0) > 255
11860 || INTVAL (op0) < 0)
11862 error ("argument 1 must be an 8-bit field value");
11863 return const0_rtx;
11866 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
11867 op0 = copy_to_mode_reg (mode0, op0);
11869 if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
11870 op1 = copy_to_mode_reg (mode1, op1);
11872 pat = GEN_FCN (icode) (op0, op1);
11873 if (! pat)
11874 return const0_rtx;
11875 emit_insn (pat);
11877 return NULL_RTX;
11881 static rtx
11882 rs6000_expand_unop_builtin (enum insn_code icode, tree exp, rtx target)
11884 rtx pat;
11885 tree arg0 = CALL_EXPR_ARG (exp, 0);
11886 rtx op0 = expand_normal (arg0);
11887 enum machine_mode tmode = insn_data[icode].operand[0].mode;
11888 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11890 if (icode == CODE_FOR_nothing)
11891 /* Builtin not supported on this processor. */
11892 return 0;
11894 /* If we got invalid arguments bail out before generating bad rtl. */
11895 if (arg0 == error_mark_node)
11896 return const0_rtx;
11898 if (icode == CODE_FOR_altivec_vspltisb
11899 || icode == CODE_FOR_altivec_vspltish
11900 || icode == CODE_FOR_altivec_vspltisw
11901 || icode == CODE_FOR_spe_evsplatfi
11902 || icode == CODE_FOR_spe_evsplati)
11904 /* Only allow 5-bit *signed* literals. */
11905 if (GET_CODE (op0) != CONST_INT
11906 || INTVAL (op0) > 15
11907 || INTVAL (op0) < -16)
11909 error ("argument 1 must be a 5-bit signed literal");
11910 return const0_rtx;
11914 if (target == 0
11915 || GET_MODE (target) != tmode
11916 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11917 target = gen_reg_rtx (tmode);
11919 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11920 op0 = copy_to_mode_reg (mode0, op0);
11922 pat = GEN_FCN (icode) (target, op0);
11923 if (! pat)
11924 return 0;
11925 emit_insn (pat);
11927 return target;
11930 static rtx
11931 altivec_expand_abs_builtin (enum insn_code icode, tree exp, rtx target)
11933 rtx pat, scratch1, scratch2;
11934 tree arg0 = CALL_EXPR_ARG (exp, 0);
11935 rtx op0 = expand_normal (arg0);
11936 enum machine_mode tmode = insn_data[icode].operand[0].mode;
11937 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11939 /* If we have invalid arguments, bail out before generating bad rtl. */
11940 if (arg0 == error_mark_node)
11941 return const0_rtx;
11943 if (target == 0
11944 || GET_MODE (target) != tmode
11945 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11946 target = gen_reg_rtx (tmode);
11948 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11949 op0 = copy_to_mode_reg (mode0, op0);
11951 scratch1 = gen_reg_rtx (mode0);
11952 scratch2 = gen_reg_rtx (mode0);
11954 pat = GEN_FCN (icode) (target, op0, scratch1, scratch2);
11955 if (! pat)
11956 return 0;
11957 emit_insn (pat);
11959 return target;
11962 static rtx
11963 rs6000_expand_binop_builtin (enum insn_code icode, tree exp, rtx target)
11965 rtx pat;
11966 tree arg0 = CALL_EXPR_ARG (exp, 0);
11967 tree arg1 = CALL_EXPR_ARG (exp, 1);
11968 rtx op0 = expand_normal (arg0);
11969 rtx op1 = expand_normal (arg1);
11970 enum machine_mode tmode = insn_data[icode].operand[0].mode;
11971 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11972 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
11974 if (icode == CODE_FOR_nothing)
11975 /* Builtin not supported on this processor. */
11976 return 0;
11978 /* If we got invalid arguments bail out before generating bad rtl. */
11979 if (arg0 == error_mark_node || arg1 == error_mark_node)
11980 return const0_rtx;
11982 if (icode == CODE_FOR_altivec_vcfux
11983 || icode == CODE_FOR_altivec_vcfsx
11984 || icode == CODE_FOR_altivec_vctsxs
11985 || icode == CODE_FOR_altivec_vctuxs
11986 || icode == CODE_FOR_altivec_vspltb
11987 || icode == CODE_FOR_altivec_vsplth
11988 || icode == CODE_FOR_altivec_vspltw
11989 || icode == CODE_FOR_spe_evaddiw
11990 || icode == CODE_FOR_spe_evldd
11991 || icode == CODE_FOR_spe_evldh
11992 || icode == CODE_FOR_spe_evldw
11993 || icode == CODE_FOR_spe_evlhhesplat
11994 || icode == CODE_FOR_spe_evlhhossplat
11995 || icode == CODE_FOR_spe_evlhhousplat
11996 || icode == CODE_FOR_spe_evlwhe
11997 || icode == CODE_FOR_spe_evlwhos
11998 || icode == CODE_FOR_spe_evlwhou
11999 || icode == CODE_FOR_spe_evlwhsplat
12000 || icode == CODE_FOR_spe_evlwwsplat
12001 || icode == CODE_FOR_spe_evrlwi
12002 || icode == CODE_FOR_spe_evslwi
12003 || icode == CODE_FOR_spe_evsrwis
12004 || icode == CODE_FOR_spe_evsubifw
12005 || icode == CODE_FOR_spe_evsrwiu)
12007 /* Only allow 5-bit unsigned literals. */
12008 STRIP_NOPS (arg1);
12009 if (TREE_CODE (arg1) != INTEGER_CST
12010 || TREE_INT_CST_LOW (arg1) & ~0x1f)
12012 error ("argument 2 must be a 5-bit unsigned literal");
12013 return const0_rtx;
12017 if (target == 0
12018 || GET_MODE (target) != tmode
12019 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12020 target = gen_reg_rtx (tmode);
12022 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12023 op0 = copy_to_mode_reg (mode0, op0);
12024 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12025 op1 = copy_to_mode_reg (mode1, op1);
12027 pat = GEN_FCN (icode) (target, op0, op1);
12028 if (! pat)
12029 return 0;
12030 emit_insn (pat);
12032 return target;
12035 static rtx
12036 altivec_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
12038 rtx pat, scratch;
12039 tree cr6_form = CALL_EXPR_ARG (exp, 0);
12040 tree arg0 = CALL_EXPR_ARG (exp, 1);
12041 tree arg1 = CALL_EXPR_ARG (exp, 2);
12042 rtx op0 = expand_normal (arg0);
12043 rtx op1 = expand_normal (arg1);
12044 enum machine_mode tmode = SImode;
12045 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12046 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
12047 int cr6_form_int;
12049 if (TREE_CODE (cr6_form) != INTEGER_CST)
12051 error ("argument 1 of __builtin_altivec_predicate must be a constant");
12052 return const0_rtx;
12054 else
12055 cr6_form_int = TREE_INT_CST_LOW (cr6_form);
12057 gcc_assert (mode0 == mode1);
12059 /* If we have invalid arguments, bail out before generating bad rtl. */
12060 if (arg0 == error_mark_node || arg1 == error_mark_node)
12061 return const0_rtx;
12063 if (target == 0
12064 || GET_MODE (target) != tmode
12065 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12066 target = gen_reg_rtx (tmode);
12068 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12069 op0 = copy_to_mode_reg (mode0, op0);
12070 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12071 op1 = copy_to_mode_reg (mode1, op1);
12073 scratch = gen_reg_rtx (mode0);
12075 pat = GEN_FCN (icode) (scratch, op0, op1);
12076 if (! pat)
12077 return 0;
12078 emit_insn (pat);
12080 /* The vec_any* and vec_all* predicates use the same opcodes for two
12081 different operations, but the bits in CR6 will be different
12082 depending on what information we want. So we have to play tricks
12083 with CR6 to get the right bits out.
12085 If you think this is disgusting, look at the specs for the
12086 AltiVec predicates. */
12088 switch (cr6_form_int)
12090 case 0:
12091 emit_insn (gen_cr6_test_for_zero (target));
12092 break;
12093 case 1:
12094 emit_insn (gen_cr6_test_for_zero_reverse (target));
12095 break;
12096 case 2:
12097 emit_insn (gen_cr6_test_for_lt (target));
12098 break;
12099 case 3:
12100 emit_insn (gen_cr6_test_for_lt_reverse (target));
12101 break;
12102 default:
12103 error ("argument 1 of __builtin_altivec_predicate is out of range");
12104 break;
12107 return target;
12110 static rtx
12111 paired_expand_lv_builtin (enum insn_code icode, tree exp, rtx target)
12113 rtx pat, addr;
12114 tree arg0 = CALL_EXPR_ARG (exp, 0);
12115 tree arg1 = CALL_EXPR_ARG (exp, 1);
12116 enum machine_mode tmode = insn_data[icode].operand[0].mode;
12117 enum machine_mode mode0 = Pmode;
12118 enum machine_mode mode1 = Pmode;
12119 rtx op0 = expand_normal (arg0);
12120 rtx op1 = expand_normal (arg1);
12122 if (icode == CODE_FOR_nothing)
12123 /* Builtin not supported on this processor. */
12124 return 0;
12126 /* If we got invalid arguments bail out before generating bad rtl. */
12127 if (arg0 == error_mark_node || arg1 == error_mark_node)
12128 return const0_rtx;
12130 if (target == 0
12131 || GET_MODE (target) != tmode
12132 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12133 target = gen_reg_rtx (tmode);
12135 op1 = copy_to_mode_reg (mode1, op1);
12137 if (op0 == const0_rtx)
12139 addr = gen_rtx_MEM (tmode, op1);
12141 else
12143 op0 = copy_to_mode_reg (mode0, op0);
12144 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op0, op1));
12147 pat = GEN_FCN (icode) (target, addr);
12149 if (! pat)
12150 return 0;
12151 emit_insn (pat);
12153 return target;
12156 /* Return a constant vector for use as a little-endian permute control vector
12157 to reverse the order of elements of the given vector mode. */
12158 static rtx
12159 swap_selector_for_mode (enum machine_mode mode)
12161 /* These are little endian vectors, so their elements are reversed
12162 from what you would normally expect for a permute control vector. */
12163 unsigned int swap2[16] = {7,6,5,4,3,2,1,0,15,14,13,12,11,10,9,8};
12164 unsigned int swap4[16] = {3,2,1,0,7,6,5,4,11,10,9,8,15,14,13,12};
12165 unsigned int swap8[16] = {1,0,3,2,5,4,7,6,9,8,11,10,13,12,15,14};
12166 unsigned int swap16[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
12167 unsigned int *swaparray, i;
12168 rtx perm[16];
12170 switch (mode)
12172 case V2DFmode:
12173 case V2DImode:
12174 swaparray = swap2;
12175 break;
12176 case V4SFmode:
12177 case V4SImode:
12178 swaparray = swap4;
12179 break;
12180 case V8HImode:
12181 swaparray = swap8;
12182 break;
12183 case V16QImode:
12184 swaparray = swap16;
12185 break;
12186 default:
12187 gcc_unreachable ();
12190 for (i = 0; i < 16; ++i)
12191 perm[i] = GEN_INT (swaparray[i]);
12193 return force_reg (V16QImode, gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, perm)));
12196 /* Generate code for an "lvx", "lvxl", or "lve*x" built-in for a little endian target
12197 with -maltivec=be specified. Issue the load followed by an element-reversing
12198 permute. */
12199 void
12200 altivec_expand_lvx_be (rtx op0, rtx op1, enum machine_mode mode, unsigned unspec)
12202 rtx tmp = gen_reg_rtx (mode);
12203 rtx load = gen_rtx_SET (VOIDmode, tmp, op1);
12204 rtx lvx = gen_rtx_UNSPEC (mode, gen_rtvec (1, const0_rtx), unspec);
12205 rtx par = gen_rtx_PARALLEL (mode, gen_rtvec (2, load, lvx));
12206 rtx sel = swap_selector_for_mode (mode);
12207 rtx vperm = gen_rtx_UNSPEC (mode, gen_rtvec (3, tmp, tmp, sel), UNSPEC_VPERM);
12209 gcc_assert (REG_P (op0));
12210 emit_insn (par);
12211 emit_insn (gen_rtx_SET (VOIDmode, op0, vperm));
12214 /* Generate code for a "stvx" or "stvxl" built-in for a little endian target
12215 with -maltivec=be specified. Issue the store preceded by an element-reversing
12216 permute. */
12217 void
12218 altivec_expand_stvx_be (rtx op0, rtx op1, enum machine_mode mode, unsigned unspec)
12220 rtx tmp = gen_reg_rtx (mode);
12221 rtx store = gen_rtx_SET (VOIDmode, op0, tmp);
12222 rtx stvx = gen_rtx_UNSPEC (mode, gen_rtvec (1, const0_rtx), unspec);
12223 rtx par = gen_rtx_PARALLEL (mode, gen_rtvec (2, store, stvx));
12224 rtx sel = swap_selector_for_mode (mode);
12225 rtx vperm;
12227 gcc_assert (REG_P (op1));
12228 vperm = gen_rtx_UNSPEC (mode, gen_rtvec (3, op1, op1, sel), UNSPEC_VPERM);
12229 emit_insn (gen_rtx_SET (VOIDmode, tmp, vperm));
12230 emit_insn (par);
12233 /* Generate code for a "stve*x" built-in for a little endian target with -maltivec=be
12234 specified. Issue the store preceded by an element-reversing permute. */
12235 void
12236 altivec_expand_stvex_be (rtx op0, rtx op1, enum machine_mode mode, unsigned unspec)
12238 enum machine_mode inner_mode = GET_MODE_INNER (mode);
12239 rtx tmp = gen_reg_rtx (mode);
12240 rtx stvx = gen_rtx_UNSPEC (inner_mode, gen_rtvec (1, tmp), unspec);
12241 rtx sel = swap_selector_for_mode (mode);
12242 rtx vperm;
12244 gcc_assert (REG_P (op1));
12245 vperm = gen_rtx_UNSPEC (mode, gen_rtvec (3, op1, op1, sel), UNSPEC_VPERM);
12246 emit_insn (gen_rtx_SET (VOIDmode, tmp, vperm));
12247 emit_insn (gen_rtx_SET (VOIDmode, op0, stvx));
12250 static rtx
12251 altivec_expand_lv_builtin (enum insn_code icode, tree exp, rtx target, bool blk)
12253 rtx pat, addr;
12254 tree arg0 = CALL_EXPR_ARG (exp, 0);
12255 tree arg1 = CALL_EXPR_ARG (exp, 1);
12256 enum machine_mode tmode = insn_data[icode].operand[0].mode;
12257 enum machine_mode mode0 = Pmode;
12258 enum machine_mode mode1 = Pmode;
12259 rtx op0 = expand_normal (arg0);
12260 rtx op1 = expand_normal (arg1);
12262 if (icode == CODE_FOR_nothing)
12263 /* Builtin not supported on this processor. */
12264 return 0;
12266 /* If we got invalid arguments bail out before generating bad rtl. */
12267 if (arg0 == error_mark_node || arg1 == error_mark_node)
12268 return const0_rtx;
12270 if (target == 0
12271 || GET_MODE (target) != tmode
12272 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12273 target = gen_reg_rtx (tmode);
12275 op1 = copy_to_mode_reg (mode1, op1);
12277 if (op0 == const0_rtx)
12279 addr = gen_rtx_MEM (blk ? BLKmode : tmode, op1);
12281 else
12283 op0 = copy_to_mode_reg (mode0, op0);
12284 addr = gen_rtx_MEM (blk ? BLKmode : tmode, gen_rtx_PLUS (Pmode, op0, op1));
12287 pat = GEN_FCN (icode) (target, addr);
12289 if (! pat)
12290 return 0;
12291 emit_insn (pat);
12293 return target;
12296 static rtx
12297 spe_expand_stv_builtin (enum insn_code icode, tree exp)
12299 tree arg0 = CALL_EXPR_ARG (exp, 0);
12300 tree arg1 = CALL_EXPR_ARG (exp, 1);
12301 tree arg2 = CALL_EXPR_ARG (exp, 2);
12302 rtx op0 = expand_normal (arg0);
12303 rtx op1 = expand_normal (arg1);
12304 rtx op2 = expand_normal (arg2);
12305 rtx pat;
12306 enum machine_mode mode0 = insn_data[icode].operand[0].mode;
12307 enum machine_mode mode1 = insn_data[icode].operand[1].mode;
12308 enum machine_mode mode2 = insn_data[icode].operand[2].mode;
12310 /* Invalid arguments. Bail before doing anything stoopid! */
12311 if (arg0 == error_mark_node
12312 || arg1 == error_mark_node
12313 || arg2 == error_mark_node)
12314 return const0_rtx;
12316 if (! (*insn_data[icode].operand[2].predicate) (op0, mode2))
12317 op0 = copy_to_mode_reg (mode2, op0);
12318 if (! (*insn_data[icode].operand[0].predicate) (op1, mode0))
12319 op1 = copy_to_mode_reg (mode0, op1);
12320 if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
12321 op2 = copy_to_mode_reg (mode1, op2);
12323 pat = GEN_FCN (icode) (op1, op2, op0);
12324 if (pat)
12325 emit_insn (pat);
12326 return NULL_RTX;
12329 static rtx
12330 paired_expand_stv_builtin (enum insn_code icode, tree exp)
12332 tree arg0 = CALL_EXPR_ARG (exp, 0);
12333 tree arg1 = CALL_EXPR_ARG (exp, 1);
12334 tree arg2 = CALL_EXPR_ARG (exp, 2);
12335 rtx op0 = expand_normal (arg0);
12336 rtx op1 = expand_normal (arg1);
12337 rtx op2 = expand_normal (arg2);
12338 rtx pat, addr;
12339 enum machine_mode tmode = insn_data[icode].operand[0].mode;
12340 enum machine_mode mode1 = Pmode;
12341 enum machine_mode mode2 = Pmode;
12343 /* Invalid arguments. Bail before doing anything stoopid! */
12344 if (arg0 == error_mark_node
12345 || arg1 == error_mark_node
12346 || arg2 == error_mark_node)
12347 return const0_rtx;
12349 if (! (*insn_data[icode].operand[1].predicate) (op0, tmode))
12350 op0 = copy_to_mode_reg (tmode, op0);
12352 op2 = copy_to_mode_reg (mode2, op2);
12354 if (op1 == const0_rtx)
12356 addr = gen_rtx_MEM (tmode, op2);
12358 else
12360 op1 = copy_to_mode_reg (mode1, op1);
12361 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op1, op2));
12364 pat = GEN_FCN (icode) (addr, op0);
12365 if (pat)
12366 emit_insn (pat);
12367 return NULL_RTX;
12370 static rtx
12371 altivec_expand_stv_builtin (enum insn_code icode, tree exp)
12373 tree arg0 = CALL_EXPR_ARG (exp, 0);
12374 tree arg1 = CALL_EXPR_ARG (exp, 1);
12375 tree arg2 = CALL_EXPR_ARG (exp, 2);
12376 rtx op0 = expand_normal (arg0);
12377 rtx op1 = expand_normal (arg1);
12378 rtx op2 = expand_normal (arg2);
12379 rtx pat, addr;
12380 enum machine_mode tmode = insn_data[icode].operand[0].mode;
12381 enum machine_mode smode = insn_data[icode].operand[1].mode;
12382 enum machine_mode mode1 = Pmode;
12383 enum machine_mode mode2 = Pmode;
12385 /* Invalid arguments. Bail before doing anything stoopid! */
12386 if (arg0 == error_mark_node
12387 || arg1 == error_mark_node
12388 || arg2 == error_mark_node)
12389 return const0_rtx;
12391 if (! (*insn_data[icode].operand[1].predicate) (op0, smode))
12392 op0 = copy_to_mode_reg (smode, op0);
12394 op2 = copy_to_mode_reg (mode2, op2);
12396 if (op1 == const0_rtx)
12398 addr = gen_rtx_MEM (tmode, op2);
12400 else
12402 op1 = copy_to_mode_reg (mode1, op1);
12403 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op1, op2));
12406 pat = GEN_FCN (icode) (addr, op0);
12407 if (pat)
12408 emit_insn (pat);
12409 return NULL_RTX;
12412 /* Return the appropriate SPR number associated with the given builtin. */
12413 static inline HOST_WIDE_INT
12414 htm_spr_num (enum rs6000_builtins code)
12416 if (code == HTM_BUILTIN_GET_TFHAR
12417 || code == HTM_BUILTIN_SET_TFHAR)
12418 return TFHAR_SPR;
12419 else if (code == HTM_BUILTIN_GET_TFIAR
12420 || code == HTM_BUILTIN_SET_TFIAR)
12421 return TFIAR_SPR;
12422 else if (code == HTM_BUILTIN_GET_TEXASR
12423 || code == HTM_BUILTIN_SET_TEXASR)
12424 return TEXASR_SPR;
12425 gcc_assert (code == HTM_BUILTIN_GET_TEXASRU
12426 || code == HTM_BUILTIN_SET_TEXASRU);
12427 return TEXASRU_SPR;
12430 /* Return the appropriate SPR regno associated with the given builtin. */
12431 static inline HOST_WIDE_INT
12432 htm_spr_regno (enum rs6000_builtins code)
12434 if (code == HTM_BUILTIN_GET_TFHAR
12435 || code == HTM_BUILTIN_SET_TFHAR)
12436 return TFHAR_REGNO;
12437 else if (code == HTM_BUILTIN_GET_TFIAR
12438 || code == HTM_BUILTIN_SET_TFIAR)
12439 return TFIAR_REGNO;
12440 gcc_assert (code == HTM_BUILTIN_GET_TEXASR
12441 || code == HTM_BUILTIN_SET_TEXASR
12442 || code == HTM_BUILTIN_GET_TEXASRU
12443 || code == HTM_BUILTIN_SET_TEXASRU);
12444 return TEXASR_REGNO;
12447 /* Return the correct ICODE value depending on whether we are
12448 setting or reading the HTM SPRs. */
12449 static inline enum insn_code
12450 rs6000_htm_spr_icode (bool nonvoid)
12452 if (nonvoid)
12453 return (TARGET_64BIT) ? CODE_FOR_htm_mfspr_di : CODE_FOR_htm_mfspr_si;
12454 else
12455 return (TARGET_64BIT) ? CODE_FOR_htm_mtspr_di : CODE_FOR_htm_mtspr_si;
12458 /* Expand the HTM builtin in EXP and store the result in TARGET.
12459 Store true in *EXPANDEDP if we found a builtin to expand. */
12460 static rtx
12461 htm_expand_builtin (tree exp, rtx target, bool * expandedp)
12463 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
12464 bool nonvoid = TREE_TYPE (TREE_TYPE (fndecl)) != void_type_node;
12465 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
12466 const struct builtin_description *d;
12467 size_t i;
12469 *expandedp = false;
12471 /* Expand the HTM builtins. */
12472 d = bdesc_htm;
12473 for (i = 0; i < ARRAY_SIZE (bdesc_htm); i++, d++)
12474 if (d->code == fcode)
12476 rtx op[MAX_HTM_OPERANDS], pat;
12477 int nopnds = 0;
12478 tree arg;
12479 call_expr_arg_iterator iter;
12480 unsigned attr = rs6000_builtin_info[fcode].attr;
12481 enum insn_code icode = d->icode;
12483 if (attr & RS6000_BTC_SPR)
12484 icode = rs6000_htm_spr_icode (nonvoid);
12486 if (nonvoid)
12488 enum machine_mode tmode = insn_data[icode].operand[0].mode;
12489 if (!target
12490 || GET_MODE (target) != tmode
12491 || !(*insn_data[icode].operand[0].predicate) (target, tmode))
12492 target = gen_reg_rtx (tmode);
12493 op[nopnds++] = target;
12496 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
12498 const struct insn_operand_data *insn_op;
12500 if (arg == error_mark_node || nopnds >= MAX_HTM_OPERANDS)
12501 return NULL_RTX;
12503 insn_op = &insn_data[icode].operand[nopnds];
12505 op[nopnds] = expand_normal (arg);
12507 if (!(*insn_op->predicate) (op[nopnds], insn_op->mode))
12509 if (!strcmp (insn_op->constraint, "n"))
12511 int arg_num = (nonvoid) ? nopnds : nopnds + 1;
12512 if (!CONST_INT_P (op[nopnds]))
12513 error ("argument %d must be an unsigned literal", arg_num);
12514 else
12515 error ("argument %d is an unsigned literal that is "
12516 "out of range", arg_num);
12517 return const0_rtx;
12519 op[nopnds] = copy_to_mode_reg (insn_op->mode, op[nopnds]);
12522 nopnds++;
12525 /* Handle the builtins for extended mnemonics. These accept
12526 no arguments, but map to builtins that take arguments. */
12527 switch (fcode)
12529 case HTM_BUILTIN_TENDALL: /* Alias for: tend. 1 */
12530 case HTM_BUILTIN_TRESUME: /* Alias for: tsr. 1 */
12531 op[nopnds++] = GEN_INT (1);
12532 #ifdef ENABLE_CHECKING
12533 attr |= RS6000_BTC_UNARY;
12534 #endif
12535 break;
12536 case HTM_BUILTIN_TSUSPEND: /* Alias for: tsr. 0 */
12537 op[nopnds++] = GEN_INT (0);
12538 #ifdef ENABLE_CHECKING
12539 attr |= RS6000_BTC_UNARY;
12540 #endif
12541 break;
12542 default:
12543 break;
12546 /* If this builtin accesses SPRs, then pass in the appropriate
12547 SPR number and SPR regno as the last two operands. */
12548 if (attr & RS6000_BTC_SPR)
12550 op[nopnds++] = gen_rtx_CONST_INT (Pmode, htm_spr_num (fcode));
12551 op[nopnds++] = gen_rtx_REG (Pmode, htm_spr_regno (fcode));
12554 #ifdef ENABLE_CHECKING
12555 int expected_nopnds = 0;
12556 if ((attr & RS6000_BTC_TYPE_MASK) == RS6000_BTC_UNARY)
12557 expected_nopnds = 1;
12558 else if ((attr & RS6000_BTC_TYPE_MASK) == RS6000_BTC_BINARY)
12559 expected_nopnds = 2;
12560 else if ((attr & RS6000_BTC_TYPE_MASK) == RS6000_BTC_TERNARY)
12561 expected_nopnds = 3;
12562 if (!(attr & RS6000_BTC_VOID))
12563 expected_nopnds += 1;
12564 if (attr & RS6000_BTC_SPR)
12565 expected_nopnds += 2;
12567 gcc_assert (nopnds == expected_nopnds && nopnds <= MAX_HTM_OPERANDS);
12568 #endif
12570 switch (nopnds)
12572 case 1:
12573 pat = GEN_FCN (icode) (op[0]);
12574 break;
12575 case 2:
12576 pat = GEN_FCN (icode) (op[0], op[1]);
12577 break;
12578 case 3:
12579 pat = GEN_FCN (icode) (op[0], op[1], op[2]);
12580 break;
12581 case 4:
12582 pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3]);
12583 break;
12584 default:
12585 gcc_unreachable ();
12587 if (!pat)
12588 return NULL_RTX;
12589 emit_insn (pat);
12591 *expandedp = true;
12592 if (nonvoid)
12593 return target;
12594 return const0_rtx;
12597 return NULL_RTX;
12600 static rtx
12601 rs6000_expand_ternop_builtin (enum insn_code icode, tree exp, rtx target)
12603 rtx pat;
12604 tree arg0 = CALL_EXPR_ARG (exp, 0);
12605 tree arg1 = CALL_EXPR_ARG (exp, 1);
12606 tree arg2 = CALL_EXPR_ARG (exp, 2);
12607 rtx op0 = expand_normal (arg0);
12608 rtx op1 = expand_normal (arg1);
12609 rtx op2 = expand_normal (arg2);
12610 enum machine_mode tmode = insn_data[icode].operand[0].mode;
12611 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12612 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
12613 enum machine_mode mode2 = insn_data[icode].operand[3].mode;
12615 if (icode == CODE_FOR_nothing)
12616 /* Builtin not supported on this processor. */
12617 return 0;
12619 /* If we got invalid arguments bail out before generating bad rtl. */
12620 if (arg0 == error_mark_node
12621 || arg1 == error_mark_node
12622 || arg2 == error_mark_node)
12623 return const0_rtx;
12625 /* Check and prepare argument depending on the instruction code.
12627 Note that a switch statement instead of the sequence of tests
12628 would be incorrect as many of the CODE_FOR values could be
12629 CODE_FOR_nothing and that would yield multiple alternatives
12630 with identical values. We'd never reach here at runtime in
12631 this case. */
12632 if (icode == CODE_FOR_altivec_vsldoi_v4sf
12633 || icode == CODE_FOR_altivec_vsldoi_v4si
12634 || icode == CODE_FOR_altivec_vsldoi_v8hi
12635 || icode == CODE_FOR_altivec_vsldoi_v16qi)
12637 /* Only allow 4-bit unsigned literals. */
12638 STRIP_NOPS (arg2);
12639 if (TREE_CODE (arg2) != INTEGER_CST
12640 || TREE_INT_CST_LOW (arg2) & ~0xf)
12642 error ("argument 3 must be a 4-bit unsigned literal");
12643 return const0_rtx;
12646 else if (icode == CODE_FOR_vsx_xxpermdi_v2df
12647 || icode == CODE_FOR_vsx_xxpermdi_v2di
12648 || icode == CODE_FOR_vsx_xxsldwi_v16qi
12649 || icode == CODE_FOR_vsx_xxsldwi_v8hi
12650 || icode == CODE_FOR_vsx_xxsldwi_v4si
12651 || icode == CODE_FOR_vsx_xxsldwi_v4sf
12652 || icode == CODE_FOR_vsx_xxsldwi_v2di
12653 || icode == CODE_FOR_vsx_xxsldwi_v2df)
12655 /* Only allow 2-bit unsigned literals. */
12656 STRIP_NOPS (arg2);
12657 if (TREE_CODE (arg2) != INTEGER_CST
12658 || TREE_INT_CST_LOW (arg2) & ~0x3)
12660 error ("argument 3 must be a 2-bit unsigned literal");
12661 return const0_rtx;
12664 else if (icode == CODE_FOR_vsx_set_v2df
12665 || icode == CODE_FOR_vsx_set_v2di
12666 || icode == CODE_FOR_bcdadd
12667 || icode == CODE_FOR_bcdadd_lt
12668 || icode == CODE_FOR_bcdadd_eq
12669 || icode == CODE_FOR_bcdadd_gt
12670 || icode == CODE_FOR_bcdsub
12671 || icode == CODE_FOR_bcdsub_lt
12672 || icode == CODE_FOR_bcdsub_eq
12673 || icode == CODE_FOR_bcdsub_gt)
12675 /* Only allow 1-bit unsigned literals. */
12676 STRIP_NOPS (arg2);
12677 if (TREE_CODE (arg2) != INTEGER_CST
12678 || TREE_INT_CST_LOW (arg2) & ~0x1)
12680 error ("argument 3 must be a 1-bit unsigned literal");
12681 return const0_rtx;
12684 else if (icode == CODE_FOR_dfp_ddedpd_dd
12685 || icode == CODE_FOR_dfp_ddedpd_td)
12687 /* Only allow 2-bit unsigned literals where the value is 0 or 2. */
12688 STRIP_NOPS (arg0);
12689 if (TREE_CODE (arg0) != INTEGER_CST
12690 || TREE_INT_CST_LOW (arg2) & ~0x3)
12692 error ("argument 1 must be 0 or 2");
12693 return const0_rtx;
12696 else if (icode == CODE_FOR_dfp_denbcd_dd
12697 || icode == CODE_FOR_dfp_denbcd_td)
12699 /* Only allow 1-bit unsigned literals. */
12700 STRIP_NOPS (arg0);
12701 if (TREE_CODE (arg0) != INTEGER_CST
12702 || TREE_INT_CST_LOW (arg0) & ~0x1)
12704 error ("argument 1 must be a 1-bit unsigned literal");
12705 return const0_rtx;
12708 else if (icode == CODE_FOR_dfp_dscli_dd
12709 || icode == CODE_FOR_dfp_dscli_td
12710 || icode == CODE_FOR_dfp_dscri_dd
12711 || icode == CODE_FOR_dfp_dscri_td)
12713 /* Only allow 6-bit unsigned literals. */
12714 STRIP_NOPS (arg1);
12715 if (TREE_CODE (arg1) != INTEGER_CST
12716 || TREE_INT_CST_LOW (arg1) & ~0x3f)
12718 error ("argument 2 must be a 6-bit unsigned literal");
12719 return const0_rtx;
12722 else if (icode == CODE_FOR_crypto_vshasigmaw
12723 || icode == CODE_FOR_crypto_vshasigmad)
12725 /* Check whether the 2nd and 3rd arguments are integer constants and in
12726 range and prepare arguments. */
12727 STRIP_NOPS (arg1);
12728 if (TREE_CODE (arg1) != INTEGER_CST || wi::geu_p (arg1, 2))
12730 error ("argument 2 must be 0 or 1");
12731 return const0_rtx;
12734 STRIP_NOPS (arg2);
12735 if (TREE_CODE (arg2) != INTEGER_CST || wi::geu_p (arg1, 16))
12737 error ("argument 3 must be in the range 0..15");
12738 return const0_rtx;
12742 if (target == 0
12743 || GET_MODE (target) != tmode
12744 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12745 target = gen_reg_rtx (tmode);
12747 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12748 op0 = copy_to_mode_reg (mode0, op0);
12749 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12750 op1 = copy_to_mode_reg (mode1, op1);
12751 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12752 op2 = copy_to_mode_reg (mode2, op2);
12754 if (TARGET_PAIRED_FLOAT && icode == CODE_FOR_selv2sf4)
12755 pat = GEN_FCN (icode) (target, op0, op1, op2, CONST0_RTX (SFmode));
12756 else
12757 pat = GEN_FCN (icode) (target, op0, op1, op2);
12758 if (! pat)
12759 return 0;
12760 emit_insn (pat);
12762 return target;
12765 /* Expand the lvx builtins. */
12766 static rtx
12767 altivec_expand_ld_builtin (tree exp, rtx target, bool *expandedp)
12769 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
12770 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
12771 tree arg0;
12772 enum machine_mode tmode, mode0;
12773 rtx pat, op0;
12774 enum insn_code icode;
12776 switch (fcode)
12778 case ALTIVEC_BUILTIN_LD_INTERNAL_16qi:
12779 icode = CODE_FOR_vector_altivec_load_v16qi;
12780 break;
12781 case ALTIVEC_BUILTIN_LD_INTERNAL_8hi:
12782 icode = CODE_FOR_vector_altivec_load_v8hi;
12783 break;
12784 case ALTIVEC_BUILTIN_LD_INTERNAL_4si:
12785 icode = CODE_FOR_vector_altivec_load_v4si;
12786 break;
12787 case ALTIVEC_BUILTIN_LD_INTERNAL_4sf:
12788 icode = CODE_FOR_vector_altivec_load_v4sf;
12789 break;
12790 case ALTIVEC_BUILTIN_LD_INTERNAL_2df:
12791 icode = CODE_FOR_vector_altivec_load_v2df;
12792 break;
12793 case ALTIVEC_BUILTIN_LD_INTERNAL_2di:
12794 icode = CODE_FOR_vector_altivec_load_v2di;
12795 case ALTIVEC_BUILTIN_LD_INTERNAL_1ti:
12796 icode = CODE_FOR_vector_altivec_load_v1ti;
12797 break;
12798 default:
12799 *expandedp = false;
12800 return NULL_RTX;
12803 *expandedp = true;
12805 arg0 = CALL_EXPR_ARG (exp, 0);
12806 op0 = expand_normal (arg0);
12807 tmode = insn_data[icode].operand[0].mode;
12808 mode0 = insn_data[icode].operand[1].mode;
12810 if (target == 0
12811 || GET_MODE (target) != tmode
12812 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12813 target = gen_reg_rtx (tmode);
12815 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12816 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
12818 pat = GEN_FCN (icode) (target, op0);
12819 if (! pat)
12820 return 0;
12821 emit_insn (pat);
12822 return target;
12825 /* Expand the stvx builtins. */
12826 static rtx
12827 altivec_expand_st_builtin (tree exp, rtx target ATTRIBUTE_UNUSED,
12828 bool *expandedp)
12830 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
12831 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
12832 tree arg0, arg1;
12833 enum machine_mode mode0, mode1;
12834 rtx pat, op0, op1;
12835 enum insn_code icode;
12837 switch (fcode)
12839 case ALTIVEC_BUILTIN_ST_INTERNAL_16qi:
12840 icode = CODE_FOR_vector_altivec_store_v16qi;
12841 break;
12842 case ALTIVEC_BUILTIN_ST_INTERNAL_8hi:
12843 icode = CODE_FOR_vector_altivec_store_v8hi;
12844 break;
12845 case ALTIVEC_BUILTIN_ST_INTERNAL_4si:
12846 icode = CODE_FOR_vector_altivec_store_v4si;
12847 break;
12848 case ALTIVEC_BUILTIN_ST_INTERNAL_4sf:
12849 icode = CODE_FOR_vector_altivec_store_v4sf;
12850 break;
12851 case ALTIVEC_BUILTIN_ST_INTERNAL_2df:
12852 icode = CODE_FOR_vector_altivec_store_v2df;
12853 break;
12854 case ALTIVEC_BUILTIN_ST_INTERNAL_2di:
12855 icode = CODE_FOR_vector_altivec_store_v2di;
12856 case ALTIVEC_BUILTIN_ST_INTERNAL_1ti:
12857 icode = CODE_FOR_vector_altivec_store_v1ti;
12858 break;
12859 default:
12860 *expandedp = false;
12861 return NULL_RTX;
12864 arg0 = CALL_EXPR_ARG (exp, 0);
12865 arg1 = CALL_EXPR_ARG (exp, 1);
12866 op0 = expand_normal (arg0);
12867 op1 = expand_normal (arg1);
12868 mode0 = insn_data[icode].operand[0].mode;
12869 mode1 = insn_data[icode].operand[1].mode;
12871 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
12872 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
12873 if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
12874 op1 = copy_to_mode_reg (mode1, op1);
12876 pat = GEN_FCN (icode) (op0, op1);
12877 if (pat)
12878 emit_insn (pat);
12880 *expandedp = true;
12881 return NULL_RTX;
12884 /* Expand the dst builtins. */
12885 static rtx
12886 altivec_expand_dst_builtin (tree exp, rtx target ATTRIBUTE_UNUSED,
12887 bool *expandedp)
12889 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
12890 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
12891 tree arg0, arg1, arg2;
12892 enum machine_mode mode0, mode1;
12893 rtx pat, op0, op1, op2;
12894 const struct builtin_description *d;
12895 size_t i;
12897 *expandedp = false;
12899 /* Handle DST variants. */
12900 d = bdesc_dst;
12901 for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
12902 if (d->code == fcode)
12904 arg0 = CALL_EXPR_ARG (exp, 0);
12905 arg1 = CALL_EXPR_ARG (exp, 1);
12906 arg2 = CALL_EXPR_ARG (exp, 2);
12907 op0 = expand_normal (arg0);
12908 op1 = expand_normal (arg1);
12909 op2 = expand_normal (arg2);
12910 mode0 = insn_data[d->icode].operand[0].mode;
12911 mode1 = insn_data[d->icode].operand[1].mode;
12913 /* Invalid arguments, bail out before generating bad rtl. */
12914 if (arg0 == error_mark_node
12915 || arg1 == error_mark_node
12916 || arg2 == error_mark_node)
12917 return const0_rtx;
12919 *expandedp = true;
12920 STRIP_NOPS (arg2);
12921 if (TREE_CODE (arg2) != INTEGER_CST
12922 || TREE_INT_CST_LOW (arg2) & ~0x3)
12924 error ("argument to %qs must be a 2-bit unsigned literal", d->name);
12925 return const0_rtx;
12928 if (! (*insn_data[d->icode].operand[0].predicate) (op0, mode0))
12929 op0 = copy_to_mode_reg (Pmode, op0);
12930 if (! (*insn_data[d->icode].operand[1].predicate) (op1, mode1))
12931 op1 = copy_to_mode_reg (mode1, op1);
12933 pat = GEN_FCN (d->icode) (op0, op1, op2);
12934 if (pat != 0)
12935 emit_insn (pat);
12937 return NULL_RTX;
12940 return NULL_RTX;
12943 /* Expand vec_init builtin. */
12944 static rtx
12945 altivec_expand_vec_init_builtin (tree type, tree exp, rtx target)
12947 enum machine_mode tmode = TYPE_MODE (type);
12948 enum machine_mode inner_mode = GET_MODE_INNER (tmode);
12949 int i, n_elt = GET_MODE_NUNITS (tmode);
12951 gcc_assert (VECTOR_MODE_P (tmode));
12952 gcc_assert (n_elt == call_expr_nargs (exp));
12954 if (!target || !register_operand (target, tmode))
12955 target = gen_reg_rtx (tmode);
12957 /* If we have a vector compromised of a single element, such as V1TImode, do
12958 the initialization directly. */
12959 if (n_elt == 1 && GET_MODE_SIZE (tmode) == GET_MODE_SIZE (inner_mode))
12961 rtx x = expand_normal (CALL_EXPR_ARG (exp, 0));
12962 emit_move_insn (target, gen_lowpart (tmode, x));
12964 else
12966 rtvec v = rtvec_alloc (n_elt);
12968 for (i = 0; i < n_elt; ++i)
12970 rtx x = expand_normal (CALL_EXPR_ARG (exp, i));
12971 RTVEC_ELT (v, i) = gen_lowpart (inner_mode, x);
12974 rs6000_expand_vector_init (target, gen_rtx_PARALLEL (tmode, v));
12977 return target;
12980 /* Return the integer constant in ARG. Constrain it to be in the range
12981 of the subparts of VEC_TYPE; issue an error if not. */
12983 static int
12984 get_element_number (tree vec_type, tree arg)
12986 unsigned HOST_WIDE_INT elt, max = TYPE_VECTOR_SUBPARTS (vec_type) - 1;
12988 if (!tree_fits_uhwi_p (arg)
12989 || (elt = tree_to_uhwi (arg), elt > max))
12991 error ("selector must be an integer constant in the range 0..%wi", max);
12992 return 0;
12995 return elt;
12998 /* Expand vec_set builtin. */
12999 static rtx
13000 altivec_expand_vec_set_builtin (tree exp)
13002 enum machine_mode tmode, mode1;
13003 tree arg0, arg1, arg2;
13004 int elt;
13005 rtx op0, op1;
13007 arg0 = CALL_EXPR_ARG (exp, 0);
13008 arg1 = CALL_EXPR_ARG (exp, 1);
13009 arg2 = CALL_EXPR_ARG (exp, 2);
13011 tmode = TYPE_MODE (TREE_TYPE (arg0));
13012 mode1 = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
13013 gcc_assert (VECTOR_MODE_P (tmode));
13015 op0 = expand_expr (arg0, NULL_RTX, tmode, EXPAND_NORMAL);
13016 op1 = expand_expr (arg1, NULL_RTX, mode1, EXPAND_NORMAL);
13017 elt = get_element_number (TREE_TYPE (arg0), arg2);
13019 if (GET_MODE (op1) != mode1 && GET_MODE (op1) != VOIDmode)
13020 op1 = convert_modes (mode1, GET_MODE (op1), op1, true);
13022 op0 = force_reg (tmode, op0);
13023 op1 = force_reg (mode1, op1);
13025 rs6000_expand_vector_set (op0, op1, elt);
13027 return op0;
13030 /* Expand vec_ext builtin. */
13031 static rtx
13032 altivec_expand_vec_ext_builtin (tree exp, rtx target)
13034 enum machine_mode tmode, mode0;
13035 tree arg0, arg1;
13036 int elt;
13037 rtx op0;
13039 arg0 = CALL_EXPR_ARG (exp, 0);
13040 arg1 = CALL_EXPR_ARG (exp, 1);
13042 op0 = expand_normal (arg0);
13043 elt = get_element_number (TREE_TYPE (arg0), arg1);
13045 tmode = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
13046 mode0 = TYPE_MODE (TREE_TYPE (arg0));
13047 gcc_assert (VECTOR_MODE_P (mode0));
13049 op0 = force_reg (mode0, op0);
13051 if (optimize || !target || !register_operand (target, tmode))
13052 target = gen_reg_rtx (tmode);
13054 rs6000_expand_vector_extract (target, op0, elt);
13056 return target;
13059 /* Expand the builtin in EXP and store the result in TARGET. Store
13060 true in *EXPANDEDP if we found a builtin to expand. */
13061 static rtx
13062 altivec_expand_builtin (tree exp, rtx target, bool *expandedp)
13064 const struct builtin_description *d;
13065 size_t i;
13066 enum insn_code icode;
13067 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
13068 tree arg0;
13069 rtx op0, pat;
13070 enum machine_mode tmode, mode0;
13071 enum rs6000_builtins fcode
13072 = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
13074 if (rs6000_overloaded_builtin_p (fcode))
13076 *expandedp = true;
13077 error ("unresolved overload for Altivec builtin %qF", fndecl);
13079 /* Given it is invalid, just generate a normal call. */
13080 return expand_call (exp, target, false);
13083 target = altivec_expand_ld_builtin (exp, target, expandedp);
13084 if (*expandedp)
13085 return target;
13087 target = altivec_expand_st_builtin (exp, target, expandedp);
13088 if (*expandedp)
13089 return target;
13091 target = altivec_expand_dst_builtin (exp, target, expandedp);
13092 if (*expandedp)
13093 return target;
13095 *expandedp = true;
13097 switch (fcode)
13099 case ALTIVEC_BUILTIN_STVX_V2DF:
13100 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v2df, exp);
13101 case ALTIVEC_BUILTIN_STVX_V2DI:
13102 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v2di, exp);
13103 case ALTIVEC_BUILTIN_STVX_V4SF:
13104 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v4sf, exp);
13105 case ALTIVEC_BUILTIN_STVX:
13106 case ALTIVEC_BUILTIN_STVX_V4SI:
13107 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v4si, exp);
13108 case ALTIVEC_BUILTIN_STVX_V8HI:
13109 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v8hi, exp);
13110 case ALTIVEC_BUILTIN_STVX_V16QI:
13111 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v16qi, exp);
13112 case ALTIVEC_BUILTIN_STVEBX:
13113 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvebx, exp);
13114 case ALTIVEC_BUILTIN_STVEHX:
13115 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvehx, exp);
13116 case ALTIVEC_BUILTIN_STVEWX:
13117 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvewx, exp);
13118 case ALTIVEC_BUILTIN_STVXL_V2DF:
13119 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v2df, exp);
13120 case ALTIVEC_BUILTIN_STVXL_V2DI:
13121 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v2di, exp);
13122 case ALTIVEC_BUILTIN_STVXL_V4SF:
13123 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v4sf, exp);
13124 case ALTIVEC_BUILTIN_STVXL:
13125 case ALTIVEC_BUILTIN_STVXL_V4SI:
13126 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v4si, exp);
13127 case ALTIVEC_BUILTIN_STVXL_V8HI:
13128 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v8hi, exp);
13129 case ALTIVEC_BUILTIN_STVXL_V16QI:
13130 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v16qi, exp);
13132 case ALTIVEC_BUILTIN_STVLX:
13133 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvlx, exp);
13134 case ALTIVEC_BUILTIN_STVLXL:
13135 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvlxl, exp);
13136 case ALTIVEC_BUILTIN_STVRX:
13137 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvrx, exp);
13138 case ALTIVEC_BUILTIN_STVRXL:
13139 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvrxl, exp);
13141 case VSX_BUILTIN_STXVD2X_V1TI:
13142 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v1ti, exp);
13143 case VSX_BUILTIN_STXVD2X_V2DF:
13144 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v2df, exp);
13145 case VSX_BUILTIN_STXVD2X_V2DI:
13146 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v2di, exp);
13147 case VSX_BUILTIN_STXVW4X_V4SF:
13148 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v4sf, exp);
13149 case VSX_BUILTIN_STXVW4X_V4SI:
13150 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v4si, exp);
13151 case VSX_BUILTIN_STXVW4X_V8HI:
13152 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v8hi, exp);
13153 case VSX_BUILTIN_STXVW4X_V16QI:
13154 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v16qi, exp);
13156 case ALTIVEC_BUILTIN_MFVSCR:
13157 icode = CODE_FOR_altivec_mfvscr;
13158 tmode = insn_data[icode].operand[0].mode;
13160 if (target == 0
13161 || GET_MODE (target) != tmode
13162 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13163 target = gen_reg_rtx (tmode);
13165 pat = GEN_FCN (icode) (target);
13166 if (! pat)
13167 return 0;
13168 emit_insn (pat);
13169 return target;
13171 case ALTIVEC_BUILTIN_MTVSCR:
13172 icode = CODE_FOR_altivec_mtvscr;
13173 arg0 = CALL_EXPR_ARG (exp, 0);
13174 op0 = expand_normal (arg0);
13175 mode0 = insn_data[icode].operand[0].mode;
13177 /* If we got invalid arguments bail out before generating bad rtl. */
13178 if (arg0 == error_mark_node)
13179 return const0_rtx;
13181 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
13182 op0 = copy_to_mode_reg (mode0, op0);
13184 pat = GEN_FCN (icode) (op0);
13185 if (pat)
13186 emit_insn (pat);
13187 return NULL_RTX;
13189 case ALTIVEC_BUILTIN_DSSALL:
13190 emit_insn (gen_altivec_dssall ());
13191 return NULL_RTX;
13193 case ALTIVEC_BUILTIN_DSS:
13194 icode = CODE_FOR_altivec_dss;
13195 arg0 = CALL_EXPR_ARG (exp, 0);
13196 STRIP_NOPS (arg0);
13197 op0 = expand_normal (arg0);
13198 mode0 = insn_data[icode].operand[0].mode;
13200 /* If we got invalid arguments bail out before generating bad rtl. */
13201 if (arg0 == error_mark_node)
13202 return const0_rtx;
13204 if (TREE_CODE (arg0) != INTEGER_CST
13205 || TREE_INT_CST_LOW (arg0) & ~0x3)
13207 error ("argument to dss must be a 2-bit unsigned literal");
13208 return const0_rtx;
13211 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
13212 op0 = copy_to_mode_reg (mode0, op0);
13214 emit_insn (gen_altivec_dss (op0));
13215 return NULL_RTX;
13217 case ALTIVEC_BUILTIN_VEC_INIT_V4SI:
13218 case ALTIVEC_BUILTIN_VEC_INIT_V8HI:
13219 case ALTIVEC_BUILTIN_VEC_INIT_V16QI:
13220 case ALTIVEC_BUILTIN_VEC_INIT_V4SF:
13221 case VSX_BUILTIN_VEC_INIT_V2DF:
13222 case VSX_BUILTIN_VEC_INIT_V2DI:
13223 case VSX_BUILTIN_VEC_INIT_V1TI:
13224 return altivec_expand_vec_init_builtin (TREE_TYPE (exp), exp, target);
13226 case ALTIVEC_BUILTIN_VEC_SET_V4SI:
13227 case ALTIVEC_BUILTIN_VEC_SET_V8HI:
13228 case ALTIVEC_BUILTIN_VEC_SET_V16QI:
13229 case ALTIVEC_BUILTIN_VEC_SET_V4SF:
13230 case VSX_BUILTIN_VEC_SET_V2DF:
13231 case VSX_BUILTIN_VEC_SET_V2DI:
13232 case VSX_BUILTIN_VEC_SET_V1TI:
13233 return altivec_expand_vec_set_builtin (exp);
13235 case ALTIVEC_BUILTIN_VEC_EXT_V4SI:
13236 case ALTIVEC_BUILTIN_VEC_EXT_V8HI:
13237 case ALTIVEC_BUILTIN_VEC_EXT_V16QI:
13238 case ALTIVEC_BUILTIN_VEC_EXT_V4SF:
13239 case VSX_BUILTIN_VEC_EXT_V2DF:
13240 case VSX_BUILTIN_VEC_EXT_V2DI:
13241 case VSX_BUILTIN_VEC_EXT_V1TI:
13242 return altivec_expand_vec_ext_builtin (exp, target);
13244 default:
13245 break;
13246 /* Fall through. */
13249 /* Expand abs* operations. */
13250 d = bdesc_abs;
13251 for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
13252 if (d->code == fcode)
13253 return altivec_expand_abs_builtin (d->icode, exp, target);
13255 /* Expand the AltiVec predicates. */
13256 d = bdesc_altivec_preds;
13257 for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, d++)
13258 if (d->code == fcode)
13259 return altivec_expand_predicate_builtin (d->icode, exp, target);
13261 /* LV* are funky. We initialized them differently. */
13262 switch (fcode)
13264 case ALTIVEC_BUILTIN_LVSL:
13265 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsl,
13266 exp, target, false);
13267 case ALTIVEC_BUILTIN_LVSR:
13268 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsr,
13269 exp, target, false);
13270 case ALTIVEC_BUILTIN_LVEBX:
13271 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvebx,
13272 exp, target, false);
13273 case ALTIVEC_BUILTIN_LVEHX:
13274 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvehx,
13275 exp, target, false);
13276 case ALTIVEC_BUILTIN_LVEWX:
13277 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvewx,
13278 exp, target, false);
13279 case ALTIVEC_BUILTIN_LVXL_V2DF:
13280 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v2df,
13281 exp, target, false);
13282 case ALTIVEC_BUILTIN_LVXL_V2DI:
13283 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v2di,
13284 exp, target, false);
13285 case ALTIVEC_BUILTIN_LVXL_V4SF:
13286 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v4sf,
13287 exp, target, false);
13288 case ALTIVEC_BUILTIN_LVXL:
13289 case ALTIVEC_BUILTIN_LVXL_V4SI:
13290 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v4si,
13291 exp, target, false);
13292 case ALTIVEC_BUILTIN_LVXL_V8HI:
13293 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v8hi,
13294 exp, target, false);
13295 case ALTIVEC_BUILTIN_LVXL_V16QI:
13296 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v16qi,
13297 exp, target, false);
13298 case ALTIVEC_BUILTIN_LVX_V2DF:
13299 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v2df,
13300 exp, target, false);
13301 case ALTIVEC_BUILTIN_LVX_V2DI:
13302 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v2di,
13303 exp, target, false);
13304 case ALTIVEC_BUILTIN_LVX_V4SF:
13305 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v4sf,
13306 exp, target, false);
13307 case ALTIVEC_BUILTIN_LVX:
13308 case ALTIVEC_BUILTIN_LVX_V4SI:
13309 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v4si,
13310 exp, target, false);
13311 case ALTIVEC_BUILTIN_LVX_V8HI:
13312 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v8hi,
13313 exp, target, false);
13314 case ALTIVEC_BUILTIN_LVX_V16QI:
13315 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v16qi,
13316 exp, target, false);
13317 case ALTIVEC_BUILTIN_LVLX:
13318 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvlx,
13319 exp, target, true);
13320 case ALTIVEC_BUILTIN_LVLXL:
13321 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvlxl,
13322 exp, target, true);
13323 case ALTIVEC_BUILTIN_LVRX:
13324 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvrx,
13325 exp, target, true);
13326 case ALTIVEC_BUILTIN_LVRXL:
13327 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvrxl,
13328 exp, target, true);
13329 case VSX_BUILTIN_LXVD2X_V1TI:
13330 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v1ti,
13331 exp, target, false);
13332 case VSX_BUILTIN_LXVD2X_V2DF:
13333 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v2df,
13334 exp, target, false);
13335 case VSX_BUILTIN_LXVD2X_V2DI:
13336 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v2di,
13337 exp, target, false);
13338 case VSX_BUILTIN_LXVW4X_V4SF:
13339 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v4sf,
13340 exp, target, false);
13341 case VSX_BUILTIN_LXVW4X_V4SI:
13342 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v4si,
13343 exp, target, false);
13344 case VSX_BUILTIN_LXVW4X_V8HI:
13345 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v8hi,
13346 exp, target, false);
13347 case VSX_BUILTIN_LXVW4X_V16QI:
13348 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v16qi,
13349 exp, target, false);
13350 break;
13351 default:
13352 break;
13353 /* Fall through. */
13356 *expandedp = false;
13357 return NULL_RTX;
13360 /* Expand the builtin in EXP and store the result in TARGET. Store
13361 true in *EXPANDEDP if we found a builtin to expand. */
13362 static rtx
13363 paired_expand_builtin (tree exp, rtx target, bool * expandedp)
13365 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
13366 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
13367 const struct builtin_description *d;
13368 size_t i;
13370 *expandedp = true;
13372 switch (fcode)
13374 case PAIRED_BUILTIN_STX:
13375 return paired_expand_stv_builtin (CODE_FOR_paired_stx, exp);
13376 case PAIRED_BUILTIN_LX:
13377 return paired_expand_lv_builtin (CODE_FOR_paired_lx, exp, target);
13378 default:
13379 break;
13380 /* Fall through. */
13383 /* Expand the paired predicates. */
13384 d = bdesc_paired_preds;
13385 for (i = 0; i < ARRAY_SIZE (bdesc_paired_preds); i++, d++)
13386 if (d->code == fcode)
13387 return paired_expand_predicate_builtin (d->icode, exp, target);
13389 *expandedp = false;
13390 return NULL_RTX;
13393 /* Binops that need to be initialized manually, but can be expanded
13394 automagically by rs6000_expand_binop_builtin. */
13395 static const struct builtin_description bdesc_2arg_spe[] =
13397 { RS6000_BTM_SPE, CODE_FOR_spe_evlddx, "__builtin_spe_evlddx", SPE_BUILTIN_EVLDDX },
13398 { RS6000_BTM_SPE, CODE_FOR_spe_evldwx, "__builtin_spe_evldwx", SPE_BUILTIN_EVLDWX },
13399 { RS6000_BTM_SPE, CODE_FOR_spe_evldhx, "__builtin_spe_evldhx", SPE_BUILTIN_EVLDHX },
13400 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhex, "__builtin_spe_evlwhex", SPE_BUILTIN_EVLWHEX },
13401 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhoux, "__builtin_spe_evlwhoux", SPE_BUILTIN_EVLWHOUX },
13402 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhosx, "__builtin_spe_evlwhosx", SPE_BUILTIN_EVLWHOSX },
13403 { RS6000_BTM_SPE, CODE_FOR_spe_evlwwsplatx, "__builtin_spe_evlwwsplatx", SPE_BUILTIN_EVLWWSPLATX },
13404 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhsplatx, "__builtin_spe_evlwhsplatx", SPE_BUILTIN_EVLWHSPLATX },
13405 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhesplatx, "__builtin_spe_evlhhesplatx", SPE_BUILTIN_EVLHHESPLATX },
13406 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhousplatx, "__builtin_spe_evlhhousplatx", SPE_BUILTIN_EVLHHOUSPLATX },
13407 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhossplatx, "__builtin_spe_evlhhossplatx", SPE_BUILTIN_EVLHHOSSPLATX },
13408 { RS6000_BTM_SPE, CODE_FOR_spe_evldd, "__builtin_spe_evldd", SPE_BUILTIN_EVLDD },
13409 { RS6000_BTM_SPE, CODE_FOR_spe_evldw, "__builtin_spe_evldw", SPE_BUILTIN_EVLDW },
13410 { RS6000_BTM_SPE, CODE_FOR_spe_evldh, "__builtin_spe_evldh", SPE_BUILTIN_EVLDH },
13411 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhe, "__builtin_spe_evlwhe", SPE_BUILTIN_EVLWHE },
13412 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhou, "__builtin_spe_evlwhou", SPE_BUILTIN_EVLWHOU },
13413 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhos, "__builtin_spe_evlwhos", SPE_BUILTIN_EVLWHOS },
13414 { RS6000_BTM_SPE, CODE_FOR_spe_evlwwsplat, "__builtin_spe_evlwwsplat", SPE_BUILTIN_EVLWWSPLAT },
13415 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhsplat, "__builtin_spe_evlwhsplat", SPE_BUILTIN_EVLWHSPLAT },
13416 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhesplat, "__builtin_spe_evlhhesplat", SPE_BUILTIN_EVLHHESPLAT },
13417 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhousplat, "__builtin_spe_evlhhousplat", SPE_BUILTIN_EVLHHOUSPLAT },
13418 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhossplat, "__builtin_spe_evlhhossplat", SPE_BUILTIN_EVLHHOSSPLAT }
13421 /* Expand the builtin in EXP and store the result in TARGET. Store
13422 true in *EXPANDEDP if we found a builtin to expand.
13424 This expands the SPE builtins that are not simple unary and binary
13425 operations. */
13426 static rtx
13427 spe_expand_builtin (tree exp, rtx target, bool *expandedp)
13429 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
13430 tree arg1, arg0;
13431 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
13432 enum insn_code icode;
13433 enum machine_mode tmode, mode0;
13434 rtx pat, op0;
13435 const struct builtin_description *d;
13436 size_t i;
13438 *expandedp = true;
13440 /* Syntax check for a 5-bit unsigned immediate. */
13441 switch (fcode)
13443 case SPE_BUILTIN_EVSTDD:
13444 case SPE_BUILTIN_EVSTDH:
13445 case SPE_BUILTIN_EVSTDW:
13446 case SPE_BUILTIN_EVSTWHE:
13447 case SPE_BUILTIN_EVSTWHO:
13448 case SPE_BUILTIN_EVSTWWE:
13449 case SPE_BUILTIN_EVSTWWO:
13450 arg1 = CALL_EXPR_ARG (exp, 2);
13451 if (TREE_CODE (arg1) != INTEGER_CST
13452 || TREE_INT_CST_LOW (arg1) & ~0x1f)
13454 error ("argument 2 must be a 5-bit unsigned literal");
13455 return const0_rtx;
13457 break;
13458 default:
13459 break;
13462 /* The evsplat*i instructions are not quite generic. */
13463 switch (fcode)
13465 case SPE_BUILTIN_EVSPLATFI:
13466 return rs6000_expand_unop_builtin (CODE_FOR_spe_evsplatfi,
13467 exp, target);
13468 case SPE_BUILTIN_EVSPLATI:
13469 return rs6000_expand_unop_builtin (CODE_FOR_spe_evsplati,
13470 exp, target);
13471 default:
13472 break;
13475 d = bdesc_2arg_spe;
13476 for (i = 0; i < ARRAY_SIZE (bdesc_2arg_spe); ++i, ++d)
13477 if (d->code == fcode)
13478 return rs6000_expand_binop_builtin (d->icode, exp, target);
13480 d = bdesc_spe_predicates;
13481 for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, ++d)
13482 if (d->code == fcode)
13483 return spe_expand_predicate_builtin (d->icode, exp, target);
13485 d = bdesc_spe_evsel;
13486 for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, ++d)
13487 if (d->code == fcode)
13488 return spe_expand_evsel_builtin (d->icode, exp, target);
13490 switch (fcode)
13492 case SPE_BUILTIN_EVSTDDX:
13493 return spe_expand_stv_builtin (CODE_FOR_spe_evstddx, exp);
13494 case SPE_BUILTIN_EVSTDHX:
13495 return spe_expand_stv_builtin (CODE_FOR_spe_evstdhx, exp);
13496 case SPE_BUILTIN_EVSTDWX:
13497 return spe_expand_stv_builtin (CODE_FOR_spe_evstdwx, exp);
13498 case SPE_BUILTIN_EVSTWHEX:
13499 return spe_expand_stv_builtin (CODE_FOR_spe_evstwhex, exp);
13500 case SPE_BUILTIN_EVSTWHOX:
13501 return spe_expand_stv_builtin (CODE_FOR_spe_evstwhox, exp);
13502 case SPE_BUILTIN_EVSTWWEX:
13503 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwex, exp);
13504 case SPE_BUILTIN_EVSTWWOX:
13505 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwox, exp);
13506 case SPE_BUILTIN_EVSTDD:
13507 return spe_expand_stv_builtin (CODE_FOR_spe_evstdd, exp);
13508 case SPE_BUILTIN_EVSTDH:
13509 return spe_expand_stv_builtin (CODE_FOR_spe_evstdh, exp);
13510 case SPE_BUILTIN_EVSTDW:
13511 return spe_expand_stv_builtin (CODE_FOR_spe_evstdw, exp);
13512 case SPE_BUILTIN_EVSTWHE:
13513 return spe_expand_stv_builtin (CODE_FOR_spe_evstwhe, exp);
13514 case SPE_BUILTIN_EVSTWHO:
13515 return spe_expand_stv_builtin (CODE_FOR_spe_evstwho, exp);
13516 case SPE_BUILTIN_EVSTWWE:
13517 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwe, exp);
13518 case SPE_BUILTIN_EVSTWWO:
13519 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwo, exp);
13520 case SPE_BUILTIN_MFSPEFSCR:
13521 icode = CODE_FOR_spe_mfspefscr;
13522 tmode = insn_data[icode].operand[0].mode;
13524 if (target == 0
13525 || GET_MODE (target) != tmode
13526 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13527 target = gen_reg_rtx (tmode);
13529 pat = GEN_FCN (icode) (target);
13530 if (! pat)
13531 return 0;
13532 emit_insn (pat);
13533 return target;
13534 case SPE_BUILTIN_MTSPEFSCR:
13535 icode = CODE_FOR_spe_mtspefscr;
13536 arg0 = CALL_EXPR_ARG (exp, 0);
13537 op0 = expand_normal (arg0);
13538 mode0 = insn_data[icode].operand[0].mode;
13540 if (arg0 == error_mark_node)
13541 return const0_rtx;
13543 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
13544 op0 = copy_to_mode_reg (mode0, op0);
13546 pat = GEN_FCN (icode) (op0);
13547 if (pat)
13548 emit_insn (pat);
13549 return NULL_RTX;
13550 default:
13551 break;
13554 *expandedp = false;
13555 return NULL_RTX;
13558 static rtx
13559 paired_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
13561 rtx pat, scratch, tmp;
13562 tree form = CALL_EXPR_ARG (exp, 0);
13563 tree arg0 = CALL_EXPR_ARG (exp, 1);
13564 tree arg1 = CALL_EXPR_ARG (exp, 2);
13565 rtx op0 = expand_normal (arg0);
13566 rtx op1 = expand_normal (arg1);
13567 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
13568 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
13569 int form_int;
13570 enum rtx_code code;
13572 if (TREE_CODE (form) != INTEGER_CST)
13574 error ("argument 1 of __builtin_paired_predicate must be a constant");
13575 return const0_rtx;
13577 else
13578 form_int = TREE_INT_CST_LOW (form);
13580 gcc_assert (mode0 == mode1);
13582 if (arg0 == error_mark_node || arg1 == error_mark_node)
13583 return const0_rtx;
13585 if (target == 0
13586 || GET_MODE (target) != SImode
13587 || !(*insn_data[icode].operand[0].predicate) (target, SImode))
13588 target = gen_reg_rtx (SImode);
13589 if (!(*insn_data[icode].operand[1].predicate) (op0, mode0))
13590 op0 = copy_to_mode_reg (mode0, op0);
13591 if (!(*insn_data[icode].operand[2].predicate) (op1, mode1))
13592 op1 = copy_to_mode_reg (mode1, op1);
13594 scratch = gen_reg_rtx (CCFPmode);
13596 pat = GEN_FCN (icode) (scratch, op0, op1);
13597 if (!pat)
13598 return const0_rtx;
13600 emit_insn (pat);
13602 switch (form_int)
13604 /* LT bit. */
13605 case 0:
13606 code = LT;
13607 break;
13608 /* GT bit. */
13609 case 1:
13610 code = GT;
13611 break;
13612 /* EQ bit. */
13613 case 2:
13614 code = EQ;
13615 break;
13616 /* UN bit. */
13617 case 3:
13618 emit_insn (gen_move_from_CR_ov_bit (target, scratch));
13619 return target;
13620 default:
13621 error ("argument 1 of __builtin_paired_predicate is out of range");
13622 return const0_rtx;
13625 tmp = gen_rtx_fmt_ee (code, SImode, scratch, const0_rtx);
13626 emit_move_insn (target, tmp);
13627 return target;
13630 static rtx
13631 spe_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
13633 rtx pat, scratch, tmp;
13634 tree form = CALL_EXPR_ARG (exp, 0);
13635 tree arg0 = CALL_EXPR_ARG (exp, 1);
13636 tree arg1 = CALL_EXPR_ARG (exp, 2);
13637 rtx op0 = expand_normal (arg0);
13638 rtx op1 = expand_normal (arg1);
13639 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
13640 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
13641 int form_int;
13642 enum rtx_code code;
13644 if (TREE_CODE (form) != INTEGER_CST)
13646 error ("argument 1 of __builtin_spe_predicate must be a constant");
13647 return const0_rtx;
13649 else
13650 form_int = TREE_INT_CST_LOW (form);
13652 gcc_assert (mode0 == mode1);
13654 if (arg0 == error_mark_node || arg1 == error_mark_node)
13655 return const0_rtx;
13657 if (target == 0
13658 || GET_MODE (target) != SImode
13659 || ! (*insn_data[icode].operand[0].predicate) (target, SImode))
13660 target = gen_reg_rtx (SImode);
13662 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13663 op0 = copy_to_mode_reg (mode0, op0);
13664 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
13665 op1 = copy_to_mode_reg (mode1, op1);
13667 scratch = gen_reg_rtx (CCmode);
13669 pat = GEN_FCN (icode) (scratch, op0, op1);
13670 if (! pat)
13671 return const0_rtx;
13672 emit_insn (pat);
13674 /* There are 4 variants for each predicate: _any_, _all_, _upper_,
13675 _lower_. We use one compare, but look in different bits of the
13676 CR for each variant.
13678 There are 2 elements in each SPE simd type (upper/lower). The CR
13679 bits are set as follows:
13681 BIT0 | BIT 1 | BIT 2 | BIT 3
13682 U | L | (U | L) | (U & L)
13684 So, for an "all" relationship, BIT 3 would be set.
13685 For an "any" relationship, BIT 2 would be set. Etc.
13687 Following traditional nomenclature, these bits map to:
13689 BIT0 | BIT 1 | BIT 2 | BIT 3
13690 LT | GT | EQ | OV
13692 Later, we will generate rtl to look in the LT/EQ/EQ/OV bits.
13695 switch (form_int)
13697 /* All variant. OV bit. */
13698 case 0:
13699 /* We need to get to the OV bit, which is the ORDERED bit. We
13700 could generate (ordered:SI (reg:CC xx) (const_int 0)), but
13701 that's ugly and will make validate_condition_mode die.
13702 So let's just use another pattern. */
13703 emit_insn (gen_move_from_CR_ov_bit (target, scratch));
13704 return target;
13705 /* Any variant. EQ bit. */
13706 case 1:
13707 code = EQ;
13708 break;
13709 /* Upper variant. LT bit. */
13710 case 2:
13711 code = LT;
13712 break;
13713 /* Lower variant. GT bit. */
13714 case 3:
13715 code = GT;
13716 break;
13717 default:
13718 error ("argument 1 of __builtin_spe_predicate is out of range");
13719 return const0_rtx;
13722 tmp = gen_rtx_fmt_ee (code, SImode, scratch, const0_rtx);
13723 emit_move_insn (target, tmp);
13725 return target;
13728 /* The evsel builtins look like this:
13730 e = __builtin_spe_evsel_OP (a, b, c, d);
13732 and work like this:
13734 e[upper] = a[upper] *OP* b[upper] ? c[upper] : d[upper];
13735 e[lower] = a[lower] *OP* b[lower] ? c[lower] : d[lower];
13738 static rtx
13739 spe_expand_evsel_builtin (enum insn_code icode, tree exp, rtx target)
13741 rtx pat, scratch;
13742 tree arg0 = CALL_EXPR_ARG (exp, 0);
13743 tree arg1 = CALL_EXPR_ARG (exp, 1);
13744 tree arg2 = CALL_EXPR_ARG (exp, 2);
13745 tree arg3 = CALL_EXPR_ARG (exp, 3);
13746 rtx op0 = expand_normal (arg0);
13747 rtx op1 = expand_normal (arg1);
13748 rtx op2 = expand_normal (arg2);
13749 rtx op3 = expand_normal (arg3);
13750 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
13751 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
13753 gcc_assert (mode0 == mode1);
13755 if (arg0 == error_mark_node || arg1 == error_mark_node
13756 || arg2 == error_mark_node || arg3 == error_mark_node)
13757 return const0_rtx;
13759 if (target == 0
13760 || GET_MODE (target) != mode0
13761 || ! (*insn_data[icode].operand[0].predicate) (target, mode0))
13762 target = gen_reg_rtx (mode0);
13764 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13765 op0 = copy_to_mode_reg (mode0, op0);
13766 if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
13767 op1 = copy_to_mode_reg (mode0, op1);
13768 if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
13769 op2 = copy_to_mode_reg (mode0, op2);
13770 if (! (*insn_data[icode].operand[1].predicate) (op3, mode1))
13771 op3 = copy_to_mode_reg (mode0, op3);
13773 /* Generate the compare. */
13774 scratch = gen_reg_rtx (CCmode);
13775 pat = GEN_FCN (icode) (scratch, op0, op1);
13776 if (! pat)
13777 return const0_rtx;
13778 emit_insn (pat);
13780 if (mode0 == V2SImode)
13781 emit_insn (gen_spe_evsel (target, op2, op3, scratch));
13782 else
13783 emit_insn (gen_spe_evsel_fs (target, op2, op3, scratch));
13785 return target;
13788 /* Raise an error message for a builtin function that is called without the
13789 appropriate target options being set. */
13791 static void
13792 rs6000_invalid_builtin (enum rs6000_builtins fncode)
13794 size_t uns_fncode = (size_t)fncode;
13795 const char *name = rs6000_builtin_info[uns_fncode].name;
13796 HOST_WIDE_INT fnmask = rs6000_builtin_info[uns_fncode].mask;
13798 gcc_assert (name != NULL);
13799 if ((fnmask & RS6000_BTM_CELL) != 0)
13800 error ("Builtin function %s is only valid for the cell processor", name);
13801 else if ((fnmask & RS6000_BTM_VSX) != 0)
13802 error ("Builtin function %s requires the -mvsx option", name);
13803 else if ((fnmask & RS6000_BTM_HTM) != 0)
13804 error ("Builtin function %s requires the -mhtm option", name);
13805 else if ((fnmask & RS6000_BTM_ALTIVEC) != 0)
13806 error ("Builtin function %s requires the -maltivec option", name);
13807 else if ((fnmask & RS6000_BTM_PAIRED) != 0)
13808 error ("Builtin function %s requires the -mpaired option", name);
13809 else if ((fnmask & RS6000_BTM_SPE) != 0)
13810 error ("Builtin function %s requires the -mspe option", name);
13811 else if ((fnmask & (RS6000_BTM_DFP | RS6000_BTM_P8_VECTOR))
13812 == (RS6000_BTM_DFP | RS6000_BTM_P8_VECTOR))
13813 error ("Builtin function %s requires the -mhard-dfp and"
13814 " -mpower8-vector options", name);
13815 else if ((fnmask & RS6000_BTM_DFP) != 0)
13816 error ("Builtin function %s requires the -mhard-dfp option", name);
13817 else if ((fnmask & RS6000_BTM_P8_VECTOR) != 0)
13818 error ("Builtin function %s requires the -mpower8-vector option", name);
13819 else if ((fnmask & (RS6000_BTM_HARD_FLOAT | RS6000_BTM_LDBL128))
13820 == (RS6000_BTM_HARD_FLOAT | RS6000_BTM_LDBL128))
13821 error ("Builtin function %s requires the -mhard-float and"
13822 " -mlong-double-128 options", name);
13823 else if ((fnmask & RS6000_BTM_HARD_FLOAT) != 0)
13824 error ("Builtin function %s requires the -mhard-float option", name);
13825 else
13826 error ("Builtin function %s is not supported with the current options",
13827 name);
13830 /* Expand an expression EXP that calls a built-in function,
13831 with result going to TARGET if that's convenient
13832 (and in mode MODE if that's convenient).
13833 SUBTARGET may be used as the target for computing one of EXP's operands.
13834 IGNORE is nonzero if the value is to be ignored. */
13836 static rtx
13837 rs6000_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
13838 enum machine_mode mode ATTRIBUTE_UNUSED,
13839 int ignore ATTRIBUTE_UNUSED)
13841 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
13842 enum rs6000_builtins fcode
13843 = (enum rs6000_builtins)DECL_FUNCTION_CODE (fndecl);
13844 size_t uns_fcode = (size_t)fcode;
13845 const struct builtin_description *d;
13846 size_t i;
13847 rtx ret;
13848 bool success;
13849 HOST_WIDE_INT mask = rs6000_builtin_info[uns_fcode].mask;
13850 bool func_valid_p = ((rs6000_builtin_mask & mask) == mask);
13852 if (TARGET_DEBUG_BUILTIN)
13854 enum insn_code icode = rs6000_builtin_info[uns_fcode].icode;
13855 const char *name1 = rs6000_builtin_info[uns_fcode].name;
13856 const char *name2 = ((icode != CODE_FOR_nothing)
13857 ? get_insn_name ((int)icode)
13858 : "nothing");
13859 const char *name3;
13861 switch (rs6000_builtin_info[uns_fcode].attr & RS6000_BTC_TYPE_MASK)
13863 default: name3 = "unknown"; break;
13864 case RS6000_BTC_SPECIAL: name3 = "special"; break;
13865 case RS6000_BTC_UNARY: name3 = "unary"; break;
13866 case RS6000_BTC_BINARY: name3 = "binary"; break;
13867 case RS6000_BTC_TERNARY: name3 = "ternary"; break;
13868 case RS6000_BTC_PREDICATE: name3 = "predicate"; break;
13869 case RS6000_BTC_ABS: name3 = "abs"; break;
13870 case RS6000_BTC_EVSEL: name3 = "evsel"; break;
13871 case RS6000_BTC_DST: name3 = "dst"; break;
13875 fprintf (stderr,
13876 "rs6000_expand_builtin, %s (%d), insn = %s (%d), type=%s%s\n",
13877 (name1) ? name1 : "---", fcode,
13878 (name2) ? name2 : "---", (int)icode,
13879 name3,
13880 func_valid_p ? "" : ", not valid");
13883 if (!func_valid_p)
13885 rs6000_invalid_builtin (fcode);
13887 /* Given it is invalid, just generate a normal call. */
13888 return expand_call (exp, target, ignore);
13891 switch (fcode)
13893 case RS6000_BUILTIN_RECIP:
13894 return rs6000_expand_binop_builtin (CODE_FOR_recipdf3, exp, target);
13896 case RS6000_BUILTIN_RECIPF:
13897 return rs6000_expand_binop_builtin (CODE_FOR_recipsf3, exp, target);
13899 case RS6000_BUILTIN_RSQRTF:
13900 return rs6000_expand_unop_builtin (CODE_FOR_rsqrtsf2, exp, target);
13902 case RS6000_BUILTIN_RSQRT:
13903 return rs6000_expand_unop_builtin (CODE_FOR_rsqrtdf2, exp, target);
13905 case POWER7_BUILTIN_BPERMD:
13906 return rs6000_expand_binop_builtin (((TARGET_64BIT)
13907 ? CODE_FOR_bpermd_di
13908 : CODE_FOR_bpermd_si), exp, target);
13910 case RS6000_BUILTIN_GET_TB:
13911 return rs6000_expand_zeroop_builtin (CODE_FOR_rs6000_get_timebase,
13912 target);
13914 case RS6000_BUILTIN_MFTB:
13915 return rs6000_expand_zeroop_builtin (((TARGET_64BIT)
13916 ? CODE_FOR_rs6000_mftb_di
13917 : CODE_FOR_rs6000_mftb_si),
13918 target);
13920 case RS6000_BUILTIN_MFFS:
13921 return rs6000_expand_zeroop_builtin (CODE_FOR_rs6000_mffs, target);
13923 case RS6000_BUILTIN_MTFSF:
13924 return rs6000_expand_mtfsf_builtin (CODE_FOR_rs6000_mtfsf, exp);
13926 case ALTIVEC_BUILTIN_MASK_FOR_LOAD:
13927 case ALTIVEC_BUILTIN_MASK_FOR_STORE:
13929 int icode = (BYTES_BIG_ENDIAN ? (int) CODE_FOR_altivec_lvsr_direct
13930 : (int) CODE_FOR_altivec_lvsl_direct);
13931 enum machine_mode tmode = insn_data[icode].operand[0].mode;
13932 enum machine_mode mode = insn_data[icode].operand[1].mode;
13933 tree arg;
13934 rtx op, addr, pat;
13936 gcc_assert (TARGET_ALTIVEC);
13938 arg = CALL_EXPR_ARG (exp, 0);
13939 gcc_assert (POINTER_TYPE_P (TREE_TYPE (arg)));
13940 op = expand_expr (arg, NULL_RTX, Pmode, EXPAND_NORMAL);
13941 addr = memory_address (mode, op);
13942 if (fcode == ALTIVEC_BUILTIN_MASK_FOR_STORE)
13943 op = addr;
13944 else
13946 /* For the load case need to negate the address. */
13947 op = gen_reg_rtx (GET_MODE (addr));
13948 emit_insn (gen_rtx_SET (VOIDmode, op,
13949 gen_rtx_NEG (GET_MODE (addr), addr)));
13951 op = gen_rtx_MEM (mode, op);
13953 if (target == 0
13954 || GET_MODE (target) != tmode
13955 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13956 target = gen_reg_rtx (tmode);
13958 pat = GEN_FCN (icode) (target, op);
13959 if (!pat)
13960 return 0;
13961 emit_insn (pat);
13963 return target;
13966 case ALTIVEC_BUILTIN_VCFUX:
13967 case ALTIVEC_BUILTIN_VCFSX:
13968 case ALTIVEC_BUILTIN_VCTUXS:
13969 case ALTIVEC_BUILTIN_VCTSXS:
13970 /* FIXME: There's got to be a nicer way to handle this case than
13971 constructing a new CALL_EXPR. */
13972 if (call_expr_nargs (exp) == 1)
13974 exp = build_call_nary (TREE_TYPE (exp), CALL_EXPR_FN (exp),
13975 2, CALL_EXPR_ARG (exp, 0), integer_zero_node);
13977 break;
13979 default:
13980 break;
13983 if (TARGET_ALTIVEC)
13985 ret = altivec_expand_builtin (exp, target, &success);
13987 if (success)
13988 return ret;
13990 if (TARGET_SPE)
13992 ret = spe_expand_builtin (exp, target, &success);
13994 if (success)
13995 return ret;
13997 if (TARGET_PAIRED_FLOAT)
13999 ret = paired_expand_builtin (exp, target, &success);
14001 if (success)
14002 return ret;
14004 if (TARGET_HTM)
14006 ret = htm_expand_builtin (exp, target, &success);
14008 if (success)
14009 return ret;
14012 unsigned attr = rs6000_builtin_info[uns_fcode].attr & RS6000_BTC_TYPE_MASK;
14013 gcc_assert (attr == RS6000_BTC_UNARY
14014 || attr == RS6000_BTC_BINARY
14015 || attr == RS6000_BTC_TERNARY);
14017 /* Handle simple unary operations. */
14018 d = bdesc_1arg;
14019 for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
14020 if (d->code == fcode)
14021 return rs6000_expand_unop_builtin (d->icode, exp, target);
14023 /* Handle simple binary operations. */
14024 d = bdesc_2arg;
14025 for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
14026 if (d->code == fcode)
14027 return rs6000_expand_binop_builtin (d->icode, exp, target);
14029 /* Handle simple ternary operations. */
14030 d = bdesc_3arg;
14031 for (i = 0; i < ARRAY_SIZE (bdesc_3arg); i++, d++)
14032 if (d->code == fcode)
14033 return rs6000_expand_ternop_builtin (d->icode, exp, target);
14035 gcc_unreachable ();
14038 static void
14039 rs6000_init_builtins (void)
14041 tree tdecl;
14042 tree ftype;
14043 enum machine_mode mode;
14045 if (TARGET_DEBUG_BUILTIN)
14046 fprintf (stderr, "rs6000_init_builtins%s%s%s%s\n",
14047 (TARGET_PAIRED_FLOAT) ? ", paired" : "",
14048 (TARGET_SPE) ? ", spe" : "",
14049 (TARGET_ALTIVEC) ? ", altivec" : "",
14050 (TARGET_VSX) ? ", vsx" : "");
14052 V2SI_type_node = build_vector_type (intSI_type_node, 2);
14053 V2SF_type_node = build_vector_type (float_type_node, 2);
14054 V2DI_type_node = build_vector_type (intDI_type_node, 2);
14055 V2DF_type_node = build_vector_type (double_type_node, 2);
14056 V4HI_type_node = build_vector_type (intHI_type_node, 4);
14057 V4SI_type_node = build_vector_type (intSI_type_node, 4);
14058 V4SF_type_node = build_vector_type (float_type_node, 4);
14059 V8HI_type_node = build_vector_type (intHI_type_node, 8);
14060 V16QI_type_node = build_vector_type (intQI_type_node, 16);
14062 unsigned_V16QI_type_node = build_vector_type (unsigned_intQI_type_node, 16);
14063 unsigned_V8HI_type_node = build_vector_type (unsigned_intHI_type_node, 8);
14064 unsigned_V4SI_type_node = build_vector_type (unsigned_intSI_type_node, 4);
14065 unsigned_V2DI_type_node = build_vector_type (unsigned_intDI_type_node, 2);
14067 opaque_V2SF_type_node = build_opaque_vector_type (float_type_node, 2);
14068 opaque_V2SI_type_node = build_opaque_vector_type (intSI_type_node, 2);
14069 opaque_p_V2SI_type_node = build_pointer_type (opaque_V2SI_type_node);
14070 opaque_V4SI_type_node = build_opaque_vector_type (intSI_type_node, 4);
14072 /* We use V1TI mode as a special container to hold __int128_t items that
14073 must live in VSX registers. */
14074 if (intTI_type_node)
14076 V1TI_type_node = build_vector_type (intTI_type_node, 1);
14077 unsigned_V1TI_type_node = build_vector_type (unsigned_intTI_type_node, 1);
14080 /* The 'vector bool ...' types must be kept distinct from 'vector unsigned ...'
14081 types, especially in C++ land. Similarly, 'vector pixel' is distinct from
14082 'vector unsigned short'. */
14084 bool_char_type_node = build_distinct_type_copy (unsigned_intQI_type_node);
14085 bool_short_type_node = build_distinct_type_copy (unsigned_intHI_type_node);
14086 bool_int_type_node = build_distinct_type_copy (unsigned_intSI_type_node);
14087 bool_long_type_node = build_distinct_type_copy (unsigned_intDI_type_node);
14088 pixel_type_node = build_distinct_type_copy (unsigned_intHI_type_node);
14090 long_integer_type_internal_node = long_integer_type_node;
14091 long_unsigned_type_internal_node = long_unsigned_type_node;
14092 long_long_integer_type_internal_node = long_long_integer_type_node;
14093 long_long_unsigned_type_internal_node = long_long_unsigned_type_node;
14094 intQI_type_internal_node = intQI_type_node;
14095 uintQI_type_internal_node = unsigned_intQI_type_node;
14096 intHI_type_internal_node = intHI_type_node;
14097 uintHI_type_internal_node = unsigned_intHI_type_node;
14098 intSI_type_internal_node = intSI_type_node;
14099 uintSI_type_internal_node = unsigned_intSI_type_node;
14100 intDI_type_internal_node = intDI_type_node;
14101 uintDI_type_internal_node = unsigned_intDI_type_node;
14102 intTI_type_internal_node = intTI_type_node;
14103 uintTI_type_internal_node = unsigned_intTI_type_node;
14104 float_type_internal_node = float_type_node;
14105 double_type_internal_node = double_type_node;
14106 long_double_type_internal_node = long_double_type_node;
14107 dfloat64_type_internal_node = dfloat64_type_node;
14108 dfloat128_type_internal_node = dfloat128_type_node;
14109 void_type_internal_node = void_type_node;
14111 /* Initialize the modes for builtin_function_type, mapping a machine mode to
14112 tree type node. */
14113 builtin_mode_to_type[QImode][0] = integer_type_node;
14114 builtin_mode_to_type[HImode][0] = integer_type_node;
14115 builtin_mode_to_type[SImode][0] = intSI_type_node;
14116 builtin_mode_to_type[SImode][1] = unsigned_intSI_type_node;
14117 builtin_mode_to_type[DImode][0] = intDI_type_node;
14118 builtin_mode_to_type[DImode][1] = unsigned_intDI_type_node;
14119 builtin_mode_to_type[TImode][0] = intTI_type_node;
14120 builtin_mode_to_type[TImode][1] = unsigned_intTI_type_node;
14121 builtin_mode_to_type[SFmode][0] = float_type_node;
14122 builtin_mode_to_type[DFmode][0] = double_type_node;
14123 builtin_mode_to_type[TFmode][0] = long_double_type_node;
14124 builtin_mode_to_type[DDmode][0] = dfloat64_type_node;
14125 builtin_mode_to_type[TDmode][0] = dfloat128_type_node;
14126 builtin_mode_to_type[V1TImode][0] = V1TI_type_node;
14127 builtin_mode_to_type[V1TImode][1] = unsigned_V1TI_type_node;
14128 builtin_mode_to_type[V2SImode][0] = V2SI_type_node;
14129 builtin_mode_to_type[V2SFmode][0] = V2SF_type_node;
14130 builtin_mode_to_type[V2DImode][0] = V2DI_type_node;
14131 builtin_mode_to_type[V2DImode][1] = unsigned_V2DI_type_node;
14132 builtin_mode_to_type[V2DFmode][0] = V2DF_type_node;
14133 builtin_mode_to_type[V4HImode][0] = V4HI_type_node;
14134 builtin_mode_to_type[V4SImode][0] = V4SI_type_node;
14135 builtin_mode_to_type[V4SImode][1] = unsigned_V4SI_type_node;
14136 builtin_mode_to_type[V4SFmode][0] = V4SF_type_node;
14137 builtin_mode_to_type[V8HImode][0] = V8HI_type_node;
14138 builtin_mode_to_type[V8HImode][1] = unsigned_V8HI_type_node;
14139 builtin_mode_to_type[V16QImode][0] = V16QI_type_node;
14140 builtin_mode_to_type[V16QImode][1] = unsigned_V16QI_type_node;
14142 tdecl = add_builtin_type ("__bool char", bool_char_type_node);
14143 TYPE_NAME (bool_char_type_node) = tdecl;
14145 tdecl = add_builtin_type ("__bool short", bool_short_type_node);
14146 TYPE_NAME (bool_short_type_node) = tdecl;
14148 tdecl = add_builtin_type ("__bool int", bool_int_type_node);
14149 TYPE_NAME (bool_int_type_node) = tdecl;
14151 tdecl = add_builtin_type ("__pixel", pixel_type_node);
14152 TYPE_NAME (pixel_type_node) = tdecl;
14154 bool_V16QI_type_node = build_vector_type (bool_char_type_node, 16);
14155 bool_V8HI_type_node = build_vector_type (bool_short_type_node, 8);
14156 bool_V4SI_type_node = build_vector_type (bool_int_type_node, 4);
14157 bool_V2DI_type_node = build_vector_type (bool_long_type_node, 2);
14158 pixel_V8HI_type_node = build_vector_type (pixel_type_node, 8);
14160 tdecl = add_builtin_type ("__vector unsigned char", unsigned_V16QI_type_node);
14161 TYPE_NAME (unsigned_V16QI_type_node) = tdecl;
14163 tdecl = add_builtin_type ("__vector signed char", V16QI_type_node);
14164 TYPE_NAME (V16QI_type_node) = tdecl;
14166 tdecl = add_builtin_type ("__vector __bool char", bool_V16QI_type_node);
14167 TYPE_NAME ( bool_V16QI_type_node) = tdecl;
14169 tdecl = add_builtin_type ("__vector unsigned short", unsigned_V8HI_type_node);
14170 TYPE_NAME (unsigned_V8HI_type_node) = tdecl;
14172 tdecl = add_builtin_type ("__vector signed short", V8HI_type_node);
14173 TYPE_NAME (V8HI_type_node) = tdecl;
14175 tdecl = add_builtin_type ("__vector __bool short", bool_V8HI_type_node);
14176 TYPE_NAME (bool_V8HI_type_node) = tdecl;
14178 tdecl = add_builtin_type ("__vector unsigned int", unsigned_V4SI_type_node);
14179 TYPE_NAME (unsigned_V4SI_type_node) = tdecl;
14181 tdecl = add_builtin_type ("__vector signed int", V4SI_type_node);
14182 TYPE_NAME (V4SI_type_node) = tdecl;
14184 tdecl = add_builtin_type ("__vector __bool int", bool_V4SI_type_node);
14185 TYPE_NAME (bool_V4SI_type_node) = tdecl;
14187 tdecl = add_builtin_type ("__vector float", V4SF_type_node);
14188 TYPE_NAME (V4SF_type_node) = tdecl;
14190 tdecl = add_builtin_type ("__vector __pixel", pixel_V8HI_type_node);
14191 TYPE_NAME (pixel_V8HI_type_node) = tdecl;
14193 tdecl = add_builtin_type ("__vector double", V2DF_type_node);
14194 TYPE_NAME (V2DF_type_node) = tdecl;
14196 if (TARGET_POWERPC64)
14198 tdecl = add_builtin_type ("__vector long", V2DI_type_node);
14199 TYPE_NAME (V2DI_type_node) = tdecl;
14201 tdecl = add_builtin_type ("__vector unsigned long",
14202 unsigned_V2DI_type_node);
14203 TYPE_NAME (unsigned_V2DI_type_node) = tdecl;
14205 tdecl = add_builtin_type ("__vector __bool long", bool_V2DI_type_node);
14206 TYPE_NAME (bool_V2DI_type_node) = tdecl;
14208 else
14210 tdecl = add_builtin_type ("__vector long long", V2DI_type_node);
14211 TYPE_NAME (V2DI_type_node) = tdecl;
14213 tdecl = add_builtin_type ("__vector unsigned long long",
14214 unsigned_V2DI_type_node);
14215 TYPE_NAME (unsigned_V2DI_type_node) = tdecl;
14217 tdecl = add_builtin_type ("__vector __bool long long",
14218 bool_V2DI_type_node);
14219 TYPE_NAME (bool_V2DI_type_node) = tdecl;
14222 if (V1TI_type_node)
14224 tdecl = add_builtin_type ("__vector __int128", V1TI_type_node);
14225 TYPE_NAME (V1TI_type_node) = tdecl;
14227 tdecl = add_builtin_type ("__vector unsigned __int128",
14228 unsigned_V1TI_type_node);
14229 TYPE_NAME (unsigned_V1TI_type_node) = tdecl;
14232 /* Paired and SPE builtins are only available if you build a compiler with
14233 the appropriate options, so only create those builtins with the
14234 appropriate compiler option. Create Altivec and VSX builtins on machines
14235 with at least the general purpose extensions (970 and newer) to allow the
14236 use of the target attribute. */
14237 if (TARGET_PAIRED_FLOAT)
14238 paired_init_builtins ();
14239 if (TARGET_SPE)
14240 spe_init_builtins ();
14241 if (TARGET_EXTRA_BUILTINS)
14242 altivec_init_builtins ();
14243 if (TARGET_HTM)
14244 htm_init_builtins ();
14246 if (TARGET_EXTRA_BUILTINS || TARGET_SPE || TARGET_PAIRED_FLOAT)
14247 rs6000_common_init_builtins ();
14249 ftype = builtin_function_type (DFmode, DFmode, DFmode, VOIDmode,
14250 RS6000_BUILTIN_RECIP, "__builtin_recipdiv");
14251 def_builtin ("__builtin_recipdiv", ftype, RS6000_BUILTIN_RECIP);
14253 ftype = builtin_function_type (SFmode, SFmode, SFmode, VOIDmode,
14254 RS6000_BUILTIN_RECIPF, "__builtin_recipdivf");
14255 def_builtin ("__builtin_recipdivf", ftype, RS6000_BUILTIN_RECIPF);
14257 ftype = builtin_function_type (DFmode, DFmode, VOIDmode, VOIDmode,
14258 RS6000_BUILTIN_RSQRT, "__builtin_rsqrt");
14259 def_builtin ("__builtin_rsqrt", ftype, RS6000_BUILTIN_RSQRT);
14261 ftype = builtin_function_type (SFmode, SFmode, VOIDmode, VOIDmode,
14262 RS6000_BUILTIN_RSQRTF, "__builtin_rsqrtf");
14263 def_builtin ("__builtin_rsqrtf", ftype, RS6000_BUILTIN_RSQRTF);
14265 mode = (TARGET_64BIT) ? DImode : SImode;
14266 ftype = builtin_function_type (mode, mode, mode, VOIDmode,
14267 POWER7_BUILTIN_BPERMD, "__builtin_bpermd");
14268 def_builtin ("__builtin_bpermd", ftype, POWER7_BUILTIN_BPERMD);
14270 ftype = build_function_type_list (unsigned_intDI_type_node,
14271 NULL_TREE);
14272 def_builtin ("__builtin_ppc_get_timebase", ftype, RS6000_BUILTIN_GET_TB);
14274 if (TARGET_64BIT)
14275 ftype = build_function_type_list (unsigned_intDI_type_node,
14276 NULL_TREE);
14277 else
14278 ftype = build_function_type_list (unsigned_intSI_type_node,
14279 NULL_TREE);
14280 def_builtin ("__builtin_ppc_mftb", ftype, RS6000_BUILTIN_MFTB);
14282 ftype = build_function_type_list (double_type_node, NULL_TREE);
14283 def_builtin ("__builtin_mffs", ftype, RS6000_BUILTIN_MFFS);
14285 ftype = build_function_type_list (void_type_node,
14286 intSI_type_node, double_type_node,
14287 NULL_TREE);
14288 def_builtin ("__builtin_mtfsf", ftype, RS6000_BUILTIN_MTFSF);
14290 #if TARGET_XCOFF
14291 /* AIX libm provides clog as __clog. */
14292 if ((tdecl = builtin_decl_explicit (BUILT_IN_CLOG)) != NULL_TREE)
14293 set_user_assembler_name (tdecl, "__clog");
14294 #endif
14296 #ifdef SUBTARGET_INIT_BUILTINS
14297 SUBTARGET_INIT_BUILTINS;
14298 #endif
14301 /* Returns the rs6000 builtin decl for CODE. */
14303 static tree
14304 rs6000_builtin_decl (unsigned code, bool initialize_p ATTRIBUTE_UNUSED)
14306 HOST_WIDE_INT fnmask;
14308 if (code >= RS6000_BUILTIN_COUNT)
14309 return error_mark_node;
14311 fnmask = rs6000_builtin_info[code].mask;
14312 if ((fnmask & rs6000_builtin_mask) != fnmask)
14314 rs6000_invalid_builtin ((enum rs6000_builtins)code);
14315 return error_mark_node;
14318 return rs6000_builtin_decls[code];
14321 static void
14322 spe_init_builtins (void)
14324 tree puint_type_node = build_pointer_type (unsigned_type_node);
14325 tree pushort_type_node = build_pointer_type (short_unsigned_type_node);
14326 const struct builtin_description *d;
14327 size_t i;
14329 tree v2si_ftype_4_v2si
14330 = build_function_type_list (opaque_V2SI_type_node,
14331 opaque_V2SI_type_node,
14332 opaque_V2SI_type_node,
14333 opaque_V2SI_type_node,
14334 opaque_V2SI_type_node,
14335 NULL_TREE);
14337 tree v2sf_ftype_4_v2sf
14338 = build_function_type_list (opaque_V2SF_type_node,
14339 opaque_V2SF_type_node,
14340 opaque_V2SF_type_node,
14341 opaque_V2SF_type_node,
14342 opaque_V2SF_type_node,
14343 NULL_TREE);
14345 tree int_ftype_int_v2si_v2si
14346 = build_function_type_list (integer_type_node,
14347 integer_type_node,
14348 opaque_V2SI_type_node,
14349 opaque_V2SI_type_node,
14350 NULL_TREE);
14352 tree int_ftype_int_v2sf_v2sf
14353 = build_function_type_list (integer_type_node,
14354 integer_type_node,
14355 opaque_V2SF_type_node,
14356 opaque_V2SF_type_node,
14357 NULL_TREE);
14359 tree void_ftype_v2si_puint_int
14360 = build_function_type_list (void_type_node,
14361 opaque_V2SI_type_node,
14362 puint_type_node,
14363 integer_type_node,
14364 NULL_TREE);
14366 tree void_ftype_v2si_puint_char
14367 = build_function_type_list (void_type_node,
14368 opaque_V2SI_type_node,
14369 puint_type_node,
14370 char_type_node,
14371 NULL_TREE);
14373 tree void_ftype_v2si_pv2si_int
14374 = build_function_type_list (void_type_node,
14375 opaque_V2SI_type_node,
14376 opaque_p_V2SI_type_node,
14377 integer_type_node,
14378 NULL_TREE);
14380 tree void_ftype_v2si_pv2si_char
14381 = build_function_type_list (void_type_node,
14382 opaque_V2SI_type_node,
14383 opaque_p_V2SI_type_node,
14384 char_type_node,
14385 NULL_TREE);
14387 tree void_ftype_int
14388 = build_function_type_list (void_type_node, integer_type_node, NULL_TREE);
14390 tree int_ftype_void
14391 = build_function_type_list (integer_type_node, NULL_TREE);
14393 tree v2si_ftype_pv2si_int
14394 = build_function_type_list (opaque_V2SI_type_node,
14395 opaque_p_V2SI_type_node,
14396 integer_type_node,
14397 NULL_TREE);
14399 tree v2si_ftype_puint_int
14400 = build_function_type_list (opaque_V2SI_type_node,
14401 puint_type_node,
14402 integer_type_node,
14403 NULL_TREE);
14405 tree v2si_ftype_pushort_int
14406 = build_function_type_list (opaque_V2SI_type_node,
14407 pushort_type_node,
14408 integer_type_node,
14409 NULL_TREE);
14411 tree v2si_ftype_signed_char
14412 = build_function_type_list (opaque_V2SI_type_node,
14413 signed_char_type_node,
14414 NULL_TREE);
14416 add_builtin_type ("__ev64_opaque__", opaque_V2SI_type_node);
14418 /* Initialize irregular SPE builtins. */
14420 def_builtin ("__builtin_spe_mtspefscr", void_ftype_int, SPE_BUILTIN_MTSPEFSCR);
14421 def_builtin ("__builtin_spe_mfspefscr", int_ftype_void, SPE_BUILTIN_MFSPEFSCR);
14422 def_builtin ("__builtin_spe_evstddx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDDX);
14423 def_builtin ("__builtin_spe_evstdhx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDHX);
14424 def_builtin ("__builtin_spe_evstdwx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDWX);
14425 def_builtin ("__builtin_spe_evstwhex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHEX);
14426 def_builtin ("__builtin_spe_evstwhox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHOX);
14427 def_builtin ("__builtin_spe_evstwwex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWEX);
14428 def_builtin ("__builtin_spe_evstwwox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWOX);
14429 def_builtin ("__builtin_spe_evstdd", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDD);
14430 def_builtin ("__builtin_spe_evstdh", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDH);
14431 def_builtin ("__builtin_spe_evstdw", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDW);
14432 def_builtin ("__builtin_spe_evstwhe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHE);
14433 def_builtin ("__builtin_spe_evstwho", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHO);
14434 def_builtin ("__builtin_spe_evstwwe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWE);
14435 def_builtin ("__builtin_spe_evstwwo", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWO);
14436 def_builtin ("__builtin_spe_evsplatfi", v2si_ftype_signed_char, SPE_BUILTIN_EVSPLATFI);
14437 def_builtin ("__builtin_spe_evsplati", v2si_ftype_signed_char, SPE_BUILTIN_EVSPLATI);
14439 /* Loads. */
14440 def_builtin ("__builtin_spe_evlddx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDDX);
14441 def_builtin ("__builtin_spe_evldwx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDWX);
14442 def_builtin ("__builtin_spe_evldhx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDHX);
14443 def_builtin ("__builtin_spe_evlwhex", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHEX);
14444 def_builtin ("__builtin_spe_evlwhoux", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOUX);
14445 def_builtin ("__builtin_spe_evlwhosx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOSX);
14446 def_builtin ("__builtin_spe_evlwwsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLATX);
14447 def_builtin ("__builtin_spe_evlwhsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLATX);
14448 def_builtin ("__builtin_spe_evlhhesplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLATX);
14449 def_builtin ("__builtin_spe_evlhhousplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLATX);
14450 def_builtin ("__builtin_spe_evlhhossplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLATX);
14451 def_builtin ("__builtin_spe_evldd", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDD);
14452 def_builtin ("__builtin_spe_evldw", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDW);
14453 def_builtin ("__builtin_spe_evldh", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDH);
14454 def_builtin ("__builtin_spe_evlhhesplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLAT);
14455 def_builtin ("__builtin_spe_evlhhossplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLAT);
14456 def_builtin ("__builtin_spe_evlhhousplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLAT);
14457 def_builtin ("__builtin_spe_evlwhe", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHE);
14458 def_builtin ("__builtin_spe_evlwhos", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOS);
14459 def_builtin ("__builtin_spe_evlwhou", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOU);
14460 def_builtin ("__builtin_spe_evlwhsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLAT);
14461 def_builtin ("__builtin_spe_evlwwsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLAT);
14463 /* Predicates. */
14464 d = bdesc_spe_predicates;
14465 for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, d++)
14467 tree type;
14469 switch (insn_data[d->icode].operand[1].mode)
14471 case V2SImode:
14472 type = int_ftype_int_v2si_v2si;
14473 break;
14474 case V2SFmode:
14475 type = int_ftype_int_v2sf_v2sf;
14476 break;
14477 default:
14478 gcc_unreachable ();
14481 def_builtin (d->name, type, d->code);
14484 /* Evsel predicates. */
14485 d = bdesc_spe_evsel;
14486 for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, d++)
14488 tree type;
14490 switch (insn_data[d->icode].operand[1].mode)
14492 case V2SImode:
14493 type = v2si_ftype_4_v2si;
14494 break;
14495 case V2SFmode:
14496 type = v2sf_ftype_4_v2sf;
14497 break;
14498 default:
14499 gcc_unreachable ();
14502 def_builtin (d->name, type, d->code);
14506 static void
14507 paired_init_builtins (void)
14509 const struct builtin_description *d;
14510 size_t i;
14512 tree int_ftype_int_v2sf_v2sf
14513 = build_function_type_list (integer_type_node,
14514 integer_type_node,
14515 V2SF_type_node,
14516 V2SF_type_node,
14517 NULL_TREE);
14518 tree pcfloat_type_node =
14519 build_pointer_type (build_qualified_type
14520 (float_type_node, TYPE_QUAL_CONST));
14522 tree v2sf_ftype_long_pcfloat = build_function_type_list (V2SF_type_node,
14523 long_integer_type_node,
14524 pcfloat_type_node,
14525 NULL_TREE);
14526 tree void_ftype_v2sf_long_pcfloat =
14527 build_function_type_list (void_type_node,
14528 V2SF_type_node,
14529 long_integer_type_node,
14530 pcfloat_type_node,
14531 NULL_TREE);
14534 def_builtin ("__builtin_paired_lx", v2sf_ftype_long_pcfloat,
14535 PAIRED_BUILTIN_LX);
14538 def_builtin ("__builtin_paired_stx", void_ftype_v2sf_long_pcfloat,
14539 PAIRED_BUILTIN_STX);
14541 /* Predicates. */
14542 d = bdesc_paired_preds;
14543 for (i = 0; i < ARRAY_SIZE (bdesc_paired_preds); ++i, d++)
14545 tree type;
14547 if (TARGET_DEBUG_BUILTIN)
14548 fprintf (stderr, "paired pred #%d, insn = %s [%d], mode = %s\n",
14549 (int)i, get_insn_name (d->icode), (int)d->icode,
14550 GET_MODE_NAME (insn_data[d->icode].operand[1].mode));
14552 switch (insn_data[d->icode].operand[1].mode)
14554 case V2SFmode:
14555 type = int_ftype_int_v2sf_v2sf;
14556 break;
14557 default:
14558 gcc_unreachable ();
14561 def_builtin (d->name, type, d->code);
14565 static void
14566 altivec_init_builtins (void)
14568 const struct builtin_description *d;
14569 size_t i;
14570 tree ftype;
14571 tree decl;
14573 tree pvoid_type_node = build_pointer_type (void_type_node);
14575 tree pcvoid_type_node
14576 = build_pointer_type (build_qualified_type (void_type_node,
14577 TYPE_QUAL_CONST));
14579 tree int_ftype_opaque
14580 = build_function_type_list (integer_type_node,
14581 opaque_V4SI_type_node, NULL_TREE);
14582 tree opaque_ftype_opaque
14583 = build_function_type_list (integer_type_node, NULL_TREE);
14584 tree opaque_ftype_opaque_int
14585 = build_function_type_list (opaque_V4SI_type_node,
14586 opaque_V4SI_type_node, integer_type_node, NULL_TREE);
14587 tree opaque_ftype_opaque_opaque_int
14588 = build_function_type_list (opaque_V4SI_type_node,
14589 opaque_V4SI_type_node, opaque_V4SI_type_node,
14590 integer_type_node, NULL_TREE);
14591 tree int_ftype_int_opaque_opaque
14592 = build_function_type_list (integer_type_node,
14593 integer_type_node, opaque_V4SI_type_node,
14594 opaque_V4SI_type_node, NULL_TREE);
14595 tree int_ftype_int_v4si_v4si
14596 = build_function_type_list (integer_type_node,
14597 integer_type_node, V4SI_type_node,
14598 V4SI_type_node, NULL_TREE);
14599 tree int_ftype_int_v2di_v2di
14600 = build_function_type_list (integer_type_node,
14601 integer_type_node, V2DI_type_node,
14602 V2DI_type_node, NULL_TREE);
14603 tree void_ftype_v4si
14604 = build_function_type_list (void_type_node, V4SI_type_node, NULL_TREE);
14605 tree v8hi_ftype_void
14606 = build_function_type_list (V8HI_type_node, NULL_TREE);
14607 tree void_ftype_void
14608 = build_function_type_list (void_type_node, NULL_TREE);
14609 tree void_ftype_int
14610 = build_function_type_list (void_type_node, integer_type_node, NULL_TREE);
14612 tree opaque_ftype_long_pcvoid
14613 = build_function_type_list (opaque_V4SI_type_node,
14614 long_integer_type_node, pcvoid_type_node,
14615 NULL_TREE);
14616 tree v16qi_ftype_long_pcvoid
14617 = build_function_type_list (V16QI_type_node,
14618 long_integer_type_node, pcvoid_type_node,
14619 NULL_TREE);
14620 tree v8hi_ftype_long_pcvoid
14621 = build_function_type_list (V8HI_type_node,
14622 long_integer_type_node, pcvoid_type_node,
14623 NULL_TREE);
14624 tree v4si_ftype_long_pcvoid
14625 = build_function_type_list (V4SI_type_node,
14626 long_integer_type_node, pcvoid_type_node,
14627 NULL_TREE);
14628 tree v4sf_ftype_long_pcvoid
14629 = build_function_type_list (V4SF_type_node,
14630 long_integer_type_node, pcvoid_type_node,
14631 NULL_TREE);
14632 tree v2df_ftype_long_pcvoid
14633 = build_function_type_list (V2DF_type_node,
14634 long_integer_type_node, pcvoid_type_node,
14635 NULL_TREE);
14636 tree v2di_ftype_long_pcvoid
14637 = build_function_type_list (V2DI_type_node,
14638 long_integer_type_node, pcvoid_type_node,
14639 NULL_TREE);
14641 tree void_ftype_opaque_long_pvoid
14642 = build_function_type_list (void_type_node,
14643 opaque_V4SI_type_node, long_integer_type_node,
14644 pvoid_type_node, NULL_TREE);
14645 tree void_ftype_v4si_long_pvoid
14646 = build_function_type_list (void_type_node,
14647 V4SI_type_node, long_integer_type_node,
14648 pvoid_type_node, NULL_TREE);
14649 tree void_ftype_v16qi_long_pvoid
14650 = build_function_type_list (void_type_node,
14651 V16QI_type_node, long_integer_type_node,
14652 pvoid_type_node, NULL_TREE);
14653 tree void_ftype_v8hi_long_pvoid
14654 = build_function_type_list (void_type_node,
14655 V8HI_type_node, long_integer_type_node,
14656 pvoid_type_node, NULL_TREE);
14657 tree void_ftype_v4sf_long_pvoid
14658 = build_function_type_list (void_type_node,
14659 V4SF_type_node, long_integer_type_node,
14660 pvoid_type_node, NULL_TREE);
14661 tree void_ftype_v2df_long_pvoid
14662 = build_function_type_list (void_type_node,
14663 V2DF_type_node, long_integer_type_node,
14664 pvoid_type_node, NULL_TREE);
14665 tree void_ftype_v2di_long_pvoid
14666 = build_function_type_list (void_type_node,
14667 V2DI_type_node, long_integer_type_node,
14668 pvoid_type_node, NULL_TREE);
14669 tree int_ftype_int_v8hi_v8hi
14670 = build_function_type_list (integer_type_node,
14671 integer_type_node, V8HI_type_node,
14672 V8HI_type_node, NULL_TREE);
14673 tree int_ftype_int_v16qi_v16qi
14674 = build_function_type_list (integer_type_node,
14675 integer_type_node, V16QI_type_node,
14676 V16QI_type_node, NULL_TREE);
14677 tree int_ftype_int_v4sf_v4sf
14678 = build_function_type_list (integer_type_node,
14679 integer_type_node, V4SF_type_node,
14680 V4SF_type_node, NULL_TREE);
14681 tree int_ftype_int_v2df_v2df
14682 = build_function_type_list (integer_type_node,
14683 integer_type_node, V2DF_type_node,
14684 V2DF_type_node, NULL_TREE);
14685 tree v2di_ftype_v2di
14686 = build_function_type_list (V2DI_type_node, V2DI_type_node, NULL_TREE);
14687 tree v4si_ftype_v4si
14688 = build_function_type_list (V4SI_type_node, V4SI_type_node, NULL_TREE);
14689 tree v8hi_ftype_v8hi
14690 = build_function_type_list (V8HI_type_node, V8HI_type_node, NULL_TREE);
14691 tree v16qi_ftype_v16qi
14692 = build_function_type_list (V16QI_type_node, V16QI_type_node, NULL_TREE);
14693 tree v4sf_ftype_v4sf
14694 = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
14695 tree v2df_ftype_v2df
14696 = build_function_type_list (V2DF_type_node, V2DF_type_node, NULL_TREE);
14697 tree void_ftype_pcvoid_int_int
14698 = build_function_type_list (void_type_node,
14699 pcvoid_type_node, integer_type_node,
14700 integer_type_node, NULL_TREE);
14702 def_builtin ("__builtin_altivec_mtvscr", void_ftype_v4si, ALTIVEC_BUILTIN_MTVSCR);
14703 def_builtin ("__builtin_altivec_mfvscr", v8hi_ftype_void, ALTIVEC_BUILTIN_MFVSCR);
14704 def_builtin ("__builtin_altivec_dssall", void_ftype_void, ALTIVEC_BUILTIN_DSSALL);
14705 def_builtin ("__builtin_altivec_dss", void_ftype_int, ALTIVEC_BUILTIN_DSS);
14706 def_builtin ("__builtin_altivec_lvsl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSL);
14707 def_builtin ("__builtin_altivec_lvsr", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSR);
14708 def_builtin ("__builtin_altivec_lvebx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEBX);
14709 def_builtin ("__builtin_altivec_lvehx", v8hi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEHX);
14710 def_builtin ("__builtin_altivec_lvewx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEWX);
14711 def_builtin ("__builtin_altivec_lvxl", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVXL);
14712 def_builtin ("__builtin_altivec_lvxl_v2df", v2df_ftype_long_pcvoid,
14713 ALTIVEC_BUILTIN_LVXL_V2DF);
14714 def_builtin ("__builtin_altivec_lvxl_v2di", v2di_ftype_long_pcvoid,
14715 ALTIVEC_BUILTIN_LVXL_V2DI);
14716 def_builtin ("__builtin_altivec_lvxl_v4sf", v4sf_ftype_long_pcvoid,
14717 ALTIVEC_BUILTIN_LVXL_V4SF);
14718 def_builtin ("__builtin_altivec_lvxl_v4si", v4si_ftype_long_pcvoid,
14719 ALTIVEC_BUILTIN_LVXL_V4SI);
14720 def_builtin ("__builtin_altivec_lvxl_v8hi", v8hi_ftype_long_pcvoid,
14721 ALTIVEC_BUILTIN_LVXL_V8HI);
14722 def_builtin ("__builtin_altivec_lvxl_v16qi", v16qi_ftype_long_pcvoid,
14723 ALTIVEC_BUILTIN_LVXL_V16QI);
14724 def_builtin ("__builtin_altivec_lvx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVX);
14725 def_builtin ("__builtin_altivec_lvx_v2df", v2df_ftype_long_pcvoid,
14726 ALTIVEC_BUILTIN_LVX_V2DF);
14727 def_builtin ("__builtin_altivec_lvx_v2di", v2di_ftype_long_pcvoid,
14728 ALTIVEC_BUILTIN_LVX_V2DI);
14729 def_builtin ("__builtin_altivec_lvx_v4sf", v4sf_ftype_long_pcvoid,
14730 ALTIVEC_BUILTIN_LVX_V4SF);
14731 def_builtin ("__builtin_altivec_lvx_v4si", v4si_ftype_long_pcvoid,
14732 ALTIVEC_BUILTIN_LVX_V4SI);
14733 def_builtin ("__builtin_altivec_lvx_v8hi", v8hi_ftype_long_pcvoid,
14734 ALTIVEC_BUILTIN_LVX_V8HI);
14735 def_builtin ("__builtin_altivec_lvx_v16qi", v16qi_ftype_long_pcvoid,
14736 ALTIVEC_BUILTIN_LVX_V16QI);
14737 def_builtin ("__builtin_altivec_stvx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVX);
14738 def_builtin ("__builtin_altivec_stvx_v2df", void_ftype_v2df_long_pvoid,
14739 ALTIVEC_BUILTIN_STVX_V2DF);
14740 def_builtin ("__builtin_altivec_stvx_v2di", void_ftype_v2di_long_pvoid,
14741 ALTIVEC_BUILTIN_STVX_V2DI);
14742 def_builtin ("__builtin_altivec_stvx_v4sf", void_ftype_v4sf_long_pvoid,
14743 ALTIVEC_BUILTIN_STVX_V4SF);
14744 def_builtin ("__builtin_altivec_stvx_v4si", void_ftype_v4si_long_pvoid,
14745 ALTIVEC_BUILTIN_STVX_V4SI);
14746 def_builtin ("__builtin_altivec_stvx_v8hi", void_ftype_v8hi_long_pvoid,
14747 ALTIVEC_BUILTIN_STVX_V8HI);
14748 def_builtin ("__builtin_altivec_stvx_v16qi", void_ftype_v16qi_long_pvoid,
14749 ALTIVEC_BUILTIN_STVX_V16QI);
14750 def_builtin ("__builtin_altivec_stvewx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVEWX);
14751 def_builtin ("__builtin_altivec_stvxl", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVXL);
14752 def_builtin ("__builtin_altivec_stvxl_v2df", void_ftype_v2df_long_pvoid,
14753 ALTIVEC_BUILTIN_STVXL_V2DF);
14754 def_builtin ("__builtin_altivec_stvxl_v2di", void_ftype_v2di_long_pvoid,
14755 ALTIVEC_BUILTIN_STVXL_V2DI);
14756 def_builtin ("__builtin_altivec_stvxl_v4sf", void_ftype_v4sf_long_pvoid,
14757 ALTIVEC_BUILTIN_STVXL_V4SF);
14758 def_builtin ("__builtin_altivec_stvxl_v4si", void_ftype_v4si_long_pvoid,
14759 ALTIVEC_BUILTIN_STVXL_V4SI);
14760 def_builtin ("__builtin_altivec_stvxl_v8hi", void_ftype_v8hi_long_pvoid,
14761 ALTIVEC_BUILTIN_STVXL_V8HI);
14762 def_builtin ("__builtin_altivec_stvxl_v16qi", void_ftype_v16qi_long_pvoid,
14763 ALTIVEC_BUILTIN_STVXL_V16QI);
14764 def_builtin ("__builtin_altivec_stvebx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVEBX);
14765 def_builtin ("__builtin_altivec_stvehx", void_ftype_v8hi_long_pvoid, ALTIVEC_BUILTIN_STVEHX);
14766 def_builtin ("__builtin_vec_ld", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LD);
14767 def_builtin ("__builtin_vec_lde", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LDE);
14768 def_builtin ("__builtin_vec_ldl", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LDL);
14769 def_builtin ("__builtin_vec_lvsl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVSL);
14770 def_builtin ("__builtin_vec_lvsr", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVSR);
14771 def_builtin ("__builtin_vec_lvebx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEBX);
14772 def_builtin ("__builtin_vec_lvehx", v8hi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEHX);
14773 def_builtin ("__builtin_vec_lvewx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEWX);
14774 def_builtin ("__builtin_vec_st", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_ST);
14775 def_builtin ("__builtin_vec_ste", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STE);
14776 def_builtin ("__builtin_vec_stl", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STL);
14777 def_builtin ("__builtin_vec_stvewx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEWX);
14778 def_builtin ("__builtin_vec_stvebx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEBX);
14779 def_builtin ("__builtin_vec_stvehx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEHX);
14781 def_builtin ("__builtin_vsx_lxvd2x_v2df", v2df_ftype_long_pcvoid,
14782 VSX_BUILTIN_LXVD2X_V2DF);
14783 def_builtin ("__builtin_vsx_lxvd2x_v2di", v2di_ftype_long_pcvoid,
14784 VSX_BUILTIN_LXVD2X_V2DI);
14785 def_builtin ("__builtin_vsx_lxvw4x_v4sf", v4sf_ftype_long_pcvoid,
14786 VSX_BUILTIN_LXVW4X_V4SF);
14787 def_builtin ("__builtin_vsx_lxvw4x_v4si", v4si_ftype_long_pcvoid,
14788 VSX_BUILTIN_LXVW4X_V4SI);
14789 def_builtin ("__builtin_vsx_lxvw4x_v8hi", v8hi_ftype_long_pcvoid,
14790 VSX_BUILTIN_LXVW4X_V8HI);
14791 def_builtin ("__builtin_vsx_lxvw4x_v16qi", v16qi_ftype_long_pcvoid,
14792 VSX_BUILTIN_LXVW4X_V16QI);
14793 def_builtin ("__builtin_vsx_stxvd2x_v2df", void_ftype_v2df_long_pvoid,
14794 VSX_BUILTIN_STXVD2X_V2DF);
14795 def_builtin ("__builtin_vsx_stxvd2x_v2di", void_ftype_v2di_long_pvoid,
14796 VSX_BUILTIN_STXVD2X_V2DI);
14797 def_builtin ("__builtin_vsx_stxvw4x_v4sf", void_ftype_v4sf_long_pvoid,
14798 VSX_BUILTIN_STXVW4X_V4SF);
14799 def_builtin ("__builtin_vsx_stxvw4x_v4si", void_ftype_v4si_long_pvoid,
14800 VSX_BUILTIN_STXVW4X_V4SI);
14801 def_builtin ("__builtin_vsx_stxvw4x_v8hi", void_ftype_v8hi_long_pvoid,
14802 VSX_BUILTIN_STXVW4X_V8HI);
14803 def_builtin ("__builtin_vsx_stxvw4x_v16qi", void_ftype_v16qi_long_pvoid,
14804 VSX_BUILTIN_STXVW4X_V16QI);
14805 def_builtin ("__builtin_vec_vsx_ld", opaque_ftype_long_pcvoid,
14806 VSX_BUILTIN_VEC_LD);
14807 def_builtin ("__builtin_vec_vsx_st", void_ftype_opaque_long_pvoid,
14808 VSX_BUILTIN_VEC_ST);
14810 def_builtin ("__builtin_vec_step", int_ftype_opaque, ALTIVEC_BUILTIN_VEC_STEP);
14811 def_builtin ("__builtin_vec_splats", opaque_ftype_opaque, ALTIVEC_BUILTIN_VEC_SPLATS);
14812 def_builtin ("__builtin_vec_promote", opaque_ftype_opaque, ALTIVEC_BUILTIN_VEC_PROMOTE);
14814 def_builtin ("__builtin_vec_sld", opaque_ftype_opaque_opaque_int, ALTIVEC_BUILTIN_VEC_SLD);
14815 def_builtin ("__builtin_vec_splat", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_SPLAT);
14816 def_builtin ("__builtin_vec_extract", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_EXTRACT);
14817 def_builtin ("__builtin_vec_insert", opaque_ftype_opaque_opaque_int, ALTIVEC_BUILTIN_VEC_INSERT);
14818 def_builtin ("__builtin_vec_vspltw", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTW);
14819 def_builtin ("__builtin_vec_vsplth", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTH);
14820 def_builtin ("__builtin_vec_vspltb", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTB);
14821 def_builtin ("__builtin_vec_ctf", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTF);
14822 def_builtin ("__builtin_vec_vcfsx", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VCFSX);
14823 def_builtin ("__builtin_vec_vcfux", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VCFUX);
14824 def_builtin ("__builtin_vec_cts", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTS);
14825 def_builtin ("__builtin_vec_ctu", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTU);
14827 /* Cell builtins. */
14828 def_builtin ("__builtin_altivec_lvlx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVLX);
14829 def_builtin ("__builtin_altivec_lvlxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVLXL);
14830 def_builtin ("__builtin_altivec_lvrx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVRX);
14831 def_builtin ("__builtin_altivec_lvrxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVRXL);
14833 def_builtin ("__builtin_vec_lvlx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVLX);
14834 def_builtin ("__builtin_vec_lvlxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVLXL);
14835 def_builtin ("__builtin_vec_lvrx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVRX);
14836 def_builtin ("__builtin_vec_lvrxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVRXL);
14838 def_builtin ("__builtin_altivec_stvlx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVLX);
14839 def_builtin ("__builtin_altivec_stvlxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVLXL);
14840 def_builtin ("__builtin_altivec_stvrx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVRX);
14841 def_builtin ("__builtin_altivec_stvrxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVRXL);
14843 def_builtin ("__builtin_vec_stvlx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVLX);
14844 def_builtin ("__builtin_vec_stvlxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVLXL);
14845 def_builtin ("__builtin_vec_stvrx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVRX);
14846 def_builtin ("__builtin_vec_stvrxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVRXL);
14848 /* Add the DST variants. */
14849 d = bdesc_dst;
14850 for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
14851 def_builtin (d->name, void_ftype_pcvoid_int_int, d->code);
14853 /* Initialize the predicates. */
14854 d = bdesc_altivec_preds;
14855 for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, d++)
14857 enum machine_mode mode1;
14858 tree type;
14860 if (rs6000_overloaded_builtin_p (d->code))
14861 mode1 = VOIDmode;
14862 else
14863 mode1 = insn_data[d->icode].operand[1].mode;
14865 switch (mode1)
14867 case VOIDmode:
14868 type = int_ftype_int_opaque_opaque;
14869 break;
14870 case V2DImode:
14871 type = int_ftype_int_v2di_v2di;
14872 break;
14873 case V4SImode:
14874 type = int_ftype_int_v4si_v4si;
14875 break;
14876 case V8HImode:
14877 type = int_ftype_int_v8hi_v8hi;
14878 break;
14879 case V16QImode:
14880 type = int_ftype_int_v16qi_v16qi;
14881 break;
14882 case V4SFmode:
14883 type = int_ftype_int_v4sf_v4sf;
14884 break;
14885 case V2DFmode:
14886 type = int_ftype_int_v2df_v2df;
14887 break;
14888 default:
14889 gcc_unreachable ();
14892 def_builtin (d->name, type, d->code);
14895 /* Initialize the abs* operators. */
14896 d = bdesc_abs;
14897 for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
14899 enum machine_mode mode0;
14900 tree type;
14902 mode0 = insn_data[d->icode].operand[0].mode;
14904 switch (mode0)
14906 case V2DImode:
14907 type = v2di_ftype_v2di;
14908 break;
14909 case V4SImode:
14910 type = v4si_ftype_v4si;
14911 break;
14912 case V8HImode:
14913 type = v8hi_ftype_v8hi;
14914 break;
14915 case V16QImode:
14916 type = v16qi_ftype_v16qi;
14917 break;
14918 case V4SFmode:
14919 type = v4sf_ftype_v4sf;
14920 break;
14921 case V2DFmode:
14922 type = v2df_ftype_v2df;
14923 break;
14924 default:
14925 gcc_unreachable ();
14928 def_builtin (d->name, type, d->code);
14931 /* Initialize target builtin that implements
14932 targetm.vectorize.builtin_mask_for_load. */
14934 decl = add_builtin_function ("__builtin_altivec_mask_for_load",
14935 v16qi_ftype_long_pcvoid,
14936 ALTIVEC_BUILTIN_MASK_FOR_LOAD,
14937 BUILT_IN_MD, NULL, NULL_TREE);
14938 TREE_READONLY (decl) = 1;
14939 /* Record the decl. Will be used by rs6000_builtin_mask_for_load. */
14940 altivec_builtin_mask_for_load = decl;
14942 /* Access to the vec_init patterns. */
14943 ftype = build_function_type_list (V4SI_type_node, integer_type_node,
14944 integer_type_node, integer_type_node,
14945 integer_type_node, NULL_TREE);
14946 def_builtin ("__builtin_vec_init_v4si", ftype, ALTIVEC_BUILTIN_VEC_INIT_V4SI);
14948 ftype = build_function_type_list (V8HI_type_node, short_integer_type_node,
14949 short_integer_type_node,
14950 short_integer_type_node,
14951 short_integer_type_node,
14952 short_integer_type_node,
14953 short_integer_type_node,
14954 short_integer_type_node,
14955 short_integer_type_node, NULL_TREE);
14956 def_builtin ("__builtin_vec_init_v8hi", ftype, ALTIVEC_BUILTIN_VEC_INIT_V8HI);
14958 ftype = build_function_type_list (V16QI_type_node, char_type_node,
14959 char_type_node, char_type_node,
14960 char_type_node, char_type_node,
14961 char_type_node, char_type_node,
14962 char_type_node, char_type_node,
14963 char_type_node, char_type_node,
14964 char_type_node, char_type_node,
14965 char_type_node, char_type_node,
14966 char_type_node, NULL_TREE);
14967 def_builtin ("__builtin_vec_init_v16qi", ftype,
14968 ALTIVEC_BUILTIN_VEC_INIT_V16QI);
14970 ftype = build_function_type_list (V4SF_type_node, float_type_node,
14971 float_type_node, float_type_node,
14972 float_type_node, NULL_TREE);
14973 def_builtin ("__builtin_vec_init_v4sf", ftype, ALTIVEC_BUILTIN_VEC_INIT_V4SF);
14975 /* VSX builtins. */
14976 ftype = build_function_type_list (V2DF_type_node, double_type_node,
14977 double_type_node, NULL_TREE);
14978 def_builtin ("__builtin_vec_init_v2df", ftype, VSX_BUILTIN_VEC_INIT_V2DF);
14980 ftype = build_function_type_list (V2DI_type_node, intDI_type_node,
14981 intDI_type_node, NULL_TREE);
14982 def_builtin ("__builtin_vec_init_v2di", ftype, VSX_BUILTIN_VEC_INIT_V2DI);
14984 /* Access to the vec_set patterns. */
14985 ftype = build_function_type_list (V4SI_type_node, V4SI_type_node,
14986 intSI_type_node,
14987 integer_type_node, NULL_TREE);
14988 def_builtin ("__builtin_vec_set_v4si", ftype, ALTIVEC_BUILTIN_VEC_SET_V4SI);
14990 ftype = build_function_type_list (V8HI_type_node, V8HI_type_node,
14991 intHI_type_node,
14992 integer_type_node, NULL_TREE);
14993 def_builtin ("__builtin_vec_set_v8hi", ftype, ALTIVEC_BUILTIN_VEC_SET_V8HI);
14995 ftype = build_function_type_list (V16QI_type_node, V16QI_type_node,
14996 intQI_type_node,
14997 integer_type_node, NULL_TREE);
14998 def_builtin ("__builtin_vec_set_v16qi", ftype, ALTIVEC_BUILTIN_VEC_SET_V16QI);
15000 ftype = build_function_type_list (V4SF_type_node, V4SF_type_node,
15001 float_type_node,
15002 integer_type_node, NULL_TREE);
15003 def_builtin ("__builtin_vec_set_v4sf", ftype, ALTIVEC_BUILTIN_VEC_SET_V4SF);
15005 ftype = build_function_type_list (V2DF_type_node, V2DF_type_node,
15006 double_type_node,
15007 integer_type_node, NULL_TREE);
15008 def_builtin ("__builtin_vec_set_v2df", ftype, VSX_BUILTIN_VEC_SET_V2DF);
15010 ftype = build_function_type_list (V2DI_type_node, V2DI_type_node,
15011 intDI_type_node,
15012 integer_type_node, NULL_TREE);
15013 def_builtin ("__builtin_vec_set_v2di", ftype, VSX_BUILTIN_VEC_SET_V2DI);
15015 /* Access to the vec_extract patterns. */
15016 ftype = build_function_type_list (intSI_type_node, V4SI_type_node,
15017 integer_type_node, NULL_TREE);
15018 def_builtin ("__builtin_vec_ext_v4si", ftype, ALTIVEC_BUILTIN_VEC_EXT_V4SI);
15020 ftype = build_function_type_list (intHI_type_node, V8HI_type_node,
15021 integer_type_node, NULL_TREE);
15022 def_builtin ("__builtin_vec_ext_v8hi", ftype, ALTIVEC_BUILTIN_VEC_EXT_V8HI);
15024 ftype = build_function_type_list (intQI_type_node, V16QI_type_node,
15025 integer_type_node, NULL_TREE);
15026 def_builtin ("__builtin_vec_ext_v16qi", ftype, ALTIVEC_BUILTIN_VEC_EXT_V16QI);
15028 ftype = build_function_type_list (float_type_node, V4SF_type_node,
15029 integer_type_node, NULL_TREE);
15030 def_builtin ("__builtin_vec_ext_v4sf", ftype, ALTIVEC_BUILTIN_VEC_EXT_V4SF);
15032 ftype = build_function_type_list (double_type_node, V2DF_type_node,
15033 integer_type_node, NULL_TREE);
15034 def_builtin ("__builtin_vec_ext_v2df", ftype, VSX_BUILTIN_VEC_EXT_V2DF);
15036 ftype = build_function_type_list (intDI_type_node, V2DI_type_node,
15037 integer_type_node, NULL_TREE);
15038 def_builtin ("__builtin_vec_ext_v2di", ftype, VSX_BUILTIN_VEC_EXT_V2DI);
15041 if (V1TI_type_node)
15043 tree v1ti_ftype_long_pcvoid
15044 = build_function_type_list (V1TI_type_node,
15045 long_integer_type_node, pcvoid_type_node,
15046 NULL_TREE);
15047 tree void_ftype_v1ti_long_pvoid
15048 = build_function_type_list (void_type_node,
15049 V1TI_type_node, long_integer_type_node,
15050 pvoid_type_node, NULL_TREE);
15051 def_builtin ("__builtin_vsx_lxvd2x_v1ti", v1ti_ftype_long_pcvoid,
15052 VSX_BUILTIN_LXVD2X_V1TI);
15053 def_builtin ("__builtin_vsx_stxvd2x_v1ti", void_ftype_v1ti_long_pvoid,
15054 VSX_BUILTIN_STXVD2X_V1TI);
15055 ftype = build_function_type_list (V1TI_type_node, intTI_type_node,
15056 NULL_TREE, NULL_TREE);
15057 def_builtin ("__builtin_vec_init_v1ti", ftype, VSX_BUILTIN_VEC_INIT_V1TI);
15058 ftype = build_function_type_list (V1TI_type_node, V1TI_type_node,
15059 intTI_type_node,
15060 integer_type_node, NULL_TREE);
15061 def_builtin ("__builtin_vec_set_v1ti", ftype, VSX_BUILTIN_VEC_SET_V1TI);
15062 ftype = build_function_type_list (intTI_type_node, V1TI_type_node,
15063 integer_type_node, NULL_TREE);
15064 def_builtin ("__builtin_vec_ext_v1ti", ftype, VSX_BUILTIN_VEC_EXT_V1TI);
15069 static void
15070 htm_init_builtins (void)
15072 HOST_WIDE_INT builtin_mask = rs6000_builtin_mask;
15073 const struct builtin_description *d;
15074 size_t i;
15076 d = bdesc_htm;
15077 for (i = 0; i < ARRAY_SIZE (bdesc_htm); i++, d++)
15079 tree op[MAX_HTM_OPERANDS], type;
15080 HOST_WIDE_INT mask = d->mask;
15081 unsigned attr = rs6000_builtin_info[d->code].attr;
15082 bool void_func = (attr & RS6000_BTC_VOID);
15083 int attr_args = (attr & RS6000_BTC_TYPE_MASK);
15084 int nopnds = 0;
15085 tree argtype = (attr & RS6000_BTC_SPR) ? long_unsigned_type_node
15086 : unsigned_type_node;
15088 if ((mask & builtin_mask) != mask)
15090 if (TARGET_DEBUG_BUILTIN)
15091 fprintf (stderr, "htm_builtin, skip binary %s\n", d->name);
15092 continue;
15095 if (d->name == 0)
15097 if (TARGET_DEBUG_BUILTIN)
15098 fprintf (stderr, "htm_builtin, bdesc_htm[%ld] no name\n",
15099 (long unsigned) i);
15100 continue;
15103 op[nopnds++] = (void_func) ? void_type_node : argtype;
15105 if (attr_args == RS6000_BTC_UNARY)
15106 op[nopnds++] = argtype;
15107 else if (attr_args == RS6000_BTC_BINARY)
15109 op[nopnds++] = argtype;
15110 op[nopnds++] = argtype;
15112 else if (attr_args == RS6000_BTC_TERNARY)
15114 op[nopnds++] = argtype;
15115 op[nopnds++] = argtype;
15116 op[nopnds++] = argtype;
15119 switch (nopnds)
15121 case 1:
15122 type = build_function_type_list (op[0], NULL_TREE);
15123 break;
15124 case 2:
15125 type = build_function_type_list (op[0], op[1], NULL_TREE);
15126 break;
15127 case 3:
15128 type = build_function_type_list (op[0], op[1], op[2], NULL_TREE);
15129 break;
15130 case 4:
15131 type = build_function_type_list (op[0], op[1], op[2], op[3],
15132 NULL_TREE);
15133 break;
15134 default:
15135 gcc_unreachable ();
15138 def_builtin (d->name, type, d->code);
15142 /* Hash function for builtin functions with up to 3 arguments and a return
15143 type. */
15144 hashval_t
15145 builtin_hasher::hash (builtin_hash_struct *bh)
15147 unsigned ret = 0;
15148 int i;
15150 for (i = 0; i < 4; i++)
15152 ret = (ret * (unsigned)MAX_MACHINE_MODE) + ((unsigned)bh->mode[i]);
15153 ret = (ret * 2) + bh->uns_p[i];
15156 return ret;
15159 /* Compare builtin hash entries H1 and H2 for equivalence. */
15160 bool
15161 builtin_hasher::equal (builtin_hash_struct *p1, builtin_hash_struct *p2)
15163 return ((p1->mode[0] == p2->mode[0])
15164 && (p1->mode[1] == p2->mode[1])
15165 && (p1->mode[2] == p2->mode[2])
15166 && (p1->mode[3] == p2->mode[3])
15167 && (p1->uns_p[0] == p2->uns_p[0])
15168 && (p1->uns_p[1] == p2->uns_p[1])
15169 && (p1->uns_p[2] == p2->uns_p[2])
15170 && (p1->uns_p[3] == p2->uns_p[3]));
15173 /* Map types for builtin functions with an explicit return type and up to 3
15174 arguments. Functions with fewer than 3 arguments use VOIDmode as the type
15175 of the argument. */
15176 static tree
15177 builtin_function_type (enum machine_mode mode_ret, enum machine_mode mode_arg0,
15178 enum machine_mode mode_arg1, enum machine_mode mode_arg2,
15179 enum rs6000_builtins builtin, const char *name)
15181 struct builtin_hash_struct h;
15182 struct builtin_hash_struct *h2;
15183 int num_args = 3;
15184 int i;
15185 tree ret_type = NULL_TREE;
15186 tree arg_type[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
15188 /* Create builtin_hash_table. */
15189 if (builtin_hash_table == NULL)
15190 builtin_hash_table = hash_table<builtin_hasher>::create_ggc (1500);
15192 h.type = NULL_TREE;
15193 h.mode[0] = mode_ret;
15194 h.mode[1] = mode_arg0;
15195 h.mode[2] = mode_arg1;
15196 h.mode[3] = mode_arg2;
15197 h.uns_p[0] = 0;
15198 h.uns_p[1] = 0;
15199 h.uns_p[2] = 0;
15200 h.uns_p[3] = 0;
15202 /* If the builtin is a type that produces unsigned results or takes unsigned
15203 arguments, and it is returned as a decl for the vectorizer (such as
15204 widening multiplies, permute), make sure the arguments and return value
15205 are type correct. */
15206 switch (builtin)
15208 /* unsigned 1 argument functions. */
15209 case CRYPTO_BUILTIN_VSBOX:
15210 case P8V_BUILTIN_VGBBD:
15211 case MISC_BUILTIN_CDTBCD:
15212 case MISC_BUILTIN_CBCDTD:
15213 h.uns_p[0] = 1;
15214 h.uns_p[1] = 1;
15215 break;
15217 /* unsigned 2 argument functions. */
15218 case ALTIVEC_BUILTIN_VMULEUB_UNS:
15219 case ALTIVEC_BUILTIN_VMULEUH_UNS:
15220 case ALTIVEC_BUILTIN_VMULOUB_UNS:
15221 case ALTIVEC_BUILTIN_VMULOUH_UNS:
15222 case CRYPTO_BUILTIN_VCIPHER:
15223 case CRYPTO_BUILTIN_VCIPHERLAST:
15224 case CRYPTO_BUILTIN_VNCIPHER:
15225 case CRYPTO_BUILTIN_VNCIPHERLAST:
15226 case CRYPTO_BUILTIN_VPMSUMB:
15227 case CRYPTO_BUILTIN_VPMSUMH:
15228 case CRYPTO_BUILTIN_VPMSUMW:
15229 case CRYPTO_BUILTIN_VPMSUMD:
15230 case CRYPTO_BUILTIN_VPMSUM:
15231 case MISC_BUILTIN_ADDG6S:
15232 case MISC_BUILTIN_DIVWEU:
15233 case MISC_BUILTIN_DIVWEUO:
15234 case MISC_BUILTIN_DIVDEU:
15235 case MISC_BUILTIN_DIVDEUO:
15236 h.uns_p[0] = 1;
15237 h.uns_p[1] = 1;
15238 h.uns_p[2] = 1;
15239 break;
15241 /* unsigned 3 argument functions. */
15242 case ALTIVEC_BUILTIN_VPERM_16QI_UNS:
15243 case ALTIVEC_BUILTIN_VPERM_8HI_UNS:
15244 case ALTIVEC_BUILTIN_VPERM_4SI_UNS:
15245 case ALTIVEC_BUILTIN_VPERM_2DI_UNS:
15246 case ALTIVEC_BUILTIN_VSEL_16QI_UNS:
15247 case ALTIVEC_BUILTIN_VSEL_8HI_UNS:
15248 case ALTIVEC_BUILTIN_VSEL_4SI_UNS:
15249 case ALTIVEC_BUILTIN_VSEL_2DI_UNS:
15250 case VSX_BUILTIN_VPERM_16QI_UNS:
15251 case VSX_BUILTIN_VPERM_8HI_UNS:
15252 case VSX_BUILTIN_VPERM_4SI_UNS:
15253 case VSX_BUILTIN_VPERM_2DI_UNS:
15254 case VSX_BUILTIN_XXSEL_16QI_UNS:
15255 case VSX_BUILTIN_XXSEL_8HI_UNS:
15256 case VSX_BUILTIN_XXSEL_4SI_UNS:
15257 case VSX_BUILTIN_XXSEL_2DI_UNS:
15258 case CRYPTO_BUILTIN_VPERMXOR:
15259 case CRYPTO_BUILTIN_VPERMXOR_V2DI:
15260 case CRYPTO_BUILTIN_VPERMXOR_V4SI:
15261 case CRYPTO_BUILTIN_VPERMXOR_V8HI:
15262 case CRYPTO_BUILTIN_VPERMXOR_V16QI:
15263 case CRYPTO_BUILTIN_VSHASIGMAW:
15264 case CRYPTO_BUILTIN_VSHASIGMAD:
15265 case CRYPTO_BUILTIN_VSHASIGMA:
15266 h.uns_p[0] = 1;
15267 h.uns_p[1] = 1;
15268 h.uns_p[2] = 1;
15269 h.uns_p[3] = 1;
15270 break;
15272 /* signed permute functions with unsigned char mask. */
15273 case ALTIVEC_BUILTIN_VPERM_16QI:
15274 case ALTIVEC_BUILTIN_VPERM_8HI:
15275 case ALTIVEC_BUILTIN_VPERM_4SI:
15276 case ALTIVEC_BUILTIN_VPERM_4SF:
15277 case ALTIVEC_BUILTIN_VPERM_2DI:
15278 case ALTIVEC_BUILTIN_VPERM_2DF:
15279 case VSX_BUILTIN_VPERM_16QI:
15280 case VSX_BUILTIN_VPERM_8HI:
15281 case VSX_BUILTIN_VPERM_4SI:
15282 case VSX_BUILTIN_VPERM_4SF:
15283 case VSX_BUILTIN_VPERM_2DI:
15284 case VSX_BUILTIN_VPERM_2DF:
15285 h.uns_p[3] = 1;
15286 break;
15288 /* unsigned args, signed return. */
15289 case VSX_BUILTIN_XVCVUXDDP_UNS:
15290 case ALTIVEC_BUILTIN_UNSFLOAT_V4SI_V4SF:
15291 h.uns_p[1] = 1;
15292 break;
15294 /* signed args, unsigned return. */
15295 case VSX_BUILTIN_XVCVDPUXDS_UNS:
15296 case ALTIVEC_BUILTIN_FIXUNS_V4SF_V4SI:
15297 case MISC_BUILTIN_UNPACK_TD:
15298 case MISC_BUILTIN_UNPACK_V1TI:
15299 h.uns_p[0] = 1;
15300 break;
15302 /* unsigned arguments for 128-bit pack instructions. */
15303 case MISC_BUILTIN_PACK_TD:
15304 case MISC_BUILTIN_PACK_V1TI:
15305 h.uns_p[1] = 1;
15306 h.uns_p[2] = 1;
15307 break;
15309 default:
15310 break;
15313 /* Figure out how many args are present. */
15314 while (num_args > 0 && h.mode[num_args] == VOIDmode)
15315 num_args--;
15317 if (num_args == 0)
15318 fatal_error ("internal error: builtin function %s had no type", name);
15320 ret_type = builtin_mode_to_type[h.mode[0]][h.uns_p[0]];
15321 if (!ret_type && h.uns_p[0])
15322 ret_type = builtin_mode_to_type[h.mode[0]][0];
15324 if (!ret_type)
15325 fatal_error ("internal error: builtin function %s had an unexpected "
15326 "return type %s", name, GET_MODE_NAME (h.mode[0]));
15328 for (i = 0; i < (int) ARRAY_SIZE (arg_type); i++)
15329 arg_type[i] = NULL_TREE;
15331 for (i = 0; i < num_args; i++)
15333 int m = (int) h.mode[i+1];
15334 int uns_p = h.uns_p[i+1];
15336 arg_type[i] = builtin_mode_to_type[m][uns_p];
15337 if (!arg_type[i] && uns_p)
15338 arg_type[i] = builtin_mode_to_type[m][0];
15340 if (!arg_type[i])
15341 fatal_error ("internal error: builtin function %s, argument %d "
15342 "had unexpected argument type %s", name, i,
15343 GET_MODE_NAME (m));
15346 builtin_hash_struct **found = builtin_hash_table->find_slot (&h, INSERT);
15347 if (*found == NULL)
15349 h2 = ggc_alloc<builtin_hash_struct> ();
15350 *h2 = h;
15351 *found = h2;
15353 h2->type = build_function_type_list (ret_type, arg_type[0], arg_type[1],
15354 arg_type[2], NULL_TREE);
15357 return (*found)->type;
15360 static void
15361 rs6000_common_init_builtins (void)
15363 const struct builtin_description *d;
15364 size_t i;
15366 tree opaque_ftype_opaque = NULL_TREE;
15367 tree opaque_ftype_opaque_opaque = NULL_TREE;
15368 tree opaque_ftype_opaque_opaque_opaque = NULL_TREE;
15369 tree v2si_ftype_qi = NULL_TREE;
15370 tree v2si_ftype_v2si_qi = NULL_TREE;
15371 tree v2si_ftype_int_qi = NULL_TREE;
15372 HOST_WIDE_INT builtin_mask = rs6000_builtin_mask;
15374 if (!TARGET_PAIRED_FLOAT)
15376 builtin_mode_to_type[V2SImode][0] = opaque_V2SI_type_node;
15377 builtin_mode_to_type[V2SFmode][0] = opaque_V2SF_type_node;
15380 /* Paired and SPE builtins are only available if you build a compiler with
15381 the appropriate options, so only create those builtins with the
15382 appropriate compiler option. Create Altivec and VSX builtins on machines
15383 with at least the general purpose extensions (970 and newer) to allow the
15384 use of the target attribute.. */
15386 if (TARGET_EXTRA_BUILTINS)
15387 builtin_mask |= RS6000_BTM_COMMON;
15389 /* Add the ternary operators. */
15390 d = bdesc_3arg;
15391 for (i = 0; i < ARRAY_SIZE (bdesc_3arg); i++, d++)
15393 tree type;
15394 HOST_WIDE_INT mask = d->mask;
15396 if ((mask & builtin_mask) != mask)
15398 if (TARGET_DEBUG_BUILTIN)
15399 fprintf (stderr, "rs6000_builtin, skip ternary %s\n", d->name);
15400 continue;
15403 if (rs6000_overloaded_builtin_p (d->code))
15405 if (! (type = opaque_ftype_opaque_opaque_opaque))
15406 type = opaque_ftype_opaque_opaque_opaque
15407 = build_function_type_list (opaque_V4SI_type_node,
15408 opaque_V4SI_type_node,
15409 opaque_V4SI_type_node,
15410 opaque_V4SI_type_node,
15411 NULL_TREE);
15413 else
15415 enum insn_code icode = d->icode;
15416 if (d->name == 0)
15418 if (TARGET_DEBUG_BUILTIN)
15419 fprintf (stderr, "rs6000_builtin, bdesc_3arg[%ld] no name\n",
15420 (long unsigned)i);
15422 continue;
15425 if (icode == CODE_FOR_nothing)
15427 if (TARGET_DEBUG_BUILTIN)
15428 fprintf (stderr, "rs6000_builtin, skip ternary %s (no code)\n",
15429 d->name);
15431 continue;
15434 type = builtin_function_type (insn_data[icode].operand[0].mode,
15435 insn_data[icode].operand[1].mode,
15436 insn_data[icode].operand[2].mode,
15437 insn_data[icode].operand[3].mode,
15438 d->code, d->name);
15441 def_builtin (d->name, type, d->code);
15444 /* Add the binary operators. */
15445 d = bdesc_2arg;
15446 for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
15448 enum machine_mode mode0, mode1, mode2;
15449 tree type;
15450 HOST_WIDE_INT mask = d->mask;
15452 if ((mask & builtin_mask) != mask)
15454 if (TARGET_DEBUG_BUILTIN)
15455 fprintf (stderr, "rs6000_builtin, skip binary %s\n", d->name);
15456 continue;
15459 if (rs6000_overloaded_builtin_p (d->code))
15461 if (! (type = opaque_ftype_opaque_opaque))
15462 type = opaque_ftype_opaque_opaque
15463 = build_function_type_list (opaque_V4SI_type_node,
15464 opaque_V4SI_type_node,
15465 opaque_V4SI_type_node,
15466 NULL_TREE);
15468 else
15470 enum insn_code icode = d->icode;
15471 if (d->name == 0)
15473 if (TARGET_DEBUG_BUILTIN)
15474 fprintf (stderr, "rs6000_builtin, bdesc_2arg[%ld] no name\n",
15475 (long unsigned)i);
15477 continue;
15480 if (icode == CODE_FOR_nothing)
15482 if (TARGET_DEBUG_BUILTIN)
15483 fprintf (stderr, "rs6000_builtin, skip binary %s (no code)\n",
15484 d->name);
15486 continue;
15489 mode0 = insn_data[icode].operand[0].mode;
15490 mode1 = insn_data[icode].operand[1].mode;
15491 mode2 = insn_data[icode].operand[2].mode;
15493 if (mode0 == V2SImode && mode1 == V2SImode && mode2 == QImode)
15495 if (! (type = v2si_ftype_v2si_qi))
15496 type = v2si_ftype_v2si_qi
15497 = build_function_type_list (opaque_V2SI_type_node,
15498 opaque_V2SI_type_node,
15499 char_type_node,
15500 NULL_TREE);
15503 else if (mode0 == V2SImode && GET_MODE_CLASS (mode1) == MODE_INT
15504 && mode2 == QImode)
15506 if (! (type = v2si_ftype_int_qi))
15507 type = v2si_ftype_int_qi
15508 = build_function_type_list (opaque_V2SI_type_node,
15509 integer_type_node,
15510 char_type_node,
15511 NULL_TREE);
15514 else
15515 type = builtin_function_type (mode0, mode1, mode2, VOIDmode,
15516 d->code, d->name);
15519 def_builtin (d->name, type, d->code);
15522 /* Add the simple unary operators. */
15523 d = bdesc_1arg;
15524 for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
15526 enum machine_mode mode0, mode1;
15527 tree type;
15528 HOST_WIDE_INT mask = d->mask;
15530 if ((mask & builtin_mask) != mask)
15532 if (TARGET_DEBUG_BUILTIN)
15533 fprintf (stderr, "rs6000_builtin, skip unary %s\n", d->name);
15534 continue;
15537 if (rs6000_overloaded_builtin_p (d->code))
15539 if (! (type = opaque_ftype_opaque))
15540 type = opaque_ftype_opaque
15541 = build_function_type_list (opaque_V4SI_type_node,
15542 opaque_V4SI_type_node,
15543 NULL_TREE);
15545 else
15547 enum insn_code icode = d->icode;
15548 if (d->name == 0)
15550 if (TARGET_DEBUG_BUILTIN)
15551 fprintf (stderr, "rs6000_builtin, bdesc_1arg[%ld] no name\n",
15552 (long unsigned)i);
15554 continue;
15557 if (icode == CODE_FOR_nothing)
15559 if (TARGET_DEBUG_BUILTIN)
15560 fprintf (stderr, "rs6000_builtin, skip unary %s (no code)\n",
15561 d->name);
15563 continue;
15566 mode0 = insn_data[icode].operand[0].mode;
15567 mode1 = insn_data[icode].operand[1].mode;
15569 if (mode0 == V2SImode && mode1 == QImode)
15571 if (! (type = v2si_ftype_qi))
15572 type = v2si_ftype_qi
15573 = build_function_type_list (opaque_V2SI_type_node,
15574 char_type_node,
15575 NULL_TREE);
15578 else
15579 type = builtin_function_type (mode0, mode1, VOIDmode, VOIDmode,
15580 d->code, d->name);
15583 def_builtin (d->name, type, d->code);
15587 static void
15588 rs6000_init_libfuncs (void)
15590 if (!TARGET_IEEEQUAD)
15591 /* AIX/Darwin/64-bit Linux quad floating point routines. */
15592 if (!TARGET_XL_COMPAT)
15594 set_optab_libfunc (add_optab, TFmode, "__gcc_qadd");
15595 set_optab_libfunc (sub_optab, TFmode, "__gcc_qsub");
15596 set_optab_libfunc (smul_optab, TFmode, "__gcc_qmul");
15597 set_optab_libfunc (sdiv_optab, TFmode, "__gcc_qdiv");
15599 if (!(TARGET_HARD_FLOAT && (TARGET_FPRS || TARGET_E500_DOUBLE)))
15601 set_optab_libfunc (neg_optab, TFmode, "__gcc_qneg");
15602 set_optab_libfunc (eq_optab, TFmode, "__gcc_qeq");
15603 set_optab_libfunc (ne_optab, TFmode, "__gcc_qne");
15604 set_optab_libfunc (gt_optab, TFmode, "__gcc_qgt");
15605 set_optab_libfunc (ge_optab, TFmode, "__gcc_qge");
15606 set_optab_libfunc (lt_optab, TFmode, "__gcc_qlt");
15607 set_optab_libfunc (le_optab, TFmode, "__gcc_qle");
15609 set_conv_libfunc (sext_optab, TFmode, SFmode, "__gcc_stoq");
15610 set_conv_libfunc (sext_optab, TFmode, DFmode, "__gcc_dtoq");
15611 set_conv_libfunc (trunc_optab, SFmode, TFmode, "__gcc_qtos");
15612 set_conv_libfunc (trunc_optab, DFmode, TFmode, "__gcc_qtod");
15613 set_conv_libfunc (sfix_optab, SImode, TFmode, "__gcc_qtoi");
15614 set_conv_libfunc (ufix_optab, SImode, TFmode, "__gcc_qtou");
15615 set_conv_libfunc (sfloat_optab, TFmode, SImode, "__gcc_itoq");
15616 set_conv_libfunc (ufloat_optab, TFmode, SImode, "__gcc_utoq");
15619 if (!(TARGET_HARD_FLOAT && TARGET_FPRS))
15620 set_optab_libfunc (unord_optab, TFmode, "__gcc_qunord");
15622 else
15624 set_optab_libfunc (add_optab, TFmode, "_xlqadd");
15625 set_optab_libfunc (sub_optab, TFmode, "_xlqsub");
15626 set_optab_libfunc (smul_optab, TFmode, "_xlqmul");
15627 set_optab_libfunc (sdiv_optab, TFmode, "_xlqdiv");
15629 else
15631 /* 32-bit SVR4 quad floating point routines. */
15633 set_optab_libfunc (add_optab, TFmode, "_q_add");
15634 set_optab_libfunc (sub_optab, TFmode, "_q_sub");
15635 set_optab_libfunc (neg_optab, TFmode, "_q_neg");
15636 set_optab_libfunc (smul_optab, TFmode, "_q_mul");
15637 set_optab_libfunc (sdiv_optab, TFmode, "_q_div");
15638 if (TARGET_PPC_GPOPT)
15639 set_optab_libfunc (sqrt_optab, TFmode, "_q_sqrt");
15641 set_optab_libfunc (eq_optab, TFmode, "_q_feq");
15642 set_optab_libfunc (ne_optab, TFmode, "_q_fne");
15643 set_optab_libfunc (gt_optab, TFmode, "_q_fgt");
15644 set_optab_libfunc (ge_optab, TFmode, "_q_fge");
15645 set_optab_libfunc (lt_optab, TFmode, "_q_flt");
15646 set_optab_libfunc (le_optab, TFmode, "_q_fle");
15648 set_conv_libfunc (sext_optab, TFmode, SFmode, "_q_stoq");
15649 set_conv_libfunc (sext_optab, TFmode, DFmode, "_q_dtoq");
15650 set_conv_libfunc (trunc_optab, SFmode, TFmode, "_q_qtos");
15651 set_conv_libfunc (trunc_optab, DFmode, TFmode, "_q_qtod");
15652 set_conv_libfunc (sfix_optab, SImode, TFmode, "_q_qtoi");
15653 set_conv_libfunc (ufix_optab, SImode, TFmode, "_q_qtou");
15654 set_conv_libfunc (sfloat_optab, TFmode, SImode, "_q_itoq");
15655 set_conv_libfunc (ufloat_optab, TFmode, SImode, "_q_utoq");
15660 /* Expand a block clear operation, and return 1 if successful. Return 0
15661 if we should let the compiler generate normal code.
15663 operands[0] is the destination
15664 operands[1] is the length
15665 operands[3] is the alignment */
15668 expand_block_clear (rtx operands[])
15670 rtx orig_dest = operands[0];
15671 rtx bytes_rtx = operands[1];
15672 rtx align_rtx = operands[3];
15673 bool constp = (GET_CODE (bytes_rtx) == CONST_INT);
15674 HOST_WIDE_INT align;
15675 HOST_WIDE_INT bytes;
15676 int offset;
15677 int clear_bytes;
15678 int clear_step;
15680 /* If this is not a fixed size move, just call memcpy */
15681 if (! constp)
15682 return 0;
15684 /* This must be a fixed size alignment */
15685 gcc_assert (GET_CODE (align_rtx) == CONST_INT);
15686 align = INTVAL (align_rtx) * BITS_PER_UNIT;
15688 /* Anything to clear? */
15689 bytes = INTVAL (bytes_rtx);
15690 if (bytes <= 0)
15691 return 1;
15693 /* Use the builtin memset after a point, to avoid huge code bloat.
15694 When optimize_size, avoid any significant code bloat; calling
15695 memset is about 4 instructions, so allow for one instruction to
15696 load zero and three to do clearing. */
15697 if (TARGET_ALTIVEC && align >= 128)
15698 clear_step = 16;
15699 else if (TARGET_POWERPC64 && (align >= 64 || !STRICT_ALIGNMENT))
15700 clear_step = 8;
15701 else if (TARGET_SPE && align >= 64)
15702 clear_step = 8;
15703 else
15704 clear_step = 4;
15706 if (optimize_size && bytes > 3 * clear_step)
15707 return 0;
15708 if (! optimize_size && bytes > 8 * clear_step)
15709 return 0;
15711 for (offset = 0; bytes > 0; offset += clear_bytes, bytes -= clear_bytes)
15713 enum machine_mode mode = BLKmode;
15714 rtx dest;
15716 if (bytes >= 16 && TARGET_ALTIVEC && align >= 128)
15718 clear_bytes = 16;
15719 mode = V4SImode;
15721 else if (bytes >= 8 && TARGET_SPE && align >= 64)
15723 clear_bytes = 8;
15724 mode = V2SImode;
15726 else if (bytes >= 8 && TARGET_POWERPC64
15727 && (align >= 64 || !STRICT_ALIGNMENT))
15729 clear_bytes = 8;
15730 mode = DImode;
15731 if (offset == 0 && align < 64)
15733 rtx addr;
15735 /* If the address form is reg+offset with offset not a
15736 multiple of four, reload into reg indirect form here
15737 rather than waiting for reload. This way we get one
15738 reload, not one per store. */
15739 addr = XEXP (orig_dest, 0);
15740 if ((GET_CODE (addr) == PLUS || GET_CODE (addr) == LO_SUM)
15741 && GET_CODE (XEXP (addr, 1)) == CONST_INT
15742 && (INTVAL (XEXP (addr, 1)) & 3) != 0)
15744 addr = copy_addr_to_reg (addr);
15745 orig_dest = replace_equiv_address (orig_dest, addr);
15749 else if (bytes >= 4 && (align >= 32 || !STRICT_ALIGNMENT))
15750 { /* move 4 bytes */
15751 clear_bytes = 4;
15752 mode = SImode;
15754 else if (bytes >= 2 && (align >= 16 || !STRICT_ALIGNMENT))
15755 { /* move 2 bytes */
15756 clear_bytes = 2;
15757 mode = HImode;
15759 else /* move 1 byte at a time */
15761 clear_bytes = 1;
15762 mode = QImode;
15765 dest = adjust_address (orig_dest, mode, offset);
15767 emit_move_insn (dest, CONST0_RTX (mode));
15770 return 1;
15774 /* Expand a block move operation, and return 1 if successful. Return 0
15775 if we should let the compiler generate normal code.
15777 operands[0] is the destination
15778 operands[1] is the source
15779 operands[2] is the length
15780 operands[3] is the alignment */
15782 #define MAX_MOVE_REG 4
15785 expand_block_move (rtx operands[])
15787 rtx orig_dest = operands[0];
15788 rtx orig_src = operands[1];
15789 rtx bytes_rtx = operands[2];
15790 rtx align_rtx = operands[3];
15791 int constp = (GET_CODE (bytes_rtx) == CONST_INT);
15792 int align;
15793 int bytes;
15794 int offset;
15795 int move_bytes;
15796 rtx stores[MAX_MOVE_REG];
15797 int num_reg = 0;
15799 /* If this is not a fixed size move, just call memcpy */
15800 if (! constp)
15801 return 0;
15803 /* This must be a fixed size alignment */
15804 gcc_assert (GET_CODE (align_rtx) == CONST_INT);
15805 align = INTVAL (align_rtx) * BITS_PER_UNIT;
15807 /* Anything to move? */
15808 bytes = INTVAL (bytes_rtx);
15809 if (bytes <= 0)
15810 return 1;
15812 if (bytes > rs6000_block_move_inline_limit)
15813 return 0;
15815 for (offset = 0; bytes > 0; offset += move_bytes, bytes -= move_bytes)
15817 union {
15818 rtx (*movmemsi) (rtx, rtx, rtx, rtx);
15819 rtx (*mov) (rtx, rtx);
15820 } gen_func;
15821 enum machine_mode mode = BLKmode;
15822 rtx src, dest;
15824 /* Altivec first, since it will be faster than a string move
15825 when it applies, and usually not significantly larger. */
15826 if (TARGET_ALTIVEC && bytes >= 16 && align >= 128)
15828 move_bytes = 16;
15829 mode = V4SImode;
15830 gen_func.mov = gen_movv4si;
15832 else if (TARGET_SPE && bytes >= 8 && align >= 64)
15834 move_bytes = 8;
15835 mode = V2SImode;
15836 gen_func.mov = gen_movv2si;
15838 else if (TARGET_STRING
15839 && bytes > 24 /* move up to 32 bytes at a time */
15840 && ! fixed_regs[5]
15841 && ! fixed_regs[6]
15842 && ! fixed_regs[7]
15843 && ! fixed_regs[8]
15844 && ! fixed_regs[9]
15845 && ! fixed_regs[10]
15846 && ! fixed_regs[11]
15847 && ! fixed_regs[12])
15849 move_bytes = (bytes > 32) ? 32 : bytes;
15850 gen_func.movmemsi = gen_movmemsi_8reg;
15852 else if (TARGET_STRING
15853 && bytes > 16 /* move up to 24 bytes at a time */
15854 && ! fixed_regs[5]
15855 && ! fixed_regs[6]
15856 && ! fixed_regs[7]
15857 && ! fixed_regs[8]
15858 && ! fixed_regs[9]
15859 && ! fixed_regs[10])
15861 move_bytes = (bytes > 24) ? 24 : bytes;
15862 gen_func.movmemsi = gen_movmemsi_6reg;
15864 else if (TARGET_STRING
15865 && bytes > 8 /* move up to 16 bytes at a time */
15866 && ! fixed_regs[5]
15867 && ! fixed_regs[6]
15868 && ! fixed_regs[7]
15869 && ! fixed_regs[8])
15871 move_bytes = (bytes > 16) ? 16 : bytes;
15872 gen_func.movmemsi = gen_movmemsi_4reg;
15874 else if (bytes >= 8 && TARGET_POWERPC64
15875 && (align >= 64 || !STRICT_ALIGNMENT))
15877 move_bytes = 8;
15878 mode = DImode;
15879 gen_func.mov = gen_movdi;
15880 if (offset == 0 && align < 64)
15882 rtx addr;
15884 /* If the address form is reg+offset with offset not a
15885 multiple of four, reload into reg indirect form here
15886 rather than waiting for reload. This way we get one
15887 reload, not one per load and/or store. */
15888 addr = XEXP (orig_dest, 0);
15889 if ((GET_CODE (addr) == PLUS || GET_CODE (addr) == LO_SUM)
15890 && GET_CODE (XEXP (addr, 1)) == CONST_INT
15891 && (INTVAL (XEXP (addr, 1)) & 3) != 0)
15893 addr = copy_addr_to_reg (addr);
15894 orig_dest = replace_equiv_address (orig_dest, addr);
15896 addr = XEXP (orig_src, 0);
15897 if ((GET_CODE (addr) == PLUS || GET_CODE (addr) == LO_SUM)
15898 && GET_CODE (XEXP (addr, 1)) == CONST_INT
15899 && (INTVAL (XEXP (addr, 1)) & 3) != 0)
15901 addr = copy_addr_to_reg (addr);
15902 orig_src = replace_equiv_address (orig_src, addr);
15906 else if (TARGET_STRING && bytes > 4 && !TARGET_POWERPC64)
15907 { /* move up to 8 bytes at a time */
15908 move_bytes = (bytes > 8) ? 8 : bytes;
15909 gen_func.movmemsi = gen_movmemsi_2reg;
15911 else if (bytes >= 4 && (align >= 32 || !STRICT_ALIGNMENT))
15912 { /* move 4 bytes */
15913 move_bytes = 4;
15914 mode = SImode;
15915 gen_func.mov = gen_movsi;
15917 else if (bytes >= 2 && (align >= 16 || !STRICT_ALIGNMENT))
15918 { /* move 2 bytes */
15919 move_bytes = 2;
15920 mode = HImode;
15921 gen_func.mov = gen_movhi;
15923 else if (TARGET_STRING && bytes > 1)
15924 { /* move up to 4 bytes at a time */
15925 move_bytes = (bytes > 4) ? 4 : bytes;
15926 gen_func.movmemsi = gen_movmemsi_1reg;
15928 else /* move 1 byte at a time */
15930 move_bytes = 1;
15931 mode = QImode;
15932 gen_func.mov = gen_movqi;
15935 src = adjust_address (orig_src, mode, offset);
15936 dest = adjust_address (orig_dest, mode, offset);
15938 if (mode != BLKmode)
15940 rtx tmp_reg = gen_reg_rtx (mode);
15942 emit_insn ((*gen_func.mov) (tmp_reg, src));
15943 stores[num_reg++] = (*gen_func.mov) (dest, tmp_reg);
15946 if (mode == BLKmode || num_reg >= MAX_MOVE_REG || bytes == move_bytes)
15948 int i;
15949 for (i = 0; i < num_reg; i++)
15950 emit_insn (stores[i]);
15951 num_reg = 0;
15954 if (mode == BLKmode)
15956 /* Move the address into scratch registers. The movmemsi
15957 patterns require zero offset. */
15958 if (!REG_P (XEXP (src, 0)))
15960 rtx src_reg = copy_addr_to_reg (XEXP (src, 0));
15961 src = replace_equiv_address (src, src_reg);
15963 set_mem_size (src, move_bytes);
15965 if (!REG_P (XEXP (dest, 0)))
15967 rtx dest_reg = copy_addr_to_reg (XEXP (dest, 0));
15968 dest = replace_equiv_address (dest, dest_reg);
15970 set_mem_size (dest, move_bytes);
15972 emit_insn ((*gen_func.movmemsi) (dest, src,
15973 GEN_INT (move_bytes & 31),
15974 align_rtx));
15978 return 1;
15982 /* Return a string to perform a load_multiple operation.
15983 operands[0] is the vector.
15984 operands[1] is the source address.
15985 operands[2] is the first destination register. */
15987 const char *
15988 rs6000_output_load_multiple (rtx operands[3])
15990 /* We have to handle the case where the pseudo used to contain the address
15991 is assigned to one of the output registers. */
15992 int i, j;
15993 int words = XVECLEN (operands[0], 0);
15994 rtx xop[10];
15996 if (XVECLEN (operands[0], 0) == 1)
15997 return "lwz %2,0(%1)";
15999 for (i = 0; i < words; i++)
16000 if (refers_to_regno_p (REGNO (operands[2]) + i,
16001 REGNO (operands[2]) + i + 1, operands[1], 0))
16003 if (i == words-1)
16005 xop[0] = GEN_INT (4 * (words-1));
16006 xop[1] = operands[1];
16007 xop[2] = operands[2];
16008 output_asm_insn ("lswi %2,%1,%0\n\tlwz %1,%0(%1)", xop);
16009 return "";
16011 else if (i == 0)
16013 xop[0] = GEN_INT (4 * (words-1));
16014 xop[1] = operands[1];
16015 xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
16016 output_asm_insn ("addi %1,%1,4\n\tlswi %2,%1,%0\n\tlwz %1,-4(%1)", xop);
16017 return "";
16019 else
16021 for (j = 0; j < words; j++)
16022 if (j != i)
16024 xop[0] = GEN_INT (j * 4);
16025 xop[1] = operands[1];
16026 xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + j);
16027 output_asm_insn ("lwz %2,%0(%1)", xop);
16029 xop[0] = GEN_INT (i * 4);
16030 xop[1] = operands[1];
16031 output_asm_insn ("lwz %1,%0(%1)", xop);
16032 return "";
16036 return "lswi %2,%1,%N0";
16040 /* A validation routine: say whether CODE, a condition code, and MODE
16041 match. The other alternatives either don't make sense or should
16042 never be generated. */
16044 void
16045 validate_condition_mode (enum rtx_code code, enum machine_mode mode)
16047 gcc_assert ((GET_RTX_CLASS (code) == RTX_COMPARE
16048 || GET_RTX_CLASS (code) == RTX_COMM_COMPARE)
16049 && GET_MODE_CLASS (mode) == MODE_CC);
16051 /* These don't make sense. */
16052 gcc_assert ((code != GT && code != LT && code != GE && code != LE)
16053 || mode != CCUNSmode);
16055 gcc_assert ((code != GTU && code != LTU && code != GEU && code != LEU)
16056 || mode == CCUNSmode);
16058 gcc_assert (mode == CCFPmode
16059 || (code != ORDERED && code != UNORDERED
16060 && code != UNEQ && code != LTGT
16061 && code != UNGT && code != UNLT
16062 && code != UNGE && code != UNLE));
16064 /* These should never be generated except for
16065 flag_finite_math_only. */
16066 gcc_assert (mode != CCFPmode
16067 || flag_finite_math_only
16068 || (code != LE && code != GE
16069 && code != UNEQ && code != LTGT
16070 && code != UNGT && code != UNLT));
16072 /* These are invalid; the information is not there. */
16073 gcc_assert (mode != CCEQmode || code == EQ || code == NE);
16077 /* Return 1 if ANDOP is a mask that has no bits on that are not in the
16078 mask required to convert the result of a rotate insn into a shift
16079 left insn of SHIFTOP bits. Both are known to be SImode CONST_INT. */
16082 includes_lshift_p (rtx shiftop, rtx andop)
16084 unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
16086 shift_mask <<= INTVAL (shiftop);
16088 return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
16091 /* Similar, but for right shift. */
16094 includes_rshift_p (rtx shiftop, rtx andop)
16096 unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
16098 shift_mask >>= INTVAL (shiftop);
16100 return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
16103 /* Return 1 if ANDOP is a mask suitable for use with an rldic insn
16104 to perform a left shift. It must have exactly SHIFTOP least
16105 significant 0's, then one or more 1's, then zero or more 0's. */
16108 includes_rldic_lshift_p (rtx shiftop, rtx andop)
16110 if (GET_CODE (andop) == CONST_INT)
16112 HOST_WIDE_INT c, lsb, shift_mask;
16114 c = INTVAL (andop);
16115 if (c == 0 || c == ~0)
16116 return 0;
16118 shift_mask = ~0;
16119 shift_mask <<= INTVAL (shiftop);
16121 /* Find the least significant one bit. */
16122 lsb = c & -c;
16124 /* It must coincide with the LSB of the shift mask. */
16125 if (-lsb != shift_mask)
16126 return 0;
16128 /* Invert to look for the next transition (if any). */
16129 c = ~c;
16131 /* Remove the low group of ones (originally low group of zeros). */
16132 c &= -lsb;
16134 /* Again find the lsb, and check we have all 1's above. */
16135 lsb = c & -c;
16136 return c == -lsb;
16138 else
16139 return 0;
16142 /* Return 1 if ANDOP is a mask suitable for use with an rldicr insn
16143 to perform a left shift. It must have SHIFTOP or more least
16144 significant 0's, with the remainder of the word 1's. */
16147 includes_rldicr_lshift_p (rtx shiftop, rtx andop)
16149 if (GET_CODE (andop) == CONST_INT)
16151 HOST_WIDE_INT c, lsb, shift_mask;
16153 shift_mask = ~0;
16154 shift_mask <<= INTVAL (shiftop);
16155 c = INTVAL (andop);
16157 /* Find the least significant one bit. */
16158 lsb = c & -c;
16160 /* It must be covered by the shift mask.
16161 This test also rejects c == 0. */
16162 if ((lsb & shift_mask) == 0)
16163 return 0;
16165 /* Check we have all 1's above the transition, and reject all 1's. */
16166 return c == -lsb && lsb != 1;
16168 else
16169 return 0;
16172 /* Return 1 if operands will generate a valid arguments to rlwimi
16173 instruction for insert with right shift in 64-bit mode. The mask may
16174 not start on the first bit or stop on the last bit because wrap-around
16175 effects of instruction do not correspond to semantics of RTL insn. */
16178 insvdi_rshift_rlwimi_p (rtx sizeop, rtx startop, rtx shiftop)
16180 if (INTVAL (startop) > 32
16181 && INTVAL (startop) < 64
16182 && INTVAL (sizeop) > 1
16183 && INTVAL (sizeop) + INTVAL (startop) < 64
16184 && INTVAL (shiftop) > 0
16185 && INTVAL (sizeop) + INTVAL (shiftop) < 32
16186 && (64 - (INTVAL (shiftop) & 63)) >= INTVAL (sizeop))
16187 return 1;
16189 return 0;
16192 /* Return 1 if REGNO (reg1) == REGNO (reg2) - 1 making them candidates
16193 for lfq and stfq insns iff the registers are hard registers. */
16196 registers_ok_for_quad_peep (rtx reg1, rtx reg2)
16198 /* We might have been passed a SUBREG. */
16199 if (GET_CODE (reg1) != REG || GET_CODE (reg2) != REG)
16200 return 0;
16202 /* We might have been passed non floating point registers. */
16203 if (!FP_REGNO_P (REGNO (reg1))
16204 || !FP_REGNO_P (REGNO (reg2)))
16205 return 0;
16207 return (REGNO (reg1) == REGNO (reg2) - 1);
16210 /* Return 1 if addr1 and addr2 are suitable for lfq or stfq insn.
16211 addr1 and addr2 must be in consecutive memory locations
16212 (addr2 == addr1 + 8). */
16215 mems_ok_for_quad_peep (rtx mem1, rtx mem2)
16217 rtx addr1, addr2;
16218 unsigned int reg1, reg2;
16219 int offset1, offset2;
16221 /* The mems cannot be volatile. */
16222 if (MEM_VOLATILE_P (mem1) || MEM_VOLATILE_P (mem2))
16223 return 0;
16225 addr1 = XEXP (mem1, 0);
16226 addr2 = XEXP (mem2, 0);
16228 /* Extract an offset (if used) from the first addr. */
16229 if (GET_CODE (addr1) == PLUS)
16231 /* If not a REG, return zero. */
16232 if (GET_CODE (XEXP (addr1, 0)) != REG)
16233 return 0;
16234 else
16236 reg1 = REGNO (XEXP (addr1, 0));
16237 /* The offset must be constant! */
16238 if (GET_CODE (XEXP (addr1, 1)) != CONST_INT)
16239 return 0;
16240 offset1 = INTVAL (XEXP (addr1, 1));
16243 else if (GET_CODE (addr1) != REG)
16244 return 0;
16245 else
16247 reg1 = REGNO (addr1);
16248 /* This was a simple (mem (reg)) expression. Offset is 0. */
16249 offset1 = 0;
16252 /* And now for the second addr. */
16253 if (GET_CODE (addr2) == PLUS)
16255 /* If not a REG, return zero. */
16256 if (GET_CODE (XEXP (addr2, 0)) != REG)
16257 return 0;
16258 else
16260 reg2 = REGNO (XEXP (addr2, 0));
16261 /* The offset must be constant. */
16262 if (GET_CODE (XEXP (addr2, 1)) != CONST_INT)
16263 return 0;
16264 offset2 = INTVAL (XEXP (addr2, 1));
16267 else if (GET_CODE (addr2) != REG)
16268 return 0;
16269 else
16271 reg2 = REGNO (addr2);
16272 /* This was a simple (mem (reg)) expression. Offset is 0. */
16273 offset2 = 0;
16276 /* Both of these must have the same base register. */
16277 if (reg1 != reg2)
16278 return 0;
16280 /* The offset for the second addr must be 8 more than the first addr. */
16281 if (offset2 != offset1 + 8)
16282 return 0;
16284 /* All the tests passed. addr1 and addr2 are valid for lfq or stfq
16285 instructions. */
16286 return 1;
16291 rs6000_secondary_memory_needed_rtx (enum machine_mode mode)
16293 static bool eliminated = false;
16294 rtx ret;
16296 if (mode != SDmode || TARGET_NO_SDMODE_STACK)
16297 ret = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
16298 else
16300 rtx mem = cfun->machine->sdmode_stack_slot;
16301 gcc_assert (mem != NULL_RTX);
16303 if (!eliminated)
16305 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
16306 cfun->machine->sdmode_stack_slot = mem;
16307 eliminated = true;
16309 ret = mem;
16312 if (TARGET_DEBUG_ADDR)
16314 fprintf (stderr, "\nrs6000_secondary_memory_needed_rtx, mode %s, rtx:\n",
16315 GET_MODE_NAME (mode));
16316 if (!ret)
16317 fprintf (stderr, "\tNULL_RTX\n");
16318 else
16319 debug_rtx (ret);
16322 return ret;
16325 /* Return the mode to be used for memory when a secondary memory
16326 location is needed. For SDmode values we need to use DDmode, in
16327 all other cases we can use the same mode. */
16328 enum machine_mode
16329 rs6000_secondary_memory_needed_mode (enum machine_mode mode)
16331 if (lra_in_progress && mode == SDmode)
16332 return DDmode;
16333 return mode;
16336 static tree
16337 rs6000_check_sdmode (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16339 /* Don't walk into types. */
16340 if (*tp == NULL_TREE || *tp == error_mark_node || TYPE_P (*tp))
16342 *walk_subtrees = 0;
16343 return NULL_TREE;
16346 switch (TREE_CODE (*tp))
16348 case VAR_DECL:
16349 case PARM_DECL:
16350 case FIELD_DECL:
16351 case RESULT_DECL:
16352 case SSA_NAME:
16353 case REAL_CST:
16354 case MEM_REF:
16355 case VIEW_CONVERT_EXPR:
16356 if (TYPE_MODE (TREE_TYPE (*tp)) == SDmode)
16357 return *tp;
16358 break;
16359 default:
16360 break;
16363 return NULL_TREE;
16366 /* Classify a register type. Because the FMRGOW/FMRGEW instructions only work
16367 on traditional floating point registers, and the VMRGOW/VMRGEW instructions
16368 only work on the traditional altivec registers, note if an altivec register
16369 was chosen. */
16371 static enum rs6000_reg_type
16372 register_to_reg_type (rtx reg, bool *is_altivec)
16374 HOST_WIDE_INT regno;
16375 enum reg_class rclass;
16377 if (GET_CODE (reg) == SUBREG)
16378 reg = SUBREG_REG (reg);
16380 if (!REG_P (reg))
16381 return NO_REG_TYPE;
16383 regno = REGNO (reg);
16384 if (regno >= FIRST_PSEUDO_REGISTER)
16386 if (!lra_in_progress && !reload_in_progress && !reload_completed)
16387 return PSEUDO_REG_TYPE;
16389 regno = true_regnum (reg);
16390 if (regno < 0 || regno >= FIRST_PSEUDO_REGISTER)
16391 return PSEUDO_REG_TYPE;
16394 gcc_assert (regno >= 0);
16396 if (is_altivec && ALTIVEC_REGNO_P (regno))
16397 *is_altivec = true;
16399 rclass = rs6000_regno_regclass[regno];
16400 return reg_class_to_reg_type[(int)rclass];
16403 /* Helper function for rs6000_secondary_reload to return true if a move to a
16404 different register classe is really a simple move. */
16406 static bool
16407 rs6000_secondary_reload_simple_move (enum rs6000_reg_type to_type,
16408 enum rs6000_reg_type from_type,
16409 enum machine_mode mode)
16411 int size;
16413 /* Add support for various direct moves available. In this function, we only
16414 look at cases where we don't need any extra registers, and one or more
16415 simple move insns are issued. At present, 32-bit integers are not allowed
16416 in FPR/VSX registers. Single precision binary floating is not a simple
16417 move because we need to convert to the single precision memory layout.
16418 The 4-byte SDmode can be moved. */
16419 size = GET_MODE_SIZE (mode);
16420 if (TARGET_DIRECT_MOVE
16421 && ((mode == SDmode) || (TARGET_POWERPC64 && size == 8))
16422 && ((to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
16423 || (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)))
16424 return true;
16426 else if (TARGET_MFPGPR && TARGET_POWERPC64 && size == 8
16427 && ((to_type == GPR_REG_TYPE && from_type == FPR_REG_TYPE)
16428 || (to_type == FPR_REG_TYPE && from_type == GPR_REG_TYPE)))
16429 return true;
16431 else if ((size == 4 || (TARGET_POWERPC64 && size == 8))
16432 && ((to_type == GPR_REG_TYPE && from_type == SPR_REG_TYPE)
16433 || (to_type == SPR_REG_TYPE && from_type == GPR_REG_TYPE)))
16434 return true;
16436 return false;
16439 /* Power8 helper function for rs6000_secondary_reload, handle all of the
16440 special direct moves that involve allocating an extra register, return the
16441 insn code of the helper function if there is such a function or
16442 CODE_FOR_nothing if not. */
16444 static bool
16445 rs6000_secondary_reload_direct_move (enum rs6000_reg_type to_type,
16446 enum rs6000_reg_type from_type,
16447 enum machine_mode mode,
16448 secondary_reload_info *sri,
16449 bool altivec_p)
16451 bool ret = false;
16452 enum insn_code icode = CODE_FOR_nothing;
16453 int cost = 0;
16454 int size = GET_MODE_SIZE (mode);
16456 if (TARGET_POWERPC64)
16458 if (size == 16)
16460 /* Handle moving 128-bit values from GPRs to VSX point registers on
16461 power8 when running in 64-bit mode using XXPERMDI to glue the two
16462 64-bit values back together. */
16463 if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)
16465 cost = 3; /* 2 mtvsrd's, 1 xxpermdi. */
16466 icode = reg_addr[mode].reload_vsx_gpr;
16469 /* Handle moving 128-bit values from VSX point registers to GPRs on
16470 power8 when running in 64-bit mode using XXPERMDI to get access to the
16471 bottom 64-bit value. */
16472 else if (to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
16474 cost = 3; /* 2 mfvsrd's, 1 xxpermdi. */
16475 icode = reg_addr[mode].reload_gpr_vsx;
16479 else if (mode == SFmode)
16481 if (to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
16483 cost = 3; /* xscvdpspn, mfvsrd, and. */
16484 icode = reg_addr[mode].reload_gpr_vsx;
16487 else if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)
16489 cost = 2; /* mtvsrz, xscvspdpn. */
16490 icode = reg_addr[mode].reload_vsx_gpr;
16495 if (TARGET_POWERPC64 && size == 16)
16497 /* Handle moving 128-bit values from GPRs to VSX point registers on
16498 power8 when running in 64-bit mode using XXPERMDI to glue the two
16499 64-bit values back together. */
16500 if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)
16502 cost = 3; /* 2 mtvsrd's, 1 xxpermdi. */
16503 icode = reg_addr[mode].reload_vsx_gpr;
16506 /* Handle moving 128-bit values from VSX point registers to GPRs on
16507 power8 when running in 64-bit mode using XXPERMDI to get access to the
16508 bottom 64-bit value. */
16509 else if (to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
16511 cost = 3; /* 2 mfvsrd's, 1 xxpermdi. */
16512 icode = reg_addr[mode].reload_gpr_vsx;
16516 else if (!TARGET_POWERPC64 && size == 8)
16518 /* Handle moving 64-bit values from GPRs to floating point registers on
16519 power8 when running in 32-bit mode using FMRGOW to glue the two 32-bit
16520 values back together. Altivec register classes must be handled
16521 specially since a different instruction is used, and the secondary
16522 reload support requires a single instruction class in the scratch
16523 register constraint. However, right now TFmode is not allowed in
16524 Altivec registers, so the pattern will never match. */
16525 if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE && !altivec_p)
16527 cost = 3; /* 2 mtvsrwz's, 1 fmrgow. */
16528 icode = reg_addr[mode].reload_fpr_gpr;
16532 if (icode != CODE_FOR_nothing)
16534 ret = true;
16535 if (sri)
16537 sri->icode = icode;
16538 sri->extra_cost = cost;
16542 return ret;
16545 /* Return whether a move between two register classes can be done either
16546 directly (simple move) or via a pattern that uses a single extra temporary
16547 (using power8's direct move in this case. */
16549 static bool
16550 rs6000_secondary_reload_move (enum rs6000_reg_type to_type,
16551 enum rs6000_reg_type from_type,
16552 enum machine_mode mode,
16553 secondary_reload_info *sri,
16554 bool altivec_p)
16556 /* Fall back to load/store reloads if either type is not a register. */
16557 if (to_type == NO_REG_TYPE || from_type == NO_REG_TYPE)
16558 return false;
16560 /* If we haven't allocated registers yet, assume the move can be done for the
16561 standard register types. */
16562 if ((to_type == PSEUDO_REG_TYPE && from_type == PSEUDO_REG_TYPE)
16563 || (to_type == PSEUDO_REG_TYPE && IS_STD_REG_TYPE (from_type))
16564 || (from_type == PSEUDO_REG_TYPE && IS_STD_REG_TYPE (to_type)))
16565 return true;
16567 /* Moves to the same set of registers is a simple move for non-specialized
16568 registers. */
16569 if (to_type == from_type && IS_STD_REG_TYPE (to_type))
16570 return true;
16572 /* Check whether a simple move can be done directly. */
16573 if (rs6000_secondary_reload_simple_move (to_type, from_type, mode))
16575 if (sri)
16577 sri->icode = CODE_FOR_nothing;
16578 sri->extra_cost = 0;
16580 return true;
16583 /* Now check if we can do it in a few steps. */
16584 return rs6000_secondary_reload_direct_move (to_type, from_type, mode, sri,
16585 altivec_p);
16588 /* Inform reload about cases where moving X with a mode MODE to a register in
16589 RCLASS requires an extra scratch or immediate register. Return the class
16590 needed for the immediate register.
16592 For VSX and Altivec, we may need a register to convert sp+offset into
16593 reg+sp.
16595 For misaligned 64-bit gpr loads and stores we need a register to
16596 convert an offset address to indirect. */
16598 static reg_class_t
16599 rs6000_secondary_reload (bool in_p,
16600 rtx x,
16601 reg_class_t rclass_i,
16602 enum machine_mode mode,
16603 secondary_reload_info *sri)
16605 enum reg_class rclass = (enum reg_class) rclass_i;
16606 reg_class_t ret = ALL_REGS;
16607 enum insn_code icode;
16608 bool default_p = false;
16610 sri->icode = CODE_FOR_nothing;
16611 icode = ((in_p)
16612 ? reg_addr[mode].reload_load
16613 : reg_addr[mode].reload_store);
16615 if (REG_P (x) || register_operand (x, mode))
16617 enum rs6000_reg_type to_type = reg_class_to_reg_type[(int)rclass];
16618 bool altivec_p = (rclass == ALTIVEC_REGS);
16619 enum rs6000_reg_type from_type = register_to_reg_type (x, &altivec_p);
16621 if (!in_p)
16623 enum rs6000_reg_type exchange = to_type;
16624 to_type = from_type;
16625 from_type = exchange;
16628 /* Can we do a direct move of some sort? */
16629 if (rs6000_secondary_reload_move (to_type, from_type, mode, sri,
16630 altivec_p))
16632 icode = (enum insn_code)sri->icode;
16633 default_p = false;
16634 ret = NO_REGS;
16638 /* Handle vector moves with reload helper functions. */
16639 if (ret == ALL_REGS && icode != CODE_FOR_nothing)
16641 ret = NO_REGS;
16642 sri->icode = CODE_FOR_nothing;
16643 sri->extra_cost = 0;
16645 if (GET_CODE (x) == MEM)
16647 rtx addr = XEXP (x, 0);
16649 /* Loads to and stores from gprs can do reg+offset, and wouldn't need
16650 an extra register in that case, but it would need an extra
16651 register if the addressing is reg+reg or (reg+reg)&(-16). Special
16652 case load/store quad. */
16653 if (rclass == GENERAL_REGS || rclass == BASE_REGS)
16655 if (TARGET_POWERPC64 && TARGET_QUAD_MEMORY
16656 && GET_MODE_SIZE (mode) == 16
16657 && quad_memory_operand (x, mode))
16659 sri->icode = icode;
16660 sri->extra_cost = 2;
16663 else if (!legitimate_indirect_address_p (addr, false)
16664 && !rs6000_legitimate_offset_address_p (PTImode, addr,
16665 false, true))
16667 sri->icode = icode;
16668 /* account for splitting the loads, and converting the
16669 address from reg+reg to reg. */
16670 sri->extra_cost = (((TARGET_64BIT) ? 3 : 5)
16671 + ((GET_CODE (addr) == AND) ? 1 : 0));
16674 /* Allow scalar loads to/from the traditional floating point
16675 registers, even if VSX memory is set. */
16676 else if ((rclass == FLOAT_REGS || rclass == NO_REGS)
16677 && (GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8)
16678 && (legitimate_indirect_address_p (addr, false)
16679 || legitimate_indirect_address_p (addr, false)
16680 || rs6000_legitimate_offset_address_p (mode, addr,
16681 false, true)))
16684 /* Loads to and stores from vector registers can only do reg+reg
16685 addressing. Altivec registers can also do (reg+reg)&(-16). Allow
16686 scalar modes loading up the traditional floating point registers
16687 to use offset addresses. */
16688 else if (rclass == VSX_REGS || rclass == ALTIVEC_REGS
16689 || rclass == FLOAT_REGS || rclass == NO_REGS)
16691 if (!VECTOR_MEM_ALTIVEC_P (mode)
16692 && GET_CODE (addr) == AND
16693 && GET_CODE (XEXP (addr, 1)) == CONST_INT
16694 && INTVAL (XEXP (addr, 1)) == -16
16695 && (legitimate_indirect_address_p (XEXP (addr, 0), false)
16696 || legitimate_indexed_address_p (XEXP (addr, 0), false)))
16698 sri->icode = icode;
16699 sri->extra_cost = ((GET_CODE (XEXP (addr, 0)) == PLUS)
16700 ? 2 : 1);
16702 else if (!legitimate_indirect_address_p (addr, false)
16703 && (rclass == NO_REGS
16704 || !legitimate_indexed_address_p (addr, false)))
16706 sri->icode = icode;
16707 sri->extra_cost = 1;
16709 else
16710 icode = CODE_FOR_nothing;
16712 /* Any other loads, including to pseudo registers which haven't been
16713 assigned to a register yet, default to require a scratch
16714 register. */
16715 else
16717 sri->icode = icode;
16718 sri->extra_cost = 2;
16721 else if (REG_P (x))
16723 int regno = true_regnum (x);
16725 icode = CODE_FOR_nothing;
16726 if (regno < 0 || regno >= FIRST_PSEUDO_REGISTER)
16727 default_p = true;
16728 else
16730 enum reg_class xclass = REGNO_REG_CLASS (regno);
16731 enum rs6000_reg_type rtype1 = reg_class_to_reg_type[(int)rclass];
16732 enum rs6000_reg_type rtype2 = reg_class_to_reg_type[(int)xclass];
16734 /* If memory is needed, use default_secondary_reload to create the
16735 stack slot. */
16736 if (rtype1 != rtype2 || !IS_STD_REG_TYPE (rtype1))
16737 default_p = true;
16738 else
16739 ret = NO_REGS;
16742 else
16743 default_p = true;
16745 else if (TARGET_POWERPC64
16746 && reg_class_to_reg_type[(int)rclass] == GPR_REG_TYPE
16747 && MEM_P (x)
16748 && GET_MODE_SIZE (GET_MODE (x)) >= UNITS_PER_WORD)
16750 rtx addr = XEXP (x, 0);
16751 rtx off = address_offset (addr);
16753 if (off != NULL_RTX)
16755 unsigned int extra = GET_MODE_SIZE (GET_MODE (x)) - UNITS_PER_WORD;
16756 unsigned HOST_WIDE_INT offset = INTVAL (off);
16758 /* We need a secondary reload when our legitimate_address_p
16759 says the address is good (as otherwise the entire address
16760 will be reloaded), and the offset is not a multiple of
16761 four or we have an address wrap. Address wrap will only
16762 occur for LO_SUMs since legitimate_offset_address_p
16763 rejects addresses for 16-byte mems that will wrap. */
16764 if (GET_CODE (addr) == LO_SUM
16765 ? (1 /* legitimate_address_p allows any offset for lo_sum */
16766 && ((offset & 3) != 0
16767 || ((offset & 0xffff) ^ 0x8000) >= 0x10000 - extra))
16768 : (offset + 0x8000 < 0x10000 - extra /* legitimate_address_p */
16769 && (offset & 3) != 0))
16771 if (in_p)
16772 sri->icode = CODE_FOR_reload_di_load;
16773 else
16774 sri->icode = CODE_FOR_reload_di_store;
16775 sri->extra_cost = 2;
16776 ret = NO_REGS;
16778 else
16779 default_p = true;
16781 else
16782 default_p = true;
16784 else if (!TARGET_POWERPC64
16785 && reg_class_to_reg_type[(int)rclass] == GPR_REG_TYPE
16786 && MEM_P (x)
16787 && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
16789 rtx addr = XEXP (x, 0);
16790 rtx off = address_offset (addr);
16792 if (off != NULL_RTX)
16794 unsigned int extra = GET_MODE_SIZE (GET_MODE (x)) - UNITS_PER_WORD;
16795 unsigned HOST_WIDE_INT offset = INTVAL (off);
16797 /* We need a secondary reload when our legitimate_address_p
16798 says the address is good (as otherwise the entire address
16799 will be reloaded), and we have a wrap.
16801 legitimate_lo_sum_address_p allows LO_SUM addresses to
16802 have any offset so test for wrap in the low 16 bits.
16804 legitimate_offset_address_p checks for the range
16805 [-0x8000,0x7fff] for mode size of 8 and [-0x8000,0x7ff7]
16806 for mode size of 16. We wrap at [0x7ffc,0x7fff] and
16807 [0x7ff4,0x7fff] respectively, so test for the
16808 intersection of these ranges, [0x7ffc,0x7fff] and
16809 [0x7ff4,0x7ff7] respectively.
16811 Note that the address we see here may have been
16812 manipulated by legitimize_reload_address. */
16813 if (GET_CODE (addr) == LO_SUM
16814 ? ((offset & 0xffff) ^ 0x8000) >= 0x10000 - extra
16815 : offset - (0x8000 - extra) < UNITS_PER_WORD)
16817 if (in_p)
16818 sri->icode = CODE_FOR_reload_si_load;
16819 else
16820 sri->icode = CODE_FOR_reload_si_store;
16821 sri->extra_cost = 2;
16822 ret = NO_REGS;
16824 else
16825 default_p = true;
16827 else
16828 default_p = true;
16830 else
16831 default_p = true;
16833 if (default_p)
16834 ret = default_secondary_reload (in_p, x, rclass, mode, sri);
16836 gcc_assert (ret != ALL_REGS);
16838 if (TARGET_DEBUG_ADDR)
16840 fprintf (stderr,
16841 "\nrs6000_secondary_reload, return %s, in_p = %s, rclass = %s, "
16842 "mode = %s",
16843 reg_class_names[ret],
16844 in_p ? "true" : "false",
16845 reg_class_names[rclass],
16846 GET_MODE_NAME (mode));
16848 if (default_p)
16849 fprintf (stderr, ", default secondary reload");
16851 if (sri->icode != CODE_FOR_nothing)
16852 fprintf (stderr, ", reload func = %s, extra cost = %d\n",
16853 insn_data[sri->icode].name, sri->extra_cost);
16854 else
16855 fprintf (stderr, "\n");
16857 debug_rtx (x);
16860 return ret;
16863 /* Better tracing for rs6000_secondary_reload_inner. */
16865 static void
16866 rs6000_secondary_reload_trace (int line, rtx reg, rtx mem, rtx scratch,
16867 bool store_p)
16869 rtx set, clobber;
16871 gcc_assert (reg != NULL_RTX && mem != NULL_RTX && scratch != NULL_RTX);
16873 fprintf (stderr, "rs6000_secondary_reload_inner:%d, type = %s\n", line,
16874 store_p ? "store" : "load");
16876 if (store_p)
16877 set = gen_rtx_SET (VOIDmode, mem, reg);
16878 else
16879 set = gen_rtx_SET (VOIDmode, reg, mem);
16881 clobber = gen_rtx_CLOBBER (VOIDmode, scratch);
16882 debug_rtx (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set, clobber)));
16885 static void
16886 rs6000_secondary_reload_fail (int line, rtx reg, rtx mem, rtx scratch,
16887 bool store_p)
16889 rs6000_secondary_reload_trace (line, reg, mem, scratch, store_p);
16890 gcc_unreachable ();
16893 /* Fixup reload addresses for Altivec or VSX loads/stores to change SP+offset
16894 to SP+reg addressing. */
16896 void
16897 rs6000_secondary_reload_inner (rtx reg, rtx mem, rtx scratch, bool store_p)
16899 int regno = true_regnum (reg);
16900 enum machine_mode mode = GET_MODE (reg);
16901 enum reg_class rclass;
16902 rtx addr;
16903 rtx and_op2 = NULL_RTX;
16904 rtx addr_op1;
16905 rtx addr_op2;
16906 rtx scratch_or_premodify = scratch;
16907 rtx and_rtx;
16908 rtx cc_clobber;
16910 if (TARGET_DEBUG_ADDR)
16911 rs6000_secondary_reload_trace (__LINE__, reg, mem, scratch, store_p);
16913 if (regno < 0 || regno >= FIRST_PSEUDO_REGISTER)
16914 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
16916 if (GET_CODE (mem) != MEM)
16917 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
16919 rclass = REGNO_REG_CLASS (regno);
16920 addr = find_replacement (&XEXP (mem, 0));
16922 switch (rclass)
16924 /* GPRs can handle reg + small constant, all other addresses need to use
16925 the scratch register. */
16926 case GENERAL_REGS:
16927 case BASE_REGS:
16928 if (GET_CODE (addr) == AND)
16930 and_op2 = XEXP (addr, 1);
16931 addr = find_replacement (&XEXP (addr, 0));
16934 if (GET_CODE (addr) == PRE_MODIFY)
16936 scratch_or_premodify = find_replacement (&XEXP (addr, 0));
16937 if (!REG_P (scratch_or_premodify))
16938 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
16940 addr = find_replacement (&XEXP (addr, 1));
16941 if (GET_CODE (addr) != PLUS)
16942 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
16945 if (GET_CODE (addr) == PLUS
16946 && (and_op2 != NULL_RTX
16947 || !rs6000_legitimate_offset_address_p (PTImode, addr,
16948 false, true)))
16950 /* find_replacement already recurses into both operands of
16951 PLUS so we don't need to call it here. */
16952 addr_op1 = XEXP (addr, 0);
16953 addr_op2 = XEXP (addr, 1);
16954 if (!legitimate_indirect_address_p (addr_op1, false))
16955 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
16957 if (!REG_P (addr_op2)
16958 && (GET_CODE (addr_op2) != CONST_INT
16959 || !satisfies_constraint_I (addr_op2)))
16961 if (TARGET_DEBUG_ADDR)
16963 fprintf (stderr,
16964 "\nMove plus addr to register %s, mode = %s: ",
16965 rs6000_reg_names[REGNO (scratch)],
16966 GET_MODE_NAME (mode));
16967 debug_rtx (addr_op2);
16969 rs6000_emit_move (scratch, addr_op2, Pmode);
16970 addr_op2 = scratch;
16973 emit_insn (gen_rtx_SET (VOIDmode,
16974 scratch_or_premodify,
16975 gen_rtx_PLUS (Pmode,
16976 addr_op1,
16977 addr_op2)));
16979 addr = scratch_or_premodify;
16980 scratch_or_premodify = scratch;
16982 else if (!legitimate_indirect_address_p (addr, false)
16983 && !rs6000_legitimate_offset_address_p (PTImode, addr,
16984 false, true))
16986 if (TARGET_DEBUG_ADDR)
16988 fprintf (stderr, "\nMove addr to register %s, mode = %s: ",
16989 rs6000_reg_names[REGNO (scratch_or_premodify)],
16990 GET_MODE_NAME (mode));
16991 debug_rtx (addr);
16993 rs6000_emit_move (scratch_or_premodify, addr, Pmode);
16994 addr = scratch_or_premodify;
16995 scratch_or_premodify = scratch;
16997 break;
16999 /* Float registers can do offset+reg addressing for scalar types. */
17000 case FLOAT_REGS:
17001 if (legitimate_indirect_address_p (addr, false) /* reg */
17002 || legitimate_indexed_address_p (addr, false) /* reg+reg */
17003 || ((GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8)
17004 && and_op2 == NULL_RTX
17005 && scratch_or_premodify == scratch
17006 && rs6000_legitimate_offset_address_p (mode, addr, false, false)))
17007 break;
17009 /* If this isn't a legacy floating point load/store, fall through to the
17010 VSX defaults. */
17012 /* VSX/Altivec registers can only handle reg+reg addressing. Move other
17013 addresses into a scratch register. */
17014 case VSX_REGS:
17015 case ALTIVEC_REGS:
17017 /* With float regs, we need to handle the AND ourselves, since we can't
17018 use the Altivec instruction with an implicit AND -16. Allow scalar
17019 loads to float registers to use reg+offset even if VSX. */
17020 if (GET_CODE (addr) == AND
17021 && (rclass != ALTIVEC_REGS || GET_MODE_SIZE (mode) != 16
17022 || GET_CODE (XEXP (addr, 1)) != CONST_INT
17023 || INTVAL (XEXP (addr, 1)) != -16
17024 || !VECTOR_MEM_ALTIVEC_P (mode)))
17026 and_op2 = XEXP (addr, 1);
17027 addr = find_replacement (&XEXP (addr, 0));
17030 /* If we aren't using a VSX load, save the PRE_MODIFY register and use it
17031 as the address later. */
17032 if (GET_CODE (addr) == PRE_MODIFY
17033 && ((ALTIVEC_OR_VSX_VECTOR_MODE (mode)
17034 && (rclass != FLOAT_REGS
17035 || (GET_MODE_SIZE (mode) != 4 && GET_MODE_SIZE (mode) != 8)))
17036 || and_op2 != NULL_RTX
17037 || !legitimate_indexed_address_p (XEXP (addr, 1), false)))
17039 scratch_or_premodify = find_replacement (&XEXP (addr, 0));
17040 if (!legitimate_indirect_address_p (scratch_or_premodify, false))
17041 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
17043 addr = find_replacement (&XEXP (addr, 1));
17044 if (GET_CODE (addr) != PLUS)
17045 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
17048 if (legitimate_indirect_address_p (addr, false) /* reg */
17049 || legitimate_indexed_address_p (addr, false) /* reg+reg */
17050 || (GET_CODE (addr) == AND /* Altivec memory */
17051 && rclass == ALTIVEC_REGS
17052 && GET_CODE (XEXP (addr, 1)) == CONST_INT
17053 && INTVAL (XEXP (addr, 1)) == -16
17054 && (legitimate_indirect_address_p (XEXP (addr, 0), false)
17055 || legitimate_indexed_address_p (XEXP (addr, 0), false))))
17058 else if (GET_CODE (addr) == PLUS)
17060 addr_op1 = XEXP (addr, 0);
17061 addr_op2 = XEXP (addr, 1);
17062 if (!REG_P (addr_op1))
17063 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
17065 if (TARGET_DEBUG_ADDR)
17067 fprintf (stderr, "\nMove plus addr to register %s, mode = %s: ",
17068 rs6000_reg_names[REGNO (scratch)], GET_MODE_NAME (mode));
17069 debug_rtx (addr_op2);
17071 rs6000_emit_move (scratch, addr_op2, Pmode);
17072 emit_insn (gen_rtx_SET (VOIDmode,
17073 scratch_or_premodify,
17074 gen_rtx_PLUS (Pmode,
17075 addr_op1,
17076 scratch)));
17077 addr = scratch_or_premodify;
17078 scratch_or_premodify = scratch;
17081 else if (GET_CODE (addr) == SYMBOL_REF || GET_CODE (addr) == CONST
17082 || GET_CODE (addr) == CONST_INT || GET_CODE (addr) == LO_SUM
17083 || REG_P (addr))
17085 if (TARGET_DEBUG_ADDR)
17087 fprintf (stderr, "\nMove addr to register %s, mode = %s: ",
17088 rs6000_reg_names[REGNO (scratch_or_premodify)],
17089 GET_MODE_NAME (mode));
17090 debug_rtx (addr);
17093 rs6000_emit_move (scratch_or_premodify, addr, Pmode);
17094 addr = scratch_or_premodify;
17095 scratch_or_premodify = scratch;
17098 else
17099 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
17101 break;
17103 default:
17104 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
17107 /* If the original address involved a pre-modify that we couldn't use the VSX
17108 memory instruction with update, and we haven't taken care of already,
17109 store the address in the pre-modify register and use that as the
17110 address. */
17111 if (scratch_or_premodify != scratch && scratch_or_premodify != addr)
17113 emit_insn (gen_rtx_SET (VOIDmode, scratch_or_premodify, addr));
17114 addr = scratch_or_premodify;
17117 /* If the original address involved an AND -16 and we couldn't use an ALTIVEC
17118 memory instruction, recreate the AND now, including the clobber which is
17119 generated by the general ANDSI3/ANDDI3 patterns for the
17120 andi. instruction. */
17121 if (and_op2 != NULL_RTX)
17123 if (! legitimate_indirect_address_p (addr, false))
17125 emit_insn (gen_rtx_SET (VOIDmode, scratch, addr));
17126 addr = scratch;
17129 if (TARGET_DEBUG_ADDR)
17131 fprintf (stderr, "\nAnd addr to register %s, mode = %s: ",
17132 rs6000_reg_names[REGNO (scratch)], GET_MODE_NAME (mode));
17133 debug_rtx (and_op2);
17136 and_rtx = gen_rtx_SET (VOIDmode,
17137 scratch,
17138 gen_rtx_AND (Pmode,
17139 addr,
17140 and_op2));
17142 cc_clobber = gen_rtx_CLOBBER (CCmode, gen_rtx_SCRATCH (CCmode));
17143 emit_insn (gen_rtx_PARALLEL (VOIDmode,
17144 gen_rtvec (2, and_rtx, cc_clobber)));
17145 addr = scratch;
17148 /* Adjust the address if it changed. */
17149 if (addr != XEXP (mem, 0))
17151 mem = replace_equiv_address_nv (mem, addr);
17152 if (TARGET_DEBUG_ADDR)
17153 fprintf (stderr, "\nrs6000_secondary_reload_inner, mem adjusted.\n");
17156 /* Now create the move. */
17157 if (store_p)
17158 emit_insn (gen_rtx_SET (VOIDmode, mem, reg));
17159 else
17160 emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
17162 return;
17165 /* Convert reloads involving 64-bit gprs and misaligned offset
17166 addressing, or multiple 32-bit gprs and offsets that are too large,
17167 to use indirect addressing. */
17169 void
17170 rs6000_secondary_reload_gpr (rtx reg, rtx mem, rtx scratch, bool store_p)
17172 int regno = true_regnum (reg);
17173 enum reg_class rclass;
17174 rtx addr;
17175 rtx scratch_or_premodify = scratch;
17177 if (TARGET_DEBUG_ADDR)
17179 fprintf (stderr, "\nrs6000_secondary_reload_gpr, type = %s\n",
17180 store_p ? "store" : "load");
17181 fprintf (stderr, "reg:\n");
17182 debug_rtx (reg);
17183 fprintf (stderr, "mem:\n");
17184 debug_rtx (mem);
17185 fprintf (stderr, "scratch:\n");
17186 debug_rtx (scratch);
17189 gcc_assert (regno >= 0 && regno < FIRST_PSEUDO_REGISTER);
17190 gcc_assert (GET_CODE (mem) == MEM);
17191 rclass = REGNO_REG_CLASS (regno);
17192 gcc_assert (rclass == GENERAL_REGS || rclass == BASE_REGS);
17193 addr = XEXP (mem, 0);
17195 if (GET_CODE (addr) == PRE_MODIFY)
17197 scratch_or_premodify = XEXP (addr, 0);
17198 gcc_assert (REG_P (scratch_or_premodify));
17199 addr = XEXP (addr, 1);
17201 gcc_assert (GET_CODE (addr) == PLUS || GET_CODE (addr) == LO_SUM);
17203 rs6000_emit_move (scratch_or_premodify, addr, Pmode);
17205 mem = replace_equiv_address_nv (mem, scratch_or_premodify);
17207 /* Now create the move. */
17208 if (store_p)
17209 emit_insn (gen_rtx_SET (VOIDmode, mem, reg));
17210 else
17211 emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
17213 return;
17216 /* Allocate a 64-bit stack slot to be used for copying SDmode values through if
17217 this function has any SDmode references. If we are on a power7 or later, we
17218 don't need the 64-bit stack slot since the LFIWZX and STIFWX instructions
17219 can load/store the value. */
17221 static void
17222 rs6000_alloc_sdmode_stack_slot (void)
17224 tree t;
17225 basic_block bb;
17226 gimple_stmt_iterator gsi;
17228 gcc_assert (cfun->machine->sdmode_stack_slot == NULL_RTX);
17229 /* We use a different approach for dealing with the secondary
17230 memory in LRA. */
17231 if (ira_use_lra_p)
17232 return;
17234 if (TARGET_NO_SDMODE_STACK)
17235 return;
17237 FOR_EACH_BB_FN (bb, cfun)
17238 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
17240 tree ret = walk_gimple_op (gsi_stmt (gsi), rs6000_check_sdmode, NULL);
17241 if (ret)
17243 rtx stack = assign_stack_local (DDmode, GET_MODE_SIZE (DDmode), 0);
17244 cfun->machine->sdmode_stack_slot = adjust_address_nv (stack,
17245 SDmode, 0);
17246 return;
17250 /* Check for any SDmode parameters of the function. */
17251 for (t = DECL_ARGUMENTS (cfun->decl); t; t = DECL_CHAIN (t))
17253 if (TREE_TYPE (t) == error_mark_node)
17254 continue;
17256 if (TYPE_MODE (TREE_TYPE (t)) == SDmode
17257 || TYPE_MODE (DECL_ARG_TYPE (t)) == SDmode)
17259 rtx stack = assign_stack_local (DDmode, GET_MODE_SIZE (DDmode), 0);
17260 cfun->machine->sdmode_stack_slot = adjust_address_nv (stack,
17261 SDmode, 0);
17262 return;
17267 static void
17268 rs6000_instantiate_decls (void)
17270 if (cfun->machine->sdmode_stack_slot != NULL_RTX)
17271 instantiate_decl_rtl (cfun->machine->sdmode_stack_slot);
17274 /* Given an rtx X being reloaded into a reg required to be
17275 in class CLASS, return the class of reg to actually use.
17276 In general this is just CLASS; but on some machines
17277 in some cases it is preferable to use a more restrictive class.
17279 On the RS/6000, we have to return NO_REGS when we want to reload a
17280 floating-point CONST_DOUBLE to force it to be copied to memory.
17282 We also don't want to reload integer values into floating-point
17283 registers if we can at all help it. In fact, this can
17284 cause reload to die, if it tries to generate a reload of CTR
17285 into a FP register and discovers it doesn't have the memory location
17286 required.
17288 ??? Would it be a good idea to have reload do the converse, that is
17289 try to reload floating modes into FP registers if possible?
17292 static enum reg_class
17293 rs6000_preferred_reload_class (rtx x, enum reg_class rclass)
17295 enum machine_mode mode = GET_MODE (x);
17297 if (TARGET_VSX && x == CONST0_RTX (mode) && VSX_REG_CLASS_P (rclass))
17298 return rclass;
17300 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (mode)
17301 && (rclass == ALTIVEC_REGS || rclass == VSX_REGS)
17302 && easy_vector_constant (x, mode))
17303 return ALTIVEC_REGS;
17305 if ((CONSTANT_P (x) || GET_CODE (x) == PLUS))
17307 if (reg_class_subset_p (GENERAL_REGS, rclass))
17308 return GENERAL_REGS;
17309 if (reg_class_subset_p (BASE_REGS, rclass))
17310 return BASE_REGS;
17311 return NO_REGS;
17314 if (GET_MODE_CLASS (mode) == MODE_INT && rclass == NON_SPECIAL_REGS)
17315 return GENERAL_REGS;
17317 /* For VSX, prefer the traditional registers for 64-bit values because we can
17318 use the non-VSX loads. Prefer the Altivec registers if Altivec is
17319 handling the vector operations (i.e. V16QI, V8HI, and V4SI), or if we
17320 prefer Altivec loads.. */
17321 if (rclass == VSX_REGS)
17323 if (MEM_P (x) && reg_addr[mode].scalar_in_vmx_p)
17325 rtx addr = XEXP (x, 0);
17326 if (rs6000_legitimate_offset_address_p (mode, addr, false, true)
17327 || legitimate_lo_sum_address_p (mode, addr, false))
17328 return FLOAT_REGS;
17330 else if (GET_MODE_SIZE (mode) <= 8 && !reg_addr[mode].scalar_in_vmx_p)
17331 return FLOAT_REGS;
17333 if (VECTOR_UNIT_ALTIVEC_P (mode) || VECTOR_MEM_ALTIVEC_P (mode)
17334 || mode == V1TImode)
17335 return ALTIVEC_REGS;
17337 return rclass;
17340 return rclass;
17343 /* Debug version of rs6000_preferred_reload_class. */
17344 static enum reg_class
17345 rs6000_debug_preferred_reload_class (rtx x, enum reg_class rclass)
17347 enum reg_class ret = rs6000_preferred_reload_class (x, rclass);
17349 fprintf (stderr,
17350 "\nrs6000_preferred_reload_class, return %s, rclass = %s, "
17351 "mode = %s, x:\n",
17352 reg_class_names[ret], reg_class_names[rclass],
17353 GET_MODE_NAME (GET_MODE (x)));
17354 debug_rtx (x);
17356 return ret;
17359 /* If we are copying between FP or AltiVec registers and anything else, we need
17360 a memory location. The exception is when we are targeting ppc64 and the
17361 move to/from fpr to gpr instructions are available. Also, under VSX, you
17362 can copy vector registers from the FP register set to the Altivec register
17363 set and vice versa. */
17365 static bool
17366 rs6000_secondary_memory_needed (enum reg_class from_class,
17367 enum reg_class to_class,
17368 enum machine_mode mode)
17370 enum rs6000_reg_type from_type, to_type;
17371 bool altivec_p = ((from_class == ALTIVEC_REGS)
17372 || (to_class == ALTIVEC_REGS));
17374 /* If a simple/direct move is available, we don't need secondary memory */
17375 from_type = reg_class_to_reg_type[(int)from_class];
17376 to_type = reg_class_to_reg_type[(int)to_class];
17378 if (rs6000_secondary_reload_move (to_type, from_type, mode,
17379 (secondary_reload_info *)0, altivec_p))
17380 return false;
17382 /* If we have a floating point or vector register class, we need to use
17383 memory to transfer the data. */
17384 if (IS_FP_VECT_REG_TYPE (from_type) || IS_FP_VECT_REG_TYPE (to_type))
17385 return true;
17387 return false;
17390 /* Debug version of rs6000_secondary_memory_needed. */
17391 static bool
17392 rs6000_debug_secondary_memory_needed (enum reg_class from_class,
17393 enum reg_class to_class,
17394 enum machine_mode mode)
17396 bool ret = rs6000_secondary_memory_needed (from_class, to_class, mode);
17398 fprintf (stderr,
17399 "rs6000_secondary_memory_needed, return: %s, from_class = %s, "
17400 "to_class = %s, mode = %s\n",
17401 ret ? "true" : "false",
17402 reg_class_names[from_class],
17403 reg_class_names[to_class],
17404 GET_MODE_NAME (mode));
17406 return ret;
17409 /* Return the register class of a scratch register needed to copy IN into
17410 or out of a register in RCLASS in MODE. If it can be done directly,
17411 NO_REGS is returned. */
17413 static enum reg_class
17414 rs6000_secondary_reload_class (enum reg_class rclass, enum machine_mode mode,
17415 rtx in)
17417 int regno;
17419 if (TARGET_ELF || (DEFAULT_ABI == ABI_DARWIN
17420 #if TARGET_MACHO
17421 && MACHOPIC_INDIRECT
17422 #endif
17425 /* We cannot copy a symbolic operand directly into anything
17426 other than BASE_REGS for TARGET_ELF. So indicate that a
17427 register from BASE_REGS is needed as an intermediate
17428 register.
17430 On Darwin, pic addresses require a load from memory, which
17431 needs a base register. */
17432 if (rclass != BASE_REGS
17433 && (GET_CODE (in) == SYMBOL_REF
17434 || GET_CODE (in) == HIGH
17435 || GET_CODE (in) == LABEL_REF
17436 || GET_CODE (in) == CONST))
17437 return BASE_REGS;
17440 if (GET_CODE (in) == REG)
17442 regno = REGNO (in);
17443 if (regno >= FIRST_PSEUDO_REGISTER)
17445 regno = true_regnum (in);
17446 if (regno >= FIRST_PSEUDO_REGISTER)
17447 regno = -1;
17450 else if (GET_CODE (in) == SUBREG)
17452 regno = true_regnum (in);
17453 if (regno >= FIRST_PSEUDO_REGISTER)
17454 regno = -1;
17456 else
17457 regno = -1;
17459 /* We can place anything into GENERAL_REGS and can put GENERAL_REGS
17460 into anything. */
17461 if (rclass == GENERAL_REGS || rclass == BASE_REGS
17462 || (regno >= 0 && INT_REGNO_P (regno)))
17463 return NO_REGS;
17465 /* Constants, memory, and FP registers can go into FP registers. */
17466 if ((regno == -1 || FP_REGNO_P (regno))
17467 && (rclass == FLOAT_REGS || rclass == NON_SPECIAL_REGS))
17468 return (mode != SDmode || lra_in_progress) ? NO_REGS : GENERAL_REGS;
17470 /* Memory, and FP/altivec registers can go into fp/altivec registers under
17471 VSX. However, for scalar variables, use the traditional floating point
17472 registers so that we can use offset+register addressing. */
17473 if (TARGET_VSX
17474 && (regno == -1 || VSX_REGNO_P (regno))
17475 && VSX_REG_CLASS_P (rclass))
17477 if (GET_MODE_SIZE (mode) < 16)
17478 return FLOAT_REGS;
17480 return NO_REGS;
17483 /* Memory, and AltiVec registers can go into AltiVec registers. */
17484 if ((regno == -1 || ALTIVEC_REGNO_P (regno))
17485 && rclass == ALTIVEC_REGS)
17486 return NO_REGS;
17488 /* We can copy among the CR registers. */
17489 if ((rclass == CR_REGS || rclass == CR0_REGS)
17490 && regno >= 0 && CR_REGNO_P (regno))
17491 return NO_REGS;
17493 /* Otherwise, we need GENERAL_REGS. */
17494 return GENERAL_REGS;
17497 /* Debug version of rs6000_secondary_reload_class. */
17498 static enum reg_class
17499 rs6000_debug_secondary_reload_class (enum reg_class rclass,
17500 enum machine_mode mode, rtx in)
17502 enum reg_class ret = rs6000_secondary_reload_class (rclass, mode, in);
17503 fprintf (stderr,
17504 "\nrs6000_secondary_reload_class, return %s, rclass = %s, "
17505 "mode = %s, input rtx:\n",
17506 reg_class_names[ret], reg_class_names[rclass],
17507 GET_MODE_NAME (mode));
17508 debug_rtx (in);
17510 return ret;
17513 /* Return nonzero if for CLASS a mode change from FROM to TO is invalid. */
17515 static bool
17516 rs6000_cannot_change_mode_class (enum machine_mode from,
17517 enum machine_mode to,
17518 enum reg_class rclass)
17520 unsigned from_size = GET_MODE_SIZE (from);
17521 unsigned to_size = GET_MODE_SIZE (to);
17523 if (from_size != to_size)
17525 enum reg_class xclass = (TARGET_VSX) ? VSX_REGS : FLOAT_REGS;
17527 if (reg_classes_intersect_p (xclass, rclass))
17529 unsigned to_nregs = hard_regno_nregs[FIRST_FPR_REGNO][to];
17530 unsigned from_nregs = hard_regno_nregs[FIRST_FPR_REGNO][from];
17532 /* Don't allow 64-bit types to overlap with 128-bit types that take a
17533 single register under VSX because the scalar part of the register
17534 is in the upper 64-bits, and not the lower 64-bits. Types like
17535 TFmode/TDmode that take 2 scalar register can overlap. 128-bit
17536 IEEE floating point can't overlap, and neither can small
17537 values. */
17539 if (TARGET_IEEEQUAD && (to == TFmode || from == TFmode))
17540 return true;
17542 /* TDmode in floating-mode registers must always go into a register
17543 pair with the most significant word in the even-numbered register
17544 to match ISA requirements. In little-endian mode, this does not
17545 match subreg numbering, so we cannot allow subregs. */
17546 if (!BYTES_BIG_ENDIAN && (to == TDmode || from == TDmode))
17547 return true;
17549 if (from_size < 8 || to_size < 8)
17550 return true;
17552 if (from_size == 8 && (8 * to_nregs) != to_size)
17553 return true;
17555 if (to_size == 8 && (8 * from_nregs) != from_size)
17556 return true;
17558 return false;
17560 else
17561 return false;
17564 if (TARGET_E500_DOUBLE
17565 && ((((to) == DFmode) + ((from) == DFmode)) == 1
17566 || (((to) == TFmode) + ((from) == TFmode)) == 1
17567 || (((to) == DDmode) + ((from) == DDmode)) == 1
17568 || (((to) == TDmode) + ((from) == TDmode)) == 1
17569 || (((to) == DImode) + ((from) == DImode)) == 1))
17570 return true;
17572 /* Since the VSX register set includes traditional floating point registers
17573 and altivec registers, just check for the size being different instead of
17574 trying to check whether the modes are vector modes. Otherwise it won't
17575 allow say DF and DI to change classes. For types like TFmode and TDmode
17576 that take 2 64-bit registers, rather than a single 128-bit register, don't
17577 allow subregs of those types to other 128 bit types. */
17578 if (TARGET_VSX && VSX_REG_CLASS_P (rclass))
17580 unsigned num_regs = (from_size + 15) / 16;
17581 if (hard_regno_nregs[FIRST_FPR_REGNO][to] > num_regs
17582 || hard_regno_nregs[FIRST_FPR_REGNO][from] > num_regs)
17583 return true;
17585 return (from_size != 8 && from_size != 16);
17588 if (TARGET_ALTIVEC && rclass == ALTIVEC_REGS
17589 && (ALTIVEC_VECTOR_MODE (from) + ALTIVEC_VECTOR_MODE (to)) == 1)
17590 return true;
17592 if (TARGET_SPE && (SPE_VECTOR_MODE (from) + SPE_VECTOR_MODE (to)) == 1
17593 && reg_classes_intersect_p (GENERAL_REGS, rclass))
17594 return true;
17596 return false;
17599 /* Debug version of rs6000_cannot_change_mode_class. */
17600 static bool
17601 rs6000_debug_cannot_change_mode_class (enum machine_mode from,
17602 enum machine_mode to,
17603 enum reg_class rclass)
17605 bool ret = rs6000_cannot_change_mode_class (from, to, rclass);
17607 fprintf (stderr,
17608 "rs6000_cannot_change_mode_class, return %s, from = %s, "
17609 "to = %s, rclass = %s\n",
17610 ret ? "true" : "false",
17611 GET_MODE_NAME (from), GET_MODE_NAME (to),
17612 reg_class_names[rclass]);
17614 return ret;
17617 /* Return a string to do a move operation of 128 bits of data. */
17619 const char *
17620 rs6000_output_move_128bit (rtx operands[])
17622 rtx dest = operands[0];
17623 rtx src = operands[1];
17624 enum machine_mode mode = GET_MODE (dest);
17625 int dest_regno;
17626 int src_regno;
17627 bool dest_gpr_p, dest_fp_p, dest_vmx_p, dest_vsx_p;
17628 bool src_gpr_p, src_fp_p, src_vmx_p, src_vsx_p;
17630 if (REG_P (dest))
17632 dest_regno = REGNO (dest);
17633 dest_gpr_p = INT_REGNO_P (dest_regno);
17634 dest_fp_p = FP_REGNO_P (dest_regno);
17635 dest_vmx_p = ALTIVEC_REGNO_P (dest_regno);
17636 dest_vsx_p = dest_fp_p | dest_vmx_p;
17638 else
17640 dest_regno = -1;
17641 dest_gpr_p = dest_fp_p = dest_vmx_p = dest_vsx_p = false;
17644 if (REG_P (src))
17646 src_regno = REGNO (src);
17647 src_gpr_p = INT_REGNO_P (src_regno);
17648 src_fp_p = FP_REGNO_P (src_regno);
17649 src_vmx_p = ALTIVEC_REGNO_P (src_regno);
17650 src_vsx_p = src_fp_p | src_vmx_p;
17652 else
17654 src_regno = -1;
17655 src_gpr_p = src_fp_p = src_vmx_p = src_vsx_p = false;
17658 /* Register moves. */
17659 if (dest_regno >= 0 && src_regno >= 0)
17661 if (dest_gpr_p)
17663 if (src_gpr_p)
17664 return "#";
17666 else if (TARGET_VSX && TARGET_DIRECT_MOVE && src_vsx_p)
17667 return "#";
17670 else if (TARGET_VSX && dest_vsx_p)
17672 if (src_vsx_p)
17673 return "xxlor %x0,%x1,%x1";
17675 else if (TARGET_DIRECT_MOVE && src_gpr_p)
17676 return "#";
17679 else if (TARGET_ALTIVEC && dest_vmx_p && src_vmx_p)
17680 return "vor %0,%1,%1";
17682 else if (dest_fp_p && src_fp_p)
17683 return "#";
17686 /* Loads. */
17687 else if (dest_regno >= 0 && MEM_P (src))
17689 if (dest_gpr_p)
17691 if (TARGET_QUAD_MEMORY && quad_load_store_p (dest, src))
17692 return "lq %0,%1";
17693 else
17694 return "#";
17697 else if (TARGET_ALTIVEC && dest_vmx_p
17698 && altivec_indexed_or_indirect_operand (src, mode))
17699 return "lvx %0,%y1";
17701 else if (TARGET_VSX && dest_vsx_p)
17703 if (mode == V16QImode || mode == V8HImode || mode == V4SImode)
17704 return "lxvw4x %x0,%y1";
17705 else
17706 return "lxvd2x %x0,%y1";
17709 else if (TARGET_ALTIVEC && dest_vmx_p)
17710 return "lvx %0,%y1";
17712 else if (dest_fp_p)
17713 return "#";
17716 /* Stores. */
17717 else if (src_regno >= 0 && MEM_P (dest))
17719 if (src_gpr_p)
17721 if (TARGET_QUAD_MEMORY && quad_load_store_p (dest, src))
17722 return "stq %1,%0";
17723 else
17724 return "#";
17727 else if (TARGET_ALTIVEC && src_vmx_p
17728 && altivec_indexed_or_indirect_operand (src, mode))
17729 return "stvx %1,%y0";
17731 else if (TARGET_VSX && src_vsx_p)
17733 if (mode == V16QImode || mode == V8HImode || mode == V4SImode)
17734 return "stxvw4x %x1,%y0";
17735 else
17736 return "stxvd2x %x1,%y0";
17739 else if (TARGET_ALTIVEC && src_vmx_p)
17740 return "stvx %1,%y0";
17742 else if (src_fp_p)
17743 return "#";
17746 /* Constants. */
17747 else if (dest_regno >= 0
17748 && (GET_CODE (src) == CONST_INT
17749 || GET_CODE (src) == CONST_WIDE_INT
17750 || GET_CODE (src) == CONST_DOUBLE
17751 || GET_CODE (src) == CONST_VECTOR))
17753 if (dest_gpr_p)
17754 return "#";
17756 else if (TARGET_VSX && dest_vsx_p && zero_constant (src, mode))
17757 return "xxlxor %x0,%x0,%x0";
17759 else if (TARGET_ALTIVEC && dest_vmx_p)
17760 return output_vec_const_move (operands);
17763 if (TARGET_DEBUG_ADDR)
17765 fprintf (stderr, "\n===== Bad 128 bit move:\n");
17766 debug_rtx (gen_rtx_SET (VOIDmode, dest, src));
17769 gcc_unreachable ();
17772 /* Validate a 128-bit move. */
17773 bool
17774 rs6000_move_128bit_ok_p (rtx operands[])
17776 enum machine_mode mode = GET_MODE (operands[0]);
17777 return (gpc_reg_operand (operands[0], mode)
17778 || gpc_reg_operand (operands[1], mode));
17781 /* Return true if a 128-bit move needs to be split. */
17782 bool
17783 rs6000_split_128bit_ok_p (rtx operands[])
17785 if (!reload_completed)
17786 return false;
17788 if (!gpr_or_gpr_p (operands[0], operands[1]))
17789 return false;
17791 if (quad_load_store_p (operands[0], operands[1]))
17792 return false;
17794 return true;
17798 /* Given a comparison operation, return the bit number in CCR to test. We
17799 know this is a valid comparison.
17801 SCC_P is 1 if this is for an scc. That means that %D will have been
17802 used instead of %C, so the bits will be in different places.
17804 Return -1 if OP isn't a valid comparison for some reason. */
17807 ccr_bit (rtx op, int scc_p)
17809 enum rtx_code code = GET_CODE (op);
17810 enum machine_mode cc_mode;
17811 int cc_regnum;
17812 int base_bit;
17813 rtx reg;
17815 if (!COMPARISON_P (op))
17816 return -1;
17818 reg = XEXP (op, 0);
17820 gcc_assert (GET_CODE (reg) == REG && CR_REGNO_P (REGNO (reg)));
17822 cc_mode = GET_MODE (reg);
17823 cc_regnum = REGNO (reg);
17824 base_bit = 4 * (cc_regnum - CR0_REGNO);
17826 validate_condition_mode (code, cc_mode);
17828 /* When generating a sCOND operation, only positive conditions are
17829 allowed. */
17830 gcc_assert (!scc_p
17831 || code == EQ || code == GT || code == LT || code == UNORDERED
17832 || code == GTU || code == LTU);
17834 switch (code)
17836 case NE:
17837 return scc_p ? base_bit + 3 : base_bit + 2;
17838 case EQ:
17839 return base_bit + 2;
17840 case GT: case GTU: case UNLE:
17841 return base_bit + 1;
17842 case LT: case LTU: case UNGE:
17843 return base_bit;
17844 case ORDERED: case UNORDERED:
17845 return base_bit + 3;
17847 case GE: case GEU:
17848 /* If scc, we will have done a cror to put the bit in the
17849 unordered position. So test that bit. For integer, this is ! LT
17850 unless this is an scc insn. */
17851 return scc_p ? base_bit + 3 : base_bit;
17853 case LE: case LEU:
17854 return scc_p ? base_bit + 3 : base_bit + 1;
17856 default:
17857 gcc_unreachable ();
17861 /* Return the GOT register. */
17864 rs6000_got_register (rtx value ATTRIBUTE_UNUSED)
17866 /* The second flow pass currently (June 1999) can't update
17867 regs_ever_live without disturbing other parts of the compiler, so
17868 update it here to make the prolog/epilogue code happy. */
17869 if (!can_create_pseudo_p ()
17870 && !df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))
17871 df_set_regs_ever_live (RS6000_PIC_OFFSET_TABLE_REGNUM, true);
17873 crtl->uses_pic_offset_table = 1;
17875 return pic_offset_table_rtx;
17878 static rs6000_stack_t stack_info;
17880 /* Function to init struct machine_function.
17881 This will be called, via a pointer variable,
17882 from push_function_context. */
17884 static struct machine_function *
17885 rs6000_init_machine_status (void)
17887 stack_info.reload_completed = 0;
17888 return ggc_cleared_alloc<machine_function> ();
17891 #define INT_P(X) (GET_CODE (X) == CONST_INT && GET_MODE (X) == VOIDmode)
17894 extract_MB (rtx op)
17896 int i;
17897 unsigned long val = INTVAL (op);
17899 /* If the high bit is zero, the value is the first 1 bit we find
17900 from the left. */
17901 if ((val & 0x80000000) == 0)
17903 gcc_assert (val & 0xffffffff);
17905 i = 1;
17906 while (((val <<= 1) & 0x80000000) == 0)
17907 ++i;
17908 return i;
17911 /* If the high bit is set and the low bit is not, or the mask is all
17912 1's, the value is zero. */
17913 if ((val & 1) == 0 || (val & 0xffffffff) == 0xffffffff)
17914 return 0;
17916 /* Otherwise we have a wrap-around mask. Look for the first 0 bit
17917 from the right. */
17918 i = 31;
17919 while (((val >>= 1) & 1) != 0)
17920 --i;
17922 return i;
17926 extract_ME (rtx op)
17928 int i;
17929 unsigned long val = INTVAL (op);
17931 /* If the low bit is zero, the value is the first 1 bit we find from
17932 the right. */
17933 if ((val & 1) == 0)
17935 gcc_assert (val & 0xffffffff);
17937 i = 30;
17938 while (((val >>= 1) & 1) == 0)
17939 --i;
17941 return i;
17944 /* If the low bit is set and the high bit is not, or the mask is all
17945 1's, the value is 31. */
17946 if ((val & 0x80000000) == 0 || (val & 0xffffffff) == 0xffffffff)
17947 return 31;
17949 /* Otherwise we have a wrap-around mask. Look for the first 0 bit
17950 from the left. */
17951 i = 0;
17952 while (((val <<= 1) & 0x80000000) != 0)
17953 ++i;
17955 return i;
17958 /* Write out a function code label. */
17960 void
17961 rs6000_output_function_entry (FILE *file, const char *fname)
17963 if (fname[0] != '.')
17965 switch (DEFAULT_ABI)
17967 default:
17968 gcc_unreachable ();
17970 case ABI_AIX:
17971 if (DOT_SYMBOLS)
17972 putc ('.', file);
17973 else
17974 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "L.");
17975 break;
17977 case ABI_ELFv2:
17978 case ABI_V4:
17979 case ABI_DARWIN:
17980 break;
17984 RS6000_OUTPUT_BASENAME (file, fname);
17987 /* Print an operand. Recognize special options, documented below. */
17989 #if TARGET_ELF
17990 #define SMALL_DATA_RELOC ((rs6000_sdata == SDATA_EABI) ? "sda21" : "sdarel")
17991 #define SMALL_DATA_REG ((rs6000_sdata == SDATA_EABI) ? 0 : 13)
17992 #else
17993 #define SMALL_DATA_RELOC "sda21"
17994 #define SMALL_DATA_REG 0
17995 #endif
17997 void
17998 print_operand (FILE *file, rtx x, int code)
18000 int i;
18001 unsigned HOST_WIDE_INT uval;
18003 switch (code)
18005 /* %a is output_address. */
18007 case 'b':
18008 /* If constant, low-order 16 bits of constant, unsigned.
18009 Otherwise, write normally. */
18010 if (INT_P (x))
18011 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0xffff);
18012 else
18013 print_operand (file, x, 0);
18014 return;
18016 case 'B':
18017 /* If the low-order bit is zero, write 'r'; otherwise, write 'l'
18018 for 64-bit mask direction. */
18019 putc (((INTVAL (x) & 1) == 0 ? 'r' : 'l'), file);
18020 return;
18022 /* %c is output_addr_const if a CONSTANT_ADDRESS_P, otherwise
18023 output_operand. */
18025 case 'D':
18026 /* Like 'J' but get to the GT bit only. */
18027 gcc_assert (REG_P (x));
18029 /* Bit 1 is GT bit. */
18030 i = 4 * (REGNO (x) - CR0_REGNO) + 1;
18032 /* Add one for shift count in rlinm for scc. */
18033 fprintf (file, "%d", i + 1);
18034 return;
18036 case 'e':
18037 /* If the low 16 bits are 0, but some other bit is set, write 's'. */
18038 if (! INT_P (x))
18040 output_operand_lossage ("invalid %%e value");
18041 return;
18044 uval = INTVAL (x);
18045 if ((uval & 0xffff) == 0 && uval != 0)
18046 putc ('s', file);
18047 return;
18049 case 'E':
18050 /* X is a CR register. Print the number of the EQ bit of the CR */
18051 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
18052 output_operand_lossage ("invalid %%E value");
18053 else
18054 fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO) + 2);
18055 return;
18057 case 'f':
18058 /* X is a CR register. Print the shift count needed to move it
18059 to the high-order four bits. */
18060 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
18061 output_operand_lossage ("invalid %%f value");
18062 else
18063 fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO));
18064 return;
18066 case 'F':
18067 /* Similar, but print the count for the rotate in the opposite
18068 direction. */
18069 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
18070 output_operand_lossage ("invalid %%F value");
18071 else
18072 fprintf (file, "%d", 32 - 4 * (REGNO (x) - CR0_REGNO));
18073 return;
18075 case 'G':
18076 /* X is a constant integer. If it is negative, print "m",
18077 otherwise print "z". This is to make an aze or ame insn. */
18078 if (GET_CODE (x) != CONST_INT)
18079 output_operand_lossage ("invalid %%G value");
18080 else if (INTVAL (x) >= 0)
18081 putc ('z', file);
18082 else
18083 putc ('m', file);
18084 return;
18086 case 'h':
18087 /* If constant, output low-order five bits. Otherwise, write
18088 normally. */
18089 if (INT_P (x))
18090 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 31);
18091 else
18092 print_operand (file, x, 0);
18093 return;
18095 case 'H':
18096 /* If constant, output low-order six bits. Otherwise, write
18097 normally. */
18098 if (INT_P (x))
18099 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 63);
18100 else
18101 print_operand (file, x, 0);
18102 return;
18104 case 'I':
18105 /* Print `i' if this is a constant, else nothing. */
18106 if (INT_P (x))
18107 putc ('i', file);
18108 return;
18110 case 'j':
18111 /* Write the bit number in CCR for jump. */
18112 i = ccr_bit (x, 0);
18113 if (i == -1)
18114 output_operand_lossage ("invalid %%j code");
18115 else
18116 fprintf (file, "%d", i);
18117 return;
18119 case 'J':
18120 /* Similar, but add one for shift count in rlinm for scc and pass
18121 scc flag to `ccr_bit'. */
18122 i = ccr_bit (x, 1);
18123 if (i == -1)
18124 output_operand_lossage ("invalid %%J code");
18125 else
18126 /* If we want bit 31, write a shift count of zero, not 32. */
18127 fprintf (file, "%d", i == 31 ? 0 : i + 1);
18128 return;
18130 case 'k':
18131 /* X must be a constant. Write the 1's complement of the
18132 constant. */
18133 if (! INT_P (x))
18134 output_operand_lossage ("invalid %%k value");
18135 else
18136 fprintf (file, HOST_WIDE_INT_PRINT_DEC, ~ INTVAL (x));
18137 return;
18139 case 'K':
18140 /* X must be a symbolic constant on ELF. Write an
18141 expression suitable for an 'addi' that adds in the low 16
18142 bits of the MEM. */
18143 if (GET_CODE (x) == CONST)
18145 if (GET_CODE (XEXP (x, 0)) != PLUS
18146 || (GET_CODE (XEXP (XEXP (x, 0), 0)) != SYMBOL_REF
18147 && GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
18148 || GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
18149 output_operand_lossage ("invalid %%K value");
18151 print_operand_address (file, x);
18152 fputs ("@l", file);
18153 return;
18155 /* %l is output_asm_label. */
18157 case 'L':
18158 /* Write second word of DImode or DFmode reference. Works on register
18159 or non-indexed memory only. */
18160 if (REG_P (x))
18161 fputs (reg_names[REGNO (x) + 1], file);
18162 else if (MEM_P (x))
18164 /* Handle possible auto-increment. Since it is pre-increment and
18165 we have already done it, we can just use an offset of word. */
18166 if (GET_CODE (XEXP (x, 0)) == PRE_INC
18167 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
18168 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0),
18169 UNITS_PER_WORD));
18170 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
18171 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0),
18172 UNITS_PER_WORD));
18173 else
18174 output_address (XEXP (adjust_address_nv (x, SImode,
18175 UNITS_PER_WORD),
18176 0));
18178 if (small_data_operand (x, GET_MODE (x)))
18179 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
18180 reg_names[SMALL_DATA_REG]);
18182 return;
18184 case 'm':
18185 /* MB value for a mask operand. */
18186 if (! mask_operand (x, SImode))
18187 output_operand_lossage ("invalid %%m value");
18189 fprintf (file, "%d", extract_MB (x));
18190 return;
18192 case 'M':
18193 /* ME value for a mask operand. */
18194 if (! mask_operand (x, SImode))
18195 output_operand_lossage ("invalid %%M value");
18197 fprintf (file, "%d", extract_ME (x));
18198 return;
18200 /* %n outputs the negative of its operand. */
18202 case 'N':
18203 /* Write the number of elements in the vector times 4. */
18204 if (GET_CODE (x) != PARALLEL)
18205 output_operand_lossage ("invalid %%N value");
18206 else
18207 fprintf (file, "%d", XVECLEN (x, 0) * 4);
18208 return;
18210 case 'O':
18211 /* Similar, but subtract 1 first. */
18212 if (GET_CODE (x) != PARALLEL)
18213 output_operand_lossage ("invalid %%O value");
18214 else
18215 fprintf (file, "%d", (XVECLEN (x, 0) - 1) * 4);
18216 return;
18218 case 'p':
18219 /* X is a CONST_INT that is a power of two. Output the logarithm. */
18220 if (! INT_P (x)
18221 || INTVAL (x) < 0
18222 || (i = exact_log2 (INTVAL (x))) < 0)
18223 output_operand_lossage ("invalid %%p value");
18224 else
18225 fprintf (file, "%d", i);
18226 return;
18228 case 'P':
18229 /* The operand must be an indirect memory reference. The result
18230 is the register name. */
18231 if (GET_CODE (x) != MEM || GET_CODE (XEXP (x, 0)) != REG
18232 || REGNO (XEXP (x, 0)) >= 32)
18233 output_operand_lossage ("invalid %%P value");
18234 else
18235 fputs (reg_names[REGNO (XEXP (x, 0))], file);
18236 return;
18238 case 'q':
18239 /* This outputs the logical code corresponding to a boolean
18240 expression. The expression may have one or both operands
18241 negated (if one, only the first one). For condition register
18242 logical operations, it will also treat the negated
18243 CR codes as NOTs, but not handle NOTs of them. */
18245 const char *const *t = 0;
18246 const char *s;
18247 enum rtx_code code = GET_CODE (x);
18248 static const char * const tbl[3][3] = {
18249 { "and", "andc", "nor" },
18250 { "or", "orc", "nand" },
18251 { "xor", "eqv", "xor" } };
18253 if (code == AND)
18254 t = tbl[0];
18255 else if (code == IOR)
18256 t = tbl[1];
18257 else if (code == XOR)
18258 t = tbl[2];
18259 else
18260 output_operand_lossage ("invalid %%q value");
18262 if (GET_CODE (XEXP (x, 0)) != NOT)
18263 s = t[0];
18264 else
18266 if (GET_CODE (XEXP (x, 1)) == NOT)
18267 s = t[2];
18268 else
18269 s = t[1];
18272 fputs (s, file);
18274 return;
18276 case 'Q':
18277 if (! TARGET_MFCRF)
18278 return;
18279 fputc (',', file);
18280 /* FALLTHRU */
18282 case 'R':
18283 /* X is a CR register. Print the mask for `mtcrf'. */
18284 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
18285 output_operand_lossage ("invalid %%R value");
18286 else
18287 fprintf (file, "%d", 128 >> (REGNO (x) - CR0_REGNO));
18288 return;
18290 case 's':
18291 /* Low 5 bits of 32 - value */
18292 if (! INT_P (x))
18293 output_operand_lossage ("invalid %%s value");
18294 else
18295 fprintf (file, HOST_WIDE_INT_PRINT_DEC, (32 - INTVAL (x)) & 31);
18296 return;
18298 case 'S':
18299 /* PowerPC64 mask position. All 0's is excluded.
18300 CONST_INT 32-bit mask is considered sign-extended so any
18301 transition must occur within the CONST_INT, not on the boundary. */
18302 if (! mask64_operand (x, DImode))
18303 output_operand_lossage ("invalid %%S value");
18305 uval = INTVAL (x);
18307 if (uval & 1) /* Clear Left */
18309 #if HOST_BITS_PER_WIDE_INT > 64
18310 uval &= ((unsigned HOST_WIDE_INT) 1 << 64) - 1;
18311 #endif
18312 i = 64;
18314 else /* Clear Right */
18316 uval = ~uval;
18317 #if HOST_BITS_PER_WIDE_INT > 64
18318 uval &= ((unsigned HOST_WIDE_INT) 1 << 64) - 1;
18319 #endif
18320 i = 63;
18322 while (uval != 0)
18323 --i, uval >>= 1;
18324 gcc_assert (i >= 0);
18325 fprintf (file, "%d", i);
18326 return;
18328 case 't':
18329 /* Like 'J' but get to the OVERFLOW/UNORDERED bit. */
18330 gcc_assert (REG_P (x) && GET_MODE (x) == CCmode);
18332 /* Bit 3 is OV bit. */
18333 i = 4 * (REGNO (x) - CR0_REGNO) + 3;
18335 /* If we want bit 31, write a shift count of zero, not 32. */
18336 fprintf (file, "%d", i == 31 ? 0 : i + 1);
18337 return;
18339 case 'T':
18340 /* Print the symbolic name of a branch target register. */
18341 if (GET_CODE (x) != REG || (REGNO (x) != LR_REGNO
18342 && REGNO (x) != CTR_REGNO))
18343 output_operand_lossage ("invalid %%T value");
18344 else if (REGNO (x) == LR_REGNO)
18345 fputs ("lr", file);
18346 else
18347 fputs ("ctr", file);
18348 return;
18350 case 'u':
18351 /* High-order or low-order 16 bits of constant, whichever is non-zero,
18352 for use in unsigned operand. */
18353 if (! INT_P (x))
18355 output_operand_lossage ("invalid %%u value");
18356 return;
18359 uval = INTVAL (x);
18360 if ((uval & 0xffff) == 0)
18361 uval >>= 16;
18363 fprintf (file, HOST_WIDE_INT_PRINT_HEX, uval & 0xffff);
18364 return;
18366 case 'v':
18367 /* High-order 16 bits of constant for use in signed operand. */
18368 if (! INT_P (x))
18369 output_operand_lossage ("invalid %%v value");
18370 else
18371 fprintf (file, HOST_WIDE_INT_PRINT_HEX,
18372 (INTVAL (x) >> 16) & 0xffff);
18373 return;
18375 case 'U':
18376 /* Print `u' if this has an auto-increment or auto-decrement. */
18377 if (MEM_P (x)
18378 && (GET_CODE (XEXP (x, 0)) == PRE_INC
18379 || GET_CODE (XEXP (x, 0)) == PRE_DEC
18380 || GET_CODE (XEXP (x, 0)) == PRE_MODIFY))
18381 putc ('u', file);
18382 return;
18384 case 'V':
18385 /* Print the trap code for this operand. */
18386 switch (GET_CODE (x))
18388 case EQ:
18389 fputs ("eq", file); /* 4 */
18390 break;
18391 case NE:
18392 fputs ("ne", file); /* 24 */
18393 break;
18394 case LT:
18395 fputs ("lt", file); /* 16 */
18396 break;
18397 case LE:
18398 fputs ("le", file); /* 20 */
18399 break;
18400 case GT:
18401 fputs ("gt", file); /* 8 */
18402 break;
18403 case GE:
18404 fputs ("ge", file); /* 12 */
18405 break;
18406 case LTU:
18407 fputs ("llt", file); /* 2 */
18408 break;
18409 case LEU:
18410 fputs ("lle", file); /* 6 */
18411 break;
18412 case GTU:
18413 fputs ("lgt", file); /* 1 */
18414 break;
18415 case GEU:
18416 fputs ("lge", file); /* 5 */
18417 break;
18418 default:
18419 gcc_unreachable ();
18421 break;
18423 case 'w':
18424 /* If constant, low-order 16 bits of constant, signed. Otherwise, write
18425 normally. */
18426 if (INT_P (x))
18427 fprintf (file, HOST_WIDE_INT_PRINT_DEC,
18428 ((INTVAL (x) & 0xffff) ^ 0x8000) - 0x8000);
18429 else
18430 print_operand (file, x, 0);
18431 return;
18433 case 'W':
18434 /* MB value for a PowerPC64 rldic operand. */
18435 i = clz_hwi (INTVAL (x));
18437 fprintf (file, "%d", i);
18438 return;
18440 case 'x':
18441 /* X is a FPR or Altivec register used in a VSX context. */
18442 if (GET_CODE (x) != REG || !VSX_REGNO_P (REGNO (x)))
18443 output_operand_lossage ("invalid %%x value");
18444 else
18446 int reg = REGNO (x);
18447 int vsx_reg = (FP_REGNO_P (reg)
18448 ? reg - 32
18449 : reg - FIRST_ALTIVEC_REGNO + 32);
18451 #ifdef TARGET_REGNAMES
18452 if (TARGET_REGNAMES)
18453 fprintf (file, "%%vs%d", vsx_reg);
18454 else
18455 #endif
18456 fprintf (file, "%d", vsx_reg);
18458 return;
18460 case 'X':
18461 if (MEM_P (x)
18462 && (legitimate_indexed_address_p (XEXP (x, 0), 0)
18463 || (GET_CODE (XEXP (x, 0)) == PRE_MODIFY
18464 && legitimate_indexed_address_p (XEXP (XEXP (x, 0), 1), 0))))
18465 putc ('x', file);
18466 return;
18468 case 'Y':
18469 /* Like 'L', for third word of TImode/PTImode */
18470 if (REG_P (x))
18471 fputs (reg_names[REGNO (x) + 2], file);
18472 else if (MEM_P (x))
18474 if (GET_CODE (XEXP (x, 0)) == PRE_INC
18475 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
18476 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 8));
18477 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
18478 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 8));
18479 else
18480 output_address (XEXP (adjust_address_nv (x, SImode, 8), 0));
18481 if (small_data_operand (x, GET_MODE (x)))
18482 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
18483 reg_names[SMALL_DATA_REG]);
18485 return;
18487 case 'z':
18488 /* X is a SYMBOL_REF. Write out the name preceded by a
18489 period and without any trailing data in brackets. Used for function
18490 names. If we are configured for System V (or the embedded ABI) on
18491 the PowerPC, do not emit the period, since those systems do not use
18492 TOCs and the like. */
18493 gcc_assert (GET_CODE (x) == SYMBOL_REF);
18495 /* For macho, check to see if we need a stub. */
18496 if (TARGET_MACHO)
18498 const char *name = XSTR (x, 0);
18499 #if TARGET_MACHO
18500 if (darwin_emit_branch_islands
18501 && MACHOPIC_INDIRECT
18502 && machopic_classify_symbol (x) == MACHOPIC_UNDEFINED_FUNCTION)
18503 name = machopic_indirection_name (x, /*stub_p=*/true);
18504 #endif
18505 assemble_name (file, name);
18507 else if (!DOT_SYMBOLS)
18508 assemble_name (file, XSTR (x, 0));
18509 else
18510 rs6000_output_function_entry (file, XSTR (x, 0));
18511 return;
18513 case 'Z':
18514 /* Like 'L', for last word of TImode/PTImode. */
18515 if (REG_P (x))
18516 fputs (reg_names[REGNO (x) + 3], file);
18517 else if (MEM_P (x))
18519 if (GET_CODE (XEXP (x, 0)) == PRE_INC
18520 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
18521 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 12));
18522 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
18523 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 12));
18524 else
18525 output_address (XEXP (adjust_address_nv (x, SImode, 12), 0));
18526 if (small_data_operand (x, GET_MODE (x)))
18527 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
18528 reg_names[SMALL_DATA_REG]);
18530 return;
18532 /* Print AltiVec or SPE memory operand. */
18533 case 'y':
18535 rtx tmp;
18537 gcc_assert (MEM_P (x));
18539 tmp = XEXP (x, 0);
18541 /* Ugly hack because %y is overloaded. */
18542 if ((TARGET_SPE || TARGET_E500_DOUBLE)
18543 && (GET_MODE_SIZE (GET_MODE (x)) == 8
18544 || GET_MODE (x) == TFmode
18545 || GET_MODE (x) == TImode
18546 || GET_MODE (x) == PTImode))
18548 /* Handle [reg]. */
18549 if (REG_P (tmp))
18551 fprintf (file, "0(%s)", reg_names[REGNO (tmp)]);
18552 break;
18554 /* Handle [reg+UIMM]. */
18555 else if (GET_CODE (tmp) == PLUS &&
18556 GET_CODE (XEXP (tmp, 1)) == CONST_INT)
18558 int x;
18560 gcc_assert (REG_P (XEXP (tmp, 0)));
18562 x = INTVAL (XEXP (tmp, 1));
18563 fprintf (file, "%d(%s)", x, reg_names[REGNO (XEXP (tmp, 0))]);
18564 break;
18567 /* Fall through. Must be [reg+reg]. */
18569 if (VECTOR_MEM_ALTIVEC_P (GET_MODE (x))
18570 && GET_CODE (tmp) == AND
18571 && GET_CODE (XEXP (tmp, 1)) == CONST_INT
18572 && INTVAL (XEXP (tmp, 1)) == -16)
18573 tmp = XEXP (tmp, 0);
18574 else if (VECTOR_MEM_VSX_P (GET_MODE (x))
18575 && GET_CODE (tmp) == PRE_MODIFY)
18576 tmp = XEXP (tmp, 1);
18577 if (REG_P (tmp))
18578 fprintf (file, "0,%s", reg_names[REGNO (tmp)]);
18579 else
18581 if (GET_CODE (tmp) != PLUS
18582 || !REG_P (XEXP (tmp, 0))
18583 || !REG_P (XEXP (tmp, 1)))
18585 output_operand_lossage ("invalid %%y value, try using the 'Z' constraint");
18586 break;
18589 if (REGNO (XEXP (tmp, 0)) == 0)
18590 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 1)) ],
18591 reg_names[ REGNO (XEXP (tmp, 0)) ]);
18592 else
18593 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 0)) ],
18594 reg_names[ REGNO (XEXP (tmp, 1)) ]);
18596 break;
18599 case 0:
18600 if (REG_P (x))
18601 fprintf (file, "%s", reg_names[REGNO (x)]);
18602 else if (MEM_P (x))
18604 /* We need to handle PRE_INC and PRE_DEC here, since we need to
18605 know the width from the mode. */
18606 if (GET_CODE (XEXP (x, 0)) == PRE_INC)
18607 fprintf (file, "%d(%s)", GET_MODE_SIZE (GET_MODE (x)),
18608 reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
18609 else if (GET_CODE (XEXP (x, 0)) == PRE_DEC)
18610 fprintf (file, "%d(%s)", - GET_MODE_SIZE (GET_MODE (x)),
18611 reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
18612 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
18613 output_address (XEXP (XEXP (x, 0), 1));
18614 else
18615 output_address (XEXP (x, 0));
18617 else
18619 if (toc_relative_expr_p (x, false))
18620 /* This hack along with a corresponding hack in
18621 rs6000_output_addr_const_extra arranges to output addends
18622 where the assembler expects to find them. eg.
18623 (plus (unspec [(symbol_ref ("x")) (reg 2)] tocrel) 4)
18624 without this hack would be output as "x@toc+4". We
18625 want "x+4@toc". */
18626 output_addr_const (file, CONST_CAST_RTX (tocrel_base));
18627 else
18628 output_addr_const (file, x);
18630 return;
18632 case '&':
18633 if (const char *name = get_some_local_dynamic_name ())
18634 assemble_name (file, name);
18635 else
18636 output_operand_lossage ("'%%&' used without any "
18637 "local dynamic TLS references");
18638 return;
18640 default:
18641 output_operand_lossage ("invalid %%xn code");
18645 /* Print the address of an operand. */
18647 void
18648 print_operand_address (FILE *file, rtx x)
18650 if (REG_P (x))
18651 fprintf (file, "0(%s)", reg_names[ REGNO (x) ]);
18652 else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST
18653 || GET_CODE (x) == LABEL_REF)
18655 output_addr_const (file, x);
18656 if (small_data_operand (x, GET_MODE (x)))
18657 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
18658 reg_names[SMALL_DATA_REG]);
18659 else
18660 gcc_assert (!TARGET_TOC);
18662 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
18663 && REG_P (XEXP (x, 1)))
18665 if (REGNO (XEXP (x, 0)) == 0)
18666 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 1)) ],
18667 reg_names[ REGNO (XEXP (x, 0)) ]);
18668 else
18669 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 0)) ],
18670 reg_names[ REGNO (XEXP (x, 1)) ]);
18672 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
18673 && GET_CODE (XEXP (x, 1)) == CONST_INT)
18674 fprintf (file, HOST_WIDE_INT_PRINT_DEC "(%s)",
18675 INTVAL (XEXP (x, 1)), reg_names[ REGNO (XEXP (x, 0)) ]);
18676 #if TARGET_MACHO
18677 else if (GET_CODE (x) == LO_SUM && REG_P (XEXP (x, 0))
18678 && CONSTANT_P (XEXP (x, 1)))
18680 fprintf (file, "lo16(");
18681 output_addr_const (file, XEXP (x, 1));
18682 fprintf (file, ")(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
18684 #endif
18685 #if TARGET_ELF
18686 else if (GET_CODE (x) == LO_SUM && REG_P (XEXP (x, 0))
18687 && CONSTANT_P (XEXP (x, 1)))
18689 output_addr_const (file, XEXP (x, 1));
18690 fprintf (file, "@l(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
18692 #endif
18693 else if (toc_relative_expr_p (x, false))
18695 /* This hack along with a corresponding hack in
18696 rs6000_output_addr_const_extra arranges to output addends
18697 where the assembler expects to find them. eg.
18698 (lo_sum (reg 9)
18699 . (plus (unspec [(symbol_ref ("x")) (reg 2)] tocrel) 8))
18700 without this hack would be output as "x@toc+8@l(9)". We
18701 want "x+8@toc@l(9)". */
18702 output_addr_const (file, CONST_CAST_RTX (tocrel_base));
18703 if (GET_CODE (x) == LO_SUM)
18704 fprintf (file, "@l(%s)", reg_names[REGNO (XEXP (x, 0))]);
18705 else
18706 fprintf (file, "(%s)", reg_names[REGNO (XVECEXP (tocrel_base, 0, 1))]);
18708 else
18709 gcc_unreachable ();
18712 /* Implement TARGET_OUTPUT_ADDR_CONST_EXTRA. */
18714 static bool
18715 rs6000_output_addr_const_extra (FILE *file, rtx x)
18717 if (GET_CODE (x) == UNSPEC)
18718 switch (XINT (x, 1))
18720 case UNSPEC_TOCREL:
18721 gcc_checking_assert (GET_CODE (XVECEXP (x, 0, 0)) == SYMBOL_REF
18722 && REG_P (XVECEXP (x, 0, 1))
18723 && REGNO (XVECEXP (x, 0, 1)) == TOC_REGISTER);
18724 output_addr_const (file, XVECEXP (x, 0, 0));
18725 if (x == tocrel_base && tocrel_offset != const0_rtx)
18727 if (INTVAL (tocrel_offset) >= 0)
18728 fprintf (file, "+");
18729 output_addr_const (file, CONST_CAST_RTX (tocrel_offset));
18731 if (!TARGET_AIX || (TARGET_ELF && TARGET_MINIMAL_TOC))
18733 putc ('-', file);
18734 assemble_name (file, toc_label_name);
18736 else if (TARGET_ELF)
18737 fputs ("@toc", file);
18738 return true;
18740 #if TARGET_MACHO
18741 case UNSPEC_MACHOPIC_OFFSET:
18742 output_addr_const (file, XVECEXP (x, 0, 0));
18743 putc ('-', file);
18744 machopic_output_function_base_name (file);
18745 return true;
18746 #endif
18748 return false;
18751 /* Target hook for assembling integer objects. The PowerPC version has
18752 to handle fixup entries for relocatable code if RELOCATABLE_NEEDS_FIXUP
18753 is defined. It also needs to handle DI-mode objects on 64-bit
18754 targets. */
18756 static bool
18757 rs6000_assemble_integer (rtx x, unsigned int size, int aligned_p)
18759 #ifdef RELOCATABLE_NEEDS_FIXUP
18760 /* Special handling for SI values. */
18761 if (RELOCATABLE_NEEDS_FIXUP && size == 4 && aligned_p)
18763 static int recurse = 0;
18765 /* For -mrelocatable, we mark all addresses that need to be fixed up in
18766 the .fixup section. Since the TOC section is already relocated, we
18767 don't need to mark it here. We used to skip the text section, but it
18768 should never be valid for relocated addresses to be placed in the text
18769 section. */
18770 if (TARGET_RELOCATABLE
18771 && in_section != toc_section
18772 && !recurse
18773 && !CONST_SCALAR_INT_P (x)
18774 && CONSTANT_P (x))
18776 char buf[256];
18778 recurse = 1;
18779 ASM_GENERATE_INTERNAL_LABEL (buf, "LCP", fixuplabelno);
18780 fixuplabelno++;
18781 ASM_OUTPUT_LABEL (asm_out_file, buf);
18782 fprintf (asm_out_file, "\t.long\t(");
18783 output_addr_const (asm_out_file, x);
18784 fprintf (asm_out_file, ")@fixup\n");
18785 fprintf (asm_out_file, "\t.section\t\".fixup\",\"aw\"\n");
18786 ASM_OUTPUT_ALIGN (asm_out_file, 2);
18787 fprintf (asm_out_file, "\t.long\t");
18788 assemble_name (asm_out_file, buf);
18789 fprintf (asm_out_file, "\n\t.previous\n");
18790 recurse = 0;
18791 return true;
18793 /* Remove initial .'s to turn a -mcall-aixdesc function
18794 address into the address of the descriptor, not the function
18795 itself. */
18796 else if (GET_CODE (x) == SYMBOL_REF
18797 && XSTR (x, 0)[0] == '.'
18798 && DEFAULT_ABI == ABI_AIX)
18800 const char *name = XSTR (x, 0);
18801 while (*name == '.')
18802 name++;
18804 fprintf (asm_out_file, "\t.long\t%s\n", name);
18805 return true;
18808 #endif /* RELOCATABLE_NEEDS_FIXUP */
18809 return default_assemble_integer (x, size, aligned_p);
18812 #if defined (HAVE_GAS_HIDDEN) && !TARGET_MACHO
18813 /* Emit an assembler directive to set symbol visibility for DECL to
18814 VISIBILITY_TYPE. */
18816 static void
18817 rs6000_assemble_visibility (tree decl, int vis)
18819 if (TARGET_XCOFF)
18820 return;
18822 /* Functions need to have their entry point symbol visibility set as
18823 well as their descriptor symbol visibility. */
18824 if (DEFAULT_ABI == ABI_AIX
18825 && DOT_SYMBOLS
18826 && TREE_CODE (decl) == FUNCTION_DECL)
18828 static const char * const visibility_types[] = {
18829 NULL, "internal", "hidden", "protected"
18832 const char *name, *type;
18834 name = ((* targetm.strip_name_encoding)
18835 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
18836 type = visibility_types[vis];
18838 fprintf (asm_out_file, "\t.%s\t%s\n", type, name);
18839 fprintf (asm_out_file, "\t.%s\t.%s\n", type, name);
18841 else
18842 default_assemble_visibility (decl, vis);
18844 #endif
18846 enum rtx_code
18847 rs6000_reverse_condition (enum machine_mode mode, enum rtx_code code)
18849 /* Reversal of FP compares takes care -- an ordered compare
18850 becomes an unordered compare and vice versa. */
18851 if (mode == CCFPmode
18852 && (!flag_finite_math_only
18853 || code == UNLT || code == UNLE || code == UNGT || code == UNGE
18854 || code == UNEQ || code == LTGT))
18855 return reverse_condition_maybe_unordered (code);
18856 else
18857 return reverse_condition (code);
18860 /* Generate a compare for CODE. Return a brand-new rtx that
18861 represents the result of the compare. */
18863 static rtx
18864 rs6000_generate_compare (rtx cmp, enum machine_mode mode)
18866 enum machine_mode comp_mode;
18867 rtx compare_result;
18868 enum rtx_code code = GET_CODE (cmp);
18869 rtx op0 = XEXP (cmp, 0);
18870 rtx op1 = XEXP (cmp, 1);
18872 if (FLOAT_MODE_P (mode))
18873 comp_mode = CCFPmode;
18874 else if (code == GTU || code == LTU
18875 || code == GEU || code == LEU)
18876 comp_mode = CCUNSmode;
18877 else if ((code == EQ || code == NE)
18878 && unsigned_reg_p (op0)
18879 && (unsigned_reg_p (op1)
18880 || (CONST_INT_P (op1) && INTVAL (op1) != 0)))
18881 /* These are unsigned values, perhaps there will be a later
18882 ordering compare that can be shared with this one. */
18883 comp_mode = CCUNSmode;
18884 else
18885 comp_mode = CCmode;
18887 /* If we have an unsigned compare, make sure we don't have a signed value as
18888 an immediate. */
18889 if (comp_mode == CCUNSmode && GET_CODE (op1) == CONST_INT
18890 && INTVAL (op1) < 0)
18892 op0 = copy_rtx_if_shared (op0);
18893 op1 = force_reg (GET_MODE (op0), op1);
18894 cmp = gen_rtx_fmt_ee (code, GET_MODE (cmp), op0, op1);
18897 /* First, the compare. */
18898 compare_result = gen_reg_rtx (comp_mode);
18900 /* E500 FP compare instructions on the GPRs. Yuck! */
18901 if ((!TARGET_FPRS && TARGET_HARD_FLOAT)
18902 && FLOAT_MODE_P (mode))
18904 rtx cmp, or_result, compare_result2;
18905 enum machine_mode op_mode = GET_MODE (op0);
18906 bool reverse_p;
18908 if (op_mode == VOIDmode)
18909 op_mode = GET_MODE (op1);
18911 /* First reverse the condition codes that aren't directly supported. */
18912 switch (code)
18914 case NE:
18915 case UNLT:
18916 case UNLE:
18917 case UNGT:
18918 case UNGE:
18919 code = reverse_condition_maybe_unordered (code);
18920 reverse_p = true;
18921 break;
18923 case EQ:
18924 case LT:
18925 case LE:
18926 case GT:
18927 case GE:
18928 reverse_p = false;
18929 break;
18931 default:
18932 gcc_unreachable ();
18935 /* The E500 FP compare instructions toggle the GT bit (CR bit 1) only.
18936 This explains the following mess. */
18938 switch (code)
18940 case EQ:
18941 switch (op_mode)
18943 case SFmode:
18944 cmp = (flag_finite_math_only && !flag_trapping_math)
18945 ? gen_tstsfeq_gpr (compare_result, op0, op1)
18946 : gen_cmpsfeq_gpr (compare_result, op0, op1);
18947 break;
18949 case DFmode:
18950 cmp = (flag_finite_math_only && !flag_trapping_math)
18951 ? gen_tstdfeq_gpr (compare_result, op0, op1)
18952 : gen_cmpdfeq_gpr (compare_result, op0, op1);
18953 break;
18955 case TFmode:
18956 cmp = (flag_finite_math_only && !flag_trapping_math)
18957 ? gen_tsttfeq_gpr (compare_result, op0, op1)
18958 : gen_cmptfeq_gpr (compare_result, op0, op1);
18959 break;
18961 default:
18962 gcc_unreachable ();
18964 break;
18966 case GT:
18967 case GE:
18968 switch (op_mode)
18970 case SFmode:
18971 cmp = (flag_finite_math_only && !flag_trapping_math)
18972 ? gen_tstsfgt_gpr (compare_result, op0, op1)
18973 : gen_cmpsfgt_gpr (compare_result, op0, op1);
18974 break;
18976 case DFmode:
18977 cmp = (flag_finite_math_only && !flag_trapping_math)
18978 ? gen_tstdfgt_gpr (compare_result, op0, op1)
18979 : gen_cmpdfgt_gpr (compare_result, op0, op1);
18980 break;
18982 case TFmode:
18983 cmp = (flag_finite_math_only && !flag_trapping_math)
18984 ? gen_tsttfgt_gpr (compare_result, op0, op1)
18985 : gen_cmptfgt_gpr (compare_result, op0, op1);
18986 break;
18988 default:
18989 gcc_unreachable ();
18991 break;
18993 case LT:
18994 case LE:
18995 switch (op_mode)
18997 case SFmode:
18998 cmp = (flag_finite_math_only && !flag_trapping_math)
18999 ? gen_tstsflt_gpr (compare_result, op0, op1)
19000 : gen_cmpsflt_gpr (compare_result, op0, op1);
19001 break;
19003 case DFmode:
19004 cmp = (flag_finite_math_only && !flag_trapping_math)
19005 ? gen_tstdflt_gpr (compare_result, op0, op1)
19006 : gen_cmpdflt_gpr (compare_result, op0, op1);
19007 break;
19009 case TFmode:
19010 cmp = (flag_finite_math_only && !flag_trapping_math)
19011 ? gen_tsttflt_gpr (compare_result, op0, op1)
19012 : gen_cmptflt_gpr (compare_result, op0, op1);
19013 break;
19015 default:
19016 gcc_unreachable ();
19018 break;
19020 default:
19021 gcc_unreachable ();
19024 /* Synthesize LE and GE from LT/GT || EQ. */
19025 if (code == LE || code == GE)
19027 emit_insn (cmp);
19029 compare_result2 = gen_reg_rtx (CCFPmode);
19031 /* Do the EQ. */
19032 switch (op_mode)
19034 case SFmode:
19035 cmp = (flag_finite_math_only && !flag_trapping_math)
19036 ? gen_tstsfeq_gpr (compare_result2, op0, op1)
19037 : gen_cmpsfeq_gpr (compare_result2, op0, op1);
19038 break;
19040 case DFmode:
19041 cmp = (flag_finite_math_only && !flag_trapping_math)
19042 ? gen_tstdfeq_gpr (compare_result2, op0, op1)
19043 : gen_cmpdfeq_gpr (compare_result2, op0, op1);
19044 break;
19046 case TFmode:
19047 cmp = (flag_finite_math_only && !flag_trapping_math)
19048 ? gen_tsttfeq_gpr (compare_result2, op0, op1)
19049 : gen_cmptfeq_gpr (compare_result2, op0, op1);
19050 break;
19052 default:
19053 gcc_unreachable ();
19056 emit_insn (cmp);
19058 /* OR them together. */
19059 or_result = gen_reg_rtx (CCFPmode);
19060 cmp = gen_e500_cr_ior_compare (or_result, compare_result,
19061 compare_result2);
19062 compare_result = or_result;
19065 code = reverse_p ? NE : EQ;
19067 emit_insn (cmp);
19069 else
19071 /* Generate XLC-compatible TFmode compare as PARALLEL with extra
19072 CLOBBERs to match cmptf_internal2 pattern. */
19073 if (comp_mode == CCFPmode && TARGET_XL_COMPAT
19074 && GET_MODE (op0) == TFmode
19075 && !TARGET_IEEEQUAD
19076 && TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_LONG_DOUBLE_128)
19077 emit_insn (gen_rtx_PARALLEL (VOIDmode,
19078 gen_rtvec (10,
19079 gen_rtx_SET (VOIDmode,
19080 compare_result,
19081 gen_rtx_COMPARE (comp_mode, op0, op1)),
19082 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
19083 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
19084 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
19085 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
19086 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
19087 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
19088 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
19089 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
19090 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (Pmode)))));
19091 else if (GET_CODE (op1) == UNSPEC
19092 && XINT (op1, 1) == UNSPEC_SP_TEST)
19094 rtx op1b = XVECEXP (op1, 0, 0);
19095 comp_mode = CCEQmode;
19096 compare_result = gen_reg_rtx (CCEQmode);
19097 if (TARGET_64BIT)
19098 emit_insn (gen_stack_protect_testdi (compare_result, op0, op1b));
19099 else
19100 emit_insn (gen_stack_protect_testsi (compare_result, op0, op1b));
19102 else
19103 emit_insn (gen_rtx_SET (VOIDmode, compare_result,
19104 gen_rtx_COMPARE (comp_mode, op0, op1)));
19107 /* Some kinds of FP comparisons need an OR operation;
19108 under flag_finite_math_only we don't bother. */
19109 if (FLOAT_MODE_P (mode)
19110 && !flag_finite_math_only
19111 && !(TARGET_HARD_FLOAT && !TARGET_FPRS)
19112 && (code == LE || code == GE
19113 || code == UNEQ || code == LTGT
19114 || code == UNGT || code == UNLT))
19116 enum rtx_code or1, or2;
19117 rtx or1_rtx, or2_rtx, compare2_rtx;
19118 rtx or_result = gen_reg_rtx (CCEQmode);
19120 switch (code)
19122 case LE: or1 = LT; or2 = EQ; break;
19123 case GE: or1 = GT; or2 = EQ; break;
19124 case UNEQ: or1 = UNORDERED; or2 = EQ; break;
19125 case LTGT: or1 = LT; or2 = GT; break;
19126 case UNGT: or1 = UNORDERED; or2 = GT; break;
19127 case UNLT: or1 = UNORDERED; or2 = LT; break;
19128 default: gcc_unreachable ();
19130 validate_condition_mode (or1, comp_mode);
19131 validate_condition_mode (or2, comp_mode);
19132 or1_rtx = gen_rtx_fmt_ee (or1, SImode, compare_result, const0_rtx);
19133 or2_rtx = gen_rtx_fmt_ee (or2, SImode, compare_result, const0_rtx);
19134 compare2_rtx = gen_rtx_COMPARE (CCEQmode,
19135 gen_rtx_IOR (SImode, or1_rtx, or2_rtx),
19136 const_true_rtx);
19137 emit_insn (gen_rtx_SET (VOIDmode, or_result, compare2_rtx));
19139 compare_result = or_result;
19140 code = EQ;
19143 validate_condition_mode (code, GET_MODE (compare_result));
19145 return gen_rtx_fmt_ee (code, VOIDmode, compare_result, const0_rtx);
19149 /* Emit the RTL for an sISEL pattern. */
19151 void
19152 rs6000_emit_sISEL (enum machine_mode mode ATTRIBUTE_UNUSED, rtx operands[])
19154 rs6000_emit_int_cmove (operands[0], operands[1], const1_rtx, const0_rtx);
19157 void
19158 rs6000_emit_sCOND (enum machine_mode mode, rtx operands[])
19160 rtx condition_rtx;
19161 enum machine_mode op_mode;
19162 enum rtx_code cond_code;
19163 rtx result = operands[0];
19165 if (TARGET_ISEL && (mode == SImode || mode == DImode))
19167 rs6000_emit_sISEL (mode, operands);
19168 return;
19171 condition_rtx = rs6000_generate_compare (operands[1], mode);
19172 cond_code = GET_CODE (condition_rtx);
19174 if (FLOAT_MODE_P (mode)
19175 && !TARGET_FPRS && TARGET_HARD_FLOAT)
19177 rtx t;
19179 PUT_MODE (condition_rtx, SImode);
19180 t = XEXP (condition_rtx, 0);
19182 gcc_assert (cond_code == NE || cond_code == EQ);
19184 if (cond_code == NE)
19185 emit_insn (gen_e500_flip_gt_bit (t, t));
19187 emit_insn (gen_move_from_CR_gt_bit (result, t));
19188 return;
19191 if (cond_code == NE
19192 || cond_code == GE || cond_code == LE
19193 || cond_code == GEU || cond_code == LEU
19194 || cond_code == ORDERED || cond_code == UNGE || cond_code == UNLE)
19196 rtx not_result = gen_reg_rtx (CCEQmode);
19197 rtx not_op, rev_cond_rtx;
19198 enum machine_mode cc_mode;
19200 cc_mode = GET_MODE (XEXP (condition_rtx, 0));
19202 rev_cond_rtx = gen_rtx_fmt_ee (rs6000_reverse_condition (cc_mode, cond_code),
19203 SImode, XEXP (condition_rtx, 0), const0_rtx);
19204 not_op = gen_rtx_COMPARE (CCEQmode, rev_cond_rtx, const0_rtx);
19205 emit_insn (gen_rtx_SET (VOIDmode, not_result, not_op));
19206 condition_rtx = gen_rtx_EQ (VOIDmode, not_result, const0_rtx);
19209 op_mode = GET_MODE (XEXP (operands[1], 0));
19210 if (op_mode == VOIDmode)
19211 op_mode = GET_MODE (XEXP (operands[1], 1));
19213 if (TARGET_POWERPC64 && (op_mode == DImode || FLOAT_MODE_P (mode)))
19215 PUT_MODE (condition_rtx, DImode);
19216 convert_move (result, condition_rtx, 0);
19218 else
19220 PUT_MODE (condition_rtx, SImode);
19221 emit_insn (gen_rtx_SET (VOIDmode, result, condition_rtx));
19225 /* Emit a branch of kind CODE to location LOC. */
19227 void
19228 rs6000_emit_cbranch (enum machine_mode mode, rtx operands[])
19230 rtx condition_rtx, loc_ref;
19232 condition_rtx = rs6000_generate_compare (operands[0], mode);
19233 loc_ref = gen_rtx_LABEL_REF (VOIDmode, operands[3]);
19234 emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx,
19235 gen_rtx_IF_THEN_ELSE (VOIDmode, condition_rtx,
19236 loc_ref, pc_rtx)));
19239 /* Return the string to output a conditional branch to LABEL, which is
19240 the operand template of the label, or NULL if the branch is really a
19241 conditional return.
19243 OP is the conditional expression. XEXP (OP, 0) is assumed to be a
19244 condition code register and its mode specifies what kind of
19245 comparison we made.
19247 REVERSED is nonzero if we should reverse the sense of the comparison.
19249 INSN is the insn. */
19251 char *
19252 output_cbranch (rtx op, const char *label, int reversed, rtx_insn *insn)
19254 static char string[64];
19255 enum rtx_code code = GET_CODE (op);
19256 rtx cc_reg = XEXP (op, 0);
19257 enum machine_mode mode = GET_MODE (cc_reg);
19258 int cc_regno = REGNO (cc_reg) - CR0_REGNO;
19259 int need_longbranch = label != NULL && get_attr_length (insn) == 8;
19260 int really_reversed = reversed ^ need_longbranch;
19261 char *s = string;
19262 const char *ccode;
19263 const char *pred;
19264 rtx note;
19266 validate_condition_mode (code, mode);
19268 /* Work out which way this really branches. We could use
19269 reverse_condition_maybe_unordered here always but this
19270 makes the resulting assembler clearer. */
19271 if (really_reversed)
19273 /* Reversal of FP compares takes care -- an ordered compare
19274 becomes an unordered compare and vice versa. */
19275 if (mode == CCFPmode)
19276 code = reverse_condition_maybe_unordered (code);
19277 else
19278 code = reverse_condition (code);
19281 if ((!TARGET_FPRS && TARGET_HARD_FLOAT) && mode == CCFPmode)
19283 /* The efscmp/tst* instructions twiddle bit 2, which maps nicely
19284 to the GT bit. */
19285 switch (code)
19287 case EQ:
19288 /* Opposite of GT. */
19289 code = GT;
19290 break;
19292 case NE:
19293 code = UNLE;
19294 break;
19296 default:
19297 gcc_unreachable ();
19301 switch (code)
19303 /* Not all of these are actually distinct opcodes, but
19304 we distinguish them for clarity of the resulting assembler. */
19305 case NE: case LTGT:
19306 ccode = "ne"; break;
19307 case EQ: case UNEQ:
19308 ccode = "eq"; break;
19309 case GE: case GEU:
19310 ccode = "ge"; break;
19311 case GT: case GTU: case UNGT:
19312 ccode = "gt"; break;
19313 case LE: case LEU:
19314 ccode = "le"; break;
19315 case LT: case LTU: case UNLT:
19316 ccode = "lt"; break;
19317 case UNORDERED: ccode = "un"; break;
19318 case ORDERED: ccode = "nu"; break;
19319 case UNGE: ccode = "nl"; break;
19320 case UNLE: ccode = "ng"; break;
19321 default:
19322 gcc_unreachable ();
19325 /* Maybe we have a guess as to how likely the branch is. */
19326 pred = "";
19327 note = find_reg_note (insn, REG_BR_PROB, NULL_RTX);
19328 if (note != NULL_RTX)
19330 /* PROB is the difference from 50%. */
19331 int prob = XINT (note, 0) - REG_BR_PROB_BASE / 2;
19333 /* Only hint for highly probable/improbable branches on newer
19334 cpus as static prediction overrides processor dynamic
19335 prediction. For older cpus we may as well always hint, but
19336 assume not taken for branches that are very close to 50% as a
19337 mispredicted taken branch is more expensive than a
19338 mispredicted not-taken branch. */
19339 if (rs6000_always_hint
19340 || (abs (prob) > REG_BR_PROB_BASE / 100 * 48
19341 && br_prob_note_reliable_p (note)))
19343 if (abs (prob) > REG_BR_PROB_BASE / 20
19344 && ((prob > 0) ^ need_longbranch))
19345 pred = "+";
19346 else
19347 pred = "-";
19351 if (label == NULL)
19352 s += sprintf (s, "b%slr%s ", ccode, pred);
19353 else
19354 s += sprintf (s, "b%s%s ", ccode, pred);
19356 /* We need to escape any '%' characters in the reg_names string.
19357 Assume they'd only be the first character.... */
19358 if (reg_names[cc_regno + CR0_REGNO][0] == '%')
19359 *s++ = '%';
19360 s += sprintf (s, "%s", reg_names[cc_regno + CR0_REGNO]);
19362 if (label != NULL)
19364 /* If the branch distance was too far, we may have to use an
19365 unconditional branch to go the distance. */
19366 if (need_longbranch)
19367 s += sprintf (s, ",$+8\n\tb %s", label);
19368 else
19369 s += sprintf (s, ",%s", label);
19372 return string;
19375 /* Return the string to flip the GT bit on a CR. */
19376 char *
19377 output_e500_flip_gt_bit (rtx dst, rtx src)
19379 static char string[64];
19380 int a, b;
19382 gcc_assert (GET_CODE (dst) == REG && CR_REGNO_P (REGNO (dst))
19383 && GET_CODE (src) == REG && CR_REGNO_P (REGNO (src)));
19385 /* GT bit. */
19386 a = 4 * (REGNO (dst) - CR0_REGNO) + 1;
19387 b = 4 * (REGNO (src) - CR0_REGNO) + 1;
19389 sprintf (string, "crnot %d,%d", a, b);
19390 return string;
19393 /* Return insn for VSX or Altivec comparisons. */
19395 static rtx
19396 rs6000_emit_vector_compare_inner (enum rtx_code code, rtx op0, rtx op1)
19398 rtx mask;
19399 enum machine_mode mode = GET_MODE (op0);
19401 switch (code)
19403 default:
19404 break;
19406 case GE:
19407 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
19408 return NULL_RTX;
19410 case EQ:
19411 case GT:
19412 case GTU:
19413 case ORDERED:
19414 case UNORDERED:
19415 case UNEQ:
19416 case LTGT:
19417 mask = gen_reg_rtx (mode);
19418 emit_insn (gen_rtx_SET (VOIDmode,
19419 mask,
19420 gen_rtx_fmt_ee (code, mode, op0, op1)));
19421 return mask;
19424 return NULL_RTX;
19427 /* Emit vector compare for operands OP0 and OP1 using code RCODE.
19428 DMODE is expected destination mode. This is a recursive function. */
19430 static rtx
19431 rs6000_emit_vector_compare (enum rtx_code rcode,
19432 rtx op0, rtx op1,
19433 enum machine_mode dmode)
19435 rtx mask;
19436 bool swap_operands = false;
19437 bool try_again = false;
19439 gcc_assert (VECTOR_UNIT_ALTIVEC_OR_VSX_P (dmode));
19440 gcc_assert (GET_MODE (op0) == GET_MODE (op1));
19442 /* See if the comparison works as is. */
19443 mask = rs6000_emit_vector_compare_inner (rcode, op0, op1);
19444 if (mask)
19445 return mask;
19447 switch (rcode)
19449 case LT:
19450 rcode = GT;
19451 swap_operands = true;
19452 try_again = true;
19453 break;
19454 case LTU:
19455 rcode = GTU;
19456 swap_operands = true;
19457 try_again = true;
19458 break;
19459 case NE:
19460 case UNLE:
19461 case UNLT:
19462 case UNGE:
19463 case UNGT:
19464 /* Invert condition and try again.
19465 e.g., A != B becomes ~(A==B). */
19467 enum rtx_code rev_code;
19468 enum insn_code nor_code;
19469 rtx mask2;
19471 rev_code = reverse_condition_maybe_unordered (rcode);
19472 if (rev_code == UNKNOWN)
19473 return NULL_RTX;
19475 nor_code = optab_handler (one_cmpl_optab, dmode);
19476 if (nor_code == CODE_FOR_nothing)
19477 return NULL_RTX;
19479 mask2 = rs6000_emit_vector_compare (rev_code, op0, op1, dmode);
19480 if (!mask2)
19481 return NULL_RTX;
19483 mask = gen_reg_rtx (dmode);
19484 emit_insn (GEN_FCN (nor_code) (mask, mask2));
19485 return mask;
19487 break;
19488 case GE:
19489 case GEU:
19490 case LE:
19491 case LEU:
19492 /* Try GT/GTU/LT/LTU OR EQ */
19494 rtx c_rtx, eq_rtx;
19495 enum insn_code ior_code;
19496 enum rtx_code new_code;
19498 switch (rcode)
19500 case GE:
19501 new_code = GT;
19502 break;
19504 case GEU:
19505 new_code = GTU;
19506 break;
19508 case LE:
19509 new_code = LT;
19510 break;
19512 case LEU:
19513 new_code = LTU;
19514 break;
19516 default:
19517 gcc_unreachable ();
19520 ior_code = optab_handler (ior_optab, dmode);
19521 if (ior_code == CODE_FOR_nothing)
19522 return NULL_RTX;
19524 c_rtx = rs6000_emit_vector_compare (new_code, op0, op1, dmode);
19525 if (!c_rtx)
19526 return NULL_RTX;
19528 eq_rtx = rs6000_emit_vector_compare (EQ, op0, op1, dmode);
19529 if (!eq_rtx)
19530 return NULL_RTX;
19532 mask = gen_reg_rtx (dmode);
19533 emit_insn (GEN_FCN (ior_code) (mask, c_rtx, eq_rtx));
19534 return mask;
19536 break;
19537 default:
19538 return NULL_RTX;
19541 if (try_again)
19543 if (swap_operands)
19545 rtx tmp;
19546 tmp = op0;
19547 op0 = op1;
19548 op1 = tmp;
19551 mask = rs6000_emit_vector_compare_inner (rcode, op0, op1);
19552 if (mask)
19553 return mask;
19556 /* You only get two chances. */
19557 return NULL_RTX;
19560 /* Emit vector conditional expression. DEST is destination. OP_TRUE and
19561 OP_FALSE are two VEC_COND_EXPR operands. CC_OP0 and CC_OP1 are the two
19562 operands for the relation operation COND. */
19565 rs6000_emit_vector_cond_expr (rtx dest, rtx op_true, rtx op_false,
19566 rtx cond, rtx cc_op0, rtx cc_op1)
19568 enum machine_mode dest_mode = GET_MODE (dest);
19569 enum machine_mode mask_mode = GET_MODE (cc_op0);
19570 enum rtx_code rcode = GET_CODE (cond);
19571 enum machine_mode cc_mode = CCmode;
19572 rtx mask;
19573 rtx cond2;
19574 rtx tmp;
19575 bool invert_move = false;
19577 if (VECTOR_UNIT_NONE_P (dest_mode))
19578 return 0;
19580 gcc_assert (GET_MODE_SIZE (dest_mode) == GET_MODE_SIZE (mask_mode)
19581 && GET_MODE_NUNITS (dest_mode) == GET_MODE_NUNITS (mask_mode));
19583 switch (rcode)
19585 /* Swap operands if we can, and fall back to doing the operation as
19586 specified, and doing a NOR to invert the test. */
19587 case NE:
19588 case UNLE:
19589 case UNLT:
19590 case UNGE:
19591 case UNGT:
19592 /* Invert condition and try again.
19593 e.g., A = (B != C) ? D : E becomes A = (B == C) ? E : D. */
19594 invert_move = true;
19595 rcode = reverse_condition_maybe_unordered (rcode);
19596 if (rcode == UNKNOWN)
19597 return 0;
19598 break;
19600 /* Mark unsigned tests with CCUNSmode. */
19601 case GTU:
19602 case GEU:
19603 case LTU:
19604 case LEU:
19605 cc_mode = CCUNSmode;
19606 break;
19608 default:
19609 break;
19612 /* Get the vector mask for the given relational operations. */
19613 mask = rs6000_emit_vector_compare (rcode, cc_op0, cc_op1, mask_mode);
19615 if (!mask)
19616 return 0;
19618 if (invert_move)
19620 tmp = op_true;
19621 op_true = op_false;
19622 op_false = tmp;
19625 cond2 = gen_rtx_fmt_ee (NE, cc_mode, gen_lowpart (dest_mode, mask),
19626 CONST0_RTX (dest_mode));
19627 emit_insn (gen_rtx_SET (VOIDmode,
19628 dest,
19629 gen_rtx_IF_THEN_ELSE (dest_mode,
19630 cond2,
19631 op_true,
19632 op_false)));
19633 return 1;
19636 /* Emit a conditional move: move TRUE_COND to DEST if OP of the
19637 operands of the last comparison is nonzero/true, FALSE_COND if it
19638 is zero/false. Return 0 if the hardware has no such operation. */
19641 rs6000_emit_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
19643 enum rtx_code code = GET_CODE (op);
19644 rtx op0 = XEXP (op, 0);
19645 rtx op1 = XEXP (op, 1);
19646 REAL_VALUE_TYPE c1;
19647 enum machine_mode compare_mode = GET_MODE (op0);
19648 enum machine_mode result_mode = GET_MODE (dest);
19649 rtx temp;
19650 bool is_against_zero;
19652 /* These modes should always match. */
19653 if (GET_MODE (op1) != compare_mode
19654 /* In the isel case however, we can use a compare immediate, so
19655 op1 may be a small constant. */
19656 && (!TARGET_ISEL || !short_cint_operand (op1, VOIDmode)))
19657 return 0;
19658 if (GET_MODE (true_cond) != result_mode)
19659 return 0;
19660 if (GET_MODE (false_cond) != result_mode)
19661 return 0;
19663 /* Don't allow using floating point comparisons for integer results for
19664 now. */
19665 if (FLOAT_MODE_P (compare_mode) && !FLOAT_MODE_P (result_mode))
19666 return 0;
19668 /* First, work out if the hardware can do this at all, or
19669 if it's too slow.... */
19670 if (!FLOAT_MODE_P (compare_mode))
19672 if (TARGET_ISEL)
19673 return rs6000_emit_int_cmove (dest, op, true_cond, false_cond);
19674 return 0;
19676 else if (TARGET_HARD_FLOAT && !TARGET_FPRS
19677 && SCALAR_FLOAT_MODE_P (compare_mode))
19678 return 0;
19680 is_against_zero = op1 == CONST0_RTX (compare_mode);
19682 /* A floating-point subtract might overflow, underflow, or produce
19683 an inexact result, thus changing the floating-point flags, so it
19684 can't be generated if we care about that. It's safe if one side
19685 of the construct is zero, since then no subtract will be
19686 generated. */
19687 if (SCALAR_FLOAT_MODE_P (compare_mode)
19688 && flag_trapping_math && ! is_against_zero)
19689 return 0;
19691 /* Eliminate half of the comparisons by switching operands, this
19692 makes the remaining code simpler. */
19693 if (code == UNLT || code == UNGT || code == UNORDERED || code == NE
19694 || code == LTGT || code == LT || code == UNLE)
19696 code = reverse_condition_maybe_unordered (code);
19697 temp = true_cond;
19698 true_cond = false_cond;
19699 false_cond = temp;
19702 /* UNEQ and LTGT take four instructions for a comparison with zero,
19703 it'll probably be faster to use a branch here too. */
19704 if (code == UNEQ && HONOR_NANS (compare_mode))
19705 return 0;
19707 if (GET_CODE (op1) == CONST_DOUBLE)
19708 REAL_VALUE_FROM_CONST_DOUBLE (c1, op1);
19710 /* We're going to try to implement comparisons by performing
19711 a subtract, then comparing against zero. Unfortunately,
19712 Inf - Inf is NaN which is not zero, and so if we don't
19713 know that the operand is finite and the comparison
19714 would treat EQ different to UNORDERED, we can't do it. */
19715 if (HONOR_INFINITIES (compare_mode)
19716 && code != GT && code != UNGE
19717 && (GET_CODE (op1) != CONST_DOUBLE || real_isinf (&c1))
19718 /* Constructs of the form (a OP b ? a : b) are safe. */
19719 && ((! rtx_equal_p (op0, false_cond) && ! rtx_equal_p (op1, false_cond))
19720 || (! rtx_equal_p (op0, true_cond)
19721 && ! rtx_equal_p (op1, true_cond))))
19722 return 0;
19724 /* At this point we know we can use fsel. */
19726 /* Reduce the comparison to a comparison against zero. */
19727 if (! is_against_zero)
19729 temp = gen_reg_rtx (compare_mode);
19730 emit_insn (gen_rtx_SET (VOIDmode, temp,
19731 gen_rtx_MINUS (compare_mode, op0, op1)));
19732 op0 = temp;
19733 op1 = CONST0_RTX (compare_mode);
19736 /* If we don't care about NaNs we can reduce some of the comparisons
19737 down to faster ones. */
19738 if (! HONOR_NANS (compare_mode))
19739 switch (code)
19741 case GT:
19742 code = LE;
19743 temp = true_cond;
19744 true_cond = false_cond;
19745 false_cond = temp;
19746 break;
19747 case UNGE:
19748 code = GE;
19749 break;
19750 case UNEQ:
19751 code = EQ;
19752 break;
19753 default:
19754 break;
19757 /* Now, reduce everything down to a GE. */
19758 switch (code)
19760 case GE:
19761 break;
19763 case LE:
19764 temp = gen_reg_rtx (compare_mode);
19765 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
19766 op0 = temp;
19767 break;
19769 case ORDERED:
19770 temp = gen_reg_rtx (compare_mode);
19771 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_ABS (compare_mode, op0)));
19772 op0 = temp;
19773 break;
19775 case EQ:
19776 temp = gen_reg_rtx (compare_mode);
19777 emit_insn (gen_rtx_SET (VOIDmode, temp,
19778 gen_rtx_NEG (compare_mode,
19779 gen_rtx_ABS (compare_mode, op0))));
19780 op0 = temp;
19781 break;
19783 case UNGE:
19784 /* a UNGE 0 <-> (a GE 0 || -a UNLT 0) */
19785 temp = gen_reg_rtx (result_mode);
19786 emit_insn (gen_rtx_SET (VOIDmode, temp,
19787 gen_rtx_IF_THEN_ELSE (result_mode,
19788 gen_rtx_GE (VOIDmode,
19789 op0, op1),
19790 true_cond, false_cond)));
19791 false_cond = true_cond;
19792 true_cond = temp;
19794 temp = gen_reg_rtx (compare_mode);
19795 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
19796 op0 = temp;
19797 break;
19799 case GT:
19800 /* a GT 0 <-> (a GE 0 && -a UNLT 0) */
19801 temp = gen_reg_rtx (result_mode);
19802 emit_insn (gen_rtx_SET (VOIDmode, temp,
19803 gen_rtx_IF_THEN_ELSE (result_mode,
19804 gen_rtx_GE (VOIDmode,
19805 op0, op1),
19806 true_cond, false_cond)));
19807 true_cond = false_cond;
19808 false_cond = temp;
19810 temp = gen_reg_rtx (compare_mode);
19811 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
19812 op0 = temp;
19813 break;
19815 default:
19816 gcc_unreachable ();
19819 emit_insn (gen_rtx_SET (VOIDmode, dest,
19820 gen_rtx_IF_THEN_ELSE (result_mode,
19821 gen_rtx_GE (VOIDmode,
19822 op0, op1),
19823 true_cond, false_cond)));
19824 return 1;
19827 /* Same as above, but for ints (isel). */
19829 static int
19830 rs6000_emit_int_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
19832 rtx condition_rtx, cr;
19833 enum machine_mode mode = GET_MODE (dest);
19834 enum rtx_code cond_code;
19835 rtx (*isel_func) (rtx, rtx, rtx, rtx, rtx);
19836 bool signedp;
19838 if (mode != SImode && (!TARGET_POWERPC64 || mode != DImode))
19839 return 0;
19841 /* We still have to do the compare, because isel doesn't do a
19842 compare, it just looks at the CRx bits set by a previous compare
19843 instruction. */
19844 condition_rtx = rs6000_generate_compare (op, mode);
19845 cond_code = GET_CODE (condition_rtx);
19846 cr = XEXP (condition_rtx, 0);
19847 signedp = GET_MODE (cr) == CCmode;
19849 isel_func = (mode == SImode
19850 ? (signedp ? gen_isel_signed_si : gen_isel_unsigned_si)
19851 : (signedp ? gen_isel_signed_di : gen_isel_unsigned_di));
19853 switch (cond_code)
19855 case LT: case GT: case LTU: case GTU: case EQ:
19856 /* isel handles these directly. */
19857 break;
19859 default:
19860 /* We need to swap the sense of the comparison. */
19862 rtx t = true_cond;
19863 true_cond = false_cond;
19864 false_cond = t;
19865 PUT_CODE (condition_rtx, reverse_condition (cond_code));
19867 break;
19870 false_cond = force_reg (mode, false_cond);
19871 if (true_cond != const0_rtx)
19872 true_cond = force_reg (mode, true_cond);
19874 emit_insn (isel_func (dest, condition_rtx, true_cond, false_cond, cr));
19876 return 1;
19879 const char *
19880 output_isel (rtx *operands)
19882 enum rtx_code code;
19884 code = GET_CODE (operands[1]);
19886 if (code == GE || code == GEU || code == LE || code == LEU || code == NE)
19888 gcc_assert (GET_CODE (operands[2]) == REG
19889 && GET_CODE (operands[3]) == REG);
19890 PUT_CODE (operands[1], reverse_condition (code));
19891 return "isel %0,%3,%2,%j1";
19894 return "isel %0,%2,%3,%j1";
19897 void
19898 rs6000_emit_minmax (rtx dest, enum rtx_code code, rtx op0, rtx op1)
19900 enum machine_mode mode = GET_MODE (op0);
19901 enum rtx_code c;
19902 rtx target;
19904 /* VSX/altivec have direct min/max insns. */
19905 if ((code == SMAX || code == SMIN)
19906 && (VECTOR_UNIT_ALTIVEC_OR_VSX_P (mode)
19907 || (mode == SFmode && VECTOR_UNIT_VSX_P (DFmode))))
19909 emit_insn (gen_rtx_SET (VOIDmode,
19910 dest,
19911 gen_rtx_fmt_ee (code, mode, op0, op1)));
19912 return;
19915 if (code == SMAX || code == SMIN)
19916 c = GE;
19917 else
19918 c = GEU;
19920 if (code == SMAX || code == UMAX)
19921 target = emit_conditional_move (dest, c, op0, op1, mode,
19922 op0, op1, mode, 0);
19923 else
19924 target = emit_conditional_move (dest, c, op0, op1, mode,
19925 op1, op0, mode, 0);
19926 gcc_assert (target);
19927 if (target != dest)
19928 emit_move_insn (dest, target);
19931 /* A subroutine of the atomic operation splitters. Jump to LABEL if
19932 COND is true. Mark the jump as unlikely to be taken. */
19934 static void
19935 emit_unlikely_jump (rtx cond, rtx label)
19937 int very_unlikely = REG_BR_PROB_BASE / 100 - 1;
19938 rtx x;
19940 x = gen_rtx_IF_THEN_ELSE (VOIDmode, cond, label, pc_rtx);
19941 x = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, x));
19942 add_int_reg_note (x, REG_BR_PROB, very_unlikely);
19945 /* A subroutine of the atomic operation splitters. Emit a load-locked
19946 instruction in MODE. For QI/HImode, possibly use a pattern than includes
19947 the zero_extend operation. */
19949 static void
19950 emit_load_locked (enum machine_mode mode, rtx reg, rtx mem)
19952 rtx (*fn) (rtx, rtx) = NULL;
19954 switch (mode)
19956 case QImode:
19957 fn = gen_load_lockedqi;
19958 break;
19959 case HImode:
19960 fn = gen_load_lockedhi;
19961 break;
19962 case SImode:
19963 if (GET_MODE (mem) == QImode)
19964 fn = gen_load_lockedqi_si;
19965 else if (GET_MODE (mem) == HImode)
19966 fn = gen_load_lockedhi_si;
19967 else
19968 fn = gen_load_lockedsi;
19969 break;
19970 case DImode:
19971 fn = gen_load_lockeddi;
19972 break;
19973 case TImode:
19974 fn = gen_load_lockedti;
19975 break;
19976 default:
19977 gcc_unreachable ();
19979 emit_insn (fn (reg, mem));
19982 /* A subroutine of the atomic operation splitters. Emit a store-conditional
19983 instruction in MODE. */
19985 static void
19986 emit_store_conditional (enum machine_mode mode, rtx res, rtx mem, rtx val)
19988 rtx (*fn) (rtx, rtx, rtx) = NULL;
19990 switch (mode)
19992 case QImode:
19993 fn = gen_store_conditionalqi;
19994 break;
19995 case HImode:
19996 fn = gen_store_conditionalhi;
19997 break;
19998 case SImode:
19999 fn = gen_store_conditionalsi;
20000 break;
20001 case DImode:
20002 fn = gen_store_conditionaldi;
20003 break;
20004 case TImode:
20005 fn = gen_store_conditionalti;
20006 break;
20007 default:
20008 gcc_unreachable ();
20011 /* Emit sync before stwcx. to address PPC405 Erratum. */
20012 if (PPC405_ERRATUM77)
20013 emit_insn (gen_hwsync ());
20015 emit_insn (fn (res, mem, val));
20018 /* Expand barriers before and after a load_locked/store_cond sequence. */
20020 static rtx
20021 rs6000_pre_atomic_barrier (rtx mem, enum memmodel model)
20023 rtx addr = XEXP (mem, 0);
20024 int strict_p = (reload_in_progress || reload_completed);
20026 if (!legitimate_indirect_address_p (addr, strict_p)
20027 && !legitimate_indexed_address_p (addr, strict_p))
20029 addr = force_reg (Pmode, addr);
20030 mem = replace_equiv_address_nv (mem, addr);
20033 switch (model)
20035 case MEMMODEL_RELAXED:
20036 case MEMMODEL_CONSUME:
20037 case MEMMODEL_ACQUIRE:
20038 break;
20039 case MEMMODEL_RELEASE:
20040 case MEMMODEL_ACQ_REL:
20041 emit_insn (gen_lwsync ());
20042 break;
20043 case MEMMODEL_SEQ_CST:
20044 emit_insn (gen_hwsync ());
20045 break;
20046 default:
20047 gcc_unreachable ();
20049 return mem;
20052 static void
20053 rs6000_post_atomic_barrier (enum memmodel model)
20055 switch (model)
20057 case MEMMODEL_RELAXED:
20058 case MEMMODEL_CONSUME:
20059 case MEMMODEL_RELEASE:
20060 break;
20061 case MEMMODEL_ACQUIRE:
20062 case MEMMODEL_ACQ_REL:
20063 case MEMMODEL_SEQ_CST:
20064 emit_insn (gen_isync ());
20065 break;
20066 default:
20067 gcc_unreachable ();
20071 /* A subroutine of the various atomic expanders. For sub-word operations,
20072 we must adjust things to operate on SImode. Given the original MEM,
20073 return a new aligned memory. Also build and return the quantities by
20074 which to shift and mask. */
20076 static rtx
20077 rs6000_adjust_atomic_subword (rtx orig_mem, rtx *pshift, rtx *pmask)
20079 rtx addr, align, shift, mask, mem;
20080 HOST_WIDE_INT shift_mask;
20081 enum machine_mode mode = GET_MODE (orig_mem);
20083 /* For smaller modes, we have to implement this via SImode. */
20084 shift_mask = (mode == QImode ? 0x18 : 0x10);
20086 addr = XEXP (orig_mem, 0);
20087 addr = force_reg (GET_MODE (addr), addr);
20089 /* Aligned memory containing subword. Generate a new memory. We
20090 do not want any of the existing MEM_ATTR data, as we're now
20091 accessing memory outside the original object. */
20092 align = expand_simple_binop (Pmode, AND, addr, GEN_INT (-4),
20093 NULL_RTX, 1, OPTAB_LIB_WIDEN);
20094 mem = gen_rtx_MEM (SImode, align);
20095 MEM_VOLATILE_P (mem) = MEM_VOLATILE_P (orig_mem);
20096 if (MEM_ALIAS_SET (orig_mem) == ALIAS_SET_MEMORY_BARRIER)
20097 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
20099 /* Shift amount for subword relative to aligned word. */
20100 shift = gen_reg_rtx (SImode);
20101 addr = gen_lowpart (SImode, addr);
20102 emit_insn (gen_rlwinm (shift, addr, GEN_INT (3), GEN_INT (shift_mask)));
20103 if (BYTES_BIG_ENDIAN)
20104 shift = expand_simple_binop (SImode, XOR, shift, GEN_INT (shift_mask),
20105 shift, 1, OPTAB_LIB_WIDEN);
20106 *pshift = shift;
20108 /* Mask for insertion. */
20109 mask = expand_simple_binop (SImode, ASHIFT, GEN_INT (GET_MODE_MASK (mode)),
20110 shift, NULL_RTX, 1, OPTAB_LIB_WIDEN);
20111 *pmask = mask;
20113 return mem;
20116 /* A subroutine of the various atomic expanders. For sub-word operands,
20117 combine OLDVAL and NEWVAL via MASK. Returns a new pseduo. */
20119 static rtx
20120 rs6000_mask_atomic_subword (rtx oldval, rtx newval, rtx mask)
20122 rtx x;
20124 x = gen_reg_rtx (SImode);
20125 emit_insn (gen_rtx_SET (VOIDmode, x,
20126 gen_rtx_AND (SImode,
20127 gen_rtx_NOT (SImode, mask),
20128 oldval)));
20130 x = expand_simple_binop (SImode, IOR, newval, x, x, 1, OPTAB_LIB_WIDEN);
20132 return x;
20135 /* A subroutine of the various atomic expanders. For sub-word operands,
20136 extract WIDE to NARROW via SHIFT. */
20138 static void
20139 rs6000_finish_atomic_subword (rtx narrow, rtx wide, rtx shift)
20141 wide = expand_simple_binop (SImode, LSHIFTRT, wide, shift,
20142 wide, 1, OPTAB_LIB_WIDEN);
20143 emit_move_insn (narrow, gen_lowpart (GET_MODE (narrow), wide));
20146 /* Expand an atomic compare and swap operation. */
20148 void
20149 rs6000_expand_atomic_compare_and_swap (rtx operands[])
20151 rtx boolval, retval, mem, oldval, newval, cond;
20152 rtx label1, label2, x, mask, shift;
20153 enum machine_mode mode, orig_mode;
20154 enum memmodel mod_s, mod_f;
20155 bool is_weak;
20157 boolval = operands[0];
20158 retval = operands[1];
20159 mem = operands[2];
20160 oldval = operands[3];
20161 newval = operands[4];
20162 is_weak = (INTVAL (operands[5]) != 0);
20163 mod_s = (enum memmodel) INTVAL (operands[6]);
20164 mod_f = (enum memmodel) INTVAL (operands[7]);
20165 orig_mode = mode = GET_MODE (mem);
20167 mask = shift = NULL_RTX;
20168 if (mode == QImode || mode == HImode)
20170 /* Before power8, we didn't have access to lbarx/lharx, so generate a
20171 lwarx and shift/mask operations. With power8, we need to do the
20172 comparison in SImode, but the store is still done in QI/HImode. */
20173 oldval = convert_modes (SImode, mode, oldval, 1);
20175 if (!TARGET_SYNC_HI_QI)
20177 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
20179 /* Shift and mask OLDVAL into position with the word. */
20180 oldval = expand_simple_binop (SImode, ASHIFT, oldval, shift,
20181 NULL_RTX, 1, OPTAB_LIB_WIDEN);
20183 /* Shift and mask NEWVAL into position within the word. */
20184 newval = convert_modes (SImode, mode, newval, 1);
20185 newval = expand_simple_binop (SImode, ASHIFT, newval, shift,
20186 NULL_RTX, 1, OPTAB_LIB_WIDEN);
20189 /* Prepare to adjust the return value. */
20190 retval = gen_reg_rtx (SImode);
20191 mode = SImode;
20193 else if (reg_overlap_mentioned_p (retval, oldval))
20194 oldval = copy_to_reg (oldval);
20196 mem = rs6000_pre_atomic_barrier (mem, mod_s);
20198 label1 = NULL_RTX;
20199 if (!is_weak)
20201 label1 = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
20202 emit_label (XEXP (label1, 0));
20204 label2 = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
20206 emit_load_locked (mode, retval, mem);
20208 x = retval;
20209 if (mask)
20211 x = expand_simple_binop (SImode, AND, retval, mask,
20212 NULL_RTX, 1, OPTAB_LIB_WIDEN);
20215 cond = gen_reg_rtx (CCmode);
20216 /* If we have TImode, synthesize a comparison. */
20217 if (mode != TImode)
20218 x = gen_rtx_COMPARE (CCmode, x, oldval);
20219 else
20221 rtx xor1_result = gen_reg_rtx (DImode);
20222 rtx xor2_result = gen_reg_rtx (DImode);
20223 rtx or_result = gen_reg_rtx (DImode);
20224 rtx new_word0 = simplify_gen_subreg (DImode, x, TImode, 0);
20225 rtx new_word1 = simplify_gen_subreg (DImode, x, TImode, 8);
20226 rtx old_word0 = simplify_gen_subreg (DImode, oldval, TImode, 0);
20227 rtx old_word1 = simplify_gen_subreg (DImode, oldval, TImode, 8);
20229 emit_insn (gen_xordi3 (xor1_result, new_word0, old_word0));
20230 emit_insn (gen_xordi3 (xor2_result, new_word1, old_word1));
20231 emit_insn (gen_iordi3 (or_result, xor1_result, xor2_result));
20232 x = gen_rtx_COMPARE (CCmode, or_result, const0_rtx);
20235 emit_insn (gen_rtx_SET (VOIDmode, cond, x));
20237 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
20238 emit_unlikely_jump (x, label2);
20240 x = newval;
20241 if (mask)
20242 x = rs6000_mask_atomic_subword (retval, newval, mask);
20244 emit_store_conditional (orig_mode, cond, mem, x);
20246 if (!is_weak)
20248 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
20249 emit_unlikely_jump (x, label1);
20252 if (mod_f != MEMMODEL_RELAXED)
20253 emit_label (XEXP (label2, 0));
20255 rs6000_post_atomic_barrier (mod_s);
20257 if (mod_f == MEMMODEL_RELAXED)
20258 emit_label (XEXP (label2, 0));
20260 if (shift)
20261 rs6000_finish_atomic_subword (operands[1], retval, shift);
20262 else if (mode != GET_MODE (operands[1]))
20263 convert_move (operands[1], retval, 1);
20265 /* In all cases, CR0 contains EQ on success, and NE on failure. */
20266 x = gen_rtx_EQ (SImode, cond, const0_rtx);
20267 emit_insn (gen_rtx_SET (VOIDmode, boolval, x));
20270 /* Expand an atomic exchange operation. */
20272 void
20273 rs6000_expand_atomic_exchange (rtx operands[])
20275 rtx retval, mem, val, cond;
20276 enum machine_mode mode;
20277 enum memmodel model;
20278 rtx label, x, mask, shift;
20280 retval = operands[0];
20281 mem = operands[1];
20282 val = operands[2];
20283 model = (enum memmodel) INTVAL (operands[3]);
20284 mode = GET_MODE (mem);
20286 mask = shift = NULL_RTX;
20287 if (!TARGET_SYNC_HI_QI && (mode == QImode || mode == HImode))
20289 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
20291 /* Shift and mask VAL into position with the word. */
20292 val = convert_modes (SImode, mode, val, 1);
20293 val = expand_simple_binop (SImode, ASHIFT, val, shift,
20294 NULL_RTX, 1, OPTAB_LIB_WIDEN);
20296 /* Prepare to adjust the return value. */
20297 retval = gen_reg_rtx (SImode);
20298 mode = SImode;
20301 mem = rs6000_pre_atomic_barrier (mem, model);
20303 label = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
20304 emit_label (XEXP (label, 0));
20306 emit_load_locked (mode, retval, mem);
20308 x = val;
20309 if (mask)
20310 x = rs6000_mask_atomic_subword (retval, val, mask);
20312 cond = gen_reg_rtx (CCmode);
20313 emit_store_conditional (mode, cond, mem, x);
20315 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
20316 emit_unlikely_jump (x, label);
20318 rs6000_post_atomic_barrier (model);
20320 if (shift)
20321 rs6000_finish_atomic_subword (operands[0], retval, shift);
20324 /* Expand an atomic fetch-and-operate pattern. CODE is the binary operation
20325 to perform. MEM is the memory on which to operate. VAL is the second
20326 operand of the binary operator. BEFORE and AFTER are optional locations to
20327 return the value of MEM either before of after the operation. MODEL_RTX
20328 is a CONST_INT containing the memory model to use. */
20330 void
20331 rs6000_expand_atomic_op (enum rtx_code code, rtx mem, rtx val,
20332 rtx orig_before, rtx orig_after, rtx model_rtx)
20334 enum memmodel model = (enum memmodel) INTVAL (model_rtx);
20335 enum machine_mode mode = GET_MODE (mem);
20336 enum machine_mode store_mode = mode;
20337 rtx label, x, cond, mask, shift;
20338 rtx before = orig_before, after = orig_after;
20340 mask = shift = NULL_RTX;
20341 /* On power8, we want to use SImode for the operation. On previous systems,
20342 use the operation in a subword and shift/mask to get the proper byte or
20343 halfword. */
20344 if (mode == QImode || mode == HImode)
20346 if (TARGET_SYNC_HI_QI)
20348 val = convert_modes (SImode, mode, val, 1);
20350 /* Prepare to adjust the return value. */
20351 before = gen_reg_rtx (SImode);
20352 if (after)
20353 after = gen_reg_rtx (SImode);
20354 mode = SImode;
20356 else
20358 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
20360 /* Shift and mask VAL into position with the word. */
20361 val = convert_modes (SImode, mode, val, 1);
20362 val = expand_simple_binop (SImode, ASHIFT, val, shift,
20363 NULL_RTX, 1, OPTAB_LIB_WIDEN);
20365 switch (code)
20367 case IOR:
20368 case XOR:
20369 /* We've already zero-extended VAL. That is sufficient to
20370 make certain that it does not affect other bits. */
20371 mask = NULL;
20372 break;
20374 case AND:
20375 /* If we make certain that all of the other bits in VAL are
20376 set, that will be sufficient to not affect other bits. */
20377 x = gen_rtx_NOT (SImode, mask);
20378 x = gen_rtx_IOR (SImode, x, val);
20379 emit_insn (gen_rtx_SET (VOIDmode, val, x));
20380 mask = NULL;
20381 break;
20383 case NOT:
20384 case PLUS:
20385 case MINUS:
20386 /* These will all affect bits outside the field and need
20387 adjustment via MASK within the loop. */
20388 break;
20390 default:
20391 gcc_unreachable ();
20394 /* Prepare to adjust the return value. */
20395 before = gen_reg_rtx (SImode);
20396 if (after)
20397 after = gen_reg_rtx (SImode);
20398 store_mode = mode = SImode;
20402 mem = rs6000_pre_atomic_barrier (mem, model);
20404 label = gen_label_rtx ();
20405 emit_label (label);
20406 label = gen_rtx_LABEL_REF (VOIDmode, label);
20408 if (before == NULL_RTX)
20409 before = gen_reg_rtx (mode);
20411 emit_load_locked (mode, before, mem);
20413 if (code == NOT)
20415 x = expand_simple_binop (mode, AND, before, val,
20416 NULL_RTX, 1, OPTAB_LIB_WIDEN);
20417 after = expand_simple_unop (mode, NOT, x, after, 1);
20419 else
20421 after = expand_simple_binop (mode, code, before, val,
20422 after, 1, OPTAB_LIB_WIDEN);
20425 x = after;
20426 if (mask)
20428 x = expand_simple_binop (SImode, AND, after, mask,
20429 NULL_RTX, 1, OPTAB_LIB_WIDEN);
20430 x = rs6000_mask_atomic_subword (before, x, mask);
20432 else if (store_mode != mode)
20433 x = convert_modes (store_mode, mode, x, 1);
20435 cond = gen_reg_rtx (CCmode);
20436 emit_store_conditional (store_mode, cond, mem, x);
20438 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
20439 emit_unlikely_jump (x, label);
20441 rs6000_post_atomic_barrier (model);
20443 if (shift)
20445 /* QImode/HImode on machines without lbarx/lharx where we do a lwarx and
20446 then do the calcuations in a SImode register. */
20447 if (orig_before)
20448 rs6000_finish_atomic_subword (orig_before, before, shift);
20449 if (orig_after)
20450 rs6000_finish_atomic_subword (orig_after, after, shift);
20452 else if (store_mode != mode)
20454 /* QImode/HImode on machines with lbarx/lharx where we do the native
20455 operation and then do the calcuations in a SImode register. */
20456 if (orig_before)
20457 convert_move (orig_before, before, 1);
20458 if (orig_after)
20459 convert_move (orig_after, after, 1);
20461 else if (orig_after && after != orig_after)
20462 emit_move_insn (orig_after, after);
20465 /* Emit instructions to move SRC to DST. Called by splitters for
20466 multi-register moves. It will emit at most one instruction for
20467 each register that is accessed; that is, it won't emit li/lis pairs
20468 (or equivalent for 64-bit code). One of SRC or DST must be a hard
20469 register. */
20471 void
20472 rs6000_split_multireg_move (rtx dst, rtx src)
20474 /* The register number of the first register being moved. */
20475 int reg;
20476 /* The mode that is to be moved. */
20477 enum machine_mode mode;
20478 /* The mode that the move is being done in, and its size. */
20479 enum machine_mode reg_mode;
20480 int reg_mode_size;
20481 /* The number of registers that will be moved. */
20482 int nregs;
20484 reg = REG_P (dst) ? REGNO (dst) : REGNO (src);
20485 mode = GET_MODE (dst);
20486 nregs = hard_regno_nregs[reg][mode];
20487 if (FP_REGNO_P (reg))
20488 reg_mode = DECIMAL_FLOAT_MODE_P (mode) ? DDmode :
20489 ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? DFmode : SFmode);
20490 else if (ALTIVEC_REGNO_P (reg))
20491 reg_mode = V16QImode;
20492 else if (TARGET_E500_DOUBLE && mode == TFmode)
20493 reg_mode = DFmode;
20494 else
20495 reg_mode = word_mode;
20496 reg_mode_size = GET_MODE_SIZE (reg_mode);
20498 gcc_assert (reg_mode_size * nregs == GET_MODE_SIZE (mode));
20500 /* TDmode residing in FP registers is special, since the ISA requires that
20501 the lower-numbered word of a register pair is always the most significant
20502 word, even in little-endian mode. This does not match the usual subreg
20503 semantics, so we cannnot use simplify_gen_subreg in those cases. Access
20504 the appropriate constituent registers "by hand" in little-endian mode.
20506 Note we do not need to check for destructive overlap here since TDmode
20507 can only reside in even/odd register pairs. */
20508 if (FP_REGNO_P (reg) && DECIMAL_FLOAT_MODE_P (mode) && !BYTES_BIG_ENDIAN)
20510 rtx p_src, p_dst;
20511 int i;
20513 for (i = 0; i < nregs; i++)
20515 if (REG_P (src) && FP_REGNO_P (REGNO (src)))
20516 p_src = gen_rtx_REG (reg_mode, REGNO (src) + nregs - 1 - i);
20517 else
20518 p_src = simplify_gen_subreg (reg_mode, src, mode,
20519 i * reg_mode_size);
20521 if (REG_P (dst) && FP_REGNO_P (REGNO (dst)))
20522 p_dst = gen_rtx_REG (reg_mode, REGNO (dst) + nregs - 1 - i);
20523 else
20524 p_dst = simplify_gen_subreg (reg_mode, dst, mode,
20525 i * reg_mode_size);
20527 emit_insn (gen_rtx_SET (VOIDmode, p_dst, p_src));
20530 return;
20533 if (REG_P (src) && REG_P (dst) && (REGNO (src) < REGNO (dst)))
20535 /* Move register range backwards, if we might have destructive
20536 overlap. */
20537 int i;
20538 for (i = nregs - 1; i >= 0; i--)
20539 emit_insn (gen_rtx_SET (VOIDmode,
20540 simplify_gen_subreg (reg_mode, dst, mode,
20541 i * reg_mode_size),
20542 simplify_gen_subreg (reg_mode, src, mode,
20543 i * reg_mode_size)));
20545 else
20547 int i;
20548 int j = -1;
20549 bool used_update = false;
20550 rtx restore_basereg = NULL_RTX;
20552 if (MEM_P (src) && INT_REGNO_P (reg))
20554 rtx breg;
20556 if (GET_CODE (XEXP (src, 0)) == PRE_INC
20557 || GET_CODE (XEXP (src, 0)) == PRE_DEC)
20559 rtx delta_rtx;
20560 breg = XEXP (XEXP (src, 0), 0);
20561 delta_rtx = (GET_CODE (XEXP (src, 0)) == PRE_INC
20562 ? GEN_INT (GET_MODE_SIZE (GET_MODE (src)))
20563 : GEN_INT (-GET_MODE_SIZE (GET_MODE (src))));
20564 emit_insn (gen_add3_insn (breg, breg, delta_rtx));
20565 src = replace_equiv_address (src, breg);
20567 else if (! rs6000_offsettable_memref_p (src, reg_mode))
20569 if (GET_CODE (XEXP (src, 0)) == PRE_MODIFY)
20571 rtx basereg = XEXP (XEXP (src, 0), 0);
20572 if (TARGET_UPDATE)
20574 rtx ndst = simplify_gen_subreg (reg_mode, dst, mode, 0);
20575 emit_insn (gen_rtx_SET (VOIDmode, ndst,
20576 gen_rtx_MEM (reg_mode, XEXP (src, 0))));
20577 used_update = true;
20579 else
20580 emit_insn (gen_rtx_SET (VOIDmode, basereg,
20581 XEXP (XEXP (src, 0), 1)));
20582 src = replace_equiv_address (src, basereg);
20584 else
20586 rtx basereg = gen_rtx_REG (Pmode, reg);
20587 emit_insn (gen_rtx_SET (VOIDmode, basereg, XEXP (src, 0)));
20588 src = replace_equiv_address (src, basereg);
20592 breg = XEXP (src, 0);
20593 if (GET_CODE (breg) == PLUS || GET_CODE (breg) == LO_SUM)
20594 breg = XEXP (breg, 0);
20596 /* If the base register we are using to address memory is
20597 also a destination reg, then change that register last. */
20598 if (REG_P (breg)
20599 && REGNO (breg) >= REGNO (dst)
20600 && REGNO (breg) < REGNO (dst) + nregs)
20601 j = REGNO (breg) - REGNO (dst);
20603 else if (MEM_P (dst) && INT_REGNO_P (reg))
20605 rtx breg;
20607 if (GET_CODE (XEXP (dst, 0)) == PRE_INC
20608 || GET_CODE (XEXP (dst, 0)) == PRE_DEC)
20610 rtx delta_rtx;
20611 breg = XEXP (XEXP (dst, 0), 0);
20612 delta_rtx = (GET_CODE (XEXP (dst, 0)) == PRE_INC
20613 ? GEN_INT (GET_MODE_SIZE (GET_MODE (dst)))
20614 : GEN_INT (-GET_MODE_SIZE (GET_MODE (dst))));
20616 /* We have to update the breg before doing the store.
20617 Use store with update, if available. */
20619 if (TARGET_UPDATE)
20621 rtx nsrc = simplify_gen_subreg (reg_mode, src, mode, 0);
20622 emit_insn (TARGET_32BIT
20623 ? (TARGET_POWERPC64
20624 ? gen_movdi_si_update (breg, breg, delta_rtx, nsrc)
20625 : gen_movsi_update (breg, breg, delta_rtx, nsrc))
20626 : gen_movdi_di_update (breg, breg, delta_rtx, nsrc));
20627 used_update = true;
20629 else
20630 emit_insn (gen_add3_insn (breg, breg, delta_rtx));
20631 dst = replace_equiv_address (dst, breg);
20633 else if (!rs6000_offsettable_memref_p (dst, reg_mode)
20634 && GET_CODE (XEXP (dst, 0)) != LO_SUM)
20636 if (GET_CODE (XEXP (dst, 0)) == PRE_MODIFY)
20638 rtx basereg = XEXP (XEXP (dst, 0), 0);
20639 if (TARGET_UPDATE)
20641 rtx nsrc = simplify_gen_subreg (reg_mode, src, mode, 0);
20642 emit_insn (gen_rtx_SET (VOIDmode,
20643 gen_rtx_MEM (reg_mode, XEXP (dst, 0)), nsrc));
20644 used_update = true;
20646 else
20647 emit_insn (gen_rtx_SET (VOIDmode, basereg,
20648 XEXP (XEXP (dst, 0), 1)));
20649 dst = replace_equiv_address (dst, basereg);
20651 else
20653 rtx basereg = XEXP (XEXP (dst, 0), 0);
20654 rtx offsetreg = XEXP (XEXP (dst, 0), 1);
20655 gcc_assert (GET_CODE (XEXP (dst, 0)) == PLUS
20656 && REG_P (basereg)
20657 && REG_P (offsetreg)
20658 && REGNO (basereg) != REGNO (offsetreg));
20659 if (REGNO (basereg) == 0)
20661 rtx tmp = offsetreg;
20662 offsetreg = basereg;
20663 basereg = tmp;
20665 emit_insn (gen_add3_insn (basereg, basereg, offsetreg));
20666 restore_basereg = gen_sub3_insn (basereg, basereg, offsetreg);
20667 dst = replace_equiv_address (dst, basereg);
20670 else if (GET_CODE (XEXP (dst, 0)) != LO_SUM)
20671 gcc_assert (rs6000_offsettable_memref_p (dst, reg_mode));
20674 for (i = 0; i < nregs; i++)
20676 /* Calculate index to next subword. */
20677 ++j;
20678 if (j == nregs)
20679 j = 0;
20681 /* If compiler already emitted move of first word by
20682 store with update, no need to do anything. */
20683 if (j == 0 && used_update)
20684 continue;
20686 emit_insn (gen_rtx_SET (VOIDmode,
20687 simplify_gen_subreg (reg_mode, dst, mode,
20688 j * reg_mode_size),
20689 simplify_gen_subreg (reg_mode, src, mode,
20690 j * reg_mode_size)));
20692 if (restore_basereg != NULL_RTX)
20693 emit_insn (restore_basereg);
20698 /* This page contains routines that are used to determine what the
20699 function prologue and epilogue code will do and write them out. */
20701 static inline bool
20702 save_reg_p (int r)
20704 return !call_used_regs[r] && df_regs_ever_live_p (r);
20707 /* Return the first fixed-point register that is required to be
20708 saved. 32 if none. */
20711 first_reg_to_save (void)
20713 int first_reg;
20715 /* Find lowest numbered live register. */
20716 for (first_reg = 13; first_reg <= 31; first_reg++)
20717 if (save_reg_p (first_reg))
20718 break;
20720 if (first_reg > RS6000_PIC_OFFSET_TABLE_REGNUM
20721 && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
20722 || (DEFAULT_ABI == ABI_DARWIN && flag_pic)
20723 || (TARGET_TOC && TARGET_MINIMAL_TOC))
20724 && df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))
20725 first_reg = RS6000_PIC_OFFSET_TABLE_REGNUM;
20727 #if TARGET_MACHO
20728 if (flag_pic
20729 && crtl->uses_pic_offset_table
20730 && first_reg > RS6000_PIC_OFFSET_TABLE_REGNUM)
20731 return RS6000_PIC_OFFSET_TABLE_REGNUM;
20732 #endif
20734 return first_reg;
20737 /* Similar, for FP regs. */
20740 first_fp_reg_to_save (void)
20742 int first_reg;
20744 /* Find lowest numbered live register. */
20745 for (first_reg = 14 + 32; first_reg <= 63; first_reg++)
20746 if (save_reg_p (first_reg))
20747 break;
20749 return first_reg;
20752 /* Similar, for AltiVec regs. */
20754 static int
20755 first_altivec_reg_to_save (void)
20757 int i;
20759 /* Stack frame remains as is unless we are in AltiVec ABI. */
20760 if (! TARGET_ALTIVEC_ABI)
20761 return LAST_ALTIVEC_REGNO + 1;
20763 /* On Darwin, the unwind routines are compiled without
20764 TARGET_ALTIVEC, and use save_world to save/restore the
20765 altivec registers when necessary. */
20766 if (DEFAULT_ABI == ABI_DARWIN && crtl->calls_eh_return
20767 && ! TARGET_ALTIVEC)
20768 return FIRST_ALTIVEC_REGNO + 20;
20770 /* Find lowest numbered live register. */
20771 for (i = FIRST_ALTIVEC_REGNO + 20; i <= LAST_ALTIVEC_REGNO; ++i)
20772 if (save_reg_p (i))
20773 break;
20775 return i;
20778 /* Return a 32-bit mask of the AltiVec registers we need to set in
20779 VRSAVE. Bit n of the return value is 1 if Vn is live. The MSB in
20780 the 32-bit word is 0. */
20782 static unsigned int
20783 compute_vrsave_mask (void)
20785 unsigned int i, mask = 0;
20787 /* On Darwin, the unwind routines are compiled without
20788 TARGET_ALTIVEC, and use save_world to save/restore the
20789 call-saved altivec registers when necessary. */
20790 if (DEFAULT_ABI == ABI_DARWIN && crtl->calls_eh_return
20791 && ! TARGET_ALTIVEC)
20792 mask |= 0xFFF;
20794 /* First, find out if we use _any_ altivec registers. */
20795 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
20796 if (df_regs_ever_live_p (i))
20797 mask |= ALTIVEC_REG_BIT (i);
20799 if (mask == 0)
20800 return mask;
20802 /* Next, remove the argument registers from the set. These must
20803 be in the VRSAVE mask set by the caller, so we don't need to add
20804 them in again. More importantly, the mask we compute here is
20805 used to generate CLOBBERs in the set_vrsave insn, and we do not
20806 wish the argument registers to die. */
20807 for (i = crtl->args.info.vregno - 1; i >= ALTIVEC_ARG_MIN_REG; --i)
20808 mask &= ~ALTIVEC_REG_BIT (i);
20810 /* Similarly, remove the return value from the set. */
20812 bool yes = false;
20813 diddle_return_value (is_altivec_return_reg, &yes);
20814 if (yes)
20815 mask &= ~ALTIVEC_REG_BIT (ALTIVEC_ARG_RETURN);
20818 return mask;
20821 /* For a very restricted set of circumstances, we can cut down the
20822 size of prologues/epilogues by calling our own save/restore-the-world
20823 routines. */
20825 static void
20826 compute_save_world_info (rs6000_stack_t *info_ptr)
20828 info_ptr->world_save_p = 1;
20829 info_ptr->world_save_p
20830 = (WORLD_SAVE_P (info_ptr)
20831 && DEFAULT_ABI == ABI_DARWIN
20832 && !cfun->has_nonlocal_label
20833 && info_ptr->first_fp_reg_save == FIRST_SAVED_FP_REGNO
20834 && info_ptr->first_gp_reg_save == FIRST_SAVED_GP_REGNO
20835 && info_ptr->first_altivec_reg_save == FIRST_SAVED_ALTIVEC_REGNO
20836 && info_ptr->cr_save_p);
20838 /* This will not work in conjunction with sibcalls. Make sure there
20839 are none. (This check is expensive, but seldom executed.) */
20840 if (WORLD_SAVE_P (info_ptr))
20842 rtx_insn *insn;
20843 for (insn = get_last_insn_anywhere (); insn; insn = PREV_INSN (insn))
20844 if (CALL_P (insn) && SIBLING_CALL_P (insn))
20846 info_ptr->world_save_p = 0;
20847 break;
20851 if (WORLD_SAVE_P (info_ptr))
20853 /* Even if we're not touching VRsave, make sure there's room on the
20854 stack for it, if it looks like we're calling SAVE_WORLD, which
20855 will attempt to save it. */
20856 info_ptr->vrsave_size = 4;
20858 /* If we are going to save the world, we need to save the link register too. */
20859 info_ptr->lr_save_p = 1;
20861 /* "Save" the VRsave register too if we're saving the world. */
20862 if (info_ptr->vrsave_mask == 0)
20863 info_ptr->vrsave_mask = compute_vrsave_mask ();
20865 /* Because the Darwin register save/restore routines only handle
20866 F14 .. F31 and V20 .. V31 as per the ABI, perform a consistency
20867 check. */
20868 gcc_assert (info_ptr->first_fp_reg_save >= FIRST_SAVED_FP_REGNO
20869 && (info_ptr->first_altivec_reg_save
20870 >= FIRST_SAVED_ALTIVEC_REGNO));
20872 return;
20876 static void
20877 is_altivec_return_reg (rtx reg, void *xyes)
20879 bool *yes = (bool *) xyes;
20880 if (REGNO (reg) == ALTIVEC_ARG_RETURN)
20881 *yes = true;
20885 /* Look for user-defined global regs in the range FIRST to LAST-1.
20886 We should not restore these, and so cannot use lmw or out-of-line
20887 restore functions if there are any. We also can't save them
20888 (well, emit frame notes for them), because frame unwinding during
20889 exception handling will restore saved registers. */
20891 static bool
20892 global_regs_p (unsigned first, unsigned last)
20894 while (first < last)
20895 if (global_regs[first++])
20896 return true;
20897 return false;
20900 /* Determine the strategy for savings/restoring registers. */
20902 enum {
20903 SAVRES_MULTIPLE = 0x1,
20904 SAVE_INLINE_FPRS = 0x2,
20905 SAVE_INLINE_GPRS = 0x4,
20906 REST_INLINE_FPRS = 0x8,
20907 REST_INLINE_GPRS = 0x10,
20908 SAVE_NOINLINE_GPRS_SAVES_LR = 0x20,
20909 SAVE_NOINLINE_FPRS_SAVES_LR = 0x40,
20910 REST_NOINLINE_FPRS_DOESNT_RESTORE_LR = 0x80,
20911 SAVE_INLINE_VRS = 0x100,
20912 REST_INLINE_VRS = 0x200
20915 static int
20916 rs6000_savres_strategy (rs6000_stack_t *info,
20917 bool using_static_chain_p)
20919 int strategy = 0;
20920 bool lr_save_p;
20922 if (TARGET_MULTIPLE
20923 && !TARGET_POWERPC64
20924 && !(TARGET_SPE_ABI && info->spe_64bit_regs_used)
20925 && info->first_gp_reg_save < 31
20926 && !global_regs_p (info->first_gp_reg_save, 32))
20927 strategy |= SAVRES_MULTIPLE;
20929 if (crtl->calls_eh_return
20930 || cfun->machine->ra_need_lr)
20931 strategy |= (SAVE_INLINE_FPRS | REST_INLINE_FPRS
20932 | SAVE_INLINE_GPRS | REST_INLINE_GPRS
20933 | SAVE_INLINE_VRS | REST_INLINE_VRS);
20935 if (info->first_fp_reg_save == 64
20936 /* The out-of-line FP routines use double-precision stores;
20937 we can't use those routines if we don't have such stores. */
20938 || (TARGET_HARD_FLOAT && !TARGET_DOUBLE_FLOAT)
20939 || global_regs_p (info->first_fp_reg_save, 64))
20940 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
20942 if (info->first_gp_reg_save == 32
20943 || (!(strategy & SAVRES_MULTIPLE)
20944 && global_regs_p (info->first_gp_reg_save, 32)))
20945 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
20947 if (info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1
20948 || global_regs_p (info->first_altivec_reg_save, LAST_ALTIVEC_REGNO + 1))
20949 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
20951 /* Define cutoff for using out-of-line functions to save registers. */
20952 if (DEFAULT_ABI == ABI_V4 || TARGET_ELF)
20954 if (!optimize_size)
20956 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
20957 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
20958 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
20960 else
20962 /* Prefer out-of-line restore if it will exit. */
20963 if (info->first_fp_reg_save > 61)
20964 strategy |= SAVE_INLINE_FPRS;
20965 if (info->first_gp_reg_save > 29)
20967 if (info->first_fp_reg_save == 64)
20968 strategy |= SAVE_INLINE_GPRS;
20969 else
20970 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
20972 if (info->first_altivec_reg_save == LAST_ALTIVEC_REGNO)
20973 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
20976 else if (DEFAULT_ABI == ABI_DARWIN)
20978 if (info->first_fp_reg_save > 60)
20979 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
20980 if (info->first_gp_reg_save > 29)
20981 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
20982 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
20984 else
20986 gcc_checking_assert (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2);
20987 if (info->first_fp_reg_save > 61)
20988 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
20989 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
20990 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
20993 /* Don't bother to try to save things out-of-line if r11 is occupied
20994 by the static chain. It would require too much fiddling and the
20995 static chain is rarely used anyway. FPRs are saved w.r.t the stack
20996 pointer on Darwin, and AIX uses r1 or r12. */
20997 if (using_static_chain_p
20998 && (DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN))
20999 strategy |= ((DEFAULT_ABI == ABI_DARWIN ? 0 : SAVE_INLINE_FPRS)
21000 | SAVE_INLINE_GPRS
21001 | SAVE_INLINE_VRS | REST_INLINE_VRS);
21003 /* We can only use the out-of-line routines to restore if we've
21004 saved all the registers from first_fp_reg_save in the prologue.
21005 Otherwise, we risk loading garbage. */
21006 if ((strategy & (SAVE_INLINE_FPRS | REST_INLINE_FPRS)) == SAVE_INLINE_FPRS)
21008 int i;
21010 for (i = info->first_fp_reg_save; i < 64; i++)
21011 if (!save_reg_p (i))
21013 strategy |= REST_INLINE_FPRS;
21014 break;
21018 /* If we are going to use store multiple, then don't even bother
21019 with the out-of-line routines, since the store-multiple
21020 instruction will always be smaller. */
21021 if ((strategy & SAVRES_MULTIPLE))
21022 strategy |= SAVE_INLINE_GPRS;
21024 /* info->lr_save_p isn't yet set if the only reason lr needs to be
21025 saved is an out-of-line save or restore. Set up the value for
21026 the next test (excluding out-of-line gpr restore). */
21027 lr_save_p = (info->lr_save_p
21028 || !(strategy & SAVE_INLINE_GPRS)
21029 || !(strategy & SAVE_INLINE_FPRS)
21030 || !(strategy & SAVE_INLINE_VRS)
21031 || !(strategy & REST_INLINE_FPRS)
21032 || !(strategy & REST_INLINE_VRS));
21034 /* The situation is more complicated with load multiple. We'd
21035 prefer to use the out-of-line routines for restores, since the
21036 "exit" out-of-line routines can handle the restore of LR and the
21037 frame teardown. However if doesn't make sense to use the
21038 out-of-line routine if that is the only reason we'd need to save
21039 LR, and we can't use the "exit" out-of-line gpr restore if we
21040 have saved some fprs; In those cases it is advantageous to use
21041 load multiple when available. */
21042 if ((strategy & SAVRES_MULTIPLE)
21043 && (!lr_save_p
21044 || info->first_fp_reg_save != 64))
21045 strategy |= REST_INLINE_GPRS;
21047 /* Saving CR interferes with the exit routines used on the SPE, so
21048 just punt here. */
21049 if (TARGET_SPE_ABI
21050 && info->spe_64bit_regs_used
21051 && info->cr_save_p)
21052 strategy |= REST_INLINE_GPRS;
21054 /* We can only use load multiple or the out-of-line routines to
21055 restore if we've used store multiple or out-of-line routines
21056 in the prologue, i.e. if we've saved all the registers from
21057 first_gp_reg_save. Otherwise, we risk loading garbage. */
21058 if ((strategy & (SAVE_INLINE_GPRS | REST_INLINE_GPRS | SAVRES_MULTIPLE))
21059 == SAVE_INLINE_GPRS)
21061 int i;
21063 for (i = info->first_gp_reg_save; i < 32; i++)
21064 if (!save_reg_p (i))
21066 strategy |= REST_INLINE_GPRS;
21067 break;
21071 if (TARGET_ELF && TARGET_64BIT)
21073 if (!(strategy & SAVE_INLINE_FPRS))
21074 strategy |= SAVE_NOINLINE_FPRS_SAVES_LR;
21075 else if (!(strategy & SAVE_INLINE_GPRS)
21076 && info->first_fp_reg_save == 64)
21077 strategy |= SAVE_NOINLINE_GPRS_SAVES_LR;
21079 else if (TARGET_AIX && !(strategy & REST_INLINE_FPRS))
21080 strategy |= REST_NOINLINE_FPRS_DOESNT_RESTORE_LR;
21082 if (TARGET_MACHO && !(strategy & SAVE_INLINE_FPRS))
21083 strategy |= SAVE_NOINLINE_FPRS_SAVES_LR;
21085 return strategy;
21088 /* Calculate the stack information for the current function. This is
21089 complicated by having two separate calling sequences, the AIX calling
21090 sequence and the V.4 calling sequence.
21092 AIX (and Darwin/Mac OS X) stack frames look like:
21093 32-bit 64-bit
21094 SP----> +---------------------------------------+
21095 | back chain to caller | 0 0
21096 +---------------------------------------+
21097 | saved CR | 4 8 (8-11)
21098 +---------------------------------------+
21099 | saved LR | 8 16
21100 +---------------------------------------+
21101 | reserved for compilers | 12 24
21102 +---------------------------------------+
21103 | reserved for binders | 16 32
21104 +---------------------------------------+
21105 | saved TOC pointer | 20 40
21106 +---------------------------------------+
21107 | Parameter save area (P) | 24 48
21108 +---------------------------------------+
21109 | Alloca space (A) | 24+P etc.
21110 +---------------------------------------+
21111 | Local variable space (L) | 24+P+A
21112 +---------------------------------------+
21113 | Float/int conversion temporary (X) | 24+P+A+L
21114 +---------------------------------------+
21115 | Save area for AltiVec registers (W) | 24+P+A+L+X
21116 +---------------------------------------+
21117 | AltiVec alignment padding (Y) | 24+P+A+L+X+W
21118 +---------------------------------------+
21119 | Save area for VRSAVE register (Z) | 24+P+A+L+X+W+Y
21120 +---------------------------------------+
21121 | Save area for GP registers (G) | 24+P+A+X+L+X+W+Y+Z
21122 +---------------------------------------+
21123 | Save area for FP registers (F) | 24+P+A+X+L+X+W+Y+Z+G
21124 +---------------------------------------+
21125 old SP->| back chain to caller's caller |
21126 +---------------------------------------+
21128 The required alignment for AIX configurations is two words (i.e., 8
21129 or 16 bytes).
21131 The ELFv2 ABI is a variant of the AIX ABI. Stack frames look like:
21133 SP----> +---------------------------------------+
21134 | Back chain to caller | 0
21135 +---------------------------------------+
21136 | Save area for CR | 8
21137 +---------------------------------------+
21138 | Saved LR | 16
21139 +---------------------------------------+
21140 | Saved TOC pointer | 24
21141 +---------------------------------------+
21142 | Parameter save area (P) | 32
21143 +---------------------------------------+
21144 | Alloca space (A) | 32+P
21145 +---------------------------------------+
21146 | Local variable space (L) | 32+P+A
21147 +---------------------------------------+
21148 | Save area for AltiVec registers (W) | 32+P+A+L
21149 +---------------------------------------+
21150 | AltiVec alignment padding (Y) | 32+P+A+L+W
21151 +---------------------------------------+
21152 | Save area for GP registers (G) | 32+P+A+L+W+Y
21153 +---------------------------------------+
21154 | Save area for FP registers (F) | 32+P+A+L+W+Y+G
21155 +---------------------------------------+
21156 old SP->| back chain to caller's caller | 32+P+A+L+W+Y+G+F
21157 +---------------------------------------+
21160 V.4 stack frames look like:
21162 SP----> +---------------------------------------+
21163 | back chain to caller | 0
21164 +---------------------------------------+
21165 | caller's saved LR | 4
21166 +---------------------------------------+
21167 | Parameter save area (P) | 8
21168 +---------------------------------------+
21169 | Alloca space (A) | 8+P
21170 +---------------------------------------+
21171 | Varargs save area (V) | 8+P+A
21172 +---------------------------------------+
21173 | Local variable space (L) | 8+P+A+V
21174 +---------------------------------------+
21175 | Float/int conversion temporary (X) | 8+P+A+V+L
21176 +---------------------------------------+
21177 | Save area for AltiVec registers (W) | 8+P+A+V+L+X
21178 +---------------------------------------+
21179 | AltiVec alignment padding (Y) | 8+P+A+V+L+X+W
21180 +---------------------------------------+
21181 | Save area for VRSAVE register (Z) | 8+P+A+V+L+X+W+Y
21182 +---------------------------------------+
21183 | SPE: area for 64-bit GP registers |
21184 +---------------------------------------+
21185 | SPE alignment padding |
21186 +---------------------------------------+
21187 | saved CR (C) | 8+P+A+V+L+X+W+Y+Z
21188 +---------------------------------------+
21189 | Save area for GP registers (G) | 8+P+A+V+L+X+W+Y+Z+C
21190 +---------------------------------------+
21191 | Save area for FP registers (F) | 8+P+A+V+L+X+W+Y+Z+C+G
21192 +---------------------------------------+
21193 old SP->| back chain to caller's caller |
21194 +---------------------------------------+
21196 The required alignment for V.4 is 16 bytes, or 8 bytes if -meabi is
21197 given. (But note below and in sysv4.h that we require only 8 and
21198 may round up the size of our stack frame anyways. The historical
21199 reason is early versions of powerpc-linux which didn't properly
21200 align the stack at program startup. A happy side-effect is that
21201 -mno-eabi libraries can be used with -meabi programs.)
21203 The EABI configuration defaults to the V.4 layout. However,
21204 the stack alignment requirements may differ. If -mno-eabi is not
21205 given, the required stack alignment is 8 bytes; if -mno-eabi is
21206 given, the required alignment is 16 bytes. (But see V.4 comment
21207 above.) */
21209 #ifndef ABI_STACK_BOUNDARY
21210 #define ABI_STACK_BOUNDARY STACK_BOUNDARY
21211 #endif
21213 static rs6000_stack_t *
21214 rs6000_stack_info (void)
21216 rs6000_stack_t *info_ptr = &stack_info;
21217 int reg_size = TARGET_32BIT ? 4 : 8;
21218 int ehrd_size;
21219 int ehcr_size;
21220 int save_align;
21221 int first_gp;
21222 HOST_WIDE_INT non_fixed_size;
21223 bool using_static_chain_p;
21225 if (reload_completed && info_ptr->reload_completed)
21226 return info_ptr;
21228 memset (info_ptr, 0, sizeof (*info_ptr));
21229 info_ptr->reload_completed = reload_completed;
21231 if (TARGET_SPE)
21233 /* Cache value so we don't rescan instruction chain over and over. */
21234 if (cfun->machine->insn_chain_scanned_p == 0)
21235 cfun->machine->insn_chain_scanned_p
21236 = spe_func_has_64bit_regs_p () + 1;
21237 info_ptr->spe_64bit_regs_used = cfun->machine->insn_chain_scanned_p - 1;
21240 /* Select which calling sequence. */
21241 info_ptr->abi = DEFAULT_ABI;
21243 /* Calculate which registers need to be saved & save area size. */
21244 info_ptr->first_gp_reg_save = first_reg_to_save ();
21245 /* Assume that we will have to save RS6000_PIC_OFFSET_TABLE_REGNUM,
21246 even if it currently looks like we won't. Reload may need it to
21247 get at a constant; if so, it will have already created a constant
21248 pool entry for it. */
21249 if (((TARGET_TOC && TARGET_MINIMAL_TOC)
21250 || (flag_pic == 1 && DEFAULT_ABI == ABI_V4)
21251 || (flag_pic && DEFAULT_ABI == ABI_DARWIN))
21252 && crtl->uses_const_pool
21253 && info_ptr->first_gp_reg_save > RS6000_PIC_OFFSET_TABLE_REGNUM)
21254 first_gp = RS6000_PIC_OFFSET_TABLE_REGNUM;
21255 else
21256 first_gp = info_ptr->first_gp_reg_save;
21258 info_ptr->gp_size = reg_size * (32 - first_gp);
21260 /* For the SPE, we have an additional upper 32-bits on each GPR.
21261 Ideally we should save the entire 64-bits only when the upper
21262 half is used in SIMD instructions. Since we only record
21263 registers live (not the size they are used in), this proves
21264 difficult because we'd have to traverse the instruction chain at
21265 the right time, taking reload into account. This is a real pain,
21266 so we opt to save the GPRs in 64-bits always if but one register
21267 gets used in 64-bits. Otherwise, all the registers in the frame
21268 get saved in 32-bits.
21270 So... since when we save all GPRs (except the SP) in 64-bits, the
21271 traditional GP save area will be empty. */
21272 if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
21273 info_ptr->gp_size = 0;
21275 info_ptr->first_fp_reg_save = first_fp_reg_to_save ();
21276 info_ptr->fp_size = 8 * (64 - info_ptr->first_fp_reg_save);
21278 info_ptr->first_altivec_reg_save = first_altivec_reg_to_save ();
21279 info_ptr->altivec_size = 16 * (LAST_ALTIVEC_REGNO + 1
21280 - info_ptr->first_altivec_reg_save);
21282 /* Does this function call anything? */
21283 info_ptr->calls_p = (! crtl->is_leaf
21284 || cfun->machine->ra_needs_full_frame);
21286 /* Determine if we need to save the condition code registers. */
21287 if (df_regs_ever_live_p (CR2_REGNO)
21288 || df_regs_ever_live_p (CR3_REGNO)
21289 || df_regs_ever_live_p (CR4_REGNO))
21291 info_ptr->cr_save_p = 1;
21292 if (DEFAULT_ABI == ABI_V4)
21293 info_ptr->cr_size = reg_size;
21296 /* If the current function calls __builtin_eh_return, then we need
21297 to allocate stack space for registers that will hold data for
21298 the exception handler. */
21299 if (crtl->calls_eh_return)
21301 unsigned int i;
21302 for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; ++i)
21303 continue;
21305 /* SPE saves EH registers in 64-bits. */
21306 ehrd_size = i * (TARGET_SPE_ABI
21307 && info_ptr->spe_64bit_regs_used != 0
21308 ? UNITS_PER_SPE_WORD : UNITS_PER_WORD);
21310 else
21311 ehrd_size = 0;
21313 /* In the ELFv2 ABI, we also need to allocate space for separate
21314 CR field save areas if the function calls __builtin_eh_return. */
21315 if (DEFAULT_ABI == ABI_ELFv2 && crtl->calls_eh_return)
21317 /* This hard-codes that we have three call-saved CR fields. */
21318 ehcr_size = 3 * reg_size;
21319 /* We do *not* use the regular CR save mechanism. */
21320 info_ptr->cr_save_p = 0;
21322 else
21323 ehcr_size = 0;
21325 /* Determine various sizes. */
21326 info_ptr->reg_size = reg_size;
21327 info_ptr->fixed_size = RS6000_SAVE_AREA;
21328 info_ptr->vars_size = RS6000_ALIGN (get_frame_size (), 8);
21329 info_ptr->parm_size = RS6000_ALIGN (crtl->outgoing_args_size,
21330 TARGET_ALTIVEC ? 16 : 8);
21331 if (FRAME_GROWS_DOWNWARD)
21332 info_ptr->vars_size
21333 += RS6000_ALIGN (info_ptr->fixed_size + info_ptr->vars_size
21334 + info_ptr->parm_size,
21335 ABI_STACK_BOUNDARY / BITS_PER_UNIT)
21336 - (info_ptr->fixed_size + info_ptr->vars_size
21337 + info_ptr->parm_size);
21339 if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
21340 info_ptr->spe_gp_size = 8 * (32 - first_gp);
21341 else
21342 info_ptr->spe_gp_size = 0;
21344 if (TARGET_ALTIVEC_ABI)
21345 info_ptr->vrsave_mask = compute_vrsave_mask ();
21346 else
21347 info_ptr->vrsave_mask = 0;
21349 if (TARGET_ALTIVEC_VRSAVE && info_ptr->vrsave_mask)
21350 info_ptr->vrsave_size = 4;
21351 else
21352 info_ptr->vrsave_size = 0;
21354 compute_save_world_info (info_ptr);
21356 /* Calculate the offsets. */
21357 switch (DEFAULT_ABI)
21359 case ABI_NONE:
21360 default:
21361 gcc_unreachable ();
21363 case ABI_AIX:
21364 case ABI_ELFv2:
21365 case ABI_DARWIN:
21366 info_ptr->fp_save_offset = - info_ptr->fp_size;
21367 info_ptr->gp_save_offset = info_ptr->fp_save_offset - info_ptr->gp_size;
21369 if (TARGET_ALTIVEC_ABI)
21371 info_ptr->vrsave_save_offset
21372 = info_ptr->gp_save_offset - info_ptr->vrsave_size;
21374 /* Align stack so vector save area is on a quadword boundary.
21375 The padding goes above the vectors. */
21376 if (info_ptr->altivec_size != 0)
21377 info_ptr->altivec_padding_size
21378 = info_ptr->vrsave_save_offset & 0xF;
21379 else
21380 info_ptr->altivec_padding_size = 0;
21382 info_ptr->altivec_save_offset
21383 = info_ptr->vrsave_save_offset
21384 - info_ptr->altivec_padding_size
21385 - info_ptr->altivec_size;
21386 gcc_assert (info_ptr->altivec_size == 0
21387 || info_ptr->altivec_save_offset % 16 == 0);
21389 /* Adjust for AltiVec case. */
21390 info_ptr->ehrd_offset = info_ptr->altivec_save_offset - ehrd_size;
21392 else
21393 info_ptr->ehrd_offset = info_ptr->gp_save_offset - ehrd_size;
21395 info_ptr->ehcr_offset = info_ptr->ehrd_offset - ehcr_size;
21396 info_ptr->cr_save_offset = reg_size; /* first word when 64-bit. */
21397 info_ptr->lr_save_offset = 2*reg_size;
21398 break;
21400 case ABI_V4:
21401 info_ptr->fp_save_offset = - info_ptr->fp_size;
21402 info_ptr->gp_save_offset = info_ptr->fp_save_offset - info_ptr->gp_size;
21403 info_ptr->cr_save_offset = info_ptr->gp_save_offset - info_ptr->cr_size;
21405 if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
21407 /* Align stack so SPE GPR save area is aligned on a
21408 double-word boundary. */
21409 if (info_ptr->spe_gp_size != 0 && info_ptr->cr_save_offset != 0)
21410 info_ptr->spe_padding_size
21411 = 8 - (-info_ptr->cr_save_offset % 8);
21412 else
21413 info_ptr->spe_padding_size = 0;
21415 info_ptr->spe_gp_save_offset
21416 = info_ptr->cr_save_offset
21417 - info_ptr->spe_padding_size
21418 - info_ptr->spe_gp_size;
21420 /* Adjust for SPE case. */
21421 info_ptr->ehrd_offset = info_ptr->spe_gp_save_offset;
21423 else if (TARGET_ALTIVEC_ABI)
21425 info_ptr->vrsave_save_offset
21426 = info_ptr->cr_save_offset - info_ptr->vrsave_size;
21428 /* Align stack so vector save area is on a quadword boundary. */
21429 if (info_ptr->altivec_size != 0)
21430 info_ptr->altivec_padding_size
21431 = 16 - (-info_ptr->vrsave_save_offset % 16);
21432 else
21433 info_ptr->altivec_padding_size = 0;
21435 info_ptr->altivec_save_offset
21436 = info_ptr->vrsave_save_offset
21437 - info_ptr->altivec_padding_size
21438 - info_ptr->altivec_size;
21440 /* Adjust for AltiVec case. */
21441 info_ptr->ehrd_offset = info_ptr->altivec_save_offset;
21443 else
21444 info_ptr->ehrd_offset = info_ptr->cr_save_offset;
21445 info_ptr->ehrd_offset -= ehrd_size;
21446 info_ptr->lr_save_offset = reg_size;
21447 break;
21450 save_align = (TARGET_ALTIVEC_ABI || DEFAULT_ABI == ABI_DARWIN) ? 16 : 8;
21451 info_ptr->save_size = RS6000_ALIGN (info_ptr->fp_size
21452 + info_ptr->gp_size
21453 + info_ptr->altivec_size
21454 + info_ptr->altivec_padding_size
21455 + info_ptr->spe_gp_size
21456 + info_ptr->spe_padding_size
21457 + ehrd_size
21458 + ehcr_size
21459 + info_ptr->cr_size
21460 + info_ptr->vrsave_size,
21461 save_align);
21463 non_fixed_size = (info_ptr->vars_size
21464 + info_ptr->parm_size
21465 + info_ptr->save_size);
21467 info_ptr->total_size = RS6000_ALIGN (non_fixed_size + info_ptr->fixed_size,
21468 ABI_STACK_BOUNDARY / BITS_PER_UNIT);
21470 /* Determine if we need to save the link register. */
21471 if (info_ptr->calls_p
21472 || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
21473 && crtl->profile
21474 && !TARGET_PROFILE_KERNEL)
21475 || (DEFAULT_ABI == ABI_V4 && cfun->calls_alloca)
21476 #ifdef TARGET_RELOCATABLE
21477 || (TARGET_RELOCATABLE && (get_pool_size () != 0))
21478 #endif
21479 || rs6000_ra_ever_killed ())
21480 info_ptr->lr_save_p = 1;
21482 using_static_chain_p = (cfun->static_chain_decl != NULL_TREE
21483 && df_regs_ever_live_p (STATIC_CHAIN_REGNUM)
21484 && call_used_regs[STATIC_CHAIN_REGNUM]);
21485 info_ptr->savres_strategy = rs6000_savres_strategy (info_ptr,
21486 using_static_chain_p);
21488 if (!(info_ptr->savres_strategy & SAVE_INLINE_GPRS)
21489 || !(info_ptr->savres_strategy & SAVE_INLINE_FPRS)
21490 || !(info_ptr->savres_strategy & SAVE_INLINE_VRS)
21491 || !(info_ptr->savres_strategy & REST_INLINE_GPRS)
21492 || !(info_ptr->savres_strategy & REST_INLINE_FPRS)
21493 || !(info_ptr->savres_strategy & REST_INLINE_VRS))
21494 info_ptr->lr_save_p = 1;
21496 if (info_ptr->lr_save_p)
21497 df_set_regs_ever_live (LR_REGNO, true);
21499 /* Determine if we need to allocate any stack frame:
21501 For AIX we need to push the stack if a frame pointer is needed
21502 (because the stack might be dynamically adjusted), if we are
21503 debugging, if we make calls, or if the sum of fp_save, gp_save,
21504 and local variables are more than the space needed to save all
21505 non-volatile registers: 32-bit: 18*8 + 19*4 = 220 or 64-bit: 18*8
21506 + 18*8 = 288 (GPR13 reserved).
21508 For V.4 we don't have the stack cushion that AIX uses, but assume
21509 that the debugger can handle stackless frames. */
21511 if (info_ptr->calls_p)
21512 info_ptr->push_p = 1;
21514 else if (DEFAULT_ABI == ABI_V4)
21515 info_ptr->push_p = non_fixed_size != 0;
21517 else if (frame_pointer_needed)
21518 info_ptr->push_p = 1;
21520 else if (TARGET_XCOFF && write_symbols != NO_DEBUG)
21521 info_ptr->push_p = 1;
21523 else
21524 info_ptr->push_p = non_fixed_size > (TARGET_32BIT ? 220 : 288);
21526 /* Zero offsets if we're not saving those registers. */
21527 if (info_ptr->fp_size == 0)
21528 info_ptr->fp_save_offset = 0;
21530 if (info_ptr->gp_size == 0)
21531 info_ptr->gp_save_offset = 0;
21533 if (! TARGET_ALTIVEC_ABI || info_ptr->altivec_size == 0)
21534 info_ptr->altivec_save_offset = 0;
21536 /* Zero VRSAVE offset if not saved and restored. */
21537 if (! TARGET_ALTIVEC_VRSAVE || info_ptr->vrsave_mask == 0)
21538 info_ptr->vrsave_save_offset = 0;
21540 if (! TARGET_SPE_ABI
21541 || info_ptr->spe_64bit_regs_used == 0
21542 || info_ptr->spe_gp_size == 0)
21543 info_ptr->spe_gp_save_offset = 0;
21545 if (! info_ptr->lr_save_p)
21546 info_ptr->lr_save_offset = 0;
21548 if (! info_ptr->cr_save_p)
21549 info_ptr->cr_save_offset = 0;
21551 return info_ptr;
21554 /* Return true if the current function uses any GPRs in 64-bit SIMD
21555 mode. */
21557 static bool
21558 spe_func_has_64bit_regs_p (void)
21560 rtx_insn *insns, *insn;
21562 /* Functions that save and restore all the call-saved registers will
21563 need to save/restore the registers in 64-bits. */
21564 if (crtl->calls_eh_return
21565 || cfun->calls_setjmp
21566 || crtl->has_nonlocal_goto)
21567 return true;
21569 insns = get_insns ();
21571 for (insn = NEXT_INSN (insns); insn != NULL_RTX; insn = NEXT_INSN (insn))
21573 if (INSN_P (insn))
21575 rtx i;
21577 /* FIXME: This should be implemented with attributes...
21579 (set_attr "spe64" "true")....then,
21580 if (get_spe64(insn)) return true;
21582 It's the only reliable way to do the stuff below. */
21584 i = PATTERN (insn);
21585 if (GET_CODE (i) == SET)
21587 enum machine_mode mode = GET_MODE (SET_SRC (i));
21589 if (SPE_VECTOR_MODE (mode))
21590 return true;
21591 if (TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode))
21592 return true;
21597 return false;
21600 static void
21601 debug_stack_info (rs6000_stack_t *info)
21603 const char *abi_string;
21605 if (! info)
21606 info = rs6000_stack_info ();
21608 fprintf (stderr, "\nStack information for function %s:\n",
21609 ((current_function_decl && DECL_NAME (current_function_decl))
21610 ? IDENTIFIER_POINTER (DECL_NAME (current_function_decl))
21611 : "<unknown>"));
21613 switch (info->abi)
21615 default: abi_string = "Unknown"; break;
21616 case ABI_NONE: abi_string = "NONE"; break;
21617 case ABI_AIX: abi_string = "AIX"; break;
21618 case ABI_ELFv2: abi_string = "ELFv2"; break;
21619 case ABI_DARWIN: abi_string = "Darwin"; break;
21620 case ABI_V4: abi_string = "V.4"; break;
21623 fprintf (stderr, "\tABI = %5s\n", abi_string);
21625 if (TARGET_ALTIVEC_ABI)
21626 fprintf (stderr, "\tALTIVEC ABI extensions enabled.\n");
21628 if (TARGET_SPE_ABI)
21629 fprintf (stderr, "\tSPE ABI extensions enabled.\n");
21631 if (info->first_gp_reg_save != 32)
21632 fprintf (stderr, "\tfirst_gp_reg_save = %5d\n", info->first_gp_reg_save);
21634 if (info->first_fp_reg_save != 64)
21635 fprintf (stderr, "\tfirst_fp_reg_save = %5d\n", info->first_fp_reg_save);
21637 if (info->first_altivec_reg_save <= LAST_ALTIVEC_REGNO)
21638 fprintf (stderr, "\tfirst_altivec_reg_save = %5d\n",
21639 info->first_altivec_reg_save);
21641 if (info->lr_save_p)
21642 fprintf (stderr, "\tlr_save_p = %5d\n", info->lr_save_p);
21644 if (info->cr_save_p)
21645 fprintf (stderr, "\tcr_save_p = %5d\n", info->cr_save_p);
21647 if (info->vrsave_mask)
21648 fprintf (stderr, "\tvrsave_mask = 0x%x\n", info->vrsave_mask);
21650 if (info->push_p)
21651 fprintf (stderr, "\tpush_p = %5d\n", info->push_p);
21653 if (info->calls_p)
21654 fprintf (stderr, "\tcalls_p = %5d\n", info->calls_p);
21656 if (info->gp_save_offset)
21657 fprintf (stderr, "\tgp_save_offset = %5d\n", info->gp_save_offset);
21659 if (info->fp_save_offset)
21660 fprintf (stderr, "\tfp_save_offset = %5d\n", info->fp_save_offset);
21662 if (info->altivec_save_offset)
21663 fprintf (stderr, "\taltivec_save_offset = %5d\n",
21664 info->altivec_save_offset);
21666 if (info->spe_gp_save_offset)
21667 fprintf (stderr, "\tspe_gp_save_offset = %5d\n",
21668 info->spe_gp_save_offset);
21670 if (info->vrsave_save_offset)
21671 fprintf (stderr, "\tvrsave_save_offset = %5d\n",
21672 info->vrsave_save_offset);
21674 if (info->lr_save_offset)
21675 fprintf (stderr, "\tlr_save_offset = %5d\n", info->lr_save_offset);
21677 if (info->cr_save_offset)
21678 fprintf (stderr, "\tcr_save_offset = %5d\n", info->cr_save_offset);
21680 if (info->varargs_save_offset)
21681 fprintf (stderr, "\tvarargs_save_offset = %5d\n", info->varargs_save_offset);
21683 if (info->total_size)
21684 fprintf (stderr, "\ttotal_size = "HOST_WIDE_INT_PRINT_DEC"\n",
21685 info->total_size);
21687 if (info->vars_size)
21688 fprintf (stderr, "\tvars_size = "HOST_WIDE_INT_PRINT_DEC"\n",
21689 info->vars_size);
21691 if (info->parm_size)
21692 fprintf (stderr, "\tparm_size = %5d\n", info->parm_size);
21694 if (info->fixed_size)
21695 fprintf (stderr, "\tfixed_size = %5d\n", info->fixed_size);
21697 if (info->gp_size)
21698 fprintf (stderr, "\tgp_size = %5d\n", info->gp_size);
21700 if (info->spe_gp_size)
21701 fprintf (stderr, "\tspe_gp_size = %5d\n", info->spe_gp_size);
21703 if (info->fp_size)
21704 fprintf (stderr, "\tfp_size = %5d\n", info->fp_size);
21706 if (info->altivec_size)
21707 fprintf (stderr, "\taltivec_size = %5d\n", info->altivec_size);
21709 if (info->vrsave_size)
21710 fprintf (stderr, "\tvrsave_size = %5d\n", info->vrsave_size);
21712 if (info->altivec_padding_size)
21713 fprintf (stderr, "\taltivec_padding_size= %5d\n",
21714 info->altivec_padding_size);
21716 if (info->spe_padding_size)
21717 fprintf (stderr, "\tspe_padding_size = %5d\n",
21718 info->spe_padding_size);
21720 if (info->cr_size)
21721 fprintf (stderr, "\tcr_size = %5d\n", info->cr_size);
21723 if (info->save_size)
21724 fprintf (stderr, "\tsave_size = %5d\n", info->save_size);
21726 if (info->reg_size != 4)
21727 fprintf (stderr, "\treg_size = %5d\n", info->reg_size);
21729 fprintf (stderr, "\tsave-strategy = %04x\n", info->savres_strategy);
21731 fprintf (stderr, "\n");
21735 rs6000_return_addr (int count, rtx frame)
21737 /* Currently we don't optimize very well between prolog and body
21738 code and for PIC code the code can be actually quite bad, so
21739 don't try to be too clever here. */
21740 if (count != 0
21741 || ((DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN) && flag_pic))
21743 cfun->machine->ra_needs_full_frame = 1;
21745 return
21746 gen_rtx_MEM
21747 (Pmode,
21748 memory_address
21749 (Pmode,
21750 plus_constant (Pmode,
21751 copy_to_reg
21752 (gen_rtx_MEM (Pmode,
21753 memory_address (Pmode, frame))),
21754 RETURN_ADDRESS_OFFSET)));
21757 cfun->machine->ra_need_lr = 1;
21758 return get_hard_reg_initial_val (Pmode, LR_REGNO);
21761 /* Say whether a function is a candidate for sibcall handling or not. */
21763 static bool
21764 rs6000_function_ok_for_sibcall (tree decl, tree exp)
21766 tree fntype;
21768 if (decl)
21769 fntype = TREE_TYPE (decl);
21770 else
21771 fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (exp)));
21773 /* We can't do it if the called function has more vector parameters
21774 than the current function; there's nowhere to put the VRsave code. */
21775 if (TARGET_ALTIVEC_ABI
21776 && TARGET_ALTIVEC_VRSAVE
21777 && !(decl && decl == current_function_decl))
21779 function_args_iterator args_iter;
21780 tree type;
21781 int nvreg = 0;
21783 /* Functions with vector parameters are required to have a
21784 prototype, so the argument type info must be available
21785 here. */
21786 FOREACH_FUNCTION_ARGS(fntype, type, args_iter)
21787 if (TREE_CODE (type) == VECTOR_TYPE
21788 && ALTIVEC_OR_VSX_VECTOR_MODE (TYPE_MODE (type)))
21789 nvreg++;
21791 FOREACH_FUNCTION_ARGS(TREE_TYPE (current_function_decl), type, args_iter)
21792 if (TREE_CODE (type) == VECTOR_TYPE
21793 && ALTIVEC_OR_VSX_VECTOR_MODE (TYPE_MODE (type)))
21794 nvreg--;
21796 if (nvreg > 0)
21797 return false;
21800 /* Under the AIX or ELFv2 ABIs we can't allow calls to non-local
21801 functions, because the callee may have a different TOC pointer to
21802 the caller and there's no way to ensure we restore the TOC when
21803 we return. With the secure-plt SYSV ABI we can't make non-local
21804 calls when -fpic/PIC because the plt call stubs use r30. */
21805 if (DEFAULT_ABI == ABI_DARWIN
21806 || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
21807 && decl
21808 && !DECL_EXTERNAL (decl)
21809 && (*targetm.binds_local_p) (decl))
21810 || (DEFAULT_ABI == ABI_V4
21811 && (!TARGET_SECURE_PLT
21812 || !flag_pic
21813 || (decl
21814 && (*targetm.binds_local_p) (decl)))))
21816 tree attr_list = TYPE_ATTRIBUTES (fntype);
21818 if (!lookup_attribute ("longcall", attr_list)
21819 || lookup_attribute ("shortcall", attr_list))
21820 return true;
21823 return false;
21826 static int
21827 rs6000_ra_ever_killed (void)
21829 rtx_insn *top;
21830 rtx reg;
21831 rtx_insn *insn;
21833 if (cfun->is_thunk)
21834 return 0;
21836 if (cfun->machine->lr_save_state)
21837 return cfun->machine->lr_save_state - 1;
21839 /* regs_ever_live has LR marked as used if any sibcalls are present,
21840 but this should not force saving and restoring in the
21841 pro/epilogue. Likewise, reg_set_between_p thinks a sibcall
21842 clobbers LR, so that is inappropriate. */
21844 /* Also, the prologue can generate a store into LR that
21845 doesn't really count, like this:
21847 move LR->R0
21848 bcl to set PIC register
21849 move LR->R31
21850 move R0->LR
21852 When we're called from the epilogue, we need to avoid counting
21853 this as a store. */
21855 push_topmost_sequence ();
21856 top = get_insns ();
21857 pop_topmost_sequence ();
21858 reg = gen_rtx_REG (Pmode, LR_REGNO);
21860 for (insn = NEXT_INSN (top); insn != NULL_RTX; insn = NEXT_INSN (insn))
21862 if (INSN_P (insn))
21864 if (CALL_P (insn))
21866 if (!SIBLING_CALL_P (insn))
21867 return 1;
21869 else if (find_regno_note (insn, REG_INC, LR_REGNO))
21870 return 1;
21871 else if (set_of (reg, insn) != NULL_RTX
21872 && !prologue_epilogue_contains (insn))
21873 return 1;
21876 return 0;
21879 /* Emit instructions needed to load the TOC register.
21880 This is only needed when TARGET_TOC, TARGET_MINIMAL_TOC, and there is
21881 a constant pool; or for SVR4 -fpic. */
21883 void
21884 rs6000_emit_load_toc_table (int fromprolog)
21886 rtx dest;
21887 dest = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
21889 if (TARGET_ELF && TARGET_SECURE_PLT && DEFAULT_ABI == ABI_V4 && flag_pic)
21891 char buf[30];
21892 rtx lab, tmp1, tmp2, got;
21894 lab = gen_label_rtx ();
21895 ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (lab));
21896 lab = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
21897 if (flag_pic == 2)
21898 got = gen_rtx_SYMBOL_REF (Pmode, toc_label_name);
21899 else
21900 got = rs6000_got_sym ();
21901 tmp1 = tmp2 = dest;
21902 if (!fromprolog)
21904 tmp1 = gen_reg_rtx (Pmode);
21905 tmp2 = gen_reg_rtx (Pmode);
21907 emit_insn (gen_load_toc_v4_PIC_1 (lab));
21908 emit_move_insn (tmp1, gen_rtx_REG (Pmode, LR_REGNO));
21909 emit_insn (gen_load_toc_v4_PIC_3b (tmp2, tmp1, got, lab));
21910 emit_insn (gen_load_toc_v4_PIC_3c (dest, tmp2, got, lab));
21912 else if (TARGET_ELF && DEFAULT_ABI == ABI_V4 && flag_pic == 1)
21914 emit_insn (gen_load_toc_v4_pic_si ());
21915 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
21917 else if (TARGET_ELF && DEFAULT_ABI == ABI_V4 && flag_pic == 2)
21919 char buf[30];
21920 rtx temp0 = (fromprolog
21921 ? gen_rtx_REG (Pmode, 0)
21922 : gen_reg_rtx (Pmode));
21924 if (fromprolog)
21926 rtx symF, symL;
21928 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
21929 symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
21931 ASM_GENERATE_INTERNAL_LABEL (buf, "LCL", rs6000_pic_labelno);
21932 symL = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
21934 emit_insn (gen_load_toc_v4_PIC_1 (symF));
21935 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
21936 emit_insn (gen_load_toc_v4_PIC_2 (temp0, dest, symL, symF));
21938 else
21940 rtx tocsym, lab;
21942 tocsym = gen_rtx_SYMBOL_REF (Pmode, toc_label_name);
21943 lab = gen_label_rtx ();
21944 emit_insn (gen_load_toc_v4_PIC_1b (tocsym, lab));
21945 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
21946 if (TARGET_LINK_STACK)
21947 emit_insn (gen_addsi3 (dest, dest, GEN_INT (4)));
21948 emit_move_insn (temp0, gen_rtx_MEM (Pmode, dest));
21950 emit_insn (gen_addsi3 (dest, temp0, dest));
21952 else if (TARGET_ELF && !TARGET_AIX && flag_pic == 0 && TARGET_MINIMAL_TOC)
21954 /* This is for AIX code running in non-PIC ELF32. */
21955 char buf[30];
21956 rtx realsym;
21957 ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
21958 realsym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
21960 emit_insn (gen_elf_high (dest, realsym));
21961 emit_insn (gen_elf_low (dest, dest, realsym));
21963 else
21965 gcc_assert (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2);
21967 if (TARGET_32BIT)
21968 emit_insn (gen_load_toc_aix_si (dest));
21969 else
21970 emit_insn (gen_load_toc_aix_di (dest));
21974 /* Emit instructions to restore the link register after determining where
21975 its value has been stored. */
21977 void
21978 rs6000_emit_eh_reg_restore (rtx source, rtx scratch)
21980 rs6000_stack_t *info = rs6000_stack_info ();
21981 rtx operands[2];
21983 operands[0] = source;
21984 operands[1] = scratch;
21986 if (info->lr_save_p)
21988 rtx frame_rtx = stack_pointer_rtx;
21989 HOST_WIDE_INT sp_offset = 0;
21990 rtx tmp;
21992 if (frame_pointer_needed
21993 || cfun->calls_alloca
21994 || info->total_size > 32767)
21996 tmp = gen_frame_mem (Pmode, frame_rtx);
21997 emit_move_insn (operands[1], tmp);
21998 frame_rtx = operands[1];
22000 else if (info->push_p)
22001 sp_offset = info->total_size;
22003 tmp = plus_constant (Pmode, frame_rtx,
22004 info->lr_save_offset + sp_offset);
22005 tmp = gen_frame_mem (Pmode, tmp);
22006 emit_move_insn (tmp, operands[0]);
22008 else
22009 emit_move_insn (gen_rtx_REG (Pmode, LR_REGNO), operands[0]);
22011 /* Freeze lr_save_p. We've just emitted rtl that depends on the
22012 state of lr_save_p so any change from here on would be a bug. In
22013 particular, stop rs6000_ra_ever_killed from considering the SET
22014 of lr we may have added just above. */
22015 cfun->machine->lr_save_state = info->lr_save_p + 1;
22018 static GTY(()) alias_set_type set = -1;
22020 alias_set_type
22021 get_TOC_alias_set (void)
22023 if (set == -1)
22024 set = new_alias_set ();
22025 return set;
22028 /* This returns nonzero if the current function uses the TOC. This is
22029 determined by the presence of (use (unspec ... UNSPEC_TOC)), which
22030 is generated by the ABI_V4 load_toc_* patterns. */
22031 #if TARGET_ELF
22032 static int
22033 uses_TOC (void)
22035 rtx_insn *insn;
22037 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
22038 if (INSN_P (insn))
22040 rtx pat = PATTERN (insn);
22041 int i;
22043 if (GET_CODE (pat) == PARALLEL)
22044 for (i = 0; i < XVECLEN (pat, 0); i++)
22046 rtx sub = XVECEXP (pat, 0, i);
22047 if (GET_CODE (sub) == USE)
22049 sub = XEXP (sub, 0);
22050 if (GET_CODE (sub) == UNSPEC
22051 && XINT (sub, 1) == UNSPEC_TOC)
22052 return 1;
22056 return 0;
22058 #endif
22061 create_TOC_reference (rtx symbol, rtx largetoc_reg)
22063 rtx tocrel, tocreg, hi;
22065 if (TARGET_DEBUG_ADDR)
22067 if (GET_CODE (symbol) == SYMBOL_REF)
22068 fprintf (stderr, "\ncreate_TOC_reference, (symbol_ref %s)\n",
22069 XSTR (symbol, 0));
22070 else
22072 fprintf (stderr, "\ncreate_TOC_reference, code %s:\n",
22073 GET_RTX_NAME (GET_CODE (symbol)));
22074 debug_rtx (symbol);
22078 if (!can_create_pseudo_p ())
22079 df_set_regs_ever_live (TOC_REGISTER, true);
22081 tocreg = gen_rtx_REG (Pmode, TOC_REGISTER);
22082 tocrel = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, symbol, tocreg), UNSPEC_TOCREL);
22083 if (TARGET_CMODEL == CMODEL_SMALL || can_create_pseudo_p ())
22084 return tocrel;
22086 hi = gen_rtx_HIGH (Pmode, copy_rtx (tocrel));
22087 if (largetoc_reg != NULL)
22089 emit_move_insn (largetoc_reg, hi);
22090 hi = largetoc_reg;
22092 return gen_rtx_LO_SUM (Pmode, hi, tocrel);
22095 /* Issue assembly directives that create a reference to the given DWARF
22096 FRAME_TABLE_LABEL from the current function section. */
22097 void
22098 rs6000_aix_asm_output_dwarf_table_ref (char * frame_table_label)
22100 fprintf (asm_out_file, "\t.ref %s\n",
22101 (* targetm.strip_name_encoding) (frame_table_label));
22104 /* This ties together stack memory (MEM with an alias set of frame_alias_set)
22105 and the change to the stack pointer. */
22107 static void
22108 rs6000_emit_stack_tie (rtx fp, bool hard_frame_needed)
22110 rtvec p;
22111 int i;
22112 rtx regs[3];
22114 i = 0;
22115 regs[i++] = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
22116 if (hard_frame_needed)
22117 regs[i++] = gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM);
22118 if (!(REGNO (fp) == STACK_POINTER_REGNUM
22119 || (hard_frame_needed
22120 && REGNO (fp) == HARD_FRAME_POINTER_REGNUM)))
22121 regs[i++] = fp;
22123 p = rtvec_alloc (i);
22124 while (--i >= 0)
22126 rtx mem = gen_frame_mem (BLKmode, regs[i]);
22127 RTVEC_ELT (p, i) = gen_rtx_SET (VOIDmode, mem, const0_rtx);
22130 emit_insn (gen_stack_tie (gen_rtx_PARALLEL (VOIDmode, p)));
22133 /* Emit the correct code for allocating stack space, as insns.
22134 If COPY_REG, make sure a copy of the old frame is left there.
22135 The generated code may use hard register 0 as a temporary. */
22137 static void
22138 rs6000_emit_allocate_stack (HOST_WIDE_INT size, rtx copy_reg, int copy_off)
22140 rtx_insn *insn;
22141 rtx stack_reg = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
22142 rtx tmp_reg = gen_rtx_REG (Pmode, 0);
22143 rtx todec = gen_int_mode (-size, Pmode);
22144 rtx par, set, mem;
22146 if (INTVAL (todec) != -size)
22148 warning (0, "stack frame too large");
22149 emit_insn (gen_trap ());
22150 return;
22153 if (crtl->limit_stack)
22155 if (REG_P (stack_limit_rtx)
22156 && REGNO (stack_limit_rtx) > 1
22157 && REGNO (stack_limit_rtx) <= 31)
22159 emit_insn (gen_add3_insn (tmp_reg, stack_limit_rtx, GEN_INT (size)));
22160 emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
22161 const0_rtx));
22163 else if (GET_CODE (stack_limit_rtx) == SYMBOL_REF
22164 && TARGET_32BIT
22165 && DEFAULT_ABI == ABI_V4)
22167 rtx toload = gen_rtx_CONST (VOIDmode,
22168 gen_rtx_PLUS (Pmode,
22169 stack_limit_rtx,
22170 GEN_INT (size)));
22172 emit_insn (gen_elf_high (tmp_reg, toload));
22173 emit_insn (gen_elf_low (tmp_reg, tmp_reg, toload));
22174 emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
22175 const0_rtx));
22177 else
22178 warning (0, "stack limit expression is not supported");
22181 if (copy_reg)
22183 if (copy_off != 0)
22184 emit_insn (gen_add3_insn (copy_reg, stack_reg, GEN_INT (copy_off)));
22185 else
22186 emit_move_insn (copy_reg, stack_reg);
22189 if (size > 32767)
22191 /* Need a note here so that try_split doesn't get confused. */
22192 if (get_last_insn () == NULL_RTX)
22193 emit_note (NOTE_INSN_DELETED);
22194 insn = emit_move_insn (tmp_reg, todec);
22195 try_split (PATTERN (insn), insn, 0);
22196 todec = tmp_reg;
22199 insn = emit_insn (TARGET_32BIT
22200 ? gen_movsi_update_stack (stack_reg, stack_reg,
22201 todec, stack_reg)
22202 : gen_movdi_di_update_stack (stack_reg, stack_reg,
22203 todec, stack_reg));
22204 /* Since we didn't use gen_frame_mem to generate the MEM, grab
22205 it now and set the alias set/attributes. The above gen_*_update
22206 calls will generate a PARALLEL with the MEM set being the first
22207 operation. */
22208 par = PATTERN (insn);
22209 gcc_assert (GET_CODE (par) == PARALLEL);
22210 set = XVECEXP (par, 0, 0);
22211 gcc_assert (GET_CODE (set) == SET);
22212 mem = SET_DEST (set);
22213 gcc_assert (MEM_P (mem));
22214 MEM_NOTRAP_P (mem) = 1;
22215 set_mem_alias_set (mem, get_frame_alias_set ());
22217 RTX_FRAME_RELATED_P (insn) = 1;
22218 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
22219 gen_rtx_SET (VOIDmode, stack_reg,
22220 gen_rtx_PLUS (Pmode, stack_reg,
22221 GEN_INT (-size))));
22224 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
22226 #if PROBE_INTERVAL > 32768
22227 #error Cannot use indexed addressing mode for stack probing
22228 #endif
22230 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
22231 inclusive. These are offsets from the current stack pointer. */
22233 static void
22234 rs6000_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
22236 /* See if we have a constant small number of probes to generate. If so,
22237 that's the easy case. */
22238 if (first + size <= 32768)
22240 HOST_WIDE_INT i;
22242 /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
22243 it exceeds SIZE. If only one probe is needed, this will not
22244 generate any code. Then probe at FIRST + SIZE. */
22245 for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
22246 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
22247 -(first + i)));
22249 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
22250 -(first + size)));
22253 /* Otherwise, do the same as above, but in a loop. Note that we must be
22254 extra careful with variables wrapping around because we might be at
22255 the very top (or the very bottom) of the address space and we have
22256 to be able to handle this case properly; in particular, we use an
22257 equality test for the loop condition. */
22258 else
22260 HOST_WIDE_INT rounded_size;
22261 rtx r12 = gen_rtx_REG (Pmode, 12);
22262 rtx r0 = gen_rtx_REG (Pmode, 0);
22264 /* Sanity check for the addressing mode we're going to use. */
22265 gcc_assert (first <= 32768);
22267 /* Step 1: round SIZE to the previous multiple of the interval. */
22269 rounded_size = size & -PROBE_INTERVAL;
22272 /* Step 2: compute initial and final value of the loop counter. */
22274 /* TEST_ADDR = SP + FIRST. */
22275 emit_insn (gen_rtx_SET (VOIDmode, r12,
22276 plus_constant (Pmode, stack_pointer_rtx,
22277 -first)));
22279 /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE. */
22280 if (rounded_size > 32768)
22282 emit_move_insn (r0, GEN_INT (-rounded_size));
22283 emit_insn (gen_rtx_SET (VOIDmode, r0,
22284 gen_rtx_PLUS (Pmode, r12, r0)));
22286 else
22287 emit_insn (gen_rtx_SET (VOIDmode, r0,
22288 plus_constant (Pmode, r12, -rounded_size)));
22291 /* Step 3: the loop
22293 while (TEST_ADDR != LAST_ADDR)
22295 TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
22296 probe at TEST_ADDR
22299 probes at FIRST + N * PROBE_INTERVAL for values of N from 1
22300 until it is equal to ROUNDED_SIZE. */
22302 if (TARGET_64BIT)
22303 emit_insn (gen_probe_stack_rangedi (r12, r12, r0));
22304 else
22305 emit_insn (gen_probe_stack_rangesi (r12, r12, r0));
22308 /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
22309 that SIZE is equal to ROUNDED_SIZE. */
22311 if (size != rounded_size)
22312 emit_stack_probe (plus_constant (Pmode, r12, rounded_size - size));
22316 /* Probe a range of stack addresses from REG1 to REG2 inclusive. These are
22317 absolute addresses. */
22319 const char *
22320 output_probe_stack_range (rtx reg1, rtx reg2)
22322 static int labelno = 0;
22323 char loop_lab[32], end_lab[32];
22324 rtx xops[2];
22326 ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno);
22327 ASM_GENERATE_INTERNAL_LABEL (end_lab, "LPSRE", labelno++);
22329 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
22331 /* Jump to END_LAB if TEST_ADDR == LAST_ADDR. */
22332 xops[0] = reg1;
22333 xops[1] = reg2;
22334 if (TARGET_64BIT)
22335 output_asm_insn ("cmpd 0,%0,%1", xops);
22336 else
22337 output_asm_insn ("cmpw 0,%0,%1", xops);
22339 fputs ("\tbeq 0,", asm_out_file);
22340 assemble_name_raw (asm_out_file, end_lab);
22341 fputc ('\n', asm_out_file);
22343 /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL. */
22344 xops[1] = GEN_INT (-PROBE_INTERVAL);
22345 output_asm_insn ("addi %0,%0,%1", xops);
22347 /* Probe at TEST_ADDR and branch. */
22348 xops[1] = gen_rtx_REG (Pmode, 0);
22349 output_asm_insn ("stw %1,0(%0)", xops);
22350 fprintf (asm_out_file, "\tb ");
22351 assemble_name_raw (asm_out_file, loop_lab);
22352 fputc ('\n', asm_out_file);
22354 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, end_lab);
22356 return "";
22359 /* Add to 'insn' a note which is PATTERN (INSN) but with REG replaced
22360 with (plus:P (reg 1) VAL), and with REG2 replaced with RREG if REG2
22361 is not NULL. It would be nice if dwarf2out_frame_debug_expr could
22362 deduce these equivalences by itself so it wasn't necessary to hold
22363 its hand so much. Don't be tempted to always supply d2_f_d_e with
22364 the actual cfa register, ie. r31 when we are using a hard frame
22365 pointer. That fails when saving regs off r1, and sched moves the
22366 r31 setup past the reg saves. */
22368 static rtx
22369 rs6000_frame_related (rtx insn, rtx reg, HOST_WIDE_INT val,
22370 rtx reg2, rtx rreg, rtx split_reg)
22372 rtx real, temp;
22374 if (REGNO (reg) == STACK_POINTER_REGNUM && reg2 == NULL_RTX)
22376 /* No need for any replacement. Just set RTX_FRAME_RELATED_P. */
22377 int i;
22379 gcc_checking_assert (val == 0);
22380 real = PATTERN (insn);
22381 if (GET_CODE (real) == PARALLEL)
22382 for (i = 0; i < XVECLEN (real, 0); i++)
22383 if (GET_CODE (XVECEXP (real, 0, i)) == SET)
22385 rtx set = XVECEXP (real, 0, i);
22387 RTX_FRAME_RELATED_P (set) = 1;
22389 RTX_FRAME_RELATED_P (insn) = 1;
22390 return insn;
22393 /* copy_rtx will not make unique copies of registers, so we need to
22394 ensure we don't have unwanted sharing here. */
22395 if (reg == reg2)
22396 reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
22398 if (reg == rreg)
22399 reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
22401 real = copy_rtx (PATTERN (insn));
22403 if (reg2 != NULL_RTX)
22404 real = replace_rtx (real, reg2, rreg);
22406 if (REGNO (reg) == STACK_POINTER_REGNUM)
22407 gcc_checking_assert (val == 0);
22408 else
22409 real = replace_rtx (real, reg,
22410 gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode,
22411 STACK_POINTER_REGNUM),
22412 GEN_INT (val)));
22414 /* We expect that 'real' is either a SET or a PARALLEL containing
22415 SETs (and possibly other stuff). In a PARALLEL, all the SETs
22416 are important so they all have to be marked RTX_FRAME_RELATED_P. */
22418 if (GET_CODE (real) == SET)
22420 rtx set = real;
22422 temp = simplify_rtx (SET_SRC (set));
22423 if (temp)
22424 SET_SRC (set) = temp;
22425 temp = simplify_rtx (SET_DEST (set));
22426 if (temp)
22427 SET_DEST (set) = temp;
22428 if (GET_CODE (SET_DEST (set)) == MEM)
22430 temp = simplify_rtx (XEXP (SET_DEST (set), 0));
22431 if (temp)
22432 XEXP (SET_DEST (set), 0) = temp;
22435 else
22437 int i;
22439 gcc_assert (GET_CODE (real) == PARALLEL);
22440 for (i = 0; i < XVECLEN (real, 0); i++)
22441 if (GET_CODE (XVECEXP (real, 0, i)) == SET)
22443 rtx set = XVECEXP (real, 0, i);
22445 temp = simplify_rtx (SET_SRC (set));
22446 if (temp)
22447 SET_SRC (set) = temp;
22448 temp = simplify_rtx (SET_DEST (set));
22449 if (temp)
22450 SET_DEST (set) = temp;
22451 if (GET_CODE (SET_DEST (set)) == MEM)
22453 temp = simplify_rtx (XEXP (SET_DEST (set), 0));
22454 if (temp)
22455 XEXP (SET_DEST (set), 0) = temp;
22457 RTX_FRAME_RELATED_P (set) = 1;
22461 /* If a store insn has been split into multiple insns, the
22462 true source register is given by split_reg. */
22463 if (split_reg != NULL_RTX)
22464 real = gen_rtx_SET (VOIDmode, SET_DEST (real), split_reg);
22466 RTX_FRAME_RELATED_P (insn) = 1;
22467 add_reg_note (insn, REG_FRAME_RELATED_EXPR, real);
22469 return insn;
22472 /* Returns an insn that has a vrsave set operation with the
22473 appropriate CLOBBERs. */
22475 static rtx
22476 generate_set_vrsave (rtx reg, rs6000_stack_t *info, int epiloguep)
22478 int nclobs, i;
22479 rtx insn, clobs[TOTAL_ALTIVEC_REGS + 1];
22480 rtx vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
22482 clobs[0]
22483 = gen_rtx_SET (VOIDmode,
22484 vrsave,
22485 gen_rtx_UNSPEC_VOLATILE (SImode,
22486 gen_rtvec (2, reg, vrsave),
22487 UNSPECV_SET_VRSAVE));
22489 nclobs = 1;
22491 /* We need to clobber the registers in the mask so the scheduler
22492 does not move sets to VRSAVE before sets of AltiVec registers.
22494 However, if the function receives nonlocal gotos, reload will set
22495 all call saved registers live. We will end up with:
22497 (set (reg 999) (mem))
22498 (parallel [ (set (reg vrsave) (unspec blah))
22499 (clobber (reg 999))])
22501 The clobber will cause the store into reg 999 to be dead, and
22502 flow will attempt to delete an epilogue insn. In this case, we
22503 need an unspec use/set of the register. */
22505 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
22506 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
22508 if (!epiloguep || call_used_regs [i])
22509 clobs[nclobs++] = gen_rtx_CLOBBER (VOIDmode,
22510 gen_rtx_REG (V4SImode, i));
22511 else
22513 rtx reg = gen_rtx_REG (V4SImode, i);
22515 clobs[nclobs++]
22516 = gen_rtx_SET (VOIDmode,
22517 reg,
22518 gen_rtx_UNSPEC (V4SImode,
22519 gen_rtvec (1, reg), 27));
22523 insn = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nclobs));
22525 for (i = 0; i < nclobs; ++i)
22526 XVECEXP (insn, 0, i) = clobs[i];
22528 return insn;
22531 static rtx
22532 gen_frame_set (rtx reg, rtx frame_reg, int offset, bool store)
22534 rtx addr, mem;
22536 addr = gen_rtx_PLUS (Pmode, frame_reg, GEN_INT (offset));
22537 mem = gen_frame_mem (GET_MODE (reg), addr);
22538 return gen_rtx_SET (VOIDmode, store ? mem : reg, store ? reg : mem);
22541 static rtx
22542 gen_frame_load (rtx reg, rtx frame_reg, int offset)
22544 return gen_frame_set (reg, frame_reg, offset, false);
22547 static rtx
22548 gen_frame_store (rtx reg, rtx frame_reg, int offset)
22550 return gen_frame_set (reg, frame_reg, offset, true);
22553 /* Save a register into the frame, and emit RTX_FRAME_RELATED_P notes.
22554 Save REGNO into [FRAME_REG + OFFSET] in mode MODE. */
22556 static rtx
22557 emit_frame_save (rtx frame_reg, enum machine_mode mode,
22558 unsigned int regno, int offset, HOST_WIDE_INT frame_reg_to_sp)
22560 rtx reg, insn;
22562 /* Some cases that need register indexed addressing. */
22563 gcc_checking_assert (!((TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
22564 || (TARGET_VSX && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
22565 || (TARGET_E500_DOUBLE && mode == DFmode)
22566 || (TARGET_SPE_ABI
22567 && SPE_VECTOR_MODE (mode)
22568 && !SPE_CONST_OFFSET_OK (offset))));
22570 reg = gen_rtx_REG (mode, regno);
22571 insn = emit_insn (gen_frame_store (reg, frame_reg, offset));
22572 return rs6000_frame_related (insn, frame_reg, frame_reg_to_sp,
22573 NULL_RTX, NULL_RTX, NULL_RTX);
22576 /* Emit an offset memory reference suitable for a frame store, while
22577 converting to a valid addressing mode. */
22579 static rtx
22580 gen_frame_mem_offset (enum machine_mode mode, rtx reg, int offset)
22582 rtx int_rtx, offset_rtx;
22584 int_rtx = GEN_INT (offset);
22586 if ((TARGET_SPE_ABI && SPE_VECTOR_MODE (mode) && !SPE_CONST_OFFSET_OK (offset))
22587 || (TARGET_E500_DOUBLE && mode == DFmode))
22589 offset_rtx = gen_rtx_REG (Pmode, FIXED_SCRATCH);
22590 emit_move_insn (offset_rtx, int_rtx);
22592 else
22593 offset_rtx = int_rtx;
22595 return gen_frame_mem (mode, gen_rtx_PLUS (Pmode, reg, offset_rtx));
22598 #ifndef TARGET_FIX_AND_CONTINUE
22599 #define TARGET_FIX_AND_CONTINUE 0
22600 #endif
22602 /* It's really GPR 13 or 14, FPR 14 and VR 20. We need the smallest. */
22603 #define FIRST_SAVRES_REGISTER FIRST_SAVED_GP_REGNO
22604 #define LAST_SAVRES_REGISTER 31
22605 #define N_SAVRES_REGISTERS (LAST_SAVRES_REGISTER - FIRST_SAVRES_REGISTER + 1)
22607 enum {
22608 SAVRES_LR = 0x1,
22609 SAVRES_SAVE = 0x2,
22610 SAVRES_REG = 0x0c,
22611 SAVRES_GPR = 0,
22612 SAVRES_FPR = 4,
22613 SAVRES_VR = 8
22616 static GTY(()) rtx savres_routine_syms[N_SAVRES_REGISTERS][12];
22618 /* Temporary holding space for an out-of-line register save/restore
22619 routine name. */
22620 static char savres_routine_name[30];
22622 /* Return the name for an out-of-line register save/restore routine.
22623 We are saving/restoring GPRs if GPR is true. */
22625 static char *
22626 rs6000_savres_routine_name (rs6000_stack_t *info, int regno, int sel)
22628 const char *prefix = "";
22629 const char *suffix = "";
22631 /* Different targets are supposed to define
22632 {SAVE,RESTORE}_FP_{PREFIX,SUFFIX} with the idea that the needed
22633 routine name could be defined with:
22635 sprintf (name, "%s%d%s", SAVE_FP_PREFIX, regno, SAVE_FP_SUFFIX)
22637 This is a nice idea in practice, but in reality, things are
22638 complicated in several ways:
22640 - ELF targets have save/restore routines for GPRs.
22642 - SPE targets use different prefixes for 32/64-bit registers, and
22643 neither of them fit neatly in the FOO_{PREFIX,SUFFIX} regimen.
22645 - PPC64 ELF targets have routines for save/restore of GPRs that
22646 differ in what they do with the link register, so having a set
22647 prefix doesn't work. (We only use one of the save routines at
22648 the moment, though.)
22650 - PPC32 elf targets have "exit" versions of the restore routines
22651 that restore the link register and can save some extra space.
22652 These require an extra suffix. (There are also "tail" versions
22653 of the restore routines and "GOT" versions of the save routines,
22654 but we don't generate those at present. Same problems apply,
22655 though.)
22657 We deal with all this by synthesizing our own prefix/suffix and
22658 using that for the simple sprintf call shown above. */
22659 if (TARGET_SPE)
22661 /* No floating point saves on the SPE. */
22662 gcc_assert ((sel & SAVRES_REG) == SAVRES_GPR);
22664 if ((sel & SAVRES_SAVE))
22665 prefix = info->spe_64bit_regs_used ? "_save64gpr_" : "_save32gpr_";
22666 else
22667 prefix = info->spe_64bit_regs_used ? "_rest64gpr_" : "_rest32gpr_";
22669 if ((sel & SAVRES_LR))
22670 suffix = "_x";
22672 else if (DEFAULT_ABI == ABI_V4)
22674 if (TARGET_64BIT)
22675 goto aix_names;
22677 if ((sel & SAVRES_REG) == SAVRES_GPR)
22678 prefix = (sel & SAVRES_SAVE) ? "_savegpr_" : "_restgpr_";
22679 else if ((sel & SAVRES_REG) == SAVRES_FPR)
22680 prefix = (sel & SAVRES_SAVE) ? "_savefpr_" : "_restfpr_";
22681 else if ((sel & SAVRES_REG) == SAVRES_VR)
22682 prefix = (sel & SAVRES_SAVE) ? "_savevr_" : "_restvr_";
22683 else
22684 abort ();
22686 if ((sel & SAVRES_LR))
22687 suffix = "_x";
22689 else if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
22691 #if !defined (POWERPC_LINUX) && !defined (POWERPC_FREEBSD)
22692 /* No out-of-line save/restore routines for GPRs on AIX. */
22693 gcc_assert (!TARGET_AIX || (sel & SAVRES_REG) != SAVRES_GPR);
22694 #endif
22696 aix_names:
22697 if ((sel & SAVRES_REG) == SAVRES_GPR)
22698 prefix = ((sel & SAVRES_SAVE)
22699 ? ((sel & SAVRES_LR) ? "_savegpr0_" : "_savegpr1_")
22700 : ((sel & SAVRES_LR) ? "_restgpr0_" : "_restgpr1_"));
22701 else if ((sel & SAVRES_REG) == SAVRES_FPR)
22703 #if defined (POWERPC_LINUX) || defined (POWERPC_FREEBSD)
22704 if ((sel & SAVRES_LR))
22705 prefix = ((sel & SAVRES_SAVE) ? "_savefpr_" : "_restfpr_");
22706 else
22707 #endif
22709 prefix = (sel & SAVRES_SAVE) ? SAVE_FP_PREFIX : RESTORE_FP_PREFIX;
22710 suffix = (sel & SAVRES_SAVE) ? SAVE_FP_SUFFIX : RESTORE_FP_SUFFIX;
22713 else if ((sel & SAVRES_REG) == SAVRES_VR)
22714 prefix = (sel & SAVRES_SAVE) ? "_savevr_" : "_restvr_";
22715 else
22716 abort ();
22719 if (DEFAULT_ABI == ABI_DARWIN)
22721 /* The Darwin approach is (slightly) different, in order to be
22722 compatible with code generated by the system toolchain. There is a
22723 single symbol for the start of save sequence, and the code here
22724 embeds an offset into that code on the basis of the first register
22725 to be saved. */
22726 prefix = (sel & SAVRES_SAVE) ? "save" : "rest" ;
22727 if ((sel & SAVRES_REG) == SAVRES_GPR)
22728 sprintf (savres_routine_name, "*%sGPR%s%s%.0d ; %s r%d-r31", prefix,
22729 ((sel & SAVRES_LR) ? "x" : ""), (regno == 13 ? "" : "+"),
22730 (regno - 13) * 4, prefix, regno);
22731 else if ((sel & SAVRES_REG) == SAVRES_FPR)
22732 sprintf (savres_routine_name, "*%sFP%s%.0d ; %s f%d-f31", prefix,
22733 (regno == 14 ? "" : "+"), (regno - 14) * 4, prefix, regno);
22734 else if ((sel & SAVRES_REG) == SAVRES_VR)
22735 sprintf (savres_routine_name, "*%sVEC%s%.0d ; %s v%d-v31", prefix,
22736 (regno == 20 ? "" : "+"), (regno - 20) * 8, prefix, regno);
22737 else
22738 abort ();
22740 else
22741 sprintf (savres_routine_name, "%s%d%s", prefix, regno, suffix);
22743 return savres_routine_name;
22746 /* Return an RTL SYMBOL_REF for an out-of-line register save/restore routine.
22747 We are saving/restoring GPRs if GPR is true. */
22749 static rtx
22750 rs6000_savres_routine_sym (rs6000_stack_t *info, int sel)
22752 int regno = ((sel & SAVRES_REG) == SAVRES_GPR
22753 ? info->first_gp_reg_save
22754 : (sel & SAVRES_REG) == SAVRES_FPR
22755 ? info->first_fp_reg_save - 32
22756 : (sel & SAVRES_REG) == SAVRES_VR
22757 ? info->first_altivec_reg_save - FIRST_ALTIVEC_REGNO
22758 : -1);
22759 rtx sym;
22760 int select = sel;
22762 /* On the SPE, we never have any FPRs, but we do have 32/64-bit
22763 versions of the gpr routines. */
22764 if (TARGET_SPE_ABI && (sel & SAVRES_REG) == SAVRES_GPR
22765 && info->spe_64bit_regs_used)
22766 select ^= SAVRES_FPR ^ SAVRES_GPR;
22768 /* Don't generate bogus routine names. */
22769 gcc_assert (FIRST_SAVRES_REGISTER <= regno
22770 && regno <= LAST_SAVRES_REGISTER
22771 && select >= 0 && select <= 12);
22773 sym = savres_routine_syms[regno-FIRST_SAVRES_REGISTER][select];
22775 if (sym == NULL)
22777 char *name;
22779 name = rs6000_savres_routine_name (info, regno, sel);
22781 sym = savres_routine_syms[regno-FIRST_SAVRES_REGISTER][select]
22782 = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
22783 SYMBOL_REF_FLAGS (sym) |= SYMBOL_FLAG_FUNCTION;
22786 return sym;
22789 /* Emit a sequence of insns, including a stack tie if needed, for
22790 resetting the stack pointer. If UPDT_REGNO is not 1, then don't
22791 reset the stack pointer, but move the base of the frame into
22792 reg UPDT_REGNO for use by out-of-line register restore routines. */
22794 static rtx
22795 rs6000_emit_stack_reset (rs6000_stack_t *info,
22796 rtx frame_reg_rtx, HOST_WIDE_INT frame_off,
22797 unsigned updt_regno)
22799 rtx updt_reg_rtx;
22801 /* This blockage is needed so that sched doesn't decide to move
22802 the sp change before the register restores. */
22803 if (DEFAULT_ABI == ABI_V4
22804 || (TARGET_SPE_ABI
22805 && info->spe_64bit_regs_used != 0
22806 && info->first_gp_reg_save != 32))
22807 rs6000_emit_stack_tie (frame_reg_rtx, frame_pointer_needed);
22809 /* If we are restoring registers out-of-line, we will be using the
22810 "exit" variants of the restore routines, which will reset the
22811 stack for us. But we do need to point updt_reg into the
22812 right place for those routines. */
22813 updt_reg_rtx = gen_rtx_REG (Pmode, updt_regno);
22815 if (frame_off != 0)
22816 return emit_insn (gen_add3_insn (updt_reg_rtx,
22817 frame_reg_rtx, GEN_INT (frame_off)));
22818 else if (REGNO (frame_reg_rtx) != updt_regno)
22819 return emit_move_insn (updt_reg_rtx, frame_reg_rtx);
22821 return NULL_RTX;
22824 /* Return the register number used as a pointer by out-of-line
22825 save/restore functions. */
22827 static inline unsigned
22828 ptr_regno_for_savres (int sel)
22830 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
22831 return (sel & SAVRES_REG) == SAVRES_FPR || (sel & SAVRES_LR) ? 1 : 12;
22832 return DEFAULT_ABI == ABI_DARWIN && (sel & SAVRES_REG) == SAVRES_FPR ? 1 : 11;
22835 /* Construct a parallel rtx describing the effect of a call to an
22836 out-of-line register save/restore routine, and emit the insn
22837 or jump_insn as appropriate. */
22839 static rtx
22840 rs6000_emit_savres_rtx (rs6000_stack_t *info,
22841 rtx frame_reg_rtx, int save_area_offset, int lr_offset,
22842 enum machine_mode reg_mode, int sel)
22844 int i;
22845 int offset, start_reg, end_reg, n_regs, use_reg;
22846 int reg_size = GET_MODE_SIZE (reg_mode);
22847 rtx sym;
22848 rtvec p;
22849 rtx par, insn;
22851 offset = 0;
22852 start_reg = ((sel & SAVRES_REG) == SAVRES_GPR
22853 ? info->first_gp_reg_save
22854 : (sel & SAVRES_REG) == SAVRES_FPR
22855 ? info->first_fp_reg_save
22856 : (sel & SAVRES_REG) == SAVRES_VR
22857 ? info->first_altivec_reg_save
22858 : -1);
22859 end_reg = ((sel & SAVRES_REG) == SAVRES_GPR
22860 ? 32
22861 : (sel & SAVRES_REG) == SAVRES_FPR
22862 ? 64
22863 : (sel & SAVRES_REG) == SAVRES_VR
22864 ? LAST_ALTIVEC_REGNO + 1
22865 : -1);
22866 n_regs = end_reg - start_reg;
22867 p = rtvec_alloc (3 + ((sel & SAVRES_LR) ? 1 : 0)
22868 + ((sel & SAVRES_REG) == SAVRES_VR ? 1 : 0)
22869 + n_regs);
22871 if (!(sel & SAVRES_SAVE) && (sel & SAVRES_LR))
22872 RTVEC_ELT (p, offset++) = ret_rtx;
22874 RTVEC_ELT (p, offset++)
22875 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, LR_REGNO));
22877 sym = rs6000_savres_routine_sym (info, sel);
22878 RTVEC_ELT (p, offset++) = gen_rtx_USE (VOIDmode, sym);
22880 use_reg = ptr_regno_for_savres (sel);
22881 if ((sel & SAVRES_REG) == SAVRES_VR)
22883 /* Vector regs are saved/restored using [reg+reg] addressing. */
22884 RTVEC_ELT (p, offset++)
22885 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, use_reg));
22886 RTVEC_ELT (p, offset++)
22887 = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, 0));
22889 else
22890 RTVEC_ELT (p, offset++)
22891 = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, use_reg));
22893 for (i = 0; i < end_reg - start_reg; i++)
22894 RTVEC_ELT (p, i + offset)
22895 = gen_frame_set (gen_rtx_REG (reg_mode, start_reg + i),
22896 frame_reg_rtx, save_area_offset + reg_size * i,
22897 (sel & SAVRES_SAVE) != 0);
22899 if ((sel & SAVRES_SAVE) && (sel & SAVRES_LR))
22900 RTVEC_ELT (p, i + offset)
22901 = gen_frame_store (gen_rtx_REG (Pmode, 0), frame_reg_rtx, lr_offset);
22903 par = gen_rtx_PARALLEL (VOIDmode, p);
22905 if (!(sel & SAVRES_SAVE) && (sel & SAVRES_LR))
22907 insn = emit_jump_insn (par);
22908 JUMP_LABEL (insn) = ret_rtx;
22910 else
22911 insn = emit_insn (par);
22912 return insn;
22915 /* Emit code to store CR fields that need to be saved into REG. */
22917 static void
22918 rs6000_emit_move_from_cr (rtx reg)
22920 /* Only the ELFv2 ABI allows storing only selected fields. */
22921 if (DEFAULT_ABI == ABI_ELFv2 && TARGET_MFCRF)
22923 int i, cr_reg[8], count = 0;
22925 /* Collect CR fields that must be saved. */
22926 for (i = 0; i < 8; i++)
22927 if (save_reg_p (CR0_REGNO + i))
22928 cr_reg[count++] = i;
22930 /* If it's just a single one, use mfcrf. */
22931 if (count == 1)
22933 rtvec p = rtvec_alloc (1);
22934 rtvec r = rtvec_alloc (2);
22935 RTVEC_ELT (r, 0) = gen_rtx_REG (CCmode, CR0_REGNO + cr_reg[0]);
22936 RTVEC_ELT (r, 1) = GEN_INT (1 << (7 - cr_reg[0]));
22937 RTVEC_ELT (p, 0)
22938 = gen_rtx_SET (VOIDmode, reg,
22939 gen_rtx_UNSPEC (SImode, r, UNSPEC_MOVESI_FROM_CR));
22941 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
22942 return;
22945 /* ??? It might be better to handle count == 2 / 3 cases here
22946 as well, using logical operations to combine the values. */
22949 emit_insn (gen_movesi_from_cr (reg));
22952 /* Determine whether the gp REG is really used. */
22954 static bool
22955 rs6000_reg_live_or_pic_offset_p (int reg)
22957 /* If the function calls eh_return, claim used all the registers that would
22958 be checked for liveness otherwise. This is required for the PIC offset
22959 register with -mminimal-toc on AIX, as it is advertised as "fixed" for
22960 register allocation purposes in this case. */
22962 return (((crtl->calls_eh_return || df_regs_ever_live_p (reg))
22963 && (!call_used_regs[reg]
22964 || (reg == RS6000_PIC_OFFSET_TABLE_REGNUM
22965 && !TARGET_SINGLE_PIC_BASE
22966 && TARGET_TOC && TARGET_MINIMAL_TOC)))
22967 || (reg == RS6000_PIC_OFFSET_TABLE_REGNUM
22968 && !TARGET_SINGLE_PIC_BASE
22969 && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
22970 || (DEFAULT_ABI == ABI_DARWIN && flag_pic))));
22973 /* Emit function prologue as insns. */
22975 void
22976 rs6000_emit_prologue (void)
22978 rs6000_stack_t *info = rs6000_stack_info ();
22979 enum machine_mode reg_mode = Pmode;
22980 int reg_size = TARGET_32BIT ? 4 : 8;
22981 rtx sp_reg_rtx = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
22982 rtx frame_reg_rtx = sp_reg_rtx;
22983 unsigned int cr_save_regno;
22984 rtx cr_save_rtx = NULL_RTX;
22985 rtx insn;
22986 int strategy;
22987 int using_static_chain_p = (cfun->static_chain_decl != NULL_TREE
22988 && df_regs_ever_live_p (STATIC_CHAIN_REGNUM)
22989 && call_used_regs[STATIC_CHAIN_REGNUM]);
22990 /* Offset to top of frame for frame_reg and sp respectively. */
22991 HOST_WIDE_INT frame_off = 0;
22992 HOST_WIDE_INT sp_off = 0;
22994 #ifdef ENABLE_CHECKING
22995 /* Track and check usage of r0, r11, r12. */
22996 int reg_inuse = using_static_chain_p ? 1 << 11 : 0;
22997 #define START_USE(R) do \
22999 gcc_assert ((reg_inuse & (1 << (R))) == 0); \
23000 reg_inuse |= 1 << (R); \
23001 } while (0)
23002 #define END_USE(R) do \
23004 gcc_assert ((reg_inuse & (1 << (R))) != 0); \
23005 reg_inuse &= ~(1 << (R)); \
23006 } while (0)
23007 #define NOT_INUSE(R) do \
23009 gcc_assert ((reg_inuse & (1 << (R))) == 0); \
23010 } while (0)
23011 #else
23012 #define START_USE(R) do {} while (0)
23013 #define END_USE(R) do {} while (0)
23014 #define NOT_INUSE(R) do {} while (0)
23015 #endif
23017 if (DEFAULT_ABI == ABI_ELFv2)
23019 cfun->machine->r2_setup_needed = df_regs_ever_live_p (TOC_REGNUM);
23021 /* With -mminimal-toc we may generate an extra use of r2 below. */
23022 if (!TARGET_SINGLE_PIC_BASE
23023 && TARGET_TOC && TARGET_MINIMAL_TOC && get_pool_size () != 0)
23024 cfun->machine->r2_setup_needed = true;
23028 if (flag_stack_usage_info)
23029 current_function_static_stack_size = info->total_size;
23031 if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
23033 HOST_WIDE_INT size = info->total_size;
23035 if (crtl->is_leaf && !cfun->calls_alloca)
23037 if (size > PROBE_INTERVAL && size > STACK_CHECK_PROTECT)
23038 rs6000_emit_probe_stack_range (STACK_CHECK_PROTECT,
23039 size - STACK_CHECK_PROTECT);
23041 else if (size > 0)
23042 rs6000_emit_probe_stack_range (STACK_CHECK_PROTECT, size);
23045 if (TARGET_FIX_AND_CONTINUE)
23047 /* gdb on darwin arranges to forward a function from the old
23048 address by modifying the first 5 instructions of the function
23049 to branch to the overriding function. This is necessary to
23050 permit function pointers that point to the old function to
23051 actually forward to the new function. */
23052 emit_insn (gen_nop ());
23053 emit_insn (gen_nop ());
23054 emit_insn (gen_nop ());
23055 emit_insn (gen_nop ());
23056 emit_insn (gen_nop ());
23059 if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
23061 reg_mode = V2SImode;
23062 reg_size = 8;
23065 /* Handle world saves specially here. */
23066 if (WORLD_SAVE_P (info))
23068 int i, j, sz;
23069 rtx treg;
23070 rtvec p;
23071 rtx reg0;
23073 /* save_world expects lr in r0. */
23074 reg0 = gen_rtx_REG (Pmode, 0);
23075 if (info->lr_save_p)
23077 insn = emit_move_insn (reg0,
23078 gen_rtx_REG (Pmode, LR_REGNO));
23079 RTX_FRAME_RELATED_P (insn) = 1;
23082 /* The SAVE_WORLD and RESTORE_WORLD routines make a number of
23083 assumptions about the offsets of various bits of the stack
23084 frame. */
23085 gcc_assert (info->gp_save_offset == -220
23086 && info->fp_save_offset == -144
23087 && info->lr_save_offset == 8
23088 && info->cr_save_offset == 4
23089 && info->push_p
23090 && info->lr_save_p
23091 && (!crtl->calls_eh_return
23092 || info->ehrd_offset == -432)
23093 && info->vrsave_save_offset == -224
23094 && info->altivec_save_offset == -416);
23096 treg = gen_rtx_REG (SImode, 11);
23097 emit_move_insn (treg, GEN_INT (-info->total_size));
23099 /* SAVE_WORLD takes the caller's LR in R0 and the frame size
23100 in R11. It also clobbers R12, so beware! */
23102 /* Preserve CR2 for save_world prologues */
23103 sz = 5;
23104 sz += 32 - info->first_gp_reg_save;
23105 sz += 64 - info->first_fp_reg_save;
23106 sz += LAST_ALTIVEC_REGNO - info->first_altivec_reg_save + 1;
23107 p = rtvec_alloc (sz);
23108 j = 0;
23109 RTVEC_ELT (p, j++) = gen_rtx_CLOBBER (VOIDmode,
23110 gen_rtx_REG (SImode,
23111 LR_REGNO));
23112 RTVEC_ELT (p, j++) = gen_rtx_USE (VOIDmode,
23113 gen_rtx_SYMBOL_REF (Pmode,
23114 "*save_world"));
23115 /* We do floats first so that the instruction pattern matches
23116 properly. */
23117 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
23118 RTVEC_ELT (p, j++)
23119 = gen_frame_store (gen_rtx_REG (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
23120 ? DFmode : SFmode,
23121 info->first_fp_reg_save + i),
23122 frame_reg_rtx,
23123 info->fp_save_offset + frame_off + 8 * i);
23124 for (i = 0; info->first_altivec_reg_save + i <= LAST_ALTIVEC_REGNO; i++)
23125 RTVEC_ELT (p, j++)
23126 = gen_frame_store (gen_rtx_REG (V4SImode,
23127 info->first_altivec_reg_save + i),
23128 frame_reg_rtx,
23129 info->altivec_save_offset + frame_off + 16 * i);
23130 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
23131 RTVEC_ELT (p, j++)
23132 = gen_frame_store (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
23133 frame_reg_rtx,
23134 info->gp_save_offset + frame_off + reg_size * i);
23136 /* CR register traditionally saved as CR2. */
23137 RTVEC_ELT (p, j++)
23138 = gen_frame_store (gen_rtx_REG (SImode, CR2_REGNO),
23139 frame_reg_rtx, info->cr_save_offset + frame_off);
23140 /* Explain about use of R0. */
23141 if (info->lr_save_p)
23142 RTVEC_ELT (p, j++)
23143 = gen_frame_store (reg0,
23144 frame_reg_rtx, info->lr_save_offset + frame_off);
23145 /* Explain what happens to the stack pointer. */
23147 rtx newval = gen_rtx_PLUS (Pmode, sp_reg_rtx, treg);
23148 RTVEC_ELT (p, j++) = gen_rtx_SET (VOIDmode, sp_reg_rtx, newval);
23151 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
23152 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
23153 treg, GEN_INT (-info->total_size), NULL_RTX);
23154 sp_off = frame_off = info->total_size;
23157 strategy = info->savres_strategy;
23159 /* For V.4, update stack before we do any saving and set back pointer. */
23160 if (! WORLD_SAVE_P (info)
23161 && info->push_p
23162 && (DEFAULT_ABI == ABI_V4
23163 || crtl->calls_eh_return))
23165 bool need_r11 = (TARGET_SPE
23166 ? (!(strategy & SAVE_INLINE_GPRS)
23167 && info->spe_64bit_regs_used == 0)
23168 : (!(strategy & SAVE_INLINE_FPRS)
23169 || !(strategy & SAVE_INLINE_GPRS)
23170 || !(strategy & SAVE_INLINE_VRS)));
23171 int ptr_regno = -1;
23172 rtx ptr_reg = NULL_RTX;
23173 int ptr_off = 0;
23175 if (info->total_size < 32767)
23176 frame_off = info->total_size;
23177 else if (need_r11)
23178 ptr_regno = 11;
23179 else if (info->cr_save_p
23180 || info->lr_save_p
23181 || info->first_fp_reg_save < 64
23182 || info->first_gp_reg_save < 32
23183 || info->altivec_size != 0
23184 || info->vrsave_mask != 0
23185 || crtl->calls_eh_return)
23186 ptr_regno = 12;
23187 else
23189 /* The prologue won't be saving any regs so there is no need
23190 to set up a frame register to access any frame save area.
23191 We also won't be using frame_off anywhere below, but set
23192 the correct value anyway to protect against future
23193 changes to this function. */
23194 frame_off = info->total_size;
23196 if (ptr_regno != -1)
23198 /* Set up the frame offset to that needed by the first
23199 out-of-line save function. */
23200 START_USE (ptr_regno);
23201 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
23202 frame_reg_rtx = ptr_reg;
23203 if (!(strategy & SAVE_INLINE_FPRS) && info->fp_size != 0)
23204 gcc_checking_assert (info->fp_save_offset + info->fp_size == 0);
23205 else if (!(strategy & SAVE_INLINE_GPRS) && info->first_gp_reg_save < 32)
23206 ptr_off = info->gp_save_offset + info->gp_size;
23207 else if (!(strategy & SAVE_INLINE_VRS) && info->altivec_size != 0)
23208 ptr_off = info->altivec_save_offset + info->altivec_size;
23209 frame_off = -ptr_off;
23211 rs6000_emit_allocate_stack (info->total_size, ptr_reg, ptr_off);
23212 sp_off = info->total_size;
23213 if (frame_reg_rtx != sp_reg_rtx)
23214 rs6000_emit_stack_tie (frame_reg_rtx, false);
23217 /* If we use the link register, get it into r0. */
23218 if (!WORLD_SAVE_P (info) && info->lr_save_p)
23220 rtx addr, reg, mem;
23222 reg = gen_rtx_REG (Pmode, 0);
23223 START_USE (0);
23224 insn = emit_move_insn (reg, gen_rtx_REG (Pmode, LR_REGNO));
23225 RTX_FRAME_RELATED_P (insn) = 1;
23227 if (!(strategy & (SAVE_NOINLINE_GPRS_SAVES_LR
23228 | SAVE_NOINLINE_FPRS_SAVES_LR)))
23230 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
23231 GEN_INT (info->lr_save_offset + frame_off));
23232 mem = gen_rtx_MEM (Pmode, addr);
23233 /* This should not be of rs6000_sr_alias_set, because of
23234 __builtin_return_address. */
23236 insn = emit_move_insn (mem, reg);
23237 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
23238 NULL_RTX, NULL_RTX, NULL_RTX);
23239 END_USE (0);
23243 /* If we need to save CR, put it into r12 or r11. Choose r12 except when
23244 r12 will be needed by out-of-line gpr restore. */
23245 cr_save_regno = ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
23246 && !(strategy & (SAVE_INLINE_GPRS
23247 | SAVE_NOINLINE_GPRS_SAVES_LR))
23248 ? 11 : 12);
23249 if (!WORLD_SAVE_P (info)
23250 && info->cr_save_p
23251 && REGNO (frame_reg_rtx) != cr_save_regno
23252 && !(using_static_chain_p && cr_save_regno == 11))
23254 cr_save_rtx = gen_rtx_REG (SImode, cr_save_regno);
23255 START_USE (cr_save_regno);
23256 rs6000_emit_move_from_cr (cr_save_rtx);
23259 /* Do any required saving of fpr's. If only one or two to save, do
23260 it ourselves. Otherwise, call function. */
23261 if (!WORLD_SAVE_P (info) && (strategy & SAVE_INLINE_FPRS))
23263 int i;
23264 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
23265 if (save_reg_p (info->first_fp_reg_save + i))
23266 emit_frame_save (frame_reg_rtx,
23267 (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
23268 ? DFmode : SFmode),
23269 info->first_fp_reg_save + i,
23270 info->fp_save_offset + frame_off + 8 * i,
23271 sp_off - frame_off);
23273 else if (!WORLD_SAVE_P (info) && info->first_fp_reg_save != 64)
23275 bool lr = (strategy & SAVE_NOINLINE_FPRS_SAVES_LR) != 0;
23276 int sel = SAVRES_SAVE | SAVRES_FPR | (lr ? SAVRES_LR : 0);
23277 unsigned ptr_regno = ptr_regno_for_savres (sel);
23278 rtx ptr_reg = frame_reg_rtx;
23280 if (REGNO (frame_reg_rtx) == ptr_regno)
23281 gcc_checking_assert (frame_off == 0);
23282 else
23284 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
23285 NOT_INUSE (ptr_regno);
23286 emit_insn (gen_add3_insn (ptr_reg,
23287 frame_reg_rtx, GEN_INT (frame_off)));
23289 insn = rs6000_emit_savres_rtx (info, ptr_reg,
23290 info->fp_save_offset,
23291 info->lr_save_offset,
23292 DFmode, sel);
23293 rs6000_frame_related (insn, ptr_reg, sp_off,
23294 NULL_RTX, NULL_RTX, NULL_RTX);
23295 if (lr)
23296 END_USE (0);
23299 /* Save GPRs. This is done as a PARALLEL if we are using
23300 the store-multiple instructions. */
23301 if (!WORLD_SAVE_P (info)
23302 && TARGET_SPE_ABI
23303 && info->spe_64bit_regs_used != 0
23304 && info->first_gp_reg_save != 32)
23306 int i;
23307 rtx spe_save_area_ptr;
23308 HOST_WIDE_INT save_off;
23309 int ool_adjust = 0;
23311 /* Determine whether we can address all of the registers that need
23312 to be saved with an offset from frame_reg_rtx that fits in
23313 the small const field for SPE memory instructions. */
23314 int spe_regs_addressable
23315 = (SPE_CONST_OFFSET_OK (info->spe_gp_save_offset + frame_off
23316 + reg_size * (32 - info->first_gp_reg_save - 1))
23317 && (strategy & SAVE_INLINE_GPRS));
23319 if (spe_regs_addressable)
23321 spe_save_area_ptr = frame_reg_rtx;
23322 save_off = frame_off;
23324 else
23326 /* Make r11 point to the start of the SPE save area. We need
23327 to be careful here if r11 is holding the static chain. If
23328 it is, then temporarily save it in r0. */
23329 HOST_WIDE_INT offset;
23331 if (!(strategy & SAVE_INLINE_GPRS))
23332 ool_adjust = 8 * (info->first_gp_reg_save - FIRST_SAVED_GP_REGNO);
23333 offset = info->spe_gp_save_offset + frame_off - ool_adjust;
23334 spe_save_area_ptr = gen_rtx_REG (Pmode, 11);
23335 save_off = frame_off - offset;
23337 if (using_static_chain_p)
23339 rtx r0 = gen_rtx_REG (Pmode, 0);
23341 START_USE (0);
23342 gcc_assert (info->first_gp_reg_save > 11);
23344 emit_move_insn (r0, spe_save_area_ptr);
23346 else if (REGNO (frame_reg_rtx) != 11)
23347 START_USE (11);
23349 emit_insn (gen_addsi3 (spe_save_area_ptr,
23350 frame_reg_rtx, GEN_INT (offset)));
23351 if (!using_static_chain_p && REGNO (frame_reg_rtx) == 11)
23352 frame_off = -info->spe_gp_save_offset + ool_adjust;
23355 if ((strategy & SAVE_INLINE_GPRS))
23357 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
23358 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
23359 emit_frame_save (spe_save_area_ptr, reg_mode,
23360 info->first_gp_reg_save + i,
23361 (info->spe_gp_save_offset + save_off
23362 + reg_size * i),
23363 sp_off - save_off);
23365 else
23367 insn = rs6000_emit_savres_rtx (info, spe_save_area_ptr,
23368 info->spe_gp_save_offset + save_off,
23369 0, reg_mode,
23370 SAVRES_SAVE | SAVRES_GPR);
23372 rs6000_frame_related (insn, spe_save_area_ptr, sp_off - save_off,
23373 NULL_RTX, NULL_RTX, NULL_RTX);
23376 /* Move the static chain pointer back. */
23377 if (!spe_regs_addressable)
23379 if (using_static_chain_p)
23381 emit_move_insn (spe_save_area_ptr, gen_rtx_REG (Pmode, 0));
23382 END_USE (0);
23384 else if (REGNO (frame_reg_rtx) != 11)
23385 END_USE (11);
23388 else if (!WORLD_SAVE_P (info) && !(strategy & SAVE_INLINE_GPRS))
23390 bool lr = (strategy & SAVE_NOINLINE_GPRS_SAVES_LR) != 0;
23391 int sel = SAVRES_SAVE | SAVRES_GPR | (lr ? SAVRES_LR : 0);
23392 unsigned ptr_regno = ptr_regno_for_savres (sel);
23393 rtx ptr_reg = frame_reg_rtx;
23394 bool ptr_set_up = REGNO (ptr_reg) == ptr_regno;
23395 int end_save = info->gp_save_offset + info->gp_size;
23396 int ptr_off;
23398 if (!ptr_set_up)
23399 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
23401 /* Need to adjust r11 (r12) if we saved any FPRs. */
23402 if (end_save + frame_off != 0)
23404 rtx offset = GEN_INT (end_save + frame_off);
23406 if (ptr_set_up)
23407 frame_off = -end_save;
23408 else
23409 NOT_INUSE (ptr_regno);
23410 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
23412 else if (!ptr_set_up)
23414 NOT_INUSE (ptr_regno);
23415 emit_move_insn (ptr_reg, frame_reg_rtx);
23417 ptr_off = -end_save;
23418 insn = rs6000_emit_savres_rtx (info, ptr_reg,
23419 info->gp_save_offset + ptr_off,
23420 info->lr_save_offset + ptr_off,
23421 reg_mode, sel);
23422 rs6000_frame_related (insn, ptr_reg, sp_off - ptr_off,
23423 NULL_RTX, NULL_RTX, NULL_RTX);
23424 if (lr)
23425 END_USE (0);
23427 else if (!WORLD_SAVE_P (info) && (strategy & SAVRES_MULTIPLE))
23429 rtvec p;
23430 int i;
23431 p = rtvec_alloc (32 - info->first_gp_reg_save);
23432 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
23433 RTVEC_ELT (p, i)
23434 = gen_frame_store (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
23435 frame_reg_rtx,
23436 info->gp_save_offset + frame_off + reg_size * i);
23437 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
23438 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
23439 NULL_RTX, NULL_RTX, NULL_RTX);
23441 else if (!WORLD_SAVE_P (info))
23443 int i;
23444 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
23445 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
23446 emit_frame_save (frame_reg_rtx, reg_mode,
23447 info->first_gp_reg_save + i,
23448 info->gp_save_offset + frame_off + reg_size * i,
23449 sp_off - frame_off);
23452 if (crtl->calls_eh_return)
23454 unsigned int i;
23455 rtvec p;
23457 for (i = 0; ; ++i)
23459 unsigned int regno = EH_RETURN_DATA_REGNO (i);
23460 if (regno == INVALID_REGNUM)
23461 break;
23464 p = rtvec_alloc (i);
23466 for (i = 0; ; ++i)
23468 unsigned int regno = EH_RETURN_DATA_REGNO (i);
23469 if (regno == INVALID_REGNUM)
23470 break;
23472 insn
23473 = gen_frame_store (gen_rtx_REG (reg_mode, regno),
23474 sp_reg_rtx,
23475 info->ehrd_offset + sp_off + reg_size * (int) i);
23476 RTVEC_ELT (p, i) = insn;
23477 RTX_FRAME_RELATED_P (insn) = 1;
23480 insn = emit_insn (gen_blockage ());
23481 RTX_FRAME_RELATED_P (insn) = 1;
23482 add_reg_note (insn, REG_FRAME_RELATED_EXPR, gen_rtx_PARALLEL (VOIDmode, p));
23485 /* In AIX ABI we need to make sure r2 is really saved. */
23486 if (TARGET_AIX && crtl->calls_eh_return)
23488 rtx tmp_reg, tmp_reg_si, hi, lo, compare_result, toc_save_done, jump;
23489 rtx save_insn, join_insn, note;
23490 long toc_restore_insn;
23492 tmp_reg = gen_rtx_REG (Pmode, 11);
23493 tmp_reg_si = gen_rtx_REG (SImode, 11);
23494 if (using_static_chain_p)
23496 START_USE (0);
23497 emit_move_insn (gen_rtx_REG (Pmode, 0), tmp_reg);
23499 else
23500 START_USE (11);
23501 emit_move_insn (tmp_reg, gen_rtx_REG (Pmode, LR_REGNO));
23502 /* Peek at instruction to which this function returns. If it's
23503 restoring r2, then we know we've already saved r2. We can't
23504 unconditionally save r2 because the value we have will already
23505 be updated if we arrived at this function via a plt call or
23506 toc adjusting stub. */
23507 emit_move_insn (tmp_reg_si, gen_rtx_MEM (SImode, tmp_reg));
23508 toc_restore_insn = ((TARGET_32BIT ? 0x80410000 : 0xE8410000)
23509 + RS6000_TOC_SAVE_SLOT);
23510 hi = gen_int_mode (toc_restore_insn & ~0xffff, SImode);
23511 emit_insn (gen_xorsi3 (tmp_reg_si, tmp_reg_si, hi));
23512 compare_result = gen_rtx_REG (CCUNSmode, CR0_REGNO);
23513 validate_condition_mode (EQ, CCUNSmode);
23514 lo = gen_int_mode (toc_restore_insn & 0xffff, SImode);
23515 emit_insn (gen_rtx_SET (VOIDmode, compare_result,
23516 gen_rtx_COMPARE (CCUNSmode, tmp_reg_si, lo)));
23517 toc_save_done = gen_label_rtx ();
23518 jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
23519 gen_rtx_EQ (VOIDmode, compare_result,
23520 const0_rtx),
23521 gen_rtx_LABEL_REF (VOIDmode, toc_save_done),
23522 pc_rtx);
23523 jump = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, jump));
23524 JUMP_LABEL (jump) = toc_save_done;
23525 LABEL_NUSES (toc_save_done) += 1;
23527 save_insn = emit_frame_save (frame_reg_rtx, reg_mode,
23528 TOC_REGNUM, frame_off + RS6000_TOC_SAVE_SLOT,
23529 sp_off - frame_off);
23531 emit_label (toc_save_done);
23533 /* ??? If we leave SAVE_INSN as marked as saving R2, then we'll
23534 have a CFG that has different saves along different paths.
23535 Move the note to a dummy blockage insn, which describes that
23536 R2 is unconditionally saved after the label. */
23537 /* ??? An alternate representation might be a special insn pattern
23538 containing both the branch and the store. That might let the
23539 code that minimizes the number of DW_CFA_advance opcodes better
23540 freedom in placing the annotations. */
23541 note = find_reg_note (save_insn, REG_FRAME_RELATED_EXPR, NULL);
23542 if (note)
23543 remove_note (save_insn, note);
23544 else
23545 note = alloc_reg_note (REG_FRAME_RELATED_EXPR,
23546 copy_rtx (PATTERN (save_insn)), NULL_RTX);
23547 RTX_FRAME_RELATED_P (save_insn) = 0;
23549 join_insn = emit_insn (gen_blockage ());
23550 REG_NOTES (join_insn) = note;
23551 RTX_FRAME_RELATED_P (join_insn) = 1;
23553 if (using_static_chain_p)
23555 emit_move_insn (tmp_reg, gen_rtx_REG (Pmode, 0));
23556 END_USE (0);
23558 else
23559 END_USE (11);
23562 /* Save CR if we use any that must be preserved. */
23563 if (!WORLD_SAVE_P (info) && info->cr_save_p)
23565 rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
23566 GEN_INT (info->cr_save_offset + frame_off));
23567 rtx mem = gen_frame_mem (SImode, addr);
23569 /* If we didn't copy cr before, do so now using r0. */
23570 if (cr_save_rtx == NULL_RTX)
23572 START_USE (0);
23573 cr_save_rtx = gen_rtx_REG (SImode, 0);
23574 rs6000_emit_move_from_cr (cr_save_rtx);
23577 /* Saving CR requires a two-instruction sequence: one instruction
23578 to move the CR to a general-purpose register, and a second
23579 instruction that stores the GPR to memory.
23581 We do not emit any DWARF CFI records for the first of these,
23582 because we cannot properly represent the fact that CR is saved in
23583 a register. One reason is that we cannot express that multiple
23584 CR fields are saved; another reason is that on 64-bit, the size
23585 of the CR register in DWARF (4 bytes) differs from the size of
23586 a general-purpose register.
23588 This means if any intervening instruction were to clobber one of
23589 the call-saved CR fields, we'd have incorrect CFI. To prevent
23590 this from happening, we mark the store to memory as a use of
23591 those CR fields, which prevents any such instruction from being
23592 scheduled in between the two instructions. */
23593 rtx crsave_v[9];
23594 int n_crsave = 0;
23595 int i;
23597 crsave_v[n_crsave++] = gen_rtx_SET (VOIDmode, mem, cr_save_rtx);
23598 for (i = 0; i < 8; i++)
23599 if (save_reg_p (CR0_REGNO + i))
23600 crsave_v[n_crsave++]
23601 = gen_rtx_USE (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO + i));
23603 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode,
23604 gen_rtvec_v (n_crsave, crsave_v)));
23605 END_USE (REGNO (cr_save_rtx));
23607 /* Now, there's no way that dwarf2out_frame_debug_expr is going to
23608 understand '(unspec:SI [(reg:CC 68) ...] UNSPEC_MOVESI_FROM_CR)',
23609 so we need to construct a frame expression manually. */
23610 RTX_FRAME_RELATED_P (insn) = 1;
23612 /* Update address to be stack-pointer relative, like
23613 rs6000_frame_related would do. */
23614 addr = gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode, STACK_POINTER_REGNUM),
23615 GEN_INT (info->cr_save_offset + sp_off));
23616 mem = gen_frame_mem (SImode, addr);
23618 if (DEFAULT_ABI == ABI_ELFv2)
23620 /* In the ELFv2 ABI we generate separate CFI records for each
23621 CR field that was actually saved. They all point to the
23622 same 32-bit stack slot. */
23623 rtx crframe[8];
23624 int n_crframe = 0;
23626 for (i = 0; i < 8; i++)
23627 if (save_reg_p (CR0_REGNO + i))
23629 crframe[n_crframe]
23630 = gen_rtx_SET (VOIDmode, mem,
23631 gen_rtx_REG (SImode, CR0_REGNO + i));
23633 RTX_FRAME_RELATED_P (crframe[n_crframe]) = 1;
23634 n_crframe++;
23637 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
23638 gen_rtx_PARALLEL (VOIDmode,
23639 gen_rtvec_v (n_crframe, crframe)));
23641 else
23643 /* In other ABIs, by convention, we use a single CR regnum to
23644 represent the fact that all call-saved CR fields are saved.
23645 We use CR2_REGNO to be compatible with gcc-2.95 on Linux. */
23646 rtx set = gen_rtx_SET (VOIDmode, mem,
23647 gen_rtx_REG (SImode, CR2_REGNO));
23648 add_reg_note (insn, REG_FRAME_RELATED_EXPR, set);
23652 /* In the ELFv2 ABI we need to save all call-saved CR fields into
23653 *separate* slots if the routine calls __builtin_eh_return, so
23654 that they can be independently restored by the unwinder. */
23655 if (DEFAULT_ABI == ABI_ELFv2 && crtl->calls_eh_return)
23657 int i, cr_off = info->ehcr_offset;
23658 rtx crsave;
23660 /* ??? We might get better performance by using multiple mfocrf
23661 instructions. */
23662 crsave = gen_rtx_REG (SImode, 0);
23663 emit_insn (gen_movesi_from_cr (crsave));
23665 for (i = 0; i < 8; i++)
23666 if (!call_used_regs[CR0_REGNO + i])
23668 rtvec p = rtvec_alloc (2);
23669 RTVEC_ELT (p, 0)
23670 = gen_frame_store (crsave, frame_reg_rtx, cr_off + frame_off);
23671 RTVEC_ELT (p, 1)
23672 = gen_rtx_USE (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO + i));
23674 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
23676 RTX_FRAME_RELATED_P (insn) = 1;
23677 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
23678 gen_frame_store (gen_rtx_REG (SImode, CR0_REGNO + i),
23679 sp_reg_rtx, cr_off + sp_off));
23681 cr_off += reg_size;
23685 /* Update stack and set back pointer unless this is V.4,
23686 for which it was done previously. */
23687 if (!WORLD_SAVE_P (info) && info->push_p
23688 && !(DEFAULT_ABI == ABI_V4 || crtl->calls_eh_return))
23690 rtx ptr_reg = NULL;
23691 int ptr_off = 0;
23693 /* If saving altivec regs we need to be able to address all save
23694 locations using a 16-bit offset. */
23695 if ((strategy & SAVE_INLINE_VRS) == 0
23696 || (info->altivec_size != 0
23697 && (info->altivec_save_offset + info->altivec_size - 16
23698 + info->total_size - frame_off) > 32767)
23699 || (info->vrsave_size != 0
23700 && (info->vrsave_save_offset
23701 + info->total_size - frame_off) > 32767))
23703 int sel = SAVRES_SAVE | SAVRES_VR;
23704 unsigned ptr_regno = ptr_regno_for_savres (sel);
23706 if (using_static_chain_p
23707 && ptr_regno == STATIC_CHAIN_REGNUM)
23708 ptr_regno = 12;
23709 if (REGNO (frame_reg_rtx) != ptr_regno)
23710 START_USE (ptr_regno);
23711 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
23712 frame_reg_rtx = ptr_reg;
23713 ptr_off = info->altivec_save_offset + info->altivec_size;
23714 frame_off = -ptr_off;
23716 else if (REGNO (frame_reg_rtx) == 1)
23717 frame_off = info->total_size;
23718 rs6000_emit_allocate_stack (info->total_size, ptr_reg, ptr_off);
23719 sp_off = info->total_size;
23720 if (frame_reg_rtx != sp_reg_rtx)
23721 rs6000_emit_stack_tie (frame_reg_rtx, false);
23724 /* Set frame pointer, if needed. */
23725 if (frame_pointer_needed)
23727 insn = emit_move_insn (gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
23728 sp_reg_rtx);
23729 RTX_FRAME_RELATED_P (insn) = 1;
23732 /* Save AltiVec registers if needed. Save here because the red zone does
23733 not always include AltiVec registers. */
23734 if (!WORLD_SAVE_P (info) && TARGET_ALTIVEC_ABI
23735 && info->altivec_size != 0 && (strategy & SAVE_INLINE_VRS) == 0)
23737 int end_save = info->altivec_save_offset + info->altivec_size;
23738 int ptr_off;
23739 /* Oddly, the vector save/restore functions point r0 at the end
23740 of the save area, then use r11 or r12 to load offsets for
23741 [reg+reg] addressing. */
23742 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
23743 int scratch_regno = ptr_regno_for_savres (SAVRES_SAVE | SAVRES_VR);
23744 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
23746 gcc_checking_assert (scratch_regno == 11 || scratch_regno == 12);
23747 NOT_INUSE (0);
23748 if (end_save + frame_off != 0)
23750 rtx offset = GEN_INT (end_save + frame_off);
23752 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
23754 else
23755 emit_move_insn (ptr_reg, frame_reg_rtx);
23757 ptr_off = -end_save;
23758 insn = rs6000_emit_savres_rtx (info, scratch_reg,
23759 info->altivec_save_offset + ptr_off,
23760 0, V4SImode, SAVRES_SAVE | SAVRES_VR);
23761 rs6000_frame_related (insn, scratch_reg, sp_off - ptr_off,
23762 NULL_RTX, NULL_RTX, NULL_RTX);
23763 if (REGNO (frame_reg_rtx) == REGNO (scratch_reg))
23765 /* The oddity mentioned above clobbered our frame reg. */
23766 emit_move_insn (frame_reg_rtx, ptr_reg);
23767 frame_off = ptr_off;
23770 else if (!WORLD_SAVE_P (info) && TARGET_ALTIVEC_ABI
23771 && info->altivec_size != 0)
23773 int i;
23775 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
23776 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
23778 rtx areg, savereg, mem, split_reg;
23779 int offset;
23781 offset = (info->altivec_save_offset + frame_off
23782 + 16 * (i - info->first_altivec_reg_save));
23784 savereg = gen_rtx_REG (V4SImode, i);
23786 NOT_INUSE (0);
23787 areg = gen_rtx_REG (Pmode, 0);
23788 emit_move_insn (areg, GEN_INT (offset));
23790 /* AltiVec addressing mode is [reg+reg]. */
23791 mem = gen_frame_mem (V4SImode,
23792 gen_rtx_PLUS (Pmode, frame_reg_rtx, areg));
23794 insn = emit_move_insn (mem, savereg);
23796 /* When we split a VSX store into two insns, we need to make
23797 sure the DWARF info knows which register we are storing.
23798 Pass it in to be used on the appropriate note. */
23799 if (!BYTES_BIG_ENDIAN
23800 && GET_CODE (PATTERN (insn)) == SET
23801 && GET_CODE (SET_SRC (PATTERN (insn))) == VEC_SELECT)
23802 split_reg = savereg;
23803 else
23804 split_reg = NULL_RTX;
23806 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
23807 areg, GEN_INT (offset), split_reg);
23811 /* VRSAVE is a bit vector representing which AltiVec registers
23812 are used. The OS uses this to determine which vector
23813 registers to save on a context switch. We need to save
23814 VRSAVE on the stack frame, add whatever AltiVec registers we
23815 used in this function, and do the corresponding magic in the
23816 epilogue. */
23818 if (!WORLD_SAVE_P (info)
23819 && TARGET_ALTIVEC
23820 && TARGET_ALTIVEC_VRSAVE
23821 && info->vrsave_mask != 0)
23823 rtx reg, vrsave;
23824 int offset;
23825 int save_regno;
23827 /* Get VRSAVE onto a GPR. Note that ABI_V4 and ABI_DARWIN might
23828 be using r12 as frame_reg_rtx and r11 as the static chain
23829 pointer for nested functions. */
23830 save_regno = 12;
23831 if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
23832 && !using_static_chain_p)
23833 save_regno = 11;
23834 else if (REGNO (frame_reg_rtx) == 12)
23836 save_regno = 11;
23837 if (using_static_chain_p)
23838 save_regno = 0;
23841 NOT_INUSE (save_regno);
23842 reg = gen_rtx_REG (SImode, save_regno);
23843 vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
23844 if (TARGET_MACHO)
23845 emit_insn (gen_get_vrsave_internal (reg));
23846 else
23847 emit_insn (gen_rtx_SET (VOIDmode, reg, vrsave));
23849 /* Save VRSAVE. */
23850 offset = info->vrsave_save_offset + frame_off;
23851 insn = emit_insn (gen_frame_store (reg, frame_reg_rtx, offset));
23853 /* Include the registers in the mask. */
23854 emit_insn (gen_iorsi3 (reg, reg, GEN_INT ((int) info->vrsave_mask)));
23856 insn = emit_insn (generate_set_vrsave (reg, info, 0));
23859 /* If we are using RS6000_PIC_OFFSET_TABLE_REGNUM, we need to set it up. */
23860 if (!TARGET_SINGLE_PIC_BASE
23861 && ((TARGET_TOC && TARGET_MINIMAL_TOC && get_pool_size () != 0)
23862 || (DEFAULT_ABI == ABI_V4
23863 && (flag_pic == 1 || (flag_pic && TARGET_SECURE_PLT))
23864 && df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))))
23866 /* If emit_load_toc_table will use the link register, we need to save
23867 it. We use R12 for this purpose because emit_load_toc_table
23868 can use register 0. This allows us to use a plain 'blr' to return
23869 from the procedure more often. */
23870 int save_LR_around_toc_setup = (TARGET_ELF
23871 && DEFAULT_ABI == ABI_V4
23872 && flag_pic
23873 && ! info->lr_save_p
23874 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds) > 0);
23875 if (save_LR_around_toc_setup)
23877 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
23878 rtx tmp = gen_rtx_REG (Pmode, 12);
23880 insn = emit_move_insn (tmp, lr);
23881 RTX_FRAME_RELATED_P (insn) = 1;
23883 rs6000_emit_load_toc_table (TRUE);
23885 insn = emit_move_insn (lr, tmp);
23886 add_reg_note (insn, REG_CFA_RESTORE, lr);
23887 RTX_FRAME_RELATED_P (insn) = 1;
23889 else
23890 rs6000_emit_load_toc_table (TRUE);
23893 #if TARGET_MACHO
23894 if (!TARGET_SINGLE_PIC_BASE
23895 && DEFAULT_ABI == ABI_DARWIN
23896 && flag_pic && crtl->uses_pic_offset_table)
23898 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
23899 rtx src = gen_rtx_SYMBOL_REF (Pmode, MACHOPIC_FUNCTION_BASE_NAME);
23901 /* Save and restore LR locally around this call (in R0). */
23902 if (!info->lr_save_p)
23903 emit_move_insn (gen_rtx_REG (Pmode, 0), lr);
23905 emit_insn (gen_load_macho_picbase (src));
23907 emit_move_insn (gen_rtx_REG (Pmode,
23908 RS6000_PIC_OFFSET_TABLE_REGNUM),
23909 lr);
23911 if (!info->lr_save_p)
23912 emit_move_insn (lr, gen_rtx_REG (Pmode, 0));
23914 #endif
23916 /* If we need to, save the TOC register after doing the stack setup.
23917 Do not emit eh frame info for this save. The unwinder wants info,
23918 conceptually attached to instructions in this function, about
23919 register values in the caller of this function. This R2 may have
23920 already been changed from the value in the caller.
23921 We don't attempt to write accurate DWARF EH frame info for R2
23922 because code emitted by gcc for a (non-pointer) function call
23923 doesn't save and restore R2. Instead, R2 is managed out-of-line
23924 by a linker generated plt call stub when the function resides in
23925 a shared library. This behaviour is costly to describe in DWARF,
23926 both in terms of the size of DWARF info and the time taken in the
23927 unwinder to interpret it. R2 changes, apart from the
23928 calls_eh_return case earlier in this function, are handled by
23929 linux-unwind.h frob_update_context. */
23930 if (rs6000_save_toc_in_prologue_p ())
23932 rtx reg = gen_rtx_REG (reg_mode, TOC_REGNUM);
23933 emit_insn (gen_frame_store (reg, sp_reg_rtx, RS6000_TOC_SAVE_SLOT));
23937 /* Write function prologue. */
23939 static void
23940 rs6000_output_function_prologue (FILE *file,
23941 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
23943 rs6000_stack_t *info = rs6000_stack_info ();
23945 if (TARGET_DEBUG_STACK)
23946 debug_stack_info (info);
23948 /* Write .extern for any function we will call to save and restore
23949 fp values. */
23950 if (info->first_fp_reg_save < 64
23951 && !TARGET_MACHO
23952 && !TARGET_ELF)
23954 char *name;
23955 int regno = info->first_fp_reg_save - 32;
23957 if ((info->savres_strategy & SAVE_INLINE_FPRS) == 0)
23959 bool lr = (info->savres_strategy & SAVE_NOINLINE_FPRS_SAVES_LR) != 0;
23960 int sel = SAVRES_SAVE | SAVRES_FPR | (lr ? SAVRES_LR : 0);
23961 name = rs6000_savres_routine_name (info, regno, sel);
23962 fprintf (file, "\t.extern %s\n", name);
23964 if ((info->savres_strategy & REST_INLINE_FPRS) == 0)
23966 bool lr = (info->savres_strategy
23967 & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
23968 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
23969 name = rs6000_savres_routine_name (info, regno, sel);
23970 fprintf (file, "\t.extern %s\n", name);
23974 /* ELFv2 ABI r2 setup code and local entry point. This must follow
23975 immediately after the global entry point label. */
23976 if (DEFAULT_ABI == ABI_ELFv2 && cfun->machine->r2_setup_needed)
23978 const char *name = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
23980 fprintf (file, "0:\taddis 2,12,.TOC.-0b@ha\n");
23981 fprintf (file, "\taddi 2,2,.TOC.-0b@l\n");
23983 fputs ("\t.localentry\t", file);
23984 assemble_name (file, name);
23985 fputs (",.-", file);
23986 assemble_name (file, name);
23987 fputs ("\n", file);
23990 /* Output -mprofile-kernel code. This needs to be done here instead of
23991 in output_function_profile since it must go after the ELFv2 ABI
23992 local entry point. */
23993 if (TARGET_PROFILE_KERNEL && crtl->profile)
23995 gcc_assert (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2);
23996 gcc_assert (!TARGET_32BIT);
23998 asm_fprintf (file, "\tmflr %s\n", reg_names[0]);
23999 asm_fprintf (file, "\tstd %s,16(%s)\n", reg_names[0], reg_names[1]);
24001 /* In the ELFv2 ABI we have no compiler stack word. It must be
24002 the resposibility of _mcount to preserve the static chain
24003 register if required. */
24004 if (DEFAULT_ABI != ABI_ELFv2
24005 && cfun->static_chain_decl != NULL)
24007 asm_fprintf (file, "\tstd %s,24(%s)\n",
24008 reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
24009 fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
24010 asm_fprintf (file, "\tld %s,24(%s)\n",
24011 reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
24013 else
24014 fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
24017 rs6000_pic_labelno++;
24020 /* Non-zero if vmx regs are restored before the frame pop, zero if
24021 we restore after the pop when possible. */
24022 #define ALWAYS_RESTORE_ALTIVEC_BEFORE_POP 0
24024 /* Restoring cr is a two step process: loading a reg from the frame
24025 save, then moving the reg to cr. For ABI_V4 we must let the
24026 unwinder know that the stack location is no longer valid at or
24027 before the stack deallocation, but we can't emit a cfa_restore for
24028 cr at the stack deallocation like we do for other registers.
24029 The trouble is that it is possible for the move to cr to be
24030 scheduled after the stack deallocation. So say exactly where cr
24031 is located on each of the two insns. */
24033 static rtx
24034 load_cr_save (int regno, rtx frame_reg_rtx, int offset, bool exit_func)
24036 rtx mem = gen_frame_mem_offset (SImode, frame_reg_rtx, offset);
24037 rtx reg = gen_rtx_REG (SImode, regno);
24038 rtx_insn *insn = emit_move_insn (reg, mem);
24040 if (!exit_func && DEFAULT_ABI == ABI_V4)
24042 rtx cr = gen_rtx_REG (SImode, CR2_REGNO);
24043 rtx set = gen_rtx_SET (VOIDmode, reg, cr);
24045 add_reg_note (insn, REG_CFA_REGISTER, set);
24046 RTX_FRAME_RELATED_P (insn) = 1;
24048 return reg;
24051 /* Reload CR from REG. */
24053 static void
24054 restore_saved_cr (rtx reg, int using_mfcr_multiple, bool exit_func)
24056 int count = 0;
24057 int i;
24059 if (using_mfcr_multiple)
24061 for (i = 0; i < 8; i++)
24062 if (save_reg_p (CR0_REGNO + i))
24063 count++;
24064 gcc_assert (count);
24067 if (using_mfcr_multiple && count > 1)
24069 rtx_insn *insn;
24070 rtvec p;
24071 int ndx;
24073 p = rtvec_alloc (count);
24075 ndx = 0;
24076 for (i = 0; i < 8; i++)
24077 if (save_reg_p (CR0_REGNO + i))
24079 rtvec r = rtvec_alloc (2);
24080 RTVEC_ELT (r, 0) = reg;
24081 RTVEC_ELT (r, 1) = GEN_INT (1 << (7-i));
24082 RTVEC_ELT (p, ndx) =
24083 gen_rtx_SET (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO + i),
24084 gen_rtx_UNSPEC (CCmode, r, UNSPEC_MOVESI_TO_CR));
24085 ndx++;
24087 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
24088 gcc_assert (ndx == count);
24090 /* For the ELFv2 ABI we generate a CFA_RESTORE for each
24091 CR field separately. */
24092 if (!exit_func && DEFAULT_ABI == ABI_ELFv2 && flag_shrink_wrap)
24094 for (i = 0; i < 8; i++)
24095 if (save_reg_p (CR0_REGNO + i))
24096 add_reg_note (insn, REG_CFA_RESTORE,
24097 gen_rtx_REG (SImode, CR0_REGNO + i));
24099 RTX_FRAME_RELATED_P (insn) = 1;
24102 else
24103 for (i = 0; i < 8; i++)
24104 if (save_reg_p (CR0_REGNO + i))
24106 rtx insn = emit_insn (gen_movsi_to_cr_one
24107 (gen_rtx_REG (CCmode, CR0_REGNO + i), reg));
24109 /* For the ELFv2 ABI we generate a CFA_RESTORE for each
24110 CR field separately, attached to the insn that in fact
24111 restores this particular CR field. */
24112 if (!exit_func && DEFAULT_ABI == ABI_ELFv2 && flag_shrink_wrap)
24114 add_reg_note (insn, REG_CFA_RESTORE,
24115 gen_rtx_REG (SImode, CR0_REGNO + i));
24117 RTX_FRAME_RELATED_P (insn) = 1;
24121 /* For other ABIs, we just generate a single CFA_RESTORE for CR2. */
24122 if (!exit_func && DEFAULT_ABI != ABI_ELFv2
24123 && (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap))
24125 rtx_insn *insn = get_last_insn ();
24126 rtx cr = gen_rtx_REG (SImode, CR2_REGNO);
24128 add_reg_note (insn, REG_CFA_RESTORE, cr);
24129 RTX_FRAME_RELATED_P (insn) = 1;
24133 /* Like cr, the move to lr instruction can be scheduled after the
24134 stack deallocation, but unlike cr, its stack frame save is still
24135 valid. So we only need to emit the cfa_restore on the correct
24136 instruction. */
24138 static void
24139 load_lr_save (int regno, rtx frame_reg_rtx, int offset)
24141 rtx mem = gen_frame_mem_offset (Pmode, frame_reg_rtx, offset);
24142 rtx reg = gen_rtx_REG (Pmode, regno);
24144 emit_move_insn (reg, mem);
24147 static void
24148 restore_saved_lr (int regno, bool exit_func)
24150 rtx reg = gen_rtx_REG (Pmode, regno);
24151 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
24152 rtx_insn *insn = emit_move_insn (lr, reg);
24154 if (!exit_func && flag_shrink_wrap)
24156 add_reg_note (insn, REG_CFA_RESTORE, lr);
24157 RTX_FRAME_RELATED_P (insn) = 1;
24161 static rtx
24162 add_crlr_cfa_restore (const rs6000_stack_t *info, rtx cfa_restores)
24164 if (DEFAULT_ABI == ABI_ELFv2)
24166 int i;
24167 for (i = 0; i < 8; i++)
24168 if (save_reg_p (CR0_REGNO + i))
24170 rtx cr = gen_rtx_REG (SImode, CR0_REGNO + i);
24171 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, cr,
24172 cfa_restores);
24175 else if (info->cr_save_p)
24176 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
24177 gen_rtx_REG (SImode, CR2_REGNO),
24178 cfa_restores);
24180 if (info->lr_save_p)
24181 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
24182 gen_rtx_REG (Pmode, LR_REGNO),
24183 cfa_restores);
24184 return cfa_restores;
24187 /* Return true if OFFSET from stack pointer can be clobbered by signals.
24188 V.4 doesn't have any stack cushion, AIX ABIs have 220 or 288 bytes
24189 below stack pointer not cloberred by signals. */
24191 static inline bool
24192 offset_below_red_zone_p (HOST_WIDE_INT offset)
24194 return offset < (DEFAULT_ABI == ABI_V4
24196 : TARGET_32BIT ? -220 : -288);
24199 /* Append CFA_RESTORES to any existing REG_NOTES on the last insn. */
24201 static void
24202 emit_cfa_restores (rtx cfa_restores)
24204 rtx_insn *insn = get_last_insn ();
24205 rtx *loc = &REG_NOTES (insn);
24207 while (*loc)
24208 loc = &XEXP (*loc, 1);
24209 *loc = cfa_restores;
24210 RTX_FRAME_RELATED_P (insn) = 1;
24213 /* Emit function epilogue as insns. */
24215 void
24216 rs6000_emit_epilogue (int sibcall)
24218 rs6000_stack_t *info;
24219 int restoring_GPRs_inline;
24220 int restoring_FPRs_inline;
24221 int using_load_multiple;
24222 int using_mtcr_multiple;
24223 int use_backchain_to_restore_sp;
24224 int restore_lr;
24225 int strategy;
24226 HOST_WIDE_INT frame_off = 0;
24227 rtx sp_reg_rtx = gen_rtx_REG (Pmode, 1);
24228 rtx frame_reg_rtx = sp_reg_rtx;
24229 rtx cfa_restores = NULL_RTX;
24230 rtx insn;
24231 rtx cr_save_reg = NULL_RTX;
24232 enum machine_mode reg_mode = Pmode;
24233 int reg_size = TARGET_32BIT ? 4 : 8;
24234 int i;
24235 bool exit_func;
24236 unsigned ptr_regno;
24238 info = rs6000_stack_info ();
24240 if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
24242 reg_mode = V2SImode;
24243 reg_size = 8;
24246 strategy = info->savres_strategy;
24247 using_load_multiple = strategy & SAVRES_MULTIPLE;
24248 restoring_FPRs_inline = sibcall || (strategy & REST_INLINE_FPRS);
24249 restoring_GPRs_inline = sibcall || (strategy & REST_INLINE_GPRS);
24250 using_mtcr_multiple = (rs6000_cpu == PROCESSOR_PPC601
24251 || rs6000_cpu == PROCESSOR_PPC603
24252 || rs6000_cpu == PROCESSOR_PPC750
24253 || optimize_size);
24254 /* Restore via the backchain when we have a large frame, since this
24255 is more efficient than an addis, addi pair. The second condition
24256 here will not trigger at the moment; We don't actually need a
24257 frame pointer for alloca, but the generic parts of the compiler
24258 give us one anyway. */
24259 use_backchain_to_restore_sp = (info->total_size > 32767 - info->lr_save_offset
24260 || (cfun->calls_alloca
24261 && !frame_pointer_needed));
24262 restore_lr = (info->lr_save_p
24263 && (restoring_FPRs_inline
24264 || (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR))
24265 && (restoring_GPRs_inline
24266 || info->first_fp_reg_save < 64));
24268 if (WORLD_SAVE_P (info))
24270 int i, j;
24271 char rname[30];
24272 const char *alloc_rname;
24273 rtvec p;
24275 /* eh_rest_world_r10 will return to the location saved in the LR
24276 stack slot (which is not likely to be our caller.)
24277 Input: R10 -- stack adjustment. Clobbers R0, R11, R12, R7, R8.
24278 rest_world is similar, except any R10 parameter is ignored.
24279 The exception-handling stuff that was here in 2.95 is no
24280 longer necessary. */
24282 p = rtvec_alloc (9
24284 + 32 - info->first_gp_reg_save
24285 + LAST_ALTIVEC_REGNO + 1 - info->first_altivec_reg_save
24286 + 63 + 1 - info->first_fp_reg_save);
24288 strcpy (rname, ((crtl->calls_eh_return) ?
24289 "*eh_rest_world_r10" : "*rest_world"));
24290 alloc_rname = ggc_strdup (rname);
24292 j = 0;
24293 RTVEC_ELT (p, j++) = ret_rtx;
24294 RTVEC_ELT (p, j++) = gen_rtx_USE (VOIDmode,
24295 gen_rtx_REG (Pmode,
24296 LR_REGNO));
24297 RTVEC_ELT (p, j++)
24298 = gen_rtx_USE (VOIDmode, gen_rtx_SYMBOL_REF (Pmode, alloc_rname));
24299 /* The instruction pattern requires a clobber here;
24300 it is shared with the restVEC helper. */
24301 RTVEC_ELT (p, j++)
24302 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 11));
24305 /* CR register traditionally saved as CR2. */
24306 rtx reg = gen_rtx_REG (SImode, CR2_REGNO);
24307 RTVEC_ELT (p, j++)
24308 = gen_frame_load (reg, frame_reg_rtx, info->cr_save_offset);
24309 if (flag_shrink_wrap)
24311 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
24312 gen_rtx_REG (Pmode, LR_REGNO),
24313 cfa_restores);
24314 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
24318 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
24320 rtx reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
24321 RTVEC_ELT (p, j++)
24322 = gen_frame_load (reg,
24323 frame_reg_rtx, info->gp_save_offset + reg_size * i);
24324 if (flag_shrink_wrap)
24325 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
24327 for (i = 0; info->first_altivec_reg_save + i <= LAST_ALTIVEC_REGNO; i++)
24329 rtx reg = gen_rtx_REG (V4SImode, info->first_altivec_reg_save + i);
24330 RTVEC_ELT (p, j++)
24331 = gen_frame_load (reg,
24332 frame_reg_rtx, info->altivec_save_offset + 16 * i);
24333 if (flag_shrink_wrap)
24334 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
24336 for (i = 0; info->first_fp_reg_save + i <= 63; i++)
24338 rtx reg = gen_rtx_REG ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
24339 ? DFmode : SFmode),
24340 info->first_fp_reg_save + i);
24341 RTVEC_ELT (p, j++)
24342 = gen_frame_load (reg, frame_reg_rtx, info->fp_save_offset + 8 * i);
24343 if (flag_shrink_wrap)
24344 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
24346 RTVEC_ELT (p, j++)
24347 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 0));
24348 RTVEC_ELT (p, j++)
24349 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 12));
24350 RTVEC_ELT (p, j++)
24351 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 7));
24352 RTVEC_ELT (p, j++)
24353 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 8));
24354 RTVEC_ELT (p, j++)
24355 = gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, 10));
24356 insn = emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
24358 if (flag_shrink_wrap)
24360 REG_NOTES (insn) = cfa_restores;
24361 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
24362 RTX_FRAME_RELATED_P (insn) = 1;
24364 return;
24367 /* frame_reg_rtx + frame_off points to the top of this stack frame. */
24368 if (info->push_p)
24369 frame_off = info->total_size;
24371 /* Restore AltiVec registers if we must do so before adjusting the
24372 stack. */
24373 if (TARGET_ALTIVEC_ABI
24374 && info->altivec_size != 0
24375 && (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
24376 || (DEFAULT_ABI != ABI_V4
24377 && offset_below_red_zone_p (info->altivec_save_offset))))
24379 int i;
24380 int scratch_regno = ptr_regno_for_savres (SAVRES_VR);
24382 gcc_checking_assert (scratch_regno == 11 || scratch_regno == 12);
24383 if (use_backchain_to_restore_sp)
24385 int frame_regno = 11;
24387 if ((strategy & REST_INLINE_VRS) == 0)
24389 /* Of r11 and r12, select the one not clobbered by an
24390 out-of-line restore function for the frame register. */
24391 frame_regno = 11 + 12 - scratch_regno;
24393 frame_reg_rtx = gen_rtx_REG (Pmode, frame_regno);
24394 emit_move_insn (frame_reg_rtx,
24395 gen_rtx_MEM (Pmode, sp_reg_rtx));
24396 frame_off = 0;
24398 else if (frame_pointer_needed)
24399 frame_reg_rtx = hard_frame_pointer_rtx;
24401 if ((strategy & REST_INLINE_VRS) == 0)
24403 int end_save = info->altivec_save_offset + info->altivec_size;
24404 int ptr_off;
24405 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
24406 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
24408 if (end_save + frame_off != 0)
24410 rtx offset = GEN_INT (end_save + frame_off);
24412 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
24414 else
24415 emit_move_insn (ptr_reg, frame_reg_rtx);
24417 ptr_off = -end_save;
24418 insn = rs6000_emit_savres_rtx (info, scratch_reg,
24419 info->altivec_save_offset + ptr_off,
24420 0, V4SImode, SAVRES_VR);
24422 else
24424 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
24425 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
24427 rtx addr, areg, mem, reg;
24429 areg = gen_rtx_REG (Pmode, 0);
24430 emit_move_insn
24431 (areg, GEN_INT (info->altivec_save_offset
24432 + frame_off
24433 + 16 * (i - info->first_altivec_reg_save)));
24435 /* AltiVec addressing mode is [reg+reg]. */
24436 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
24437 mem = gen_frame_mem (V4SImode, addr);
24439 reg = gen_rtx_REG (V4SImode, i);
24440 emit_move_insn (reg, mem);
24444 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
24445 if (((strategy & REST_INLINE_VRS) == 0
24446 || (info->vrsave_mask & ALTIVEC_REG_BIT (i)) != 0)
24447 && (flag_shrink_wrap
24448 || (offset_below_red_zone_p
24449 (info->altivec_save_offset
24450 + 16 * (i - info->first_altivec_reg_save)))))
24452 rtx reg = gen_rtx_REG (V4SImode, i);
24453 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
24457 /* Restore VRSAVE if we must do so before adjusting the stack. */
24458 if (TARGET_ALTIVEC
24459 && TARGET_ALTIVEC_VRSAVE
24460 && info->vrsave_mask != 0
24461 && (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
24462 || (DEFAULT_ABI != ABI_V4
24463 && offset_below_red_zone_p (info->vrsave_save_offset))))
24465 rtx reg;
24467 if (frame_reg_rtx == sp_reg_rtx)
24469 if (use_backchain_to_restore_sp)
24471 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
24472 emit_move_insn (frame_reg_rtx,
24473 gen_rtx_MEM (Pmode, sp_reg_rtx));
24474 frame_off = 0;
24476 else if (frame_pointer_needed)
24477 frame_reg_rtx = hard_frame_pointer_rtx;
24480 reg = gen_rtx_REG (SImode, 12);
24481 emit_insn (gen_frame_load (reg, frame_reg_rtx,
24482 info->vrsave_save_offset + frame_off));
24484 emit_insn (generate_set_vrsave (reg, info, 1));
24487 insn = NULL_RTX;
24488 /* If we have a large stack frame, restore the old stack pointer
24489 using the backchain. */
24490 if (use_backchain_to_restore_sp)
24492 if (frame_reg_rtx == sp_reg_rtx)
24494 /* Under V.4, don't reset the stack pointer until after we're done
24495 loading the saved registers. */
24496 if (DEFAULT_ABI == ABI_V4)
24497 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
24499 insn = emit_move_insn (frame_reg_rtx,
24500 gen_rtx_MEM (Pmode, sp_reg_rtx));
24501 frame_off = 0;
24503 else if (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
24504 && DEFAULT_ABI == ABI_V4)
24505 /* frame_reg_rtx has been set up by the altivec restore. */
24507 else
24509 insn = emit_move_insn (sp_reg_rtx, frame_reg_rtx);
24510 frame_reg_rtx = sp_reg_rtx;
24513 /* If we have a frame pointer, we can restore the old stack pointer
24514 from it. */
24515 else if (frame_pointer_needed)
24517 frame_reg_rtx = sp_reg_rtx;
24518 if (DEFAULT_ABI == ABI_V4)
24519 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
24520 /* Prevent reordering memory accesses against stack pointer restore. */
24521 else if (cfun->calls_alloca
24522 || offset_below_red_zone_p (-info->total_size))
24523 rs6000_emit_stack_tie (frame_reg_rtx, true);
24525 insn = emit_insn (gen_add3_insn (frame_reg_rtx, hard_frame_pointer_rtx,
24526 GEN_INT (info->total_size)));
24527 frame_off = 0;
24529 else if (info->push_p
24530 && DEFAULT_ABI != ABI_V4
24531 && !crtl->calls_eh_return)
24533 /* Prevent reordering memory accesses against stack pointer restore. */
24534 if (cfun->calls_alloca
24535 || offset_below_red_zone_p (-info->total_size))
24536 rs6000_emit_stack_tie (frame_reg_rtx, false);
24537 insn = emit_insn (gen_add3_insn (sp_reg_rtx, sp_reg_rtx,
24538 GEN_INT (info->total_size)));
24539 frame_off = 0;
24541 if (insn && frame_reg_rtx == sp_reg_rtx)
24543 if (cfa_restores)
24545 REG_NOTES (insn) = cfa_restores;
24546 cfa_restores = NULL_RTX;
24548 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
24549 RTX_FRAME_RELATED_P (insn) = 1;
24552 /* Restore AltiVec registers if we have not done so already. */
24553 if (!ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
24554 && TARGET_ALTIVEC_ABI
24555 && info->altivec_size != 0
24556 && (DEFAULT_ABI == ABI_V4
24557 || !offset_below_red_zone_p (info->altivec_save_offset)))
24559 int i;
24561 if ((strategy & REST_INLINE_VRS) == 0)
24563 int end_save = info->altivec_save_offset + info->altivec_size;
24564 int ptr_off;
24565 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
24566 int scratch_regno = ptr_regno_for_savres (SAVRES_VR);
24567 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
24569 if (end_save + frame_off != 0)
24571 rtx offset = GEN_INT (end_save + frame_off);
24573 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
24575 else
24576 emit_move_insn (ptr_reg, frame_reg_rtx);
24578 ptr_off = -end_save;
24579 insn = rs6000_emit_savres_rtx (info, scratch_reg,
24580 info->altivec_save_offset + ptr_off,
24581 0, V4SImode, SAVRES_VR);
24582 if (REGNO (frame_reg_rtx) == REGNO (scratch_reg))
24584 /* Frame reg was clobbered by out-of-line save. Restore it
24585 from ptr_reg, and if we are calling out-of-line gpr or
24586 fpr restore set up the correct pointer and offset. */
24587 unsigned newptr_regno = 1;
24588 if (!restoring_GPRs_inline)
24590 bool lr = info->gp_save_offset + info->gp_size == 0;
24591 int sel = SAVRES_GPR | (lr ? SAVRES_LR : 0);
24592 newptr_regno = ptr_regno_for_savres (sel);
24593 end_save = info->gp_save_offset + info->gp_size;
24595 else if (!restoring_FPRs_inline)
24597 bool lr = !(strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR);
24598 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
24599 newptr_regno = ptr_regno_for_savres (sel);
24600 end_save = info->gp_save_offset + info->gp_size;
24603 if (newptr_regno != 1 && REGNO (frame_reg_rtx) != newptr_regno)
24604 frame_reg_rtx = gen_rtx_REG (Pmode, newptr_regno);
24606 if (end_save + ptr_off != 0)
24608 rtx offset = GEN_INT (end_save + ptr_off);
24610 frame_off = -end_save;
24611 emit_insn (gen_add3_insn (frame_reg_rtx, ptr_reg, offset));
24613 else
24615 frame_off = ptr_off;
24616 emit_move_insn (frame_reg_rtx, ptr_reg);
24620 else
24622 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
24623 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
24625 rtx addr, areg, mem, reg;
24627 areg = gen_rtx_REG (Pmode, 0);
24628 emit_move_insn
24629 (areg, GEN_INT (info->altivec_save_offset
24630 + frame_off
24631 + 16 * (i - info->first_altivec_reg_save)));
24633 /* AltiVec addressing mode is [reg+reg]. */
24634 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
24635 mem = gen_frame_mem (V4SImode, addr);
24637 reg = gen_rtx_REG (V4SImode, i);
24638 emit_move_insn (reg, mem);
24642 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
24643 if (((strategy & REST_INLINE_VRS) == 0
24644 || (info->vrsave_mask & ALTIVEC_REG_BIT (i)) != 0)
24645 && (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap))
24647 rtx reg = gen_rtx_REG (V4SImode, i);
24648 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
24652 /* Restore VRSAVE if we have not done so already. */
24653 if (!ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
24654 && TARGET_ALTIVEC
24655 && TARGET_ALTIVEC_VRSAVE
24656 && info->vrsave_mask != 0
24657 && (DEFAULT_ABI == ABI_V4
24658 || !offset_below_red_zone_p (info->vrsave_save_offset)))
24660 rtx reg;
24662 reg = gen_rtx_REG (SImode, 12);
24663 emit_insn (gen_frame_load (reg, frame_reg_rtx,
24664 info->vrsave_save_offset + frame_off));
24666 emit_insn (generate_set_vrsave (reg, info, 1));
24669 /* If we exit by an out-of-line restore function on ABI_V4 then that
24670 function will deallocate the stack, so we don't need to worry
24671 about the unwinder restoring cr from an invalid stack frame
24672 location. */
24673 exit_func = (!restoring_FPRs_inline
24674 || (!restoring_GPRs_inline
24675 && info->first_fp_reg_save == 64));
24677 /* In the ELFv2 ABI we need to restore all call-saved CR fields from
24678 *separate* slots if the routine calls __builtin_eh_return, so
24679 that they can be independently restored by the unwinder. */
24680 if (DEFAULT_ABI == ABI_ELFv2 && crtl->calls_eh_return)
24682 int i, cr_off = info->ehcr_offset;
24684 for (i = 0; i < 8; i++)
24685 if (!call_used_regs[CR0_REGNO + i])
24687 rtx reg = gen_rtx_REG (SImode, 0);
24688 emit_insn (gen_frame_load (reg, frame_reg_rtx,
24689 cr_off + frame_off));
24691 insn = emit_insn (gen_movsi_to_cr_one
24692 (gen_rtx_REG (CCmode, CR0_REGNO + i), reg));
24694 if (!exit_func && flag_shrink_wrap)
24696 add_reg_note (insn, REG_CFA_RESTORE,
24697 gen_rtx_REG (SImode, CR0_REGNO + i));
24699 RTX_FRAME_RELATED_P (insn) = 1;
24702 cr_off += reg_size;
24706 /* Get the old lr if we saved it. If we are restoring registers
24707 out-of-line, then the out-of-line routines can do this for us. */
24708 if (restore_lr && restoring_GPRs_inline)
24709 load_lr_save (0, frame_reg_rtx, info->lr_save_offset + frame_off);
24711 /* Get the old cr if we saved it. */
24712 if (info->cr_save_p)
24714 unsigned cr_save_regno = 12;
24716 if (!restoring_GPRs_inline)
24718 /* Ensure we don't use the register used by the out-of-line
24719 gpr register restore below. */
24720 bool lr = info->gp_save_offset + info->gp_size == 0;
24721 int sel = SAVRES_GPR | (lr ? SAVRES_LR : 0);
24722 int gpr_ptr_regno = ptr_regno_for_savres (sel);
24724 if (gpr_ptr_regno == 12)
24725 cr_save_regno = 11;
24726 gcc_checking_assert (REGNO (frame_reg_rtx) != cr_save_regno);
24728 else if (REGNO (frame_reg_rtx) == 12)
24729 cr_save_regno = 11;
24731 cr_save_reg = load_cr_save (cr_save_regno, frame_reg_rtx,
24732 info->cr_save_offset + frame_off,
24733 exit_func);
24736 /* Set LR here to try to overlap restores below. */
24737 if (restore_lr && restoring_GPRs_inline)
24738 restore_saved_lr (0, exit_func);
24740 /* Load exception handler data registers, if needed. */
24741 if (crtl->calls_eh_return)
24743 unsigned int i, regno;
24745 if (TARGET_AIX)
24747 rtx reg = gen_rtx_REG (reg_mode, 2);
24748 emit_insn (gen_frame_load (reg, frame_reg_rtx,
24749 frame_off + RS6000_TOC_SAVE_SLOT));
24752 for (i = 0; ; ++i)
24754 rtx mem;
24756 regno = EH_RETURN_DATA_REGNO (i);
24757 if (regno == INVALID_REGNUM)
24758 break;
24760 /* Note: possible use of r0 here to address SPE regs. */
24761 mem = gen_frame_mem_offset (reg_mode, frame_reg_rtx,
24762 info->ehrd_offset + frame_off
24763 + reg_size * (int) i);
24765 emit_move_insn (gen_rtx_REG (reg_mode, regno), mem);
24769 /* Restore GPRs. This is done as a PARALLEL if we are using
24770 the load-multiple instructions. */
24771 if (TARGET_SPE_ABI
24772 && info->spe_64bit_regs_used
24773 && info->first_gp_reg_save != 32)
24775 /* Determine whether we can address all of the registers that need
24776 to be saved with an offset from frame_reg_rtx that fits in
24777 the small const field for SPE memory instructions. */
24778 int spe_regs_addressable
24779 = (SPE_CONST_OFFSET_OK (info->spe_gp_save_offset + frame_off
24780 + reg_size * (32 - info->first_gp_reg_save - 1))
24781 && restoring_GPRs_inline);
24783 if (!spe_regs_addressable)
24785 int ool_adjust = 0;
24786 rtx old_frame_reg_rtx = frame_reg_rtx;
24787 /* Make r11 point to the start of the SPE save area. We worried about
24788 not clobbering it when we were saving registers in the prologue.
24789 There's no need to worry here because the static chain is passed
24790 anew to every function. */
24792 if (!restoring_GPRs_inline)
24793 ool_adjust = 8 * (info->first_gp_reg_save - FIRST_SAVED_GP_REGNO);
24794 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
24795 emit_insn (gen_addsi3 (frame_reg_rtx, old_frame_reg_rtx,
24796 GEN_INT (info->spe_gp_save_offset
24797 + frame_off
24798 - ool_adjust)));
24799 /* Keep the invariant that frame_reg_rtx + frame_off points
24800 at the top of the stack frame. */
24801 frame_off = -info->spe_gp_save_offset + ool_adjust;
24804 if (restoring_GPRs_inline)
24806 HOST_WIDE_INT spe_offset = info->spe_gp_save_offset + frame_off;
24808 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
24809 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
24811 rtx offset, addr, mem, reg;
24813 /* We're doing all this to ensure that the immediate offset
24814 fits into the immediate field of 'evldd'. */
24815 gcc_assert (SPE_CONST_OFFSET_OK (spe_offset + reg_size * i));
24817 offset = GEN_INT (spe_offset + reg_size * i);
24818 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, offset);
24819 mem = gen_rtx_MEM (V2SImode, addr);
24820 reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
24822 emit_move_insn (reg, mem);
24825 else
24826 rs6000_emit_savres_rtx (info, frame_reg_rtx,
24827 info->spe_gp_save_offset + frame_off,
24828 info->lr_save_offset + frame_off,
24829 reg_mode,
24830 SAVRES_GPR | SAVRES_LR);
24832 else if (!restoring_GPRs_inline)
24834 /* We are jumping to an out-of-line function. */
24835 rtx ptr_reg;
24836 int end_save = info->gp_save_offset + info->gp_size;
24837 bool can_use_exit = end_save == 0;
24838 int sel = SAVRES_GPR | (can_use_exit ? SAVRES_LR : 0);
24839 int ptr_off;
24841 /* Emit stack reset code if we need it. */
24842 ptr_regno = ptr_regno_for_savres (sel);
24843 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
24844 if (can_use_exit)
24845 rs6000_emit_stack_reset (info, frame_reg_rtx, frame_off, ptr_regno);
24846 else if (end_save + frame_off != 0)
24847 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx,
24848 GEN_INT (end_save + frame_off)));
24849 else if (REGNO (frame_reg_rtx) != ptr_regno)
24850 emit_move_insn (ptr_reg, frame_reg_rtx);
24851 if (REGNO (frame_reg_rtx) == ptr_regno)
24852 frame_off = -end_save;
24854 if (can_use_exit && info->cr_save_p)
24855 restore_saved_cr (cr_save_reg, using_mtcr_multiple, true);
24857 ptr_off = -end_save;
24858 rs6000_emit_savres_rtx (info, ptr_reg,
24859 info->gp_save_offset + ptr_off,
24860 info->lr_save_offset + ptr_off,
24861 reg_mode, sel);
24863 else if (using_load_multiple)
24865 rtvec p;
24866 p = rtvec_alloc (32 - info->first_gp_reg_save);
24867 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
24868 RTVEC_ELT (p, i)
24869 = gen_frame_load (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
24870 frame_reg_rtx,
24871 info->gp_save_offset + frame_off + reg_size * i);
24872 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
24874 else
24876 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
24877 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
24878 emit_insn (gen_frame_load
24879 (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
24880 frame_reg_rtx,
24881 info->gp_save_offset + frame_off + reg_size * i));
24884 if (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
24886 /* If the frame pointer was used then we can't delay emitting
24887 a REG_CFA_DEF_CFA note. This must happen on the insn that
24888 restores the frame pointer, r31. We may have already emitted
24889 a REG_CFA_DEF_CFA note, but that's OK; A duplicate is
24890 discarded by dwarf2cfi.c/dwarf2out.c, and in any case would
24891 be harmless if emitted. */
24892 if (frame_pointer_needed)
24894 insn = get_last_insn ();
24895 add_reg_note (insn, REG_CFA_DEF_CFA,
24896 plus_constant (Pmode, frame_reg_rtx, frame_off));
24897 RTX_FRAME_RELATED_P (insn) = 1;
24900 /* Set up cfa_restores. We always need these when
24901 shrink-wrapping. If not shrink-wrapping then we only need
24902 the cfa_restore when the stack location is no longer valid.
24903 The cfa_restores must be emitted on or before the insn that
24904 invalidates the stack, and of course must not be emitted
24905 before the insn that actually does the restore. The latter
24906 is why it is a bad idea to emit the cfa_restores as a group
24907 on the last instruction here that actually does a restore:
24908 That insn may be reordered with respect to others doing
24909 restores. */
24910 if (flag_shrink_wrap
24911 && !restoring_GPRs_inline
24912 && info->first_fp_reg_save == 64)
24913 cfa_restores = add_crlr_cfa_restore (info, cfa_restores);
24915 for (i = info->first_gp_reg_save; i < 32; i++)
24916 if (!restoring_GPRs_inline
24917 || using_load_multiple
24918 || rs6000_reg_live_or_pic_offset_p (i))
24920 rtx reg = gen_rtx_REG (reg_mode, i);
24922 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
24926 if (!restoring_GPRs_inline
24927 && info->first_fp_reg_save == 64)
24929 /* We are jumping to an out-of-line function. */
24930 if (cfa_restores)
24931 emit_cfa_restores (cfa_restores);
24932 return;
24935 if (restore_lr && !restoring_GPRs_inline)
24937 load_lr_save (0, frame_reg_rtx, info->lr_save_offset + frame_off);
24938 restore_saved_lr (0, exit_func);
24941 /* Restore fpr's if we need to do it without calling a function. */
24942 if (restoring_FPRs_inline)
24943 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
24944 if (save_reg_p (info->first_fp_reg_save + i))
24946 rtx reg = gen_rtx_REG ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
24947 ? DFmode : SFmode),
24948 info->first_fp_reg_save + i);
24949 emit_insn (gen_frame_load (reg, frame_reg_rtx,
24950 info->fp_save_offset + frame_off + 8 * i));
24951 if (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
24952 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
24955 /* If we saved cr, restore it here. Just those that were used. */
24956 if (info->cr_save_p)
24957 restore_saved_cr (cr_save_reg, using_mtcr_multiple, exit_func);
24959 /* If this is V.4, unwind the stack pointer after all of the loads
24960 have been done, or set up r11 if we are restoring fp out of line. */
24961 ptr_regno = 1;
24962 if (!restoring_FPRs_inline)
24964 bool lr = (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
24965 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
24966 ptr_regno = ptr_regno_for_savres (sel);
24969 insn = rs6000_emit_stack_reset (info, frame_reg_rtx, frame_off, ptr_regno);
24970 if (REGNO (frame_reg_rtx) == ptr_regno)
24971 frame_off = 0;
24973 if (insn && restoring_FPRs_inline)
24975 if (cfa_restores)
24977 REG_NOTES (insn) = cfa_restores;
24978 cfa_restores = NULL_RTX;
24980 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
24981 RTX_FRAME_RELATED_P (insn) = 1;
24984 if (crtl->calls_eh_return)
24986 rtx sa = EH_RETURN_STACKADJ_RTX;
24987 emit_insn (gen_add3_insn (sp_reg_rtx, sp_reg_rtx, sa));
24990 if (!sibcall)
24992 rtvec p;
24993 bool lr = (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
24994 if (! restoring_FPRs_inline)
24996 p = rtvec_alloc (4 + 64 - info->first_fp_reg_save);
24997 RTVEC_ELT (p, 0) = ret_rtx;
24999 else
25001 if (cfa_restores)
25003 /* We can't hang the cfa_restores off a simple return,
25004 since the shrink-wrap code sometimes uses an existing
25005 return. This means there might be a path from
25006 pre-prologue code to this return, and dwarf2cfi code
25007 wants the eh_frame unwinder state to be the same on
25008 all paths to any point. So we need to emit the
25009 cfa_restores before the return. For -m64 we really
25010 don't need epilogue cfa_restores at all, except for
25011 this irritating dwarf2cfi with shrink-wrap
25012 requirement; The stack red-zone means eh_frame info
25013 from the prologue telling the unwinder to restore
25014 from the stack is perfectly good right to the end of
25015 the function. */
25016 emit_insn (gen_blockage ());
25017 emit_cfa_restores (cfa_restores);
25018 cfa_restores = NULL_RTX;
25020 p = rtvec_alloc (2);
25021 RTVEC_ELT (p, 0) = simple_return_rtx;
25024 RTVEC_ELT (p, 1) = ((restoring_FPRs_inline || !lr)
25025 ? gen_rtx_USE (VOIDmode,
25026 gen_rtx_REG (Pmode, LR_REGNO))
25027 : gen_rtx_CLOBBER (VOIDmode,
25028 gen_rtx_REG (Pmode, LR_REGNO)));
25030 /* If we have to restore more than two FP registers, branch to the
25031 restore function. It will return to our caller. */
25032 if (! restoring_FPRs_inline)
25034 int i;
25035 int reg;
25036 rtx sym;
25038 if (flag_shrink_wrap)
25039 cfa_restores = add_crlr_cfa_restore (info, cfa_restores);
25041 sym = rs6000_savres_routine_sym (info,
25042 SAVRES_FPR | (lr ? SAVRES_LR : 0));
25043 RTVEC_ELT (p, 2) = gen_rtx_USE (VOIDmode, sym);
25044 reg = (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)? 1 : 11;
25045 RTVEC_ELT (p, 3) = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, reg));
25047 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
25049 rtx reg = gen_rtx_REG (DFmode, info->first_fp_reg_save + i);
25051 RTVEC_ELT (p, i + 4)
25052 = gen_frame_load (reg, sp_reg_rtx, info->fp_save_offset + 8 * i);
25053 if (flag_shrink_wrap)
25054 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg,
25055 cfa_restores);
25059 emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
25062 if (cfa_restores)
25064 if (sibcall)
25065 /* Ensure the cfa_restores are hung off an insn that won't
25066 be reordered above other restores. */
25067 emit_insn (gen_blockage ());
25069 emit_cfa_restores (cfa_restores);
25073 /* Write function epilogue. */
25075 static void
25076 rs6000_output_function_epilogue (FILE *file,
25077 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
25079 #if TARGET_MACHO
25080 macho_branch_islands ();
25081 /* Mach-O doesn't support labels at the end of objects, so if
25082 it looks like we might want one, insert a NOP. */
25084 rtx_insn *insn = get_last_insn ();
25085 rtx_insn *deleted_debug_label = NULL;
25086 while (insn
25087 && NOTE_P (insn)
25088 && NOTE_KIND (insn) != NOTE_INSN_DELETED_LABEL)
25090 /* Don't insert a nop for NOTE_INSN_DELETED_DEBUG_LABEL
25091 notes only, instead set their CODE_LABEL_NUMBER to -1,
25092 otherwise there would be code generation differences
25093 in between -g and -g0. */
25094 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
25095 deleted_debug_label = insn;
25096 insn = PREV_INSN (insn);
25098 if (insn
25099 && (LABEL_P (insn)
25100 || (NOTE_P (insn)
25101 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL)))
25102 fputs ("\tnop\n", file);
25103 else if (deleted_debug_label)
25104 for (insn = deleted_debug_label; insn; insn = NEXT_INSN (insn))
25105 if (NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
25106 CODE_LABEL_NUMBER (insn) = -1;
25108 #endif
25110 /* Output a traceback table here. See /usr/include/sys/debug.h for info
25111 on its format.
25113 We don't output a traceback table if -finhibit-size-directive was
25114 used. The documentation for -finhibit-size-directive reads
25115 ``don't output a @code{.size} assembler directive, or anything
25116 else that would cause trouble if the function is split in the
25117 middle, and the two halves are placed at locations far apart in
25118 memory.'' The traceback table has this property, since it
25119 includes the offset from the start of the function to the
25120 traceback table itself.
25122 System V.4 Powerpc's (and the embedded ABI derived from it) use a
25123 different traceback table. */
25124 if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
25125 && ! flag_inhibit_size_directive
25126 && rs6000_traceback != traceback_none && !cfun->is_thunk)
25128 const char *fname = NULL;
25129 const char *language_string = lang_hooks.name;
25130 int fixed_parms = 0, float_parms = 0, parm_info = 0;
25131 int i;
25132 int optional_tbtab;
25133 rs6000_stack_t *info = rs6000_stack_info ();
25135 if (rs6000_traceback == traceback_full)
25136 optional_tbtab = 1;
25137 else if (rs6000_traceback == traceback_part)
25138 optional_tbtab = 0;
25139 else
25140 optional_tbtab = !optimize_size && !TARGET_ELF;
25142 if (optional_tbtab)
25144 fname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
25145 while (*fname == '.') /* V.4 encodes . in the name */
25146 fname++;
25148 /* Need label immediately before tbtab, so we can compute
25149 its offset from the function start. */
25150 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
25151 ASM_OUTPUT_LABEL (file, fname);
25154 /* The .tbtab pseudo-op can only be used for the first eight
25155 expressions, since it can't handle the possibly variable
25156 length fields that follow. However, if you omit the optional
25157 fields, the assembler outputs zeros for all optional fields
25158 anyways, giving each variable length field is minimum length
25159 (as defined in sys/debug.h). Thus we can not use the .tbtab
25160 pseudo-op at all. */
25162 /* An all-zero word flags the start of the tbtab, for debuggers
25163 that have to find it by searching forward from the entry
25164 point or from the current pc. */
25165 fputs ("\t.long 0\n", file);
25167 /* Tbtab format type. Use format type 0. */
25168 fputs ("\t.byte 0,", file);
25170 /* Language type. Unfortunately, there does not seem to be any
25171 official way to discover the language being compiled, so we
25172 use language_string.
25173 C is 0. Fortran is 1. Pascal is 2. Ada is 3. C++ is 9.
25174 Java is 13. Objective-C is 14. Objective-C++ isn't assigned
25175 a number, so for now use 9. LTO and Go aren't assigned numbers
25176 either, so for now use 0. */
25177 if (! strcmp (language_string, "GNU C")
25178 || ! strcmp (language_string, "GNU GIMPLE")
25179 || ! strcmp (language_string, "GNU Go"))
25180 i = 0;
25181 else if (! strcmp (language_string, "GNU F77")
25182 || ! strcmp (language_string, "GNU Fortran"))
25183 i = 1;
25184 else if (! strcmp (language_string, "GNU Pascal"))
25185 i = 2;
25186 else if (! strcmp (language_string, "GNU Ada"))
25187 i = 3;
25188 else if (! strcmp (language_string, "GNU C++")
25189 || ! strcmp (language_string, "GNU Objective-C++"))
25190 i = 9;
25191 else if (! strcmp (language_string, "GNU Java"))
25192 i = 13;
25193 else if (! strcmp (language_string, "GNU Objective-C"))
25194 i = 14;
25195 else
25196 gcc_unreachable ();
25197 fprintf (file, "%d,", i);
25199 /* 8 single bit fields: global linkage (not set for C extern linkage,
25200 apparently a PL/I convention?), out-of-line epilogue/prologue, offset
25201 from start of procedure stored in tbtab, internal function, function
25202 has controlled storage, function has no toc, function uses fp,
25203 function logs/aborts fp operations. */
25204 /* Assume that fp operations are used if any fp reg must be saved. */
25205 fprintf (file, "%d,",
25206 (optional_tbtab << 5) | ((info->first_fp_reg_save != 64) << 1));
25208 /* 6 bitfields: function is interrupt handler, name present in
25209 proc table, function calls alloca, on condition directives
25210 (controls stack walks, 3 bits), saves condition reg, saves
25211 link reg. */
25212 /* The `function calls alloca' bit seems to be set whenever reg 31 is
25213 set up as a frame pointer, even when there is no alloca call. */
25214 fprintf (file, "%d,",
25215 ((optional_tbtab << 6)
25216 | ((optional_tbtab & frame_pointer_needed) << 5)
25217 | (info->cr_save_p << 1)
25218 | (info->lr_save_p)));
25220 /* 3 bitfields: saves backchain, fixup code, number of fpr saved
25221 (6 bits). */
25222 fprintf (file, "%d,",
25223 (info->push_p << 7) | (64 - info->first_fp_reg_save));
25225 /* 2 bitfields: spare bits (2 bits), number of gpr saved (6 bits). */
25226 fprintf (file, "%d,", (32 - first_reg_to_save ()));
25228 if (optional_tbtab)
25230 /* Compute the parameter info from the function decl argument
25231 list. */
25232 tree decl;
25233 int next_parm_info_bit = 31;
25235 for (decl = DECL_ARGUMENTS (current_function_decl);
25236 decl; decl = DECL_CHAIN (decl))
25238 rtx parameter = DECL_INCOMING_RTL (decl);
25239 enum machine_mode mode = GET_MODE (parameter);
25241 if (GET_CODE (parameter) == REG)
25243 if (SCALAR_FLOAT_MODE_P (mode))
25245 int bits;
25247 float_parms++;
25249 switch (mode)
25251 case SFmode:
25252 case SDmode:
25253 bits = 0x2;
25254 break;
25256 case DFmode:
25257 case DDmode:
25258 case TFmode:
25259 case TDmode:
25260 bits = 0x3;
25261 break;
25263 default:
25264 gcc_unreachable ();
25267 /* If only one bit will fit, don't or in this entry. */
25268 if (next_parm_info_bit > 0)
25269 parm_info |= (bits << (next_parm_info_bit - 1));
25270 next_parm_info_bit -= 2;
25272 else
25274 fixed_parms += ((GET_MODE_SIZE (mode)
25275 + (UNITS_PER_WORD - 1))
25276 / UNITS_PER_WORD);
25277 next_parm_info_bit -= 1;
25283 /* Number of fixed point parameters. */
25284 /* This is actually the number of words of fixed point parameters; thus
25285 an 8 byte struct counts as 2; and thus the maximum value is 8. */
25286 fprintf (file, "%d,", fixed_parms);
25288 /* 2 bitfields: number of floating point parameters (7 bits), parameters
25289 all on stack. */
25290 /* This is actually the number of fp registers that hold parameters;
25291 and thus the maximum value is 13. */
25292 /* Set parameters on stack bit if parameters are not in their original
25293 registers, regardless of whether they are on the stack? Xlc
25294 seems to set the bit when not optimizing. */
25295 fprintf (file, "%d\n", ((float_parms << 1) | (! optimize)));
25297 if (! optional_tbtab)
25298 return;
25300 /* Optional fields follow. Some are variable length. */
25302 /* Parameter types, left adjusted bit fields: 0 fixed, 10 single float,
25303 11 double float. */
25304 /* There is an entry for each parameter in a register, in the order that
25305 they occur in the parameter list. Any intervening arguments on the
25306 stack are ignored. If the list overflows a long (max possible length
25307 34 bits) then completely leave off all elements that don't fit. */
25308 /* Only emit this long if there was at least one parameter. */
25309 if (fixed_parms || float_parms)
25310 fprintf (file, "\t.long %d\n", parm_info);
25312 /* Offset from start of code to tb table. */
25313 fputs ("\t.long ", file);
25314 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
25315 RS6000_OUTPUT_BASENAME (file, fname);
25316 putc ('-', file);
25317 rs6000_output_function_entry (file, fname);
25318 putc ('\n', file);
25320 /* Interrupt handler mask. */
25321 /* Omit this long, since we never set the interrupt handler bit
25322 above. */
25324 /* Number of CTL (controlled storage) anchors. */
25325 /* Omit this long, since the has_ctl bit is never set above. */
25327 /* Displacement into stack of each CTL anchor. */
25328 /* Omit this list of longs, because there are no CTL anchors. */
25330 /* Length of function name. */
25331 if (*fname == '*')
25332 ++fname;
25333 fprintf (file, "\t.short %d\n", (int) strlen (fname));
25335 /* Function name. */
25336 assemble_string (fname, strlen (fname));
25338 /* Register for alloca automatic storage; this is always reg 31.
25339 Only emit this if the alloca bit was set above. */
25340 if (frame_pointer_needed)
25341 fputs ("\t.byte 31\n", file);
25343 fputs ("\t.align 2\n", file);
25347 /* A C compound statement that outputs the assembler code for a thunk
25348 function, used to implement C++ virtual function calls with
25349 multiple inheritance. The thunk acts as a wrapper around a virtual
25350 function, adjusting the implicit object parameter before handing
25351 control off to the real function.
25353 First, emit code to add the integer DELTA to the location that
25354 contains the incoming first argument. Assume that this argument
25355 contains a pointer, and is the one used to pass the `this' pointer
25356 in C++. This is the incoming argument *before* the function
25357 prologue, e.g. `%o0' on a sparc. The addition must preserve the
25358 values of all other incoming arguments.
25360 After the addition, emit code to jump to FUNCTION, which is a
25361 `FUNCTION_DECL'. This is a direct pure jump, not a call, and does
25362 not touch the return address. Hence returning from FUNCTION will
25363 return to whoever called the current `thunk'.
25365 The effect must be as if FUNCTION had been called directly with the
25366 adjusted first argument. This macro is responsible for emitting
25367 all of the code for a thunk function; output_function_prologue()
25368 and output_function_epilogue() are not invoked.
25370 The THUNK_FNDECL is redundant. (DELTA and FUNCTION have already
25371 been extracted from it.) It might possibly be useful on some
25372 targets, but probably not.
25374 If you do not define this macro, the target-independent code in the
25375 C++ frontend will generate a less efficient heavyweight thunk that
25376 calls FUNCTION instead of jumping to it. The generic approach does
25377 not support varargs. */
25379 static void
25380 rs6000_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
25381 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
25382 tree function)
25384 rtx this_rtx, funexp;
25385 rtx_insn *insn;
25387 reload_completed = 1;
25388 epilogue_completed = 1;
25390 /* Mark the end of the (empty) prologue. */
25391 emit_note (NOTE_INSN_PROLOGUE_END);
25393 /* Find the "this" pointer. If the function returns a structure,
25394 the structure return pointer is in r3. */
25395 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
25396 this_rtx = gen_rtx_REG (Pmode, 4);
25397 else
25398 this_rtx = gen_rtx_REG (Pmode, 3);
25400 /* Apply the constant offset, if required. */
25401 if (delta)
25402 emit_insn (gen_add3_insn (this_rtx, this_rtx, GEN_INT (delta)));
25404 /* Apply the offset from the vtable, if required. */
25405 if (vcall_offset)
25407 rtx vcall_offset_rtx = GEN_INT (vcall_offset);
25408 rtx tmp = gen_rtx_REG (Pmode, 12);
25410 emit_move_insn (tmp, gen_rtx_MEM (Pmode, this_rtx));
25411 if (((unsigned HOST_WIDE_INT) vcall_offset) + 0x8000 >= 0x10000)
25413 emit_insn (gen_add3_insn (tmp, tmp, vcall_offset_rtx));
25414 emit_move_insn (tmp, gen_rtx_MEM (Pmode, tmp));
25416 else
25418 rtx loc = gen_rtx_PLUS (Pmode, tmp, vcall_offset_rtx);
25420 emit_move_insn (tmp, gen_rtx_MEM (Pmode, loc));
25422 emit_insn (gen_add3_insn (this_rtx, this_rtx, tmp));
25425 /* Generate a tail call to the target function. */
25426 if (!TREE_USED (function))
25428 assemble_external (function);
25429 TREE_USED (function) = 1;
25431 funexp = XEXP (DECL_RTL (function), 0);
25432 funexp = gen_rtx_MEM (FUNCTION_MODE, funexp);
25434 #if TARGET_MACHO
25435 if (MACHOPIC_INDIRECT)
25436 funexp = machopic_indirect_call_target (funexp);
25437 #endif
25439 /* gen_sibcall expects reload to convert scratch pseudo to LR so we must
25440 generate sibcall RTL explicitly. */
25441 insn = emit_call_insn (
25442 gen_rtx_PARALLEL (VOIDmode,
25443 gen_rtvec (4,
25444 gen_rtx_CALL (VOIDmode,
25445 funexp, const0_rtx),
25446 gen_rtx_USE (VOIDmode, const0_rtx),
25447 gen_rtx_USE (VOIDmode,
25448 gen_rtx_REG (SImode,
25449 LR_REGNO)),
25450 simple_return_rtx)));
25451 SIBLING_CALL_P (insn) = 1;
25452 emit_barrier ();
25454 /* Ensure we have a global entry point for the thunk. ??? We could
25455 avoid that if the target routine doesn't need a global entry point,
25456 but we do not know whether this is the case at this point. */
25457 if (DEFAULT_ABI == ABI_ELFv2)
25458 cfun->machine->r2_setup_needed = true;
25460 /* Run just enough of rest_of_compilation to get the insns emitted.
25461 There's not really enough bulk here to make other passes such as
25462 instruction scheduling worth while. Note that use_thunk calls
25463 assemble_start_function and assemble_end_function. */
25464 insn = get_insns ();
25465 shorten_branches (insn);
25466 final_start_function (insn, file, 1);
25467 final (insn, file, 1);
25468 final_end_function ();
25470 reload_completed = 0;
25471 epilogue_completed = 0;
25474 /* A quick summary of the various types of 'constant-pool tables'
25475 under PowerPC:
25477 Target Flags Name One table per
25478 AIX (none) AIX TOC object file
25479 AIX -mfull-toc AIX TOC object file
25480 AIX -mminimal-toc AIX minimal TOC translation unit
25481 SVR4/EABI (none) SVR4 SDATA object file
25482 SVR4/EABI -fpic SVR4 pic object file
25483 SVR4/EABI -fPIC SVR4 PIC translation unit
25484 SVR4/EABI -mrelocatable EABI TOC function
25485 SVR4/EABI -maix AIX TOC object file
25486 SVR4/EABI -maix -mminimal-toc
25487 AIX minimal TOC translation unit
25489 Name Reg. Set by entries contains:
25490 made by addrs? fp? sum?
25492 AIX TOC 2 crt0 as Y option option
25493 AIX minimal TOC 30 prolog gcc Y Y option
25494 SVR4 SDATA 13 crt0 gcc N Y N
25495 SVR4 pic 30 prolog ld Y not yet N
25496 SVR4 PIC 30 prolog gcc Y option option
25497 EABI TOC 30 prolog gcc Y option option
25501 /* Hash functions for the hash table. */
25503 static unsigned
25504 rs6000_hash_constant (rtx k)
25506 enum rtx_code code = GET_CODE (k);
25507 enum machine_mode mode = GET_MODE (k);
25508 unsigned result = (code << 3) ^ mode;
25509 const char *format;
25510 int flen, fidx;
25512 format = GET_RTX_FORMAT (code);
25513 flen = strlen (format);
25514 fidx = 0;
25516 switch (code)
25518 case LABEL_REF:
25519 return result * 1231 + (unsigned) INSN_UID (XEXP (k, 0));
25521 case CONST_WIDE_INT:
25523 int i;
25524 flen = CONST_WIDE_INT_NUNITS (k);
25525 for (i = 0; i < flen; i++)
25526 result = result * 613 + CONST_WIDE_INT_ELT (k, i);
25527 return result;
25530 case CONST_DOUBLE:
25531 if (mode != VOIDmode)
25532 return real_hash (CONST_DOUBLE_REAL_VALUE (k)) * result;
25533 flen = 2;
25534 break;
25536 case CODE_LABEL:
25537 fidx = 3;
25538 break;
25540 default:
25541 break;
25544 for (; fidx < flen; fidx++)
25545 switch (format[fidx])
25547 case 's':
25549 unsigned i, len;
25550 const char *str = XSTR (k, fidx);
25551 len = strlen (str);
25552 result = result * 613 + len;
25553 for (i = 0; i < len; i++)
25554 result = result * 613 + (unsigned) str[i];
25555 break;
25557 case 'u':
25558 case 'e':
25559 result = result * 1231 + rs6000_hash_constant (XEXP (k, fidx));
25560 break;
25561 case 'i':
25562 case 'n':
25563 result = result * 613 + (unsigned) XINT (k, fidx);
25564 break;
25565 case 'w':
25566 if (sizeof (unsigned) >= sizeof (HOST_WIDE_INT))
25567 result = result * 613 + (unsigned) XWINT (k, fidx);
25568 else
25570 size_t i;
25571 for (i = 0; i < sizeof (HOST_WIDE_INT) / sizeof (unsigned); i++)
25572 result = result * 613 + (unsigned) (XWINT (k, fidx)
25573 >> CHAR_BIT * i);
25575 break;
25576 case '0':
25577 break;
25578 default:
25579 gcc_unreachable ();
25582 return result;
25585 hashval_t
25586 toc_hasher::hash (toc_hash_struct *thc)
25588 return rs6000_hash_constant (thc->key) ^ thc->key_mode;
25591 /* Compare H1 and H2 for equivalence. */
25593 bool
25594 toc_hasher::equal (toc_hash_struct *h1, toc_hash_struct *h2)
25596 rtx r1 = h1->key;
25597 rtx r2 = h2->key;
25599 if (h1->key_mode != h2->key_mode)
25600 return 0;
25602 return rtx_equal_p (r1, r2);
25605 /* These are the names given by the C++ front-end to vtables, and
25606 vtable-like objects. Ideally, this logic should not be here;
25607 instead, there should be some programmatic way of inquiring as
25608 to whether or not an object is a vtable. */
25610 #define VTABLE_NAME_P(NAME) \
25611 (strncmp ("_vt.", name, strlen ("_vt.")) == 0 \
25612 || strncmp ("_ZTV", name, strlen ("_ZTV")) == 0 \
25613 || strncmp ("_ZTT", name, strlen ("_ZTT")) == 0 \
25614 || strncmp ("_ZTI", name, strlen ("_ZTI")) == 0 \
25615 || strncmp ("_ZTC", name, strlen ("_ZTC")) == 0)
25617 #ifdef NO_DOLLAR_IN_LABEL
25618 /* Return a GGC-allocated character string translating dollar signs in
25619 input NAME to underscores. Used by XCOFF ASM_OUTPUT_LABELREF. */
25621 const char *
25622 rs6000_xcoff_strip_dollar (const char *name)
25624 char *strip, *p;
25625 const char *q;
25626 size_t len;
25628 q = (const char *) strchr (name, '$');
25630 if (q == 0 || q == name)
25631 return name;
25633 len = strlen (name);
25634 strip = XALLOCAVEC (char, len + 1);
25635 strcpy (strip, name);
25636 p = strip + (q - name);
25637 while (p)
25639 *p = '_';
25640 p = strchr (p + 1, '$');
25643 return ggc_alloc_string (strip, len);
25645 #endif
25647 void
25648 rs6000_output_symbol_ref (FILE *file, rtx x)
25650 /* Currently C++ toc references to vtables can be emitted before it
25651 is decided whether the vtable is public or private. If this is
25652 the case, then the linker will eventually complain that there is
25653 a reference to an unknown section. Thus, for vtables only,
25654 we emit the TOC reference to reference the symbol and not the
25655 section. */
25656 const char *name = XSTR (x, 0);
25658 if (VTABLE_NAME_P (name))
25660 RS6000_OUTPUT_BASENAME (file, name);
25662 else
25663 assemble_name (file, name);
25666 /* Output a TOC entry. We derive the entry name from what is being
25667 written. */
25669 void
25670 output_toc (FILE *file, rtx x, int labelno, enum machine_mode mode)
25672 char buf[256];
25673 const char *name = buf;
25674 rtx base = x;
25675 HOST_WIDE_INT offset = 0;
25677 gcc_assert (!TARGET_NO_TOC);
25679 /* When the linker won't eliminate them, don't output duplicate
25680 TOC entries (this happens on AIX if there is any kind of TOC,
25681 and on SVR4 under -fPIC or -mrelocatable). Don't do this for
25682 CODE_LABELs. */
25683 if (TARGET_TOC && GET_CODE (x) != LABEL_REF)
25685 struct toc_hash_struct *h;
25687 /* Create toc_hash_table. This can't be done at TARGET_OPTION_OVERRIDE
25688 time because GGC is not initialized at that point. */
25689 if (toc_hash_table == NULL)
25690 toc_hash_table = hash_table<toc_hasher>::create_ggc (1021);
25692 h = ggc_alloc<toc_hash_struct> ();
25693 h->key = x;
25694 h->key_mode = mode;
25695 h->labelno = labelno;
25697 toc_hash_struct **found = toc_hash_table->find_slot (h, INSERT);
25698 if (*found == NULL)
25699 *found = h;
25700 else /* This is indeed a duplicate.
25701 Set this label equal to that label. */
25703 fputs ("\t.set ", file);
25704 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
25705 fprintf (file, "%d,", labelno);
25706 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
25707 fprintf (file, "%d\n", ((*found)->labelno));
25709 #ifdef HAVE_AS_TLS
25710 if (TARGET_XCOFF && GET_CODE (x) == SYMBOL_REF
25711 && (SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_GLOBAL_DYNAMIC
25712 || SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC))
25714 fputs ("\t.set ", file);
25715 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LCM");
25716 fprintf (file, "%d,", labelno);
25717 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LCM");
25718 fprintf (file, "%d\n", ((*found)->labelno));
25720 #endif
25721 return;
25725 /* If we're going to put a double constant in the TOC, make sure it's
25726 aligned properly when strict alignment is on. */
25727 if ((CONST_DOUBLE_P (x) || CONST_WIDE_INT_P (x))
25728 && STRICT_ALIGNMENT
25729 && GET_MODE_BITSIZE (mode) >= 64
25730 && ! (TARGET_NO_FP_IN_TOC && ! TARGET_MINIMAL_TOC)) {
25731 ASM_OUTPUT_ALIGN (file, 3);
25734 (*targetm.asm_out.internal_label) (file, "LC", labelno);
25736 /* Handle FP constants specially. Note that if we have a minimal
25737 TOC, things we put here aren't actually in the TOC, so we can allow
25738 FP constants. */
25739 if (GET_CODE (x) == CONST_DOUBLE &&
25740 (GET_MODE (x) == TFmode || GET_MODE (x) == TDmode))
25742 REAL_VALUE_TYPE rv;
25743 long k[4];
25745 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
25746 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
25747 REAL_VALUE_TO_TARGET_DECIMAL128 (rv, k);
25748 else
25749 REAL_VALUE_TO_TARGET_LONG_DOUBLE (rv, k);
25751 if (TARGET_64BIT)
25753 if (TARGET_ELF || TARGET_MINIMAL_TOC)
25754 fputs (DOUBLE_INT_ASM_OP, file);
25755 else
25756 fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
25757 k[0] & 0xffffffff, k[1] & 0xffffffff,
25758 k[2] & 0xffffffff, k[3] & 0xffffffff);
25759 fprintf (file, "0x%lx%08lx,0x%lx%08lx\n",
25760 k[WORDS_BIG_ENDIAN ? 0 : 1] & 0xffffffff,
25761 k[WORDS_BIG_ENDIAN ? 1 : 0] & 0xffffffff,
25762 k[WORDS_BIG_ENDIAN ? 2 : 3] & 0xffffffff,
25763 k[WORDS_BIG_ENDIAN ? 3 : 2] & 0xffffffff);
25764 return;
25766 else
25768 if (TARGET_ELF || TARGET_MINIMAL_TOC)
25769 fputs ("\t.long ", file);
25770 else
25771 fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
25772 k[0] & 0xffffffff, k[1] & 0xffffffff,
25773 k[2] & 0xffffffff, k[3] & 0xffffffff);
25774 fprintf (file, "0x%lx,0x%lx,0x%lx,0x%lx\n",
25775 k[0] & 0xffffffff, k[1] & 0xffffffff,
25776 k[2] & 0xffffffff, k[3] & 0xffffffff);
25777 return;
25780 else if (GET_CODE (x) == CONST_DOUBLE &&
25781 (GET_MODE (x) == DFmode || GET_MODE (x) == DDmode))
25783 REAL_VALUE_TYPE rv;
25784 long k[2];
25786 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
25788 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
25789 REAL_VALUE_TO_TARGET_DECIMAL64 (rv, k);
25790 else
25791 REAL_VALUE_TO_TARGET_DOUBLE (rv, k);
25793 if (TARGET_64BIT)
25795 if (TARGET_ELF || TARGET_MINIMAL_TOC)
25796 fputs (DOUBLE_INT_ASM_OP, file);
25797 else
25798 fprintf (file, "\t.tc FD_%lx_%lx[TC],",
25799 k[0] & 0xffffffff, k[1] & 0xffffffff);
25800 fprintf (file, "0x%lx%08lx\n",
25801 k[WORDS_BIG_ENDIAN ? 0 : 1] & 0xffffffff,
25802 k[WORDS_BIG_ENDIAN ? 1 : 0] & 0xffffffff);
25803 return;
25805 else
25807 if (TARGET_ELF || TARGET_MINIMAL_TOC)
25808 fputs ("\t.long ", file);
25809 else
25810 fprintf (file, "\t.tc FD_%lx_%lx[TC],",
25811 k[0] & 0xffffffff, k[1] & 0xffffffff);
25812 fprintf (file, "0x%lx,0x%lx\n",
25813 k[0] & 0xffffffff, k[1] & 0xffffffff);
25814 return;
25817 else if (GET_CODE (x) == CONST_DOUBLE &&
25818 (GET_MODE (x) == SFmode || GET_MODE (x) == SDmode))
25820 REAL_VALUE_TYPE rv;
25821 long l;
25823 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
25824 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
25825 REAL_VALUE_TO_TARGET_DECIMAL32 (rv, l);
25826 else
25827 REAL_VALUE_TO_TARGET_SINGLE (rv, l);
25829 if (TARGET_64BIT)
25831 if (TARGET_ELF || TARGET_MINIMAL_TOC)
25832 fputs (DOUBLE_INT_ASM_OP, file);
25833 else
25834 fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
25835 if (WORDS_BIG_ENDIAN)
25836 fprintf (file, "0x%lx00000000\n", l & 0xffffffff);
25837 else
25838 fprintf (file, "0x%lx\n", l & 0xffffffff);
25839 return;
25841 else
25843 if (TARGET_ELF || TARGET_MINIMAL_TOC)
25844 fputs ("\t.long ", file);
25845 else
25846 fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
25847 fprintf (file, "0x%lx\n", l & 0xffffffff);
25848 return;
25851 else if (GET_MODE (x) == VOIDmode && GET_CODE (x) == CONST_INT)
25853 unsigned HOST_WIDE_INT low;
25854 HOST_WIDE_INT high;
25856 low = INTVAL (x) & 0xffffffff;
25857 high = (HOST_WIDE_INT) INTVAL (x) >> 32;
25859 /* TOC entries are always Pmode-sized, so when big-endian
25860 smaller integer constants in the TOC need to be padded.
25861 (This is still a win over putting the constants in
25862 a separate constant pool, because then we'd have
25863 to have both a TOC entry _and_ the actual constant.)
25865 For a 32-bit target, CONST_INT values are loaded and shifted
25866 entirely within `low' and can be stored in one TOC entry. */
25868 /* It would be easy to make this work, but it doesn't now. */
25869 gcc_assert (!TARGET_64BIT || POINTER_SIZE >= GET_MODE_BITSIZE (mode));
25871 if (WORDS_BIG_ENDIAN && POINTER_SIZE > GET_MODE_BITSIZE (mode))
25873 low |= high << 32;
25874 low <<= POINTER_SIZE - GET_MODE_BITSIZE (mode);
25875 high = (HOST_WIDE_INT) low >> 32;
25876 low &= 0xffffffff;
25879 if (TARGET_64BIT)
25881 if (TARGET_ELF || TARGET_MINIMAL_TOC)
25882 fputs (DOUBLE_INT_ASM_OP, file);
25883 else
25884 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
25885 (long) high & 0xffffffff, (long) low & 0xffffffff);
25886 fprintf (file, "0x%lx%08lx\n",
25887 (long) high & 0xffffffff, (long) low & 0xffffffff);
25888 return;
25890 else
25892 if (POINTER_SIZE < GET_MODE_BITSIZE (mode))
25894 if (TARGET_ELF || TARGET_MINIMAL_TOC)
25895 fputs ("\t.long ", file);
25896 else
25897 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
25898 (long) high & 0xffffffff, (long) low & 0xffffffff);
25899 fprintf (file, "0x%lx,0x%lx\n",
25900 (long) high & 0xffffffff, (long) low & 0xffffffff);
25902 else
25904 if (TARGET_ELF || TARGET_MINIMAL_TOC)
25905 fputs ("\t.long ", file);
25906 else
25907 fprintf (file, "\t.tc IS_%lx[TC],", (long) low & 0xffffffff);
25908 fprintf (file, "0x%lx\n", (long) low & 0xffffffff);
25910 return;
25914 if (GET_CODE (x) == CONST)
25916 gcc_assert (GET_CODE (XEXP (x, 0)) == PLUS
25917 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT);
25919 base = XEXP (XEXP (x, 0), 0);
25920 offset = INTVAL (XEXP (XEXP (x, 0), 1));
25923 switch (GET_CODE (base))
25925 case SYMBOL_REF:
25926 name = XSTR (base, 0);
25927 break;
25929 case LABEL_REF:
25930 ASM_GENERATE_INTERNAL_LABEL (buf, "L",
25931 CODE_LABEL_NUMBER (XEXP (base, 0)));
25932 break;
25934 case CODE_LABEL:
25935 ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (base));
25936 break;
25938 default:
25939 gcc_unreachable ();
25942 if (TARGET_ELF || TARGET_MINIMAL_TOC)
25943 fputs (TARGET_32BIT ? "\t.long " : DOUBLE_INT_ASM_OP, file);
25944 else
25946 fputs ("\t.tc ", file);
25947 RS6000_OUTPUT_BASENAME (file, name);
25949 if (offset < 0)
25950 fprintf (file, ".N" HOST_WIDE_INT_PRINT_UNSIGNED, - offset);
25951 else if (offset)
25952 fprintf (file, ".P" HOST_WIDE_INT_PRINT_UNSIGNED, offset);
25954 /* Mark large TOC symbols on AIX with [TE] so they are mapped
25955 after other TOC symbols, reducing overflow of small TOC access
25956 to [TC] symbols. */
25957 fputs (TARGET_XCOFF && TARGET_CMODEL != CMODEL_SMALL
25958 ? "[TE]," : "[TC],", file);
25961 /* Currently C++ toc references to vtables can be emitted before it
25962 is decided whether the vtable is public or private. If this is
25963 the case, then the linker will eventually complain that there is
25964 a TOC reference to an unknown section. Thus, for vtables only,
25965 we emit the TOC reference to reference the symbol and not the
25966 section. */
25967 if (VTABLE_NAME_P (name))
25969 RS6000_OUTPUT_BASENAME (file, name);
25970 if (offset < 0)
25971 fprintf (file, HOST_WIDE_INT_PRINT_DEC, offset);
25972 else if (offset > 0)
25973 fprintf (file, "+" HOST_WIDE_INT_PRINT_DEC, offset);
25975 else
25976 output_addr_const (file, x);
25978 #if HAVE_AS_TLS
25979 if (TARGET_XCOFF && GET_CODE (base) == SYMBOL_REF
25980 && SYMBOL_REF_TLS_MODEL (base) != 0)
25982 if (SYMBOL_REF_TLS_MODEL (base) == TLS_MODEL_LOCAL_EXEC)
25983 fputs ("@le", file);
25984 else if (SYMBOL_REF_TLS_MODEL (base) == TLS_MODEL_INITIAL_EXEC)
25985 fputs ("@ie", file);
25986 /* Use global-dynamic for local-dynamic. */
25987 else if (SYMBOL_REF_TLS_MODEL (base) == TLS_MODEL_GLOBAL_DYNAMIC
25988 || SYMBOL_REF_TLS_MODEL (base) == TLS_MODEL_LOCAL_DYNAMIC)
25990 putc ('\n', file);
25991 (*targetm.asm_out.internal_label) (file, "LCM", labelno);
25992 fputs ("\t.tc .", file);
25993 RS6000_OUTPUT_BASENAME (file, name);
25994 fputs ("[TC],", file);
25995 output_addr_const (file, x);
25996 fputs ("@m", file);
25999 #endif
26001 putc ('\n', file);
26004 /* Output an assembler pseudo-op to write an ASCII string of N characters
26005 starting at P to FILE.
26007 On the RS/6000, we have to do this using the .byte operation and
26008 write out special characters outside the quoted string.
26009 Also, the assembler is broken; very long strings are truncated,
26010 so we must artificially break them up early. */
26012 void
26013 output_ascii (FILE *file, const char *p, int n)
26015 char c;
26016 int i, count_string;
26017 const char *for_string = "\t.byte \"";
26018 const char *for_decimal = "\t.byte ";
26019 const char *to_close = NULL;
26021 count_string = 0;
26022 for (i = 0; i < n; i++)
26024 c = *p++;
26025 if (c >= ' ' && c < 0177)
26027 if (for_string)
26028 fputs (for_string, file);
26029 putc (c, file);
26031 /* Write two quotes to get one. */
26032 if (c == '"')
26034 putc (c, file);
26035 ++count_string;
26038 for_string = NULL;
26039 for_decimal = "\"\n\t.byte ";
26040 to_close = "\"\n";
26041 ++count_string;
26043 if (count_string >= 512)
26045 fputs (to_close, file);
26047 for_string = "\t.byte \"";
26048 for_decimal = "\t.byte ";
26049 to_close = NULL;
26050 count_string = 0;
26053 else
26055 if (for_decimal)
26056 fputs (for_decimal, file);
26057 fprintf (file, "%d", c);
26059 for_string = "\n\t.byte \"";
26060 for_decimal = ", ";
26061 to_close = "\n";
26062 count_string = 0;
26066 /* Now close the string if we have written one. Then end the line. */
26067 if (to_close)
26068 fputs (to_close, file);
26071 /* Generate a unique section name for FILENAME for a section type
26072 represented by SECTION_DESC. Output goes into BUF.
26074 SECTION_DESC can be any string, as long as it is different for each
26075 possible section type.
26077 We name the section in the same manner as xlc. The name begins with an
26078 underscore followed by the filename (after stripping any leading directory
26079 names) with the last period replaced by the string SECTION_DESC. If
26080 FILENAME does not contain a period, SECTION_DESC is appended to the end of
26081 the name. */
26083 void
26084 rs6000_gen_section_name (char **buf, const char *filename,
26085 const char *section_desc)
26087 const char *q, *after_last_slash, *last_period = 0;
26088 char *p;
26089 int len;
26091 after_last_slash = filename;
26092 for (q = filename; *q; q++)
26094 if (*q == '/')
26095 after_last_slash = q + 1;
26096 else if (*q == '.')
26097 last_period = q;
26100 len = strlen (after_last_slash) + strlen (section_desc) + 2;
26101 *buf = (char *) xmalloc (len);
26103 p = *buf;
26104 *p++ = '_';
26106 for (q = after_last_slash; *q; q++)
26108 if (q == last_period)
26110 strcpy (p, section_desc);
26111 p += strlen (section_desc);
26112 break;
26115 else if (ISALNUM (*q))
26116 *p++ = *q;
26119 if (last_period == 0)
26120 strcpy (p, section_desc);
26121 else
26122 *p = '\0';
26125 /* Emit profile function. */
26127 void
26128 output_profile_hook (int labelno ATTRIBUTE_UNUSED)
26130 /* Non-standard profiling for kernels, which just saves LR then calls
26131 _mcount without worrying about arg saves. The idea is to change
26132 the function prologue as little as possible as it isn't easy to
26133 account for arg save/restore code added just for _mcount. */
26134 if (TARGET_PROFILE_KERNEL)
26135 return;
26137 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
26139 #ifndef NO_PROFILE_COUNTERS
26140 # define NO_PROFILE_COUNTERS 0
26141 #endif
26142 if (NO_PROFILE_COUNTERS)
26143 emit_library_call (init_one_libfunc (RS6000_MCOUNT),
26144 LCT_NORMAL, VOIDmode, 0);
26145 else
26147 char buf[30];
26148 const char *label_name;
26149 rtx fun;
26151 ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
26152 label_name = ggc_strdup ((*targetm.strip_name_encoding) (buf));
26153 fun = gen_rtx_SYMBOL_REF (Pmode, label_name);
26155 emit_library_call (init_one_libfunc (RS6000_MCOUNT),
26156 LCT_NORMAL, VOIDmode, 1, fun, Pmode);
26159 else if (DEFAULT_ABI == ABI_DARWIN)
26161 const char *mcount_name = RS6000_MCOUNT;
26162 int caller_addr_regno = LR_REGNO;
26164 /* Be conservative and always set this, at least for now. */
26165 crtl->uses_pic_offset_table = 1;
26167 #if TARGET_MACHO
26168 /* For PIC code, set up a stub and collect the caller's address
26169 from r0, which is where the prologue puts it. */
26170 if (MACHOPIC_INDIRECT
26171 && crtl->uses_pic_offset_table)
26172 caller_addr_regno = 0;
26173 #endif
26174 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, mcount_name),
26175 LCT_NORMAL, VOIDmode, 1,
26176 gen_rtx_REG (Pmode, caller_addr_regno), Pmode);
26180 /* Write function profiler code. */
26182 void
26183 output_function_profiler (FILE *file, int labelno)
26185 char buf[100];
26187 switch (DEFAULT_ABI)
26189 default:
26190 gcc_unreachable ();
26192 case ABI_V4:
26193 if (!TARGET_32BIT)
26195 warning (0, "no profiling of 64-bit code for this ABI");
26196 return;
26198 ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
26199 fprintf (file, "\tmflr %s\n", reg_names[0]);
26200 if (NO_PROFILE_COUNTERS)
26202 asm_fprintf (file, "\tstw %s,4(%s)\n",
26203 reg_names[0], reg_names[1]);
26205 else if (TARGET_SECURE_PLT && flag_pic)
26207 if (TARGET_LINK_STACK)
26209 char name[32];
26210 get_ppc476_thunk_name (name);
26211 asm_fprintf (file, "\tbl %s\n", name);
26213 else
26214 asm_fprintf (file, "\tbcl 20,31,1f\n1:\n");
26215 asm_fprintf (file, "\tstw %s,4(%s)\n",
26216 reg_names[0], reg_names[1]);
26217 asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
26218 asm_fprintf (file, "\taddis %s,%s,",
26219 reg_names[12], reg_names[12]);
26220 assemble_name (file, buf);
26221 asm_fprintf (file, "-1b@ha\n\tla %s,", reg_names[0]);
26222 assemble_name (file, buf);
26223 asm_fprintf (file, "-1b@l(%s)\n", reg_names[12]);
26225 else if (flag_pic == 1)
26227 fputs ("\tbl _GLOBAL_OFFSET_TABLE_@local-4\n", file);
26228 asm_fprintf (file, "\tstw %s,4(%s)\n",
26229 reg_names[0], reg_names[1]);
26230 asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
26231 asm_fprintf (file, "\tlwz %s,", reg_names[0]);
26232 assemble_name (file, buf);
26233 asm_fprintf (file, "@got(%s)\n", reg_names[12]);
26235 else if (flag_pic > 1)
26237 asm_fprintf (file, "\tstw %s,4(%s)\n",
26238 reg_names[0], reg_names[1]);
26239 /* Now, we need to get the address of the label. */
26240 if (TARGET_LINK_STACK)
26242 char name[32];
26243 get_ppc476_thunk_name (name);
26244 asm_fprintf (file, "\tbl %s\n\tb 1f\n\t.long ", name);
26245 assemble_name (file, buf);
26246 fputs ("-.\n1:", file);
26247 asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
26248 asm_fprintf (file, "\taddi %s,%s,4\n",
26249 reg_names[11], reg_names[11]);
26251 else
26253 fputs ("\tbcl 20,31,1f\n\t.long ", file);
26254 assemble_name (file, buf);
26255 fputs ("-.\n1:", file);
26256 asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
26258 asm_fprintf (file, "\tlwz %s,0(%s)\n",
26259 reg_names[0], reg_names[11]);
26260 asm_fprintf (file, "\tadd %s,%s,%s\n",
26261 reg_names[0], reg_names[0], reg_names[11]);
26263 else
26265 asm_fprintf (file, "\tlis %s,", reg_names[12]);
26266 assemble_name (file, buf);
26267 fputs ("@ha\n", file);
26268 asm_fprintf (file, "\tstw %s,4(%s)\n",
26269 reg_names[0], reg_names[1]);
26270 asm_fprintf (file, "\tla %s,", reg_names[0]);
26271 assemble_name (file, buf);
26272 asm_fprintf (file, "@l(%s)\n", reg_names[12]);
26275 /* ABI_V4 saves the static chain reg with ASM_OUTPUT_REG_PUSH. */
26276 fprintf (file, "\tbl %s%s\n",
26277 RS6000_MCOUNT, flag_pic ? "@plt" : "");
26278 break;
26280 case ABI_AIX:
26281 case ABI_ELFv2:
26282 case ABI_DARWIN:
26283 /* Don't do anything, done in output_profile_hook (). */
26284 break;
26290 /* The following variable value is the last issued insn. */
26292 static rtx last_scheduled_insn;
26294 /* The following variable helps to balance issuing of load and
26295 store instructions */
26297 static int load_store_pendulum;
26299 /* Power4 load update and store update instructions are cracked into a
26300 load or store and an integer insn which are executed in the same cycle.
26301 Branches have their own dispatch slot which does not count against the
26302 GCC issue rate, but it changes the program flow so there are no other
26303 instructions to issue in this cycle. */
26305 static int
26306 rs6000_variable_issue_1 (rtx_insn *insn, int more)
26308 last_scheduled_insn = insn;
26309 if (GET_CODE (PATTERN (insn)) == USE
26310 || GET_CODE (PATTERN (insn)) == CLOBBER)
26312 cached_can_issue_more = more;
26313 return cached_can_issue_more;
26316 if (insn_terminates_group_p (insn, current_group))
26318 cached_can_issue_more = 0;
26319 return cached_can_issue_more;
26322 /* If no reservation, but reach here */
26323 if (recog_memoized (insn) < 0)
26324 return more;
26326 if (rs6000_sched_groups)
26328 if (is_microcoded_insn (insn))
26329 cached_can_issue_more = 0;
26330 else if (is_cracked_insn (insn))
26331 cached_can_issue_more = more > 2 ? more - 2 : 0;
26332 else
26333 cached_can_issue_more = more - 1;
26335 return cached_can_issue_more;
26338 if (rs6000_cpu_attr == CPU_CELL && is_nonpipeline_insn (insn))
26339 return 0;
26341 cached_can_issue_more = more - 1;
26342 return cached_can_issue_more;
26345 static int
26346 rs6000_variable_issue (FILE *stream, int verbose, rtx_insn *insn, int more)
26348 int r = rs6000_variable_issue_1 (insn, more);
26349 if (verbose)
26350 fprintf (stream, "// rs6000_variable_issue (more = %d) = %d\n", more, r);
26351 return r;
26354 /* Adjust the cost of a scheduling dependency. Return the new cost of
26355 a dependency LINK or INSN on DEP_INSN. COST is the current cost. */
26357 static int
26358 rs6000_adjust_cost (rtx_insn *insn, rtx link, rtx_insn *dep_insn, int cost)
26360 enum attr_type attr_type;
26362 if (! recog_memoized (insn))
26363 return 0;
26365 switch (REG_NOTE_KIND (link))
26367 case REG_DEP_TRUE:
26369 /* Data dependency; DEP_INSN writes a register that INSN reads
26370 some cycles later. */
26372 /* Separate a load from a narrower, dependent store. */
26373 if (rs6000_sched_groups
26374 && GET_CODE (PATTERN (insn)) == SET
26375 && GET_CODE (PATTERN (dep_insn)) == SET
26376 && GET_CODE (XEXP (PATTERN (insn), 1)) == MEM
26377 && GET_CODE (XEXP (PATTERN (dep_insn), 0)) == MEM
26378 && (GET_MODE_SIZE (GET_MODE (XEXP (PATTERN (insn), 1)))
26379 > GET_MODE_SIZE (GET_MODE (XEXP (PATTERN (dep_insn), 0)))))
26380 return cost + 14;
26382 attr_type = get_attr_type (insn);
26384 switch (attr_type)
26386 case TYPE_JMPREG:
26387 /* Tell the first scheduling pass about the latency between
26388 a mtctr and bctr (and mtlr and br/blr). The first
26389 scheduling pass will not know about this latency since
26390 the mtctr instruction, which has the latency associated
26391 to it, will be generated by reload. */
26392 return 4;
26393 case TYPE_BRANCH:
26394 /* Leave some extra cycles between a compare and its
26395 dependent branch, to inhibit expensive mispredicts. */
26396 if ((rs6000_cpu_attr == CPU_PPC603
26397 || rs6000_cpu_attr == CPU_PPC604
26398 || rs6000_cpu_attr == CPU_PPC604E
26399 || rs6000_cpu_attr == CPU_PPC620
26400 || rs6000_cpu_attr == CPU_PPC630
26401 || rs6000_cpu_attr == CPU_PPC750
26402 || rs6000_cpu_attr == CPU_PPC7400
26403 || rs6000_cpu_attr == CPU_PPC7450
26404 || rs6000_cpu_attr == CPU_PPCE5500
26405 || rs6000_cpu_attr == CPU_PPCE6500
26406 || rs6000_cpu_attr == CPU_POWER4
26407 || rs6000_cpu_attr == CPU_POWER5
26408 || rs6000_cpu_attr == CPU_POWER7
26409 || rs6000_cpu_attr == CPU_POWER8
26410 || rs6000_cpu_attr == CPU_CELL)
26411 && recog_memoized (dep_insn)
26412 && (INSN_CODE (dep_insn) >= 0))
26414 switch (get_attr_type (dep_insn))
26416 case TYPE_CMP:
26417 case TYPE_COMPARE:
26418 case TYPE_FPCOMPARE:
26419 case TYPE_CR_LOGICAL:
26420 case TYPE_DELAYED_CR:
26421 return cost + 2;
26422 case TYPE_EXTS:
26423 case TYPE_MUL:
26424 if (get_attr_dot (dep_insn) == DOT_YES)
26425 return cost + 2;
26426 else
26427 break;
26428 case TYPE_SHIFT:
26429 if (get_attr_dot (dep_insn) == DOT_YES
26430 && get_attr_var_shift (dep_insn) == VAR_SHIFT_NO)
26431 return cost + 2;
26432 else
26433 break;
26434 default:
26435 break;
26437 break;
26439 case TYPE_STORE:
26440 case TYPE_FPSTORE:
26441 if ((rs6000_cpu == PROCESSOR_POWER6)
26442 && recog_memoized (dep_insn)
26443 && (INSN_CODE (dep_insn) >= 0))
26446 if (GET_CODE (PATTERN (insn)) != SET)
26447 /* If this happens, we have to extend this to schedule
26448 optimally. Return default for now. */
26449 return cost;
26451 /* Adjust the cost for the case where the value written
26452 by a fixed point operation is used as the address
26453 gen value on a store. */
26454 switch (get_attr_type (dep_insn))
26456 case TYPE_LOAD:
26457 case TYPE_CNTLZ:
26459 if (! store_data_bypass_p (dep_insn, insn))
26460 return get_attr_sign_extend (dep_insn)
26461 == SIGN_EXTEND_YES ? 6 : 4;
26462 break;
26464 case TYPE_SHIFT:
26466 if (! store_data_bypass_p (dep_insn, insn))
26467 return get_attr_var_shift (dep_insn) == VAR_SHIFT_YES ?
26468 6 : 3;
26469 break;
26471 case TYPE_INTEGER:
26472 case TYPE_ADD:
26473 case TYPE_LOGICAL:
26474 case TYPE_COMPARE:
26475 case TYPE_EXTS:
26476 case TYPE_INSERT:
26478 if (! store_data_bypass_p (dep_insn, insn))
26479 return 3;
26480 break;
26482 case TYPE_STORE:
26483 case TYPE_FPLOAD:
26484 case TYPE_FPSTORE:
26486 if (get_attr_update (dep_insn) == UPDATE_YES
26487 && ! store_data_bypass_p (dep_insn, insn))
26488 return 3;
26489 break;
26491 case TYPE_MUL:
26493 if (! store_data_bypass_p (dep_insn, insn))
26494 return 17;
26495 break;
26497 case TYPE_DIV:
26499 if (! store_data_bypass_p (dep_insn, insn))
26500 return get_attr_size (dep_insn) == SIZE_32 ? 45 : 57;
26501 break;
26503 default:
26504 break;
26507 break;
26509 case TYPE_LOAD:
26510 if ((rs6000_cpu == PROCESSOR_POWER6)
26511 && recog_memoized (dep_insn)
26512 && (INSN_CODE (dep_insn) >= 0))
26515 /* Adjust the cost for the case where the value written
26516 by a fixed point instruction is used within the address
26517 gen portion of a subsequent load(u)(x) */
26518 switch (get_attr_type (dep_insn))
26520 case TYPE_LOAD:
26521 case TYPE_CNTLZ:
26523 if (set_to_load_agen (dep_insn, insn))
26524 return get_attr_sign_extend (dep_insn)
26525 == SIGN_EXTEND_YES ? 6 : 4;
26526 break;
26528 case TYPE_SHIFT:
26530 if (set_to_load_agen (dep_insn, insn))
26531 return get_attr_var_shift (dep_insn) == VAR_SHIFT_YES ?
26532 6 : 3;
26533 break;
26535 case TYPE_INTEGER:
26536 case TYPE_ADD:
26537 case TYPE_LOGICAL:
26538 case TYPE_COMPARE:
26539 case TYPE_EXTS:
26540 case TYPE_INSERT:
26542 if (set_to_load_agen (dep_insn, insn))
26543 return 3;
26544 break;
26546 case TYPE_STORE:
26547 case TYPE_FPLOAD:
26548 case TYPE_FPSTORE:
26550 if (get_attr_update (dep_insn) == UPDATE_YES
26551 && set_to_load_agen (dep_insn, insn))
26552 return 3;
26553 break;
26555 case TYPE_MUL:
26557 if (set_to_load_agen (dep_insn, insn))
26558 return 17;
26559 break;
26561 case TYPE_DIV:
26563 if (set_to_load_agen (dep_insn, insn))
26564 return get_attr_size (dep_insn) == SIZE_32 ? 45 : 57;
26565 break;
26567 default:
26568 break;
26571 break;
26573 case TYPE_FPLOAD:
26574 if ((rs6000_cpu == PROCESSOR_POWER6)
26575 && get_attr_update (insn) == UPDATE_NO
26576 && recog_memoized (dep_insn)
26577 && (INSN_CODE (dep_insn) >= 0)
26578 && (get_attr_type (dep_insn) == TYPE_MFFGPR))
26579 return 2;
26581 default:
26582 break;
26585 /* Fall out to return default cost. */
26587 break;
26589 case REG_DEP_OUTPUT:
26590 /* Output dependency; DEP_INSN writes a register that INSN writes some
26591 cycles later. */
26592 if ((rs6000_cpu == PROCESSOR_POWER6)
26593 && recog_memoized (dep_insn)
26594 && (INSN_CODE (dep_insn) >= 0))
26596 attr_type = get_attr_type (insn);
26598 switch (attr_type)
26600 case TYPE_FP:
26601 if (get_attr_type (dep_insn) == TYPE_FP)
26602 return 1;
26603 break;
26604 case TYPE_FPLOAD:
26605 if (get_attr_update (insn) == UPDATE_NO
26606 && get_attr_type (dep_insn) == TYPE_MFFGPR)
26607 return 2;
26608 break;
26609 default:
26610 break;
26613 case REG_DEP_ANTI:
26614 /* Anti dependency; DEP_INSN reads a register that INSN writes some
26615 cycles later. */
26616 return 0;
26618 default:
26619 gcc_unreachable ();
26622 return cost;
26625 /* Debug version of rs6000_adjust_cost. */
26627 static int
26628 rs6000_debug_adjust_cost (rtx_insn *insn, rtx link, rtx_insn *dep_insn,
26629 int cost)
26631 int ret = rs6000_adjust_cost (insn, link, dep_insn, cost);
26633 if (ret != cost)
26635 const char *dep;
26637 switch (REG_NOTE_KIND (link))
26639 default: dep = "unknown depencency"; break;
26640 case REG_DEP_TRUE: dep = "data dependency"; break;
26641 case REG_DEP_OUTPUT: dep = "output dependency"; break;
26642 case REG_DEP_ANTI: dep = "anti depencency"; break;
26645 fprintf (stderr,
26646 "\nrs6000_adjust_cost, final cost = %d, orig cost = %d, "
26647 "%s, insn:\n", ret, cost, dep);
26649 debug_rtx (insn);
26652 return ret;
26655 /* The function returns a true if INSN is microcoded.
26656 Return false otherwise. */
26658 static bool
26659 is_microcoded_insn (rtx_insn *insn)
26661 if (!insn || !NONDEBUG_INSN_P (insn)
26662 || GET_CODE (PATTERN (insn)) == USE
26663 || GET_CODE (PATTERN (insn)) == CLOBBER)
26664 return false;
26666 if (rs6000_cpu_attr == CPU_CELL)
26667 return get_attr_cell_micro (insn) == CELL_MICRO_ALWAYS;
26669 if (rs6000_sched_groups
26670 && (rs6000_cpu == PROCESSOR_POWER4 || rs6000_cpu == PROCESSOR_POWER5))
26672 enum attr_type type = get_attr_type (insn);
26673 if ((type == TYPE_LOAD
26674 && get_attr_update (insn) == UPDATE_YES
26675 && get_attr_sign_extend (insn) == SIGN_EXTEND_YES)
26676 || ((type == TYPE_LOAD || type == TYPE_STORE)
26677 && get_attr_update (insn) == UPDATE_YES
26678 && get_attr_indexed (insn) == INDEXED_YES)
26679 || type == TYPE_MFCR)
26680 return true;
26683 return false;
26686 /* The function returns true if INSN is cracked into 2 instructions
26687 by the processor (and therefore occupies 2 issue slots). */
26689 static bool
26690 is_cracked_insn (rtx_insn *insn)
26692 if (!insn || !NONDEBUG_INSN_P (insn)
26693 || GET_CODE (PATTERN (insn)) == USE
26694 || GET_CODE (PATTERN (insn)) == CLOBBER)
26695 return false;
26697 if (rs6000_sched_groups
26698 && (rs6000_cpu == PROCESSOR_POWER4 || rs6000_cpu == PROCESSOR_POWER5))
26700 enum attr_type type = get_attr_type (insn);
26701 if ((type == TYPE_LOAD
26702 && get_attr_sign_extend (insn) == SIGN_EXTEND_YES
26703 && get_attr_update (insn) == UPDATE_NO)
26704 || (type == TYPE_LOAD
26705 && get_attr_sign_extend (insn) == SIGN_EXTEND_NO
26706 && get_attr_update (insn) == UPDATE_YES
26707 && get_attr_indexed (insn) == INDEXED_NO)
26708 || (type == TYPE_STORE
26709 && get_attr_update (insn) == UPDATE_YES
26710 && get_attr_indexed (insn) == INDEXED_NO)
26711 || ((type == TYPE_FPLOAD || type == TYPE_FPSTORE)
26712 && get_attr_update (insn) == UPDATE_YES)
26713 || type == TYPE_DELAYED_CR
26714 || type == TYPE_COMPARE
26715 || (type == TYPE_EXTS
26716 && get_attr_dot (insn) == DOT_YES)
26717 || (type == TYPE_SHIFT
26718 && get_attr_dot (insn) == DOT_YES
26719 && get_attr_var_shift (insn) == VAR_SHIFT_NO)
26720 || (type == TYPE_MUL
26721 && get_attr_dot (insn) == DOT_YES)
26722 || type == TYPE_DIV
26723 || (type == TYPE_INSERT
26724 && get_attr_size (insn) == SIZE_32))
26725 return true;
26728 return false;
26731 /* The function returns true if INSN can be issued only from
26732 the branch slot. */
26734 static bool
26735 is_branch_slot_insn (rtx_insn *insn)
26737 if (!insn || !NONDEBUG_INSN_P (insn)
26738 || GET_CODE (PATTERN (insn)) == USE
26739 || GET_CODE (PATTERN (insn)) == CLOBBER)
26740 return false;
26742 if (rs6000_sched_groups)
26744 enum attr_type type = get_attr_type (insn);
26745 if (type == TYPE_BRANCH || type == TYPE_JMPREG)
26746 return true;
26747 return false;
26750 return false;
26753 /* The function returns true if out_inst sets a value that is
26754 used in the address generation computation of in_insn */
26755 static bool
26756 set_to_load_agen (rtx_insn *out_insn, rtx_insn *in_insn)
26758 rtx out_set, in_set;
26760 /* For performance reasons, only handle the simple case where
26761 both loads are a single_set. */
26762 out_set = single_set (out_insn);
26763 if (out_set)
26765 in_set = single_set (in_insn);
26766 if (in_set)
26767 return reg_mentioned_p (SET_DEST (out_set), SET_SRC (in_set));
26770 return false;
26773 /* Try to determine base/offset/size parts of the given MEM.
26774 Return true if successful, false if all the values couldn't
26775 be determined.
26777 This function only looks for REG or REG+CONST address forms.
26778 REG+REG address form will return false. */
26780 static bool
26781 get_memref_parts (rtx mem, rtx *base, HOST_WIDE_INT *offset,
26782 HOST_WIDE_INT *size)
26784 rtx addr_rtx;
26785 if MEM_SIZE_KNOWN_P (mem)
26786 *size = MEM_SIZE (mem);
26787 else
26788 return false;
26790 if (GET_CODE (XEXP (mem, 0)) == PRE_MODIFY)
26791 addr_rtx = XEXP (XEXP (mem, 0), 1);
26792 else
26793 addr_rtx = (XEXP (mem, 0));
26795 if (GET_CODE (addr_rtx) == REG)
26797 *base = addr_rtx;
26798 *offset = 0;
26800 else if (GET_CODE (addr_rtx) == PLUS
26801 && CONST_INT_P (XEXP (addr_rtx, 1)))
26803 *base = XEXP (addr_rtx, 0);
26804 *offset = INTVAL (XEXP (addr_rtx, 1));
26806 else
26807 return false;
26809 return true;
26812 /* The function returns true if the target storage location of
26813 mem1 is adjacent to the target storage location of mem2 */
26814 /* Return 1 if memory locations are adjacent. */
26816 static bool
26817 adjacent_mem_locations (rtx mem1, rtx mem2)
26819 rtx reg1, reg2;
26820 HOST_WIDE_INT off1, size1, off2, size2;
26822 if (get_memref_parts (mem1, &reg1, &off1, &size1)
26823 && get_memref_parts (mem2, &reg2, &off2, &size2))
26824 return ((REGNO (reg1) == REGNO (reg2))
26825 && ((off1 + size1 == off2)
26826 || (off2 + size2 == off1)));
26828 return false;
26831 /* This function returns true if it can be determined that the two MEM
26832 locations overlap by at least 1 byte based on base reg/offset/size. */
26834 static bool
26835 mem_locations_overlap (rtx mem1, rtx mem2)
26837 rtx reg1, reg2;
26838 HOST_WIDE_INT off1, size1, off2, size2;
26840 if (get_memref_parts (mem1, &reg1, &off1, &size1)
26841 && get_memref_parts (mem2, &reg2, &off2, &size2))
26842 return ((REGNO (reg1) == REGNO (reg2))
26843 && (((off1 <= off2) && (off1 + size1 > off2))
26844 || ((off2 <= off1) && (off2 + size2 > off1))));
26846 return false;
26849 /* A C statement (sans semicolon) to update the integer scheduling
26850 priority INSN_PRIORITY (INSN). Increase the priority to execute the
26851 INSN earlier, reduce the priority to execute INSN later. Do not
26852 define this macro if you do not need to adjust the scheduling
26853 priorities of insns. */
26855 static int
26856 rs6000_adjust_priority (rtx_insn *insn ATTRIBUTE_UNUSED, int priority)
26858 rtx load_mem, str_mem;
26859 /* On machines (like the 750) which have asymmetric integer units,
26860 where one integer unit can do multiply and divides and the other
26861 can't, reduce the priority of multiply/divide so it is scheduled
26862 before other integer operations. */
26864 #if 0
26865 if (! INSN_P (insn))
26866 return priority;
26868 if (GET_CODE (PATTERN (insn)) == USE)
26869 return priority;
26871 switch (rs6000_cpu_attr) {
26872 case CPU_PPC750:
26873 switch (get_attr_type (insn))
26875 default:
26876 break;
26878 case TYPE_MUL:
26879 case TYPE_DIV:
26880 fprintf (stderr, "priority was %#x (%d) before adjustment\n",
26881 priority, priority);
26882 if (priority >= 0 && priority < 0x01000000)
26883 priority >>= 3;
26884 break;
26887 #endif
26889 if (insn_must_be_first_in_group (insn)
26890 && reload_completed
26891 && current_sched_info->sched_max_insns_priority
26892 && rs6000_sched_restricted_insns_priority)
26895 /* Prioritize insns that can be dispatched only in the first
26896 dispatch slot. */
26897 if (rs6000_sched_restricted_insns_priority == 1)
26898 /* Attach highest priority to insn. This means that in
26899 haifa-sched.c:ready_sort(), dispatch-slot restriction considerations
26900 precede 'priority' (critical path) considerations. */
26901 return current_sched_info->sched_max_insns_priority;
26902 else if (rs6000_sched_restricted_insns_priority == 2)
26903 /* Increase priority of insn by a minimal amount. This means that in
26904 haifa-sched.c:ready_sort(), only 'priority' (critical path)
26905 considerations precede dispatch-slot restriction considerations. */
26906 return (priority + 1);
26909 if (rs6000_cpu == PROCESSOR_POWER6
26910 && ((load_store_pendulum == -2 && is_load_insn (insn, &load_mem))
26911 || (load_store_pendulum == 2 && is_store_insn (insn, &str_mem))))
26912 /* Attach highest priority to insn if the scheduler has just issued two
26913 stores and this instruction is a load, or two loads and this instruction
26914 is a store. Power6 wants loads and stores scheduled alternately
26915 when possible */
26916 return current_sched_info->sched_max_insns_priority;
26918 return priority;
26921 /* Return true if the instruction is nonpipelined on the Cell. */
26922 static bool
26923 is_nonpipeline_insn (rtx_insn *insn)
26925 enum attr_type type;
26926 if (!insn || !NONDEBUG_INSN_P (insn)
26927 || GET_CODE (PATTERN (insn)) == USE
26928 || GET_CODE (PATTERN (insn)) == CLOBBER)
26929 return false;
26931 type = get_attr_type (insn);
26932 if (type == TYPE_MUL
26933 || type == TYPE_DIV
26934 || type == TYPE_SDIV
26935 || type == TYPE_DDIV
26936 || type == TYPE_SSQRT
26937 || type == TYPE_DSQRT
26938 || type == TYPE_MFCR
26939 || type == TYPE_MFCRF
26940 || type == TYPE_MFJMPR)
26942 return true;
26944 return false;
26948 /* Return how many instructions the machine can issue per cycle. */
26950 static int
26951 rs6000_issue_rate (void)
26953 /* Unless scheduling for register pressure, use issue rate of 1 for
26954 first scheduling pass to decrease degradation. */
26955 if (!reload_completed && !flag_sched_pressure)
26956 return 1;
26958 switch (rs6000_cpu_attr) {
26959 case CPU_RS64A:
26960 case CPU_PPC601: /* ? */
26961 case CPU_PPC7450:
26962 return 3;
26963 case CPU_PPC440:
26964 case CPU_PPC603:
26965 case CPU_PPC750:
26966 case CPU_PPC7400:
26967 case CPU_PPC8540:
26968 case CPU_PPC8548:
26969 case CPU_CELL:
26970 case CPU_PPCE300C2:
26971 case CPU_PPCE300C3:
26972 case CPU_PPCE500MC:
26973 case CPU_PPCE500MC64:
26974 case CPU_PPCE5500:
26975 case CPU_PPCE6500:
26976 case CPU_TITAN:
26977 return 2;
26978 case CPU_PPC476:
26979 case CPU_PPC604:
26980 case CPU_PPC604E:
26981 case CPU_PPC620:
26982 case CPU_PPC630:
26983 return 4;
26984 case CPU_POWER4:
26985 case CPU_POWER5:
26986 case CPU_POWER6:
26987 case CPU_POWER7:
26988 return 5;
26989 case CPU_POWER8:
26990 return 7;
26991 default:
26992 return 1;
26996 /* Return how many instructions to look ahead for better insn
26997 scheduling. */
26999 static int
27000 rs6000_use_sched_lookahead (void)
27002 switch (rs6000_cpu_attr)
27004 case CPU_PPC8540:
27005 case CPU_PPC8548:
27006 return 4;
27008 case CPU_CELL:
27009 return (reload_completed ? 8 : 0);
27011 default:
27012 return 0;
27016 /* We are choosing insn from the ready queue. Return zero if INSN can be
27017 chosen. */
27018 static int
27019 rs6000_use_sched_lookahead_guard (rtx_insn *insn, int ready_index)
27021 if (ready_index == 0)
27022 return 0;
27024 if (rs6000_cpu_attr != CPU_CELL)
27025 return 0;
27027 gcc_assert (insn != NULL_RTX && INSN_P (insn));
27029 if (!reload_completed
27030 || is_nonpipeline_insn (insn)
27031 || is_microcoded_insn (insn))
27032 return 1;
27034 return 0;
27037 /* Determine if PAT refers to memory. If so, set MEM_REF to the MEM rtx
27038 and return true. */
27040 static bool
27041 find_mem_ref (rtx pat, rtx *mem_ref)
27043 const char * fmt;
27044 int i, j;
27046 /* stack_tie does not produce any real memory traffic. */
27047 if (tie_operand (pat, VOIDmode))
27048 return false;
27050 if (GET_CODE (pat) == MEM)
27052 *mem_ref = pat;
27053 return true;
27056 /* Recursively process the pattern. */
27057 fmt = GET_RTX_FORMAT (GET_CODE (pat));
27059 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
27061 if (fmt[i] == 'e')
27063 if (find_mem_ref (XEXP (pat, i), mem_ref))
27064 return true;
27066 else if (fmt[i] == 'E')
27067 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
27069 if (find_mem_ref (XVECEXP (pat, i, j), mem_ref))
27070 return true;
27074 return false;
27077 /* Determine if PAT is a PATTERN of a load insn. */
27079 static bool
27080 is_load_insn1 (rtx pat, rtx *load_mem)
27082 if (!pat || pat == NULL_RTX)
27083 return false;
27085 if (GET_CODE (pat) == SET)
27086 return find_mem_ref (SET_SRC (pat), load_mem);
27088 if (GET_CODE (pat) == PARALLEL)
27090 int i;
27092 for (i = 0; i < XVECLEN (pat, 0); i++)
27093 if (is_load_insn1 (XVECEXP (pat, 0, i), load_mem))
27094 return true;
27097 return false;
27100 /* Determine if INSN loads from memory. */
27102 static bool
27103 is_load_insn (rtx insn, rtx *load_mem)
27105 if (!insn || !INSN_P (insn))
27106 return false;
27108 if (CALL_P (insn))
27109 return false;
27111 return is_load_insn1 (PATTERN (insn), load_mem);
27114 /* Determine if PAT is a PATTERN of a store insn. */
27116 static bool
27117 is_store_insn1 (rtx pat, rtx *str_mem)
27119 if (!pat || pat == NULL_RTX)
27120 return false;
27122 if (GET_CODE (pat) == SET)
27123 return find_mem_ref (SET_DEST (pat), str_mem);
27125 if (GET_CODE (pat) == PARALLEL)
27127 int i;
27129 for (i = 0; i < XVECLEN (pat, 0); i++)
27130 if (is_store_insn1 (XVECEXP (pat, 0, i), str_mem))
27131 return true;
27134 return false;
27137 /* Determine if INSN stores to memory. */
27139 static bool
27140 is_store_insn (rtx insn, rtx *str_mem)
27142 if (!insn || !INSN_P (insn))
27143 return false;
27145 return is_store_insn1 (PATTERN (insn), str_mem);
27148 /* Returns whether the dependence between INSN and NEXT is considered
27149 costly by the given target. */
27151 static bool
27152 rs6000_is_costly_dependence (dep_t dep, int cost, int distance)
27154 rtx insn;
27155 rtx next;
27156 rtx load_mem, str_mem;
27158 /* If the flag is not enabled - no dependence is considered costly;
27159 allow all dependent insns in the same group.
27160 This is the most aggressive option. */
27161 if (rs6000_sched_costly_dep == no_dep_costly)
27162 return false;
27164 /* If the flag is set to 1 - a dependence is always considered costly;
27165 do not allow dependent instructions in the same group.
27166 This is the most conservative option. */
27167 if (rs6000_sched_costly_dep == all_deps_costly)
27168 return true;
27170 insn = DEP_PRO (dep);
27171 next = DEP_CON (dep);
27173 if (rs6000_sched_costly_dep == store_to_load_dep_costly
27174 && is_load_insn (next, &load_mem)
27175 && is_store_insn (insn, &str_mem))
27176 /* Prevent load after store in the same group. */
27177 return true;
27179 if (rs6000_sched_costly_dep == true_store_to_load_dep_costly
27180 && is_load_insn (next, &load_mem)
27181 && is_store_insn (insn, &str_mem)
27182 && DEP_TYPE (dep) == REG_DEP_TRUE
27183 && mem_locations_overlap(str_mem, load_mem))
27184 /* Prevent load after store in the same group if it is a true
27185 dependence. */
27186 return true;
27188 /* The flag is set to X; dependences with latency >= X are considered costly,
27189 and will not be scheduled in the same group. */
27190 if (rs6000_sched_costly_dep <= max_dep_latency
27191 && ((cost - distance) >= (int)rs6000_sched_costly_dep))
27192 return true;
27194 return false;
27197 /* Return the next insn after INSN that is found before TAIL is reached,
27198 skipping any "non-active" insns - insns that will not actually occupy
27199 an issue slot. Return NULL_RTX if such an insn is not found. */
27201 static rtx_insn *
27202 get_next_active_insn (rtx_insn *insn, rtx_insn *tail)
27204 if (insn == NULL_RTX || insn == tail)
27205 return NULL;
27207 while (1)
27209 insn = NEXT_INSN (insn);
27210 if (insn == NULL_RTX || insn == tail)
27211 return NULL;
27213 if (CALL_P (insn)
27214 || JUMP_P (insn) || JUMP_TABLE_DATA_P (insn)
27215 || (NONJUMP_INSN_P (insn)
27216 && GET_CODE (PATTERN (insn)) != USE
27217 && GET_CODE (PATTERN (insn)) != CLOBBER
27218 && INSN_CODE (insn) != CODE_FOR_stack_tie))
27219 break;
27221 return insn;
27224 /* We are about to begin issuing insns for this clock cycle. */
27226 static int
27227 rs6000_sched_reorder (FILE *dump ATTRIBUTE_UNUSED, int sched_verbose,
27228 rtx_insn **ready ATTRIBUTE_UNUSED,
27229 int *pn_ready ATTRIBUTE_UNUSED,
27230 int clock_var ATTRIBUTE_UNUSED)
27232 int n_ready = *pn_ready;
27234 if (sched_verbose)
27235 fprintf (dump, "// rs6000_sched_reorder :\n");
27237 /* Reorder the ready list, if the second to last ready insn
27238 is a nonepipeline insn. */
27239 if (rs6000_cpu_attr == CPU_CELL && n_ready > 1)
27241 if (is_nonpipeline_insn (ready[n_ready - 1])
27242 && (recog_memoized (ready[n_ready - 2]) > 0))
27243 /* Simply swap first two insns. */
27245 rtx_insn *tmp = ready[n_ready - 1];
27246 ready[n_ready - 1] = ready[n_ready - 2];
27247 ready[n_ready - 2] = tmp;
27251 if (rs6000_cpu == PROCESSOR_POWER6)
27252 load_store_pendulum = 0;
27254 return rs6000_issue_rate ();
27257 /* Like rs6000_sched_reorder, but called after issuing each insn. */
27259 static int
27260 rs6000_sched_reorder2 (FILE *dump, int sched_verbose, rtx_insn **ready,
27261 int *pn_ready, int clock_var ATTRIBUTE_UNUSED)
27263 if (sched_verbose)
27264 fprintf (dump, "// rs6000_sched_reorder2 :\n");
27266 /* For Power6, we need to handle some special cases to try and keep the
27267 store queue from overflowing and triggering expensive flushes.
27269 This code monitors how load and store instructions are being issued
27270 and skews the ready list one way or the other to increase the likelihood
27271 that a desired instruction is issued at the proper time.
27273 A couple of things are done. First, we maintain a "load_store_pendulum"
27274 to track the current state of load/store issue.
27276 - If the pendulum is at zero, then no loads or stores have been
27277 issued in the current cycle so we do nothing.
27279 - If the pendulum is 1, then a single load has been issued in this
27280 cycle and we attempt to locate another load in the ready list to
27281 issue with it.
27283 - If the pendulum is -2, then two stores have already been
27284 issued in this cycle, so we increase the priority of the first load
27285 in the ready list to increase it's likelihood of being chosen first
27286 in the next cycle.
27288 - If the pendulum is -1, then a single store has been issued in this
27289 cycle and we attempt to locate another store in the ready list to
27290 issue with it, preferring a store to an adjacent memory location to
27291 facilitate store pairing in the store queue.
27293 - If the pendulum is 2, then two loads have already been
27294 issued in this cycle, so we increase the priority of the first store
27295 in the ready list to increase it's likelihood of being chosen first
27296 in the next cycle.
27298 - If the pendulum < -2 or > 2, then do nothing.
27300 Note: This code covers the most common scenarios. There exist non
27301 load/store instructions which make use of the LSU and which
27302 would need to be accounted for to strictly model the behavior
27303 of the machine. Those instructions are currently unaccounted
27304 for to help minimize compile time overhead of this code.
27306 if (rs6000_cpu == PROCESSOR_POWER6 && last_scheduled_insn)
27308 int pos;
27309 int i;
27310 rtx_insn *tmp;
27311 rtx load_mem, str_mem;
27313 if (is_store_insn (last_scheduled_insn, &str_mem))
27314 /* Issuing a store, swing the load_store_pendulum to the left */
27315 load_store_pendulum--;
27316 else if (is_load_insn (last_scheduled_insn, &load_mem))
27317 /* Issuing a load, swing the load_store_pendulum to the right */
27318 load_store_pendulum++;
27319 else
27320 return cached_can_issue_more;
27322 /* If the pendulum is balanced, or there is only one instruction on
27323 the ready list, then all is well, so return. */
27324 if ((load_store_pendulum == 0) || (*pn_ready <= 1))
27325 return cached_can_issue_more;
27327 if (load_store_pendulum == 1)
27329 /* A load has been issued in this cycle. Scan the ready list
27330 for another load to issue with it */
27331 pos = *pn_ready-1;
27333 while (pos >= 0)
27335 if (is_load_insn (ready[pos], &load_mem))
27337 /* Found a load. Move it to the head of the ready list,
27338 and adjust it's priority so that it is more likely to
27339 stay there */
27340 tmp = ready[pos];
27341 for (i=pos; i<*pn_ready-1; i++)
27342 ready[i] = ready[i + 1];
27343 ready[*pn_ready-1] = tmp;
27345 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
27346 INSN_PRIORITY (tmp)++;
27347 break;
27349 pos--;
27352 else if (load_store_pendulum == -2)
27354 /* Two stores have been issued in this cycle. Increase the
27355 priority of the first load in the ready list to favor it for
27356 issuing in the next cycle. */
27357 pos = *pn_ready-1;
27359 while (pos >= 0)
27361 if (is_load_insn (ready[pos], &load_mem)
27362 && !sel_sched_p ()
27363 && INSN_PRIORITY_KNOWN (ready[pos]))
27365 INSN_PRIORITY (ready[pos])++;
27367 /* Adjust the pendulum to account for the fact that a load
27368 was found and increased in priority. This is to prevent
27369 increasing the priority of multiple loads */
27370 load_store_pendulum--;
27372 break;
27374 pos--;
27377 else if (load_store_pendulum == -1)
27379 /* A store has been issued in this cycle. Scan the ready list for
27380 another store to issue with it, preferring a store to an adjacent
27381 memory location */
27382 int first_store_pos = -1;
27384 pos = *pn_ready-1;
27386 while (pos >= 0)
27388 if (is_store_insn (ready[pos], &str_mem))
27390 rtx str_mem2;
27391 /* Maintain the index of the first store found on the
27392 list */
27393 if (first_store_pos == -1)
27394 first_store_pos = pos;
27396 if (is_store_insn (last_scheduled_insn, &str_mem2)
27397 && adjacent_mem_locations (str_mem, str_mem2))
27399 /* Found an adjacent store. Move it to the head of the
27400 ready list, and adjust it's priority so that it is
27401 more likely to stay there */
27402 tmp = ready[pos];
27403 for (i=pos; i<*pn_ready-1; i++)
27404 ready[i] = ready[i + 1];
27405 ready[*pn_ready-1] = tmp;
27407 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
27408 INSN_PRIORITY (tmp)++;
27410 first_store_pos = -1;
27412 break;
27415 pos--;
27418 if (first_store_pos >= 0)
27420 /* An adjacent store wasn't found, but a non-adjacent store was,
27421 so move the non-adjacent store to the front of the ready
27422 list, and adjust its priority so that it is more likely to
27423 stay there. */
27424 tmp = ready[first_store_pos];
27425 for (i=first_store_pos; i<*pn_ready-1; i++)
27426 ready[i] = ready[i + 1];
27427 ready[*pn_ready-1] = tmp;
27428 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
27429 INSN_PRIORITY (tmp)++;
27432 else if (load_store_pendulum == 2)
27434 /* Two loads have been issued in this cycle. Increase the priority
27435 of the first store in the ready list to favor it for issuing in
27436 the next cycle. */
27437 pos = *pn_ready-1;
27439 while (pos >= 0)
27441 if (is_store_insn (ready[pos], &str_mem)
27442 && !sel_sched_p ()
27443 && INSN_PRIORITY_KNOWN (ready[pos]))
27445 INSN_PRIORITY (ready[pos])++;
27447 /* Adjust the pendulum to account for the fact that a store
27448 was found and increased in priority. This is to prevent
27449 increasing the priority of multiple stores */
27450 load_store_pendulum++;
27452 break;
27454 pos--;
27459 return cached_can_issue_more;
27462 /* Return whether the presence of INSN causes a dispatch group termination
27463 of group WHICH_GROUP.
27465 If WHICH_GROUP == current_group, this function will return true if INSN
27466 causes the termination of the current group (i.e, the dispatch group to
27467 which INSN belongs). This means that INSN will be the last insn in the
27468 group it belongs to.
27470 If WHICH_GROUP == previous_group, this function will return true if INSN
27471 causes the termination of the previous group (i.e, the dispatch group that
27472 precedes the group to which INSN belongs). This means that INSN will be
27473 the first insn in the group it belongs to). */
27475 static bool
27476 insn_terminates_group_p (rtx_insn *insn, enum group_termination which_group)
27478 bool first, last;
27480 if (! insn)
27481 return false;
27483 first = insn_must_be_first_in_group (insn);
27484 last = insn_must_be_last_in_group (insn);
27486 if (first && last)
27487 return true;
27489 if (which_group == current_group)
27490 return last;
27491 else if (which_group == previous_group)
27492 return first;
27494 return false;
27498 static bool
27499 insn_must_be_first_in_group (rtx_insn *insn)
27501 enum attr_type type;
27503 if (!insn
27504 || NOTE_P (insn)
27505 || DEBUG_INSN_P (insn)
27506 || GET_CODE (PATTERN (insn)) == USE
27507 || GET_CODE (PATTERN (insn)) == CLOBBER)
27508 return false;
27510 switch (rs6000_cpu)
27512 case PROCESSOR_POWER5:
27513 if (is_cracked_insn (insn))
27514 return true;
27515 case PROCESSOR_POWER4:
27516 if (is_microcoded_insn (insn))
27517 return true;
27519 if (!rs6000_sched_groups)
27520 return false;
27522 type = get_attr_type (insn);
27524 switch (type)
27526 case TYPE_MFCR:
27527 case TYPE_MFCRF:
27528 case TYPE_MTCR:
27529 case TYPE_DELAYED_CR:
27530 case TYPE_CR_LOGICAL:
27531 case TYPE_MTJMPR:
27532 case TYPE_MFJMPR:
27533 case TYPE_DIV:
27534 case TYPE_LOAD_L:
27535 case TYPE_STORE_C:
27536 case TYPE_ISYNC:
27537 case TYPE_SYNC:
27538 return true;
27539 default:
27540 break;
27542 break;
27543 case PROCESSOR_POWER6:
27544 type = get_attr_type (insn);
27546 switch (type)
27548 case TYPE_EXTS:
27549 case TYPE_CNTLZ:
27550 case TYPE_TRAP:
27551 case TYPE_MUL:
27552 case TYPE_INSERT:
27553 case TYPE_FPCOMPARE:
27554 case TYPE_MFCR:
27555 case TYPE_MTCR:
27556 case TYPE_MFJMPR:
27557 case TYPE_MTJMPR:
27558 case TYPE_ISYNC:
27559 case TYPE_SYNC:
27560 case TYPE_LOAD_L:
27561 case TYPE_STORE_C:
27562 return true;
27563 case TYPE_SHIFT:
27564 if (get_attr_dot (insn) == DOT_NO
27565 || get_attr_var_shift (insn) == VAR_SHIFT_NO)
27566 return true;
27567 else
27568 break;
27569 case TYPE_DIV:
27570 if (get_attr_size (insn) == SIZE_32)
27571 return true;
27572 else
27573 break;
27574 case TYPE_LOAD:
27575 case TYPE_STORE:
27576 case TYPE_FPLOAD:
27577 case TYPE_FPSTORE:
27578 if (get_attr_update (insn) == UPDATE_YES)
27579 return true;
27580 else
27581 break;
27582 default:
27583 break;
27585 break;
27586 case PROCESSOR_POWER7:
27587 type = get_attr_type (insn);
27589 switch (type)
27591 case TYPE_CR_LOGICAL:
27592 case TYPE_MFCR:
27593 case TYPE_MFCRF:
27594 case TYPE_MTCR:
27595 case TYPE_DIV:
27596 case TYPE_COMPARE:
27597 case TYPE_ISYNC:
27598 case TYPE_LOAD_L:
27599 case TYPE_STORE_C:
27600 case TYPE_MFJMPR:
27601 case TYPE_MTJMPR:
27602 return true;
27603 case TYPE_MUL:
27604 case TYPE_SHIFT:
27605 case TYPE_EXTS:
27606 if (get_attr_dot (insn) == DOT_YES)
27607 return true;
27608 else
27609 break;
27610 case TYPE_LOAD:
27611 if (get_attr_sign_extend (insn) == SIGN_EXTEND_YES
27612 || get_attr_update (insn) == UPDATE_YES)
27613 return true;
27614 else
27615 break;
27616 case TYPE_STORE:
27617 case TYPE_FPLOAD:
27618 case TYPE_FPSTORE:
27619 if (get_attr_update (insn) == UPDATE_YES)
27620 return true;
27621 else
27622 break;
27623 default:
27624 break;
27626 break;
27627 case PROCESSOR_POWER8:
27628 type = get_attr_type (insn);
27630 switch (type)
27632 case TYPE_CR_LOGICAL:
27633 case TYPE_DELAYED_CR:
27634 case TYPE_MFCR:
27635 case TYPE_MFCRF:
27636 case TYPE_MTCR:
27637 case TYPE_COMPARE:
27638 case TYPE_SYNC:
27639 case TYPE_ISYNC:
27640 case TYPE_LOAD_L:
27641 case TYPE_STORE_C:
27642 case TYPE_VECSTORE:
27643 case TYPE_MFJMPR:
27644 case TYPE_MTJMPR:
27645 return true;
27646 case TYPE_SHIFT:
27647 case TYPE_EXTS:
27648 case TYPE_MUL:
27649 if (get_attr_dot (insn) == DOT_YES)
27650 return true;
27651 else
27652 break;
27653 case TYPE_LOAD:
27654 if (get_attr_sign_extend (insn) == SIGN_EXTEND_YES
27655 || get_attr_update (insn) == UPDATE_YES)
27656 return true;
27657 else
27658 break;
27659 case TYPE_STORE:
27660 if (get_attr_update (insn) == UPDATE_YES
27661 && get_attr_indexed (insn) == INDEXED_YES)
27662 return true;
27663 else
27664 break;
27665 default:
27666 break;
27668 break;
27669 default:
27670 break;
27673 return false;
27676 static bool
27677 insn_must_be_last_in_group (rtx_insn *insn)
27679 enum attr_type type;
27681 if (!insn
27682 || NOTE_P (insn)
27683 || DEBUG_INSN_P (insn)
27684 || GET_CODE (PATTERN (insn)) == USE
27685 || GET_CODE (PATTERN (insn)) == CLOBBER)
27686 return false;
27688 switch (rs6000_cpu) {
27689 case PROCESSOR_POWER4:
27690 case PROCESSOR_POWER5:
27691 if (is_microcoded_insn (insn))
27692 return true;
27694 if (is_branch_slot_insn (insn))
27695 return true;
27697 break;
27698 case PROCESSOR_POWER6:
27699 type = get_attr_type (insn);
27701 switch (type)
27703 case TYPE_EXTS:
27704 case TYPE_CNTLZ:
27705 case TYPE_TRAP:
27706 case TYPE_MUL:
27707 case TYPE_FPCOMPARE:
27708 case TYPE_MFCR:
27709 case TYPE_MTCR:
27710 case TYPE_MFJMPR:
27711 case TYPE_MTJMPR:
27712 case TYPE_ISYNC:
27713 case TYPE_SYNC:
27714 case TYPE_LOAD_L:
27715 case TYPE_STORE_C:
27716 return true;
27717 case TYPE_SHIFT:
27718 if (get_attr_dot (insn) == DOT_NO
27719 || get_attr_var_shift (insn) == VAR_SHIFT_NO)
27720 return true;
27721 else
27722 break;
27723 case TYPE_DIV:
27724 if (get_attr_size (insn) == SIZE_32)
27725 return true;
27726 else
27727 break;
27728 default:
27729 break;
27731 break;
27732 case PROCESSOR_POWER7:
27733 type = get_attr_type (insn);
27735 switch (type)
27737 case TYPE_ISYNC:
27738 case TYPE_SYNC:
27739 case TYPE_LOAD_L:
27740 case TYPE_STORE_C:
27741 return true;
27742 case TYPE_LOAD:
27743 if (get_attr_sign_extend (insn) == SIGN_EXTEND_YES
27744 && get_attr_update (insn) == UPDATE_YES)
27745 return true;
27746 else
27747 break;
27748 case TYPE_STORE:
27749 if (get_attr_update (insn) == UPDATE_YES
27750 && get_attr_indexed (insn) == INDEXED_YES)
27751 return true;
27752 else
27753 break;
27754 default:
27755 break;
27757 break;
27758 case PROCESSOR_POWER8:
27759 type = get_attr_type (insn);
27761 switch (type)
27763 case TYPE_MFCR:
27764 case TYPE_MTCR:
27765 case TYPE_ISYNC:
27766 case TYPE_SYNC:
27767 case TYPE_LOAD_L:
27768 case TYPE_STORE_C:
27769 return true;
27770 case TYPE_LOAD:
27771 if (get_attr_sign_extend (insn) == SIGN_EXTEND_YES
27772 && get_attr_update (insn) == UPDATE_YES)
27773 return true;
27774 else
27775 break;
27776 case TYPE_STORE:
27777 if (get_attr_update (insn) == UPDATE_YES
27778 && get_attr_indexed (insn) == INDEXED_YES)
27779 return true;
27780 else
27781 break;
27782 default:
27783 break;
27785 break;
27786 default:
27787 break;
27790 return false;
27793 /* Return true if it is recommended to keep NEXT_INSN "far" (in a separate
27794 dispatch group) from the insns in GROUP_INSNS. Return false otherwise. */
27796 static bool
27797 is_costly_group (rtx *group_insns, rtx next_insn)
27799 int i;
27800 int issue_rate = rs6000_issue_rate ();
27802 for (i = 0; i < issue_rate; i++)
27804 sd_iterator_def sd_it;
27805 dep_t dep;
27806 rtx insn = group_insns[i];
27808 if (!insn)
27809 continue;
27811 FOR_EACH_DEP (insn, SD_LIST_RES_FORW, sd_it, dep)
27813 rtx next = DEP_CON (dep);
27815 if (next == next_insn
27816 && rs6000_is_costly_dependence (dep, dep_cost (dep), 0))
27817 return true;
27821 return false;
27824 /* Utility of the function redefine_groups.
27825 Check if it is too costly to schedule NEXT_INSN together with GROUP_INSNS
27826 in the same dispatch group. If so, insert nops before NEXT_INSN, in order
27827 to keep it "far" (in a separate group) from GROUP_INSNS, following
27828 one of the following schemes, depending on the value of the flag
27829 -minsert_sched_nops = X:
27830 (1) X == sched_finish_regroup_exact: insert exactly as many nops as needed
27831 in order to force NEXT_INSN into a separate group.
27832 (2) X < sched_finish_regroup_exact: insert exactly X nops.
27833 GROUP_END, CAN_ISSUE_MORE and GROUP_COUNT record the state after nop
27834 insertion (has a group just ended, how many vacant issue slots remain in the
27835 last group, and how many dispatch groups were encountered so far). */
27837 static int
27838 force_new_group (int sched_verbose, FILE *dump, rtx *group_insns,
27839 rtx_insn *next_insn, bool *group_end, int can_issue_more,
27840 int *group_count)
27842 rtx nop;
27843 bool force;
27844 int issue_rate = rs6000_issue_rate ();
27845 bool end = *group_end;
27846 int i;
27848 if (next_insn == NULL_RTX || DEBUG_INSN_P (next_insn))
27849 return can_issue_more;
27851 if (rs6000_sched_insert_nops > sched_finish_regroup_exact)
27852 return can_issue_more;
27854 force = is_costly_group (group_insns, next_insn);
27855 if (!force)
27856 return can_issue_more;
27858 if (sched_verbose > 6)
27859 fprintf (dump,"force: group count = %d, can_issue_more = %d\n",
27860 *group_count ,can_issue_more);
27862 if (rs6000_sched_insert_nops == sched_finish_regroup_exact)
27864 if (*group_end)
27865 can_issue_more = 0;
27867 /* Since only a branch can be issued in the last issue_slot, it is
27868 sufficient to insert 'can_issue_more - 1' nops if next_insn is not
27869 a branch. If next_insn is a branch, we insert 'can_issue_more' nops;
27870 in this case the last nop will start a new group and the branch
27871 will be forced to the new group. */
27872 if (can_issue_more && !is_branch_slot_insn (next_insn))
27873 can_issue_more--;
27875 /* Do we have a special group ending nop? */
27876 if (rs6000_cpu_attr == CPU_POWER6 || rs6000_cpu_attr == CPU_POWER7
27877 || rs6000_cpu_attr == CPU_POWER8)
27879 nop = gen_group_ending_nop ();
27880 emit_insn_before (nop, next_insn);
27881 can_issue_more = 0;
27883 else
27884 while (can_issue_more > 0)
27886 nop = gen_nop ();
27887 emit_insn_before (nop, next_insn);
27888 can_issue_more--;
27891 *group_end = true;
27892 return 0;
27895 if (rs6000_sched_insert_nops < sched_finish_regroup_exact)
27897 int n_nops = rs6000_sched_insert_nops;
27899 /* Nops can't be issued from the branch slot, so the effective
27900 issue_rate for nops is 'issue_rate - 1'. */
27901 if (can_issue_more == 0)
27902 can_issue_more = issue_rate;
27903 can_issue_more--;
27904 if (can_issue_more == 0)
27906 can_issue_more = issue_rate - 1;
27907 (*group_count)++;
27908 end = true;
27909 for (i = 0; i < issue_rate; i++)
27911 group_insns[i] = 0;
27915 while (n_nops > 0)
27917 nop = gen_nop ();
27918 emit_insn_before (nop, next_insn);
27919 if (can_issue_more == issue_rate - 1) /* new group begins */
27920 end = false;
27921 can_issue_more--;
27922 if (can_issue_more == 0)
27924 can_issue_more = issue_rate - 1;
27925 (*group_count)++;
27926 end = true;
27927 for (i = 0; i < issue_rate; i++)
27929 group_insns[i] = 0;
27932 n_nops--;
27935 /* Scale back relative to 'issue_rate' (instead of 'issue_rate - 1'). */
27936 can_issue_more++;
27938 /* Is next_insn going to start a new group? */
27939 *group_end
27940 = (end
27941 || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
27942 || (can_issue_more <= 2 && is_cracked_insn (next_insn))
27943 || (can_issue_more < issue_rate &&
27944 insn_terminates_group_p (next_insn, previous_group)));
27945 if (*group_end && end)
27946 (*group_count)--;
27948 if (sched_verbose > 6)
27949 fprintf (dump, "done force: group count = %d, can_issue_more = %d\n",
27950 *group_count, can_issue_more);
27951 return can_issue_more;
27954 return can_issue_more;
27957 /* This function tries to synch the dispatch groups that the compiler "sees"
27958 with the dispatch groups that the processor dispatcher is expected to
27959 form in practice. It tries to achieve this synchronization by forcing the
27960 estimated processor grouping on the compiler (as opposed to the function
27961 'pad_goups' which tries to force the scheduler's grouping on the processor).
27963 The function scans the insn sequence between PREV_HEAD_INSN and TAIL and
27964 examines the (estimated) dispatch groups that will be formed by the processor
27965 dispatcher. It marks these group boundaries to reflect the estimated
27966 processor grouping, overriding the grouping that the scheduler had marked.
27967 Depending on the value of the flag '-minsert-sched-nops' this function can
27968 force certain insns into separate groups or force a certain distance between
27969 them by inserting nops, for example, if there exists a "costly dependence"
27970 between the insns.
27972 The function estimates the group boundaries that the processor will form as
27973 follows: It keeps track of how many vacant issue slots are available after
27974 each insn. A subsequent insn will start a new group if one of the following
27975 4 cases applies:
27976 - no more vacant issue slots remain in the current dispatch group.
27977 - only the last issue slot, which is the branch slot, is vacant, but the next
27978 insn is not a branch.
27979 - only the last 2 or less issue slots, including the branch slot, are vacant,
27980 which means that a cracked insn (which occupies two issue slots) can't be
27981 issued in this group.
27982 - less than 'issue_rate' slots are vacant, and the next insn always needs to
27983 start a new group. */
27985 static int
27986 redefine_groups (FILE *dump, int sched_verbose, rtx_insn *prev_head_insn,
27987 rtx_insn *tail)
27989 rtx_insn *insn, *next_insn;
27990 int issue_rate;
27991 int can_issue_more;
27992 int slot, i;
27993 bool group_end;
27994 int group_count = 0;
27995 rtx *group_insns;
27997 /* Initialize. */
27998 issue_rate = rs6000_issue_rate ();
27999 group_insns = XALLOCAVEC (rtx, issue_rate);
28000 for (i = 0; i < issue_rate; i++)
28002 group_insns[i] = 0;
28004 can_issue_more = issue_rate;
28005 slot = 0;
28006 insn = get_next_active_insn (prev_head_insn, tail);
28007 group_end = false;
28009 while (insn != NULL_RTX)
28011 slot = (issue_rate - can_issue_more);
28012 group_insns[slot] = insn;
28013 can_issue_more =
28014 rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
28015 if (insn_terminates_group_p (insn, current_group))
28016 can_issue_more = 0;
28018 next_insn = get_next_active_insn (insn, tail);
28019 if (next_insn == NULL_RTX)
28020 return group_count + 1;
28022 /* Is next_insn going to start a new group? */
28023 group_end
28024 = (can_issue_more == 0
28025 || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
28026 || (can_issue_more <= 2 && is_cracked_insn (next_insn))
28027 || (can_issue_more < issue_rate &&
28028 insn_terminates_group_p (next_insn, previous_group)));
28030 can_issue_more = force_new_group (sched_verbose, dump, group_insns,
28031 next_insn, &group_end, can_issue_more,
28032 &group_count);
28034 if (group_end)
28036 group_count++;
28037 can_issue_more = 0;
28038 for (i = 0; i < issue_rate; i++)
28040 group_insns[i] = 0;
28044 if (GET_MODE (next_insn) == TImode && can_issue_more)
28045 PUT_MODE (next_insn, VOIDmode);
28046 else if (!can_issue_more && GET_MODE (next_insn) != TImode)
28047 PUT_MODE (next_insn, TImode);
28049 insn = next_insn;
28050 if (can_issue_more == 0)
28051 can_issue_more = issue_rate;
28052 } /* while */
28054 return group_count;
28057 /* Scan the insn sequence between PREV_HEAD_INSN and TAIL and examine the
28058 dispatch group boundaries that the scheduler had marked. Pad with nops
28059 any dispatch groups which have vacant issue slots, in order to force the
28060 scheduler's grouping on the processor dispatcher. The function
28061 returns the number of dispatch groups found. */
28063 static int
28064 pad_groups (FILE *dump, int sched_verbose, rtx_insn *prev_head_insn,
28065 rtx_insn *tail)
28067 rtx_insn *insn, *next_insn;
28068 rtx nop;
28069 int issue_rate;
28070 int can_issue_more;
28071 int group_end;
28072 int group_count = 0;
28074 /* Initialize issue_rate. */
28075 issue_rate = rs6000_issue_rate ();
28076 can_issue_more = issue_rate;
28078 insn = get_next_active_insn (prev_head_insn, tail);
28079 next_insn = get_next_active_insn (insn, tail);
28081 while (insn != NULL_RTX)
28083 can_issue_more =
28084 rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
28086 group_end = (next_insn == NULL_RTX || GET_MODE (next_insn) == TImode);
28088 if (next_insn == NULL_RTX)
28089 break;
28091 if (group_end)
28093 /* If the scheduler had marked group termination at this location
28094 (between insn and next_insn), and neither insn nor next_insn will
28095 force group termination, pad the group with nops to force group
28096 termination. */
28097 if (can_issue_more
28098 && (rs6000_sched_insert_nops == sched_finish_pad_groups)
28099 && !insn_terminates_group_p (insn, current_group)
28100 && !insn_terminates_group_p (next_insn, previous_group))
28102 if (!is_branch_slot_insn (next_insn))
28103 can_issue_more--;
28105 while (can_issue_more)
28107 nop = gen_nop ();
28108 emit_insn_before (nop, next_insn);
28109 can_issue_more--;
28113 can_issue_more = issue_rate;
28114 group_count++;
28117 insn = next_insn;
28118 next_insn = get_next_active_insn (insn, tail);
28121 return group_count;
28124 /* We're beginning a new block. Initialize data structures as necessary. */
28126 static void
28127 rs6000_sched_init (FILE *dump ATTRIBUTE_UNUSED,
28128 int sched_verbose ATTRIBUTE_UNUSED,
28129 int max_ready ATTRIBUTE_UNUSED)
28131 last_scheduled_insn = NULL_RTX;
28132 load_store_pendulum = 0;
28135 /* The following function is called at the end of scheduling BB.
28136 After reload, it inserts nops at insn group bundling. */
28138 static void
28139 rs6000_sched_finish (FILE *dump, int sched_verbose)
28141 int n_groups;
28143 if (sched_verbose)
28144 fprintf (dump, "=== Finishing schedule.\n");
28146 if (reload_completed && rs6000_sched_groups)
28148 /* Do not run sched_finish hook when selective scheduling enabled. */
28149 if (sel_sched_p ())
28150 return;
28152 if (rs6000_sched_insert_nops == sched_finish_none)
28153 return;
28155 if (rs6000_sched_insert_nops == sched_finish_pad_groups)
28156 n_groups = pad_groups (dump, sched_verbose,
28157 current_sched_info->prev_head,
28158 current_sched_info->next_tail);
28159 else
28160 n_groups = redefine_groups (dump, sched_verbose,
28161 current_sched_info->prev_head,
28162 current_sched_info->next_tail);
28164 if (sched_verbose >= 6)
28166 fprintf (dump, "ngroups = %d\n", n_groups);
28167 print_rtl (dump, current_sched_info->prev_head);
28168 fprintf (dump, "Done finish_sched\n");
28173 struct _rs6000_sched_context
28175 short cached_can_issue_more;
28176 rtx last_scheduled_insn;
28177 int load_store_pendulum;
28180 typedef struct _rs6000_sched_context rs6000_sched_context_def;
28181 typedef rs6000_sched_context_def *rs6000_sched_context_t;
28183 /* Allocate store for new scheduling context. */
28184 static void *
28185 rs6000_alloc_sched_context (void)
28187 return xmalloc (sizeof (rs6000_sched_context_def));
28190 /* If CLEAN_P is true then initializes _SC with clean data,
28191 and from the global context otherwise. */
28192 static void
28193 rs6000_init_sched_context (void *_sc, bool clean_p)
28195 rs6000_sched_context_t sc = (rs6000_sched_context_t) _sc;
28197 if (clean_p)
28199 sc->cached_can_issue_more = 0;
28200 sc->last_scheduled_insn = NULL_RTX;
28201 sc->load_store_pendulum = 0;
28203 else
28205 sc->cached_can_issue_more = cached_can_issue_more;
28206 sc->last_scheduled_insn = last_scheduled_insn;
28207 sc->load_store_pendulum = load_store_pendulum;
28211 /* Sets the global scheduling context to the one pointed to by _SC. */
28212 static void
28213 rs6000_set_sched_context (void *_sc)
28215 rs6000_sched_context_t sc = (rs6000_sched_context_t) _sc;
28217 gcc_assert (sc != NULL);
28219 cached_can_issue_more = sc->cached_can_issue_more;
28220 last_scheduled_insn = sc->last_scheduled_insn;
28221 load_store_pendulum = sc->load_store_pendulum;
28224 /* Free _SC. */
28225 static void
28226 rs6000_free_sched_context (void *_sc)
28228 gcc_assert (_sc != NULL);
28230 free (_sc);
28234 /* Length in units of the trampoline for entering a nested function. */
28237 rs6000_trampoline_size (void)
28239 int ret = 0;
28241 switch (DEFAULT_ABI)
28243 default:
28244 gcc_unreachable ();
28246 case ABI_AIX:
28247 ret = (TARGET_32BIT) ? 12 : 24;
28248 break;
28250 case ABI_ELFv2:
28251 gcc_assert (!TARGET_32BIT);
28252 ret = 32;
28253 break;
28255 case ABI_DARWIN:
28256 case ABI_V4:
28257 ret = (TARGET_32BIT) ? 40 : 48;
28258 break;
28261 return ret;
28264 /* Emit RTL insns to initialize the variable parts of a trampoline.
28265 FNADDR is an RTX for the address of the function's pure code.
28266 CXT is an RTX for the static chain value for the function. */
28268 static void
28269 rs6000_trampoline_init (rtx m_tramp, tree fndecl, rtx cxt)
28271 int regsize = (TARGET_32BIT) ? 4 : 8;
28272 rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
28273 rtx ctx_reg = force_reg (Pmode, cxt);
28274 rtx addr = force_reg (Pmode, XEXP (m_tramp, 0));
28276 switch (DEFAULT_ABI)
28278 default:
28279 gcc_unreachable ();
28281 /* Under AIX, just build the 3 word function descriptor */
28282 case ABI_AIX:
28284 rtx fnmem, fn_reg, toc_reg;
28286 if (!TARGET_POINTERS_TO_NESTED_FUNCTIONS)
28287 error ("You cannot take the address of a nested function if you use "
28288 "the -mno-pointers-to-nested-functions option.");
28290 fnmem = gen_const_mem (Pmode, force_reg (Pmode, fnaddr));
28291 fn_reg = gen_reg_rtx (Pmode);
28292 toc_reg = gen_reg_rtx (Pmode);
28294 /* Macro to shorten the code expansions below. */
28295 # define MEM_PLUS(MEM, OFFSET) adjust_address (MEM, Pmode, OFFSET)
28297 m_tramp = replace_equiv_address (m_tramp, addr);
28299 emit_move_insn (fn_reg, MEM_PLUS (fnmem, 0));
28300 emit_move_insn (toc_reg, MEM_PLUS (fnmem, regsize));
28301 emit_move_insn (MEM_PLUS (m_tramp, 0), fn_reg);
28302 emit_move_insn (MEM_PLUS (m_tramp, regsize), toc_reg);
28303 emit_move_insn (MEM_PLUS (m_tramp, 2*regsize), ctx_reg);
28305 # undef MEM_PLUS
28307 break;
28309 /* Under V.4/eabi/darwin, __trampoline_setup does the real work. */
28310 case ABI_ELFv2:
28311 case ABI_DARWIN:
28312 case ABI_V4:
28313 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__trampoline_setup"),
28314 LCT_NORMAL, VOIDmode, 4,
28315 addr, Pmode,
28316 GEN_INT (rs6000_trampoline_size ()), SImode,
28317 fnaddr, Pmode,
28318 ctx_reg, Pmode);
28319 break;
28324 /* Returns TRUE iff the target attribute indicated by ATTR_ID takes a plain
28325 identifier as an argument, so the front end shouldn't look it up. */
28327 static bool
28328 rs6000_attribute_takes_identifier_p (const_tree attr_id)
28330 return is_attribute_p ("altivec", attr_id);
28333 /* Handle the "altivec" attribute. The attribute may have
28334 arguments as follows:
28336 __attribute__((altivec(vector__)))
28337 __attribute__((altivec(pixel__))) (always followed by 'unsigned short')
28338 __attribute__((altivec(bool__))) (always followed by 'unsigned')
28340 and may appear more than once (e.g., 'vector bool char') in a
28341 given declaration. */
28343 static tree
28344 rs6000_handle_altivec_attribute (tree *node,
28345 tree name ATTRIBUTE_UNUSED,
28346 tree args,
28347 int flags ATTRIBUTE_UNUSED,
28348 bool *no_add_attrs)
28350 tree type = *node, result = NULL_TREE;
28351 enum machine_mode mode;
28352 int unsigned_p;
28353 char altivec_type
28354 = ((args && TREE_CODE (args) == TREE_LIST && TREE_VALUE (args)
28355 && TREE_CODE (TREE_VALUE (args)) == IDENTIFIER_NODE)
28356 ? *IDENTIFIER_POINTER (TREE_VALUE (args))
28357 : '?');
28359 while (POINTER_TYPE_P (type)
28360 || TREE_CODE (type) == FUNCTION_TYPE
28361 || TREE_CODE (type) == METHOD_TYPE
28362 || TREE_CODE (type) == ARRAY_TYPE)
28363 type = TREE_TYPE (type);
28365 mode = TYPE_MODE (type);
28367 /* Check for invalid AltiVec type qualifiers. */
28368 if (type == long_double_type_node)
28369 error ("use of %<long double%> in AltiVec types is invalid");
28370 else if (type == boolean_type_node)
28371 error ("use of boolean types in AltiVec types is invalid");
28372 else if (TREE_CODE (type) == COMPLEX_TYPE)
28373 error ("use of %<complex%> in AltiVec types is invalid");
28374 else if (DECIMAL_FLOAT_MODE_P (mode))
28375 error ("use of decimal floating point types in AltiVec types is invalid");
28376 else if (!TARGET_VSX)
28378 if (type == long_unsigned_type_node || type == long_integer_type_node)
28380 if (TARGET_64BIT)
28381 error ("use of %<long%> in AltiVec types is invalid for "
28382 "64-bit code without -mvsx");
28383 else if (rs6000_warn_altivec_long)
28384 warning (0, "use of %<long%> in AltiVec types is deprecated; "
28385 "use %<int%>");
28387 else if (type == long_long_unsigned_type_node
28388 || type == long_long_integer_type_node)
28389 error ("use of %<long long%> in AltiVec types is invalid without "
28390 "-mvsx");
28391 else if (type == double_type_node)
28392 error ("use of %<double%> in AltiVec types is invalid without -mvsx");
28395 switch (altivec_type)
28397 case 'v':
28398 unsigned_p = TYPE_UNSIGNED (type);
28399 switch (mode)
28401 case TImode:
28402 result = (unsigned_p ? unsigned_V1TI_type_node : V1TI_type_node);
28403 break;
28404 case DImode:
28405 result = (unsigned_p ? unsigned_V2DI_type_node : V2DI_type_node);
28406 break;
28407 case SImode:
28408 result = (unsigned_p ? unsigned_V4SI_type_node : V4SI_type_node);
28409 break;
28410 case HImode:
28411 result = (unsigned_p ? unsigned_V8HI_type_node : V8HI_type_node);
28412 break;
28413 case QImode:
28414 result = (unsigned_p ? unsigned_V16QI_type_node : V16QI_type_node);
28415 break;
28416 case SFmode: result = V4SF_type_node; break;
28417 case DFmode: result = V2DF_type_node; break;
28418 /* If the user says 'vector int bool', we may be handed the 'bool'
28419 attribute _before_ the 'vector' attribute, and so select the
28420 proper type in the 'b' case below. */
28421 case V4SImode: case V8HImode: case V16QImode: case V4SFmode:
28422 case V2DImode: case V2DFmode:
28423 result = type;
28424 default: break;
28426 break;
28427 case 'b':
28428 switch (mode)
28430 case DImode: case V2DImode: result = bool_V2DI_type_node; break;
28431 case SImode: case V4SImode: result = bool_V4SI_type_node; break;
28432 case HImode: case V8HImode: result = bool_V8HI_type_node; break;
28433 case QImode: case V16QImode: result = bool_V16QI_type_node;
28434 default: break;
28436 break;
28437 case 'p':
28438 switch (mode)
28440 case V8HImode: result = pixel_V8HI_type_node;
28441 default: break;
28443 default: break;
28446 /* Propagate qualifiers attached to the element type
28447 onto the vector type. */
28448 if (result && result != type && TYPE_QUALS (type))
28449 result = build_qualified_type (result, TYPE_QUALS (type));
28451 *no_add_attrs = true; /* No need to hang on to the attribute. */
28453 if (result)
28454 *node = lang_hooks.types.reconstruct_complex_type (*node, result);
28456 return NULL_TREE;
28459 /* AltiVec defines four built-in scalar types that serve as vector
28460 elements; we must teach the compiler how to mangle them. */
28462 static const char *
28463 rs6000_mangle_type (const_tree type)
28465 type = TYPE_MAIN_VARIANT (type);
28467 if (TREE_CODE (type) != VOID_TYPE && TREE_CODE (type) != BOOLEAN_TYPE
28468 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
28469 return NULL;
28471 if (type == bool_char_type_node) return "U6__boolc";
28472 if (type == bool_short_type_node) return "U6__bools";
28473 if (type == pixel_type_node) return "u7__pixel";
28474 if (type == bool_int_type_node) return "U6__booli";
28475 if (type == bool_long_type_node) return "U6__booll";
28477 /* Mangle IBM extended float long double as `g' (__float128) on
28478 powerpc*-linux where long-double-64 previously was the default. */
28479 if (TYPE_MAIN_VARIANT (type) == long_double_type_node
28480 && TARGET_ELF
28481 && TARGET_LONG_DOUBLE_128
28482 && !TARGET_IEEEQUAD)
28483 return "g";
28485 /* For all other types, use normal C++ mangling. */
28486 return NULL;
28489 /* Handle a "longcall" or "shortcall" attribute; arguments as in
28490 struct attribute_spec.handler. */
28492 static tree
28493 rs6000_handle_longcall_attribute (tree *node, tree name,
28494 tree args ATTRIBUTE_UNUSED,
28495 int flags ATTRIBUTE_UNUSED,
28496 bool *no_add_attrs)
28498 if (TREE_CODE (*node) != FUNCTION_TYPE
28499 && TREE_CODE (*node) != FIELD_DECL
28500 && TREE_CODE (*node) != TYPE_DECL)
28502 warning (OPT_Wattributes, "%qE attribute only applies to functions",
28503 name);
28504 *no_add_attrs = true;
28507 return NULL_TREE;
28510 /* Set longcall attributes on all functions declared when
28511 rs6000_default_long_calls is true. */
28512 static void
28513 rs6000_set_default_type_attributes (tree type)
28515 if (rs6000_default_long_calls
28516 && (TREE_CODE (type) == FUNCTION_TYPE
28517 || TREE_CODE (type) == METHOD_TYPE))
28518 TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("longcall"),
28519 NULL_TREE,
28520 TYPE_ATTRIBUTES (type));
28522 #if TARGET_MACHO
28523 darwin_set_default_type_attributes (type);
28524 #endif
28527 /* Return a reference suitable for calling a function with the
28528 longcall attribute. */
28531 rs6000_longcall_ref (rtx call_ref)
28533 const char *call_name;
28534 tree node;
28536 if (GET_CODE (call_ref) != SYMBOL_REF)
28537 return call_ref;
28539 /* System V adds '.' to the internal name, so skip them. */
28540 call_name = XSTR (call_ref, 0);
28541 if (*call_name == '.')
28543 while (*call_name == '.')
28544 call_name++;
28546 node = get_identifier (call_name);
28547 call_ref = gen_rtx_SYMBOL_REF (VOIDmode, IDENTIFIER_POINTER (node));
28550 return force_reg (Pmode, call_ref);
28553 #ifndef TARGET_USE_MS_BITFIELD_LAYOUT
28554 #define TARGET_USE_MS_BITFIELD_LAYOUT 0
28555 #endif
28557 /* Handle a "ms_struct" or "gcc_struct" attribute; arguments as in
28558 struct attribute_spec.handler. */
28559 static tree
28560 rs6000_handle_struct_attribute (tree *node, tree name,
28561 tree args ATTRIBUTE_UNUSED,
28562 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
28564 tree *type = NULL;
28565 if (DECL_P (*node))
28567 if (TREE_CODE (*node) == TYPE_DECL)
28568 type = &TREE_TYPE (*node);
28570 else
28571 type = node;
28573 if (!(type && (TREE_CODE (*type) == RECORD_TYPE
28574 || TREE_CODE (*type) == UNION_TYPE)))
28576 warning (OPT_Wattributes, "%qE attribute ignored", name);
28577 *no_add_attrs = true;
28580 else if ((is_attribute_p ("ms_struct", name)
28581 && lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (*type)))
28582 || ((is_attribute_p ("gcc_struct", name)
28583 && lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (*type)))))
28585 warning (OPT_Wattributes, "%qE incompatible attribute ignored",
28586 name);
28587 *no_add_attrs = true;
28590 return NULL_TREE;
28593 static bool
28594 rs6000_ms_bitfield_layout_p (const_tree record_type)
28596 return (TARGET_USE_MS_BITFIELD_LAYOUT &&
28597 !lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (record_type)))
28598 || lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (record_type));
28601 #ifdef USING_ELFOS_H
28603 /* A get_unnamed_section callback, used for switching to toc_section. */
28605 static void
28606 rs6000_elf_output_toc_section_asm_op (const void *data ATTRIBUTE_UNUSED)
28608 if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
28609 && TARGET_MINIMAL_TOC
28610 && !TARGET_RELOCATABLE)
28612 if (!toc_initialized)
28614 toc_initialized = 1;
28615 fprintf (asm_out_file, "%s\n", TOC_SECTION_ASM_OP);
28616 (*targetm.asm_out.internal_label) (asm_out_file, "LCTOC", 0);
28617 fprintf (asm_out_file, "\t.tc ");
28618 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1[TC],");
28619 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
28620 fprintf (asm_out_file, "\n");
28622 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
28623 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
28624 fprintf (asm_out_file, " = .+32768\n");
28626 else
28627 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
28629 else if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
28630 && !TARGET_RELOCATABLE)
28631 fprintf (asm_out_file, "%s\n", TOC_SECTION_ASM_OP);
28632 else
28634 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
28635 if (!toc_initialized)
28637 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
28638 fprintf (asm_out_file, " = .+32768\n");
28639 toc_initialized = 1;
28644 /* Implement TARGET_ASM_INIT_SECTIONS. */
28646 static void
28647 rs6000_elf_asm_init_sections (void)
28649 toc_section
28650 = get_unnamed_section (0, rs6000_elf_output_toc_section_asm_op, NULL);
28652 sdata2_section
28653 = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
28654 SDATA2_SECTION_ASM_OP);
28657 /* Implement TARGET_SELECT_RTX_SECTION. */
28659 static section *
28660 rs6000_elf_select_rtx_section (enum machine_mode mode, rtx x,
28661 unsigned HOST_WIDE_INT align)
28663 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
28664 return toc_section;
28665 else
28666 return default_elf_select_rtx_section (mode, x, align);
28669 /* For a SYMBOL_REF, set generic flags and then perform some
28670 target-specific processing.
28672 When the AIX ABI is requested on a non-AIX system, replace the
28673 function name with the real name (with a leading .) rather than the
28674 function descriptor name. This saves a lot of overriding code to
28675 read the prefixes. */
28677 static void rs6000_elf_encode_section_info (tree, rtx, int) ATTRIBUTE_UNUSED;
28678 static void
28679 rs6000_elf_encode_section_info (tree decl, rtx rtl, int first)
28681 default_encode_section_info (decl, rtl, first);
28683 if (first
28684 && TREE_CODE (decl) == FUNCTION_DECL
28685 && !TARGET_AIX
28686 && DEFAULT_ABI == ABI_AIX)
28688 rtx sym_ref = XEXP (rtl, 0);
28689 size_t len = strlen (XSTR (sym_ref, 0));
28690 char *str = XALLOCAVEC (char, len + 2);
28691 str[0] = '.';
28692 memcpy (str + 1, XSTR (sym_ref, 0), len + 1);
28693 XSTR (sym_ref, 0) = ggc_alloc_string (str, len + 1);
28697 static inline bool
28698 compare_section_name (const char *section, const char *templ)
28700 int len;
28702 len = strlen (templ);
28703 return (strncmp (section, templ, len) == 0
28704 && (section[len] == 0 || section[len] == '.'));
28707 bool
28708 rs6000_elf_in_small_data_p (const_tree decl)
28710 if (rs6000_sdata == SDATA_NONE)
28711 return false;
28713 /* We want to merge strings, so we never consider them small data. */
28714 if (TREE_CODE (decl) == STRING_CST)
28715 return false;
28717 /* Functions are never in the small data area. */
28718 if (TREE_CODE (decl) == FUNCTION_DECL)
28719 return false;
28721 if (TREE_CODE (decl) == VAR_DECL && DECL_SECTION_NAME (decl))
28723 const char *section = DECL_SECTION_NAME (decl);
28724 if (compare_section_name (section, ".sdata")
28725 || compare_section_name (section, ".sdata2")
28726 || compare_section_name (section, ".gnu.linkonce.s")
28727 || compare_section_name (section, ".sbss")
28728 || compare_section_name (section, ".sbss2")
28729 || compare_section_name (section, ".gnu.linkonce.sb")
28730 || strcmp (section, ".PPC.EMB.sdata0") == 0
28731 || strcmp (section, ".PPC.EMB.sbss0") == 0)
28732 return true;
28734 else
28736 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (decl));
28738 if (size > 0
28739 && size <= g_switch_value
28740 /* If it's not public, and we're not going to reference it there,
28741 there's no need to put it in the small data section. */
28742 && (rs6000_sdata != SDATA_DATA || TREE_PUBLIC (decl)))
28743 return true;
28746 return false;
28749 #endif /* USING_ELFOS_H */
28751 /* Implement TARGET_USE_BLOCKS_FOR_CONSTANT_P. */
28753 static bool
28754 rs6000_use_blocks_for_constant_p (enum machine_mode mode, const_rtx x)
28756 return !ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode);
28759 /* Do not place thread-local symbols refs in the object blocks. */
28761 static bool
28762 rs6000_use_blocks_for_decl_p (const_tree decl)
28764 return !DECL_THREAD_LOCAL_P (decl);
28767 /* Return a REG that occurs in ADDR with coefficient 1.
28768 ADDR can be effectively incremented by incrementing REG.
28770 r0 is special and we must not select it as an address
28771 register by this routine since our caller will try to
28772 increment the returned register via an "la" instruction. */
28775 find_addr_reg (rtx addr)
28777 while (GET_CODE (addr) == PLUS)
28779 if (GET_CODE (XEXP (addr, 0)) == REG
28780 && REGNO (XEXP (addr, 0)) != 0)
28781 addr = XEXP (addr, 0);
28782 else if (GET_CODE (XEXP (addr, 1)) == REG
28783 && REGNO (XEXP (addr, 1)) != 0)
28784 addr = XEXP (addr, 1);
28785 else if (CONSTANT_P (XEXP (addr, 0)))
28786 addr = XEXP (addr, 1);
28787 else if (CONSTANT_P (XEXP (addr, 1)))
28788 addr = XEXP (addr, 0);
28789 else
28790 gcc_unreachable ();
28792 gcc_assert (GET_CODE (addr) == REG && REGNO (addr) != 0);
28793 return addr;
28796 void
28797 rs6000_fatal_bad_address (rtx op)
28799 fatal_insn ("bad address", op);
28802 #if TARGET_MACHO
28804 typedef struct branch_island_d {
28805 tree function_name;
28806 tree label_name;
28807 int line_number;
28808 } branch_island;
28811 static vec<branch_island, va_gc> *branch_islands;
28813 /* Remember to generate a branch island for far calls to the given
28814 function. */
28816 static void
28817 add_compiler_branch_island (tree label_name, tree function_name,
28818 int line_number)
28820 branch_island bi = {function_name, label_name, line_number};
28821 vec_safe_push (branch_islands, bi);
28824 /* Generate far-jump branch islands for everything recorded in
28825 branch_islands. Invoked immediately after the last instruction of
28826 the epilogue has been emitted; the branch islands must be appended
28827 to, and contiguous with, the function body. Mach-O stubs are
28828 generated in machopic_output_stub(). */
28830 static void
28831 macho_branch_islands (void)
28833 char tmp_buf[512];
28835 while (!vec_safe_is_empty (branch_islands))
28837 branch_island *bi = &branch_islands->last ();
28838 const char *label = IDENTIFIER_POINTER (bi->label_name);
28839 const char *name = IDENTIFIER_POINTER (bi->function_name);
28840 char name_buf[512];
28841 /* Cheap copy of the details from the Darwin ASM_OUTPUT_LABELREF(). */
28842 if (name[0] == '*' || name[0] == '&')
28843 strcpy (name_buf, name+1);
28844 else
28846 name_buf[0] = '_';
28847 strcpy (name_buf+1, name);
28849 strcpy (tmp_buf, "\n");
28850 strcat (tmp_buf, label);
28851 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
28852 if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
28853 dbxout_stabd (N_SLINE, bi->line_number);
28854 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
28855 if (flag_pic)
28857 if (TARGET_LINK_STACK)
28859 char name[32];
28860 get_ppc476_thunk_name (name);
28861 strcat (tmp_buf, ":\n\tmflr r0\n\tbl ");
28862 strcat (tmp_buf, name);
28863 strcat (tmp_buf, "\n");
28864 strcat (tmp_buf, label);
28865 strcat (tmp_buf, "_pic:\n\tmflr r11\n");
28867 else
28869 strcat (tmp_buf, ":\n\tmflr r0\n\tbcl 20,31,");
28870 strcat (tmp_buf, label);
28871 strcat (tmp_buf, "_pic\n");
28872 strcat (tmp_buf, label);
28873 strcat (tmp_buf, "_pic:\n\tmflr r11\n");
28876 strcat (tmp_buf, "\taddis r11,r11,ha16(");
28877 strcat (tmp_buf, name_buf);
28878 strcat (tmp_buf, " - ");
28879 strcat (tmp_buf, label);
28880 strcat (tmp_buf, "_pic)\n");
28882 strcat (tmp_buf, "\tmtlr r0\n");
28884 strcat (tmp_buf, "\taddi r12,r11,lo16(");
28885 strcat (tmp_buf, name_buf);
28886 strcat (tmp_buf, " - ");
28887 strcat (tmp_buf, label);
28888 strcat (tmp_buf, "_pic)\n");
28890 strcat (tmp_buf, "\tmtctr r12\n\tbctr\n");
28892 else
28894 strcat (tmp_buf, ":\nlis r12,hi16(");
28895 strcat (tmp_buf, name_buf);
28896 strcat (tmp_buf, ")\n\tori r12,r12,lo16(");
28897 strcat (tmp_buf, name_buf);
28898 strcat (tmp_buf, ")\n\tmtctr r12\n\tbctr");
28900 output_asm_insn (tmp_buf, 0);
28901 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
28902 if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
28903 dbxout_stabd (N_SLINE, bi->line_number);
28904 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
28905 branch_islands->pop ();
28909 /* NO_PREVIOUS_DEF checks in the link list whether the function name is
28910 already there or not. */
28912 static int
28913 no_previous_def (tree function_name)
28915 branch_island *bi;
28916 unsigned ix;
28918 FOR_EACH_VEC_SAFE_ELT (branch_islands, ix, bi)
28919 if (function_name == bi->function_name)
28920 return 0;
28921 return 1;
28924 /* GET_PREV_LABEL gets the label name from the previous definition of
28925 the function. */
28927 static tree
28928 get_prev_label (tree function_name)
28930 branch_island *bi;
28931 unsigned ix;
28933 FOR_EACH_VEC_SAFE_ELT (branch_islands, ix, bi)
28934 if (function_name == bi->function_name)
28935 return bi->label_name;
28936 return NULL_TREE;
28939 /* INSN is either a function call or a millicode call. It may have an
28940 unconditional jump in its delay slot.
28942 CALL_DEST is the routine we are calling. */
28944 char *
28945 output_call (rtx_insn *insn, rtx *operands, int dest_operand_number,
28946 int cookie_operand_number)
28948 static char buf[256];
28949 if (darwin_emit_branch_islands
28950 && GET_CODE (operands[dest_operand_number]) == SYMBOL_REF
28951 && (INTVAL (operands[cookie_operand_number]) & CALL_LONG))
28953 tree labelname;
28954 tree funname = get_identifier (XSTR (operands[dest_operand_number], 0));
28956 if (no_previous_def (funname))
28958 rtx label_rtx = gen_label_rtx ();
28959 char *label_buf, temp_buf[256];
28960 ASM_GENERATE_INTERNAL_LABEL (temp_buf, "L",
28961 CODE_LABEL_NUMBER (label_rtx));
28962 label_buf = temp_buf[0] == '*' ? temp_buf + 1 : temp_buf;
28963 labelname = get_identifier (label_buf);
28964 add_compiler_branch_island (labelname, funname, insn_line (insn));
28966 else
28967 labelname = get_prev_label (funname);
28969 /* "jbsr foo, L42" is Mach-O for "Link as 'bl foo' if a 'bl'
28970 instruction will reach 'foo', otherwise link as 'bl L42'".
28971 "L42" should be a 'branch island', that will do a far jump to
28972 'foo'. Branch islands are generated in
28973 macho_branch_islands(). */
28974 sprintf (buf, "jbsr %%z%d,%.246s",
28975 dest_operand_number, IDENTIFIER_POINTER (labelname));
28977 else
28978 sprintf (buf, "bl %%z%d", dest_operand_number);
28979 return buf;
28982 /* Generate PIC and indirect symbol stubs. */
28984 void
28985 machopic_output_stub (FILE *file, const char *symb, const char *stub)
28987 unsigned int length;
28988 char *symbol_name, *lazy_ptr_name;
28989 char *local_label_0;
28990 static int label = 0;
28992 /* Lose our funky encoding stuff so it doesn't contaminate the stub. */
28993 symb = (*targetm.strip_name_encoding) (symb);
28996 length = strlen (symb);
28997 symbol_name = XALLOCAVEC (char, length + 32);
28998 GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
29000 lazy_ptr_name = XALLOCAVEC (char, length + 32);
29001 GEN_LAZY_PTR_NAME_FOR_SYMBOL (lazy_ptr_name, symb, length);
29003 if (flag_pic == 2)
29004 switch_to_section (darwin_sections[machopic_picsymbol_stub1_section]);
29005 else
29006 switch_to_section (darwin_sections[machopic_symbol_stub1_section]);
29008 if (flag_pic == 2)
29010 fprintf (file, "\t.align 5\n");
29012 fprintf (file, "%s:\n", stub);
29013 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
29015 label++;
29016 local_label_0 = XALLOCAVEC (char, sizeof ("\"L00000000000$spb\""));
29017 sprintf (local_label_0, "\"L%011d$spb\"", label);
29019 fprintf (file, "\tmflr r0\n");
29020 if (TARGET_LINK_STACK)
29022 char name[32];
29023 get_ppc476_thunk_name (name);
29024 fprintf (file, "\tbl %s\n", name);
29025 fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
29027 else
29029 fprintf (file, "\tbcl 20,31,%s\n", local_label_0);
29030 fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
29032 fprintf (file, "\taddis r11,r11,ha16(%s-%s)\n",
29033 lazy_ptr_name, local_label_0);
29034 fprintf (file, "\tmtlr r0\n");
29035 fprintf (file, "\t%s r12,lo16(%s-%s)(r11)\n",
29036 (TARGET_64BIT ? "ldu" : "lwzu"),
29037 lazy_ptr_name, local_label_0);
29038 fprintf (file, "\tmtctr r12\n");
29039 fprintf (file, "\tbctr\n");
29041 else
29043 fprintf (file, "\t.align 4\n");
29045 fprintf (file, "%s:\n", stub);
29046 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
29048 fprintf (file, "\tlis r11,ha16(%s)\n", lazy_ptr_name);
29049 fprintf (file, "\t%s r12,lo16(%s)(r11)\n",
29050 (TARGET_64BIT ? "ldu" : "lwzu"),
29051 lazy_ptr_name);
29052 fprintf (file, "\tmtctr r12\n");
29053 fprintf (file, "\tbctr\n");
29056 switch_to_section (darwin_sections[machopic_lazy_symbol_ptr_section]);
29057 fprintf (file, "%s:\n", lazy_ptr_name);
29058 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
29059 fprintf (file, "%sdyld_stub_binding_helper\n",
29060 (TARGET_64BIT ? DOUBLE_INT_ASM_OP : "\t.long\t"));
29063 /* Legitimize PIC addresses. If the address is already
29064 position-independent, we return ORIG. Newly generated
29065 position-independent addresses go into a reg. This is REG if non
29066 zero, otherwise we allocate register(s) as necessary. */
29068 #define SMALL_INT(X) ((UINTVAL (X) + 0x8000) < 0x10000)
29071 rs6000_machopic_legitimize_pic_address (rtx orig, enum machine_mode mode,
29072 rtx reg)
29074 rtx base, offset;
29076 if (reg == NULL && ! reload_in_progress && ! reload_completed)
29077 reg = gen_reg_rtx (Pmode);
29079 if (GET_CODE (orig) == CONST)
29081 rtx reg_temp;
29083 if (GET_CODE (XEXP (orig, 0)) == PLUS
29084 && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
29085 return orig;
29087 gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
29089 /* Use a different reg for the intermediate value, as
29090 it will be marked UNCHANGING. */
29091 reg_temp = !can_create_pseudo_p () ? reg : gen_reg_rtx (Pmode);
29092 base = rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 0),
29093 Pmode, reg_temp);
29094 offset =
29095 rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 1),
29096 Pmode, reg);
29098 if (GET_CODE (offset) == CONST_INT)
29100 if (SMALL_INT (offset))
29101 return plus_constant (Pmode, base, INTVAL (offset));
29102 else if (! reload_in_progress && ! reload_completed)
29103 offset = force_reg (Pmode, offset);
29104 else
29106 rtx mem = force_const_mem (Pmode, orig);
29107 return machopic_legitimize_pic_address (mem, Pmode, reg);
29110 return gen_rtx_PLUS (Pmode, base, offset);
29113 /* Fall back on generic machopic code. */
29114 return machopic_legitimize_pic_address (orig, mode, reg);
29117 /* Output a .machine directive for the Darwin assembler, and call
29118 the generic start_file routine. */
29120 static void
29121 rs6000_darwin_file_start (void)
29123 static const struct
29125 const char *arg;
29126 const char *name;
29127 HOST_WIDE_INT if_set;
29128 } mapping[] = {
29129 { "ppc64", "ppc64", MASK_64BIT },
29130 { "970", "ppc970", MASK_PPC_GPOPT | MASK_MFCRF | MASK_POWERPC64 },
29131 { "power4", "ppc970", 0 },
29132 { "G5", "ppc970", 0 },
29133 { "7450", "ppc7450", 0 },
29134 { "7400", "ppc7400", MASK_ALTIVEC },
29135 { "G4", "ppc7400", 0 },
29136 { "750", "ppc750", 0 },
29137 { "740", "ppc750", 0 },
29138 { "G3", "ppc750", 0 },
29139 { "604e", "ppc604e", 0 },
29140 { "604", "ppc604", 0 },
29141 { "603e", "ppc603", 0 },
29142 { "603", "ppc603", 0 },
29143 { "601", "ppc601", 0 },
29144 { NULL, "ppc", 0 } };
29145 const char *cpu_id = "";
29146 size_t i;
29148 rs6000_file_start ();
29149 darwin_file_start ();
29151 /* Determine the argument to -mcpu=. Default to G3 if not specified. */
29153 if (rs6000_default_cpu != 0 && rs6000_default_cpu[0] != '\0')
29154 cpu_id = rs6000_default_cpu;
29156 if (global_options_set.x_rs6000_cpu_index)
29157 cpu_id = processor_target_table[rs6000_cpu_index].name;
29159 /* Look through the mapping array. Pick the first name that either
29160 matches the argument, has a bit set in IF_SET that is also set
29161 in the target flags, or has a NULL name. */
29163 i = 0;
29164 while (mapping[i].arg != NULL
29165 && strcmp (mapping[i].arg, cpu_id) != 0
29166 && (mapping[i].if_set & rs6000_isa_flags) == 0)
29167 i++;
29169 fprintf (asm_out_file, "\t.machine %s\n", mapping[i].name);
29172 #endif /* TARGET_MACHO */
29174 #if TARGET_ELF
29175 static int
29176 rs6000_elf_reloc_rw_mask (void)
29178 if (flag_pic)
29179 return 3;
29180 else if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
29181 return 2;
29182 else
29183 return 0;
29186 /* Record an element in the table of global constructors. SYMBOL is
29187 a SYMBOL_REF of the function to be called; PRIORITY is a number
29188 between 0 and MAX_INIT_PRIORITY.
29190 This differs from default_named_section_asm_out_constructor in
29191 that we have special handling for -mrelocatable. */
29193 static void rs6000_elf_asm_out_constructor (rtx, int) ATTRIBUTE_UNUSED;
29194 static void
29195 rs6000_elf_asm_out_constructor (rtx symbol, int priority)
29197 const char *section = ".ctors";
29198 char buf[16];
29200 if (priority != DEFAULT_INIT_PRIORITY)
29202 sprintf (buf, ".ctors.%.5u",
29203 /* Invert the numbering so the linker puts us in the proper
29204 order; constructors are run from right to left, and the
29205 linker sorts in increasing order. */
29206 MAX_INIT_PRIORITY - priority);
29207 section = buf;
29210 switch_to_section (get_section (section, SECTION_WRITE, NULL));
29211 assemble_align (POINTER_SIZE);
29213 if (TARGET_RELOCATABLE)
29215 fputs ("\t.long (", asm_out_file);
29216 output_addr_const (asm_out_file, symbol);
29217 fputs (")@fixup\n", asm_out_file);
29219 else
29220 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
29223 static void rs6000_elf_asm_out_destructor (rtx, int) ATTRIBUTE_UNUSED;
29224 static void
29225 rs6000_elf_asm_out_destructor (rtx symbol, int priority)
29227 const char *section = ".dtors";
29228 char buf[16];
29230 if (priority != DEFAULT_INIT_PRIORITY)
29232 sprintf (buf, ".dtors.%.5u",
29233 /* Invert the numbering so the linker puts us in the proper
29234 order; constructors are run from right to left, and the
29235 linker sorts in increasing order. */
29236 MAX_INIT_PRIORITY - priority);
29237 section = buf;
29240 switch_to_section (get_section (section, SECTION_WRITE, NULL));
29241 assemble_align (POINTER_SIZE);
29243 if (TARGET_RELOCATABLE)
29245 fputs ("\t.long (", asm_out_file);
29246 output_addr_const (asm_out_file, symbol);
29247 fputs (")@fixup\n", asm_out_file);
29249 else
29250 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
29253 void
29254 rs6000_elf_declare_function_name (FILE *file, const char *name, tree decl)
29256 if (TARGET_64BIT && DEFAULT_ABI != ABI_ELFv2)
29258 fputs ("\t.section\t\".opd\",\"aw\"\n\t.align 3\n", file);
29259 ASM_OUTPUT_LABEL (file, name);
29260 fputs (DOUBLE_INT_ASM_OP, file);
29261 rs6000_output_function_entry (file, name);
29262 fputs (",.TOC.@tocbase,0\n\t.previous\n", file);
29263 if (DOT_SYMBOLS)
29265 fputs ("\t.size\t", file);
29266 assemble_name (file, name);
29267 fputs (",24\n\t.type\t.", file);
29268 assemble_name (file, name);
29269 fputs (",@function\n", file);
29270 if (TREE_PUBLIC (decl) && ! DECL_WEAK (decl))
29272 fputs ("\t.globl\t.", file);
29273 assemble_name (file, name);
29274 putc ('\n', file);
29277 else
29278 ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
29279 ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
29280 rs6000_output_function_entry (file, name);
29281 fputs (":\n", file);
29282 return;
29285 if (TARGET_RELOCATABLE
29286 && !TARGET_SECURE_PLT
29287 && (get_pool_size () != 0 || crtl->profile)
29288 && uses_TOC ())
29290 char buf[256];
29292 (*targetm.asm_out.internal_label) (file, "LCL", rs6000_pic_labelno);
29294 ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
29295 fprintf (file, "\t.long ");
29296 assemble_name (file, buf);
29297 putc ('-', file);
29298 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
29299 assemble_name (file, buf);
29300 putc ('\n', file);
29303 ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
29304 ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
29306 if (DEFAULT_ABI == ABI_AIX)
29308 const char *desc_name, *orig_name;
29310 orig_name = (*targetm.strip_name_encoding) (name);
29311 desc_name = orig_name;
29312 while (*desc_name == '.')
29313 desc_name++;
29315 if (TREE_PUBLIC (decl))
29316 fprintf (file, "\t.globl %s\n", desc_name);
29318 fprintf (file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
29319 fprintf (file, "%s:\n", desc_name);
29320 fprintf (file, "\t.long %s\n", orig_name);
29321 fputs ("\t.long _GLOBAL_OFFSET_TABLE_\n", file);
29322 fputs ("\t.long 0\n", file);
29323 fprintf (file, "\t.previous\n");
29325 ASM_OUTPUT_LABEL (file, name);
29328 static void rs6000_elf_file_end (void) ATTRIBUTE_UNUSED;
29329 static void
29330 rs6000_elf_file_end (void)
29332 #ifdef HAVE_AS_GNU_ATTRIBUTE
29333 if (TARGET_32BIT && DEFAULT_ABI == ABI_V4)
29335 if (rs6000_passes_float)
29336 fprintf (asm_out_file, "\t.gnu_attribute 4, %d\n",
29337 ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT) ? 1
29338 : (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_SINGLE_FLOAT) ? 3
29339 : 2));
29340 if (rs6000_passes_vector)
29341 fprintf (asm_out_file, "\t.gnu_attribute 8, %d\n",
29342 (TARGET_ALTIVEC_ABI ? 2
29343 : TARGET_SPE_ABI ? 3
29344 : 1));
29345 if (rs6000_returns_struct)
29346 fprintf (asm_out_file, "\t.gnu_attribute 12, %d\n",
29347 aix_struct_return ? 2 : 1);
29349 #endif
29350 #if defined (POWERPC_LINUX) || defined (POWERPC_FREEBSD)
29351 if (TARGET_32BIT || DEFAULT_ABI == ABI_ELFv2)
29352 file_end_indicate_exec_stack ();
29353 #endif
29355 #endif
29357 #if TARGET_XCOFF
29358 static void
29359 rs6000_xcoff_asm_output_anchor (rtx symbol)
29361 char buffer[100];
29363 sprintf (buffer, "$ + " HOST_WIDE_INT_PRINT_DEC,
29364 SYMBOL_REF_BLOCK_OFFSET (symbol));
29365 fprintf (asm_out_file, "%s", SET_ASM_OP);
29366 RS6000_OUTPUT_BASENAME (asm_out_file, XSTR (symbol, 0));
29367 fprintf (asm_out_file, ",");
29368 RS6000_OUTPUT_BASENAME (asm_out_file, buffer);
29369 fprintf (asm_out_file, "\n");
29372 static void
29373 rs6000_xcoff_asm_globalize_label (FILE *stream, const char *name)
29375 fputs (GLOBAL_ASM_OP, stream);
29376 RS6000_OUTPUT_BASENAME (stream, name);
29377 putc ('\n', stream);
29380 /* A get_unnamed_decl callback, used for read-only sections. PTR
29381 points to the section string variable. */
29383 static void
29384 rs6000_xcoff_output_readonly_section_asm_op (const void *directive)
29386 fprintf (asm_out_file, "\t.csect %s[RO],%s\n",
29387 *(const char *const *) directive,
29388 XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
29391 /* Likewise for read-write sections. */
29393 static void
29394 rs6000_xcoff_output_readwrite_section_asm_op (const void *directive)
29396 fprintf (asm_out_file, "\t.csect %s[RW],%s\n",
29397 *(const char *const *) directive,
29398 XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
29401 static void
29402 rs6000_xcoff_output_tls_section_asm_op (const void *directive)
29404 fprintf (asm_out_file, "\t.csect %s[TL],%s\n",
29405 *(const char *const *) directive,
29406 XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
29409 /* A get_unnamed_section callback, used for switching to toc_section. */
29411 static void
29412 rs6000_xcoff_output_toc_section_asm_op (const void *data ATTRIBUTE_UNUSED)
29414 if (TARGET_MINIMAL_TOC)
29416 /* toc_section is always selected at least once from
29417 rs6000_xcoff_file_start, so this is guaranteed to
29418 always be defined once and only once in each file. */
29419 if (!toc_initialized)
29421 fputs ("\t.toc\nLCTOC..1:\n", asm_out_file);
29422 fputs ("\t.tc toc_table[TC],toc_table[RW]\n", asm_out_file);
29423 toc_initialized = 1;
29425 fprintf (asm_out_file, "\t.csect toc_table[RW]%s\n",
29426 (TARGET_32BIT ? "" : ",3"));
29428 else
29429 fputs ("\t.toc\n", asm_out_file);
29432 /* Implement TARGET_ASM_INIT_SECTIONS. */
29434 static void
29435 rs6000_xcoff_asm_init_sections (void)
29437 read_only_data_section
29438 = get_unnamed_section (0, rs6000_xcoff_output_readonly_section_asm_op,
29439 &xcoff_read_only_section_name);
29441 private_data_section
29442 = get_unnamed_section (SECTION_WRITE,
29443 rs6000_xcoff_output_readwrite_section_asm_op,
29444 &xcoff_private_data_section_name);
29446 tls_data_section
29447 = get_unnamed_section (SECTION_TLS,
29448 rs6000_xcoff_output_tls_section_asm_op,
29449 &xcoff_tls_data_section_name);
29451 tls_private_data_section
29452 = get_unnamed_section (SECTION_TLS,
29453 rs6000_xcoff_output_tls_section_asm_op,
29454 &xcoff_private_data_section_name);
29456 read_only_private_data_section
29457 = get_unnamed_section (0, rs6000_xcoff_output_readonly_section_asm_op,
29458 &xcoff_private_data_section_name);
29460 toc_section
29461 = get_unnamed_section (0, rs6000_xcoff_output_toc_section_asm_op, NULL);
29463 readonly_data_section = read_only_data_section;
29464 exception_section = data_section;
29467 static int
29468 rs6000_xcoff_reloc_rw_mask (void)
29470 return 3;
29473 static void
29474 rs6000_xcoff_asm_named_section (const char *name, unsigned int flags,
29475 tree decl ATTRIBUTE_UNUSED)
29477 int smclass;
29478 static const char * const suffix[4] = { "PR", "RO", "RW", "TL" };
29480 if (flags & SECTION_CODE)
29481 smclass = 0;
29482 else if (flags & SECTION_TLS)
29483 smclass = 3;
29484 else if (flags & SECTION_WRITE)
29485 smclass = 2;
29486 else
29487 smclass = 1;
29489 fprintf (asm_out_file, "\t.csect %s%s[%s],%u\n",
29490 (flags & SECTION_CODE) ? "." : "",
29491 name, suffix[smclass], flags & SECTION_ENTSIZE);
29494 #define IN_NAMED_SECTION(DECL) \
29495 ((TREE_CODE (DECL) == FUNCTION_DECL || TREE_CODE (DECL) == VAR_DECL) \
29496 && DECL_SECTION_NAME (DECL) != NULL)
29498 static section *
29499 rs6000_xcoff_select_section (tree decl, int reloc,
29500 unsigned HOST_WIDE_INT align)
29502 /* Place variables with alignment stricter than BIGGEST_ALIGNMENT into
29503 named section. */
29504 if (align > BIGGEST_ALIGNMENT)
29506 resolve_unique_section (decl, reloc, true);
29507 if (IN_NAMED_SECTION (decl))
29508 return get_named_section (decl, NULL, reloc);
29511 if (decl_readonly_section (decl, reloc))
29513 if (TREE_PUBLIC (decl))
29514 return read_only_data_section;
29515 else
29516 return read_only_private_data_section;
29518 else
29520 #if HAVE_AS_TLS
29521 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
29523 if (TREE_PUBLIC (decl))
29524 return tls_data_section;
29525 else if (bss_initializer_p (decl))
29527 /* Convert to COMMON to emit in BSS. */
29528 DECL_COMMON (decl) = 1;
29529 return tls_comm_section;
29531 else
29532 return tls_private_data_section;
29534 else
29535 #endif
29536 if (TREE_PUBLIC (decl))
29537 return data_section;
29538 else
29539 return private_data_section;
29543 static void
29544 rs6000_xcoff_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED)
29546 const char *name;
29548 /* Use select_section for private data and uninitialized data with
29549 alignment <= BIGGEST_ALIGNMENT. */
29550 if (!TREE_PUBLIC (decl)
29551 || DECL_COMMON (decl)
29552 || (DECL_INITIAL (decl) == NULL_TREE
29553 && DECL_ALIGN (decl) <= BIGGEST_ALIGNMENT)
29554 || DECL_INITIAL (decl) == error_mark_node
29555 || (flag_zero_initialized_in_bss
29556 && initializer_zerop (DECL_INITIAL (decl))))
29557 return;
29559 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
29560 name = (*targetm.strip_name_encoding) (name);
29561 set_decl_section_name (decl, name);
29564 /* Select section for constant in constant pool.
29566 On RS/6000, all constants are in the private read-only data area.
29567 However, if this is being placed in the TOC it must be output as a
29568 toc entry. */
29570 static section *
29571 rs6000_xcoff_select_rtx_section (enum machine_mode mode, rtx x,
29572 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
29574 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
29575 return toc_section;
29576 else
29577 return read_only_private_data_section;
29580 /* Remove any trailing [DS] or the like from the symbol name. */
29582 static const char *
29583 rs6000_xcoff_strip_name_encoding (const char *name)
29585 size_t len;
29586 if (*name == '*')
29587 name++;
29588 len = strlen (name);
29589 if (name[len - 1] == ']')
29590 return ggc_alloc_string (name, len - 4);
29591 else
29592 return name;
29595 /* Section attributes. AIX is always PIC. */
29597 static unsigned int
29598 rs6000_xcoff_section_type_flags (tree decl, const char *name, int reloc)
29600 unsigned int align;
29601 unsigned int flags = default_section_type_flags (decl, name, reloc);
29603 /* Align to at least UNIT size. */
29604 if ((flags & SECTION_CODE) != 0 || !decl || !DECL_P (decl))
29605 align = MIN_UNITS_PER_WORD;
29606 else
29607 /* Increase alignment of large objects if not already stricter. */
29608 align = MAX ((DECL_ALIGN (decl) / BITS_PER_UNIT),
29609 int_size_in_bytes (TREE_TYPE (decl)) > MIN_UNITS_PER_WORD
29610 ? UNITS_PER_FP_WORD : MIN_UNITS_PER_WORD);
29612 return flags | (exact_log2 (align) & SECTION_ENTSIZE);
29615 /* Output at beginning of assembler file.
29617 Initialize the section names for the RS/6000 at this point.
29619 Specify filename, including full path, to assembler.
29621 We want to go into the TOC section so at least one .toc will be emitted.
29622 Also, in order to output proper .bs/.es pairs, we need at least one static
29623 [RW] section emitted.
29625 Finally, declare mcount when profiling to make the assembler happy. */
29627 static void
29628 rs6000_xcoff_file_start (void)
29630 rs6000_gen_section_name (&xcoff_bss_section_name,
29631 main_input_filename, ".bss_");
29632 rs6000_gen_section_name (&xcoff_private_data_section_name,
29633 main_input_filename, ".rw_");
29634 rs6000_gen_section_name (&xcoff_read_only_section_name,
29635 main_input_filename, ".ro_");
29636 rs6000_gen_section_name (&xcoff_tls_data_section_name,
29637 main_input_filename, ".tls_");
29638 rs6000_gen_section_name (&xcoff_tbss_section_name,
29639 main_input_filename, ".tbss_[UL]");
29641 fputs ("\t.file\t", asm_out_file);
29642 output_quoted_string (asm_out_file, main_input_filename);
29643 fputc ('\n', asm_out_file);
29644 if (write_symbols != NO_DEBUG)
29645 switch_to_section (private_data_section);
29646 switch_to_section (text_section);
29647 if (profile_flag)
29648 fprintf (asm_out_file, "\t.extern %s\n", RS6000_MCOUNT);
29649 rs6000_file_start ();
29652 /* Output at end of assembler file.
29653 On the RS/6000, referencing data should automatically pull in text. */
29655 static void
29656 rs6000_xcoff_file_end (void)
29658 switch_to_section (text_section);
29659 fputs ("_section_.text:\n", asm_out_file);
29660 switch_to_section (data_section);
29661 fputs (TARGET_32BIT
29662 ? "\t.long _section_.text\n" : "\t.llong _section_.text\n",
29663 asm_out_file);
29666 struct declare_alias_data
29668 FILE *file;
29669 bool function_descriptor;
29672 /* Declare alias N. A helper function for for_node_and_aliases. */
29674 static bool
29675 rs6000_declare_alias (struct symtab_node *n, void *d)
29677 struct declare_alias_data *data = (struct declare_alias_data *)d;
29678 /* Main symbol is output specially, because varasm machinery does part of
29679 the job for us - we do not need to declare .globl/lglobs and such. */
29680 if (!n->alias || n->weakref)
29681 return false;
29683 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (n->decl)))
29684 return false;
29686 /* Prevent assemble_alias from trying to use .set pseudo operation
29687 that does not behave as expected by the middle-end. */
29688 TREE_ASM_WRITTEN (n->decl) = true;
29690 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n->decl));
29691 char *buffer = (char *) alloca (strlen (name) + 2);
29692 char *p;
29693 int dollar_inside = 0;
29695 strcpy (buffer, name);
29696 p = strchr (buffer, '$');
29697 while (p) {
29698 *p = '_';
29699 dollar_inside++;
29700 p = strchr (p + 1, '$');
29702 if (TREE_PUBLIC (n->decl))
29704 if (!RS6000_WEAK || !DECL_WEAK (n->decl))
29706 if (dollar_inside) {
29707 if (data->function_descriptor)
29708 fprintf(data->file, "\t.rename .%s,\".%s\"\n", buffer, name);
29709 else
29710 fprintf(data->file, "\t.rename %s,\"%s\"\n", buffer, name);
29712 if (data->function_descriptor)
29713 fputs ("\t.globl .", data->file);
29714 else
29715 fputs ("\t.globl ", data->file);
29716 RS6000_OUTPUT_BASENAME (data->file, buffer);
29717 putc ('\n', data->file);
29719 else if (DECL_WEAK (n->decl) && !data->function_descriptor)
29720 ASM_WEAKEN_DECL (data->file, n->decl, name, NULL);
29722 else
29724 if (dollar_inside)
29726 if (data->function_descriptor)
29727 fprintf(data->file, "\t.rename %s,\"%s\"\n", buffer, name);
29728 else
29729 fprintf(data->file, "\t.rename .%s,\".%s\"\n", buffer, name);
29731 if (data->function_descriptor)
29732 fputs ("\t.lglobl .", data->file);
29733 else
29734 fputs ("\t.lglobl ", data->file);
29735 RS6000_OUTPUT_BASENAME (data->file, buffer);
29736 putc ('\n', data->file);
29738 if (data->function_descriptor)
29739 fputs (".", data->file);
29740 RS6000_OUTPUT_BASENAME (data->file, buffer);
29741 fputs (":\n", data->file);
29742 return false;
29745 /* This macro produces the initial definition of a function name.
29746 On the RS/6000, we need to place an extra '.' in the function name and
29747 output the function descriptor.
29748 Dollar signs are converted to underscores.
29750 The csect for the function will have already been created when
29751 text_section was selected. We do have to go back to that csect, however.
29753 The third and fourth parameters to the .function pseudo-op (16 and 044)
29754 are placeholders which no longer have any use.
29756 Because AIX assembler's .set command has unexpected semantics, we output
29757 all aliases as alternative labels in front of the definition. */
29759 void
29760 rs6000_xcoff_declare_function_name (FILE *file, const char *name, tree decl)
29762 char *buffer = (char *) alloca (strlen (name) + 1);
29763 char *p;
29764 int dollar_inside = 0;
29765 struct declare_alias_data data = {file, false};
29767 strcpy (buffer, name);
29768 p = strchr (buffer, '$');
29769 while (p) {
29770 *p = '_';
29771 dollar_inside++;
29772 p = strchr (p + 1, '$');
29774 if (TREE_PUBLIC (decl))
29776 if (!RS6000_WEAK || !DECL_WEAK (decl))
29778 if (dollar_inside) {
29779 fprintf(file, "\t.rename .%s,\".%s\"\n", buffer, name);
29780 fprintf(file, "\t.rename %s,\"%s\"\n", buffer, name);
29782 fputs ("\t.globl .", file);
29783 RS6000_OUTPUT_BASENAME (file, buffer);
29784 putc ('\n', file);
29787 else
29789 if (dollar_inside) {
29790 fprintf(file, "\t.rename .%s,\".%s\"\n", buffer, name);
29791 fprintf(file, "\t.rename %s,\"%s\"\n", buffer, name);
29793 fputs ("\t.lglobl .", file);
29794 RS6000_OUTPUT_BASENAME (file, buffer);
29795 putc ('\n', file);
29797 fputs ("\t.csect ", file);
29798 RS6000_OUTPUT_BASENAME (file, buffer);
29799 fputs (TARGET_32BIT ? "[DS]\n" : "[DS],3\n", file);
29800 RS6000_OUTPUT_BASENAME (file, buffer);
29801 fputs (":\n", file);
29802 symtab_node::get (decl)->call_for_symbol_and_aliases (rs6000_declare_alias, &data, true);
29803 fputs (TARGET_32BIT ? "\t.long ." : "\t.llong .", file);
29804 RS6000_OUTPUT_BASENAME (file, buffer);
29805 fputs (", TOC[tc0], 0\n", file);
29806 in_section = NULL;
29807 switch_to_section (function_section (decl));
29808 putc ('.', file);
29809 RS6000_OUTPUT_BASENAME (file, buffer);
29810 fputs (":\n", file);
29811 data.function_descriptor = true;
29812 symtab_node::get (decl)->call_for_symbol_and_aliases (rs6000_declare_alias, &data, true);
29813 if (write_symbols != NO_DEBUG && !DECL_IGNORED_P (decl))
29814 xcoffout_declare_function (file, decl, buffer);
29815 return;
29818 /* This macro produces the initial definition of a object (variable) name.
29819 Because AIX assembler's .set command has unexpected semantics, we output
29820 all aliases as alternative labels in front of the definition. */
29822 void
29823 rs6000_xcoff_declare_object_name (FILE *file, const char *name, tree decl)
29825 struct declare_alias_data data = {file, false};
29826 RS6000_OUTPUT_BASENAME (file, name);
29827 fputs (":\n", file);
29828 symtab_node::get (decl)->call_for_symbol_and_aliases (rs6000_declare_alias, &data, true);
29831 #ifdef HAVE_AS_TLS
29832 static void
29833 rs6000_xcoff_encode_section_info (tree decl, rtx rtl, int first)
29835 rtx symbol;
29836 int flags;
29838 default_encode_section_info (decl, rtl, first);
29840 /* Careful not to prod global register variables. */
29841 if (!MEM_P (rtl))
29842 return;
29843 symbol = XEXP (rtl, 0);
29844 if (GET_CODE (symbol) != SYMBOL_REF)
29845 return;
29847 flags = SYMBOL_REF_FLAGS (symbol);
29849 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
29850 flags &= ~SYMBOL_FLAG_HAS_BLOCK_INFO;
29852 SYMBOL_REF_FLAGS (symbol) = flags;
29854 #endif /* HAVE_AS_TLS */
29855 #endif /* TARGET_XCOFF */
29857 /* Compute a (partial) cost for rtx X. Return true if the complete
29858 cost has been computed, and false if subexpressions should be
29859 scanned. In either case, *TOTAL contains the cost result. */
29861 static bool
29862 rs6000_rtx_costs (rtx x, int code, int outer_code, int opno ATTRIBUTE_UNUSED,
29863 int *total, bool speed)
29865 enum machine_mode mode = GET_MODE (x);
29867 switch (code)
29869 /* On the RS/6000, if it is valid in the insn, it is free. */
29870 case CONST_INT:
29871 if (((outer_code == SET
29872 || outer_code == PLUS
29873 || outer_code == MINUS)
29874 && (satisfies_constraint_I (x)
29875 || satisfies_constraint_L (x)))
29876 || (outer_code == AND
29877 && (satisfies_constraint_K (x)
29878 || (mode == SImode
29879 ? satisfies_constraint_L (x)
29880 : satisfies_constraint_J (x))
29881 || mask_operand (x, mode)
29882 || (mode == DImode
29883 && mask64_operand (x, DImode))))
29884 || ((outer_code == IOR || outer_code == XOR)
29885 && (satisfies_constraint_K (x)
29886 || (mode == SImode
29887 ? satisfies_constraint_L (x)
29888 : satisfies_constraint_J (x))))
29889 || outer_code == ASHIFT
29890 || outer_code == ASHIFTRT
29891 || outer_code == LSHIFTRT
29892 || outer_code == ROTATE
29893 || outer_code == ROTATERT
29894 || outer_code == ZERO_EXTRACT
29895 || (outer_code == MULT
29896 && satisfies_constraint_I (x))
29897 || ((outer_code == DIV || outer_code == UDIV
29898 || outer_code == MOD || outer_code == UMOD)
29899 && exact_log2 (INTVAL (x)) >= 0)
29900 || (outer_code == COMPARE
29901 && (satisfies_constraint_I (x)
29902 || satisfies_constraint_K (x)))
29903 || ((outer_code == EQ || outer_code == NE)
29904 && (satisfies_constraint_I (x)
29905 || satisfies_constraint_K (x)
29906 || (mode == SImode
29907 ? satisfies_constraint_L (x)
29908 : satisfies_constraint_J (x))))
29909 || (outer_code == GTU
29910 && satisfies_constraint_I (x))
29911 || (outer_code == LTU
29912 && satisfies_constraint_P (x)))
29914 *total = 0;
29915 return true;
29917 else if ((outer_code == PLUS
29918 && reg_or_add_cint_operand (x, VOIDmode))
29919 || (outer_code == MINUS
29920 && reg_or_sub_cint_operand (x, VOIDmode))
29921 || ((outer_code == SET
29922 || outer_code == IOR
29923 || outer_code == XOR)
29924 && (INTVAL (x)
29925 & ~ (unsigned HOST_WIDE_INT) 0xffffffff) == 0))
29927 *total = COSTS_N_INSNS (1);
29928 return true;
29930 /* FALLTHRU */
29932 case CONST_DOUBLE:
29933 case CONST_WIDE_INT:
29934 case CONST:
29935 case HIGH:
29936 case SYMBOL_REF:
29937 case MEM:
29938 /* When optimizing for size, MEM should be slightly more expensive
29939 than generating address, e.g., (plus (reg) (const)).
29940 L1 cache latency is about two instructions. */
29941 *total = !speed ? COSTS_N_INSNS (1) + 1 : COSTS_N_INSNS (2);
29942 return true;
29944 case LABEL_REF:
29945 *total = 0;
29946 return true;
29948 case PLUS:
29949 case MINUS:
29950 if (FLOAT_MODE_P (mode))
29951 *total = rs6000_cost->fp;
29952 else
29953 *total = COSTS_N_INSNS (1);
29954 return false;
29956 case MULT:
29957 if (GET_CODE (XEXP (x, 1)) == CONST_INT
29958 && satisfies_constraint_I (XEXP (x, 1)))
29960 if (INTVAL (XEXP (x, 1)) >= -256
29961 && INTVAL (XEXP (x, 1)) <= 255)
29962 *total = rs6000_cost->mulsi_const9;
29963 else
29964 *total = rs6000_cost->mulsi_const;
29966 else if (mode == SFmode)
29967 *total = rs6000_cost->fp;
29968 else if (FLOAT_MODE_P (mode))
29969 *total = rs6000_cost->dmul;
29970 else if (mode == DImode)
29971 *total = rs6000_cost->muldi;
29972 else
29973 *total = rs6000_cost->mulsi;
29974 return false;
29976 case FMA:
29977 if (mode == SFmode)
29978 *total = rs6000_cost->fp;
29979 else
29980 *total = rs6000_cost->dmul;
29981 break;
29983 case DIV:
29984 case MOD:
29985 if (FLOAT_MODE_P (mode))
29987 *total = mode == DFmode ? rs6000_cost->ddiv
29988 : rs6000_cost->sdiv;
29989 return false;
29991 /* FALLTHRU */
29993 case UDIV:
29994 case UMOD:
29995 if (GET_CODE (XEXP (x, 1)) == CONST_INT
29996 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0)
29998 if (code == DIV || code == MOD)
29999 /* Shift, addze */
30000 *total = COSTS_N_INSNS (2);
30001 else
30002 /* Shift */
30003 *total = COSTS_N_INSNS (1);
30005 else
30007 if (GET_MODE (XEXP (x, 1)) == DImode)
30008 *total = rs6000_cost->divdi;
30009 else
30010 *total = rs6000_cost->divsi;
30012 /* Add in shift and subtract for MOD. */
30013 if (code == MOD || code == UMOD)
30014 *total += COSTS_N_INSNS (2);
30015 return false;
30017 case CTZ:
30018 case FFS:
30019 *total = COSTS_N_INSNS (4);
30020 return false;
30022 case POPCOUNT:
30023 *total = COSTS_N_INSNS (TARGET_POPCNTD ? 1 : 6);
30024 return false;
30026 case PARITY:
30027 *total = COSTS_N_INSNS (TARGET_CMPB ? 2 : 6);
30028 return false;
30030 case NOT:
30031 if (outer_code == AND || outer_code == IOR || outer_code == XOR)
30033 *total = 0;
30034 return false;
30036 /* FALLTHRU */
30038 case AND:
30039 case CLZ:
30040 case IOR:
30041 case XOR:
30042 case ZERO_EXTRACT:
30043 *total = COSTS_N_INSNS (1);
30044 return false;
30046 case ASHIFT:
30047 case ASHIFTRT:
30048 case LSHIFTRT:
30049 case ROTATE:
30050 case ROTATERT:
30051 /* Handle mul_highpart. */
30052 if (outer_code == TRUNCATE
30053 && GET_CODE (XEXP (x, 0)) == MULT)
30055 if (mode == DImode)
30056 *total = rs6000_cost->muldi;
30057 else
30058 *total = rs6000_cost->mulsi;
30059 return true;
30061 else if (outer_code == AND)
30062 *total = 0;
30063 else
30064 *total = COSTS_N_INSNS (1);
30065 return false;
30067 case SIGN_EXTEND:
30068 case ZERO_EXTEND:
30069 if (GET_CODE (XEXP (x, 0)) == MEM)
30070 *total = 0;
30071 else
30072 *total = COSTS_N_INSNS (1);
30073 return false;
30075 case COMPARE:
30076 case NEG:
30077 case ABS:
30078 if (!FLOAT_MODE_P (mode))
30080 *total = COSTS_N_INSNS (1);
30081 return false;
30083 /* FALLTHRU */
30085 case FLOAT:
30086 case UNSIGNED_FLOAT:
30087 case FIX:
30088 case UNSIGNED_FIX:
30089 case FLOAT_TRUNCATE:
30090 *total = rs6000_cost->fp;
30091 return false;
30093 case FLOAT_EXTEND:
30094 if (mode == DFmode)
30095 *total = 0;
30096 else
30097 *total = rs6000_cost->fp;
30098 return false;
30100 case UNSPEC:
30101 switch (XINT (x, 1))
30103 case UNSPEC_FRSP:
30104 *total = rs6000_cost->fp;
30105 return true;
30107 default:
30108 break;
30110 break;
30112 case CALL:
30113 case IF_THEN_ELSE:
30114 if (!speed)
30116 *total = COSTS_N_INSNS (1);
30117 return true;
30119 else if (FLOAT_MODE_P (mode)
30120 && TARGET_PPC_GFXOPT && TARGET_HARD_FLOAT && TARGET_FPRS)
30122 *total = rs6000_cost->fp;
30123 return false;
30125 break;
30127 case NE:
30128 case EQ:
30129 case GTU:
30130 case LTU:
30131 /* Carry bit requires mode == Pmode.
30132 NEG or PLUS already counted so only add one. */
30133 if (mode == Pmode
30134 && (outer_code == NEG || outer_code == PLUS))
30136 *total = COSTS_N_INSNS (1);
30137 return true;
30139 if (outer_code == SET)
30141 if (XEXP (x, 1) == const0_rtx)
30143 if (TARGET_ISEL && !TARGET_MFCRF)
30144 *total = COSTS_N_INSNS (8);
30145 else
30146 *total = COSTS_N_INSNS (2);
30147 return true;
30149 else if (mode == Pmode)
30151 *total = COSTS_N_INSNS (3);
30152 return false;
30155 /* FALLTHRU */
30157 case GT:
30158 case LT:
30159 case UNORDERED:
30160 if (outer_code == SET && (XEXP (x, 1) == const0_rtx))
30162 if (TARGET_ISEL && !TARGET_MFCRF)
30163 *total = COSTS_N_INSNS (8);
30164 else
30165 *total = COSTS_N_INSNS (2);
30166 return true;
30168 /* CC COMPARE. */
30169 if (outer_code == COMPARE)
30171 *total = 0;
30172 return true;
30174 break;
30176 default:
30177 break;
30180 return false;
30183 /* Debug form of r6000_rtx_costs that is selected if -mdebug=cost. */
30185 static bool
30186 rs6000_debug_rtx_costs (rtx x, int code, int outer_code, int opno, int *total,
30187 bool speed)
30189 bool ret = rs6000_rtx_costs (x, code, outer_code, opno, total, speed);
30191 fprintf (stderr,
30192 "\nrs6000_rtx_costs, return = %s, code = %s, outer_code = %s, "
30193 "opno = %d, total = %d, speed = %s, x:\n",
30194 ret ? "complete" : "scan inner",
30195 GET_RTX_NAME (code),
30196 GET_RTX_NAME (outer_code),
30197 opno,
30198 *total,
30199 speed ? "true" : "false");
30201 debug_rtx (x);
30203 return ret;
30206 /* Debug form of ADDRESS_COST that is selected if -mdebug=cost. */
30208 static int
30209 rs6000_debug_address_cost (rtx x, enum machine_mode mode,
30210 addr_space_t as, bool speed)
30212 int ret = TARGET_ADDRESS_COST (x, mode, as, speed);
30214 fprintf (stderr, "\nrs6000_address_cost, return = %d, speed = %s, x:\n",
30215 ret, speed ? "true" : "false");
30216 debug_rtx (x);
30218 return ret;
30222 /* A C expression returning the cost of moving data from a register of class
30223 CLASS1 to one of CLASS2. */
30225 static int
30226 rs6000_register_move_cost (enum machine_mode mode,
30227 reg_class_t from, reg_class_t to)
30229 int ret;
30231 if (TARGET_DEBUG_COST)
30232 dbg_cost_ctrl++;
30234 /* Moves from/to GENERAL_REGS. */
30235 if (reg_classes_intersect_p (to, GENERAL_REGS)
30236 || reg_classes_intersect_p (from, GENERAL_REGS))
30238 reg_class_t rclass = from;
30240 if (! reg_classes_intersect_p (to, GENERAL_REGS))
30241 rclass = to;
30243 if (rclass == FLOAT_REGS || rclass == ALTIVEC_REGS || rclass == VSX_REGS)
30244 ret = (rs6000_memory_move_cost (mode, rclass, false)
30245 + rs6000_memory_move_cost (mode, GENERAL_REGS, false));
30247 /* It's more expensive to move CR_REGS than CR0_REGS because of the
30248 shift. */
30249 else if (rclass == CR_REGS)
30250 ret = 4;
30252 /* For those processors that have slow LR/CTR moves, make them more
30253 expensive than memory in order to bias spills to memory .*/
30254 else if ((rs6000_cpu == PROCESSOR_POWER6
30255 || rs6000_cpu == PROCESSOR_POWER7
30256 || rs6000_cpu == PROCESSOR_POWER8)
30257 && reg_classes_intersect_p (rclass, LINK_OR_CTR_REGS))
30258 ret = 6 * hard_regno_nregs[0][mode];
30260 else
30261 /* A move will cost one instruction per GPR moved. */
30262 ret = 2 * hard_regno_nregs[0][mode];
30265 /* If we have VSX, we can easily move between FPR or Altivec registers. */
30266 else if (VECTOR_MEM_VSX_P (mode)
30267 && reg_classes_intersect_p (to, VSX_REGS)
30268 && reg_classes_intersect_p (from, VSX_REGS))
30269 ret = 2 * hard_regno_nregs[32][mode];
30271 /* Moving between two similar registers is just one instruction. */
30272 else if (reg_classes_intersect_p (to, from))
30273 ret = (mode == TFmode || mode == TDmode) ? 4 : 2;
30275 /* Everything else has to go through GENERAL_REGS. */
30276 else
30277 ret = (rs6000_register_move_cost (mode, GENERAL_REGS, to)
30278 + rs6000_register_move_cost (mode, from, GENERAL_REGS));
30280 if (TARGET_DEBUG_COST)
30282 if (dbg_cost_ctrl == 1)
30283 fprintf (stderr,
30284 "rs6000_register_move_cost:, ret=%d, mode=%s, from=%s, to=%s\n",
30285 ret, GET_MODE_NAME (mode), reg_class_names[from],
30286 reg_class_names[to]);
30287 dbg_cost_ctrl--;
30290 return ret;
30293 /* A C expressions returning the cost of moving data of MODE from a register to
30294 or from memory. */
30296 static int
30297 rs6000_memory_move_cost (enum machine_mode mode, reg_class_t rclass,
30298 bool in ATTRIBUTE_UNUSED)
30300 int ret;
30302 if (TARGET_DEBUG_COST)
30303 dbg_cost_ctrl++;
30305 if (reg_classes_intersect_p (rclass, GENERAL_REGS))
30306 ret = 4 * hard_regno_nregs[0][mode];
30307 else if ((reg_classes_intersect_p (rclass, FLOAT_REGS)
30308 || reg_classes_intersect_p (rclass, VSX_REGS)))
30309 ret = 4 * hard_regno_nregs[32][mode];
30310 else if (reg_classes_intersect_p (rclass, ALTIVEC_REGS))
30311 ret = 4 * hard_regno_nregs[FIRST_ALTIVEC_REGNO][mode];
30312 else
30313 ret = 4 + rs6000_register_move_cost (mode, rclass, GENERAL_REGS);
30315 if (TARGET_DEBUG_COST)
30317 if (dbg_cost_ctrl == 1)
30318 fprintf (stderr,
30319 "rs6000_memory_move_cost: ret=%d, mode=%s, rclass=%s, in=%d\n",
30320 ret, GET_MODE_NAME (mode), reg_class_names[rclass], in);
30321 dbg_cost_ctrl--;
30324 return ret;
30327 /* Returns a code for a target-specific builtin that implements
30328 reciprocal of the function, or NULL_TREE if not available. */
30330 static tree
30331 rs6000_builtin_reciprocal (unsigned int fn, bool md_fn,
30332 bool sqrt ATTRIBUTE_UNUSED)
30334 if (optimize_insn_for_size_p ())
30335 return NULL_TREE;
30337 if (md_fn)
30338 switch (fn)
30340 case VSX_BUILTIN_XVSQRTDP:
30341 if (!RS6000_RECIP_AUTO_RSQRTE_P (V2DFmode))
30342 return NULL_TREE;
30344 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_2DF];
30346 case VSX_BUILTIN_XVSQRTSP:
30347 if (!RS6000_RECIP_AUTO_RSQRTE_P (V4SFmode))
30348 return NULL_TREE;
30350 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_4SF];
30352 default:
30353 return NULL_TREE;
30356 else
30357 switch (fn)
30359 case BUILT_IN_SQRT:
30360 if (!RS6000_RECIP_AUTO_RSQRTE_P (DFmode))
30361 return NULL_TREE;
30363 return rs6000_builtin_decls[RS6000_BUILTIN_RSQRT];
30365 case BUILT_IN_SQRTF:
30366 if (!RS6000_RECIP_AUTO_RSQRTE_P (SFmode))
30367 return NULL_TREE;
30369 return rs6000_builtin_decls[RS6000_BUILTIN_RSQRTF];
30371 default:
30372 return NULL_TREE;
30376 /* Load up a constant. If the mode is a vector mode, splat the value across
30377 all of the vector elements. */
30379 static rtx
30380 rs6000_load_constant_and_splat (enum machine_mode mode, REAL_VALUE_TYPE dconst)
30382 rtx reg;
30384 if (mode == SFmode || mode == DFmode)
30386 rtx d = CONST_DOUBLE_FROM_REAL_VALUE (dconst, mode);
30387 reg = force_reg (mode, d);
30389 else if (mode == V4SFmode)
30391 rtx d = CONST_DOUBLE_FROM_REAL_VALUE (dconst, SFmode);
30392 rtvec v = gen_rtvec (4, d, d, d, d);
30393 reg = gen_reg_rtx (mode);
30394 rs6000_expand_vector_init (reg, gen_rtx_PARALLEL (mode, v));
30396 else if (mode == V2DFmode)
30398 rtx d = CONST_DOUBLE_FROM_REAL_VALUE (dconst, DFmode);
30399 rtvec v = gen_rtvec (2, d, d);
30400 reg = gen_reg_rtx (mode);
30401 rs6000_expand_vector_init (reg, gen_rtx_PARALLEL (mode, v));
30403 else
30404 gcc_unreachable ();
30406 return reg;
30409 /* Generate an FMA instruction. */
30411 static void
30412 rs6000_emit_madd (rtx target, rtx m1, rtx m2, rtx a)
30414 enum machine_mode mode = GET_MODE (target);
30415 rtx dst;
30417 dst = expand_ternary_op (mode, fma_optab, m1, m2, a, target, 0);
30418 gcc_assert (dst != NULL);
30420 if (dst != target)
30421 emit_move_insn (target, dst);
30424 /* Generate a FMSUB instruction: dst = fma(m1, m2, -a). */
30426 static void
30427 rs6000_emit_msub (rtx target, rtx m1, rtx m2, rtx a)
30429 enum machine_mode mode = GET_MODE (target);
30430 rtx dst;
30432 /* Altivec does not support fms directly;
30433 generate in terms of fma in that case. */
30434 if (optab_handler (fms_optab, mode) != CODE_FOR_nothing)
30435 dst = expand_ternary_op (mode, fms_optab, m1, m2, a, target, 0);
30436 else
30438 a = expand_unop (mode, neg_optab, a, NULL_RTX, 0);
30439 dst = expand_ternary_op (mode, fma_optab, m1, m2, a, target, 0);
30441 gcc_assert (dst != NULL);
30443 if (dst != target)
30444 emit_move_insn (target, dst);
30447 /* Generate a FNMSUB instruction: dst = -fma(m1, m2, -a). */
30449 static void
30450 rs6000_emit_nmsub (rtx dst, rtx m1, rtx m2, rtx a)
30452 enum machine_mode mode = GET_MODE (dst);
30453 rtx r;
30455 /* This is a tad more complicated, since the fnma_optab is for
30456 a different expression: fma(-m1, m2, a), which is the same
30457 thing except in the case of signed zeros.
30459 Fortunately we know that if FMA is supported that FNMSUB is
30460 also supported in the ISA. Just expand it directly. */
30462 gcc_assert (optab_handler (fma_optab, mode) != CODE_FOR_nothing);
30464 r = gen_rtx_NEG (mode, a);
30465 r = gen_rtx_FMA (mode, m1, m2, r);
30466 r = gen_rtx_NEG (mode, r);
30467 emit_insn (gen_rtx_SET (VOIDmode, dst, r));
30470 /* Newton-Raphson approximation of floating point divide DST = N/D. If NOTE_P,
30471 add a reg_note saying that this was a division. Support both scalar and
30472 vector divide. Assumes no trapping math and finite arguments. */
30474 void
30475 rs6000_emit_swdiv (rtx dst, rtx n, rtx d, bool note_p)
30477 enum machine_mode mode = GET_MODE (dst);
30478 rtx one, x0, e0, x1, xprev, eprev, xnext, enext, u, v;
30479 int i;
30481 /* Low precision estimates guarantee 5 bits of accuracy. High
30482 precision estimates guarantee 14 bits of accuracy. SFmode
30483 requires 23 bits of accuracy. DFmode requires 52 bits of
30484 accuracy. Each pass at least doubles the accuracy, leading
30485 to the following. */
30486 int passes = (TARGET_RECIP_PRECISION) ? 1 : 3;
30487 if (mode == DFmode || mode == V2DFmode)
30488 passes++;
30490 enum insn_code code = optab_handler (smul_optab, mode);
30491 insn_gen_fn gen_mul = GEN_FCN (code);
30493 gcc_assert (code != CODE_FOR_nothing);
30495 one = rs6000_load_constant_and_splat (mode, dconst1);
30497 /* x0 = 1./d estimate */
30498 x0 = gen_reg_rtx (mode);
30499 emit_insn (gen_rtx_SET (VOIDmode, x0,
30500 gen_rtx_UNSPEC (mode, gen_rtvec (1, d),
30501 UNSPEC_FRES)));
30503 /* Each iteration but the last calculates x_(i+1) = x_i * (2 - d * x_i). */
30504 if (passes > 1) {
30506 /* e0 = 1. - d * x0 */
30507 e0 = gen_reg_rtx (mode);
30508 rs6000_emit_nmsub (e0, d, x0, one);
30510 /* x1 = x0 + e0 * x0 */
30511 x1 = gen_reg_rtx (mode);
30512 rs6000_emit_madd (x1, e0, x0, x0);
30514 for (i = 0, xprev = x1, eprev = e0; i < passes - 2;
30515 ++i, xprev = xnext, eprev = enext) {
30517 /* enext = eprev * eprev */
30518 enext = gen_reg_rtx (mode);
30519 emit_insn (gen_mul (enext, eprev, eprev));
30521 /* xnext = xprev + enext * xprev */
30522 xnext = gen_reg_rtx (mode);
30523 rs6000_emit_madd (xnext, enext, xprev, xprev);
30526 } else
30527 xprev = x0;
30529 /* The last iteration calculates x_(i+1) = n * x_i * (2 - d * x_i). */
30531 /* u = n * xprev */
30532 u = gen_reg_rtx (mode);
30533 emit_insn (gen_mul (u, n, xprev));
30535 /* v = n - (d * u) */
30536 v = gen_reg_rtx (mode);
30537 rs6000_emit_nmsub (v, d, u, n);
30539 /* dst = (v * xprev) + u */
30540 rs6000_emit_madd (dst, v, xprev, u);
30542 if (note_p)
30543 add_reg_note (get_last_insn (), REG_EQUAL, gen_rtx_DIV (mode, n, d));
30546 /* Newton-Raphson approximation of single/double-precision floating point
30547 rsqrt. Assumes no trapping math and finite arguments. */
30549 void
30550 rs6000_emit_swrsqrt (rtx dst, rtx src)
30552 enum machine_mode mode = GET_MODE (src);
30553 rtx x0 = gen_reg_rtx (mode);
30554 rtx y = gen_reg_rtx (mode);
30556 /* Low precision estimates guarantee 5 bits of accuracy. High
30557 precision estimates guarantee 14 bits of accuracy. SFmode
30558 requires 23 bits of accuracy. DFmode requires 52 bits of
30559 accuracy. Each pass at least doubles the accuracy, leading
30560 to the following. */
30561 int passes = (TARGET_RECIP_PRECISION) ? 1 : 3;
30562 if (mode == DFmode || mode == V2DFmode)
30563 passes++;
30565 REAL_VALUE_TYPE dconst3_2;
30566 int i;
30567 rtx halfthree;
30568 enum insn_code code = optab_handler (smul_optab, mode);
30569 insn_gen_fn gen_mul = GEN_FCN (code);
30571 gcc_assert (code != CODE_FOR_nothing);
30573 /* Load up the constant 1.5 either as a scalar, or as a vector. */
30574 real_from_integer (&dconst3_2, VOIDmode, 3, SIGNED);
30575 SET_REAL_EXP (&dconst3_2, REAL_EXP (&dconst3_2) - 1);
30577 halfthree = rs6000_load_constant_and_splat (mode, dconst3_2);
30579 /* x0 = rsqrt estimate */
30580 emit_insn (gen_rtx_SET (VOIDmode, x0,
30581 gen_rtx_UNSPEC (mode, gen_rtvec (1, src),
30582 UNSPEC_RSQRT)));
30584 /* y = 0.5 * src = 1.5 * src - src -> fewer constants */
30585 rs6000_emit_msub (y, src, halfthree, src);
30587 for (i = 0; i < passes; i++)
30589 rtx x1 = gen_reg_rtx (mode);
30590 rtx u = gen_reg_rtx (mode);
30591 rtx v = gen_reg_rtx (mode);
30593 /* x1 = x0 * (1.5 - y * (x0 * x0)) */
30594 emit_insn (gen_mul (u, x0, x0));
30595 rs6000_emit_nmsub (v, y, u, halfthree);
30596 emit_insn (gen_mul (x1, x0, v));
30597 x0 = x1;
30600 emit_move_insn (dst, x0);
30601 return;
30604 /* Emit popcount intrinsic on TARGET_POPCNTB (Power5) and TARGET_POPCNTD
30605 (Power7) targets. DST is the target, and SRC is the argument operand. */
30607 void
30608 rs6000_emit_popcount (rtx dst, rtx src)
30610 enum machine_mode mode = GET_MODE (dst);
30611 rtx tmp1, tmp2;
30613 /* Use the PPC ISA 2.06 popcnt{w,d} instruction if we can. */
30614 if (TARGET_POPCNTD)
30616 if (mode == SImode)
30617 emit_insn (gen_popcntdsi2 (dst, src));
30618 else
30619 emit_insn (gen_popcntddi2 (dst, src));
30620 return;
30623 tmp1 = gen_reg_rtx (mode);
30625 if (mode == SImode)
30627 emit_insn (gen_popcntbsi2 (tmp1, src));
30628 tmp2 = expand_mult (SImode, tmp1, GEN_INT (0x01010101),
30629 NULL_RTX, 0);
30630 tmp2 = force_reg (SImode, tmp2);
30631 emit_insn (gen_lshrsi3 (dst, tmp2, GEN_INT (24)));
30633 else
30635 emit_insn (gen_popcntbdi2 (tmp1, src));
30636 tmp2 = expand_mult (DImode, tmp1,
30637 GEN_INT ((HOST_WIDE_INT)
30638 0x01010101 << 32 | 0x01010101),
30639 NULL_RTX, 0);
30640 tmp2 = force_reg (DImode, tmp2);
30641 emit_insn (gen_lshrdi3 (dst, tmp2, GEN_INT (56)));
30646 /* Emit parity intrinsic on TARGET_POPCNTB targets. DST is the
30647 target, and SRC is the argument operand. */
30649 void
30650 rs6000_emit_parity (rtx dst, rtx src)
30652 enum machine_mode mode = GET_MODE (dst);
30653 rtx tmp;
30655 tmp = gen_reg_rtx (mode);
30657 /* Use the PPC ISA 2.05 prtyw/prtyd instruction if we can. */
30658 if (TARGET_CMPB)
30660 if (mode == SImode)
30662 emit_insn (gen_popcntbsi2 (tmp, src));
30663 emit_insn (gen_paritysi2_cmpb (dst, tmp));
30665 else
30667 emit_insn (gen_popcntbdi2 (tmp, src));
30668 emit_insn (gen_paritydi2_cmpb (dst, tmp));
30670 return;
30673 if (mode == SImode)
30675 /* Is mult+shift >= shift+xor+shift+xor? */
30676 if (rs6000_cost->mulsi_const >= COSTS_N_INSNS (3))
30678 rtx tmp1, tmp2, tmp3, tmp4;
30680 tmp1 = gen_reg_rtx (SImode);
30681 emit_insn (gen_popcntbsi2 (tmp1, src));
30683 tmp2 = gen_reg_rtx (SImode);
30684 emit_insn (gen_lshrsi3 (tmp2, tmp1, GEN_INT (16)));
30685 tmp3 = gen_reg_rtx (SImode);
30686 emit_insn (gen_xorsi3 (tmp3, tmp1, tmp2));
30688 tmp4 = gen_reg_rtx (SImode);
30689 emit_insn (gen_lshrsi3 (tmp4, tmp3, GEN_INT (8)));
30690 emit_insn (gen_xorsi3 (tmp, tmp3, tmp4));
30692 else
30693 rs6000_emit_popcount (tmp, src);
30694 emit_insn (gen_andsi3 (dst, tmp, const1_rtx));
30696 else
30698 /* Is mult+shift >= shift+xor+shift+xor+shift+xor? */
30699 if (rs6000_cost->muldi >= COSTS_N_INSNS (5))
30701 rtx tmp1, tmp2, tmp3, tmp4, tmp5, tmp6;
30703 tmp1 = gen_reg_rtx (DImode);
30704 emit_insn (gen_popcntbdi2 (tmp1, src));
30706 tmp2 = gen_reg_rtx (DImode);
30707 emit_insn (gen_lshrdi3 (tmp2, tmp1, GEN_INT (32)));
30708 tmp3 = gen_reg_rtx (DImode);
30709 emit_insn (gen_xordi3 (tmp3, tmp1, tmp2));
30711 tmp4 = gen_reg_rtx (DImode);
30712 emit_insn (gen_lshrdi3 (tmp4, tmp3, GEN_INT (16)));
30713 tmp5 = gen_reg_rtx (DImode);
30714 emit_insn (gen_xordi3 (tmp5, tmp3, tmp4));
30716 tmp6 = gen_reg_rtx (DImode);
30717 emit_insn (gen_lshrdi3 (tmp6, tmp5, GEN_INT (8)));
30718 emit_insn (gen_xordi3 (tmp, tmp5, tmp6));
30720 else
30721 rs6000_emit_popcount (tmp, src);
30722 emit_insn (gen_anddi3 (dst, tmp, const1_rtx));
30726 /* Expand an Altivec constant permutation for little endian mode.
30727 There are two issues: First, the two input operands must be
30728 swapped so that together they form a double-wide array in LE
30729 order. Second, the vperm instruction has surprising behavior
30730 in LE mode: it interprets the elements of the source vectors
30731 in BE mode ("left to right") and interprets the elements of
30732 the destination vector in LE mode ("right to left"). To
30733 correct for this, we must subtract each element of the permute
30734 control vector from 31.
30736 For example, suppose we want to concatenate vr10 = {0, 1, 2, 3}
30737 with vr11 = {4, 5, 6, 7} and extract {0, 2, 4, 6} using a vperm.
30738 We place {0,1,2,3,8,9,10,11,16,17,18,19,24,25,26,27} in vr12 to
30739 serve as the permute control vector. Then, in BE mode,
30741 vperm 9,10,11,12
30743 places the desired result in vr9. However, in LE mode the
30744 vector contents will be
30746 vr10 = 00000003 00000002 00000001 00000000
30747 vr11 = 00000007 00000006 00000005 00000004
30749 The result of the vperm using the same permute control vector is
30751 vr9 = 05000000 07000000 01000000 03000000
30753 That is, the leftmost 4 bytes of vr10 are interpreted as the
30754 source for the rightmost 4 bytes of vr9, and so on.
30756 If we change the permute control vector to
30758 vr12 = {31,20,29,28,23,22,21,20,15,14,13,12,7,6,5,4}
30760 and issue
30762 vperm 9,11,10,12
30764 we get the desired
30766 vr9 = 00000006 00000004 00000002 00000000. */
30768 void
30769 altivec_expand_vec_perm_const_le (rtx operands[4])
30771 unsigned int i;
30772 rtx perm[16];
30773 rtx constv, unspec;
30774 rtx target = operands[0];
30775 rtx op0 = operands[1];
30776 rtx op1 = operands[2];
30777 rtx sel = operands[3];
30779 /* Unpack and adjust the constant selector. */
30780 for (i = 0; i < 16; ++i)
30782 rtx e = XVECEXP (sel, 0, i);
30783 unsigned int elt = 31 - (INTVAL (e) & 31);
30784 perm[i] = GEN_INT (elt);
30787 /* Expand to a permute, swapping the inputs and using the
30788 adjusted selector. */
30789 if (!REG_P (op0))
30790 op0 = force_reg (V16QImode, op0);
30791 if (!REG_P (op1))
30792 op1 = force_reg (V16QImode, op1);
30794 constv = gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, perm));
30795 constv = force_reg (V16QImode, constv);
30796 unspec = gen_rtx_UNSPEC (V16QImode, gen_rtvec (3, op1, op0, constv),
30797 UNSPEC_VPERM);
30798 if (!REG_P (target))
30800 rtx tmp = gen_reg_rtx (V16QImode);
30801 emit_move_insn (tmp, unspec);
30802 unspec = tmp;
30805 emit_move_insn (target, unspec);
30808 /* Similarly to altivec_expand_vec_perm_const_le, we must adjust the
30809 permute control vector. But here it's not a constant, so we must
30810 generate a vector NAND or NOR to do the adjustment. */
30812 void
30813 altivec_expand_vec_perm_le (rtx operands[4])
30815 rtx notx, iorx, unspec;
30816 rtx target = operands[0];
30817 rtx op0 = operands[1];
30818 rtx op1 = operands[2];
30819 rtx sel = operands[3];
30820 rtx tmp = target;
30821 rtx norreg = gen_reg_rtx (V16QImode);
30822 enum machine_mode mode = GET_MODE (target);
30824 /* Get everything in regs so the pattern matches. */
30825 if (!REG_P (op0))
30826 op0 = force_reg (mode, op0);
30827 if (!REG_P (op1))
30828 op1 = force_reg (mode, op1);
30829 if (!REG_P (sel))
30830 sel = force_reg (V16QImode, sel);
30831 if (!REG_P (target))
30832 tmp = gen_reg_rtx (mode);
30834 /* Invert the selector with a VNAND if available, else a VNOR.
30835 The VNAND is preferred for future fusion opportunities. */
30836 notx = gen_rtx_NOT (V16QImode, sel);
30837 iorx = (TARGET_P8_VECTOR
30838 ? gen_rtx_IOR (V16QImode, notx, notx)
30839 : gen_rtx_AND (V16QImode, notx, notx));
30840 emit_insn (gen_rtx_SET (VOIDmode, norreg, iorx));
30842 /* Permute with operands reversed and adjusted selector. */
30843 unspec = gen_rtx_UNSPEC (mode, gen_rtvec (3, op1, op0, norreg),
30844 UNSPEC_VPERM);
30846 /* Copy into target, possibly by way of a register. */
30847 if (!REG_P (target))
30849 emit_move_insn (tmp, unspec);
30850 unspec = tmp;
30853 emit_move_insn (target, unspec);
30856 /* Expand an Altivec constant permutation. Return true if we match
30857 an efficient implementation; false to fall back to VPERM. */
30859 bool
30860 altivec_expand_vec_perm_const (rtx operands[4])
30862 struct altivec_perm_insn {
30863 HOST_WIDE_INT mask;
30864 enum insn_code impl;
30865 unsigned char perm[16];
30867 static const struct altivec_perm_insn patterns[] = {
30868 { OPTION_MASK_ALTIVEC, CODE_FOR_altivec_vpkuhum_direct,
30869 { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } },
30870 { OPTION_MASK_ALTIVEC, CODE_FOR_altivec_vpkuwum_direct,
30871 { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } },
30872 { OPTION_MASK_ALTIVEC,
30873 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrghb_direct
30874 : CODE_FOR_altivec_vmrglb_direct),
30875 { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 } },
30876 { OPTION_MASK_ALTIVEC,
30877 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrghh_direct
30878 : CODE_FOR_altivec_vmrglh_direct),
30879 { 0, 1, 16, 17, 2, 3, 18, 19, 4, 5, 20, 21, 6, 7, 22, 23 } },
30880 { OPTION_MASK_ALTIVEC,
30881 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrghw_direct
30882 : CODE_FOR_altivec_vmrglw_direct),
30883 { 0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23 } },
30884 { OPTION_MASK_ALTIVEC,
30885 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrglb_direct
30886 : CODE_FOR_altivec_vmrghb_direct),
30887 { 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } },
30888 { OPTION_MASK_ALTIVEC,
30889 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrglh_direct
30890 : CODE_FOR_altivec_vmrghh_direct),
30891 { 8, 9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } },
30892 { OPTION_MASK_ALTIVEC,
30893 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrglw_direct
30894 : CODE_FOR_altivec_vmrghw_direct),
30895 { 8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } },
30896 { OPTION_MASK_P8_VECTOR, CODE_FOR_p8_vmrgew,
30897 { 0, 1, 2, 3, 16, 17, 18, 19, 8, 9, 10, 11, 24, 25, 26, 27 } },
30898 { OPTION_MASK_P8_VECTOR, CODE_FOR_p8_vmrgow,
30899 { 4, 5, 6, 7, 20, 21, 22, 23, 12, 13, 14, 15, 28, 29, 30, 31 } }
30902 unsigned int i, j, elt, which;
30903 unsigned char perm[16];
30904 rtx target, op0, op1, sel, x;
30905 bool one_vec;
30907 target = operands[0];
30908 op0 = operands[1];
30909 op1 = operands[2];
30910 sel = operands[3];
30912 /* Unpack the constant selector. */
30913 for (i = which = 0; i < 16; ++i)
30915 rtx e = XVECEXP (sel, 0, i);
30916 elt = INTVAL (e) & 31;
30917 which |= (elt < 16 ? 1 : 2);
30918 perm[i] = elt;
30921 /* Simplify the constant selector based on operands. */
30922 switch (which)
30924 default:
30925 gcc_unreachable ();
30927 case 3:
30928 one_vec = false;
30929 if (!rtx_equal_p (op0, op1))
30930 break;
30931 /* FALLTHRU */
30933 case 2:
30934 for (i = 0; i < 16; ++i)
30935 perm[i] &= 15;
30936 op0 = op1;
30937 one_vec = true;
30938 break;
30940 case 1:
30941 op1 = op0;
30942 one_vec = true;
30943 break;
30946 /* Look for splat patterns. */
30947 if (one_vec)
30949 elt = perm[0];
30951 for (i = 0; i < 16; ++i)
30952 if (perm[i] != elt)
30953 break;
30954 if (i == 16)
30956 if (!BYTES_BIG_ENDIAN)
30957 elt = 15 - elt;
30958 emit_insn (gen_altivec_vspltb_direct (target, op0, GEN_INT (elt)));
30959 return true;
30962 if (elt % 2 == 0)
30964 for (i = 0; i < 16; i += 2)
30965 if (perm[i] != elt || perm[i + 1] != elt + 1)
30966 break;
30967 if (i == 16)
30969 int field = BYTES_BIG_ENDIAN ? elt / 2 : 7 - elt / 2;
30970 x = gen_reg_rtx (V8HImode);
30971 emit_insn (gen_altivec_vsplth_direct (x, gen_lowpart (V8HImode, op0),
30972 GEN_INT (field)));
30973 emit_move_insn (target, gen_lowpart (V16QImode, x));
30974 return true;
30978 if (elt % 4 == 0)
30980 for (i = 0; i < 16; i += 4)
30981 if (perm[i] != elt
30982 || perm[i + 1] != elt + 1
30983 || perm[i + 2] != elt + 2
30984 || perm[i + 3] != elt + 3)
30985 break;
30986 if (i == 16)
30988 int field = BYTES_BIG_ENDIAN ? elt / 4 : 3 - elt / 4;
30989 x = gen_reg_rtx (V4SImode);
30990 emit_insn (gen_altivec_vspltw_direct (x, gen_lowpart (V4SImode, op0),
30991 GEN_INT (field)));
30992 emit_move_insn (target, gen_lowpart (V16QImode, x));
30993 return true;
30998 /* Look for merge and pack patterns. */
30999 for (j = 0; j < ARRAY_SIZE (patterns); ++j)
31001 bool swapped;
31003 if ((patterns[j].mask & rs6000_isa_flags) == 0)
31004 continue;
31006 elt = patterns[j].perm[0];
31007 if (perm[0] == elt)
31008 swapped = false;
31009 else if (perm[0] == elt + 16)
31010 swapped = true;
31011 else
31012 continue;
31013 for (i = 1; i < 16; ++i)
31015 elt = patterns[j].perm[i];
31016 if (swapped)
31017 elt = (elt >= 16 ? elt - 16 : elt + 16);
31018 else if (one_vec && elt >= 16)
31019 elt -= 16;
31020 if (perm[i] != elt)
31021 break;
31023 if (i == 16)
31025 enum insn_code icode = patterns[j].impl;
31026 enum machine_mode omode = insn_data[icode].operand[0].mode;
31027 enum machine_mode imode = insn_data[icode].operand[1].mode;
31029 /* For little-endian, don't use vpkuwum and vpkuhum if the
31030 underlying vector type is not V4SI and V8HI, respectively.
31031 For example, using vpkuwum with a V8HI picks up the even
31032 halfwords (BE numbering) when the even halfwords (LE
31033 numbering) are what we need. */
31034 if (!BYTES_BIG_ENDIAN
31035 && icode == CODE_FOR_altivec_vpkuwum_direct
31036 && ((GET_CODE (op0) == REG
31037 && GET_MODE (op0) != V4SImode)
31038 || (GET_CODE (op0) == SUBREG
31039 && GET_MODE (XEXP (op0, 0)) != V4SImode)))
31040 continue;
31041 if (!BYTES_BIG_ENDIAN
31042 && icode == CODE_FOR_altivec_vpkuhum_direct
31043 && ((GET_CODE (op0) == REG
31044 && GET_MODE (op0) != V8HImode)
31045 || (GET_CODE (op0) == SUBREG
31046 && GET_MODE (XEXP (op0, 0)) != V8HImode)))
31047 continue;
31049 /* For little-endian, the two input operands must be swapped
31050 (or swapped back) to ensure proper right-to-left numbering
31051 from 0 to 2N-1. */
31052 if (swapped ^ !BYTES_BIG_ENDIAN)
31053 x = op0, op0 = op1, op1 = x;
31054 if (imode != V16QImode)
31056 op0 = gen_lowpart (imode, op0);
31057 op1 = gen_lowpart (imode, op1);
31059 if (omode == V16QImode)
31060 x = target;
31061 else
31062 x = gen_reg_rtx (omode);
31063 emit_insn (GEN_FCN (icode) (x, op0, op1));
31064 if (omode != V16QImode)
31065 emit_move_insn (target, gen_lowpart (V16QImode, x));
31066 return true;
31070 if (!BYTES_BIG_ENDIAN)
31072 altivec_expand_vec_perm_const_le (operands);
31073 return true;
31076 return false;
31079 /* Expand a Paired Single, VSX Permute Doubleword, or SPE constant permutation.
31080 Return true if we match an efficient implementation. */
31082 static bool
31083 rs6000_expand_vec_perm_const_1 (rtx target, rtx op0, rtx op1,
31084 unsigned char perm0, unsigned char perm1)
31086 rtx x;
31088 /* If both selectors come from the same operand, fold to single op. */
31089 if ((perm0 & 2) == (perm1 & 2))
31091 if (perm0 & 2)
31092 op0 = op1;
31093 else
31094 op1 = op0;
31096 /* If both operands are equal, fold to simpler permutation. */
31097 if (rtx_equal_p (op0, op1))
31099 perm0 = perm0 & 1;
31100 perm1 = (perm1 & 1) + 2;
31102 /* If the first selector comes from the second operand, swap. */
31103 else if (perm0 & 2)
31105 if (perm1 & 2)
31106 return false;
31107 perm0 -= 2;
31108 perm1 += 2;
31109 x = op0, op0 = op1, op1 = x;
31111 /* If the second selector does not come from the second operand, fail. */
31112 else if ((perm1 & 2) == 0)
31113 return false;
31115 /* Success! */
31116 if (target != NULL)
31118 enum machine_mode vmode, dmode;
31119 rtvec v;
31121 vmode = GET_MODE (target);
31122 gcc_assert (GET_MODE_NUNITS (vmode) == 2);
31123 dmode = mode_for_vector (GET_MODE_INNER (vmode), 4);
31124 x = gen_rtx_VEC_CONCAT (dmode, op0, op1);
31125 v = gen_rtvec (2, GEN_INT (perm0), GEN_INT (perm1));
31126 x = gen_rtx_VEC_SELECT (vmode, x, gen_rtx_PARALLEL (VOIDmode, v));
31127 emit_insn (gen_rtx_SET (VOIDmode, target, x));
31129 return true;
31132 bool
31133 rs6000_expand_vec_perm_const (rtx operands[4])
31135 rtx target, op0, op1, sel;
31136 unsigned char perm0, perm1;
31138 target = operands[0];
31139 op0 = operands[1];
31140 op1 = operands[2];
31141 sel = operands[3];
31143 /* Unpack the constant selector. */
31144 perm0 = INTVAL (XVECEXP (sel, 0, 0)) & 3;
31145 perm1 = INTVAL (XVECEXP (sel, 0, 1)) & 3;
31147 return rs6000_expand_vec_perm_const_1 (target, op0, op1, perm0, perm1);
31150 /* Test whether a constant permutation is supported. */
31152 static bool
31153 rs6000_vectorize_vec_perm_const_ok (enum machine_mode vmode,
31154 const unsigned char *sel)
31156 /* AltiVec (and thus VSX) can handle arbitrary permutations. */
31157 if (TARGET_ALTIVEC)
31158 return true;
31160 /* Check for ps_merge* or evmerge* insns. */
31161 if ((TARGET_PAIRED_FLOAT && vmode == V2SFmode)
31162 || (TARGET_SPE && vmode == V2SImode))
31164 rtx op0 = gen_raw_REG (vmode, LAST_VIRTUAL_REGISTER + 1);
31165 rtx op1 = gen_raw_REG (vmode, LAST_VIRTUAL_REGISTER + 2);
31166 return rs6000_expand_vec_perm_const_1 (NULL, op0, op1, sel[0], sel[1]);
31169 return false;
31172 /* A subroutine for rs6000_expand_extract_even & rs6000_expand_interleave. */
31174 static void
31175 rs6000_do_expand_vec_perm (rtx target, rtx op0, rtx op1,
31176 enum machine_mode vmode, unsigned nelt, rtx perm[])
31178 enum machine_mode imode;
31179 rtx x;
31181 imode = vmode;
31182 if (GET_MODE_CLASS (vmode) != MODE_VECTOR_INT)
31184 imode = GET_MODE_INNER (vmode);
31185 imode = mode_for_size (GET_MODE_BITSIZE (imode), MODE_INT, 0);
31186 imode = mode_for_vector (imode, nelt);
31189 x = gen_rtx_CONST_VECTOR (imode, gen_rtvec_v (nelt, perm));
31190 x = expand_vec_perm (vmode, op0, op1, x, target);
31191 if (x != target)
31192 emit_move_insn (target, x);
31195 /* Expand an extract even operation. */
31197 void
31198 rs6000_expand_extract_even (rtx target, rtx op0, rtx op1)
31200 enum machine_mode vmode = GET_MODE (target);
31201 unsigned i, nelt = GET_MODE_NUNITS (vmode);
31202 rtx perm[16];
31204 for (i = 0; i < nelt; i++)
31205 perm[i] = GEN_INT (i * 2);
31207 rs6000_do_expand_vec_perm (target, op0, op1, vmode, nelt, perm);
31210 /* Expand a vector interleave operation. */
31212 void
31213 rs6000_expand_interleave (rtx target, rtx op0, rtx op1, bool highp)
31215 enum machine_mode vmode = GET_MODE (target);
31216 unsigned i, high, nelt = GET_MODE_NUNITS (vmode);
31217 rtx perm[16];
31219 high = (highp ? 0 : nelt / 2);
31220 for (i = 0; i < nelt / 2; i++)
31222 perm[i * 2] = GEN_INT (i + high);
31223 perm[i * 2 + 1] = GEN_INT (i + nelt + high);
31226 rs6000_do_expand_vec_perm (target, op0, op1, vmode, nelt, perm);
31229 /* Scale a V2DF vector SRC by two to the SCALE and place in TGT. */
31230 void
31231 rs6000_scale_v2df (rtx tgt, rtx src, int scale)
31233 HOST_WIDE_INT hwi_scale (scale);
31234 REAL_VALUE_TYPE r_pow;
31235 rtvec v = rtvec_alloc (2);
31236 rtx elt;
31237 rtx scale_vec = gen_reg_rtx (V2DFmode);
31238 (void)real_powi (&r_pow, DFmode, &dconst2, hwi_scale);
31239 elt = CONST_DOUBLE_FROM_REAL_VALUE (r_pow, DFmode);
31240 RTVEC_ELT (v, 0) = elt;
31241 RTVEC_ELT (v, 1) = elt;
31242 rs6000_expand_vector_init (scale_vec, gen_rtx_PARALLEL (V2DFmode, v));
31243 emit_insn (gen_mulv2df3 (tgt, src, scale_vec));
31246 /* Return an RTX representing where to find the function value of a
31247 function returning MODE. */
31248 static rtx
31249 rs6000_complex_function_value (enum machine_mode mode)
31251 unsigned int regno;
31252 rtx r1, r2;
31253 enum machine_mode inner = GET_MODE_INNER (mode);
31254 unsigned int inner_bytes = GET_MODE_SIZE (inner);
31256 if (FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
31257 regno = FP_ARG_RETURN;
31258 else
31260 regno = GP_ARG_RETURN;
31262 /* 32-bit is OK since it'll go in r3/r4. */
31263 if (TARGET_32BIT && inner_bytes >= 4)
31264 return gen_rtx_REG (mode, regno);
31267 if (inner_bytes >= 8)
31268 return gen_rtx_REG (mode, regno);
31270 r1 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno),
31271 const0_rtx);
31272 r2 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno + 1),
31273 GEN_INT (inner_bytes));
31274 return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
31277 /* Target hook for TARGET_FUNCTION_VALUE.
31279 On the SPE, both FPs and vectors are returned in r3.
31281 On RS/6000 an integer value is in r3 and a floating-point value is in
31282 fp1, unless -msoft-float. */
31284 static rtx
31285 rs6000_function_value (const_tree valtype,
31286 const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
31287 bool outgoing ATTRIBUTE_UNUSED)
31289 enum machine_mode mode;
31290 unsigned int regno;
31291 enum machine_mode elt_mode;
31292 int n_elts;
31294 /* Special handling for structs in darwin64. */
31295 if (TARGET_MACHO
31296 && rs6000_darwin64_struct_check_p (TYPE_MODE (valtype), valtype))
31298 CUMULATIVE_ARGS valcum;
31299 rtx valret;
31301 valcum.words = 0;
31302 valcum.fregno = FP_ARG_MIN_REG;
31303 valcum.vregno = ALTIVEC_ARG_MIN_REG;
31304 /* Do a trial code generation as if this were going to be passed as
31305 an argument; if any part goes in memory, we return NULL. */
31306 valret = rs6000_darwin64_record_arg (&valcum, valtype, true, /* retval= */ true);
31307 if (valret)
31308 return valret;
31309 /* Otherwise fall through to standard ABI rules. */
31312 /* The ELFv2 ABI returns homogeneous VFP aggregates in registers. */
31313 if (rs6000_discover_homogeneous_aggregate (TYPE_MODE (valtype), valtype,
31314 &elt_mode, &n_elts))
31316 int first_reg, n_regs, i;
31317 rtx par;
31319 if (SCALAR_FLOAT_MODE_P (elt_mode))
31321 /* _Decimal128 must use even/odd register pairs. */
31322 first_reg = (elt_mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
31323 n_regs = (GET_MODE_SIZE (elt_mode) + 7) >> 3;
31325 else
31327 first_reg = ALTIVEC_ARG_RETURN;
31328 n_regs = 1;
31331 par = gen_rtx_PARALLEL (TYPE_MODE (valtype), rtvec_alloc (n_elts));
31332 for (i = 0; i < n_elts; i++)
31334 rtx r = gen_rtx_REG (elt_mode, first_reg + i * n_regs);
31335 rtx off = GEN_INT (i * GET_MODE_SIZE (elt_mode));
31336 XVECEXP (par, 0, i) = gen_rtx_EXPR_LIST (VOIDmode, r, off);
31339 return par;
31342 if (TARGET_32BIT && TARGET_POWERPC64 && TYPE_MODE (valtype) == DImode)
31344 /* Long long return value need be split in -mpowerpc64, 32bit ABI. */
31345 return gen_rtx_PARALLEL (DImode,
31346 gen_rtvec (2,
31347 gen_rtx_EXPR_LIST (VOIDmode,
31348 gen_rtx_REG (SImode, GP_ARG_RETURN),
31349 const0_rtx),
31350 gen_rtx_EXPR_LIST (VOIDmode,
31351 gen_rtx_REG (SImode,
31352 GP_ARG_RETURN + 1),
31353 GEN_INT (4))));
31355 if (TARGET_32BIT && TARGET_POWERPC64 && TYPE_MODE (valtype) == DCmode)
31357 return gen_rtx_PARALLEL (DCmode,
31358 gen_rtvec (4,
31359 gen_rtx_EXPR_LIST (VOIDmode,
31360 gen_rtx_REG (SImode, GP_ARG_RETURN),
31361 const0_rtx),
31362 gen_rtx_EXPR_LIST (VOIDmode,
31363 gen_rtx_REG (SImode,
31364 GP_ARG_RETURN + 1),
31365 GEN_INT (4)),
31366 gen_rtx_EXPR_LIST (VOIDmode,
31367 gen_rtx_REG (SImode,
31368 GP_ARG_RETURN + 2),
31369 GEN_INT (8)),
31370 gen_rtx_EXPR_LIST (VOIDmode,
31371 gen_rtx_REG (SImode,
31372 GP_ARG_RETURN + 3),
31373 GEN_INT (12))));
31376 mode = TYPE_MODE (valtype);
31377 if ((INTEGRAL_TYPE_P (valtype) && GET_MODE_BITSIZE (mode) < BITS_PER_WORD)
31378 || POINTER_TYPE_P (valtype))
31379 mode = TARGET_32BIT ? SImode : DImode;
31381 if (DECIMAL_FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
31382 /* _Decimal128 must use an even/odd register pair. */
31383 regno = (mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
31384 else if (SCALAR_FLOAT_TYPE_P (valtype) && TARGET_HARD_FLOAT && TARGET_FPRS
31385 && ((TARGET_SINGLE_FLOAT && (mode == SFmode)) || TARGET_DOUBLE_FLOAT))
31386 regno = FP_ARG_RETURN;
31387 else if (TREE_CODE (valtype) == COMPLEX_TYPE
31388 && targetm.calls.split_complex_arg)
31389 return rs6000_complex_function_value (mode);
31390 /* VSX is a superset of Altivec and adds V2DImode/V2DFmode. Since the same
31391 return register is used in both cases, and we won't see V2DImode/V2DFmode
31392 for pure altivec, combine the two cases. */
31393 else if (TREE_CODE (valtype) == VECTOR_TYPE
31394 && TARGET_ALTIVEC && TARGET_ALTIVEC_ABI
31395 && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
31396 regno = ALTIVEC_ARG_RETURN;
31397 else if (TARGET_E500_DOUBLE && TARGET_HARD_FLOAT
31398 && (mode == DFmode || mode == DCmode
31399 || mode == TFmode || mode == TCmode))
31400 return spe_build_register_parallel (mode, GP_ARG_RETURN);
31401 else
31402 regno = GP_ARG_RETURN;
31404 return gen_rtx_REG (mode, regno);
31407 /* Define how to find the value returned by a library function
31408 assuming the value has mode MODE. */
31410 rs6000_libcall_value (enum machine_mode mode)
31412 unsigned int regno;
31414 if (TARGET_32BIT && TARGET_POWERPC64 && mode == DImode)
31416 /* Long long return value need be split in -mpowerpc64, 32bit ABI. */
31417 return gen_rtx_PARALLEL (DImode,
31418 gen_rtvec (2,
31419 gen_rtx_EXPR_LIST (VOIDmode,
31420 gen_rtx_REG (SImode, GP_ARG_RETURN),
31421 const0_rtx),
31422 gen_rtx_EXPR_LIST (VOIDmode,
31423 gen_rtx_REG (SImode,
31424 GP_ARG_RETURN + 1),
31425 GEN_INT (4))));
31428 if (DECIMAL_FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
31429 /* _Decimal128 must use an even/odd register pair. */
31430 regno = (mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
31431 else if (SCALAR_FLOAT_MODE_P (mode)
31432 && TARGET_HARD_FLOAT && TARGET_FPRS
31433 && ((TARGET_SINGLE_FLOAT && mode == SFmode) || TARGET_DOUBLE_FLOAT))
31434 regno = FP_ARG_RETURN;
31435 /* VSX is a superset of Altivec and adds V2DImode/V2DFmode. Since the same
31436 return register is used in both cases, and we won't see V2DImode/V2DFmode
31437 for pure altivec, combine the two cases. */
31438 else if (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
31439 && TARGET_ALTIVEC && TARGET_ALTIVEC_ABI)
31440 regno = ALTIVEC_ARG_RETURN;
31441 else if (COMPLEX_MODE_P (mode) && targetm.calls.split_complex_arg)
31442 return rs6000_complex_function_value (mode);
31443 else if (TARGET_E500_DOUBLE && TARGET_HARD_FLOAT
31444 && (mode == DFmode || mode == DCmode
31445 || mode == TFmode || mode == TCmode))
31446 return spe_build_register_parallel (mode, GP_ARG_RETURN);
31447 else
31448 regno = GP_ARG_RETURN;
31450 return gen_rtx_REG (mode, regno);
31454 /* Return true if we use LRA instead of reload pass. */
31455 static bool
31456 rs6000_lra_p (void)
31458 return rs6000_lra_flag;
31461 /* Given FROM and TO register numbers, say whether this elimination is allowed.
31462 Frame pointer elimination is automatically handled.
31464 For the RS/6000, if frame pointer elimination is being done, we would like
31465 to convert ap into fp, not sp.
31467 We need r30 if -mminimal-toc was specified, and there are constant pool
31468 references. */
31470 static bool
31471 rs6000_can_eliminate (const int from, const int to)
31473 return (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM
31474 ? ! frame_pointer_needed
31475 : from == RS6000_PIC_OFFSET_TABLE_REGNUM
31476 ? ! TARGET_MINIMAL_TOC || TARGET_NO_TOC || get_pool_size () == 0
31477 : true);
31480 /* Define the offset between two registers, FROM to be eliminated and its
31481 replacement TO, at the start of a routine. */
31482 HOST_WIDE_INT
31483 rs6000_initial_elimination_offset (int from, int to)
31485 rs6000_stack_t *info = rs6000_stack_info ();
31486 HOST_WIDE_INT offset;
31488 if (from == HARD_FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
31489 offset = info->push_p ? 0 : -info->total_size;
31490 else if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
31492 offset = info->push_p ? 0 : -info->total_size;
31493 if (FRAME_GROWS_DOWNWARD)
31494 offset += info->fixed_size + info->vars_size + info->parm_size;
31496 else if (from == FRAME_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
31497 offset = FRAME_GROWS_DOWNWARD
31498 ? info->fixed_size + info->vars_size + info->parm_size
31499 : 0;
31500 else if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
31501 offset = info->total_size;
31502 else if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
31503 offset = info->push_p ? info->total_size : 0;
31504 else if (from == RS6000_PIC_OFFSET_TABLE_REGNUM)
31505 offset = 0;
31506 else
31507 gcc_unreachable ();
31509 return offset;
31512 static rtx
31513 rs6000_dwarf_register_span (rtx reg)
31515 rtx parts[8];
31516 int i, words;
31517 unsigned regno = REGNO (reg);
31518 enum machine_mode mode = GET_MODE (reg);
31520 if (TARGET_SPE
31521 && regno < 32
31522 && (SPE_VECTOR_MODE (GET_MODE (reg))
31523 || (TARGET_E500_DOUBLE && FLOAT_MODE_P (mode)
31524 && mode != SFmode && mode != SDmode && mode != SCmode)))
31526 else
31527 return NULL_RTX;
31529 regno = REGNO (reg);
31531 /* The duality of the SPE register size wreaks all kinds of havoc.
31532 This is a way of distinguishing r0 in 32-bits from r0 in
31533 64-bits. */
31534 words = (GET_MODE_SIZE (mode) + UNITS_PER_FP_WORD - 1) / UNITS_PER_FP_WORD;
31535 gcc_assert (words <= 4);
31536 for (i = 0; i < words; i++, regno++)
31538 if (BYTES_BIG_ENDIAN)
31540 parts[2 * i] = gen_rtx_REG (SImode, regno + FIRST_SPE_HIGH_REGNO);
31541 parts[2 * i + 1] = gen_rtx_REG (SImode, regno);
31543 else
31545 parts[2 * i] = gen_rtx_REG (SImode, regno);
31546 parts[2 * i + 1] = gen_rtx_REG (SImode, regno + FIRST_SPE_HIGH_REGNO);
31550 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (words * 2, parts));
31553 /* Fill in sizes for SPE register high parts in table used by unwinder. */
31555 static void
31556 rs6000_init_dwarf_reg_sizes_extra (tree address)
31558 if (TARGET_SPE)
31560 int i;
31561 enum machine_mode mode = TYPE_MODE (char_type_node);
31562 rtx addr = expand_expr (address, NULL_RTX, VOIDmode, EXPAND_NORMAL);
31563 rtx mem = gen_rtx_MEM (BLKmode, addr);
31564 rtx value = gen_int_mode (4, mode);
31566 for (i = FIRST_SPE_HIGH_REGNO; i < LAST_SPE_HIGH_REGNO+1; i++)
31568 int column = DWARF_REG_TO_UNWIND_COLUMN
31569 (DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), true));
31570 HOST_WIDE_INT offset = column * GET_MODE_SIZE (mode);
31572 emit_move_insn (adjust_address (mem, mode, offset), value);
31576 if (TARGET_MACHO && ! TARGET_ALTIVEC)
31578 int i;
31579 enum machine_mode mode = TYPE_MODE (char_type_node);
31580 rtx addr = expand_expr (address, NULL_RTX, VOIDmode, EXPAND_NORMAL);
31581 rtx mem = gen_rtx_MEM (BLKmode, addr);
31582 rtx value = gen_int_mode (16, mode);
31584 /* On Darwin, libgcc may be built to run on both G3 and G4/5.
31585 The unwinder still needs to know the size of Altivec registers. */
31587 for (i = FIRST_ALTIVEC_REGNO; i < LAST_ALTIVEC_REGNO+1; i++)
31589 int column = DWARF_REG_TO_UNWIND_COLUMN
31590 (DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), true));
31591 HOST_WIDE_INT offset = column * GET_MODE_SIZE (mode);
31593 emit_move_insn (adjust_address (mem, mode, offset), value);
31598 /* Map internal gcc register numbers to debug format register numbers.
31599 FORMAT specifies the type of debug register number to use:
31600 0 -- debug information, except for frame-related sections
31601 1 -- DWARF .debug_frame section
31602 2 -- DWARF .eh_frame section */
31604 unsigned int
31605 rs6000_dbx_register_number (unsigned int regno, unsigned int format)
31607 /* We never use the GCC internal number for SPE high registers.
31608 Those are mapped to the 1200..1231 range for all debug formats. */
31609 if (SPE_HIGH_REGNO_P (regno))
31610 return regno - FIRST_SPE_HIGH_REGNO + 1200;
31612 /* Except for the above, we use the internal number for non-DWARF
31613 debug information, and also for .eh_frame. */
31614 if ((format == 0 && write_symbols != DWARF2_DEBUG) || format == 2)
31615 return regno;
31617 /* On some platforms, we use the standard DWARF register
31618 numbering for .debug_info and .debug_frame. */
31619 #ifdef RS6000_USE_DWARF_NUMBERING
31620 if (regno <= 63)
31621 return regno;
31622 if (regno == LR_REGNO)
31623 return 108;
31624 if (regno == CTR_REGNO)
31625 return 109;
31626 /* Special handling for CR for .debug_frame: rs6000_emit_prologue has
31627 translated any combination of CR2, CR3, CR4 saves to a save of CR2.
31628 The actual code emitted saves the whole of CR, so we map CR2_REGNO
31629 to the DWARF reg for CR. */
31630 if (format == 1 && regno == CR2_REGNO)
31631 return 64;
31632 if (CR_REGNO_P (regno))
31633 return regno - CR0_REGNO + 86;
31634 if (regno == CA_REGNO)
31635 return 101; /* XER */
31636 if (ALTIVEC_REGNO_P (regno))
31637 return regno - FIRST_ALTIVEC_REGNO + 1124;
31638 if (regno == VRSAVE_REGNO)
31639 return 356;
31640 if (regno == VSCR_REGNO)
31641 return 67;
31642 if (regno == SPE_ACC_REGNO)
31643 return 99;
31644 if (regno == SPEFSCR_REGNO)
31645 return 612;
31646 #endif
31647 return regno;
31650 /* target hook eh_return_filter_mode */
31651 static enum machine_mode
31652 rs6000_eh_return_filter_mode (void)
31654 return TARGET_32BIT ? SImode : word_mode;
31657 /* Target hook for scalar_mode_supported_p. */
31658 static bool
31659 rs6000_scalar_mode_supported_p (enum machine_mode mode)
31661 if (DECIMAL_FLOAT_MODE_P (mode))
31662 return default_decimal_float_supported_p ();
31663 else
31664 return default_scalar_mode_supported_p (mode);
31667 /* Target hook for vector_mode_supported_p. */
31668 static bool
31669 rs6000_vector_mode_supported_p (enum machine_mode mode)
31672 if (TARGET_PAIRED_FLOAT && PAIRED_VECTOR_MODE (mode))
31673 return true;
31675 if (TARGET_SPE && SPE_VECTOR_MODE (mode))
31676 return true;
31678 else if (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode))
31679 return true;
31681 else
31682 return false;
31685 /* Target hook for invalid_arg_for_unprototyped_fn. */
31686 static const char *
31687 invalid_arg_for_unprototyped_fn (const_tree typelist, const_tree funcdecl, const_tree val)
31689 return (!rs6000_darwin64_abi
31690 && typelist == 0
31691 && TREE_CODE (TREE_TYPE (val)) == VECTOR_TYPE
31692 && (funcdecl == NULL_TREE
31693 || (TREE_CODE (funcdecl) == FUNCTION_DECL
31694 && DECL_BUILT_IN_CLASS (funcdecl) != BUILT_IN_MD)))
31695 ? N_("AltiVec argument passed to unprototyped function")
31696 : NULL;
31699 /* For TARGET_SECURE_PLT 32-bit PIC code we can save PIC register
31700 setup by using __stack_chk_fail_local hidden function instead of
31701 calling __stack_chk_fail directly. Otherwise it is better to call
31702 __stack_chk_fail directly. */
31704 static tree ATTRIBUTE_UNUSED
31705 rs6000_stack_protect_fail (void)
31707 return (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
31708 ? default_hidden_stack_protect_fail ()
31709 : default_external_stack_protect_fail ();
31712 void
31713 rs6000_final_prescan_insn (rtx_insn *insn, rtx *operand ATTRIBUTE_UNUSED,
31714 int num_operands ATTRIBUTE_UNUSED)
31716 if (rs6000_warn_cell_microcode)
31718 const char *temp;
31719 int insn_code_number = recog_memoized (insn);
31720 location_t location = INSN_LOCATION (insn);
31722 /* Punt on insns we cannot recognize. */
31723 if (insn_code_number < 0)
31724 return;
31726 temp = get_insn_template (insn_code_number, insn);
31728 if (get_attr_cell_micro (insn) == CELL_MICRO_ALWAYS)
31729 warning_at (location, OPT_mwarn_cell_microcode,
31730 "emitting microcode insn %s\t[%s] #%d",
31731 temp, insn_data[INSN_CODE (insn)].name, INSN_UID (insn));
31732 else if (get_attr_cell_micro (insn) == CELL_MICRO_CONDITIONAL)
31733 warning_at (location, OPT_mwarn_cell_microcode,
31734 "emitting conditional microcode insn %s\t[%s] #%d",
31735 temp, insn_data[INSN_CODE (insn)].name, INSN_UID (insn));
31739 /* Implement the TARGET_ASAN_SHADOW_OFFSET hook. */
31741 #if TARGET_ELF
31742 static unsigned HOST_WIDE_INT
31743 rs6000_asan_shadow_offset (void)
31745 return (unsigned HOST_WIDE_INT) 1 << (TARGET_64BIT ? 41 : 29);
31747 #endif
31749 /* Mask options that we want to support inside of attribute((target)) and
31750 #pragma GCC target operations. Note, we do not include things like
31751 64/32-bit, endianess, hard/soft floating point, etc. that would have
31752 different calling sequences. */
31754 struct rs6000_opt_mask {
31755 const char *name; /* option name */
31756 HOST_WIDE_INT mask; /* mask to set */
31757 bool invert; /* invert sense of mask */
31758 bool valid_target; /* option is a target option */
31761 static struct rs6000_opt_mask const rs6000_opt_masks[] =
31763 { "altivec", OPTION_MASK_ALTIVEC, false, true },
31764 { "cmpb", OPTION_MASK_CMPB, false, true },
31765 { "crypto", OPTION_MASK_CRYPTO, false, true },
31766 { "direct-move", OPTION_MASK_DIRECT_MOVE, false, true },
31767 { "dlmzb", OPTION_MASK_DLMZB, false, true },
31768 { "fprnd", OPTION_MASK_FPRND, false, true },
31769 { "hard-dfp", OPTION_MASK_DFP, false, true },
31770 { "htm", OPTION_MASK_HTM, false, true },
31771 { "isel", OPTION_MASK_ISEL, false, true },
31772 { "mfcrf", OPTION_MASK_MFCRF, false, true },
31773 { "mfpgpr", OPTION_MASK_MFPGPR, false, true },
31774 { "mulhw", OPTION_MASK_MULHW, false, true },
31775 { "multiple", OPTION_MASK_MULTIPLE, false, true },
31776 { "popcntb", OPTION_MASK_POPCNTB, false, true },
31777 { "popcntd", OPTION_MASK_POPCNTD, false, true },
31778 { "power8-fusion", OPTION_MASK_P8_FUSION, false, true },
31779 { "power8-fusion-sign", OPTION_MASK_P8_FUSION_SIGN, false, true },
31780 { "power8-vector", OPTION_MASK_P8_VECTOR, false, true },
31781 { "powerpc-gfxopt", OPTION_MASK_PPC_GFXOPT, false, true },
31782 { "powerpc-gpopt", OPTION_MASK_PPC_GPOPT, false, true },
31783 { "quad-memory", OPTION_MASK_QUAD_MEMORY, false, true },
31784 { "quad-memory-atomic", OPTION_MASK_QUAD_MEMORY_ATOMIC, false, true },
31785 { "recip-precision", OPTION_MASK_RECIP_PRECISION, false, true },
31786 { "string", OPTION_MASK_STRING, false, true },
31787 { "update", OPTION_MASK_NO_UPDATE, true , true },
31788 { "upper-regs-df", OPTION_MASK_UPPER_REGS_DF, false, false },
31789 { "upper-regs-sf", OPTION_MASK_UPPER_REGS_SF, false, false },
31790 { "vsx", OPTION_MASK_VSX, false, true },
31791 { "vsx-timode", OPTION_MASK_VSX_TIMODE, false, true },
31792 #ifdef OPTION_MASK_64BIT
31793 #if TARGET_AIX_OS
31794 { "aix64", OPTION_MASK_64BIT, false, false },
31795 { "aix32", OPTION_MASK_64BIT, true, false },
31796 #else
31797 { "64", OPTION_MASK_64BIT, false, false },
31798 { "32", OPTION_MASK_64BIT, true, false },
31799 #endif
31800 #endif
31801 #ifdef OPTION_MASK_EABI
31802 { "eabi", OPTION_MASK_EABI, false, false },
31803 #endif
31804 #ifdef OPTION_MASK_LITTLE_ENDIAN
31805 { "little", OPTION_MASK_LITTLE_ENDIAN, false, false },
31806 { "big", OPTION_MASK_LITTLE_ENDIAN, true, false },
31807 #endif
31808 #ifdef OPTION_MASK_RELOCATABLE
31809 { "relocatable", OPTION_MASK_RELOCATABLE, false, false },
31810 #endif
31811 #ifdef OPTION_MASK_STRICT_ALIGN
31812 { "strict-align", OPTION_MASK_STRICT_ALIGN, false, false },
31813 #endif
31814 { "soft-float", OPTION_MASK_SOFT_FLOAT, false, false },
31815 { "string", OPTION_MASK_STRING, false, false },
31818 /* Builtin mask mapping for printing the flags. */
31819 static struct rs6000_opt_mask const rs6000_builtin_mask_names[] =
31821 { "altivec", RS6000_BTM_ALTIVEC, false, false },
31822 { "vsx", RS6000_BTM_VSX, false, false },
31823 { "spe", RS6000_BTM_SPE, false, false },
31824 { "paired", RS6000_BTM_PAIRED, false, false },
31825 { "fre", RS6000_BTM_FRE, false, false },
31826 { "fres", RS6000_BTM_FRES, false, false },
31827 { "frsqrte", RS6000_BTM_FRSQRTE, false, false },
31828 { "frsqrtes", RS6000_BTM_FRSQRTES, false, false },
31829 { "popcntd", RS6000_BTM_POPCNTD, false, false },
31830 { "cell", RS6000_BTM_CELL, false, false },
31831 { "power8-vector", RS6000_BTM_P8_VECTOR, false, false },
31832 { "crypto", RS6000_BTM_CRYPTO, false, false },
31833 { "htm", RS6000_BTM_HTM, false, false },
31834 { "hard-dfp", RS6000_BTM_DFP, false, false },
31835 { "hard-float", RS6000_BTM_HARD_FLOAT, false, false },
31836 { "long-double-128", RS6000_BTM_LDBL128, false, false },
31839 /* Option variables that we want to support inside attribute((target)) and
31840 #pragma GCC target operations. */
31842 struct rs6000_opt_var {
31843 const char *name; /* option name */
31844 size_t global_offset; /* offset of the option in global_options. */
31845 size_t target_offset; /* offset of the option in target optiosn. */
31848 static struct rs6000_opt_var const rs6000_opt_vars[] =
31850 { "friz",
31851 offsetof (struct gcc_options, x_TARGET_FRIZ),
31852 offsetof (struct cl_target_option, x_TARGET_FRIZ), },
31853 { "avoid-indexed-addresses",
31854 offsetof (struct gcc_options, x_TARGET_AVOID_XFORM),
31855 offsetof (struct cl_target_option, x_TARGET_AVOID_XFORM) },
31856 { "paired",
31857 offsetof (struct gcc_options, x_rs6000_paired_float),
31858 offsetof (struct cl_target_option, x_rs6000_paired_float), },
31859 { "longcall",
31860 offsetof (struct gcc_options, x_rs6000_default_long_calls),
31861 offsetof (struct cl_target_option, x_rs6000_default_long_calls), },
31864 /* Inner function to handle attribute((target("..."))) and #pragma GCC target
31865 parsing. Return true if there were no errors. */
31867 static bool
31868 rs6000_inner_target_options (tree args, bool attr_p)
31870 bool ret = true;
31872 if (args == NULL_TREE)
31875 else if (TREE_CODE (args) == STRING_CST)
31877 char *p = ASTRDUP (TREE_STRING_POINTER (args));
31878 char *q;
31880 while ((q = strtok (p, ",")) != NULL)
31882 bool error_p = false;
31883 bool not_valid_p = false;
31884 const char *cpu_opt = NULL;
31886 p = NULL;
31887 if (strncmp (q, "cpu=", 4) == 0)
31889 int cpu_index = rs6000_cpu_name_lookup (q+4);
31890 if (cpu_index >= 0)
31891 rs6000_cpu_index = cpu_index;
31892 else
31894 error_p = true;
31895 cpu_opt = q+4;
31898 else if (strncmp (q, "tune=", 5) == 0)
31900 int tune_index = rs6000_cpu_name_lookup (q+5);
31901 if (tune_index >= 0)
31902 rs6000_tune_index = tune_index;
31903 else
31905 error_p = true;
31906 cpu_opt = q+5;
31909 else
31911 size_t i;
31912 bool invert = false;
31913 char *r = q;
31915 error_p = true;
31916 if (strncmp (r, "no-", 3) == 0)
31918 invert = true;
31919 r += 3;
31922 for (i = 0; i < ARRAY_SIZE (rs6000_opt_masks); i++)
31923 if (strcmp (r, rs6000_opt_masks[i].name) == 0)
31925 HOST_WIDE_INT mask = rs6000_opt_masks[i].mask;
31927 if (!rs6000_opt_masks[i].valid_target)
31928 not_valid_p = true;
31929 else
31931 error_p = false;
31932 rs6000_isa_flags_explicit |= mask;
31934 /* VSX needs altivec, so -mvsx automagically sets
31935 altivec. */
31936 if (mask == OPTION_MASK_VSX && !invert)
31937 mask |= OPTION_MASK_ALTIVEC;
31939 if (rs6000_opt_masks[i].invert)
31940 invert = !invert;
31942 if (invert)
31943 rs6000_isa_flags &= ~mask;
31944 else
31945 rs6000_isa_flags |= mask;
31947 break;
31950 if (error_p && !not_valid_p)
31952 for (i = 0; i < ARRAY_SIZE (rs6000_opt_vars); i++)
31953 if (strcmp (r, rs6000_opt_vars[i].name) == 0)
31955 size_t j = rs6000_opt_vars[i].global_offset;
31956 *((int *) ((char *)&global_options + j)) = !invert;
31957 error_p = false;
31958 break;
31963 if (error_p)
31965 const char *eprefix, *esuffix;
31967 ret = false;
31968 if (attr_p)
31970 eprefix = "__attribute__((__target__(";
31971 esuffix = ")))";
31973 else
31975 eprefix = "#pragma GCC target ";
31976 esuffix = "";
31979 if (cpu_opt)
31980 error ("invalid cpu \"%s\" for %s\"%s\"%s", cpu_opt, eprefix,
31981 q, esuffix);
31982 else if (not_valid_p)
31983 error ("%s\"%s\"%s is not allowed", eprefix, q, esuffix);
31984 else
31985 error ("%s\"%s\"%s is invalid", eprefix, q, esuffix);
31990 else if (TREE_CODE (args) == TREE_LIST)
31994 tree value = TREE_VALUE (args);
31995 if (value)
31997 bool ret2 = rs6000_inner_target_options (value, attr_p);
31998 if (!ret2)
31999 ret = false;
32001 args = TREE_CHAIN (args);
32003 while (args != NULL_TREE);
32006 else
32007 gcc_unreachable ();
32009 return ret;
32012 /* Print out the target options as a list for -mdebug=target. */
32014 static void
32015 rs6000_debug_target_options (tree args, const char *prefix)
32017 if (args == NULL_TREE)
32018 fprintf (stderr, "%s<NULL>", prefix);
32020 else if (TREE_CODE (args) == STRING_CST)
32022 char *p = ASTRDUP (TREE_STRING_POINTER (args));
32023 char *q;
32025 while ((q = strtok (p, ",")) != NULL)
32027 p = NULL;
32028 fprintf (stderr, "%s\"%s\"", prefix, q);
32029 prefix = ", ";
32033 else if (TREE_CODE (args) == TREE_LIST)
32037 tree value = TREE_VALUE (args);
32038 if (value)
32040 rs6000_debug_target_options (value, prefix);
32041 prefix = ", ";
32043 args = TREE_CHAIN (args);
32045 while (args != NULL_TREE);
32048 else
32049 gcc_unreachable ();
32051 return;
32055 /* Hook to validate attribute((target("..."))). */
32057 static bool
32058 rs6000_valid_attribute_p (tree fndecl,
32059 tree ARG_UNUSED (name),
32060 tree args,
32061 int flags)
32063 struct cl_target_option cur_target;
32064 bool ret;
32065 tree old_optimize = build_optimization_node (&global_options);
32066 tree new_target, new_optimize;
32067 tree func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
32069 gcc_assert ((fndecl != NULL_TREE) && (args != NULL_TREE));
32071 if (TARGET_DEBUG_TARGET)
32073 tree tname = DECL_NAME (fndecl);
32074 fprintf (stderr, "\n==================== rs6000_valid_attribute_p:\n");
32075 if (tname)
32076 fprintf (stderr, "function: %.*s\n",
32077 (int) IDENTIFIER_LENGTH (tname),
32078 IDENTIFIER_POINTER (tname));
32079 else
32080 fprintf (stderr, "function: unknown\n");
32082 fprintf (stderr, "args:");
32083 rs6000_debug_target_options (args, " ");
32084 fprintf (stderr, "\n");
32086 if (flags)
32087 fprintf (stderr, "flags: 0x%x\n", flags);
32089 fprintf (stderr, "--------------------\n");
32092 old_optimize = build_optimization_node (&global_options);
32093 func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
32095 /* If the function changed the optimization levels as well as setting target
32096 options, start with the optimizations specified. */
32097 if (func_optimize && func_optimize != old_optimize)
32098 cl_optimization_restore (&global_options,
32099 TREE_OPTIMIZATION (func_optimize));
32101 /* The target attributes may also change some optimization flags, so update
32102 the optimization options if necessary. */
32103 cl_target_option_save (&cur_target, &global_options);
32104 rs6000_cpu_index = rs6000_tune_index = -1;
32105 ret = rs6000_inner_target_options (args, true);
32107 /* Set up any additional state. */
32108 if (ret)
32110 ret = rs6000_option_override_internal (false);
32111 new_target = build_target_option_node (&global_options);
32113 else
32114 new_target = NULL;
32116 new_optimize = build_optimization_node (&global_options);
32118 if (!new_target)
32119 ret = false;
32121 else if (fndecl)
32123 DECL_FUNCTION_SPECIFIC_TARGET (fndecl) = new_target;
32125 if (old_optimize != new_optimize)
32126 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl) = new_optimize;
32129 cl_target_option_restore (&global_options, &cur_target);
32131 if (old_optimize != new_optimize)
32132 cl_optimization_restore (&global_options,
32133 TREE_OPTIMIZATION (old_optimize));
32135 return ret;
32139 /* Hook to validate the current #pragma GCC target and set the state, and
32140 update the macros based on what was changed. If ARGS is NULL, then
32141 POP_TARGET is used to reset the options. */
32143 bool
32144 rs6000_pragma_target_parse (tree args, tree pop_target)
32146 tree prev_tree = build_target_option_node (&global_options);
32147 tree cur_tree;
32148 struct cl_target_option *prev_opt, *cur_opt;
32149 HOST_WIDE_INT prev_flags, cur_flags, diff_flags;
32150 HOST_WIDE_INT prev_bumask, cur_bumask, diff_bumask;
32152 if (TARGET_DEBUG_TARGET)
32154 fprintf (stderr, "\n==================== rs6000_pragma_target_parse\n");
32155 fprintf (stderr, "args:");
32156 rs6000_debug_target_options (args, " ");
32157 fprintf (stderr, "\n");
32159 if (pop_target)
32161 fprintf (stderr, "pop_target:\n");
32162 debug_tree (pop_target);
32164 else
32165 fprintf (stderr, "pop_target: <NULL>\n");
32167 fprintf (stderr, "--------------------\n");
32170 if (! args)
32172 cur_tree = ((pop_target)
32173 ? pop_target
32174 : target_option_default_node);
32175 cl_target_option_restore (&global_options,
32176 TREE_TARGET_OPTION (cur_tree));
32178 else
32180 rs6000_cpu_index = rs6000_tune_index = -1;
32181 if (!rs6000_inner_target_options (args, false)
32182 || !rs6000_option_override_internal (false)
32183 || (cur_tree = build_target_option_node (&global_options))
32184 == NULL_TREE)
32186 if (TARGET_DEBUG_BUILTIN || TARGET_DEBUG_TARGET)
32187 fprintf (stderr, "invalid pragma\n");
32189 return false;
32193 target_option_current_node = cur_tree;
32195 /* If we have the preprocessor linked in (i.e. C or C++ languages), possibly
32196 change the macros that are defined. */
32197 if (rs6000_target_modify_macros_ptr)
32199 prev_opt = TREE_TARGET_OPTION (prev_tree);
32200 prev_bumask = prev_opt->x_rs6000_builtin_mask;
32201 prev_flags = prev_opt->x_rs6000_isa_flags;
32203 cur_opt = TREE_TARGET_OPTION (cur_tree);
32204 cur_flags = cur_opt->x_rs6000_isa_flags;
32205 cur_bumask = cur_opt->x_rs6000_builtin_mask;
32207 diff_bumask = (prev_bumask ^ cur_bumask);
32208 diff_flags = (prev_flags ^ cur_flags);
32210 if ((diff_flags != 0) || (diff_bumask != 0))
32212 /* Delete old macros. */
32213 rs6000_target_modify_macros_ptr (false,
32214 prev_flags & diff_flags,
32215 prev_bumask & diff_bumask);
32217 /* Define new macros. */
32218 rs6000_target_modify_macros_ptr (true,
32219 cur_flags & diff_flags,
32220 cur_bumask & diff_bumask);
32224 return true;
32228 /* Remember the last target of rs6000_set_current_function. */
32229 static GTY(()) tree rs6000_previous_fndecl;
32231 /* Establish appropriate back-end context for processing the function
32232 FNDECL. The argument might be NULL to indicate processing at top
32233 level, outside of any function scope. */
32234 static void
32235 rs6000_set_current_function (tree fndecl)
32237 tree old_tree = (rs6000_previous_fndecl
32238 ? DECL_FUNCTION_SPECIFIC_TARGET (rs6000_previous_fndecl)
32239 : NULL_TREE);
32241 tree new_tree = (fndecl
32242 ? DECL_FUNCTION_SPECIFIC_TARGET (fndecl)
32243 : NULL_TREE);
32245 if (TARGET_DEBUG_TARGET)
32247 bool print_final = false;
32248 fprintf (stderr, "\n==================== rs6000_set_current_function");
32250 if (fndecl)
32251 fprintf (stderr, ", fndecl %s (%p)",
32252 (DECL_NAME (fndecl)
32253 ? IDENTIFIER_POINTER (DECL_NAME (fndecl))
32254 : "<unknown>"), (void *)fndecl);
32256 if (rs6000_previous_fndecl)
32257 fprintf (stderr, ", prev_fndecl (%p)", (void *)rs6000_previous_fndecl);
32259 fprintf (stderr, "\n");
32260 if (new_tree)
32262 fprintf (stderr, "\nnew fndecl target specific options:\n");
32263 debug_tree (new_tree);
32264 print_final = true;
32267 if (old_tree)
32269 fprintf (stderr, "\nold fndecl target specific options:\n");
32270 debug_tree (old_tree);
32271 print_final = true;
32274 if (print_final)
32275 fprintf (stderr, "--------------------\n");
32278 /* Only change the context if the function changes. This hook is called
32279 several times in the course of compiling a function, and we don't want to
32280 slow things down too much or call target_reinit when it isn't safe. */
32281 if (fndecl && fndecl != rs6000_previous_fndecl)
32283 rs6000_previous_fndecl = fndecl;
32284 if (old_tree == new_tree)
32287 else if (new_tree)
32289 cl_target_option_restore (&global_options,
32290 TREE_TARGET_OPTION (new_tree));
32291 if (TREE_TARGET_GLOBALS (new_tree))
32292 restore_target_globals (TREE_TARGET_GLOBALS (new_tree));
32293 else
32294 TREE_TARGET_GLOBALS (new_tree)
32295 = save_target_globals_default_opts ();
32298 else if (old_tree)
32300 new_tree = target_option_current_node;
32301 cl_target_option_restore (&global_options,
32302 TREE_TARGET_OPTION (new_tree));
32303 if (TREE_TARGET_GLOBALS (new_tree))
32304 restore_target_globals (TREE_TARGET_GLOBALS (new_tree));
32305 else if (new_tree == target_option_default_node)
32306 restore_target_globals (&default_target_globals);
32307 else
32308 TREE_TARGET_GLOBALS (new_tree)
32309 = save_target_globals_default_opts ();
32315 /* Save the current options */
32317 static void
32318 rs6000_function_specific_save (struct cl_target_option *ptr,
32319 struct gcc_options *opts)
32321 ptr->x_rs6000_isa_flags = opts->x_rs6000_isa_flags;
32322 ptr->x_rs6000_isa_flags_explicit = opts->x_rs6000_isa_flags_explicit;
32325 /* Restore the current options */
32327 static void
32328 rs6000_function_specific_restore (struct gcc_options *opts,
32329 struct cl_target_option *ptr)
32332 opts->x_rs6000_isa_flags = ptr->x_rs6000_isa_flags;
32333 opts->x_rs6000_isa_flags_explicit = ptr->x_rs6000_isa_flags_explicit;
32334 (void) rs6000_option_override_internal (false);
32337 /* Print the current options */
32339 static void
32340 rs6000_function_specific_print (FILE *file, int indent,
32341 struct cl_target_option *ptr)
32343 rs6000_print_isa_options (file, indent, "Isa options set",
32344 ptr->x_rs6000_isa_flags);
32346 rs6000_print_isa_options (file, indent, "Isa options explicit",
32347 ptr->x_rs6000_isa_flags_explicit);
32350 /* Helper function to print the current isa or misc options on a line. */
32352 static void
32353 rs6000_print_options_internal (FILE *file,
32354 int indent,
32355 const char *string,
32356 HOST_WIDE_INT flags,
32357 const char *prefix,
32358 const struct rs6000_opt_mask *opts,
32359 size_t num_elements)
32361 size_t i;
32362 size_t start_column = 0;
32363 size_t cur_column;
32364 size_t max_column = 76;
32365 const char *comma = "";
32367 if (indent)
32368 start_column += fprintf (file, "%*s", indent, "");
32370 if (!flags)
32372 fprintf (stderr, DEBUG_FMT_S, string, "<none>");
32373 return;
32376 start_column += fprintf (stderr, DEBUG_FMT_WX, string, flags);
32378 /* Print the various mask options. */
32379 cur_column = start_column;
32380 for (i = 0; i < num_elements; i++)
32382 if ((flags & opts[i].mask) != 0)
32384 const char *no_str = rs6000_opt_masks[i].invert ? "no-" : "";
32385 size_t len = (strlen (comma)
32386 + strlen (prefix)
32387 + strlen (no_str)
32388 + strlen (rs6000_opt_masks[i].name));
32390 cur_column += len;
32391 if (cur_column > max_column)
32393 fprintf (stderr, ", \\\n%*s", (int)start_column, "");
32394 cur_column = start_column + len;
32395 comma = "";
32398 fprintf (file, "%s%s%s%s", comma, prefix, no_str,
32399 rs6000_opt_masks[i].name);
32400 flags &= ~ opts[i].mask;
32401 comma = ", ";
32405 fputs ("\n", file);
32408 /* Helper function to print the current isa options on a line. */
32410 static void
32411 rs6000_print_isa_options (FILE *file, int indent, const char *string,
32412 HOST_WIDE_INT flags)
32414 rs6000_print_options_internal (file, indent, string, flags, "-m",
32415 &rs6000_opt_masks[0],
32416 ARRAY_SIZE (rs6000_opt_masks));
32419 static void
32420 rs6000_print_builtin_options (FILE *file, int indent, const char *string,
32421 HOST_WIDE_INT flags)
32423 rs6000_print_options_internal (file, indent, string, flags, "",
32424 &rs6000_builtin_mask_names[0],
32425 ARRAY_SIZE (rs6000_builtin_mask_names));
32429 /* Hook to determine if one function can safely inline another. */
32431 static bool
32432 rs6000_can_inline_p (tree caller, tree callee)
32434 bool ret = false;
32435 tree caller_tree = DECL_FUNCTION_SPECIFIC_TARGET (caller);
32436 tree callee_tree = DECL_FUNCTION_SPECIFIC_TARGET (callee);
32438 /* If callee has no option attributes, then it is ok to inline. */
32439 if (!callee_tree)
32440 ret = true;
32442 /* If caller has no option attributes, but callee does then it is not ok to
32443 inline. */
32444 else if (!caller_tree)
32445 ret = false;
32447 else
32449 struct cl_target_option *caller_opts = TREE_TARGET_OPTION (caller_tree);
32450 struct cl_target_option *callee_opts = TREE_TARGET_OPTION (callee_tree);
32452 /* Callee's options should a subset of the caller's, i.e. a vsx function
32453 can inline an altivec function but a non-vsx function can't inline a
32454 vsx function. */
32455 if ((caller_opts->x_rs6000_isa_flags & callee_opts->x_rs6000_isa_flags)
32456 == callee_opts->x_rs6000_isa_flags)
32457 ret = true;
32460 if (TARGET_DEBUG_TARGET)
32461 fprintf (stderr, "rs6000_can_inline_p:, caller %s, callee %s, %s inline\n",
32462 (DECL_NAME (caller)
32463 ? IDENTIFIER_POINTER (DECL_NAME (caller))
32464 : "<unknown>"),
32465 (DECL_NAME (callee)
32466 ? IDENTIFIER_POINTER (DECL_NAME (callee))
32467 : "<unknown>"),
32468 (ret ? "can" : "cannot"));
32470 return ret;
32473 /* Allocate a stack temp and fixup the address so it meets the particular
32474 memory requirements (either offetable or REG+REG addressing). */
32477 rs6000_allocate_stack_temp (enum machine_mode mode,
32478 bool offsettable_p,
32479 bool reg_reg_p)
32481 rtx stack = assign_stack_temp (mode, GET_MODE_SIZE (mode));
32482 rtx addr = XEXP (stack, 0);
32483 int strict_p = (reload_in_progress || reload_completed);
32485 if (!legitimate_indirect_address_p (addr, strict_p))
32487 if (offsettable_p
32488 && !rs6000_legitimate_offset_address_p (mode, addr, strict_p, true))
32489 stack = replace_equiv_address (stack, copy_addr_to_reg (addr));
32491 else if (reg_reg_p && !legitimate_indexed_address_p (addr, strict_p))
32492 stack = replace_equiv_address (stack, copy_addr_to_reg (addr));
32495 return stack;
32498 /* Given a memory reference, if it is not a reg or reg+reg addressing, convert
32499 to such a form to deal with memory reference instructions like STFIWX that
32500 only take reg+reg addressing. */
32503 rs6000_address_for_fpconvert (rtx x)
32505 int strict_p = (reload_in_progress || reload_completed);
32506 rtx addr;
32508 gcc_assert (MEM_P (x));
32509 addr = XEXP (x, 0);
32510 if (! legitimate_indirect_address_p (addr, strict_p)
32511 && ! legitimate_indexed_address_p (addr, strict_p))
32513 if (GET_CODE (addr) == PRE_INC || GET_CODE (addr) == PRE_DEC)
32515 rtx reg = XEXP (addr, 0);
32516 HOST_WIDE_INT size = GET_MODE_SIZE (GET_MODE (x));
32517 rtx size_rtx = GEN_INT ((GET_CODE (addr) == PRE_DEC) ? -size : size);
32518 gcc_assert (REG_P (reg));
32519 emit_insn (gen_add3_insn (reg, reg, size_rtx));
32520 addr = reg;
32522 else if (GET_CODE (addr) == PRE_MODIFY)
32524 rtx reg = XEXP (addr, 0);
32525 rtx expr = XEXP (addr, 1);
32526 gcc_assert (REG_P (reg));
32527 gcc_assert (GET_CODE (expr) == PLUS);
32528 emit_insn (gen_add3_insn (reg, XEXP (expr, 0), XEXP (expr, 1)));
32529 addr = reg;
32532 x = replace_equiv_address (x, copy_addr_to_reg (addr));
32535 return x;
32538 /* Given a memory reference, if it is not in the form for altivec memory
32539 reference instructions (i.e. reg or reg+reg addressing with AND of -16),
32540 convert to the altivec format. */
32543 rs6000_address_for_altivec (rtx x)
32545 gcc_assert (MEM_P (x));
32546 if (!altivec_indexed_or_indirect_operand (x, GET_MODE (x)))
32548 rtx addr = XEXP (x, 0);
32549 int strict_p = (reload_in_progress || reload_completed);
32551 if (!legitimate_indexed_address_p (addr, strict_p)
32552 && !legitimate_indirect_address_p (addr, strict_p))
32553 addr = copy_to_mode_reg (Pmode, addr);
32555 addr = gen_rtx_AND (Pmode, addr, GEN_INT (-16));
32556 x = change_address (x, GET_MODE (x), addr);
32559 return x;
32562 /* Implement TARGET_LEGITIMATE_CONSTANT_P.
32564 On the RS/6000, all integer constants are acceptable, most won't be valid
32565 for particular insns, though. Only easy FP constants are acceptable. */
32567 static bool
32568 rs6000_legitimate_constant_p (enum machine_mode mode, rtx x)
32570 if (TARGET_ELF && tls_referenced_p (x))
32571 return false;
32573 return ((GET_CODE (x) != CONST_DOUBLE && GET_CODE (x) != CONST_VECTOR)
32574 || GET_MODE (x) == VOIDmode
32575 || (TARGET_POWERPC64 && mode == DImode)
32576 || easy_fp_constant (x, mode)
32577 || easy_vector_constant (x, mode));
32582 /* Expand code to perform a call under the AIX or ELFv2 ABI. */
32584 void
32585 rs6000_call_aix (rtx value, rtx func_desc, rtx flag, rtx cookie)
32587 rtx toc_reg = gen_rtx_REG (Pmode, TOC_REGNUM);
32588 rtx toc_load = NULL_RTX;
32589 rtx toc_restore = NULL_RTX;
32590 rtx func_addr;
32591 rtx abi_reg = NULL_RTX;
32592 rtx call[4];
32593 int n_call;
32594 rtx insn;
32596 /* Handle longcall attributes. */
32597 if (INTVAL (cookie) & CALL_LONG)
32598 func_desc = rs6000_longcall_ref (func_desc);
32600 /* Handle indirect calls. */
32601 if (GET_CODE (func_desc) != SYMBOL_REF
32602 || (DEFAULT_ABI == ABI_AIX && !SYMBOL_REF_FUNCTION_P (func_desc)))
32604 /* Save the TOC into its reserved slot before the call,
32605 and prepare to restore it after the call. */
32606 rtx stack_ptr = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
32607 rtx stack_toc_offset = GEN_INT (RS6000_TOC_SAVE_SLOT);
32608 rtx stack_toc_mem = gen_frame_mem (Pmode,
32609 gen_rtx_PLUS (Pmode, stack_ptr,
32610 stack_toc_offset));
32611 toc_restore = gen_rtx_SET (VOIDmode, toc_reg, stack_toc_mem);
32613 /* Can we optimize saving the TOC in the prologue or
32614 do we need to do it at every call? */
32615 if (TARGET_SAVE_TOC_INDIRECT && !cfun->calls_alloca)
32616 cfun->machine->save_toc_in_prologue = true;
32617 else
32619 MEM_VOLATILE_P (stack_toc_mem) = 1;
32620 emit_move_insn (stack_toc_mem, toc_reg);
32623 if (DEFAULT_ABI == ABI_ELFv2)
32625 /* A function pointer in the ELFv2 ABI is just a plain address, but
32626 the ABI requires it to be loaded into r12 before the call. */
32627 func_addr = gen_rtx_REG (Pmode, 12);
32628 emit_move_insn (func_addr, func_desc);
32629 abi_reg = func_addr;
32631 else
32633 /* A function pointer under AIX is a pointer to a data area whose
32634 first word contains the actual address of the function, whose
32635 second word contains a pointer to its TOC, and whose third word
32636 contains a value to place in the static chain register (r11).
32637 Note that if we load the static chain, our "trampoline" need
32638 not have any executable code. */
32640 /* Load up address of the actual function. */
32641 func_desc = force_reg (Pmode, func_desc);
32642 func_addr = gen_reg_rtx (Pmode);
32643 emit_move_insn (func_addr, gen_rtx_MEM (Pmode, func_desc));
32645 /* Prepare to load the TOC of the called function. Note that the
32646 TOC load must happen immediately before the actual call so
32647 that unwinding the TOC registers works correctly. See the
32648 comment in frob_update_context. */
32649 rtx func_toc_offset = GEN_INT (GET_MODE_SIZE (Pmode));
32650 rtx func_toc_mem = gen_rtx_MEM (Pmode,
32651 gen_rtx_PLUS (Pmode, func_desc,
32652 func_toc_offset));
32653 toc_load = gen_rtx_USE (VOIDmode, func_toc_mem);
32655 /* If we have a static chain, load it up. */
32656 if (TARGET_POINTERS_TO_NESTED_FUNCTIONS)
32658 rtx sc_reg = gen_rtx_REG (Pmode, STATIC_CHAIN_REGNUM);
32659 rtx func_sc_offset = GEN_INT (2 * GET_MODE_SIZE (Pmode));
32660 rtx func_sc_mem = gen_rtx_MEM (Pmode,
32661 gen_rtx_PLUS (Pmode, func_desc,
32662 func_sc_offset));
32663 emit_move_insn (sc_reg, func_sc_mem);
32664 abi_reg = sc_reg;
32668 else
32670 /* Direct calls use the TOC: for local calls, the callee will
32671 assume the TOC register is set; for non-local calls, the
32672 PLT stub needs the TOC register. */
32673 abi_reg = toc_reg;
32674 func_addr = func_desc;
32677 /* Create the call. */
32678 call[0] = gen_rtx_CALL (VOIDmode, gen_rtx_MEM (SImode, func_addr), flag);
32679 if (value != NULL_RTX)
32680 call[0] = gen_rtx_SET (VOIDmode, value, call[0]);
32681 n_call = 1;
32683 if (toc_load)
32684 call[n_call++] = toc_load;
32685 if (toc_restore)
32686 call[n_call++] = toc_restore;
32688 call[n_call++] = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, LR_REGNO));
32690 insn = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (n_call, call));
32691 insn = emit_call_insn (insn);
32693 /* Mention all registers defined by the ABI to hold information
32694 as uses in CALL_INSN_FUNCTION_USAGE. */
32695 if (abi_reg)
32696 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), abi_reg);
32699 /* Expand code to perform a sibling call under the AIX or ELFv2 ABI. */
32701 void
32702 rs6000_sibcall_aix (rtx value, rtx func_desc, rtx flag, rtx cookie)
32704 rtx call[2];
32705 rtx insn;
32707 gcc_assert (INTVAL (cookie) == 0);
32709 /* Create the call. */
32710 call[0] = gen_rtx_CALL (VOIDmode, gen_rtx_MEM (SImode, func_desc), flag);
32711 if (value != NULL_RTX)
32712 call[0] = gen_rtx_SET (VOIDmode, value, call[0]);
32714 call[1] = simple_return_rtx;
32716 insn = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (2, call));
32717 insn = emit_call_insn (insn);
32719 /* Note use of the TOC register. */
32720 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), gen_rtx_REG (Pmode, TOC_REGNUM));
32721 /* We need to also mark a use of the link register since the function we
32722 sibling-call to will use it to return to our caller. */
32723 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), gen_rtx_REG (Pmode, LR_REGNO));
32726 /* Return whether we need to always update the saved TOC pointer when we update
32727 the stack pointer. */
32729 static bool
32730 rs6000_save_toc_in_prologue_p (void)
32732 return (cfun && cfun->machine && cfun->machine->save_toc_in_prologue);
32735 #ifdef HAVE_GAS_HIDDEN
32736 # define USE_HIDDEN_LINKONCE 1
32737 #else
32738 # define USE_HIDDEN_LINKONCE 0
32739 #endif
32741 /* Fills in the label name that should be used for a 476 link stack thunk. */
32743 void
32744 get_ppc476_thunk_name (char name[32])
32746 gcc_assert (TARGET_LINK_STACK);
32748 if (USE_HIDDEN_LINKONCE)
32749 sprintf (name, "__ppc476.get_thunk");
32750 else
32751 ASM_GENERATE_INTERNAL_LABEL (name, "LPPC476_", 0);
32754 /* This function emits the simple thunk routine that is used to preserve
32755 the link stack on the 476 cpu. */
32757 static void rs6000_code_end (void) ATTRIBUTE_UNUSED;
32758 static void
32759 rs6000_code_end (void)
32761 char name[32];
32762 tree decl;
32764 if (!TARGET_LINK_STACK)
32765 return;
32767 get_ppc476_thunk_name (name);
32769 decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL, get_identifier (name),
32770 build_function_type_list (void_type_node, NULL_TREE));
32771 DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
32772 NULL_TREE, void_type_node);
32773 TREE_PUBLIC (decl) = 1;
32774 TREE_STATIC (decl) = 1;
32776 #if RS6000_WEAK
32777 if (USE_HIDDEN_LINKONCE)
32779 cgraph_node::create (decl)->set_comdat_group (DECL_ASSEMBLER_NAME (decl));
32780 targetm.asm_out.unique_section (decl, 0);
32781 switch_to_section (get_named_section (decl, NULL, 0));
32782 DECL_WEAK (decl) = 1;
32783 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
32784 targetm.asm_out.globalize_label (asm_out_file, name);
32785 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
32786 ASM_DECLARE_FUNCTION_NAME (asm_out_file, name, decl);
32788 else
32789 #endif
32791 switch_to_section (text_section);
32792 ASM_OUTPUT_LABEL (asm_out_file, name);
32795 DECL_INITIAL (decl) = make_node (BLOCK);
32796 current_function_decl = decl;
32797 init_function_start (decl);
32798 first_function_block_is_cold = false;
32799 /* Make sure unwind info is emitted for the thunk if needed. */
32800 final_start_function (emit_barrier (), asm_out_file, 1);
32802 fputs ("\tblr\n", asm_out_file);
32804 final_end_function ();
32805 init_insn_lengths ();
32806 free_after_compilation (cfun);
32807 set_cfun (NULL);
32808 current_function_decl = NULL;
32811 /* Add r30 to hard reg set if the prologue sets it up and it is not
32812 pic_offset_table_rtx. */
32814 static void
32815 rs6000_set_up_by_prologue (struct hard_reg_set_container *set)
32817 if (!TARGET_SINGLE_PIC_BASE
32818 && TARGET_TOC
32819 && TARGET_MINIMAL_TOC
32820 && get_pool_size () != 0)
32821 add_to_hard_reg_set (&set->set, Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
32825 /* Helper function for rs6000_split_logical to emit a logical instruction after
32826 spliting the operation to single GPR registers.
32828 DEST is the destination register.
32829 OP1 and OP2 are the input source registers.
32830 CODE is the base operation (AND, IOR, XOR, NOT).
32831 MODE is the machine mode.
32832 If COMPLEMENT_FINAL_P is true, wrap the whole operation with NOT.
32833 If COMPLEMENT_OP1_P is true, wrap operand1 with NOT.
32834 If COMPLEMENT_OP2_P is true, wrap operand2 with NOT. */
32836 static void
32837 rs6000_split_logical_inner (rtx dest,
32838 rtx op1,
32839 rtx op2,
32840 enum rtx_code code,
32841 enum machine_mode mode,
32842 bool complement_final_p,
32843 bool complement_op1_p,
32844 bool complement_op2_p)
32846 rtx bool_rtx;
32848 /* Optimize AND of 0/0xffffffff and IOR/XOR of 0. */
32849 if (op2 && GET_CODE (op2) == CONST_INT
32850 && (mode == SImode || (mode == DImode && TARGET_POWERPC64))
32851 && !complement_final_p && !complement_op1_p && !complement_op2_p)
32853 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
32854 HOST_WIDE_INT value = INTVAL (op2) & mask;
32856 /* Optimize AND of 0 to just set 0. Optimize AND of -1 to be a move. */
32857 if (code == AND)
32859 if (value == 0)
32861 emit_insn (gen_rtx_SET (VOIDmode, dest, const0_rtx));
32862 return;
32865 else if (value == mask)
32867 if (!rtx_equal_p (dest, op1))
32868 emit_insn (gen_rtx_SET (VOIDmode, dest, op1));
32869 return;
32873 /* Optimize IOR/XOR of 0 to be a simple move. Split large operations
32874 into separate ORI/ORIS or XORI/XORIS instrucitons. */
32875 else if (code == IOR || code == XOR)
32877 if (value == 0)
32879 if (!rtx_equal_p (dest, op1))
32880 emit_insn (gen_rtx_SET (VOIDmode, dest, op1));
32881 return;
32886 if (code == AND && mode == SImode
32887 && !complement_final_p && !complement_op1_p && !complement_op2_p)
32889 emit_insn (gen_andsi3 (dest, op1, op2));
32890 return;
32893 if (complement_op1_p)
32894 op1 = gen_rtx_NOT (mode, op1);
32896 if (complement_op2_p)
32897 op2 = gen_rtx_NOT (mode, op2);
32899 bool_rtx = ((code == NOT)
32900 ? gen_rtx_NOT (mode, op1)
32901 : gen_rtx_fmt_ee (code, mode, op1, op2));
32903 if (complement_final_p)
32904 bool_rtx = gen_rtx_NOT (mode, bool_rtx);
32906 emit_insn (gen_rtx_SET (VOIDmode, dest, bool_rtx));
32909 /* Split a DImode AND/IOR/XOR with a constant on a 32-bit system. These
32910 operations are split immediately during RTL generation to allow for more
32911 optimizations of the AND/IOR/XOR.
32913 OPERANDS is an array containing the destination and two input operands.
32914 CODE is the base operation (AND, IOR, XOR, NOT).
32915 MODE is the machine mode.
32916 If COMPLEMENT_FINAL_P is true, wrap the whole operation with NOT.
32917 If COMPLEMENT_OP1_P is true, wrap operand1 with NOT.
32918 If COMPLEMENT_OP2_P is true, wrap operand2 with NOT.
32919 CLOBBER_REG is either NULL or a scratch register of type CC to allow
32920 formation of the AND instructions. */
32922 static void
32923 rs6000_split_logical_di (rtx operands[3],
32924 enum rtx_code code,
32925 bool complement_final_p,
32926 bool complement_op1_p,
32927 bool complement_op2_p)
32929 const HOST_WIDE_INT lower_32bits = HOST_WIDE_INT_C(0xffffffff);
32930 const HOST_WIDE_INT upper_32bits = ~ lower_32bits;
32931 const HOST_WIDE_INT sign_bit = HOST_WIDE_INT_C(0x80000000);
32932 enum hi_lo { hi = 0, lo = 1 };
32933 rtx op0_hi_lo[2], op1_hi_lo[2], op2_hi_lo[2];
32934 size_t i;
32936 op0_hi_lo[hi] = gen_highpart (SImode, operands[0]);
32937 op1_hi_lo[hi] = gen_highpart (SImode, operands[1]);
32938 op0_hi_lo[lo] = gen_lowpart (SImode, operands[0]);
32939 op1_hi_lo[lo] = gen_lowpart (SImode, operands[1]);
32941 if (code == NOT)
32942 op2_hi_lo[hi] = op2_hi_lo[lo] = NULL_RTX;
32943 else
32945 if (GET_CODE (operands[2]) != CONST_INT)
32947 op2_hi_lo[hi] = gen_highpart_mode (SImode, DImode, operands[2]);
32948 op2_hi_lo[lo] = gen_lowpart (SImode, operands[2]);
32950 else
32952 HOST_WIDE_INT value = INTVAL (operands[2]);
32953 HOST_WIDE_INT value_hi_lo[2];
32955 gcc_assert (!complement_final_p);
32956 gcc_assert (!complement_op1_p);
32957 gcc_assert (!complement_op2_p);
32959 value_hi_lo[hi] = value >> 32;
32960 value_hi_lo[lo] = value & lower_32bits;
32962 for (i = 0; i < 2; i++)
32964 HOST_WIDE_INT sub_value = value_hi_lo[i];
32966 if (sub_value & sign_bit)
32967 sub_value |= upper_32bits;
32969 op2_hi_lo[i] = GEN_INT (sub_value);
32971 /* If this is an AND instruction, check to see if we need to load
32972 the value in a register. */
32973 if (code == AND && sub_value != -1 && sub_value != 0
32974 && !and_operand (op2_hi_lo[i], SImode))
32975 op2_hi_lo[i] = force_reg (SImode, op2_hi_lo[i]);
32980 for (i = 0; i < 2; i++)
32982 /* Split large IOR/XOR operations. */
32983 if ((code == IOR || code == XOR)
32984 && GET_CODE (op2_hi_lo[i]) == CONST_INT
32985 && !complement_final_p
32986 && !complement_op1_p
32987 && !complement_op2_p
32988 && !logical_const_operand (op2_hi_lo[i], SImode))
32990 HOST_WIDE_INT value = INTVAL (op2_hi_lo[i]);
32991 HOST_WIDE_INT hi_16bits = value & HOST_WIDE_INT_C(0xffff0000);
32992 HOST_WIDE_INT lo_16bits = value & HOST_WIDE_INT_C(0x0000ffff);
32993 rtx tmp = gen_reg_rtx (SImode);
32995 /* Make sure the constant is sign extended. */
32996 if ((hi_16bits & sign_bit) != 0)
32997 hi_16bits |= upper_32bits;
32999 rs6000_split_logical_inner (tmp, op1_hi_lo[i], GEN_INT (hi_16bits),
33000 code, SImode, false, false, false);
33002 rs6000_split_logical_inner (op0_hi_lo[i], tmp, GEN_INT (lo_16bits),
33003 code, SImode, false, false, false);
33005 else
33006 rs6000_split_logical_inner (op0_hi_lo[i], op1_hi_lo[i], op2_hi_lo[i],
33007 code, SImode, complement_final_p,
33008 complement_op1_p, complement_op2_p);
33011 return;
33014 /* Split the insns that make up boolean operations operating on multiple GPR
33015 registers. The boolean MD patterns ensure that the inputs either are
33016 exactly the same as the output registers, or there is no overlap.
33018 OPERANDS is an array containing the destination and two input operands.
33019 CODE is the base operation (AND, IOR, XOR, NOT).
33020 If COMPLEMENT_FINAL_P is true, wrap the whole operation with NOT.
33021 If COMPLEMENT_OP1_P is true, wrap operand1 with NOT.
33022 If COMPLEMENT_OP2_P is true, wrap operand2 with NOT. */
33024 void
33025 rs6000_split_logical (rtx operands[3],
33026 enum rtx_code code,
33027 bool complement_final_p,
33028 bool complement_op1_p,
33029 bool complement_op2_p)
33031 enum machine_mode mode = GET_MODE (operands[0]);
33032 enum machine_mode sub_mode;
33033 rtx op0, op1, op2;
33034 int sub_size, regno0, regno1, nregs, i;
33036 /* If this is DImode, use the specialized version that can run before
33037 register allocation. */
33038 if (mode == DImode && !TARGET_POWERPC64)
33040 rs6000_split_logical_di (operands, code, complement_final_p,
33041 complement_op1_p, complement_op2_p);
33042 return;
33045 op0 = operands[0];
33046 op1 = operands[1];
33047 op2 = (code == NOT) ? NULL_RTX : operands[2];
33048 sub_mode = (TARGET_POWERPC64) ? DImode : SImode;
33049 sub_size = GET_MODE_SIZE (sub_mode);
33050 regno0 = REGNO (op0);
33051 regno1 = REGNO (op1);
33053 gcc_assert (reload_completed);
33054 gcc_assert (IN_RANGE (regno0, FIRST_GPR_REGNO, LAST_GPR_REGNO));
33055 gcc_assert (IN_RANGE (regno1, FIRST_GPR_REGNO, LAST_GPR_REGNO));
33057 nregs = rs6000_hard_regno_nregs[(int)mode][regno0];
33058 gcc_assert (nregs > 1);
33060 if (op2 && REG_P (op2))
33061 gcc_assert (IN_RANGE (REGNO (op2), FIRST_GPR_REGNO, LAST_GPR_REGNO));
33063 for (i = 0; i < nregs; i++)
33065 int offset = i * sub_size;
33066 rtx sub_op0 = simplify_subreg (sub_mode, op0, mode, offset);
33067 rtx sub_op1 = simplify_subreg (sub_mode, op1, mode, offset);
33068 rtx sub_op2 = ((code == NOT)
33069 ? NULL_RTX
33070 : simplify_subreg (sub_mode, op2, mode, offset));
33072 rs6000_split_logical_inner (sub_op0, sub_op1, sub_op2, code, sub_mode,
33073 complement_final_p, complement_op1_p,
33074 complement_op2_p);
33077 return;
33081 /* Return true if the peephole2 can combine a load involving a combination of
33082 an addis instruction and a load with an offset that can be fused together on
33083 a power8. */
33085 bool
33086 fusion_gpr_load_p (rtx addis_reg, /* register set via addis. */
33087 rtx addis_value, /* addis value. */
33088 rtx target, /* target register that is loaded. */
33089 rtx mem) /* bottom part of the memory addr. */
33091 rtx addr;
33092 rtx base_reg;
33094 /* Validate arguments. */
33095 if (!base_reg_operand (addis_reg, GET_MODE (addis_reg)))
33096 return false;
33098 if (!base_reg_operand (target, GET_MODE (target)))
33099 return false;
33101 if (!fusion_gpr_addis (addis_value, GET_MODE (addis_value)))
33102 return false;
33104 /* Allow sign/zero extension. */
33105 if (GET_CODE (mem) == ZERO_EXTEND
33106 || (GET_CODE (mem) == SIGN_EXTEND && TARGET_P8_FUSION_SIGN))
33107 mem = XEXP (mem, 0);
33109 if (!MEM_P (mem))
33110 return false;
33112 if (!fusion_gpr_mem_load (mem, GET_MODE (mem)))
33113 return false;
33115 addr = XEXP (mem, 0); /* either PLUS or LO_SUM. */
33116 if (GET_CODE (addr) != PLUS && GET_CODE (addr) != LO_SUM)
33117 return false;
33119 /* Validate that the register used to load the high value is either the
33120 register being loaded, or we can safely replace its use.
33122 This function is only called from the peephole2 pass and we assume that
33123 there are 2 instructions in the peephole (addis and load), so we want to
33124 check if the target register was not used in the memory address and the
33125 register to hold the addis result is dead after the peephole. */
33126 if (REGNO (addis_reg) != REGNO (target))
33128 if (reg_mentioned_p (target, mem))
33129 return false;
33131 if (!peep2_reg_dead_p (2, addis_reg))
33132 return false;
33134 /* If the target register being loaded is the stack pointer, we must
33135 avoid loading any other value into it, even temporarily. */
33136 if (REG_P (target) && REGNO (target) == STACK_POINTER_REGNUM)
33137 return false;
33140 base_reg = XEXP (addr, 0);
33141 return REGNO (addis_reg) == REGNO (base_reg);
33144 /* During the peephole2 pass, adjust and expand the insns for a load fusion
33145 sequence. We adjust the addis register to use the target register. If the
33146 load sign extends, we adjust the code to do the zero extending load, and an
33147 explicit sign extension later since the fusion only covers zero extending
33148 loads.
33150 The operands are:
33151 operands[0] register set with addis (to be replaced with target)
33152 operands[1] value set via addis
33153 operands[2] target register being loaded
33154 operands[3] D-form memory reference using operands[0]. */
33156 void
33157 expand_fusion_gpr_load (rtx *operands)
33159 rtx addis_value = operands[1];
33160 rtx target = operands[2];
33161 rtx orig_mem = operands[3];
33162 rtx new_addr, new_mem, orig_addr, offset;
33163 enum rtx_code plus_or_lo_sum;
33164 enum machine_mode target_mode = GET_MODE (target);
33165 enum machine_mode extend_mode = target_mode;
33166 enum machine_mode ptr_mode = Pmode;
33167 enum rtx_code extend = UNKNOWN;
33169 if (GET_CODE (orig_mem) == ZERO_EXTEND
33170 || (TARGET_P8_FUSION_SIGN && GET_CODE (orig_mem) == SIGN_EXTEND))
33172 extend = GET_CODE (orig_mem);
33173 orig_mem = XEXP (orig_mem, 0);
33174 target_mode = GET_MODE (orig_mem);
33177 gcc_assert (MEM_P (orig_mem));
33179 orig_addr = XEXP (orig_mem, 0);
33180 plus_or_lo_sum = GET_CODE (orig_addr);
33181 gcc_assert (plus_or_lo_sum == PLUS || plus_or_lo_sum == LO_SUM);
33183 offset = XEXP (orig_addr, 1);
33184 new_addr = gen_rtx_fmt_ee (plus_or_lo_sum, ptr_mode, addis_value, offset);
33185 new_mem = replace_equiv_address_nv (orig_mem, new_addr, false);
33187 if (extend != UNKNOWN)
33188 new_mem = gen_rtx_fmt_e (ZERO_EXTEND, extend_mode, new_mem);
33190 new_mem = gen_rtx_UNSPEC (extend_mode, gen_rtvec (1, new_mem),
33191 UNSPEC_FUSION_GPR);
33192 emit_insn (gen_rtx_SET (VOIDmode, target, new_mem));
33194 if (extend == SIGN_EXTEND)
33196 int sub_off = ((BYTES_BIG_ENDIAN)
33197 ? GET_MODE_SIZE (extend_mode) - GET_MODE_SIZE (target_mode)
33198 : 0);
33199 rtx sign_reg
33200 = simplify_subreg (target_mode, target, extend_mode, sub_off);
33202 emit_insn (gen_rtx_SET (VOIDmode, target,
33203 gen_rtx_SIGN_EXTEND (extend_mode, sign_reg)));
33206 return;
33209 /* Return a string to fuse an addis instruction with a gpr load to the same
33210 register that we loaded up the addis instruction. The address that is used
33211 is the logical address that was formed during peephole2:
33212 (lo_sum (high) (low-part))
33214 The code is complicated, so we call output_asm_insn directly, and just
33215 return "". */
33217 const char *
33218 emit_fusion_gpr_load (rtx target, rtx mem)
33220 rtx addis_value;
33221 rtx fuse_ops[10];
33222 rtx addr;
33223 rtx load_offset;
33224 const char *addis_str = NULL;
33225 const char *load_str = NULL;
33226 const char *mode_name = NULL;
33227 char insn_template[80];
33228 enum machine_mode mode;
33229 const char *comment_str = ASM_COMMENT_START;
33231 if (GET_CODE (mem) == ZERO_EXTEND)
33232 mem = XEXP (mem, 0);
33234 gcc_assert (REG_P (target) && MEM_P (mem));
33236 if (*comment_str == ' ')
33237 comment_str++;
33239 addr = XEXP (mem, 0);
33240 if (GET_CODE (addr) != PLUS && GET_CODE (addr) != LO_SUM)
33241 gcc_unreachable ();
33243 addis_value = XEXP (addr, 0);
33244 load_offset = XEXP (addr, 1);
33246 /* Now emit the load instruction to the same register. */
33247 mode = GET_MODE (mem);
33248 switch (mode)
33250 case QImode:
33251 mode_name = "char";
33252 load_str = "lbz";
33253 break;
33255 case HImode:
33256 mode_name = "short";
33257 load_str = "lhz";
33258 break;
33260 case SImode:
33261 mode_name = "int";
33262 load_str = "lwz";
33263 break;
33265 case DImode:
33266 gcc_assert (TARGET_POWERPC64);
33267 mode_name = "long";
33268 load_str = "ld";
33269 break;
33271 default:
33272 gcc_unreachable ();
33275 /* Emit the addis instruction. */
33276 fuse_ops[0] = target;
33277 if (satisfies_constraint_L (addis_value))
33279 fuse_ops[1] = addis_value;
33280 addis_str = "lis %0,%v1";
33283 else if (GET_CODE (addis_value) == PLUS)
33285 rtx op0 = XEXP (addis_value, 0);
33286 rtx op1 = XEXP (addis_value, 1);
33288 if (REG_P (op0) && CONST_INT_P (op1)
33289 && satisfies_constraint_L (op1))
33291 fuse_ops[1] = op0;
33292 fuse_ops[2] = op1;
33293 addis_str = "addis %0,%1,%v2";
33297 else if (GET_CODE (addis_value) == HIGH)
33299 rtx value = XEXP (addis_value, 0);
33300 if (GET_CODE (value) == UNSPEC && XINT (value, 1) == UNSPEC_TOCREL)
33302 fuse_ops[1] = XVECEXP (value, 0, 0); /* symbol ref. */
33303 fuse_ops[2] = XVECEXP (value, 0, 1); /* TOC register. */
33304 if (TARGET_ELF)
33305 addis_str = "addis %0,%2,%1@toc@ha";
33307 else if (TARGET_XCOFF)
33308 addis_str = "addis %0,%1@u(%2)";
33310 else
33311 gcc_unreachable ();
33314 else if (GET_CODE (value) == PLUS)
33316 rtx op0 = XEXP (value, 0);
33317 rtx op1 = XEXP (value, 1);
33319 if (GET_CODE (op0) == UNSPEC
33320 && XINT (op0, 1) == UNSPEC_TOCREL
33321 && CONST_INT_P (op1))
33323 fuse_ops[1] = XVECEXP (op0, 0, 0); /* symbol ref. */
33324 fuse_ops[2] = XVECEXP (op0, 0, 1); /* TOC register. */
33325 fuse_ops[3] = op1;
33326 if (TARGET_ELF)
33327 addis_str = "addis %0,%2,%1+%3@toc@ha";
33329 else if (TARGET_XCOFF)
33330 addis_str = "addis %0,%1+%3@u(%2)";
33332 else
33333 gcc_unreachable ();
33337 else if (satisfies_constraint_L (value))
33339 fuse_ops[1] = value;
33340 addis_str = "lis %0,%v1";
33343 else if (TARGET_ELF && !TARGET_POWERPC64 && CONSTANT_P (value))
33345 fuse_ops[1] = value;
33346 addis_str = "lis %0,%1@ha";
33350 if (!addis_str)
33351 fatal_insn ("Could not generate addis value for fusion", addis_value);
33353 sprintf (insn_template, "%s\t\t%s gpr load fusion, type %s", addis_str,
33354 comment_str, mode_name);
33355 output_asm_insn (insn_template, fuse_ops);
33357 /* Emit the D-form load instruction. */
33358 if (CONST_INT_P (load_offset) && satisfies_constraint_I (load_offset))
33360 sprintf (insn_template, "%s %%0,%%1(%%0)", load_str);
33361 fuse_ops[1] = load_offset;
33362 output_asm_insn (insn_template, fuse_ops);
33365 else if (GET_CODE (load_offset) == UNSPEC
33366 && XINT (load_offset, 1) == UNSPEC_TOCREL)
33368 if (TARGET_ELF)
33369 sprintf (insn_template, "%s %%0,%%1@toc@l(%%0)", load_str);
33371 else if (TARGET_XCOFF)
33372 sprintf (insn_template, "%s %%0,%%1@l(%%0)", load_str);
33374 else
33375 gcc_unreachable ();
33377 fuse_ops[1] = XVECEXP (load_offset, 0, 0);
33378 output_asm_insn (insn_template, fuse_ops);
33381 else if (GET_CODE (load_offset) == PLUS
33382 && GET_CODE (XEXP (load_offset, 0)) == UNSPEC
33383 && XINT (XEXP (load_offset, 0), 1) == UNSPEC_TOCREL
33384 && CONST_INT_P (XEXP (load_offset, 1)))
33386 rtx tocrel_unspec = XEXP (load_offset, 0);
33387 if (TARGET_ELF)
33388 sprintf (insn_template, "%s %%0,%%1+%%2@toc@l(%%0)", load_str);
33390 else if (TARGET_XCOFF)
33391 sprintf (insn_template, "%s %%0,%%1+%%2@l(%%0)", load_str);
33393 else
33394 gcc_unreachable ();
33396 fuse_ops[1] = XVECEXP (tocrel_unspec, 0, 0);
33397 fuse_ops[2] = XEXP (load_offset, 1);
33398 output_asm_insn (insn_template, fuse_ops);
33401 else if (TARGET_ELF && !TARGET_POWERPC64 && CONSTANT_P (load_offset))
33403 sprintf (insn_template, "%s %%0,%%1@l(%%0)", load_str);
33405 fuse_ops[1] = load_offset;
33406 output_asm_insn (insn_template, fuse_ops);
33409 else
33410 fatal_insn ("Unable to generate load offset for fusion", load_offset);
33412 return "";
33415 /* Analyze vector computations and remove unnecessary doubleword
33416 swaps (xxswapdi instructions). This pass is performed only
33417 for little-endian VSX code generation.
33419 For this specific case, loads and stores of 4x32 and 2x64 vectors
33420 are inefficient. These are implemented using the lvx2dx and
33421 stvx2dx instructions, which invert the order of doublewords in
33422 a vector register. Thus the code generation inserts an xxswapdi
33423 after each such load, and prior to each such store. (For spill
33424 code after register assignment, an additional xxswapdi is inserted
33425 following each store in order to return a hard register to its
33426 unpermuted value.)
33428 The extra xxswapdi instructions reduce performance. This can be
33429 particularly bad for vectorized code. The purpose of this pass
33430 is to reduce the number of xxswapdi instructions required for
33431 correctness.
33433 The primary insight is that much code that operates on vectors
33434 does not care about the relative order of elements in a register,
33435 so long as the correct memory order is preserved. If we have
33436 a computation where all input values are provided by lvxd2x/xxswapdi
33437 sequences, all outputs are stored using xxswapdi/stvxd2x sequences,
33438 and all intermediate computations are pure SIMD (independent of
33439 element order), then all the xxswapdi's associated with the loads
33440 and stores may be removed.
33442 This pass uses some of the infrastructure and logical ideas from
33443 the "web" pass in web.c. We create maximal webs of computations
33444 fitting the description above using union-find. Each such web is
33445 then optimized by removing its unnecessary xxswapdi instructions.
33447 The pass is placed prior to global optimization so that we can
33448 perform the optimization in the safest and simplest way possible;
33449 that is, by replacing each xxswapdi insn with a register copy insn.
33450 Subsequent forward propagation will remove copies where possible.
33452 There are some operations sensitive to element order for which we
33453 can still allow the operation, provided we modify those operations.
33454 These include CONST_VECTORs, for which we must swap the first and
33455 second halves of the constant vector; and SUBREGs, for which we
33456 must adjust the byte offset to account for the swapped doublewords.
33457 A remaining opportunity would be non-immediate-form splats, for
33458 which we should adjust the selected lane of the input. We should
33459 also make code generation adjustments for sum-across operations,
33460 since this is a common vectorizer reduction.
33462 Because we run prior to the first split, we can see loads and stores
33463 here that match *vsx_le_perm_{load,store}_<mode>. These are vanilla
33464 vector loads and stores that have not yet been split into a permuting
33465 load/store and a swap. (One way this can happen is with a builtin
33466 call to vec_vsx_{ld,st}.) We can handle these as well, but rather
33467 than deleting a swap, we convert the load/store into a permuting
33468 load/store (which effectively removes the swap). */
33470 /* Notes on Permutes
33472 We do not currently handle computations that contain permutes. There
33473 is a general transformation that can be performed correctly, but it
33474 may introduce more expensive code than it replaces. To handle these
33475 would require a cost model to determine when to perform the optimization.
33476 This commentary records how this could be done if desired.
33478 The most general permute is something like this (example for V16QI):
33480 (vec_select:V16QI (vec_concat:V32QI (op1:V16QI) (op2:V16QI))
33481 (parallel [(const_int a0) (const_int a1)
33483 (const_int a14) (const_int a15)]))
33485 where a0,...,a15 are in [0,31] and select elements from op1 and op2
33486 to produce in the result.
33488 Regardless of mode, we can convert the PARALLEL to a mask of 16
33489 byte-element selectors. Let's call this M, with M[i] representing
33490 the ith byte-element selector value. Then if we swap doublewords
33491 throughout the computation, we can get correct behavior by replacing
33492 M with M' as follows:
33494 { M[i+8]+8 : i < 8, M[i+8] in [0,7] U [16,23]
33495 M'[i] = { M[i+8]-8 : i < 8, M[i+8] in [8,15] U [24,31]
33496 { M[i-8]+8 : i >= 8, M[i-8] in [0,7] U [16,23]
33497 { M[i-8]-8 : i >= 8, M[i-8] in [8,15] U [24,31]
33499 This seems promising at first, since we are just replacing one mask
33500 with another. But certain masks are preferable to others. If M
33501 is a mask that matches a vmrghh pattern, for example, M' certainly
33502 will not. Instead of a single vmrghh, we would generate a load of
33503 M' and a vperm. So we would need to know how many xxswapd's we can
33504 remove as a result of this transformation to determine if it's
33505 profitable; and preferably the logic would need to be aware of all
33506 the special preferable masks.
33508 Another form of permute is an UNSPEC_VPERM, in which the mask is
33509 already in a register. In some cases, this mask may be a constant
33510 that we can discover with ud-chains, in which case the above
33511 transformation is ok. However, the common usage here is for the
33512 mask to be produced by an UNSPEC_LVSL, in which case the mask
33513 cannot be known at compile time. In such a case we would have to
33514 generate several instructions to compute M' as above at run time,
33515 and a cost model is needed again. */
33517 /* This is based on the union-find logic in web.c. web_entry_base is
33518 defined in df.h. */
33519 class swap_web_entry : public web_entry_base
33521 public:
33522 /* Pointer to the insn. */
33523 rtx_insn *insn;
33524 /* Set if insn contains a mention of a vector register. All other
33525 fields are undefined if this field is unset. */
33526 unsigned int is_relevant : 1;
33527 /* Set if insn is a load. */
33528 unsigned int is_load : 1;
33529 /* Set if insn is a store. */
33530 unsigned int is_store : 1;
33531 /* Set if insn is a doubleword swap. This can either be a register swap
33532 or a permuting load or store (test is_load and is_store for this). */
33533 unsigned int is_swap : 1;
33534 /* Set if the insn has a live-in use of a parameter register. */
33535 unsigned int is_live_in : 1;
33536 /* Set if the insn has a live-out def of a return register. */
33537 unsigned int is_live_out : 1;
33538 /* Set if the insn contains a subreg reference of a vector register. */
33539 unsigned int contains_subreg : 1;
33540 /* Set if the insn contains a 128-bit integer operand. */
33541 unsigned int is_128_int : 1;
33542 /* Set if this is a call-insn. */
33543 unsigned int is_call : 1;
33544 /* Set if this insn does not perform a vector operation for which
33545 element order matters, or if we know how to fix it up if it does.
33546 Undefined if is_swap is set. */
33547 unsigned int is_swappable : 1;
33548 /* A nonzero value indicates what kind of special handling for this
33549 insn is required if doublewords are swapped. Undefined if
33550 is_swappable is not set. */
33551 unsigned int special_handling : 3;
33552 /* Set if the web represented by this entry cannot be optimized. */
33553 unsigned int web_not_optimizable : 1;
33554 /* Set if this insn should be deleted. */
33555 unsigned int will_delete : 1;
33558 enum special_handling_values {
33559 SH_NONE = 0,
33560 SH_CONST_VECTOR,
33561 SH_SUBREG,
33562 SH_NOSWAP_LD,
33563 SH_NOSWAP_ST,
33564 SH_EXTRACT,
33565 SH_SPLAT
33568 /* Union INSN with all insns containing definitions that reach USE.
33569 Detect whether USE is live-in to the current function. */
33570 static void
33571 union_defs (swap_web_entry *insn_entry, rtx insn, df_ref use)
33573 struct df_link *link = DF_REF_CHAIN (use);
33575 if (!link)
33576 insn_entry[INSN_UID (insn)].is_live_in = 1;
33578 while (link)
33580 if (DF_REF_IS_ARTIFICIAL (link->ref))
33581 insn_entry[INSN_UID (insn)].is_live_in = 1;
33583 if (DF_REF_INSN_INFO (link->ref))
33585 rtx def_insn = DF_REF_INSN (link->ref);
33586 (void)unionfind_union (insn_entry + INSN_UID (insn),
33587 insn_entry + INSN_UID (def_insn));
33590 link = link->next;
33594 /* Union INSN with all insns containing uses reached from DEF.
33595 Detect whether DEF is live-out from the current function. */
33596 static void
33597 union_uses (swap_web_entry *insn_entry, rtx insn, df_ref def)
33599 struct df_link *link = DF_REF_CHAIN (def);
33601 if (!link)
33602 insn_entry[INSN_UID (insn)].is_live_out = 1;
33604 while (link)
33606 /* This could be an eh use or some other artificial use;
33607 we treat these all the same (killing the optimization). */
33608 if (DF_REF_IS_ARTIFICIAL (link->ref))
33609 insn_entry[INSN_UID (insn)].is_live_out = 1;
33611 if (DF_REF_INSN_INFO (link->ref))
33613 rtx use_insn = DF_REF_INSN (link->ref);
33614 (void)unionfind_union (insn_entry + INSN_UID (insn),
33615 insn_entry + INSN_UID (use_insn));
33618 link = link->next;
33622 /* Return 1 iff INSN is a load insn, including permuting loads that
33623 represent an lvxd2x instruction; else return 0. */
33624 static unsigned int
33625 insn_is_load_p (rtx insn)
33627 rtx body = PATTERN (insn);
33629 if (GET_CODE (body) == SET)
33631 if (GET_CODE (SET_SRC (body)) == MEM)
33632 return 1;
33634 if (GET_CODE (SET_SRC (body)) == VEC_SELECT
33635 && GET_CODE (XEXP (SET_SRC (body), 0)) == MEM)
33636 return 1;
33638 return 0;
33641 if (GET_CODE (body) != PARALLEL)
33642 return 0;
33644 rtx set = XVECEXP (body, 0, 0);
33646 if (GET_CODE (set) == SET && GET_CODE (SET_SRC (set)) == MEM)
33647 return 1;
33649 return 0;
33652 /* Return 1 iff INSN is a store insn, including permuting stores that
33653 represent an stvxd2x instruction; else return 0. */
33654 static unsigned int
33655 insn_is_store_p (rtx insn)
33657 rtx body = PATTERN (insn);
33658 if (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == MEM)
33659 return 1;
33660 if (GET_CODE (body) != PARALLEL)
33661 return 0;
33662 rtx set = XVECEXP (body, 0, 0);
33663 if (GET_CODE (set) == SET && GET_CODE (SET_DEST (set)) == MEM)
33664 return 1;
33665 return 0;
33668 /* Return 1 iff INSN swaps doublewords. This may be a reg-reg swap,
33669 a permuting load, or a permuting store. */
33670 static unsigned int
33671 insn_is_swap_p (rtx insn)
33673 rtx body = PATTERN (insn);
33674 if (GET_CODE (body) != SET)
33675 return 0;
33676 rtx rhs = SET_SRC (body);
33677 if (GET_CODE (rhs) != VEC_SELECT)
33678 return 0;
33679 rtx parallel = XEXP (rhs, 1);
33680 if (GET_CODE (parallel) != PARALLEL)
33681 return 0;
33682 unsigned int len = XVECLEN (parallel, 0);
33683 if (len != 2 && len != 4 && len != 8 && len != 16)
33684 return 0;
33685 for (unsigned int i = 0; i < len / 2; ++i)
33687 rtx op = XVECEXP (parallel, 0, i);
33688 if (GET_CODE (op) != CONST_INT || INTVAL (op) != len / 2 + i)
33689 return 0;
33691 for (unsigned int i = len / 2; i < len; ++i)
33693 rtx op = XVECEXP (parallel, 0, i);
33694 if (GET_CODE (op) != CONST_INT || INTVAL (op) != i - len / 2)
33695 return 0;
33697 return 1;
33700 /* Return 1 iff OP is an operand that will not be affected by having
33701 vector doublewords swapped in memory. */
33702 static unsigned int
33703 rtx_is_swappable_p (rtx op, unsigned int *special)
33705 enum rtx_code code = GET_CODE (op);
33706 int i, j;
33707 rtx parallel;
33709 switch (code)
33711 case LABEL_REF:
33712 case SYMBOL_REF:
33713 case CLOBBER:
33714 case REG:
33715 return 1;
33717 case VEC_CONCAT:
33718 case ASM_INPUT:
33719 case ASM_OPERANDS:
33720 return 0;
33722 case CONST_VECTOR:
33724 *special = SH_CONST_VECTOR;
33725 return 1;
33728 case VEC_DUPLICATE:
33729 /* Opportunity: If XEXP (op, 0) has the same mode as the result,
33730 and XEXP (op, 1) is a PARALLEL with a single QImode const int,
33731 it represents a vector splat for which we can do special
33732 handling. */
33733 if (GET_CODE (XEXP (op, 0)) == CONST_INT)
33734 return 1;
33735 else if (GET_CODE (XEXP (op, 0)) == REG
33736 && GET_MODE_INNER (GET_MODE (op)) == GET_MODE (XEXP (op, 0)))
33737 /* This catches V2DF and V2DI splat, at a minimum. */
33738 return 1;
33739 else if (GET_CODE (XEXP (op, 0)) == VEC_SELECT)
33740 /* If the duplicated item is from a select, defer to the select
33741 processing to see if we can change the lane for the splat. */
33742 return rtx_is_swappable_p (XEXP (op, 0), special);
33743 else
33744 return 0;
33746 case VEC_SELECT:
33747 /* A vec_extract operation is ok if we change the lane. */
33748 if (GET_CODE (XEXP (op, 0)) == REG
33749 && GET_MODE_INNER (GET_MODE (XEXP (op, 0))) == GET_MODE (op)
33750 && GET_CODE ((parallel = XEXP (op, 1))) == PARALLEL
33751 && XVECLEN (parallel, 0) == 1
33752 && GET_CODE (XVECEXP (parallel, 0, 0)) == CONST_INT)
33754 *special = SH_EXTRACT;
33755 return 1;
33757 else
33758 return 0;
33760 case UNSPEC:
33762 /* Various operations are unsafe for this optimization, at least
33763 without significant additional work. Permutes are obviously
33764 problematic, as both the permute control vector and the ordering
33765 of the target values are invalidated by doubleword swapping.
33766 Vector pack and unpack modify the number of vector lanes.
33767 Merge-high/low will not operate correctly on swapped operands.
33768 Vector shifts across element boundaries are clearly uncool,
33769 as are vector select and concatenate operations. Vector
33770 sum-across instructions define one operand with a specific
33771 order-dependent element, so additional fixup code would be
33772 needed to make those work. Vector set and non-immediate-form
33773 vector splat are element-order sensitive. A few of these
33774 cases might be workable with special handling if required. */
33775 int val = XINT (op, 1);
33776 switch (val)
33778 default:
33779 break;
33780 case UNSPEC_VMRGH_DIRECT:
33781 case UNSPEC_VMRGL_DIRECT:
33782 case UNSPEC_VPACK_SIGN_SIGN_SAT:
33783 case UNSPEC_VPACK_SIGN_UNS_SAT:
33784 case UNSPEC_VPACK_UNS_UNS_MOD:
33785 case UNSPEC_VPACK_UNS_UNS_MOD_DIRECT:
33786 case UNSPEC_VPACK_UNS_UNS_SAT:
33787 case UNSPEC_VPERM:
33788 case UNSPEC_VPERM_UNS:
33789 case UNSPEC_VPERMHI:
33790 case UNSPEC_VPERMSI:
33791 case UNSPEC_VPKPX:
33792 case UNSPEC_VSLDOI:
33793 case UNSPEC_VSLO:
33794 case UNSPEC_VSRO:
33795 case UNSPEC_VSUM2SWS:
33796 case UNSPEC_VSUM4S:
33797 case UNSPEC_VSUM4UBS:
33798 case UNSPEC_VSUMSWS:
33799 case UNSPEC_VSUMSWS_DIRECT:
33800 case UNSPEC_VSX_CONCAT:
33801 case UNSPEC_VSX_SET:
33802 case UNSPEC_VSX_SLDWI:
33803 case UNSPEC_VUNPACK_HI_SIGN:
33804 case UNSPEC_VUNPACK_HI_SIGN_DIRECT:
33805 case UNSPEC_VUNPACK_LO_SIGN:
33806 case UNSPEC_VUNPACK_LO_SIGN_DIRECT:
33807 case UNSPEC_VUPKHPX:
33808 case UNSPEC_VUPKHS_V4SF:
33809 case UNSPEC_VUPKHU_V4SF:
33810 case UNSPEC_VUPKLPX:
33811 case UNSPEC_VUPKLS_V4SF:
33812 case UNSPEC_VUPKLU_V4SF:
33813 /* The following could be handled as an idiom with XXSPLTW.
33814 These place a scalar in BE element zero, but the XXSPLTW
33815 will currently expect it in BE element 2 in a swapped
33816 region. When one of these feeds an XXSPLTW with no other
33817 defs/uses either way, we can avoid the lane change for
33818 XXSPLTW and things will be correct. TBD. */
33819 case UNSPEC_VSX_CVDPSPN:
33820 case UNSPEC_VSX_CVSPDP:
33821 case UNSPEC_VSX_CVSPDPN:
33822 return 0;
33823 case UNSPEC_VSPLT_DIRECT:
33824 *special = SH_SPLAT;
33825 return 1;
33829 default:
33830 break;
33833 const char *fmt = GET_RTX_FORMAT (code);
33834 int ok = 1;
33836 for (i = 0; i < GET_RTX_LENGTH (code); ++i)
33837 if (fmt[i] == 'e' || fmt[i] == 'u')
33839 unsigned int special_op = SH_NONE;
33840 ok &= rtx_is_swappable_p (XEXP (op, i), &special_op);
33841 /* Ensure we never have two kinds of special handling
33842 for the same insn. */
33843 if (*special != SH_NONE && special_op != SH_NONE
33844 && *special != special_op)
33845 return 0;
33846 *special = special_op;
33848 else if (fmt[i] == 'E')
33849 for (j = 0; j < XVECLEN (op, i); ++j)
33851 unsigned int special_op = SH_NONE;
33852 ok &= rtx_is_swappable_p (XVECEXP (op, i, j), &special_op);
33853 /* Ensure we never have two kinds of special handling
33854 for the same insn. */
33855 if (*special != SH_NONE && special_op != SH_NONE
33856 && *special != special_op)
33857 return 0;
33858 *special = special_op;
33861 return ok;
33864 /* Return 1 iff INSN is an operand that will not be affected by
33865 having vector doublewords swapped in memory (in which case
33866 *SPECIAL is unchanged), or that can be modified to be correct
33867 if vector doublewords are swapped in memory (in which case
33868 *SPECIAL is changed to a value indicating how). */
33869 static unsigned int
33870 insn_is_swappable_p (swap_web_entry *insn_entry, rtx insn,
33871 unsigned int *special)
33873 /* Calls are always bad. */
33874 if (GET_CODE (insn) == CALL_INSN)
33875 return 0;
33877 /* Loads and stores seen here are not permuting, but we can still
33878 fix them up by converting them to permuting ones. Exceptions:
33879 UNSPEC_LVE, UNSPEC_LVX, and UNSPEC_STVX, which have a PARALLEL
33880 body instead of a SET; and UNSPEC_STVE, which has an UNSPEC
33881 for the SET source. */
33882 rtx body = PATTERN (insn);
33883 int i = INSN_UID (insn);
33885 if (insn_entry[i].is_load)
33887 if (GET_CODE (body) == SET)
33889 *special = SH_NOSWAP_LD;
33890 return 1;
33892 else
33893 return 0;
33896 if (insn_entry[i].is_store)
33898 if (GET_CODE (body) == SET && GET_CODE (SET_SRC (body)) != UNSPEC)
33900 *special = SH_NOSWAP_ST;
33901 return 1;
33903 else
33904 return 0;
33907 /* Otherwise check the operands for vector lane violations. */
33908 return rtx_is_swappable_p (body, special);
33911 enum chain_purpose { FOR_LOADS, FOR_STORES };
33913 /* Return true if the UD or DU chain headed by LINK is non-empty,
33914 and every entry on the chain references an insn that is a
33915 register swap. Furthermore, if PURPOSE is FOR_LOADS, each such
33916 register swap must have only permuting loads as reaching defs.
33917 If PURPOSE is FOR_STORES, each such register swap must have only
33918 register swaps or permuting stores as reached uses. */
33919 static bool
33920 chain_contains_only_swaps (swap_web_entry *insn_entry, struct df_link *link,
33921 enum chain_purpose purpose)
33923 if (!link)
33924 return false;
33926 for (; link; link = link->next)
33928 if (!VECTOR_MODE_P (GET_MODE (DF_REF_REG (link->ref))))
33929 continue;
33931 if (DF_REF_IS_ARTIFICIAL (link->ref))
33932 return false;
33934 rtx reached_insn = DF_REF_INSN (link->ref);
33935 unsigned uid = INSN_UID (reached_insn);
33936 struct df_insn_info *insn_info = DF_INSN_INFO_GET (reached_insn);
33938 if (!insn_entry[uid].is_swap || insn_entry[uid].is_load
33939 || insn_entry[uid].is_store)
33940 return false;
33942 if (purpose == FOR_LOADS)
33944 df_ref use;
33945 FOR_EACH_INSN_INFO_USE (use, insn_info)
33947 struct df_link *swap_link = DF_REF_CHAIN (use);
33949 while (swap_link)
33951 if (DF_REF_IS_ARTIFICIAL (link->ref))
33952 return false;
33954 rtx swap_def_insn = DF_REF_INSN (swap_link->ref);
33955 unsigned uid2 = INSN_UID (swap_def_insn);
33957 /* Only permuting loads are allowed. */
33958 if (!insn_entry[uid2].is_swap || !insn_entry[uid2].is_load)
33959 return false;
33961 swap_link = swap_link->next;
33965 else if (purpose == FOR_STORES)
33967 df_ref def;
33968 FOR_EACH_INSN_INFO_DEF (def, insn_info)
33970 struct df_link *swap_link = DF_REF_CHAIN (def);
33972 while (swap_link)
33974 if (DF_REF_IS_ARTIFICIAL (link->ref))
33975 return false;
33977 rtx swap_use_insn = DF_REF_INSN (swap_link->ref);
33978 unsigned uid2 = INSN_UID (swap_use_insn);
33980 /* Permuting stores or register swaps are allowed. */
33981 if (!insn_entry[uid2].is_swap || insn_entry[uid2].is_load)
33982 return false;
33984 swap_link = swap_link->next;
33990 return true;
33993 /* Mark the xxswapdi instructions associated with permuting loads and
33994 stores for removal. Note that we only flag them for deletion here,
33995 as there is a possibility of a swap being reached from multiple
33996 loads, etc. */
33997 static void
33998 mark_swaps_for_removal (swap_web_entry *insn_entry, unsigned int i)
34000 rtx insn = insn_entry[i].insn;
34001 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
34003 if (insn_entry[i].is_load)
34005 df_ref def;
34006 FOR_EACH_INSN_INFO_DEF (def, insn_info)
34008 struct df_link *link = DF_REF_CHAIN (def);
34010 /* We know by now that these are swaps, so we can delete
34011 them confidently. */
34012 while (link)
34014 rtx use_insn = DF_REF_INSN (link->ref);
34015 insn_entry[INSN_UID (use_insn)].will_delete = 1;
34016 link = link->next;
34020 else if (insn_entry[i].is_store)
34022 df_ref use;
34023 FOR_EACH_INSN_INFO_USE (use, insn_info)
34025 /* Ignore uses for addressability. */
34026 enum machine_mode mode = GET_MODE (DF_REF_REG (use));
34027 if (!VECTOR_MODE_P (mode))
34028 continue;
34030 struct df_link *link = DF_REF_CHAIN (use);
34032 /* We know by now that these are swaps, so we can delete
34033 them confidently. */
34034 while (link)
34036 rtx def_insn = DF_REF_INSN (link->ref);
34037 insn_entry[INSN_UID (def_insn)].will_delete = 1;
34038 link = link->next;
34044 /* OP is either a CONST_VECTOR or an expression containing one.
34045 Swap the first half of the vector with the second in the first
34046 case. Recurse to find it in the second. */
34047 static void
34048 swap_const_vector_halves (rtx op)
34050 int i;
34051 enum rtx_code code = GET_CODE (op);
34052 if (GET_CODE (op) == CONST_VECTOR)
34054 int half_units = GET_MODE_NUNITS (GET_MODE (op)) / 2;
34055 for (i = 0; i < half_units; ++i)
34057 rtx temp = CONST_VECTOR_ELT (op, i);
34058 CONST_VECTOR_ELT (op, i) = CONST_VECTOR_ELT (op, i + half_units);
34059 CONST_VECTOR_ELT (op, i + half_units) = temp;
34062 else
34064 int j;
34065 const char *fmt = GET_RTX_FORMAT (code);
34066 for (i = 0; i < GET_RTX_LENGTH (code); ++i)
34067 if (fmt[i] == 'e' || fmt[i] == 'u')
34068 swap_const_vector_halves (XEXP (op, i));
34069 else if (fmt[i] == 'E')
34070 for (j = 0; j < XVECLEN (op, i); ++j)
34071 swap_const_vector_halves (XVECEXP (op, i, j));
34075 /* Find all subregs of a vector expression that perform a narrowing,
34076 and adjust the subreg index to account for doubleword swapping. */
34077 static void
34078 adjust_subreg_index (rtx op)
34080 enum rtx_code code = GET_CODE (op);
34081 if (code == SUBREG
34082 && (GET_MODE_SIZE (GET_MODE (op))
34083 < GET_MODE_SIZE (GET_MODE (XEXP (op, 0)))))
34085 unsigned int index = SUBREG_BYTE (op);
34086 if (index < 8)
34087 index += 8;
34088 else
34089 index -= 8;
34090 SUBREG_BYTE (op) = index;
34093 const char *fmt = GET_RTX_FORMAT (code);
34094 int i,j;
34095 for (i = 0; i < GET_RTX_LENGTH (code); ++i)
34096 if (fmt[i] == 'e' || fmt[i] == 'u')
34097 adjust_subreg_index (XEXP (op, i));
34098 else if (fmt[i] == 'E')
34099 for (j = 0; j < XVECLEN (op, i); ++j)
34100 adjust_subreg_index (XVECEXP (op, i, j));
34103 /* Convert the non-permuting load INSN to a permuting one. */
34104 static void
34105 permute_load (rtx_insn *insn)
34107 rtx body = PATTERN (insn);
34108 rtx mem_op = SET_SRC (body);
34109 rtx tgt_reg = SET_DEST (body);
34110 enum machine_mode mode = GET_MODE (tgt_reg);
34111 int n_elts = GET_MODE_NUNITS (mode);
34112 int half_elts = n_elts / 2;
34113 rtx par = gen_rtx_PARALLEL (mode, rtvec_alloc (n_elts));
34114 int i, j;
34115 for (i = 0, j = half_elts; i < half_elts; ++i, ++j)
34116 XVECEXP (par, 0, i) = GEN_INT (j);
34117 for (i = half_elts, j = 0; j < half_elts; ++i, ++j)
34118 XVECEXP (par, 0, i) = GEN_INT (j);
34119 rtx sel = gen_rtx_VEC_SELECT (mode, mem_op, par);
34120 SET_SRC (body) = sel;
34121 INSN_CODE (insn) = -1; /* Force re-recognition. */
34122 df_insn_rescan (insn);
34124 if (dump_file)
34125 fprintf (dump_file, "Replacing load %d with permuted load\n",
34126 INSN_UID (insn));
34129 /* Convert the non-permuting store INSN to a permuting one. */
34130 static void
34131 permute_store (rtx_insn *insn)
34133 rtx body = PATTERN (insn);
34134 rtx src_reg = SET_SRC (body);
34135 enum machine_mode mode = GET_MODE (src_reg);
34136 int n_elts = GET_MODE_NUNITS (mode);
34137 int half_elts = n_elts / 2;
34138 rtx par = gen_rtx_PARALLEL (mode, rtvec_alloc (n_elts));
34139 int i, j;
34140 for (i = 0, j = half_elts; i < half_elts; ++i, ++j)
34141 XVECEXP (par, 0, i) = GEN_INT (j);
34142 for (i = half_elts, j = 0; j < half_elts; ++i, ++j)
34143 XVECEXP (par, 0, i) = GEN_INT (j);
34144 rtx sel = gen_rtx_VEC_SELECT (mode, src_reg, par);
34145 SET_SRC (body) = sel;
34146 INSN_CODE (insn) = -1; /* Force re-recognition. */
34147 df_insn_rescan (insn);
34149 if (dump_file)
34150 fprintf (dump_file, "Replacing store %d with permuted store\n",
34151 INSN_UID (insn));
34154 /* Given OP that contains a vector extract operation, adjust the index
34155 of the extracted lane to account for the doubleword swap. */
34156 static void
34157 adjust_extract (rtx_insn *insn)
34159 rtx src = SET_SRC (PATTERN (insn));
34160 /* The vec_select may be wrapped in a vec_duplicate for a splat, so
34161 account for that. */
34162 rtx sel = GET_CODE (src) == VEC_DUPLICATE ? XEXP (src, 0) : src;
34163 rtx par = XEXP (sel, 1);
34164 int half_elts = GET_MODE_NUNITS (GET_MODE (XEXP (sel, 0))) >> 1;
34165 int lane = INTVAL (XVECEXP (par, 0, 0));
34166 lane = lane >= half_elts ? lane - half_elts : lane + half_elts;
34167 XVECEXP (par, 0, 0) = GEN_INT (lane);
34168 INSN_CODE (insn) = -1; /* Force re-recognition. */
34169 df_insn_rescan (insn);
34171 if (dump_file)
34172 fprintf (dump_file, "Changing lane for extract %d\n", INSN_UID (insn));
34175 /* Given OP that contains a vector direct-splat operation, adjust the index
34176 of the source lane to account for the doubleword swap. */
34177 static void
34178 adjust_splat (rtx_insn *insn)
34180 rtx body = PATTERN (insn);
34181 rtx unspec = XEXP (body, 1);
34182 int half_elts = GET_MODE_NUNITS (GET_MODE (unspec)) >> 1;
34183 int lane = INTVAL (XVECEXP (unspec, 0, 1));
34184 lane = lane >= half_elts ? lane - half_elts : lane + half_elts;
34185 XVECEXP (unspec, 0, 1) = GEN_INT (lane);
34186 INSN_CODE (insn) = -1; /* Force re-recognition. */
34187 df_insn_rescan (insn);
34189 if (dump_file)
34190 fprintf (dump_file, "Changing lane for splat %d\n", INSN_UID (insn));
34193 /* The insn described by INSN_ENTRY[I] can be swapped, but only
34194 with special handling. Take care of that here. */
34195 static void
34196 handle_special_swappables (swap_web_entry *insn_entry, unsigned i)
34198 rtx_insn *insn = insn_entry[i].insn;
34199 rtx body = PATTERN (insn);
34201 switch (insn_entry[i].special_handling)
34203 default:
34204 gcc_unreachable ();
34205 case SH_CONST_VECTOR:
34207 /* A CONST_VECTOR will only show up somewhere in the RHS of a SET. */
34208 gcc_assert (GET_CODE (body) == SET);
34209 rtx rhs = SET_SRC (body);
34210 swap_const_vector_halves (rhs);
34211 if (dump_file)
34212 fprintf (dump_file, "Swapping constant halves in insn %d\n", i);
34213 break;
34215 case SH_SUBREG:
34216 /* A subreg of the same size is already safe. For subregs that
34217 select a smaller portion of a reg, adjust the index for
34218 swapped doublewords. */
34219 adjust_subreg_index (body);
34220 if (dump_file)
34221 fprintf (dump_file, "Adjusting subreg in insn %d\n", i);
34222 break;
34223 case SH_NOSWAP_LD:
34224 /* Convert a non-permuting load to a permuting one. */
34225 permute_load (insn);
34226 break;
34227 case SH_NOSWAP_ST:
34228 /* Convert a non-permuting store to a permuting one. */
34229 permute_store (insn);
34230 break;
34231 case SH_EXTRACT:
34232 /* Change the lane on an extract operation. */
34233 adjust_extract (insn);
34234 break;
34235 case SH_SPLAT:
34236 /* Change the lane on a direct-splat operation. */
34237 adjust_splat (insn);
34238 break;
34242 /* Find the insn from the Ith table entry, which is known to be a
34243 register swap Y = SWAP(X). Replace it with a copy Y = X. */
34244 static void
34245 replace_swap_with_copy (swap_web_entry *insn_entry, unsigned i)
34247 rtx_insn *insn = insn_entry[i].insn;
34248 rtx body = PATTERN (insn);
34249 rtx src_reg = XEXP (SET_SRC (body), 0);
34250 rtx copy = gen_rtx_SET (VOIDmode, SET_DEST (body), src_reg);
34251 rtx_insn *new_insn = emit_insn_before (copy, insn);
34252 set_block_for_insn (new_insn, BLOCK_FOR_INSN (insn));
34253 df_insn_rescan (new_insn);
34255 if (dump_file)
34257 unsigned int new_uid = INSN_UID (new_insn);
34258 fprintf (dump_file, "Replacing swap %d with copy %d\n", i, new_uid);
34261 df_insn_delete (insn);
34262 remove_insn (insn);
34263 insn->set_deleted ();
34266 /* Dump the swap table to DUMP_FILE. */
34267 static void
34268 dump_swap_insn_table (swap_web_entry *insn_entry)
34270 int e = get_max_uid ();
34271 fprintf (dump_file, "\nRelevant insns with their flag settings\n\n");
34273 for (int i = 0; i < e; ++i)
34274 if (insn_entry[i].is_relevant)
34276 swap_web_entry *pred_entry = (swap_web_entry *)insn_entry[i].pred ();
34277 fprintf (dump_file, "%6d %6d ", i,
34278 pred_entry && pred_entry->insn
34279 ? INSN_UID (pred_entry->insn) : 0);
34280 if (insn_entry[i].is_load)
34281 fputs ("load ", dump_file);
34282 if (insn_entry[i].is_store)
34283 fputs ("store ", dump_file);
34284 if (insn_entry[i].is_swap)
34285 fputs ("swap ", dump_file);
34286 if (insn_entry[i].is_live_in)
34287 fputs ("live-in ", dump_file);
34288 if (insn_entry[i].is_live_out)
34289 fputs ("live-out ", dump_file);
34290 if (insn_entry[i].contains_subreg)
34291 fputs ("subreg ", dump_file);
34292 if (insn_entry[i].is_128_int)
34293 fputs ("int128 ", dump_file);
34294 if (insn_entry[i].is_call)
34295 fputs ("call ", dump_file);
34296 if (insn_entry[i].is_swappable)
34298 fputs ("swappable ", dump_file);
34299 if (insn_entry[i].special_handling == SH_CONST_VECTOR)
34300 fputs ("special:constvec ", dump_file);
34301 else if (insn_entry[i].special_handling == SH_SUBREG)
34302 fputs ("special:subreg ", dump_file);
34303 else if (insn_entry[i].special_handling == SH_NOSWAP_LD)
34304 fputs ("special:load ", dump_file);
34305 else if (insn_entry[i].special_handling == SH_NOSWAP_ST)
34306 fputs ("special:store ", dump_file);
34307 else if (insn_entry[i].special_handling == SH_EXTRACT)
34308 fputs ("special:extract ", dump_file);
34309 else if (insn_entry[i].special_handling == SH_SPLAT)
34310 fputs ("special:splat ", dump_file);
34312 if (insn_entry[i].web_not_optimizable)
34313 fputs ("unoptimizable ", dump_file);
34314 if (insn_entry[i].will_delete)
34315 fputs ("delete ", dump_file);
34316 fputs ("\n", dump_file);
34318 fputs ("\n", dump_file);
34321 /* Main entry point for this pass. */
34322 unsigned int
34323 rs6000_analyze_swaps (function *fun)
34325 swap_web_entry *insn_entry;
34326 basic_block bb;
34327 rtx_insn *insn;
34329 /* Dataflow analysis for use-def chains. */
34330 df_set_flags (DF_RD_PRUNE_DEAD_DEFS);
34331 df_chain_add_problem (DF_DU_CHAIN | DF_UD_CHAIN);
34332 df_analyze ();
34333 df_set_flags (DF_DEFER_INSN_RESCAN);
34335 /* Allocate structure to represent webs of insns. */
34336 insn_entry = XCNEWVEC (swap_web_entry, get_max_uid ());
34338 /* Walk the insns to gather basic data. */
34339 FOR_ALL_BB_FN (bb, fun)
34340 FOR_BB_INSNS (bb, insn)
34342 unsigned int uid = INSN_UID (insn);
34343 if (NONDEBUG_INSN_P (insn))
34345 insn_entry[uid].insn = insn;
34347 if (GET_CODE (insn) == CALL_INSN)
34348 insn_entry[uid].is_call = 1;
34350 /* Walk the uses and defs to see if we mention vector regs.
34351 Record any constraints on optimization of such mentions. */
34352 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
34353 df_ref mention;
34354 FOR_EACH_INSN_INFO_USE (mention, insn_info)
34356 /* We use DF_REF_REAL_REG here to get inside any subregs. */
34357 enum machine_mode mode = GET_MODE (DF_REF_REAL_REG (mention));
34359 /* If a use gets its value from a call insn, it will be
34360 a hard register and will look like (reg:V4SI 3 3).
34361 The df analysis creates two mentions for GPR3 and GPR4,
34362 both DImode. We must recognize this and treat it as a
34363 vector mention to ensure the call is unioned with this
34364 use. */
34365 if (mode == DImode && DF_REF_INSN_INFO (mention))
34367 rtx feeder = DF_REF_INSN (mention);
34368 /* FIXME: It is pretty hard to get from the df mention
34369 to the mode of the use in the insn. We arbitrarily
34370 pick a vector mode here, even though the use might
34371 be a real DImode. We can be too conservative
34372 (create a web larger than necessary) because of
34373 this, so consider eventually fixing this. */
34374 if (GET_CODE (feeder) == CALL_INSN)
34375 mode = V4SImode;
34378 if (VECTOR_MODE_P (mode))
34380 insn_entry[uid].is_relevant = 1;
34381 if (mode == TImode || mode == V1TImode)
34382 insn_entry[uid].is_128_int = 1;
34383 if (DF_REF_INSN_INFO (mention))
34384 insn_entry[uid].contains_subreg
34385 = !rtx_equal_p (DF_REF_REG (mention),
34386 DF_REF_REAL_REG (mention));
34387 union_defs (insn_entry, insn, mention);
34390 FOR_EACH_INSN_INFO_DEF (mention, insn_info)
34392 /* We use DF_REF_REAL_REG here to get inside any subregs. */
34393 enum machine_mode mode = GET_MODE (DF_REF_REAL_REG (mention));
34395 /* If we're loading up a hard vector register for a call,
34396 it looks like (set (reg:V4SI 9 9) (...)). The df
34397 analysis creates two mentions for GPR9 and GPR10, both
34398 DImode. So relying on the mode from the mentions
34399 isn't sufficient to ensure we union the call into the
34400 web with the parameter setup code. */
34401 if (mode == DImode && GET_CODE (insn) == SET
34402 && VECTOR_MODE_P (GET_MODE (SET_DEST (insn))))
34403 mode = GET_MODE (SET_DEST (insn));
34405 if (VECTOR_MODE_P (mode))
34407 insn_entry[uid].is_relevant = 1;
34408 if (mode == TImode || mode == V1TImode)
34409 insn_entry[uid].is_128_int = 1;
34410 if (DF_REF_INSN_INFO (mention))
34411 insn_entry[uid].contains_subreg
34412 = !rtx_equal_p (DF_REF_REG (mention),
34413 DF_REF_REAL_REG (mention));
34414 /* REG_FUNCTION_VALUE_P is not valid for subregs. */
34415 else if (REG_FUNCTION_VALUE_P (DF_REF_REG (mention)))
34416 insn_entry[uid].is_live_out = 1;
34417 union_uses (insn_entry, insn, mention);
34421 if (insn_entry[uid].is_relevant)
34423 /* Determine if this is a load or store. */
34424 insn_entry[uid].is_load = insn_is_load_p (insn);
34425 insn_entry[uid].is_store = insn_is_store_p (insn);
34427 /* Determine if this is a doubleword swap. If not,
34428 determine whether it can legally be swapped. */
34429 if (insn_is_swap_p (insn))
34430 insn_entry[uid].is_swap = 1;
34431 else
34433 unsigned int special = SH_NONE;
34434 insn_entry[uid].is_swappable
34435 = insn_is_swappable_p (insn_entry, insn, &special);
34436 if (special != SH_NONE && insn_entry[uid].contains_subreg)
34437 insn_entry[uid].is_swappable = 0;
34438 else if (special != SH_NONE)
34439 insn_entry[uid].special_handling = special;
34440 else if (insn_entry[uid].contains_subreg)
34441 insn_entry[uid].special_handling = SH_SUBREG;
34447 if (dump_file)
34449 fprintf (dump_file, "\nSwap insn entry table when first built\n");
34450 dump_swap_insn_table (insn_entry);
34453 /* Record unoptimizable webs. */
34454 unsigned e = get_max_uid (), i;
34455 for (i = 0; i < e; ++i)
34457 if (!insn_entry[i].is_relevant)
34458 continue;
34460 swap_web_entry *root
34461 = (swap_web_entry*)(&insn_entry[i])->unionfind_root ();
34463 if (insn_entry[i].is_live_in || insn_entry[i].is_live_out
34464 || (insn_entry[i].contains_subreg
34465 && insn_entry[i].special_handling != SH_SUBREG)
34466 || insn_entry[i].is_128_int || insn_entry[i].is_call
34467 || !(insn_entry[i].is_swappable || insn_entry[i].is_swap))
34468 root->web_not_optimizable = 1;
34470 /* If we have loads or stores that aren't permuting then the
34471 optimization isn't appropriate. */
34472 else if ((insn_entry[i].is_load || insn_entry[i].is_store)
34473 && !insn_entry[i].is_swap && !insn_entry[i].is_swappable)
34474 root->web_not_optimizable = 1;
34476 /* If we have permuting loads or stores that are not accompanied
34477 by a register swap, the optimization isn't appropriate. */
34478 else if (insn_entry[i].is_load && insn_entry[i].is_swap)
34480 rtx insn = insn_entry[i].insn;
34481 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
34482 df_ref def;
34484 FOR_EACH_INSN_INFO_DEF (def, insn_info)
34486 struct df_link *link = DF_REF_CHAIN (def);
34488 if (!chain_contains_only_swaps (insn_entry, link, FOR_LOADS))
34490 root->web_not_optimizable = 1;
34491 break;
34495 else if (insn_entry[i].is_store && insn_entry[i].is_swap)
34497 rtx insn = insn_entry[i].insn;
34498 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
34499 df_ref use;
34501 FOR_EACH_INSN_INFO_USE (use, insn_info)
34503 struct df_link *link = DF_REF_CHAIN (use);
34505 if (!chain_contains_only_swaps (insn_entry, link, FOR_STORES))
34507 root->web_not_optimizable = 1;
34508 break;
34514 if (dump_file)
34516 fprintf (dump_file, "\nSwap insn entry table after web analysis\n");
34517 dump_swap_insn_table (insn_entry);
34520 /* For each load and store in an optimizable web (which implies
34521 the loads and stores are permuting), find the associated
34522 register swaps and mark them for removal. Due to various
34523 optimizations we may mark the same swap more than once. Also
34524 perform special handling for swappable insns that require it. */
34525 for (i = 0; i < e; ++i)
34526 if ((insn_entry[i].is_load || insn_entry[i].is_store)
34527 && insn_entry[i].is_swap)
34529 swap_web_entry* root_entry
34530 = (swap_web_entry*)((&insn_entry[i])->unionfind_root ());
34531 if (!root_entry->web_not_optimizable)
34532 mark_swaps_for_removal (insn_entry, i);
34534 else if (insn_entry[i].is_swappable && insn_entry[i].special_handling)
34536 swap_web_entry* root_entry
34537 = (swap_web_entry*)((&insn_entry[i])->unionfind_root ());
34538 if (!root_entry->web_not_optimizable)
34539 handle_special_swappables (insn_entry, i);
34542 /* Now delete the swaps marked for removal. */
34543 for (i = 0; i < e; ++i)
34544 if (insn_entry[i].will_delete)
34545 replace_swap_with_copy (insn_entry, i);
34547 /* Clean up. */
34548 free (insn_entry);
34549 return 0;
34552 const pass_data pass_data_analyze_swaps =
34554 RTL_PASS, /* type */
34555 "swaps", /* name */
34556 OPTGROUP_NONE, /* optinfo_flags */
34557 TV_NONE, /* tv_id */
34558 0, /* properties_required */
34559 0, /* properties_provided */
34560 0, /* properties_destroyed */
34561 0, /* todo_flags_start */
34562 TODO_df_finish, /* todo_flags_finish */
34565 class pass_analyze_swaps : public rtl_opt_pass
34567 public:
34568 pass_analyze_swaps(gcc::context *ctxt)
34569 : rtl_opt_pass(pass_data_analyze_swaps, ctxt)
34572 /* opt_pass methods: */
34573 virtual bool gate (function *)
34575 return (optimize > 0 && !BYTES_BIG_ENDIAN && TARGET_VSX
34576 && rs6000_optimize_swaps);
34579 virtual unsigned int execute (function *fun)
34581 return rs6000_analyze_swaps (fun);
34584 }; // class pass_analyze_swaps
34586 rtl_opt_pass *
34587 make_pass_analyze_swaps (gcc::context *ctxt)
34589 return new pass_analyze_swaps (ctxt);
34592 /* Implement TARGET_ATOMIC_ASSIGN_EXPAND_FENV hook. */
34594 static void
34595 rs6000_atomic_assign_expand_fenv (tree *hold, tree *clear, tree *update)
34597 if (!TARGET_HARD_FLOAT || !TARGET_FPRS)
34598 return;
34600 tree mffs = rs6000_builtin_decls[RS6000_BUILTIN_MFFS];
34601 tree mtfsf = rs6000_builtin_decls[RS6000_BUILTIN_MTFSF];
34602 tree call_mffs = build_call_expr (mffs, 0);
34604 /* Generates the equivalent of feholdexcept (&fenv_var)
34606 *fenv_var = __builtin_mffs ();
34607 double fenv_hold;
34608 *(uint64_t*)&fenv_hold = *(uint64_t*)fenv_var & 0xffffffff00000007LL;
34609 __builtin_mtfsf (0xff, fenv_hold); */
34611 /* Mask to clear everything except for the rounding modes and non-IEEE
34612 arithmetic flag. */
34613 const unsigned HOST_WIDE_INT hold_exception_mask =
34614 HOST_WIDE_INT_C (0xffffffff00000007);
34616 tree fenv_var = create_tmp_var (double_type_node, NULL);
34618 tree hold_mffs = build2 (MODIFY_EXPR, void_type_node, fenv_var, call_mffs);
34620 tree fenv_llu = build1 (VIEW_CONVERT_EXPR, uint64_type_node, fenv_var);
34621 tree fenv_llu_and = build2 (BIT_AND_EXPR, uint64_type_node, fenv_llu,
34622 build_int_cst (uint64_type_node,
34623 hold_exception_mask));
34625 tree fenv_hold_mtfsf = build1 (VIEW_CONVERT_EXPR, double_type_node,
34626 fenv_llu_and);
34628 tree hold_mtfsf = build_call_expr (mtfsf, 2,
34629 build_int_cst (unsigned_type_node, 0xff),
34630 fenv_hold_mtfsf);
34632 *hold = build2 (COMPOUND_EXPR, void_type_node, hold_mffs, hold_mtfsf);
34634 /* Generates the equivalent of feclearexcept (FE_ALL_EXCEPT):
34636 double fenv_clear = __builtin_mffs ();
34637 *(uint64_t)&fenv_clear &= 0xffffffff00000000LL;
34638 __builtin_mtfsf (0xff, fenv_clear); */
34640 /* Mask to clear everything except for the rounding modes and non-IEEE
34641 arithmetic flag. */
34642 const unsigned HOST_WIDE_INT clear_exception_mask =
34643 HOST_WIDE_INT_C (0xffffffff00000000);
34645 tree fenv_clear = create_tmp_var (double_type_node, NULL);
34647 tree clear_mffs = build2 (MODIFY_EXPR, void_type_node, fenv_clear, call_mffs);
34649 tree fenv_clean_llu = build1 (VIEW_CONVERT_EXPR, uint64_type_node, fenv_clear);
34650 tree fenv_clear_llu_and = build2 (BIT_AND_EXPR, uint64_type_node,
34651 fenv_clean_llu,
34652 build_int_cst (uint64_type_node,
34653 clear_exception_mask));
34655 tree fenv_clear_mtfsf = build1 (VIEW_CONVERT_EXPR, double_type_node,
34656 fenv_clear_llu_and);
34658 tree clear_mtfsf = build_call_expr (mtfsf, 2,
34659 build_int_cst (unsigned_type_node, 0xff),
34660 fenv_clear_mtfsf);
34662 *clear = build2 (COMPOUND_EXPR, void_type_node, clear_mffs, clear_mtfsf);
34664 /* Generates the equivalent of feupdateenv (&fenv_var)
34666 double old_fenv = __builtin_mffs ();
34667 double fenv_update;
34668 *(uint64_t*)&fenv_update = (*(uint64_t*)&old & 0xffffffff1fffff00LL) |
34669 (*(uint64_t*)fenv_var 0x1ff80fff);
34670 __builtin_mtfsf (0xff, fenv_update); */
34672 const unsigned HOST_WIDE_INT update_exception_mask =
34673 HOST_WIDE_INT_C (0xffffffff1fffff00);
34674 const unsigned HOST_WIDE_INT new_exception_mask =
34675 HOST_WIDE_INT_C (0x1ff80fff);
34677 tree old_fenv = create_tmp_var (double_type_node, NULL);
34678 tree update_mffs = build2 (MODIFY_EXPR, void_type_node, old_fenv, call_mffs);
34680 tree old_llu = build1 (VIEW_CONVERT_EXPR, uint64_type_node, old_fenv);
34681 tree old_llu_and = build2 (BIT_AND_EXPR, uint64_type_node, old_llu,
34682 build_int_cst (uint64_type_node,
34683 update_exception_mask));
34685 tree new_llu_and = build2 (BIT_AND_EXPR, uint64_type_node, fenv_llu,
34686 build_int_cst (uint64_type_node,
34687 new_exception_mask));
34689 tree new_llu_mask = build2 (BIT_IOR_EXPR, uint64_type_node,
34690 old_llu_and, new_llu_and);
34692 tree fenv_update_mtfsf = build1 (VIEW_CONVERT_EXPR, double_type_node,
34693 new_llu_mask);
34695 tree update_mtfsf = build_call_expr (mtfsf, 2,
34696 build_int_cst (unsigned_type_node, 0xff),
34697 fenv_update_mtfsf);
34699 *update = build2 (COMPOUND_EXPR, void_type_node, update_mffs, update_mtfsf);
34703 struct gcc_target targetm = TARGET_INITIALIZER;
34705 #include "gt-rs6000.h"