Merge trunk version 204659 into gupc branch.
[official-gcc.git] / gcc / config / rs6000 / rs6000.c
bloba16e5d11f17574728533fa92797cc542e758c815
1 /* Subroutines used for code generation on IBM RS/6000.
2 Copyright (C) 1991-2013 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 "expr.h"
36 #include "optabs.h"
37 #include "except.h"
38 #include "function.h"
39 #include "output.h"
40 #include "dbxout.h"
41 #include "basic-block.h"
42 #include "diagnostic-core.h"
43 #include "toplev.h"
44 #include "ggc.h"
45 #include "hashtab.h"
46 #include "tm_p.h"
47 #include "target.h"
48 #include "target-def.h"
49 #include "common/common-target.h"
50 #include "langhooks.h"
51 #include "reload.h"
52 #include "cfgloop.h"
53 #include "sched-int.h"
54 #include "gimple.h"
55 #include "intl.h"
56 #include "params.h"
57 #include "tm-constrs.h"
58 #include "ira.h"
59 #include "opts.h"
60 #include "tree-vectorizer.h"
61 #include "dumpfile.h"
62 #include "cgraph.h"
63 #if TARGET_XCOFF
64 #include "xcoffout.h" /* get declarations of xcoff_*_section_name */
65 #endif
66 #if TARGET_MACHO
67 #include "gstab.h" /* for N_SLINE */
68 #endif
70 #ifndef TARGET_NO_PROTOTYPE
71 #define TARGET_NO_PROTOTYPE 0
72 #endif
74 #define min(A,B) ((A) < (B) ? (A) : (B))
75 #define max(A,B) ((A) > (B) ? (A) : (B))
77 /* Structure used to define the rs6000 stack */
78 typedef struct rs6000_stack {
79 int reload_completed; /* stack info won't change from here on */
80 int first_gp_reg_save; /* first callee saved GP register used */
81 int first_fp_reg_save; /* first callee saved FP register used */
82 int first_altivec_reg_save; /* first callee saved AltiVec register used */
83 int lr_save_p; /* true if the link reg needs to be saved */
84 int cr_save_p; /* true if the CR reg needs to be saved */
85 unsigned int vrsave_mask; /* mask of vec registers to save */
86 int push_p; /* true if we need to allocate stack space */
87 int calls_p; /* true if the function makes any calls */
88 int world_save_p; /* true if we're saving *everything*:
89 r13-r31, cr, f14-f31, vrsave, v20-v31 */
90 enum rs6000_abi abi; /* which ABI to use */
91 int gp_save_offset; /* offset to save GP regs from initial SP */
92 int fp_save_offset; /* offset to save FP regs from initial SP */
93 int altivec_save_offset; /* offset to save AltiVec regs from initial SP */
94 int lr_save_offset; /* offset to save LR from initial SP */
95 int cr_save_offset; /* offset to save CR from initial SP */
96 int vrsave_save_offset; /* offset to save VRSAVE from initial SP */
97 int spe_gp_save_offset; /* offset to save spe 64-bit gprs */
98 int varargs_save_offset; /* offset to save the varargs registers */
99 int ehrd_offset; /* offset to EH return data */
100 int reg_size; /* register size (4 or 8) */
101 HOST_WIDE_INT vars_size; /* variable save area size */
102 int parm_size; /* outgoing parameter size */
103 int save_size; /* save area size */
104 int fixed_size; /* fixed size of stack frame */
105 int gp_size; /* size of saved GP registers */
106 int fp_size; /* size of saved FP registers */
107 int altivec_size; /* size of saved AltiVec registers */
108 int cr_size; /* size to hold CR if not in save_size */
109 int vrsave_size; /* size to hold VRSAVE if not in save_size */
110 int altivec_padding_size; /* size of altivec alignment padding if
111 not in save_size */
112 int spe_gp_size; /* size of 64-bit GPR save size for SPE */
113 int spe_padding_size;
114 HOST_WIDE_INT total_size; /* total bytes allocated for stack */
115 int spe_64bit_regs_used;
116 int savres_strategy;
117 } rs6000_stack_t;
119 /* A C structure for machine-specific, per-function data.
120 This is added to the cfun structure. */
121 typedef struct GTY(()) machine_function
123 /* Some local-dynamic symbol. */
124 const char *some_ld_name;
125 /* Whether the instruction chain has been scanned already. */
126 int insn_chain_scanned_p;
127 /* Flags if __builtin_return_address (n) with n >= 1 was used. */
128 int ra_needs_full_frame;
129 /* Flags if __builtin_return_address (0) was used. */
130 int ra_need_lr;
131 /* Cache lr_save_p after expansion of builtin_eh_return. */
132 int lr_save_state;
133 /* Whether we need to save the TOC to the reserved stack location in the
134 function prologue. */
135 bool save_toc_in_prologue;
136 /* Offset from virtual_stack_vars_rtx to the start of the ABI_V4
137 varargs save area. */
138 HOST_WIDE_INT varargs_save_offset;
139 /* Temporary stack slot to use for SDmode copies. This slot is
140 64-bits wide and is allocated early enough so that the offset
141 does not overflow the 16-bit load/store offset field. */
142 rtx sdmode_stack_slot;
143 } machine_function;
145 /* Support targetm.vectorize.builtin_mask_for_load. */
146 static GTY(()) tree altivec_builtin_mask_for_load;
148 /* Set to nonzero once AIX common-mode calls have been defined. */
149 static GTY(()) int common_mode_defined;
151 /* Label number of label created for -mrelocatable, to call to so we can
152 get the address of the GOT section */
153 static int rs6000_pic_labelno;
155 #ifdef USING_ELFOS_H
156 /* Counter for labels which are to be placed in .fixup. */
157 int fixuplabelno = 0;
158 #endif
160 /* Whether to use variant of AIX ABI for PowerPC64 Linux. */
161 int dot_symbols;
163 /* Specify the machine mode that pointers have. After generation of rtl, the
164 compiler makes no further distinction between pointers and any other objects
165 of this machine mode. The type is unsigned since not all things that
166 include rs6000.h also include machmode.h. */
167 unsigned rs6000_pmode;
169 /* Width in bits of a pointer. */
170 unsigned rs6000_pointer_size;
172 #ifdef HAVE_AS_GNU_ATTRIBUTE
173 /* Flag whether floating point values have been passed/returned. */
174 static bool rs6000_passes_float;
175 /* Flag whether vector values have been passed/returned. */
176 static bool rs6000_passes_vector;
177 /* Flag whether small (<= 8 byte) structures have been returned. */
178 static bool rs6000_returns_struct;
179 #endif
181 /* Value is TRUE if register/mode pair is acceptable. */
182 bool rs6000_hard_regno_mode_ok_p[NUM_MACHINE_MODES][FIRST_PSEUDO_REGISTER];
184 /* Maximum number of registers needed for a given register class and mode. */
185 unsigned char rs6000_class_max_nregs[NUM_MACHINE_MODES][LIM_REG_CLASSES];
187 /* How many registers are needed for a given register and mode. */
188 unsigned char rs6000_hard_regno_nregs[NUM_MACHINE_MODES][FIRST_PSEUDO_REGISTER];
190 /* Map register number to register class. */
191 enum reg_class rs6000_regno_regclass[FIRST_PSEUDO_REGISTER];
193 static int dbg_cost_ctrl;
195 /* Built in types. */
196 tree rs6000_builtin_types[RS6000_BTI_MAX];
197 tree rs6000_builtin_decls[RS6000_BUILTIN_COUNT];
199 /* Flag to say the TOC is initialized */
200 int toc_initialized;
201 char toc_label_name[10];
203 /* Cached value of rs6000_variable_issue. This is cached in
204 rs6000_variable_issue hook and returned from rs6000_sched_reorder2. */
205 static short cached_can_issue_more;
207 static GTY(()) section *read_only_data_section;
208 static GTY(()) section *private_data_section;
209 static GTY(()) section *tls_data_section;
210 static GTY(()) section *tls_private_data_section;
211 static GTY(()) section *read_only_private_data_section;
212 static GTY(()) section *sdata2_section;
213 static GTY(()) section *toc_section;
215 struct builtin_description
217 const HOST_WIDE_INT mask;
218 const enum insn_code icode;
219 const char *const name;
220 const enum rs6000_builtins code;
223 /* Describe the vector unit used for modes. */
224 enum rs6000_vector rs6000_vector_unit[NUM_MACHINE_MODES];
225 enum rs6000_vector rs6000_vector_mem[NUM_MACHINE_MODES];
227 /* Register classes for various constraints that are based on the target
228 switches. */
229 enum reg_class rs6000_constraints[RS6000_CONSTRAINT_MAX];
231 /* Describe the alignment of a vector. */
232 int rs6000_vector_align[NUM_MACHINE_MODES];
234 /* Map selected modes to types for builtins. */
235 static GTY(()) tree builtin_mode_to_type[MAX_MACHINE_MODE][2];
237 /* What modes to automatically generate reciprocal divide estimate (fre) and
238 reciprocal sqrt (frsqrte) for. */
239 unsigned char rs6000_recip_bits[MAX_MACHINE_MODE];
241 /* Masks to determine which reciprocal esitmate instructions to generate
242 automatically. */
243 enum rs6000_recip_mask {
244 RECIP_SF_DIV = 0x001, /* Use divide estimate */
245 RECIP_DF_DIV = 0x002,
246 RECIP_V4SF_DIV = 0x004,
247 RECIP_V2DF_DIV = 0x008,
249 RECIP_SF_RSQRT = 0x010, /* Use reciprocal sqrt estimate. */
250 RECIP_DF_RSQRT = 0x020,
251 RECIP_V4SF_RSQRT = 0x040,
252 RECIP_V2DF_RSQRT = 0x080,
254 /* Various combination of flags for -mrecip=xxx. */
255 RECIP_NONE = 0,
256 RECIP_ALL = (RECIP_SF_DIV | RECIP_DF_DIV | RECIP_V4SF_DIV
257 | RECIP_V2DF_DIV | RECIP_SF_RSQRT | RECIP_DF_RSQRT
258 | RECIP_V4SF_RSQRT | RECIP_V2DF_RSQRT),
260 RECIP_HIGH_PRECISION = RECIP_ALL,
262 /* On low precision machines like the power5, don't enable double precision
263 reciprocal square root estimate, since it isn't accurate enough. */
264 RECIP_LOW_PRECISION = (RECIP_ALL & ~(RECIP_DF_RSQRT | RECIP_V2DF_RSQRT))
267 /* -mrecip options. */
268 static struct
270 const char *string; /* option name */
271 unsigned int mask; /* mask bits to set */
272 } recip_options[] = {
273 { "all", RECIP_ALL },
274 { "none", RECIP_NONE },
275 { "div", (RECIP_SF_DIV | RECIP_DF_DIV | RECIP_V4SF_DIV
276 | RECIP_V2DF_DIV) },
277 { "divf", (RECIP_SF_DIV | RECIP_V4SF_DIV) },
278 { "divd", (RECIP_DF_DIV | RECIP_V2DF_DIV) },
279 { "rsqrt", (RECIP_SF_RSQRT | RECIP_DF_RSQRT | RECIP_V4SF_RSQRT
280 | RECIP_V2DF_RSQRT) },
281 { "rsqrtf", (RECIP_SF_RSQRT | RECIP_V4SF_RSQRT) },
282 { "rsqrtd", (RECIP_DF_RSQRT | RECIP_V2DF_RSQRT) },
285 /* Pointer to function (in rs6000-c.c) that can define or undefine target
286 macros that have changed. Languages that don't support the preprocessor
287 don't link in rs6000-c.c, so we can't call it directly. */
288 void (*rs6000_target_modify_macros_ptr) (bool, HOST_WIDE_INT, HOST_WIDE_INT);
290 /* Simplfy register classes into simpler classifications. We assume
291 GPR_REG_TYPE - FPR_REG_TYPE are ordered so that we can use a simple range
292 check for standard register classes (gpr/floating/altivec/vsx) and
293 floating/vector classes (float/altivec/vsx). */
295 enum rs6000_reg_type {
296 NO_REG_TYPE,
297 PSEUDO_REG_TYPE,
298 GPR_REG_TYPE,
299 VSX_REG_TYPE,
300 ALTIVEC_REG_TYPE,
301 FPR_REG_TYPE,
302 SPR_REG_TYPE,
303 CR_REG_TYPE,
304 SPE_ACC_TYPE,
305 SPEFSCR_REG_TYPE
308 /* Map register class to register type. */
309 static enum rs6000_reg_type reg_class_to_reg_type[N_REG_CLASSES];
311 /* First/last register type for the 'normal' register types (i.e. general
312 purpose, floating point, altivec, and VSX registers). */
313 #define IS_STD_REG_TYPE(RTYPE) IN_RANGE(RTYPE, GPR_REG_TYPE, FPR_REG_TYPE)
315 #define IS_FP_VECT_REG_TYPE(RTYPE) IN_RANGE(RTYPE, VSX_REG_TYPE, FPR_REG_TYPE)
318 /* Register classes we care about in secondary reload or go if legitimate
319 address. We only need to worry about GPR, FPR, and Altivec registers here,
320 along an ANY field that is the OR of the 3 register classes. */
322 enum rs6000_reload_reg_type {
323 RELOAD_REG_GPR, /* General purpose registers. */
324 RELOAD_REG_FPR, /* Traditional floating point regs. */
325 RELOAD_REG_VMX, /* Altivec (VMX) registers. */
326 RELOAD_REG_ANY, /* OR of GPR, FPR, Altivec masks. */
327 N_RELOAD_REG
330 /* For setting up register classes, loop through the 3 register classes mapping
331 into real registers, and skip the ANY class, which is just an OR of the
332 bits. */
333 #define FIRST_RELOAD_REG_CLASS RELOAD_REG_GPR
334 #define LAST_RELOAD_REG_CLASS RELOAD_REG_VMX
336 /* Map reload register type to a register in the register class. */
337 struct reload_reg_map_type {
338 const char *name; /* Register class name. */
339 int reg; /* Register in the register class. */
342 static const struct reload_reg_map_type reload_reg_map[N_RELOAD_REG] = {
343 { "Gpr", FIRST_GPR_REGNO }, /* RELOAD_REG_GPR. */
344 { "Fpr", FIRST_FPR_REGNO }, /* RELOAD_REG_FPR. */
345 { "VMX", FIRST_ALTIVEC_REGNO }, /* RELOAD_REG_VMX. */
346 { "Any", -1 }, /* RELOAD_REG_ANY. */
349 /* Mask bits for each register class, indexed per mode. Historically the
350 compiler has been more restrictive which types can do PRE_MODIFY instead of
351 PRE_INC and PRE_DEC, so keep track of sepaate bits for these two. */
352 typedef unsigned char addr_mask_type;
354 #define RELOAD_REG_VALID 0x01 /* Mode valid in register.. */
355 #define RELOAD_REG_MULTIPLE 0x02 /* Mode takes multiple registers. */
356 #define RELOAD_REG_INDEXED 0x04 /* Reg+reg addressing. */
357 #define RELOAD_REG_OFFSET 0x08 /* Reg+offset addressing. */
358 #define RELOAD_REG_PRE_INCDEC 0x10 /* PRE_INC/PRE_DEC valid. */
359 #define RELOAD_REG_PRE_MODIFY 0x20 /* PRE_MODIFY valid. */
361 /* Register type masks based on the type, of valid addressing modes. */
362 struct rs6000_reg_addr {
363 enum insn_code reload_load; /* INSN to reload for loading. */
364 enum insn_code reload_store; /* INSN to reload for storing. */
365 enum insn_code reload_fpr_gpr; /* INSN to move from FPR to GPR. */
366 enum insn_code reload_gpr_vsx; /* INSN to move from GPR to VSX. */
367 enum insn_code reload_vsx_gpr; /* INSN to move from VSX to GPR. */
368 addr_mask_type addr_mask[(int)N_RELOAD_REG]; /* Valid address masks. */
371 static struct rs6000_reg_addr reg_addr[NUM_MACHINE_MODES];
373 /* Helper function to say whether a mode supports PRE_INC or PRE_DEC. */
374 static inline bool
375 mode_supports_pre_incdec_p (enum machine_mode mode)
377 return ((reg_addr[mode].addr_mask[RELOAD_REG_ANY] & RELOAD_REG_PRE_INCDEC)
378 != 0);
381 /* Helper function to say whether a mode supports PRE_MODIFY. */
382 static inline bool
383 mode_supports_pre_modify_p (enum machine_mode mode)
385 return ((reg_addr[mode].addr_mask[RELOAD_REG_ANY] & RELOAD_REG_PRE_MODIFY)
386 != 0);
390 /* Target cpu costs. */
392 struct processor_costs {
393 const int mulsi; /* cost of SImode multiplication. */
394 const int mulsi_const; /* cost of SImode multiplication by constant. */
395 const int mulsi_const9; /* cost of SImode mult by short constant. */
396 const int muldi; /* cost of DImode multiplication. */
397 const int divsi; /* cost of SImode division. */
398 const int divdi; /* cost of DImode division. */
399 const int fp; /* cost of simple SFmode and DFmode insns. */
400 const int dmul; /* cost of DFmode multiplication (and fmadd). */
401 const int sdiv; /* cost of SFmode division (fdivs). */
402 const int ddiv; /* cost of DFmode division (fdiv). */
403 const int cache_line_size; /* cache line size in bytes. */
404 const int l1_cache_size; /* size of l1 cache, in kilobytes. */
405 const int l2_cache_size; /* size of l2 cache, in kilobytes. */
406 const int simultaneous_prefetches; /* number of parallel prefetch
407 operations. */
410 const struct processor_costs *rs6000_cost;
412 /* Processor costs (relative to an add) */
414 /* Instruction size costs on 32bit processors. */
415 static const
416 struct processor_costs size32_cost = {
417 COSTS_N_INSNS (1), /* mulsi */
418 COSTS_N_INSNS (1), /* mulsi_const */
419 COSTS_N_INSNS (1), /* mulsi_const9 */
420 COSTS_N_INSNS (1), /* muldi */
421 COSTS_N_INSNS (1), /* divsi */
422 COSTS_N_INSNS (1), /* divdi */
423 COSTS_N_INSNS (1), /* fp */
424 COSTS_N_INSNS (1), /* dmul */
425 COSTS_N_INSNS (1), /* sdiv */
426 COSTS_N_INSNS (1), /* ddiv */
433 /* Instruction size costs on 64bit processors. */
434 static const
435 struct processor_costs size64_cost = {
436 COSTS_N_INSNS (1), /* mulsi */
437 COSTS_N_INSNS (1), /* mulsi_const */
438 COSTS_N_INSNS (1), /* mulsi_const9 */
439 COSTS_N_INSNS (1), /* muldi */
440 COSTS_N_INSNS (1), /* divsi */
441 COSTS_N_INSNS (1), /* divdi */
442 COSTS_N_INSNS (1), /* fp */
443 COSTS_N_INSNS (1), /* dmul */
444 COSTS_N_INSNS (1), /* sdiv */
445 COSTS_N_INSNS (1), /* ddiv */
446 128,
452 /* Instruction costs on RS64A processors. */
453 static const
454 struct processor_costs rs64a_cost = {
455 COSTS_N_INSNS (20), /* mulsi */
456 COSTS_N_INSNS (12), /* mulsi_const */
457 COSTS_N_INSNS (8), /* mulsi_const9 */
458 COSTS_N_INSNS (34), /* muldi */
459 COSTS_N_INSNS (65), /* divsi */
460 COSTS_N_INSNS (67), /* divdi */
461 COSTS_N_INSNS (4), /* fp */
462 COSTS_N_INSNS (4), /* dmul */
463 COSTS_N_INSNS (31), /* sdiv */
464 COSTS_N_INSNS (31), /* ddiv */
465 128, /* cache line size */
466 128, /* l1 cache */
467 2048, /* l2 cache */
468 1, /* streams */
471 /* Instruction costs on MPCCORE processors. */
472 static const
473 struct processor_costs mpccore_cost = {
474 COSTS_N_INSNS (2), /* mulsi */
475 COSTS_N_INSNS (2), /* mulsi_const */
476 COSTS_N_INSNS (2), /* mulsi_const9 */
477 COSTS_N_INSNS (2), /* muldi */
478 COSTS_N_INSNS (6), /* divsi */
479 COSTS_N_INSNS (6), /* divdi */
480 COSTS_N_INSNS (4), /* fp */
481 COSTS_N_INSNS (5), /* dmul */
482 COSTS_N_INSNS (10), /* sdiv */
483 COSTS_N_INSNS (17), /* ddiv */
484 32, /* cache line size */
485 4, /* l1 cache */
486 16, /* l2 cache */
487 1, /* streams */
490 /* Instruction costs on PPC403 processors. */
491 static const
492 struct processor_costs ppc403_cost = {
493 COSTS_N_INSNS (4), /* mulsi */
494 COSTS_N_INSNS (4), /* mulsi_const */
495 COSTS_N_INSNS (4), /* mulsi_const9 */
496 COSTS_N_INSNS (4), /* muldi */
497 COSTS_N_INSNS (33), /* divsi */
498 COSTS_N_INSNS (33), /* divdi */
499 COSTS_N_INSNS (11), /* fp */
500 COSTS_N_INSNS (11), /* dmul */
501 COSTS_N_INSNS (11), /* sdiv */
502 COSTS_N_INSNS (11), /* ddiv */
503 32, /* cache line size */
504 4, /* l1 cache */
505 16, /* l2 cache */
506 1, /* streams */
509 /* Instruction costs on PPC405 processors. */
510 static const
511 struct processor_costs ppc405_cost = {
512 COSTS_N_INSNS (5), /* mulsi */
513 COSTS_N_INSNS (4), /* mulsi_const */
514 COSTS_N_INSNS (3), /* mulsi_const9 */
515 COSTS_N_INSNS (5), /* muldi */
516 COSTS_N_INSNS (35), /* divsi */
517 COSTS_N_INSNS (35), /* divdi */
518 COSTS_N_INSNS (11), /* fp */
519 COSTS_N_INSNS (11), /* dmul */
520 COSTS_N_INSNS (11), /* sdiv */
521 COSTS_N_INSNS (11), /* ddiv */
522 32, /* cache line size */
523 16, /* l1 cache */
524 128, /* l2 cache */
525 1, /* streams */
528 /* Instruction costs on PPC440 processors. */
529 static const
530 struct processor_costs ppc440_cost = {
531 COSTS_N_INSNS (3), /* mulsi */
532 COSTS_N_INSNS (2), /* mulsi_const */
533 COSTS_N_INSNS (2), /* mulsi_const9 */
534 COSTS_N_INSNS (3), /* muldi */
535 COSTS_N_INSNS (34), /* divsi */
536 COSTS_N_INSNS (34), /* divdi */
537 COSTS_N_INSNS (5), /* fp */
538 COSTS_N_INSNS (5), /* dmul */
539 COSTS_N_INSNS (19), /* sdiv */
540 COSTS_N_INSNS (33), /* ddiv */
541 32, /* cache line size */
542 32, /* l1 cache */
543 256, /* l2 cache */
544 1, /* streams */
547 /* Instruction costs on PPC476 processors. */
548 static const
549 struct processor_costs ppc476_cost = {
550 COSTS_N_INSNS (4), /* mulsi */
551 COSTS_N_INSNS (4), /* mulsi_const */
552 COSTS_N_INSNS (4), /* mulsi_const9 */
553 COSTS_N_INSNS (4), /* muldi */
554 COSTS_N_INSNS (11), /* divsi */
555 COSTS_N_INSNS (11), /* divdi */
556 COSTS_N_INSNS (6), /* fp */
557 COSTS_N_INSNS (6), /* dmul */
558 COSTS_N_INSNS (19), /* sdiv */
559 COSTS_N_INSNS (33), /* ddiv */
560 32, /* l1 cache line size */
561 32, /* l1 cache */
562 512, /* l2 cache */
563 1, /* streams */
566 /* Instruction costs on PPC601 processors. */
567 static const
568 struct processor_costs ppc601_cost = {
569 COSTS_N_INSNS (5), /* mulsi */
570 COSTS_N_INSNS (5), /* mulsi_const */
571 COSTS_N_INSNS (5), /* mulsi_const9 */
572 COSTS_N_INSNS (5), /* muldi */
573 COSTS_N_INSNS (36), /* divsi */
574 COSTS_N_INSNS (36), /* divdi */
575 COSTS_N_INSNS (4), /* fp */
576 COSTS_N_INSNS (5), /* dmul */
577 COSTS_N_INSNS (17), /* sdiv */
578 COSTS_N_INSNS (31), /* ddiv */
579 32, /* cache line size */
580 32, /* l1 cache */
581 256, /* l2 cache */
582 1, /* streams */
585 /* Instruction costs on PPC603 processors. */
586 static const
587 struct processor_costs ppc603_cost = {
588 COSTS_N_INSNS (5), /* mulsi */
589 COSTS_N_INSNS (3), /* mulsi_const */
590 COSTS_N_INSNS (2), /* mulsi_const9 */
591 COSTS_N_INSNS (5), /* muldi */
592 COSTS_N_INSNS (37), /* divsi */
593 COSTS_N_INSNS (37), /* divdi */
594 COSTS_N_INSNS (3), /* fp */
595 COSTS_N_INSNS (4), /* dmul */
596 COSTS_N_INSNS (18), /* sdiv */
597 COSTS_N_INSNS (33), /* ddiv */
598 32, /* cache line size */
599 8, /* l1 cache */
600 64, /* l2 cache */
601 1, /* streams */
604 /* Instruction costs on PPC604 processors. */
605 static const
606 struct processor_costs ppc604_cost = {
607 COSTS_N_INSNS (4), /* mulsi */
608 COSTS_N_INSNS (4), /* mulsi_const */
609 COSTS_N_INSNS (4), /* mulsi_const9 */
610 COSTS_N_INSNS (4), /* muldi */
611 COSTS_N_INSNS (20), /* divsi */
612 COSTS_N_INSNS (20), /* divdi */
613 COSTS_N_INSNS (3), /* fp */
614 COSTS_N_INSNS (3), /* dmul */
615 COSTS_N_INSNS (18), /* sdiv */
616 COSTS_N_INSNS (32), /* ddiv */
617 32, /* cache line size */
618 16, /* l1 cache */
619 512, /* l2 cache */
620 1, /* streams */
623 /* Instruction costs on PPC604e processors. */
624 static const
625 struct processor_costs ppc604e_cost = {
626 COSTS_N_INSNS (2), /* mulsi */
627 COSTS_N_INSNS (2), /* mulsi_const */
628 COSTS_N_INSNS (2), /* mulsi_const9 */
629 COSTS_N_INSNS (2), /* muldi */
630 COSTS_N_INSNS (20), /* divsi */
631 COSTS_N_INSNS (20), /* divdi */
632 COSTS_N_INSNS (3), /* fp */
633 COSTS_N_INSNS (3), /* dmul */
634 COSTS_N_INSNS (18), /* sdiv */
635 COSTS_N_INSNS (32), /* ddiv */
636 32, /* cache line size */
637 32, /* l1 cache */
638 1024, /* l2 cache */
639 1, /* streams */
642 /* Instruction costs on PPC620 processors. */
643 static const
644 struct processor_costs ppc620_cost = {
645 COSTS_N_INSNS (5), /* mulsi */
646 COSTS_N_INSNS (4), /* mulsi_const */
647 COSTS_N_INSNS (3), /* mulsi_const9 */
648 COSTS_N_INSNS (7), /* muldi */
649 COSTS_N_INSNS (21), /* divsi */
650 COSTS_N_INSNS (37), /* divdi */
651 COSTS_N_INSNS (3), /* fp */
652 COSTS_N_INSNS (3), /* dmul */
653 COSTS_N_INSNS (18), /* sdiv */
654 COSTS_N_INSNS (32), /* ddiv */
655 128, /* cache line size */
656 32, /* l1 cache */
657 1024, /* l2 cache */
658 1, /* streams */
661 /* Instruction costs on PPC630 processors. */
662 static const
663 struct processor_costs ppc630_cost = {
664 COSTS_N_INSNS (5), /* mulsi */
665 COSTS_N_INSNS (4), /* mulsi_const */
666 COSTS_N_INSNS (3), /* mulsi_const9 */
667 COSTS_N_INSNS (7), /* muldi */
668 COSTS_N_INSNS (21), /* divsi */
669 COSTS_N_INSNS (37), /* divdi */
670 COSTS_N_INSNS (3), /* fp */
671 COSTS_N_INSNS (3), /* dmul */
672 COSTS_N_INSNS (17), /* sdiv */
673 COSTS_N_INSNS (21), /* ddiv */
674 128, /* cache line size */
675 64, /* l1 cache */
676 1024, /* l2 cache */
677 1, /* streams */
680 /* Instruction costs on Cell processor. */
681 /* COSTS_N_INSNS (1) ~ one add. */
682 static const
683 struct processor_costs ppccell_cost = {
684 COSTS_N_INSNS (9/2)+2, /* mulsi */
685 COSTS_N_INSNS (6/2), /* mulsi_const */
686 COSTS_N_INSNS (6/2), /* mulsi_const9 */
687 COSTS_N_INSNS (15/2)+2, /* muldi */
688 COSTS_N_INSNS (38/2), /* divsi */
689 COSTS_N_INSNS (70/2), /* divdi */
690 COSTS_N_INSNS (10/2), /* fp */
691 COSTS_N_INSNS (10/2), /* dmul */
692 COSTS_N_INSNS (74/2), /* sdiv */
693 COSTS_N_INSNS (74/2), /* ddiv */
694 128, /* cache line size */
695 32, /* l1 cache */
696 512, /* l2 cache */
697 6, /* streams */
700 /* Instruction costs on PPC750 and PPC7400 processors. */
701 static const
702 struct processor_costs ppc750_cost = {
703 COSTS_N_INSNS (5), /* mulsi */
704 COSTS_N_INSNS (3), /* mulsi_const */
705 COSTS_N_INSNS (2), /* mulsi_const9 */
706 COSTS_N_INSNS (5), /* muldi */
707 COSTS_N_INSNS (17), /* divsi */
708 COSTS_N_INSNS (17), /* divdi */
709 COSTS_N_INSNS (3), /* fp */
710 COSTS_N_INSNS (3), /* dmul */
711 COSTS_N_INSNS (17), /* sdiv */
712 COSTS_N_INSNS (31), /* ddiv */
713 32, /* cache line size */
714 32, /* l1 cache */
715 512, /* l2 cache */
716 1, /* streams */
719 /* Instruction costs on PPC7450 processors. */
720 static const
721 struct processor_costs ppc7450_cost = {
722 COSTS_N_INSNS (4), /* mulsi */
723 COSTS_N_INSNS (3), /* mulsi_const */
724 COSTS_N_INSNS (3), /* mulsi_const9 */
725 COSTS_N_INSNS (4), /* muldi */
726 COSTS_N_INSNS (23), /* divsi */
727 COSTS_N_INSNS (23), /* divdi */
728 COSTS_N_INSNS (5), /* fp */
729 COSTS_N_INSNS (5), /* dmul */
730 COSTS_N_INSNS (21), /* sdiv */
731 COSTS_N_INSNS (35), /* ddiv */
732 32, /* cache line size */
733 32, /* l1 cache */
734 1024, /* l2 cache */
735 1, /* streams */
738 /* Instruction costs on PPC8540 processors. */
739 static const
740 struct processor_costs ppc8540_cost = {
741 COSTS_N_INSNS (4), /* mulsi */
742 COSTS_N_INSNS (4), /* mulsi_const */
743 COSTS_N_INSNS (4), /* mulsi_const9 */
744 COSTS_N_INSNS (4), /* muldi */
745 COSTS_N_INSNS (19), /* divsi */
746 COSTS_N_INSNS (19), /* divdi */
747 COSTS_N_INSNS (4), /* fp */
748 COSTS_N_INSNS (4), /* dmul */
749 COSTS_N_INSNS (29), /* sdiv */
750 COSTS_N_INSNS (29), /* ddiv */
751 32, /* cache line size */
752 32, /* l1 cache */
753 256, /* l2 cache */
754 1, /* prefetch streams /*/
757 /* Instruction costs on E300C2 and E300C3 cores. */
758 static const
759 struct processor_costs ppce300c2c3_cost = {
760 COSTS_N_INSNS (4), /* mulsi */
761 COSTS_N_INSNS (4), /* mulsi_const */
762 COSTS_N_INSNS (4), /* mulsi_const9 */
763 COSTS_N_INSNS (4), /* muldi */
764 COSTS_N_INSNS (19), /* divsi */
765 COSTS_N_INSNS (19), /* divdi */
766 COSTS_N_INSNS (3), /* fp */
767 COSTS_N_INSNS (4), /* dmul */
768 COSTS_N_INSNS (18), /* sdiv */
769 COSTS_N_INSNS (33), /* ddiv */
771 16, /* l1 cache */
772 16, /* l2 cache */
773 1, /* prefetch streams /*/
776 /* Instruction costs on PPCE500MC processors. */
777 static const
778 struct processor_costs ppce500mc_cost = {
779 COSTS_N_INSNS (4), /* mulsi */
780 COSTS_N_INSNS (4), /* mulsi_const */
781 COSTS_N_INSNS (4), /* mulsi_const9 */
782 COSTS_N_INSNS (4), /* muldi */
783 COSTS_N_INSNS (14), /* divsi */
784 COSTS_N_INSNS (14), /* divdi */
785 COSTS_N_INSNS (8), /* fp */
786 COSTS_N_INSNS (10), /* dmul */
787 COSTS_N_INSNS (36), /* sdiv */
788 COSTS_N_INSNS (66), /* ddiv */
789 64, /* cache line size */
790 32, /* l1 cache */
791 128, /* l2 cache */
792 1, /* prefetch streams /*/
795 /* Instruction costs on PPCE500MC64 processors. */
796 static const
797 struct processor_costs ppce500mc64_cost = {
798 COSTS_N_INSNS (4), /* mulsi */
799 COSTS_N_INSNS (4), /* mulsi_const */
800 COSTS_N_INSNS (4), /* mulsi_const9 */
801 COSTS_N_INSNS (4), /* muldi */
802 COSTS_N_INSNS (14), /* divsi */
803 COSTS_N_INSNS (14), /* divdi */
804 COSTS_N_INSNS (4), /* fp */
805 COSTS_N_INSNS (10), /* dmul */
806 COSTS_N_INSNS (36), /* sdiv */
807 COSTS_N_INSNS (66), /* ddiv */
808 64, /* cache line size */
809 32, /* l1 cache */
810 128, /* l2 cache */
811 1, /* prefetch streams /*/
814 /* Instruction costs on PPCE5500 processors. */
815 static const
816 struct processor_costs ppce5500_cost = {
817 COSTS_N_INSNS (5), /* mulsi */
818 COSTS_N_INSNS (5), /* mulsi_const */
819 COSTS_N_INSNS (4), /* mulsi_const9 */
820 COSTS_N_INSNS (5), /* muldi */
821 COSTS_N_INSNS (14), /* divsi */
822 COSTS_N_INSNS (14), /* divdi */
823 COSTS_N_INSNS (7), /* fp */
824 COSTS_N_INSNS (10), /* dmul */
825 COSTS_N_INSNS (36), /* sdiv */
826 COSTS_N_INSNS (66), /* ddiv */
827 64, /* cache line size */
828 32, /* l1 cache */
829 128, /* l2 cache */
830 1, /* prefetch streams /*/
833 /* Instruction costs on PPCE6500 processors. */
834 static const
835 struct processor_costs ppce6500_cost = {
836 COSTS_N_INSNS (5), /* mulsi */
837 COSTS_N_INSNS (5), /* mulsi_const */
838 COSTS_N_INSNS (4), /* mulsi_const9 */
839 COSTS_N_INSNS (5), /* muldi */
840 COSTS_N_INSNS (14), /* divsi */
841 COSTS_N_INSNS (14), /* divdi */
842 COSTS_N_INSNS (7), /* fp */
843 COSTS_N_INSNS (10), /* dmul */
844 COSTS_N_INSNS (36), /* sdiv */
845 COSTS_N_INSNS (66), /* ddiv */
846 64, /* cache line size */
847 32, /* l1 cache */
848 128, /* l2 cache */
849 1, /* prefetch streams /*/
852 /* Instruction costs on AppliedMicro Titan processors. */
853 static const
854 struct processor_costs titan_cost = {
855 COSTS_N_INSNS (5), /* mulsi */
856 COSTS_N_INSNS (5), /* mulsi_const */
857 COSTS_N_INSNS (5), /* mulsi_const9 */
858 COSTS_N_INSNS (5), /* muldi */
859 COSTS_N_INSNS (18), /* divsi */
860 COSTS_N_INSNS (18), /* divdi */
861 COSTS_N_INSNS (10), /* fp */
862 COSTS_N_INSNS (10), /* dmul */
863 COSTS_N_INSNS (46), /* sdiv */
864 COSTS_N_INSNS (72), /* ddiv */
865 32, /* cache line size */
866 32, /* l1 cache */
867 512, /* l2 cache */
868 1, /* prefetch streams /*/
871 /* Instruction costs on POWER4 and POWER5 processors. */
872 static const
873 struct processor_costs power4_cost = {
874 COSTS_N_INSNS (3), /* mulsi */
875 COSTS_N_INSNS (2), /* mulsi_const */
876 COSTS_N_INSNS (2), /* mulsi_const9 */
877 COSTS_N_INSNS (4), /* muldi */
878 COSTS_N_INSNS (18), /* divsi */
879 COSTS_N_INSNS (34), /* divdi */
880 COSTS_N_INSNS (3), /* fp */
881 COSTS_N_INSNS (3), /* dmul */
882 COSTS_N_INSNS (17), /* sdiv */
883 COSTS_N_INSNS (17), /* ddiv */
884 128, /* cache line size */
885 32, /* l1 cache */
886 1024, /* l2 cache */
887 8, /* prefetch streams /*/
890 /* Instruction costs on POWER6 processors. */
891 static const
892 struct processor_costs power6_cost = {
893 COSTS_N_INSNS (8), /* mulsi */
894 COSTS_N_INSNS (8), /* mulsi_const */
895 COSTS_N_INSNS (8), /* mulsi_const9 */
896 COSTS_N_INSNS (8), /* muldi */
897 COSTS_N_INSNS (22), /* divsi */
898 COSTS_N_INSNS (28), /* divdi */
899 COSTS_N_INSNS (3), /* fp */
900 COSTS_N_INSNS (3), /* dmul */
901 COSTS_N_INSNS (13), /* sdiv */
902 COSTS_N_INSNS (16), /* ddiv */
903 128, /* cache line size */
904 64, /* l1 cache */
905 2048, /* l2 cache */
906 16, /* prefetch streams */
909 /* Instruction costs on POWER7 processors. */
910 static const
911 struct processor_costs power7_cost = {
912 COSTS_N_INSNS (2), /* mulsi */
913 COSTS_N_INSNS (2), /* mulsi_const */
914 COSTS_N_INSNS (2), /* mulsi_const9 */
915 COSTS_N_INSNS (2), /* muldi */
916 COSTS_N_INSNS (18), /* divsi */
917 COSTS_N_INSNS (34), /* divdi */
918 COSTS_N_INSNS (3), /* fp */
919 COSTS_N_INSNS (3), /* dmul */
920 COSTS_N_INSNS (13), /* sdiv */
921 COSTS_N_INSNS (16), /* ddiv */
922 128, /* cache line size */
923 32, /* l1 cache */
924 256, /* l2 cache */
925 12, /* prefetch streams */
928 /* Instruction costs on POWER8 processors. */
929 static const
930 struct processor_costs power8_cost = {
931 COSTS_N_INSNS (3), /* mulsi */
932 COSTS_N_INSNS (3), /* mulsi_const */
933 COSTS_N_INSNS (3), /* mulsi_const9 */
934 COSTS_N_INSNS (3), /* muldi */
935 COSTS_N_INSNS (19), /* divsi */
936 COSTS_N_INSNS (35), /* divdi */
937 COSTS_N_INSNS (3), /* fp */
938 COSTS_N_INSNS (3), /* dmul */
939 COSTS_N_INSNS (14), /* sdiv */
940 COSTS_N_INSNS (17), /* ddiv */
941 128, /* cache line size */
942 32, /* l1 cache */
943 256, /* l2 cache */
944 12, /* prefetch streams */
947 /* Instruction costs on POWER A2 processors. */
948 static const
949 struct processor_costs ppca2_cost = {
950 COSTS_N_INSNS (16), /* mulsi */
951 COSTS_N_INSNS (16), /* mulsi_const */
952 COSTS_N_INSNS (16), /* mulsi_const9 */
953 COSTS_N_INSNS (16), /* muldi */
954 COSTS_N_INSNS (22), /* divsi */
955 COSTS_N_INSNS (28), /* divdi */
956 COSTS_N_INSNS (3), /* fp */
957 COSTS_N_INSNS (3), /* dmul */
958 COSTS_N_INSNS (59), /* sdiv */
959 COSTS_N_INSNS (72), /* ddiv */
961 16, /* l1 cache */
962 2048, /* l2 cache */
963 16, /* prefetch streams */
967 /* Table that classifies rs6000 builtin functions (pure, const, etc.). */
968 #undef RS6000_BUILTIN_1
969 #undef RS6000_BUILTIN_2
970 #undef RS6000_BUILTIN_3
971 #undef RS6000_BUILTIN_A
972 #undef RS6000_BUILTIN_D
973 #undef RS6000_BUILTIN_E
974 #undef RS6000_BUILTIN_H
975 #undef RS6000_BUILTIN_P
976 #undef RS6000_BUILTIN_Q
977 #undef RS6000_BUILTIN_S
978 #undef RS6000_BUILTIN_X
980 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE) \
981 { NAME, ICODE, MASK, ATTR },
983 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE) \
984 { NAME, ICODE, MASK, ATTR },
986 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE) \
987 { NAME, ICODE, MASK, ATTR },
989 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE) \
990 { NAME, ICODE, MASK, ATTR },
992 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE) \
993 { NAME, ICODE, MASK, ATTR },
995 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE) \
996 { NAME, ICODE, MASK, ATTR },
998 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE) \
999 { NAME, ICODE, MASK, ATTR },
1001 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE) \
1002 { NAME, ICODE, MASK, ATTR },
1004 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE) \
1005 { NAME, ICODE, MASK, ATTR },
1007 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE) \
1008 { NAME, ICODE, MASK, ATTR },
1010 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE) \
1011 { NAME, ICODE, MASK, ATTR },
1013 struct rs6000_builtin_info_type {
1014 const char *name;
1015 const enum insn_code icode;
1016 const HOST_WIDE_INT mask;
1017 const unsigned attr;
1020 static const struct rs6000_builtin_info_type rs6000_builtin_info[] =
1022 #include "rs6000-builtin.def"
1025 #undef RS6000_BUILTIN_1
1026 #undef RS6000_BUILTIN_2
1027 #undef RS6000_BUILTIN_3
1028 #undef RS6000_BUILTIN_A
1029 #undef RS6000_BUILTIN_D
1030 #undef RS6000_BUILTIN_E
1031 #undef RS6000_BUILTIN_H
1032 #undef RS6000_BUILTIN_P
1033 #undef RS6000_BUILTIN_Q
1034 #undef RS6000_BUILTIN_S
1035 #undef RS6000_BUILTIN_X
1037 /* Support for -mveclibabi=<xxx> to control which vector library to use. */
1038 static tree (*rs6000_veclib_handler) (tree, tree, tree);
1041 static bool rs6000_debug_legitimate_address_p (enum machine_mode, rtx, bool);
1042 static bool spe_func_has_64bit_regs_p (void);
1043 static struct machine_function * rs6000_init_machine_status (void);
1044 static int rs6000_ra_ever_killed (void);
1045 static tree rs6000_handle_longcall_attribute (tree *, tree, tree, int, bool *);
1046 static tree rs6000_handle_altivec_attribute (tree *, tree, tree, int, bool *);
1047 static tree rs6000_handle_struct_attribute (tree *, tree, tree, int, bool *);
1048 static tree rs6000_builtin_vectorized_libmass (tree, tree, tree);
1049 static rtx rs6000_emit_set_long_const (rtx, HOST_WIDE_INT, HOST_WIDE_INT);
1050 static int rs6000_memory_move_cost (enum machine_mode, reg_class_t, bool);
1051 static bool rs6000_debug_rtx_costs (rtx, int, int, int, int *, bool);
1052 static int rs6000_debug_address_cost (rtx, enum machine_mode, addr_space_t,
1053 bool);
1054 static int rs6000_debug_adjust_cost (rtx, rtx, rtx, int);
1055 static bool is_microcoded_insn (rtx);
1056 static bool is_nonpipeline_insn (rtx);
1057 static bool is_cracked_insn (rtx);
1058 static bool is_load_insn (rtx, rtx *);
1059 static bool is_store_insn (rtx, rtx *);
1060 static bool set_to_load_agen (rtx,rtx);
1061 static bool insn_terminates_group_p (rtx , enum group_termination);
1062 static bool insn_must_be_first_in_group (rtx);
1063 static bool insn_must_be_last_in_group (rtx);
1064 static void altivec_init_builtins (void);
1065 static tree builtin_function_type (enum machine_mode, enum machine_mode,
1066 enum machine_mode, enum machine_mode,
1067 enum rs6000_builtins, const char *name);
1068 static void rs6000_common_init_builtins (void);
1069 static void paired_init_builtins (void);
1070 static rtx paired_expand_predicate_builtin (enum insn_code, tree, rtx);
1071 static void spe_init_builtins (void);
1072 static void htm_init_builtins (void);
1073 static rtx spe_expand_predicate_builtin (enum insn_code, tree, rtx);
1074 static rtx spe_expand_evsel_builtin (enum insn_code, tree, rtx);
1075 static int rs6000_emit_int_cmove (rtx, rtx, rtx, rtx);
1076 static rs6000_stack_t *rs6000_stack_info (void);
1077 static void is_altivec_return_reg (rtx, void *);
1078 int easy_vector_constant (rtx, enum machine_mode);
1079 static rtx rs6000_debug_legitimize_address (rtx, rtx, enum machine_mode);
1080 static rtx rs6000_legitimize_tls_address (rtx, enum tls_model);
1081 static int rs6000_tls_symbol_ref_1 (rtx *, void *);
1082 static int rs6000_get_some_local_dynamic_name_1 (rtx *, void *);
1083 static rtx rs6000_darwin64_record_arg (CUMULATIVE_ARGS *, const_tree,
1084 bool, bool);
1085 #if TARGET_MACHO
1086 static void macho_branch_islands (void);
1087 #endif
1088 static rtx rs6000_legitimize_reload_address (rtx, enum machine_mode, int, int,
1089 int, int *);
1090 static rtx rs6000_debug_legitimize_reload_address (rtx, enum machine_mode, int,
1091 int, int, int *);
1092 static bool rs6000_mode_dependent_address (const_rtx);
1093 static bool rs6000_debug_mode_dependent_address (const_rtx);
1094 static enum reg_class rs6000_secondary_reload_class (enum reg_class,
1095 enum machine_mode, rtx);
1096 static enum reg_class rs6000_debug_secondary_reload_class (enum reg_class,
1097 enum machine_mode,
1098 rtx);
1099 static enum reg_class rs6000_preferred_reload_class (rtx, enum reg_class);
1100 static enum reg_class rs6000_debug_preferred_reload_class (rtx,
1101 enum reg_class);
1102 static bool rs6000_secondary_memory_needed (enum reg_class, enum reg_class,
1103 enum machine_mode);
1104 static bool rs6000_debug_secondary_memory_needed (enum reg_class,
1105 enum reg_class,
1106 enum machine_mode);
1107 static bool rs6000_cannot_change_mode_class (enum machine_mode,
1108 enum machine_mode,
1109 enum reg_class);
1110 static bool rs6000_debug_cannot_change_mode_class (enum machine_mode,
1111 enum machine_mode,
1112 enum reg_class);
1113 static bool rs6000_save_toc_in_prologue_p (void);
1115 rtx (*rs6000_legitimize_reload_address_ptr) (rtx, enum machine_mode, int, int,
1116 int, int *)
1117 = rs6000_legitimize_reload_address;
1119 static bool (*rs6000_mode_dependent_address_ptr) (const_rtx)
1120 = rs6000_mode_dependent_address;
1122 enum reg_class (*rs6000_secondary_reload_class_ptr) (enum reg_class,
1123 enum machine_mode, rtx)
1124 = rs6000_secondary_reload_class;
1126 enum reg_class (*rs6000_preferred_reload_class_ptr) (rtx, enum reg_class)
1127 = rs6000_preferred_reload_class;
1129 bool (*rs6000_secondary_memory_needed_ptr) (enum reg_class, enum reg_class,
1130 enum machine_mode)
1131 = rs6000_secondary_memory_needed;
1133 bool (*rs6000_cannot_change_mode_class_ptr) (enum machine_mode,
1134 enum machine_mode,
1135 enum reg_class)
1136 = rs6000_cannot_change_mode_class;
1138 const int INSN_NOT_AVAILABLE = -1;
1140 static void rs6000_print_isa_options (FILE *, int, const char *,
1141 HOST_WIDE_INT);
1142 static void rs6000_print_builtin_options (FILE *, int, const char *,
1143 HOST_WIDE_INT);
1145 static enum rs6000_reg_type register_to_reg_type (rtx, bool *);
1146 static bool rs6000_secondary_reload_move (enum rs6000_reg_type,
1147 enum rs6000_reg_type,
1148 enum machine_mode,
1149 secondary_reload_info *,
1150 bool);
1152 /* Hash table stuff for keeping track of TOC entries. */
1154 struct GTY(()) toc_hash_struct
1156 /* `key' will satisfy CONSTANT_P; in fact, it will satisfy
1157 ASM_OUTPUT_SPECIAL_POOL_ENTRY_P. */
1158 rtx key;
1159 enum machine_mode key_mode;
1160 int labelno;
1163 static GTY ((param_is (struct toc_hash_struct))) htab_t toc_hash_table;
1165 /* Hash table to keep track of the argument types for builtin functions. */
1167 struct GTY(()) builtin_hash_struct
1169 tree type;
1170 enum machine_mode mode[4]; /* return value + 3 arguments. */
1171 unsigned char uns_p[4]; /* and whether the types are unsigned. */
1174 static GTY ((param_is (struct builtin_hash_struct))) htab_t builtin_hash_table;
1177 /* Default register names. */
1178 char rs6000_reg_names[][8] =
1180 "0", "1", "2", "3", "4", "5", "6", "7",
1181 "8", "9", "10", "11", "12", "13", "14", "15",
1182 "16", "17", "18", "19", "20", "21", "22", "23",
1183 "24", "25", "26", "27", "28", "29", "30", "31",
1184 "0", "1", "2", "3", "4", "5", "6", "7",
1185 "8", "9", "10", "11", "12", "13", "14", "15",
1186 "16", "17", "18", "19", "20", "21", "22", "23",
1187 "24", "25", "26", "27", "28", "29", "30", "31",
1188 "mq", "lr", "ctr","ap",
1189 "0", "1", "2", "3", "4", "5", "6", "7",
1190 "ca",
1191 /* AltiVec registers. */
1192 "0", "1", "2", "3", "4", "5", "6", "7",
1193 "8", "9", "10", "11", "12", "13", "14", "15",
1194 "16", "17", "18", "19", "20", "21", "22", "23",
1195 "24", "25", "26", "27", "28", "29", "30", "31",
1196 "vrsave", "vscr",
1197 /* SPE registers. */
1198 "spe_acc", "spefscr",
1199 /* Soft frame pointer. */
1200 "sfp",
1201 /* HTM SPR registers. */
1202 "tfhar", "tfiar", "texasr"
1205 #ifdef TARGET_REGNAMES
1206 static const char alt_reg_names[][8] =
1208 "%r0", "%r1", "%r2", "%r3", "%r4", "%r5", "%r6", "%r7",
1209 "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15",
1210 "%r16", "%r17", "%r18", "%r19", "%r20", "%r21", "%r22", "%r23",
1211 "%r24", "%r25", "%r26", "%r27", "%r28", "%r29", "%r30", "%r31",
1212 "%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7",
1213 "%f8", "%f9", "%f10", "%f11", "%f12", "%f13", "%f14", "%f15",
1214 "%f16", "%f17", "%f18", "%f19", "%f20", "%f21", "%f22", "%f23",
1215 "%f24", "%f25", "%f26", "%f27", "%f28", "%f29", "%f30", "%f31",
1216 "mq", "lr", "ctr", "ap",
1217 "%cr0", "%cr1", "%cr2", "%cr3", "%cr4", "%cr5", "%cr6", "%cr7",
1218 "ca",
1219 /* AltiVec registers. */
1220 "%v0", "%v1", "%v2", "%v3", "%v4", "%v5", "%v6", "%v7",
1221 "%v8", "%v9", "%v10", "%v11", "%v12", "%v13", "%v14", "%v15",
1222 "%v16", "%v17", "%v18", "%v19", "%v20", "%v21", "%v22", "%v23",
1223 "%v24", "%v25", "%v26", "%v27", "%v28", "%v29", "%v30", "%v31",
1224 "vrsave", "vscr",
1225 /* SPE registers. */
1226 "spe_acc", "spefscr",
1227 /* Soft frame pointer. */
1228 "sfp",
1229 /* HTM SPR registers. */
1230 "tfhar", "tfiar", "texasr"
1232 #endif
1234 /* Table of valid machine attributes. */
1236 static const struct attribute_spec rs6000_attribute_table[] =
1238 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
1239 affects_type_identity } */
1240 { "altivec", 1, 1, false, true, false, rs6000_handle_altivec_attribute,
1241 false },
1242 { "longcall", 0, 0, false, true, true, rs6000_handle_longcall_attribute,
1243 false },
1244 { "shortcall", 0, 0, false, true, true, rs6000_handle_longcall_attribute,
1245 false },
1246 { "ms_struct", 0, 0, false, false, false, rs6000_handle_struct_attribute,
1247 false },
1248 { "gcc_struct", 0, 0, false, false, false, rs6000_handle_struct_attribute,
1249 false },
1250 #ifdef SUBTARGET_ATTRIBUTE_TABLE
1251 SUBTARGET_ATTRIBUTE_TABLE,
1252 #endif
1253 { NULL, 0, 0, false, false, false, NULL, false }
1256 #ifndef TARGET_PROFILE_KERNEL
1257 #define TARGET_PROFILE_KERNEL 0
1258 #endif
1260 /* The VRSAVE bitmask puts bit %v0 as the most significant bit. */
1261 #define ALTIVEC_REG_BIT(REGNO) (0x80000000 >> ((REGNO) - FIRST_ALTIVEC_REGNO))
1263 /* Initialize the GCC target structure. */
1264 #undef TARGET_ATTRIBUTE_TABLE
1265 #define TARGET_ATTRIBUTE_TABLE rs6000_attribute_table
1266 #undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
1267 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES rs6000_set_default_type_attributes
1268 #undef TARGET_ATTRIBUTE_TAKES_IDENTIFIER_P
1269 #define TARGET_ATTRIBUTE_TAKES_IDENTIFIER_P rs6000_attribute_takes_identifier_p
1271 #undef TARGET_ASM_ALIGNED_DI_OP
1272 #define TARGET_ASM_ALIGNED_DI_OP DOUBLE_INT_ASM_OP
1274 /* Default unaligned ops are only provided for ELF. Find the ops needed
1275 for non-ELF systems. */
1276 #ifndef OBJECT_FORMAT_ELF
1277 #if TARGET_XCOFF
1278 /* For XCOFF. rs6000_assemble_integer will handle unaligned DIs on
1279 64-bit targets. */
1280 #undef TARGET_ASM_UNALIGNED_HI_OP
1281 #define TARGET_ASM_UNALIGNED_HI_OP "\t.vbyte\t2,"
1282 #undef TARGET_ASM_UNALIGNED_SI_OP
1283 #define TARGET_ASM_UNALIGNED_SI_OP "\t.vbyte\t4,"
1284 #undef TARGET_ASM_UNALIGNED_DI_OP
1285 #define TARGET_ASM_UNALIGNED_DI_OP "\t.vbyte\t8,"
1286 #else
1287 /* For Darwin. */
1288 #undef TARGET_ASM_UNALIGNED_HI_OP
1289 #define TARGET_ASM_UNALIGNED_HI_OP "\t.short\t"
1290 #undef TARGET_ASM_UNALIGNED_SI_OP
1291 #define TARGET_ASM_UNALIGNED_SI_OP "\t.long\t"
1292 #undef TARGET_ASM_UNALIGNED_DI_OP
1293 #define TARGET_ASM_UNALIGNED_DI_OP "\t.quad\t"
1294 #undef TARGET_ASM_ALIGNED_DI_OP
1295 #define TARGET_ASM_ALIGNED_DI_OP "\t.quad\t"
1296 #endif
1297 #endif
1299 /* This hook deals with fixups for relocatable code and DI-mode objects
1300 in 64-bit code. */
1301 #undef TARGET_ASM_INTEGER
1302 #define TARGET_ASM_INTEGER rs6000_assemble_integer
1304 #if defined (HAVE_GAS_HIDDEN) && !TARGET_MACHO
1305 #undef TARGET_ASM_ASSEMBLE_VISIBILITY
1306 #define TARGET_ASM_ASSEMBLE_VISIBILITY rs6000_assemble_visibility
1307 #endif
1309 #undef TARGET_SET_UP_BY_PROLOGUE
1310 #define TARGET_SET_UP_BY_PROLOGUE rs6000_set_up_by_prologue
1312 #undef TARGET_HAVE_TLS
1313 #define TARGET_HAVE_TLS HAVE_AS_TLS
1315 #undef TARGET_CANNOT_FORCE_CONST_MEM
1316 #define TARGET_CANNOT_FORCE_CONST_MEM rs6000_cannot_force_const_mem
1318 #undef TARGET_DELEGITIMIZE_ADDRESS
1319 #define TARGET_DELEGITIMIZE_ADDRESS rs6000_delegitimize_address
1321 #undef TARGET_CONST_NOT_OK_FOR_DEBUG_P
1322 #define TARGET_CONST_NOT_OK_FOR_DEBUG_P rs6000_const_not_ok_for_debug_p
1324 #undef TARGET_ASM_FUNCTION_PROLOGUE
1325 #define TARGET_ASM_FUNCTION_PROLOGUE rs6000_output_function_prologue
1326 #undef TARGET_ASM_FUNCTION_EPILOGUE
1327 #define TARGET_ASM_FUNCTION_EPILOGUE rs6000_output_function_epilogue
1329 #undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
1330 #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA rs6000_output_addr_const_extra
1332 #undef TARGET_LEGITIMIZE_ADDRESS
1333 #define TARGET_LEGITIMIZE_ADDRESS rs6000_legitimize_address
1335 #undef TARGET_SCHED_VARIABLE_ISSUE
1336 #define TARGET_SCHED_VARIABLE_ISSUE rs6000_variable_issue
1338 #undef TARGET_SCHED_ISSUE_RATE
1339 #define TARGET_SCHED_ISSUE_RATE rs6000_issue_rate
1340 #undef TARGET_SCHED_ADJUST_COST
1341 #define TARGET_SCHED_ADJUST_COST rs6000_adjust_cost
1342 #undef TARGET_SCHED_ADJUST_PRIORITY
1343 #define TARGET_SCHED_ADJUST_PRIORITY rs6000_adjust_priority
1344 #undef TARGET_SCHED_IS_COSTLY_DEPENDENCE
1345 #define TARGET_SCHED_IS_COSTLY_DEPENDENCE rs6000_is_costly_dependence
1346 #undef TARGET_SCHED_INIT
1347 #define TARGET_SCHED_INIT rs6000_sched_init
1348 #undef TARGET_SCHED_FINISH
1349 #define TARGET_SCHED_FINISH rs6000_sched_finish
1350 #undef TARGET_SCHED_REORDER
1351 #define TARGET_SCHED_REORDER rs6000_sched_reorder
1352 #undef TARGET_SCHED_REORDER2
1353 #define TARGET_SCHED_REORDER2 rs6000_sched_reorder2
1355 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
1356 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD rs6000_use_sched_lookahead
1358 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD
1359 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD rs6000_use_sched_lookahead_guard
1361 #undef TARGET_SCHED_ALLOC_SCHED_CONTEXT
1362 #define TARGET_SCHED_ALLOC_SCHED_CONTEXT rs6000_alloc_sched_context
1363 #undef TARGET_SCHED_INIT_SCHED_CONTEXT
1364 #define TARGET_SCHED_INIT_SCHED_CONTEXT rs6000_init_sched_context
1365 #undef TARGET_SCHED_SET_SCHED_CONTEXT
1366 #define TARGET_SCHED_SET_SCHED_CONTEXT rs6000_set_sched_context
1367 #undef TARGET_SCHED_FREE_SCHED_CONTEXT
1368 #define TARGET_SCHED_FREE_SCHED_CONTEXT rs6000_free_sched_context
1370 #undef TARGET_VECTORIZE_BUILTIN_MASK_FOR_LOAD
1371 #define TARGET_VECTORIZE_BUILTIN_MASK_FOR_LOAD rs6000_builtin_mask_for_load
1372 #undef TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT
1373 #define TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT \
1374 rs6000_builtin_support_vector_misalignment
1375 #undef TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE
1376 #define TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE rs6000_vector_alignment_reachable
1377 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST
1378 #define TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST \
1379 rs6000_builtin_vectorization_cost
1380 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
1381 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE \
1382 rs6000_preferred_simd_mode
1383 #undef TARGET_VECTORIZE_INIT_COST
1384 #define TARGET_VECTORIZE_INIT_COST rs6000_init_cost
1385 #undef TARGET_VECTORIZE_ADD_STMT_COST
1386 #define TARGET_VECTORIZE_ADD_STMT_COST rs6000_add_stmt_cost
1387 #undef TARGET_VECTORIZE_FINISH_COST
1388 #define TARGET_VECTORIZE_FINISH_COST rs6000_finish_cost
1389 #undef TARGET_VECTORIZE_DESTROY_COST_DATA
1390 #define TARGET_VECTORIZE_DESTROY_COST_DATA rs6000_destroy_cost_data
1392 #undef TARGET_INIT_BUILTINS
1393 #define TARGET_INIT_BUILTINS rs6000_init_builtins
1394 #undef TARGET_BUILTIN_DECL
1395 #define TARGET_BUILTIN_DECL rs6000_builtin_decl
1397 #undef TARGET_EXPAND_BUILTIN
1398 #define TARGET_EXPAND_BUILTIN rs6000_expand_builtin
1400 #undef TARGET_MANGLE_TYPE
1401 #define TARGET_MANGLE_TYPE rs6000_mangle_type
1403 #undef TARGET_INIT_LIBFUNCS
1404 #define TARGET_INIT_LIBFUNCS rs6000_init_libfuncs
1406 #if TARGET_MACHO
1407 #undef TARGET_BINDS_LOCAL_P
1408 #define TARGET_BINDS_LOCAL_P darwin_binds_local_p
1409 #endif
1411 #undef TARGET_MS_BITFIELD_LAYOUT_P
1412 #define TARGET_MS_BITFIELD_LAYOUT_P rs6000_ms_bitfield_layout_p
1414 #undef TARGET_ASM_OUTPUT_MI_THUNK
1415 #define TARGET_ASM_OUTPUT_MI_THUNK rs6000_output_mi_thunk
1417 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
1418 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
1420 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
1421 #define TARGET_FUNCTION_OK_FOR_SIBCALL rs6000_function_ok_for_sibcall
1423 #undef TARGET_REGISTER_MOVE_COST
1424 #define TARGET_REGISTER_MOVE_COST rs6000_register_move_cost
1425 #undef TARGET_MEMORY_MOVE_COST
1426 #define TARGET_MEMORY_MOVE_COST rs6000_memory_move_cost
1427 #undef TARGET_RTX_COSTS
1428 #define TARGET_RTX_COSTS rs6000_rtx_costs
1429 #undef TARGET_ADDRESS_COST
1430 #define TARGET_ADDRESS_COST hook_int_rtx_mode_as_bool_0
1432 #undef TARGET_DWARF_REGISTER_SPAN
1433 #define TARGET_DWARF_REGISTER_SPAN rs6000_dwarf_register_span
1435 #undef TARGET_INIT_DWARF_REG_SIZES_EXTRA
1436 #define TARGET_INIT_DWARF_REG_SIZES_EXTRA rs6000_init_dwarf_reg_sizes_extra
1438 #undef TARGET_MEMBER_TYPE_FORCES_BLK
1439 #define TARGET_MEMBER_TYPE_FORCES_BLK rs6000_member_type_forces_blk
1441 /* On rs6000, function arguments are promoted, as are function return
1442 values. */
1443 #undef TARGET_PROMOTE_FUNCTION_MODE
1444 #define TARGET_PROMOTE_FUNCTION_MODE default_promote_function_mode_always_promote
1446 #undef TARGET_RETURN_IN_MEMORY
1447 #define TARGET_RETURN_IN_MEMORY rs6000_return_in_memory
1449 #undef TARGET_SETUP_INCOMING_VARARGS
1450 #define TARGET_SETUP_INCOMING_VARARGS setup_incoming_varargs
1452 /* Always strict argument naming on rs6000. */
1453 #undef TARGET_STRICT_ARGUMENT_NAMING
1454 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
1455 #undef TARGET_PRETEND_OUTGOING_VARARGS_NAMED
1456 #define TARGET_PRETEND_OUTGOING_VARARGS_NAMED hook_bool_CUMULATIVE_ARGS_true
1457 #undef TARGET_SPLIT_COMPLEX_ARG
1458 #define TARGET_SPLIT_COMPLEX_ARG hook_bool_const_tree_true
1459 #undef TARGET_MUST_PASS_IN_STACK
1460 #define TARGET_MUST_PASS_IN_STACK rs6000_must_pass_in_stack
1461 #undef TARGET_PASS_BY_REFERENCE
1462 #define TARGET_PASS_BY_REFERENCE rs6000_pass_by_reference
1463 #undef TARGET_ARG_PARTIAL_BYTES
1464 #define TARGET_ARG_PARTIAL_BYTES rs6000_arg_partial_bytes
1465 #undef TARGET_FUNCTION_ARG_ADVANCE
1466 #define TARGET_FUNCTION_ARG_ADVANCE rs6000_function_arg_advance
1467 #undef TARGET_FUNCTION_ARG
1468 #define TARGET_FUNCTION_ARG rs6000_function_arg
1469 #undef TARGET_FUNCTION_ARG_BOUNDARY
1470 #define TARGET_FUNCTION_ARG_BOUNDARY rs6000_function_arg_boundary
1472 #undef TARGET_BUILD_BUILTIN_VA_LIST
1473 #define TARGET_BUILD_BUILTIN_VA_LIST rs6000_build_builtin_va_list
1475 #undef TARGET_EXPAND_BUILTIN_VA_START
1476 #define TARGET_EXPAND_BUILTIN_VA_START rs6000_va_start
1478 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
1479 #define TARGET_GIMPLIFY_VA_ARG_EXPR rs6000_gimplify_va_arg
1481 #undef TARGET_EH_RETURN_FILTER_MODE
1482 #define TARGET_EH_RETURN_FILTER_MODE rs6000_eh_return_filter_mode
1484 #undef TARGET_SCALAR_MODE_SUPPORTED_P
1485 #define TARGET_SCALAR_MODE_SUPPORTED_P rs6000_scalar_mode_supported_p
1487 #undef TARGET_VECTOR_MODE_SUPPORTED_P
1488 #define TARGET_VECTOR_MODE_SUPPORTED_P rs6000_vector_mode_supported_p
1490 #undef TARGET_INVALID_ARG_FOR_UNPROTOTYPED_FN
1491 #define TARGET_INVALID_ARG_FOR_UNPROTOTYPED_FN invalid_arg_for_unprototyped_fn
1493 #undef TARGET_ASM_LOOP_ALIGN_MAX_SKIP
1494 #define TARGET_ASM_LOOP_ALIGN_MAX_SKIP rs6000_loop_align_max_skip
1496 #undef TARGET_OPTION_OVERRIDE
1497 #define TARGET_OPTION_OVERRIDE rs6000_option_override
1499 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION
1500 #define TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION \
1501 rs6000_builtin_vectorized_function
1503 #if !TARGET_MACHO
1504 #undef TARGET_STACK_PROTECT_FAIL
1505 #define TARGET_STACK_PROTECT_FAIL rs6000_stack_protect_fail
1506 #endif
1508 /* MPC604EUM 3.5.2 Weak Consistency between Multiple Processors
1509 The PowerPC architecture requires only weak consistency among
1510 processors--that is, memory accesses between processors need not be
1511 sequentially consistent and memory accesses among processors can occur
1512 in any order. The ability to order memory accesses weakly provides
1513 opportunities for more efficient use of the system bus. Unless a
1514 dependency exists, the 604e allows read operations to precede store
1515 operations. */
1516 #undef TARGET_RELAXED_ORDERING
1517 #define TARGET_RELAXED_ORDERING true
1519 #ifdef HAVE_AS_TLS
1520 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
1521 #define TARGET_ASM_OUTPUT_DWARF_DTPREL rs6000_output_dwarf_dtprel
1522 #endif
1524 /* Use a 32-bit anchor range. This leads to sequences like:
1526 addis tmp,anchor,high
1527 add dest,tmp,low
1529 where tmp itself acts as an anchor, and can be shared between
1530 accesses to the same 64k page. */
1531 #undef TARGET_MIN_ANCHOR_OFFSET
1532 #define TARGET_MIN_ANCHOR_OFFSET -0x7fffffff - 1
1533 #undef TARGET_MAX_ANCHOR_OFFSET
1534 #define TARGET_MAX_ANCHOR_OFFSET 0x7fffffff
1535 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
1536 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P rs6000_use_blocks_for_constant_p
1537 #undef TARGET_USE_BLOCKS_FOR_DECL_P
1538 #define TARGET_USE_BLOCKS_FOR_DECL_P rs6000_use_blocks_for_decl_p
1540 #undef TARGET_BUILTIN_RECIPROCAL
1541 #define TARGET_BUILTIN_RECIPROCAL rs6000_builtin_reciprocal
1543 #undef TARGET_EXPAND_TO_RTL_HOOK
1544 #define TARGET_EXPAND_TO_RTL_HOOK rs6000_alloc_sdmode_stack_slot
1546 #undef TARGET_INSTANTIATE_DECLS
1547 #define TARGET_INSTANTIATE_DECLS rs6000_instantiate_decls
1549 #undef TARGET_SECONDARY_RELOAD
1550 #define TARGET_SECONDARY_RELOAD rs6000_secondary_reload
1552 #undef TARGET_LEGITIMATE_ADDRESS_P
1553 #define TARGET_LEGITIMATE_ADDRESS_P rs6000_legitimate_address_p
1555 #undef TARGET_MODE_DEPENDENT_ADDRESS_P
1556 #define TARGET_MODE_DEPENDENT_ADDRESS_P rs6000_mode_dependent_address_p
1558 #undef TARGET_LRA_P
1559 #define TARGET_LRA_P rs6000_lra_p
1561 #undef TARGET_CAN_ELIMINATE
1562 #define TARGET_CAN_ELIMINATE rs6000_can_eliminate
1564 #undef TARGET_CONDITIONAL_REGISTER_USAGE
1565 #define TARGET_CONDITIONAL_REGISTER_USAGE rs6000_conditional_register_usage
1567 #undef TARGET_TRAMPOLINE_INIT
1568 #define TARGET_TRAMPOLINE_INIT rs6000_trampoline_init
1570 #undef TARGET_FUNCTION_VALUE
1571 #define TARGET_FUNCTION_VALUE rs6000_function_value
1573 #undef TARGET_OPTION_VALID_ATTRIBUTE_P
1574 #define TARGET_OPTION_VALID_ATTRIBUTE_P rs6000_valid_attribute_p
1576 #undef TARGET_OPTION_SAVE
1577 #define TARGET_OPTION_SAVE rs6000_function_specific_save
1579 #undef TARGET_OPTION_RESTORE
1580 #define TARGET_OPTION_RESTORE rs6000_function_specific_restore
1582 #undef TARGET_OPTION_PRINT
1583 #define TARGET_OPTION_PRINT rs6000_function_specific_print
1585 #undef TARGET_CAN_INLINE_P
1586 #define TARGET_CAN_INLINE_P rs6000_can_inline_p
1588 #undef TARGET_SET_CURRENT_FUNCTION
1589 #define TARGET_SET_CURRENT_FUNCTION rs6000_set_current_function
1591 #undef TARGET_LEGITIMATE_CONSTANT_P
1592 #define TARGET_LEGITIMATE_CONSTANT_P rs6000_legitimate_constant_p
1594 #undef TARGET_VECTORIZE_VEC_PERM_CONST_OK
1595 #define TARGET_VECTORIZE_VEC_PERM_CONST_OK rs6000_vectorize_vec_perm_const_ok
1597 #undef TARGET_CAN_USE_DOLOOP_P
1598 #define TARGET_CAN_USE_DOLOOP_P can_use_doloop_if_innermost
1601 /* Processor table. */
1602 struct rs6000_ptt
1604 const char *const name; /* Canonical processor name. */
1605 const enum processor_type processor; /* Processor type enum value. */
1606 const HOST_WIDE_INT target_enable; /* Target flags to enable. */
1609 static struct rs6000_ptt const processor_target_table[] =
1611 #define RS6000_CPU(NAME, CPU, FLAGS) { NAME, CPU, FLAGS },
1612 #include "rs6000-cpus.def"
1613 #undef RS6000_CPU
1616 /* Look up a processor name for -mcpu=xxx and -mtune=xxx. Return -1 if the
1617 name is invalid. */
1619 static int
1620 rs6000_cpu_name_lookup (const char *name)
1622 size_t i;
1624 if (name != NULL)
1626 for (i = 0; i < ARRAY_SIZE (processor_target_table); i++)
1627 if (! strcmp (name, processor_target_table[i].name))
1628 return (int)i;
1631 return -1;
1635 /* Return number of consecutive hard regs needed starting at reg REGNO
1636 to hold something of mode MODE.
1637 This is ordinarily the length in words of a value of mode MODE
1638 but can be less for certain modes in special long registers.
1640 For the SPE, GPRs are 64 bits but only 32 bits are visible in
1641 scalar instructions. The upper 32 bits are only available to the
1642 SIMD instructions.
1644 POWER and PowerPC GPRs hold 32 bits worth;
1645 PowerPC64 GPRs and FPRs point register holds 64 bits worth. */
1647 static int
1648 rs6000_hard_regno_nregs_internal (int regno, enum machine_mode mode)
1650 unsigned HOST_WIDE_INT reg_size;
1652 /* TF/TD modes are special in that they always take 2 registers. */
1653 if (FP_REGNO_P (regno))
1654 reg_size = ((VECTOR_MEM_VSX_P (mode) && mode != TDmode && mode != TFmode)
1655 ? UNITS_PER_VSX_WORD
1656 : UNITS_PER_FP_WORD);
1658 else if (SPE_SIMD_REGNO_P (regno) && TARGET_SPE && SPE_VECTOR_MODE (mode))
1659 reg_size = UNITS_PER_SPE_WORD;
1661 else if (ALTIVEC_REGNO_P (regno))
1662 reg_size = UNITS_PER_ALTIVEC_WORD;
1664 /* The value returned for SCmode in the E500 double case is 2 for
1665 ABI compatibility; storing an SCmode value in a single register
1666 would require function_arg and rs6000_spe_function_arg to handle
1667 SCmode so as to pass the value correctly in a pair of
1668 registers. */
1669 else if (TARGET_E500_DOUBLE && FLOAT_MODE_P (mode) && mode != SCmode
1670 && !DECIMAL_FLOAT_MODE_P (mode))
1671 reg_size = UNITS_PER_FP_WORD;
1673 else
1674 reg_size = UNITS_PER_WORD;
1676 return (GET_MODE_SIZE (mode) + reg_size - 1) / reg_size;
1679 /* Value is 1 if hard register REGNO can hold a value of machine-mode
1680 MODE. */
1681 static int
1682 rs6000_hard_regno_mode_ok (int regno, enum machine_mode mode)
1684 int last_regno = regno + rs6000_hard_regno_nregs[mode][regno] - 1;
1686 /* PTImode can only go in GPRs. Quad word memory operations require even/odd
1687 register combinations, and use PTImode where we need to deal with quad
1688 word memory operations. Don't allow quad words in the argument or frame
1689 pointer registers, just registers 0..31. */
1690 if (mode == PTImode)
1691 return (IN_RANGE (regno, FIRST_GPR_REGNO, LAST_GPR_REGNO)
1692 && IN_RANGE (last_regno, FIRST_GPR_REGNO, LAST_GPR_REGNO)
1693 && ((regno & 1) == 0));
1695 /* VSX registers that overlap the FPR registers are larger than for non-VSX
1696 implementations. Don't allow an item to be split between a FP register
1697 and an Altivec register. Allow TImode in all VSX registers if the user
1698 asked for it. */
1699 if (TARGET_VSX && VSX_REGNO_P (regno)
1700 && (VECTOR_MEM_VSX_P (mode)
1701 || (TARGET_VSX_SCALAR_FLOAT && mode == SFmode)
1702 || (TARGET_VSX_SCALAR_DOUBLE && (mode == DFmode || mode == DImode))
1703 || (TARGET_VSX_TIMODE && mode == TImode)))
1705 if (FP_REGNO_P (regno))
1706 return FP_REGNO_P (last_regno);
1708 if (ALTIVEC_REGNO_P (regno))
1710 if (mode == SFmode && !TARGET_UPPER_REGS_SF)
1711 return 0;
1713 if ((mode == DFmode || mode == DImode) && !TARGET_UPPER_REGS_DF)
1714 return 0;
1716 return ALTIVEC_REGNO_P (last_regno);
1720 /* The GPRs can hold any mode, but values bigger than one register
1721 cannot go past R31. */
1722 if (INT_REGNO_P (regno))
1723 return INT_REGNO_P (last_regno);
1725 /* The float registers (except for VSX vector modes) can only hold floating
1726 modes and DImode. */
1727 if (FP_REGNO_P (regno))
1729 if (SCALAR_FLOAT_MODE_P (mode)
1730 && (mode != TDmode || (regno % 2) == 0)
1731 && FP_REGNO_P (last_regno))
1732 return 1;
1734 if (GET_MODE_CLASS (mode) == MODE_INT
1735 && GET_MODE_SIZE (mode) == UNITS_PER_FP_WORD)
1736 return 1;
1738 if (PAIRED_SIMD_REGNO_P (regno) && TARGET_PAIRED_FLOAT
1739 && PAIRED_VECTOR_MODE (mode))
1740 return 1;
1742 return 0;
1745 /* The CR register can only hold CC modes. */
1746 if (CR_REGNO_P (regno))
1747 return GET_MODE_CLASS (mode) == MODE_CC;
1749 if (CA_REGNO_P (regno))
1750 return mode == BImode;
1752 /* AltiVec only in AldyVec registers. */
1753 if (ALTIVEC_REGNO_P (regno))
1754 return VECTOR_MEM_ALTIVEC_OR_VSX_P (mode);
1756 /* ...but GPRs can hold SIMD data on the SPE in one register. */
1757 if (SPE_SIMD_REGNO_P (regno) && TARGET_SPE && SPE_VECTOR_MODE (mode))
1758 return 1;
1760 /* We cannot put non-VSX TImode or PTImode anywhere except general register
1761 and it must be able to fit within the register set. */
1763 return GET_MODE_SIZE (mode) <= UNITS_PER_WORD;
1766 /* Print interesting facts about registers. */
1767 static void
1768 rs6000_debug_reg_print (int first_regno, int last_regno, const char *reg_name)
1770 int r, m;
1772 for (r = first_regno; r <= last_regno; ++r)
1774 const char *comma = "";
1775 int len;
1777 if (first_regno == last_regno)
1778 fprintf (stderr, "%s:\t", reg_name);
1779 else
1780 fprintf (stderr, "%s%d:\t", reg_name, r - first_regno);
1782 len = 8;
1783 for (m = 0; m < NUM_MACHINE_MODES; ++m)
1784 if (rs6000_hard_regno_mode_ok_p[m][r] && rs6000_hard_regno_nregs[m][r])
1786 if (len > 70)
1788 fprintf (stderr, ",\n\t");
1789 len = 8;
1790 comma = "";
1793 if (rs6000_hard_regno_nregs[m][r] > 1)
1794 len += fprintf (stderr, "%s%s/%d", comma, GET_MODE_NAME (m),
1795 rs6000_hard_regno_nregs[m][r]);
1796 else
1797 len += fprintf (stderr, "%s%s", comma, GET_MODE_NAME (m));
1799 comma = ", ";
1802 if (call_used_regs[r])
1804 if (len > 70)
1806 fprintf (stderr, ",\n\t");
1807 len = 8;
1808 comma = "";
1811 len += fprintf (stderr, "%s%s", comma, "call-used");
1812 comma = ", ";
1815 if (fixed_regs[r])
1817 if (len > 70)
1819 fprintf (stderr, ",\n\t");
1820 len = 8;
1821 comma = "";
1824 len += fprintf (stderr, "%s%s", comma, "fixed");
1825 comma = ", ";
1828 if (len > 70)
1830 fprintf (stderr, ",\n\t");
1831 comma = "";
1834 len += fprintf (stderr, "%sreg-class = %s", comma,
1835 reg_class_names[(int)rs6000_regno_regclass[r]]);
1836 comma = ", ";
1838 if (len > 70)
1840 fprintf (stderr, ",\n\t");
1841 comma = "";
1844 fprintf (stderr, "%sregno = %d\n", comma, r);
1848 static const char *
1849 rs6000_debug_vector_unit (enum rs6000_vector v)
1851 const char *ret;
1853 switch (v)
1855 case VECTOR_NONE: ret = "none"; break;
1856 case VECTOR_ALTIVEC: ret = "altivec"; break;
1857 case VECTOR_VSX: ret = "vsx"; break;
1858 case VECTOR_P8_VECTOR: ret = "p8_vector"; break;
1859 case VECTOR_PAIRED: ret = "paired"; break;
1860 case VECTOR_SPE: ret = "spe"; break;
1861 case VECTOR_OTHER: ret = "other"; break;
1862 default: ret = "unknown"; break;
1865 return ret;
1868 /* Print the address masks in a human readble fashion. */
1869 DEBUG_FUNCTION void
1870 rs6000_debug_print_mode (ssize_t m)
1872 ssize_t rc;
1874 fprintf (stderr, "Mode: %-5s", GET_MODE_NAME (m));
1875 for (rc = 0; rc < N_RELOAD_REG; rc++)
1877 addr_mask_type mask = reg_addr[m].addr_mask[rc];
1878 fprintf (stderr,
1879 " %s: %c%c%c%c%c%c",
1880 reload_reg_map[rc].name,
1881 (mask & RELOAD_REG_VALID) != 0 ? 'v' : ' ',
1882 (mask & RELOAD_REG_MULTIPLE) != 0 ? 'm' : ' ',
1883 (mask & RELOAD_REG_INDEXED) != 0 ? 'i' : ' ',
1884 (mask & RELOAD_REG_OFFSET) != 0 ? 'o' : ' ',
1885 (mask & RELOAD_REG_PRE_INCDEC) != 0 ? '+' : ' ',
1886 (mask & RELOAD_REG_PRE_MODIFY) != 0 ? '+' : ' ');
1889 if (rs6000_vector_unit[m] != VECTOR_NONE
1890 || rs6000_vector_mem[m] != VECTOR_NONE
1891 || (reg_addr[m].reload_store != CODE_FOR_nothing)
1892 || (reg_addr[m].reload_load != CODE_FOR_nothing))
1894 fprintf (stderr,
1895 " Vector-arith=%-10s Vector-mem=%-10s Reload=%c%c",
1896 rs6000_debug_vector_unit (rs6000_vector_unit[m]),
1897 rs6000_debug_vector_unit (rs6000_vector_mem[m]),
1898 (reg_addr[m].reload_store != CODE_FOR_nothing) ? 's' : '*',
1899 (reg_addr[m].reload_load != CODE_FOR_nothing) ? 'l' : '*');
1902 fputs ("\n", stderr);
1905 #define DEBUG_FMT_ID "%-32s= "
1906 #define DEBUG_FMT_D DEBUG_FMT_ID "%d\n"
1907 #define DEBUG_FMT_WX DEBUG_FMT_ID "%#.12" HOST_WIDE_INT_PRINT "x: "
1908 #define DEBUG_FMT_S DEBUG_FMT_ID "%s\n"
1910 /* Print various interesting information with -mdebug=reg. */
1911 static void
1912 rs6000_debug_reg_global (void)
1914 static const char *const tf[2] = { "false", "true" };
1915 const char *nl = (const char *)0;
1916 int m;
1917 size_t m1, m2, v;
1918 char costly_num[20];
1919 char nop_num[20];
1920 char flags_buffer[40];
1921 const char *costly_str;
1922 const char *nop_str;
1923 const char *trace_str;
1924 const char *abi_str;
1925 const char *cmodel_str;
1926 struct cl_target_option cl_opts;
1928 /* Modes we want tieable information on. */
1929 static const enum machine_mode print_tieable_modes[] = {
1930 QImode,
1931 HImode,
1932 SImode,
1933 DImode,
1934 TImode,
1935 PTImode,
1936 SFmode,
1937 DFmode,
1938 TFmode,
1939 SDmode,
1940 DDmode,
1941 TDmode,
1942 V8QImode,
1943 V4HImode,
1944 V2SImode,
1945 V16QImode,
1946 V8HImode,
1947 V4SImode,
1948 V2DImode,
1949 V32QImode,
1950 V16HImode,
1951 V8SImode,
1952 V4DImode,
1953 V2SFmode,
1954 V4SFmode,
1955 V2DFmode,
1956 V8SFmode,
1957 V4DFmode,
1958 CCmode,
1959 CCUNSmode,
1960 CCEQmode,
1963 /* Virtual regs we are interested in. */
1964 const static struct {
1965 int regno; /* register number. */
1966 const char *name; /* register name. */
1967 } virtual_regs[] = {
1968 { STACK_POINTER_REGNUM, "stack pointer:" },
1969 { TOC_REGNUM, "toc: " },
1970 { STATIC_CHAIN_REGNUM, "static chain: " },
1971 { RS6000_PIC_OFFSET_TABLE_REGNUM, "pic offset: " },
1972 { HARD_FRAME_POINTER_REGNUM, "hard frame: " },
1973 { ARG_POINTER_REGNUM, "arg pointer: " },
1974 { FRAME_POINTER_REGNUM, "frame pointer:" },
1975 { FIRST_PSEUDO_REGISTER, "first pseudo: " },
1976 { FIRST_VIRTUAL_REGISTER, "first virtual:" },
1977 { VIRTUAL_INCOMING_ARGS_REGNUM, "incoming_args:" },
1978 { VIRTUAL_STACK_VARS_REGNUM, "stack_vars: " },
1979 { VIRTUAL_STACK_DYNAMIC_REGNUM, "stack_dynamic:" },
1980 { VIRTUAL_OUTGOING_ARGS_REGNUM, "outgoing_args:" },
1981 { VIRTUAL_CFA_REGNUM, "cfa (frame): " },
1982 { VIRTUAL_PREFERRED_STACK_BOUNDARY_REGNUM, "stack boundry:" },
1983 { LAST_VIRTUAL_REGISTER, "last virtual: " },
1986 fputs ("\nHard register information:\n", stderr);
1987 rs6000_debug_reg_print (FIRST_GPR_REGNO, LAST_GPR_REGNO, "gr");
1988 rs6000_debug_reg_print (FIRST_FPR_REGNO, LAST_FPR_REGNO, "fp");
1989 rs6000_debug_reg_print (FIRST_ALTIVEC_REGNO,
1990 LAST_ALTIVEC_REGNO,
1991 "vs");
1992 rs6000_debug_reg_print (LR_REGNO, LR_REGNO, "lr");
1993 rs6000_debug_reg_print (CTR_REGNO, CTR_REGNO, "ctr");
1994 rs6000_debug_reg_print (CR0_REGNO, CR7_REGNO, "cr");
1995 rs6000_debug_reg_print (CA_REGNO, CA_REGNO, "ca");
1996 rs6000_debug_reg_print (VRSAVE_REGNO, VRSAVE_REGNO, "vrsave");
1997 rs6000_debug_reg_print (VSCR_REGNO, VSCR_REGNO, "vscr");
1998 rs6000_debug_reg_print (SPE_ACC_REGNO, SPE_ACC_REGNO, "spe_a");
1999 rs6000_debug_reg_print (SPEFSCR_REGNO, SPEFSCR_REGNO, "spe_f");
2001 fputs ("\nVirtual/stack/frame registers:\n", stderr);
2002 for (v = 0; v < ARRAY_SIZE (virtual_regs); v++)
2003 fprintf (stderr, "%s regno = %3d\n", virtual_regs[v].name, virtual_regs[v].regno);
2005 fprintf (stderr,
2006 "\n"
2007 "d reg_class = %s\n"
2008 "f reg_class = %s\n"
2009 "v reg_class = %s\n"
2010 "wa reg_class = %s\n"
2011 "wd reg_class = %s\n"
2012 "wf reg_class = %s\n"
2013 "wg reg_class = %s\n"
2014 "wl reg_class = %s\n"
2015 "wm reg_class = %s\n"
2016 "wr reg_class = %s\n"
2017 "ws reg_class = %s\n"
2018 "wt reg_class = %s\n"
2019 "wu reg_class = %s\n"
2020 "wv reg_class = %s\n"
2021 "ww reg_class = %s\n"
2022 "wx reg_class = %s\n"
2023 "wy reg_class = %s\n"
2024 "wz reg_class = %s\n"
2025 "\n",
2026 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_d]],
2027 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_f]],
2028 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_v]],
2029 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wa]],
2030 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wd]],
2031 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wf]],
2032 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wg]],
2033 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wl]],
2034 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wm]],
2035 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wr]],
2036 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_ws]],
2037 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wt]],
2038 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wu]],
2039 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wv]],
2040 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_ww]],
2041 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wx]],
2042 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wy]],
2043 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wz]]);
2045 nl = "\n";
2046 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2047 rs6000_debug_print_mode (m);
2049 fputs ("\n", stderr);
2051 for (m1 = 0; m1 < ARRAY_SIZE (print_tieable_modes); m1++)
2053 enum machine_mode mode1 = print_tieable_modes[m1];
2054 bool first_time = true;
2056 nl = (const char *)0;
2057 for (m2 = 0; m2 < ARRAY_SIZE (print_tieable_modes); m2++)
2059 enum machine_mode mode2 = print_tieable_modes[m2];
2060 if (mode1 != mode2 && MODES_TIEABLE_P (mode1, mode2))
2062 if (first_time)
2064 fprintf (stderr, "Tieable modes %s:", GET_MODE_NAME (mode1));
2065 nl = "\n";
2066 first_time = false;
2069 fprintf (stderr, " %s", GET_MODE_NAME (mode2));
2073 if (!first_time)
2074 fputs ("\n", stderr);
2077 if (nl)
2078 fputs (nl, stderr);
2080 if (rs6000_recip_control)
2082 fprintf (stderr, "\nReciprocal mask = 0x%x\n", rs6000_recip_control);
2084 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2085 if (rs6000_recip_bits[m])
2087 fprintf (stderr,
2088 "Reciprocal estimate mode: %-5s divide: %s rsqrt: %s\n",
2089 GET_MODE_NAME (m),
2090 (RS6000_RECIP_AUTO_RE_P (m)
2091 ? "auto"
2092 : (RS6000_RECIP_HAVE_RE_P (m) ? "have" : "none")),
2093 (RS6000_RECIP_AUTO_RSQRTE_P (m)
2094 ? "auto"
2095 : (RS6000_RECIP_HAVE_RSQRTE_P (m) ? "have" : "none")));
2098 fputs ("\n", stderr);
2101 if (rs6000_cpu_index >= 0)
2103 const char *name = processor_target_table[rs6000_cpu_index].name;
2104 HOST_WIDE_INT flags
2105 = processor_target_table[rs6000_cpu_index].target_enable;
2107 sprintf (flags_buffer, "-mcpu=%s flags", name);
2108 rs6000_print_isa_options (stderr, 0, flags_buffer, flags);
2110 else
2111 fprintf (stderr, DEBUG_FMT_S, "cpu", "<none>");
2113 if (rs6000_tune_index >= 0)
2115 const char *name = processor_target_table[rs6000_tune_index].name;
2116 HOST_WIDE_INT flags
2117 = processor_target_table[rs6000_tune_index].target_enable;
2119 sprintf (flags_buffer, "-mtune=%s flags", name);
2120 rs6000_print_isa_options (stderr, 0, flags_buffer, flags);
2122 else
2123 fprintf (stderr, DEBUG_FMT_S, "tune", "<none>");
2125 cl_target_option_save (&cl_opts, &global_options);
2126 rs6000_print_isa_options (stderr, 0, "rs6000_isa_flags",
2127 rs6000_isa_flags);
2129 rs6000_print_isa_options (stderr, 0, "rs6000_isa_flags_explicit",
2130 rs6000_isa_flags_explicit);
2132 rs6000_print_builtin_options (stderr, 0, "rs6000_builtin_mask",
2133 rs6000_builtin_mask);
2135 rs6000_print_isa_options (stderr, 0, "TARGET_DEFAULT", TARGET_DEFAULT);
2137 fprintf (stderr, DEBUG_FMT_S, "--with-cpu default",
2138 OPTION_TARGET_CPU_DEFAULT ? OPTION_TARGET_CPU_DEFAULT : "<none>");
2140 switch (rs6000_sched_costly_dep)
2142 case max_dep_latency:
2143 costly_str = "max_dep_latency";
2144 break;
2146 case no_dep_costly:
2147 costly_str = "no_dep_costly";
2148 break;
2150 case all_deps_costly:
2151 costly_str = "all_deps_costly";
2152 break;
2154 case true_store_to_load_dep_costly:
2155 costly_str = "true_store_to_load_dep_costly";
2156 break;
2158 case store_to_load_dep_costly:
2159 costly_str = "store_to_load_dep_costly";
2160 break;
2162 default:
2163 costly_str = costly_num;
2164 sprintf (costly_num, "%d", (int)rs6000_sched_costly_dep);
2165 break;
2168 fprintf (stderr, DEBUG_FMT_S, "sched_costly_dep", costly_str);
2170 switch (rs6000_sched_insert_nops)
2172 case sched_finish_regroup_exact:
2173 nop_str = "sched_finish_regroup_exact";
2174 break;
2176 case sched_finish_pad_groups:
2177 nop_str = "sched_finish_pad_groups";
2178 break;
2180 case sched_finish_none:
2181 nop_str = "sched_finish_none";
2182 break;
2184 default:
2185 nop_str = nop_num;
2186 sprintf (nop_num, "%d", (int)rs6000_sched_insert_nops);
2187 break;
2190 fprintf (stderr, DEBUG_FMT_S, "sched_insert_nops", nop_str);
2192 switch (rs6000_sdata)
2194 default:
2195 case SDATA_NONE:
2196 break;
2198 case SDATA_DATA:
2199 fprintf (stderr, DEBUG_FMT_S, "sdata", "data");
2200 break;
2202 case SDATA_SYSV:
2203 fprintf (stderr, DEBUG_FMT_S, "sdata", "sysv");
2204 break;
2206 case SDATA_EABI:
2207 fprintf (stderr, DEBUG_FMT_S, "sdata", "eabi");
2208 break;
2212 switch (rs6000_traceback)
2214 case traceback_default: trace_str = "default"; break;
2215 case traceback_none: trace_str = "none"; break;
2216 case traceback_part: trace_str = "part"; break;
2217 case traceback_full: trace_str = "full"; break;
2218 default: trace_str = "unknown"; break;
2221 fprintf (stderr, DEBUG_FMT_S, "traceback", trace_str);
2223 switch (rs6000_current_cmodel)
2225 case CMODEL_SMALL: cmodel_str = "small"; break;
2226 case CMODEL_MEDIUM: cmodel_str = "medium"; break;
2227 case CMODEL_LARGE: cmodel_str = "large"; break;
2228 default: cmodel_str = "unknown"; break;
2231 fprintf (stderr, DEBUG_FMT_S, "cmodel", cmodel_str);
2233 switch (rs6000_current_abi)
2235 case ABI_NONE: abi_str = "none"; break;
2236 case ABI_AIX: abi_str = "aix"; break;
2237 case ABI_V4: abi_str = "V4"; break;
2238 case ABI_DARWIN: abi_str = "darwin"; break;
2239 default: abi_str = "unknown"; break;
2242 fprintf (stderr, DEBUG_FMT_S, "abi", abi_str);
2244 if (rs6000_altivec_abi)
2245 fprintf (stderr, DEBUG_FMT_S, "altivec_abi", "true");
2247 if (rs6000_spe_abi)
2248 fprintf (stderr, DEBUG_FMT_S, "spe_abi", "true");
2250 if (rs6000_darwin64_abi)
2251 fprintf (stderr, DEBUG_FMT_S, "darwin64_abi", "true");
2253 if (rs6000_float_gprs)
2254 fprintf (stderr, DEBUG_FMT_S, "float_gprs", "true");
2256 if (TARGET_LINK_STACK)
2257 fprintf (stderr, DEBUG_FMT_S, "link_stack", "true");
2259 if (targetm.lra_p ())
2260 fprintf (stderr, DEBUG_FMT_S, "lra", "true");
2262 if (TARGET_P8_FUSION)
2263 fprintf (stderr, DEBUG_FMT_S, "p8 fusion",
2264 (TARGET_P8_FUSION_SIGN) ? "zero+sign" : "zero");
2266 fprintf (stderr, DEBUG_FMT_S, "plt-format",
2267 TARGET_SECURE_PLT ? "secure" : "bss");
2268 fprintf (stderr, DEBUG_FMT_S, "struct-return",
2269 aix_struct_return ? "aix" : "sysv");
2270 fprintf (stderr, DEBUG_FMT_S, "always_hint", tf[!!rs6000_always_hint]);
2271 fprintf (stderr, DEBUG_FMT_S, "sched_groups", tf[!!rs6000_sched_groups]);
2272 fprintf (stderr, DEBUG_FMT_S, "align_branch",
2273 tf[!!rs6000_align_branch_targets]);
2274 fprintf (stderr, DEBUG_FMT_D, "tls_size", rs6000_tls_size);
2275 fprintf (stderr, DEBUG_FMT_D, "long_double_size",
2276 rs6000_long_double_type_size);
2277 fprintf (stderr, DEBUG_FMT_D, "sched_restricted_insns_priority",
2278 (int)rs6000_sched_restricted_insns_priority);
2279 fprintf (stderr, DEBUG_FMT_D, "Number of standard builtins",
2280 (int)END_BUILTINS);
2281 fprintf (stderr, DEBUG_FMT_D, "Number of rs6000 builtins",
2282 (int)RS6000_BUILTIN_COUNT);
2286 /* Update the addr mask bits in reg_addr to help secondary reload and go if
2287 legitimate address support to figure out the appropriate addressing to
2288 use. */
2290 static void
2291 rs6000_setup_reg_addr_masks (void)
2293 ssize_t rc, reg, m, nregs;
2294 addr_mask_type any_addr_mask, addr_mask;
2296 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2298 /* SDmode is special in that we want to access it only via REG+REG
2299 addressing on power7 and above, since we want to use the LFIWZX and
2300 STFIWZX instructions to load it. */
2301 bool indexed_only_p = (m == SDmode && TARGET_NO_SDMODE_STACK);
2303 any_addr_mask = 0;
2304 for (rc = FIRST_RELOAD_REG_CLASS; rc <= LAST_RELOAD_REG_CLASS; rc++)
2306 addr_mask = 0;
2307 reg = reload_reg_map[rc].reg;
2309 /* Can mode values go in the GPR/FPR/Altivec registers? */
2310 if (reg >= 0 && rs6000_hard_regno_mode_ok_p[m][reg])
2312 nregs = rs6000_hard_regno_nregs[m][reg];
2313 addr_mask |= RELOAD_REG_VALID;
2315 /* Indicate if the mode takes more than 1 physical register. If
2316 it takes a single register, indicate it can do REG+REG
2317 addressing. */
2318 if (nregs > 1 || m == BLKmode)
2319 addr_mask |= RELOAD_REG_MULTIPLE;
2320 else
2321 addr_mask |= RELOAD_REG_INDEXED;
2323 /* Figure out if we can do PRE_INC, PRE_DEC, or PRE_MODIFY
2324 addressing. Restrict addressing on SPE for 64-bit types
2325 because of the SUBREG hackery used to address 64-bit floats in
2326 '32-bit' GPRs. To simplify secondary reload, don't allow
2327 update forms on scalar floating point types that can go in the
2328 upper registers. */
2330 if (TARGET_UPDATE
2331 && (rc == RELOAD_REG_GPR || rc == RELOAD_REG_FPR)
2332 && GET_MODE_SIZE (m) <= 8
2333 && !VECTOR_MODE_P (m)
2334 && !COMPLEX_MODE_P (m)
2335 && !indexed_only_p
2336 && !(TARGET_E500_DOUBLE && GET_MODE_SIZE (m) == 8)
2337 && !(m == DFmode && TARGET_UPPER_REGS_DF)
2338 && !(m == SFmode && TARGET_UPPER_REGS_SF))
2340 addr_mask |= RELOAD_REG_PRE_INCDEC;
2342 /* PRE_MODIFY is more restricted than PRE_INC/PRE_DEC in that
2343 we don't allow PRE_MODIFY for some multi-register
2344 operations. */
2345 switch (m)
2347 default:
2348 addr_mask |= RELOAD_REG_PRE_MODIFY;
2349 break;
2351 case DImode:
2352 if (TARGET_POWERPC64)
2353 addr_mask |= RELOAD_REG_PRE_MODIFY;
2354 break;
2356 case DFmode:
2357 case DDmode:
2358 if (TARGET_DF_INSN)
2359 addr_mask |= RELOAD_REG_PRE_MODIFY;
2360 break;
2365 /* GPR and FPR registers can do REG+OFFSET addressing, except
2366 possibly for SDmode. */
2367 if ((addr_mask != 0) && !indexed_only_p
2368 && (rc == RELOAD_REG_GPR || rc == RELOAD_REG_FPR))
2369 addr_mask |= RELOAD_REG_OFFSET;
2371 reg_addr[m].addr_mask[rc] = addr_mask;
2372 any_addr_mask |= addr_mask;
2375 reg_addr[m].addr_mask[RELOAD_REG_ANY] = any_addr_mask;
2380 /* Initialize the various global tables that are based on register size. */
2381 static void
2382 rs6000_init_hard_regno_mode_ok (bool global_init_p)
2384 ssize_t r, m, c;
2385 int align64;
2386 int align32;
2388 /* Precalculate REGNO_REG_CLASS. */
2389 rs6000_regno_regclass[0] = GENERAL_REGS;
2390 for (r = 1; r < 32; ++r)
2391 rs6000_regno_regclass[r] = BASE_REGS;
2393 for (r = 32; r < 64; ++r)
2394 rs6000_regno_regclass[r] = FLOAT_REGS;
2396 for (r = 64; r < FIRST_PSEUDO_REGISTER; ++r)
2397 rs6000_regno_regclass[r] = NO_REGS;
2399 for (r = FIRST_ALTIVEC_REGNO; r <= LAST_ALTIVEC_REGNO; ++r)
2400 rs6000_regno_regclass[r] = ALTIVEC_REGS;
2402 rs6000_regno_regclass[CR0_REGNO] = CR0_REGS;
2403 for (r = CR1_REGNO; r <= CR7_REGNO; ++r)
2404 rs6000_regno_regclass[r] = CR_REGS;
2406 rs6000_regno_regclass[LR_REGNO] = LINK_REGS;
2407 rs6000_regno_regclass[CTR_REGNO] = CTR_REGS;
2408 rs6000_regno_regclass[CA_REGNO] = CA_REGS;
2409 rs6000_regno_regclass[VRSAVE_REGNO] = VRSAVE_REGS;
2410 rs6000_regno_regclass[VSCR_REGNO] = VRSAVE_REGS;
2411 rs6000_regno_regclass[SPE_ACC_REGNO] = SPE_ACC_REGS;
2412 rs6000_regno_regclass[SPEFSCR_REGNO] = SPEFSCR_REGS;
2413 rs6000_regno_regclass[TFHAR_REGNO] = SPR_REGS;
2414 rs6000_regno_regclass[TFIAR_REGNO] = SPR_REGS;
2415 rs6000_regno_regclass[TEXASR_REGNO] = SPR_REGS;
2416 rs6000_regno_regclass[ARG_POINTER_REGNUM] = BASE_REGS;
2417 rs6000_regno_regclass[FRAME_POINTER_REGNUM] = BASE_REGS;
2419 /* Precalculate register class to simpler reload register class. We don't
2420 need all of the register classes that are combinations of different
2421 classes, just the simple ones that have constraint letters. */
2422 for (c = 0; c < N_REG_CLASSES; c++)
2423 reg_class_to_reg_type[c] = NO_REG_TYPE;
2425 reg_class_to_reg_type[(int)GENERAL_REGS] = GPR_REG_TYPE;
2426 reg_class_to_reg_type[(int)BASE_REGS] = GPR_REG_TYPE;
2427 reg_class_to_reg_type[(int)VSX_REGS] = VSX_REG_TYPE;
2428 reg_class_to_reg_type[(int)VRSAVE_REGS] = SPR_REG_TYPE;
2429 reg_class_to_reg_type[(int)VSCR_REGS] = SPR_REG_TYPE;
2430 reg_class_to_reg_type[(int)LINK_REGS] = SPR_REG_TYPE;
2431 reg_class_to_reg_type[(int)CTR_REGS] = SPR_REG_TYPE;
2432 reg_class_to_reg_type[(int)LINK_OR_CTR_REGS] = SPR_REG_TYPE;
2433 reg_class_to_reg_type[(int)CR_REGS] = CR_REG_TYPE;
2434 reg_class_to_reg_type[(int)CR0_REGS] = CR_REG_TYPE;
2435 reg_class_to_reg_type[(int)SPE_ACC_REGS] = SPE_ACC_TYPE;
2436 reg_class_to_reg_type[(int)SPEFSCR_REGS] = SPEFSCR_REG_TYPE;
2438 if (TARGET_VSX)
2440 reg_class_to_reg_type[(int)FLOAT_REGS] = VSX_REG_TYPE;
2441 reg_class_to_reg_type[(int)ALTIVEC_REGS] = VSX_REG_TYPE;
2443 else
2445 reg_class_to_reg_type[(int)FLOAT_REGS] = FPR_REG_TYPE;
2446 reg_class_to_reg_type[(int)ALTIVEC_REGS] = ALTIVEC_REG_TYPE;
2449 /* Precalculate the valid memory formats as well as the vector information,
2450 this must be set up before the rs6000_hard_regno_nregs_internal calls
2451 below. */
2452 gcc_assert ((int)VECTOR_NONE == 0);
2453 memset ((void *) &rs6000_vector_unit[0], '\0', sizeof (rs6000_vector_unit));
2454 memset ((void *) &rs6000_vector_mem[0], '\0', sizeof (rs6000_vector_unit));
2456 gcc_assert ((int)CODE_FOR_nothing == 0);
2457 memset ((void *) &reg_addr[0], '\0', sizeof (reg_addr));
2459 gcc_assert ((int)NO_REGS == 0);
2460 memset ((void *) &rs6000_constraints[0], '\0', sizeof (rs6000_constraints));
2462 /* The VSX hardware allows native alignment for vectors, but control whether the compiler
2463 believes it can use native alignment or still uses 128-bit alignment. */
2464 if (TARGET_VSX && !TARGET_VSX_ALIGN_128)
2466 align64 = 64;
2467 align32 = 32;
2469 else
2471 align64 = 128;
2472 align32 = 128;
2475 /* V2DF mode, VSX only. */
2476 if (TARGET_VSX)
2478 rs6000_vector_unit[V2DFmode] = VECTOR_VSX;
2479 rs6000_vector_mem[V2DFmode] = VECTOR_VSX;
2480 rs6000_vector_align[V2DFmode] = align64;
2483 /* V4SF mode, either VSX or Altivec. */
2484 if (TARGET_VSX)
2486 rs6000_vector_unit[V4SFmode] = VECTOR_VSX;
2487 rs6000_vector_mem[V4SFmode] = VECTOR_VSX;
2488 rs6000_vector_align[V4SFmode] = align32;
2490 else if (TARGET_ALTIVEC)
2492 rs6000_vector_unit[V4SFmode] = VECTOR_ALTIVEC;
2493 rs6000_vector_mem[V4SFmode] = VECTOR_ALTIVEC;
2494 rs6000_vector_align[V4SFmode] = align32;
2497 /* V16QImode, V8HImode, V4SImode are Altivec only, but possibly do VSX loads
2498 and stores. */
2499 if (TARGET_ALTIVEC)
2501 rs6000_vector_unit[V4SImode] = VECTOR_ALTIVEC;
2502 rs6000_vector_unit[V8HImode] = VECTOR_ALTIVEC;
2503 rs6000_vector_unit[V16QImode] = VECTOR_ALTIVEC;
2504 rs6000_vector_align[V4SImode] = align32;
2505 rs6000_vector_align[V8HImode] = align32;
2506 rs6000_vector_align[V16QImode] = align32;
2508 if (TARGET_VSX)
2510 rs6000_vector_mem[V4SImode] = VECTOR_VSX;
2511 rs6000_vector_mem[V8HImode] = VECTOR_VSX;
2512 rs6000_vector_mem[V16QImode] = VECTOR_VSX;
2514 else
2516 rs6000_vector_mem[V4SImode] = VECTOR_ALTIVEC;
2517 rs6000_vector_mem[V8HImode] = VECTOR_ALTIVEC;
2518 rs6000_vector_mem[V16QImode] = VECTOR_ALTIVEC;
2522 /* V2DImode, full mode depends on ISA 2.07 vector mode. Allow under VSX to
2523 do insert/splat/extract. Altivec doesn't have 64-bit integer support. */
2524 if (TARGET_VSX)
2526 rs6000_vector_mem[V2DImode] = VECTOR_VSX;
2527 rs6000_vector_unit[V2DImode]
2528 = (TARGET_P8_VECTOR) ? VECTOR_P8_VECTOR : VECTOR_NONE;
2529 rs6000_vector_align[V2DImode] = align64;
2532 /* DFmode, see if we want to use the VSX unit. */
2533 if (TARGET_VSX && TARGET_VSX_SCALAR_DOUBLE)
2535 rs6000_vector_unit[DFmode] = VECTOR_VSX;
2536 rs6000_vector_mem[DFmode]
2537 = (TARGET_UPPER_REGS_DF ? VECTOR_VSX : VECTOR_NONE);
2538 rs6000_vector_align[DFmode] = align64;
2541 /* Allow TImode in VSX register and set the VSX memory macros. */
2542 if (TARGET_VSX && TARGET_VSX_TIMODE)
2544 rs6000_vector_mem[TImode] = VECTOR_VSX;
2545 rs6000_vector_align[TImode] = align64;
2548 /* TODO add SPE and paired floating point vector support. */
2550 /* Register class constraints for the constraints that depend on compile
2551 switches. When the VSX code was added, different constraints were added
2552 based on the type (DFmode, V2DFmode, V4SFmode). For the vector types, all
2553 of the VSX registers are used. The register classes for scalar floating
2554 point types is set, based on whether we allow that type into the upper
2555 (Altivec) registers. GCC has register classes to target the Altivec
2556 registers for load/store operations, to select using a VSX memory
2557 operation instead of the traditional floating point operation. The
2558 constraints are:
2560 d - Register class to use with traditional DFmode instructions.
2561 f - Register class to use with traditional SFmode instructions.
2562 v - Altivec register.
2563 wa - Any VSX register.
2564 wd - Preferred register class for V2DFmode.
2565 wf - Preferred register class for V4SFmode.
2566 wg - Float register for power6x move insns.
2567 wl - Float register if we can do 32-bit signed int loads.
2568 wm - VSX register for ISA 2.07 direct move operations.
2569 wr - GPR if 64-bit mode is permitted.
2570 ws - Register class to do ISA 2.06 DF operations.
2571 wu - Altivec register for ISA 2.07 VSX SF/SI load/stores.
2572 wv - Altivec register for ISA 2.06 VSX DF/DI load/stores.
2573 wt - VSX register for TImode in VSX registers.
2574 ww - Register class to do SF conversions in with VSX operations.
2575 wx - Float register if we can do 32-bit int stores.
2576 wy - Register class to do ISA 2.07 SF operations.
2577 wz - Float register if we can do 32-bit unsigned int loads. */
2579 if (TARGET_HARD_FLOAT && TARGET_FPRS)
2580 rs6000_constraints[RS6000_CONSTRAINT_f] = FLOAT_REGS;
2582 if (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
2583 rs6000_constraints[RS6000_CONSTRAINT_d] = FLOAT_REGS;
2585 if (TARGET_VSX)
2587 rs6000_constraints[RS6000_CONSTRAINT_wa] = VSX_REGS;
2588 rs6000_constraints[RS6000_CONSTRAINT_wd] = VSX_REGS;
2589 rs6000_constraints[RS6000_CONSTRAINT_wf] = VSX_REGS;
2591 if (TARGET_VSX_TIMODE)
2592 rs6000_constraints[RS6000_CONSTRAINT_wt] = VSX_REGS;
2594 if (TARGET_UPPER_REGS_DF)
2596 rs6000_constraints[RS6000_CONSTRAINT_ws] = VSX_REGS;
2597 rs6000_constraints[RS6000_CONSTRAINT_wv] = ALTIVEC_REGS;
2599 else
2600 rs6000_constraints[RS6000_CONSTRAINT_ws] = FLOAT_REGS;
2603 /* Add conditional constraints based on various options, to allow us to
2604 collapse multiple insn patterns. */
2605 if (TARGET_ALTIVEC)
2606 rs6000_constraints[RS6000_CONSTRAINT_v] = ALTIVEC_REGS;
2608 if (TARGET_MFPGPR)
2609 rs6000_constraints[RS6000_CONSTRAINT_wg] = FLOAT_REGS;
2611 if (TARGET_LFIWAX)
2612 rs6000_constraints[RS6000_CONSTRAINT_wl] = FLOAT_REGS;
2614 if (TARGET_DIRECT_MOVE)
2615 rs6000_constraints[RS6000_CONSTRAINT_wm] = VSX_REGS;
2617 if (TARGET_POWERPC64)
2618 rs6000_constraints[RS6000_CONSTRAINT_wr] = GENERAL_REGS;
2620 if (TARGET_P8_VECTOR && TARGET_UPPER_REGS_SF)
2622 rs6000_constraints[RS6000_CONSTRAINT_wu] = ALTIVEC_REGS;
2623 rs6000_constraints[RS6000_CONSTRAINT_wy] = VSX_REGS;
2624 rs6000_constraints[RS6000_CONSTRAINT_ww] = VSX_REGS;
2626 else if (TARGET_P8_VECTOR)
2628 rs6000_constraints[RS6000_CONSTRAINT_wy] = FLOAT_REGS;
2629 rs6000_constraints[RS6000_CONSTRAINT_ww] = FLOAT_REGS;
2631 else if (TARGET_VSX)
2632 rs6000_constraints[RS6000_CONSTRAINT_ww] = FLOAT_REGS;
2634 if (TARGET_STFIWX)
2635 rs6000_constraints[RS6000_CONSTRAINT_wx] = FLOAT_REGS;
2637 if (TARGET_LFIWZX)
2638 rs6000_constraints[RS6000_CONSTRAINT_wz] = FLOAT_REGS;
2640 /* Set up the reload helper and direct move functions. */
2641 if (TARGET_VSX || TARGET_ALTIVEC)
2643 if (TARGET_64BIT)
2645 reg_addr[V16QImode].reload_store = CODE_FOR_reload_v16qi_di_store;
2646 reg_addr[V16QImode].reload_load = CODE_FOR_reload_v16qi_di_load;
2647 reg_addr[V8HImode].reload_store = CODE_FOR_reload_v8hi_di_store;
2648 reg_addr[V8HImode].reload_load = CODE_FOR_reload_v8hi_di_load;
2649 reg_addr[V4SImode].reload_store = CODE_FOR_reload_v4si_di_store;
2650 reg_addr[V4SImode].reload_load = CODE_FOR_reload_v4si_di_load;
2651 reg_addr[V2DImode].reload_store = CODE_FOR_reload_v2di_di_store;
2652 reg_addr[V2DImode].reload_load = CODE_FOR_reload_v2di_di_load;
2653 reg_addr[V4SFmode].reload_store = CODE_FOR_reload_v4sf_di_store;
2654 reg_addr[V4SFmode].reload_load = CODE_FOR_reload_v4sf_di_load;
2655 reg_addr[V2DFmode].reload_store = CODE_FOR_reload_v2df_di_store;
2656 reg_addr[V2DFmode].reload_load = CODE_FOR_reload_v2df_di_load;
2657 if (TARGET_VSX && TARGET_UPPER_REGS_DF)
2659 reg_addr[DFmode].reload_store = CODE_FOR_reload_df_di_store;
2660 reg_addr[DFmode].reload_load = CODE_FOR_reload_df_di_load;
2661 reg_addr[DDmode].reload_store = CODE_FOR_reload_dd_di_store;
2662 reg_addr[DDmode].reload_load = CODE_FOR_reload_dd_di_load;
2664 if (TARGET_P8_VECTOR)
2666 reg_addr[SFmode].reload_store = CODE_FOR_reload_sf_di_store;
2667 reg_addr[SFmode].reload_load = CODE_FOR_reload_sf_di_load;
2668 reg_addr[SDmode].reload_store = CODE_FOR_reload_sd_di_store;
2669 reg_addr[SDmode].reload_load = CODE_FOR_reload_sd_di_load;
2671 if (TARGET_VSX_TIMODE)
2673 reg_addr[TImode].reload_store = CODE_FOR_reload_ti_di_store;
2674 reg_addr[TImode].reload_load = CODE_FOR_reload_ti_di_load;
2676 if (TARGET_DIRECT_MOVE)
2678 if (TARGET_POWERPC64)
2680 reg_addr[TImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxti;
2681 reg_addr[V2DFmode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv2df;
2682 reg_addr[V2DImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv2di;
2683 reg_addr[V4SFmode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv4sf;
2684 reg_addr[V4SImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv4si;
2685 reg_addr[V8HImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv8hi;
2686 reg_addr[V16QImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv16qi;
2687 reg_addr[SFmode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxsf;
2689 reg_addr[TImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprti;
2690 reg_addr[V2DFmode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv2df;
2691 reg_addr[V2DImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv2di;
2692 reg_addr[V4SFmode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv4sf;
2693 reg_addr[V4SImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv4si;
2694 reg_addr[V8HImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv8hi;
2695 reg_addr[V16QImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv16qi;
2696 reg_addr[SFmode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprsf;
2698 else
2700 reg_addr[DImode].reload_fpr_gpr = CODE_FOR_reload_fpr_from_gprdi;
2701 reg_addr[DDmode].reload_fpr_gpr = CODE_FOR_reload_fpr_from_gprdd;
2702 reg_addr[DFmode].reload_fpr_gpr = CODE_FOR_reload_fpr_from_gprdf;
2706 else
2708 reg_addr[V16QImode].reload_store = CODE_FOR_reload_v16qi_si_store;
2709 reg_addr[V16QImode].reload_load = CODE_FOR_reload_v16qi_si_load;
2710 reg_addr[V8HImode].reload_store = CODE_FOR_reload_v8hi_si_store;
2711 reg_addr[V8HImode].reload_load = CODE_FOR_reload_v8hi_si_load;
2712 reg_addr[V4SImode].reload_store = CODE_FOR_reload_v4si_si_store;
2713 reg_addr[V4SImode].reload_load = CODE_FOR_reload_v4si_si_load;
2714 reg_addr[V2DImode].reload_store = CODE_FOR_reload_v2di_si_store;
2715 reg_addr[V2DImode].reload_load = CODE_FOR_reload_v2di_si_load;
2716 reg_addr[V4SFmode].reload_store = CODE_FOR_reload_v4sf_si_store;
2717 reg_addr[V4SFmode].reload_load = CODE_FOR_reload_v4sf_si_load;
2718 reg_addr[V2DFmode].reload_store = CODE_FOR_reload_v2df_si_store;
2719 reg_addr[V2DFmode].reload_load = CODE_FOR_reload_v2df_si_load;
2720 if (TARGET_VSX && TARGET_UPPER_REGS_DF)
2722 reg_addr[DFmode].reload_store = CODE_FOR_reload_df_si_store;
2723 reg_addr[DFmode].reload_load = CODE_FOR_reload_df_si_load;
2724 reg_addr[DDmode].reload_store = CODE_FOR_reload_dd_si_store;
2725 reg_addr[DDmode].reload_load = CODE_FOR_reload_dd_si_load;
2727 if (TARGET_P8_VECTOR)
2729 reg_addr[SFmode].reload_store = CODE_FOR_reload_sf_si_store;
2730 reg_addr[SFmode].reload_load = CODE_FOR_reload_sf_si_load;
2731 reg_addr[SDmode].reload_store = CODE_FOR_reload_sd_si_store;
2732 reg_addr[SDmode].reload_load = CODE_FOR_reload_sd_si_load;
2734 if (TARGET_VSX_TIMODE)
2736 reg_addr[TImode].reload_store = CODE_FOR_reload_ti_si_store;
2737 reg_addr[TImode].reload_load = CODE_FOR_reload_ti_si_load;
2742 /* Precalculate HARD_REGNO_NREGS. */
2743 for (r = 0; r < FIRST_PSEUDO_REGISTER; ++r)
2744 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2745 rs6000_hard_regno_nregs[m][r]
2746 = rs6000_hard_regno_nregs_internal (r, (enum machine_mode)m);
2748 /* Precalculate HARD_REGNO_MODE_OK. */
2749 for (r = 0; r < FIRST_PSEUDO_REGISTER; ++r)
2750 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2751 if (rs6000_hard_regno_mode_ok (r, (enum machine_mode)m))
2752 rs6000_hard_regno_mode_ok_p[m][r] = true;
2754 /* Precalculate CLASS_MAX_NREGS sizes. */
2755 for (c = 0; c < LIM_REG_CLASSES; ++c)
2757 int reg_size;
2759 if (TARGET_VSX && VSX_REG_CLASS_P (c))
2760 reg_size = UNITS_PER_VSX_WORD;
2762 else if (c == ALTIVEC_REGS)
2763 reg_size = UNITS_PER_ALTIVEC_WORD;
2765 else if (c == FLOAT_REGS)
2766 reg_size = UNITS_PER_FP_WORD;
2768 else
2769 reg_size = UNITS_PER_WORD;
2771 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2773 int reg_size2 = reg_size;
2775 /* TFmode/TDmode always takes 2 registers, even in VSX. */
2776 if (TARGET_VSX && VSX_REG_CLASS_P (c)
2777 && (m == TDmode || m == TFmode))
2778 reg_size2 = UNITS_PER_FP_WORD;
2780 rs6000_class_max_nregs[m][c]
2781 = (GET_MODE_SIZE (m) + reg_size2 - 1) / reg_size2;
2785 if (TARGET_E500_DOUBLE)
2786 rs6000_class_max_nregs[DFmode][GENERAL_REGS] = 1;
2788 /* Calculate which modes to automatically generate code to use a the
2789 reciprocal divide and square root instructions. In the future, possibly
2790 automatically generate the instructions even if the user did not specify
2791 -mrecip. The older machines double precision reciprocal sqrt estimate is
2792 not accurate enough. */
2793 memset (rs6000_recip_bits, 0, sizeof (rs6000_recip_bits));
2794 if (TARGET_FRES)
2795 rs6000_recip_bits[SFmode] = RS6000_RECIP_MASK_HAVE_RE;
2796 if (TARGET_FRE)
2797 rs6000_recip_bits[DFmode] = RS6000_RECIP_MASK_HAVE_RE;
2798 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode))
2799 rs6000_recip_bits[V4SFmode] = RS6000_RECIP_MASK_HAVE_RE;
2800 if (VECTOR_UNIT_VSX_P (V2DFmode))
2801 rs6000_recip_bits[V2DFmode] = RS6000_RECIP_MASK_HAVE_RE;
2803 if (TARGET_FRSQRTES)
2804 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2805 if (TARGET_FRSQRTE)
2806 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2807 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode))
2808 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2809 if (VECTOR_UNIT_VSX_P (V2DFmode))
2810 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2812 if (rs6000_recip_control)
2814 if (!flag_finite_math_only)
2815 warning (0, "-mrecip requires -ffinite-math or -ffast-math");
2816 if (flag_trapping_math)
2817 warning (0, "-mrecip requires -fno-trapping-math or -ffast-math");
2818 if (!flag_reciprocal_math)
2819 warning (0, "-mrecip requires -freciprocal-math or -ffast-math");
2820 if (flag_finite_math_only && !flag_trapping_math && flag_reciprocal_math)
2822 if (RS6000_RECIP_HAVE_RE_P (SFmode)
2823 && (rs6000_recip_control & RECIP_SF_DIV) != 0)
2824 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2826 if (RS6000_RECIP_HAVE_RE_P (DFmode)
2827 && (rs6000_recip_control & RECIP_DF_DIV) != 0)
2828 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2830 if (RS6000_RECIP_HAVE_RE_P (V4SFmode)
2831 && (rs6000_recip_control & RECIP_V4SF_DIV) != 0)
2832 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2834 if (RS6000_RECIP_HAVE_RE_P (V2DFmode)
2835 && (rs6000_recip_control & RECIP_V2DF_DIV) != 0)
2836 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2838 if (RS6000_RECIP_HAVE_RSQRTE_P (SFmode)
2839 && (rs6000_recip_control & RECIP_SF_RSQRT) != 0)
2840 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2842 if (RS6000_RECIP_HAVE_RSQRTE_P (DFmode)
2843 && (rs6000_recip_control & RECIP_DF_RSQRT) != 0)
2844 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2846 if (RS6000_RECIP_HAVE_RSQRTE_P (V4SFmode)
2847 && (rs6000_recip_control & RECIP_V4SF_RSQRT) != 0)
2848 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2850 if (RS6000_RECIP_HAVE_RSQRTE_P (V2DFmode)
2851 && (rs6000_recip_control & RECIP_V2DF_RSQRT) != 0)
2852 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2856 /* Update the addr mask bits in reg_addr to help secondary reload and go if
2857 legitimate address support to figure out the appropriate addressing to
2858 use. */
2859 rs6000_setup_reg_addr_masks ();
2861 if (global_init_p || TARGET_DEBUG_TARGET)
2863 if (TARGET_DEBUG_REG)
2864 rs6000_debug_reg_global ();
2866 if (TARGET_DEBUG_COST || TARGET_DEBUG_REG)
2867 fprintf (stderr,
2868 "SImode variable mult cost = %d\n"
2869 "SImode constant mult cost = %d\n"
2870 "SImode short constant mult cost = %d\n"
2871 "DImode multipliciation cost = %d\n"
2872 "SImode division cost = %d\n"
2873 "DImode division cost = %d\n"
2874 "Simple fp operation cost = %d\n"
2875 "DFmode multiplication cost = %d\n"
2876 "SFmode division cost = %d\n"
2877 "DFmode division cost = %d\n"
2878 "cache line size = %d\n"
2879 "l1 cache size = %d\n"
2880 "l2 cache size = %d\n"
2881 "simultaneous prefetches = %d\n"
2882 "\n",
2883 rs6000_cost->mulsi,
2884 rs6000_cost->mulsi_const,
2885 rs6000_cost->mulsi_const9,
2886 rs6000_cost->muldi,
2887 rs6000_cost->divsi,
2888 rs6000_cost->divdi,
2889 rs6000_cost->fp,
2890 rs6000_cost->dmul,
2891 rs6000_cost->sdiv,
2892 rs6000_cost->ddiv,
2893 rs6000_cost->cache_line_size,
2894 rs6000_cost->l1_cache_size,
2895 rs6000_cost->l2_cache_size,
2896 rs6000_cost->simultaneous_prefetches);
2900 #if TARGET_MACHO
2901 /* The Darwin version of SUBTARGET_OVERRIDE_OPTIONS. */
2903 static void
2904 darwin_rs6000_override_options (void)
2906 /* The Darwin ABI always includes AltiVec, can't be (validly) turned
2907 off. */
2908 rs6000_altivec_abi = 1;
2909 TARGET_ALTIVEC_VRSAVE = 1;
2910 rs6000_current_abi = ABI_DARWIN;
2912 if (DEFAULT_ABI == ABI_DARWIN
2913 && TARGET_64BIT)
2914 darwin_one_byte_bool = 1;
2916 if (TARGET_64BIT && ! TARGET_POWERPC64)
2918 rs6000_isa_flags |= OPTION_MASK_POWERPC64;
2919 warning (0, "-m64 requires PowerPC64 architecture, enabling");
2921 if (flag_mkernel)
2923 rs6000_default_long_calls = 1;
2924 rs6000_isa_flags |= OPTION_MASK_SOFT_FLOAT;
2927 /* Make -m64 imply -maltivec. Darwin's 64-bit ABI includes
2928 Altivec. */
2929 if (!flag_mkernel && !flag_apple_kext
2930 && TARGET_64BIT
2931 && ! (rs6000_isa_flags_explicit & OPTION_MASK_ALTIVEC))
2932 rs6000_isa_flags |= OPTION_MASK_ALTIVEC;
2934 /* Unless the user (not the configurer) has explicitly overridden
2935 it with -mcpu=G3 or -mno-altivec, then 10.5+ targets default to
2936 G4 unless targeting the kernel. */
2937 if (!flag_mkernel
2938 && !flag_apple_kext
2939 && strverscmp (darwin_macosx_version_min, "10.5") >= 0
2940 && ! (rs6000_isa_flags_explicit & OPTION_MASK_ALTIVEC)
2941 && ! global_options_set.x_rs6000_cpu_index)
2943 rs6000_isa_flags |= OPTION_MASK_ALTIVEC;
2946 #endif
2948 /* If not otherwise specified by a target, make 'long double' equivalent to
2949 'double'. */
2951 #ifndef RS6000_DEFAULT_LONG_DOUBLE_SIZE
2952 #define RS6000_DEFAULT_LONG_DOUBLE_SIZE 64
2953 #endif
2955 /* Return the builtin mask of the various options used that could affect which
2956 builtins were used. In the past we used target_flags, but we've run out of
2957 bits, and some options like SPE and PAIRED are no longer in
2958 target_flags. */
2960 HOST_WIDE_INT
2961 rs6000_builtin_mask_calculate (void)
2963 return (((TARGET_ALTIVEC) ? RS6000_BTM_ALTIVEC : 0)
2964 | ((TARGET_VSX) ? RS6000_BTM_VSX : 0)
2965 | ((TARGET_SPE) ? RS6000_BTM_SPE : 0)
2966 | ((TARGET_PAIRED_FLOAT) ? RS6000_BTM_PAIRED : 0)
2967 | ((TARGET_FRE) ? RS6000_BTM_FRE : 0)
2968 | ((TARGET_FRES) ? RS6000_BTM_FRES : 0)
2969 | ((TARGET_FRSQRTE) ? RS6000_BTM_FRSQRTE : 0)
2970 | ((TARGET_FRSQRTES) ? RS6000_BTM_FRSQRTES : 0)
2971 | ((TARGET_POPCNTD) ? RS6000_BTM_POPCNTD : 0)
2972 | ((rs6000_cpu == PROCESSOR_CELL) ? RS6000_BTM_CELL : 0)
2973 | ((TARGET_P8_VECTOR) ? RS6000_BTM_P8_VECTOR : 0)
2974 | ((TARGET_CRYPTO) ? RS6000_BTM_CRYPTO : 0)
2975 | ((TARGET_HTM) ? RS6000_BTM_HTM : 0));
2978 /* Override command line options. Mostly we process the processor type and
2979 sometimes adjust other TARGET_ options. */
2981 static bool
2982 rs6000_option_override_internal (bool global_init_p)
2984 bool ret = true;
2985 bool have_cpu = false;
2987 /* The default cpu requested at configure time, if any. */
2988 const char *implicit_cpu = OPTION_TARGET_CPU_DEFAULT;
2990 HOST_WIDE_INT set_masks;
2991 int cpu_index;
2992 int tune_index;
2993 struct cl_target_option *main_target_opt
2994 = ((global_init_p || target_option_default_node == NULL)
2995 ? NULL : TREE_TARGET_OPTION (target_option_default_node));
2997 /* Remember the explicit arguments. */
2998 if (global_init_p)
2999 rs6000_isa_flags_explicit = global_options_set.x_rs6000_isa_flags;
3001 /* On 64-bit Darwin, power alignment is ABI-incompatible with some C
3002 library functions, so warn about it. The flag may be useful for
3003 performance studies from time to time though, so don't disable it
3004 entirely. */
3005 if (global_options_set.x_rs6000_alignment_flags
3006 && rs6000_alignment_flags == MASK_ALIGN_POWER
3007 && DEFAULT_ABI == ABI_DARWIN
3008 && TARGET_64BIT)
3009 warning (0, "-malign-power is not supported for 64-bit Darwin;"
3010 " it is incompatible with the installed C and C++ libraries");
3012 /* Numerous experiment shows that IRA based loop pressure
3013 calculation works better for RTL loop invariant motion on targets
3014 with enough (>= 32) registers. It is an expensive optimization.
3015 So it is on only for peak performance. */
3016 if (optimize >= 3 && global_init_p)
3017 flag_ira_loop_pressure = 1;
3019 /* Set the pointer size. */
3020 if (TARGET_64BIT)
3022 rs6000_pmode = (int)DImode;
3023 rs6000_pointer_size = 64;
3025 else
3027 rs6000_pmode = (int)SImode;
3028 rs6000_pointer_size = 32;
3031 /* Some OSs don't support saving the high part of 64-bit registers on context
3032 switch. Other OSs don't support saving Altivec registers. On those OSs,
3033 we don't touch the OPTION_MASK_POWERPC64 or OPTION_MASK_ALTIVEC settings;
3034 if the user wants either, the user must explicitly specify them and we
3035 won't interfere with the user's specification. */
3037 set_masks = POWERPC_MASKS;
3038 #ifdef OS_MISSING_POWERPC64
3039 if (OS_MISSING_POWERPC64)
3040 set_masks &= ~OPTION_MASK_POWERPC64;
3041 #endif
3042 #ifdef OS_MISSING_ALTIVEC
3043 if (OS_MISSING_ALTIVEC)
3044 set_masks &= ~(OPTION_MASK_ALTIVEC | OPTION_MASK_VSX);
3045 #endif
3047 /* Don't override by the processor default if given explicitly. */
3048 set_masks &= ~rs6000_isa_flags_explicit;
3050 /* Process the -mcpu=<xxx> and -mtune=<xxx> argument. If the user changed
3051 the cpu in a target attribute or pragma, but did not specify a tuning
3052 option, use the cpu for the tuning option rather than the option specified
3053 with -mtune on the command line. Process a '--with-cpu' configuration
3054 request as an implicit --cpu. */
3055 if (rs6000_cpu_index >= 0)
3057 cpu_index = rs6000_cpu_index;
3058 have_cpu = true;
3060 else if (main_target_opt != NULL && main_target_opt->x_rs6000_cpu_index >= 0)
3062 rs6000_cpu_index = cpu_index = main_target_opt->x_rs6000_cpu_index;
3063 have_cpu = true;
3065 else if (implicit_cpu)
3067 rs6000_cpu_index = cpu_index = rs6000_cpu_name_lookup (implicit_cpu);
3068 have_cpu = true;
3070 else
3072 const char *default_cpu = (TARGET_POWERPC64 ? "powerpc64" : "powerpc");
3073 rs6000_cpu_index = cpu_index = rs6000_cpu_name_lookup (default_cpu);
3074 have_cpu = false;
3077 gcc_assert (cpu_index >= 0);
3079 /* If we have a cpu, either through an explicit -mcpu=<xxx> or if the
3080 compiler was configured with --with-cpu=<xxx>, replace all of the ISA bits
3081 with those from the cpu, except for options that were explicitly set. If
3082 we don't have a cpu, do not override the target bits set in
3083 TARGET_DEFAULT. */
3084 if (have_cpu)
3086 rs6000_isa_flags &= ~set_masks;
3087 rs6000_isa_flags |= (processor_target_table[cpu_index].target_enable
3088 & set_masks);
3090 else
3091 rs6000_isa_flags |= (processor_target_table[cpu_index].target_enable
3092 & ~rs6000_isa_flags_explicit);
3094 /* If no -mcpu=<xxx>, inherit any default options that were cleared via
3095 POWERPC_MASKS. Originally, TARGET_DEFAULT was used to initialize
3096 target_flags via the TARGET_DEFAULT_TARGET_FLAGS hook. When we switched
3097 to using rs6000_isa_flags, we need to do the initialization here. */
3098 if (!have_cpu)
3099 rs6000_isa_flags |= (TARGET_DEFAULT & ~rs6000_isa_flags_explicit);
3101 if (rs6000_tune_index >= 0)
3102 tune_index = rs6000_tune_index;
3103 else if (have_cpu)
3104 rs6000_tune_index = tune_index = cpu_index;
3105 else
3107 size_t i;
3108 enum processor_type tune_proc
3109 = (TARGET_POWERPC64 ? PROCESSOR_DEFAULT64 : PROCESSOR_DEFAULT);
3111 tune_index = -1;
3112 for (i = 0; i < ARRAY_SIZE (processor_target_table); i++)
3113 if (processor_target_table[i].processor == tune_proc)
3115 rs6000_tune_index = tune_index = i;
3116 break;
3120 gcc_assert (tune_index >= 0);
3121 rs6000_cpu = processor_target_table[tune_index].processor;
3123 /* Pick defaults for SPE related control flags. Do this early to make sure
3124 that the TARGET_ macros are representative ASAP. */
3126 int spe_capable_cpu =
3127 (rs6000_cpu == PROCESSOR_PPC8540
3128 || rs6000_cpu == PROCESSOR_PPC8548);
3130 if (!global_options_set.x_rs6000_spe_abi)
3131 rs6000_spe_abi = spe_capable_cpu;
3133 if (!global_options_set.x_rs6000_spe)
3134 rs6000_spe = spe_capable_cpu;
3136 if (!global_options_set.x_rs6000_float_gprs)
3137 rs6000_float_gprs =
3138 (rs6000_cpu == PROCESSOR_PPC8540 ? 1
3139 : rs6000_cpu == PROCESSOR_PPC8548 ? 2
3140 : 0);
3143 if (global_options_set.x_rs6000_spe_abi
3144 && rs6000_spe_abi
3145 && !TARGET_SPE_ABI)
3146 error ("not configured for SPE ABI");
3148 if (global_options_set.x_rs6000_spe
3149 && rs6000_spe
3150 && !TARGET_SPE)
3151 error ("not configured for SPE instruction set");
3153 if (main_target_opt != NULL
3154 && ((main_target_opt->x_rs6000_spe_abi != rs6000_spe_abi)
3155 || (main_target_opt->x_rs6000_spe != rs6000_spe)
3156 || (main_target_opt->x_rs6000_float_gprs != rs6000_float_gprs)))
3157 error ("target attribute or pragma changes SPE ABI");
3159 if (rs6000_cpu == PROCESSOR_PPCE300C2 || rs6000_cpu == PROCESSOR_PPCE300C3
3160 || rs6000_cpu == PROCESSOR_PPCE500MC || rs6000_cpu == PROCESSOR_PPCE500MC64
3161 || rs6000_cpu == PROCESSOR_PPCE5500)
3163 if (TARGET_ALTIVEC)
3164 error ("AltiVec not supported in this target");
3165 if (TARGET_SPE)
3166 error ("SPE not supported in this target");
3168 if (rs6000_cpu == PROCESSOR_PPCE6500)
3170 if (TARGET_SPE)
3171 error ("SPE not supported in this target");
3174 /* Disable Cell microcode if we are optimizing for the Cell
3175 and not optimizing for size. */
3176 if (rs6000_gen_cell_microcode == -1)
3177 rs6000_gen_cell_microcode = !(rs6000_cpu == PROCESSOR_CELL
3178 && !optimize_size);
3180 /* If we are optimizing big endian systems for space and it's OK to
3181 use instructions that would be microcoded on the Cell, use the
3182 load/store multiple and string instructions. */
3183 if (BYTES_BIG_ENDIAN && optimize_size && rs6000_gen_cell_microcode)
3184 rs6000_isa_flags |= ~rs6000_isa_flags_explicit & (OPTION_MASK_MULTIPLE
3185 | OPTION_MASK_STRING);
3187 /* Don't allow -mmultiple or -mstring on little endian systems
3188 unless the cpu is a 750, because the hardware doesn't support the
3189 instructions used in little endian mode, and causes an alignment
3190 trap. The 750 does not cause an alignment trap (except when the
3191 target is unaligned). */
3193 if (!BYTES_BIG_ENDIAN && rs6000_cpu != PROCESSOR_PPC750)
3195 if (TARGET_MULTIPLE)
3197 rs6000_isa_flags &= ~OPTION_MASK_MULTIPLE;
3198 if ((rs6000_isa_flags_explicit & OPTION_MASK_MULTIPLE) != 0)
3199 warning (0, "-mmultiple is not supported on little endian systems");
3202 if (TARGET_STRING)
3204 rs6000_isa_flags &= ~OPTION_MASK_STRING;
3205 if ((rs6000_isa_flags_explicit & OPTION_MASK_STRING) != 0)
3206 warning (0, "-mstring is not supported on little endian systems");
3210 /* Add some warnings for VSX. */
3211 if (TARGET_VSX)
3213 const char *msg = NULL;
3214 if (!TARGET_HARD_FLOAT || !TARGET_FPRS
3215 || !TARGET_SINGLE_FLOAT || !TARGET_DOUBLE_FLOAT)
3217 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX)
3218 msg = N_("-mvsx requires hardware floating point");
3219 else
3221 rs6000_isa_flags &= ~ OPTION_MASK_VSX;
3222 rs6000_isa_flags_explicit |= OPTION_MASK_VSX;
3225 else if (TARGET_PAIRED_FLOAT)
3226 msg = N_("-mvsx and -mpaired are incompatible");
3227 else if (TARGET_AVOID_XFORM > 0)
3228 msg = N_("-mvsx needs indexed addressing");
3229 else if (!TARGET_ALTIVEC && (rs6000_isa_flags_explicit
3230 & OPTION_MASK_ALTIVEC))
3232 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX)
3233 msg = N_("-mvsx and -mno-altivec are incompatible");
3234 else
3235 msg = N_("-mno-altivec disables vsx");
3238 if (msg)
3240 warning (0, msg);
3241 rs6000_isa_flags &= ~ OPTION_MASK_VSX;
3242 rs6000_isa_flags_explicit |= OPTION_MASK_VSX;
3246 /* If hard-float/altivec/vsx were explicitly turned off then don't allow
3247 the -mcpu setting to enable options that conflict. */
3248 if ((!TARGET_HARD_FLOAT || !TARGET_ALTIVEC || !TARGET_VSX)
3249 && (rs6000_isa_flags_explicit & (OPTION_MASK_SOFT_FLOAT
3250 | OPTION_MASK_ALTIVEC
3251 | OPTION_MASK_VSX)) != 0)
3252 rs6000_isa_flags &= ~((OPTION_MASK_P8_VECTOR | OPTION_MASK_CRYPTO
3253 | OPTION_MASK_DIRECT_MOVE)
3254 & ~rs6000_isa_flags_explicit);
3256 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
3257 rs6000_print_isa_options (stderr, 0, "before defaults", rs6000_isa_flags);
3259 /* For the newer switches (vsx, dfp, etc.) set some of the older options,
3260 unless the user explicitly used the -mno-<option> to disable the code. */
3261 if (TARGET_P8_VECTOR || TARGET_DIRECT_MOVE || TARGET_CRYPTO)
3262 rs6000_isa_flags |= (ISA_2_7_MASKS_SERVER & ~rs6000_isa_flags_explicit);
3263 else if (TARGET_VSX)
3264 rs6000_isa_flags |= (ISA_2_6_MASKS_SERVER & ~rs6000_isa_flags_explicit);
3265 else if (TARGET_POPCNTD)
3266 rs6000_isa_flags |= (ISA_2_6_MASKS_EMBEDDED & ~rs6000_isa_flags_explicit);
3267 else if (TARGET_DFP)
3268 rs6000_isa_flags |= (ISA_2_5_MASKS_SERVER & ~rs6000_isa_flags_explicit);
3269 else if (TARGET_CMPB)
3270 rs6000_isa_flags |= (ISA_2_5_MASKS_EMBEDDED & ~rs6000_isa_flags_explicit);
3271 else if (TARGET_FPRND)
3272 rs6000_isa_flags |= (ISA_2_4_MASKS & ~rs6000_isa_flags_explicit);
3273 else if (TARGET_POPCNTB)
3274 rs6000_isa_flags |= (ISA_2_2_MASKS & ~rs6000_isa_flags_explicit);
3275 else if (TARGET_ALTIVEC)
3276 rs6000_isa_flags |= (OPTION_MASK_PPC_GFXOPT & ~rs6000_isa_flags_explicit);
3278 if (TARGET_CRYPTO && !TARGET_ALTIVEC)
3280 if (rs6000_isa_flags_explicit & OPTION_MASK_CRYPTO)
3281 error ("-mcrypto requires -maltivec");
3282 rs6000_isa_flags &= ~OPTION_MASK_CRYPTO;
3285 if (TARGET_DIRECT_MOVE && !TARGET_VSX)
3287 if (rs6000_isa_flags_explicit & OPTION_MASK_DIRECT_MOVE)
3288 error ("-mdirect-move requires -mvsx");
3289 rs6000_isa_flags &= ~OPTION_MASK_DIRECT_MOVE;
3292 if (TARGET_P8_VECTOR && !TARGET_ALTIVEC)
3294 if (rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR)
3295 error ("-mpower8-vector requires -maltivec");
3296 rs6000_isa_flags &= ~OPTION_MASK_P8_VECTOR;
3299 if (TARGET_P8_VECTOR && !TARGET_VSX)
3301 if (rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR)
3302 error ("-mpower8-vector requires -mvsx");
3303 rs6000_isa_flags &= ~OPTION_MASK_P8_VECTOR;
3306 if (TARGET_VSX_TIMODE && !TARGET_VSX)
3308 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX_TIMODE)
3309 error ("-mvsx-timode requires -mvsx");
3310 rs6000_isa_flags &= ~OPTION_MASK_VSX_TIMODE;
3313 /* The quad memory instructions only works in 64-bit mode. In 32-bit mode,
3314 silently turn off quad memory mode. */
3315 if (TARGET_QUAD_MEMORY && !TARGET_POWERPC64)
3317 if ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY) != 0)
3318 warning (0, N_("-mquad-memory requires 64-bit mode"));
3320 rs6000_isa_flags &= ~OPTION_MASK_QUAD_MEMORY;
3323 /* Enable power8 fusion if we are tuning for power8, even if we aren't
3324 generating power8 instructions. */
3325 if (!(rs6000_isa_flags_explicit & OPTION_MASK_P8_FUSION))
3326 rs6000_isa_flags |= (processor_target_table[tune_index].target_enable
3327 & OPTION_MASK_P8_FUSION);
3329 /* Power8 does not fuse sign extended loads with the addis. If we are
3330 optimizing at high levels for speed, convert a sign extended load into a
3331 zero extending load, and an explicit sign extension. */
3332 if (TARGET_P8_FUSION
3333 && !(rs6000_isa_flags_explicit & OPTION_MASK_P8_FUSION_SIGN)
3334 && optimize_function_for_speed_p (cfun)
3335 && optimize >= 3)
3336 rs6000_isa_flags |= OPTION_MASK_P8_FUSION_SIGN;
3338 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
3339 rs6000_print_isa_options (stderr, 0, "after defaults", rs6000_isa_flags);
3341 /* E500mc does "better" if we inline more aggressively. Respect the
3342 user's opinion, though. */
3343 if (rs6000_block_move_inline_limit == 0
3344 && (rs6000_cpu == PROCESSOR_PPCE500MC
3345 || rs6000_cpu == PROCESSOR_PPCE500MC64
3346 || rs6000_cpu == PROCESSOR_PPCE5500
3347 || rs6000_cpu == PROCESSOR_PPCE6500))
3348 rs6000_block_move_inline_limit = 128;
3350 /* store_one_arg depends on expand_block_move to handle at least the
3351 size of reg_parm_stack_space. */
3352 if (rs6000_block_move_inline_limit < (TARGET_POWERPC64 ? 64 : 32))
3353 rs6000_block_move_inline_limit = (TARGET_POWERPC64 ? 64 : 32);
3355 if (global_init_p)
3357 /* If the appropriate debug option is enabled, replace the target hooks
3358 with debug versions that call the real version and then prints
3359 debugging information. */
3360 if (TARGET_DEBUG_COST)
3362 targetm.rtx_costs = rs6000_debug_rtx_costs;
3363 targetm.address_cost = rs6000_debug_address_cost;
3364 targetm.sched.adjust_cost = rs6000_debug_adjust_cost;
3367 if (TARGET_DEBUG_ADDR)
3369 targetm.legitimate_address_p = rs6000_debug_legitimate_address_p;
3370 targetm.legitimize_address = rs6000_debug_legitimize_address;
3371 rs6000_secondary_reload_class_ptr
3372 = rs6000_debug_secondary_reload_class;
3373 rs6000_secondary_memory_needed_ptr
3374 = rs6000_debug_secondary_memory_needed;
3375 rs6000_cannot_change_mode_class_ptr
3376 = rs6000_debug_cannot_change_mode_class;
3377 rs6000_preferred_reload_class_ptr
3378 = rs6000_debug_preferred_reload_class;
3379 rs6000_legitimize_reload_address_ptr
3380 = rs6000_debug_legitimize_reload_address;
3381 rs6000_mode_dependent_address_ptr
3382 = rs6000_debug_mode_dependent_address;
3385 if (rs6000_veclibabi_name)
3387 if (strcmp (rs6000_veclibabi_name, "mass") == 0)
3388 rs6000_veclib_handler = rs6000_builtin_vectorized_libmass;
3389 else
3391 error ("unknown vectorization library ABI type (%s) for "
3392 "-mveclibabi= switch", rs6000_veclibabi_name);
3393 ret = false;
3398 if (!global_options_set.x_rs6000_long_double_type_size)
3400 if (main_target_opt != NULL
3401 && (main_target_opt->x_rs6000_long_double_type_size
3402 != RS6000_DEFAULT_LONG_DOUBLE_SIZE))
3403 error ("target attribute or pragma changes long double size");
3404 else
3405 rs6000_long_double_type_size = RS6000_DEFAULT_LONG_DOUBLE_SIZE;
3408 #if !defined (POWERPC_LINUX) && !defined (POWERPC_FREEBSD)
3409 if (!global_options_set.x_rs6000_ieeequad)
3410 rs6000_ieeequad = 1;
3411 #endif
3413 /* Disable VSX and Altivec silently if the user switched cpus to power7 in a
3414 target attribute or pragma which automatically enables both options,
3415 unless the altivec ABI was set. This is set by default for 64-bit, but
3416 not for 32-bit. */
3417 if (main_target_opt != NULL && !main_target_opt->x_rs6000_altivec_abi)
3418 rs6000_isa_flags &= ~((OPTION_MASK_VSX | OPTION_MASK_ALTIVEC)
3419 & ~rs6000_isa_flags_explicit);
3421 /* Enable Altivec ABI for AIX -maltivec. */
3422 if (TARGET_XCOFF && (TARGET_ALTIVEC || TARGET_VSX))
3424 if (main_target_opt != NULL && !main_target_opt->x_rs6000_altivec_abi)
3425 error ("target attribute or pragma changes AltiVec ABI");
3426 else
3427 rs6000_altivec_abi = 1;
3430 /* The AltiVec ABI is the default for PowerPC-64 GNU/Linux. For
3431 PowerPC-32 GNU/Linux, -maltivec implies the AltiVec ABI. It can
3432 be explicitly overridden in either case. */
3433 if (TARGET_ELF)
3435 if (!global_options_set.x_rs6000_altivec_abi
3436 && (TARGET_64BIT || TARGET_ALTIVEC || TARGET_VSX))
3438 if (main_target_opt != NULL &&
3439 !main_target_opt->x_rs6000_altivec_abi)
3440 error ("target attribute or pragma changes AltiVec ABI");
3441 else
3442 rs6000_altivec_abi = 1;
3446 /* Set the Darwin64 ABI as default for 64-bit Darwin.
3447 So far, the only darwin64 targets are also MACH-O. */
3448 if (TARGET_MACHO
3449 && DEFAULT_ABI == ABI_DARWIN
3450 && TARGET_64BIT)
3452 if (main_target_opt != NULL && !main_target_opt->x_rs6000_darwin64_abi)
3453 error ("target attribute or pragma changes darwin64 ABI");
3454 else
3456 rs6000_darwin64_abi = 1;
3457 /* Default to natural alignment, for better performance. */
3458 rs6000_alignment_flags = MASK_ALIGN_NATURAL;
3462 /* Place FP constants in the constant pool instead of TOC
3463 if section anchors enabled. */
3464 if (flag_section_anchors
3465 && !global_options_set.x_TARGET_NO_FP_IN_TOC)
3466 TARGET_NO_FP_IN_TOC = 1;
3468 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
3469 rs6000_print_isa_options (stderr, 0, "before subtarget", rs6000_isa_flags);
3471 #ifdef SUBTARGET_OVERRIDE_OPTIONS
3472 SUBTARGET_OVERRIDE_OPTIONS;
3473 #endif
3474 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
3475 SUBSUBTARGET_OVERRIDE_OPTIONS;
3476 #endif
3477 #ifdef SUB3TARGET_OVERRIDE_OPTIONS
3478 SUB3TARGET_OVERRIDE_OPTIONS;
3479 #endif
3481 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
3482 rs6000_print_isa_options (stderr, 0, "after subtarget", rs6000_isa_flags);
3484 /* For the E500 family of cores, reset the single/double FP flags to let us
3485 check that they remain constant across attributes or pragmas. Also,
3486 clear a possible request for string instructions, not supported and which
3487 we might have silently queried above for -Os.
3489 For other families, clear ISEL in case it was set implicitly.
3492 switch (rs6000_cpu)
3494 case PROCESSOR_PPC8540:
3495 case PROCESSOR_PPC8548:
3496 case PROCESSOR_PPCE500MC:
3497 case PROCESSOR_PPCE500MC64:
3498 case PROCESSOR_PPCE5500:
3499 case PROCESSOR_PPCE6500:
3501 rs6000_single_float = TARGET_E500_SINGLE || TARGET_E500_DOUBLE;
3502 rs6000_double_float = TARGET_E500_DOUBLE;
3504 rs6000_isa_flags &= ~OPTION_MASK_STRING;
3506 break;
3508 default:
3510 if (have_cpu && !(rs6000_isa_flags_explicit & OPTION_MASK_ISEL))
3511 rs6000_isa_flags &= ~OPTION_MASK_ISEL;
3513 break;
3516 if (main_target_opt)
3518 if (main_target_opt->x_rs6000_single_float != rs6000_single_float)
3519 error ("target attribute or pragma changes single precision floating "
3520 "point");
3521 if (main_target_opt->x_rs6000_double_float != rs6000_double_float)
3522 error ("target attribute or pragma changes double precision floating "
3523 "point");
3526 /* Detect invalid option combinations with E500. */
3527 CHECK_E500_OPTIONS;
3529 rs6000_always_hint = (rs6000_cpu != PROCESSOR_POWER4
3530 && rs6000_cpu != PROCESSOR_POWER5
3531 && rs6000_cpu != PROCESSOR_POWER6
3532 && rs6000_cpu != PROCESSOR_POWER7
3533 && rs6000_cpu != PROCESSOR_POWER8
3534 && rs6000_cpu != PROCESSOR_PPCA2
3535 && rs6000_cpu != PROCESSOR_CELL
3536 && rs6000_cpu != PROCESSOR_PPC476);
3537 rs6000_sched_groups = (rs6000_cpu == PROCESSOR_POWER4
3538 || rs6000_cpu == PROCESSOR_POWER5
3539 || rs6000_cpu == PROCESSOR_POWER7
3540 || rs6000_cpu == PROCESSOR_POWER8);
3541 rs6000_align_branch_targets = (rs6000_cpu == PROCESSOR_POWER4
3542 || rs6000_cpu == PROCESSOR_POWER5
3543 || rs6000_cpu == PROCESSOR_POWER6
3544 || rs6000_cpu == PROCESSOR_POWER7
3545 || rs6000_cpu == PROCESSOR_POWER8
3546 || rs6000_cpu == PROCESSOR_PPCE500MC
3547 || rs6000_cpu == PROCESSOR_PPCE500MC64
3548 || rs6000_cpu == PROCESSOR_PPCE5500
3549 || rs6000_cpu == PROCESSOR_PPCE6500);
3551 /* Allow debug switches to override the above settings. These are set to -1
3552 in rs6000.opt to indicate the user hasn't directly set the switch. */
3553 if (TARGET_ALWAYS_HINT >= 0)
3554 rs6000_always_hint = TARGET_ALWAYS_HINT;
3556 if (TARGET_SCHED_GROUPS >= 0)
3557 rs6000_sched_groups = TARGET_SCHED_GROUPS;
3559 if (TARGET_ALIGN_BRANCH_TARGETS >= 0)
3560 rs6000_align_branch_targets = TARGET_ALIGN_BRANCH_TARGETS;
3562 rs6000_sched_restricted_insns_priority
3563 = (rs6000_sched_groups ? 1 : 0);
3565 /* Handle -msched-costly-dep option. */
3566 rs6000_sched_costly_dep
3567 = (rs6000_sched_groups ? true_store_to_load_dep_costly : no_dep_costly);
3569 if (rs6000_sched_costly_dep_str)
3571 if (! strcmp (rs6000_sched_costly_dep_str, "no"))
3572 rs6000_sched_costly_dep = no_dep_costly;
3573 else if (! strcmp (rs6000_sched_costly_dep_str, "all"))
3574 rs6000_sched_costly_dep = all_deps_costly;
3575 else if (! strcmp (rs6000_sched_costly_dep_str, "true_store_to_load"))
3576 rs6000_sched_costly_dep = true_store_to_load_dep_costly;
3577 else if (! strcmp (rs6000_sched_costly_dep_str, "store_to_load"))
3578 rs6000_sched_costly_dep = store_to_load_dep_costly;
3579 else
3580 rs6000_sched_costly_dep = ((enum rs6000_dependence_cost)
3581 atoi (rs6000_sched_costly_dep_str));
3584 /* Handle -minsert-sched-nops option. */
3585 rs6000_sched_insert_nops
3586 = (rs6000_sched_groups ? sched_finish_regroup_exact : sched_finish_none);
3588 if (rs6000_sched_insert_nops_str)
3590 if (! strcmp (rs6000_sched_insert_nops_str, "no"))
3591 rs6000_sched_insert_nops = sched_finish_none;
3592 else if (! strcmp (rs6000_sched_insert_nops_str, "pad"))
3593 rs6000_sched_insert_nops = sched_finish_pad_groups;
3594 else if (! strcmp (rs6000_sched_insert_nops_str, "regroup_exact"))
3595 rs6000_sched_insert_nops = sched_finish_regroup_exact;
3596 else
3597 rs6000_sched_insert_nops = ((enum rs6000_nop_insertion)
3598 atoi (rs6000_sched_insert_nops_str));
3601 if (global_init_p)
3603 #ifdef TARGET_REGNAMES
3604 /* If the user desires alternate register names, copy in the
3605 alternate names now. */
3606 if (TARGET_REGNAMES)
3607 memcpy (rs6000_reg_names, alt_reg_names, sizeof (rs6000_reg_names));
3608 #endif
3610 /* Set aix_struct_return last, after the ABI is determined.
3611 If -maix-struct-return or -msvr4-struct-return was explicitly
3612 used, don't override with the ABI default. */
3613 if (!global_options_set.x_aix_struct_return)
3614 aix_struct_return = (DEFAULT_ABI != ABI_V4 || DRAFT_V4_STRUCT_RET);
3616 #if 0
3617 /* IBM XL compiler defaults to unsigned bitfields. */
3618 if (TARGET_XL_COMPAT)
3619 flag_signed_bitfields = 0;
3620 #endif
3622 if (TARGET_LONG_DOUBLE_128 && !TARGET_IEEEQUAD)
3623 REAL_MODE_FORMAT (TFmode) = &ibm_extended_format;
3625 if (TARGET_TOC)
3626 ASM_GENERATE_INTERNAL_LABEL (toc_label_name, "LCTOC", 1);
3628 /* We can only guarantee the availability of DI pseudo-ops when
3629 assembling for 64-bit targets. */
3630 if (!TARGET_64BIT)
3632 targetm.asm_out.aligned_op.di = NULL;
3633 targetm.asm_out.unaligned_op.di = NULL;
3637 /* Set branch target alignment, if not optimizing for size. */
3638 if (!optimize_size)
3640 /* Cell wants to be aligned 8byte for dual issue. Titan wants to be
3641 aligned 8byte to avoid misprediction by the branch predictor. */
3642 if (rs6000_cpu == PROCESSOR_TITAN
3643 || rs6000_cpu == PROCESSOR_CELL)
3645 if (align_functions <= 0)
3646 align_functions = 8;
3647 if (align_jumps <= 0)
3648 align_jumps = 8;
3649 if (align_loops <= 0)
3650 align_loops = 8;
3652 if (rs6000_align_branch_targets)
3654 if (align_functions <= 0)
3655 align_functions = 16;
3656 if (align_jumps <= 0)
3657 align_jumps = 16;
3658 if (align_loops <= 0)
3660 can_override_loop_align = 1;
3661 align_loops = 16;
3664 if (align_jumps_max_skip <= 0)
3665 align_jumps_max_skip = 15;
3666 if (align_loops_max_skip <= 0)
3667 align_loops_max_skip = 15;
3670 /* Arrange to save and restore machine status around nested functions. */
3671 init_machine_status = rs6000_init_machine_status;
3673 /* We should always be splitting complex arguments, but we can't break
3674 Linux and Darwin ABIs at the moment. For now, only AIX is fixed. */
3675 if (DEFAULT_ABI != ABI_AIX)
3676 targetm.calls.split_complex_arg = NULL;
3679 /* Initialize rs6000_cost with the appropriate target costs. */
3680 if (optimize_size)
3681 rs6000_cost = TARGET_POWERPC64 ? &size64_cost : &size32_cost;
3682 else
3683 switch (rs6000_cpu)
3685 case PROCESSOR_RS64A:
3686 rs6000_cost = &rs64a_cost;
3687 break;
3689 case PROCESSOR_MPCCORE:
3690 rs6000_cost = &mpccore_cost;
3691 break;
3693 case PROCESSOR_PPC403:
3694 rs6000_cost = &ppc403_cost;
3695 break;
3697 case PROCESSOR_PPC405:
3698 rs6000_cost = &ppc405_cost;
3699 break;
3701 case PROCESSOR_PPC440:
3702 rs6000_cost = &ppc440_cost;
3703 break;
3705 case PROCESSOR_PPC476:
3706 rs6000_cost = &ppc476_cost;
3707 break;
3709 case PROCESSOR_PPC601:
3710 rs6000_cost = &ppc601_cost;
3711 break;
3713 case PROCESSOR_PPC603:
3714 rs6000_cost = &ppc603_cost;
3715 break;
3717 case PROCESSOR_PPC604:
3718 rs6000_cost = &ppc604_cost;
3719 break;
3721 case PROCESSOR_PPC604e:
3722 rs6000_cost = &ppc604e_cost;
3723 break;
3725 case PROCESSOR_PPC620:
3726 rs6000_cost = &ppc620_cost;
3727 break;
3729 case PROCESSOR_PPC630:
3730 rs6000_cost = &ppc630_cost;
3731 break;
3733 case PROCESSOR_CELL:
3734 rs6000_cost = &ppccell_cost;
3735 break;
3737 case PROCESSOR_PPC750:
3738 case PROCESSOR_PPC7400:
3739 rs6000_cost = &ppc750_cost;
3740 break;
3742 case PROCESSOR_PPC7450:
3743 rs6000_cost = &ppc7450_cost;
3744 break;
3746 case PROCESSOR_PPC8540:
3747 case PROCESSOR_PPC8548:
3748 rs6000_cost = &ppc8540_cost;
3749 break;
3751 case PROCESSOR_PPCE300C2:
3752 case PROCESSOR_PPCE300C3:
3753 rs6000_cost = &ppce300c2c3_cost;
3754 break;
3756 case PROCESSOR_PPCE500MC:
3757 rs6000_cost = &ppce500mc_cost;
3758 break;
3760 case PROCESSOR_PPCE500MC64:
3761 rs6000_cost = &ppce500mc64_cost;
3762 break;
3764 case PROCESSOR_PPCE5500:
3765 rs6000_cost = &ppce5500_cost;
3766 break;
3768 case PROCESSOR_PPCE6500:
3769 rs6000_cost = &ppce6500_cost;
3770 break;
3772 case PROCESSOR_TITAN:
3773 rs6000_cost = &titan_cost;
3774 break;
3776 case PROCESSOR_POWER4:
3777 case PROCESSOR_POWER5:
3778 rs6000_cost = &power4_cost;
3779 break;
3781 case PROCESSOR_POWER6:
3782 rs6000_cost = &power6_cost;
3783 break;
3785 case PROCESSOR_POWER7:
3786 rs6000_cost = &power7_cost;
3787 break;
3789 case PROCESSOR_POWER8:
3790 rs6000_cost = &power8_cost;
3791 break;
3793 case PROCESSOR_PPCA2:
3794 rs6000_cost = &ppca2_cost;
3795 break;
3797 default:
3798 gcc_unreachable ();
3801 if (global_init_p)
3803 maybe_set_param_value (PARAM_SIMULTANEOUS_PREFETCHES,
3804 rs6000_cost->simultaneous_prefetches,
3805 global_options.x_param_values,
3806 global_options_set.x_param_values);
3807 maybe_set_param_value (PARAM_L1_CACHE_SIZE, rs6000_cost->l1_cache_size,
3808 global_options.x_param_values,
3809 global_options_set.x_param_values);
3810 maybe_set_param_value (PARAM_L1_CACHE_LINE_SIZE,
3811 rs6000_cost->cache_line_size,
3812 global_options.x_param_values,
3813 global_options_set.x_param_values);
3814 maybe_set_param_value (PARAM_L2_CACHE_SIZE, rs6000_cost->l2_cache_size,
3815 global_options.x_param_values,
3816 global_options_set.x_param_values);
3818 /* Increase loop peeling limits based on performance analysis. */
3819 maybe_set_param_value (PARAM_MAX_PEELED_INSNS, 400,
3820 global_options.x_param_values,
3821 global_options_set.x_param_values);
3822 maybe_set_param_value (PARAM_MAX_COMPLETELY_PEELED_INSNS, 400,
3823 global_options.x_param_values,
3824 global_options_set.x_param_values);
3826 /* If using typedef char *va_list, signal that
3827 __builtin_va_start (&ap, 0) can be optimized to
3828 ap = __builtin_next_arg (0). */
3829 if (DEFAULT_ABI != ABI_V4)
3830 targetm.expand_builtin_va_start = NULL;
3833 /* Set up single/double float flags.
3834 If TARGET_HARD_FLOAT is set, but neither single or double is set,
3835 then set both flags. */
3836 if (TARGET_HARD_FLOAT && TARGET_FPRS
3837 && rs6000_single_float == 0 && rs6000_double_float == 0)
3838 rs6000_single_float = rs6000_double_float = 1;
3840 /* If not explicitly specified via option, decide whether to generate indexed
3841 load/store instructions. */
3842 if (TARGET_AVOID_XFORM == -1)
3843 /* Avoid indexed addressing when targeting Power6 in order to avoid the
3844 DERAT mispredict penalty. However the LVE and STVE altivec instructions
3845 need indexed accesses and the type used is the scalar type of the element
3846 being loaded or stored. */
3847 TARGET_AVOID_XFORM = (rs6000_cpu == PROCESSOR_POWER6 && TARGET_CMPB
3848 && !TARGET_ALTIVEC);
3850 /* Set the -mrecip options. */
3851 if (rs6000_recip_name)
3853 char *p = ASTRDUP (rs6000_recip_name);
3854 char *q;
3855 unsigned int mask, i;
3856 bool invert;
3858 while ((q = strtok (p, ",")) != NULL)
3860 p = NULL;
3861 if (*q == '!')
3863 invert = true;
3864 q++;
3866 else
3867 invert = false;
3869 if (!strcmp (q, "default"))
3870 mask = ((TARGET_RECIP_PRECISION)
3871 ? RECIP_HIGH_PRECISION : RECIP_LOW_PRECISION);
3872 else
3874 for (i = 0; i < ARRAY_SIZE (recip_options); i++)
3875 if (!strcmp (q, recip_options[i].string))
3877 mask = recip_options[i].mask;
3878 break;
3881 if (i == ARRAY_SIZE (recip_options))
3883 error ("unknown option for -mrecip=%s", q);
3884 invert = false;
3885 mask = 0;
3886 ret = false;
3890 if (invert)
3891 rs6000_recip_control &= ~mask;
3892 else
3893 rs6000_recip_control |= mask;
3897 /* Set the builtin mask of the various options used that could affect which
3898 builtins were used. In the past we used target_flags, but we've run out
3899 of bits, and some options like SPE and PAIRED are no longer in
3900 target_flags. */
3901 rs6000_builtin_mask = rs6000_builtin_mask_calculate ();
3902 if (TARGET_DEBUG_BUILTIN || TARGET_DEBUG_TARGET)
3904 fprintf (stderr,
3905 "new builtin mask = " HOST_WIDE_INT_PRINT_HEX ", ",
3906 rs6000_builtin_mask);
3907 rs6000_print_builtin_options (stderr, 0, NULL, rs6000_builtin_mask);
3910 /* Initialize all of the registers. */
3911 rs6000_init_hard_regno_mode_ok (global_init_p);
3913 /* Save the initial options in case the user does function specific options */
3914 if (global_init_p)
3915 target_option_default_node = target_option_current_node
3916 = build_target_option_node (&global_options);
3918 /* If not explicitly specified via option, decide whether to generate the
3919 extra blr's required to preserve the link stack on some cpus (eg, 476). */
3920 if (TARGET_LINK_STACK == -1)
3921 SET_TARGET_LINK_STACK (rs6000_cpu == PROCESSOR_PPC476 && flag_pic);
3923 return ret;
3926 /* Implement TARGET_OPTION_OVERRIDE. On the RS/6000 this is used to
3927 define the target cpu type. */
3929 static void
3930 rs6000_option_override (void)
3932 (void) rs6000_option_override_internal (true);
3936 /* Implement targetm.vectorize.builtin_mask_for_load. */
3937 static tree
3938 rs6000_builtin_mask_for_load (void)
3940 if (TARGET_ALTIVEC || TARGET_VSX)
3941 return altivec_builtin_mask_for_load;
3942 else
3943 return 0;
3946 /* Implement LOOP_ALIGN. */
3948 rs6000_loop_align (rtx label)
3950 basic_block bb;
3951 int ninsns;
3953 /* Don't override loop alignment if -falign-loops was specified. */
3954 if (!can_override_loop_align)
3955 return align_loops_log;
3957 bb = BLOCK_FOR_INSN (label);
3958 ninsns = num_loop_insns(bb->loop_father);
3960 /* Align small loops to 32 bytes to fit in an icache sector, otherwise return default. */
3961 if (ninsns > 4 && ninsns <= 8
3962 && (rs6000_cpu == PROCESSOR_POWER4
3963 || rs6000_cpu == PROCESSOR_POWER5
3964 || rs6000_cpu == PROCESSOR_POWER6
3965 || rs6000_cpu == PROCESSOR_POWER7
3966 || rs6000_cpu == PROCESSOR_POWER8))
3967 return 5;
3968 else
3969 return align_loops_log;
3972 /* Implement TARGET_LOOP_ALIGN_MAX_SKIP. */
3973 static int
3974 rs6000_loop_align_max_skip (rtx label)
3976 return (1 << rs6000_loop_align (label)) - 1;
3979 /* Return true iff, data reference of TYPE can reach vector alignment (16)
3980 after applying N number of iterations. This routine does not determine
3981 how may iterations are required to reach desired alignment. */
3983 static bool
3984 rs6000_vector_alignment_reachable (const_tree type ATTRIBUTE_UNUSED, bool is_packed)
3986 if (is_packed)
3987 return false;
3989 if (TARGET_32BIT)
3991 if (rs6000_alignment_flags == MASK_ALIGN_NATURAL)
3992 return true;
3994 if (rs6000_alignment_flags == MASK_ALIGN_POWER)
3995 return true;
3997 return false;
3999 else
4001 if (TARGET_MACHO)
4002 return false;
4004 /* Assuming that all other types are naturally aligned. CHECKME! */
4005 return true;
4009 /* Return true if the vector misalignment factor is supported by the
4010 target. */
4011 static bool
4012 rs6000_builtin_support_vector_misalignment (enum machine_mode mode,
4013 const_tree type,
4014 int misalignment,
4015 bool is_packed)
4017 if (TARGET_VSX)
4019 /* Return if movmisalign pattern is not supported for this mode. */
4020 if (optab_handler (movmisalign_optab, mode) == CODE_FOR_nothing)
4021 return false;
4023 if (misalignment == -1)
4025 /* Misalignment factor is unknown at compile time but we know
4026 it's word aligned. */
4027 if (rs6000_vector_alignment_reachable (type, is_packed))
4029 int element_size = TREE_INT_CST_LOW (TYPE_SIZE (type));
4031 if (element_size == 64 || element_size == 32)
4032 return true;
4035 return false;
4038 /* VSX supports word-aligned vector. */
4039 if (misalignment % 4 == 0)
4040 return true;
4042 return false;
4045 /* Implement targetm.vectorize.builtin_vectorization_cost. */
4046 static int
4047 rs6000_builtin_vectorization_cost (enum vect_cost_for_stmt type_of_cost,
4048 tree vectype, int misalign)
4050 unsigned elements;
4051 tree elem_type;
4053 switch (type_of_cost)
4055 case scalar_stmt:
4056 case scalar_load:
4057 case scalar_store:
4058 case vector_stmt:
4059 case vector_load:
4060 case vector_store:
4061 case vec_to_scalar:
4062 case scalar_to_vec:
4063 case cond_branch_not_taken:
4064 return 1;
4066 case vec_perm:
4067 if (TARGET_VSX)
4068 return 3;
4069 else
4070 return 1;
4072 case vec_promote_demote:
4073 if (TARGET_VSX)
4074 return 4;
4075 else
4076 return 1;
4078 case cond_branch_taken:
4079 return 3;
4081 case unaligned_load:
4082 if (TARGET_VSX && TARGET_ALLOW_MOVMISALIGN)
4084 elements = TYPE_VECTOR_SUBPARTS (vectype);
4085 if (elements == 2)
4086 /* Double word aligned. */
4087 return 2;
4089 if (elements == 4)
4091 switch (misalign)
4093 case 8:
4094 /* Double word aligned. */
4095 return 2;
4097 case -1:
4098 /* Unknown misalignment. */
4099 case 4:
4100 case 12:
4101 /* Word aligned. */
4102 return 22;
4104 default:
4105 gcc_unreachable ();
4110 if (TARGET_ALTIVEC)
4111 /* Misaligned loads are not supported. */
4112 gcc_unreachable ();
4114 return 2;
4116 case unaligned_store:
4117 if (TARGET_VSX && TARGET_ALLOW_MOVMISALIGN)
4119 elements = TYPE_VECTOR_SUBPARTS (vectype);
4120 if (elements == 2)
4121 /* Double word aligned. */
4122 return 2;
4124 if (elements == 4)
4126 switch (misalign)
4128 case 8:
4129 /* Double word aligned. */
4130 return 2;
4132 case -1:
4133 /* Unknown misalignment. */
4134 case 4:
4135 case 12:
4136 /* Word aligned. */
4137 return 23;
4139 default:
4140 gcc_unreachable ();
4145 if (TARGET_ALTIVEC)
4146 /* Misaligned stores are not supported. */
4147 gcc_unreachable ();
4149 return 2;
4151 case vec_construct:
4152 elements = TYPE_VECTOR_SUBPARTS (vectype);
4153 elem_type = TREE_TYPE (vectype);
4154 /* 32-bit vectors loaded into registers are stored as double
4155 precision, so we need n/2 converts in addition to the usual
4156 n/2 merges to construct a vector of short floats from them. */
4157 if (SCALAR_FLOAT_TYPE_P (elem_type)
4158 && TYPE_PRECISION (elem_type) == 32)
4159 return elements + 1;
4160 else
4161 return elements / 2 + 1;
4163 default:
4164 gcc_unreachable ();
4168 /* Implement targetm.vectorize.preferred_simd_mode. */
4170 static enum machine_mode
4171 rs6000_preferred_simd_mode (enum machine_mode mode)
4173 if (TARGET_VSX)
4174 switch (mode)
4176 case DFmode:
4177 return V2DFmode;
4178 default:;
4180 if (TARGET_ALTIVEC || TARGET_VSX)
4181 switch (mode)
4183 case SFmode:
4184 return V4SFmode;
4185 case DImode:
4186 return V2DImode;
4187 case SImode:
4188 return V4SImode;
4189 case HImode:
4190 return V8HImode;
4191 case QImode:
4192 return V16QImode;
4193 default:;
4195 if (TARGET_SPE)
4196 switch (mode)
4198 case SFmode:
4199 return V2SFmode;
4200 case SImode:
4201 return V2SImode;
4202 default:;
4204 if (TARGET_PAIRED_FLOAT
4205 && mode == SFmode)
4206 return V2SFmode;
4207 return word_mode;
4210 typedef struct _rs6000_cost_data
4212 struct loop *loop_info;
4213 unsigned cost[3];
4214 } rs6000_cost_data;
4216 /* Test for likely overcommitment of vector hardware resources. If a
4217 loop iteration is relatively large, and too large a percentage of
4218 instructions in the loop are vectorized, the cost model may not
4219 adequately reflect delays from unavailable vector resources.
4220 Penalize the loop body cost for this case. */
4222 static void
4223 rs6000_density_test (rs6000_cost_data *data)
4225 const int DENSITY_PCT_THRESHOLD = 85;
4226 const int DENSITY_SIZE_THRESHOLD = 70;
4227 const int DENSITY_PENALTY = 10;
4228 struct loop *loop = data->loop_info;
4229 basic_block *bbs = get_loop_body (loop);
4230 int nbbs = loop->num_nodes;
4231 int vec_cost = data->cost[vect_body], not_vec_cost = 0;
4232 int i, density_pct;
4234 for (i = 0; i < nbbs; i++)
4236 basic_block bb = bbs[i];
4237 gimple_stmt_iterator gsi;
4239 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4241 gimple stmt = gsi_stmt (gsi);
4242 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4244 if (!STMT_VINFO_RELEVANT_P (stmt_info)
4245 && !STMT_VINFO_IN_PATTERN_P (stmt_info))
4246 not_vec_cost++;
4250 free (bbs);
4251 density_pct = (vec_cost * 100) / (vec_cost + not_vec_cost);
4253 if (density_pct > DENSITY_PCT_THRESHOLD
4254 && vec_cost + not_vec_cost > DENSITY_SIZE_THRESHOLD)
4256 data->cost[vect_body] = vec_cost * (100 + DENSITY_PENALTY) / 100;
4257 if (dump_enabled_p ())
4258 dump_printf_loc (MSG_NOTE, vect_location,
4259 "density %d%%, cost %d exceeds threshold, penalizing "
4260 "loop body cost by %d%%", density_pct,
4261 vec_cost + not_vec_cost, DENSITY_PENALTY);
4265 /* Implement targetm.vectorize.init_cost. */
4267 static void *
4268 rs6000_init_cost (struct loop *loop_info)
4270 rs6000_cost_data *data = XNEW (struct _rs6000_cost_data);
4271 data->loop_info = loop_info;
4272 data->cost[vect_prologue] = 0;
4273 data->cost[vect_body] = 0;
4274 data->cost[vect_epilogue] = 0;
4275 return data;
4278 /* Implement targetm.vectorize.add_stmt_cost. */
4280 static unsigned
4281 rs6000_add_stmt_cost (void *data, int count, enum vect_cost_for_stmt kind,
4282 struct _stmt_vec_info *stmt_info, int misalign,
4283 enum vect_cost_model_location where)
4285 rs6000_cost_data *cost_data = (rs6000_cost_data*) data;
4286 unsigned retval = 0;
4288 if (flag_vect_cost_model)
4290 tree vectype = stmt_info ? stmt_vectype (stmt_info) : NULL_TREE;
4291 int stmt_cost = rs6000_builtin_vectorization_cost (kind, vectype,
4292 misalign);
4293 /* Statements in an inner loop relative to the loop being
4294 vectorized are weighted more heavily. The value here is
4295 arbitrary and could potentially be improved with analysis. */
4296 if (where == vect_body && stmt_info && stmt_in_inner_loop_p (stmt_info))
4297 count *= 50; /* FIXME. */
4299 retval = (unsigned) (count * stmt_cost);
4300 cost_data->cost[where] += retval;
4303 return retval;
4306 /* Implement targetm.vectorize.finish_cost. */
4308 static void
4309 rs6000_finish_cost (void *data, unsigned *prologue_cost,
4310 unsigned *body_cost, unsigned *epilogue_cost)
4312 rs6000_cost_data *cost_data = (rs6000_cost_data*) data;
4314 if (cost_data->loop_info)
4315 rs6000_density_test (cost_data);
4317 *prologue_cost = cost_data->cost[vect_prologue];
4318 *body_cost = cost_data->cost[vect_body];
4319 *epilogue_cost = cost_data->cost[vect_epilogue];
4322 /* Implement targetm.vectorize.destroy_cost_data. */
4324 static void
4325 rs6000_destroy_cost_data (void *data)
4327 free (data);
4330 /* Handler for the Mathematical Acceleration Subsystem (mass) interface to a
4331 library with vectorized intrinsics. */
4333 static tree
4334 rs6000_builtin_vectorized_libmass (tree fndecl, tree type_out, tree type_in)
4336 char name[32];
4337 const char *suffix = NULL;
4338 tree fntype, new_fndecl, bdecl = NULL_TREE;
4339 int n_args = 1;
4340 const char *bname;
4341 enum machine_mode el_mode, in_mode;
4342 int n, in_n;
4344 /* Libmass is suitable for unsafe math only as it does not correctly support
4345 parts of IEEE with the required precision such as denormals. Only support
4346 it if we have VSX to use the simd d2 or f4 functions.
4347 XXX: Add variable length support. */
4348 if (!flag_unsafe_math_optimizations || !TARGET_VSX)
4349 return NULL_TREE;
4351 el_mode = TYPE_MODE (TREE_TYPE (type_out));
4352 n = TYPE_VECTOR_SUBPARTS (type_out);
4353 in_mode = TYPE_MODE (TREE_TYPE (type_in));
4354 in_n = TYPE_VECTOR_SUBPARTS (type_in);
4355 if (el_mode != in_mode
4356 || n != in_n)
4357 return NULL_TREE;
4359 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
4361 enum built_in_function fn = DECL_FUNCTION_CODE (fndecl);
4362 switch (fn)
4364 case BUILT_IN_ATAN2:
4365 case BUILT_IN_HYPOT:
4366 case BUILT_IN_POW:
4367 n_args = 2;
4368 /* fall through */
4370 case BUILT_IN_ACOS:
4371 case BUILT_IN_ACOSH:
4372 case BUILT_IN_ASIN:
4373 case BUILT_IN_ASINH:
4374 case BUILT_IN_ATAN:
4375 case BUILT_IN_ATANH:
4376 case BUILT_IN_CBRT:
4377 case BUILT_IN_COS:
4378 case BUILT_IN_COSH:
4379 case BUILT_IN_ERF:
4380 case BUILT_IN_ERFC:
4381 case BUILT_IN_EXP2:
4382 case BUILT_IN_EXP:
4383 case BUILT_IN_EXPM1:
4384 case BUILT_IN_LGAMMA:
4385 case BUILT_IN_LOG10:
4386 case BUILT_IN_LOG1P:
4387 case BUILT_IN_LOG2:
4388 case BUILT_IN_LOG:
4389 case BUILT_IN_SIN:
4390 case BUILT_IN_SINH:
4391 case BUILT_IN_SQRT:
4392 case BUILT_IN_TAN:
4393 case BUILT_IN_TANH:
4394 bdecl = builtin_decl_implicit (fn);
4395 suffix = "d2"; /* pow -> powd2 */
4396 if (el_mode != DFmode
4397 || n != 2
4398 || !bdecl)
4399 return NULL_TREE;
4400 break;
4402 case BUILT_IN_ATAN2F:
4403 case BUILT_IN_HYPOTF:
4404 case BUILT_IN_POWF:
4405 n_args = 2;
4406 /* fall through */
4408 case BUILT_IN_ACOSF:
4409 case BUILT_IN_ACOSHF:
4410 case BUILT_IN_ASINF:
4411 case BUILT_IN_ASINHF:
4412 case BUILT_IN_ATANF:
4413 case BUILT_IN_ATANHF:
4414 case BUILT_IN_CBRTF:
4415 case BUILT_IN_COSF:
4416 case BUILT_IN_COSHF:
4417 case BUILT_IN_ERFF:
4418 case BUILT_IN_ERFCF:
4419 case BUILT_IN_EXP2F:
4420 case BUILT_IN_EXPF:
4421 case BUILT_IN_EXPM1F:
4422 case BUILT_IN_LGAMMAF:
4423 case BUILT_IN_LOG10F:
4424 case BUILT_IN_LOG1PF:
4425 case BUILT_IN_LOG2F:
4426 case BUILT_IN_LOGF:
4427 case BUILT_IN_SINF:
4428 case BUILT_IN_SINHF:
4429 case BUILT_IN_SQRTF:
4430 case BUILT_IN_TANF:
4431 case BUILT_IN_TANHF:
4432 bdecl = builtin_decl_implicit (fn);
4433 suffix = "4"; /* powf -> powf4 */
4434 if (el_mode != SFmode
4435 || n != 4
4436 || !bdecl)
4437 return NULL_TREE;
4438 break;
4440 default:
4441 return NULL_TREE;
4444 else
4445 return NULL_TREE;
4447 gcc_assert (suffix != NULL);
4448 bname = IDENTIFIER_POINTER (DECL_NAME (bdecl));
4449 if (!bname)
4450 return NULL_TREE;
4452 strcpy (name, bname + sizeof ("__builtin_") - 1);
4453 strcat (name, suffix);
4455 if (n_args == 1)
4456 fntype = build_function_type_list (type_out, type_in, NULL);
4457 else if (n_args == 2)
4458 fntype = build_function_type_list (type_out, type_in, type_in, NULL);
4459 else
4460 gcc_unreachable ();
4462 /* Build a function declaration for the vectorized function. */
4463 new_fndecl = build_decl (BUILTINS_LOCATION,
4464 FUNCTION_DECL, get_identifier (name), fntype);
4465 TREE_PUBLIC (new_fndecl) = 1;
4466 DECL_EXTERNAL (new_fndecl) = 1;
4467 DECL_IS_NOVOPS (new_fndecl) = 1;
4468 TREE_READONLY (new_fndecl) = 1;
4470 return new_fndecl;
4473 /* Returns a function decl for a vectorized version of the builtin function
4474 with builtin function code FN and the result vector type TYPE, or NULL_TREE
4475 if it is not available. */
4477 static tree
4478 rs6000_builtin_vectorized_function (tree fndecl, tree type_out,
4479 tree type_in)
4481 enum machine_mode in_mode, out_mode;
4482 int in_n, out_n;
4484 if (TARGET_DEBUG_BUILTIN)
4485 fprintf (stderr, "rs6000_builtin_vectorized_function (%s, %s, %s)\n",
4486 IDENTIFIER_POINTER (DECL_NAME (fndecl)),
4487 GET_MODE_NAME (TYPE_MODE (type_out)),
4488 GET_MODE_NAME (TYPE_MODE (type_in)));
4490 if (TREE_CODE (type_out) != VECTOR_TYPE
4491 || TREE_CODE (type_in) != VECTOR_TYPE
4492 || !TARGET_VECTORIZE_BUILTINS)
4493 return NULL_TREE;
4495 out_mode = TYPE_MODE (TREE_TYPE (type_out));
4496 out_n = TYPE_VECTOR_SUBPARTS (type_out);
4497 in_mode = TYPE_MODE (TREE_TYPE (type_in));
4498 in_n = TYPE_VECTOR_SUBPARTS (type_in);
4500 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
4502 enum built_in_function fn = DECL_FUNCTION_CODE (fndecl);
4503 switch (fn)
4505 case BUILT_IN_CLZIMAX:
4506 case BUILT_IN_CLZLL:
4507 case BUILT_IN_CLZL:
4508 case BUILT_IN_CLZ:
4509 if (TARGET_P8_VECTOR && in_mode == out_mode && out_n == in_n)
4511 if (out_mode == QImode && out_n == 16)
4512 return rs6000_builtin_decls[P8V_BUILTIN_VCLZB];
4513 else if (out_mode == HImode && out_n == 8)
4514 return rs6000_builtin_decls[P8V_BUILTIN_VCLZH];
4515 else if (out_mode == SImode && out_n == 4)
4516 return rs6000_builtin_decls[P8V_BUILTIN_VCLZW];
4517 else if (out_mode == DImode && out_n == 2)
4518 return rs6000_builtin_decls[P8V_BUILTIN_VCLZD];
4520 break;
4521 case BUILT_IN_COPYSIGN:
4522 if (VECTOR_UNIT_VSX_P (V2DFmode)
4523 && out_mode == DFmode && out_n == 2
4524 && in_mode == DFmode && in_n == 2)
4525 return rs6000_builtin_decls[VSX_BUILTIN_CPSGNDP];
4526 break;
4527 case BUILT_IN_COPYSIGNF:
4528 if (out_mode != SFmode || out_n != 4
4529 || in_mode != SFmode || in_n != 4)
4530 break;
4531 if (VECTOR_UNIT_VSX_P (V4SFmode))
4532 return rs6000_builtin_decls[VSX_BUILTIN_CPSGNSP];
4533 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
4534 return rs6000_builtin_decls[ALTIVEC_BUILTIN_COPYSIGN_V4SF];
4535 break;
4536 case BUILT_IN_POPCOUNTIMAX:
4537 case BUILT_IN_POPCOUNTLL:
4538 case BUILT_IN_POPCOUNTL:
4539 case BUILT_IN_POPCOUNT:
4540 if (TARGET_P8_VECTOR && in_mode == out_mode && out_n == in_n)
4542 if (out_mode == QImode && out_n == 16)
4543 return rs6000_builtin_decls[P8V_BUILTIN_VPOPCNTB];
4544 else if (out_mode == HImode && out_n == 8)
4545 return rs6000_builtin_decls[P8V_BUILTIN_VPOPCNTH];
4546 else if (out_mode == SImode && out_n == 4)
4547 return rs6000_builtin_decls[P8V_BUILTIN_VPOPCNTW];
4548 else if (out_mode == DImode && out_n == 2)
4549 return rs6000_builtin_decls[P8V_BUILTIN_VPOPCNTD];
4551 break;
4552 case BUILT_IN_SQRT:
4553 if (VECTOR_UNIT_VSX_P (V2DFmode)
4554 && out_mode == DFmode && out_n == 2
4555 && in_mode == DFmode && in_n == 2)
4556 return rs6000_builtin_decls[VSX_BUILTIN_XVSQRTDP];
4557 break;
4558 case BUILT_IN_SQRTF:
4559 if (VECTOR_UNIT_VSX_P (V4SFmode)
4560 && out_mode == SFmode && out_n == 4
4561 && in_mode == SFmode && in_n == 4)
4562 return rs6000_builtin_decls[VSX_BUILTIN_XVSQRTSP];
4563 break;
4564 case BUILT_IN_CEIL:
4565 if (VECTOR_UNIT_VSX_P (V2DFmode)
4566 && out_mode == DFmode && out_n == 2
4567 && in_mode == DFmode && in_n == 2)
4568 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIP];
4569 break;
4570 case BUILT_IN_CEILF:
4571 if (out_mode != SFmode || out_n != 4
4572 || in_mode != SFmode || in_n != 4)
4573 break;
4574 if (VECTOR_UNIT_VSX_P (V4SFmode))
4575 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIP];
4576 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
4577 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIP];
4578 break;
4579 case BUILT_IN_FLOOR:
4580 if (VECTOR_UNIT_VSX_P (V2DFmode)
4581 && out_mode == DFmode && out_n == 2
4582 && in_mode == DFmode && in_n == 2)
4583 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIM];
4584 break;
4585 case BUILT_IN_FLOORF:
4586 if (out_mode != SFmode || out_n != 4
4587 || in_mode != SFmode || in_n != 4)
4588 break;
4589 if (VECTOR_UNIT_VSX_P (V4SFmode))
4590 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIM];
4591 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
4592 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIM];
4593 break;
4594 case BUILT_IN_FMA:
4595 if (VECTOR_UNIT_VSX_P (V2DFmode)
4596 && out_mode == DFmode && out_n == 2
4597 && in_mode == DFmode && in_n == 2)
4598 return rs6000_builtin_decls[VSX_BUILTIN_XVMADDDP];
4599 break;
4600 case BUILT_IN_FMAF:
4601 if (VECTOR_UNIT_VSX_P (V4SFmode)
4602 && out_mode == SFmode && out_n == 4
4603 && in_mode == SFmode && in_n == 4)
4604 return rs6000_builtin_decls[VSX_BUILTIN_XVMADDSP];
4605 else if (VECTOR_UNIT_ALTIVEC_P (V4SFmode)
4606 && out_mode == SFmode && out_n == 4
4607 && in_mode == SFmode && in_n == 4)
4608 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VMADDFP];
4609 break;
4610 case BUILT_IN_TRUNC:
4611 if (VECTOR_UNIT_VSX_P (V2DFmode)
4612 && out_mode == DFmode && out_n == 2
4613 && in_mode == DFmode && in_n == 2)
4614 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIZ];
4615 break;
4616 case BUILT_IN_TRUNCF:
4617 if (out_mode != SFmode || out_n != 4
4618 || in_mode != SFmode || in_n != 4)
4619 break;
4620 if (VECTOR_UNIT_VSX_P (V4SFmode))
4621 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIZ];
4622 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
4623 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIZ];
4624 break;
4625 case BUILT_IN_NEARBYINT:
4626 if (VECTOR_UNIT_VSX_P (V2DFmode)
4627 && flag_unsafe_math_optimizations
4628 && out_mode == DFmode && out_n == 2
4629 && in_mode == DFmode && in_n == 2)
4630 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPI];
4631 break;
4632 case BUILT_IN_NEARBYINTF:
4633 if (VECTOR_UNIT_VSX_P (V4SFmode)
4634 && flag_unsafe_math_optimizations
4635 && out_mode == SFmode && out_n == 4
4636 && in_mode == SFmode && in_n == 4)
4637 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPI];
4638 break;
4639 case BUILT_IN_RINT:
4640 if (VECTOR_UNIT_VSX_P (V2DFmode)
4641 && !flag_trapping_math
4642 && out_mode == DFmode && out_n == 2
4643 && in_mode == DFmode && in_n == 2)
4644 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIC];
4645 break;
4646 case BUILT_IN_RINTF:
4647 if (VECTOR_UNIT_VSX_P (V4SFmode)
4648 && !flag_trapping_math
4649 && out_mode == SFmode && out_n == 4
4650 && in_mode == SFmode && in_n == 4)
4651 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIC];
4652 break;
4653 default:
4654 break;
4658 else if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
4660 enum rs6000_builtins fn
4661 = (enum rs6000_builtins)DECL_FUNCTION_CODE (fndecl);
4662 switch (fn)
4664 case RS6000_BUILTIN_RSQRTF:
4665 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode)
4666 && out_mode == SFmode && out_n == 4
4667 && in_mode == SFmode && in_n == 4)
4668 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRSQRTFP];
4669 break;
4670 case RS6000_BUILTIN_RSQRT:
4671 if (VECTOR_UNIT_VSX_P (V2DFmode)
4672 && out_mode == DFmode && out_n == 2
4673 && in_mode == DFmode && in_n == 2)
4674 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_2DF];
4675 break;
4676 case RS6000_BUILTIN_RECIPF:
4677 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode)
4678 && out_mode == SFmode && out_n == 4
4679 && in_mode == SFmode && in_n == 4)
4680 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRECIPFP];
4681 break;
4682 case RS6000_BUILTIN_RECIP:
4683 if (VECTOR_UNIT_VSX_P (V2DFmode)
4684 && out_mode == DFmode && out_n == 2
4685 && in_mode == DFmode && in_n == 2)
4686 return rs6000_builtin_decls[VSX_BUILTIN_RECIP_V2DF];
4687 break;
4688 default:
4689 break;
4693 /* Generate calls to libmass if appropriate. */
4694 if (rs6000_veclib_handler)
4695 return rs6000_veclib_handler (fndecl, type_out, type_in);
4697 return NULL_TREE;
4700 /* Default CPU string for rs6000*_file_start functions. */
4701 static const char *rs6000_default_cpu;
4703 /* Do anything needed at the start of the asm file. */
4705 static void
4706 rs6000_file_start (void)
4708 char buffer[80];
4709 const char *start = buffer;
4710 FILE *file = asm_out_file;
4712 rs6000_default_cpu = TARGET_CPU_DEFAULT;
4714 default_file_start ();
4716 if (flag_verbose_asm)
4718 sprintf (buffer, "\n%s rs6000/powerpc options:", ASM_COMMENT_START);
4720 if (rs6000_default_cpu != 0 && rs6000_default_cpu[0] != '\0')
4722 fprintf (file, "%s --with-cpu=%s", start, rs6000_default_cpu);
4723 start = "";
4726 if (global_options_set.x_rs6000_cpu_index)
4728 fprintf (file, "%s -mcpu=%s", start,
4729 processor_target_table[rs6000_cpu_index].name);
4730 start = "";
4733 if (global_options_set.x_rs6000_tune_index)
4735 fprintf (file, "%s -mtune=%s", start,
4736 processor_target_table[rs6000_tune_index].name);
4737 start = "";
4740 if (PPC405_ERRATUM77)
4742 fprintf (file, "%s PPC405CR_ERRATUM77", start);
4743 start = "";
4746 #ifdef USING_ELFOS_H
4747 switch (rs6000_sdata)
4749 case SDATA_NONE: fprintf (file, "%s -msdata=none", start); start = ""; break;
4750 case SDATA_DATA: fprintf (file, "%s -msdata=data", start); start = ""; break;
4751 case SDATA_SYSV: fprintf (file, "%s -msdata=sysv", start); start = ""; break;
4752 case SDATA_EABI: fprintf (file, "%s -msdata=eabi", start); start = ""; break;
4755 if (rs6000_sdata && g_switch_value)
4757 fprintf (file, "%s -G %d", start,
4758 g_switch_value);
4759 start = "";
4761 #endif
4763 if (*start == '\0')
4764 putc ('\n', file);
4767 if (DEFAULT_ABI == ABI_AIX || (TARGET_ELF && flag_pic == 2))
4769 switch_to_section (toc_section);
4770 switch_to_section (text_section);
4775 /* Return nonzero if this function is known to have a null epilogue. */
4778 direct_return (void)
4780 if (reload_completed)
4782 rs6000_stack_t *info = rs6000_stack_info ();
4784 if (info->first_gp_reg_save == 32
4785 && info->first_fp_reg_save == 64
4786 && info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1
4787 && ! info->lr_save_p
4788 && ! info->cr_save_p
4789 && info->vrsave_mask == 0
4790 && ! info->push_p)
4791 return 1;
4794 return 0;
4797 /* Return the number of instructions it takes to form a constant in an
4798 integer register. */
4801 num_insns_constant_wide (HOST_WIDE_INT value)
4803 /* signed constant loadable with addi */
4804 if ((unsigned HOST_WIDE_INT) (value + 0x8000) < 0x10000)
4805 return 1;
4807 /* constant loadable with addis */
4808 else if ((value & 0xffff) == 0
4809 && (value >> 31 == -1 || value >> 31 == 0))
4810 return 1;
4812 else if (TARGET_POWERPC64)
4814 HOST_WIDE_INT low = ((value & 0xffffffff) ^ 0x80000000) - 0x80000000;
4815 HOST_WIDE_INT high = value >> 31;
4817 if (high == 0 || high == -1)
4818 return 2;
4820 high >>= 1;
4822 if (low == 0)
4823 return num_insns_constant_wide (high) + 1;
4824 else if (high == 0)
4825 return num_insns_constant_wide (low) + 1;
4826 else
4827 return (num_insns_constant_wide (high)
4828 + num_insns_constant_wide (low) + 1);
4831 else
4832 return 2;
4836 num_insns_constant (rtx op, enum machine_mode mode)
4838 HOST_WIDE_INT low, high;
4840 switch (GET_CODE (op))
4842 case CONST_INT:
4843 if ((INTVAL (op) >> 31) != 0 && (INTVAL (op) >> 31) != -1
4844 && mask64_operand (op, mode))
4845 return 2;
4846 else
4847 return num_insns_constant_wide (INTVAL (op));
4849 case CONST_DOUBLE:
4850 if (mode == SFmode || mode == SDmode)
4852 long l;
4853 REAL_VALUE_TYPE rv;
4855 REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
4856 if (DECIMAL_FLOAT_MODE_P (mode))
4857 REAL_VALUE_TO_TARGET_DECIMAL32 (rv, l);
4858 else
4859 REAL_VALUE_TO_TARGET_SINGLE (rv, l);
4860 return num_insns_constant_wide ((HOST_WIDE_INT) l);
4863 long l[2];
4864 REAL_VALUE_TYPE rv;
4866 REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
4867 if (DECIMAL_FLOAT_MODE_P (mode))
4868 REAL_VALUE_TO_TARGET_DECIMAL64 (rv, l);
4869 else
4870 REAL_VALUE_TO_TARGET_DOUBLE (rv, l);
4871 high = l[WORDS_BIG_ENDIAN == 0];
4872 low = l[WORDS_BIG_ENDIAN != 0];
4874 if (TARGET_32BIT)
4875 return (num_insns_constant_wide (low)
4876 + num_insns_constant_wide (high));
4877 else
4879 if ((high == 0 && low >= 0)
4880 || (high == -1 && low < 0))
4881 return num_insns_constant_wide (low);
4883 else if (mask64_operand (op, mode))
4884 return 2;
4886 else if (low == 0)
4887 return num_insns_constant_wide (high) + 1;
4889 else
4890 return (num_insns_constant_wide (high)
4891 + num_insns_constant_wide (low) + 1);
4894 default:
4895 gcc_unreachable ();
4899 /* Interpret element ELT of the CONST_VECTOR OP as an integer value.
4900 If the mode of OP is MODE_VECTOR_INT, this simply returns the
4901 corresponding element of the vector, but for V4SFmode and V2SFmode,
4902 the corresponding "float" is interpreted as an SImode integer. */
4904 HOST_WIDE_INT
4905 const_vector_elt_as_int (rtx op, unsigned int elt)
4907 rtx tmp;
4909 /* We can't handle V2DImode and V2DFmode vector constants here yet. */
4910 gcc_assert (GET_MODE (op) != V2DImode
4911 && GET_MODE (op) != V2DFmode);
4913 tmp = CONST_VECTOR_ELT (op, elt);
4914 if (GET_MODE (op) == V4SFmode
4915 || GET_MODE (op) == V2SFmode)
4916 tmp = gen_lowpart (SImode, tmp);
4917 return INTVAL (tmp);
4920 /* Return true if OP can be synthesized with a particular vspltisb, vspltish
4921 or vspltisw instruction. OP is a CONST_VECTOR. Which instruction is used
4922 depends on STEP and COPIES, one of which will be 1. If COPIES > 1,
4923 all items are set to the same value and contain COPIES replicas of the
4924 vsplt's operand; if STEP > 1, one in STEP elements is set to the vsplt's
4925 operand and the others are set to the value of the operand's msb. */
4927 static bool
4928 vspltis_constant (rtx op, unsigned step, unsigned copies)
4930 enum machine_mode mode = GET_MODE (op);
4931 enum machine_mode inner = GET_MODE_INNER (mode);
4933 unsigned i;
4934 unsigned nunits;
4935 unsigned bitsize;
4936 unsigned mask;
4938 HOST_WIDE_INT val;
4939 HOST_WIDE_INT splat_val;
4940 HOST_WIDE_INT msb_val;
4942 if (mode == V2DImode || mode == V2DFmode)
4943 return false;
4945 nunits = GET_MODE_NUNITS (mode);
4946 bitsize = GET_MODE_BITSIZE (inner);
4947 mask = GET_MODE_MASK (inner);
4949 val = const_vector_elt_as_int (op, BYTES_BIG_ENDIAN ? nunits - 1 : 0);
4950 splat_val = val;
4951 msb_val = val > 0 ? 0 : -1;
4953 /* Construct the value to be splatted, if possible. If not, return 0. */
4954 for (i = 2; i <= copies; i *= 2)
4956 HOST_WIDE_INT small_val;
4957 bitsize /= 2;
4958 small_val = splat_val >> bitsize;
4959 mask >>= bitsize;
4960 if (splat_val != ((small_val << bitsize) | (small_val & mask)))
4961 return false;
4962 splat_val = small_val;
4965 /* Check if SPLAT_VAL can really be the operand of a vspltis[bhw]. */
4966 if (EASY_VECTOR_15 (splat_val))
4969 /* Also check if we can splat, and then add the result to itself. Do so if
4970 the value is positive, of if the splat instruction is using OP's mode;
4971 for splat_val < 0, the splat and the add should use the same mode. */
4972 else if (EASY_VECTOR_15_ADD_SELF (splat_val)
4973 && (splat_val >= 0 || (step == 1 && copies == 1)))
4976 /* Also check if are loading up the most significant bit which can be done by
4977 loading up -1 and shifting the value left by -1. */
4978 else if (EASY_VECTOR_MSB (splat_val, inner))
4981 else
4982 return false;
4984 /* Check if VAL is present in every STEP-th element, and the
4985 other elements are filled with its most significant bit. */
4986 for (i = 1; i < nunits; ++i)
4988 HOST_WIDE_INT desired_val;
4989 unsigned elt = BYTES_BIG_ENDIAN ? nunits - 1 - i : i;
4990 if ((i & (step - 1)) == 0)
4991 desired_val = val;
4992 else
4993 desired_val = msb_val;
4995 if (desired_val != const_vector_elt_as_int (op, elt))
4996 return false;
4999 return true;
5003 /* Return true if OP is of the given MODE and can be synthesized
5004 with a vspltisb, vspltish or vspltisw. */
5006 bool
5007 easy_altivec_constant (rtx op, enum machine_mode mode)
5009 unsigned step, copies;
5011 if (mode == VOIDmode)
5012 mode = GET_MODE (op);
5013 else if (mode != GET_MODE (op))
5014 return false;
5016 /* V2DI/V2DF was added with VSX. Only allow 0 and all 1's as easy
5017 constants. */
5018 if (mode == V2DFmode)
5019 return zero_constant (op, mode);
5021 if (mode == V2DImode)
5023 /* In case the compiler is built 32-bit, CONST_DOUBLE constants are not
5024 easy. */
5025 if (GET_CODE (CONST_VECTOR_ELT (op, 0)) != CONST_INT
5026 || GET_CODE (CONST_VECTOR_ELT (op, 1)) != CONST_INT)
5027 return false;
5029 if (zero_constant (op, mode))
5030 return true;
5032 if (INTVAL (CONST_VECTOR_ELT (op, 0)) == -1
5033 && INTVAL (CONST_VECTOR_ELT (op, 1)) == -1)
5034 return true;
5036 return false;
5039 /* Start with a vspltisw. */
5040 step = GET_MODE_NUNITS (mode) / 4;
5041 copies = 1;
5043 if (vspltis_constant (op, step, copies))
5044 return true;
5046 /* Then try with a vspltish. */
5047 if (step == 1)
5048 copies <<= 1;
5049 else
5050 step >>= 1;
5052 if (vspltis_constant (op, step, copies))
5053 return true;
5055 /* And finally a vspltisb. */
5056 if (step == 1)
5057 copies <<= 1;
5058 else
5059 step >>= 1;
5061 if (vspltis_constant (op, step, copies))
5062 return true;
5064 return false;
5067 /* Generate a VEC_DUPLICATE representing a vspltis[bhw] instruction whose
5068 result is OP. Abort if it is not possible. */
5071 gen_easy_altivec_constant (rtx op)
5073 enum machine_mode mode = GET_MODE (op);
5074 int nunits = GET_MODE_NUNITS (mode);
5075 rtx val = CONST_VECTOR_ELT (op, BYTES_BIG_ENDIAN ? nunits - 1 : 0);
5076 unsigned step = nunits / 4;
5077 unsigned copies = 1;
5079 /* Start with a vspltisw. */
5080 if (vspltis_constant (op, step, copies))
5081 return gen_rtx_VEC_DUPLICATE (V4SImode, gen_lowpart (SImode, val));
5083 /* Then try with a vspltish. */
5084 if (step == 1)
5085 copies <<= 1;
5086 else
5087 step >>= 1;
5089 if (vspltis_constant (op, step, copies))
5090 return gen_rtx_VEC_DUPLICATE (V8HImode, gen_lowpart (HImode, val));
5092 /* And finally a vspltisb. */
5093 if (step == 1)
5094 copies <<= 1;
5095 else
5096 step >>= 1;
5098 if (vspltis_constant (op, step, copies))
5099 return gen_rtx_VEC_DUPLICATE (V16QImode, gen_lowpart (QImode, val));
5101 gcc_unreachable ();
5104 const char *
5105 output_vec_const_move (rtx *operands)
5107 int cst, cst2;
5108 enum machine_mode mode;
5109 rtx dest, vec;
5111 dest = operands[0];
5112 vec = operands[1];
5113 mode = GET_MODE (dest);
5115 if (TARGET_VSX)
5117 if (zero_constant (vec, mode))
5118 return "xxlxor %x0,%x0,%x0";
5120 if (mode == V2DImode
5121 && INTVAL (CONST_VECTOR_ELT (vec, 0)) == -1
5122 && INTVAL (CONST_VECTOR_ELT (vec, 1)) == -1)
5123 return "vspltisw %0,-1";
5126 if (TARGET_ALTIVEC)
5128 rtx splat_vec;
5129 if (zero_constant (vec, mode))
5130 return "vxor %0,%0,%0";
5132 splat_vec = gen_easy_altivec_constant (vec);
5133 gcc_assert (GET_CODE (splat_vec) == VEC_DUPLICATE);
5134 operands[1] = XEXP (splat_vec, 0);
5135 if (!EASY_VECTOR_15 (INTVAL (operands[1])))
5136 return "#";
5138 switch (GET_MODE (splat_vec))
5140 case V4SImode:
5141 return "vspltisw %0,%1";
5143 case V8HImode:
5144 return "vspltish %0,%1";
5146 case V16QImode:
5147 return "vspltisb %0,%1";
5149 default:
5150 gcc_unreachable ();
5154 gcc_assert (TARGET_SPE);
5156 /* Vector constant 0 is handled as a splitter of V2SI, and in the
5157 pattern of V1DI, V4HI, and V2SF.
5159 FIXME: We should probably return # and add post reload
5160 splitters for these, but this way is so easy ;-). */
5161 cst = INTVAL (CONST_VECTOR_ELT (vec, 0));
5162 cst2 = INTVAL (CONST_VECTOR_ELT (vec, 1));
5163 operands[1] = CONST_VECTOR_ELT (vec, 0);
5164 operands[2] = CONST_VECTOR_ELT (vec, 1);
5165 if (cst == cst2)
5166 return "li %0,%1\n\tevmergelo %0,%0,%0";
5167 else
5168 return "li %0,%1\n\tevmergelo %0,%0,%0\n\tli %0,%2";
5171 /* Initialize TARGET of vector PAIRED to VALS. */
5173 void
5174 paired_expand_vector_init (rtx target, rtx vals)
5176 enum machine_mode mode = GET_MODE (target);
5177 int n_elts = GET_MODE_NUNITS (mode);
5178 int n_var = 0;
5179 rtx x, new_rtx, tmp, constant_op, op1, op2;
5180 int i;
5182 for (i = 0; i < n_elts; ++i)
5184 x = XVECEXP (vals, 0, i);
5185 if (!(CONST_INT_P (x)
5186 || GET_CODE (x) == CONST_DOUBLE
5187 || GET_CODE (x) == CONST_FIXED))
5188 ++n_var;
5190 if (n_var == 0)
5192 /* Load from constant pool. */
5193 emit_move_insn (target, gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)));
5194 return;
5197 if (n_var == 2)
5199 /* The vector is initialized only with non-constants. */
5200 new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, XVECEXP (vals, 0, 0),
5201 XVECEXP (vals, 0, 1));
5203 emit_move_insn (target, new_rtx);
5204 return;
5207 /* One field is non-constant and the other one is a constant. Load the
5208 constant from the constant pool and use ps_merge instruction to
5209 construct the whole vector. */
5210 op1 = XVECEXP (vals, 0, 0);
5211 op2 = XVECEXP (vals, 0, 1);
5213 constant_op = (CONSTANT_P (op1)) ? op1 : op2;
5215 tmp = gen_reg_rtx (GET_MODE (constant_op));
5216 emit_move_insn (tmp, constant_op);
5218 if (CONSTANT_P (op1))
5219 new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, tmp, op2);
5220 else
5221 new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, op1, tmp);
5223 emit_move_insn (target, new_rtx);
5226 void
5227 paired_expand_vector_move (rtx operands[])
5229 rtx op0 = operands[0], op1 = operands[1];
5231 emit_move_insn (op0, op1);
5234 /* Emit vector compare for code RCODE. DEST is destination, OP1 and
5235 OP2 are two VEC_COND_EXPR operands, CC_OP0 and CC_OP1 are the two
5236 operands for the relation operation COND. This is a recursive
5237 function. */
5239 static void
5240 paired_emit_vector_compare (enum rtx_code rcode,
5241 rtx dest, rtx op0, rtx op1,
5242 rtx cc_op0, rtx cc_op1)
5244 rtx tmp = gen_reg_rtx (V2SFmode);
5245 rtx tmp1, max, min;
5247 gcc_assert (TARGET_PAIRED_FLOAT);
5248 gcc_assert (GET_MODE (op0) == GET_MODE (op1));
5250 switch (rcode)
5252 case LT:
5253 case LTU:
5254 paired_emit_vector_compare (GE, dest, op1, op0, cc_op0, cc_op1);
5255 return;
5256 case GE:
5257 case GEU:
5258 emit_insn (gen_subv2sf3 (tmp, cc_op0, cc_op1));
5259 emit_insn (gen_selv2sf4 (dest, tmp, op0, op1, CONST0_RTX (SFmode)));
5260 return;
5261 case LE:
5262 case LEU:
5263 paired_emit_vector_compare (GE, dest, op0, op1, cc_op1, cc_op0);
5264 return;
5265 case GT:
5266 paired_emit_vector_compare (LE, dest, op1, op0, cc_op0, cc_op1);
5267 return;
5268 case EQ:
5269 tmp1 = gen_reg_rtx (V2SFmode);
5270 max = gen_reg_rtx (V2SFmode);
5271 min = gen_reg_rtx (V2SFmode);
5272 gen_reg_rtx (V2SFmode);
5274 emit_insn (gen_subv2sf3 (tmp, cc_op0, cc_op1));
5275 emit_insn (gen_selv2sf4
5276 (max, tmp, cc_op0, cc_op1, CONST0_RTX (SFmode)));
5277 emit_insn (gen_subv2sf3 (tmp, cc_op1, cc_op0));
5278 emit_insn (gen_selv2sf4
5279 (min, tmp, cc_op0, cc_op1, CONST0_RTX (SFmode)));
5280 emit_insn (gen_subv2sf3 (tmp1, min, max));
5281 emit_insn (gen_selv2sf4 (dest, tmp1, op0, op1, CONST0_RTX (SFmode)));
5282 return;
5283 case NE:
5284 paired_emit_vector_compare (EQ, dest, op1, op0, cc_op0, cc_op1);
5285 return;
5286 case UNLE:
5287 paired_emit_vector_compare (LE, dest, op1, op0, cc_op0, cc_op1);
5288 return;
5289 case UNLT:
5290 paired_emit_vector_compare (LT, dest, op1, op0, cc_op0, cc_op1);
5291 return;
5292 case UNGE:
5293 paired_emit_vector_compare (GE, dest, op1, op0, cc_op0, cc_op1);
5294 return;
5295 case UNGT:
5296 paired_emit_vector_compare (GT, dest, op1, op0, cc_op0, cc_op1);
5297 return;
5298 default:
5299 gcc_unreachable ();
5302 return;
5305 /* Emit vector conditional expression.
5306 DEST is destination. OP1 and OP2 are two VEC_COND_EXPR operands.
5307 CC_OP0 and CC_OP1 are the two operands for the relation operation COND. */
5310 paired_emit_vector_cond_expr (rtx dest, rtx op1, rtx op2,
5311 rtx cond, rtx cc_op0, rtx cc_op1)
5313 enum rtx_code rcode = GET_CODE (cond);
5315 if (!TARGET_PAIRED_FLOAT)
5316 return 0;
5318 paired_emit_vector_compare (rcode, dest, op1, op2, cc_op0, cc_op1);
5320 return 1;
5323 /* Initialize vector TARGET to VALS. */
5325 void
5326 rs6000_expand_vector_init (rtx target, rtx vals)
5328 enum machine_mode mode = GET_MODE (target);
5329 enum machine_mode inner_mode = GET_MODE_INNER (mode);
5330 int n_elts = GET_MODE_NUNITS (mode);
5331 int n_var = 0, one_var = -1;
5332 bool all_same = true, all_const_zero = true;
5333 rtx x, mem;
5334 int i;
5336 for (i = 0; i < n_elts; ++i)
5338 x = XVECEXP (vals, 0, i);
5339 if (!(CONST_INT_P (x)
5340 || GET_CODE (x) == CONST_DOUBLE
5341 || GET_CODE (x) == CONST_FIXED))
5342 ++n_var, one_var = i;
5343 else if (x != CONST0_RTX (inner_mode))
5344 all_const_zero = false;
5346 if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
5347 all_same = false;
5350 if (n_var == 0)
5352 rtx const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0));
5353 bool int_vector_p = (GET_MODE_CLASS (mode) == MODE_VECTOR_INT);
5354 if ((int_vector_p || TARGET_VSX) && all_const_zero)
5356 /* Zero register. */
5357 emit_insn (gen_rtx_SET (VOIDmode, target,
5358 gen_rtx_XOR (mode, target, target)));
5359 return;
5361 else if (int_vector_p && easy_vector_constant (const_vec, mode))
5363 /* Splat immediate. */
5364 emit_insn (gen_rtx_SET (VOIDmode, target, const_vec));
5365 return;
5367 else
5369 /* Load from constant pool. */
5370 emit_move_insn (target, const_vec);
5371 return;
5375 /* Double word values on VSX can use xxpermdi or lxvdsx. */
5376 if (VECTOR_MEM_VSX_P (mode) && (mode == V2DFmode || mode == V2DImode))
5378 rtx op0 = XVECEXP (vals, 0, 0);
5379 rtx op1 = XVECEXP (vals, 0, 1);
5380 if (all_same)
5382 if (!MEM_P (op0) && !REG_P (op0))
5383 op0 = force_reg (inner_mode, op0);
5384 if (mode == V2DFmode)
5385 emit_insn (gen_vsx_splat_v2df (target, op0));
5386 else
5387 emit_insn (gen_vsx_splat_v2di (target, op0));
5389 else
5391 op0 = force_reg (inner_mode, op0);
5392 op1 = force_reg (inner_mode, op1);
5393 if (mode == V2DFmode)
5394 emit_insn (gen_vsx_concat_v2df (target, op0, op1));
5395 else
5396 emit_insn (gen_vsx_concat_v2di (target, op0, op1));
5398 return;
5401 /* With single precision floating point on VSX, know that internally single
5402 precision is actually represented as a double, and either make 2 V2DF
5403 vectors, and convert these vectors to single precision, or do one
5404 conversion, and splat the result to the other elements. */
5405 if (mode == V4SFmode && VECTOR_MEM_VSX_P (mode))
5407 if (all_same)
5409 rtx freg = gen_reg_rtx (V4SFmode);
5410 rtx sreg = force_reg (SFmode, XVECEXP (vals, 0, 0));
5411 rtx cvt = ((TARGET_XSCVDPSPN)
5412 ? gen_vsx_xscvdpspn_scalar (freg, sreg)
5413 : gen_vsx_xscvdpsp_scalar (freg, sreg));
5415 emit_insn (cvt);
5416 emit_insn (gen_vsx_xxspltw_v4sf (target, freg, const0_rtx));
5418 else
5420 rtx dbl_even = gen_reg_rtx (V2DFmode);
5421 rtx dbl_odd = gen_reg_rtx (V2DFmode);
5422 rtx flt_even = gen_reg_rtx (V4SFmode);
5423 rtx flt_odd = gen_reg_rtx (V4SFmode);
5424 rtx op0 = force_reg (SFmode, XVECEXP (vals, 0, 0));
5425 rtx op1 = force_reg (SFmode, XVECEXP (vals, 0, 1));
5426 rtx op2 = force_reg (SFmode, XVECEXP (vals, 0, 2));
5427 rtx op3 = force_reg (SFmode, XVECEXP (vals, 0, 3));
5429 emit_insn (gen_vsx_concat_v2sf (dbl_even, op0, op1));
5430 emit_insn (gen_vsx_concat_v2sf (dbl_odd, op2, op3));
5431 emit_insn (gen_vsx_xvcvdpsp (flt_even, dbl_even));
5432 emit_insn (gen_vsx_xvcvdpsp (flt_odd, dbl_odd));
5433 rs6000_expand_extract_even (target, flt_even, flt_odd);
5435 return;
5438 /* Store value to stack temp. Load vector element. Splat. However, splat
5439 of 64-bit items is not supported on Altivec. */
5440 if (all_same && GET_MODE_SIZE (inner_mode) <= 4)
5442 rtx field;
5443 mem = assign_stack_temp (mode, GET_MODE_SIZE (inner_mode));
5444 emit_move_insn (adjust_address_nv (mem, inner_mode, 0),
5445 XVECEXP (vals, 0, 0));
5446 x = gen_rtx_UNSPEC (VOIDmode,
5447 gen_rtvec (1, const0_rtx), UNSPEC_LVE);
5448 emit_insn (gen_rtx_PARALLEL (VOIDmode,
5449 gen_rtvec (2,
5450 gen_rtx_SET (VOIDmode,
5451 target, mem),
5452 x)));
5453 field = (BYTES_BIG_ENDIAN ? const0_rtx
5454 : GEN_INT (GET_MODE_NUNITS (mode) - 1));
5455 x = gen_rtx_VEC_SELECT (inner_mode, target,
5456 gen_rtx_PARALLEL (VOIDmode,
5457 gen_rtvec (1, field)));
5458 emit_insn (gen_rtx_SET (VOIDmode, target,
5459 gen_rtx_VEC_DUPLICATE (mode, x)));
5460 return;
5463 /* One field is non-constant. Load constant then overwrite
5464 varying field. */
5465 if (n_var == 1)
5467 rtx copy = copy_rtx (vals);
5469 /* Load constant part of vector, substitute neighboring value for
5470 varying element. */
5471 XVECEXP (copy, 0, one_var) = XVECEXP (vals, 0, (one_var + 1) % n_elts);
5472 rs6000_expand_vector_init (target, copy);
5474 /* Insert variable. */
5475 rs6000_expand_vector_set (target, XVECEXP (vals, 0, one_var), one_var);
5476 return;
5479 /* Construct the vector in memory one field at a time
5480 and load the whole vector. */
5481 mem = assign_stack_temp (mode, GET_MODE_SIZE (mode));
5482 for (i = 0; i < n_elts; i++)
5483 emit_move_insn (adjust_address_nv (mem, inner_mode,
5484 i * GET_MODE_SIZE (inner_mode)),
5485 XVECEXP (vals, 0, i));
5486 emit_move_insn (target, mem);
5489 /* Set field ELT of TARGET to VAL. */
5491 void
5492 rs6000_expand_vector_set (rtx target, rtx val, int elt)
5494 enum machine_mode mode = GET_MODE (target);
5495 enum machine_mode inner_mode = GET_MODE_INNER (mode);
5496 rtx reg = gen_reg_rtx (mode);
5497 rtx mask, mem, x;
5498 int width = GET_MODE_SIZE (inner_mode);
5499 int i;
5501 if (VECTOR_MEM_VSX_P (mode) && (mode == V2DFmode || mode == V2DImode))
5503 rtx (*set_func) (rtx, rtx, rtx, rtx)
5504 = ((mode == V2DFmode) ? gen_vsx_set_v2df : gen_vsx_set_v2di);
5505 emit_insn (set_func (target, target, val, GEN_INT (elt)));
5506 return;
5509 /* Load single variable value. */
5510 mem = assign_stack_temp (mode, GET_MODE_SIZE (inner_mode));
5511 emit_move_insn (adjust_address_nv (mem, inner_mode, 0), val);
5512 x = gen_rtx_UNSPEC (VOIDmode,
5513 gen_rtvec (1, const0_rtx), UNSPEC_LVE);
5514 emit_insn (gen_rtx_PARALLEL (VOIDmode,
5515 gen_rtvec (2,
5516 gen_rtx_SET (VOIDmode,
5517 reg, mem),
5518 x)));
5520 /* Linear sequence. */
5521 mask = gen_rtx_PARALLEL (V16QImode, rtvec_alloc (16));
5522 for (i = 0; i < 16; ++i)
5523 XVECEXP (mask, 0, i) = GEN_INT (i);
5525 /* Set permute mask to insert element into target. */
5526 for (i = 0; i < width; ++i)
5527 XVECEXP (mask, 0, elt*width + i)
5528 = GEN_INT (i + 0x10);
5529 x = gen_rtx_CONST_VECTOR (V16QImode, XVEC (mask, 0));
5531 if (BYTES_BIG_ENDIAN)
5532 x = gen_rtx_UNSPEC (mode,
5533 gen_rtvec (3, target, reg,
5534 force_reg (V16QImode, x)),
5535 UNSPEC_VPERM);
5536 else
5538 /* Invert selector. */
5539 rtx splat = gen_rtx_VEC_DUPLICATE (V16QImode,
5540 gen_rtx_CONST_INT (QImode, -1));
5541 rtx tmp = gen_reg_rtx (V16QImode);
5542 emit_move_insn (tmp, splat);
5543 x = gen_rtx_MINUS (V16QImode, tmp, force_reg (V16QImode, x));
5544 emit_move_insn (tmp, x);
5546 /* Permute with operands reversed and adjusted selector. */
5547 x = gen_rtx_UNSPEC (mode, gen_rtvec (3, reg, target, tmp),
5548 UNSPEC_VPERM);
5551 emit_insn (gen_rtx_SET (VOIDmode, target, x));
5554 /* Extract field ELT from VEC into TARGET. */
5556 void
5557 rs6000_expand_vector_extract (rtx target, rtx vec, int elt)
5559 enum machine_mode mode = GET_MODE (vec);
5560 enum machine_mode inner_mode = GET_MODE_INNER (mode);
5561 rtx mem;
5563 if (VECTOR_MEM_VSX_P (mode))
5565 switch (mode)
5567 default:
5568 break;
5569 case V2DFmode:
5570 emit_insn (gen_vsx_extract_v2df (target, vec, GEN_INT (elt)));
5571 return;
5572 case V2DImode:
5573 emit_insn (gen_vsx_extract_v2di (target, vec, GEN_INT (elt)));
5574 return;
5575 case V4SFmode:
5576 emit_insn (gen_vsx_extract_v4sf (target, vec, GEN_INT (elt)));
5577 return;
5581 /* Allocate mode-sized buffer. */
5582 mem = assign_stack_temp (mode, GET_MODE_SIZE (mode));
5584 emit_move_insn (mem, vec);
5586 /* Add offset to field within buffer matching vector element. */
5587 mem = adjust_address_nv (mem, inner_mode, elt * GET_MODE_SIZE (inner_mode));
5589 emit_move_insn (target, adjust_address_nv (mem, inner_mode, 0));
5592 /* Generates shifts and masks for a pair of rldicl or rldicr insns to
5593 implement ANDing by the mask IN. */
5594 void
5595 build_mask64_2_operands (rtx in, rtx *out)
5597 unsigned HOST_WIDE_INT c, lsb, m1, m2;
5598 int shift;
5600 gcc_assert (GET_CODE (in) == CONST_INT);
5602 c = INTVAL (in);
5603 if (c & 1)
5605 /* Assume c initially something like 0x00fff000000fffff. The idea
5606 is to rotate the word so that the middle ^^^^^^ group of zeros
5607 is at the MS end and can be cleared with an rldicl mask. We then
5608 rotate back and clear off the MS ^^ group of zeros with a
5609 second rldicl. */
5610 c = ~c; /* c == 0xff000ffffff00000 */
5611 lsb = c & -c; /* lsb == 0x0000000000100000 */
5612 m1 = -lsb; /* m1 == 0xfffffffffff00000 */
5613 c = ~c; /* c == 0x00fff000000fffff */
5614 c &= -lsb; /* c == 0x00fff00000000000 */
5615 lsb = c & -c; /* lsb == 0x0000100000000000 */
5616 c = ~c; /* c == 0xff000fffffffffff */
5617 c &= -lsb; /* c == 0xff00000000000000 */
5618 shift = 0;
5619 while ((lsb >>= 1) != 0)
5620 shift++; /* shift == 44 on exit from loop */
5621 m1 <<= 64 - shift; /* m1 == 0xffffff0000000000 */
5622 m1 = ~m1; /* m1 == 0x000000ffffffffff */
5623 m2 = ~c; /* m2 == 0x00ffffffffffffff */
5625 else
5627 /* Assume c initially something like 0xff000f0000000000. The idea
5628 is to rotate the word so that the ^^^ middle group of zeros
5629 is at the LS end and can be cleared with an rldicr mask. We then
5630 rotate back and clear off the LS group of ^^^^^^^^^^ zeros with
5631 a second rldicr. */
5632 lsb = c & -c; /* lsb == 0x0000010000000000 */
5633 m2 = -lsb; /* m2 == 0xffffff0000000000 */
5634 c = ~c; /* c == 0x00fff0ffffffffff */
5635 c &= -lsb; /* c == 0x00fff00000000000 */
5636 lsb = c & -c; /* lsb == 0x0000100000000000 */
5637 c = ~c; /* c == 0xff000fffffffffff */
5638 c &= -lsb; /* c == 0xff00000000000000 */
5639 shift = 0;
5640 while ((lsb >>= 1) != 0)
5641 shift++; /* shift == 44 on exit from loop */
5642 m1 = ~c; /* m1 == 0x00ffffffffffffff */
5643 m1 >>= shift; /* m1 == 0x0000000000000fff */
5644 m1 = ~m1; /* m1 == 0xfffffffffffff000 */
5647 /* Note that when we only have two 0->1 and 1->0 transitions, one of the
5648 masks will be all 1's. We are guaranteed more than one transition. */
5649 out[0] = GEN_INT (64 - shift);
5650 out[1] = GEN_INT (m1);
5651 out[2] = GEN_INT (shift);
5652 out[3] = GEN_INT (m2);
5655 /* Return TRUE if OP is an invalid SUBREG operation on the e500. */
5657 bool
5658 invalid_e500_subreg (rtx op, enum machine_mode mode)
5660 if (TARGET_E500_DOUBLE)
5662 /* Reject (subreg:SI (reg:DF)); likewise with subreg:DI or
5663 subreg:TI and reg:TF. Decimal float modes are like integer
5664 modes (only low part of each register used) for this
5665 purpose. */
5666 if (GET_CODE (op) == SUBREG
5667 && (mode == SImode || mode == DImode || mode == TImode
5668 || mode == DDmode || mode == TDmode || mode == PTImode)
5669 && REG_P (SUBREG_REG (op))
5670 && (GET_MODE (SUBREG_REG (op)) == DFmode
5671 || GET_MODE (SUBREG_REG (op)) == TFmode))
5672 return true;
5674 /* Reject (subreg:DF (reg:DI)); likewise with subreg:TF and
5675 reg:TI. */
5676 if (GET_CODE (op) == SUBREG
5677 && (mode == DFmode || mode == TFmode)
5678 && REG_P (SUBREG_REG (op))
5679 && (GET_MODE (SUBREG_REG (op)) == DImode
5680 || GET_MODE (SUBREG_REG (op)) == TImode
5681 || GET_MODE (SUBREG_REG (op)) == PTImode
5682 || GET_MODE (SUBREG_REG (op)) == DDmode
5683 || GET_MODE (SUBREG_REG (op)) == TDmode))
5684 return true;
5687 if (TARGET_SPE
5688 && GET_CODE (op) == SUBREG
5689 && mode == SImode
5690 && REG_P (SUBREG_REG (op))
5691 && SPE_VECTOR_MODE (GET_MODE (SUBREG_REG (op))))
5692 return true;
5694 return false;
5697 /* Return alignment of TYPE. Existing alignment is ALIGN. HOW
5698 selects whether the alignment is abi mandated, optional, or
5699 both abi and optional alignment. */
5701 unsigned int
5702 rs6000_data_alignment (tree type, unsigned int align, enum data_align how)
5704 if (how != align_opt)
5706 if (TREE_CODE (type) == VECTOR_TYPE)
5708 if ((TARGET_SPE && SPE_VECTOR_MODE (TYPE_MODE (type)))
5709 || (TARGET_PAIRED_FLOAT && PAIRED_VECTOR_MODE (TYPE_MODE (type))))
5711 if (align < 64)
5712 align = 64;
5714 else if (align < 128)
5715 align = 128;
5717 else if (TARGET_E500_DOUBLE
5718 && TREE_CODE (type) == REAL_TYPE
5719 && TYPE_MODE (type) == DFmode)
5721 if (align < 64)
5722 align = 64;
5726 if (how != align_abi)
5728 if (TREE_CODE (type) == ARRAY_TYPE
5729 && TYPE_MODE (TREE_TYPE (type)) == QImode)
5731 if (align < BITS_PER_WORD)
5732 align = BITS_PER_WORD;
5736 return align;
5739 /* AIX increases natural record alignment to doubleword if the first
5740 field is an FP double while the FP fields remain word aligned. */
5742 unsigned int
5743 rs6000_special_round_type_align (tree type, unsigned int computed,
5744 unsigned int specified)
5746 unsigned int align = MAX (computed, specified);
5747 tree field = TYPE_FIELDS (type);
5749 /* Skip all non field decls */
5750 while (field != NULL && TREE_CODE (field) != FIELD_DECL)
5751 field = DECL_CHAIN (field);
5753 if (field != NULL && field != type)
5755 type = TREE_TYPE (field);
5756 while (TREE_CODE (type) == ARRAY_TYPE)
5757 type = TREE_TYPE (type);
5759 if (type != error_mark_node && TYPE_MODE (type) == DFmode)
5760 align = MAX (align, 64);
5763 return align;
5766 /* Darwin increases record alignment to the natural alignment of
5767 the first field. */
5769 unsigned int
5770 darwin_rs6000_special_round_type_align (tree type, unsigned int computed,
5771 unsigned int specified)
5773 unsigned int align = MAX (computed, specified);
5775 if (TYPE_PACKED (type))
5776 return align;
5778 /* Find the first field, looking down into aggregates. */
5779 do {
5780 tree field = TYPE_FIELDS (type);
5781 /* Skip all non field decls */
5782 while (field != NULL && TREE_CODE (field) != FIELD_DECL)
5783 field = DECL_CHAIN (field);
5784 if (! field)
5785 break;
5786 /* A packed field does not contribute any extra alignment. */
5787 if (DECL_PACKED (field))
5788 return align;
5789 type = TREE_TYPE (field);
5790 while (TREE_CODE (type) == ARRAY_TYPE)
5791 type = TREE_TYPE (type);
5792 } while (AGGREGATE_TYPE_P (type));
5794 if (! AGGREGATE_TYPE_P (type) && type != error_mark_node)
5795 align = MAX (align, TYPE_ALIGN (type));
5797 return align;
5800 /* Return 1 for an operand in small memory on V.4/eabi. */
5803 small_data_operand (rtx op ATTRIBUTE_UNUSED,
5804 enum machine_mode mode ATTRIBUTE_UNUSED)
5806 #if TARGET_ELF
5807 rtx sym_ref;
5809 if (rs6000_sdata == SDATA_NONE || rs6000_sdata == SDATA_DATA)
5810 return 0;
5812 if (DEFAULT_ABI != ABI_V4)
5813 return 0;
5815 /* Vector and float memory instructions have a limited offset on the
5816 SPE, so using a vector or float variable directly as an operand is
5817 not useful. */
5818 if (TARGET_SPE
5819 && (SPE_VECTOR_MODE (mode) || FLOAT_MODE_P (mode)))
5820 return 0;
5822 if (GET_CODE (op) == SYMBOL_REF)
5823 sym_ref = op;
5825 else if (GET_CODE (op) != CONST
5826 || GET_CODE (XEXP (op, 0)) != PLUS
5827 || GET_CODE (XEXP (XEXP (op, 0), 0)) != SYMBOL_REF
5828 || GET_CODE (XEXP (XEXP (op, 0), 1)) != CONST_INT)
5829 return 0;
5831 else
5833 rtx sum = XEXP (op, 0);
5834 HOST_WIDE_INT summand;
5836 /* We have to be careful here, because it is the referenced address
5837 that must be 32k from _SDA_BASE_, not just the symbol. */
5838 summand = INTVAL (XEXP (sum, 1));
5839 if (summand < 0 || summand > g_switch_value)
5840 return 0;
5842 sym_ref = XEXP (sum, 0);
5845 return SYMBOL_REF_SMALL_P (sym_ref);
5846 #else
5847 return 0;
5848 #endif
5851 /* Return true if either operand is a general purpose register. */
5853 bool
5854 gpr_or_gpr_p (rtx op0, rtx op1)
5856 return ((REG_P (op0) && INT_REGNO_P (REGNO (op0)))
5857 || (REG_P (op1) && INT_REGNO_P (REGNO (op1))));
5860 /* Return true if this is a move direct operation between GPR registers and
5861 floating point/VSX registers. */
5863 bool
5864 direct_move_p (rtx op0, rtx op1)
5866 int regno0, regno1;
5868 if (!REG_P (op0) || !REG_P (op1))
5869 return false;
5871 if (!TARGET_DIRECT_MOVE && !TARGET_MFPGPR)
5872 return false;
5874 regno0 = REGNO (op0);
5875 regno1 = REGNO (op1);
5876 if (regno0 >= FIRST_PSEUDO_REGISTER || regno1 >= FIRST_PSEUDO_REGISTER)
5877 return false;
5879 if (INT_REGNO_P (regno0))
5880 return (TARGET_DIRECT_MOVE) ? VSX_REGNO_P (regno1) : FP_REGNO_P (regno1);
5882 else if (INT_REGNO_P (regno1))
5884 if (TARGET_MFPGPR && FP_REGNO_P (regno0))
5885 return true;
5887 else if (TARGET_DIRECT_MOVE && VSX_REGNO_P (regno0))
5888 return true;
5891 return false;
5894 /* Return true if this is a load or store quad operation. */
5896 bool
5897 quad_load_store_p (rtx op0, rtx op1)
5899 bool ret;
5901 if (!TARGET_QUAD_MEMORY)
5902 ret = false;
5904 else if (REG_P (op0) && MEM_P (op1))
5905 ret = (quad_int_reg_operand (op0, GET_MODE (op0))
5906 && quad_memory_operand (op1, GET_MODE (op1))
5907 && !reg_overlap_mentioned_p (op0, op1));
5909 else if (MEM_P (op0) && REG_P (op1))
5910 ret = (quad_memory_operand (op0, GET_MODE (op0))
5911 && quad_int_reg_operand (op1, GET_MODE (op1)));
5913 else
5914 ret = false;
5916 if (TARGET_DEBUG_ADDR)
5918 fprintf (stderr, "\n========== quad_load_store, return %s\n",
5919 ret ? "true" : "false");
5920 debug_rtx (gen_rtx_SET (VOIDmode, op0, op1));
5923 return ret;
5926 /* Given an address, return a constant offset term if one exists. */
5928 static rtx
5929 address_offset (rtx op)
5931 if (GET_CODE (op) == PRE_INC
5932 || GET_CODE (op) == PRE_DEC)
5933 op = XEXP (op, 0);
5934 else if (GET_CODE (op) == PRE_MODIFY
5935 || GET_CODE (op) == LO_SUM)
5936 op = XEXP (op, 1);
5938 if (GET_CODE (op) == CONST)
5939 op = XEXP (op, 0);
5941 if (GET_CODE (op) == PLUS)
5942 op = XEXP (op, 1);
5944 if (CONST_INT_P (op))
5945 return op;
5947 return NULL_RTX;
5950 /* Return true if the MEM operand is a memory operand suitable for use
5951 with a (full width, possibly multiple) gpr load/store. On
5952 powerpc64 this means the offset must be divisible by 4.
5953 Implements 'Y' constraint.
5955 Accept direct, indexed, offset, lo_sum and tocref. Since this is
5956 a constraint function we know the operand has satisfied a suitable
5957 memory predicate. Also accept some odd rtl generated by reload
5958 (see rs6000_legitimize_reload_address for various forms). It is
5959 important that reload rtl be accepted by appropriate constraints
5960 but not by the operand predicate.
5962 Offsetting a lo_sum should not be allowed, except where we know by
5963 alignment that a 32k boundary is not crossed, but see the ???
5964 comment in rs6000_legitimize_reload_address. Note that by
5965 "offsetting" here we mean a further offset to access parts of the
5966 MEM. It's fine to have a lo_sum where the inner address is offset
5967 from a sym, since the same sym+offset will appear in the high part
5968 of the address calculation. */
5970 bool
5971 mem_operand_gpr (rtx op, enum machine_mode mode)
5973 unsigned HOST_WIDE_INT offset;
5974 int extra;
5975 rtx addr = XEXP (op, 0);
5977 op = address_offset (addr);
5978 if (op == NULL_RTX)
5979 return true;
5981 offset = INTVAL (op);
5982 if (TARGET_POWERPC64 && (offset & 3) != 0)
5983 return false;
5985 extra = GET_MODE_SIZE (mode) - UNITS_PER_WORD;
5986 gcc_assert (extra >= 0);
5988 if (GET_CODE (addr) == LO_SUM)
5989 /* For lo_sum addresses, we must allow any offset except one that
5990 causes a wrap, so test only the low 16 bits. */
5991 offset = ((offset & 0xffff) ^ 0x8000) - 0x8000;
5993 return offset + 0x8000 < 0x10000u - extra;
5996 /* Subroutines of rs6000_legitimize_address and rs6000_legitimate_address_p. */
5998 static bool
5999 reg_offset_addressing_ok_p (enum machine_mode mode)
6001 switch (mode)
6003 case V16QImode:
6004 case V8HImode:
6005 case V4SFmode:
6006 case V4SImode:
6007 case V2DFmode:
6008 case V2DImode:
6009 case TImode:
6010 /* AltiVec/VSX vector modes. Only reg+reg addressing is valid. While
6011 TImode is not a vector mode, if we want to use the VSX registers to
6012 move it around, we need to restrict ourselves to reg+reg
6013 addressing. */
6014 if (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode))
6015 return false;
6016 break;
6018 case V4HImode:
6019 case V2SImode:
6020 case V1DImode:
6021 case V2SFmode:
6022 /* Paired vector modes. Only reg+reg addressing is valid. */
6023 if (TARGET_PAIRED_FLOAT)
6024 return false;
6025 break;
6027 case SDmode:
6028 /* If we can do direct load/stores of SDmode, restrict it to reg+reg
6029 addressing for the LFIWZX and STFIWX instructions. */
6030 if (TARGET_NO_SDMODE_STACK)
6031 return false;
6032 break;
6034 default:
6035 break;
6038 return true;
6041 static bool
6042 virtual_stack_registers_memory_p (rtx op)
6044 int regnum;
6046 if (GET_CODE (op) == REG)
6047 regnum = REGNO (op);
6049 else if (GET_CODE (op) == PLUS
6050 && GET_CODE (XEXP (op, 0)) == REG
6051 && GET_CODE (XEXP (op, 1)) == CONST_INT)
6052 regnum = REGNO (XEXP (op, 0));
6054 else
6055 return false;
6057 return (regnum >= FIRST_VIRTUAL_REGISTER
6058 && regnum <= LAST_VIRTUAL_POINTER_REGISTER);
6061 /* Return true if a MODE sized memory accesses to OP plus OFFSET
6062 is known to not straddle a 32k boundary. */
6064 static bool
6065 offsettable_ok_by_alignment (rtx op, HOST_WIDE_INT offset,
6066 enum machine_mode mode)
6068 tree decl, type;
6069 unsigned HOST_WIDE_INT dsize, dalign, lsb, mask;
6071 if (GET_CODE (op) != SYMBOL_REF)
6072 return false;
6074 dsize = GET_MODE_SIZE (mode);
6075 decl = SYMBOL_REF_DECL (op);
6076 if (!decl)
6078 if (dsize == 0)
6079 return false;
6081 /* -fsection-anchors loses the original SYMBOL_REF_DECL when
6082 replacing memory addresses with an anchor plus offset. We
6083 could find the decl by rummaging around in the block->objects
6084 VEC for the given offset but that seems like too much work. */
6085 dalign = BITS_PER_UNIT;
6086 if (SYMBOL_REF_HAS_BLOCK_INFO_P (op)
6087 && SYMBOL_REF_ANCHOR_P (op)
6088 && SYMBOL_REF_BLOCK (op) != NULL)
6090 struct object_block *block = SYMBOL_REF_BLOCK (op);
6092 dalign = block->alignment;
6093 offset += SYMBOL_REF_BLOCK_OFFSET (op);
6095 else if (CONSTANT_POOL_ADDRESS_P (op))
6097 /* It would be nice to have get_pool_align().. */
6098 enum machine_mode cmode = get_pool_mode (op);
6100 dalign = GET_MODE_ALIGNMENT (cmode);
6103 else if (DECL_P (decl))
6105 dalign = DECL_ALIGN (decl);
6107 if (dsize == 0)
6109 /* Allow BLKmode when the entire object is known to not
6110 cross a 32k boundary. */
6111 if (!DECL_SIZE_UNIT (decl))
6112 return false;
6114 if (!host_integerp (DECL_SIZE_UNIT (decl), 1))
6115 return false;
6117 dsize = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
6118 if (dsize > 32768)
6119 return false;
6121 return dalign / BITS_PER_UNIT >= dsize;
6124 else
6126 type = TREE_TYPE (decl);
6128 dalign = TYPE_ALIGN (type);
6129 if (CONSTANT_CLASS_P (decl))
6130 dalign = CONSTANT_ALIGNMENT (decl, dalign);
6131 else
6132 dalign = DATA_ALIGNMENT (decl, dalign);
6134 if (dsize == 0)
6136 /* BLKmode, check the entire object. */
6137 if (TREE_CODE (decl) == STRING_CST)
6138 dsize = TREE_STRING_LENGTH (decl);
6139 else if (TYPE_SIZE_UNIT (type)
6140 && host_integerp (TYPE_SIZE_UNIT (type), 1))
6141 dsize = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
6142 else
6143 return false;
6144 if (dsize > 32768)
6145 return false;
6147 return dalign / BITS_PER_UNIT >= dsize;
6151 /* Find how many bits of the alignment we know for this access. */
6152 mask = dalign / BITS_PER_UNIT - 1;
6153 lsb = offset & -offset;
6154 mask &= lsb - 1;
6155 dalign = mask + 1;
6157 return dalign >= dsize;
6160 static bool
6161 constant_pool_expr_p (rtx op)
6163 rtx base, offset;
6165 split_const (op, &base, &offset);
6166 return (GET_CODE (base) == SYMBOL_REF
6167 && CONSTANT_POOL_ADDRESS_P (base)
6168 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (base), Pmode));
6171 static const_rtx tocrel_base, tocrel_offset;
6173 /* Return true if OP is a toc pointer relative address (the output
6174 of create_TOC_reference). If STRICT, do not match high part or
6175 non-split -mcmodel=large/medium toc pointer relative addresses. */
6177 bool
6178 toc_relative_expr_p (const_rtx op, bool strict)
6180 if (!TARGET_TOC)
6181 return false;
6183 if (TARGET_CMODEL != CMODEL_SMALL)
6185 /* Only match the low part. */
6186 if (GET_CODE (op) == LO_SUM
6187 && REG_P (XEXP (op, 0))
6188 && INT_REG_OK_FOR_BASE_P (XEXP (op, 0), strict))
6189 op = XEXP (op, 1);
6190 else if (strict)
6191 return false;
6194 tocrel_base = op;
6195 tocrel_offset = const0_rtx;
6196 if (GET_CODE (op) == PLUS && add_cint_operand (XEXP (op, 1), GET_MODE (op)))
6198 tocrel_base = XEXP (op, 0);
6199 tocrel_offset = XEXP (op, 1);
6202 return (GET_CODE (tocrel_base) == UNSPEC
6203 && XINT (tocrel_base, 1) == UNSPEC_TOCREL);
6206 /* Return true if X is a constant pool address, and also for cmodel=medium
6207 if X is a toc-relative address known to be offsettable within MODE. */
6209 bool
6210 legitimate_constant_pool_address_p (const_rtx x, enum machine_mode mode,
6211 bool strict)
6213 return (toc_relative_expr_p (x, strict)
6214 && (TARGET_CMODEL != CMODEL_MEDIUM
6215 || constant_pool_expr_p (XVECEXP (tocrel_base, 0, 0))
6216 || mode == QImode
6217 || offsettable_ok_by_alignment (XVECEXP (tocrel_base, 0, 0),
6218 INTVAL (tocrel_offset), mode)));
6221 static bool
6222 legitimate_small_data_p (enum machine_mode mode, rtx x)
6224 return (DEFAULT_ABI == ABI_V4
6225 && !flag_pic && !TARGET_TOC
6226 && (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST)
6227 && small_data_operand (x, mode));
6230 /* SPE offset addressing is limited to 5-bits worth of double words. */
6231 #define SPE_CONST_OFFSET_OK(x) (((x) & ~0xf8) == 0)
6233 bool
6234 rs6000_legitimate_offset_address_p (enum machine_mode mode, rtx x,
6235 bool strict, bool worst_case)
6237 unsigned HOST_WIDE_INT offset;
6238 unsigned int extra;
6240 if (GET_CODE (x) != PLUS)
6241 return false;
6242 if (!REG_P (XEXP (x, 0)))
6243 return false;
6244 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
6245 return false;
6246 if (!reg_offset_addressing_ok_p (mode))
6247 return virtual_stack_registers_memory_p (x);
6248 if (legitimate_constant_pool_address_p (x, mode, strict || lra_in_progress))
6249 return true;
6250 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6251 return false;
6253 offset = INTVAL (XEXP (x, 1));
6254 extra = 0;
6255 switch (mode)
6257 case V4HImode:
6258 case V2SImode:
6259 case V1DImode:
6260 case V2SFmode:
6261 /* SPE vector modes. */
6262 return SPE_CONST_OFFSET_OK (offset);
6264 case DFmode:
6265 case DDmode:
6266 case DImode:
6267 /* On e500v2, we may have:
6269 (subreg:DF (mem:DI (plus (reg) (const_int))) 0).
6271 Which gets addressed with evldd instructions. */
6272 if (TARGET_E500_DOUBLE)
6273 return SPE_CONST_OFFSET_OK (offset);
6275 /* If we are using VSX scalar loads, restrict ourselves to reg+reg
6276 addressing. */
6277 if (VECTOR_MEM_VSX_P (mode))
6278 return false;
6280 if (!worst_case)
6281 break;
6282 if (!TARGET_POWERPC64)
6283 extra = 4;
6284 else if (offset & 3)
6285 return false;
6286 break;
6288 case TFmode:
6289 case TDmode:
6290 case TImode:
6291 case PTImode:
6292 if (TARGET_E500_DOUBLE)
6293 return (SPE_CONST_OFFSET_OK (offset)
6294 && SPE_CONST_OFFSET_OK (offset + 8));
6296 extra = 8;
6297 if (!worst_case)
6298 break;
6299 if (!TARGET_POWERPC64)
6300 extra = 12;
6301 else if (offset & 3)
6302 return false;
6303 break;
6305 default:
6306 break;
6309 offset += 0x8000;
6310 return offset < 0x10000 - extra;
6313 bool
6314 legitimate_indexed_address_p (rtx x, int strict)
6316 rtx op0, op1;
6318 if (GET_CODE (x) != PLUS)
6319 return false;
6321 op0 = XEXP (x, 0);
6322 op1 = XEXP (x, 1);
6324 /* Recognize the rtl generated by reload which we know will later be
6325 replaced with proper base and index regs. */
6326 if (!strict
6327 && reload_in_progress
6328 && (REG_P (op0) || GET_CODE (op0) == PLUS)
6329 && REG_P (op1))
6330 return true;
6332 return (REG_P (op0) && REG_P (op1)
6333 && ((INT_REG_OK_FOR_BASE_P (op0, strict)
6334 && INT_REG_OK_FOR_INDEX_P (op1, strict))
6335 || (INT_REG_OK_FOR_BASE_P (op1, strict)
6336 && INT_REG_OK_FOR_INDEX_P (op0, strict))));
6339 bool
6340 avoiding_indexed_address_p (enum machine_mode mode)
6342 /* Avoid indexed addressing for modes that have non-indexed
6343 load/store instruction forms. */
6344 return (TARGET_AVOID_XFORM && VECTOR_MEM_NONE_P (mode));
6347 bool
6348 legitimate_indirect_address_p (rtx x, int strict)
6350 return GET_CODE (x) == REG && INT_REG_OK_FOR_BASE_P (x, strict);
6353 bool
6354 macho_lo_sum_memory_operand (rtx x, enum machine_mode mode)
6356 if (!TARGET_MACHO || !flag_pic
6357 || mode != SImode || GET_CODE (x) != MEM)
6358 return false;
6359 x = XEXP (x, 0);
6361 if (GET_CODE (x) != LO_SUM)
6362 return false;
6363 if (GET_CODE (XEXP (x, 0)) != REG)
6364 return false;
6365 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), 0))
6366 return false;
6367 x = XEXP (x, 1);
6369 return CONSTANT_P (x);
6372 static bool
6373 legitimate_lo_sum_address_p (enum machine_mode mode, rtx x, int strict)
6375 if (GET_CODE (x) != LO_SUM)
6376 return false;
6377 if (GET_CODE (XEXP (x, 0)) != REG)
6378 return false;
6379 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
6380 return false;
6381 /* Restrict addressing for DI because of our SUBREG hackery. */
6382 if (TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
6383 return false;
6384 x = XEXP (x, 1);
6386 if (TARGET_ELF || TARGET_MACHO)
6388 bool large_toc_ok;
6390 if (DEFAULT_ABI != ABI_AIX && DEFAULT_ABI != ABI_DARWIN && flag_pic)
6391 return false;
6392 /* LRA don't use LEGITIMIZE_RELOAD_ADDRESS as it usually calls
6393 push_reload from reload pass code. LEGITIMIZE_RELOAD_ADDRESS
6394 recognizes some LO_SUM addresses as valid although this
6395 function says opposite. In most cases, LRA through different
6396 transformations can generate correct code for address reloads.
6397 It can not manage only some LO_SUM cases. So we need to add
6398 code analogous to one in rs6000_legitimize_reload_address for
6399 LOW_SUM here saying that some addresses are still valid. */
6400 large_toc_ok = (lra_in_progress && TARGET_CMODEL != CMODEL_SMALL
6401 && small_toc_ref (x, VOIDmode));
6402 if (TARGET_TOC && ! large_toc_ok)
6403 return false;
6404 if (GET_MODE_NUNITS (mode) != 1)
6405 return false;
6406 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
6407 && !(/* ??? Assume floating point reg based on mode? */
6408 TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT
6409 && (mode == DFmode || mode == DDmode)))
6410 return false;
6412 return CONSTANT_P (x) || large_toc_ok;
6415 return false;
6419 /* Try machine-dependent ways of modifying an illegitimate address
6420 to be legitimate. If we find one, return the new, valid address.
6421 This is used from only one place: `memory_address' in explow.c.
6423 OLDX is the address as it was before break_out_memory_refs was
6424 called. In some cases it is useful to look at this to decide what
6425 needs to be done.
6427 It is always safe for this function to do nothing. It exists to
6428 recognize opportunities to optimize the output.
6430 On RS/6000, first check for the sum of a register with a constant
6431 integer that is out of range. If so, generate code to add the
6432 constant with the low-order 16 bits masked to the register and force
6433 this result into another register (this can be done with `cau').
6434 Then generate an address of REG+(CONST&0xffff), allowing for the
6435 possibility of bit 16 being a one.
6437 Then check for the sum of a register and something not constant, try to
6438 load the other things into a register and return the sum. */
6440 static rtx
6441 rs6000_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
6442 enum machine_mode mode)
6444 unsigned int extra;
6446 if (!reg_offset_addressing_ok_p (mode))
6448 if (virtual_stack_registers_memory_p (x))
6449 return x;
6451 /* In theory we should not be seeing addresses of the form reg+0,
6452 but just in case it is generated, optimize it away. */
6453 if (GET_CODE (x) == PLUS && XEXP (x, 1) == const0_rtx)
6454 return force_reg (Pmode, XEXP (x, 0));
6456 /* For TImode with load/store quad, restrict addresses to just a single
6457 pointer, so it works with both GPRs and VSX registers. */
6458 /* Make sure both operands are registers. */
6459 else if (GET_CODE (x) == PLUS
6460 && (mode != TImode || !TARGET_QUAD_MEMORY))
6461 return gen_rtx_PLUS (Pmode,
6462 force_reg (Pmode, XEXP (x, 0)),
6463 force_reg (Pmode, XEXP (x, 1)));
6464 else
6465 return force_reg (Pmode, x);
6467 if (GET_CODE (x) == SYMBOL_REF)
6469 enum tls_model model = SYMBOL_REF_TLS_MODEL (x);
6470 if (model != 0)
6471 return rs6000_legitimize_tls_address (x, model);
6474 extra = 0;
6475 switch (mode)
6477 case TFmode:
6478 case TDmode:
6479 case TImode:
6480 case PTImode:
6481 /* As in legitimate_offset_address_p we do not assume
6482 worst-case. The mode here is just a hint as to the registers
6483 used. A TImode is usually in gprs, but may actually be in
6484 fprs. Leave worst-case scenario for reload to handle via
6485 insn constraints. PTImode is only GPRs. */
6486 extra = 8;
6487 break;
6488 default:
6489 break;
6492 if (GET_CODE (x) == PLUS
6493 && GET_CODE (XEXP (x, 0)) == REG
6494 && GET_CODE (XEXP (x, 1)) == CONST_INT
6495 && ((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 1)) + 0x8000)
6496 >= 0x10000 - extra)
6497 && !(SPE_VECTOR_MODE (mode)
6498 || (TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD)))
6500 HOST_WIDE_INT high_int, low_int;
6501 rtx sum;
6502 low_int = ((INTVAL (XEXP (x, 1)) & 0xffff) ^ 0x8000) - 0x8000;
6503 if (low_int >= 0x8000 - extra)
6504 low_int = 0;
6505 high_int = INTVAL (XEXP (x, 1)) - low_int;
6506 sum = force_operand (gen_rtx_PLUS (Pmode, XEXP (x, 0),
6507 GEN_INT (high_int)), 0);
6508 return plus_constant (Pmode, sum, low_int);
6510 else if (GET_CODE (x) == PLUS
6511 && GET_CODE (XEXP (x, 0)) == REG
6512 && GET_CODE (XEXP (x, 1)) != CONST_INT
6513 && GET_MODE_NUNITS (mode) == 1
6514 && (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
6515 || (/* ??? Assume floating point reg based on mode? */
6516 (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
6517 && (mode == DFmode || mode == DDmode)))
6518 && !avoiding_indexed_address_p (mode))
6520 return gen_rtx_PLUS (Pmode, XEXP (x, 0),
6521 force_reg (Pmode, force_operand (XEXP (x, 1), 0)));
6523 else if (SPE_VECTOR_MODE (mode)
6524 || (TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD))
6526 if (mode == DImode)
6527 return x;
6528 /* We accept [reg + reg] and [reg + OFFSET]. */
6530 if (GET_CODE (x) == PLUS)
6532 rtx op1 = XEXP (x, 0);
6533 rtx op2 = XEXP (x, 1);
6534 rtx y;
6536 op1 = force_reg (Pmode, op1);
6538 if (GET_CODE (op2) != REG
6539 && (GET_CODE (op2) != CONST_INT
6540 || !SPE_CONST_OFFSET_OK (INTVAL (op2))
6541 || (GET_MODE_SIZE (mode) > 8
6542 && !SPE_CONST_OFFSET_OK (INTVAL (op2) + 8))))
6543 op2 = force_reg (Pmode, op2);
6545 /* We can't always do [reg + reg] for these, because [reg +
6546 reg + offset] is not a legitimate addressing mode. */
6547 y = gen_rtx_PLUS (Pmode, op1, op2);
6549 if ((GET_MODE_SIZE (mode) > 8 || mode == DDmode) && REG_P (op2))
6550 return force_reg (Pmode, y);
6551 else
6552 return y;
6555 return force_reg (Pmode, x);
6557 else if ((TARGET_ELF
6558 #if TARGET_MACHO
6559 || !MACHO_DYNAMIC_NO_PIC_P
6560 #endif
6562 && TARGET_32BIT
6563 && TARGET_NO_TOC
6564 && ! flag_pic
6565 && GET_CODE (x) != CONST_INT
6566 && GET_CODE (x) != CONST_DOUBLE
6567 && CONSTANT_P (x)
6568 && GET_MODE_NUNITS (mode) == 1
6569 && (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
6570 || (/* ??? Assume floating point reg based on mode? */
6571 (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
6572 && (mode == DFmode || mode == DDmode))))
6574 rtx reg = gen_reg_rtx (Pmode);
6575 if (TARGET_ELF)
6576 emit_insn (gen_elf_high (reg, x));
6577 else
6578 emit_insn (gen_macho_high (reg, x));
6579 return gen_rtx_LO_SUM (Pmode, reg, x);
6581 else if (TARGET_TOC
6582 && GET_CODE (x) == SYMBOL_REF
6583 && constant_pool_expr_p (x)
6584 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (x), Pmode))
6585 return create_TOC_reference (x, NULL_RTX);
6586 else
6587 return x;
6590 /* Debug version of rs6000_legitimize_address. */
6591 static rtx
6592 rs6000_debug_legitimize_address (rtx x, rtx oldx, enum machine_mode mode)
6594 rtx ret;
6595 rtx insns;
6597 start_sequence ();
6598 ret = rs6000_legitimize_address (x, oldx, mode);
6599 insns = get_insns ();
6600 end_sequence ();
6602 if (ret != x)
6604 fprintf (stderr,
6605 "\nrs6000_legitimize_address: mode %s, old code %s, "
6606 "new code %s, modified\n",
6607 GET_MODE_NAME (mode), GET_RTX_NAME (GET_CODE (x)),
6608 GET_RTX_NAME (GET_CODE (ret)));
6610 fprintf (stderr, "Original address:\n");
6611 debug_rtx (x);
6613 fprintf (stderr, "oldx:\n");
6614 debug_rtx (oldx);
6616 fprintf (stderr, "New address:\n");
6617 debug_rtx (ret);
6619 if (insns)
6621 fprintf (stderr, "Insns added:\n");
6622 debug_rtx_list (insns, 20);
6625 else
6627 fprintf (stderr,
6628 "\nrs6000_legitimize_address: mode %s, code %s, no change:\n",
6629 GET_MODE_NAME (mode), GET_RTX_NAME (GET_CODE (x)));
6631 debug_rtx (x);
6634 if (insns)
6635 emit_insn (insns);
6637 return ret;
6640 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
6641 We need to emit DTP-relative relocations. */
6643 static void rs6000_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
6644 static void
6645 rs6000_output_dwarf_dtprel (FILE *file, int size, rtx x)
6647 switch (size)
6649 case 4:
6650 fputs ("\t.long\t", file);
6651 break;
6652 case 8:
6653 fputs (DOUBLE_INT_ASM_OP, file);
6654 break;
6655 default:
6656 gcc_unreachable ();
6658 output_addr_const (file, x);
6659 fputs ("@dtprel+0x8000", file);
6662 /* In the name of slightly smaller debug output, and to cater to
6663 general assembler lossage, recognize various UNSPEC sequences
6664 and turn them back into a direct symbol reference. */
6666 static rtx
6667 rs6000_delegitimize_address (rtx orig_x)
6669 rtx x, y, offset;
6671 orig_x = delegitimize_mem_from_attrs (orig_x);
6672 x = orig_x;
6673 if (MEM_P (x))
6674 x = XEXP (x, 0);
6676 y = x;
6677 if (TARGET_CMODEL != CMODEL_SMALL
6678 && GET_CODE (y) == LO_SUM)
6679 y = XEXP (y, 1);
6681 offset = NULL_RTX;
6682 if (GET_CODE (y) == PLUS
6683 && GET_MODE (y) == Pmode
6684 && CONST_INT_P (XEXP (y, 1)))
6686 offset = XEXP (y, 1);
6687 y = XEXP (y, 0);
6690 if (GET_CODE (y) == UNSPEC
6691 && XINT (y, 1) == UNSPEC_TOCREL)
6693 #ifdef ENABLE_CHECKING
6694 if (REG_P (XVECEXP (y, 0, 1))
6695 && REGNO (XVECEXP (y, 0, 1)) == TOC_REGISTER)
6697 /* All good. */
6699 else if (GET_CODE (XVECEXP (y, 0, 1)) == DEBUG_EXPR)
6701 /* Weirdness alert. df_note_compute can replace r2 with a
6702 debug_expr when this unspec is in a debug_insn.
6703 Seen in gcc.dg/pr51957-1.c */
6705 else
6707 debug_rtx (orig_x);
6708 abort ();
6710 #endif
6711 y = XVECEXP (y, 0, 0);
6713 #ifdef HAVE_AS_TLS
6714 /* Do not associate thread-local symbols with the original
6715 constant pool symbol. */
6716 if (TARGET_XCOFF
6717 && GET_CODE (y) == SYMBOL_REF
6718 && CONSTANT_POOL_ADDRESS_P (y)
6719 && SYMBOL_REF_TLS_MODEL (get_pool_constant (y)) >= TLS_MODEL_REAL)
6720 return orig_x;
6721 #endif
6723 if (offset != NULL_RTX)
6724 y = gen_rtx_PLUS (Pmode, y, offset);
6725 if (!MEM_P (orig_x))
6726 return y;
6727 else
6728 return replace_equiv_address_nv (orig_x, y);
6731 if (TARGET_MACHO
6732 && GET_CODE (orig_x) == LO_SUM
6733 && GET_CODE (XEXP (orig_x, 1)) == CONST)
6735 y = XEXP (XEXP (orig_x, 1), 0);
6736 if (GET_CODE (y) == UNSPEC
6737 && XINT (y, 1) == UNSPEC_MACHOPIC_OFFSET)
6738 return XVECEXP (y, 0, 0);
6741 return orig_x;
6744 /* Return true if X shouldn't be emitted into the debug info.
6745 The linker doesn't like .toc section references from
6746 .debug_* sections, so reject .toc section symbols. */
6748 static bool
6749 rs6000_const_not_ok_for_debug_p (rtx x)
6751 if (GET_CODE (x) == SYMBOL_REF
6752 && CONSTANT_POOL_ADDRESS_P (x))
6754 rtx c = get_pool_constant (x);
6755 enum machine_mode cmode = get_pool_mode (x);
6756 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (c, cmode))
6757 return true;
6760 return false;
6763 /* Construct the SYMBOL_REF for the tls_get_addr function. */
6765 static GTY(()) rtx rs6000_tls_symbol;
6766 static rtx
6767 rs6000_tls_get_addr (void)
6769 if (!rs6000_tls_symbol)
6770 rs6000_tls_symbol = init_one_libfunc ("__tls_get_addr");
6772 return rs6000_tls_symbol;
6775 /* Construct the SYMBOL_REF for TLS GOT references. */
6777 static GTY(()) rtx rs6000_got_symbol;
6778 static rtx
6779 rs6000_got_sym (void)
6781 if (!rs6000_got_symbol)
6783 rs6000_got_symbol = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
6784 SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_LOCAL;
6785 SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_EXTERNAL;
6788 return rs6000_got_symbol;
6791 /* AIX Thread-Local Address support. */
6793 static rtx
6794 rs6000_legitimize_tls_address_aix (rtx addr, enum tls_model model)
6796 rtx sym, mem, tocref, tlsreg, tmpreg, dest, tlsaddr;
6797 const char *name;
6798 char *tlsname;
6800 name = XSTR (addr, 0);
6801 /* Append TLS CSECT qualifier, unless the symbol already is qualified
6802 or the symbol will be in TLS private data section. */
6803 if (name[strlen (name) - 1] != ']'
6804 && (TREE_PUBLIC (SYMBOL_REF_DECL (addr))
6805 || bss_initializer_p (SYMBOL_REF_DECL (addr))))
6807 tlsname = XALLOCAVEC (char, strlen (name) + 4);
6808 strcpy (tlsname, name);
6809 strcat (tlsname,
6810 bss_initializer_p (SYMBOL_REF_DECL (addr)) ? "[UL]" : "[TL]");
6811 tlsaddr = copy_rtx (addr);
6812 XSTR (tlsaddr, 0) = ggc_strdup (tlsname);
6814 else
6815 tlsaddr = addr;
6817 /* Place addr into TOC constant pool. */
6818 sym = force_const_mem (GET_MODE (tlsaddr), tlsaddr);
6820 /* Output the TOC entry and create the MEM referencing the value. */
6821 if (constant_pool_expr_p (XEXP (sym, 0))
6822 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (XEXP (sym, 0)), Pmode))
6824 tocref = create_TOC_reference (XEXP (sym, 0), NULL_RTX);
6825 mem = gen_const_mem (Pmode, tocref);
6826 set_mem_alias_set (mem, get_TOC_alias_set ());
6828 else
6829 return sym;
6831 /* Use global-dynamic for local-dynamic. */
6832 if (model == TLS_MODEL_GLOBAL_DYNAMIC
6833 || model == TLS_MODEL_LOCAL_DYNAMIC)
6835 /* Create new TOC reference for @m symbol. */
6836 name = XSTR (XVECEXP (XEXP (mem, 0), 0, 0), 0);
6837 tlsname = XALLOCAVEC (char, strlen (name) + 1);
6838 strcpy (tlsname, "*LCM");
6839 strcat (tlsname, name + 3);
6840 rtx modaddr = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (tlsname));
6841 SYMBOL_REF_FLAGS (modaddr) |= SYMBOL_FLAG_LOCAL;
6842 tocref = create_TOC_reference (modaddr, NULL_RTX);
6843 rtx modmem = gen_const_mem (Pmode, tocref);
6844 set_mem_alias_set (modmem, get_TOC_alias_set ());
6846 rtx modreg = gen_reg_rtx (Pmode);
6847 emit_insn (gen_rtx_SET (VOIDmode, modreg, modmem));
6849 tmpreg = gen_reg_rtx (Pmode);
6850 emit_insn (gen_rtx_SET (VOIDmode, tmpreg, mem));
6852 dest = gen_reg_rtx (Pmode);
6853 if (TARGET_32BIT)
6854 emit_insn (gen_tls_get_addrsi (dest, modreg, tmpreg));
6855 else
6856 emit_insn (gen_tls_get_addrdi (dest, modreg, tmpreg));
6857 return dest;
6859 /* Obtain TLS pointer: 32 bit call or 64 bit GPR 13. */
6860 else if (TARGET_32BIT)
6862 tlsreg = gen_reg_rtx (SImode);
6863 emit_insn (gen_tls_get_tpointer (tlsreg));
6865 else
6866 tlsreg = gen_rtx_REG (DImode, 13);
6868 /* Load the TOC value into temporary register. */
6869 tmpreg = gen_reg_rtx (Pmode);
6870 emit_insn (gen_rtx_SET (VOIDmode, tmpreg, mem));
6871 set_unique_reg_note (get_last_insn (), REG_EQUAL,
6872 gen_rtx_MINUS (Pmode, addr, tlsreg));
6874 /* Add TOC symbol value to TLS pointer. */
6875 dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, tmpreg, tlsreg));
6877 return dest;
6880 /* ADDR contains a thread-local SYMBOL_REF. Generate code to compute
6881 this (thread-local) address. */
6883 static rtx
6884 rs6000_legitimize_tls_address (rtx addr, enum tls_model model)
6886 rtx dest, insn;
6888 if (TARGET_XCOFF)
6889 return rs6000_legitimize_tls_address_aix (addr, model);
6891 dest = gen_reg_rtx (Pmode);
6892 if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 16)
6894 rtx tlsreg;
6896 if (TARGET_64BIT)
6898 tlsreg = gen_rtx_REG (Pmode, 13);
6899 insn = gen_tls_tprel_64 (dest, tlsreg, addr);
6901 else
6903 tlsreg = gen_rtx_REG (Pmode, 2);
6904 insn = gen_tls_tprel_32 (dest, tlsreg, addr);
6906 emit_insn (insn);
6908 else if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 32)
6910 rtx tlsreg, tmp;
6912 tmp = gen_reg_rtx (Pmode);
6913 if (TARGET_64BIT)
6915 tlsreg = gen_rtx_REG (Pmode, 13);
6916 insn = gen_tls_tprel_ha_64 (tmp, tlsreg, addr);
6918 else
6920 tlsreg = gen_rtx_REG (Pmode, 2);
6921 insn = gen_tls_tprel_ha_32 (tmp, tlsreg, addr);
6923 emit_insn (insn);
6924 if (TARGET_64BIT)
6925 insn = gen_tls_tprel_lo_64 (dest, tmp, addr);
6926 else
6927 insn = gen_tls_tprel_lo_32 (dest, tmp, addr);
6928 emit_insn (insn);
6930 else
6932 rtx r3, got, tga, tmp1, tmp2, call_insn;
6934 /* We currently use relocations like @got@tlsgd for tls, which
6935 means the linker will handle allocation of tls entries, placing
6936 them in the .got section. So use a pointer to the .got section,
6937 not one to secondary TOC sections used by 64-bit -mminimal-toc,
6938 or to secondary GOT sections used by 32-bit -fPIC. */
6939 if (TARGET_64BIT)
6940 got = gen_rtx_REG (Pmode, 2);
6941 else
6943 if (flag_pic == 1)
6944 got = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
6945 else
6947 rtx gsym = rs6000_got_sym ();
6948 got = gen_reg_rtx (Pmode);
6949 if (flag_pic == 0)
6950 rs6000_emit_move (got, gsym, Pmode);
6951 else
6953 rtx mem, lab, last;
6955 tmp1 = gen_reg_rtx (Pmode);
6956 tmp2 = gen_reg_rtx (Pmode);
6957 mem = gen_const_mem (Pmode, tmp1);
6958 lab = gen_label_rtx ();
6959 emit_insn (gen_load_toc_v4_PIC_1b (gsym, lab));
6960 emit_move_insn (tmp1, gen_rtx_REG (Pmode, LR_REGNO));
6961 if (TARGET_LINK_STACK)
6962 emit_insn (gen_addsi3 (tmp1, tmp1, GEN_INT (4)));
6963 emit_move_insn (tmp2, mem);
6964 last = emit_insn (gen_addsi3 (got, tmp1, tmp2));
6965 set_unique_reg_note (last, REG_EQUAL, gsym);
6970 if (model == TLS_MODEL_GLOBAL_DYNAMIC)
6972 tga = rs6000_tls_get_addr ();
6973 emit_library_call_value (tga, dest, LCT_CONST, Pmode,
6974 1, const0_rtx, Pmode);
6976 r3 = gen_rtx_REG (Pmode, 3);
6977 if (DEFAULT_ABI == ABI_AIX && TARGET_64BIT)
6978 insn = gen_tls_gd_aix64 (r3, got, addr, tga, const0_rtx);
6979 else if (DEFAULT_ABI == ABI_AIX && !TARGET_64BIT)
6980 insn = gen_tls_gd_aix32 (r3, got, addr, tga, const0_rtx);
6981 else if (DEFAULT_ABI == ABI_V4)
6982 insn = gen_tls_gd_sysvsi (r3, got, addr, tga, const0_rtx);
6983 else
6984 gcc_unreachable ();
6985 call_insn = last_call_insn ();
6986 PATTERN (call_insn) = insn;
6987 if (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
6988 use_reg (&CALL_INSN_FUNCTION_USAGE (call_insn),
6989 pic_offset_table_rtx);
6991 else if (model == TLS_MODEL_LOCAL_DYNAMIC)
6993 tga = rs6000_tls_get_addr ();
6994 tmp1 = gen_reg_rtx (Pmode);
6995 emit_library_call_value (tga, tmp1, LCT_CONST, Pmode,
6996 1, const0_rtx, Pmode);
6998 r3 = gen_rtx_REG (Pmode, 3);
6999 if (DEFAULT_ABI == ABI_AIX && TARGET_64BIT)
7000 insn = gen_tls_ld_aix64 (r3, got, tga, const0_rtx);
7001 else if (DEFAULT_ABI == ABI_AIX && !TARGET_64BIT)
7002 insn = gen_tls_ld_aix32 (r3, got, tga, const0_rtx);
7003 else if (DEFAULT_ABI == ABI_V4)
7004 insn = gen_tls_ld_sysvsi (r3, got, tga, const0_rtx);
7005 else
7006 gcc_unreachable ();
7007 call_insn = last_call_insn ();
7008 PATTERN (call_insn) = insn;
7009 if (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
7010 use_reg (&CALL_INSN_FUNCTION_USAGE (call_insn),
7011 pic_offset_table_rtx);
7013 if (rs6000_tls_size == 16)
7015 if (TARGET_64BIT)
7016 insn = gen_tls_dtprel_64 (dest, tmp1, addr);
7017 else
7018 insn = gen_tls_dtprel_32 (dest, tmp1, addr);
7020 else if (rs6000_tls_size == 32)
7022 tmp2 = gen_reg_rtx (Pmode);
7023 if (TARGET_64BIT)
7024 insn = gen_tls_dtprel_ha_64 (tmp2, tmp1, addr);
7025 else
7026 insn = gen_tls_dtprel_ha_32 (tmp2, tmp1, addr);
7027 emit_insn (insn);
7028 if (TARGET_64BIT)
7029 insn = gen_tls_dtprel_lo_64 (dest, tmp2, addr);
7030 else
7031 insn = gen_tls_dtprel_lo_32 (dest, tmp2, addr);
7033 else
7035 tmp2 = gen_reg_rtx (Pmode);
7036 if (TARGET_64BIT)
7037 insn = gen_tls_got_dtprel_64 (tmp2, got, addr);
7038 else
7039 insn = gen_tls_got_dtprel_32 (tmp2, got, addr);
7040 emit_insn (insn);
7041 insn = gen_rtx_SET (Pmode, dest,
7042 gen_rtx_PLUS (Pmode, tmp2, tmp1));
7044 emit_insn (insn);
7046 else
7048 /* IE, or 64-bit offset LE. */
7049 tmp2 = gen_reg_rtx (Pmode);
7050 if (TARGET_64BIT)
7051 insn = gen_tls_got_tprel_64 (tmp2, got, addr);
7052 else
7053 insn = gen_tls_got_tprel_32 (tmp2, got, addr);
7054 emit_insn (insn);
7055 if (TARGET_64BIT)
7056 insn = gen_tls_tls_64 (dest, tmp2, addr);
7057 else
7058 insn = gen_tls_tls_32 (dest, tmp2, addr);
7059 emit_insn (insn);
7063 return dest;
7066 /* Return 1 if X contains a thread-local symbol. */
7068 static bool
7069 rs6000_tls_referenced_p (rtx x)
7071 if (! TARGET_HAVE_TLS)
7072 return false;
7074 return for_each_rtx (&x, &rs6000_tls_symbol_ref_1, 0);
7077 /* Implement TARGET_CANNOT_FORCE_CONST_MEM. */
7079 static bool
7080 rs6000_cannot_force_const_mem (enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
7082 if (GET_CODE (x) == HIGH
7083 && GET_CODE (XEXP (x, 0)) == UNSPEC)
7084 return true;
7086 /* A TLS symbol in the TOC cannot contain a sum. */
7087 if (GET_CODE (x) == CONST
7088 && GET_CODE (XEXP (x, 0)) == PLUS
7089 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
7090 && SYMBOL_REF_TLS_MODEL (XEXP (XEXP (x, 0), 0)) != 0)
7091 return true;
7093 /* Do not place an ELF TLS symbol in the constant pool. */
7094 return TARGET_ELF && rs6000_tls_referenced_p (x);
7097 /* Return 1 if *X is a thread-local symbol. This is the same as
7098 rs6000_tls_symbol_ref except for the type of the unused argument. */
7100 static int
7101 rs6000_tls_symbol_ref_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
7103 return RS6000_SYMBOL_REF_TLS_P (*x);
7106 /* Return true iff the given SYMBOL_REF refers to a constant pool entry
7107 that we have put in the TOC, or for cmodel=medium, if the SYMBOL_REF
7108 can be addressed relative to the toc pointer. */
7110 static bool
7111 use_toc_relative_ref (rtx sym)
7113 return ((constant_pool_expr_p (sym)
7114 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (sym),
7115 get_pool_mode (sym)))
7116 || (TARGET_CMODEL == CMODEL_MEDIUM
7117 && SYMBOL_REF_LOCAL_P (sym)));
7120 /* Our implementation of LEGITIMIZE_RELOAD_ADDRESS. Returns a value to
7121 replace the input X, or the original X if no replacement is called for.
7122 The output parameter *WIN is 1 if the calling macro should goto WIN,
7123 0 if it should not.
7125 For RS/6000, we wish to handle large displacements off a base
7126 register by splitting the addend across an addiu/addis and the mem insn.
7127 This cuts number of extra insns needed from 3 to 1.
7129 On Darwin, we use this to generate code for floating point constants.
7130 A movsf_low is generated so we wind up with 2 instructions rather than 3.
7131 The Darwin code is inside #if TARGET_MACHO because only then are the
7132 machopic_* functions defined. */
7133 static rtx
7134 rs6000_legitimize_reload_address (rtx x, enum machine_mode mode,
7135 int opnum, int type,
7136 int ind_levels ATTRIBUTE_UNUSED, int *win)
7138 bool reg_offset_p = reg_offset_addressing_ok_p (mode);
7140 /* Nasty hack for vsx_splat_V2DF/V2DI load from mem, which takes a
7141 DFmode/DImode MEM. */
7142 if (reg_offset_p
7143 && opnum == 1
7144 && ((mode == DFmode && recog_data.operand_mode[0] == V2DFmode)
7145 || (mode == DImode && recog_data.operand_mode[0] == V2DImode)))
7146 reg_offset_p = false;
7148 /* We must recognize output that we have already generated ourselves. */
7149 if (GET_CODE (x) == PLUS
7150 && GET_CODE (XEXP (x, 0)) == PLUS
7151 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
7152 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7153 && GET_CODE (XEXP (x, 1)) == CONST_INT)
7155 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7156 BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
7157 opnum, (enum reload_type) type);
7158 *win = 1;
7159 return x;
7162 /* Likewise for (lo_sum (high ...) ...) output we have generated. */
7163 if (GET_CODE (x) == LO_SUM
7164 && GET_CODE (XEXP (x, 0)) == HIGH)
7166 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7167 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
7168 opnum, (enum reload_type) type);
7169 *win = 1;
7170 return x;
7173 #if TARGET_MACHO
7174 if (DEFAULT_ABI == ABI_DARWIN && flag_pic
7175 && GET_CODE (x) == LO_SUM
7176 && GET_CODE (XEXP (x, 0)) == PLUS
7177 && XEXP (XEXP (x, 0), 0) == pic_offset_table_rtx
7178 && GET_CODE (XEXP (XEXP (x, 0), 1)) == HIGH
7179 && XEXP (XEXP (XEXP (x, 0), 1), 0) == XEXP (x, 1)
7180 && machopic_operand_p (XEXP (x, 1)))
7182 /* Result of previous invocation of this function on Darwin
7183 floating point constant. */
7184 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7185 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
7186 opnum, (enum reload_type) type);
7187 *win = 1;
7188 return x;
7190 #endif
7192 if (TARGET_CMODEL != CMODEL_SMALL
7193 && reg_offset_p
7194 && small_toc_ref (x, VOIDmode))
7196 rtx hi = gen_rtx_HIGH (Pmode, copy_rtx (x));
7197 x = gen_rtx_LO_SUM (Pmode, hi, x);
7198 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7199 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
7200 opnum, (enum reload_type) type);
7201 *win = 1;
7202 return x;
7205 if (GET_CODE (x) == PLUS
7206 && GET_CODE (XEXP (x, 0)) == REG
7207 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
7208 && INT_REG_OK_FOR_BASE_P (XEXP (x, 0), 1)
7209 && GET_CODE (XEXP (x, 1)) == CONST_INT
7210 && reg_offset_p
7211 && !SPE_VECTOR_MODE (mode)
7212 && !(TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
7213 && (!VECTOR_MODE_P (mode) || VECTOR_MEM_NONE_P (mode)))
7215 HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
7216 HOST_WIDE_INT low = ((val & 0xffff) ^ 0x8000) - 0x8000;
7217 HOST_WIDE_INT high
7218 = (((val - low) & 0xffffffff) ^ 0x80000000) - 0x80000000;
7220 /* Check for 32-bit overflow. */
7221 if (high + low != val)
7223 *win = 0;
7224 return x;
7227 /* Reload the high part into a base reg; leave the low part
7228 in the mem directly. */
7230 x = gen_rtx_PLUS (GET_MODE (x),
7231 gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0),
7232 GEN_INT (high)),
7233 GEN_INT (low));
7235 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7236 BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
7237 opnum, (enum reload_type) type);
7238 *win = 1;
7239 return x;
7242 if (GET_CODE (x) == SYMBOL_REF
7243 && reg_offset_p
7244 && (!VECTOR_MODE_P (mode) || VECTOR_MEM_NONE_P (mode))
7245 && !SPE_VECTOR_MODE (mode)
7246 #if TARGET_MACHO
7247 && DEFAULT_ABI == ABI_DARWIN
7248 && (flag_pic || MACHO_DYNAMIC_NO_PIC_P)
7249 && machopic_symbol_defined_p (x)
7250 #else
7251 && DEFAULT_ABI == ABI_V4
7252 && !flag_pic
7253 #endif
7254 /* Don't do this for TFmode or TDmode, since the result isn't offsettable.
7255 The same goes for DImode without 64-bit gprs and DFmode and DDmode
7256 without fprs.
7257 ??? Assume floating point reg based on mode? This assumption is
7258 violated by eg. powerpc-linux -m32 compile of gcc.dg/pr28796-2.c
7259 where reload ends up doing a DFmode load of a constant from
7260 mem using two gprs. Unfortunately, at this point reload
7261 hasn't yet selected regs so poking around in reload data
7262 won't help and even if we could figure out the regs reliably,
7263 we'd still want to allow this transformation when the mem is
7264 naturally aligned. Since we say the address is good here, we
7265 can't disable offsets from LO_SUMs in mem_operand_gpr.
7266 FIXME: Allow offset from lo_sum for other modes too, when
7267 mem is sufficiently aligned. */
7268 && mode != TFmode
7269 && mode != TDmode
7270 && (mode != TImode || !TARGET_VSX_TIMODE)
7271 && mode != PTImode
7272 && (mode != DImode || TARGET_POWERPC64)
7273 && ((mode != DFmode && mode != DDmode) || TARGET_POWERPC64
7274 || (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)))
7276 #if TARGET_MACHO
7277 if (flag_pic)
7279 rtx offset = machopic_gen_offset (x);
7280 x = gen_rtx_LO_SUM (GET_MODE (x),
7281 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
7282 gen_rtx_HIGH (Pmode, offset)), offset);
7284 else
7285 #endif
7286 x = gen_rtx_LO_SUM (GET_MODE (x),
7287 gen_rtx_HIGH (Pmode, x), x);
7289 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7290 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
7291 opnum, (enum reload_type) type);
7292 *win = 1;
7293 return x;
7296 /* Reload an offset address wrapped by an AND that represents the
7297 masking of the lower bits. Strip the outer AND and let reload
7298 convert the offset address into an indirect address. For VSX,
7299 force reload to create the address with an AND in a separate
7300 register, because we can't guarantee an altivec register will
7301 be used. */
7302 if (VECTOR_MEM_ALTIVEC_P (mode)
7303 && GET_CODE (x) == AND
7304 && GET_CODE (XEXP (x, 0)) == PLUS
7305 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
7306 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7307 && GET_CODE (XEXP (x, 1)) == CONST_INT
7308 && INTVAL (XEXP (x, 1)) == -16)
7310 x = XEXP (x, 0);
7311 *win = 1;
7312 return x;
7315 if (TARGET_TOC
7316 && reg_offset_p
7317 && GET_CODE (x) == SYMBOL_REF
7318 && use_toc_relative_ref (x))
7320 x = create_TOC_reference (x, NULL_RTX);
7321 if (TARGET_CMODEL != CMODEL_SMALL)
7322 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7323 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
7324 opnum, (enum reload_type) type);
7325 *win = 1;
7326 return x;
7328 *win = 0;
7329 return x;
7332 /* Debug version of rs6000_legitimize_reload_address. */
7333 static rtx
7334 rs6000_debug_legitimize_reload_address (rtx x, enum machine_mode mode,
7335 int opnum, int type,
7336 int ind_levels, int *win)
7338 rtx ret = rs6000_legitimize_reload_address (x, mode, opnum, type,
7339 ind_levels, win);
7340 fprintf (stderr,
7341 "\nrs6000_legitimize_reload_address: mode = %s, opnum = %d, "
7342 "type = %d, ind_levels = %d, win = %d, original addr:\n",
7343 GET_MODE_NAME (mode), opnum, type, ind_levels, *win);
7344 debug_rtx (x);
7346 if (x == ret)
7347 fprintf (stderr, "Same address returned\n");
7348 else if (!ret)
7349 fprintf (stderr, "NULL returned\n");
7350 else
7352 fprintf (stderr, "New address:\n");
7353 debug_rtx (ret);
7356 return ret;
7359 /* TARGET_LEGITIMATE_ADDRESS_P recognizes an RTL expression
7360 that is a valid memory address for an instruction.
7361 The MODE argument is the machine mode for the MEM expression
7362 that wants to use this address.
7364 On the RS/6000, there are four valid address: a SYMBOL_REF that
7365 refers to a constant pool entry of an address (or the sum of it
7366 plus a constant), a short (16-bit signed) constant plus a register,
7367 the sum of two registers, or a register indirect, possibly with an
7368 auto-increment. For DFmode, DDmode and DImode with a constant plus
7369 register, we must ensure that both words are addressable or PowerPC64
7370 with offset word aligned.
7372 For modes spanning multiple registers (DFmode and DDmode in 32-bit GPRs,
7373 32-bit DImode, TImode, TFmode, TDmode), indexed addressing cannot be used
7374 because adjacent memory cells are accessed by adding word-sized offsets
7375 during assembly output. */
7376 static bool
7377 rs6000_legitimate_address_p (enum machine_mode mode, rtx x, bool reg_ok_strict)
7379 bool reg_offset_p = reg_offset_addressing_ok_p (mode);
7381 /* If this is an unaligned stvx/ldvx type address, discard the outer AND. */
7382 if (VECTOR_MEM_ALTIVEC_P (mode)
7383 && GET_CODE (x) == AND
7384 && GET_CODE (XEXP (x, 1)) == CONST_INT
7385 && INTVAL (XEXP (x, 1)) == -16)
7386 x = XEXP (x, 0);
7388 if (TARGET_ELF && RS6000_SYMBOL_REF_TLS_P (x))
7389 return 0;
7390 if (legitimate_indirect_address_p (x, reg_ok_strict))
7391 return 1;
7392 if (TARGET_UPDATE
7393 && (GET_CODE (x) == PRE_INC || GET_CODE (x) == PRE_DEC)
7394 && mode_supports_pre_incdec_p (mode)
7395 && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict))
7396 return 1;
7397 if (virtual_stack_registers_memory_p (x))
7398 return 1;
7399 if (reg_offset_p && legitimate_small_data_p (mode, x))
7400 return 1;
7401 if (reg_offset_p
7402 && legitimate_constant_pool_address_p (x, mode,
7403 reg_ok_strict || lra_in_progress))
7404 return 1;
7405 /* For TImode, if we have load/store quad and TImode in VSX registers, only
7406 allow register indirect addresses. This will allow the values to go in
7407 either GPRs or VSX registers without reloading. The vector types would
7408 tend to go into VSX registers, so we allow REG+REG, while TImode seems
7409 somewhat split, in that some uses are GPR based, and some VSX based. */
7410 if (mode == TImode && TARGET_QUAD_MEMORY && TARGET_VSX_TIMODE)
7411 return 0;
7412 /* If not REG_OK_STRICT (before reload) let pass any stack offset. */
7413 if (! reg_ok_strict
7414 && reg_offset_p
7415 && GET_CODE (x) == PLUS
7416 && GET_CODE (XEXP (x, 0)) == REG
7417 && (XEXP (x, 0) == virtual_stack_vars_rtx
7418 || XEXP (x, 0) == arg_pointer_rtx)
7419 && GET_CODE (XEXP (x, 1)) == CONST_INT)
7420 return 1;
7421 if (rs6000_legitimate_offset_address_p (mode, x, reg_ok_strict, false))
7422 return 1;
7423 if (mode != TFmode
7424 && mode != TDmode
7425 && ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
7426 || TARGET_POWERPC64
7427 || (mode != DFmode && mode != DDmode)
7428 || (TARGET_E500_DOUBLE && mode != DDmode))
7429 && (TARGET_POWERPC64 || mode != DImode)
7430 && (mode != TImode || VECTOR_MEM_VSX_P (TImode))
7431 && mode != PTImode
7432 && !avoiding_indexed_address_p (mode)
7433 && legitimate_indexed_address_p (x, reg_ok_strict))
7434 return 1;
7435 if (TARGET_UPDATE && GET_CODE (x) == PRE_MODIFY
7436 && mode_supports_pre_modify_p (mode)
7437 && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict)
7438 && (rs6000_legitimate_offset_address_p (mode, XEXP (x, 1),
7439 reg_ok_strict, false)
7440 || (!avoiding_indexed_address_p (mode)
7441 && legitimate_indexed_address_p (XEXP (x, 1), reg_ok_strict)))
7442 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
7443 return 1;
7444 if (reg_offset_p && legitimate_lo_sum_address_p (mode, x, reg_ok_strict))
7445 return 1;
7446 return 0;
7449 /* Debug version of rs6000_legitimate_address_p. */
7450 static bool
7451 rs6000_debug_legitimate_address_p (enum machine_mode mode, rtx x,
7452 bool reg_ok_strict)
7454 bool ret = rs6000_legitimate_address_p (mode, x, reg_ok_strict);
7455 fprintf (stderr,
7456 "\nrs6000_legitimate_address_p: return = %s, mode = %s, "
7457 "strict = %d, reload = %s, code = %s\n",
7458 ret ? "true" : "false",
7459 GET_MODE_NAME (mode),
7460 reg_ok_strict,
7461 (reload_completed
7462 ? "after"
7463 : (reload_in_progress ? "progress" : "before")),
7464 GET_RTX_NAME (GET_CODE (x)));
7465 debug_rtx (x);
7467 return ret;
7470 /* Implement TARGET_MODE_DEPENDENT_ADDRESS_P. */
7472 static bool
7473 rs6000_mode_dependent_address_p (const_rtx addr,
7474 addr_space_t as ATTRIBUTE_UNUSED)
7476 return rs6000_mode_dependent_address_ptr (addr);
7479 /* Go to LABEL if ADDR (a legitimate address expression)
7480 has an effect that depends on the machine mode it is used for.
7482 On the RS/6000 this is true of all integral offsets (since AltiVec
7483 and VSX modes don't allow them) or is a pre-increment or decrement.
7485 ??? Except that due to conceptual problems in offsettable_address_p
7486 we can't really report the problems of integral offsets. So leave
7487 this assuming that the adjustable offset must be valid for the
7488 sub-words of a TFmode operand, which is what we had before. */
7490 static bool
7491 rs6000_mode_dependent_address (const_rtx addr)
7493 switch (GET_CODE (addr))
7495 case PLUS:
7496 /* Any offset from virtual_stack_vars_rtx and arg_pointer_rtx
7497 is considered a legitimate address before reload, so there
7498 are no offset restrictions in that case. Note that this
7499 condition is safe in strict mode because any address involving
7500 virtual_stack_vars_rtx or arg_pointer_rtx would already have
7501 been rejected as illegitimate. */
7502 if (XEXP (addr, 0) != virtual_stack_vars_rtx
7503 && XEXP (addr, 0) != arg_pointer_rtx
7504 && GET_CODE (XEXP (addr, 1)) == CONST_INT)
7506 unsigned HOST_WIDE_INT val = INTVAL (XEXP (addr, 1));
7507 return val + 0x8000 >= 0x10000 - (TARGET_POWERPC64 ? 8 : 12);
7509 break;
7511 case LO_SUM:
7512 /* Anything in the constant pool is sufficiently aligned that
7513 all bytes have the same high part address. */
7514 return !legitimate_constant_pool_address_p (addr, QImode, false);
7516 /* Auto-increment cases are now treated generically in recog.c. */
7517 case PRE_MODIFY:
7518 return TARGET_UPDATE;
7520 /* AND is only allowed in Altivec loads. */
7521 case AND:
7522 return true;
7524 default:
7525 break;
7528 return false;
7531 /* Debug version of rs6000_mode_dependent_address. */
7532 static bool
7533 rs6000_debug_mode_dependent_address (const_rtx addr)
7535 bool ret = rs6000_mode_dependent_address (addr);
7537 fprintf (stderr, "\nrs6000_mode_dependent_address: ret = %s\n",
7538 ret ? "true" : "false");
7539 debug_rtx (addr);
7541 return ret;
7544 /* Implement FIND_BASE_TERM. */
7547 rs6000_find_base_term (rtx op)
7549 rtx base;
7551 base = op;
7552 if (GET_CODE (base) == CONST)
7553 base = XEXP (base, 0);
7554 if (GET_CODE (base) == PLUS)
7555 base = XEXP (base, 0);
7556 if (GET_CODE (base) == UNSPEC)
7557 switch (XINT (base, 1))
7559 case UNSPEC_TOCREL:
7560 case UNSPEC_MACHOPIC_OFFSET:
7561 /* OP represents SYM [+ OFFSET] - ANCHOR. SYM is the base term
7562 for aliasing purposes. */
7563 return XVECEXP (base, 0, 0);
7566 return op;
7569 /* More elaborate version of recog's offsettable_memref_p predicate
7570 that works around the ??? note of rs6000_mode_dependent_address.
7571 In particular it accepts
7573 (mem:DI (plus:SI (reg/f:SI 31 31) (const_int 32760 [0x7ff8])))
7575 in 32-bit mode, that the recog predicate rejects. */
7577 static bool
7578 rs6000_offsettable_memref_p (rtx op, enum machine_mode reg_mode)
7580 bool worst_case;
7582 if (!MEM_P (op))
7583 return false;
7585 /* First mimic offsettable_memref_p. */
7586 if (offsettable_address_p (true, GET_MODE (op), XEXP (op, 0)))
7587 return true;
7589 /* offsettable_address_p invokes rs6000_mode_dependent_address, but
7590 the latter predicate knows nothing about the mode of the memory
7591 reference and, therefore, assumes that it is the largest supported
7592 mode (TFmode). As a consequence, legitimate offsettable memory
7593 references are rejected. rs6000_legitimate_offset_address_p contains
7594 the correct logic for the PLUS case of rs6000_mode_dependent_address,
7595 at least with a little bit of help here given that we know the
7596 actual registers used. */
7597 worst_case = ((TARGET_POWERPC64 && GET_MODE_CLASS (reg_mode) == MODE_INT)
7598 || GET_MODE_SIZE (reg_mode) == 4);
7599 return rs6000_legitimate_offset_address_p (GET_MODE (op), XEXP (op, 0),
7600 true, worst_case);
7603 /* Change register usage conditional on target flags. */
7604 static void
7605 rs6000_conditional_register_usage (void)
7607 int i;
7609 if (TARGET_DEBUG_TARGET)
7610 fprintf (stderr, "rs6000_conditional_register_usage called\n");
7612 /* Set MQ register fixed (already call_used) so that it will not be
7613 allocated. */
7614 fixed_regs[64] = 1;
7616 /* 64-bit AIX and Linux reserve GPR13 for thread-private data. */
7617 if (TARGET_64BIT)
7618 fixed_regs[13] = call_used_regs[13]
7619 = call_really_used_regs[13] = 1;
7621 /* Conditionally disable FPRs. */
7622 if (TARGET_SOFT_FLOAT || !TARGET_FPRS)
7623 for (i = 32; i < 64; i++)
7624 fixed_regs[i] = call_used_regs[i]
7625 = call_really_used_regs[i] = 1;
7627 /* The TOC register is not killed across calls in a way that is
7628 visible to the compiler. */
7629 if (DEFAULT_ABI == ABI_AIX)
7630 call_really_used_regs[2] = 0;
7632 if (DEFAULT_ABI == ABI_V4
7633 && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
7634 && flag_pic == 2)
7635 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
7637 if (DEFAULT_ABI == ABI_V4
7638 && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
7639 && flag_pic == 1)
7640 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
7641 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
7642 = call_really_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
7644 if (DEFAULT_ABI == ABI_DARWIN
7645 && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM)
7646 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
7647 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
7648 = call_really_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
7650 if (TARGET_TOC && TARGET_MINIMAL_TOC)
7651 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
7652 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
7654 if (TARGET_SPE)
7656 global_regs[SPEFSCR_REGNO] = 1;
7657 /* We used to use r14 as FIXED_SCRATCH to address SPE 64-bit
7658 registers in prologues and epilogues. We no longer use r14
7659 for FIXED_SCRATCH, but we're keeping r14 out of the allocation
7660 pool for link-compatibility with older versions of GCC. Once
7661 "old" code has died out, we can return r14 to the allocation
7662 pool. */
7663 fixed_regs[14]
7664 = call_used_regs[14]
7665 = call_really_used_regs[14] = 1;
7668 if (!TARGET_ALTIVEC && !TARGET_VSX)
7670 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
7671 fixed_regs[i] = call_used_regs[i] = call_really_used_regs[i] = 1;
7672 call_really_used_regs[VRSAVE_REGNO] = 1;
7675 if (TARGET_ALTIVEC || TARGET_VSX)
7676 global_regs[VSCR_REGNO] = 1;
7678 if (TARGET_ALTIVEC_ABI)
7680 for (i = FIRST_ALTIVEC_REGNO; i < FIRST_ALTIVEC_REGNO + 20; ++i)
7681 call_used_regs[i] = call_really_used_regs[i] = 1;
7683 /* AIX reserves VR20:31 in non-extended ABI mode. */
7684 if (TARGET_XCOFF)
7685 for (i = FIRST_ALTIVEC_REGNO + 20; i < FIRST_ALTIVEC_REGNO + 32; ++i)
7686 fixed_regs[i] = call_used_regs[i] = call_really_used_regs[i] = 1;
7691 /* Try to output insns to set TARGET equal to the constant C if it can
7692 be done in less than N insns. Do all computations in MODE.
7693 Returns the place where the output has been placed if it can be
7694 done and the insns have been emitted. If it would take more than N
7695 insns, zero is returned and no insns and emitted. */
7698 rs6000_emit_set_const (rtx dest, enum machine_mode mode,
7699 rtx source, int n ATTRIBUTE_UNUSED)
7701 rtx result, insn, set;
7702 HOST_WIDE_INT c0, c1;
7704 switch (mode)
7706 case QImode:
7707 case HImode:
7708 if (dest == NULL)
7709 dest = gen_reg_rtx (mode);
7710 emit_insn (gen_rtx_SET (VOIDmode, dest, source));
7711 return dest;
7713 case SImode:
7714 result = !can_create_pseudo_p () ? dest : gen_reg_rtx (SImode);
7716 emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (result),
7717 GEN_INT (INTVAL (source)
7718 & (~ (HOST_WIDE_INT) 0xffff))));
7719 emit_insn (gen_rtx_SET (VOIDmode, dest,
7720 gen_rtx_IOR (SImode, copy_rtx (result),
7721 GEN_INT (INTVAL (source) & 0xffff))));
7722 result = dest;
7723 break;
7725 case DImode:
7726 switch (GET_CODE (source))
7728 case CONST_INT:
7729 c0 = INTVAL (source);
7730 c1 = -(c0 < 0);
7731 break;
7733 default:
7734 gcc_unreachable ();
7737 result = rs6000_emit_set_long_const (dest, c0, c1);
7738 break;
7740 default:
7741 gcc_unreachable ();
7744 insn = get_last_insn ();
7745 set = single_set (insn);
7746 if (! CONSTANT_P (SET_SRC (set)))
7747 set_unique_reg_note (insn, REG_EQUAL, source);
7749 return result;
7752 /* Having failed to find a 3 insn sequence in rs6000_emit_set_const,
7753 fall back to a straight forward decomposition. We do this to avoid
7754 exponential run times encountered when looking for longer sequences
7755 with rs6000_emit_set_const. */
7756 static rtx
7757 rs6000_emit_set_long_const (rtx dest, HOST_WIDE_INT c1, HOST_WIDE_INT c2)
7759 if (!TARGET_POWERPC64)
7761 rtx operand1, operand2;
7763 operand1 = operand_subword_force (dest, WORDS_BIG_ENDIAN == 0,
7764 DImode);
7765 operand2 = operand_subword_force (copy_rtx (dest), WORDS_BIG_ENDIAN != 0,
7766 DImode);
7767 emit_move_insn (operand1, GEN_INT (c1));
7768 emit_move_insn (operand2, GEN_INT (c2));
7770 else
7772 HOST_WIDE_INT ud1, ud2, ud3, ud4;
7774 ud1 = c1 & 0xffff;
7775 ud2 = (c1 & 0xffff0000) >> 16;
7776 c2 = c1 >> 32;
7777 ud3 = c2 & 0xffff;
7778 ud4 = (c2 & 0xffff0000) >> 16;
7780 if ((ud4 == 0xffff && ud3 == 0xffff && ud2 == 0xffff && (ud1 & 0x8000))
7781 || (ud4 == 0 && ud3 == 0 && ud2 == 0 && ! (ud1 & 0x8000)))
7782 emit_move_insn (dest, GEN_INT ((ud1 ^ 0x8000) - 0x8000));
7784 else if ((ud4 == 0xffff && ud3 == 0xffff && (ud2 & 0x8000))
7785 || (ud4 == 0 && ud3 == 0 && ! (ud2 & 0x8000)))
7787 emit_move_insn (dest, GEN_INT (((ud2 << 16) ^ 0x80000000)
7788 - 0x80000000));
7789 if (ud1 != 0)
7790 emit_move_insn (copy_rtx (dest),
7791 gen_rtx_IOR (DImode, copy_rtx (dest),
7792 GEN_INT (ud1)));
7794 else if (ud3 == 0 && ud4 == 0)
7796 gcc_assert (ud2 & 0x8000);
7797 emit_move_insn (dest, GEN_INT (((ud2 << 16) ^ 0x80000000)
7798 - 0x80000000));
7799 if (ud1 != 0)
7800 emit_move_insn (copy_rtx (dest),
7801 gen_rtx_IOR (DImode, copy_rtx (dest),
7802 GEN_INT (ud1)));
7803 emit_move_insn (copy_rtx (dest),
7804 gen_rtx_ZERO_EXTEND (DImode,
7805 gen_lowpart (SImode,
7806 copy_rtx (dest))));
7808 else if ((ud4 == 0xffff && (ud3 & 0x8000))
7809 || (ud4 == 0 && ! (ud3 & 0x8000)))
7811 emit_move_insn (dest, GEN_INT (((ud3 << 16) ^ 0x80000000)
7812 - 0x80000000));
7813 if (ud2 != 0)
7814 emit_move_insn (copy_rtx (dest),
7815 gen_rtx_IOR (DImode, copy_rtx (dest),
7816 GEN_INT (ud2)));
7817 emit_move_insn (copy_rtx (dest),
7818 gen_rtx_ASHIFT (DImode, copy_rtx (dest),
7819 GEN_INT (16)));
7820 if (ud1 != 0)
7821 emit_move_insn (copy_rtx (dest),
7822 gen_rtx_IOR (DImode, copy_rtx (dest),
7823 GEN_INT (ud1)));
7825 else
7827 emit_move_insn (dest, GEN_INT (((ud4 << 16) ^ 0x80000000)
7828 - 0x80000000));
7829 if (ud3 != 0)
7830 emit_move_insn (copy_rtx (dest),
7831 gen_rtx_IOR (DImode, copy_rtx (dest),
7832 GEN_INT (ud3)));
7834 emit_move_insn (copy_rtx (dest),
7835 gen_rtx_ASHIFT (DImode, copy_rtx (dest),
7836 GEN_INT (32)));
7837 if (ud2 != 0)
7838 emit_move_insn (copy_rtx (dest),
7839 gen_rtx_IOR (DImode, copy_rtx (dest),
7840 GEN_INT (ud2 << 16)));
7841 if (ud1 != 0)
7842 emit_move_insn (copy_rtx (dest),
7843 gen_rtx_IOR (DImode, copy_rtx (dest),
7844 GEN_INT (ud1)));
7847 return dest;
7850 /* Helper for the following. Get rid of [r+r] memory refs
7851 in cases where it won't work (TImode, TFmode, TDmode, PTImode). */
7853 static void
7854 rs6000_eliminate_indexed_memrefs (rtx operands[2])
7856 if (reload_in_progress)
7857 return;
7859 if (GET_CODE (operands[0]) == MEM
7860 && GET_CODE (XEXP (operands[0], 0)) != REG
7861 && ! legitimate_constant_pool_address_p (XEXP (operands[0], 0),
7862 GET_MODE (operands[0]), false))
7863 operands[0]
7864 = replace_equiv_address (operands[0],
7865 copy_addr_to_reg (XEXP (operands[0], 0)));
7867 if (GET_CODE (operands[1]) == MEM
7868 && GET_CODE (XEXP (operands[1], 0)) != REG
7869 && ! legitimate_constant_pool_address_p (XEXP (operands[1], 0),
7870 GET_MODE (operands[1]), false))
7871 operands[1]
7872 = replace_equiv_address (operands[1],
7873 copy_addr_to_reg (XEXP (operands[1], 0)));
7876 /* Generate a vector of constants to permute MODE for a little-endian
7877 storage operation by swapping the two halves of a vector. */
7878 static rtvec
7879 rs6000_const_vec (enum machine_mode mode)
7881 int i, subparts;
7882 rtvec v;
7884 switch (mode)
7886 case V2DFmode:
7887 case V2DImode:
7888 subparts = 2;
7889 break;
7890 case V4SFmode:
7891 case V4SImode:
7892 subparts = 4;
7893 break;
7894 case V8HImode:
7895 subparts = 8;
7896 break;
7897 case V16QImode:
7898 subparts = 16;
7899 break;
7900 default:
7901 gcc_unreachable();
7904 v = rtvec_alloc (subparts);
7906 for (i = 0; i < subparts / 2; ++i)
7907 RTVEC_ELT (v, i) = gen_rtx_CONST_INT (DImode, i + subparts / 2);
7908 for (i = subparts / 2; i < subparts; ++i)
7909 RTVEC_ELT (v, i) = gen_rtx_CONST_INT (DImode, i - subparts / 2);
7911 return v;
7914 /* Generate a permute rtx that represents an lxvd2x, stxvd2x, or xxpermdi
7915 for a VSX load or store operation. */
7917 rs6000_gen_le_vsx_permute (rtx source, enum machine_mode mode)
7919 rtx par = gen_rtx_PARALLEL (VOIDmode, rs6000_const_vec (mode));
7920 return gen_rtx_VEC_SELECT (mode, source, par);
7923 /* Emit a little-endian load from vector memory location SOURCE to VSX
7924 register DEST in mode MODE. The load is done with two permuting
7925 insn's that represent an lxvd2x and xxpermdi. */
7926 void
7927 rs6000_emit_le_vsx_load (rtx dest, rtx source, enum machine_mode mode)
7929 rtx tmp = can_create_pseudo_p () ? gen_reg_rtx_and_attrs (dest) : dest;
7930 rtx permute_mem = rs6000_gen_le_vsx_permute (source, mode);
7931 rtx permute_reg = rs6000_gen_le_vsx_permute (tmp, mode);
7932 emit_insn (gen_rtx_SET (VOIDmode, tmp, permute_mem));
7933 emit_insn (gen_rtx_SET (VOIDmode, dest, permute_reg));
7936 /* Emit a little-endian store to vector memory location DEST from VSX
7937 register SOURCE in mode MODE. The store is done with two permuting
7938 insn's that represent an xxpermdi and an stxvd2x. */
7939 void
7940 rs6000_emit_le_vsx_store (rtx dest, rtx source, enum machine_mode mode)
7942 rtx tmp = can_create_pseudo_p () ? gen_reg_rtx_and_attrs (source) : source;
7943 rtx permute_src = rs6000_gen_le_vsx_permute (source, mode);
7944 rtx permute_tmp = rs6000_gen_le_vsx_permute (tmp, mode);
7945 emit_insn (gen_rtx_SET (VOIDmode, tmp, permute_src));
7946 emit_insn (gen_rtx_SET (VOIDmode, dest, permute_tmp));
7949 /* Emit a sequence representing a little-endian VSX load or store,
7950 moving data from SOURCE to DEST in mode MODE. This is done
7951 separately from rs6000_emit_move to ensure it is called only
7952 during expand. LE VSX loads and stores introduced later are
7953 handled with a split. The expand-time RTL generation allows
7954 us to optimize away redundant pairs of register-permutes. */
7955 void
7956 rs6000_emit_le_vsx_move (rtx dest, rtx source, enum machine_mode mode)
7958 gcc_assert (!BYTES_BIG_ENDIAN
7959 && VECTOR_MEM_VSX_P (mode)
7960 && mode != TImode
7961 && (MEM_P (source) ^ MEM_P (dest)));
7963 if (MEM_P (source))
7965 gcc_assert (REG_P (dest));
7966 rs6000_emit_le_vsx_load (dest, source, mode);
7968 else
7970 if (!REG_P (source))
7971 source = force_reg (mode, source);
7972 rs6000_emit_le_vsx_store (dest, source, mode);
7976 /* Emit a move from SOURCE to DEST in mode MODE. */
7977 void
7978 rs6000_emit_move (rtx dest, rtx source, enum machine_mode mode)
7980 rtx operands[2];
7981 operands[0] = dest;
7982 operands[1] = source;
7984 if (TARGET_DEBUG_ADDR)
7986 fprintf (stderr,
7987 "\nrs6000_emit_move: mode = %s, reload_in_progress = %d, "
7988 "reload_completed = %d, can_create_pseudos = %d.\ndest:\n",
7989 GET_MODE_NAME (mode),
7990 reload_in_progress,
7991 reload_completed,
7992 can_create_pseudo_p ());
7993 debug_rtx (dest);
7994 fprintf (stderr, "source:\n");
7995 debug_rtx (source);
7998 /* Sanity checks. Check that we get CONST_DOUBLE only when we should. */
7999 if (GET_CODE (operands[1]) == CONST_DOUBLE
8000 && ! FLOAT_MODE_P (mode)
8001 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
8003 /* FIXME. This should never happen. */
8004 /* Since it seems that it does, do the safe thing and convert
8005 to a CONST_INT. */
8006 operands[1] = gen_int_mode (CONST_DOUBLE_LOW (operands[1]), mode);
8008 gcc_assert (GET_CODE (operands[1]) != CONST_DOUBLE
8009 || FLOAT_MODE_P (mode)
8010 || ((CONST_DOUBLE_HIGH (operands[1]) != 0
8011 || CONST_DOUBLE_LOW (operands[1]) < 0)
8012 && (CONST_DOUBLE_HIGH (operands[1]) != -1
8013 || CONST_DOUBLE_LOW (operands[1]) >= 0)));
8015 /* Check if GCC is setting up a block move that will end up using FP
8016 registers as temporaries. We must make sure this is acceptable. */
8017 if (GET_CODE (operands[0]) == MEM
8018 && GET_CODE (operands[1]) == MEM
8019 && mode == DImode
8020 && (SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[0]))
8021 || SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[1])))
8022 && ! (SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[0]) > 32
8023 ? 32 : MEM_ALIGN (operands[0])))
8024 || SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[1]) > 32
8025 ? 32
8026 : MEM_ALIGN (operands[1]))))
8027 && ! MEM_VOLATILE_P (operands [0])
8028 && ! MEM_VOLATILE_P (operands [1]))
8030 emit_move_insn (adjust_address (operands[0], SImode, 0),
8031 adjust_address (operands[1], SImode, 0));
8032 emit_move_insn (adjust_address (copy_rtx (operands[0]), SImode, 4),
8033 adjust_address (copy_rtx (operands[1]), SImode, 4));
8034 return;
8037 if (can_create_pseudo_p () && GET_CODE (operands[0]) == MEM
8038 && !gpc_reg_operand (operands[1], mode))
8039 operands[1] = force_reg (mode, operands[1]);
8041 /* Recognize the case where operand[1] is a reference to thread-local
8042 data and load its address to a register. */
8043 if (rs6000_tls_referenced_p (operands[1]))
8045 enum tls_model model;
8046 rtx tmp = operands[1];
8047 rtx addend = NULL;
8049 if (GET_CODE (tmp) == CONST && GET_CODE (XEXP (tmp, 0)) == PLUS)
8051 addend = XEXP (XEXP (tmp, 0), 1);
8052 tmp = XEXP (XEXP (tmp, 0), 0);
8055 gcc_assert (GET_CODE (tmp) == SYMBOL_REF);
8056 model = SYMBOL_REF_TLS_MODEL (tmp);
8057 gcc_assert (model != 0);
8059 tmp = rs6000_legitimize_tls_address (tmp, model);
8060 if (addend)
8062 tmp = gen_rtx_PLUS (mode, tmp, addend);
8063 tmp = force_operand (tmp, operands[0]);
8065 operands[1] = tmp;
8068 /* Handle the case where reload calls us with an invalid address. */
8069 if (reload_in_progress && mode == Pmode
8070 && (! general_operand (operands[1], mode)
8071 || ! nonimmediate_operand (operands[0], mode)))
8072 goto emit_set;
8074 /* 128-bit constant floating-point values on Darwin should really be
8075 loaded as two parts. */
8076 if (!TARGET_IEEEQUAD && TARGET_LONG_DOUBLE_128
8077 && mode == TFmode && GET_CODE (operands[1]) == CONST_DOUBLE)
8079 rs6000_emit_move (simplify_gen_subreg (DFmode, operands[0], mode, 0),
8080 simplify_gen_subreg (DFmode, operands[1], mode, 0),
8081 DFmode);
8082 rs6000_emit_move (simplify_gen_subreg (DFmode, operands[0], mode,
8083 GET_MODE_SIZE (DFmode)),
8084 simplify_gen_subreg (DFmode, operands[1], mode,
8085 GET_MODE_SIZE (DFmode)),
8086 DFmode);
8087 return;
8090 if (reload_in_progress && cfun->machine->sdmode_stack_slot != NULL_RTX)
8091 cfun->machine->sdmode_stack_slot =
8092 eliminate_regs (cfun->machine->sdmode_stack_slot, VOIDmode, NULL_RTX);
8095 if (lra_in_progress
8096 && mode == SDmode
8097 && REG_P (operands[0]) && REGNO (operands[0]) >= FIRST_PSEUDO_REGISTER
8098 && reg_preferred_class (REGNO (operands[0])) == NO_REGS
8099 && (REG_P (operands[1])
8100 || (GET_CODE (operands[1]) == SUBREG
8101 && REG_P (SUBREG_REG (operands[1])))))
8103 int regno = REGNO (GET_CODE (operands[1]) == SUBREG
8104 ? SUBREG_REG (operands[1]) : operands[1]);
8105 enum reg_class cl;
8107 if (regno >= FIRST_PSEUDO_REGISTER)
8109 cl = reg_preferred_class (regno);
8110 gcc_assert (cl != NO_REGS);
8111 regno = ira_class_hard_regs[cl][0];
8113 if (FP_REGNO_P (regno))
8115 if (GET_MODE (operands[0]) != DDmode)
8116 operands[0] = gen_rtx_SUBREG (DDmode, operands[0], 0);
8117 emit_insn (gen_movsd_store (operands[0], operands[1]));
8119 else if (INT_REGNO_P (regno))
8120 emit_insn (gen_movsd_hardfloat (operands[0], operands[1]));
8121 else
8122 gcc_unreachable();
8123 return;
8125 if (lra_in_progress
8126 && mode == SDmode
8127 && (REG_P (operands[0])
8128 || (GET_CODE (operands[0]) == SUBREG
8129 && REG_P (SUBREG_REG (operands[0]))))
8130 && REG_P (operands[1]) && REGNO (operands[1]) >= FIRST_PSEUDO_REGISTER
8131 && reg_preferred_class (REGNO (operands[1])) == NO_REGS)
8133 int regno = REGNO (GET_CODE (operands[0]) == SUBREG
8134 ? SUBREG_REG (operands[0]) : operands[0]);
8135 enum reg_class cl;
8137 if (regno >= FIRST_PSEUDO_REGISTER)
8139 cl = reg_preferred_class (regno);
8140 gcc_assert (cl != NO_REGS);
8141 regno = ira_class_hard_regs[cl][0];
8143 if (FP_REGNO_P (regno))
8145 if (GET_MODE (operands[1]) != DDmode)
8146 operands[1] = gen_rtx_SUBREG (DDmode, operands[1], 0);
8147 emit_insn (gen_movsd_load (operands[0], operands[1]));
8149 else if (INT_REGNO_P (regno))
8150 emit_insn (gen_movsd_hardfloat (operands[0], operands[1]));
8151 else
8152 gcc_unreachable();
8153 return;
8156 if (reload_in_progress
8157 && mode == SDmode
8158 && cfun->machine->sdmode_stack_slot != NULL_RTX
8159 && MEM_P (operands[0])
8160 && rtx_equal_p (operands[0], cfun->machine->sdmode_stack_slot)
8161 && REG_P (operands[1]))
8163 if (FP_REGNO_P (REGNO (operands[1])))
8165 rtx mem = adjust_address_nv (operands[0], DDmode, 0);
8166 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
8167 emit_insn (gen_movsd_store (mem, operands[1]));
8169 else if (INT_REGNO_P (REGNO (operands[1])))
8171 rtx mem = adjust_address_nv (operands[0], mode, 4);
8172 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
8173 emit_insn (gen_movsd_hardfloat (mem, operands[1]));
8175 else
8176 gcc_unreachable();
8177 return;
8179 if (reload_in_progress
8180 && mode == SDmode
8181 && REG_P (operands[0])
8182 && MEM_P (operands[1])
8183 && cfun->machine->sdmode_stack_slot != NULL_RTX
8184 && rtx_equal_p (operands[1], cfun->machine->sdmode_stack_slot))
8186 if (FP_REGNO_P (REGNO (operands[0])))
8188 rtx mem = adjust_address_nv (operands[1], DDmode, 0);
8189 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
8190 emit_insn (gen_movsd_load (operands[0], mem));
8192 else if (INT_REGNO_P (REGNO (operands[0])))
8194 rtx mem = adjust_address_nv (operands[1], mode, 4);
8195 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
8196 emit_insn (gen_movsd_hardfloat (operands[0], mem));
8198 else
8199 gcc_unreachable();
8200 return;
8203 /* FIXME: In the long term, this switch statement should go away
8204 and be replaced by a sequence of tests based on things like
8205 mode == Pmode. */
8206 switch (mode)
8208 case HImode:
8209 case QImode:
8210 if (CONSTANT_P (operands[1])
8211 && GET_CODE (operands[1]) != CONST_INT)
8212 operands[1] = force_const_mem (mode, operands[1]);
8213 break;
8215 case TFmode:
8216 case TDmode:
8217 rs6000_eliminate_indexed_memrefs (operands);
8218 /* fall through */
8220 case DFmode:
8221 case DDmode:
8222 case SFmode:
8223 case SDmode:
8224 if (CONSTANT_P (operands[1])
8225 && ! easy_fp_constant (operands[1], mode))
8226 operands[1] = force_const_mem (mode, operands[1]);
8227 break;
8229 case V16QImode:
8230 case V8HImode:
8231 case V4SFmode:
8232 case V4SImode:
8233 case V4HImode:
8234 case V2SFmode:
8235 case V2SImode:
8236 case V1DImode:
8237 case V2DFmode:
8238 case V2DImode:
8239 if (CONSTANT_P (operands[1])
8240 && !easy_vector_constant (operands[1], mode))
8241 operands[1] = force_const_mem (mode, operands[1]);
8242 break;
8244 case SImode:
8245 case DImode:
8246 /* Use default pattern for address of ELF small data */
8247 if (TARGET_ELF
8248 && mode == Pmode
8249 && DEFAULT_ABI == ABI_V4
8250 && (GET_CODE (operands[1]) == SYMBOL_REF
8251 || GET_CODE (operands[1]) == CONST)
8252 && small_data_operand (operands[1], mode))
8254 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
8255 return;
8258 if (DEFAULT_ABI == ABI_V4
8259 && mode == Pmode && mode == SImode
8260 && flag_pic == 1 && got_operand (operands[1], mode))
8262 emit_insn (gen_movsi_got (operands[0], operands[1]));
8263 return;
8266 if ((TARGET_ELF || DEFAULT_ABI == ABI_DARWIN)
8267 && TARGET_NO_TOC
8268 && ! flag_pic
8269 && mode == Pmode
8270 && CONSTANT_P (operands[1])
8271 && GET_CODE (operands[1]) != HIGH
8272 && GET_CODE (operands[1]) != CONST_INT)
8274 rtx target = (!can_create_pseudo_p ()
8275 ? operands[0]
8276 : gen_reg_rtx (mode));
8278 /* If this is a function address on -mcall-aixdesc,
8279 convert it to the address of the descriptor. */
8280 if (DEFAULT_ABI == ABI_AIX
8281 && GET_CODE (operands[1]) == SYMBOL_REF
8282 && XSTR (operands[1], 0)[0] == '.')
8284 const char *name = XSTR (operands[1], 0);
8285 rtx new_ref;
8286 while (*name == '.')
8287 name++;
8288 new_ref = gen_rtx_SYMBOL_REF (Pmode, name);
8289 CONSTANT_POOL_ADDRESS_P (new_ref)
8290 = CONSTANT_POOL_ADDRESS_P (operands[1]);
8291 SYMBOL_REF_FLAGS (new_ref) = SYMBOL_REF_FLAGS (operands[1]);
8292 SYMBOL_REF_USED (new_ref) = SYMBOL_REF_USED (operands[1]);
8293 SYMBOL_REF_DATA (new_ref) = SYMBOL_REF_DATA (operands[1]);
8294 operands[1] = new_ref;
8297 if (DEFAULT_ABI == ABI_DARWIN)
8299 #if TARGET_MACHO
8300 if (MACHO_DYNAMIC_NO_PIC_P)
8302 /* Take care of any required data indirection. */
8303 operands[1] = rs6000_machopic_legitimize_pic_address (
8304 operands[1], mode, operands[0]);
8305 if (operands[0] != operands[1])
8306 emit_insn (gen_rtx_SET (VOIDmode,
8307 operands[0], operands[1]));
8308 return;
8310 #endif
8311 emit_insn (gen_macho_high (target, operands[1]));
8312 emit_insn (gen_macho_low (operands[0], target, operands[1]));
8313 return;
8316 emit_insn (gen_elf_high (target, operands[1]));
8317 emit_insn (gen_elf_low (operands[0], target, operands[1]));
8318 return;
8321 /* If this is a SYMBOL_REF that refers to a constant pool entry,
8322 and we have put it in the TOC, we just need to make a TOC-relative
8323 reference to it. */
8324 if (TARGET_TOC
8325 && GET_CODE (operands[1]) == SYMBOL_REF
8326 && use_toc_relative_ref (operands[1]))
8327 operands[1] = create_TOC_reference (operands[1], operands[0]);
8328 else if (mode == Pmode
8329 && CONSTANT_P (operands[1])
8330 && GET_CODE (operands[1]) != HIGH
8331 && ((GET_CODE (operands[1]) != CONST_INT
8332 && ! easy_fp_constant (operands[1], mode))
8333 || (GET_CODE (operands[1]) == CONST_INT
8334 && (num_insns_constant (operands[1], mode)
8335 > (TARGET_CMODEL != CMODEL_SMALL ? 3 : 2)))
8336 || (GET_CODE (operands[0]) == REG
8337 && FP_REGNO_P (REGNO (operands[0]))))
8338 && !toc_relative_expr_p (operands[1], false)
8339 && (TARGET_CMODEL == CMODEL_SMALL
8340 || can_create_pseudo_p ()
8341 || (REG_P (operands[0])
8342 && INT_REG_OK_FOR_BASE_P (operands[0], true))))
8345 #if TARGET_MACHO
8346 /* Darwin uses a special PIC legitimizer. */
8347 if (DEFAULT_ABI == ABI_DARWIN && MACHOPIC_INDIRECT)
8349 operands[1] =
8350 rs6000_machopic_legitimize_pic_address (operands[1], mode,
8351 operands[0]);
8352 if (operands[0] != operands[1])
8353 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
8354 return;
8356 #endif
8358 /* If we are to limit the number of things we put in the TOC and
8359 this is a symbol plus a constant we can add in one insn,
8360 just put the symbol in the TOC and add the constant. Don't do
8361 this if reload is in progress. */
8362 if (GET_CODE (operands[1]) == CONST
8363 && TARGET_NO_SUM_IN_TOC && ! reload_in_progress
8364 && GET_CODE (XEXP (operands[1], 0)) == PLUS
8365 && add_operand (XEXP (XEXP (operands[1], 0), 1), mode)
8366 && (GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == LABEL_REF
8367 || GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == SYMBOL_REF)
8368 && ! side_effects_p (operands[0]))
8370 rtx sym =
8371 force_const_mem (mode, XEXP (XEXP (operands[1], 0), 0));
8372 rtx other = XEXP (XEXP (operands[1], 0), 1);
8374 sym = force_reg (mode, sym);
8375 emit_insn (gen_add3_insn (operands[0], sym, other));
8376 return;
8379 operands[1] = force_const_mem (mode, operands[1]);
8381 if (TARGET_TOC
8382 && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
8383 && constant_pool_expr_p (XEXP (operands[1], 0))
8384 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (
8385 get_pool_constant (XEXP (operands[1], 0)),
8386 get_pool_mode (XEXP (operands[1], 0))))
8388 rtx tocref = create_TOC_reference (XEXP (operands[1], 0),
8389 operands[0]);
8390 operands[1] = gen_const_mem (mode, tocref);
8391 set_mem_alias_set (operands[1], get_TOC_alias_set ());
8394 break;
8396 case TImode:
8397 if (!VECTOR_MEM_VSX_P (TImode))
8398 rs6000_eliminate_indexed_memrefs (operands);
8399 break;
8401 case PTImode:
8402 rs6000_eliminate_indexed_memrefs (operands);
8403 break;
8405 default:
8406 fatal_insn ("bad move", gen_rtx_SET (VOIDmode, dest, source));
8409 /* Above, we may have called force_const_mem which may have returned
8410 an invalid address. If we can, fix this up; otherwise, reload will
8411 have to deal with it. */
8412 if (GET_CODE (operands[1]) == MEM && ! reload_in_progress)
8413 operands[1] = validize_mem (operands[1]);
8415 emit_set:
8416 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
8419 /* Return true if a structure, union or array containing FIELD should be
8420 accessed using `BLKMODE'.
8422 For the SPE, simd types are V2SI, and gcc can be tempted to put the
8423 entire thing in a DI and use subregs to access the internals.
8424 store_bit_field() will force (subreg:DI (reg:V2SI x))'s to the
8425 back-end. Because a single GPR can hold a V2SI, but not a DI, the
8426 best thing to do is set structs to BLKmode and avoid Severe Tire
8427 Damage.
8429 On e500 v2, DF and DI modes suffer from the same anomaly. DF can
8430 fit into 1, whereas DI still needs two. */
8432 static bool
8433 rs6000_member_type_forces_blk (const_tree field, enum machine_mode mode)
8435 return ((TARGET_SPE && TREE_CODE (TREE_TYPE (field)) == VECTOR_TYPE)
8436 || (TARGET_E500_DOUBLE && mode == DFmode));
8439 /* Nonzero if we can use a floating-point register to pass this arg. */
8440 #define USE_FP_FOR_ARG_P(CUM,MODE,TYPE) \
8441 (SCALAR_FLOAT_MODE_P (MODE) \
8442 && (CUM)->fregno <= FP_ARG_MAX_REG \
8443 && TARGET_HARD_FLOAT && TARGET_FPRS)
8445 /* Nonzero if we can use an AltiVec register to pass this arg. */
8446 #define USE_ALTIVEC_FOR_ARG_P(CUM,MODE,TYPE,NAMED) \
8447 (ALTIVEC_OR_VSX_VECTOR_MODE (MODE) \
8448 && (CUM)->vregno <= ALTIVEC_ARG_MAX_REG \
8449 && TARGET_ALTIVEC_ABI \
8450 && (NAMED))
8452 /* Return a nonzero value to say to return the function value in
8453 memory, just as large structures are always returned. TYPE will be
8454 the data type of the value, and FNTYPE will be the type of the
8455 function doing the returning, or @code{NULL} for libcalls.
8457 The AIX ABI for the RS/6000 specifies that all structures are
8458 returned in memory. The Darwin ABI does the same.
8460 For the Darwin 64 Bit ABI, a function result can be returned in
8461 registers or in memory, depending on the size of the return data
8462 type. If it is returned in registers, the value occupies the same
8463 registers as it would if it were the first and only function
8464 argument. Otherwise, the function places its result in memory at
8465 the location pointed to by GPR3.
8467 The SVR4 ABI specifies that structures <= 8 bytes are returned in r3/r4,
8468 but a draft put them in memory, and GCC used to implement the draft
8469 instead of the final standard. Therefore, aix_struct_return
8470 controls this instead of DEFAULT_ABI; V.4 targets needing backward
8471 compatibility can change DRAFT_V4_STRUCT_RET to override the
8472 default, and -m switches get the final word. See
8473 rs6000_option_override_internal for more details.
8475 The PPC32 SVR4 ABI uses IEEE double extended for long double, if 128-bit
8476 long double support is enabled. These values are returned in memory.
8478 int_size_in_bytes returns -1 for variable size objects, which go in
8479 memory always. The cast to unsigned makes -1 > 8. */
8481 static bool
8482 rs6000_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
8484 /* For the Darwin64 ABI, test if we can fit the return value in regs. */
8485 if (TARGET_MACHO
8486 && rs6000_darwin64_abi
8487 && TREE_CODE (type) == RECORD_TYPE
8488 && int_size_in_bytes (type) > 0)
8490 CUMULATIVE_ARGS valcum;
8491 rtx valret;
8493 valcum.words = 0;
8494 valcum.fregno = FP_ARG_MIN_REG;
8495 valcum.vregno = ALTIVEC_ARG_MIN_REG;
8496 /* Do a trial code generation as if this were going to be passed
8497 as an argument; if any part goes in memory, we return NULL. */
8498 valret = rs6000_darwin64_record_arg (&valcum, type, true, true);
8499 if (valret)
8500 return false;
8501 /* Otherwise fall through to more conventional ABI rules. */
8504 #if HAVE_UPC_PTS_STRUCT_REP
8505 if (POINTER_TYPE_P (type) && upc_shared_type_p (TREE_TYPE (type)))
8506 return true;
8507 #endif
8509 if (AGGREGATE_TYPE_P (type)
8510 && (aix_struct_return
8511 || (unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 8))
8512 return true;
8514 /* Allow -maltivec -mabi=no-altivec without warning. Altivec vector
8515 modes only exist for GCC vector types if -maltivec. */
8516 if (TARGET_32BIT && !TARGET_ALTIVEC_ABI
8517 && ALTIVEC_VECTOR_MODE (TYPE_MODE (type)))
8518 return false;
8520 /* Return synthetic vectors in memory. */
8521 if (TREE_CODE (type) == VECTOR_TYPE
8522 && int_size_in_bytes (type) > (TARGET_ALTIVEC_ABI ? 16 : 8))
8524 static bool warned_for_return_big_vectors = false;
8525 if (!warned_for_return_big_vectors)
8527 warning (0, "GCC vector returned by reference: "
8528 "non-standard ABI extension with no compatibility guarantee");
8529 warned_for_return_big_vectors = true;
8531 return true;
8534 if (DEFAULT_ABI == ABI_V4 && TARGET_IEEEQUAD && TYPE_MODE (type) == TFmode)
8535 return true;
8537 return false;
8540 #ifdef HAVE_AS_GNU_ATTRIBUTE
8541 /* Return TRUE if a call to function FNDECL may be one that
8542 potentially affects the function calling ABI of the object file. */
8544 static bool
8545 call_ABI_of_interest (tree fndecl)
8547 if (cgraph_state == CGRAPH_STATE_EXPANSION)
8549 struct cgraph_node *c_node;
8551 /* Libcalls are always interesting. */
8552 if (fndecl == NULL_TREE)
8553 return true;
8555 /* Any call to an external function is interesting. */
8556 if (DECL_EXTERNAL (fndecl))
8557 return true;
8559 /* Interesting functions that we are emitting in this object file. */
8560 c_node = cgraph_get_node (fndecl);
8561 c_node = cgraph_function_or_thunk_node (c_node, NULL);
8562 return !cgraph_only_called_directly_p (c_node);
8564 return false;
8566 #endif
8568 /* Initialize a variable CUM of type CUMULATIVE_ARGS
8569 for a call to a function whose data type is FNTYPE.
8570 For a library call, FNTYPE is 0 and RETURN_MODE the return value mode.
8572 For incoming args we set the number of arguments in the prototype large
8573 so we never return a PARALLEL. */
8575 void
8576 init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype,
8577 rtx libname ATTRIBUTE_UNUSED, int incoming,
8578 int libcall, int n_named_args,
8579 tree fndecl ATTRIBUTE_UNUSED,
8580 enum machine_mode return_mode ATTRIBUTE_UNUSED)
8582 static CUMULATIVE_ARGS zero_cumulative;
8584 *cum = zero_cumulative;
8585 cum->words = 0;
8586 cum->fregno = FP_ARG_MIN_REG;
8587 cum->vregno = ALTIVEC_ARG_MIN_REG;
8588 cum->prototype = (fntype && prototype_p (fntype));
8589 cum->call_cookie = ((DEFAULT_ABI == ABI_V4 && libcall)
8590 ? CALL_LIBCALL : CALL_NORMAL);
8591 cum->sysv_gregno = GP_ARG_MIN_REG;
8592 cum->stdarg = stdarg_p (fntype);
8594 cum->nargs_prototype = 0;
8595 if (incoming || cum->prototype)
8596 cum->nargs_prototype = n_named_args;
8598 /* Check for a longcall attribute. */
8599 if ((!fntype && rs6000_default_long_calls)
8600 || (fntype
8601 && lookup_attribute ("longcall", TYPE_ATTRIBUTES (fntype))
8602 && !lookup_attribute ("shortcall", TYPE_ATTRIBUTES (fntype))))
8603 cum->call_cookie |= CALL_LONG;
8605 if (TARGET_DEBUG_ARG)
8607 fprintf (stderr, "\ninit_cumulative_args:");
8608 if (fntype)
8610 tree ret_type = TREE_TYPE (fntype);
8611 fprintf (stderr, " ret code = %s,",
8612 get_tree_code_name (TREE_CODE (ret_type)));
8615 if (cum->call_cookie & CALL_LONG)
8616 fprintf (stderr, " longcall,");
8618 fprintf (stderr, " proto = %d, nargs = %d\n",
8619 cum->prototype, cum->nargs_prototype);
8622 #ifdef HAVE_AS_GNU_ATTRIBUTE
8623 if (DEFAULT_ABI == ABI_V4)
8625 cum->escapes = call_ABI_of_interest (fndecl);
8626 if (cum->escapes)
8628 tree return_type;
8630 if (fntype)
8632 return_type = TREE_TYPE (fntype);
8633 return_mode = TYPE_MODE (return_type);
8635 else
8636 return_type = lang_hooks.types.type_for_mode (return_mode, 0);
8638 if (return_type != NULL)
8640 if (TREE_CODE (return_type) == RECORD_TYPE
8641 && TYPE_TRANSPARENT_AGGR (return_type))
8643 return_type = TREE_TYPE (first_field (return_type));
8644 return_mode = TYPE_MODE (return_type);
8646 if (AGGREGATE_TYPE_P (return_type)
8647 && ((unsigned HOST_WIDE_INT) int_size_in_bytes (return_type)
8648 <= 8))
8649 rs6000_returns_struct = true;
8651 if (SCALAR_FLOAT_MODE_P (return_mode))
8652 rs6000_passes_float = true;
8653 else if (ALTIVEC_OR_VSX_VECTOR_MODE (return_mode)
8654 || SPE_VECTOR_MODE (return_mode))
8655 rs6000_passes_vector = true;
8658 #endif
8660 if (fntype
8661 && !TARGET_ALTIVEC
8662 && TARGET_ALTIVEC_ABI
8663 && ALTIVEC_VECTOR_MODE (TYPE_MODE (TREE_TYPE (fntype))))
8665 error ("cannot return value in vector register because"
8666 " altivec instructions are disabled, use -maltivec"
8667 " to enable them");
8671 /* Return true if TYPE must be passed on the stack and not in registers. */
8673 static bool
8674 rs6000_must_pass_in_stack (enum machine_mode mode, const_tree type)
8676 if (DEFAULT_ABI == ABI_AIX || TARGET_64BIT)
8677 return must_pass_in_stack_var_size (mode, type);
8678 else
8679 return must_pass_in_stack_var_size_or_pad (mode, type);
8682 /* If defined, a C expression which determines whether, and in which
8683 direction, to pad out an argument with extra space. The value
8684 should be of type `enum direction': either `upward' to pad above
8685 the argument, `downward' to pad below, or `none' to inhibit
8686 padding.
8688 For the AIX ABI structs are always stored left shifted in their
8689 argument slot. */
8691 enum direction
8692 function_arg_padding (enum machine_mode mode, const_tree type)
8694 #ifndef AGGREGATE_PADDING_FIXED
8695 #define AGGREGATE_PADDING_FIXED 0
8696 #endif
8697 #ifndef AGGREGATES_PAD_UPWARD_ALWAYS
8698 #define AGGREGATES_PAD_UPWARD_ALWAYS 0
8699 #endif
8701 if (!AGGREGATE_PADDING_FIXED)
8703 /* GCC used to pass structures of the same size as integer types as
8704 if they were in fact integers, ignoring FUNCTION_ARG_PADDING.
8705 i.e. Structures of size 1 or 2 (or 4 when TARGET_64BIT) were
8706 passed padded downward, except that -mstrict-align further
8707 muddied the water in that multi-component structures of 2 and 4
8708 bytes in size were passed padded upward.
8710 The following arranges for best compatibility with previous
8711 versions of gcc, but removes the -mstrict-align dependency. */
8712 if (BYTES_BIG_ENDIAN)
8714 HOST_WIDE_INT size = 0;
8716 if (mode == BLKmode)
8718 if (type && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
8719 size = int_size_in_bytes (type);
8721 else
8722 size = GET_MODE_SIZE (mode);
8724 if (size == 1 || size == 2 || size == 4)
8725 return downward;
8727 return upward;
8730 if (AGGREGATES_PAD_UPWARD_ALWAYS)
8732 if (type != 0 && AGGREGATE_TYPE_P (type))
8733 return upward;
8736 /* Fall back to the default. */
8737 return DEFAULT_FUNCTION_ARG_PADDING (mode, type);
8740 /* If defined, a C expression that gives the alignment boundary, in bits,
8741 of an argument with the specified mode and type. If it is not defined,
8742 PARM_BOUNDARY is used for all arguments.
8744 V.4 wants long longs and doubles to be double word aligned. Just
8745 testing the mode size is a boneheaded way to do this as it means
8746 that other types such as complex int are also double word aligned.
8747 However, we're stuck with this because changing the ABI might break
8748 existing library interfaces.
8750 Doubleword align SPE vectors.
8751 Quadword align Altivec/VSX vectors.
8752 Quadword align large synthetic vector types. */
8754 static unsigned int
8755 rs6000_function_arg_boundary (enum machine_mode mode, const_tree type)
8757 if (DEFAULT_ABI == ABI_V4
8758 && (GET_MODE_SIZE (mode) == 8
8759 || (TARGET_HARD_FLOAT
8760 && TARGET_FPRS
8761 && (mode == TFmode || mode == TDmode))))
8762 return 64;
8763 else if (SPE_VECTOR_MODE (mode)
8764 || (type && TREE_CODE (type) == VECTOR_TYPE
8765 && int_size_in_bytes (type) >= 8
8766 && int_size_in_bytes (type) < 16))
8767 return 64;
8768 else if (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
8769 || (type && TREE_CODE (type) == VECTOR_TYPE
8770 && int_size_in_bytes (type) >= 16))
8771 return 128;
8772 else if (((TARGET_MACHO && rs6000_darwin64_abi)
8773 || (DEFAULT_ABI == ABI_AIX && !rs6000_compat_align_parm))
8774 && mode == BLKmode
8775 && type && TYPE_ALIGN (type) > 64)
8776 return 128;
8777 else
8778 return PARM_BOUNDARY;
8781 /* For a function parm of MODE and TYPE, return the starting word in
8782 the parameter area. NWORDS of the parameter area are already used. */
8784 static unsigned int
8785 rs6000_parm_start (enum machine_mode mode, const_tree type,
8786 unsigned int nwords)
8788 unsigned int align;
8789 unsigned int parm_offset;
8791 align = rs6000_function_arg_boundary (mode, type) / PARM_BOUNDARY - 1;
8792 parm_offset = DEFAULT_ABI == ABI_V4 ? 2 : 6;
8793 return nwords + (-(parm_offset + nwords) & align);
8796 /* Compute the size (in words) of a function argument. */
8798 static unsigned long
8799 rs6000_arg_size (enum machine_mode mode, const_tree type)
8801 unsigned long size;
8803 if (mode != BLKmode)
8804 size = GET_MODE_SIZE (mode);
8805 else
8806 size = int_size_in_bytes (type);
8808 if (TARGET_32BIT)
8809 return (size + 3) >> 2;
8810 else
8811 return (size + 7) >> 3;
8814 /* Use this to flush pending int fields. */
8816 static void
8817 rs6000_darwin64_record_arg_advance_flush (CUMULATIVE_ARGS *cum,
8818 HOST_WIDE_INT bitpos, int final)
8820 unsigned int startbit, endbit;
8821 int intregs, intoffset;
8822 enum machine_mode mode;
8824 /* Handle the situations where a float is taking up the first half
8825 of the GPR, and the other half is empty (typically due to
8826 alignment restrictions). We can detect this by a 8-byte-aligned
8827 int field, or by seeing that this is the final flush for this
8828 argument. Count the word and continue on. */
8829 if (cum->floats_in_gpr == 1
8830 && (cum->intoffset % 64 == 0
8831 || (cum->intoffset == -1 && final)))
8833 cum->words++;
8834 cum->floats_in_gpr = 0;
8837 if (cum->intoffset == -1)
8838 return;
8840 intoffset = cum->intoffset;
8841 cum->intoffset = -1;
8842 cum->floats_in_gpr = 0;
8844 if (intoffset % BITS_PER_WORD != 0)
8846 mode = mode_for_size (BITS_PER_WORD - intoffset % BITS_PER_WORD,
8847 MODE_INT, 0);
8848 if (mode == BLKmode)
8850 /* We couldn't find an appropriate mode, which happens,
8851 e.g., in packed structs when there are 3 bytes to load.
8852 Back intoffset back to the beginning of the word in this
8853 case. */
8854 intoffset = intoffset & -BITS_PER_WORD;
8858 startbit = intoffset & -BITS_PER_WORD;
8859 endbit = (bitpos + BITS_PER_WORD - 1) & -BITS_PER_WORD;
8860 intregs = (endbit - startbit) / BITS_PER_WORD;
8861 cum->words += intregs;
8862 /* words should be unsigned. */
8863 if ((unsigned)cum->words < (endbit/BITS_PER_WORD))
8865 int pad = (endbit/BITS_PER_WORD) - cum->words;
8866 cum->words += pad;
8870 /* The darwin64 ABI calls for us to recurse down through structs,
8871 looking for elements passed in registers. Unfortunately, we have
8872 to track int register count here also because of misalignments
8873 in powerpc alignment mode. */
8875 static void
8876 rs6000_darwin64_record_arg_advance_recurse (CUMULATIVE_ARGS *cum,
8877 const_tree type,
8878 HOST_WIDE_INT startbitpos)
8880 tree f;
8882 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
8883 if (TREE_CODE (f) == FIELD_DECL)
8885 HOST_WIDE_INT bitpos = startbitpos;
8886 tree ftype = TREE_TYPE (f);
8887 enum machine_mode mode;
8888 if (ftype == error_mark_node)
8889 continue;
8890 mode = TYPE_MODE (ftype);
8892 if (DECL_SIZE (f) != 0
8893 && host_integerp (bit_position (f), 1))
8894 bitpos += int_bit_position (f);
8896 /* ??? FIXME: else assume zero offset. */
8898 if (TREE_CODE (ftype) == RECORD_TYPE)
8899 rs6000_darwin64_record_arg_advance_recurse (cum, ftype, bitpos);
8900 else if (USE_FP_FOR_ARG_P (cum, mode, ftype))
8902 unsigned n_fpregs = (GET_MODE_SIZE (mode) + 7) >> 3;
8903 rs6000_darwin64_record_arg_advance_flush (cum, bitpos, 0);
8904 cum->fregno += n_fpregs;
8905 /* Single-precision floats present a special problem for
8906 us, because they are smaller than an 8-byte GPR, and so
8907 the structure-packing rules combined with the standard
8908 varargs behavior mean that we want to pack float/float
8909 and float/int combinations into a single register's
8910 space. This is complicated by the arg advance flushing,
8911 which works on arbitrarily large groups of int-type
8912 fields. */
8913 if (mode == SFmode)
8915 if (cum->floats_in_gpr == 1)
8917 /* Two floats in a word; count the word and reset
8918 the float count. */
8919 cum->words++;
8920 cum->floats_in_gpr = 0;
8922 else if (bitpos % 64 == 0)
8924 /* A float at the beginning of an 8-byte word;
8925 count it and put off adjusting cum->words until
8926 we see if a arg advance flush is going to do it
8927 for us. */
8928 cum->floats_in_gpr++;
8930 else
8932 /* The float is at the end of a word, preceded
8933 by integer fields, so the arg advance flush
8934 just above has already set cum->words and
8935 everything is taken care of. */
8938 else
8939 cum->words += n_fpregs;
8941 else if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, 1))
8943 rs6000_darwin64_record_arg_advance_flush (cum, bitpos, 0);
8944 cum->vregno++;
8945 cum->words += 2;
8947 else if (cum->intoffset == -1)
8948 cum->intoffset = bitpos;
8952 /* Check for an item that needs to be considered specially under the darwin 64
8953 bit ABI. These are record types where the mode is BLK or the structure is
8954 8 bytes in size. */
8955 static int
8956 rs6000_darwin64_struct_check_p (enum machine_mode mode, const_tree type)
8958 return rs6000_darwin64_abi
8959 && ((mode == BLKmode
8960 && TREE_CODE (type) == RECORD_TYPE
8961 && int_size_in_bytes (type) > 0)
8962 || (type && TREE_CODE (type) == RECORD_TYPE
8963 && int_size_in_bytes (type) == 8)) ? 1 : 0;
8966 /* Update the data in CUM to advance over an argument
8967 of mode MODE and data type TYPE.
8968 (TYPE is null for libcalls where that information may not be available.)
8970 Note that for args passed by reference, function_arg will be called
8971 with MODE and TYPE set to that of the pointer to the arg, not the arg
8972 itself. */
8974 static void
8975 rs6000_function_arg_advance_1 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
8976 const_tree type, bool named, int depth)
8978 /* Only tick off an argument if we're not recursing. */
8979 if (depth == 0)
8980 cum->nargs_prototype--;
8982 #ifdef HAVE_AS_GNU_ATTRIBUTE
8983 if (DEFAULT_ABI == ABI_V4
8984 && cum->escapes)
8986 if (SCALAR_FLOAT_MODE_P (mode))
8987 rs6000_passes_float = true;
8988 else if (named && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
8989 rs6000_passes_vector = true;
8990 else if (SPE_VECTOR_MODE (mode)
8991 && !cum->stdarg
8992 && cum->sysv_gregno <= GP_ARG_MAX_REG)
8993 rs6000_passes_vector = true;
8995 #endif
8997 if (TARGET_ALTIVEC_ABI
8998 && (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
8999 || (type && TREE_CODE (type) == VECTOR_TYPE
9000 && int_size_in_bytes (type) == 16)))
9002 bool stack = false;
9004 if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, named))
9006 cum->vregno++;
9007 if (!TARGET_ALTIVEC)
9008 error ("cannot pass argument in vector register because"
9009 " altivec instructions are disabled, use -maltivec"
9010 " to enable them");
9012 /* PowerPC64 Linux and AIX allocate GPRs for a vector argument
9013 even if it is going to be passed in a vector register.
9014 Darwin does the same for variable-argument functions. */
9015 if ((DEFAULT_ABI == ABI_AIX && TARGET_64BIT)
9016 || (cum->stdarg && DEFAULT_ABI != ABI_V4))
9017 stack = true;
9019 else
9020 stack = true;
9022 if (stack)
9024 int align;
9026 /* Vector parameters must be 16-byte aligned. This places
9027 them at 2 mod 4 in terms of words in 32-bit mode, since
9028 the parameter save area starts at offset 24 from the
9029 stack. In 64-bit mode, they just have to start on an
9030 even word, since the parameter save area is 16-byte
9031 aligned. Space for GPRs is reserved even if the argument
9032 will be passed in memory. */
9033 if (TARGET_32BIT)
9034 align = (2 - cum->words) & 3;
9035 else
9036 align = cum->words & 1;
9037 cum->words += align + rs6000_arg_size (mode, type);
9039 if (TARGET_DEBUG_ARG)
9041 fprintf (stderr, "function_adv: words = %2d, align=%d, ",
9042 cum->words, align);
9043 fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s\n",
9044 cum->nargs_prototype, cum->prototype,
9045 GET_MODE_NAME (mode));
9049 else if (TARGET_SPE_ABI && TARGET_SPE && SPE_VECTOR_MODE (mode)
9050 && !cum->stdarg
9051 && cum->sysv_gregno <= GP_ARG_MAX_REG)
9052 cum->sysv_gregno++;
9054 else if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
9056 int size = int_size_in_bytes (type);
9057 /* Variable sized types have size == -1 and are
9058 treated as if consisting entirely of ints.
9059 Pad to 16 byte boundary if needed. */
9060 if (TYPE_ALIGN (type) >= 2 * BITS_PER_WORD
9061 && (cum->words % 2) != 0)
9062 cum->words++;
9063 /* For varargs, we can just go up by the size of the struct. */
9064 if (!named)
9065 cum->words += (size + 7) / 8;
9066 else
9068 /* It is tempting to say int register count just goes up by
9069 sizeof(type)/8, but this is wrong in a case such as
9070 { int; double; int; } [powerpc alignment]. We have to
9071 grovel through the fields for these too. */
9072 cum->intoffset = 0;
9073 cum->floats_in_gpr = 0;
9074 rs6000_darwin64_record_arg_advance_recurse (cum, type, 0);
9075 rs6000_darwin64_record_arg_advance_flush (cum,
9076 size * BITS_PER_UNIT, 1);
9078 if (TARGET_DEBUG_ARG)
9080 fprintf (stderr, "function_adv: words = %2d, align=%d, size=%d",
9081 cum->words, TYPE_ALIGN (type), size);
9082 fprintf (stderr,
9083 "nargs = %4d, proto = %d, mode = %4s (darwin64 abi)\n",
9084 cum->nargs_prototype, cum->prototype,
9085 GET_MODE_NAME (mode));
9088 else if (DEFAULT_ABI == ABI_V4)
9090 if (TARGET_HARD_FLOAT && TARGET_FPRS
9091 && ((TARGET_SINGLE_FLOAT && mode == SFmode)
9092 || (TARGET_DOUBLE_FLOAT && mode == DFmode)
9093 || (mode == TFmode && !TARGET_IEEEQUAD)
9094 || mode == SDmode || mode == DDmode || mode == TDmode))
9096 /* _Decimal128 must use an even/odd register pair. This assumes
9097 that the register number is odd when fregno is odd. */
9098 if (mode == TDmode && (cum->fregno % 2) == 1)
9099 cum->fregno++;
9101 if (cum->fregno + (mode == TFmode || mode == TDmode ? 1 : 0)
9102 <= FP_ARG_V4_MAX_REG)
9103 cum->fregno += (GET_MODE_SIZE (mode) + 7) >> 3;
9104 else
9106 cum->fregno = FP_ARG_V4_MAX_REG + 1;
9107 if (mode == DFmode || mode == TFmode
9108 || mode == DDmode || mode == TDmode)
9109 cum->words += cum->words & 1;
9110 cum->words += rs6000_arg_size (mode, type);
9113 else
9115 int n_words = rs6000_arg_size (mode, type);
9116 int gregno = cum->sysv_gregno;
9118 /* Long long and SPE vectors are put in (r3,r4), (r5,r6),
9119 (r7,r8) or (r9,r10). As does any other 2 word item such
9120 as complex int due to a historical mistake. */
9121 if (n_words == 2)
9122 gregno += (1 - gregno) & 1;
9124 /* Multi-reg args are not split between registers and stack. */
9125 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
9127 /* Long long and SPE vectors are aligned on the stack.
9128 So are other 2 word items such as complex int due to
9129 a historical mistake. */
9130 if (n_words == 2)
9131 cum->words += cum->words & 1;
9132 cum->words += n_words;
9135 /* Note: continuing to accumulate gregno past when we've started
9136 spilling to the stack indicates the fact that we've started
9137 spilling to the stack to expand_builtin_saveregs. */
9138 cum->sysv_gregno = gregno + n_words;
9141 if (TARGET_DEBUG_ARG)
9143 fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
9144 cum->words, cum->fregno);
9145 fprintf (stderr, "gregno = %2d, nargs = %4d, proto = %d, ",
9146 cum->sysv_gregno, cum->nargs_prototype, cum->prototype);
9147 fprintf (stderr, "mode = %4s, named = %d\n",
9148 GET_MODE_NAME (mode), named);
9151 else
9153 int n_words = rs6000_arg_size (mode, type);
9154 int start_words = cum->words;
9155 int align_words = rs6000_parm_start (mode, type, start_words);
9157 cum->words = align_words + n_words;
9159 if (SCALAR_FLOAT_MODE_P (mode)
9160 && TARGET_HARD_FLOAT && TARGET_FPRS)
9162 /* _Decimal128 must be passed in an even/odd float register pair.
9163 This assumes that the register number is odd when fregno is
9164 odd. */
9165 if (mode == TDmode && (cum->fregno % 2) == 1)
9166 cum->fregno++;
9167 cum->fregno += (GET_MODE_SIZE (mode) + 7) >> 3;
9170 if (TARGET_DEBUG_ARG)
9172 fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
9173 cum->words, cum->fregno);
9174 fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s, ",
9175 cum->nargs_prototype, cum->prototype, GET_MODE_NAME (mode));
9176 fprintf (stderr, "named = %d, align = %d, depth = %d\n",
9177 named, align_words - start_words, depth);
9182 static void
9183 rs6000_function_arg_advance (cumulative_args_t cum, enum machine_mode mode,
9184 const_tree type, bool named)
9186 rs6000_function_arg_advance_1 (get_cumulative_args (cum), mode, type, named,
9190 static rtx
9191 spe_build_register_parallel (enum machine_mode mode, int gregno)
9193 rtx r1, r3, r5, r7;
9195 switch (mode)
9197 case DFmode:
9198 r1 = gen_rtx_REG (DImode, gregno);
9199 r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
9200 return gen_rtx_PARALLEL (mode, gen_rtvec (1, r1));
9202 case DCmode:
9203 case TFmode:
9204 r1 = gen_rtx_REG (DImode, gregno);
9205 r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
9206 r3 = gen_rtx_REG (DImode, gregno + 2);
9207 r3 = gen_rtx_EXPR_LIST (VOIDmode, r3, GEN_INT (8));
9208 return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r3));
9210 case TCmode:
9211 r1 = gen_rtx_REG (DImode, gregno);
9212 r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
9213 r3 = gen_rtx_REG (DImode, gregno + 2);
9214 r3 = gen_rtx_EXPR_LIST (VOIDmode, r3, GEN_INT (8));
9215 r5 = gen_rtx_REG (DImode, gregno + 4);
9216 r5 = gen_rtx_EXPR_LIST (VOIDmode, r5, GEN_INT (16));
9217 r7 = gen_rtx_REG (DImode, gregno + 6);
9218 r7 = gen_rtx_EXPR_LIST (VOIDmode, r7, GEN_INT (24));
9219 return gen_rtx_PARALLEL (mode, gen_rtvec (4, r1, r3, r5, r7));
9221 default:
9222 gcc_unreachable ();
9226 /* Determine where to put a SIMD argument on the SPE. */
9227 static rtx
9228 rs6000_spe_function_arg (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
9229 const_tree type)
9231 int gregno = cum->sysv_gregno;
9233 /* On E500 v2, double arithmetic is done on the full 64-bit GPR, but
9234 are passed and returned in a pair of GPRs for ABI compatibility. */
9235 if (TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode
9236 || mode == DCmode || mode == TCmode))
9238 int n_words = rs6000_arg_size (mode, type);
9240 /* Doubles go in an odd/even register pair (r5/r6, etc). */
9241 if (mode == DFmode)
9242 gregno += (1 - gregno) & 1;
9244 /* Multi-reg args are not split between registers and stack. */
9245 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
9246 return NULL_RTX;
9248 return spe_build_register_parallel (mode, gregno);
9250 if (cum->stdarg)
9252 int n_words = rs6000_arg_size (mode, type);
9254 /* SPE vectors are put in odd registers. */
9255 if (n_words == 2 && (gregno & 1) == 0)
9256 gregno += 1;
9258 if (gregno + n_words - 1 <= GP_ARG_MAX_REG)
9260 rtx r1, r2;
9261 enum machine_mode m = SImode;
9263 r1 = gen_rtx_REG (m, gregno);
9264 r1 = gen_rtx_EXPR_LIST (m, r1, const0_rtx);
9265 r2 = gen_rtx_REG (m, gregno + 1);
9266 r2 = gen_rtx_EXPR_LIST (m, r2, GEN_INT (4));
9267 return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
9269 else
9270 return NULL_RTX;
9272 else
9274 if (gregno <= GP_ARG_MAX_REG)
9275 return gen_rtx_REG (mode, gregno);
9276 else
9277 return NULL_RTX;
9281 /* A subroutine of rs6000_darwin64_record_arg. Assign the bits of the
9282 structure between cum->intoffset and bitpos to integer registers. */
9284 static void
9285 rs6000_darwin64_record_arg_flush (CUMULATIVE_ARGS *cum,
9286 HOST_WIDE_INT bitpos, rtx rvec[], int *k)
9288 enum machine_mode mode;
9289 unsigned int regno;
9290 unsigned int startbit, endbit;
9291 int this_regno, intregs, intoffset;
9292 rtx reg;
9294 if (cum->intoffset == -1)
9295 return;
9297 intoffset = cum->intoffset;
9298 cum->intoffset = -1;
9300 /* If this is the trailing part of a word, try to only load that
9301 much into the register. Otherwise load the whole register. Note
9302 that in the latter case we may pick up unwanted bits. It's not a
9303 problem at the moment but may wish to revisit. */
9305 if (intoffset % BITS_PER_WORD != 0)
9307 mode = mode_for_size (BITS_PER_WORD - intoffset % BITS_PER_WORD,
9308 MODE_INT, 0);
9309 if (mode == BLKmode)
9311 /* We couldn't find an appropriate mode, which happens,
9312 e.g., in packed structs when there are 3 bytes to load.
9313 Back intoffset back to the beginning of the word in this
9314 case. */
9315 intoffset = intoffset & -BITS_PER_WORD;
9316 mode = word_mode;
9319 else
9320 mode = word_mode;
9322 startbit = intoffset & -BITS_PER_WORD;
9323 endbit = (bitpos + BITS_PER_WORD - 1) & -BITS_PER_WORD;
9324 intregs = (endbit - startbit) / BITS_PER_WORD;
9325 this_regno = cum->words + intoffset / BITS_PER_WORD;
9327 if (intregs > 0 && intregs > GP_ARG_NUM_REG - this_regno)
9328 cum->use_stack = 1;
9330 intregs = MIN (intregs, GP_ARG_NUM_REG - this_regno);
9331 if (intregs <= 0)
9332 return;
9334 intoffset /= BITS_PER_UNIT;
9337 regno = GP_ARG_MIN_REG + this_regno;
9338 reg = gen_rtx_REG (mode, regno);
9339 rvec[(*k)++] =
9340 gen_rtx_EXPR_LIST (VOIDmode, reg, GEN_INT (intoffset));
9342 this_regno += 1;
9343 intoffset = (intoffset | (UNITS_PER_WORD-1)) + 1;
9344 mode = word_mode;
9345 intregs -= 1;
9347 while (intregs > 0);
9350 /* Recursive workhorse for the following. */
9352 static void
9353 rs6000_darwin64_record_arg_recurse (CUMULATIVE_ARGS *cum, const_tree type,
9354 HOST_WIDE_INT startbitpos, rtx rvec[],
9355 int *k)
9357 tree f;
9359 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
9360 if (TREE_CODE (f) == FIELD_DECL)
9362 HOST_WIDE_INT bitpos = startbitpos;
9363 tree ftype = TREE_TYPE (f);
9364 enum machine_mode mode;
9365 if (ftype == error_mark_node)
9366 continue;
9367 mode = TYPE_MODE (ftype);
9369 if (DECL_SIZE (f) != 0
9370 && host_integerp (bit_position (f), 1))
9371 bitpos += int_bit_position (f);
9373 /* ??? FIXME: else assume zero offset. */
9375 if (TREE_CODE (ftype) == RECORD_TYPE)
9376 rs6000_darwin64_record_arg_recurse (cum, ftype, bitpos, rvec, k);
9377 else if (cum->named && USE_FP_FOR_ARG_P (cum, mode, ftype))
9379 unsigned n_fpreg = (GET_MODE_SIZE (mode) + 7) >> 3;
9380 #if 0
9381 switch (mode)
9383 case SCmode: mode = SFmode; break;
9384 case DCmode: mode = DFmode; break;
9385 case TCmode: mode = TFmode; break;
9386 default: break;
9388 #endif
9389 rs6000_darwin64_record_arg_flush (cum, bitpos, rvec, k);
9390 if (cum->fregno + n_fpreg > FP_ARG_MAX_REG + 1)
9392 gcc_assert (cum->fregno == FP_ARG_MAX_REG
9393 && (mode == TFmode || mode == TDmode));
9394 /* Long double or _Decimal128 split over regs and memory. */
9395 mode = DECIMAL_FLOAT_MODE_P (mode) ? DDmode : DFmode;
9396 cum->use_stack=1;
9398 rvec[(*k)++]
9399 = gen_rtx_EXPR_LIST (VOIDmode,
9400 gen_rtx_REG (mode, cum->fregno++),
9401 GEN_INT (bitpos / BITS_PER_UNIT));
9402 if (mode == TFmode || mode == TDmode)
9403 cum->fregno++;
9405 else if (cum->named && USE_ALTIVEC_FOR_ARG_P (cum, mode, ftype, 1))
9407 rs6000_darwin64_record_arg_flush (cum, bitpos, rvec, k);
9408 rvec[(*k)++]
9409 = gen_rtx_EXPR_LIST (VOIDmode,
9410 gen_rtx_REG (mode, cum->vregno++),
9411 GEN_INT (bitpos / BITS_PER_UNIT));
9413 else if (cum->intoffset == -1)
9414 cum->intoffset = bitpos;
9418 /* For the darwin64 ABI, we want to construct a PARALLEL consisting of
9419 the register(s) to be used for each field and subfield of a struct
9420 being passed by value, along with the offset of where the
9421 register's value may be found in the block. FP fields go in FP
9422 register, vector fields go in vector registers, and everything
9423 else goes in int registers, packed as in memory.
9425 This code is also used for function return values. RETVAL indicates
9426 whether this is the case.
9428 Much of this is taken from the SPARC V9 port, which has a similar
9429 calling convention. */
9431 static rtx
9432 rs6000_darwin64_record_arg (CUMULATIVE_ARGS *orig_cum, const_tree type,
9433 bool named, bool retval)
9435 rtx rvec[FIRST_PSEUDO_REGISTER];
9436 int k = 1, kbase = 1;
9437 HOST_WIDE_INT typesize = int_size_in_bytes (type);
9438 /* This is a copy; modifications are not visible to our caller. */
9439 CUMULATIVE_ARGS copy_cum = *orig_cum;
9440 CUMULATIVE_ARGS *cum = &copy_cum;
9442 /* Pad to 16 byte boundary if needed. */
9443 if (!retval && TYPE_ALIGN (type) >= 2 * BITS_PER_WORD
9444 && (cum->words % 2) != 0)
9445 cum->words++;
9447 cum->intoffset = 0;
9448 cum->use_stack = 0;
9449 cum->named = named;
9451 /* Put entries into rvec[] for individual FP and vector fields, and
9452 for the chunks of memory that go in int regs. Note we start at
9453 element 1; 0 is reserved for an indication of using memory, and
9454 may or may not be filled in below. */
9455 rs6000_darwin64_record_arg_recurse (cum, type, /* startbit pos= */ 0, rvec, &k);
9456 rs6000_darwin64_record_arg_flush (cum, typesize * BITS_PER_UNIT, rvec, &k);
9458 /* If any part of the struct went on the stack put all of it there.
9459 This hack is because the generic code for
9460 FUNCTION_ARG_PARTIAL_NREGS cannot handle cases where the register
9461 parts of the struct are not at the beginning. */
9462 if (cum->use_stack)
9464 if (retval)
9465 return NULL_RTX; /* doesn't go in registers at all */
9466 kbase = 0;
9467 rvec[0] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
9469 if (k > 1 || cum->use_stack)
9470 return gen_rtx_PARALLEL (BLKmode, gen_rtvec_v (k - kbase, &rvec[kbase]));
9471 else
9472 return NULL_RTX;
9475 /* Determine where to place an argument in 64-bit mode with 32-bit ABI. */
9477 static rtx
9478 rs6000_mixed_function_arg (enum machine_mode mode, const_tree type,
9479 int align_words)
9481 int n_units;
9482 int i, k;
9483 rtx rvec[GP_ARG_NUM_REG + 1];
9485 if (align_words >= GP_ARG_NUM_REG)
9486 return NULL_RTX;
9488 n_units = rs6000_arg_size (mode, type);
9490 /* Optimize the simple case where the arg fits in one gpr, except in
9491 the case of BLKmode due to assign_parms assuming that registers are
9492 BITS_PER_WORD wide. */
9493 if (n_units == 0
9494 || (n_units == 1 && mode != BLKmode))
9495 return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
9497 k = 0;
9498 if (align_words + n_units > GP_ARG_NUM_REG)
9499 /* Not all of the arg fits in gprs. Say that it goes in memory too,
9500 using a magic NULL_RTX component.
9501 This is not strictly correct. Only some of the arg belongs in
9502 memory, not all of it. However, the normal scheme using
9503 function_arg_partial_nregs can result in unusual subregs, eg.
9504 (subreg:SI (reg:DF) 4), which are not handled well. The code to
9505 store the whole arg to memory is often more efficient than code
9506 to store pieces, and we know that space is available in the right
9507 place for the whole arg. */
9508 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
9510 i = 0;
9513 rtx r = gen_rtx_REG (SImode, GP_ARG_MIN_REG + align_words);
9514 rtx off = GEN_INT (i++ * 4);
9515 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
9517 while (++align_words < GP_ARG_NUM_REG && --n_units != 0);
9519 return gen_rtx_PARALLEL (mode, gen_rtvec_v (k, rvec));
9522 /* Determine where to put an argument to a function.
9523 Value is zero to push the argument on the stack,
9524 or a hard register in which to store the argument.
9526 MODE is the argument's machine mode.
9527 TYPE is the data type of the argument (as a tree).
9528 This is null for libcalls where that information may
9529 not be available.
9530 CUM is a variable of type CUMULATIVE_ARGS which gives info about
9531 the preceding args and about the function being called. It is
9532 not modified in this routine.
9533 NAMED is nonzero if this argument is a named parameter
9534 (otherwise it is an extra parameter matching an ellipsis).
9536 On RS/6000 the first eight words of non-FP are normally in registers
9537 and the rest are pushed. Under AIX, the first 13 FP args are in registers.
9538 Under V.4, the first 8 FP args are in registers.
9540 If this is floating-point and no prototype is specified, we use
9541 both an FP and integer register (or possibly FP reg and stack). Library
9542 functions (when CALL_LIBCALL is set) always have the proper types for args,
9543 so we can pass the FP value just in one register. emit_library_function
9544 doesn't support PARALLEL anyway.
9546 Note that for args passed by reference, function_arg will be called
9547 with MODE and TYPE set to that of the pointer to the arg, not the arg
9548 itself. */
9550 static rtx
9551 rs6000_function_arg (cumulative_args_t cum_v, enum machine_mode mode,
9552 const_tree type, bool named)
9554 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
9555 enum rs6000_abi abi = DEFAULT_ABI;
9557 /* Return a marker to indicate whether CR1 needs to set or clear the
9558 bit that V.4 uses to say fp args were passed in registers.
9559 Assume that we don't need the marker for software floating point,
9560 or compiler generated library calls. */
9561 if (mode == VOIDmode)
9563 if (abi == ABI_V4
9564 && (cum->call_cookie & CALL_LIBCALL) == 0
9565 && (cum->stdarg
9566 || (cum->nargs_prototype < 0
9567 && (cum->prototype || TARGET_NO_PROTOTYPE))))
9569 /* For the SPE, we need to crxor CR6 always. */
9570 if (TARGET_SPE_ABI)
9571 return GEN_INT (cum->call_cookie | CALL_V4_SET_FP_ARGS);
9572 else if (TARGET_HARD_FLOAT && TARGET_FPRS)
9573 return GEN_INT (cum->call_cookie
9574 | ((cum->fregno == FP_ARG_MIN_REG)
9575 ? CALL_V4_SET_FP_ARGS
9576 : CALL_V4_CLEAR_FP_ARGS));
9579 return GEN_INT (cum->call_cookie & ~CALL_LIBCALL);
9582 if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
9584 rtx rslt = rs6000_darwin64_record_arg (cum, type, named, /*retval= */false);
9585 if (rslt != NULL_RTX)
9586 return rslt;
9587 /* Else fall through to usual handling. */
9590 if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, named))
9591 if (TARGET_64BIT && ! cum->prototype)
9593 /* Vector parameters get passed in vector register
9594 and also in GPRs or memory, in absence of prototype. */
9595 int align_words;
9596 rtx slot;
9597 align_words = (cum->words + 1) & ~1;
9599 if (align_words >= GP_ARG_NUM_REG)
9601 slot = NULL_RTX;
9603 else
9605 slot = gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
9607 return gen_rtx_PARALLEL (mode,
9608 gen_rtvec (2,
9609 gen_rtx_EXPR_LIST (VOIDmode,
9610 slot, const0_rtx),
9611 gen_rtx_EXPR_LIST (VOIDmode,
9612 gen_rtx_REG (mode, cum->vregno),
9613 const0_rtx)));
9615 else
9616 return gen_rtx_REG (mode, cum->vregno);
9617 else if (TARGET_ALTIVEC_ABI
9618 && (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
9619 || (type && TREE_CODE (type) == VECTOR_TYPE
9620 && int_size_in_bytes (type) == 16)))
9622 if (named || abi == ABI_V4)
9623 return NULL_RTX;
9624 else
9626 /* Vector parameters to varargs functions under AIX or Darwin
9627 get passed in memory and possibly also in GPRs. */
9628 int align, align_words, n_words;
9629 enum machine_mode part_mode;
9631 /* Vector parameters must be 16-byte aligned. This places them at
9632 2 mod 4 in terms of words in 32-bit mode, since the parameter
9633 save area starts at offset 24 from the stack. In 64-bit mode,
9634 they just have to start on an even word, since the parameter
9635 save area is 16-byte aligned. */
9636 if (TARGET_32BIT)
9637 align = (2 - cum->words) & 3;
9638 else
9639 align = cum->words & 1;
9640 align_words = cum->words + align;
9642 /* Out of registers? Memory, then. */
9643 if (align_words >= GP_ARG_NUM_REG)
9644 return NULL_RTX;
9646 if (TARGET_32BIT && TARGET_POWERPC64)
9647 return rs6000_mixed_function_arg (mode, type, align_words);
9649 /* The vector value goes in GPRs. Only the part of the
9650 value in GPRs is reported here. */
9651 part_mode = mode;
9652 n_words = rs6000_arg_size (mode, type);
9653 if (align_words + n_words > GP_ARG_NUM_REG)
9654 /* Fortunately, there are only two possibilities, the value
9655 is either wholly in GPRs or half in GPRs and half not. */
9656 part_mode = DImode;
9658 return gen_rtx_REG (part_mode, GP_ARG_MIN_REG + align_words);
9661 else if (TARGET_SPE_ABI && TARGET_SPE
9662 && (SPE_VECTOR_MODE (mode)
9663 || (TARGET_E500_DOUBLE && (mode == DFmode
9664 || mode == DCmode
9665 || mode == TFmode
9666 || mode == TCmode))))
9667 return rs6000_spe_function_arg (cum, mode, type);
9669 else if (abi == ABI_V4)
9671 if (TARGET_HARD_FLOAT && TARGET_FPRS
9672 && ((TARGET_SINGLE_FLOAT && mode == SFmode)
9673 || (TARGET_DOUBLE_FLOAT && mode == DFmode)
9674 || (mode == TFmode && !TARGET_IEEEQUAD)
9675 || mode == SDmode || mode == DDmode || mode == TDmode))
9677 /* _Decimal128 must use an even/odd register pair. This assumes
9678 that the register number is odd when fregno is odd. */
9679 if (mode == TDmode && (cum->fregno % 2) == 1)
9680 cum->fregno++;
9682 if (cum->fregno + (mode == TFmode || mode == TDmode ? 1 : 0)
9683 <= FP_ARG_V4_MAX_REG)
9684 return gen_rtx_REG (mode, cum->fregno);
9685 else
9686 return NULL_RTX;
9688 else
9690 int n_words = rs6000_arg_size (mode, type);
9691 int gregno = cum->sysv_gregno;
9693 /* Long long and SPE vectors are put in (r3,r4), (r5,r6),
9694 (r7,r8) or (r9,r10). As does any other 2 word item such
9695 as complex int due to a historical mistake. */
9696 if (n_words == 2)
9697 gregno += (1 - gregno) & 1;
9699 /* Multi-reg args are not split between registers and stack. */
9700 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
9701 return NULL_RTX;
9703 if (TARGET_32BIT && TARGET_POWERPC64)
9704 return rs6000_mixed_function_arg (mode, type,
9705 gregno - GP_ARG_MIN_REG);
9706 return gen_rtx_REG (mode, gregno);
9709 else
9711 int align_words = rs6000_parm_start (mode, type, cum->words);
9713 /* _Decimal128 must be passed in an even/odd float register pair.
9714 This assumes that the register number is odd when fregno is odd. */
9715 if (mode == TDmode && (cum->fregno % 2) == 1)
9716 cum->fregno++;
9718 if (USE_FP_FOR_ARG_P (cum, mode, type))
9720 rtx rvec[GP_ARG_NUM_REG + 1];
9721 rtx r;
9722 int k;
9723 bool needs_psave;
9724 enum machine_mode fmode = mode;
9725 unsigned long n_fpreg = (GET_MODE_SIZE (mode) + 7) >> 3;
9727 if (cum->fregno + n_fpreg > FP_ARG_MAX_REG + 1)
9729 /* Currently, we only ever need one reg here because complex
9730 doubles are split. */
9731 gcc_assert (cum->fregno == FP_ARG_MAX_REG
9732 && (fmode == TFmode || fmode == TDmode));
9734 /* Long double or _Decimal128 split over regs and memory. */
9735 fmode = DECIMAL_FLOAT_MODE_P (fmode) ? DDmode : DFmode;
9738 /* Do we also need to pass this arg in the parameter save
9739 area? */
9740 needs_psave = (type
9741 && (cum->nargs_prototype <= 0
9742 || (DEFAULT_ABI == ABI_AIX
9743 && TARGET_XL_COMPAT
9744 && align_words >= GP_ARG_NUM_REG)));
9746 if (!needs_psave && mode == fmode)
9747 return gen_rtx_REG (fmode, cum->fregno);
9749 k = 0;
9750 if (needs_psave)
9752 /* Describe the part that goes in gprs or the stack.
9753 This piece must come first, before the fprs. */
9754 if (align_words < GP_ARG_NUM_REG)
9756 unsigned long n_words = rs6000_arg_size (mode, type);
9758 if (align_words + n_words > GP_ARG_NUM_REG
9759 || (TARGET_32BIT && TARGET_POWERPC64))
9761 /* If this is partially on the stack, then we only
9762 include the portion actually in registers here. */
9763 enum machine_mode rmode = TARGET_32BIT ? SImode : DImode;
9764 rtx off;
9765 int i = 0;
9766 if (align_words + n_words > GP_ARG_NUM_REG)
9767 /* Not all of the arg fits in gprs. Say that it
9768 goes in memory too, using a magic NULL_RTX
9769 component. Also see comment in
9770 rs6000_mixed_function_arg for why the normal
9771 function_arg_partial_nregs scheme doesn't work
9772 in this case. */
9773 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX,
9774 const0_rtx);
9777 r = gen_rtx_REG (rmode,
9778 GP_ARG_MIN_REG + align_words);
9779 off = GEN_INT (i++ * GET_MODE_SIZE (rmode));
9780 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
9782 while (++align_words < GP_ARG_NUM_REG && --n_words != 0);
9784 else
9786 /* The whole arg fits in gprs. */
9787 r = gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
9788 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, const0_rtx);
9791 else
9792 /* It's entirely in memory. */
9793 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
9796 /* Describe where this piece goes in the fprs. */
9797 r = gen_rtx_REG (fmode, cum->fregno);
9798 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, const0_rtx);
9800 return gen_rtx_PARALLEL (mode, gen_rtvec_v (k, rvec));
9802 else if (align_words < GP_ARG_NUM_REG)
9804 if (TARGET_32BIT && TARGET_POWERPC64)
9805 return rs6000_mixed_function_arg (mode, type, align_words);
9807 if (mode == BLKmode)
9808 mode = Pmode;
9810 return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
9812 else
9813 return NULL_RTX;
9817 /* For an arg passed partly in registers and partly in memory, this is
9818 the number of bytes passed in registers. For args passed entirely in
9819 registers or entirely in memory, zero. When an arg is described by a
9820 PARALLEL, perhaps using more than one register type, this function
9821 returns the number of bytes used by the first element of the PARALLEL. */
9823 static int
9824 rs6000_arg_partial_bytes (cumulative_args_t cum_v, enum machine_mode mode,
9825 tree type, bool named)
9827 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
9828 int ret = 0;
9829 int align_words;
9831 if (DEFAULT_ABI == ABI_V4)
9832 return 0;
9834 if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, named)
9835 && cum->nargs_prototype >= 0)
9836 return 0;
9838 /* In this complicated case we just disable the partial_nregs code. */
9839 if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
9840 return 0;
9842 align_words = rs6000_parm_start (mode, type, cum->words);
9844 if (USE_FP_FOR_ARG_P (cum, mode, type))
9846 /* If we are passing this arg in the fixed parameter save area
9847 (gprs or memory) as well as fprs, then this function should
9848 return the number of partial bytes passed in the parameter
9849 save area rather than partial bytes passed in fprs. */
9850 if (type
9851 && (cum->nargs_prototype <= 0
9852 || (DEFAULT_ABI == ABI_AIX
9853 && TARGET_XL_COMPAT
9854 && align_words >= GP_ARG_NUM_REG)))
9855 return 0;
9856 else if (cum->fregno + ((GET_MODE_SIZE (mode) + 7) >> 3)
9857 > FP_ARG_MAX_REG + 1)
9858 ret = (FP_ARG_MAX_REG + 1 - cum->fregno) * 8;
9859 else if (cum->nargs_prototype >= 0)
9860 return 0;
9863 if (align_words < GP_ARG_NUM_REG
9864 && GP_ARG_NUM_REG < align_words + rs6000_arg_size (mode, type))
9865 ret = (GP_ARG_NUM_REG - align_words) * (TARGET_32BIT ? 4 : 8);
9867 if (ret != 0 && TARGET_DEBUG_ARG)
9868 fprintf (stderr, "rs6000_arg_partial_bytes: %d\n", ret);
9870 return ret;
9873 /* A C expression that indicates when an argument must be passed by
9874 reference. If nonzero for an argument, a copy of that argument is
9875 made in memory and a pointer to the argument is passed instead of
9876 the argument itself. The pointer is passed in whatever way is
9877 appropriate for passing a pointer to that type.
9879 Under V.4, aggregates and long double are passed by reference.
9881 As an extension to all 32-bit ABIs, AltiVec vectors are passed by
9882 reference unless the AltiVec vector extension ABI is in force.
9884 As an extension to all ABIs, variable sized types are passed by
9885 reference. */
9887 static bool
9888 rs6000_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED,
9889 enum machine_mode mode, const_tree type,
9890 bool named ATTRIBUTE_UNUSED)
9892 if (DEFAULT_ABI == ABI_V4 && TARGET_IEEEQUAD && mode == TFmode)
9894 if (TARGET_DEBUG_ARG)
9895 fprintf (stderr, "function_arg_pass_by_reference: V4 long double\n");
9896 return 1;
9899 if (!type)
9900 return 0;
9902 #if HAVE_UPC_PTS_STRUCT_REP
9903 if (DEFAULT_ABI == ABI_V4 && POINTER_TYPE_P (type)
9904 && upc_shared_type_p (TREE_TYPE (type)))
9906 if (TARGET_DEBUG_ARG)
9907 fprintf (stderr,
9908 "function_arg_pass_by_reference: V4 UPC ptr to shared\n");
9909 return 1;
9911 #endif
9913 if (DEFAULT_ABI == ABI_V4 && AGGREGATE_TYPE_P (type))
9915 if (TARGET_DEBUG_ARG)
9916 fprintf (stderr, "function_arg_pass_by_reference: V4 aggregate\n");
9917 return 1;
9920 if (int_size_in_bytes (type) < 0)
9922 if (TARGET_DEBUG_ARG)
9923 fprintf (stderr, "function_arg_pass_by_reference: variable size\n");
9924 return 1;
9927 /* Allow -maltivec -mabi=no-altivec without warning. Altivec vector
9928 modes only exist for GCC vector types if -maltivec. */
9929 if (TARGET_32BIT && !TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
9931 if (TARGET_DEBUG_ARG)
9932 fprintf (stderr, "function_arg_pass_by_reference: AltiVec\n");
9933 return 1;
9936 /* Pass synthetic vectors in memory. */
9937 if (TREE_CODE (type) == VECTOR_TYPE
9938 && int_size_in_bytes (type) > (TARGET_ALTIVEC_ABI ? 16 : 8))
9940 static bool warned_for_pass_big_vectors = false;
9941 if (TARGET_DEBUG_ARG)
9942 fprintf (stderr, "function_arg_pass_by_reference: synthetic vector\n");
9943 if (!warned_for_pass_big_vectors)
9945 warning (0, "GCC vector passed by reference: "
9946 "non-standard ABI extension with no compatibility guarantee");
9947 warned_for_pass_big_vectors = true;
9949 return 1;
9952 return 0;
9955 static void
9956 rs6000_move_block_from_reg (int regno, rtx x, int nregs)
9958 int i;
9959 enum machine_mode reg_mode = TARGET_32BIT ? SImode : DImode;
9961 if (nregs == 0)
9962 return;
9964 for (i = 0; i < nregs; i++)
9966 rtx tem = adjust_address_nv (x, reg_mode, i * GET_MODE_SIZE (reg_mode));
9967 if (reload_completed)
9969 if (! strict_memory_address_p (reg_mode, XEXP (tem, 0)))
9970 tem = NULL_RTX;
9971 else
9972 tem = simplify_gen_subreg (reg_mode, x, BLKmode,
9973 i * GET_MODE_SIZE (reg_mode));
9975 else
9976 tem = replace_equiv_address (tem, XEXP (tem, 0));
9978 gcc_assert (tem);
9980 emit_move_insn (tem, gen_rtx_REG (reg_mode, regno + i));
9984 /* Perform any needed actions needed for a function that is receiving a
9985 variable number of arguments.
9987 CUM is as above.
9989 MODE and TYPE are the mode and type of the current parameter.
9991 PRETEND_SIZE is a variable that should be set to the amount of stack
9992 that must be pushed by the prolog to pretend that our caller pushed
9995 Normally, this macro will push all remaining incoming registers on the
9996 stack and set PRETEND_SIZE to the length of the registers pushed. */
9998 static void
9999 setup_incoming_varargs (cumulative_args_t cum, enum machine_mode mode,
10000 tree type, int *pretend_size ATTRIBUTE_UNUSED,
10001 int no_rtl)
10003 CUMULATIVE_ARGS next_cum;
10004 int reg_size = TARGET_32BIT ? 4 : 8;
10005 rtx save_area = NULL_RTX, mem;
10006 int first_reg_offset;
10007 alias_set_type set;
10009 /* Skip the last named argument. */
10010 next_cum = *get_cumulative_args (cum);
10011 rs6000_function_arg_advance_1 (&next_cum, mode, type, true, 0);
10013 if (DEFAULT_ABI == ABI_V4)
10015 first_reg_offset = next_cum.sysv_gregno - GP_ARG_MIN_REG;
10017 if (! no_rtl)
10019 int gpr_reg_num = 0, gpr_size = 0, fpr_size = 0;
10020 HOST_WIDE_INT offset = 0;
10022 /* Try to optimize the size of the varargs save area.
10023 The ABI requires that ap.reg_save_area is doubleword
10024 aligned, but we don't need to allocate space for all
10025 the bytes, only those to which we actually will save
10026 anything. */
10027 if (cfun->va_list_gpr_size && first_reg_offset < GP_ARG_NUM_REG)
10028 gpr_reg_num = GP_ARG_NUM_REG - first_reg_offset;
10029 if (TARGET_HARD_FLOAT && TARGET_FPRS
10030 && next_cum.fregno <= FP_ARG_V4_MAX_REG
10031 && cfun->va_list_fpr_size)
10033 if (gpr_reg_num)
10034 fpr_size = (next_cum.fregno - FP_ARG_MIN_REG)
10035 * UNITS_PER_FP_WORD;
10036 if (cfun->va_list_fpr_size
10037 < FP_ARG_V4_MAX_REG + 1 - next_cum.fregno)
10038 fpr_size += cfun->va_list_fpr_size * UNITS_PER_FP_WORD;
10039 else
10040 fpr_size += (FP_ARG_V4_MAX_REG + 1 - next_cum.fregno)
10041 * UNITS_PER_FP_WORD;
10043 if (gpr_reg_num)
10045 offset = -((first_reg_offset * reg_size) & ~7);
10046 if (!fpr_size && gpr_reg_num > cfun->va_list_gpr_size)
10048 gpr_reg_num = cfun->va_list_gpr_size;
10049 if (reg_size == 4 && (first_reg_offset & 1))
10050 gpr_reg_num++;
10052 gpr_size = (gpr_reg_num * reg_size + 7) & ~7;
10054 else if (fpr_size)
10055 offset = - (int) (next_cum.fregno - FP_ARG_MIN_REG)
10056 * UNITS_PER_FP_WORD
10057 - (int) (GP_ARG_NUM_REG * reg_size);
10059 if (gpr_size + fpr_size)
10061 rtx reg_save_area
10062 = assign_stack_local (BLKmode, gpr_size + fpr_size, 64);
10063 gcc_assert (GET_CODE (reg_save_area) == MEM);
10064 reg_save_area = XEXP (reg_save_area, 0);
10065 if (GET_CODE (reg_save_area) == PLUS)
10067 gcc_assert (XEXP (reg_save_area, 0)
10068 == virtual_stack_vars_rtx);
10069 gcc_assert (GET_CODE (XEXP (reg_save_area, 1)) == CONST_INT);
10070 offset += INTVAL (XEXP (reg_save_area, 1));
10072 else
10073 gcc_assert (reg_save_area == virtual_stack_vars_rtx);
10076 cfun->machine->varargs_save_offset = offset;
10077 save_area = plus_constant (Pmode, virtual_stack_vars_rtx, offset);
10080 else
10082 first_reg_offset = next_cum.words;
10083 save_area = virtual_incoming_args_rtx;
10085 if (targetm.calls.must_pass_in_stack (mode, type))
10086 first_reg_offset += rs6000_arg_size (TYPE_MODE (type), type);
10089 set = get_varargs_alias_set ();
10090 if (! no_rtl && first_reg_offset < GP_ARG_NUM_REG
10091 && cfun->va_list_gpr_size)
10093 int n_gpr, nregs = GP_ARG_NUM_REG - first_reg_offset;
10095 if (va_list_gpr_counter_field)
10096 /* V4 va_list_gpr_size counts number of registers needed. */
10097 n_gpr = cfun->va_list_gpr_size;
10098 else
10099 /* char * va_list instead counts number of bytes needed. */
10100 n_gpr = (cfun->va_list_gpr_size + reg_size - 1) / reg_size;
10102 if (nregs > n_gpr)
10103 nregs = n_gpr;
10105 mem = gen_rtx_MEM (BLKmode,
10106 plus_constant (Pmode, save_area,
10107 first_reg_offset * reg_size));
10108 MEM_NOTRAP_P (mem) = 1;
10109 set_mem_alias_set (mem, set);
10110 set_mem_align (mem, BITS_PER_WORD);
10112 rs6000_move_block_from_reg (GP_ARG_MIN_REG + first_reg_offset, mem,
10113 nregs);
10116 /* Save FP registers if needed. */
10117 if (DEFAULT_ABI == ABI_V4
10118 && TARGET_HARD_FLOAT && TARGET_FPRS
10119 && ! no_rtl
10120 && next_cum.fregno <= FP_ARG_V4_MAX_REG
10121 && cfun->va_list_fpr_size)
10123 int fregno = next_cum.fregno, nregs;
10124 rtx cr1 = gen_rtx_REG (CCmode, CR1_REGNO);
10125 rtx lab = gen_label_rtx ();
10126 int off = (GP_ARG_NUM_REG * reg_size) + ((fregno - FP_ARG_MIN_REG)
10127 * UNITS_PER_FP_WORD);
10129 emit_jump_insn
10130 (gen_rtx_SET (VOIDmode,
10131 pc_rtx,
10132 gen_rtx_IF_THEN_ELSE (VOIDmode,
10133 gen_rtx_NE (VOIDmode, cr1,
10134 const0_rtx),
10135 gen_rtx_LABEL_REF (VOIDmode, lab),
10136 pc_rtx)));
10138 for (nregs = 0;
10139 fregno <= FP_ARG_V4_MAX_REG && nregs < cfun->va_list_fpr_size;
10140 fregno++, off += UNITS_PER_FP_WORD, nregs++)
10142 mem = gen_rtx_MEM ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
10143 ? DFmode : SFmode,
10144 plus_constant (Pmode, save_area, off));
10145 MEM_NOTRAP_P (mem) = 1;
10146 set_mem_alias_set (mem, set);
10147 set_mem_align (mem, GET_MODE_ALIGNMENT (
10148 (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
10149 ? DFmode : SFmode));
10150 emit_move_insn (mem, gen_rtx_REG (
10151 (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
10152 ? DFmode : SFmode, fregno));
10155 emit_label (lab);
10159 /* Create the va_list data type. */
10161 static tree
10162 rs6000_build_builtin_va_list (void)
10164 tree f_gpr, f_fpr, f_res, f_ovf, f_sav, record, type_decl;
10166 /* For AIX, prefer 'char *' because that's what the system
10167 header files like. */
10168 if (DEFAULT_ABI != ABI_V4)
10169 return build_pointer_type (char_type_node);
10171 record = (*lang_hooks.types.make_type) (RECORD_TYPE);
10172 type_decl = build_decl (BUILTINS_LOCATION, TYPE_DECL,
10173 get_identifier ("__va_list_tag"), record);
10175 f_gpr = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("gpr"),
10176 unsigned_char_type_node);
10177 f_fpr = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("fpr"),
10178 unsigned_char_type_node);
10179 /* Give the two bytes of padding a name, so that -Wpadded won't warn on
10180 every user file. */
10181 f_res = build_decl (BUILTINS_LOCATION, FIELD_DECL,
10182 get_identifier ("reserved"), short_unsigned_type_node);
10183 f_ovf = build_decl (BUILTINS_LOCATION, FIELD_DECL,
10184 get_identifier ("overflow_arg_area"),
10185 ptr_type_node);
10186 f_sav = build_decl (BUILTINS_LOCATION, FIELD_DECL,
10187 get_identifier ("reg_save_area"),
10188 ptr_type_node);
10190 va_list_gpr_counter_field = f_gpr;
10191 va_list_fpr_counter_field = f_fpr;
10193 DECL_FIELD_CONTEXT (f_gpr) = record;
10194 DECL_FIELD_CONTEXT (f_fpr) = record;
10195 DECL_FIELD_CONTEXT (f_res) = record;
10196 DECL_FIELD_CONTEXT (f_ovf) = record;
10197 DECL_FIELD_CONTEXT (f_sav) = record;
10199 TYPE_STUB_DECL (record) = type_decl;
10200 TYPE_NAME (record) = type_decl;
10201 TYPE_FIELDS (record) = f_gpr;
10202 DECL_CHAIN (f_gpr) = f_fpr;
10203 DECL_CHAIN (f_fpr) = f_res;
10204 DECL_CHAIN (f_res) = f_ovf;
10205 DECL_CHAIN (f_ovf) = f_sav;
10207 layout_type (record);
10209 /* The correct type is an array type of one element. */
10210 return build_array_type (record, build_index_type (size_zero_node));
10213 /* Implement va_start. */
10215 static void
10216 rs6000_va_start (tree valist, rtx nextarg)
10218 HOST_WIDE_INT words, n_gpr, n_fpr;
10219 tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
10220 tree gpr, fpr, ovf, sav, t;
10222 /* Only SVR4 needs something special. */
10223 if (DEFAULT_ABI != ABI_V4)
10225 std_expand_builtin_va_start (valist, nextarg);
10226 return;
10229 f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
10230 f_fpr = DECL_CHAIN (f_gpr);
10231 f_res = DECL_CHAIN (f_fpr);
10232 f_ovf = DECL_CHAIN (f_res);
10233 f_sav = DECL_CHAIN (f_ovf);
10235 valist = build_simple_mem_ref (valist);
10236 gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
10237 fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
10238 f_fpr, NULL_TREE);
10239 ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
10240 f_ovf, NULL_TREE);
10241 sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
10242 f_sav, NULL_TREE);
10244 /* Count number of gp and fp argument registers used. */
10245 words = crtl->args.info.words;
10246 n_gpr = MIN (crtl->args.info.sysv_gregno - GP_ARG_MIN_REG,
10247 GP_ARG_NUM_REG);
10248 n_fpr = MIN (crtl->args.info.fregno - FP_ARG_MIN_REG,
10249 FP_ARG_NUM_REG);
10251 if (TARGET_DEBUG_ARG)
10252 fprintf (stderr, "va_start: words = "HOST_WIDE_INT_PRINT_DEC", n_gpr = "
10253 HOST_WIDE_INT_PRINT_DEC", n_fpr = "HOST_WIDE_INT_PRINT_DEC"\n",
10254 words, n_gpr, n_fpr);
10256 if (cfun->va_list_gpr_size)
10258 t = build2 (MODIFY_EXPR, TREE_TYPE (gpr), gpr,
10259 build_int_cst (NULL_TREE, n_gpr));
10260 TREE_SIDE_EFFECTS (t) = 1;
10261 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
10264 if (cfun->va_list_fpr_size)
10266 t = build2 (MODIFY_EXPR, TREE_TYPE (fpr), fpr,
10267 build_int_cst (NULL_TREE, n_fpr));
10268 TREE_SIDE_EFFECTS (t) = 1;
10269 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
10271 #ifdef HAVE_AS_GNU_ATTRIBUTE
10272 if (call_ABI_of_interest (cfun->decl))
10273 rs6000_passes_float = true;
10274 #endif
10277 /* Find the overflow area. */
10278 t = make_tree (TREE_TYPE (ovf), virtual_incoming_args_rtx);
10279 if (words != 0)
10280 t = fold_build_pointer_plus_hwi (t, words * UNITS_PER_WORD);
10281 t = build2 (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
10282 TREE_SIDE_EFFECTS (t) = 1;
10283 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
10285 /* If there were no va_arg invocations, don't set up the register
10286 save area. */
10287 if (!cfun->va_list_gpr_size
10288 && !cfun->va_list_fpr_size
10289 && n_gpr < GP_ARG_NUM_REG
10290 && n_fpr < FP_ARG_V4_MAX_REG)
10291 return;
10293 /* Find the register save area. */
10294 t = make_tree (TREE_TYPE (sav), virtual_stack_vars_rtx);
10295 if (cfun->machine->varargs_save_offset)
10296 t = fold_build_pointer_plus_hwi (t, cfun->machine->varargs_save_offset);
10297 t = build2 (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
10298 TREE_SIDE_EFFECTS (t) = 1;
10299 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
10302 /* Implement va_arg. */
10304 static tree
10305 rs6000_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p,
10306 gimple_seq *post_p)
10308 tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
10309 tree gpr, fpr, ovf, sav, reg, t, u;
10310 int size, rsize, n_reg, sav_ofs, sav_scale;
10311 tree lab_false, lab_over, addr;
10312 int align;
10313 tree ptrtype = build_pointer_type_for_mode (type, ptr_mode, true);
10314 int regalign = 0;
10315 gimple stmt;
10317 if (pass_by_reference (NULL, TYPE_MODE (type), type, false))
10319 t = rs6000_gimplify_va_arg (valist, ptrtype, pre_p, post_p);
10320 return build_va_arg_indirect_ref (t);
10323 /* We need to deal with the fact that the darwin ppc64 ABI is defined by an
10324 earlier version of gcc, with the property that it always applied alignment
10325 adjustments to the va-args (even for zero-sized types). The cheapest way
10326 to deal with this is to replicate the effect of the part of
10327 std_gimplify_va_arg_expr that carries out the align adjust, for the case
10328 of relevance.
10329 We don't need to check for pass-by-reference because of the test above.
10330 We can return a simplifed answer, since we know there's no offset to add. */
10332 if (((TARGET_MACHO
10333 && rs6000_darwin64_abi)
10334 || (DEFAULT_ABI == ABI_AIX && !rs6000_compat_align_parm))
10335 && integer_zerop (TYPE_SIZE (type)))
10337 unsigned HOST_WIDE_INT align, boundary;
10338 tree valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
10339 align = PARM_BOUNDARY / BITS_PER_UNIT;
10340 boundary = rs6000_function_arg_boundary (TYPE_MODE (type), type);
10341 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
10342 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
10343 boundary /= BITS_PER_UNIT;
10344 if (boundary > align)
10346 tree t ;
10347 /* This updates arg ptr by the amount that would be necessary
10348 to align the zero-sized (but not zero-alignment) item. */
10349 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
10350 fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
10351 gimplify_and_add (t, pre_p);
10353 t = fold_convert (sizetype, valist_tmp);
10354 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
10355 fold_convert (TREE_TYPE (valist),
10356 fold_build2 (BIT_AND_EXPR, sizetype, t,
10357 size_int (-boundary))));
10358 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
10359 gimplify_and_add (t, pre_p);
10361 /* Since it is zero-sized there's no increment for the item itself. */
10362 valist_tmp = fold_convert (build_pointer_type (type), valist_tmp);
10363 return build_va_arg_indirect_ref (valist_tmp);
10366 if (DEFAULT_ABI != ABI_V4)
10368 if (targetm.calls.split_complex_arg && TREE_CODE (type) == COMPLEX_TYPE)
10370 tree elem_type = TREE_TYPE (type);
10371 enum machine_mode elem_mode = TYPE_MODE (elem_type);
10372 int elem_size = GET_MODE_SIZE (elem_mode);
10374 if (elem_size < UNITS_PER_WORD)
10376 tree real_part, imag_part;
10377 gimple_seq post = NULL;
10379 real_part = rs6000_gimplify_va_arg (valist, elem_type, pre_p,
10380 &post);
10381 /* Copy the value into a temporary, lest the formal temporary
10382 be reused out from under us. */
10383 real_part = get_initialized_tmp_var (real_part, pre_p, &post);
10384 gimple_seq_add_seq (pre_p, post);
10386 imag_part = rs6000_gimplify_va_arg (valist, elem_type, pre_p,
10387 post_p);
10389 return build2 (COMPLEX_EXPR, type, real_part, imag_part);
10393 return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
10396 f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
10397 f_fpr = DECL_CHAIN (f_gpr);
10398 f_res = DECL_CHAIN (f_fpr);
10399 f_ovf = DECL_CHAIN (f_res);
10400 f_sav = DECL_CHAIN (f_ovf);
10402 valist = build_va_arg_indirect_ref (valist);
10403 gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
10404 fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
10405 f_fpr, NULL_TREE);
10406 ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
10407 f_ovf, NULL_TREE);
10408 sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
10409 f_sav, NULL_TREE);
10411 size = int_size_in_bytes (type);
10412 rsize = (size + 3) / 4;
10413 align = 1;
10415 if (TARGET_HARD_FLOAT && TARGET_FPRS
10416 && ((TARGET_SINGLE_FLOAT && TYPE_MODE (type) == SFmode)
10417 || (TARGET_DOUBLE_FLOAT
10418 && (TYPE_MODE (type) == DFmode
10419 || TYPE_MODE (type) == TFmode
10420 || TYPE_MODE (type) == SDmode
10421 || TYPE_MODE (type) == DDmode
10422 || TYPE_MODE (type) == TDmode))))
10424 /* FP args go in FP registers, if present. */
10425 reg = fpr;
10426 n_reg = (size + 7) / 8;
10427 sav_ofs = ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? 8 : 4) * 4;
10428 sav_scale = ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? 8 : 4);
10429 if (TYPE_MODE (type) != SFmode && TYPE_MODE (type) != SDmode)
10430 align = 8;
10432 else
10434 /* Otherwise into GP registers. */
10435 reg = gpr;
10436 n_reg = rsize;
10437 sav_ofs = 0;
10438 sav_scale = 4;
10439 if (n_reg == 2)
10440 align = 8;
10443 /* Pull the value out of the saved registers.... */
10445 lab_over = NULL;
10446 addr = create_tmp_var (ptr_type_node, "addr");
10448 /* AltiVec vectors never go in registers when -mabi=altivec. */
10449 if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (TYPE_MODE (type)))
10450 align = 16;
10451 else
10453 lab_false = create_artificial_label (input_location);
10454 lab_over = create_artificial_label (input_location);
10456 /* Long long and SPE vectors are aligned in the registers.
10457 As are any other 2 gpr item such as complex int due to a
10458 historical mistake. */
10459 u = reg;
10460 if (n_reg == 2 && reg == gpr)
10462 regalign = 1;
10463 u = build2 (BIT_AND_EXPR, TREE_TYPE (reg), unshare_expr (reg),
10464 build_int_cst (TREE_TYPE (reg), n_reg - 1));
10465 u = build2 (POSTINCREMENT_EXPR, TREE_TYPE (reg),
10466 unshare_expr (reg), u);
10468 /* _Decimal128 is passed in even/odd fpr pairs; the stored
10469 reg number is 0 for f1, so we want to make it odd. */
10470 else if (reg == fpr && TYPE_MODE (type) == TDmode)
10472 t = build2 (BIT_IOR_EXPR, TREE_TYPE (reg), unshare_expr (reg),
10473 build_int_cst (TREE_TYPE (reg), 1));
10474 u = build2 (MODIFY_EXPR, void_type_node, unshare_expr (reg), t);
10477 t = fold_convert (TREE_TYPE (reg), size_int (8 - n_reg + 1));
10478 t = build2 (GE_EXPR, boolean_type_node, u, t);
10479 u = build1 (GOTO_EXPR, void_type_node, lab_false);
10480 t = build3 (COND_EXPR, void_type_node, t, u, NULL_TREE);
10481 gimplify_and_add (t, pre_p);
10483 t = sav;
10484 if (sav_ofs)
10485 t = fold_build_pointer_plus_hwi (sav, sav_ofs);
10487 u = build2 (POSTINCREMENT_EXPR, TREE_TYPE (reg), unshare_expr (reg),
10488 build_int_cst (TREE_TYPE (reg), n_reg));
10489 u = fold_convert (sizetype, u);
10490 u = build2 (MULT_EXPR, sizetype, u, size_int (sav_scale));
10491 t = fold_build_pointer_plus (t, u);
10493 /* _Decimal32 varargs are located in the second word of the 64-bit
10494 FP register for 32-bit binaries. */
10495 if (!TARGET_POWERPC64
10496 && TARGET_HARD_FLOAT && TARGET_FPRS
10497 && TYPE_MODE (type) == SDmode)
10498 t = fold_build_pointer_plus_hwi (t, size);
10500 gimplify_assign (addr, t, pre_p);
10502 gimple_seq_add_stmt (pre_p, gimple_build_goto (lab_over));
10504 stmt = gimple_build_label (lab_false);
10505 gimple_seq_add_stmt (pre_p, stmt);
10507 if ((n_reg == 2 && !regalign) || n_reg > 2)
10509 /* Ensure that we don't find any more args in regs.
10510 Alignment has taken care of for special cases. */
10511 gimplify_assign (reg, build_int_cst (TREE_TYPE (reg), 8), pre_p);
10515 /* ... otherwise out of the overflow area. */
10517 /* Care for on-stack alignment if needed. */
10518 t = ovf;
10519 if (align != 1)
10521 t = fold_build_pointer_plus_hwi (t, align - 1);
10522 t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t,
10523 build_int_cst (TREE_TYPE (t), -align));
10525 gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
10527 gimplify_assign (unshare_expr (addr), t, pre_p);
10529 t = fold_build_pointer_plus_hwi (t, size);
10530 gimplify_assign (unshare_expr (ovf), t, pre_p);
10532 if (lab_over)
10534 stmt = gimple_build_label (lab_over);
10535 gimple_seq_add_stmt (pre_p, stmt);
10538 if (STRICT_ALIGNMENT
10539 && (TYPE_ALIGN (type)
10540 > (unsigned) BITS_PER_UNIT * (align < 4 ? 4 : align)))
10542 /* The value (of type complex double, for example) may not be
10543 aligned in memory in the saved registers, so copy via a
10544 temporary. (This is the same code as used for SPARC.) */
10545 tree tmp = create_tmp_var (type, "va_arg_tmp");
10546 tree dest_addr = build_fold_addr_expr (tmp);
10548 tree copy = build_call_expr (builtin_decl_implicit (BUILT_IN_MEMCPY),
10549 3, dest_addr, addr, size_int (rsize * 4));
10551 gimplify_and_add (copy, pre_p);
10552 addr = dest_addr;
10555 addr = fold_convert (ptrtype, addr);
10556 return build_va_arg_indirect_ref (addr);
10559 /* Builtins. */
10561 static void
10562 def_builtin (const char *name, tree type, enum rs6000_builtins code)
10564 tree t;
10565 unsigned classify = rs6000_builtin_info[(int)code].attr;
10566 const char *attr_string = "";
10568 gcc_assert (name != NULL);
10569 gcc_assert (IN_RANGE ((int)code, 0, (int)RS6000_BUILTIN_COUNT));
10571 if (rs6000_builtin_decls[(int)code])
10572 fatal_error ("internal error: builtin function %s already processed", name);
10574 rs6000_builtin_decls[(int)code] = t =
10575 add_builtin_function (name, type, (int)code, BUILT_IN_MD, NULL, NULL_TREE);
10577 /* Set any special attributes. */
10578 if ((classify & RS6000_BTC_CONST) != 0)
10580 /* const function, function only depends on the inputs. */
10581 TREE_READONLY (t) = 1;
10582 TREE_NOTHROW (t) = 1;
10583 attr_string = ", pure";
10585 else if ((classify & RS6000_BTC_PURE) != 0)
10587 /* pure function, function can read global memory, but does not set any
10588 external state. */
10589 DECL_PURE_P (t) = 1;
10590 TREE_NOTHROW (t) = 1;
10591 attr_string = ", const";
10593 else if ((classify & RS6000_BTC_FP) != 0)
10595 /* Function is a math function. If rounding mode is on, then treat the
10596 function as not reading global memory, but it can have arbitrary side
10597 effects. If it is off, then assume the function is a const function.
10598 This mimics the ATTR_MATHFN_FPROUNDING attribute in
10599 builtin-attribute.def that is used for the math functions. */
10600 TREE_NOTHROW (t) = 1;
10601 if (flag_rounding_math)
10603 DECL_PURE_P (t) = 1;
10604 DECL_IS_NOVOPS (t) = 1;
10605 attr_string = ", fp, pure";
10607 else
10609 TREE_READONLY (t) = 1;
10610 attr_string = ", fp, const";
10613 else if ((classify & RS6000_BTC_ATTR_MASK) != 0)
10614 gcc_unreachable ();
10616 if (TARGET_DEBUG_BUILTIN)
10617 fprintf (stderr, "rs6000_builtin, code = %4d, %s%s\n",
10618 (int)code, name, attr_string);
10621 /* Simple ternary operations: VECd = foo (VECa, VECb, VECc). */
10623 #undef RS6000_BUILTIN_1
10624 #undef RS6000_BUILTIN_2
10625 #undef RS6000_BUILTIN_3
10626 #undef RS6000_BUILTIN_A
10627 #undef RS6000_BUILTIN_D
10628 #undef RS6000_BUILTIN_E
10629 #undef RS6000_BUILTIN_H
10630 #undef RS6000_BUILTIN_P
10631 #undef RS6000_BUILTIN_Q
10632 #undef RS6000_BUILTIN_S
10633 #undef RS6000_BUILTIN_X
10635 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
10636 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
10637 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE) \
10638 { MASK, ICODE, NAME, ENUM },
10640 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
10641 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
10642 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
10643 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
10644 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
10645 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
10646 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
10647 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
10649 static const struct builtin_description bdesc_3arg[] =
10651 #include "rs6000-builtin.def"
10654 /* DST operations: void foo (void *, const int, const char). */
10656 #undef RS6000_BUILTIN_1
10657 #undef RS6000_BUILTIN_2
10658 #undef RS6000_BUILTIN_3
10659 #undef RS6000_BUILTIN_A
10660 #undef RS6000_BUILTIN_D
10661 #undef RS6000_BUILTIN_E
10662 #undef RS6000_BUILTIN_H
10663 #undef RS6000_BUILTIN_P
10664 #undef RS6000_BUILTIN_Q
10665 #undef RS6000_BUILTIN_S
10666 #undef RS6000_BUILTIN_X
10668 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
10669 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
10670 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
10671 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
10672 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE) \
10673 { MASK, ICODE, NAME, ENUM },
10675 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
10676 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
10677 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
10678 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
10679 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
10680 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
10682 static const struct builtin_description bdesc_dst[] =
10684 #include "rs6000-builtin.def"
10687 /* Simple binary operations: VECc = foo (VECa, VECb). */
10689 #undef RS6000_BUILTIN_1
10690 #undef RS6000_BUILTIN_2
10691 #undef RS6000_BUILTIN_3
10692 #undef RS6000_BUILTIN_A
10693 #undef RS6000_BUILTIN_D
10694 #undef RS6000_BUILTIN_E
10695 #undef RS6000_BUILTIN_H
10696 #undef RS6000_BUILTIN_P
10697 #undef RS6000_BUILTIN_Q
10698 #undef RS6000_BUILTIN_S
10699 #undef RS6000_BUILTIN_X
10701 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
10702 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE) \
10703 { MASK, ICODE, NAME, ENUM },
10705 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
10706 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
10707 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
10708 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
10709 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
10710 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
10711 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
10712 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
10713 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
10715 static const struct builtin_description bdesc_2arg[] =
10717 #include "rs6000-builtin.def"
10720 #undef RS6000_BUILTIN_1
10721 #undef RS6000_BUILTIN_2
10722 #undef RS6000_BUILTIN_3
10723 #undef RS6000_BUILTIN_A
10724 #undef RS6000_BUILTIN_D
10725 #undef RS6000_BUILTIN_E
10726 #undef RS6000_BUILTIN_H
10727 #undef RS6000_BUILTIN_P
10728 #undef RS6000_BUILTIN_Q
10729 #undef RS6000_BUILTIN_S
10730 #undef RS6000_BUILTIN_X
10732 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
10733 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
10734 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
10735 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
10736 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
10737 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
10738 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
10739 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE) \
10740 { MASK, ICODE, NAME, ENUM },
10742 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
10743 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
10744 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
10746 /* AltiVec predicates. */
10748 static const struct builtin_description bdesc_altivec_preds[] =
10750 #include "rs6000-builtin.def"
10753 /* SPE predicates. */
10754 #undef RS6000_BUILTIN_1
10755 #undef RS6000_BUILTIN_2
10756 #undef RS6000_BUILTIN_3
10757 #undef RS6000_BUILTIN_A
10758 #undef RS6000_BUILTIN_D
10759 #undef RS6000_BUILTIN_E
10760 #undef RS6000_BUILTIN_H
10761 #undef RS6000_BUILTIN_P
10762 #undef RS6000_BUILTIN_Q
10763 #undef RS6000_BUILTIN_S
10764 #undef RS6000_BUILTIN_X
10766 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
10767 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
10768 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
10769 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
10770 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
10771 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
10772 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
10773 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
10774 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
10775 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE) \
10776 { MASK, ICODE, NAME, ENUM },
10778 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
10780 static const struct builtin_description bdesc_spe_predicates[] =
10782 #include "rs6000-builtin.def"
10785 /* SPE evsel predicates. */
10786 #undef RS6000_BUILTIN_1
10787 #undef RS6000_BUILTIN_2
10788 #undef RS6000_BUILTIN_3
10789 #undef RS6000_BUILTIN_A
10790 #undef RS6000_BUILTIN_D
10791 #undef RS6000_BUILTIN_E
10792 #undef RS6000_BUILTIN_H
10793 #undef RS6000_BUILTIN_P
10794 #undef RS6000_BUILTIN_Q
10795 #undef RS6000_BUILTIN_S
10796 #undef RS6000_BUILTIN_X
10798 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
10799 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
10800 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
10801 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
10802 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
10803 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE) \
10804 { MASK, ICODE, NAME, ENUM },
10806 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
10807 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
10808 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
10809 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
10810 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
10812 static const struct builtin_description bdesc_spe_evsel[] =
10814 #include "rs6000-builtin.def"
10817 /* PAIRED predicates. */
10818 #undef RS6000_BUILTIN_1
10819 #undef RS6000_BUILTIN_2
10820 #undef RS6000_BUILTIN_3
10821 #undef RS6000_BUILTIN_A
10822 #undef RS6000_BUILTIN_D
10823 #undef RS6000_BUILTIN_E
10824 #undef RS6000_BUILTIN_H
10825 #undef RS6000_BUILTIN_P
10826 #undef RS6000_BUILTIN_Q
10827 #undef RS6000_BUILTIN_S
10828 #undef RS6000_BUILTIN_X
10830 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
10831 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
10832 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
10833 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
10834 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
10835 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
10836 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
10837 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
10838 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE) \
10839 { MASK, ICODE, NAME, ENUM },
10841 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
10842 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
10844 static const struct builtin_description bdesc_paired_preds[] =
10846 #include "rs6000-builtin.def"
10849 /* ABS* operations. */
10851 #undef RS6000_BUILTIN_1
10852 #undef RS6000_BUILTIN_2
10853 #undef RS6000_BUILTIN_3
10854 #undef RS6000_BUILTIN_A
10855 #undef RS6000_BUILTIN_D
10856 #undef RS6000_BUILTIN_E
10857 #undef RS6000_BUILTIN_H
10858 #undef RS6000_BUILTIN_P
10859 #undef RS6000_BUILTIN_Q
10860 #undef RS6000_BUILTIN_S
10861 #undef RS6000_BUILTIN_X
10863 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
10864 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
10865 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
10866 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE) \
10867 { MASK, ICODE, NAME, ENUM },
10869 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
10870 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
10871 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
10872 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
10873 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
10874 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
10875 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
10877 static const struct builtin_description bdesc_abs[] =
10879 #include "rs6000-builtin.def"
10882 /* Simple unary operations: VECb = foo (unsigned literal) or VECb =
10883 foo (VECa). */
10885 #undef RS6000_BUILTIN_1
10886 #undef RS6000_BUILTIN_2
10887 #undef RS6000_BUILTIN_3
10888 #undef RS6000_BUILTIN_A
10889 #undef RS6000_BUILTIN_D
10890 #undef RS6000_BUILTIN_E
10891 #undef RS6000_BUILTIN_H
10892 #undef RS6000_BUILTIN_P
10893 #undef RS6000_BUILTIN_Q
10894 #undef RS6000_BUILTIN_S
10895 #undef RS6000_BUILTIN_X
10897 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE) \
10898 { MASK, ICODE, NAME, ENUM },
10900 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
10901 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
10902 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
10903 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
10904 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
10905 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
10906 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
10907 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
10908 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
10909 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
10911 static const struct builtin_description bdesc_1arg[] =
10913 #include "rs6000-builtin.def"
10916 /* HTM builtins. */
10917 #undef RS6000_BUILTIN_1
10918 #undef RS6000_BUILTIN_2
10919 #undef RS6000_BUILTIN_3
10920 #undef RS6000_BUILTIN_A
10921 #undef RS6000_BUILTIN_D
10922 #undef RS6000_BUILTIN_E
10923 #undef RS6000_BUILTIN_H
10924 #undef RS6000_BUILTIN_P
10925 #undef RS6000_BUILTIN_Q
10926 #undef RS6000_BUILTIN_S
10927 #undef RS6000_BUILTIN_X
10929 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
10930 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
10931 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
10932 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
10933 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
10934 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
10935 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE) \
10936 { MASK, ICODE, NAME, ENUM },
10938 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
10939 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
10940 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
10941 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
10943 static const struct builtin_description bdesc_htm[] =
10945 #include "rs6000-builtin.def"
10948 #undef RS6000_BUILTIN_1
10949 #undef RS6000_BUILTIN_2
10950 #undef RS6000_BUILTIN_3
10951 #undef RS6000_BUILTIN_A
10952 #undef RS6000_BUILTIN_D
10953 #undef RS6000_BUILTIN_E
10954 #undef RS6000_BUILTIN_H
10955 #undef RS6000_BUILTIN_P
10956 #undef RS6000_BUILTIN_Q
10957 #undef RS6000_BUILTIN_S
10959 /* Return true if a builtin function is overloaded. */
10960 bool
10961 rs6000_overloaded_builtin_p (enum rs6000_builtins fncode)
10963 return (rs6000_builtin_info[(int)fncode].attr & RS6000_BTC_OVERLOADED) != 0;
10966 /* Expand an expression EXP that calls a builtin without arguments. */
10967 static rtx
10968 rs6000_expand_zeroop_builtin (enum insn_code icode, rtx target)
10970 rtx pat;
10971 enum machine_mode tmode = insn_data[icode].operand[0].mode;
10973 if (icode == CODE_FOR_nothing)
10974 /* Builtin not supported on this processor. */
10975 return 0;
10977 if (target == 0
10978 || GET_MODE (target) != tmode
10979 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10980 target = gen_reg_rtx (tmode);
10982 pat = GEN_FCN (icode) (target);
10983 if (! pat)
10984 return 0;
10985 emit_insn (pat);
10987 return target;
10991 static rtx
10992 rs6000_expand_unop_builtin (enum insn_code icode, tree exp, rtx target)
10994 rtx pat;
10995 tree arg0 = CALL_EXPR_ARG (exp, 0);
10996 rtx op0 = expand_normal (arg0);
10997 enum machine_mode tmode = insn_data[icode].operand[0].mode;
10998 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11000 if (icode == CODE_FOR_nothing)
11001 /* Builtin not supported on this processor. */
11002 return 0;
11004 /* If we got invalid arguments bail out before generating bad rtl. */
11005 if (arg0 == error_mark_node)
11006 return const0_rtx;
11008 if (icode == CODE_FOR_altivec_vspltisb
11009 || icode == CODE_FOR_altivec_vspltish
11010 || icode == CODE_FOR_altivec_vspltisw
11011 || icode == CODE_FOR_spe_evsplatfi
11012 || icode == CODE_FOR_spe_evsplati)
11014 /* Only allow 5-bit *signed* literals. */
11015 if (GET_CODE (op0) != CONST_INT
11016 || INTVAL (op0) > 15
11017 || INTVAL (op0) < -16)
11019 error ("argument 1 must be a 5-bit signed literal");
11020 return const0_rtx;
11024 if (target == 0
11025 || GET_MODE (target) != tmode
11026 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11027 target = gen_reg_rtx (tmode);
11029 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11030 op0 = copy_to_mode_reg (mode0, op0);
11032 pat = GEN_FCN (icode) (target, op0);
11033 if (! pat)
11034 return 0;
11035 emit_insn (pat);
11037 return target;
11040 static rtx
11041 altivec_expand_abs_builtin (enum insn_code icode, tree exp, rtx target)
11043 rtx pat, scratch1, scratch2;
11044 tree arg0 = CALL_EXPR_ARG (exp, 0);
11045 rtx op0 = expand_normal (arg0);
11046 enum machine_mode tmode = insn_data[icode].operand[0].mode;
11047 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11049 /* If we have invalid arguments, bail out before generating bad rtl. */
11050 if (arg0 == error_mark_node)
11051 return const0_rtx;
11053 if (target == 0
11054 || GET_MODE (target) != tmode
11055 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11056 target = gen_reg_rtx (tmode);
11058 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11059 op0 = copy_to_mode_reg (mode0, op0);
11061 scratch1 = gen_reg_rtx (mode0);
11062 scratch2 = gen_reg_rtx (mode0);
11064 pat = GEN_FCN (icode) (target, op0, scratch1, scratch2);
11065 if (! pat)
11066 return 0;
11067 emit_insn (pat);
11069 return target;
11072 static rtx
11073 rs6000_expand_binop_builtin (enum insn_code icode, tree exp, rtx target)
11075 rtx pat;
11076 tree arg0 = CALL_EXPR_ARG (exp, 0);
11077 tree arg1 = CALL_EXPR_ARG (exp, 1);
11078 rtx op0 = expand_normal (arg0);
11079 rtx op1 = expand_normal (arg1);
11080 enum machine_mode tmode = insn_data[icode].operand[0].mode;
11081 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11082 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
11084 if (icode == CODE_FOR_nothing)
11085 /* Builtin not supported on this processor. */
11086 return 0;
11088 /* If we got invalid arguments bail out before generating bad rtl. */
11089 if (arg0 == error_mark_node || arg1 == error_mark_node)
11090 return const0_rtx;
11092 if (icode == CODE_FOR_altivec_vcfux
11093 || icode == CODE_FOR_altivec_vcfsx
11094 || icode == CODE_FOR_altivec_vctsxs
11095 || icode == CODE_FOR_altivec_vctuxs
11096 || icode == CODE_FOR_altivec_vspltb
11097 || icode == CODE_FOR_altivec_vsplth
11098 || icode == CODE_FOR_altivec_vspltw
11099 || icode == CODE_FOR_spe_evaddiw
11100 || icode == CODE_FOR_spe_evldd
11101 || icode == CODE_FOR_spe_evldh
11102 || icode == CODE_FOR_spe_evldw
11103 || icode == CODE_FOR_spe_evlhhesplat
11104 || icode == CODE_FOR_spe_evlhhossplat
11105 || icode == CODE_FOR_spe_evlhhousplat
11106 || icode == CODE_FOR_spe_evlwhe
11107 || icode == CODE_FOR_spe_evlwhos
11108 || icode == CODE_FOR_spe_evlwhou
11109 || icode == CODE_FOR_spe_evlwhsplat
11110 || icode == CODE_FOR_spe_evlwwsplat
11111 || icode == CODE_FOR_spe_evrlwi
11112 || icode == CODE_FOR_spe_evslwi
11113 || icode == CODE_FOR_spe_evsrwis
11114 || icode == CODE_FOR_spe_evsubifw
11115 || icode == CODE_FOR_spe_evsrwiu)
11117 /* Only allow 5-bit unsigned literals. */
11118 STRIP_NOPS (arg1);
11119 if (TREE_CODE (arg1) != INTEGER_CST
11120 || TREE_INT_CST_LOW (arg1) & ~0x1f)
11122 error ("argument 2 must be a 5-bit unsigned literal");
11123 return const0_rtx;
11127 if (target == 0
11128 || GET_MODE (target) != tmode
11129 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11130 target = gen_reg_rtx (tmode);
11132 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11133 op0 = copy_to_mode_reg (mode0, op0);
11134 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
11135 op1 = copy_to_mode_reg (mode1, op1);
11137 pat = GEN_FCN (icode) (target, op0, op1);
11138 if (! pat)
11139 return 0;
11140 emit_insn (pat);
11142 return target;
11145 static rtx
11146 altivec_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
11148 rtx pat, scratch;
11149 tree cr6_form = CALL_EXPR_ARG (exp, 0);
11150 tree arg0 = CALL_EXPR_ARG (exp, 1);
11151 tree arg1 = CALL_EXPR_ARG (exp, 2);
11152 rtx op0 = expand_normal (arg0);
11153 rtx op1 = expand_normal (arg1);
11154 enum machine_mode tmode = SImode;
11155 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11156 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
11157 int cr6_form_int;
11159 if (TREE_CODE (cr6_form) != INTEGER_CST)
11161 error ("argument 1 of __builtin_altivec_predicate must be a constant");
11162 return const0_rtx;
11164 else
11165 cr6_form_int = TREE_INT_CST_LOW (cr6_form);
11167 gcc_assert (mode0 == mode1);
11169 /* If we have invalid arguments, bail out before generating bad rtl. */
11170 if (arg0 == error_mark_node || arg1 == error_mark_node)
11171 return const0_rtx;
11173 if (target == 0
11174 || GET_MODE (target) != tmode
11175 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11176 target = gen_reg_rtx (tmode);
11178 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11179 op0 = copy_to_mode_reg (mode0, op0);
11180 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
11181 op1 = copy_to_mode_reg (mode1, op1);
11183 scratch = gen_reg_rtx (mode0);
11185 pat = GEN_FCN (icode) (scratch, op0, op1);
11186 if (! pat)
11187 return 0;
11188 emit_insn (pat);
11190 /* The vec_any* and vec_all* predicates use the same opcodes for two
11191 different operations, but the bits in CR6 will be different
11192 depending on what information we want. So we have to play tricks
11193 with CR6 to get the right bits out.
11195 If you think this is disgusting, look at the specs for the
11196 AltiVec predicates. */
11198 switch (cr6_form_int)
11200 case 0:
11201 emit_insn (gen_cr6_test_for_zero (target));
11202 break;
11203 case 1:
11204 emit_insn (gen_cr6_test_for_zero_reverse (target));
11205 break;
11206 case 2:
11207 emit_insn (gen_cr6_test_for_lt (target));
11208 break;
11209 case 3:
11210 emit_insn (gen_cr6_test_for_lt_reverse (target));
11211 break;
11212 default:
11213 error ("argument 1 of __builtin_altivec_predicate is out of range");
11214 break;
11217 return target;
11220 static rtx
11221 paired_expand_lv_builtin (enum insn_code icode, tree exp, rtx target)
11223 rtx pat, addr;
11224 tree arg0 = CALL_EXPR_ARG (exp, 0);
11225 tree arg1 = CALL_EXPR_ARG (exp, 1);
11226 enum machine_mode tmode = insn_data[icode].operand[0].mode;
11227 enum machine_mode mode0 = Pmode;
11228 enum machine_mode mode1 = Pmode;
11229 rtx op0 = expand_normal (arg0);
11230 rtx op1 = expand_normal (arg1);
11232 if (icode == CODE_FOR_nothing)
11233 /* Builtin not supported on this processor. */
11234 return 0;
11236 /* If we got invalid arguments bail out before generating bad rtl. */
11237 if (arg0 == error_mark_node || arg1 == error_mark_node)
11238 return const0_rtx;
11240 if (target == 0
11241 || GET_MODE (target) != tmode
11242 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11243 target = gen_reg_rtx (tmode);
11245 op1 = copy_to_mode_reg (mode1, op1);
11247 if (op0 == const0_rtx)
11249 addr = gen_rtx_MEM (tmode, op1);
11251 else
11253 op0 = copy_to_mode_reg (mode0, op0);
11254 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op0, op1));
11257 pat = GEN_FCN (icode) (target, addr);
11259 if (! pat)
11260 return 0;
11261 emit_insn (pat);
11263 return target;
11266 static rtx
11267 altivec_expand_lv_builtin (enum insn_code icode, tree exp, rtx target, bool blk)
11269 rtx pat, addr;
11270 tree arg0 = CALL_EXPR_ARG (exp, 0);
11271 tree arg1 = CALL_EXPR_ARG (exp, 1);
11272 enum machine_mode tmode = insn_data[icode].operand[0].mode;
11273 enum machine_mode mode0 = Pmode;
11274 enum machine_mode mode1 = Pmode;
11275 rtx op0 = expand_normal (arg0);
11276 rtx op1 = expand_normal (arg1);
11278 if (icode == CODE_FOR_nothing)
11279 /* Builtin not supported on this processor. */
11280 return 0;
11282 /* If we got invalid arguments bail out before generating bad rtl. */
11283 if (arg0 == error_mark_node || arg1 == error_mark_node)
11284 return const0_rtx;
11286 if (target == 0
11287 || GET_MODE (target) != tmode
11288 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11289 target = gen_reg_rtx (tmode);
11291 op1 = copy_to_mode_reg (mode1, op1);
11293 if (op0 == const0_rtx)
11295 addr = gen_rtx_MEM (blk ? BLKmode : tmode, op1);
11297 else
11299 op0 = copy_to_mode_reg (mode0, op0);
11300 addr = gen_rtx_MEM (blk ? BLKmode : tmode, gen_rtx_PLUS (Pmode, op0, op1));
11303 pat = GEN_FCN (icode) (target, addr);
11305 if (! pat)
11306 return 0;
11307 emit_insn (pat);
11309 return target;
11312 static rtx
11313 spe_expand_stv_builtin (enum insn_code icode, tree exp)
11315 tree arg0 = CALL_EXPR_ARG (exp, 0);
11316 tree arg1 = CALL_EXPR_ARG (exp, 1);
11317 tree arg2 = CALL_EXPR_ARG (exp, 2);
11318 rtx op0 = expand_normal (arg0);
11319 rtx op1 = expand_normal (arg1);
11320 rtx op2 = expand_normal (arg2);
11321 rtx pat;
11322 enum machine_mode mode0 = insn_data[icode].operand[0].mode;
11323 enum machine_mode mode1 = insn_data[icode].operand[1].mode;
11324 enum machine_mode mode2 = insn_data[icode].operand[2].mode;
11326 /* Invalid arguments. Bail before doing anything stoopid! */
11327 if (arg0 == error_mark_node
11328 || arg1 == error_mark_node
11329 || arg2 == error_mark_node)
11330 return const0_rtx;
11332 if (! (*insn_data[icode].operand[2].predicate) (op0, mode2))
11333 op0 = copy_to_mode_reg (mode2, op0);
11334 if (! (*insn_data[icode].operand[0].predicate) (op1, mode0))
11335 op1 = copy_to_mode_reg (mode0, op1);
11336 if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
11337 op2 = copy_to_mode_reg (mode1, op2);
11339 pat = GEN_FCN (icode) (op1, op2, op0);
11340 if (pat)
11341 emit_insn (pat);
11342 return NULL_RTX;
11345 static rtx
11346 paired_expand_stv_builtin (enum insn_code icode, tree exp)
11348 tree arg0 = CALL_EXPR_ARG (exp, 0);
11349 tree arg1 = CALL_EXPR_ARG (exp, 1);
11350 tree arg2 = CALL_EXPR_ARG (exp, 2);
11351 rtx op0 = expand_normal (arg0);
11352 rtx op1 = expand_normal (arg1);
11353 rtx op2 = expand_normal (arg2);
11354 rtx pat, addr;
11355 enum machine_mode tmode = insn_data[icode].operand[0].mode;
11356 enum machine_mode mode1 = Pmode;
11357 enum machine_mode mode2 = Pmode;
11359 /* Invalid arguments. Bail before doing anything stoopid! */
11360 if (arg0 == error_mark_node
11361 || arg1 == error_mark_node
11362 || arg2 == error_mark_node)
11363 return const0_rtx;
11365 if (! (*insn_data[icode].operand[1].predicate) (op0, tmode))
11366 op0 = copy_to_mode_reg (tmode, op0);
11368 op2 = copy_to_mode_reg (mode2, op2);
11370 if (op1 == const0_rtx)
11372 addr = gen_rtx_MEM (tmode, op2);
11374 else
11376 op1 = copy_to_mode_reg (mode1, op1);
11377 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op1, op2));
11380 pat = GEN_FCN (icode) (addr, op0);
11381 if (pat)
11382 emit_insn (pat);
11383 return NULL_RTX;
11386 static rtx
11387 altivec_expand_stv_builtin (enum insn_code icode, tree exp)
11389 tree arg0 = CALL_EXPR_ARG (exp, 0);
11390 tree arg1 = CALL_EXPR_ARG (exp, 1);
11391 tree arg2 = CALL_EXPR_ARG (exp, 2);
11392 rtx op0 = expand_normal (arg0);
11393 rtx op1 = expand_normal (arg1);
11394 rtx op2 = expand_normal (arg2);
11395 rtx pat, addr;
11396 enum machine_mode tmode = insn_data[icode].operand[0].mode;
11397 enum machine_mode smode = insn_data[icode].operand[1].mode;
11398 enum machine_mode mode1 = Pmode;
11399 enum machine_mode mode2 = Pmode;
11401 /* Invalid arguments. Bail before doing anything stoopid! */
11402 if (arg0 == error_mark_node
11403 || arg1 == error_mark_node
11404 || arg2 == error_mark_node)
11405 return const0_rtx;
11407 if (! (*insn_data[icode].operand[1].predicate) (op0, smode))
11408 op0 = copy_to_mode_reg (smode, op0);
11410 op2 = copy_to_mode_reg (mode2, op2);
11412 if (op1 == const0_rtx)
11414 addr = gen_rtx_MEM (tmode, op2);
11416 else
11418 op1 = copy_to_mode_reg (mode1, op1);
11419 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op1, op2));
11422 pat = GEN_FCN (icode) (addr, op0);
11423 if (pat)
11424 emit_insn (pat);
11425 return NULL_RTX;
11428 /* Return the appropriate SPR number associated with the given builtin. */
11429 static inline HOST_WIDE_INT
11430 htm_spr_num (enum rs6000_builtins code)
11432 if (code == HTM_BUILTIN_GET_TFHAR
11433 || code == HTM_BUILTIN_SET_TFHAR)
11434 return TFHAR_SPR;
11435 else if (code == HTM_BUILTIN_GET_TFIAR
11436 || code == HTM_BUILTIN_SET_TFIAR)
11437 return TFIAR_SPR;
11438 else if (code == HTM_BUILTIN_GET_TEXASR
11439 || code == HTM_BUILTIN_SET_TEXASR)
11440 return TEXASR_SPR;
11441 gcc_assert (code == HTM_BUILTIN_GET_TEXASRU
11442 || code == HTM_BUILTIN_SET_TEXASRU);
11443 return TEXASRU_SPR;
11446 /* Return the appropriate SPR regno associated with the given builtin. */
11447 static inline HOST_WIDE_INT
11448 htm_spr_regno (enum rs6000_builtins code)
11450 if (code == HTM_BUILTIN_GET_TFHAR
11451 || code == HTM_BUILTIN_SET_TFHAR)
11452 return TFHAR_REGNO;
11453 else if (code == HTM_BUILTIN_GET_TFIAR
11454 || code == HTM_BUILTIN_SET_TFIAR)
11455 return TFIAR_REGNO;
11456 gcc_assert (code == HTM_BUILTIN_GET_TEXASR
11457 || code == HTM_BUILTIN_SET_TEXASR
11458 || code == HTM_BUILTIN_GET_TEXASRU
11459 || code == HTM_BUILTIN_SET_TEXASRU);
11460 return TEXASR_REGNO;
11463 /* Return the correct ICODE value depending on whether we are
11464 setting or reading the HTM SPRs. */
11465 static inline enum insn_code
11466 rs6000_htm_spr_icode (bool nonvoid)
11468 if (nonvoid)
11469 return (TARGET_64BIT) ? CODE_FOR_htm_mfspr_di : CODE_FOR_htm_mfspr_si;
11470 else
11471 return (TARGET_64BIT) ? CODE_FOR_htm_mtspr_di : CODE_FOR_htm_mtspr_si;
11474 /* Expand the HTM builtin in EXP and store the result in TARGET.
11475 Store true in *EXPANDEDP if we found a builtin to expand. */
11476 static rtx
11477 htm_expand_builtin (tree exp, rtx target, bool * expandedp)
11479 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
11480 bool nonvoid = TREE_TYPE (TREE_TYPE (fndecl)) != void_type_node;
11481 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
11482 const struct builtin_description *d;
11483 size_t i;
11485 *expandedp = false;
11487 /* Expand the HTM builtins. */
11488 d = bdesc_htm;
11489 for (i = 0; i < ARRAY_SIZE (bdesc_htm); i++, d++)
11490 if (d->code == fcode)
11492 rtx op[MAX_HTM_OPERANDS], pat;
11493 int nopnds = 0;
11494 tree arg;
11495 call_expr_arg_iterator iter;
11496 unsigned attr = rs6000_builtin_info[fcode].attr;
11497 enum insn_code icode = d->icode;
11499 if (attr & RS6000_BTC_SPR)
11500 icode = rs6000_htm_spr_icode (nonvoid);
11502 if (nonvoid)
11504 enum machine_mode tmode = insn_data[icode].operand[0].mode;
11505 if (!target
11506 || GET_MODE (target) != tmode
11507 || !(*insn_data[icode].operand[0].predicate) (target, tmode))
11508 target = gen_reg_rtx (tmode);
11509 op[nopnds++] = target;
11512 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
11514 const struct insn_operand_data *insn_op;
11516 if (arg == error_mark_node || nopnds >= MAX_HTM_OPERANDS)
11517 return NULL_RTX;
11519 insn_op = &insn_data[icode].operand[nopnds];
11521 op[nopnds] = expand_normal (arg);
11523 if (!(*insn_op->predicate) (op[nopnds], insn_op->mode))
11525 if (!strcmp (insn_op->constraint, "n"))
11527 int arg_num = (nonvoid) ? nopnds : nopnds + 1;
11528 if (!CONST_INT_P (op[nopnds]))
11529 error ("argument %d must be an unsigned literal", arg_num);
11530 else
11531 error ("argument %d is an unsigned literal that is "
11532 "out of range", arg_num);
11533 return const0_rtx;
11535 op[nopnds] = copy_to_mode_reg (insn_op->mode, op[nopnds]);
11538 nopnds++;
11541 /* Handle the builtins for extended mnemonics. These accept
11542 no arguments, but map to builtins that take arguments. */
11543 switch (fcode)
11545 case HTM_BUILTIN_TENDALL: /* Alias for: tend. 1 */
11546 case HTM_BUILTIN_TRESUME: /* Alias for: tsr. 1 */
11547 op[nopnds++] = GEN_INT (1);
11548 #ifdef ENABLE_CHECKING
11549 attr |= RS6000_BTC_UNARY;
11550 #endif
11551 break;
11552 case HTM_BUILTIN_TSUSPEND: /* Alias for: tsr. 0 */
11553 op[nopnds++] = GEN_INT (0);
11554 #ifdef ENABLE_CHECKING
11555 attr |= RS6000_BTC_UNARY;
11556 #endif
11557 break;
11558 default:
11559 break;
11562 /* If this builtin accesses SPRs, then pass in the appropriate
11563 SPR number and SPR regno as the last two operands. */
11564 if (attr & RS6000_BTC_SPR)
11566 op[nopnds++] = gen_rtx_CONST_INT (Pmode, htm_spr_num (fcode));
11567 op[nopnds++] = gen_rtx_REG (Pmode, htm_spr_regno (fcode));
11570 #ifdef ENABLE_CHECKING
11571 int expected_nopnds = 0;
11572 if ((attr & RS6000_BTC_TYPE_MASK) == RS6000_BTC_UNARY)
11573 expected_nopnds = 1;
11574 else if ((attr & RS6000_BTC_TYPE_MASK) == RS6000_BTC_BINARY)
11575 expected_nopnds = 2;
11576 else if ((attr & RS6000_BTC_TYPE_MASK) == RS6000_BTC_TERNARY)
11577 expected_nopnds = 3;
11578 if (!(attr & RS6000_BTC_VOID))
11579 expected_nopnds += 1;
11580 if (attr & RS6000_BTC_SPR)
11581 expected_nopnds += 2;
11583 gcc_assert (nopnds == expected_nopnds && nopnds <= MAX_HTM_OPERANDS);
11584 #endif
11586 switch (nopnds)
11588 case 1:
11589 pat = GEN_FCN (icode) (op[0]);
11590 break;
11591 case 2:
11592 pat = GEN_FCN (icode) (op[0], op[1]);
11593 break;
11594 case 3:
11595 pat = GEN_FCN (icode) (op[0], op[1], op[2]);
11596 break;
11597 case 4:
11598 pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3]);
11599 break;
11600 default:
11601 gcc_unreachable ();
11603 if (!pat)
11604 return NULL_RTX;
11605 emit_insn (pat);
11607 *expandedp = true;
11608 if (nonvoid)
11609 return target;
11610 return const0_rtx;
11613 return NULL_RTX;
11616 static rtx
11617 rs6000_expand_ternop_builtin (enum insn_code icode, tree exp, rtx target)
11619 rtx pat;
11620 tree arg0 = CALL_EXPR_ARG (exp, 0);
11621 tree arg1 = CALL_EXPR_ARG (exp, 1);
11622 tree arg2 = CALL_EXPR_ARG (exp, 2);
11623 rtx op0 = expand_normal (arg0);
11624 rtx op1 = expand_normal (arg1);
11625 rtx op2 = expand_normal (arg2);
11626 enum machine_mode tmode = insn_data[icode].operand[0].mode;
11627 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11628 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
11629 enum machine_mode mode2 = insn_data[icode].operand[3].mode;
11631 if (icode == CODE_FOR_nothing)
11632 /* Builtin not supported on this processor. */
11633 return 0;
11635 /* If we got invalid arguments bail out before generating bad rtl. */
11636 if (arg0 == error_mark_node
11637 || arg1 == error_mark_node
11638 || arg2 == error_mark_node)
11639 return const0_rtx;
11641 /* Check and prepare argument depending on the instruction code.
11643 Note that a switch statement instead of the sequence of tests
11644 would be incorrect as many of the CODE_FOR values could be
11645 CODE_FOR_nothing and that would yield multiple alternatives
11646 with identical values. We'd never reach here at runtime in
11647 this case. */
11648 if (icode == CODE_FOR_altivec_vsldoi_v4sf
11649 || icode == CODE_FOR_altivec_vsldoi_v4si
11650 || icode == CODE_FOR_altivec_vsldoi_v8hi
11651 || icode == CODE_FOR_altivec_vsldoi_v16qi)
11653 /* Only allow 4-bit unsigned literals. */
11654 STRIP_NOPS (arg2);
11655 if (TREE_CODE (arg2) != INTEGER_CST
11656 || TREE_INT_CST_LOW (arg2) & ~0xf)
11658 error ("argument 3 must be a 4-bit unsigned literal");
11659 return const0_rtx;
11662 else if (icode == CODE_FOR_vsx_xxpermdi_v2df
11663 || icode == CODE_FOR_vsx_xxpermdi_v2di
11664 || icode == CODE_FOR_vsx_xxsldwi_v16qi
11665 || icode == CODE_FOR_vsx_xxsldwi_v8hi
11666 || icode == CODE_FOR_vsx_xxsldwi_v4si
11667 || icode == CODE_FOR_vsx_xxsldwi_v4sf
11668 || icode == CODE_FOR_vsx_xxsldwi_v2di
11669 || icode == CODE_FOR_vsx_xxsldwi_v2df)
11671 /* Only allow 2-bit unsigned literals. */
11672 STRIP_NOPS (arg2);
11673 if (TREE_CODE (arg2) != INTEGER_CST
11674 || TREE_INT_CST_LOW (arg2) & ~0x3)
11676 error ("argument 3 must be a 2-bit unsigned literal");
11677 return const0_rtx;
11680 else if (icode == CODE_FOR_vsx_set_v2df
11681 || icode == CODE_FOR_vsx_set_v2di)
11683 /* Only allow 1-bit unsigned literals. */
11684 STRIP_NOPS (arg2);
11685 if (TREE_CODE (arg2) != INTEGER_CST
11686 || TREE_INT_CST_LOW (arg2) & ~0x1)
11688 error ("argument 3 must be a 1-bit unsigned literal");
11689 return const0_rtx;
11692 else if (icode == CODE_FOR_crypto_vshasigmaw
11693 || icode == CODE_FOR_crypto_vshasigmad)
11695 /* Check whether the 2nd and 3rd arguments are integer constants and in
11696 range and prepare arguments. */
11697 STRIP_NOPS (arg1);
11698 if (TREE_CODE (arg1) != INTEGER_CST
11699 || !IN_RANGE (TREE_INT_CST_LOW (arg1), 0, 1))
11701 error ("argument 2 must be 0 or 1");
11702 return const0_rtx;
11705 STRIP_NOPS (arg2);
11706 if (TREE_CODE (arg2) != INTEGER_CST
11707 || !IN_RANGE (TREE_INT_CST_LOW (arg2), 0, 15))
11709 error ("argument 3 must be in the range 0..15");
11710 return const0_rtx;
11714 if (target == 0
11715 || GET_MODE (target) != tmode
11716 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11717 target = gen_reg_rtx (tmode);
11719 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11720 op0 = copy_to_mode_reg (mode0, op0);
11721 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
11722 op1 = copy_to_mode_reg (mode1, op1);
11723 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
11724 op2 = copy_to_mode_reg (mode2, op2);
11726 if (TARGET_PAIRED_FLOAT && icode == CODE_FOR_selv2sf4)
11727 pat = GEN_FCN (icode) (target, op0, op1, op2, CONST0_RTX (SFmode));
11728 else
11729 pat = GEN_FCN (icode) (target, op0, op1, op2);
11730 if (! pat)
11731 return 0;
11732 emit_insn (pat);
11734 return target;
11737 /* Expand the lvx builtins. */
11738 static rtx
11739 altivec_expand_ld_builtin (tree exp, rtx target, bool *expandedp)
11741 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
11742 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
11743 tree arg0;
11744 enum machine_mode tmode, mode0;
11745 rtx pat, op0;
11746 enum insn_code icode;
11748 switch (fcode)
11750 case ALTIVEC_BUILTIN_LD_INTERNAL_16qi:
11751 icode = CODE_FOR_vector_altivec_load_v16qi;
11752 break;
11753 case ALTIVEC_BUILTIN_LD_INTERNAL_8hi:
11754 icode = CODE_FOR_vector_altivec_load_v8hi;
11755 break;
11756 case ALTIVEC_BUILTIN_LD_INTERNAL_4si:
11757 icode = CODE_FOR_vector_altivec_load_v4si;
11758 break;
11759 case ALTIVEC_BUILTIN_LD_INTERNAL_4sf:
11760 icode = CODE_FOR_vector_altivec_load_v4sf;
11761 break;
11762 case ALTIVEC_BUILTIN_LD_INTERNAL_2df:
11763 icode = CODE_FOR_vector_altivec_load_v2df;
11764 break;
11765 case ALTIVEC_BUILTIN_LD_INTERNAL_2di:
11766 icode = CODE_FOR_vector_altivec_load_v2di;
11767 break;
11768 default:
11769 *expandedp = false;
11770 return NULL_RTX;
11773 *expandedp = true;
11775 arg0 = CALL_EXPR_ARG (exp, 0);
11776 op0 = expand_normal (arg0);
11777 tmode = insn_data[icode].operand[0].mode;
11778 mode0 = insn_data[icode].operand[1].mode;
11780 if (target == 0
11781 || GET_MODE (target) != tmode
11782 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11783 target = gen_reg_rtx (tmode);
11785 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11786 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
11788 pat = GEN_FCN (icode) (target, op0);
11789 if (! pat)
11790 return 0;
11791 emit_insn (pat);
11792 return target;
11795 /* Expand the stvx builtins. */
11796 static rtx
11797 altivec_expand_st_builtin (tree exp, rtx target ATTRIBUTE_UNUSED,
11798 bool *expandedp)
11800 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
11801 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
11802 tree arg0, arg1;
11803 enum machine_mode mode0, mode1;
11804 rtx pat, op0, op1;
11805 enum insn_code icode;
11807 switch (fcode)
11809 case ALTIVEC_BUILTIN_ST_INTERNAL_16qi:
11810 icode = CODE_FOR_vector_altivec_store_v16qi;
11811 break;
11812 case ALTIVEC_BUILTIN_ST_INTERNAL_8hi:
11813 icode = CODE_FOR_vector_altivec_store_v8hi;
11814 break;
11815 case ALTIVEC_BUILTIN_ST_INTERNAL_4si:
11816 icode = CODE_FOR_vector_altivec_store_v4si;
11817 break;
11818 case ALTIVEC_BUILTIN_ST_INTERNAL_4sf:
11819 icode = CODE_FOR_vector_altivec_store_v4sf;
11820 break;
11821 case ALTIVEC_BUILTIN_ST_INTERNAL_2df:
11822 icode = CODE_FOR_vector_altivec_store_v2df;
11823 break;
11824 case ALTIVEC_BUILTIN_ST_INTERNAL_2di:
11825 icode = CODE_FOR_vector_altivec_store_v2di;
11826 break;
11827 default:
11828 *expandedp = false;
11829 return NULL_RTX;
11832 arg0 = CALL_EXPR_ARG (exp, 0);
11833 arg1 = CALL_EXPR_ARG (exp, 1);
11834 op0 = expand_normal (arg0);
11835 op1 = expand_normal (arg1);
11836 mode0 = insn_data[icode].operand[0].mode;
11837 mode1 = insn_data[icode].operand[1].mode;
11839 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
11840 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
11841 if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
11842 op1 = copy_to_mode_reg (mode1, op1);
11844 pat = GEN_FCN (icode) (op0, op1);
11845 if (pat)
11846 emit_insn (pat);
11848 *expandedp = true;
11849 return NULL_RTX;
11852 /* Expand the dst builtins. */
11853 static rtx
11854 altivec_expand_dst_builtin (tree exp, rtx target ATTRIBUTE_UNUSED,
11855 bool *expandedp)
11857 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
11858 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
11859 tree arg0, arg1, arg2;
11860 enum machine_mode mode0, mode1;
11861 rtx pat, op0, op1, op2;
11862 const struct builtin_description *d;
11863 size_t i;
11865 *expandedp = false;
11867 /* Handle DST variants. */
11868 d = bdesc_dst;
11869 for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
11870 if (d->code == fcode)
11872 arg0 = CALL_EXPR_ARG (exp, 0);
11873 arg1 = CALL_EXPR_ARG (exp, 1);
11874 arg2 = CALL_EXPR_ARG (exp, 2);
11875 op0 = expand_normal (arg0);
11876 op1 = expand_normal (arg1);
11877 op2 = expand_normal (arg2);
11878 mode0 = insn_data[d->icode].operand[0].mode;
11879 mode1 = insn_data[d->icode].operand[1].mode;
11881 /* Invalid arguments, bail out before generating bad rtl. */
11882 if (arg0 == error_mark_node
11883 || arg1 == error_mark_node
11884 || arg2 == error_mark_node)
11885 return const0_rtx;
11887 *expandedp = true;
11888 STRIP_NOPS (arg2);
11889 if (TREE_CODE (arg2) != INTEGER_CST
11890 || TREE_INT_CST_LOW (arg2) & ~0x3)
11892 error ("argument to %qs must be a 2-bit unsigned literal", d->name);
11893 return const0_rtx;
11896 if (! (*insn_data[d->icode].operand[0].predicate) (op0, mode0))
11897 op0 = copy_to_mode_reg (Pmode, op0);
11898 if (! (*insn_data[d->icode].operand[1].predicate) (op1, mode1))
11899 op1 = copy_to_mode_reg (mode1, op1);
11901 pat = GEN_FCN (d->icode) (op0, op1, op2);
11902 if (pat != 0)
11903 emit_insn (pat);
11905 return NULL_RTX;
11908 return NULL_RTX;
11911 /* Expand vec_init builtin. */
11912 static rtx
11913 altivec_expand_vec_init_builtin (tree type, tree exp, rtx target)
11915 enum machine_mode tmode = TYPE_MODE (type);
11916 enum machine_mode inner_mode = GET_MODE_INNER (tmode);
11917 int i, n_elt = GET_MODE_NUNITS (tmode);
11918 rtvec v = rtvec_alloc (n_elt);
11920 gcc_assert (VECTOR_MODE_P (tmode));
11921 gcc_assert (n_elt == call_expr_nargs (exp));
11923 for (i = 0; i < n_elt; ++i)
11925 rtx x = expand_normal (CALL_EXPR_ARG (exp, i));
11926 RTVEC_ELT (v, i) = gen_lowpart (inner_mode, x);
11929 if (!target || !register_operand (target, tmode))
11930 target = gen_reg_rtx (tmode);
11932 rs6000_expand_vector_init (target, gen_rtx_PARALLEL (tmode, v));
11933 return target;
11936 /* Return the integer constant in ARG. Constrain it to be in the range
11937 of the subparts of VEC_TYPE; issue an error if not. */
11939 static int
11940 get_element_number (tree vec_type, tree arg)
11942 unsigned HOST_WIDE_INT elt, max = TYPE_VECTOR_SUBPARTS (vec_type) - 1;
11944 if (!host_integerp (arg, 1)
11945 || (elt = tree_low_cst (arg, 1), elt > max))
11947 error ("selector must be an integer constant in the range 0..%wi", max);
11948 return 0;
11951 return elt;
11954 /* Expand vec_set builtin. */
11955 static rtx
11956 altivec_expand_vec_set_builtin (tree exp)
11958 enum machine_mode tmode, mode1;
11959 tree arg0, arg1, arg2;
11960 int elt;
11961 rtx op0, op1;
11963 arg0 = CALL_EXPR_ARG (exp, 0);
11964 arg1 = CALL_EXPR_ARG (exp, 1);
11965 arg2 = CALL_EXPR_ARG (exp, 2);
11967 tmode = TYPE_MODE (TREE_TYPE (arg0));
11968 mode1 = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
11969 gcc_assert (VECTOR_MODE_P (tmode));
11971 op0 = expand_expr (arg0, NULL_RTX, tmode, EXPAND_NORMAL);
11972 op1 = expand_expr (arg1, NULL_RTX, mode1, EXPAND_NORMAL);
11973 elt = get_element_number (TREE_TYPE (arg0), arg2);
11975 if (GET_MODE (op1) != mode1 && GET_MODE (op1) != VOIDmode)
11976 op1 = convert_modes (mode1, GET_MODE (op1), op1, true);
11978 op0 = force_reg (tmode, op0);
11979 op1 = force_reg (mode1, op1);
11981 rs6000_expand_vector_set (op0, op1, elt);
11983 return op0;
11986 /* Expand vec_ext builtin. */
11987 static rtx
11988 altivec_expand_vec_ext_builtin (tree exp, rtx target)
11990 enum machine_mode tmode, mode0;
11991 tree arg0, arg1;
11992 int elt;
11993 rtx op0;
11995 arg0 = CALL_EXPR_ARG (exp, 0);
11996 arg1 = CALL_EXPR_ARG (exp, 1);
11998 op0 = expand_normal (arg0);
11999 elt = get_element_number (TREE_TYPE (arg0), arg1);
12001 tmode = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
12002 mode0 = TYPE_MODE (TREE_TYPE (arg0));
12003 gcc_assert (VECTOR_MODE_P (mode0));
12005 op0 = force_reg (mode0, op0);
12007 if (optimize || !target || !register_operand (target, tmode))
12008 target = gen_reg_rtx (tmode);
12010 rs6000_expand_vector_extract (target, op0, elt);
12012 return target;
12015 /* Expand the builtin in EXP and store the result in TARGET. Store
12016 true in *EXPANDEDP if we found a builtin to expand. */
12017 static rtx
12018 altivec_expand_builtin (tree exp, rtx target, bool *expandedp)
12020 const struct builtin_description *d;
12021 size_t i;
12022 enum insn_code icode;
12023 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
12024 tree arg0;
12025 rtx op0, pat;
12026 enum machine_mode tmode, mode0;
12027 enum rs6000_builtins fcode
12028 = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
12030 if (rs6000_overloaded_builtin_p (fcode))
12032 *expandedp = true;
12033 error ("unresolved overload for Altivec builtin %qF", fndecl);
12035 /* Given it is invalid, just generate a normal call. */
12036 return expand_call (exp, target, false);
12039 target = altivec_expand_ld_builtin (exp, target, expandedp);
12040 if (*expandedp)
12041 return target;
12043 target = altivec_expand_st_builtin (exp, target, expandedp);
12044 if (*expandedp)
12045 return target;
12047 target = altivec_expand_dst_builtin (exp, target, expandedp);
12048 if (*expandedp)
12049 return target;
12051 *expandedp = true;
12053 switch (fcode)
12055 case ALTIVEC_BUILTIN_STVX:
12056 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v4si, exp);
12057 case ALTIVEC_BUILTIN_STVEBX:
12058 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvebx, exp);
12059 case ALTIVEC_BUILTIN_STVEHX:
12060 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvehx, exp);
12061 case ALTIVEC_BUILTIN_STVEWX:
12062 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvewx, exp);
12063 case ALTIVEC_BUILTIN_STVXL:
12064 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl, exp);
12066 case ALTIVEC_BUILTIN_STVLX:
12067 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvlx, exp);
12068 case ALTIVEC_BUILTIN_STVLXL:
12069 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvlxl, exp);
12070 case ALTIVEC_BUILTIN_STVRX:
12071 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvrx, exp);
12072 case ALTIVEC_BUILTIN_STVRXL:
12073 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvrxl, exp);
12075 case VSX_BUILTIN_STXVD2X_V2DF:
12076 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v2df, exp);
12077 case VSX_BUILTIN_STXVD2X_V2DI:
12078 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v2di, exp);
12079 case VSX_BUILTIN_STXVW4X_V4SF:
12080 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v4sf, exp);
12081 case VSX_BUILTIN_STXVW4X_V4SI:
12082 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v4si, exp);
12083 case VSX_BUILTIN_STXVW4X_V8HI:
12084 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v8hi, exp);
12085 case VSX_BUILTIN_STXVW4X_V16QI:
12086 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v16qi, exp);
12088 case ALTIVEC_BUILTIN_MFVSCR:
12089 icode = CODE_FOR_altivec_mfvscr;
12090 tmode = insn_data[icode].operand[0].mode;
12092 if (target == 0
12093 || GET_MODE (target) != tmode
12094 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12095 target = gen_reg_rtx (tmode);
12097 pat = GEN_FCN (icode) (target);
12098 if (! pat)
12099 return 0;
12100 emit_insn (pat);
12101 return target;
12103 case ALTIVEC_BUILTIN_MTVSCR:
12104 icode = CODE_FOR_altivec_mtvscr;
12105 arg0 = CALL_EXPR_ARG (exp, 0);
12106 op0 = expand_normal (arg0);
12107 mode0 = insn_data[icode].operand[0].mode;
12109 /* If we got invalid arguments bail out before generating bad rtl. */
12110 if (arg0 == error_mark_node)
12111 return const0_rtx;
12113 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
12114 op0 = copy_to_mode_reg (mode0, op0);
12116 pat = GEN_FCN (icode) (op0);
12117 if (pat)
12118 emit_insn (pat);
12119 return NULL_RTX;
12121 case ALTIVEC_BUILTIN_DSSALL:
12122 emit_insn (gen_altivec_dssall ());
12123 return NULL_RTX;
12125 case ALTIVEC_BUILTIN_DSS:
12126 icode = CODE_FOR_altivec_dss;
12127 arg0 = CALL_EXPR_ARG (exp, 0);
12128 STRIP_NOPS (arg0);
12129 op0 = expand_normal (arg0);
12130 mode0 = insn_data[icode].operand[0].mode;
12132 /* If we got invalid arguments bail out before generating bad rtl. */
12133 if (arg0 == error_mark_node)
12134 return const0_rtx;
12136 if (TREE_CODE (arg0) != INTEGER_CST
12137 || TREE_INT_CST_LOW (arg0) & ~0x3)
12139 error ("argument to dss must be a 2-bit unsigned literal");
12140 return const0_rtx;
12143 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
12144 op0 = copy_to_mode_reg (mode0, op0);
12146 emit_insn (gen_altivec_dss (op0));
12147 return NULL_RTX;
12149 case ALTIVEC_BUILTIN_VEC_INIT_V4SI:
12150 case ALTIVEC_BUILTIN_VEC_INIT_V8HI:
12151 case ALTIVEC_BUILTIN_VEC_INIT_V16QI:
12152 case ALTIVEC_BUILTIN_VEC_INIT_V4SF:
12153 case VSX_BUILTIN_VEC_INIT_V2DF:
12154 case VSX_BUILTIN_VEC_INIT_V2DI:
12155 return altivec_expand_vec_init_builtin (TREE_TYPE (exp), exp, target);
12157 case ALTIVEC_BUILTIN_VEC_SET_V4SI:
12158 case ALTIVEC_BUILTIN_VEC_SET_V8HI:
12159 case ALTIVEC_BUILTIN_VEC_SET_V16QI:
12160 case ALTIVEC_BUILTIN_VEC_SET_V4SF:
12161 case VSX_BUILTIN_VEC_SET_V2DF:
12162 case VSX_BUILTIN_VEC_SET_V2DI:
12163 return altivec_expand_vec_set_builtin (exp);
12165 case ALTIVEC_BUILTIN_VEC_EXT_V4SI:
12166 case ALTIVEC_BUILTIN_VEC_EXT_V8HI:
12167 case ALTIVEC_BUILTIN_VEC_EXT_V16QI:
12168 case ALTIVEC_BUILTIN_VEC_EXT_V4SF:
12169 case VSX_BUILTIN_VEC_EXT_V2DF:
12170 case VSX_BUILTIN_VEC_EXT_V2DI:
12171 return altivec_expand_vec_ext_builtin (exp, target);
12173 default:
12174 break;
12175 /* Fall through. */
12178 /* Expand abs* operations. */
12179 d = bdesc_abs;
12180 for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
12181 if (d->code == fcode)
12182 return altivec_expand_abs_builtin (d->icode, exp, target);
12184 /* Expand the AltiVec predicates. */
12185 d = bdesc_altivec_preds;
12186 for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, d++)
12187 if (d->code == fcode)
12188 return altivec_expand_predicate_builtin (d->icode, exp, target);
12190 /* LV* are funky. We initialized them differently. */
12191 switch (fcode)
12193 case ALTIVEC_BUILTIN_LVSL:
12194 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsl,
12195 exp, target, false);
12196 case ALTIVEC_BUILTIN_LVSR:
12197 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsr,
12198 exp, target, false);
12199 case ALTIVEC_BUILTIN_LVEBX:
12200 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvebx,
12201 exp, target, false);
12202 case ALTIVEC_BUILTIN_LVEHX:
12203 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvehx,
12204 exp, target, false);
12205 case ALTIVEC_BUILTIN_LVEWX:
12206 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvewx,
12207 exp, target, false);
12208 case ALTIVEC_BUILTIN_LVXL:
12209 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl,
12210 exp, target, false);
12211 case ALTIVEC_BUILTIN_LVX:
12212 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v4si,
12213 exp, target, false);
12214 case ALTIVEC_BUILTIN_LVLX:
12215 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvlx,
12216 exp, target, true);
12217 case ALTIVEC_BUILTIN_LVLXL:
12218 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvlxl,
12219 exp, target, true);
12220 case ALTIVEC_BUILTIN_LVRX:
12221 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvrx,
12222 exp, target, true);
12223 case ALTIVEC_BUILTIN_LVRXL:
12224 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvrxl,
12225 exp, target, true);
12226 case VSX_BUILTIN_LXVD2X_V2DF:
12227 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v2df,
12228 exp, target, false);
12229 case VSX_BUILTIN_LXVD2X_V2DI:
12230 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v2di,
12231 exp, target, false);
12232 case VSX_BUILTIN_LXVW4X_V4SF:
12233 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v4sf,
12234 exp, target, false);
12235 case VSX_BUILTIN_LXVW4X_V4SI:
12236 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v4si,
12237 exp, target, false);
12238 case VSX_BUILTIN_LXVW4X_V8HI:
12239 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v8hi,
12240 exp, target, false);
12241 case VSX_BUILTIN_LXVW4X_V16QI:
12242 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v16qi,
12243 exp, target, false);
12244 break;
12245 default:
12246 break;
12247 /* Fall through. */
12250 *expandedp = false;
12251 return NULL_RTX;
12254 /* Expand the builtin in EXP and store the result in TARGET. Store
12255 true in *EXPANDEDP if we found a builtin to expand. */
12256 static rtx
12257 paired_expand_builtin (tree exp, rtx target, bool * expandedp)
12259 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
12260 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
12261 const struct builtin_description *d;
12262 size_t i;
12264 *expandedp = true;
12266 switch (fcode)
12268 case PAIRED_BUILTIN_STX:
12269 return paired_expand_stv_builtin (CODE_FOR_paired_stx, exp);
12270 case PAIRED_BUILTIN_LX:
12271 return paired_expand_lv_builtin (CODE_FOR_paired_lx, exp, target);
12272 default:
12273 break;
12274 /* Fall through. */
12277 /* Expand the paired predicates. */
12278 d = bdesc_paired_preds;
12279 for (i = 0; i < ARRAY_SIZE (bdesc_paired_preds); i++, d++)
12280 if (d->code == fcode)
12281 return paired_expand_predicate_builtin (d->icode, exp, target);
12283 *expandedp = false;
12284 return NULL_RTX;
12287 /* Binops that need to be initialized manually, but can be expanded
12288 automagically by rs6000_expand_binop_builtin. */
12289 static const struct builtin_description bdesc_2arg_spe[] =
12291 { RS6000_BTM_SPE, CODE_FOR_spe_evlddx, "__builtin_spe_evlddx", SPE_BUILTIN_EVLDDX },
12292 { RS6000_BTM_SPE, CODE_FOR_spe_evldwx, "__builtin_spe_evldwx", SPE_BUILTIN_EVLDWX },
12293 { RS6000_BTM_SPE, CODE_FOR_spe_evldhx, "__builtin_spe_evldhx", SPE_BUILTIN_EVLDHX },
12294 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhex, "__builtin_spe_evlwhex", SPE_BUILTIN_EVLWHEX },
12295 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhoux, "__builtin_spe_evlwhoux", SPE_BUILTIN_EVLWHOUX },
12296 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhosx, "__builtin_spe_evlwhosx", SPE_BUILTIN_EVLWHOSX },
12297 { RS6000_BTM_SPE, CODE_FOR_spe_evlwwsplatx, "__builtin_spe_evlwwsplatx", SPE_BUILTIN_EVLWWSPLATX },
12298 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhsplatx, "__builtin_spe_evlwhsplatx", SPE_BUILTIN_EVLWHSPLATX },
12299 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhesplatx, "__builtin_spe_evlhhesplatx", SPE_BUILTIN_EVLHHESPLATX },
12300 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhousplatx, "__builtin_spe_evlhhousplatx", SPE_BUILTIN_EVLHHOUSPLATX },
12301 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhossplatx, "__builtin_spe_evlhhossplatx", SPE_BUILTIN_EVLHHOSSPLATX },
12302 { RS6000_BTM_SPE, CODE_FOR_spe_evldd, "__builtin_spe_evldd", SPE_BUILTIN_EVLDD },
12303 { RS6000_BTM_SPE, CODE_FOR_spe_evldw, "__builtin_spe_evldw", SPE_BUILTIN_EVLDW },
12304 { RS6000_BTM_SPE, CODE_FOR_spe_evldh, "__builtin_spe_evldh", SPE_BUILTIN_EVLDH },
12305 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhe, "__builtin_spe_evlwhe", SPE_BUILTIN_EVLWHE },
12306 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhou, "__builtin_spe_evlwhou", SPE_BUILTIN_EVLWHOU },
12307 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhos, "__builtin_spe_evlwhos", SPE_BUILTIN_EVLWHOS },
12308 { RS6000_BTM_SPE, CODE_FOR_spe_evlwwsplat, "__builtin_spe_evlwwsplat", SPE_BUILTIN_EVLWWSPLAT },
12309 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhsplat, "__builtin_spe_evlwhsplat", SPE_BUILTIN_EVLWHSPLAT },
12310 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhesplat, "__builtin_spe_evlhhesplat", SPE_BUILTIN_EVLHHESPLAT },
12311 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhousplat, "__builtin_spe_evlhhousplat", SPE_BUILTIN_EVLHHOUSPLAT },
12312 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhossplat, "__builtin_spe_evlhhossplat", SPE_BUILTIN_EVLHHOSSPLAT }
12315 /* Expand the builtin in EXP and store the result in TARGET. Store
12316 true in *EXPANDEDP if we found a builtin to expand.
12318 This expands the SPE builtins that are not simple unary and binary
12319 operations. */
12320 static rtx
12321 spe_expand_builtin (tree exp, rtx target, bool *expandedp)
12323 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
12324 tree arg1, arg0;
12325 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
12326 enum insn_code icode;
12327 enum machine_mode tmode, mode0;
12328 rtx pat, op0;
12329 const struct builtin_description *d;
12330 size_t i;
12332 *expandedp = true;
12334 /* Syntax check for a 5-bit unsigned immediate. */
12335 switch (fcode)
12337 case SPE_BUILTIN_EVSTDD:
12338 case SPE_BUILTIN_EVSTDH:
12339 case SPE_BUILTIN_EVSTDW:
12340 case SPE_BUILTIN_EVSTWHE:
12341 case SPE_BUILTIN_EVSTWHO:
12342 case SPE_BUILTIN_EVSTWWE:
12343 case SPE_BUILTIN_EVSTWWO:
12344 arg1 = CALL_EXPR_ARG (exp, 2);
12345 if (TREE_CODE (arg1) != INTEGER_CST
12346 || TREE_INT_CST_LOW (arg1) & ~0x1f)
12348 error ("argument 2 must be a 5-bit unsigned literal");
12349 return const0_rtx;
12351 break;
12352 default:
12353 break;
12356 /* The evsplat*i instructions are not quite generic. */
12357 switch (fcode)
12359 case SPE_BUILTIN_EVSPLATFI:
12360 return rs6000_expand_unop_builtin (CODE_FOR_spe_evsplatfi,
12361 exp, target);
12362 case SPE_BUILTIN_EVSPLATI:
12363 return rs6000_expand_unop_builtin (CODE_FOR_spe_evsplati,
12364 exp, target);
12365 default:
12366 break;
12369 d = bdesc_2arg_spe;
12370 for (i = 0; i < ARRAY_SIZE (bdesc_2arg_spe); ++i, ++d)
12371 if (d->code == fcode)
12372 return rs6000_expand_binop_builtin (d->icode, exp, target);
12374 d = bdesc_spe_predicates;
12375 for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, ++d)
12376 if (d->code == fcode)
12377 return spe_expand_predicate_builtin (d->icode, exp, target);
12379 d = bdesc_spe_evsel;
12380 for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, ++d)
12381 if (d->code == fcode)
12382 return spe_expand_evsel_builtin (d->icode, exp, target);
12384 switch (fcode)
12386 case SPE_BUILTIN_EVSTDDX:
12387 return spe_expand_stv_builtin (CODE_FOR_spe_evstddx, exp);
12388 case SPE_BUILTIN_EVSTDHX:
12389 return spe_expand_stv_builtin (CODE_FOR_spe_evstdhx, exp);
12390 case SPE_BUILTIN_EVSTDWX:
12391 return spe_expand_stv_builtin (CODE_FOR_spe_evstdwx, exp);
12392 case SPE_BUILTIN_EVSTWHEX:
12393 return spe_expand_stv_builtin (CODE_FOR_spe_evstwhex, exp);
12394 case SPE_BUILTIN_EVSTWHOX:
12395 return spe_expand_stv_builtin (CODE_FOR_spe_evstwhox, exp);
12396 case SPE_BUILTIN_EVSTWWEX:
12397 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwex, exp);
12398 case SPE_BUILTIN_EVSTWWOX:
12399 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwox, exp);
12400 case SPE_BUILTIN_EVSTDD:
12401 return spe_expand_stv_builtin (CODE_FOR_spe_evstdd, exp);
12402 case SPE_BUILTIN_EVSTDH:
12403 return spe_expand_stv_builtin (CODE_FOR_spe_evstdh, exp);
12404 case SPE_BUILTIN_EVSTDW:
12405 return spe_expand_stv_builtin (CODE_FOR_spe_evstdw, exp);
12406 case SPE_BUILTIN_EVSTWHE:
12407 return spe_expand_stv_builtin (CODE_FOR_spe_evstwhe, exp);
12408 case SPE_BUILTIN_EVSTWHO:
12409 return spe_expand_stv_builtin (CODE_FOR_spe_evstwho, exp);
12410 case SPE_BUILTIN_EVSTWWE:
12411 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwe, exp);
12412 case SPE_BUILTIN_EVSTWWO:
12413 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwo, exp);
12414 case SPE_BUILTIN_MFSPEFSCR:
12415 icode = CODE_FOR_spe_mfspefscr;
12416 tmode = insn_data[icode].operand[0].mode;
12418 if (target == 0
12419 || GET_MODE (target) != tmode
12420 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12421 target = gen_reg_rtx (tmode);
12423 pat = GEN_FCN (icode) (target);
12424 if (! pat)
12425 return 0;
12426 emit_insn (pat);
12427 return target;
12428 case SPE_BUILTIN_MTSPEFSCR:
12429 icode = CODE_FOR_spe_mtspefscr;
12430 arg0 = CALL_EXPR_ARG (exp, 0);
12431 op0 = expand_normal (arg0);
12432 mode0 = insn_data[icode].operand[0].mode;
12434 if (arg0 == error_mark_node)
12435 return const0_rtx;
12437 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
12438 op0 = copy_to_mode_reg (mode0, op0);
12440 pat = GEN_FCN (icode) (op0);
12441 if (pat)
12442 emit_insn (pat);
12443 return NULL_RTX;
12444 default:
12445 break;
12448 *expandedp = false;
12449 return NULL_RTX;
12452 static rtx
12453 paired_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
12455 rtx pat, scratch, tmp;
12456 tree form = CALL_EXPR_ARG (exp, 0);
12457 tree arg0 = CALL_EXPR_ARG (exp, 1);
12458 tree arg1 = CALL_EXPR_ARG (exp, 2);
12459 rtx op0 = expand_normal (arg0);
12460 rtx op1 = expand_normal (arg1);
12461 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12462 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
12463 int form_int;
12464 enum rtx_code code;
12466 if (TREE_CODE (form) != INTEGER_CST)
12468 error ("argument 1 of __builtin_paired_predicate must be a constant");
12469 return const0_rtx;
12471 else
12472 form_int = TREE_INT_CST_LOW (form);
12474 gcc_assert (mode0 == mode1);
12476 if (arg0 == error_mark_node || arg1 == error_mark_node)
12477 return const0_rtx;
12479 if (target == 0
12480 || GET_MODE (target) != SImode
12481 || !(*insn_data[icode].operand[0].predicate) (target, SImode))
12482 target = gen_reg_rtx (SImode);
12483 if (!(*insn_data[icode].operand[1].predicate) (op0, mode0))
12484 op0 = copy_to_mode_reg (mode0, op0);
12485 if (!(*insn_data[icode].operand[2].predicate) (op1, mode1))
12486 op1 = copy_to_mode_reg (mode1, op1);
12488 scratch = gen_reg_rtx (CCFPmode);
12490 pat = GEN_FCN (icode) (scratch, op0, op1);
12491 if (!pat)
12492 return const0_rtx;
12494 emit_insn (pat);
12496 switch (form_int)
12498 /* LT bit. */
12499 case 0:
12500 code = LT;
12501 break;
12502 /* GT bit. */
12503 case 1:
12504 code = GT;
12505 break;
12506 /* EQ bit. */
12507 case 2:
12508 code = EQ;
12509 break;
12510 /* UN bit. */
12511 case 3:
12512 emit_insn (gen_move_from_CR_ov_bit (target, scratch));
12513 return target;
12514 default:
12515 error ("argument 1 of __builtin_paired_predicate is out of range");
12516 return const0_rtx;
12519 tmp = gen_rtx_fmt_ee (code, SImode, scratch, const0_rtx);
12520 emit_move_insn (target, tmp);
12521 return target;
12524 static rtx
12525 spe_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
12527 rtx pat, scratch, tmp;
12528 tree form = CALL_EXPR_ARG (exp, 0);
12529 tree arg0 = CALL_EXPR_ARG (exp, 1);
12530 tree arg1 = CALL_EXPR_ARG (exp, 2);
12531 rtx op0 = expand_normal (arg0);
12532 rtx op1 = expand_normal (arg1);
12533 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12534 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
12535 int form_int;
12536 enum rtx_code code;
12538 if (TREE_CODE (form) != INTEGER_CST)
12540 error ("argument 1 of __builtin_spe_predicate must be a constant");
12541 return const0_rtx;
12543 else
12544 form_int = TREE_INT_CST_LOW (form);
12546 gcc_assert (mode0 == mode1);
12548 if (arg0 == error_mark_node || arg1 == error_mark_node)
12549 return const0_rtx;
12551 if (target == 0
12552 || GET_MODE (target) != SImode
12553 || ! (*insn_data[icode].operand[0].predicate) (target, SImode))
12554 target = gen_reg_rtx (SImode);
12556 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12557 op0 = copy_to_mode_reg (mode0, op0);
12558 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12559 op1 = copy_to_mode_reg (mode1, op1);
12561 scratch = gen_reg_rtx (CCmode);
12563 pat = GEN_FCN (icode) (scratch, op0, op1);
12564 if (! pat)
12565 return const0_rtx;
12566 emit_insn (pat);
12568 /* There are 4 variants for each predicate: _any_, _all_, _upper_,
12569 _lower_. We use one compare, but look in different bits of the
12570 CR for each variant.
12572 There are 2 elements in each SPE simd type (upper/lower). The CR
12573 bits are set as follows:
12575 BIT0 | BIT 1 | BIT 2 | BIT 3
12576 U | L | (U | L) | (U & L)
12578 So, for an "all" relationship, BIT 3 would be set.
12579 For an "any" relationship, BIT 2 would be set. Etc.
12581 Following traditional nomenclature, these bits map to:
12583 BIT0 | BIT 1 | BIT 2 | BIT 3
12584 LT | GT | EQ | OV
12586 Later, we will generate rtl to look in the LT/EQ/EQ/OV bits.
12589 switch (form_int)
12591 /* All variant. OV bit. */
12592 case 0:
12593 /* We need to get to the OV bit, which is the ORDERED bit. We
12594 could generate (ordered:SI (reg:CC xx) (const_int 0)), but
12595 that's ugly and will make validate_condition_mode die.
12596 So let's just use another pattern. */
12597 emit_insn (gen_move_from_CR_ov_bit (target, scratch));
12598 return target;
12599 /* Any variant. EQ bit. */
12600 case 1:
12601 code = EQ;
12602 break;
12603 /* Upper variant. LT bit. */
12604 case 2:
12605 code = LT;
12606 break;
12607 /* Lower variant. GT bit. */
12608 case 3:
12609 code = GT;
12610 break;
12611 default:
12612 error ("argument 1 of __builtin_spe_predicate is out of range");
12613 return const0_rtx;
12616 tmp = gen_rtx_fmt_ee (code, SImode, scratch, const0_rtx);
12617 emit_move_insn (target, tmp);
12619 return target;
12622 /* The evsel builtins look like this:
12624 e = __builtin_spe_evsel_OP (a, b, c, d);
12626 and work like this:
12628 e[upper] = a[upper] *OP* b[upper] ? c[upper] : d[upper];
12629 e[lower] = a[lower] *OP* b[lower] ? c[lower] : d[lower];
12632 static rtx
12633 spe_expand_evsel_builtin (enum insn_code icode, tree exp, rtx target)
12635 rtx pat, scratch;
12636 tree arg0 = CALL_EXPR_ARG (exp, 0);
12637 tree arg1 = CALL_EXPR_ARG (exp, 1);
12638 tree arg2 = CALL_EXPR_ARG (exp, 2);
12639 tree arg3 = CALL_EXPR_ARG (exp, 3);
12640 rtx op0 = expand_normal (arg0);
12641 rtx op1 = expand_normal (arg1);
12642 rtx op2 = expand_normal (arg2);
12643 rtx op3 = expand_normal (arg3);
12644 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12645 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
12647 gcc_assert (mode0 == mode1);
12649 if (arg0 == error_mark_node || arg1 == error_mark_node
12650 || arg2 == error_mark_node || arg3 == error_mark_node)
12651 return const0_rtx;
12653 if (target == 0
12654 || GET_MODE (target) != mode0
12655 || ! (*insn_data[icode].operand[0].predicate) (target, mode0))
12656 target = gen_reg_rtx (mode0);
12658 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12659 op0 = copy_to_mode_reg (mode0, op0);
12660 if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
12661 op1 = copy_to_mode_reg (mode0, op1);
12662 if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
12663 op2 = copy_to_mode_reg (mode0, op2);
12664 if (! (*insn_data[icode].operand[1].predicate) (op3, mode1))
12665 op3 = copy_to_mode_reg (mode0, op3);
12667 /* Generate the compare. */
12668 scratch = gen_reg_rtx (CCmode);
12669 pat = GEN_FCN (icode) (scratch, op0, op1);
12670 if (! pat)
12671 return const0_rtx;
12672 emit_insn (pat);
12674 if (mode0 == V2SImode)
12675 emit_insn (gen_spe_evsel (target, op2, op3, scratch));
12676 else
12677 emit_insn (gen_spe_evsel_fs (target, op2, op3, scratch));
12679 return target;
12682 /* Raise an error message for a builtin function that is called without the
12683 appropriate target options being set. */
12685 static void
12686 rs6000_invalid_builtin (enum rs6000_builtins fncode)
12688 size_t uns_fncode = (size_t)fncode;
12689 const char *name = rs6000_builtin_info[uns_fncode].name;
12690 HOST_WIDE_INT fnmask = rs6000_builtin_info[uns_fncode].mask;
12692 gcc_assert (name != NULL);
12693 if ((fnmask & RS6000_BTM_CELL) != 0)
12694 error ("Builtin function %s is only valid for the cell processor", name);
12695 else if ((fnmask & RS6000_BTM_VSX) != 0)
12696 error ("Builtin function %s requires the -mvsx option", name);
12697 else if ((fnmask & RS6000_BTM_HTM) != 0)
12698 error ("Builtin function %s requires the -mhtm option", name);
12699 else if ((fnmask & RS6000_BTM_ALTIVEC) != 0)
12700 error ("Builtin function %s requires the -maltivec option", name);
12701 else if ((fnmask & RS6000_BTM_PAIRED) != 0)
12702 error ("Builtin function %s requires the -mpaired option", name);
12703 else if ((fnmask & RS6000_BTM_SPE) != 0)
12704 error ("Builtin function %s requires the -mspe option", name);
12705 else
12706 error ("Builtin function %s is not supported with the current options",
12707 name);
12710 /* Expand an expression EXP that calls a built-in function,
12711 with result going to TARGET if that's convenient
12712 (and in mode MODE if that's convenient).
12713 SUBTARGET may be used as the target for computing one of EXP's operands.
12714 IGNORE is nonzero if the value is to be ignored. */
12716 static rtx
12717 rs6000_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
12718 enum machine_mode mode ATTRIBUTE_UNUSED,
12719 int ignore ATTRIBUTE_UNUSED)
12721 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
12722 enum rs6000_builtins fcode
12723 = (enum rs6000_builtins)DECL_FUNCTION_CODE (fndecl);
12724 size_t uns_fcode = (size_t)fcode;
12725 const struct builtin_description *d;
12726 size_t i;
12727 rtx ret;
12728 bool success;
12729 HOST_WIDE_INT mask = rs6000_builtin_info[uns_fcode].mask;
12730 bool func_valid_p = ((rs6000_builtin_mask & mask) == mask);
12732 if (TARGET_DEBUG_BUILTIN)
12734 enum insn_code icode = rs6000_builtin_info[uns_fcode].icode;
12735 const char *name1 = rs6000_builtin_info[uns_fcode].name;
12736 const char *name2 = ((icode != CODE_FOR_nothing)
12737 ? get_insn_name ((int)icode)
12738 : "nothing");
12739 const char *name3;
12741 switch (rs6000_builtin_info[uns_fcode].attr & RS6000_BTC_TYPE_MASK)
12743 default: name3 = "unknown"; break;
12744 case RS6000_BTC_SPECIAL: name3 = "special"; break;
12745 case RS6000_BTC_UNARY: name3 = "unary"; break;
12746 case RS6000_BTC_BINARY: name3 = "binary"; break;
12747 case RS6000_BTC_TERNARY: name3 = "ternary"; break;
12748 case RS6000_BTC_PREDICATE: name3 = "predicate"; break;
12749 case RS6000_BTC_ABS: name3 = "abs"; break;
12750 case RS6000_BTC_EVSEL: name3 = "evsel"; break;
12751 case RS6000_BTC_DST: name3 = "dst"; break;
12755 fprintf (stderr,
12756 "rs6000_expand_builtin, %s (%d), insn = %s (%d), type=%s%s\n",
12757 (name1) ? name1 : "---", fcode,
12758 (name2) ? name2 : "---", (int)icode,
12759 name3,
12760 func_valid_p ? "" : ", not valid");
12763 if (!func_valid_p)
12765 rs6000_invalid_builtin (fcode);
12767 /* Given it is invalid, just generate a normal call. */
12768 return expand_call (exp, target, ignore);
12771 switch (fcode)
12773 case RS6000_BUILTIN_RECIP:
12774 return rs6000_expand_binop_builtin (CODE_FOR_recipdf3, exp, target);
12776 case RS6000_BUILTIN_RECIPF:
12777 return rs6000_expand_binop_builtin (CODE_FOR_recipsf3, exp, target);
12779 case RS6000_BUILTIN_RSQRTF:
12780 return rs6000_expand_unop_builtin (CODE_FOR_rsqrtsf2, exp, target);
12782 case RS6000_BUILTIN_RSQRT:
12783 return rs6000_expand_unop_builtin (CODE_FOR_rsqrtdf2, exp, target);
12785 case POWER7_BUILTIN_BPERMD:
12786 return rs6000_expand_binop_builtin (((TARGET_64BIT)
12787 ? CODE_FOR_bpermd_di
12788 : CODE_FOR_bpermd_si), exp, target);
12790 case RS6000_BUILTIN_GET_TB:
12791 return rs6000_expand_zeroop_builtin (CODE_FOR_rs6000_get_timebase,
12792 target);
12794 case RS6000_BUILTIN_MFTB:
12795 return rs6000_expand_zeroop_builtin (((TARGET_64BIT)
12796 ? CODE_FOR_rs6000_mftb_di
12797 : CODE_FOR_rs6000_mftb_si),
12798 target);
12800 case ALTIVEC_BUILTIN_MASK_FOR_LOAD:
12801 case ALTIVEC_BUILTIN_MASK_FOR_STORE:
12803 int icode = (BYTES_BIG_ENDIAN ? (int) CODE_FOR_altivec_lvsr
12804 : (int) CODE_FOR_altivec_lvsl);
12805 enum machine_mode tmode = insn_data[icode].operand[0].mode;
12806 enum machine_mode mode = insn_data[icode].operand[1].mode;
12807 tree arg;
12808 rtx op, addr, pat;
12810 gcc_assert (TARGET_ALTIVEC);
12812 arg = CALL_EXPR_ARG (exp, 0);
12813 gcc_assert (POINTER_TYPE_P (TREE_TYPE (arg)));
12814 op = expand_expr (arg, NULL_RTX, Pmode, EXPAND_NORMAL);
12815 addr = memory_address (mode, op);
12816 if (fcode == ALTIVEC_BUILTIN_MASK_FOR_STORE)
12817 op = addr;
12818 else
12820 /* For the load case need to negate the address. */
12821 op = gen_reg_rtx (GET_MODE (addr));
12822 emit_insn (gen_rtx_SET (VOIDmode, op,
12823 gen_rtx_NEG (GET_MODE (addr), addr)));
12825 op = gen_rtx_MEM (mode, op);
12827 if (target == 0
12828 || GET_MODE (target) != tmode
12829 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12830 target = gen_reg_rtx (tmode);
12832 /*pat = gen_altivec_lvsr (target, op);*/
12833 pat = GEN_FCN (icode) (target, op);
12834 if (!pat)
12835 return 0;
12836 emit_insn (pat);
12838 return target;
12841 case ALTIVEC_BUILTIN_VCFUX:
12842 case ALTIVEC_BUILTIN_VCFSX:
12843 case ALTIVEC_BUILTIN_VCTUXS:
12844 case ALTIVEC_BUILTIN_VCTSXS:
12845 /* FIXME: There's got to be a nicer way to handle this case than
12846 constructing a new CALL_EXPR. */
12847 if (call_expr_nargs (exp) == 1)
12849 exp = build_call_nary (TREE_TYPE (exp), CALL_EXPR_FN (exp),
12850 2, CALL_EXPR_ARG (exp, 0), integer_zero_node);
12852 break;
12854 default:
12855 break;
12858 if (TARGET_ALTIVEC)
12860 ret = altivec_expand_builtin (exp, target, &success);
12862 if (success)
12863 return ret;
12865 if (TARGET_SPE)
12867 ret = spe_expand_builtin (exp, target, &success);
12869 if (success)
12870 return ret;
12872 if (TARGET_PAIRED_FLOAT)
12874 ret = paired_expand_builtin (exp, target, &success);
12876 if (success)
12877 return ret;
12879 if (TARGET_HTM)
12881 ret = htm_expand_builtin (exp, target, &success);
12883 if (success)
12884 return ret;
12887 gcc_assert (TARGET_ALTIVEC || TARGET_VSX || TARGET_SPE || TARGET_PAIRED_FLOAT);
12889 /* Handle simple unary operations. */
12890 d = bdesc_1arg;
12891 for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
12892 if (d->code == fcode)
12893 return rs6000_expand_unop_builtin (d->icode, exp, target);
12895 /* Handle simple binary operations. */
12896 d = bdesc_2arg;
12897 for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
12898 if (d->code == fcode)
12899 return rs6000_expand_binop_builtin (d->icode, exp, target);
12901 /* Handle simple ternary operations. */
12902 d = bdesc_3arg;
12903 for (i = 0; i < ARRAY_SIZE (bdesc_3arg); i++, d++)
12904 if (d->code == fcode)
12905 return rs6000_expand_ternop_builtin (d->icode, exp, target);
12907 gcc_unreachable ();
12910 static void
12911 rs6000_init_builtins (void)
12913 tree tdecl;
12914 tree ftype;
12915 enum machine_mode mode;
12917 if (TARGET_DEBUG_BUILTIN)
12918 fprintf (stderr, "rs6000_init_builtins%s%s%s%s\n",
12919 (TARGET_PAIRED_FLOAT) ? ", paired" : "",
12920 (TARGET_SPE) ? ", spe" : "",
12921 (TARGET_ALTIVEC) ? ", altivec" : "",
12922 (TARGET_VSX) ? ", vsx" : "");
12924 V2SI_type_node = build_vector_type (intSI_type_node, 2);
12925 V2SF_type_node = build_vector_type (float_type_node, 2);
12926 V2DI_type_node = build_vector_type (intDI_type_node, 2);
12927 V2DF_type_node = build_vector_type (double_type_node, 2);
12928 V4HI_type_node = build_vector_type (intHI_type_node, 4);
12929 V4SI_type_node = build_vector_type (intSI_type_node, 4);
12930 V4SF_type_node = build_vector_type (float_type_node, 4);
12931 V8HI_type_node = build_vector_type (intHI_type_node, 8);
12932 V16QI_type_node = build_vector_type (intQI_type_node, 16);
12934 unsigned_V16QI_type_node = build_vector_type (unsigned_intQI_type_node, 16);
12935 unsigned_V8HI_type_node = build_vector_type (unsigned_intHI_type_node, 8);
12936 unsigned_V4SI_type_node = build_vector_type (unsigned_intSI_type_node, 4);
12937 unsigned_V2DI_type_node = build_vector_type (unsigned_intDI_type_node, 2);
12939 opaque_V2SF_type_node = build_opaque_vector_type (float_type_node, 2);
12940 opaque_V2SI_type_node = build_opaque_vector_type (intSI_type_node, 2);
12941 opaque_p_V2SI_type_node = build_pointer_type (opaque_V2SI_type_node);
12942 opaque_V4SI_type_node = build_opaque_vector_type (intSI_type_node, 4);
12944 /* The 'vector bool ...' types must be kept distinct from 'vector unsigned ...'
12945 types, especially in C++ land. Similarly, 'vector pixel' is distinct from
12946 'vector unsigned short'. */
12948 bool_char_type_node = build_distinct_type_copy (unsigned_intQI_type_node);
12949 bool_short_type_node = build_distinct_type_copy (unsigned_intHI_type_node);
12950 bool_int_type_node = build_distinct_type_copy (unsigned_intSI_type_node);
12951 bool_long_type_node = build_distinct_type_copy (unsigned_intDI_type_node);
12952 pixel_type_node = build_distinct_type_copy (unsigned_intHI_type_node);
12954 long_integer_type_internal_node = long_integer_type_node;
12955 long_unsigned_type_internal_node = long_unsigned_type_node;
12956 long_long_integer_type_internal_node = long_long_integer_type_node;
12957 long_long_unsigned_type_internal_node = long_long_unsigned_type_node;
12958 intQI_type_internal_node = intQI_type_node;
12959 uintQI_type_internal_node = unsigned_intQI_type_node;
12960 intHI_type_internal_node = intHI_type_node;
12961 uintHI_type_internal_node = unsigned_intHI_type_node;
12962 intSI_type_internal_node = intSI_type_node;
12963 uintSI_type_internal_node = unsigned_intSI_type_node;
12964 intDI_type_internal_node = intDI_type_node;
12965 uintDI_type_internal_node = unsigned_intDI_type_node;
12966 float_type_internal_node = float_type_node;
12967 double_type_internal_node = double_type_node;
12968 void_type_internal_node = void_type_node;
12970 /* Initialize the modes for builtin_function_type, mapping a machine mode to
12971 tree type node. */
12972 builtin_mode_to_type[QImode][0] = integer_type_node;
12973 builtin_mode_to_type[HImode][0] = integer_type_node;
12974 builtin_mode_to_type[SImode][0] = intSI_type_node;
12975 builtin_mode_to_type[SImode][1] = unsigned_intSI_type_node;
12976 builtin_mode_to_type[DImode][0] = intDI_type_node;
12977 builtin_mode_to_type[DImode][1] = unsigned_intDI_type_node;
12978 builtin_mode_to_type[SFmode][0] = float_type_node;
12979 builtin_mode_to_type[DFmode][0] = double_type_node;
12980 builtin_mode_to_type[V2SImode][0] = V2SI_type_node;
12981 builtin_mode_to_type[V2SFmode][0] = V2SF_type_node;
12982 builtin_mode_to_type[V2DImode][0] = V2DI_type_node;
12983 builtin_mode_to_type[V2DImode][1] = unsigned_V2DI_type_node;
12984 builtin_mode_to_type[V2DFmode][0] = V2DF_type_node;
12985 builtin_mode_to_type[V4HImode][0] = V4HI_type_node;
12986 builtin_mode_to_type[V4SImode][0] = V4SI_type_node;
12987 builtin_mode_to_type[V4SImode][1] = unsigned_V4SI_type_node;
12988 builtin_mode_to_type[V4SFmode][0] = V4SF_type_node;
12989 builtin_mode_to_type[V8HImode][0] = V8HI_type_node;
12990 builtin_mode_to_type[V8HImode][1] = unsigned_V8HI_type_node;
12991 builtin_mode_to_type[V16QImode][0] = V16QI_type_node;
12992 builtin_mode_to_type[V16QImode][1] = unsigned_V16QI_type_node;
12994 tdecl = add_builtin_type ("__bool char", bool_char_type_node);
12995 TYPE_NAME (bool_char_type_node) = tdecl;
12997 tdecl = add_builtin_type ("__bool short", bool_short_type_node);
12998 TYPE_NAME (bool_short_type_node) = tdecl;
13000 tdecl = add_builtin_type ("__bool int", bool_int_type_node);
13001 TYPE_NAME (bool_int_type_node) = tdecl;
13003 tdecl = add_builtin_type ("__pixel", pixel_type_node);
13004 TYPE_NAME (pixel_type_node) = tdecl;
13006 bool_V16QI_type_node = build_vector_type (bool_char_type_node, 16);
13007 bool_V8HI_type_node = build_vector_type (bool_short_type_node, 8);
13008 bool_V4SI_type_node = build_vector_type (bool_int_type_node, 4);
13009 bool_V2DI_type_node = build_vector_type (bool_long_type_node, 2);
13010 pixel_V8HI_type_node = build_vector_type (pixel_type_node, 8);
13012 tdecl = add_builtin_type ("__vector unsigned char", unsigned_V16QI_type_node);
13013 TYPE_NAME (unsigned_V16QI_type_node) = tdecl;
13015 tdecl = add_builtin_type ("__vector signed char", V16QI_type_node);
13016 TYPE_NAME (V16QI_type_node) = tdecl;
13018 tdecl = add_builtin_type ("__vector __bool char", bool_V16QI_type_node);
13019 TYPE_NAME ( bool_V16QI_type_node) = tdecl;
13021 tdecl = add_builtin_type ("__vector unsigned short", unsigned_V8HI_type_node);
13022 TYPE_NAME (unsigned_V8HI_type_node) = tdecl;
13024 tdecl = add_builtin_type ("__vector signed short", V8HI_type_node);
13025 TYPE_NAME (V8HI_type_node) = tdecl;
13027 tdecl = add_builtin_type ("__vector __bool short", bool_V8HI_type_node);
13028 TYPE_NAME (bool_V8HI_type_node) = tdecl;
13030 tdecl = add_builtin_type ("__vector unsigned int", unsigned_V4SI_type_node);
13031 TYPE_NAME (unsigned_V4SI_type_node) = tdecl;
13033 tdecl = add_builtin_type ("__vector signed int", V4SI_type_node);
13034 TYPE_NAME (V4SI_type_node) = tdecl;
13036 tdecl = add_builtin_type ("__vector __bool int", bool_V4SI_type_node);
13037 TYPE_NAME (bool_V4SI_type_node) = tdecl;
13039 tdecl = add_builtin_type ("__vector float", V4SF_type_node);
13040 TYPE_NAME (V4SF_type_node) = tdecl;
13042 tdecl = add_builtin_type ("__vector __pixel", pixel_V8HI_type_node);
13043 TYPE_NAME (pixel_V8HI_type_node) = tdecl;
13045 tdecl = add_builtin_type ("__vector double", V2DF_type_node);
13046 TYPE_NAME (V2DF_type_node) = tdecl;
13048 tdecl = add_builtin_type ("__vector long", V2DI_type_node);
13049 TYPE_NAME (V2DI_type_node) = tdecl;
13051 tdecl = add_builtin_type ("__vector unsigned long", unsigned_V2DI_type_node);
13052 TYPE_NAME (unsigned_V2DI_type_node) = tdecl;
13054 tdecl = add_builtin_type ("__vector __bool long", bool_V2DI_type_node);
13055 TYPE_NAME (bool_V2DI_type_node) = tdecl;
13057 /* Paired and SPE builtins are only available if you build a compiler with
13058 the appropriate options, so only create those builtins with the
13059 appropriate compiler option. Create Altivec and VSX builtins on machines
13060 with at least the general purpose extensions (970 and newer) to allow the
13061 use of the target attribute. */
13062 if (TARGET_PAIRED_FLOAT)
13063 paired_init_builtins ();
13064 if (TARGET_SPE)
13065 spe_init_builtins ();
13066 if (TARGET_EXTRA_BUILTINS)
13067 altivec_init_builtins ();
13068 if (TARGET_HTM)
13069 htm_init_builtins ();
13071 if (TARGET_EXTRA_BUILTINS || TARGET_SPE || TARGET_PAIRED_FLOAT)
13072 rs6000_common_init_builtins ();
13074 ftype = builtin_function_type (DFmode, DFmode, DFmode, VOIDmode,
13075 RS6000_BUILTIN_RECIP, "__builtin_recipdiv");
13076 def_builtin ("__builtin_recipdiv", ftype, RS6000_BUILTIN_RECIP);
13078 ftype = builtin_function_type (SFmode, SFmode, SFmode, VOIDmode,
13079 RS6000_BUILTIN_RECIPF, "__builtin_recipdivf");
13080 def_builtin ("__builtin_recipdivf", ftype, RS6000_BUILTIN_RECIPF);
13082 ftype = builtin_function_type (DFmode, DFmode, VOIDmode, VOIDmode,
13083 RS6000_BUILTIN_RSQRT, "__builtin_rsqrt");
13084 def_builtin ("__builtin_rsqrt", ftype, RS6000_BUILTIN_RSQRT);
13086 ftype = builtin_function_type (SFmode, SFmode, VOIDmode, VOIDmode,
13087 RS6000_BUILTIN_RSQRTF, "__builtin_rsqrtf");
13088 def_builtin ("__builtin_rsqrtf", ftype, RS6000_BUILTIN_RSQRTF);
13090 mode = (TARGET_64BIT) ? DImode : SImode;
13091 ftype = builtin_function_type (mode, mode, mode, VOIDmode,
13092 POWER7_BUILTIN_BPERMD, "__builtin_bpermd");
13093 def_builtin ("__builtin_bpermd", ftype, POWER7_BUILTIN_BPERMD);
13095 ftype = build_function_type_list (unsigned_intDI_type_node,
13096 NULL_TREE);
13097 def_builtin ("__builtin_ppc_get_timebase", ftype, RS6000_BUILTIN_GET_TB);
13099 if (TARGET_64BIT)
13100 ftype = build_function_type_list (unsigned_intDI_type_node,
13101 NULL_TREE);
13102 else
13103 ftype = build_function_type_list (unsigned_intSI_type_node,
13104 NULL_TREE);
13105 def_builtin ("__builtin_ppc_mftb", ftype, RS6000_BUILTIN_MFTB);
13107 #if TARGET_XCOFF
13108 /* AIX libm provides clog as __clog. */
13109 if ((tdecl = builtin_decl_explicit (BUILT_IN_CLOG)) != NULL_TREE)
13110 set_user_assembler_name (tdecl, "__clog");
13111 #endif
13113 #ifdef SUBTARGET_INIT_BUILTINS
13114 SUBTARGET_INIT_BUILTINS;
13115 #endif
13118 /* Returns the rs6000 builtin decl for CODE. */
13120 static tree
13121 rs6000_builtin_decl (unsigned code, bool initialize_p ATTRIBUTE_UNUSED)
13123 HOST_WIDE_INT fnmask;
13125 if (code >= RS6000_BUILTIN_COUNT)
13126 return error_mark_node;
13128 fnmask = rs6000_builtin_info[code].mask;
13129 if ((fnmask & rs6000_builtin_mask) != fnmask)
13131 rs6000_invalid_builtin ((enum rs6000_builtins)code);
13132 return error_mark_node;
13135 return rs6000_builtin_decls[code];
13138 static void
13139 spe_init_builtins (void)
13141 tree puint_type_node = build_pointer_type (unsigned_type_node);
13142 tree pushort_type_node = build_pointer_type (short_unsigned_type_node);
13143 const struct builtin_description *d;
13144 size_t i;
13146 tree v2si_ftype_4_v2si
13147 = build_function_type_list (opaque_V2SI_type_node,
13148 opaque_V2SI_type_node,
13149 opaque_V2SI_type_node,
13150 opaque_V2SI_type_node,
13151 opaque_V2SI_type_node,
13152 NULL_TREE);
13154 tree v2sf_ftype_4_v2sf
13155 = build_function_type_list (opaque_V2SF_type_node,
13156 opaque_V2SF_type_node,
13157 opaque_V2SF_type_node,
13158 opaque_V2SF_type_node,
13159 opaque_V2SF_type_node,
13160 NULL_TREE);
13162 tree int_ftype_int_v2si_v2si
13163 = build_function_type_list (integer_type_node,
13164 integer_type_node,
13165 opaque_V2SI_type_node,
13166 opaque_V2SI_type_node,
13167 NULL_TREE);
13169 tree int_ftype_int_v2sf_v2sf
13170 = build_function_type_list (integer_type_node,
13171 integer_type_node,
13172 opaque_V2SF_type_node,
13173 opaque_V2SF_type_node,
13174 NULL_TREE);
13176 tree void_ftype_v2si_puint_int
13177 = build_function_type_list (void_type_node,
13178 opaque_V2SI_type_node,
13179 puint_type_node,
13180 integer_type_node,
13181 NULL_TREE);
13183 tree void_ftype_v2si_puint_char
13184 = build_function_type_list (void_type_node,
13185 opaque_V2SI_type_node,
13186 puint_type_node,
13187 char_type_node,
13188 NULL_TREE);
13190 tree void_ftype_v2si_pv2si_int
13191 = build_function_type_list (void_type_node,
13192 opaque_V2SI_type_node,
13193 opaque_p_V2SI_type_node,
13194 integer_type_node,
13195 NULL_TREE);
13197 tree void_ftype_v2si_pv2si_char
13198 = build_function_type_list (void_type_node,
13199 opaque_V2SI_type_node,
13200 opaque_p_V2SI_type_node,
13201 char_type_node,
13202 NULL_TREE);
13204 tree void_ftype_int
13205 = build_function_type_list (void_type_node, integer_type_node, NULL_TREE);
13207 tree int_ftype_void
13208 = build_function_type_list (integer_type_node, NULL_TREE);
13210 tree v2si_ftype_pv2si_int
13211 = build_function_type_list (opaque_V2SI_type_node,
13212 opaque_p_V2SI_type_node,
13213 integer_type_node,
13214 NULL_TREE);
13216 tree v2si_ftype_puint_int
13217 = build_function_type_list (opaque_V2SI_type_node,
13218 puint_type_node,
13219 integer_type_node,
13220 NULL_TREE);
13222 tree v2si_ftype_pushort_int
13223 = build_function_type_list (opaque_V2SI_type_node,
13224 pushort_type_node,
13225 integer_type_node,
13226 NULL_TREE);
13228 tree v2si_ftype_signed_char
13229 = build_function_type_list (opaque_V2SI_type_node,
13230 signed_char_type_node,
13231 NULL_TREE);
13233 add_builtin_type ("__ev64_opaque__", opaque_V2SI_type_node);
13235 /* Initialize irregular SPE builtins. */
13237 def_builtin ("__builtin_spe_mtspefscr", void_ftype_int, SPE_BUILTIN_MTSPEFSCR);
13238 def_builtin ("__builtin_spe_mfspefscr", int_ftype_void, SPE_BUILTIN_MFSPEFSCR);
13239 def_builtin ("__builtin_spe_evstddx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDDX);
13240 def_builtin ("__builtin_spe_evstdhx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDHX);
13241 def_builtin ("__builtin_spe_evstdwx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDWX);
13242 def_builtin ("__builtin_spe_evstwhex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHEX);
13243 def_builtin ("__builtin_spe_evstwhox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHOX);
13244 def_builtin ("__builtin_spe_evstwwex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWEX);
13245 def_builtin ("__builtin_spe_evstwwox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWOX);
13246 def_builtin ("__builtin_spe_evstdd", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDD);
13247 def_builtin ("__builtin_spe_evstdh", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDH);
13248 def_builtin ("__builtin_spe_evstdw", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDW);
13249 def_builtin ("__builtin_spe_evstwhe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHE);
13250 def_builtin ("__builtin_spe_evstwho", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHO);
13251 def_builtin ("__builtin_spe_evstwwe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWE);
13252 def_builtin ("__builtin_spe_evstwwo", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWO);
13253 def_builtin ("__builtin_spe_evsplatfi", v2si_ftype_signed_char, SPE_BUILTIN_EVSPLATFI);
13254 def_builtin ("__builtin_spe_evsplati", v2si_ftype_signed_char, SPE_BUILTIN_EVSPLATI);
13256 /* Loads. */
13257 def_builtin ("__builtin_spe_evlddx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDDX);
13258 def_builtin ("__builtin_spe_evldwx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDWX);
13259 def_builtin ("__builtin_spe_evldhx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDHX);
13260 def_builtin ("__builtin_spe_evlwhex", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHEX);
13261 def_builtin ("__builtin_spe_evlwhoux", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOUX);
13262 def_builtin ("__builtin_spe_evlwhosx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOSX);
13263 def_builtin ("__builtin_spe_evlwwsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLATX);
13264 def_builtin ("__builtin_spe_evlwhsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLATX);
13265 def_builtin ("__builtin_spe_evlhhesplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLATX);
13266 def_builtin ("__builtin_spe_evlhhousplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLATX);
13267 def_builtin ("__builtin_spe_evlhhossplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLATX);
13268 def_builtin ("__builtin_spe_evldd", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDD);
13269 def_builtin ("__builtin_spe_evldw", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDW);
13270 def_builtin ("__builtin_spe_evldh", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDH);
13271 def_builtin ("__builtin_spe_evlhhesplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLAT);
13272 def_builtin ("__builtin_spe_evlhhossplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLAT);
13273 def_builtin ("__builtin_spe_evlhhousplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLAT);
13274 def_builtin ("__builtin_spe_evlwhe", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHE);
13275 def_builtin ("__builtin_spe_evlwhos", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOS);
13276 def_builtin ("__builtin_spe_evlwhou", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOU);
13277 def_builtin ("__builtin_spe_evlwhsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLAT);
13278 def_builtin ("__builtin_spe_evlwwsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLAT);
13280 /* Predicates. */
13281 d = bdesc_spe_predicates;
13282 for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, d++)
13284 tree type;
13286 switch (insn_data[d->icode].operand[1].mode)
13288 case V2SImode:
13289 type = int_ftype_int_v2si_v2si;
13290 break;
13291 case V2SFmode:
13292 type = int_ftype_int_v2sf_v2sf;
13293 break;
13294 default:
13295 gcc_unreachable ();
13298 def_builtin (d->name, type, d->code);
13301 /* Evsel predicates. */
13302 d = bdesc_spe_evsel;
13303 for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, d++)
13305 tree type;
13307 switch (insn_data[d->icode].operand[1].mode)
13309 case V2SImode:
13310 type = v2si_ftype_4_v2si;
13311 break;
13312 case V2SFmode:
13313 type = v2sf_ftype_4_v2sf;
13314 break;
13315 default:
13316 gcc_unreachable ();
13319 def_builtin (d->name, type, d->code);
13323 static void
13324 paired_init_builtins (void)
13326 const struct builtin_description *d;
13327 size_t i;
13329 tree int_ftype_int_v2sf_v2sf
13330 = build_function_type_list (integer_type_node,
13331 integer_type_node,
13332 V2SF_type_node,
13333 V2SF_type_node,
13334 NULL_TREE);
13335 tree pcfloat_type_node =
13336 build_pointer_type (build_qualified_type
13337 (float_type_node, TYPE_QUAL_CONST));
13339 tree v2sf_ftype_long_pcfloat = build_function_type_list (V2SF_type_node,
13340 long_integer_type_node,
13341 pcfloat_type_node,
13342 NULL_TREE);
13343 tree void_ftype_v2sf_long_pcfloat =
13344 build_function_type_list (void_type_node,
13345 V2SF_type_node,
13346 long_integer_type_node,
13347 pcfloat_type_node,
13348 NULL_TREE);
13351 def_builtin ("__builtin_paired_lx", v2sf_ftype_long_pcfloat,
13352 PAIRED_BUILTIN_LX);
13355 def_builtin ("__builtin_paired_stx", void_ftype_v2sf_long_pcfloat,
13356 PAIRED_BUILTIN_STX);
13358 /* Predicates. */
13359 d = bdesc_paired_preds;
13360 for (i = 0; i < ARRAY_SIZE (bdesc_paired_preds); ++i, d++)
13362 tree type;
13364 if (TARGET_DEBUG_BUILTIN)
13365 fprintf (stderr, "paired pred #%d, insn = %s [%d], mode = %s\n",
13366 (int)i, get_insn_name (d->icode), (int)d->icode,
13367 GET_MODE_NAME (insn_data[d->icode].operand[1].mode));
13369 switch (insn_data[d->icode].operand[1].mode)
13371 case V2SFmode:
13372 type = int_ftype_int_v2sf_v2sf;
13373 break;
13374 default:
13375 gcc_unreachable ();
13378 def_builtin (d->name, type, d->code);
13382 static void
13383 altivec_init_builtins (void)
13385 const struct builtin_description *d;
13386 size_t i;
13387 tree ftype;
13388 tree decl;
13390 tree pvoid_type_node = build_pointer_type (void_type_node);
13392 tree pcvoid_type_node
13393 = build_pointer_type (build_qualified_type (void_type_node,
13394 TYPE_QUAL_CONST));
13396 tree int_ftype_opaque
13397 = build_function_type_list (integer_type_node,
13398 opaque_V4SI_type_node, NULL_TREE);
13399 tree opaque_ftype_opaque
13400 = build_function_type_list (integer_type_node, NULL_TREE);
13401 tree opaque_ftype_opaque_int
13402 = build_function_type_list (opaque_V4SI_type_node,
13403 opaque_V4SI_type_node, integer_type_node, NULL_TREE);
13404 tree opaque_ftype_opaque_opaque_int
13405 = build_function_type_list (opaque_V4SI_type_node,
13406 opaque_V4SI_type_node, opaque_V4SI_type_node,
13407 integer_type_node, NULL_TREE);
13408 tree int_ftype_int_opaque_opaque
13409 = build_function_type_list (integer_type_node,
13410 integer_type_node, opaque_V4SI_type_node,
13411 opaque_V4SI_type_node, NULL_TREE);
13412 tree int_ftype_int_v4si_v4si
13413 = build_function_type_list (integer_type_node,
13414 integer_type_node, V4SI_type_node,
13415 V4SI_type_node, NULL_TREE);
13416 tree int_ftype_int_v2di_v2di
13417 = build_function_type_list (integer_type_node,
13418 integer_type_node, V2DI_type_node,
13419 V2DI_type_node, NULL_TREE);
13420 tree void_ftype_v4si
13421 = build_function_type_list (void_type_node, V4SI_type_node, NULL_TREE);
13422 tree v8hi_ftype_void
13423 = build_function_type_list (V8HI_type_node, NULL_TREE);
13424 tree void_ftype_void
13425 = build_function_type_list (void_type_node, NULL_TREE);
13426 tree void_ftype_int
13427 = build_function_type_list (void_type_node, integer_type_node, NULL_TREE);
13429 tree opaque_ftype_long_pcvoid
13430 = build_function_type_list (opaque_V4SI_type_node,
13431 long_integer_type_node, pcvoid_type_node,
13432 NULL_TREE);
13433 tree v16qi_ftype_long_pcvoid
13434 = build_function_type_list (V16QI_type_node,
13435 long_integer_type_node, pcvoid_type_node,
13436 NULL_TREE);
13437 tree v8hi_ftype_long_pcvoid
13438 = build_function_type_list (V8HI_type_node,
13439 long_integer_type_node, pcvoid_type_node,
13440 NULL_TREE);
13441 tree v4si_ftype_long_pcvoid
13442 = build_function_type_list (V4SI_type_node,
13443 long_integer_type_node, pcvoid_type_node,
13444 NULL_TREE);
13445 tree v4sf_ftype_long_pcvoid
13446 = build_function_type_list (V4SF_type_node,
13447 long_integer_type_node, pcvoid_type_node,
13448 NULL_TREE);
13449 tree v2df_ftype_long_pcvoid
13450 = build_function_type_list (V2DF_type_node,
13451 long_integer_type_node, pcvoid_type_node,
13452 NULL_TREE);
13453 tree v2di_ftype_long_pcvoid
13454 = build_function_type_list (V2DI_type_node,
13455 long_integer_type_node, pcvoid_type_node,
13456 NULL_TREE);
13458 tree void_ftype_opaque_long_pvoid
13459 = build_function_type_list (void_type_node,
13460 opaque_V4SI_type_node, long_integer_type_node,
13461 pvoid_type_node, NULL_TREE);
13462 tree void_ftype_v4si_long_pvoid
13463 = build_function_type_list (void_type_node,
13464 V4SI_type_node, long_integer_type_node,
13465 pvoid_type_node, NULL_TREE);
13466 tree void_ftype_v16qi_long_pvoid
13467 = build_function_type_list (void_type_node,
13468 V16QI_type_node, long_integer_type_node,
13469 pvoid_type_node, NULL_TREE);
13470 tree void_ftype_v8hi_long_pvoid
13471 = build_function_type_list (void_type_node,
13472 V8HI_type_node, long_integer_type_node,
13473 pvoid_type_node, NULL_TREE);
13474 tree void_ftype_v4sf_long_pvoid
13475 = build_function_type_list (void_type_node,
13476 V4SF_type_node, long_integer_type_node,
13477 pvoid_type_node, NULL_TREE);
13478 tree void_ftype_v2df_long_pvoid
13479 = build_function_type_list (void_type_node,
13480 V2DF_type_node, long_integer_type_node,
13481 pvoid_type_node, NULL_TREE);
13482 tree void_ftype_v2di_long_pvoid
13483 = build_function_type_list (void_type_node,
13484 V2DI_type_node, long_integer_type_node,
13485 pvoid_type_node, NULL_TREE);
13486 tree int_ftype_int_v8hi_v8hi
13487 = build_function_type_list (integer_type_node,
13488 integer_type_node, V8HI_type_node,
13489 V8HI_type_node, NULL_TREE);
13490 tree int_ftype_int_v16qi_v16qi
13491 = build_function_type_list (integer_type_node,
13492 integer_type_node, V16QI_type_node,
13493 V16QI_type_node, NULL_TREE);
13494 tree int_ftype_int_v4sf_v4sf
13495 = build_function_type_list (integer_type_node,
13496 integer_type_node, V4SF_type_node,
13497 V4SF_type_node, NULL_TREE);
13498 tree int_ftype_int_v2df_v2df
13499 = build_function_type_list (integer_type_node,
13500 integer_type_node, V2DF_type_node,
13501 V2DF_type_node, NULL_TREE);
13502 tree v2di_ftype_v2di
13503 = build_function_type_list (V2DI_type_node, V2DI_type_node, NULL_TREE);
13504 tree v4si_ftype_v4si
13505 = build_function_type_list (V4SI_type_node, V4SI_type_node, NULL_TREE);
13506 tree v8hi_ftype_v8hi
13507 = build_function_type_list (V8HI_type_node, V8HI_type_node, NULL_TREE);
13508 tree v16qi_ftype_v16qi
13509 = build_function_type_list (V16QI_type_node, V16QI_type_node, NULL_TREE);
13510 tree v4sf_ftype_v4sf
13511 = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
13512 tree v2df_ftype_v2df
13513 = build_function_type_list (V2DF_type_node, V2DF_type_node, NULL_TREE);
13514 tree void_ftype_pcvoid_int_int
13515 = build_function_type_list (void_type_node,
13516 pcvoid_type_node, integer_type_node,
13517 integer_type_node, NULL_TREE);
13519 def_builtin ("__builtin_altivec_mtvscr", void_ftype_v4si, ALTIVEC_BUILTIN_MTVSCR);
13520 def_builtin ("__builtin_altivec_mfvscr", v8hi_ftype_void, ALTIVEC_BUILTIN_MFVSCR);
13521 def_builtin ("__builtin_altivec_dssall", void_ftype_void, ALTIVEC_BUILTIN_DSSALL);
13522 def_builtin ("__builtin_altivec_dss", void_ftype_int, ALTIVEC_BUILTIN_DSS);
13523 def_builtin ("__builtin_altivec_lvsl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSL);
13524 def_builtin ("__builtin_altivec_lvsr", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSR);
13525 def_builtin ("__builtin_altivec_lvebx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEBX);
13526 def_builtin ("__builtin_altivec_lvehx", v8hi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEHX);
13527 def_builtin ("__builtin_altivec_lvewx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEWX);
13528 def_builtin ("__builtin_altivec_lvxl", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVXL);
13529 def_builtin ("__builtin_altivec_lvx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVX);
13530 def_builtin ("__builtin_altivec_stvx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVX);
13531 def_builtin ("__builtin_altivec_stvewx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVEWX);
13532 def_builtin ("__builtin_altivec_stvxl", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVXL);
13533 def_builtin ("__builtin_altivec_stvebx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVEBX);
13534 def_builtin ("__builtin_altivec_stvehx", void_ftype_v8hi_long_pvoid, ALTIVEC_BUILTIN_STVEHX);
13535 def_builtin ("__builtin_vec_ld", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LD);
13536 def_builtin ("__builtin_vec_lde", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LDE);
13537 def_builtin ("__builtin_vec_ldl", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LDL);
13538 def_builtin ("__builtin_vec_lvsl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVSL);
13539 def_builtin ("__builtin_vec_lvsr", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVSR);
13540 def_builtin ("__builtin_vec_lvebx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEBX);
13541 def_builtin ("__builtin_vec_lvehx", v8hi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEHX);
13542 def_builtin ("__builtin_vec_lvewx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEWX);
13543 def_builtin ("__builtin_vec_st", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_ST);
13544 def_builtin ("__builtin_vec_ste", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STE);
13545 def_builtin ("__builtin_vec_stl", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STL);
13546 def_builtin ("__builtin_vec_stvewx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEWX);
13547 def_builtin ("__builtin_vec_stvebx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEBX);
13548 def_builtin ("__builtin_vec_stvehx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEHX);
13550 def_builtin ("__builtin_vsx_lxvd2x_v2df", v2df_ftype_long_pcvoid,
13551 VSX_BUILTIN_LXVD2X_V2DF);
13552 def_builtin ("__builtin_vsx_lxvd2x_v2di", v2di_ftype_long_pcvoid,
13553 VSX_BUILTIN_LXVD2X_V2DI);
13554 def_builtin ("__builtin_vsx_lxvw4x_v4sf", v4sf_ftype_long_pcvoid,
13555 VSX_BUILTIN_LXVW4X_V4SF);
13556 def_builtin ("__builtin_vsx_lxvw4x_v4si", v4si_ftype_long_pcvoid,
13557 VSX_BUILTIN_LXVW4X_V4SI);
13558 def_builtin ("__builtin_vsx_lxvw4x_v8hi", v8hi_ftype_long_pcvoid,
13559 VSX_BUILTIN_LXVW4X_V8HI);
13560 def_builtin ("__builtin_vsx_lxvw4x_v16qi", v16qi_ftype_long_pcvoid,
13561 VSX_BUILTIN_LXVW4X_V16QI);
13562 def_builtin ("__builtin_vsx_stxvd2x_v2df", void_ftype_v2df_long_pvoid,
13563 VSX_BUILTIN_STXVD2X_V2DF);
13564 def_builtin ("__builtin_vsx_stxvd2x_v2di", void_ftype_v2di_long_pvoid,
13565 VSX_BUILTIN_STXVD2X_V2DI);
13566 def_builtin ("__builtin_vsx_stxvw4x_v4sf", void_ftype_v4sf_long_pvoid,
13567 VSX_BUILTIN_STXVW4X_V4SF);
13568 def_builtin ("__builtin_vsx_stxvw4x_v4si", void_ftype_v4si_long_pvoid,
13569 VSX_BUILTIN_STXVW4X_V4SI);
13570 def_builtin ("__builtin_vsx_stxvw4x_v8hi", void_ftype_v8hi_long_pvoid,
13571 VSX_BUILTIN_STXVW4X_V8HI);
13572 def_builtin ("__builtin_vsx_stxvw4x_v16qi", void_ftype_v16qi_long_pvoid,
13573 VSX_BUILTIN_STXVW4X_V16QI);
13574 def_builtin ("__builtin_vec_vsx_ld", opaque_ftype_long_pcvoid,
13575 VSX_BUILTIN_VEC_LD);
13576 def_builtin ("__builtin_vec_vsx_st", void_ftype_opaque_long_pvoid,
13577 VSX_BUILTIN_VEC_ST);
13579 def_builtin ("__builtin_vec_step", int_ftype_opaque, ALTIVEC_BUILTIN_VEC_STEP);
13580 def_builtin ("__builtin_vec_splats", opaque_ftype_opaque, ALTIVEC_BUILTIN_VEC_SPLATS);
13581 def_builtin ("__builtin_vec_promote", opaque_ftype_opaque, ALTIVEC_BUILTIN_VEC_PROMOTE);
13583 def_builtin ("__builtin_vec_sld", opaque_ftype_opaque_opaque_int, ALTIVEC_BUILTIN_VEC_SLD);
13584 def_builtin ("__builtin_vec_splat", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_SPLAT);
13585 def_builtin ("__builtin_vec_extract", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_EXTRACT);
13586 def_builtin ("__builtin_vec_insert", opaque_ftype_opaque_opaque_int, ALTIVEC_BUILTIN_VEC_INSERT);
13587 def_builtin ("__builtin_vec_vspltw", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTW);
13588 def_builtin ("__builtin_vec_vsplth", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTH);
13589 def_builtin ("__builtin_vec_vspltb", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTB);
13590 def_builtin ("__builtin_vec_ctf", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTF);
13591 def_builtin ("__builtin_vec_vcfsx", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VCFSX);
13592 def_builtin ("__builtin_vec_vcfux", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VCFUX);
13593 def_builtin ("__builtin_vec_cts", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTS);
13594 def_builtin ("__builtin_vec_ctu", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTU);
13596 /* Cell builtins. */
13597 def_builtin ("__builtin_altivec_lvlx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVLX);
13598 def_builtin ("__builtin_altivec_lvlxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVLXL);
13599 def_builtin ("__builtin_altivec_lvrx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVRX);
13600 def_builtin ("__builtin_altivec_lvrxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVRXL);
13602 def_builtin ("__builtin_vec_lvlx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVLX);
13603 def_builtin ("__builtin_vec_lvlxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVLXL);
13604 def_builtin ("__builtin_vec_lvrx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVRX);
13605 def_builtin ("__builtin_vec_lvrxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVRXL);
13607 def_builtin ("__builtin_altivec_stvlx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVLX);
13608 def_builtin ("__builtin_altivec_stvlxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVLXL);
13609 def_builtin ("__builtin_altivec_stvrx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVRX);
13610 def_builtin ("__builtin_altivec_stvrxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVRXL);
13612 def_builtin ("__builtin_vec_stvlx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVLX);
13613 def_builtin ("__builtin_vec_stvlxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVLXL);
13614 def_builtin ("__builtin_vec_stvrx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVRX);
13615 def_builtin ("__builtin_vec_stvrxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVRXL);
13617 /* Add the DST variants. */
13618 d = bdesc_dst;
13619 for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
13620 def_builtin (d->name, void_ftype_pcvoid_int_int, d->code);
13622 /* Initialize the predicates. */
13623 d = bdesc_altivec_preds;
13624 for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, d++)
13626 enum machine_mode mode1;
13627 tree type;
13629 if (rs6000_overloaded_builtin_p (d->code))
13630 mode1 = VOIDmode;
13631 else
13632 mode1 = insn_data[d->icode].operand[1].mode;
13634 switch (mode1)
13636 case VOIDmode:
13637 type = int_ftype_int_opaque_opaque;
13638 break;
13639 case V2DImode:
13640 type = int_ftype_int_v2di_v2di;
13641 break;
13642 case V4SImode:
13643 type = int_ftype_int_v4si_v4si;
13644 break;
13645 case V8HImode:
13646 type = int_ftype_int_v8hi_v8hi;
13647 break;
13648 case V16QImode:
13649 type = int_ftype_int_v16qi_v16qi;
13650 break;
13651 case V4SFmode:
13652 type = int_ftype_int_v4sf_v4sf;
13653 break;
13654 case V2DFmode:
13655 type = int_ftype_int_v2df_v2df;
13656 break;
13657 default:
13658 gcc_unreachable ();
13661 def_builtin (d->name, type, d->code);
13664 /* Initialize the abs* operators. */
13665 d = bdesc_abs;
13666 for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
13668 enum machine_mode mode0;
13669 tree type;
13671 mode0 = insn_data[d->icode].operand[0].mode;
13673 switch (mode0)
13675 case V2DImode:
13676 type = v2di_ftype_v2di;
13677 break;
13678 case V4SImode:
13679 type = v4si_ftype_v4si;
13680 break;
13681 case V8HImode:
13682 type = v8hi_ftype_v8hi;
13683 break;
13684 case V16QImode:
13685 type = v16qi_ftype_v16qi;
13686 break;
13687 case V4SFmode:
13688 type = v4sf_ftype_v4sf;
13689 break;
13690 case V2DFmode:
13691 type = v2df_ftype_v2df;
13692 break;
13693 default:
13694 gcc_unreachable ();
13697 def_builtin (d->name, type, d->code);
13700 /* Initialize target builtin that implements
13701 targetm.vectorize.builtin_mask_for_load. */
13703 decl = add_builtin_function ("__builtin_altivec_mask_for_load",
13704 v16qi_ftype_long_pcvoid,
13705 ALTIVEC_BUILTIN_MASK_FOR_LOAD,
13706 BUILT_IN_MD, NULL, NULL_TREE);
13707 TREE_READONLY (decl) = 1;
13708 /* Record the decl. Will be used by rs6000_builtin_mask_for_load. */
13709 altivec_builtin_mask_for_load = decl;
13711 /* Access to the vec_init patterns. */
13712 ftype = build_function_type_list (V4SI_type_node, integer_type_node,
13713 integer_type_node, integer_type_node,
13714 integer_type_node, NULL_TREE);
13715 def_builtin ("__builtin_vec_init_v4si", ftype, ALTIVEC_BUILTIN_VEC_INIT_V4SI);
13717 ftype = build_function_type_list (V8HI_type_node, short_integer_type_node,
13718 short_integer_type_node,
13719 short_integer_type_node,
13720 short_integer_type_node,
13721 short_integer_type_node,
13722 short_integer_type_node,
13723 short_integer_type_node,
13724 short_integer_type_node, NULL_TREE);
13725 def_builtin ("__builtin_vec_init_v8hi", ftype, ALTIVEC_BUILTIN_VEC_INIT_V8HI);
13727 ftype = build_function_type_list (V16QI_type_node, char_type_node,
13728 char_type_node, char_type_node,
13729 char_type_node, char_type_node,
13730 char_type_node, char_type_node,
13731 char_type_node, char_type_node,
13732 char_type_node, char_type_node,
13733 char_type_node, char_type_node,
13734 char_type_node, char_type_node,
13735 char_type_node, NULL_TREE);
13736 def_builtin ("__builtin_vec_init_v16qi", ftype,
13737 ALTIVEC_BUILTIN_VEC_INIT_V16QI);
13739 ftype = build_function_type_list (V4SF_type_node, float_type_node,
13740 float_type_node, float_type_node,
13741 float_type_node, NULL_TREE);
13742 def_builtin ("__builtin_vec_init_v4sf", ftype, ALTIVEC_BUILTIN_VEC_INIT_V4SF);
13744 /* VSX builtins. */
13745 ftype = build_function_type_list (V2DF_type_node, double_type_node,
13746 double_type_node, NULL_TREE);
13747 def_builtin ("__builtin_vec_init_v2df", ftype, VSX_BUILTIN_VEC_INIT_V2DF);
13749 ftype = build_function_type_list (V2DI_type_node, intDI_type_node,
13750 intDI_type_node, NULL_TREE);
13751 def_builtin ("__builtin_vec_init_v2di", ftype, VSX_BUILTIN_VEC_INIT_V2DI);
13753 /* Access to the vec_set patterns. */
13754 ftype = build_function_type_list (V4SI_type_node, V4SI_type_node,
13755 intSI_type_node,
13756 integer_type_node, NULL_TREE);
13757 def_builtin ("__builtin_vec_set_v4si", ftype, ALTIVEC_BUILTIN_VEC_SET_V4SI);
13759 ftype = build_function_type_list (V8HI_type_node, V8HI_type_node,
13760 intHI_type_node,
13761 integer_type_node, NULL_TREE);
13762 def_builtin ("__builtin_vec_set_v8hi", ftype, ALTIVEC_BUILTIN_VEC_SET_V8HI);
13764 ftype = build_function_type_list (V16QI_type_node, V16QI_type_node,
13765 intQI_type_node,
13766 integer_type_node, NULL_TREE);
13767 def_builtin ("__builtin_vec_set_v16qi", ftype, ALTIVEC_BUILTIN_VEC_SET_V16QI);
13769 ftype = build_function_type_list (V4SF_type_node, V4SF_type_node,
13770 float_type_node,
13771 integer_type_node, NULL_TREE);
13772 def_builtin ("__builtin_vec_set_v4sf", ftype, ALTIVEC_BUILTIN_VEC_SET_V4SF);
13774 ftype = build_function_type_list (V2DF_type_node, V2DF_type_node,
13775 double_type_node,
13776 integer_type_node, NULL_TREE);
13777 def_builtin ("__builtin_vec_set_v2df", ftype, VSX_BUILTIN_VEC_SET_V2DF);
13779 ftype = build_function_type_list (V2DI_type_node, V2DI_type_node,
13780 intDI_type_node,
13781 integer_type_node, NULL_TREE);
13782 def_builtin ("__builtin_vec_set_v2di", ftype, VSX_BUILTIN_VEC_SET_V2DI);
13784 /* Access to the vec_extract patterns. */
13785 ftype = build_function_type_list (intSI_type_node, V4SI_type_node,
13786 integer_type_node, NULL_TREE);
13787 def_builtin ("__builtin_vec_ext_v4si", ftype, ALTIVEC_BUILTIN_VEC_EXT_V4SI);
13789 ftype = build_function_type_list (intHI_type_node, V8HI_type_node,
13790 integer_type_node, NULL_TREE);
13791 def_builtin ("__builtin_vec_ext_v8hi", ftype, ALTIVEC_BUILTIN_VEC_EXT_V8HI);
13793 ftype = build_function_type_list (intQI_type_node, V16QI_type_node,
13794 integer_type_node, NULL_TREE);
13795 def_builtin ("__builtin_vec_ext_v16qi", ftype, ALTIVEC_BUILTIN_VEC_EXT_V16QI);
13797 ftype = build_function_type_list (float_type_node, V4SF_type_node,
13798 integer_type_node, NULL_TREE);
13799 def_builtin ("__builtin_vec_ext_v4sf", ftype, ALTIVEC_BUILTIN_VEC_EXT_V4SF);
13801 ftype = build_function_type_list (double_type_node, V2DF_type_node,
13802 integer_type_node, NULL_TREE);
13803 def_builtin ("__builtin_vec_ext_v2df", ftype, VSX_BUILTIN_VEC_EXT_V2DF);
13805 ftype = build_function_type_list (intDI_type_node, V2DI_type_node,
13806 integer_type_node, NULL_TREE);
13807 def_builtin ("__builtin_vec_ext_v2di", ftype, VSX_BUILTIN_VEC_EXT_V2DI);
13810 static void
13811 htm_init_builtins (void)
13813 HOST_WIDE_INT builtin_mask = rs6000_builtin_mask;
13814 const struct builtin_description *d;
13815 size_t i;
13817 d = bdesc_htm;
13818 for (i = 0; i < ARRAY_SIZE (bdesc_htm); i++, d++)
13820 tree op[MAX_HTM_OPERANDS], type;
13821 HOST_WIDE_INT mask = d->mask;
13822 unsigned attr = rs6000_builtin_info[d->code].attr;
13823 bool void_func = (attr & RS6000_BTC_VOID);
13824 int attr_args = (attr & RS6000_BTC_TYPE_MASK);
13825 int nopnds = 0;
13826 tree argtype = (attr & RS6000_BTC_SPR) ? long_unsigned_type_node
13827 : unsigned_type_node;
13829 if ((mask & builtin_mask) != mask)
13831 if (TARGET_DEBUG_BUILTIN)
13832 fprintf (stderr, "htm_builtin, skip binary %s\n", d->name);
13833 continue;
13836 if (d->name == 0)
13838 if (TARGET_DEBUG_BUILTIN)
13839 fprintf (stderr, "htm_builtin, bdesc_htm[%ld] no name\n",
13840 (long unsigned) i);
13841 continue;
13844 op[nopnds++] = (void_func) ? void_type_node : argtype;
13846 if (attr_args == RS6000_BTC_UNARY)
13847 op[nopnds++] = argtype;
13848 else if (attr_args == RS6000_BTC_BINARY)
13850 op[nopnds++] = argtype;
13851 op[nopnds++] = argtype;
13853 else if (attr_args == RS6000_BTC_TERNARY)
13855 op[nopnds++] = argtype;
13856 op[nopnds++] = argtype;
13857 op[nopnds++] = argtype;
13860 switch (nopnds)
13862 case 1:
13863 type = build_function_type_list (op[0], NULL_TREE);
13864 break;
13865 case 2:
13866 type = build_function_type_list (op[0], op[1], NULL_TREE);
13867 break;
13868 case 3:
13869 type = build_function_type_list (op[0], op[1], op[2], NULL_TREE);
13870 break;
13871 case 4:
13872 type = build_function_type_list (op[0], op[1], op[2], op[3],
13873 NULL_TREE);
13874 break;
13875 default:
13876 gcc_unreachable ();
13879 def_builtin (d->name, type, d->code);
13883 /* Hash function for builtin functions with up to 3 arguments and a return
13884 type. */
13885 static unsigned
13886 builtin_hash_function (const void *hash_entry)
13888 unsigned ret = 0;
13889 int i;
13890 const struct builtin_hash_struct *bh =
13891 (const struct builtin_hash_struct *) hash_entry;
13893 for (i = 0; i < 4; i++)
13895 ret = (ret * (unsigned)MAX_MACHINE_MODE) + ((unsigned)bh->mode[i]);
13896 ret = (ret * 2) + bh->uns_p[i];
13899 return ret;
13902 /* Compare builtin hash entries H1 and H2 for equivalence. */
13903 static int
13904 builtin_hash_eq (const void *h1, const void *h2)
13906 const struct builtin_hash_struct *p1 = (const struct builtin_hash_struct *) h1;
13907 const struct builtin_hash_struct *p2 = (const struct builtin_hash_struct *) h2;
13909 return ((p1->mode[0] == p2->mode[0])
13910 && (p1->mode[1] == p2->mode[1])
13911 && (p1->mode[2] == p2->mode[2])
13912 && (p1->mode[3] == p2->mode[3])
13913 && (p1->uns_p[0] == p2->uns_p[0])
13914 && (p1->uns_p[1] == p2->uns_p[1])
13915 && (p1->uns_p[2] == p2->uns_p[2])
13916 && (p1->uns_p[3] == p2->uns_p[3]));
13919 /* Map types for builtin functions with an explicit return type and up to 3
13920 arguments. Functions with fewer than 3 arguments use VOIDmode as the type
13921 of the argument. */
13922 static tree
13923 builtin_function_type (enum machine_mode mode_ret, enum machine_mode mode_arg0,
13924 enum machine_mode mode_arg1, enum machine_mode mode_arg2,
13925 enum rs6000_builtins builtin, const char *name)
13927 struct builtin_hash_struct h;
13928 struct builtin_hash_struct *h2;
13929 void **found;
13930 int num_args = 3;
13931 int i;
13932 tree ret_type = NULL_TREE;
13933 tree arg_type[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
13935 /* Create builtin_hash_table. */
13936 if (builtin_hash_table == NULL)
13937 builtin_hash_table = htab_create_ggc (1500, builtin_hash_function,
13938 builtin_hash_eq, NULL);
13940 h.type = NULL_TREE;
13941 h.mode[0] = mode_ret;
13942 h.mode[1] = mode_arg0;
13943 h.mode[2] = mode_arg1;
13944 h.mode[3] = mode_arg2;
13945 h.uns_p[0] = 0;
13946 h.uns_p[1] = 0;
13947 h.uns_p[2] = 0;
13948 h.uns_p[3] = 0;
13950 /* If the builtin is a type that produces unsigned results or takes unsigned
13951 arguments, and it is returned as a decl for the vectorizer (such as
13952 widening multiplies, permute), make sure the arguments and return value
13953 are type correct. */
13954 switch (builtin)
13956 /* unsigned 1 argument functions. */
13957 case CRYPTO_BUILTIN_VSBOX:
13958 case P8V_BUILTIN_VGBBD:
13959 h.uns_p[0] = 1;
13960 h.uns_p[1] = 1;
13961 break;
13963 /* unsigned 2 argument functions. */
13964 case ALTIVEC_BUILTIN_VMULEUB_UNS:
13965 case ALTIVEC_BUILTIN_VMULEUH_UNS:
13966 case ALTIVEC_BUILTIN_VMULOUB_UNS:
13967 case ALTIVEC_BUILTIN_VMULOUH_UNS:
13968 case CRYPTO_BUILTIN_VCIPHER:
13969 case CRYPTO_BUILTIN_VCIPHERLAST:
13970 case CRYPTO_BUILTIN_VNCIPHER:
13971 case CRYPTO_BUILTIN_VNCIPHERLAST:
13972 case CRYPTO_BUILTIN_VPMSUMB:
13973 case CRYPTO_BUILTIN_VPMSUMH:
13974 case CRYPTO_BUILTIN_VPMSUMW:
13975 case CRYPTO_BUILTIN_VPMSUMD:
13976 case CRYPTO_BUILTIN_VPMSUM:
13977 h.uns_p[0] = 1;
13978 h.uns_p[1] = 1;
13979 h.uns_p[2] = 1;
13980 break;
13982 /* unsigned 3 argument functions. */
13983 case ALTIVEC_BUILTIN_VPERM_16QI_UNS:
13984 case ALTIVEC_BUILTIN_VPERM_8HI_UNS:
13985 case ALTIVEC_BUILTIN_VPERM_4SI_UNS:
13986 case ALTIVEC_BUILTIN_VPERM_2DI_UNS:
13987 case ALTIVEC_BUILTIN_VSEL_16QI_UNS:
13988 case ALTIVEC_BUILTIN_VSEL_8HI_UNS:
13989 case ALTIVEC_BUILTIN_VSEL_4SI_UNS:
13990 case ALTIVEC_BUILTIN_VSEL_2DI_UNS:
13991 case VSX_BUILTIN_VPERM_16QI_UNS:
13992 case VSX_BUILTIN_VPERM_8HI_UNS:
13993 case VSX_BUILTIN_VPERM_4SI_UNS:
13994 case VSX_BUILTIN_VPERM_2DI_UNS:
13995 case VSX_BUILTIN_XXSEL_16QI_UNS:
13996 case VSX_BUILTIN_XXSEL_8HI_UNS:
13997 case VSX_BUILTIN_XXSEL_4SI_UNS:
13998 case VSX_BUILTIN_XXSEL_2DI_UNS:
13999 case CRYPTO_BUILTIN_VPERMXOR:
14000 case CRYPTO_BUILTIN_VPERMXOR_V2DI:
14001 case CRYPTO_BUILTIN_VPERMXOR_V4SI:
14002 case CRYPTO_BUILTIN_VPERMXOR_V8HI:
14003 case CRYPTO_BUILTIN_VPERMXOR_V16QI:
14004 case CRYPTO_BUILTIN_VSHASIGMAW:
14005 case CRYPTO_BUILTIN_VSHASIGMAD:
14006 case CRYPTO_BUILTIN_VSHASIGMA:
14007 h.uns_p[0] = 1;
14008 h.uns_p[1] = 1;
14009 h.uns_p[2] = 1;
14010 h.uns_p[3] = 1;
14011 break;
14013 /* signed permute functions with unsigned char mask. */
14014 case ALTIVEC_BUILTIN_VPERM_16QI:
14015 case ALTIVEC_BUILTIN_VPERM_8HI:
14016 case ALTIVEC_BUILTIN_VPERM_4SI:
14017 case ALTIVEC_BUILTIN_VPERM_4SF:
14018 case ALTIVEC_BUILTIN_VPERM_2DI:
14019 case ALTIVEC_BUILTIN_VPERM_2DF:
14020 case VSX_BUILTIN_VPERM_16QI:
14021 case VSX_BUILTIN_VPERM_8HI:
14022 case VSX_BUILTIN_VPERM_4SI:
14023 case VSX_BUILTIN_VPERM_4SF:
14024 case VSX_BUILTIN_VPERM_2DI:
14025 case VSX_BUILTIN_VPERM_2DF:
14026 h.uns_p[3] = 1;
14027 break;
14029 /* unsigned args, signed return. */
14030 case VSX_BUILTIN_XVCVUXDDP_UNS:
14031 case ALTIVEC_BUILTIN_UNSFLOAT_V4SI_V4SF:
14032 h.uns_p[1] = 1;
14033 break;
14035 /* signed args, unsigned return. */
14036 case VSX_BUILTIN_XVCVDPUXDS_UNS:
14037 case ALTIVEC_BUILTIN_FIXUNS_V4SF_V4SI:
14038 h.uns_p[0] = 1;
14039 break;
14041 default:
14042 break;
14045 /* Figure out how many args are present. */
14046 while (num_args > 0 && h.mode[num_args] == VOIDmode)
14047 num_args--;
14049 if (num_args == 0)
14050 fatal_error ("internal error: builtin function %s had no type", name);
14052 ret_type = builtin_mode_to_type[h.mode[0]][h.uns_p[0]];
14053 if (!ret_type && h.uns_p[0])
14054 ret_type = builtin_mode_to_type[h.mode[0]][0];
14056 if (!ret_type)
14057 fatal_error ("internal error: builtin function %s had an unexpected "
14058 "return type %s", name, GET_MODE_NAME (h.mode[0]));
14060 for (i = 0; i < (int) ARRAY_SIZE (arg_type); i++)
14061 arg_type[i] = NULL_TREE;
14063 for (i = 0; i < num_args; i++)
14065 int m = (int) h.mode[i+1];
14066 int uns_p = h.uns_p[i+1];
14068 arg_type[i] = builtin_mode_to_type[m][uns_p];
14069 if (!arg_type[i] && uns_p)
14070 arg_type[i] = builtin_mode_to_type[m][0];
14072 if (!arg_type[i])
14073 fatal_error ("internal error: builtin function %s, argument %d "
14074 "had unexpected argument type %s", name, i,
14075 GET_MODE_NAME (m));
14078 found = htab_find_slot (builtin_hash_table, &h, INSERT);
14079 if (*found == NULL)
14081 h2 = ggc_alloc_builtin_hash_struct ();
14082 *h2 = h;
14083 *found = (void *)h2;
14085 h2->type = build_function_type_list (ret_type, arg_type[0], arg_type[1],
14086 arg_type[2], NULL_TREE);
14089 return ((struct builtin_hash_struct *)(*found))->type;
14092 static void
14093 rs6000_common_init_builtins (void)
14095 const struct builtin_description *d;
14096 size_t i;
14098 tree opaque_ftype_opaque = NULL_TREE;
14099 tree opaque_ftype_opaque_opaque = NULL_TREE;
14100 tree opaque_ftype_opaque_opaque_opaque = NULL_TREE;
14101 tree v2si_ftype_qi = NULL_TREE;
14102 tree v2si_ftype_v2si_qi = NULL_TREE;
14103 tree v2si_ftype_int_qi = NULL_TREE;
14104 HOST_WIDE_INT builtin_mask = rs6000_builtin_mask;
14106 if (!TARGET_PAIRED_FLOAT)
14108 builtin_mode_to_type[V2SImode][0] = opaque_V2SI_type_node;
14109 builtin_mode_to_type[V2SFmode][0] = opaque_V2SF_type_node;
14112 /* Paired and SPE builtins are only available if you build a compiler with
14113 the appropriate options, so only create those builtins with the
14114 appropriate compiler option. Create Altivec and VSX builtins on machines
14115 with at least the general purpose extensions (970 and newer) to allow the
14116 use of the target attribute.. */
14118 if (TARGET_EXTRA_BUILTINS)
14119 builtin_mask |= RS6000_BTM_COMMON;
14121 /* Add the ternary operators. */
14122 d = bdesc_3arg;
14123 for (i = 0; i < ARRAY_SIZE (bdesc_3arg); i++, d++)
14125 tree type;
14126 HOST_WIDE_INT mask = d->mask;
14128 if ((mask & builtin_mask) != mask)
14130 if (TARGET_DEBUG_BUILTIN)
14131 fprintf (stderr, "rs6000_builtin, skip ternary %s\n", d->name);
14132 continue;
14135 if (rs6000_overloaded_builtin_p (d->code))
14137 if (! (type = opaque_ftype_opaque_opaque_opaque))
14138 type = opaque_ftype_opaque_opaque_opaque
14139 = build_function_type_list (opaque_V4SI_type_node,
14140 opaque_V4SI_type_node,
14141 opaque_V4SI_type_node,
14142 opaque_V4SI_type_node,
14143 NULL_TREE);
14145 else
14147 enum insn_code icode = d->icode;
14148 if (d->name == 0)
14150 if (TARGET_DEBUG_BUILTIN)
14151 fprintf (stderr, "rs6000_builtin, bdesc_3arg[%ld] no name\n",
14152 (long unsigned)i);
14154 continue;
14157 if (icode == CODE_FOR_nothing)
14159 if (TARGET_DEBUG_BUILTIN)
14160 fprintf (stderr, "rs6000_builtin, skip ternary %s (no code)\n",
14161 d->name);
14163 continue;
14166 type = builtin_function_type (insn_data[icode].operand[0].mode,
14167 insn_data[icode].operand[1].mode,
14168 insn_data[icode].operand[2].mode,
14169 insn_data[icode].operand[3].mode,
14170 d->code, d->name);
14173 def_builtin (d->name, type, d->code);
14176 /* Add the binary operators. */
14177 d = bdesc_2arg;
14178 for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
14180 enum machine_mode mode0, mode1, mode2;
14181 tree type;
14182 HOST_WIDE_INT mask = d->mask;
14184 if ((mask & builtin_mask) != mask)
14186 if (TARGET_DEBUG_BUILTIN)
14187 fprintf (stderr, "rs6000_builtin, skip binary %s\n", d->name);
14188 continue;
14191 if (rs6000_overloaded_builtin_p (d->code))
14193 if (! (type = opaque_ftype_opaque_opaque))
14194 type = opaque_ftype_opaque_opaque
14195 = build_function_type_list (opaque_V4SI_type_node,
14196 opaque_V4SI_type_node,
14197 opaque_V4SI_type_node,
14198 NULL_TREE);
14200 else
14202 enum insn_code icode = d->icode;
14203 if (d->name == 0)
14205 if (TARGET_DEBUG_BUILTIN)
14206 fprintf (stderr, "rs6000_builtin, bdesc_2arg[%ld] no name\n",
14207 (long unsigned)i);
14209 continue;
14212 if (icode == CODE_FOR_nothing)
14214 if (TARGET_DEBUG_BUILTIN)
14215 fprintf (stderr, "rs6000_builtin, skip binary %s (no code)\n",
14216 d->name);
14218 continue;
14221 mode0 = insn_data[icode].operand[0].mode;
14222 mode1 = insn_data[icode].operand[1].mode;
14223 mode2 = insn_data[icode].operand[2].mode;
14225 if (mode0 == V2SImode && mode1 == V2SImode && mode2 == QImode)
14227 if (! (type = v2si_ftype_v2si_qi))
14228 type = v2si_ftype_v2si_qi
14229 = build_function_type_list (opaque_V2SI_type_node,
14230 opaque_V2SI_type_node,
14231 char_type_node,
14232 NULL_TREE);
14235 else if (mode0 == V2SImode && GET_MODE_CLASS (mode1) == MODE_INT
14236 && mode2 == QImode)
14238 if (! (type = v2si_ftype_int_qi))
14239 type = v2si_ftype_int_qi
14240 = build_function_type_list (opaque_V2SI_type_node,
14241 integer_type_node,
14242 char_type_node,
14243 NULL_TREE);
14246 else
14247 type = builtin_function_type (mode0, mode1, mode2, VOIDmode,
14248 d->code, d->name);
14251 def_builtin (d->name, type, d->code);
14254 /* Add the simple unary operators. */
14255 d = bdesc_1arg;
14256 for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
14258 enum machine_mode mode0, mode1;
14259 tree type;
14260 HOST_WIDE_INT mask = d->mask;
14262 if ((mask & builtin_mask) != mask)
14264 if (TARGET_DEBUG_BUILTIN)
14265 fprintf (stderr, "rs6000_builtin, skip unary %s\n", d->name);
14266 continue;
14269 if (rs6000_overloaded_builtin_p (d->code))
14271 if (! (type = opaque_ftype_opaque))
14272 type = opaque_ftype_opaque
14273 = build_function_type_list (opaque_V4SI_type_node,
14274 opaque_V4SI_type_node,
14275 NULL_TREE);
14277 else
14279 enum insn_code icode = d->icode;
14280 if (d->name == 0)
14282 if (TARGET_DEBUG_BUILTIN)
14283 fprintf (stderr, "rs6000_builtin, bdesc_1arg[%ld] no name\n",
14284 (long unsigned)i);
14286 continue;
14289 if (icode == CODE_FOR_nothing)
14291 if (TARGET_DEBUG_BUILTIN)
14292 fprintf (stderr, "rs6000_builtin, skip unary %s (no code)\n",
14293 d->name);
14295 continue;
14298 mode0 = insn_data[icode].operand[0].mode;
14299 mode1 = insn_data[icode].operand[1].mode;
14301 if (mode0 == V2SImode && mode1 == QImode)
14303 if (! (type = v2si_ftype_qi))
14304 type = v2si_ftype_qi
14305 = build_function_type_list (opaque_V2SI_type_node,
14306 char_type_node,
14307 NULL_TREE);
14310 else
14311 type = builtin_function_type (mode0, mode1, VOIDmode, VOIDmode,
14312 d->code, d->name);
14315 def_builtin (d->name, type, d->code);
14319 static void
14320 rs6000_init_libfuncs (void)
14322 if (!TARGET_IEEEQUAD)
14323 /* AIX/Darwin/64-bit Linux quad floating point routines. */
14324 if (!TARGET_XL_COMPAT)
14326 set_optab_libfunc (add_optab, TFmode, "__gcc_qadd");
14327 set_optab_libfunc (sub_optab, TFmode, "__gcc_qsub");
14328 set_optab_libfunc (smul_optab, TFmode, "__gcc_qmul");
14329 set_optab_libfunc (sdiv_optab, TFmode, "__gcc_qdiv");
14331 if (!(TARGET_HARD_FLOAT && (TARGET_FPRS || TARGET_E500_DOUBLE)))
14333 set_optab_libfunc (neg_optab, TFmode, "__gcc_qneg");
14334 set_optab_libfunc (eq_optab, TFmode, "__gcc_qeq");
14335 set_optab_libfunc (ne_optab, TFmode, "__gcc_qne");
14336 set_optab_libfunc (gt_optab, TFmode, "__gcc_qgt");
14337 set_optab_libfunc (ge_optab, TFmode, "__gcc_qge");
14338 set_optab_libfunc (lt_optab, TFmode, "__gcc_qlt");
14339 set_optab_libfunc (le_optab, TFmode, "__gcc_qle");
14341 set_conv_libfunc (sext_optab, TFmode, SFmode, "__gcc_stoq");
14342 set_conv_libfunc (sext_optab, TFmode, DFmode, "__gcc_dtoq");
14343 set_conv_libfunc (trunc_optab, SFmode, TFmode, "__gcc_qtos");
14344 set_conv_libfunc (trunc_optab, DFmode, TFmode, "__gcc_qtod");
14345 set_conv_libfunc (sfix_optab, SImode, TFmode, "__gcc_qtoi");
14346 set_conv_libfunc (ufix_optab, SImode, TFmode, "__gcc_qtou");
14347 set_conv_libfunc (sfloat_optab, TFmode, SImode, "__gcc_itoq");
14348 set_conv_libfunc (ufloat_optab, TFmode, SImode, "__gcc_utoq");
14351 if (!(TARGET_HARD_FLOAT && TARGET_FPRS))
14352 set_optab_libfunc (unord_optab, TFmode, "__gcc_qunord");
14354 else
14356 set_optab_libfunc (add_optab, TFmode, "_xlqadd");
14357 set_optab_libfunc (sub_optab, TFmode, "_xlqsub");
14358 set_optab_libfunc (smul_optab, TFmode, "_xlqmul");
14359 set_optab_libfunc (sdiv_optab, TFmode, "_xlqdiv");
14361 else
14363 /* 32-bit SVR4 quad floating point routines. */
14365 set_optab_libfunc (add_optab, TFmode, "_q_add");
14366 set_optab_libfunc (sub_optab, TFmode, "_q_sub");
14367 set_optab_libfunc (neg_optab, TFmode, "_q_neg");
14368 set_optab_libfunc (smul_optab, TFmode, "_q_mul");
14369 set_optab_libfunc (sdiv_optab, TFmode, "_q_div");
14370 if (TARGET_PPC_GPOPT)
14371 set_optab_libfunc (sqrt_optab, TFmode, "_q_sqrt");
14373 set_optab_libfunc (eq_optab, TFmode, "_q_feq");
14374 set_optab_libfunc (ne_optab, TFmode, "_q_fne");
14375 set_optab_libfunc (gt_optab, TFmode, "_q_fgt");
14376 set_optab_libfunc (ge_optab, TFmode, "_q_fge");
14377 set_optab_libfunc (lt_optab, TFmode, "_q_flt");
14378 set_optab_libfunc (le_optab, TFmode, "_q_fle");
14380 set_conv_libfunc (sext_optab, TFmode, SFmode, "_q_stoq");
14381 set_conv_libfunc (sext_optab, TFmode, DFmode, "_q_dtoq");
14382 set_conv_libfunc (trunc_optab, SFmode, TFmode, "_q_qtos");
14383 set_conv_libfunc (trunc_optab, DFmode, TFmode, "_q_qtod");
14384 set_conv_libfunc (sfix_optab, SImode, TFmode, "_q_qtoi");
14385 set_conv_libfunc (ufix_optab, SImode, TFmode, "_q_qtou");
14386 set_conv_libfunc (sfloat_optab, TFmode, SImode, "_q_itoq");
14387 set_conv_libfunc (ufloat_optab, TFmode, SImode, "_q_utoq");
14392 /* Expand a block clear operation, and return 1 if successful. Return 0
14393 if we should let the compiler generate normal code.
14395 operands[0] is the destination
14396 operands[1] is the length
14397 operands[3] is the alignment */
14400 expand_block_clear (rtx operands[])
14402 rtx orig_dest = operands[0];
14403 rtx bytes_rtx = operands[1];
14404 rtx align_rtx = operands[3];
14405 bool constp = (GET_CODE (bytes_rtx) == CONST_INT);
14406 HOST_WIDE_INT align;
14407 HOST_WIDE_INT bytes;
14408 int offset;
14409 int clear_bytes;
14410 int clear_step;
14412 /* If this is not a fixed size move, just call memcpy */
14413 if (! constp)
14414 return 0;
14416 /* This must be a fixed size alignment */
14417 gcc_assert (GET_CODE (align_rtx) == CONST_INT);
14418 align = INTVAL (align_rtx) * BITS_PER_UNIT;
14420 /* Anything to clear? */
14421 bytes = INTVAL (bytes_rtx);
14422 if (bytes <= 0)
14423 return 1;
14425 /* Use the builtin memset after a point, to avoid huge code bloat.
14426 When optimize_size, avoid any significant code bloat; calling
14427 memset is about 4 instructions, so allow for one instruction to
14428 load zero and three to do clearing. */
14429 if (TARGET_ALTIVEC && align >= 128)
14430 clear_step = 16;
14431 else if (TARGET_POWERPC64 && align >= 32)
14432 clear_step = 8;
14433 else if (TARGET_SPE && align >= 64)
14434 clear_step = 8;
14435 else
14436 clear_step = 4;
14438 if (optimize_size && bytes > 3 * clear_step)
14439 return 0;
14440 if (! optimize_size && bytes > 8 * clear_step)
14441 return 0;
14443 for (offset = 0; bytes > 0; offset += clear_bytes, bytes -= clear_bytes)
14445 enum machine_mode mode = BLKmode;
14446 rtx dest;
14448 if (bytes >= 16 && TARGET_ALTIVEC && align >= 128)
14450 clear_bytes = 16;
14451 mode = V4SImode;
14453 else if (bytes >= 8 && TARGET_SPE && align >= 64)
14455 clear_bytes = 8;
14456 mode = V2SImode;
14458 else if (bytes >= 8 && TARGET_POWERPC64
14459 /* 64-bit loads and stores require word-aligned
14460 displacements. */
14461 && (align >= 64 || (!STRICT_ALIGNMENT && align >= 32)))
14463 clear_bytes = 8;
14464 mode = DImode;
14466 else if (bytes >= 4 && (align >= 32 || !STRICT_ALIGNMENT))
14467 { /* move 4 bytes */
14468 clear_bytes = 4;
14469 mode = SImode;
14471 else if (bytes >= 2 && (align >= 16 || !STRICT_ALIGNMENT))
14472 { /* move 2 bytes */
14473 clear_bytes = 2;
14474 mode = HImode;
14476 else /* move 1 byte at a time */
14478 clear_bytes = 1;
14479 mode = QImode;
14482 dest = adjust_address (orig_dest, mode, offset);
14484 emit_move_insn (dest, CONST0_RTX (mode));
14487 return 1;
14491 /* Expand a block move operation, and return 1 if successful. Return 0
14492 if we should let the compiler generate normal code.
14494 operands[0] is the destination
14495 operands[1] is the source
14496 operands[2] is the length
14497 operands[3] is the alignment */
14499 #define MAX_MOVE_REG 4
14502 expand_block_move (rtx operands[])
14504 rtx orig_dest = operands[0];
14505 rtx orig_src = operands[1];
14506 rtx bytes_rtx = operands[2];
14507 rtx align_rtx = operands[3];
14508 int constp = (GET_CODE (bytes_rtx) == CONST_INT);
14509 int align;
14510 int bytes;
14511 int offset;
14512 int move_bytes;
14513 rtx stores[MAX_MOVE_REG];
14514 int num_reg = 0;
14516 /* If this is not a fixed size move, just call memcpy */
14517 if (! constp)
14518 return 0;
14520 /* This must be a fixed size alignment */
14521 gcc_assert (GET_CODE (align_rtx) == CONST_INT);
14522 align = INTVAL (align_rtx) * BITS_PER_UNIT;
14524 /* Anything to move? */
14525 bytes = INTVAL (bytes_rtx);
14526 if (bytes <= 0)
14527 return 1;
14529 if (bytes > rs6000_block_move_inline_limit)
14530 return 0;
14532 for (offset = 0; bytes > 0; offset += move_bytes, bytes -= move_bytes)
14534 union {
14535 rtx (*movmemsi) (rtx, rtx, rtx, rtx);
14536 rtx (*mov) (rtx, rtx);
14537 } gen_func;
14538 enum machine_mode mode = BLKmode;
14539 rtx src, dest;
14541 /* Altivec first, since it will be faster than a string move
14542 when it applies, and usually not significantly larger. */
14543 if (TARGET_ALTIVEC && bytes >= 16 && align >= 128)
14545 move_bytes = 16;
14546 mode = V4SImode;
14547 gen_func.mov = gen_movv4si;
14549 else if (TARGET_SPE && bytes >= 8 && align >= 64)
14551 move_bytes = 8;
14552 mode = V2SImode;
14553 gen_func.mov = gen_movv2si;
14555 else if (TARGET_STRING
14556 && bytes > 24 /* move up to 32 bytes at a time */
14557 && ! fixed_regs[5]
14558 && ! fixed_regs[6]
14559 && ! fixed_regs[7]
14560 && ! fixed_regs[8]
14561 && ! fixed_regs[9]
14562 && ! fixed_regs[10]
14563 && ! fixed_regs[11]
14564 && ! fixed_regs[12])
14566 move_bytes = (bytes > 32) ? 32 : bytes;
14567 gen_func.movmemsi = gen_movmemsi_8reg;
14569 else if (TARGET_STRING
14570 && bytes > 16 /* move up to 24 bytes at a time */
14571 && ! fixed_regs[5]
14572 && ! fixed_regs[6]
14573 && ! fixed_regs[7]
14574 && ! fixed_regs[8]
14575 && ! fixed_regs[9]
14576 && ! fixed_regs[10])
14578 move_bytes = (bytes > 24) ? 24 : bytes;
14579 gen_func.movmemsi = gen_movmemsi_6reg;
14581 else if (TARGET_STRING
14582 && bytes > 8 /* move up to 16 bytes at a time */
14583 && ! fixed_regs[5]
14584 && ! fixed_regs[6]
14585 && ! fixed_regs[7]
14586 && ! fixed_regs[8])
14588 move_bytes = (bytes > 16) ? 16 : bytes;
14589 gen_func.movmemsi = gen_movmemsi_4reg;
14591 else if (bytes >= 8 && TARGET_POWERPC64
14592 /* 64-bit loads and stores require word-aligned
14593 displacements. */
14594 && (align >= 64 || (!STRICT_ALIGNMENT && align >= 32)))
14596 move_bytes = 8;
14597 mode = DImode;
14598 gen_func.mov = gen_movdi;
14600 else if (TARGET_STRING && bytes > 4 && !TARGET_POWERPC64)
14601 { /* move up to 8 bytes at a time */
14602 move_bytes = (bytes > 8) ? 8 : bytes;
14603 gen_func.movmemsi = gen_movmemsi_2reg;
14605 else if (bytes >= 4 && (align >= 32 || !STRICT_ALIGNMENT))
14606 { /* move 4 bytes */
14607 move_bytes = 4;
14608 mode = SImode;
14609 gen_func.mov = gen_movsi;
14611 else if (bytes >= 2 && (align >= 16 || !STRICT_ALIGNMENT))
14612 { /* move 2 bytes */
14613 move_bytes = 2;
14614 mode = HImode;
14615 gen_func.mov = gen_movhi;
14617 else if (TARGET_STRING && bytes > 1)
14618 { /* move up to 4 bytes at a time */
14619 move_bytes = (bytes > 4) ? 4 : bytes;
14620 gen_func.movmemsi = gen_movmemsi_1reg;
14622 else /* move 1 byte at a time */
14624 move_bytes = 1;
14625 mode = QImode;
14626 gen_func.mov = gen_movqi;
14629 src = adjust_address (orig_src, mode, offset);
14630 dest = adjust_address (orig_dest, mode, offset);
14632 if (mode != BLKmode)
14634 rtx tmp_reg = gen_reg_rtx (mode);
14636 emit_insn ((*gen_func.mov) (tmp_reg, src));
14637 stores[num_reg++] = (*gen_func.mov) (dest, tmp_reg);
14640 if (mode == BLKmode || num_reg >= MAX_MOVE_REG || bytes == move_bytes)
14642 int i;
14643 for (i = 0; i < num_reg; i++)
14644 emit_insn (stores[i]);
14645 num_reg = 0;
14648 if (mode == BLKmode)
14650 /* Move the address into scratch registers. The movmemsi
14651 patterns require zero offset. */
14652 if (!REG_P (XEXP (src, 0)))
14654 rtx src_reg = copy_addr_to_reg (XEXP (src, 0));
14655 src = replace_equiv_address (src, src_reg);
14657 set_mem_size (src, move_bytes);
14659 if (!REG_P (XEXP (dest, 0)))
14661 rtx dest_reg = copy_addr_to_reg (XEXP (dest, 0));
14662 dest = replace_equiv_address (dest, dest_reg);
14664 set_mem_size (dest, move_bytes);
14666 emit_insn ((*gen_func.movmemsi) (dest, src,
14667 GEN_INT (move_bytes & 31),
14668 align_rtx));
14672 return 1;
14676 /* Return a string to perform a load_multiple operation.
14677 operands[0] is the vector.
14678 operands[1] is the source address.
14679 operands[2] is the first destination register. */
14681 const char *
14682 rs6000_output_load_multiple (rtx operands[3])
14684 /* We have to handle the case where the pseudo used to contain the address
14685 is assigned to one of the output registers. */
14686 int i, j;
14687 int words = XVECLEN (operands[0], 0);
14688 rtx xop[10];
14690 if (XVECLEN (operands[0], 0) == 1)
14691 return "lwz %2,0(%1)";
14693 for (i = 0; i < words; i++)
14694 if (refers_to_regno_p (REGNO (operands[2]) + i,
14695 REGNO (operands[2]) + i + 1, operands[1], 0))
14697 if (i == words-1)
14699 xop[0] = GEN_INT (4 * (words-1));
14700 xop[1] = operands[1];
14701 xop[2] = operands[2];
14702 output_asm_insn ("lswi %2,%1,%0\n\tlwz %1,%0(%1)", xop);
14703 return "";
14705 else if (i == 0)
14707 xop[0] = GEN_INT (4 * (words-1));
14708 xop[1] = operands[1];
14709 xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
14710 output_asm_insn ("addi %1,%1,4\n\tlswi %2,%1,%0\n\tlwz %1,-4(%1)", xop);
14711 return "";
14713 else
14715 for (j = 0; j < words; j++)
14716 if (j != i)
14718 xop[0] = GEN_INT (j * 4);
14719 xop[1] = operands[1];
14720 xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + j);
14721 output_asm_insn ("lwz %2,%0(%1)", xop);
14723 xop[0] = GEN_INT (i * 4);
14724 xop[1] = operands[1];
14725 output_asm_insn ("lwz %1,%0(%1)", xop);
14726 return "";
14730 return "lswi %2,%1,%N0";
14734 /* A validation routine: say whether CODE, a condition code, and MODE
14735 match. The other alternatives either don't make sense or should
14736 never be generated. */
14738 void
14739 validate_condition_mode (enum rtx_code code, enum machine_mode mode)
14741 gcc_assert ((GET_RTX_CLASS (code) == RTX_COMPARE
14742 || GET_RTX_CLASS (code) == RTX_COMM_COMPARE)
14743 && GET_MODE_CLASS (mode) == MODE_CC);
14745 /* These don't make sense. */
14746 gcc_assert ((code != GT && code != LT && code != GE && code != LE)
14747 || mode != CCUNSmode);
14749 gcc_assert ((code != GTU && code != LTU && code != GEU && code != LEU)
14750 || mode == CCUNSmode);
14752 gcc_assert (mode == CCFPmode
14753 || (code != ORDERED && code != UNORDERED
14754 && code != UNEQ && code != LTGT
14755 && code != UNGT && code != UNLT
14756 && code != UNGE && code != UNLE));
14758 /* These should never be generated except for
14759 flag_finite_math_only. */
14760 gcc_assert (mode != CCFPmode
14761 || flag_finite_math_only
14762 || (code != LE && code != GE
14763 && code != UNEQ && code != LTGT
14764 && code != UNGT && code != UNLT));
14766 /* These are invalid; the information is not there. */
14767 gcc_assert (mode != CCEQmode || code == EQ || code == NE);
14771 /* Return 1 if ANDOP is a mask that has no bits on that are not in the
14772 mask required to convert the result of a rotate insn into a shift
14773 left insn of SHIFTOP bits. Both are known to be SImode CONST_INT. */
14776 includes_lshift_p (rtx shiftop, rtx andop)
14778 unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
14780 shift_mask <<= INTVAL (shiftop);
14782 return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
14785 /* Similar, but for right shift. */
14788 includes_rshift_p (rtx shiftop, rtx andop)
14790 unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
14792 shift_mask >>= INTVAL (shiftop);
14794 return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
14797 /* Return 1 if ANDOP is a mask suitable for use with an rldic insn
14798 to perform a left shift. It must have exactly SHIFTOP least
14799 significant 0's, then one or more 1's, then zero or more 0's. */
14802 includes_rldic_lshift_p (rtx shiftop, rtx andop)
14804 if (GET_CODE (andop) == CONST_INT)
14806 HOST_WIDE_INT c, lsb, shift_mask;
14808 c = INTVAL (andop);
14809 if (c == 0 || c == ~0)
14810 return 0;
14812 shift_mask = ~0;
14813 shift_mask <<= INTVAL (shiftop);
14815 /* Find the least significant one bit. */
14816 lsb = c & -c;
14818 /* It must coincide with the LSB of the shift mask. */
14819 if (-lsb != shift_mask)
14820 return 0;
14822 /* Invert to look for the next transition (if any). */
14823 c = ~c;
14825 /* Remove the low group of ones (originally low group of zeros). */
14826 c &= -lsb;
14828 /* Again find the lsb, and check we have all 1's above. */
14829 lsb = c & -c;
14830 return c == -lsb;
14832 else
14833 return 0;
14836 /* Return 1 if ANDOP is a mask suitable for use with an rldicr insn
14837 to perform a left shift. It must have SHIFTOP or more least
14838 significant 0's, with the remainder of the word 1's. */
14841 includes_rldicr_lshift_p (rtx shiftop, rtx andop)
14843 if (GET_CODE (andop) == CONST_INT)
14845 HOST_WIDE_INT c, lsb, shift_mask;
14847 shift_mask = ~0;
14848 shift_mask <<= INTVAL (shiftop);
14849 c = INTVAL (andop);
14851 /* Find the least significant one bit. */
14852 lsb = c & -c;
14854 /* It must be covered by the shift mask.
14855 This test also rejects c == 0. */
14856 if ((lsb & shift_mask) == 0)
14857 return 0;
14859 /* Check we have all 1's above the transition, and reject all 1's. */
14860 return c == -lsb && lsb != 1;
14862 else
14863 return 0;
14866 /* Return 1 if operands will generate a valid arguments to rlwimi
14867 instruction for insert with right shift in 64-bit mode. The mask may
14868 not start on the first bit or stop on the last bit because wrap-around
14869 effects of instruction do not correspond to semantics of RTL insn. */
14872 insvdi_rshift_rlwimi_p (rtx sizeop, rtx startop, rtx shiftop)
14874 if (INTVAL (startop) > 32
14875 && INTVAL (startop) < 64
14876 && INTVAL (sizeop) > 1
14877 && INTVAL (sizeop) + INTVAL (startop) < 64
14878 && INTVAL (shiftop) > 0
14879 && INTVAL (sizeop) + INTVAL (shiftop) < 32
14880 && (64 - (INTVAL (shiftop) & 63)) >= INTVAL (sizeop))
14881 return 1;
14883 return 0;
14886 /* Return 1 if REGNO (reg1) == REGNO (reg2) - 1 making them candidates
14887 for lfq and stfq insns iff the registers are hard registers. */
14890 registers_ok_for_quad_peep (rtx reg1, rtx reg2)
14892 /* We might have been passed a SUBREG. */
14893 if (GET_CODE (reg1) != REG || GET_CODE (reg2) != REG)
14894 return 0;
14896 /* We might have been passed non floating point registers. */
14897 if (!FP_REGNO_P (REGNO (reg1))
14898 || !FP_REGNO_P (REGNO (reg2)))
14899 return 0;
14901 return (REGNO (reg1) == REGNO (reg2) - 1);
14904 /* Return 1 if addr1 and addr2 are suitable for lfq or stfq insn.
14905 addr1 and addr2 must be in consecutive memory locations
14906 (addr2 == addr1 + 8). */
14909 mems_ok_for_quad_peep (rtx mem1, rtx mem2)
14911 rtx addr1, addr2;
14912 unsigned int reg1, reg2;
14913 int offset1, offset2;
14915 /* The mems cannot be volatile. */
14916 if (MEM_VOLATILE_P (mem1) || MEM_VOLATILE_P (mem2))
14917 return 0;
14919 addr1 = XEXP (mem1, 0);
14920 addr2 = XEXP (mem2, 0);
14922 /* Extract an offset (if used) from the first addr. */
14923 if (GET_CODE (addr1) == PLUS)
14925 /* If not a REG, return zero. */
14926 if (GET_CODE (XEXP (addr1, 0)) != REG)
14927 return 0;
14928 else
14930 reg1 = REGNO (XEXP (addr1, 0));
14931 /* The offset must be constant! */
14932 if (GET_CODE (XEXP (addr1, 1)) != CONST_INT)
14933 return 0;
14934 offset1 = INTVAL (XEXP (addr1, 1));
14937 else if (GET_CODE (addr1) != REG)
14938 return 0;
14939 else
14941 reg1 = REGNO (addr1);
14942 /* This was a simple (mem (reg)) expression. Offset is 0. */
14943 offset1 = 0;
14946 /* And now for the second addr. */
14947 if (GET_CODE (addr2) == PLUS)
14949 /* If not a REG, return zero. */
14950 if (GET_CODE (XEXP (addr2, 0)) != REG)
14951 return 0;
14952 else
14954 reg2 = REGNO (XEXP (addr2, 0));
14955 /* The offset must be constant. */
14956 if (GET_CODE (XEXP (addr2, 1)) != CONST_INT)
14957 return 0;
14958 offset2 = INTVAL (XEXP (addr2, 1));
14961 else if (GET_CODE (addr2) != REG)
14962 return 0;
14963 else
14965 reg2 = REGNO (addr2);
14966 /* This was a simple (mem (reg)) expression. Offset is 0. */
14967 offset2 = 0;
14970 /* Both of these must have the same base register. */
14971 if (reg1 != reg2)
14972 return 0;
14974 /* The offset for the second addr must be 8 more than the first addr. */
14975 if (offset2 != offset1 + 8)
14976 return 0;
14978 /* All the tests passed. addr1 and addr2 are valid for lfq or stfq
14979 instructions. */
14980 return 1;
14985 rs6000_secondary_memory_needed_rtx (enum machine_mode mode)
14987 static bool eliminated = false;
14988 rtx ret;
14990 if (mode != SDmode || TARGET_NO_SDMODE_STACK)
14991 ret = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
14992 else
14994 rtx mem = cfun->machine->sdmode_stack_slot;
14995 gcc_assert (mem != NULL_RTX);
14997 if (!eliminated)
14999 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
15000 cfun->machine->sdmode_stack_slot = mem;
15001 eliminated = true;
15003 ret = mem;
15006 if (TARGET_DEBUG_ADDR)
15008 fprintf (stderr, "\nrs6000_secondary_memory_needed_rtx, mode %s, rtx:\n",
15009 GET_MODE_NAME (mode));
15010 if (!ret)
15011 fprintf (stderr, "\tNULL_RTX\n");
15012 else
15013 debug_rtx (ret);
15016 return ret;
15019 /* Return the mode to be used for memory when a secondary memory
15020 location is needed. For SDmode values we need to use DDmode, in
15021 all other cases we can use the same mode. */
15022 enum machine_mode
15023 rs6000_secondary_memory_needed_mode (enum machine_mode mode)
15025 if (mode == SDmode)
15026 return DDmode;
15027 return mode;
15030 static tree
15031 rs6000_check_sdmode (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
15033 /* Don't walk into types. */
15034 if (*tp == NULL_TREE || *tp == error_mark_node || TYPE_P (*tp))
15036 *walk_subtrees = 0;
15037 return NULL_TREE;
15040 switch (TREE_CODE (*tp))
15042 case VAR_DECL:
15043 case PARM_DECL:
15044 case FIELD_DECL:
15045 case RESULT_DECL:
15046 case SSA_NAME:
15047 case REAL_CST:
15048 case MEM_REF:
15049 case VIEW_CONVERT_EXPR:
15050 if (TYPE_MODE (TREE_TYPE (*tp)) == SDmode)
15051 return *tp;
15052 break;
15053 default:
15054 break;
15057 return NULL_TREE;
15060 /* Classify a register type. Because the FMRGOW/FMRGEW instructions only work
15061 on traditional floating point registers, and the VMRGOW/VMRGEW instructions
15062 only work on the traditional altivec registers, note if an altivec register
15063 was chosen. */
15065 static enum rs6000_reg_type
15066 register_to_reg_type (rtx reg, bool *is_altivec)
15068 HOST_WIDE_INT regno;
15069 enum reg_class rclass;
15071 if (GET_CODE (reg) == SUBREG)
15072 reg = SUBREG_REG (reg);
15074 if (!REG_P (reg))
15075 return NO_REG_TYPE;
15077 regno = REGNO (reg);
15078 if (regno >= FIRST_PSEUDO_REGISTER)
15080 if (!lra_in_progress && !reload_in_progress && !reload_completed)
15081 return PSEUDO_REG_TYPE;
15083 regno = true_regnum (reg);
15084 if (regno < 0 || regno >= FIRST_PSEUDO_REGISTER)
15085 return PSEUDO_REG_TYPE;
15088 gcc_assert (regno >= 0);
15090 if (is_altivec && ALTIVEC_REGNO_P (regno))
15091 *is_altivec = true;
15093 rclass = rs6000_regno_regclass[regno];
15094 return reg_class_to_reg_type[(int)rclass];
15097 /* Helper function for rs6000_secondary_reload to return true if a move to a
15098 different register classe is really a simple move. */
15100 static bool
15101 rs6000_secondary_reload_simple_move (enum rs6000_reg_type to_type,
15102 enum rs6000_reg_type from_type,
15103 enum machine_mode mode)
15105 int size;
15107 /* Add support for various direct moves available. In this function, we only
15108 look at cases where we don't need any extra registers, and one or more
15109 simple move insns are issued. At present, 32-bit integers are not allowed
15110 in FPR/VSX registers. Single precision binary floating is not a simple
15111 move because we need to convert to the single precision memory layout.
15112 The 4-byte SDmode can be moved. */
15113 size = GET_MODE_SIZE (mode);
15114 if (TARGET_DIRECT_MOVE
15115 && ((mode == SDmode) || (TARGET_POWERPC64 && size == 8))
15116 && ((to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
15117 || (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)))
15118 return true;
15120 else if (TARGET_MFPGPR && TARGET_POWERPC64 && size == 8
15121 && ((to_type == GPR_REG_TYPE && from_type == FPR_REG_TYPE)
15122 || (to_type == FPR_REG_TYPE && from_type == GPR_REG_TYPE)))
15123 return true;
15125 else if ((size == 4 || (TARGET_POWERPC64 && size == 8))
15126 && ((to_type == GPR_REG_TYPE && from_type == SPR_REG_TYPE)
15127 || (to_type == SPR_REG_TYPE && from_type == GPR_REG_TYPE)))
15128 return true;
15130 return false;
15133 /* Power8 helper function for rs6000_secondary_reload, handle all of the
15134 special direct moves that involve allocating an extra register, return the
15135 insn code of the helper function if there is such a function or
15136 CODE_FOR_nothing if not. */
15138 static bool
15139 rs6000_secondary_reload_direct_move (enum rs6000_reg_type to_type,
15140 enum rs6000_reg_type from_type,
15141 enum machine_mode mode,
15142 secondary_reload_info *sri,
15143 bool altivec_p)
15145 bool ret = false;
15146 enum insn_code icode = CODE_FOR_nothing;
15147 int cost = 0;
15148 int size = GET_MODE_SIZE (mode);
15150 if (TARGET_POWERPC64)
15152 if (size == 16)
15154 /* Handle moving 128-bit values from GPRs to VSX point registers on
15155 power8 when running in 64-bit mode using XXPERMDI to glue the two
15156 64-bit values back together. */
15157 if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)
15159 cost = 3; /* 2 mtvsrd's, 1 xxpermdi. */
15160 icode = reg_addr[mode].reload_vsx_gpr;
15163 /* Handle moving 128-bit values from VSX point registers to GPRs on
15164 power8 when running in 64-bit mode using XXPERMDI to get access to the
15165 bottom 64-bit value. */
15166 else if (to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
15168 cost = 3; /* 2 mfvsrd's, 1 xxpermdi. */
15169 icode = reg_addr[mode].reload_gpr_vsx;
15173 else if (mode == SFmode)
15175 if (to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
15177 cost = 3; /* xscvdpspn, mfvsrd, and. */
15178 icode = reg_addr[mode].reload_gpr_vsx;
15181 else if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)
15183 cost = 2; /* mtvsrz, xscvspdpn. */
15184 icode = reg_addr[mode].reload_vsx_gpr;
15189 if (TARGET_POWERPC64 && size == 16)
15191 /* Handle moving 128-bit values from GPRs to VSX point registers on
15192 power8 when running in 64-bit mode using XXPERMDI to glue the two
15193 64-bit values back together. */
15194 if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)
15196 cost = 3; /* 2 mtvsrd's, 1 xxpermdi. */
15197 icode = reg_addr[mode].reload_vsx_gpr;
15200 /* Handle moving 128-bit values from VSX point registers to GPRs on
15201 power8 when running in 64-bit mode using XXPERMDI to get access to the
15202 bottom 64-bit value. */
15203 else if (to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
15205 cost = 3; /* 2 mfvsrd's, 1 xxpermdi. */
15206 icode = reg_addr[mode].reload_gpr_vsx;
15210 else if (!TARGET_POWERPC64 && size == 8)
15212 /* Handle moving 64-bit values from GPRs to floating point registers on
15213 power8 when running in 32-bit mode using FMRGOW to glue the two 32-bit
15214 values back together. Altivec register classes must be handled
15215 specially since a different instruction is used, and the secondary
15216 reload support requires a single instruction class in the scratch
15217 register constraint. However, right now TFmode is not allowed in
15218 Altivec registers, so the pattern will never match. */
15219 if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE && !altivec_p)
15221 cost = 3; /* 2 mtvsrwz's, 1 fmrgow. */
15222 icode = reg_addr[mode].reload_fpr_gpr;
15226 if (icode != CODE_FOR_nothing)
15228 ret = true;
15229 if (sri)
15231 sri->icode = icode;
15232 sri->extra_cost = cost;
15236 return ret;
15239 /* Return whether a move between two register classes can be done either
15240 directly (simple move) or via a pattern that uses a single extra temporary
15241 (using power8's direct move in this case. */
15243 static bool
15244 rs6000_secondary_reload_move (enum rs6000_reg_type to_type,
15245 enum rs6000_reg_type from_type,
15246 enum machine_mode mode,
15247 secondary_reload_info *sri,
15248 bool altivec_p)
15250 /* Fall back to load/store reloads if either type is not a register. */
15251 if (to_type == NO_REG_TYPE || from_type == NO_REG_TYPE)
15252 return false;
15254 /* If we haven't allocated registers yet, assume the move can be done for the
15255 standard register types. */
15256 if ((to_type == PSEUDO_REG_TYPE && from_type == PSEUDO_REG_TYPE)
15257 || (to_type == PSEUDO_REG_TYPE && IS_STD_REG_TYPE (from_type))
15258 || (from_type == PSEUDO_REG_TYPE && IS_STD_REG_TYPE (to_type)))
15259 return true;
15261 /* Moves to the same set of registers is a simple move for non-specialized
15262 registers. */
15263 if (to_type == from_type && IS_STD_REG_TYPE (to_type))
15264 return true;
15266 /* Check whether a simple move can be done directly. */
15267 if (rs6000_secondary_reload_simple_move (to_type, from_type, mode))
15269 if (sri)
15271 sri->icode = CODE_FOR_nothing;
15272 sri->extra_cost = 0;
15274 return true;
15277 /* Now check if we can do it in a few steps. */
15278 return rs6000_secondary_reload_direct_move (to_type, from_type, mode, sri,
15279 altivec_p);
15282 /* Inform reload about cases where moving X with a mode MODE to a register in
15283 RCLASS requires an extra scratch or immediate register. Return the class
15284 needed for the immediate register.
15286 For VSX and Altivec, we may need a register to convert sp+offset into
15287 reg+sp.
15289 For misaligned 64-bit gpr loads and stores we need a register to
15290 convert an offset address to indirect. */
15292 static reg_class_t
15293 rs6000_secondary_reload (bool in_p,
15294 rtx x,
15295 reg_class_t rclass_i,
15296 enum machine_mode mode,
15297 secondary_reload_info *sri)
15299 enum reg_class rclass = (enum reg_class) rclass_i;
15300 reg_class_t ret = ALL_REGS;
15301 enum insn_code icode;
15302 bool default_p = false;
15304 sri->icode = CODE_FOR_nothing;
15305 icode = ((in_p)
15306 ? reg_addr[mode].reload_load
15307 : reg_addr[mode].reload_store);
15309 if (REG_P (x) || register_operand (x, mode))
15311 enum rs6000_reg_type to_type = reg_class_to_reg_type[(int)rclass];
15312 bool altivec_p = (rclass == ALTIVEC_REGS);
15313 enum rs6000_reg_type from_type = register_to_reg_type (x, &altivec_p);
15315 if (!in_p)
15317 enum rs6000_reg_type exchange = to_type;
15318 to_type = from_type;
15319 from_type = exchange;
15322 /* Can we do a direct move of some sort? */
15323 if (rs6000_secondary_reload_move (to_type, from_type, mode, sri,
15324 altivec_p))
15326 icode = (enum insn_code)sri->icode;
15327 default_p = false;
15328 ret = NO_REGS;
15332 /* Handle vector moves with reload helper functions. */
15333 if (ret == ALL_REGS && icode != CODE_FOR_nothing)
15335 ret = NO_REGS;
15336 sri->icode = CODE_FOR_nothing;
15337 sri->extra_cost = 0;
15339 if (GET_CODE (x) == MEM)
15341 rtx addr = XEXP (x, 0);
15343 /* Loads to and stores from gprs can do reg+offset, and wouldn't need
15344 an extra register in that case, but it would need an extra
15345 register if the addressing is reg+reg or (reg+reg)&(-16). Special
15346 case load/store quad. */
15347 if (rclass == GENERAL_REGS || rclass == BASE_REGS)
15349 if (TARGET_POWERPC64 && TARGET_QUAD_MEMORY
15350 && GET_MODE_SIZE (mode) == 16
15351 && quad_memory_operand (x, mode))
15353 sri->icode = icode;
15354 sri->extra_cost = 2;
15357 else if (!legitimate_indirect_address_p (addr, false)
15358 && !rs6000_legitimate_offset_address_p (PTImode, addr,
15359 false, true))
15361 sri->icode = icode;
15362 /* account for splitting the loads, and converting the
15363 address from reg+reg to reg. */
15364 sri->extra_cost = (((TARGET_64BIT) ? 3 : 5)
15365 + ((GET_CODE (addr) == AND) ? 1 : 0));
15368 /* Allow scalar loads to/from the traditional floating point
15369 registers, even if VSX memory is set. */
15370 else if ((rclass == FLOAT_REGS || rclass == NO_REGS)
15371 && (GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8)
15372 && (legitimate_indirect_address_p (addr, false)
15373 || legitimate_indirect_address_p (addr, false)
15374 || rs6000_legitimate_offset_address_p (mode, addr,
15375 false, true)))
15378 /* Loads to and stores from vector registers can only do reg+reg
15379 addressing. Altivec registers can also do (reg+reg)&(-16). Allow
15380 scalar modes loading up the traditional floating point registers
15381 to use offset addresses. */
15382 else if (rclass == VSX_REGS || rclass == ALTIVEC_REGS
15383 || rclass == FLOAT_REGS || rclass == NO_REGS)
15385 if (!VECTOR_MEM_ALTIVEC_P (mode)
15386 && GET_CODE (addr) == AND
15387 && GET_CODE (XEXP (addr, 1)) == CONST_INT
15388 && INTVAL (XEXP (addr, 1)) == -16
15389 && (legitimate_indirect_address_p (XEXP (addr, 0), false)
15390 || legitimate_indexed_address_p (XEXP (addr, 0), false)))
15392 sri->icode = icode;
15393 sri->extra_cost = ((GET_CODE (XEXP (addr, 0)) == PLUS)
15394 ? 2 : 1);
15396 else if (!legitimate_indirect_address_p (addr, false)
15397 && (rclass == NO_REGS
15398 || !legitimate_indexed_address_p (addr, false)))
15400 sri->icode = icode;
15401 sri->extra_cost = 1;
15403 else
15404 icode = CODE_FOR_nothing;
15406 /* Any other loads, including to pseudo registers which haven't been
15407 assigned to a register yet, default to require a scratch
15408 register. */
15409 else
15411 sri->icode = icode;
15412 sri->extra_cost = 2;
15415 else if (REG_P (x))
15417 int regno = true_regnum (x);
15419 icode = CODE_FOR_nothing;
15420 if (regno < 0 || regno >= FIRST_PSEUDO_REGISTER)
15421 default_p = true;
15422 else
15424 enum reg_class xclass = REGNO_REG_CLASS (regno);
15425 enum rs6000_reg_type rtype1 = reg_class_to_reg_type[(int)rclass];
15426 enum rs6000_reg_type rtype2 = reg_class_to_reg_type[(int)xclass];
15428 /* If memory is needed, use default_secondary_reload to create the
15429 stack slot. */
15430 if (rtype1 != rtype2 || !IS_STD_REG_TYPE (rtype1))
15431 default_p = true;
15432 else
15433 ret = NO_REGS;
15436 else
15437 default_p = true;
15439 else if (TARGET_POWERPC64
15440 && reg_class_to_reg_type[(int)rclass] == GPR_REG_TYPE
15441 && MEM_P (x)
15442 && GET_MODE_SIZE (GET_MODE (x)) >= UNITS_PER_WORD)
15444 rtx addr = XEXP (x, 0);
15445 rtx off = address_offset (addr);
15447 if (off != NULL_RTX)
15449 unsigned int extra = GET_MODE_SIZE (GET_MODE (x)) - UNITS_PER_WORD;
15450 unsigned HOST_WIDE_INT offset = INTVAL (off);
15452 /* We need a secondary reload when our legitimate_address_p
15453 says the address is good (as otherwise the entire address
15454 will be reloaded), and the offset is not a multiple of
15455 four or we have an address wrap. Address wrap will only
15456 occur for LO_SUMs since legitimate_offset_address_p
15457 rejects addresses for 16-byte mems that will wrap. */
15458 if (GET_CODE (addr) == LO_SUM
15459 ? (1 /* legitimate_address_p allows any offset for lo_sum */
15460 && ((offset & 3) != 0
15461 || ((offset & 0xffff) ^ 0x8000) >= 0x10000 - extra))
15462 : (offset + 0x8000 < 0x10000 - extra /* legitimate_address_p */
15463 && (offset & 3) != 0))
15465 if (in_p)
15466 sri->icode = CODE_FOR_reload_di_load;
15467 else
15468 sri->icode = CODE_FOR_reload_di_store;
15469 sri->extra_cost = 2;
15470 ret = NO_REGS;
15472 else
15473 default_p = true;
15475 else
15476 default_p = true;
15478 else if (!TARGET_POWERPC64
15479 && reg_class_to_reg_type[(int)rclass] == GPR_REG_TYPE
15480 && MEM_P (x)
15481 && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
15483 rtx addr = XEXP (x, 0);
15484 rtx off = address_offset (addr);
15486 if (off != NULL_RTX)
15488 unsigned int extra = GET_MODE_SIZE (GET_MODE (x)) - UNITS_PER_WORD;
15489 unsigned HOST_WIDE_INT offset = INTVAL (off);
15491 /* We need a secondary reload when our legitimate_address_p
15492 says the address is good (as otherwise the entire address
15493 will be reloaded), and we have a wrap.
15495 legitimate_lo_sum_address_p allows LO_SUM addresses to
15496 have any offset so test for wrap in the low 16 bits.
15498 legitimate_offset_address_p checks for the range
15499 [-0x8000,0x7fff] for mode size of 8 and [-0x8000,0x7ff7]
15500 for mode size of 16. We wrap at [0x7ffc,0x7fff] and
15501 [0x7ff4,0x7fff] respectively, so test for the
15502 intersection of these ranges, [0x7ffc,0x7fff] and
15503 [0x7ff4,0x7ff7] respectively.
15505 Note that the address we see here may have been
15506 manipulated by legitimize_reload_address. */
15507 if (GET_CODE (addr) == LO_SUM
15508 ? ((offset & 0xffff) ^ 0x8000) >= 0x10000 - extra
15509 : offset - (0x8000 - extra) < UNITS_PER_WORD)
15511 if (in_p)
15512 sri->icode = CODE_FOR_reload_si_load;
15513 else
15514 sri->icode = CODE_FOR_reload_si_store;
15515 sri->extra_cost = 2;
15516 ret = NO_REGS;
15518 else
15519 default_p = true;
15521 else
15522 default_p = true;
15524 else
15525 default_p = true;
15527 if (default_p)
15528 ret = default_secondary_reload (in_p, x, rclass, mode, sri);
15530 gcc_assert (ret != ALL_REGS);
15532 if (TARGET_DEBUG_ADDR)
15534 fprintf (stderr,
15535 "\nrs6000_secondary_reload, return %s, in_p = %s, rclass = %s, "
15536 "mode = %s",
15537 reg_class_names[ret],
15538 in_p ? "true" : "false",
15539 reg_class_names[rclass],
15540 GET_MODE_NAME (mode));
15542 if (default_p)
15543 fprintf (stderr, ", default secondary reload");
15545 if (sri->icode != CODE_FOR_nothing)
15546 fprintf (stderr, ", reload func = %s, extra cost = %d\n",
15547 insn_data[sri->icode].name, sri->extra_cost);
15548 else
15549 fprintf (stderr, "\n");
15551 debug_rtx (x);
15554 return ret;
15557 /* Better tracing for rs6000_secondary_reload_inner. */
15559 static void
15560 rs6000_secondary_reload_trace (int line, rtx reg, rtx mem, rtx scratch,
15561 bool store_p)
15563 rtx set, clobber;
15565 gcc_assert (reg != NULL_RTX && mem != NULL_RTX && scratch != NULL_RTX);
15567 fprintf (stderr, "rs6000_secondary_reload_inner:%d, type = %s\n", line,
15568 store_p ? "store" : "load");
15570 if (store_p)
15571 set = gen_rtx_SET (VOIDmode, mem, reg);
15572 else
15573 set = gen_rtx_SET (VOIDmode, reg, mem);
15575 clobber = gen_rtx_CLOBBER (VOIDmode, scratch);
15576 debug_rtx (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set, clobber)));
15579 static void
15580 rs6000_secondary_reload_fail (int line, rtx reg, rtx mem, rtx scratch,
15581 bool store_p)
15583 rs6000_secondary_reload_trace (line, reg, mem, scratch, store_p);
15584 gcc_unreachable ();
15587 /* Fixup reload addresses for Altivec or VSX loads/stores to change SP+offset
15588 to SP+reg addressing. */
15590 void
15591 rs6000_secondary_reload_inner (rtx reg, rtx mem, rtx scratch, bool store_p)
15593 int regno = true_regnum (reg);
15594 enum machine_mode mode = GET_MODE (reg);
15595 enum reg_class rclass;
15596 rtx addr;
15597 rtx and_op2 = NULL_RTX;
15598 rtx addr_op1;
15599 rtx addr_op2;
15600 rtx scratch_or_premodify = scratch;
15601 rtx and_rtx;
15602 rtx cc_clobber;
15604 if (TARGET_DEBUG_ADDR)
15605 rs6000_secondary_reload_trace (__LINE__, reg, mem, scratch, store_p);
15607 if (regno < 0 || regno >= FIRST_PSEUDO_REGISTER)
15608 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
15610 if (GET_CODE (mem) != MEM)
15611 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
15613 rclass = REGNO_REG_CLASS (regno);
15614 addr = XEXP (mem, 0);
15616 switch (rclass)
15618 /* GPRs can handle reg + small constant, all other addresses need to use
15619 the scratch register. */
15620 case GENERAL_REGS:
15621 case BASE_REGS:
15622 if (GET_CODE (addr) == AND)
15624 and_op2 = XEXP (addr, 1);
15625 addr = XEXP (addr, 0);
15628 if (GET_CODE (addr) == PRE_MODIFY)
15630 scratch_or_premodify = XEXP (addr, 0);
15631 if (!REG_P (scratch_or_premodify))
15632 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
15634 if (GET_CODE (XEXP (addr, 1)) != PLUS)
15635 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
15637 addr = XEXP (addr, 1);
15640 if (GET_CODE (addr) == PLUS
15641 && (and_op2 != NULL_RTX
15642 || !rs6000_legitimate_offset_address_p (PTImode, addr,
15643 false, true)))
15645 addr_op1 = XEXP (addr, 0);
15646 addr_op2 = XEXP (addr, 1);
15647 if (!legitimate_indirect_address_p (addr_op1, false))
15648 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
15650 if (!REG_P (addr_op2)
15651 && (GET_CODE (addr_op2) != CONST_INT
15652 || !satisfies_constraint_I (addr_op2)))
15654 if (TARGET_DEBUG_ADDR)
15656 fprintf (stderr,
15657 "\nMove plus addr to register %s, mode = %s: ",
15658 rs6000_reg_names[REGNO (scratch)],
15659 GET_MODE_NAME (mode));
15660 debug_rtx (addr_op2);
15662 rs6000_emit_move (scratch, addr_op2, Pmode);
15663 addr_op2 = scratch;
15666 emit_insn (gen_rtx_SET (VOIDmode,
15667 scratch_or_premodify,
15668 gen_rtx_PLUS (Pmode,
15669 addr_op1,
15670 addr_op2)));
15672 addr = scratch_or_premodify;
15673 scratch_or_premodify = scratch;
15675 else if (!legitimate_indirect_address_p (addr, false)
15676 && !rs6000_legitimate_offset_address_p (PTImode, addr,
15677 false, true))
15679 if (TARGET_DEBUG_ADDR)
15681 fprintf (stderr, "\nMove addr to register %s, mode = %s: ",
15682 rs6000_reg_names[REGNO (scratch_or_premodify)],
15683 GET_MODE_NAME (mode));
15684 debug_rtx (addr);
15686 rs6000_emit_move (scratch_or_premodify, addr, Pmode);
15687 addr = scratch_or_premodify;
15688 scratch_or_premodify = scratch;
15690 break;
15692 /* Float registers can do offset+reg addressing for scalar types. */
15693 case FLOAT_REGS:
15694 if (legitimate_indirect_address_p (addr, false) /* reg */
15695 || legitimate_indexed_address_p (addr, false) /* reg+reg */
15696 || ((GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8)
15697 && and_op2 == NULL_RTX
15698 && scratch_or_premodify == scratch
15699 && rs6000_legitimate_offset_address_p (mode, addr, false, false)))
15700 break;
15702 /* If this isn't a legacy floating point load/store, fall through to the
15703 VSX defaults. */
15705 /* VSX/Altivec registers can only handle reg+reg addressing. Move other
15706 addresses into a scratch register. */
15707 case VSX_REGS:
15708 case ALTIVEC_REGS:
15710 /* With float regs, we need to handle the AND ourselves, since we can't
15711 use the Altivec instruction with an implicit AND -16. Allow scalar
15712 loads to float registers to use reg+offset even if VSX. */
15713 if (GET_CODE (addr) == AND
15714 && (rclass != ALTIVEC_REGS || GET_MODE_SIZE (mode) != 16
15715 || GET_CODE (XEXP (addr, 1)) != CONST_INT
15716 || INTVAL (XEXP (addr, 1)) != -16
15717 || !VECTOR_MEM_ALTIVEC_P (mode)))
15719 and_op2 = XEXP (addr, 1);
15720 addr = XEXP (addr, 0);
15723 /* If we aren't using a VSX load, save the PRE_MODIFY register and use it
15724 as the address later. */
15725 if (GET_CODE (addr) == PRE_MODIFY
15726 && ((ALTIVEC_OR_VSX_VECTOR_MODE (mode)
15727 && (rclass != FLOAT_REGS
15728 || (GET_MODE_SIZE (mode) != 4 && GET_MODE_SIZE (mode) != 8)))
15729 || and_op2 != NULL_RTX
15730 || !legitimate_indexed_address_p (XEXP (addr, 1), false)))
15732 scratch_or_premodify = XEXP (addr, 0);
15733 if (!legitimate_indirect_address_p (scratch_or_premodify, false))
15734 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
15736 if (GET_CODE (XEXP (addr, 1)) != PLUS)
15737 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
15739 addr = XEXP (addr, 1);
15742 if (legitimate_indirect_address_p (addr, false) /* reg */
15743 || legitimate_indexed_address_p (addr, false) /* reg+reg */
15744 || (GET_CODE (addr) == AND /* Altivec memory */
15745 && rclass == ALTIVEC_REGS
15746 && GET_CODE (XEXP (addr, 1)) == CONST_INT
15747 && INTVAL (XEXP (addr, 1)) == -16
15748 && (legitimate_indirect_address_p (XEXP (addr, 0), false)
15749 || legitimate_indexed_address_p (XEXP (addr, 0), false))))
15752 else if (GET_CODE (addr) == PLUS)
15754 addr_op1 = XEXP (addr, 0);
15755 addr_op2 = XEXP (addr, 1);
15756 if (!REG_P (addr_op1))
15757 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
15759 if (TARGET_DEBUG_ADDR)
15761 fprintf (stderr, "\nMove plus addr to register %s, mode = %s: ",
15762 rs6000_reg_names[REGNO (scratch)], GET_MODE_NAME (mode));
15763 debug_rtx (addr_op2);
15765 rs6000_emit_move (scratch, addr_op2, Pmode);
15766 emit_insn (gen_rtx_SET (VOIDmode,
15767 scratch_or_premodify,
15768 gen_rtx_PLUS (Pmode,
15769 addr_op1,
15770 scratch)));
15771 addr = scratch_or_premodify;
15772 scratch_or_premodify = scratch;
15775 else if (GET_CODE (addr) == SYMBOL_REF || GET_CODE (addr) == CONST
15776 || GET_CODE (addr) == CONST_INT || GET_CODE (addr) == LO_SUM
15777 || REG_P (addr))
15779 if (TARGET_DEBUG_ADDR)
15781 fprintf (stderr, "\nMove addr to register %s, mode = %s: ",
15782 rs6000_reg_names[REGNO (scratch_or_premodify)],
15783 GET_MODE_NAME (mode));
15784 debug_rtx (addr);
15787 rs6000_emit_move (scratch_or_premodify, addr, Pmode);
15788 addr = scratch_or_premodify;
15789 scratch_or_premodify = scratch;
15792 else
15793 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
15795 break;
15797 default:
15798 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
15801 /* If the original address involved a pre-modify that we couldn't use the VSX
15802 memory instruction with update, and we haven't taken care of already,
15803 store the address in the pre-modify register and use that as the
15804 address. */
15805 if (scratch_or_premodify != scratch && scratch_or_premodify != addr)
15807 emit_insn (gen_rtx_SET (VOIDmode, scratch_or_premodify, addr));
15808 addr = scratch_or_premodify;
15811 /* If the original address involved an AND -16 and we couldn't use an ALTIVEC
15812 memory instruction, recreate the AND now, including the clobber which is
15813 generated by the general ANDSI3/ANDDI3 patterns for the
15814 andi. instruction. */
15815 if (and_op2 != NULL_RTX)
15817 if (! legitimate_indirect_address_p (addr, false))
15819 emit_insn (gen_rtx_SET (VOIDmode, scratch, addr));
15820 addr = scratch;
15823 if (TARGET_DEBUG_ADDR)
15825 fprintf (stderr, "\nAnd addr to register %s, mode = %s: ",
15826 rs6000_reg_names[REGNO (scratch)], GET_MODE_NAME (mode));
15827 debug_rtx (and_op2);
15830 and_rtx = gen_rtx_SET (VOIDmode,
15831 scratch,
15832 gen_rtx_AND (Pmode,
15833 addr,
15834 and_op2));
15836 cc_clobber = gen_rtx_CLOBBER (CCmode, gen_rtx_SCRATCH (CCmode));
15837 emit_insn (gen_rtx_PARALLEL (VOIDmode,
15838 gen_rtvec (2, and_rtx, cc_clobber)));
15839 addr = scratch;
15842 /* Adjust the address if it changed. */
15843 if (addr != XEXP (mem, 0))
15845 mem = replace_equiv_address_nv (mem, addr);
15846 if (TARGET_DEBUG_ADDR)
15847 fprintf (stderr, "\nrs6000_secondary_reload_inner, mem adjusted.\n");
15850 /* Now create the move. */
15851 if (store_p)
15852 emit_insn (gen_rtx_SET (VOIDmode, mem, reg));
15853 else
15854 emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
15856 return;
15859 /* Convert reloads involving 64-bit gprs and misaligned offset
15860 addressing, or multiple 32-bit gprs and offsets that are too large,
15861 to use indirect addressing. */
15863 void
15864 rs6000_secondary_reload_gpr (rtx reg, rtx mem, rtx scratch, bool store_p)
15866 int regno = true_regnum (reg);
15867 enum reg_class rclass;
15868 rtx addr;
15869 rtx scratch_or_premodify = scratch;
15871 if (TARGET_DEBUG_ADDR)
15873 fprintf (stderr, "\nrs6000_secondary_reload_gpr, type = %s\n",
15874 store_p ? "store" : "load");
15875 fprintf (stderr, "reg:\n");
15876 debug_rtx (reg);
15877 fprintf (stderr, "mem:\n");
15878 debug_rtx (mem);
15879 fprintf (stderr, "scratch:\n");
15880 debug_rtx (scratch);
15883 gcc_assert (regno >= 0 && regno < FIRST_PSEUDO_REGISTER);
15884 gcc_assert (GET_CODE (mem) == MEM);
15885 rclass = REGNO_REG_CLASS (regno);
15886 gcc_assert (rclass == GENERAL_REGS || rclass == BASE_REGS);
15887 addr = XEXP (mem, 0);
15889 if (GET_CODE (addr) == PRE_MODIFY)
15891 scratch_or_premodify = XEXP (addr, 0);
15892 gcc_assert (REG_P (scratch_or_premodify));
15893 addr = XEXP (addr, 1);
15895 gcc_assert (GET_CODE (addr) == PLUS || GET_CODE (addr) == LO_SUM);
15897 rs6000_emit_move (scratch_or_premodify, addr, Pmode);
15899 mem = replace_equiv_address_nv (mem, scratch_or_premodify);
15901 /* Now create the move. */
15902 if (store_p)
15903 emit_insn (gen_rtx_SET (VOIDmode, mem, reg));
15904 else
15905 emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
15907 return;
15910 /* Allocate a 64-bit stack slot to be used for copying SDmode values through if
15911 this function has any SDmode references. If we are on a power7 or later, we
15912 don't need the 64-bit stack slot since the LFIWZX and STIFWX instructions
15913 can load/store the value. */
15915 static void
15916 rs6000_alloc_sdmode_stack_slot (void)
15918 tree t;
15919 basic_block bb;
15920 gimple_stmt_iterator gsi;
15922 gcc_assert (cfun->machine->sdmode_stack_slot == NULL_RTX);
15923 /* We use a different approach for dealing with the secondary
15924 memory in LRA. */
15925 if (ira_use_lra_p)
15926 return;
15928 if (TARGET_NO_SDMODE_STACK)
15929 return;
15931 FOR_EACH_BB (bb)
15932 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
15934 tree ret = walk_gimple_op (gsi_stmt (gsi), rs6000_check_sdmode, NULL);
15935 if (ret)
15937 rtx stack = assign_stack_local (DDmode, GET_MODE_SIZE (DDmode), 0);
15938 cfun->machine->sdmode_stack_slot = adjust_address_nv (stack,
15939 SDmode, 0);
15940 return;
15944 /* Check for any SDmode parameters of the function. */
15945 for (t = DECL_ARGUMENTS (cfun->decl); t; t = DECL_CHAIN (t))
15947 if (TREE_TYPE (t) == error_mark_node)
15948 continue;
15950 if (TYPE_MODE (TREE_TYPE (t)) == SDmode
15951 || TYPE_MODE (DECL_ARG_TYPE (t)) == SDmode)
15953 rtx stack = assign_stack_local (DDmode, GET_MODE_SIZE (DDmode), 0);
15954 cfun->machine->sdmode_stack_slot = adjust_address_nv (stack,
15955 SDmode, 0);
15956 return;
15961 static void
15962 rs6000_instantiate_decls (void)
15964 if (cfun->machine->sdmode_stack_slot != NULL_RTX)
15965 instantiate_decl_rtl (cfun->machine->sdmode_stack_slot);
15968 /* Given an rtx X being reloaded into a reg required to be
15969 in class CLASS, return the class of reg to actually use.
15970 In general this is just CLASS; but on some machines
15971 in some cases it is preferable to use a more restrictive class.
15973 On the RS/6000, we have to return NO_REGS when we want to reload a
15974 floating-point CONST_DOUBLE to force it to be copied to memory.
15976 We also don't want to reload integer values into floating-point
15977 registers if we can at all help it. In fact, this can
15978 cause reload to die, if it tries to generate a reload of CTR
15979 into a FP register and discovers it doesn't have the memory location
15980 required.
15982 ??? Would it be a good idea to have reload do the converse, that is
15983 try to reload floating modes into FP registers if possible?
15986 static enum reg_class
15987 rs6000_preferred_reload_class (rtx x, enum reg_class rclass)
15989 enum machine_mode mode = GET_MODE (x);
15991 if (TARGET_VSX && x == CONST0_RTX (mode) && VSX_REG_CLASS_P (rclass))
15992 return rclass;
15994 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (mode)
15995 && (rclass == ALTIVEC_REGS || rclass == VSX_REGS)
15996 && easy_vector_constant (x, mode))
15997 return ALTIVEC_REGS;
15999 if (CONSTANT_P (x) && reg_classes_intersect_p (rclass, FLOAT_REGS))
16000 return NO_REGS;
16002 if (GET_MODE_CLASS (mode) == MODE_INT && rclass == NON_SPECIAL_REGS)
16003 return GENERAL_REGS;
16005 /* For VSX, prefer the traditional registers for 64-bit values because we can
16006 use the non-VSX loads. Prefer the Altivec registers if Altivec is
16007 handling the vector operations (i.e. V16QI, V8HI, and V4SI), or if we
16008 prefer Altivec loads.. */
16009 if (rclass == VSX_REGS)
16011 if (GET_MODE_SIZE (mode) <= 8)
16012 return FLOAT_REGS;
16014 if (VECTOR_UNIT_ALTIVEC_P (mode) || VECTOR_MEM_ALTIVEC_P (mode))
16015 return ALTIVEC_REGS;
16017 return rclass;
16020 return rclass;
16023 /* Debug version of rs6000_preferred_reload_class. */
16024 static enum reg_class
16025 rs6000_debug_preferred_reload_class (rtx x, enum reg_class rclass)
16027 enum reg_class ret = rs6000_preferred_reload_class (x, rclass);
16029 fprintf (stderr,
16030 "\nrs6000_preferred_reload_class, return %s, rclass = %s, "
16031 "mode = %s, x:\n",
16032 reg_class_names[ret], reg_class_names[rclass],
16033 GET_MODE_NAME (GET_MODE (x)));
16034 debug_rtx (x);
16036 return ret;
16039 /* If we are copying between FP or AltiVec registers and anything else, we need
16040 a memory location. The exception is when we are targeting ppc64 and the
16041 move to/from fpr to gpr instructions are available. Also, under VSX, you
16042 can copy vector registers from the FP register set to the Altivec register
16043 set and vice versa. */
16045 static bool
16046 rs6000_secondary_memory_needed (enum reg_class from_class,
16047 enum reg_class to_class,
16048 enum machine_mode mode)
16050 enum rs6000_reg_type from_type, to_type;
16051 bool altivec_p = ((from_class == ALTIVEC_REGS)
16052 || (to_class == ALTIVEC_REGS));
16054 /* If a simple/direct move is available, we don't need secondary memory */
16055 from_type = reg_class_to_reg_type[(int)from_class];
16056 to_type = reg_class_to_reg_type[(int)to_class];
16058 if (rs6000_secondary_reload_move (to_type, from_type, mode,
16059 (secondary_reload_info *)0, altivec_p))
16060 return false;
16062 /* If we have a floating point or vector register class, we need to use
16063 memory to transfer the data. */
16064 if (IS_FP_VECT_REG_TYPE (from_type) || IS_FP_VECT_REG_TYPE (to_type))
16065 return true;
16067 return false;
16070 /* Debug version of rs6000_secondary_memory_needed. */
16071 static bool
16072 rs6000_debug_secondary_memory_needed (enum reg_class from_class,
16073 enum reg_class to_class,
16074 enum machine_mode mode)
16076 bool ret = rs6000_secondary_memory_needed (from_class, to_class, mode);
16078 fprintf (stderr,
16079 "rs6000_secondary_memory_needed, return: %s, from_class = %s, "
16080 "to_class = %s, mode = %s\n",
16081 ret ? "true" : "false",
16082 reg_class_names[from_class],
16083 reg_class_names[to_class],
16084 GET_MODE_NAME (mode));
16086 return ret;
16089 /* Return the register class of a scratch register needed to copy IN into
16090 or out of a register in RCLASS in MODE. If it can be done directly,
16091 NO_REGS is returned. */
16093 static enum reg_class
16094 rs6000_secondary_reload_class (enum reg_class rclass, enum machine_mode mode,
16095 rtx in)
16097 int regno;
16099 if (TARGET_ELF || (DEFAULT_ABI == ABI_DARWIN
16100 #if TARGET_MACHO
16101 && MACHOPIC_INDIRECT
16102 #endif
16105 /* We cannot copy a symbolic operand directly into anything
16106 other than BASE_REGS for TARGET_ELF. So indicate that a
16107 register from BASE_REGS is needed as an intermediate
16108 register.
16110 On Darwin, pic addresses require a load from memory, which
16111 needs a base register. */
16112 if (rclass != BASE_REGS
16113 && (GET_CODE (in) == SYMBOL_REF
16114 || GET_CODE (in) == HIGH
16115 || GET_CODE (in) == LABEL_REF
16116 || GET_CODE (in) == CONST))
16117 return BASE_REGS;
16120 if (GET_CODE (in) == REG)
16122 regno = REGNO (in);
16123 if (regno >= FIRST_PSEUDO_REGISTER)
16125 regno = true_regnum (in);
16126 if (regno >= FIRST_PSEUDO_REGISTER)
16127 regno = -1;
16130 else if (GET_CODE (in) == SUBREG)
16132 regno = true_regnum (in);
16133 if (regno >= FIRST_PSEUDO_REGISTER)
16134 regno = -1;
16136 else
16137 regno = -1;
16139 /* We can place anything into GENERAL_REGS and can put GENERAL_REGS
16140 into anything. */
16141 if (rclass == GENERAL_REGS || rclass == BASE_REGS
16142 || (regno >= 0 && INT_REGNO_P (regno)))
16143 return NO_REGS;
16145 /* Constants, memory, and FP registers can go into FP registers. */
16146 if ((regno == -1 || FP_REGNO_P (regno))
16147 && (rclass == FLOAT_REGS || rclass == NON_SPECIAL_REGS))
16148 return (mode != SDmode || lra_in_progress) ? NO_REGS : GENERAL_REGS;
16150 /* Memory, and FP/altivec registers can go into fp/altivec registers under
16151 VSX. However, for scalar variables, use the traditional floating point
16152 registers so that we can use offset+register addressing. */
16153 if (TARGET_VSX
16154 && (regno == -1 || VSX_REGNO_P (regno))
16155 && VSX_REG_CLASS_P (rclass))
16157 if (GET_MODE_SIZE (mode) < 16)
16158 return FLOAT_REGS;
16160 return NO_REGS;
16163 /* Memory, and AltiVec registers can go into AltiVec registers. */
16164 if ((regno == -1 || ALTIVEC_REGNO_P (regno))
16165 && rclass == ALTIVEC_REGS)
16166 return NO_REGS;
16168 /* We can copy among the CR registers. */
16169 if ((rclass == CR_REGS || rclass == CR0_REGS)
16170 && regno >= 0 && CR_REGNO_P (regno))
16171 return NO_REGS;
16173 /* Otherwise, we need GENERAL_REGS. */
16174 return GENERAL_REGS;
16177 /* Debug version of rs6000_secondary_reload_class. */
16178 static enum reg_class
16179 rs6000_debug_secondary_reload_class (enum reg_class rclass,
16180 enum machine_mode mode, rtx in)
16182 enum reg_class ret = rs6000_secondary_reload_class (rclass, mode, in);
16183 fprintf (stderr,
16184 "\nrs6000_secondary_reload_class, return %s, rclass = %s, "
16185 "mode = %s, input rtx:\n",
16186 reg_class_names[ret], reg_class_names[rclass],
16187 GET_MODE_NAME (mode));
16188 debug_rtx (in);
16190 return ret;
16193 /* Return nonzero if for CLASS a mode change from FROM to TO is invalid. */
16195 static bool
16196 rs6000_cannot_change_mode_class (enum machine_mode from,
16197 enum machine_mode to,
16198 enum reg_class rclass)
16200 unsigned from_size = GET_MODE_SIZE (from);
16201 unsigned to_size = GET_MODE_SIZE (to);
16203 if (from_size != to_size)
16205 enum reg_class xclass = (TARGET_VSX) ? VSX_REGS : FLOAT_REGS;
16207 if (reg_classes_intersect_p (xclass, rclass))
16209 unsigned to_nregs = hard_regno_nregs[FIRST_FPR_REGNO][to];
16210 unsigned from_nregs = hard_regno_nregs[FIRST_FPR_REGNO][from];
16212 /* Don't allow 64-bit types to overlap with 128-bit types that take a
16213 single register under VSX because the scalar part of the register
16214 is in the upper 64-bits, and not the lower 64-bits. Types like
16215 TFmode/TDmode that take 2 scalar register can overlap. 128-bit
16216 IEEE floating point can't overlap, and neither can small
16217 values. */
16219 if (TARGET_IEEEQUAD && (to == TFmode || from == TFmode))
16220 return true;
16222 if (from_size < 8 || to_size < 8)
16223 return true;
16225 if (from_size == 8 && (8 * to_nregs) != to_size)
16226 return true;
16228 if (to_size == 8 && (8 * from_nregs) != from_size)
16229 return true;
16231 return false;
16233 else
16234 return false;
16237 if (TARGET_E500_DOUBLE
16238 && ((((to) == DFmode) + ((from) == DFmode)) == 1
16239 || (((to) == TFmode) + ((from) == TFmode)) == 1
16240 || (((to) == DDmode) + ((from) == DDmode)) == 1
16241 || (((to) == TDmode) + ((from) == TDmode)) == 1
16242 || (((to) == DImode) + ((from) == DImode)) == 1))
16243 return true;
16245 /* Since the VSX register set includes traditional floating point registers
16246 and altivec registers, just check for the size being different instead of
16247 trying to check whether the modes are vector modes. Otherwise it won't
16248 allow say DF and DI to change classes. For types like TFmode and TDmode
16249 that take 2 64-bit registers, rather than a single 128-bit register, don't
16250 allow subregs of those types to other 128 bit types. */
16251 if (TARGET_VSX && VSX_REG_CLASS_P (rclass))
16253 unsigned num_regs = (from_size + 15) / 16;
16254 if (hard_regno_nregs[FIRST_FPR_REGNO][to] > num_regs
16255 || hard_regno_nregs[FIRST_FPR_REGNO][from] > num_regs)
16256 return true;
16258 return (from_size != 8 && from_size != 16);
16261 if (TARGET_ALTIVEC && rclass == ALTIVEC_REGS
16262 && (ALTIVEC_VECTOR_MODE (from) + ALTIVEC_VECTOR_MODE (to)) == 1)
16263 return true;
16265 if (TARGET_SPE && (SPE_VECTOR_MODE (from) + SPE_VECTOR_MODE (to)) == 1
16266 && reg_classes_intersect_p (GENERAL_REGS, rclass))
16267 return true;
16269 return false;
16272 /* Debug version of rs6000_cannot_change_mode_class. */
16273 static bool
16274 rs6000_debug_cannot_change_mode_class (enum machine_mode from,
16275 enum machine_mode to,
16276 enum reg_class rclass)
16278 bool ret = rs6000_cannot_change_mode_class (from, to, rclass);
16280 fprintf (stderr,
16281 "rs6000_cannot_change_mode_class, return %s, from = %s, "
16282 "to = %s, rclass = %s\n",
16283 ret ? "true" : "false",
16284 GET_MODE_NAME (from), GET_MODE_NAME (to),
16285 reg_class_names[rclass]);
16287 return ret;
16290 /* Return a string to do a move operation of 128 bits of data. */
16292 const char *
16293 rs6000_output_move_128bit (rtx operands[])
16295 rtx dest = operands[0];
16296 rtx src = operands[1];
16297 enum machine_mode mode = GET_MODE (dest);
16298 int dest_regno;
16299 int src_regno;
16300 bool dest_gpr_p, dest_fp_p, dest_vmx_p, dest_vsx_p;
16301 bool src_gpr_p, src_fp_p, src_vmx_p, src_vsx_p;
16303 if (REG_P (dest))
16305 dest_regno = REGNO (dest);
16306 dest_gpr_p = INT_REGNO_P (dest_regno);
16307 dest_fp_p = FP_REGNO_P (dest_regno);
16308 dest_vmx_p = ALTIVEC_REGNO_P (dest_regno);
16309 dest_vsx_p = dest_fp_p | dest_vmx_p;
16311 else
16313 dest_regno = -1;
16314 dest_gpr_p = dest_fp_p = dest_vmx_p = dest_vsx_p = false;
16317 if (REG_P (src))
16319 src_regno = REGNO (src);
16320 src_gpr_p = INT_REGNO_P (src_regno);
16321 src_fp_p = FP_REGNO_P (src_regno);
16322 src_vmx_p = ALTIVEC_REGNO_P (src_regno);
16323 src_vsx_p = src_fp_p | src_vmx_p;
16325 else
16327 src_regno = -1;
16328 src_gpr_p = src_fp_p = src_vmx_p = src_vsx_p = false;
16331 /* Register moves. */
16332 if (dest_regno >= 0 && src_regno >= 0)
16334 if (dest_gpr_p)
16336 if (src_gpr_p)
16337 return "#";
16339 else if (TARGET_VSX && TARGET_DIRECT_MOVE && src_vsx_p)
16340 return "#";
16343 else if (TARGET_VSX && dest_vsx_p)
16345 if (src_vsx_p)
16346 return "xxlor %x0,%x1,%x1";
16348 else if (TARGET_DIRECT_MOVE && src_gpr_p)
16349 return "#";
16352 else if (TARGET_ALTIVEC && dest_vmx_p && src_vmx_p)
16353 return "vor %0,%1,%1";
16355 else if (dest_fp_p && src_fp_p)
16356 return "#";
16359 /* Loads. */
16360 else if (dest_regno >= 0 && MEM_P (src))
16362 if (dest_gpr_p)
16364 if (TARGET_QUAD_MEMORY && quad_load_store_p (dest, src))
16365 return "lq %0,%1";
16366 else
16367 return "#";
16370 else if (TARGET_ALTIVEC && dest_vmx_p
16371 && altivec_indexed_or_indirect_operand (src, mode))
16372 return "lvx %0,%y1";
16374 else if (TARGET_VSX && dest_vsx_p)
16376 if (mode == V16QImode || mode == V8HImode || mode == V4SImode)
16377 return "lxvw4x %x0,%y1";
16378 else
16379 return "lxvd2x %x0,%y1";
16382 else if (TARGET_ALTIVEC && dest_vmx_p)
16383 return "lvx %0,%y1";
16385 else if (dest_fp_p)
16386 return "#";
16389 /* Stores. */
16390 else if (src_regno >= 0 && MEM_P (dest))
16392 if (src_gpr_p)
16394 if (TARGET_QUAD_MEMORY && quad_load_store_p (dest, src))
16395 return "stq %1,%0";
16396 else
16397 return "#";
16400 else if (TARGET_ALTIVEC && src_vmx_p
16401 && altivec_indexed_or_indirect_operand (src, mode))
16402 return "stvx %1,%y0";
16404 else if (TARGET_VSX && src_vsx_p)
16406 if (mode == V16QImode || mode == V8HImode || mode == V4SImode)
16407 return "stxvw4x %x1,%y0";
16408 else
16409 return "stxvd2x %x1,%y0";
16412 else if (TARGET_ALTIVEC && src_vmx_p)
16413 return "stvx %1,%y0";
16415 else if (src_fp_p)
16416 return "#";
16419 /* Constants. */
16420 else if (dest_regno >= 0
16421 && (GET_CODE (src) == CONST_INT
16422 || GET_CODE (src) == CONST_DOUBLE
16423 || GET_CODE (src) == CONST_VECTOR))
16425 if (dest_gpr_p)
16426 return "#";
16428 else if (TARGET_VSX && dest_vsx_p && zero_constant (src, mode))
16429 return "xxlxor %x0,%x0,%x0";
16431 else if (TARGET_ALTIVEC && dest_vmx_p)
16432 return output_vec_const_move (operands);
16435 if (TARGET_DEBUG_ADDR)
16437 fprintf (stderr, "\n===== Bad 128 bit move:\n");
16438 debug_rtx (gen_rtx_SET (VOIDmode, dest, src));
16441 gcc_unreachable ();
16445 /* Given a comparison operation, return the bit number in CCR to test. We
16446 know this is a valid comparison.
16448 SCC_P is 1 if this is for an scc. That means that %D will have been
16449 used instead of %C, so the bits will be in different places.
16451 Return -1 if OP isn't a valid comparison for some reason. */
16454 ccr_bit (rtx op, int scc_p)
16456 enum rtx_code code = GET_CODE (op);
16457 enum machine_mode cc_mode;
16458 int cc_regnum;
16459 int base_bit;
16460 rtx reg;
16462 if (!COMPARISON_P (op))
16463 return -1;
16465 reg = XEXP (op, 0);
16467 gcc_assert (GET_CODE (reg) == REG && CR_REGNO_P (REGNO (reg)));
16469 cc_mode = GET_MODE (reg);
16470 cc_regnum = REGNO (reg);
16471 base_bit = 4 * (cc_regnum - CR0_REGNO);
16473 validate_condition_mode (code, cc_mode);
16475 /* When generating a sCOND operation, only positive conditions are
16476 allowed. */
16477 gcc_assert (!scc_p
16478 || code == EQ || code == GT || code == LT || code == UNORDERED
16479 || code == GTU || code == LTU);
16481 switch (code)
16483 case NE:
16484 return scc_p ? base_bit + 3 : base_bit + 2;
16485 case EQ:
16486 return base_bit + 2;
16487 case GT: case GTU: case UNLE:
16488 return base_bit + 1;
16489 case LT: case LTU: case UNGE:
16490 return base_bit;
16491 case ORDERED: case UNORDERED:
16492 return base_bit + 3;
16494 case GE: case GEU:
16495 /* If scc, we will have done a cror to put the bit in the
16496 unordered position. So test that bit. For integer, this is ! LT
16497 unless this is an scc insn. */
16498 return scc_p ? base_bit + 3 : base_bit;
16500 case LE: case LEU:
16501 return scc_p ? base_bit + 3 : base_bit + 1;
16503 default:
16504 gcc_unreachable ();
16508 /* Return the GOT register. */
16511 rs6000_got_register (rtx value ATTRIBUTE_UNUSED)
16513 /* The second flow pass currently (June 1999) can't update
16514 regs_ever_live without disturbing other parts of the compiler, so
16515 update it here to make the prolog/epilogue code happy. */
16516 if (!can_create_pseudo_p ()
16517 && !df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))
16518 df_set_regs_ever_live (RS6000_PIC_OFFSET_TABLE_REGNUM, true);
16520 crtl->uses_pic_offset_table = 1;
16522 return pic_offset_table_rtx;
16525 static rs6000_stack_t stack_info;
16527 /* Function to init struct machine_function.
16528 This will be called, via a pointer variable,
16529 from push_function_context. */
16531 static struct machine_function *
16532 rs6000_init_machine_status (void)
16534 stack_info.reload_completed = 0;
16535 return ggc_alloc_cleared_machine_function ();
16538 #define INT_P(X) (GET_CODE (X) == CONST_INT && GET_MODE (X) == VOIDmode)
16541 extract_MB (rtx op)
16543 int i;
16544 unsigned long val = INTVAL (op);
16546 /* If the high bit is zero, the value is the first 1 bit we find
16547 from the left. */
16548 if ((val & 0x80000000) == 0)
16550 gcc_assert (val & 0xffffffff);
16552 i = 1;
16553 while (((val <<= 1) & 0x80000000) == 0)
16554 ++i;
16555 return i;
16558 /* If the high bit is set and the low bit is not, or the mask is all
16559 1's, the value is zero. */
16560 if ((val & 1) == 0 || (val & 0xffffffff) == 0xffffffff)
16561 return 0;
16563 /* Otherwise we have a wrap-around mask. Look for the first 0 bit
16564 from the right. */
16565 i = 31;
16566 while (((val >>= 1) & 1) != 0)
16567 --i;
16569 return i;
16573 extract_ME (rtx op)
16575 int i;
16576 unsigned long val = INTVAL (op);
16578 /* If the low bit is zero, the value is the first 1 bit we find from
16579 the right. */
16580 if ((val & 1) == 0)
16582 gcc_assert (val & 0xffffffff);
16584 i = 30;
16585 while (((val >>= 1) & 1) == 0)
16586 --i;
16588 return i;
16591 /* If the low bit is set and the high bit is not, or the mask is all
16592 1's, the value is 31. */
16593 if ((val & 0x80000000) == 0 || (val & 0xffffffff) == 0xffffffff)
16594 return 31;
16596 /* Otherwise we have a wrap-around mask. Look for the first 0 bit
16597 from the left. */
16598 i = 0;
16599 while (((val <<= 1) & 0x80000000) != 0)
16600 ++i;
16602 return i;
16605 /* Locate some local-dynamic symbol still in use by this function
16606 so that we can print its name in some tls_ld pattern. */
16608 static const char *
16609 rs6000_get_some_local_dynamic_name (void)
16611 rtx insn;
16613 if (cfun->machine->some_ld_name)
16614 return cfun->machine->some_ld_name;
16616 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
16617 if (INSN_P (insn)
16618 && for_each_rtx (&PATTERN (insn),
16619 rs6000_get_some_local_dynamic_name_1, 0))
16620 return cfun->machine->some_ld_name;
16622 gcc_unreachable ();
16625 /* Helper function for rs6000_get_some_local_dynamic_name. */
16627 static int
16628 rs6000_get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
16630 rtx x = *px;
16632 if (GET_CODE (x) == SYMBOL_REF)
16634 const char *str = XSTR (x, 0);
16635 if (SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC)
16637 cfun->machine->some_ld_name = str;
16638 return 1;
16642 return 0;
16645 /* Write out a function code label. */
16647 void
16648 rs6000_output_function_entry (FILE *file, const char *fname)
16650 if (fname[0] != '.')
16652 switch (DEFAULT_ABI)
16654 default:
16655 gcc_unreachable ();
16657 case ABI_AIX:
16658 if (DOT_SYMBOLS)
16659 putc ('.', file);
16660 else
16661 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "L.");
16662 break;
16664 case ABI_V4:
16665 case ABI_DARWIN:
16666 break;
16670 RS6000_OUTPUT_BASENAME (file, fname);
16673 /* Print an operand. Recognize special options, documented below. */
16675 #if TARGET_ELF
16676 #define SMALL_DATA_RELOC ((rs6000_sdata == SDATA_EABI) ? "sda21" : "sdarel")
16677 #define SMALL_DATA_REG ((rs6000_sdata == SDATA_EABI) ? 0 : 13)
16678 #else
16679 #define SMALL_DATA_RELOC "sda21"
16680 #define SMALL_DATA_REG 0
16681 #endif
16683 void
16684 print_operand (FILE *file, rtx x, int code)
16686 int i;
16687 unsigned HOST_WIDE_INT uval;
16689 switch (code)
16691 /* %a is output_address. */
16693 case 'b':
16694 /* If constant, low-order 16 bits of constant, unsigned.
16695 Otherwise, write normally. */
16696 if (INT_P (x))
16697 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0xffff);
16698 else
16699 print_operand (file, x, 0);
16700 return;
16702 case 'B':
16703 /* If the low-order bit is zero, write 'r'; otherwise, write 'l'
16704 for 64-bit mask direction. */
16705 putc (((INTVAL (x) & 1) == 0 ? 'r' : 'l'), file);
16706 return;
16708 /* %c is output_addr_const if a CONSTANT_ADDRESS_P, otherwise
16709 output_operand. */
16711 case 'D':
16712 /* Like 'J' but get to the GT bit only. */
16713 gcc_assert (REG_P (x));
16715 /* Bit 1 is GT bit. */
16716 i = 4 * (REGNO (x) - CR0_REGNO) + 1;
16718 /* Add one for shift count in rlinm for scc. */
16719 fprintf (file, "%d", i + 1);
16720 return;
16722 case 'E':
16723 /* X is a CR register. Print the number of the EQ bit of the CR */
16724 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
16725 output_operand_lossage ("invalid %%E value");
16726 else
16727 fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO) + 2);
16728 return;
16730 case 'f':
16731 /* X is a CR register. Print the shift count needed to move it
16732 to the high-order four bits. */
16733 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
16734 output_operand_lossage ("invalid %%f value");
16735 else
16736 fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO));
16737 return;
16739 case 'F':
16740 /* Similar, but print the count for the rotate in the opposite
16741 direction. */
16742 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
16743 output_operand_lossage ("invalid %%F value");
16744 else
16745 fprintf (file, "%d", 32 - 4 * (REGNO (x) - CR0_REGNO));
16746 return;
16748 case 'G':
16749 /* X is a constant integer. If it is negative, print "m",
16750 otherwise print "z". This is to make an aze or ame insn. */
16751 if (GET_CODE (x) != CONST_INT)
16752 output_operand_lossage ("invalid %%G value");
16753 else if (INTVAL (x) >= 0)
16754 putc ('z', file);
16755 else
16756 putc ('m', file);
16757 return;
16759 case 'h':
16760 /* If constant, output low-order five bits. Otherwise, write
16761 normally. */
16762 if (INT_P (x))
16763 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 31);
16764 else
16765 print_operand (file, x, 0);
16766 return;
16768 case 'H':
16769 /* If constant, output low-order six bits. Otherwise, write
16770 normally. */
16771 if (INT_P (x))
16772 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 63);
16773 else
16774 print_operand (file, x, 0);
16775 return;
16777 case 'I':
16778 /* Print `i' if this is a constant, else nothing. */
16779 if (INT_P (x))
16780 putc ('i', file);
16781 return;
16783 case 'j':
16784 /* Write the bit number in CCR for jump. */
16785 i = ccr_bit (x, 0);
16786 if (i == -1)
16787 output_operand_lossage ("invalid %%j code");
16788 else
16789 fprintf (file, "%d", i);
16790 return;
16792 case 'J':
16793 /* Similar, but add one for shift count in rlinm for scc and pass
16794 scc flag to `ccr_bit'. */
16795 i = ccr_bit (x, 1);
16796 if (i == -1)
16797 output_operand_lossage ("invalid %%J code");
16798 else
16799 /* If we want bit 31, write a shift count of zero, not 32. */
16800 fprintf (file, "%d", i == 31 ? 0 : i + 1);
16801 return;
16803 case 'k':
16804 /* X must be a constant. Write the 1's complement of the
16805 constant. */
16806 if (! INT_P (x))
16807 output_operand_lossage ("invalid %%k value");
16808 else
16809 fprintf (file, HOST_WIDE_INT_PRINT_DEC, ~ INTVAL (x));
16810 return;
16812 case 'K':
16813 /* X must be a symbolic constant on ELF. Write an
16814 expression suitable for an 'addi' that adds in the low 16
16815 bits of the MEM. */
16816 if (GET_CODE (x) == CONST)
16818 if (GET_CODE (XEXP (x, 0)) != PLUS
16819 || (GET_CODE (XEXP (XEXP (x, 0), 0)) != SYMBOL_REF
16820 && GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
16821 || GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
16822 output_operand_lossage ("invalid %%K value");
16824 print_operand_address (file, x);
16825 fputs ("@l", file);
16826 return;
16828 /* %l is output_asm_label. */
16830 case 'L':
16831 /* Write second word of DImode or DFmode reference. Works on register
16832 or non-indexed memory only. */
16833 if (REG_P (x))
16834 fputs (reg_names[REGNO (x) + 1], file);
16835 else if (MEM_P (x))
16837 /* Handle possible auto-increment. Since it is pre-increment and
16838 we have already done it, we can just use an offset of word. */
16839 if (GET_CODE (XEXP (x, 0)) == PRE_INC
16840 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
16841 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0),
16842 UNITS_PER_WORD));
16843 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
16844 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0),
16845 UNITS_PER_WORD));
16846 else
16847 output_address (XEXP (adjust_address_nv (x, SImode,
16848 UNITS_PER_WORD),
16849 0));
16851 if (small_data_operand (x, GET_MODE (x)))
16852 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
16853 reg_names[SMALL_DATA_REG]);
16855 return;
16857 case 'm':
16858 /* MB value for a mask operand. */
16859 if (! mask_operand (x, SImode))
16860 output_operand_lossage ("invalid %%m value");
16862 fprintf (file, "%d", extract_MB (x));
16863 return;
16865 case 'M':
16866 /* ME value for a mask operand. */
16867 if (! mask_operand (x, SImode))
16868 output_operand_lossage ("invalid %%M value");
16870 fprintf (file, "%d", extract_ME (x));
16871 return;
16873 /* %n outputs the negative of its operand. */
16875 case 'N':
16876 /* Write the number of elements in the vector times 4. */
16877 if (GET_CODE (x) != PARALLEL)
16878 output_operand_lossage ("invalid %%N value");
16879 else
16880 fprintf (file, "%d", XVECLEN (x, 0) * 4);
16881 return;
16883 case 'O':
16884 /* Similar, but subtract 1 first. */
16885 if (GET_CODE (x) != PARALLEL)
16886 output_operand_lossage ("invalid %%O value");
16887 else
16888 fprintf (file, "%d", (XVECLEN (x, 0) - 1) * 4);
16889 return;
16891 case 'p':
16892 /* X is a CONST_INT that is a power of two. Output the logarithm. */
16893 if (! INT_P (x)
16894 || INTVAL (x) < 0
16895 || (i = exact_log2 (INTVAL (x))) < 0)
16896 output_operand_lossage ("invalid %%p value");
16897 else
16898 fprintf (file, "%d", i);
16899 return;
16901 case 'P':
16902 /* The operand must be an indirect memory reference. The result
16903 is the register name. */
16904 if (GET_CODE (x) != MEM || GET_CODE (XEXP (x, 0)) != REG
16905 || REGNO (XEXP (x, 0)) >= 32)
16906 output_operand_lossage ("invalid %%P value");
16907 else
16908 fputs (reg_names[REGNO (XEXP (x, 0))], file);
16909 return;
16911 case 'q':
16912 /* This outputs the logical code corresponding to a boolean
16913 expression. The expression may have one or both operands
16914 negated (if one, only the first one). For condition register
16915 logical operations, it will also treat the negated
16916 CR codes as NOTs, but not handle NOTs of them. */
16918 const char *const *t = 0;
16919 const char *s;
16920 enum rtx_code code = GET_CODE (x);
16921 static const char * const tbl[3][3] = {
16922 { "and", "andc", "nor" },
16923 { "or", "orc", "nand" },
16924 { "xor", "eqv", "xor" } };
16926 if (code == AND)
16927 t = tbl[0];
16928 else if (code == IOR)
16929 t = tbl[1];
16930 else if (code == XOR)
16931 t = tbl[2];
16932 else
16933 output_operand_lossage ("invalid %%q value");
16935 if (GET_CODE (XEXP (x, 0)) != NOT)
16936 s = t[0];
16937 else
16939 if (GET_CODE (XEXP (x, 1)) == NOT)
16940 s = t[2];
16941 else
16942 s = t[1];
16945 fputs (s, file);
16947 return;
16949 case 'Q':
16950 if (! TARGET_MFCRF)
16951 return;
16952 fputc (',', file);
16953 /* FALLTHRU */
16955 case 'R':
16956 /* X is a CR register. Print the mask for `mtcrf'. */
16957 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
16958 output_operand_lossage ("invalid %%R value");
16959 else
16960 fprintf (file, "%d", 128 >> (REGNO (x) - CR0_REGNO));
16961 return;
16963 case 's':
16964 /* Low 5 bits of 32 - value */
16965 if (! INT_P (x))
16966 output_operand_lossage ("invalid %%s value");
16967 else
16968 fprintf (file, HOST_WIDE_INT_PRINT_DEC, (32 - INTVAL (x)) & 31);
16969 return;
16971 case 'S':
16972 /* PowerPC64 mask position. All 0's is excluded.
16973 CONST_INT 32-bit mask is considered sign-extended so any
16974 transition must occur within the CONST_INT, not on the boundary. */
16975 if (! mask64_operand (x, DImode))
16976 output_operand_lossage ("invalid %%S value");
16978 uval = INTVAL (x);
16980 if (uval & 1) /* Clear Left */
16982 #if HOST_BITS_PER_WIDE_INT > 64
16983 uval &= ((unsigned HOST_WIDE_INT) 1 << 64) - 1;
16984 #endif
16985 i = 64;
16987 else /* Clear Right */
16989 uval = ~uval;
16990 #if HOST_BITS_PER_WIDE_INT > 64
16991 uval &= ((unsigned HOST_WIDE_INT) 1 << 64) - 1;
16992 #endif
16993 i = 63;
16995 while (uval != 0)
16996 --i, uval >>= 1;
16997 gcc_assert (i >= 0);
16998 fprintf (file, "%d", i);
16999 return;
17001 case 't':
17002 /* Like 'J' but get to the OVERFLOW/UNORDERED bit. */
17003 gcc_assert (REG_P (x) && GET_MODE (x) == CCmode);
17005 /* Bit 3 is OV bit. */
17006 i = 4 * (REGNO (x) - CR0_REGNO) + 3;
17008 /* If we want bit 31, write a shift count of zero, not 32. */
17009 fprintf (file, "%d", i == 31 ? 0 : i + 1);
17010 return;
17012 case 'T':
17013 /* Print the symbolic name of a branch target register. */
17014 if (GET_CODE (x) != REG || (REGNO (x) != LR_REGNO
17015 && REGNO (x) != CTR_REGNO))
17016 output_operand_lossage ("invalid %%T value");
17017 else if (REGNO (x) == LR_REGNO)
17018 fputs ("lr", file);
17019 else
17020 fputs ("ctr", file);
17021 return;
17023 case 'u':
17024 /* High-order 16 bits of constant for use in unsigned operand. */
17025 if (! INT_P (x))
17026 output_operand_lossage ("invalid %%u value");
17027 else
17028 fprintf (file, HOST_WIDE_INT_PRINT_HEX,
17029 (INTVAL (x) >> 16) & 0xffff);
17030 return;
17032 case 'v':
17033 /* High-order 16 bits of constant for use in signed operand. */
17034 if (! INT_P (x))
17035 output_operand_lossage ("invalid %%v value");
17036 else
17037 fprintf (file, HOST_WIDE_INT_PRINT_HEX,
17038 (INTVAL (x) >> 16) & 0xffff);
17039 return;
17041 case 'U':
17042 /* Print `u' if this has an auto-increment or auto-decrement. */
17043 if (MEM_P (x)
17044 && (GET_CODE (XEXP (x, 0)) == PRE_INC
17045 || GET_CODE (XEXP (x, 0)) == PRE_DEC
17046 || GET_CODE (XEXP (x, 0)) == PRE_MODIFY))
17047 putc ('u', file);
17048 return;
17050 case 'V':
17051 /* Print the trap code for this operand. */
17052 switch (GET_CODE (x))
17054 case EQ:
17055 fputs ("eq", file); /* 4 */
17056 break;
17057 case NE:
17058 fputs ("ne", file); /* 24 */
17059 break;
17060 case LT:
17061 fputs ("lt", file); /* 16 */
17062 break;
17063 case LE:
17064 fputs ("le", file); /* 20 */
17065 break;
17066 case GT:
17067 fputs ("gt", file); /* 8 */
17068 break;
17069 case GE:
17070 fputs ("ge", file); /* 12 */
17071 break;
17072 case LTU:
17073 fputs ("llt", file); /* 2 */
17074 break;
17075 case LEU:
17076 fputs ("lle", file); /* 6 */
17077 break;
17078 case GTU:
17079 fputs ("lgt", file); /* 1 */
17080 break;
17081 case GEU:
17082 fputs ("lge", file); /* 5 */
17083 break;
17084 default:
17085 gcc_unreachable ();
17087 break;
17089 case 'w':
17090 /* If constant, low-order 16 bits of constant, signed. Otherwise, write
17091 normally. */
17092 if (INT_P (x))
17093 fprintf (file, HOST_WIDE_INT_PRINT_DEC,
17094 ((INTVAL (x) & 0xffff) ^ 0x8000) - 0x8000);
17095 else
17096 print_operand (file, x, 0);
17097 return;
17099 case 'W':
17100 /* MB value for a PowerPC64 rldic operand. */
17101 i = clz_hwi (INTVAL (x));
17103 fprintf (file, "%d", i);
17104 return;
17106 case 'x':
17107 /* X is a FPR or Altivec register used in a VSX context. */
17108 if (GET_CODE (x) != REG || !VSX_REGNO_P (REGNO (x)))
17109 output_operand_lossage ("invalid %%x value");
17110 else
17112 int reg = REGNO (x);
17113 int vsx_reg = (FP_REGNO_P (reg)
17114 ? reg - 32
17115 : reg - FIRST_ALTIVEC_REGNO + 32);
17117 #ifdef TARGET_REGNAMES
17118 if (TARGET_REGNAMES)
17119 fprintf (file, "%%vs%d", vsx_reg);
17120 else
17121 #endif
17122 fprintf (file, "%d", vsx_reg);
17124 return;
17126 case 'X':
17127 if (MEM_P (x)
17128 && (legitimate_indexed_address_p (XEXP (x, 0), 0)
17129 || (GET_CODE (XEXP (x, 0)) == PRE_MODIFY
17130 && legitimate_indexed_address_p (XEXP (XEXP (x, 0), 1), 0))))
17131 putc ('x', file);
17132 return;
17134 case 'Y':
17135 /* Like 'L', for third word of TImode/PTImode */
17136 if (REG_P (x))
17137 fputs (reg_names[REGNO (x) + 2], file);
17138 else if (MEM_P (x))
17140 if (GET_CODE (XEXP (x, 0)) == PRE_INC
17141 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
17142 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 8));
17143 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
17144 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 8));
17145 else
17146 output_address (XEXP (adjust_address_nv (x, SImode, 8), 0));
17147 if (small_data_operand (x, GET_MODE (x)))
17148 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
17149 reg_names[SMALL_DATA_REG]);
17151 return;
17153 case 'z':
17154 /* X is a SYMBOL_REF. Write out the name preceded by a
17155 period and without any trailing data in brackets. Used for function
17156 names. If we are configured for System V (or the embedded ABI) on
17157 the PowerPC, do not emit the period, since those systems do not use
17158 TOCs and the like. */
17159 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17161 /* For macho, check to see if we need a stub. */
17162 if (TARGET_MACHO)
17164 const char *name = XSTR (x, 0);
17165 #if TARGET_MACHO
17166 if (darwin_emit_branch_islands
17167 && MACHOPIC_INDIRECT
17168 && machopic_classify_symbol (x) == MACHOPIC_UNDEFINED_FUNCTION)
17169 name = machopic_indirection_name (x, /*stub_p=*/true);
17170 #endif
17171 assemble_name (file, name);
17173 else if (!DOT_SYMBOLS)
17174 assemble_name (file, XSTR (x, 0));
17175 else
17176 rs6000_output_function_entry (file, XSTR (x, 0));
17177 return;
17179 case 'Z':
17180 /* Like 'L', for last word of TImode/PTImode. */
17181 if (REG_P (x))
17182 fputs (reg_names[REGNO (x) + 3], file);
17183 else if (MEM_P (x))
17185 if (GET_CODE (XEXP (x, 0)) == PRE_INC
17186 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
17187 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 12));
17188 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
17189 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 12));
17190 else
17191 output_address (XEXP (adjust_address_nv (x, SImode, 12), 0));
17192 if (small_data_operand (x, GET_MODE (x)))
17193 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
17194 reg_names[SMALL_DATA_REG]);
17196 return;
17198 /* Print AltiVec or SPE memory operand. */
17199 case 'y':
17201 rtx tmp;
17203 gcc_assert (MEM_P (x));
17205 tmp = XEXP (x, 0);
17207 /* Ugly hack because %y is overloaded. */
17208 if ((TARGET_SPE || TARGET_E500_DOUBLE)
17209 && (GET_MODE_SIZE (GET_MODE (x)) == 8
17210 || GET_MODE (x) == TFmode
17211 || GET_MODE (x) == TImode
17212 || GET_MODE (x) == PTImode))
17214 /* Handle [reg]. */
17215 if (REG_P (tmp))
17217 fprintf (file, "0(%s)", reg_names[REGNO (tmp)]);
17218 break;
17220 /* Handle [reg+UIMM]. */
17221 else if (GET_CODE (tmp) == PLUS &&
17222 GET_CODE (XEXP (tmp, 1)) == CONST_INT)
17224 int x;
17226 gcc_assert (REG_P (XEXP (tmp, 0)));
17228 x = INTVAL (XEXP (tmp, 1));
17229 fprintf (file, "%d(%s)", x, reg_names[REGNO (XEXP (tmp, 0))]);
17230 break;
17233 /* Fall through. Must be [reg+reg]. */
17235 if (VECTOR_MEM_ALTIVEC_P (GET_MODE (x))
17236 && GET_CODE (tmp) == AND
17237 && GET_CODE (XEXP (tmp, 1)) == CONST_INT
17238 && INTVAL (XEXP (tmp, 1)) == -16)
17239 tmp = XEXP (tmp, 0);
17240 else if (VECTOR_MEM_VSX_P (GET_MODE (x))
17241 && GET_CODE (tmp) == PRE_MODIFY)
17242 tmp = XEXP (tmp, 1);
17243 if (REG_P (tmp))
17244 fprintf (file, "0,%s", reg_names[REGNO (tmp)]);
17245 else
17247 if (!GET_CODE (tmp) == PLUS
17248 || !REG_P (XEXP (tmp, 0))
17249 || !REG_P (XEXP (tmp, 1)))
17251 output_operand_lossage ("invalid %%y value, try using the 'Z' constraint");
17252 break;
17255 if (REGNO (XEXP (tmp, 0)) == 0)
17256 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 1)) ],
17257 reg_names[ REGNO (XEXP (tmp, 0)) ]);
17258 else
17259 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 0)) ],
17260 reg_names[ REGNO (XEXP (tmp, 1)) ]);
17262 break;
17265 case 0:
17266 if (REG_P (x))
17267 fprintf (file, "%s", reg_names[REGNO (x)]);
17268 else if (MEM_P (x))
17270 /* We need to handle PRE_INC and PRE_DEC here, since we need to
17271 know the width from the mode. */
17272 if (GET_CODE (XEXP (x, 0)) == PRE_INC)
17273 fprintf (file, "%d(%s)", GET_MODE_SIZE (GET_MODE (x)),
17274 reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
17275 else if (GET_CODE (XEXP (x, 0)) == PRE_DEC)
17276 fprintf (file, "%d(%s)", - GET_MODE_SIZE (GET_MODE (x)),
17277 reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
17278 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
17279 output_address (XEXP (XEXP (x, 0), 1));
17280 else
17281 output_address (XEXP (x, 0));
17283 else
17285 if (toc_relative_expr_p (x, false))
17286 /* This hack along with a corresponding hack in
17287 rs6000_output_addr_const_extra arranges to output addends
17288 where the assembler expects to find them. eg.
17289 (plus (unspec [(symbol_ref ("x")) (reg 2)] tocrel) 4)
17290 without this hack would be output as "x@toc+4". We
17291 want "x+4@toc". */
17292 output_addr_const (file, CONST_CAST_RTX (tocrel_base));
17293 else
17294 output_addr_const (file, x);
17296 return;
17298 case '&':
17299 assemble_name (file, rs6000_get_some_local_dynamic_name ());
17300 return;
17302 default:
17303 output_operand_lossage ("invalid %%xn code");
17307 /* Print the address of an operand. */
17309 void
17310 print_operand_address (FILE *file, rtx x)
17312 if (REG_P (x))
17313 fprintf (file, "0(%s)", reg_names[ REGNO (x) ]);
17314 else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST
17315 || GET_CODE (x) == LABEL_REF)
17317 output_addr_const (file, x);
17318 if (small_data_operand (x, GET_MODE (x)))
17319 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
17320 reg_names[SMALL_DATA_REG]);
17321 else
17322 gcc_assert (!TARGET_TOC);
17324 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
17325 && REG_P (XEXP (x, 1)))
17327 if (REGNO (XEXP (x, 0)) == 0)
17328 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 1)) ],
17329 reg_names[ REGNO (XEXP (x, 0)) ]);
17330 else
17331 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 0)) ],
17332 reg_names[ REGNO (XEXP (x, 1)) ]);
17334 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
17335 && GET_CODE (XEXP (x, 1)) == CONST_INT)
17336 fprintf (file, HOST_WIDE_INT_PRINT_DEC "(%s)",
17337 INTVAL (XEXP (x, 1)), reg_names[ REGNO (XEXP (x, 0)) ]);
17338 #if TARGET_MACHO
17339 else if (GET_CODE (x) == LO_SUM && REG_P (XEXP (x, 0))
17340 && CONSTANT_P (XEXP (x, 1)))
17342 fprintf (file, "lo16(");
17343 output_addr_const (file, XEXP (x, 1));
17344 fprintf (file, ")(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
17346 #endif
17347 #if TARGET_ELF
17348 else if (GET_CODE (x) == LO_SUM && REG_P (XEXP (x, 0))
17349 && CONSTANT_P (XEXP (x, 1)))
17351 output_addr_const (file, XEXP (x, 1));
17352 fprintf (file, "@l(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
17354 #endif
17355 else if (toc_relative_expr_p (x, false))
17357 /* This hack along with a corresponding hack in
17358 rs6000_output_addr_const_extra arranges to output addends
17359 where the assembler expects to find them. eg.
17360 (lo_sum (reg 9)
17361 . (plus (unspec [(symbol_ref ("x")) (reg 2)] tocrel) 8))
17362 without this hack would be output as "x@toc+8@l(9)". We
17363 want "x+8@toc@l(9)". */
17364 output_addr_const (file, CONST_CAST_RTX (tocrel_base));
17365 if (GET_CODE (x) == LO_SUM)
17366 fprintf (file, "@l(%s)", reg_names[REGNO (XEXP (x, 0))]);
17367 else
17368 fprintf (file, "(%s)", reg_names[REGNO (XVECEXP (tocrel_base, 0, 1))]);
17370 else
17371 gcc_unreachable ();
17374 /* Implement TARGET_OUTPUT_ADDR_CONST_EXTRA. */
17376 static bool
17377 rs6000_output_addr_const_extra (FILE *file, rtx x)
17379 if (GET_CODE (x) == UNSPEC)
17380 switch (XINT (x, 1))
17382 case UNSPEC_TOCREL:
17383 gcc_checking_assert (GET_CODE (XVECEXP (x, 0, 0)) == SYMBOL_REF
17384 && REG_P (XVECEXP (x, 0, 1))
17385 && REGNO (XVECEXP (x, 0, 1)) == TOC_REGISTER);
17386 output_addr_const (file, XVECEXP (x, 0, 0));
17387 if (x == tocrel_base && tocrel_offset != const0_rtx)
17389 if (INTVAL (tocrel_offset) >= 0)
17390 fprintf (file, "+");
17391 output_addr_const (file, CONST_CAST_RTX (tocrel_offset));
17393 if (!TARGET_AIX || (TARGET_ELF && TARGET_MINIMAL_TOC))
17395 putc ('-', file);
17396 assemble_name (file, toc_label_name);
17398 else if (TARGET_ELF)
17399 fputs ("@toc", file);
17400 return true;
17402 #if TARGET_MACHO
17403 case UNSPEC_MACHOPIC_OFFSET:
17404 output_addr_const (file, XVECEXP (x, 0, 0));
17405 putc ('-', file);
17406 machopic_output_function_base_name (file);
17407 return true;
17408 #endif
17410 return false;
17413 /* Target hook for assembling integer objects. The PowerPC version has
17414 to handle fixup entries for relocatable code if RELOCATABLE_NEEDS_FIXUP
17415 is defined. It also needs to handle DI-mode objects on 64-bit
17416 targets. */
17418 static bool
17419 rs6000_assemble_integer (rtx x, unsigned int size, int aligned_p)
17421 #ifdef RELOCATABLE_NEEDS_FIXUP
17422 /* Special handling for SI values. */
17423 if (RELOCATABLE_NEEDS_FIXUP && size == 4 && aligned_p)
17425 static int recurse = 0;
17427 /* For -mrelocatable, we mark all addresses that need to be fixed up in
17428 the .fixup section. Since the TOC section is already relocated, we
17429 don't need to mark it here. We used to skip the text section, but it
17430 should never be valid for relocated addresses to be placed in the text
17431 section. */
17432 if (TARGET_RELOCATABLE
17433 && in_section != toc_section
17434 && !recurse
17435 && GET_CODE (x) != CONST_INT
17436 && GET_CODE (x) != CONST_DOUBLE
17437 && CONSTANT_P (x))
17439 char buf[256];
17441 recurse = 1;
17442 ASM_GENERATE_INTERNAL_LABEL (buf, "LCP", fixuplabelno);
17443 fixuplabelno++;
17444 ASM_OUTPUT_LABEL (asm_out_file, buf);
17445 fprintf (asm_out_file, "\t.long\t(");
17446 output_addr_const (asm_out_file, x);
17447 fprintf (asm_out_file, ")@fixup\n");
17448 fprintf (asm_out_file, "\t.section\t\".fixup\",\"aw\"\n");
17449 ASM_OUTPUT_ALIGN (asm_out_file, 2);
17450 fprintf (asm_out_file, "\t.long\t");
17451 assemble_name (asm_out_file, buf);
17452 fprintf (asm_out_file, "\n\t.previous\n");
17453 recurse = 0;
17454 return true;
17456 /* Remove initial .'s to turn a -mcall-aixdesc function
17457 address into the address of the descriptor, not the function
17458 itself. */
17459 else if (GET_CODE (x) == SYMBOL_REF
17460 && XSTR (x, 0)[0] == '.'
17461 && DEFAULT_ABI == ABI_AIX)
17463 const char *name = XSTR (x, 0);
17464 while (*name == '.')
17465 name++;
17467 fprintf (asm_out_file, "\t.long\t%s\n", name);
17468 return true;
17471 #endif /* RELOCATABLE_NEEDS_FIXUP */
17472 return default_assemble_integer (x, size, aligned_p);
17475 #if defined (HAVE_GAS_HIDDEN) && !TARGET_MACHO
17476 /* Emit an assembler directive to set symbol visibility for DECL to
17477 VISIBILITY_TYPE. */
17479 static void
17480 rs6000_assemble_visibility (tree decl, int vis)
17482 if (TARGET_XCOFF)
17483 return;
17485 /* Functions need to have their entry point symbol visibility set as
17486 well as their descriptor symbol visibility. */
17487 if (DEFAULT_ABI == ABI_AIX
17488 && DOT_SYMBOLS
17489 && TREE_CODE (decl) == FUNCTION_DECL)
17491 static const char * const visibility_types[] = {
17492 NULL, "internal", "hidden", "protected"
17495 const char *name, *type;
17497 name = ((* targetm.strip_name_encoding)
17498 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
17499 type = visibility_types[vis];
17501 fprintf (asm_out_file, "\t.%s\t%s\n", type, name);
17502 fprintf (asm_out_file, "\t.%s\t.%s\n", type, name);
17504 else
17505 default_assemble_visibility (decl, vis);
17507 #endif
17509 enum rtx_code
17510 rs6000_reverse_condition (enum machine_mode mode, enum rtx_code code)
17512 /* Reversal of FP compares takes care -- an ordered compare
17513 becomes an unordered compare and vice versa. */
17514 if (mode == CCFPmode
17515 && (!flag_finite_math_only
17516 || code == UNLT || code == UNLE || code == UNGT || code == UNGE
17517 || code == UNEQ || code == LTGT))
17518 return reverse_condition_maybe_unordered (code);
17519 else
17520 return reverse_condition (code);
17523 /* Generate a compare for CODE. Return a brand-new rtx that
17524 represents the result of the compare. */
17526 static rtx
17527 rs6000_generate_compare (rtx cmp, enum machine_mode mode)
17529 enum machine_mode comp_mode;
17530 rtx compare_result;
17531 enum rtx_code code = GET_CODE (cmp);
17532 rtx op0 = XEXP (cmp, 0);
17533 rtx op1 = XEXP (cmp, 1);
17535 if (FLOAT_MODE_P (mode))
17536 comp_mode = CCFPmode;
17537 else if (code == GTU || code == LTU
17538 || code == GEU || code == LEU)
17539 comp_mode = CCUNSmode;
17540 else if ((code == EQ || code == NE)
17541 && unsigned_reg_p (op0)
17542 && (unsigned_reg_p (op1)
17543 || (CONST_INT_P (op1) && INTVAL (op1) != 0)))
17544 /* These are unsigned values, perhaps there will be a later
17545 ordering compare that can be shared with this one. */
17546 comp_mode = CCUNSmode;
17547 else
17548 comp_mode = CCmode;
17550 /* If we have an unsigned compare, make sure we don't have a signed value as
17551 an immediate. */
17552 if (comp_mode == CCUNSmode && GET_CODE (op1) == CONST_INT
17553 && INTVAL (op1) < 0)
17555 op0 = copy_rtx_if_shared (op0);
17556 op1 = force_reg (GET_MODE (op0), op1);
17557 cmp = gen_rtx_fmt_ee (code, GET_MODE (cmp), op0, op1);
17560 /* First, the compare. */
17561 compare_result = gen_reg_rtx (comp_mode);
17563 /* E500 FP compare instructions on the GPRs. Yuck! */
17564 if ((!TARGET_FPRS && TARGET_HARD_FLOAT)
17565 && FLOAT_MODE_P (mode))
17567 rtx cmp, or_result, compare_result2;
17568 enum machine_mode op_mode = GET_MODE (op0);
17569 bool reverse_p;
17571 if (op_mode == VOIDmode)
17572 op_mode = GET_MODE (op1);
17574 /* First reverse the condition codes that aren't directly supported. */
17575 switch (code)
17577 case NE:
17578 case UNLT:
17579 case UNLE:
17580 case UNGT:
17581 case UNGE:
17582 code = reverse_condition_maybe_unordered (code);
17583 reverse_p = true;
17584 break;
17586 case EQ:
17587 case LT:
17588 case LE:
17589 case GT:
17590 case GE:
17591 reverse_p = false;
17592 break;
17594 default:
17595 gcc_unreachable ();
17598 /* The E500 FP compare instructions toggle the GT bit (CR bit 1) only.
17599 This explains the following mess. */
17601 switch (code)
17603 case EQ:
17604 switch (op_mode)
17606 case SFmode:
17607 cmp = (flag_finite_math_only && !flag_trapping_math)
17608 ? gen_tstsfeq_gpr (compare_result, op0, op1)
17609 : gen_cmpsfeq_gpr (compare_result, op0, op1);
17610 break;
17612 case DFmode:
17613 cmp = (flag_finite_math_only && !flag_trapping_math)
17614 ? gen_tstdfeq_gpr (compare_result, op0, op1)
17615 : gen_cmpdfeq_gpr (compare_result, op0, op1);
17616 break;
17618 case TFmode:
17619 cmp = (flag_finite_math_only && !flag_trapping_math)
17620 ? gen_tsttfeq_gpr (compare_result, op0, op1)
17621 : gen_cmptfeq_gpr (compare_result, op0, op1);
17622 break;
17624 default:
17625 gcc_unreachable ();
17627 break;
17629 case GT:
17630 case GE:
17631 switch (op_mode)
17633 case SFmode:
17634 cmp = (flag_finite_math_only && !flag_trapping_math)
17635 ? gen_tstsfgt_gpr (compare_result, op0, op1)
17636 : gen_cmpsfgt_gpr (compare_result, op0, op1);
17637 break;
17639 case DFmode:
17640 cmp = (flag_finite_math_only && !flag_trapping_math)
17641 ? gen_tstdfgt_gpr (compare_result, op0, op1)
17642 : gen_cmpdfgt_gpr (compare_result, op0, op1);
17643 break;
17645 case TFmode:
17646 cmp = (flag_finite_math_only && !flag_trapping_math)
17647 ? gen_tsttfgt_gpr (compare_result, op0, op1)
17648 : gen_cmptfgt_gpr (compare_result, op0, op1);
17649 break;
17651 default:
17652 gcc_unreachable ();
17654 break;
17656 case LT:
17657 case LE:
17658 switch (op_mode)
17660 case SFmode:
17661 cmp = (flag_finite_math_only && !flag_trapping_math)
17662 ? gen_tstsflt_gpr (compare_result, op0, op1)
17663 : gen_cmpsflt_gpr (compare_result, op0, op1);
17664 break;
17666 case DFmode:
17667 cmp = (flag_finite_math_only && !flag_trapping_math)
17668 ? gen_tstdflt_gpr (compare_result, op0, op1)
17669 : gen_cmpdflt_gpr (compare_result, op0, op1);
17670 break;
17672 case TFmode:
17673 cmp = (flag_finite_math_only && !flag_trapping_math)
17674 ? gen_tsttflt_gpr (compare_result, op0, op1)
17675 : gen_cmptflt_gpr (compare_result, op0, op1);
17676 break;
17678 default:
17679 gcc_unreachable ();
17681 break;
17683 default:
17684 gcc_unreachable ();
17687 /* Synthesize LE and GE from LT/GT || EQ. */
17688 if (code == LE || code == GE)
17690 emit_insn (cmp);
17692 compare_result2 = gen_reg_rtx (CCFPmode);
17694 /* Do the EQ. */
17695 switch (op_mode)
17697 case SFmode:
17698 cmp = (flag_finite_math_only && !flag_trapping_math)
17699 ? gen_tstsfeq_gpr (compare_result2, op0, op1)
17700 : gen_cmpsfeq_gpr (compare_result2, op0, op1);
17701 break;
17703 case DFmode:
17704 cmp = (flag_finite_math_only && !flag_trapping_math)
17705 ? gen_tstdfeq_gpr (compare_result2, op0, op1)
17706 : gen_cmpdfeq_gpr (compare_result2, op0, op1);
17707 break;
17709 case TFmode:
17710 cmp = (flag_finite_math_only && !flag_trapping_math)
17711 ? gen_tsttfeq_gpr (compare_result2, op0, op1)
17712 : gen_cmptfeq_gpr (compare_result2, op0, op1);
17713 break;
17715 default:
17716 gcc_unreachable ();
17719 emit_insn (cmp);
17721 /* OR them together. */
17722 or_result = gen_reg_rtx (CCFPmode);
17723 cmp = gen_e500_cr_ior_compare (or_result, compare_result,
17724 compare_result2);
17725 compare_result = or_result;
17728 code = reverse_p ? NE : EQ;
17730 emit_insn (cmp);
17732 else
17734 /* Generate XLC-compatible TFmode compare as PARALLEL with extra
17735 CLOBBERs to match cmptf_internal2 pattern. */
17736 if (comp_mode == CCFPmode && TARGET_XL_COMPAT
17737 && GET_MODE (op0) == TFmode
17738 && !TARGET_IEEEQUAD
17739 && TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_LONG_DOUBLE_128)
17740 emit_insn (gen_rtx_PARALLEL (VOIDmode,
17741 gen_rtvec (10,
17742 gen_rtx_SET (VOIDmode,
17743 compare_result,
17744 gen_rtx_COMPARE (comp_mode, op0, op1)),
17745 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
17746 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
17747 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
17748 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
17749 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
17750 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
17751 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
17752 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
17753 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (Pmode)))));
17754 else if (GET_CODE (op1) == UNSPEC
17755 && XINT (op1, 1) == UNSPEC_SP_TEST)
17757 rtx op1b = XVECEXP (op1, 0, 0);
17758 comp_mode = CCEQmode;
17759 compare_result = gen_reg_rtx (CCEQmode);
17760 if (TARGET_64BIT)
17761 emit_insn (gen_stack_protect_testdi (compare_result, op0, op1b));
17762 else
17763 emit_insn (gen_stack_protect_testsi (compare_result, op0, op1b));
17765 else
17766 emit_insn (gen_rtx_SET (VOIDmode, compare_result,
17767 gen_rtx_COMPARE (comp_mode, op0, op1)));
17770 /* Some kinds of FP comparisons need an OR operation;
17771 under flag_finite_math_only we don't bother. */
17772 if (FLOAT_MODE_P (mode)
17773 && !flag_finite_math_only
17774 && !(TARGET_HARD_FLOAT && !TARGET_FPRS)
17775 && (code == LE || code == GE
17776 || code == UNEQ || code == LTGT
17777 || code == UNGT || code == UNLT))
17779 enum rtx_code or1, or2;
17780 rtx or1_rtx, or2_rtx, compare2_rtx;
17781 rtx or_result = gen_reg_rtx (CCEQmode);
17783 switch (code)
17785 case LE: or1 = LT; or2 = EQ; break;
17786 case GE: or1 = GT; or2 = EQ; break;
17787 case UNEQ: or1 = UNORDERED; or2 = EQ; break;
17788 case LTGT: or1 = LT; or2 = GT; break;
17789 case UNGT: or1 = UNORDERED; or2 = GT; break;
17790 case UNLT: or1 = UNORDERED; or2 = LT; break;
17791 default: gcc_unreachable ();
17793 validate_condition_mode (or1, comp_mode);
17794 validate_condition_mode (or2, comp_mode);
17795 or1_rtx = gen_rtx_fmt_ee (or1, SImode, compare_result, const0_rtx);
17796 or2_rtx = gen_rtx_fmt_ee (or2, SImode, compare_result, const0_rtx);
17797 compare2_rtx = gen_rtx_COMPARE (CCEQmode,
17798 gen_rtx_IOR (SImode, or1_rtx, or2_rtx),
17799 const_true_rtx);
17800 emit_insn (gen_rtx_SET (VOIDmode, or_result, compare2_rtx));
17802 compare_result = or_result;
17803 code = EQ;
17806 validate_condition_mode (code, GET_MODE (compare_result));
17808 return gen_rtx_fmt_ee (code, VOIDmode, compare_result, const0_rtx);
17812 /* Emit the RTL for an sISEL pattern. */
17814 void
17815 rs6000_emit_sISEL (enum machine_mode mode ATTRIBUTE_UNUSED, rtx operands[])
17817 rs6000_emit_int_cmove (operands[0], operands[1], const1_rtx, const0_rtx);
17820 void
17821 rs6000_emit_sCOND (enum machine_mode mode, rtx operands[])
17823 rtx condition_rtx;
17824 enum machine_mode op_mode;
17825 enum rtx_code cond_code;
17826 rtx result = operands[0];
17828 if (TARGET_ISEL && (mode == SImode || mode == DImode))
17830 rs6000_emit_sISEL (mode, operands);
17831 return;
17834 condition_rtx = rs6000_generate_compare (operands[1], mode);
17835 cond_code = GET_CODE (condition_rtx);
17837 if (FLOAT_MODE_P (mode)
17838 && !TARGET_FPRS && TARGET_HARD_FLOAT)
17840 rtx t;
17842 PUT_MODE (condition_rtx, SImode);
17843 t = XEXP (condition_rtx, 0);
17845 gcc_assert (cond_code == NE || cond_code == EQ);
17847 if (cond_code == NE)
17848 emit_insn (gen_e500_flip_gt_bit (t, t));
17850 emit_insn (gen_move_from_CR_gt_bit (result, t));
17851 return;
17854 if (cond_code == NE
17855 || cond_code == GE || cond_code == LE
17856 || cond_code == GEU || cond_code == LEU
17857 || cond_code == ORDERED || cond_code == UNGE || cond_code == UNLE)
17859 rtx not_result = gen_reg_rtx (CCEQmode);
17860 rtx not_op, rev_cond_rtx;
17861 enum machine_mode cc_mode;
17863 cc_mode = GET_MODE (XEXP (condition_rtx, 0));
17865 rev_cond_rtx = gen_rtx_fmt_ee (rs6000_reverse_condition (cc_mode, cond_code),
17866 SImode, XEXP (condition_rtx, 0), const0_rtx);
17867 not_op = gen_rtx_COMPARE (CCEQmode, rev_cond_rtx, const0_rtx);
17868 emit_insn (gen_rtx_SET (VOIDmode, not_result, not_op));
17869 condition_rtx = gen_rtx_EQ (VOIDmode, not_result, const0_rtx);
17872 op_mode = GET_MODE (XEXP (operands[1], 0));
17873 if (op_mode == VOIDmode)
17874 op_mode = GET_MODE (XEXP (operands[1], 1));
17876 if (TARGET_POWERPC64 && (op_mode == DImode || FLOAT_MODE_P (mode)))
17878 PUT_MODE (condition_rtx, DImode);
17879 convert_move (result, condition_rtx, 0);
17881 else
17883 PUT_MODE (condition_rtx, SImode);
17884 emit_insn (gen_rtx_SET (VOIDmode, result, condition_rtx));
17888 /* Emit a branch of kind CODE to location LOC. */
17890 void
17891 rs6000_emit_cbranch (enum machine_mode mode, rtx operands[])
17893 rtx condition_rtx, loc_ref;
17895 condition_rtx = rs6000_generate_compare (operands[0], mode);
17896 loc_ref = gen_rtx_LABEL_REF (VOIDmode, operands[3]);
17897 emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx,
17898 gen_rtx_IF_THEN_ELSE (VOIDmode, condition_rtx,
17899 loc_ref, pc_rtx)));
17902 /* Return the string to output a conditional branch to LABEL, which is
17903 the operand template of the label, or NULL if the branch is really a
17904 conditional return.
17906 OP is the conditional expression. XEXP (OP, 0) is assumed to be a
17907 condition code register and its mode specifies what kind of
17908 comparison we made.
17910 REVERSED is nonzero if we should reverse the sense of the comparison.
17912 INSN is the insn. */
17914 char *
17915 output_cbranch (rtx op, const char *label, int reversed, rtx insn)
17917 static char string[64];
17918 enum rtx_code code = GET_CODE (op);
17919 rtx cc_reg = XEXP (op, 0);
17920 enum machine_mode mode = GET_MODE (cc_reg);
17921 int cc_regno = REGNO (cc_reg) - CR0_REGNO;
17922 int need_longbranch = label != NULL && get_attr_length (insn) == 8;
17923 int really_reversed = reversed ^ need_longbranch;
17924 char *s = string;
17925 const char *ccode;
17926 const char *pred;
17927 rtx note;
17929 validate_condition_mode (code, mode);
17931 /* Work out which way this really branches. We could use
17932 reverse_condition_maybe_unordered here always but this
17933 makes the resulting assembler clearer. */
17934 if (really_reversed)
17936 /* Reversal of FP compares takes care -- an ordered compare
17937 becomes an unordered compare and vice versa. */
17938 if (mode == CCFPmode)
17939 code = reverse_condition_maybe_unordered (code);
17940 else
17941 code = reverse_condition (code);
17944 if ((!TARGET_FPRS && TARGET_HARD_FLOAT) && mode == CCFPmode)
17946 /* The efscmp/tst* instructions twiddle bit 2, which maps nicely
17947 to the GT bit. */
17948 switch (code)
17950 case EQ:
17951 /* Opposite of GT. */
17952 code = GT;
17953 break;
17955 case NE:
17956 code = UNLE;
17957 break;
17959 default:
17960 gcc_unreachable ();
17964 switch (code)
17966 /* Not all of these are actually distinct opcodes, but
17967 we distinguish them for clarity of the resulting assembler. */
17968 case NE: case LTGT:
17969 ccode = "ne"; break;
17970 case EQ: case UNEQ:
17971 ccode = "eq"; break;
17972 case GE: case GEU:
17973 ccode = "ge"; break;
17974 case GT: case GTU: case UNGT:
17975 ccode = "gt"; break;
17976 case LE: case LEU:
17977 ccode = "le"; break;
17978 case LT: case LTU: case UNLT:
17979 ccode = "lt"; break;
17980 case UNORDERED: ccode = "un"; break;
17981 case ORDERED: ccode = "nu"; break;
17982 case UNGE: ccode = "nl"; break;
17983 case UNLE: ccode = "ng"; break;
17984 default:
17985 gcc_unreachable ();
17988 /* Maybe we have a guess as to how likely the branch is. */
17989 pred = "";
17990 note = find_reg_note (insn, REG_BR_PROB, NULL_RTX);
17991 if (note != NULL_RTX)
17993 /* PROB is the difference from 50%. */
17994 int prob = XINT (note, 0) - REG_BR_PROB_BASE / 2;
17996 /* Only hint for highly probable/improbable branches on newer
17997 cpus as static prediction overrides processor dynamic
17998 prediction. For older cpus we may as well always hint, but
17999 assume not taken for branches that are very close to 50% as a
18000 mispredicted taken branch is more expensive than a
18001 mispredicted not-taken branch. */
18002 if (rs6000_always_hint
18003 || (abs (prob) > REG_BR_PROB_BASE / 100 * 48
18004 && br_prob_note_reliable_p (note)))
18006 if (abs (prob) > REG_BR_PROB_BASE / 20
18007 && ((prob > 0) ^ need_longbranch))
18008 pred = "+";
18009 else
18010 pred = "-";
18014 if (label == NULL)
18015 s += sprintf (s, "b%slr%s ", ccode, pred);
18016 else
18017 s += sprintf (s, "b%s%s ", ccode, pred);
18019 /* We need to escape any '%' characters in the reg_names string.
18020 Assume they'd only be the first character.... */
18021 if (reg_names[cc_regno + CR0_REGNO][0] == '%')
18022 *s++ = '%';
18023 s += sprintf (s, "%s", reg_names[cc_regno + CR0_REGNO]);
18025 if (label != NULL)
18027 /* If the branch distance was too far, we may have to use an
18028 unconditional branch to go the distance. */
18029 if (need_longbranch)
18030 s += sprintf (s, ",$+8\n\tb %s", label);
18031 else
18032 s += sprintf (s, ",%s", label);
18035 return string;
18038 /* Return the string to flip the GT bit on a CR. */
18039 char *
18040 output_e500_flip_gt_bit (rtx dst, rtx src)
18042 static char string[64];
18043 int a, b;
18045 gcc_assert (GET_CODE (dst) == REG && CR_REGNO_P (REGNO (dst))
18046 && GET_CODE (src) == REG && CR_REGNO_P (REGNO (src)));
18048 /* GT bit. */
18049 a = 4 * (REGNO (dst) - CR0_REGNO) + 1;
18050 b = 4 * (REGNO (src) - CR0_REGNO) + 1;
18052 sprintf (string, "crnot %d,%d", a, b);
18053 return string;
18056 /* Return insn for VSX or Altivec comparisons. */
18058 static rtx
18059 rs6000_emit_vector_compare_inner (enum rtx_code code, rtx op0, rtx op1)
18061 rtx mask;
18062 enum machine_mode mode = GET_MODE (op0);
18064 switch (code)
18066 default:
18067 break;
18069 case GE:
18070 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
18071 return NULL_RTX;
18073 case EQ:
18074 case GT:
18075 case GTU:
18076 case ORDERED:
18077 case UNORDERED:
18078 case UNEQ:
18079 case LTGT:
18080 mask = gen_reg_rtx (mode);
18081 emit_insn (gen_rtx_SET (VOIDmode,
18082 mask,
18083 gen_rtx_fmt_ee (code, mode, op0, op1)));
18084 return mask;
18087 return NULL_RTX;
18090 /* Emit vector compare for operands OP0 and OP1 using code RCODE.
18091 DMODE is expected destination mode. This is a recursive function. */
18093 static rtx
18094 rs6000_emit_vector_compare (enum rtx_code rcode,
18095 rtx op0, rtx op1,
18096 enum machine_mode dmode)
18098 rtx mask;
18099 bool swap_operands = false;
18100 bool try_again = false;
18102 gcc_assert (VECTOR_UNIT_ALTIVEC_OR_VSX_P (dmode));
18103 gcc_assert (GET_MODE (op0) == GET_MODE (op1));
18105 /* See if the comparison works as is. */
18106 mask = rs6000_emit_vector_compare_inner (rcode, op0, op1);
18107 if (mask)
18108 return mask;
18110 switch (rcode)
18112 case LT:
18113 rcode = GT;
18114 swap_operands = true;
18115 try_again = true;
18116 break;
18117 case LTU:
18118 rcode = GTU;
18119 swap_operands = true;
18120 try_again = true;
18121 break;
18122 case NE:
18123 case UNLE:
18124 case UNLT:
18125 case UNGE:
18126 case UNGT:
18127 /* Invert condition and try again.
18128 e.g., A != B becomes ~(A==B). */
18130 enum rtx_code rev_code;
18131 enum insn_code nor_code;
18132 rtx mask2;
18134 rev_code = reverse_condition_maybe_unordered (rcode);
18135 if (rev_code == UNKNOWN)
18136 return NULL_RTX;
18138 nor_code = optab_handler (one_cmpl_optab, dmode);
18139 if (nor_code == CODE_FOR_nothing)
18140 return NULL_RTX;
18142 mask2 = rs6000_emit_vector_compare (rev_code, op0, op1, dmode);
18143 if (!mask2)
18144 return NULL_RTX;
18146 mask = gen_reg_rtx (dmode);
18147 emit_insn (GEN_FCN (nor_code) (mask, mask2));
18148 return mask;
18150 break;
18151 case GE:
18152 case GEU:
18153 case LE:
18154 case LEU:
18155 /* Try GT/GTU/LT/LTU OR EQ */
18157 rtx c_rtx, eq_rtx;
18158 enum insn_code ior_code;
18159 enum rtx_code new_code;
18161 switch (rcode)
18163 case GE:
18164 new_code = GT;
18165 break;
18167 case GEU:
18168 new_code = GTU;
18169 break;
18171 case LE:
18172 new_code = LT;
18173 break;
18175 case LEU:
18176 new_code = LTU;
18177 break;
18179 default:
18180 gcc_unreachable ();
18183 ior_code = optab_handler (ior_optab, dmode);
18184 if (ior_code == CODE_FOR_nothing)
18185 return NULL_RTX;
18187 c_rtx = rs6000_emit_vector_compare (new_code, op0, op1, dmode);
18188 if (!c_rtx)
18189 return NULL_RTX;
18191 eq_rtx = rs6000_emit_vector_compare (EQ, op0, op1, dmode);
18192 if (!eq_rtx)
18193 return NULL_RTX;
18195 mask = gen_reg_rtx (dmode);
18196 emit_insn (GEN_FCN (ior_code) (mask, c_rtx, eq_rtx));
18197 return mask;
18199 break;
18200 default:
18201 return NULL_RTX;
18204 if (try_again)
18206 if (swap_operands)
18208 rtx tmp;
18209 tmp = op0;
18210 op0 = op1;
18211 op1 = tmp;
18214 mask = rs6000_emit_vector_compare_inner (rcode, op0, op1);
18215 if (mask)
18216 return mask;
18219 /* You only get two chances. */
18220 return NULL_RTX;
18223 /* Emit vector conditional expression. DEST is destination. OP_TRUE and
18224 OP_FALSE are two VEC_COND_EXPR operands. CC_OP0 and CC_OP1 are the two
18225 operands for the relation operation COND. */
18228 rs6000_emit_vector_cond_expr (rtx dest, rtx op_true, rtx op_false,
18229 rtx cond, rtx cc_op0, rtx cc_op1)
18231 enum machine_mode dest_mode = GET_MODE (dest);
18232 enum machine_mode mask_mode = GET_MODE (cc_op0);
18233 enum rtx_code rcode = GET_CODE (cond);
18234 enum machine_mode cc_mode = CCmode;
18235 rtx mask;
18236 rtx cond2;
18237 rtx tmp;
18238 bool invert_move = false;
18240 if (VECTOR_UNIT_NONE_P (dest_mode))
18241 return 0;
18243 gcc_assert (GET_MODE_SIZE (dest_mode) == GET_MODE_SIZE (mask_mode)
18244 && GET_MODE_NUNITS (dest_mode) == GET_MODE_NUNITS (mask_mode));
18246 switch (rcode)
18248 /* Swap operands if we can, and fall back to doing the operation as
18249 specified, and doing a NOR to invert the test. */
18250 case NE:
18251 case UNLE:
18252 case UNLT:
18253 case UNGE:
18254 case UNGT:
18255 /* Invert condition and try again.
18256 e.g., A = (B != C) ? D : E becomes A = (B == C) ? E : D. */
18257 invert_move = true;
18258 rcode = reverse_condition_maybe_unordered (rcode);
18259 if (rcode == UNKNOWN)
18260 return 0;
18261 break;
18263 /* Mark unsigned tests with CCUNSmode. */
18264 case GTU:
18265 case GEU:
18266 case LTU:
18267 case LEU:
18268 cc_mode = CCUNSmode;
18269 break;
18271 default:
18272 break;
18275 /* Get the vector mask for the given relational operations. */
18276 mask = rs6000_emit_vector_compare (rcode, cc_op0, cc_op1, mask_mode);
18278 if (!mask)
18279 return 0;
18281 if (invert_move)
18283 tmp = op_true;
18284 op_true = op_false;
18285 op_false = tmp;
18288 cond2 = gen_rtx_fmt_ee (NE, cc_mode, gen_lowpart (dest_mode, mask),
18289 CONST0_RTX (dest_mode));
18290 emit_insn (gen_rtx_SET (VOIDmode,
18291 dest,
18292 gen_rtx_IF_THEN_ELSE (dest_mode,
18293 cond2,
18294 op_true,
18295 op_false)));
18296 return 1;
18299 /* Emit a conditional move: move TRUE_COND to DEST if OP of the
18300 operands of the last comparison is nonzero/true, FALSE_COND if it
18301 is zero/false. Return 0 if the hardware has no such operation. */
18304 rs6000_emit_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
18306 enum rtx_code code = GET_CODE (op);
18307 rtx op0 = XEXP (op, 0);
18308 rtx op1 = XEXP (op, 1);
18309 REAL_VALUE_TYPE c1;
18310 enum machine_mode compare_mode = GET_MODE (op0);
18311 enum machine_mode result_mode = GET_MODE (dest);
18312 rtx temp;
18313 bool is_against_zero;
18315 /* These modes should always match. */
18316 if (GET_MODE (op1) != compare_mode
18317 /* In the isel case however, we can use a compare immediate, so
18318 op1 may be a small constant. */
18319 && (!TARGET_ISEL || !short_cint_operand (op1, VOIDmode)))
18320 return 0;
18321 if (GET_MODE (true_cond) != result_mode)
18322 return 0;
18323 if (GET_MODE (false_cond) != result_mode)
18324 return 0;
18326 /* Don't allow using floating point comparisons for integer results for
18327 now. */
18328 if (FLOAT_MODE_P (compare_mode) && !FLOAT_MODE_P (result_mode))
18329 return 0;
18331 /* First, work out if the hardware can do this at all, or
18332 if it's too slow.... */
18333 if (!FLOAT_MODE_P (compare_mode))
18335 if (TARGET_ISEL)
18336 return rs6000_emit_int_cmove (dest, op, true_cond, false_cond);
18337 return 0;
18339 else if (TARGET_HARD_FLOAT && !TARGET_FPRS
18340 && SCALAR_FLOAT_MODE_P (compare_mode))
18341 return 0;
18343 is_against_zero = op1 == CONST0_RTX (compare_mode);
18345 /* A floating-point subtract might overflow, underflow, or produce
18346 an inexact result, thus changing the floating-point flags, so it
18347 can't be generated if we care about that. It's safe if one side
18348 of the construct is zero, since then no subtract will be
18349 generated. */
18350 if (SCALAR_FLOAT_MODE_P (compare_mode)
18351 && flag_trapping_math && ! is_against_zero)
18352 return 0;
18354 /* Eliminate half of the comparisons by switching operands, this
18355 makes the remaining code simpler. */
18356 if (code == UNLT || code == UNGT || code == UNORDERED || code == NE
18357 || code == LTGT || code == LT || code == UNLE)
18359 code = reverse_condition_maybe_unordered (code);
18360 temp = true_cond;
18361 true_cond = false_cond;
18362 false_cond = temp;
18365 /* UNEQ and LTGT take four instructions for a comparison with zero,
18366 it'll probably be faster to use a branch here too. */
18367 if (code == UNEQ && HONOR_NANS (compare_mode))
18368 return 0;
18370 if (GET_CODE (op1) == CONST_DOUBLE)
18371 REAL_VALUE_FROM_CONST_DOUBLE (c1, op1);
18373 /* We're going to try to implement comparisons by performing
18374 a subtract, then comparing against zero. Unfortunately,
18375 Inf - Inf is NaN which is not zero, and so if we don't
18376 know that the operand is finite and the comparison
18377 would treat EQ different to UNORDERED, we can't do it. */
18378 if (HONOR_INFINITIES (compare_mode)
18379 && code != GT && code != UNGE
18380 && (GET_CODE (op1) != CONST_DOUBLE || real_isinf (&c1))
18381 /* Constructs of the form (a OP b ? a : b) are safe. */
18382 && ((! rtx_equal_p (op0, false_cond) && ! rtx_equal_p (op1, false_cond))
18383 || (! rtx_equal_p (op0, true_cond)
18384 && ! rtx_equal_p (op1, true_cond))))
18385 return 0;
18387 /* At this point we know we can use fsel. */
18389 /* Reduce the comparison to a comparison against zero. */
18390 if (! is_against_zero)
18392 temp = gen_reg_rtx (compare_mode);
18393 emit_insn (gen_rtx_SET (VOIDmode, temp,
18394 gen_rtx_MINUS (compare_mode, op0, op1)));
18395 op0 = temp;
18396 op1 = CONST0_RTX (compare_mode);
18399 /* If we don't care about NaNs we can reduce some of the comparisons
18400 down to faster ones. */
18401 if (! HONOR_NANS (compare_mode))
18402 switch (code)
18404 case GT:
18405 code = LE;
18406 temp = true_cond;
18407 true_cond = false_cond;
18408 false_cond = temp;
18409 break;
18410 case UNGE:
18411 code = GE;
18412 break;
18413 case UNEQ:
18414 code = EQ;
18415 break;
18416 default:
18417 break;
18420 /* Now, reduce everything down to a GE. */
18421 switch (code)
18423 case GE:
18424 break;
18426 case LE:
18427 temp = gen_reg_rtx (compare_mode);
18428 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
18429 op0 = temp;
18430 break;
18432 case ORDERED:
18433 temp = gen_reg_rtx (compare_mode);
18434 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_ABS (compare_mode, op0)));
18435 op0 = temp;
18436 break;
18438 case EQ:
18439 temp = gen_reg_rtx (compare_mode);
18440 emit_insn (gen_rtx_SET (VOIDmode, temp,
18441 gen_rtx_NEG (compare_mode,
18442 gen_rtx_ABS (compare_mode, op0))));
18443 op0 = temp;
18444 break;
18446 case UNGE:
18447 /* a UNGE 0 <-> (a GE 0 || -a UNLT 0) */
18448 temp = gen_reg_rtx (result_mode);
18449 emit_insn (gen_rtx_SET (VOIDmode, temp,
18450 gen_rtx_IF_THEN_ELSE (result_mode,
18451 gen_rtx_GE (VOIDmode,
18452 op0, op1),
18453 true_cond, false_cond)));
18454 false_cond = true_cond;
18455 true_cond = temp;
18457 temp = gen_reg_rtx (compare_mode);
18458 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
18459 op0 = temp;
18460 break;
18462 case GT:
18463 /* a GT 0 <-> (a GE 0 && -a UNLT 0) */
18464 temp = gen_reg_rtx (result_mode);
18465 emit_insn (gen_rtx_SET (VOIDmode, temp,
18466 gen_rtx_IF_THEN_ELSE (result_mode,
18467 gen_rtx_GE (VOIDmode,
18468 op0, op1),
18469 true_cond, false_cond)));
18470 true_cond = false_cond;
18471 false_cond = temp;
18473 temp = gen_reg_rtx (compare_mode);
18474 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
18475 op0 = temp;
18476 break;
18478 default:
18479 gcc_unreachable ();
18482 emit_insn (gen_rtx_SET (VOIDmode, dest,
18483 gen_rtx_IF_THEN_ELSE (result_mode,
18484 gen_rtx_GE (VOIDmode,
18485 op0, op1),
18486 true_cond, false_cond)));
18487 return 1;
18490 /* Same as above, but for ints (isel). */
18492 static int
18493 rs6000_emit_int_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
18495 rtx condition_rtx, cr;
18496 enum machine_mode mode = GET_MODE (dest);
18497 enum rtx_code cond_code;
18498 rtx (*isel_func) (rtx, rtx, rtx, rtx, rtx);
18499 bool signedp;
18501 if (mode != SImode && (!TARGET_POWERPC64 || mode != DImode))
18502 return 0;
18504 /* We still have to do the compare, because isel doesn't do a
18505 compare, it just looks at the CRx bits set by a previous compare
18506 instruction. */
18507 condition_rtx = rs6000_generate_compare (op, mode);
18508 cond_code = GET_CODE (condition_rtx);
18509 cr = XEXP (condition_rtx, 0);
18510 signedp = GET_MODE (cr) == CCmode;
18512 isel_func = (mode == SImode
18513 ? (signedp ? gen_isel_signed_si : gen_isel_unsigned_si)
18514 : (signedp ? gen_isel_signed_di : gen_isel_unsigned_di));
18516 switch (cond_code)
18518 case LT: case GT: case LTU: case GTU: case EQ:
18519 /* isel handles these directly. */
18520 break;
18522 default:
18523 /* We need to swap the sense of the comparison. */
18525 rtx t = true_cond;
18526 true_cond = false_cond;
18527 false_cond = t;
18528 PUT_CODE (condition_rtx, reverse_condition (cond_code));
18530 break;
18533 false_cond = force_reg (mode, false_cond);
18534 if (true_cond != const0_rtx)
18535 true_cond = force_reg (mode, true_cond);
18537 emit_insn (isel_func (dest, condition_rtx, true_cond, false_cond, cr));
18539 return 1;
18542 const char *
18543 output_isel (rtx *operands)
18545 enum rtx_code code;
18547 code = GET_CODE (operands[1]);
18549 if (code == GE || code == GEU || code == LE || code == LEU || code == NE)
18551 gcc_assert (GET_CODE (operands[2]) == REG
18552 && GET_CODE (operands[3]) == REG);
18553 PUT_CODE (operands[1], reverse_condition (code));
18554 return "isel %0,%3,%2,%j1";
18557 return "isel %0,%2,%3,%j1";
18560 void
18561 rs6000_emit_minmax (rtx dest, enum rtx_code code, rtx op0, rtx op1)
18563 enum machine_mode mode = GET_MODE (op0);
18564 enum rtx_code c;
18565 rtx target;
18567 /* VSX/altivec have direct min/max insns. */
18568 if ((code == SMAX || code == SMIN)
18569 && (VECTOR_UNIT_ALTIVEC_OR_VSX_P (mode)
18570 || (mode == SFmode && VECTOR_UNIT_VSX_P (DFmode))))
18572 emit_insn (gen_rtx_SET (VOIDmode,
18573 dest,
18574 gen_rtx_fmt_ee (code, mode, op0, op1)));
18575 return;
18578 if (code == SMAX || code == SMIN)
18579 c = GE;
18580 else
18581 c = GEU;
18583 if (code == SMAX || code == UMAX)
18584 target = emit_conditional_move (dest, c, op0, op1, mode,
18585 op0, op1, mode, 0);
18586 else
18587 target = emit_conditional_move (dest, c, op0, op1, mode,
18588 op1, op0, mode, 0);
18589 gcc_assert (target);
18590 if (target != dest)
18591 emit_move_insn (dest, target);
18594 /* A subroutine of the atomic operation splitters. Jump to LABEL if
18595 COND is true. Mark the jump as unlikely to be taken. */
18597 static void
18598 emit_unlikely_jump (rtx cond, rtx label)
18600 int very_unlikely = REG_BR_PROB_BASE / 100 - 1;
18601 rtx x;
18603 x = gen_rtx_IF_THEN_ELSE (VOIDmode, cond, label, pc_rtx);
18604 x = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, x));
18605 add_int_reg_note (x, REG_BR_PROB, very_unlikely);
18608 /* A subroutine of the atomic operation splitters. Emit a load-locked
18609 instruction in MODE. For QI/HImode, possibly use a pattern than includes
18610 the zero_extend operation. */
18612 static void
18613 emit_load_locked (enum machine_mode mode, rtx reg, rtx mem)
18615 rtx (*fn) (rtx, rtx) = NULL;
18617 switch (mode)
18619 case QImode:
18620 fn = gen_load_lockedqi;
18621 break;
18622 case HImode:
18623 fn = gen_load_lockedhi;
18624 break;
18625 case SImode:
18626 if (GET_MODE (mem) == QImode)
18627 fn = gen_load_lockedqi_si;
18628 else if (GET_MODE (mem) == HImode)
18629 fn = gen_load_lockedhi_si;
18630 else
18631 fn = gen_load_lockedsi;
18632 break;
18633 case DImode:
18634 fn = gen_load_lockeddi;
18635 break;
18636 case TImode:
18637 fn = gen_load_lockedti;
18638 break;
18639 default:
18640 gcc_unreachable ();
18642 emit_insn (fn (reg, mem));
18645 /* A subroutine of the atomic operation splitters. Emit a store-conditional
18646 instruction in MODE. */
18648 static void
18649 emit_store_conditional (enum machine_mode mode, rtx res, rtx mem, rtx val)
18651 rtx (*fn) (rtx, rtx, rtx) = NULL;
18653 switch (mode)
18655 case QImode:
18656 fn = gen_store_conditionalqi;
18657 break;
18658 case HImode:
18659 fn = gen_store_conditionalhi;
18660 break;
18661 case SImode:
18662 fn = gen_store_conditionalsi;
18663 break;
18664 case DImode:
18665 fn = gen_store_conditionaldi;
18666 break;
18667 case TImode:
18668 fn = gen_store_conditionalti;
18669 break;
18670 default:
18671 gcc_unreachable ();
18674 /* Emit sync before stwcx. to address PPC405 Erratum. */
18675 if (PPC405_ERRATUM77)
18676 emit_insn (gen_hwsync ());
18678 emit_insn (fn (res, mem, val));
18681 /* Expand barriers before and after a load_locked/store_cond sequence. */
18683 static rtx
18684 rs6000_pre_atomic_barrier (rtx mem, enum memmodel model)
18686 rtx addr = XEXP (mem, 0);
18687 int strict_p = (reload_in_progress || reload_completed);
18689 if (!legitimate_indirect_address_p (addr, strict_p)
18690 && !legitimate_indexed_address_p (addr, strict_p))
18692 addr = force_reg (Pmode, addr);
18693 mem = replace_equiv_address_nv (mem, addr);
18696 switch (model)
18698 case MEMMODEL_RELAXED:
18699 case MEMMODEL_CONSUME:
18700 case MEMMODEL_ACQUIRE:
18701 break;
18702 case MEMMODEL_RELEASE:
18703 case MEMMODEL_ACQ_REL:
18704 emit_insn (gen_lwsync ());
18705 break;
18706 case MEMMODEL_SEQ_CST:
18707 emit_insn (gen_hwsync ());
18708 break;
18709 default:
18710 gcc_unreachable ();
18712 return mem;
18715 static void
18716 rs6000_post_atomic_barrier (enum memmodel model)
18718 switch (model)
18720 case MEMMODEL_RELAXED:
18721 case MEMMODEL_CONSUME:
18722 case MEMMODEL_RELEASE:
18723 break;
18724 case MEMMODEL_ACQUIRE:
18725 case MEMMODEL_ACQ_REL:
18726 case MEMMODEL_SEQ_CST:
18727 emit_insn (gen_isync ());
18728 break;
18729 default:
18730 gcc_unreachable ();
18734 /* A subroutine of the various atomic expanders. For sub-word operations,
18735 we must adjust things to operate on SImode. Given the original MEM,
18736 return a new aligned memory. Also build and return the quantities by
18737 which to shift and mask. */
18739 static rtx
18740 rs6000_adjust_atomic_subword (rtx orig_mem, rtx *pshift, rtx *pmask)
18742 rtx addr, align, shift, mask, mem;
18743 HOST_WIDE_INT shift_mask;
18744 enum machine_mode mode = GET_MODE (orig_mem);
18746 /* For smaller modes, we have to implement this via SImode. */
18747 shift_mask = (mode == QImode ? 0x18 : 0x10);
18749 addr = XEXP (orig_mem, 0);
18750 addr = force_reg (GET_MODE (addr), addr);
18752 /* Aligned memory containing subword. Generate a new memory. We
18753 do not want any of the existing MEM_ATTR data, as we're now
18754 accessing memory outside the original object. */
18755 align = expand_simple_binop (Pmode, AND, addr, GEN_INT (-4),
18756 NULL_RTX, 1, OPTAB_LIB_WIDEN);
18757 mem = gen_rtx_MEM (SImode, align);
18758 MEM_VOLATILE_P (mem) = MEM_VOLATILE_P (orig_mem);
18759 if (MEM_ALIAS_SET (orig_mem) == ALIAS_SET_MEMORY_BARRIER)
18760 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
18762 /* Shift amount for subword relative to aligned word. */
18763 shift = gen_reg_rtx (SImode);
18764 addr = gen_lowpart (SImode, addr);
18765 emit_insn (gen_rlwinm (shift, addr, GEN_INT (3), GEN_INT (shift_mask)));
18766 if (WORDS_BIG_ENDIAN)
18767 shift = expand_simple_binop (SImode, XOR, shift, GEN_INT (shift_mask),
18768 shift, 1, OPTAB_LIB_WIDEN);
18769 *pshift = shift;
18771 /* Mask for insertion. */
18772 mask = expand_simple_binop (SImode, ASHIFT, GEN_INT (GET_MODE_MASK (mode)),
18773 shift, NULL_RTX, 1, OPTAB_LIB_WIDEN);
18774 *pmask = mask;
18776 return mem;
18779 /* A subroutine of the various atomic expanders. For sub-word operands,
18780 combine OLDVAL and NEWVAL via MASK. Returns a new pseduo. */
18782 static rtx
18783 rs6000_mask_atomic_subword (rtx oldval, rtx newval, rtx mask)
18785 rtx x;
18787 x = gen_reg_rtx (SImode);
18788 emit_insn (gen_rtx_SET (VOIDmode, x,
18789 gen_rtx_AND (SImode,
18790 gen_rtx_NOT (SImode, mask),
18791 oldval)));
18793 x = expand_simple_binop (SImode, IOR, newval, x, x, 1, OPTAB_LIB_WIDEN);
18795 return x;
18798 /* A subroutine of the various atomic expanders. For sub-word operands,
18799 extract WIDE to NARROW via SHIFT. */
18801 static void
18802 rs6000_finish_atomic_subword (rtx narrow, rtx wide, rtx shift)
18804 wide = expand_simple_binop (SImode, LSHIFTRT, wide, shift,
18805 wide, 1, OPTAB_LIB_WIDEN);
18806 emit_move_insn (narrow, gen_lowpart (GET_MODE (narrow), wide));
18809 /* Expand an atomic compare and swap operation. */
18811 void
18812 rs6000_expand_atomic_compare_and_swap (rtx operands[])
18814 rtx boolval, retval, mem, oldval, newval, cond;
18815 rtx label1, label2, x, mask, shift;
18816 enum machine_mode mode, orig_mode;
18817 enum memmodel mod_s, mod_f;
18818 bool is_weak;
18820 boolval = operands[0];
18821 retval = operands[1];
18822 mem = operands[2];
18823 oldval = operands[3];
18824 newval = operands[4];
18825 is_weak = (INTVAL (operands[5]) != 0);
18826 mod_s = (enum memmodel) INTVAL (operands[6]);
18827 mod_f = (enum memmodel) INTVAL (operands[7]);
18828 orig_mode = mode = GET_MODE (mem);
18830 mask = shift = NULL_RTX;
18831 if (mode == QImode || mode == HImode)
18833 /* Before power8, we didn't have access to lbarx/lharx, so generate a
18834 lwarx and shift/mask operations. With power8, we need to do the
18835 comparison in SImode, but the store is still done in QI/HImode. */
18836 oldval = convert_modes (SImode, mode, oldval, 1);
18838 if (!TARGET_SYNC_HI_QI)
18840 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
18842 /* Shift and mask OLDVAL into position with the word. */
18843 oldval = expand_simple_binop (SImode, ASHIFT, oldval, shift,
18844 NULL_RTX, 1, OPTAB_LIB_WIDEN);
18846 /* Shift and mask NEWVAL into position within the word. */
18847 newval = convert_modes (SImode, mode, newval, 1);
18848 newval = expand_simple_binop (SImode, ASHIFT, newval, shift,
18849 NULL_RTX, 1, OPTAB_LIB_WIDEN);
18852 /* Prepare to adjust the return value. */
18853 retval = gen_reg_rtx (SImode);
18854 mode = SImode;
18856 else if (reg_overlap_mentioned_p (retval, oldval))
18857 oldval = copy_to_reg (oldval);
18859 mem = rs6000_pre_atomic_barrier (mem, mod_s);
18861 label1 = NULL_RTX;
18862 if (!is_weak)
18864 label1 = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
18865 emit_label (XEXP (label1, 0));
18867 label2 = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
18869 emit_load_locked (mode, retval, mem);
18871 x = retval;
18872 if (mask)
18874 x = expand_simple_binop (SImode, AND, retval, mask,
18875 NULL_RTX, 1, OPTAB_LIB_WIDEN);
18878 cond = gen_reg_rtx (CCmode);
18879 /* If we have TImode, synthesize a comparison. */
18880 if (mode != TImode)
18881 x = gen_rtx_COMPARE (CCmode, x, oldval);
18882 else
18884 rtx xor1_result = gen_reg_rtx (DImode);
18885 rtx xor2_result = gen_reg_rtx (DImode);
18886 rtx or_result = gen_reg_rtx (DImode);
18887 rtx new_word0 = simplify_gen_subreg (DImode, x, TImode, 0);
18888 rtx new_word1 = simplify_gen_subreg (DImode, x, TImode, 8);
18889 rtx old_word0 = simplify_gen_subreg (DImode, oldval, TImode, 0);
18890 rtx old_word1 = simplify_gen_subreg (DImode, oldval, TImode, 8);
18892 emit_insn (gen_xordi3 (xor1_result, new_word0, old_word0));
18893 emit_insn (gen_xordi3 (xor2_result, new_word1, old_word1));
18894 emit_insn (gen_iordi3 (or_result, xor1_result, xor2_result));
18895 x = gen_rtx_COMPARE (CCmode, or_result, const0_rtx);
18898 emit_insn (gen_rtx_SET (VOIDmode, cond, x));
18900 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
18901 emit_unlikely_jump (x, label2);
18903 x = newval;
18904 if (mask)
18905 x = rs6000_mask_atomic_subword (retval, newval, mask);
18907 emit_store_conditional (orig_mode, cond, mem, x);
18909 if (!is_weak)
18911 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
18912 emit_unlikely_jump (x, label1);
18915 if (mod_f != MEMMODEL_RELAXED)
18916 emit_label (XEXP (label2, 0));
18918 rs6000_post_atomic_barrier (mod_s);
18920 if (mod_f == MEMMODEL_RELAXED)
18921 emit_label (XEXP (label2, 0));
18923 if (shift)
18924 rs6000_finish_atomic_subword (operands[1], retval, shift);
18925 else if (mode != GET_MODE (operands[1]))
18926 convert_move (operands[1], retval, 1);
18928 /* In all cases, CR0 contains EQ on success, and NE on failure. */
18929 x = gen_rtx_EQ (SImode, cond, const0_rtx);
18930 emit_insn (gen_rtx_SET (VOIDmode, boolval, x));
18933 /* Expand an atomic exchange operation. */
18935 void
18936 rs6000_expand_atomic_exchange (rtx operands[])
18938 rtx retval, mem, val, cond;
18939 enum machine_mode mode;
18940 enum memmodel model;
18941 rtx label, x, mask, shift;
18943 retval = operands[0];
18944 mem = operands[1];
18945 val = operands[2];
18946 model = (enum memmodel) INTVAL (operands[3]);
18947 mode = GET_MODE (mem);
18949 mask = shift = NULL_RTX;
18950 if (!TARGET_SYNC_HI_QI && (mode == QImode || mode == HImode))
18952 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
18954 /* Shift and mask VAL into position with the word. */
18955 val = convert_modes (SImode, mode, val, 1);
18956 val = expand_simple_binop (SImode, ASHIFT, val, shift,
18957 NULL_RTX, 1, OPTAB_LIB_WIDEN);
18959 /* Prepare to adjust the return value. */
18960 retval = gen_reg_rtx (SImode);
18961 mode = SImode;
18964 mem = rs6000_pre_atomic_barrier (mem, model);
18966 label = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
18967 emit_label (XEXP (label, 0));
18969 emit_load_locked (mode, retval, mem);
18971 x = val;
18972 if (mask)
18973 x = rs6000_mask_atomic_subword (retval, val, mask);
18975 cond = gen_reg_rtx (CCmode);
18976 emit_store_conditional (mode, cond, mem, x);
18978 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
18979 emit_unlikely_jump (x, label);
18981 rs6000_post_atomic_barrier (model);
18983 if (shift)
18984 rs6000_finish_atomic_subword (operands[0], retval, shift);
18987 /* Expand an atomic fetch-and-operate pattern. CODE is the binary operation
18988 to perform. MEM is the memory on which to operate. VAL is the second
18989 operand of the binary operator. BEFORE and AFTER are optional locations to
18990 return the value of MEM either before of after the operation. MODEL_RTX
18991 is a CONST_INT containing the memory model to use. */
18993 void
18994 rs6000_expand_atomic_op (enum rtx_code code, rtx mem, rtx val,
18995 rtx orig_before, rtx orig_after, rtx model_rtx)
18997 enum memmodel model = (enum memmodel) INTVAL (model_rtx);
18998 enum machine_mode mode = GET_MODE (mem);
18999 enum machine_mode store_mode = mode;
19000 rtx label, x, cond, mask, shift;
19001 rtx before = orig_before, after = orig_after;
19003 mask = shift = NULL_RTX;
19004 /* On power8, we want to use SImode for the operation. On previous systems,
19005 use the operation in a subword and shift/mask to get the proper byte or
19006 halfword. */
19007 if (mode == QImode || mode == HImode)
19009 if (TARGET_SYNC_HI_QI)
19011 val = convert_modes (SImode, mode, val, 1);
19013 /* Prepare to adjust the return value. */
19014 before = gen_reg_rtx (SImode);
19015 if (after)
19016 after = gen_reg_rtx (SImode);
19017 mode = SImode;
19019 else
19021 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
19023 /* Shift and mask VAL into position with the word. */
19024 val = convert_modes (SImode, mode, val, 1);
19025 val = expand_simple_binop (SImode, ASHIFT, val, shift,
19026 NULL_RTX, 1, OPTAB_LIB_WIDEN);
19028 switch (code)
19030 case IOR:
19031 case XOR:
19032 /* We've already zero-extended VAL. That is sufficient to
19033 make certain that it does not affect other bits. */
19034 mask = NULL;
19035 break;
19037 case AND:
19038 /* If we make certain that all of the other bits in VAL are
19039 set, that will be sufficient to not affect other bits. */
19040 x = gen_rtx_NOT (SImode, mask);
19041 x = gen_rtx_IOR (SImode, x, val);
19042 emit_insn (gen_rtx_SET (VOIDmode, val, x));
19043 mask = NULL;
19044 break;
19046 case NOT:
19047 case PLUS:
19048 case MINUS:
19049 /* These will all affect bits outside the field and need
19050 adjustment via MASK within the loop. */
19051 break;
19053 default:
19054 gcc_unreachable ();
19057 /* Prepare to adjust the return value. */
19058 before = gen_reg_rtx (SImode);
19059 if (after)
19060 after = gen_reg_rtx (SImode);
19061 store_mode = mode = SImode;
19065 mem = rs6000_pre_atomic_barrier (mem, model);
19067 label = gen_label_rtx ();
19068 emit_label (label);
19069 label = gen_rtx_LABEL_REF (VOIDmode, label);
19071 if (before == NULL_RTX)
19072 before = gen_reg_rtx (mode);
19074 emit_load_locked (mode, before, mem);
19076 if (code == NOT)
19078 x = expand_simple_binop (mode, AND, before, val,
19079 NULL_RTX, 1, OPTAB_LIB_WIDEN);
19080 after = expand_simple_unop (mode, NOT, x, after, 1);
19082 else
19084 after = expand_simple_binop (mode, code, before, val,
19085 after, 1, OPTAB_LIB_WIDEN);
19088 x = after;
19089 if (mask)
19091 x = expand_simple_binop (SImode, AND, after, mask,
19092 NULL_RTX, 1, OPTAB_LIB_WIDEN);
19093 x = rs6000_mask_atomic_subword (before, x, mask);
19095 else if (store_mode != mode)
19096 x = convert_modes (store_mode, mode, x, 1);
19098 cond = gen_reg_rtx (CCmode);
19099 emit_store_conditional (store_mode, cond, mem, x);
19101 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
19102 emit_unlikely_jump (x, label);
19104 rs6000_post_atomic_barrier (model);
19106 if (shift)
19108 /* QImode/HImode on machines without lbarx/lharx where we do a lwarx and
19109 then do the calcuations in a SImode register. */
19110 if (orig_before)
19111 rs6000_finish_atomic_subword (orig_before, before, shift);
19112 if (orig_after)
19113 rs6000_finish_atomic_subword (orig_after, after, shift);
19115 else if (store_mode != mode)
19117 /* QImode/HImode on machines with lbarx/lharx where we do the native
19118 operation and then do the calcuations in a SImode register. */
19119 if (orig_before)
19120 convert_move (orig_before, before, 1);
19121 if (orig_after)
19122 convert_move (orig_after, after, 1);
19124 else if (orig_after && after != orig_after)
19125 emit_move_insn (orig_after, after);
19128 /* Emit instructions to move SRC to DST. Called by splitters for
19129 multi-register moves. It will emit at most one instruction for
19130 each register that is accessed; that is, it won't emit li/lis pairs
19131 (or equivalent for 64-bit code). One of SRC or DST must be a hard
19132 register. */
19134 void
19135 rs6000_split_multireg_move (rtx dst, rtx src)
19137 /* The register number of the first register being moved. */
19138 int reg;
19139 /* The mode that is to be moved. */
19140 enum machine_mode mode;
19141 /* The mode that the move is being done in, and its size. */
19142 enum machine_mode reg_mode;
19143 int reg_mode_size;
19144 /* The number of registers that will be moved. */
19145 int nregs;
19147 reg = REG_P (dst) ? REGNO (dst) : REGNO (src);
19148 mode = GET_MODE (dst);
19149 nregs = hard_regno_nregs[reg][mode];
19150 if (FP_REGNO_P (reg))
19151 reg_mode = DECIMAL_FLOAT_MODE_P (mode) ? DDmode :
19152 ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? DFmode : SFmode);
19153 else if (ALTIVEC_REGNO_P (reg))
19154 reg_mode = V16QImode;
19155 else if (TARGET_E500_DOUBLE && mode == TFmode)
19156 reg_mode = DFmode;
19157 else
19158 reg_mode = word_mode;
19159 reg_mode_size = GET_MODE_SIZE (reg_mode);
19161 gcc_assert (reg_mode_size * nregs == GET_MODE_SIZE (mode));
19163 if (REG_P (src) && REG_P (dst) && (REGNO (src) < REGNO (dst)))
19165 /* Move register range backwards, if we might have destructive
19166 overlap. */
19167 int i;
19168 for (i = nregs - 1; i >= 0; i--)
19169 emit_insn (gen_rtx_SET (VOIDmode,
19170 simplify_gen_subreg (reg_mode, dst, mode,
19171 i * reg_mode_size),
19172 simplify_gen_subreg (reg_mode, src, mode,
19173 i * reg_mode_size)));
19175 else
19177 int i;
19178 int j = -1;
19179 bool used_update = false;
19180 rtx restore_basereg = NULL_RTX;
19182 if (MEM_P (src) && INT_REGNO_P (reg))
19184 rtx breg;
19186 if (GET_CODE (XEXP (src, 0)) == PRE_INC
19187 || GET_CODE (XEXP (src, 0)) == PRE_DEC)
19189 rtx delta_rtx;
19190 breg = XEXP (XEXP (src, 0), 0);
19191 delta_rtx = (GET_CODE (XEXP (src, 0)) == PRE_INC
19192 ? GEN_INT (GET_MODE_SIZE (GET_MODE (src)))
19193 : GEN_INT (-GET_MODE_SIZE (GET_MODE (src))));
19194 emit_insn (gen_add3_insn (breg, breg, delta_rtx));
19195 src = replace_equiv_address (src, breg);
19197 else if (! rs6000_offsettable_memref_p (src, reg_mode))
19199 if (GET_CODE (XEXP (src, 0)) == PRE_MODIFY)
19201 rtx basereg = XEXP (XEXP (src, 0), 0);
19202 if (TARGET_UPDATE)
19204 rtx ndst = simplify_gen_subreg (reg_mode, dst, mode, 0);
19205 emit_insn (gen_rtx_SET (VOIDmode, ndst,
19206 gen_rtx_MEM (reg_mode, XEXP (src, 0))));
19207 used_update = true;
19209 else
19210 emit_insn (gen_rtx_SET (VOIDmode, basereg,
19211 XEXP (XEXP (src, 0), 1)));
19212 src = replace_equiv_address (src, basereg);
19214 else
19216 rtx basereg = gen_rtx_REG (Pmode, reg);
19217 emit_insn (gen_rtx_SET (VOIDmode, basereg, XEXP (src, 0)));
19218 src = replace_equiv_address (src, basereg);
19222 breg = XEXP (src, 0);
19223 if (GET_CODE (breg) == PLUS || GET_CODE (breg) == LO_SUM)
19224 breg = XEXP (breg, 0);
19226 /* If the base register we are using to address memory is
19227 also a destination reg, then change that register last. */
19228 if (REG_P (breg)
19229 && REGNO (breg) >= REGNO (dst)
19230 && REGNO (breg) < REGNO (dst) + nregs)
19231 j = REGNO (breg) - REGNO (dst);
19233 else if (MEM_P (dst) && INT_REGNO_P (reg))
19235 rtx breg;
19237 if (GET_CODE (XEXP (dst, 0)) == PRE_INC
19238 || GET_CODE (XEXP (dst, 0)) == PRE_DEC)
19240 rtx delta_rtx;
19241 breg = XEXP (XEXP (dst, 0), 0);
19242 delta_rtx = (GET_CODE (XEXP (dst, 0)) == PRE_INC
19243 ? GEN_INT (GET_MODE_SIZE (GET_MODE (dst)))
19244 : GEN_INT (-GET_MODE_SIZE (GET_MODE (dst))));
19246 /* We have to update the breg before doing the store.
19247 Use store with update, if available. */
19249 if (TARGET_UPDATE)
19251 rtx nsrc = simplify_gen_subreg (reg_mode, src, mode, 0);
19252 emit_insn (TARGET_32BIT
19253 ? (TARGET_POWERPC64
19254 ? gen_movdi_si_update (breg, breg, delta_rtx, nsrc)
19255 : gen_movsi_update (breg, breg, delta_rtx, nsrc))
19256 : gen_movdi_di_update (breg, breg, delta_rtx, nsrc));
19257 used_update = true;
19259 else
19260 emit_insn (gen_add3_insn (breg, breg, delta_rtx));
19261 dst = replace_equiv_address (dst, breg);
19263 else if (!rs6000_offsettable_memref_p (dst, reg_mode)
19264 && GET_CODE (XEXP (dst, 0)) != LO_SUM)
19266 if (GET_CODE (XEXP (dst, 0)) == PRE_MODIFY)
19268 rtx basereg = XEXP (XEXP (dst, 0), 0);
19269 if (TARGET_UPDATE)
19271 rtx nsrc = simplify_gen_subreg (reg_mode, src, mode, 0);
19272 emit_insn (gen_rtx_SET (VOIDmode,
19273 gen_rtx_MEM (reg_mode, XEXP (dst, 0)), nsrc));
19274 used_update = true;
19276 else
19277 emit_insn (gen_rtx_SET (VOIDmode, basereg,
19278 XEXP (XEXP (dst, 0), 1)));
19279 dst = replace_equiv_address (dst, basereg);
19281 else
19283 rtx basereg = XEXP (XEXP (dst, 0), 0);
19284 rtx offsetreg = XEXP (XEXP (dst, 0), 1);
19285 gcc_assert (GET_CODE (XEXP (dst, 0)) == PLUS
19286 && REG_P (basereg)
19287 && REG_P (offsetreg)
19288 && REGNO (basereg) != REGNO (offsetreg));
19289 if (REGNO (basereg) == 0)
19291 rtx tmp = offsetreg;
19292 offsetreg = basereg;
19293 basereg = tmp;
19295 emit_insn (gen_add3_insn (basereg, basereg, offsetreg));
19296 restore_basereg = gen_sub3_insn (basereg, basereg, offsetreg);
19297 dst = replace_equiv_address (dst, basereg);
19300 else if (GET_CODE (XEXP (dst, 0)) != LO_SUM)
19301 gcc_assert (rs6000_offsettable_memref_p (dst, reg_mode));
19304 for (i = 0; i < nregs; i++)
19306 /* Calculate index to next subword. */
19307 ++j;
19308 if (j == nregs)
19309 j = 0;
19311 /* If compiler already emitted move of first word by
19312 store with update, no need to do anything. */
19313 if (j == 0 && used_update)
19314 continue;
19316 emit_insn (gen_rtx_SET (VOIDmode,
19317 simplify_gen_subreg (reg_mode, dst, mode,
19318 j * reg_mode_size),
19319 simplify_gen_subreg (reg_mode, src, mode,
19320 j * reg_mode_size)));
19322 if (restore_basereg != NULL_RTX)
19323 emit_insn (restore_basereg);
19328 /* This page contains routines that are used to determine what the
19329 function prologue and epilogue code will do and write them out. */
19331 static inline bool
19332 save_reg_p (int r)
19334 return !call_used_regs[r] && df_regs_ever_live_p (r);
19337 /* Return the first fixed-point register that is required to be
19338 saved. 32 if none. */
19341 first_reg_to_save (void)
19343 int first_reg;
19345 /* Find lowest numbered live register. */
19346 for (first_reg = 13; first_reg <= 31; first_reg++)
19347 if (save_reg_p (first_reg))
19348 break;
19350 if (first_reg > RS6000_PIC_OFFSET_TABLE_REGNUM
19351 && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
19352 || (DEFAULT_ABI == ABI_DARWIN && flag_pic)
19353 || (TARGET_TOC && TARGET_MINIMAL_TOC))
19354 && df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))
19355 first_reg = RS6000_PIC_OFFSET_TABLE_REGNUM;
19357 #if TARGET_MACHO
19358 if (flag_pic
19359 && crtl->uses_pic_offset_table
19360 && first_reg > RS6000_PIC_OFFSET_TABLE_REGNUM)
19361 return RS6000_PIC_OFFSET_TABLE_REGNUM;
19362 #endif
19364 return first_reg;
19367 /* Similar, for FP regs. */
19370 first_fp_reg_to_save (void)
19372 int first_reg;
19374 /* Find lowest numbered live register. */
19375 for (first_reg = 14 + 32; first_reg <= 63; first_reg++)
19376 if (save_reg_p (first_reg))
19377 break;
19379 return first_reg;
19382 /* Similar, for AltiVec regs. */
19384 static int
19385 first_altivec_reg_to_save (void)
19387 int i;
19389 /* Stack frame remains as is unless we are in AltiVec ABI. */
19390 if (! TARGET_ALTIVEC_ABI)
19391 return LAST_ALTIVEC_REGNO + 1;
19393 /* On Darwin, the unwind routines are compiled without
19394 TARGET_ALTIVEC, and use save_world to save/restore the
19395 altivec registers when necessary. */
19396 if (DEFAULT_ABI == ABI_DARWIN && crtl->calls_eh_return
19397 && ! TARGET_ALTIVEC)
19398 return FIRST_ALTIVEC_REGNO + 20;
19400 /* Find lowest numbered live register. */
19401 for (i = FIRST_ALTIVEC_REGNO + 20; i <= LAST_ALTIVEC_REGNO; ++i)
19402 if (save_reg_p (i))
19403 break;
19405 return i;
19408 /* Return a 32-bit mask of the AltiVec registers we need to set in
19409 VRSAVE. Bit n of the return value is 1 if Vn is live. The MSB in
19410 the 32-bit word is 0. */
19412 static unsigned int
19413 compute_vrsave_mask (void)
19415 unsigned int i, mask = 0;
19417 /* On Darwin, the unwind routines are compiled without
19418 TARGET_ALTIVEC, and use save_world to save/restore the
19419 call-saved altivec registers when necessary. */
19420 if (DEFAULT_ABI == ABI_DARWIN && crtl->calls_eh_return
19421 && ! TARGET_ALTIVEC)
19422 mask |= 0xFFF;
19424 /* First, find out if we use _any_ altivec registers. */
19425 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
19426 if (df_regs_ever_live_p (i))
19427 mask |= ALTIVEC_REG_BIT (i);
19429 if (mask == 0)
19430 return mask;
19432 /* Next, remove the argument registers from the set. These must
19433 be in the VRSAVE mask set by the caller, so we don't need to add
19434 them in again. More importantly, the mask we compute here is
19435 used to generate CLOBBERs in the set_vrsave insn, and we do not
19436 wish the argument registers to die. */
19437 for (i = crtl->args.info.vregno - 1; i >= ALTIVEC_ARG_MIN_REG; --i)
19438 mask &= ~ALTIVEC_REG_BIT (i);
19440 /* Similarly, remove the return value from the set. */
19442 bool yes = false;
19443 diddle_return_value (is_altivec_return_reg, &yes);
19444 if (yes)
19445 mask &= ~ALTIVEC_REG_BIT (ALTIVEC_ARG_RETURN);
19448 return mask;
19451 /* For a very restricted set of circumstances, we can cut down the
19452 size of prologues/epilogues by calling our own save/restore-the-world
19453 routines. */
19455 static void
19456 compute_save_world_info (rs6000_stack_t *info_ptr)
19458 info_ptr->world_save_p = 1;
19459 info_ptr->world_save_p
19460 = (WORLD_SAVE_P (info_ptr)
19461 && DEFAULT_ABI == ABI_DARWIN
19462 && !cfun->has_nonlocal_label
19463 && info_ptr->first_fp_reg_save == FIRST_SAVED_FP_REGNO
19464 && info_ptr->first_gp_reg_save == FIRST_SAVED_GP_REGNO
19465 && info_ptr->first_altivec_reg_save == FIRST_SAVED_ALTIVEC_REGNO
19466 && info_ptr->cr_save_p);
19468 /* This will not work in conjunction with sibcalls. Make sure there
19469 are none. (This check is expensive, but seldom executed.) */
19470 if (WORLD_SAVE_P (info_ptr))
19472 rtx insn;
19473 for (insn = get_last_insn_anywhere (); insn; insn = PREV_INSN (insn))
19474 if (CALL_P (insn) && SIBLING_CALL_P (insn))
19476 info_ptr->world_save_p = 0;
19477 break;
19481 if (WORLD_SAVE_P (info_ptr))
19483 /* Even if we're not touching VRsave, make sure there's room on the
19484 stack for it, if it looks like we're calling SAVE_WORLD, which
19485 will attempt to save it. */
19486 info_ptr->vrsave_size = 4;
19488 /* If we are going to save the world, we need to save the link register too. */
19489 info_ptr->lr_save_p = 1;
19491 /* "Save" the VRsave register too if we're saving the world. */
19492 if (info_ptr->vrsave_mask == 0)
19493 info_ptr->vrsave_mask = compute_vrsave_mask ();
19495 /* Because the Darwin register save/restore routines only handle
19496 F14 .. F31 and V20 .. V31 as per the ABI, perform a consistency
19497 check. */
19498 gcc_assert (info_ptr->first_fp_reg_save >= FIRST_SAVED_FP_REGNO
19499 && (info_ptr->first_altivec_reg_save
19500 >= FIRST_SAVED_ALTIVEC_REGNO));
19502 return;
19506 static void
19507 is_altivec_return_reg (rtx reg, void *xyes)
19509 bool *yes = (bool *) xyes;
19510 if (REGNO (reg) == ALTIVEC_ARG_RETURN)
19511 *yes = true;
19515 /* Look for user-defined global regs in the range FIRST to LAST-1.
19516 We should not restore these, and so cannot use lmw or out-of-line
19517 restore functions if there are any. We also can't save them
19518 (well, emit frame notes for them), because frame unwinding during
19519 exception handling will restore saved registers. */
19521 static bool
19522 global_regs_p (unsigned first, unsigned last)
19524 while (first < last)
19525 if (global_regs[first++])
19526 return true;
19527 return false;
19530 /* Determine the strategy for savings/restoring registers. */
19532 enum {
19533 SAVRES_MULTIPLE = 0x1,
19534 SAVE_INLINE_FPRS = 0x2,
19535 SAVE_INLINE_GPRS = 0x4,
19536 REST_INLINE_FPRS = 0x8,
19537 REST_INLINE_GPRS = 0x10,
19538 SAVE_NOINLINE_GPRS_SAVES_LR = 0x20,
19539 SAVE_NOINLINE_FPRS_SAVES_LR = 0x40,
19540 REST_NOINLINE_FPRS_DOESNT_RESTORE_LR = 0x80,
19541 SAVE_INLINE_VRS = 0x100,
19542 REST_INLINE_VRS = 0x200
19545 static int
19546 rs6000_savres_strategy (rs6000_stack_t *info,
19547 bool using_static_chain_p)
19549 int strategy = 0;
19550 bool lr_save_p;
19552 if (TARGET_MULTIPLE
19553 && !TARGET_POWERPC64
19554 && !(TARGET_SPE_ABI && info->spe_64bit_regs_used)
19555 && info->first_gp_reg_save < 31
19556 && !global_regs_p (info->first_gp_reg_save, 32))
19557 strategy |= SAVRES_MULTIPLE;
19559 if (crtl->calls_eh_return
19560 || cfun->machine->ra_need_lr)
19561 strategy |= (SAVE_INLINE_FPRS | REST_INLINE_FPRS
19562 | SAVE_INLINE_GPRS | REST_INLINE_GPRS
19563 | SAVE_INLINE_VRS | REST_INLINE_VRS);
19565 if (info->first_fp_reg_save == 64
19566 /* The out-of-line FP routines use double-precision stores;
19567 we can't use those routines if we don't have such stores. */
19568 || (TARGET_HARD_FLOAT && !TARGET_DOUBLE_FLOAT)
19569 || global_regs_p (info->first_fp_reg_save, 64))
19570 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
19572 if (info->first_gp_reg_save == 32
19573 || (!(strategy & SAVRES_MULTIPLE)
19574 && global_regs_p (info->first_gp_reg_save, 32)))
19575 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
19577 if (info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1
19578 || global_regs_p (info->first_altivec_reg_save, LAST_ALTIVEC_REGNO + 1))
19579 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
19581 /* Define cutoff for using out-of-line functions to save registers. */
19582 if (DEFAULT_ABI == ABI_V4 || TARGET_ELF)
19584 if (!optimize_size)
19586 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
19587 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
19588 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
19590 else
19592 /* Prefer out-of-line restore if it will exit. */
19593 if (info->first_fp_reg_save > 61)
19594 strategy |= SAVE_INLINE_FPRS;
19595 if (info->first_gp_reg_save > 29)
19597 if (info->first_fp_reg_save == 64)
19598 strategy |= SAVE_INLINE_GPRS;
19599 else
19600 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
19602 if (info->first_altivec_reg_save == LAST_ALTIVEC_REGNO)
19603 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
19606 else if (DEFAULT_ABI == ABI_DARWIN)
19608 if (info->first_fp_reg_save > 60)
19609 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
19610 if (info->first_gp_reg_save > 29)
19611 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
19612 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
19614 else
19616 gcc_checking_assert (DEFAULT_ABI == ABI_AIX);
19617 if (info->first_fp_reg_save > 61)
19618 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
19619 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
19620 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
19623 /* Don't bother to try to save things out-of-line if r11 is occupied
19624 by the static chain. It would require too much fiddling and the
19625 static chain is rarely used anyway. FPRs are saved w.r.t the stack
19626 pointer on Darwin, and AIX uses r1 or r12. */
19627 if (using_static_chain_p && DEFAULT_ABI != ABI_AIX)
19628 strategy |= ((DEFAULT_ABI == ABI_DARWIN ? 0 : SAVE_INLINE_FPRS)
19629 | SAVE_INLINE_GPRS
19630 | SAVE_INLINE_VRS | REST_INLINE_VRS);
19632 /* We can only use the out-of-line routines to restore if we've
19633 saved all the registers from first_fp_reg_save in the prologue.
19634 Otherwise, we risk loading garbage. */
19635 if ((strategy & (SAVE_INLINE_FPRS | REST_INLINE_FPRS)) == SAVE_INLINE_FPRS)
19637 int i;
19639 for (i = info->first_fp_reg_save; i < 64; i++)
19640 if (!save_reg_p (i))
19642 strategy |= REST_INLINE_FPRS;
19643 break;
19647 /* If we are going to use store multiple, then don't even bother
19648 with the out-of-line routines, since the store-multiple
19649 instruction will always be smaller. */
19650 if ((strategy & SAVRES_MULTIPLE))
19651 strategy |= SAVE_INLINE_GPRS;
19653 /* info->lr_save_p isn't yet set if the only reason lr needs to be
19654 saved is an out-of-line save or restore. Set up the value for
19655 the next test (excluding out-of-line gpr restore). */
19656 lr_save_p = (info->lr_save_p
19657 || !(strategy & SAVE_INLINE_GPRS)
19658 || !(strategy & SAVE_INLINE_FPRS)
19659 || !(strategy & SAVE_INLINE_VRS)
19660 || !(strategy & REST_INLINE_FPRS)
19661 || !(strategy & REST_INLINE_VRS));
19663 /* The situation is more complicated with load multiple. We'd
19664 prefer to use the out-of-line routines for restores, since the
19665 "exit" out-of-line routines can handle the restore of LR and the
19666 frame teardown. However if doesn't make sense to use the
19667 out-of-line routine if that is the only reason we'd need to save
19668 LR, and we can't use the "exit" out-of-line gpr restore if we
19669 have saved some fprs; In those cases it is advantageous to use
19670 load multiple when available. */
19671 if ((strategy & SAVRES_MULTIPLE)
19672 && (!lr_save_p
19673 || info->first_fp_reg_save != 64))
19674 strategy |= REST_INLINE_GPRS;
19676 /* Saving CR interferes with the exit routines used on the SPE, so
19677 just punt here. */
19678 if (TARGET_SPE_ABI
19679 && info->spe_64bit_regs_used
19680 && info->cr_save_p)
19681 strategy |= REST_INLINE_GPRS;
19683 /* We can only use load multiple or the out-of-line routines to
19684 restore if we've used store multiple or out-of-line routines
19685 in the prologue, i.e. if we've saved all the registers from
19686 first_gp_reg_save. Otherwise, we risk loading garbage. */
19687 if ((strategy & (SAVE_INLINE_GPRS | REST_INLINE_GPRS | SAVRES_MULTIPLE))
19688 == SAVE_INLINE_GPRS)
19690 int i;
19692 for (i = info->first_gp_reg_save; i < 32; i++)
19693 if (!save_reg_p (i))
19695 strategy |= REST_INLINE_GPRS;
19696 break;
19700 if (TARGET_ELF && TARGET_64BIT)
19702 if (!(strategy & SAVE_INLINE_FPRS))
19703 strategy |= SAVE_NOINLINE_FPRS_SAVES_LR;
19704 else if (!(strategy & SAVE_INLINE_GPRS)
19705 && info->first_fp_reg_save == 64)
19706 strategy |= SAVE_NOINLINE_GPRS_SAVES_LR;
19708 else if (TARGET_AIX && !(strategy & REST_INLINE_FPRS))
19709 strategy |= REST_NOINLINE_FPRS_DOESNT_RESTORE_LR;
19711 if (TARGET_MACHO && !(strategy & SAVE_INLINE_FPRS))
19712 strategy |= SAVE_NOINLINE_FPRS_SAVES_LR;
19714 return strategy;
19717 /* Calculate the stack information for the current function. This is
19718 complicated by having two separate calling sequences, the AIX calling
19719 sequence and the V.4 calling sequence.
19721 AIX (and Darwin/Mac OS X) stack frames look like:
19722 32-bit 64-bit
19723 SP----> +---------------------------------------+
19724 | back chain to caller | 0 0
19725 +---------------------------------------+
19726 | saved CR | 4 8 (8-11)
19727 +---------------------------------------+
19728 | saved LR | 8 16
19729 +---------------------------------------+
19730 | reserved for compilers | 12 24
19731 +---------------------------------------+
19732 | reserved for binders | 16 32
19733 +---------------------------------------+
19734 | saved TOC pointer | 20 40
19735 +---------------------------------------+
19736 | Parameter save area (P) | 24 48
19737 +---------------------------------------+
19738 | Alloca space (A) | 24+P etc.
19739 +---------------------------------------+
19740 | Local variable space (L) | 24+P+A
19741 +---------------------------------------+
19742 | Float/int conversion temporary (X) | 24+P+A+L
19743 +---------------------------------------+
19744 | Save area for AltiVec registers (W) | 24+P+A+L+X
19745 +---------------------------------------+
19746 | AltiVec alignment padding (Y) | 24+P+A+L+X+W
19747 +---------------------------------------+
19748 | Save area for VRSAVE register (Z) | 24+P+A+L+X+W+Y
19749 +---------------------------------------+
19750 | Save area for GP registers (G) | 24+P+A+X+L+X+W+Y+Z
19751 +---------------------------------------+
19752 | Save area for FP registers (F) | 24+P+A+X+L+X+W+Y+Z+G
19753 +---------------------------------------+
19754 old SP->| back chain to caller's caller |
19755 +---------------------------------------+
19757 The required alignment for AIX configurations is two words (i.e., 8
19758 or 16 bytes).
19761 V.4 stack frames look like:
19763 SP----> +---------------------------------------+
19764 | back chain to caller | 0
19765 +---------------------------------------+
19766 | caller's saved LR | 4
19767 +---------------------------------------+
19768 | Parameter save area (P) | 8
19769 +---------------------------------------+
19770 | Alloca space (A) | 8+P
19771 +---------------------------------------+
19772 | Varargs save area (V) | 8+P+A
19773 +---------------------------------------+
19774 | Local variable space (L) | 8+P+A+V
19775 +---------------------------------------+
19776 | Float/int conversion temporary (X) | 8+P+A+V+L
19777 +---------------------------------------+
19778 | Save area for AltiVec registers (W) | 8+P+A+V+L+X
19779 +---------------------------------------+
19780 | AltiVec alignment padding (Y) | 8+P+A+V+L+X+W
19781 +---------------------------------------+
19782 | Save area for VRSAVE register (Z) | 8+P+A+V+L+X+W+Y
19783 +---------------------------------------+
19784 | SPE: area for 64-bit GP registers |
19785 +---------------------------------------+
19786 | SPE alignment padding |
19787 +---------------------------------------+
19788 | saved CR (C) | 8+P+A+V+L+X+W+Y+Z
19789 +---------------------------------------+
19790 | Save area for GP registers (G) | 8+P+A+V+L+X+W+Y+Z+C
19791 +---------------------------------------+
19792 | Save area for FP registers (F) | 8+P+A+V+L+X+W+Y+Z+C+G
19793 +---------------------------------------+
19794 old SP->| back chain to caller's caller |
19795 +---------------------------------------+
19797 The required alignment for V.4 is 16 bytes, or 8 bytes if -meabi is
19798 given. (But note below and in sysv4.h that we require only 8 and
19799 may round up the size of our stack frame anyways. The historical
19800 reason is early versions of powerpc-linux which didn't properly
19801 align the stack at program startup. A happy side-effect is that
19802 -mno-eabi libraries can be used with -meabi programs.)
19804 The EABI configuration defaults to the V.4 layout. However,
19805 the stack alignment requirements may differ. If -mno-eabi is not
19806 given, the required stack alignment is 8 bytes; if -mno-eabi is
19807 given, the required alignment is 16 bytes. (But see V.4 comment
19808 above.) */
19810 #ifndef ABI_STACK_BOUNDARY
19811 #define ABI_STACK_BOUNDARY STACK_BOUNDARY
19812 #endif
19814 static rs6000_stack_t *
19815 rs6000_stack_info (void)
19817 rs6000_stack_t *info_ptr = &stack_info;
19818 int reg_size = TARGET_32BIT ? 4 : 8;
19819 int ehrd_size;
19820 int save_align;
19821 int first_gp;
19822 HOST_WIDE_INT non_fixed_size;
19823 bool using_static_chain_p;
19825 if (reload_completed && info_ptr->reload_completed)
19826 return info_ptr;
19828 memset (info_ptr, 0, sizeof (*info_ptr));
19829 info_ptr->reload_completed = reload_completed;
19831 if (TARGET_SPE)
19833 /* Cache value so we don't rescan instruction chain over and over. */
19834 if (cfun->machine->insn_chain_scanned_p == 0)
19835 cfun->machine->insn_chain_scanned_p
19836 = spe_func_has_64bit_regs_p () + 1;
19837 info_ptr->spe_64bit_regs_used = cfun->machine->insn_chain_scanned_p - 1;
19840 /* Select which calling sequence. */
19841 info_ptr->abi = DEFAULT_ABI;
19843 /* Calculate which registers need to be saved & save area size. */
19844 info_ptr->first_gp_reg_save = first_reg_to_save ();
19845 /* Assume that we will have to save RS6000_PIC_OFFSET_TABLE_REGNUM,
19846 even if it currently looks like we won't. Reload may need it to
19847 get at a constant; if so, it will have already created a constant
19848 pool entry for it. */
19849 if (((TARGET_TOC && TARGET_MINIMAL_TOC)
19850 || (flag_pic == 1 && DEFAULT_ABI == ABI_V4)
19851 || (flag_pic && DEFAULT_ABI == ABI_DARWIN))
19852 && crtl->uses_const_pool
19853 && info_ptr->first_gp_reg_save > RS6000_PIC_OFFSET_TABLE_REGNUM)
19854 first_gp = RS6000_PIC_OFFSET_TABLE_REGNUM;
19855 else
19856 first_gp = info_ptr->first_gp_reg_save;
19858 info_ptr->gp_size = reg_size * (32 - first_gp);
19860 /* For the SPE, we have an additional upper 32-bits on each GPR.
19861 Ideally we should save the entire 64-bits only when the upper
19862 half is used in SIMD instructions. Since we only record
19863 registers live (not the size they are used in), this proves
19864 difficult because we'd have to traverse the instruction chain at
19865 the right time, taking reload into account. This is a real pain,
19866 so we opt to save the GPRs in 64-bits always if but one register
19867 gets used in 64-bits. Otherwise, all the registers in the frame
19868 get saved in 32-bits.
19870 So... since when we save all GPRs (except the SP) in 64-bits, the
19871 traditional GP save area will be empty. */
19872 if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
19873 info_ptr->gp_size = 0;
19875 info_ptr->first_fp_reg_save = first_fp_reg_to_save ();
19876 info_ptr->fp_size = 8 * (64 - info_ptr->first_fp_reg_save);
19878 info_ptr->first_altivec_reg_save = first_altivec_reg_to_save ();
19879 info_ptr->altivec_size = 16 * (LAST_ALTIVEC_REGNO + 1
19880 - info_ptr->first_altivec_reg_save);
19882 /* Does this function call anything? */
19883 info_ptr->calls_p = (! crtl->is_leaf
19884 || cfun->machine->ra_needs_full_frame);
19886 /* Determine if we need to save the condition code registers. */
19887 if (df_regs_ever_live_p (CR2_REGNO)
19888 || df_regs_ever_live_p (CR3_REGNO)
19889 || df_regs_ever_live_p (CR4_REGNO))
19891 info_ptr->cr_save_p = 1;
19892 if (DEFAULT_ABI == ABI_V4)
19893 info_ptr->cr_size = reg_size;
19896 /* If the current function calls __builtin_eh_return, then we need
19897 to allocate stack space for registers that will hold data for
19898 the exception handler. */
19899 if (crtl->calls_eh_return)
19901 unsigned int i;
19902 for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; ++i)
19903 continue;
19905 /* SPE saves EH registers in 64-bits. */
19906 ehrd_size = i * (TARGET_SPE_ABI
19907 && info_ptr->spe_64bit_regs_used != 0
19908 ? UNITS_PER_SPE_WORD : UNITS_PER_WORD);
19910 else
19911 ehrd_size = 0;
19913 /* Determine various sizes. */
19914 info_ptr->reg_size = reg_size;
19915 info_ptr->fixed_size = RS6000_SAVE_AREA;
19916 info_ptr->vars_size = RS6000_ALIGN (get_frame_size (), 8);
19917 info_ptr->parm_size = RS6000_ALIGN (crtl->outgoing_args_size,
19918 TARGET_ALTIVEC ? 16 : 8);
19919 if (FRAME_GROWS_DOWNWARD)
19920 info_ptr->vars_size
19921 += RS6000_ALIGN (info_ptr->fixed_size + info_ptr->vars_size
19922 + info_ptr->parm_size,
19923 ABI_STACK_BOUNDARY / BITS_PER_UNIT)
19924 - (info_ptr->fixed_size + info_ptr->vars_size
19925 + info_ptr->parm_size);
19927 if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
19928 info_ptr->spe_gp_size = 8 * (32 - first_gp);
19929 else
19930 info_ptr->spe_gp_size = 0;
19932 if (TARGET_ALTIVEC_ABI)
19933 info_ptr->vrsave_mask = compute_vrsave_mask ();
19934 else
19935 info_ptr->vrsave_mask = 0;
19937 if (TARGET_ALTIVEC_VRSAVE && info_ptr->vrsave_mask)
19938 info_ptr->vrsave_size = 4;
19939 else
19940 info_ptr->vrsave_size = 0;
19942 compute_save_world_info (info_ptr);
19944 /* Calculate the offsets. */
19945 switch (DEFAULT_ABI)
19947 case ABI_NONE:
19948 default:
19949 gcc_unreachable ();
19951 case ABI_AIX:
19952 case ABI_DARWIN:
19953 info_ptr->fp_save_offset = - info_ptr->fp_size;
19954 info_ptr->gp_save_offset = info_ptr->fp_save_offset - info_ptr->gp_size;
19956 if (TARGET_ALTIVEC_ABI)
19958 info_ptr->vrsave_save_offset
19959 = info_ptr->gp_save_offset - info_ptr->vrsave_size;
19961 /* Align stack so vector save area is on a quadword boundary.
19962 The padding goes above the vectors. */
19963 if (info_ptr->altivec_size != 0)
19964 info_ptr->altivec_padding_size
19965 = info_ptr->vrsave_save_offset & 0xF;
19966 else
19967 info_ptr->altivec_padding_size = 0;
19969 info_ptr->altivec_save_offset
19970 = info_ptr->vrsave_save_offset
19971 - info_ptr->altivec_padding_size
19972 - info_ptr->altivec_size;
19973 gcc_assert (info_ptr->altivec_size == 0
19974 || info_ptr->altivec_save_offset % 16 == 0);
19976 /* Adjust for AltiVec case. */
19977 info_ptr->ehrd_offset = info_ptr->altivec_save_offset - ehrd_size;
19979 else
19980 info_ptr->ehrd_offset = info_ptr->gp_save_offset - ehrd_size;
19981 info_ptr->cr_save_offset = reg_size; /* first word when 64-bit. */
19982 info_ptr->lr_save_offset = 2*reg_size;
19983 break;
19985 case ABI_V4:
19986 info_ptr->fp_save_offset = - info_ptr->fp_size;
19987 info_ptr->gp_save_offset = info_ptr->fp_save_offset - info_ptr->gp_size;
19988 info_ptr->cr_save_offset = info_ptr->gp_save_offset - info_ptr->cr_size;
19990 if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
19992 /* Align stack so SPE GPR save area is aligned on a
19993 double-word boundary. */
19994 if (info_ptr->spe_gp_size != 0 && info_ptr->cr_save_offset != 0)
19995 info_ptr->spe_padding_size
19996 = 8 - (-info_ptr->cr_save_offset % 8);
19997 else
19998 info_ptr->spe_padding_size = 0;
20000 info_ptr->spe_gp_save_offset
20001 = info_ptr->cr_save_offset
20002 - info_ptr->spe_padding_size
20003 - info_ptr->spe_gp_size;
20005 /* Adjust for SPE case. */
20006 info_ptr->ehrd_offset = info_ptr->spe_gp_save_offset;
20008 else if (TARGET_ALTIVEC_ABI)
20010 info_ptr->vrsave_save_offset
20011 = info_ptr->cr_save_offset - info_ptr->vrsave_size;
20013 /* Align stack so vector save area is on a quadword boundary. */
20014 if (info_ptr->altivec_size != 0)
20015 info_ptr->altivec_padding_size
20016 = 16 - (-info_ptr->vrsave_save_offset % 16);
20017 else
20018 info_ptr->altivec_padding_size = 0;
20020 info_ptr->altivec_save_offset
20021 = info_ptr->vrsave_save_offset
20022 - info_ptr->altivec_padding_size
20023 - info_ptr->altivec_size;
20025 /* Adjust for AltiVec case. */
20026 info_ptr->ehrd_offset = info_ptr->altivec_save_offset;
20028 else
20029 info_ptr->ehrd_offset = info_ptr->cr_save_offset;
20030 info_ptr->ehrd_offset -= ehrd_size;
20031 info_ptr->lr_save_offset = reg_size;
20032 break;
20035 save_align = (TARGET_ALTIVEC_ABI || DEFAULT_ABI == ABI_DARWIN) ? 16 : 8;
20036 info_ptr->save_size = RS6000_ALIGN (info_ptr->fp_size
20037 + info_ptr->gp_size
20038 + info_ptr->altivec_size
20039 + info_ptr->altivec_padding_size
20040 + info_ptr->spe_gp_size
20041 + info_ptr->spe_padding_size
20042 + ehrd_size
20043 + info_ptr->cr_size
20044 + info_ptr->vrsave_size,
20045 save_align);
20047 non_fixed_size = (info_ptr->vars_size
20048 + info_ptr->parm_size
20049 + info_ptr->save_size);
20051 info_ptr->total_size = RS6000_ALIGN (non_fixed_size + info_ptr->fixed_size,
20052 ABI_STACK_BOUNDARY / BITS_PER_UNIT);
20054 /* Determine if we need to save the link register. */
20055 if (info_ptr->calls_p
20056 || (DEFAULT_ABI == ABI_AIX
20057 && crtl->profile
20058 && !TARGET_PROFILE_KERNEL)
20059 || (DEFAULT_ABI == ABI_V4 && cfun->calls_alloca)
20060 #ifdef TARGET_RELOCATABLE
20061 || (TARGET_RELOCATABLE && (get_pool_size () != 0))
20062 #endif
20063 || rs6000_ra_ever_killed ())
20064 info_ptr->lr_save_p = 1;
20066 using_static_chain_p = (cfun->static_chain_decl != NULL_TREE
20067 && df_regs_ever_live_p (STATIC_CHAIN_REGNUM)
20068 && call_used_regs[STATIC_CHAIN_REGNUM]);
20069 info_ptr->savres_strategy = rs6000_savres_strategy (info_ptr,
20070 using_static_chain_p);
20072 if (!(info_ptr->savres_strategy & SAVE_INLINE_GPRS)
20073 || !(info_ptr->savres_strategy & SAVE_INLINE_FPRS)
20074 || !(info_ptr->savres_strategy & SAVE_INLINE_VRS)
20075 || !(info_ptr->savres_strategy & REST_INLINE_GPRS)
20076 || !(info_ptr->savres_strategy & REST_INLINE_FPRS)
20077 || !(info_ptr->savres_strategy & REST_INLINE_VRS))
20078 info_ptr->lr_save_p = 1;
20080 if (info_ptr->lr_save_p)
20081 df_set_regs_ever_live (LR_REGNO, true);
20083 /* Determine if we need to allocate any stack frame:
20085 For AIX we need to push the stack if a frame pointer is needed
20086 (because the stack might be dynamically adjusted), if we are
20087 debugging, if we make calls, or if the sum of fp_save, gp_save,
20088 and local variables are more than the space needed to save all
20089 non-volatile registers: 32-bit: 18*8 + 19*4 = 220 or 64-bit: 18*8
20090 + 18*8 = 288 (GPR13 reserved).
20092 For V.4 we don't have the stack cushion that AIX uses, but assume
20093 that the debugger can handle stackless frames. */
20095 if (info_ptr->calls_p)
20096 info_ptr->push_p = 1;
20098 else if (DEFAULT_ABI == ABI_V4)
20099 info_ptr->push_p = non_fixed_size != 0;
20101 else if (frame_pointer_needed)
20102 info_ptr->push_p = 1;
20104 else if (TARGET_XCOFF && write_symbols != NO_DEBUG)
20105 info_ptr->push_p = 1;
20107 else
20108 info_ptr->push_p = non_fixed_size > (TARGET_32BIT ? 220 : 288);
20110 /* Zero offsets if we're not saving those registers. */
20111 if (info_ptr->fp_size == 0)
20112 info_ptr->fp_save_offset = 0;
20114 if (info_ptr->gp_size == 0)
20115 info_ptr->gp_save_offset = 0;
20117 if (! TARGET_ALTIVEC_ABI || info_ptr->altivec_size == 0)
20118 info_ptr->altivec_save_offset = 0;
20120 /* Zero VRSAVE offset if not saved and restored. */
20121 if (! TARGET_ALTIVEC_VRSAVE || info_ptr->vrsave_mask == 0)
20122 info_ptr->vrsave_save_offset = 0;
20124 if (! TARGET_SPE_ABI
20125 || info_ptr->spe_64bit_regs_used == 0
20126 || info_ptr->spe_gp_size == 0)
20127 info_ptr->spe_gp_save_offset = 0;
20129 if (! info_ptr->lr_save_p)
20130 info_ptr->lr_save_offset = 0;
20132 if (! info_ptr->cr_save_p)
20133 info_ptr->cr_save_offset = 0;
20135 return info_ptr;
20138 /* Return true if the current function uses any GPRs in 64-bit SIMD
20139 mode. */
20141 static bool
20142 spe_func_has_64bit_regs_p (void)
20144 rtx insns, insn;
20146 /* Functions that save and restore all the call-saved registers will
20147 need to save/restore the registers in 64-bits. */
20148 if (crtl->calls_eh_return
20149 || cfun->calls_setjmp
20150 || crtl->has_nonlocal_goto)
20151 return true;
20153 insns = get_insns ();
20155 for (insn = NEXT_INSN (insns); insn != NULL_RTX; insn = NEXT_INSN (insn))
20157 if (INSN_P (insn))
20159 rtx i;
20161 /* FIXME: This should be implemented with attributes...
20163 (set_attr "spe64" "true")....then,
20164 if (get_spe64(insn)) return true;
20166 It's the only reliable way to do the stuff below. */
20168 i = PATTERN (insn);
20169 if (GET_CODE (i) == SET)
20171 enum machine_mode mode = GET_MODE (SET_SRC (i));
20173 if (SPE_VECTOR_MODE (mode))
20174 return true;
20175 if (TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode))
20176 return true;
20181 return false;
20184 static void
20185 debug_stack_info (rs6000_stack_t *info)
20187 const char *abi_string;
20189 if (! info)
20190 info = rs6000_stack_info ();
20192 fprintf (stderr, "\nStack information for function %s:\n",
20193 ((current_function_decl && DECL_NAME (current_function_decl))
20194 ? IDENTIFIER_POINTER (DECL_NAME (current_function_decl))
20195 : "<unknown>"));
20197 switch (info->abi)
20199 default: abi_string = "Unknown"; break;
20200 case ABI_NONE: abi_string = "NONE"; break;
20201 case ABI_AIX: abi_string = "AIX"; break;
20202 case ABI_DARWIN: abi_string = "Darwin"; break;
20203 case ABI_V4: abi_string = "V.4"; break;
20206 fprintf (stderr, "\tABI = %5s\n", abi_string);
20208 if (TARGET_ALTIVEC_ABI)
20209 fprintf (stderr, "\tALTIVEC ABI extensions enabled.\n");
20211 if (TARGET_SPE_ABI)
20212 fprintf (stderr, "\tSPE ABI extensions enabled.\n");
20214 if (info->first_gp_reg_save != 32)
20215 fprintf (stderr, "\tfirst_gp_reg_save = %5d\n", info->first_gp_reg_save);
20217 if (info->first_fp_reg_save != 64)
20218 fprintf (stderr, "\tfirst_fp_reg_save = %5d\n", info->first_fp_reg_save);
20220 if (info->first_altivec_reg_save <= LAST_ALTIVEC_REGNO)
20221 fprintf (stderr, "\tfirst_altivec_reg_save = %5d\n",
20222 info->first_altivec_reg_save);
20224 if (info->lr_save_p)
20225 fprintf (stderr, "\tlr_save_p = %5d\n", info->lr_save_p);
20227 if (info->cr_save_p)
20228 fprintf (stderr, "\tcr_save_p = %5d\n", info->cr_save_p);
20230 if (info->vrsave_mask)
20231 fprintf (stderr, "\tvrsave_mask = 0x%x\n", info->vrsave_mask);
20233 if (info->push_p)
20234 fprintf (stderr, "\tpush_p = %5d\n", info->push_p);
20236 if (info->calls_p)
20237 fprintf (stderr, "\tcalls_p = %5d\n", info->calls_p);
20239 if (info->gp_save_offset)
20240 fprintf (stderr, "\tgp_save_offset = %5d\n", info->gp_save_offset);
20242 if (info->fp_save_offset)
20243 fprintf (stderr, "\tfp_save_offset = %5d\n", info->fp_save_offset);
20245 if (info->altivec_save_offset)
20246 fprintf (stderr, "\taltivec_save_offset = %5d\n",
20247 info->altivec_save_offset);
20249 if (info->spe_gp_save_offset)
20250 fprintf (stderr, "\tspe_gp_save_offset = %5d\n",
20251 info->spe_gp_save_offset);
20253 if (info->vrsave_save_offset)
20254 fprintf (stderr, "\tvrsave_save_offset = %5d\n",
20255 info->vrsave_save_offset);
20257 if (info->lr_save_offset)
20258 fprintf (stderr, "\tlr_save_offset = %5d\n", info->lr_save_offset);
20260 if (info->cr_save_offset)
20261 fprintf (stderr, "\tcr_save_offset = %5d\n", info->cr_save_offset);
20263 if (info->varargs_save_offset)
20264 fprintf (stderr, "\tvarargs_save_offset = %5d\n", info->varargs_save_offset);
20266 if (info->total_size)
20267 fprintf (stderr, "\ttotal_size = "HOST_WIDE_INT_PRINT_DEC"\n",
20268 info->total_size);
20270 if (info->vars_size)
20271 fprintf (stderr, "\tvars_size = "HOST_WIDE_INT_PRINT_DEC"\n",
20272 info->vars_size);
20274 if (info->parm_size)
20275 fprintf (stderr, "\tparm_size = %5d\n", info->parm_size);
20277 if (info->fixed_size)
20278 fprintf (stderr, "\tfixed_size = %5d\n", info->fixed_size);
20280 if (info->gp_size)
20281 fprintf (stderr, "\tgp_size = %5d\n", info->gp_size);
20283 if (info->spe_gp_size)
20284 fprintf (stderr, "\tspe_gp_size = %5d\n", info->spe_gp_size);
20286 if (info->fp_size)
20287 fprintf (stderr, "\tfp_size = %5d\n", info->fp_size);
20289 if (info->altivec_size)
20290 fprintf (stderr, "\taltivec_size = %5d\n", info->altivec_size);
20292 if (info->vrsave_size)
20293 fprintf (stderr, "\tvrsave_size = %5d\n", info->vrsave_size);
20295 if (info->altivec_padding_size)
20296 fprintf (stderr, "\taltivec_padding_size= %5d\n",
20297 info->altivec_padding_size);
20299 if (info->spe_padding_size)
20300 fprintf (stderr, "\tspe_padding_size = %5d\n",
20301 info->spe_padding_size);
20303 if (info->cr_size)
20304 fprintf (stderr, "\tcr_size = %5d\n", info->cr_size);
20306 if (info->save_size)
20307 fprintf (stderr, "\tsave_size = %5d\n", info->save_size);
20309 if (info->reg_size != 4)
20310 fprintf (stderr, "\treg_size = %5d\n", info->reg_size);
20312 fprintf (stderr, "\tsave-strategy = %04x\n", info->savres_strategy);
20314 fprintf (stderr, "\n");
20318 rs6000_return_addr (int count, rtx frame)
20320 /* Currently we don't optimize very well between prolog and body
20321 code and for PIC code the code can be actually quite bad, so
20322 don't try to be too clever here. */
20323 if (count != 0 || (DEFAULT_ABI != ABI_AIX && flag_pic))
20325 cfun->machine->ra_needs_full_frame = 1;
20327 return
20328 gen_rtx_MEM
20329 (Pmode,
20330 memory_address
20331 (Pmode,
20332 plus_constant (Pmode,
20333 copy_to_reg
20334 (gen_rtx_MEM (Pmode,
20335 memory_address (Pmode, frame))),
20336 RETURN_ADDRESS_OFFSET)));
20339 cfun->machine->ra_need_lr = 1;
20340 return get_hard_reg_initial_val (Pmode, LR_REGNO);
20343 /* Say whether a function is a candidate for sibcall handling or not. */
20345 static bool
20346 rs6000_function_ok_for_sibcall (tree decl, tree exp)
20348 tree fntype;
20350 if (decl)
20351 fntype = TREE_TYPE (decl);
20352 else
20353 fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (exp)));
20355 /* We can't do it if the called function has more vector parameters
20356 than the current function; there's nowhere to put the VRsave code. */
20357 if (TARGET_ALTIVEC_ABI
20358 && TARGET_ALTIVEC_VRSAVE
20359 && !(decl && decl == current_function_decl))
20361 function_args_iterator args_iter;
20362 tree type;
20363 int nvreg = 0;
20365 /* Functions with vector parameters are required to have a
20366 prototype, so the argument type info must be available
20367 here. */
20368 FOREACH_FUNCTION_ARGS(fntype, type, args_iter)
20369 if (TREE_CODE (type) == VECTOR_TYPE
20370 && ALTIVEC_OR_VSX_VECTOR_MODE (TYPE_MODE (type)))
20371 nvreg++;
20373 FOREACH_FUNCTION_ARGS(TREE_TYPE (current_function_decl), type, args_iter)
20374 if (TREE_CODE (type) == VECTOR_TYPE
20375 && ALTIVEC_OR_VSX_VECTOR_MODE (TYPE_MODE (type)))
20376 nvreg--;
20378 if (nvreg > 0)
20379 return false;
20382 /* Under the AIX ABI we can't allow calls to non-local functions,
20383 because the callee may have a different TOC pointer to the
20384 caller and there's no way to ensure we restore the TOC when we
20385 return. With the secure-plt SYSV ABI we can't make non-local
20386 calls when -fpic/PIC because the plt call stubs use r30. */
20387 if (DEFAULT_ABI == ABI_DARWIN
20388 || (DEFAULT_ABI == ABI_AIX
20389 && decl
20390 && !DECL_EXTERNAL (decl)
20391 && (*targetm.binds_local_p) (decl))
20392 || (DEFAULT_ABI == ABI_V4
20393 && (!TARGET_SECURE_PLT
20394 || !flag_pic
20395 || (decl
20396 && (*targetm.binds_local_p) (decl)))))
20398 tree attr_list = TYPE_ATTRIBUTES (fntype);
20400 if (!lookup_attribute ("longcall", attr_list)
20401 || lookup_attribute ("shortcall", attr_list))
20402 return true;
20405 return false;
20408 static int
20409 rs6000_ra_ever_killed (void)
20411 rtx top;
20412 rtx reg;
20413 rtx insn;
20415 if (cfun->is_thunk)
20416 return 0;
20418 if (cfun->machine->lr_save_state)
20419 return cfun->machine->lr_save_state - 1;
20421 /* regs_ever_live has LR marked as used if any sibcalls are present,
20422 but this should not force saving and restoring in the
20423 pro/epilogue. Likewise, reg_set_between_p thinks a sibcall
20424 clobbers LR, so that is inappropriate. */
20426 /* Also, the prologue can generate a store into LR that
20427 doesn't really count, like this:
20429 move LR->R0
20430 bcl to set PIC register
20431 move LR->R31
20432 move R0->LR
20434 When we're called from the epilogue, we need to avoid counting
20435 this as a store. */
20437 push_topmost_sequence ();
20438 top = get_insns ();
20439 pop_topmost_sequence ();
20440 reg = gen_rtx_REG (Pmode, LR_REGNO);
20442 for (insn = NEXT_INSN (top); insn != NULL_RTX; insn = NEXT_INSN (insn))
20444 if (INSN_P (insn))
20446 if (CALL_P (insn))
20448 if (!SIBLING_CALL_P (insn))
20449 return 1;
20451 else if (find_regno_note (insn, REG_INC, LR_REGNO))
20452 return 1;
20453 else if (set_of (reg, insn) != NULL_RTX
20454 && !prologue_epilogue_contains (insn))
20455 return 1;
20458 return 0;
20461 /* Emit instructions needed to load the TOC register.
20462 This is only needed when TARGET_TOC, TARGET_MINIMAL_TOC, and there is
20463 a constant pool; or for SVR4 -fpic. */
20465 void
20466 rs6000_emit_load_toc_table (int fromprolog)
20468 rtx dest;
20469 dest = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
20471 if (TARGET_ELF && TARGET_SECURE_PLT && DEFAULT_ABI != ABI_AIX && flag_pic)
20473 char buf[30];
20474 rtx lab, tmp1, tmp2, got;
20476 lab = gen_label_rtx ();
20477 ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (lab));
20478 lab = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
20479 if (flag_pic == 2)
20480 got = gen_rtx_SYMBOL_REF (Pmode, toc_label_name);
20481 else
20482 got = rs6000_got_sym ();
20483 tmp1 = tmp2 = dest;
20484 if (!fromprolog)
20486 tmp1 = gen_reg_rtx (Pmode);
20487 tmp2 = gen_reg_rtx (Pmode);
20489 emit_insn (gen_load_toc_v4_PIC_1 (lab));
20490 emit_move_insn (tmp1, gen_rtx_REG (Pmode, LR_REGNO));
20491 emit_insn (gen_load_toc_v4_PIC_3b (tmp2, tmp1, got, lab));
20492 emit_insn (gen_load_toc_v4_PIC_3c (dest, tmp2, got, lab));
20494 else if (TARGET_ELF && DEFAULT_ABI == ABI_V4 && flag_pic == 1)
20496 emit_insn (gen_load_toc_v4_pic_si ());
20497 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
20499 else if (TARGET_ELF && DEFAULT_ABI != ABI_AIX && flag_pic == 2)
20501 char buf[30];
20502 rtx temp0 = (fromprolog
20503 ? gen_rtx_REG (Pmode, 0)
20504 : gen_reg_rtx (Pmode));
20506 if (fromprolog)
20508 rtx symF, symL;
20510 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
20511 symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
20513 ASM_GENERATE_INTERNAL_LABEL (buf, "LCL", rs6000_pic_labelno);
20514 symL = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
20516 emit_insn (gen_load_toc_v4_PIC_1 (symF));
20517 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
20518 emit_insn (gen_load_toc_v4_PIC_2 (temp0, dest, symL, symF));
20520 else
20522 rtx tocsym, lab;
20524 tocsym = gen_rtx_SYMBOL_REF (Pmode, toc_label_name);
20525 lab = gen_label_rtx ();
20526 emit_insn (gen_load_toc_v4_PIC_1b (tocsym, lab));
20527 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
20528 if (TARGET_LINK_STACK)
20529 emit_insn (gen_addsi3 (dest, dest, GEN_INT (4)));
20530 emit_move_insn (temp0, gen_rtx_MEM (Pmode, dest));
20532 emit_insn (gen_addsi3 (dest, temp0, dest));
20534 else if (TARGET_ELF && !TARGET_AIX && flag_pic == 0 && TARGET_MINIMAL_TOC)
20536 /* This is for AIX code running in non-PIC ELF32. */
20537 char buf[30];
20538 rtx realsym;
20539 ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
20540 realsym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
20542 emit_insn (gen_elf_high (dest, realsym));
20543 emit_insn (gen_elf_low (dest, dest, realsym));
20545 else
20547 gcc_assert (DEFAULT_ABI == ABI_AIX);
20549 if (TARGET_32BIT)
20550 emit_insn (gen_load_toc_aix_si (dest));
20551 else
20552 emit_insn (gen_load_toc_aix_di (dest));
20556 /* Emit instructions to restore the link register after determining where
20557 its value has been stored. */
20559 void
20560 rs6000_emit_eh_reg_restore (rtx source, rtx scratch)
20562 rs6000_stack_t *info = rs6000_stack_info ();
20563 rtx operands[2];
20565 operands[0] = source;
20566 operands[1] = scratch;
20568 if (info->lr_save_p)
20570 rtx frame_rtx = stack_pointer_rtx;
20571 HOST_WIDE_INT sp_offset = 0;
20572 rtx tmp;
20574 if (frame_pointer_needed
20575 || cfun->calls_alloca
20576 || info->total_size > 32767)
20578 tmp = gen_frame_mem (Pmode, frame_rtx);
20579 emit_move_insn (operands[1], tmp);
20580 frame_rtx = operands[1];
20582 else if (info->push_p)
20583 sp_offset = info->total_size;
20585 tmp = plus_constant (Pmode, frame_rtx,
20586 info->lr_save_offset + sp_offset);
20587 tmp = gen_frame_mem (Pmode, tmp);
20588 emit_move_insn (tmp, operands[0]);
20590 else
20591 emit_move_insn (gen_rtx_REG (Pmode, LR_REGNO), operands[0]);
20593 /* Freeze lr_save_p. We've just emitted rtl that depends on the
20594 state of lr_save_p so any change from here on would be a bug. In
20595 particular, stop rs6000_ra_ever_killed from considering the SET
20596 of lr we may have added just above. */
20597 cfun->machine->lr_save_state = info->lr_save_p + 1;
20600 static GTY(()) alias_set_type set = -1;
20602 alias_set_type
20603 get_TOC_alias_set (void)
20605 if (set == -1)
20606 set = new_alias_set ();
20607 return set;
20610 /* This returns nonzero if the current function uses the TOC. This is
20611 determined by the presence of (use (unspec ... UNSPEC_TOC)), which
20612 is generated by the ABI_V4 load_toc_* patterns. */
20613 #if TARGET_ELF
20614 static int
20615 uses_TOC (void)
20617 rtx insn;
20619 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
20620 if (INSN_P (insn))
20622 rtx pat = PATTERN (insn);
20623 int i;
20625 if (GET_CODE (pat) == PARALLEL)
20626 for (i = 0; i < XVECLEN (pat, 0); i++)
20628 rtx sub = XVECEXP (pat, 0, i);
20629 if (GET_CODE (sub) == USE)
20631 sub = XEXP (sub, 0);
20632 if (GET_CODE (sub) == UNSPEC
20633 && XINT (sub, 1) == UNSPEC_TOC)
20634 return 1;
20638 return 0;
20640 #endif
20643 create_TOC_reference (rtx symbol, rtx largetoc_reg)
20645 rtx tocrel, tocreg, hi;
20647 if (TARGET_DEBUG_ADDR)
20649 if (GET_CODE (symbol) == SYMBOL_REF)
20650 fprintf (stderr, "\ncreate_TOC_reference, (symbol_ref %s)\n",
20651 XSTR (symbol, 0));
20652 else
20654 fprintf (stderr, "\ncreate_TOC_reference, code %s:\n",
20655 GET_RTX_NAME (GET_CODE (symbol)));
20656 debug_rtx (symbol);
20660 if (!can_create_pseudo_p ())
20661 df_set_regs_ever_live (TOC_REGISTER, true);
20663 tocreg = gen_rtx_REG (Pmode, TOC_REGISTER);
20664 tocrel = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, symbol, tocreg), UNSPEC_TOCREL);
20665 if (TARGET_CMODEL == CMODEL_SMALL || can_create_pseudo_p ())
20666 return tocrel;
20668 hi = gen_rtx_HIGH (Pmode, copy_rtx (tocrel));
20669 if (largetoc_reg != NULL)
20671 emit_move_insn (largetoc_reg, hi);
20672 hi = largetoc_reg;
20674 return gen_rtx_LO_SUM (Pmode, hi, tocrel);
20677 /* Issue assembly directives that create a reference to the given DWARF
20678 FRAME_TABLE_LABEL from the current function section. */
20679 void
20680 rs6000_aix_asm_output_dwarf_table_ref (char * frame_table_label)
20682 fprintf (asm_out_file, "\t.ref %s\n",
20683 (* targetm.strip_name_encoding) (frame_table_label));
20686 /* This ties together stack memory (MEM with an alias set of frame_alias_set)
20687 and the change to the stack pointer. */
20689 static void
20690 rs6000_emit_stack_tie (rtx fp, bool hard_frame_needed)
20692 rtvec p;
20693 int i;
20694 rtx regs[3];
20696 i = 0;
20697 regs[i++] = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
20698 if (hard_frame_needed)
20699 regs[i++] = gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM);
20700 if (!(REGNO (fp) == STACK_POINTER_REGNUM
20701 || (hard_frame_needed
20702 && REGNO (fp) == HARD_FRAME_POINTER_REGNUM)))
20703 regs[i++] = fp;
20705 p = rtvec_alloc (i);
20706 while (--i >= 0)
20708 rtx mem = gen_frame_mem (BLKmode, regs[i]);
20709 RTVEC_ELT (p, i) = gen_rtx_SET (VOIDmode, mem, const0_rtx);
20712 emit_insn (gen_stack_tie (gen_rtx_PARALLEL (VOIDmode, p)));
20715 /* Emit the correct code for allocating stack space, as insns.
20716 If COPY_REG, make sure a copy of the old frame is left there.
20717 The generated code may use hard register 0 as a temporary. */
20719 static void
20720 rs6000_emit_allocate_stack (HOST_WIDE_INT size, rtx copy_reg, int copy_off)
20722 rtx insn;
20723 rtx stack_reg = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
20724 rtx tmp_reg = gen_rtx_REG (Pmode, 0);
20725 rtx todec = gen_int_mode (-size, Pmode);
20726 rtx par, set, mem;
20728 if (INTVAL (todec) != -size)
20730 warning (0, "stack frame too large");
20731 emit_insn (gen_trap ());
20732 return;
20735 if (crtl->limit_stack)
20737 if (REG_P (stack_limit_rtx)
20738 && REGNO (stack_limit_rtx) > 1
20739 && REGNO (stack_limit_rtx) <= 31)
20741 emit_insn (gen_add3_insn (tmp_reg, stack_limit_rtx, GEN_INT (size)));
20742 emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
20743 const0_rtx));
20745 else if (GET_CODE (stack_limit_rtx) == SYMBOL_REF
20746 && TARGET_32BIT
20747 && DEFAULT_ABI == ABI_V4)
20749 rtx toload = gen_rtx_CONST (VOIDmode,
20750 gen_rtx_PLUS (Pmode,
20751 stack_limit_rtx,
20752 GEN_INT (size)));
20754 emit_insn (gen_elf_high (tmp_reg, toload));
20755 emit_insn (gen_elf_low (tmp_reg, tmp_reg, toload));
20756 emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
20757 const0_rtx));
20759 else
20760 warning (0, "stack limit expression is not supported");
20763 if (copy_reg)
20765 if (copy_off != 0)
20766 emit_insn (gen_add3_insn (copy_reg, stack_reg, GEN_INT (copy_off)));
20767 else
20768 emit_move_insn (copy_reg, stack_reg);
20771 if (size > 32767)
20773 /* Need a note here so that try_split doesn't get confused. */
20774 if (get_last_insn () == NULL_RTX)
20775 emit_note (NOTE_INSN_DELETED);
20776 insn = emit_move_insn (tmp_reg, todec);
20777 try_split (PATTERN (insn), insn, 0);
20778 todec = tmp_reg;
20781 insn = emit_insn (TARGET_32BIT
20782 ? gen_movsi_update_stack (stack_reg, stack_reg,
20783 todec, stack_reg)
20784 : gen_movdi_di_update_stack (stack_reg, stack_reg,
20785 todec, stack_reg));
20786 /* Since we didn't use gen_frame_mem to generate the MEM, grab
20787 it now and set the alias set/attributes. The above gen_*_update
20788 calls will generate a PARALLEL with the MEM set being the first
20789 operation. */
20790 par = PATTERN (insn);
20791 gcc_assert (GET_CODE (par) == PARALLEL);
20792 set = XVECEXP (par, 0, 0);
20793 gcc_assert (GET_CODE (set) == SET);
20794 mem = SET_DEST (set);
20795 gcc_assert (MEM_P (mem));
20796 MEM_NOTRAP_P (mem) = 1;
20797 set_mem_alias_set (mem, get_frame_alias_set ());
20799 RTX_FRAME_RELATED_P (insn) = 1;
20800 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
20801 gen_rtx_SET (VOIDmode, stack_reg,
20802 gen_rtx_PLUS (Pmode, stack_reg,
20803 GEN_INT (-size))));
20806 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
20808 #if PROBE_INTERVAL > 32768
20809 #error Cannot use indexed addressing mode for stack probing
20810 #endif
20812 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
20813 inclusive. These are offsets from the current stack pointer. */
20815 static void
20816 rs6000_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
20818 /* See if we have a constant small number of probes to generate. If so,
20819 that's the easy case. */
20820 if (first + size <= 32768)
20822 HOST_WIDE_INT i;
20824 /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
20825 it exceeds SIZE. If only one probe is needed, this will not
20826 generate any code. Then probe at FIRST + SIZE. */
20827 for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
20828 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
20829 -(first + i)));
20831 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
20832 -(first + size)));
20835 /* Otherwise, do the same as above, but in a loop. Note that we must be
20836 extra careful with variables wrapping around because we might be at
20837 the very top (or the very bottom) of the address space and we have
20838 to be able to handle this case properly; in particular, we use an
20839 equality test for the loop condition. */
20840 else
20842 HOST_WIDE_INT rounded_size;
20843 rtx r12 = gen_rtx_REG (Pmode, 12);
20844 rtx r0 = gen_rtx_REG (Pmode, 0);
20846 /* Sanity check for the addressing mode we're going to use. */
20847 gcc_assert (first <= 32768);
20849 /* Step 1: round SIZE to the previous multiple of the interval. */
20851 rounded_size = size & -PROBE_INTERVAL;
20854 /* Step 2: compute initial and final value of the loop counter. */
20856 /* TEST_ADDR = SP + FIRST. */
20857 emit_insn (gen_rtx_SET (VOIDmode, r12,
20858 plus_constant (Pmode, stack_pointer_rtx,
20859 -first)));
20861 /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE. */
20862 if (rounded_size > 32768)
20864 emit_move_insn (r0, GEN_INT (-rounded_size));
20865 emit_insn (gen_rtx_SET (VOIDmode, r0,
20866 gen_rtx_PLUS (Pmode, r12, r0)));
20868 else
20869 emit_insn (gen_rtx_SET (VOIDmode, r0,
20870 plus_constant (Pmode, r12, -rounded_size)));
20873 /* Step 3: the loop
20875 while (TEST_ADDR != LAST_ADDR)
20877 TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
20878 probe at TEST_ADDR
20881 probes at FIRST + N * PROBE_INTERVAL for values of N from 1
20882 until it is equal to ROUNDED_SIZE. */
20884 if (TARGET_64BIT)
20885 emit_insn (gen_probe_stack_rangedi (r12, r12, r0));
20886 else
20887 emit_insn (gen_probe_stack_rangesi (r12, r12, r0));
20890 /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
20891 that SIZE is equal to ROUNDED_SIZE. */
20893 if (size != rounded_size)
20894 emit_stack_probe (plus_constant (Pmode, r12, rounded_size - size));
20898 /* Probe a range of stack addresses from REG1 to REG2 inclusive. These are
20899 absolute addresses. */
20901 const char *
20902 output_probe_stack_range (rtx reg1, rtx reg2)
20904 static int labelno = 0;
20905 char loop_lab[32], end_lab[32];
20906 rtx xops[2];
20908 ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno);
20909 ASM_GENERATE_INTERNAL_LABEL (end_lab, "LPSRE", labelno++);
20911 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
20913 /* Jump to END_LAB if TEST_ADDR == LAST_ADDR. */
20914 xops[0] = reg1;
20915 xops[1] = reg2;
20916 if (TARGET_64BIT)
20917 output_asm_insn ("cmpd 0,%0,%1", xops);
20918 else
20919 output_asm_insn ("cmpw 0,%0,%1", xops);
20921 fputs ("\tbeq 0,", asm_out_file);
20922 assemble_name_raw (asm_out_file, end_lab);
20923 fputc ('\n', asm_out_file);
20925 /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL. */
20926 xops[1] = GEN_INT (-PROBE_INTERVAL);
20927 output_asm_insn ("addi %0,%0,%1", xops);
20929 /* Probe at TEST_ADDR and branch. */
20930 xops[1] = gen_rtx_REG (Pmode, 0);
20931 output_asm_insn ("stw %1,0(%0)", xops);
20932 fprintf (asm_out_file, "\tb ");
20933 assemble_name_raw (asm_out_file, loop_lab);
20934 fputc ('\n', asm_out_file);
20936 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, end_lab);
20938 return "";
20941 /* Add to 'insn' a note which is PATTERN (INSN) but with REG replaced
20942 with (plus:P (reg 1) VAL), and with REG2 replaced with RREG if REG2
20943 is not NULL. It would be nice if dwarf2out_frame_debug_expr could
20944 deduce these equivalences by itself so it wasn't necessary to hold
20945 its hand so much. Don't be tempted to always supply d2_f_d_e with
20946 the actual cfa register, ie. r31 when we are using a hard frame
20947 pointer. That fails when saving regs off r1, and sched moves the
20948 r31 setup past the reg saves. */
20950 static rtx
20951 rs6000_frame_related (rtx insn, rtx reg, HOST_WIDE_INT val,
20952 rtx reg2, rtx rreg)
20954 rtx real, temp;
20956 if (REGNO (reg) == STACK_POINTER_REGNUM && reg2 == NULL_RTX)
20958 /* No need for any replacement. Just set RTX_FRAME_RELATED_P. */
20959 int i;
20961 gcc_checking_assert (val == 0);
20962 real = PATTERN (insn);
20963 if (GET_CODE (real) == PARALLEL)
20964 for (i = 0; i < XVECLEN (real, 0); i++)
20965 if (GET_CODE (XVECEXP (real, 0, i)) == SET)
20967 rtx set = XVECEXP (real, 0, i);
20969 RTX_FRAME_RELATED_P (set) = 1;
20971 RTX_FRAME_RELATED_P (insn) = 1;
20972 return insn;
20975 /* copy_rtx will not make unique copies of registers, so we need to
20976 ensure we don't have unwanted sharing here. */
20977 if (reg == reg2)
20978 reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
20980 if (reg == rreg)
20981 reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
20983 real = copy_rtx (PATTERN (insn));
20985 if (reg2 != NULL_RTX)
20986 real = replace_rtx (real, reg2, rreg);
20988 if (REGNO (reg) == STACK_POINTER_REGNUM)
20989 gcc_checking_assert (val == 0);
20990 else
20991 real = replace_rtx (real, reg,
20992 gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode,
20993 STACK_POINTER_REGNUM),
20994 GEN_INT (val)));
20996 /* We expect that 'real' is either a SET or a PARALLEL containing
20997 SETs (and possibly other stuff). In a PARALLEL, all the SETs
20998 are important so they all have to be marked RTX_FRAME_RELATED_P. */
21000 if (GET_CODE (real) == SET)
21002 rtx set = real;
21004 temp = simplify_rtx (SET_SRC (set));
21005 if (temp)
21006 SET_SRC (set) = temp;
21007 temp = simplify_rtx (SET_DEST (set));
21008 if (temp)
21009 SET_DEST (set) = temp;
21010 if (GET_CODE (SET_DEST (set)) == MEM)
21012 temp = simplify_rtx (XEXP (SET_DEST (set), 0));
21013 if (temp)
21014 XEXP (SET_DEST (set), 0) = temp;
21017 else
21019 int i;
21021 gcc_assert (GET_CODE (real) == PARALLEL);
21022 for (i = 0; i < XVECLEN (real, 0); i++)
21023 if (GET_CODE (XVECEXP (real, 0, i)) == SET)
21025 rtx set = XVECEXP (real, 0, i);
21027 temp = simplify_rtx (SET_SRC (set));
21028 if (temp)
21029 SET_SRC (set) = temp;
21030 temp = simplify_rtx (SET_DEST (set));
21031 if (temp)
21032 SET_DEST (set) = temp;
21033 if (GET_CODE (SET_DEST (set)) == MEM)
21035 temp = simplify_rtx (XEXP (SET_DEST (set), 0));
21036 if (temp)
21037 XEXP (SET_DEST (set), 0) = temp;
21039 RTX_FRAME_RELATED_P (set) = 1;
21043 RTX_FRAME_RELATED_P (insn) = 1;
21044 add_reg_note (insn, REG_FRAME_RELATED_EXPR, real);
21046 return insn;
21049 /* Returns an insn that has a vrsave set operation with the
21050 appropriate CLOBBERs. */
21052 static rtx
21053 generate_set_vrsave (rtx reg, rs6000_stack_t *info, int epiloguep)
21055 int nclobs, i;
21056 rtx insn, clobs[TOTAL_ALTIVEC_REGS + 1];
21057 rtx vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
21059 clobs[0]
21060 = gen_rtx_SET (VOIDmode,
21061 vrsave,
21062 gen_rtx_UNSPEC_VOLATILE (SImode,
21063 gen_rtvec (2, reg, vrsave),
21064 UNSPECV_SET_VRSAVE));
21066 nclobs = 1;
21068 /* We need to clobber the registers in the mask so the scheduler
21069 does not move sets to VRSAVE before sets of AltiVec registers.
21071 However, if the function receives nonlocal gotos, reload will set
21072 all call saved registers live. We will end up with:
21074 (set (reg 999) (mem))
21075 (parallel [ (set (reg vrsave) (unspec blah))
21076 (clobber (reg 999))])
21078 The clobber will cause the store into reg 999 to be dead, and
21079 flow will attempt to delete an epilogue insn. In this case, we
21080 need an unspec use/set of the register. */
21082 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
21083 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
21085 if (!epiloguep || call_used_regs [i])
21086 clobs[nclobs++] = gen_rtx_CLOBBER (VOIDmode,
21087 gen_rtx_REG (V4SImode, i));
21088 else
21090 rtx reg = gen_rtx_REG (V4SImode, i);
21092 clobs[nclobs++]
21093 = gen_rtx_SET (VOIDmode,
21094 reg,
21095 gen_rtx_UNSPEC (V4SImode,
21096 gen_rtvec (1, reg), 27));
21100 insn = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nclobs));
21102 for (i = 0; i < nclobs; ++i)
21103 XVECEXP (insn, 0, i) = clobs[i];
21105 return insn;
21108 static rtx
21109 gen_frame_set (rtx reg, rtx frame_reg, int offset, bool store)
21111 rtx addr, mem;
21113 addr = gen_rtx_PLUS (Pmode, frame_reg, GEN_INT (offset));
21114 mem = gen_frame_mem (GET_MODE (reg), addr);
21115 return gen_rtx_SET (VOIDmode, store ? mem : reg, store ? reg : mem);
21118 static rtx
21119 gen_frame_load (rtx reg, rtx frame_reg, int offset)
21121 return gen_frame_set (reg, frame_reg, offset, false);
21124 static rtx
21125 gen_frame_store (rtx reg, rtx frame_reg, int offset)
21127 return gen_frame_set (reg, frame_reg, offset, true);
21130 /* Save a register into the frame, and emit RTX_FRAME_RELATED_P notes.
21131 Save REGNO into [FRAME_REG + OFFSET] in mode MODE. */
21133 static rtx
21134 emit_frame_save (rtx frame_reg, enum machine_mode mode,
21135 unsigned int regno, int offset, HOST_WIDE_INT frame_reg_to_sp)
21137 rtx reg, insn;
21139 /* Some cases that need register indexed addressing. */
21140 gcc_checking_assert (!((TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
21141 || (TARGET_VSX && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
21142 || (TARGET_E500_DOUBLE && mode == DFmode)
21143 || (TARGET_SPE_ABI
21144 && SPE_VECTOR_MODE (mode)
21145 && !SPE_CONST_OFFSET_OK (offset))));
21147 reg = gen_rtx_REG (mode, regno);
21148 insn = emit_insn (gen_frame_store (reg, frame_reg, offset));
21149 return rs6000_frame_related (insn, frame_reg, frame_reg_to_sp,
21150 NULL_RTX, NULL_RTX);
21153 /* Emit an offset memory reference suitable for a frame store, while
21154 converting to a valid addressing mode. */
21156 static rtx
21157 gen_frame_mem_offset (enum machine_mode mode, rtx reg, int offset)
21159 rtx int_rtx, offset_rtx;
21161 int_rtx = GEN_INT (offset);
21163 if ((TARGET_SPE_ABI && SPE_VECTOR_MODE (mode) && !SPE_CONST_OFFSET_OK (offset))
21164 || (TARGET_E500_DOUBLE && mode == DFmode))
21166 offset_rtx = gen_rtx_REG (Pmode, FIXED_SCRATCH);
21167 emit_move_insn (offset_rtx, int_rtx);
21169 else
21170 offset_rtx = int_rtx;
21172 return gen_frame_mem (mode, gen_rtx_PLUS (Pmode, reg, offset_rtx));
21175 #ifndef TARGET_FIX_AND_CONTINUE
21176 #define TARGET_FIX_AND_CONTINUE 0
21177 #endif
21179 /* It's really GPR 13 or 14, FPR 14 and VR 20. We need the smallest. */
21180 #define FIRST_SAVRES_REGISTER FIRST_SAVED_GP_REGNO
21181 #define LAST_SAVRES_REGISTER 31
21182 #define N_SAVRES_REGISTERS (LAST_SAVRES_REGISTER - FIRST_SAVRES_REGISTER + 1)
21184 enum {
21185 SAVRES_LR = 0x1,
21186 SAVRES_SAVE = 0x2,
21187 SAVRES_REG = 0x0c,
21188 SAVRES_GPR = 0,
21189 SAVRES_FPR = 4,
21190 SAVRES_VR = 8
21193 static GTY(()) rtx savres_routine_syms[N_SAVRES_REGISTERS][12];
21195 /* Temporary holding space for an out-of-line register save/restore
21196 routine name. */
21197 static char savres_routine_name[30];
21199 /* Return the name for an out-of-line register save/restore routine.
21200 We are saving/restoring GPRs if GPR is true. */
21202 static char *
21203 rs6000_savres_routine_name (rs6000_stack_t *info, int regno, int sel)
21205 const char *prefix = "";
21206 const char *suffix = "";
21208 /* Different targets are supposed to define
21209 {SAVE,RESTORE}_FP_{PREFIX,SUFFIX} with the idea that the needed
21210 routine name could be defined with:
21212 sprintf (name, "%s%d%s", SAVE_FP_PREFIX, regno, SAVE_FP_SUFFIX)
21214 This is a nice idea in practice, but in reality, things are
21215 complicated in several ways:
21217 - ELF targets have save/restore routines for GPRs.
21219 - SPE targets use different prefixes for 32/64-bit registers, and
21220 neither of them fit neatly in the FOO_{PREFIX,SUFFIX} regimen.
21222 - PPC64 ELF targets have routines for save/restore of GPRs that
21223 differ in what they do with the link register, so having a set
21224 prefix doesn't work. (We only use one of the save routines at
21225 the moment, though.)
21227 - PPC32 elf targets have "exit" versions of the restore routines
21228 that restore the link register and can save some extra space.
21229 These require an extra suffix. (There are also "tail" versions
21230 of the restore routines and "GOT" versions of the save routines,
21231 but we don't generate those at present. Same problems apply,
21232 though.)
21234 We deal with all this by synthesizing our own prefix/suffix and
21235 using that for the simple sprintf call shown above. */
21236 if (TARGET_SPE)
21238 /* No floating point saves on the SPE. */
21239 gcc_assert ((sel & SAVRES_REG) == SAVRES_GPR);
21241 if ((sel & SAVRES_SAVE))
21242 prefix = info->spe_64bit_regs_used ? "_save64gpr_" : "_save32gpr_";
21243 else
21244 prefix = info->spe_64bit_regs_used ? "_rest64gpr_" : "_rest32gpr_";
21246 if ((sel & SAVRES_LR))
21247 suffix = "_x";
21249 else if (DEFAULT_ABI == ABI_V4)
21251 if (TARGET_64BIT)
21252 goto aix_names;
21254 if ((sel & SAVRES_REG) == SAVRES_GPR)
21255 prefix = (sel & SAVRES_SAVE) ? "_savegpr_" : "_restgpr_";
21256 else if ((sel & SAVRES_REG) == SAVRES_FPR)
21257 prefix = (sel & SAVRES_SAVE) ? "_savefpr_" : "_restfpr_";
21258 else if ((sel & SAVRES_REG) == SAVRES_VR)
21259 prefix = (sel & SAVRES_SAVE) ? "_savevr_" : "_restvr_";
21260 else
21261 abort ();
21263 if ((sel & SAVRES_LR))
21264 suffix = "_x";
21266 else if (DEFAULT_ABI == ABI_AIX)
21268 #if !defined (POWERPC_LINUX) && !defined (POWERPC_FREEBSD)
21269 /* No out-of-line save/restore routines for GPRs on AIX. */
21270 gcc_assert (!TARGET_AIX || (sel & SAVRES_REG) != SAVRES_GPR);
21271 #endif
21273 aix_names:
21274 if ((sel & SAVRES_REG) == SAVRES_GPR)
21275 prefix = ((sel & SAVRES_SAVE)
21276 ? ((sel & SAVRES_LR) ? "_savegpr0_" : "_savegpr1_")
21277 : ((sel & SAVRES_LR) ? "_restgpr0_" : "_restgpr1_"));
21278 else if ((sel & SAVRES_REG) == SAVRES_FPR)
21280 #if defined (POWERPC_LINUX) || defined (POWERPC_FREEBSD)
21281 if ((sel & SAVRES_LR))
21282 prefix = ((sel & SAVRES_SAVE) ? "_savefpr_" : "_restfpr_");
21283 else
21284 #endif
21286 prefix = (sel & SAVRES_SAVE) ? SAVE_FP_PREFIX : RESTORE_FP_PREFIX;
21287 suffix = (sel & SAVRES_SAVE) ? SAVE_FP_SUFFIX : RESTORE_FP_SUFFIX;
21290 else if ((sel & SAVRES_REG) == SAVRES_VR)
21291 prefix = (sel & SAVRES_SAVE) ? "_savevr_" : "_restvr_";
21292 else
21293 abort ();
21296 if (DEFAULT_ABI == ABI_DARWIN)
21298 /* The Darwin approach is (slightly) different, in order to be
21299 compatible with code generated by the system toolchain. There is a
21300 single symbol for the start of save sequence, and the code here
21301 embeds an offset into that code on the basis of the first register
21302 to be saved. */
21303 prefix = (sel & SAVRES_SAVE) ? "save" : "rest" ;
21304 if ((sel & SAVRES_REG) == SAVRES_GPR)
21305 sprintf (savres_routine_name, "*%sGPR%s%s%.0d ; %s r%d-r31", prefix,
21306 ((sel & SAVRES_LR) ? "x" : ""), (regno == 13 ? "" : "+"),
21307 (regno - 13) * 4, prefix, regno);
21308 else if ((sel & SAVRES_REG) == SAVRES_FPR)
21309 sprintf (savres_routine_name, "*%sFP%s%.0d ; %s f%d-f31", prefix,
21310 (regno == 14 ? "" : "+"), (regno - 14) * 4, prefix, regno);
21311 else if ((sel & SAVRES_REG) == SAVRES_VR)
21312 sprintf (savres_routine_name, "*%sVEC%s%.0d ; %s v%d-v31", prefix,
21313 (regno == 20 ? "" : "+"), (regno - 20) * 8, prefix, regno);
21314 else
21315 abort ();
21317 else
21318 sprintf (savres_routine_name, "%s%d%s", prefix, regno, suffix);
21320 return savres_routine_name;
21323 /* Return an RTL SYMBOL_REF for an out-of-line register save/restore routine.
21324 We are saving/restoring GPRs if GPR is true. */
21326 static rtx
21327 rs6000_savres_routine_sym (rs6000_stack_t *info, int sel)
21329 int regno = ((sel & SAVRES_REG) == SAVRES_GPR
21330 ? info->first_gp_reg_save
21331 : (sel & SAVRES_REG) == SAVRES_FPR
21332 ? info->first_fp_reg_save - 32
21333 : (sel & SAVRES_REG) == SAVRES_VR
21334 ? info->first_altivec_reg_save - FIRST_ALTIVEC_REGNO
21335 : -1);
21336 rtx sym;
21337 int select = sel;
21339 /* On the SPE, we never have any FPRs, but we do have 32/64-bit
21340 versions of the gpr routines. */
21341 if (TARGET_SPE_ABI && (sel & SAVRES_REG) == SAVRES_GPR
21342 && info->spe_64bit_regs_used)
21343 select ^= SAVRES_FPR ^ SAVRES_GPR;
21345 /* Don't generate bogus routine names. */
21346 gcc_assert (FIRST_SAVRES_REGISTER <= regno
21347 && regno <= LAST_SAVRES_REGISTER
21348 && select >= 0 && select <= 12);
21350 sym = savres_routine_syms[regno-FIRST_SAVRES_REGISTER][select];
21352 if (sym == NULL)
21354 char *name;
21356 name = rs6000_savres_routine_name (info, regno, sel);
21358 sym = savres_routine_syms[regno-FIRST_SAVRES_REGISTER][select]
21359 = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
21360 SYMBOL_REF_FLAGS (sym) |= SYMBOL_FLAG_FUNCTION;
21363 return sym;
21366 /* Emit a sequence of insns, including a stack tie if needed, for
21367 resetting the stack pointer. If UPDT_REGNO is not 1, then don't
21368 reset the stack pointer, but move the base of the frame into
21369 reg UPDT_REGNO for use by out-of-line register restore routines. */
21371 static rtx
21372 rs6000_emit_stack_reset (rs6000_stack_t *info,
21373 rtx frame_reg_rtx, HOST_WIDE_INT frame_off,
21374 unsigned updt_regno)
21376 rtx updt_reg_rtx;
21378 /* This blockage is needed so that sched doesn't decide to move
21379 the sp change before the register restores. */
21380 if (DEFAULT_ABI == ABI_V4
21381 || (TARGET_SPE_ABI
21382 && info->spe_64bit_regs_used != 0
21383 && info->first_gp_reg_save != 32))
21384 rs6000_emit_stack_tie (frame_reg_rtx, frame_pointer_needed);
21386 /* If we are restoring registers out-of-line, we will be using the
21387 "exit" variants of the restore routines, which will reset the
21388 stack for us. But we do need to point updt_reg into the
21389 right place for those routines. */
21390 updt_reg_rtx = gen_rtx_REG (Pmode, updt_regno);
21392 if (frame_off != 0)
21393 return emit_insn (gen_add3_insn (updt_reg_rtx,
21394 frame_reg_rtx, GEN_INT (frame_off)));
21395 else if (REGNO (frame_reg_rtx) != updt_regno)
21396 return emit_move_insn (updt_reg_rtx, frame_reg_rtx);
21398 return NULL_RTX;
21401 /* Return the register number used as a pointer by out-of-line
21402 save/restore functions. */
21404 static inline unsigned
21405 ptr_regno_for_savres (int sel)
21407 if (DEFAULT_ABI == ABI_AIX)
21408 return (sel & SAVRES_REG) == SAVRES_FPR || (sel & SAVRES_LR) ? 1 : 12;
21409 return DEFAULT_ABI == ABI_DARWIN && (sel & SAVRES_REG) == SAVRES_FPR ? 1 : 11;
21412 /* Construct a parallel rtx describing the effect of a call to an
21413 out-of-line register save/restore routine, and emit the insn
21414 or jump_insn as appropriate. */
21416 static rtx
21417 rs6000_emit_savres_rtx (rs6000_stack_t *info,
21418 rtx frame_reg_rtx, int save_area_offset, int lr_offset,
21419 enum machine_mode reg_mode, int sel)
21421 int i;
21422 int offset, start_reg, end_reg, n_regs, use_reg;
21423 int reg_size = GET_MODE_SIZE (reg_mode);
21424 rtx sym;
21425 rtvec p;
21426 rtx par, insn;
21428 offset = 0;
21429 start_reg = ((sel & SAVRES_REG) == SAVRES_GPR
21430 ? info->first_gp_reg_save
21431 : (sel & SAVRES_REG) == SAVRES_FPR
21432 ? info->first_fp_reg_save
21433 : (sel & SAVRES_REG) == SAVRES_VR
21434 ? info->first_altivec_reg_save
21435 : -1);
21436 end_reg = ((sel & SAVRES_REG) == SAVRES_GPR
21437 ? 32
21438 : (sel & SAVRES_REG) == SAVRES_FPR
21439 ? 64
21440 : (sel & SAVRES_REG) == SAVRES_VR
21441 ? LAST_ALTIVEC_REGNO + 1
21442 : -1);
21443 n_regs = end_reg - start_reg;
21444 p = rtvec_alloc (3 + ((sel & SAVRES_LR) ? 1 : 0)
21445 + ((sel & SAVRES_REG) == SAVRES_VR ? 1 : 0)
21446 + n_regs);
21448 if (!(sel & SAVRES_SAVE) && (sel & SAVRES_LR))
21449 RTVEC_ELT (p, offset++) = ret_rtx;
21451 RTVEC_ELT (p, offset++)
21452 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, LR_REGNO));
21454 sym = rs6000_savres_routine_sym (info, sel);
21455 RTVEC_ELT (p, offset++) = gen_rtx_USE (VOIDmode, sym);
21457 use_reg = ptr_regno_for_savres (sel);
21458 if ((sel & SAVRES_REG) == SAVRES_VR)
21460 /* Vector regs are saved/restored using [reg+reg] addressing. */
21461 RTVEC_ELT (p, offset++)
21462 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, use_reg));
21463 RTVEC_ELT (p, offset++)
21464 = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, 0));
21466 else
21467 RTVEC_ELT (p, offset++)
21468 = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, use_reg));
21470 for (i = 0; i < end_reg - start_reg; i++)
21471 RTVEC_ELT (p, i + offset)
21472 = gen_frame_set (gen_rtx_REG (reg_mode, start_reg + i),
21473 frame_reg_rtx, save_area_offset + reg_size * i,
21474 (sel & SAVRES_SAVE) != 0);
21476 if ((sel & SAVRES_SAVE) && (sel & SAVRES_LR))
21477 RTVEC_ELT (p, i + offset)
21478 = gen_frame_store (gen_rtx_REG (Pmode, 0), frame_reg_rtx, lr_offset);
21480 par = gen_rtx_PARALLEL (VOIDmode, p);
21482 if (!(sel & SAVRES_SAVE) && (sel & SAVRES_LR))
21484 insn = emit_jump_insn (par);
21485 JUMP_LABEL (insn) = ret_rtx;
21487 else
21488 insn = emit_insn (par);
21489 return insn;
21492 /* Determine whether the gp REG is really used. */
21494 static bool
21495 rs6000_reg_live_or_pic_offset_p (int reg)
21497 /* If the function calls eh_return, claim used all the registers that would
21498 be checked for liveness otherwise. This is required for the PIC offset
21499 register with -mminimal-toc on AIX, as it is advertised as "fixed" for
21500 register allocation purposes in this case. */
21502 return (((crtl->calls_eh_return || df_regs_ever_live_p (reg))
21503 && (!call_used_regs[reg]
21504 || (reg == RS6000_PIC_OFFSET_TABLE_REGNUM
21505 && !TARGET_SINGLE_PIC_BASE
21506 && TARGET_TOC && TARGET_MINIMAL_TOC)))
21507 || (reg == RS6000_PIC_OFFSET_TABLE_REGNUM
21508 && !TARGET_SINGLE_PIC_BASE
21509 && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
21510 || (DEFAULT_ABI == ABI_DARWIN && flag_pic))));
21513 /* Emit function prologue as insns. */
21515 void
21516 rs6000_emit_prologue (void)
21518 rs6000_stack_t *info = rs6000_stack_info ();
21519 enum machine_mode reg_mode = Pmode;
21520 int reg_size = TARGET_32BIT ? 4 : 8;
21521 rtx sp_reg_rtx = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
21522 rtx frame_reg_rtx = sp_reg_rtx;
21523 unsigned int cr_save_regno;
21524 rtx cr_save_rtx = NULL_RTX;
21525 rtx insn;
21526 int strategy;
21527 int using_static_chain_p = (cfun->static_chain_decl != NULL_TREE
21528 && df_regs_ever_live_p (STATIC_CHAIN_REGNUM)
21529 && call_used_regs[STATIC_CHAIN_REGNUM]);
21530 /* Offset to top of frame for frame_reg and sp respectively. */
21531 HOST_WIDE_INT frame_off = 0;
21532 HOST_WIDE_INT sp_off = 0;
21534 #ifdef ENABLE_CHECKING
21535 /* Track and check usage of r0, r11, r12. */
21536 int reg_inuse = using_static_chain_p ? 1 << 11 : 0;
21537 #define START_USE(R) do \
21539 gcc_assert ((reg_inuse & (1 << (R))) == 0); \
21540 reg_inuse |= 1 << (R); \
21541 } while (0)
21542 #define END_USE(R) do \
21544 gcc_assert ((reg_inuse & (1 << (R))) != 0); \
21545 reg_inuse &= ~(1 << (R)); \
21546 } while (0)
21547 #define NOT_INUSE(R) do \
21549 gcc_assert ((reg_inuse & (1 << (R))) == 0); \
21550 } while (0)
21551 #else
21552 #define START_USE(R) do {} while (0)
21553 #define END_USE(R) do {} while (0)
21554 #define NOT_INUSE(R) do {} while (0)
21555 #endif
21557 if (flag_stack_usage_info)
21558 current_function_static_stack_size = info->total_size;
21560 if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
21562 HOST_WIDE_INT size = info->total_size;
21564 if (crtl->is_leaf && !cfun->calls_alloca)
21566 if (size > PROBE_INTERVAL && size > STACK_CHECK_PROTECT)
21567 rs6000_emit_probe_stack_range (STACK_CHECK_PROTECT,
21568 size - STACK_CHECK_PROTECT);
21570 else if (size > 0)
21571 rs6000_emit_probe_stack_range (STACK_CHECK_PROTECT, size);
21574 if (TARGET_FIX_AND_CONTINUE)
21576 /* gdb on darwin arranges to forward a function from the old
21577 address by modifying the first 5 instructions of the function
21578 to branch to the overriding function. This is necessary to
21579 permit function pointers that point to the old function to
21580 actually forward to the new function. */
21581 emit_insn (gen_nop ());
21582 emit_insn (gen_nop ());
21583 emit_insn (gen_nop ());
21584 emit_insn (gen_nop ());
21585 emit_insn (gen_nop ());
21588 if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
21590 reg_mode = V2SImode;
21591 reg_size = 8;
21594 /* Handle world saves specially here. */
21595 if (WORLD_SAVE_P (info))
21597 int i, j, sz;
21598 rtx treg;
21599 rtvec p;
21600 rtx reg0;
21602 /* save_world expects lr in r0. */
21603 reg0 = gen_rtx_REG (Pmode, 0);
21604 if (info->lr_save_p)
21606 insn = emit_move_insn (reg0,
21607 gen_rtx_REG (Pmode, LR_REGNO));
21608 RTX_FRAME_RELATED_P (insn) = 1;
21611 /* The SAVE_WORLD and RESTORE_WORLD routines make a number of
21612 assumptions about the offsets of various bits of the stack
21613 frame. */
21614 gcc_assert (info->gp_save_offset == -220
21615 && info->fp_save_offset == -144
21616 && info->lr_save_offset == 8
21617 && info->cr_save_offset == 4
21618 && info->push_p
21619 && info->lr_save_p
21620 && (!crtl->calls_eh_return
21621 || info->ehrd_offset == -432)
21622 && info->vrsave_save_offset == -224
21623 && info->altivec_save_offset == -416);
21625 treg = gen_rtx_REG (SImode, 11);
21626 emit_move_insn (treg, GEN_INT (-info->total_size));
21628 /* SAVE_WORLD takes the caller's LR in R0 and the frame size
21629 in R11. It also clobbers R12, so beware! */
21631 /* Preserve CR2 for save_world prologues */
21632 sz = 5;
21633 sz += 32 - info->first_gp_reg_save;
21634 sz += 64 - info->first_fp_reg_save;
21635 sz += LAST_ALTIVEC_REGNO - info->first_altivec_reg_save + 1;
21636 p = rtvec_alloc (sz);
21637 j = 0;
21638 RTVEC_ELT (p, j++) = gen_rtx_CLOBBER (VOIDmode,
21639 gen_rtx_REG (SImode,
21640 LR_REGNO));
21641 RTVEC_ELT (p, j++) = gen_rtx_USE (VOIDmode,
21642 gen_rtx_SYMBOL_REF (Pmode,
21643 "*save_world"));
21644 /* We do floats first so that the instruction pattern matches
21645 properly. */
21646 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
21647 RTVEC_ELT (p, j++)
21648 = gen_frame_store (gen_rtx_REG (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
21649 ? DFmode : SFmode,
21650 info->first_fp_reg_save + i),
21651 frame_reg_rtx,
21652 info->fp_save_offset + frame_off + 8 * i);
21653 for (i = 0; info->first_altivec_reg_save + i <= LAST_ALTIVEC_REGNO; i++)
21654 RTVEC_ELT (p, j++)
21655 = gen_frame_store (gen_rtx_REG (V4SImode,
21656 info->first_altivec_reg_save + i),
21657 frame_reg_rtx,
21658 info->altivec_save_offset + frame_off + 16 * i);
21659 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
21660 RTVEC_ELT (p, j++)
21661 = gen_frame_store (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
21662 frame_reg_rtx,
21663 info->gp_save_offset + frame_off + reg_size * i);
21665 /* CR register traditionally saved as CR2. */
21666 RTVEC_ELT (p, j++)
21667 = gen_frame_store (gen_rtx_REG (SImode, CR2_REGNO),
21668 frame_reg_rtx, info->cr_save_offset + frame_off);
21669 /* Explain about use of R0. */
21670 if (info->lr_save_p)
21671 RTVEC_ELT (p, j++)
21672 = gen_frame_store (reg0,
21673 frame_reg_rtx, info->lr_save_offset + frame_off);
21674 /* Explain what happens to the stack pointer. */
21676 rtx newval = gen_rtx_PLUS (Pmode, sp_reg_rtx, treg);
21677 RTVEC_ELT (p, j++) = gen_rtx_SET (VOIDmode, sp_reg_rtx, newval);
21680 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
21681 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
21682 treg, GEN_INT (-info->total_size));
21683 sp_off = frame_off = info->total_size;
21686 strategy = info->savres_strategy;
21688 /* For V.4, update stack before we do any saving and set back pointer. */
21689 if (! WORLD_SAVE_P (info)
21690 && info->push_p
21691 && (DEFAULT_ABI == ABI_V4
21692 || crtl->calls_eh_return))
21694 bool need_r11 = (TARGET_SPE
21695 ? (!(strategy & SAVE_INLINE_GPRS)
21696 && info->spe_64bit_regs_used == 0)
21697 : (!(strategy & SAVE_INLINE_FPRS)
21698 || !(strategy & SAVE_INLINE_GPRS)
21699 || !(strategy & SAVE_INLINE_VRS)));
21700 int ptr_regno = -1;
21701 rtx ptr_reg = NULL_RTX;
21702 int ptr_off = 0;
21704 if (info->total_size < 32767)
21705 frame_off = info->total_size;
21706 else if (need_r11)
21707 ptr_regno = 11;
21708 else if (info->cr_save_p
21709 || info->lr_save_p
21710 || info->first_fp_reg_save < 64
21711 || info->first_gp_reg_save < 32
21712 || info->altivec_size != 0
21713 || info->vrsave_mask != 0
21714 || crtl->calls_eh_return)
21715 ptr_regno = 12;
21716 else
21718 /* The prologue won't be saving any regs so there is no need
21719 to set up a frame register to access any frame save area.
21720 We also won't be using frame_off anywhere below, but set
21721 the correct value anyway to protect against future
21722 changes to this function. */
21723 frame_off = info->total_size;
21725 if (ptr_regno != -1)
21727 /* Set up the frame offset to that needed by the first
21728 out-of-line save function. */
21729 START_USE (ptr_regno);
21730 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
21731 frame_reg_rtx = ptr_reg;
21732 if (!(strategy & SAVE_INLINE_FPRS) && info->fp_size != 0)
21733 gcc_checking_assert (info->fp_save_offset + info->fp_size == 0);
21734 else if (!(strategy & SAVE_INLINE_GPRS) && info->first_gp_reg_save < 32)
21735 ptr_off = info->gp_save_offset + info->gp_size;
21736 else if (!(strategy & SAVE_INLINE_VRS) && info->altivec_size != 0)
21737 ptr_off = info->altivec_save_offset + info->altivec_size;
21738 frame_off = -ptr_off;
21740 rs6000_emit_allocate_stack (info->total_size, ptr_reg, ptr_off);
21741 sp_off = info->total_size;
21742 if (frame_reg_rtx != sp_reg_rtx)
21743 rs6000_emit_stack_tie (frame_reg_rtx, false);
21746 /* If we use the link register, get it into r0. */
21747 if (!WORLD_SAVE_P (info) && info->lr_save_p)
21749 rtx addr, reg, mem;
21751 reg = gen_rtx_REG (Pmode, 0);
21752 START_USE (0);
21753 insn = emit_move_insn (reg, gen_rtx_REG (Pmode, LR_REGNO));
21754 RTX_FRAME_RELATED_P (insn) = 1;
21756 if (!(strategy & (SAVE_NOINLINE_GPRS_SAVES_LR
21757 | SAVE_NOINLINE_FPRS_SAVES_LR)))
21759 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
21760 GEN_INT (info->lr_save_offset + frame_off));
21761 mem = gen_rtx_MEM (Pmode, addr);
21762 /* This should not be of rs6000_sr_alias_set, because of
21763 __builtin_return_address. */
21765 insn = emit_move_insn (mem, reg);
21766 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
21767 NULL_RTX, NULL_RTX);
21768 END_USE (0);
21772 /* If we need to save CR, put it into r12 or r11. Choose r12 except when
21773 r12 will be needed by out-of-line gpr restore. */
21774 cr_save_regno = (DEFAULT_ABI == ABI_AIX
21775 && !(strategy & (SAVE_INLINE_GPRS
21776 | SAVE_NOINLINE_GPRS_SAVES_LR))
21777 ? 11 : 12);
21778 if (!WORLD_SAVE_P (info)
21779 && info->cr_save_p
21780 && REGNO (frame_reg_rtx) != cr_save_regno
21781 && !(using_static_chain_p && cr_save_regno == 11))
21783 rtx set;
21785 cr_save_rtx = gen_rtx_REG (SImode, cr_save_regno);
21786 START_USE (cr_save_regno);
21787 insn = emit_insn (gen_movesi_from_cr (cr_save_rtx));
21788 RTX_FRAME_RELATED_P (insn) = 1;
21789 /* Now, there's no way that dwarf2out_frame_debug_expr is going
21790 to understand '(unspec:SI [(reg:CC 68) ...] UNSPEC_MOVESI_FROM_CR)'.
21791 But that's OK. All we have to do is specify that _one_ condition
21792 code register is saved in this stack slot. The thrower's epilogue
21793 will then restore all the call-saved registers.
21794 We use CR2_REGNO (70) to be compatible with gcc-2.95 on Linux. */
21795 set = gen_rtx_SET (VOIDmode, cr_save_rtx,
21796 gen_rtx_REG (SImode, CR2_REGNO));
21797 add_reg_note (insn, REG_FRAME_RELATED_EXPR, set);
21800 /* Do any required saving of fpr's. If only one or two to save, do
21801 it ourselves. Otherwise, call function. */
21802 if (!WORLD_SAVE_P (info) && (strategy & SAVE_INLINE_FPRS))
21804 int i;
21805 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
21806 if (save_reg_p (info->first_fp_reg_save + i))
21807 emit_frame_save (frame_reg_rtx,
21808 (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
21809 ? DFmode : SFmode),
21810 info->first_fp_reg_save + i,
21811 info->fp_save_offset + frame_off + 8 * i,
21812 sp_off - frame_off);
21814 else if (!WORLD_SAVE_P (info) && info->first_fp_reg_save != 64)
21816 bool lr = (strategy & SAVE_NOINLINE_FPRS_SAVES_LR) != 0;
21817 int sel = SAVRES_SAVE | SAVRES_FPR | (lr ? SAVRES_LR : 0);
21818 unsigned ptr_regno = ptr_regno_for_savres (sel);
21819 rtx ptr_reg = frame_reg_rtx;
21821 if (REGNO (frame_reg_rtx) == ptr_regno)
21822 gcc_checking_assert (frame_off == 0);
21823 else
21825 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
21826 NOT_INUSE (ptr_regno);
21827 emit_insn (gen_add3_insn (ptr_reg,
21828 frame_reg_rtx, GEN_INT (frame_off)));
21830 insn = rs6000_emit_savres_rtx (info, ptr_reg,
21831 info->fp_save_offset,
21832 info->lr_save_offset,
21833 DFmode, sel);
21834 rs6000_frame_related (insn, ptr_reg, sp_off,
21835 NULL_RTX, NULL_RTX);
21836 if (lr)
21837 END_USE (0);
21840 /* Save GPRs. This is done as a PARALLEL if we are using
21841 the store-multiple instructions. */
21842 if (!WORLD_SAVE_P (info)
21843 && TARGET_SPE_ABI
21844 && info->spe_64bit_regs_used != 0
21845 && info->first_gp_reg_save != 32)
21847 int i;
21848 rtx spe_save_area_ptr;
21849 HOST_WIDE_INT save_off;
21850 int ool_adjust = 0;
21852 /* Determine whether we can address all of the registers that need
21853 to be saved with an offset from frame_reg_rtx that fits in
21854 the small const field for SPE memory instructions. */
21855 int spe_regs_addressable
21856 = (SPE_CONST_OFFSET_OK (info->spe_gp_save_offset + frame_off
21857 + reg_size * (32 - info->first_gp_reg_save - 1))
21858 && (strategy & SAVE_INLINE_GPRS));
21860 if (spe_regs_addressable)
21862 spe_save_area_ptr = frame_reg_rtx;
21863 save_off = frame_off;
21865 else
21867 /* Make r11 point to the start of the SPE save area. We need
21868 to be careful here if r11 is holding the static chain. If
21869 it is, then temporarily save it in r0. */
21870 HOST_WIDE_INT offset;
21872 if (!(strategy & SAVE_INLINE_GPRS))
21873 ool_adjust = 8 * (info->first_gp_reg_save - FIRST_SAVED_GP_REGNO);
21874 offset = info->spe_gp_save_offset + frame_off - ool_adjust;
21875 spe_save_area_ptr = gen_rtx_REG (Pmode, 11);
21876 save_off = frame_off - offset;
21878 if (using_static_chain_p)
21880 rtx r0 = gen_rtx_REG (Pmode, 0);
21882 START_USE (0);
21883 gcc_assert (info->first_gp_reg_save > 11);
21885 emit_move_insn (r0, spe_save_area_ptr);
21887 else if (REGNO (frame_reg_rtx) != 11)
21888 START_USE (11);
21890 emit_insn (gen_addsi3 (spe_save_area_ptr,
21891 frame_reg_rtx, GEN_INT (offset)));
21892 if (!using_static_chain_p && REGNO (frame_reg_rtx) == 11)
21893 frame_off = -info->spe_gp_save_offset + ool_adjust;
21896 if ((strategy & SAVE_INLINE_GPRS))
21898 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
21899 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
21900 emit_frame_save (spe_save_area_ptr, reg_mode,
21901 info->first_gp_reg_save + i,
21902 (info->spe_gp_save_offset + save_off
21903 + reg_size * i),
21904 sp_off - save_off);
21906 else
21908 insn = rs6000_emit_savres_rtx (info, spe_save_area_ptr,
21909 info->spe_gp_save_offset + save_off,
21910 0, reg_mode,
21911 SAVRES_SAVE | SAVRES_GPR);
21913 rs6000_frame_related (insn, spe_save_area_ptr, sp_off - save_off,
21914 NULL_RTX, NULL_RTX);
21917 /* Move the static chain pointer back. */
21918 if (!spe_regs_addressable)
21920 if (using_static_chain_p)
21922 emit_move_insn (spe_save_area_ptr, gen_rtx_REG (Pmode, 0));
21923 END_USE (0);
21925 else if (REGNO (frame_reg_rtx) != 11)
21926 END_USE (11);
21929 else if (!WORLD_SAVE_P (info) && !(strategy & SAVE_INLINE_GPRS))
21931 bool lr = (strategy & SAVE_NOINLINE_GPRS_SAVES_LR) != 0;
21932 int sel = SAVRES_SAVE | SAVRES_GPR | (lr ? SAVRES_LR : 0);
21933 unsigned ptr_regno = ptr_regno_for_savres (sel);
21934 rtx ptr_reg = frame_reg_rtx;
21935 bool ptr_set_up = REGNO (ptr_reg) == ptr_regno;
21936 int end_save = info->gp_save_offset + info->gp_size;
21937 int ptr_off;
21939 if (!ptr_set_up)
21940 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
21942 /* Need to adjust r11 (r12) if we saved any FPRs. */
21943 if (end_save + frame_off != 0)
21945 rtx offset = GEN_INT (end_save + frame_off);
21947 if (ptr_set_up)
21948 frame_off = -end_save;
21949 else
21950 NOT_INUSE (ptr_regno);
21951 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
21953 else if (!ptr_set_up)
21955 NOT_INUSE (ptr_regno);
21956 emit_move_insn (ptr_reg, frame_reg_rtx);
21958 ptr_off = -end_save;
21959 insn = rs6000_emit_savres_rtx (info, ptr_reg,
21960 info->gp_save_offset + ptr_off,
21961 info->lr_save_offset + ptr_off,
21962 reg_mode, sel);
21963 rs6000_frame_related (insn, ptr_reg, sp_off - ptr_off,
21964 NULL_RTX, NULL_RTX);
21965 if (lr)
21966 END_USE (0);
21968 else if (!WORLD_SAVE_P (info) && (strategy & SAVRES_MULTIPLE))
21970 rtvec p;
21971 int i;
21972 p = rtvec_alloc (32 - info->first_gp_reg_save);
21973 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
21974 RTVEC_ELT (p, i)
21975 = gen_frame_store (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
21976 frame_reg_rtx,
21977 info->gp_save_offset + frame_off + reg_size * i);
21978 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
21979 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
21980 NULL_RTX, NULL_RTX);
21982 else if (!WORLD_SAVE_P (info))
21984 int i;
21985 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
21986 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
21987 emit_frame_save (frame_reg_rtx, reg_mode,
21988 info->first_gp_reg_save + i,
21989 info->gp_save_offset + frame_off + reg_size * i,
21990 sp_off - frame_off);
21993 if (crtl->calls_eh_return)
21995 unsigned int i;
21996 rtvec p;
21998 for (i = 0; ; ++i)
22000 unsigned int regno = EH_RETURN_DATA_REGNO (i);
22001 if (regno == INVALID_REGNUM)
22002 break;
22005 p = rtvec_alloc (i);
22007 for (i = 0; ; ++i)
22009 unsigned int regno = EH_RETURN_DATA_REGNO (i);
22010 if (regno == INVALID_REGNUM)
22011 break;
22013 insn
22014 = gen_frame_store (gen_rtx_REG (reg_mode, regno),
22015 sp_reg_rtx,
22016 info->ehrd_offset + sp_off + reg_size * (int) i);
22017 RTVEC_ELT (p, i) = insn;
22018 RTX_FRAME_RELATED_P (insn) = 1;
22021 insn = emit_insn (gen_blockage ());
22022 RTX_FRAME_RELATED_P (insn) = 1;
22023 add_reg_note (insn, REG_FRAME_RELATED_EXPR, gen_rtx_PARALLEL (VOIDmode, p));
22026 /* In AIX ABI we need to make sure r2 is really saved. */
22027 if (TARGET_AIX && crtl->calls_eh_return)
22029 rtx tmp_reg, tmp_reg_si, hi, lo, compare_result, toc_save_done, jump;
22030 rtx save_insn, join_insn, note;
22031 long toc_restore_insn;
22033 tmp_reg = gen_rtx_REG (Pmode, 11);
22034 tmp_reg_si = gen_rtx_REG (SImode, 11);
22035 if (using_static_chain_p)
22037 START_USE (0);
22038 emit_move_insn (gen_rtx_REG (Pmode, 0), tmp_reg);
22040 else
22041 START_USE (11);
22042 emit_move_insn (tmp_reg, gen_rtx_REG (Pmode, LR_REGNO));
22043 /* Peek at instruction to which this function returns. If it's
22044 restoring r2, then we know we've already saved r2. We can't
22045 unconditionally save r2 because the value we have will already
22046 be updated if we arrived at this function via a plt call or
22047 toc adjusting stub. */
22048 emit_move_insn (tmp_reg_si, gen_rtx_MEM (SImode, tmp_reg));
22049 toc_restore_insn = TARGET_32BIT ? 0x80410014 : 0xE8410028;
22050 hi = gen_int_mode (toc_restore_insn & ~0xffff, SImode);
22051 emit_insn (gen_xorsi3 (tmp_reg_si, tmp_reg_si, hi));
22052 compare_result = gen_rtx_REG (CCUNSmode, CR0_REGNO);
22053 validate_condition_mode (EQ, CCUNSmode);
22054 lo = gen_int_mode (toc_restore_insn & 0xffff, SImode);
22055 emit_insn (gen_rtx_SET (VOIDmode, compare_result,
22056 gen_rtx_COMPARE (CCUNSmode, tmp_reg_si, lo)));
22057 toc_save_done = gen_label_rtx ();
22058 jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
22059 gen_rtx_EQ (VOIDmode, compare_result,
22060 const0_rtx),
22061 gen_rtx_LABEL_REF (VOIDmode, toc_save_done),
22062 pc_rtx);
22063 jump = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, jump));
22064 JUMP_LABEL (jump) = toc_save_done;
22065 LABEL_NUSES (toc_save_done) += 1;
22067 save_insn = emit_frame_save (frame_reg_rtx, reg_mode,
22068 TOC_REGNUM, frame_off + 5 * reg_size,
22069 sp_off - frame_off);
22071 emit_label (toc_save_done);
22073 /* ??? If we leave SAVE_INSN as marked as saving R2, then we'll
22074 have a CFG that has different saves along different paths.
22075 Move the note to a dummy blockage insn, which describes that
22076 R2 is unconditionally saved after the label. */
22077 /* ??? An alternate representation might be a special insn pattern
22078 containing both the branch and the store. That might let the
22079 code that minimizes the number of DW_CFA_advance opcodes better
22080 freedom in placing the annotations. */
22081 note = find_reg_note (save_insn, REG_FRAME_RELATED_EXPR, NULL);
22082 if (note)
22083 remove_note (save_insn, note);
22084 else
22085 note = alloc_reg_note (REG_FRAME_RELATED_EXPR,
22086 copy_rtx (PATTERN (save_insn)), NULL_RTX);
22087 RTX_FRAME_RELATED_P (save_insn) = 0;
22089 join_insn = emit_insn (gen_blockage ());
22090 REG_NOTES (join_insn) = note;
22091 RTX_FRAME_RELATED_P (join_insn) = 1;
22093 if (using_static_chain_p)
22095 emit_move_insn (tmp_reg, gen_rtx_REG (Pmode, 0));
22096 END_USE (0);
22098 else
22099 END_USE (11);
22102 /* Save CR if we use any that must be preserved. */
22103 if (!WORLD_SAVE_P (info) && info->cr_save_p)
22105 rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
22106 GEN_INT (info->cr_save_offset + frame_off));
22107 rtx mem = gen_frame_mem (SImode, addr);
22108 /* See the large comment above about why CR2_REGNO is used. */
22109 rtx magic_eh_cr_reg = gen_rtx_REG (SImode, CR2_REGNO);
22111 /* If we didn't copy cr before, do so now using r0. */
22112 if (cr_save_rtx == NULL_RTX)
22114 rtx set;
22116 START_USE (0);
22117 cr_save_rtx = gen_rtx_REG (SImode, 0);
22118 insn = emit_insn (gen_movesi_from_cr (cr_save_rtx));
22119 RTX_FRAME_RELATED_P (insn) = 1;
22120 set = gen_rtx_SET (VOIDmode, cr_save_rtx, magic_eh_cr_reg);
22121 add_reg_note (insn, REG_FRAME_RELATED_EXPR, set);
22123 insn = emit_move_insn (mem, cr_save_rtx);
22124 END_USE (REGNO (cr_save_rtx));
22126 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
22127 NULL_RTX, NULL_RTX);
22130 /* Update stack and set back pointer unless this is V.4,
22131 for which it was done previously. */
22132 if (!WORLD_SAVE_P (info) && info->push_p
22133 && !(DEFAULT_ABI == ABI_V4 || crtl->calls_eh_return))
22135 rtx ptr_reg = NULL;
22136 int ptr_off = 0;
22138 /* If saving altivec regs we need to be able to address all save
22139 locations using a 16-bit offset. */
22140 if ((strategy & SAVE_INLINE_VRS) == 0
22141 || (info->altivec_size != 0
22142 && (info->altivec_save_offset + info->altivec_size - 16
22143 + info->total_size - frame_off) > 32767)
22144 || (info->vrsave_size != 0
22145 && (info->vrsave_save_offset
22146 + info->total_size - frame_off) > 32767))
22148 int sel = SAVRES_SAVE | SAVRES_VR;
22149 unsigned ptr_regno = ptr_regno_for_savres (sel);
22151 if (using_static_chain_p
22152 && ptr_regno == STATIC_CHAIN_REGNUM)
22153 ptr_regno = 12;
22154 if (REGNO (frame_reg_rtx) != ptr_regno)
22155 START_USE (ptr_regno);
22156 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
22157 frame_reg_rtx = ptr_reg;
22158 ptr_off = info->altivec_save_offset + info->altivec_size;
22159 frame_off = -ptr_off;
22161 else if (REGNO (frame_reg_rtx) == 1)
22162 frame_off = info->total_size;
22163 rs6000_emit_allocate_stack (info->total_size, ptr_reg, ptr_off);
22164 sp_off = info->total_size;
22165 if (frame_reg_rtx != sp_reg_rtx)
22166 rs6000_emit_stack_tie (frame_reg_rtx, false);
22169 /* Set frame pointer, if needed. */
22170 if (frame_pointer_needed)
22172 insn = emit_move_insn (gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
22173 sp_reg_rtx);
22174 RTX_FRAME_RELATED_P (insn) = 1;
22177 /* Save AltiVec registers if needed. Save here because the red zone does
22178 not always include AltiVec registers. */
22179 if (!WORLD_SAVE_P (info) && TARGET_ALTIVEC_ABI
22180 && info->altivec_size != 0 && (strategy & SAVE_INLINE_VRS) == 0)
22182 int end_save = info->altivec_save_offset + info->altivec_size;
22183 int ptr_off;
22184 /* Oddly, the vector save/restore functions point r0 at the end
22185 of the save area, then use r11 or r12 to load offsets for
22186 [reg+reg] addressing. */
22187 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
22188 int scratch_regno = ptr_regno_for_savres (SAVRES_SAVE | SAVRES_VR);
22189 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
22191 gcc_checking_assert (scratch_regno == 11 || scratch_regno == 12);
22192 NOT_INUSE (0);
22193 if (end_save + frame_off != 0)
22195 rtx offset = GEN_INT (end_save + frame_off);
22197 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
22199 else
22200 emit_move_insn (ptr_reg, frame_reg_rtx);
22202 ptr_off = -end_save;
22203 insn = rs6000_emit_savres_rtx (info, scratch_reg,
22204 info->altivec_save_offset + ptr_off,
22205 0, V4SImode, SAVRES_SAVE | SAVRES_VR);
22206 rs6000_frame_related (insn, scratch_reg, sp_off - ptr_off,
22207 NULL_RTX, NULL_RTX);
22208 if (REGNO (frame_reg_rtx) == REGNO (scratch_reg))
22210 /* The oddity mentioned above clobbered our frame reg. */
22211 emit_move_insn (frame_reg_rtx, ptr_reg);
22212 frame_off = ptr_off;
22215 else if (!WORLD_SAVE_P (info) && TARGET_ALTIVEC_ABI
22216 && info->altivec_size != 0)
22218 int i;
22220 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
22221 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
22223 rtx areg, savereg, mem;
22224 int offset;
22226 offset = (info->altivec_save_offset + frame_off
22227 + 16 * (i - info->first_altivec_reg_save));
22229 savereg = gen_rtx_REG (V4SImode, i);
22231 NOT_INUSE (0);
22232 areg = gen_rtx_REG (Pmode, 0);
22233 emit_move_insn (areg, GEN_INT (offset));
22235 /* AltiVec addressing mode is [reg+reg]. */
22236 mem = gen_frame_mem (V4SImode,
22237 gen_rtx_PLUS (Pmode, frame_reg_rtx, areg));
22239 insn = emit_move_insn (mem, savereg);
22241 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
22242 areg, GEN_INT (offset));
22246 /* VRSAVE is a bit vector representing which AltiVec registers
22247 are used. The OS uses this to determine which vector
22248 registers to save on a context switch. We need to save
22249 VRSAVE on the stack frame, add whatever AltiVec registers we
22250 used in this function, and do the corresponding magic in the
22251 epilogue. */
22253 if (!WORLD_SAVE_P (info)
22254 && TARGET_ALTIVEC
22255 && TARGET_ALTIVEC_VRSAVE
22256 && info->vrsave_mask != 0)
22258 rtx reg, vrsave;
22259 int offset;
22260 int save_regno;
22262 /* Get VRSAVE onto a GPR. Note that ABI_V4 and ABI_DARWIN might
22263 be using r12 as frame_reg_rtx and r11 as the static chain
22264 pointer for nested functions. */
22265 save_regno = 12;
22266 if (DEFAULT_ABI == ABI_AIX && !using_static_chain_p)
22267 save_regno = 11;
22268 else if (REGNO (frame_reg_rtx) == 12)
22270 save_regno = 11;
22271 if (using_static_chain_p)
22272 save_regno = 0;
22275 NOT_INUSE (save_regno);
22276 reg = gen_rtx_REG (SImode, save_regno);
22277 vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
22278 if (TARGET_MACHO)
22279 emit_insn (gen_get_vrsave_internal (reg));
22280 else
22281 emit_insn (gen_rtx_SET (VOIDmode, reg, vrsave));
22283 /* Save VRSAVE. */
22284 offset = info->vrsave_save_offset + frame_off;
22285 insn = emit_insn (gen_frame_store (reg, frame_reg_rtx, offset));
22287 /* Include the registers in the mask. */
22288 emit_insn (gen_iorsi3 (reg, reg, GEN_INT ((int) info->vrsave_mask)));
22290 insn = emit_insn (generate_set_vrsave (reg, info, 0));
22293 /* If we are using RS6000_PIC_OFFSET_TABLE_REGNUM, we need to set it up. */
22294 if (!TARGET_SINGLE_PIC_BASE
22295 && ((TARGET_TOC && TARGET_MINIMAL_TOC && get_pool_size () != 0)
22296 || (DEFAULT_ABI == ABI_V4
22297 && (flag_pic == 1 || (flag_pic && TARGET_SECURE_PLT))
22298 && df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))))
22300 /* If emit_load_toc_table will use the link register, we need to save
22301 it. We use R12 for this purpose because emit_load_toc_table
22302 can use register 0. This allows us to use a plain 'blr' to return
22303 from the procedure more often. */
22304 int save_LR_around_toc_setup = (TARGET_ELF
22305 && DEFAULT_ABI != ABI_AIX
22306 && flag_pic
22307 && ! info->lr_save_p
22308 && EDGE_COUNT (EXIT_BLOCK_PTR->preds) > 0);
22309 if (save_LR_around_toc_setup)
22311 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
22312 rtx tmp = gen_rtx_REG (Pmode, 12);
22314 insn = emit_move_insn (tmp, lr);
22315 RTX_FRAME_RELATED_P (insn) = 1;
22317 rs6000_emit_load_toc_table (TRUE);
22319 insn = emit_move_insn (lr, tmp);
22320 add_reg_note (insn, REG_CFA_RESTORE, lr);
22321 RTX_FRAME_RELATED_P (insn) = 1;
22323 else
22324 rs6000_emit_load_toc_table (TRUE);
22327 #if TARGET_MACHO
22328 if (!TARGET_SINGLE_PIC_BASE
22329 && DEFAULT_ABI == ABI_DARWIN
22330 && flag_pic && crtl->uses_pic_offset_table)
22332 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
22333 rtx src = gen_rtx_SYMBOL_REF (Pmode, MACHOPIC_FUNCTION_BASE_NAME);
22335 /* Save and restore LR locally around this call (in R0). */
22336 if (!info->lr_save_p)
22337 emit_move_insn (gen_rtx_REG (Pmode, 0), lr);
22339 emit_insn (gen_load_macho_picbase (src));
22341 emit_move_insn (gen_rtx_REG (Pmode,
22342 RS6000_PIC_OFFSET_TABLE_REGNUM),
22343 lr);
22345 if (!info->lr_save_p)
22346 emit_move_insn (lr, gen_rtx_REG (Pmode, 0));
22348 #endif
22350 /* If we need to, save the TOC register after doing the stack setup.
22351 Do not emit eh frame info for this save. The unwinder wants info,
22352 conceptually attached to instructions in this function, about
22353 register values in the caller of this function. This R2 may have
22354 already been changed from the value in the caller.
22355 We don't attempt to write accurate DWARF EH frame info for R2
22356 because code emitted by gcc for a (non-pointer) function call
22357 doesn't save and restore R2. Instead, R2 is managed out-of-line
22358 by a linker generated plt call stub when the function resides in
22359 a shared library. This behaviour is costly to describe in DWARF,
22360 both in terms of the size of DWARF info and the time taken in the
22361 unwinder to interpret it. R2 changes, apart from the
22362 calls_eh_return case earlier in this function, are handled by
22363 linux-unwind.h frob_update_context. */
22364 if (rs6000_save_toc_in_prologue_p ())
22366 rtx reg = gen_rtx_REG (reg_mode, TOC_REGNUM);
22367 emit_insn (gen_frame_store (reg, sp_reg_rtx, 5 * reg_size));
22371 /* Write function prologue. */
22373 static void
22374 rs6000_output_function_prologue (FILE *file,
22375 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
22377 rs6000_stack_t *info = rs6000_stack_info ();
22379 if (TARGET_DEBUG_STACK)
22380 debug_stack_info (info);
22382 /* Write .extern for any function we will call to save and restore
22383 fp values. */
22384 if (info->first_fp_reg_save < 64
22385 && !TARGET_MACHO
22386 && !TARGET_ELF)
22388 char *name;
22389 int regno = info->first_fp_reg_save - 32;
22391 if ((info->savres_strategy & SAVE_INLINE_FPRS) == 0)
22393 bool lr = (info->savres_strategy & SAVE_NOINLINE_FPRS_SAVES_LR) != 0;
22394 int sel = SAVRES_SAVE | SAVRES_FPR | (lr ? SAVRES_LR : 0);
22395 name = rs6000_savres_routine_name (info, regno, sel);
22396 fprintf (file, "\t.extern %s\n", name);
22398 if ((info->savres_strategy & REST_INLINE_FPRS) == 0)
22400 bool lr = (info->savres_strategy
22401 & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
22402 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
22403 name = rs6000_savres_routine_name (info, regno, sel);
22404 fprintf (file, "\t.extern %s\n", name);
22408 rs6000_pic_labelno++;
22411 /* Non-zero if vmx regs are restored before the frame pop, zero if
22412 we restore after the pop when possible. */
22413 #define ALWAYS_RESTORE_ALTIVEC_BEFORE_POP 0
22415 /* Restoring cr is a two step process: loading a reg from the frame
22416 save, then moving the reg to cr. For ABI_V4 we must let the
22417 unwinder know that the stack location is no longer valid at or
22418 before the stack deallocation, but we can't emit a cfa_restore for
22419 cr at the stack deallocation like we do for other registers.
22420 The trouble is that it is possible for the move to cr to be
22421 scheduled after the stack deallocation. So say exactly where cr
22422 is located on each of the two insns. */
22424 static rtx
22425 load_cr_save (int regno, rtx frame_reg_rtx, int offset, bool exit_func)
22427 rtx mem = gen_frame_mem_offset (SImode, frame_reg_rtx, offset);
22428 rtx reg = gen_rtx_REG (SImode, regno);
22429 rtx insn = emit_move_insn (reg, mem);
22431 if (!exit_func && DEFAULT_ABI == ABI_V4)
22433 rtx cr = gen_rtx_REG (SImode, CR2_REGNO);
22434 rtx set = gen_rtx_SET (VOIDmode, reg, cr);
22436 add_reg_note (insn, REG_CFA_REGISTER, set);
22437 RTX_FRAME_RELATED_P (insn) = 1;
22439 return reg;
22442 /* Reload CR from REG. */
22444 static void
22445 restore_saved_cr (rtx reg, int using_mfcr_multiple, bool exit_func)
22447 int count = 0;
22448 int i;
22450 if (using_mfcr_multiple)
22452 for (i = 0; i < 8; i++)
22453 if (save_reg_p (CR0_REGNO + i))
22454 count++;
22455 gcc_assert (count);
22458 if (using_mfcr_multiple && count > 1)
22460 rtvec p;
22461 int ndx;
22463 p = rtvec_alloc (count);
22465 ndx = 0;
22466 for (i = 0; i < 8; i++)
22467 if (save_reg_p (CR0_REGNO + i))
22469 rtvec r = rtvec_alloc (2);
22470 RTVEC_ELT (r, 0) = reg;
22471 RTVEC_ELT (r, 1) = GEN_INT (1 << (7-i));
22472 RTVEC_ELT (p, ndx) =
22473 gen_rtx_SET (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO + i),
22474 gen_rtx_UNSPEC (CCmode, r, UNSPEC_MOVESI_TO_CR));
22475 ndx++;
22477 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
22478 gcc_assert (ndx == count);
22480 else
22481 for (i = 0; i < 8; i++)
22482 if (save_reg_p (CR0_REGNO + i))
22483 emit_insn (gen_movsi_to_cr_one (gen_rtx_REG (CCmode, CR0_REGNO + i),
22484 reg));
22486 if (!exit_func && (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap))
22488 rtx insn = get_last_insn ();
22489 rtx cr = gen_rtx_REG (SImode, CR2_REGNO);
22491 add_reg_note (insn, REG_CFA_RESTORE, cr);
22492 RTX_FRAME_RELATED_P (insn) = 1;
22496 /* Like cr, the move to lr instruction can be scheduled after the
22497 stack deallocation, but unlike cr, its stack frame save is still
22498 valid. So we only need to emit the cfa_restore on the correct
22499 instruction. */
22501 static void
22502 load_lr_save (int regno, rtx frame_reg_rtx, int offset)
22504 rtx mem = gen_frame_mem_offset (Pmode, frame_reg_rtx, offset);
22505 rtx reg = gen_rtx_REG (Pmode, regno);
22507 emit_move_insn (reg, mem);
22510 static void
22511 restore_saved_lr (int regno, bool exit_func)
22513 rtx reg = gen_rtx_REG (Pmode, regno);
22514 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
22515 rtx insn = emit_move_insn (lr, reg);
22517 if (!exit_func && flag_shrink_wrap)
22519 add_reg_note (insn, REG_CFA_RESTORE, lr);
22520 RTX_FRAME_RELATED_P (insn) = 1;
22524 static rtx
22525 add_crlr_cfa_restore (const rs6000_stack_t *info, rtx cfa_restores)
22527 if (info->cr_save_p)
22528 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
22529 gen_rtx_REG (SImode, CR2_REGNO),
22530 cfa_restores);
22531 if (info->lr_save_p)
22532 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
22533 gen_rtx_REG (Pmode, LR_REGNO),
22534 cfa_restores);
22535 return cfa_restores;
22538 /* Return true if OFFSET from stack pointer can be clobbered by signals.
22539 V.4 doesn't have any stack cushion, AIX ABIs have 220 or 288 bytes
22540 below stack pointer not cloberred by signals. */
22542 static inline bool
22543 offset_below_red_zone_p (HOST_WIDE_INT offset)
22545 return offset < (DEFAULT_ABI == ABI_V4
22547 : TARGET_32BIT ? -220 : -288);
22550 /* Append CFA_RESTORES to any existing REG_NOTES on the last insn. */
22552 static void
22553 emit_cfa_restores (rtx cfa_restores)
22555 rtx insn = get_last_insn ();
22556 rtx *loc = &REG_NOTES (insn);
22558 while (*loc)
22559 loc = &XEXP (*loc, 1);
22560 *loc = cfa_restores;
22561 RTX_FRAME_RELATED_P (insn) = 1;
22564 /* Emit function epilogue as insns. */
22566 void
22567 rs6000_emit_epilogue (int sibcall)
22569 rs6000_stack_t *info;
22570 int restoring_GPRs_inline;
22571 int restoring_FPRs_inline;
22572 int using_load_multiple;
22573 int using_mtcr_multiple;
22574 int use_backchain_to_restore_sp;
22575 int restore_lr;
22576 int strategy;
22577 HOST_WIDE_INT frame_off = 0;
22578 rtx sp_reg_rtx = gen_rtx_REG (Pmode, 1);
22579 rtx frame_reg_rtx = sp_reg_rtx;
22580 rtx cfa_restores = NULL_RTX;
22581 rtx insn;
22582 rtx cr_save_reg = NULL_RTX;
22583 enum machine_mode reg_mode = Pmode;
22584 int reg_size = TARGET_32BIT ? 4 : 8;
22585 int i;
22586 bool exit_func;
22587 unsigned ptr_regno;
22589 info = rs6000_stack_info ();
22591 if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
22593 reg_mode = V2SImode;
22594 reg_size = 8;
22597 strategy = info->savres_strategy;
22598 using_load_multiple = strategy & SAVRES_MULTIPLE;
22599 restoring_FPRs_inline = sibcall || (strategy & REST_INLINE_FPRS);
22600 restoring_GPRs_inline = sibcall || (strategy & REST_INLINE_GPRS);
22601 using_mtcr_multiple = (rs6000_cpu == PROCESSOR_PPC601
22602 || rs6000_cpu == PROCESSOR_PPC603
22603 || rs6000_cpu == PROCESSOR_PPC750
22604 || optimize_size);
22605 /* Restore via the backchain when we have a large frame, since this
22606 is more efficient than an addis, addi pair. The second condition
22607 here will not trigger at the moment; We don't actually need a
22608 frame pointer for alloca, but the generic parts of the compiler
22609 give us one anyway. */
22610 use_backchain_to_restore_sp = (info->total_size > 32767 - info->lr_save_offset
22611 || (cfun->calls_alloca
22612 && !frame_pointer_needed));
22613 restore_lr = (info->lr_save_p
22614 && (restoring_FPRs_inline
22615 || (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR))
22616 && (restoring_GPRs_inline
22617 || info->first_fp_reg_save < 64));
22619 if (WORLD_SAVE_P (info))
22621 int i, j;
22622 char rname[30];
22623 const char *alloc_rname;
22624 rtvec p;
22626 /* eh_rest_world_r10 will return to the location saved in the LR
22627 stack slot (which is not likely to be our caller.)
22628 Input: R10 -- stack adjustment. Clobbers R0, R11, R12, R7, R8.
22629 rest_world is similar, except any R10 parameter is ignored.
22630 The exception-handling stuff that was here in 2.95 is no
22631 longer necessary. */
22633 p = rtvec_alloc (9
22635 + 32 - info->first_gp_reg_save
22636 + LAST_ALTIVEC_REGNO + 1 - info->first_altivec_reg_save
22637 + 63 + 1 - info->first_fp_reg_save);
22639 strcpy (rname, ((crtl->calls_eh_return) ?
22640 "*eh_rest_world_r10" : "*rest_world"));
22641 alloc_rname = ggc_strdup (rname);
22643 j = 0;
22644 RTVEC_ELT (p, j++) = ret_rtx;
22645 RTVEC_ELT (p, j++) = gen_rtx_USE (VOIDmode,
22646 gen_rtx_REG (Pmode,
22647 LR_REGNO));
22648 RTVEC_ELT (p, j++)
22649 = gen_rtx_USE (VOIDmode, gen_rtx_SYMBOL_REF (Pmode, alloc_rname));
22650 /* The instruction pattern requires a clobber here;
22651 it is shared with the restVEC helper. */
22652 RTVEC_ELT (p, j++)
22653 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 11));
22656 /* CR register traditionally saved as CR2. */
22657 rtx reg = gen_rtx_REG (SImode, CR2_REGNO);
22658 RTVEC_ELT (p, j++)
22659 = gen_frame_load (reg, frame_reg_rtx, info->cr_save_offset);
22660 if (flag_shrink_wrap)
22662 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
22663 gen_rtx_REG (Pmode, LR_REGNO),
22664 cfa_restores);
22665 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
22669 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
22671 rtx reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
22672 RTVEC_ELT (p, j++)
22673 = gen_frame_load (reg,
22674 frame_reg_rtx, info->gp_save_offset + reg_size * i);
22675 if (flag_shrink_wrap)
22676 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
22678 for (i = 0; info->first_altivec_reg_save + i <= LAST_ALTIVEC_REGNO; i++)
22680 rtx reg = gen_rtx_REG (V4SImode, info->first_altivec_reg_save + i);
22681 RTVEC_ELT (p, j++)
22682 = gen_frame_load (reg,
22683 frame_reg_rtx, info->altivec_save_offset + 16 * i);
22684 if (flag_shrink_wrap)
22685 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
22687 for (i = 0; info->first_fp_reg_save + i <= 63; i++)
22689 rtx reg = gen_rtx_REG ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
22690 ? DFmode : SFmode),
22691 info->first_fp_reg_save + i);
22692 RTVEC_ELT (p, j++)
22693 = gen_frame_load (reg, frame_reg_rtx, info->fp_save_offset + 8 * i);
22694 if (flag_shrink_wrap)
22695 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
22697 RTVEC_ELT (p, j++)
22698 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 0));
22699 RTVEC_ELT (p, j++)
22700 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 12));
22701 RTVEC_ELT (p, j++)
22702 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 7));
22703 RTVEC_ELT (p, j++)
22704 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 8));
22705 RTVEC_ELT (p, j++)
22706 = gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, 10));
22707 insn = emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
22709 if (flag_shrink_wrap)
22711 REG_NOTES (insn) = cfa_restores;
22712 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
22713 RTX_FRAME_RELATED_P (insn) = 1;
22715 return;
22718 /* frame_reg_rtx + frame_off points to the top of this stack frame. */
22719 if (info->push_p)
22720 frame_off = info->total_size;
22722 /* Restore AltiVec registers if we must do so before adjusting the
22723 stack. */
22724 if (TARGET_ALTIVEC_ABI
22725 && info->altivec_size != 0
22726 && (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
22727 || (DEFAULT_ABI != ABI_V4
22728 && offset_below_red_zone_p (info->altivec_save_offset))))
22730 int i;
22731 int scratch_regno = ptr_regno_for_savres (SAVRES_VR);
22733 gcc_checking_assert (scratch_regno == 11 || scratch_regno == 12);
22734 if (use_backchain_to_restore_sp)
22736 int frame_regno = 11;
22738 if ((strategy & REST_INLINE_VRS) == 0)
22740 /* Of r11 and r12, select the one not clobbered by an
22741 out-of-line restore function for the frame register. */
22742 frame_regno = 11 + 12 - scratch_regno;
22744 frame_reg_rtx = gen_rtx_REG (Pmode, frame_regno);
22745 emit_move_insn (frame_reg_rtx,
22746 gen_rtx_MEM (Pmode, sp_reg_rtx));
22747 frame_off = 0;
22749 else if (frame_pointer_needed)
22750 frame_reg_rtx = hard_frame_pointer_rtx;
22752 if ((strategy & REST_INLINE_VRS) == 0)
22754 int end_save = info->altivec_save_offset + info->altivec_size;
22755 int ptr_off;
22756 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
22757 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
22759 if (end_save + frame_off != 0)
22761 rtx offset = GEN_INT (end_save + frame_off);
22763 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
22765 else
22766 emit_move_insn (ptr_reg, frame_reg_rtx);
22768 ptr_off = -end_save;
22769 insn = rs6000_emit_savres_rtx (info, scratch_reg,
22770 info->altivec_save_offset + ptr_off,
22771 0, V4SImode, SAVRES_VR);
22773 else
22775 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
22776 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
22778 rtx addr, areg, mem, reg;
22780 areg = gen_rtx_REG (Pmode, 0);
22781 emit_move_insn
22782 (areg, GEN_INT (info->altivec_save_offset
22783 + frame_off
22784 + 16 * (i - info->first_altivec_reg_save)));
22786 /* AltiVec addressing mode is [reg+reg]. */
22787 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
22788 mem = gen_frame_mem (V4SImode, addr);
22790 reg = gen_rtx_REG (V4SImode, i);
22791 emit_move_insn (reg, mem);
22795 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
22796 if (((strategy & REST_INLINE_VRS) == 0
22797 || (info->vrsave_mask & ALTIVEC_REG_BIT (i)) != 0)
22798 && (flag_shrink_wrap
22799 || (offset_below_red_zone_p
22800 (info->altivec_save_offset
22801 + 16 * (i - info->first_altivec_reg_save)))))
22803 rtx reg = gen_rtx_REG (V4SImode, i);
22804 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
22808 /* Restore VRSAVE if we must do so before adjusting the stack. */
22809 if (TARGET_ALTIVEC
22810 && TARGET_ALTIVEC_VRSAVE
22811 && info->vrsave_mask != 0
22812 && (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
22813 || (DEFAULT_ABI != ABI_V4
22814 && offset_below_red_zone_p (info->vrsave_save_offset))))
22816 rtx reg;
22818 if (frame_reg_rtx == sp_reg_rtx)
22820 if (use_backchain_to_restore_sp)
22822 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
22823 emit_move_insn (frame_reg_rtx,
22824 gen_rtx_MEM (Pmode, sp_reg_rtx));
22825 frame_off = 0;
22827 else if (frame_pointer_needed)
22828 frame_reg_rtx = hard_frame_pointer_rtx;
22831 reg = gen_rtx_REG (SImode, 12);
22832 emit_insn (gen_frame_load (reg, frame_reg_rtx,
22833 info->vrsave_save_offset + frame_off));
22835 emit_insn (generate_set_vrsave (reg, info, 1));
22838 insn = NULL_RTX;
22839 /* If we have a large stack frame, restore the old stack pointer
22840 using the backchain. */
22841 if (use_backchain_to_restore_sp)
22843 if (frame_reg_rtx == sp_reg_rtx)
22845 /* Under V.4, don't reset the stack pointer until after we're done
22846 loading the saved registers. */
22847 if (DEFAULT_ABI == ABI_V4)
22848 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
22850 insn = emit_move_insn (frame_reg_rtx,
22851 gen_rtx_MEM (Pmode, sp_reg_rtx));
22852 frame_off = 0;
22854 else if (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
22855 && DEFAULT_ABI == ABI_V4)
22856 /* frame_reg_rtx has been set up by the altivec restore. */
22858 else
22860 insn = emit_move_insn (sp_reg_rtx, frame_reg_rtx);
22861 frame_reg_rtx = sp_reg_rtx;
22864 /* If we have a frame pointer, we can restore the old stack pointer
22865 from it. */
22866 else if (frame_pointer_needed)
22868 frame_reg_rtx = sp_reg_rtx;
22869 if (DEFAULT_ABI == ABI_V4)
22870 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
22871 /* Prevent reordering memory accesses against stack pointer restore. */
22872 else if (cfun->calls_alloca
22873 || offset_below_red_zone_p (-info->total_size))
22874 rs6000_emit_stack_tie (frame_reg_rtx, true);
22876 insn = emit_insn (gen_add3_insn (frame_reg_rtx, hard_frame_pointer_rtx,
22877 GEN_INT (info->total_size)));
22878 frame_off = 0;
22880 else if (info->push_p
22881 && DEFAULT_ABI != ABI_V4
22882 && !crtl->calls_eh_return)
22884 /* Prevent reordering memory accesses against stack pointer restore. */
22885 if (cfun->calls_alloca
22886 || offset_below_red_zone_p (-info->total_size))
22887 rs6000_emit_stack_tie (frame_reg_rtx, false);
22888 insn = emit_insn (gen_add3_insn (sp_reg_rtx, sp_reg_rtx,
22889 GEN_INT (info->total_size)));
22890 frame_off = 0;
22892 if (insn && frame_reg_rtx == sp_reg_rtx)
22894 if (cfa_restores)
22896 REG_NOTES (insn) = cfa_restores;
22897 cfa_restores = NULL_RTX;
22899 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
22900 RTX_FRAME_RELATED_P (insn) = 1;
22903 /* Restore AltiVec registers if we have not done so already. */
22904 if (!ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
22905 && TARGET_ALTIVEC_ABI
22906 && info->altivec_size != 0
22907 && (DEFAULT_ABI == ABI_V4
22908 || !offset_below_red_zone_p (info->altivec_save_offset)))
22910 int i;
22912 if ((strategy & REST_INLINE_VRS) == 0)
22914 int end_save = info->altivec_save_offset + info->altivec_size;
22915 int ptr_off;
22916 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
22917 int scratch_regno = ptr_regno_for_savres (SAVRES_VR);
22918 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
22920 if (end_save + frame_off != 0)
22922 rtx offset = GEN_INT (end_save + frame_off);
22924 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
22926 else
22927 emit_move_insn (ptr_reg, frame_reg_rtx);
22929 ptr_off = -end_save;
22930 insn = rs6000_emit_savres_rtx (info, scratch_reg,
22931 info->altivec_save_offset + ptr_off,
22932 0, V4SImode, SAVRES_VR);
22933 if (REGNO (frame_reg_rtx) == REGNO (scratch_reg))
22935 /* Frame reg was clobbered by out-of-line save. Restore it
22936 from ptr_reg, and if we are calling out-of-line gpr or
22937 fpr restore set up the correct pointer and offset. */
22938 unsigned newptr_regno = 1;
22939 if (!restoring_GPRs_inline)
22941 bool lr = info->gp_save_offset + info->gp_size == 0;
22942 int sel = SAVRES_GPR | (lr ? SAVRES_LR : 0);
22943 newptr_regno = ptr_regno_for_savres (sel);
22944 end_save = info->gp_save_offset + info->gp_size;
22946 else if (!restoring_FPRs_inline)
22948 bool lr = !(strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR);
22949 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
22950 newptr_regno = ptr_regno_for_savres (sel);
22951 end_save = info->gp_save_offset + info->gp_size;
22954 if (newptr_regno != 1 && REGNO (frame_reg_rtx) != newptr_regno)
22955 frame_reg_rtx = gen_rtx_REG (Pmode, newptr_regno);
22957 if (end_save + ptr_off != 0)
22959 rtx offset = GEN_INT (end_save + ptr_off);
22961 frame_off = -end_save;
22962 emit_insn (gen_add3_insn (frame_reg_rtx, ptr_reg, offset));
22964 else
22966 frame_off = ptr_off;
22967 emit_move_insn (frame_reg_rtx, ptr_reg);
22971 else
22973 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
22974 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
22976 rtx addr, areg, mem, reg;
22978 areg = gen_rtx_REG (Pmode, 0);
22979 emit_move_insn
22980 (areg, GEN_INT (info->altivec_save_offset
22981 + frame_off
22982 + 16 * (i - info->first_altivec_reg_save)));
22984 /* AltiVec addressing mode is [reg+reg]. */
22985 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
22986 mem = gen_frame_mem (V4SImode, addr);
22988 reg = gen_rtx_REG (V4SImode, i);
22989 emit_move_insn (reg, mem);
22993 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
22994 if (((strategy & REST_INLINE_VRS) == 0
22995 || (info->vrsave_mask & ALTIVEC_REG_BIT (i)) != 0)
22996 && (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap))
22998 rtx reg = gen_rtx_REG (V4SImode, i);
22999 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
23003 /* Restore VRSAVE if we have not done so already. */
23004 if (!ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
23005 && TARGET_ALTIVEC
23006 && TARGET_ALTIVEC_VRSAVE
23007 && info->vrsave_mask != 0
23008 && (DEFAULT_ABI == ABI_V4
23009 || !offset_below_red_zone_p (info->vrsave_save_offset)))
23011 rtx reg;
23013 reg = gen_rtx_REG (SImode, 12);
23014 emit_insn (gen_frame_load (reg, frame_reg_rtx,
23015 info->vrsave_save_offset + frame_off));
23017 emit_insn (generate_set_vrsave (reg, info, 1));
23020 /* If we exit by an out-of-line restore function on ABI_V4 then that
23021 function will deallocate the stack, so we don't need to worry
23022 about the unwinder restoring cr from an invalid stack frame
23023 location. */
23024 exit_func = (!restoring_FPRs_inline
23025 || (!restoring_GPRs_inline
23026 && info->first_fp_reg_save == 64));
23028 /* Get the old lr if we saved it. If we are restoring registers
23029 out-of-line, then the out-of-line routines can do this for us. */
23030 if (restore_lr && restoring_GPRs_inline)
23031 load_lr_save (0, frame_reg_rtx, info->lr_save_offset + frame_off);
23033 /* Get the old cr if we saved it. */
23034 if (info->cr_save_p)
23036 unsigned cr_save_regno = 12;
23038 if (!restoring_GPRs_inline)
23040 /* Ensure we don't use the register used by the out-of-line
23041 gpr register restore below. */
23042 bool lr = info->gp_save_offset + info->gp_size == 0;
23043 int sel = SAVRES_GPR | (lr ? SAVRES_LR : 0);
23044 int gpr_ptr_regno = ptr_regno_for_savres (sel);
23046 if (gpr_ptr_regno == 12)
23047 cr_save_regno = 11;
23048 gcc_checking_assert (REGNO (frame_reg_rtx) != cr_save_regno);
23050 else if (REGNO (frame_reg_rtx) == 12)
23051 cr_save_regno = 11;
23053 cr_save_reg = load_cr_save (cr_save_regno, frame_reg_rtx,
23054 info->cr_save_offset + frame_off,
23055 exit_func);
23058 /* Set LR here to try to overlap restores below. */
23059 if (restore_lr && restoring_GPRs_inline)
23060 restore_saved_lr (0, exit_func);
23062 /* Load exception handler data registers, if needed. */
23063 if (crtl->calls_eh_return)
23065 unsigned int i, regno;
23067 if (TARGET_AIX)
23069 rtx reg = gen_rtx_REG (reg_mode, 2);
23070 emit_insn (gen_frame_load (reg, frame_reg_rtx,
23071 frame_off + 5 * reg_size));
23074 for (i = 0; ; ++i)
23076 rtx mem;
23078 regno = EH_RETURN_DATA_REGNO (i);
23079 if (regno == INVALID_REGNUM)
23080 break;
23082 /* Note: possible use of r0 here to address SPE regs. */
23083 mem = gen_frame_mem_offset (reg_mode, frame_reg_rtx,
23084 info->ehrd_offset + frame_off
23085 + reg_size * (int) i);
23087 emit_move_insn (gen_rtx_REG (reg_mode, regno), mem);
23091 /* Restore GPRs. This is done as a PARALLEL if we are using
23092 the load-multiple instructions. */
23093 if (TARGET_SPE_ABI
23094 && info->spe_64bit_regs_used
23095 && info->first_gp_reg_save != 32)
23097 /* Determine whether we can address all of the registers that need
23098 to be saved with an offset from frame_reg_rtx that fits in
23099 the small const field for SPE memory instructions. */
23100 int spe_regs_addressable
23101 = (SPE_CONST_OFFSET_OK (info->spe_gp_save_offset + frame_off
23102 + reg_size * (32 - info->first_gp_reg_save - 1))
23103 && restoring_GPRs_inline);
23105 if (!spe_regs_addressable)
23107 int ool_adjust = 0;
23108 rtx old_frame_reg_rtx = frame_reg_rtx;
23109 /* Make r11 point to the start of the SPE save area. We worried about
23110 not clobbering it when we were saving registers in the prologue.
23111 There's no need to worry here because the static chain is passed
23112 anew to every function. */
23114 if (!restoring_GPRs_inline)
23115 ool_adjust = 8 * (info->first_gp_reg_save - FIRST_SAVED_GP_REGNO);
23116 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
23117 emit_insn (gen_addsi3 (frame_reg_rtx, old_frame_reg_rtx,
23118 GEN_INT (info->spe_gp_save_offset
23119 + frame_off
23120 - ool_adjust)));
23121 /* Keep the invariant that frame_reg_rtx + frame_off points
23122 at the top of the stack frame. */
23123 frame_off = -info->spe_gp_save_offset + ool_adjust;
23126 if (restoring_GPRs_inline)
23128 HOST_WIDE_INT spe_offset = info->spe_gp_save_offset + frame_off;
23130 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
23131 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
23133 rtx offset, addr, mem, reg;
23135 /* We're doing all this to ensure that the immediate offset
23136 fits into the immediate field of 'evldd'. */
23137 gcc_assert (SPE_CONST_OFFSET_OK (spe_offset + reg_size * i));
23139 offset = GEN_INT (spe_offset + reg_size * i);
23140 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, offset);
23141 mem = gen_rtx_MEM (V2SImode, addr);
23142 reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
23144 emit_move_insn (reg, mem);
23147 else
23148 rs6000_emit_savres_rtx (info, frame_reg_rtx,
23149 info->spe_gp_save_offset + frame_off,
23150 info->lr_save_offset + frame_off,
23151 reg_mode,
23152 SAVRES_GPR | SAVRES_LR);
23154 else if (!restoring_GPRs_inline)
23156 /* We are jumping to an out-of-line function. */
23157 rtx ptr_reg;
23158 int end_save = info->gp_save_offset + info->gp_size;
23159 bool can_use_exit = end_save == 0;
23160 int sel = SAVRES_GPR | (can_use_exit ? SAVRES_LR : 0);
23161 int ptr_off;
23163 /* Emit stack reset code if we need it. */
23164 ptr_regno = ptr_regno_for_savres (sel);
23165 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
23166 if (can_use_exit)
23167 rs6000_emit_stack_reset (info, frame_reg_rtx, frame_off, ptr_regno);
23168 else if (end_save + frame_off != 0)
23169 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx,
23170 GEN_INT (end_save + frame_off)));
23171 else if (REGNO (frame_reg_rtx) != ptr_regno)
23172 emit_move_insn (ptr_reg, frame_reg_rtx);
23173 if (REGNO (frame_reg_rtx) == ptr_regno)
23174 frame_off = -end_save;
23176 if (can_use_exit && info->cr_save_p)
23177 restore_saved_cr (cr_save_reg, using_mtcr_multiple, true);
23179 ptr_off = -end_save;
23180 rs6000_emit_savres_rtx (info, ptr_reg,
23181 info->gp_save_offset + ptr_off,
23182 info->lr_save_offset + ptr_off,
23183 reg_mode, sel);
23185 else if (using_load_multiple)
23187 rtvec p;
23188 p = rtvec_alloc (32 - info->first_gp_reg_save);
23189 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
23190 RTVEC_ELT (p, i)
23191 = gen_frame_load (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
23192 frame_reg_rtx,
23193 info->gp_save_offset + frame_off + reg_size * i);
23194 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
23196 else
23198 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
23199 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
23200 emit_insn (gen_frame_load
23201 (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
23202 frame_reg_rtx,
23203 info->gp_save_offset + frame_off + reg_size * i));
23206 if (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
23208 /* If the frame pointer was used then we can't delay emitting
23209 a REG_CFA_DEF_CFA note. This must happen on the insn that
23210 restores the frame pointer, r31. We may have already emitted
23211 a REG_CFA_DEF_CFA note, but that's OK; A duplicate is
23212 discarded by dwarf2cfi.c/dwarf2out.c, and in any case would
23213 be harmless if emitted. */
23214 if (frame_pointer_needed)
23216 insn = get_last_insn ();
23217 add_reg_note (insn, REG_CFA_DEF_CFA,
23218 plus_constant (Pmode, frame_reg_rtx, frame_off));
23219 RTX_FRAME_RELATED_P (insn) = 1;
23222 /* Set up cfa_restores. We always need these when
23223 shrink-wrapping. If not shrink-wrapping then we only need
23224 the cfa_restore when the stack location is no longer valid.
23225 The cfa_restores must be emitted on or before the insn that
23226 invalidates the stack, and of course must not be emitted
23227 before the insn that actually does the restore. The latter
23228 is why it is a bad idea to emit the cfa_restores as a group
23229 on the last instruction here that actually does a restore:
23230 That insn may be reordered with respect to others doing
23231 restores. */
23232 if (flag_shrink_wrap
23233 && !restoring_GPRs_inline
23234 && info->first_fp_reg_save == 64)
23235 cfa_restores = add_crlr_cfa_restore (info, cfa_restores);
23237 for (i = info->first_gp_reg_save; i < 32; i++)
23238 if (!restoring_GPRs_inline
23239 || using_load_multiple
23240 || rs6000_reg_live_or_pic_offset_p (i))
23242 rtx reg = gen_rtx_REG (reg_mode, i);
23244 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
23248 if (!restoring_GPRs_inline
23249 && info->first_fp_reg_save == 64)
23251 /* We are jumping to an out-of-line function. */
23252 if (cfa_restores)
23253 emit_cfa_restores (cfa_restores);
23254 return;
23257 if (restore_lr && !restoring_GPRs_inline)
23259 load_lr_save (0, frame_reg_rtx, info->lr_save_offset + frame_off);
23260 restore_saved_lr (0, exit_func);
23263 /* Restore fpr's if we need to do it without calling a function. */
23264 if (restoring_FPRs_inline)
23265 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
23266 if (save_reg_p (info->first_fp_reg_save + i))
23268 rtx reg = gen_rtx_REG ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
23269 ? DFmode : SFmode),
23270 info->first_fp_reg_save + i);
23271 emit_insn (gen_frame_load (reg, frame_reg_rtx,
23272 info->fp_save_offset + frame_off + 8 * i));
23273 if (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
23274 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
23277 /* If we saved cr, restore it here. Just those that were used. */
23278 if (info->cr_save_p)
23279 restore_saved_cr (cr_save_reg, using_mtcr_multiple, exit_func);
23281 /* If this is V.4, unwind the stack pointer after all of the loads
23282 have been done, or set up r11 if we are restoring fp out of line. */
23283 ptr_regno = 1;
23284 if (!restoring_FPRs_inline)
23286 bool lr = (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
23287 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
23288 ptr_regno = ptr_regno_for_savres (sel);
23291 insn = rs6000_emit_stack_reset (info, frame_reg_rtx, frame_off, ptr_regno);
23292 if (REGNO (frame_reg_rtx) == ptr_regno)
23293 frame_off = 0;
23295 if (insn && restoring_FPRs_inline)
23297 if (cfa_restores)
23299 REG_NOTES (insn) = cfa_restores;
23300 cfa_restores = NULL_RTX;
23302 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
23303 RTX_FRAME_RELATED_P (insn) = 1;
23306 if (crtl->calls_eh_return)
23308 rtx sa = EH_RETURN_STACKADJ_RTX;
23309 emit_insn (gen_add3_insn (sp_reg_rtx, sp_reg_rtx, sa));
23312 if (!sibcall)
23314 rtvec p;
23315 bool lr = (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
23316 if (! restoring_FPRs_inline)
23318 p = rtvec_alloc (4 + 64 - info->first_fp_reg_save);
23319 RTVEC_ELT (p, 0) = ret_rtx;
23321 else
23323 if (cfa_restores)
23325 /* We can't hang the cfa_restores off a simple return,
23326 since the shrink-wrap code sometimes uses an existing
23327 return. This means there might be a path from
23328 pre-prologue code to this return, and dwarf2cfi code
23329 wants the eh_frame unwinder state to be the same on
23330 all paths to any point. So we need to emit the
23331 cfa_restores before the return. For -m64 we really
23332 don't need epilogue cfa_restores at all, except for
23333 this irritating dwarf2cfi with shrink-wrap
23334 requirement; The stack red-zone means eh_frame info
23335 from the prologue telling the unwinder to restore
23336 from the stack is perfectly good right to the end of
23337 the function. */
23338 emit_insn (gen_blockage ());
23339 emit_cfa_restores (cfa_restores);
23340 cfa_restores = NULL_RTX;
23342 p = rtvec_alloc (2);
23343 RTVEC_ELT (p, 0) = simple_return_rtx;
23346 RTVEC_ELT (p, 1) = ((restoring_FPRs_inline || !lr)
23347 ? gen_rtx_USE (VOIDmode,
23348 gen_rtx_REG (Pmode, LR_REGNO))
23349 : gen_rtx_CLOBBER (VOIDmode,
23350 gen_rtx_REG (Pmode, LR_REGNO)));
23352 /* If we have to restore more than two FP registers, branch to the
23353 restore function. It will return to our caller. */
23354 if (! restoring_FPRs_inline)
23356 int i;
23357 rtx sym;
23359 if (flag_shrink_wrap)
23360 cfa_restores = add_crlr_cfa_restore (info, cfa_restores);
23362 sym = rs6000_savres_routine_sym (info,
23363 SAVRES_FPR | (lr ? SAVRES_LR : 0));
23364 RTVEC_ELT (p, 2) = gen_rtx_USE (VOIDmode, sym);
23365 RTVEC_ELT (p, 3) = gen_rtx_USE (VOIDmode,
23366 gen_rtx_REG (Pmode,
23367 DEFAULT_ABI == ABI_AIX
23368 ? 1 : 11));
23369 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
23371 rtx reg = gen_rtx_REG (DFmode, info->first_fp_reg_save + i);
23373 RTVEC_ELT (p, i + 4)
23374 = gen_frame_load (reg, sp_reg_rtx, info->fp_save_offset + 8 * i);
23375 if (flag_shrink_wrap)
23376 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg,
23377 cfa_restores);
23381 emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
23384 if (cfa_restores)
23386 if (sibcall)
23387 /* Ensure the cfa_restores are hung off an insn that won't
23388 be reordered above other restores. */
23389 emit_insn (gen_blockage ());
23391 emit_cfa_restores (cfa_restores);
23395 /* Write function epilogue. */
23397 static void
23398 rs6000_output_function_epilogue (FILE *file,
23399 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
23401 #if TARGET_MACHO
23402 macho_branch_islands ();
23403 /* Mach-O doesn't support labels at the end of objects, so if
23404 it looks like we might want one, insert a NOP. */
23406 rtx insn = get_last_insn ();
23407 rtx deleted_debug_label = NULL_RTX;
23408 while (insn
23409 && NOTE_P (insn)
23410 && NOTE_KIND (insn) != NOTE_INSN_DELETED_LABEL)
23412 /* Don't insert a nop for NOTE_INSN_DELETED_DEBUG_LABEL
23413 notes only, instead set their CODE_LABEL_NUMBER to -1,
23414 otherwise there would be code generation differences
23415 in between -g and -g0. */
23416 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
23417 deleted_debug_label = insn;
23418 insn = PREV_INSN (insn);
23420 if (insn
23421 && (LABEL_P (insn)
23422 || (NOTE_P (insn)
23423 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL)))
23424 fputs ("\tnop\n", file);
23425 else if (deleted_debug_label)
23426 for (insn = deleted_debug_label; insn; insn = NEXT_INSN (insn))
23427 if (NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
23428 CODE_LABEL_NUMBER (insn) = -1;
23430 #endif
23432 /* Output a traceback table here. See /usr/include/sys/debug.h for info
23433 on its format.
23435 We don't output a traceback table if -finhibit-size-directive was
23436 used. The documentation for -finhibit-size-directive reads
23437 ``don't output a @code{.size} assembler directive, or anything
23438 else that would cause trouble if the function is split in the
23439 middle, and the two halves are placed at locations far apart in
23440 memory.'' The traceback table has this property, since it
23441 includes the offset from the start of the function to the
23442 traceback table itself.
23444 System V.4 Powerpc's (and the embedded ABI derived from it) use a
23445 different traceback table. */
23446 if (DEFAULT_ABI == ABI_AIX && ! flag_inhibit_size_directive
23447 && rs6000_traceback != traceback_none && !cfun->is_thunk)
23449 const char *fname = NULL;
23450 const char *language_string = lang_hooks.name;
23451 int fixed_parms = 0, float_parms = 0, parm_info = 0;
23452 int i;
23453 int optional_tbtab;
23454 rs6000_stack_t *info = rs6000_stack_info ();
23456 if (rs6000_traceback == traceback_full)
23457 optional_tbtab = 1;
23458 else if (rs6000_traceback == traceback_part)
23459 optional_tbtab = 0;
23460 else
23461 optional_tbtab = !optimize_size && !TARGET_ELF;
23463 if (optional_tbtab)
23465 fname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
23466 while (*fname == '.') /* V.4 encodes . in the name */
23467 fname++;
23469 /* Need label immediately before tbtab, so we can compute
23470 its offset from the function start. */
23471 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
23472 ASM_OUTPUT_LABEL (file, fname);
23475 /* The .tbtab pseudo-op can only be used for the first eight
23476 expressions, since it can't handle the possibly variable
23477 length fields that follow. However, if you omit the optional
23478 fields, the assembler outputs zeros for all optional fields
23479 anyways, giving each variable length field is minimum length
23480 (as defined in sys/debug.h). Thus we can not use the .tbtab
23481 pseudo-op at all. */
23483 /* An all-zero word flags the start of the tbtab, for debuggers
23484 that have to find it by searching forward from the entry
23485 point or from the current pc. */
23486 fputs ("\t.long 0\n", file);
23488 /* Tbtab format type. Use format type 0. */
23489 fputs ("\t.byte 0,", file);
23491 /* Language type. Unfortunately, there does not seem to be any
23492 official way to discover the language being compiled, so we
23493 use language_string.
23494 C is 0. Fortran is 1. Pascal is 2. Ada is 3. C++ is 9.
23495 Java is 13. Objective-C is 14. Objective-C++ isn't assigned
23496 a number, so for now use 9. LTO, Go, and UPC aren't assigned numbers
23497 either, so for now use 0. */
23498 if (! strcmp (language_string, "GNU C")
23499 || ! strcmp (language_string, "GNU GIMPLE")
23500 || ! strcmp (language_string, "GNU Go")
23501 || ! strcmp (language_string, "GNU UPC"))
23502 i = 0;
23503 else if (! strcmp (language_string, "GNU F77")
23504 || ! strcmp (language_string, "GNU Fortran"))
23505 i = 1;
23506 else if (! strcmp (language_string, "GNU Pascal"))
23507 i = 2;
23508 else if (! strcmp (language_string, "GNU Ada"))
23509 i = 3;
23510 else if (! strcmp (language_string, "GNU C++")
23511 || ! strcmp (language_string, "GNU Objective-C++"))
23512 i = 9;
23513 else if (! strcmp (language_string, "GNU Java"))
23514 i = 13;
23515 else if (! strcmp (language_string, "GNU Objective-C"))
23516 i = 14;
23517 else
23518 gcc_unreachable ();
23519 fprintf (file, "%d,", i);
23521 /* 8 single bit fields: global linkage (not set for C extern linkage,
23522 apparently a PL/I convention?), out-of-line epilogue/prologue, offset
23523 from start of procedure stored in tbtab, internal function, function
23524 has controlled storage, function has no toc, function uses fp,
23525 function logs/aborts fp operations. */
23526 /* Assume that fp operations are used if any fp reg must be saved. */
23527 fprintf (file, "%d,",
23528 (optional_tbtab << 5) | ((info->first_fp_reg_save != 64) << 1));
23530 /* 6 bitfields: function is interrupt handler, name present in
23531 proc table, function calls alloca, on condition directives
23532 (controls stack walks, 3 bits), saves condition reg, saves
23533 link reg. */
23534 /* The `function calls alloca' bit seems to be set whenever reg 31 is
23535 set up as a frame pointer, even when there is no alloca call. */
23536 fprintf (file, "%d,",
23537 ((optional_tbtab << 6)
23538 | ((optional_tbtab & frame_pointer_needed) << 5)
23539 | (info->cr_save_p << 1)
23540 | (info->lr_save_p)));
23542 /* 3 bitfields: saves backchain, fixup code, number of fpr saved
23543 (6 bits). */
23544 fprintf (file, "%d,",
23545 (info->push_p << 7) | (64 - info->first_fp_reg_save));
23547 /* 2 bitfields: spare bits (2 bits), number of gpr saved (6 bits). */
23548 fprintf (file, "%d,", (32 - first_reg_to_save ()));
23550 if (optional_tbtab)
23552 /* Compute the parameter info from the function decl argument
23553 list. */
23554 tree decl;
23555 int next_parm_info_bit = 31;
23557 for (decl = DECL_ARGUMENTS (current_function_decl);
23558 decl; decl = DECL_CHAIN (decl))
23560 rtx parameter = DECL_INCOMING_RTL (decl);
23561 enum machine_mode mode = GET_MODE (parameter);
23563 if (GET_CODE (parameter) == REG)
23565 if (SCALAR_FLOAT_MODE_P (mode))
23567 int bits;
23569 float_parms++;
23571 switch (mode)
23573 case SFmode:
23574 case SDmode:
23575 bits = 0x2;
23576 break;
23578 case DFmode:
23579 case DDmode:
23580 case TFmode:
23581 case TDmode:
23582 bits = 0x3;
23583 break;
23585 default:
23586 gcc_unreachable ();
23589 /* If only one bit will fit, don't or in this entry. */
23590 if (next_parm_info_bit > 0)
23591 parm_info |= (bits << (next_parm_info_bit - 1));
23592 next_parm_info_bit -= 2;
23594 else
23596 fixed_parms += ((GET_MODE_SIZE (mode)
23597 + (UNITS_PER_WORD - 1))
23598 / UNITS_PER_WORD);
23599 next_parm_info_bit -= 1;
23605 /* Number of fixed point parameters. */
23606 /* This is actually the number of words of fixed point parameters; thus
23607 an 8 byte struct counts as 2; and thus the maximum value is 8. */
23608 fprintf (file, "%d,", fixed_parms);
23610 /* 2 bitfields: number of floating point parameters (7 bits), parameters
23611 all on stack. */
23612 /* This is actually the number of fp registers that hold parameters;
23613 and thus the maximum value is 13. */
23614 /* Set parameters on stack bit if parameters are not in their original
23615 registers, regardless of whether they are on the stack? Xlc
23616 seems to set the bit when not optimizing. */
23617 fprintf (file, "%d\n", ((float_parms << 1) | (! optimize)));
23619 if (! optional_tbtab)
23620 return;
23622 /* Optional fields follow. Some are variable length. */
23624 /* Parameter types, left adjusted bit fields: 0 fixed, 10 single float,
23625 11 double float. */
23626 /* There is an entry for each parameter in a register, in the order that
23627 they occur in the parameter list. Any intervening arguments on the
23628 stack are ignored. If the list overflows a long (max possible length
23629 34 bits) then completely leave off all elements that don't fit. */
23630 /* Only emit this long if there was at least one parameter. */
23631 if (fixed_parms || float_parms)
23632 fprintf (file, "\t.long %d\n", parm_info);
23634 /* Offset from start of code to tb table. */
23635 fputs ("\t.long ", file);
23636 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
23637 RS6000_OUTPUT_BASENAME (file, fname);
23638 putc ('-', file);
23639 rs6000_output_function_entry (file, fname);
23640 putc ('\n', file);
23642 /* Interrupt handler mask. */
23643 /* Omit this long, since we never set the interrupt handler bit
23644 above. */
23646 /* Number of CTL (controlled storage) anchors. */
23647 /* Omit this long, since the has_ctl bit is never set above. */
23649 /* Displacement into stack of each CTL anchor. */
23650 /* Omit this list of longs, because there are no CTL anchors. */
23652 /* Length of function name. */
23653 if (*fname == '*')
23654 ++fname;
23655 fprintf (file, "\t.short %d\n", (int) strlen (fname));
23657 /* Function name. */
23658 assemble_string (fname, strlen (fname));
23660 /* Register for alloca automatic storage; this is always reg 31.
23661 Only emit this if the alloca bit was set above. */
23662 if (frame_pointer_needed)
23663 fputs ("\t.byte 31\n", file);
23665 fputs ("\t.align 2\n", file);
23669 /* A C compound statement that outputs the assembler code for a thunk
23670 function, used to implement C++ virtual function calls with
23671 multiple inheritance. The thunk acts as a wrapper around a virtual
23672 function, adjusting the implicit object parameter before handing
23673 control off to the real function.
23675 First, emit code to add the integer DELTA to the location that
23676 contains the incoming first argument. Assume that this argument
23677 contains a pointer, and is the one used to pass the `this' pointer
23678 in C++. This is the incoming argument *before* the function
23679 prologue, e.g. `%o0' on a sparc. The addition must preserve the
23680 values of all other incoming arguments.
23682 After the addition, emit code to jump to FUNCTION, which is a
23683 `FUNCTION_DECL'. This is a direct pure jump, not a call, and does
23684 not touch the return address. Hence returning from FUNCTION will
23685 return to whoever called the current `thunk'.
23687 The effect must be as if FUNCTION had been called directly with the
23688 adjusted first argument. This macro is responsible for emitting
23689 all of the code for a thunk function; output_function_prologue()
23690 and output_function_epilogue() are not invoked.
23692 The THUNK_FNDECL is redundant. (DELTA and FUNCTION have already
23693 been extracted from it.) It might possibly be useful on some
23694 targets, but probably not.
23696 If you do not define this macro, the target-independent code in the
23697 C++ frontend will generate a less efficient heavyweight thunk that
23698 calls FUNCTION instead of jumping to it. The generic approach does
23699 not support varargs. */
23701 static void
23702 rs6000_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
23703 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
23704 tree function)
23706 rtx this_rtx, insn, funexp;
23708 reload_completed = 1;
23709 epilogue_completed = 1;
23711 /* Mark the end of the (empty) prologue. */
23712 emit_note (NOTE_INSN_PROLOGUE_END);
23714 /* Find the "this" pointer. If the function returns a structure,
23715 the structure return pointer is in r3. */
23716 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
23717 this_rtx = gen_rtx_REG (Pmode, 4);
23718 else
23719 this_rtx = gen_rtx_REG (Pmode, 3);
23721 /* Apply the constant offset, if required. */
23722 if (delta)
23723 emit_insn (gen_add3_insn (this_rtx, this_rtx, GEN_INT (delta)));
23725 /* Apply the offset from the vtable, if required. */
23726 if (vcall_offset)
23728 rtx vcall_offset_rtx = GEN_INT (vcall_offset);
23729 rtx tmp = gen_rtx_REG (Pmode, 12);
23731 emit_move_insn (tmp, gen_rtx_MEM (Pmode, this_rtx));
23732 if (((unsigned HOST_WIDE_INT) vcall_offset) + 0x8000 >= 0x10000)
23734 emit_insn (gen_add3_insn (tmp, tmp, vcall_offset_rtx));
23735 emit_move_insn (tmp, gen_rtx_MEM (Pmode, tmp));
23737 else
23739 rtx loc = gen_rtx_PLUS (Pmode, tmp, vcall_offset_rtx);
23741 emit_move_insn (tmp, gen_rtx_MEM (Pmode, loc));
23743 emit_insn (gen_add3_insn (this_rtx, this_rtx, tmp));
23746 /* Generate a tail call to the target function. */
23747 if (!TREE_USED (function))
23749 assemble_external (function);
23750 TREE_USED (function) = 1;
23752 funexp = XEXP (DECL_RTL (function), 0);
23753 funexp = gen_rtx_MEM (FUNCTION_MODE, funexp);
23755 #if TARGET_MACHO
23756 if (MACHOPIC_INDIRECT)
23757 funexp = machopic_indirect_call_target (funexp);
23758 #endif
23760 /* gen_sibcall expects reload to convert scratch pseudo to LR so we must
23761 generate sibcall RTL explicitly. */
23762 insn = emit_call_insn (
23763 gen_rtx_PARALLEL (VOIDmode,
23764 gen_rtvec (4,
23765 gen_rtx_CALL (VOIDmode,
23766 funexp, const0_rtx),
23767 gen_rtx_USE (VOIDmode, const0_rtx),
23768 gen_rtx_USE (VOIDmode,
23769 gen_rtx_REG (SImode,
23770 LR_REGNO)),
23771 simple_return_rtx)));
23772 SIBLING_CALL_P (insn) = 1;
23773 emit_barrier ();
23775 /* Run just enough of rest_of_compilation to get the insns emitted.
23776 There's not really enough bulk here to make other passes such as
23777 instruction scheduling worth while. Note that use_thunk calls
23778 assemble_start_function and assemble_end_function. */
23779 insn = get_insns ();
23780 shorten_branches (insn);
23781 final_start_function (insn, file, 1);
23782 final (insn, file, 1);
23783 final_end_function ();
23785 reload_completed = 0;
23786 epilogue_completed = 0;
23789 /* A quick summary of the various types of 'constant-pool tables'
23790 under PowerPC:
23792 Target Flags Name One table per
23793 AIX (none) AIX TOC object file
23794 AIX -mfull-toc AIX TOC object file
23795 AIX -mminimal-toc AIX minimal TOC translation unit
23796 SVR4/EABI (none) SVR4 SDATA object file
23797 SVR4/EABI -fpic SVR4 pic object file
23798 SVR4/EABI -fPIC SVR4 PIC translation unit
23799 SVR4/EABI -mrelocatable EABI TOC function
23800 SVR4/EABI -maix AIX TOC object file
23801 SVR4/EABI -maix -mminimal-toc
23802 AIX minimal TOC translation unit
23804 Name Reg. Set by entries contains:
23805 made by addrs? fp? sum?
23807 AIX TOC 2 crt0 as Y option option
23808 AIX minimal TOC 30 prolog gcc Y Y option
23809 SVR4 SDATA 13 crt0 gcc N Y N
23810 SVR4 pic 30 prolog ld Y not yet N
23811 SVR4 PIC 30 prolog gcc Y option option
23812 EABI TOC 30 prolog gcc Y option option
23816 /* Hash functions for the hash table. */
23818 static unsigned
23819 rs6000_hash_constant (rtx k)
23821 enum rtx_code code = GET_CODE (k);
23822 enum machine_mode mode = GET_MODE (k);
23823 unsigned result = (code << 3) ^ mode;
23824 const char *format;
23825 int flen, fidx;
23827 format = GET_RTX_FORMAT (code);
23828 flen = strlen (format);
23829 fidx = 0;
23831 switch (code)
23833 case LABEL_REF:
23834 return result * 1231 + (unsigned) INSN_UID (XEXP (k, 0));
23836 case CONST_DOUBLE:
23837 if (mode != VOIDmode)
23838 return real_hash (CONST_DOUBLE_REAL_VALUE (k)) * result;
23839 flen = 2;
23840 break;
23842 case CODE_LABEL:
23843 fidx = 3;
23844 break;
23846 default:
23847 break;
23850 for (; fidx < flen; fidx++)
23851 switch (format[fidx])
23853 case 's':
23855 unsigned i, len;
23856 const char *str = XSTR (k, fidx);
23857 len = strlen (str);
23858 result = result * 613 + len;
23859 for (i = 0; i < len; i++)
23860 result = result * 613 + (unsigned) str[i];
23861 break;
23863 case 'u':
23864 case 'e':
23865 result = result * 1231 + rs6000_hash_constant (XEXP (k, fidx));
23866 break;
23867 case 'i':
23868 case 'n':
23869 result = result * 613 + (unsigned) XINT (k, fidx);
23870 break;
23871 case 'w':
23872 if (sizeof (unsigned) >= sizeof (HOST_WIDE_INT))
23873 result = result * 613 + (unsigned) XWINT (k, fidx);
23874 else
23876 size_t i;
23877 for (i = 0; i < sizeof (HOST_WIDE_INT) / sizeof (unsigned); i++)
23878 result = result * 613 + (unsigned) (XWINT (k, fidx)
23879 >> CHAR_BIT * i);
23881 break;
23882 case '0':
23883 break;
23884 default:
23885 gcc_unreachable ();
23888 return result;
23891 static unsigned
23892 toc_hash_function (const void *hash_entry)
23894 const struct toc_hash_struct *thc =
23895 (const struct toc_hash_struct *) hash_entry;
23896 return rs6000_hash_constant (thc->key) ^ thc->key_mode;
23899 /* Compare H1 and H2 for equivalence. */
23901 static int
23902 toc_hash_eq (const void *h1, const void *h2)
23904 rtx r1 = ((const struct toc_hash_struct *) h1)->key;
23905 rtx r2 = ((const struct toc_hash_struct *) h2)->key;
23907 if (((const struct toc_hash_struct *) h1)->key_mode
23908 != ((const struct toc_hash_struct *) h2)->key_mode)
23909 return 0;
23911 return rtx_equal_p (r1, r2);
23914 /* These are the names given by the C++ front-end to vtables, and
23915 vtable-like objects. Ideally, this logic should not be here;
23916 instead, there should be some programmatic way of inquiring as
23917 to whether or not an object is a vtable. */
23919 #define VTABLE_NAME_P(NAME) \
23920 (strncmp ("_vt.", name, strlen ("_vt.")) == 0 \
23921 || strncmp ("_ZTV", name, strlen ("_ZTV")) == 0 \
23922 || strncmp ("_ZTT", name, strlen ("_ZTT")) == 0 \
23923 || strncmp ("_ZTI", name, strlen ("_ZTI")) == 0 \
23924 || strncmp ("_ZTC", name, strlen ("_ZTC")) == 0)
23926 #ifdef NO_DOLLAR_IN_LABEL
23927 /* Return a GGC-allocated character string translating dollar signs in
23928 input NAME to underscores. Used by XCOFF ASM_OUTPUT_LABELREF. */
23930 const char *
23931 rs6000_xcoff_strip_dollar (const char *name)
23933 char *strip, *p;
23934 const char *q;
23935 size_t len;
23937 q = (const char *) strchr (name, '$');
23939 if (q == 0 || q == name)
23940 return name;
23942 len = strlen (name);
23943 strip = XALLOCAVEC (char, len + 1);
23944 strcpy (strip, name);
23945 p = strip + (q - name);
23946 while (p)
23948 *p = '_';
23949 p = strchr (p + 1, '$');
23952 return ggc_alloc_string (strip, len);
23954 #endif
23956 void
23957 rs6000_output_symbol_ref (FILE *file, rtx x)
23959 /* Currently C++ toc references to vtables can be emitted before it
23960 is decided whether the vtable is public or private. If this is
23961 the case, then the linker will eventually complain that there is
23962 a reference to an unknown section. Thus, for vtables only,
23963 we emit the TOC reference to reference the symbol and not the
23964 section. */
23965 const char *name = XSTR (x, 0);
23967 if (VTABLE_NAME_P (name))
23969 RS6000_OUTPUT_BASENAME (file, name);
23971 else
23972 assemble_name (file, name);
23975 /* Output a TOC entry. We derive the entry name from what is being
23976 written. */
23978 void
23979 output_toc (FILE *file, rtx x, int labelno, enum machine_mode mode)
23981 char buf[256];
23982 const char *name = buf;
23983 rtx base = x;
23984 HOST_WIDE_INT offset = 0;
23986 gcc_assert (!TARGET_NO_TOC);
23988 /* When the linker won't eliminate them, don't output duplicate
23989 TOC entries (this happens on AIX if there is any kind of TOC,
23990 and on SVR4 under -fPIC or -mrelocatable). Don't do this for
23991 CODE_LABELs. */
23992 if (TARGET_TOC && GET_CODE (x) != LABEL_REF)
23994 struct toc_hash_struct *h;
23995 void * * found;
23997 /* Create toc_hash_table. This can't be done at TARGET_OPTION_OVERRIDE
23998 time because GGC is not initialized at that point. */
23999 if (toc_hash_table == NULL)
24000 toc_hash_table = htab_create_ggc (1021, toc_hash_function,
24001 toc_hash_eq, NULL);
24003 h = ggc_alloc_toc_hash_struct ();
24004 h->key = x;
24005 h->key_mode = mode;
24006 h->labelno = labelno;
24008 found = htab_find_slot (toc_hash_table, h, INSERT);
24009 if (*found == NULL)
24010 *found = h;
24011 else /* This is indeed a duplicate.
24012 Set this label equal to that label. */
24014 fputs ("\t.set ", file);
24015 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
24016 fprintf (file, "%d,", labelno);
24017 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
24018 fprintf (file, "%d\n", ((*(const struct toc_hash_struct **)
24019 found)->labelno));
24021 #ifdef HAVE_AS_TLS
24022 if (TARGET_XCOFF && GET_CODE (x) == SYMBOL_REF
24023 && (SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_GLOBAL_DYNAMIC
24024 || SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC))
24026 fputs ("\t.set ", file);
24027 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LCM");
24028 fprintf (file, "%d,", labelno);
24029 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LCM");
24030 fprintf (file, "%d\n", ((*(const struct toc_hash_struct **)
24031 found)->labelno));
24033 #endif
24034 return;
24038 /* If we're going to put a double constant in the TOC, make sure it's
24039 aligned properly when strict alignment is on. */
24040 if (GET_CODE (x) == CONST_DOUBLE
24041 && STRICT_ALIGNMENT
24042 && GET_MODE_BITSIZE (mode) >= 64
24043 && ! (TARGET_NO_FP_IN_TOC && ! TARGET_MINIMAL_TOC)) {
24044 ASM_OUTPUT_ALIGN (file, 3);
24047 (*targetm.asm_out.internal_label) (file, "LC", labelno);
24049 /* Handle FP constants specially. Note that if we have a minimal
24050 TOC, things we put here aren't actually in the TOC, so we can allow
24051 FP constants. */
24052 if (GET_CODE (x) == CONST_DOUBLE &&
24053 (GET_MODE (x) == TFmode || GET_MODE (x) == TDmode))
24055 REAL_VALUE_TYPE rv;
24056 long k[4];
24058 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
24059 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
24060 REAL_VALUE_TO_TARGET_DECIMAL128 (rv, k);
24061 else
24062 REAL_VALUE_TO_TARGET_LONG_DOUBLE (rv, k);
24064 if (TARGET_64BIT)
24066 if (TARGET_ELF || TARGET_MINIMAL_TOC)
24067 fputs (DOUBLE_INT_ASM_OP, file);
24068 else
24069 fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
24070 k[0] & 0xffffffff, k[1] & 0xffffffff,
24071 k[2] & 0xffffffff, k[3] & 0xffffffff);
24072 fprintf (file, "0x%lx%08lx,0x%lx%08lx\n",
24073 k[WORDS_BIG_ENDIAN ? 0 : 1] & 0xffffffff,
24074 k[WORDS_BIG_ENDIAN ? 1 : 0] & 0xffffffff,
24075 k[WORDS_BIG_ENDIAN ? 2 : 3] & 0xffffffff,
24076 k[WORDS_BIG_ENDIAN ? 3 : 2] & 0xffffffff);
24077 return;
24079 else
24081 if (TARGET_ELF || TARGET_MINIMAL_TOC)
24082 fputs ("\t.long ", file);
24083 else
24084 fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
24085 k[0] & 0xffffffff, k[1] & 0xffffffff,
24086 k[2] & 0xffffffff, k[3] & 0xffffffff);
24087 fprintf (file, "0x%lx,0x%lx,0x%lx,0x%lx\n",
24088 k[0] & 0xffffffff, k[1] & 0xffffffff,
24089 k[2] & 0xffffffff, k[3] & 0xffffffff);
24090 return;
24093 else if (GET_CODE (x) == CONST_DOUBLE &&
24094 (GET_MODE (x) == DFmode || GET_MODE (x) == DDmode))
24096 REAL_VALUE_TYPE rv;
24097 long k[2];
24099 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
24101 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
24102 REAL_VALUE_TO_TARGET_DECIMAL64 (rv, k);
24103 else
24104 REAL_VALUE_TO_TARGET_DOUBLE (rv, k);
24106 if (TARGET_64BIT)
24108 if (TARGET_ELF || TARGET_MINIMAL_TOC)
24109 fputs (DOUBLE_INT_ASM_OP, file);
24110 else
24111 fprintf (file, "\t.tc FD_%lx_%lx[TC],",
24112 k[0] & 0xffffffff, k[1] & 0xffffffff);
24113 fprintf (file, "0x%lx%08lx\n",
24114 k[WORDS_BIG_ENDIAN ? 0 : 1] & 0xffffffff,
24115 k[WORDS_BIG_ENDIAN ? 1 : 0] & 0xffffffff);
24116 return;
24118 else
24120 if (TARGET_ELF || TARGET_MINIMAL_TOC)
24121 fputs ("\t.long ", file);
24122 else
24123 fprintf (file, "\t.tc FD_%lx_%lx[TC],",
24124 k[0] & 0xffffffff, k[1] & 0xffffffff);
24125 fprintf (file, "0x%lx,0x%lx\n",
24126 k[0] & 0xffffffff, k[1] & 0xffffffff);
24127 return;
24130 else if (GET_CODE (x) == CONST_DOUBLE &&
24131 (GET_MODE (x) == SFmode || GET_MODE (x) == SDmode))
24133 REAL_VALUE_TYPE rv;
24134 long l;
24136 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
24137 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
24138 REAL_VALUE_TO_TARGET_DECIMAL32 (rv, l);
24139 else
24140 REAL_VALUE_TO_TARGET_SINGLE (rv, l);
24142 if (TARGET_64BIT)
24144 if (TARGET_ELF || TARGET_MINIMAL_TOC)
24145 fputs (DOUBLE_INT_ASM_OP, file);
24146 else
24147 fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
24148 if (WORDS_BIG_ENDIAN)
24149 fprintf (file, "0x%lx00000000\n", l & 0xffffffff);
24150 else
24151 fprintf (file, "0x%lx\n", l & 0xffffffff);
24152 return;
24154 else
24156 if (TARGET_ELF || TARGET_MINIMAL_TOC)
24157 fputs ("\t.long ", file);
24158 else
24159 fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
24160 fprintf (file, "0x%lx\n", l & 0xffffffff);
24161 return;
24164 else if (GET_MODE (x) == VOIDmode && GET_CODE (x) == CONST_INT)
24166 unsigned HOST_WIDE_INT low;
24167 HOST_WIDE_INT high;
24169 low = INTVAL (x) & 0xffffffff;
24170 high = (HOST_WIDE_INT) INTVAL (x) >> 32;
24172 /* TOC entries are always Pmode-sized, so when big-endian
24173 smaller integer constants in the TOC need to be padded.
24174 (This is still a win over putting the constants in
24175 a separate constant pool, because then we'd have
24176 to have both a TOC entry _and_ the actual constant.)
24178 For a 32-bit target, CONST_INT values are loaded and shifted
24179 entirely within `low' and can be stored in one TOC entry. */
24181 /* It would be easy to make this work, but it doesn't now. */
24182 gcc_assert (!TARGET_64BIT || POINTER_SIZE >= GET_MODE_BITSIZE (mode));
24184 if (WORDS_BIG_ENDIAN && POINTER_SIZE > GET_MODE_BITSIZE (mode))
24186 low |= high << 32;
24187 low <<= POINTER_SIZE - GET_MODE_BITSIZE (mode);
24188 high = (HOST_WIDE_INT) low >> 32;
24189 low &= 0xffffffff;
24192 if (TARGET_64BIT)
24194 if (TARGET_ELF || TARGET_MINIMAL_TOC)
24195 fputs (DOUBLE_INT_ASM_OP, file);
24196 else
24197 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
24198 (long) high & 0xffffffff, (long) low & 0xffffffff);
24199 fprintf (file, "0x%lx%08lx\n",
24200 (long) high & 0xffffffff, (long) low & 0xffffffff);
24201 return;
24203 else
24205 if (POINTER_SIZE < GET_MODE_BITSIZE (mode))
24207 if (TARGET_ELF || TARGET_MINIMAL_TOC)
24208 fputs ("\t.long ", file);
24209 else
24210 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
24211 (long) high & 0xffffffff, (long) low & 0xffffffff);
24212 fprintf (file, "0x%lx,0x%lx\n",
24213 (long) high & 0xffffffff, (long) low & 0xffffffff);
24215 else
24217 if (TARGET_ELF || TARGET_MINIMAL_TOC)
24218 fputs ("\t.long ", file);
24219 else
24220 fprintf (file, "\t.tc IS_%lx[TC],", (long) low & 0xffffffff);
24221 fprintf (file, "0x%lx\n", (long) low & 0xffffffff);
24223 return;
24227 if (GET_CODE (x) == CONST)
24229 gcc_assert (GET_CODE (XEXP (x, 0)) == PLUS
24230 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT);
24232 base = XEXP (XEXP (x, 0), 0);
24233 offset = INTVAL (XEXP (XEXP (x, 0), 1));
24236 switch (GET_CODE (base))
24238 case SYMBOL_REF:
24239 name = XSTR (base, 0);
24240 break;
24242 case LABEL_REF:
24243 ASM_GENERATE_INTERNAL_LABEL (buf, "L",
24244 CODE_LABEL_NUMBER (XEXP (base, 0)));
24245 break;
24247 case CODE_LABEL:
24248 ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (base));
24249 break;
24251 default:
24252 gcc_unreachable ();
24255 if (TARGET_ELF || TARGET_MINIMAL_TOC)
24256 fputs (TARGET_32BIT ? "\t.long " : DOUBLE_INT_ASM_OP, file);
24257 else
24259 fputs ("\t.tc ", file);
24260 RS6000_OUTPUT_BASENAME (file, name);
24262 if (offset < 0)
24263 fprintf (file, ".N" HOST_WIDE_INT_PRINT_UNSIGNED, - offset);
24264 else if (offset)
24265 fprintf (file, ".P" HOST_WIDE_INT_PRINT_UNSIGNED, offset);
24267 /* Mark large TOC symbols on AIX with [TE] so they are mapped
24268 after other TOC symbols, reducing overflow of small TOC access
24269 to [TC] symbols. */
24270 fputs (TARGET_XCOFF && TARGET_CMODEL != CMODEL_SMALL
24271 ? "[TE]," : "[TC],", file);
24274 /* Currently C++ toc references to vtables can be emitted before it
24275 is decided whether the vtable is public or private. If this is
24276 the case, then the linker will eventually complain that there is
24277 a TOC reference to an unknown section. Thus, for vtables only,
24278 we emit the TOC reference to reference the symbol and not the
24279 section. */
24280 if (VTABLE_NAME_P (name))
24282 RS6000_OUTPUT_BASENAME (file, name);
24283 if (offset < 0)
24284 fprintf (file, HOST_WIDE_INT_PRINT_DEC, offset);
24285 else if (offset > 0)
24286 fprintf (file, "+" HOST_WIDE_INT_PRINT_DEC, offset);
24288 else
24289 output_addr_const (file, x);
24291 #if HAVE_AS_TLS
24292 if (TARGET_XCOFF && GET_CODE (base) == SYMBOL_REF
24293 && SYMBOL_REF_TLS_MODEL (base) != 0)
24295 if (SYMBOL_REF_TLS_MODEL (base) == TLS_MODEL_LOCAL_EXEC)
24296 fputs ("@le", file);
24297 else if (SYMBOL_REF_TLS_MODEL (base) == TLS_MODEL_INITIAL_EXEC)
24298 fputs ("@ie", file);
24299 /* Use global-dynamic for local-dynamic. */
24300 else if (SYMBOL_REF_TLS_MODEL (base) == TLS_MODEL_GLOBAL_DYNAMIC
24301 || SYMBOL_REF_TLS_MODEL (base) == TLS_MODEL_LOCAL_DYNAMIC)
24303 putc ('\n', file);
24304 (*targetm.asm_out.internal_label) (file, "LCM", labelno);
24305 fputs ("\t.tc .", file);
24306 RS6000_OUTPUT_BASENAME (file, name);
24307 fputs ("[TC],", file);
24308 output_addr_const (file, x);
24309 fputs ("@m", file);
24312 #endif
24314 putc ('\n', file);
24317 /* Output an assembler pseudo-op to write an ASCII string of N characters
24318 starting at P to FILE.
24320 On the RS/6000, we have to do this using the .byte operation and
24321 write out special characters outside the quoted string.
24322 Also, the assembler is broken; very long strings are truncated,
24323 so we must artificially break them up early. */
24325 void
24326 output_ascii (FILE *file, const char *p, int n)
24328 char c;
24329 int i, count_string;
24330 const char *for_string = "\t.byte \"";
24331 const char *for_decimal = "\t.byte ";
24332 const char *to_close = NULL;
24334 count_string = 0;
24335 for (i = 0; i < n; i++)
24337 c = *p++;
24338 if (c >= ' ' && c < 0177)
24340 if (for_string)
24341 fputs (for_string, file);
24342 putc (c, file);
24344 /* Write two quotes to get one. */
24345 if (c == '"')
24347 putc (c, file);
24348 ++count_string;
24351 for_string = NULL;
24352 for_decimal = "\"\n\t.byte ";
24353 to_close = "\"\n";
24354 ++count_string;
24356 if (count_string >= 512)
24358 fputs (to_close, file);
24360 for_string = "\t.byte \"";
24361 for_decimal = "\t.byte ";
24362 to_close = NULL;
24363 count_string = 0;
24366 else
24368 if (for_decimal)
24369 fputs (for_decimal, file);
24370 fprintf (file, "%d", c);
24372 for_string = "\n\t.byte \"";
24373 for_decimal = ", ";
24374 to_close = "\n";
24375 count_string = 0;
24379 /* Now close the string if we have written one. Then end the line. */
24380 if (to_close)
24381 fputs (to_close, file);
24384 /* Generate a unique section name for FILENAME for a section type
24385 represented by SECTION_DESC. Output goes into BUF.
24387 SECTION_DESC can be any string, as long as it is different for each
24388 possible section type.
24390 We name the section in the same manner as xlc. The name begins with an
24391 underscore followed by the filename (after stripping any leading directory
24392 names) with the last period replaced by the string SECTION_DESC. If
24393 FILENAME does not contain a period, SECTION_DESC is appended to the end of
24394 the name. */
24396 void
24397 rs6000_gen_section_name (char **buf, const char *filename,
24398 const char *section_desc)
24400 const char *q, *after_last_slash, *last_period = 0;
24401 char *p;
24402 int len;
24404 after_last_slash = filename;
24405 for (q = filename; *q; q++)
24407 if (*q == '/')
24408 after_last_slash = q + 1;
24409 else if (*q == '.')
24410 last_period = q;
24413 len = strlen (after_last_slash) + strlen (section_desc) + 2;
24414 *buf = (char *) xmalloc (len);
24416 p = *buf;
24417 *p++ = '_';
24419 for (q = after_last_slash; *q; q++)
24421 if (q == last_period)
24423 strcpy (p, section_desc);
24424 p += strlen (section_desc);
24425 break;
24428 else if (ISALNUM (*q))
24429 *p++ = *q;
24432 if (last_period == 0)
24433 strcpy (p, section_desc);
24434 else
24435 *p = '\0';
24438 /* Emit profile function. */
24440 void
24441 output_profile_hook (int labelno ATTRIBUTE_UNUSED)
24443 /* Non-standard profiling for kernels, which just saves LR then calls
24444 _mcount without worrying about arg saves. The idea is to change
24445 the function prologue as little as possible as it isn't easy to
24446 account for arg save/restore code added just for _mcount. */
24447 if (TARGET_PROFILE_KERNEL)
24448 return;
24450 if (DEFAULT_ABI == ABI_AIX)
24452 #ifndef NO_PROFILE_COUNTERS
24453 # define NO_PROFILE_COUNTERS 0
24454 #endif
24455 if (NO_PROFILE_COUNTERS)
24456 emit_library_call (init_one_libfunc (RS6000_MCOUNT),
24457 LCT_NORMAL, VOIDmode, 0);
24458 else
24460 char buf[30];
24461 const char *label_name;
24462 rtx fun;
24464 ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
24465 label_name = ggc_strdup ((*targetm.strip_name_encoding) (buf));
24466 fun = gen_rtx_SYMBOL_REF (Pmode, label_name);
24468 emit_library_call (init_one_libfunc (RS6000_MCOUNT),
24469 LCT_NORMAL, VOIDmode, 1, fun, Pmode);
24472 else if (DEFAULT_ABI == ABI_DARWIN)
24474 const char *mcount_name = RS6000_MCOUNT;
24475 int caller_addr_regno = LR_REGNO;
24477 /* Be conservative and always set this, at least for now. */
24478 crtl->uses_pic_offset_table = 1;
24480 #if TARGET_MACHO
24481 /* For PIC code, set up a stub and collect the caller's address
24482 from r0, which is where the prologue puts it. */
24483 if (MACHOPIC_INDIRECT
24484 && crtl->uses_pic_offset_table)
24485 caller_addr_regno = 0;
24486 #endif
24487 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, mcount_name),
24488 LCT_NORMAL, VOIDmode, 1,
24489 gen_rtx_REG (Pmode, caller_addr_regno), Pmode);
24493 /* Write function profiler code. */
24495 void
24496 output_function_profiler (FILE *file, int labelno)
24498 char buf[100];
24500 switch (DEFAULT_ABI)
24502 default:
24503 gcc_unreachable ();
24505 case ABI_V4:
24506 if (!TARGET_32BIT)
24508 warning (0, "no profiling of 64-bit code for this ABI");
24509 return;
24511 ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
24512 fprintf (file, "\tmflr %s\n", reg_names[0]);
24513 if (NO_PROFILE_COUNTERS)
24515 asm_fprintf (file, "\tstw %s,4(%s)\n",
24516 reg_names[0], reg_names[1]);
24518 else if (TARGET_SECURE_PLT && flag_pic)
24520 if (TARGET_LINK_STACK)
24522 char name[32];
24523 get_ppc476_thunk_name (name);
24524 asm_fprintf (file, "\tbl %s\n", name);
24526 else
24527 asm_fprintf (file, "\tbcl 20,31,1f\n1:\n");
24528 asm_fprintf (file, "\tstw %s,4(%s)\n",
24529 reg_names[0], reg_names[1]);
24530 asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
24531 asm_fprintf (file, "\taddis %s,%s,",
24532 reg_names[12], reg_names[12]);
24533 assemble_name (file, buf);
24534 asm_fprintf (file, "-1b@ha\n\tla %s,", reg_names[0]);
24535 assemble_name (file, buf);
24536 asm_fprintf (file, "-1b@l(%s)\n", reg_names[12]);
24538 else if (flag_pic == 1)
24540 fputs ("\tbl _GLOBAL_OFFSET_TABLE_@local-4\n", file);
24541 asm_fprintf (file, "\tstw %s,4(%s)\n",
24542 reg_names[0], reg_names[1]);
24543 asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
24544 asm_fprintf (file, "\tlwz %s,", reg_names[0]);
24545 assemble_name (file, buf);
24546 asm_fprintf (file, "@got(%s)\n", reg_names[12]);
24548 else if (flag_pic > 1)
24550 asm_fprintf (file, "\tstw %s,4(%s)\n",
24551 reg_names[0], reg_names[1]);
24552 /* Now, we need to get the address of the label. */
24553 if (TARGET_LINK_STACK)
24555 char name[32];
24556 get_ppc476_thunk_name (name);
24557 asm_fprintf (file, "\tbl %s\n\tb 1f\n\t.long ", name);
24558 assemble_name (file, buf);
24559 fputs ("-.\n1:", file);
24560 asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
24561 asm_fprintf (file, "\taddi %s,%s,4\n",
24562 reg_names[11], reg_names[11]);
24564 else
24566 fputs ("\tbcl 20,31,1f\n\t.long ", file);
24567 assemble_name (file, buf);
24568 fputs ("-.\n1:", file);
24569 asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
24571 asm_fprintf (file, "\tlwz %s,0(%s)\n",
24572 reg_names[0], reg_names[11]);
24573 asm_fprintf (file, "\tadd %s,%s,%s\n",
24574 reg_names[0], reg_names[0], reg_names[11]);
24576 else
24578 asm_fprintf (file, "\tlis %s,", reg_names[12]);
24579 assemble_name (file, buf);
24580 fputs ("@ha\n", file);
24581 asm_fprintf (file, "\tstw %s,4(%s)\n",
24582 reg_names[0], reg_names[1]);
24583 asm_fprintf (file, "\tla %s,", reg_names[0]);
24584 assemble_name (file, buf);
24585 asm_fprintf (file, "@l(%s)\n", reg_names[12]);
24588 /* ABI_V4 saves the static chain reg with ASM_OUTPUT_REG_PUSH. */
24589 fprintf (file, "\tbl %s%s\n",
24590 RS6000_MCOUNT, flag_pic ? "@plt" : "");
24591 break;
24593 case ABI_AIX:
24594 case ABI_DARWIN:
24595 if (!TARGET_PROFILE_KERNEL)
24597 /* Don't do anything, done in output_profile_hook (). */
24599 else
24601 gcc_assert (!TARGET_32BIT);
24603 asm_fprintf (file, "\tmflr %s\n", reg_names[0]);
24604 asm_fprintf (file, "\tstd %s,16(%s)\n", reg_names[0], reg_names[1]);
24606 if (cfun->static_chain_decl != NULL)
24608 asm_fprintf (file, "\tstd %s,24(%s)\n",
24609 reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
24610 fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
24611 asm_fprintf (file, "\tld %s,24(%s)\n",
24612 reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
24614 else
24615 fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
24617 break;
24623 /* The following variable value is the last issued insn. */
24625 static rtx last_scheduled_insn;
24627 /* The following variable helps to balance issuing of load and
24628 store instructions */
24630 static int load_store_pendulum;
24632 /* Power4 load update and store update instructions are cracked into a
24633 load or store and an integer insn which are executed in the same cycle.
24634 Branches have their own dispatch slot which does not count against the
24635 GCC issue rate, but it changes the program flow so there are no other
24636 instructions to issue in this cycle. */
24638 static int
24639 rs6000_variable_issue_1 (rtx insn, int more)
24641 last_scheduled_insn = insn;
24642 if (GET_CODE (PATTERN (insn)) == USE
24643 || GET_CODE (PATTERN (insn)) == CLOBBER)
24645 cached_can_issue_more = more;
24646 return cached_can_issue_more;
24649 if (insn_terminates_group_p (insn, current_group))
24651 cached_can_issue_more = 0;
24652 return cached_can_issue_more;
24655 /* If no reservation, but reach here */
24656 if (recog_memoized (insn) < 0)
24657 return more;
24659 if (rs6000_sched_groups)
24661 if (is_microcoded_insn (insn))
24662 cached_can_issue_more = 0;
24663 else if (is_cracked_insn (insn))
24664 cached_can_issue_more = more > 2 ? more - 2 : 0;
24665 else
24666 cached_can_issue_more = more - 1;
24668 return cached_can_issue_more;
24671 if (rs6000_cpu_attr == CPU_CELL && is_nonpipeline_insn (insn))
24672 return 0;
24674 cached_can_issue_more = more - 1;
24675 return cached_can_issue_more;
24678 static int
24679 rs6000_variable_issue (FILE *stream, int verbose, rtx insn, int more)
24681 int r = rs6000_variable_issue_1 (insn, more);
24682 if (verbose)
24683 fprintf (stream, "// rs6000_variable_issue (more = %d) = %d\n", more, r);
24684 return r;
24687 /* Adjust the cost of a scheduling dependency. Return the new cost of
24688 a dependency LINK or INSN on DEP_INSN. COST is the current cost. */
24690 static int
24691 rs6000_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
24693 enum attr_type attr_type;
24695 if (! recog_memoized (insn))
24696 return 0;
24698 switch (REG_NOTE_KIND (link))
24700 case REG_DEP_TRUE:
24702 /* Data dependency; DEP_INSN writes a register that INSN reads
24703 some cycles later. */
24705 /* Separate a load from a narrower, dependent store. */
24706 if (rs6000_sched_groups
24707 && GET_CODE (PATTERN (insn)) == SET
24708 && GET_CODE (PATTERN (dep_insn)) == SET
24709 && GET_CODE (XEXP (PATTERN (insn), 1)) == MEM
24710 && GET_CODE (XEXP (PATTERN (dep_insn), 0)) == MEM
24711 && (GET_MODE_SIZE (GET_MODE (XEXP (PATTERN (insn), 1)))
24712 > GET_MODE_SIZE (GET_MODE (XEXP (PATTERN (dep_insn), 0)))))
24713 return cost + 14;
24715 attr_type = get_attr_type (insn);
24717 switch (attr_type)
24719 case TYPE_JMPREG:
24720 /* Tell the first scheduling pass about the latency between
24721 a mtctr and bctr (and mtlr and br/blr). The first
24722 scheduling pass will not know about this latency since
24723 the mtctr instruction, which has the latency associated
24724 to it, will be generated by reload. */
24725 return 4;
24726 case TYPE_BRANCH:
24727 /* Leave some extra cycles between a compare and its
24728 dependent branch, to inhibit expensive mispredicts. */
24729 if ((rs6000_cpu_attr == CPU_PPC603
24730 || rs6000_cpu_attr == CPU_PPC604
24731 || rs6000_cpu_attr == CPU_PPC604E
24732 || rs6000_cpu_attr == CPU_PPC620
24733 || rs6000_cpu_attr == CPU_PPC630
24734 || rs6000_cpu_attr == CPU_PPC750
24735 || rs6000_cpu_attr == CPU_PPC7400
24736 || rs6000_cpu_attr == CPU_PPC7450
24737 || rs6000_cpu_attr == CPU_PPCE5500
24738 || rs6000_cpu_attr == CPU_PPCE6500
24739 || rs6000_cpu_attr == CPU_POWER4
24740 || rs6000_cpu_attr == CPU_POWER5
24741 || rs6000_cpu_attr == CPU_POWER7
24742 || rs6000_cpu_attr == CPU_POWER8
24743 || rs6000_cpu_attr == CPU_CELL)
24744 && recog_memoized (dep_insn)
24745 && (INSN_CODE (dep_insn) >= 0))
24747 switch (get_attr_type (dep_insn))
24749 case TYPE_CMP:
24750 case TYPE_COMPARE:
24751 case TYPE_DELAYED_COMPARE:
24752 case TYPE_IMUL_COMPARE:
24753 case TYPE_LMUL_COMPARE:
24754 case TYPE_FPCOMPARE:
24755 case TYPE_CR_LOGICAL:
24756 case TYPE_DELAYED_CR:
24757 return cost + 2;
24758 default:
24759 break;
24761 break;
24763 case TYPE_STORE:
24764 case TYPE_STORE_U:
24765 case TYPE_STORE_UX:
24766 case TYPE_FPSTORE:
24767 case TYPE_FPSTORE_U:
24768 case TYPE_FPSTORE_UX:
24769 if ((rs6000_cpu == PROCESSOR_POWER6)
24770 && recog_memoized (dep_insn)
24771 && (INSN_CODE (dep_insn) >= 0))
24774 if (GET_CODE (PATTERN (insn)) != SET)
24775 /* If this happens, we have to extend this to schedule
24776 optimally. Return default for now. */
24777 return cost;
24779 /* Adjust the cost for the case where the value written
24780 by a fixed point operation is used as the address
24781 gen value on a store. */
24782 switch (get_attr_type (dep_insn))
24784 case TYPE_LOAD:
24785 case TYPE_LOAD_U:
24786 case TYPE_LOAD_UX:
24787 case TYPE_CNTLZ:
24789 if (! store_data_bypass_p (dep_insn, insn))
24790 return 4;
24791 break;
24793 case TYPE_LOAD_EXT:
24794 case TYPE_LOAD_EXT_U:
24795 case TYPE_LOAD_EXT_UX:
24796 case TYPE_VAR_SHIFT_ROTATE:
24797 case TYPE_VAR_DELAYED_COMPARE:
24799 if (! store_data_bypass_p (dep_insn, insn))
24800 return 6;
24801 break;
24803 case TYPE_INTEGER:
24804 case TYPE_COMPARE:
24805 case TYPE_FAST_COMPARE:
24806 case TYPE_EXTS:
24807 case TYPE_SHIFT:
24808 case TYPE_INSERT_WORD:
24809 case TYPE_INSERT_DWORD:
24810 case TYPE_FPLOAD_U:
24811 case TYPE_FPLOAD_UX:
24812 case TYPE_STORE_U:
24813 case TYPE_STORE_UX:
24814 case TYPE_FPSTORE_U:
24815 case TYPE_FPSTORE_UX:
24817 if (! store_data_bypass_p (dep_insn, insn))
24818 return 3;
24819 break;
24821 case TYPE_IMUL:
24822 case TYPE_IMUL2:
24823 case TYPE_IMUL3:
24824 case TYPE_LMUL:
24825 case TYPE_IMUL_COMPARE:
24826 case TYPE_LMUL_COMPARE:
24828 if (! store_data_bypass_p (dep_insn, insn))
24829 return 17;
24830 break;
24832 case TYPE_IDIV:
24834 if (! store_data_bypass_p (dep_insn, insn))
24835 return 45;
24836 break;
24838 case TYPE_LDIV:
24840 if (! store_data_bypass_p (dep_insn, insn))
24841 return 57;
24842 break;
24844 default:
24845 break;
24848 break;
24850 case TYPE_LOAD:
24851 case TYPE_LOAD_U:
24852 case TYPE_LOAD_UX:
24853 case TYPE_LOAD_EXT:
24854 case TYPE_LOAD_EXT_U:
24855 case TYPE_LOAD_EXT_UX:
24856 if ((rs6000_cpu == PROCESSOR_POWER6)
24857 && recog_memoized (dep_insn)
24858 && (INSN_CODE (dep_insn) >= 0))
24861 /* Adjust the cost for the case where the value written
24862 by a fixed point instruction is used within the address
24863 gen portion of a subsequent load(u)(x) */
24864 switch (get_attr_type (dep_insn))
24866 case TYPE_LOAD:
24867 case TYPE_LOAD_U:
24868 case TYPE_LOAD_UX:
24869 case TYPE_CNTLZ:
24871 if (set_to_load_agen (dep_insn, insn))
24872 return 4;
24873 break;
24875 case TYPE_LOAD_EXT:
24876 case TYPE_LOAD_EXT_U:
24877 case TYPE_LOAD_EXT_UX:
24878 case TYPE_VAR_SHIFT_ROTATE:
24879 case TYPE_VAR_DELAYED_COMPARE:
24881 if (set_to_load_agen (dep_insn, insn))
24882 return 6;
24883 break;
24885 case TYPE_INTEGER:
24886 case TYPE_COMPARE:
24887 case TYPE_FAST_COMPARE:
24888 case TYPE_EXTS:
24889 case TYPE_SHIFT:
24890 case TYPE_INSERT_WORD:
24891 case TYPE_INSERT_DWORD:
24892 case TYPE_FPLOAD_U:
24893 case TYPE_FPLOAD_UX:
24894 case TYPE_STORE_U:
24895 case TYPE_STORE_UX:
24896 case TYPE_FPSTORE_U:
24897 case TYPE_FPSTORE_UX:
24899 if (set_to_load_agen (dep_insn, insn))
24900 return 3;
24901 break;
24903 case TYPE_IMUL:
24904 case TYPE_IMUL2:
24905 case TYPE_IMUL3:
24906 case TYPE_LMUL:
24907 case TYPE_IMUL_COMPARE:
24908 case TYPE_LMUL_COMPARE:
24910 if (set_to_load_agen (dep_insn, insn))
24911 return 17;
24912 break;
24914 case TYPE_IDIV:
24916 if (set_to_load_agen (dep_insn, insn))
24917 return 45;
24918 break;
24920 case TYPE_LDIV:
24922 if (set_to_load_agen (dep_insn, insn))
24923 return 57;
24924 break;
24926 default:
24927 break;
24930 break;
24932 case TYPE_FPLOAD:
24933 if ((rs6000_cpu == PROCESSOR_POWER6)
24934 && recog_memoized (dep_insn)
24935 && (INSN_CODE (dep_insn) >= 0)
24936 && (get_attr_type (dep_insn) == TYPE_MFFGPR))
24937 return 2;
24939 default:
24940 break;
24943 /* Fall out to return default cost. */
24945 break;
24947 case REG_DEP_OUTPUT:
24948 /* Output dependency; DEP_INSN writes a register that INSN writes some
24949 cycles later. */
24950 if ((rs6000_cpu == PROCESSOR_POWER6)
24951 && recog_memoized (dep_insn)
24952 && (INSN_CODE (dep_insn) >= 0))
24954 attr_type = get_attr_type (insn);
24956 switch (attr_type)
24958 case TYPE_FP:
24959 if (get_attr_type (dep_insn) == TYPE_FP)
24960 return 1;
24961 break;
24962 case TYPE_FPLOAD:
24963 if (get_attr_type (dep_insn) == TYPE_MFFGPR)
24964 return 2;
24965 break;
24966 default:
24967 break;
24970 case REG_DEP_ANTI:
24971 /* Anti dependency; DEP_INSN reads a register that INSN writes some
24972 cycles later. */
24973 return 0;
24975 default:
24976 gcc_unreachable ();
24979 return cost;
24982 /* Debug version of rs6000_adjust_cost. */
24984 static int
24985 rs6000_debug_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
24987 int ret = rs6000_adjust_cost (insn, link, dep_insn, cost);
24989 if (ret != cost)
24991 const char *dep;
24993 switch (REG_NOTE_KIND (link))
24995 default: dep = "unknown depencency"; break;
24996 case REG_DEP_TRUE: dep = "data dependency"; break;
24997 case REG_DEP_OUTPUT: dep = "output dependency"; break;
24998 case REG_DEP_ANTI: dep = "anti depencency"; break;
25001 fprintf (stderr,
25002 "\nrs6000_adjust_cost, final cost = %d, orig cost = %d, "
25003 "%s, insn:\n", ret, cost, dep);
25005 debug_rtx (insn);
25008 return ret;
25011 /* The function returns a true if INSN is microcoded.
25012 Return false otherwise. */
25014 static bool
25015 is_microcoded_insn (rtx insn)
25017 if (!insn || !NONDEBUG_INSN_P (insn)
25018 || GET_CODE (PATTERN (insn)) == USE
25019 || GET_CODE (PATTERN (insn)) == CLOBBER)
25020 return false;
25022 if (rs6000_cpu_attr == CPU_CELL)
25023 return get_attr_cell_micro (insn) == CELL_MICRO_ALWAYS;
25025 if (rs6000_sched_groups
25026 && (rs6000_cpu == PROCESSOR_POWER4 || rs6000_cpu == PROCESSOR_POWER5))
25028 enum attr_type type = get_attr_type (insn);
25029 if (type == TYPE_LOAD_EXT_U
25030 || type == TYPE_LOAD_EXT_UX
25031 || type == TYPE_LOAD_UX
25032 || type == TYPE_STORE_UX
25033 || type == TYPE_MFCR)
25034 return true;
25037 return false;
25040 /* The function returns true if INSN is cracked into 2 instructions
25041 by the processor (and therefore occupies 2 issue slots). */
25043 static bool
25044 is_cracked_insn (rtx insn)
25046 if (!insn || !NONDEBUG_INSN_P (insn)
25047 || GET_CODE (PATTERN (insn)) == USE
25048 || GET_CODE (PATTERN (insn)) == CLOBBER)
25049 return false;
25051 if (rs6000_sched_groups
25052 && (rs6000_cpu == PROCESSOR_POWER4 || rs6000_cpu == PROCESSOR_POWER5))
25054 enum attr_type type = get_attr_type (insn);
25055 if (type == TYPE_LOAD_U || type == TYPE_STORE_U
25056 || type == TYPE_FPLOAD_U || type == TYPE_FPSTORE_U
25057 || type == TYPE_FPLOAD_UX || type == TYPE_FPSTORE_UX
25058 || type == TYPE_LOAD_EXT || type == TYPE_DELAYED_CR
25059 || type == TYPE_COMPARE || type == TYPE_DELAYED_COMPARE
25060 || type == TYPE_IMUL_COMPARE || type == TYPE_LMUL_COMPARE
25061 || type == TYPE_IDIV || type == TYPE_LDIV
25062 || type == TYPE_INSERT_WORD)
25063 return true;
25066 return false;
25069 /* The function returns true if INSN can be issued only from
25070 the branch slot. */
25072 static bool
25073 is_branch_slot_insn (rtx insn)
25075 if (!insn || !NONDEBUG_INSN_P (insn)
25076 || GET_CODE (PATTERN (insn)) == USE
25077 || GET_CODE (PATTERN (insn)) == CLOBBER)
25078 return false;
25080 if (rs6000_sched_groups)
25082 enum attr_type type = get_attr_type (insn);
25083 if (type == TYPE_BRANCH || type == TYPE_JMPREG)
25084 return true;
25085 return false;
25088 return false;
25091 /* The function returns true if out_inst sets a value that is
25092 used in the address generation computation of in_insn */
25093 static bool
25094 set_to_load_agen (rtx out_insn, rtx in_insn)
25096 rtx out_set, in_set;
25098 /* For performance reasons, only handle the simple case where
25099 both loads are a single_set. */
25100 out_set = single_set (out_insn);
25101 if (out_set)
25103 in_set = single_set (in_insn);
25104 if (in_set)
25105 return reg_mentioned_p (SET_DEST (out_set), SET_SRC (in_set));
25108 return false;
25111 /* Try to determine base/offset/size parts of the given MEM.
25112 Return true if successful, false if all the values couldn't
25113 be determined.
25115 This function only looks for REG or REG+CONST address forms.
25116 REG+REG address form will return false. */
25118 static bool
25119 get_memref_parts (rtx mem, rtx *base, HOST_WIDE_INT *offset,
25120 HOST_WIDE_INT *size)
25122 rtx addr_rtx;
25123 if MEM_SIZE_KNOWN_P (mem)
25124 *size = MEM_SIZE (mem);
25125 else
25126 return false;
25128 if (GET_CODE (XEXP (mem, 0)) == PRE_MODIFY)
25129 addr_rtx = XEXP (XEXP (mem, 0), 1);
25130 else
25131 addr_rtx = (XEXP (mem, 0));
25133 if (GET_CODE (addr_rtx) == REG)
25135 *base = addr_rtx;
25136 *offset = 0;
25138 else if (GET_CODE (addr_rtx) == PLUS
25139 && CONST_INT_P (XEXP (addr_rtx, 1)))
25141 *base = XEXP (addr_rtx, 0);
25142 *offset = INTVAL (XEXP (addr_rtx, 1));
25144 else
25145 return false;
25147 return true;
25150 /* The function returns true if the target storage location of
25151 mem1 is adjacent to the target storage location of mem2 */
25152 /* Return 1 if memory locations are adjacent. */
25154 static bool
25155 adjacent_mem_locations (rtx mem1, rtx mem2)
25157 rtx reg1, reg2;
25158 HOST_WIDE_INT off1, size1, off2, size2;
25160 if (get_memref_parts (mem1, &reg1, &off1, &size1)
25161 && get_memref_parts (mem2, &reg2, &off2, &size2))
25162 return ((REGNO (reg1) == REGNO (reg2))
25163 && ((off1 + size1 == off2)
25164 || (off2 + size2 == off1)));
25166 return false;
25169 /* This function returns true if it can be determined that the two MEM
25170 locations overlap by at least 1 byte based on base reg/offset/size. */
25172 static bool
25173 mem_locations_overlap (rtx mem1, rtx mem2)
25175 rtx reg1, reg2;
25176 HOST_WIDE_INT off1, size1, off2, size2;
25178 if (get_memref_parts (mem1, &reg1, &off1, &size1)
25179 && get_memref_parts (mem2, &reg2, &off2, &size2))
25180 return ((REGNO (reg1) == REGNO (reg2))
25181 && (((off1 <= off2) && (off1 + size1 > off2))
25182 || ((off2 <= off1) && (off2 + size2 > off1))));
25184 return false;
25187 /* A C statement (sans semicolon) to update the integer scheduling
25188 priority INSN_PRIORITY (INSN). Increase the priority to execute the
25189 INSN earlier, reduce the priority to execute INSN later. Do not
25190 define this macro if you do not need to adjust the scheduling
25191 priorities of insns. */
25193 static int
25194 rs6000_adjust_priority (rtx insn ATTRIBUTE_UNUSED, int priority)
25196 rtx load_mem, str_mem;
25197 /* On machines (like the 750) which have asymmetric integer units,
25198 where one integer unit can do multiply and divides and the other
25199 can't, reduce the priority of multiply/divide so it is scheduled
25200 before other integer operations. */
25202 #if 0
25203 if (! INSN_P (insn))
25204 return priority;
25206 if (GET_CODE (PATTERN (insn)) == USE)
25207 return priority;
25209 switch (rs6000_cpu_attr) {
25210 case CPU_PPC750:
25211 switch (get_attr_type (insn))
25213 default:
25214 break;
25216 case TYPE_IMUL:
25217 case TYPE_IDIV:
25218 fprintf (stderr, "priority was %#x (%d) before adjustment\n",
25219 priority, priority);
25220 if (priority >= 0 && priority < 0x01000000)
25221 priority >>= 3;
25222 break;
25225 #endif
25227 if (insn_must_be_first_in_group (insn)
25228 && reload_completed
25229 && current_sched_info->sched_max_insns_priority
25230 && rs6000_sched_restricted_insns_priority)
25233 /* Prioritize insns that can be dispatched only in the first
25234 dispatch slot. */
25235 if (rs6000_sched_restricted_insns_priority == 1)
25236 /* Attach highest priority to insn. This means that in
25237 haifa-sched.c:ready_sort(), dispatch-slot restriction considerations
25238 precede 'priority' (critical path) considerations. */
25239 return current_sched_info->sched_max_insns_priority;
25240 else if (rs6000_sched_restricted_insns_priority == 2)
25241 /* Increase priority of insn by a minimal amount. This means that in
25242 haifa-sched.c:ready_sort(), only 'priority' (critical path)
25243 considerations precede dispatch-slot restriction considerations. */
25244 return (priority + 1);
25247 if (rs6000_cpu == PROCESSOR_POWER6
25248 && ((load_store_pendulum == -2 && is_load_insn (insn, &load_mem))
25249 || (load_store_pendulum == 2 && is_store_insn (insn, &str_mem))))
25250 /* Attach highest priority to insn if the scheduler has just issued two
25251 stores and this instruction is a load, or two loads and this instruction
25252 is a store. Power6 wants loads and stores scheduled alternately
25253 when possible */
25254 return current_sched_info->sched_max_insns_priority;
25256 return priority;
25259 /* Return true if the instruction is nonpipelined on the Cell. */
25260 static bool
25261 is_nonpipeline_insn (rtx insn)
25263 enum attr_type type;
25264 if (!insn || !NONDEBUG_INSN_P (insn)
25265 || GET_CODE (PATTERN (insn)) == USE
25266 || GET_CODE (PATTERN (insn)) == CLOBBER)
25267 return false;
25269 type = get_attr_type (insn);
25270 if (type == TYPE_IMUL
25271 || type == TYPE_IMUL2
25272 || type == TYPE_IMUL3
25273 || type == TYPE_LMUL
25274 || type == TYPE_IDIV
25275 || type == TYPE_LDIV
25276 || type == TYPE_SDIV
25277 || type == TYPE_DDIV
25278 || type == TYPE_SSQRT
25279 || type == TYPE_DSQRT
25280 || type == TYPE_MFCR
25281 || type == TYPE_MFCRF
25282 || type == TYPE_MFJMPR)
25284 return true;
25286 return false;
25290 /* Return how many instructions the machine can issue per cycle. */
25292 static int
25293 rs6000_issue_rate (void)
25295 /* Unless scheduling for register pressure, use issue rate of 1 for
25296 first scheduling pass to decrease degradation. */
25297 if (!reload_completed && !flag_sched_pressure)
25298 return 1;
25300 switch (rs6000_cpu_attr) {
25301 case CPU_RS64A:
25302 case CPU_PPC601: /* ? */
25303 case CPU_PPC7450:
25304 return 3;
25305 case CPU_PPC440:
25306 case CPU_PPC603:
25307 case CPU_PPC750:
25308 case CPU_PPC7400:
25309 case CPU_PPC8540:
25310 case CPU_PPC8548:
25311 case CPU_CELL:
25312 case CPU_PPCE300C2:
25313 case CPU_PPCE300C3:
25314 case CPU_PPCE500MC:
25315 case CPU_PPCE500MC64:
25316 case CPU_PPCE5500:
25317 case CPU_PPCE6500:
25318 case CPU_TITAN:
25319 return 2;
25320 case CPU_PPC476:
25321 case CPU_PPC604:
25322 case CPU_PPC604E:
25323 case CPU_PPC620:
25324 case CPU_PPC630:
25325 return 4;
25326 case CPU_POWER4:
25327 case CPU_POWER5:
25328 case CPU_POWER6:
25329 case CPU_POWER7:
25330 return 5;
25331 case CPU_POWER8:
25332 return 7;
25333 default:
25334 return 1;
25338 /* Return how many instructions to look ahead for better insn
25339 scheduling. */
25341 static int
25342 rs6000_use_sched_lookahead (void)
25344 switch (rs6000_cpu_attr)
25346 case CPU_PPC8540:
25347 case CPU_PPC8548:
25348 return 4;
25350 case CPU_CELL:
25351 return (reload_completed ? 8 : 0);
25353 default:
25354 return 0;
25358 /* We are choosing insn from the ready queue. Return nonzero if INSN can be chosen. */
25359 static int
25360 rs6000_use_sched_lookahead_guard (rtx insn)
25362 if (rs6000_cpu_attr != CPU_CELL)
25363 return 1;
25365 if (insn == NULL_RTX || !INSN_P (insn))
25366 abort ();
25368 if (!reload_completed
25369 || is_nonpipeline_insn (insn)
25370 || is_microcoded_insn (insn))
25371 return 0;
25373 return 1;
25376 /* Determine if PAT refers to memory. If so, set MEM_REF to the MEM rtx
25377 and return true. */
25379 static bool
25380 find_mem_ref (rtx pat, rtx *mem_ref)
25382 const char * fmt;
25383 int i, j;
25385 /* stack_tie does not produce any real memory traffic. */
25386 if (tie_operand (pat, VOIDmode))
25387 return false;
25389 if (GET_CODE (pat) == MEM)
25391 *mem_ref = pat;
25392 return true;
25395 /* Recursively process the pattern. */
25396 fmt = GET_RTX_FORMAT (GET_CODE (pat));
25398 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
25400 if (fmt[i] == 'e')
25402 if (find_mem_ref (XEXP (pat, i), mem_ref))
25403 return true;
25405 else if (fmt[i] == 'E')
25406 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
25408 if (find_mem_ref (XVECEXP (pat, i, j), mem_ref))
25409 return true;
25413 return false;
25416 /* Determine if PAT is a PATTERN of a load insn. */
25418 static bool
25419 is_load_insn1 (rtx pat, rtx *load_mem)
25421 if (!pat || pat == NULL_RTX)
25422 return false;
25424 if (GET_CODE (pat) == SET)
25425 return find_mem_ref (SET_SRC (pat), load_mem);
25427 if (GET_CODE (pat) == PARALLEL)
25429 int i;
25431 for (i = 0; i < XVECLEN (pat, 0); i++)
25432 if (is_load_insn1 (XVECEXP (pat, 0, i), load_mem))
25433 return true;
25436 return false;
25439 /* Determine if INSN loads from memory. */
25441 static bool
25442 is_load_insn (rtx insn, rtx *load_mem)
25444 if (!insn || !INSN_P (insn))
25445 return false;
25447 if (CALL_P (insn))
25448 return false;
25450 return is_load_insn1 (PATTERN (insn), load_mem);
25453 /* Determine if PAT is a PATTERN of a store insn. */
25455 static bool
25456 is_store_insn1 (rtx pat, rtx *str_mem)
25458 if (!pat || pat == NULL_RTX)
25459 return false;
25461 if (GET_CODE (pat) == SET)
25462 return find_mem_ref (SET_DEST (pat), str_mem);
25464 if (GET_CODE (pat) == PARALLEL)
25466 int i;
25468 for (i = 0; i < XVECLEN (pat, 0); i++)
25469 if (is_store_insn1 (XVECEXP (pat, 0, i), str_mem))
25470 return true;
25473 return false;
25476 /* Determine if INSN stores to memory. */
25478 static bool
25479 is_store_insn (rtx insn, rtx *str_mem)
25481 if (!insn || !INSN_P (insn))
25482 return false;
25484 return is_store_insn1 (PATTERN (insn), str_mem);
25487 /* Returns whether the dependence between INSN and NEXT is considered
25488 costly by the given target. */
25490 static bool
25491 rs6000_is_costly_dependence (dep_t dep, int cost, int distance)
25493 rtx insn;
25494 rtx next;
25495 rtx load_mem, str_mem;
25497 /* If the flag is not enabled - no dependence is considered costly;
25498 allow all dependent insns in the same group.
25499 This is the most aggressive option. */
25500 if (rs6000_sched_costly_dep == no_dep_costly)
25501 return false;
25503 /* If the flag is set to 1 - a dependence is always considered costly;
25504 do not allow dependent instructions in the same group.
25505 This is the most conservative option. */
25506 if (rs6000_sched_costly_dep == all_deps_costly)
25507 return true;
25509 insn = DEP_PRO (dep);
25510 next = DEP_CON (dep);
25512 if (rs6000_sched_costly_dep == store_to_load_dep_costly
25513 && is_load_insn (next, &load_mem)
25514 && is_store_insn (insn, &str_mem))
25515 /* Prevent load after store in the same group. */
25516 return true;
25518 if (rs6000_sched_costly_dep == true_store_to_load_dep_costly
25519 && is_load_insn (next, &load_mem)
25520 && is_store_insn (insn, &str_mem)
25521 && DEP_TYPE (dep) == REG_DEP_TRUE
25522 && mem_locations_overlap(str_mem, load_mem))
25523 /* Prevent load after store in the same group if it is a true
25524 dependence. */
25525 return true;
25527 /* The flag is set to X; dependences with latency >= X are considered costly,
25528 and will not be scheduled in the same group. */
25529 if (rs6000_sched_costly_dep <= max_dep_latency
25530 && ((cost - distance) >= (int)rs6000_sched_costly_dep))
25531 return true;
25533 return false;
25536 /* Return the next insn after INSN that is found before TAIL is reached,
25537 skipping any "non-active" insns - insns that will not actually occupy
25538 an issue slot. Return NULL_RTX if such an insn is not found. */
25540 static rtx
25541 get_next_active_insn (rtx insn, rtx tail)
25543 if (insn == NULL_RTX || insn == tail)
25544 return NULL_RTX;
25546 while (1)
25548 insn = NEXT_INSN (insn);
25549 if (insn == NULL_RTX || insn == tail)
25550 return NULL_RTX;
25552 if (CALL_P (insn)
25553 || JUMP_P (insn) || JUMP_TABLE_DATA_P (insn)
25554 || (NONJUMP_INSN_P (insn)
25555 && GET_CODE (PATTERN (insn)) != USE
25556 && GET_CODE (PATTERN (insn)) != CLOBBER
25557 && INSN_CODE (insn) != CODE_FOR_stack_tie))
25558 break;
25560 return insn;
25563 /* We are about to begin issuing insns for this clock cycle. */
25565 static int
25566 rs6000_sched_reorder (FILE *dump ATTRIBUTE_UNUSED, int sched_verbose,
25567 rtx *ready ATTRIBUTE_UNUSED,
25568 int *pn_ready ATTRIBUTE_UNUSED,
25569 int clock_var ATTRIBUTE_UNUSED)
25571 int n_ready = *pn_ready;
25573 if (sched_verbose)
25574 fprintf (dump, "// rs6000_sched_reorder :\n");
25576 /* Reorder the ready list, if the second to last ready insn
25577 is a nonepipeline insn. */
25578 if (rs6000_cpu_attr == CPU_CELL && n_ready > 1)
25580 if (is_nonpipeline_insn (ready[n_ready - 1])
25581 && (recog_memoized (ready[n_ready - 2]) > 0))
25582 /* Simply swap first two insns. */
25584 rtx tmp = ready[n_ready - 1];
25585 ready[n_ready - 1] = ready[n_ready - 2];
25586 ready[n_ready - 2] = tmp;
25590 if (rs6000_cpu == PROCESSOR_POWER6)
25591 load_store_pendulum = 0;
25593 return rs6000_issue_rate ();
25596 /* Like rs6000_sched_reorder, but called after issuing each insn. */
25598 static int
25599 rs6000_sched_reorder2 (FILE *dump, int sched_verbose, rtx *ready,
25600 int *pn_ready, int clock_var ATTRIBUTE_UNUSED)
25602 if (sched_verbose)
25603 fprintf (dump, "// rs6000_sched_reorder2 :\n");
25605 /* For Power6, we need to handle some special cases to try and keep the
25606 store queue from overflowing and triggering expensive flushes.
25608 This code monitors how load and store instructions are being issued
25609 and skews the ready list one way or the other to increase the likelihood
25610 that a desired instruction is issued at the proper time.
25612 A couple of things are done. First, we maintain a "load_store_pendulum"
25613 to track the current state of load/store issue.
25615 - If the pendulum is at zero, then no loads or stores have been
25616 issued in the current cycle so we do nothing.
25618 - If the pendulum is 1, then a single load has been issued in this
25619 cycle and we attempt to locate another load in the ready list to
25620 issue with it.
25622 - If the pendulum is -2, then two stores have already been
25623 issued in this cycle, so we increase the priority of the first load
25624 in the ready list to increase it's likelihood of being chosen first
25625 in the next cycle.
25627 - If the pendulum is -1, then a single store has been issued in this
25628 cycle and we attempt to locate another store in the ready list to
25629 issue with it, preferring a store to an adjacent memory location to
25630 facilitate store pairing in the store queue.
25632 - If the pendulum is 2, then two loads have already been
25633 issued in this cycle, so we increase the priority of the first store
25634 in the ready list to increase it's likelihood of being chosen first
25635 in the next cycle.
25637 - If the pendulum < -2 or > 2, then do nothing.
25639 Note: This code covers the most common scenarios. There exist non
25640 load/store instructions which make use of the LSU and which
25641 would need to be accounted for to strictly model the behavior
25642 of the machine. Those instructions are currently unaccounted
25643 for to help minimize compile time overhead of this code.
25645 if (rs6000_cpu == PROCESSOR_POWER6 && last_scheduled_insn)
25647 int pos;
25648 int i;
25649 rtx tmp, load_mem, str_mem;
25651 if (is_store_insn (last_scheduled_insn, &str_mem))
25652 /* Issuing a store, swing the load_store_pendulum to the left */
25653 load_store_pendulum--;
25654 else if (is_load_insn (last_scheduled_insn, &load_mem))
25655 /* Issuing a load, swing the load_store_pendulum to the right */
25656 load_store_pendulum++;
25657 else
25658 return cached_can_issue_more;
25660 /* If the pendulum is balanced, or there is only one instruction on
25661 the ready list, then all is well, so return. */
25662 if ((load_store_pendulum == 0) || (*pn_ready <= 1))
25663 return cached_can_issue_more;
25665 if (load_store_pendulum == 1)
25667 /* A load has been issued in this cycle. Scan the ready list
25668 for another load to issue with it */
25669 pos = *pn_ready-1;
25671 while (pos >= 0)
25673 if (is_load_insn (ready[pos], &load_mem))
25675 /* Found a load. Move it to the head of the ready list,
25676 and adjust it's priority so that it is more likely to
25677 stay there */
25678 tmp = ready[pos];
25679 for (i=pos; i<*pn_ready-1; i++)
25680 ready[i] = ready[i + 1];
25681 ready[*pn_ready-1] = tmp;
25683 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
25684 INSN_PRIORITY (tmp)++;
25685 break;
25687 pos--;
25690 else if (load_store_pendulum == -2)
25692 /* Two stores have been issued in this cycle. Increase the
25693 priority of the first load in the ready list to favor it for
25694 issuing in the next cycle. */
25695 pos = *pn_ready-1;
25697 while (pos >= 0)
25699 if (is_load_insn (ready[pos], &load_mem)
25700 && !sel_sched_p ()
25701 && INSN_PRIORITY_KNOWN (ready[pos]))
25703 INSN_PRIORITY (ready[pos])++;
25705 /* Adjust the pendulum to account for the fact that a load
25706 was found and increased in priority. This is to prevent
25707 increasing the priority of multiple loads */
25708 load_store_pendulum--;
25710 break;
25712 pos--;
25715 else if (load_store_pendulum == -1)
25717 /* A store has been issued in this cycle. Scan the ready list for
25718 another store to issue with it, preferring a store to an adjacent
25719 memory location */
25720 int first_store_pos = -1;
25722 pos = *pn_ready-1;
25724 while (pos >= 0)
25726 if (is_store_insn (ready[pos], &str_mem))
25728 rtx str_mem2;
25729 /* Maintain the index of the first store found on the
25730 list */
25731 if (first_store_pos == -1)
25732 first_store_pos = pos;
25734 if (is_store_insn (last_scheduled_insn, &str_mem2)
25735 && adjacent_mem_locations (str_mem, str_mem2))
25737 /* Found an adjacent store. Move it to the head of the
25738 ready list, and adjust it's priority so that it is
25739 more likely to stay there */
25740 tmp = ready[pos];
25741 for (i=pos; i<*pn_ready-1; i++)
25742 ready[i] = ready[i + 1];
25743 ready[*pn_ready-1] = tmp;
25745 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
25746 INSN_PRIORITY (tmp)++;
25748 first_store_pos = -1;
25750 break;
25753 pos--;
25756 if (first_store_pos >= 0)
25758 /* An adjacent store wasn't found, but a non-adjacent store was,
25759 so move the non-adjacent store to the front of the ready
25760 list, and adjust its priority so that it is more likely to
25761 stay there. */
25762 tmp = ready[first_store_pos];
25763 for (i=first_store_pos; i<*pn_ready-1; i++)
25764 ready[i] = ready[i + 1];
25765 ready[*pn_ready-1] = tmp;
25766 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
25767 INSN_PRIORITY (tmp)++;
25770 else if (load_store_pendulum == 2)
25772 /* Two loads have been issued in this cycle. Increase the priority
25773 of the first store in the ready list to favor it for issuing in
25774 the next cycle. */
25775 pos = *pn_ready-1;
25777 while (pos >= 0)
25779 if (is_store_insn (ready[pos], &str_mem)
25780 && !sel_sched_p ()
25781 && INSN_PRIORITY_KNOWN (ready[pos]))
25783 INSN_PRIORITY (ready[pos])++;
25785 /* Adjust the pendulum to account for the fact that a store
25786 was found and increased in priority. This is to prevent
25787 increasing the priority of multiple stores */
25788 load_store_pendulum++;
25790 break;
25792 pos--;
25797 return cached_can_issue_more;
25800 /* Return whether the presence of INSN causes a dispatch group termination
25801 of group WHICH_GROUP.
25803 If WHICH_GROUP == current_group, this function will return true if INSN
25804 causes the termination of the current group (i.e, the dispatch group to
25805 which INSN belongs). This means that INSN will be the last insn in the
25806 group it belongs to.
25808 If WHICH_GROUP == previous_group, this function will return true if INSN
25809 causes the termination of the previous group (i.e, the dispatch group that
25810 precedes the group to which INSN belongs). This means that INSN will be
25811 the first insn in the group it belongs to). */
25813 static bool
25814 insn_terminates_group_p (rtx insn, enum group_termination which_group)
25816 bool first, last;
25818 if (! insn)
25819 return false;
25821 first = insn_must_be_first_in_group (insn);
25822 last = insn_must_be_last_in_group (insn);
25824 if (first && last)
25825 return true;
25827 if (which_group == current_group)
25828 return last;
25829 else if (which_group == previous_group)
25830 return first;
25832 return false;
25836 static bool
25837 insn_must_be_first_in_group (rtx insn)
25839 enum attr_type type;
25841 if (!insn
25842 || NOTE_P (insn)
25843 || DEBUG_INSN_P (insn)
25844 || GET_CODE (PATTERN (insn)) == USE
25845 || GET_CODE (PATTERN (insn)) == CLOBBER)
25846 return false;
25848 switch (rs6000_cpu)
25850 case PROCESSOR_POWER5:
25851 if (is_cracked_insn (insn))
25852 return true;
25853 case PROCESSOR_POWER4:
25854 if (is_microcoded_insn (insn))
25855 return true;
25857 if (!rs6000_sched_groups)
25858 return false;
25860 type = get_attr_type (insn);
25862 switch (type)
25864 case TYPE_MFCR:
25865 case TYPE_MFCRF:
25866 case TYPE_MTCR:
25867 case TYPE_DELAYED_CR:
25868 case TYPE_CR_LOGICAL:
25869 case TYPE_MTJMPR:
25870 case TYPE_MFJMPR:
25871 case TYPE_IDIV:
25872 case TYPE_LDIV:
25873 case TYPE_LOAD_L:
25874 case TYPE_STORE_C:
25875 case TYPE_ISYNC:
25876 case TYPE_SYNC:
25877 return true;
25878 default:
25879 break;
25881 break;
25882 case PROCESSOR_POWER6:
25883 type = get_attr_type (insn);
25885 switch (type)
25887 case TYPE_INSERT_DWORD:
25888 case TYPE_EXTS:
25889 case TYPE_CNTLZ:
25890 case TYPE_SHIFT:
25891 case TYPE_VAR_SHIFT_ROTATE:
25892 case TYPE_TRAP:
25893 case TYPE_IMUL:
25894 case TYPE_IMUL2:
25895 case TYPE_IMUL3:
25896 case TYPE_LMUL:
25897 case TYPE_IDIV:
25898 case TYPE_INSERT_WORD:
25899 case TYPE_DELAYED_COMPARE:
25900 case TYPE_IMUL_COMPARE:
25901 case TYPE_LMUL_COMPARE:
25902 case TYPE_FPCOMPARE:
25903 case TYPE_MFCR:
25904 case TYPE_MTCR:
25905 case TYPE_MFJMPR:
25906 case TYPE_MTJMPR:
25907 case TYPE_ISYNC:
25908 case TYPE_SYNC:
25909 case TYPE_LOAD_L:
25910 case TYPE_STORE_C:
25911 case TYPE_LOAD_U:
25912 case TYPE_LOAD_UX:
25913 case TYPE_LOAD_EXT_UX:
25914 case TYPE_STORE_U:
25915 case TYPE_STORE_UX:
25916 case TYPE_FPLOAD_U:
25917 case TYPE_FPLOAD_UX:
25918 case TYPE_FPSTORE_U:
25919 case TYPE_FPSTORE_UX:
25920 return true;
25921 default:
25922 break;
25924 break;
25925 case PROCESSOR_POWER7:
25926 type = get_attr_type (insn);
25928 switch (type)
25930 case TYPE_CR_LOGICAL:
25931 case TYPE_MFCR:
25932 case TYPE_MFCRF:
25933 case TYPE_MTCR:
25934 case TYPE_IDIV:
25935 case TYPE_LDIV:
25936 case TYPE_COMPARE:
25937 case TYPE_DELAYED_COMPARE:
25938 case TYPE_VAR_DELAYED_COMPARE:
25939 case TYPE_ISYNC:
25940 case TYPE_LOAD_L:
25941 case TYPE_STORE_C:
25942 case TYPE_LOAD_U:
25943 case TYPE_LOAD_UX:
25944 case TYPE_LOAD_EXT:
25945 case TYPE_LOAD_EXT_U:
25946 case TYPE_LOAD_EXT_UX:
25947 case TYPE_STORE_U:
25948 case TYPE_STORE_UX:
25949 case TYPE_FPLOAD_U:
25950 case TYPE_FPLOAD_UX:
25951 case TYPE_FPSTORE_U:
25952 case TYPE_FPSTORE_UX:
25953 case TYPE_MFJMPR:
25954 case TYPE_MTJMPR:
25955 return true;
25956 default:
25957 break;
25959 break;
25960 case PROCESSOR_POWER8:
25961 type = get_attr_type (insn);
25963 switch (type)
25965 case TYPE_CR_LOGICAL:
25966 case TYPE_DELAYED_CR:
25967 case TYPE_MFCR:
25968 case TYPE_MFCRF:
25969 case TYPE_MTCR:
25970 case TYPE_COMPARE:
25971 case TYPE_DELAYED_COMPARE:
25972 case TYPE_VAR_DELAYED_COMPARE:
25973 case TYPE_IMUL_COMPARE:
25974 case TYPE_LMUL_COMPARE:
25975 case TYPE_SYNC:
25976 case TYPE_ISYNC:
25977 case TYPE_LOAD_L:
25978 case TYPE_STORE_C:
25979 case TYPE_LOAD_U:
25980 case TYPE_LOAD_UX:
25981 case TYPE_LOAD_EXT:
25982 case TYPE_LOAD_EXT_U:
25983 case TYPE_LOAD_EXT_UX:
25984 case TYPE_STORE_UX:
25985 case TYPE_VECSTORE:
25986 case TYPE_MFJMPR:
25987 case TYPE_MTJMPR:
25988 return true;
25989 default:
25990 break;
25992 break;
25993 default:
25994 break;
25997 return false;
26000 static bool
26001 insn_must_be_last_in_group (rtx insn)
26003 enum attr_type type;
26005 if (!insn
26006 || NOTE_P (insn)
26007 || DEBUG_INSN_P (insn)
26008 || GET_CODE (PATTERN (insn)) == USE
26009 || GET_CODE (PATTERN (insn)) == CLOBBER)
26010 return false;
26012 switch (rs6000_cpu) {
26013 case PROCESSOR_POWER4:
26014 case PROCESSOR_POWER5:
26015 if (is_microcoded_insn (insn))
26016 return true;
26018 if (is_branch_slot_insn (insn))
26019 return true;
26021 break;
26022 case PROCESSOR_POWER6:
26023 type = get_attr_type (insn);
26025 switch (type)
26027 case TYPE_EXTS:
26028 case TYPE_CNTLZ:
26029 case TYPE_SHIFT:
26030 case TYPE_VAR_SHIFT_ROTATE:
26031 case TYPE_TRAP:
26032 case TYPE_IMUL:
26033 case TYPE_IMUL2:
26034 case TYPE_IMUL3:
26035 case TYPE_LMUL:
26036 case TYPE_IDIV:
26037 case TYPE_DELAYED_COMPARE:
26038 case TYPE_IMUL_COMPARE:
26039 case TYPE_LMUL_COMPARE:
26040 case TYPE_FPCOMPARE:
26041 case TYPE_MFCR:
26042 case TYPE_MTCR:
26043 case TYPE_MFJMPR:
26044 case TYPE_MTJMPR:
26045 case TYPE_ISYNC:
26046 case TYPE_SYNC:
26047 case TYPE_LOAD_L:
26048 case TYPE_STORE_C:
26049 return true;
26050 default:
26051 break;
26053 break;
26054 case PROCESSOR_POWER7:
26055 type = get_attr_type (insn);
26057 switch (type)
26059 case TYPE_ISYNC:
26060 case TYPE_SYNC:
26061 case TYPE_LOAD_L:
26062 case TYPE_STORE_C:
26063 case TYPE_LOAD_EXT_U:
26064 case TYPE_LOAD_EXT_UX:
26065 case TYPE_STORE_UX:
26066 return true;
26067 default:
26068 break;
26070 break;
26071 case PROCESSOR_POWER8:
26072 type = get_attr_type (insn);
26074 switch (type)
26076 case TYPE_MFCR:
26077 case TYPE_MTCR:
26078 case TYPE_ISYNC:
26079 case TYPE_SYNC:
26080 case TYPE_LOAD_L:
26081 case TYPE_STORE_C:
26082 case TYPE_LOAD_EXT_U:
26083 case TYPE_LOAD_EXT_UX:
26084 case TYPE_STORE_UX:
26085 return true;
26086 default:
26087 break;
26089 break;
26090 default:
26091 break;
26094 return false;
26097 /* Return true if it is recommended to keep NEXT_INSN "far" (in a separate
26098 dispatch group) from the insns in GROUP_INSNS. Return false otherwise. */
26100 static bool
26101 is_costly_group (rtx *group_insns, rtx next_insn)
26103 int i;
26104 int issue_rate = rs6000_issue_rate ();
26106 for (i = 0; i < issue_rate; i++)
26108 sd_iterator_def sd_it;
26109 dep_t dep;
26110 rtx insn = group_insns[i];
26112 if (!insn)
26113 continue;
26115 FOR_EACH_DEP (insn, SD_LIST_RES_FORW, sd_it, dep)
26117 rtx next = DEP_CON (dep);
26119 if (next == next_insn
26120 && rs6000_is_costly_dependence (dep, dep_cost (dep), 0))
26121 return true;
26125 return false;
26128 /* Utility of the function redefine_groups.
26129 Check if it is too costly to schedule NEXT_INSN together with GROUP_INSNS
26130 in the same dispatch group. If so, insert nops before NEXT_INSN, in order
26131 to keep it "far" (in a separate group) from GROUP_INSNS, following
26132 one of the following schemes, depending on the value of the flag
26133 -minsert_sched_nops = X:
26134 (1) X == sched_finish_regroup_exact: insert exactly as many nops as needed
26135 in order to force NEXT_INSN into a separate group.
26136 (2) X < sched_finish_regroup_exact: insert exactly X nops.
26137 GROUP_END, CAN_ISSUE_MORE and GROUP_COUNT record the state after nop
26138 insertion (has a group just ended, how many vacant issue slots remain in the
26139 last group, and how many dispatch groups were encountered so far). */
26141 static int
26142 force_new_group (int sched_verbose, FILE *dump, rtx *group_insns,
26143 rtx next_insn, bool *group_end, int can_issue_more,
26144 int *group_count)
26146 rtx nop;
26147 bool force;
26148 int issue_rate = rs6000_issue_rate ();
26149 bool end = *group_end;
26150 int i;
26152 if (next_insn == NULL_RTX || DEBUG_INSN_P (next_insn))
26153 return can_issue_more;
26155 if (rs6000_sched_insert_nops > sched_finish_regroup_exact)
26156 return can_issue_more;
26158 force = is_costly_group (group_insns, next_insn);
26159 if (!force)
26160 return can_issue_more;
26162 if (sched_verbose > 6)
26163 fprintf (dump,"force: group count = %d, can_issue_more = %d\n",
26164 *group_count ,can_issue_more);
26166 if (rs6000_sched_insert_nops == sched_finish_regroup_exact)
26168 if (*group_end)
26169 can_issue_more = 0;
26171 /* Since only a branch can be issued in the last issue_slot, it is
26172 sufficient to insert 'can_issue_more - 1' nops if next_insn is not
26173 a branch. If next_insn is a branch, we insert 'can_issue_more' nops;
26174 in this case the last nop will start a new group and the branch
26175 will be forced to the new group. */
26176 if (can_issue_more && !is_branch_slot_insn (next_insn))
26177 can_issue_more--;
26179 /* Do we have a special group ending nop? */
26180 if (rs6000_cpu_attr == CPU_POWER6 || rs6000_cpu_attr == CPU_POWER7
26181 || rs6000_cpu_attr == CPU_POWER8)
26183 nop = gen_group_ending_nop ();
26184 emit_insn_before (nop, next_insn);
26185 can_issue_more = 0;
26187 else
26188 while (can_issue_more > 0)
26190 nop = gen_nop ();
26191 emit_insn_before (nop, next_insn);
26192 can_issue_more--;
26195 *group_end = true;
26196 return 0;
26199 if (rs6000_sched_insert_nops < sched_finish_regroup_exact)
26201 int n_nops = rs6000_sched_insert_nops;
26203 /* Nops can't be issued from the branch slot, so the effective
26204 issue_rate for nops is 'issue_rate - 1'. */
26205 if (can_issue_more == 0)
26206 can_issue_more = issue_rate;
26207 can_issue_more--;
26208 if (can_issue_more == 0)
26210 can_issue_more = issue_rate - 1;
26211 (*group_count)++;
26212 end = true;
26213 for (i = 0; i < issue_rate; i++)
26215 group_insns[i] = 0;
26219 while (n_nops > 0)
26221 nop = gen_nop ();
26222 emit_insn_before (nop, next_insn);
26223 if (can_issue_more == issue_rate - 1) /* new group begins */
26224 end = false;
26225 can_issue_more--;
26226 if (can_issue_more == 0)
26228 can_issue_more = issue_rate - 1;
26229 (*group_count)++;
26230 end = true;
26231 for (i = 0; i < issue_rate; i++)
26233 group_insns[i] = 0;
26236 n_nops--;
26239 /* Scale back relative to 'issue_rate' (instead of 'issue_rate - 1'). */
26240 can_issue_more++;
26242 /* Is next_insn going to start a new group? */
26243 *group_end
26244 = (end
26245 || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
26246 || (can_issue_more <= 2 && is_cracked_insn (next_insn))
26247 || (can_issue_more < issue_rate &&
26248 insn_terminates_group_p (next_insn, previous_group)));
26249 if (*group_end && end)
26250 (*group_count)--;
26252 if (sched_verbose > 6)
26253 fprintf (dump, "done force: group count = %d, can_issue_more = %d\n",
26254 *group_count, can_issue_more);
26255 return can_issue_more;
26258 return can_issue_more;
26261 /* This function tries to synch the dispatch groups that the compiler "sees"
26262 with the dispatch groups that the processor dispatcher is expected to
26263 form in practice. It tries to achieve this synchronization by forcing the
26264 estimated processor grouping on the compiler (as opposed to the function
26265 'pad_goups' which tries to force the scheduler's grouping on the processor).
26267 The function scans the insn sequence between PREV_HEAD_INSN and TAIL and
26268 examines the (estimated) dispatch groups that will be formed by the processor
26269 dispatcher. It marks these group boundaries to reflect the estimated
26270 processor grouping, overriding the grouping that the scheduler had marked.
26271 Depending on the value of the flag '-minsert-sched-nops' this function can
26272 force certain insns into separate groups or force a certain distance between
26273 them by inserting nops, for example, if there exists a "costly dependence"
26274 between the insns.
26276 The function estimates the group boundaries that the processor will form as
26277 follows: It keeps track of how many vacant issue slots are available after
26278 each insn. A subsequent insn will start a new group if one of the following
26279 4 cases applies:
26280 - no more vacant issue slots remain in the current dispatch group.
26281 - only the last issue slot, which is the branch slot, is vacant, but the next
26282 insn is not a branch.
26283 - only the last 2 or less issue slots, including the branch slot, are vacant,
26284 which means that a cracked insn (which occupies two issue slots) can't be
26285 issued in this group.
26286 - less than 'issue_rate' slots are vacant, and the next insn always needs to
26287 start a new group. */
26289 static int
26290 redefine_groups (FILE *dump, int sched_verbose, rtx prev_head_insn, rtx tail)
26292 rtx insn, next_insn;
26293 int issue_rate;
26294 int can_issue_more;
26295 int slot, i;
26296 bool group_end;
26297 int group_count = 0;
26298 rtx *group_insns;
26300 /* Initialize. */
26301 issue_rate = rs6000_issue_rate ();
26302 group_insns = XALLOCAVEC (rtx, issue_rate);
26303 for (i = 0; i < issue_rate; i++)
26305 group_insns[i] = 0;
26307 can_issue_more = issue_rate;
26308 slot = 0;
26309 insn = get_next_active_insn (prev_head_insn, tail);
26310 group_end = false;
26312 while (insn != NULL_RTX)
26314 slot = (issue_rate - can_issue_more);
26315 group_insns[slot] = insn;
26316 can_issue_more =
26317 rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
26318 if (insn_terminates_group_p (insn, current_group))
26319 can_issue_more = 0;
26321 next_insn = get_next_active_insn (insn, tail);
26322 if (next_insn == NULL_RTX)
26323 return group_count + 1;
26325 /* Is next_insn going to start a new group? */
26326 group_end
26327 = (can_issue_more == 0
26328 || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
26329 || (can_issue_more <= 2 && is_cracked_insn (next_insn))
26330 || (can_issue_more < issue_rate &&
26331 insn_terminates_group_p (next_insn, previous_group)));
26333 can_issue_more = force_new_group (sched_verbose, dump, group_insns,
26334 next_insn, &group_end, can_issue_more,
26335 &group_count);
26337 if (group_end)
26339 group_count++;
26340 can_issue_more = 0;
26341 for (i = 0; i < issue_rate; i++)
26343 group_insns[i] = 0;
26347 if (GET_MODE (next_insn) == TImode && can_issue_more)
26348 PUT_MODE (next_insn, VOIDmode);
26349 else if (!can_issue_more && GET_MODE (next_insn) != TImode)
26350 PUT_MODE (next_insn, TImode);
26352 insn = next_insn;
26353 if (can_issue_more == 0)
26354 can_issue_more = issue_rate;
26355 } /* while */
26357 return group_count;
26360 /* Scan the insn sequence between PREV_HEAD_INSN and TAIL and examine the
26361 dispatch group boundaries that the scheduler had marked. Pad with nops
26362 any dispatch groups which have vacant issue slots, in order to force the
26363 scheduler's grouping on the processor dispatcher. The function
26364 returns the number of dispatch groups found. */
26366 static int
26367 pad_groups (FILE *dump, int sched_verbose, rtx prev_head_insn, rtx tail)
26369 rtx insn, next_insn;
26370 rtx nop;
26371 int issue_rate;
26372 int can_issue_more;
26373 int group_end;
26374 int group_count = 0;
26376 /* Initialize issue_rate. */
26377 issue_rate = rs6000_issue_rate ();
26378 can_issue_more = issue_rate;
26380 insn = get_next_active_insn (prev_head_insn, tail);
26381 next_insn = get_next_active_insn (insn, tail);
26383 while (insn != NULL_RTX)
26385 can_issue_more =
26386 rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
26388 group_end = (next_insn == NULL_RTX || GET_MODE (next_insn) == TImode);
26390 if (next_insn == NULL_RTX)
26391 break;
26393 if (group_end)
26395 /* If the scheduler had marked group termination at this location
26396 (between insn and next_insn), and neither insn nor next_insn will
26397 force group termination, pad the group with nops to force group
26398 termination. */
26399 if (can_issue_more
26400 && (rs6000_sched_insert_nops == sched_finish_pad_groups)
26401 && !insn_terminates_group_p (insn, current_group)
26402 && !insn_terminates_group_p (next_insn, previous_group))
26404 if (!is_branch_slot_insn (next_insn))
26405 can_issue_more--;
26407 while (can_issue_more)
26409 nop = gen_nop ();
26410 emit_insn_before (nop, next_insn);
26411 can_issue_more--;
26415 can_issue_more = issue_rate;
26416 group_count++;
26419 insn = next_insn;
26420 next_insn = get_next_active_insn (insn, tail);
26423 return group_count;
26426 /* We're beginning a new block. Initialize data structures as necessary. */
26428 static void
26429 rs6000_sched_init (FILE *dump ATTRIBUTE_UNUSED,
26430 int sched_verbose ATTRIBUTE_UNUSED,
26431 int max_ready ATTRIBUTE_UNUSED)
26433 last_scheduled_insn = NULL_RTX;
26434 load_store_pendulum = 0;
26437 /* The following function is called at the end of scheduling BB.
26438 After reload, it inserts nops at insn group bundling. */
26440 static void
26441 rs6000_sched_finish (FILE *dump, int sched_verbose)
26443 int n_groups;
26445 if (sched_verbose)
26446 fprintf (dump, "=== Finishing schedule.\n");
26448 if (reload_completed && rs6000_sched_groups)
26450 /* Do not run sched_finish hook when selective scheduling enabled. */
26451 if (sel_sched_p ())
26452 return;
26454 if (rs6000_sched_insert_nops == sched_finish_none)
26455 return;
26457 if (rs6000_sched_insert_nops == sched_finish_pad_groups)
26458 n_groups = pad_groups (dump, sched_verbose,
26459 current_sched_info->prev_head,
26460 current_sched_info->next_tail);
26461 else
26462 n_groups = redefine_groups (dump, sched_verbose,
26463 current_sched_info->prev_head,
26464 current_sched_info->next_tail);
26466 if (sched_verbose >= 6)
26468 fprintf (dump, "ngroups = %d\n", n_groups);
26469 print_rtl (dump, current_sched_info->prev_head);
26470 fprintf (dump, "Done finish_sched\n");
26475 struct _rs6000_sched_context
26477 short cached_can_issue_more;
26478 rtx last_scheduled_insn;
26479 int load_store_pendulum;
26482 typedef struct _rs6000_sched_context rs6000_sched_context_def;
26483 typedef rs6000_sched_context_def *rs6000_sched_context_t;
26485 /* Allocate store for new scheduling context. */
26486 static void *
26487 rs6000_alloc_sched_context (void)
26489 return xmalloc (sizeof (rs6000_sched_context_def));
26492 /* If CLEAN_P is true then initializes _SC with clean data,
26493 and from the global context otherwise. */
26494 static void
26495 rs6000_init_sched_context (void *_sc, bool clean_p)
26497 rs6000_sched_context_t sc = (rs6000_sched_context_t) _sc;
26499 if (clean_p)
26501 sc->cached_can_issue_more = 0;
26502 sc->last_scheduled_insn = NULL_RTX;
26503 sc->load_store_pendulum = 0;
26505 else
26507 sc->cached_can_issue_more = cached_can_issue_more;
26508 sc->last_scheduled_insn = last_scheduled_insn;
26509 sc->load_store_pendulum = load_store_pendulum;
26513 /* Sets the global scheduling context to the one pointed to by _SC. */
26514 static void
26515 rs6000_set_sched_context (void *_sc)
26517 rs6000_sched_context_t sc = (rs6000_sched_context_t) _sc;
26519 gcc_assert (sc != NULL);
26521 cached_can_issue_more = sc->cached_can_issue_more;
26522 last_scheduled_insn = sc->last_scheduled_insn;
26523 load_store_pendulum = sc->load_store_pendulum;
26526 /* Free _SC. */
26527 static void
26528 rs6000_free_sched_context (void *_sc)
26530 gcc_assert (_sc != NULL);
26532 free (_sc);
26536 /* Length in units of the trampoline for entering a nested function. */
26539 rs6000_trampoline_size (void)
26541 int ret = 0;
26543 switch (DEFAULT_ABI)
26545 default:
26546 gcc_unreachable ();
26548 case ABI_AIX:
26549 ret = (TARGET_32BIT) ? 12 : 24;
26550 break;
26552 case ABI_DARWIN:
26553 case ABI_V4:
26554 ret = (TARGET_32BIT) ? 40 : 48;
26555 break;
26558 return ret;
26561 /* Emit RTL insns to initialize the variable parts of a trampoline.
26562 FNADDR is an RTX for the address of the function's pure code.
26563 CXT is an RTX for the static chain value for the function. */
26565 static void
26566 rs6000_trampoline_init (rtx m_tramp, tree fndecl, rtx cxt)
26568 int regsize = (TARGET_32BIT) ? 4 : 8;
26569 rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
26570 rtx ctx_reg = force_reg (Pmode, cxt);
26571 rtx addr = force_reg (Pmode, XEXP (m_tramp, 0));
26573 switch (DEFAULT_ABI)
26575 default:
26576 gcc_unreachable ();
26578 /* Under AIX, just build the 3 word function descriptor */
26579 case ABI_AIX:
26581 rtx fnmem, fn_reg, toc_reg;
26583 if (!TARGET_POINTERS_TO_NESTED_FUNCTIONS)
26584 error ("You cannot take the address of a nested function if you use "
26585 "the -mno-pointers-to-nested-functions option.");
26587 fnmem = gen_const_mem (Pmode, force_reg (Pmode, fnaddr));
26588 fn_reg = gen_reg_rtx (Pmode);
26589 toc_reg = gen_reg_rtx (Pmode);
26591 /* Macro to shorten the code expansions below. */
26592 # define MEM_PLUS(MEM, OFFSET) adjust_address (MEM, Pmode, OFFSET)
26594 m_tramp = replace_equiv_address (m_tramp, addr);
26596 emit_move_insn (fn_reg, MEM_PLUS (fnmem, 0));
26597 emit_move_insn (toc_reg, MEM_PLUS (fnmem, regsize));
26598 emit_move_insn (MEM_PLUS (m_tramp, 0), fn_reg);
26599 emit_move_insn (MEM_PLUS (m_tramp, regsize), toc_reg);
26600 emit_move_insn (MEM_PLUS (m_tramp, 2*regsize), ctx_reg);
26602 # undef MEM_PLUS
26604 break;
26606 /* Under V.4/eabi/darwin, __trampoline_setup does the real work. */
26607 case ABI_DARWIN:
26608 case ABI_V4:
26609 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__trampoline_setup"),
26610 LCT_NORMAL, VOIDmode, 4,
26611 addr, Pmode,
26612 GEN_INT (rs6000_trampoline_size ()), SImode,
26613 fnaddr, Pmode,
26614 ctx_reg, Pmode);
26615 break;
26620 /* Returns TRUE iff the target attribute indicated by ATTR_ID takes a plain
26621 identifier as an argument, so the front end shouldn't look it up. */
26623 static bool
26624 rs6000_attribute_takes_identifier_p (const_tree attr_id)
26626 return is_attribute_p ("altivec", attr_id);
26629 /* Handle the "altivec" attribute. The attribute may have
26630 arguments as follows:
26632 __attribute__((altivec(vector__)))
26633 __attribute__((altivec(pixel__))) (always followed by 'unsigned short')
26634 __attribute__((altivec(bool__))) (always followed by 'unsigned')
26636 and may appear more than once (e.g., 'vector bool char') in a
26637 given declaration. */
26639 static tree
26640 rs6000_handle_altivec_attribute (tree *node,
26641 tree name ATTRIBUTE_UNUSED,
26642 tree args,
26643 int flags ATTRIBUTE_UNUSED,
26644 bool *no_add_attrs)
26646 tree type = *node, result = NULL_TREE;
26647 enum machine_mode mode;
26648 int unsigned_p;
26649 char altivec_type
26650 = ((args && TREE_CODE (args) == TREE_LIST && TREE_VALUE (args)
26651 && TREE_CODE (TREE_VALUE (args)) == IDENTIFIER_NODE)
26652 ? *IDENTIFIER_POINTER (TREE_VALUE (args))
26653 : '?');
26655 while (POINTER_TYPE_P (type)
26656 || TREE_CODE (type) == FUNCTION_TYPE
26657 || TREE_CODE (type) == METHOD_TYPE
26658 || TREE_CODE (type) == ARRAY_TYPE)
26659 type = TREE_TYPE (type);
26661 mode = TYPE_MODE (type);
26663 /* Check for invalid AltiVec type qualifiers. */
26664 if (type == long_double_type_node)
26665 error ("use of %<long double%> in AltiVec types is invalid");
26666 else if (type == boolean_type_node)
26667 error ("use of boolean types in AltiVec types is invalid");
26668 else if (TREE_CODE (type) == COMPLEX_TYPE)
26669 error ("use of %<complex%> in AltiVec types is invalid");
26670 else if (DECIMAL_FLOAT_MODE_P (mode))
26671 error ("use of decimal floating point types in AltiVec types is invalid");
26672 else if (!TARGET_VSX)
26674 if (type == long_unsigned_type_node || type == long_integer_type_node)
26676 if (TARGET_64BIT)
26677 error ("use of %<long%> in AltiVec types is invalid for "
26678 "64-bit code without -mvsx");
26679 else if (rs6000_warn_altivec_long)
26680 warning (0, "use of %<long%> in AltiVec types is deprecated; "
26681 "use %<int%>");
26683 else if (type == long_long_unsigned_type_node
26684 || type == long_long_integer_type_node)
26685 error ("use of %<long long%> in AltiVec types is invalid without "
26686 "-mvsx");
26687 else if (type == double_type_node)
26688 error ("use of %<double%> in AltiVec types is invalid without -mvsx");
26691 switch (altivec_type)
26693 case 'v':
26694 unsigned_p = TYPE_UNSIGNED (type);
26695 switch (mode)
26697 case DImode:
26698 result = (unsigned_p ? unsigned_V2DI_type_node : V2DI_type_node);
26699 break;
26700 case SImode:
26701 result = (unsigned_p ? unsigned_V4SI_type_node : V4SI_type_node);
26702 break;
26703 case HImode:
26704 result = (unsigned_p ? unsigned_V8HI_type_node : V8HI_type_node);
26705 break;
26706 case QImode:
26707 result = (unsigned_p ? unsigned_V16QI_type_node : V16QI_type_node);
26708 break;
26709 case SFmode: result = V4SF_type_node; break;
26710 case DFmode: result = V2DF_type_node; break;
26711 /* If the user says 'vector int bool', we may be handed the 'bool'
26712 attribute _before_ the 'vector' attribute, and so select the
26713 proper type in the 'b' case below. */
26714 case V4SImode: case V8HImode: case V16QImode: case V4SFmode:
26715 case V2DImode: case V2DFmode:
26716 result = type;
26717 default: break;
26719 break;
26720 case 'b':
26721 switch (mode)
26723 case DImode: case V2DImode: result = bool_V2DI_type_node; break;
26724 case SImode: case V4SImode: result = bool_V4SI_type_node; break;
26725 case HImode: case V8HImode: result = bool_V8HI_type_node; break;
26726 case QImode: case V16QImode: result = bool_V16QI_type_node;
26727 default: break;
26729 break;
26730 case 'p':
26731 switch (mode)
26733 case V8HImode: result = pixel_V8HI_type_node;
26734 default: break;
26736 default: break;
26739 /* Propagate qualifiers attached to the element type
26740 onto the vector type. */
26741 if (result && result != type && TYPE_QUALS (type))
26742 result = build_qualified_type (result, TYPE_QUALS (type));
26744 *no_add_attrs = true; /* No need to hang on to the attribute. */
26746 if (result)
26747 *node = lang_hooks.types.reconstruct_complex_type (*node, result);
26749 return NULL_TREE;
26752 /* AltiVec defines four built-in scalar types that serve as vector
26753 elements; we must teach the compiler how to mangle them. */
26755 static const char *
26756 rs6000_mangle_type (const_tree type)
26758 type = TYPE_MAIN_VARIANT (type);
26760 if (TREE_CODE (type) != VOID_TYPE && TREE_CODE (type) != BOOLEAN_TYPE
26761 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
26762 return NULL;
26764 if (type == bool_char_type_node) return "U6__boolc";
26765 if (type == bool_short_type_node) return "U6__bools";
26766 if (type == pixel_type_node) return "u7__pixel";
26767 if (type == bool_int_type_node) return "U6__booli";
26768 if (type == bool_long_type_node) return "U6__booll";
26770 /* Mangle IBM extended float long double as `g' (__float128) on
26771 powerpc*-linux where long-double-64 previously was the default. */
26772 if (TYPE_MAIN_VARIANT (type) == long_double_type_node
26773 && TARGET_ELF
26774 && TARGET_LONG_DOUBLE_128
26775 && !TARGET_IEEEQUAD)
26776 return "g";
26778 /* For all other types, use normal C++ mangling. */
26779 return NULL;
26782 /* Handle a "longcall" or "shortcall" attribute; arguments as in
26783 struct attribute_spec.handler. */
26785 static tree
26786 rs6000_handle_longcall_attribute (tree *node, tree name,
26787 tree args ATTRIBUTE_UNUSED,
26788 int flags ATTRIBUTE_UNUSED,
26789 bool *no_add_attrs)
26791 if (TREE_CODE (*node) != FUNCTION_TYPE
26792 && TREE_CODE (*node) != FIELD_DECL
26793 && TREE_CODE (*node) != TYPE_DECL)
26795 warning (OPT_Wattributes, "%qE attribute only applies to functions",
26796 name);
26797 *no_add_attrs = true;
26800 return NULL_TREE;
26803 /* Set longcall attributes on all functions declared when
26804 rs6000_default_long_calls is true. */
26805 static void
26806 rs6000_set_default_type_attributes (tree type)
26808 if (rs6000_default_long_calls
26809 && (TREE_CODE (type) == FUNCTION_TYPE
26810 || TREE_CODE (type) == METHOD_TYPE))
26811 TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("longcall"),
26812 NULL_TREE,
26813 TYPE_ATTRIBUTES (type));
26815 #if TARGET_MACHO
26816 darwin_set_default_type_attributes (type);
26817 #endif
26820 /* Return a reference suitable for calling a function with the
26821 longcall attribute. */
26824 rs6000_longcall_ref (rtx call_ref)
26826 const char *call_name;
26827 tree node;
26829 if (GET_CODE (call_ref) != SYMBOL_REF)
26830 return call_ref;
26832 /* System V adds '.' to the internal name, so skip them. */
26833 call_name = XSTR (call_ref, 0);
26834 if (*call_name == '.')
26836 while (*call_name == '.')
26837 call_name++;
26839 node = get_identifier (call_name);
26840 call_ref = gen_rtx_SYMBOL_REF (VOIDmode, IDENTIFIER_POINTER (node));
26843 return force_reg (Pmode, call_ref);
26846 #ifndef TARGET_USE_MS_BITFIELD_LAYOUT
26847 #define TARGET_USE_MS_BITFIELD_LAYOUT 0
26848 #endif
26850 /* Handle a "ms_struct" or "gcc_struct" attribute; arguments as in
26851 struct attribute_spec.handler. */
26852 static tree
26853 rs6000_handle_struct_attribute (tree *node, tree name,
26854 tree args ATTRIBUTE_UNUSED,
26855 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
26857 tree *type = NULL;
26858 if (DECL_P (*node))
26860 if (TREE_CODE (*node) == TYPE_DECL)
26861 type = &TREE_TYPE (*node);
26863 else
26864 type = node;
26866 if (!(type && (TREE_CODE (*type) == RECORD_TYPE
26867 || TREE_CODE (*type) == UNION_TYPE)))
26869 warning (OPT_Wattributes, "%qE attribute ignored", name);
26870 *no_add_attrs = true;
26873 else if ((is_attribute_p ("ms_struct", name)
26874 && lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (*type)))
26875 || ((is_attribute_p ("gcc_struct", name)
26876 && lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (*type)))))
26878 warning (OPT_Wattributes, "%qE incompatible attribute ignored",
26879 name);
26880 *no_add_attrs = true;
26883 return NULL_TREE;
26886 static bool
26887 rs6000_ms_bitfield_layout_p (const_tree record_type)
26889 return (TARGET_USE_MS_BITFIELD_LAYOUT &&
26890 !lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (record_type)))
26891 || lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (record_type));
26894 #ifdef USING_ELFOS_H
26896 /* A get_unnamed_section callback, used for switching to toc_section. */
26898 static void
26899 rs6000_elf_output_toc_section_asm_op (const void *data ATTRIBUTE_UNUSED)
26901 if (DEFAULT_ABI == ABI_AIX
26902 && TARGET_MINIMAL_TOC
26903 && !TARGET_RELOCATABLE)
26905 if (!toc_initialized)
26907 toc_initialized = 1;
26908 fprintf (asm_out_file, "%s\n", TOC_SECTION_ASM_OP);
26909 (*targetm.asm_out.internal_label) (asm_out_file, "LCTOC", 0);
26910 fprintf (asm_out_file, "\t.tc ");
26911 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1[TC],");
26912 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
26913 fprintf (asm_out_file, "\n");
26915 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
26916 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
26917 fprintf (asm_out_file, " = .+32768\n");
26919 else
26920 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
26922 else if (DEFAULT_ABI == ABI_AIX && !TARGET_RELOCATABLE)
26923 fprintf (asm_out_file, "%s\n", TOC_SECTION_ASM_OP);
26924 else
26926 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
26927 if (!toc_initialized)
26929 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
26930 fprintf (asm_out_file, " = .+32768\n");
26931 toc_initialized = 1;
26936 /* Implement TARGET_ASM_INIT_SECTIONS. */
26938 static void
26939 rs6000_elf_asm_init_sections (void)
26941 toc_section
26942 = get_unnamed_section (0, rs6000_elf_output_toc_section_asm_op, NULL);
26944 sdata2_section
26945 = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
26946 SDATA2_SECTION_ASM_OP);
26949 /* Implement TARGET_SELECT_RTX_SECTION. */
26951 static section *
26952 rs6000_elf_select_rtx_section (enum machine_mode mode, rtx x,
26953 unsigned HOST_WIDE_INT align)
26955 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
26956 return toc_section;
26957 else
26958 return default_elf_select_rtx_section (mode, x, align);
26961 /* For a SYMBOL_REF, set generic flags and then perform some
26962 target-specific processing.
26964 When the AIX ABI is requested on a non-AIX system, replace the
26965 function name with the real name (with a leading .) rather than the
26966 function descriptor name. This saves a lot of overriding code to
26967 read the prefixes. */
26969 static void rs6000_elf_encode_section_info (tree, rtx, int) ATTRIBUTE_UNUSED;
26970 static void
26971 rs6000_elf_encode_section_info (tree decl, rtx rtl, int first)
26973 default_encode_section_info (decl, rtl, first);
26975 if (first
26976 && TREE_CODE (decl) == FUNCTION_DECL
26977 && !TARGET_AIX
26978 && DEFAULT_ABI == ABI_AIX)
26980 rtx sym_ref = XEXP (rtl, 0);
26981 size_t len = strlen (XSTR (sym_ref, 0));
26982 char *str = XALLOCAVEC (char, len + 2);
26983 str[0] = '.';
26984 memcpy (str + 1, XSTR (sym_ref, 0), len + 1);
26985 XSTR (sym_ref, 0) = ggc_alloc_string (str, len + 1);
26989 static inline bool
26990 compare_section_name (const char *section, const char *templ)
26992 int len;
26994 len = strlen (templ);
26995 return (strncmp (section, templ, len) == 0
26996 && (section[len] == 0 || section[len] == '.'));
26999 bool
27000 rs6000_elf_in_small_data_p (const_tree decl)
27002 if (rs6000_sdata == SDATA_NONE)
27003 return false;
27005 /* We want to merge strings, so we never consider them small data. */
27006 if (TREE_CODE (decl) == STRING_CST)
27007 return false;
27009 /* Functions are never in the small data area. */
27010 if (TREE_CODE (decl) == FUNCTION_DECL)
27011 return false;
27013 if (TREE_CODE (decl) == VAR_DECL && DECL_SECTION_NAME (decl))
27015 const char *section = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
27016 if (compare_section_name (section, ".sdata")
27017 || compare_section_name (section, ".sdata2")
27018 || compare_section_name (section, ".gnu.linkonce.s")
27019 || compare_section_name (section, ".sbss")
27020 || compare_section_name (section, ".sbss2")
27021 || compare_section_name (section, ".gnu.linkonce.sb")
27022 || strcmp (section, ".PPC.EMB.sdata0") == 0
27023 || strcmp (section, ".PPC.EMB.sbss0") == 0)
27024 return true;
27026 else
27028 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (decl));
27030 if (size > 0
27031 && size <= g_switch_value
27032 /* If it's not public, and we're not going to reference it there,
27033 there's no need to put it in the small data section. */
27034 && (rs6000_sdata != SDATA_DATA || TREE_PUBLIC (decl)))
27035 return true;
27038 return false;
27041 #endif /* USING_ELFOS_H */
27043 /* Implement TARGET_USE_BLOCKS_FOR_CONSTANT_P. */
27045 static bool
27046 rs6000_use_blocks_for_constant_p (enum machine_mode mode, const_rtx x)
27048 return !ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode);
27051 /* Do not place thread-local symbols refs in the object blocks. */
27053 static bool
27054 rs6000_use_blocks_for_decl_p (const_tree decl)
27056 return !DECL_THREAD_LOCAL_P (decl);
27059 /* Return a REG that occurs in ADDR with coefficient 1.
27060 ADDR can be effectively incremented by incrementing REG.
27062 r0 is special and we must not select it as an address
27063 register by this routine since our caller will try to
27064 increment the returned register via an "la" instruction. */
27067 find_addr_reg (rtx addr)
27069 while (GET_CODE (addr) == PLUS)
27071 if (GET_CODE (XEXP (addr, 0)) == REG
27072 && REGNO (XEXP (addr, 0)) != 0)
27073 addr = XEXP (addr, 0);
27074 else if (GET_CODE (XEXP (addr, 1)) == REG
27075 && REGNO (XEXP (addr, 1)) != 0)
27076 addr = XEXP (addr, 1);
27077 else if (CONSTANT_P (XEXP (addr, 0)))
27078 addr = XEXP (addr, 1);
27079 else if (CONSTANT_P (XEXP (addr, 1)))
27080 addr = XEXP (addr, 0);
27081 else
27082 gcc_unreachable ();
27084 gcc_assert (GET_CODE (addr) == REG && REGNO (addr) != 0);
27085 return addr;
27088 void
27089 rs6000_fatal_bad_address (rtx op)
27091 fatal_insn ("bad address", op);
27094 #if TARGET_MACHO
27096 typedef struct branch_island_d {
27097 tree function_name;
27098 tree label_name;
27099 int line_number;
27100 } branch_island;
27103 static vec<branch_island, va_gc> *branch_islands;
27105 /* Remember to generate a branch island for far calls to the given
27106 function. */
27108 static void
27109 add_compiler_branch_island (tree label_name, tree function_name,
27110 int line_number)
27112 branch_island bi = {function_name, label_name, line_number};
27113 vec_safe_push (branch_islands, bi);
27116 /* Generate far-jump branch islands for everything recorded in
27117 branch_islands. Invoked immediately after the last instruction of
27118 the epilogue has been emitted; the branch islands must be appended
27119 to, and contiguous with, the function body. Mach-O stubs are
27120 generated in machopic_output_stub(). */
27122 static void
27123 macho_branch_islands (void)
27125 char tmp_buf[512];
27127 while (!vec_safe_is_empty (branch_islands))
27129 branch_island *bi = &branch_islands->last ();
27130 const char *label = IDENTIFIER_POINTER (bi->label_name);
27131 const char *name = IDENTIFIER_POINTER (bi->function_name);
27132 char name_buf[512];
27133 /* Cheap copy of the details from the Darwin ASM_OUTPUT_LABELREF(). */
27134 if (name[0] == '*' || name[0] == '&')
27135 strcpy (name_buf, name+1);
27136 else
27138 name_buf[0] = '_';
27139 strcpy (name_buf+1, name);
27141 strcpy (tmp_buf, "\n");
27142 strcat (tmp_buf, label);
27143 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
27144 if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
27145 dbxout_stabd (N_SLINE, bi->line_number);
27146 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
27147 if (flag_pic)
27149 if (TARGET_LINK_STACK)
27151 char name[32];
27152 get_ppc476_thunk_name (name);
27153 strcat (tmp_buf, ":\n\tmflr r0\n\tbl ");
27154 strcat (tmp_buf, name);
27155 strcat (tmp_buf, "\n");
27156 strcat (tmp_buf, label);
27157 strcat (tmp_buf, "_pic:\n\tmflr r11\n");
27159 else
27161 strcat (tmp_buf, ":\n\tmflr r0\n\tbcl 20,31,");
27162 strcat (tmp_buf, label);
27163 strcat (tmp_buf, "_pic\n");
27164 strcat (tmp_buf, label);
27165 strcat (tmp_buf, "_pic:\n\tmflr r11\n");
27168 strcat (tmp_buf, "\taddis r11,r11,ha16(");
27169 strcat (tmp_buf, name_buf);
27170 strcat (tmp_buf, " - ");
27171 strcat (tmp_buf, label);
27172 strcat (tmp_buf, "_pic)\n");
27174 strcat (tmp_buf, "\tmtlr r0\n");
27176 strcat (tmp_buf, "\taddi r12,r11,lo16(");
27177 strcat (tmp_buf, name_buf);
27178 strcat (tmp_buf, " - ");
27179 strcat (tmp_buf, label);
27180 strcat (tmp_buf, "_pic)\n");
27182 strcat (tmp_buf, "\tmtctr r12\n\tbctr\n");
27184 else
27186 strcat (tmp_buf, ":\nlis r12,hi16(");
27187 strcat (tmp_buf, name_buf);
27188 strcat (tmp_buf, ")\n\tori r12,r12,lo16(");
27189 strcat (tmp_buf, name_buf);
27190 strcat (tmp_buf, ")\n\tmtctr r12\n\tbctr");
27192 output_asm_insn (tmp_buf, 0);
27193 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
27194 if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
27195 dbxout_stabd (N_SLINE, bi->line_number);
27196 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
27197 branch_islands->pop ();
27201 /* NO_PREVIOUS_DEF checks in the link list whether the function name is
27202 already there or not. */
27204 static int
27205 no_previous_def (tree function_name)
27207 branch_island *bi;
27208 unsigned ix;
27210 FOR_EACH_VEC_SAFE_ELT (branch_islands, ix, bi)
27211 if (function_name == bi->function_name)
27212 return 0;
27213 return 1;
27216 /* GET_PREV_LABEL gets the label name from the previous definition of
27217 the function. */
27219 static tree
27220 get_prev_label (tree function_name)
27222 branch_island *bi;
27223 unsigned ix;
27225 FOR_EACH_VEC_SAFE_ELT (branch_islands, ix, bi)
27226 if (function_name == bi->function_name)
27227 return bi->label_name;
27228 return NULL_TREE;
27231 /* INSN is either a function call or a millicode call. It may have an
27232 unconditional jump in its delay slot.
27234 CALL_DEST is the routine we are calling. */
27236 char *
27237 output_call (rtx insn, rtx *operands, int dest_operand_number,
27238 int cookie_operand_number)
27240 static char buf[256];
27241 if (darwin_emit_branch_islands
27242 && GET_CODE (operands[dest_operand_number]) == SYMBOL_REF
27243 && (INTVAL (operands[cookie_operand_number]) & CALL_LONG))
27245 tree labelname;
27246 tree funname = get_identifier (XSTR (operands[dest_operand_number], 0));
27248 if (no_previous_def (funname))
27250 rtx label_rtx = gen_label_rtx ();
27251 char *label_buf, temp_buf[256];
27252 ASM_GENERATE_INTERNAL_LABEL (temp_buf, "L",
27253 CODE_LABEL_NUMBER (label_rtx));
27254 label_buf = temp_buf[0] == '*' ? temp_buf + 1 : temp_buf;
27255 labelname = get_identifier (label_buf);
27256 add_compiler_branch_island (labelname, funname, insn_line (insn));
27258 else
27259 labelname = get_prev_label (funname);
27261 /* "jbsr foo, L42" is Mach-O for "Link as 'bl foo' if a 'bl'
27262 instruction will reach 'foo', otherwise link as 'bl L42'".
27263 "L42" should be a 'branch island', that will do a far jump to
27264 'foo'. Branch islands are generated in
27265 macho_branch_islands(). */
27266 sprintf (buf, "jbsr %%z%d,%.246s",
27267 dest_operand_number, IDENTIFIER_POINTER (labelname));
27269 else
27270 sprintf (buf, "bl %%z%d", dest_operand_number);
27271 return buf;
27274 /* Generate PIC and indirect symbol stubs. */
27276 void
27277 machopic_output_stub (FILE *file, const char *symb, const char *stub)
27279 unsigned int length;
27280 char *symbol_name, *lazy_ptr_name;
27281 char *local_label_0;
27282 static int label = 0;
27284 /* Lose our funky encoding stuff so it doesn't contaminate the stub. */
27285 symb = (*targetm.strip_name_encoding) (symb);
27288 length = strlen (symb);
27289 symbol_name = XALLOCAVEC (char, length + 32);
27290 GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
27292 lazy_ptr_name = XALLOCAVEC (char, length + 32);
27293 GEN_LAZY_PTR_NAME_FOR_SYMBOL (lazy_ptr_name, symb, length);
27295 if (flag_pic == 2)
27296 switch_to_section (darwin_sections[machopic_picsymbol_stub1_section]);
27297 else
27298 switch_to_section (darwin_sections[machopic_symbol_stub1_section]);
27300 if (flag_pic == 2)
27302 fprintf (file, "\t.align 5\n");
27304 fprintf (file, "%s:\n", stub);
27305 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
27307 label++;
27308 local_label_0 = XALLOCAVEC (char, sizeof ("\"L00000000000$spb\""));
27309 sprintf (local_label_0, "\"L%011d$spb\"", label);
27311 fprintf (file, "\tmflr r0\n");
27312 if (TARGET_LINK_STACK)
27314 char name[32];
27315 get_ppc476_thunk_name (name);
27316 fprintf (file, "\tbl %s\n", name);
27317 fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
27319 else
27321 fprintf (file, "\tbcl 20,31,%s\n", local_label_0);
27322 fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
27324 fprintf (file, "\taddis r11,r11,ha16(%s-%s)\n",
27325 lazy_ptr_name, local_label_0);
27326 fprintf (file, "\tmtlr r0\n");
27327 fprintf (file, "\t%s r12,lo16(%s-%s)(r11)\n",
27328 (TARGET_64BIT ? "ldu" : "lwzu"),
27329 lazy_ptr_name, local_label_0);
27330 fprintf (file, "\tmtctr r12\n");
27331 fprintf (file, "\tbctr\n");
27333 else
27335 fprintf (file, "\t.align 4\n");
27337 fprintf (file, "%s:\n", stub);
27338 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
27340 fprintf (file, "\tlis r11,ha16(%s)\n", lazy_ptr_name);
27341 fprintf (file, "\t%s r12,lo16(%s)(r11)\n",
27342 (TARGET_64BIT ? "ldu" : "lwzu"),
27343 lazy_ptr_name);
27344 fprintf (file, "\tmtctr r12\n");
27345 fprintf (file, "\tbctr\n");
27348 switch_to_section (darwin_sections[machopic_lazy_symbol_ptr_section]);
27349 fprintf (file, "%s:\n", lazy_ptr_name);
27350 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
27351 fprintf (file, "%sdyld_stub_binding_helper\n",
27352 (TARGET_64BIT ? DOUBLE_INT_ASM_OP : "\t.long\t"));
27355 /* Legitimize PIC addresses. If the address is already
27356 position-independent, we return ORIG. Newly generated
27357 position-independent addresses go into a reg. This is REG if non
27358 zero, otherwise we allocate register(s) as necessary. */
27360 #define SMALL_INT(X) ((UINTVAL (X) + 0x8000) < 0x10000)
27363 rs6000_machopic_legitimize_pic_address (rtx orig, enum machine_mode mode,
27364 rtx reg)
27366 rtx base, offset;
27368 if (reg == NULL && ! reload_in_progress && ! reload_completed)
27369 reg = gen_reg_rtx (Pmode);
27371 if (GET_CODE (orig) == CONST)
27373 rtx reg_temp;
27375 if (GET_CODE (XEXP (orig, 0)) == PLUS
27376 && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
27377 return orig;
27379 gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
27381 /* Use a different reg for the intermediate value, as
27382 it will be marked UNCHANGING. */
27383 reg_temp = !can_create_pseudo_p () ? reg : gen_reg_rtx (Pmode);
27384 base = rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 0),
27385 Pmode, reg_temp);
27386 offset =
27387 rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 1),
27388 Pmode, reg);
27390 if (GET_CODE (offset) == CONST_INT)
27392 if (SMALL_INT (offset))
27393 return plus_constant (Pmode, base, INTVAL (offset));
27394 else if (! reload_in_progress && ! reload_completed)
27395 offset = force_reg (Pmode, offset);
27396 else
27398 rtx mem = force_const_mem (Pmode, orig);
27399 return machopic_legitimize_pic_address (mem, Pmode, reg);
27402 return gen_rtx_PLUS (Pmode, base, offset);
27405 /* Fall back on generic machopic code. */
27406 return machopic_legitimize_pic_address (orig, mode, reg);
27409 /* Output a .machine directive for the Darwin assembler, and call
27410 the generic start_file routine. */
27412 static void
27413 rs6000_darwin_file_start (void)
27415 static const struct
27417 const char *arg;
27418 const char *name;
27419 HOST_WIDE_INT if_set;
27420 } mapping[] = {
27421 { "ppc64", "ppc64", MASK_64BIT },
27422 { "970", "ppc970", MASK_PPC_GPOPT | MASK_MFCRF | MASK_POWERPC64 },
27423 { "power4", "ppc970", 0 },
27424 { "G5", "ppc970", 0 },
27425 { "7450", "ppc7450", 0 },
27426 { "7400", "ppc7400", MASK_ALTIVEC },
27427 { "G4", "ppc7400", 0 },
27428 { "750", "ppc750", 0 },
27429 { "740", "ppc750", 0 },
27430 { "G3", "ppc750", 0 },
27431 { "604e", "ppc604e", 0 },
27432 { "604", "ppc604", 0 },
27433 { "603e", "ppc603", 0 },
27434 { "603", "ppc603", 0 },
27435 { "601", "ppc601", 0 },
27436 { NULL, "ppc", 0 } };
27437 const char *cpu_id = "";
27438 size_t i;
27440 rs6000_file_start ();
27441 darwin_file_start ();
27443 /* Determine the argument to -mcpu=. Default to G3 if not specified. */
27445 if (rs6000_default_cpu != 0 && rs6000_default_cpu[0] != '\0')
27446 cpu_id = rs6000_default_cpu;
27448 if (global_options_set.x_rs6000_cpu_index)
27449 cpu_id = processor_target_table[rs6000_cpu_index].name;
27451 /* Look through the mapping array. Pick the first name that either
27452 matches the argument, has a bit set in IF_SET that is also set
27453 in the target flags, or has a NULL name. */
27455 i = 0;
27456 while (mapping[i].arg != NULL
27457 && strcmp (mapping[i].arg, cpu_id) != 0
27458 && (mapping[i].if_set & rs6000_isa_flags) == 0)
27459 i++;
27461 fprintf (asm_out_file, "\t.machine %s\n", mapping[i].name);
27464 #endif /* TARGET_MACHO */
27466 #if TARGET_ELF
27467 static int
27468 rs6000_elf_reloc_rw_mask (void)
27470 if (flag_pic)
27471 return 3;
27472 else if (DEFAULT_ABI == ABI_AIX)
27473 return 2;
27474 else
27475 return 0;
27478 /* Record an element in the table of global constructors. SYMBOL is
27479 a SYMBOL_REF of the function to be called; PRIORITY is a number
27480 between 0 and MAX_INIT_PRIORITY.
27482 This differs from default_named_section_asm_out_constructor in
27483 that we have special handling for -mrelocatable. */
27485 static void rs6000_elf_asm_out_constructor (rtx, int) ATTRIBUTE_UNUSED;
27486 static void
27487 rs6000_elf_asm_out_constructor (rtx symbol, int priority)
27489 const char *section = ".ctors";
27490 char buf[16];
27492 if (priority != DEFAULT_INIT_PRIORITY)
27494 sprintf (buf, ".ctors.%.5u",
27495 /* Invert the numbering so the linker puts us in the proper
27496 order; constructors are run from right to left, and the
27497 linker sorts in increasing order. */
27498 MAX_INIT_PRIORITY - priority);
27499 section = buf;
27502 switch_to_section (get_section (section, SECTION_WRITE, NULL));
27503 assemble_align (POINTER_SIZE);
27505 if (TARGET_RELOCATABLE)
27507 fputs ("\t.long (", asm_out_file);
27508 output_addr_const (asm_out_file, symbol);
27509 fputs (")@fixup\n", asm_out_file);
27511 else
27512 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
27515 static void rs6000_elf_asm_out_destructor (rtx, int) ATTRIBUTE_UNUSED;
27516 static void
27517 rs6000_elf_asm_out_destructor (rtx symbol, int priority)
27519 const char *section = ".dtors";
27520 char buf[16];
27522 if (priority != DEFAULT_INIT_PRIORITY)
27524 sprintf (buf, ".dtors.%.5u",
27525 /* Invert the numbering so the linker puts us in the proper
27526 order; constructors are run from right to left, and the
27527 linker sorts in increasing order. */
27528 MAX_INIT_PRIORITY - priority);
27529 section = buf;
27532 switch_to_section (get_section (section, SECTION_WRITE, NULL));
27533 assemble_align (POINTER_SIZE);
27535 if (TARGET_RELOCATABLE)
27537 fputs ("\t.long (", asm_out_file);
27538 output_addr_const (asm_out_file, symbol);
27539 fputs (")@fixup\n", asm_out_file);
27541 else
27542 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
27545 void
27546 rs6000_elf_declare_function_name (FILE *file, const char *name, tree decl)
27548 if (TARGET_64BIT)
27550 fputs ("\t.section\t\".opd\",\"aw\"\n\t.align 3\n", file);
27551 ASM_OUTPUT_LABEL (file, name);
27552 fputs (DOUBLE_INT_ASM_OP, file);
27553 rs6000_output_function_entry (file, name);
27554 fputs (",.TOC.@tocbase,0\n\t.previous\n", file);
27555 if (DOT_SYMBOLS)
27557 fputs ("\t.size\t", file);
27558 assemble_name (file, name);
27559 fputs (",24\n\t.type\t.", file);
27560 assemble_name (file, name);
27561 fputs (",@function\n", file);
27562 if (TREE_PUBLIC (decl) && ! DECL_WEAK (decl))
27564 fputs ("\t.globl\t.", file);
27565 assemble_name (file, name);
27566 putc ('\n', file);
27569 else
27570 ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
27571 ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
27572 rs6000_output_function_entry (file, name);
27573 fputs (":\n", file);
27574 return;
27577 if (TARGET_RELOCATABLE
27578 && !TARGET_SECURE_PLT
27579 && (get_pool_size () != 0 || crtl->profile)
27580 && uses_TOC ())
27582 char buf[256];
27584 (*targetm.asm_out.internal_label) (file, "LCL", rs6000_pic_labelno);
27586 ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
27587 fprintf (file, "\t.long ");
27588 assemble_name (file, buf);
27589 putc ('-', file);
27590 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
27591 assemble_name (file, buf);
27592 putc ('\n', file);
27595 ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
27596 ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
27598 if (DEFAULT_ABI == ABI_AIX)
27600 const char *desc_name, *orig_name;
27602 orig_name = (*targetm.strip_name_encoding) (name);
27603 desc_name = orig_name;
27604 while (*desc_name == '.')
27605 desc_name++;
27607 if (TREE_PUBLIC (decl))
27608 fprintf (file, "\t.globl %s\n", desc_name);
27610 fprintf (file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
27611 fprintf (file, "%s:\n", desc_name);
27612 fprintf (file, "\t.long %s\n", orig_name);
27613 fputs ("\t.long _GLOBAL_OFFSET_TABLE_\n", file);
27614 if (DEFAULT_ABI == ABI_AIX)
27615 fputs ("\t.long 0\n", file);
27616 fprintf (file, "\t.previous\n");
27618 ASM_OUTPUT_LABEL (file, name);
27621 static void rs6000_elf_file_end (void) ATTRIBUTE_UNUSED;
27622 static void
27623 rs6000_elf_file_end (void)
27625 #ifdef HAVE_AS_GNU_ATTRIBUTE
27626 if (TARGET_32BIT && DEFAULT_ABI == ABI_V4)
27628 if (rs6000_passes_float)
27629 fprintf (asm_out_file, "\t.gnu_attribute 4, %d\n",
27630 ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT) ? 1
27631 : (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_SINGLE_FLOAT) ? 3
27632 : 2));
27633 if (rs6000_passes_vector)
27634 fprintf (asm_out_file, "\t.gnu_attribute 8, %d\n",
27635 (TARGET_ALTIVEC_ABI ? 2
27636 : TARGET_SPE_ABI ? 3
27637 : 1));
27638 if (rs6000_returns_struct)
27639 fprintf (asm_out_file, "\t.gnu_attribute 12, %d\n",
27640 aix_struct_return ? 2 : 1);
27642 #endif
27643 #if defined (POWERPC_LINUX) || defined (POWERPC_FREEBSD)
27644 if (TARGET_32BIT)
27645 file_end_indicate_exec_stack ();
27646 #endif
27648 #endif
27650 #if TARGET_XCOFF
27651 static void
27652 rs6000_xcoff_asm_output_anchor (rtx symbol)
27654 char buffer[100];
27656 sprintf (buffer, "$ + " HOST_WIDE_INT_PRINT_DEC,
27657 SYMBOL_REF_BLOCK_OFFSET (symbol));
27658 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
27661 static void
27662 rs6000_xcoff_asm_globalize_label (FILE *stream, const char *name)
27664 fputs (GLOBAL_ASM_OP, stream);
27665 RS6000_OUTPUT_BASENAME (stream, name);
27666 putc ('\n', stream);
27669 /* A get_unnamed_decl callback, used for read-only sections. PTR
27670 points to the section string variable. */
27672 static void
27673 rs6000_xcoff_output_readonly_section_asm_op (const void *directive)
27675 fprintf (asm_out_file, "\t.csect %s[RO],%s\n",
27676 *(const char *const *) directive,
27677 XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
27680 /* Likewise for read-write sections. */
27682 static void
27683 rs6000_xcoff_output_readwrite_section_asm_op (const void *directive)
27685 fprintf (asm_out_file, "\t.csect %s[RW],%s\n",
27686 *(const char *const *) directive,
27687 XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
27690 static void
27691 rs6000_xcoff_output_tls_section_asm_op (const void *directive)
27693 fprintf (asm_out_file, "\t.csect %s[TL],%s\n",
27694 *(const char *const *) directive,
27695 XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
27698 /* A get_unnamed_section callback, used for switching to toc_section. */
27700 static void
27701 rs6000_xcoff_output_toc_section_asm_op (const void *data ATTRIBUTE_UNUSED)
27703 if (TARGET_MINIMAL_TOC)
27705 /* toc_section is always selected at least once from
27706 rs6000_xcoff_file_start, so this is guaranteed to
27707 always be defined once and only once in each file. */
27708 if (!toc_initialized)
27710 fputs ("\t.toc\nLCTOC..1:\n", asm_out_file);
27711 fputs ("\t.tc toc_table[TC],toc_table[RW]\n", asm_out_file);
27712 toc_initialized = 1;
27714 fprintf (asm_out_file, "\t.csect toc_table[RW]%s\n",
27715 (TARGET_32BIT ? "" : ",3"));
27717 else
27718 fputs ("\t.toc\n", asm_out_file);
27721 /* Implement TARGET_ASM_INIT_SECTIONS. */
27723 static void
27724 rs6000_xcoff_asm_init_sections (void)
27726 read_only_data_section
27727 = get_unnamed_section (0, rs6000_xcoff_output_readonly_section_asm_op,
27728 &xcoff_read_only_section_name);
27730 private_data_section
27731 = get_unnamed_section (SECTION_WRITE,
27732 rs6000_xcoff_output_readwrite_section_asm_op,
27733 &xcoff_private_data_section_name);
27735 tls_data_section
27736 = get_unnamed_section (SECTION_TLS,
27737 rs6000_xcoff_output_tls_section_asm_op,
27738 &xcoff_tls_data_section_name);
27740 tls_private_data_section
27741 = get_unnamed_section (SECTION_TLS,
27742 rs6000_xcoff_output_tls_section_asm_op,
27743 &xcoff_private_data_section_name);
27745 read_only_private_data_section
27746 = get_unnamed_section (0, rs6000_xcoff_output_readonly_section_asm_op,
27747 &xcoff_private_data_section_name);
27749 toc_section
27750 = get_unnamed_section (0, rs6000_xcoff_output_toc_section_asm_op, NULL);
27752 readonly_data_section = read_only_data_section;
27753 exception_section = data_section;
27756 static int
27757 rs6000_xcoff_reloc_rw_mask (void)
27759 return 3;
27762 static void
27763 rs6000_xcoff_asm_named_section (const char *name, unsigned int flags,
27764 tree decl ATTRIBUTE_UNUSED)
27766 int smclass;
27767 static const char * const suffix[4] = { "PR", "RO", "RW", "TL" };
27769 if (flags & SECTION_CODE)
27770 smclass = 0;
27771 else if (flags & SECTION_TLS)
27772 smclass = 3;
27773 else if (flags & SECTION_WRITE)
27774 smclass = 2;
27775 else
27776 smclass = 1;
27778 fprintf (asm_out_file, "\t.csect %s%s[%s],%u\n",
27779 (flags & SECTION_CODE) ? "." : "",
27780 name, suffix[smclass], flags & SECTION_ENTSIZE);
27783 static section *
27784 rs6000_xcoff_select_section (tree decl, int reloc,
27785 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
27787 if (decl_readonly_section (decl, reloc))
27789 if (TREE_PUBLIC (decl))
27790 return read_only_data_section;
27791 else
27792 return read_only_private_data_section;
27794 else
27796 #if HAVE_AS_TLS
27797 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
27799 if (TREE_PUBLIC (decl))
27800 return tls_data_section;
27801 else if (bss_initializer_p (decl))
27803 /* Convert to COMMON to emit in BSS. */
27804 DECL_COMMON (decl) = 1;
27805 return tls_comm_section;
27807 else
27808 return tls_private_data_section;
27810 else
27811 #endif
27812 if (TREE_PUBLIC (decl))
27813 return data_section;
27814 else
27815 return private_data_section;
27819 static void
27820 rs6000_xcoff_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED)
27822 const char *name;
27824 /* Use select_section for private and uninitialized data. */
27825 if (!TREE_PUBLIC (decl)
27826 || DECL_COMMON (decl)
27827 || DECL_INITIAL (decl) == NULL_TREE
27828 || DECL_INITIAL (decl) == error_mark_node
27829 || (flag_zero_initialized_in_bss
27830 && initializer_zerop (DECL_INITIAL (decl))))
27831 return;
27833 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
27834 name = (*targetm.strip_name_encoding) (name);
27835 DECL_SECTION_NAME (decl) = build_string (strlen (name), name);
27838 /* Select section for constant in constant pool.
27840 On RS/6000, all constants are in the private read-only data area.
27841 However, if this is being placed in the TOC it must be output as a
27842 toc entry. */
27844 static section *
27845 rs6000_xcoff_select_rtx_section (enum machine_mode mode, rtx x,
27846 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
27848 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
27849 return toc_section;
27850 else
27851 return read_only_private_data_section;
27854 /* Remove any trailing [DS] or the like from the symbol name. */
27856 static const char *
27857 rs6000_xcoff_strip_name_encoding (const char *name)
27859 size_t len;
27860 if (*name == '*')
27861 name++;
27862 len = strlen (name);
27863 if (name[len - 1] == ']')
27864 return ggc_alloc_string (name, len - 4);
27865 else
27866 return name;
27869 /* Section attributes. AIX is always PIC. */
27871 static unsigned int
27872 rs6000_xcoff_section_type_flags (tree decl, const char *name, int reloc)
27874 unsigned int align;
27875 unsigned int flags = default_section_type_flags (decl, name, reloc);
27877 /* Align to at least UNIT size. */
27878 if ((flags & SECTION_CODE) != 0 || !decl || !DECL_P (decl))
27879 align = MIN_UNITS_PER_WORD;
27880 else
27881 /* Increase alignment of large objects if not already stricter. */
27882 align = MAX ((DECL_ALIGN (decl) / BITS_PER_UNIT),
27883 int_size_in_bytes (TREE_TYPE (decl)) > MIN_UNITS_PER_WORD
27884 ? UNITS_PER_FP_WORD : MIN_UNITS_PER_WORD);
27886 return flags | (exact_log2 (align) & SECTION_ENTSIZE);
27889 /* Output at beginning of assembler file.
27891 Initialize the section names for the RS/6000 at this point.
27893 Specify filename, including full path, to assembler.
27895 We want to go into the TOC section so at least one .toc will be emitted.
27896 Also, in order to output proper .bs/.es pairs, we need at least one static
27897 [RW] section emitted.
27899 Finally, declare mcount when profiling to make the assembler happy. */
27901 static void
27902 rs6000_xcoff_file_start (void)
27904 rs6000_gen_section_name (&xcoff_bss_section_name,
27905 main_input_filename, ".bss_");
27906 rs6000_gen_section_name (&xcoff_private_data_section_name,
27907 main_input_filename, ".rw_");
27908 rs6000_gen_section_name (&xcoff_read_only_section_name,
27909 main_input_filename, ".ro_");
27910 rs6000_gen_section_name (&xcoff_tls_data_section_name,
27911 main_input_filename, ".tls_");
27912 rs6000_gen_section_name (&xcoff_tbss_section_name,
27913 main_input_filename, ".tbss_[UL]");
27915 fputs ("\t.file\t", asm_out_file);
27916 output_quoted_string (asm_out_file, main_input_filename);
27917 fputc ('\n', asm_out_file);
27918 if (write_symbols != NO_DEBUG)
27919 switch_to_section (private_data_section);
27920 switch_to_section (text_section);
27921 if (profile_flag)
27922 fprintf (asm_out_file, "\t.extern %s\n", RS6000_MCOUNT);
27923 rs6000_file_start ();
27926 /* Output at end of assembler file.
27927 On the RS/6000, referencing data should automatically pull in text. */
27929 static void
27930 rs6000_xcoff_file_end (void)
27932 switch_to_section (text_section);
27933 fputs ("_section_.text:\n", asm_out_file);
27934 switch_to_section (data_section);
27935 fputs (TARGET_32BIT
27936 ? "\t.long _section_.text\n" : "\t.llong _section_.text\n",
27937 asm_out_file);
27940 #ifdef HAVE_AS_TLS
27941 static void
27942 rs6000_xcoff_encode_section_info (tree decl, rtx rtl, int first)
27944 rtx symbol;
27945 int flags;
27947 default_encode_section_info (decl, rtl, first);
27949 /* Careful not to prod global register variables. */
27950 if (!MEM_P (rtl))
27951 return;
27952 symbol = XEXP (rtl, 0);
27953 if (GET_CODE (symbol) != SYMBOL_REF)
27954 return;
27956 flags = SYMBOL_REF_FLAGS (symbol);
27958 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
27959 flags &= ~SYMBOL_FLAG_HAS_BLOCK_INFO;
27961 SYMBOL_REF_FLAGS (symbol) = flags;
27963 #endif /* HAVE_AS_TLS */
27964 #endif /* TARGET_XCOFF */
27966 /* Compute a (partial) cost for rtx X. Return true if the complete
27967 cost has been computed, and false if subexpressions should be
27968 scanned. In either case, *TOTAL contains the cost result. */
27970 static bool
27971 rs6000_rtx_costs (rtx x, int code, int outer_code, int opno ATTRIBUTE_UNUSED,
27972 int *total, bool speed)
27974 enum machine_mode mode = GET_MODE (x);
27976 switch (code)
27978 /* On the RS/6000, if it is valid in the insn, it is free. */
27979 case CONST_INT:
27980 if (((outer_code == SET
27981 || outer_code == PLUS
27982 || outer_code == MINUS)
27983 && (satisfies_constraint_I (x)
27984 || satisfies_constraint_L (x)))
27985 || (outer_code == AND
27986 && (satisfies_constraint_K (x)
27987 || (mode == SImode
27988 ? satisfies_constraint_L (x)
27989 : satisfies_constraint_J (x))
27990 || mask_operand (x, mode)
27991 || (mode == DImode
27992 && mask64_operand (x, DImode))))
27993 || ((outer_code == IOR || outer_code == XOR)
27994 && (satisfies_constraint_K (x)
27995 || (mode == SImode
27996 ? satisfies_constraint_L (x)
27997 : satisfies_constraint_J (x))))
27998 || outer_code == ASHIFT
27999 || outer_code == ASHIFTRT
28000 || outer_code == LSHIFTRT
28001 || outer_code == ROTATE
28002 || outer_code == ROTATERT
28003 || outer_code == ZERO_EXTRACT
28004 || (outer_code == MULT
28005 && satisfies_constraint_I (x))
28006 || ((outer_code == DIV || outer_code == UDIV
28007 || outer_code == MOD || outer_code == UMOD)
28008 && exact_log2 (INTVAL (x)) >= 0)
28009 || (outer_code == COMPARE
28010 && (satisfies_constraint_I (x)
28011 || satisfies_constraint_K (x)))
28012 || ((outer_code == EQ || outer_code == NE)
28013 && (satisfies_constraint_I (x)
28014 || satisfies_constraint_K (x)
28015 || (mode == SImode
28016 ? satisfies_constraint_L (x)
28017 : satisfies_constraint_J (x))))
28018 || (outer_code == GTU
28019 && satisfies_constraint_I (x))
28020 || (outer_code == LTU
28021 && satisfies_constraint_P (x)))
28023 *total = 0;
28024 return true;
28026 else if ((outer_code == PLUS
28027 && reg_or_add_cint_operand (x, VOIDmode))
28028 || (outer_code == MINUS
28029 && reg_or_sub_cint_operand (x, VOIDmode))
28030 || ((outer_code == SET
28031 || outer_code == IOR
28032 || outer_code == XOR)
28033 && (INTVAL (x)
28034 & ~ (unsigned HOST_WIDE_INT) 0xffffffff) == 0))
28036 *total = COSTS_N_INSNS (1);
28037 return true;
28039 /* FALLTHRU */
28041 case CONST_DOUBLE:
28042 case CONST:
28043 case HIGH:
28044 case SYMBOL_REF:
28045 case MEM:
28046 /* When optimizing for size, MEM should be slightly more expensive
28047 than generating address, e.g., (plus (reg) (const)).
28048 L1 cache latency is about two instructions. */
28049 *total = !speed ? COSTS_N_INSNS (1) + 1 : COSTS_N_INSNS (2);
28050 return true;
28052 case LABEL_REF:
28053 *total = 0;
28054 return true;
28056 case PLUS:
28057 case MINUS:
28058 if (FLOAT_MODE_P (mode))
28059 *total = rs6000_cost->fp;
28060 else
28061 *total = COSTS_N_INSNS (1);
28062 return false;
28064 case MULT:
28065 if (GET_CODE (XEXP (x, 1)) == CONST_INT
28066 && satisfies_constraint_I (XEXP (x, 1)))
28068 if (INTVAL (XEXP (x, 1)) >= -256
28069 && INTVAL (XEXP (x, 1)) <= 255)
28070 *total = rs6000_cost->mulsi_const9;
28071 else
28072 *total = rs6000_cost->mulsi_const;
28074 else if (mode == SFmode)
28075 *total = rs6000_cost->fp;
28076 else if (FLOAT_MODE_P (mode))
28077 *total = rs6000_cost->dmul;
28078 else if (mode == DImode)
28079 *total = rs6000_cost->muldi;
28080 else
28081 *total = rs6000_cost->mulsi;
28082 return false;
28084 case FMA:
28085 if (mode == SFmode)
28086 *total = rs6000_cost->fp;
28087 else
28088 *total = rs6000_cost->dmul;
28089 break;
28091 case DIV:
28092 case MOD:
28093 if (FLOAT_MODE_P (mode))
28095 *total = mode == DFmode ? rs6000_cost->ddiv
28096 : rs6000_cost->sdiv;
28097 return false;
28099 /* FALLTHRU */
28101 case UDIV:
28102 case UMOD:
28103 if (GET_CODE (XEXP (x, 1)) == CONST_INT
28104 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0)
28106 if (code == DIV || code == MOD)
28107 /* Shift, addze */
28108 *total = COSTS_N_INSNS (2);
28109 else
28110 /* Shift */
28111 *total = COSTS_N_INSNS (1);
28113 else
28115 if (GET_MODE (XEXP (x, 1)) == DImode)
28116 *total = rs6000_cost->divdi;
28117 else
28118 *total = rs6000_cost->divsi;
28120 /* Add in shift and subtract for MOD. */
28121 if (code == MOD || code == UMOD)
28122 *total += COSTS_N_INSNS (2);
28123 return false;
28125 case CTZ:
28126 case FFS:
28127 *total = COSTS_N_INSNS (4);
28128 return false;
28130 case POPCOUNT:
28131 *total = COSTS_N_INSNS (TARGET_POPCNTD ? 1 : 6);
28132 return false;
28134 case PARITY:
28135 *total = COSTS_N_INSNS (TARGET_CMPB ? 2 : 6);
28136 return false;
28138 case NOT:
28139 if (outer_code == AND || outer_code == IOR || outer_code == XOR)
28141 *total = 0;
28142 return false;
28144 /* FALLTHRU */
28146 case AND:
28147 case CLZ:
28148 case IOR:
28149 case XOR:
28150 case ZERO_EXTRACT:
28151 *total = COSTS_N_INSNS (1);
28152 return false;
28154 case ASHIFT:
28155 case ASHIFTRT:
28156 case LSHIFTRT:
28157 case ROTATE:
28158 case ROTATERT:
28159 /* Handle mul_highpart. */
28160 if (outer_code == TRUNCATE
28161 && GET_CODE (XEXP (x, 0)) == MULT)
28163 if (mode == DImode)
28164 *total = rs6000_cost->muldi;
28165 else
28166 *total = rs6000_cost->mulsi;
28167 return true;
28169 else if (outer_code == AND)
28170 *total = 0;
28171 else
28172 *total = COSTS_N_INSNS (1);
28173 return false;
28175 case SIGN_EXTEND:
28176 case ZERO_EXTEND:
28177 if (GET_CODE (XEXP (x, 0)) == MEM)
28178 *total = 0;
28179 else
28180 *total = COSTS_N_INSNS (1);
28181 return false;
28183 case COMPARE:
28184 case NEG:
28185 case ABS:
28186 if (!FLOAT_MODE_P (mode))
28188 *total = COSTS_N_INSNS (1);
28189 return false;
28191 /* FALLTHRU */
28193 case FLOAT:
28194 case UNSIGNED_FLOAT:
28195 case FIX:
28196 case UNSIGNED_FIX:
28197 case FLOAT_TRUNCATE:
28198 *total = rs6000_cost->fp;
28199 return false;
28201 case FLOAT_EXTEND:
28202 if (mode == DFmode)
28203 *total = 0;
28204 else
28205 *total = rs6000_cost->fp;
28206 return false;
28208 case UNSPEC:
28209 switch (XINT (x, 1))
28211 case UNSPEC_FRSP:
28212 *total = rs6000_cost->fp;
28213 return true;
28215 default:
28216 break;
28218 break;
28220 case CALL:
28221 case IF_THEN_ELSE:
28222 if (!speed)
28224 *total = COSTS_N_INSNS (1);
28225 return true;
28227 else if (FLOAT_MODE_P (mode)
28228 && TARGET_PPC_GFXOPT && TARGET_HARD_FLOAT && TARGET_FPRS)
28230 *total = rs6000_cost->fp;
28231 return false;
28233 break;
28235 case EQ:
28236 case GTU:
28237 case LTU:
28238 /* Carry bit requires mode == Pmode.
28239 NEG or PLUS already counted so only add one. */
28240 if (mode == Pmode
28241 && (outer_code == NEG || outer_code == PLUS))
28243 *total = COSTS_N_INSNS (1);
28244 return true;
28246 if (outer_code == SET)
28248 if (XEXP (x, 1) == const0_rtx)
28250 if (TARGET_ISEL && !TARGET_MFCRF)
28251 *total = COSTS_N_INSNS (8);
28252 else
28253 *total = COSTS_N_INSNS (2);
28254 return true;
28256 else if (mode == Pmode)
28258 *total = COSTS_N_INSNS (3);
28259 return false;
28262 /* FALLTHRU */
28264 case GT:
28265 case LT:
28266 case UNORDERED:
28267 if (outer_code == SET && (XEXP (x, 1) == const0_rtx))
28269 if (TARGET_ISEL && !TARGET_MFCRF)
28270 *total = COSTS_N_INSNS (8);
28271 else
28272 *total = COSTS_N_INSNS (2);
28273 return true;
28275 /* CC COMPARE. */
28276 if (outer_code == COMPARE)
28278 *total = 0;
28279 return true;
28281 break;
28283 default:
28284 break;
28287 return false;
28290 /* Debug form of r6000_rtx_costs that is selected if -mdebug=cost. */
28292 static bool
28293 rs6000_debug_rtx_costs (rtx x, int code, int outer_code, int opno, int *total,
28294 bool speed)
28296 bool ret = rs6000_rtx_costs (x, code, outer_code, opno, total, speed);
28298 fprintf (stderr,
28299 "\nrs6000_rtx_costs, return = %s, code = %s, outer_code = %s, "
28300 "opno = %d, total = %d, speed = %s, x:\n",
28301 ret ? "complete" : "scan inner",
28302 GET_RTX_NAME (code),
28303 GET_RTX_NAME (outer_code),
28304 opno,
28305 *total,
28306 speed ? "true" : "false");
28308 debug_rtx (x);
28310 return ret;
28313 /* Debug form of ADDRESS_COST that is selected if -mdebug=cost. */
28315 static int
28316 rs6000_debug_address_cost (rtx x, enum machine_mode mode,
28317 addr_space_t as, bool speed)
28319 int ret = TARGET_ADDRESS_COST (x, mode, as, speed);
28321 fprintf (stderr, "\nrs6000_address_cost, return = %d, speed = %s, x:\n",
28322 ret, speed ? "true" : "false");
28323 debug_rtx (x);
28325 return ret;
28329 /* A C expression returning the cost of moving data from a register of class
28330 CLASS1 to one of CLASS2. */
28332 static int
28333 rs6000_register_move_cost (enum machine_mode mode,
28334 reg_class_t from, reg_class_t to)
28336 int ret;
28338 if (TARGET_DEBUG_COST)
28339 dbg_cost_ctrl++;
28341 /* Moves from/to GENERAL_REGS. */
28342 if (reg_classes_intersect_p (to, GENERAL_REGS)
28343 || reg_classes_intersect_p (from, GENERAL_REGS))
28345 reg_class_t rclass = from;
28347 if (! reg_classes_intersect_p (to, GENERAL_REGS))
28348 rclass = to;
28350 if (rclass == FLOAT_REGS || rclass == ALTIVEC_REGS || rclass == VSX_REGS)
28351 ret = (rs6000_memory_move_cost (mode, rclass, false)
28352 + rs6000_memory_move_cost (mode, GENERAL_REGS, false));
28354 /* It's more expensive to move CR_REGS than CR0_REGS because of the
28355 shift. */
28356 else if (rclass == CR_REGS)
28357 ret = 4;
28359 /* For those processors that have slow LR/CTR moves, make them more
28360 expensive than memory in order to bias spills to memory .*/
28361 else if ((rs6000_cpu == PROCESSOR_POWER6
28362 || rs6000_cpu == PROCESSOR_POWER7
28363 || rs6000_cpu == PROCESSOR_POWER8)
28364 && reg_classes_intersect_p (rclass, LINK_OR_CTR_REGS))
28365 ret = 6 * hard_regno_nregs[0][mode];
28367 else
28368 /* A move will cost one instruction per GPR moved. */
28369 ret = 2 * hard_regno_nregs[0][mode];
28372 /* If we have VSX, we can easily move between FPR or Altivec registers. */
28373 else if (VECTOR_MEM_VSX_P (mode)
28374 && reg_classes_intersect_p (to, VSX_REGS)
28375 && reg_classes_intersect_p (from, VSX_REGS))
28376 ret = 2 * hard_regno_nregs[32][mode];
28378 /* Moving between two similar registers is just one instruction. */
28379 else if (reg_classes_intersect_p (to, from))
28380 ret = (mode == TFmode || mode == TDmode) ? 4 : 2;
28382 /* Everything else has to go through GENERAL_REGS. */
28383 else
28384 ret = (rs6000_register_move_cost (mode, GENERAL_REGS, to)
28385 + rs6000_register_move_cost (mode, from, GENERAL_REGS));
28387 if (TARGET_DEBUG_COST)
28389 if (dbg_cost_ctrl == 1)
28390 fprintf (stderr,
28391 "rs6000_register_move_cost:, ret=%d, mode=%s, from=%s, to=%s\n",
28392 ret, GET_MODE_NAME (mode), reg_class_names[from],
28393 reg_class_names[to]);
28394 dbg_cost_ctrl--;
28397 return ret;
28400 /* A C expressions returning the cost of moving data of MODE from a register to
28401 or from memory. */
28403 static int
28404 rs6000_memory_move_cost (enum machine_mode mode, reg_class_t rclass,
28405 bool in ATTRIBUTE_UNUSED)
28407 int ret;
28409 if (TARGET_DEBUG_COST)
28410 dbg_cost_ctrl++;
28412 if (reg_classes_intersect_p (rclass, GENERAL_REGS))
28413 ret = 4 * hard_regno_nregs[0][mode];
28414 else if ((reg_classes_intersect_p (rclass, FLOAT_REGS)
28415 || reg_classes_intersect_p (rclass, VSX_REGS)))
28416 ret = 4 * hard_regno_nregs[32][mode];
28417 else if (reg_classes_intersect_p (rclass, ALTIVEC_REGS))
28418 ret = 4 * hard_regno_nregs[FIRST_ALTIVEC_REGNO][mode];
28419 else
28420 ret = 4 + rs6000_register_move_cost (mode, rclass, GENERAL_REGS);
28422 if (TARGET_DEBUG_COST)
28424 if (dbg_cost_ctrl == 1)
28425 fprintf (stderr,
28426 "rs6000_memory_move_cost: ret=%d, mode=%s, rclass=%s, in=%d\n",
28427 ret, GET_MODE_NAME (mode), reg_class_names[rclass], in);
28428 dbg_cost_ctrl--;
28431 return ret;
28434 /* Returns a code for a target-specific builtin that implements
28435 reciprocal of the function, or NULL_TREE if not available. */
28437 static tree
28438 rs6000_builtin_reciprocal (unsigned int fn, bool md_fn,
28439 bool sqrt ATTRIBUTE_UNUSED)
28441 if (optimize_insn_for_size_p ())
28442 return NULL_TREE;
28444 if (md_fn)
28445 switch (fn)
28447 case VSX_BUILTIN_XVSQRTDP:
28448 if (!RS6000_RECIP_AUTO_RSQRTE_P (V2DFmode))
28449 return NULL_TREE;
28451 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_2DF];
28453 case VSX_BUILTIN_XVSQRTSP:
28454 if (!RS6000_RECIP_AUTO_RSQRTE_P (V4SFmode))
28455 return NULL_TREE;
28457 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_4SF];
28459 default:
28460 return NULL_TREE;
28463 else
28464 switch (fn)
28466 case BUILT_IN_SQRT:
28467 if (!RS6000_RECIP_AUTO_RSQRTE_P (DFmode))
28468 return NULL_TREE;
28470 return rs6000_builtin_decls[RS6000_BUILTIN_RSQRT];
28472 case BUILT_IN_SQRTF:
28473 if (!RS6000_RECIP_AUTO_RSQRTE_P (SFmode))
28474 return NULL_TREE;
28476 return rs6000_builtin_decls[RS6000_BUILTIN_RSQRTF];
28478 default:
28479 return NULL_TREE;
28483 /* Load up a constant. If the mode is a vector mode, splat the value across
28484 all of the vector elements. */
28486 static rtx
28487 rs6000_load_constant_and_splat (enum machine_mode mode, REAL_VALUE_TYPE dconst)
28489 rtx reg;
28491 if (mode == SFmode || mode == DFmode)
28493 rtx d = CONST_DOUBLE_FROM_REAL_VALUE (dconst, mode);
28494 reg = force_reg (mode, d);
28496 else if (mode == V4SFmode)
28498 rtx d = CONST_DOUBLE_FROM_REAL_VALUE (dconst, SFmode);
28499 rtvec v = gen_rtvec (4, d, d, d, d);
28500 reg = gen_reg_rtx (mode);
28501 rs6000_expand_vector_init (reg, gen_rtx_PARALLEL (mode, v));
28503 else if (mode == V2DFmode)
28505 rtx d = CONST_DOUBLE_FROM_REAL_VALUE (dconst, DFmode);
28506 rtvec v = gen_rtvec (2, d, d);
28507 reg = gen_reg_rtx (mode);
28508 rs6000_expand_vector_init (reg, gen_rtx_PARALLEL (mode, v));
28510 else
28511 gcc_unreachable ();
28513 return reg;
28516 /* Generate an FMA instruction. */
28518 static void
28519 rs6000_emit_madd (rtx target, rtx m1, rtx m2, rtx a)
28521 enum machine_mode mode = GET_MODE (target);
28522 rtx dst;
28524 dst = expand_ternary_op (mode, fma_optab, m1, m2, a, target, 0);
28525 gcc_assert (dst != NULL);
28527 if (dst != target)
28528 emit_move_insn (target, dst);
28531 /* Generate a FMSUB instruction: dst = fma(m1, m2, -a). */
28533 static void
28534 rs6000_emit_msub (rtx target, rtx m1, rtx m2, rtx a)
28536 enum machine_mode mode = GET_MODE (target);
28537 rtx dst;
28539 /* Altivec does not support fms directly;
28540 generate in terms of fma in that case. */
28541 if (optab_handler (fms_optab, mode) != CODE_FOR_nothing)
28542 dst = expand_ternary_op (mode, fms_optab, m1, m2, a, target, 0);
28543 else
28545 a = expand_unop (mode, neg_optab, a, NULL_RTX, 0);
28546 dst = expand_ternary_op (mode, fma_optab, m1, m2, a, target, 0);
28548 gcc_assert (dst != NULL);
28550 if (dst != target)
28551 emit_move_insn (target, dst);
28554 /* Generate a FNMSUB instruction: dst = -fma(m1, m2, -a). */
28556 static void
28557 rs6000_emit_nmsub (rtx dst, rtx m1, rtx m2, rtx a)
28559 enum machine_mode mode = GET_MODE (dst);
28560 rtx r;
28562 /* This is a tad more complicated, since the fnma_optab is for
28563 a different expression: fma(-m1, m2, a), which is the same
28564 thing except in the case of signed zeros.
28566 Fortunately we know that if FMA is supported that FNMSUB is
28567 also supported in the ISA. Just expand it directly. */
28569 gcc_assert (optab_handler (fma_optab, mode) != CODE_FOR_nothing);
28571 r = gen_rtx_NEG (mode, a);
28572 r = gen_rtx_FMA (mode, m1, m2, r);
28573 r = gen_rtx_NEG (mode, r);
28574 emit_insn (gen_rtx_SET (VOIDmode, dst, r));
28577 /* Newton-Raphson approximation of floating point divide DST = N/D. If NOTE_P,
28578 add a reg_note saying that this was a division. Support both scalar and
28579 vector divide. Assumes no trapping math and finite arguments. */
28581 void
28582 rs6000_emit_swdiv (rtx dst, rtx n, rtx d, bool note_p)
28584 enum machine_mode mode = GET_MODE (dst);
28585 rtx one, x0, e0, x1, xprev, eprev, xnext, enext, u, v;
28586 int i;
28588 /* Low precision estimates guarantee 5 bits of accuracy. High
28589 precision estimates guarantee 14 bits of accuracy. SFmode
28590 requires 23 bits of accuracy. DFmode requires 52 bits of
28591 accuracy. Each pass at least doubles the accuracy, leading
28592 to the following. */
28593 int passes = (TARGET_RECIP_PRECISION) ? 1 : 3;
28594 if (mode == DFmode || mode == V2DFmode)
28595 passes++;
28597 enum insn_code code = optab_handler (smul_optab, mode);
28598 insn_gen_fn gen_mul = GEN_FCN (code);
28600 gcc_assert (code != CODE_FOR_nothing);
28602 one = rs6000_load_constant_and_splat (mode, dconst1);
28604 /* x0 = 1./d estimate */
28605 x0 = gen_reg_rtx (mode);
28606 emit_insn (gen_rtx_SET (VOIDmode, x0,
28607 gen_rtx_UNSPEC (mode, gen_rtvec (1, d),
28608 UNSPEC_FRES)));
28610 /* Each iteration but the last calculates x_(i+1) = x_i * (2 - d * x_i). */
28611 if (passes > 1) {
28613 /* e0 = 1. - d * x0 */
28614 e0 = gen_reg_rtx (mode);
28615 rs6000_emit_nmsub (e0, d, x0, one);
28617 /* x1 = x0 + e0 * x0 */
28618 x1 = gen_reg_rtx (mode);
28619 rs6000_emit_madd (x1, e0, x0, x0);
28621 for (i = 0, xprev = x1, eprev = e0; i < passes - 2;
28622 ++i, xprev = xnext, eprev = enext) {
28624 /* enext = eprev * eprev */
28625 enext = gen_reg_rtx (mode);
28626 emit_insn (gen_mul (enext, eprev, eprev));
28628 /* xnext = xprev + enext * xprev */
28629 xnext = gen_reg_rtx (mode);
28630 rs6000_emit_madd (xnext, enext, xprev, xprev);
28633 } else
28634 xprev = x0;
28636 /* The last iteration calculates x_(i+1) = n * x_i * (2 - d * x_i). */
28638 /* u = n * xprev */
28639 u = gen_reg_rtx (mode);
28640 emit_insn (gen_mul (u, n, xprev));
28642 /* v = n - (d * u) */
28643 v = gen_reg_rtx (mode);
28644 rs6000_emit_nmsub (v, d, u, n);
28646 /* dst = (v * xprev) + u */
28647 rs6000_emit_madd (dst, v, xprev, u);
28649 if (note_p)
28650 add_reg_note (get_last_insn (), REG_EQUAL, gen_rtx_DIV (mode, n, d));
28653 /* Newton-Raphson approximation of single/double-precision floating point
28654 rsqrt. Assumes no trapping math and finite arguments. */
28656 void
28657 rs6000_emit_swrsqrt (rtx dst, rtx src)
28659 enum machine_mode mode = GET_MODE (src);
28660 rtx x0 = gen_reg_rtx (mode);
28661 rtx y = gen_reg_rtx (mode);
28663 /* Low precision estimates guarantee 5 bits of accuracy. High
28664 precision estimates guarantee 14 bits of accuracy. SFmode
28665 requires 23 bits of accuracy. DFmode requires 52 bits of
28666 accuracy. Each pass at least doubles the accuracy, leading
28667 to the following. */
28668 int passes = (TARGET_RECIP_PRECISION) ? 1 : 3;
28669 if (mode == DFmode || mode == V2DFmode)
28670 passes++;
28672 REAL_VALUE_TYPE dconst3_2;
28673 int i;
28674 rtx halfthree;
28675 enum insn_code code = optab_handler (smul_optab, mode);
28676 insn_gen_fn gen_mul = GEN_FCN (code);
28678 gcc_assert (code != CODE_FOR_nothing);
28680 /* Load up the constant 1.5 either as a scalar, or as a vector. */
28681 real_from_integer (&dconst3_2, VOIDmode, 3, 0, 0);
28682 SET_REAL_EXP (&dconst3_2, REAL_EXP (&dconst3_2) - 1);
28684 halfthree = rs6000_load_constant_and_splat (mode, dconst3_2);
28686 /* x0 = rsqrt estimate */
28687 emit_insn (gen_rtx_SET (VOIDmode, x0,
28688 gen_rtx_UNSPEC (mode, gen_rtvec (1, src),
28689 UNSPEC_RSQRT)));
28691 /* y = 0.5 * src = 1.5 * src - src -> fewer constants */
28692 rs6000_emit_msub (y, src, halfthree, src);
28694 for (i = 0; i < passes; i++)
28696 rtx x1 = gen_reg_rtx (mode);
28697 rtx u = gen_reg_rtx (mode);
28698 rtx v = gen_reg_rtx (mode);
28700 /* x1 = x0 * (1.5 - y * (x0 * x0)) */
28701 emit_insn (gen_mul (u, x0, x0));
28702 rs6000_emit_nmsub (v, y, u, halfthree);
28703 emit_insn (gen_mul (x1, x0, v));
28704 x0 = x1;
28707 emit_move_insn (dst, x0);
28708 return;
28711 /* Emit popcount intrinsic on TARGET_POPCNTB (Power5) and TARGET_POPCNTD
28712 (Power7) targets. DST is the target, and SRC is the argument operand. */
28714 void
28715 rs6000_emit_popcount (rtx dst, rtx src)
28717 enum machine_mode mode = GET_MODE (dst);
28718 rtx tmp1, tmp2;
28720 /* Use the PPC ISA 2.06 popcnt{w,d} instruction if we can. */
28721 if (TARGET_POPCNTD)
28723 if (mode == SImode)
28724 emit_insn (gen_popcntdsi2 (dst, src));
28725 else
28726 emit_insn (gen_popcntddi2 (dst, src));
28727 return;
28730 tmp1 = gen_reg_rtx (mode);
28732 if (mode == SImode)
28734 emit_insn (gen_popcntbsi2 (tmp1, src));
28735 tmp2 = expand_mult (SImode, tmp1, GEN_INT (0x01010101),
28736 NULL_RTX, 0);
28737 tmp2 = force_reg (SImode, tmp2);
28738 emit_insn (gen_lshrsi3 (dst, tmp2, GEN_INT (24)));
28740 else
28742 emit_insn (gen_popcntbdi2 (tmp1, src));
28743 tmp2 = expand_mult (DImode, tmp1,
28744 GEN_INT ((HOST_WIDE_INT)
28745 0x01010101 << 32 | 0x01010101),
28746 NULL_RTX, 0);
28747 tmp2 = force_reg (DImode, tmp2);
28748 emit_insn (gen_lshrdi3 (dst, tmp2, GEN_INT (56)));
28753 /* Emit parity intrinsic on TARGET_POPCNTB targets. DST is the
28754 target, and SRC is the argument operand. */
28756 void
28757 rs6000_emit_parity (rtx dst, rtx src)
28759 enum machine_mode mode = GET_MODE (dst);
28760 rtx tmp;
28762 tmp = gen_reg_rtx (mode);
28764 /* Use the PPC ISA 2.05 prtyw/prtyd instruction if we can. */
28765 if (TARGET_CMPB)
28767 if (mode == SImode)
28769 emit_insn (gen_popcntbsi2 (tmp, src));
28770 emit_insn (gen_paritysi2_cmpb (dst, tmp));
28772 else
28774 emit_insn (gen_popcntbdi2 (tmp, src));
28775 emit_insn (gen_paritydi2_cmpb (dst, tmp));
28777 return;
28780 if (mode == SImode)
28782 /* Is mult+shift >= shift+xor+shift+xor? */
28783 if (rs6000_cost->mulsi_const >= COSTS_N_INSNS (3))
28785 rtx tmp1, tmp2, tmp3, tmp4;
28787 tmp1 = gen_reg_rtx (SImode);
28788 emit_insn (gen_popcntbsi2 (tmp1, src));
28790 tmp2 = gen_reg_rtx (SImode);
28791 emit_insn (gen_lshrsi3 (tmp2, tmp1, GEN_INT (16)));
28792 tmp3 = gen_reg_rtx (SImode);
28793 emit_insn (gen_xorsi3 (tmp3, tmp1, tmp2));
28795 tmp4 = gen_reg_rtx (SImode);
28796 emit_insn (gen_lshrsi3 (tmp4, tmp3, GEN_INT (8)));
28797 emit_insn (gen_xorsi3 (tmp, tmp3, tmp4));
28799 else
28800 rs6000_emit_popcount (tmp, src);
28801 emit_insn (gen_andsi3 (dst, tmp, const1_rtx));
28803 else
28805 /* Is mult+shift >= shift+xor+shift+xor+shift+xor? */
28806 if (rs6000_cost->muldi >= COSTS_N_INSNS (5))
28808 rtx tmp1, tmp2, tmp3, tmp4, tmp5, tmp6;
28810 tmp1 = gen_reg_rtx (DImode);
28811 emit_insn (gen_popcntbdi2 (tmp1, src));
28813 tmp2 = gen_reg_rtx (DImode);
28814 emit_insn (gen_lshrdi3 (tmp2, tmp1, GEN_INT (32)));
28815 tmp3 = gen_reg_rtx (DImode);
28816 emit_insn (gen_xordi3 (tmp3, tmp1, tmp2));
28818 tmp4 = gen_reg_rtx (DImode);
28819 emit_insn (gen_lshrdi3 (tmp4, tmp3, GEN_INT (16)));
28820 tmp5 = gen_reg_rtx (DImode);
28821 emit_insn (gen_xordi3 (tmp5, tmp3, tmp4));
28823 tmp6 = gen_reg_rtx (DImode);
28824 emit_insn (gen_lshrdi3 (tmp6, tmp5, GEN_INT (8)));
28825 emit_insn (gen_xordi3 (tmp, tmp5, tmp6));
28827 else
28828 rs6000_emit_popcount (tmp, src);
28829 emit_insn (gen_anddi3 (dst, tmp, const1_rtx));
28833 /* Expand an Altivec constant permutation for little endian mode.
28834 There are two issues: First, the two input operands must be
28835 swapped so that together they form a double-wide array in LE
28836 order. Second, the vperm instruction has surprising behavior
28837 in LE mode: it interprets the elements of the source vectors
28838 in BE mode ("left to right") and interprets the elements of
28839 the destination vector in LE mode ("right to left"). To
28840 correct for this, we must subtract each element of the permute
28841 control vector from 31.
28843 For example, suppose we want to concatenate vr10 = {0, 1, 2, 3}
28844 with vr11 = {4, 5, 6, 7} and extract {0, 2, 4, 6} using a vperm.
28845 We place {0,1,2,3,8,9,10,11,16,17,18,19,24,25,26,27} in vr12 to
28846 serve as the permute control vector. Then, in BE mode,
28848 vperm 9,10,11,12
28850 places the desired result in vr9. However, in LE mode the
28851 vector contents will be
28853 vr10 = 00000003 00000002 00000001 00000000
28854 vr11 = 00000007 00000006 00000005 00000004
28856 The result of the vperm using the same permute control vector is
28858 vr9 = 05000000 07000000 01000000 03000000
28860 That is, the leftmost 4 bytes of vr10 are interpreted as the
28861 source for the rightmost 4 bytes of vr9, and so on.
28863 If we change the permute control vector to
28865 vr12 = {31,20,29,28,23,22,21,20,15,14,13,12,7,6,5,4}
28867 and issue
28869 vperm 9,11,10,12
28871 we get the desired
28873 vr9 = 00000006 00000004 00000002 00000000. */
28875 void
28876 altivec_expand_vec_perm_const_le (rtx operands[4])
28878 unsigned int i;
28879 rtx perm[16];
28880 rtx constv, unspec;
28881 rtx target = operands[0];
28882 rtx op0 = operands[1];
28883 rtx op1 = operands[2];
28884 rtx sel = operands[3];
28886 /* Unpack and adjust the constant selector. */
28887 for (i = 0; i < 16; ++i)
28889 rtx e = XVECEXP (sel, 0, i);
28890 unsigned int elt = 31 - (INTVAL (e) & 31);
28891 perm[i] = GEN_INT (elt);
28894 /* Expand to a permute, swapping the inputs and using the
28895 adjusted selector. */
28896 if (!REG_P (op0))
28897 op0 = force_reg (V16QImode, op0);
28898 if (!REG_P (op1))
28899 op1 = force_reg (V16QImode, op1);
28901 constv = gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, perm));
28902 constv = force_reg (V16QImode, constv);
28903 unspec = gen_rtx_UNSPEC (V16QImode, gen_rtvec (3, op1, op0, constv),
28904 UNSPEC_VPERM);
28905 if (!REG_P (target))
28907 rtx tmp = gen_reg_rtx (V16QImode);
28908 emit_move_insn (tmp, unspec);
28909 unspec = tmp;
28912 emit_move_insn (target, unspec);
28915 /* Similarly to altivec_expand_vec_perm_const_le, we must adjust the
28916 permute control vector. But here it's not a constant, so we must
28917 generate a vector splat/subtract to do the adjustment. */
28919 void
28920 altivec_expand_vec_perm_le (rtx operands[4])
28922 rtx splat, unspec;
28923 rtx target = operands[0];
28924 rtx op0 = operands[1];
28925 rtx op1 = operands[2];
28926 rtx sel = operands[3];
28927 rtx tmp = target;
28929 /* Get everything in regs so the pattern matches. */
28930 if (!REG_P (op0))
28931 op0 = force_reg (V16QImode, op0);
28932 if (!REG_P (op1))
28933 op1 = force_reg (V16QImode, op1);
28934 if (!REG_P (sel))
28935 sel = force_reg (V16QImode, sel);
28936 if (!REG_P (target))
28937 tmp = gen_reg_rtx (V16QImode);
28939 /* SEL = splat(31) - SEL. */
28940 /* We want to subtract from 31, but we can't vspltisb 31 since
28941 it's out of range. -1 works as well because only the low-order
28942 five bits of the permute control vector elements are used. */
28943 splat = gen_rtx_VEC_DUPLICATE (V16QImode,
28944 gen_rtx_CONST_INT (QImode, -1));
28945 emit_move_insn (tmp, splat);
28946 sel = gen_rtx_MINUS (V16QImode, tmp, sel);
28947 emit_move_insn (tmp, sel);
28949 /* Permute with operands reversed and adjusted selector. */
28950 unspec = gen_rtx_UNSPEC (V16QImode, gen_rtvec (3, op1, op0, tmp),
28951 UNSPEC_VPERM);
28953 /* Copy into target, possibly by way of a register. */
28954 if (!REG_P (target))
28956 emit_move_insn (tmp, unspec);
28957 unspec = tmp;
28960 emit_move_insn (target, unspec);
28963 /* Expand an Altivec constant permutation. Return true if we match
28964 an efficient implementation; false to fall back to VPERM. */
28966 bool
28967 altivec_expand_vec_perm_const (rtx operands[4])
28969 struct altivec_perm_insn {
28970 HOST_WIDE_INT mask;
28971 enum insn_code impl;
28972 unsigned char perm[16];
28974 static const struct altivec_perm_insn patterns[] = {
28975 { OPTION_MASK_ALTIVEC, CODE_FOR_altivec_vpkuhum,
28976 { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } },
28977 { OPTION_MASK_ALTIVEC, CODE_FOR_altivec_vpkuwum,
28978 { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } },
28979 { OPTION_MASK_ALTIVEC,
28980 BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrghb : CODE_FOR_altivec_vmrglb,
28981 { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 } },
28982 { OPTION_MASK_ALTIVEC,
28983 BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrghh : CODE_FOR_altivec_vmrglh,
28984 { 0, 1, 16, 17, 2, 3, 18, 19, 4, 5, 20, 21, 6, 7, 22, 23 } },
28985 { OPTION_MASK_ALTIVEC,
28986 BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrghw : CODE_FOR_altivec_vmrglw,
28987 { 0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23 } },
28988 { OPTION_MASK_ALTIVEC,
28989 BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrglb : CODE_FOR_altivec_vmrghb,
28990 { 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } },
28991 { OPTION_MASK_ALTIVEC,
28992 BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrglh : CODE_FOR_altivec_vmrghh,
28993 { 8, 9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } },
28994 { OPTION_MASK_ALTIVEC,
28995 BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrglw : CODE_FOR_altivec_vmrghw,
28996 { 8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } },
28997 { OPTION_MASK_P8_VECTOR, CODE_FOR_p8_vmrgew,
28998 { 0, 1, 2, 3, 16, 17, 18, 19, 8, 9, 10, 11, 24, 25, 26, 27 } },
28999 { OPTION_MASK_P8_VECTOR, CODE_FOR_p8_vmrgow,
29000 { 4, 5, 6, 7, 20, 21, 22, 23, 12, 13, 14, 15, 28, 29, 30, 31 } }
29003 unsigned int i, j, elt, which;
29004 unsigned char perm[16];
29005 rtx target, op0, op1, sel, x;
29006 bool one_vec;
29008 target = operands[0];
29009 op0 = operands[1];
29010 op1 = operands[2];
29011 sel = operands[3];
29013 /* Unpack the constant selector. */
29014 for (i = which = 0; i < 16; ++i)
29016 rtx e = XVECEXP (sel, 0, i);
29017 elt = INTVAL (e) & 31;
29018 which |= (elt < 16 ? 1 : 2);
29019 perm[i] = elt;
29022 /* Simplify the constant selector based on operands. */
29023 switch (which)
29025 default:
29026 gcc_unreachable ();
29028 case 3:
29029 one_vec = false;
29030 if (!rtx_equal_p (op0, op1))
29031 break;
29032 /* FALLTHRU */
29034 case 2:
29035 for (i = 0; i < 16; ++i)
29036 perm[i] &= 15;
29037 op0 = op1;
29038 one_vec = true;
29039 break;
29041 case 1:
29042 op1 = op0;
29043 one_vec = true;
29044 break;
29047 /* Look for splat patterns. */
29048 if (one_vec)
29050 elt = perm[0];
29052 for (i = 0; i < 16; ++i)
29053 if (perm[i] != elt)
29054 break;
29055 if (i == 16)
29057 emit_insn (gen_altivec_vspltb (target, op0, GEN_INT (elt)));
29058 return true;
29061 if (elt % 2 == 0)
29063 for (i = 0; i < 16; i += 2)
29064 if (perm[i] != elt || perm[i + 1] != elt + 1)
29065 break;
29066 if (i == 16)
29068 int field = BYTES_BIG_ENDIAN ? elt / 2 : 7 - elt / 2;
29069 x = gen_reg_rtx (V8HImode);
29070 emit_insn (gen_altivec_vsplth (x, gen_lowpart (V8HImode, op0),
29071 GEN_INT (field)));
29072 emit_move_insn (target, gen_lowpart (V16QImode, x));
29073 return true;
29077 if (elt % 4 == 0)
29079 for (i = 0; i < 16; i += 4)
29080 if (perm[i] != elt
29081 || perm[i + 1] != elt + 1
29082 || perm[i + 2] != elt + 2
29083 || perm[i + 3] != elt + 3)
29084 break;
29085 if (i == 16)
29087 int field = BYTES_BIG_ENDIAN ? elt / 4 : 3 - elt / 4;
29088 x = gen_reg_rtx (V4SImode);
29089 emit_insn (gen_altivec_vspltw (x, gen_lowpart (V4SImode, op0),
29090 GEN_INT (field)));
29091 emit_move_insn (target, gen_lowpart (V16QImode, x));
29092 return true;
29097 /* Look for merge and pack patterns. */
29098 for (j = 0; j < ARRAY_SIZE (patterns); ++j)
29100 bool swapped;
29102 if ((patterns[j].mask & rs6000_isa_flags) == 0)
29103 continue;
29105 elt = patterns[j].perm[0];
29106 if (perm[0] == elt)
29107 swapped = false;
29108 else if (perm[0] == elt + 16)
29109 swapped = true;
29110 else
29111 continue;
29112 for (i = 1; i < 16; ++i)
29114 elt = patterns[j].perm[i];
29115 if (swapped)
29116 elt = (elt >= 16 ? elt - 16 : elt + 16);
29117 else if (one_vec && elt >= 16)
29118 elt -= 16;
29119 if (perm[i] != elt)
29120 break;
29122 if (i == 16)
29124 enum insn_code icode = patterns[j].impl;
29125 enum machine_mode omode = insn_data[icode].operand[0].mode;
29126 enum machine_mode imode = insn_data[icode].operand[1].mode;
29128 /* For little-endian, don't use vpkuwum and vpkuhum if the
29129 underlying vector type is not V4SI and V8HI, respectively.
29130 For example, using vpkuwum with a V8HI picks up the even
29131 halfwords (BE numbering) when the even halfwords (LE
29132 numbering) are what we need. */
29133 if (!BYTES_BIG_ENDIAN
29134 && icode == CODE_FOR_altivec_vpkuwum
29135 && ((GET_CODE (op0) == REG
29136 && GET_MODE (op0) != V4SImode)
29137 || (GET_CODE (op0) == SUBREG
29138 && GET_MODE (XEXP (op0, 0)) != V4SImode)))
29139 continue;
29140 if (!BYTES_BIG_ENDIAN
29141 && icode == CODE_FOR_altivec_vpkuhum
29142 && ((GET_CODE (op0) == REG
29143 && GET_MODE (op0) != V8HImode)
29144 || (GET_CODE (op0) == SUBREG
29145 && GET_MODE (XEXP (op0, 0)) != V8HImode)))
29146 continue;
29148 /* For little-endian, the two input operands must be swapped
29149 (or swapped back) to ensure proper right-to-left numbering
29150 from 0 to 2N-1. */
29151 if (swapped ^ !BYTES_BIG_ENDIAN)
29152 x = op0, op0 = op1, op1 = x;
29153 if (imode != V16QImode)
29155 op0 = gen_lowpart (imode, op0);
29156 op1 = gen_lowpart (imode, op1);
29158 if (omode == V16QImode)
29159 x = target;
29160 else
29161 x = gen_reg_rtx (omode);
29162 emit_insn (GEN_FCN (icode) (x, op0, op1));
29163 if (omode != V16QImode)
29164 emit_move_insn (target, gen_lowpart (V16QImode, x));
29165 return true;
29169 if (!BYTES_BIG_ENDIAN)
29171 altivec_expand_vec_perm_const_le (operands);
29172 return true;
29175 return false;
29178 /* Expand a Paired Single, VSX Permute Doubleword, or SPE constant permutation.
29179 Return true if we match an efficient implementation. */
29181 static bool
29182 rs6000_expand_vec_perm_const_1 (rtx target, rtx op0, rtx op1,
29183 unsigned char perm0, unsigned char perm1)
29185 rtx x;
29187 /* If both selectors come from the same operand, fold to single op. */
29188 if ((perm0 & 2) == (perm1 & 2))
29190 if (perm0 & 2)
29191 op0 = op1;
29192 else
29193 op1 = op0;
29195 /* If both operands are equal, fold to simpler permutation. */
29196 if (rtx_equal_p (op0, op1))
29198 perm0 = perm0 & 1;
29199 perm1 = (perm1 & 1) + 2;
29201 /* If the first selector comes from the second operand, swap. */
29202 else if (perm0 & 2)
29204 if (perm1 & 2)
29205 return false;
29206 perm0 -= 2;
29207 perm1 += 2;
29208 x = op0, op0 = op1, op1 = x;
29210 /* If the second selector does not come from the second operand, fail. */
29211 else if ((perm1 & 2) == 0)
29212 return false;
29214 /* Success! */
29215 if (target != NULL)
29217 enum machine_mode vmode, dmode;
29218 rtvec v;
29220 vmode = GET_MODE (target);
29221 gcc_assert (GET_MODE_NUNITS (vmode) == 2);
29222 dmode = mode_for_vector (GET_MODE_INNER (vmode), 4);
29224 x = gen_rtx_VEC_CONCAT (dmode, op0, op1);
29225 v = gen_rtvec (2, GEN_INT (perm0), GEN_INT (perm1));
29226 x = gen_rtx_VEC_SELECT (vmode, x, gen_rtx_PARALLEL (VOIDmode, v));
29227 emit_insn (gen_rtx_SET (VOIDmode, target, x));
29229 return true;
29232 bool
29233 rs6000_expand_vec_perm_const (rtx operands[4])
29235 rtx target, op0, op1, sel;
29236 unsigned char perm0, perm1;
29238 target = operands[0];
29239 op0 = operands[1];
29240 op1 = operands[2];
29241 sel = operands[3];
29243 /* Unpack the constant selector. */
29244 perm0 = INTVAL (XVECEXP (sel, 0, 0)) & 3;
29245 perm1 = INTVAL (XVECEXP (sel, 0, 1)) & 3;
29247 return rs6000_expand_vec_perm_const_1 (target, op0, op1, perm0, perm1);
29250 /* Test whether a constant permutation is supported. */
29252 static bool
29253 rs6000_vectorize_vec_perm_const_ok (enum machine_mode vmode,
29254 const unsigned char *sel)
29256 /* AltiVec (and thus VSX) can handle arbitrary permutations. */
29257 if (TARGET_ALTIVEC)
29258 return true;
29260 /* Check for ps_merge* or evmerge* insns. */
29261 if ((TARGET_PAIRED_FLOAT && vmode == V2SFmode)
29262 || (TARGET_SPE && vmode == V2SImode))
29264 rtx op0 = gen_raw_REG (vmode, LAST_VIRTUAL_REGISTER + 1);
29265 rtx op1 = gen_raw_REG (vmode, LAST_VIRTUAL_REGISTER + 2);
29266 return rs6000_expand_vec_perm_const_1 (NULL, op0, op1, sel[0], sel[1]);
29269 return false;
29272 /* A subroutine for rs6000_expand_extract_even & rs6000_expand_interleave. */
29274 static void
29275 rs6000_do_expand_vec_perm (rtx target, rtx op0, rtx op1,
29276 enum machine_mode vmode, unsigned nelt, rtx perm[])
29278 enum machine_mode imode;
29279 rtx x;
29281 imode = vmode;
29282 if (GET_MODE_CLASS (vmode) != MODE_VECTOR_INT)
29284 imode = GET_MODE_INNER (vmode);
29285 imode = mode_for_size (GET_MODE_BITSIZE (imode), MODE_INT, 0);
29286 imode = mode_for_vector (imode, nelt);
29289 x = gen_rtx_CONST_VECTOR (imode, gen_rtvec_v (nelt, perm));
29290 x = expand_vec_perm (vmode, op0, op1, x, target);
29291 if (x != target)
29292 emit_move_insn (target, x);
29295 /* Expand an extract even operation. */
29297 void
29298 rs6000_expand_extract_even (rtx target, rtx op0, rtx op1)
29300 enum machine_mode vmode = GET_MODE (target);
29301 unsigned i, nelt = GET_MODE_NUNITS (vmode);
29302 rtx perm[16];
29304 for (i = 0; i < nelt; i++)
29305 perm[i] = GEN_INT (i * 2);
29307 rs6000_do_expand_vec_perm (target, op0, op1, vmode, nelt, perm);
29310 /* Expand a vector interleave operation. */
29312 void
29313 rs6000_expand_interleave (rtx target, rtx op0, rtx op1, bool highp)
29315 enum machine_mode vmode = GET_MODE (target);
29316 unsigned i, high, nelt = GET_MODE_NUNITS (vmode);
29317 rtx perm[16];
29319 high = (highp == BYTES_BIG_ENDIAN ? 0 : nelt / 2);
29320 for (i = 0; i < nelt / 2; i++)
29322 perm[i * 2] = GEN_INT (i + high);
29323 perm[i * 2 + 1] = GEN_INT (i + nelt + high);
29326 rs6000_do_expand_vec_perm (target, op0, op1, vmode, nelt, perm);
29329 /* Return an RTX representing where to find the function value of a
29330 function returning MODE. */
29331 static rtx
29332 rs6000_complex_function_value (enum machine_mode mode)
29334 unsigned int regno;
29335 rtx r1, r2;
29336 enum machine_mode inner = GET_MODE_INNER (mode);
29337 unsigned int inner_bytes = GET_MODE_SIZE (inner);
29339 if (FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
29340 regno = FP_ARG_RETURN;
29341 else
29343 regno = GP_ARG_RETURN;
29345 /* 32-bit is OK since it'll go in r3/r4. */
29346 if (TARGET_32BIT && inner_bytes >= 4)
29347 return gen_rtx_REG (mode, regno);
29350 if (inner_bytes >= 8)
29351 return gen_rtx_REG (mode, regno);
29353 r1 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno),
29354 const0_rtx);
29355 r2 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno + 1),
29356 GEN_INT (inner_bytes));
29357 return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
29360 /* Target hook for TARGET_FUNCTION_VALUE.
29362 On the SPE, both FPs and vectors are returned in r3.
29364 On RS/6000 an integer value is in r3 and a floating-point value is in
29365 fp1, unless -msoft-float. */
29367 static rtx
29368 rs6000_function_value (const_tree valtype,
29369 const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
29370 bool outgoing ATTRIBUTE_UNUSED)
29372 enum machine_mode mode;
29373 unsigned int regno;
29375 /* Special handling for structs in darwin64. */
29376 if (TARGET_MACHO
29377 && rs6000_darwin64_struct_check_p (TYPE_MODE (valtype), valtype))
29379 CUMULATIVE_ARGS valcum;
29380 rtx valret;
29382 valcum.words = 0;
29383 valcum.fregno = FP_ARG_MIN_REG;
29384 valcum.vregno = ALTIVEC_ARG_MIN_REG;
29385 /* Do a trial code generation as if this were going to be passed as
29386 an argument; if any part goes in memory, we return NULL. */
29387 valret = rs6000_darwin64_record_arg (&valcum, valtype, true, /* retval= */ true);
29388 if (valret)
29389 return valret;
29390 /* Otherwise fall through to standard ABI rules. */
29393 if (TARGET_32BIT && TARGET_POWERPC64 && TYPE_MODE (valtype) == DImode)
29395 /* Long long return value need be split in -mpowerpc64, 32bit ABI. */
29396 return gen_rtx_PARALLEL (DImode,
29397 gen_rtvec (2,
29398 gen_rtx_EXPR_LIST (VOIDmode,
29399 gen_rtx_REG (SImode, GP_ARG_RETURN),
29400 const0_rtx),
29401 gen_rtx_EXPR_LIST (VOIDmode,
29402 gen_rtx_REG (SImode,
29403 GP_ARG_RETURN + 1),
29404 GEN_INT (4))));
29406 if (TARGET_32BIT && TARGET_POWERPC64 && TYPE_MODE (valtype) == DCmode)
29408 return gen_rtx_PARALLEL (DCmode,
29409 gen_rtvec (4,
29410 gen_rtx_EXPR_LIST (VOIDmode,
29411 gen_rtx_REG (SImode, GP_ARG_RETURN),
29412 const0_rtx),
29413 gen_rtx_EXPR_LIST (VOIDmode,
29414 gen_rtx_REG (SImode,
29415 GP_ARG_RETURN + 1),
29416 GEN_INT (4)),
29417 gen_rtx_EXPR_LIST (VOIDmode,
29418 gen_rtx_REG (SImode,
29419 GP_ARG_RETURN + 2),
29420 GEN_INT (8)),
29421 gen_rtx_EXPR_LIST (VOIDmode,
29422 gen_rtx_REG (SImode,
29423 GP_ARG_RETURN + 3),
29424 GEN_INT (12))));
29427 mode = TYPE_MODE (valtype);
29428 if ((INTEGRAL_TYPE_P (valtype) && GET_MODE_BITSIZE (mode) < BITS_PER_WORD)
29429 || (POINTER_TYPE_P (valtype) && !upc_shared_type_p (TREE_TYPE (valtype))))
29430 mode = TARGET_32BIT ? SImode : DImode;
29432 if (DECIMAL_FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
29433 /* _Decimal128 must use an even/odd register pair. */
29434 regno = (mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
29435 else if (SCALAR_FLOAT_TYPE_P (valtype) && TARGET_HARD_FLOAT && TARGET_FPRS
29436 && ((TARGET_SINGLE_FLOAT && (mode == SFmode)) || TARGET_DOUBLE_FLOAT))
29437 regno = FP_ARG_RETURN;
29438 else if (TREE_CODE (valtype) == COMPLEX_TYPE
29439 && targetm.calls.split_complex_arg)
29440 return rs6000_complex_function_value (mode);
29441 /* VSX is a superset of Altivec and adds V2DImode/V2DFmode. Since the same
29442 return register is used in both cases, and we won't see V2DImode/V2DFmode
29443 for pure altivec, combine the two cases. */
29444 else if (TREE_CODE (valtype) == VECTOR_TYPE
29445 && TARGET_ALTIVEC && TARGET_ALTIVEC_ABI
29446 && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
29447 regno = ALTIVEC_ARG_RETURN;
29448 else if (TARGET_E500_DOUBLE && TARGET_HARD_FLOAT
29449 && (mode == DFmode || mode == DCmode
29450 || mode == TFmode || mode == TCmode))
29451 return spe_build_register_parallel (mode, GP_ARG_RETURN);
29452 else
29453 regno = GP_ARG_RETURN;
29455 return gen_rtx_REG (mode, regno);
29458 /* Define how to find the value returned by a library function
29459 assuming the value has mode MODE. */
29461 rs6000_libcall_value (enum machine_mode mode)
29463 unsigned int regno;
29465 if (TARGET_32BIT && TARGET_POWERPC64 && mode == DImode)
29467 /* Long long return value need be split in -mpowerpc64, 32bit ABI. */
29468 return gen_rtx_PARALLEL (DImode,
29469 gen_rtvec (2,
29470 gen_rtx_EXPR_LIST (VOIDmode,
29471 gen_rtx_REG (SImode, GP_ARG_RETURN),
29472 const0_rtx),
29473 gen_rtx_EXPR_LIST (VOIDmode,
29474 gen_rtx_REG (SImode,
29475 GP_ARG_RETURN + 1),
29476 GEN_INT (4))));
29479 if (DECIMAL_FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
29480 /* _Decimal128 must use an even/odd register pair. */
29481 regno = (mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
29482 else if (SCALAR_FLOAT_MODE_P (mode)
29483 && TARGET_HARD_FLOAT && TARGET_FPRS
29484 && ((TARGET_SINGLE_FLOAT && mode == SFmode) || TARGET_DOUBLE_FLOAT))
29485 regno = FP_ARG_RETURN;
29486 /* VSX is a superset of Altivec and adds V2DImode/V2DFmode. Since the same
29487 return register is used in both cases, and we won't see V2DImode/V2DFmode
29488 for pure altivec, combine the two cases. */
29489 else if (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
29490 && TARGET_ALTIVEC && TARGET_ALTIVEC_ABI)
29491 regno = ALTIVEC_ARG_RETURN;
29492 else if (COMPLEX_MODE_P (mode) && targetm.calls.split_complex_arg)
29493 return rs6000_complex_function_value (mode);
29494 else if (TARGET_E500_DOUBLE && TARGET_HARD_FLOAT
29495 && (mode == DFmode || mode == DCmode
29496 || mode == TFmode || mode == TCmode))
29497 return spe_build_register_parallel (mode, GP_ARG_RETURN);
29498 else
29499 regno = GP_ARG_RETURN;
29501 return gen_rtx_REG (mode, regno);
29505 /* Return true if we use LRA instead of reload pass. */
29506 static bool
29507 rs6000_lra_p (void)
29509 return rs6000_lra_flag;
29512 /* Given FROM and TO register numbers, say whether this elimination is allowed.
29513 Frame pointer elimination is automatically handled.
29515 For the RS/6000, if frame pointer elimination is being done, we would like
29516 to convert ap into fp, not sp.
29518 We need r30 if -mminimal-toc was specified, and there are constant pool
29519 references. */
29521 static bool
29522 rs6000_can_eliminate (const int from, const int to)
29524 return (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM
29525 ? ! frame_pointer_needed
29526 : from == RS6000_PIC_OFFSET_TABLE_REGNUM
29527 ? ! TARGET_MINIMAL_TOC || TARGET_NO_TOC || get_pool_size () == 0
29528 : true);
29531 /* Define the offset between two registers, FROM to be eliminated and its
29532 replacement TO, at the start of a routine. */
29533 HOST_WIDE_INT
29534 rs6000_initial_elimination_offset (int from, int to)
29536 rs6000_stack_t *info = rs6000_stack_info ();
29537 HOST_WIDE_INT offset;
29539 if (from == HARD_FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
29540 offset = info->push_p ? 0 : -info->total_size;
29541 else if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
29543 offset = info->push_p ? 0 : -info->total_size;
29544 if (FRAME_GROWS_DOWNWARD)
29545 offset += info->fixed_size + info->vars_size + info->parm_size;
29547 else if (from == FRAME_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
29548 offset = FRAME_GROWS_DOWNWARD
29549 ? info->fixed_size + info->vars_size + info->parm_size
29550 : 0;
29551 else if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
29552 offset = info->total_size;
29553 else if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
29554 offset = info->push_p ? info->total_size : 0;
29555 else if (from == RS6000_PIC_OFFSET_TABLE_REGNUM)
29556 offset = 0;
29557 else
29558 gcc_unreachable ();
29560 return offset;
29563 static rtx
29564 rs6000_dwarf_register_span (rtx reg)
29566 rtx parts[8];
29567 int i, words;
29568 unsigned regno = REGNO (reg);
29569 enum machine_mode mode = GET_MODE (reg);
29571 if (TARGET_SPE
29572 && regno < 32
29573 && (SPE_VECTOR_MODE (GET_MODE (reg))
29574 || (TARGET_E500_DOUBLE && FLOAT_MODE_P (mode)
29575 && mode != SFmode && mode != SDmode && mode != SCmode)))
29577 else
29578 return NULL_RTX;
29580 regno = REGNO (reg);
29582 /* The duality of the SPE register size wreaks all kinds of havoc.
29583 This is a way of distinguishing r0 in 32-bits from r0 in
29584 64-bits. */
29585 words = (GET_MODE_SIZE (mode) + UNITS_PER_FP_WORD - 1) / UNITS_PER_FP_WORD;
29586 gcc_assert (words <= 4);
29587 for (i = 0; i < words; i++, regno++)
29589 if (BYTES_BIG_ENDIAN)
29591 parts[2 * i] = gen_rtx_REG (SImode, regno + 1200);
29592 parts[2 * i + 1] = gen_rtx_REG (SImode, regno);
29594 else
29596 parts[2 * i] = gen_rtx_REG (SImode, regno);
29597 parts[2 * i + 1] = gen_rtx_REG (SImode, regno + 1200);
29601 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (words * 2, parts));
29604 /* Fill in sizes for SPE register high parts in table used by unwinder. */
29606 static void
29607 rs6000_init_dwarf_reg_sizes_extra (tree address)
29609 if (TARGET_SPE)
29611 int i;
29612 enum machine_mode mode = TYPE_MODE (char_type_node);
29613 rtx addr = expand_expr (address, NULL_RTX, VOIDmode, EXPAND_NORMAL);
29614 rtx mem = gen_rtx_MEM (BLKmode, addr);
29615 rtx value = gen_int_mode (4, mode);
29617 for (i = 1201; i < 1232; i++)
29619 int column = DWARF_REG_TO_UNWIND_COLUMN (i);
29620 HOST_WIDE_INT offset
29621 = DWARF_FRAME_REGNUM (column) * GET_MODE_SIZE (mode);
29623 emit_move_insn (adjust_address (mem, mode, offset), value);
29627 if (TARGET_MACHO && ! TARGET_ALTIVEC)
29629 int i;
29630 enum machine_mode mode = TYPE_MODE (char_type_node);
29631 rtx addr = expand_expr (address, NULL_RTX, VOIDmode, EXPAND_NORMAL);
29632 rtx mem = gen_rtx_MEM (BLKmode, addr);
29633 rtx value = gen_int_mode (16, mode);
29635 /* On Darwin, libgcc may be built to run on both G3 and G4/5.
29636 The unwinder still needs to know the size of Altivec registers. */
29638 for (i = FIRST_ALTIVEC_REGNO; i < LAST_ALTIVEC_REGNO+1; i++)
29640 int column = DWARF_REG_TO_UNWIND_COLUMN (i);
29641 HOST_WIDE_INT offset
29642 = DWARF_FRAME_REGNUM (column) * GET_MODE_SIZE (mode);
29644 emit_move_insn (adjust_address (mem, mode, offset), value);
29649 /* Map internal gcc register numbers to DWARF2 register numbers. */
29651 unsigned int
29652 rs6000_dbx_register_number (unsigned int regno)
29654 if (regno <= 63 || write_symbols != DWARF2_DEBUG)
29655 return regno;
29656 if (regno == LR_REGNO)
29657 return 108;
29658 if (regno == CTR_REGNO)
29659 return 109;
29660 if (CR_REGNO_P (regno))
29661 return regno - CR0_REGNO + 86;
29662 if (regno == CA_REGNO)
29663 return 101; /* XER */
29664 if (ALTIVEC_REGNO_P (regno))
29665 return regno - FIRST_ALTIVEC_REGNO + 1124;
29666 if (regno == VRSAVE_REGNO)
29667 return 356;
29668 if (regno == VSCR_REGNO)
29669 return 67;
29670 if (regno == SPE_ACC_REGNO)
29671 return 99;
29672 if (regno == SPEFSCR_REGNO)
29673 return 612;
29674 /* SPE high reg number. We get these values of regno from
29675 rs6000_dwarf_register_span. */
29676 gcc_assert (regno >= 1200 && regno < 1232);
29677 return regno;
29680 /* target hook eh_return_filter_mode */
29681 static enum machine_mode
29682 rs6000_eh_return_filter_mode (void)
29684 return TARGET_32BIT ? SImode : word_mode;
29687 /* Target hook for scalar_mode_supported_p. */
29688 static bool
29689 rs6000_scalar_mode_supported_p (enum machine_mode mode)
29691 if (DECIMAL_FLOAT_MODE_P (mode))
29692 return default_decimal_float_supported_p ();
29693 else
29694 return default_scalar_mode_supported_p (mode);
29697 /* Target hook for vector_mode_supported_p. */
29698 static bool
29699 rs6000_vector_mode_supported_p (enum machine_mode mode)
29702 if (TARGET_PAIRED_FLOAT && PAIRED_VECTOR_MODE (mode))
29703 return true;
29705 if (TARGET_SPE && SPE_VECTOR_MODE (mode))
29706 return true;
29708 else if (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode))
29709 return true;
29711 else
29712 return false;
29715 /* Target hook for invalid_arg_for_unprototyped_fn. */
29716 static const char *
29717 invalid_arg_for_unprototyped_fn (const_tree typelist, const_tree funcdecl, const_tree val)
29719 return (!rs6000_darwin64_abi
29720 && typelist == 0
29721 && TREE_CODE (TREE_TYPE (val)) == VECTOR_TYPE
29722 && (funcdecl == NULL_TREE
29723 || (TREE_CODE (funcdecl) == FUNCTION_DECL
29724 && DECL_BUILT_IN_CLASS (funcdecl) != BUILT_IN_MD)))
29725 ? N_("AltiVec argument passed to unprototyped function")
29726 : NULL;
29729 /* For TARGET_SECURE_PLT 32-bit PIC code we can save PIC register
29730 setup by using __stack_chk_fail_local hidden function instead of
29731 calling __stack_chk_fail directly. Otherwise it is better to call
29732 __stack_chk_fail directly. */
29734 static tree ATTRIBUTE_UNUSED
29735 rs6000_stack_protect_fail (void)
29737 return (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
29738 ? default_hidden_stack_protect_fail ()
29739 : default_external_stack_protect_fail ();
29742 void
29743 rs6000_final_prescan_insn (rtx insn, rtx *operand ATTRIBUTE_UNUSED,
29744 int num_operands ATTRIBUTE_UNUSED)
29746 if (rs6000_warn_cell_microcode)
29748 const char *temp;
29749 int insn_code_number = recog_memoized (insn);
29750 location_t location = INSN_LOCATION (insn);
29752 /* Punt on insns we cannot recognize. */
29753 if (insn_code_number < 0)
29754 return;
29756 temp = get_insn_template (insn_code_number, insn);
29758 if (get_attr_cell_micro (insn) == CELL_MICRO_ALWAYS)
29759 warning_at (location, OPT_mwarn_cell_microcode,
29760 "emitting microcode insn %s\t[%s] #%d",
29761 temp, insn_data[INSN_CODE (insn)].name, INSN_UID (insn));
29762 else if (get_attr_cell_micro (insn) == CELL_MICRO_CONDITIONAL)
29763 warning_at (location, OPT_mwarn_cell_microcode,
29764 "emitting conditional microcode insn %s\t[%s] #%d",
29765 temp, insn_data[INSN_CODE (insn)].name, INSN_UID (insn));
29769 /* Implement the TARGET_ASAN_SHADOW_OFFSET hook. */
29771 #if TARGET_ELF
29772 static unsigned HOST_WIDE_INT
29773 rs6000_asan_shadow_offset (void)
29775 return (unsigned HOST_WIDE_INT) 1 << (TARGET_64BIT ? 41 : 29);
29777 #endif
29779 /* Mask options that we want to support inside of attribute((target)) and
29780 #pragma GCC target operations. Note, we do not include things like
29781 64/32-bit, endianess, hard/soft floating point, etc. that would have
29782 different calling sequences. */
29784 struct rs6000_opt_mask {
29785 const char *name; /* option name */
29786 HOST_WIDE_INT mask; /* mask to set */
29787 bool invert; /* invert sense of mask */
29788 bool valid_target; /* option is a target option */
29791 static struct rs6000_opt_mask const rs6000_opt_masks[] =
29793 { "altivec", OPTION_MASK_ALTIVEC, false, true },
29794 { "cmpb", OPTION_MASK_CMPB, false, true },
29795 { "crypto", OPTION_MASK_CRYPTO, false, true },
29796 { "direct-move", OPTION_MASK_DIRECT_MOVE, false, true },
29797 { "dlmzb", OPTION_MASK_DLMZB, false, true },
29798 { "fprnd", OPTION_MASK_FPRND, false, true },
29799 { "hard-dfp", OPTION_MASK_DFP, false, true },
29800 { "htm", OPTION_MASK_HTM, false, true },
29801 { "isel", OPTION_MASK_ISEL, false, true },
29802 { "mfcrf", OPTION_MASK_MFCRF, false, true },
29803 { "mfpgpr", OPTION_MASK_MFPGPR, false, true },
29804 { "mulhw", OPTION_MASK_MULHW, false, true },
29805 { "multiple", OPTION_MASK_MULTIPLE, false, true },
29806 { "popcntb", OPTION_MASK_POPCNTB, false, true },
29807 { "popcntd", OPTION_MASK_POPCNTD, false, true },
29808 { "power8-fusion", OPTION_MASK_P8_FUSION, false, true },
29809 { "power8-fusion-sign", OPTION_MASK_P8_FUSION_SIGN, false, true },
29810 { "power8-vector", OPTION_MASK_P8_VECTOR, false, true },
29811 { "powerpc-gfxopt", OPTION_MASK_PPC_GFXOPT, false, true },
29812 { "powerpc-gpopt", OPTION_MASK_PPC_GPOPT, false, true },
29813 { "quad-memory", OPTION_MASK_QUAD_MEMORY, false, true },
29814 { "recip-precision", OPTION_MASK_RECIP_PRECISION, false, true },
29815 { "string", OPTION_MASK_STRING, false, true },
29816 { "update", OPTION_MASK_NO_UPDATE, true , true },
29817 { "upper-regs-df", OPTION_MASK_UPPER_REGS_DF, false, false },
29818 { "upper-regs-sf", OPTION_MASK_UPPER_REGS_SF, false, false },
29819 { "vsx", OPTION_MASK_VSX, false, true },
29820 { "vsx-timode", OPTION_MASK_VSX_TIMODE, false, true },
29821 #ifdef OPTION_MASK_64BIT
29822 #if TARGET_AIX_OS
29823 { "aix64", OPTION_MASK_64BIT, false, false },
29824 { "aix32", OPTION_MASK_64BIT, true, false },
29825 #else
29826 { "64", OPTION_MASK_64BIT, false, false },
29827 { "32", OPTION_MASK_64BIT, true, false },
29828 #endif
29829 #endif
29830 #ifdef OPTION_MASK_EABI
29831 { "eabi", OPTION_MASK_EABI, false, false },
29832 #endif
29833 #ifdef OPTION_MASK_LITTLE_ENDIAN
29834 { "little", OPTION_MASK_LITTLE_ENDIAN, false, false },
29835 { "big", OPTION_MASK_LITTLE_ENDIAN, true, false },
29836 #endif
29837 #ifdef OPTION_MASK_RELOCATABLE
29838 { "relocatable", OPTION_MASK_RELOCATABLE, false, false },
29839 #endif
29840 #ifdef OPTION_MASK_STRICT_ALIGN
29841 { "strict-align", OPTION_MASK_STRICT_ALIGN, false, false },
29842 #endif
29843 { "soft-float", OPTION_MASK_SOFT_FLOAT, false, false },
29844 { "string", OPTION_MASK_STRING, false, false },
29847 /* Builtin mask mapping for printing the flags. */
29848 static struct rs6000_opt_mask const rs6000_builtin_mask_names[] =
29850 { "altivec", RS6000_BTM_ALTIVEC, false, false },
29851 { "vsx", RS6000_BTM_VSX, false, false },
29852 { "spe", RS6000_BTM_SPE, false, false },
29853 { "paired", RS6000_BTM_PAIRED, false, false },
29854 { "fre", RS6000_BTM_FRE, false, false },
29855 { "fres", RS6000_BTM_FRES, false, false },
29856 { "frsqrte", RS6000_BTM_FRSQRTE, false, false },
29857 { "frsqrtes", RS6000_BTM_FRSQRTES, false, false },
29858 { "popcntd", RS6000_BTM_POPCNTD, false, false },
29859 { "cell", RS6000_BTM_CELL, false, false },
29860 { "power8-vector", RS6000_BTM_P8_VECTOR, false, false },
29861 { "crypto", RS6000_BTM_CRYPTO, false, false },
29862 { "htm", RS6000_BTM_HTM, false, false },
29865 /* Option variables that we want to support inside attribute((target)) and
29866 #pragma GCC target operations. */
29868 struct rs6000_opt_var {
29869 const char *name; /* option name */
29870 size_t global_offset; /* offset of the option in global_options. */
29871 size_t target_offset; /* offset of the option in target optiosn. */
29874 static struct rs6000_opt_var const rs6000_opt_vars[] =
29876 { "friz",
29877 offsetof (struct gcc_options, x_TARGET_FRIZ),
29878 offsetof (struct cl_target_option, x_TARGET_FRIZ), },
29879 { "avoid-indexed-addresses",
29880 offsetof (struct gcc_options, x_TARGET_AVOID_XFORM),
29881 offsetof (struct cl_target_option, x_TARGET_AVOID_XFORM) },
29882 { "paired",
29883 offsetof (struct gcc_options, x_rs6000_paired_float),
29884 offsetof (struct cl_target_option, x_rs6000_paired_float), },
29885 { "longcall",
29886 offsetof (struct gcc_options, x_rs6000_default_long_calls),
29887 offsetof (struct cl_target_option, x_rs6000_default_long_calls), },
29890 /* Inner function to handle attribute((target("..."))) and #pragma GCC target
29891 parsing. Return true if there were no errors. */
29893 static bool
29894 rs6000_inner_target_options (tree args, bool attr_p)
29896 bool ret = true;
29898 if (args == NULL_TREE)
29901 else if (TREE_CODE (args) == STRING_CST)
29903 char *p = ASTRDUP (TREE_STRING_POINTER (args));
29904 char *q;
29906 while ((q = strtok (p, ",")) != NULL)
29908 bool error_p = false;
29909 bool not_valid_p = false;
29910 const char *cpu_opt = NULL;
29912 p = NULL;
29913 if (strncmp (q, "cpu=", 4) == 0)
29915 int cpu_index = rs6000_cpu_name_lookup (q+4);
29916 if (cpu_index >= 0)
29917 rs6000_cpu_index = cpu_index;
29918 else
29920 error_p = true;
29921 cpu_opt = q+4;
29924 else if (strncmp (q, "tune=", 5) == 0)
29926 int tune_index = rs6000_cpu_name_lookup (q+5);
29927 if (tune_index >= 0)
29928 rs6000_tune_index = tune_index;
29929 else
29931 error_p = true;
29932 cpu_opt = q+5;
29935 else
29937 size_t i;
29938 bool invert = false;
29939 char *r = q;
29941 error_p = true;
29942 if (strncmp (r, "no-", 3) == 0)
29944 invert = true;
29945 r += 3;
29948 for (i = 0; i < ARRAY_SIZE (rs6000_opt_masks); i++)
29949 if (strcmp (r, rs6000_opt_masks[i].name) == 0)
29951 HOST_WIDE_INT mask = rs6000_opt_masks[i].mask;
29953 if (!rs6000_opt_masks[i].valid_target)
29954 not_valid_p = true;
29955 else
29957 error_p = false;
29958 rs6000_isa_flags_explicit |= mask;
29960 /* VSX needs altivec, so -mvsx automagically sets
29961 altivec. */
29962 if (mask == OPTION_MASK_VSX && !invert)
29963 mask |= OPTION_MASK_ALTIVEC;
29965 if (rs6000_opt_masks[i].invert)
29966 invert = !invert;
29968 if (invert)
29969 rs6000_isa_flags &= ~mask;
29970 else
29971 rs6000_isa_flags |= mask;
29973 break;
29976 if (error_p && !not_valid_p)
29978 for (i = 0; i < ARRAY_SIZE (rs6000_opt_vars); i++)
29979 if (strcmp (r, rs6000_opt_vars[i].name) == 0)
29981 size_t j = rs6000_opt_vars[i].global_offset;
29982 *((int *) ((char *)&global_options + j)) = !invert;
29983 error_p = false;
29984 break;
29989 if (error_p)
29991 const char *eprefix, *esuffix;
29993 ret = false;
29994 if (attr_p)
29996 eprefix = "__attribute__((__target__(";
29997 esuffix = ")))";
29999 else
30001 eprefix = "#pragma GCC target ";
30002 esuffix = "";
30005 if (cpu_opt)
30006 error ("invalid cpu \"%s\" for %s\"%s\"%s", cpu_opt, eprefix,
30007 q, esuffix);
30008 else if (not_valid_p)
30009 error ("%s\"%s\"%s is not allowed", eprefix, q, esuffix);
30010 else
30011 error ("%s\"%s\"%s is invalid", eprefix, q, esuffix);
30016 else if (TREE_CODE (args) == TREE_LIST)
30020 tree value = TREE_VALUE (args);
30021 if (value)
30023 bool ret2 = rs6000_inner_target_options (value, attr_p);
30024 if (!ret2)
30025 ret = false;
30027 args = TREE_CHAIN (args);
30029 while (args != NULL_TREE);
30032 else
30033 gcc_unreachable ();
30035 return ret;
30038 /* Print out the target options as a list for -mdebug=target. */
30040 static void
30041 rs6000_debug_target_options (tree args, const char *prefix)
30043 if (args == NULL_TREE)
30044 fprintf (stderr, "%s<NULL>", prefix);
30046 else if (TREE_CODE (args) == STRING_CST)
30048 char *p = ASTRDUP (TREE_STRING_POINTER (args));
30049 char *q;
30051 while ((q = strtok (p, ",")) != NULL)
30053 p = NULL;
30054 fprintf (stderr, "%s\"%s\"", prefix, q);
30055 prefix = ", ";
30059 else if (TREE_CODE (args) == TREE_LIST)
30063 tree value = TREE_VALUE (args);
30064 if (value)
30066 rs6000_debug_target_options (value, prefix);
30067 prefix = ", ";
30069 args = TREE_CHAIN (args);
30071 while (args != NULL_TREE);
30074 else
30075 gcc_unreachable ();
30077 return;
30081 /* Hook to validate attribute((target("..."))). */
30083 static bool
30084 rs6000_valid_attribute_p (tree fndecl,
30085 tree ARG_UNUSED (name),
30086 tree args,
30087 int flags)
30089 struct cl_target_option cur_target;
30090 bool ret;
30091 tree old_optimize = build_optimization_node (&global_options);
30092 tree new_target, new_optimize;
30093 tree func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
30095 gcc_assert ((fndecl != NULL_TREE) && (args != NULL_TREE));
30097 if (TARGET_DEBUG_TARGET)
30099 tree tname = DECL_NAME (fndecl);
30100 fprintf (stderr, "\n==================== rs6000_valid_attribute_p:\n");
30101 if (tname)
30102 fprintf (stderr, "function: %.*s\n",
30103 (int) IDENTIFIER_LENGTH (tname),
30104 IDENTIFIER_POINTER (tname));
30105 else
30106 fprintf (stderr, "function: unknown\n");
30108 fprintf (stderr, "args:");
30109 rs6000_debug_target_options (args, " ");
30110 fprintf (stderr, "\n");
30112 if (flags)
30113 fprintf (stderr, "flags: 0x%x\n", flags);
30115 fprintf (stderr, "--------------------\n");
30118 old_optimize = build_optimization_node (&global_options);
30119 func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
30121 /* If the function changed the optimization levels as well as setting target
30122 options, start with the optimizations specified. */
30123 if (func_optimize && func_optimize != old_optimize)
30124 cl_optimization_restore (&global_options,
30125 TREE_OPTIMIZATION (func_optimize));
30127 /* The target attributes may also change some optimization flags, so update
30128 the optimization options if necessary. */
30129 cl_target_option_save (&cur_target, &global_options);
30130 rs6000_cpu_index = rs6000_tune_index = -1;
30131 ret = rs6000_inner_target_options (args, true);
30133 /* Set up any additional state. */
30134 if (ret)
30136 ret = rs6000_option_override_internal (false);
30137 new_target = build_target_option_node (&global_options);
30139 else
30140 new_target = NULL;
30142 new_optimize = build_optimization_node (&global_options);
30144 if (!new_target)
30145 ret = false;
30147 else if (fndecl)
30149 DECL_FUNCTION_SPECIFIC_TARGET (fndecl) = new_target;
30151 if (old_optimize != new_optimize)
30152 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl) = new_optimize;
30155 cl_target_option_restore (&global_options, &cur_target);
30157 if (old_optimize != new_optimize)
30158 cl_optimization_restore (&global_options,
30159 TREE_OPTIMIZATION (old_optimize));
30161 return ret;
30165 /* Hook to validate the current #pragma GCC target and set the state, and
30166 update the macros based on what was changed. If ARGS is NULL, then
30167 POP_TARGET is used to reset the options. */
30169 bool
30170 rs6000_pragma_target_parse (tree args, tree pop_target)
30172 tree prev_tree = build_target_option_node (&global_options);
30173 tree cur_tree;
30174 struct cl_target_option *prev_opt, *cur_opt;
30175 HOST_WIDE_INT prev_flags, cur_flags, diff_flags;
30176 HOST_WIDE_INT prev_bumask, cur_bumask, diff_bumask;
30178 if (TARGET_DEBUG_TARGET)
30180 fprintf (stderr, "\n==================== rs6000_pragma_target_parse\n");
30181 fprintf (stderr, "args:");
30182 rs6000_debug_target_options (args, " ");
30183 fprintf (stderr, "\n");
30185 if (pop_target)
30187 fprintf (stderr, "pop_target:\n");
30188 debug_tree (pop_target);
30190 else
30191 fprintf (stderr, "pop_target: <NULL>\n");
30193 fprintf (stderr, "--------------------\n");
30196 if (! args)
30198 cur_tree = ((pop_target)
30199 ? pop_target
30200 : target_option_default_node);
30201 cl_target_option_restore (&global_options,
30202 TREE_TARGET_OPTION (cur_tree));
30204 else
30206 rs6000_cpu_index = rs6000_tune_index = -1;
30207 if (!rs6000_inner_target_options (args, false)
30208 || !rs6000_option_override_internal (false)
30209 || (cur_tree = build_target_option_node (&global_options))
30210 == NULL_TREE)
30212 if (TARGET_DEBUG_BUILTIN || TARGET_DEBUG_TARGET)
30213 fprintf (stderr, "invalid pragma\n");
30215 return false;
30219 target_option_current_node = cur_tree;
30221 /* If we have the preprocessor linked in (i.e. C or C++ languages), possibly
30222 change the macros that are defined. */
30223 if (rs6000_target_modify_macros_ptr)
30225 prev_opt = TREE_TARGET_OPTION (prev_tree);
30226 prev_bumask = prev_opt->x_rs6000_builtin_mask;
30227 prev_flags = prev_opt->x_rs6000_isa_flags;
30229 cur_opt = TREE_TARGET_OPTION (cur_tree);
30230 cur_flags = cur_opt->x_rs6000_isa_flags;
30231 cur_bumask = cur_opt->x_rs6000_builtin_mask;
30233 diff_bumask = (prev_bumask ^ cur_bumask);
30234 diff_flags = (prev_flags ^ cur_flags);
30236 if ((diff_flags != 0) || (diff_bumask != 0))
30238 /* Delete old macros. */
30239 rs6000_target_modify_macros_ptr (false,
30240 prev_flags & diff_flags,
30241 prev_bumask & diff_bumask);
30243 /* Define new macros. */
30244 rs6000_target_modify_macros_ptr (true,
30245 cur_flags & diff_flags,
30246 cur_bumask & diff_bumask);
30250 return true;
30254 /* Remember the last target of rs6000_set_current_function. */
30255 static GTY(()) tree rs6000_previous_fndecl;
30257 /* Establish appropriate back-end context for processing the function
30258 FNDECL. The argument might be NULL to indicate processing at top
30259 level, outside of any function scope. */
30260 static void
30261 rs6000_set_current_function (tree fndecl)
30263 tree old_tree = (rs6000_previous_fndecl
30264 ? DECL_FUNCTION_SPECIFIC_TARGET (rs6000_previous_fndecl)
30265 : NULL_TREE);
30267 tree new_tree = (fndecl
30268 ? DECL_FUNCTION_SPECIFIC_TARGET (fndecl)
30269 : NULL_TREE);
30271 if (TARGET_DEBUG_TARGET)
30273 bool print_final = false;
30274 fprintf (stderr, "\n==================== rs6000_set_current_function");
30276 if (fndecl)
30277 fprintf (stderr, ", fndecl %s (%p)",
30278 (DECL_NAME (fndecl)
30279 ? IDENTIFIER_POINTER (DECL_NAME (fndecl))
30280 : "<unknown>"), (void *)fndecl);
30282 if (rs6000_previous_fndecl)
30283 fprintf (stderr, ", prev_fndecl (%p)", (void *)rs6000_previous_fndecl);
30285 fprintf (stderr, "\n");
30286 if (new_tree)
30288 fprintf (stderr, "\nnew fndecl target specific options:\n");
30289 debug_tree (new_tree);
30290 print_final = true;
30293 if (old_tree)
30295 fprintf (stderr, "\nold fndecl target specific options:\n");
30296 debug_tree (old_tree);
30297 print_final = true;
30300 if (print_final)
30301 fprintf (stderr, "--------------------\n");
30304 /* Only change the context if the function changes. This hook is called
30305 several times in the course of compiling a function, and we don't want to
30306 slow things down too much or call target_reinit when it isn't safe. */
30307 if (fndecl && fndecl != rs6000_previous_fndecl)
30309 rs6000_previous_fndecl = fndecl;
30310 if (old_tree == new_tree)
30313 else if (new_tree)
30315 cl_target_option_restore (&global_options,
30316 TREE_TARGET_OPTION (new_tree));
30317 target_reinit ();
30320 else if (old_tree)
30322 struct cl_target_option *def
30323 = TREE_TARGET_OPTION (target_option_current_node);
30325 cl_target_option_restore (&global_options, def);
30326 target_reinit ();
30332 /* Save the current options */
30334 static void
30335 rs6000_function_specific_save (struct cl_target_option *ptr,
30336 struct gcc_options *opts)
30338 ptr->x_rs6000_isa_flags = opts->x_rs6000_isa_flags;
30339 ptr->x_rs6000_isa_flags_explicit = opts->x_rs6000_isa_flags_explicit;
30342 /* Restore the current options */
30344 static void
30345 rs6000_function_specific_restore (struct gcc_options *opts,
30346 struct cl_target_option *ptr)
30349 opts->x_rs6000_isa_flags = ptr->x_rs6000_isa_flags;
30350 opts->x_rs6000_isa_flags_explicit = ptr->x_rs6000_isa_flags_explicit;
30351 (void) rs6000_option_override_internal (false);
30354 /* Print the current options */
30356 static void
30357 rs6000_function_specific_print (FILE *file, int indent,
30358 struct cl_target_option *ptr)
30360 rs6000_print_isa_options (file, indent, "Isa options set",
30361 ptr->x_rs6000_isa_flags);
30363 rs6000_print_isa_options (file, indent, "Isa options explicit",
30364 ptr->x_rs6000_isa_flags_explicit);
30367 /* Helper function to print the current isa or misc options on a line. */
30369 static void
30370 rs6000_print_options_internal (FILE *file,
30371 int indent,
30372 const char *string,
30373 HOST_WIDE_INT flags,
30374 const char *prefix,
30375 const struct rs6000_opt_mask *opts,
30376 size_t num_elements)
30378 size_t i;
30379 size_t start_column = 0;
30380 size_t cur_column;
30381 size_t max_column = 76;
30382 const char *comma = "";
30384 if (indent)
30385 start_column += fprintf (file, "%*s", indent, "");
30387 if (!flags)
30389 fprintf (stderr, DEBUG_FMT_S, string, "<none>");
30390 return;
30393 start_column += fprintf (stderr, DEBUG_FMT_WX, string, flags);
30395 /* Print the various mask options. */
30396 cur_column = start_column;
30397 for (i = 0; i < num_elements; i++)
30399 if ((flags & opts[i].mask) != 0)
30401 const char *no_str = rs6000_opt_masks[i].invert ? "no-" : "";
30402 size_t len = (strlen (comma)
30403 + strlen (prefix)
30404 + strlen (no_str)
30405 + strlen (rs6000_opt_masks[i].name));
30407 cur_column += len;
30408 if (cur_column > max_column)
30410 fprintf (stderr, ", \\\n%*s", (int)start_column, "");
30411 cur_column = start_column + len;
30412 comma = "";
30415 fprintf (file, "%s%s%s%s", comma, prefix, no_str,
30416 rs6000_opt_masks[i].name);
30417 flags &= ~ opts[i].mask;
30418 comma = ", ";
30422 fputs ("\n", file);
30425 /* Helper function to print the current isa options on a line. */
30427 static void
30428 rs6000_print_isa_options (FILE *file, int indent, const char *string,
30429 HOST_WIDE_INT flags)
30431 rs6000_print_options_internal (file, indent, string, flags, "-m",
30432 &rs6000_opt_masks[0],
30433 ARRAY_SIZE (rs6000_opt_masks));
30436 static void
30437 rs6000_print_builtin_options (FILE *file, int indent, const char *string,
30438 HOST_WIDE_INT flags)
30440 rs6000_print_options_internal (file, indent, string, flags, "",
30441 &rs6000_builtin_mask_names[0],
30442 ARRAY_SIZE (rs6000_builtin_mask_names));
30446 /* Hook to determine if one function can safely inline another. */
30448 static bool
30449 rs6000_can_inline_p (tree caller, tree callee)
30451 bool ret = false;
30452 tree caller_tree = DECL_FUNCTION_SPECIFIC_TARGET (caller);
30453 tree callee_tree = DECL_FUNCTION_SPECIFIC_TARGET (callee);
30455 /* If callee has no option attributes, then it is ok to inline. */
30456 if (!callee_tree)
30457 ret = true;
30459 /* If caller has no option attributes, but callee does then it is not ok to
30460 inline. */
30461 else if (!caller_tree)
30462 ret = false;
30464 else
30466 struct cl_target_option *caller_opts = TREE_TARGET_OPTION (caller_tree);
30467 struct cl_target_option *callee_opts = TREE_TARGET_OPTION (callee_tree);
30469 /* Callee's options should a subset of the caller's, i.e. a vsx function
30470 can inline an altivec function but a non-vsx function can't inline a
30471 vsx function. */
30472 if ((caller_opts->x_rs6000_isa_flags & callee_opts->x_rs6000_isa_flags)
30473 == callee_opts->x_rs6000_isa_flags)
30474 ret = true;
30477 if (TARGET_DEBUG_TARGET)
30478 fprintf (stderr, "rs6000_can_inline_p:, caller %s, callee %s, %s inline\n",
30479 (DECL_NAME (caller)
30480 ? IDENTIFIER_POINTER (DECL_NAME (caller))
30481 : "<unknown>"),
30482 (DECL_NAME (callee)
30483 ? IDENTIFIER_POINTER (DECL_NAME (callee))
30484 : "<unknown>"),
30485 (ret ? "can" : "cannot"));
30487 return ret;
30490 /* Allocate a stack temp and fixup the address so it meets the particular
30491 memory requirements (either offetable or REG+REG addressing). */
30494 rs6000_allocate_stack_temp (enum machine_mode mode,
30495 bool offsettable_p,
30496 bool reg_reg_p)
30498 rtx stack = assign_stack_temp (mode, GET_MODE_SIZE (mode));
30499 rtx addr = XEXP (stack, 0);
30500 int strict_p = (reload_in_progress || reload_completed);
30502 if (!legitimate_indirect_address_p (addr, strict_p))
30504 if (offsettable_p
30505 && !rs6000_legitimate_offset_address_p (mode, addr, strict_p, true))
30506 stack = replace_equiv_address (stack, copy_addr_to_reg (addr));
30508 else if (reg_reg_p && !legitimate_indexed_address_p (addr, strict_p))
30509 stack = replace_equiv_address (stack, copy_addr_to_reg (addr));
30512 return stack;
30515 /* Given a memory reference, if it is not a reg or reg+reg addressing, convert
30516 to such a form to deal with memory reference instructions like STFIWX that
30517 only take reg+reg addressing. */
30520 rs6000_address_for_fpconvert (rtx x)
30522 int strict_p = (reload_in_progress || reload_completed);
30523 rtx addr;
30525 gcc_assert (MEM_P (x));
30526 addr = XEXP (x, 0);
30527 if (! legitimate_indirect_address_p (addr, strict_p)
30528 && ! legitimate_indexed_address_p (addr, strict_p))
30530 if (GET_CODE (addr) == PRE_INC || GET_CODE (addr) == PRE_DEC)
30532 rtx reg = XEXP (addr, 0);
30533 HOST_WIDE_INT size = GET_MODE_SIZE (GET_MODE (x));
30534 rtx size_rtx = GEN_INT ((GET_CODE (addr) == PRE_DEC) ? -size : size);
30535 gcc_assert (REG_P (reg));
30536 emit_insn (gen_add3_insn (reg, reg, size_rtx));
30537 addr = reg;
30539 else if (GET_CODE (addr) == PRE_MODIFY)
30541 rtx reg = XEXP (addr, 0);
30542 rtx expr = XEXP (addr, 1);
30543 gcc_assert (REG_P (reg));
30544 gcc_assert (GET_CODE (expr) == PLUS);
30545 emit_insn (gen_add3_insn (reg, XEXP (expr, 0), XEXP (expr, 1)));
30546 addr = reg;
30549 x = replace_equiv_address (x, copy_addr_to_reg (addr));
30552 return x;
30555 /* Given a memory reference, if it is not in the form for altivec memory
30556 reference instructions (i.e. reg or reg+reg addressing with AND of -16),
30557 convert to the altivec format. */
30560 rs6000_address_for_altivec (rtx x)
30562 gcc_assert (MEM_P (x));
30563 if (!altivec_indexed_or_indirect_operand (x, GET_MODE (x)))
30565 rtx addr = XEXP (x, 0);
30566 int strict_p = (reload_in_progress || reload_completed);
30568 if (!legitimate_indexed_address_p (addr, strict_p)
30569 && !legitimate_indirect_address_p (addr, strict_p))
30570 addr = copy_to_mode_reg (Pmode, addr);
30572 addr = gen_rtx_AND (Pmode, addr, GEN_INT (-16));
30573 x = change_address (x, GET_MODE (x), addr);
30576 return x;
30579 /* Implement TARGET_LEGITIMATE_CONSTANT_P.
30581 On the RS/6000, all integer constants are acceptable, most won't be valid
30582 for particular insns, though. Only easy FP constants are acceptable. */
30584 static bool
30585 rs6000_legitimate_constant_p (enum machine_mode mode, rtx x)
30587 if (TARGET_ELF && rs6000_tls_referenced_p (x))
30588 return false;
30590 return ((GET_CODE (x) != CONST_DOUBLE && GET_CODE (x) != CONST_VECTOR)
30591 || GET_MODE (x) == VOIDmode
30592 || (TARGET_POWERPC64 && mode == DImode)
30593 || easy_fp_constant (x, mode)
30594 || easy_vector_constant (x, mode));
30598 /* A function pointer under AIX is a pointer to a data area whose first word
30599 contains the actual address of the function, whose second word contains a
30600 pointer to its TOC, and whose third word contains a value to place in the
30601 static chain register (r11). Note that if we load the static chain, our
30602 "trampoline" need not have any executable code. */
30604 void
30605 rs6000_call_indirect_aix (rtx value, rtx func_desc, rtx flag)
30607 rtx func_addr;
30608 rtx toc_reg;
30609 rtx sc_reg;
30610 rtx stack_ptr;
30611 rtx stack_toc_offset;
30612 rtx stack_toc_mem;
30613 rtx func_toc_offset;
30614 rtx func_toc_mem;
30615 rtx func_sc_offset;
30616 rtx func_sc_mem;
30617 rtx insn;
30618 rtx (*call_func) (rtx, rtx, rtx, rtx);
30619 rtx (*call_value_func) (rtx, rtx, rtx, rtx, rtx);
30621 stack_ptr = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
30622 toc_reg = gen_rtx_REG (Pmode, TOC_REGNUM);
30624 /* Load up address of the actual function. */
30625 func_desc = force_reg (Pmode, func_desc);
30626 func_addr = gen_reg_rtx (Pmode);
30627 emit_move_insn (func_addr, gen_rtx_MEM (Pmode, func_desc));
30629 if (TARGET_32BIT)
30632 stack_toc_offset = GEN_INT (TOC_SAVE_OFFSET_32BIT);
30633 func_toc_offset = GEN_INT (AIX_FUNC_DESC_TOC_32BIT);
30634 func_sc_offset = GEN_INT (AIX_FUNC_DESC_SC_32BIT);
30635 if (TARGET_POINTERS_TO_NESTED_FUNCTIONS)
30637 call_func = gen_call_indirect_aix32bit;
30638 call_value_func = gen_call_value_indirect_aix32bit;
30640 else
30642 call_func = gen_call_indirect_aix32bit_nor11;
30643 call_value_func = gen_call_value_indirect_aix32bit_nor11;
30646 else
30648 stack_toc_offset = GEN_INT (TOC_SAVE_OFFSET_64BIT);
30649 func_toc_offset = GEN_INT (AIX_FUNC_DESC_TOC_64BIT);
30650 func_sc_offset = GEN_INT (AIX_FUNC_DESC_SC_64BIT);
30651 if (TARGET_POINTERS_TO_NESTED_FUNCTIONS)
30653 call_func = gen_call_indirect_aix64bit;
30654 call_value_func = gen_call_value_indirect_aix64bit;
30656 else
30658 call_func = gen_call_indirect_aix64bit_nor11;
30659 call_value_func = gen_call_value_indirect_aix64bit_nor11;
30663 /* Reserved spot to store the TOC. */
30664 stack_toc_mem = gen_frame_mem (Pmode,
30665 gen_rtx_PLUS (Pmode,
30666 stack_ptr,
30667 stack_toc_offset));
30669 gcc_assert (cfun);
30670 gcc_assert (cfun->machine);
30672 /* Can we optimize saving the TOC in the prologue or do we need to do it at
30673 every call? */
30674 if (TARGET_SAVE_TOC_INDIRECT && !cfun->calls_alloca)
30675 cfun->machine->save_toc_in_prologue = true;
30677 else
30679 MEM_VOLATILE_P (stack_toc_mem) = 1;
30680 emit_move_insn (stack_toc_mem, toc_reg);
30683 /* Calculate the address to load the TOC of the called function. We don't
30684 actually load this until the split after reload. */
30685 func_toc_mem = gen_rtx_MEM (Pmode,
30686 gen_rtx_PLUS (Pmode,
30687 func_desc,
30688 func_toc_offset));
30690 /* If we have a static chain, load it up. */
30691 if (TARGET_POINTERS_TO_NESTED_FUNCTIONS)
30693 func_sc_mem = gen_rtx_MEM (Pmode,
30694 gen_rtx_PLUS (Pmode,
30695 func_desc,
30696 func_sc_offset));
30698 sc_reg = gen_rtx_REG (Pmode, STATIC_CHAIN_REGNUM);
30699 emit_move_insn (sc_reg, func_sc_mem);
30702 /* Create the call. */
30703 if (value)
30704 insn = call_value_func (value, func_addr, flag, func_toc_mem,
30705 stack_toc_mem);
30706 else
30707 insn = call_func (func_addr, flag, func_toc_mem, stack_toc_mem);
30709 emit_call_insn (insn);
30712 /* Return whether we need to always update the saved TOC pointer when we update
30713 the stack pointer. */
30715 static bool
30716 rs6000_save_toc_in_prologue_p (void)
30718 return (cfun && cfun->machine && cfun->machine->save_toc_in_prologue);
30721 #ifdef HAVE_GAS_HIDDEN
30722 # define USE_HIDDEN_LINKONCE 1
30723 #else
30724 # define USE_HIDDEN_LINKONCE 0
30725 #endif
30727 /* Fills in the label name that should be used for a 476 link stack thunk. */
30729 void
30730 get_ppc476_thunk_name (char name[32])
30732 gcc_assert (TARGET_LINK_STACK);
30734 if (USE_HIDDEN_LINKONCE)
30735 sprintf (name, "__ppc476.get_thunk");
30736 else
30737 ASM_GENERATE_INTERNAL_LABEL (name, "LPPC476_", 0);
30740 /* This function emits the simple thunk routine that is used to preserve
30741 the link stack on the 476 cpu. */
30743 static void rs6000_code_end (void) ATTRIBUTE_UNUSED;
30744 static void
30745 rs6000_code_end (void)
30747 char name[32];
30748 tree decl;
30750 if (!TARGET_LINK_STACK)
30751 return;
30753 get_ppc476_thunk_name (name);
30755 decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL, get_identifier (name),
30756 build_function_type_list (void_type_node, NULL_TREE));
30757 DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
30758 NULL_TREE, void_type_node);
30759 TREE_PUBLIC (decl) = 1;
30760 TREE_STATIC (decl) = 1;
30762 #if RS6000_WEAK
30763 if (USE_HIDDEN_LINKONCE)
30765 DECL_COMDAT_GROUP (decl) = DECL_ASSEMBLER_NAME (decl);
30766 targetm.asm_out.unique_section (decl, 0);
30767 switch_to_section (get_named_section (decl, NULL, 0));
30768 DECL_WEAK (decl) = 1;
30769 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
30770 targetm.asm_out.globalize_label (asm_out_file, name);
30771 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
30772 ASM_DECLARE_FUNCTION_NAME (asm_out_file, name, decl);
30774 else
30775 #endif
30777 switch_to_section (text_section);
30778 ASM_OUTPUT_LABEL (asm_out_file, name);
30781 DECL_INITIAL (decl) = make_node (BLOCK);
30782 current_function_decl = decl;
30783 init_function_start (decl);
30784 first_function_block_is_cold = false;
30785 /* Make sure unwind info is emitted for the thunk if needed. */
30786 final_start_function (emit_barrier (), asm_out_file, 1);
30788 fputs ("\tblr\n", asm_out_file);
30790 final_end_function ();
30791 init_insn_lengths ();
30792 free_after_compilation (cfun);
30793 set_cfun (NULL);
30794 current_function_decl = NULL;
30797 /* Add r30 to hard reg set if the prologue sets it up and it is not
30798 pic_offset_table_rtx. */
30800 static void
30801 rs6000_set_up_by_prologue (struct hard_reg_set_container *set)
30803 if (!TARGET_SINGLE_PIC_BASE
30804 && TARGET_TOC
30805 && TARGET_MINIMAL_TOC
30806 && get_pool_size () != 0)
30807 add_to_hard_reg_set (&set->set, Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
30811 /* Helper function for rs6000_split_logical to emit a logical instruction after
30812 spliting the operation to single GPR registers.
30814 DEST is the destination register.
30815 OP1 and OP2 are the input source registers.
30816 CODE is the base operation (AND, IOR, XOR, NOT).
30817 MODE is the machine mode.
30818 If COMPLEMENT_FINAL_P is true, wrap the whole operation with NOT.
30819 If COMPLEMENT_OP1_P is true, wrap operand1 with NOT.
30820 If COMPLEMENT_OP2_P is true, wrap operand2 with NOT.
30821 CLOBBER_REG is either NULL or a scratch register of type CC to allow
30822 formation of the AND instructions. */
30824 static void
30825 rs6000_split_logical_inner (rtx dest,
30826 rtx op1,
30827 rtx op2,
30828 enum rtx_code code,
30829 enum machine_mode mode,
30830 bool complement_final_p,
30831 bool complement_op1_p,
30832 bool complement_op2_p,
30833 rtx clobber_reg)
30835 rtx bool_rtx;
30836 rtx set_rtx;
30838 /* Optimize AND of 0/0xffffffff and IOR/XOR of 0. */
30839 if (op2 && GET_CODE (op2) == CONST_INT
30840 && (mode == SImode || (mode == DImode && TARGET_POWERPC64))
30841 && !complement_final_p && !complement_op1_p && !complement_op2_p)
30843 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
30844 HOST_WIDE_INT value = INTVAL (op2) & mask;
30846 /* Optimize AND of 0 to just set 0. Optimize AND of -1 to be a move. */
30847 if (code == AND)
30849 if (value == 0)
30851 emit_insn (gen_rtx_SET (VOIDmode, dest, const0_rtx));
30852 return;
30855 else if (value == mask)
30857 if (!rtx_equal_p (dest, op1))
30858 emit_insn (gen_rtx_SET (VOIDmode, dest, op1));
30859 return;
30863 /* Optimize IOR/XOR of 0 to be a simple move. Split large operations
30864 into separate ORI/ORIS or XORI/XORIS instrucitons. */
30865 else if (code == IOR || code == XOR)
30867 if (value == 0)
30869 if (!rtx_equal_p (dest, op1))
30870 emit_insn (gen_rtx_SET (VOIDmode, dest, op1));
30871 return;
30876 if (complement_op1_p)
30877 op1 = gen_rtx_NOT (mode, op1);
30879 if (complement_op2_p)
30880 op2 = gen_rtx_NOT (mode, op2);
30882 bool_rtx = ((code == NOT)
30883 ? gen_rtx_NOT (mode, op1)
30884 : gen_rtx_fmt_ee (code, mode, op1, op2));
30886 if (complement_final_p)
30887 bool_rtx = gen_rtx_NOT (mode, bool_rtx);
30889 set_rtx = gen_rtx_SET (VOIDmode, dest, bool_rtx);
30891 /* Is this AND with an explicit clobber? */
30892 if (clobber_reg)
30894 rtx clobber = gen_rtx_CLOBBER (VOIDmode, clobber_reg);
30895 set_rtx = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set_rtx, clobber));
30898 emit_insn (set_rtx);
30899 return;
30902 /* Split a DImode AND/IOR/XOR with a constant on a 32-bit system. These
30903 operations are split immediately during RTL generation to allow for more
30904 optimizations of the AND/IOR/XOR.
30906 OPERANDS is an array containing the destination and two input operands.
30907 CODE is the base operation (AND, IOR, XOR, NOT).
30908 MODE is the machine mode.
30909 If COMPLEMENT_FINAL_P is true, wrap the whole operation with NOT.
30910 If COMPLEMENT_OP1_P is true, wrap operand1 with NOT.
30911 If COMPLEMENT_OP2_P is true, wrap operand2 with NOT.
30912 CLOBBER_REG is either NULL or a scratch register of type CC to allow
30913 formation of the AND instructions. */
30915 static void
30916 rs6000_split_logical_di (rtx operands[3],
30917 enum rtx_code code,
30918 bool complement_final_p,
30919 bool complement_op1_p,
30920 bool complement_op2_p,
30921 rtx clobber_reg)
30923 const HOST_WIDE_INT lower_32bits = HOST_WIDE_INT_C(0xffffffff);
30924 const HOST_WIDE_INT upper_32bits = ~ lower_32bits;
30925 const HOST_WIDE_INT sign_bit = HOST_WIDE_INT_C(0x80000000);
30926 enum hi_lo { hi = 0, lo = 1 };
30927 rtx op0_hi_lo[2], op1_hi_lo[2], op2_hi_lo[2];
30928 size_t i;
30930 op0_hi_lo[hi] = gen_highpart (SImode, operands[0]);
30931 op1_hi_lo[hi] = gen_highpart (SImode, operands[1]);
30932 op0_hi_lo[lo] = gen_lowpart (SImode, operands[0]);
30933 op1_hi_lo[lo] = gen_lowpart (SImode, operands[1]);
30935 if (code == NOT)
30936 op2_hi_lo[hi] = op2_hi_lo[lo] = NULL_RTX;
30937 else
30939 if (GET_CODE (operands[2]) != CONST_INT)
30941 op2_hi_lo[hi] = gen_highpart_mode (SImode, DImode, operands[2]);
30942 op2_hi_lo[lo] = gen_lowpart (SImode, operands[2]);
30944 else
30946 HOST_WIDE_INT value = INTVAL (operands[2]);
30947 HOST_WIDE_INT value_hi_lo[2];
30949 gcc_assert (!complement_final_p);
30950 gcc_assert (!complement_op1_p);
30951 gcc_assert (!complement_op2_p);
30953 value_hi_lo[hi] = value >> 32;
30954 value_hi_lo[lo] = value & lower_32bits;
30956 for (i = 0; i < 2; i++)
30958 HOST_WIDE_INT sub_value = value_hi_lo[i];
30960 if (sub_value & sign_bit)
30961 sub_value |= upper_32bits;
30963 op2_hi_lo[i] = GEN_INT (sub_value);
30965 /* If this is an AND instruction, check to see if we need to load
30966 the value in a register. */
30967 if (code == AND && sub_value != -1 && sub_value != 0
30968 && !and_operand (op2_hi_lo[i], SImode))
30969 op2_hi_lo[i] = force_reg (SImode, op2_hi_lo[i]);
30974 for (i = 0; i < 2; i++)
30976 /* Split large IOR/XOR operations. */
30977 if ((code == IOR || code == XOR)
30978 && GET_CODE (op2_hi_lo[i]) == CONST_INT
30979 && !complement_final_p
30980 && !complement_op1_p
30981 && !complement_op2_p
30982 && clobber_reg == NULL_RTX
30983 && !logical_const_operand (op2_hi_lo[i], SImode))
30985 HOST_WIDE_INT value = INTVAL (op2_hi_lo[i]);
30986 HOST_WIDE_INT hi_16bits = value & HOST_WIDE_INT_C(0xffff0000);
30987 HOST_WIDE_INT lo_16bits = value & HOST_WIDE_INT_C(0x0000ffff);
30988 rtx tmp = gen_reg_rtx (SImode);
30990 /* Make sure the constant is sign extended. */
30991 if ((hi_16bits & sign_bit) != 0)
30992 hi_16bits |= upper_32bits;
30994 rs6000_split_logical_inner (tmp, op1_hi_lo[i], GEN_INT (hi_16bits),
30995 code, SImode, false, false, false,
30996 NULL_RTX);
30998 rs6000_split_logical_inner (op0_hi_lo[i], tmp, GEN_INT (lo_16bits),
30999 code, SImode, false, false, false,
31000 NULL_RTX);
31002 else
31003 rs6000_split_logical_inner (op0_hi_lo[i], op1_hi_lo[i], op2_hi_lo[i],
31004 code, SImode, complement_final_p,
31005 complement_op1_p, complement_op2_p,
31006 clobber_reg);
31009 return;
31012 /* Split the insns that make up boolean operations operating on multiple GPR
31013 registers. The boolean MD patterns ensure that the inputs either are
31014 exactly the same as the output registers, or there is no overlap.
31016 OPERANDS is an array containing the destination and two input operands.
31017 CODE is the base operation (AND, IOR, XOR, NOT).
31018 MODE is the machine mode.
31019 If COMPLEMENT_FINAL_P is true, wrap the whole operation with NOT.
31020 If COMPLEMENT_OP1_P is true, wrap operand1 with NOT.
31021 If COMPLEMENT_OP2_P is true, wrap operand2 with NOT.
31022 CLOBBER_REG is either NULL or a scratch register of type CC to allow
31023 formation of the AND instructions. */
31025 void
31026 rs6000_split_logical (rtx operands[3],
31027 enum rtx_code code,
31028 bool complement_final_p,
31029 bool complement_op1_p,
31030 bool complement_op2_p,
31031 rtx clobber_reg)
31033 enum machine_mode mode = GET_MODE (operands[0]);
31034 enum machine_mode sub_mode;
31035 rtx op0, op1, op2;
31036 int sub_size, regno0, regno1, nregs, i;
31038 /* If this is DImode, use the specialized version that can run before
31039 register allocation. */
31040 if (mode == DImode && !TARGET_POWERPC64)
31042 rs6000_split_logical_di (operands, code, complement_final_p,
31043 complement_op1_p, complement_op2_p,
31044 clobber_reg);
31045 return;
31048 op0 = operands[0];
31049 op1 = operands[1];
31050 op2 = (code == NOT) ? NULL_RTX : operands[2];
31051 sub_mode = (TARGET_POWERPC64) ? DImode : SImode;
31052 sub_size = GET_MODE_SIZE (sub_mode);
31053 regno0 = REGNO (op0);
31054 regno1 = REGNO (op1);
31056 gcc_assert (reload_completed);
31057 gcc_assert (IN_RANGE (regno0, FIRST_GPR_REGNO, LAST_GPR_REGNO));
31058 gcc_assert (IN_RANGE (regno1, FIRST_GPR_REGNO, LAST_GPR_REGNO));
31060 nregs = rs6000_hard_regno_nregs[(int)mode][regno0];
31061 gcc_assert (nregs > 1);
31063 if (op2 && REG_P (op2))
31064 gcc_assert (IN_RANGE (REGNO (op2), FIRST_GPR_REGNO, LAST_GPR_REGNO));
31066 for (i = 0; i < nregs; i++)
31068 int offset = i * sub_size;
31069 rtx sub_op0 = simplify_subreg (sub_mode, op0, mode, offset);
31070 rtx sub_op1 = simplify_subreg (sub_mode, op1, mode, offset);
31071 rtx sub_op2 = ((code == NOT)
31072 ? NULL_RTX
31073 : simplify_subreg (sub_mode, op2, mode, offset));
31075 rs6000_split_logical_inner (sub_op0, sub_op1, sub_op2, code, sub_mode,
31076 complement_final_p, complement_op1_p,
31077 complement_op2_p, clobber_reg);
31080 return;
31084 /* Return true if the peephole2 can combine a load involving a combination of
31085 an addis instruction and a load with an offset that can be fused together on
31086 a power8.
31088 The operands are:
31089 operands[0] register set with addis
31090 operands[1] value set via addis
31091 operands[2] target register being loaded
31092 operands[3] D-form memory reference using operands[0].
31094 In addition, we are passed a boolean that is true if this is a peephole2,
31095 and we can use see if the addis_reg is dead after the insn and can be
31096 replaced by the target register. */
31098 bool
31099 fusion_gpr_load_p (rtx *operands, bool peep2_p)
31101 rtx addis_reg = operands[0];
31102 rtx addis_value = operands[1];
31103 rtx target = operands[2];
31104 rtx mem = operands[3];
31105 rtx addr;
31106 rtx base_reg;
31108 /* Validate arguments. */
31109 if (!base_reg_operand (addis_reg, GET_MODE (addis_reg)))
31110 return false;
31112 if (!base_reg_operand (target, GET_MODE (target)))
31113 return false;
31115 if (!fusion_gpr_addis (addis_value, GET_MODE (addis_value)))
31116 return false;
31118 if (!fusion_gpr_mem_load (mem, GET_MODE (mem)))
31119 return false;
31121 /* Allow sign/zero extension. */
31122 if (GET_CODE (mem) == ZERO_EXTEND
31123 || (GET_CODE (mem) == SIGN_EXTEND && TARGET_P8_FUSION_SIGN))
31124 mem = XEXP (mem, 0);
31126 if (!MEM_P (mem))
31127 return false;
31129 addr = XEXP (mem, 0); /* either PLUS or LO_SUM. */
31130 if (GET_CODE (addr) != PLUS && GET_CODE (addr) != LO_SUM)
31131 return false;
31133 /* Validate that the register used to load the high value is either the
31134 register being loaded, or we can safely replace its use in a peephole2.
31136 If this is a peephole2, we assume that there are 2 instructions in the
31137 peephole (addis and load), so we want to check if the target register was
31138 not used in the memory address and the register to hold the addis result
31139 is dead after the peephole. */
31140 if (REGNO (addis_reg) != REGNO (target))
31142 if (!peep2_p)
31143 return false;
31145 if (reg_mentioned_p (target, mem))
31146 return false;
31148 if (!peep2_reg_dead_p (2, addis_reg))
31149 return false;
31152 base_reg = XEXP (addr, 0);
31153 return REGNO (addis_reg) == REGNO (base_reg);
31156 /* During the peephole2 pass, adjust and expand the insns for a load fusion
31157 sequence. We adjust the addis register to use the target register. If the
31158 load sign extends, we adjust the code to do the zero extending load, and an
31159 explicit sign extension later since the fusion only covers zero extending
31160 loads.
31162 The operands are:
31163 operands[0] register set with addis (to be replaced with target)
31164 operands[1] value set via addis
31165 operands[2] target register being loaded
31166 operands[3] D-form memory reference using operands[0]. */
31168 void
31169 expand_fusion_gpr_load (rtx *operands)
31171 rtx addis_value = operands[1];
31172 rtx target = operands[2];
31173 rtx orig_mem = operands[3];
31174 rtx new_addr, new_mem, orig_addr, offset;
31175 enum rtx_code plus_or_lo_sum;
31176 enum machine_mode target_mode = GET_MODE (target);
31177 enum machine_mode extend_mode = target_mode;
31178 enum machine_mode ptr_mode = Pmode;
31179 enum rtx_code extend = UNKNOWN;
31180 rtx addis_reg = ((ptr_mode == target_mode)
31181 ? target
31182 : simplify_subreg (ptr_mode, target, target_mode, 0));
31184 if (GET_CODE (orig_mem) == ZERO_EXTEND
31185 || (TARGET_P8_FUSION_SIGN && GET_CODE (orig_mem) == SIGN_EXTEND))
31187 extend = GET_CODE (orig_mem);
31188 orig_mem = XEXP (orig_mem, 0);
31189 target_mode = GET_MODE (orig_mem);
31192 gcc_assert (MEM_P (orig_mem));
31194 orig_addr = XEXP (orig_mem, 0);
31195 plus_or_lo_sum = GET_CODE (orig_addr);
31196 gcc_assert (plus_or_lo_sum == PLUS || plus_or_lo_sum == LO_SUM);
31198 offset = XEXP (orig_addr, 1);
31199 new_addr = gen_rtx_fmt_ee (plus_or_lo_sum, ptr_mode, addis_reg, offset);
31200 new_mem = change_address (orig_mem, target_mode, new_addr);
31202 if (extend != UNKNOWN)
31203 new_mem = gen_rtx_fmt_e (ZERO_EXTEND, extend_mode, new_mem);
31205 emit_insn (gen_rtx_SET (VOIDmode, addis_reg, addis_value));
31206 emit_insn (gen_rtx_SET (VOIDmode, target, new_mem));
31208 if (extend == SIGN_EXTEND)
31210 int sub_off = ((BYTES_BIG_ENDIAN)
31211 ? GET_MODE_SIZE (extend_mode) - GET_MODE_SIZE (target_mode)
31212 : 0);
31213 rtx sign_reg
31214 = simplify_subreg (target_mode, target, extend_mode, sub_off);
31216 emit_insn (gen_rtx_SET (VOIDmode, target,
31217 gen_rtx_SIGN_EXTEND (extend_mode, sign_reg)));
31220 return;
31223 /* Return a string to fuse an addis instruction with a gpr load to the same
31224 register that we loaded up the addis instruction. The code is complicated,
31225 so we call output_asm_insn directly, and just return "".
31227 The operands are:
31228 operands[0] register set with addis (must be same reg as target).
31229 operands[1] value set via addis
31230 operands[2] target register being loaded
31231 operands[3] D-form memory reference using operands[0]. */
31233 const char *
31234 emit_fusion_gpr_load (rtx *operands)
31236 rtx addis_reg = operands[0];
31237 rtx addis_value = operands[1];
31238 rtx target = operands[2];
31239 rtx mem = operands[3];
31240 rtx fuse_ops[10];
31241 rtx addr;
31242 rtx load_offset;
31243 const char *addis_str = NULL;
31244 const char *load_str = NULL;
31245 const char *extend_insn = NULL;
31246 const char *mode_name = NULL;
31247 char insn_template[80];
31248 enum machine_mode mode;
31249 const char *comment_str = ASM_COMMENT_START;
31250 bool sign_p = false;
31252 gcc_assert (REG_P (addis_reg) && REG_P (target));
31253 gcc_assert (REGNO (addis_reg) == REGNO (target));
31255 if (*comment_str == ' ')
31256 comment_str++;
31258 /* Allow sign/zero extension. */
31259 if (GET_CODE (mem) == ZERO_EXTEND)
31260 mem = XEXP (mem, 0);
31262 else if (GET_CODE (mem) == SIGN_EXTEND && TARGET_P8_FUSION_SIGN)
31264 sign_p = true;
31265 mem = XEXP (mem, 0);
31268 gcc_assert (MEM_P (mem));
31269 addr = XEXP (mem, 0);
31270 if (GET_CODE (addr) != PLUS && GET_CODE (addr) != LO_SUM)
31271 gcc_unreachable ();
31273 load_offset = XEXP (addr, 1);
31275 /* Now emit the load instruction to the same register. */
31276 mode = GET_MODE (mem);
31277 switch (mode)
31279 case QImode:
31280 mode_name = "char";
31281 load_str = "lbz";
31282 extend_insn = "extsb %0,%0";
31283 break;
31285 case HImode:
31286 mode_name = "short";
31287 load_str = "lhz";
31288 extend_insn = "extsh %0,%0";
31289 break;
31291 case SImode:
31292 mode_name = "int";
31293 load_str = "lwz";
31294 extend_insn = "extsw %0,%0";
31295 break;
31297 case DImode:
31298 if (TARGET_POWERPC64)
31300 mode_name = "long";
31301 load_str = "ld";
31303 else
31304 gcc_unreachable ();
31305 break;
31307 default:
31308 gcc_unreachable ();
31311 /* Emit the addis instruction. */
31312 fuse_ops[0] = target;
31313 if (satisfies_constraint_L (addis_value))
31315 fuse_ops[1] = addis_value;
31316 addis_str = "lis %0,%v1";
31319 else if (GET_CODE (addis_value) == PLUS)
31321 rtx op0 = XEXP (addis_value, 0);
31322 rtx op1 = XEXP (addis_value, 1);
31324 if (REG_P (op0) && CONST_INT_P (op1)
31325 && satisfies_constraint_L (op1))
31327 fuse_ops[1] = op0;
31328 fuse_ops[2] = op1;
31329 addis_str = "addis %0,%1,%v2";
31333 else if (GET_CODE (addis_value) == HIGH)
31335 rtx value = XEXP (addis_value, 0);
31336 if (GET_CODE (value) == UNSPEC && XINT (value, 1) == UNSPEC_TOCREL)
31338 fuse_ops[1] = XVECEXP (value, 0, 0); /* symbol ref. */
31339 fuse_ops[2] = XVECEXP (value, 0, 1); /* TOC register. */
31340 if (TARGET_ELF)
31341 addis_str = "addis %0,%2,%1@toc@ha";
31343 else if (TARGET_XCOFF)
31344 addis_str = "addis %0,%1@u(%2)";
31346 else
31347 gcc_unreachable ();
31350 else if (GET_CODE (value) == PLUS)
31352 rtx op0 = XEXP (value, 0);
31353 rtx op1 = XEXP (value, 1);
31355 if (GET_CODE (op0) == UNSPEC
31356 && XINT (op0, 1) == UNSPEC_TOCREL
31357 && CONST_INT_P (op1))
31359 fuse_ops[1] = XVECEXP (op0, 0, 0); /* symbol ref. */
31360 fuse_ops[2] = XVECEXP (op0, 0, 1); /* TOC register. */
31361 fuse_ops[3] = op1;
31362 if (TARGET_ELF)
31363 addis_str = "addis %0,%2,%1+%3@toc@ha";
31365 else if (TARGET_XCOFF)
31366 addis_str = "addis %0,%1+%3@u(%2)";
31368 else
31369 gcc_unreachable ();
31373 else if (satisfies_constraint_L (value))
31375 fuse_ops[1] = value;
31376 addis_str = "lis %0,%v1";
31379 else if (TARGET_ELF && !TARGET_POWERPC64 && CONSTANT_P (value))
31381 fuse_ops[1] = value;
31382 addis_str = "lis %0,%1@ha";
31386 if (!addis_str)
31387 fatal_insn ("Could not generate addis value for fusion", addis_value);
31389 sprintf (insn_template, "%s\t\t%s gpr load fusion, type %s", addis_str,
31390 comment_str, mode_name);
31391 output_asm_insn (insn_template, fuse_ops);
31393 /* Emit the D-form load instruction. */
31394 if (CONST_INT_P (load_offset) && satisfies_constraint_I (load_offset))
31396 sprintf (insn_template, "%s %%0,%%1(%%0)", load_str);
31397 fuse_ops[1] = load_offset;
31398 output_asm_insn (insn_template, fuse_ops);
31401 else if (GET_CODE (load_offset) == UNSPEC
31402 && XINT (load_offset, 1) == UNSPEC_TOCREL)
31404 if (TARGET_ELF)
31405 sprintf (insn_template, "%s %%0,%%1@toc@l(%%0)", load_str);
31407 else if (TARGET_XCOFF)
31408 sprintf (insn_template, "%s %%0,%%1@l(%%0)", load_str);
31410 else
31411 gcc_unreachable ();
31413 fuse_ops[1] = XVECEXP (load_offset, 0, 0);
31414 output_asm_insn (insn_template, fuse_ops);
31417 else if (GET_CODE (load_offset) == PLUS
31418 && GET_CODE (XEXP (load_offset, 0)) == UNSPEC
31419 && XINT (XEXP (load_offset, 0), 1) == UNSPEC_TOCREL
31420 && CONST_INT_P (XEXP (load_offset, 1)))
31422 rtx tocrel_unspec = XEXP (load_offset, 0);
31423 if (TARGET_ELF)
31424 sprintf (insn_template, "%s %%0,%%1+%%2@toc@l(%%0)", load_str);
31426 else if (TARGET_XCOFF)
31427 sprintf (insn_template, "%s %%0,%%1+%%2@l(%%0)", load_str);
31429 else
31430 gcc_unreachable ();
31432 fuse_ops[1] = XVECEXP (tocrel_unspec, 0, 0);
31433 fuse_ops[2] = XEXP (load_offset, 1);
31434 output_asm_insn (insn_template, fuse_ops);
31437 else if (TARGET_ELF && !TARGET_POWERPC64 && CONSTANT_P (load_offset))
31439 sprintf (insn_template, "%s %%0,%%1@l(%%0)", load_str);
31441 fuse_ops[1] = load_offset;
31442 output_asm_insn (insn_template, fuse_ops);
31445 else
31446 fatal_insn ("Unable to generate load offset for fusion", load_offset);
31448 /* Handle sign extension. The peephole2 pass generates this as a separate
31449 insn, but we handle it just in case it got reattached. */
31450 if (sign_p)
31452 gcc_assert (extend_insn != NULL);
31453 output_asm_insn (extend_insn, fuse_ops);
31456 return "";
31460 struct gcc_target targetm = TARGET_INITIALIZER;
31462 #include "gt-rs6000.h"