rs6000: New attributes for load/store: "sign_extend", "update" and "indexed"
[official-gcc.git] / gcc / config / rs6000 / rs6000.c
blob2de6125b1c56724389d9db5284c603a0ff20bc65
1 /* Subroutines used for code generation on IBM RS/6000.
2 Copyright (C) 1991-2014 Free Software Foundation, Inc.
3 Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published
9 by the Free Software Foundation; either version 3, or (at your
10 option) any later version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "regs.h"
27 #include "hard-reg-set.h"
28 #include "insn-config.h"
29 #include "conditions.h"
30 #include "insn-attr.h"
31 #include "flags.h"
32 #include "recog.h"
33 #include "obstack.h"
34 #include "tree.h"
35 #include "stringpool.h"
36 #include "stor-layout.h"
37 #include "calls.h"
38 #include "print-tree.h"
39 #include "varasm.h"
40 #include "expr.h"
41 #include "optabs.h"
42 #include "except.h"
43 #include "function.h"
44 #include "output.h"
45 #include "dbxout.h"
46 #include "basic-block.h"
47 #include "diagnostic-core.h"
48 #include "toplev.h"
49 #include "ggc.h"
50 #include "hashtab.h"
51 #include "tm_p.h"
52 #include "target.h"
53 #include "target-def.h"
54 #include "common/common-target.h"
55 #include "langhooks.h"
56 #include "reload.h"
57 #include "cfgloop.h"
58 #include "sched-int.h"
59 #include "pointer-set.h"
60 #include "hash-table.h"
61 #include "vec.h"
62 #include "basic-block.h"
63 #include "tree-ssa-alias.h"
64 #include "internal-fn.h"
65 #include "gimple-fold.h"
66 #include "tree-eh.h"
67 #include "gimple-expr.h"
68 #include "is-a.h"
69 #include "gimple.h"
70 #include "gimplify.h"
71 #include "gimple-iterator.h"
72 #include "gimple-walk.h"
73 #include "intl.h"
74 #include "params.h"
75 #include "tm-constrs.h"
76 #include "ira.h"
77 #include "opts.h"
78 #include "tree-vectorizer.h"
79 #include "dumpfile.h"
80 #include "cgraph.h"
81 #include "target-globals.h"
82 #if TARGET_XCOFF
83 #include "xcoffout.h" /* get declarations of xcoff_*_section_name */
84 #endif
85 #if TARGET_MACHO
86 #include "gstab.h" /* for N_SLINE */
87 #endif
89 #ifndef TARGET_NO_PROTOTYPE
90 #define TARGET_NO_PROTOTYPE 0
91 #endif
93 #define min(A,B) ((A) < (B) ? (A) : (B))
94 #define max(A,B) ((A) > (B) ? (A) : (B))
96 /* Structure used to define the rs6000 stack */
97 typedef struct rs6000_stack {
98 int reload_completed; /* stack info won't change from here on */
99 int first_gp_reg_save; /* first callee saved GP register used */
100 int first_fp_reg_save; /* first callee saved FP register used */
101 int first_altivec_reg_save; /* first callee saved AltiVec register used */
102 int lr_save_p; /* true if the link reg needs to be saved */
103 int cr_save_p; /* true if the CR reg needs to be saved */
104 unsigned int vrsave_mask; /* mask of vec registers to save */
105 int push_p; /* true if we need to allocate stack space */
106 int calls_p; /* true if the function makes any calls */
107 int world_save_p; /* true if we're saving *everything*:
108 r13-r31, cr, f14-f31, vrsave, v20-v31 */
109 enum rs6000_abi abi; /* which ABI to use */
110 int gp_save_offset; /* offset to save GP regs from initial SP */
111 int fp_save_offset; /* offset to save FP regs from initial SP */
112 int altivec_save_offset; /* offset to save AltiVec regs from initial SP */
113 int lr_save_offset; /* offset to save LR from initial SP */
114 int cr_save_offset; /* offset to save CR from initial SP */
115 int vrsave_save_offset; /* offset to save VRSAVE from initial SP */
116 int spe_gp_save_offset; /* offset to save spe 64-bit gprs */
117 int varargs_save_offset; /* offset to save the varargs registers */
118 int ehrd_offset; /* offset to EH return data */
119 int ehcr_offset; /* offset to EH CR field data */
120 int reg_size; /* register size (4 or 8) */
121 HOST_WIDE_INT vars_size; /* variable save area size */
122 int parm_size; /* outgoing parameter size */
123 int save_size; /* save area size */
124 int fixed_size; /* fixed size of stack frame */
125 int gp_size; /* size of saved GP registers */
126 int fp_size; /* size of saved FP registers */
127 int altivec_size; /* size of saved AltiVec registers */
128 int cr_size; /* size to hold CR if not in save_size */
129 int vrsave_size; /* size to hold VRSAVE if not in save_size */
130 int altivec_padding_size; /* size of altivec alignment padding if
131 not in save_size */
132 int spe_gp_size; /* size of 64-bit GPR save size for SPE */
133 int spe_padding_size;
134 HOST_WIDE_INT total_size; /* total bytes allocated for stack */
135 int spe_64bit_regs_used;
136 int savres_strategy;
137 } rs6000_stack_t;
139 /* A C structure for machine-specific, per-function data.
140 This is added to the cfun structure. */
141 typedef struct GTY(()) machine_function
143 /* Some local-dynamic symbol. */
144 const char *some_ld_name;
145 /* Whether the instruction chain has been scanned already. */
146 int insn_chain_scanned_p;
147 /* Flags if __builtin_return_address (n) with n >= 1 was used. */
148 int ra_needs_full_frame;
149 /* Flags if __builtin_return_address (0) was used. */
150 int ra_need_lr;
151 /* Cache lr_save_p after expansion of builtin_eh_return. */
152 int lr_save_state;
153 /* Whether we need to save the TOC to the reserved stack location in the
154 function prologue. */
155 bool save_toc_in_prologue;
156 /* Offset from virtual_stack_vars_rtx to the start of the ABI_V4
157 varargs save area. */
158 HOST_WIDE_INT varargs_save_offset;
159 /* Temporary stack slot to use for SDmode copies. This slot is
160 64-bits wide and is allocated early enough so that the offset
161 does not overflow the 16-bit load/store offset field. */
162 rtx sdmode_stack_slot;
163 /* Flag if r2 setup is needed with ELFv2 ABI. */
164 bool r2_setup_needed;
165 } machine_function;
167 /* Support targetm.vectorize.builtin_mask_for_load. */
168 static GTY(()) tree altivec_builtin_mask_for_load;
170 /* Set to nonzero once AIX common-mode calls have been defined. */
171 static GTY(()) int common_mode_defined;
173 /* Label number of label created for -mrelocatable, to call to so we can
174 get the address of the GOT section */
175 static int rs6000_pic_labelno;
177 #ifdef USING_ELFOS_H
178 /* Counter for labels which are to be placed in .fixup. */
179 int fixuplabelno = 0;
180 #endif
182 /* Whether to use variant of AIX ABI for PowerPC64 Linux. */
183 int dot_symbols;
185 /* Specify the machine mode that pointers have. After generation of rtl, the
186 compiler makes no further distinction between pointers and any other objects
187 of this machine mode. The type is unsigned since not all things that
188 include rs6000.h also include machmode.h. */
189 unsigned rs6000_pmode;
191 /* Width in bits of a pointer. */
192 unsigned rs6000_pointer_size;
194 #ifdef HAVE_AS_GNU_ATTRIBUTE
195 /* Flag whether floating point values have been passed/returned. */
196 static bool rs6000_passes_float;
197 /* Flag whether vector values have been passed/returned. */
198 static bool rs6000_passes_vector;
199 /* Flag whether small (<= 8 byte) structures have been returned. */
200 static bool rs6000_returns_struct;
201 #endif
203 /* Value is TRUE if register/mode pair is acceptable. */
204 bool rs6000_hard_regno_mode_ok_p[NUM_MACHINE_MODES][FIRST_PSEUDO_REGISTER];
206 /* Maximum number of registers needed for a given register class and mode. */
207 unsigned char rs6000_class_max_nregs[NUM_MACHINE_MODES][LIM_REG_CLASSES];
209 /* How many registers are needed for a given register and mode. */
210 unsigned char rs6000_hard_regno_nregs[NUM_MACHINE_MODES][FIRST_PSEUDO_REGISTER];
212 /* Map register number to register class. */
213 enum reg_class rs6000_regno_regclass[FIRST_PSEUDO_REGISTER];
215 static int dbg_cost_ctrl;
217 /* Built in types. */
218 tree rs6000_builtin_types[RS6000_BTI_MAX];
219 tree rs6000_builtin_decls[RS6000_BUILTIN_COUNT];
221 /* Flag to say the TOC is initialized */
222 int toc_initialized;
223 char toc_label_name[10];
225 /* Cached value of rs6000_variable_issue. This is cached in
226 rs6000_variable_issue hook and returned from rs6000_sched_reorder2. */
227 static short cached_can_issue_more;
229 static GTY(()) section *read_only_data_section;
230 static GTY(()) section *private_data_section;
231 static GTY(()) section *tls_data_section;
232 static GTY(()) section *tls_private_data_section;
233 static GTY(()) section *read_only_private_data_section;
234 static GTY(()) section *sdata2_section;
235 static GTY(()) section *toc_section;
237 struct builtin_description
239 const HOST_WIDE_INT mask;
240 const enum insn_code icode;
241 const char *const name;
242 const enum rs6000_builtins code;
245 /* Describe the vector unit used for modes. */
246 enum rs6000_vector rs6000_vector_unit[NUM_MACHINE_MODES];
247 enum rs6000_vector rs6000_vector_mem[NUM_MACHINE_MODES];
249 /* Register classes for various constraints that are based on the target
250 switches. */
251 enum reg_class rs6000_constraints[RS6000_CONSTRAINT_MAX];
253 /* Describe the alignment of a vector. */
254 int rs6000_vector_align[NUM_MACHINE_MODES];
256 /* Map selected modes to types for builtins. */
257 static GTY(()) tree builtin_mode_to_type[MAX_MACHINE_MODE][2];
259 /* What modes to automatically generate reciprocal divide estimate (fre) and
260 reciprocal sqrt (frsqrte) for. */
261 unsigned char rs6000_recip_bits[MAX_MACHINE_MODE];
263 /* Masks to determine which reciprocal esitmate instructions to generate
264 automatically. */
265 enum rs6000_recip_mask {
266 RECIP_SF_DIV = 0x001, /* Use divide estimate */
267 RECIP_DF_DIV = 0x002,
268 RECIP_V4SF_DIV = 0x004,
269 RECIP_V2DF_DIV = 0x008,
271 RECIP_SF_RSQRT = 0x010, /* Use reciprocal sqrt estimate. */
272 RECIP_DF_RSQRT = 0x020,
273 RECIP_V4SF_RSQRT = 0x040,
274 RECIP_V2DF_RSQRT = 0x080,
276 /* Various combination of flags for -mrecip=xxx. */
277 RECIP_NONE = 0,
278 RECIP_ALL = (RECIP_SF_DIV | RECIP_DF_DIV | RECIP_V4SF_DIV
279 | RECIP_V2DF_DIV | RECIP_SF_RSQRT | RECIP_DF_RSQRT
280 | RECIP_V4SF_RSQRT | RECIP_V2DF_RSQRT),
282 RECIP_HIGH_PRECISION = RECIP_ALL,
284 /* On low precision machines like the power5, don't enable double precision
285 reciprocal square root estimate, since it isn't accurate enough. */
286 RECIP_LOW_PRECISION = (RECIP_ALL & ~(RECIP_DF_RSQRT | RECIP_V2DF_RSQRT))
289 /* -mrecip options. */
290 static struct
292 const char *string; /* option name */
293 unsigned int mask; /* mask bits to set */
294 } recip_options[] = {
295 { "all", RECIP_ALL },
296 { "none", RECIP_NONE },
297 { "div", (RECIP_SF_DIV | RECIP_DF_DIV | RECIP_V4SF_DIV
298 | RECIP_V2DF_DIV) },
299 { "divf", (RECIP_SF_DIV | RECIP_V4SF_DIV) },
300 { "divd", (RECIP_DF_DIV | RECIP_V2DF_DIV) },
301 { "rsqrt", (RECIP_SF_RSQRT | RECIP_DF_RSQRT | RECIP_V4SF_RSQRT
302 | RECIP_V2DF_RSQRT) },
303 { "rsqrtf", (RECIP_SF_RSQRT | RECIP_V4SF_RSQRT) },
304 { "rsqrtd", (RECIP_DF_RSQRT | RECIP_V2DF_RSQRT) },
307 /* Pointer to function (in rs6000-c.c) that can define or undefine target
308 macros that have changed. Languages that don't support the preprocessor
309 don't link in rs6000-c.c, so we can't call it directly. */
310 void (*rs6000_target_modify_macros_ptr) (bool, HOST_WIDE_INT, HOST_WIDE_INT);
312 /* Simplfy register classes into simpler classifications. We assume
313 GPR_REG_TYPE - FPR_REG_TYPE are ordered so that we can use a simple range
314 check for standard register classes (gpr/floating/altivec/vsx) and
315 floating/vector classes (float/altivec/vsx). */
317 enum rs6000_reg_type {
318 NO_REG_TYPE,
319 PSEUDO_REG_TYPE,
320 GPR_REG_TYPE,
321 VSX_REG_TYPE,
322 ALTIVEC_REG_TYPE,
323 FPR_REG_TYPE,
324 SPR_REG_TYPE,
325 CR_REG_TYPE,
326 SPE_ACC_TYPE,
327 SPEFSCR_REG_TYPE
330 /* Map register class to register type. */
331 static enum rs6000_reg_type reg_class_to_reg_type[N_REG_CLASSES];
333 /* First/last register type for the 'normal' register types (i.e. general
334 purpose, floating point, altivec, and VSX registers). */
335 #define IS_STD_REG_TYPE(RTYPE) IN_RANGE(RTYPE, GPR_REG_TYPE, FPR_REG_TYPE)
337 #define IS_FP_VECT_REG_TYPE(RTYPE) IN_RANGE(RTYPE, VSX_REG_TYPE, FPR_REG_TYPE)
340 /* Register classes we care about in secondary reload or go if legitimate
341 address. We only need to worry about GPR, FPR, and Altivec registers here,
342 along an ANY field that is the OR of the 3 register classes. */
344 enum rs6000_reload_reg_type {
345 RELOAD_REG_GPR, /* General purpose registers. */
346 RELOAD_REG_FPR, /* Traditional floating point regs. */
347 RELOAD_REG_VMX, /* Altivec (VMX) registers. */
348 RELOAD_REG_ANY, /* OR of GPR, FPR, Altivec masks. */
349 N_RELOAD_REG
352 /* For setting up register classes, loop through the 3 register classes mapping
353 into real registers, and skip the ANY class, which is just an OR of the
354 bits. */
355 #define FIRST_RELOAD_REG_CLASS RELOAD_REG_GPR
356 #define LAST_RELOAD_REG_CLASS RELOAD_REG_VMX
358 /* Map reload register type to a register in the register class. */
359 struct reload_reg_map_type {
360 const char *name; /* Register class name. */
361 int reg; /* Register in the register class. */
364 static const struct reload_reg_map_type reload_reg_map[N_RELOAD_REG] = {
365 { "Gpr", FIRST_GPR_REGNO }, /* RELOAD_REG_GPR. */
366 { "Fpr", FIRST_FPR_REGNO }, /* RELOAD_REG_FPR. */
367 { "VMX", FIRST_ALTIVEC_REGNO }, /* RELOAD_REG_VMX. */
368 { "Any", -1 }, /* RELOAD_REG_ANY. */
371 /* Mask bits for each register class, indexed per mode. Historically the
372 compiler has been more restrictive which types can do PRE_MODIFY instead of
373 PRE_INC and PRE_DEC, so keep track of sepaate bits for these two. */
374 typedef unsigned char addr_mask_type;
376 #define RELOAD_REG_VALID 0x01 /* Mode valid in register.. */
377 #define RELOAD_REG_MULTIPLE 0x02 /* Mode takes multiple registers. */
378 #define RELOAD_REG_INDEXED 0x04 /* Reg+reg addressing. */
379 #define RELOAD_REG_OFFSET 0x08 /* Reg+offset addressing. */
380 #define RELOAD_REG_PRE_INCDEC 0x10 /* PRE_INC/PRE_DEC valid. */
381 #define RELOAD_REG_PRE_MODIFY 0x20 /* PRE_MODIFY valid. */
383 /* Register type masks based on the type, of valid addressing modes. */
384 struct rs6000_reg_addr {
385 enum insn_code reload_load; /* INSN to reload for loading. */
386 enum insn_code reload_store; /* INSN to reload for storing. */
387 enum insn_code reload_fpr_gpr; /* INSN to move from FPR to GPR. */
388 enum insn_code reload_gpr_vsx; /* INSN to move from GPR to VSX. */
389 enum insn_code reload_vsx_gpr; /* INSN to move from VSX to GPR. */
390 addr_mask_type addr_mask[(int)N_RELOAD_REG]; /* Valid address masks. */
393 static struct rs6000_reg_addr reg_addr[NUM_MACHINE_MODES];
395 /* Helper function to say whether a mode supports PRE_INC or PRE_DEC. */
396 static inline bool
397 mode_supports_pre_incdec_p (enum machine_mode mode)
399 return ((reg_addr[mode].addr_mask[RELOAD_REG_ANY] & RELOAD_REG_PRE_INCDEC)
400 != 0);
403 /* Helper function to say whether a mode supports PRE_MODIFY. */
404 static inline bool
405 mode_supports_pre_modify_p (enum machine_mode mode)
407 return ((reg_addr[mode].addr_mask[RELOAD_REG_ANY] & RELOAD_REG_PRE_MODIFY)
408 != 0);
412 /* Target cpu costs. */
414 struct processor_costs {
415 const int mulsi; /* cost of SImode multiplication. */
416 const int mulsi_const; /* cost of SImode multiplication by constant. */
417 const int mulsi_const9; /* cost of SImode mult by short constant. */
418 const int muldi; /* cost of DImode multiplication. */
419 const int divsi; /* cost of SImode division. */
420 const int divdi; /* cost of DImode division. */
421 const int fp; /* cost of simple SFmode and DFmode insns. */
422 const int dmul; /* cost of DFmode multiplication (and fmadd). */
423 const int sdiv; /* cost of SFmode division (fdivs). */
424 const int ddiv; /* cost of DFmode division (fdiv). */
425 const int cache_line_size; /* cache line size in bytes. */
426 const int l1_cache_size; /* size of l1 cache, in kilobytes. */
427 const int l2_cache_size; /* size of l2 cache, in kilobytes. */
428 const int simultaneous_prefetches; /* number of parallel prefetch
429 operations. */
432 const struct processor_costs *rs6000_cost;
434 /* Processor costs (relative to an add) */
436 /* Instruction size costs on 32bit processors. */
437 static const
438 struct processor_costs size32_cost = {
439 COSTS_N_INSNS (1), /* mulsi */
440 COSTS_N_INSNS (1), /* mulsi_const */
441 COSTS_N_INSNS (1), /* mulsi_const9 */
442 COSTS_N_INSNS (1), /* muldi */
443 COSTS_N_INSNS (1), /* divsi */
444 COSTS_N_INSNS (1), /* divdi */
445 COSTS_N_INSNS (1), /* fp */
446 COSTS_N_INSNS (1), /* dmul */
447 COSTS_N_INSNS (1), /* sdiv */
448 COSTS_N_INSNS (1), /* ddiv */
455 /* Instruction size costs on 64bit processors. */
456 static const
457 struct processor_costs size64_cost = {
458 COSTS_N_INSNS (1), /* mulsi */
459 COSTS_N_INSNS (1), /* mulsi_const */
460 COSTS_N_INSNS (1), /* mulsi_const9 */
461 COSTS_N_INSNS (1), /* muldi */
462 COSTS_N_INSNS (1), /* divsi */
463 COSTS_N_INSNS (1), /* divdi */
464 COSTS_N_INSNS (1), /* fp */
465 COSTS_N_INSNS (1), /* dmul */
466 COSTS_N_INSNS (1), /* sdiv */
467 COSTS_N_INSNS (1), /* ddiv */
468 128,
474 /* Instruction costs on RS64A processors. */
475 static const
476 struct processor_costs rs64a_cost = {
477 COSTS_N_INSNS (20), /* mulsi */
478 COSTS_N_INSNS (12), /* mulsi_const */
479 COSTS_N_INSNS (8), /* mulsi_const9 */
480 COSTS_N_INSNS (34), /* muldi */
481 COSTS_N_INSNS (65), /* divsi */
482 COSTS_N_INSNS (67), /* divdi */
483 COSTS_N_INSNS (4), /* fp */
484 COSTS_N_INSNS (4), /* dmul */
485 COSTS_N_INSNS (31), /* sdiv */
486 COSTS_N_INSNS (31), /* ddiv */
487 128, /* cache line size */
488 128, /* l1 cache */
489 2048, /* l2 cache */
490 1, /* streams */
493 /* Instruction costs on MPCCORE processors. */
494 static const
495 struct processor_costs mpccore_cost = {
496 COSTS_N_INSNS (2), /* mulsi */
497 COSTS_N_INSNS (2), /* mulsi_const */
498 COSTS_N_INSNS (2), /* mulsi_const9 */
499 COSTS_N_INSNS (2), /* muldi */
500 COSTS_N_INSNS (6), /* divsi */
501 COSTS_N_INSNS (6), /* divdi */
502 COSTS_N_INSNS (4), /* fp */
503 COSTS_N_INSNS (5), /* dmul */
504 COSTS_N_INSNS (10), /* sdiv */
505 COSTS_N_INSNS (17), /* ddiv */
506 32, /* cache line size */
507 4, /* l1 cache */
508 16, /* l2 cache */
509 1, /* streams */
512 /* Instruction costs on PPC403 processors. */
513 static const
514 struct processor_costs ppc403_cost = {
515 COSTS_N_INSNS (4), /* mulsi */
516 COSTS_N_INSNS (4), /* mulsi_const */
517 COSTS_N_INSNS (4), /* mulsi_const9 */
518 COSTS_N_INSNS (4), /* muldi */
519 COSTS_N_INSNS (33), /* divsi */
520 COSTS_N_INSNS (33), /* divdi */
521 COSTS_N_INSNS (11), /* fp */
522 COSTS_N_INSNS (11), /* dmul */
523 COSTS_N_INSNS (11), /* sdiv */
524 COSTS_N_INSNS (11), /* ddiv */
525 32, /* cache line size */
526 4, /* l1 cache */
527 16, /* l2 cache */
528 1, /* streams */
531 /* Instruction costs on PPC405 processors. */
532 static const
533 struct processor_costs ppc405_cost = {
534 COSTS_N_INSNS (5), /* mulsi */
535 COSTS_N_INSNS (4), /* mulsi_const */
536 COSTS_N_INSNS (3), /* mulsi_const9 */
537 COSTS_N_INSNS (5), /* muldi */
538 COSTS_N_INSNS (35), /* divsi */
539 COSTS_N_INSNS (35), /* divdi */
540 COSTS_N_INSNS (11), /* fp */
541 COSTS_N_INSNS (11), /* dmul */
542 COSTS_N_INSNS (11), /* sdiv */
543 COSTS_N_INSNS (11), /* ddiv */
544 32, /* cache line size */
545 16, /* l1 cache */
546 128, /* l2 cache */
547 1, /* streams */
550 /* Instruction costs on PPC440 processors. */
551 static const
552 struct processor_costs ppc440_cost = {
553 COSTS_N_INSNS (3), /* mulsi */
554 COSTS_N_INSNS (2), /* mulsi_const */
555 COSTS_N_INSNS (2), /* mulsi_const9 */
556 COSTS_N_INSNS (3), /* muldi */
557 COSTS_N_INSNS (34), /* divsi */
558 COSTS_N_INSNS (34), /* divdi */
559 COSTS_N_INSNS (5), /* fp */
560 COSTS_N_INSNS (5), /* dmul */
561 COSTS_N_INSNS (19), /* sdiv */
562 COSTS_N_INSNS (33), /* ddiv */
563 32, /* cache line size */
564 32, /* l1 cache */
565 256, /* l2 cache */
566 1, /* streams */
569 /* Instruction costs on PPC476 processors. */
570 static const
571 struct processor_costs ppc476_cost = {
572 COSTS_N_INSNS (4), /* mulsi */
573 COSTS_N_INSNS (4), /* mulsi_const */
574 COSTS_N_INSNS (4), /* mulsi_const9 */
575 COSTS_N_INSNS (4), /* muldi */
576 COSTS_N_INSNS (11), /* divsi */
577 COSTS_N_INSNS (11), /* divdi */
578 COSTS_N_INSNS (6), /* fp */
579 COSTS_N_INSNS (6), /* dmul */
580 COSTS_N_INSNS (19), /* sdiv */
581 COSTS_N_INSNS (33), /* ddiv */
582 32, /* l1 cache line size */
583 32, /* l1 cache */
584 512, /* l2 cache */
585 1, /* streams */
588 /* Instruction costs on PPC601 processors. */
589 static const
590 struct processor_costs ppc601_cost = {
591 COSTS_N_INSNS (5), /* mulsi */
592 COSTS_N_INSNS (5), /* mulsi_const */
593 COSTS_N_INSNS (5), /* mulsi_const9 */
594 COSTS_N_INSNS (5), /* muldi */
595 COSTS_N_INSNS (36), /* divsi */
596 COSTS_N_INSNS (36), /* divdi */
597 COSTS_N_INSNS (4), /* fp */
598 COSTS_N_INSNS (5), /* dmul */
599 COSTS_N_INSNS (17), /* sdiv */
600 COSTS_N_INSNS (31), /* ddiv */
601 32, /* cache line size */
602 32, /* l1 cache */
603 256, /* l2 cache */
604 1, /* streams */
607 /* Instruction costs on PPC603 processors. */
608 static const
609 struct processor_costs ppc603_cost = {
610 COSTS_N_INSNS (5), /* mulsi */
611 COSTS_N_INSNS (3), /* mulsi_const */
612 COSTS_N_INSNS (2), /* mulsi_const9 */
613 COSTS_N_INSNS (5), /* muldi */
614 COSTS_N_INSNS (37), /* divsi */
615 COSTS_N_INSNS (37), /* divdi */
616 COSTS_N_INSNS (3), /* fp */
617 COSTS_N_INSNS (4), /* dmul */
618 COSTS_N_INSNS (18), /* sdiv */
619 COSTS_N_INSNS (33), /* ddiv */
620 32, /* cache line size */
621 8, /* l1 cache */
622 64, /* l2 cache */
623 1, /* streams */
626 /* Instruction costs on PPC604 processors. */
627 static const
628 struct processor_costs ppc604_cost = {
629 COSTS_N_INSNS (4), /* mulsi */
630 COSTS_N_INSNS (4), /* mulsi_const */
631 COSTS_N_INSNS (4), /* mulsi_const9 */
632 COSTS_N_INSNS (4), /* muldi */
633 COSTS_N_INSNS (20), /* divsi */
634 COSTS_N_INSNS (20), /* divdi */
635 COSTS_N_INSNS (3), /* fp */
636 COSTS_N_INSNS (3), /* dmul */
637 COSTS_N_INSNS (18), /* sdiv */
638 COSTS_N_INSNS (32), /* ddiv */
639 32, /* cache line size */
640 16, /* l1 cache */
641 512, /* l2 cache */
642 1, /* streams */
645 /* Instruction costs on PPC604e processors. */
646 static const
647 struct processor_costs ppc604e_cost = {
648 COSTS_N_INSNS (2), /* mulsi */
649 COSTS_N_INSNS (2), /* mulsi_const */
650 COSTS_N_INSNS (2), /* mulsi_const9 */
651 COSTS_N_INSNS (2), /* muldi */
652 COSTS_N_INSNS (20), /* divsi */
653 COSTS_N_INSNS (20), /* divdi */
654 COSTS_N_INSNS (3), /* fp */
655 COSTS_N_INSNS (3), /* dmul */
656 COSTS_N_INSNS (18), /* sdiv */
657 COSTS_N_INSNS (32), /* ddiv */
658 32, /* cache line size */
659 32, /* l1 cache */
660 1024, /* l2 cache */
661 1, /* streams */
664 /* Instruction costs on PPC620 processors. */
665 static const
666 struct processor_costs ppc620_cost = {
667 COSTS_N_INSNS (5), /* mulsi */
668 COSTS_N_INSNS (4), /* mulsi_const */
669 COSTS_N_INSNS (3), /* mulsi_const9 */
670 COSTS_N_INSNS (7), /* muldi */
671 COSTS_N_INSNS (21), /* divsi */
672 COSTS_N_INSNS (37), /* divdi */
673 COSTS_N_INSNS (3), /* fp */
674 COSTS_N_INSNS (3), /* dmul */
675 COSTS_N_INSNS (18), /* sdiv */
676 COSTS_N_INSNS (32), /* ddiv */
677 128, /* cache line size */
678 32, /* l1 cache */
679 1024, /* l2 cache */
680 1, /* streams */
683 /* Instruction costs on PPC630 processors. */
684 static const
685 struct processor_costs ppc630_cost = {
686 COSTS_N_INSNS (5), /* mulsi */
687 COSTS_N_INSNS (4), /* mulsi_const */
688 COSTS_N_INSNS (3), /* mulsi_const9 */
689 COSTS_N_INSNS (7), /* muldi */
690 COSTS_N_INSNS (21), /* divsi */
691 COSTS_N_INSNS (37), /* divdi */
692 COSTS_N_INSNS (3), /* fp */
693 COSTS_N_INSNS (3), /* dmul */
694 COSTS_N_INSNS (17), /* sdiv */
695 COSTS_N_INSNS (21), /* ddiv */
696 128, /* cache line size */
697 64, /* l1 cache */
698 1024, /* l2 cache */
699 1, /* streams */
702 /* Instruction costs on Cell processor. */
703 /* COSTS_N_INSNS (1) ~ one add. */
704 static const
705 struct processor_costs ppccell_cost = {
706 COSTS_N_INSNS (9/2)+2, /* mulsi */
707 COSTS_N_INSNS (6/2), /* mulsi_const */
708 COSTS_N_INSNS (6/2), /* mulsi_const9 */
709 COSTS_N_INSNS (15/2)+2, /* muldi */
710 COSTS_N_INSNS (38/2), /* divsi */
711 COSTS_N_INSNS (70/2), /* divdi */
712 COSTS_N_INSNS (10/2), /* fp */
713 COSTS_N_INSNS (10/2), /* dmul */
714 COSTS_N_INSNS (74/2), /* sdiv */
715 COSTS_N_INSNS (74/2), /* ddiv */
716 128, /* cache line size */
717 32, /* l1 cache */
718 512, /* l2 cache */
719 6, /* streams */
722 /* Instruction costs on PPC750 and PPC7400 processors. */
723 static const
724 struct processor_costs ppc750_cost = {
725 COSTS_N_INSNS (5), /* mulsi */
726 COSTS_N_INSNS (3), /* mulsi_const */
727 COSTS_N_INSNS (2), /* mulsi_const9 */
728 COSTS_N_INSNS (5), /* muldi */
729 COSTS_N_INSNS (17), /* divsi */
730 COSTS_N_INSNS (17), /* divdi */
731 COSTS_N_INSNS (3), /* fp */
732 COSTS_N_INSNS (3), /* dmul */
733 COSTS_N_INSNS (17), /* sdiv */
734 COSTS_N_INSNS (31), /* ddiv */
735 32, /* cache line size */
736 32, /* l1 cache */
737 512, /* l2 cache */
738 1, /* streams */
741 /* Instruction costs on PPC7450 processors. */
742 static const
743 struct processor_costs ppc7450_cost = {
744 COSTS_N_INSNS (4), /* mulsi */
745 COSTS_N_INSNS (3), /* mulsi_const */
746 COSTS_N_INSNS (3), /* mulsi_const9 */
747 COSTS_N_INSNS (4), /* muldi */
748 COSTS_N_INSNS (23), /* divsi */
749 COSTS_N_INSNS (23), /* divdi */
750 COSTS_N_INSNS (5), /* fp */
751 COSTS_N_INSNS (5), /* dmul */
752 COSTS_N_INSNS (21), /* sdiv */
753 COSTS_N_INSNS (35), /* ddiv */
754 32, /* cache line size */
755 32, /* l1 cache */
756 1024, /* l2 cache */
757 1, /* streams */
760 /* Instruction costs on PPC8540 processors. */
761 static const
762 struct processor_costs ppc8540_cost = {
763 COSTS_N_INSNS (4), /* mulsi */
764 COSTS_N_INSNS (4), /* mulsi_const */
765 COSTS_N_INSNS (4), /* mulsi_const9 */
766 COSTS_N_INSNS (4), /* muldi */
767 COSTS_N_INSNS (19), /* divsi */
768 COSTS_N_INSNS (19), /* divdi */
769 COSTS_N_INSNS (4), /* fp */
770 COSTS_N_INSNS (4), /* dmul */
771 COSTS_N_INSNS (29), /* sdiv */
772 COSTS_N_INSNS (29), /* ddiv */
773 32, /* cache line size */
774 32, /* l1 cache */
775 256, /* l2 cache */
776 1, /* prefetch streams /*/
779 /* Instruction costs on E300C2 and E300C3 cores. */
780 static const
781 struct processor_costs ppce300c2c3_cost = {
782 COSTS_N_INSNS (4), /* mulsi */
783 COSTS_N_INSNS (4), /* mulsi_const */
784 COSTS_N_INSNS (4), /* mulsi_const9 */
785 COSTS_N_INSNS (4), /* muldi */
786 COSTS_N_INSNS (19), /* divsi */
787 COSTS_N_INSNS (19), /* divdi */
788 COSTS_N_INSNS (3), /* fp */
789 COSTS_N_INSNS (4), /* dmul */
790 COSTS_N_INSNS (18), /* sdiv */
791 COSTS_N_INSNS (33), /* ddiv */
793 16, /* l1 cache */
794 16, /* l2 cache */
795 1, /* prefetch streams /*/
798 /* Instruction costs on PPCE500MC processors. */
799 static const
800 struct processor_costs ppce500mc_cost = {
801 COSTS_N_INSNS (4), /* mulsi */
802 COSTS_N_INSNS (4), /* mulsi_const */
803 COSTS_N_INSNS (4), /* mulsi_const9 */
804 COSTS_N_INSNS (4), /* muldi */
805 COSTS_N_INSNS (14), /* divsi */
806 COSTS_N_INSNS (14), /* divdi */
807 COSTS_N_INSNS (8), /* fp */
808 COSTS_N_INSNS (10), /* dmul */
809 COSTS_N_INSNS (36), /* sdiv */
810 COSTS_N_INSNS (66), /* ddiv */
811 64, /* cache line size */
812 32, /* l1 cache */
813 128, /* l2 cache */
814 1, /* prefetch streams /*/
817 /* Instruction costs on PPCE500MC64 processors. */
818 static const
819 struct processor_costs ppce500mc64_cost = {
820 COSTS_N_INSNS (4), /* mulsi */
821 COSTS_N_INSNS (4), /* mulsi_const */
822 COSTS_N_INSNS (4), /* mulsi_const9 */
823 COSTS_N_INSNS (4), /* muldi */
824 COSTS_N_INSNS (14), /* divsi */
825 COSTS_N_INSNS (14), /* divdi */
826 COSTS_N_INSNS (4), /* fp */
827 COSTS_N_INSNS (10), /* dmul */
828 COSTS_N_INSNS (36), /* sdiv */
829 COSTS_N_INSNS (66), /* ddiv */
830 64, /* cache line size */
831 32, /* l1 cache */
832 128, /* l2 cache */
833 1, /* prefetch streams /*/
836 /* Instruction costs on PPCE5500 processors. */
837 static const
838 struct processor_costs ppce5500_cost = {
839 COSTS_N_INSNS (5), /* mulsi */
840 COSTS_N_INSNS (5), /* mulsi_const */
841 COSTS_N_INSNS (4), /* mulsi_const9 */
842 COSTS_N_INSNS (5), /* muldi */
843 COSTS_N_INSNS (14), /* divsi */
844 COSTS_N_INSNS (14), /* divdi */
845 COSTS_N_INSNS (7), /* fp */
846 COSTS_N_INSNS (10), /* dmul */
847 COSTS_N_INSNS (36), /* sdiv */
848 COSTS_N_INSNS (66), /* ddiv */
849 64, /* cache line size */
850 32, /* l1 cache */
851 128, /* l2 cache */
852 1, /* prefetch streams /*/
855 /* Instruction costs on PPCE6500 processors. */
856 static const
857 struct processor_costs ppce6500_cost = {
858 COSTS_N_INSNS (5), /* mulsi */
859 COSTS_N_INSNS (5), /* mulsi_const */
860 COSTS_N_INSNS (4), /* mulsi_const9 */
861 COSTS_N_INSNS (5), /* muldi */
862 COSTS_N_INSNS (14), /* divsi */
863 COSTS_N_INSNS (14), /* divdi */
864 COSTS_N_INSNS (7), /* fp */
865 COSTS_N_INSNS (10), /* dmul */
866 COSTS_N_INSNS (36), /* sdiv */
867 COSTS_N_INSNS (66), /* ddiv */
868 64, /* cache line size */
869 32, /* l1 cache */
870 128, /* l2 cache */
871 1, /* prefetch streams /*/
874 /* Instruction costs on AppliedMicro Titan processors. */
875 static const
876 struct processor_costs titan_cost = {
877 COSTS_N_INSNS (5), /* mulsi */
878 COSTS_N_INSNS (5), /* mulsi_const */
879 COSTS_N_INSNS (5), /* mulsi_const9 */
880 COSTS_N_INSNS (5), /* muldi */
881 COSTS_N_INSNS (18), /* divsi */
882 COSTS_N_INSNS (18), /* divdi */
883 COSTS_N_INSNS (10), /* fp */
884 COSTS_N_INSNS (10), /* dmul */
885 COSTS_N_INSNS (46), /* sdiv */
886 COSTS_N_INSNS (72), /* ddiv */
887 32, /* cache line size */
888 32, /* l1 cache */
889 512, /* l2 cache */
890 1, /* prefetch streams /*/
893 /* Instruction costs on POWER4 and POWER5 processors. */
894 static const
895 struct processor_costs power4_cost = {
896 COSTS_N_INSNS (3), /* mulsi */
897 COSTS_N_INSNS (2), /* mulsi_const */
898 COSTS_N_INSNS (2), /* mulsi_const9 */
899 COSTS_N_INSNS (4), /* muldi */
900 COSTS_N_INSNS (18), /* divsi */
901 COSTS_N_INSNS (34), /* divdi */
902 COSTS_N_INSNS (3), /* fp */
903 COSTS_N_INSNS (3), /* dmul */
904 COSTS_N_INSNS (17), /* sdiv */
905 COSTS_N_INSNS (17), /* ddiv */
906 128, /* cache line size */
907 32, /* l1 cache */
908 1024, /* l2 cache */
909 8, /* prefetch streams /*/
912 /* Instruction costs on POWER6 processors. */
913 static const
914 struct processor_costs power6_cost = {
915 COSTS_N_INSNS (8), /* mulsi */
916 COSTS_N_INSNS (8), /* mulsi_const */
917 COSTS_N_INSNS (8), /* mulsi_const9 */
918 COSTS_N_INSNS (8), /* muldi */
919 COSTS_N_INSNS (22), /* divsi */
920 COSTS_N_INSNS (28), /* divdi */
921 COSTS_N_INSNS (3), /* fp */
922 COSTS_N_INSNS (3), /* dmul */
923 COSTS_N_INSNS (13), /* sdiv */
924 COSTS_N_INSNS (16), /* ddiv */
925 128, /* cache line size */
926 64, /* l1 cache */
927 2048, /* l2 cache */
928 16, /* prefetch streams */
931 /* Instruction costs on POWER7 processors. */
932 static const
933 struct processor_costs power7_cost = {
934 COSTS_N_INSNS (2), /* mulsi */
935 COSTS_N_INSNS (2), /* mulsi_const */
936 COSTS_N_INSNS (2), /* mulsi_const9 */
937 COSTS_N_INSNS (2), /* muldi */
938 COSTS_N_INSNS (18), /* divsi */
939 COSTS_N_INSNS (34), /* divdi */
940 COSTS_N_INSNS (3), /* fp */
941 COSTS_N_INSNS (3), /* dmul */
942 COSTS_N_INSNS (13), /* sdiv */
943 COSTS_N_INSNS (16), /* ddiv */
944 128, /* cache line size */
945 32, /* l1 cache */
946 256, /* l2 cache */
947 12, /* prefetch streams */
950 /* Instruction costs on POWER8 processors. */
951 static const
952 struct processor_costs power8_cost = {
953 COSTS_N_INSNS (3), /* mulsi */
954 COSTS_N_INSNS (3), /* mulsi_const */
955 COSTS_N_INSNS (3), /* mulsi_const9 */
956 COSTS_N_INSNS (3), /* muldi */
957 COSTS_N_INSNS (19), /* divsi */
958 COSTS_N_INSNS (35), /* divdi */
959 COSTS_N_INSNS (3), /* fp */
960 COSTS_N_INSNS (3), /* dmul */
961 COSTS_N_INSNS (14), /* sdiv */
962 COSTS_N_INSNS (17), /* ddiv */
963 128, /* cache line size */
964 32, /* l1 cache */
965 256, /* l2 cache */
966 12, /* prefetch streams */
969 /* Instruction costs on POWER A2 processors. */
970 static const
971 struct processor_costs ppca2_cost = {
972 COSTS_N_INSNS (16), /* mulsi */
973 COSTS_N_INSNS (16), /* mulsi_const */
974 COSTS_N_INSNS (16), /* mulsi_const9 */
975 COSTS_N_INSNS (16), /* muldi */
976 COSTS_N_INSNS (22), /* divsi */
977 COSTS_N_INSNS (28), /* divdi */
978 COSTS_N_INSNS (3), /* fp */
979 COSTS_N_INSNS (3), /* dmul */
980 COSTS_N_INSNS (59), /* sdiv */
981 COSTS_N_INSNS (72), /* ddiv */
983 16, /* l1 cache */
984 2048, /* l2 cache */
985 16, /* prefetch streams */
989 /* Table that classifies rs6000 builtin functions (pure, const, etc.). */
990 #undef RS6000_BUILTIN_1
991 #undef RS6000_BUILTIN_2
992 #undef RS6000_BUILTIN_3
993 #undef RS6000_BUILTIN_A
994 #undef RS6000_BUILTIN_D
995 #undef RS6000_BUILTIN_E
996 #undef RS6000_BUILTIN_H
997 #undef RS6000_BUILTIN_P
998 #undef RS6000_BUILTIN_Q
999 #undef RS6000_BUILTIN_S
1000 #undef RS6000_BUILTIN_X
1002 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE) \
1003 { NAME, ICODE, MASK, ATTR },
1005 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE) \
1006 { NAME, ICODE, MASK, ATTR },
1008 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE) \
1009 { NAME, ICODE, MASK, ATTR },
1011 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE) \
1012 { NAME, ICODE, MASK, ATTR },
1014 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE) \
1015 { NAME, ICODE, MASK, ATTR },
1017 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE) \
1018 { NAME, ICODE, MASK, ATTR },
1020 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE) \
1021 { NAME, ICODE, MASK, ATTR },
1023 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE) \
1024 { NAME, ICODE, MASK, ATTR },
1026 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE) \
1027 { NAME, ICODE, MASK, ATTR },
1029 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE) \
1030 { NAME, ICODE, MASK, ATTR },
1032 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE) \
1033 { NAME, ICODE, MASK, ATTR },
1035 struct rs6000_builtin_info_type {
1036 const char *name;
1037 const enum insn_code icode;
1038 const HOST_WIDE_INT mask;
1039 const unsigned attr;
1042 static const struct rs6000_builtin_info_type rs6000_builtin_info[] =
1044 #include "rs6000-builtin.def"
1047 #undef RS6000_BUILTIN_1
1048 #undef RS6000_BUILTIN_2
1049 #undef RS6000_BUILTIN_3
1050 #undef RS6000_BUILTIN_A
1051 #undef RS6000_BUILTIN_D
1052 #undef RS6000_BUILTIN_E
1053 #undef RS6000_BUILTIN_H
1054 #undef RS6000_BUILTIN_P
1055 #undef RS6000_BUILTIN_Q
1056 #undef RS6000_BUILTIN_S
1057 #undef RS6000_BUILTIN_X
1059 /* Support for -mveclibabi=<xxx> to control which vector library to use. */
1060 static tree (*rs6000_veclib_handler) (tree, tree, tree);
1063 static bool rs6000_debug_legitimate_address_p (enum machine_mode, rtx, bool);
1064 static bool spe_func_has_64bit_regs_p (void);
1065 static struct machine_function * rs6000_init_machine_status (void);
1066 static int rs6000_ra_ever_killed (void);
1067 static tree rs6000_handle_longcall_attribute (tree *, tree, tree, int, bool *);
1068 static tree rs6000_handle_altivec_attribute (tree *, tree, tree, int, bool *);
1069 static tree rs6000_handle_struct_attribute (tree *, tree, tree, int, bool *);
1070 static tree rs6000_builtin_vectorized_libmass (tree, tree, tree);
1071 static rtx rs6000_emit_set_long_const (rtx, HOST_WIDE_INT, HOST_WIDE_INT);
1072 static int rs6000_memory_move_cost (enum machine_mode, reg_class_t, bool);
1073 static bool rs6000_debug_rtx_costs (rtx, int, int, int, int *, bool);
1074 static int rs6000_debug_address_cost (rtx, enum machine_mode, addr_space_t,
1075 bool);
1076 static int rs6000_debug_adjust_cost (rtx, rtx, rtx, int);
1077 static bool is_microcoded_insn (rtx);
1078 static bool is_nonpipeline_insn (rtx);
1079 static bool is_cracked_insn (rtx);
1080 static bool is_load_insn (rtx, rtx *);
1081 static bool is_store_insn (rtx, rtx *);
1082 static bool set_to_load_agen (rtx,rtx);
1083 static bool insn_terminates_group_p (rtx , enum group_termination);
1084 static bool insn_must_be_first_in_group (rtx);
1085 static bool insn_must_be_last_in_group (rtx);
1086 static void altivec_init_builtins (void);
1087 static tree builtin_function_type (enum machine_mode, enum machine_mode,
1088 enum machine_mode, enum machine_mode,
1089 enum rs6000_builtins, const char *name);
1090 static void rs6000_common_init_builtins (void);
1091 static void paired_init_builtins (void);
1092 static rtx paired_expand_predicate_builtin (enum insn_code, tree, rtx);
1093 static void spe_init_builtins (void);
1094 static void htm_init_builtins (void);
1095 static rtx spe_expand_predicate_builtin (enum insn_code, tree, rtx);
1096 static rtx spe_expand_evsel_builtin (enum insn_code, tree, rtx);
1097 static int rs6000_emit_int_cmove (rtx, rtx, rtx, rtx);
1098 static rs6000_stack_t *rs6000_stack_info (void);
1099 static void is_altivec_return_reg (rtx, void *);
1100 int easy_vector_constant (rtx, enum machine_mode);
1101 static rtx rs6000_debug_legitimize_address (rtx, rtx, enum machine_mode);
1102 static rtx rs6000_legitimize_tls_address (rtx, enum tls_model);
1103 static int rs6000_tls_symbol_ref_1 (rtx *, void *);
1104 static int rs6000_get_some_local_dynamic_name_1 (rtx *, void *);
1105 static rtx rs6000_darwin64_record_arg (CUMULATIVE_ARGS *, const_tree,
1106 bool, bool);
1107 #if TARGET_MACHO
1108 static void macho_branch_islands (void);
1109 #endif
1110 static rtx rs6000_legitimize_reload_address (rtx, enum machine_mode, int, int,
1111 int, int *);
1112 static rtx rs6000_debug_legitimize_reload_address (rtx, enum machine_mode, int,
1113 int, int, int *);
1114 static bool rs6000_mode_dependent_address (const_rtx);
1115 static bool rs6000_debug_mode_dependent_address (const_rtx);
1116 static enum reg_class rs6000_secondary_reload_class (enum reg_class,
1117 enum machine_mode, rtx);
1118 static enum reg_class rs6000_debug_secondary_reload_class (enum reg_class,
1119 enum machine_mode,
1120 rtx);
1121 static enum reg_class rs6000_preferred_reload_class (rtx, enum reg_class);
1122 static enum reg_class rs6000_debug_preferred_reload_class (rtx,
1123 enum reg_class);
1124 static bool rs6000_secondary_memory_needed (enum reg_class, enum reg_class,
1125 enum machine_mode);
1126 static bool rs6000_debug_secondary_memory_needed (enum reg_class,
1127 enum reg_class,
1128 enum machine_mode);
1129 static bool rs6000_cannot_change_mode_class (enum machine_mode,
1130 enum machine_mode,
1131 enum reg_class);
1132 static bool rs6000_debug_cannot_change_mode_class (enum machine_mode,
1133 enum machine_mode,
1134 enum reg_class);
1135 static bool rs6000_save_toc_in_prologue_p (void);
1137 rtx (*rs6000_legitimize_reload_address_ptr) (rtx, enum machine_mode, int, int,
1138 int, int *)
1139 = rs6000_legitimize_reload_address;
1141 static bool (*rs6000_mode_dependent_address_ptr) (const_rtx)
1142 = rs6000_mode_dependent_address;
1144 enum reg_class (*rs6000_secondary_reload_class_ptr) (enum reg_class,
1145 enum machine_mode, rtx)
1146 = rs6000_secondary_reload_class;
1148 enum reg_class (*rs6000_preferred_reload_class_ptr) (rtx, enum reg_class)
1149 = rs6000_preferred_reload_class;
1151 bool (*rs6000_secondary_memory_needed_ptr) (enum reg_class, enum reg_class,
1152 enum machine_mode)
1153 = rs6000_secondary_memory_needed;
1155 bool (*rs6000_cannot_change_mode_class_ptr) (enum machine_mode,
1156 enum machine_mode,
1157 enum reg_class)
1158 = rs6000_cannot_change_mode_class;
1160 const int INSN_NOT_AVAILABLE = -1;
1162 static void rs6000_print_isa_options (FILE *, int, const char *,
1163 HOST_WIDE_INT);
1164 static void rs6000_print_builtin_options (FILE *, int, const char *,
1165 HOST_WIDE_INT);
1167 static enum rs6000_reg_type register_to_reg_type (rtx, bool *);
1168 static bool rs6000_secondary_reload_move (enum rs6000_reg_type,
1169 enum rs6000_reg_type,
1170 enum machine_mode,
1171 secondary_reload_info *,
1172 bool);
1174 /* Hash table stuff for keeping track of TOC entries. */
1176 struct GTY(()) toc_hash_struct
1178 /* `key' will satisfy CONSTANT_P; in fact, it will satisfy
1179 ASM_OUTPUT_SPECIAL_POOL_ENTRY_P. */
1180 rtx key;
1181 enum machine_mode key_mode;
1182 int labelno;
1185 static GTY ((param_is (struct toc_hash_struct))) htab_t toc_hash_table;
1187 /* Hash table to keep track of the argument types for builtin functions. */
1189 struct GTY(()) builtin_hash_struct
1191 tree type;
1192 enum machine_mode mode[4]; /* return value + 3 arguments. */
1193 unsigned char uns_p[4]; /* and whether the types are unsigned. */
1196 static GTY ((param_is (struct builtin_hash_struct))) htab_t builtin_hash_table;
1199 /* Default register names. */
1200 char rs6000_reg_names[][8] =
1202 "0", "1", "2", "3", "4", "5", "6", "7",
1203 "8", "9", "10", "11", "12", "13", "14", "15",
1204 "16", "17", "18", "19", "20", "21", "22", "23",
1205 "24", "25", "26", "27", "28", "29", "30", "31",
1206 "0", "1", "2", "3", "4", "5", "6", "7",
1207 "8", "9", "10", "11", "12", "13", "14", "15",
1208 "16", "17", "18", "19", "20", "21", "22", "23",
1209 "24", "25", "26", "27", "28", "29", "30", "31",
1210 "mq", "lr", "ctr","ap",
1211 "0", "1", "2", "3", "4", "5", "6", "7",
1212 "ca",
1213 /* AltiVec registers. */
1214 "0", "1", "2", "3", "4", "5", "6", "7",
1215 "8", "9", "10", "11", "12", "13", "14", "15",
1216 "16", "17", "18", "19", "20", "21", "22", "23",
1217 "24", "25", "26", "27", "28", "29", "30", "31",
1218 "vrsave", "vscr",
1219 /* SPE registers. */
1220 "spe_acc", "spefscr",
1221 /* Soft frame pointer. */
1222 "sfp",
1223 /* HTM SPR registers. */
1224 "tfhar", "tfiar", "texasr"
1227 #ifdef TARGET_REGNAMES
1228 static const char alt_reg_names[][8] =
1230 "%r0", "%r1", "%r2", "%r3", "%r4", "%r5", "%r6", "%r7",
1231 "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15",
1232 "%r16", "%r17", "%r18", "%r19", "%r20", "%r21", "%r22", "%r23",
1233 "%r24", "%r25", "%r26", "%r27", "%r28", "%r29", "%r30", "%r31",
1234 "%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7",
1235 "%f8", "%f9", "%f10", "%f11", "%f12", "%f13", "%f14", "%f15",
1236 "%f16", "%f17", "%f18", "%f19", "%f20", "%f21", "%f22", "%f23",
1237 "%f24", "%f25", "%f26", "%f27", "%f28", "%f29", "%f30", "%f31",
1238 "mq", "lr", "ctr", "ap",
1239 "%cr0", "%cr1", "%cr2", "%cr3", "%cr4", "%cr5", "%cr6", "%cr7",
1240 "ca",
1241 /* AltiVec registers. */
1242 "%v0", "%v1", "%v2", "%v3", "%v4", "%v5", "%v6", "%v7",
1243 "%v8", "%v9", "%v10", "%v11", "%v12", "%v13", "%v14", "%v15",
1244 "%v16", "%v17", "%v18", "%v19", "%v20", "%v21", "%v22", "%v23",
1245 "%v24", "%v25", "%v26", "%v27", "%v28", "%v29", "%v30", "%v31",
1246 "vrsave", "vscr",
1247 /* SPE registers. */
1248 "spe_acc", "spefscr",
1249 /* Soft frame pointer. */
1250 "sfp",
1251 /* HTM SPR registers. */
1252 "tfhar", "tfiar", "texasr"
1254 #endif
1256 /* Table of valid machine attributes. */
1258 static const struct attribute_spec rs6000_attribute_table[] =
1260 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
1261 affects_type_identity } */
1262 { "altivec", 1, 1, false, true, false, rs6000_handle_altivec_attribute,
1263 false },
1264 { "longcall", 0, 0, false, true, true, rs6000_handle_longcall_attribute,
1265 false },
1266 { "shortcall", 0, 0, false, true, true, rs6000_handle_longcall_attribute,
1267 false },
1268 { "ms_struct", 0, 0, false, false, false, rs6000_handle_struct_attribute,
1269 false },
1270 { "gcc_struct", 0, 0, false, false, false, rs6000_handle_struct_attribute,
1271 false },
1272 #ifdef SUBTARGET_ATTRIBUTE_TABLE
1273 SUBTARGET_ATTRIBUTE_TABLE,
1274 #endif
1275 { NULL, 0, 0, false, false, false, NULL, false }
1278 #ifndef TARGET_PROFILE_KERNEL
1279 #define TARGET_PROFILE_KERNEL 0
1280 #endif
1282 /* The VRSAVE bitmask puts bit %v0 as the most significant bit. */
1283 #define ALTIVEC_REG_BIT(REGNO) (0x80000000 >> ((REGNO) - FIRST_ALTIVEC_REGNO))
1285 /* Initialize the GCC target structure. */
1286 #undef TARGET_ATTRIBUTE_TABLE
1287 #define TARGET_ATTRIBUTE_TABLE rs6000_attribute_table
1288 #undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
1289 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES rs6000_set_default_type_attributes
1290 #undef TARGET_ATTRIBUTE_TAKES_IDENTIFIER_P
1291 #define TARGET_ATTRIBUTE_TAKES_IDENTIFIER_P rs6000_attribute_takes_identifier_p
1293 #undef TARGET_ASM_ALIGNED_DI_OP
1294 #define TARGET_ASM_ALIGNED_DI_OP DOUBLE_INT_ASM_OP
1296 /* Default unaligned ops are only provided for ELF. Find the ops needed
1297 for non-ELF systems. */
1298 #ifndef OBJECT_FORMAT_ELF
1299 #if TARGET_XCOFF
1300 /* For XCOFF. rs6000_assemble_integer will handle unaligned DIs on
1301 64-bit targets. */
1302 #undef TARGET_ASM_UNALIGNED_HI_OP
1303 #define TARGET_ASM_UNALIGNED_HI_OP "\t.vbyte\t2,"
1304 #undef TARGET_ASM_UNALIGNED_SI_OP
1305 #define TARGET_ASM_UNALIGNED_SI_OP "\t.vbyte\t4,"
1306 #undef TARGET_ASM_UNALIGNED_DI_OP
1307 #define TARGET_ASM_UNALIGNED_DI_OP "\t.vbyte\t8,"
1308 #else
1309 /* For Darwin. */
1310 #undef TARGET_ASM_UNALIGNED_HI_OP
1311 #define TARGET_ASM_UNALIGNED_HI_OP "\t.short\t"
1312 #undef TARGET_ASM_UNALIGNED_SI_OP
1313 #define TARGET_ASM_UNALIGNED_SI_OP "\t.long\t"
1314 #undef TARGET_ASM_UNALIGNED_DI_OP
1315 #define TARGET_ASM_UNALIGNED_DI_OP "\t.quad\t"
1316 #undef TARGET_ASM_ALIGNED_DI_OP
1317 #define TARGET_ASM_ALIGNED_DI_OP "\t.quad\t"
1318 #endif
1319 #endif
1321 /* This hook deals with fixups for relocatable code and DI-mode objects
1322 in 64-bit code. */
1323 #undef TARGET_ASM_INTEGER
1324 #define TARGET_ASM_INTEGER rs6000_assemble_integer
1326 #if defined (HAVE_GAS_HIDDEN) && !TARGET_MACHO
1327 #undef TARGET_ASM_ASSEMBLE_VISIBILITY
1328 #define TARGET_ASM_ASSEMBLE_VISIBILITY rs6000_assemble_visibility
1329 #endif
1331 #undef TARGET_SET_UP_BY_PROLOGUE
1332 #define TARGET_SET_UP_BY_PROLOGUE rs6000_set_up_by_prologue
1334 #undef TARGET_HAVE_TLS
1335 #define TARGET_HAVE_TLS HAVE_AS_TLS
1337 #undef TARGET_CANNOT_FORCE_CONST_MEM
1338 #define TARGET_CANNOT_FORCE_CONST_MEM rs6000_cannot_force_const_mem
1340 #undef TARGET_DELEGITIMIZE_ADDRESS
1341 #define TARGET_DELEGITIMIZE_ADDRESS rs6000_delegitimize_address
1343 #undef TARGET_CONST_NOT_OK_FOR_DEBUG_P
1344 #define TARGET_CONST_NOT_OK_FOR_DEBUG_P rs6000_const_not_ok_for_debug_p
1346 #undef TARGET_ASM_FUNCTION_PROLOGUE
1347 #define TARGET_ASM_FUNCTION_PROLOGUE rs6000_output_function_prologue
1348 #undef TARGET_ASM_FUNCTION_EPILOGUE
1349 #define TARGET_ASM_FUNCTION_EPILOGUE rs6000_output_function_epilogue
1351 #undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
1352 #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA rs6000_output_addr_const_extra
1354 #undef TARGET_LEGITIMIZE_ADDRESS
1355 #define TARGET_LEGITIMIZE_ADDRESS rs6000_legitimize_address
1357 #undef TARGET_SCHED_VARIABLE_ISSUE
1358 #define TARGET_SCHED_VARIABLE_ISSUE rs6000_variable_issue
1360 #undef TARGET_SCHED_ISSUE_RATE
1361 #define TARGET_SCHED_ISSUE_RATE rs6000_issue_rate
1362 #undef TARGET_SCHED_ADJUST_COST
1363 #define TARGET_SCHED_ADJUST_COST rs6000_adjust_cost
1364 #undef TARGET_SCHED_ADJUST_PRIORITY
1365 #define TARGET_SCHED_ADJUST_PRIORITY rs6000_adjust_priority
1366 #undef TARGET_SCHED_IS_COSTLY_DEPENDENCE
1367 #define TARGET_SCHED_IS_COSTLY_DEPENDENCE rs6000_is_costly_dependence
1368 #undef TARGET_SCHED_INIT
1369 #define TARGET_SCHED_INIT rs6000_sched_init
1370 #undef TARGET_SCHED_FINISH
1371 #define TARGET_SCHED_FINISH rs6000_sched_finish
1372 #undef TARGET_SCHED_REORDER
1373 #define TARGET_SCHED_REORDER rs6000_sched_reorder
1374 #undef TARGET_SCHED_REORDER2
1375 #define TARGET_SCHED_REORDER2 rs6000_sched_reorder2
1377 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
1378 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD rs6000_use_sched_lookahead
1380 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD
1381 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD rs6000_use_sched_lookahead_guard
1383 #undef TARGET_SCHED_ALLOC_SCHED_CONTEXT
1384 #define TARGET_SCHED_ALLOC_SCHED_CONTEXT rs6000_alloc_sched_context
1385 #undef TARGET_SCHED_INIT_SCHED_CONTEXT
1386 #define TARGET_SCHED_INIT_SCHED_CONTEXT rs6000_init_sched_context
1387 #undef TARGET_SCHED_SET_SCHED_CONTEXT
1388 #define TARGET_SCHED_SET_SCHED_CONTEXT rs6000_set_sched_context
1389 #undef TARGET_SCHED_FREE_SCHED_CONTEXT
1390 #define TARGET_SCHED_FREE_SCHED_CONTEXT rs6000_free_sched_context
1392 #undef TARGET_VECTORIZE_BUILTIN_MASK_FOR_LOAD
1393 #define TARGET_VECTORIZE_BUILTIN_MASK_FOR_LOAD rs6000_builtin_mask_for_load
1394 #undef TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT
1395 #define TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT \
1396 rs6000_builtin_support_vector_misalignment
1397 #undef TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE
1398 #define TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE rs6000_vector_alignment_reachable
1399 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST
1400 #define TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST \
1401 rs6000_builtin_vectorization_cost
1402 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
1403 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE \
1404 rs6000_preferred_simd_mode
1405 #undef TARGET_VECTORIZE_INIT_COST
1406 #define TARGET_VECTORIZE_INIT_COST rs6000_init_cost
1407 #undef TARGET_VECTORIZE_ADD_STMT_COST
1408 #define TARGET_VECTORIZE_ADD_STMT_COST rs6000_add_stmt_cost
1409 #undef TARGET_VECTORIZE_FINISH_COST
1410 #define TARGET_VECTORIZE_FINISH_COST rs6000_finish_cost
1411 #undef TARGET_VECTORIZE_DESTROY_COST_DATA
1412 #define TARGET_VECTORIZE_DESTROY_COST_DATA rs6000_destroy_cost_data
1414 #undef TARGET_INIT_BUILTINS
1415 #define TARGET_INIT_BUILTINS rs6000_init_builtins
1416 #undef TARGET_BUILTIN_DECL
1417 #define TARGET_BUILTIN_DECL rs6000_builtin_decl
1419 #undef TARGET_EXPAND_BUILTIN
1420 #define TARGET_EXPAND_BUILTIN rs6000_expand_builtin
1422 #undef TARGET_MANGLE_TYPE
1423 #define TARGET_MANGLE_TYPE rs6000_mangle_type
1425 #undef TARGET_INIT_LIBFUNCS
1426 #define TARGET_INIT_LIBFUNCS rs6000_init_libfuncs
1428 #if TARGET_MACHO
1429 #undef TARGET_BINDS_LOCAL_P
1430 #define TARGET_BINDS_LOCAL_P darwin_binds_local_p
1431 #endif
1433 #undef TARGET_MS_BITFIELD_LAYOUT_P
1434 #define TARGET_MS_BITFIELD_LAYOUT_P rs6000_ms_bitfield_layout_p
1436 #undef TARGET_ASM_OUTPUT_MI_THUNK
1437 #define TARGET_ASM_OUTPUT_MI_THUNK rs6000_output_mi_thunk
1439 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
1440 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
1442 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
1443 #define TARGET_FUNCTION_OK_FOR_SIBCALL rs6000_function_ok_for_sibcall
1445 #undef TARGET_REGISTER_MOVE_COST
1446 #define TARGET_REGISTER_MOVE_COST rs6000_register_move_cost
1447 #undef TARGET_MEMORY_MOVE_COST
1448 #define TARGET_MEMORY_MOVE_COST rs6000_memory_move_cost
1449 #undef TARGET_RTX_COSTS
1450 #define TARGET_RTX_COSTS rs6000_rtx_costs
1451 #undef TARGET_ADDRESS_COST
1452 #define TARGET_ADDRESS_COST hook_int_rtx_mode_as_bool_0
1454 #undef TARGET_DWARF_REGISTER_SPAN
1455 #define TARGET_DWARF_REGISTER_SPAN rs6000_dwarf_register_span
1457 #undef TARGET_INIT_DWARF_REG_SIZES_EXTRA
1458 #define TARGET_INIT_DWARF_REG_SIZES_EXTRA rs6000_init_dwarf_reg_sizes_extra
1460 #undef TARGET_MEMBER_TYPE_FORCES_BLK
1461 #define TARGET_MEMBER_TYPE_FORCES_BLK rs6000_member_type_forces_blk
1463 /* On rs6000, function arguments are promoted, as are function return
1464 values. */
1465 #undef TARGET_PROMOTE_FUNCTION_MODE
1466 #define TARGET_PROMOTE_FUNCTION_MODE default_promote_function_mode_always_promote
1468 #undef TARGET_RETURN_IN_MEMORY
1469 #define TARGET_RETURN_IN_MEMORY rs6000_return_in_memory
1471 #undef TARGET_RETURN_IN_MSB
1472 #define TARGET_RETURN_IN_MSB rs6000_return_in_msb
1474 #undef TARGET_SETUP_INCOMING_VARARGS
1475 #define TARGET_SETUP_INCOMING_VARARGS setup_incoming_varargs
1477 /* Always strict argument naming on rs6000. */
1478 #undef TARGET_STRICT_ARGUMENT_NAMING
1479 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
1480 #undef TARGET_PRETEND_OUTGOING_VARARGS_NAMED
1481 #define TARGET_PRETEND_OUTGOING_VARARGS_NAMED hook_bool_CUMULATIVE_ARGS_true
1482 #undef TARGET_SPLIT_COMPLEX_ARG
1483 #define TARGET_SPLIT_COMPLEX_ARG hook_bool_const_tree_true
1484 #undef TARGET_MUST_PASS_IN_STACK
1485 #define TARGET_MUST_PASS_IN_STACK rs6000_must_pass_in_stack
1486 #undef TARGET_PASS_BY_REFERENCE
1487 #define TARGET_PASS_BY_REFERENCE rs6000_pass_by_reference
1488 #undef TARGET_ARG_PARTIAL_BYTES
1489 #define TARGET_ARG_PARTIAL_BYTES rs6000_arg_partial_bytes
1490 #undef TARGET_FUNCTION_ARG_ADVANCE
1491 #define TARGET_FUNCTION_ARG_ADVANCE rs6000_function_arg_advance
1492 #undef TARGET_FUNCTION_ARG
1493 #define TARGET_FUNCTION_ARG rs6000_function_arg
1494 #undef TARGET_FUNCTION_ARG_BOUNDARY
1495 #define TARGET_FUNCTION_ARG_BOUNDARY rs6000_function_arg_boundary
1497 #undef TARGET_BUILD_BUILTIN_VA_LIST
1498 #define TARGET_BUILD_BUILTIN_VA_LIST rs6000_build_builtin_va_list
1500 #undef TARGET_EXPAND_BUILTIN_VA_START
1501 #define TARGET_EXPAND_BUILTIN_VA_START rs6000_va_start
1503 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
1504 #define TARGET_GIMPLIFY_VA_ARG_EXPR rs6000_gimplify_va_arg
1506 #undef TARGET_EH_RETURN_FILTER_MODE
1507 #define TARGET_EH_RETURN_FILTER_MODE rs6000_eh_return_filter_mode
1509 #undef TARGET_SCALAR_MODE_SUPPORTED_P
1510 #define TARGET_SCALAR_MODE_SUPPORTED_P rs6000_scalar_mode_supported_p
1512 #undef TARGET_VECTOR_MODE_SUPPORTED_P
1513 #define TARGET_VECTOR_MODE_SUPPORTED_P rs6000_vector_mode_supported_p
1515 #undef TARGET_INVALID_ARG_FOR_UNPROTOTYPED_FN
1516 #define TARGET_INVALID_ARG_FOR_UNPROTOTYPED_FN invalid_arg_for_unprototyped_fn
1518 #undef TARGET_ASM_LOOP_ALIGN_MAX_SKIP
1519 #define TARGET_ASM_LOOP_ALIGN_MAX_SKIP rs6000_loop_align_max_skip
1521 #undef TARGET_OPTION_OVERRIDE
1522 #define TARGET_OPTION_OVERRIDE rs6000_option_override
1524 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION
1525 #define TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION \
1526 rs6000_builtin_vectorized_function
1528 #if !TARGET_MACHO
1529 #undef TARGET_STACK_PROTECT_FAIL
1530 #define TARGET_STACK_PROTECT_FAIL rs6000_stack_protect_fail
1531 #endif
1533 /* MPC604EUM 3.5.2 Weak Consistency between Multiple Processors
1534 The PowerPC architecture requires only weak consistency among
1535 processors--that is, memory accesses between processors need not be
1536 sequentially consistent and memory accesses among processors can occur
1537 in any order. The ability to order memory accesses weakly provides
1538 opportunities for more efficient use of the system bus. Unless a
1539 dependency exists, the 604e allows read operations to precede store
1540 operations. */
1541 #undef TARGET_RELAXED_ORDERING
1542 #define TARGET_RELAXED_ORDERING true
1544 #ifdef HAVE_AS_TLS
1545 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
1546 #define TARGET_ASM_OUTPUT_DWARF_DTPREL rs6000_output_dwarf_dtprel
1547 #endif
1549 /* Use a 32-bit anchor range. This leads to sequences like:
1551 addis tmp,anchor,high
1552 add dest,tmp,low
1554 where tmp itself acts as an anchor, and can be shared between
1555 accesses to the same 64k page. */
1556 #undef TARGET_MIN_ANCHOR_OFFSET
1557 #define TARGET_MIN_ANCHOR_OFFSET -0x7fffffff - 1
1558 #undef TARGET_MAX_ANCHOR_OFFSET
1559 #define TARGET_MAX_ANCHOR_OFFSET 0x7fffffff
1560 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
1561 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P rs6000_use_blocks_for_constant_p
1562 #undef TARGET_USE_BLOCKS_FOR_DECL_P
1563 #define TARGET_USE_BLOCKS_FOR_DECL_P rs6000_use_blocks_for_decl_p
1565 #undef TARGET_BUILTIN_RECIPROCAL
1566 #define TARGET_BUILTIN_RECIPROCAL rs6000_builtin_reciprocal
1568 #undef TARGET_EXPAND_TO_RTL_HOOK
1569 #define TARGET_EXPAND_TO_RTL_HOOK rs6000_alloc_sdmode_stack_slot
1571 #undef TARGET_INSTANTIATE_DECLS
1572 #define TARGET_INSTANTIATE_DECLS rs6000_instantiate_decls
1574 #undef TARGET_SECONDARY_RELOAD
1575 #define TARGET_SECONDARY_RELOAD rs6000_secondary_reload
1577 #undef TARGET_LEGITIMATE_ADDRESS_P
1578 #define TARGET_LEGITIMATE_ADDRESS_P rs6000_legitimate_address_p
1580 #undef TARGET_MODE_DEPENDENT_ADDRESS_P
1581 #define TARGET_MODE_DEPENDENT_ADDRESS_P rs6000_mode_dependent_address_p
1583 #undef TARGET_LRA_P
1584 #define TARGET_LRA_P rs6000_lra_p
1586 #undef TARGET_CAN_ELIMINATE
1587 #define TARGET_CAN_ELIMINATE rs6000_can_eliminate
1589 #undef TARGET_CONDITIONAL_REGISTER_USAGE
1590 #define TARGET_CONDITIONAL_REGISTER_USAGE rs6000_conditional_register_usage
1592 #undef TARGET_TRAMPOLINE_INIT
1593 #define TARGET_TRAMPOLINE_INIT rs6000_trampoline_init
1595 #undef TARGET_FUNCTION_VALUE
1596 #define TARGET_FUNCTION_VALUE rs6000_function_value
1598 #undef TARGET_OPTION_VALID_ATTRIBUTE_P
1599 #define TARGET_OPTION_VALID_ATTRIBUTE_P rs6000_valid_attribute_p
1601 #undef TARGET_OPTION_SAVE
1602 #define TARGET_OPTION_SAVE rs6000_function_specific_save
1604 #undef TARGET_OPTION_RESTORE
1605 #define TARGET_OPTION_RESTORE rs6000_function_specific_restore
1607 #undef TARGET_OPTION_PRINT
1608 #define TARGET_OPTION_PRINT rs6000_function_specific_print
1610 #undef TARGET_CAN_INLINE_P
1611 #define TARGET_CAN_INLINE_P rs6000_can_inline_p
1613 #undef TARGET_SET_CURRENT_FUNCTION
1614 #define TARGET_SET_CURRENT_FUNCTION rs6000_set_current_function
1616 #undef TARGET_LEGITIMATE_CONSTANT_P
1617 #define TARGET_LEGITIMATE_CONSTANT_P rs6000_legitimate_constant_p
1619 #undef TARGET_VECTORIZE_VEC_PERM_CONST_OK
1620 #define TARGET_VECTORIZE_VEC_PERM_CONST_OK rs6000_vectorize_vec_perm_const_ok
1622 #undef TARGET_CAN_USE_DOLOOP_P
1623 #define TARGET_CAN_USE_DOLOOP_P can_use_doloop_if_innermost
1626 /* Processor table. */
1627 struct rs6000_ptt
1629 const char *const name; /* Canonical processor name. */
1630 const enum processor_type processor; /* Processor type enum value. */
1631 const HOST_WIDE_INT target_enable; /* Target flags to enable. */
1634 static struct rs6000_ptt const processor_target_table[] =
1636 #define RS6000_CPU(NAME, CPU, FLAGS) { NAME, CPU, FLAGS },
1637 #include "rs6000-cpus.def"
1638 #undef RS6000_CPU
1641 /* Look up a processor name for -mcpu=xxx and -mtune=xxx. Return -1 if the
1642 name is invalid. */
1644 static int
1645 rs6000_cpu_name_lookup (const char *name)
1647 size_t i;
1649 if (name != NULL)
1651 for (i = 0; i < ARRAY_SIZE (processor_target_table); i++)
1652 if (! strcmp (name, processor_target_table[i].name))
1653 return (int)i;
1656 return -1;
1660 /* Return number of consecutive hard regs needed starting at reg REGNO
1661 to hold something of mode MODE.
1662 This is ordinarily the length in words of a value of mode MODE
1663 but can be less for certain modes in special long registers.
1665 For the SPE, GPRs are 64 bits but only 32 bits are visible in
1666 scalar instructions. The upper 32 bits are only available to the
1667 SIMD instructions.
1669 POWER and PowerPC GPRs hold 32 bits worth;
1670 PowerPC64 GPRs and FPRs point register holds 64 bits worth. */
1672 static int
1673 rs6000_hard_regno_nregs_internal (int regno, enum machine_mode mode)
1675 unsigned HOST_WIDE_INT reg_size;
1677 /* TF/TD modes are special in that they always take 2 registers. */
1678 if (FP_REGNO_P (regno))
1679 reg_size = ((VECTOR_MEM_VSX_P (mode) && mode != TDmode && mode != TFmode)
1680 ? UNITS_PER_VSX_WORD
1681 : UNITS_PER_FP_WORD);
1683 else if (SPE_SIMD_REGNO_P (regno) && TARGET_SPE && SPE_VECTOR_MODE (mode))
1684 reg_size = UNITS_PER_SPE_WORD;
1686 else if (ALTIVEC_REGNO_P (regno))
1687 reg_size = UNITS_PER_ALTIVEC_WORD;
1689 /* The value returned for SCmode in the E500 double case is 2 for
1690 ABI compatibility; storing an SCmode value in a single register
1691 would require function_arg and rs6000_spe_function_arg to handle
1692 SCmode so as to pass the value correctly in a pair of
1693 registers. */
1694 else if (TARGET_E500_DOUBLE && FLOAT_MODE_P (mode) && mode != SCmode
1695 && !DECIMAL_FLOAT_MODE_P (mode))
1696 reg_size = UNITS_PER_FP_WORD;
1698 else
1699 reg_size = UNITS_PER_WORD;
1701 return (GET_MODE_SIZE (mode) + reg_size - 1) / reg_size;
1704 /* Value is 1 if hard register REGNO can hold a value of machine-mode
1705 MODE. */
1706 static int
1707 rs6000_hard_regno_mode_ok (int regno, enum machine_mode mode)
1709 int last_regno = regno + rs6000_hard_regno_nregs[mode][regno] - 1;
1711 /* PTImode can only go in GPRs. Quad word memory operations require even/odd
1712 register combinations, and use PTImode where we need to deal with quad
1713 word memory operations. Don't allow quad words in the argument or frame
1714 pointer registers, just registers 0..31. */
1715 if (mode == PTImode)
1716 return (IN_RANGE (regno, FIRST_GPR_REGNO, LAST_GPR_REGNO)
1717 && IN_RANGE (last_regno, FIRST_GPR_REGNO, LAST_GPR_REGNO)
1718 && ((regno & 1) == 0));
1720 /* VSX registers that overlap the FPR registers are larger than for non-VSX
1721 implementations. Don't allow an item to be split between a FP register
1722 and an Altivec register. Allow TImode in all VSX registers if the user
1723 asked for it. */
1724 if (TARGET_VSX && VSX_REGNO_P (regno)
1725 && (VECTOR_MEM_VSX_P (mode)
1726 || (TARGET_VSX_SCALAR_FLOAT && mode == SFmode)
1727 || (TARGET_VSX_SCALAR_DOUBLE && (mode == DFmode || mode == DImode))
1728 || (TARGET_VSX_TIMODE && mode == TImode)
1729 || (TARGET_VADDUQM && mode == V1TImode)))
1731 if (FP_REGNO_P (regno))
1732 return FP_REGNO_P (last_regno);
1734 if (ALTIVEC_REGNO_P (regno))
1736 if (mode == SFmode && !TARGET_UPPER_REGS_SF)
1737 return 0;
1739 if ((mode == DFmode || mode == DImode) && !TARGET_UPPER_REGS_DF)
1740 return 0;
1742 return ALTIVEC_REGNO_P (last_regno);
1746 /* The GPRs can hold any mode, but values bigger than one register
1747 cannot go past R31. */
1748 if (INT_REGNO_P (regno))
1749 return INT_REGNO_P (last_regno);
1751 /* The float registers (except for VSX vector modes) can only hold floating
1752 modes and DImode. */
1753 if (FP_REGNO_P (regno))
1755 if (SCALAR_FLOAT_MODE_P (mode)
1756 && (mode != TDmode || (regno % 2) == 0)
1757 && FP_REGNO_P (last_regno))
1758 return 1;
1760 if (GET_MODE_CLASS (mode) == MODE_INT
1761 && GET_MODE_SIZE (mode) == UNITS_PER_FP_WORD)
1762 return 1;
1764 if (PAIRED_SIMD_REGNO_P (regno) && TARGET_PAIRED_FLOAT
1765 && PAIRED_VECTOR_MODE (mode))
1766 return 1;
1768 return 0;
1771 /* The CR register can only hold CC modes. */
1772 if (CR_REGNO_P (regno))
1773 return GET_MODE_CLASS (mode) == MODE_CC;
1775 if (CA_REGNO_P (regno))
1776 return mode == BImode;
1778 /* AltiVec only in AldyVec registers. */
1779 if (ALTIVEC_REGNO_P (regno))
1780 return (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode)
1781 || mode == V1TImode);
1783 /* ...but GPRs can hold SIMD data on the SPE in one register. */
1784 if (SPE_SIMD_REGNO_P (regno) && TARGET_SPE && SPE_VECTOR_MODE (mode))
1785 return 1;
1787 /* We cannot put non-VSX TImode or PTImode anywhere except general register
1788 and it must be able to fit within the register set. */
1790 return GET_MODE_SIZE (mode) <= UNITS_PER_WORD;
1793 /* Print interesting facts about registers. */
1794 static void
1795 rs6000_debug_reg_print (int first_regno, int last_regno, const char *reg_name)
1797 int r, m;
1799 for (r = first_regno; r <= last_regno; ++r)
1801 const char *comma = "";
1802 int len;
1804 if (first_regno == last_regno)
1805 fprintf (stderr, "%s:\t", reg_name);
1806 else
1807 fprintf (stderr, "%s%d:\t", reg_name, r - first_regno);
1809 len = 8;
1810 for (m = 0; m < NUM_MACHINE_MODES; ++m)
1811 if (rs6000_hard_regno_mode_ok_p[m][r] && rs6000_hard_regno_nregs[m][r])
1813 if (len > 70)
1815 fprintf (stderr, ",\n\t");
1816 len = 8;
1817 comma = "";
1820 if (rs6000_hard_regno_nregs[m][r] > 1)
1821 len += fprintf (stderr, "%s%s/%d", comma, GET_MODE_NAME (m),
1822 rs6000_hard_regno_nregs[m][r]);
1823 else
1824 len += fprintf (stderr, "%s%s", comma, GET_MODE_NAME (m));
1826 comma = ", ";
1829 if (call_used_regs[r])
1831 if (len > 70)
1833 fprintf (stderr, ",\n\t");
1834 len = 8;
1835 comma = "";
1838 len += fprintf (stderr, "%s%s", comma, "call-used");
1839 comma = ", ";
1842 if (fixed_regs[r])
1844 if (len > 70)
1846 fprintf (stderr, ",\n\t");
1847 len = 8;
1848 comma = "";
1851 len += fprintf (stderr, "%s%s", comma, "fixed");
1852 comma = ", ";
1855 if (len > 70)
1857 fprintf (stderr, ",\n\t");
1858 comma = "";
1861 len += fprintf (stderr, "%sreg-class = %s", comma,
1862 reg_class_names[(int)rs6000_regno_regclass[r]]);
1863 comma = ", ";
1865 if (len > 70)
1867 fprintf (stderr, ",\n\t");
1868 comma = "";
1871 fprintf (stderr, "%sregno = %d\n", comma, r);
1875 static const char *
1876 rs6000_debug_vector_unit (enum rs6000_vector v)
1878 const char *ret;
1880 switch (v)
1882 case VECTOR_NONE: ret = "none"; break;
1883 case VECTOR_ALTIVEC: ret = "altivec"; break;
1884 case VECTOR_VSX: ret = "vsx"; break;
1885 case VECTOR_P8_VECTOR: ret = "p8_vector"; break;
1886 case VECTOR_PAIRED: ret = "paired"; break;
1887 case VECTOR_SPE: ret = "spe"; break;
1888 case VECTOR_OTHER: ret = "other"; break;
1889 default: ret = "unknown"; break;
1892 return ret;
1895 /* Print the address masks in a human readble fashion. */
1896 DEBUG_FUNCTION void
1897 rs6000_debug_print_mode (ssize_t m)
1899 ssize_t rc;
1901 fprintf (stderr, "Mode: %-5s", GET_MODE_NAME (m));
1902 for (rc = 0; rc < N_RELOAD_REG; rc++)
1904 addr_mask_type mask = reg_addr[m].addr_mask[rc];
1905 fprintf (stderr,
1906 " %s: %c%c%c%c%c%c",
1907 reload_reg_map[rc].name,
1908 (mask & RELOAD_REG_VALID) != 0 ? 'v' : ' ',
1909 (mask & RELOAD_REG_MULTIPLE) != 0 ? 'm' : ' ',
1910 (mask & RELOAD_REG_INDEXED) != 0 ? 'i' : ' ',
1911 (mask & RELOAD_REG_OFFSET) != 0 ? 'o' : ' ',
1912 (mask & RELOAD_REG_PRE_INCDEC) != 0 ? '+' : ' ',
1913 (mask & RELOAD_REG_PRE_MODIFY) != 0 ? '+' : ' ');
1916 if (rs6000_vector_unit[m] != VECTOR_NONE
1917 || rs6000_vector_mem[m] != VECTOR_NONE
1918 || (reg_addr[m].reload_store != CODE_FOR_nothing)
1919 || (reg_addr[m].reload_load != CODE_FOR_nothing))
1921 fprintf (stderr,
1922 " Vector-arith=%-10s Vector-mem=%-10s Reload=%c%c",
1923 rs6000_debug_vector_unit (rs6000_vector_unit[m]),
1924 rs6000_debug_vector_unit (rs6000_vector_mem[m]),
1925 (reg_addr[m].reload_store != CODE_FOR_nothing) ? 's' : '*',
1926 (reg_addr[m].reload_load != CODE_FOR_nothing) ? 'l' : '*');
1929 fputs ("\n", stderr);
1932 #define DEBUG_FMT_ID "%-32s= "
1933 #define DEBUG_FMT_D DEBUG_FMT_ID "%d\n"
1934 #define DEBUG_FMT_WX DEBUG_FMT_ID "%#.12" HOST_WIDE_INT_PRINT "x: "
1935 #define DEBUG_FMT_S DEBUG_FMT_ID "%s\n"
1937 /* Print various interesting information with -mdebug=reg. */
1938 static void
1939 rs6000_debug_reg_global (void)
1941 static const char *const tf[2] = { "false", "true" };
1942 const char *nl = (const char *)0;
1943 int m;
1944 size_t m1, m2, v;
1945 char costly_num[20];
1946 char nop_num[20];
1947 char flags_buffer[40];
1948 const char *costly_str;
1949 const char *nop_str;
1950 const char *trace_str;
1951 const char *abi_str;
1952 const char *cmodel_str;
1953 struct cl_target_option cl_opts;
1955 /* Modes we want tieable information on. */
1956 static const enum machine_mode print_tieable_modes[] = {
1957 QImode,
1958 HImode,
1959 SImode,
1960 DImode,
1961 TImode,
1962 PTImode,
1963 SFmode,
1964 DFmode,
1965 TFmode,
1966 SDmode,
1967 DDmode,
1968 TDmode,
1969 V8QImode,
1970 V4HImode,
1971 V2SImode,
1972 V16QImode,
1973 V8HImode,
1974 V4SImode,
1975 V2DImode,
1976 V1TImode,
1977 V32QImode,
1978 V16HImode,
1979 V8SImode,
1980 V4DImode,
1981 V2TImode,
1982 V2SFmode,
1983 V4SFmode,
1984 V2DFmode,
1985 V8SFmode,
1986 V4DFmode,
1987 CCmode,
1988 CCUNSmode,
1989 CCEQmode,
1992 /* Virtual regs we are interested in. */
1993 const static struct {
1994 int regno; /* register number. */
1995 const char *name; /* register name. */
1996 } virtual_regs[] = {
1997 { STACK_POINTER_REGNUM, "stack pointer:" },
1998 { TOC_REGNUM, "toc: " },
1999 { STATIC_CHAIN_REGNUM, "static chain: " },
2000 { RS6000_PIC_OFFSET_TABLE_REGNUM, "pic offset: " },
2001 { HARD_FRAME_POINTER_REGNUM, "hard frame: " },
2002 { ARG_POINTER_REGNUM, "arg pointer: " },
2003 { FRAME_POINTER_REGNUM, "frame pointer:" },
2004 { FIRST_PSEUDO_REGISTER, "first pseudo: " },
2005 { FIRST_VIRTUAL_REGISTER, "first virtual:" },
2006 { VIRTUAL_INCOMING_ARGS_REGNUM, "incoming_args:" },
2007 { VIRTUAL_STACK_VARS_REGNUM, "stack_vars: " },
2008 { VIRTUAL_STACK_DYNAMIC_REGNUM, "stack_dynamic:" },
2009 { VIRTUAL_OUTGOING_ARGS_REGNUM, "outgoing_args:" },
2010 { VIRTUAL_CFA_REGNUM, "cfa (frame): " },
2011 { VIRTUAL_PREFERRED_STACK_BOUNDARY_REGNUM, "stack boundry:" },
2012 { LAST_VIRTUAL_REGISTER, "last virtual: " },
2015 fputs ("\nHard register information:\n", stderr);
2016 rs6000_debug_reg_print (FIRST_GPR_REGNO, LAST_GPR_REGNO, "gr");
2017 rs6000_debug_reg_print (FIRST_FPR_REGNO, LAST_FPR_REGNO, "fp");
2018 rs6000_debug_reg_print (FIRST_ALTIVEC_REGNO,
2019 LAST_ALTIVEC_REGNO,
2020 "vs");
2021 rs6000_debug_reg_print (LR_REGNO, LR_REGNO, "lr");
2022 rs6000_debug_reg_print (CTR_REGNO, CTR_REGNO, "ctr");
2023 rs6000_debug_reg_print (CR0_REGNO, CR7_REGNO, "cr");
2024 rs6000_debug_reg_print (CA_REGNO, CA_REGNO, "ca");
2025 rs6000_debug_reg_print (VRSAVE_REGNO, VRSAVE_REGNO, "vrsave");
2026 rs6000_debug_reg_print (VSCR_REGNO, VSCR_REGNO, "vscr");
2027 rs6000_debug_reg_print (SPE_ACC_REGNO, SPE_ACC_REGNO, "spe_a");
2028 rs6000_debug_reg_print (SPEFSCR_REGNO, SPEFSCR_REGNO, "spe_f");
2030 fputs ("\nVirtual/stack/frame registers:\n", stderr);
2031 for (v = 0; v < ARRAY_SIZE (virtual_regs); v++)
2032 fprintf (stderr, "%s regno = %3d\n", virtual_regs[v].name, virtual_regs[v].regno);
2034 fprintf (stderr,
2035 "\n"
2036 "d reg_class = %s\n"
2037 "f reg_class = %s\n"
2038 "v reg_class = %s\n"
2039 "wa reg_class = %s\n"
2040 "wd reg_class = %s\n"
2041 "wf reg_class = %s\n"
2042 "wg reg_class = %s\n"
2043 "wl reg_class = %s\n"
2044 "wm reg_class = %s\n"
2045 "wr reg_class = %s\n"
2046 "ws reg_class = %s\n"
2047 "wt reg_class = %s\n"
2048 "wu reg_class = %s\n"
2049 "wv reg_class = %s\n"
2050 "ww reg_class = %s\n"
2051 "wx reg_class = %s\n"
2052 "wy reg_class = %s\n"
2053 "wz reg_class = %s\n"
2054 "\n",
2055 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_d]],
2056 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_f]],
2057 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_v]],
2058 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wa]],
2059 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wd]],
2060 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wf]],
2061 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wg]],
2062 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wl]],
2063 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wm]],
2064 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wr]],
2065 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_ws]],
2066 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wt]],
2067 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wu]],
2068 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wv]],
2069 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_ww]],
2070 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wx]],
2071 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wy]],
2072 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wz]]);
2074 nl = "\n";
2075 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2076 rs6000_debug_print_mode (m);
2078 fputs ("\n", stderr);
2080 for (m1 = 0; m1 < ARRAY_SIZE (print_tieable_modes); m1++)
2082 enum machine_mode mode1 = print_tieable_modes[m1];
2083 bool first_time = true;
2085 nl = (const char *)0;
2086 for (m2 = 0; m2 < ARRAY_SIZE (print_tieable_modes); m2++)
2088 enum machine_mode mode2 = print_tieable_modes[m2];
2089 if (mode1 != mode2 && MODES_TIEABLE_P (mode1, mode2))
2091 if (first_time)
2093 fprintf (stderr, "Tieable modes %s:", GET_MODE_NAME (mode1));
2094 nl = "\n";
2095 first_time = false;
2098 fprintf (stderr, " %s", GET_MODE_NAME (mode2));
2102 if (!first_time)
2103 fputs ("\n", stderr);
2106 if (nl)
2107 fputs (nl, stderr);
2109 if (rs6000_recip_control)
2111 fprintf (stderr, "\nReciprocal mask = 0x%x\n", rs6000_recip_control);
2113 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2114 if (rs6000_recip_bits[m])
2116 fprintf (stderr,
2117 "Reciprocal estimate mode: %-5s divide: %s rsqrt: %s\n",
2118 GET_MODE_NAME (m),
2119 (RS6000_RECIP_AUTO_RE_P (m)
2120 ? "auto"
2121 : (RS6000_RECIP_HAVE_RE_P (m) ? "have" : "none")),
2122 (RS6000_RECIP_AUTO_RSQRTE_P (m)
2123 ? "auto"
2124 : (RS6000_RECIP_HAVE_RSQRTE_P (m) ? "have" : "none")));
2127 fputs ("\n", stderr);
2130 if (rs6000_cpu_index >= 0)
2132 const char *name = processor_target_table[rs6000_cpu_index].name;
2133 HOST_WIDE_INT flags
2134 = processor_target_table[rs6000_cpu_index].target_enable;
2136 sprintf (flags_buffer, "-mcpu=%s flags", name);
2137 rs6000_print_isa_options (stderr, 0, flags_buffer, flags);
2139 else
2140 fprintf (stderr, DEBUG_FMT_S, "cpu", "<none>");
2142 if (rs6000_tune_index >= 0)
2144 const char *name = processor_target_table[rs6000_tune_index].name;
2145 HOST_WIDE_INT flags
2146 = processor_target_table[rs6000_tune_index].target_enable;
2148 sprintf (flags_buffer, "-mtune=%s flags", name);
2149 rs6000_print_isa_options (stderr, 0, flags_buffer, flags);
2151 else
2152 fprintf (stderr, DEBUG_FMT_S, "tune", "<none>");
2154 cl_target_option_save (&cl_opts, &global_options);
2155 rs6000_print_isa_options (stderr, 0, "rs6000_isa_flags",
2156 rs6000_isa_flags);
2158 rs6000_print_isa_options (stderr, 0, "rs6000_isa_flags_explicit",
2159 rs6000_isa_flags_explicit);
2161 rs6000_print_builtin_options (stderr, 0, "rs6000_builtin_mask",
2162 rs6000_builtin_mask);
2164 rs6000_print_isa_options (stderr, 0, "TARGET_DEFAULT", TARGET_DEFAULT);
2166 fprintf (stderr, DEBUG_FMT_S, "--with-cpu default",
2167 OPTION_TARGET_CPU_DEFAULT ? OPTION_TARGET_CPU_DEFAULT : "<none>");
2169 switch (rs6000_sched_costly_dep)
2171 case max_dep_latency:
2172 costly_str = "max_dep_latency";
2173 break;
2175 case no_dep_costly:
2176 costly_str = "no_dep_costly";
2177 break;
2179 case all_deps_costly:
2180 costly_str = "all_deps_costly";
2181 break;
2183 case true_store_to_load_dep_costly:
2184 costly_str = "true_store_to_load_dep_costly";
2185 break;
2187 case store_to_load_dep_costly:
2188 costly_str = "store_to_load_dep_costly";
2189 break;
2191 default:
2192 costly_str = costly_num;
2193 sprintf (costly_num, "%d", (int)rs6000_sched_costly_dep);
2194 break;
2197 fprintf (stderr, DEBUG_FMT_S, "sched_costly_dep", costly_str);
2199 switch (rs6000_sched_insert_nops)
2201 case sched_finish_regroup_exact:
2202 nop_str = "sched_finish_regroup_exact";
2203 break;
2205 case sched_finish_pad_groups:
2206 nop_str = "sched_finish_pad_groups";
2207 break;
2209 case sched_finish_none:
2210 nop_str = "sched_finish_none";
2211 break;
2213 default:
2214 nop_str = nop_num;
2215 sprintf (nop_num, "%d", (int)rs6000_sched_insert_nops);
2216 break;
2219 fprintf (stderr, DEBUG_FMT_S, "sched_insert_nops", nop_str);
2221 switch (rs6000_sdata)
2223 default:
2224 case SDATA_NONE:
2225 break;
2227 case SDATA_DATA:
2228 fprintf (stderr, DEBUG_FMT_S, "sdata", "data");
2229 break;
2231 case SDATA_SYSV:
2232 fprintf (stderr, DEBUG_FMT_S, "sdata", "sysv");
2233 break;
2235 case SDATA_EABI:
2236 fprintf (stderr, DEBUG_FMT_S, "sdata", "eabi");
2237 break;
2241 switch (rs6000_traceback)
2243 case traceback_default: trace_str = "default"; break;
2244 case traceback_none: trace_str = "none"; break;
2245 case traceback_part: trace_str = "part"; break;
2246 case traceback_full: trace_str = "full"; break;
2247 default: trace_str = "unknown"; break;
2250 fprintf (stderr, DEBUG_FMT_S, "traceback", trace_str);
2252 switch (rs6000_current_cmodel)
2254 case CMODEL_SMALL: cmodel_str = "small"; break;
2255 case CMODEL_MEDIUM: cmodel_str = "medium"; break;
2256 case CMODEL_LARGE: cmodel_str = "large"; break;
2257 default: cmodel_str = "unknown"; break;
2260 fprintf (stderr, DEBUG_FMT_S, "cmodel", cmodel_str);
2262 switch (rs6000_current_abi)
2264 case ABI_NONE: abi_str = "none"; break;
2265 case ABI_AIX: abi_str = "aix"; break;
2266 case ABI_ELFv2: abi_str = "ELFv2"; break;
2267 case ABI_V4: abi_str = "V4"; break;
2268 case ABI_DARWIN: abi_str = "darwin"; break;
2269 default: abi_str = "unknown"; break;
2272 fprintf (stderr, DEBUG_FMT_S, "abi", abi_str);
2274 if (rs6000_altivec_abi)
2275 fprintf (stderr, DEBUG_FMT_S, "altivec_abi", "true");
2277 if (rs6000_spe_abi)
2278 fprintf (stderr, DEBUG_FMT_S, "spe_abi", "true");
2280 if (rs6000_darwin64_abi)
2281 fprintf (stderr, DEBUG_FMT_S, "darwin64_abi", "true");
2283 if (rs6000_float_gprs)
2284 fprintf (stderr, DEBUG_FMT_S, "float_gprs", "true");
2286 fprintf (stderr, DEBUG_FMT_S, "fprs",
2287 (TARGET_FPRS ? "true" : "false"));
2289 fprintf (stderr, DEBUG_FMT_S, "single_float",
2290 (TARGET_SINGLE_FLOAT ? "true" : "false"));
2292 fprintf (stderr, DEBUG_FMT_S, "double_float",
2293 (TARGET_DOUBLE_FLOAT ? "true" : "false"));
2295 fprintf (stderr, DEBUG_FMT_S, "soft_float",
2296 (TARGET_SOFT_FLOAT ? "true" : "false"));
2298 fprintf (stderr, DEBUG_FMT_S, "e500_single",
2299 (TARGET_E500_SINGLE ? "true" : "false"));
2301 fprintf (stderr, DEBUG_FMT_S, "e500_double",
2302 (TARGET_E500_DOUBLE ? "true" : "false"));
2304 if (TARGET_LINK_STACK)
2305 fprintf (stderr, DEBUG_FMT_S, "link_stack", "true");
2307 if (targetm.lra_p ())
2308 fprintf (stderr, DEBUG_FMT_S, "lra", "true");
2310 if (TARGET_P8_FUSION)
2311 fprintf (stderr, DEBUG_FMT_S, "p8 fusion",
2312 (TARGET_P8_FUSION_SIGN) ? "zero+sign" : "zero");
2314 fprintf (stderr, DEBUG_FMT_S, "plt-format",
2315 TARGET_SECURE_PLT ? "secure" : "bss");
2316 fprintf (stderr, DEBUG_FMT_S, "struct-return",
2317 aix_struct_return ? "aix" : "sysv");
2318 fprintf (stderr, DEBUG_FMT_S, "always_hint", tf[!!rs6000_always_hint]);
2319 fprintf (stderr, DEBUG_FMT_S, "sched_groups", tf[!!rs6000_sched_groups]);
2320 fprintf (stderr, DEBUG_FMT_S, "align_branch",
2321 tf[!!rs6000_align_branch_targets]);
2322 fprintf (stderr, DEBUG_FMT_D, "tls_size", rs6000_tls_size);
2323 fprintf (stderr, DEBUG_FMT_D, "long_double_size",
2324 rs6000_long_double_type_size);
2325 fprintf (stderr, DEBUG_FMT_D, "sched_restricted_insns_priority",
2326 (int)rs6000_sched_restricted_insns_priority);
2327 fprintf (stderr, DEBUG_FMT_D, "Number of standard builtins",
2328 (int)END_BUILTINS);
2329 fprintf (stderr, DEBUG_FMT_D, "Number of rs6000 builtins",
2330 (int)RS6000_BUILTIN_COUNT);
2332 if (TARGET_VSX)
2333 fprintf (stderr, DEBUG_FMT_D, "VSX easy 64-bit scalar element",
2334 (int)VECTOR_ELEMENT_SCALAR_64BIT);
2338 /* Update the addr mask bits in reg_addr to help secondary reload and go if
2339 legitimate address support to figure out the appropriate addressing to
2340 use. */
2342 static void
2343 rs6000_setup_reg_addr_masks (void)
2345 ssize_t rc, reg, m, nregs;
2346 addr_mask_type any_addr_mask, addr_mask;
2348 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2350 enum machine_mode m2 = (enum machine_mode)m;
2352 /* SDmode is special in that we want to access it only via REG+REG
2353 addressing on power7 and above, since we want to use the LFIWZX and
2354 STFIWZX instructions to load it. */
2355 bool indexed_only_p = (m == SDmode && TARGET_NO_SDMODE_STACK);
2357 any_addr_mask = 0;
2358 for (rc = FIRST_RELOAD_REG_CLASS; rc <= LAST_RELOAD_REG_CLASS; rc++)
2360 addr_mask = 0;
2361 reg = reload_reg_map[rc].reg;
2363 /* Can mode values go in the GPR/FPR/Altivec registers? */
2364 if (reg >= 0 && rs6000_hard_regno_mode_ok_p[m][reg])
2366 nregs = rs6000_hard_regno_nregs[m][reg];
2367 addr_mask |= RELOAD_REG_VALID;
2369 /* Indicate if the mode takes more than 1 physical register. If
2370 it takes a single register, indicate it can do REG+REG
2371 addressing. */
2372 if (nregs > 1 || m == BLKmode)
2373 addr_mask |= RELOAD_REG_MULTIPLE;
2374 else
2375 addr_mask |= RELOAD_REG_INDEXED;
2377 /* Figure out if we can do PRE_INC, PRE_DEC, or PRE_MODIFY
2378 addressing. Restrict addressing on SPE for 64-bit types
2379 because of the SUBREG hackery used to address 64-bit floats in
2380 '32-bit' GPRs. To simplify secondary reload, don't allow
2381 update forms on scalar floating point types that can go in the
2382 upper registers. */
2384 if (TARGET_UPDATE
2385 && (rc == RELOAD_REG_GPR || rc == RELOAD_REG_FPR)
2386 && GET_MODE_SIZE (m2) <= 8
2387 && !VECTOR_MODE_P (m2)
2388 && !COMPLEX_MODE_P (m2)
2389 && !indexed_only_p
2390 && !(TARGET_E500_DOUBLE && GET_MODE_SIZE (m2) == 8)
2391 && !(m2 == DFmode && TARGET_UPPER_REGS_DF)
2392 && !(m2 == SFmode && TARGET_UPPER_REGS_SF))
2394 addr_mask |= RELOAD_REG_PRE_INCDEC;
2396 /* PRE_MODIFY is more restricted than PRE_INC/PRE_DEC in that
2397 we don't allow PRE_MODIFY for some multi-register
2398 operations. */
2399 switch (m)
2401 default:
2402 addr_mask |= RELOAD_REG_PRE_MODIFY;
2403 break;
2405 case DImode:
2406 if (TARGET_POWERPC64)
2407 addr_mask |= RELOAD_REG_PRE_MODIFY;
2408 break;
2410 case DFmode:
2411 case DDmode:
2412 if (TARGET_DF_INSN)
2413 addr_mask |= RELOAD_REG_PRE_MODIFY;
2414 break;
2419 /* GPR and FPR registers can do REG+OFFSET addressing, except
2420 possibly for SDmode. */
2421 if ((addr_mask != 0) && !indexed_only_p
2422 && (rc == RELOAD_REG_GPR || rc == RELOAD_REG_FPR))
2423 addr_mask |= RELOAD_REG_OFFSET;
2425 reg_addr[m].addr_mask[rc] = addr_mask;
2426 any_addr_mask |= addr_mask;
2429 reg_addr[m].addr_mask[RELOAD_REG_ANY] = any_addr_mask;
2434 /* Initialize the various global tables that are based on register size. */
2435 static void
2436 rs6000_init_hard_regno_mode_ok (bool global_init_p)
2438 ssize_t r, m, c;
2439 int align64;
2440 int align32;
2442 /* Precalculate REGNO_REG_CLASS. */
2443 rs6000_regno_regclass[0] = GENERAL_REGS;
2444 for (r = 1; r < 32; ++r)
2445 rs6000_regno_regclass[r] = BASE_REGS;
2447 for (r = 32; r < 64; ++r)
2448 rs6000_regno_regclass[r] = FLOAT_REGS;
2450 for (r = 64; r < FIRST_PSEUDO_REGISTER; ++r)
2451 rs6000_regno_regclass[r] = NO_REGS;
2453 for (r = FIRST_ALTIVEC_REGNO; r <= LAST_ALTIVEC_REGNO; ++r)
2454 rs6000_regno_regclass[r] = ALTIVEC_REGS;
2456 rs6000_regno_regclass[CR0_REGNO] = CR0_REGS;
2457 for (r = CR1_REGNO; r <= CR7_REGNO; ++r)
2458 rs6000_regno_regclass[r] = CR_REGS;
2460 rs6000_regno_regclass[LR_REGNO] = LINK_REGS;
2461 rs6000_regno_regclass[CTR_REGNO] = CTR_REGS;
2462 rs6000_regno_regclass[CA_REGNO] = CA_REGS;
2463 rs6000_regno_regclass[VRSAVE_REGNO] = VRSAVE_REGS;
2464 rs6000_regno_regclass[VSCR_REGNO] = VRSAVE_REGS;
2465 rs6000_regno_regclass[SPE_ACC_REGNO] = SPE_ACC_REGS;
2466 rs6000_regno_regclass[SPEFSCR_REGNO] = SPEFSCR_REGS;
2467 rs6000_regno_regclass[TFHAR_REGNO] = SPR_REGS;
2468 rs6000_regno_regclass[TFIAR_REGNO] = SPR_REGS;
2469 rs6000_regno_regclass[TEXASR_REGNO] = SPR_REGS;
2470 rs6000_regno_regclass[ARG_POINTER_REGNUM] = BASE_REGS;
2471 rs6000_regno_regclass[FRAME_POINTER_REGNUM] = BASE_REGS;
2473 /* Precalculate register class to simpler reload register class. We don't
2474 need all of the register classes that are combinations of different
2475 classes, just the simple ones that have constraint letters. */
2476 for (c = 0; c < N_REG_CLASSES; c++)
2477 reg_class_to_reg_type[c] = NO_REG_TYPE;
2479 reg_class_to_reg_type[(int)GENERAL_REGS] = GPR_REG_TYPE;
2480 reg_class_to_reg_type[(int)BASE_REGS] = GPR_REG_TYPE;
2481 reg_class_to_reg_type[(int)VSX_REGS] = VSX_REG_TYPE;
2482 reg_class_to_reg_type[(int)VRSAVE_REGS] = SPR_REG_TYPE;
2483 reg_class_to_reg_type[(int)VSCR_REGS] = SPR_REG_TYPE;
2484 reg_class_to_reg_type[(int)LINK_REGS] = SPR_REG_TYPE;
2485 reg_class_to_reg_type[(int)CTR_REGS] = SPR_REG_TYPE;
2486 reg_class_to_reg_type[(int)LINK_OR_CTR_REGS] = SPR_REG_TYPE;
2487 reg_class_to_reg_type[(int)CR_REGS] = CR_REG_TYPE;
2488 reg_class_to_reg_type[(int)CR0_REGS] = CR_REG_TYPE;
2489 reg_class_to_reg_type[(int)SPE_ACC_REGS] = SPE_ACC_TYPE;
2490 reg_class_to_reg_type[(int)SPEFSCR_REGS] = SPEFSCR_REG_TYPE;
2492 if (TARGET_VSX)
2494 reg_class_to_reg_type[(int)FLOAT_REGS] = VSX_REG_TYPE;
2495 reg_class_to_reg_type[(int)ALTIVEC_REGS] = VSX_REG_TYPE;
2497 else
2499 reg_class_to_reg_type[(int)FLOAT_REGS] = FPR_REG_TYPE;
2500 reg_class_to_reg_type[(int)ALTIVEC_REGS] = ALTIVEC_REG_TYPE;
2503 /* Precalculate the valid memory formats as well as the vector information,
2504 this must be set up before the rs6000_hard_regno_nregs_internal calls
2505 below. */
2506 gcc_assert ((int)VECTOR_NONE == 0);
2507 memset ((void *) &rs6000_vector_unit[0], '\0', sizeof (rs6000_vector_unit));
2508 memset ((void *) &rs6000_vector_mem[0], '\0', sizeof (rs6000_vector_unit));
2510 gcc_assert ((int)CODE_FOR_nothing == 0);
2511 memset ((void *) &reg_addr[0], '\0', sizeof (reg_addr));
2513 gcc_assert ((int)NO_REGS == 0);
2514 memset ((void *) &rs6000_constraints[0], '\0', sizeof (rs6000_constraints));
2516 /* The VSX hardware allows native alignment for vectors, but control whether the compiler
2517 believes it can use native alignment or still uses 128-bit alignment. */
2518 if (TARGET_VSX && !TARGET_VSX_ALIGN_128)
2520 align64 = 64;
2521 align32 = 32;
2523 else
2525 align64 = 128;
2526 align32 = 128;
2529 /* V2DF mode, VSX only. */
2530 if (TARGET_VSX)
2532 rs6000_vector_unit[V2DFmode] = VECTOR_VSX;
2533 rs6000_vector_mem[V2DFmode] = VECTOR_VSX;
2534 rs6000_vector_align[V2DFmode] = align64;
2537 /* V4SF mode, either VSX or Altivec. */
2538 if (TARGET_VSX)
2540 rs6000_vector_unit[V4SFmode] = VECTOR_VSX;
2541 rs6000_vector_mem[V4SFmode] = VECTOR_VSX;
2542 rs6000_vector_align[V4SFmode] = align32;
2544 else if (TARGET_ALTIVEC)
2546 rs6000_vector_unit[V4SFmode] = VECTOR_ALTIVEC;
2547 rs6000_vector_mem[V4SFmode] = VECTOR_ALTIVEC;
2548 rs6000_vector_align[V4SFmode] = align32;
2551 /* V16QImode, V8HImode, V4SImode are Altivec only, but possibly do VSX loads
2552 and stores. */
2553 if (TARGET_ALTIVEC)
2555 rs6000_vector_unit[V4SImode] = VECTOR_ALTIVEC;
2556 rs6000_vector_unit[V8HImode] = VECTOR_ALTIVEC;
2557 rs6000_vector_unit[V16QImode] = VECTOR_ALTIVEC;
2558 rs6000_vector_align[V4SImode] = align32;
2559 rs6000_vector_align[V8HImode] = align32;
2560 rs6000_vector_align[V16QImode] = align32;
2562 if (TARGET_VSX)
2564 rs6000_vector_mem[V4SImode] = VECTOR_VSX;
2565 rs6000_vector_mem[V8HImode] = VECTOR_VSX;
2566 rs6000_vector_mem[V16QImode] = VECTOR_VSX;
2568 else
2570 rs6000_vector_mem[V4SImode] = VECTOR_ALTIVEC;
2571 rs6000_vector_mem[V8HImode] = VECTOR_ALTIVEC;
2572 rs6000_vector_mem[V16QImode] = VECTOR_ALTIVEC;
2576 /* V2DImode, full mode depends on ISA 2.07 vector mode. Allow under VSX to
2577 do insert/splat/extract. Altivec doesn't have 64-bit integer support. */
2578 if (TARGET_VSX)
2580 rs6000_vector_mem[V2DImode] = VECTOR_VSX;
2581 rs6000_vector_unit[V2DImode]
2582 = (TARGET_P8_VECTOR) ? VECTOR_P8_VECTOR : VECTOR_NONE;
2583 rs6000_vector_align[V2DImode] = align64;
2585 rs6000_vector_mem[V1TImode] = VECTOR_VSX;
2586 rs6000_vector_unit[V1TImode]
2587 = (TARGET_P8_VECTOR) ? VECTOR_P8_VECTOR : VECTOR_NONE;
2588 rs6000_vector_align[V1TImode] = 128;
2591 /* DFmode, see if we want to use the VSX unit. */
2592 if (TARGET_VSX && TARGET_VSX_SCALAR_DOUBLE)
2594 rs6000_vector_unit[DFmode] = VECTOR_VSX;
2595 rs6000_vector_mem[DFmode]
2596 = (TARGET_UPPER_REGS_DF ? VECTOR_VSX : VECTOR_NONE);
2597 rs6000_vector_align[DFmode] = align64;
2600 /* Allow TImode in VSX register and set the VSX memory macros. */
2601 if (TARGET_VSX && TARGET_VSX_TIMODE)
2603 rs6000_vector_mem[TImode] = VECTOR_VSX;
2604 rs6000_vector_align[TImode] = align64;
2607 /* TODO add SPE and paired floating point vector support. */
2609 /* Register class constraints for the constraints that depend on compile
2610 switches. When the VSX code was added, different constraints were added
2611 based on the type (DFmode, V2DFmode, V4SFmode). For the vector types, all
2612 of the VSX registers are used. The register classes for scalar floating
2613 point types is set, based on whether we allow that type into the upper
2614 (Altivec) registers. GCC has register classes to target the Altivec
2615 registers for load/store operations, to select using a VSX memory
2616 operation instead of the traditional floating point operation. The
2617 constraints are:
2619 d - Register class to use with traditional DFmode instructions.
2620 f - Register class to use with traditional SFmode instructions.
2621 v - Altivec register.
2622 wa - Any VSX register.
2623 wd - Preferred register class for V2DFmode.
2624 wf - Preferred register class for V4SFmode.
2625 wg - Float register for power6x move insns.
2626 wl - Float register if we can do 32-bit signed int loads.
2627 wm - VSX register for ISA 2.07 direct move operations.
2628 wr - GPR if 64-bit mode is permitted.
2629 ws - Register class to do ISA 2.06 DF operations.
2630 wu - Altivec register for ISA 2.07 VSX SF/SI load/stores.
2631 wv - Altivec register for ISA 2.06 VSX DF/DI load/stores.
2632 wt - VSX register for TImode in VSX registers.
2633 ww - Register class to do SF conversions in with VSX operations.
2634 wx - Float register if we can do 32-bit int stores.
2635 wy - Register class to do ISA 2.07 SF operations.
2636 wz - Float register if we can do 32-bit unsigned int loads. */
2638 if (TARGET_HARD_FLOAT && TARGET_FPRS)
2639 rs6000_constraints[RS6000_CONSTRAINT_f] = FLOAT_REGS;
2641 if (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
2642 rs6000_constraints[RS6000_CONSTRAINT_d] = FLOAT_REGS;
2644 if (TARGET_VSX)
2646 rs6000_constraints[RS6000_CONSTRAINT_wa] = VSX_REGS;
2647 rs6000_constraints[RS6000_CONSTRAINT_wd] = VSX_REGS;
2648 rs6000_constraints[RS6000_CONSTRAINT_wf] = VSX_REGS;
2650 if (TARGET_VSX_TIMODE)
2651 rs6000_constraints[RS6000_CONSTRAINT_wt] = VSX_REGS;
2653 if (TARGET_UPPER_REGS_DF)
2655 rs6000_constraints[RS6000_CONSTRAINT_ws] = VSX_REGS;
2656 rs6000_constraints[RS6000_CONSTRAINT_wv] = ALTIVEC_REGS;
2658 else
2659 rs6000_constraints[RS6000_CONSTRAINT_ws] = FLOAT_REGS;
2662 /* Add conditional constraints based on various options, to allow us to
2663 collapse multiple insn patterns. */
2664 if (TARGET_ALTIVEC)
2665 rs6000_constraints[RS6000_CONSTRAINT_v] = ALTIVEC_REGS;
2667 if (TARGET_MFPGPR)
2668 rs6000_constraints[RS6000_CONSTRAINT_wg] = FLOAT_REGS;
2670 if (TARGET_LFIWAX)
2671 rs6000_constraints[RS6000_CONSTRAINT_wl] = FLOAT_REGS;
2673 if (TARGET_DIRECT_MOVE)
2674 rs6000_constraints[RS6000_CONSTRAINT_wm] = VSX_REGS;
2676 if (TARGET_POWERPC64)
2677 rs6000_constraints[RS6000_CONSTRAINT_wr] = GENERAL_REGS;
2679 if (TARGET_P8_VECTOR && TARGET_UPPER_REGS_SF)
2681 rs6000_constraints[RS6000_CONSTRAINT_wu] = ALTIVEC_REGS;
2682 rs6000_constraints[RS6000_CONSTRAINT_wy] = VSX_REGS;
2683 rs6000_constraints[RS6000_CONSTRAINT_ww] = VSX_REGS;
2685 else if (TARGET_P8_VECTOR)
2687 rs6000_constraints[RS6000_CONSTRAINT_wy] = FLOAT_REGS;
2688 rs6000_constraints[RS6000_CONSTRAINT_ww] = FLOAT_REGS;
2690 else if (TARGET_VSX)
2691 rs6000_constraints[RS6000_CONSTRAINT_ww] = FLOAT_REGS;
2693 if (TARGET_STFIWX)
2694 rs6000_constraints[RS6000_CONSTRAINT_wx] = FLOAT_REGS;
2696 if (TARGET_LFIWZX)
2697 rs6000_constraints[RS6000_CONSTRAINT_wz] = FLOAT_REGS;
2699 /* Set up the reload helper and direct move functions. */
2700 if (TARGET_VSX || TARGET_ALTIVEC)
2702 if (TARGET_64BIT)
2704 reg_addr[V16QImode].reload_store = CODE_FOR_reload_v16qi_di_store;
2705 reg_addr[V16QImode].reload_load = CODE_FOR_reload_v16qi_di_load;
2706 reg_addr[V8HImode].reload_store = CODE_FOR_reload_v8hi_di_store;
2707 reg_addr[V8HImode].reload_load = CODE_FOR_reload_v8hi_di_load;
2708 reg_addr[V4SImode].reload_store = CODE_FOR_reload_v4si_di_store;
2709 reg_addr[V4SImode].reload_load = CODE_FOR_reload_v4si_di_load;
2710 reg_addr[V2DImode].reload_store = CODE_FOR_reload_v2di_di_store;
2711 reg_addr[V2DImode].reload_load = CODE_FOR_reload_v2di_di_load;
2712 reg_addr[V1TImode].reload_store = CODE_FOR_reload_v1ti_di_store;
2713 reg_addr[V1TImode].reload_load = CODE_FOR_reload_v1ti_di_load;
2714 reg_addr[V4SFmode].reload_store = CODE_FOR_reload_v4sf_di_store;
2715 reg_addr[V4SFmode].reload_load = CODE_FOR_reload_v4sf_di_load;
2716 reg_addr[V2DFmode].reload_store = CODE_FOR_reload_v2df_di_store;
2717 reg_addr[V2DFmode].reload_load = CODE_FOR_reload_v2df_di_load;
2718 if (TARGET_VSX && TARGET_UPPER_REGS_DF)
2720 reg_addr[DFmode].reload_store = CODE_FOR_reload_df_di_store;
2721 reg_addr[DFmode].reload_load = CODE_FOR_reload_df_di_load;
2722 reg_addr[DDmode].reload_store = CODE_FOR_reload_dd_di_store;
2723 reg_addr[DDmode].reload_load = CODE_FOR_reload_dd_di_load;
2725 if (TARGET_P8_VECTOR)
2727 reg_addr[SFmode].reload_store = CODE_FOR_reload_sf_di_store;
2728 reg_addr[SFmode].reload_load = CODE_FOR_reload_sf_di_load;
2729 reg_addr[SDmode].reload_store = CODE_FOR_reload_sd_di_store;
2730 reg_addr[SDmode].reload_load = CODE_FOR_reload_sd_di_load;
2732 if (TARGET_VSX_TIMODE)
2734 reg_addr[TImode].reload_store = CODE_FOR_reload_ti_di_store;
2735 reg_addr[TImode].reload_load = CODE_FOR_reload_ti_di_load;
2737 if (TARGET_DIRECT_MOVE)
2739 if (TARGET_POWERPC64)
2741 reg_addr[TImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxti;
2742 reg_addr[V1TImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv1ti;
2743 reg_addr[V2DFmode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv2df;
2744 reg_addr[V2DImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv2di;
2745 reg_addr[V4SFmode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv4sf;
2746 reg_addr[V4SImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv4si;
2747 reg_addr[V8HImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv8hi;
2748 reg_addr[V16QImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv16qi;
2749 reg_addr[SFmode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxsf;
2751 reg_addr[TImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprti;
2752 reg_addr[V1TImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv1ti;
2753 reg_addr[V2DFmode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv2df;
2754 reg_addr[V2DImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv2di;
2755 reg_addr[V4SFmode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv4sf;
2756 reg_addr[V4SImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv4si;
2757 reg_addr[V8HImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv8hi;
2758 reg_addr[V16QImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv16qi;
2759 reg_addr[SFmode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprsf;
2761 else
2763 reg_addr[DImode].reload_fpr_gpr = CODE_FOR_reload_fpr_from_gprdi;
2764 reg_addr[DDmode].reload_fpr_gpr = CODE_FOR_reload_fpr_from_gprdd;
2765 reg_addr[DFmode].reload_fpr_gpr = CODE_FOR_reload_fpr_from_gprdf;
2769 else
2771 reg_addr[V16QImode].reload_store = CODE_FOR_reload_v16qi_si_store;
2772 reg_addr[V16QImode].reload_load = CODE_FOR_reload_v16qi_si_load;
2773 reg_addr[V8HImode].reload_store = CODE_FOR_reload_v8hi_si_store;
2774 reg_addr[V8HImode].reload_load = CODE_FOR_reload_v8hi_si_load;
2775 reg_addr[V4SImode].reload_store = CODE_FOR_reload_v4si_si_store;
2776 reg_addr[V4SImode].reload_load = CODE_FOR_reload_v4si_si_load;
2777 reg_addr[V2DImode].reload_store = CODE_FOR_reload_v2di_si_store;
2778 reg_addr[V2DImode].reload_load = CODE_FOR_reload_v2di_si_load;
2779 reg_addr[V1TImode].reload_store = CODE_FOR_reload_v1ti_si_store;
2780 reg_addr[V1TImode].reload_load = CODE_FOR_reload_v1ti_si_load;
2781 reg_addr[V4SFmode].reload_store = CODE_FOR_reload_v4sf_si_store;
2782 reg_addr[V4SFmode].reload_load = CODE_FOR_reload_v4sf_si_load;
2783 reg_addr[V2DFmode].reload_store = CODE_FOR_reload_v2df_si_store;
2784 reg_addr[V2DFmode].reload_load = CODE_FOR_reload_v2df_si_load;
2785 if (TARGET_VSX && TARGET_UPPER_REGS_DF)
2787 reg_addr[DFmode].reload_store = CODE_FOR_reload_df_si_store;
2788 reg_addr[DFmode].reload_load = CODE_FOR_reload_df_si_load;
2789 reg_addr[DDmode].reload_store = CODE_FOR_reload_dd_si_store;
2790 reg_addr[DDmode].reload_load = CODE_FOR_reload_dd_si_load;
2792 if (TARGET_P8_VECTOR)
2794 reg_addr[SFmode].reload_store = CODE_FOR_reload_sf_si_store;
2795 reg_addr[SFmode].reload_load = CODE_FOR_reload_sf_si_load;
2796 reg_addr[SDmode].reload_store = CODE_FOR_reload_sd_si_store;
2797 reg_addr[SDmode].reload_load = CODE_FOR_reload_sd_si_load;
2799 if (TARGET_VSX_TIMODE)
2801 reg_addr[TImode].reload_store = CODE_FOR_reload_ti_si_store;
2802 reg_addr[TImode].reload_load = CODE_FOR_reload_ti_si_load;
2807 /* Precalculate HARD_REGNO_NREGS. */
2808 for (r = 0; r < FIRST_PSEUDO_REGISTER; ++r)
2809 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2810 rs6000_hard_regno_nregs[m][r]
2811 = rs6000_hard_regno_nregs_internal (r, (enum machine_mode)m);
2813 /* Precalculate HARD_REGNO_MODE_OK. */
2814 for (r = 0; r < FIRST_PSEUDO_REGISTER; ++r)
2815 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2816 if (rs6000_hard_regno_mode_ok (r, (enum machine_mode)m))
2817 rs6000_hard_regno_mode_ok_p[m][r] = true;
2819 /* Precalculate CLASS_MAX_NREGS sizes. */
2820 for (c = 0; c < LIM_REG_CLASSES; ++c)
2822 int reg_size;
2824 if (TARGET_VSX && VSX_REG_CLASS_P (c))
2825 reg_size = UNITS_PER_VSX_WORD;
2827 else if (c == ALTIVEC_REGS)
2828 reg_size = UNITS_PER_ALTIVEC_WORD;
2830 else if (c == FLOAT_REGS)
2831 reg_size = UNITS_PER_FP_WORD;
2833 else
2834 reg_size = UNITS_PER_WORD;
2836 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2838 enum machine_mode m2 = (enum machine_mode)m;
2839 int reg_size2 = reg_size;
2841 /* TFmode/TDmode always takes 2 registers, even in VSX. */
2842 if (TARGET_VSX && VSX_REG_CLASS_P (c)
2843 && (m == TDmode || m == TFmode))
2844 reg_size2 = UNITS_PER_FP_WORD;
2846 rs6000_class_max_nregs[m][c]
2847 = (GET_MODE_SIZE (m2) + reg_size2 - 1) / reg_size2;
2851 if (TARGET_E500_DOUBLE)
2852 rs6000_class_max_nregs[DFmode][GENERAL_REGS] = 1;
2854 /* Calculate which modes to automatically generate code to use a the
2855 reciprocal divide and square root instructions. In the future, possibly
2856 automatically generate the instructions even if the user did not specify
2857 -mrecip. The older machines double precision reciprocal sqrt estimate is
2858 not accurate enough. */
2859 memset (rs6000_recip_bits, 0, sizeof (rs6000_recip_bits));
2860 if (TARGET_FRES)
2861 rs6000_recip_bits[SFmode] = RS6000_RECIP_MASK_HAVE_RE;
2862 if (TARGET_FRE)
2863 rs6000_recip_bits[DFmode] = RS6000_RECIP_MASK_HAVE_RE;
2864 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode))
2865 rs6000_recip_bits[V4SFmode] = RS6000_RECIP_MASK_HAVE_RE;
2866 if (VECTOR_UNIT_VSX_P (V2DFmode))
2867 rs6000_recip_bits[V2DFmode] = RS6000_RECIP_MASK_HAVE_RE;
2869 if (TARGET_FRSQRTES)
2870 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2871 if (TARGET_FRSQRTE)
2872 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2873 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode))
2874 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2875 if (VECTOR_UNIT_VSX_P (V2DFmode))
2876 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2878 if (rs6000_recip_control)
2880 if (!flag_finite_math_only)
2881 warning (0, "-mrecip requires -ffinite-math or -ffast-math");
2882 if (flag_trapping_math)
2883 warning (0, "-mrecip requires -fno-trapping-math or -ffast-math");
2884 if (!flag_reciprocal_math)
2885 warning (0, "-mrecip requires -freciprocal-math or -ffast-math");
2886 if (flag_finite_math_only && !flag_trapping_math && flag_reciprocal_math)
2888 if (RS6000_RECIP_HAVE_RE_P (SFmode)
2889 && (rs6000_recip_control & RECIP_SF_DIV) != 0)
2890 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2892 if (RS6000_RECIP_HAVE_RE_P (DFmode)
2893 && (rs6000_recip_control & RECIP_DF_DIV) != 0)
2894 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2896 if (RS6000_RECIP_HAVE_RE_P (V4SFmode)
2897 && (rs6000_recip_control & RECIP_V4SF_DIV) != 0)
2898 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2900 if (RS6000_RECIP_HAVE_RE_P (V2DFmode)
2901 && (rs6000_recip_control & RECIP_V2DF_DIV) != 0)
2902 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2904 if (RS6000_RECIP_HAVE_RSQRTE_P (SFmode)
2905 && (rs6000_recip_control & RECIP_SF_RSQRT) != 0)
2906 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2908 if (RS6000_RECIP_HAVE_RSQRTE_P (DFmode)
2909 && (rs6000_recip_control & RECIP_DF_RSQRT) != 0)
2910 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2912 if (RS6000_RECIP_HAVE_RSQRTE_P (V4SFmode)
2913 && (rs6000_recip_control & RECIP_V4SF_RSQRT) != 0)
2914 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2916 if (RS6000_RECIP_HAVE_RSQRTE_P (V2DFmode)
2917 && (rs6000_recip_control & RECIP_V2DF_RSQRT) != 0)
2918 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2922 /* Update the addr mask bits in reg_addr to help secondary reload and go if
2923 legitimate address support to figure out the appropriate addressing to
2924 use. */
2925 rs6000_setup_reg_addr_masks ();
2927 if (global_init_p || TARGET_DEBUG_TARGET)
2929 if (TARGET_DEBUG_REG)
2930 rs6000_debug_reg_global ();
2932 if (TARGET_DEBUG_COST || TARGET_DEBUG_REG)
2933 fprintf (stderr,
2934 "SImode variable mult cost = %d\n"
2935 "SImode constant mult cost = %d\n"
2936 "SImode short constant mult cost = %d\n"
2937 "DImode multipliciation cost = %d\n"
2938 "SImode division cost = %d\n"
2939 "DImode division cost = %d\n"
2940 "Simple fp operation cost = %d\n"
2941 "DFmode multiplication cost = %d\n"
2942 "SFmode division cost = %d\n"
2943 "DFmode division cost = %d\n"
2944 "cache line size = %d\n"
2945 "l1 cache size = %d\n"
2946 "l2 cache size = %d\n"
2947 "simultaneous prefetches = %d\n"
2948 "\n",
2949 rs6000_cost->mulsi,
2950 rs6000_cost->mulsi_const,
2951 rs6000_cost->mulsi_const9,
2952 rs6000_cost->muldi,
2953 rs6000_cost->divsi,
2954 rs6000_cost->divdi,
2955 rs6000_cost->fp,
2956 rs6000_cost->dmul,
2957 rs6000_cost->sdiv,
2958 rs6000_cost->ddiv,
2959 rs6000_cost->cache_line_size,
2960 rs6000_cost->l1_cache_size,
2961 rs6000_cost->l2_cache_size,
2962 rs6000_cost->simultaneous_prefetches);
2966 #if TARGET_MACHO
2967 /* The Darwin version of SUBTARGET_OVERRIDE_OPTIONS. */
2969 static void
2970 darwin_rs6000_override_options (void)
2972 /* The Darwin ABI always includes AltiVec, can't be (validly) turned
2973 off. */
2974 rs6000_altivec_abi = 1;
2975 TARGET_ALTIVEC_VRSAVE = 1;
2976 rs6000_current_abi = ABI_DARWIN;
2978 if (DEFAULT_ABI == ABI_DARWIN
2979 && TARGET_64BIT)
2980 darwin_one_byte_bool = 1;
2982 if (TARGET_64BIT && ! TARGET_POWERPC64)
2984 rs6000_isa_flags |= OPTION_MASK_POWERPC64;
2985 warning (0, "-m64 requires PowerPC64 architecture, enabling");
2987 if (flag_mkernel)
2989 rs6000_default_long_calls = 1;
2990 rs6000_isa_flags |= OPTION_MASK_SOFT_FLOAT;
2993 /* Make -m64 imply -maltivec. Darwin's 64-bit ABI includes
2994 Altivec. */
2995 if (!flag_mkernel && !flag_apple_kext
2996 && TARGET_64BIT
2997 && ! (rs6000_isa_flags_explicit & OPTION_MASK_ALTIVEC))
2998 rs6000_isa_flags |= OPTION_MASK_ALTIVEC;
3000 /* Unless the user (not the configurer) has explicitly overridden
3001 it with -mcpu=G3 or -mno-altivec, then 10.5+ targets default to
3002 G4 unless targeting the kernel. */
3003 if (!flag_mkernel
3004 && !flag_apple_kext
3005 && strverscmp (darwin_macosx_version_min, "10.5") >= 0
3006 && ! (rs6000_isa_flags_explicit & OPTION_MASK_ALTIVEC)
3007 && ! global_options_set.x_rs6000_cpu_index)
3009 rs6000_isa_flags |= OPTION_MASK_ALTIVEC;
3012 #endif
3014 /* If not otherwise specified by a target, make 'long double' equivalent to
3015 'double'. */
3017 #ifndef RS6000_DEFAULT_LONG_DOUBLE_SIZE
3018 #define RS6000_DEFAULT_LONG_DOUBLE_SIZE 64
3019 #endif
3021 /* Return the builtin mask of the various options used that could affect which
3022 builtins were used. In the past we used target_flags, but we've run out of
3023 bits, and some options like SPE and PAIRED are no longer in
3024 target_flags. */
3026 HOST_WIDE_INT
3027 rs6000_builtin_mask_calculate (void)
3029 return (((TARGET_ALTIVEC) ? RS6000_BTM_ALTIVEC : 0)
3030 | ((TARGET_VSX) ? RS6000_BTM_VSX : 0)
3031 | ((TARGET_SPE) ? RS6000_BTM_SPE : 0)
3032 | ((TARGET_PAIRED_FLOAT) ? RS6000_BTM_PAIRED : 0)
3033 | ((TARGET_FRE) ? RS6000_BTM_FRE : 0)
3034 | ((TARGET_FRES) ? RS6000_BTM_FRES : 0)
3035 | ((TARGET_FRSQRTE) ? RS6000_BTM_FRSQRTE : 0)
3036 | ((TARGET_FRSQRTES) ? RS6000_BTM_FRSQRTES : 0)
3037 | ((TARGET_POPCNTD) ? RS6000_BTM_POPCNTD : 0)
3038 | ((rs6000_cpu == PROCESSOR_CELL) ? RS6000_BTM_CELL : 0)
3039 | ((TARGET_P8_VECTOR) ? RS6000_BTM_P8_VECTOR : 0)
3040 | ((TARGET_CRYPTO) ? RS6000_BTM_CRYPTO : 0)
3041 | ((TARGET_HTM) ? RS6000_BTM_HTM : 0)
3042 | ((TARGET_DFP) ? RS6000_BTM_DFP : 0)
3043 | ((TARGET_HARD_FLOAT) ? RS6000_BTM_HARD_FLOAT : 0));
3046 /* Override command line options. Mostly we process the processor type and
3047 sometimes adjust other TARGET_ options. */
3049 static bool
3050 rs6000_option_override_internal (bool global_init_p)
3052 bool ret = true;
3053 bool have_cpu = false;
3055 /* The default cpu requested at configure time, if any. */
3056 const char *implicit_cpu = OPTION_TARGET_CPU_DEFAULT;
3058 HOST_WIDE_INT set_masks;
3059 int cpu_index;
3060 int tune_index;
3061 struct cl_target_option *main_target_opt
3062 = ((global_init_p || target_option_default_node == NULL)
3063 ? NULL : TREE_TARGET_OPTION (target_option_default_node));
3065 /* Remember the explicit arguments. */
3066 if (global_init_p)
3067 rs6000_isa_flags_explicit = global_options_set.x_rs6000_isa_flags;
3069 /* On 64-bit Darwin, power alignment is ABI-incompatible with some C
3070 library functions, so warn about it. The flag may be useful for
3071 performance studies from time to time though, so don't disable it
3072 entirely. */
3073 if (global_options_set.x_rs6000_alignment_flags
3074 && rs6000_alignment_flags == MASK_ALIGN_POWER
3075 && DEFAULT_ABI == ABI_DARWIN
3076 && TARGET_64BIT)
3077 warning (0, "-malign-power is not supported for 64-bit Darwin;"
3078 " it is incompatible with the installed C and C++ libraries");
3080 /* Numerous experiment shows that IRA based loop pressure
3081 calculation works better for RTL loop invariant motion on targets
3082 with enough (>= 32) registers. It is an expensive optimization.
3083 So it is on only for peak performance. */
3084 if (optimize >= 3 && global_init_p
3085 && !global_options_set.x_flag_ira_loop_pressure)
3086 flag_ira_loop_pressure = 1;
3088 /* Set the pointer size. */
3089 if (TARGET_64BIT)
3091 rs6000_pmode = (int)DImode;
3092 rs6000_pointer_size = 64;
3094 else
3096 rs6000_pmode = (int)SImode;
3097 rs6000_pointer_size = 32;
3100 /* Some OSs don't support saving the high part of 64-bit registers on context
3101 switch. Other OSs don't support saving Altivec registers. On those OSs,
3102 we don't touch the OPTION_MASK_POWERPC64 or OPTION_MASK_ALTIVEC settings;
3103 if the user wants either, the user must explicitly specify them and we
3104 won't interfere with the user's specification. */
3106 set_masks = POWERPC_MASKS;
3107 #ifdef OS_MISSING_POWERPC64
3108 if (OS_MISSING_POWERPC64)
3109 set_masks &= ~OPTION_MASK_POWERPC64;
3110 #endif
3111 #ifdef OS_MISSING_ALTIVEC
3112 if (OS_MISSING_ALTIVEC)
3113 set_masks &= ~(OPTION_MASK_ALTIVEC | OPTION_MASK_VSX);
3114 #endif
3116 /* Don't override by the processor default if given explicitly. */
3117 set_masks &= ~rs6000_isa_flags_explicit;
3119 /* Process the -mcpu=<xxx> and -mtune=<xxx> argument. If the user changed
3120 the cpu in a target attribute or pragma, but did not specify a tuning
3121 option, use the cpu for the tuning option rather than the option specified
3122 with -mtune on the command line. Process a '--with-cpu' configuration
3123 request as an implicit --cpu. */
3124 if (rs6000_cpu_index >= 0)
3126 cpu_index = rs6000_cpu_index;
3127 have_cpu = true;
3129 else if (main_target_opt != NULL && main_target_opt->x_rs6000_cpu_index >= 0)
3131 rs6000_cpu_index = cpu_index = main_target_opt->x_rs6000_cpu_index;
3132 have_cpu = true;
3134 else if (implicit_cpu)
3136 rs6000_cpu_index = cpu_index = rs6000_cpu_name_lookup (implicit_cpu);
3137 have_cpu = true;
3139 else
3141 const char *default_cpu = (TARGET_POWERPC64 ? "powerpc64" : "powerpc");
3142 rs6000_cpu_index = cpu_index = rs6000_cpu_name_lookup (default_cpu);
3143 have_cpu = false;
3146 gcc_assert (cpu_index >= 0);
3148 /* If we have a cpu, either through an explicit -mcpu=<xxx> or if the
3149 compiler was configured with --with-cpu=<xxx>, replace all of the ISA bits
3150 with those from the cpu, except for options that were explicitly set. If
3151 we don't have a cpu, do not override the target bits set in
3152 TARGET_DEFAULT. */
3153 if (have_cpu)
3155 rs6000_isa_flags &= ~set_masks;
3156 rs6000_isa_flags |= (processor_target_table[cpu_index].target_enable
3157 & set_masks);
3159 else
3160 rs6000_isa_flags |= (processor_target_table[cpu_index].target_enable
3161 & ~rs6000_isa_flags_explicit);
3163 /* If no -mcpu=<xxx>, inherit any default options that were cleared via
3164 POWERPC_MASKS. Originally, TARGET_DEFAULT was used to initialize
3165 target_flags via the TARGET_DEFAULT_TARGET_FLAGS hook. When we switched
3166 to using rs6000_isa_flags, we need to do the initialization here. */
3167 if (!have_cpu)
3168 rs6000_isa_flags |= (TARGET_DEFAULT & ~rs6000_isa_flags_explicit);
3170 if (rs6000_tune_index >= 0)
3171 tune_index = rs6000_tune_index;
3172 else if (have_cpu)
3173 rs6000_tune_index = tune_index = cpu_index;
3174 else
3176 size_t i;
3177 enum processor_type tune_proc
3178 = (TARGET_POWERPC64 ? PROCESSOR_DEFAULT64 : PROCESSOR_DEFAULT);
3180 tune_index = -1;
3181 for (i = 0; i < ARRAY_SIZE (processor_target_table); i++)
3182 if (processor_target_table[i].processor == tune_proc)
3184 rs6000_tune_index = tune_index = i;
3185 break;
3189 gcc_assert (tune_index >= 0);
3190 rs6000_cpu = processor_target_table[tune_index].processor;
3192 /* Pick defaults for SPE related control flags. Do this early to make sure
3193 that the TARGET_ macros are representative ASAP. */
3195 int spe_capable_cpu =
3196 (rs6000_cpu == PROCESSOR_PPC8540
3197 || rs6000_cpu == PROCESSOR_PPC8548);
3199 if (!global_options_set.x_rs6000_spe_abi)
3200 rs6000_spe_abi = spe_capable_cpu;
3202 if (!global_options_set.x_rs6000_spe)
3203 rs6000_spe = spe_capable_cpu;
3205 if (!global_options_set.x_rs6000_float_gprs)
3206 rs6000_float_gprs =
3207 (rs6000_cpu == PROCESSOR_PPC8540 ? 1
3208 : rs6000_cpu == PROCESSOR_PPC8548 ? 2
3209 : 0);
3212 if (global_options_set.x_rs6000_spe_abi
3213 && rs6000_spe_abi
3214 && !TARGET_SPE_ABI)
3215 error ("not configured for SPE ABI");
3217 if (global_options_set.x_rs6000_spe
3218 && rs6000_spe
3219 && !TARGET_SPE)
3220 error ("not configured for SPE instruction set");
3222 if (main_target_opt != NULL
3223 && ((main_target_opt->x_rs6000_spe_abi != rs6000_spe_abi)
3224 || (main_target_opt->x_rs6000_spe != rs6000_spe)
3225 || (main_target_opt->x_rs6000_float_gprs != rs6000_float_gprs)))
3226 error ("target attribute or pragma changes SPE ABI");
3228 if (rs6000_cpu == PROCESSOR_PPCE300C2 || rs6000_cpu == PROCESSOR_PPCE300C3
3229 || rs6000_cpu == PROCESSOR_PPCE500MC || rs6000_cpu == PROCESSOR_PPCE500MC64
3230 || rs6000_cpu == PROCESSOR_PPCE5500)
3232 if (TARGET_ALTIVEC)
3233 error ("AltiVec not supported in this target");
3234 if (TARGET_SPE)
3235 error ("SPE not supported in this target");
3237 if (rs6000_cpu == PROCESSOR_PPCE6500)
3239 if (TARGET_SPE)
3240 error ("SPE not supported in this target");
3243 /* Disable Cell microcode if we are optimizing for the Cell
3244 and not optimizing for size. */
3245 if (rs6000_gen_cell_microcode == -1)
3246 rs6000_gen_cell_microcode = !(rs6000_cpu == PROCESSOR_CELL
3247 && !optimize_size);
3249 /* If we are optimizing big endian systems for space and it's OK to
3250 use instructions that would be microcoded on the Cell, use the
3251 load/store multiple and string instructions. */
3252 if (BYTES_BIG_ENDIAN && optimize_size && rs6000_gen_cell_microcode)
3253 rs6000_isa_flags |= ~rs6000_isa_flags_explicit & (OPTION_MASK_MULTIPLE
3254 | OPTION_MASK_STRING);
3256 /* Don't allow -mmultiple or -mstring on little endian systems
3257 unless the cpu is a 750, because the hardware doesn't support the
3258 instructions used in little endian mode, and causes an alignment
3259 trap. The 750 does not cause an alignment trap (except when the
3260 target is unaligned). */
3262 if (!BYTES_BIG_ENDIAN && rs6000_cpu != PROCESSOR_PPC750)
3264 if (TARGET_MULTIPLE)
3266 rs6000_isa_flags &= ~OPTION_MASK_MULTIPLE;
3267 if ((rs6000_isa_flags_explicit & OPTION_MASK_MULTIPLE) != 0)
3268 warning (0, "-mmultiple is not supported on little endian systems");
3271 if (TARGET_STRING)
3273 rs6000_isa_flags &= ~OPTION_MASK_STRING;
3274 if ((rs6000_isa_flags_explicit & OPTION_MASK_STRING) != 0)
3275 warning (0, "-mstring is not supported on little endian systems");
3279 /* If little-endian, default to -mstrict-align on older processors.
3280 Testing for htm matches power8 and later. */
3281 if (!BYTES_BIG_ENDIAN
3282 && !(processor_target_table[tune_index].target_enable & OPTION_MASK_HTM))
3283 rs6000_isa_flags |= ~rs6000_isa_flags_explicit & OPTION_MASK_STRICT_ALIGN;
3285 /* -maltivec={le,be} implies -maltivec. */
3286 if (rs6000_altivec_element_order != 0)
3287 rs6000_isa_flags |= OPTION_MASK_ALTIVEC;
3289 /* Disallow -maltivec=le in big endian mode for now. This is not
3290 known to be useful for anyone. */
3291 if (BYTES_BIG_ENDIAN && rs6000_altivec_element_order == 1)
3293 warning (0, N_("-maltivec=le not allowed for big-endian targets"));
3294 rs6000_altivec_element_order = 0;
3297 /* Add some warnings for VSX. */
3298 if (TARGET_VSX)
3300 const char *msg = NULL;
3301 if (!TARGET_HARD_FLOAT || !TARGET_FPRS
3302 || !TARGET_SINGLE_FLOAT || !TARGET_DOUBLE_FLOAT)
3304 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX)
3305 msg = N_("-mvsx requires hardware floating point");
3306 else
3308 rs6000_isa_flags &= ~ OPTION_MASK_VSX;
3309 rs6000_isa_flags_explicit |= OPTION_MASK_VSX;
3312 else if (TARGET_PAIRED_FLOAT)
3313 msg = N_("-mvsx and -mpaired are incompatible");
3314 else if (TARGET_AVOID_XFORM > 0)
3315 msg = N_("-mvsx needs indexed addressing");
3316 else if (!TARGET_ALTIVEC && (rs6000_isa_flags_explicit
3317 & OPTION_MASK_ALTIVEC))
3319 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX)
3320 msg = N_("-mvsx and -mno-altivec are incompatible");
3321 else
3322 msg = N_("-mno-altivec disables vsx");
3325 if (msg)
3327 warning (0, msg);
3328 rs6000_isa_flags &= ~ OPTION_MASK_VSX;
3329 rs6000_isa_flags_explicit |= OPTION_MASK_VSX;
3333 /* If hard-float/altivec/vsx were explicitly turned off then don't allow
3334 the -mcpu setting to enable options that conflict. */
3335 if ((!TARGET_HARD_FLOAT || !TARGET_ALTIVEC || !TARGET_VSX)
3336 && (rs6000_isa_flags_explicit & (OPTION_MASK_SOFT_FLOAT
3337 | OPTION_MASK_ALTIVEC
3338 | OPTION_MASK_VSX)) != 0)
3339 rs6000_isa_flags &= ~((OPTION_MASK_P8_VECTOR | OPTION_MASK_CRYPTO
3340 | OPTION_MASK_DIRECT_MOVE)
3341 & ~rs6000_isa_flags_explicit);
3343 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
3344 rs6000_print_isa_options (stderr, 0, "before defaults", rs6000_isa_flags);
3346 /* For the newer switches (vsx, dfp, etc.) set some of the older options,
3347 unless the user explicitly used the -mno-<option> to disable the code. */
3348 if (TARGET_P8_VECTOR || TARGET_DIRECT_MOVE || TARGET_CRYPTO)
3349 rs6000_isa_flags |= (ISA_2_7_MASKS_SERVER & ~rs6000_isa_flags_explicit);
3350 else if (TARGET_VSX)
3351 rs6000_isa_flags |= (ISA_2_6_MASKS_SERVER & ~rs6000_isa_flags_explicit);
3352 else if (TARGET_POPCNTD)
3353 rs6000_isa_flags |= (ISA_2_6_MASKS_EMBEDDED & ~rs6000_isa_flags_explicit);
3354 else if (TARGET_DFP)
3355 rs6000_isa_flags |= (ISA_2_5_MASKS_SERVER & ~rs6000_isa_flags_explicit);
3356 else if (TARGET_CMPB)
3357 rs6000_isa_flags |= (ISA_2_5_MASKS_EMBEDDED & ~rs6000_isa_flags_explicit);
3358 else if (TARGET_FPRND)
3359 rs6000_isa_flags |= (ISA_2_4_MASKS & ~rs6000_isa_flags_explicit);
3360 else if (TARGET_POPCNTB)
3361 rs6000_isa_flags |= (ISA_2_2_MASKS & ~rs6000_isa_flags_explicit);
3362 else if (TARGET_ALTIVEC)
3363 rs6000_isa_flags |= (OPTION_MASK_PPC_GFXOPT & ~rs6000_isa_flags_explicit);
3365 if (TARGET_CRYPTO && !TARGET_ALTIVEC)
3367 if (rs6000_isa_flags_explicit & OPTION_MASK_CRYPTO)
3368 error ("-mcrypto requires -maltivec");
3369 rs6000_isa_flags &= ~OPTION_MASK_CRYPTO;
3372 if (TARGET_DIRECT_MOVE && !TARGET_VSX)
3374 if (rs6000_isa_flags_explicit & OPTION_MASK_DIRECT_MOVE)
3375 error ("-mdirect-move requires -mvsx");
3376 rs6000_isa_flags &= ~OPTION_MASK_DIRECT_MOVE;
3379 if (TARGET_P8_VECTOR && !TARGET_ALTIVEC)
3381 if (rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR)
3382 error ("-mpower8-vector requires -maltivec");
3383 rs6000_isa_flags &= ~OPTION_MASK_P8_VECTOR;
3386 if (TARGET_P8_VECTOR && !TARGET_VSX)
3388 if (rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR)
3389 error ("-mpower8-vector requires -mvsx");
3390 rs6000_isa_flags &= ~OPTION_MASK_P8_VECTOR;
3393 if (TARGET_VSX_TIMODE && !TARGET_VSX)
3395 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX_TIMODE)
3396 error ("-mvsx-timode requires -mvsx");
3397 rs6000_isa_flags &= ~OPTION_MASK_VSX_TIMODE;
3400 if (TARGET_DFP && !TARGET_HARD_FLOAT)
3402 if (rs6000_isa_flags_explicit & OPTION_MASK_DFP)
3403 error ("-mhard-dfp requires -mhard-float");
3404 rs6000_isa_flags &= ~OPTION_MASK_DFP;
3407 /* The quad memory instructions only works in 64-bit mode. In 32-bit mode,
3408 silently turn off quad memory mode. */
3409 if ((TARGET_QUAD_MEMORY || TARGET_QUAD_MEMORY_ATOMIC) && !TARGET_POWERPC64)
3411 if ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY) != 0)
3412 warning (0, N_("-mquad-memory requires 64-bit mode"));
3414 if ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY_ATOMIC) != 0)
3415 warning (0, N_("-mquad-memory-atomic requires 64-bit mode"));
3417 rs6000_isa_flags &= ~(OPTION_MASK_QUAD_MEMORY
3418 | OPTION_MASK_QUAD_MEMORY_ATOMIC);
3421 /* Non-atomic quad memory load/store are disabled for little endian, since
3422 the words are reversed, but atomic operations can still be done by
3423 swapping the words. */
3424 if (TARGET_QUAD_MEMORY && !WORDS_BIG_ENDIAN)
3426 if ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY) != 0)
3427 warning (0, N_("-mquad-memory is not available in little endian mode"));
3429 rs6000_isa_flags &= ~OPTION_MASK_QUAD_MEMORY;
3432 /* Assume if the user asked for normal quad memory instructions, they want
3433 the atomic versions as well, unless they explicity told us not to use quad
3434 word atomic instructions. */
3435 if (TARGET_QUAD_MEMORY
3436 && !TARGET_QUAD_MEMORY_ATOMIC
3437 && ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY_ATOMIC) == 0))
3438 rs6000_isa_flags |= OPTION_MASK_QUAD_MEMORY_ATOMIC;
3440 /* Enable power8 fusion if we are tuning for power8, even if we aren't
3441 generating power8 instructions. */
3442 if (!(rs6000_isa_flags_explicit & OPTION_MASK_P8_FUSION))
3443 rs6000_isa_flags |= (processor_target_table[tune_index].target_enable
3444 & OPTION_MASK_P8_FUSION);
3446 /* Power8 does not fuse sign extended loads with the addis. If we are
3447 optimizing at high levels for speed, convert a sign extended load into a
3448 zero extending load, and an explicit sign extension. */
3449 if (TARGET_P8_FUSION
3450 && !(rs6000_isa_flags_explicit & OPTION_MASK_P8_FUSION_SIGN)
3451 && optimize_function_for_speed_p (cfun)
3452 && optimize >= 3)
3453 rs6000_isa_flags |= OPTION_MASK_P8_FUSION_SIGN;
3455 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
3456 rs6000_print_isa_options (stderr, 0, "after defaults", rs6000_isa_flags);
3458 /* E500mc does "better" if we inline more aggressively. Respect the
3459 user's opinion, though. */
3460 if (rs6000_block_move_inline_limit == 0
3461 && (rs6000_cpu == PROCESSOR_PPCE500MC
3462 || rs6000_cpu == PROCESSOR_PPCE500MC64
3463 || rs6000_cpu == PROCESSOR_PPCE5500
3464 || rs6000_cpu == PROCESSOR_PPCE6500))
3465 rs6000_block_move_inline_limit = 128;
3467 /* store_one_arg depends on expand_block_move to handle at least the
3468 size of reg_parm_stack_space. */
3469 if (rs6000_block_move_inline_limit < (TARGET_POWERPC64 ? 64 : 32))
3470 rs6000_block_move_inline_limit = (TARGET_POWERPC64 ? 64 : 32);
3472 if (global_init_p)
3474 /* If the appropriate debug option is enabled, replace the target hooks
3475 with debug versions that call the real version and then prints
3476 debugging information. */
3477 if (TARGET_DEBUG_COST)
3479 targetm.rtx_costs = rs6000_debug_rtx_costs;
3480 targetm.address_cost = rs6000_debug_address_cost;
3481 targetm.sched.adjust_cost = rs6000_debug_adjust_cost;
3484 if (TARGET_DEBUG_ADDR)
3486 targetm.legitimate_address_p = rs6000_debug_legitimate_address_p;
3487 targetm.legitimize_address = rs6000_debug_legitimize_address;
3488 rs6000_secondary_reload_class_ptr
3489 = rs6000_debug_secondary_reload_class;
3490 rs6000_secondary_memory_needed_ptr
3491 = rs6000_debug_secondary_memory_needed;
3492 rs6000_cannot_change_mode_class_ptr
3493 = rs6000_debug_cannot_change_mode_class;
3494 rs6000_preferred_reload_class_ptr
3495 = rs6000_debug_preferred_reload_class;
3496 rs6000_legitimize_reload_address_ptr
3497 = rs6000_debug_legitimize_reload_address;
3498 rs6000_mode_dependent_address_ptr
3499 = rs6000_debug_mode_dependent_address;
3502 if (rs6000_veclibabi_name)
3504 if (strcmp (rs6000_veclibabi_name, "mass") == 0)
3505 rs6000_veclib_handler = rs6000_builtin_vectorized_libmass;
3506 else
3508 error ("unknown vectorization library ABI type (%s) for "
3509 "-mveclibabi= switch", rs6000_veclibabi_name);
3510 ret = false;
3515 if (!global_options_set.x_rs6000_long_double_type_size)
3517 if (main_target_opt != NULL
3518 && (main_target_opt->x_rs6000_long_double_type_size
3519 != RS6000_DEFAULT_LONG_DOUBLE_SIZE))
3520 error ("target attribute or pragma changes long double size");
3521 else
3522 rs6000_long_double_type_size = RS6000_DEFAULT_LONG_DOUBLE_SIZE;
3525 #if !defined (POWERPC_LINUX) && !defined (POWERPC_FREEBSD)
3526 if (!global_options_set.x_rs6000_ieeequad)
3527 rs6000_ieeequad = 1;
3528 #endif
3530 /* Disable VSX and Altivec silently if the user switched cpus to power7 in a
3531 target attribute or pragma which automatically enables both options,
3532 unless the altivec ABI was set. This is set by default for 64-bit, but
3533 not for 32-bit. */
3534 if (main_target_opt != NULL && !main_target_opt->x_rs6000_altivec_abi)
3535 rs6000_isa_flags &= ~((OPTION_MASK_VSX | OPTION_MASK_ALTIVEC)
3536 & ~rs6000_isa_flags_explicit);
3538 /* Enable Altivec ABI for AIX -maltivec. */
3539 if (TARGET_XCOFF && (TARGET_ALTIVEC || TARGET_VSX))
3541 if (main_target_opt != NULL && !main_target_opt->x_rs6000_altivec_abi)
3542 error ("target attribute or pragma changes AltiVec ABI");
3543 else
3544 rs6000_altivec_abi = 1;
3547 /* The AltiVec ABI is the default for PowerPC-64 GNU/Linux. For
3548 PowerPC-32 GNU/Linux, -maltivec implies the AltiVec ABI. It can
3549 be explicitly overridden in either case. */
3550 if (TARGET_ELF)
3552 if (!global_options_set.x_rs6000_altivec_abi
3553 && (TARGET_64BIT || TARGET_ALTIVEC || TARGET_VSX))
3555 if (main_target_opt != NULL &&
3556 !main_target_opt->x_rs6000_altivec_abi)
3557 error ("target attribute or pragma changes AltiVec ABI");
3558 else
3559 rs6000_altivec_abi = 1;
3563 /* Set the Darwin64 ABI as default for 64-bit Darwin.
3564 So far, the only darwin64 targets are also MACH-O. */
3565 if (TARGET_MACHO
3566 && DEFAULT_ABI == ABI_DARWIN
3567 && TARGET_64BIT)
3569 if (main_target_opt != NULL && !main_target_opt->x_rs6000_darwin64_abi)
3570 error ("target attribute or pragma changes darwin64 ABI");
3571 else
3573 rs6000_darwin64_abi = 1;
3574 /* Default to natural alignment, for better performance. */
3575 rs6000_alignment_flags = MASK_ALIGN_NATURAL;
3579 /* Place FP constants in the constant pool instead of TOC
3580 if section anchors enabled. */
3581 if (flag_section_anchors
3582 && !global_options_set.x_TARGET_NO_FP_IN_TOC)
3583 TARGET_NO_FP_IN_TOC = 1;
3585 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
3586 rs6000_print_isa_options (stderr, 0, "before subtarget", rs6000_isa_flags);
3588 #ifdef SUBTARGET_OVERRIDE_OPTIONS
3589 SUBTARGET_OVERRIDE_OPTIONS;
3590 #endif
3591 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
3592 SUBSUBTARGET_OVERRIDE_OPTIONS;
3593 #endif
3594 #ifdef SUB3TARGET_OVERRIDE_OPTIONS
3595 SUB3TARGET_OVERRIDE_OPTIONS;
3596 #endif
3598 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
3599 rs6000_print_isa_options (stderr, 0, "after subtarget", rs6000_isa_flags);
3601 /* For the E500 family of cores, reset the single/double FP flags to let us
3602 check that they remain constant across attributes or pragmas. Also,
3603 clear a possible request for string instructions, not supported and which
3604 we might have silently queried above for -Os.
3606 For other families, clear ISEL in case it was set implicitly.
3609 switch (rs6000_cpu)
3611 case PROCESSOR_PPC8540:
3612 case PROCESSOR_PPC8548:
3613 case PROCESSOR_PPCE500MC:
3614 case PROCESSOR_PPCE500MC64:
3615 case PROCESSOR_PPCE5500:
3616 case PROCESSOR_PPCE6500:
3618 rs6000_single_float = TARGET_E500_SINGLE || TARGET_E500_DOUBLE;
3619 rs6000_double_float = TARGET_E500_DOUBLE;
3621 rs6000_isa_flags &= ~OPTION_MASK_STRING;
3623 break;
3625 default:
3627 if (have_cpu && !(rs6000_isa_flags_explicit & OPTION_MASK_ISEL))
3628 rs6000_isa_flags &= ~OPTION_MASK_ISEL;
3630 break;
3633 if (main_target_opt)
3635 if (main_target_opt->x_rs6000_single_float != rs6000_single_float)
3636 error ("target attribute or pragma changes single precision floating "
3637 "point");
3638 if (main_target_opt->x_rs6000_double_float != rs6000_double_float)
3639 error ("target attribute or pragma changes double precision floating "
3640 "point");
3643 /* Detect invalid option combinations with E500. */
3644 CHECK_E500_OPTIONS;
3646 rs6000_always_hint = (rs6000_cpu != PROCESSOR_POWER4
3647 && rs6000_cpu != PROCESSOR_POWER5
3648 && rs6000_cpu != PROCESSOR_POWER6
3649 && rs6000_cpu != PROCESSOR_POWER7
3650 && rs6000_cpu != PROCESSOR_POWER8
3651 && rs6000_cpu != PROCESSOR_PPCA2
3652 && rs6000_cpu != PROCESSOR_CELL
3653 && rs6000_cpu != PROCESSOR_PPC476);
3654 rs6000_sched_groups = (rs6000_cpu == PROCESSOR_POWER4
3655 || rs6000_cpu == PROCESSOR_POWER5
3656 || rs6000_cpu == PROCESSOR_POWER7
3657 || rs6000_cpu == PROCESSOR_POWER8);
3658 rs6000_align_branch_targets = (rs6000_cpu == PROCESSOR_POWER4
3659 || rs6000_cpu == PROCESSOR_POWER5
3660 || rs6000_cpu == PROCESSOR_POWER6
3661 || rs6000_cpu == PROCESSOR_POWER7
3662 || rs6000_cpu == PROCESSOR_POWER8
3663 || rs6000_cpu == PROCESSOR_PPCE500MC
3664 || rs6000_cpu == PROCESSOR_PPCE500MC64
3665 || rs6000_cpu == PROCESSOR_PPCE5500
3666 || rs6000_cpu == PROCESSOR_PPCE6500);
3668 /* Allow debug switches to override the above settings. These are set to -1
3669 in rs6000.opt to indicate the user hasn't directly set the switch. */
3670 if (TARGET_ALWAYS_HINT >= 0)
3671 rs6000_always_hint = TARGET_ALWAYS_HINT;
3673 if (TARGET_SCHED_GROUPS >= 0)
3674 rs6000_sched_groups = TARGET_SCHED_GROUPS;
3676 if (TARGET_ALIGN_BRANCH_TARGETS >= 0)
3677 rs6000_align_branch_targets = TARGET_ALIGN_BRANCH_TARGETS;
3679 rs6000_sched_restricted_insns_priority
3680 = (rs6000_sched_groups ? 1 : 0);
3682 /* Handle -msched-costly-dep option. */
3683 rs6000_sched_costly_dep
3684 = (rs6000_sched_groups ? true_store_to_load_dep_costly : no_dep_costly);
3686 if (rs6000_sched_costly_dep_str)
3688 if (! strcmp (rs6000_sched_costly_dep_str, "no"))
3689 rs6000_sched_costly_dep = no_dep_costly;
3690 else if (! strcmp (rs6000_sched_costly_dep_str, "all"))
3691 rs6000_sched_costly_dep = all_deps_costly;
3692 else if (! strcmp (rs6000_sched_costly_dep_str, "true_store_to_load"))
3693 rs6000_sched_costly_dep = true_store_to_load_dep_costly;
3694 else if (! strcmp (rs6000_sched_costly_dep_str, "store_to_load"))
3695 rs6000_sched_costly_dep = store_to_load_dep_costly;
3696 else
3697 rs6000_sched_costly_dep = ((enum rs6000_dependence_cost)
3698 atoi (rs6000_sched_costly_dep_str));
3701 /* Handle -minsert-sched-nops option. */
3702 rs6000_sched_insert_nops
3703 = (rs6000_sched_groups ? sched_finish_regroup_exact : sched_finish_none);
3705 if (rs6000_sched_insert_nops_str)
3707 if (! strcmp (rs6000_sched_insert_nops_str, "no"))
3708 rs6000_sched_insert_nops = sched_finish_none;
3709 else if (! strcmp (rs6000_sched_insert_nops_str, "pad"))
3710 rs6000_sched_insert_nops = sched_finish_pad_groups;
3711 else if (! strcmp (rs6000_sched_insert_nops_str, "regroup_exact"))
3712 rs6000_sched_insert_nops = sched_finish_regroup_exact;
3713 else
3714 rs6000_sched_insert_nops = ((enum rs6000_nop_insertion)
3715 atoi (rs6000_sched_insert_nops_str));
3718 if (global_init_p)
3720 #ifdef TARGET_REGNAMES
3721 /* If the user desires alternate register names, copy in the
3722 alternate names now. */
3723 if (TARGET_REGNAMES)
3724 memcpy (rs6000_reg_names, alt_reg_names, sizeof (rs6000_reg_names));
3725 #endif
3727 /* Set aix_struct_return last, after the ABI is determined.
3728 If -maix-struct-return or -msvr4-struct-return was explicitly
3729 used, don't override with the ABI default. */
3730 if (!global_options_set.x_aix_struct_return)
3731 aix_struct_return = (DEFAULT_ABI != ABI_V4 || DRAFT_V4_STRUCT_RET);
3733 #if 0
3734 /* IBM XL compiler defaults to unsigned bitfields. */
3735 if (TARGET_XL_COMPAT)
3736 flag_signed_bitfields = 0;
3737 #endif
3739 if (TARGET_LONG_DOUBLE_128 && !TARGET_IEEEQUAD)
3740 REAL_MODE_FORMAT (TFmode) = &ibm_extended_format;
3742 if (TARGET_TOC)
3743 ASM_GENERATE_INTERNAL_LABEL (toc_label_name, "LCTOC", 1);
3745 /* We can only guarantee the availability of DI pseudo-ops when
3746 assembling for 64-bit targets. */
3747 if (!TARGET_64BIT)
3749 targetm.asm_out.aligned_op.di = NULL;
3750 targetm.asm_out.unaligned_op.di = NULL;
3754 /* Set branch target alignment, if not optimizing for size. */
3755 if (!optimize_size)
3757 /* Cell wants to be aligned 8byte for dual issue. Titan wants to be
3758 aligned 8byte to avoid misprediction by the branch predictor. */
3759 if (rs6000_cpu == PROCESSOR_TITAN
3760 || rs6000_cpu == PROCESSOR_CELL)
3762 if (align_functions <= 0)
3763 align_functions = 8;
3764 if (align_jumps <= 0)
3765 align_jumps = 8;
3766 if (align_loops <= 0)
3767 align_loops = 8;
3769 if (rs6000_align_branch_targets)
3771 if (align_functions <= 0)
3772 align_functions = 16;
3773 if (align_jumps <= 0)
3774 align_jumps = 16;
3775 if (align_loops <= 0)
3777 can_override_loop_align = 1;
3778 align_loops = 16;
3781 if (align_jumps_max_skip <= 0)
3782 align_jumps_max_skip = 15;
3783 if (align_loops_max_skip <= 0)
3784 align_loops_max_skip = 15;
3787 /* Arrange to save and restore machine status around nested functions. */
3788 init_machine_status = rs6000_init_machine_status;
3790 /* We should always be splitting complex arguments, but we can't break
3791 Linux and Darwin ABIs at the moment. For now, only AIX is fixed. */
3792 if (DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN)
3793 targetm.calls.split_complex_arg = NULL;
3796 /* Initialize rs6000_cost with the appropriate target costs. */
3797 if (optimize_size)
3798 rs6000_cost = TARGET_POWERPC64 ? &size64_cost : &size32_cost;
3799 else
3800 switch (rs6000_cpu)
3802 case PROCESSOR_RS64A:
3803 rs6000_cost = &rs64a_cost;
3804 break;
3806 case PROCESSOR_MPCCORE:
3807 rs6000_cost = &mpccore_cost;
3808 break;
3810 case PROCESSOR_PPC403:
3811 rs6000_cost = &ppc403_cost;
3812 break;
3814 case PROCESSOR_PPC405:
3815 rs6000_cost = &ppc405_cost;
3816 break;
3818 case PROCESSOR_PPC440:
3819 rs6000_cost = &ppc440_cost;
3820 break;
3822 case PROCESSOR_PPC476:
3823 rs6000_cost = &ppc476_cost;
3824 break;
3826 case PROCESSOR_PPC601:
3827 rs6000_cost = &ppc601_cost;
3828 break;
3830 case PROCESSOR_PPC603:
3831 rs6000_cost = &ppc603_cost;
3832 break;
3834 case PROCESSOR_PPC604:
3835 rs6000_cost = &ppc604_cost;
3836 break;
3838 case PROCESSOR_PPC604e:
3839 rs6000_cost = &ppc604e_cost;
3840 break;
3842 case PROCESSOR_PPC620:
3843 rs6000_cost = &ppc620_cost;
3844 break;
3846 case PROCESSOR_PPC630:
3847 rs6000_cost = &ppc630_cost;
3848 break;
3850 case PROCESSOR_CELL:
3851 rs6000_cost = &ppccell_cost;
3852 break;
3854 case PROCESSOR_PPC750:
3855 case PROCESSOR_PPC7400:
3856 rs6000_cost = &ppc750_cost;
3857 break;
3859 case PROCESSOR_PPC7450:
3860 rs6000_cost = &ppc7450_cost;
3861 break;
3863 case PROCESSOR_PPC8540:
3864 case PROCESSOR_PPC8548:
3865 rs6000_cost = &ppc8540_cost;
3866 break;
3868 case PROCESSOR_PPCE300C2:
3869 case PROCESSOR_PPCE300C3:
3870 rs6000_cost = &ppce300c2c3_cost;
3871 break;
3873 case PROCESSOR_PPCE500MC:
3874 rs6000_cost = &ppce500mc_cost;
3875 break;
3877 case PROCESSOR_PPCE500MC64:
3878 rs6000_cost = &ppce500mc64_cost;
3879 break;
3881 case PROCESSOR_PPCE5500:
3882 rs6000_cost = &ppce5500_cost;
3883 break;
3885 case PROCESSOR_PPCE6500:
3886 rs6000_cost = &ppce6500_cost;
3887 break;
3889 case PROCESSOR_TITAN:
3890 rs6000_cost = &titan_cost;
3891 break;
3893 case PROCESSOR_POWER4:
3894 case PROCESSOR_POWER5:
3895 rs6000_cost = &power4_cost;
3896 break;
3898 case PROCESSOR_POWER6:
3899 rs6000_cost = &power6_cost;
3900 break;
3902 case PROCESSOR_POWER7:
3903 rs6000_cost = &power7_cost;
3904 break;
3906 case PROCESSOR_POWER8:
3907 rs6000_cost = &power8_cost;
3908 break;
3910 case PROCESSOR_PPCA2:
3911 rs6000_cost = &ppca2_cost;
3912 break;
3914 default:
3915 gcc_unreachable ();
3918 if (global_init_p)
3920 maybe_set_param_value (PARAM_SIMULTANEOUS_PREFETCHES,
3921 rs6000_cost->simultaneous_prefetches,
3922 global_options.x_param_values,
3923 global_options_set.x_param_values);
3924 maybe_set_param_value (PARAM_L1_CACHE_SIZE, rs6000_cost->l1_cache_size,
3925 global_options.x_param_values,
3926 global_options_set.x_param_values);
3927 maybe_set_param_value (PARAM_L1_CACHE_LINE_SIZE,
3928 rs6000_cost->cache_line_size,
3929 global_options.x_param_values,
3930 global_options_set.x_param_values);
3931 maybe_set_param_value (PARAM_L2_CACHE_SIZE, rs6000_cost->l2_cache_size,
3932 global_options.x_param_values,
3933 global_options_set.x_param_values);
3935 /* Increase loop peeling limits based on performance analysis. */
3936 maybe_set_param_value (PARAM_MAX_PEELED_INSNS, 400,
3937 global_options.x_param_values,
3938 global_options_set.x_param_values);
3939 maybe_set_param_value (PARAM_MAX_COMPLETELY_PEELED_INSNS, 400,
3940 global_options.x_param_values,
3941 global_options_set.x_param_values);
3943 /* If using typedef char *va_list, signal that
3944 __builtin_va_start (&ap, 0) can be optimized to
3945 ap = __builtin_next_arg (0). */
3946 if (DEFAULT_ABI != ABI_V4)
3947 targetm.expand_builtin_va_start = NULL;
3950 /* Set up single/double float flags.
3951 If TARGET_HARD_FLOAT is set, but neither single or double is set,
3952 then set both flags. */
3953 if (TARGET_HARD_FLOAT && TARGET_FPRS
3954 && rs6000_single_float == 0 && rs6000_double_float == 0)
3955 rs6000_single_float = rs6000_double_float = 1;
3957 /* If not explicitly specified via option, decide whether to generate indexed
3958 load/store instructions. */
3959 if (TARGET_AVOID_XFORM == -1)
3960 /* Avoid indexed addressing when targeting Power6 in order to avoid the
3961 DERAT mispredict penalty. However the LVE and STVE altivec instructions
3962 need indexed accesses and the type used is the scalar type of the element
3963 being loaded or stored. */
3964 TARGET_AVOID_XFORM = (rs6000_cpu == PROCESSOR_POWER6 && TARGET_CMPB
3965 && !TARGET_ALTIVEC);
3967 /* Set the -mrecip options. */
3968 if (rs6000_recip_name)
3970 char *p = ASTRDUP (rs6000_recip_name);
3971 char *q;
3972 unsigned int mask, i;
3973 bool invert;
3975 while ((q = strtok (p, ",")) != NULL)
3977 p = NULL;
3978 if (*q == '!')
3980 invert = true;
3981 q++;
3983 else
3984 invert = false;
3986 if (!strcmp (q, "default"))
3987 mask = ((TARGET_RECIP_PRECISION)
3988 ? RECIP_HIGH_PRECISION : RECIP_LOW_PRECISION);
3989 else
3991 for (i = 0; i < ARRAY_SIZE (recip_options); i++)
3992 if (!strcmp (q, recip_options[i].string))
3994 mask = recip_options[i].mask;
3995 break;
3998 if (i == ARRAY_SIZE (recip_options))
4000 error ("unknown option for -mrecip=%s", q);
4001 invert = false;
4002 mask = 0;
4003 ret = false;
4007 if (invert)
4008 rs6000_recip_control &= ~mask;
4009 else
4010 rs6000_recip_control |= mask;
4014 /* Set the builtin mask of the various options used that could affect which
4015 builtins were used. In the past we used target_flags, but we've run out
4016 of bits, and some options like SPE and PAIRED are no longer in
4017 target_flags. */
4018 rs6000_builtin_mask = rs6000_builtin_mask_calculate ();
4019 if (TARGET_DEBUG_BUILTIN || TARGET_DEBUG_TARGET)
4021 fprintf (stderr,
4022 "new builtin mask = " HOST_WIDE_INT_PRINT_HEX ", ",
4023 rs6000_builtin_mask);
4024 rs6000_print_builtin_options (stderr, 0, NULL, rs6000_builtin_mask);
4027 /* Initialize all of the registers. */
4028 rs6000_init_hard_regno_mode_ok (global_init_p);
4030 /* Save the initial options in case the user does function specific options */
4031 if (global_init_p)
4032 target_option_default_node = target_option_current_node
4033 = build_target_option_node (&global_options);
4035 /* If not explicitly specified via option, decide whether to generate the
4036 extra blr's required to preserve the link stack on some cpus (eg, 476). */
4037 if (TARGET_LINK_STACK == -1)
4038 SET_TARGET_LINK_STACK (rs6000_cpu == PROCESSOR_PPC476 && flag_pic);
4040 return ret;
4043 /* Implement TARGET_OPTION_OVERRIDE. On the RS/6000 this is used to
4044 define the target cpu type. */
4046 static void
4047 rs6000_option_override (void)
4049 (void) rs6000_option_override_internal (true);
4053 /* Implement targetm.vectorize.builtin_mask_for_load. */
4054 static tree
4055 rs6000_builtin_mask_for_load (void)
4057 if (TARGET_ALTIVEC || TARGET_VSX)
4058 return altivec_builtin_mask_for_load;
4059 else
4060 return 0;
4063 /* Implement LOOP_ALIGN. */
4065 rs6000_loop_align (rtx label)
4067 basic_block bb;
4068 int ninsns;
4070 /* Don't override loop alignment if -falign-loops was specified. */
4071 if (!can_override_loop_align)
4072 return align_loops_log;
4074 bb = BLOCK_FOR_INSN (label);
4075 ninsns = num_loop_insns(bb->loop_father);
4077 /* Align small loops to 32 bytes to fit in an icache sector, otherwise return default. */
4078 if (ninsns > 4 && ninsns <= 8
4079 && (rs6000_cpu == PROCESSOR_POWER4
4080 || rs6000_cpu == PROCESSOR_POWER5
4081 || rs6000_cpu == PROCESSOR_POWER6
4082 || rs6000_cpu == PROCESSOR_POWER7
4083 || rs6000_cpu == PROCESSOR_POWER8))
4084 return 5;
4085 else
4086 return align_loops_log;
4089 /* Implement TARGET_LOOP_ALIGN_MAX_SKIP. */
4090 static int
4091 rs6000_loop_align_max_skip (rtx label)
4093 return (1 << rs6000_loop_align (label)) - 1;
4096 /* Return true iff, data reference of TYPE can reach vector alignment (16)
4097 after applying N number of iterations. This routine does not determine
4098 how may iterations are required to reach desired alignment. */
4100 static bool
4101 rs6000_vector_alignment_reachable (const_tree type ATTRIBUTE_UNUSED, bool is_packed)
4103 if (is_packed)
4104 return false;
4106 if (TARGET_32BIT)
4108 if (rs6000_alignment_flags == MASK_ALIGN_NATURAL)
4109 return true;
4111 if (rs6000_alignment_flags == MASK_ALIGN_POWER)
4112 return true;
4114 return false;
4116 else
4118 if (TARGET_MACHO)
4119 return false;
4121 /* Assuming that all other types are naturally aligned. CHECKME! */
4122 return true;
4126 /* Return true if the vector misalignment factor is supported by the
4127 target. */
4128 static bool
4129 rs6000_builtin_support_vector_misalignment (enum machine_mode mode,
4130 const_tree type,
4131 int misalignment,
4132 bool is_packed)
4134 if (TARGET_VSX)
4136 /* Return if movmisalign pattern is not supported for this mode. */
4137 if (optab_handler (movmisalign_optab, mode) == CODE_FOR_nothing)
4138 return false;
4140 if (misalignment == -1)
4142 /* Misalignment factor is unknown at compile time but we know
4143 it's word aligned. */
4144 if (rs6000_vector_alignment_reachable (type, is_packed))
4146 int element_size = TREE_INT_CST_LOW (TYPE_SIZE (type));
4148 if (element_size == 64 || element_size == 32)
4149 return true;
4152 return false;
4155 /* VSX supports word-aligned vector. */
4156 if (misalignment % 4 == 0)
4157 return true;
4159 return false;
4162 /* Implement targetm.vectorize.builtin_vectorization_cost. */
4163 static int
4164 rs6000_builtin_vectorization_cost (enum vect_cost_for_stmt type_of_cost,
4165 tree vectype, int misalign)
4167 unsigned elements;
4168 tree elem_type;
4170 switch (type_of_cost)
4172 case scalar_stmt:
4173 case scalar_load:
4174 case scalar_store:
4175 case vector_stmt:
4176 case vector_load:
4177 case vector_store:
4178 case vec_to_scalar:
4179 case scalar_to_vec:
4180 case cond_branch_not_taken:
4181 return 1;
4183 case vec_perm:
4184 if (TARGET_VSX)
4185 return 3;
4186 else
4187 return 1;
4189 case vec_promote_demote:
4190 if (TARGET_VSX)
4191 return 4;
4192 else
4193 return 1;
4195 case cond_branch_taken:
4196 return 3;
4198 case unaligned_load:
4199 if (TARGET_VSX && TARGET_ALLOW_MOVMISALIGN)
4201 elements = TYPE_VECTOR_SUBPARTS (vectype);
4202 if (elements == 2)
4203 /* Double word aligned. */
4204 return 2;
4206 if (elements == 4)
4208 switch (misalign)
4210 case 8:
4211 /* Double word aligned. */
4212 return 2;
4214 case -1:
4215 /* Unknown misalignment. */
4216 case 4:
4217 case 12:
4218 /* Word aligned. */
4219 return 22;
4221 default:
4222 gcc_unreachable ();
4227 if (TARGET_ALTIVEC)
4228 /* Misaligned loads are not supported. */
4229 gcc_unreachable ();
4231 return 2;
4233 case unaligned_store:
4234 if (TARGET_VSX && TARGET_ALLOW_MOVMISALIGN)
4236 elements = TYPE_VECTOR_SUBPARTS (vectype);
4237 if (elements == 2)
4238 /* Double word aligned. */
4239 return 2;
4241 if (elements == 4)
4243 switch (misalign)
4245 case 8:
4246 /* Double word aligned. */
4247 return 2;
4249 case -1:
4250 /* Unknown misalignment. */
4251 case 4:
4252 case 12:
4253 /* Word aligned. */
4254 return 23;
4256 default:
4257 gcc_unreachable ();
4262 if (TARGET_ALTIVEC)
4263 /* Misaligned stores are not supported. */
4264 gcc_unreachable ();
4266 return 2;
4268 case vec_construct:
4269 elements = TYPE_VECTOR_SUBPARTS (vectype);
4270 elem_type = TREE_TYPE (vectype);
4271 /* 32-bit vectors loaded into registers are stored as double
4272 precision, so we need n/2 converts in addition to the usual
4273 n/2 merges to construct a vector of short floats from them. */
4274 if (SCALAR_FLOAT_TYPE_P (elem_type)
4275 && TYPE_PRECISION (elem_type) == 32)
4276 return elements + 1;
4277 else
4278 return elements / 2 + 1;
4280 default:
4281 gcc_unreachable ();
4285 /* Implement targetm.vectorize.preferred_simd_mode. */
4287 static enum machine_mode
4288 rs6000_preferred_simd_mode (enum machine_mode mode)
4290 if (TARGET_VSX)
4291 switch (mode)
4293 case DFmode:
4294 return V2DFmode;
4295 default:;
4297 if (TARGET_ALTIVEC || TARGET_VSX)
4298 switch (mode)
4300 case SFmode:
4301 return V4SFmode;
4302 case TImode:
4303 return V1TImode;
4304 case DImode:
4305 return V2DImode;
4306 case SImode:
4307 return V4SImode;
4308 case HImode:
4309 return V8HImode;
4310 case QImode:
4311 return V16QImode;
4312 default:;
4314 if (TARGET_SPE)
4315 switch (mode)
4317 case SFmode:
4318 return V2SFmode;
4319 case SImode:
4320 return V2SImode;
4321 default:;
4323 if (TARGET_PAIRED_FLOAT
4324 && mode == SFmode)
4325 return V2SFmode;
4326 return word_mode;
4329 typedef struct _rs6000_cost_data
4331 struct loop *loop_info;
4332 unsigned cost[3];
4333 } rs6000_cost_data;
4335 /* Test for likely overcommitment of vector hardware resources. If a
4336 loop iteration is relatively large, and too large a percentage of
4337 instructions in the loop are vectorized, the cost model may not
4338 adequately reflect delays from unavailable vector resources.
4339 Penalize the loop body cost for this case. */
4341 static void
4342 rs6000_density_test (rs6000_cost_data *data)
4344 const int DENSITY_PCT_THRESHOLD = 85;
4345 const int DENSITY_SIZE_THRESHOLD = 70;
4346 const int DENSITY_PENALTY = 10;
4347 struct loop *loop = data->loop_info;
4348 basic_block *bbs = get_loop_body (loop);
4349 int nbbs = loop->num_nodes;
4350 int vec_cost = data->cost[vect_body], not_vec_cost = 0;
4351 int i, density_pct;
4353 for (i = 0; i < nbbs; i++)
4355 basic_block bb = bbs[i];
4356 gimple_stmt_iterator gsi;
4358 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4360 gimple stmt = gsi_stmt (gsi);
4361 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4363 if (!STMT_VINFO_RELEVANT_P (stmt_info)
4364 && !STMT_VINFO_IN_PATTERN_P (stmt_info))
4365 not_vec_cost++;
4369 free (bbs);
4370 density_pct = (vec_cost * 100) / (vec_cost + not_vec_cost);
4372 if (density_pct > DENSITY_PCT_THRESHOLD
4373 && vec_cost + not_vec_cost > DENSITY_SIZE_THRESHOLD)
4375 data->cost[vect_body] = vec_cost * (100 + DENSITY_PENALTY) / 100;
4376 if (dump_enabled_p ())
4377 dump_printf_loc (MSG_NOTE, vect_location,
4378 "density %d%%, cost %d exceeds threshold, penalizing "
4379 "loop body cost by %d%%", density_pct,
4380 vec_cost + not_vec_cost, DENSITY_PENALTY);
4384 /* Implement targetm.vectorize.init_cost. */
4386 static void *
4387 rs6000_init_cost (struct loop *loop_info)
4389 rs6000_cost_data *data = XNEW (struct _rs6000_cost_data);
4390 data->loop_info = loop_info;
4391 data->cost[vect_prologue] = 0;
4392 data->cost[vect_body] = 0;
4393 data->cost[vect_epilogue] = 0;
4394 return data;
4397 /* Implement targetm.vectorize.add_stmt_cost. */
4399 static unsigned
4400 rs6000_add_stmt_cost (void *data, int count, enum vect_cost_for_stmt kind,
4401 struct _stmt_vec_info *stmt_info, int misalign,
4402 enum vect_cost_model_location where)
4404 rs6000_cost_data *cost_data = (rs6000_cost_data*) data;
4405 unsigned retval = 0;
4407 if (flag_vect_cost_model)
4409 tree vectype = stmt_info ? stmt_vectype (stmt_info) : NULL_TREE;
4410 int stmt_cost = rs6000_builtin_vectorization_cost (kind, vectype,
4411 misalign);
4412 /* Statements in an inner loop relative to the loop being
4413 vectorized are weighted more heavily. The value here is
4414 arbitrary and could potentially be improved with analysis. */
4415 if (where == vect_body && stmt_info && stmt_in_inner_loop_p (stmt_info))
4416 count *= 50; /* FIXME. */
4418 retval = (unsigned) (count * stmt_cost);
4419 cost_data->cost[where] += retval;
4422 return retval;
4425 /* Implement targetm.vectorize.finish_cost. */
4427 static void
4428 rs6000_finish_cost (void *data, unsigned *prologue_cost,
4429 unsigned *body_cost, unsigned *epilogue_cost)
4431 rs6000_cost_data *cost_data = (rs6000_cost_data*) data;
4433 if (cost_data->loop_info)
4434 rs6000_density_test (cost_data);
4436 *prologue_cost = cost_data->cost[vect_prologue];
4437 *body_cost = cost_data->cost[vect_body];
4438 *epilogue_cost = cost_data->cost[vect_epilogue];
4441 /* Implement targetm.vectorize.destroy_cost_data. */
4443 static void
4444 rs6000_destroy_cost_data (void *data)
4446 free (data);
4449 /* Handler for the Mathematical Acceleration Subsystem (mass) interface to a
4450 library with vectorized intrinsics. */
4452 static tree
4453 rs6000_builtin_vectorized_libmass (tree fndecl, tree type_out, tree type_in)
4455 char name[32];
4456 const char *suffix = NULL;
4457 tree fntype, new_fndecl, bdecl = NULL_TREE;
4458 int n_args = 1;
4459 const char *bname;
4460 enum machine_mode el_mode, in_mode;
4461 int n, in_n;
4463 /* Libmass is suitable for unsafe math only as it does not correctly support
4464 parts of IEEE with the required precision such as denormals. Only support
4465 it if we have VSX to use the simd d2 or f4 functions.
4466 XXX: Add variable length support. */
4467 if (!flag_unsafe_math_optimizations || !TARGET_VSX)
4468 return NULL_TREE;
4470 el_mode = TYPE_MODE (TREE_TYPE (type_out));
4471 n = TYPE_VECTOR_SUBPARTS (type_out);
4472 in_mode = TYPE_MODE (TREE_TYPE (type_in));
4473 in_n = TYPE_VECTOR_SUBPARTS (type_in);
4474 if (el_mode != in_mode
4475 || n != in_n)
4476 return NULL_TREE;
4478 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
4480 enum built_in_function fn = DECL_FUNCTION_CODE (fndecl);
4481 switch (fn)
4483 case BUILT_IN_ATAN2:
4484 case BUILT_IN_HYPOT:
4485 case BUILT_IN_POW:
4486 n_args = 2;
4487 /* fall through */
4489 case BUILT_IN_ACOS:
4490 case BUILT_IN_ACOSH:
4491 case BUILT_IN_ASIN:
4492 case BUILT_IN_ASINH:
4493 case BUILT_IN_ATAN:
4494 case BUILT_IN_ATANH:
4495 case BUILT_IN_CBRT:
4496 case BUILT_IN_COS:
4497 case BUILT_IN_COSH:
4498 case BUILT_IN_ERF:
4499 case BUILT_IN_ERFC:
4500 case BUILT_IN_EXP2:
4501 case BUILT_IN_EXP:
4502 case BUILT_IN_EXPM1:
4503 case BUILT_IN_LGAMMA:
4504 case BUILT_IN_LOG10:
4505 case BUILT_IN_LOG1P:
4506 case BUILT_IN_LOG2:
4507 case BUILT_IN_LOG:
4508 case BUILT_IN_SIN:
4509 case BUILT_IN_SINH:
4510 case BUILT_IN_SQRT:
4511 case BUILT_IN_TAN:
4512 case BUILT_IN_TANH:
4513 bdecl = builtin_decl_implicit (fn);
4514 suffix = "d2"; /* pow -> powd2 */
4515 if (el_mode != DFmode
4516 || n != 2
4517 || !bdecl)
4518 return NULL_TREE;
4519 break;
4521 case BUILT_IN_ATAN2F:
4522 case BUILT_IN_HYPOTF:
4523 case BUILT_IN_POWF:
4524 n_args = 2;
4525 /* fall through */
4527 case BUILT_IN_ACOSF:
4528 case BUILT_IN_ACOSHF:
4529 case BUILT_IN_ASINF:
4530 case BUILT_IN_ASINHF:
4531 case BUILT_IN_ATANF:
4532 case BUILT_IN_ATANHF:
4533 case BUILT_IN_CBRTF:
4534 case BUILT_IN_COSF:
4535 case BUILT_IN_COSHF:
4536 case BUILT_IN_ERFF:
4537 case BUILT_IN_ERFCF:
4538 case BUILT_IN_EXP2F:
4539 case BUILT_IN_EXPF:
4540 case BUILT_IN_EXPM1F:
4541 case BUILT_IN_LGAMMAF:
4542 case BUILT_IN_LOG10F:
4543 case BUILT_IN_LOG1PF:
4544 case BUILT_IN_LOG2F:
4545 case BUILT_IN_LOGF:
4546 case BUILT_IN_SINF:
4547 case BUILT_IN_SINHF:
4548 case BUILT_IN_SQRTF:
4549 case BUILT_IN_TANF:
4550 case BUILT_IN_TANHF:
4551 bdecl = builtin_decl_implicit (fn);
4552 suffix = "4"; /* powf -> powf4 */
4553 if (el_mode != SFmode
4554 || n != 4
4555 || !bdecl)
4556 return NULL_TREE;
4557 break;
4559 default:
4560 return NULL_TREE;
4563 else
4564 return NULL_TREE;
4566 gcc_assert (suffix != NULL);
4567 bname = IDENTIFIER_POINTER (DECL_NAME (bdecl));
4568 if (!bname)
4569 return NULL_TREE;
4571 strcpy (name, bname + sizeof ("__builtin_") - 1);
4572 strcat (name, suffix);
4574 if (n_args == 1)
4575 fntype = build_function_type_list (type_out, type_in, NULL);
4576 else if (n_args == 2)
4577 fntype = build_function_type_list (type_out, type_in, type_in, NULL);
4578 else
4579 gcc_unreachable ();
4581 /* Build a function declaration for the vectorized function. */
4582 new_fndecl = build_decl (BUILTINS_LOCATION,
4583 FUNCTION_DECL, get_identifier (name), fntype);
4584 TREE_PUBLIC (new_fndecl) = 1;
4585 DECL_EXTERNAL (new_fndecl) = 1;
4586 DECL_IS_NOVOPS (new_fndecl) = 1;
4587 TREE_READONLY (new_fndecl) = 1;
4589 return new_fndecl;
4592 /* Returns a function decl for a vectorized version of the builtin function
4593 with builtin function code FN and the result vector type TYPE, or NULL_TREE
4594 if it is not available. */
4596 static tree
4597 rs6000_builtin_vectorized_function (tree fndecl, tree type_out,
4598 tree type_in)
4600 enum machine_mode in_mode, out_mode;
4601 int in_n, out_n;
4603 if (TARGET_DEBUG_BUILTIN)
4604 fprintf (stderr, "rs6000_builtin_vectorized_function (%s, %s, %s)\n",
4605 IDENTIFIER_POINTER (DECL_NAME (fndecl)),
4606 GET_MODE_NAME (TYPE_MODE (type_out)),
4607 GET_MODE_NAME (TYPE_MODE (type_in)));
4609 if (TREE_CODE (type_out) != VECTOR_TYPE
4610 || TREE_CODE (type_in) != VECTOR_TYPE
4611 || !TARGET_VECTORIZE_BUILTINS)
4612 return NULL_TREE;
4614 out_mode = TYPE_MODE (TREE_TYPE (type_out));
4615 out_n = TYPE_VECTOR_SUBPARTS (type_out);
4616 in_mode = TYPE_MODE (TREE_TYPE (type_in));
4617 in_n = TYPE_VECTOR_SUBPARTS (type_in);
4619 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
4621 enum built_in_function fn = DECL_FUNCTION_CODE (fndecl);
4622 switch (fn)
4624 case BUILT_IN_CLZIMAX:
4625 case BUILT_IN_CLZLL:
4626 case BUILT_IN_CLZL:
4627 case BUILT_IN_CLZ:
4628 if (TARGET_P8_VECTOR && in_mode == out_mode && out_n == in_n)
4630 if (out_mode == QImode && out_n == 16)
4631 return rs6000_builtin_decls[P8V_BUILTIN_VCLZB];
4632 else if (out_mode == HImode && out_n == 8)
4633 return rs6000_builtin_decls[P8V_BUILTIN_VCLZH];
4634 else if (out_mode == SImode && out_n == 4)
4635 return rs6000_builtin_decls[P8V_BUILTIN_VCLZW];
4636 else if (out_mode == DImode && out_n == 2)
4637 return rs6000_builtin_decls[P8V_BUILTIN_VCLZD];
4639 break;
4640 case BUILT_IN_COPYSIGN:
4641 if (VECTOR_UNIT_VSX_P (V2DFmode)
4642 && out_mode == DFmode && out_n == 2
4643 && in_mode == DFmode && in_n == 2)
4644 return rs6000_builtin_decls[VSX_BUILTIN_CPSGNDP];
4645 break;
4646 case BUILT_IN_COPYSIGNF:
4647 if (out_mode != SFmode || out_n != 4
4648 || in_mode != SFmode || in_n != 4)
4649 break;
4650 if (VECTOR_UNIT_VSX_P (V4SFmode))
4651 return rs6000_builtin_decls[VSX_BUILTIN_CPSGNSP];
4652 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
4653 return rs6000_builtin_decls[ALTIVEC_BUILTIN_COPYSIGN_V4SF];
4654 break;
4655 case BUILT_IN_POPCOUNTIMAX:
4656 case BUILT_IN_POPCOUNTLL:
4657 case BUILT_IN_POPCOUNTL:
4658 case BUILT_IN_POPCOUNT:
4659 if (TARGET_P8_VECTOR && in_mode == out_mode && out_n == in_n)
4661 if (out_mode == QImode && out_n == 16)
4662 return rs6000_builtin_decls[P8V_BUILTIN_VPOPCNTB];
4663 else if (out_mode == HImode && out_n == 8)
4664 return rs6000_builtin_decls[P8V_BUILTIN_VPOPCNTH];
4665 else if (out_mode == SImode && out_n == 4)
4666 return rs6000_builtin_decls[P8V_BUILTIN_VPOPCNTW];
4667 else if (out_mode == DImode && out_n == 2)
4668 return rs6000_builtin_decls[P8V_BUILTIN_VPOPCNTD];
4670 break;
4671 case BUILT_IN_SQRT:
4672 if (VECTOR_UNIT_VSX_P (V2DFmode)
4673 && out_mode == DFmode && out_n == 2
4674 && in_mode == DFmode && in_n == 2)
4675 return rs6000_builtin_decls[VSX_BUILTIN_XVSQRTDP];
4676 break;
4677 case BUILT_IN_SQRTF:
4678 if (VECTOR_UNIT_VSX_P (V4SFmode)
4679 && out_mode == SFmode && out_n == 4
4680 && in_mode == SFmode && in_n == 4)
4681 return rs6000_builtin_decls[VSX_BUILTIN_XVSQRTSP];
4682 break;
4683 case BUILT_IN_CEIL:
4684 if (VECTOR_UNIT_VSX_P (V2DFmode)
4685 && out_mode == DFmode && out_n == 2
4686 && in_mode == DFmode && in_n == 2)
4687 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIP];
4688 break;
4689 case BUILT_IN_CEILF:
4690 if (out_mode != SFmode || out_n != 4
4691 || in_mode != SFmode || in_n != 4)
4692 break;
4693 if (VECTOR_UNIT_VSX_P (V4SFmode))
4694 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIP];
4695 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
4696 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIP];
4697 break;
4698 case BUILT_IN_FLOOR:
4699 if (VECTOR_UNIT_VSX_P (V2DFmode)
4700 && out_mode == DFmode && out_n == 2
4701 && in_mode == DFmode && in_n == 2)
4702 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIM];
4703 break;
4704 case BUILT_IN_FLOORF:
4705 if (out_mode != SFmode || out_n != 4
4706 || in_mode != SFmode || in_n != 4)
4707 break;
4708 if (VECTOR_UNIT_VSX_P (V4SFmode))
4709 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIM];
4710 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
4711 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIM];
4712 break;
4713 case BUILT_IN_FMA:
4714 if (VECTOR_UNIT_VSX_P (V2DFmode)
4715 && out_mode == DFmode && out_n == 2
4716 && in_mode == DFmode && in_n == 2)
4717 return rs6000_builtin_decls[VSX_BUILTIN_XVMADDDP];
4718 break;
4719 case BUILT_IN_FMAF:
4720 if (VECTOR_UNIT_VSX_P (V4SFmode)
4721 && out_mode == SFmode && out_n == 4
4722 && in_mode == SFmode && in_n == 4)
4723 return rs6000_builtin_decls[VSX_BUILTIN_XVMADDSP];
4724 else if (VECTOR_UNIT_ALTIVEC_P (V4SFmode)
4725 && out_mode == SFmode && out_n == 4
4726 && in_mode == SFmode && in_n == 4)
4727 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VMADDFP];
4728 break;
4729 case BUILT_IN_TRUNC:
4730 if (VECTOR_UNIT_VSX_P (V2DFmode)
4731 && out_mode == DFmode && out_n == 2
4732 && in_mode == DFmode && in_n == 2)
4733 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIZ];
4734 break;
4735 case BUILT_IN_TRUNCF:
4736 if (out_mode != SFmode || out_n != 4
4737 || in_mode != SFmode || in_n != 4)
4738 break;
4739 if (VECTOR_UNIT_VSX_P (V4SFmode))
4740 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIZ];
4741 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
4742 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIZ];
4743 break;
4744 case BUILT_IN_NEARBYINT:
4745 if (VECTOR_UNIT_VSX_P (V2DFmode)
4746 && flag_unsafe_math_optimizations
4747 && out_mode == DFmode && out_n == 2
4748 && in_mode == DFmode && in_n == 2)
4749 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPI];
4750 break;
4751 case BUILT_IN_NEARBYINTF:
4752 if (VECTOR_UNIT_VSX_P (V4SFmode)
4753 && flag_unsafe_math_optimizations
4754 && out_mode == SFmode && out_n == 4
4755 && in_mode == SFmode && in_n == 4)
4756 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPI];
4757 break;
4758 case BUILT_IN_RINT:
4759 if (VECTOR_UNIT_VSX_P (V2DFmode)
4760 && !flag_trapping_math
4761 && out_mode == DFmode && out_n == 2
4762 && in_mode == DFmode && in_n == 2)
4763 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIC];
4764 break;
4765 case BUILT_IN_RINTF:
4766 if (VECTOR_UNIT_VSX_P (V4SFmode)
4767 && !flag_trapping_math
4768 && out_mode == SFmode && out_n == 4
4769 && in_mode == SFmode && in_n == 4)
4770 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIC];
4771 break;
4772 default:
4773 break;
4777 else if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
4779 enum rs6000_builtins fn
4780 = (enum rs6000_builtins)DECL_FUNCTION_CODE (fndecl);
4781 switch (fn)
4783 case RS6000_BUILTIN_RSQRTF:
4784 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode)
4785 && out_mode == SFmode && out_n == 4
4786 && in_mode == SFmode && in_n == 4)
4787 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRSQRTFP];
4788 break;
4789 case RS6000_BUILTIN_RSQRT:
4790 if (VECTOR_UNIT_VSX_P (V2DFmode)
4791 && out_mode == DFmode && out_n == 2
4792 && in_mode == DFmode && in_n == 2)
4793 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_2DF];
4794 break;
4795 case RS6000_BUILTIN_RECIPF:
4796 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode)
4797 && out_mode == SFmode && out_n == 4
4798 && in_mode == SFmode && in_n == 4)
4799 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRECIPFP];
4800 break;
4801 case RS6000_BUILTIN_RECIP:
4802 if (VECTOR_UNIT_VSX_P (V2DFmode)
4803 && out_mode == DFmode && out_n == 2
4804 && in_mode == DFmode && in_n == 2)
4805 return rs6000_builtin_decls[VSX_BUILTIN_RECIP_V2DF];
4806 break;
4807 default:
4808 break;
4812 /* Generate calls to libmass if appropriate. */
4813 if (rs6000_veclib_handler)
4814 return rs6000_veclib_handler (fndecl, type_out, type_in);
4816 return NULL_TREE;
4819 /* Default CPU string for rs6000*_file_start functions. */
4820 static const char *rs6000_default_cpu;
4822 /* Do anything needed at the start of the asm file. */
4824 static void
4825 rs6000_file_start (void)
4827 char buffer[80];
4828 const char *start = buffer;
4829 FILE *file = asm_out_file;
4831 rs6000_default_cpu = TARGET_CPU_DEFAULT;
4833 default_file_start ();
4835 if (flag_verbose_asm)
4837 sprintf (buffer, "\n%s rs6000/powerpc options:", ASM_COMMENT_START);
4839 if (rs6000_default_cpu != 0 && rs6000_default_cpu[0] != '\0')
4841 fprintf (file, "%s --with-cpu=%s", start, rs6000_default_cpu);
4842 start = "";
4845 if (global_options_set.x_rs6000_cpu_index)
4847 fprintf (file, "%s -mcpu=%s", start,
4848 processor_target_table[rs6000_cpu_index].name);
4849 start = "";
4852 if (global_options_set.x_rs6000_tune_index)
4854 fprintf (file, "%s -mtune=%s", start,
4855 processor_target_table[rs6000_tune_index].name);
4856 start = "";
4859 if (PPC405_ERRATUM77)
4861 fprintf (file, "%s PPC405CR_ERRATUM77", start);
4862 start = "";
4865 #ifdef USING_ELFOS_H
4866 switch (rs6000_sdata)
4868 case SDATA_NONE: fprintf (file, "%s -msdata=none", start); start = ""; break;
4869 case SDATA_DATA: fprintf (file, "%s -msdata=data", start); start = ""; break;
4870 case SDATA_SYSV: fprintf (file, "%s -msdata=sysv", start); start = ""; break;
4871 case SDATA_EABI: fprintf (file, "%s -msdata=eabi", start); start = ""; break;
4874 if (rs6000_sdata && g_switch_value)
4876 fprintf (file, "%s -G %d", start,
4877 g_switch_value);
4878 start = "";
4880 #endif
4882 if (*start == '\0')
4883 putc ('\n', file);
4886 if (DEFAULT_ABI == ABI_ELFv2)
4887 fprintf (file, "\t.abiversion 2\n");
4889 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2
4890 || (TARGET_ELF && flag_pic == 2))
4892 switch_to_section (toc_section);
4893 switch_to_section (text_section);
4898 /* Return nonzero if this function is known to have a null epilogue. */
4901 direct_return (void)
4903 if (reload_completed)
4905 rs6000_stack_t *info = rs6000_stack_info ();
4907 if (info->first_gp_reg_save == 32
4908 && info->first_fp_reg_save == 64
4909 && info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1
4910 && ! info->lr_save_p
4911 && ! info->cr_save_p
4912 && info->vrsave_mask == 0
4913 && ! info->push_p)
4914 return 1;
4917 return 0;
4920 /* Return the number of instructions it takes to form a constant in an
4921 integer register. */
4924 num_insns_constant_wide (HOST_WIDE_INT value)
4926 /* signed constant loadable with addi */
4927 if ((unsigned HOST_WIDE_INT) (value + 0x8000) < 0x10000)
4928 return 1;
4930 /* constant loadable with addis */
4931 else if ((value & 0xffff) == 0
4932 && (value >> 31 == -1 || value >> 31 == 0))
4933 return 1;
4935 else if (TARGET_POWERPC64)
4937 HOST_WIDE_INT low = ((value & 0xffffffff) ^ 0x80000000) - 0x80000000;
4938 HOST_WIDE_INT high = value >> 31;
4940 if (high == 0 || high == -1)
4941 return 2;
4943 high >>= 1;
4945 if (low == 0)
4946 return num_insns_constant_wide (high) + 1;
4947 else if (high == 0)
4948 return num_insns_constant_wide (low) + 1;
4949 else
4950 return (num_insns_constant_wide (high)
4951 + num_insns_constant_wide (low) + 1);
4954 else
4955 return 2;
4959 num_insns_constant (rtx op, enum machine_mode mode)
4961 HOST_WIDE_INT low, high;
4963 switch (GET_CODE (op))
4965 case CONST_INT:
4966 if ((INTVAL (op) >> 31) != 0 && (INTVAL (op) >> 31) != -1
4967 && mask64_operand (op, mode))
4968 return 2;
4969 else
4970 return num_insns_constant_wide (INTVAL (op));
4972 case CONST_WIDE_INT:
4974 int i;
4975 int ins = CONST_WIDE_INT_NUNITS (op) - 1;
4976 for (i = 0; i < CONST_WIDE_INT_NUNITS (op); i++)
4977 ins += num_insns_constant_wide (CONST_WIDE_INT_ELT (op, i));
4978 return ins;
4981 case CONST_DOUBLE:
4982 if (mode == SFmode || mode == SDmode)
4984 long l;
4985 REAL_VALUE_TYPE rv;
4987 REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
4988 if (DECIMAL_FLOAT_MODE_P (mode))
4989 REAL_VALUE_TO_TARGET_DECIMAL32 (rv, l);
4990 else
4991 REAL_VALUE_TO_TARGET_SINGLE (rv, l);
4992 return num_insns_constant_wide ((HOST_WIDE_INT) l);
4995 long l[2];
4996 REAL_VALUE_TYPE rv;
4998 REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
4999 if (DECIMAL_FLOAT_MODE_P (mode))
5000 REAL_VALUE_TO_TARGET_DECIMAL64 (rv, l);
5001 else
5002 REAL_VALUE_TO_TARGET_DOUBLE (rv, l);
5003 high = l[WORDS_BIG_ENDIAN == 0];
5004 low = l[WORDS_BIG_ENDIAN != 0];
5006 if (TARGET_32BIT)
5007 return (num_insns_constant_wide (low)
5008 + num_insns_constant_wide (high));
5009 else
5011 if ((high == 0 && low >= 0)
5012 || (high == -1 && low < 0))
5013 return num_insns_constant_wide (low);
5015 else if (mask64_operand (op, mode))
5016 return 2;
5018 else if (low == 0)
5019 return num_insns_constant_wide (high) + 1;
5021 else
5022 return (num_insns_constant_wide (high)
5023 + num_insns_constant_wide (low) + 1);
5026 default:
5027 gcc_unreachable ();
5031 /* Interpret element ELT of the CONST_VECTOR OP as an integer value.
5032 If the mode of OP is MODE_VECTOR_INT, this simply returns the
5033 corresponding element of the vector, but for V4SFmode and V2SFmode,
5034 the corresponding "float" is interpreted as an SImode integer. */
5036 HOST_WIDE_INT
5037 const_vector_elt_as_int (rtx op, unsigned int elt)
5039 rtx tmp;
5041 /* We can't handle V2DImode and V2DFmode vector constants here yet. */
5042 gcc_assert (GET_MODE (op) != V2DImode
5043 && GET_MODE (op) != V2DFmode);
5045 tmp = CONST_VECTOR_ELT (op, elt);
5046 if (GET_MODE (op) == V4SFmode
5047 || GET_MODE (op) == V2SFmode)
5048 tmp = gen_lowpart (SImode, tmp);
5049 return INTVAL (tmp);
5052 /* Return true if OP can be synthesized with a particular vspltisb, vspltish
5053 or vspltisw instruction. OP is a CONST_VECTOR. Which instruction is used
5054 depends on STEP and COPIES, one of which will be 1. If COPIES > 1,
5055 all items are set to the same value and contain COPIES replicas of the
5056 vsplt's operand; if STEP > 1, one in STEP elements is set to the vsplt's
5057 operand and the others are set to the value of the operand's msb. */
5059 static bool
5060 vspltis_constant (rtx op, unsigned step, unsigned copies)
5062 enum machine_mode mode = GET_MODE (op);
5063 enum machine_mode inner = GET_MODE_INNER (mode);
5065 unsigned i;
5066 unsigned nunits;
5067 unsigned bitsize;
5068 unsigned mask;
5070 HOST_WIDE_INT val;
5071 HOST_WIDE_INT splat_val;
5072 HOST_WIDE_INT msb_val;
5074 if (mode == V2DImode || mode == V2DFmode || mode == V1TImode)
5075 return false;
5077 nunits = GET_MODE_NUNITS (mode);
5078 bitsize = GET_MODE_BITSIZE (inner);
5079 mask = GET_MODE_MASK (inner);
5081 val = const_vector_elt_as_int (op, BYTES_BIG_ENDIAN ? nunits - 1 : 0);
5082 splat_val = val;
5083 msb_val = val >= 0 ? 0 : -1;
5085 /* Construct the value to be splatted, if possible. If not, return 0. */
5086 for (i = 2; i <= copies; i *= 2)
5088 HOST_WIDE_INT small_val;
5089 bitsize /= 2;
5090 small_val = splat_val >> bitsize;
5091 mask >>= bitsize;
5092 if (splat_val != ((small_val << bitsize) | (small_val & mask)))
5093 return false;
5094 splat_val = small_val;
5097 /* Check if SPLAT_VAL can really be the operand of a vspltis[bhw]. */
5098 if (EASY_VECTOR_15 (splat_val))
5101 /* Also check if we can splat, and then add the result to itself. Do so if
5102 the value is positive, of if the splat instruction is using OP's mode;
5103 for splat_val < 0, the splat and the add should use the same mode. */
5104 else if (EASY_VECTOR_15_ADD_SELF (splat_val)
5105 && (splat_val >= 0 || (step == 1 && copies == 1)))
5108 /* Also check if are loading up the most significant bit which can be done by
5109 loading up -1 and shifting the value left by -1. */
5110 else if (EASY_VECTOR_MSB (splat_val, inner))
5113 else
5114 return false;
5116 /* Check if VAL is present in every STEP-th element, and the
5117 other elements are filled with its most significant bit. */
5118 for (i = 1; i < nunits; ++i)
5120 HOST_WIDE_INT desired_val;
5121 unsigned elt = BYTES_BIG_ENDIAN ? nunits - 1 - i : i;
5122 if ((i & (step - 1)) == 0)
5123 desired_val = val;
5124 else
5125 desired_val = msb_val;
5127 if (desired_val != const_vector_elt_as_int (op, elt))
5128 return false;
5131 return true;
5135 /* Return true if OP is of the given MODE and can be synthesized
5136 with a vspltisb, vspltish or vspltisw. */
5138 bool
5139 easy_altivec_constant (rtx op, enum machine_mode mode)
5141 unsigned step, copies;
5143 if (mode == VOIDmode)
5144 mode = GET_MODE (op);
5145 else if (mode != GET_MODE (op))
5146 return false;
5148 /* V2DI/V2DF was added with VSX. Only allow 0 and all 1's as easy
5149 constants. */
5150 if (mode == V2DFmode)
5151 return zero_constant (op, mode);
5153 else if (mode == V2DImode)
5155 if (GET_CODE (CONST_VECTOR_ELT (op, 0)) != CONST_INT
5156 || GET_CODE (CONST_VECTOR_ELT (op, 1)) != CONST_INT)
5157 return false;
5159 if (zero_constant (op, mode))
5160 return true;
5162 if (INTVAL (CONST_VECTOR_ELT (op, 0)) == -1
5163 && INTVAL (CONST_VECTOR_ELT (op, 1)) == -1)
5164 return true;
5166 return false;
5169 /* V1TImode is a special container for TImode. Ignore for now. */
5170 else if (mode == V1TImode)
5171 return false;
5173 /* Start with a vspltisw. */
5174 step = GET_MODE_NUNITS (mode) / 4;
5175 copies = 1;
5177 if (vspltis_constant (op, step, copies))
5178 return true;
5180 /* Then try with a vspltish. */
5181 if (step == 1)
5182 copies <<= 1;
5183 else
5184 step >>= 1;
5186 if (vspltis_constant (op, step, copies))
5187 return true;
5189 /* And finally a vspltisb. */
5190 if (step == 1)
5191 copies <<= 1;
5192 else
5193 step >>= 1;
5195 if (vspltis_constant (op, step, copies))
5196 return true;
5198 return false;
5201 /* Generate a VEC_DUPLICATE representing a vspltis[bhw] instruction whose
5202 result is OP. Abort if it is not possible. */
5205 gen_easy_altivec_constant (rtx op)
5207 enum machine_mode mode = GET_MODE (op);
5208 int nunits = GET_MODE_NUNITS (mode);
5209 rtx val = CONST_VECTOR_ELT (op, BYTES_BIG_ENDIAN ? nunits - 1 : 0);
5210 unsigned step = nunits / 4;
5211 unsigned copies = 1;
5213 /* Start with a vspltisw. */
5214 if (vspltis_constant (op, step, copies))
5215 return gen_rtx_VEC_DUPLICATE (V4SImode, gen_lowpart (SImode, val));
5217 /* Then try with a vspltish. */
5218 if (step == 1)
5219 copies <<= 1;
5220 else
5221 step >>= 1;
5223 if (vspltis_constant (op, step, copies))
5224 return gen_rtx_VEC_DUPLICATE (V8HImode, gen_lowpart (HImode, val));
5226 /* And finally a vspltisb. */
5227 if (step == 1)
5228 copies <<= 1;
5229 else
5230 step >>= 1;
5232 if (vspltis_constant (op, step, copies))
5233 return gen_rtx_VEC_DUPLICATE (V16QImode, gen_lowpart (QImode, val));
5235 gcc_unreachable ();
5238 const char *
5239 output_vec_const_move (rtx *operands)
5241 int cst, cst2;
5242 enum machine_mode mode;
5243 rtx dest, vec;
5245 dest = operands[0];
5246 vec = operands[1];
5247 mode = GET_MODE (dest);
5249 if (TARGET_VSX)
5251 if (zero_constant (vec, mode))
5252 return "xxlxor %x0,%x0,%x0";
5254 if ((mode == V2DImode || mode == V1TImode)
5255 && INTVAL (CONST_VECTOR_ELT (vec, 0)) == -1
5256 && INTVAL (CONST_VECTOR_ELT (vec, 1)) == -1)
5257 return "vspltisw %0,-1";
5260 if (TARGET_ALTIVEC)
5262 rtx splat_vec;
5263 if (zero_constant (vec, mode))
5264 return "vxor %0,%0,%0";
5266 splat_vec = gen_easy_altivec_constant (vec);
5267 gcc_assert (GET_CODE (splat_vec) == VEC_DUPLICATE);
5268 operands[1] = XEXP (splat_vec, 0);
5269 if (!EASY_VECTOR_15 (INTVAL (operands[1])))
5270 return "#";
5272 switch (GET_MODE (splat_vec))
5274 case V4SImode:
5275 return "vspltisw %0,%1";
5277 case V8HImode:
5278 return "vspltish %0,%1";
5280 case V16QImode:
5281 return "vspltisb %0,%1";
5283 default:
5284 gcc_unreachable ();
5288 gcc_assert (TARGET_SPE);
5290 /* Vector constant 0 is handled as a splitter of V2SI, and in the
5291 pattern of V1DI, V4HI, and V2SF.
5293 FIXME: We should probably return # and add post reload
5294 splitters for these, but this way is so easy ;-). */
5295 cst = INTVAL (CONST_VECTOR_ELT (vec, 0));
5296 cst2 = INTVAL (CONST_VECTOR_ELT (vec, 1));
5297 operands[1] = CONST_VECTOR_ELT (vec, 0);
5298 operands[2] = CONST_VECTOR_ELT (vec, 1);
5299 if (cst == cst2)
5300 return "li %0,%1\n\tevmergelo %0,%0,%0";
5301 else
5302 return "li %0,%1\n\tevmergelo %0,%0,%0\n\tli %0,%2";
5305 /* Initialize TARGET of vector PAIRED to VALS. */
5307 void
5308 paired_expand_vector_init (rtx target, rtx vals)
5310 enum machine_mode mode = GET_MODE (target);
5311 int n_elts = GET_MODE_NUNITS (mode);
5312 int n_var = 0;
5313 rtx x, new_rtx, tmp, constant_op, op1, op2;
5314 int i;
5316 for (i = 0; i < n_elts; ++i)
5318 x = XVECEXP (vals, 0, i);
5319 if (!CONSTANT_P (x))
5320 ++n_var;
5322 if (n_var == 0)
5324 /* Load from constant pool. */
5325 emit_move_insn (target, gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)));
5326 return;
5329 if (n_var == 2)
5331 /* The vector is initialized only with non-constants. */
5332 new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, XVECEXP (vals, 0, 0),
5333 XVECEXP (vals, 0, 1));
5335 emit_move_insn (target, new_rtx);
5336 return;
5339 /* One field is non-constant and the other one is a constant. Load the
5340 constant from the constant pool and use ps_merge instruction to
5341 construct the whole vector. */
5342 op1 = XVECEXP (vals, 0, 0);
5343 op2 = XVECEXP (vals, 0, 1);
5345 constant_op = (CONSTANT_P (op1)) ? op1 : op2;
5347 tmp = gen_reg_rtx (GET_MODE (constant_op));
5348 emit_move_insn (tmp, constant_op);
5350 if (CONSTANT_P (op1))
5351 new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, tmp, op2);
5352 else
5353 new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, op1, tmp);
5355 emit_move_insn (target, new_rtx);
5358 void
5359 paired_expand_vector_move (rtx operands[])
5361 rtx op0 = operands[0], op1 = operands[1];
5363 emit_move_insn (op0, op1);
5366 /* Emit vector compare for code RCODE. DEST is destination, OP1 and
5367 OP2 are two VEC_COND_EXPR operands, CC_OP0 and CC_OP1 are the two
5368 operands for the relation operation COND. This is a recursive
5369 function. */
5371 static void
5372 paired_emit_vector_compare (enum rtx_code rcode,
5373 rtx dest, rtx op0, rtx op1,
5374 rtx cc_op0, rtx cc_op1)
5376 rtx tmp = gen_reg_rtx (V2SFmode);
5377 rtx tmp1, max, min;
5379 gcc_assert (TARGET_PAIRED_FLOAT);
5380 gcc_assert (GET_MODE (op0) == GET_MODE (op1));
5382 switch (rcode)
5384 case LT:
5385 case LTU:
5386 paired_emit_vector_compare (GE, dest, op1, op0, cc_op0, cc_op1);
5387 return;
5388 case GE:
5389 case GEU:
5390 emit_insn (gen_subv2sf3 (tmp, cc_op0, cc_op1));
5391 emit_insn (gen_selv2sf4 (dest, tmp, op0, op1, CONST0_RTX (SFmode)));
5392 return;
5393 case LE:
5394 case LEU:
5395 paired_emit_vector_compare (GE, dest, op0, op1, cc_op1, cc_op0);
5396 return;
5397 case GT:
5398 paired_emit_vector_compare (LE, dest, op1, op0, cc_op0, cc_op1);
5399 return;
5400 case EQ:
5401 tmp1 = gen_reg_rtx (V2SFmode);
5402 max = gen_reg_rtx (V2SFmode);
5403 min = gen_reg_rtx (V2SFmode);
5404 gen_reg_rtx (V2SFmode);
5406 emit_insn (gen_subv2sf3 (tmp, cc_op0, cc_op1));
5407 emit_insn (gen_selv2sf4
5408 (max, tmp, cc_op0, cc_op1, CONST0_RTX (SFmode)));
5409 emit_insn (gen_subv2sf3 (tmp, cc_op1, cc_op0));
5410 emit_insn (gen_selv2sf4
5411 (min, tmp, cc_op0, cc_op1, CONST0_RTX (SFmode)));
5412 emit_insn (gen_subv2sf3 (tmp1, min, max));
5413 emit_insn (gen_selv2sf4 (dest, tmp1, op0, op1, CONST0_RTX (SFmode)));
5414 return;
5415 case NE:
5416 paired_emit_vector_compare (EQ, dest, op1, op0, cc_op0, cc_op1);
5417 return;
5418 case UNLE:
5419 paired_emit_vector_compare (LE, dest, op1, op0, cc_op0, cc_op1);
5420 return;
5421 case UNLT:
5422 paired_emit_vector_compare (LT, dest, op1, op0, cc_op0, cc_op1);
5423 return;
5424 case UNGE:
5425 paired_emit_vector_compare (GE, dest, op1, op0, cc_op0, cc_op1);
5426 return;
5427 case UNGT:
5428 paired_emit_vector_compare (GT, dest, op1, op0, cc_op0, cc_op1);
5429 return;
5430 default:
5431 gcc_unreachable ();
5434 return;
5437 /* Emit vector conditional expression.
5438 DEST is destination. OP1 and OP2 are two VEC_COND_EXPR operands.
5439 CC_OP0 and CC_OP1 are the two operands for the relation operation COND. */
5442 paired_emit_vector_cond_expr (rtx dest, rtx op1, rtx op2,
5443 rtx cond, rtx cc_op0, rtx cc_op1)
5445 enum rtx_code rcode = GET_CODE (cond);
5447 if (!TARGET_PAIRED_FLOAT)
5448 return 0;
5450 paired_emit_vector_compare (rcode, dest, op1, op2, cc_op0, cc_op1);
5452 return 1;
5455 /* Initialize vector TARGET to VALS. */
5457 void
5458 rs6000_expand_vector_init (rtx target, rtx vals)
5460 enum machine_mode mode = GET_MODE (target);
5461 enum machine_mode inner_mode = GET_MODE_INNER (mode);
5462 int n_elts = GET_MODE_NUNITS (mode);
5463 int n_var = 0, one_var = -1;
5464 bool all_same = true, all_const_zero = true;
5465 rtx x, mem;
5466 int i;
5468 for (i = 0; i < n_elts; ++i)
5470 x = XVECEXP (vals, 0, i);
5471 if (!CONSTANT_P (x))
5472 ++n_var, one_var = i;
5473 else if (x != CONST0_RTX (inner_mode))
5474 all_const_zero = false;
5476 if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
5477 all_same = false;
5480 if (n_var == 0)
5482 rtx const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0));
5483 bool int_vector_p = (GET_MODE_CLASS (mode) == MODE_VECTOR_INT);
5484 if ((int_vector_p || TARGET_VSX) && all_const_zero)
5486 /* Zero register. */
5487 emit_insn (gen_rtx_SET (VOIDmode, target,
5488 gen_rtx_XOR (mode, target, target)));
5489 return;
5491 else if (int_vector_p && easy_vector_constant (const_vec, mode))
5493 /* Splat immediate. */
5494 emit_insn (gen_rtx_SET (VOIDmode, target, const_vec));
5495 return;
5497 else
5499 /* Load from constant pool. */
5500 emit_move_insn (target, const_vec);
5501 return;
5505 /* Double word values on VSX can use xxpermdi or lxvdsx. */
5506 if (VECTOR_MEM_VSX_P (mode) && (mode == V2DFmode || mode == V2DImode))
5508 rtx op0 = XVECEXP (vals, 0, 0);
5509 rtx op1 = XVECEXP (vals, 0, 1);
5510 if (all_same)
5512 if (!MEM_P (op0) && !REG_P (op0))
5513 op0 = force_reg (inner_mode, op0);
5514 if (mode == V2DFmode)
5515 emit_insn (gen_vsx_splat_v2df (target, op0));
5516 else
5517 emit_insn (gen_vsx_splat_v2di (target, op0));
5519 else
5521 op0 = force_reg (inner_mode, op0);
5522 op1 = force_reg (inner_mode, op1);
5523 if (mode == V2DFmode)
5524 emit_insn (gen_vsx_concat_v2df (target, op0, op1));
5525 else
5526 emit_insn (gen_vsx_concat_v2di (target, op0, op1));
5528 return;
5531 /* With single precision floating point on VSX, know that internally single
5532 precision is actually represented as a double, and either make 2 V2DF
5533 vectors, and convert these vectors to single precision, or do one
5534 conversion, and splat the result to the other elements. */
5535 if (mode == V4SFmode && VECTOR_MEM_VSX_P (mode))
5537 if (all_same)
5539 rtx freg = gen_reg_rtx (V4SFmode);
5540 rtx sreg = force_reg (SFmode, XVECEXP (vals, 0, 0));
5541 rtx cvt = ((TARGET_XSCVDPSPN)
5542 ? gen_vsx_xscvdpspn_scalar (freg, sreg)
5543 : gen_vsx_xscvdpsp_scalar (freg, sreg));
5545 emit_insn (cvt);
5546 emit_insn (gen_vsx_xxspltw_v4sf_direct (target, freg, const0_rtx));
5548 else
5550 rtx dbl_even = gen_reg_rtx (V2DFmode);
5551 rtx dbl_odd = gen_reg_rtx (V2DFmode);
5552 rtx flt_even = gen_reg_rtx (V4SFmode);
5553 rtx flt_odd = gen_reg_rtx (V4SFmode);
5554 rtx op0 = force_reg (SFmode, XVECEXP (vals, 0, 0));
5555 rtx op1 = force_reg (SFmode, XVECEXP (vals, 0, 1));
5556 rtx op2 = force_reg (SFmode, XVECEXP (vals, 0, 2));
5557 rtx op3 = force_reg (SFmode, XVECEXP (vals, 0, 3));
5559 emit_insn (gen_vsx_concat_v2sf (dbl_even, op0, op1));
5560 emit_insn (gen_vsx_concat_v2sf (dbl_odd, op2, op3));
5561 emit_insn (gen_vsx_xvcvdpsp (flt_even, dbl_even));
5562 emit_insn (gen_vsx_xvcvdpsp (flt_odd, dbl_odd));
5563 rs6000_expand_extract_even (target, flt_even, flt_odd);
5565 return;
5568 /* Store value to stack temp. Load vector element. Splat. However, splat
5569 of 64-bit items is not supported on Altivec. */
5570 if (all_same && GET_MODE_SIZE (inner_mode) <= 4)
5572 mem = assign_stack_temp (mode, GET_MODE_SIZE (inner_mode));
5573 emit_move_insn (adjust_address_nv (mem, inner_mode, 0),
5574 XVECEXP (vals, 0, 0));
5575 x = gen_rtx_UNSPEC (VOIDmode,
5576 gen_rtvec (1, const0_rtx), UNSPEC_LVE);
5577 emit_insn (gen_rtx_PARALLEL (VOIDmode,
5578 gen_rtvec (2,
5579 gen_rtx_SET (VOIDmode,
5580 target, mem),
5581 x)));
5582 x = gen_rtx_VEC_SELECT (inner_mode, target,
5583 gen_rtx_PARALLEL (VOIDmode,
5584 gen_rtvec (1, const0_rtx)));
5585 emit_insn (gen_rtx_SET (VOIDmode, target,
5586 gen_rtx_VEC_DUPLICATE (mode, x)));
5587 return;
5590 /* One field is non-constant. Load constant then overwrite
5591 varying field. */
5592 if (n_var == 1)
5594 rtx copy = copy_rtx (vals);
5596 /* Load constant part of vector, substitute neighboring value for
5597 varying element. */
5598 XVECEXP (copy, 0, one_var) = XVECEXP (vals, 0, (one_var + 1) % n_elts);
5599 rs6000_expand_vector_init (target, copy);
5601 /* Insert variable. */
5602 rs6000_expand_vector_set (target, XVECEXP (vals, 0, one_var), one_var);
5603 return;
5606 /* Construct the vector in memory one field at a time
5607 and load the whole vector. */
5608 mem = assign_stack_temp (mode, GET_MODE_SIZE (mode));
5609 for (i = 0; i < n_elts; i++)
5610 emit_move_insn (adjust_address_nv (mem, inner_mode,
5611 i * GET_MODE_SIZE (inner_mode)),
5612 XVECEXP (vals, 0, i));
5613 emit_move_insn (target, mem);
5616 /* Set field ELT of TARGET to VAL. */
5618 void
5619 rs6000_expand_vector_set (rtx target, rtx val, int elt)
5621 enum machine_mode mode = GET_MODE (target);
5622 enum machine_mode inner_mode = GET_MODE_INNER (mode);
5623 rtx reg = gen_reg_rtx (mode);
5624 rtx mask, mem, x;
5625 int width = GET_MODE_SIZE (inner_mode);
5626 int i;
5628 if (VECTOR_MEM_VSX_P (mode) && (mode == V2DFmode || mode == V2DImode))
5630 rtx (*set_func) (rtx, rtx, rtx, rtx)
5631 = ((mode == V2DFmode) ? gen_vsx_set_v2df : gen_vsx_set_v2di);
5632 emit_insn (set_func (target, target, val, GEN_INT (elt)));
5633 return;
5636 /* Simplify setting single element vectors like V1TImode. */
5637 if (GET_MODE_SIZE (mode) == GET_MODE_SIZE (inner_mode) && elt == 0)
5639 emit_move_insn (target, gen_lowpart (mode, val));
5640 return;
5643 /* Load single variable value. */
5644 mem = assign_stack_temp (mode, GET_MODE_SIZE (inner_mode));
5645 emit_move_insn (adjust_address_nv (mem, inner_mode, 0), val);
5646 x = gen_rtx_UNSPEC (VOIDmode,
5647 gen_rtvec (1, const0_rtx), UNSPEC_LVE);
5648 emit_insn (gen_rtx_PARALLEL (VOIDmode,
5649 gen_rtvec (2,
5650 gen_rtx_SET (VOIDmode,
5651 reg, mem),
5652 x)));
5654 /* Linear sequence. */
5655 mask = gen_rtx_PARALLEL (V16QImode, rtvec_alloc (16));
5656 for (i = 0; i < 16; ++i)
5657 XVECEXP (mask, 0, i) = GEN_INT (i);
5659 /* Set permute mask to insert element into target. */
5660 for (i = 0; i < width; ++i)
5661 XVECEXP (mask, 0, elt*width + i)
5662 = GEN_INT (i + 0x10);
5663 x = gen_rtx_CONST_VECTOR (V16QImode, XVEC (mask, 0));
5665 if (BYTES_BIG_ENDIAN)
5666 x = gen_rtx_UNSPEC (mode,
5667 gen_rtvec (3, target, reg,
5668 force_reg (V16QImode, x)),
5669 UNSPEC_VPERM);
5670 else
5672 /* Invert selector. We prefer to generate VNAND on P8 so
5673 that future fusion opportunities can kick in, but must
5674 generate VNOR elsewhere. */
5675 rtx notx = gen_rtx_NOT (V16QImode, force_reg (V16QImode, x));
5676 rtx iorx = (TARGET_P8_VECTOR
5677 ? gen_rtx_IOR (V16QImode, notx, notx)
5678 : gen_rtx_AND (V16QImode, notx, notx));
5679 rtx tmp = gen_reg_rtx (V16QImode);
5680 emit_insn (gen_rtx_SET (VOIDmode, tmp, iorx));
5682 /* Permute with operands reversed and adjusted selector. */
5683 x = gen_rtx_UNSPEC (mode, gen_rtvec (3, reg, target, tmp),
5684 UNSPEC_VPERM);
5687 emit_insn (gen_rtx_SET (VOIDmode, target, x));
5690 /* Extract field ELT from VEC into TARGET. */
5692 void
5693 rs6000_expand_vector_extract (rtx target, rtx vec, int elt)
5695 enum machine_mode mode = GET_MODE (vec);
5696 enum machine_mode inner_mode = GET_MODE_INNER (mode);
5697 rtx mem;
5699 if (VECTOR_MEM_VSX_P (mode))
5701 switch (mode)
5703 default:
5704 break;
5705 case V1TImode:
5706 gcc_assert (elt == 0 && inner_mode == TImode);
5707 emit_move_insn (target, gen_lowpart (TImode, vec));
5708 break;
5709 case V2DFmode:
5710 emit_insn (gen_vsx_extract_v2df (target, vec, GEN_INT (elt)));
5711 return;
5712 case V2DImode:
5713 emit_insn (gen_vsx_extract_v2di (target, vec, GEN_INT (elt)));
5714 return;
5715 case V4SFmode:
5716 emit_insn (gen_vsx_extract_v4sf (target, vec, GEN_INT (elt)));
5717 return;
5721 /* Allocate mode-sized buffer. */
5722 mem = assign_stack_temp (mode, GET_MODE_SIZE (mode));
5724 emit_move_insn (mem, vec);
5726 /* Add offset to field within buffer matching vector element. */
5727 mem = adjust_address_nv (mem, inner_mode, elt * GET_MODE_SIZE (inner_mode));
5729 emit_move_insn (target, adjust_address_nv (mem, inner_mode, 0));
5732 /* Generates shifts and masks for a pair of rldicl or rldicr insns to
5733 implement ANDing by the mask IN. */
5734 void
5735 build_mask64_2_operands (rtx in, rtx *out)
5737 unsigned HOST_WIDE_INT c, lsb, m1, m2;
5738 int shift;
5740 gcc_assert (GET_CODE (in) == CONST_INT);
5742 c = INTVAL (in);
5743 if (c & 1)
5745 /* Assume c initially something like 0x00fff000000fffff. The idea
5746 is to rotate the word so that the middle ^^^^^^ group of zeros
5747 is at the MS end and can be cleared with an rldicl mask. We then
5748 rotate back and clear off the MS ^^ group of zeros with a
5749 second rldicl. */
5750 c = ~c; /* c == 0xff000ffffff00000 */
5751 lsb = c & -c; /* lsb == 0x0000000000100000 */
5752 m1 = -lsb; /* m1 == 0xfffffffffff00000 */
5753 c = ~c; /* c == 0x00fff000000fffff */
5754 c &= -lsb; /* c == 0x00fff00000000000 */
5755 lsb = c & -c; /* lsb == 0x0000100000000000 */
5756 c = ~c; /* c == 0xff000fffffffffff */
5757 c &= -lsb; /* c == 0xff00000000000000 */
5758 shift = 0;
5759 while ((lsb >>= 1) != 0)
5760 shift++; /* shift == 44 on exit from loop */
5761 m1 <<= 64 - shift; /* m1 == 0xffffff0000000000 */
5762 m1 = ~m1; /* m1 == 0x000000ffffffffff */
5763 m2 = ~c; /* m2 == 0x00ffffffffffffff */
5765 else
5767 /* Assume c initially something like 0xff000f0000000000. The idea
5768 is to rotate the word so that the ^^^ middle group of zeros
5769 is at the LS end and can be cleared with an rldicr mask. We then
5770 rotate back and clear off the LS group of ^^^^^^^^^^ zeros with
5771 a second rldicr. */
5772 lsb = c & -c; /* lsb == 0x0000010000000000 */
5773 m2 = -lsb; /* m2 == 0xffffff0000000000 */
5774 c = ~c; /* c == 0x00fff0ffffffffff */
5775 c &= -lsb; /* c == 0x00fff00000000000 */
5776 lsb = c & -c; /* lsb == 0x0000100000000000 */
5777 c = ~c; /* c == 0xff000fffffffffff */
5778 c &= -lsb; /* c == 0xff00000000000000 */
5779 shift = 0;
5780 while ((lsb >>= 1) != 0)
5781 shift++; /* shift == 44 on exit from loop */
5782 m1 = ~c; /* m1 == 0x00ffffffffffffff */
5783 m1 >>= shift; /* m1 == 0x0000000000000fff */
5784 m1 = ~m1; /* m1 == 0xfffffffffffff000 */
5787 /* Note that when we only have two 0->1 and 1->0 transitions, one of the
5788 masks will be all 1's. We are guaranteed more than one transition. */
5789 out[0] = GEN_INT (64 - shift);
5790 out[1] = GEN_INT (m1);
5791 out[2] = GEN_INT (shift);
5792 out[3] = GEN_INT (m2);
5795 /* Return TRUE if OP is an invalid SUBREG operation on the e500. */
5797 bool
5798 invalid_e500_subreg (rtx op, enum machine_mode mode)
5800 if (TARGET_E500_DOUBLE)
5802 /* Reject (subreg:SI (reg:DF)); likewise with subreg:DI or
5803 subreg:TI and reg:TF. Decimal float modes are like integer
5804 modes (only low part of each register used) for this
5805 purpose. */
5806 if (GET_CODE (op) == SUBREG
5807 && (mode == SImode || mode == DImode || mode == TImode
5808 || mode == DDmode || mode == TDmode || mode == PTImode)
5809 && REG_P (SUBREG_REG (op))
5810 && (GET_MODE (SUBREG_REG (op)) == DFmode
5811 || GET_MODE (SUBREG_REG (op)) == TFmode))
5812 return true;
5814 /* Reject (subreg:DF (reg:DI)); likewise with subreg:TF and
5815 reg:TI. */
5816 if (GET_CODE (op) == SUBREG
5817 && (mode == DFmode || mode == TFmode)
5818 && REG_P (SUBREG_REG (op))
5819 && (GET_MODE (SUBREG_REG (op)) == DImode
5820 || GET_MODE (SUBREG_REG (op)) == TImode
5821 || GET_MODE (SUBREG_REG (op)) == PTImode
5822 || GET_MODE (SUBREG_REG (op)) == DDmode
5823 || GET_MODE (SUBREG_REG (op)) == TDmode))
5824 return true;
5827 if (TARGET_SPE
5828 && GET_CODE (op) == SUBREG
5829 && mode == SImode
5830 && REG_P (SUBREG_REG (op))
5831 && SPE_VECTOR_MODE (GET_MODE (SUBREG_REG (op))))
5832 return true;
5834 return false;
5837 /* Return alignment of TYPE. Existing alignment is ALIGN. HOW
5838 selects whether the alignment is abi mandated, optional, or
5839 both abi and optional alignment. */
5841 unsigned int
5842 rs6000_data_alignment (tree type, unsigned int align, enum data_align how)
5844 if (how != align_opt)
5846 if (TREE_CODE (type) == VECTOR_TYPE)
5848 if ((TARGET_SPE && SPE_VECTOR_MODE (TYPE_MODE (type)))
5849 || (TARGET_PAIRED_FLOAT && PAIRED_VECTOR_MODE (TYPE_MODE (type))))
5851 if (align < 64)
5852 align = 64;
5854 else if (align < 128)
5855 align = 128;
5857 else if (TARGET_E500_DOUBLE
5858 && TREE_CODE (type) == REAL_TYPE
5859 && TYPE_MODE (type) == DFmode)
5861 if (align < 64)
5862 align = 64;
5866 if (how != align_abi)
5868 if (TREE_CODE (type) == ARRAY_TYPE
5869 && TYPE_MODE (TREE_TYPE (type)) == QImode)
5871 if (align < BITS_PER_WORD)
5872 align = BITS_PER_WORD;
5876 return align;
5879 /* AIX increases natural record alignment to doubleword if the first
5880 field is an FP double while the FP fields remain word aligned. */
5882 unsigned int
5883 rs6000_special_round_type_align (tree type, unsigned int computed,
5884 unsigned int specified)
5886 unsigned int align = MAX (computed, specified);
5887 tree field = TYPE_FIELDS (type);
5889 /* Skip all non field decls */
5890 while (field != NULL && TREE_CODE (field) != FIELD_DECL)
5891 field = DECL_CHAIN (field);
5893 if (field != NULL && field != type)
5895 type = TREE_TYPE (field);
5896 while (TREE_CODE (type) == ARRAY_TYPE)
5897 type = TREE_TYPE (type);
5899 if (type != error_mark_node && TYPE_MODE (type) == DFmode)
5900 align = MAX (align, 64);
5903 return align;
5906 /* Darwin increases record alignment to the natural alignment of
5907 the first field. */
5909 unsigned int
5910 darwin_rs6000_special_round_type_align (tree type, unsigned int computed,
5911 unsigned int specified)
5913 unsigned int align = MAX (computed, specified);
5915 if (TYPE_PACKED (type))
5916 return align;
5918 /* Find the first field, looking down into aggregates. */
5919 do {
5920 tree field = TYPE_FIELDS (type);
5921 /* Skip all non field decls */
5922 while (field != NULL && TREE_CODE (field) != FIELD_DECL)
5923 field = DECL_CHAIN (field);
5924 if (! field)
5925 break;
5926 /* A packed field does not contribute any extra alignment. */
5927 if (DECL_PACKED (field))
5928 return align;
5929 type = TREE_TYPE (field);
5930 while (TREE_CODE (type) == ARRAY_TYPE)
5931 type = TREE_TYPE (type);
5932 } while (AGGREGATE_TYPE_P (type));
5934 if (! AGGREGATE_TYPE_P (type) && type != error_mark_node)
5935 align = MAX (align, TYPE_ALIGN (type));
5937 return align;
5940 /* Return 1 for an operand in small memory on V.4/eabi. */
5943 small_data_operand (rtx op ATTRIBUTE_UNUSED,
5944 enum machine_mode mode ATTRIBUTE_UNUSED)
5946 #if TARGET_ELF
5947 rtx sym_ref;
5949 if (rs6000_sdata == SDATA_NONE || rs6000_sdata == SDATA_DATA)
5950 return 0;
5952 if (DEFAULT_ABI != ABI_V4)
5953 return 0;
5955 /* Vector and float memory instructions have a limited offset on the
5956 SPE, so using a vector or float variable directly as an operand is
5957 not useful. */
5958 if (TARGET_SPE
5959 && (SPE_VECTOR_MODE (mode) || FLOAT_MODE_P (mode)))
5960 return 0;
5962 if (GET_CODE (op) == SYMBOL_REF)
5963 sym_ref = op;
5965 else if (GET_CODE (op) != CONST
5966 || GET_CODE (XEXP (op, 0)) != PLUS
5967 || GET_CODE (XEXP (XEXP (op, 0), 0)) != SYMBOL_REF
5968 || GET_CODE (XEXP (XEXP (op, 0), 1)) != CONST_INT)
5969 return 0;
5971 else
5973 rtx sum = XEXP (op, 0);
5974 HOST_WIDE_INT summand;
5976 /* We have to be careful here, because it is the referenced address
5977 that must be 32k from _SDA_BASE_, not just the symbol. */
5978 summand = INTVAL (XEXP (sum, 1));
5979 if (summand < 0 || summand > g_switch_value)
5980 return 0;
5982 sym_ref = XEXP (sum, 0);
5985 return SYMBOL_REF_SMALL_P (sym_ref);
5986 #else
5987 return 0;
5988 #endif
5991 /* Return true if either operand is a general purpose register. */
5993 bool
5994 gpr_or_gpr_p (rtx op0, rtx op1)
5996 return ((REG_P (op0) && INT_REGNO_P (REGNO (op0)))
5997 || (REG_P (op1) && INT_REGNO_P (REGNO (op1))));
6000 /* Return true if this is a move direct operation between GPR registers and
6001 floating point/VSX registers. */
6003 bool
6004 direct_move_p (rtx op0, rtx op1)
6006 int regno0, regno1;
6008 if (!REG_P (op0) || !REG_P (op1))
6009 return false;
6011 if (!TARGET_DIRECT_MOVE && !TARGET_MFPGPR)
6012 return false;
6014 regno0 = REGNO (op0);
6015 regno1 = REGNO (op1);
6016 if (regno0 >= FIRST_PSEUDO_REGISTER || regno1 >= FIRST_PSEUDO_REGISTER)
6017 return false;
6019 if (INT_REGNO_P (regno0))
6020 return (TARGET_DIRECT_MOVE) ? VSX_REGNO_P (regno1) : FP_REGNO_P (regno1);
6022 else if (INT_REGNO_P (regno1))
6024 if (TARGET_MFPGPR && FP_REGNO_P (regno0))
6025 return true;
6027 else if (TARGET_DIRECT_MOVE && VSX_REGNO_P (regno0))
6028 return true;
6031 return false;
6034 /* Return true if this is a load or store quad operation. This function does
6035 not handle the atomic quad memory instructions. */
6037 bool
6038 quad_load_store_p (rtx op0, rtx op1)
6040 bool ret;
6042 if (!TARGET_QUAD_MEMORY)
6043 ret = false;
6045 else if (REG_P (op0) && MEM_P (op1))
6046 ret = (quad_int_reg_operand (op0, GET_MODE (op0))
6047 && quad_memory_operand (op1, GET_MODE (op1))
6048 && !reg_overlap_mentioned_p (op0, op1));
6050 else if (MEM_P (op0) && REG_P (op1))
6051 ret = (quad_memory_operand (op0, GET_MODE (op0))
6052 && quad_int_reg_operand (op1, GET_MODE (op1)));
6054 else
6055 ret = false;
6057 if (TARGET_DEBUG_ADDR)
6059 fprintf (stderr, "\n========== quad_load_store, return %s\n",
6060 ret ? "true" : "false");
6061 debug_rtx (gen_rtx_SET (VOIDmode, op0, op1));
6064 return ret;
6067 /* Given an address, return a constant offset term if one exists. */
6069 static rtx
6070 address_offset (rtx op)
6072 if (GET_CODE (op) == PRE_INC
6073 || GET_CODE (op) == PRE_DEC)
6074 op = XEXP (op, 0);
6075 else if (GET_CODE (op) == PRE_MODIFY
6076 || GET_CODE (op) == LO_SUM)
6077 op = XEXP (op, 1);
6079 if (GET_CODE (op) == CONST)
6080 op = XEXP (op, 0);
6082 if (GET_CODE (op) == PLUS)
6083 op = XEXP (op, 1);
6085 if (CONST_INT_P (op))
6086 return op;
6088 return NULL_RTX;
6091 /* Return true if the MEM operand is a memory operand suitable for use
6092 with a (full width, possibly multiple) gpr load/store. On
6093 powerpc64 this means the offset must be divisible by 4.
6094 Implements 'Y' constraint.
6096 Accept direct, indexed, offset, lo_sum and tocref. Since this is
6097 a constraint function we know the operand has satisfied a suitable
6098 memory predicate. Also accept some odd rtl generated by reload
6099 (see rs6000_legitimize_reload_address for various forms). It is
6100 important that reload rtl be accepted by appropriate constraints
6101 but not by the operand predicate.
6103 Offsetting a lo_sum should not be allowed, except where we know by
6104 alignment that a 32k boundary is not crossed, but see the ???
6105 comment in rs6000_legitimize_reload_address. Note that by
6106 "offsetting" here we mean a further offset to access parts of the
6107 MEM. It's fine to have a lo_sum where the inner address is offset
6108 from a sym, since the same sym+offset will appear in the high part
6109 of the address calculation. */
6111 bool
6112 mem_operand_gpr (rtx op, enum machine_mode mode)
6114 unsigned HOST_WIDE_INT offset;
6115 int extra;
6116 rtx addr = XEXP (op, 0);
6118 op = address_offset (addr);
6119 if (op == NULL_RTX)
6120 return true;
6122 offset = INTVAL (op);
6123 if (TARGET_POWERPC64 && (offset & 3) != 0)
6124 return false;
6126 extra = GET_MODE_SIZE (mode) - UNITS_PER_WORD;
6127 gcc_assert (extra >= 0);
6129 if (GET_CODE (addr) == LO_SUM)
6130 /* For lo_sum addresses, we must allow any offset except one that
6131 causes a wrap, so test only the low 16 bits. */
6132 offset = ((offset & 0xffff) ^ 0x8000) - 0x8000;
6134 return offset + 0x8000 < 0x10000u - extra;
6137 /* Subroutines of rs6000_legitimize_address and rs6000_legitimate_address_p. */
6139 static bool
6140 reg_offset_addressing_ok_p (enum machine_mode mode)
6142 switch (mode)
6144 case V16QImode:
6145 case V8HImode:
6146 case V4SFmode:
6147 case V4SImode:
6148 case V2DFmode:
6149 case V2DImode:
6150 case V1TImode:
6151 case TImode:
6152 /* AltiVec/VSX vector modes. Only reg+reg addressing is valid. While
6153 TImode is not a vector mode, if we want to use the VSX registers to
6154 move it around, we need to restrict ourselves to reg+reg
6155 addressing. */
6156 if (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode))
6157 return false;
6158 break;
6160 case V4HImode:
6161 case V2SImode:
6162 case V1DImode:
6163 case V2SFmode:
6164 /* Paired vector modes. Only reg+reg addressing is valid. */
6165 if (TARGET_PAIRED_FLOAT)
6166 return false;
6167 break;
6169 case SDmode:
6170 /* If we can do direct load/stores of SDmode, restrict it to reg+reg
6171 addressing for the LFIWZX and STFIWX instructions. */
6172 if (TARGET_NO_SDMODE_STACK)
6173 return false;
6174 break;
6176 default:
6177 break;
6180 return true;
6183 static bool
6184 virtual_stack_registers_memory_p (rtx op)
6186 int regnum;
6188 if (GET_CODE (op) == REG)
6189 regnum = REGNO (op);
6191 else if (GET_CODE (op) == PLUS
6192 && GET_CODE (XEXP (op, 0)) == REG
6193 && GET_CODE (XEXP (op, 1)) == CONST_INT)
6194 regnum = REGNO (XEXP (op, 0));
6196 else
6197 return false;
6199 return (regnum >= FIRST_VIRTUAL_REGISTER
6200 && regnum <= LAST_VIRTUAL_POINTER_REGISTER);
6203 /* Return true if a MODE sized memory accesses to OP plus OFFSET
6204 is known to not straddle a 32k boundary. */
6206 static bool
6207 offsettable_ok_by_alignment (rtx op, HOST_WIDE_INT offset,
6208 enum machine_mode mode)
6210 tree decl, type;
6211 unsigned HOST_WIDE_INT dsize, dalign, lsb, mask;
6213 if (GET_CODE (op) != SYMBOL_REF)
6214 return false;
6216 dsize = GET_MODE_SIZE (mode);
6217 decl = SYMBOL_REF_DECL (op);
6218 if (!decl)
6220 if (dsize == 0)
6221 return false;
6223 /* -fsection-anchors loses the original SYMBOL_REF_DECL when
6224 replacing memory addresses with an anchor plus offset. We
6225 could find the decl by rummaging around in the block->objects
6226 VEC for the given offset but that seems like too much work. */
6227 dalign = BITS_PER_UNIT;
6228 if (SYMBOL_REF_HAS_BLOCK_INFO_P (op)
6229 && SYMBOL_REF_ANCHOR_P (op)
6230 && SYMBOL_REF_BLOCK (op) != NULL)
6232 struct object_block *block = SYMBOL_REF_BLOCK (op);
6234 dalign = block->alignment;
6235 offset += SYMBOL_REF_BLOCK_OFFSET (op);
6237 else if (CONSTANT_POOL_ADDRESS_P (op))
6239 /* It would be nice to have get_pool_align().. */
6240 enum machine_mode cmode = get_pool_mode (op);
6242 dalign = GET_MODE_ALIGNMENT (cmode);
6245 else if (DECL_P (decl))
6247 dalign = DECL_ALIGN (decl);
6249 if (dsize == 0)
6251 /* Allow BLKmode when the entire object is known to not
6252 cross a 32k boundary. */
6253 if (!DECL_SIZE_UNIT (decl))
6254 return false;
6256 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (decl)))
6257 return false;
6259 dsize = tree_to_uhwi (DECL_SIZE_UNIT (decl));
6260 if (dsize > 32768)
6261 return false;
6263 return dalign / BITS_PER_UNIT >= dsize;
6266 else
6268 type = TREE_TYPE (decl);
6270 dalign = TYPE_ALIGN (type);
6271 if (CONSTANT_CLASS_P (decl))
6272 dalign = CONSTANT_ALIGNMENT (decl, dalign);
6273 else
6274 dalign = DATA_ALIGNMENT (decl, dalign);
6276 if (dsize == 0)
6278 /* BLKmode, check the entire object. */
6279 if (TREE_CODE (decl) == STRING_CST)
6280 dsize = TREE_STRING_LENGTH (decl);
6281 else if (TYPE_SIZE_UNIT (type)
6282 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
6283 dsize = tree_to_uhwi (TYPE_SIZE_UNIT (type));
6284 else
6285 return false;
6286 if (dsize > 32768)
6287 return false;
6289 return dalign / BITS_PER_UNIT >= dsize;
6293 /* Find how many bits of the alignment we know for this access. */
6294 mask = dalign / BITS_PER_UNIT - 1;
6295 lsb = offset & -offset;
6296 mask &= lsb - 1;
6297 dalign = mask + 1;
6299 return dalign >= dsize;
6302 static bool
6303 constant_pool_expr_p (rtx op)
6305 rtx base, offset;
6307 split_const (op, &base, &offset);
6308 return (GET_CODE (base) == SYMBOL_REF
6309 && CONSTANT_POOL_ADDRESS_P (base)
6310 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (base), Pmode));
6313 static const_rtx tocrel_base, tocrel_offset;
6315 /* Return true if OP is a toc pointer relative address (the output
6316 of create_TOC_reference). If STRICT, do not match high part or
6317 non-split -mcmodel=large/medium toc pointer relative addresses. */
6319 bool
6320 toc_relative_expr_p (const_rtx op, bool strict)
6322 if (!TARGET_TOC)
6323 return false;
6325 if (TARGET_CMODEL != CMODEL_SMALL)
6327 /* Only match the low part. */
6328 if (GET_CODE (op) == LO_SUM
6329 && REG_P (XEXP (op, 0))
6330 && INT_REG_OK_FOR_BASE_P (XEXP (op, 0), strict))
6331 op = XEXP (op, 1);
6332 else if (strict)
6333 return false;
6336 tocrel_base = op;
6337 tocrel_offset = const0_rtx;
6338 if (GET_CODE (op) == PLUS && add_cint_operand (XEXP (op, 1), GET_MODE (op)))
6340 tocrel_base = XEXP (op, 0);
6341 tocrel_offset = XEXP (op, 1);
6344 return (GET_CODE (tocrel_base) == UNSPEC
6345 && XINT (tocrel_base, 1) == UNSPEC_TOCREL);
6348 /* Return true if X is a constant pool address, and also for cmodel=medium
6349 if X is a toc-relative address known to be offsettable within MODE. */
6351 bool
6352 legitimate_constant_pool_address_p (const_rtx x, enum machine_mode mode,
6353 bool strict)
6355 return (toc_relative_expr_p (x, strict)
6356 && (TARGET_CMODEL != CMODEL_MEDIUM
6357 || constant_pool_expr_p (XVECEXP (tocrel_base, 0, 0))
6358 || mode == QImode
6359 || offsettable_ok_by_alignment (XVECEXP (tocrel_base, 0, 0),
6360 INTVAL (tocrel_offset), mode)));
6363 static bool
6364 legitimate_small_data_p (enum machine_mode mode, rtx x)
6366 return (DEFAULT_ABI == ABI_V4
6367 && !flag_pic && !TARGET_TOC
6368 && (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST)
6369 && small_data_operand (x, mode));
6372 /* SPE offset addressing is limited to 5-bits worth of double words. */
6373 #define SPE_CONST_OFFSET_OK(x) (((x) & ~0xf8) == 0)
6375 bool
6376 rs6000_legitimate_offset_address_p (enum machine_mode mode, rtx x,
6377 bool strict, bool worst_case)
6379 unsigned HOST_WIDE_INT offset;
6380 unsigned int extra;
6382 if (GET_CODE (x) != PLUS)
6383 return false;
6384 if (!REG_P (XEXP (x, 0)))
6385 return false;
6386 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
6387 return false;
6388 if (!reg_offset_addressing_ok_p (mode))
6389 return virtual_stack_registers_memory_p (x);
6390 if (legitimate_constant_pool_address_p (x, mode, strict || lra_in_progress))
6391 return true;
6392 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6393 return false;
6395 offset = INTVAL (XEXP (x, 1));
6396 extra = 0;
6397 switch (mode)
6399 case V4HImode:
6400 case V2SImode:
6401 case V1DImode:
6402 case V2SFmode:
6403 /* SPE vector modes. */
6404 return SPE_CONST_OFFSET_OK (offset);
6406 case DFmode:
6407 case DDmode:
6408 case DImode:
6409 /* On e500v2, we may have:
6411 (subreg:DF (mem:DI (plus (reg) (const_int))) 0).
6413 Which gets addressed with evldd instructions. */
6414 if (TARGET_E500_DOUBLE)
6415 return SPE_CONST_OFFSET_OK (offset);
6417 /* If we are using VSX scalar loads, restrict ourselves to reg+reg
6418 addressing. */
6419 if (VECTOR_MEM_VSX_P (mode))
6420 return false;
6422 if (!worst_case)
6423 break;
6424 if (!TARGET_POWERPC64)
6425 extra = 4;
6426 else if (offset & 3)
6427 return false;
6428 break;
6430 case TFmode:
6431 if (TARGET_E500_DOUBLE)
6432 return (SPE_CONST_OFFSET_OK (offset)
6433 && SPE_CONST_OFFSET_OK (offset + 8));
6434 /* fall through */
6436 case TDmode:
6437 case TImode:
6438 case PTImode:
6439 extra = 8;
6440 if (!worst_case)
6441 break;
6442 if (!TARGET_POWERPC64)
6443 extra = 12;
6444 else if (offset & 3)
6445 return false;
6446 break;
6448 default:
6449 break;
6452 offset += 0x8000;
6453 return offset < 0x10000 - extra;
6456 bool
6457 legitimate_indexed_address_p (rtx x, int strict)
6459 rtx op0, op1;
6461 if (GET_CODE (x) != PLUS)
6462 return false;
6464 op0 = XEXP (x, 0);
6465 op1 = XEXP (x, 1);
6467 /* Recognize the rtl generated by reload which we know will later be
6468 replaced with proper base and index regs. */
6469 if (!strict
6470 && reload_in_progress
6471 && (REG_P (op0) || GET_CODE (op0) == PLUS)
6472 && REG_P (op1))
6473 return true;
6475 return (REG_P (op0) && REG_P (op1)
6476 && ((INT_REG_OK_FOR_BASE_P (op0, strict)
6477 && INT_REG_OK_FOR_INDEX_P (op1, strict))
6478 || (INT_REG_OK_FOR_BASE_P (op1, strict)
6479 && INT_REG_OK_FOR_INDEX_P (op0, strict))));
6482 bool
6483 avoiding_indexed_address_p (enum machine_mode mode)
6485 /* Avoid indexed addressing for modes that have non-indexed
6486 load/store instruction forms. */
6487 return (TARGET_AVOID_XFORM && VECTOR_MEM_NONE_P (mode));
6490 bool
6491 legitimate_indirect_address_p (rtx x, int strict)
6493 return GET_CODE (x) == REG && INT_REG_OK_FOR_BASE_P (x, strict);
6496 bool
6497 macho_lo_sum_memory_operand (rtx x, enum machine_mode mode)
6499 if (!TARGET_MACHO || !flag_pic
6500 || mode != SImode || GET_CODE (x) != MEM)
6501 return false;
6502 x = XEXP (x, 0);
6504 if (GET_CODE (x) != LO_SUM)
6505 return false;
6506 if (GET_CODE (XEXP (x, 0)) != REG)
6507 return false;
6508 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), 0))
6509 return false;
6510 x = XEXP (x, 1);
6512 return CONSTANT_P (x);
6515 static bool
6516 legitimate_lo_sum_address_p (enum machine_mode mode, rtx x, int strict)
6518 if (GET_CODE (x) != LO_SUM)
6519 return false;
6520 if (GET_CODE (XEXP (x, 0)) != REG)
6521 return false;
6522 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
6523 return false;
6524 /* Restrict addressing for DI because of our SUBREG hackery. */
6525 if (TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
6526 return false;
6527 x = XEXP (x, 1);
6529 if (TARGET_ELF || TARGET_MACHO)
6531 bool large_toc_ok;
6533 if (DEFAULT_ABI == ABI_V4 && flag_pic)
6534 return false;
6535 /* LRA don't use LEGITIMIZE_RELOAD_ADDRESS as it usually calls
6536 push_reload from reload pass code. LEGITIMIZE_RELOAD_ADDRESS
6537 recognizes some LO_SUM addresses as valid although this
6538 function says opposite. In most cases, LRA through different
6539 transformations can generate correct code for address reloads.
6540 It can not manage only some LO_SUM cases. So we need to add
6541 code analogous to one in rs6000_legitimize_reload_address for
6542 LOW_SUM here saying that some addresses are still valid. */
6543 large_toc_ok = (lra_in_progress && TARGET_CMODEL != CMODEL_SMALL
6544 && small_toc_ref (x, VOIDmode));
6545 if (TARGET_TOC && ! large_toc_ok)
6546 return false;
6547 if (GET_MODE_NUNITS (mode) != 1)
6548 return false;
6549 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
6550 && !(/* ??? Assume floating point reg based on mode? */
6551 TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT
6552 && (mode == DFmode || mode == DDmode)))
6553 return false;
6555 return CONSTANT_P (x) || large_toc_ok;
6558 return false;
6562 /* Try machine-dependent ways of modifying an illegitimate address
6563 to be legitimate. If we find one, return the new, valid address.
6564 This is used from only one place: `memory_address' in explow.c.
6566 OLDX is the address as it was before break_out_memory_refs was
6567 called. In some cases it is useful to look at this to decide what
6568 needs to be done.
6570 It is always safe for this function to do nothing. It exists to
6571 recognize opportunities to optimize the output.
6573 On RS/6000, first check for the sum of a register with a constant
6574 integer that is out of range. If so, generate code to add the
6575 constant with the low-order 16 bits masked to the register and force
6576 this result into another register (this can be done with `cau').
6577 Then generate an address of REG+(CONST&0xffff), allowing for the
6578 possibility of bit 16 being a one.
6580 Then check for the sum of a register and something not constant, try to
6581 load the other things into a register and return the sum. */
6583 static rtx
6584 rs6000_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
6585 enum machine_mode mode)
6587 unsigned int extra;
6589 if (!reg_offset_addressing_ok_p (mode))
6591 if (virtual_stack_registers_memory_p (x))
6592 return x;
6594 /* In theory we should not be seeing addresses of the form reg+0,
6595 but just in case it is generated, optimize it away. */
6596 if (GET_CODE (x) == PLUS && XEXP (x, 1) == const0_rtx)
6597 return force_reg (Pmode, XEXP (x, 0));
6599 /* For TImode with load/store quad, restrict addresses to just a single
6600 pointer, so it works with both GPRs and VSX registers. */
6601 /* Make sure both operands are registers. */
6602 else if (GET_CODE (x) == PLUS
6603 && (mode != TImode || !TARGET_QUAD_MEMORY))
6604 return gen_rtx_PLUS (Pmode,
6605 force_reg (Pmode, XEXP (x, 0)),
6606 force_reg (Pmode, XEXP (x, 1)));
6607 else
6608 return force_reg (Pmode, x);
6610 if (GET_CODE (x) == SYMBOL_REF)
6612 enum tls_model model = SYMBOL_REF_TLS_MODEL (x);
6613 if (model != 0)
6614 return rs6000_legitimize_tls_address (x, model);
6617 extra = 0;
6618 switch (mode)
6620 case TFmode:
6621 case TDmode:
6622 case TImode:
6623 case PTImode:
6624 /* As in legitimate_offset_address_p we do not assume
6625 worst-case. The mode here is just a hint as to the registers
6626 used. A TImode is usually in gprs, but may actually be in
6627 fprs. Leave worst-case scenario for reload to handle via
6628 insn constraints. PTImode is only GPRs. */
6629 extra = 8;
6630 break;
6631 default:
6632 break;
6635 if (GET_CODE (x) == PLUS
6636 && GET_CODE (XEXP (x, 0)) == REG
6637 && GET_CODE (XEXP (x, 1)) == CONST_INT
6638 && ((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 1)) + 0x8000)
6639 >= 0x10000 - extra)
6640 && !(SPE_VECTOR_MODE (mode)
6641 || (TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD)))
6643 HOST_WIDE_INT high_int, low_int;
6644 rtx sum;
6645 low_int = ((INTVAL (XEXP (x, 1)) & 0xffff) ^ 0x8000) - 0x8000;
6646 if (low_int >= 0x8000 - extra)
6647 low_int = 0;
6648 high_int = INTVAL (XEXP (x, 1)) - low_int;
6649 sum = force_operand (gen_rtx_PLUS (Pmode, XEXP (x, 0),
6650 GEN_INT (high_int)), 0);
6651 return plus_constant (Pmode, sum, low_int);
6653 else if (GET_CODE (x) == PLUS
6654 && GET_CODE (XEXP (x, 0)) == REG
6655 && GET_CODE (XEXP (x, 1)) != CONST_INT
6656 && GET_MODE_NUNITS (mode) == 1
6657 && (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
6658 || (/* ??? Assume floating point reg based on mode? */
6659 (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
6660 && (mode == DFmode || mode == DDmode)))
6661 && !avoiding_indexed_address_p (mode))
6663 return gen_rtx_PLUS (Pmode, XEXP (x, 0),
6664 force_reg (Pmode, force_operand (XEXP (x, 1), 0)));
6666 else if (SPE_VECTOR_MODE (mode)
6667 || (TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD))
6669 if (mode == DImode)
6670 return x;
6671 /* We accept [reg + reg] and [reg + OFFSET]. */
6673 if (GET_CODE (x) == PLUS)
6675 rtx op1 = XEXP (x, 0);
6676 rtx op2 = XEXP (x, 1);
6677 rtx y;
6679 op1 = force_reg (Pmode, op1);
6681 if (GET_CODE (op2) != REG
6682 && (GET_CODE (op2) != CONST_INT
6683 || !SPE_CONST_OFFSET_OK (INTVAL (op2))
6684 || (GET_MODE_SIZE (mode) > 8
6685 && !SPE_CONST_OFFSET_OK (INTVAL (op2) + 8))))
6686 op2 = force_reg (Pmode, op2);
6688 /* We can't always do [reg + reg] for these, because [reg +
6689 reg + offset] is not a legitimate addressing mode. */
6690 y = gen_rtx_PLUS (Pmode, op1, op2);
6692 if ((GET_MODE_SIZE (mode) > 8 || mode == DDmode) && REG_P (op2))
6693 return force_reg (Pmode, y);
6694 else
6695 return y;
6698 return force_reg (Pmode, x);
6700 else if ((TARGET_ELF
6701 #if TARGET_MACHO
6702 || !MACHO_DYNAMIC_NO_PIC_P
6703 #endif
6705 && TARGET_32BIT
6706 && TARGET_NO_TOC
6707 && ! flag_pic
6708 && GET_CODE (x) != CONST_INT
6709 && GET_CODE (x) != CONST_WIDE_INT
6710 && GET_CODE (x) != CONST_DOUBLE
6711 && CONSTANT_P (x)
6712 && GET_MODE_NUNITS (mode) == 1
6713 && (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
6714 || (/* ??? Assume floating point reg based on mode? */
6715 (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
6716 && (mode == DFmode || mode == DDmode))))
6718 rtx reg = gen_reg_rtx (Pmode);
6719 if (TARGET_ELF)
6720 emit_insn (gen_elf_high (reg, x));
6721 else
6722 emit_insn (gen_macho_high (reg, x));
6723 return gen_rtx_LO_SUM (Pmode, reg, x);
6725 else if (TARGET_TOC
6726 && GET_CODE (x) == SYMBOL_REF
6727 && constant_pool_expr_p (x)
6728 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (x), Pmode))
6729 return create_TOC_reference (x, NULL_RTX);
6730 else
6731 return x;
6734 /* Debug version of rs6000_legitimize_address. */
6735 static rtx
6736 rs6000_debug_legitimize_address (rtx x, rtx oldx, enum machine_mode mode)
6738 rtx ret;
6739 rtx insns;
6741 start_sequence ();
6742 ret = rs6000_legitimize_address (x, oldx, mode);
6743 insns = get_insns ();
6744 end_sequence ();
6746 if (ret != x)
6748 fprintf (stderr,
6749 "\nrs6000_legitimize_address: mode %s, old code %s, "
6750 "new code %s, modified\n",
6751 GET_MODE_NAME (mode), GET_RTX_NAME (GET_CODE (x)),
6752 GET_RTX_NAME (GET_CODE (ret)));
6754 fprintf (stderr, "Original address:\n");
6755 debug_rtx (x);
6757 fprintf (stderr, "oldx:\n");
6758 debug_rtx (oldx);
6760 fprintf (stderr, "New address:\n");
6761 debug_rtx (ret);
6763 if (insns)
6765 fprintf (stderr, "Insns added:\n");
6766 debug_rtx_list (insns, 20);
6769 else
6771 fprintf (stderr,
6772 "\nrs6000_legitimize_address: mode %s, code %s, no change:\n",
6773 GET_MODE_NAME (mode), GET_RTX_NAME (GET_CODE (x)));
6775 debug_rtx (x);
6778 if (insns)
6779 emit_insn (insns);
6781 return ret;
6784 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
6785 We need to emit DTP-relative relocations. */
6787 static void rs6000_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
6788 static void
6789 rs6000_output_dwarf_dtprel (FILE *file, int size, rtx x)
6791 switch (size)
6793 case 4:
6794 fputs ("\t.long\t", file);
6795 break;
6796 case 8:
6797 fputs (DOUBLE_INT_ASM_OP, file);
6798 break;
6799 default:
6800 gcc_unreachable ();
6802 output_addr_const (file, x);
6803 fputs ("@dtprel+0x8000", file);
6806 /* In the name of slightly smaller debug output, and to cater to
6807 general assembler lossage, recognize various UNSPEC sequences
6808 and turn them back into a direct symbol reference. */
6810 static rtx
6811 rs6000_delegitimize_address (rtx orig_x)
6813 rtx x, y, offset;
6815 orig_x = delegitimize_mem_from_attrs (orig_x);
6816 x = orig_x;
6817 if (MEM_P (x))
6818 x = XEXP (x, 0);
6820 y = x;
6821 if (TARGET_CMODEL != CMODEL_SMALL
6822 && GET_CODE (y) == LO_SUM)
6823 y = XEXP (y, 1);
6825 offset = NULL_RTX;
6826 if (GET_CODE (y) == PLUS
6827 && GET_MODE (y) == Pmode
6828 && CONST_INT_P (XEXP (y, 1)))
6830 offset = XEXP (y, 1);
6831 y = XEXP (y, 0);
6834 if (GET_CODE (y) == UNSPEC
6835 && XINT (y, 1) == UNSPEC_TOCREL)
6837 #ifdef ENABLE_CHECKING
6838 if (REG_P (XVECEXP (y, 0, 1))
6839 && REGNO (XVECEXP (y, 0, 1)) == TOC_REGISTER)
6841 /* All good. */
6843 else if (GET_CODE (XVECEXP (y, 0, 1)) == DEBUG_EXPR)
6845 /* Weirdness alert. df_note_compute can replace r2 with a
6846 debug_expr when this unspec is in a debug_insn.
6847 Seen in gcc.dg/pr51957-1.c */
6849 else
6851 debug_rtx (orig_x);
6852 abort ();
6854 #endif
6855 y = XVECEXP (y, 0, 0);
6857 #ifdef HAVE_AS_TLS
6858 /* Do not associate thread-local symbols with the original
6859 constant pool symbol. */
6860 if (TARGET_XCOFF
6861 && GET_CODE (y) == SYMBOL_REF
6862 && CONSTANT_POOL_ADDRESS_P (y)
6863 && SYMBOL_REF_TLS_MODEL (get_pool_constant (y)) >= TLS_MODEL_REAL)
6864 return orig_x;
6865 #endif
6867 if (offset != NULL_RTX)
6868 y = gen_rtx_PLUS (Pmode, y, offset);
6869 if (!MEM_P (orig_x))
6870 return y;
6871 else
6872 return replace_equiv_address_nv (orig_x, y);
6875 if (TARGET_MACHO
6876 && GET_CODE (orig_x) == LO_SUM
6877 && GET_CODE (XEXP (orig_x, 1)) == CONST)
6879 y = XEXP (XEXP (orig_x, 1), 0);
6880 if (GET_CODE (y) == UNSPEC
6881 && XINT (y, 1) == UNSPEC_MACHOPIC_OFFSET)
6882 return XVECEXP (y, 0, 0);
6885 return orig_x;
6888 /* Return true if X shouldn't be emitted into the debug info.
6889 The linker doesn't like .toc section references from
6890 .debug_* sections, so reject .toc section symbols. */
6892 static bool
6893 rs6000_const_not_ok_for_debug_p (rtx x)
6895 if (GET_CODE (x) == SYMBOL_REF
6896 && CONSTANT_POOL_ADDRESS_P (x))
6898 rtx c = get_pool_constant (x);
6899 enum machine_mode cmode = get_pool_mode (x);
6900 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (c, cmode))
6901 return true;
6904 return false;
6907 /* Construct the SYMBOL_REF for the tls_get_addr function. */
6909 static GTY(()) rtx rs6000_tls_symbol;
6910 static rtx
6911 rs6000_tls_get_addr (void)
6913 if (!rs6000_tls_symbol)
6914 rs6000_tls_symbol = init_one_libfunc ("__tls_get_addr");
6916 return rs6000_tls_symbol;
6919 /* Construct the SYMBOL_REF for TLS GOT references. */
6921 static GTY(()) rtx rs6000_got_symbol;
6922 static rtx
6923 rs6000_got_sym (void)
6925 if (!rs6000_got_symbol)
6927 rs6000_got_symbol = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
6928 SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_LOCAL;
6929 SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_EXTERNAL;
6932 return rs6000_got_symbol;
6935 /* AIX Thread-Local Address support. */
6937 static rtx
6938 rs6000_legitimize_tls_address_aix (rtx addr, enum tls_model model)
6940 rtx sym, mem, tocref, tlsreg, tmpreg, dest, tlsaddr;
6941 const char *name;
6942 char *tlsname;
6944 name = XSTR (addr, 0);
6945 /* Append TLS CSECT qualifier, unless the symbol already is qualified
6946 or the symbol will be in TLS private data section. */
6947 if (name[strlen (name) - 1] != ']'
6948 && (TREE_PUBLIC (SYMBOL_REF_DECL (addr))
6949 || bss_initializer_p (SYMBOL_REF_DECL (addr))))
6951 tlsname = XALLOCAVEC (char, strlen (name) + 4);
6952 strcpy (tlsname, name);
6953 strcat (tlsname,
6954 bss_initializer_p (SYMBOL_REF_DECL (addr)) ? "[UL]" : "[TL]");
6955 tlsaddr = copy_rtx (addr);
6956 XSTR (tlsaddr, 0) = ggc_strdup (tlsname);
6958 else
6959 tlsaddr = addr;
6961 /* Place addr into TOC constant pool. */
6962 sym = force_const_mem (GET_MODE (tlsaddr), tlsaddr);
6964 /* Output the TOC entry and create the MEM referencing the value. */
6965 if (constant_pool_expr_p (XEXP (sym, 0))
6966 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (XEXP (sym, 0)), Pmode))
6968 tocref = create_TOC_reference (XEXP (sym, 0), NULL_RTX);
6969 mem = gen_const_mem (Pmode, tocref);
6970 set_mem_alias_set (mem, get_TOC_alias_set ());
6972 else
6973 return sym;
6975 /* Use global-dynamic for local-dynamic. */
6976 if (model == TLS_MODEL_GLOBAL_DYNAMIC
6977 || model == TLS_MODEL_LOCAL_DYNAMIC)
6979 /* Create new TOC reference for @m symbol. */
6980 name = XSTR (XVECEXP (XEXP (mem, 0), 0, 0), 0);
6981 tlsname = XALLOCAVEC (char, strlen (name) + 1);
6982 strcpy (tlsname, "*LCM");
6983 strcat (tlsname, name + 3);
6984 rtx modaddr = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (tlsname));
6985 SYMBOL_REF_FLAGS (modaddr) |= SYMBOL_FLAG_LOCAL;
6986 tocref = create_TOC_reference (modaddr, NULL_RTX);
6987 rtx modmem = gen_const_mem (Pmode, tocref);
6988 set_mem_alias_set (modmem, get_TOC_alias_set ());
6990 rtx modreg = gen_reg_rtx (Pmode);
6991 emit_insn (gen_rtx_SET (VOIDmode, modreg, modmem));
6993 tmpreg = gen_reg_rtx (Pmode);
6994 emit_insn (gen_rtx_SET (VOIDmode, tmpreg, mem));
6996 dest = gen_reg_rtx (Pmode);
6997 if (TARGET_32BIT)
6998 emit_insn (gen_tls_get_addrsi (dest, modreg, tmpreg));
6999 else
7000 emit_insn (gen_tls_get_addrdi (dest, modreg, tmpreg));
7001 return dest;
7003 /* Obtain TLS pointer: 32 bit call or 64 bit GPR 13. */
7004 else if (TARGET_32BIT)
7006 tlsreg = gen_reg_rtx (SImode);
7007 emit_insn (gen_tls_get_tpointer (tlsreg));
7009 else
7010 tlsreg = gen_rtx_REG (DImode, 13);
7012 /* Load the TOC value into temporary register. */
7013 tmpreg = gen_reg_rtx (Pmode);
7014 emit_insn (gen_rtx_SET (VOIDmode, tmpreg, mem));
7015 set_unique_reg_note (get_last_insn (), REG_EQUAL,
7016 gen_rtx_MINUS (Pmode, addr, tlsreg));
7018 /* Add TOC symbol value to TLS pointer. */
7019 dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, tmpreg, tlsreg));
7021 return dest;
7024 /* ADDR contains a thread-local SYMBOL_REF. Generate code to compute
7025 this (thread-local) address. */
7027 static rtx
7028 rs6000_legitimize_tls_address (rtx addr, enum tls_model model)
7030 rtx dest, insn;
7032 if (TARGET_XCOFF)
7033 return rs6000_legitimize_tls_address_aix (addr, model);
7035 dest = gen_reg_rtx (Pmode);
7036 if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 16)
7038 rtx tlsreg;
7040 if (TARGET_64BIT)
7042 tlsreg = gen_rtx_REG (Pmode, 13);
7043 insn = gen_tls_tprel_64 (dest, tlsreg, addr);
7045 else
7047 tlsreg = gen_rtx_REG (Pmode, 2);
7048 insn = gen_tls_tprel_32 (dest, tlsreg, addr);
7050 emit_insn (insn);
7052 else if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 32)
7054 rtx tlsreg, tmp;
7056 tmp = gen_reg_rtx (Pmode);
7057 if (TARGET_64BIT)
7059 tlsreg = gen_rtx_REG (Pmode, 13);
7060 insn = gen_tls_tprel_ha_64 (tmp, tlsreg, addr);
7062 else
7064 tlsreg = gen_rtx_REG (Pmode, 2);
7065 insn = gen_tls_tprel_ha_32 (tmp, tlsreg, addr);
7067 emit_insn (insn);
7068 if (TARGET_64BIT)
7069 insn = gen_tls_tprel_lo_64 (dest, tmp, addr);
7070 else
7071 insn = gen_tls_tprel_lo_32 (dest, tmp, addr);
7072 emit_insn (insn);
7074 else
7076 rtx r3, got, tga, tmp1, tmp2, call_insn;
7078 /* We currently use relocations like @got@tlsgd for tls, which
7079 means the linker will handle allocation of tls entries, placing
7080 them in the .got section. So use a pointer to the .got section,
7081 not one to secondary TOC sections used by 64-bit -mminimal-toc,
7082 or to secondary GOT sections used by 32-bit -fPIC. */
7083 if (TARGET_64BIT)
7084 got = gen_rtx_REG (Pmode, 2);
7085 else
7087 if (flag_pic == 1)
7088 got = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
7089 else
7091 rtx gsym = rs6000_got_sym ();
7092 got = gen_reg_rtx (Pmode);
7093 if (flag_pic == 0)
7094 rs6000_emit_move (got, gsym, Pmode);
7095 else
7097 rtx mem, lab, last;
7099 tmp1 = gen_reg_rtx (Pmode);
7100 tmp2 = gen_reg_rtx (Pmode);
7101 mem = gen_const_mem (Pmode, tmp1);
7102 lab = gen_label_rtx ();
7103 emit_insn (gen_load_toc_v4_PIC_1b (gsym, lab));
7104 emit_move_insn (tmp1, gen_rtx_REG (Pmode, LR_REGNO));
7105 if (TARGET_LINK_STACK)
7106 emit_insn (gen_addsi3 (tmp1, tmp1, GEN_INT (4)));
7107 emit_move_insn (tmp2, mem);
7108 last = emit_insn (gen_addsi3 (got, tmp1, tmp2));
7109 set_unique_reg_note (last, REG_EQUAL, gsym);
7114 if (model == TLS_MODEL_GLOBAL_DYNAMIC)
7116 tga = rs6000_tls_get_addr ();
7117 emit_library_call_value (tga, dest, LCT_CONST, Pmode,
7118 1, const0_rtx, Pmode);
7120 r3 = gen_rtx_REG (Pmode, 3);
7121 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
7123 if (TARGET_64BIT)
7124 insn = gen_tls_gd_aix64 (r3, got, addr, tga, const0_rtx);
7125 else
7126 insn = gen_tls_gd_aix32 (r3, got, addr, tga, const0_rtx);
7128 else if (DEFAULT_ABI == ABI_V4)
7129 insn = gen_tls_gd_sysvsi (r3, got, addr, tga, const0_rtx);
7130 else
7131 gcc_unreachable ();
7132 call_insn = last_call_insn ();
7133 PATTERN (call_insn) = insn;
7134 if (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
7135 use_reg (&CALL_INSN_FUNCTION_USAGE (call_insn),
7136 pic_offset_table_rtx);
7138 else if (model == TLS_MODEL_LOCAL_DYNAMIC)
7140 tga = rs6000_tls_get_addr ();
7141 tmp1 = gen_reg_rtx (Pmode);
7142 emit_library_call_value (tga, tmp1, LCT_CONST, Pmode,
7143 1, const0_rtx, Pmode);
7145 r3 = gen_rtx_REG (Pmode, 3);
7146 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
7148 if (TARGET_64BIT)
7149 insn = gen_tls_ld_aix64 (r3, got, tga, const0_rtx);
7150 else
7151 insn = gen_tls_ld_aix32 (r3, got, tga, const0_rtx);
7153 else if (DEFAULT_ABI == ABI_V4)
7154 insn = gen_tls_ld_sysvsi (r3, got, tga, const0_rtx);
7155 else
7156 gcc_unreachable ();
7157 call_insn = last_call_insn ();
7158 PATTERN (call_insn) = insn;
7159 if (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
7160 use_reg (&CALL_INSN_FUNCTION_USAGE (call_insn),
7161 pic_offset_table_rtx);
7163 if (rs6000_tls_size == 16)
7165 if (TARGET_64BIT)
7166 insn = gen_tls_dtprel_64 (dest, tmp1, addr);
7167 else
7168 insn = gen_tls_dtprel_32 (dest, tmp1, addr);
7170 else if (rs6000_tls_size == 32)
7172 tmp2 = gen_reg_rtx (Pmode);
7173 if (TARGET_64BIT)
7174 insn = gen_tls_dtprel_ha_64 (tmp2, tmp1, addr);
7175 else
7176 insn = gen_tls_dtprel_ha_32 (tmp2, tmp1, addr);
7177 emit_insn (insn);
7178 if (TARGET_64BIT)
7179 insn = gen_tls_dtprel_lo_64 (dest, tmp2, addr);
7180 else
7181 insn = gen_tls_dtprel_lo_32 (dest, tmp2, addr);
7183 else
7185 tmp2 = gen_reg_rtx (Pmode);
7186 if (TARGET_64BIT)
7187 insn = gen_tls_got_dtprel_64 (tmp2, got, addr);
7188 else
7189 insn = gen_tls_got_dtprel_32 (tmp2, got, addr);
7190 emit_insn (insn);
7191 insn = gen_rtx_SET (Pmode, dest,
7192 gen_rtx_PLUS (Pmode, tmp2, tmp1));
7194 emit_insn (insn);
7196 else
7198 /* IE, or 64-bit offset LE. */
7199 tmp2 = gen_reg_rtx (Pmode);
7200 if (TARGET_64BIT)
7201 insn = gen_tls_got_tprel_64 (tmp2, got, addr);
7202 else
7203 insn = gen_tls_got_tprel_32 (tmp2, got, addr);
7204 emit_insn (insn);
7205 if (TARGET_64BIT)
7206 insn = gen_tls_tls_64 (dest, tmp2, addr);
7207 else
7208 insn = gen_tls_tls_32 (dest, tmp2, addr);
7209 emit_insn (insn);
7213 return dest;
7216 /* Return 1 if X contains a thread-local symbol. */
7218 static bool
7219 rs6000_tls_referenced_p (rtx x)
7221 if (! TARGET_HAVE_TLS)
7222 return false;
7224 return for_each_rtx (&x, &rs6000_tls_symbol_ref_1, 0);
7227 /* Implement TARGET_CANNOT_FORCE_CONST_MEM. */
7229 static bool
7230 rs6000_cannot_force_const_mem (enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
7232 if (GET_CODE (x) == HIGH
7233 && GET_CODE (XEXP (x, 0)) == UNSPEC)
7234 return true;
7236 /* A TLS symbol in the TOC cannot contain a sum. */
7237 if (GET_CODE (x) == CONST
7238 && GET_CODE (XEXP (x, 0)) == PLUS
7239 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
7240 && SYMBOL_REF_TLS_MODEL (XEXP (XEXP (x, 0), 0)) != 0)
7241 return true;
7243 /* Do not place an ELF TLS symbol in the constant pool. */
7244 return TARGET_ELF && rs6000_tls_referenced_p (x);
7247 /* Return 1 if *X is a thread-local symbol. This is the same as
7248 rs6000_tls_symbol_ref except for the type of the unused argument. */
7250 static int
7251 rs6000_tls_symbol_ref_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
7253 return RS6000_SYMBOL_REF_TLS_P (*x);
7256 /* Return true iff the given SYMBOL_REF refers to a constant pool entry
7257 that we have put in the TOC, or for cmodel=medium, if the SYMBOL_REF
7258 can be addressed relative to the toc pointer. */
7260 static bool
7261 use_toc_relative_ref (rtx sym)
7263 return ((constant_pool_expr_p (sym)
7264 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (sym),
7265 get_pool_mode (sym)))
7266 || (TARGET_CMODEL == CMODEL_MEDIUM
7267 && SYMBOL_REF_LOCAL_P (sym)));
7270 /* Our implementation of LEGITIMIZE_RELOAD_ADDRESS. Returns a value to
7271 replace the input X, or the original X if no replacement is called for.
7272 The output parameter *WIN is 1 if the calling macro should goto WIN,
7273 0 if it should not.
7275 For RS/6000, we wish to handle large displacements off a base
7276 register by splitting the addend across an addiu/addis and the mem insn.
7277 This cuts number of extra insns needed from 3 to 1.
7279 On Darwin, we use this to generate code for floating point constants.
7280 A movsf_low is generated so we wind up with 2 instructions rather than 3.
7281 The Darwin code is inside #if TARGET_MACHO because only then are the
7282 machopic_* functions defined. */
7283 static rtx
7284 rs6000_legitimize_reload_address (rtx x, enum machine_mode mode,
7285 int opnum, int type,
7286 int ind_levels ATTRIBUTE_UNUSED, int *win)
7288 bool reg_offset_p = reg_offset_addressing_ok_p (mode);
7290 /* Nasty hack for vsx_splat_V2DF/V2DI load from mem, which takes a
7291 DFmode/DImode MEM. */
7292 if (reg_offset_p
7293 && opnum == 1
7294 && ((mode == DFmode && recog_data.operand_mode[0] == V2DFmode)
7295 || (mode == DImode && recog_data.operand_mode[0] == V2DImode)))
7296 reg_offset_p = false;
7298 /* We must recognize output that we have already generated ourselves. */
7299 if (GET_CODE (x) == PLUS
7300 && GET_CODE (XEXP (x, 0)) == PLUS
7301 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
7302 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7303 && GET_CODE (XEXP (x, 1)) == CONST_INT)
7305 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7306 BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
7307 opnum, (enum reload_type) type);
7308 *win = 1;
7309 return x;
7312 /* Likewise for (lo_sum (high ...) ...) output we have generated. */
7313 if (GET_CODE (x) == LO_SUM
7314 && GET_CODE (XEXP (x, 0)) == HIGH)
7316 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7317 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
7318 opnum, (enum reload_type) type);
7319 *win = 1;
7320 return x;
7323 #if TARGET_MACHO
7324 if (DEFAULT_ABI == ABI_DARWIN && flag_pic
7325 && GET_CODE (x) == LO_SUM
7326 && GET_CODE (XEXP (x, 0)) == PLUS
7327 && XEXP (XEXP (x, 0), 0) == pic_offset_table_rtx
7328 && GET_CODE (XEXP (XEXP (x, 0), 1)) == HIGH
7329 && XEXP (XEXP (XEXP (x, 0), 1), 0) == XEXP (x, 1)
7330 && machopic_operand_p (XEXP (x, 1)))
7332 /* Result of previous invocation of this function on Darwin
7333 floating point constant. */
7334 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7335 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
7336 opnum, (enum reload_type) type);
7337 *win = 1;
7338 return x;
7340 #endif
7342 if (TARGET_CMODEL != CMODEL_SMALL
7343 && reg_offset_p
7344 && small_toc_ref (x, VOIDmode))
7346 rtx hi = gen_rtx_HIGH (Pmode, copy_rtx (x));
7347 x = gen_rtx_LO_SUM (Pmode, hi, x);
7348 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7349 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
7350 opnum, (enum reload_type) type);
7351 *win = 1;
7352 return x;
7355 if (GET_CODE (x) == PLUS
7356 && GET_CODE (XEXP (x, 0)) == REG
7357 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
7358 && INT_REG_OK_FOR_BASE_P (XEXP (x, 0), 1)
7359 && GET_CODE (XEXP (x, 1)) == CONST_INT
7360 && reg_offset_p
7361 && !SPE_VECTOR_MODE (mode)
7362 && !(TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
7363 && (!VECTOR_MODE_P (mode) || VECTOR_MEM_NONE_P (mode)))
7365 HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
7366 HOST_WIDE_INT low = ((val & 0xffff) ^ 0x8000) - 0x8000;
7367 HOST_WIDE_INT high
7368 = (((val - low) & 0xffffffff) ^ 0x80000000) - 0x80000000;
7370 /* Check for 32-bit overflow. */
7371 if (high + low != val)
7373 *win = 0;
7374 return x;
7377 /* Reload the high part into a base reg; leave the low part
7378 in the mem directly. */
7380 x = gen_rtx_PLUS (GET_MODE (x),
7381 gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0),
7382 GEN_INT (high)),
7383 GEN_INT (low));
7385 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7386 BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
7387 opnum, (enum reload_type) type);
7388 *win = 1;
7389 return x;
7392 if (GET_CODE (x) == SYMBOL_REF
7393 && reg_offset_p
7394 && (!VECTOR_MODE_P (mode) || VECTOR_MEM_NONE_P (mode))
7395 && !SPE_VECTOR_MODE (mode)
7396 #if TARGET_MACHO
7397 && DEFAULT_ABI == ABI_DARWIN
7398 && (flag_pic || MACHO_DYNAMIC_NO_PIC_P)
7399 && machopic_symbol_defined_p (x)
7400 #else
7401 && DEFAULT_ABI == ABI_V4
7402 && !flag_pic
7403 #endif
7404 /* Don't do this for TFmode or TDmode, since the result isn't offsettable.
7405 The same goes for DImode without 64-bit gprs and DFmode and DDmode
7406 without fprs.
7407 ??? Assume floating point reg based on mode? This assumption is
7408 violated by eg. powerpc-linux -m32 compile of gcc.dg/pr28796-2.c
7409 where reload ends up doing a DFmode load of a constant from
7410 mem using two gprs. Unfortunately, at this point reload
7411 hasn't yet selected regs so poking around in reload data
7412 won't help and even if we could figure out the regs reliably,
7413 we'd still want to allow this transformation when the mem is
7414 naturally aligned. Since we say the address is good here, we
7415 can't disable offsets from LO_SUMs in mem_operand_gpr.
7416 FIXME: Allow offset from lo_sum for other modes too, when
7417 mem is sufficiently aligned. */
7418 && mode != TFmode
7419 && mode != TDmode
7420 && (mode != TImode || !TARGET_VSX_TIMODE)
7421 && mode != PTImode
7422 && (mode != DImode || TARGET_POWERPC64)
7423 && ((mode != DFmode && mode != DDmode) || TARGET_POWERPC64
7424 || (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)))
7426 #if TARGET_MACHO
7427 if (flag_pic)
7429 rtx offset = machopic_gen_offset (x);
7430 x = gen_rtx_LO_SUM (GET_MODE (x),
7431 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
7432 gen_rtx_HIGH (Pmode, offset)), offset);
7434 else
7435 #endif
7436 x = gen_rtx_LO_SUM (GET_MODE (x),
7437 gen_rtx_HIGH (Pmode, x), x);
7439 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7440 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
7441 opnum, (enum reload_type) type);
7442 *win = 1;
7443 return x;
7446 /* Reload an offset address wrapped by an AND that represents the
7447 masking of the lower bits. Strip the outer AND and let reload
7448 convert the offset address into an indirect address. For VSX,
7449 force reload to create the address with an AND in a separate
7450 register, because we can't guarantee an altivec register will
7451 be used. */
7452 if (VECTOR_MEM_ALTIVEC_P (mode)
7453 && GET_CODE (x) == AND
7454 && GET_CODE (XEXP (x, 0)) == PLUS
7455 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
7456 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7457 && GET_CODE (XEXP (x, 1)) == CONST_INT
7458 && INTVAL (XEXP (x, 1)) == -16)
7460 x = XEXP (x, 0);
7461 *win = 1;
7462 return x;
7465 if (TARGET_TOC
7466 && reg_offset_p
7467 && GET_CODE (x) == SYMBOL_REF
7468 && use_toc_relative_ref (x))
7470 x = create_TOC_reference (x, NULL_RTX);
7471 if (TARGET_CMODEL != CMODEL_SMALL)
7472 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7473 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
7474 opnum, (enum reload_type) type);
7475 *win = 1;
7476 return x;
7478 *win = 0;
7479 return x;
7482 /* Debug version of rs6000_legitimize_reload_address. */
7483 static rtx
7484 rs6000_debug_legitimize_reload_address (rtx x, enum machine_mode mode,
7485 int opnum, int type,
7486 int ind_levels, int *win)
7488 rtx ret = rs6000_legitimize_reload_address (x, mode, opnum, type,
7489 ind_levels, win);
7490 fprintf (stderr,
7491 "\nrs6000_legitimize_reload_address: mode = %s, opnum = %d, "
7492 "type = %d, ind_levels = %d, win = %d, original addr:\n",
7493 GET_MODE_NAME (mode), opnum, type, ind_levels, *win);
7494 debug_rtx (x);
7496 if (x == ret)
7497 fprintf (stderr, "Same address returned\n");
7498 else if (!ret)
7499 fprintf (stderr, "NULL returned\n");
7500 else
7502 fprintf (stderr, "New address:\n");
7503 debug_rtx (ret);
7506 return ret;
7509 /* TARGET_LEGITIMATE_ADDRESS_P recognizes an RTL expression
7510 that is a valid memory address for an instruction.
7511 The MODE argument is the machine mode for the MEM expression
7512 that wants to use this address.
7514 On the RS/6000, there are four valid address: a SYMBOL_REF that
7515 refers to a constant pool entry of an address (or the sum of it
7516 plus a constant), a short (16-bit signed) constant plus a register,
7517 the sum of two registers, or a register indirect, possibly with an
7518 auto-increment. For DFmode, DDmode and DImode with a constant plus
7519 register, we must ensure that both words are addressable or PowerPC64
7520 with offset word aligned.
7522 For modes spanning multiple registers (DFmode and DDmode in 32-bit GPRs,
7523 32-bit DImode, TImode, TFmode, TDmode), indexed addressing cannot be used
7524 because adjacent memory cells are accessed by adding word-sized offsets
7525 during assembly output. */
7526 static bool
7527 rs6000_legitimate_address_p (enum machine_mode mode, rtx x, bool reg_ok_strict)
7529 bool reg_offset_p = reg_offset_addressing_ok_p (mode);
7531 /* If this is an unaligned stvx/ldvx type address, discard the outer AND. */
7532 if (VECTOR_MEM_ALTIVEC_P (mode)
7533 && GET_CODE (x) == AND
7534 && GET_CODE (XEXP (x, 1)) == CONST_INT
7535 && INTVAL (XEXP (x, 1)) == -16)
7536 x = XEXP (x, 0);
7538 if (TARGET_ELF && RS6000_SYMBOL_REF_TLS_P (x))
7539 return 0;
7540 if (legitimate_indirect_address_p (x, reg_ok_strict))
7541 return 1;
7542 if (TARGET_UPDATE
7543 && (GET_CODE (x) == PRE_INC || GET_CODE (x) == PRE_DEC)
7544 && mode_supports_pre_incdec_p (mode)
7545 && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict))
7546 return 1;
7547 if (virtual_stack_registers_memory_p (x))
7548 return 1;
7549 if (reg_offset_p && legitimate_small_data_p (mode, x))
7550 return 1;
7551 if (reg_offset_p
7552 && legitimate_constant_pool_address_p (x, mode,
7553 reg_ok_strict || lra_in_progress))
7554 return 1;
7555 /* For TImode, if we have load/store quad and TImode in VSX registers, only
7556 allow register indirect addresses. This will allow the values to go in
7557 either GPRs or VSX registers without reloading. The vector types would
7558 tend to go into VSX registers, so we allow REG+REG, while TImode seems
7559 somewhat split, in that some uses are GPR based, and some VSX based. */
7560 if (mode == TImode && TARGET_QUAD_MEMORY && TARGET_VSX_TIMODE)
7561 return 0;
7562 /* If not REG_OK_STRICT (before reload) let pass any stack offset. */
7563 if (! reg_ok_strict
7564 && reg_offset_p
7565 && GET_CODE (x) == PLUS
7566 && GET_CODE (XEXP (x, 0)) == REG
7567 && (XEXP (x, 0) == virtual_stack_vars_rtx
7568 || XEXP (x, 0) == arg_pointer_rtx)
7569 && GET_CODE (XEXP (x, 1)) == CONST_INT)
7570 return 1;
7571 if (rs6000_legitimate_offset_address_p (mode, x, reg_ok_strict, false))
7572 return 1;
7573 if (mode != TFmode
7574 && mode != TDmode
7575 && ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
7576 || TARGET_POWERPC64
7577 || (mode != DFmode && mode != DDmode)
7578 || (TARGET_E500_DOUBLE && mode != DDmode))
7579 && (TARGET_POWERPC64 || mode != DImode)
7580 && (mode != TImode || VECTOR_MEM_VSX_P (TImode))
7581 && mode != PTImode
7582 && !avoiding_indexed_address_p (mode)
7583 && legitimate_indexed_address_p (x, reg_ok_strict))
7584 return 1;
7585 if (TARGET_UPDATE && GET_CODE (x) == PRE_MODIFY
7586 && mode_supports_pre_modify_p (mode)
7587 && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict)
7588 && (rs6000_legitimate_offset_address_p (mode, XEXP (x, 1),
7589 reg_ok_strict, false)
7590 || (!avoiding_indexed_address_p (mode)
7591 && legitimate_indexed_address_p (XEXP (x, 1), reg_ok_strict)))
7592 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
7593 return 1;
7594 if (reg_offset_p && legitimate_lo_sum_address_p (mode, x, reg_ok_strict))
7595 return 1;
7596 return 0;
7599 /* Debug version of rs6000_legitimate_address_p. */
7600 static bool
7601 rs6000_debug_legitimate_address_p (enum machine_mode mode, rtx x,
7602 bool reg_ok_strict)
7604 bool ret = rs6000_legitimate_address_p (mode, x, reg_ok_strict);
7605 fprintf (stderr,
7606 "\nrs6000_legitimate_address_p: return = %s, mode = %s, "
7607 "strict = %d, reload = %s, code = %s\n",
7608 ret ? "true" : "false",
7609 GET_MODE_NAME (mode),
7610 reg_ok_strict,
7611 (reload_completed
7612 ? "after"
7613 : (reload_in_progress ? "progress" : "before")),
7614 GET_RTX_NAME (GET_CODE (x)));
7615 debug_rtx (x);
7617 return ret;
7620 /* Implement TARGET_MODE_DEPENDENT_ADDRESS_P. */
7622 static bool
7623 rs6000_mode_dependent_address_p (const_rtx addr,
7624 addr_space_t as ATTRIBUTE_UNUSED)
7626 return rs6000_mode_dependent_address_ptr (addr);
7629 /* Go to LABEL if ADDR (a legitimate address expression)
7630 has an effect that depends on the machine mode it is used for.
7632 On the RS/6000 this is true of all integral offsets (since AltiVec
7633 and VSX modes don't allow them) or is a pre-increment or decrement.
7635 ??? Except that due to conceptual problems in offsettable_address_p
7636 we can't really report the problems of integral offsets. So leave
7637 this assuming that the adjustable offset must be valid for the
7638 sub-words of a TFmode operand, which is what we had before. */
7640 static bool
7641 rs6000_mode_dependent_address (const_rtx addr)
7643 switch (GET_CODE (addr))
7645 case PLUS:
7646 /* Any offset from virtual_stack_vars_rtx and arg_pointer_rtx
7647 is considered a legitimate address before reload, so there
7648 are no offset restrictions in that case. Note that this
7649 condition is safe in strict mode because any address involving
7650 virtual_stack_vars_rtx or arg_pointer_rtx would already have
7651 been rejected as illegitimate. */
7652 if (XEXP (addr, 0) != virtual_stack_vars_rtx
7653 && XEXP (addr, 0) != arg_pointer_rtx
7654 && GET_CODE (XEXP (addr, 1)) == CONST_INT)
7656 unsigned HOST_WIDE_INT val = INTVAL (XEXP (addr, 1));
7657 return val + 0x8000 >= 0x10000 - (TARGET_POWERPC64 ? 8 : 12);
7659 break;
7661 case LO_SUM:
7662 /* Anything in the constant pool is sufficiently aligned that
7663 all bytes have the same high part address. */
7664 return !legitimate_constant_pool_address_p (addr, QImode, false);
7666 /* Auto-increment cases are now treated generically in recog.c. */
7667 case PRE_MODIFY:
7668 return TARGET_UPDATE;
7670 /* AND is only allowed in Altivec loads. */
7671 case AND:
7672 return true;
7674 default:
7675 break;
7678 return false;
7681 /* Debug version of rs6000_mode_dependent_address. */
7682 static bool
7683 rs6000_debug_mode_dependent_address (const_rtx addr)
7685 bool ret = rs6000_mode_dependent_address (addr);
7687 fprintf (stderr, "\nrs6000_mode_dependent_address: ret = %s\n",
7688 ret ? "true" : "false");
7689 debug_rtx (addr);
7691 return ret;
7694 /* Implement FIND_BASE_TERM. */
7697 rs6000_find_base_term (rtx op)
7699 rtx base;
7701 base = op;
7702 if (GET_CODE (base) == CONST)
7703 base = XEXP (base, 0);
7704 if (GET_CODE (base) == PLUS)
7705 base = XEXP (base, 0);
7706 if (GET_CODE (base) == UNSPEC)
7707 switch (XINT (base, 1))
7709 case UNSPEC_TOCREL:
7710 case UNSPEC_MACHOPIC_OFFSET:
7711 /* OP represents SYM [+ OFFSET] - ANCHOR. SYM is the base term
7712 for aliasing purposes. */
7713 return XVECEXP (base, 0, 0);
7716 return op;
7719 /* More elaborate version of recog's offsettable_memref_p predicate
7720 that works around the ??? note of rs6000_mode_dependent_address.
7721 In particular it accepts
7723 (mem:DI (plus:SI (reg/f:SI 31 31) (const_int 32760 [0x7ff8])))
7725 in 32-bit mode, that the recog predicate rejects. */
7727 static bool
7728 rs6000_offsettable_memref_p (rtx op, enum machine_mode reg_mode)
7730 bool worst_case;
7732 if (!MEM_P (op))
7733 return false;
7735 /* First mimic offsettable_memref_p. */
7736 if (offsettable_address_p (true, GET_MODE (op), XEXP (op, 0)))
7737 return true;
7739 /* offsettable_address_p invokes rs6000_mode_dependent_address, but
7740 the latter predicate knows nothing about the mode of the memory
7741 reference and, therefore, assumes that it is the largest supported
7742 mode (TFmode). As a consequence, legitimate offsettable memory
7743 references are rejected. rs6000_legitimate_offset_address_p contains
7744 the correct logic for the PLUS case of rs6000_mode_dependent_address,
7745 at least with a little bit of help here given that we know the
7746 actual registers used. */
7747 worst_case = ((TARGET_POWERPC64 && GET_MODE_CLASS (reg_mode) == MODE_INT)
7748 || GET_MODE_SIZE (reg_mode) == 4);
7749 return rs6000_legitimate_offset_address_p (GET_MODE (op), XEXP (op, 0),
7750 true, worst_case);
7753 /* Change register usage conditional on target flags. */
7754 static void
7755 rs6000_conditional_register_usage (void)
7757 int i;
7759 if (TARGET_DEBUG_TARGET)
7760 fprintf (stderr, "rs6000_conditional_register_usage called\n");
7762 /* Set MQ register fixed (already call_used) so that it will not be
7763 allocated. */
7764 fixed_regs[64] = 1;
7766 /* 64-bit AIX and Linux reserve GPR13 for thread-private data. */
7767 if (TARGET_64BIT)
7768 fixed_regs[13] = call_used_regs[13]
7769 = call_really_used_regs[13] = 1;
7771 /* Conditionally disable FPRs. */
7772 if (TARGET_SOFT_FLOAT || !TARGET_FPRS)
7773 for (i = 32; i < 64; i++)
7774 fixed_regs[i] = call_used_regs[i]
7775 = call_really_used_regs[i] = 1;
7777 /* The TOC register is not killed across calls in a way that is
7778 visible to the compiler. */
7779 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
7780 call_really_used_regs[2] = 0;
7782 if (DEFAULT_ABI == ABI_V4
7783 && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
7784 && flag_pic == 2)
7785 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
7787 if (DEFAULT_ABI == ABI_V4
7788 && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
7789 && flag_pic == 1)
7790 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
7791 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
7792 = call_really_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
7794 if (DEFAULT_ABI == ABI_DARWIN
7795 && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM)
7796 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
7797 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
7798 = call_really_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
7800 if (TARGET_TOC && TARGET_MINIMAL_TOC)
7801 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
7802 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
7804 if (TARGET_SPE)
7806 global_regs[SPEFSCR_REGNO] = 1;
7807 /* We used to use r14 as FIXED_SCRATCH to address SPE 64-bit
7808 registers in prologues and epilogues. We no longer use r14
7809 for FIXED_SCRATCH, but we're keeping r14 out of the allocation
7810 pool for link-compatibility with older versions of GCC. Once
7811 "old" code has died out, we can return r14 to the allocation
7812 pool. */
7813 fixed_regs[14]
7814 = call_used_regs[14]
7815 = call_really_used_regs[14] = 1;
7818 if (!TARGET_ALTIVEC && !TARGET_VSX)
7820 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
7821 fixed_regs[i] = call_used_regs[i] = call_really_used_regs[i] = 1;
7822 call_really_used_regs[VRSAVE_REGNO] = 1;
7825 if (TARGET_ALTIVEC || TARGET_VSX)
7826 global_regs[VSCR_REGNO] = 1;
7828 if (TARGET_ALTIVEC_ABI)
7830 for (i = FIRST_ALTIVEC_REGNO; i < FIRST_ALTIVEC_REGNO + 20; ++i)
7831 call_used_regs[i] = call_really_used_regs[i] = 1;
7833 /* AIX reserves VR20:31 in non-extended ABI mode. */
7834 if (TARGET_XCOFF)
7835 for (i = FIRST_ALTIVEC_REGNO + 20; i < FIRST_ALTIVEC_REGNO + 32; ++i)
7836 fixed_regs[i] = call_used_regs[i] = call_really_used_regs[i] = 1;
7841 /* Try to output insns to set TARGET equal to the constant C if it can
7842 be done in less than N insns. Do all computations in MODE.
7843 Returns the place where the output has been placed if it can be
7844 done and the insns have been emitted. If it would take more than N
7845 insns, zero is returned and no insns and emitted. */
7848 rs6000_emit_set_const (rtx dest, enum machine_mode mode,
7849 rtx source, int n ATTRIBUTE_UNUSED)
7851 rtx result, insn, set;
7852 HOST_WIDE_INT c0, c1;
7854 switch (mode)
7856 case QImode:
7857 case HImode:
7858 if (dest == NULL)
7859 dest = gen_reg_rtx (mode);
7860 emit_insn (gen_rtx_SET (VOIDmode, dest, source));
7861 return dest;
7863 case SImode:
7864 result = !can_create_pseudo_p () ? dest : gen_reg_rtx (SImode);
7866 emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (result),
7867 GEN_INT (INTVAL (source)
7868 & (~ (HOST_WIDE_INT) 0xffff))));
7869 emit_insn (gen_rtx_SET (VOIDmode, dest,
7870 gen_rtx_IOR (SImode, copy_rtx (result),
7871 GEN_INT (INTVAL (source) & 0xffff))));
7872 result = dest;
7873 break;
7875 case DImode:
7876 switch (GET_CODE (source))
7878 case CONST_INT:
7879 c0 = INTVAL (source);
7880 c1 = -(c0 < 0);
7881 break;
7883 default:
7884 gcc_unreachable ();
7887 result = rs6000_emit_set_long_const (dest, c0, c1);
7888 break;
7890 default:
7891 gcc_unreachable ();
7894 insn = get_last_insn ();
7895 set = single_set (insn);
7896 if (! CONSTANT_P (SET_SRC (set)))
7897 set_unique_reg_note (insn, REG_EQUAL, source);
7899 return result;
7902 /* Having failed to find a 3 insn sequence in rs6000_emit_set_const,
7903 fall back to a straight forward decomposition. We do this to avoid
7904 exponential run times encountered when looking for longer sequences
7905 with rs6000_emit_set_const. */
7906 static rtx
7907 rs6000_emit_set_long_const (rtx dest, HOST_WIDE_INT c1, HOST_WIDE_INT c2)
7909 if (!TARGET_POWERPC64)
7911 rtx operand1, operand2;
7913 operand1 = operand_subword_force (dest, WORDS_BIG_ENDIAN == 0,
7914 DImode);
7915 operand2 = operand_subword_force (copy_rtx (dest), WORDS_BIG_ENDIAN != 0,
7916 DImode);
7917 emit_move_insn (operand1, GEN_INT (c1));
7918 emit_move_insn (operand2, GEN_INT (c2));
7920 else
7922 HOST_WIDE_INT ud1, ud2, ud3, ud4;
7924 ud1 = c1 & 0xffff;
7925 ud2 = (c1 & 0xffff0000) >> 16;
7926 c2 = c1 >> 32;
7927 ud3 = c2 & 0xffff;
7928 ud4 = (c2 & 0xffff0000) >> 16;
7930 if ((ud4 == 0xffff && ud3 == 0xffff && ud2 == 0xffff && (ud1 & 0x8000))
7931 || (ud4 == 0 && ud3 == 0 && ud2 == 0 && ! (ud1 & 0x8000)))
7932 emit_move_insn (dest, GEN_INT ((ud1 ^ 0x8000) - 0x8000));
7934 else if ((ud4 == 0xffff && ud3 == 0xffff && (ud2 & 0x8000))
7935 || (ud4 == 0 && ud3 == 0 && ! (ud2 & 0x8000)))
7937 emit_move_insn (dest, GEN_INT (((ud2 << 16) ^ 0x80000000)
7938 - 0x80000000));
7939 if (ud1 != 0)
7940 emit_move_insn (copy_rtx (dest),
7941 gen_rtx_IOR (DImode, copy_rtx (dest),
7942 GEN_INT (ud1)));
7944 else if (ud3 == 0 && ud4 == 0)
7946 gcc_assert (ud2 & 0x8000);
7947 emit_move_insn (dest, GEN_INT (((ud2 << 16) ^ 0x80000000)
7948 - 0x80000000));
7949 if (ud1 != 0)
7950 emit_move_insn (copy_rtx (dest),
7951 gen_rtx_IOR (DImode, copy_rtx (dest),
7952 GEN_INT (ud1)));
7953 emit_move_insn (copy_rtx (dest),
7954 gen_rtx_ZERO_EXTEND (DImode,
7955 gen_lowpart (SImode,
7956 copy_rtx (dest))));
7958 else if ((ud4 == 0xffff && (ud3 & 0x8000))
7959 || (ud4 == 0 && ! (ud3 & 0x8000)))
7961 emit_move_insn (dest, GEN_INT (((ud3 << 16) ^ 0x80000000)
7962 - 0x80000000));
7963 if (ud2 != 0)
7964 emit_move_insn (copy_rtx (dest),
7965 gen_rtx_IOR (DImode, copy_rtx (dest),
7966 GEN_INT (ud2)));
7967 emit_move_insn (copy_rtx (dest),
7968 gen_rtx_ASHIFT (DImode, copy_rtx (dest),
7969 GEN_INT (16)));
7970 if (ud1 != 0)
7971 emit_move_insn (copy_rtx (dest),
7972 gen_rtx_IOR (DImode, copy_rtx (dest),
7973 GEN_INT (ud1)));
7975 else
7977 emit_move_insn (dest, GEN_INT (((ud4 << 16) ^ 0x80000000)
7978 - 0x80000000));
7979 if (ud3 != 0)
7980 emit_move_insn (copy_rtx (dest),
7981 gen_rtx_IOR (DImode, copy_rtx (dest),
7982 GEN_INT (ud3)));
7984 emit_move_insn (copy_rtx (dest),
7985 gen_rtx_ASHIFT (DImode, copy_rtx (dest),
7986 GEN_INT (32)));
7987 if (ud2 != 0)
7988 emit_move_insn (copy_rtx (dest),
7989 gen_rtx_IOR (DImode, copy_rtx (dest),
7990 GEN_INT (ud2 << 16)));
7991 if (ud1 != 0)
7992 emit_move_insn (copy_rtx (dest),
7993 gen_rtx_IOR (DImode, copy_rtx (dest),
7994 GEN_INT (ud1)));
7997 return dest;
8000 /* Helper for the following. Get rid of [r+r] memory refs
8001 in cases where it won't work (TImode, TFmode, TDmode, PTImode). */
8003 static void
8004 rs6000_eliminate_indexed_memrefs (rtx operands[2])
8006 if (reload_in_progress)
8007 return;
8009 if (GET_CODE (operands[0]) == MEM
8010 && GET_CODE (XEXP (operands[0], 0)) != REG
8011 && ! legitimate_constant_pool_address_p (XEXP (operands[0], 0),
8012 GET_MODE (operands[0]), false))
8013 operands[0]
8014 = replace_equiv_address (operands[0],
8015 copy_addr_to_reg (XEXP (operands[0], 0)));
8017 if (GET_CODE (operands[1]) == MEM
8018 && GET_CODE (XEXP (operands[1], 0)) != REG
8019 && ! legitimate_constant_pool_address_p (XEXP (operands[1], 0),
8020 GET_MODE (operands[1]), false))
8021 operands[1]
8022 = replace_equiv_address (operands[1],
8023 copy_addr_to_reg (XEXP (operands[1], 0)));
8026 /* Generate a vector of constants to permute MODE for a little-endian
8027 storage operation by swapping the two halves of a vector. */
8028 static rtvec
8029 rs6000_const_vec (enum machine_mode mode)
8031 int i, subparts;
8032 rtvec v;
8034 switch (mode)
8036 case V1TImode:
8037 subparts = 1;
8038 break;
8039 case V2DFmode:
8040 case V2DImode:
8041 subparts = 2;
8042 break;
8043 case V4SFmode:
8044 case V4SImode:
8045 subparts = 4;
8046 break;
8047 case V8HImode:
8048 subparts = 8;
8049 break;
8050 case V16QImode:
8051 subparts = 16;
8052 break;
8053 default:
8054 gcc_unreachable();
8057 v = rtvec_alloc (subparts);
8059 for (i = 0; i < subparts / 2; ++i)
8060 RTVEC_ELT (v, i) = gen_rtx_CONST_INT (DImode, i + subparts / 2);
8061 for (i = subparts / 2; i < subparts; ++i)
8062 RTVEC_ELT (v, i) = gen_rtx_CONST_INT (DImode, i - subparts / 2);
8064 return v;
8067 /* Generate a permute rtx that represents an lxvd2x, stxvd2x, or xxpermdi
8068 for a VSX load or store operation. */
8070 rs6000_gen_le_vsx_permute (rtx source, enum machine_mode mode)
8072 rtx par = gen_rtx_PARALLEL (VOIDmode, rs6000_const_vec (mode));
8073 return gen_rtx_VEC_SELECT (mode, source, par);
8076 /* Emit a little-endian load from vector memory location SOURCE to VSX
8077 register DEST in mode MODE. The load is done with two permuting
8078 insn's that represent an lxvd2x and xxpermdi. */
8079 void
8080 rs6000_emit_le_vsx_load (rtx dest, rtx source, enum machine_mode mode)
8082 rtx tmp, permute_mem, permute_reg;
8084 /* Use V2DImode to do swaps of types with 128-bit scalare parts (TImode,
8085 V1TImode). */
8086 if (mode == TImode || mode == V1TImode)
8088 mode = V2DImode;
8089 dest = gen_lowpart (V2DImode, dest);
8090 source = adjust_address (source, V2DImode, 0);
8093 tmp = can_create_pseudo_p () ? gen_reg_rtx_and_attrs (dest) : dest;
8094 permute_mem = rs6000_gen_le_vsx_permute (source, mode);
8095 permute_reg = rs6000_gen_le_vsx_permute (tmp, mode);
8096 emit_insn (gen_rtx_SET (VOIDmode, tmp, permute_mem));
8097 emit_insn (gen_rtx_SET (VOIDmode, dest, permute_reg));
8100 /* Emit a little-endian store to vector memory location DEST from VSX
8101 register SOURCE in mode MODE. The store is done with two permuting
8102 insn's that represent an xxpermdi and an stxvd2x. */
8103 void
8104 rs6000_emit_le_vsx_store (rtx dest, rtx source, enum machine_mode mode)
8106 rtx tmp, permute_src, permute_tmp;
8108 /* Use V2DImode to do swaps of types with 128-bit scalare parts (TImode,
8109 V1TImode). */
8110 if (mode == TImode || mode == V1TImode)
8112 mode = V2DImode;
8113 dest = adjust_address (dest, V2DImode, 0);
8114 source = gen_lowpart (V2DImode, source);
8117 tmp = can_create_pseudo_p () ? gen_reg_rtx_and_attrs (source) : source;
8118 permute_src = rs6000_gen_le_vsx_permute (source, mode);
8119 permute_tmp = rs6000_gen_le_vsx_permute (tmp, mode);
8120 emit_insn (gen_rtx_SET (VOIDmode, tmp, permute_src));
8121 emit_insn (gen_rtx_SET (VOIDmode, dest, permute_tmp));
8124 /* Emit a sequence representing a little-endian VSX load or store,
8125 moving data from SOURCE to DEST in mode MODE. This is done
8126 separately from rs6000_emit_move to ensure it is called only
8127 during expand. LE VSX loads and stores introduced later are
8128 handled with a split. The expand-time RTL generation allows
8129 us to optimize away redundant pairs of register-permutes. */
8130 void
8131 rs6000_emit_le_vsx_move (rtx dest, rtx source, enum machine_mode mode)
8133 gcc_assert (!BYTES_BIG_ENDIAN
8134 && VECTOR_MEM_VSX_P (mode)
8135 && !gpr_or_gpr_p (dest, source)
8136 && (MEM_P (source) ^ MEM_P (dest)));
8138 if (MEM_P (source))
8140 gcc_assert (REG_P (dest) || GET_CODE (dest) == SUBREG);
8141 rs6000_emit_le_vsx_load (dest, source, mode);
8143 else
8145 if (!REG_P (source))
8146 source = force_reg (mode, source);
8147 rs6000_emit_le_vsx_store (dest, source, mode);
8151 /* Emit a move from SOURCE to DEST in mode MODE. */
8152 void
8153 rs6000_emit_move (rtx dest, rtx source, enum machine_mode mode)
8155 rtx operands[2];
8156 operands[0] = dest;
8157 operands[1] = source;
8159 if (TARGET_DEBUG_ADDR)
8161 fprintf (stderr,
8162 "\nrs6000_emit_move: mode = %s, reload_in_progress = %d, "
8163 "reload_completed = %d, can_create_pseudos = %d.\ndest:\n",
8164 GET_MODE_NAME (mode),
8165 reload_in_progress,
8166 reload_completed,
8167 can_create_pseudo_p ());
8168 debug_rtx (dest);
8169 fprintf (stderr, "source:\n");
8170 debug_rtx (source);
8173 /* Sanity checks. Check that we get CONST_DOUBLE only when we should. */
8174 if (CONST_WIDE_INT_P (operands[1])
8175 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
8177 /* This should be fixed with the introduction of CONST_WIDE_INT. */
8178 gcc_unreachable ();
8181 /* Check if GCC is setting up a block move that will end up using FP
8182 registers as temporaries. We must make sure this is acceptable. */
8183 if (GET_CODE (operands[0]) == MEM
8184 && GET_CODE (operands[1]) == MEM
8185 && mode == DImode
8186 && (SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[0]))
8187 || SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[1])))
8188 && ! (SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[0]) > 32
8189 ? 32 : MEM_ALIGN (operands[0])))
8190 || SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[1]) > 32
8191 ? 32
8192 : MEM_ALIGN (operands[1]))))
8193 && ! MEM_VOLATILE_P (operands [0])
8194 && ! MEM_VOLATILE_P (operands [1]))
8196 emit_move_insn (adjust_address (operands[0], SImode, 0),
8197 adjust_address (operands[1], SImode, 0));
8198 emit_move_insn (adjust_address (copy_rtx (operands[0]), SImode, 4),
8199 adjust_address (copy_rtx (operands[1]), SImode, 4));
8200 return;
8203 if (can_create_pseudo_p () && GET_CODE (operands[0]) == MEM
8204 && !gpc_reg_operand (operands[1], mode))
8205 operands[1] = force_reg (mode, operands[1]);
8207 /* Recognize the case where operand[1] is a reference to thread-local
8208 data and load its address to a register. */
8209 if (rs6000_tls_referenced_p (operands[1]))
8211 enum tls_model model;
8212 rtx tmp = operands[1];
8213 rtx addend = NULL;
8215 if (GET_CODE (tmp) == CONST && GET_CODE (XEXP (tmp, 0)) == PLUS)
8217 addend = XEXP (XEXP (tmp, 0), 1);
8218 tmp = XEXP (XEXP (tmp, 0), 0);
8221 gcc_assert (GET_CODE (tmp) == SYMBOL_REF);
8222 model = SYMBOL_REF_TLS_MODEL (tmp);
8223 gcc_assert (model != 0);
8225 tmp = rs6000_legitimize_tls_address (tmp, model);
8226 if (addend)
8228 tmp = gen_rtx_PLUS (mode, tmp, addend);
8229 tmp = force_operand (tmp, operands[0]);
8231 operands[1] = tmp;
8234 /* Handle the case where reload calls us with an invalid address. */
8235 if (reload_in_progress && mode == Pmode
8236 && (! general_operand (operands[1], mode)
8237 || ! nonimmediate_operand (operands[0], mode)))
8238 goto emit_set;
8240 /* 128-bit constant floating-point values on Darwin should really be
8241 loaded as two parts. */
8242 if (!TARGET_IEEEQUAD && TARGET_LONG_DOUBLE_128
8243 && mode == TFmode && GET_CODE (operands[1]) == CONST_DOUBLE)
8245 rs6000_emit_move (simplify_gen_subreg (DFmode, operands[0], mode, 0),
8246 simplify_gen_subreg (DFmode, operands[1], mode, 0),
8247 DFmode);
8248 rs6000_emit_move (simplify_gen_subreg (DFmode, operands[0], mode,
8249 GET_MODE_SIZE (DFmode)),
8250 simplify_gen_subreg (DFmode, operands[1], mode,
8251 GET_MODE_SIZE (DFmode)),
8252 DFmode);
8253 return;
8256 if (reload_in_progress && cfun->machine->sdmode_stack_slot != NULL_RTX)
8257 cfun->machine->sdmode_stack_slot =
8258 eliminate_regs (cfun->machine->sdmode_stack_slot, VOIDmode, NULL_RTX);
8261 if (lra_in_progress
8262 && mode == SDmode
8263 && REG_P (operands[0]) && REGNO (operands[0]) >= FIRST_PSEUDO_REGISTER
8264 && reg_preferred_class (REGNO (operands[0])) == NO_REGS
8265 && (REG_P (operands[1])
8266 || (GET_CODE (operands[1]) == SUBREG
8267 && REG_P (SUBREG_REG (operands[1])))))
8269 int regno = REGNO (GET_CODE (operands[1]) == SUBREG
8270 ? SUBREG_REG (operands[1]) : operands[1]);
8271 enum reg_class cl;
8273 if (regno >= FIRST_PSEUDO_REGISTER)
8275 cl = reg_preferred_class (regno);
8276 gcc_assert (cl != NO_REGS);
8277 regno = ira_class_hard_regs[cl][0];
8279 if (FP_REGNO_P (regno))
8281 if (GET_MODE (operands[0]) != DDmode)
8282 operands[0] = gen_rtx_SUBREG (DDmode, operands[0], 0);
8283 emit_insn (gen_movsd_store (operands[0], operands[1]));
8285 else if (INT_REGNO_P (regno))
8286 emit_insn (gen_movsd_hardfloat (operands[0], operands[1]));
8287 else
8288 gcc_unreachable();
8289 return;
8291 if (lra_in_progress
8292 && mode == SDmode
8293 && (REG_P (operands[0])
8294 || (GET_CODE (operands[0]) == SUBREG
8295 && REG_P (SUBREG_REG (operands[0]))))
8296 && REG_P (operands[1]) && REGNO (operands[1]) >= FIRST_PSEUDO_REGISTER
8297 && reg_preferred_class (REGNO (operands[1])) == NO_REGS)
8299 int regno = REGNO (GET_CODE (operands[0]) == SUBREG
8300 ? SUBREG_REG (operands[0]) : operands[0]);
8301 enum reg_class cl;
8303 if (regno >= FIRST_PSEUDO_REGISTER)
8305 cl = reg_preferred_class (regno);
8306 gcc_assert (cl != NO_REGS);
8307 regno = ira_class_hard_regs[cl][0];
8309 if (FP_REGNO_P (regno))
8311 if (GET_MODE (operands[1]) != DDmode)
8312 operands[1] = gen_rtx_SUBREG (DDmode, operands[1], 0);
8313 emit_insn (gen_movsd_load (operands[0], operands[1]));
8315 else if (INT_REGNO_P (regno))
8316 emit_insn (gen_movsd_hardfloat (operands[0], operands[1]));
8317 else
8318 gcc_unreachable();
8319 return;
8322 if (reload_in_progress
8323 && mode == SDmode
8324 && cfun->machine->sdmode_stack_slot != NULL_RTX
8325 && MEM_P (operands[0])
8326 && rtx_equal_p (operands[0], cfun->machine->sdmode_stack_slot)
8327 && REG_P (operands[1]))
8329 if (FP_REGNO_P (REGNO (operands[1])))
8331 rtx mem = adjust_address_nv (operands[0], DDmode, 0);
8332 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
8333 emit_insn (gen_movsd_store (mem, operands[1]));
8335 else if (INT_REGNO_P (REGNO (operands[1])))
8337 rtx mem = operands[0];
8338 if (BYTES_BIG_ENDIAN)
8339 mem = adjust_address_nv (mem, mode, 4);
8340 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
8341 emit_insn (gen_movsd_hardfloat (mem, operands[1]));
8343 else
8344 gcc_unreachable();
8345 return;
8347 if (reload_in_progress
8348 && mode == SDmode
8349 && REG_P (operands[0])
8350 && MEM_P (operands[1])
8351 && cfun->machine->sdmode_stack_slot != NULL_RTX
8352 && rtx_equal_p (operands[1], cfun->machine->sdmode_stack_slot))
8354 if (FP_REGNO_P (REGNO (operands[0])))
8356 rtx mem = adjust_address_nv (operands[1], DDmode, 0);
8357 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
8358 emit_insn (gen_movsd_load (operands[0], mem));
8360 else if (INT_REGNO_P (REGNO (operands[0])))
8362 rtx mem = operands[1];
8363 if (BYTES_BIG_ENDIAN)
8364 mem = adjust_address_nv (mem, mode, 4);
8365 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
8366 emit_insn (gen_movsd_hardfloat (operands[0], mem));
8368 else
8369 gcc_unreachable();
8370 return;
8373 /* FIXME: In the long term, this switch statement should go away
8374 and be replaced by a sequence of tests based on things like
8375 mode == Pmode. */
8376 switch (mode)
8378 case HImode:
8379 case QImode:
8380 if (CONSTANT_P (operands[1])
8381 && GET_CODE (operands[1]) != CONST_INT)
8382 operands[1] = force_const_mem (mode, operands[1]);
8383 break;
8385 case TFmode:
8386 case TDmode:
8387 rs6000_eliminate_indexed_memrefs (operands);
8388 /* fall through */
8390 case DFmode:
8391 case DDmode:
8392 case SFmode:
8393 case SDmode:
8394 if (CONSTANT_P (operands[1])
8395 && ! easy_fp_constant (operands[1], mode))
8396 operands[1] = force_const_mem (mode, operands[1]);
8397 break;
8399 case V16QImode:
8400 case V8HImode:
8401 case V4SFmode:
8402 case V4SImode:
8403 case V4HImode:
8404 case V2SFmode:
8405 case V2SImode:
8406 case V1DImode:
8407 case V2DFmode:
8408 case V2DImode:
8409 case V1TImode:
8410 if (CONSTANT_P (operands[1])
8411 && !easy_vector_constant (operands[1], mode))
8412 operands[1] = force_const_mem (mode, operands[1]);
8413 break;
8415 case SImode:
8416 case DImode:
8417 /* Use default pattern for address of ELF small data */
8418 if (TARGET_ELF
8419 && mode == Pmode
8420 && DEFAULT_ABI == ABI_V4
8421 && (GET_CODE (operands[1]) == SYMBOL_REF
8422 || GET_CODE (operands[1]) == CONST)
8423 && small_data_operand (operands[1], mode))
8425 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
8426 return;
8429 if (DEFAULT_ABI == ABI_V4
8430 && mode == Pmode && mode == SImode
8431 && flag_pic == 1 && got_operand (operands[1], mode))
8433 emit_insn (gen_movsi_got (operands[0], operands[1]));
8434 return;
8437 if ((TARGET_ELF || DEFAULT_ABI == ABI_DARWIN)
8438 && TARGET_NO_TOC
8439 && ! flag_pic
8440 && mode == Pmode
8441 && CONSTANT_P (operands[1])
8442 && GET_CODE (operands[1]) != HIGH
8443 && GET_CODE (operands[1]) != CONST_INT)
8445 rtx target = (!can_create_pseudo_p ()
8446 ? operands[0]
8447 : gen_reg_rtx (mode));
8449 /* If this is a function address on -mcall-aixdesc,
8450 convert it to the address of the descriptor. */
8451 if (DEFAULT_ABI == ABI_AIX
8452 && GET_CODE (operands[1]) == SYMBOL_REF
8453 && XSTR (operands[1], 0)[0] == '.')
8455 const char *name = XSTR (operands[1], 0);
8456 rtx new_ref;
8457 while (*name == '.')
8458 name++;
8459 new_ref = gen_rtx_SYMBOL_REF (Pmode, name);
8460 CONSTANT_POOL_ADDRESS_P (new_ref)
8461 = CONSTANT_POOL_ADDRESS_P (operands[1]);
8462 SYMBOL_REF_FLAGS (new_ref) = SYMBOL_REF_FLAGS (operands[1]);
8463 SYMBOL_REF_USED (new_ref) = SYMBOL_REF_USED (operands[1]);
8464 SYMBOL_REF_DATA (new_ref) = SYMBOL_REF_DATA (operands[1]);
8465 operands[1] = new_ref;
8468 if (DEFAULT_ABI == ABI_DARWIN)
8470 #if TARGET_MACHO
8471 if (MACHO_DYNAMIC_NO_PIC_P)
8473 /* Take care of any required data indirection. */
8474 operands[1] = rs6000_machopic_legitimize_pic_address (
8475 operands[1], mode, operands[0]);
8476 if (operands[0] != operands[1])
8477 emit_insn (gen_rtx_SET (VOIDmode,
8478 operands[0], operands[1]));
8479 return;
8481 #endif
8482 emit_insn (gen_macho_high (target, operands[1]));
8483 emit_insn (gen_macho_low (operands[0], target, operands[1]));
8484 return;
8487 emit_insn (gen_elf_high (target, operands[1]));
8488 emit_insn (gen_elf_low (operands[0], target, operands[1]));
8489 return;
8492 /* If this is a SYMBOL_REF that refers to a constant pool entry,
8493 and we have put it in the TOC, we just need to make a TOC-relative
8494 reference to it. */
8495 if (TARGET_TOC
8496 && GET_CODE (operands[1]) == SYMBOL_REF
8497 && use_toc_relative_ref (operands[1]))
8498 operands[1] = create_TOC_reference (operands[1], operands[0]);
8499 else if (mode == Pmode
8500 && CONSTANT_P (operands[1])
8501 && GET_CODE (operands[1]) != HIGH
8502 && ((GET_CODE (operands[1]) != CONST_INT
8503 && ! easy_fp_constant (operands[1], mode))
8504 || (GET_CODE (operands[1]) == CONST_INT
8505 && (num_insns_constant (operands[1], mode)
8506 > (TARGET_CMODEL != CMODEL_SMALL ? 3 : 2)))
8507 || (GET_CODE (operands[0]) == REG
8508 && FP_REGNO_P (REGNO (operands[0]))))
8509 && !toc_relative_expr_p (operands[1], false)
8510 && (TARGET_CMODEL == CMODEL_SMALL
8511 || can_create_pseudo_p ()
8512 || (REG_P (operands[0])
8513 && INT_REG_OK_FOR_BASE_P (operands[0], true))))
8516 #if TARGET_MACHO
8517 /* Darwin uses a special PIC legitimizer. */
8518 if (DEFAULT_ABI == ABI_DARWIN && MACHOPIC_INDIRECT)
8520 operands[1] =
8521 rs6000_machopic_legitimize_pic_address (operands[1], mode,
8522 operands[0]);
8523 if (operands[0] != operands[1])
8524 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
8525 return;
8527 #endif
8529 /* If we are to limit the number of things we put in the TOC and
8530 this is a symbol plus a constant we can add in one insn,
8531 just put the symbol in the TOC and add the constant. Don't do
8532 this if reload is in progress. */
8533 if (GET_CODE (operands[1]) == CONST
8534 && TARGET_NO_SUM_IN_TOC && ! reload_in_progress
8535 && GET_CODE (XEXP (operands[1], 0)) == PLUS
8536 && add_operand (XEXP (XEXP (operands[1], 0), 1), mode)
8537 && (GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == LABEL_REF
8538 || GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == SYMBOL_REF)
8539 && ! side_effects_p (operands[0]))
8541 rtx sym =
8542 force_const_mem (mode, XEXP (XEXP (operands[1], 0), 0));
8543 rtx other = XEXP (XEXP (operands[1], 0), 1);
8545 sym = force_reg (mode, sym);
8546 emit_insn (gen_add3_insn (operands[0], sym, other));
8547 return;
8550 operands[1] = force_const_mem (mode, operands[1]);
8552 if (TARGET_TOC
8553 && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
8554 && constant_pool_expr_p (XEXP (operands[1], 0))
8555 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (
8556 get_pool_constant (XEXP (operands[1], 0)),
8557 get_pool_mode (XEXP (operands[1], 0))))
8559 rtx tocref = create_TOC_reference (XEXP (operands[1], 0),
8560 operands[0]);
8561 operands[1] = gen_const_mem (mode, tocref);
8562 set_mem_alias_set (operands[1], get_TOC_alias_set ());
8565 break;
8567 case TImode:
8568 if (!VECTOR_MEM_VSX_P (TImode))
8569 rs6000_eliminate_indexed_memrefs (operands);
8570 break;
8572 case PTImode:
8573 rs6000_eliminate_indexed_memrefs (operands);
8574 break;
8576 default:
8577 fatal_insn ("bad move", gen_rtx_SET (VOIDmode, dest, source));
8580 /* Above, we may have called force_const_mem which may have returned
8581 an invalid address. If we can, fix this up; otherwise, reload will
8582 have to deal with it. */
8583 if (GET_CODE (operands[1]) == MEM && ! reload_in_progress)
8584 operands[1] = validize_mem (operands[1]);
8586 emit_set:
8587 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
8590 /* Return true if a structure, union or array containing FIELD should be
8591 accessed using `BLKMODE'.
8593 For the SPE, simd types are V2SI, and gcc can be tempted to put the
8594 entire thing in a DI and use subregs to access the internals.
8595 store_bit_field() will force (subreg:DI (reg:V2SI x))'s to the
8596 back-end. Because a single GPR can hold a V2SI, but not a DI, the
8597 best thing to do is set structs to BLKmode and avoid Severe Tire
8598 Damage.
8600 On e500 v2, DF and DI modes suffer from the same anomaly. DF can
8601 fit into 1, whereas DI still needs two. */
8603 static bool
8604 rs6000_member_type_forces_blk (const_tree field, enum machine_mode mode)
8606 return ((TARGET_SPE && TREE_CODE (TREE_TYPE (field)) == VECTOR_TYPE)
8607 || (TARGET_E500_DOUBLE && mode == DFmode));
8610 /* Nonzero if we can use a floating-point register to pass this arg. */
8611 #define USE_FP_FOR_ARG_P(CUM,MODE) \
8612 (SCALAR_FLOAT_MODE_P (MODE) \
8613 && (CUM)->fregno <= FP_ARG_MAX_REG \
8614 && TARGET_HARD_FLOAT && TARGET_FPRS)
8616 /* Nonzero if we can use an AltiVec register to pass this arg. */
8617 #define USE_ALTIVEC_FOR_ARG_P(CUM,MODE,NAMED) \
8618 (ALTIVEC_OR_VSX_VECTOR_MODE (MODE) \
8619 && (CUM)->vregno <= ALTIVEC_ARG_MAX_REG \
8620 && TARGET_ALTIVEC_ABI \
8621 && (NAMED))
8623 /* Walk down the type tree of TYPE counting consecutive base elements.
8624 If *MODEP is VOIDmode, then set it to the first valid floating point
8625 or vector type. If a non-floating point or vector type is found, or
8626 if a floating point or vector type that doesn't match a non-VOIDmode
8627 *MODEP is found, then return -1, otherwise return the count in the
8628 sub-tree. */
8630 static int
8631 rs6000_aggregate_candidate (const_tree type, enum machine_mode *modep)
8633 enum machine_mode mode;
8634 HOST_WIDE_INT size;
8636 switch (TREE_CODE (type))
8638 case REAL_TYPE:
8639 mode = TYPE_MODE (type);
8640 if (!SCALAR_FLOAT_MODE_P (mode))
8641 return -1;
8643 if (*modep == VOIDmode)
8644 *modep = mode;
8646 if (*modep == mode)
8647 return 1;
8649 break;
8651 case COMPLEX_TYPE:
8652 mode = TYPE_MODE (TREE_TYPE (type));
8653 if (!SCALAR_FLOAT_MODE_P (mode))
8654 return -1;
8656 if (*modep == VOIDmode)
8657 *modep = mode;
8659 if (*modep == mode)
8660 return 2;
8662 break;
8664 case VECTOR_TYPE:
8665 if (!TARGET_ALTIVEC_ABI || !TARGET_ALTIVEC)
8666 return -1;
8668 /* Use V4SImode as representative of all 128-bit vector types. */
8669 size = int_size_in_bytes (type);
8670 switch (size)
8672 case 16:
8673 mode = V4SImode;
8674 break;
8675 default:
8676 return -1;
8679 if (*modep == VOIDmode)
8680 *modep = mode;
8682 /* Vector modes are considered to be opaque: two vectors are
8683 equivalent for the purposes of being homogeneous aggregates
8684 if they are the same size. */
8685 if (*modep == mode)
8686 return 1;
8688 break;
8690 case ARRAY_TYPE:
8692 int count;
8693 tree index = TYPE_DOMAIN (type);
8695 /* Can't handle incomplete types nor sizes that are not
8696 fixed. */
8697 if (!COMPLETE_TYPE_P (type)
8698 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
8699 return -1;
8701 count = rs6000_aggregate_candidate (TREE_TYPE (type), modep);
8702 if (count == -1
8703 || !index
8704 || !TYPE_MAX_VALUE (index)
8705 || !tree_fits_uhwi_p (TYPE_MAX_VALUE (index))
8706 || !TYPE_MIN_VALUE (index)
8707 || !tree_fits_uhwi_p (TYPE_MIN_VALUE (index))
8708 || count < 0)
8709 return -1;
8711 count *= (1 + tree_to_uhwi (TYPE_MAX_VALUE (index))
8712 - tree_to_uhwi (TYPE_MIN_VALUE (index)));
8714 /* There must be no padding. */
8715 if (wi::ne_p (TYPE_SIZE (type), count * GET_MODE_BITSIZE (*modep)))
8716 return -1;
8718 return count;
8721 case RECORD_TYPE:
8723 int count = 0;
8724 int sub_count;
8725 tree field;
8727 /* Can't handle incomplete types nor sizes that are not
8728 fixed. */
8729 if (!COMPLETE_TYPE_P (type)
8730 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
8731 return -1;
8733 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
8735 if (TREE_CODE (field) != FIELD_DECL)
8736 continue;
8738 sub_count = rs6000_aggregate_candidate (TREE_TYPE (field), modep);
8739 if (sub_count < 0)
8740 return -1;
8741 count += sub_count;
8744 /* There must be no padding. */
8745 if (wi::ne_p (TYPE_SIZE (type), count * GET_MODE_BITSIZE (*modep)))
8746 return -1;
8748 return count;
8751 case UNION_TYPE:
8752 case QUAL_UNION_TYPE:
8754 /* These aren't very interesting except in a degenerate case. */
8755 int count = 0;
8756 int sub_count;
8757 tree field;
8759 /* Can't handle incomplete types nor sizes that are not
8760 fixed. */
8761 if (!COMPLETE_TYPE_P (type)
8762 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
8764 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
8766 if (TREE_CODE (field) != FIELD_DECL)
8767 continue;
8769 sub_count = rs6000_aggregate_candidate (TREE_TYPE (field), modep);
8770 if (sub_count < 0)
8771 return -1;
8772 count = count > sub_count ? count : sub_count;
8775 /* There must be no padding. */
8776 if (wi::ne_p (TYPE_SIZE (type), count * GET_MODE_BITSIZE (*modep)))
8777 return -1;
8779 return count;
8782 default:
8783 break;
8786 return -1;
8789 /* If an argument, whose type is described by TYPE and MODE, is a homogeneous
8790 float or vector aggregate that shall be passed in FP/vector registers
8791 according to the ELFv2 ABI, return the homogeneous element mode in
8792 *ELT_MODE and the number of elements in *N_ELTS, and return TRUE.
8794 Otherwise, set *ELT_MODE to MODE and *N_ELTS to 1, and return FALSE. */
8796 static bool
8797 rs6000_discover_homogeneous_aggregate (enum machine_mode mode, const_tree type,
8798 enum machine_mode *elt_mode,
8799 int *n_elts)
8801 /* Note that we do not accept complex types at the top level as
8802 homogeneous aggregates; these types are handled via the
8803 targetm.calls.split_complex_arg mechanism. Complex types
8804 can be elements of homogeneous aggregates, however. */
8805 if (DEFAULT_ABI == ABI_ELFv2 && type && AGGREGATE_TYPE_P (type))
8807 enum machine_mode field_mode = VOIDmode;
8808 int field_count = rs6000_aggregate_candidate (type, &field_mode);
8810 if (field_count > 0)
8812 int n_regs = (SCALAR_FLOAT_MODE_P (field_mode)?
8813 (GET_MODE_SIZE (field_mode) + 7) >> 3 : 1);
8815 /* The ELFv2 ABI allows homogeneous aggregates to occupy
8816 up to AGGR_ARG_NUM_REG registers. */
8817 if (field_count * n_regs <= AGGR_ARG_NUM_REG)
8819 if (elt_mode)
8820 *elt_mode = field_mode;
8821 if (n_elts)
8822 *n_elts = field_count;
8823 return true;
8828 if (elt_mode)
8829 *elt_mode = mode;
8830 if (n_elts)
8831 *n_elts = 1;
8832 return false;
8835 /* Return a nonzero value to say to return the function value in
8836 memory, just as large structures are always returned. TYPE will be
8837 the data type of the value, and FNTYPE will be the type of the
8838 function doing the returning, or @code{NULL} for libcalls.
8840 The AIX ABI for the RS/6000 specifies that all structures are
8841 returned in memory. The Darwin ABI does the same.
8843 For the Darwin 64 Bit ABI, a function result can be returned in
8844 registers or in memory, depending on the size of the return data
8845 type. If it is returned in registers, the value occupies the same
8846 registers as it would if it were the first and only function
8847 argument. Otherwise, the function places its result in memory at
8848 the location pointed to by GPR3.
8850 The SVR4 ABI specifies that structures <= 8 bytes are returned in r3/r4,
8851 but a draft put them in memory, and GCC used to implement the draft
8852 instead of the final standard. Therefore, aix_struct_return
8853 controls this instead of DEFAULT_ABI; V.4 targets needing backward
8854 compatibility can change DRAFT_V4_STRUCT_RET to override the
8855 default, and -m switches get the final word. See
8856 rs6000_option_override_internal for more details.
8858 The PPC32 SVR4 ABI uses IEEE double extended for long double, if 128-bit
8859 long double support is enabled. These values are returned in memory.
8861 int_size_in_bytes returns -1 for variable size objects, which go in
8862 memory always. The cast to unsigned makes -1 > 8. */
8864 static bool
8865 rs6000_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
8867 /* For the Darwin64 ABI, test if we can fit the return value in regs. */
8868 if (TARGET_MACHO
8869 && rs6000_darwin64_abi
8870 && TREE_CODE (type) == RECORD_TYPE
8871 && int_size_in_bytes (type) > 0)
8873 CUMULATIVE_ARGS valcum;
8874 rtx valret;
8876 valcum.words = 0;
8877 valcum.fregno = FP_ARG_MIN_REG;
8878 valcum.vregno = ALTIVEC_ARG_MIN_REG;
8879 /* Do a trial code generation as if this were going to be passed
8880 as an argument; if any part goes in memory, we return NULL. */
8881 valret = rs6000_darwin64_record_arg (&valcum, type, true, true);
8882 if (valret)
8883 return false;
8884 /* Otherwise fall through to more conventional ABI rules. */
8887 /* The ELFv2 ABI returns homogeneous VFP aggregates in registers */
8888 if (rs6000_discover_homogeneous_aggregate (TYPE_MODE (type), type,
8889 NULL, NULL))
8890 return false;
8892 /* The ELFv2 ABI returns aggregates up to 16B in registers */
8893 if (DEFAULT_ABI == ABI_ELFv2 && AGGREGATE_TYPE_P (type)
8894 && (unsigned HOST_WIDE_INT) int_size_in_bytes (type) <= 16)
8895 return false;
8897 if (AGGREGATE_TYPE_P (type)
8898 && (aix_struct_return
8899 || (unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 8))
8900 return true;
8902 /* Allow -maltivec -mabi=no-altivec without warning. Altivec vector
8903 modes only exist for GCC vector types if -maltivec. */
8904 if (TARGET_32BIT && !TARGET_ALTIVEC_ABI
8905 && ALTIVEC_VECTOR_MODE (TYPE_MODE (type)))
8906 return false;
8908 /* Return synthetic vectors in memory. */
8909 if (TREE_CODE (type) == VECTOR_TYPE
8910 && int_size_in_bytes (type) > (TARGET_ALTIVEC_ABI ? 16 : 8))
8912 static bool warned_for_return_big_vectors = false;
8913 if (!warned_for_return_big_vectors)
8915 warning (0, "GCC vector returned by reference: "
8916 "non-standard ABI extension with no compatibility guarantee");
8917 warned_for_return_big_vectors = true;
8919 return true;
8922 if (DEFAULT_ABI == ABI_V4 && TARGET_IEEEQUAD && TYPE_MODE (type) == TFmode)
8923 return true;
8925 return false;
8928 /* Specify whether values returned in registers should be at the most
8929 significant end of a register. We want aggregates returned by
8930 value to match the way aggregates are passed to functions. */
8932 static bool
8933 rs6000_return_in_msb (const_tree valtype)
8935 return (DEFAULT_ABI == ABI_ELFv2
8936 && BYTES_BIG_ENDIAN
8937 && AGGREGATE_TYPE_P (valtype)
8938 && FUNCTION_ARG_PADDING (TYPE_MODE (valtype), valtype) == upward);
8941 #ifdef HAVE_AS_GNU_ATTRIBUTE
8942 /* Return TRUE if a call to function FNDECL may be one that
8943 potentially affects the function calling ABI of the object file. */
8945 static bool
8946 call_ABI_of_interest (tree fndecl)
8948 if (cgraph_state == CGRAPH_STATE_EXPANSION)
8950 struct cgraph_node *c_node;
8952 /* Libcalls are always interesting. */
8953 if (fndecl == NULL_TREE)
8954 return true;
8956 /* Any call to an external function is interesting. */
8957 if (DECL_EXTERNAL (fndecl))
8958 return true;
8960 /* Interesting functions that we are emitting in this object file. */
8961 c_node = cgraph_get_node (fndecl);
8962 c_node = cgraph_function_or_thunk_node (c_node, NULL);
8963 return !cgraph_only_called_directly_p (c_node);
8965 return false;
8967 #endif
8969 /* Initialize a variable CUM of type CUMULATIVE_ARGS
8970 for a call to a function whose data type is FNTYPE.
8971 For a library call, FNTYPE is 0 and RETURN_MODE the return value mode.
8973 For incoming args we set the number of arguments in the prototype large
8974 so we never return a PARALLEL. */
8976 void
8977 init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype,
8978 rtx libname ATTRIBUTE_UNUSED, int incoming,
8979 int libcall, int n_named_args,
8980 tree fndecl ATTRIBUTE_UNUSED,
8981 enum machine_mode return_mode ATTRIBUTE_UNUSED)
8983 static CUMULATIVE_ARGS zero_cumulative;
8985 *cum = zero_cumulative;
8986 cum->words = 0;
8987 cum->fregno = FP_ARG_MIN_REG;
8988 cum->vregno = ALTIVEC_ARG_MIN_REG;
8989 cum->prototype = (fntype && prototype_p (fntype));
8990 cum->call_cookie = ((DEFAULT_ABI == ABI_V4 && libcall)
8991 ? CALL_LIBCALL : CALL_NORMAL);
8992 cum->sysv_gregno = GP_ARG_MIN_REG;
8993 cum->stdarg = stdarg_p (fntype);
8995 cum->nargs_prototype = 0;
8996 if (incoming || cum->prototype)
8997 cum->nargs_prototype = n_named_args;
8999 /* Check for a longcall attribute. */
9000 if ((!fntype && rs6000_default_long_calls)
9001 || (fntype
9002 && lookup_attribute ("longcall", TYPE_ATTRIBUTES (fntype))
9003 && !lookup_attribute ("shortcall", TYPE_ATTRIBUTES (fntype))))
9004 cum->call_cookie |= CALL_LONG;
9006 if (TARGET_DEBUG_ARG)
9008 fprintf (stderr, "\ninit_cumulative_args:");
9009 if (fntype)
9011 tree ret_type = TREE_TYPE (fntype);
9012 fprintf (stderr, " ret code = %s,",
9013 get_tree_code_name (TREE_CODE (ret_type)));
9016 if (cum->call_cookie & CALL_LONG)
9017 fprintf (stderr, " longcall,");
9019 fprintf (stderr, " proto = %d, nargs = %d\n",
9020 cum->prototype, cum->nargs_prototype);
9023 #ifdef HAVE_AS_GNU_ATTRIBUTE
9024 if (DEFAULT_ABI == ABI_V4)
9026 cum->escapes = call_ABI_of_interest (fndecl);
9027 if (cum->escapes)
9029 tree return_type;
9031 if (fntype)
9033 return_type = TREE_TYPE (fntype);
9034 return_mode = TYPE_MODE (return_type);
9036 else
9037 return_type = lang_hooks.types.type_for_mode (return_mode, 0);
9039 if (return_type != NULL)
9041 if (TREE_CODE (return_type) == RECORD_TYPE
9042 && TYPE_TRANSPARENT_AGGR (return_type))
9044 return_type = TREE_TYPE (first_field (return_type));
9045 return_mode = TYPE_MODE (return_type);
9047 if (AGGREGATE_TYPE_P (return_type)
9048 && ((unsigned HOST_WIDE_INT) int_size_in_bytes (return_type)
9049 <= 8))
9050 rs6000_returns_struct = true;
9052 if (SCALAR_FLOAT_MODE_P (return_mode))
9053 rs6000_passes_float = true;
9054 else if (ALTIVEC_OR_VSX_VECTOR_MODE (return_mode)
9055 || SPE_VECTOR_MODE (return_mode))
9056 rs6000_passes_vector = true;
9059 #endif
9061 if (fntype
9062 && !TARGET_ALTIVEC
9063 && TARGET_ALTIVEC_ABI
9064 && ALTIVEC_VECTOR_MODE (TYPE_MODE (TREE_TYPE (fntype))))
9066 error ("cannot return value in vector register because"
9067 " altivec instructions are disabled, use -maltivec"
9068 " to enable them");
9072 /* Return true if TYPE must be passed on the stack and not in registers. */
9074 static bool
9075 rs6000_must_pass_in_stack (enum machine_mode mode, const_tree type)
9077 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2 || TARGET_64BIT)
9078 return must_pass_in_stack_var_size (mode, type);
9079 else
9080 return must_pass_in_stack_var_size_or_pad (mode, type);
9083 /* If defined, a C expression which determines whether, and in which
9084 direction, to pad out an argument with extra space. The value
9085 should be of type `enum direction': either `upward' to pad above
9086 the argument, `downward' to pad below, or `none' to inhibit
9087 padding.
9089 For the AIX ABI structs are always stored left shifted in their
9090 argument slot. */
9092 enum direction
9093 function_arg_padding (enum machine_mode mode, const_tree type)
9095 #ifndef AGGREGATE_PADDING_FIXED
9096 #define AGGREGATE_PADDING_FIXED 0
9097 #endif
9098 #ifndef AGGREGATES_PAD_UPWARD_ALWAYS
9099 #define AGGREGATES_PAD_UPWARD_ALWAYS 0
9100 #endif
9102 if (!AGGREGATE_PADDING_FIXED)
9104 /* GCC used to pass structures of the same size as integer types as
9105 if they were in fact integers, ignoring FUNCTION_ARG_PADDING.
9106 i.e. Structures of size 1 or 2 (or 4 when TARGET_64BIT) were
9107 passed padded downward, except that -mstrict-align further
9108 muddied the water in that multi-component structures of 2 and 4
9109 bytes in size were passed padded upward.
9111 The following arranges for best compatibility with previous
9112 versions of gcc, but removes the -mstrict-align dependency. */
9113 if (BYTES_BIG_ENDIAN)
9115 HOST_WIDE_INT size = 0;
9117 if (mode == BLKmode)
9119 if (type && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
9120 size = int_size_in_bytes (type);
9122 else
9123 size = GET_MODE_SIZE (mode);
9125 if (size == 1 || size == 2 || size == 4)
9126 return downward;
9128 return upward;
9131 if (AGGREGATES_PAD_UPWARD_ALWAYS)
9133 if (type != 0 && AGGREGATE_TYPE_P (type))
9134 return upward;
9137 /* Fall back to the default. */
9138 return DEFAULT_FUNCTION_ARG_PADDING (mode, type);
9141 /* If defined, a C expression that gives the alignment boundary, in bits,
9142 of an argument with the specified mode and type. If it is not defined,
9143 PARM_BOUNDARY is used for all arguments.
9145 V.4 wants long longs and doubles to be double word aligned. Just
9146 testing the mode size is a boneheaded way to do this as it means
9147 that other types such as complex int are also double word aligned.
9148 However, we're stuck with this because changing the ABI might break
9149 existing library interfaces.
9151 Doubleword align SPE vectors.
9152 Quadword align Altivec/VSX vectors.
9153 Quadword align large synthetic vector types. */
9155 static unsigned int
9156 rs6000_function_arg_boundary (enum machine_mode mode, const_tree type)
9158 enum machine_mode elt_mode;
9159 int n_elts;
9161 rs6000_discover_homogeneous_aggregate (mode, type, &elt_mode, &n_elts);
9163 if (DEFAULT_ABI == ABI_V4
9164 && (GET_MODE_SIZE (mode) == 8
9165 || (TARGET_HARD_FLOAT
9166 && TARGET_FPRS
9167 && (mode == TFmode || mode == TDmode))))
9168 return 64;
9169 else if (SPE_VECTOR_MODE (mode)
9170 || (type && TREE_CODE (type) == VECTOR_TYPE
9171 && int_size_in_bytes (type) >= 8
9172 && int_size_in_bytes (type) < 16))
9173 return 64;
9174 else if (ALTIVEC_OR_VSX_VECTOR_MODE (elt_mode)
9175 || (type && TREE_CODE (type) == VECTOR_TYPE
9176 && int_size_in_bytes (type) >= 16))
9177 return 128;
9178 else if (((TARGET_MACHO && rs6000_darwin64_abi)
9179 || DEFAULT_ABI == ABI_ELFv2
9180 || (DEFAULT_ABI == ABI_AIX && !rs6000_compat_align_parm))
9181 && mode == BLKmode
9182 && type && TYPE_ALIGN (type) > 64)
9183 return 128;
9184 else
9185 return PARM_BOUNDARY;
9188 /* The offset in words to the start of the parameter save area. */
9190 static unsigned int
9191 rs6000_parm_offset (void)
9193 return (DEFAULT_ABI == ABI_V4 ? 2
9194 : DEFAULT_ABI == ABI_ELFv2 ? 4
9195 : 6);
9198 /* For a function parm of MODE and TYPE, return the starting word in
9199 the parameter area. NWORDS of the parameter area are already used. */
9201 static unsigned int
9202 rs6000_parm_start (enum machine_mode mode, const_tree type,
9203 unsigned int nwords)
9205 unsigned int align;
9207 align = rs6000_function_arg_boundary (mode, type) / PARM_BOUNDARY - 1;
9208 return nwords + (-(rs6000_parm_offset () + nwords) & align);
9211 /* Compute the size (in words) of a function argument. */
9213 static unsigned long
9214 rs6000_arg_size (enum machine_mode mode, const_tree type)
9216 unsigned long size;
9218 if (mode != BLKmode)
9219 size = GET_MODE_SIZE (mode);
9220 else
9221 size = int_size_in_bytes (type);
9223 if (TARGET_32BIT)
9224 return (size + 3) >> 2;
9225 else
9226 return (size + 7) >> 3;
9229 /* Use this to flush pending int fields. */
9231 static void
9232 rs6000_darwin64_record_arg_advance_flush (CUMULATIVE_ARGS *cum,
9233 HOST_WIDE_INT bitpos, int final)
9235 unsigned int startbit, endbit;
9236 int intregs, intoffset;
9237 enum machine_mode mode;
9239 /* Handle the situations where a float is taking up the first half
9240 of the GPR, and the other half is empty (typically due to
9241 alignment restrictions). We can detect this by a 8-byte-aligned
9242 int field, or by seeing that this is the final flush for this
9243 argument. Count the word and continue on. */
9244 if (cum->floats_in_gpr == 1
9245 && (cum->intoffset % 64 == 0
9246 || (cum->intoffset == -1 && final)))
9248 cum->words++;
9249 cum->floats_in_gpr = 0;
9252 if (cum->intoffset == -1)
9253 return;
9255 intoffset = cum->intoffset;
9256 cum->intoffset = -1;
9257 cum->floats_in_gpr = 0;
9259 if (intoffset % BITS_PER_WORD != 0)
9261 mode = mode_for_size (BITS_PER_WORD - intoffset % BITS_PER_WORD,
9262 MODE_INT, 0);
9263 if (mode == BLKmode)
9265 /* We couldn't find an appropriate mode, which happens,
9266 e.g., in packed structs when there are 3 bytes to load.
9267 Back intoffset back to the beginning of the word in this
9268 case. */
9269 intoffset = intoffset & -BITS_PER_WORD;
9273 startbit = intoffset & -BITS_PER_WORD;
9274 endbit = (bitpos + BITS_PER_WORD - 1) & -BITS_PER_WORD;
9275 intregs = (endbit - startbit) / BITS_PER_WORD;
9276 cum->words += intregs;
9277 /* words should be unsigned. */
9278 if ((unsigned)cum->words < (endbit/BITS_PER_WORD))
9280 int pad = (endbit/BITS_PER_WORD) - cum->words;
9281 cum->words += pad;
9285 /* The darwin64 ABI calls for us to recurse down through structs,
9286 looking for elements passed in registers. Unfortunately, we have
9287 to track int register count here also because of misalignments
9288 in powerpc alignment mode. */
9290 static void
9291 rs6000_darwin64_record_arg_advance_recurse (CUMULATIVE_ARGS *cum,
9292 const_tree type,
9293 HOST_WIDE_INT startbitpos)
9295 tree f;
9297 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
9298 if (TREE_CODE (f) == FIELD_DECL)
9300 HOST_WIDE_INT bitpos = startbitpos;
9301 tree ftype = TREE_TYPE (f);
9302 enum machine_mode mode;
9303 if (ftype == error_mark_node)
9304 continue;
9305 mode = TYPE_MODE (ftype);
9307 if (DECL_SIZE (f) != 0
9308 && tree_fits_uhwi_p (bit_position (f)))
9309 bitpos += int_bit_position (f);
9311 /* ??? FIXME: else assume zero offset. */
9313 if (TREE_CODE (ftype) == RECORD_TYPE)
9314 rs6000_darwin64_record_arg_advance_recurse (cum, ftype, bitpos);
9315 else if (USE_FP_FOR_ARG_P (cum, mode))
9317 unsigned n_fpregs = (GET_MODE_SIZE (mode) + 7) >> 3;
9318 rs6000_darwin64_record_arg_advance_flush (cum, bitpos, 0);
9319 cum->fregno += n_fpregs;
9320 /* Single-precision floats present a special problem for
9321 us, because they are smaller than an 8-byte GPR, and so
9322 the structure-packing rules combined with the standard
9323 varargs behavior mean that we want to pack float/float
9324 and float/int combinations into a single register's
9325 space. This is complicated by the arg advance flushing,
9326 which works on arbitrarily large groups of int-type
9327 fields. */
9328 if (mode == SFmode)
9330 if (cum->floats_in_gpr == 1)
9332 /* Two floats in a word; count the word and reset
9333 the float count. */
9334 cum->words++;
9335 cum->floats_in_gpr = 0;
9337 else if (bitpos % 64 == 0)
9339 /* A float at the beginning of an 8-byte word;
9340 count it and put off adjusting cum->words until
9341 we see if a arg advance flush is going to do it
9342 for us. */
9343 cum->floats_in_gpr++;
9345 else
9347 /* The float is at the end of a word, preceded
9348 by integer fields, so the arg advance flush
9349 just above has already set cum->words and
9350 everything is taken care of. */
9353 else
9354 cum->words += n_fpregs;
9356 else if (USE_ALTIVEC_FOR_ARG_P (cum, mode, 1))
9358 rs6000_darwin64_record_arg_advance_flush (cum, bitpos, 0);
9359 cum->vregno++;
9360 cum->words += 2;
9362 else if (cum->intoffset == -1)
9363 cum->intoffset = bitpos;
9367 /* Check for an item that needs to be considered specially under the darwin 64
9368 bit ABI. These are record types where the mode is BLK or the structure is
9369 8 bytes in size. */
9370 static int
9371 rs6000_darwin64_struct_check_p (enum machine_mode mode, const_tree type)
9373 return rs6000_darwin64_abi
9374 && ((mode == BLKmode
9375 && TREE_CODE (type) == RECORD_TYPE
9376 && int_size_in_bytes (type) > 0)
9377 || (type && TREE_CODE (type) == RECORD_TYPE
9378 && int_size_in_bytes (type) == 8)) ? 1 : 0;
9381 /* Update the data in CUM to advance over an argument
9382 of mode MODE and data type TYPE.
9383 (TYPE is null for libcalls where that information may not be available.)
9385 Note that for args passed by reference, function_arg will be called
9386 with MODE and TYPE set to that of the pointer to the arg, not the arg
9387 itself. */
9389 static void
9390 rs6000_function_arg_advance_1 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
9391 const_tree type, bool named, int depth)
9393 enum machine_mode elt_mode;
9394 int n_elts;
9396 rs6000_discover_homogeneous_aggregate (mode, type, &elt_mode, &n_elts);
9398 /* Only tick off an argument if we're not recursing. */
9399 if (depth == 0)
9400 cum->nargs_prototype--;
9402 #ifdef HAVE_AS_GNU_ATTRIBUTE
9403 if (DEFAULT_ABI == ABI_V4
9404 && cum->escapes)
9406 if (SCALAR_FLOAT_MODE_P (mode))
9407 rs6000_passes_float = true;
9408 else if (named && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
9409 rs6000_passes_vector = true;
9410 else if (SPE_VECTOR_MODE (mode)
9411 && !cum->stdarg
9412 && cum->sysv_gregno <= GP_ARG_MAX_REG)
9413 rs6000_passes_vector = true;
9415 #endif
9417 if (TARGET_ALTIVEC_ABI
9418 && (ALTIVEC_OR_VSX_VECTOR_MODE (elt_mode)
9419 || (type && TREE_CODE (type) == VECTOR_TYPE
9420 && int_size_in_bytes (type) == 16)))
9422 bool stack = false;
9424 if (USE_ALTIVEC_FOR_ARG_P (cum, elt_mode, named))
9426 cum->vregno += n_elts;
9428 if (!TARGET_ALTIVEC)
9429 error ("cannot pass argument in vector register because"
9430 " altivec instructions are disabled, use -maltivec"
9431 " to enable them");
9433 /* PowerPC64 Linux and AIX allocate GPRs for a vector argument
9434 even if it is going to be passed in a vector register.
9435 Darwin does the same for variable-argument functions. */
9436 if (((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
9437 && TARGET_64BIT)
9438 || (cum->stdarg && DEFAULT_ABI != ABI_V4))
9439 stack = true;
9441 else
9442 stack = true;
9444 if (stack)
9446 int align;
9448 /* Vector parameters must be 16-byte aligned. In 32-bit
9449 mode this means we need to take into account the offset
9450 to the parameter save area. In 64-bit mode, they just
9451 have to start on an even word, since the parameter save
9452 area is 16-byte aligned. */
9453 if (TARGET_32BIT)
9454 align = -(rs6000_parm_offset () + cum->words) & 3;
9455 else
9456 align = cum->words & 1;
9457 cum->words += align + rs6000_arg_size (mode, type);
9459 if (TARGET_DEBUG_ARG)
9461 fprintf (stderr, "function_adv: words = %2d, align=%d, ",
9462 cum->words, align);
9463 fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s\n",
9464 cum->nargs_prototype, cum->prototype,
9465 GET_MODE_NAME (mode));
9469 else if (TARGET_SPE_ABI && TARGET_SPE && SPE_VECTOR_MODE (mode)
9470 && !cum->stdarg
9471 && cum->sysv_gregno <= GP_ARG_MAX_REG)
9472 cum->sysv_gregno++;
9474 else if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
9476 int size = int_size_in_bytes (type);
9477 /* Variable sized types have size == -1 and are
9478 treated as if consisting entirely of ints.
9479 Pad to 16 byte boundary if needed. */
9480 if (TYPE_ALIGN (type) >= 2 * BITS_PER_WORD
9481 && (cum->words % 2) != 0)
9482 cum->words++;
9483 /* For varargs, we can just go up by the size of the struct. */
9484 if (!named)
9485 cum->words += (size + 7) / 8;
9486 else
9488 /* It is tempting to say int register count just goes up by
9489 sizeof(type)/8, but this is wrong in a case such as
9490 { int; double; int; } [powerpc alignment]. We have to
9491 grovel through the fields for these too. */
9492 cum->intoffset = 0;
9493 cum->floats_in_gpr = 0;
9494 rs6000_darwin64_record_arg_advance_recurse (cum, type, 0);
9495 rs6000_darwin64_record_arg_advance_flush (cum,
9496 size * BITS_PER_UNIT, 1);
9498 if (TARGET_DEBUG_ARG)
9500 fprintf (stderr, "function_adv: words = %2d, align=%d, size=%d",
9501 cum->words, TYPE_ALIGN (type), size);
9502 fprintf (stderr,
9503 "nargs = %4d, proto = %d, mode = %4s (darwin64 abi)\n",
9504 cum->nargs_prototype, cum->prototype,
9505 GET_MODE_NAME (mode));
9508 else if (DEFAULT_ABI == ABI_V4)
9510 if (TARGET_HARD_FLOAT && TARGET_FPRS
9511 && ((TARGET_SINGLE_FLOAT && mode == SFmode)
9512 || (TARGET_DOUBLE_FLOAT && mode == DFmode)
9513 || (mode == TFmode && !TARGET_IEEEQUAD)
9514 || mode == SDmode || mode == DDmode || mode == TDmode))
9516 /* _Decimal128 must use an even/odd register pair. This assumes
9517 that the register number is odd when fregno is odd. */
9518 if (mode == TDmode && (cum->fregno % 2) == 1)
9519 cum->fregno++;
9521 if (cum->fregno + (mode == TFmode || mode == TDmode ? 1 : 0)
9522 <= FP_ARG_V4_MAX_REG)
9523 cum->fregno += (GET_MODE_SIZE (mode) + 7) >> 3;
9524 else
9526 cum->fregno = FP_ARG_V4_MAX_REG + 1;
9527 if (mode == DFmode || mode == TFmode
9528 || mode == DDmode || mode == TDmode)
9529 cum->words += cum->words & 1;
9530 cum->words += rs6000_arg_size (mode, type);
9533 else
9535 int n_words = rs6000_arg_size (mode, type);
9536 int gregno = cum->sysv_gregno;
9538 /* Long long and SPE vectors are put in (r3,r4), (r5,r6),
9539 (r7,r8) or (r9,r10). As does any other 2 word item such
9540 as complex int due to a historical mistake. */
9541 if (n_words == 2)
9542 gregno += (1 - gregno) & 1;
9544 /* Multi-reg args are not split between registers and stack. */
9545 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
9547 /* Long long and SPE vectors are aligned on the stack.
9548 So are other 2 word items such as complex int due to
9549 a historical mistake. */
9550 if (n_words == 2)
9551 cum->words += cum->words & 1;
9552 cum->words += n_words;
9555 /* Note: continuing to accumulate gregno past when we've started
9556 spilling to the stack indicates the fact that we've started
9557 spilling to the stack to expand_builtin_saveregs. */
9558 cum->sysv_gregno = gregno + n_words;
9561 if (TARGET_DEBUG_ARG)
9563 fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
9564 cum->words, cum->fregno);
9565 fprintf (stderr, "gregno = %2d, nargs = %4d, proto = %d, ",
9566 cum->sysv_gregno, cum->nargs_prototype, cum->prototype);
9567 fprintf (stderr, "mode = %4s, named = %d\n",
9568 GET_MODE_NAME (mode), named);
9571 else
9573 int n_words = rs6000_arg_size (mode, type);
9574 int start_words = cum->words;
9575 int align_words = rs6000_parm_start (mode, type, start_words);
9577 cum->words = align_words + n_words;
9579 if (SCALAR_FLOAT_MODE_P (elt_mode)
9580 && TARGET_HARD_FLOAT && TARGET_FPRS)
9582 /* _Decimal128 must be passed in an even/odd float register pair.
9583 This assumes that the register number is odd when fregno is
9584 odd. */
9585 if (elt_mode == TDmode && (cum->fregno % 2) == 1)
9586 cum->fregno++;
9587 cum->fregno += n_elts * ((GET_MODE_SIZE (elt_mode) + 7) >> 3);
9590 if (TARGET_DEBUG_ARG)
9592 fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
9593 cum->words, cum->fregno);
9594 fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s, ",
9595 cum->nargs_prototype, cum->prototype, GET_MODE_NAME (mode));
9596 fprintf (stderr, "named = %d, align = %d, depth = %d\n",
9597 named, align_words - start_words, depth);
9602 static void
9603 rs6000_function_arg_advance (cumulative_args_t cum, enum machine_mode mode,
9604 const_tree type, bool named)
9606 rs6000_function_arg_advance_1 (get_cumulative_args (cum), mode, type, named,
9610 static rtx
9611 spe_build_register_parallel (enum machine_mode mode, int gregno)
9613 rtx r1, r3, r5, r7;
9615 switch (mode)
9617 case DFmode:
9618 r1 = gen_rtx_REG (DImode, gregno);
9619 r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
9620 return gen_rtx_PARALLEL (mode, gen_rtvec (1, r1));
9622 case DCmode:
9623 case TFmode:
9624 r1 = gen_rtx_REG (DImode, gregno);
9625 r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
9626 r3 = gen_rtx_REG (DImode, gregno + 2);
9627 r3 = gen_rtx_EXPR_LIST (VOIDmode, r3, GEN_INT (8));
9628 return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r3));
9630 case TCmode:
9631 r1 = gen_rtx_REG (DImode, gregno);
9632 r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
9633 r3 = gen_rtx_REG (DImode, gregno + 2);
9634 r3 = gen_rtx_EXPR_LIST (VOIDmode, r3, GEN_INT (8));
9635 r5 = gen_rtx_REG (DImode, gregno + 4);
9636 r5 = gen_rtx_EXPR_LIST (VOIDmode, r5, GEN_INT (16));
9637 r7 = gen_rtx_REG (DImode, gregno + 6);
9638 r7 = gen_rtx_EXPR_LIST (VOIDmode, r7, GEN_INT (24));
9639 return gen_rtx_PARALLEL (mode, gen_rtvec (4, r1, r3, r5, r7));
9641 default:
9642 gcc_unreachable ();
9646 /* Determine where to put a SIMD argument on the SPE. */
9647 static rtx
9648 rs6000_spe_function_arg (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
9649 const_tree type)
9651 int gregno = cum->sysv_gregno;
9653 /* On E500 v2, double arithmetic is done on the full 64-bit GPR, but
9654 are passed and returned in a pair of GPRs for ABI compatibility. */
9655 if (TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode
9656 || mode == DCmode || mode == TCmode))
9658 int n_words = rs6000_arg_size (mode, type);
9660 /* Doubles go in an odd/even register pair (r5/r6, etc). */
9661 if (mode == DFmode)
9662 gregno += (1 - gregno) & 1;
9664 /* Multi-reg args are not split between registers and stack. */
9665 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
9666 return NULL_RTX;
9668 return spe_build_register_parallel (mode, gregno);
9670 if (cum->stdarg)
9672 int n_words = rs6000_arg_size (mode, type);
9674 /* SPE vectors are put in odd registers. */
9675 if (n_words == 2 && (gregno & 1) == 0)
9676 gregno += 1;
9678 if (gregno + n_words - 1 <= GP_ARG_MAX_REG)
9680 rtx r1, r2;
9681 enum machine_mode m = SImode;
9683 r1 = gen_rtx_REG (m, gregno);
9684 r1 = gen_rtx_EXPR_LIST (m, r1, const0_rtx);
9685 r2 = gen_rtx_REG (m, gregno + 1);
9686 r2 = gen_rtx_EXPR_LIST (m, r2, GEN_INT (4));
9687 return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
9689 else
9690 return NULL_RTX;
9692 else
9694 if (gregno <= GP_ARG_MAX_REG)
9695 return gen_rtx_REG (mode, gregno);
9696 else
9697 return NULL_RTX;
9701 /* A subroutine of rs6000_darwin64_record_arg. Assign the bits of the
9702 structure between cum->intoffset and bitpos to integer registers. */
9704 static void
9705 rs6000_darwin64_record_arg_flush (CUMULATIVE_ARGS *cum,
9706 HOST_WIDE_INT bitpos, rtx rvec[], int *k)
9708 enum machine_mode mode;
9709 unsigned int regno;
9710 unsigned int startbit, endbit;
9711 int this_regno, intregs, intoffset;
9712 rtx reg;
9714 if (cum->intoffset == -1)
9715 return;
9717 intoffset = cum->intoffset;
9718 cum->intoffset = -1;
9720 /* If this is the trailing part of a word, try to only load that
9721 much into the register. Otherwise load the whole register. Note
9722 that in the latter case we may pick up unwanted bits. It's not a
9723 problem at the moment but may wish to revisit. */
9725 if (intoffset % BITS_PER_WORD != 0)
9727 mode = mode_for_size (BITS_PER_WORD - intoffset % BITS_PER_WORD,
9728 MODE_INT, 0);
9729 if (mode == BLKmode)
9731 /* We couldn't find an appropriate mode, which happens,
9732 e.g., in packed structs when there are 3 bytes to load.
9733 Back intoffset back to the beginning of the word in this
9734 case. */
9735 intoffset = intoffset & -BITS_PER_WORD;
9736 mode = word_mode;
9739 else
9740 mode = word_mode;
9742 startbit = intoffset & -BITS_PER_WORD;
9743 endbit = (bitpos + BITS_PER_WORD - 1) & -BITS_PER_WORD;
9744 intregs = (endbit - startbit) / BITS_PER_WORD;
9745 this_regno = cum->words + intoffset / BITS_PER_WORD;
9747 if (intregs > 0 && intregs > GP_ARG_NUM_REG - this_regno)
9748 cum->use_stack = 1;
9750 intregs = MIN (intregs, GP_ARG_NUM_REG - this_regno);
9751 if (intregs <= 0)
9752 return;
9754 intoffset /= BITS_PER_UNIT;
9757 regno = GP_ARG_MIN_REG + this_regno;
9758 reg = gen_rtx_REG (mode, regno);
9759 rvec[(*k)++] =
9760 gen_rtx_EXPR_LIST (VOIDmode, reg, GEN_INT (intoffset));
9762 this_regno += 1;
9763 intoffset = (intoffset | (UNITS_PER_WORD-1)) + 1;
9764 mode = word_mode;
9765 intregs -= 1;
9767 while (intregs > 0);
9770 /* Recursive workhorse for the following. */
9772 static void
9773 rs6000_darwin64_record_arg_recurse (CUMULATIVE_ARGS *cum, const_tree type,
9774 HOST_WIDE_INT startbitpos, rtx rvec[],
9775 int *k)
9777 tree f;
9779 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
9780 if (TREE_CODE (f) == FIELD_DECL)
9782 HOST_WIDE_INT bitpos = startbitpos;
9783 tree ftype = TREE_TYPE (f);
9784 enum machine_mode mode;
9785 if (ftype == error_mark_node)
9786 continue;
9787 mode = TYPE_MODE (ftype);
9789 if (DECL_SIZE (f) != 0
9790 && tree_fits_uhwi_p (bit_position (f)))
9791 bitpos += int_bit_position (f);
9793 /* ??? FIXME: else assume zero offset. */
9795 if (TREE_CODE (ftype) == RECORD_TYPE)
9796 rs6000_darwin64_record_arg_recurse (cum, ftype, bitpos, rvec, k);
9797 else if (cum->named && USE_FP_FOR_ARG_P (cum, mode))
9799 unsigned n_fpreg = (GET_MODE_SIZE (mode) + 7) >> 3;
9800 #if 0
9801 switch (mode)
9803 case SCmode: mode = SFmode; break;
9804 case DCmode: mode = DFmode; break;
9805 case TCmode: mode = TFmode; break;
9806 default: break;
9808 #endif
9809 rs6000_darwin64_record_arg_flush (cum, bitpos, rvec, k);
9810 if (cum->fregno + n_fpreg > FP_ARG_MAX_REG + 1)
9812 gcc_assert (cum->fregno == FP_ARG_MAX_REG
9813 && (mode == TFmode || mode == TDmode));
9814 /* Long double or _Decimal128 split over regs and memory. */
9815 mode = DECIMAL_FLOAT_MODE_P (mode) ? DDmode : DFmode;
9816 cum->use_stack=1;
9818 rvec[(*k)++]
9819 = gen_rtx_EXPR_LIST (VOIDmode,
9820 gen_rtx_REG (mode, cum->fregno++),
9821 GEN_INT (bitpos / BITS_PER_UNIT));
9822 if (mode == TFmode || mode == TDmode)
9823 cum->fregno++;
9825 else if (cum->named && USE_ALTIVEC_FOR_ARG_P (cum, mode, 1))
9827 rs6000_darwin64_record_arg_flush (cum, bitpos, rvec, k);
9828 rvec[(*k)++]
9829 = gen_rtx_EXPR_LIST (VOIDmode,
9830 gen_rtx_REG (mode, cum->vregno++),
9831 GEN_INT (bitpos / BITS_PER_UNIT));
9833 else if (cum->intoffset == -1)
9834 cum->intoffset = bitpos;
9838 /* For the darwin64 ABI, we want to construct a PARALLEL consisting of
9839 the register(s) to be used for each field and subfield of a struct
9840 being passed by value, along with the offset of where the
9841 register's value may be found in the block. FP fields go in FP
9842 register, vector fields go in vector registers, and everything
9843 else goes in int registers, packed as in memory.
9845 This code is also used for function return values. RETVAL indicates
9846 whether this is the case.
9848 Much of this is taken from the SPARC V9 port, which has a similar
9849 calling convention. */
9851 static rtx
9852 rs6000_darwin64_record_arg (CUMULATIVE_ARGS *orig_cum, const_tree type,
9853 bool named, bool retval)
9855 rtx rvec[FIRST_PSEUDO_REGISTER];
9856 int k = 1, kbase = 1;
9857 HOST_WIDE_INT typesize = int_size_in_bytes (type);
9858 /* This is a copy; modifications are not visible to our caller. */
9859 CUMULATIVE_ARGS copy_cum = *orig_cum;
9860 CUMULATIVE_ARGS *cum = &copy_cum;
9862 /* Pad to 16 byte boundary if needed. */
9863 if (!retval && TYPE_ALIGN (type) >= 2 * BITS_PER_WORD
9864 && (cum->words % 2) != 0)
9865 cum->words++;
9867 cum->intoffset = 0;
9868 cum->use_stack = 0;
9869 cum->named = named;
9871 /* Put entries into rvec[] for individual FP and vector fields, and
9872 for the chunks of memory that go in int regs. Note we start at
9873 element 1; 0 is reserved for an indication of using memory, and
9874 may or may not be filled in below. */
9875 rs6000_darwin64_record_arg_recurse (cum, type, /* startbit pos= */ 0, rvec, &k);
9876 rs6000_darwin64_record_arg_flush (cum, typesize * BITS_PER_UNIT, rvec, &k);
9878 /* If any part of the struct went on the stack put all of it there.
9879 This hack is because the generic code for
9880 FUNCTION_ARG_PARTIAL_NREGS cannot handle cases where the register
9881 parts of the struct are not at the beginning. */
9882 if (cum->use_stack)
9884 if (retval)
9885 return NULL_RTX; /* doesn't go in registers at all */
9886 kbase = 0;
9887 rvec[0] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
9889 if (k > 1 || cum->use_stack)
9890 return gen_rtx_PARALLEL (BLKmode, gen_rtvec_v (k - kbase, &rvec[kbase]));
9891 else
9892 return NULL_RTX;
9895 /* Determine where to place an argument in 64-bit mode with 32-bit ABI. */
9897 static rtx
9898 rs6000_mixed_function_arg (enum machine_mode mode, const_tree type,
9899 int align_words)
9901 int n_units;
9902 int i, k;
9903 rtx rvec[GP_ARG_NUM_REG + 1];
9905 if (align_words >= GP_ARG_NUM_REG)
9906 return NULL_RTX;
9908 n_units = rs6000_arg_size (mode, type);
9910 /* Optimize the simple case where the arg fits in one gpr, except in
9911 the case of BLKmode due to assign_parms assuming that registers are
9912 BITS_PER_WORD wide. */
9913 if (n_units == 0
9914 || (n_units == 1 && mode != BLKmode))
9915 return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
9917 k = 0;
9918 if (align_words + n_units > GP_ARG_NUM_REG)
9919 /* Not all of the arg fits in gprs. Say that it goes in memory too,
9920 using a magic NULL_RTX component.
9921 This is not strictly correct. Only some of the arg belongs in
9922 memory, not all of it. However, the normal scheme using
9923 function_arg_partial_nregs can result in unusual subregs, eg.
9924 (subreg:SI (reg:DF) 4), which are not handled well. The code to
9925 store the whole arg to memory is often more efficient than code
9926 to store pieces, and we know that space is available in the right
9927 place for the whole arg. */
9928 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
9930 i = 0;
9933 rtx r = gen_rtx_REG (SImode, GP_ARG_MIN_REG + align_words);
9934 rtx off = GEN_INT (i++ * 4);
9935 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
9937 while (++align_words < GP_ARG_NUM_REG && --n_units != 0);
9939 return gen_rtx_PARALLEL (mode, gen_rtvec_v (k, rvec));
9942 /* We have an argument of MODE and TYPE that goes into FPRs or VRs,
9943 but must also be copied into the parameter save area starting at
9944 offset ALIGN_WORDS. Fill in RVEC with the elements corresponding
9945 to the GPRs and/or memory. Return the number of elements used. */
9947 static int
9948 rs6000_psave_function_arg (enum machine_mode mode, const_tree type,
9949 int align_words, rtx *rvec)
9951 int k = 0;
9953 if (align_words < GP_ARG_NUM_REG)
9955 int n_words = rs6000_arg_size (mode, type);
9957 if (align_words + n_words > GP_ARG_NUM_REG
9958 || mode == BLKmode
9959 || (TARGET_32BIT && TARGET_POWERPC64))
9961 /* If this is partially on the stack, then we only
9962 include the portion actually in registers here. */
9963 enum machine_mode rmode = TARGET_32BIT ? SImode : DImode;
9964 int i = 0;
9966 if (align_words + n_words > GP_ARG_NUM_REG)
9968 /* Not all of the arg fits in gprs. Say that it goes in memory
9969 too, using a magic NULL_RTX component. Also see comment in
9970 rs6000_mixed_function_arg for why the normal
9971 function_arg_partial_nregs scheme doesn't work in this case. */
9972 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
9977 rtx r = gen_rtx_REG (rmode, GP_ARG_MIN_REG + align_words);
9978 rtx off = GEN_INT (i++ * GET_MODE_SIZE (rmode));
9979 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
9981 while (++align_words < GP_ARG_NUM_REG && --n_words != 0);
9983 else
9985 /* The whole arg fits in gprs. */
9986 rtx r = gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
9987 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, const0_rtx);
9990 else
9992 /* It's entirely in memory. */
9993 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
9996 return k;
9999 /* RVEC is a vector of K components of an argument of mode MODE.
10000 Construct the final function_arg return value from it. */
10002 static rtx
10003 rs6000_finish_function_arg (enum machine_mode mode, rtx *rvec, int k)
10005 gcc_assert (k >= 1);
10007 /* Avoid returning a PARALLEL in the trivial cases. */
10008 if (k == 1)
10010 if (XEXP (rvec[0], 0) == NULL_RTX)
10011 return NULL_RTX;
10013 if (GET_MODE (XEXP (rvec[0], 0)) == mode)
10014 return XEXP (rvec[0], 0);
10017 return gen_rtx_PARALLEL (mode, gen_rtvec_v (k, rvec));
10020 /* Determine where to put an argument to a function.
10021 Value is zero to push the argument on the stack,
10022 or a hard register in which to store the argument.
10024 MODE is the argument's machine mode.
10025 TYPE is the data type of the argument (as a tree).
10026 This is null for libcalls where that information may
10027 not be available.
10028 CUM is a variable of type CUMULATIVE_ARGS which gives info about
10029 the preceding args and about the function being called. It is
10030 not modified in this routine.
10031 NAMED is nonzero if this argument is a named parameter
10032 (otherwise it is an extra parameter matching an ellipsis).
10034 On RS/6000 the first eight words of non-FP are normally in registers
10035 and the rest are pushed. Under AIX, the first 13 FP args are in registers.
10036 Under V.4, the first 8 FP args are in registers.
10038 If this is floating-point and no prototype is specified, we use
10039 both an FP and integer register (or possibly FP reg and stack). Library
10040 functions (when CALL_LIBCALL is set) always have the proper types for args,
10041 so we can pass the FP value just in one register. emit_library_function
10042 doesn't support PARALLEL anyway.
10044 Note that for args passed by reference, function_arg will be called
10045 with MODE and TYPE set to that of the pointer to the arg, not the arg
10046 itself. */
10048 static rtx
10049 rs6000_function_arg (cumulative_args_t cum_v, enum machine_mode mode,
10050 const_tree type, bool named)
10052 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
10053 enum rs6000_abi abi = DEFAULT_ABI;
10054 enum machine_mode elt_mode;
10055 int n_elts;
10057 /* Return a marker to indicate whether CR1 needs to set or clear the
10058 bit that V.4 uses to say fp args were passed in registers.
10059 Assume that we don't need the marker for software floating point,
10060 or compiler generated library calls. */
10061 if (mode == VOIDmode)
10063 if (abi == ABI_V4
10064 && (cum->call_cookie & CALL_LIBCALL) == 0
10065 && (cum->stdarg
10066 || (cum->nargs_prototype < 0
10067 && (cum->prototype || TARGET_NO_PROTOTYPE))))
10069 /* For the SPE, we need to crxor CR6 always. */
10070 if (TARGET_SPE_ABI)
10071 return GEN_INT (cum->call_cookie | CALL_V4_SET_FP_ARGS);
10072 else if (TARGET_HARD_FLOAT && TARGET_FPRS)
10073 return GEN_INT (cum->call_cookie
10074 | ((cum->fregno == FP_ARG_MIN_REG)
10075 ? CALL_V4_SET_FP_ARGS
10076 : CALL_V4_CLEAR_FP_ARGS));
10079 return GEN_INT (cum->call_cookie & ~CALL_LIBCALL);
10082 rs6000_discover_homogeneous_aggregate (mode, type, &elt_mode, &n_elts);
10084 if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
10086 rtx rslt = rs6000_darwin64_record_arg (cum, type, named, /*retval= */false);
10087 if (rslt != NULL_RTX)
10088 return rslt;
10089 /* Else fall through to usual handling. */
10092 if (USE_ALTIVEC_FOR_ARG_P (cum, elt_mode, named))
10094 rtx rvec[GP_ARG_NUM_REG + AGGR_ARG_NUM_REG + 1];
10095 rtx r, off;
10096 int i, k = 0;
10098 /* Do we also need to pass this argument in the parameter
10099 save area? */
10100 if (TARGET_64BIT && ! cum->prototype)
10102 int align_words = (cum->words + 1) & ~1;
10103 k = rs6000_psave_function_arg (mode, type, align_words, rvec);
10106 /* Describe where this argument goes in the vector registers. */
10107 for (i = 0; i < n_elts && cum->vregno + i <= ALTIVEC_ARG_MAX_REG; i++)
10109 r = gen_rtx_REG (elt_mode, cum->vregno + i);
10110 off = GEN_INT (i * GET_MODE_SIZE (elt_mode));
10111 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
10114 return rs6000_finish_function_arg (mode, rvec, k);
10116 else if (TARGET_ALTIVEC_ABI
10117 && (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
10118 || (type && TREE_CODE (type) == VECTOR_TYPE
10119 && int_size_in_bytes (type) == 16)))
10121 if (named || abi == ABI_V4)
10122 return NULL_RTX;
10123 else
10125 /* Vector parameters to varargs functions under AIX or Darwin
10126 get passed in memory and possibly also in GPRs. */
10127 int align, align_words, n_words;
10128 enum machine_mode part_mode;
10130 /* Vector parameters must be 16-byte aligned. In 32-bit
10131 mode this means we need to take into account the offset
10132 to the parameter save area. In 64-bit mode, they just
10133 have to start on an even word, since the parameter save
10134 area is 16-byte aligned. */
10135 if (TARGET_32BIT)
10136 align = -(rs6000_parm_offset () + cum->words) & 3;
10137 else
10138 align = cum->words & 1;
10139 align_words = cum->words + align;
10141 /* Out of registers? Memory, then. */
10142 if (align_words >= GP_ARG_NUM_REG)
10143 return NULL_RTX;
10145 if (TARGET_32BIT && TARGET_POWERPC64)
10146 return rs6000_mixed_function_arg (mode, type, align_words);
10148 /* The vector value goes in GPRs. Only the part of the
10149 value in GPRs is reported here. */
10150 part_mode = mode;
10151 n_words = rs6000_arg_size (mode, type);
10152 if (align_words + n_words > GP_ARG_NUM_REG)
10153 /* Fortunately, there are only two possibilities, the value
10154 is either wholly in GPRs or half in GPRs and half not. */
10155 part_mode = DImode;
10157 return gen_rtx_REG (part_mode, GP_ARG_MIN_REG + align_words);
10160 else if (TARGET_SPE_ABI && TARGET_SPE
10161 && (SPE_VECTOR_MODE (mode)
10162 || (TARGET_E500_DOUBLE && (mode == DFmode
10163 || mode == DCmode
10164 || mode == TFmode
10165 || mode == TCmode))))
10166 return rs6000_spe_function_arg (cum, mode, type);
10168 else if (abi == ABI_V4)
10170 if (TARGET_HARD_FLOAT && TARGET_FPRS
10171 && ((TARGET_SINGLE_FLOAT && mode == SFmode)
10172 || (TARGET_DOUBLE_FLOAT && mode == DFmode)
10173 || (mode == TFmode && !TARGET_IEEEQUAD)
10174 || mode == SDmode || mode == DDmode || mode == TDmode))
10176 /* _Decimal128 must use an even/odd register pair. This assumes
10177 that the register number is odd when fregno is odd. */
10178 if (mode == TDmode && (cum->fregno % 2) == 1)
10179 cum->fregno++;
10181 if (cum->fregno + (mode == TFmode || mode == TDmode ? 1 : 0)
10182 <= FP_ARG_V4_MAX_REG)
10183 return gen_rtx_REG (mode, cum->fregno);
10184 else
10185 return NULL_RTX;
10187 else
10189 int n_words = rs6000_arg_size (mode, type);
10190 int gregno = cum->sysv_gregno;
10192 /* Long long and SPE vectors are put in (r3,r4), (r5,r6),
10193 (r7,r8) or (r9,r10). As does any other 2 word item such
10194 as complex int due to a historical mistake. */
10195 if (n_words == 2)
10196 gregno += (1 - gregno) & 1;
10198 /* Multi-reg args are not split between registers and stack. */
10199 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
10200 return NULL_RTX;
10202 if (TARGET_32BIT && TARGET_POWERPC64)
10203 return rs6000_mixed_function_arg (mode, type,
10204 gregno - GP_ARG_MIN_REG);
10205 return gen_rtx_REG (mode, gregno);
10208 else
10210 int align_words = rs6000_parm_start (mode, type, cum->words);
10212 /* _Decimal128 must be passed in an even/odd float register pair.
10213 This assumes that the register number is odd when fregno is odd. */
10214 if (elt_mode == TDmode && (cum->fregno % 2) == 1)
10215 cum->fregno++;
10217 if (USE_FP_FOR_ARG_P (cum, elt_mode))
10219 rtx rvec[GP_ARG_NUM_REG + AGGR_ARG_NUM_REG + 1];
10220 rtx r, off;
10221 int i, k = 0;
10222 unsigned long n_fpreg = (GET_MODE_SIZE (elt_mode) + 7) >> 3;
10224 /* Do we also need to pass this argument in the parameter
10225 save area? */
10226 if (type && (cum->nargs_prototype <= 0
10227 || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
10228 && TARGET_XL_COMPAT
10229 && align_words >= GP_ARG_NUM_REG)))
10230 k = rs6000_psave_function_arg (mode, type, align_words, rvec);
10232 /* Describe where this argument goes in the fprs. */
10233 for (i = 0; i < n_elts
10234 && cum->fregno + i * n_fpreg <= FP_ARG_MAX_REG; i++)
10236 /* Check if the argument is split over registers and memory.
10237 This can only ever happen for long double or _Decimal128;
10238 complex types are handled via split_complex_arg. */
10239 enum machine_mode fmode = elt_mode;
10240 if (cum->fregno + (i + 1) * n_fpreg > FP_ARG_MAX_REG + 1)
10242 gcc_assert (fmode == TFmode || fmode == TDmode);
10243 fmode = DECIMAL_FLOAT_MODE_P (fmode) ? DDmode : DFmode;
10246 r = gen_rtx_REG (fmode, cum->fregno + i * n_fpreg);
10247 off = GEN_INT (i * GET_MODE_SIZE (elt_mode));
10248 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
10251 return rs6000_finish_function_arg (mode, rvec, k);
10253 else if (align_words < GP_ARG_NUM_REG)
10255 if (TARGET_32BIT && TARGET_POWERPC64)
10256 return rs6000_mixed_function_arg (mode, type, align_words);
10258 return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
10260 else
10261 return NULL_RTX;
10265 /* For an arg passed partly in registers and partly in memory, this is
10266 the number of bytes passed in registers. For args passed entirely in
10267 registers or entirely in memory, zero. When an arg is described by a
10268 PARALLEL, perhaps using more than one register type, this function
10269 returns the number of bytes used by the first element of the PARALLEL. */
10271 static int
10272 rs6000_arg_partial_bytes (cumulative_args_t cum_v, enum machine_mode mode,
10273 tree type, bool named)
10275 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
10276 bool passed_in_gprs = true;
10277 int ret = 0;
10278 int align_words;
10279 enum machine_mode elt_mode;
10280 int n_elts;
10282 rs6000_discover_homogeneous_aggregate (mode, type, &elt_mode, &n_elts);
10284 if (DEFAULT_ABI == ABI_V4)
10285 return 0;
10287 if (USE_ALTIVEC_FOR_ARG_P (cum, elt_mode, named))
10289 /* If we are passing this arg in the fixed parameter save area
10290 (gprs or memory) as well as VRs, we do not use the partial
10291 bytes mechanism; instead, rs6000_function_arg will return a
10292 PARALLEL including a memory element as necessary. */
10293 if (TARGET_64BIT && ! cum->prototype)
10294 return 0;
10296 /* Otherwise, we pass in VRs only. Check for partial copies. */
10297 passed_in_gprs = false;
10298 if (cum->vregno + n_elts > ALTIVEC_ARG_MAX_REG + 1)
10299 ret = (ALTIVEC_ARG_MAX_REG + 1 - cum->vregno) * 16;
10302 /* In this complicated case we just disable the partial_nregs code. */
10303 if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
10304 return 0;
10306 align_words = rs6000_parm_start (mode, type, cum->words);
10308 if (USE_FP_FOR_ARG_P (cum, elt_mode))
10310 unsigned long n_fpreg = (GET_MODE_SIZE (elt_mode) + 7) >> 3;
10312 /* If we are passing this arg in the fixed parameter save area
10313 (gprs or memory) as well as FPRs, we do not use the partial
10314 bytes mechanism; instead, rs6000_function_arg will return a
10315 PARALLEL including a memory element as necessary. */
10316 if (type
10317 && (cum->nargs_prototype <= 0
10318 || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
10319 && TARGET_XL_COMPAT
10320 && align_words >= GP_ARG_NUM_REG)))
10321 return 0;
10323 /* Otherwise, we pass in FPRs only. Check for partial copies. */
10324 passed_in_gprs = false;
10325 if (cum->fregno + n_elts * n_fpreg > FP_ARG_MAX_REG + 1)
10326 ret = ((FP_ARG_MAX_REG + 1 - cum->fregno)
10327 * MIN (8, GET_MODE_SIZE (elt_mode)));
10330 if (passed_in_gprs
10331 && align_words < GP_ARG_NUM_REG
10332 && GP_ARG_NUM_REG < align_words + rs6000_arg_size (mode, type))
10333 ret = (GP_ARG_NUM_REG - align_words) * (TARGET_32BIT ? 4 : 8);
10335 if (ret != 0 && TARGET_DEBUG_ARG)
10336 fprintf (stderr, "rs6000_arg_partial_bytes: %d\n", ret);
10338 return ret;
10341 /* A C expression that indicates when an argument must be passed by
10342 reference. If nonzero for an argument, a copy of that argument is
10343 made in memory and a pointer to the argument is passed instead of
10344 the argument itself. The pointer is passed in whatever way is
10345 appropriate for passing a pointer to that type.
10347 Under V.4, aggregates and long double are passed by reference.
10349 As an extension to all 32-bit ABIs, AltiVec vectors are passed by
10350 reference unless the AltiVec vector extension ABI is in force.
10352 As an extension to all ABIs, variable sized types are passed by
10353 reference. */
10355 static bool
10356 rs6000_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED,
10357 enum machine_mode mode, const_tree type,
10358 bool named ATTRIBUTE_UNUSED)
10360 if (DEFAULT_ABI == ABI_V4 && TARGET_IEEEQUAD && mode == TFmode)
10362 if (TARGET_DEBUG_ARG)
10363 fprintf (stderr, "function_arg_pass_by_reference: V4 long double\n");
10364 return 1;
10367 if (!type)
10368 return 0;
10370 if (DEFAULT_ABI == ABI_V4 && AGGREGATE_TYPE_P (type))
10372 if (TARGET_DEBUG_ARG)
10373 fprintf (stderr, "function_arg_pass_by_reference: V4 aggregate\n");
10374 return 1;
10377 if (int_size_in_bytes (type) < 0)
10379 if (TARGET_DEBUG_ARG)
10380 fprintf (stderr, "function_arg_pass_by_reference: variable size\n");
10381 return 1;
10384 /* Allow -maltivec -mabi=no-altivec without warning. Altivec vector
10385 modes only exist for GCC vector types if -maltivec. */
10386 if (TARGET_32BIT && !TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
10388 if (TARGET_DEBUG_ARG)
10389 fprintf (stderr, "function_arg_pass_by_reference: AltiVec\n");
10390 return 1;
10393 /* Pass synthetic vectors in memory. */
10394 if (TREE_CODE (type) == VECTOR_TYPE
10395 && int_size_in_bytes (type) > (TARGET_ALTIVEC_ABI ? 16 : 8))
10397 static bool warned_for_pass_big_vectors = false;
10398 if (TARGET_DEBUG_ARG)
10399 fprintf (stderr, "function_arg_pass_by_reference: synthetic vector\n");
10400 if (!warned_for_pass_big_vectors)
10402 warning (0, "GCC vector passed by reference: "
10403 "non-standard ABI extension with no compatibility guarantee");
10404 warned_for_pass_big_vectors = true;
10406 return 1;
10409 return 0;
10412 /* Process parameter of type TYPE after ARGS_SO_FAR parameters were
10413 already processes. Return true if the parameter must be passed
10414 (fully or partially) on the stack. */
10416 static bool
10417 rs6000_parm_needs_stack (cumulative_args_t args_so_far, tree type)
10419 enum machine_mode mode;
10420 int unsignedp;
10421 rtx entry_parm;
10423 /* Catch errors. */
10424 if (type == NULL || type == error_mark_node)
10425 return true;
10427 /* Handle types with no storage requirement. */
10428 if (TYPE_MODE (type) == VOIDmode)
10429 return false;
10431 /* Handle complex types. */
10432 if (TREE_CODE (type) == COMPLEX_TYPE)
10433 return (rs6000_parm_needs_stack (args_so_far, TREE_TYPE (type))
10434 || rs6000_parm_needs_stack (args_so_far, TREE_TYPE (type)));
10436 /* Handle transparent aggregates. */
10437 if ((TREE_CODE (type) == UNION_TYPE || TREE_CODE (type) == RECORD_TYPE)
10438 && TYPE_TRANSPARENT_AGGR (type))
10439 type = TREE_TYPE (first_field (type));
10441 /* See if this arg was passed by invisible reference. */
10442 if (pass_by_reference (get_cumulative_args (args_so_far),
10443 TYPE_MODE (type), type, true))
10444 type = build_pointer_type (type);
10446 /* Find mode as it is passed by the ABI. */
10447 unsignedp = TYPE_UNSIGNED (type);
10448 mode = promote_mode (type, TYPE_MODE (type), &unsignedp);
10450 /* If we must pass in stack, we need a stack. */
10451 if (rs6000_must_pass_in_stack (mode, type))
10452 return true;
10454 /* If there is no incoming register, we need a stack. */
10455 entry_parm = rs6000_function_arg (args_so_far, mode, type, true);
10456 if (entry_parm == NULL)
10457 return true;
10459 /* Likewise if we need to pass both in registers and on the stack. */
10460 if (GET_CODE (entry_parm) == PARALLEL
10461 && XEXP (XVECEXP (entry_parm, 0, 0), 0) == NULL_RTX)
10462 return true;
10464 /* Also true if we're partially in registers and partially not. */
10465 if (rs6000_arg_partial_bytes (args_so_far, mode, type, true) != 0)
10466 return true;
10468 /* Update info on where next arg arrives in registers. */
10469 rs6000_function_arg_advance (args_so_far, mode, type, true);
10470 return false;
10473 /* Return true if FUN has no prototype, has a variable argument
10474 list, or passes any parameter in memory. */
10476 static bool
10477 rs6000_function_parms_need_stack (tree fun)
10479 function_args_iterator args_iter;
10480 tree arg_type;
10481 CUMULATIVE_ARGS args_so_far_v;
10482 cumulative_args_t args_so_far;
10484 if (!fun)
10485 /* Must be a libcall, all of which only use reg parms. */
10486 return false;
10487 if (!TYPE_P (fun))
10488 fun = TREE_TYPE (fun);
10490 /* Varargs functions need the parameter save area. */
10491 if (!prototype_p (fun) || stdarg_p (fun))
10492 return true;
10494 INIT_CUMULATIVE_INCOMING_ARGS (args_so_far_v, fun, NULL_RTX);
10495 args_so_far = pack_cumulative_args (&args_so_far_v);
10497 if (aggregate_value_p (TREE_TYPE (fun), fun))
10499 tree type = build_pointer_type (TREE_TYPE (fun));
10500 rs6000_parm_needs_stack (args_so_far, type);
10503 FOREACH_FUNCTION_ARGS (fun, arg_type, args_iter)
10504 if (rs6000_parm_needs_stack (args_so_far, arg_type))
10505 return true;
10507 return false;
10510 /* Return the size of the REG_PARM_STACK_SPACE are for FUN. This is
10511 usually a constant depending on the ABI. However, in the ELFv2 ABI
10512 the register parameter area is optional when calling a function that
10513 has a prototype is scope, has no variable argument list, and passes
10514 all parameters in registers. */
10517 rs6000_reg_parm_stack_space (tree fun)
10519 int reg_parm_stack_space;
10521 switch (DEFAULT_ABI)
10523 default:
10524 reg_parm_stack_space = 0;
10525 break;
10527 case ABI_AIX:
10528 case ABI_DARWIN:
10529 reg_parm_stack_space = TARGET_64BIT ? 64 : 32;
10530 break;
10532 case ABI_ELFv2:
10533 /* ??? Recomputing this every time is a bit expensive. Is there
10534 a place to cache this information? */
10535 if (rs6000_function_parms_need_stack (fun))
10536 reg_parm_stack_space = TARGET_64BIT ? 64 : 32;
10537 else
10538 reg_parm_stack_space = 0;
10539 break;
10542 return reg_parm_stack_space;
10545 static void
10546 rs6000_move_block_from_reg (int regno, rtx x, int nregs)
10548 int i;
10549 enum machine_mode reg_mode = TARGET_32BIT ? SImode : DImode;
10551 if (nregs == 0)
10552 return;
10554 for (i = 0; i < nregs; i++)
10556 rtx tem = adjust_address_nv (x, reg_mode, i * GET_MODE_SIZE (reg_mode));
10557 if (reload_completed)
10559 if (! strict_memory_address_p (reg_mode, XEXP (tem, 0)))
10560 tem = NULL_RTX;
10561 else
10562 tem = simplify_gen_subreg (reg_mode, x, BLKmode,
10563 i * GET_MODE_SIZE (reg_mode));
10565 else
10566 tem = replace_equiv_address (tem, XEXP (tem, 0));
10568 gcc_assert (tem);
10570 emit_move_insn (tem, gen_rtx_REG (reg_mode, regno + i));
10574 /* Perform any needed actions needed for a function that is receiving a
10575 variable number of arguments.
10577 CUM is as above.
10579 MODE and TYPE are the mode and type of the current parameter.
10581 PRETEND_SIZE is a variable that should be set to the amount of stack
10582 that must be pushed by the prolog to pretend that our caller pushed
10585 Normally, this macro will push all remaining incoming registers on the
10586 stack and set PRETEND_SIZE to the length of the registers pushed. */
10588 static void
10589 setup_incoming_varargs (cumulative_args_t cum, enum machine_mode mode,
10590 tree type, int *pretend_size ATTRIBUTE_UNUSED,
10591 int no_rtl)
10593 CUMULATIVE_ARGS next_cum;
10594 int reg_size = TARGET_32BIT ? 4 : 8;
10595 rtx save_area = NULL_RTX, mem;
10596 int first_reg_offset;
10597 alias_set_type set;
10599 /* Skip the last named argument. */
10600 next_cum = *get_cumulative_args (cum);
10601 rs6000_function_arg_advance_1 (&next_cum, mode, type, true, 0);
10603 if (DEFAULT_ABI == ABI_V4)
10605 first_reg_offset = next_cum.sysv_gregno - GP_ARG_MIN_REG;
10607 if (! no_rtl)
10609 int gpr_reg_num = 0, gpr_size = 0, fpr_size = 0;
10610 HOST_WIDE_INT offset = 0;
10612 /* Try to optimize the size of the varargs save area.
10613 The ABI requires that ap.reg_save_area is doubleword
10614 aligned, but we don't need to allocate space for all
10615 the bytes, only those to which we actually will save
10616 anything. */
10617 if (cfun->va_list_gpr_size && first_reg_offset < GP_ARG_NUM_REG)
10618 gpr_reg_num = GP_ARG_NUM_REG - first_reg_offset;
10619 if (TARGET_HARD_FLOAT && TARGET_FPRS
10620 && next_cum.fregno <= FP_ARG_V4_MAX_REG
10621 && cfun->va_list_fpr_size)
10623 if (gpr_reg_num)
10624 fpr_size = (next_cum.fregno - FP_ARG_MIN_REG)
10625 * UNITS_PER_FP_WORD;
10626 if (cfun->va_list_fpr_size
10627 < FP_ARG_V4_MAX_REG + 1 - next_cum.fregno)
10628 fpr_size += cfun->va_list_fpr_size * UNITS_PER_FP_WORD;
10629 else
10630 fpr_size += (FP_ARG_V4_MAX_REG + 1 - next_cum.fregno)
10631 * UNITS_PER_FP_WORD;
10633 if (gpr_reg_num)
10635 offset = -((first_reg_offset * reg_size) & ~7);
10636 if (!fpr_size && gpr_reg_num > cfun->va_list_gpr_size)
10638 gpr_reg_num = cfun->va_list_gpr_size;
10639 if (reg_size == 4 && (first_reg_offset & 1))
10640 gpr_reg_num++;
10642 gpr_size = (gpr_reg_num * reg_size + 7) & ~7;
10644 else if (fpr_size)
10645 offset = - (int) (next_cum.fregno - FP_ARG_MIN_REG)
10646 * UNITS_PER_FP_WORD
10647 - (int) (GP_ARG_NUM_REG * reg_size);
10649 if (gpr_size + fpr_size)
10651 rtx reg_save_area
10652 = assign_stack_local (BLKmode, gpr_size + fpr_size, 64);
10653 gcc_assert (GET_CODE (reg_save_area) == MEM);
10654 reg_save_area = XEXP (reg_save_area, 0);
10655 if (GET_CODE (reg_save_area) == PLUS)
10657 gcc_assert (XEXP (reg_save_area, 0)
10658 == virtual_stack_vars_rtx);
10659 gcc_assert (GET_CODE (XEXP (reg_save_area, 1)) == CONST_INT);
10660 offset += INTVAL (XEXP (reg_save_area, 1));
10662 else
10663 gcc_assert (reg_save_area == virtual_stack_vars_rtx);
10666 cfun->machine->varargs_save_offset = offset;
10667 save_area = plus_constant (Pmode, virtual_stack_vars_rtx, offset);
10670 else
10672 first_reg_offset = next_cum.words;
10673 save_area = virtual_incoming_args_rtx;
10675 if (targetm.calls.must_pass_in_stack (mode, type))
10676 first_reg_offset += rs6000_arg_size (TYPE_MODE (type), type);
10679 set = get_varargs_alias_set ();
10680 if (! no_rtl && first_reg_offset < GP_ARG_NUM_REG
10681 && cfun->va_list_gpr_size)
10683 int n_gpr, nregs = GP_ARG_NUM_REG - first_reg_offset;
10685 if (va_list_gpr_counter_field)
10686 /* V4 va_list_gpr_size counts number of registers needed. */
10687 n_gpr = cfun->va_list_gpr_size;
10688 else
10689 /* char * va_list instead counts number of bytes needed. */
10690 n_gpr = (cfun->va_list_gpr_size + reg_size - 1) / reg_size;
10692 if (nregs > n_gpr)
10693 nregs = n_gpr;
10695 mem = gen_rtx_MEM (BLKmode,
10696 plus_constant (Pmode, save_area,
10697 first_reg_offset * reg_size));
10698 MEM_NOTRAP_P (mem) = 1;
10699 set_mem_alias_set (mem, set);
10700 set_mem_align (mem, BITS_PER_WORD);
10702 rs6000_move_block_from_reg (GP_ARG_MIN_REG + first_reg_offset, mem,
10703 nregs);
10706 /* Save FP registers if needed. */
10707 if (DEFAULT_ABI == ABI_V4
10708 && TARGET_HARD_FLOAT && TARGET_FPRS
10709 && ! no_rtl
10710 && next_cum.fregno <= FP_ARG_V4_MAX_REG
10711 && cfun->va_list_fpr_size)
10713 int fregno = next_cum.fregno, nregs;
10714 rtx cr1 = gen_rtx_REG (CCmode, CR1_REGNO);
10715 rtx lab = gen_label_rtx ();
10716 int off = (GP_ARG_NUM_REG * reg_size) + ((fregno - FP_ARG_MIN_REG)
10717 * UNITS_PER_FP_WORD);
10719 emit_jump_insn
10720 (gen_rtx_SET (VOIDmode,
10721 pc_rtx,
10722 gen_rtx_IF_THEN_ELSE (VOIDmode,
10723 gen_rtx_NE (VOIDmode, cr1,
10724 const0_rtx),
10725 gen_rtx_LABEL_REF (VOIDmode, lab),
10726 pc_rtx)));
10728 for (nregs = 0;
10729 fregno <= FP_ARG_V4_MAX_REG && nregs < cfun->va_list_fpr_size;
10730 fregno++, off += UNITS_PER_FP_WORD, nregs++)
10732 mem = gen_rtx_MEM ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
10733 ? DFmode : SFmode,
10734 plus_constant (Pmode, save_area, off));
10735 MEM_NOTRAP_P (mem) = 1;
10736 set_mem_alias_set (mem, set);
10737 set_mem_align (mem, GET_MODE_ALIGNMENT (
10738 (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
10739 ? DFmode : SFmode));
10740 emit_move_insn (mem, gen_rtx_REG (
10741 (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
10742 ? DFmode : SFmode, fregno));
10745 emit_label (lab);
10749 /* Create the va_list data type. */
10751 static tree
10752 rs6000_build_builtin_va_list (void)
10754 tree f_gpr, f_fpr, f_res, f_ovf, f_sav, record, type_decl;
10756 /* For AIX, prefer 'char *' because that's what the system
10757 header files like. */
10758 if (DEFAULT_ABI != ABI_V4)
10759 return build_pointer_type (char_type_node);
10761 record = (*lang_hooks.types.make_type) (RECORD_TYPE);
10762 type_decl = build_decl (BUILTINS_LOCATION, TYPE_DECL,
10763 get_identifier ("__va_list_tag"), record);
10765 f_gpr = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("gpr"),
10766 unsigned_char_type_node);
10767 f_fpr = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("fpr"),
10768 unsigned_char_type_node);
10769 /* Give the two bytes of padding a name, so that -Wpadded won't warn on
10770 every user file. */
10771 f_res = build_decl (BUILTINS_LOCATION, FIELD_DECL,
10772 get_identifier ("reserved"), short_unsigned_type_node);
10773 f_ovf = build_decl (BUILTINS_LOCATION, FIELD_DECL,
10774 get_identifier ("overflow_arg_area"),
10775 ptr_type_node);
10776 f_sav = build_decl (BUILTINS_LOCATION, FIELD_DECL,
10777 get_identifier ("reg_save_area"),
10778 ptr_type_node);
10780 va_list_gpr_counter_field = f_gpr;
10781 va_list_fpr_counter_field = f_fpr;
10783 DECL_FIELD_CONTEXT (f_gpr) = record;
10784 DECL_FIELD_CONTEXT (f_fpr) = record;
10785 DECL_FIELD_CONTEXT (f_res) = record;
10786 DECL_FIELD_CONTEXT (f_ovf) = record;
10787 DECL_FIELD_CONTEXT (f_sav) = record;
10789 TYPE_STUB_DECL (record) = type_decl;
10790 TYPE_NAME (record) = type_decl;
10791 TYPE_FIELDS (record) = f_gpr;
10792 DECL_CHAIN (f_gpr) = f_fpr;
10793 DECL_CHAIN (f_fpr) = f_res;
10794 DECL_CHAIN (f_res) = f_ovf;
10795 DECL_CHAIN (f_ovf) = f_sav;
10797 layout_type (record);
10799 /* The correct type is an array type of one element. */
10800 return build_array_type (record, build_index_type (size_zero_node));
10803 /* Implement va_start. */
10805 static void
10806 rs6000_va_start (tree valist, rtx nextarg)
10808 HOST_WIDE_INT words, n_gpr, n_fpr;
10809 tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
10810 tree gpr, fpr, ovf, sav, t;
10812 /* Only SVR4 needs something special. */
10813 if (DEFAULT_ABI != ABI_V4)
10815 std_expand_builtin_va_start (valist, nextarg);
10816 return;
10819 f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
10820 f_fpr = DECL_CHAIN (f_gpr);
10821 f_res = DECL_CHAIN (f_fpr);
10822 f_ovf = DECL_CHAIN (f_res);
10823 f_sav = DECL_CHAIN (f_ovf);
10825 valist = build_simple_mem_ref (valist);
10826 gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
10827 fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
10828 f_fpr, NULL_TREE);
10829 ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
10830 f_ovf, NULL_TREE);
10831 sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
10832 f_sav, NULL_TREE);
10834 /* Count number of gp and fp argument registers used. */
10835 words = crtl->args.info.words;
10836 n_gpr = MIN (crtl->args.info.sysv_gregno - GP_ARG_MIN_REG,
10837 GP_ARG_NUM_REG);
10838 n_fpr = MIN (crtl->args.info.fregno - FP_ARG_MIN_REG,
10839 FP_ARG_NUM_REG);
10841 if (TARGET_DEBUG_ARG)
10842 fprintf (stderr, "va_start: words = "HOST_WIDE_INT_PRINT_DEC", n_gpr = "
10843 HOST_WIDE_INT_PRINT_DEC", n_fpr = "HOST_WIDE_INT_PRINT_DEC"\n",
10844 words, n_gpr, n_fpr);
10846 if (cfun->va_list_gpr_size)
10848 t = build2 (MODIFY_EXPR, TREE_TYPE (gpr), gpr,
10849 build_int_cst (NULL_TREE, n_gpr));
10850 TREE_SIDE_EFFECTS (t) = 1;
10851 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
10854 if (cfun->va_list_fpr_size)
10856 t = build2 (MODIFY_EXPR, TREE_TYPE (fpr), fpr,
10857 build_int_cst (NULL_TREE, n_fpr));
10858 TREE_SIDE_EFFECTS (t) = 1;
10859 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
10861 #ifdef HAVE_AS_GNU_ATTRIBUTE
10862 if (call_ABI_of_interest (cfun->decl))
10863 rs6000_passes_float = true;
10864 #endif
10867 /* Find the overflow area. */
10868 t = make_tree (TREE_TYPE (ovf), virtual_incoming_args_rtx);
10869 if (words != 0)
10870 t = fold_build_pointer_plus_hwi (t, words * UNITS_PER_WORD);
10871 t = build2 (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
10872 TREE_SIDE_EFFECTS (t) = 1;
10873 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
10875 /* If there were no va_arg invocations, don't set up the register
10876 save area. */
10877 if (!cfun->va_list_gpr_size
10878 && !cfun->va_list_fpr_size
10879 && n_gpr < GP_ARG_NUM_REG
10880 && n_fpr < FP_ARG_V4_MAX_REG)
10881 return;
10883 /* Find the register save area. */
10884 t = make_tree (TREE_TYPE (sav), virtual_stack_vars_rtx);
10885 if (cfun->machine->varargs_save_offset)
10886 t = fold_build_pointer_plus_hwi (t, cfun->machine->varargs_save_offset);
10887 t = build2 (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
10888 TREE_SIDE_EFFECTS (t) = 1;
10889 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
10892 /* Implement va_arg. */
10894 static tree
10895 rs6000_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p,
10896 gimple_seq *post_p)
10898 tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
10899 tree gpr, fpr, ovf, sav, reg, t, u;
10900 int size, rsize, n_reg, sav_ofs, sav_scale;
10901 tree lab_false, lab_over, addr;
10902 int align;
10903 tree ptrtype = build_pointer_type_for_mode (type, ptr_mode, true);
10904 int regalign = 0;
10905 gimple stmt;
10907 if (pass_by_reference (NULL, TYPE_MODE (type), type, false))
10909 t = rs6000_gimplify_va_arg (valist, ptrtype, pre_p, post_p);
10910 return build_va_arg_indirect_ref (t);
10913 /* We need to deal with the fact that the darwin ppc64 ABI is defined by an
10914 earlier version of gcc, with the property that it always applied alignment
10915 adjustments to the va-args (even for zero-sized types). The cheapest way
10916 to deal with this is to replicate the effect of the part of
10917 std_gimplify_va_arg_expr that carries out the align adjust, for the case
10918 of relevance.
10919 We don't need to check for pass-by-reference because of the test above.
10920 We can return a simplifed answer, since we know there's no offset to add. */
10922 if (((TARGET_MACHO
10923 && rs6000_darwin64_abi)
10924 || DEFAULT_ABI == ABI_ELFv2
10925 || (DEFAULT_ABI == ABI_AIX && !rs6000_compat_align_parm))
10926 && integer_zerop (TYPE_SIZE (type)))
10928 unsigned HOST_WIDE_INT align, boundary;
10929 tree valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
10930 align = PARM_BOUNDARY / BITS_PER_UNIT;
10931 boundary = rs6000_function_arg_boundary (TYPE_MODE (type), type);
10932 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
10933 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
10934 boundary /= BITS_PER_UNIT;
10935 if (boundary > align)
10937 tree t ;
10938 /* This updates arg ptr by the amount that would be necessary
10939 to align the zero-sized (but not zero-alignment) item. */
10940 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
10941 fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
10942 gimplify_and_add (t, pre_p);
10944 t = fold_convert (sizetype, valist_tmp);
10945 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
10946 fold_convert (TREE_TYPE (valist),
10947 fold_build2 (BIT_AND_EXPR, sizetype, t,
10948 size_int (-boundary))));
10949 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
10950 gimplify_and_add (t, pre_p);
10952 /* Since it is zero-sized there's no increment for the item itself. */
10953 valist_tmp = fold_convert (build_pointer_type (type), valist_tmp);
10954 return build_va_arg_indirect_ref (valist_tmp);
10957 if (DEFAULT_ABI != ABI_V4)
10959 if (targetm.calls.split_complex_arg && TREE_CODE (type) == COMPLEX_TYPE)
10961 tree elem_type = TREE_TYPE (type);
10962 enum machine_mode elem_mode = TYPE_MODE (elem_type);
10963 int elem_size = GET_MODE_SIZE (elem_mode);
10965 if (elem_size < UNITS_PER_WORD)
10967 tree real_part, imag_part;
10968 gimple_seq post = NULL;
10970 real_part = rs6000_gimplify_va_arg (valist, elem_type, pre_p,
10971 &post);
10972 /* Copy the value into a temporary, lest the formal temporary
10973 be reused out from under us. */
10974 real_part = get_initialized_tmp_var (real_part, pre_p, &post);
10975 gimple_seq_add_seq (pre_p, post);
10977 imag_part = rs6000_gimplify_va_arg (valist, elem_type, pre_p,
10978 post_p);
10980 return build2 (COMPLEX_EXPR, type, real_part, imag_part);
10984 return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
10987 f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
10988 f_fpr = DECL_CHAIN (f_gpr);
10989 f_res = DECL_CHAIN (f_fpr);
10990 f_ovf = DECL_CHAIN (f_res);
10991 f_sav = DECL_CHAIN (f_ovf);
10993 valist = build_va_arg_indirect_ref (valist);
10994 gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
10995 fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
10996 f_fpr, NULL_TREE);
10997 ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
10998 f_ovf, NULL_TREE);
10999 sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
11000 f_sav, NULL_TREE);
11002 size = int_size_in_bytes (type);
11003 rsize = (size + 3) / 4;
11004 align = 1;
11006 if (TARGET_HARD_FLOAT && TARGET_FPRS
11007 && ((TARGET_SINGLE_FLOAT && TYPE_MODE (type) == SFmode)
11008 || (TARGET_DOUBLE_FLOAT
11009 && (TYPE_MODE (type) == DFmode
11010 || TYPE_MODE (type) == TFmode
11011 || TYPE_MODE (type) == SDmode
11012 || TYPE_MODE (type) == DDmode
11013 || TYPE_MODE (type) == TDmode))))
11015 /* FP args go in FP registers, if present. */
11016 reg = fpr;
11017 n_reg = (size + 7) / 8;
11018 sav_ofs = ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? 8 : 4) * 4;
11019 sav_scale = ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? 8 : 4);
11020 if (TYPE_MODE (type) != SFmode && TYPE_MODE (type) != SDmode)
11021 align = 8;
11023 else
11025 /* Otherwise into GP registers. */
11026 reg = gpr;
11027 n_reg = rsize;
11028 sav_ofs = 0;
11029 sav_scale = 4;
11030 if (n_reg == 2)
11031 align = 8;
11034 /* Pull the value out of the saved registers.... */
11036 lab_over = NULL;
11037 addr = create_tmp_var (ptr_type_node, "addr");
11039 /* AltiVec vectors never go in registers when -mabi=altivec. */
11040 if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (TYPE_MODE (type)))
11041 align = 16;
11042 else
11044 lab_false = create_artificial_label (input_location);
11045 lab_over = create_artificial_label (input_location);
11047 /* Long long and SPE vectors are aligned in the registers.
11048 As are any other 2 gpr item such as complex int due to a
11049 historical mistake. */
11050 u = reg;
11051 if (n_reg == 2 && reg == gpr)
11053 regalign = 1;
11054 u = build2 (BIT_AND_EXPR, TREE_TYPE (reg), unshare_expr (reg),
11055 build_int_cst (TREE_TYPE (reg), n_reg - 1));
11056 u = build2 (POSTINCREMENT_EXPR, TREE_TYPE (reg),
11057 unshare_expr (reg), u);
11059 /* _Decimal128 is passed in even/odd fpr pairs; the stored
11060 reg number is 0 for f1, so we want to make it odd. */
11061 else if (reg == fpr && TYPE_MODE (type) == TDmode)
11063 t = build2 (BIT_IOR_EXPR, TREE_TYPE (reg), unshare_expr (reg),
11064 build_int_cst (TREE_TYPE (reg), 1));
11065 u = build2 (MODIFY_EXPR, void_type_node, unshare_expr (reg), t);
11068 t = fold_convert (TREE_TYPE (reg), size_int (8 - n_reg + 1));
11069 t = build2 (GE_EXPR, boolean_type_node, u, t);
11070 u = build1 (GOTO_EXPR, void_type_node, lab_false);
11071 t = build3 (COND_EXPR, void_type_node, t, u, NULL_TREE);
11072 gimplify_and_add (t, pre_p);
11074 t = sav;
11075 if (sav_ofs)
11076 t = fold_build_pointer_plus_hwi (sav, sav_ofs);
11078 u = build2 (POSTINCREMENT_EXPR, TREE_TYPE (reg), unshare_expr (reg),
11079 build_int_cst (TREE_TYPE (reg), n_reg));
11080 u = fold_convert (sizetype, u);
11081 u = build2 (MULT_EXPR, sizetype, u, size_int (sav_scale));
11082 t = fold_build_pointer_plus (t, u);
11084 /* _Decimal32 varargs are located in the second word of the 64-bit
11085 FP register for 32-bit binaries. */
11086 if (!TARGET_POWERPC64
11087 && TARGET_HARD_FLOAT && TARGET_FPRS
11088 && TYPE_MODE (type) == SDmode)
11089 t = fold_build_pointer_plus_hwi (t, size);
11091 gimplify_assign (addr, t, pre_p);
11093 gimple_seq_add_stmt (pre_p, gimple_build_goto (lab_over));
11095 stmt = gimple_build_label (lab_false);
11096 gimple_seq_add_stmt (pre_p, stmt);
11098 if ((n_reg == 2 && !regalign) || n_reg > 2)
11100 /* Ensure that we don't find any more args in regs.
11101 Alignment has taken care of for special cases. */
11102 gimplify_assign (reg, build_int_cst (TREE_TYPE (reg), 8), pre_p);
11106 /* ... otherwise out of the overflow area. */
11108 /* Care for on-stack alignment if needed. */
11109 t = ovf;
11110 if (align != 1)
11112 t = fold_build_pointer_plus_hwi (t, align - 1);
11113 t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t,
11114 build_int_cst (TREE_TYPE (t), -align));
11116 gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
11118 gimplify_assign (unshare_expr (addr), t, pre_p);
11120 t = fold_build_pointer_plus_hwi (t, size);
11121 gimplify_assign (unshare_expr (ovf), t, pre_p);
11123 if (lab_over)
11125 stmt = gimple_build_label (lab_over);
11126 gimple_seq_add_stmt (pre_p, stmt);
11129 if (STRICT_ALIGNMENT
11130 && (TYPE_ALIGN (type)
11131 > (unsigned) BITS_PER_UNIT * (align < 4 ? 4 : align)))
11133 /* The value (of type complex double, for example) may not be
11134 aligned in memory in the saved registers, so copy via a
11135 temporary. (This is the same code as used for SPARC.) */
11136 tree tmp = create_tmp_var (type, "va_arg_tmp");
11137 tree dest_addr = build_fold_addr_expr (tmp);
11139 tree copy = build_call_expr (builtin_decl_implicit (BUILT_IN_MEMCPY),
11140 3, dest_addr, addr, size_int (rsize * 4));
11142 gimplify_and_add (copy, pre_p);
11143 addr = dest_addr;
11146 addr = fold_convert (ptrtype, addr);
11147 return build_va_arg_indirect_ref (addr);
11150 /* Builtins. */
11152 static void
11153 def_builtin (const char *name, tree type, enum rs6000_builtins code)
11155 tree t;
11156 unsigned classify = rs6000_builtin_info[(int)code].attr;
11157 const char *attr_string = "";
11159 gcc_assert (name != NULL);
11160 gcc_assert (IN_RANGE ((int)code, 0, (int)RS6000_BUILTIN_COUNT));
11162 if (rs6000_builtin_decls[(int)code])
11163 fatal_error ("internal error: builtin function %s already processed", name);
11165 rs6000_builtin_decls[(int)code] = t =
11166 add_builtin_function (name, type, (int)code, BUILT_IN_MD, NULL, NULL_TREE);
11168 /* Set any special attributes. */
11169 if ((classify & RS6000_BTC_CONST) != 0)
11171 /* const function, function only depends on the inputs. */
11172 TREE_READONLY (t) = 1;
11173 TREE_NOTHROW (t) = 1;
11174 attr_string = ", pure";
11176 else if ((classify & RS6000_BTC_PURE) != 0)
11178 /* pure function, function can read global memory, but does not set any
11179 external state. */
11180 DECL_PURE_P (t) = 1;
11181 TREE_NOTHROW (t) = 1;
11182 attr_string = ", const";
11184 else if ((classify & RS6000_BTC_FP) != 0)
11186 /* Function is a math function. If rounding mode is on, then treat the
11187 function as not reading global memory, but it can have arbitrary side
11188 effects. If it is off, then assume the function is a const function.
11189 This mimics the ATTR_MATHFN_FPROUNDING attribute in
11190 builtin-attribute.def that is used for the math functions. */
11191 TREE_NOTHROW (t) = 1;
11192 if (flag_rounding_math)
11194 DECL_PURE_P (t) = 1;
11195 DECL_IS_NOVOPS (t) = 1;
11196 attr_string = ", fp, pure";
11198 else
11200 TREE_READONLY (t) = 1;
11201 attr_string = ", fp, const";
11204 else if ((classify & RS6000_BTC_ATTR_MASK) != 0)
11205 gcc_unreachable ();
11207 if (TARGET_DEBUG_BUILTIN)
11208 fprintf (stderr, "rs6000_builtin, code = %4d, %s%s\n",
11209 (int)code, name, attr_string);
11212 /* Simple ternary operations: VECd = foo (VECa, VECb, VECc). */
11214 #undef RS6000_BUILTIN_1
11215 #undef RS6000_BUILTIN_2
11216 #undef RS6000_BUILTIN_3
11217 #undef RS6000_BUILTIN_A
11218 #undef RS6000_BUILTIN_D
11219 #undef RS6000_BUILTIN_E
11220 #undef RS6000_BUILTIN_H
11221 #undef RS6000_BUILTIN_P
11222 #undef RS6000_BUILTIN_Q
11223 #undef RS6000_BUILTIN_S
11224 #undef RS6000_BUILTIN_X
11226 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
11227 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
11228 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE) \
11229 { MASK, ICODE, NAME, ENUM },
11231 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
11232 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
11233 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
11234 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
11235 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
11236 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
11237 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
11238 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11240 static const struct builtin_description bdesc_3arg[] =
11242 #include "rs6000-builtin.def"
11245 /* DST operations: void foo (void *, const int, const char). */
11247 #undef RS6000_BUILTIN_1
11248 #undef RS6000_BUILTIN_2
11249 #undef RS6000_BUILTIN_3
11250 #undef RS6000_BUILTIN_A
11251 #undef RS6000_BUILTIN_D
11252 #undef RS6000_BUILTIN_E
11253 #undef RS6000_BUILTIN_H
11254 #undef RS6000_BUILTIN_P
11255 #undef RS6000_BUILTIN_Q
11256 #undef RS6000_BUILTIN_S
11257 #undef RS6000_BUILTIN_X
11259 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
11260 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
11261 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
11262 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
11263 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE) \
11264 { MASK, ICODE, NAME, ENUM },
11266 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
11267 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
11268 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
11269 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
11270 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
11271 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11273 static const struct builtin_description bdesc_dst[] =
11275 #include "rs6000-builtin.def"
11278 /* Simple binary operations: VECc = foo (VECa, VECb). */
11280 #undef RS6000_BUILTIN_1
11281 #undef RS6000_BUILTIN_2
11282 #undef RS6000_BUILTIN_3
11283 #undef RS6000_BUILTIN_A
11284 #undef RS6000_BUILTIN_D
11285 #undef RS6000_BUILTIN_E
11286 #undef RS6000_BUILTIN_H
11287 #undef RS6000_BUILTIN_P
11288 #undef RS6000_BUILTIN_Q
11289 #undef RS6000_BUILTIN_S
11290 #undef RS6000_BUILTIN_X
11292 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
11293 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE) \
11294 { MASK, ICODE, NAME, ENUM },
11296 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
11297 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
11298 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
11299 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
11300 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
11301 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
11302 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
11303 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
11304 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11306 static const struct builtin_description bdesc_2arg[] =
11308 #include "rs6000-builtin.def"
11311 #undef RS6000_BUILTIN_1
11312 #undef RS6000_BUILTIN_2
11313 #undef RS6000_BUILTIN_3
11314 #undef RS6000_BUILTIN_A
11315 #undef RS6000_BUILTIN_D
11316 #undef RS6000_BUILTIN_E
11317 #undef RS6000_BUILTIN_H
11318 #undef RS6000_BUILTIN_P
11319 #undef RS6000_BUILTIN_Q
11320 #undef RS6000_BUILTIN_S
11321 #undef RS6000_BUILTIN_X
11323 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
11324 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
11325 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
11326 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
11327 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
11328 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
11329 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
11330 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE) \
11331 { MASK, ICODE, NAME, ENUM },
11333 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
11334 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
11335 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11337 /* AltiVec predicates. */
11339 static const struct builtin_description bdesc_altivec_preds[] =
11341 #include "rs6000-builtin.def"
11344 /* SPE predicates. */
11345 #undef RS6000_BUILTIN_1
11346 #undef RS6000_BUILTIN_2
11347 #undef RS6000_BUILTIN_3
11348 #undef RS6000_BUILTIN_A
11349 #undef RS6000_BUILTIN_D
11350 #undef RS6000_BUILTIN_E
11351 #undef RS6000_BUILTIN_H
11352 #undef RS6000_BUILTIN_P
11353 #undef RS6000_BUILTIN_Q
11354 #undef RS6000_BUILTIN_S
11355 #undef RS6000_BUILTIN_X
11357 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
11358 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
11359 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
11360 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
11361 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
11362 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
11363 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
11364 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
11365 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
11366 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE) \
11367 { MASK, ICODE, NAME, ENUM },
11369 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11371 static const struct builtin_description bdesc_spe_predicates[] =
11373 #include "rs6000-builtin.def"
11376 /* SPE evsel predicates. */
11377 #undef RS6000_BUILTIN_1
11378 #undef RS6000_BUILTIN_2
11379 #undef RS6000_BUILTIN_3
11380 #undef RS6000_BUILTIN_A
11381 #undef RS6000_BUILTIN_D
11382 #undef RS6000_BUILTIN_E
11383 #undef RS6000_BUILTIN_H
11384 #undef RS6000_BUILTIN_P
11385 #undef RS6000_BUILTIN_Q
11386 #undef RS6000_BUILTIN_S
11387 #undef RS6000_BUILTIN_X
11389 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
11390 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
11391 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
11392 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
11393 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
11394 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE) \
11395 { MASK, ICODE, NAME, ENUM },
11397 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
11398 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
11399 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
11400 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
11401 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11403 static const struct builtin_description bdesc_spe_evsel[] =
11405 #include "rs6000-builtin.def"
11408 /* PAIRED predicates. */
11409 #undef RS6000_BUILTIN_1
11410 #undef RS6000_BUILTIN_2
11411 #undef RS6000_BUILTIN_3
11412 #undef RS6000_BUILTIN_A
11413 #undef RS6000_BUILTIN_D
11414 #undef RS6000_BUILTIN_E
11415 #undef RS6000_BUILTIN_H
11416 #undef RS6000_BUILTIN_P
11417 #undef RS6000_BUILTIN_Q
11418 #undef RS6000_BUILTIN_S
11419 #undef RS6000_BUILTIN_X
11421 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
11422 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
11423 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
11424 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
11425 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
11426 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
11427 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
11428 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
11429 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE) \
11430 { MASK, ICODE, NAME, ENUM },
11432 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
11433 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11435 static const struct builtin_description bdesc_paired_preds[] =
11437 #include "rs6000-builtin.def"
11440 /* ABS* operations. */
11442 #undef RS6000_BUILTIN_1
11443 #undef RS6000_BUILTIN_2
11444 #undef RS6000_BUILTIN_3
11445 #undef RS6000_BUILTIN_A
11446 #undef RS6000_BUILTIN_D
11447 #undef RS6000_BUILTIN_E
11448 #undef RS6000_BUILTIN_H
11449 #undef RS6000_BUILTIN_P
11450 #undef RS6000_BUILTIN_Q
11451 #undef RS6000_BUILTIN_S
11452 #undef RS6000_BUILTIN_X
11454 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
11455 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
11456 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
11457 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE) \
11458 { MASK, ICODE, NAME, ENUM },
11460 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
11461 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
11462 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
11463 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
11464 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
11465 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
11466 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11468 static const struct builtin_description bdesc_abs[] =
11470 #include "rs6000-builtin.def"
11473 /* Simple unary operations: VECb = foo (unsigned literal) or VECb =
11474 foo (VECa). */
11476 #undef RS6000_BUILTIN_1
11477 #undef RS6000_BUILTIN_2
11478 #undef RS6000_BUILTIN_3
11479 #undef RS6000_BUILTIN_A
11480 #undef RS6000_BUILTIN_D
11481 #undef RS6000_BUILTIN_E
11482 #undef RS6000_BUILTIN_H
11483 #undef RS6000_BUILTIN_P
11484 #undef RS6000_BUILTIN_Q
11485 #undef RS6000_BUILTIN_S
11486 #undef RS6000_BUILTIN_X
11488 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE) \
11489 { MASK, ICODE, NAME, ENUM },
11491 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
11492 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
11493 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
11494 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
11495 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
11496 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
11497 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
11498 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
11499 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
11500 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11502 static const struct builtin_description bdesc_1arg[] =
11504 #include "rs6000-builtin.def"
11507 /* HTM builtins. */
11508 #undef RS6000_BUILTIN_1
11509 #undef RS6000_BUILTIN_2
11510 #undef RS6000_BUILTIN_3
11511 #undef RS6000_BUILTIN_A
11512 #undef RS6000_BUILTIN_D
11513 #undef RS6000_BUILTIN_E
11514 #undef RS6000_BUILTIN_H
11515 #undef RS6000_BUILTIN_P
11516 #undef RS6000_BUILTIN_Q
11517 #undef RS6000_BUILTIN_S
11518 #undef RS6000_BUILTIN_X
11520 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
11521 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
11522 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
11523 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
11524 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
11525 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
11526 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE) \
11527 { MASK, ICODE, NAME, ENUM },
11529 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
11530 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
11531 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
11532 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11534 static const struct builtin_description bdesc_htm[] =
11536 #include "rs6000-builtin.def"
11539 #undef RS6000_BUILTIN_1
11540 #undef RS6000_BUILTIN_2
11541 #undef RS6000_BUILTIN_3
11542 #undef RS6000_BUILTIN_A
11543 #undef RS6000_BUILTIN_D
11544 #undef RS6000_BUILTIN_E
11545 #undef RS6000_BUILTIN_H
11546 #undef RS6000_BUILTIN_P
11547 #undef RS6000_BUILTIN_Q
11548 #undef RS6000_BUILTIN_S
11550 /* Return true if a builtin function is overloaded. */
11551 bool
11552 rs6000_overloaded_builtin_p (enum rs6000_builtins fncode)
11554 return (rs6000_builtin_info[(int)fncode].attr & RS6000_BTC_OVERLOADED) != 0;
11557 /* Expand an expression EXP that calls a builtin without arguments. */
11558 static rtx
11559 rs6000_expand_zeroop_builtin (enum insn_code icode, rtx target)
11561 rtx pat;
11562 enum machine_mode tmode = insn_data[icode].operand[0].mode;
11564 if (icode == CODE_FOR_nothing)
11565 /* Builtin not supported on this processor. */
11566 return 0;
11568 if (target == 0
11569 || GET_MODE (target) != tmode
11570 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11571 target = gen_reg_rtx (tmode);
11573 pat = GEN_FCN (icode) (target);
11574 if (! pat)
11575 return 0;
11576 emit_insn (pat);
11578 return target;
11582 static rtx
11583 rs6000_expand_mtfsf_builtin (enum insn_code icode, tree exp)
11585 rtx pat;
11586 tree arg0 = CALL_EXPR_ARG (exp, 0);
11587 tree arg1 = CALL_EXPR_ARG (exp, 1);
11588 rtx op0 = expand_normal (arg0);
11589 rtx op1 = expand_normal (arg1);
11590 enum machine_mode mode0 = insn_data[icode].operand[0].mode;
11591 enum machine_mode mode1 = insn_data[icode].operand[1].mode;
11593 if (icode == CODE_FOR_nothing)
11594 /* Builtin not supported on this processor. */
11595 return 0;
11597 /* If we got invalid arguments bail out before generating bad rtl. */
11598 if (arg0 == error_mark_node || arg1 == error_mark_node)
11599 return const0_rtx;
11601 if (GET_CODE (op0) != CONST_INT
11602 || INTVAL (op0) > 255
11603 || INTVAL (op0) < 0)
11605 error ("argument 1 must be an 8-bit field value");
11606 return const0_rtx;
11609 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
11610 op0 = copy_to_mode_reg (mode0, op0);
11612 if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
11613 op1 = copy_to_mode_reg (mode1, op1);
11615 pat = GEN_FCN (icode) (op0, op1);
11616 if (! pat)
11617 return const0_rtx;
11618 emit_insn (pat);
11620 return NULL_RTX;
11624 static rtx
11625 rs6000_expand_unop_builtin (enum insn_code icode, tree exp, rtx target)
11627 rtx pat;
11628 tree arg0 = CALL_EXPR_ARG (exp, 0);
11629 rtx op0 = expand_normal (arg0);
11630 enum machine_mode tmode = insn_data[icode].operand[0].mode;
11631 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11633 if (icode == CODE_FOR_nothing)
11634 /* Builtin not supported on this processor. */
11635 return 0;
11637 /* If we got invalid arguments bail out before generating bad rtl. */
11638 if (arg0 == error_mark_node)
11639 return const0_rtx;
11641 if (icode == CODE_FOR_altivec_vspltisb
11642 || icode == CODE_FOR_altivec_vspltish
11643 || icode == CODE_FOR_altivec_vspltisw
11644 || icode == CODE_FOR_spe_evsplatfi
11645 || icode == CODE_FOR_spe_evsplati)
11647 /* Only allow 5-bit *signed* literals. */
11648 if (GET_CODE (op0) != CONST_INT
11649 || INTVAL (op0) > 15
11650 || INTVAL (op0) < -16)
11652 error ("argument 1 must be a 5-bit signed literal");
11653 return const0_rtx;
11657 if (target == 0
11658 || GET_MODE (target) != tmode
11659 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11660 target = gen_reg_rtx (tmode);
11662 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11663 op0 = copy_to_mode_reg (mode0, op0);
11665 pat = GEN_FCN (icode) (target, op0);
11666 if (! pat)
11667 return 0;
11668 emit_insn (pat);
11670 return target;
11673 static rtx
11674 altivec_expand_abs_builtin (enum insn_code icode, tree exp, rtx target)
11676 rtx pat, scratch1, scratch2;
11677 tree arg0 = CALL_EXPR_ARG (exp, 0);
11678 rtx op0 = expand_normal (arg0);
11679 enum machine_mode tmode = insn_data[icode].operand[0].mode;
11680 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11682 /* If we have invalid arguments, bail out before generating bad rtl. */
11683 if (arg0 == error_mark_node)
11684 return const0_rtx;
11686 if (target == 0
11687 || GET_MODE (target) != tmode
11688 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11689 target = gen_reg_rtx (tmode);
11691 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11692 op0 = copy_to_mode_reg (mode0, op0);
11694 scratch1 = gen_reg_rtx (mode0);
11695 scratch2 = gen_reg_rtx (mode0);
11697 pat = GEN_FCN (icode) (target, op0, scratch1, scratch2);
11698 if (! pat)
11699 return 0;
11700 emit_insn (pat);
11702 return target;
11705 static rtx
11706 rs6000_expand_binop_builtin (enum insn_code icode, tree exp, rtx target)
11708 rtx pat;
11709 tree arg0 = CALL_EXPR_ARG (exp, 0);
11710 tree arg1 = CALL_EXPR_ARG (exp, 1);
11711 rtx op0 = expand_normal (arg0);
11712 rtx op1 = expand_normal (arg1);
11713 enum machine_mode tmode = insn_data[icode].operand[0].mode;
11714 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11715 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
11717 if (icode == CODE_FOR_nothing)
11718 /* Builtin not supported on this processor. */
11719 return 0;
11721 /* If we got invalid arguments bail out before generating bad rtl. */
11722 if (arg0 == error_mark_node || arg1 == error_mark_node)
11723 return const0_rtx;
11725 if (icode == CODE_FOR_altivec_vcfux
11726 || icode == CODE_FOR_altivec_vcfsx
11727 || icode == CODE_FOR_altivec_vctsxs
11728 || icode == CODE_FOR_altivec_vctuxs
11729 || icode == CODE_FOR_altivec_vspltb
11730 || icode == CODE_FOR_altivec_vsplth
11731 || icode == CODE_FOR_altivec_vspltw
11732 || icode == CODE_FOR_spe_evaddiw
11733 || icode == CODE_FOR_spe_evldd
11734 || icode == CODE_FOR_spe_evldh
11735 || icode == CODE_FOR_spe_evldw
11736 || icode == CODE_FOR_spe_evlhhesplat
11737 || icode == CODE_FOR_spe_evlhhossplat
11738 || icode == CODE_FOR_spe_evlhhousplat
11739 || icode == CODE_FOR_spe_evlwhe
11740 || icode == CODE_FOR_spe_evlwhos
11741 || icode == CODE_FOR_spe_evlwhou
11742 || icode == CODE_FOR_spe_evlwhsplat
11743 || icode == CODE_FOR_spe_evlwwsplat
11744 || icode == CODE_FOR_spe_evrlwi
11745 || icode == CODE_FOR_spe_evslwi
11746 || icode == CODE_FOR_spe_evsrwis
11747 || icode == CODE_FOR_spe_evsubifw
11748 || icode == CODE_FOR_spe_evsrwiu)
11750 /* Only allow 5-bit unsigned literals. */
11751 STRIP_NOPS (arg1);
11752 if (TREE_CODE (arg1) != INTEGER_CST
11753 || TREE_INT_CST_LOW (arg1) & ~0x1f)
11755 error ("argument 2 must be a 5-bit unsigned literal");
11756 return const0_rtx;
11760 if (target == 0
11761 || GET_MODE (target) != tmode
11762 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11763 target = gen_reg_rtx (tmode);
11765 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11766 op0 = copy_to_mode_reg (mode0, op0);
11767 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
11768 op1 = copy_to_mode_reg (mode1, op1);
11770 pat = GEN_FCN (icode) (target, op0, op1);
11771 if (! pat)
11772 return 0;
11773 emit_insn (pat);
11775 return target;
11778 static rtx
11779 altivec_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
11781 rtx pat, scratch;
11782 tree cr6_form = CALL_EXPR_ARG (exp, 0);
11783 tree arg0 = CALL_EXPR_ARG (exp, 1);
11784 tree arg1 = CALL_EXPR_ARG (exp, 2);
11785 rtx op0 = expand_normal (arg0);
11786 rtx op1 = expand_normal (arg1);
11787 enum machine_mode tmode = SImode;
11788 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11789 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
11790 int cr6_form_int;
11792 if (TREE_CODE (cr6_form) != INTEGER_CST)
11794 error ("argument 1 of __builtin_altivec_predicate must be a constant");
11795 return const0_rtx;
11797 else
11798 cr6_form_int = TREE_INT_CST_LOW (cr6_form);
11800 gcc_assert (mode0 == mode1);
11802 /* If we have invalid arguments, bail out before generating bad rtl. */
11803 if (arg0 == error_mark_node || arg1 == error_mark_node)
11804 return const0_rtx;
11806 if (target == 0
11807 || GET_MODE (target) != tmode
11808 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11809 target = gen_reg_rtx (tmode);
11811 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11812 op0 = copy_to_mode_reg (mode0, op0);
11813 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
11814 op1 = copy_to_mode_reg (mode1, op1);
11816 scratch = gen_reg_rtx (mode0);
11818 pat = GEN_FCN (icode) (scratch, op0, op1);
11819 if (! pat)
11820 return 0;
11821 emit_insn (pat);
11823 /* The vec_any* and vec_all* predicates use the same opcodes for two
11824 different operations, but the bits in CR6 will be different
11825 depending on what information we want. So we have to play tricks
11826 with CR6 to get the right bits out.
11828 If you think this is disgusting, look at the specs for the
11829 AltiVec predicates. */
11831 switch (cr6_form_int)
11833 case 0:
11834 emit_insn (gen_cr6_test_for_zero (target));
11835 break;
11836 case 1:
11837 emit_insn (gen_cr6_test_for_zero_reverse (target));
11838 break;
11839 case 2:
11840 emit_insn (gen_cr6_test_for_lt (target));
11841 break;
11842 case 3:
11843 emit_insn (gen_cr6_test_for_lt_reverse (target));
11844 break;
11845 default:
11846 error ("argument 1 of __builtin_altivec_predicate is out of range");
11847 break;
11850 return target;
11853 static rtx
11854 paired_expand_lv_builtin (enum insn_code icode, tree exp, rtx target)
11856 rtx pat, addr;
11857 tree arg0 = CALL_EXPR_ARG (exp, 0);
11858 tree arg1 = CALL_EXPR_ARG (exp, 1);
11859 enum machine_mode tmode = insn_data[icode].operand[0].mode;
11860 enum machine_mode mode0 = Pmode;
11861 enum machine_mode mode1 = Pmode;
11862 rtx op0 = expand_normal (arg0);
11863 rtx op1 = expand_normal (arg1);
11865 if (icode == CODE_FOR_nothing)
11866 /* Builtin not supported on this processor. */
11867 return 0;
11869 /* If we got invalid arguments bail out before generating bad rtl. */
11870 if (arg0 == error_mark_node || arg1 == error_mark_node)
11871 return const0_rtx;
11873 if (target == 0
11874 || GET_MODE (target) != tmode
11875 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11876 target = gen_reg_rtx (tmode);
11878 op1 = copy_to_mode_reg (mode1, op1);
11880 if (op0 == const0_rtx)
11882 addr = gen_rtx_MEM (tmode, op1);
11884 else
11886 op0 = copy_to_mode_reg (mode0, op0);
11887 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op0, op1));
11890 pat = GEN_FCN (icode) (target, addr);
11892 if (! pat)
11893 return 0;
11894 emit_insn (pat);
11896 return target;
11899 /* Return a constant vector for use as a little-endian permute control vector
11900 to reverse the order of elements of the given vector mode. */
11901 static rtx
11902 swap_selector_for_mode (enum machine_mode mode)
11904 /* These are little endian vectors, so their elements are reversed
11905 from what you would normally expect for a permute control vector. */
11906 unsigned int swap2[16] = {7,6,5,4,3,2,1,0,15,14,13,12,11,10,9,8};
11907 unsigned int swap4[16] = {3,2,1,0,7,6,5,4,11,10,9,8,15,14,13,12};
11908 unsigned int swap8[16] = {1,0,3,2,5,4,7,6,9,8,11,10,13,12,15,14};
11909 unsigned int swap16[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
11910 unsigned int *swaparray, i;
11911 rtx perm[16];
11913 switch (mode)
11915 case V2DFmode:
11916 case V2DImode:
11917 swaparray = swap2;
11918 break;
11919 case V4SFmode:
11920 case V4SImode:
11921 swaparray = swap4;
11922 break;
11923 case V8HImode:
11924 swaparray = swap8;
11925 break;
11926 case V16QImode:
11927 swaparray = swap16;
11928 break;
11929 default:
11930 gcc_unreachable ();
11933 for (i = 0; i < 16; ++i)
11934 perm[i] = GEN_INT (swaparray[i]);
11936 return force_reg (V16QImode, gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, perm)));
11939 /* Generate code for an "lvx", "lvxl", or "lve*x" built-in for a little endian target
11940 with -maltivec=be specified. Issue the load followed by an element-reversing
11941 permute. */
11942 void
11943 altivec_expand_lvx_be (rtx op0, rtx op1, enum machine_mode mode, unsigned unspec)
11945 rtx tmp = gen_reg_rtx (mode);
11946 rtx load = gen_rtx_SET (VOIDmode, tmp, op1);
11947 rtx lvx = gen_rtx_UNSPEC (mode, gen_rtvec (1, const0_rtx), unspec);
11948 rtx par = gen_rtx_PARALLEL (mode, gen_rtvec (2, load, lvx));
11949 rtx sel = swap_selector_for_mode (mode);
11950 rtx vperm = gen_rtx_UNSPEC (mode, gen_rtvec (3, tmp, tmp, sel), UNSPEC_VPERM);
11952 gcc_assert (REG_P (op0));
11953 emit_insn (par);
11954 emit_insn (gen_rtx_SET (VOIDmode, op0, vperm));
11957 /* Generate code for a "stvx" or "stvxl" built-in for a little endian target
11958 with -maltivec=be specified. Issue the store preceded by an element-reversing
11959 permute. */
11960 void
11961 altivec_expand_stvx_be (rtx op0, rtx op1, enum machine_mode mode, unsigned unspec)
11963 rtx tmp = gen_reg_rtx (mode);
11964 rtx store = gen_rtx_SET (VOIDmode, op0, tmp);
11965 rtx stvx = gen_rtx_UNSPEC (mode, gen_rtvec (1, const0_rtx), unspec);
11966 rtx par = gen_rtx_PARALLEL (mode, gen_rtvec (2, store, stvx));
11967 rtx sel = swap_selector_for_mode (mode);
11968 rtx vperm;
11970 gcc_assert (REG_P (op1));
11971 vperm = gen_rtx_UNSPEC (mode, gen_rtvec (3, op1, op1, sel), UNSPEC_VPERM);
11972 emit_insn (gen_rtx_SET (VOIDmode, tmp, vperm));
11973 emit_insn (par);
11976 /* Generate code for a "stve*x" built-in for a little endian target with -maltivec=be
11977 specified. Issue the store preceded by an element-reversing permute. */
11978 void
11979 altivec_expand_stvex_be (rtx op0, rtx op1, enum machine_mode mode, unsigned unspec)
11981 enum machine_mode inner_mode = GET_MODE_INNER (mode);
11982 rtx tmp = gen_reg_rtx (mode);
11983 rtx stvx = gen_rtx_UNSPEC (inner_mode, gen_rtvec (1, tmp), unspec);
11984 rtx sel = swap_selector_for_mode (mode);
11985 rtx vperm;
11987 gcc_assert (REG_P (op1));
11988 vperm = gen_rtx_UNSPEC (mode, gen_rtvec (3, op1, op1, sel), UNSPEC_VPERM);
11989 emit_insn (gen_rtx_SET (VOIDmode, tmp, vperm));
11990 emit_insn (gen_rtx_SET (VOIDmode, op0, stvx));
11993 static rtx
11994 altivec_expand_lv_builtin (enum insn_code icode, tree exp, rtx target, bool blk)
11996 rtx pat, addr;
11997 tree arg0 = CALL_EXPR_ARG (exp, 0);
11998 tree arg1 = CALL_EXPR_ARG (exp, 1);
11999 enum machine_mode tmode = insn_data[icode].operand[0].mode;
12000 enum machine_mode mode0 = Pmode;
12001 enum machine_mode mode1 = Pmode;
12002 rtx op0 = expand_normal (arg0);
12003 rtx op1 = expand_normal (arg1);
12005 if (icode == CODE_FOR_nothing)
12006 /* Builtin not supported on this processor. */
12007 return 0;
12009 /* If we got invalid arguments bail out before generating bad rtl. */
12010 if (arg0 == error_mark_node || arg1 == error_mark_node)
12011 return const0_rtx;
12013 if (target == 0
12014 || GET_MODE (target) != tmode
12015 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12016 target = gen_reg_rtx (tmode);
12018 op1 = copy_to_mode_reg (mode1, op1);
12020 if (op0 == const0_rtx)
12022 addr = gen_rtx_MEM (blk ? BLKmode : tmode, op1);
12024 else
12026 op0 = copy_to_mode_reg (mode0, op0);
12027 addr = gen_rtx_MEM (blk ? BLKmode : tmode, gen_rtx_PLUS (Pmode, op0, op1));
12030 pat = GEN_FCN (icode) (target, addr);
12032 if (! pat)
12033 return 0;
12034 emit_insn (pat);
12036 return target;
12039 static rtx
12040 spe_expand_stv_builtin (enum insn_code icode, tree exp)
12042 tree arg0 = CALL_EXPR_ARG (exp, 0);
12043 tree arg1 = CALL_EXPR_ARG (exp, 1);
12044 tree arg2 = CALL_EXPR_ARG (exp, 2);
12045 rtx op0 = expand_normal (arg0);
12046 rtx op1 = expand_normal (arg1);
12047 rtx op2 = expand_normal (arg2);
12048 rtx pat;
12049 enum machine_mode mode0 = insn_data[icode].operand[0].mode;
12050 enum machine_mode mode1 = insn_data[icode].operand[1].mode;
12051 enum machine_mode mode2 = insn_data[icode].operand[2].mode;
12053 /* Invalid arguments. Bail before doing anything stoopid! */
12054 if (arg0 == error_mark_node
12055 || arg1 == error_mark_node
12056 || arg2 == error_mark_node)
12057 return const0_rtx;
12059 if (! (*insn_data[icode].operand[2].predicate) (op0, mode2))
12060 op0 = copy_to_mode_reg (mode2, op0);
12061 if (! (*insn_data[icode].operand[0].predicate) (op1, mode0))
12062 op1 = copy_to_mode_reg (mode0, op1);
12063 if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
12064 op2 = copy_to_mode_reg (mode1, op2);
12066 pat = GEN_FCN (icode) (op1, op2, op0);
12067 if (pat)
12068 emit_insn (pat);
12069 return NULL_RTX;
12072 static rtx
12073 paired_expand_stv_builtin (enum insn_code icode, tree exp)
12075 tree arg0 = CALL_EXPR_ARG (exp, 0);
12076 tree arg1 = CALL_EXPR_ARG (exp, 1);
12077 tree arg2 = CALL_EXPR_ARG (exp, 2);
12078 rtx op0 = expand_normal (arg0);
12079 rtx op1 = expand_normal (arg1);
12080 rtx op2 = expand_normal (arg2);
12081 rtx pat, addr;
12082 enum machine_mode tmode = insn_data[icode].operand[0].mode;
12083 enum machine_mode mode1 = Pmode;
12084 enum machine_mode mode2 = Pmode;
12086 /* Invalid arguments. Bail before doing anything stoopid! */
12087 if (arg0 == error_mark_node
12088 || arg1 == error_mark_node
12089 || arg2 == error_mark_node)
12090 return const0_rtx;
12092 if (! (*insn_data[icode].operand[1].predicate) (op0, tmode))
12093 op0 = copy_to_mode_reg (tmode, op0);
12095 op2 = copy_to_mode_reg (mode2, op2);
12097 if (op1 == const0_rtx)
12099 addr = gen_rtx_MEM (tmode, op2);
12101 else
12103 op1 = copy_to_mode_reg (mode1, op1);
12104 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op1, op2));
12107 pat = GEN_FCN (icode) (addr, op0);
12108 if (pat)
12109 emit_insn (pat);
12110 return NULL_RTX;
12113 static rtx
12114 altivec_expand_stv_builtin (enum insn_code icode, tree exp)
12116 tree arg0 = CALL_EXPR_ARG (exp, 0);
12117 tree arg1 = CALL_EXPR_ARG (exp, 1);
12118 tree arg2 = CALL_EXPR_ARG (exp, 2);
12119 rtx op0 = expand_normal (arg0);
12120 rtx op1 = expand_normal (arg1);
12121 rtx op2 = expand_normal (arg2);
12122 rtx pat, addr;
12123 enum machine_mode tmode = insn_data[icode].operand[0].mode;
12124 enum machine_mode smode = insn_data[icode].operand[1].mode;
12125 enum machine_mode mode1 = Pmode;
12126 enum machine_mode mode2 = Pmode;
12128 /* Invalid arguments. Bail before doing anything stoopid! */
12129 if (arg0 == error_mark_node
12130 || arg1 == error_mark_node
12131 || arg2 == error_mark_node)
12132 return const0_rtx;
12134 if (! (*insn_data[icode].operand[1].predicate) (op0, smode))
12135 op0 = copy_to_mode_reg (smode, op0);
12137 op2 = copy_to_mode_reg (mode2, op2);
12139 if (op1 == const0_rtx)
12141 addr = gen_rtx_MEM (tmode, op2);
12143 else
12145 op1 = copy_to_mode_reg (mode1, op1);
12146 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op1, op2));
12149 pat = GEN_FCN (icode) (addr, op0);
12150 if (pat)
12151 emit_insn (pat);
12152 return NULL_RTX;
12155 /* Return the appropriate SPR number associated with the given builtin. */
12156 static inline HOST_WIDE_INT
12157 htm_spr_num (enum rs6000_builtins code)
12159 if (code == HTM_BUILTIN_GET_TFHAR
12160 || code == HTM_BUILTIN_SET_TFHAR)
12161 return TFHAR_SPR;
12162 else if (code == HTM_BUILTIN_GET_TFIAR
12163 || code == HTM_BUILTIN_SET_TFIAR)
12164 return TFIAR_SPR;
12165 else if (code == HTM_BUILTIN_GET_TEXASR
12166 || code == HTM_BUILTIN_SET_TEXASR)
12167 return TEXASR_SPR;
12168 gcc_assert (code == HTM_BUILTIN_GET_TEXASRU
12169 || code == HTM_BUILTIN_SET_TEXASRU);
12170 return TEXASRU_SPR;
12173 /* Return the appropriate SPR regno associated with the given builtin. */
12174 static inline HOST_WIDE_INT
12175 htm_spr_regno (enum rs6000_builtins code)
12177 if (code == HTM_BUILTIN_GET_TFHAR
12178 || code == HTM_BUILTIN_SET_TFHAR)
12179 return TFHAR_REGNO;
12180 else if (code == HTM_BUILTIN_GET_TFIAR
12181 || code == HTM_BUILTIN_SET_TFIAR)
12182 return TFIAR_REGNO;
12183 gcc_assert (code == HTM_BUILTIN_GET_TEXASR
12184 || code == HTM_BUILTIN_SET_TEXASR
12185 || code == HTM_BUILTIN_GET_TEXASRU
12186 || code == HTM_BUILTIN_SET_TEXASRU);
12187 return TEXASR_REGNO;
12190 /* Return the correct ICODE value depending on whether we are
12191 setting or reading the HTM SPRs. */
12192 static inline enum insn_code
12193 rs6000_htm_spr_icode (bool nonvoid)
12195 if (nonvoid)
12196 return (TARGET_64BIT) ? CODE_FOR_htm_mfspr_di : CODE_FOR_htm_mfspr_si;
12197 else
12198 return (TARGET_64BIT) ? CODE_FOR_htm_mtspr_di : CODE_FOR_htm_mtspr_si;
12201 /* Expand the HTM builtin in EXP and store the result in TARGET.
12202 Store true in *EXPANDEDP if we found a builtin to expand. */
12203 static rtx
12204 htm_expand_builtin (tree exp, rtx target, bool * expandedp)
12206 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
12207 bool nonvoid = TREE_TYPE (TREE_TYPE (fndecl)) != void_type_node;
12208 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
12209 const struct builtin_description *d;
12210 size_t i;
12212 *expandedp = false;
12214 /* Expand the HTM builtins. */
12215 d = bdesc_htm;
12216 for (i = 0; i < ARRAY_SIZE (bdesc_htm); i++, d++)
12217 if (d->code == fcode)
12219 rtx op[MAX_HTM_OPERANDS], pat;
12220 int nopnds = 0;
12221 tree arg;
12222 call_expr_arg_iterator iter;
12223 unsigned attr = rs6000_builtin_info[fcode].attr;
12224 enum insn_code icode = d->icode;
12226 if (attr & RS6000_BTC_SPR)
12227 icode = rs6000_htm_spr_icode (nonvoid);
12229 if (nonvoid)
12231 enum machine_mode tmode = insn_data[icode].operand[0].mode;
12232 if (!target
12233 || GET_MODE (target) != tmode
12234 || !(*insn_data[icode].operand[0].predicate) (target, tmode))
12235 target = gen_reg_rtx (tmode);
12236 op[nopnds++] = target;
12239 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
12241 const struct insn_operand_data *insn_op;
12243 if (arg == error_mark_node || nopnds >= MAX_HTM_OPERANDS)
12244 return NULL_RTX;
12246 insn_op = &insn_data[icode].operand[nopnds];
12248 op[nopnds] = expand_normal (arg);
12250 if (!(*insn_op->predicate) (op[nopnds], insn_op->mode))
12252 if (!strcmp (insn_op->constraint, "n"))
12254 int arg_num = (nonvoid) ? nopnds : nopnds + 1;
12255 if (!CONST_INT_P (op[nopnds]))
12256 error ("argument %d must be an unsigned literal", arg_num);
12257 else
12258 error ("argument %d is an unsigned literal that is "
12259 "out of range", arg_num);
12260 return const0_rtx;
12262 op[nopnds] = copy_to_mode_reg (insn_op->mode, op[nopnds]);
12265 nopnds++;
12268 /* Handle the builtins for extended mnemonics. These accept
12269 no arguments, but map to builtins that take arguments. */
12270 switch (fcode)
12272 case HTM_BUILTIN_TENDALL: /* Alias for: tend. 1 */
12273 case HTM_BUILTIN_TRESUME: /* Alias for: tsr. 1 */
12274 op[nopnds++] = GEN_INT (1);
12275 #ifdef ENABLE_CHECKING
12276 attr |= RS6000_BTC_UNARY;
12277 #endif
12278 break;
12279 case HTM_BUILTIN_TSUSPEND: /* Alias for: tsr. 0 */
12280 op[nopnds++] = GEN_INT (0);
12281 #ifdef ENABLE_CHECKING
12282 attr |= RS6000_BTC_UNARY;
12283 #endif
12284 break;
12285 default:
12286 break;
12289 /* If this builtin accesses SPRs, then pass in the appropriate
12290 SPR number and SPR regno as the last two operands. */
12291 if (attr & RS6000_BTC_SPR)
12293 op[nopnds++] = gen_rtx_CONST_INT (Pmode, htm_spr_num (fcode));
12294 op[nopnds++] = gen_rtx_REG (Pmode, htm_spr_regno (fcode));
12297 #ifdef ENABLE_CHECKING
12298 int expected_nopnds = 0;
12299 if ((attr & RS6000_BTC_TYPE_MASK) == RS6000_BTC_UNARY)
12300 expected_nopnds = 1;
12301 else if ((attr & RS6000_BTC_TYPE_MASK) == RS6000_BTC_BINARY)
12302 expected_nopnds = 2;
12303 else if ((attr & RS6000_BTC_TYPE_MASK) == RS6000_BTC_TERNARY)
12304 expected_nopnds = 3;
12305 if (!(attr & RS6000_BTC_VOID))
12306 expected_nopnds += 1;
12307 if (attr & RS6000_BTC_SPR)
12308 expected_nopnds += 2;
12310 gcc_assert (nopnds == expected_nopnds && nopnds <= MAX_HTM_OPERANDS);
12311 #endif
12313 switch (nopnds)
12315 case 1:
12316 pat = GEN_FCN (icode) (op[0]);
12317 break;
12318 case 2:
12319 pat = GEN_FCN (icode) (op[0], op[1]);
12320 break;
12321 case 3:
12322 pat = GEN_FCN (icode) (op[0], op[1], op[2]);
12323 break;
12324 case 4:
12325 pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3]);
12326 break;
12327 default:
12328 gcc_unreachable ();
12330 if (!pat)
12331 return NULL_RTX;
12332 emit_insn (pat);
12334 *expandedp = true;
12335 if (nonvoid)
12336 return target;
12337 return const0_rtx;
12340 return NULL_RTX;
12343 static rtx
12344 rs6000_expand_ternop_builtin (enum insn_code icode, tree exp, rtx target)
12346 rtx pat;
12347 tree arg0 = CALL_EXPR_ARG (exp, 0);
12348 tree arg1 = CALL_EXPR_ARG (exp, 1);
12349 tree arg2 = CALL_EXPR_ARG (exp, 2);
12350 rtx op0 = expand_normal (arg0);
12351 rtx op1 = expand_normal (arg1);
12352 rtx op2 = expand_normal (arg2);
12353 enum machine_mode tmode = insn_data[icode].operand[0].mode;
12354 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12355 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
12356 enum machine_mode mode2 = insn_data[icode].operand[3].mode;
12358 if (icode == CODE_FOR_nothing)
12359 /* Builtin not supported on this processor. */
12360 return 0;
12362 /* If we got invalid arguments bail out before generating bad rtl. */
12363 if (arg0 == error_mark_node
12364 || arg1 == error_mark_node
12365 || arg2 == error_mark_node)
12366 return const0_rtx;
12368 /* Check and prepare argument depending on the instruction code.
12370 Note that a switch statement instead of the sequence of tests
12371 would be incorrect as many of the CODE_FOR values could be
12372 CODE_FOR_nothing and that would yield multiple alternatives
12373 with identical values. We'd never reach here at runtime in
12374 this case. */
12375 if (icode == CODE_FOR_altivec_vsldoi_v4sf
12376 || icode == CODE_FOR_altivec_vsldoi_v4si
12377 || icode == CODE_FOR_altivec_vsldoi_v8hi
12378 || icode == CODE_FOR_altivec_vsldoi_v16qi)
12380 /* Only allow 4-bit unsigned literals. */
12381 STRIP_NOPS (arg2);
12382 if (TREE_CODE (arg2) != INTEGER_CST
12383 || TREE_INT_CST_LOW (arg2) & ~0xf)
12385 error ("argument 3 must be a 4-bit unsigned literal");
12386 return const0_rtx;
12389 else if (icode == CODE_FOR_vsx_xxpermdi_v2df
12390 || icode == CODE_FOR_vsx_xxpermdi_v2di
12391 || icode == CODE_FOR_vsx_xxsldwi_v16qi
12392 || icode == CODE_FOR_vsx_xxsldwi_v8hi
12393 || icode == CODE_FOR_vsx_xxsldwi_v4si
12394 || icode == CODE_FOR_vsx_xxsldwi_v4sf
12395 || icode == CODE_FOR_vsx_xxsldwi_v2di
12396 || icode == CODE_FOR_vsx_xxsldwi_v2df)
12398 /* Only allow 2-bit unsigned literals. */
12399 STRIP_NOPS (arg2);
12400 if (TREE_CODE (arg2) != INTEGER_CST
12401 || TREE_INT_CST_LOW (arg2) & ~0x3)
12403 error ("argument 3 must be a 2-bit unsigned literal");
12404 return const0_rtx;
12407 else if (icode == CODE_FOR_vsx_set_v2df
12408 || icode == CODE_FOR_vsx_set_v2di
12409 || icode == CODE_FOR_bcdadd
12410 || icode == CODE_FOR_bcdadd_lt
12411 || icode == CODE_FOR_bcdadd_eq
12412 || icode == CODE_FOR_bcdadd_gt
12413 || icode == CODE_FOR_bcdsub
12414 || icode == CODE_FOR_bcdsub_lt
12415 || icode == CODE_FOR_bcdsub_eq
12416 || icode == CODE_FOR_bcdsub_gt)
12418 /* Only allow 1-bit unsigned literals. */
12419 STRIP_NOPS (arg2);
12420 if (TREE_CODE (arg2) != INTEGER_CST
12421 || TREE_INT_CST_LOW (arg2) & ~0x1)
12423 error ("argument 3 must be a 1-bit unsigned literal");
12424 return const0_rtx;
12427 else if (icode == CODE_FOR_dfp_ddedpd_dd
12428 || icode == CODE_FOR_dfp_ddedpd_td)
12430 /* Only allow 2-bit unsigned literals where the value is 0 or 2. */
12431 STRIP_NOPS (arg0);
12432 if (TREE_CODE (arg0) != INTEGER_CST
12433 || TREE_INT_CST_LOW (arg2) & ~0x3)
12435 error ("argument 1 must be 0 or 2");
12436 return const0_rtx;
12439 else if (icode == CODE_FOR_dfp_denbcd_dd
12440 || icode == CODE_FOR_dfp_denbcd_td)
12442 /* Only allow 1-bit unsigned literals. */
12443 STRIP_NOPS (arg0);
12444 if (TREE_CODE (arg0) != INTEGER_CST
12445 || TREE_INT_CST_LOW (arg0) & ~0x1)
12447 error ("argument 1 must be a 1-bit unsigned literal");
12448 return const0_rtx;
12451 else if (icode == CODE_FOR_dfp_dscli_dd
12452 || icode == CODE_FOR_dfp_dscli_td
12453 || icode == CODE_FOR_dfp_dscri_dd
12454 || icode == CODE_FOR_dfp_dscri_td)
12456 /* Only allow 6-bit unsigned literals. */
12457 STRIP_NOPS (arg1);
12458 if (TREE_CODE (arg1) != INTEGER_CST
12459 || TREE_INT_CST_LOW (arg1) & ~0x3f)
12461 error ("argument 2 must be a 6-bit unsigned literal");
12462 return const0_rtx;
12465 else if (icode == CODE_FOR_crypto_vshasigmaw
12466 || icode == CODE_FOR_crypto_vshasigmad)
12468 /* Check whether the 2nd and 3rd arguments are integer constants and in
12469 range and prepare arguments. */
12470 STRIP_NOPS (arg1);
12471 if (TREE_CODE (arg1) != INTEGER_CST || wi::geu_p (arg1, 2))
12473 error ("argument 2 must be 0 or 1");
12474 return const0_rtx;
12477 STRIP_NOPS (arg2);
12478 if (TREE_CODE (arg2) != INTEGER_CST || wi::geu_p (arg1, 16))
12480 error ("argument 3 must be in the range 0..15");
12481 return const0_rtx;
12485 if (target == 0
12486 || GET_MODE (target) != tmode
12487 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12488 target = gen_reg_rtx (tmode);
12490 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12491 op0 = copy_to_mode_reg (mode0, op0);
12492 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12493 op1 = copy_to_mode_reg (mode1, op1);
12494 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12495 op2 = copy_to_mode_reg (mode2, op2);
12497 if (TARGET_PAIRED_FLOAT && icode == CODE_FOR_selv2sf4)
12498 pat = GEN_FCN (icode) (target, op0, op1, op2, CONST0_RTX (SFmode));
12499 else
12500 pat = GEN_FCN (icode) (target, op0, op1, op2);
12501 if (! pat)
12502 return 0;
12503 emit_insn (pat);
12505 return target;
12508 /* Expand the lvx builtins. */
12509 static rtx
12510 altivec_expand_ld_builtin (tree exp, rtx target, bool *expandedp)
12512 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
12513 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
12514 tree arg0;
12515 enum machine_mode tmode, mode0;
12516 rtx pat, op0;
12517 enum insn_code icode;
12519 switch (fcode)
12521 case ALTIVEC_BUILTIN_LD_INTERNAL_16qi:
12522 icode = CODE_FOR_vector_altivec_load_v16qi;
12523 break;
12524 case ALTIVEC_BUILTIN_LD_INTERNAL_8hi:
12525 icode = CODE_FOR_vector_altivec_load_v8hi;
12526 break;
12527 case ALTIVEC_BUILTIN_LD_INTERNAL_4si:
12528 icode = CODE_FOR_vector_altivec_load_v4si;
12529 break;
12530 case ALTIVEC_BUILTIN_LD_INTERNAL_4sf:
12531 icode = CODE_FOR_vector_altivec_load_v4sf;
12532 break;
12533 case ALTIVEC_BUILTIN_LD_INTERNAL_2df:
12534 icode = CODE_FOR_vector_altivec_load_v2df;
12535 break;
12536 case ALTIVEC_BUILTIN_LD_INTERNAL_2di:
12537 icode = CODE_FOR_vector_altivec_load_v2di;
12538 case ALTIVEC_BUILTIN_LD_INTERNAL_1ti:
12539 icode = CODE_FOR_vector_altivec_load_v1ti;
12540 break;
12541 default:
12542 *expandedp = false;
12543 return NULL_RTX;
12546 *expandedp = true;
12548 arg0 = CALL_EXPR_ARG (exp, 0);
12549 op0 = expand_normal (arg0);
12550 tmode = insn_data[icode].operand[0].mode;
12551 mode0 = insn_data[icode].operand[1].mode;
12553 if (target == 0
12554 || GET_MODE (target) != tmode
12555 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12556 target = gen_reg_rtx (tmode);
12558 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12559 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
12561 pat = GEN_FCN (icode) (target, op0);
12562 if (! pat)
12563 return 0;
12564 emit_insn (pat);
12565 return target;
12568 /* Expand the stvx builtins. */
12569 static rtx
12570 altivec_expand_st_builtin (tree exp, rtx target ATTRIBUTE_UNUSED,
12571 bool *expandedp)
12573 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
12574 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
12575 tree arg0, arg1;
12576 enum machine_mode mode0, mode1;
12577 rtx pat, op0, op1;
12578 enum insn_code icode;
12580 switch (fcode)
12582 case ALTIVEC_BUILTIN_ST_INTERNAL_16qi:
12583 icode = CODE_FOR_vector_altivec_store_v16qi;
12584 break;
12585 case ALTIVEC_BUILTIN_ST_INTERNAL_8hi:
12586 icode = CODE_FOR_vector_altivec_store_v8hi;
12587 break;
12588 case ALTIVEC_BUILTIN_ST_INTERNAL_4si:
12589 icode = CODE_FOR_vector_altivec_store_v4si;
12590 break;
12591 case ALTIVEC_BUILTIN_ST_INTERNAL_4sf:
12592 icode = CODE_FOR_vector_altivec_store_v4sf;
12593 break;
12594 case ALTIVEC_BUILTIN_ST_INTERNAL_2df:
12595 icode = CODE_FOR_vector_altivec_store_v2df;
12596 break;
12597 case ALTIVEC_BUILTIN_ST_INTERNAL_2di:
12598 icode = CODE_FOR_vector_altivec_store_v2di;
12599 case ALTIVEC_BUILTIN_ST_INTERNAL_1ti:
12600 icode = CODE_FOR_vector_altivec_store_v1ti;
12601 break;
12602 default:
12603 *expandedp = false;
12604 return NULL_RTX;
12607 arg0 = CALL_EXPR_ARG (exp, 0);
12608 arg1 = CALL_EXPR_ARG (exp, 1);
12609 op0 = expand_normal (arg0);
12610 op1 = expand_normal (arg1);
12611 mode0 = insn_data[icode].operand[0].mode;
12612 mode1 = insn_data[icode].operand[1].mode;
12614 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
12615 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
12616 if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
12617 op1 = copy_to_mode_reg (mode1, op1);
12619 pat = GEN_FCN (icode) (op0, op1);
12620 if (pat)
12621 emit_insn (pat);
12623 *expandedp = true;
12624 return NULL_RTX;
12627 /* Expand the dst builtins. */
12628 static rtx
12629 altivec_expand_dst_builtin (tree exp, rtx target ATTRIBUTE_UNUSED,
12630 bool *expandedp)
12632 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
12633 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
12634 tree arg0, arg1, arg2;
12635 enum machine_mode mode0, mode1;
12636 rtx pat, op0, op1, op2;
12637 const struct builtin_description *d;
12638 size_t i;
12640 *expandedp = false;
12642 /* Handle DST variants. */
12643 d = bdesc_dst;
12644 for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
12645 if (d->code == fcode)
12647 arg0 = CALL_EXPR_ARG (exp, 0);
12648 arg1 = CALL_EXPR_ARG (exp, 1);
12649 arg2 = CALL_EXPR_ARG (exp, 2);
12650 op0 = expand_normal (arg0);
12651 op1 = expand_normal (arg1);
12652 op2 = expand_normal (arg2);
12653 mode0 = insn_data[d->icode].operand[0].mode;
12654 mode1 = insn_data[d->icode].operand[1].mode;
12656 /* Invalid arguments, bail out before generating bad rtl. */
12657 if (arg0 == error_mark_node
12658 || arg1 == error_mark_node
12659 || arg2 == error_mark_node)
12660 return const0_rtx;
12662 *expandedp = true;
12663 STRIP_NOPS (arg2);
12664 if (TREE_CODE (arg2) != INTEGER_CST
12665 || TREE_INT_CST_LOW (arg2) & ~0x3)
12667 error ("argument to %qs must be a 2-bit unsigned literal", d->name);
12668 return const0_rtx;
12671 if (! (*insn_data[d->icode].operand[0].predicate) (op0, mode0))
12672 op0 = copy_to_mode_reg (Pmode, op0);
12673 if (! (*insn_data[d->icode].operand[1].predicate) (op1, mode1))
12674 op1 = copy_to_mode_reg (mode1, op1);
12676 pat = GEN_FCN (d->icode) (op0, op1, op2);
12677 if (pat != 0)
12678 emit_insn (pat);
12680 return NULL_RTX;
12683 return NULL_RTX;
12686 /* Expand vec_init builtin. */
12687 static rtx
12688 altivec_expand_vec_init_builtin (tree type, tree exp, rtx target)
12690 enum machine_mode tmode = TYPE_MODE (type);
12691 enum machine_mode inner_mode = GET_MODE_INNER (tmode);
12692 int i, n_elt = GET_MODE_NUNITS (tmode);
12694 gcc_assert (VECTOR_MODE_P (tmode));
12695 gcc_assert (n_elt == call_expr_nargs (exp));
12697 if (!target || !register_operand (target, tmode))
12698 target = gen_reg_rtx (tmode);
12700 /* If we have a vector compromised of a single element, such as V1TImode, do
12701 the initialization directly. */
12702 if (n_elt == 1 && GET_MODE_SIZE (tmode) == GET_MODE_SIZE (inner_mode))
12704 rtx x = expand_normal (CALL_EXPR_ARG (exp, 0));
12705 emit_move_insn (target, gen_lowpart (tmode, x));
12707 else
12709 rtvec v = rtvec_alloc (n_elt);
12711 for (i = 0; i < n_elt; ++i)
12713 rtx x = expand_normal (CALL_EXPR_ARG (exp, i));
12714 RTVEC_ELT (v, i) = gen_lowpart (inner_mode, x);
12717 rs6000_expand_vector_init (target, gen_rtx_PARALLEL (tmode, v));
12720 return target;
12723 /* Return the integer constant in ARG. Constrain it to be in the range
12724 of the subparts of VEC_TYPE; issue an error if not. */
12726 static int
12727 get_element_number (tree vec_type, tree arg)
12729 unsigned HOST_WIDE_INT elt, max = TYPE_VECTOR_SUBPARTS (vec_type) - 1;
12731 if (!tree_fits_uhwi_p (arg)
12732 || (elt = tree_to_uhwi (arg), elt > max))
12734 error ("selector must be an integer constant in the range 0..%wi", max);
12735 return 0;
12738 return elt;
12741 /* Expand vec_set builtin. */
12742 static rtx
12743 altivec_expand_vec_set_builtin (tree exp)
12745 enum machine_mode tmode, mode1;
12746 tree arg0, arg1, arg2;
12747 int elt;
12748 rtx op0, op1;
12750 arg0 = CALL_EXPR_ARG (exp, 0);
12751 arg1 = CALL_EXPR_ARG (exp, 1);
12752 arg2 = CALL_EXPR_ARG (exp, 2);
12754 tmode = TYPE_MODE (TREE_TYPE (arg0));
12755 mode1 = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
12756 gcc_assert (VECTOR_MODE_P (tmode));
12758 op0 = expand_expr (arg0, NULL_RTX, tmode, EXPAND_NORMAL);
12759 op1 = expand_expr (arg1, NULL_RTX, mode1, EXPAND_NORMAL);
12760 elt = get_element_number (TREE_TYPE (arg0), arg2);
12762 if (GET_MODE (op1) != mode1 && GET_MODE (op1) != VOIDmode)
12763 op1 = convert_modes (mode1, GET_MODE (op1), op1, true);
12765 op0 = force_reg (tmode, op0);
12766 op1 = force_reg (mode1, op1);
12768 rs6000_expand_vector_set (op0, op1, elt);
12770 return op0;
12773 /* Expand vec_ext builtin. */
12774 static rtx
12775 altivec_expand_vec_ext_builtin (tree exp, rtx target)
12777 enum machine_mode tmode, mode0;
12778 tree arg0, arg1;
12779 int elt;
12780 rtx op0;
12782 arg0 = CALL_EXPR_ARG (exp, 0);
12783 arg1 = CALL_EXPR_ARG (exp, 1);
12785 op0 = expand_normal (arg0);
12786 elt = get_element_number (TREE_TYPE (arg0), arg1);
12788 tmode = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
12789 mode0 = TYPE_MODE (TREE_TYPE (arg0));
12790 gcc_assert (VECTOR_MODE_P (mode0));
12792 op0 = force_reg (mode0, op0);
12794 if (optimize || !target || !register_operand (target, tmode))
12795 target = gen_reg_rtx (tmode);
12797 rs6000_expand_vector_extract (target, op0, elt);
12799 return target;
12802 /* Expand the builtin in EXP and store the result in TARGET. Store
12803 true in *EXPANDEDP if we found a builtin to expand. */
12804 static rtx
12805 altivec_expand_builtin (tree exp, rtx target, bool *expandedp)
12807 const struct builtin_description *d;
12808 size_t i;
12809 enum insn_code icode;
12810 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
12811 tree arg0;
12812 rtx op0, pat;
12813 enum machine_mode tmode, mode0;
12814 enum rs6000_builtins fcode
12815 = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
12817 if (rs6000_overloaded_builtin_p (fcode))
12819 *expandedp = true;
12820 error ("unresolved overload for Altivec builtin %qF", fndecl);
12822 /* Given it is invalid, just generate a normal call. */
12823 return expand_call (exp, target, false);
12826 target = altivec_expand_ld_builtin (exp, target, expandedp);
12827 if (*expandedp)
12828 return target;
12830 target = altivec_expand_st_builtin (exp, target, expandedp);
12831 if (*expandedp)
12832 return target;
12834 target = altivec_expand_dst_builtin (exp, target, expandedp);
12835 if (*expandedp)
12836 return target;
12838 *expandedp = true;
12840 switch (fcode)
12842 case ALTIVEC_BUILTIN_STVX_V2DF:
12843 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v2df, exp);
12844 case ALTIVEC_BUILTIN_STVX_V2DI:
12845 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v2di, exp);
12846 case ALTIVEC_BUILTIN_STVX_V4SF:
12847 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v4sf, exp);
12848 case ALTIVEC_BUILTIN_STVX:
12849 case ALTIVEC_BUILTIN_STVX_V4SI:
12850 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v4si, exp);
12851 case ALTIVEC_BUILTIN_STVX_V8HI:
12852 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v8hi, exp);
12853 case ALTIVEC_BUILTIN_STVX_V16QI:
12854 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v16qi, exp);
12855 case ALTIVEC_BUILTIN_STVEBX:
12856 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvebx, exp);
12857 case ALTIVEC_BUILTIN_STVEHX:
12858 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvehx, exp);
12859 case ALTIVEC_BUILTIN_STVEWX:
12860 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvewx, exp);
12861 case ALTIVEC_BUILTIN_STVXL_V2DF:
12862 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v2df, exp);
12863 case ALTIVEC_BUILTIN_STVXL_V2DI:
12864 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v2di, exp);
12865 case ALTIVEC_BUILTIN_STVXL_V4SF:
12866 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v4sf, exp);
12867 case ALTIVEC_BUILTIN_STVXL:
12868 case ALTIVEC_BUILTIN_STVXL_V4SI:
12869 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v4si, exp);
12870 case ALTIVEC_BUILTIN_STVXL_V8HI:
12871 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v8hi, exp);
12872 case ALTIVEC_BUILTIN_STVXL_V16QI:
12873 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v16qi, exp);
12875 case ALTIVEC_BUILTIN_STVLX:
12876 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvlx, exp);
12877 case ALTIVEC_BUILTIN_STVLXL:
12878 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvlxl, exp);
12879 case ALTIVEC_BUILTIN_STVRX:
12880 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvrx, exp);
12881 case ALTIVEC_BUILTIN_STVRXL:
12882 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvrxl, exp);
12884 case VSX_BUILTIN_STXVD2X_V1TI:
12885 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v1ti, exp);
12886 case VSX_BUILTIN_STXVD2X_V2DF:
12887 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v2df, exp);
12888 case VSX_BUILTIN_STXVD2X_V2DI:
12889 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v2di, exp);
12890 case VSX_BUILTIN_STXVW4X_V4SF:
12891 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v4sf, exp);
12892 case VSX_BUILTIN_STXVW4X_V4SI:
12893 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v4si, exp);
12894 case VSX_BUILTIN_STXVW4X_V8HI:
12895 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v8hi, exp);
12896 case VSX_BUILTIN_STXVW4X_V16QI:
12897 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v16qi, exp);
12899 case ALTIVEC_BUILTIN_MFVSCR:
12900 icode = CODE_FOR_altivec_mfvscr;
12901 tmode = insn_data[icode].operand[0].mode;
12903 if (target == 0
12904 || GET_MODE (target) != tmode
12905 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12906 target = gen_reg_rtx (tmode);
12908 pat = GEN_FCN (icode) (target);
12909 if (! pat)
12910 return 0;
12911 emit_insn (pat);
12912 return target;
12914 case ALTIVEC_BUILTIN_MTVSCR:
12915 icode = CODE_FOR_altivec_mtvscr;
12916 arg0 = CALL_EXPR_ARG (exp, 0);
12917 op0 = expand_normal (arg0);
12918 mode0 = insn_data[icode].operand[0].mode;
12920 /* If we got invalid arguments bail out before generating bad rtl. */
12921 if (arg0 == error_mark_node)
12922 return const0_rtx;
12924 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
12925 op0 = copy_to_mode_reg (mode0, op0);
12927 pat = GEN_FCN (icode) (op0);
12928 if (pat)
12929 emit_insn (pat);
12930 return NULL_RTX;
12932 case ALTIVEC_BUILTIN_DSSALL:
12933 emit_insn (gen_altivec_dssall ());
12934 return NULL_RTX;
12936 case ALTIVEC_BUILTIN_DSS:
12937 icode = CODE_FOR_altivec_dss;
12938 arg0 = CALL_EXPR_ARG (exp, 0);
12939 STRIP_NOPS (arg0);
12940 op0 = expand_normal (arg0);
12941 mode0 = insn_data[icode].operand[0].mode;
12943 /* If we got invalid arguments bail out before generating bad rtl. */
12944 if (arg0 == error_mark_node)
12945 return const0_rtx;
12947 if (TREE_CODE (arg0) != INTEGER_CST
12948 || TREE_INT_CST_LOW (arg0) & ~0x3)
12950 error ("argument to dss must be a 2-bit unsigned literal");
12951 return const0_rtx;
12954 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
12955 op0 = copy_to_mode_reg (mode0, op0);
12957 emit_insn (gen_altivec_dss (op0));
12958 return NULL_RTX;
12960 case ALTIVEC_BUILTIN_VEC_INIT_V4SI:
12961 case ALTIVEC_BUILTIN_VEC_INIT_V8HI:
12962 case ALTIVEC_BUILTIN_VEC_INIT_V16QI:
12963 case ALTIVEC_BUILTIN_VEC_INIT_V4SF:
12964 case VSX_BUILTIN_VEC_INIT_V2DF:
12965 case VSX_BUILTIN_VEC_INIT_V2DI:
12966 case VSX_BUILTIN_VEC_INIT_V1TI:
12967 return altivec_expand_vec_init_builtin (TREE_TYPE (exp), exp, target);
12969 case ALTIVEC_BUILTIN_VEC_SET_V4SI:
12970 case ALTIVEC_BUILTIN_VEC_SET_V8HI:
12971 case ALTIVEC_BUILTIN_VEC_SET_V16QI:
12972 case ALTIVEC_BUILTIN_VEC_SET_V4SF:
12973 case VSX_BUILTIN_VEC_SET_V2DF:
12974 case VSX_BUILTIN_VEC_SET_V2DI:
12975 case VSX_BUILTIN_VEC_SET_V1TI:
12976 return altivec_expand_vec_set_builtin (exp);
12978 case ALTIVEC_BUILTIN_VEC_EXT_V4SI:
12979 case ALTIVEC_BUILTIN_VEC_EXT_V8HI:
12980 case ALTIVEC_BUILTIN_VEC_EXT_V16QI:
12981 case ALTIVEC_BUILTIN_VEC_EXT_V4SF:
12982 case VSX_BUILTIN_VEC_EXT_V2DF:
12983 case VSX_BUILTIN_VEC_EXT_V2DI:
12984 case VSX_BUILTIN_VEC_EXT_V1TI:
12985 return altivec_expand_vec_ext_builtin (exp, target);
12987 default:
12988 break;
12989 /* Fall through. */
12992 /* Expand abs* operations. */
12993 d = bdesc_abs;
12994 for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
12995 if (d->code == fcode)
12996 return altivec_expand_abs_builtin (d->icode, exp, target);
12998 /* Expand the AltiVec predicates. */
12999 d = bdesc_altivec_preds;
13000 for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, d++)
13001 if (d->code == fcode)
13002 return altivec_expand_predicate_builtin (d->icode, exp, target);
13004 /* LV* are funky. We initialized them differently. */
13005 switch (fcode)
13007 case ALTIVEC_BUILTIN_LVSL:
13008 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsl,
13009 exp, target, false);
13010 case ALTIVEC_BUILTIN_LVSR:
13011 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsr,
13012 exp, target, false);
13013 case ALTIVEC_BUILTIN_LVEBX:
13014 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvebx,
13015 exp, target, false);
13016 case ALTIVEC_BUILTIN_LVEHX:
13017 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvehx,
13018 exp, target, false);
13019 case ALTIVEC_BUILTIN_LVEWX:
13020 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvewx,
13021 exp, target, false);
13022 case ALTIVEC_BUILTIN_LVXL_V2DF:
13023 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v2df,
13024 exp, target, false);
13025 case ALTIVEC_BUILTIN_LVXL_V2DI:
13026 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v2di,
13027 exp, target, false);
13028 case ALTIVEC_BUILTIN_LVXL_V4SF:
13029 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v4sf,
13030 exp, target, false);
13031 case ALTIVEC_BUILTIN_LVXL:
13032 case ALTIVEC_BUILTIN_LVXL_V4SI:
13033 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v4si,
13034 exp, target, false);
13035 case ALTIVEC_BUILTIN_LVXL_V8HI:
13036 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v8hi,
13037 exp, target, false);
13038 case ALTIVEC_BUILTIN_LVXL_V16QI:
13039 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v16qi,
13040 exp, target, false);
13041 case ALTIVEC_BUILTIN_LVX_V2DF:
13042 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v2df,
13043 exp, target, false);
13044 case ALTIVEC_BUILTIN_LVX_V2DI:
13045 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v2di,
13046 exp, target, false);
13047 case ALTIVEC_BUILTIN_LVX_V4SF:
13048 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v4sf,
13049 exp, target, false);
13050 case ALTIVEC_BUILTIN_LVX:
13051 case ALTIVEC_BUILTIN_LVX_V4SI:
13052 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v4si,
13053 exp, target, false);
13054 case ALTIVEC_BUILTIN_LVX_V8HI:
13055 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v8hi,
13056 exp, target, false);
13057 case ALTIVEC_BUILTIN_LVX_V16QI:
13058 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v16qi,
13059 exp, target, false);
13060 case ALTIVEC_BUILTIN_LVLX:
13061 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvlx,
13062 exp, target, true);
13063 case ALTIVEC_BUILTIN_LVLXL:
13064 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvlxl,
13065 exp, target, true);
13066 case ALTIVEC_BUILTIN_LVRX:
13067 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvrx,
13068 exp, target, true);
13069 case ALTIVEC_BUILTIN_LVRXL:
13070 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvrxl,
13071 exp, target, true);
13072 case VSX_BUILTIN_LXVD2X_V1TI:
13073 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v1ti,
13074 exp, target, false);
13075 case VSX_BUILTIN_LXVD2X_V2DF:
13076 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v2df,
13077 exp, target, false);
13078 case VSX_BUILTIN_LXVD2X_V2DI:
13079 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v2di,
13080 exp, target, false);
13081 case VSX_BUILTIN_LXVW4X_V4SF:
13082 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v4sf,
13083 exp, target, false);
13084 case VSX_BUILTIN_LXVW4X_V4SI:
13085 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v4si,
13086 exp, target, false);
13087 case VSX_BUILTIN_LXVW4X_V8HI:
13088 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v8hi,
13089 exp, target, false);
13090 case VSX_BUILTIN_LXVW4X_V16QI:
13091 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v16qi,
13092 exp, target, false);
13093 break;
13094 default:
13095 break;
13096 /* Fall through. */
13099 *expandedp = false;
13100 return NULL_RTX;
13103 /* Expand the builtin in EXP and store the result in TARGET. Store
13104 true in *EXPANDEDP if we found a builtin to expand. */
13105 static rtx
13106 paired_expand_builtin (tree exp, rtx target, bool * expandedp)
13108 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
13109 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
13110 const struct builtin_description *d;
13111 size_t i;
13113 *expandedp = true;
13115 switch (fcode)
13117 case PAIRED_BUILTIN_STX:
13118 return paired_expand_stv_builtin (CODE_FOR_paired_stx, exp);
13119 case PAIRED_BUILTIN_LX:
13120 return paired_expand_lv_builtin (CODE_FOR_paired_lx, exp, target);
13121 default:
13122 break;
13123 /* Fall through. */
13126 /* Expand the paired predicates. */
13127 d = bdesc_paired_preds;
13128 for (i = 0; i < ARRAY_SIZE (bdesc_paired_preds); i++, d++)
13129 if (d->code == fcode)
13130 return paired_expand_predicate_builtin (d->icode, exp, target);
13132 *expandedp = false;
13133 return NULL_RTX;
13136 /* Binops that need to be initialized manually, but can be expanded
13137 automagically by rs6000_expand_binop_builtin. */
13138 static const struct builtin_description bdesc_2arg_spe[] =
13140 { RS6000_BTM_SPE, CODE_FOR_spe_evlddx, "__builtin_spe_evlddx", SPE_BUILTIN_EVLDDX },
13141 { RS6000_BTM_SPE, CODE_FOR_spe_evldwx, "__builtin_spe_evldwx", SPE_BUILTIN_EVLDWX },
13142 { RS6000_BTM_SPE, CODE_FOR_spe_evldhx, "__builtin_spe_evldhx", SPE_BUILTIN_EVLDHX },
13143 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhex, "__builtin_spe_evlwhex", SPE_BUILTIN_EVLWHEX },
13144 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhoux, "__builtin_spe_evlwhoux", SPE_BUILTIN_EVLWHOUX },
13145 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhosx, "__builtin_spe_evlwhosx", SPE_BUILTIN_EVLWHOSX },
13146 { RS6000_BTM_SPE, CODE_FOR_spe_evlwwsplatx, "__builtin_spe_evlwwsplatx", SPE_BUILTIN_EVLWWSPLATX },
13147 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhsplatx, "__builtin_spe_evlwhsplatx", SPE_BUILTIN_EVLWHSPLATX },
13148 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhesplatx, "__builtin_spe_evlhhesplatx", SPE_BUILTIN_EVLHHESPLATX },
13149 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhousplatx, "__builtin_spe_evlhhousplatx", SPE_BUILTIN_EVLHHOUSPLATX },
13150 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhossplatx, "__builtin_spe_evlhhossplatx", SPE_BUILTIN_EVLHHOSSPLATX },
13151 { RS6000_BTM_SPE, CODE_FOR_spe_evldd, "__builtin_spe_evldd", SPE_BUILTIN_EVLDD },
13152 { RS6000_BTM_SPE, CODE_FOR_spe_evldw, "__builtin_spe_evldw", SPE_BUILTIN_EVLDW },
13153 { RS6000_BTM_SPE, CODE_FOR_spe_evldh, "__builtin_spe_evldh", SPE_BUILTIN_EVLDH },
13154 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhe, "__builtin_spe_evlwhe", SPE_BUILTIN_EVLWHE },
13155 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhou, "__builtin_spe_evlwhou", SPE_BUILTIN_EVLWHOU },
13156 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhos, "__builtin_spe_evlwhos", SPE_BUILTIN_EVLWHOS },
13157 { RS6000_BTM_SPE, CODE_FOR_spe_evlwwsplat, "__builtin_spe_evlwwsplat", SPE_BUILTIN_EVLWWSPLAT },
13158 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhsplat, "__builtin_spe_evlwhsplat", SPE_BUILTIN_EVLWHSPLAT },
13159 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhesplat, "__builtin_spe_evlhhesplat", SPE_BUILTIN_EVLHHESPLAT },
13160 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhousplat, "__builtin_spe_evlhhousplat", SPE_BUILTIN_EVLHHOUSPLAT },
13161 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhossplat, "__builtin_spe_evlhhossplat", SPE_BUILTIN_EVLHHOSSPLAT }
13164 /* Expand the builtin in EXP and store the result in TARGET. Store
13165 true in *EXPANDEDP if we found a builtin to expand.
13167 This expands the SPE builtins that are not simple unary and binary
13168 operations. */
13169 static rtx
13170 spe_expand_builtin (tree exp, rtx target, bool *expandedp)
13172 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
13173 tree arg1, arg0;
13174 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
13175 enum insn_code icode;
13176 enum machine_mode tmode, mode0;
13177 rtx pat, op0;
13178 const struct builtin_description *d;
13179 size_t i;
13181 *expandedp = true;
13183 /* Syntax check for a 5-bit unsigned immediate. */
13184 switch (fcode)
13186 case SPE_BUILTIN_EVSTDD:
13187 case SPE_BUILTIN_EVSTDH:
13188 case SPE_BUILTIN_EVSTDW:
13189 case SPE_BUILTIN_EVSTWHE:
13190 case SPE_BUILTIN_EVSTWHO:
13191 case SPE_BUILTIN_EVSTWWE:
13192 case SPE_BUILTIN_EVSTWWO:
13193 arg1 = CALL_EXPR_ARG (exp, 2);
13194 if (TREE_CODE (arg1) != INTEGER_CST
13195 || TREE_INT_CST_LOW (arg1) & ~0x1f)
13197 error ("argument 2 must be a 5-bit unsigned literal");
13198 return const0_rtx;
13200 break;
13201 default:
13202 break;
13205 /* The evsplat*i instructions are not quite generic. */
13206 switch (fcode)
13208 case SPE_BUILTIN_EVSPLATFI:
13209 return rs6000_expand_unop_builtin (CODE_FOR_spe_evsplatfi,
13210 exp, target);
13211 case SPE_BUILTIN_EVSPLATI:
13212 return rs6000_expand_unop_builtin (CODE_FOR_spe_evsplati,
13213 exp, target);
13214 default:
13215 break;
13218 d = bdesc_2arg_spe;
13219 for (i = 0; i < ARRAY_SIZE (bdesc_2arg_spe); ++i, ++d)
13220 if (d->code == fcode)
13221 return rs6000_expand_binop_builtin (d->icode, exp, target);
13223 d = bdesc_spe_predicates;
13224 for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, ++d)
13225 if (d->code == fcode)
13226 return spe_expand_predicate_builtin (d->icode, exp, target);
13228 d = bdesc_spe_evsel;
13229 for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, ++d)
13230 if (d->code == fcode)
13231 return spe_expand_evsel_builtin (d->icode, exp, target);
13233 switch (fcode)
13235 case SPE_BUILTIN_EVSTDDX:
13236 return spe_expand_stv_builtin (CODE_FOR_spe_evstddx, exp);
13237 case SPE_BUILTIN_EVSTDHX:
13238 return spe_expand_stv_builtin (CODE_FOR_spe_evstdhx, exp);
13239 case SPE_BUILTIN_EVSTDWX:
13240 return spe_expand_stv_builtin (CODE_FOR_spe_evstdwx, exp);
13241 case SPE_BUILTIN_EVSTWHEX:
13242 return spe_expand_stv_builtin (CODE_FOR_spe_evstwhex, exp);
13243 case SPE_BUILTIN_EVSTWHOX:
13244 return spe_expand_stv_builtin (CODE_FOR_spe_evstwhox, exp);
13245 case SPE_BUILTIN_EVSTWWEX:
13246 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwex, exp);
13247 case SPE_BUILTIN_EVSTWWOX:
13248 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwox, exp);
13249 case SPE_BUILTIN_EVSTDD:
13250 return spe_expand_stv_builtin (CODE_FOR_spe_evstdd, exp);
13251 case SPE_BUILTIN_EVSTDH:
13252 return spe_expand_stv_builtin (CODE_FOR_spe_evstdh, exp);
13253 case SPE_BUILTIN_EVSTDW:
13254 return spe_expand_stv_builtin (CODE_FOR_spe_evstdw, exp);
13255 case SPE_BUILTIN_EVSTWHE:
13256 return spe_expand_stv_builtin (CODE_FOR_spe_evstwhe, exp);
13257 case SPE_BUILTIN_EVSTWHO:
13258 return spe_expand_stv_builtin (CODE_FOR_spe_evstwho, exp);
13259 case SPE_BUILTIN_EVSTWWE:
13260 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwe, exp);
13261 case SPE_BUILTIN_EVSTWWO:
13262 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwo, exp);
13263 case SPE_BUILTIN_MFSPEFSCR:
13264 icode = CODE_FOR_spe_mfspefscr;
13265 tmode = insn_data[icode].operand[0].mode;
13267 if (target == 0
13268 || GET_MODE (target) != tmode
13269 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13270 target = gen_reg_rtx (tmode);
13272 pat = GEN_FCN (icode) (target);
13273 if (! pat)
13274 return 0;
13275 emit_insn (pat);
13276 return target;
13277 case SPE_BUILTIN_MTSPEFSCR:
13278 icode = CODE_FOR_spe_mtspefscr;
13279 arg0 = CALL_EXPR_ARG (exp, 0);
13280 op0 = expand_normal (arg0);
13281 mode0 = insn_data[icode].operand[0].mode;
13283 if (arg0 == error_mark_node)
13284 return const0_rtx;
13286 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
13287 op0 = copy_to_mode_reg (mode0, op0);
13289 pat = GEN_FCN (icode) (op0);
13290 if (pat)
13291 emit_insn (pat);
13292 return NULL_RTX;
13293 default:
13294 break;
13297 *expandedp = false;
13298 return NULL_RTX;
13301 static rtx
13302 paired_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
13304 rtx pat, scratch, tmp;
13305 tree form = CALL_EXPR_ARG (exp, 0);
13306 tree arg0 = CALL_EXPR_ARG (exp, 1);
13307 tree arg1 = CALL_EXPR_ARG (exp, 2);
13308 rtx op0 = expand_normal (arg0);
13309 rtx op1 = expand_normal (arg1);
13310 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
13311 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
13312 int form_int;
13313 enum rtx_code code;
13315 if (TREE_CODE (form) != INTEGER_CST)
13317 error ("argument 1 of __builtin_paired_predicate must be a constant");
13318 return const0_rtx;
13320 else
13321 form_int = TREE_INT_CST_LOW (form);
13323 gcc_assert (mode0 == mode1);
13325 if (arg0 == error_mark_node || arg1 == error_mark_node)
13326 return const0_rtx;
13328 if (target == 0
13329 || GET_MODE (target) != SImode
13330 || !(*insn_data[icode].operand[0].predicate) (target, SImode))
13331 target = gen_reg_rtx (SImode);
13332 if (!(*insn_data[icode].operand[1].predicate) (op0, mode0))
13333 op0 = copy_to_mode_reg (mode0, op0);
13334 if (!(*insn_data[icode].operand[2].predicate) (op1, mode1))
13335 op1 = copy_to_mode_reg (mode1, op1);
13337 scratch = gen_reg_rtx (CCFPmode);
13339 pat = GEN_FCN (icode) (scratch, op0, op1);
13340 if (!pat)
13341 return const0_rtx;
13343 emit_insn (pat);
13345 switch (form_int)
13347 /* LT bit. */
13348 case 0:
13349 code = LT;
13350 break;
13351 /* GT bit. */
13352 case 1:
13353 code = GT;
13354 break;
13355 /* EQ bit. */
13356 case 2:
13357 code = EQ;
13358 break;
13359 /* UN bit. */
13360 case 3:
13361 emit_insn (gen_move_from_CR_ov_bit (target, scratch));
13362 return target;
13363 default:
13364 error ("argument 1 of __builtin_paired_predicate is out of range");
13365 return const0_rtx;
13368 tmp = gen_rtx_fmt_ee (code, SImode, scratch, const0_rtx);
13369 emit_move_insn (target, tmp);
13370 return target;
13373 static rtx
13374 spe_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
13376 rtx pat, scratch, tmp;
13377 tree form = CALL_EXPR_ARG (exp, 0);
13378 tree arg0 = CALL_EXPR_ARG (exp, 1);
13379 tree arg1 = CALL_EXPR_ARG (exp, 2);
13380 rtx op0 = expand_normal (arg0);
13381 rtx op1 = expand_normal (arg1);
13382 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
13383 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
13384 int form_int;
13385 enum rtx_code code;
13387 if (TREE_CODE (form) != INTEGER_CST)
13389 error ("argument 1 of __builtin_spe_predicate must be a constant");
13390 return const0_rtx;
13392 else
13393 form_int = TREE_INT_CST_LOW (form);
13395 gcc_assert (mode0 == mode1);
13397 if (arg0 == error_mark_node || arg1 == error_mark_node)
13398 return const0_rtx;
13400 if (target == 0
13401 || GET_MODE (target) != SImode
13402 || ! (*insn_data[icode].operand[0].predicate) (target, SImode))
13403 target = gen_reg_rtx (SImode);
13405 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13406 op0 = copy_to_mode_reg (mode0, op0);
13407 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
13408 op1 = copy_to_mode_reg (mode1, op1);
13410 scratch = gen_reg_rtx (CCmode);
13412 pat = GEN_FCN (icode) (scratch, op0, op1);
13413 if (! pat)
13414 return const0_rtx;
13415 emit_insn (pat);
13417 /* There are 4 variants for each predicate: _any_, _all_, _upper_,
13418 _lower_. We use one compare, but look in different bits of the
13419 CR for each variant.
13421 There are 2 elements in each SPE simd type (upper/lower). The CR
13422 bits are set as follows:
13424 BIT0 | BIT 1 | BIT 2 | BIT 3
13425 U | L | (U | L) | (U & L)
13427 So, for an "all" relationship, BIT 3 would be set.
13428 For an "any" relationship, BIT 2 would be set. Etc.
13430 Following traditional nomenclature, these bits map to:
13432 BIT0 | BIT 1 | BIT 2 | BIT 3
13433 LT | GT | EQ | OV
13435 Later, we will generate rtl to look in the LT/EQ/EQ/OV bits.
13438 switch (form_int)
13440 /* All variant. OV bit. */
13441 case 0:
13442 /* We need to get to the OV bit, which is the ORDERED bit. We
13443 could generate (ordered:SI (reg:CC xx) (const_int 0)), but
13444 that's ugly and will make validate_condition_mode die.
13445 So let's just use another pattern. */
13446 emit_insn (gen_move_from_CR_ov_bit (target, scratch));
13447 return target;
13448 /* Any variant. EQ bit. */
13449 case 1:
13450 code = EQ;
13451 break;
13452 /* Upper variant. LT bit. */
13453 case 2:
13454 code = LT;
13455 break;
13456 /* Lower variant. GT bit. */
13457 case 3:
13458 code = GT;
13459 break;
13460 default:
13461 error ("argument 1 of __builtin_spe_predicate is out of range");
13462 return const0_rtx;
13465 tmp = gen_rtx_fmt_ee (code, SImode, scratch, const0_rtx);
13466 emit_move_insn (target, tmp);
13468 return target;
13471 /* The evsel builtins look like this:
13473 e = __builtin_spe_evsel_OP (a, b, c, d);
13475 and work like this:
13477 e[upper] = a[upper] *OP* b[upper] ? c[upper] : d[upper];
13478 e[lower] = a[lower] *OP* b[lower] ? c[lower] : d[lower];
13481 static rtx
13482 spe_expand_evsel_builtin (enum insn_code icode, tree exp, rtx target)
13484 rtx pat, scratch;
13485 tree arg0 = CALL_EXPR_ARG (exp, 0);
13486 tree arg1 = CALL_EXPR_ARG (exp, 1);
13487 tree arg2 = CALL_EXPR_ARG (exp, 2);
13488 tree arg3 = CALL_EXPR_ARG (exp, 3);
13489 rtx op0 = expand_normal (arg0);
13490 rtx op1 = expand_normal (arg1);
13491 rtx op2 = expand_normal (arg2);
13492 rtx op3 = expand_normal (arg3);
13493 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
13494 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
13496 gcc_assert (mode0 == mode1);
13498 if (arg0 == error_mark_node || arg1 == error_mark_node
13499 || arg2 == error_mark_node || arg3 == error_mark_node)
13500 return const0_rtx;
13502 if (target == 0
13503 || GET_MODE (target) != mode0
13504 || ! (*insn_data[icode].operand[0].predicate) (target, mode0))
13505 target = gen_reg_rtx (mode0);
13507 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13508 op0 = copy_to_mode_reg (mode0, op0);
13509 if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
13510 op1 = copy_to_mode_reg (mode0, op1);
13511 if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
13512 op2 = copy_to_mode_reg (mode0, op2);
13513 if (! (*insn_data[icode].operand[1].predicate) (op3, mode1))
13514 op3 = copy_to_mode_reg (mode0, op3);
13516 /* Generate the compare. */
13517 scratch = gen_reg_rtx (CCmode);
13518 pat = GEN_FCN (icode) (scratch, op0, op1);
13519 if (! pat)
13520 return const0_rtx;
13521 emit_insn (pat);
13523 if (mode0 == V2SImode)
13524 emit_insn (gen_spe_evsel (target, op2, op3, scratch));
13525 else
13526 emit_insn (gen_spe_evsel_fs (target, op2, op3, scratch));
13528 return target;
13531 /* Raise an error message for a builtin function that is called without the
13532 appropriate target options being set. */
13534 static void
13535 rs6000_invalid_builtin (enum rs6000_builtins fncode)
13537 size_t uns_fncode = (size_t)fncode;
13538 const char *name = rs6000_builtin_info[uns_fncode].name;
13539 HOST_WIDE_INT fnmask = rs6000_builtin_info[uns_fncode].mask;
13541 gcc_assert (name != NULL);
13542 if ((fnmask & RS6000_BTM_CELL) != 0)
13543 error ("Builtin function %s is only valid for the cell processor", name);
13544 else if ((fnmask & RS6000_BTM_VSX) != 0)
13545 error ("Builtin function %s requires the -mvsx option", name);
13546 else if ((fnmask & RS6000_BTM_HTM) != 0)
13547 error ("Builtin function %s requires the -mhtm option", name);
13548 else if ((fnmask & RS6000_BTM_ALTIVEC) != 0)
13549 error ("Builtin function %s requires the -maltivec option", name);
13550 else if ((fnmask & RS6000_BTM_PAIRED) != 0)
13551 error ("Builtin function %s requires the -mpaired option", name);
13552 else if ((fnmask & RS6000_BTM_SPE) != 0)
13553 error ("Builtin function %s requires the -mspe option", name);
13554 else if ((fnmask & (RS6000_BTM_DFP | RS6000_BTM_P8_VECTOR))
13555 == (RS6000_BTM_DFP | RS6000_BTM_P8_VECTOR))
13556 error ("Builtin function %s requires the -mhard-dfp and"
13557 "-mpower8-vector options", name);
13558 else if ((fnmask & RS6000_BTM_DFP) != 0)
13559 error ("Builtin function %s requires the -mhard-dfp option", name);
13560 else if ((fnmask & RS6000_BTM_P8_VECTOR) != 0)
13561 error ("Builtin function %s requires the -mpower8-vector option", name);
13562 else if ((fnmask & RS6000_BTM_HARD_FLOAT) != 0)
13563 error ("Builtin function %s requires the -mhard-float option", name);
13564 else
13565 error ("Builtin function %s is not supported with the current options",
13566 name);
13569 /* Expand an expression EXP that calls a built-in function,
13570 with result going to TARGET if that's convenient
13571 (and in mode MODE if that's convenient).
13572 SUBTARGET may be used as the target for computing one of EXP's operands.
13573 IGNORE is nonzero if the value is to be ignored. */
13575 static rtx
13576 rs6000_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
13577 enum machine_mode mode ATTRIBUTE_UNUSED,
13578 int ignore ATTRIBUTE_UNUSED)
13580 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
13581 enum rs6000_builtins fcode
13582 = (enum rs6000_builtins)DECL_FUNCTION_CODE (fndecl);
13583 size_t uns_fcode = (size_t)fcode;
13584 const struct builtin_description *d;
13585 size_t i;
13586 rtx ret;
13587 bool success;
13588 HOST_WIDE_INT mask = rs6000_builtin_info[uns_fcode].mask;
13589 bool func_valid_p = ((rs6000_builtin_mask & mask) == mask);
13591 if (TARGET_DEBUG_BUILTIN)
13593 enum insn_code icode = rs6000_builtin_info[uns_fcode].icode;
13594 const char *name1 = rs6000_builtin_info[uns_fcode].name;
13595 const char *name2 = ((icode != CODE_FOR_nothing)
13596 ? get_insn_name ((int)icode)
13597 : "nothing");
13598 const char *name3;
13600 switch (rs6000_builtin_info[uns_fcode].attr & RS6000_BTC_TYPE_MASK)
13602 default: name3 = "unknown"; break;
13603 case RS6000_BTC_SPECIAL: name3 = "special"; break;
13604 case RS6000_BTC_UNARY: name3 = "unary"; break;
13605 case RS6000_BTC_BINARY: name3 = "binary"; break;
13606 case RS6000_BTC_TERNARY: name3 = "ternary"; break;
13607 case RS6000_BTC_PREDICATE: name3 = "predicate"; break;
13608 case RS6000_BTC_ABS: name3 = "abs"; break;
13609 case RS6000_BTC_EVSEL: name3 = "evsel"; break;
13610 case RS6000_BTC_DST: name3 = "dst"; break;
13614 fprintf (stderr,
13615 "rs6000_expand_builtin, %s (%d), insn = %s (%d), type=%s%s\n",
13616 (name1) ? name1 : "---", fcode,
13617 (name2) ? name2 : "---", (int)icode,
13618 name3,
13619 func_valid_p ? "" : ", not valid");
13622 if (!func_valid_p)
13624 rs6000_invalid_builtin (fcode);
13626 /* Given it is invalid, just generate a normal call. */
13627 return expand_call (exp, target, ignore);
13630 switch (fcode)
13632 case RS6000_BUILTIN_RECIP:
13633 return rs6000_expand_binop_builtin (CODE_FOR_recipdf3, exp, target);
13635 case RS6000_BUILTIN_RECIPF:
13636 return rs6000_expand_binop_builtin (CODE_FOR_recipsf3, exp, target);
13638 case RS6000_BUILTIN_RSQRTF:
13639 return rs6000_expand_unop_builtin (CODE_FOR_rsqrtsf2, exp, target);
13641 case RS6000_BUILTIN_RSQRT:
13642 return rs6000_expand_unop_builtin (CODE_FOR_rsqrtdf2, exp, target);
13644 case POWER7_BUILTIN_BPERMD:
13645 return rs6000_expand_binop_builtin (((TARGET_64BIT)
13646 ? CODE_FOR_bpermd_di
13647 : CODE_FOR_bpermd_si), exp, target);
13649 case RS6000_BUILTIN_GET_TB:
13650 return rs6000_expand_zeroop_builtin (CODE_FOR_rs6000_get_timebase,
13651 target);
13653 case RS6000_BUILTIN_MFTB:
13654 return rs6000_expand_zeroop_builtin (((TARGET_64BIT)
13655 ? CODE_FOR_rs6000_mftb_di
13656 : CODE_FOR_rs6000_mftb_si),
13657 target);
13659 case RS6000_BUILTIN_MFFS:
13660 return rs6000_expand_zeroop_builtin (CODE_FOR_rs6000_mffs, target);
13662 case RS6000_BUILTIN_MTFSF:
13663 return rs6000_expand_mtfsf_builtin (CODE_FOR_rs6000_mtfsf, exp);
13665 case ALTIVEC_BUILTIN_MASK_FOR_LOAD:
13666 case ALTIVEC_BUILTIN_MASK_FOR_STORE:
13668 int icode = (BYTES_BIG_ENDIAN ? (int) CODE_FOR_altivec_lvsr
13669 : (int) CODE_FOR_altivec_lvsl);
13670 enum machine_mode tmode = insn_data[icode].operand[0].mode;
13671 enum machine_mode mode = insn_data[icode].operand[1].mode;
13672 tree arg;
13673 rtx op, addr, pat;
13675 gcc_assert (TARGET_ALTIVEC);
13677 arg = CALL_EXPR_ARG (exp, 0);
13678 gcc_assert (POINTER_TYPE_P (TREE_TYPE (arg)));
13679 op = expand_expr (arg, NULL_RTX, Pmode, EXPAND_NORMAL);
13680 addr = memory_address (mode, op);
13681 if (fcode == ALTIVEC_BUILTIN_MASK_FOR_STORE)
13682 op = addr;
13683 else
13685 /* For the load case need to negate the address. */
13686 op = gen_reg_rtx (GET_MODE (addr));
13687 emit_insn (gen_rtx_SET (VOIDmode, op,
13688 gen_rtx_NEG (GET_MODE (addr), addr)));
13690 op = gen_rtx_MEM (mode, op);
13692 if (target == 0
13693 || GET_MODE (target) != tmode
13694 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13695 target = gen_reg_rtx (tmode);
13697 /*pat = gen_altivec_lvsr (target, op);*/
13698 pat = GEN_FCN (icode) (target, op);
13699 if (!pat)
13700 return 0;
13701 emit_insn (pat);
13703 return target;
13706 case ALTIVEC_BUILTIN_VCFUX:
13707 case ALTIVEC_BUILTIN_VCFSX:
13708 case ALTIVEC_BUILTIN_VCTUXS:
13709 case ALTIVEC_BUILTIN_VCTSXS:
13710 /* FIXME: There's got to be a nicer way to handle this case than
13711 constructing a new CALL_EXPR. */
13712 if (call_expr_nargs (exp) == 1)
13714 exp = build_call_nary (TREE_TYPE (exp), CALL_EXPR_FN (exp),
13715 2, CALL_EXPR_ARG (exp, 0), integer_zero_node);
13717 break;
13719 default:
13720 break;
13723 if (TARGET_ALTIVEC)
13725 ret = altivec_expand_builtin (exp, target, &success);
13727 if (success)
13728 return ret;
13730 if (TARGET_SPE)
13732 ret = spe_expand_builtin (exp, target, &success);
13734 if (success)
13735 return ret;
13737 if (TARGET_PAIRED_FLOAT)
13739 ret = paired_expand_builtin (exp, target, &success);
13741 if (success)
13742 return ret;
13744 if (TARGET_HTM)
13746 ret = htm_expand_builtin (exp, target, &success);
13748 if (success)
13749 return ret;
13752 unsigned attr = rs6000_builtin_info[uns_fcode].attr & RS6000_BTC_TYPE_MASK;
13753 gcc_assert (attr == RS6000_BTC_UNARY
13754 || attr == RS6000_BTC_BINARY
13755 || attr == RS6000_BTC_TERNARY);
13757 /* Handle simple unary operations. */
13758 d = bdesc_1arg;
13759 for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
13760 if (d->code == fcode)
13761 return rs6000_expand_unop_builtin (d->icode, exp, target);
13763 /* Handle simple binary operations. */
13764 d = bdesc_2arg;
13765 for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
13766 if (d->code == fcode)
13767 return rs6000_expand_binop_builtin (d->icode, exp, target);
13769 /* Handle simple ternary operations. */
13770 d = bdesc_3arg;
13771 for (i = 0; i < ARRAY_SIZE (bdesc_3arg); i++, d++)
13772 if (d->code == fcode)
13773 return rs6000_expand_ternop_builtin (d->icode, exp, target);
13775 gcc_unreachable ();
13778 static void
13779 rs6000_init_builtins (void)
13781 tree tdecl;
13782 tree ftype;
13783 enum machine_mode mode;
13785 if (TARGET_DEBUG_BUILTIN)
13786 fprintf (stderr, "rs6000_init_builtins%s%s%s%s\n",
13787 (TARGET_PAIRED_FLOAT) ? ", paired" : "",
13788 (TARGET_SPE) ? ", spe" : "",
13789 (TARGET_ALTIVEC) ? ", altivec" : "",
13790 (TARGET_VSX) ? ", vsx" : "");
13792 V2SI_type_node = build_vector_type (intSI_type_node, 2);
13793 V2SF_type_node = build_vector_type (float_type_node, 2);
13794 V2DI_type_node = build_vector_type (intDI_type_node, 2);
13795 V2DF_type_node = build_vector_type (double_type_node, 2);
13796 V4HI_type_node = build_vector_type (intHI_type_node, 4);
13797 V4SI_type_node = build_vector_type (intSI_type_node, 4);
13798 V4SF_type_node = build_vector_type (float_type_node, 4);
13799 V8HI_type_node = build_vector_type (intHI_type_node, 8);
13800 V16QI_type_node = build_vector_type (intQI_type_node, 16);
13802 unsigned_V16QI_type_node = build_vector_type (unsigned_intQI_type_node, 16);
13803 unsigned_V8HI_type_node = build_vector_type (unsigned_intHI_type_node, 8);
13804 unsigned_V4SI_type_node = build_vector_type (unsigned_intSI_type_node, 4);
13805 unsigned_V2DI_type_node = build_vector_type (unsigned_intDI_type_node, 2);
13807 opaque_V2SF_type_node = build_opaque_vector_type (float_type_node, 2);
13808 opaque_V2SI_type_node = build_opaque_vector_type (intSI_type_node, 2);
13809 opaque_p_V2SI_type_node = build_pointer_type (opaque_V2SI_type_node);
13810 opaque_V4SI_type_node = build_opaque_vector_type (intSI_type_node, 4);
13812 /* We use V1TI mode as a special container to hold __int128_t items that
13813 must live in VSX registers. */
13814 if (intTI_type_node)
13816 V1TI_type_node = build_vector_type (intTI_type_node, 1);
13817 unsigned_V1TI_type_node = build_vector_type (unsigned_intTI_type_node, 1);
13820 /* The 'vector bool ...' types must be kept distinct from 'vector unsigned ...'
13821 types, especially in C++ land. Similarly, 'vector pixel' is distinct from
13822 'vector unsigned short'. */
13824 bool_char_type_node = build_distinct_type_copy (unsigned_intQI_type_node);
13825 bool_short_type_node = build_distinct_type_copy (unsigned_intHI_type_node);
13826 bool_int_type_node = build_distinct_type_copy (unsigned_intSI_type_node);
13827 bool_long_type_node = build_distinct_type_copy (unsigned_intDI_type_node);
13828 pixel_type_node = build_distinct_type_copy (unsigned_intHI_type_node);
13830 long_integer_type_internal_node = long_integer_type_node;
13831 long_unsigned_type_internal_node = long_unsigned_type_node;
13832 long_long_integer_type_internal_node = long_long_integer_type_node;
13833 long_long_unsigned_type_internal_node = long_long_unsigned_type_node;
13834 intQI_type_internal_node = intQI_type_node;
13835 uintQI_type_internal_node = unsigned_intQI_type_node;
13836 intHI_type_internal_node = intHI_type_node;
13837 uintHI_type_internal_node = unsigned_intHI_type_node;
13838 intSI_type_internal_node = intSI_type_node;
13839 uintSI_type_internal_node = unsigned_intSI_type_node;
13840 intDI_type_internal_node = intDI_type_node;
13841 uintDI_type_internal_node = unsigned_intDI_type_node;
13842 intTI_type_internal_node = intTI_type_node;
13843 uintTI_type_internal_node = unsigned_intTI_type_node;
13844 float_type_internal_node = float_type_node;
13845 double_type_internal_node = double_type_node;
13846 long_double_type_internal_node = long_double_type_node;
13847 dfloat64_type_internal_node = dfloat64_type_node;
13848 dfloat128_type_internal_node = dfloat128_type_node;
13849 void_type_internal_node = void_type_node;
13851 /* Initialize the modes for builtin_function_type, mapping a machine mode to
13852 tree type node. */
13853 builtin_mode_to_type[QImode][0] = integer_type_node;
13854 builtin_mode_to_type[HImode][0] = integer_type_node;
13855 builtin_mode_to_type[SImode][0] = intSI_type_node;
13856 builtin_mode_to_type[SImode][1] = unsigned_intSI_type_node;
13857 builtin_mode_to_type[DImode][0] = intDI_type_node;
13858 builtin_mode_to_type[DImode][1] = unsigned_intDI_type_node;
13859 builtin_mode_to_type[TImode][0] = intTI_type_node;
13860 builtin_mode_to_type[TImode][1] = unsigned_intTI_type_node;
13861 builtin_mode_to_type[SFmode][0] = float_type_node;
13862 builtin_mode_to_type[DFmode][0] = double_type_node;
13863 builtin_mode_to_type[TFmode][0] = long_double_type_node;
13864 builtin_mode_to_type[DDmode][0] = dfloat64_type_node;
13865 builtin_mode_to_type[TDmode][0] = dfloat128_type_node;
13866 builtin_mode_to_type[V1TImode][0] = V1TI_type_node;
13867 builtin_mode_to_type[V1TImode][1] = unsigned_V1TI_type_node;
13868 builtin_mode_to_type[V2SImode][0] = V2SI_type_node;
13869 builtin_mode_to_type[V2SFmode][0] = V2SF_type_node;
13870 builtin_mode_to_type[V2DImode][0] = V2DI_type_node;
13871 builtin_mode_to_type[V2DImode][1] = unsigned_V2DI_type_node;
13872 builtin_mode_to_type[V2DFmode][0] = V2DF_type_node;
13873 builtin_mode_to_type[V4HImode][0] = V4HI_type_node;
13874 builtin_mode_to_type[V4SImode][0] = V4SI_type_node;
13875 builtin_mode_to_type[V4SImode][1] = unsigned_V4SI_type_node;
13876 builtin_mode_to_type[V4SFmode][0] = V4SF_type_node;
13877 builtin_mode_to_type[V8HImode][0] = V8HI_type_node;
13878 builtin_mode_to_type[V8HImode][1] = unsigned_V8HI_type_node;
13879 builtin_mode_to_type[V16QImode][0] = V16QI_type_node;
13880 builtin_mode_to_type[V16QImode][1] = unsigned_V16QI_type_node;
13882 tdecl = add_builtin_type ("__bool char", bool_char_type_node);
13883 TYPE_NAME (bool_char_type_node) = tdecl;
13885 tdecl = add_builtin_type ("__bool short", bool_short_type_node);
13886 TYPE_NAME (bool_short_type_node) = tdecl;
13888 tdecl = add_builtin_type ("__bool int", bool_int_type_node);
13889 TYPE_NAME (bool_int_type_node) = tdecl;
13891 tdecl = add_builtin_type ("__pixel", pixel_type_node);
13892 TYPE_NAME (pixel_type_node) = tdecl;
13894 bool_V16QI_type_node = build_vector_type (bool_char_type_node, 16);
13895 bool_V8HI_type_node = build_vector_type (bool_short_type_node, 8);
13896 bool_V4SI_type_node = build_vector_type (bool_int_type_node, 4);
13897 bool_V2DI_type_node = build_vector_type (bool_long_type_node, 2);
13898 pixel_V8HI_type_node = build_vector_type (pixel_type_node, 8);
13900 tdecl = add_builtin_type ("__vector unsigned char", unsigned_V16QI_type_node);
13901 TYPE_NAME (unsigned_V16QI_type_node) = tdecl;
13903 tdecl = add_builtin_type ("__vector signed char", V16QI_type_node);
13904 TYPE_NAME (V16QI_type_node) = tdecl;
13906 tdecl = add_builtin_type ("__vector __bool char", bool_V16QI_type_node);
13907 TYPE_NAME ( bool_V16QI_type_node) = tdecl;
13909 tdecl = add_builtin_type ("__vector unsigned short", unsigned_V8HI_type_node);
13910 TYPE_NAME (unsigned_V8HI_type_node) = tdecl;
13912 tdecl = add_builtin_type ("__vector signed short", V8HI_type_node);
13913 TYPE_NAME (V8HI_type_node) = tdecl;
13915 tdecl = add_builtin_type ("__vector __bool short", bool_V8HI_type_node);
13916 TYPE_NAME (bool_V8HI_type_node) = tdecl;
13918 tdecl = add_builtin_type ("__vector unsigned int", unsigned_V4SI_type_node);
13919 TYPE_NAME (unsigned_V4SI_type_node) = tdecl;
13921 tdecl = add_builtin_type ("__vector signed int", V4SI_type_node);
13922 TYPE_NAME (V4SI_type_node) = tdecl;
13924 tdecl = add_builtin_type ("__vector __bool int", bool_V4SI_type_node);
13925 TYPE_NAME (bool_V4SI_type_node) = tdecl;
13927 tdecl = add_builtin_type ("__vector float", V4SF_type_node);
13928 TYPE_NAME (V4SF_type_node) = tdecl;
13930 tdecl = add_builtin_type ("__vector __pixel", pixel_V8HI_type_node);
13931 TYPE_NAME (pixel_V8HI_type_node) = tdecl;
13933 tdecl = add_builtin_type ("__vector double", V2DF_type_node);
13934 TYPE_NAME (V2DF_type_node) = tdecl;
13936 if (TARGET_POWERPC64)
13938 tdecl = add_builtin_type ("__vector long", V2DI_type_node);
13939 TYPE_NAME (V2DI_type_node) = tdecl;
13941 tdecl = add_builtin_type ("__vector unsigned long",
13942 unsigned_V2DI_type_node);
13943 TYPE_NAME (unsigned_V2DI_type_node) = tdecl;
13945 tdecl = add_builtin_type ("__vector __bool long", bool_V2DI_type_node);
13946 TYPE_NAME (bool_V2DI_type_node) = tdecl;
13948 else
13950 tdecl = add_builtin_type ("__vector long long", V2DI_type_node);
13951 TYPE_NAME (V2DI_type_node) = tdecl;
13953 tdecl = add_builtin_type ("__vector unsigned long long",
13954 unsigned_V2DI_type_node);
13955 TYPE_NAME (unsigned_V2DI_type_node) = tdecl;
13957 tdecl = add_builtin_type ("__vector __bool long long",
13958 bool_V2DI_type_node);
13959 TYPE_NAME (bool_V2DI_type_node) = tdecl;
13962 if (V1TI_type_node)
13964 tdecl = add_builtin_type ("__vector __int128", V1TI_type_node);
13965 TYPE_NAME (V1TI_type_node) = tdecl;
13967 tdecl = add_builtin_type ("__vector unsigned __int128",
13968 unsigned_V1TI_type_node);
13969 TYPE_NAME (unsigned_V1TI_type_node) = tdecl;
13972 /* Paired and SPE builtins are only available if you build a compiler with
13973 the appropriate options, so only create those builtins with the
13974 appropriate compiler option. Create Altivec and VSX builtins on machines
13975 with at least the general purpose extensions (970 and newer) to allow the
13976 use of the target attribute. */
13977 if (TARGET_PAIRED_FLOAT)
13978 paired_init_builtins ();
13979 if (TARGET_SPE)
13980 spe_init_builtins ();
13981 if (TARGET_EXTRA_BUILTINS)
13982 altivec_init_builtins ();
13983 if (TARGET_HTM)
13984 htm_init_builtins ();
13986 if (TARGET_EXTRA_BUILTINS || TARGET_SPE || TARGET_PAIRED_FLOAT)
13987 rs6000_common_init_builtins ();
13989 ftype = builtin_function_type (DFmode, DFmode, DFmode, VOIDmode,
13990 RS6000_BUILTIN_RECIP, "__builtin_recipdiv");
13991 def_builtin ("__builtin_recipdiv", ftype, RS6000_BUILTIN_RECIP);
13993 ftype = builtin_function_type (SFmode, SFmode, SFmode, VOIDmode,
13994 RS6000_BUILTIN_RECIPF, "__builtin_recipdivf");
13995 def_builtin ("__builtin_recipdivf", ftype, RS6000_BUILTIN_RECIPF);
13997 ftype = builtin_function_type (DFmode, DFmode, VOIDmode, VOIDmode,
13998 RS6000_BUILTIN_RSQRT, "__builtin_rsqrt");
13999 def_builtin ("__builtin_rsqrt", ftype, RS6000_BUILTIN_RSQRT);
14001 ftype = builtin_function_type (SFmode, SFmode, VOIDmode, VOIDmode,
14002 RS6000_BUILTIN_RSQRTF, "__builtin_rsqrtf");
14003 def_builtin ("__builtin_rsqrtf", ftype, RS6000_BUILTIN_RSQRTF);
14005 mode = (TARGET_64BIT) ? DImode : SImode;
14006 ftype = builtin_function_type (mode, mode, mode, VOIDmode,
14007 POWER7_BUILTIN_BPERMD, "__builtin_bpermd");
14008 def_builtin ("__builtin_bpermd", ftype, POWER7_BUILTIN_BPERMD);
14010 ftype = build_function_type_list (unsigned_intDI_type_node,
14011 NULL_TREE);
14012 def_builtin ("__builtin_ppc_get_timebase", ftype, RS6000_BUILTIN_GET_TB);
14014 if (TARGET_64BIT)
14015 ftype = build_function_type_list (unsigned_intDI_type_node,
14016 NULL_TREE);
14017 else
14018 ftype = build_function_type_list (unsigned_intSI_type_node,
14019 NULL_TREE);
14020 def_builtin ("__builtin_ppc_mftb", ftype, RS6000_BUILTIN_MFTB);
14022 ftype = build_function_type_list (double_type_node, NULL_TREE);
14023 def_builtin ("__builtin_mffs", ftype, RS6000_BUILTIN_MFFS);
14025 ftype = build_function_type_list (void_type_node,
14026 intSI_type_node, double_type_node,
14027 NULL_TREE);
14028 def_builtin ("__builtin_mtfsf", ftype, RS6000_BUILTIN_MTFSF);
14030 #if TARGET_XCOFF
14031 /* AIX libm provides clog as __clog. */
14032 if ((tdecl = builtin_decl_explicit (BUILT_IN_CLOG)) != NULL_TREE)
14033 set_user_assembler_name (tdecl, "__clog");
14034 #endif
14036 #ifdef SUBTARGET_INIT_BUILTINS
14037 SUBTARGET_INIT_BUILTINS;
14038 #endif
14041 /* Returns the rs6000 builtin decl for CODE. */
14043 static tree
14044 rs6000_builtin_decl (unsigned code, bool initialize_p ATTRIBUTE_UNUSED)
14046 HOST_WIDE_INT fnmask;
14048 if (code >= RS6000_BUILTIN_COUNT)
14049 return error_mark_node;
14051 fnmask = rs6000_builtin_info[code].mask;
14052 if ((fnmask & rs6000_builtin_mask) != fnmask)
14054 rs6000_invalid_builtin ((enum rs6000_builtins)code);
14055 return error_mark_node;
14058 return rs6000_builtin_decls[code];
14061 static void
14062 spe_init_builtins (void)
14064 tree puint_type_node = build_pointer_type (unsigned_type_node);
14065 tree pushort_type_node = build_pointer_type (short_unsigned_type_node);
14066 const struct builtin_description *d;
14067 size_t i;
14069 tree v2si_ftype_4_v2si
14070 = build_function_type_list (opaque_V2SI_type_node,
14071 opaque_V2SI_type_node,
14072 opaque_V2SI_type_node,
14073 opaque_V2SI_type_node,
14074 opaque_V2SI_type_node,
14075 NULL_TREE);
14077 tree v2sf_ftype_4_v2sf
14078 = build_function_type_list (opaque_V2SF_type_node,
14079 opaque_V2SF_type_node,
14080 opaque_V2SF_type_node,
14081 opaque_V2SF_type_node,
14082 opaque_V2SF_type_node,
14083 NULL_TREE);
14085 tree int_ftype_int_v2si_v2si
14086 = build_function_type_list (integer_type_node,
14087 integer_type_node,
14088 opaque_V2SI_type_node,
14089 opaque_V2SI_type_node,
14090 NULL_TREE);
14092 tree int_ftype_int_v2sf_v2sf
14093 = build_function_type_list (integer_type_node,
14094 integer_type_node,
14095 opaque_V2SF_type_node,
14096 opaque_V2SF_type_node,
14097 NULL_TREE);
14099 tree void_ftype_v2si_puint_int
14100 = build_function_type_list (void_type_node,
14101 opaque_V2SI_type_node,
14102 puint_type_node,
14103 integer_type_node,
14104 NULL_TREE);
14106 tree void_ftype_v2si_puint_char
14107 = build_function_type_list (void_type_node,
14108 opaque_V2SI_type_node,
14109 puint_type_node,
14110 char_type_node,
14111 NULL_TREE);
14113 tree void_ftype_v2si_pv2si_int
14114 = build_function_type_list (void_type_node,
14115 opaque_V2SI_type_node,
14116 opaque_p_V2SI_type_node,
14117 integer_type_node,
14118 NULL_TREE);
14120 tree void_ftype_v2si_pv2si_char
14121 = build_function_type_list (void_type_node,
14122 opaque_V2SI_type_node,
14123 opaque_p_V2SI_type_node,
14124 char_type_node,
14125 NULL_TREE);
14127 tree void_ftype_int
14128 = build_function_type_list (void_type_node, integer_type_node, NULL_TREE);
14130 tree int_ftype_void
14131 = build_function_type_list (integer_type_node, NULL_TREE);
14133 tree v2si_ftype_pv2si_int
14134 = build_function_type_list (opaque_V2SI_type_node,
14135 opaque_p_V2SI_type_node,
14136 integer_type_node,
14137 NULL_TREE);
14139 tree v2si_ftype_puint_int
14140 = build_function_type_list (opaque_V2SI_type_node,
14141 puint_type_node,
14142 integer_type_node,
14143 NULL_TREE);
14145 tree v2si_ftype_pushort_int
14146 = build_function_type_list (opaque_V2SI_type_node,
14147 pushort_type_node,
14148 integer_type_node,
14149 NULL_TREE);
14151 tree v2si_ftype_signed_char
14152 = build_function_type_list (opaque_V2SI_type_node,
14153 signed_char_type_node,
14154 NULL_TREE);
14156 add_builtin_type ("__ev64_opaque__", opaque_V2SI_type_node);
14158 /* Initialize irregular SPE builtins. */
14160 def_builtin ("__builtin_spe_mtspefscr", void_ftype_int, SPE_BUILTIN_MTSPEFSCR);
14161 def_builtin ("__builtin_spe_mfspefscr", int_ftype_void, SPE_BUILTIN_MFSPEFSCR);
14162 def_builtin ("__builtin_spe_evstddx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDDX);
14163 def_builtin ("__builtin_spe_evstdhx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDHX);
14164 def_builtin ("__builtin_spe_evstdwx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDWX);
14165 def_builtin ("__builtin_spe_evstwhex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHEX);
14166 def_builtin ("__builtin_spe_evstwhox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHOX);
14167 def_builtin ("__builtin_spe_evstwwex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWEX);
14168 def_builtin ("__builtin_spe_evstwwox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWOX);
14169 def_builtin ("__builtin_spe_evstdd", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDD);
14170 def_builtin ("__builtin_spe_evstdh", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDH);
14171 def_builtin ("__builtin_spe_evstdw", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDW);
14172 def_builtin ("__builtin_spe_evstwhe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHE);
14173 def_builtin ("__builtin_spe_evstwho", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHO);
14174 def_builtin ("__builtin_spe_evstwwe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWE);
14175 def_builtin ("__builtin_spe_evstwwo", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWO);
14176 def_builtin ("__builtin_spe_evsplatfi", v2si_ftype_signed_char, SPE_BUILTIN_EVSPLATFI);
14177 def_builtin ("__builtin_spe_evsplati", v2si_ftype_signed_char, SPE_BUILTIN_EVSPLATI);
14179 /* Loads. */
14180 def_builtin ("__builtin_spe_evlddx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDDX);
14181 def_builtin ("__builtin_spe_evldwx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDWX);
14182 def_builtin ("__builtin_spe_evldhx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDHX);
14183 def_builtin ("__builtin_spe_evlwhex", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHEX);
14184 def_builtin ("__builtin_spe_evlwhoux", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOUX);
14185 def_builtin ("__builtin_spe_evlwhosx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOSX);
14186 def_builtin ("__builtin_spe_evlwwsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLATX);
14187 def_builtin ("__builtin_spe_evlwhsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLATX);
14188 def_builtin ("__builtin_spe_evlhhesplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLATX);
14189 def_builtin ("__builtin_spe_evlhhousplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLATX);
14190 def_builtin ("__builtin_spe_evlhhossplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLATX);
14191 def_builtin ("__builtin_spe_evldd", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDD);
14192 def_builtin ("__builtin_spe_evldw", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDW);
14193 def_builtin ("__builtin_spe_evldh", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDH);
14194 def_builtin ("__builtin_spe_evlhhesplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLAT);
14195 def_builtin ("__builtin_spe_evlhhossplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLAT);
14196 def_builtin ("__builtin_spe_evlhhousplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLAT);
14197 def_builtin ("__builtin_spe_evlwhe", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHE);
14198 def_builtin ("__builtin_spe_evlwhos", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOS);
14199 def_builtin ("__builtin_spe_evlwhou", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOU);
14200 def_builtin ("__builtin_spe_evlwhsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLAT);
14201 def_builtin ("__builtin_spe_evlwwsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLAT);
14203 /* Predicates. */
14204 d = bdesc_spe_predicates;
14205 for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, d++)
14207 tree type;
14209 switch (insn_data[d->icode].operand[1].mode)
14211 case V2SImode:
14212 type = int_ftype_int_v2si_v2si;
14213 break;
14214 case V2SFmode:
14215 type = int_ftype_int_v2sf_v2sf;
14216 break;
14217 default:
14218 gcc_unreachable ();
14221 def_builtin (d->name, type, d->code);
14224 /* Evsel predicates. */
14225 d = bdesc_spe_evsel;
14226 for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, d++)
14228 tree type;
14230 switch (insn_data[d->icode].operand[1].mode)
14232 case V2SImode:
14233 type = v2si_ftype_4_v2si;
14234 break;
14235 case V2SFmode:
14236 type = v2sf_ftype_4_v2sf;
14237 break;
14238 default:
14239 gcc_unreachable ();
14242 def_builtin (d->name, type, d->code);
14246 static void
14247 paired_init_builtins (void)
14249 const struct builtin_description *d;
14250 size_t i;
14252 tree int_ftype_int_v2sf_v2sf
14253 = build_function_type_list (integer_type_node,
14254 integer_type_node,
14255 V2SF_type_node,
14256 V2SF_type_node,
14257 NULL_TREE);
14258 tree pcfloat_type_node =
14259 build_pointer_type (build_qualified_type
14260 (float_type_node, TYPE_QUAL_CONST));
14262 tree v2sf_ftype_long_pcfloat = build_function_type_list (V2SF_type_node,
14263 long_integer_type_node,
14264 pcfloat_type_node,
14265 NULL_TREE);
14266 tree void_ftype_v2sf_long_pcfloat =
14267 build_function_type_list (void_type_node,
14268 V2SF_type_node,
14269 long_integer_type_node,
14270 pcfloat_type_node,
14271 NULL_TREE);
14274 def_builtin ("__builtin_paired_lx", v2sf_ftype_long_pcfloat,
14275 PAIRED_BUILTIN_LX);
14278 def_builtin ("__builtin_paired_stx", void_ftype_v2sf_long_pcfloat,
14279 PAIRED_BUILTIN_STX);
14281 /* Predicates. */
14282 d = bdesc_paired_preds;
14283 for (i = 0; i < ARRAY_SIZE (bdesc_paired_preds); ++i, d++)
14285 tree type;
14287 if (TARGET_DEBUG_BUILTIN)
14288 fprintf (stderr, "paired pred #%d, insn = %s [%d], mode = %s\n",
14289 (int)i, get_insn_name (d->icode), (int)d->icode,
14290 GET_MODE_NAME (insn_data[d->icode].operand[1].mode));
14292 switch (insn_data[d->icode].operand[1].mode)
14294 case V2SFmode:
14295 type = int_ftype_int_v2sf_v2sf;
14296 break;
14297 default:
14298 gcc_unreachable ();
14301 def_builtin (d->name, type, d->code);
14305 static void
14306 altivec_init_builtins (void)
14308 const struct builtin_description *d;
14309 size_t i;
14310 tree ftype;
14311 tree decl;
14313 tree pvoid_type_node = build_pointer_type (void_type_node);
14315 tree pcvoid_type_node
14316 = build_pointer_type (build_qualified_type (void_type_node,
14317 TYPE_QUAL_CONST));
14319 tree int_ftype_opaque
14320 = build_function_type_list (integer_type_node,
14321 opaque_V4SI_type_node, NULL_TREE);
14322 tree opaque_ftype_opaque
14323 = build_function_type_list (integer_type_node, NULL_TREE);
14324 tree opaque_ftype_opaque_int
14325 = build_function_type_list (opaque_V4SI_type_node,
14326 opaque_V4SI_type_node, integer_type_node, NULL_TREE);
14327 tree opaque_ftype_opaque_opaque_int
14328 = build_function_type_list (opaque_V4SI_type_node,
14329 opaque_V4SI_type_node, opaque_V4SI_type_node,
14330 integer_type_node, NULL_TREE);
14331 tree int_ftype_int_opaque_opaque
14332 = build_function_type_list (integer_type_node,
14333 integer_type_node, opaque_V4SI_type_node,
14334 opaque_V4SI_type_node, NULL_TREE);
14335 tree int_ftype_int_v4si_v4si
14336 = build_function_type_list (integer_type_node,
14337 integer_type_node, V4SI_type_node,
14338 V4SI_type_node, NULL_TREE);
14339 tree int_ftype_int_v2di_v2di
14340 = build_function_type_list (integer_type_node,
14341 integer_type_node, V2DI_type_node,
14342 V2DI_type_node, NULL_TREE);
14343 tree void_ftype_v4si
14344 = build_function_type_list (void_type_node, V4SI_type_node, NULL_TREE);
14345 tree v8hi_ftype_void
14346 = build_function_type_list (V8HI_type_node, NULL_TREE);
14347 tree void_ftype_void
14348 = build_function_type_list (void_type_node, NULL_TREE);
14349 tree void_ftype_int
14350 = build_function_type_list (void_type_node, integer_type_node, NULL_TREE);
14352 tree opaque_ftype_long_pcvoid
14353 = build_function_type_list (opaque_V4SI_type_node,
14354 long_integer_type_node, pcvoid_type_node,
14355 NULL_TREE);
14356 tree v16qi_ftype_long_pcvoid
14357 = build_function_type_list (V16QI_type_node,
14358 long_integer_type_node, pcvoid_type_node,
14359 NULL_TREE);
14360 tree v8hi_ftype_long_pcvoid
14361 = build_function_type_list (V8HI_type_node,
14362 long_integer_type_node, pcvoid_type_node,
14363 NULL_TREE);
14364 tree v4si_ftype_long_pcvoid
14365 = build_function_type_list (V4SI_type_node,
14366 long_integer_type_node, pcvoid_type_node,
14367 NULL_TREE);
14368 tree v4sf_ftype_long_pcvoid
14369 = build_function_type_list (V4SF_type_node,
14370 long_integer_type_node, pcvoid_type_node,
14371 NULL_TREE);
14372 tree v2df_ftype_long_pcvoid
14373 = build_function_type_list (V2DF_type_node,
14374 long_integer_type_node, pcvoid_type_node,
14375 NULL_TREE);
14376 tree v2di_ftype_long_pcvoid
14377 = build_function_type_list (V2DI_type_node,
14378 long_integer_type_node, pcvoid_type_node,
14379 NULL_TREE);
14381 tree void_ftype_opaque_long_pvoid
14382 = build_function_type_list (void_type_node,
14383 opaque_V4SI_type_node, long_integer_type_node,
14384 pvoid_type_node, NULL_TREE);
14385 tree void_ftype_v4si_long_pvoid
14386 = build_function_type_list (void_type_node,
14387 V4SI_type_node, long_integer_type_node,
14388 pvoid_type_node, NULL_TREE);
14389 tree void_ftype_v16qi_long_pvoid
14390 = build_function_type_list (void_type_node,
14391 V16QI_type_node, long_integer_type_node,
14392 pvoid_type_node, NULL_TREE);
14393 tree void_ftype_v8hi_long_pvoid
14394 = build_function_type_list (void_type_node,
14395 V8HI_type_node, long_integer_type_node,
14396 pvoid_type_node, NULL_TREE);
14397 tree void_ftype_v4sf_long_pvoid
14398 = build_function_type_list (void_type_node,
14399 V4SF_type_node, long_integer_type_node,
14400 pvoid_type_node, NULL_TREE);
14401 tree void_ftype_v2df_long_pvoid
14402 = build_function_type_list (void_type_node,
14403 V2DF_type_node, long_integer_type_node,
14404 pvoid_type_node, NULL_TREE);
14405 tree void_ftype_v2di_long_pvoid
14406 = build_function_type_list (void_type_node,
14407 V2DI_type_node, long_integer_type_node,
14408 pvoid_type_node, NULL_TREE);
14409 tree int_ftype_int_v8hi_v8hi
14410 = build_function_type_list (integer_type_node,
14411 integer_type_node, V8HI_type_node,
14412 V8HI_type_node, NULL_TREE);
14413 tree int_ftype_int_v16qi_v16qi
14414 = build_function_type_list (integer_type_node,
14415 integer_type_node, V16QI_type_node,
14416 V16QI_type_node, NULL_TREE);
14417 tree int_ftype_int_v4sf_v4sf
14418 = build_function_type_list (integer_type_node,
14419 integer_type_node, V4SF_type_node,
14420 V4SF_type_node, NULL_TREE);
14421 tree int_ftype_int_v2df_v2df
14422 = build_function_type_list (integer_type_node,
14423 integer_type_node, V2DF_type_node,
14424 V2DF_type_node, NULL_TREE);
14425 tree v2di_ftype_v2di
14426 = build_function_type_list (V2DI_type_node, V2DI_type_node, NULL_TREE);
14427 tree v4si_ftype_v4si
14428 = build_function_type_list (V4SI_type_node, V4SI_type_node, NULL_TREE);
14429 tree v8hi_ftype_v8hi
14430 = build_function_type_list (V8HI_type_node, V8HI_type_node, NULL_TREE);
14431 tree v16qi_ftype_v16qi
14432 = build_function_type_list (V16QI_type_node, V16QI_type_node, NULL_TREE);
14433 tree v4sf_ftype_v4sf
14434 = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
14435 tree v2df_ftype_v2df
14436 = build_function_type_list (V2DF_type_node, V2DF_type_node, NULL_TREE);
14437 tree void_ftype_pcvoid_int_int
14438 = build_function_type_list (void_type_node,
14439 pcvoid_type_node, integer_type_node,
14440 integer_type_node, NULL_TREE);
14442 def_builtin ("__builtin_altivec_mtvscr", void_ftype_v4si, ALTIVEC_BUILTIN_MTVSCR);
14443 def_builtin ("__builtin_altivec_mfvscr", v8hi_ftype_void, ALTIVEC_BUILTIN_MFVSCR);
14444 def_builtin ("__builtin_altivec_dssall", void_ftype_void, ALTIVEC_BUILTIN_DSSALL);
14445 def_builtin ("__builtin_altivec_dss", void_ftype_int, ALTIVEC_BUILTIN_DSS);
14446 def_builtin ("__builtin_altivec_lvsl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSL);
14447 def_builtin ("__builtin_altivec_lvsr", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSR);
14448 def_builtin ("__builtin_altivec_lvebx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEBX);
14449 def_builtin ("__builtin_altivec_lvehx", v8hi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEHX);
14450 def_builtin ("__builtin_altivec_lvewx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEWX);
14451 def_builtin ("__builtin_altivec_lvxl", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVXL);
14452 def_builtin ("__builtin_altivec_lvxl_v2df", v2df_ftype_long_pcvoid,
14453 ALTIVEC_BUILTIN_LVXL_V2DF);
14454 def_builtin ("__builtin_altivec_lvxl_v2di", v2di_ftype_long_pcvoid,
14455 ALTIVEC_BUILTIN_LVXL_V2DI);
14456 def_builtin ("__builtin_altivec_lvxl_v4sf", v4sf_ftype_long_pcvoid,
14457 ALTIVEC_BUILTIN_LVXL_V4SF);
14458 def_builtin ("__builtin_altivec_lvxl_v4si", v4si_ftype_long_pcvoid,
14459 ALTIVEC_BUILTIN_LVXL_V4SI);
14460 def_builtin ("__builtin_altivec_lvxl_v8hi", v8hi_ftype_long_pcvoid,
14461 ALTIVEC_BUILTIN_LVXL_V8HI);
14462 def_builtin ("__builtin_altivec_lvxl_v16qi", v16qi_ftype_long_pcvoid,
14463 ALTIVEC_BUILTIN_LVXL_V16QI);
14464 def_builtin ("__builtin_altivec_lvx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVX);
14465 def_builtin ("__builtin_altivec_lvx_v2df", v2df_ftype_long_pcvoid,
14466 ALTIVEC_BUILTIN_LVX_V2DF);
14467 def_builtin ("__builtin_altivec_lvx_v2di", v2di_ftype_long_pcvoid,
14468 ALTIVEC_BUILTIN_LVX_V2DI);
14469 def_builtin ("__builtin_altivec_lvx_v4sf", v4sf_ftype_long_pcvoid,
14470 ALTIVEC_BUILTIN_LVX_V4SF);
14471 def_builtin ("__builtin_altivec_lvx_v4si", v4si_ftype_long_pcvoid,
14472 ALTIVEC_BUILTIN_LVX_V4SI);
14473 def_builtin ("__builtin_altivec_lvx_v8hi", v8hi_ftype_long_pcvoid,
14474 ALTIVEC_BUILTIN_LVX_V8HI);
14475 def_builtin ("__builtin_altivec_lvx_v16qi", v16qi_ftype_long_pcvoid,
14476 ALTIVEC_BUILTIN_LVX_V16QI);
14477 def_builtin ("__builtin_altivec_stvx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVX);
14478 def_builtin ("__builtin_altivec_stvx_v2df", void_ftype_v2df_long_pvoid,
14479 ALTIVEC_BUILTIN_STVX_V2DF);
14480 def_builtin ("__builtin_altivec_stvx_v2di", void_ftype_v2di_long_pvoid,
14481 ALTIVEC_BUILTIN_STVX_V2DI);
14482 def_builtin ("__builtin_altivec_stvx_v4sf", void_ftype_v4sf_long_pvoid,
14483 ALTIVEC_BUILTIN_STVX_V4SF);
14484 def_builtin ("__builtin_altivec_stvx_v4si", void_ftype_v4si_long_pvoid,
14485 ALTIVEC_BUILTIN_STVX_V4SI);
14486 def_builtin ("__builtin_altivec_stvx_v8hi", void_ftype_v8hi_long_pvoid,
14487 ALTIVEC_BUILTIN_STVX_V8HI);
14488 def_builtin ("__builtin_altivec_stvx_v16qi", void_ftype_v16qi_long_pvoid,
14489 ALTIVEC_BUILTIN_STVX_V16QI);
14490 def_builtin ("__builtin_altivec_stvewx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVEWX);
14491 def_builtin ("__builtin_altivec_stvxl", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVXL);
14492 def_builtin ("__builtin_altivec_stvxl_v2df", void_ftype_v2df_long_pvoid,
14493 ALTIVEC_BUILTIN_STVXL_V2DF);
14494 def_builtin ("__builtin_altivec_stvxl_v2di", void_ftype_v2di_long_pvoid,
14495 ALTIVEC_BUILTIN_STVXL_V2DI);
14496 def_builtin ("__builtin_altivec_stvxl_v4sf", void_ftype_v4sf_long_pvoid,
14497 ALTIVEC_BUILTIN_STVXL_V4SF);
14498 def_builtin ("__builtin_altivec_stvxl_v4si", void_ftype_v4si_long_pvoid,
14499 ALTIVEC_BUILTIN_STVXL_V4SI);
14500 def_builtin ("__builtin_altivec_stvxl_v8hi", void_ftype_v8hi_long_pvoid,
14501 ALTIVEC_BUILTIN_STVXL_V8HI);
14502 def_builtin ("__builtin_altivec_stvxl_v16qi", void_ftype_v16qi_long_pvoid,
14503 ALTIVEC_BUILTIN_STVXL_V16QI);
14504 def_builtin ("__builtin_altivec_stvebx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVEBX);
14505 def_builtin ("__builtin_altivec_stvehx", void_ftype_v8hi_long_pvoid, ALTIVEC_BUILTIN_STVEHX);
14506 def_builtin ("__builtin_vec_ld", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LD);
14507 def_builtin ("__builtin_vec_lde", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LDE);
14508 def_builtin ("__builtin_vec_ldl", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LDL);
14509 def_builtin ("__builtin_vec_lvsl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVSL);
14510 def_builtin ("__builtin_vec_lvsr", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVSR);
14511 def_builtin ("__builtin_vec_lvebx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEBX);
14512 def_builtin ("__builtin_vec_lvehx", v8hi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEHX);
14513 def_builtin ("__builtin_vec_lvewx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEWX);
14514 def_builtin ("__builtin_vec_st", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_ST);
14515 def_builtin ("__builtin_vec_ste", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STE);
14516 def_builtin ("__builtin_vec_stl", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STL);
14517 def_builtin ("__builtin_vec_stvewx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEWX);
14518 def_builtin ("__builtin_vec_stvebx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEBX);
14519 def_builtin ("__builtin_vec_stvehx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEHX);
14521 def_builtin ("__builtin_vsx_lxvd2x_v2df", v2df_ftype_long_pcvoid,
14522 VSX_BUILTIN_LXVD2X_V2DF);
14523 def_builtin ("__builtin_vsx_lxvd2x_v2di", v2di_ftype_long_pcvoid,
14524 VSX_BUILTIN_LXVD2X_V2DI);
14525 def_builtin ("__builtin_vsx_lxvw4x_v4sf", v4sf_ftype_long_pcvoid,
14526 VSX_BUILTIN_LXVW4X_V4SF);
14527 def_builtin ("__builtin_vsx_lxvw4x_v4si", v4si_ftype_long_pcvoid,
14528 VSX_BUILTIN_LXVW4X_V4SI);
14529 def_builtin ("__builtin_vsx_lxvw4x_v8hi", v8hi_ftype_long_pcvoid,
14530 VSX_BUILTIN_LXVW4X_V8HI);
14531 def_builtin ("__builtin_vsx_lxvw4x_v16qi", v16qi_ftype_long_pcvoid,
14532 VSX_BUILTIN_LXVW4X_V16QI);
14533 def_builtin ("__builtin_vsx_stxvd2x_v2df", void_ftype_v2df_long_pvoid,
14534 VSX_BUILTIN_STXVD2X_V2DF);
14535 def_builtin ("__builtin_vsx_stxvd2x_v2di", void_ftype_v2di_long_pvoid,
14536 VSX_BUILTIN_STXVD2X_V2DI);
14537 def_builtin ("__builtin_vsx_stxvw4x_v4sf", void_ftype_v4sf_long_pvoid,
14538 VSX_BUILTIN_STXVW4X_V4SF);
14539 def_builtin ("__builtin_vsx_stxvw4x_v4si", void_ftype_v4si_long_pvoid,
14540 VSX_BUILTIN_STXVW4X_V4SI);
14541 def_builtin ("__builtin_vsx_stxvw4x_v8hi", void_ftype_v8hi_long_pvoid,
14542 VSX_BUILTIN_STXVW4X_V8HI);
14543 def_builtin ("__builtin_vsx_stxvw4x_v16qi", void_ftype_v16qi_long_pvoid,
14544 VSX_BUILTIN_STXVW4X_V16QI);
14545 def_builtin ("__builtin_vec_vsx_ld", opaque_ftype_long_pcvoid,
14546 VSX_BUILTIN_VEC_LD);
14547 def_builtin ("__builtin_vec_vsx_st", void_ftype_opaque_long_pvoid,
14548 VSX_BUILTIN_VEC_ST);
14550 def_builtin ("__builtin_vec_step", int_ftype_opaque, ALTIVEC_BUILTIN_VEC_STEP);
14551 def_builtin ("__builtin_vec_splats", opaque_ftype_opaque, ALTIVEC_BUILTIN_VEC_SPLATS);
14552 def_builtin ("__builtin_vec_promote", opaque_ftype_opaque, ALTIVEC_BUILTIN_VEC_PROMOTE);
14554 def_builtin ("__builtin_vec_sld", opaque_ftype_opaque_opaque_int, ALTIVEC_BUILTIN_VEC_SLD);
14555 def_builtin ("__builtin_vec_splat", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_SPLAT);
14556 def_builtin ("__builtin_vec_extract", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_EXTRACT);
14557 def_builtin ("__builtin_vec_insert", opaque_ftype_opaque_opaque_int, ALTIVEC_BUILTIN_VEC_INSERT);
14558 def_builtin ("__builtin_vec_vspltw", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTW);
14559 def_builtin ("__builtin_vec_vsplth", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTH);
14560 def_builtin ("__builtin_vec_vspltb", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTB);
14561 def_builtin ("__builtin_vec_ctf", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTF);
14562 def_builtin ("__builtin_vec_vcfsx", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VCFSX);
14563 def_builtin ("__builtin_vec_vcfux", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VCFUX);
14564 def_builtin ("__builtin_vec_cts", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTS);
14565 def_builtin ("__builtin_vec_ctu", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTU);
14567 /* Cell builtins. */
14568 def_builtin ("__builtin_altivec_lvlx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVLX);
14569 def_builtin ("__builtin_altivec_lvlxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVLXL);
14570 def_builtin ("__builtin_altivec_lvrx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVRX);
14571 def_builtin ("__builtin_altivec_lvrxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVRXL);
14573 def_builtin ("__builtin_vec_lvlx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVLX);
14574 def_builtin ("__builtin_vec_lvlxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVLXL);
14575 def_builtin ("__builtin_vec_lvrx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVRX);
14576 def_builtin ("__builtin_vec_lvrxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVRXL);
14578 def_builtin ("__builtin_altivec_stvlx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVLX);
14579 def_builtin ("__builtin_altivec_stvlxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVLXL);
14580 def_builtin ("__builtin_altivec_stvrx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVRX);
14581 def_builtin ("__builtin_altivec_stvrxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVRXL);
14583 def_builtin ("__builtin_vec_stvlx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVLX);
14584 def_builtin ("__builtin_vec_stvlxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVLXL);
14585 def_builtin ("__builtin_vec_stvrx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVRX);
14586 def_builtin ("__builtin_vec_stvrxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVRXL);
14588 /* Add the DST variants. */
14589 d = bdesc_dst;
14590 for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
14591 def_builtin (d->name, void_ftype_pcvoid_int_int, d->code);
14593 /* Initialize the predicates. */
14594 d = bdesc_altivec_preds;
14595 for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, d++)
14597 enum machine_mode mode1;
14598 tree type;
14600 if (rs6000_overloaded_builtin_p (d->code))
14601 mode1 = VOIDmode;
14602 else
14603 mode1 = insn_data[d->icode].operand[1].mode;
14605 switch (mode1)
14607 case VOIDmode:
14608 type = int_ftype_int_opaque_opaque;
14609 break;
14610 case V2DImode:
14611 type = int_ftype_int_v2di_v2di;
14612 break;
14613 case V4SImode:
14614 type = int_ftype_int_v4si_v4si;
14615 break;
14616 case V8HImode:
14617 type = int_ftype_int_v8hi_v8hi;
14618 break;
14619 case V16QImode:
14620 type = int_ftype_int_v16qi_v16qi;
14621 break;
14622 case V4SFmode:
14623 type = int_ftype_int_v4sf_v4sf;
14624 break;
14625 case V2DFmode:
14626 type = int_ftype_int_v2df_v2df;
14627 break;
14628 default:
14629 gcc_unreachable ();
14632 def_builtin (d->name, type, d->code);
14635 /* Initialize the abs* operators. */
14636 d = bdesc_abs;
14637 for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
14639 enum machine_mode mode0;
14640 tree type;
14642 mode0 = insn_data[d->icode].operand[0].mode;
14644 switch (mode0)
14646 case V2DImode:
14647 type = v2di_ftype_v2di;
14648 break;
14649 case V4SImode:
14650 type = v4si_ftype_v4si;
14651 break;
14652 case V8HImode:
14653 type = v8hi_ftype_v8hi;
14654 break;
14655 case V16QImode:
14656 type = v16qi_ftype_v16qi;
14657 break;
14658 case V4SFmode:
14659 type = v4sf_ftype_v4sf;
14660 break;
14661 case V2DFmode:
14662 type = v2df_ftype_v2df;
14663 break;
14664 default:
14665 gcc_unreachable ();
14668 def_builtin (d->name, type, d->code);
14671 /* Initialize target builtin that implements
14672 targetm.vectorize.builtin_mask_for_load. */
14674 decl = add_builtin_function ("__builtin_altivec_mask_for_load",
14675 v16qi_ftype_long_pcvoid,
14676 ALTIVEC_BUILTIN_MASK_FOR_LOAD,
14677 BUILT_IN_MD, NULL, NULL_TREE);
14678 TREE_READONLY (decl) = 1;
14679 /* Record the decl. Will be used by rs6000_builtin_mask_for_load. */
14680 altivec_builtin_mask_for_load = decl;
14682 /* Access to the vec_init patterns. */
14683 ftype = build_function_type_list (V4SI_type_node, integer_type_node,
14684 integer_type_node, integer_type_node,
14685 integer_type_node, NULL_TREE);
14686 def_builtin ("__builtin_vec_init_v4si", ftype, ALTIVEC_BUILTIN_VEC_INIT_V4SI);
14688 ftype = build_function_type_list (V8HI_type_node, short_integer_type_node,
14689 short_integer_type_node,
14690 short_integer_type_node,
14691 short_integer_type_node,
14692 short_integer_type_node,
14693 short_integer_type_node,
14694 short_integer_type_node,
14695 short_integer_type_node, NULL_TREE);
14696 def_builtin ("__builtin_vec_init_v8hi", ftype, ALTIVEC_BUILTIN_VEC_INIT_V8HI);
14698 ftype = build_function_type_list (V16QI_type_node, char_type_node,
14699 char_type_node, char_type_node,
14700 char_type_node, char_type_node,
14701 char_type_node, char_type_node,
14702 char_type_node, char_type_node,
14703 char_type_node, char_type_node,
14704 char_type_node, char_type_node,
14705 char_type_node, char_type_node,
14706 char_type_node, NULL_TREE);
14707 def_builtin ("__builtin_vec_init_v16qi", ftype,
14708 ALTIVEC_BUILTIN_VEC_INIT_V16QI);
14710 ftype = build_function_type_list (V4SF_type_node, float_type_node,
14711 float_type_node, float_type_node,
14712 float_type_node, NULL_TREE);
14713 def_builtin ("__builtin_vec_init_v4sf", ftype, ALTIVEC_BUILTIN_VEC_INIT_V4SF);
14715 /* VSX builtins. */
14716 ftype = build_function_type_list (V2DF_type_node, double_type_node,
14717 double_type_node, NULL_TREE);
14718 def_builtin ("__builtin_vec_init_v2df", ftype, VSX_BUILTIN_VEC_INIT_V2DF);
14720 ftype = build_function_type_list (V2DI_type_node, intDI_type_node,
14721 intDI_type_node, NULL_TREE);
14722 def_builtin ("__builtin_vec_init_v2di", ftype, VSX_BUILTIN_VEC_INIT_V2DI);
14724 /* Access to the vec_set patterns. */
14725 ftype = build_function_type_list (V4SI_type_node, V4SI_type_node,
14726 intSI_type_node,
14727 integer_type_node, NULL_TREE);
14728 def_builtin ("__builtin_vec_set_v4si", ftype, ALTIVEC_BUILTIN_VEC_SET_V4SI);
14730 ftype = build_function_type_list (V8HI_type_node, V8HI_type_node,
14731 intHI_type_node,
14732 integer_type_node, NULL_TREE);
14733 def_builtin ("__builtin_vec_set_v8hi", ftype, ALTIVEC_BUILTIN_VEC_SET_V8HI);
14735 ftype = build_function_type_list (V16QI_type_node, V16QI_type_node,
14736 intQI_type_node,
14737 integer_type_node, NULL_TREE);
14738 def_builtin ("__builtin_vec_set_v16qi", ftype, ALTIVEC_BUILTIN_VEC_SET_V16QI);
14740 ftype = build_function_type_list (V4SF_type_node, V4SF_type_node,
14741 float_type_node,
14742 integer_type_node, NULL_TREE);
14743 def_builtin ("__builtin_vec_set_v4sf", ftype, ALTIVEC_BUILTIN_VEC_SET_V4SF);
14745 ftype = build_function_type_list (V2DF_type_node, V2DF_type_node,
14746 double_type_node,
14747 integer_type_node, NULL_TREE);
14748 def_builtin ("__builtin_vec_set_v2df", ftype, VSX_BUILTIN_VEC_SET_V2DF);
14750 ftype = build_function_type_list (V2DI_type_node, V2DI_type_node,
14751 intDI_type_node,
14752 integer_type_node, NULL_TREE);
14753 def_builtin ("__builtin_vec_set_v2di", ftype, VSX_BUILTIN_VEC_SET_V2DI);
14755 /* Access to the vec_extract patterns. */
14756 ftype = build_function_type_list (intSI_type_node, V4SI_type_node,
14757 integer_type_node, NULL_TREE);
14758 def_builtin ("__builtin_vec_ext_v4si", ftype, ALTIVEC_BUILTIN_VEC_EXT_V4SI);
14760 ftype = build_function_type_list (intHI_type_node, V8HI_type_node,
14761 integer_type_node, NULL_TREE);
14762 def_builtin ("__builtin_vec_ext_v8hi", ftype, ALTIVEC_BUILTIN_VEC_EXT_V8HI);
14764 ftype = build_function_type_list (intQI_type_node, V16QI_type_node,
14765 integer_type_node, NULL_TREE);
14766 def_builtin ("__builtin_vec_ext_v16qi", ftype, ALTIVEC_BUILTIN_VEC_EXT_V16QI);
14768 ftype = build_function_type_list (float_type_node, V4SF_type_node,
14769 integer_type_node, NULL_TREE);
14770 def_builtin ("__builtin_vec_ext_v4sf", ftype, ALTIVEC_BUILTIN_VEC_EXT_V4SF);
14772 ftype = build_function_type_list (double_type_node, V2DF_type_node,
14773 integer_type_node, NULL_TREE);
14774 def_builtin ("__builtin_vec_ext_v2df", ftype, VSX_BUILTIN_VEC_EXT_V2DF);
14776 ftype = build_function_type_list (intDI_type_node, V2DI_type_node,
14777 integer_type_node, NULL_TREE);
14778 def_builtin ("__builtin_vec_ext_v2di", ftype, VSX_BUILTIN_VEC_EXT_V2DI);
14781 if (V1TI_type_node)
14783 tree v1ti_ftype_long_pcvoid
14784 = build_function_type_list (V1TI_type_node,
14785 long_integer_type_node, pcvoid_type_node,
14786 NULL_TREE);
14787 tree void_ftype_v1ti_long_pvoid
14788 = build_function_type_list (void_type_node,
14789 V1TI_type_node, long_integer_type_node,
14790 pvoid_type_node, NULL_TREE);
14791 def_builtin ("__builtin_vsx_lxvd2x_v1ti", v1ti_ftype_long_pcvoid,
14792 VSX_BUILTIN_LXVD2X_V1TI);
14793 def_builtin ("__builtin_vsx_stxvd2x_v1ti", void_ftype_v1ti_long_pvoid,
14794 VSX_BUILTIN_STXVD2X_V1TI);
14795 ftype = build_function_type_list (V1TI_type_node, intTI_type_node,
14796 NULL_TREE, NULL_TREE);
14797 def_builtin ("__builtin_vec_init_v1ti", ftype, VSX_BUILTIN_VEC_INIT_V1TI);
14798 ftype = build_function_type_list (V1TI_type_node, V1TI_type_node,
14799 intTI_type_node,
14800 integer_type_node, NULL_TREE);
14801 def_builtin ("__builtin_vec_set_v1ti", ftype, VSX_BUILTIN_VEC_SET_V1TI);
14802 ftype = build_function_type_list (intTI_type_node, V1TI_type_node,
14803 integer_type_node, NULL_TREE);
14804 def_builtin ("__builtin_vec_ext_v1ti", ftype, VSX_BUILTIN_VEC_EXT_V1TI);
14809 static void
14810 htm_init_builtins (void)
14812 HOST_WIDE_INT builtin_mask = rs6000_builtin_mask;
14813 const struct builtin_description *d;
14814 size_t i;
14816 d = bdesc_htm;
14817 for (i = 0; i < ARRAY_SIZE (bdesc_htm); i++, d++)
14819 tree op[MAX_HTM_OPERANDS], type;
14820 HOST_WIDE_INT mask = d->mask;
14821 unsigned attr = rs6000_builtin_info[d->code].attr;
14822 bool void_func = (attr & RS6000_BTC_VOID);
14823 int attr_args = (attr & RS6000_BTC_TYPE_MASK);
14824 int nopnds = 0;
14825 tree argtype = (attr & RS6000_BTC_SPR) ? long_unsigned_type_node
14826 : unsigned_type_node;
14828 if ((mask & builtin_mask) != mask)
14830 if (TARGET_DEBUG_BUILTIN)
14831 fprintf (stderr, "htm_builtin, skip binary %s\n", d->name);
14832 continue;
14835 if (d->name == 0)
14837 if (TARGET_DEBUG_BUILTIN)
14838 fprintf (stderr, "htm_builtin, bdesc_htm[%ld] no name\n",
14839 (long unsigned) i);
14840 continue;
14843 op[nopnds++] = (void_func) ? void_type_node : argtype;
14845 if (attr_args == RS6000_BTC_UNARY)
14846 op[nopnds++] = argtype;
14847 else if (attr_args == RS6000_BTC_BINARY)
14849 op[nopnds++] = argtype;
14850 op[nopnds++] = argtype;
14852 else if (attr_args == RS6000_BTC_TERNARY)
14854 op[nopnds++] = argtype;
14855 op[nopnds++] = argtype;
14856 op[nopnds++] = argtype;
14859 switch (nopnds)
14861 case 1:
14862 type = build_function_type_list (op[0], NULL_TREE);
14863 break;
14864 case 2:
14865 type = build_function_type_list (op[0], op[1], NULL_TREE);
14866 break;
14867 case 3:
14868 type = build_function_type_list (op[0], op[1], op[2], NULL_TREE);
14869 break;
14870 case 4:
14871 type = build_function_type_list (op[0], op[1], op[2], op[3],
14872 NULL_TREE);
14873 break;
14874 default:
14875 gcc_unreachable ();
14878 def_builtin (d->name, type, d->code);
14882 /* Hash function for builtin functions with up to 3 arguments and a return
14883 type. */
14884 static unsigned
14885 builtin_hash_function (const void *hash_entry)
14887 unsigned ret = 0;
14888 int i;
14889 const struct builtin_hash_struct *bh =
14890 (const struct builtin_hash_struct *) hash_entry;
14892 for (i = 0; i < 4; i++)
14894 ret = (ret * (unsigned)MAX_MACHINE_MODE) + ((unsigned)bh->mode[i]);
14895 ret = (ret * 2) + bh->uns_p[i];
14898 return ret;
14901 /* Compare builtin hash entries H1 and H2 for equivalence. */
14902 static int
14903 builtin_hash_eq (const void *h1, const void *h2)
14905 const struct builtin_hash_struct *p1 = (const struct builtin_hash_struct *) h1;
14906 const struct builtin_hash_struct *p2 = (const struct builtin_hash_struct *) h2;
14908 return ((p1->mode[0] == p2->mode[0])
14909 && (p1->mode[1] == p2->mode[1])
14910 && (p1->mode[2] == p2->mode[2])
14911 && (p1->mode[3] == p2->mode[3])
14912 && (p1->uns_p[0] == p2->uns_p[0])
14913 && (p1->uns_p[1] == p2->uns_p[1])
14914 && (p1->uns_p[2] == p2->uns_p[2])
14915 && (p1->uns_p[3] == p2->uns_p[3]));
14918 /* Map types for builtin functions with an explicit return type and up to 3
14919 arguments. Functions with fewer than 3 arguments use VOIDmode as the type
14920 of the argument. */
14921 static tree
14922 builtin_function_type (enum machine_mode mode_ret, enum machine_mode mode_arg0,
14923 enum machine_mode mode_arg1, enum machine_mode mode_arg2,
14924 enum rs6000_builtins builtin, const char *name)
14926 struct builtin_hash_struct h;
14927 struct builtin_hash_struct *h2;
14928 void **found;
14929 int num_args = 3;
14930 int i;
14931 tree ret_type = NULL_TREE;
14932 tree arg_type[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
14934 /* Create builtin_hash_table. */
14935 if (builtin_hash_table == NULL)
14936 builtin_hash_table = htab_create_ggc (1500, builtin_hash_function,
14937 builtin_hash_eq, NULL);
14939 h.type = NULL_TREE;
14940 h.mode[0] = mode_ret;
14941 h.mode[1] = mode_arg0;
14942 h.mode[2] = mode_arg1;
14943 h.mode[3] = mode_arg2;
14944 h.uns_p[0] = 0;
14945 h.uns_p[1] = 0;
14946 h.uns_p[2] = 0;
14947 h.uns_p[3] = 0;
14949 /* If the builtin is a type that produces unsigned results or takes unsigned
14950 arguments, and it is returned as a decl for the vectorizer (such as
14951 widening multiplies, permute), make sure the arguments and return value
14952 are type correct. */
14953 switch (builtin)
14955 /* unsigned 1 argument functions. */
14956 case CRYPTO_BUILTIN_VSBOX:
14957 case P8V_BUILTIN_VGBBD:
14958 case MISC_BUILTIN_CDTBCD:
14959 case MISC_BUILTIN_CBCDTD:
14960 h.uns_p[0] = 1;
14961 h.uns_p[1] = 1;
14962 break;
14964 /* unsigned 2 argument functions. */
14965 case ALTIVEC_BUILTIN_VMULEUB_UNS:
14966 case ALTIVEC_BUILTIN_VMULEUH_UNS:
14967 case ALTIVEC_BUILTIN_VMULOUB_UNS:
14968 case ALTIVEC_BUILTIN_VMULOUH_UNS:
14969 case CRYPTO_BUILTIN_VCIPHER:
14970 case CRYPTO_BUILTIN_VCIPHERLAST:
14971 case CRYPTO_BUILTIN_VNCIPHER:
14972 case CRYPTO_BUILTIN_VNCIPHERLAST:
14973 case CRYPTO_BUILTIN_VPMSUMB:
14974 case CRYPTO_BUILTIN_VPMSUMH:
14975 case CRYPTO_BUILTIN_VPMSUMW:
14976 case CRYPTO_BUILTIN_VPMSUMD:
14977 case CRYPTO_BUILTIN_VPMSUM:
14978 case MISC_BUILTIN_ADDG6S:
14979 case MISC_BUILTIN_DIVWEU:
14980 case MISC_BUILTIN_DIVWEUO:
14981 case MISC_BUILTIN_DIVDEU:
14982 case MISC_BUILTIN_DIVDEUO:
14983 h.uns_p[0] = 1;
14984 h.uns_p[1] = 1;
14985 h.uns_p[2] = 1;
14986 break;
14988 /* unsigned 3 argument functions. */
14989 case ALTIVEC_BUILTIN_VPERM_16QI_UNS:
14990 case ALTIVEC_BUILTIN_VPERM_8HI_UNS:
14991 case ALTIVEC_BUILTIN_VPERM_4SI_UNS:
14992 case ALTIVEC_BUILTIN_VPERM_2DI_UNS:
14993 case ALTIVEC_BUILTIN_VSEL_16QI_UNS:
14994 case ALTIVEC_BUILTIN_VSEL_8HI_UNS:
14995 case ALTIVEC_BUILTIN_VSEL_4SI_UNS:
14996 case ALTIVEC_BUILTIN_VSEL_2DI_UNS:
14997 case VSX_BUILTIN_VPERM_16QI_UNS:
14998 case VSX_BUILTIN_VPERM_8HI_UNS:
14999 case VSX_BUILTIN_VPERM_4SI_UNS:
15000 case VSX_BUILTIN_VPERM_2DI_UNS:
15001 case VSX_BUILTIN_XXSEL_16QI_UNS:
15002 case VSX_BUILTIN_XXSEL_8HI_UNS:
15003 case VSX_BUILTIN_XXSEL_4SI_UNS:
15004 case VSX_BUILTIN_XXSEL_2DI_UNS:
15005 case CRYPTO_BUILTIN_VPERMXOR:
15006 case CRYPTO_BUILTIN_VPERMXOR_V2DI:
15007 case CRYPTO_BUILTIN_VPERMXOR_V4SI:
15008 case CRYPTO_BUILTIN_VPERMXOR_V8HI:
15009 case CRYPTO_BUILTIN_VPERMXOR_V16QI:
15010 case CRYPTO_BUILTIN_VSHASIGMAW:
15011 case CRYPTO_BUILTIN_VSHASIGMAD:
15012 case CRYPTO_BUILTIN_VSHASIGMA:
15013 h.uns_p[0] = 1;
15014 h.uns_p[1] = 1;
15015 h.uns_p[2] = 1;
15016 h.uns_p[3] = 1;
15017 break;
15019 /* signed permute functions with unsigned char mask. */
15020 case ALTIVEC_BUILTIN_VPERM_16QI:
15021 case ALTIVEC_BUILTIN_VPERM_8HI:
15022 case ALTIVEC_BUILTIN_VPERM_4SI:
15023 case ALTIVEC_BUILTIN_VPERM_4SF:
15024 case ALTIVEC_BUILTIN_VPERM_2DI:
15025 case ALTIVEC_BUILTIN_VPERM_2DF:
15026 case VSX_BUILTIN_VPERM_16QI:
15027 case VSX_BUILTIN_VPERM_8HI:
15028 case VSX_BUILTIN_VPERM_4SI:
15029 case VSX_BUILTIN_VPERM_4SF:
15030 case VSX_BUILTIN_VPERM_2DI:
15031 case VSX_BUILTIN_VPERM_2DF:
15032 h.uns_p[3] = 1;
15033 break;
15035 /* unsigned args, signed return. */
15036 case VSX_BUILTIN_XVCVUXDDP_UNS:
15037 case ALTIVEC_BUILTIN_UNSFLOAT_V4SI_V4SF:
15038 h.uns_p[1] = 1;
15039 break;
15041 /* signed args, unsigned return. */
15042 case VSX_BUILTIN_XVCVDPUXDS_UNS:
15043 case ALTIVEC_BUILTIN_FIXUNS_V4SF_V4SI:
15044 case MISC_BUILTIN_UNPACK_TD:
15045 case MISC_BUILTIN_UNPACK_V1TI:
15046 h.uns_p[0] = 1;
15047 break;
15049 /* unsigned arguments for 128-bit pack instructions. */
15050 case MISC_BUILTIN_PACK_TD:
15051 case MISC_BUILTIN_PACK_V1TI:
15052 h.uns_p[1] = 1;
15053 h.uns_p[2] = 1;
15054 break;
15056 default:
15057 break;
15060 /* Figure out how many args are present. */
15061 while (num_args > 0 && h.mode[num_args] == VOIDmode)
15062 num_args--;
15064 if (num_args == 0)
15065 fatal_error ("internal error: builtin function %s had no type", name);
15067 ret_type = builtin_mode_to_type[h.mode[0]][h.uns_p[0]];
15068 if (!ret_type && h.uns_p[0])
15069 ret_type = builtin_mode_to_type[h.mode[0]][0];
15071 if (!ret_type)
15072 fatal_error ("internal error: builtin function %s had an unexpected "
15073 "return type %s", name, GET_MODE_NAME (h.mode[0]));
15075 for (i = 0; i < (int) ARRAY_SIZE (arg_type); i++)
15076 arg_type[i] = NULL_TREE;
15078 for (i = 0; i < num_args; i++)
15080 int m = (int) h.mode[i+1];
15081 int uns_p = h.uns_p[i+1];
15083 arg_type[i] = builtin_mode_to_type[m][uns_p];
15084 if (!arg_type[i] && uns_p)
15085 arg_type[i] = builtin_mode_to_type[m][0];
15087 if (!arg_type[i])
15088 fatal_error ("internal error: builtin function %s, argument %d "
15089 "had unexpected argument type %s", name, i,
15090 GET_MODE_NAME (m));
15093 found = htab_find_slot (builtin_hash_table, &h, INSERT);
15094 if (*found == NULL)
15096 h2 = ggc_alloc_builtin_hash_struct ();
15097 *h2 = h;
15098 *found = (void *)h2;
15100 h2->type = build_function_type_list (ret_type, arg_type[0], arg_type[1],
15101 arg_type[2], NULL_TREE);
15104 return ((struct builtin_hash_struct *)(*found))->type;
15107 static void
15108 rs6000_common_init_builtins (void)
15110 const struct builtin_description *d;
15111 size_t i;
15113 tree opaque_ftype_opaque = NULL_TREE;
15114 tree opaque_ftype_opaque_opaque = NULL_TREE;
15115 tree opaque_ftype_opaque_opaque_opaque = NULL_TREE;
15116 tree v2si_ftype_qi = NULL_TREE;
15117 tree v2si_ftype_v2si_qi = NULL_TREE;
15118 tree v2si_ftype_int_qi = NULL_TREE;
15119 HOST_WIDE_INT builtin_mask = rs6000_builtin_mask;
15121 if (!TARGET_PAIRED_FLOAT)
15123 builtin_mode_to_type[V2SImode][0] = opaque_V2SI_type_node;
15124 builtin_mode_to_type[V2SFmode][0] = opaque_V2SF_type_node;
15127 /* Paired and SPE builtins are only available if you build a compiler with
15128 the appropriate options, so only create those builtins with the
15129 appropriate compiler option. Create Altivec and VSX builtins on machines
15130 with at least the general purpose extensions (970 and newer) to allow the
15131 use of the target attribute.. */
15133 if (TARGET_EXTRA_BUILTINS)
15134 builtin_mask |= RS6000_BTM_COMMON;
15136 /* Add the ternary operators. */
15137 d = bdesc_3arg;
15138 for (i = 0; i < ARRAY_SIZE (bdesc_3arg); i++, d++)
15140 tree type;
15141 HOST_WIDE_INT mask = d->mask;
15143 if ((mask & builtin_mask) != mask)
15145 if (TARGET_DEBUG_BUILTIN)
15146 fprintf (stderr, "rs6000_builtin, skip ternary %s\n", d->name);
15147 continue;
15150 if (rs6000_overloaded_builtin_p (d->code))
15152 if (! (type = opaque_ftype_opaque_opaque_opaque))
15153 type = opaque_ftype_opaque_opaque_opaque
15154 = build_function_type_list (opaque_V4SI_type_node,
15155 opaque_V4SI_type_node,
15156 opaque_V4SI_type_node,
15157 opaque_V4SI_type_node,
15158 NULL_TREE);
15160 else
15162 enum insn_code icode = d->icode;
15163 if (d->name == 0)
15165 if (TARGET_DEBUG_BUILTIN)
15166 fprintf (stderr, "rs6000_builtin, bdesc_3arg[%ld] no name\n",
15167 (long unsigned)i);
15169 continue;
15172 if (icode == CODE_FOR_nothing)
15174 if (TARGET_DEBUG_BUILTIN)
15175 fprintf (stderr, "rs6000_builtin, skip ternary %s (no code)\n",
15176 d->name);
15178 continue;
15181 type = builtin_function_type (insn_data[icode].operand[0].mode,
15182 insn_data[icode].operand[1].mode,
15183 insn_data[icode].operand[2].mode,
15184 insn_data[icode].operand[3].mode,
15185 d->code, d->name);
15188 def_builtin (d->name, type, d->code);
15191 /* Add the binary operators. */
15192 d = bdesc_2arg;
15193 for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
15195 enum machine_mode mode0, mode1, mode2;
15196 tree type;
15197 HOST_WIDE_INT mask = d->mask;
15199 if ((mask & builtin_mask) != mask)
15201 if (TARGET_DEBUG_BUILTIN)
15202 fprintf (stderr, "rs6000_builtin, skip binary %s\n", d->name);
15203 continue;
15206 if (rs6000_overloaded_builtin_p (d->code))
15208 if (! (type = opaque_ftype_opaque_opaque))
15209 type = opaque_ftype_opaque_opaque
15210 = build_function_type_list (opaque_V4SI_type_node,
15211 opaque_V4SI_type_node,
15212 opaque_V4SI_type_node,
15213 NULL_TREE);
15215 else
15217 enum insn_code icode = d->icode;
15218 if (d->name == 0)
15220 if (TARGET_DEBUG_BUILTIN)
15221 fprintf (stderr, "rs6000_builtin, bdesc_2arg[%ld] no name\n",
15222 (long unsigned)i);
15224 continue;
15227 if (icode == CODE_FOR_nothing)
15229 if (TARGET_DEBUG_BUILTIN)
15230 fprintf (stderr, "rs6000_builtin, skip binary %s (no code)\n",
15231 d->name);
15233 continue;
15236 mode0 = insn_data[icode].operand[0].mode;
15237 mode1 = insn_data[icode].operand[1].mode;
15238 mode2 = insn_data[icode].operand[2].mode;
15240 if (mode0 == V2SImode && mode1 == V2SImode && mode2 == QImode)
15242 if (! (type = v2si_ftype_v2si_qi))
15243 type = v2si_ftype_v2si_qi
15244 = build_function_type_list (opaque_V2SI_type_node,
15245 opaque_V2SI_type_node,
15246 char_type_node,
15247 NULL_TREE);
15250 else if (mode0 == V2SImode && GET_MODE_CLASS (mode1) == MODE_INT
15251 && mode2 == QImode)
15253 if (! (type = v2si_ftype_int_qi))
15254 type = v2si_ftype_int_qi
15255 = build_function_type_list (opaque_V2SI_type_node,
15256 integer_type_node,
15257 char_type_node,
15258 NULL_TREE);
15261 else
15262 type = builtin_function_type (mode0, mode1, mode2, VOIDmode,
15263 d->code, d->name);
15266 def_builtin (d->name, type, d->code);
15269 /* Add the simple unary operators. */
15270 d = bdesc_1arg;
15271 for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
15273 enum machine_mode mode0, mode1;
15274 tree type;
15275 HOST_WIDE_INT mask = d->mask;
15277 if ((mask & builtin_mask) != mask)
15279 if (TARGET_DEBUG_BUILTIN)
15280 fprintf (stderr, "rs6000_builtin, skip unary %s\n", d->name);
15281 continue;
15284 if (rs6000_overloaded_builtin_p (d->code))
15286 if (! (type = opaque_ftype_opaque))
15287 type = opaque_ftype_opaque
15288 = build_function_type_list (opaque_V4SI_type_node,
15289 opaque_V4SI_type_node,
15290 NULL_TREE);
15292 else
15294 enum insn_code icode = d->icode;
15295 if (d->name == 0)
15297 if (TARGET_DEBUG_BUILTIN)
15298 fprintf (stderr, "rs6000_builtin, bdesc_1arg[%ld] no name\n",
15299 (long unsigned)i);
15301 continue;
15304 if (icode == CODE_FOR_nothing)
15306 if (TARGET_DEBUG_BUILTIN)
15307 fprintf (stderr, "rs6000_builtin, skip unary %s (no code)\n",
15308 d->name);
15310 continue;
15313 mode0 = insn_data[icode].operand[0].mode;
15314 mode1 = insn_data[icode].operand[1].mode;
15316 if (mode0 == V2SImode && mode1 == QImode)
15318 if (! (type = v2si_ftype_qi))
15319 type = v2si_ftype_qi
15320 = build_function_type_list (opaque_V2SI_type_node,
15321 char_type_node,
15322 NULL_TREE);
15325 else
15326 type = builtin_function_type (mode0, mode1, VOIDmode, VOIDmode,
15327 d->code, d->name);
15330 def_builtin (d->name, type, d->code);
15334 static void
15335 rs6000_init_libfuncs (void)
15337 if (!TARGET_IEEEQUAD)
15338 /* AIX/Darwin/64-bit Linux quad floating point routines. */
15339 if (!TARGET_XL_COMPAT)
15341 set_optab_libfunc (add_optab, TFmode, "__gcc_qadd");
15342 set_optab_libfunc (sub_optab, TFmode, "__gcc_qsub");
15343 set_optab_libfunc (smul_optab, TFmode, "__gcc_qmul");
15344 set_optab_libfunc (sdiv_optab, TFmode, "__gcc_qdiv");
15346 if (!(TARGET_HARD_FLOAT && (TARGET_FPRS || TARGET_E500_DOUBLE)))
15348 set_optab_libfunc (neg_optab, TFmode, "__gcc_qneg");
15349 set_optab_libfunc (eq_optab, TFmode, "__gcc_qeq");
15350 set_optab_libfunc (ne_optab, TFmode, "__gcc_qne");
15351 set_optab_libfunc (gt_optab, TFmode, "__gcc_qgt");
15352 set_optab_libfunc (ge_optab, TFmode, "__gcc_qge");
15353 set_optab_libfunc (lt_optab, TFmode, "__gcc_qlt");
15354 set_optab_libfunc (le_optab, TFmode, "__gcc_qle");
15356 set_conv_libfunc (sext_optab, TFmode, SFmode, "__gcc_stoq");
15357 set_conv_libfunc (sext_optab, TFmode, DFmode, "__gcc_dtoq");
15358 set_conv_libfunc (trunc_optab, SFmode, TFmode, "__gcc_qtos");
15359 set_conv_libfunc (trunc_optab, DFmode, TFmode, "__gcc_qtod");
15360 set_conv_libfunc (sfix_optab, SImode, TFmode, "__gcc_qtoi");
15361 set_conv_libfunc (ufix_optab, SImode, TFmode, "__gcc_qtou");
15362 set_conv_libfunc (sfloat_optab, TFmode, SImode, "__gcc_itoq");
15363 set_conv_libfunc (ufloat_optab, TFmode, SImode, "__gcc_utoq");
15366 if (!(TARGET_HARD_FLOAT && TARGET_FPRS))
15367 set_optab_libfunc (unord_optab, TFmode, "__gcc_qunord");
15369 else
15371 set_optab_libfunc (add_optab, TFmode, "_xlqadd");
15372 set_optab_libfunc (sub_optab, TFmode, "_xlqsub");
15373 set_optab_libfunc (smul_optab, TFmode, "_xlqmul");
15374 set_optab_libfunc (sdiv_optab, TFmode, "_xlqdiv");
15376 else
15378 /* 32-bit SVR4 quad floating point routines. */
15380 set_optab_libfunc (add_optab, TFmode, "_q_add");
15381 set_optab_libfunc (sub_optab, TFmode, "_q_sub");
15382 set_optab_libfunc (neg_optab, TFmode, "_q_neg");
15383 set_optab_libfunc (smul_optab, TFmode, "_q_mul");
15384 set_optab_libfunc (sdiv_optab, TFmode, "_q_div");
15385 if (TARGET_PPC_GPOPT)
15386 set_optab_libfunc (sqrt_optab, TFmode, "_q_sqrt");
15388 set_optab_libfunc (eq_optab, TFmode, "_q_feq");
15389 set_optab_libfunc (ne_optab, TFmode, "_q_fne");
15390 set_optab_libfunc (gt_optab, TFmode, "_q_fgt");
15391 set_optab_libfunc (ge_optab, TFmode, "_q_fge");
15392 set_optab_libfunc (lt_optab, TFmode, "_q_flt");
15393 set_optab_libfunc (le_optab, TFmode, "_q_fle");
15395 set_conv_libfunc (sext_optab, TFmode, SFmode, "_q_stoq");
15396 set_conv_libfunc (sext_optab, TFmode, DFmode, "_q_dtoq");
15397 set_conv_libfunc (trunc_optab, SFmode, TFmode, "_q_qtos");
15398 set_conv_libfunc (trunc_optab, DFmode, TFmode, "_q_qtod");
15399 set_conv_libfunc (sfix_optab, SImode, TFmode, "_q_qtoi");
15400 set_conv_libfunc (ufix_optab, SImode, TFmode, "_q_qtou");
15401 set_conv_libfunc (sfloat_optab, TFmode, SImode, "_q_itoq");
15402 set_conv_libfunc (ufloat_optab, TFmode, SImode, "_q_utoq");
15407 /* Expand a block clear operation, and return 1 if successful. Return 0
15408 if we should let the compiler generate normal code.
15410 operands[0] is the destination
15411 operands[1] is the length
15412 operands[3] is the alignment */
15415 expand_block_clear (rtx operands[])
15417 rtx orig_dest = operands[0];
15418 rtx bytes_rtx = operands[1];
15419 rtx align_rtx = operands[3];
15420 bool constp = (GET_CODE (bytes_rtx) == CONST_INT);
15421 HOST_WIDE_INT align;
15422 HOST_WIDE_INT bytes;
15423 int offset;
15424 int clear_bytes;
15425 int clear_step;
15427 /* If this is not a fixed size move, just call memcpy */
15428 if (! constp)
15429 return 0;
15431 /* This must be a fixed size alignment */
15432 gcc_assert (GET_CODE (align_rtx) == CONST_INT);
15433 align = INTVAL (align_rtx) * BITS_PER_UNIT;
15435 /* Anything to clear? */
15436 bytes = INTVAL (bytes_rtx);
15437 if (bytes <= 0)
15438 return 1;
15440 /* Use the builtin memset after a point, to avoid huge code bloat.
15441 When optimize_size, avoid any significant code bloat; calling
15442 memset is about 4 instructions, so allow for one instruction to
15443 load zero and three to do clearing. */
15444 if (TARGET_ALTIVEC && align >= 128)
15445 clear_step = 16;
15446 else if (TARGET_POWERPC64 && align >= 32)
15447 clear_step = 8;
15448 else if (TARGET_SPE && align >= 64)
15449 clear_step = 8;
15450 else
15451 clear_step = 4;
15453 if (optimize_size && bytes > 3 * clear_step)
15454 return 0;
15455 if (! optimize_size && bytes > 8 * clear_step)
15456 return 0;
15458 for (offset = 0; bytes > 0; offset += clear_bytes, bytes -= clear_bytes)
15460 enum machine_mode mode = BLKmode;
15461 rtx dest;
15463 if (bytes >= 16 && TARGET_ALTIVEC && align >= 128)
15465 clear_bytes = 16;
15466 mode = V4SImode;
15468 else if (bytes >= 8 && TARGET_SPE && align >= 64)
15470 clear_bytes = 8;
15471 mode = V2SImode;
15473 else if (bytes >= 8 && TARGET_POWERPC64
15474 /* 64-bit loads and stores require word-aligned
15475 displacements. */
15476 && (align >= 64 || (!STRICT_ALIGNMENT && align >= 32)))
15478 clear_bytes = 8;
15479 mode = DImode;
15481 else if (bytes >= 4 && (align >= 32 || !STRICT_ALIGNMENT))
15482 { /* move 4 bytes */
15483 clear_bytes = 4;
15484 mode = SImode;
15486 else if (bytes >= 2 && (align >= 16 || !STRICT_ALIGNMENT))
15487 { /* move 2 bytes */
15488 clear_bytes = 2;
15489 mode = HImode;
15491 else /* move 1 byte at a time */
15493 clear_bytes = 1;
15494 mode = QImode;
15497 dest = adjust_address (orig_dest, mode, offset);
15499 emit_move_insn (dest, CONST0_RTX (mode));
15502 return 1;
15506 /* Expand a block move operation, and return 1 if successful. Return 0
15507 if we should let the compiler generate normal code.
15509 operands[0] is the destination
15510 operands[1] is the source
15511 operands[2] is the length
15512 operands[3] is the alignment */
15514 #define MAX_MOVE_REG 4
15517 expand_block_move (rtx operands[])
15519 rtx orig_dest = operands[0];
15520 rtx orig_src = operands[1];
15521 rtx bytes_rtx = operands[2];
15522 rtx align_rtx = operands[3];
15523 int constp = (GET_CODE (bytes_rtx) == CONST_INT);
15524 int align;
15525 int bytes;
15526 int offset;
15527 int move_bytes;
15528 rtx stores[MAX_MOVE_REG];
15529 int num_reg = 0;
15531 /* If this is not a fixed size move, just call memcpy */
15532 if (! constp)
15533 return 0;
15535 /* This must be a fixed size alignment */
15536 gcc_assert (GET_CODE (align_rtx) == CONST_INT);
15537 align = INTVAL (align_rtx) * BITS_PER_UNIT;
15539 /* Anything to move? */
15540 bytes = INTVAL (bytes_rtx);
15541 if (bytes <= 0)
15542 return 1;
15544 if (bytes > rs6000_block_move_inline_limit)
15545 return 0;
15547 for (offset = 0; bytes > 0; offset += move_bytes, bytes -= move_bytes)
15549 union {
15550 rtx (*movmemsi) (rtx, rtx, rtx, rtx);
15551 rtx (*mov) (rtx, rtx);
15552 } gen_func;
15553 enum machine_mode mode = BLKmode;
15554 rtx src, dest;
15556 /* Altivec first, since it will be faster than a string move
15557 when it applies, and usually not significantly larger. */
15558 if (TARGET_ALTIVEC && bytes >= 16 && align >= 128)
15560 move_bytes = 16;
15561 mode = V4SImode;
15562 gen_func.mov = gen_movv4si;
15564 else if (TARGET_SPE && bytes >= 8 && align >= 64)
15566 move_bytes = 8;
15567 mode = V2SImode;
15568 gen_func.mov = gen_movv2si;
15570 else if (TARGET_STRING
15571 && bytes > 24 /* move up to 32 bytes at a time */
15572 && ! fixed_regs[5]
15573 && ! fixed_regs[6]
15574 && ! fixed_regs[7]
15575 && ! fixed_regs[8]
15576 && ! fixed_regs[9]
15577 && ! fixed_regs[10]
15578 && ! fixed_regs[11]
15579 && ! fixed_regs[12])
15581 move_bytes = (bytes > 32) ? 32 : bytes;
15582 gen_func.movmemsi = gen_movmemsi_8reg;
15584 else if (TARGET_STRING
15585 && bytes > 16 /* move up to 24 bytes at a time */
15586 && ! fixed_regs[5]
15587 && ! fixed_regs[6]
15588 && ! fixed_regs[7]
15589 && ! fixed_regs[8]
15590 && ! fixed_regs[9]
15591 && ! fixed_regs[10])
15593 move_bytes = (bytes > 24) ? 24 : bytes;
15594 gen_func.movmemsi = gen_movmemsi_6reg;
15596 else if (TARGET_STRING
15597 && bytes > 8 /* move up to 16 bytes at a time */
15598 && ! fixed_regs[5]
15599 && ! fixed_regs[6]
15600 && ! fixed_regs[7]
15601 && ! fixed_regs[8])
15603 move_bytes = (bytes > 16) ? 16 : bytes;
15604 gen_func.movmemsi = gen_movmemsi_4reg;
15606 else if (bytes >= 8 && TARGET_POWERPC64
15607 /* 64-bit loads and stores require word-aligned
15608 displacements. */
15609 && (align >= 64 || (!STRICT_ALIGNMENT && align >= 32)))
15611 move_bytes = 8;
15612 mode = DImode;
15613 gen_func.mov = gen_movdi;
15615 else if (TARGET_STRING && bytes > 4 && !TARGET_POWERPC64)
15616 { /* move up to 8 bytes at a time */
15617 move_bytes = (bytes > 8) ? 8 : bytes;
15618 gen_func.movmemsi = gen_movmemsi_2reg;
15620 else if (bytes >= 4 && (align >= 32 || !STRICT_ALIGNMENT))
15621 { /* move 4 bytes */
15622 move_bytes = 4;
15623 mode = SImode;
15624 gen_func.mov = gen_movsi;
15626 else if (bytes >= 2 && (align >= 16 || !STRICT_ALIGNMENT))
15627 { /* move 2 bytes */
15628 move_bytes = 2;
15629 mode = HImode;
15630 gen_func.mov = gen_movhi;
15632 else if (TARGET_STRING && bytes > 1)
15633 { /* move up to 4 bytes at a time */
15634 move_bytes = (bytes > 4) ? 4 : bytes;
15635 gen_func.movmemsi = gen_movmemsi_1reg;
15637 else /* move 1 byte at a time */
15639 move_bytes = 1;
15640 mode = QImode;
15641 gen_func.mov = gen_movqi;
15644 src = adjust_address (orig_src, mode, offset);
15645 dest = adjust_address (orig_dest, mode, offset);
15647 if (mode != BLKmode)
15649 rtx tmp_reg = gen_reg_rtx (mode);
15651 emit_insn ((*gen_func.mov) (tmp_reg, src));
15652 stores[num_reg++] = (*gen_func.mov) (dest, tmp_reg);
15655 if (mode == BLKmode || num_reg >= MAX_MOVE_REG || bytes == move_bytes)
15657 int i;
15658 for (i = 0; i < num_reg; i++)
15659 emit_insn (stores[i]);
15660 num_reg = 0;
15663 if (mode == BLKmode)
15665 /* Move the address into scratch registers. The movmemsi
15666 patterns require zero offset. */
15667 if (!REG_P (XEXP (src, 0)))
15669 rtx src_reg = copy_addr_to_reg (XEXP (src, 0));
15670 src = replace_equiv_address (src, src_reg);
15672 set_mem_size (src, move_bytes);
15674 if (!REG_P (XEXP (dest, 0)))
15676 rtx dest_reg = copy_addr_to_reg (XEXP (dest, 0));
15677 dest = replace_equiv_address (dest, dest_reg);
15679 set_mem_size (dest, move_bytes);
15681 emit_insn ((*gen_func.movmemsi) (dest, src,
15682 GEN_INT (move_bytes & 31),
15683 align_rtx));
15687 return 1;
15691 /* Return a string to perform a load_multiple operation.
15692 operands[0] is the vector.
15693 operands[1] is the source address.
15694 operands[2] is the first destination register. */
15696 const char *
15697 rs6000_output_load_multiple (rtx operands[3])
15699 /* We have to handle the case where the pseudo used to contain the address
15700 is assigned to one of the output registers. */
15701 int i, j;
15702 int words = XVECLEN (operands[0], 0);
15703 rtx xop[10];
15705 if (XVECLEN (operands[0], 0) == 1)
15706 return "lwz %2,0(%1)";
15708 for (i = 0; i < words; i++)
15709 if (refers_to_regno_p (REGNO (operands[2]) + i,
15710 REGNO (operands[2]) + i + 1, operands[1], 0))
15712 if (i == words-1)
15714 xop[0] = GEN_INT (4 * (words-1));
15715 xop[1] = operands[1];
15716 xop[2] = operands[2];
15717 output_asm_insn ("lswi %2,%1,%0\n\tlwz %1,%0(%1)", xop);
15718 return "";
15720 else if (i == 0)
15722 xop[0] = GEN_INT (4 * (words-1));
15723 xop[1] = operands[1];
15724 xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
15725 output_asm_insn ("addi %1,%1,4\n\tlswi %2,%1,%0\n\tlwz %1,-4(%1)", xop);
15726 return "";
15728 else
15730 for (j = 0; j < words; j++)
15731 if (j != i)
15733 xop[0] = GEN_INT (j * 4);
15734 xop[1] = operands[1];
15735 xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + j);
15736 output_asm_insn ("lwz %2,%0(%1)", xop);
15738 xop[0] = GEN_INT (i * 4);
15739 xop[1] = operands[1];
15740 output_asm_insn ("lwz %1,%0(%1)", xop);
15741 return "";
15745 return "lswi %2,%1,%N0";
15749 /* A validation routine: say whether CODE, a condition code, and MODE
15750 match. The other alternatives either don't make sense or should
15751 never be generated. */
15753 void
15754 validate_condition_mode (enum rtx_code code, enum machine_mode mode)
15756 gcc_assert ((GET_RTX_CLASS (code) == RTX_COMPARE
15757 || GET_RTX_CLASS (code) == RTX_COMM_COMPARE)
15758 && GET_MODE_CLASS (mode) == MODE_CC);
15760 /* These don't make sense. */
15761 gcc_assert ((code != GT && code != LT && code != GE && code != LE)
15762 || mode != CCUNSmode);
15764 gcc_assert ((code != GTU && code != LTU && code != GEU && code != LEU)
15765 || mode == CCUNSmode);
15767 gcc_assert (mode == CCFPmode
15768 || (code != ORDERED && code != UNORDERED
15769 && code != UNEQ && code != LTGT
15770 && code != UNGT && code != UNLT
15771 && code != UNGE && code != UNLE));
15773 /* These should never be generated except for
15774 flag_finite_math_only. */
15775 gcc_assert (mode != CCFPmode
15776 || flag_finite_math_only
15777 || (code != LE && code != GE
15778 && code != UNEQ && code != LTGT
15779 && code != UNGT && code != UNLT));
15781 /* These are invalid; the information is not there. */
15782 gcc_assert (mode != CCEQmode || code == EQ || code == NE);
15786 /* Return 1 if ANDOP is a mask that has no bits on that are not in the
15787 mask required to convert the result of a rotate insn into a shift
15788 left insn of SHIFTOP bits. Both are known to be SImode CONST_INT. */
15791 includes_lshift_p (rtx shiftop, rtx andop)
15793 unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
15795 shift_mask <<= INTVAL (shiftop);
15797 return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
15800 /* Similar, but for right shift. */
15803 includes_rshift_p (rtx shiftop, rtx andop)
15805 unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
15807 shift_mask >>= INTVAL (shiftop);
15809 return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
15812 /* Return 1 if ANDOP is a mask suitable for use with an rldic insn
15813 to perform a left shift. It must have exactly SHIFTOP least
15814 significant 0's, then one or more 1's, then zero or more 0's. */
15817 includes_rldic_lshift_p (rtx shiftop, rtx andop)
15819 if (GET_CODE (andop) == CONST_INT)
15821 HOST_WIDE_INT c, lsb, shift_mask;
15823 c = INTVAL (andop);
15824 if (c == 0 || c == ~0)
15825 return 0;
15827 shift_mask = ~0;
15828 shift_mask <<= INTVAL (shiftop);
15830 /* Find the least significant one bit. */
15831 lsb = c & -c;
15833 /* It must coincide with the LSB of the shift mask. */
15834 if (-lsb != shift_mask)
15835 return 0;
15837 /* Invert to look for the next transition (if any). */
15838 c = ~c;
15840 /* Remove the low group of ones (originally low group of zeros). */
15841 c &= -lsb;
15843 /* Again find the lsb, and check we have all 1's above. */
15844 lsb = c & -c;
15845 return c == -lsb;
15847 else
15848 return 0;
15851 /* Return 1 if ANDOP is a mask suitable for use with an rldicr insn
15852 to perform a left shift. It must have SHIFTOP or more least
15853 significant 0's, with the remainder of the word 1's. */
15856 includes_rldicr_lshift_p (rtx shiftop, rtx andop)
15858 if (GET_CODE (andop) == CONST_INT)
15860 HOST_WIDE_INT c, lsb, shift_mask;
15862 shift_mask = ~0;
15863 shift_mask <<= INTVAL (shiftop);
15864 c = INTVAL (andop);
15866 /* Find the least significant one bit. */
15867 lsb = c & -c;
15869 /* It must be covered by the shift mask.
15870 This test also rejects c == 0. */
15871 if ((lsb & shift_mask) == 0)
15872 return 0;
15874 /* Check we have all 1's above the transition, and reject all 1's. */
15875 return c == -lsb && lsb != 1;
15877 else
15878 return 0;
15881 /* Return 1 if operands will generate a valid arguments to rlwimi
15882 instruction for insert with right shift in 64-bit mode. The mask may
15883 not start on the first bit or stop on the last bit because wrap-around
15884 effects of instruction do not correspond to semantics of RTL insn. */
15887 insvdi_rshift_rlwimi_p (rtx sizeop, rtx startop, rtx shiftop)
15889 if (INTVAL (startop) > 32
15890 && INTVAL (startop) < 64
15891 && INTVAL (sizeop) > 1
15892 && INTVAL (sizeop) + INTVAL (startop) < 64
15893 && INTVAL (shiftop) > 0
15894 && INTVAL (sizeop) + INTVAL (shiftop) < 32
15895 && (64 - (INTVAL (shiftop) & 63)) >= INTVAL (sizeop))
15896 return 1;
15898 return 0;
15901 /* Return 1 if REGNO (reg1) == REGNO (reg2) - 1 making them candidates
15902 for lfq and stfq insns iff the registers are hard registers. */
15905 registers_ok_for_quad_peep (rtx reg1, rtx reg2)
15907 /* We might have been passed a SUBREG. */
15908 if (GET_CODE (reg1) != REG || GET_CODE (reg2) != REG)
15909 return 0;
15911 /* We might have been passed non floating point registers. */
15912 if (!FP_REGNO_P (REGNO (reg1))
15913 || !FP_REGNO_P (REGNO (reg2)))
15914 return 0;
15916 return (REGNO (reg1) == REGNO (reg2) - 1);
15919 /* Return 1 if addr1 and addr2 are suitable for lfq or stfq insn.
15920 addr1 and addr2 must be in consecutive memory locations
15921 (addr2 == addr1 + 8). */
15924 mems_ok_for_quad_peep (rtx mem1, rtx mem2)
15926 rtx addr1, addr2;
15927 unsigned int reg1, reg2;
15928 int offset1, offset2;
15930 /* The mems cannot be volatile. */
15931 if (MEM_VOLATILE_P (mem1) || MEM_VOLATILE_P (mem2))
15932 return 0;
15934 addr1 = XEXP (mem1, 0);
15935 addr2 = XEXP (mem2, 0);
15937 /* Extract an offset (if used) from the first addr. */
15938 if (GET_CODE (addr1) == PLUS)
15940 /* If not a REG, return zero. */
15941 if (GET_CODE (XEXP (addr1, 0)) != REG)
15942 return 0;
15943 else
15945 reg1 = REGNO (XEXP (addr1, 0));
15946 /* The offset must be constant! */
15947 if (GET_CODE (XEXP (addr1, 1)) != CONST_INT)
15948 return 0;
15949 offset1 = INTVAL (XEXP (addr1, 1));
15952 else if (GET_CODE (addr1) != REG)
15953 return 0;
15954 else
15956 reg1 = REGNO (addr1);
15957 /* This was a simple (mem (reg)) expression. Offset is 0. */
15958 offset1 = 0;
15961 /* And now for the second addr. */
15962 if (GET_CODE (addr2) == PLUS)
15964 /* If not a REG, return zero. */
15965 if (GET_CODE (XEXP (addr2, 0)) != REG)
15966 return 0;
15967 else
15969 reg2 = REGNO (XEXP (addr2, 0));
15970 /* The offset must be constant. */
15971 if (GET_CODE (XEXP (addr2, 1)) != CONST_INT)
15972 return 0;
15973 offset2 = INTVAL (XEXP (addr2, 1));
15976 else if (GET_CODE (addr2) != REG)
15977 return 0;
15978 else
15980 reg2 = REGNO (addr2);
15981 /* This was a simple (mem (reg)) expression. Offset is 0. */
15982 offset2 = 0;
15985 /* Both of these must have the same base register. */
15986 if (reg1 != reg2)
15987 return 0;
15989 /* The offset for the second addr must be 8 more than the first addr. */
15990 if (offset2 != offset1 + 8)
15991 return 0;
15993 /* All the tests passed. addr1 and addr2 are valid for lfq or stfq
15994 instructions. */
15995 return 1;
16000 rs6000_secondary_memory_needed_rtx (enum machine_mode mode)
16002 static bool eliminated = false;
16003 rtx ret;
16005 if (mode != SDmode || TARGET_NO_SDMODE_STACK)
16006 ret = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
16007 else
16009 rtx mem = cfun->machine->sdmode_stack_slot;
16010 gcc_assert (mem != NULL_RTX);
16012 if (!eliminated)
16014 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
16015 cfun->machine->sdmode_stack_slot = mem;
16016 eliminated = true;
16018 ret = mem;
16021 if (TARGET_DEBUG_ADDR)
16023 fprintf (stderr, "\nrs6000_secondary_memory_needed_rtx, mode %s, rtx:\n",
16024 GET_MODE_NAME (mode));
16025 if (!ret)
16026 fprintf (stderr, "\tNULL_RTX\n");
16027 else
16028 debug_rtx (ret);
16031 return ret;
16034 /* Return the mode to be used for memory when a secondary memory
16035 location is needed. For SDmode values we need to use DDmode, in
16036 all other cases we can use the same mode. */
16037 enum machine_mode
16038 rs6000_secondary_memory_needed_mode (enum machine_mode mode)
16040 if (lra_in_progress && mode == SDmode)
16041 return DDmode;
16042 return mode;
16045 static tree
16046 rs6000_check_sdmode (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16048 /* Don't walk into types. */
16049 if (*tp == NULL_TREE || *tp == error_mark_node || TYPE_P (*tp))
16051 *walk_subtrees = 0;
16052 return NULL_TREE;
16055 switch (TREE_CODE (*tp))
16057 case VAR_DECL:
16058 case PARM_DECL:
16059 case FIELD_DECL:
16060 case RESULT_DECL:
16061 case SSA_NAME:
16062 case REAL_CST:
16063 case MEM_REF:
16064 case VIEW_CONVERT_EXPR:
16065 if (TYPE_MODE (TREE_TYPE (*tp)) == SDmode)
16066 return *tp;
16067 break;
16068 default:
16069 break;
16072 return NULL_TREE;
16075 /* Classify a register type. Because the FMRGOW/FMRGEW instructions only work
16076 on traditional floating point registers, and the VMRGOW/VMRGEW instructions
16077 only work on the traditional altivec registers, note if an altivec register
16078 was chosen. */
16080 static enum rs6000_reg_type
16081 register_to_reg_type (rtx reg, bool *is_altivec)
16083 HOST_WIDE_INT regno;
16084 enum reg_class rclass;
16086 if (GET_CODE (reg) == SUBREG)
16087 reg = SUBREG_REG (reg);
16089 if (!REG_P (reg))
16090 return NO_REG_TYPE;
16092 regno = REGNO (reg);
16093 if (regno >= FIRST_PSEUDO_REGISTER)
16095 if (!lra_in_progress && !reload_in_progress && !reload_completed)
16096 return PSEUDO_REG_TYPE;
16098 regno = true_regnum (reg);
16099 if (regno < 0 || regno >= FIRST_PSEUDO_REGISTER)
16100 return PSEUDO_REG_TYPE;
16103 gcc_assert (regno >= 0);
16105 if (is_altivec && ALTIVEC_REGNO_P (regno))
16106 *is_altivec = true;
16108 rclass = rs6000_regno_regclass[regno];
16109 return reg_class_to_reg_type[(int)rclass];
16112 /* Helper function for rs6000_secondary_reload to return true if a move to a
16113 different register classe is really a simple move. */
16115 static bool
16116 rs6000_secondary_reload_simple_move (enum rs6000_reg_type to_type,
16117 enum rs6000_reg_type from_type,
16118 enum machine_mode mode)
16120 int size;
16122 /* Add support for various direct moves available. In this function, we only
16123 look at cases where we don't need any extra registers, and one or more
16124 simple move insns are issued. At present, 32-bit integers are not allowed
16125 in FPR/VSX registers. Single precision binary floating is not a simple
16126 move because we need to convert to the single precision memory layout.
16127 The 4-byte SDmode can be moved. */
16128 size = GET_MODE_SIZE (mode);
16129 if (TARGET_DIRECT_MOVE
16130 && ((mode == SDmode) || (TARGET_POWERPC64 && size == 8))
16131 && ((to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
16132 || (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)))
16133 return true;
16135 else if (TARGET_MFPGPR && TARGET_POWERPC64 && size == 8
16136 && ((to_type == GPR_REG_TYPE && from_type == FPR_REG_TYPE)
16137 || (to_type == FPR_REG_TYPE && from_type == GPR_REG_TYPE)))
16138 return true;
16140 else if ((size == 4 || (TARGET_POWERPC64 && size == 8))
16141 && ((to_type == GPR_REG_TYPE && from_type == SPR_REG_TYPE)
16142 || (to_type == SPR_REG_TYPE && from_type == GPR_REG_TYPE)))
16143 return true;
16145 return false;
16148 /* Power8 helper function for rs6000_secondary_reload, handle all of the
16149 special direct moves that involve allocating an extra register, return the
16150 insn code of the helper function if there is such a function or
16151 CODE_FOR_nothing if not. */
16153 static bool
16154 rs6000_secondary_reload_direct_move (enum rs6000_reg_type to_type,
16155 enum rs6000_reg_type from_type,
16156 enum machine_mode mode,
16157 secondary_reload_info *sri,
16158 bool altivec_p)
16160 bool ret = false;
16161 enum insn_code icode = CODE_FOR_nothing;
16162 int cost = 0;
16163 int size = GET_MODE_SIZE (mode);
16165 if (TARGET_POWERPC64)
16167 if (size == 16)
16169 /* Handle moving 128-bit values from GPRs to VSX point registers on
16170 power8 when running in 64-bit mode using XXPERMDI to glue the two
16171 64-bit values back together. */
16172 if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)
16174 cost = 3; /* 2 mtvsrd's, 1 xxpermdi. */
16175 icode = reg_addr[mode].reload_vsx_gpr;
16178 /* Handle moving 128-bit values from VSX point registers to GPRs on
16179 power8 when running in 64-bit mode using XXPERMDI to get access to the
16180 bottom 64-bit value. */
16181 else if (to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
16183 cost = 3; /* 2 mfvsrd's, 1 xxpermdi. */
16184 icode = reg_addr[mode].reload_gpr_vsx;
16188 else if (mode == SFmode)
16190 if (to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
16192 cost = 3; /* xscvdpspn, mfvsrd, and. */
16193 icode = reg_addr[mode].reload_gpr_vsx;
16196 else if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)
16198 cost = 2; /* mtvsrz, xscvspdpn. */
16199 icode = reg_addr[mode].reload_vsx_gpr;
16204 if (TARGET_POWERPC64 && size == 16)
16206 /* Handle moving 128-bit values from GPRs to VSX point registers on
16207 power8 when running in 64-bit mode using XXPERMDI to glue the two
16208 64-bit values back together. */
16209 if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)
16211 cost = 3; /* 2 mtvsrd's, 1 xxpermdi. */
16212 icode = reg_addr[mode].reload_vsx_gpr;
16215 /* Handle moving 128-bit values from VSX point registers to GPRs on
16216 power8 when running in 64-bit mode using XXPERMDI to get access to the
16217 bottom 64-bit value. */
16218 else if (to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
16220 cost = 3; /* 2 mfvsrd's, 1 xxpermdi. */
16221 icode = reg_addr[mode].reload_gpr_vsx;
16225 else if (!TARGET_POWERPC64 && size == 8)
16227 /* Handle moving 64-bit values from GPRs to floating point registers on
16228 power8 when running in 32-bit mode using FMRGOW to glue the two 32-bit
16229 values back together. Altivec register classes must be handled
16230 specially since a different instruction is used, and the secondary
16231 reload support requires a single instruction class in the scratch
16232 register constraint. However, right now TFmode is not allowed in
16233 Altivec registers, so the pattern will never match. */
16234 if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE && !altivec_p)
16236 cost = 3; /* 2 mtvsrwz's, 1 fmrgow. */
16237 icode = reg_addr[mode].reload_fpr_gpr;
16241 if (icode != CODE_FOR_nothing)
16243 ret = true;
16244 if (sri)
16246 sri->icode = icode;
16247 sri->extra_cost = cost;
16251 return ret;
16254 /* Return whether a move between two register classes can be done either
16255 directly (simple move) or via a pattern that uses a single extra temporary
16256 (using power8's direct move in this case. */
16258 static bool
16259 rs6000_secondary_reload_move (enum rs6000_reg_type to_type,
16260 enum rs6000_reg_type from_type,
16261 enum machine_mode mode,
16262 secondary_reload_info *sri,
16263 bool altivec_p)
16265 /* Fall back to load/store reloads if either type is not a register. */
16266 if (to_type == NO_REG_TYPE || from_type == NO_REG_TYPE)
16267 return false;
16269 /* If we haven't allocated registers yet, assume the move can be done for the
16270 standard register types. */
16271 if ((to_type == PSEUDO_REG_TYPE && from_type == PSEUDO_REG_TYPE)
16272 || (to_type == PSEUDO_REG_TYPE && IS_STD_REG_TYPE (from_type))
16273 || (from_type == PSEUDO_REG_TYPE && IS_STD_REG_TYPE (to_type)))
16274 return true;
16276 /* Moves to the same set of registers is a simple move for non-specialized
16277 registers. */
16278 if (to_type == from_type && IS_STD_REG_TYPE (to_type))
16279 return true;
16281 /* Check whether a simple move can be done directly. */
16282 if (rs6000_secondary_reload_simple_move (to_type, from_type, mode))
16284 if (sri)
16286 sri->icode = CODE_FOR_nothing;
16287 sri->extra_cost = 0;
16289 return true;
16292 /* Now check if we can do it in a few steps. */
16293 return rs6000_secondary_reload_direct_move (to_type, from_type, mode, sri,
16294 altivec_p);
16297 /* Inform reload about cases where moving X with a mode MODE to a register in
16298 RCLASS requires an extra scratch or immediate register. Return the class
16299 needed for the immediate register.
16301 For VSX and Altivec, we may need a register to convert sp+offset into
16302 reg+sp.
16304 For misaligned 64-bit gpr loads and stores we need a register to
16305 convert an offset address to indirect. */
16307 static reg_class_t
16308 rs6000_secondary_reload (bool in_p,
16309 rtx x,
16310 reg_class_t rclass_i,
16311 enum machine_mode mode,
16312 secondary_reload_info *sri)
16314 enum reg_class rclass = (enum reg_class) rclass_i;
16315 reg_class_t ret = ALL_REGS;
16316 enum insn_code icode;
16317 bool default_p = false;
16319 sri->icode = CODE_FOR_nothing;
16320 icode = ((in_p)
16321 ? reg_addr[mode].reload_load
16322 : reg_addr[mode].reload_store);
16324 if (REG_P (x) || register_operand (x, mode))
16326 enum rs6000_reg_type to_type = reg_class_to_reg_type[(int)rclass];
16327 bool altivec_p = (rclass == ALTIVEC_REGS);
16328 enum rs6000_reg_type from_type = register_to_reg_type (x, &altivec_p);
16330 if (!in_p)
16332 enum rs6000_reg_type exchange = to_type;
16333 to_type = from_type;
16334 from_type = exchange;
16337 /* Can we do a direct move of some sort? */
16338 if (rs6000_secondary_reload_move (to_type, from_type, mode, sri,
16339 altivec_p))
16341 icode = (enum insn_code)sri->icode;
16342 default_p = false;
16343 ret = NO_REGS;
16347 /* Handle vector moves with reload helper functions. */
16348 if (ret == ALL_REGS && icode != CODE_FOR_nothing)
16350 ret = NO_REGS;
16351 sri->icode = CODE_FOR_nothing;
16352 sri->extra_cost = 0;
16354 if (GET_CODE (x) == MEM)
16356 rtx addr = XEXP (x, 0);
16358 /* Loads to and stores from gprs can do reg+offset, and wouldn't need
16359 an extra register in that case, but it would need an extra
16360 register if the addressing is reg+reg or (reg+reg)&(-16). Special
16361 case load/store quad. */
16362 if (rclass == GENERAL_REGS || rclass == BASE_REGS)
16364 if (TARGET_POWERPC64 && TARGET_QUAD_MEMORY
16365 && GET_MODE_SIZE (mode) == 16
16366 && quad_memory_operand (x, mode))
16368 sri->icode = icode;
16369 sri->extra_cost = 2;
16372 else if (!legitimate_indirect_address_p (addr, false)
16373 && !rs6000_legitimate_offset_address_p (PTImode, addr,
16374 false, true))
16376 sri->icode = icode;
16377 /* account for splitting the loads, and converting the
16378 address from reg+reg to reg. */
16379 sri->extra_cost = (((TARGET_64BIT) ? 3 : 5)
16380 + ((GET_CODE (addr) == AND) ? 1 : 0));
16383 /* Allow scalar loads to/from the traditional floating point
16384 registers, even if VSX memory is set. */
16385 else if ((rclass == FLOAT_REGS || rclass == NO_REGS)
16386 && (GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8)
16387 && (legitimate_indirect_address_p (addr, false)
16388 || legitimate_indirect_address_p (addr, false)
16389 || rs6000_legitimate_offset_address_p (mode, addr,
16390 false, true)))
16393 /* Loads to and stores from vector registers can only do reg+reg
16394 addressing. Altivec registers can also do (reg+reg)&(-16). Allow
16395 scalar modes loading up the traditional floating point registers
16396 to use offset addresses. */
16397 else if (rclass == VSX_REGS || rclass == ALTIVEC_REGS
16398 || rclass == FLOAT_REGS || rclass == NO_REGS)
16400 if (!VECTOR_MEM_ALTIVEC_P (mode)
16401 && GET_CODE (addr) == AND
16402 && GET_CODE (XEXP (addr, 1)) == CONST_INT
16403 && INTVAL (XEXP (addr, 1)) == -16
16404 && (legitimate_indirect_address_p (XEXP (addr, 0), false)
16405 || legitimate_indexed_address_p (XEXP (addr, 0), false)))
16407 sri->icode = icode;
16408 sri->extra_cost = ((GET_CODE (XEXP (addr, 0)) == PLUS)
16409 ? 2 : 1);
16411 else if (!legitimate_indirect_address_p (addr, false)
16412 && (rclass == NO_REGS
16413 || !legitimate_indexed_address_p (addr, false)))
16415 sri->icode = icode;
16416 sri->extra_cost = 1;
16418 else
16419 icode = CODE_FOR_nothing;
16421 /* Any other loads, including to pseudo registers which haven't been
16422 assigned to a register yet, default to require a scratch
16423 register. */
16424 else
16426 sri->icode = icode;
16427 sri->extra_cost = 2;
16430 else if (REG_P (x))
16432 int regno = true_regnum (x);
16434 icode = CODE_FOR_nothing;
16435 if (regno < 0 || regno >= FIRST_PSEUDO_REGISTER)
16436 default_p = true;
16437 else
16439 enum reg_class xclass = REGNO_REG_CLASS (regno);
16440 enum rs6000_reg_type rtype1 = reg_class_to_reg_type[(int)rclass];
16441 enum rs6000_reg_type rtype2 = reg_class_to_reg_type[(int)xclass];
16443 /* If memory is needed, use default_secondary_reload to create the
16444 stack slot. */
16445 if (rtype1 != rtype2 || !IS_STD_REG_TYPE (rtype1))
16446 default_p = true;
16447 else
16448 ret = NO_REGS;
16451 else
16452 default_p = true;
16454 else if (TARGET_POWERPC64
16455 && reg_class_to_reg_type[(int)rclass] == GPR_REG_TYPE
16456 && MEM_P (x)
16457 && GET_MODE_SIZE (GET_MODE (x)) >= UNITS_PER_WORD)
16459 rtx addr = XEXP (x, 0);
16460 rtx off = address_offset (addr);
16462 if (off != NULL_RTX)
16464 unsigned int extra = GET_MODE_SIZE (GET_MODE (x)) - UNITS_PER_WORD;
16465 unsigned HOST_WIDE_INT offset = INTVAL (off);
16467 /* We need a secondary reload when our legitimate_address_p
16468 says the address is good (as otherwise the entire address
16469 will be reloaded), and the offset is not a multiple of
16470 four or we have an address wrap. Address wrap will only
16471 occur for LO_SUMs since legitimate_offset_address_p
16472 rejects addresses for 16-byte mems that will wrap. */
16473 if (GET_CODE (addr) == LO_SUM
16474 ? (1 /* legitimate_address_p allows any offset for lo_sum */
16475 && ((offset & 3) != 0
16476 || ((offset & 0xffff) ^ 0x8000) >= 0x10000 - extra))
16477 : (offset + 0x8000 < 0x10000 - extra /* legitimate_address_p */
16478 && (offset & 3) != 0))
16480 if (in_p)
16481 sri->icode = CODE_FOR_reload_di_load;
16482 else
16483 sri->icode = CODE_FOR_reload_di_store;
16484 sri->extra_cost = 2;
16485 ret = NO_REGS;
16487 else
16488 default_p = true;
16490 else
16491 default_p = true;
16493 else if (!TARGET_POWERPC64
16494 && reg_class_to_reg_type[(int)rclass] == GPR_REG_TYPE
16495 && MEM_P (x)
16496 && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
16498 rtx addr = XEXP (x, 0);
16499 rtx off = address_offset (addr);
16501 if (off != NULL_RTX)
16503 unsigned int extra = GET_MODE_SIZE (GET_MODE (x)) - UNITS_PER_WORD;
16504 unsigned HOST_WIDE_INT offset = INTVAL (off);
16506 /* We need a secondary reload when our legitimate_address_p
16507 says the address is good (as otherwise the entire address
16508 will be reloaded), and we have a wrap.
16510 legitimate_lo_sum_address_p allows LO_SUM addresses to
16511 have any offset so test for wrap in the low 16 bits.
16513 legitimate_offset_address_p checks for the range
16514 [-0x8000,0x7fff] for mode size of 8 and [-0x8000,0x7ff7]
16515 for mode size of 16. We wrap at [0x7ffc,0x7fff] and
16516 [0x7ff4,0x7fff] respectively, so test for the
16517 intersection of these ranges, [0x7ffc,0x7fff] and
16518 [0x7ff4,0x7ff7] respectively.
16520 Note that the address we see here may have been
16521 manipulated by legitimize_reload_address. */
16522 if (GET_CODE (addr) == LO_SUM
16523 ? ((offset & 0xffff) ^ 0x8000) >= 0x10000 - extra
16524 : offset - (0x8000 - extra) < UNITS_PER_WORD)
16526 if (in_p)
16527 sri->icode = CODE_FOR_reload_si_load;
16528 else
16529 sri->icode = CODE_FOR_reload_si_store;
16530 sri->extra_cost = 2;
16531 ret = NO_REGS;
16533 else
16534 default_p = true;
16536 else
16537 default_p = true;
16539 else
16540 default_p = true;
16542 if (default_p)
16543 ret = default_secondary_reload (in_p, x, rclass, mode, sri);
16545 gcc_assert (ret != ALL_REGS);
16547 if (TARGET_DEBUG_ADDR)
16549 fprintf (stderr,
16550 "\nrs6000_secondary_reload, return %s, in_p = %s, rclass = %s, "
16551 "mode = %s",
16552 reg_class_names[ret],
16553 in_p ? "true" : "false",
16554 reg_class_names[rclass],
16555 GET_MODE_NAME (mode));
16557 if (default_p)
16558 fprintf (stderr, ", default secondary reload");
16560 if (sri->icode != CODE_FOR_nothing)
16561 fprintf (stderr, ", reload func = %s, extra cost = %d\n",
16562 insn_data[sri->icode].name, sri->extra_cost);
16563 else
16564 fprintf (stderr, "\n");
16566 debug_rtx (x);
16569 return ret;
16572 /* Better tracing for rs6000_secondary_reload_inner. */
16574 static void
16575 rs6000_secondary_reload_trace (int line, rtx reg, rtx mem, rtx scratch,
16576 bool store_p)
16578 rtx set, clobber;
16580 gcc_assert (reg != NULL_RTX && mem != NULL_RTX && scratch != NULL_RTX);
16582 fprintf (stderr, "rs6000_secondary_reload_inner:%d, type = %s\n", line,
16583 store_p ? "store" : "load");
16585 if (store_p)
16586 set = gen_rtx_SET (VOIDmode, mem, reg);
16587 else
16588 set = gen_rtx_SET (VOIDmode, reg, mem);
16590 clobber = gen_rtx_CLOBBER (VOIDmode, scratch);
16591 debug_rtx (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set, clobber)));
16594 static void
16595 rs6000_secondary_reload_fail (int line, rtx reg, rtx mem, rtx scratch,
16596 bool store_p)
16598 rs6000_secondary_reload_trace (line, reg, mem, scratch, store_p);
16599 gcc_unreachable ();
16602 /* Fixup reload addresses for Altivec or VSX loads/stores to change SP+offset
16603 to SP+reg addressing. */
16605 void
16606 rs6000_secondary_reload_inner (rtx reg, rtx mem, rtx scratch, bool store_p)
16608 int regno = true_regnum (reg);
16609 enum machine_mode mode = GET_MODE (reg);
16610 enum reg_class rclass;
16611 rtx addr;
16612 rtx and_op2 = NULL_RTX;
16613 rtx addr_op1;
16614 rtx addr_op2;
16615 rtx scratch_or_premodify = scratch;
16616 rtx and_rtx;
16617 rtx cc_clobber;
16619 if (TARGET_DEBUG_ADDR)
16620 rs6000_secondary_reload_trace (__LINE__, reg, mem, scratch, store_p);
16622 if (regno < 0 || regno >= FIRST_PSEUDO_REGISTER)
16623 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
16625 if (GET_CODE (mem) != MEM)
16626 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
16628 rclass = REGNO_REG_CLASS (regno);
16629 addr = find_replacement (&XEXP (mem, 0));
16631 switch (rclass)
16633 /* GPRs can handle reg + small constant, all other addresses need to use
16634 the scratch register. */
16635 case GENERAL_REGS:
16636 case BASE_REGS:
16637 if (GET_CODE (addr) == AND)
16639 and_op2 = XEXP (addr, 1);
16640 addr = find_replacement (&XEXP (addr, 0));
16643 if (GET_CODE (addr) == PRE_MODIFY)
16645 scratch_or_premodify = find_replacement (&XEXP (addr, 0));
16646 if (!REG_P (scratch_or_premodify))
16647 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
16649 addr = find_replacement (&XEXP (addr, 1));
16650 if (GET_CODE (addr) != PLUS)
16651 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
16654 if (GET_CODE (addr) == PLUS
16655 && (and_op2 != NULL_RTX
16656 || !rs6000_legitimate_offset_address_p (PTImode, addr,
16657 false, true)))
16659 /* find_replacement already recurses into both operands of
16660 PLUS so we don't need to call it here. */
16661 addr_op1 = XEXP (addr, 0);
16662 addr_op2 = XEXP (addr, 1);
16663 if (!legitimate_indirect_address_p (addr_op1, false))
16664 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
16666 if (!REG_P (addr_op2)
16667 && (GET_CODE (addr_op2) != CONST_INT
16668 || !satisfies_constraint_I (addr_op2)))
16670 if (TARGET_DEBUG_ADDR)
16672 fprintf (stderr,
16673 "\nMove plus addr to register %s, mode = %s: ",
16674 rs6000_reg_names[REGNO (scratch)],
16675 GET_MODE_NAME (mode));
16676 debug_rtx (addr_op2);
16678 rs6000_emit_move (scratch, addr_op2, Pmode);
16679 addr_op2 = scratch;
16682 emit_insn (gen_rtx_SET (VOIDmode,
16683 scratch_or_premodify,
16684 gen_rtx_PLUS (Pmode,
16685 addr_op1,
16686 addr_op2)));
16688 addr = scratch_or_premodify;
16689 scratch_or_premodify = scratch;
16691 else if (!legitimate_indirect_address_p (addr, false)
16692 && !rs6000_legitimate_offset_address_p (PTImode, addr,
16693 false, true))
16695 if (TARGET_DEBUG_ADDR)
16697 fprintf (stderr, "\nMove addr to register %s, mode = %s: ",
16698 rs6000_reg_names[REGNO (scratch_or_premodify)],
16699 GET_MODE_NAME (mode));
16700 debug_rtx (addr);
16702 rs6000_emit_move (scratch_or_premodify, addr, Pmode);
16703 addr = scratch_or_premodify;
16704 scratch_or_premodify = scratch;
16706 break;
16708 /* Float registers can do offset+reg addressing for scalar types. */
16709 case FLOAT_REGS:
16710 if (legitimate_indirect_address_p (addr, false) /* reg */
16711 || legitimate_indexed_address_p (addr, false) /* reg+reg */
16712 || ((GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8)
16713 && and_op2 == NULL_RTX
16714 && scratch_or_premodify == scratch
16715 && rs6000_legitimate_offset_address_p (mode, addr, false, false)))
16716 break;
16718 /* If this isn't a legacy floating point load/store, fall through to the
16719 VSX defaults. */
16721 /* VSX/Altivec registers can only handle reg+reg addressing. Move other
16722 addresses into a scratch register. */
16723 case VSX_REGS:
16724 case ALTIVEC_REGS:
16726 /* With float regs, we need to handle the AND ourselves, since we can't
16727 use the Altivec instruction with an implicit AND -16. Allow scalar
16728 loads to float registers to use reg+offset even if VSX. */
16729 if (GET_CODE (addr) == AND
16730 && (rclass != ALTIVEC_REGS || GET_MODE_SIZE (mode) != 16
16731 || GET_CODE (XEXP (addr, 1)) != CONST_INT
16732 || INTVAL (XEXP (addr, 1)) != -16
16733 || !VECTOR_MEM_ALTIVEC_P (mode)))
16735 and_op2 = XEXP (addr, 1);
16736 addr = find_replacement (&XEXP (addr, 0));
16739 /* If we aren't using a VSX load, save the PRE_MODIFY register and use it
16740 as the address later. */
16741 if (GET_CODE (addr) == PRE_MODIFY
16742 && ((ALTIVEC_OR_VSX_VECTOR_MODE (mode)
16743 && (rclass != FLOAT_REGS
16744 || (GET_MODE_SIZE (mode) != 4 && GET_MODE_SIZE (mode) != 8)))
16745 || and_op2 != NULL_RTX
16746 || !legitimate_indexed_address_p (XEXP (addr, 1), false)))
16748 scratch_or_premodify = find_replacement (&XEXP (addr, 0));
16749 if (!legitimate_indirect_address_p (scratch_or_premodify, false))
16750 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
16752 addr = find_replacement (&XEXP (addr, 1));
16753 if (GET_CODE (addr) != PLUS)
16754 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
16757 if (legitimate_indirect_address_p (addr, false) /* reg */
16758 || legitimate_indexed_address_p (addr, false) /* reg+reg */
16759 || (GET_CODE (addr) == AND /* Altivec memory */
16760 && rclass == ALTIVEC_REGS
16761 && GET_CODE (XEXP (addr, 1)) == CONST_INT
16762 && INTVAL (XEXP (addr, 1)) == -16
16763 && (legitimate_indirect_address_p (XEXP (addr, 0), false)
16764 || legitimate_indexed_address_p (XEXP (addr, 0), false))))
16767 else if (GET_CODE (addr) == PLUS)
16769 addr_op1 = XEXP (addr, 0);
16770 addr_op2 = XEXP (addr, 1);
16771 if (!REG_P (addr_op1))
16772 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
16774 if (TARGET_DEBUG_ADDR)
16776 fprintf (stderr, "\nMove plus addr to register %s, mode = %s: ",
16777 rs6000_reg_names[REGNO (scratch)], GET_MODE_NAME (mode));
16778 debug_rtx (addr_op2);
16780 rs6000_emit_move (scratch, addr_op2, Pmode);
16781 emit_insn (gen_rtx_SET (VOIDmode,
16782 scratch_or_premodify,
16783 gen_rtx_PLUS (Pmode,
16784 addr_op1,
16785 scratch)));
16786 addr = scratch_or_premodify;
16787 scratch_or_premodify = scratch;
16790 else if (GET_CODE (addr) == SYMBOL_REF || GET_CODE (addr) == CONST
16791 || GET_CODE (addr) == CONST_INT || GET_CODE (addr) == LO_SUM
16792 || REG_P (addr))
16794 if (TARGET_DEBUG_ADDR)
16796 fprintf (stderr, "\nMove addr to register %s, mode = %s: ",
16797 rs6000_reg_names[REGNO (scratch_or_premodify)],
16798 GET_MODE_NAME (mode));
16799 debug_rtx (addr);
16802 rs6000_emit_move (scratch_or_premodify, addr, Pmode);
16803 addr = scratch_or_premodify;
16804 scratch_or_premodify = scratch;
16807 else
16808 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
16810 break;
16812 default:
16813 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
16816 /* If the original address involved a pre-modify that we couldn't use the VSX
16817 memory instruction with update, and we haven't taken care of already,
16818 store the address in the pre-modify register and use that as the
16819 address. */
16820 if (scratch_or_premodify != scratch && scratch_or_premodify != addr)
16822 emit_insn (gen_rtx_SET (VOIDmode, scratch_or_premodify, addr));
16823 addr = scratch_or_premodify;
16826 /* If the original address involved an AND -16 and we couldn't use an ALTIVEC
16827 memory instruction, recreate the AND now, including the clobber which is
16828 generated by the general ANDSI3/ANDDI3 patterns for the
16829 andi. instruction. */
16830 if (and_op2 != NULL_RTX)
16832 if (! legitimate_indirect_address_p (addr, false))
16834 emit_insn (gen_rtx_SET (VOIDmode, scratch, addr));
16835 addr = scratch;
16838 if (TARGET_DEBUG_ADDR)
16840 fprintf (stderr, "\nAnd addr to register %s, mode = %s: ",
16841 rs6000_reg_names[REGNO (scratch)], GET_MODE_NAME (mode));
16842 debug_rtx (and_op2);
16845 and_rtx = gen_rtx_SET (VOIDmode,
16846 scratch,
16847 gen_rtx_AND (Pmode,
16848 addr,
16849 and_op2));
16851 cc_clobber = gen_rtx_CLOBBER (CCmode, gen_rtx_SCRATCH (CCmode));
16852 emit_insn (gen_rtx_PARALLEL (VOIDmode,
16853 gen_rtvec (2, and_rtx, cc_clobber)));
16854 addr = scratch;
16857 /* Adjust the address if it changed. */
16858 if (addr != XEXP (mem, 0))
16860 mem = replace_equiv_address_nv (mem, addr);
16861 if (TARGET_DEBUG_ADDR)
16862 fprintf (stderr, "\nrs6000_secondary_reload_inner, mem adjusted.\n");
16865 /* Now create the move. */
16866 if (store_p)
16867 emit_insn (gen_rtx_SET (VOIDmode, mem, reg));
16868 else
16869 emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
16871 return;
16874 /* Convert reloads involving 64-bit gprs and misaligned offset
16875 addressing, or multiple 32-bit gprs and offsets that are too large,
16876 to use indirect addressing. */
16878 void
16879 rs6000_secondary_reload_gpr (rtx reg, rtx mem, rtx scratch, bool store_p)
16881 int regno = true_regnum (reg);
16882 enum reg_class rclass;
16883 rtx addr;
16884 rtx scratch_or_premodify = scratch;
16886 if (TARGET_DEBUG_ADDR)
16888 fprintf (stderr, "\nrs6000_secondary_reload_gpr, type = %s\n",
16889 store_p ? "store" : "load");
16890 fprintf (stderr, "reg:\n");
16891 debug_rtx (reg);
16892 fprintf (stderr, "mem:\n");
16893 debug_rtx (mem);
16894 fprintf (stderr, "scratch:\n");
16895 debug_rtx (scratch);
16898 gcc_assert (regno >= 0 && regno < FIRST_PSEUDO_REGISTER);
16899 gcc_assert (GET_CODE (mem) == MEM);
16900 rclass = REGNO_REG_CLASS (regno);
16901 gcc_assert (rclass == GENERAL_REGS || rclass == BASE_REGS);
16902 addr = XEXP (mem, 0);
16904 if (GET_CODE (addr) == PRE_MODIFY)
16906 scratch_or_premodify = XEXP (addr, 0);
16907 gcc_assert (REG_P (scratch_or_premodify));
16908 addr = XEXP (addr, 1);
16910 gcc_assert (GET_CODE (addr) == PLUS || GET_CODE (addr) == LO_SUM);
16912 rs6000_emit_move (scratch_or_premodify, addr, Pmode);
16914 mem = replace_equiv_address_nv (mem, scratch_or_premodify);
16916 /* Now create the move. */
16917 if (store_p)
16918 emit_insn (gen_rtx_SET (VOIDmode, mem, reg));
16919 else
16920 emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
16922 return;
16925 /* Allocate a 64-bit stack slot to be used for copying SDmode values through if
16926 this function has any SDmode references. If we are on a power7 or later, we
16927 don't need the 64-bit stack slot since the LFIWZX and STIFWX instructions
16928 can load/store the value. */
16930 static void
16931 rs6000_alloc_sdmode_stack_slot (void)
16933 tree t;
16934 basic_block bb;
16935 gimple_stmt_iterator gsi;
16937 gcc_assert (cfun->machine->sdmode_stack_slot == NULL_RTX);
16938 /* We use a different approach for dealing with the secondary
16939 memory in LRA. */
16940 if (ira_use_lra_p)
16941 return;
16943 if (TARGET_NO_SDMODE_STACK)
16944 return;
16946 FOR_EACH_BB_FN (bb, cfun)
16947 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
16949 tree ret = walk_gimple_op (gsi_stmt (gsi), rs6000_check_sdmode, NULL);
16950 if (ret)
16952 rtx stack = assign_stack_local (DDmode, GET_MODE_SIZE (DDmode), 0);
16953 cfun->machine->sdmode_stack_slot = adjust_address_nv (stack,
16954 SDmode, 0);
16955 return;
16959 /* Check for any SDmode parameters of the function. */
16960 for (t = DECL_ARGUMENTS (cfun->decl); t; t = DECL_CHAIN (t))
16962 if (TREE_TYPE (t) == error_mark_node)
16963 continue;
16965 if (TYPE_MODE (TREE_TYPE (t)) == SDmode
16966 || TYPE_MODE (DECL_ARG_TYPE (t)) == SDmode)
16968 rtx stack = assign_stack_local (DDmode, GET_MODE_SIZE (DDmode), 0);
16969 cfun->machine->sdmode_stack_slot = adjust_address_nv (stack,
16970 SDmode, 0);
16971 return;
16976 static void
16977 rs6000_instantiate_decls (void)
16979 if (cfun->machine->sdmode_stack_slot != NULL_RTX)
16980 instantiate_decl_rtl (cfun->machine->sdmode_stack_slot);
16983 /* Given an rtx X being reloaded into a reg required to be
16984 in class CLASS, return the class of reg to actually use.
16985 In general this is just CLASS; but on some machines
16986 in some cases it is preferable to use a more restrictive class.
16988 On the RS/6000, we have to return NO_REGS when we want to reload a
16989 floating-point CONST_DOUBLE to force it to be copied to memory.
16991 We also don't want to reload integer values into floating-point
16992 registers if we can at all help it. In fact, this can
16993 cause reload to die, if it tries to generate a reload of CTR
16994 into a FP register and discovers it doesn't have the memory location
16995 required.
16997 ??? Would it be a good idea to have reload do the converse, that is
16998 try to reload floating modes into FP registers if possible?
17001 static enum reg_class
17002 rs6000_preferred_reload_class (rtx x, enum reg_class rclass)
17004 enum machine_mode mode = GET_MODE (x);
17006 if (TARGET_VSX && x == CONST0_RTX (mode) && VSX_REG_CLASS_P (rclass))
17007 return rclass;
17009 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (mode)
17010 && (rclass == ALTIVEC_REGS || rclass == VSX_REGS)
17011 && easy_vector_constant (x, mode))
17012 return ALTIVEC_REGS;
17014 if ((CONSTANT_P (x) || GET_CODE (x) == PLUS))
17016 if (reg_class_subset_p (GENERAL_REGS, rclass))
17017 return GENERAL_REGS;
17018 if (reg_class_subset_p (BASE_REGS, rclass))
17019 return BASE_REGS;
17020 return NO_REGS;
17023 if (GET_MODE_CLASS (mode) == MODE_INT && rclass == NON_SPECIAL_REGS)
17024 return GENERAL_REGS;
17026 /* For VSX, prefer the traditional registers for 64-bit values because we can
17027 use the non-VSX loads. Prefer the Altivec registers if Altivec is
17028 handling the vector operations (i.e. V16QI, V8HI, and V4SI), or if we
17029 prefer Altivec loads.. */
17030 if (rclass == VSX_REGS)
17032 if (GET_MODE_SIZE (mode) <= 8)
17033 return FLOAT_REGS;
17035 if (VECTOR_UNIT_ALTIVEC_P (mode) || VECTOR_MEM_ALTIVEC_P (mode)
17036 || mode == V1TImode)
17037 return ALTIVEC_REGS;
17039 return rclass;
17042 return rclass;
17045 /* Debug version of rs6000_preferred_reload_class. */
17046 static enum reg_class
17047 rs6000_debug_preferred_reload_class (rtx x, enum reg_class rclass)
17049 enum reg_class ret = rs6000_preferred_reload_class (x, rclass);
17051 fprintf (stderr,
17052 "\nrs6000_preferred_reload_class, return %s, rclass = %s, "
17053 "mode = %s, x:\n",
17054 reg_class_names[ret], reg_class_names[rclass],
17055 GET_MODE_NAME (GET_MODE (x)));
17056 debug_rtx (x);
17058 return ret;
17061 /* If we are copying between FP or AltiVec registers and anything else, we need
17062 a memory location. The exception is when we are targeting ppc64 and the
17063 move to/from fpr to gpr instructions are available. Also, under VSX, you
17064 can copy vector registers from the FP register set to the Altivec register
17065 set and vice versa. */
17067 static bool
17068 rs6000_secondary_memory_needed (enum reg_class from_class,
17069 enum reg_class to_class,
17070 enum machine_mode mode)
17072 enum rs6000_reg_type from_type, to_type;
17073 bool altivec_p = ((from_class == ALTIVEC_REGS)
17074 || (to_class == ALTIVEC_REGS));
17076 /* If a simple/direct move is available, we don't need secondary memory */
17077 from_type = reg_class_to_reg_type[(int)from_class];
17078 to_type = reg_class_to_reg_type[(int)to_class];
17080 if (rs6000_secondary_reload_move (to_type, from_type, mode,
17081 (secondary_reload_info *)0, altivec_p))
17082 return false;
17084 /* If we have a floating point or vector register class, we need to use
17085 memory to transfer the data. */
17086 if (IS_FP_VECT_REG_TYPE (from_type) || IS_FP_VECT_REG_TYPE (to_type))
17087 return true;
17089 return false;
17092 /* Debug version of rs6000_secondary_memory_needed. */
17093 static bool
17094 rs6000_debug_secondary_memory_needed (enum reg_class from_class,
17095 enum reg_class to_class,
17096 enum machine_mode mode)
17098 bool ret = rs6000_secondary_memory_needed (from_class, to_class, mode);
17100 fprintf (stderr,
17101 "rs6000_secondary_memory_needed, return: %s, from_class = %s, "
17102 "to_class = %s, mode = %s\n",
17103 ret ? "true" : "false",
17104 reg_class_names[from_class],
17105 reg_class_names[to_class],
17106 GET_MODE_NAME (mode));
17108 return ret;
17111 /* Return the register class of a scratch register needed to copy IN into
17112 or out of a register in RCLASS in MODE. If it can be done directly,
17113 NO_REGS is returned. */
17115 static enum reg_class
17116 rs6000_secondary_reload_class (enum reg_class rclass, enum machine_mode mode,
17117 rtx in)
17119 int regno;
17121 if (TARGET_ELF || (DEFAULT_ABI == ABI_DARWIN
17122 #if TARGET_MACHO
17123 && MACHOPIC_INDIRECT
17124 #endif
17127 /* We cannot copy a symbolic operand directly into anything
17128 other than BASE_REGS for TARGET_ELF. So indicate that a
17129 register from BASE_REGS is needed as an intermediate
17130 register.
17132 On Darwin, pic addresses require a load from memory, which
17133 needs a base register. */
17134 if (rclass != BASE_REGS
17135 && (GET_CODE (in) == SYMBOL_REF
17136 || GET_CODE (in) == HIGH
17137 || GET_CODE (in) == LABEL_REF
17138 || GET_CODE (in) == CONST))
17139 return BASE_REGS;
17142 if (GET_CODE (in) == REG)
17144 regno = REGNO (in);
17145 if (regno >= FIRST_PSEUDO_REGISTER)
17147 regno = true_regnum (in);
17148 if (regno >= FIRST_PSEUDO_REGISTER)
17149 regno = -1;
17152 else if (GET_CODE (in) == SUBREG)
17154 regno = true_regnum (in);
17155 if (regno >= FIRST_PSEUDO_REGISTER)
17156 regno = -1;
17158 else
17159 regno = -1;
17161 /* We can place anything into GENERAL_REGS and can put GENERAL_REGS
17162 into anything. */
17163 if (rclass == GENERAL_REGS || rclass == BASE_REGS
17164 || (regno >= 0 && INT_REGNO_P (regno)))
17165 return NO_REGS;
17167 /* Constants, memory, and FP registers can go into FP registers. */
17168 if ((regno == -1 || FP_REGNO_P (regno))
17169 && (rclass == FLOAT_REGS || rclass == NON_SPECIAL_REGS))
17170 return (mode != SDmode || lra_in_progress) ? NO_REGS : GENERAL_REGS;
17172 /* Memory, and FP/altivec registers can go into fp/altivec registers under
17173 VSX. However, for scalar variables, use the traditional floating point
17174 registers so that we can use offset+register addressing. */
17175 if (TARGET_VSX
17176 && (regno == -1 || VSX_REGNO_P (regno))
17177 && VSX_REG_CLASS_P (rclass))
17179 if (GET_MODE_SIZE (mode) < 16)
17180 return FLOAT_REGS;
17182 return NO_REGS;
17185 /* Memory, and AltiVec registers can go into AltiVec registers. */
17186 if ((regno == -1 || ALTIVEC_REGNO_P (regno))
17187 && rclass == ALTIVEC_REGS)
17188 return NO_REGS;
17190 /* We can copy among the CR registers. */
17191 if ((rclass == CR_REGS || rclass == CR0_REGS)
17192 && regno >= 0 && CR_REGNO_P (regno))
17193 return NO_REGS;
17195 /* Otherwise, we need GENERAL_REGS. */
17196 return GENERAL_REGS;
17199 /* Debug version of rs6000_secondary_reload_class. */
17200 static enum reg_class
17201 rs6000_debug_secondary_reload_class (enum reg_class rclass,
17202 enum machine_mode mode, rtx in)
17204 enum reg_class ret = rs6000_secondary_reload_class (rclass, mode, in);
17205 fprintf (stderr,
17206 "\nrs6000_secondary_reload_class, return %s, rclass = %s, "
17207 "mode = %s, input rtx:\n",
17208 reg_class_names[ret], reg_class_names[rclass],
17209 GET_MODE_NAME (mode));
17210 debug_rtx (in);
17212 return ret;
17215 /* Return nonzero if for CLASS a mode change from FROM to TO is invalid. */
17217 static bool
17218 rs6000_cannot_change_mode_class (enum machine_mode from,
17219 enum machine_mode to,
17220 enum reg_class rclass)
17222 unsigned from_size = GET_MODE_SIZE (from);
17223 unsigned to_size = GET_MODE_SIZE (to);
17225 if (from_size != to_size)
17227 enum reg_class xclass = (TARGET_VSX) ? VSX_REGS : FLOAT_REGS;
17229 if (reg_classes_intersect_p (xclass, rclass))
17231 unsigned to_nregs = hard_regno_nregs[FIRST_FPR_REGNO][to];
17232 unsigned from_nregs = hard_regno_nregs[FIRST_FPR_REGNO][from];
17234 /* Don't allow 64-bit types to overlap with 128-bit types that take a
17235 single register under VSX because the scalar part of the register
17236 is in the upper 64-bits, and not the lower 64-bits. Types like
17237 TFmode/TDmode that take 2 scalar register can overlap. 128-bit
17238 IEEE floating point can't overlap, and neither can small
17239 values. */
17241 if (TARGET_IEEEQUAD && (to == TFmode || from == TFmode))
17242 return true;
17244 /* TDmode in floating-mode registers must always go into a register
17245 pair with the most significant word in the even-numbered register
17246 to match ISA requirements. In little-endian mode, this does not
17247 match subreg numbering, so we cannot allow subregs. */
17248 if (!BYTES_BIG_ENDIAN && (to == TDmode || from == TDmode))
17249 return true;
17251 if (from_size < 8 || to_size < 8)
17252 return true;
17254 if (from_size == 8 && (8 * to_nregs) != to_size)
17255 return true;
17257 if (to_size == 8 && (8 * from_nregs) != from_size)
17258 return true;
17260 return false;
17262 else
17263 return false;
17266 if (TARGET_E500_DOUBLE
17267 && ((((to) == DFmode) + ((from) == DFmode)) == 1
17268 || (((to) == TFmode) + ((from) == TFmode)) == 1
17269 || (((to) == DDmode) + ((from) == DDmode)) == 1
17270 || (((to) == TDmode) + ((from) == TDmode)) == 1
17271 || (((to) == DImode) + ((from) == DImode)) == 1))
17272 return true;
17274 /* Since the VSX register set includes traditional floating point registers
17275 and altivec registers, just check for the size being different instead of
17276 trying to check whether the modes are vector modes. Otherwise it won't
17277 allow say DF and DI to change classes. For types like TFmode and TDmode
17278 that take 2 64-bit registers, rather than a single 128-bit register, don't
17279 allow subregs of those types to other 128 bit types. */
17280 if (TARGET_VSX && VSX_REG_CLASS_P (rclass))
17282 unsigned num_regs = (from_size + 15) / 16;
17283 if (hard_regno_nregs[FIRST_FPR_REGNO][to] > num_regs
17284 || hard_regno_nregs[FIRST_FPR_REGNO][from] > num_regs)
17285 return true;
17287 return (from_size != 8 && from_size != 16);
17290 if (TARGET_ALTIVEC && rclass == ALTIVEC_REGS
17291 && (ALTIVEC_VECTOR_MODE (from) + ALTIVEC_VECTOR_MODE (to)) == 1)
17292 return true;
17294 if (TARGET_SPE && (SPE_VECTOR_MODE (from) + SPE_VECTOR_MODE (to)) == 1
17295 && reg_classes_intersect_p (GENERAL_REGS, rclass))
17296 return true;
17298 return false;
17301 /* Debug version of rs6000_cannot_change_mode_class. */
17302 static bool
17303 rs6000_debug_cannot_change_mode_class (enum machine_mode from,
17304 enum machine_mode to,
17305 enum reg_class rclass)
17307 bool ret = rs6000_cannot_change_mode_class (from, to, rclass);
17309 fprintf (stderr,
17310 "rs6000_cannot_change_mode_class, return %s, from = %s, "
17311 "to = %s, rclass = %s\n",
17312 ret ? "true" : "false",
17313 GET_MODE_NAME (from), GET_MODE_NAME (to),
17314 reg_class_names[rclass]);
17316 return ret;
17319 /* Return a string to do a move operation of 128 bits of data. */
17321 const char *
17322 rs6000_output_move_128bit (rtx operands[])
17324 rtx dest = operands[0];
17325 rtx src = operands[1];
17326 enum machine_mode mode = GET_MODE (dest);
17327 int dest_regno;
17328 int src_regno;
17329 bool dest_gpr_p, dest_fp_p, dest_vmx_p, dest_vsx_p;
17330 bool src_gpr_p, src_fp_p, src_vmx_p, src_vsx_p;
17332 if (REG_P (dest))
17334 dest_regno = REGNO (dest);
17335 dest_gpr_p = INT_REGNO_P (dest_regno);
17336 dest_fp_p = FP_REGNO_P (dest_regno);
17337 dest_vmx_p = ALTIVEC_REGNO_P (dest_regno);
17338 dest_vsx_p = dest_fp_p | dest_vmx_p;
17340 else
17342 dest_regno = -1;
17343 dest_gpr_p = dest_fp_p = dest_vmx_p = dest_vsx_p = false;
17346 if (REG_P (src))
17348 src_regno = REGNO (src);
17349 src_gpr_p = INT_REGNO_P (src_regno);
17350 src_fp_p = FP_REGNO_P (src_regno);
17351 src_vmx_p = ALTIVEC_REGNO_P (src_regno);
17352 src_vsx_p = src_fp_p | src_vmx_p;
17354 else
17356 src_regno = -1;
17357 src_gpr_p = src_fp_p = src_vmx_p = src_vsx_p = false;
17360 /* Register moves. */
17361 if (dest_regno >= 0 && src_regno >= 0)
17363 if (dest_gpr_p)
17365 if (src_gpr_p)
17366 return "#";
17368 else if (TARGET_VSX && TARGET_DIRECT_MOVE && src_vsx_p)
17369 return "#";
17372 else if (TARGET_VSX && dest_vsx_p)
17374 if (src_vsx_p)
17375 return "xxlor %x0,%x1,%x1";
17377 else if (TARGET_DIRECT_MOVE && src_gpr_p)
17378 return "#";
17381 else if (TARGET_ALTIVEC && dest_vmx_p && src_vmx_p)
17382 return "vor %0,%1,%1";
17384 else if (dest_fp_p && src_fp_p)
17385 return "#";
17388 /* Loads. */
17389 else if (dest_regno >= 0 && MEM_P (src))
17391 if (dest_gpr_p)
17393 if (TARGET_QUAD_MEMORY && quad_load_store_p (dest, src))
17394 return "lq %0,%1";
17395 else
17396 return "#";
17399 else if (TARGET_ALTIVEC && dest_vmx_p
17400 && altivec_indexed_or_indirect_operand (src, mode))
17401 return "lvx %0,%y1";
17403 else if (TARGET_VSX && dest_vsx_p)
17405 if (mode == V16QImode || mode == V8HImode || mode == V4SImode)
17406 return "lxvw4x %x0,%y1";
17407 else
17408 return "lxvd2x %x0,%y1";
17411 else if (TARGET_ALTIVEC && dest_vmx_p)
17412 return "lvx %0,%y1";
17414 else if (dest_fp_p)
17415 return "#";
17418 /* Stores. */
17419 else if (src_regno >= 0 && MEM_P (dest))
17421 if (src_gpr_p)
17423 if (TARGET_QUAD_MEMORY && quad_load_store_p (dest, src))
17424 return "stq %1,%0";
17425 else
17426 return "#";
17429 else if (TARGET_ALTIVEC && src_vmx_p
17430 && altivec_indexed_or_indirect_operand (src, mode))
17431 return "stvx %1,%y0";
17433 else if (TARGET_VSX && src_vsx_p)
17435 if (mode == V16QImode || mode == V8HImode || mode == V4SImode)
17436 return "stxvw4x %x1,%y0";
17437 else
17438 return "stxvd2x %x1,%y0";
17441 else if (TARGET_ALTIVEC && src_vmx_p)
17442 return "stvx %1,%y0";
17444 else if (src_fp_p)
17445 return "#";
17448 /* Constants. */
17449 else if (dest_regno >= 0
17450 && (GET_CODE (src) == CONST_INT
17451 || GET_CODE (src) == CONST_WIDE_INT
17452 || GET_CODE (src) == CONST_DOUBLE
17453 || GET_CODE (src) == CONST_VECTOR))
17455 if (dest_gpr_p)
17456 return "#";
17458 else if (TARGET_VSX && dest_vsx_p && zero_constant (src, mode))
17459 return "xxlxor %x0,%x0,%x0";
17461 else if (TARGET_ALTIVEC && dest_vmx_p)
17462 return output_vec_const_move (operands);
17465 if (TARGET_DEBUG_ADDR)
17467 fprintf (stderr, "\n===== Bad 128 bit move:\n");
17468 debug_rtx (gen_rtx_SET (VOIDmode, dest, src));
17471 gcc_unreachable ();
17474 /* Validate a 128-bit move. */
17475 bool
17476 rs6000_move_128bit_ok_p (rtx operands[])
17478 enum machine_mode mode = GET_MODE (operands[0]);
17479 return (gpc_reg_operand (operands[0], mode)
17480 || gpc_reg_operand (operands[1], mode));
17483 /* Return true if a 128-bit move needs to be split. */
17484 bool
17485 rs6000_split_128bit_ok_p (rtx operands[])
17487 if (!reload_completed)
17488 return false;
17490 if (!gpr_or_gpr_p (operands[0], operands[1]))
17491 return false;
17493 if (quad_load_store_p (operands[0], operands[1]))
17494 return false;
17496 return true;
17500 /* Given a comparison operation, return the bit number in CCR to test. We
17501 know this is a valid comparison.
17503 SCC_P is 1 if this is for an scc. That means that %D will have been
17504 used instead of %C, so the bits will be in different places.
17506 Return -1 if OP isn't a valid comparison for some reason. */
17509 ccr_bit (rtx op, int scc_p)
17511 enum rtx_code code = GET_CODE (op);
17512 enum machine_mode cc_mode;
17513 int cc_regnum;
17514 int base_bit;
17515 rtx reg;
17517 if (!COMPARISON_P (op))
17518 return -1;
17520 reg = XEXP (op, 0);
17522 gcc_assert (GET_CODE (reg) == REG && CR_REGNO_P (REGNO (reg)));
17524 cc_mode = GET_MODE (reg);
17525 cc_regnum = REGNO (reg);
17526 base_bit = 4 * (cc_regnum - CR0_REGNO);
17528 validate_condition_mode (code, cc_mode);
17530 /* When generating a sCOND operation, only positive conditions are
17531 allowed. */
17532 gcc_assert (!scc_p
17533 || code == EQ || code == GT || code == LT || code == UNORDERED
17534 || code == GTU || code == LTU);
17536 switch (code)
17538 case NE:
17539 return scc_p ? base_bit + 3 : base_bit + 2;
17540 case EQ:
17541 return base_bit + 2;
17542 case GT: case GTU: case UNLE:
17543 return base_bit + 1;
17544 case LT: case LTU: case UNGE:
17545 return base_bit;
17546 case ORDERED: case UNORDERED:
17547 return base_bit + 3;
17549 case GE: case GEU:
17550 /* If scc, we will have done a cror to put the bit in the
17551 unordered position. So test that bit. For integer, this is ! LT
17552 unless this is an scc insn. */
17553 return scc_p ? base_bit + 3 : base_bit;
17555 case LE: case LEU:
17556 return scc_p ? base_bit + 3 : base_bit + 1;
17558 default:
17559 gcc_unreachable ();
17563 /* Return the GOT register. */
17566 rs6000_got_register (rtx value ATTRIBUTE_UNUSED)
17568 /* The second flow pass currently (June 1999) can't update
17569 regs_ever_live without disturbing other parts of the compiler, so
17570 update it here to make the prolog/epilogue code happy. */
17571 if (!can_create_pseudo_p ()
17572 && !df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))
17573 df_set_regs_ever_live (RS6000_PIC_OFFSET_TABLE_REGNUM, true);
17575 crtl->uses_pic_offset_table = 1;
17577 return pic_offset_table_rtx;
17580 static rs6000_stack_t stack_info;
17582 /* Function to init struct machine_function.
17583 This will be called, via a pointer variable,
17584 from push_function_context. */
17586 static struct machine_function *
17587 rs6000_init_machine_status (void)
17589 stack_info.reload_completed = 0;
17590 return ggc_alloc_cleared_machine_function ();
17593 #define INT_P(X) (GET_CODE (X) == CONST_INT && GET_MODE (X) == VOIDmode)
17596 extract_MB (rtx op)
17598 int i;
17599 unsigned long val = INTVAL (op);
17601 /* If the high bit is zero, the value is the first 1 bit we find
17602 from the left. */
17603 if ((val & 0x80000000) == 0)
17605 gcc_assert (val & 0xffffffff);
17607 i = 1;
17608 while (((val <<= 1) & 0x80000000) == 0)
17609 ++i;
17610 return i;
17613 /* If the high bit is set and the low bit is not, or the mask is all
17614 1's, the value is zero. */
17615 if ((val & 1) == 0 || (val & 0xffffffff) == 0xffffffff)
17616 return 0;
17618 /* Otherwise we have a wrap-around mask. Look for the first 0 bit
17619 from the right. */
17620 i = 31;
17621 while (((val >>= 1) & 1) != 0)
17622 --i;
17624 return i;
17628 extract_ME (rtx op)
17630 int i;
17631 unsigned long val = INTVAL (op);
17633 /* If the low bit is zero, the value is the first 1 bit we find from
17634 the right. */
17635 if ((val & 1) == 0)
17637 gcc_assert (val & 0xffffffff);
17639 i = 30;
17640 while (((val >>= 1) & 1) == 0)
17641 --i;
17643 return i;
17646 /* If the low bit is set and the high bit is not, or the mask is all
17647 1's, the value is 31. */
17648 if ((val & 0x80000000) == 0 || (val & 0xffffffff) == 0xffffffff)
17649 return 31;
17651 /* Otherwise we have a wrap-around mask. Look for the first 0 bit
17652 from the left. */
17653 i = 0;
17654 while (((val <<= 1) & 0x80000000) != 0)
17655 ++i;
17657 return i;
17660 /* Locate some local-dynamic symbol still in use by this function
17661 so that we can print its name in some tls_ld pattern. */
17663 static const char *
17664 rs6000_get_some_local_dynamic_name (void)
17666 rtx insn;
17668 if (cfun->machine->some_ld_name)
17669 return cfun->machine->some_ld_name;
17671 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
17672 if (INSN_P (insn)
17673 && for_each_rtx (&PATTERN (insn),
17674 rs6000_get_some_local_dynamic_name_1, 0))
17675 return cfun->machine->some_ld_name;
17677 gcc_unreachable ();
17680 /* Helper function for rs6000_get_some_local_dynamic_name. */
17682 static int
17683 rs6000_get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
17685 rtx x = *px;
17687 if (GET_CODE (x) == SYMBOL_REF)
17689 const char *str = XSTR (x, 0);
17690 if (SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC)
17692 cfun->machine->some_ld_name = str;
17693 return 1;
17697 return 0;
17700 /* Write out a function code label. */
17702 void
17703 rs6000_output_function_entry (FILE *file, const char *fname)
17705 if (fname[0] != '.')
17707 switch (DEFAULT_ABI)
17709 default:
17710 gcc_unreachable ();
17712 case ABI_AIX:
17713 if (DOT_SYMBOLS)
17714 putc ('.', file);
17715 else
17716 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "L.");
17717 break;
17719 case ABI_ELFv2:
17720 case ABI_V4:
17721 case ABI_DARWIN:
17722 break;
17726 RS6000_OUTPUT_BASENAME (file, fname);
17729 /* Print an operand. Recognize special options, documented below. */
17731 #if TARGET_ELF
17732 #define SMALL_DATA_RELOC ((rs6000_sdata == SDATA_EABI) ? "sda21" : "sdarel")
17733 #define SMALL_DATA_REG ((rs6000_sdata == SDATA_EABI) ? 0 : 13)
17734 #else
17735 #define SMALL_DATA_RELOC "sda21"
17736 #define SMALL_DATA_REG 0
17737 #endif
17739 void
17740 print_operand (FILE *file, rtx x, int code)
17742 int i;
17743 unsigned HOST_WIDE_INT uval;
17745 switch (code)
17747 /* %a is output_address. */
17749 case 'b':
17750 /* If constant, low-order 16 bits of constant, unsigned.
17751 Otherwise, write normally. */
17752 if (INT_P (x))
17753 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0xffff);
17754 else
17755 print_operand (file, x, 0);
17756 return;
17758 case 'B':
17759 /* If the low-order bit is zero, write 'r'; otherwise, write 'l'
17760 for 64-bit mask direction. */
17761 putc (((INTVAL (x) & 1) == 0 ? 'r' : 'l'), file);
17762 return;
17764 /* %c is output_addr_const if a CONSTANT_ADDRESS_P, otherwise
17765 output_operand. */
17767 case 'D':
17768 /* Like 'J' but get to the GT bit only. */
17769 gcc_assert (REG_P (x));
17771 /* Bit 1 is GT bit. */
17772 i = 4 * (REGNO (x) - CR0_REGNO) + 1;
17774 /* Add one for shift count in rlinm for scc. */
17775 fprintf (file, "%d", i + 1);
17776 return;
17778 case 'E':
17779 /* X is a CR register. Print the number of the EQ bit of the CR */
17780 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
17781 output_operand_lossage ("invalid %%E value");
17782 else
17783 fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO) + 2);
17784 return;
17786 case 'f':
17787 /* X is a CR register. Print the shift count needed to move it
17788 to the high-order four bits. */
17789 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
17790 output_operand_lossage ("invalid %%f value");
17791 else
17792 fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO));
17793 return;
17795 case 'F':
17796 /* Similar, but print the count for the rotate in the opposite
17797 direction. */
17798 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
17799 output_operand_lossage ("invalid %%F value");
17800 else
17801 fprintf (file, "%d", 32 - 4 * (REGNO (x) - CR0_REGNO));
17802 return;
17804 case 'G':
17805 /* X is a constant integer. If it is negative, print "m",
17806 otherwise print "z". This is to make an aze or ame insn. */
17807 if (GET_CODE (x) != CONST_INT)
17808 output_operand_lossage ("invalid %%G value");
17809 else if (INTVAL (x) >= 0)
17810 putc ('z', file);
17811 else
17812 putc ('m', file);
17813 return;
17815 case 'h':
17816 /* If constant, output low-order five bits. Otherwise, write
17817 normally. */
17818 if (INT_P (x))
17819 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 31);
17820 else
17821 print_operand (file, x, 0);
17822 return;
17824 case 'H':
17825 /* If constant, output low-order six bits. Otherwise, write
17826 normally. */
17827 if (INT_P (x))
17828 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 63);
17829 else
17830 print_operand (file, x, 0);
17831 return;
17833 case 'I':
17834 /* Print `i' if this is a constant, else nothing. */
17835 if (INT_P (x))
17836 putc ('i', file);
17837 return;
17839 case 'j':
17840 /* Write the bit number in CCR for jump. */
17841 i = ccr_bit (x, 0);
17842 if (i == -1)
17843 output_operand_lossage ("invalid %%j code");
17844 else
17845 fprintf (file, "%d", i);
17846 return;
17848 case 'J':
17849 /* Similar, but add one for shift count in rlinm for scc and pass
17850 scc flag to `ccr_bit'. */
17851 i = ccr_bit (x, 1);
17852 if (i == -1)
17853 output_operand_lossage ("invalid %%J code");
17854 else
17855 /* If we want bit 31, write a shift count of zero, not 32. */
17856 fprintf (file, "%d", i == 31 ? 0 : i + 1);
17857 return;
17859 case 'k':
17860 /* X must be a constant. Write the 1's complement of the
17861 constant. */
17862 if (! INT_P (x))
17863 output_operand_lossage ("invalid %%k value");
17864 else
17865 fprintf (file, HOST_WIDE_INT_PRINT_DEC, ~ INTVAL (x));
17866 return;
17868 case 'K':
17869 /* X must be a symbolic constant on ELF. Write an
17870 expression suitable for an 'addi' that adds in the low 16
17871 bits of the MEM. */
17872 if (GET_CODE (x) == CONST)
17874 if (GET_CODE (XEXP (x, 0)) != PLUS
17875 || (GET_CODE (XEXP (XEXP (x, 0), 0)) != SYMBOL_REF
17876 && GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
17877 || GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
17878 output_operand_lossage ("invalid %%K value");
17880 print_operand_address (file, x);
17881 fputs ("@l", file);
17882 return;
17884 /* %l is output_asm_label. */
17886 case 'L':
17887 /* Write second word of DImode or DFmode reference. Works on register
17888 or non-indexed memory only. */
17889 if (REG_P (x))
17890 fputs (reg_names[REGNO (x) + 1], file);
17891 else if (MEM_P (x))
17893 /* Handle possible auto-increment. Since it is pre-increment and
17894 we have already done it, we can just use an offset of word. */
17895 if (GET_CODE (XEXP (x, 0)) == PRE_INC
17896 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
17897 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0),
17898 UNITS_PER_WORD));
17899 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
17900 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0),
17901 UNITS_PER_WORD));
17902 else
17903 output_address (XEXP (adjust_address_nv (x, SImode,
17904 UNITS_PER_WORD),
17905 0));
17907 if (small_data_operand (x, GET_MODE (x)))
17908 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
17909 reg_names[SMALL_DATA_REG]);
17911 return;
17913 case 'm':
17914 /* MB value for a mask operand. */
17915 if (! mask_operand (x, SImode))
17916 output_operand_lossage ("invalid %%m value");
17918 fprintf (file, "%d", extract_MB (x));
17919 return;
17921 case 'M':
17922 /* ME value for a mask operand. */
17923 if (! mask_operand (x, SImode))
17924 output_operand_lossage ("invalid %%M value");
17926 fprintf (file, "%d", extract_ME (x));
17927 return;
17929 /* %n outputs the negative of its operand. */
17931 case 'N':
17932 /* Write the number of elements in the vector times 4. */
17933 if (GET_CODE (x) != PARALLEL)
17934 output_operand_lossage ("invalid %%N value");
17935 else
17936 fprintf (file, "%d", XVECLEN (x, 0) * 4);
17937 return;
17939 case 'O':
17940 /* Similar, but subtract 1 first. */
17941 if (GET_CODE (x) != PARALLEL)
17942 output_operand_lossage ("invalid %%O value");
17943 else
17944 fprintf (file, "%d", (XVECLEN (x, 0) - 1) * 4);
17945 return;
17947 case 'p':
17948 /* X is a CONST_INT that is a power of two. Output the logarithm. */
17949 if (! INT_P (x)
17950 || INTVAL (x) < 0
17951 || (i = exact_log2 (INTVAL (x))) < 0)
17952 output_operand_lossage ("invalid %%p value");
17953 else
17954 fprintf (file, "%d", i);
17955 return;
17957 case 'P':
17958 /* The operand must be an indirect memory reference. The result
17959 is the register name. */
17960 if (GET_CODE (x) != MEM || GET_CODE (XEXP (x, 0)) != REG
17961 || REGNO (XEXP (x, 0)) >= 32)
17962 output_operand_lossage ("invalid %%P value");
17963 else
17964 fputs (reg_names[REGNO (XEXP (x, 0))], file);
17965 return;
17967 case 'q':
17968 /* This outputs the logical code corresponding to a boolean
17969 expression. The expression may have one or both operands
17970 negated (if one, only the first one). For condition register
17971 logical operations, it will also treat the negated
17972 CR codes as NOTs, but not handle NOTs of them. */
17974 const char *const *t = 0;
17975 const char *s;
17976 enum rtx_code code = GET_CODE (x);
17977 static const char * const tbl[3][3] = {
17978 { "and", "andc", "nor" },
17979 { "or", "orc", "nand" },
17980 { "xor", "eqv", "xor" } };
17982 if (code == AND)
17983 t = tbl[0];
17984 else if (code == IOR)
17985 t = tbl[1];
17986 else if (code == XOR)
17987 t = tbl[2];
17988 else
17989 output_operand_lossage ("invalid %%q value");
17991 if (GET_CODE (XEXP (x, 0)) != NOT)
17992 s = t[0];
17993 else
17995 if (GET_CODE (XEXP (x, 1)) == NOT)
17996 s = t[2];
17997 else
17998 s = t[1];
18001 fputs (s, file);
18003 return;
18005 case 'Q':
18006 if (! TARGET_MFCRF)
18007 return;
18008 fputc (',', file);
18009 /* FALLTHRU */
18011 case 'R':
18012 /* X is a CR register. Print the mask for `mtcrf'. */
18013 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
18014 output_operand_lossage ("invalid %%R value");
18015 else
18016 fprintf (file, "%d", 128 >> (REGNO (x) - CR0_REGNO));
18017 return;
18019 case 's':
18020 /* Low 5 bits of 32 - value */
18021 if (! INT_P (x))
18022 output_operand_lossage ("invalid %%s value");
18023 else
18024 fprintf (file, HOST_WIDE_INT_PRINT_DEC, (32 - INTVAL (x)) & 31);
18025 return;
18027 case 'S':
18028 /* PowerPC64 mask position. All 0's is excluded.
18029 CONST_INT 32-bit mask is considered sign-extended so any
18030 transition must occur within the CONST_INT, not on the boundary. */
18031 if (! mask64_operand (x, DImode))
18032 output_operand_lossage ("invalid %%S value");
18034 uval = INTVAL (x);
18036 if (uval & 1) /* Clear Left */
18038 #if HOST_BITS_PER_WIDE_INT > 64
18039 uval &= ((unsigned HOST_WIDE_INT) 1 << 64) - 1;
18040 #endif
18041 i = 64;
18043 else /* Clear Right */
18045 uval = ~uval;
18046 #if HOST_BITS_PER_WIDE_INT > 64
18047 uval &= ((unsigned HOST_WIDE_INT) 1 << 64) - 1;
18048 #endif
18049 i = 63;
18051 while (uval != 0)
18052 --i, uval >>= 1;
18053 gcc_assert (i >= 0);
18054 fprintf (file, "%d", i);
18055 return;
18057 case 't':
18058 /* Like 'J' but get to the OVERFLOW/UNORDERED bit. */
18059 gcc_assert (REG_P (x) && GET_MODE (x) == CCmode);
18061 /* Bit 3 is OV bit. */
18062 i = 4 * (REGNO (x) - CR0_REGNO) + 3;
18064 /* If we want bit 31, write a shift count of zero, not 32. */
18065 fprintf (file, "%d", i == 31 ? 0 : i + 1);
18066 return;
18068 case 'T':
18069 /* Print the symbolic name of a branch target register. */
18070 if (GET_CODE (x) != REG || (REGNO (x) != LR_REGNO
18071 && REGNO (x) != CTR_REGNO))
18072 output_operand_lossage ("invalid %%T value");
18073 else if (REGNO (x) == LR_REGNO)
18074 fputs ("lr", file);
18075 else
18076 fputs ("ctr", file);
18077 return;
18079 case 'u':
18080 /* High-order 16 bits of constant for use in unsigned operand. */
18081 if (! INT_P (x))
18082 output_operand_lossage ("invalid %%u value");
18083 else
18084 fprintf (file, HOST_WIDE_INT_PRINT_HEX,
18085 (INTVAL (x) >> 16) & 0xffff);
18086 return;
18088 case 'v':
18089 /* High-order 16 bits of constant for use in signed operand. */
18090 if (! INT_P (x))
18091 output_operand_lossage ("invalid %%v value");
18092 else
18093 fprintf (file, HOST_WIDE_INT_PRINT_HEX,
18094 (INTVAL (x) >> 16) & 0xffff);
18095 return;
18097 case 'U':
18098 /* Print `u' if this has an auto-increment or auto-decrement. */
18099 if (MEM_P (x)
18100 && (GET_CODE (XEXP (x, 0)) == PRE_INC
18101 || GET_CODE (XEXP (x, 0)) == PRE_DEC
18102 || GET_CODE (XEXP (x, 0)) == PRE_MODIFY))
18103 putc ('u', file);
18104 return;
18106 case 'V':
18107 /* Print the trap code for this operand. */
18108 switch (GET_CODE (x))
18110 case EQ:
18111 fputs ("eq", file); /* 4 */
18112 break;
18113 case NE:
18114 fputs ("ne", file); /* 24 */
18115 break;
18116 case LT:
18117 fputs ("lt", file); /* 16 */
18118 break;
18119 case LE:
18120 fputs ("le", file); /* 20 */
18121 break;
18122 case GT:
18123 fputs ("gt", file); /* 8 */
18124 break;
18125 case GE:
18126 fputs ("ge", file); /* 12 */
18127 break;
18128 case LTU:
18129 fputs ("llt", file); /* 2 */
18130 break;
18131 case LEU:
18132 fputs ("lle", file); /* 6 */
18133 break;
18134 case GTU:
18135 fputs ("lgt", file); /* 1 */
18136 break;
18137 case GEU:
18138 fputs ("lge", file); /* 5 */
18139 break;
18140 default:
18141 gcc_unreachable ();
18143 break;
18145 case 'w':
18146 /* If constant, low-order 16 bits of constant, signed. Otherwise, write
18147 normally. */
18148 if (INT_P (x))
18149 fprintf (file, HOST_WIDE_INT_PRINT_DEC,
18150 ((INTVAL (x) & 0xffff) ^ 0x8000) - 0x8000);
18151 else
18152 print_operand (file, x, 0);
18153 return;
18155 case 'W':
18156 /* MB value for a PowerPC64 rldic operand. */
18157 i = clz_hwi (INTVAL (x));
18159 fprintf (file, "%d", i);
18160 return;
18162 case 'x':
18163 /* X is a FPR or Altivec register used in a VSX context. */
18164 if (GET_CODE (x) != REG || !VSX_REGNO_P (REGNO (x)))
18165 output_operand_lossage ("invalid %%x value");
18166 else
18168 int reg = REGNO (x);
18169 int vsx_reg = (FP_REGNO_P (reg)
18170 ? reg - 32
18171 : reg - FIRST_ALTIVEC_REGNO + 32);
18173 #ifdef TARGET_REGNAMES
18174 if (TARGET_REGNAMES)
18175 fprintf (file, "%%vs%d", vsx_reg);
18176 else
18177 #endif
18178 fprintf (file, "%d", vsx_reg);
18180 return;
18182 case 'X':
18183 if (MEM_P (x)
18184 && (legitimate_indexed_address_p (XEXP (x, 0), 0)
18185 || (GET_CODE (XEXP (x, 0)) == PRE_MODIFY
18186 && legitimate_indexed_address_p (XEXP (XEXP (x, 0), 1), 0))))
18187 putc ('x', file);
18188 return;
18190 case 'Y':
18191 /* Like 'L', for third word of TImode/PTImode */
18192 if (REG_P (x))
18193 fputs (reg_names[REGNO (x) + 2], file);
18194 else if (MEM_P (x))
18196 if (GET_CODE (XEXP (x, 0)) == PRE_INC
18197 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
18198 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 8));
18199 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
18200 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 8));
18201 else
18202 output_address (XEXP (adjust_address_nv (x, SImode, 8), 0));
18203 if (small_data_operand (x, GET_MODE (x)))
18204 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
18205 reg_names[SMALL_DATA_REG]);
18207 return;
18209 case 'z':
18210 /* X is a SYMBOL_REF. Write out the name preceded by a
18211 period and without any trailing data in brackets. Used for function
18212 names. If we are configured for System V (or the embedded ABI) on
18213 the PowerPC, do not emit the period, since those systems do not use
18214 TOCs and the like. */
18215 gcc_assert (GET_CODE (x) == SYMBOL_REF);
18217 /* For macho, check to see if we need a stub. */
18218 if (TARGET_MACHO)
18220 const char *name = XSTR (x, 0);
18221 #if TARGET_MACHO
18222 if (darwin_emit_branch_islands
18223 && MACHOPIC_INDIRECT
18224 && machopic_classify_symbol (x) == MACHOPIC_UNDEFINED_FUNCTION)
18225 name = machopic_indirection_name (x, /*stub_p=*/true);
18226 #endif
18227 assemble_name (file, name);
18229 else if (!DOT_SYMBOLS)
18230 assemble_name (file, XSTR (x, 0));
18231 else
18232 rs6000_output_function_entry (file, XSTR (x, 0));
18233 return;
18235 case 'Z':
18236 /* Like 'L', for last word of TImode/PTImode. */
18237 if (REG_P (x))
18238 fputs (reg_names[REGNO (x) + 3], file);
18239 else if (MEM_P (x))
18241 if (GET_CODE (XEXP (x, 0)) == PRE_INC
18242 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
18243 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 12));
18244 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
18245 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 12));
18246 else
18247 output_address (XEXP (adjust_address_nv (x, SImode, 12), 0));
18248 if (small_data_operand (x, GET_MODE (x)))
18249 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
18250 reg_names[SMALL_DATA_REG]);
18252 return;
18254 /* Print AltiVec or SPE memory operand. */
18255 case 'y':
18257 rtx tmp;
18259 gcc_assert (MEM_P (x));
18261 tmp = XEXP (x, 0);
18263 /* Ugly hack because %y is overloaded. */
18264 if ((TARGET_SPE || TARGET_E500_DOUBLE)
18265 && (GET_MODE_SIZE (GET_MODE (x)) == 8
18266 || GET_MODE (x) == TFmode
18267 || GET_MODE (x) == TImode
18268 || GET_MODE (x) == PTImode))
18270 /* Handle [reg]. */
18271 if (REG_P (tmp))
18273 fprintf (file, "0(%s)", reg_names[REGNO (tmp)]);
18274 break;
18276 /* Handle [reg+UIMM]. */
18277 else if (GET_CODE (tmp) == PLUS &&
18278 GET_CODE (XEXP (tmp, 1)) == CONST_INT)
18280 int x;
18282 gcc_assert (REG_P (XEXP (tmp, 0)));
18284 x = INTVAL (XEXP (tmp, 1));
18285 fprintf (file, "%d(%s)", x, reg_names[REGNO (XEXP (tmp, 0))]);
18286 break;
18289 /* Fall through. Must be [reg+reg]. */
18291 if (VECTOR_MEM_ALTIVEC_P (GET_MODE (x))
18292 && GET_CODE (tmp) == AND
18293 && GET_CODE (XEXP (tmp, 1)) == CONST_INT
18294 && INTVAL (XEXP (tmp, 1)) == -16)
18295 tmp = XEXP (tmp, 0);
18296 else if (VECTOR_MEM_VSX_P (GET_MODE (x))
18297 && GET_CODE (tmp) == PRE_MODIFY)
18298 tmp = XEXP (tmp, 1);
18299 if (REG_P (tmp))
18300 fprintf (file, "0,%s", reg_names[REGNO (tmp)]);
18301 else
18303 if (!GET_CODE (tmp) == PLUS
18304 || !REG_P (XEXP (tmp, 0))
18305 || !REG_P (XEXP (tmp, 1)))
18307 output_operand_lossage ("invalid %%y value, try using the 'Z' constraint");
18308 break;
18311 if (REGNO (XEXP (tmp, 0)) == 0)
18312 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 1)) ],
18313 reg_names[ REGNO (XEXP (tmp, 0)) ]);
18314 else
18315 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 0)) ],
18316 reg_names[ REGNO (XEXP (tmp, 1)) ]);
18318 break;
18321 case 0:
18322 if (REG_P (x))
18323 fprintf (file, "%s", reg_names[REGNO (x)]);
18324 else if (MEM_P (x))
18326 /* We need to handle PRE_INC and PRE_DEC here, since we need to
18327 know the width from the mode. */
18328 if (GET_CODE (XEXP (x, 0)) == PRE_INC)
18329 fprintf (file, "%d(%s)", GET_MODE_SIZE (GET_MODE (x)),
18330 reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
18331 else if (GET_CODE (XEXP (x, 0)) == PRE_DEC)
18332 fprintf (file, "%d(%s)", - GET_MODE_SIZE (GET_MODE (x)),
18333 reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
18334 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
18335 output_address (XEXP (XEXP (x, 0), 1));
18336 else
18337 output_address (XEXP (x, 0));
18339 else
18341 if (toc_relative_expr_p (x, false))
18342 /* This hack along with a corresponding hack in
18343 rs6000_output_addr_const_extra arranges to output addends
18344 where the assembler expects to find them. eg.
18345 (plus (unspec [(symbol_ref ("x")) (reg 2)] tocrel) 4)
18346 without this hack would be output as "x@toc+4". We
18347 want "x+4@toc". */
18348 output_addr_const (file, CONST_CAST_RTX (tocrel_base));
18349 else
18350 output_addr_const (file, x);
18352 return;
18354 case '&':
18355 assemble_name (file, rs6000_get_some_local_dynamic_name ());
18356 return;
18358 default:
18359 output_operand_lossage ("invalid %%xn code");
18363 /* Print the address of an operand. */
18365 void
18366 print_operand_address (FILE *file, rtx x)
18368 if (REG_P (x))
18369 fprintf (file, "0(%s)", reg_names[ REGNO (x) ]);
18370 else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST
18371 || GET_CODE (x) == LABEL_REF)
18373 output_addr_const (file, x);
18374 if (small_data_operand (x, GET_MODE (x)))
18375 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
18376 reg_names[SMALL_DATA_REG]);
18377 else
18378 gcc_assert (!TARGET_TOC);
18380 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
18381 && REG_P (XEXP (x, 1)))
18383 if (REGNO (XEXP (x, 0)) == 0)
18384 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 1)) ],
18385 reg_names[ REGNO (XEXP (x, 0)) ]);
18386 else
18387 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 0)) ],
18388 reg_names[ REGNO (XEXP (x, 1)) ]);
18390 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
18391 && GET_CODE (XEXP (x, 1)) == CONST_INT)
18392 fprintf (file, HOST_WIDE_INT_PRINT_DEC "(%s)",
18393 INTVAL (XEXP (x, 1)), reg_names[ REGNO (XEXP (x, 0)) ]);
18394 #if TARGET_MACHO
18395 else if (GET_CODE (x) == LO_SUM && REG_P (XEXP (x, 0))
18396 && CONSTANT_P (XEXP (x, 1)))
18398 fprintf (file, "lo16(");
18399 output_addr_const (file, XEXP (x, 1));
18400 fprintf (file, ")(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
18402 #endif
18403 #if TARGET_ELF
18404 else if (GET_CODE (x) == LO_SUM && REG_P (XEXP (x, 0))
18405 && CONSTANT_P (XEXP (x, 1)))
18407 output_addr_const (file, XEXP (x, 1));
18408 fprintf (file, "@l(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
18410 #endif
18411 else if (toc_relative_expr_p (x, false))
18413 /* This hack along with a corresponding hack in
18414 rs6000_output_addr_const_extra arranges to output addends
18415 where the assembler expects to find them. eg.
18416 (lo_sum (reg 9)
18417 . (plus (unspec [(symbol_ref ("x")) (reg 2)] tocrel) 8))
18418 without this hack would be output as "x@toc+8@l(9)". We
18419 want "x+8@toc@l(9)". */
18420 output_addr_const (file, CONST_CAST_RTX (tocrel_base));
18421 if (GET_CODE (x) == LO_SUM)
18422 fprintf (file, "@l(%s)", reg_names[REGNO (XEXP (x, 0))]);
18423 else
18424 fprintf (file, "(%s)", reg_names[REGNO (XVECEXP (tocrel_base, 0, 1))]);
18426 else
18427 gcc_unreachable ();
18430 /* Implement TARGET_OUTPUT_ADDR_CONST_EXTRA. */
18432 static bool
18433 rs6000_output_addr_const_extra (FILE *file, rtx x)
18435 if (GET_CODE (x) == UNSPEC)
18436 switch (XINT (x, 1))
18438 case UNSPEC_TOCREL:
18439 gcc_checking_assert (GET_CODE (XVECEXP (x, 0, 0)) == SYMBOL_REF
18440 && REG_P (XVECEXP (x, 0, 1))
18441 && REGNO (XVECEXP (x, 0, 1)) == TOC_REGISTER);
18442 output_addr_const (file, XVECEXP (x, 0, 0));
18443 if (x == tocrel_base && tocrel_offset != const0_rtx)
18445 if (INTVAL (tocrel_offset) >= 0)
18446 fprintf (file, "+");
18447 output_addr_const (file, CONST_CAST_RTX (tocrel_offset));
18449 if (!TARGET_AIX || (TARGET_ELF && TARGET_MINIMAL_TOC))
18451 putc ('-', file);
18452 assemble_name (file, toc_label_name);
18454 else if (TARGET_ELF)
18455 fputs ("@toc", file);
18456 return true;
18458 #if TARGET_MACHO
18459 case UNSPEC_MACHOPIC_OFFSET:
18460 output_addr_const (file, XVECEXP (x, 0, 0));
18461 putc ('-', file);
18462 machopic_output_function_base_name (file);
18463 return true;
18464 #endif
18466 return false;
18469 /* Target hook for assembling integer objects. The PowerPC version has
18470 to handle fixup entries for relocatable code if RELOCATABLE_NEEDS_FIXUP
18471 is defined. It also needs to handle DI-mode objects on 64-bit
18472 targets. */
18474 static bool
18475 rs6000_assemble_integer (rtx x, unsigned int size, int aligned_p)
18477 #ifdef RELOCATABLE_NEEDS_FIXUP
18478 /* Special handling for SI values. */
18479 if (RELOCATABLE_NEEDS_FIXUP && size == 4 && aligned_p)
18481 static int recurse = 0;
18483 /* For -mrelocatable, we mark all addresses that need to be fixed up in
18484 the .fixup section. Since the TOC section is already relocated, we
18485 don't need to mark it here. We used to skip the text section, but it
18486 should never be valid for relocated addresses to be placed in the text
18487 section. */
18488 if (TARGET_RELOCATABLE
18489 && in_section != toc_section
18490 && !recurse
18491 && !CONST_SCALAR_INT_P (x)
18492 && CONSTANT_P (x))
18494 char buf[256];
18496 recurse = 1;
18497 ASM_GENERATE_INTERNAL_LABEL (buf, "LCP", fixuplabelno);
18498 fixuplabelno++;
18499 ASM_OUTPUT_LABEL (asm_out_file, buf);
18500 fprintf (asm_out_file, "\t.long\t(");
18501 output_addr_const (asm_out_file, x);
18502 fprintf (asm_out_file, ")@fixup\n");
18503 fprintf (asm_out_file, "\t.section\t\".fixup\",\"aw\"\n");
18504 ASM_OUTPUT_ALIGN (asm_out_file, 2);
18505 fprintf (asm_out_file, "\t.long\t");
18506 assemble_name (asm_out_file, buf);
18507 fprintf (asm_out_file, "\n\t.previous\n");
18508 recurse = 0;
18509 return true;
18511 /* Remove initial .'s to turn a -mcall-aixdesc function
18512 address into the address of the descriptor, not the function
18513 itself. */
18514 else if (GET_CODE (x) == SYMBOL_REF
18515 && XSTR (x, 0)[0] == '.'
18516 && DEFAULT_ABI == ABI_AIX)
18518 const char *name = XSTR (x, 0);
18519 while (*name == '.')
18520 name++;
18522 fprintf (asm_out_file, "\t.long\t%s\n", name);
18523 return true;
18526 #endif /* RELOCATABLE_NEEDS_FIXUP */
18527 return default_assemble_integer (x, size, aligned_p);
18530 #if defined (HAVE_GAS_HIDDEN) && !TARGET_MACHO
18531 /* Emit an assembler directive to set symbol visibility for DECL to
18532 VISIBILITY_TYPE. */
18534 static void
18535 rs6000_assemble_visibility (tree decl, int vis)
18537 if (TARGET_XCOFF)
18538 return;
18540 /* Functions need to have their entry point symbol visibility set as
18541 well as their descriptor symbol visibility. */
18542 if (DEFAULT_ABI == ABI_AIX
18543 && DOT_SYMBOLS
18544 && TREE_CODE (decl) == FUNCTION_DECL)
18546 static const char * const visibility_types[] = {
18547 NULL, "internal", "hidden", "protected"
18550 const char *name, *type;
18552 name = ((* targetm.strip_name_encoding)
18553 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
18554 type = visibility_types[vis];
18556 fprintf (asm_out_file, "\t.%s\t%s\n", type, name);
18557 fprintf (asm_out_file, "\t.%s\t.%s\n", type, name);
18559 else
18560 default_assemble_visibility (decl, vis);
18562 #endif
18564 enum rtx_code
18565 rs6000_reverse_condition (enum machine_mode mode, enum rtx_code code)
18567 /* Reversal of FP compares takes care -- an ordered compare
18568 becomes an unordered compare and vice versa. */
18569 if (mode == CCFPmode
18570 && (!flag_finite_math_only
18571 || code == UNLT || code == UNLE || code == UNGT || code == UNGE
18572 || code == UNEQ || code == LTGT))
18573 return reverse_condition_maybe_unordered (code);
18574 else
18575 return reverse_condition (code);
18578 /* Generate a compare for CODE. Return a brand-new rtx that
18579 represents the result of the compare. */
18581 static rtx
18582 rs6000_generate_compare (rtx cmp, enum machine_mode mode)
18584 enum machine_mode comp_mode;
18585 rtx compare_result;
18586 enum rtx_code code = GET_CODE (cmp);
18587 rtx op0 = XEXP (cmp, 0);
18588 rtx op1 = XEXP (cmp, 1);
18590 if (FLOAT_MODE_P (mode))
18591 comp_mode = CCFPmode;
18592 else if (code == GTU || code == LTU
18593 || code == GEU || code == LEU)
18594 comp_mode = CCUNSmode;
18595 else if ((code == EQ || code == NE)
18596 && unsigned_reg_p (op0)
18597 && (unsigned_reg_p (op1)
18598 || (CONST_INT_P (op1) && INTVAL (op1) != 0)))
18599 /* These are unsigned values, perhaps there will be a later
18600 ordering compare that can be shared with this one. */
18601 comp_mode = CCUNSmode;
18602 else
18603 comp_mode = CCmode;
18605 /* If we have an unsigned compare, make sure we don't have a signed value as
18606 an immediate. */
18607 if (comp_mode == CCUNSmode && GET_CODE (op1) == CONST_INT
18608 && INTVAL (op1) < 0)
18610 op0 = copy_rtx_if_shared (op0);
18611 op1 = force_reg (GET_MODE (op0), op1);
18612 cmp = gen_rtx_fmt_ee (code, GET_MODE (cmp), op0, op1);
18615 /* First, the compare. */
18616 compare_result = gen_reg_rtx (comp_mode);
18618 /* E500 FP compare instructions on the GPRs. Yuck! */
18619 if ((!TARGET_FPRS && TARGET_HARD_FLOAT)
18620 && FLOAT_MODE_P (mode))
18622 rtx cmp, or_result, compare_result2;
18623 enum machine_mode op_mode = GET_MODE (op0);
18624 bool reverse_p;
18626 if (op_mode == VOIDmode)
18627 op_mode = GET_MODE (op1);
18629 /* First reverse the condition codes that aren't directly supported. */
18630 switch (code)
18632 case NE:
18633 case UNLT:
18634 case UNLE:
18635 case UNGT:
18636 case UNGE:
18637 code = reverse_condition_maybe_unordered (code);
18638 reverse_p = true;
18639 break;
18641 case EQ:
18642 case LT:
18643 case LE:
18644 case GT:
18645 case GE:
18646 reverse_p = false;
18647 break;
18649 default:
18650 gcc_unreachable ();
18653 /* The E500 FP compare instructions toggle the GT bit (CR bit 1) only.
18654 This explains the following mess. */
18656 switch (code)
18658 case EQ:
18659 switch (op_mode)
18661 case SFmode:
18662 cmp = (flag_finite_math_only && !flag_trapping_math)
18663 ? gen_tstsfeq_gpr (compare_result, op0, op1)
18664 : gen_cmpsfeq_gpr (compare_result, op0, op1);
18665 break;
18667 case DFmode:
18668 cmp = (flag_finite_math_only && !flag_trapping_math)
18669 ? gen_tstdfeq_gpr (compare_result, op0, op1)
18670 : gen_cmpdfeq_gpr (compare_result, op0, op1);
18671 break;
18673 case TFmode:
18674 cmp = (flag_finite_math_only && !flag_trapping_math)
18675 ? gen_tsttfeq_gpr (compare_result, op0, op1)
18676 : gen_cmptfeq_gpr (compare_result, op0, op1);
18677 break;
18679 default:
18680 gcc_unreachable ();
18682 break;
18684 case GT:
18685 case GE:
18686 switch (op_mode)
18688 case SFmode:
18689 cmp = (flag_finite_math_only && !flag_trapping_math)
18690 ? gen_tstsfgt_gpr (compare_result, op0, op1)
18691 : gen_cmpsfgt_gpr (compare_result, op0, op1);
18692 break;
18694 case DFmode:
18695 cmp = (flag_finite_math_only && !flag_trapping_math)
18696 ? gen_tstdfgt_gpr (compare_result, op0, op1)
18697 : gen_cmpdfgt_gpr (compare_result, op0, op1);
18698 break;
18700 case TFmode:
18701 cmp = (flag_finite_math_only && !flag_trapping_math)
18702 ? gen_tsttfgt_gpr (compare_result, op0, op1)
18703 : gen_cmptfgt_gpr (compare_result, op0, op1);
18704 break;
18706 default:
18707 gcc_unreachable ();
18709 break;
18711 case LT:
18712 case LE:
18713 switch (op_mode)
18715 case SFmode:
18716 cmp = (flag_finite_math_only && !flag_trapping_math)
18717 ? gen_tstsflt_gpr (compare_result, op0, op1)
18718 : gen_cmpsflt_gpr (compare_result, op0, op1);
18719 break;
18721 case DFmode:
18722 cmp = (flag_finite_math_only && !flag_trapping_math)
18723 ? gen_tstdflt_gpr (compare_result, op0, op1)
18724 : gen_cmpdflt_gpr (compare_result, op0, op1);
18725 break;
18727 case TFmode:
18728 cmp = (flag_finite_math_only && !flag_trapping_math)
18729 ? gen_tsttflt_gpr (compare_result, op0, op1)
18730 : gen_cmptflt_gpr (compare_result, op0, op1);
18731 break;
18733 default:
18734 gcc_unreachable ();
18736 break;
18738 default:
18739 gcc_unreachable ();
18742 /* Synthesize LE and GE from LT/GT || EQ. */
18743 if (code == LE || code == GE)
18745 emit_insn (cmp);
18747 compare_result2 = gen_reg_rtx (CCFPmode);
18749 /* Do the EQ. */
18750 switch (op_mode)
18752 case SFmode:
18753 cmp = (flag_finite_math_only && !flag_trapping_math)
18754 ? gen_tstsfeq_gpr (compare_result2, op0, op1)
18755 : gen_cmpsfeq_gpr (compare_result2, op0, op1);
18756 break;
18758 case DFmode:
18759 cmp = (flag_finite_math_only && !flag_trapping_math)
18760 ? gen_tstdfeq_gpr (compare_result2, op0, op1)
18761 : gen_cmpdfeq_gpr (compare_result2, op0, op1);
18762 break;
18764 case TFmode:
18765 cmp = (flag_finite_math_only && !flag_trapping_math)
18766 ? gen_tsttfeq_gpr (compare_result2, op0, op1)
18767 : gen_cmptfeq_gpr (compare_result2, op0, op1);
18768 break;
18770 default:
18771 gcc_unreachable ();
18774 emit_insn (cmp);
18776 /* OR them together. */
18777 or_result = gen_reg_rtx (CCFPmode);
18778 cmp = gen_e500_cr_ior_compare (or_result, compare_result,
18779 compare_result2);
18780 compare_result = or_result;
18783 code = reverse_p ? NE : EQ;
18785 emit_insn (cmp);
18787 else
18789 /* Generate XLC-compatible TFmode compare as PARALLEL with extra
18790 CLOBBERs to match cmptf_internal2 pattern. */
18791 if (comp_mode == CCFPmode && TARGET_XL_COMPAT
18792 && GET_MODE (op0) == TFmode
18793 && !TARGET_IEEEQUAD
18794 && TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_LONG_DOUBLE_128)
18795 emit_insn (gen_rtx_PARALLEL (VOIDmode,
18796 gen_rtvec (10,
18797 gen_rtx_SET (VOIDmode,
18798 compare_result,
18799 gen_rtx_COMPARE (comp_mode, op0, op1)),
18800 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
18801 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
18802 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
18803 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
18804 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
18805 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
18806 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
18807 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
18808 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (Pmode)))));
18809 else if (GET_CODE (op1) == UNSPEC
18810 && XINT (op1, 1) == UNSPEC_SP_TEST)
18812 rtx op1b = XVECEXP (op1, 0, 0);
18813 comp_mode = CCEQmode;
18814 compare_result = gen_reg_rtx (CCEQmode);
18815 if (TARGET_64BIT)
18816 emit_insn (gen_stack_protect_testdi (compare_result, op0, op1b));
18817 else
18818 emit_insn (gen_stack_protect_testsi (compare_result, op0, op1b));
18820 else
18821 emit_insn (gen_rtx_SET (VOIDmode, compare_result,
18822 gen_rtx_COMPARE (comp_mode, op0, op1)));
18825 /* Some kinds of FP comparisons need an OR operation;
18826 under flag_finite_math_only we don't bother. */
18827 if (FLOAT_MODE_P (mode)
18828 && !flag_finite_math_only
18829 && !(TARGET_HARD_FLOAT && !TARGET_FPRS)
18830 && (code == LE || code == GE
18831 || code == UNEQ || code == LTGT
18832 || code == UNGT || code == UNLT))
18834 enum rtx_code or1, or2;
18835 rtx or1_rtx, or2_rtx, compare2_rtx;
18836 rtx or_result = gen_reg_rtx (CCEQmode);
18838 switch (code)
18840 case LE: or1 = LT; or2 = EQ; break;
18841 case GE: or1 = GT; or2 = EQ; break;
18842 case UNEQ: or1 = UNORDERED; or2 = EQ; break;
18843 case LTGT: or1 = LT; or2 = GT; break;
18844 case UNGT: or1 = UNORDERED; or2 = GT; break;
18845 case UNLT: or1 = UNORDERED; or2 = LT; break;
18846 default: gcc_unreachable ();
18848 validate_condition_mode (or1, comp_mode);
18849 validate_condition_mode (or2, comp_mode);
18850 or1_rtx = gen_rtx_fmt_ee (or1, SImode, compare_result, const0_rtx);
18851 or2_rtx = gen_rtx_fmt_ee (or2, SImode, compare_result, const0_rtx);
18852 compare2_rtx = gen_rtx_COMPARE (CCEQmode,
18853 gen_rtx_IOR (SImode, or1_rtx, or2_rtx),
18854 const_true_rtx);
18855 emit_insn (gen_rtx_SET (VOIDmode, or_result, compare2_rtx));
18857 compare_result = or_result;
18858 code = EQ;
18861 validate_condition_mode (code, GET_MODE (compare_result));
18863 return gen_rtx_fmt_ee (code, VOIDmode, compare_result, const0_rtx);
18867 /* Emit the RTL for an sISEL pattern. */
18869 void
18870 rs6000_emit_sISEL (enum machine_mode mode ATTRIBUTE_UNUSED, rtx operands[])
18872 rs6000_emit_int_cmove (operands[0], operands[1], const1_rtx, const0_rtx);
18875 void
18876 rs6000_emit_sCOND (enum machine_mode mode, rtx operands[])
18878 rtx condition_rtx;
18879 enum machine_mode op_mode;
18880 enum rtx_code cond_code;
18881 rtx result = operands[0];
18883 if (TARGET_ISEL && (mode == SImode || mode == DImode))
18885 rs6000_emit_sISEL (mode, operands);
18886 return;
18889 condition_rtx = rs6000_generate_compare (operands[1], mode);
18890 cond_code = GET_CODE (condition_rtx);
18892 if (FLOAT_MODE_P (mode)
18893 && !TARGET_FPRS && TARGET_HARD_FLOAT)
18895 rtx t;
18897 PUT_MODE (condition_rtx, SImode);
18898 t = XEXP (condition_rtx, 0);
18900 gcc_assert (cond_code == NE || cond_code == EQ);
18902 if (cond_code == NE)
18903 emit_insn (gen_e500_flip_gt_bit (t, t));
18905 emit_insn (gen_move_from_CR_gt_bit (result, t));
18906 return;
18909 if (cond_code == NE
18910 || cond_code == GE || cond_code == LE
18911 || cond_code == GEU || cond_code == LEU
18912 || cond_code == ORDERED || cond_code == UNGE || cond_code == UNLE)
18914 rtx not_result = gen_reg_rtx (CCEQmode);
18915 rtx not_op, rev_cond_rtx;
18916 enum machine_mode cc_mode;
18918 cc_mode = GET_MODE (XEXP (condition_rtx, 0));
18920 rev_cond_rtx = gen_rtx_fmt_ee (rs6000_reverse_condition (cc_mode, cond_code),
18921 SImode, XEXP (condition_rtx, 0), const0_rtx);
18922 not_op = gen_rtx_COMPARE (CCEQmode, rev_cond_rtx, const0_rtx);
18923 emit_insn (gen_rtx_SET (VOIDmode, not_result, not_op));
18924 condition_rtx = gen_rtx_EQ (VOIDmode, not_result, const0_rtx);
18927 op_mode = GET_MODE (XEXP (operands[1], 0));
18928 if (op_mode == VOIDmode)
18929 op_mode = GET_MODE (XEXP (operands[1], 1));
18931 if (TARGET_POWERPC64 && (op_mode == DImode || FLOAT_MODE_P (mode)))
18933 PUT_MODE (condition_rtx, DImode);
18934 convert_move (result, condition_rtx, 0);
18936 else
18938 PUT_MODE (condition_rtx, SImode);
18939 emit_insn (gen_rtx_SET (VOIDmode, result, condition_rtx));
18943 /* Emit a branch of kind CODE to location LOC. */
18945 void
18946 rs6000_emit_cbranch (enum machine_mode mode, rtx operands[])
18948 rtx condition_rtx, loc_ref;
18950 condition_rtx = rs6000_generate_compare (operands[0], mode);
18951 loc_ref = gen_rtx_LABEL_REF (VOIDmode, operands[3]);
18952 emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx,
18953 gen_rtx_IF_THEN_ELSE (VOIDmode, condition_rtx,
18954 loc_ref, pc_rtx)));
18957 /* Return the string to output a conditional branch to LABEL, which is
18958 the operand template of the label, or NULL if the branch is really a
18959 conditional return.
18961 OP is the conditional expression. XEXP (OP, 0) is assumed to be a
18962 condition code register and its mode specifies what kind of
18963 comparison we made.
18965 REVERSED is nonzero if we should reverse the sense of the comparison.
18967 INSN is the insn. */
18969 char *
18970 output_cbranch (rtx op, const char *label, int reversed, rtx insn)
18972 static char string[64];
18973 enum rtx_code code = GET_CODE (op);
18974 rtx cc_reg = XEXP (op, 0);
18975 enum machine_mode mode = GET_MODE (cc_reg);
18976 int cc_regno = REGNO (cc_reg) - CR0_REGNO;
18977 int need_longbranch = label != NULL && get_attr_length (insn) == 8;
18978 int really_reversed = reversed ^ need_longbranch;
18979 char *s = string;
18980 const char *ccode;
18981 const char *pred;
18982 rtx note;
18984 validate_condition_mode (code, mode);
18986 /* Work out which way this really branches. We could use
18987 reverse_condition_maybe_unordered here always but this
18988 makes the resulting assembler clearer. */
18989 if (really_reversed)
18991 /* Reversal of FP compares takes care -- an ordered compare
18992 becomes an unordered compare and vice versa. */
18993 if (mode == CCFPmode)
18994 code = reverse_condition_maybe_unordered (code);
18995 else
18996 code = reverse_condition (code);
18999 if ((!TARGET_FPRS && TARGET_HARD_FLOAT) && mode == CCFPmode)
19001 /* The efscmp/tst* instructions twiddle bit 2, which maps nicely
19002 to the GT bit. */
19003 switch (code)
19005 case EQ:
19006 /* Opposite of GT. */
19007 code = GT;
19008 break;
19010 case NE:
19011 code = UNLE;
19012 break;
19014 default:
19015 gcc_unreachable ();
19019 switch (code)
19021 /* Not all of these are actually distinct opcodes, but
19022 we distinguish them for clarity of the resulting assembler. */
19023 case NE: case LTGT:
19024 ccode = "ne"; break;
19025 case EQ: case UNEQ:
19026 ccode = "eq"; break;
19027 case GE: case GEU:
19028 ccode = "ge"; break;
19029 case GT: case GTU: case UNGT:
19030 ccode = "gt"; break;
19031 case LE: case LEU:
19032 ccode = "le"; break;
19033 case LT: case LTU: case UNLT:
19034 ccode = "lt"; break;
19035 case UNORDERED: ccode = "un"; break;
19036 case ORDERED: ccode = "nu"; break;
19037 case UNGE: ccode = "nl"; break;
19038 case UNLE: ccode = "ng"; break;
19039 default:
19040 gcc_unreachable ();
19043 /* Maybe we have a guess as to how likely the branch is. */
19044 pred = "";
19045 note = find_reg_note (insn, REG_BR_PROB, NULL_RTX);
19046 if (note != NULL_RTX)
19048 /* PROB is the difference from 50%. */
19049 int prob = XINT (note, 0) - REG_BR_PROB_BASE / 2;
19051 /* Only hint for highly probable/improbable branches on newer
19052 cpus as static prediction overrides processor dynamic
19053 prediction. For older cpus we may as well always hint, but
19054 assume not taken for branches that are very close to 50% as a
19055 mispredicted taken branch is more expensive than a
19056 mispredicted not-taken branch. */
19057 if (rs6000_always_hint
19058 || (abs (prob) > REG_BR_PROB_BASE / 100 * 48
19059 && br_prob_note_reliable_p (note)))
19061 if (abs (prob) > REG_BR_PROB_BASE / 20
19062 && ((prob > 0) ^ need_longbranch))
19063 pred = "+";
19064 else
19065 pred = "-";
19069 if (label == NULL)
19070 s += sprintf (s, "b%slr%s ", ccode, pred);
19071 else
19072 s += sprintf (s, "b%s%s ", ccode, pred);
19074 /* We need to escape any '%' characters in the reg_names string.
19075 Assume they'd only be the first character.... */
19076 if (reg_names[cc_regno + CR0_REGNO][0] == '%')
19077 *s++ = '%';
19078 s += sprintf (s, "%s", reg_names[cc_regno + CR0_REGNO]);
19080 if (label != NULL)
19082 /* If the branch distance was too far, we may have to use an
19083 unconditional branch to go the distance. */
19084 if (need_longbranch)
19085 s += sprintf (s, ",$+8\n\tb %s", label);
19086 else
19087 s += sprintf (s, ",%s", label);
19090 return string;
19093 /* Return the string to flip the GT bit on a CR. */
19094 char *
19095 output_e500_flip_gt_bit (rtx dst, rtx src)
19097 static char string[64];
19098 int a, b;
19100 gcc_assert (GET_CODE (dst) == REG && CR_REGNO_P (REGNO (dst))
19101 && GET_CODE (src) == REG && CR_REGNO_P (REGNO (src)));
19103 /* GT bit. */
19104 a = 4 * (REGNO (dst) - CR0_REGNO) + 1;
19105 b = 4 * (REGNO (src) - CR0_REGNO) + 1;
19107 sprintf (string, "crnot %d,%d", a, b);
19108 return string;
19111 /* Return insn for VSX or Altivec comparisons. */
19113 static rtx
19114 rs6000_emit_vector_compare_inner (enum rtx_code code, rtx op0, rtx op1)
19116 rtx mask;
19117 enum machine_mode mode = GET_MODE (op0);
19119 switch (code)
19121 default:
19122 break;
19124 case GE:
19125 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
19126 return NULL_RTX;
19128 case EQ:
19129 case GT:
19130 case GTU:
19131 case ORDERED:
19132 case UNORDERED:
19133 case UNEQ:
19134 case LTGT:
19135 mask = gen_reg_rtx (mode);
19136 emit_insn (gen_rtx_SET (VOIDmode,
19137 mask,
19138 gen_rtx_fmt_ee (code, mode, op0, op1)));
19139 return mask;
19142 return NULL_RTX;
19145 /* Emit vector compare for operands OP0 and OP1 using code RCODE.
19146 DMODE is expected destination mode. This is a recursive function. */
19148 static rtx
19149 rs6000_emit_vector_compare (enum rtx_code rcode,
19150 rtx op0, rtx op1,
19151 enum machine_mode dmode)
19153 rtx mask;
19154 bool swap_operands = false;
19155 bool try_again = false;
19157 gcc_assert (VECTOR_UNIT_ALTIVEC_OR_VSX_P (dmode));
19158 gcc_assert (GET_MODE (op0) == GET_MODE (op1));
19160 /* See if the comparison works as is. */
19161 mask = rs6000_emit_vector_compare_inner (rcode, op0, op1);
19162 if (mask)
19163 return mask;
19165 switch (rcode)
19167 case LT:
19168 rcode = GT;
19169 swap_operands = true;
19170 try_again = true;
19171 break;
19172 case LTU:
19173 rcode = GTU;
19174 swap_operands = true;
19175 try_again = true;
19176 break;
19177 case NE:
19178 case UNLE:
19179 case UNLT:
19180 case UNGE:
19181 case UNGT:
19182 /* Invert condition and try again.
19183 e.g., A != B becomes ~(A==B). */
19185 enum rtx_code rev_code;
19186 enum insn_code nor_code;
19187 rtx mask2;
19189 rev_code = reverse_condition_maybe_unordered (rcode);
19190 if (rev_code == UNKNOWN)
19191 return NULL_RTX;
19193 nor_code = optab_handler (one_cmpl_optab, dmode);
19194 if (nor_code == CODE_FOR_nothing)
19195 return NULL_RTX;
19197 mask2 = rs6000_emit_vector_compare (rev_code, op0, op1, dmode);
19198 if (!mask2)
19199 return NULL_RTX;
19201 mask = gen_reg_rtx (dmode);
19202 emit_insn (GEN_FCN (nor_code) (mask, mask2));
19203 return mask;
19205 break;
19206 case GE:
19207 case GEU:
19208 case LE:
19209 case LEU:
19210 /* Try GT/GTU/LT/LTU OR EQ */
19212 rtx c_rtx, eq_rtx;
19213 enum insn_code ior_code;
19214 enum rtx_code new_code;
19216 switch (rcode)
19218 case GE:
19219 new_code = GT;
19220 break;
19222 case GEU:
19223 new_code = GTU;
19224 break;
19226 case LE:
19227 new_code = LT;
19228 break;
19230 case LEU:
19231 new_code = LTU;
19232 break;
19234 default:
19235 gcc_unreachable ();
19238 ior_code = optab_handler (ior_optab, dmode);
19239 if (ior_code == CODE_FOR_nothing)
19240 return NULL_RTX;
19242 c_rtx = rs6000_emit_vector_compare (new_code, op0, op1, dmode);
19243 if (!c_rtx)
19244 return NULL_RTX;
19246 eq_rtx = rs6000_emit_vector_compare (EQ, op0, op1, dmode);
19247 if (!eq_rtx)
19248 return NULL_RTX;
19250 mask = gen_reg_rtx (dmode);
19251 emit_insn (GEN_FCN (ior_code) (mask, c_rtx, eq_rtx));
19252 return mask;
19254 break;
19255 default:
19256 return NULL_RTX;
19259 if (try_again)
19261 if (swap_operands)
19263 rtx tmp;
19264 tmp = op0;
19265 op0 = op1;
19266 op1 = tmp;
19269 mask = rs6000_emit_vector_compare_inner (rcode, op0, op1);
19270 if (mask)
19271 return mask;
19274 /* You only get two chances. */
19275 return NULL_RTX;
19278 /* Emit vector conditional expression. DEST is destination. OP_TRUE and
19279 OP_FALSE are two VEC_COND_EXPR operands. CC_OP0 and CC_OP1 are the two
19280 operands for the relation operation COND. */
19283 rs6000_emit_vector_cond_expr (rtx dest, rtx op_true, rtx op_false,
19284 rtx cond, rtx cc_op0, rtx cc_op1)
19286 enum machine_mode dest_mode = GET_MODE (dest);
19287 enum machine_mode mask_mode = GET_MODE (cc_op0);
19288 enum rtx_code rcode = GET_CODE (cond);
19289 enum machine_mode cc_mode = CCmode;
19290 rtx mask;
19291 rtx cond2;
19292 rtx tmp;
19293 bool invert_move = false;
19295 if (VECTOR_UNIT_NONE_P (dest_mode))
19296 return 0;
19298 gcc_assert (GET_MODE_SIZE (dest_mode) == GET_MODE_SIZE (mask_mode)
19299 && GET_MODE_NUNITS (dest_mode) == GET_MODE_NUNITS (mask_mode));
19301 switch (rcode)
19303 /* Swap operands if we can, and fall back to doing the operation as
19304 specified, and doing a NOR to invert the test. */
19305 case NE:
19306 case UNLE:
19307 case UNLT:
19308 case UNGE:
19309 case UNGT:
19310 /* Invert condition and try again.
19311 e.g., A = (B != C) ? D : E becomes A = (B == C) ? E : D. */
19312 invert_move = true;
19313 rcode = reverse_condition_maybe_unordered (rcode);
19314 if (rcode == UNKNOWN)
19315 return 0;
19316 break;
19318 /* Mark unsigned tests with CCUNSmode. */
19319 case GTU:
19320 case GEU:
19321 case LTU:
19322 case LEU:
19323 cc_mode = CCUNSmode;
19324 break;
19326 default:
19327 break;
19330 /* Get the vector mask for the given relational operations. */
19331 mask = rs6000_emit_vector_compare (rcode, cc_op0, cc_op1, mask_mode);
19333 if (!mask)
19334 return 0;
19336 if (invert_move)
19338 tmp = op_true;
19339 op_true = op_false;
19340 op_false = tmp;
19343 cond2 = gen_rtx_fmt_ee (NE, cc_mode, gen_lowpart (dest_mode, mask),
19344 CONST0_RTX (dest_mode));
19345 emit_insn (gen_rtx_SET (VOIDmode,
19346 dest,
19347 gen_rtx_IF_THEN_ELSE (dest_mode,
19348 cond2,
19349 op_true,
19350 op_false)));
19351 return 1;
19354 /* Emit a conditional move: move TRUE_COND to DEST if OP of the
19355 operands of the last comparison is nonzero/true, FALSE_COND if it
19356 is zero/false. Return 0 if the hardware has no such operation. */
19359 rs6000_emit_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
19361 enum rtx_code code = GET_CODE (op);
19362 rtx op0 = XEXP (op, 0);
19363 rtx op1 = XEXP (op, 1);
19364 REAL_VALUE_TYPE c1;
19365 enum machine_mode compare_mode = GET_MODE (op0);
19366 enum machine_mode result_mode = GET_MODE (dest);
19367 rtx temp;
19368 bool is_against_zero;
19370 /* These modes should always match. */
19371 if (GET_MODE (op1) != compare_mode
19372 /* In the isel case however, we can use a compare immediate, so
19373 op1 may be a small constant. */
19374 && (!TARGET_ISEL || !short_cint_operand (op1, VOIDmode)))
19375 return 0;
19376 if (GET_MODE (true_cond) != result_mode)
19377 return 0;
19378 if (GET_MODE (false_cond) != result_mode)
19379 return 0;
19381 /* Don't allow using floating point comparisons for integer results for
19382 now. */
19383 if (FLOAT_MODE_P (compare_mode) && !FLOAT_MODE_P (result_mode))
19384 return 0;
19386 /* First, work out if the hardware can do this at all, or
19387 if it's too slow.... */
19388 if (!FLOAT_MODE_P (compare_mode))
19390 if (TARGET_ISEL)
19391 return rs6000_emit_int_cmove (dest, op, true_cond, false_cond);
19392 return 0;
19394 else if (TARGET_HARD_FLOAT && !TARGET_FPRS
19395 && SCALAR_FLOAT_MODE_P (compare_mode))
19396 return 0;
19398 is_against_zero = op1 == CONST0_RTX (compare_mode);
19400 /* A floating-point subtract might overflow, underflow, or produce
19401 an inexact result, thus changing the floating-point flags, so it
19402 can't be generated if we care about that. It's safe if one side
19403 of the construct is zero, since then no subtract will be
19404 generated. */
19405 if (SCALAR_FLOAT_MODE_P (compare_mode)
19406 && flag_trapping_math && ! is_against_zero)
19407 return 0;
19409 /* Eliminate half of the comparisons by switching operands, this
19410 makes the remaining code simpler. */
19411 if (code == UNLT || code == UNGT || code == UNORDERED || code == NE
19412 || code == LTGT || code == LT || code == UNLE)
19414 code = reverse_condition_maybe_unordered (code);
19415 temp = true_cond;
19416 true_cond = false_cond;
19417 false_cond = temp;
19420 /* UNEQ and LTGT take four instructions for a comparison with zero,
19421 it'll probably be faster to use a branch here too. */
19422 if (code == UNEQ && HONOR_NANS (compare_mode))
19423 return 0;
19425 if (GET_CODE (op1) == CONST_DOUBLE)
19426 REAL_VALUE_FROM_CONST_DOUBLE (c1, op1);
19428 /* We're going to try to implement comparisons by performing
19429 a subtract, then comparing against zero. Unfortunately,
19430 Inf - Inf is NaN which is not zero, and so if we don't
19431 know that the operand is finite and the comparison
19432 would treat EQ different to UNORDERED, we can't do it. */
19433 if (HONOR_INFINITIES (compare_mode)
19434 && code != GT && code != UNGE
19435 && (GET_CODE (op1) != CONST_DOUBLE || real_isinf (&c1))
19436 /* Constructs of the form (a OP b ? a : b) are safe. */
19437 && ((! rtx_equal_p (op0, false_cond) && ! rtx_equal_p (op1, false_cond))
19438 || (! rtx_equal_p (op0, true_cond)
19439 && ! rtx_equal_p (op1, true_cond))))
19440 return 0;
19442 /* At this point we know we can use fsel. */
19444 /* Reduce the comparison to a comparison against zero. */
19445 if (! is_against_zero)
19447 temp = gen_reg_rtx (compare_mode);
19448 emit_insn (gen_rtx_SET (VOIDmode, temp,
19449 gen_rtx_MINUS (compare_mode, op0, op1)));
19450 op0 = temp;
19451 op1 = CONST0_RTX (compare_mode);
19454 /* If we don't care about NaNs we can reduce some of the comparisons
19455 down to faster ones. */
19456 if (! HONOR_NANS (compare_mode))
19457 switch (code)
19459 case GT:
19460 code = LE;
19461 temp = true_cond;
19462 true_cond = false_cond;
19463 false_cond = temp;
19464 break;
19465 case UNGE:
19466 code = GE;
19467 break;
19468 case UNEQ:
19469 code = EQ;
19470 break;
19471 default:
19472 break;
19475 /* Now, reduce everything down to a GE. */
19476 switch (code)
19478 case GE:
19479 break;
19481 case LE:
19482 temp = gen_reg_rtx (compare_mode);
19483 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
19484 op0 = temp;
19485 break;
19487 case ORDERED:
19488 temp = gen_reg_rtx (compare_mode);
19489 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_ABS (compare_mode, op0)));
19490 op0 = temp;
19491 break;
19493 case EQ:
19494 temp = gen_reg_rtx (compare_mode);
19495 emit_insn (gen_rtx_SET (VOIDmode, temp,
19496 gen_rtx_NEG (compare_mode,
19497 gen_rtx_ABS (compare_mode, op0))));
19498 op0 = temp;
19499 break;
19501 case UNGE:
19502 /* a UNGE 0 <-> (a GE 0 || -a UNLT 0) */
19503 temp = gen_reg_rtx (result_mode);
19504 emit_insn (gen_rtx_SET (VOIDmode, temp,
19505 gen_rtx_IF_THEN_ELSE (result_mode,
19506 gen_rtx_GE (VOIDmode,
19507 op0, op1),
19508 true_cond, false_cond)));
19509 false_cond = true_cond;
19510 true_cond = temp;
19512 temp = gen_reg_rtx (compare_mode);
19513 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
19514 op0 = temp;
19515 break;
19517 case GT:
19518 /* a GT 0 <-> (a GE 0 && -a UNLT 0) */
19519 temp = gen_reg_rtx (result_mode);
19520 emit_insn (gen_rtx_SET (VOIDmode, temp,
19521 gen_rtx_IF_THEN_ELSE (result_mode,
19522 gen_rtx_GE (VOIDmode,
19523 op0, op1),
19524 true_cond, false_cond)));
19525 true_cond = false_cond;
19526 false_cond = temp;
19528 temp = gen_reg_rtx (compare_mode);
19529 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
19530 op0 = temp;
19531 break;
19533 default:
19534 gcc_unreachable ();
19537 emit_insn (gen_rtx_SET (VOIDmode, dest,
19538 gen_rtx_IF_THEN_ELSE (result_mode,
19539 gen_rtx_GE (VOIDmode,
19540 op0, op1),
19541 true_cond, false_cond)));
19542 return 1;
19545 /* Same as above, but for ints (isel). */
19547 static int
19548 rs6000_emit_int_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
19550 rtx condition_rtx, cr;
19551 enum machine_mode mode = GET_MODE (dest);
19552 enum rtx_code cond_code;
19553 rtx (*isel_func) (rtx, rtx, rtx, rtx, rtx);
19554 bool signedp;
19556 if (mode != SImode && (!TARGET_POWERPC64 || mode != DImode))
19557 return 0;
19559 /* We still have to do the compare, because isel doesn't do a
19560 compare, it just looks at the CRx bits set by a previous compare
19561 instruction. */
19562 condition_rtx = rs6000_generate_compare (op, mode);
19563 cond_code = GET_CODE (condition_rtx);
19564 cr = XEXP (condition_rtx, 0);
19565 signedp = GET_MODE (cr) == CCmode;
19567 isel_func = (mode == SImode
19568 ? (signedp ? gen_isel_signed_si : gen_isel_unsigned_si)
19569 : (signedp ? gen_isel_signed_di : gen_isel_unsigned_di));
19571 switch (cond_code)
19573 case LT: case GT: case LTU: case GTU: case EQ:
19574 /* isel handles these directly. */
19575 break;
19577 default:
19578 /* We need to swap the sense of the comparison. */
19580 rtx t = true_cond;
19581 true_cond = false_cond;
19582 false_cond = t;
19583 PUT_CODE (condition_rtx, reverse_condition (cond_code));
19585 break;
19588 false_cond = force_reg (mode, false_cond);
19589 if (true_cond != const0_rtx)
19590 true_cond = force_reg (mode, true_cond);
19592 emit_insn (isel_func (dest, condition_rtx, true_cond, false_cond, cr));
19594 return 1;
19597 const char *
19598 output_isel (rtx *operands)
19600 enum rtx_code code;
19602 code = GET_CODE (operands[1]);
19604 if (code == GE || code == GEU || code == LE || code == LEU || code == NE)
19606 gcc_assert (GET_CODE (operands[2]) == REG
19607 && GET_CODE (operands[3]) == REG);
19608 PUT_CODE (operands[1], reverse_condition (code));
19609 return "isel %0,%3,%2,%j1";
19612 return "isel %0,%2,%3,%j1";
19615 void
19616 rs6000_emit_minmax (rtx dest, enum rtx_code code, rtx op0, rtx op1)
19618 enum machine_mode mode = GET_MODE (op0);
19619 enum rtx_code c;
19620 rtx target;
19622 /* VSX/altivec have direct min/max insns. */
19623 if ((code == SMAX || code == SMIN)
19624 && (VECTOR_UNIT_ALTIVEC_OR_VSX_P (mode)
19625 || (mode == SFmode && VECTOR_UNIT_VSX_P (DFmode))))
19627 emit_insn (gen_rtx_SET (VOIDmode,
19628 dest,
19629 gen_rtx_fmt_ee (code, mode, op0, op1)));
19630 return;
19633 if (code == SMAX || code == SMIN)
19634 c = GE;
19635 else
19636 c = GEU;
19638 if (code == SMAX || code == UMAX)
19639 target = emit_conditional_move (dest, c, op0, op1, mode,
19640 op0, op1, mode, 0);
19641 else
19642 target = emit_conditional_move (dest, c, op0, op1, mode,
19643 op1, op0, mode, 0);
19644 gcc_assert (target);
19645 if (target != dest)
19646 emit_move_insn (dest, target);
19649 /* A subroutine of the atomic operation splitters. Jump to LABEL if
19650 COND is true. Mark the jump as unlikely to be taken. */
19652 static void
19653 emit_unlikely_jump (rtx cond, rtx label)
19655 int very_unlikely = REG_BR_PROB_BASE / 100 - 1;
19656 rtx x;
19658 x = gen_rtx_IF_THEN_ELSE (VOIDmode, cond, label, pc_rtx);
19659 x = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, x));
19660 add_int_reg_note (x, REG_BR_PROB, very_unlikely);
19663 /* A subroutine of the atomic operation splitters. Emit a load-locked
19664 instruction in MODE. For QI/HImode, possibly use a pattern than includes
19665 the zero_extend operation. */
19667 static void
19668 emit_load_locked (enum machine_mode mode, rtx reg, rtx mem)
19670 rtx (*fn) (rtx, rtx) = NULL;
19672 switch (mode)
19674 case QImode:
19675 fn = gen_load_lockedqi;
19676 break;
19677 case HImode:
19678 fn = gen_load_lockedhi;
19679 break;
19680 case SImode:
19681 if (GET_MODE (mem) == QImode)
19682 fn = gen_load_lockedqi_si;
19683 else if (GET_MODE (mem) == HImode)
19684 fn = gen_load_lockedhi_si;
19685 else
19686 fn = gen_load_lockedsi;
19687 break;
19688 case DImode:
19689 fn = gen_load_lockeddi;
19690 break;
19691 case TImode:
19692 fn = gen_load_lockedti;
19693 break;
19694 default:
19695 gcc_unreachable ();
19697 emit_insn (fn (reg, mem));
19700 /* A subroutine of the atomic operation splitters. Emit a store-conditional
19701 instruction in MODE. */
19703 static void
19704 emit_store_conditional (enum machine_mode mode, rtx res, rtx mem, rtx val)
19706 rtx (*fn) (rtx, rtx, rtx) = NULL;
19708 switch (mode)
19710 case QImode:
19711 fn = gen_store_conditionalqi;
19712 break;
19713 case HImode:
19714 fn = gen_store_conditionalhi;
19715 break;
19716 case SImode:
19717 fn = gen_store_conditionalsi;
19718 break;
19719 case DImode:
19720 fn = gen_store_conditionaldi;
19721 break;
19722 case TImode:
19723 fn = gen_store_conditionalti;
19724 break;
19725 default:
19726 gcc_unreachable ();
19729 /* Emit sync before stwcx. to address PPC405 Erratum. */
19730 if (PPC405_ERRATUM77)
19731 emit_insn (gen_hwsync ());
19733 emit_insn (fn (res, mem, val));
19736 /* Expand barriers before and after a load_locked/store_cond sequence. */
19738 static rtx
19739 rs6000_pre_atomic_barrier (rtx mem, enum memmodel model)
19741 rtx addr = XEXP (mem, 0);
19742 int strict_p = (reload_in_progress || reload_completed);
19744 if (!legitimate_indirect_address_p (addr, strict_p)
19745 && !legitimate_indexed_address_p (addr, strict_p))
19747 addr = force_reg (Pmode, addr);
19748 mem = replace_equiv_address_nv (mem, addr);
19751 switch (model)
19753 case MEMMODEL_RELAXED:
19754 case MEMMODEL_CONSUME:
19755 case MEMMODEL_ACQUIRE:
19756 break;
19757 case MEMMODEL_RELEASE:
19758 case MEMMODEL_ACQ_REL:
19759 emit_insn (gen_lwsync ());
19760 break;
19761 case MEMMODEL_SEQ_CST:
19762 emit_insn (gen_hwsync ());
19763 break;
19764 default:
19765 gcc_unreachable ();
19767 return mem;
19770 static void
19771 rs6000_post_atomic_barrier (enum memmodel model)
19773 switch (model)
19775 case MEMMODEL_RELAXED:
19776 case MEMMODEL_CONSUME:
19777 case MEMMODEL_RELEASE:
19778 break;
19779 case MEMMODEL_ACQUIRE:
19780 case MEMMODEL_ACQ_REL:
19781 case MEMMODEL_SEQ_CST:
19782 emit_insn (gen_isync ());
19783 break;
19784 default:
19785 gcc_unreachable ();
19789 /* A subroutine of the various atomic expanders. For sub-word operations,
19790 we must adjust things to operate on SImode. Given the original MEM,
19791 return a new aligned memory. Also build and return the quantities by
19792 which to shift and mask. */
19794 static rtx
19795 rs6000_adjust_atomic_subword (rtx orig_mem, rtx *pshift, rtx *pmask)
19797 rtx addr, align, shift, mask, mem;
19798 HOST_WIDE_INT shift_mask;
19799 enum machine_mode mode = GET_MODE (orig_mem);
19801 /* For smaller modes, we have to implement this via SImode. */
19802 shift_mask = (mode == QImode ? 0x18 : 0x10);
19804 addr = XEXP (orig_mem, 0);
19805 addr = force_reg (GET_MODE (addr), addr);
19807 /* Aligned memory containing subword. Generate a new memory. We
19808 do not want any of the existing MEM_ATTR data, as we're now
19809 accessing memory outside the original object. */
19810 align = expand_simple_binop (Pmode, AND, addr, GEN_INT (-4),
19811 NULL_RTX, 1, OPTAB_LIB_WIDEN);
19812 mem = gen_rtx_MEM (SImode, align);
19813 MEM_VOLATILE_P (mem) = MEM_VOLATILE_P (orig_mem);
19814 if (MEM_ALIAS_SET (orig_mem) == ALIAS_SET_MEMORY_BARRIER)
19815 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
19817 /* Shift amount for subword relative to aligned word. */
19818 shift = gen_reg_rtx (SImode);
19819 addr = gen_lowpart (SImode, addr);
19820 emit_insn (gen_rlwinm (shift, addr, GEN_INT (3), GEN_INT (shift_mask)));
19821 if (WORDS_BIG_ENDIAN)
19822 shift = expand_simple_binop (SImode, XOR, shift, GEN_INT (shift_mask),
19823 shift, 1, OPTAB_LIB_WIDEN);
19824 *pshift = shift;
19826 /* Mask for insertion. */
19827 mask = expand_simple_binop (SImode, ASHIFT, GEN_INT (GET_MODE_MASK (mode)),
19828 shift, NULL_RTX, 1, OPTAB_LIB_WIDEN);
19829 *pmask = mask;
19831 return mem;
19834 /* A subroutine of the various atomic expanders. For sub-word operands,
19835 combine OLDVAL and NEWVAL via MASK. Returns a new pseduo. */
19837 static rtx
19838 rs6000_mask_atomic_subword (rtx oldval, rtx newval, rtx mask)
19840 rtx x;
19842 x = gen_reg_rtx (SImode);
19843 emit_insn (gen_rtx_SET (VOIDmode, x,
19844 gen_rtx_AND (SImode,
19845 gen_rtx_NOT (SImode, mask),
19846 oldval)));
19848 x = expand_simple_binop (SImode, IOR, newval, x, x, 1, OPTAB_LIB_WIDEN);
19850 return x;
19853 /* A subroutine of the various atomic expanders. For sub-word operands,
19854 extract WIDE to NARROW via SHIFT. */
19856 static void
19857 rs6000_finish_atomic_subword (rtx narrow, rtx wide, rtx shift)
19859 wide = expand_simple_binop (SImode, LSHIFTRT, wide, shift,
19860 wide, 1, OPTAB_LIB_WIDEN);
19861 emit_move_insn (narrow, gen_lowpart (GET_MODE (narrow), wide));
19864 /* Expand an atomic compare and swap operation. */
19866 void
19867 rs6000_expand_atomic_compare_and_swap (rtx operands[])
19869 rtx boolval, retval, mem, oldval, newval, cond;
19870 rtx label1, label2, x, mask, shift;
19871 enum machine_mode mode, orig_mode;
19872 enum memmodel mod_s, mod_f;
19873 bool is_weak;
19875 boolval = operands[0];
19876 retval = operands[1];
19877 mem = operands[2];
19878 oldval = operands[3];
19879 newval = operands[4];
19880 is_weak = (INTVAL (operands[5]) != 0);
19881 mod_s = (enum memmodel) INTVAL (operands[6]);
19882 mod_f = (enum memmodel) INTVAL (operands[7]);
19883 orig_mode = mode = GET_MODE (mem);
19885 mask = shift = NULL_RTX;
19886 if (mode == QImode || mode == HImode)
19888 /* Before power8, we didn't have access to lbarx/lharx, so generate a
19889 lwarx and shift/mask operations. With power8, we need to do the
19890 comparison in SImode, but the store is still done in QI/HImode. */
19891 oldval = convert_modes (SImode, mode, oldval, 1);
19893 if (!TARGET_SYNC_HI_QI)
19895 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
19897 /* Shift and mask OLDVAL into position with the word. */
19898 oldval = expand_simple_binop (SImode, ASHIFT, oldval, shift,
19899 NULL_RTX, 1, OPTAB_LIB_WIDEN);
19901 /* Shift and mask NEWVAL into position within the word. */
19902 newval = convert_modes (SImode, mode, newval, 1);
19903 newval = expand_simple_binop (SImode, ASHIFT, newval, shift,
19904 NULL_RTX, 1, OPTAB_LIB_WIDEN);
19907 /* Prepare to adjust the return value. */
19908 retval = gen_reg_rtx (SImode);
19909 mode = SImode;
19911 else if (reg_overlap_mentioned_p (retval, oldval))
19912 oldval = copy_to_reg (oldval);
19914 mem = rs6000_pre_atomic_barrier (mem, mod_s);
19916 label1 = NULL_RTX;
19917 if (!is_weak)
19919 label1 = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
19920 emit_label (XEXP (label1, 0));
19922 label2 = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
19924 emit_load_locked (mode, retval, mem);
19926 x = retval;
19927 if (mask)
19929 x = expand_simple_binop (SImode, AND, retval, mask,
19930 NULL_RTX, 1, OPTAB_LIB_WIDEN);
19933 cond = gen_reg_rtx (CCmode);
19934 /* If we have TImode, synthesize a comparison. */
19935 if (mode != TImode)
19936 x = gen_rtx_COMPARE (CCmode, x, oldval);
19937 else
19939 rtx xor1_result = gen_reg_rtx (DImode);
19940 rtx xor2_result = gen_reg_rtx (DImode);
19941 rtx or_result = gen_reg_rtx (DImode);
19942 rtx new_word0 = simplify_gen_subreg (DImode, x, TImode, 0);
19943 rtx new_word1 = simplify_gen_subreg (DImode, x, TImode, 8);
19944 rtx old_word0 = simplify_gen_subreg (DImode, oldval, TImode, 0);
19945 rtx old_word1 = simplify_gen_subreg (DImode, oldval, TImode, 8);
19947 emit_insn (gen_xordi3 (xor1_result, new_word0, old_word0));
19948 emit_insn (gen_xordi3 (xor2_result, new_word1, old_word1));
19949 emit_insn (gen_iordi3 (or_result, xor1_result, xor2_result));
19950 x = gen_rtx_COMPARE (CCmode, or_result, const0_rtx);
19953 emit_insn (gen_rtx_SET (VOIDmode, cond, x));
19955 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
19956 emit_unlikely_jump (x, label2);
19958 x = newval;
19959 if (mask)
19960 x = rs6000_mask_atomic_subword (retval, newval, mask);
19962 emit_store_conditional (orig_mode, cond, mem, x);
19964 if (!is_weak)
19966 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
19967 emit_unlikely_jump (x, label1);
19970 if (mod_f != MEMMODEL_RELAXED)
19971 emit_label (XEXP (label2, 0));
19973 rs6000_post_atomic_barrier (mod_s);
19975 if (mod_f == MEMMODEL_RELAXED)
19976 emit_label (XEXP (label2, 0));
19978 if (shift)
19979 rs6000_finish_atomic_subword (operands[1], retval, shift);
19980 else if (mode != GET_MODE (operands[1]))
19981 convert_move (operands[1], retval, 1);
19983 /* In all cases, CR0 contains EQ on success, and NE on failure. */
19984 x = gen_rtx_EQ (SImode, cond, const0_rtx);
19985 emit_insn (gen_rtx_SET (VOIDmode, boolval, x));
19988 /* Expand an atomic exchange operation. */
19990 void
19991 rs6000_expand_atomic_exchange (rtx operands[])
19993 rtx retval, mem, val, cond;
19994 enum machine_mode mode;
19995 enum memmodel model;
19996 rtx label, x, mask, shift;
19998 retval = operands[0];
19999 mem = operands[1];
20000 val = operands[2];
20001 model = (enum memmodel) INTVAL (operands[3]);
20002 mode = GET_MODE (mem);
20004 mask = shift = NULL_RTX;
20005 if (!TARGET_SYNC_HI_QI && (mode == QImode || mode == HImode))
20007 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
20009 /* Shift and mask VAL into position with the word. */
20010 val = convert_modes (SImode, mode, val, 1);
20011 val = expand_simple_binop (SImode, ASHIFT, val, shift,
20012 NULL_RTX, 1, OPTAB_LIB_WIDEN);
20014 /* Prepare to adjust the return value. */
20015 retval = gen_reg_rtx (SImode);
20016 mode = SImode;
20019 mem = rs6000_pre_atomic_barrier (mem, model);
20021 label = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
20022 emit_label (XEXP (label, 0));
20024 emit_load_locked (mode, retval, mem);
20026 x = val;
20027 if (mask)
20028 x = rs6000_mask_atomic_subword (retval, val, mask);
20030 cond = gen_reg_rtx (CCmode);
20031 emit_store_conditional (mode, cond, mem, x);
20033 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
20034 emit_unlikely_jump (x, label);
20036 rs6000_post_atomic_barrier (model);
20038 if (shift)
20039 rs6000_finish_atomic_subword (operands[0], retval, shift);
20042 /* Expand an atomic fetch-and-operate pattern. CODE is the binary operation
20043 to perform. MEM is the memory on which to operate. VAL is the second
20044 operand of the binary operator. BEFORE and AFTER are optional locations to
20045 return the value of MEM either before of after the operation. MODEL_RTX
20046 is a CONST_INT containing the memory model to use. */
20048 void
20049 rs6000_expand_atomic_op (enum rtx_code code, rtx mem, rtx val,
20050 rtx orig_before, rtx orig_after, rtx model_rtx)
20052 enum memmodel model = (enum memmodel) INTVAL (model_rtx);
20053 enum machine_mode mode = GET_MODE (mem);
20054 enum machine_mode store_mode = mode;
20055 rtx label, x, cond, mask, shift;
20056 rtx before = orig_before, after = orig_after;
20058 mask = shift = NULL_RTX;
20059 /* On power8, we want to use SImode for the operation. On previous systems,
20060 use the operation in a subword and shift/mask to get the proper byte or
20061 halfword. */
20062 if (mode == QImode || mode == HImode)
20064 if (TARGET_SYNC_HI_QI)
20066 val = convert_modes (SImode, mode, val, 1);
20068 /* Prepare to adjust the return value. */
20069 before = gen_reg_rtx (SImode);
20070 if (after)
20071 after = gen_reg_rtx (SImode);
20072 mode = SImode;
20074 else
20076 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
20078 /* Shift and mask VAL into position with the word. */
20079 val = convert_modes (SImode, mode, val, 1);
20080 val = expand_simple_binop (SImode, ASHIFT, val, shift,
20081 NULL_RTX, 1, OPTAB_LIB_WIDEN);
20083 switch (code)
20085 case IOR:
20086 case XOR:
20087 /* We've already zero-extended VAL. That is sufficient to
20088 make certain that it does not affect other bits. */
20089 mask = NULL;
20090 break;
20092 case AND:
20093 /* If we make certain that all of the other bits in VAL are
20094 set, that will be sufficient to not affect other bits. */
20095 x = gen_rtx_NOT (SImode, mask);
20096 x = gen_rtx_IOR (SImode, x, val);
20097 emit_insn (gen_rtx_SET (VOIDmode, val, x));
20098 mask = NULL;
20099 break;
20101 case NOT:
20102 case PLUS:
20103 case MINUS:
20104 /* These will all affect bits outside the field and need
20105 adjustment via MASK within the loop. */
20106 break;
20108 default:
20109 gcc_unreachable ();
20112 /* Prepare to adjust the return value. */
20113 before = gen_reg_rtx (SImode);
20114 if (after)
20115 after = gen_reg_rtx (SImode);
20116 store_mode = mode = SImode;
20120 mem = rs6000_pre_atomic_barrier (mem, model);
20122 label = gen_label_rtx ();
20123 emit_label (label);
20124 label = gen_rtx_LABEL_REF (VOIDmode, label);
20126 if (before == NULL_RTX)
20127 before = gen_reg_rtx (mode);
20129 emit_load_locked (mode, before, mem);
20131 if (code == NOT)
20133 x = expand_simple_binop (mode, AND, before, val,
20134 NULL_RTX, 1, OPTAB_LIB_WIDEN);
20135 after = expand_simple_unop (mode, NOT, x, after, 1);
20137 else
20139 after = expand_simple_binop (mode, code, before, val,
20140 after, 1, OPTAB_LIB_WIDEN);
20143 x = after;
20144 if (mask)
20146 x = expand_simple_binop (SImode, AND, after, mask,
20147 NULL_RTX, 1, OPTAB_LIB_WIDEN);
20148 x = rs6000_mask_atomic_subword (before, x, mask);
20150 else if (store_mode != mode)
20151 x = convert_modes (store_mode, mode, x, 1);
20153 cond = gen_reg_rtx (CCmode);
20154 emit_store_conditional (store_mode, cond, mem, x);
20156 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
20157 emit_unlikely_jump (x, label);
20159 rs6000_post_atomic_barrier (model);
20161 if (shift)
20163 /* QImode/HImode on machines without lbarx/lharx where we do a lwarx and
20164 then do the calcuations in a SImode register. */
20165 if (orig_before)
20166 rs6000_finish_atomic_subword (orig_before, before, shift);
20167 if (orig_after)
20168 rs6000_finish_atomic_subword (orig_after, after, shift);
20170 else if (store_mode != mode)
20172 /* QImode/HImode on machines with lbarx/lharx where we do the native
20173 operation and then do the calcuations in a SImode register. */
20174 if (orig_before)
20175 convert_move (orig_before, before, 1);
20176 if (orig_after)
20177 convert_move (orig_after, after, 1);
20179 else if (orig_after && after != orig_after)
20180 emit_move_insn (orig_after, after);
20183 /* Emit instructions to move SRC to DST. Called by splitters for
20184 multi-register moves. It will emit at most one instruction for
20185 each register that is accessed; that is, it won't emit li/lis pairs
20186 (or equivalent for 64-bit code). One of SRC or DST must be a hard
20187 register. */
20189 void
20190 rs6000_split_multireg_move (rtx dst, rtx src)
20192 /* The register number of the first register being moved. */
20193 int reg;
20194 /* The mode that is to be moved. */
20195 enum machine_mode mode;
20196 /* The mode that the move is being done in, and its size. */
20197 enum machine_mode reg_mode;
20198 int reg_mode_size;
20199 /* The number of registers that will be moved. */
20200 int nregs;
20202 reg = REG_P (dst) ? REGNO (dst) : REGNO (src);
20203 mode = GET_MODE (dst);
20204 nregs = hard_regno_nregs[reg][mode];
20205 if (FP_REGNO_P (reg))
20206 reg_mode = DECIMAL_FLOAT_MODE_P (mode) ? DDmode :
20207 ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? DFmode : SFmode);
20208 else if (ALTIVEC_REGNO_P (reg))
20209 reg_mode = V16QImode;
20210 else if (TARGET_E500_DOUBLE && mode == TFmode)
20211 reg_mode = DFmode;
20212 else
20213 reg_mode = word_mode;
20214 reg_mode_size = GET_MODE_SIZE (reg_mode);
20216 gcc_assert (reg_mode_size * nregs == GET_MODE_SIZE (mode));
20218 /* TDmode residing in FP registers is special, since the ISA requires that
20219 the lower-numbered word of a register pair is always the most significant
20220 word, even in little-endian mode. This does not match the usual subreg
20221 semantics, so we cannnot use simplify_gen_subreg in those cases. Access
20222 the appropriate constituent registers "by hand" in little-endian mode.
20224 Note we do not need to check for destructive overlap here since TDmode
20225 can only reside in even/odd register pairs. */
20226 if (FP_REGNO_P (reg) && DECIMAL_FLOAT_MODE_P (mode) && !BYTES_BIG_ENDIAN)
20228 rtx p_src, p_dst;
20229 int i;
20231 for (i = 0; i < nregs; i++)
20233 if (REG_P (src) && FP_REGNO_P (REGNO (src)))
20234 p_src = gen_rtx_REG (reg_mode, REGNO (src) + nregs - 1 - i);
20235 else
20236 p_src = simplify_gen_subreg (reg_mode, src, mode,
20237 i * reg_mode_size);
20239 if (REG_P (dst) && FP_REGNO_P (REGNO (dst)))
20240 p_dst = gen_rtx_REG (reg_mode, REGNO (dst) + nregs - 1 - i);
20241 else
20242 p_dst = simplify_gen_subreg (reg_mode, dst, mode,
20243 i * reg_mode_size);
20245 emit_insn (gen_rtx_SET (VOIDmode, p_dst, p_src));
20248 return;
20251 if (REG_P (src) && REG_P (dst) && (REGNO (src) < REGNO (dst)))
20253 /* Move register range backwards, if we might have destructive
20254 overlap. */
20255 int i;
20256 for (i = nregs - 1; i >= 0; i--)
20257 emit_insn (gen_rtx_SET (VOIDmode,
20258 simplify_gen_subreg (reg_mode, dst, mode,
20259 i * reg_mode_size),
20260 simplify_gen_subreg (reg_mode, src, mode,
20261 i * reg_mode_size)));
20263 else
20265 int i;
20266 int j = -1;
20267 bool used_update = false;
20268 rtx restore_basereg = NULL_RTX;
20270 if (MEM_P (src) && INT_REGNO_P (reg))
20272 rtx breg;
20274 if (GET_CODE (XEXP (src, 0)) == PRE_INC
20275 || GET_CODE (XEXP (src, 0)) == PRE_DEC)
20277 rtx delta_rtx;
20278 breg = XEXP (XEXP (src, 0), 0);
20279 delta_rtx = (GET_CODE (XEXP (src, 0)) == PRE_INC
20280 ? GEN_INT (GET_MODE_SIZE (GET_MODE (src)))
20281 : GEN_INT (-GET_MODE_SIZE (GET_MODE (src))));
20282 emit_insn (gen_add3_insn (breg, breg, delta_rtx));
20283 src = replace_equiv_address (src, breg);
20285 else if (! rs6000_offsettable_memref_p (src, reg_mode))
20287 if (GET_CODE (XEXP (src, 0)) == PRE_MODIFY)
20289 rtx basereg = XEXP (XEXP (src, 0), 0);
20290 if (TARGET_UPDATE)
20292 rtx ndst = simplify_gen_subreg (reg_mode, dst, mode, 0);
20293 emit_insn (gen_rtx_SET (VOIDmode, ndst,
20294 gen_rtx_MEM (reg_mode, XEXP (src, 0))));
20295 used_update = true;
20297 else
20298 emit_insn (gen_rtx_SET (VOIDmode, basereg,
20299 XEXP (XEXP (src, 0), 1)));
20300 src = replace_equiv_address (src, basereg);
20302 else
20304 rtx basereg = gen_rtx_REG (Pmode, reg);
20305 emit_insn (gen_rtx_SET (VOIDmode, basereg, XEXP (src, 0)));
20306 src = replace_equiv_address (src, basereg);
20310 breg = XEXP (src, 0);
20311 if (GET_CODE (breg) == PLUS || GET_CODE (breg) == LO_SUM)
20312 breg = XEXP (breg, 0);
20314 /* If the base register we are using to address memory is
20315 also a destination reg, then change that register last. */
20316 if (REG_P (breg)
20317 && REGNO (breg) >= REGNO (dst)
20318 && REGNO (breg) < REGNO (dst) + nregs)
20319 j = REGNO (breg) - REGNO (dst);
20321 else if (MEM_P (dst) && INT_REGNO_P (reg))
20323 rtx breg;
20325 if (GET_CODE (XEXP (dst, 0)) == PRE_INC
20326 || GET_CODE (XEXP (dst, 0)) == PRE_DEC)
20328 rtx delta_rtx;
20329 breg = XEXP (XEXP (dst, 0), 0);
20330 delta_rtx = (GET_CODE (XEXP (dst, 0)) == PRE_INC
20331 ? GEN_INT (GET_MODE_SIZE (GET_MODE (dst)))
20332 : GEN_INT (-GET_MODE_SIZE (GET_MODE (dst))));
20334 /* We have to update the breg before doing the store.
20335 Use store with update, if available. */
20337 if (TARGET_UPDATE)
20339 rtx nsrc = simplify_gen_subreg (reg_mode, src, mode, 0);
20340 emit_insn (TARGET_32BIT
20341 ? (TARGET_POWERPC64
20342 ? gen_movdi_si_update (breg, breg, delta_rtx, nsrc)
20343 : gen_movsi_update (breg, breg, delta_rtx, nsrc))
20344 : gen_movdi_di_update (breg, breg, delta_rtx, nsrc));
20345 used_update = true;
20347 else
20348 emit_insn (gen_add3_insn (breg, breg, delta_rtx));
20349 dst = replace_equiv_address (dst, breg);
20351 else if (!rs6000_offsettable_memref_p (dst, reg_mode)
20352 && GET_CODE (XEXP (dst, 0)) != LO_SUM)
20354 if (GET_CODE (XEXP (dst, 0)) == PRE_MODIFY)
20356 rtx basereg = XEXP (XEXP (dst, 0), 0);
20357 if (TARGET_UPDATE)
20359 rtx nsrc = simplify_gen_subreg (reg_mode, src, mode, 0);
20360 emit_insn (gen_rtx_SET (VOIDmode,
20361 gen_rtx_MEM (reg_mode, XEXP (dst, 0)), nsrc));
20362 used_update = true;
20364 else
20365 emit_insn (gen_rtx_SET (VOIDmode, basereg,
20366 XEXP (XEXP (dst, 0), 1)));
20367 dst = replace_equiv_address (dst, basereg);
20369 else
20371 rtx basereg = XEXP (XEXP (dst, 0), 0);
20372 rtx offsetreg = XEXP (XEXP (dst, 0), 1);
20373 gcc_assert (GET_CODE (XEXP (dst, 0)) == PLUS
20374 && REG_P (basereg)
20375 && REG_P (offsetreg)
20376 && REGNO (basereg) != REGNO (offsetreg));
20377 if (REGNO (basereg) == 0)
20379 rtx tmp = offsetreg;
20380 offsetreg = basereg;
20381 basereg = tmp;
20383 emit_insn (gen_add3_insn (basereg, basereg, offsetreg));
20384 restore_basereg = gen_sub3_insn (basereg, basereg, offsetreg);
20385 dst = replace_equiv_address (dst, basereg);
20388 else if (GET_CODE (XEXP (dst, 0)) != LO_SUM)
20389 gcc_assert (rs6000_offsettable_memref_p (dst, reg_mode));
20392 for (i = 0; i < nregs; i++)
20394 /* Calculate index to next subword. */
20395 ++j;
20396 if (j == nregs)
20397 j = 0;
20399 /* If compiler already emitted move of first word by
20400 store with update, no need to do anything. */
20401 if (j == 0 && used_update)
20402 continue;
20404 emit_insn (gen_rtx_SET (VOIDmode,
20405 simplify_gen_subreg (reg_mode, dst, mode,
20406 j * reg_mode_size),
20407 simplify_gen_subreg (reg_mode, src, mode,
20408 j * reg_mode_size)));
20410 if (restore_basereg != NULL_RTX)
20411 emit_insn (restore_basereg);
20416 /* This page contains routines that are used to determine what the
20417 function prologue and epilogue code will do and write them out. */
20419 static inline bool
20420 save_reg_p (int r)
20422 return !call_used_regs[r] && df_regs_ever_live_p (r);
20425 /* Return the first fixed-point register that is required to be
20426 saved. 32 if none. */
20429 first_reg_to_save (void)
20431 int first_reg;
20433 /* Find lowest numbered live register. */
20434 for (first_reg = 13; first_reg <= 31; first_reg++)
20435 if (save_reg_p (first_reg))
20436 break;
20438 if (first_reg > RS6000_PIC_OFFSET_TABLE_REGNUM
20439 && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
20440 || (DEFAULT_ABI == ABI_DARWIN && flag_pic)
20441 || (TARGET_TOC && TARGET_MINIMAL_TOC))
20442 && df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))
20443 first_reg = RS6000_PIC_OFFSET_TABLE_REGNUM;
20445 #if TARGET_MACHO
20446 if (flag_pic
20447 && crtl->uses_pic_offset_table
20448 && first_reg > RS6000_PIC_OFFSET_TABLE_REGNUM)
20449 return RS6000_PIC_OFFSET_TABLE_REGNUM;
20450 #endif
20452 return first_reg;
20455 /* Similar, for FP regs. */
20458 first_fp_reg_to_save (void)
20460 int first_reg;
20462 /* Find lowest numbered live register. */
20463 for (first_reg = 14 + 32; first_reg <= 63; first_reg++)
20464 if (save_reg_p (first_reg))
20465 break;
20467 return first_reg;
20470 /* Similar, for AltiVec regs. */
20472 static int
20473 first_altivec_reg_to_save (void)
20475 int i;
20477 /* Stack frame remains as is unless we are in AltiVec ABI. */
20478 if (! TARGET_ALTIVEC_ABI)
20479 return LAST_ALTIVEC_REGNO + 1;
20481 /* On Darwin, the unwind routines are compiled without
20482 TARGET_ALTIVEC, and use save_world to save/restore the
20483 altivec registers when necessary. */
20484 if (DEFAULT_ABI == ABI_DARWIN && crtl->calls_eh_return
20485 && ! TARGET_ALTIVEC)
20486 return FIRST_ALTIVEC_REGNO + 20;
20488 /* Find lowest numbered live register. */
20489 for (i = FIRST_ALTIVEC_REGNO + 20; i <= LAST_ALTIVEC_REGNO; ++i)
20490 if (save_reg_p (i))
20491 break;
20493 return i;
20496 /* Return a 32-bit mask of the AltiVec registers we need to set in
20497 VRSAVE. Bit n of the return value is 1 if Vn is live. The MSB in
20498 the 32-bit word is 0. */
20500 static unsigned int
20501 compute_vrsave_mask (void)
20503 unsigned int i, mask = 0;
20505 /* On Darwin, the unwind routines are compiled without
20506 TARGET_ALTIVEC, and use save_world to save/restore the
20507 call-saved altivec registers when necessary. */
20508 if (DEFAULT_ABI == ABI_DARWIN && crtl->calls_eh_return
20509 && ! TARGET_ALTIVEC)
20510 mask |= 0xFFF;
20512 /* First, find out if we use _any_ altivec registers. */
20513 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
20514 if (df_regs_ever_live_p (i))
20515 mask |= ALTIVEC_REG_BIT (i);
20517 if (mask == 0)
20518 return mask;
20520 /* Next, remove the argument registers from the set. These must
20521 be in the VRSAVE mask set by the caller, so we don't need to add
20522 them in again. More importantly, the mask we compute here is
20523 used to generate CLOBBERs in the set_vrsave insn, and we do not
20524 wish the argument registers to die. */
20525 for (i = crtl->args.info.vregno - 1; i >= ALTIVEC_ARG_MIN_REG; --i)
20526 mask &= ~ALTIVEC_REG_BIT (i);
20528 /* Similarly, remove the return value from the set. */
20530 bool yes = false;
20531 diddle_return_value (is_altivec_return_reg, &yes);
20532 if (yes)
20533 mask &= ~ALTIVEC_REG_BIT (ALTIVEC_ARG_RETURN);
20536 return mask;
20539 /* For a very restricted set of circumstances, we can cut down the
20540 size of prologues/epilogues by calling our own save/restore-the-world
20541 routines. */
20543 static void
20544 compute_save_world_info (rs6000_stack_t *info_ptr)
20546 info_ptr->world_save_p = 1;
20547 info_ptr->world_save_p
20548 = (WORLD_SAVE_P (info_ptr)
20549 && DEFAULT_ABI == ABI_DARWIN
20550 && !cfun->has_nonlocal_label
20551 && info_ptr->first_fp_reg_save == FIRST_SAVED_FP_REGNO
20552 && info_ptr->first_gp_reg_save == FIRST_SAVED_GP_REGNO
20553 && info_ptr->first_altivec_reg_save == FIRST_SAVED_ALTIVEC_REGNO
20554 && info_ptr->cr_save_p);
20556 /* This will not work in conjunction with sibcalls. Make sure there
20557 are none. (This check is expensive, but seldom executed.) */
20558 if (WORLD_SAVE_P (info_ptr))
20560 rtx insn;
20561 for (insn = get_last_insn_anywhere (); insn; insn = PREV_INSN (insn))
20562 if (CALL_P (insn) && SIBLING_CALL_P (insn))
20564 info_ptr->world_save_p = 0;
20565 break;
20569 if (WORLD_SAVE_P (info_ptr))
20571 /* Even if we're not touching VRsave, make sure there's room on the
20572 stack for it, if it looks like we're calling SAVE_WORLD, which
20573 will attempt to save it. */
20574 info_ptr->vrsave_size = 4;
20576 /* If we are going to save the world, we need to save the link register too. */
20577 info_ptr->lr_save_p = 1;
20579 /* "Save" the VRsave register too if we're saving the world. */
20580 if (info_ptr->vrsave_mask == 0)
20581 info_ptr->vrsave_mask = compute_vrsave_mask ();
20583 /* Because the Darwin register save/restore routines only handle
20584 F14 .. F31 and V20 .. V31 as per the ABI, perform a consistency
20585 check. */
20586 gcc_assert (info_ptr->first_fp_reg_save >= FIRST_SAVED_FP_REGNO
20587 && (info_ptr->first_altivec_reg_save
20588 >= FIRST_SAVED_ALTIVEC_REGNO));
20590 return;
20594 static void
20595 is_altivec_return_reg (rtx reg, void *xyes)
20597 bool *yes = (bool *) xyes;
20598 if (REGNO (reg) == ALTIVEC_ARG_RETURN)
20599 *yes = true;
20603 /* Look for user-defined global regs in the range FIRST to LAST-1.
20604 We should not restore these, and so cannot use lmw or out-of-line
20605 restore functions if there are any. We also can't save them
20606 (well, emit frame notes for them), because frame unwinding during
20607 exception handling will restore saved registers. */
20609 static bool
20610 global_regs_p (unsigned first, unsigned last)
20612 while (first < last)
20613 if (global_regs[first++])
20614 return true;
20615 return false;
20618 /* Determine the strategy for savings/restoring registers. */
20620 enum {
20621 SAVRES_MULTIPLE = 0x1,
20622 SAVE_INLINE_FPRS = 0x2,
20623 SAVE_INLINE_GPRS = 0x4,
20624 REST_INLINE_FPRS = 0x8,
20625 REST_INLINE_GPRS = 0x10,
20626 SAVE_NOINLINE_GPRS_SAVES_LR = 0x20,
20627 SAVE_NOINLINE_FPRS_SAVES_LR = 0x40,
20628 REST_NOINLINE_FPRS_DOESNT_RESTORE_LR = 0x80,
20629 SAVE_INLINE_VRS = 0x100,
20630 REST_INLINE_VRS = 0x200
20633 static int
20634 rs6000_savres_strategy (rs6000_stack_t *info,
20635 bool using_static_chain_p)
20637 int strategy = 0;
20638 bool lr_save_p;
20640 if (TARGET_MULTIPLE
20641 && !TARGET_POWERPC64
20642 && !(TARGET_SPE_ABI && info->spe_64bit_regs_used)
20643 && info->first_gp_reg_save < 31
20644 && !global_regs_p (info->first_gp_reg_save, 32))
20645 strategy |= SAVRES_MULTIPLE;
20647 if (crtl->calls_eh_return
20648 || cfun->machine->ra_need_lr)
20649 strategy |= (SAVE_INLINE_FPRS | REST_INLINE_FPRS
20650 | SAVE_INLINE_GPRS | REST_INLINE_GPRS
20651 | SAVE_INLINE_VRS | REST_INLINE_VRS);
20653 if (info->first_fp_reg_save == 64
20654 /* The out-of-line FP routines use double-precision stores;
20655 we can't use those routines if we don't have such stores. */
20656 || (TARGET_HARD_FLOAT && !TARGET_DOUBLE_FLOAT)
20657 || global_regs_p (info->first_fp_reg_save, 64))
20658 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
20660 if (info->first_gp_reg_save == 32
20661 || (!(strategy & SAVRES_MULTIPLE)
20662 && global_regs_p (info->first_gp_reg_save, 32)))
20663 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
20665 if (info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1
20666 || global_regs_p (info->first_altivec_reg_save, LAST_ALTIVEC_REGNO + 1))
20667 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
20669 /* Define cutoff for using out-of-line functions to save registers. */
20670 if (DEFAULT_ABI == ABI_V4 || TARGET_ELF)
20672 if (!optimize_size)
20674 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
20675 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
20676 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
20678 else
20680 /* Prefer out-of-line restore if it will exit. */
20681 if (info->first_fp_reg_save > 61)
20682 strategy |= SAVE_INLINE_FPRS;
20683 if (info->first_gp_reg_save > 29)
20685 if (info->first_fp_reg_save == 64)
20686 strategy |= SAVE_INLINE_GPRS;
20687 else
20688 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
20690 if (info->first_altivec_reg_save == LAST_ALTIVEC_REGNO)
20691 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
20694 else if (DEFAULT_ABI == ABI_DARWIN)
20696 if (info->first_fp_reg_save > 60)
20697 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
20698 if (info->first_gp_reg_save > 29)
20699 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
20700 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
20702 else
20704 gcc_checking_assert (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2);
20705 if (info->first_fp_reg_save > 61)
20706 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
20707 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
20708 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
20711 /* Don't bother to try to save things out-of-line if r11 is occupied
20712 by the static chain. It would require too much fiddling and the
20713 static chain is rarely used anyway. FPRs are saved w.r.t the stack
20714 pointer on Darwin, and AIX uses r1 or r12. */
20715 if (using_static_chain_p
20716 && (DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN))
20717 strategy |= ((DEFAULT_ABI == ABI_DARWIN ? 0 : SAVE_INLINE_FPRS)
20718 | SAVE_INLINE_GPRS
20719 | SAVE_INLINE_VRS | REST_INLINE_VRS);
20721 /* We can only use the out-of-line routines to restore if we've
20722 saved all the registers from first_fp_reg_save in the prologue.
20723 Otherwise, we risk loading garbage. */
20724 if ((strategy & (SAVE_INLINE_FPRS | REST_INLINE_FPRS)) == SAVE_INLINE_FPRS)
20726 int i;
20728 for (i = info->first_fp_reg_save; i < 64; i++)
20729 if (!save_reg_p (i))
20731 strategy |= REST_INLINE_FPRS;
20732 break;
20736 /* If we are going to use store multiple, then don't even bother
20737 with the out-of-line routines, since the store-multiple
20738 instruction will always be smaller. */
20739 if ((strategy & SAVRES_MULTIPLE))
20740 strategy |= SAVE_INLINE_GPRS;
20742 /* info->lr_save_p isn't yet set if the only reason lr needs to be
20743 saved is an out-of-line save or restore. Set up the value for
20744 the next test (excluding out-of-line gpr restore). */
20745 lr_save_p = (info->lr_save_p
20746 || !(strategy & SAVE_INLINE_GPRS)
20747 || !(strategy & SAVE_INLINE_FPRS)
20748 || !(strategy & SAVE_INLINE_VRS)
20749 || !(strategy & REST_INLINE_FPRS)
20750 || !(strategy & REST_INLINE_VRS));
20752 /* The situation is more complicated with load multiple. We'd
20753 prefer to use the out-of-line routines for restores, since the
20754 "exit" out-of-line routines can handle the restore of LR and the
20755 frame teardown. However if doesn't make sense to use the
20756 out-of-line routine if that is the only reason we'd need to save
20757 LR, and we can't use the "exit" out-of-line gpr restore if we
20758 have saved some fprs; In those cases it is advantageous to use
20759 load multiple when available. */
20760 if ((strategy & SAVRES_MULTIPLE)
20761 && (!lr_save_p
20762 || info->first_fp_reg_save != 64))
20763 strategy |= REST_INLINE_GPRS;
20765 /* Saving CR interferes with the exit routines used on the SPE, so
20766 just punt here. */
20767 if (TARGET_SPE_ABI
20768 && info->spe_64bit_regs_used
20769 && info->cr_save_p)
20770 strategy |= REST_INLINE_GPRS;
20772 /* We can only use load multiple or the out-of-line routines to
20773 restore if we've used store multiple or out-of-line routines
20774 in the prologue, i.e. if we've saved all the registers from
20775 first_gp_reg_save. Otherwise, we risk loading garbage. */
20776 if ((strategy & (SAVE_INLINE_GPRS | REST_INLINE_GPRS | SAVRES_MULTIPLE))
20777 == SAVE_INLINE_GPRS)
20779 int i;
20781 for (i = info->first_gp_reg_save; i < 32; i++)
20782 if (!save_reg_p (i))
20784 strategy |= REST_INLINE_GPRS;
20785 break;
20789 if (TARGET_ELF && TARGET_64BIT)
20791 if (!(strategy & SAVE_INLINE_FPRS))
20792 strategy |= SAVE_NOINLINE_FPRS_SAVES_LR;
20793 else if (!(strategy & SAVE_INLINE_GPRS)
20794 && info->first_fp_reg_save == 64)
20795 strategy |= SAVE_NOINLINE_GPRS_SAVES_LR;
20797 else if (TARGET_AIX && !(strategy & REST_INLINE_FPRS))
20798 strategy |= REST_NOINLINE_FPRS_DOESNT_RESTORE_LR;
20800 if (TARGET_MACHO && !(strategy & SAVE_INLINE_FPRS))
20801 strategy |= SAVE_NOINLINE_FPRS_SAVES_LR;
20803 return strategy;
20806 /* Calculate the stack information for the current function. This is
20807 complicated by having two separate calling sequences, the AIX calling
20808 sequence and the V.4 calling sequence.
20810 AIX (and Darwin/Mac OS X) stack frames look like:
20811 32-bit 64-bit
20812 SP----> +---------------------------------------+
20813 | back chain to caller | 0 0
20814 +---------------------------------------+
20815 | saved CR | 4 8 (8-11)
20816 +---------------------------------------+
20817 | saved LR | 8 16
20818 +---------------------------------------+
20819 | reserved for compilers | 12 24
20820 +---------------------------------------+
20821 | reserved for binders | 16 32
20822 +---------------------------------------+
20823 | saved TOC pointer | 20 40
20824 +---------------------------------------+
20825 | Parameter save area (P) | 24 48
20826 +---------------------------------------+
20827 | Alloca space (A) | 24+P etc.
20828 +---------------------------------------+
20829 | Local variable space (L) | 24+P+A
20830 +---------------------------------------+
20831 | Float/int conversion temporary (X) | 24+P+A+L
20832 +---------------------------------------+
20833 | Save area for AltiVec registers (W) | 24+P+A+L+X
20834 +---------------------------------------+
20835 | AltiVec alignment padding (Y) | 24+P+A+L+X+W
20836 +---------------------------------------+
20837 | Save area for VRSAVE register (Z) | 24+P+A+L+X+W+Y
20838 +---------------------------------------+
20839 | Save area for GP registers (G) | 24+P+A+X+L+X+W+Y+Z
20840 +---------------------------------------+
20841 | Save area for FP registers (F) | 24+P+A+X+L+X+W+Y+Z+G
20842 +---------------------------------------+
20843 old SP->| back chain to caller's caller |
20844 +---------------------------------------+
20846 The required alignment for AIX configurations is two words (i.e., 8
20847 or 16 bytes).
20849 The ELFv2 ABI is a variant of the AIX ABI. Stack frames look like:
20851 SP----> +---------------------------------------+
20852 | Back chain to caller | 0
20853 +---------------------------------------+
20854 | Save area for CR | 8
20855 +---------------------------------------+
20856 | Saved LR | 16
20857 +---------------------------------------+
20858 | Saved TOC pointer | 24
20859 +---------------------------------------+
20860 | Parameter save area (P) | 32
20861 +---------------------------------------+
20862 | Alloca space (A) | 32+P
20863 +---------------------------------------+
20864 | Local variable space (L) | 32+P+A
20865 +---------------------------------------+
20866 | Save area for AltiVec registers (W) | 32+P+A+L
20867 +---------------------------------------+
20868 | AltiVec alignment padding (Y) | 32+P+A+L+W
20869 +---------------------------------------+
20870 | Save area for GP registers (G) | 32+P+A+L+W+Y
20871 +---------------------------------------+
20872 | Save area for FP registers (F) | 32+P+A+L+W+Y+G
20873 +---------------------------------------+
20874 old SP->| back chain to caller's caller | 32+P+A+L+W+Y+G+F
20875 +---------------------------------------+
20878 V.4 stack frames look like:
20880 SP----> +---------------------------------------+
20881 | back chain to caller | 0
20882 +---------------------------------------+
20883 | caller's saved LR | 4
20884 +---------------------------------------+
20885 | Parameter save area (P) | 8
20886 +---------------------------------------+
20887 | Alloca space (A) | 8+P
20888 +---------------------------------------+
20889 | Varargs save area (V) | 8+P+A
20890 +---------------------------------------+
20891 | Local variable space (L) | 8+P+A+V
20892 +---------------------------------------+
20893 | Float/int conversion temporary (X) | 8+P+A+V+L
20894 +---------------------------------------+
20895 | Save area for AltiVec registers (W) | 8+P+A+V+L+X
20896 +---------------------------------------+
20897 | AltiVec alignment padding (Y) | 8+P+A+V+L+X+W
20898 +---------------------------------------+
20899 | Save area for VRSAVE register (Z) | 8+P+A+V+L+X+W+Y
20900 +---------------------------------------+
20901 | SPE: area for 64-bit GP registers |
20902 +---------------------------------------+
20903 | SPE alignment padding |
20904 +---------------------------------------+
20905 | saved CR (C) | 8+P+A+V+L+X+W+Y+Z
20906 +---------------------------------------+
20907 | Save area for GP registers (G) | 8+P+A+V+L+X+W+Y+Z+C
20908 +---------------------------------------+
20909 | Save area for FP registers (F) | 8+P+A+V+L+X+W+Y+Z+C+G
20910 +---------------------------------------+
20911 old SP->| back chain to caller's caller |
20912 +---------------------------------------+
20914 The required alignment for V.4 is 16 bytes, or 8 bytes if -meabi is
20915 given. (But note below and in sysv4.h that we require only 8 and
20916 may round up the size of our stack frame anyways. The historical
20917 reason is early versions of powerpc-linux which didn't properly
20918 align the stack at program startup. A happy side-effect is that
20919 -mno-eabi libraries can be used with -meabi programs.)
20921 The EABI configuration defaults to the V.4 layout. However,
20922 the stack alignment requirements may differ. If -mno-eabi is not
20923 given, the required stack alignment is 8 bytes; if -mno-eabi is
20924 given, the required alignment is 16 bytes. (But see V.4 comment
20925 above.) */
20927 #ifndef ABI_STACK_BOUNDARY
20928 #define ABI_STACK_BOUNDARY STACK_BOUNDARY
20929 #endif
20931 static rs6000_stack_t *
20932 rs6000_stack_info (void)
20934 rs6000_stack_t *info_ptr = &stack_info;
20935 int reg_size = TARGET_32BIT ? 4 : 8;
20936 int ehrd_size;
20937 int ehcr_size;
20938 int save_align;
20939 int first_gp;
20940 HOST_WIDE_INT non_fixed_size;
20941 bool using_static_chain_p;
20943 if (reload_completed && info_ptr->reload_completed)
20944 return info_ptr;
20946 memset (info_ptr, 0, sizeof (*info_ptr));
20947 info_ptr->reload_completed = reload_completed;
20949 if (TARGET_SPE)
20951 /* Cache value so we don't rescan instruction chain over and over. */
20952 if (cfun->machine->insn_chain_scanned_p == 0)
20953 cfun->machine->insn_chain_scanned_p
20954 = spe_func_has_64bit_regs_p () + 1;
20955 info_ptr->spe_64bit_regs_used = cfun->machine->insn_chain_scanned_p - 1;
20958 /* Select which calling sequence. */
20959 info_ptr->abi = DEFAULT_ABI;
20961 /* Calculate which registers need to be saved & save area size. */
20962 info_ptr->first_gp_reg_save = first_reg_to_save ();
20963 /* Assume that we will have to save RS6000_PIC_OFFSET_TABLE_REGNUM,
20964 even if it currently looks like we won't. Reload may need it to
20965 get at a constant; if so, it will have already created a constant
20966 pool entry for it. */
20967 if (((TARGET_TOC && TARGET_MINIMAL_TOC)
20968 || (flag_pic == 1 && DEFAULT_ABI == ABI_V4)
20969 || (flag_pic && DEFAULT_ABI == ABI_DARWIN))
20970 && crtl->uses_const_pool
20971 && info_ptr->first_gp_reg_save > RS6000_PIC_OFFSET_TABLE_REGNUM)
20972 first_gp = RS6000_PIC_OFFSET_TABLE_REGNUM;
20973 else
20974 first_gp = info_ptr->first_gp_reg_save;
20976 info_ptr->gp_size = reg_size * (32 - first_gp);
20978 /* For the SPE, we have an additional upper 32-bits on each GPR.
20979 Ideally we should save the entire 64-bits only when the upper
20980 half is used in SIMD instructions. Since we only record
20981 registers live (not the size they are used in), this proves
20982 difficult because we'd have to traverse the instruction chain at
20983 the right time, taking reload into account. This is a real pain,
20984 so we opt to save the GPRs in 64-bits always if but one register
20985 gets used in 64-bits. Otherwise, all the registers in the frame
20986 get saved in 32-bits.
20988 So... since when we save all GPRs (except the SP) in 64-bits, the
20989 traditional GP save area will be empty. */
20990 if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
20991 info_ptr->gp_size = 0;
20993 info_ptr->first_fp_reg_save = first_fp_reg_to_save ();
20994 info_ptr->fp_size = 8 * (64 - info_ptr->first_fp_reg_save);
20996 info_ptr->first_altivec_reg_save = first_altivec_reg_to_save ();
20997 info_ptr->altivec_size = 16 * (LAST_ALTIVEC_REGNO + 1
20998 - info_ptr->first_altivec_reg_save);
21000 /* Does this function call anything? */
21001 info_ptr->calls_p = (! crtl->is_leaf
21002 || cfun->machine->ra_needs_full_frame);
21004 /* Determine if we need to save the condition code registers. */
21005 if (df_regs_ever_live_p (CR2_REGNO)
21006 || df_regs_ever_live_p (CR3_REGNO)
21007 || df_regs_ever_live_p (CR4_REGNO))
21009 info_ptr->cr_save_p = 1;
21010 if (DEFAULT_ABI == ABI_V4)
21011 info_ptr->cr_size = reg_size;
21014 /* If the current function calls __builtin_eh_return, then we need
21015 to allocate stack space for registers that will hold data for
21016 the exception handler. */
21017 if (crtl->calls_eh_return)
21019 unsigned int i;
21020 for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; ++i)
21021 continue;
21023 /* SPE saves EH registers in 64-bits. */
21024 ehrd_size = i * (TARGET_SPE_ABI
21025 && info_ptr->spe_64bit_regs_used != 0
21026 ? UNITS_PER_SPE_WORD : UNITS_PER_WORD);
21028 else
21029 ehrd_size = 0;
21031 /* In the ELFv2 ABI, we also need to allocate space for separate
21032 CR field save areas if the function calls __builtin_eh_return. */
21033 if (DEFAULT_ABI == ABI_ELFv2 && crtl->calls_eh_return)
21035 /* This hard-codes that we have three call-saved CR fields. */
21036 ehcr_size = 3 * reg_size;
21037 /* We do *not* use the regular CR save mechanism. */
21038 info_ptr->cr_save_p = 0;
21040 else
21041 ehcr_size = 0;
21043 /* Determine various sizes. */
21044 info_ptr->reg_size = reg_size;
21045 info_ptr->fixed_size = RS6000_SAVE_AREA;
21046 info_ptr->vars_size = RS6000_ALIGN (get_frame_size (), 8);
21047 info_ptr->parm_size = RS6000_ALIGN (crtl->outgoing_args_size,
21048 TARGET_ALTIVEC ? 16 : 8);
21049 if (FRAME_GROWS_DOWNWARD)
21050 info_ptr->vars_size
21051 += RS6000_ALIGN (info_ptr->fixed_size + info_ptr->vars_size
21052 + info_ptr->parm_size,
21053 ABI_STACK_BOUNDARY / BITS_PER_UNIT)
21054 - (info_ptr->fixed_size + info_ptr->vars_size
21055 + info_ptr->parm_size);
21057 if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
21058 info_ptr->spe_gp_size = 8 * (32 - first_gp);
21059 else
21060 info_ptr->spe_gp_size = 0;
21062 if (TARGET_ALTIVEC_ABI)
21063 info_ptr->vrsave_mask = compute_vrsave_mask ();
21064 else
21065 info_ptr->vrsave_mask = 0;
21067 if (TARGET_ALTIVEC_VRSAVE && info_ptr->vrsave_mask)
21068 info_ptr->vrsave_size = 4;
21069 else
21070 info_ptr->vrsave_size = 0;
21072 compute_save_world_info (info_ptr);
21074 /* Calculate the offsets. */
21075 switch (DEFAULT_ABI)
21077 case ABI_NONE:
21078 default:
21079 gcc_unreachable ();
21081 case ABI_AIX:
21082 case ABI_ELFv2:
21083 case ABI_DARWIN:
21084 info_ptr->fp_save_offset = - info_ptr->fp_size;
21085 info_ptr->gp_save_offset = info_ptr->fp_save_offset - info_ptr->gp_size;
21087 if (TARGET_ALTIVEC_ABI)
21089 info_ptr->vrsave_save_offset
21090 = info_ptr->gp_save_offset - info_ptr->vrsave_size;
21092 /* Align stack so vector save area is on a quadword boundary.
21093 The padding goes above the vectors. */
21094 if (info_ptr->altivec_size != 0)
21095 info_ptr->altivec_padding_size
21096 = info_ptr->vrsave_save_offset & 0xF;
21097 else
21098 info_ptr->altivec_padding_size = 0;
21100 info_ptr->altivec_save_offset
21101 = info_ptr->vrsave_save_offset
21102 - info_ptr->altivec_padding_size
21103 - info_ptr->altivec_size;
21104 gcc_assert (info_ptr->altivec_size == 0
21105 || info_ptr->altivec_save_offset % 16 == 0);
21107 /* Adjust for AltiVec case. */
21108 info_ptr->ehrd_offset = info_ptr->altivec_save_offset - ehrd_size;
21110 else
21111 info_ptr->ehrd_offset = info_ptr->gp_save_offset - ehrd_size;
21113 info_ptr->ehcr_offset = info_ptr->ehrd_offset - ehcr_size;
21114 info_ptr->cr_save_offset = reg_size; /* first word when 64-bit. */
21115 info_ptr->lr_save_offset = 2*reg_size;
21116 break;
21118 case ABI_V4:
21119 info_ptr->fp_save_offset = - info_ptr->fp_size;
21120 info_ptr->gp_save_offset = info_ptr->fp_save_offset - info_ptr->gp_size;
21121 info_ptr->cr_save_offset = info_ptr->gp_save_offset - info_ptr->cr_size;
21123 if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
21125 /* Align stack so SPE GPR save area is aligned on a
21126 double-word boundary. */
21127 if (info_ptr->spe_gp_size != 0 && info_ptr->cr_save_offset != 0)
21128 info_ptr->spe_padding_size
21129 = 8 - (-info_ptr->cr_save_offset % 8);
21130 else
21131 info_ptr->spe_padding_size = 0;
21133 info_ptr->spe_gp_save_offset
21134 = info_ptr->cr_save_offset
21135 - info_ptr->spe_padding_size
21136 - info_ptr->spe_gp_size;
21138 /* Adjust for SPE case. */
21139 info_ptr->ehrd_offset = info_ptr->spe_gp_save_offset;
21141 else if (TARGET_ALTIVEC_ABI)
21143 info_ptr->vrsave_save_offset
21144 = info_ptr->cr_save_offset - info_ptr->vrsave_size;
21146 /* Align stack so vector save area is on a quadword boundary. */
21147 if (info_ptr->altivec_size != 0)
21148 info_ptr->altivec_padding_size
21149 = 16 - (-info_ptr->vrsave_save_offset % 16);
21150 else
21151 info_ptr->altivec_padding_size = 0;
21153 info_ptr->altivec_save_offset
21154 = info_ptr->vrsave_save_offset
21155 - info_ptr->altivec_padding_size
21156 - info_ptr->altivec_size;
21158 /* Adjust for AltiVec case. */
21159 info_ptr->ehrd_offset = info_ptr->altivec_save_offset;
21161 else
21162 info_ptr->ehrd_offset = info_ptr->cr_save_offset;
21163 info_ptr->ehrd_offset -= ehrd_size;
21164 info_ptr->lr_save_offset = reg_size;
21165 break;
21168 save_align = (TARGET_ALTIVEC_ABI || DEFAULT_ABI == ABI_DARWIN) ? 16 : 8;
21169 info_ptr->save_size = RS6000_ALIGN (info_ptr->fp_size
21170 + info_ptr->gp_size
21171 + info_ptr->altivec_size
21172 + info_ptr->altivec_padding_size
21173 + info_ptr->spe_gp_size
21174 + info_ptr->spe_padding_size
21175 + ehrd_size
21176 + ehcr_size
21177 + info_ptr->cr_size
21178 + info_ptr->vrsave_size,
21179 save_align);
21181 non_fixed_size = (info_ptr->vars_size
21182 + info_ptr->parm_size
21183 + info_ptr->save_size);
21185 info_ptr->total_size = RS6000_ALIGN (non_fixed_size + info_ptr->fixed_size,
21186 ABI_STACK_BOUNDARY / BITS_PER_UNIT);
21188 /* Determine if we need to save the link register. */
21189 if (info_ptr->calls_p
21190 || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
21191 && crtl->profile
21192 && !TARGET_PROFILE_KERNEL)
21193 || (DEFAULT_ABI == ABI_V4 && cfun->calls_alloca)
21194 #ifdef TARGET_RELOCATABLE
21195 || (TARGET_RELOCATABLE && (get_pool_size () != 0))
21196 #endif
21197 || rs6000_ra_ever_killed ())
21198 info_ptr->lr_save_p = 1;
21200 using_static_chain_p = (cfun->static_chain_decl != NULL_TREE
21201 && df_regs_ever_live_p (STATIC_CHAIN_REGNUM)
21202 && call_used_regs[STATIC_CHAIN_REGNUM]);
21203 info_ptr->savres_strategy = rs6000_savres_strategy (info_ptr,
21204 using_static_chain_p);
21206 if (!(info_ptr->savres_strategy & SAVE_INLINE_GPRS)
21207 || !(info_ptr->savres_strategy & SAVE_INLINE_FPRS)
21208 || !(info_ptr->savres_strategy & SAVE_INLINE_VRS)
21209 || !(info_ptr->savres_strategy & REST_INLINE_GPRS)
21210 || !(info_ptr->savres_strategy & REST_INLINE_FPRS)
21211 || !(info_ptr->savres_strategy & REST_INLINE_VRS))
21212 info_ptr->lr_save_p = 1;
21214 if (info_ptr->lr_save_p)
21215 df_set_regs_ever_live (LR_REGNO, true);
21217 /* Determine if we need to allocate any stack frame:
21219 For AIX we need to push the stack if a frame pointer is needed
21220 (because the stack might be dynamically adjusted), if we are
21221 debugging, if we make calls, or if the sum of fp_save, gp_save,
21222 and local variables are more than the space needed to save all
21223 non-volatile registers: 32-bit: 18*8 + 19*4 = 220 or 64-bit: 18*8
21224 + 18*8 = 288 (GPR13 reserved).
21226 For V.4 we don't have the stack cushion that AIX uses, but assume
21227 that the debugger can handle stackless frames. */
21229 if (info_ptr->calls_p)
21230 info_ptr->push_p = 1;
21232 else if (DEFAULT_ABI == ABI_V4)
21233 info_ptr->push_p = non_fixed_size != 0;
21235 else if (frame_pointer_needed)
21236 info_ptr->push_p = 1;
21238 else if (TARGET_XCOFF && write_symbols != NO_DEBUG)
21239 info_ptr->push_p = 1;
21241 else
21242 info_ptr->push_p = non_fixed_size > (TARGET_32BIT ? 220 : 288);
21244 /* Zero offsets if we're not saving those registers. */
21245 if (info_ptr->fp_size == 0)
21246 info_ptr->fp_save_offset = 0;
21248 if (info_ptr->gp_size == 0)
21249 info_ptr->gp_save_offset = 0;
21251 if (! TARGET_ALTIVEC_ABI || info_ptr->altivec_size == 0)
21252 info_ptr->altivec_save_offset = 0;
21254 /* Zero VRSAVE offset if not saved and restored. */
21255 if (! TARGET_ALTIVEC_VRSAVE || info_ptr->vrsave_mask == 0)
21256 info_ptr->vrsave_save_offset = 0;
21258 if (! TARGET_SPE_ABI
21259 || info_ptr->spe_64bit_regs_used == 0
21260 || info_ptr->spe_gp_size == 0)
21261 info_ptr->spe_gp_save_offset = 0;
21263 if (! info_ptr->lr_save_p)
21264 info_ptr->lr_save_offset = 0;
21266 if (! info_ptr->cr_save_p)
21267 info_ptr->cr_save_offset = 0;
21269 return info_ptr;
21272 /* Return true if the current function uses any GPRs in 64-bit SIMD
21273 mode. */
21275 static bool
21276 spe_func_has_64bit_regs_p (void)
21278 rtx insns, insn;
21280 /* Functions that save and restore all the call-saved registers will
21281 need to save/restore the registers in 64-bits. */
21282 if (crtl->calls_eh_return
21283 || cfun->calls_setjmp
21284 || crtl->has_nonlocal_goto)
21285 return true;
21287 insns = get_insns ();
21289 for (insn = NEXT_INSN (insns); insn != NULL_RTX; insn = NEXT_INSN (insn))
21291 if (INSN_P (insn))
21293 rtx i;
21295 /* FIXME: This should be implemented with attributes...
21297 (set_attr "spe64" "true")....then,
21298 if (get_spe64(insn)) return true;
21300 It's the only reliable way to do the stuff below. */
21302 i = PATTERN (insn);
21303 if (GET_CODE (i) == SET)
21305 enum machine_mode mode = GET_MODE (SET_SRC (i));
21307 if (SPE_VECTOR_MODE (mode))
21308 return true;
21309 if (TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode))
21310 return true;
21315 return false;
21318 static void
21319 debug_stack_info (rs6000_stack_t *info)
21321 const char *abi_string;
21323 if (! info)
21324 info = rs6000_stack_info ();
21326 fprintf (stderr, "\nStack information for function %s:\n",
21327 ((current_function_decl && DECL_NAME (current_function_decl))
21328 ? IDENTIFIER_POINTER (DECL_NAME (current_function_decl))
21329 : "<unknown>"));
21331 switch (info->abi)
21333 default: abi_string = "Unknown"; break;
21334 case ABI_NONE: abi_string = "NONE"; break;
21335 case ABI_AIX: abi_string = "AIX"; break;
21336 case ABI_ELFv2: abi_string = "ELFv2"; break;
21337 case ABI_DARWIN: abi_string = "Darwin"; break;
21338 case ABI_V4: abi_string = "V.4"; break;
21341 fprintf (stderr, "\tABI = %5s\n", abi_string);
21343 if (TARGET_ALTIVEC_ABI)
21344 fprintf (stderr, "\tALTIVEC ABI extensions enabled.\n");
21346 if (TARGET_SPE_ABI)
21347 fprintf (stderr, "\tSPE ABI extensions enabled.\n");
21349 if (info->first_gp_reg_save != 32)
21350 fprintf (stderr, "\tfirst_gp_reg_save = %5d\n", info->first_gp_reg_save);
21352 if (info->first_fp_reg_save != 64)
21353 fprintf (stderr, "\tfirst_fp_reg_save = %5d\n", info->first_fp_reg_save);
21355 if (info->first_altivec_reg_save <= LAST_ALTIVEC_REGNO)
21356 fprintf (stderr, "\tfirst_altivec_reg_save = %5d\n",
21357 info->first_altivec_reg_save);
21359 if (info->lr_save_p)
21360 fprintf (stderr, "\tlr_save_p = %5d\n", info->lr_save_p);
21362 if (info->cr_save_p)
21363 fprintf (stderr, "\tcr_save_p = %5d\n", info->cr_save_p);
21365 if (info->vrsave_mask)
21366 fprintf (stderr, "\tvrsave_mask = 0x%x\n", info->vrsave_mask);
21368 if (info->push_p)
21369 fprintf (stderr, "\tpush_p = %5d\n", info->push_p);
21371 if (info->calls_p)
21372 fprintf (stderr, "\tcalls_p = %5d\n", info->calls_p);
21374 if (info->gp_save_offset)
21375 fprintf (stderr, "\tgp_save_offset = %5d\n", info->gp_save_offset);
21377 if (info->fp_save_offset)
21378 fprintf (stderr, "\tfp_save_offset = %5d\n", info->fp_save_offset);
21380 if (info->altivec_save_offset)
21381 fprintf (stderr, "\taltivec_save_offset = %5d\n",
21382 info->altivec_save_offset);
21384 if (info->spe_gp_save_offset)
21385 fprintf (stderr, "\tspe_gp_save_offset = %5d\n",
21386 info->spe_gp_save_offset);
21388 if (info->vrsave_save_offset)
21389 fprintf (stderr, "\tvrsave_save_offset = %5d\n",
21390 info->vrsave_save_offset);
21392 if (info->lr_save_offset)
21393 fprintf (stderr, "\tlr_save_offset = %5d\n", info->lr_save_offset);
21395 if (info->cr_save_offset)
21396 fprintf (stderr, "\tcr_save_offset = %5d\n", info->cr_save_offset);
21398 if (info->varargs_save_offset)
21399 fprintf (stderr, "\tvarargs_save_offset = %5d\n", info->varargs_save_offset);
21401 if (info->total_size)
21402 fprintf (stderr, "\ttotal_size = "HOST_WIDE_INT_PRINT_DEC"\n",
21403 info->total_size);
21405 if (info->vars_size)
21406 fprintf (stderr, "\tvars_size = "HOST_WIDE_INT_PRINT_DEC"\n",
21407 info->vars_size);
21409 if (info->parm_size)
21410 fprintf (stderr, "\tparm_size = %5d\n", info->parm_size);
21412 if (info->fixed_size)
21413 fprintf (stderr, "\tfixed_size = %5d\n", info->fixed_size);
21415 if (info->gp_size)
21416 fprintf (stderr, "\tgp_size = %5d\n", info->gp_size);
21418 if (info->spe_gp_size)
21419 fprintf (stderr, "\tspe_gp_size = %5d\n", info->spe_gp_size);
21421 if (info->fp_size)
21422 fprintf (stderr, "\tfp_size = %5d\n", info->fp_size);
21424 if (info->altivec_size)
21425 fprintf (stderr, "\taltivec_size = %5d\n", info->altivec_size);
21427 if (info->vrsave_size)
21428 fprintf (stderr, "\tvrsave_size = %5d\n", info->vrsave_size);
21430 if (info->altivec_padding_size)
21431 fprintf (stderr, "\taltivec_padding_size= %5d\n",
21432 info->altivec_padding_size);
21434 if (info->spe_padding_size)
21435 fprintf (stderr, "\tspe_padding_size = %5d\n",
21436 info->spe_padding_size);
21438 if (info->cr_size)
21439 fprintf (stderr, "\tcr_size = %5d\n", info->cr_size);
21441 if (info->save_size)
21442 fprintf (stderr, "\tsave_size = %5d\n", info->save_size);
21444 if (info->reg_size != 4)
21445 fprintf (stderr, "\treg_size = %5d\n", info->reg_size);
21447 fprintf (stderr, "\tsave-strategy = %04x\n", info->savres_strategy);
21449 fprintf (stderr, "\n");
21453 rs6000_return_addr (int count, rtx frame)
21455 /* Currently we don't optimize very well between prolog and body
21456 code and for PIC code the code can be actually quite bad, so
21457 don't try to be too clever here. */
21458 if (count != 0
21459 || ((DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN) && flag_pic))
21461 cfun->machine->ra_needs_full_frame = 1;
21463 return
21464 gen_rtx_MEM
21465 (Pmode,
21466 memory_address
21467 (Pmode,
21468 plus_constant (Pmode,
21469 copy_to_reg
21470 (gen_rtx_MEM (Pmode,
21471 memory_address (Pmode, frame))),
21472 RETURN_ADDRESS_OFFSET)));
21475 cfun->machine->ra_need_lr = 1;
21476 return get_hard_reg_initial_val (Pmode, LR_REGNO);
21479 /* Say whether a function is a candidate for sibcall handling or not. */
21481 static bool
21482 rs6000_function_ok_for_sibcall (tree decl, tree exp)
21484 tree fntype;
21486 if (decl)
21487 fntype = TREE_TYPE (decl);
21488 else
21489 fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (exp)));
21491 /* We can't do it if the called function has more vector parameters
21492 than the current function; there's nowhere to put the VRsave code. */
21493 if (TARGET_ALTIVEC_ABI
21494 && TARGET_ALTIVEC_VRSAVE
21495 && !(decl && decl == current_function_decl))
21497 function_args_iterator args_iter;
21498 tree type;
21499 int nvreg = 0;
21501 /* Functions with vector parameters are required to have a
21502 prototype, so the argument type info must be available
21503 here. */
21504 FOREACH_FUNCTION_ARGS(fntype, type, args_iter)
21505 if (TREE_CODE (type) == VECTOR_TYPE
21506 && ALTIVEC_OR_VSX_VECTOR_MODE (TYPE_MODE (type)))
21507 nvreg++;
21509 FOREACH_FUNCTION_ARGS(TREE_TYPE (current_function_decl), type, args_iter)
21510 if (TREE_CODE (type) == VECTOR_TYPE
21511 && ALTIVEC_OR_VSX_VECTOR_MODE (TYPE_MODE (type)))
21512 nvreg--;
21514 if (nvreg > 0)
21515 return false;
21518 /* Under the AIX or ELFv2 ABIs we can't allow calls to non-local
21519 functions, because the callee may have a different TOC pointer to
21520 the caller and there's no way to ensure we restore the TOC when
21521 we return. With the secure-plt SYSV ABI we can't make non-local
21522 calls when -fpic/PIC because the plt call stubs use r30. */
21523 if (DEFAULT_ABI == ABI_DARWIN
21524 || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
21525 && decl
21526 && !DECL_EXTERNAL (decl)
21527 && (*targetm.binds_local_p) (decl))
21528 || (DEFAULT_ABI == ABI_V4
21529 && (!TARGET_SECURE_PLT
21530 || !flag_pic
21531 || (decl
21532 && (*targetm.binds_local_p) (decl)))))
21534 tree attr_list = TYPE_ATTRIBUTES (fntype);
21536 if (!lookup_attribute ("longcall", attr_list)
21537 || lookup_attribute ("shortcall", attr_list))
21538 return true;
21541 return false;
21544 static int
21545 rs6000_ra_ever_killed (void)
21547 rtx top;
21548 rtx reg;
21549 rtx insn;
21551 if (cfun->is_thunk)
21552 return 0;
21554 if (cfun->machine->lr_save_state)
21555 return cfun->machine->lr_save_state - 1;
21557 /* regs_ever_live has LR marked as used if any sibcalls are present,
21558 but this should not force saving and restoring in the
21559 pro/epilogue. Likewise, reg_set_between_p thinks a sibcall
21560 clobbers LR, so that is inappropriate. */
21562 /* Also, the prologue can generate a store into LR that
21563 doesn't really count, like this:
21565 move LR->R0
21566 bcl to set PIC register
21567 move LR->R31
21568 move R0->LR
21570 When we're called from the epilogue, we need to avoid counting
21571 this as a store. */
21573 push_topmost_sequence ();
21574 top = get_insns ();
21575 pop_topmost_sequence ();
21576 reg = gen_rtx_REG (Pmode, LR_REGNO);
21578 for (insn = NEXT_INSN (top); insn != NULL_RTX; insn = NEXT_INSN (insn))
21580 if (INSN_P (insn))
21582 if (CALL_P (insn))
21584 if (!SIBLING_CALL_P (insn))
21585 return 1;
21587 else if (find_regno_note (insn, REG_INC, LR_REGNO))
21588 return 1;
21589 else if (set_of (reg, insn) != NULL_RTX
21590 && !prologue_epilogue_contains (insn))
21591 return 1;
21594 return 0;
21597 /* Emit instructions needed to load the TOC register.
21598 This is only needed when TARGET_TOC, TARGET_MINIMAL_TOC, and there is
21599 a constant pool; or for SVR4 -fpic. */
21601 void
21602 rs6000_emit_load_toc_table (int fromprolog)
21604 rtx dest;
21605 dest = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
21607 if (TARGET_ELF && TARGET_SECURE_PLT && DEFAULT_ABI == ABI_V4 && flag_pic)
21609 char buf[30];
21610 rtx lab, tmp1, tmp2, got;
21612 lab = gen_label_rtx ();
21613 ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (lab));
21614 lab = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
21615 if (flag_pic == 2)
21616 got = gen_rtx_SYMBOL_REF (Pmode, toc_label_name);
21617 else
21618 got = rs6000_got_sym ();
21619 tmp1 = tmp2 = dest;
21620 if (!fromprolog)
21622 tmp1 = gen_reg_rtx (Pmode);
21623 tmp2 = gen_reg_rtx (Pmode);
21625 emit_insn (gen_load_toc_v4_PIC_1 (lab));
21626 emit_move_insn (tmp1, gen_rtx_REG (Pmode, LR_REGNO));
21627 emit_insn (gen_load_toc_v4_PIC_3b (tmp2, tmp1, got, lab));
21628 emit_insn (gen_load_toc_v4_PIC_3c (dest, tmp2, got, lab));
21630 else if (TARGET_ELF && DEFAULT_ABI == ABI_V4 && flag_pic == 1)
21632 emit_insn (gen_load_toc_v4_pic_si ());
21633 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
21635 else if (TARGET_ELF && DEFAULT_ABI == ABI_V4 && flag_pic == 2)
21637 char buf[30];
21638 rtx temp0 = (fromprolog
21639 ? gen_rtx_REG (Pmode, 0)
21640 : gen_reg_rtx (Pmode));
21642 if (fromprolog)
21644 rtx symF, symL;
21646 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
21647 symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
21649 ASM_GENERATE_INTERNAL_LABEL (buf, "LCL", rs6000_pic_labelno);
21650 symL = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
21652 emit_insn (gen_load_toc_v4_PIC_1 (symF));
21653 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
21654 emit_insn (gen_load_toc_v4_PIC_2 (temp0, dest, symL, symF));
21656 else
21658 rtx tocsym, lab;
21660 tocsym = gen_rtx_SYMBOL_REF (Pmode, toc_label_name);
21661 lab = gen_label_rtx ();
21662 emit_insn (gen_load_toc_v4_PIC_1b (tocsym, lab));
21663 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
21664 if (TARGET_LINK_STACK)
21665 emit_insn (gen_addsi3 (dest, dest, GEN_INT (4)));
21666 emit_move_insn (temp0, gen_rtx_MEM (Pmode, dest));
21668 emit_insn (gen_addsi3 (dest, temp0, dest));
21670 else if (TARGET_ELF && !TARGET_AIX && flag_pic == 0 && TARGET_MINIMAL_TOC)
21672 /* This is for AIX code running in non-PIC ELF32. */
21673 char buf[30];
21674 rtx realsym;
21675 ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
21676 realsym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
21678 emit_insn (gen_elf_high (dest, realsym));
21679 emit_insn (gen_elf_low (dest, dest, realsym));
21681 else
21683 gcc_assert (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2);
21685 if (TARGET_32BIT)
21686 emit_insn (gen_load_toc_aix_si (dest));
21687 else
21688 emit_insn (gen_load_toc_aix_di (dest));
21692 /* Emit instructions to restore the link register after determining where
21693 its value has been stored. */
21695 void
21696 rs6000_emit_eh_reg_restore (rtx source, rtx scratch)
21698 rs6000_stack_t *info = rs6000_stack_info ();
21699 rtx operands[2];
21701 operands[0] = source;
21702 operands[1] = scratch;
21704 if (info->lr_save_p)
21706 rtx frame_rtx = stack_pointer_rtx;
21707 HOST_WIDE_INT sp_offset = 0;
21708 rtx tmp;
21710 if (frame_pointer_needed
21711 || cfun->calls_alloca
21712 || info->total_size > 32767)
21714 tmp = gen_frame_mem (Pmode, frame_rtx);
21715 emit_move_insn (operands[1], tmp);
21716 frame_rtx = operands[1];
21718 else if (info->push_p)
21719 sp_offset = info->total_size;
21721 tmp = plus_constant (Pmode, frame_rtx,
21722 info->lr_save_offset + sp_offset);
21723 tmp = gen_frame_mem (Pmode, tmp);
21724 emit_move_insn (tmp, operands[0]);
21726 else
21727 emit_move_insn (gen_rtx_REG (Pmode, LR_REGNO), operands[0]);
21729 /* Freeze lr_save_p. We've just emitted rtl that depends on the
21730 state of lr_save_p so any change from here on would be a bug. In
21731 particular, stop rs6000_ra_ever_killed from considering the SET
21732 of lr we may have added just above. */
21733 cfun->machine->lr_save_state = info->lr_save_p + 1;
21736 static GTY(()) alias_set_type set = -1;
21738 alias_set_type
21739 get_TOC_alias_set (void)
21741 if (set == -1)
21742 set = new_alias_set ();
21743 return set;
21746 /* This returns nonzero if the current function uses the TOC. This is
21747 determined by the presence of (use (unspec ... UNSPEC_TOC)), which
21748 is generated by the ABI_V4 load_toc_* patterns. */
21749 #if TARGET_ELF
21750 static int
21751 uses_TOC (void)
21753 rtx insn;
21755 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
21756 if (INSN_P (insn))
21758 rtx pat = PATTERN (insn);
21759 int i;
21761 if (GET_CODE (pat) == PARALLEL)
21762 for (i = 0; i < XVECLEN (pat, 0); i++)
21764 rtx sub = XVECEXP (pat, 0, i);
21765 if (GET_CODE (sub) == USE)
21767 sub = XEXP (sub, 0);
21768 if (GET_CODE (sub) == UNSPEC
21769 && XINT (sub, 1) == UNSPEC_TOC)
21770 return 1;
21774 return 0;
21776 #endif
21779 create_TOC_reference (rtx symbol, rtx largetoc_reg)
21781 rtx tocrel, tocreg, hi;
21783 if (TARGET_DEBUG_ADDR)
21785 if (GET_CODE (symbol) == SYMBOL_REF)
21786 fprintf (stderr, "\ncreate_TOC_reference, (symbol_ref %s)\n",
21787 XSTR (symbol, 0));
21788 else
21790 fprintf (stderr, "\ncreate_TOC_reference, code %s:\n",
21791 GET_RTX_NAME (GET_CODE (symbol)));
21792 debug_rtx (symbol);
21796 if (!can_create_pseudo_p ())
21797 df_set_regs_ever_live (TOC_REGISTER, true);
21799 tocreg = gen_rtx_REG (Pmode, TOC_REGISTER);
21800 tocrel = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, symbol, tocreg), UNSPEC_TOCREL);
21801 if (TARGET_CMODEL == CMODEL_SMALL || can_create_pseudo_p ())
21802 return tocrel;
21804 hi = gen_rtx_HIGH (Pmode, copy_rtx (tocrel));
21805 if (largetoc_reg != NULL)
21807 emit_move_insn (largetoc_reg, hi);
21808 hi = largetoc_reg;
21810 return gen_rtx_LO_SUM (Pmode, hi, tocrel);
21813 /* Issue assembly directives that create a reference to the given DWARF
21814 FRAME_TABLE_LABEL from the current function section. */
21815 void
21816 rs6000_aix_asm_output_dwarf_table_ref (char * frame_table_label)
21818 fprintf (asm_out_file, "\t.ref %s\n",
21819 (* targetm.strip_name_encoding) (frame_table_label));
21822 /* This ties together stack memory (MEM with an alias set of frame_alias_set)
21823 and the change to the stack pointer. */
21825 static void
21826 rs6000_emit_stack_tie (rtx fp, bool hard_frame_needed)
21828 rtvec p;
21829 int i;
21830 rtx regs[3];
21832 i = 0;
21833 regs[i++] = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
21834 if (hard_frame_needed)
21835 regs[i++] = gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM);
21836 if (!(REGNO (fp) == STACK_POINTER_REGNUM
21837 || (hard_frame_needed
21838 && REGNO (fp) == HARD_FRAME_POINTER_REGNUM)))
21839 regs[i++] = fp;
21841 p = rtvec_alloc (i);
21842 while (--i >= 0)
21844 rtx mem = gen_frame_mem (BLKmode, regs[i]);
21845 RTVEC_ELT (p, i) = gen_rtx_SET (VOIDmode, mem, const0_rtx);
21848 emit_insn (gen_stack_tie (gen_rtx_PARALLEL (VOIDmode, p)));
21851 /* Emit the correct code for allocating stack space, as insns.
21852 If COPY_REG, make sure a copy of the old frame is left there.
21853 The generated code may use hard register 0 as a temporary. */
21855 static void
21856 rs6000_emit_allocate_stack (HOST_WIDE_INT size, rtx copy_reg, int copy_off)
21858 rtx insn;
21859 rtx stack_reg = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
21860 rtx tmp_reg = gen_rtx_REG (Pmode, 0);
21861 rtx todec = gen_int_mode (-size, Pmode);
21862 rtx par, set, mem;
21864 if (INTVAL (todec) != -size)
21866 warning (0, "stack frame too large");
21867 emit_insn (gen_trap ());
21868 return;
21871 if (crtl->limit_stack)
21873 if (REG_P (stack_limit_rtx)
21874 && REGNO (stack_limit_rtx) > 1
21875 && REGNO (stack_limit_rtx) <= 31)
21877 emit_insn (gen_add3_insn (tmp_reg, stack_limit_rtx, GEN_INT (size)));
21878 emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
21879 const0_rtx));
21881 else if (GET_CODE (stack_limit_rtx) == SYMBOL_REF
21882 && TARGET_32BIT
21883 && DEFAULT_ABI == ABI_V4)
21885 rtx toload = gen_rtx_CONST (VOIDmode,
21886 gen_rtx_PLUS (Pmode,
21887 stack_limit_rtx,
21888 GEN_INT (size)));
21890 emit_insn (gen_elf_high (tmp_reg, toload));
21891 emit_insn (gen_elf_low (tmp_reg, tmp_reg, toload));
21892 emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
21893 const0_rtx));
21895 else
21896 warning (0, "stack limit expression is not supported");
21899 if (copy_reg)
21901 if (copy_off != 0)
21902 emit_insn (gen_add3_insn (copy_reg, stack_reg, GEN_INT (copy_off)));
21903 else
21904 emit_move_insn (copy_reg, stack_reg);
21907 if (size > 32767)
21909 /* Need a note here so that try_split doesn't get confused. */
21910 if (get_last_insn () == NULL_RTX)
21911 emit_note (NOTE_INSN_DELETED);
21912 insn = emit_move_insn (tmp_reg, todec);
21913 try_split (PATTERN (insn), insn, 0);
21914 todec = tmp_reg;
21917 insn = emit_insn (TARGET_32BIT
21918 ? gen_movsi_update_stack (stack_reg, stack_reg,
21919 todec, stack_reg)
21920 : gen_movdi_di_update_stack (stack_reg, stack_reg,
21921 todec, stack_reg));
21922 /* Since we didn't use gen_frame_mem to generate the MEM, grab
21923 it now and set the alias set/attributes. The above gen_*_update
21924 calls will generate a PARALLEL with the MEM set being the first
21925 operation. */
21926 par = PATTERN (insn);
21927 gcc_assert (GET_CODE (par) == PARALLEL);
21928 set = XVECEXP (par, 0, 0);
21929 gcc_assert (GET_CODE (set) == SET);
21930 mem = SET_DEST (set);
21931 gcc_assert (MEM_P (mem));
21932 MEM_NOTRAP_P (mem) = 1;
21933 set_mem_alias_set (mem, get_frame_alias_set ());
21935 RTX_FRAME_RELATED_P (insn) = 1;
21936 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
21937 gen_rtx_SET (VOIDmode, stack_reg,
21938 gen_rtx_PLUS (Pmode, stack_reg,
21939 GEN_INT (-size))));
21942 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
21944 #if PROBE_INTERVAL > 32768
21945 #error Cannot use indexed addressing mode for stack probing
21946 #endif
21948 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
21949 inclusive. These are offsets from the current stack pointer. */
21951 static void
21952 rs6000_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
21954 /* See if we have a constant small number of probes to generate. If so,
21955 that's the easy case. */
21956 if (first + size <= 32768)
21958 HOST_WIDE_INT i;
21960 /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
21961 it exceeds SIZE. If only one probe is needed, this will not
21962 generate any code. Then probe at FIRST + SIZE. */
21963 for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
21964 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
21965 -(first + i)));
21967 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
21968 -(first + size)));
21971 /* Otherwise, do the same as above, but in a loop. Note that we must be
21972 extra careful with variables wrapping around because we might be at
21973 the very top (or the very bottom) of the address space and we have
21974 to be able to handle this case properly; in particular, we use an
21975 equality test for the loop condition. */
21976 else
21978 HOST_WIDE_INT rounded_size;
21979 rtx r12 = gen_rtx_REG (Pmode, 12);
21980 rtx r0 = gen_rtx_REG (Pmode, 0);
21982 /* Sanity check for the addressing mode we're going to use. */
21983 gcc_assert (first <= 32768);
21985 /* Step 1: round SIZE to the previous multiple of the interval. */
21987 rounded_size = size & -PROBE_INTERVAL;
21990 /* Step 2: compute initial and final value of the loop counter. */
21992 /* TEST_ADDR = SP + FIRST. */
21993 emit_insn (gen_rtx_SET (VOIDmode, r12,
21994 plus_constant (Pmode, stack_pointer_rtx,
21995 -first)));
21997 /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE. */
21998 if (rounded_size > 32768)
22000 emit_move_insn (r0, GEN_INT (-rounded_size));
22001 emit_insn (gen_rtx_SET (VOIDmode, r0,
22002 gen_rtx_PLUS (Pmode, r12, r0)));
22004 else
22005 emit_insn (gen_rtx_SET (VOIDmode, r0,
22006 plus_constant (Pmode, r12, -rounded_size)));
22009 /* Step 3: the loop
22011 while (TEST_ADDR != LAST_ADDR)
22013 TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
22014 probe at TEST_ADDR
22017 probes at FIRST + N * PROBE_INTERVAL for values of N from 1
22018 until it is equal to ROUNDED_SIZE. */
22020 if (TARGET_64BIT)
22021 emit_insn (gen_probe_stack_rangedi (r12, r12, r0));
22022 else
22023 emit_insn (gen_probe_stack_rangesi (r12, r12, r0));
22026 /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
22027 that SIZE is equal to ROUNDED_SIZE. */
22029 if (size != rounded_size)
22030 emit_stack_probe (plus_constant (Pmode, r12, rounded_size - size));
22034 /* Probe a range of stack addresses from REG1 to REG2 inclusive. These are
22035 absolute addresses. */
22037 const char *
22038 output_probe_stack_range (rtx reg1, rtx reg2)
22040 static int labelno = 0;
22041 char loop_lab[32], end_lab[32];
22042 rtx xops[2];
22044 ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno);
22045 ASM_GENERATE_INTERNAL_LABEL (end_lab, "LPSRE", labelno++);
22047 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
22049 /* Jump to END_LAB if TEST_ADDR == LAST_ADDR. */
22050 xops[0] = reg1;
22051 xops[1] = reg2;
22052 if (TARGET_64BIT)
22053 output_asm_insn ("cmpd 0,%0,%1", xops);
22054 else
22055 output_asm_insn ("cmpw 0,%0,%1", xops);
22057 fputs ("\tbeq 0,", asm_out_file);
22058 assemble_name_raw (asm_out_file, end_lab);
22059 fputc ('\n', asm_out_file);
22061 /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL. */
22062 xops[1] = GEN_INT (-PROBE_INTERVAL);
22063 output_asm_insn ("addi %0,%0,%1", xops);
22065 /* Probe at TEST_ADDR and branch. */
22066 xops[1] = gen_rtx_REG (Pmode, 0);
22067 output_asm_insn ("stw %1,0(%0)", xops);
22068 fprintf (asm_out_file, "\tb ");
22069 assemble_name_raw (asm_out_file, loop_lab);
22070 fputc ('\n', asm_out_file);
22072 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, end_lab);
22074 return "";
22077 /* Add to 'insn' a note which is PATTERN (INSN) but with REG replaced
22078 with (plus:P (reg 1) VAL), and with REG2 replaced with RREG if REG2
22079 is not NULL. It would be nice if dwarf2out_frame_debug_expr could
22080 deduce these equivalences by itself so it wasn't necessary to hold
22081 its hand so much. Don't be tempted to always supply d2_f_d_e with
22082 the actual cfa register, ie. r31 when we are using a hard frame
22083 pointer. That fails when saving regs off r1, and sched moves the
22084 r31 setup past the reg saves. */
22086 static rtx
22087 rs6000_frame_related (rtx insn, rtx reg, HOST_WIDE_INT val,
22088 rtx reg2, rtx rreg, rtx split_reg)
22090 rtx real, temp;
22092 if (REGNO (reg) == STACK_POINTER_REGNUM && reg2 == NULL_RTX)
22094 /* No need for any replacement. Just set RTX_FRAME_RELATED_P. */
22095 int i;
22097 gcc_checking_assert (val == 0);
22098 real = PATTERN (insn);
22099 if (GET_CODE (real) == PARALLEL)
22100 for (i = 0; i < XVECLEN (real, 0); i++)
22101 if (GET_CODE (XVECEXP (real, 0, i)) == SET)
22103 rtx set = XVECEXP (real, 0, i);
22105 RTX_FRAME_RELATED_P (set) = 1;
22107 RTX_FRAME_RELATED_P (insn) = 1;
22108 return insn;
22111 /* copy_rtx will not make unique copies of registers, so we need to
22112 ensure we don't have unwanted sharing here. */
22113 if (reg == reg2)
22114 reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
22116 if (reg == rreg)
22117 reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
22119 real = copy_rtx (PATTERN (insn));
22121 if (reg2 != NULL_RTX)
22122 real = replace_rtx (real, reg2, rreg);
22124 if (REGNO (reg) == STACK_POINTER_REGNUM)
22125 gcc_checking_assert (val == 0);
22126 else
22127 real = replace_rtx (real, reg,
22128 gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode,
22129 STACK_POINTER_REGNUM),
22130 GEN_INT (val)));
22132 /* We expect that 'real' is either a SET or a PARALLEL containing
22133 SETs (and possibly other stuff). In a PARALLEL, all the SETs
22134 are important so they all have to be marked RTX_FRAME_RELATED_P. */
22136 if (GET_CODE (real) == SET)
22138 rtx set = real;
22140 temp = simplify_rtx (SET_SRC (set));
22141 if (temp)
22142 SET_SRC (set) = temp;
22143 temp = simplify_rtx (SET_DEST (set));
22144 if (temp)
22145 SET_DEST (set) = temp;
22146 if (GET_CODE (SET_DEST (set)) == MEM)
22148 temp = simplify_rtx (XEXP (SET_DEST (set), 0));
22149 if (temp)
22150 XEXP (SET_DEST (set), 0) = temp;
22153 else
22155 int i;
22157 gcc_assert (GET_CODE (real) == PARALLEL);
22158 for (i = 0; i < XVECLEN (real, 0); i++)
22159 if (GET_CODE (XVECEXP (real, 0, i)) == SET)
22161 rtx set = XVECEXP (real, 0, i);
22163 temp = simplify_rtx (SET_SRC (set));
22164 if (temp)
22165 SET_SRC (set) = temp;
22166 temp = simplify_rtx (SET_DEST (set));
22167 if (temp)
22168 SET_DEST (set) = temp;
22169 if (GET_CODE (SET_DEST (set)) == MEM)
22171 temp = simplify_rtx (XEXP (SET_DEST (set), 0));
22172 if (temp)
22173 XEXP (SET_DEST (set), 0) = temp;
22175 RTX_FRAME_RELATED_P (set) = 1;
22179 /* If a store insn has been split into multiple insns, the
22180 true source register is given by split_reg. */
22181 if (split_reg != NULL_RTX)
22182 real = gen_rtx_SET (VOIDmode, SET_DEST (real), split_reg);
22184 RTX_FRAME_RELATED_P (insn) = 1;
22185 add_reg_note (insn, REG_FRAME_RELATED_EXPR, real);
22187 return insn;
22190 /* Returns an insn that has a vrsave set operation with the
22191 appropriate CLOBBERs. */
22193 static rtx
22194 generate_set_vrsave (rtx reg, rs6000_stack_t *info, int epiloguep)
22196 int nclobs, i;
22197 rtx insn, clobs[TOTAL_ALTIVEC_REGS + 1];
22198 rtx vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
22200 clobs[0]
22201 = gen_rtx_SET (VOIDmode,
22202 vrsave,
22203 gen_rtx_UNSPEC_VOLATILE (SImode,
22204 gen_rtvec (2, reg, vrsave),
22205 UNSPECV_SET_VRSAVE));
22207 nclobs = 1;
22209 /* We need to clobber the registers in the mask so the scheduler
22210 does not move sets to VRSAVE before sets of AltiVec registers.
22212 However, if the function receives nonlocal gotos, reload will set
22213 all call saved registers live. We will end up with:
22215 (set (reg 999) (mem))
22216 (parallel [ (set (reg vrsave) (unspec blah))
22217 (clobber (reg 999))])
22219 The clobber will cause the store into reg 999 to be dead, and
22220 flow will attempt to delete an epilogue insn. In this case, we
22221 need an unspec use/set of the register. */
22223 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
22224 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
22226 if (!epiloguep || call_used_regs [i])
22227 clobs[nclobs++] = gen_rtx_CLOBBER (VOIDmode,
22228 gen_rtx_REG (V4SImode, i));
22229 else
22231 rtx reg = gen_rtx_REG (V4SImode, i);
22233 clobs[nclobs++]
22234 = gen_rtx_SET (VOIDmode,
22235 reg,
22236 gen_rtx_UNSPEC (V4SImode,
22237 gen_rtvec (1, reg), 27));
22241 insn = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nclobs));
22243 for (i = 0; i < nclobs; ++i)
22244 XVECEXP (insn, 0, i) = clobs[i];
22246 return insn;
22249 static rtx
22250 gen_frame_set (rtx reg, rtx frame_reg, int offset, bool store)
22252 rtx addr, mem;
22254 addr = gen_rtx_PLUS (Pmode, frame_reg, GEN_INT (offset));
22255 mem = gen_frame_mem (GET_MODE (reg), addr);
22256 return gen_rtx_SET (VOIDmode, store ? mem : reg, store ? reg : mem);
22259 static rtx
22260 gen_frame_load (rtx reg, rtx frame_reg, int offset)
22262 return gen_frame_set (reg, frame_reg, offset, false);
22265 static rtx
22266 gen_frame_store (rtx reg, rtx frame_reg, int offset)
22268 return gen_frame_set (reg, frame_reg, offset, true);
22271 /* Save a register into the frame, and emit RTX_FRAME_RELATED_P notes.
22272 Save REGNO into [FRAME_REG + OFFSET] in mode MODE. */
22274 static rtx
22275 emit_frame_save (rtx frame_reg, enum machine_mode mode,
22276 unsigned int regno, int offset, HOST_WIDE_INT frame_reg_to_sp)
22278 rtx reg, insn;
22280 /* Some cases that need register indexed addressing. */
22281 gcc_checking_assert (!((TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
22282 || (TARGET_VSX && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
22283 || (TARGET_E500_DOUBLE && mode == DFmode)
22284 || (TARGET_SPE_ABI
22285 && SPE_VECTOR_MODE (mode)
22286 && !SPE_CONST_OFFSET_OK (offset))));
22288 reg = gen_rtx_REG (mode, regno);
22289 insn = emit_insn (gen_frame_store (reg, frame_reg, offset));
22290 return rs6000_frame_related (insn, frame_reg, frame_reg_to_sp,
22291 NULL_RTX, NULL_RTX, NULL_RTX);
22294 /* Emit an offset memory reference suitable for a frame store, while
22295 converting to a valid addressing mode. */
22297 static rtx
22298 gen_frame_mem_offset (enum machine_mode mode, rtx reg, int offset)
22300 rtx int_rtx, offset_rtx;
22302 int_rtx = GEN_INT (offset);
22304 if ((TARGET_SPE_ABI && SPE_VECTOR_MODE (mode) && !SPE_CONST_OFFSET_OK (offset))
22305 || (TARGET_E500_DOUBLE && mode == DFmode))
22307 offset_rtx = gen_rtx_REG (Pmode, FIXED_SCRATCH);
22308 emit_move_insn (offset_rtx, int_rtx);
22310 else
22311 offset_rtx = int_rtx;
22313 return gen_frame_mem (mode, gen_rtx_PLUS (Pmode, reg, offset_rtx));
22316 #ifndef TARGET_FIX_AND_CONTINUE
22317 #define TARGET_FIX_AND_CONTINUE 0
22318 #endif
22320 /* It's really GPR 13 or 14, FPR 14 and VR 20. We need the smallest. */
22321 #define FIRST_SAVRES_REGISTER FIRST_SAVED_GP_REGNO
22322 #define LAST_SAVRES_REGISTER 31
22323 #define N_SAVRES_REGISTERS (LAST_SAVRES_REGISTER - FIRST_SAVRES_REGISTER + 1)
22325 enum {
22326 SAVRES_LR = 0x1,
22327 SAVRES_SAVE = 0x2,
22328 SAVRES_REG = 0x0c,
22329 SAVRES_GPR = 0,
22330 SAVRES_FPR = 4,
22331 SAVRES_VR = 8
22334 static GTY(()) rtx savres_routine_syms[N_SAVRES_REGISTERS][12];
22336 /* Temporary holding space for an out-of-line register save/restore
22337 routine name. */
22338 static char savres_routine_name[30];
22340 /* Return the name for an out-of-line register save/restore routine.
22341 We are saving/restoring GPRs if GPR is true. */
22343 static char *
22344 rs6000_savres_routine_name (rs6000_stack_t *info, int regno, int sel)
22346 const char *prefix = "";
22347 const char *suffix = "";
22349 /* Different targets are supposed to define
22350 {SAVE,RESTORE}_FP_{PREFIX,SUFFIX} with the idea that the needed
22351 routine name could be defined with:
22353 sprintf (name, "%s%d%s", SAVE_FP_PREFIX, regno, SAVE_FP_SUFFIX)
22355 This is a nice idea in practice, but in reality, things are
22356 complicated in several ways:
22358 - ELF targets have save/restore routines for GPRs.
22360 - SPE targets use different prefixes for 32/64-bit registers, and
22361 neither of them fit neatly in the FOO_{PREFIX,SUFFIX} regimen.
22363 - PPC64 ELF targets have routines for save/restore of GPRs that
22364 differ in what they do with the link register, so having a set
22365 prefix doesn't work. (We only use one of the save routines at
22366 the moment, though.)
22368 - PPC32 elf targets have "exit" versions of the restore routines
22369 that restore the link register and can save some extra space.
22370 These require an extra suffix. (There are also "tail" versions
22371 of the restore routines and "GOT" versions of the save routines,
22372 but we don't generate those at present. Same problems apply,
22373 though.)
22375 We deal with all this by synthesizing our own prefix/suffix and
22376 using that for the simple sprintf call shown above. */
22377 if (TARGET_SPE)
22379 /* No floating point saves on the SPE. */
22380 gcc_assert ((sel & SAVRES_REG) == SAVRES_GPR);
22382 if ((sel & SAVRES_SAVE))
22383 prefix = info->spe_64bit_regs_used ? "_save64gpr_" : "_save32gpr_";
22384 else
22385 prefix = info->spe_64bit_regs_used ? "_rest64gpr_" : "_rest32gpr_";
22387 if ((sel & SAVRES_LR))
22388 suffix = "_x";
22390 else if (DEFAULT_ABI == ABI_V4)
22392 if (TARGET_64BIT)
22393 goto aix_names;
22395 if ((sel & SAVRES_REG) == SAVRES_GPR)
22396 prefix = (sel & SAVRES_SAVE) ? "_savegpr_" : "_restgpr_";
22397 else if ((sel & SAVRES_REG) == SAVRES_FPR)
22398 prefix = (sel & SAVRES_SAVE) ? "_savefpr_" : "_restfpr_";
22399 else if ((sel & SAVRES_REG) == SAVRES_VR)
22400 prefix = (sel & SAVRES_SAVE) ? "_savevr_" : "_restvr_";
22401 else
22402 abort ();
22404 if ((sel & SAVRES_LR))
22405 suffix = "_x";
22407 else if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
22409 #if !defined (POWERPC_LINUX) && !defined (POWERPC_FREEBSD)
22410 /* No out-of-line save/restore routines for GPRs on AIX. */
22411 gcc_assert (!TARGET_AIX || (sel & SAVRES_REG) != SAVRES_GPR);
22412 #endif
22414 aix_names:
22415 if ((sel & SAVRES_REG) == SAVRES_GPR)
22416 prefix = ((sel & SAVRES_SAVE)
22417 ? ((sel & SAVRES_LR) ? "_savegpr0_" : "_savegpr1_")
22418 : ((sel & SAVRES_LR) ? "_restgpr0_" : "_restgpr1_"));
22419 else if ((sel & SAVRES_REG) == SAVRES_FPR)
22421 #if defined (POWERPC_LINUX) || defined (POWERPC_FREEBSD)
22422 if ((sel & SAVRES_LR))
22423 prefix = ((sel & SAVRES_SAVE) ? "_savefpr_" : "_restfpr_");
22424 else
22425 #endif
22427 prefix = (sel & SAVRES_SAVE) ? SAVE_FP_PREFIX : RESTORE_FP_PREFIX;
22428 suffix = (sel & SAVRES_SAVE) ? SAVE_FP_SUFFIX : RESTORE_FP_SUFFIX;
22431 else if ((sel & SAVRES_REG) == SAVRES_VR)
22432 prefix = (sel & SAVRES_SAVE) ? "_savevr_" : "_restvr_";
22433 else
22434 abort ();
22437 if (DEFAULT_ABI == ABI_DARWIN)
22439 /* The Darwin approach is (slightly) different, in order to be
22440 compatible with code generated by the system toolchain. There is a
22441 single symbol for the start of save sequence, and the code here
22442 embeds an offset into that code on the basis of the first register
22443 to be saved. */
22444 prefix = (sel & SAVRES_SAVE) ? "save" : "rest" ;
22445 if ((sel & SAVRES_REG) == SAVRES_GPR)
22446 sprintf (savres_routine_name, "*%sGPR%s%s%.0d ; %s r%d-r31", prefix,
22447 ((sel & SAVRES_LR) ? "x" : ""), (regno == 13 ? "" : "+"),
22448 (regno - 13) * 4, prefix, regno);
22449 else if ((sel & SAVRES_REG) == SAVRES_FPR)
22450 sprintf (savres_routine_name, "*%sFP%s%.0d ; %s f%d-f31", prefix,
22451 (regno == 14 ? "" : "+"), (regno - 14) * 4, prefix, regno);
22452 else if ((sel & SAVRES_REG) == SAVRES_VR)
22453 sprintf (savres_routine_name, "*%sVEC%s%.0d ; %s v%d-v31", prefix,
22454 (regno == 20 ? "" : "+"), (regno - 20) * 8, prefix, regno);
22455 else
22456 abort ();
22458 else
22459 sprintf (savres_routine_name, "%s%d%s", prefix, regno, suffix);
22461 return savres_routine_name;
22464 /* Return an RTL SYMBOL_REF for an out-of-line register save/restore routine.
22465 We are saving/restoring GPRs if GPR is true. */
22467 static rtx
22468 rs6000_savres_routine_sym (rs6000_stack_t *info, int sel)
22470 int regno = ((sel & SAVRES_REG) == SAVRES_GPR
22471 ? info->first_gp_reg_save
22472 : (sel & SAVRES_REG) == SAVRES_FPR
22473 ? info->first_fp_reg_save - 32
22474 : (sel & SAVRES_REG) == SAVRES_VR
22475 ? info->first_altivec_reg_save - FIRST_ALTIVEC_REGNO
22476 : -1);
22477 rtx sym;
22478 int select = sel;
22480 /* On the SPE, we never have any FPRs, but we do have 32/64-bit
22481 versions of the gpr routines. */
22482 if (TARGET_SPE_ABI && (sel & SAVRES_REG) == SAVRES_GPR
22483 && info->spe_64bit_regs_used)
22484 select ^= SAVRES_FPR ^ SAVRES_GPR;
22486 /* Don't generate bogus routine names. */
22487 gcc_assert (FIRST_SAVRES_REGISTER <= regno
22488 && regno <= LAST_SAVRES_REGISTER
22489 && select >= 0 && select <= 12);
22491 sym = savres_routine_syms[regno-FIRST_SAVRES_REGISTER][select];
22493 if (sym == NULL)
22495 char *name;
22497 name = rs6000_savres_routine_name (info, regno, sel);
22499 sym = savres_routine_syms[regno-FIRST_SAVRES_REGISTER][select]
22500 = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
22501 SYMBOL_REF_FLAGS (sym) |= SYMBOL_FLAG_FUNCTION;
22504 return sym;
22507 /* Emit a sequence of insns, including a stack tie if needed, for
22508 resetting the stack pointer. If UPDT_REGNO is not 1, then don't
22509 reset the stack pointer, but move the base of the frame into
22510 reg UPDT_REGNO for use by out-of-line register restore routines. */
22512 static rtx
22513 rs6000_emit_stack_reset (rs6000_stack_t *info,
22514 rtx frame_reg_rtx, HOST_WIDE_INT frame_off,
22515 unsigned updt_regno)
22517 rtx updt_reg_rtx;
22519 /* This blockage is needed so that sched doesn't decide to move
22520 the sp change before the register restores. */
22521 if (DEFAULT_ABI == ABI_V4
22522 || (TARGET_SPE_ABI
22523 && info->spe_64bit_regs_used != 0
22524 && info->first_gp_reg_save != 32))
22525 rs6000_emit_stack_tie (frame_reg_rtx, frame_pointer_needed);
22527 /* If we are restoring registers out-of-line, we will be using the
22528 "exit" variants of the restore routines, which will reset the
22529 stack for us. But we do need to point updt_reg into the
22530 right place for those routines. */
22531 updt_reg_rtx = gen_rtx_REG (Pmode, updt_regno);
22533 if (frame_off != 0)
22534 return emit_insn (gen_add3_insn (updt_reg_rtx,
22535 frame_reg_rtx, GEN_INT (frame_off)));
22536 else if (REGNO (frame_reg_rtx) != updt_regno)
22537 return emit_move_insn (updt_reg_rtx, frame_reg_rtx);
22539 return NULL_RTX;
22542 /* Return the register number used as a pointer by out-of-line
22543 save/restore functions. */
22545 static inline unsigned
22546 ptr_regno_for_savres (int sel)
22548 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
22549 return (sel & SAVRES_REG) == SAVRES_FPR || (sel & SAVRES_LR) ? 1 : 12;
22550 return DEFAULT_ABI == ABI_DARWIN && (sel & SAVRES_REG) == SAVRES_FPR ? 1 : 11;
22553 /* Construct a parallel rtx describing the effect of a call to an
22554 out-of-line register save/restore routine, and emit the insn
22555 or jump_insn as appropriate. */
22557 static rtx
22558 rs6000_emit_savres_rtx (rs6000_stack_t *info,
22559 rtx frame_reg_rtx, int save_area_offset, int lr_offset,
22560 enum machine_mode reg_mode, int sel)
22562 int i;
22563 int offset, start_reg, end_reg, n_regs, use_reg;
22564 int reg_size = GET_MODE_SIZE (reg_mode);
22565 rtx sym;
22566 rtvec p;
22567 rtx par, insn;
22569 offset = 0;
22570 start_reg = ((sel & SAVRES_REG) == SAVRES_GPR
22571 ? info->first_gp_reg_save
22572 : (sel & SAVRES_REG) == SAVRES_FPR
22573 ? info->first_fp_reg_save
22574 : (sel & SAVRES_REG) == SAVRES_VR
22575 ? info->first_altivec_reg_save
22576 : -1);
22577 end_reg = ((sel & SAVRES_REG) == SAVRES_GPR
22578 ? 32
22579 : (sel & SAVRES_REG) == SAVRES_FPR
22580 ? 64
22581 : (sel & SAVRES_REG) == SAVRES_VR
22582 ? LAST_ALTIVEC_REGNO + 1
22583 : -1);
22584 n_regs = end_reg - start_reg;
22585 p = rtvec_alloc (3 + ((sel & SAVRES_LR) ? 1 : 0)
22586 + ((sel & SAVRES_REG) == SAVRES_VR ? 1 : 0)
22587 + n_regs);
22589 if (!(sel & SAVRES_SAVE) && (sel & SAVRES_LR))
22590 RTVEC_ELT (p, offset++) = ret_rtx;
22592 RTVEC_ELT (p, offset++)
22593 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, LR_REGNO));
22595 sym = rs6000_savres_routine_sym (info, sel);
22596 RTVEC_ELT (p, offset++) = gen_rtx_USE (VOIDmode, sym);
22598 use_reg = ptr_regno_for_savres (sel);
22599 if ((sel & SAVRES_REG) == SAVRES_VR)
22601 /* Vector regs are saved/restored using [reg+reg] addressing. */
22602 RTVEC_ELT (p, offset++)
22603 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, use_reg));
22604 RTVEC_ELT (p, offset++)
22605 = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, 0));
22607 else
22608 RTVEC_ELT (p, offset++)
22609 = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, use_reg));
22611 for (i = 0; i < end_reg - start_reg; i++)
22612 RTVEC_ELT (p, i + offset)
22613 = gen_frame_set (gen_rtx_REG (reg_mode, start_reg + i),
22614 frame_reg_rtx, save_area_offset + reg_size * i,
22615 (sel & SAVRES_SAVE) != 0);
22617 if ((sel & SAVRES_SAVE) && (sel & SAVRES_LR))
22618 RTVEC_ELT (p, i + offset)
22619 = gen_frame_store (gen_rtx_REG (Pmode, 0), frame_reg_rtx, lr_offset);
22621 par = gen_rtx_PARALLEL (VOIDmode, p);
22623 if (!(sel & SAVRES_SAVE) && (sel & SAVRES_LR))
22625 insn = emit_jump_insn (par);
22626 JUMP_LABEL (insn) = ret_rtx;
22628 else
22629 insn = emit_insn (par);
22630 return insn;
22633 /* Emit code to store CR fields that need to be saved into REG. */
22635 static void
22636 rs6000_emit_move_from_cr (rtx reg)
22638 /* Only the ELFv2 ABI allows storing only selected fields. */
22639 if (DEFAULT_ABI == ABI_ELFv2 && TARGET_MFCRF)
22641 int i, cr_reg[8], count = 0;
22643 /* Collect CR fields that must be saved. */
22644 for (i = 0; i < 8; i++)
22645 if (save_reg_p (CR0_REGNO + i))
22646 cr_reg[count++] = i;
22648 /* If it's just a single one, use mfcrf. */
22649 if (count == 1)
22651 rtvec p = rtvec_alloc (1);
22652 rtvec r = rtvec_alloc (2);
22653 RTVEC_ELT (r, 0) = gen_rtx_REG (CCmode, CR0_REGNO + cr_reg[0]);
22654 RTVEC_ELT (r, 1) = GEN_INT (1 << (7 - cr_reg[0]));
22655 RTVEC_ELT (p, 0)
22656 = gen_rtx_SET (VOIDmode, reg,
22657 gen_rtx_UNSPEC (SImode, r, UNSPEC_MOVESI_FROM_CR));
22659 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
22660 return;
22663 /* ??? It might be better to handle count == 2 / 3 cases here
22664 as well, using logical operations to combine the values. */
22667 emit_insn (gen_movesi_from_cr (reg));
22670 /* Determine whether the gp REG is really used. */
22672 static bool
22673 rs6000_reg_live_or_pic_offset_p (int reg)
22675 /* If the function calls eh_return, claim used all the registers that would
22676 be checked for liveness otherwise. This is required for the PIC offset
22677 register with -mminimal-toc on AIX, as it is advertised as "fixed" for
22678 register allocation purposes in this case. */
22680 return (((crtl->calls_eh_return || df_regs_ever_live_p (reg))
22681 && (!call_used_regs[reg]
22682 || (reg == RS6000_PIC_OFFSET_TABLE_REGNUM
22683 && !TARGET_SINGLE_PIC_BASE
22684 && TARGET_TOC && TARGET_MINIMAL_TOC)))
22685 || (reg == RS6000_PIC_OFFSET_TABLE_REGNUM
22686 && !TARGET_SINGLE_PIC_BASE
22687 && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
22688 || (DEFAULT_ABI == ABI_DARWIN && flag_pic))));
22691 /* Emit function prologue as insns. */
22693 void
22694 rs6000_emit_prologue (void)
22696 rs6000_stack_t *info = rs6000_stack_info ();
22697 enum machine_mode reg_mode = Pmode;
22698 int reg_size = TARGET_32BIT ? 4 : 8;
22699 rtx sp_reg_rtx = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
22700 rtx frame_reg_rtx = sp_reg_rtx;
22701 unsigned int cr_save_regno;
22702 rtx cr_save_rtx = NULL_RTX;
22703 rtx insn;
22704 int strategy;
22705 int using_static_chain_p = (cfun->static_chain_decl != NULL_TREE
22706 && df_regs_ever_live_p (STATIC_CHAIN_REGNUM)
22707 && call_used_regs[STATIC_CHAIN_REGNUM]);
22708 /* Offset to top of frame for frame_reg and sp respectively. */
22709 HOST_WIDE_INT frame_off = 0;
22710 HOST_WIDE_INT sp_off = 0;
22712 #ifdef ENABLE_CHECKING
22713 /* Track and check usage of r0, r11, r12. */
22714 int reg_inuse = using_static_chain_p ? 1 << 11 : 0;
22715 #define START_USE(R) do \
22717 gcc_assert ((reg_inuse & (1 << (R))) == 0); \
22718 reg_inuse |= 1 << (R); \
22719 } while (0)
22720 #define END_USE(R) do \
22722 gcc_assert ((reg_inuse & (1 << (R))) != 0); \
22723 reg_inuse &= ~(1 << (R)); \
22724 } while (0)
22725 #define NOT_INUSE(R) do \
22727 gcc_assert ((reg_inuse & (1 << (R))) == 0); \
22728 } while (0)
22729 #else
22730 #define START_USE(R) do {} while (0)
22731 #define END_USE(R) do {} while (0)
22732 #define NOT_INUSE(R) do {} while (0)
22733 #endif
22735 if (DEFAULT_ABI == ABI_ELFv2)
22737 cfun->machine->r2_setup_needed = df_regs_ever_live_p (TOC_REGNUM);
22739 /* With -mminimal-toc we may generate an extra use of r2 below. */
22740 if (!TARGET_SINGLE_PIC_BASE
22741 && TARGET_TOC && TARGET_MINIMAL_TOC && get_pool_size () != 0)
22742 cfun->machine->r2_setup_needed = true;
22746 if (flag_stack_usage_info)
22747 current_function_static_stack_size = info->total_size;
22749 if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
22751 HOST_WIDE_INT size = info->total_size;
22753 if (crtl->is_leaf && !cfun->calls_alloca)
22755 if (size > PROBE_INTERVAL && size > STACK_CHECK_PROTECT)
22756 rs6000_emit_probe_stack_range (STACK_CHECK_PROTECT,
22757 size - STACK_CHECK_PROTECT);
22759 else if (size > 0)
22760 rs6000_emit_probe_stack_range (STACK_CHECK_PROTECT, size);
22763 if (TARGET_FIX_AND_CONTINUE)
22765 /* gdb on darwin arranges to forward a function from the old
22766 address by modifying the first 5 instructions of the function
22767 to branch to the overriding function. This is necessary to
22768 permit function pointers that point to the old function to
22769 actually forward to the new function. */
22770 emit_insn (gen_nop ());
22771 emit_insn (gen_nop ());
22772 emit_insn (gen_nop ());
22773 emit_insn (gen_nop ());
22774 emit_insn (gen_nop ());
22777 if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
22779 reg_mode = V2SImode;
22780 reg_size = 8;
22783 /* Handle world saves specially here. */
22784 if (WORLD_SAVE_P (info))
22786 int i, j, sz;
22787 rtx treg;
22788 rtvec p;
22789 rtx reg0;
22791 /* save_world expects lr in r0. */
22792 reg0 = gen_rtx_REG (Pmode, 0);
22793 if (info->lr_save_p)
22795 insn = emit_move_insn (reg0,
22796 gen_rtx_REG (Pmode, LR_REGNO));
22797 RTX_FRAME_RELATED_P (insn) = 1;
22800 /* The SAVE_WORLD and RESTORE_WORLD routines make a number of
22801 assumptions about the offsets of various bits of the stack
22802 frame. */
22803 gcc_assert (info->gp_save_offset == -220
22804 && info->fp_save_offset == -144
22805 && info->lr_save_offset == 8
22806 && info->cr_save_offset == 4
22807 && info->push_p
22808 && info->lr_save_p
22809 && (!crtl->calls_eh_return
22810 || info->ehrd_offset == -432)
22811 && info->vrsave_save_offset == -224
22812 && info->altivec_save_offset == -416);
22814 treg = gen_rtx_REG (SImode, 11);
22815 emit_move_insn (treg, GEN_INT (-info->total_size));
22817 /* SAVE_WORLD takes the caller's LR in R0 and the frame size
22818 in R11. It also clobbers R12, so beware! */
22820 /* Preserve CR2 for save_world prologues */
22821 sz = 5;
22822 sz += 32 - info->first_gp_reg_save;
22823 sz += 64 - info->first_fp_reg_save;
22824 sz += LAST_ALTIVEC_REGNO - info->first_altivec_reg_save + 1;
22825 p = rtvec_alloc (sz);
22826 j = 0;
22827 RTVEC_ELT (p, j++) = gen_rtx_CLOBBER (VOIDmode,
22828 gen_rtx_REG (SImode,
22829 LR_REGNO));
22830 RTVEC_ELT (p, j++) = gen_rtx_USE (VOIDmode,
22831 gen_rtx_SYMBOL_REF (Pmode,
22832 "*save_world"));
22833 /* We do floats first so that the instruction pattern matches
22834 properly. */
22835 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
22836 RTVEC_ELT (p, j++)
22837 = gen_frame_store (gen_rtx_REG (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
22838 ? DFmode : SFmode,
22839 info->first_fp_reg_save + i),
22840 frame_reg_rtx,
22841 info->fp_save_offset + frame_off + 8 * i);
22842 for (i = 0; info->first_altivec_reg_save + i <= LAST_ALTIVEC_REGNO; i++)
22843 RTVEC_ELT (p, j++)
22844 = gen_frame_store (gen_rtx_REG (V4SImode,
22845 info->first_altivec_reg_save + i),
22846 frame_reg_rtx,
22847 info->altivec_save_offset + frame_off + 16 * i);
22848 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
22849 RTVEC_ELT (p, j++)
22850 = gen_frame_store (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
22851 frame_reg_rtx,
22852 info->gp_save_offset + frame_off + reg_size * i);
22854 /* CR register traditionally saved as CR2. */
22855 RTVEC_ELT (p, j++)
22856 = gen_frame_store (gen_rtx_REG (SImode, CR2_REGNO),
22857 frame_reg_rtx, info->cr_save_offset + frame_off);
22858 /* Explain about use of R0. */
22859 if (info->lr_save_p)
22860 RTVEC_ELT (p, j++)
22861 = gen_frame_store (reg0,
22862 frame_reg_rtx, info->lr_save_offset + frame_off);
22863 /* Explain what happens to the stack pointer. */
22865 rtx newval = gen_rtx_PLUS (Pmode, sp_reg_rtx, treg);
22866 RTVEC_ELT (p, j++) = gen_rtx_SET (VOIDmode, sp_reg_rtx, newval);
22869 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
22870 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
22871 treg, GEN_INT (-info->total_size), NULL_RTX);
22872 sp_off = frame_off = info->total_size;
22875 strategy = info->savres_strategy;
22877 /* For V.4, update stack before we do any saving and set back pointer. */
22878 if (! WORLD_SAVE_P (info)
22879 && info->push_p
22880 && (DEFAULT_ABI == ABI_V4
22881 || crtl->calls_eh_return))
22883 bool need_r11 = (TARGET_SPE
22884 ? (!(strategy & SAVE_INLINE_GPRS)
22885 && info->spe_64bit_regs_used == 0)
22886 : (!(strategy & SAVE_INLINE_FPRS)
22887 || !(strategy & SAVE_INLINE_GPRS)
22888 || !(strategy & SAVE_INLINE_VRS)));
22889 int ptr_regno = -1;
22890 rtx ptr_reg = NULL_RTX;
22891 int ptr_off = 0;
22893 if (info->total_size < 32767)
22894 frame_off = info->total_size;
22895 else if (need_r11)
22896 ptr_regno = 11;
22897 else if (info->cr_save_p
22898 || info->lr_save_p
22899 || info->first_fp_reg_save < 64
22900 || info->first_gp_reg_save < 32
22901 || info->altivec_size != 0
22902 || info->vrsave_mask != 0
22903 || crtl->calls_eh_return)
22904 ptr_regno = 12;
22905 else
22907 /* The prologue won't be saving any regs so there is no need
22908 to set up a frame register to access any frame save area.
22909 We also won't be using frame_off anywhere below, but set
22910 the correct value anyway to protect against future
22911 changes to this function. */
22912 frame_off = info->total_size;
22914 if (ptr_regno != -1)
22916 /* Set up the frame offset to that needed by the first
22917 out-of-line save function. */
22918 START_USE (ptr_regno);
22919 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
22920 frame_reg_rtx = ptr_reg;
22921 if (!(strategy & SAVE_INLINE_FPRS) && info->fp_size != 0)
22922 gcc_checking_assert (info->fp_save_offset + info->fp_size == 0);
22923 else if (!(strategy & SAVE_INLINE_GPRS) && info->first_gp_reg_save < 32)
22924 ptr_off = info->gp_save_offset + info->gp_size;
22925 else if (!(strategy & SAVE_INLINE_VRS) && info->altivec_size != 0)
22926 ptr_off = info->altivec_save_offset + info->altivec_size;
22927 frame_off = -ptr_off;
22929 rs6000_emit_allocate_stack (info->total_size, ptr_reg, ptr_off);
22930 sp_off = info->total_size;
22931 if (frame_reg_rtx != sp_reg_rtx)
22932 rs6000_emit_stack_tie (frame_reg_rtx, false);
22935 /* If we use the link register, get it into r0. */
22936 if (!WORLD_SAVE_P (info) && info->lr_save_p)
22938 rtx addr, reg, mem;
22940 reg = gen_rtx_REG (Pmode, 0);
22941 START_USE (0);
22942 insn = emit_move_insn (reg, gen_rtx_REG (Pmode, LR_REGNO));
22943 RTX_FRAME_RELATED_P (insn) = 1;
22945 if (!(strategy & (SAVE_NOINLINE_GPRS_SAVES_LR
22946 | SAVE_NOINLINE_FPRS_SAVES_LR)))
22948 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
22949 GEN_INT (info->lr_save_offset + frame_off));
22950 mem = gen_rtx_MEM (Pmode, addr);
22951 /* This should not be of rs6000_sr_alias_set, because of
22952 __builtin_return_address. */
22954 insn = emit_move_insn (mem, reg);
22955 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
22956 NULL_RTX, NULL_RTX, NULL_RTX);
22957 END_USE (0);
22961 /* If we need to save CR, put it into r12 or r11. Choose r12 except when
22962 r12 will be needed by out-of-line gpr restore. */
22963 cr_save_regno = ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
22964 && !(strategy & (SAVE_INLINE_GPRS
22965 | SAVE_NOINLINE_GPRS_SAVES_LR))
22966 ? 11 : 12);
22967 if (!WORLD_SAVE_P (info)
22968 && info->cr_save_p
22969 && REGNO (frame_reg_rtx) != cr_save_regno
22970 && !(using_static_chain_p && cr_save_regno == 11))
22972 cr_save_rtx = gen_rtx_REG (SImode, cr_save_regno);
22973 START_USE (cr_save_regno);
22974 rs6000_emit_move_from_cr (cr_save_rtx);
22977 /* Do any required saving of fpr's. If only one or two to save, do
22978 it ourselves. Otherwise, call function. */
22979 if (!WORLD_SAVE_P (info) && (strategy & SAVE_INLINE_FPRS))
22981 int i;
22982 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
22983 if (save_reg_p (info->first_fp_reg_save + i))
22984 emit_frame_save (frame_reg_rtx,
22985 (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
22986 ? DFmode : SFmode),
22987 info->first_fp_reg_save + i,
22988 info->fp_save_offset + frame_off + 8 * i,
22989 sp_off - frame_off);
22991 else if (!WORLD_SAVE_P (info) && info->first_fp_reg_save != 64)
22993 bool lr = (strategy & SAVE_NOINLINE_FPRS_SAVES_LR) != 0;
22994 int sel = SAVRES_SAVE | SAVRES_FPR | (lr ? SAVRES_LR : 0);
22995 unsigned ptr_regno = ptr_regno_for_savres (sel);
22996 rtx ptr_reg = frame_reg_rtx;
22998 if (REGNO (frame_reg_rtx) == ptr_regno)
22999 gcc_checking_assert (frame_off == 0);
23000 else
23002 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
23003 NOT_INUSE (ptr_regno);
23004 emit_insn (gen_add3_insn (ptr_reg,
23005 frame_reg_rtx, GEN_INT (frame_off)));
23007 insn = rs6000_emit_savres_rtx (info, ptr_reg,
23008 info->fp_save_offset,
23009 info->lr_save_offset,
23010 DFmode, sel);
23011 rs6000_frame_related (insn, ptr_reg, sp_off,
23012 NULL_RTX, NULL_RTX, NULL_RTX);
23013 if (lr)
23014 END_USE (0);
23017 /* Save GPRs. This is done as a PARALLEL if we are using
23018 the store-multiple instructions. */
23019 if (!WORLD_SAVE_P (info)
23020 && TARGET_SPE_ABI
23021 && info->spe_64bit_regs_used != 0
23022 && info->first_gp_reg_save != 32)
23024 int i;
23025 rtx spe_save_area_ptr;
23026 HOST_WIDE_INT save_off;
23027 int ool_adjust = 0;
23029 /* Determine whether we can address all of the registers that need
23030 to be saved with an offset from frame_reg_rtx that fits in
23031 the small const field for SPE memory instructions. */
23032 int spe_regs_addressable
23033 = (SPE_CONST_OFFSET_OK (info->spe_gp_save_offset + frame_off
23034 + reg_size * (32 - info->first_gp_reg_save - 1))
23035 && (strategy & SAVE_INLINE_GPRS));
23037 if (spe_regs_addressable)
23039 spe_save_area_ptr = frame_reg_rtx;
23040 save_off = frame_off;
23042 else
23044 /* Make r11 point to the start of the SPE save area. We need
23045 to be careful here if r11 is holding the static chain. If
23046 it is, then temporarily save it in r0. */
23047 HOST_WIDE_INT offset;
23049 if (!(strategy & SAVE_INLINE_GPRS))
23050 ool_adjust = 8 * (info->first_gp_reg_save - FIRST_SAVED_GP_REGNO);
23051 offset = info->spe_gp_save_offset + frame_off - ool_adjust;
23052 spe_save_area_ptr = gen_rtx_REG (Pmode, 11);
23053 save_off = frame_off - offset;
23055 if (using_static_chain_p)
23057 rtx r0 = gen_rtx_REG (Pmode, 0);
23059 START_USE (0);
23060 gcc_assert (info->first_gp_reg_save > 11);
23062 emit_move_insn (r0, spe_save_area_ptr);
23064 else if (REGNO (frame_reg_rtx) != 11)
23065 START_USE (11);
23067 emit_insn (gen_addsi3 (spe_save_area_ptr,
23068 frame_reg_rtx, GEN_INT (offset)));
23069 if (!using_static_chain_p && REGNO (frame_reg_rtx) == 11)
23070 frame_off = -info->spe_gp_save_offset + ool_adjust;
23073 if ((strategy & SAVE_INLINE_GPRS))
23075 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
23076 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
23077 emit_frame_save (spe_save_area_ptr, reg_mode,
23078 info->first_gp_reg_save + i,
23079 (info->spe_gp_save_offset + save_off
23080 + reg_size * i),
23081 sp_off - save_off);
23083 else
23085 insn = rs6000_emit_savres_rtx (info, spe_save_area_ptr,
23086 info->spe_gp_save_offset + save_off,
23087 0, reg_mode,
23088 SAVRES_SAVE | SAVRES_GPR);
23090 rs6000_frame_related (insn, spe_save_area_ptr, sp_off - save_off,
23091 NULL_RTX, NULL_RTX, NULL_RTX);
23094 /* Move the static chain pointer back. */
23095 if (!spe_regs_addressable)
23097 if (using_static_chain_p)
23099 emit_move_insn (spe_save_area_ptr, gen_rtx_REG (Pmode, 0));
23100 END_USE (0);
23102 else if (REGNO (frame_reg_rtx) != 11)
23103 END_USE (11);
23106 else if (!WORLD_SAVE_P (info) && !(strategy & SAVE_INLINE_GPRS))
23108 bool lr = (strategy & SAVE_NOINLINE_GPRS_SAVES_LR) != 0;
23109 int sel = SAVRES_SAVE | SAVRES_GPR | (lr ? SAVRES_LR : 0);
23110 unsigned ptr_regno = ptr_regno_for_savres (sel);
23111 rtx ptr_reg = frame_reg_rtx;
23112 bool ptr_set_up = REGNO (ptr_reg) == ptr_regno;
23113 int end_save = info->gp_save_offset + info->gp_size;
23114 int ptr_off;
23116 if (!ptr_set_up)
23117 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
23119 /* Need to adjust r11 (r12) if we saved any FPRs. */
23120 if (end_save + frame_off != 0)
23122 rtx offset = GEN_INT (end_save + frame_off);
23124 if (ptr_set_up)
23125 frame_off = -end_save;
23126 else
23127 NOT_INUSE (ptr_regno);
23128 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
23130 else if (!ptr_set_up)
23132 NOT_INUSE (ptr_regno);
23133 emit_move_insn (ptr_reg, frame_reg_rtx);
23135 ptr_off = -end_save;
23136 insn = rs6000_emit_savres_rtx (info, ptr_reg,
23137 info->gp_save_offset + ptr_off,
23138 info->lr_save_offset + ptr_off,
23139 reg_mode, sel);
23140 rs6000_frame_related (insn, ptr_reg, sp_off - ptr_off,
23141 NULL_RTX, NULL_RTX, NULL_RTX);
23142 if (lr)
23143 END_USE (0);
23145 else if (!WORLD_SAVE_P (info) && (strategy & SAVRES_MULTIPLE))
23147 rtvec p;
23148 int i;
23149 p = rtvec_alloc (32 - info->first_gp_reg_save);
23150 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
23151 RTVEC_ELT (p, i)
23152 = gen_frame_store (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
23153 frame_reg_rtx,
23154 info->gp_save_offset + frame_off + reg_size * i);
23155 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
23156 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
23157 NULL_RTX, NULL_RTX, NULL_RTX);
23159 else if (!WORLD_SAVE_P (info))
23161 int i;
23162 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
23163 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
23164 emit_frame_save (frame_reg_rtx, reg_mode,
23165 info->first_gp_reg_save + i,
23166 info->gp_save_offset + frame_off + reg_size * i,
23167 sp_off - frame_off);
23170 if (crtl->calls_eh_return)
23172 unsigned int i;
23173 rtvec p;
23175 for (i = 0; ; ++i)
23177 unsigned int regno = EH_RETURN_DATA_REGNO (i);
23178 if (regno == INVALID_REGNUM)
23179 break;
23182 p = rtvec_alloc (i);
23184 for (i = 0; ; ++i)
23186 unsigned int regno = EH_RETURN_DATA_REGNO (i);
23187 if (regno == INVALID_REGNUM)
23188 break;
23190 insn
23191 = gen_frame_store (gen_rtx_REG (reg_mode, regno),
23192 sp_reg_rtx,
23193 info->ehrd_offset + sp_off + reg_size * (int) i);
23194 RTVEC_ELT (p, i) = insn;
23195 RTX_FRAME_RELATED_P (insn) = 1;
23198 insn = emit_insn (gen_blockage ());
23199 RTX_FRAME_RELATED_P (insn) = 1;
23200 add_reg_note (insn, REG_FRAME_RELATED_EXPR, gen_rtx_PARALLEL (VOIDmode, p));
23203 /* In AIX ABI we need to make sure r2 is really saved. */
23204 if (TARGET_AIX && crtl->calls_eh_return)
23206 rtx tmp_reg, tmp_reg_si, hi, lo, compare_result, toc_save_done, jump;
23207 rtx save_insn, join_insn, note;
23208 long toc_restore_insn;
23210 tmp_reg = gen_rtx_REG (Pmode, 11);
23211 tmp_reg_si = gen_rtx_REG (SImode, 11);
23212 if (using_static_chain_p)
23214 START_USE (0);
23215 emit_move_insn (gen_rtx_REG (Pmode, 0), tmp_reg);
23217 else
23218 START_USE (11);
23219 emit_move_insn (tmp_reg, gen_rtx_REG (Pmode, LR_REGNO));
23220 /* Peek at instruction to which this function returns. If it's
23221 restoring r2, then we know we've already saved r2. We can't
23222 unconditionally save r2 because the value we have will already
23223 be updated if we arrived at this function via a plt call or
23224 toc adjusting stub. */
23225 emit_move_insn (tmp_reg_si, gen_rtx_MEM (SImode, tmp_reg));
23226 toc_restore_insn = ((TARGET_32BIT ? 0x80410000 : 0xE8410000)
23227 + RS6000_TOC_SAVE_SLOT);
23228 hi = gen_int_mode (toc_restore_insn & ~0xffff, SImode);
23229 emit_insn (gen_xorsi3 (tmp_reg_si, tmp_reg_si, hi));
23230 compare_result = gen_rtx_REG (CCUNSmode, CR0_REGNO);
23231 validate_condition_mode (EQ, CCUNSmode);
23232 lo = gen_int_mode (toc_restore_insn & 0xffff, SImode);
23233 emit_insn (gen_rtx_SET (VOIDmode, compare_result,
23234 gen_rtx_COMPARE (CCUNSmode, tmp_reg_si, lo)));
23235 toc_save_done = gen_label_rtx ();
23236 jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
23237 gen_rtx_EQ (VOIDmode, compare_result,
23238 const0_rtx),
23239 gen_rtx_LABEL_REF (VOIDmode, toc_save_done),
23240 pc_rtx);
23241 jump = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, jump));
23242 JUMP_LABEL (jump) = toc_save_done;
23243 LABEL_NUSES (toc_save_done) += 1;
23245 save_insn = emit_frame_save (frame_reg_rtx, reg_mode,
23246 TOC_REGNUM, frame_off + RS6000_TOC_SAVE_SLOT,
23247 sp_off - frame_off);
23249 emit_label (toc_save_done);
23251 /* ??? If we leave SAVE_INSN as marked as saving R2, then we'll
23252 have a CFG that has different saves along different paths.
23253 Move the note to a dummy blockage insn, which describes that
23254 R2 is unconditionally saved after the label. */
23255 /* ??? An alternate representation might be a special insn pattern
23256 containing both the branch and the store. That might let the
23257 code that minimizes the number of DW_CFA_advance opcodes better
23258 freedom in placing the annotations. */
23259 note = find_reg_note (save_insn, REG_FRAME_RELATED_EXPR, NULL);
23260 if (note)
23261 remove_note (save_insn, note);
23262 else
23263 note = alloc_reg_note (REG_FRAME_RELATED_EXPR,
23264 copy_rtx (PATTERN (save_insn)), NULL_RTX);
23265 RTX_FRAME_RELATED_P (save_insn) = 0;
23267 join_insn = emit_insn (gen_blockage ());
23268 REG_NOTES (join_insn) = note;
23269 RTX_FRAME_RELATED_P (join_insn) = 1;
23271 if (using_static_chain_p)
23273 emit_move_insn (tmp_reg, gen_rtx_REG (Pmode, 0));
23274 END_USE (0);
23276 else
23277 END_USE (11);
23280 /* Save CR if we use any that must be preserved. */
23281 if (!WORLD_SAVE_P (info) && info->cr_save_p)
23283 rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
23284 GEN_INT (info->cr_save_offset + frame_off));
23285 rtx mem = gen_frame_mem (SImode, addr);
23287 /* If we didn't copy cr before, do so now using r0. */
23288 if (cr_save_rtx == NULL_RTX)
23290 START_USE (0);
23291 cr_save_rtx = gen_rtx_REG (SImode, 0);
23292 rs6000_emit_move_from_cr (cr_save_rtx);
23295 /* Saving CR requires a two-instruction sequence: one instruction
23296 to move the CR to a general-purpose register, and a second
23297 instruction that stores the GPR to memory.
23299 We do not emit any DWARF CFI records for the first of these,
23300 because we cannot properly represent the fact that CR is saved in
23301 a register. One reason is that we cannot express that multiple
23302 CR fields are saved; another reason is that on 64-bit, the size
23303 of the CR register in DWARF (4 bytes) differs from the size of
23304 a general-purpose register.
23306 This means if any intervening instruction were to clobber one of
23307 the call-saved CR fields, we'd have incorrect CFI. To prevent
23308 this from happening, we mark the store to memory as a use of
23309 those CR fields, which prevents any such instruction from being
23310 scheduled in between the two instructions. */
23311 rtx crsave_v[9];
23312 int n_crsave = 0;
23313 int i;
23315 crsave_v[n_crsave++] = gen_rtx_SET (VOIDmode, mem, cr_save_rtx);
23316 for (i = 0; i < 8; i++)
23317 if (save_reg_p (CR0_REGNO + i))
23318 crsave_v[n_crsave++]
23319 = gen_rtx_USE (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO + i));
23321 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode,
23322 gen_rtvec_v (n_crsave, crsave_v)));
23323 END_USE (REGNO (cr_save_rtx));
23325 /* Now, there's no way that dwarf2out_frame_debug_expr is going to
23326 understand '(unspec:SI [(reg:CC 68) ...] UNSPEC_MOVESI_FROM_CR)',
23327 so we need to construct a frame expression manually. */
23328 RTX_FRAME_RELATED_P (insn) = 1;
23330 /* Update address to be stack-pointer relative, like
23331 rs6000_frame_related would do. */
23332 addr = gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode, STACK_POINTER_REGNUM),
23333 GEN_INT (info->cr_save_offset + sp_off));
23334 mem = gen_frame_mem (SImode, addr);
23336 if (DEFAULT_ABI == ABI_ELFv2)
23338 /* In the ELFv2 ABI we generate separate CFI records for each
23339 CR field that was actually saved. They all point to the
23340 same 32-bit stack slot. */
23341 rtx crframe[8];
23342 int n_crframe = 0;
23344 for (i = 0; i < 8; i++)
23345 if (save_reg_p (CR0_REGNO + i))
23347 crframe[n_crframe]
23348 = gen_rtx_SET (VOIDmode, mem,
23349 gen_rtx_REG (SImode, CR0_REGNO + i));
23351 RTX_FRAME_RELATED_P (crframe[n_crframe]) = 1;
23352 n_crframe++;
23355 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
23356 gen_rtx_PARALLEL (VOIDmode,
23357 gen_rtvec_v (n_crframe, crframe)));
23359 else
23361 /* In other ABIs, by convention, we use a single CR regnum to
23362 represent the fact that all call-saved CR fields are saved.
23363 We use CR2_REGNO to be compatible with gcc-2.95 on Linux. */
23364 rtx set = gen_rtx_SET (VOIDmode, mem,
23365 gen_rtx_REG (SImode, CR2_REGNO));
23366 add_reg_note (insn, REG_FRAME_RELATED_EXPR, set);
23370 /* In the ELFv2 ABI we need to save all call-saved CR fields into
23371 *separate* slots if the routine calls __builtin_eh_return, so
23372 that they can be independently restored by the unwinder. */
23373 if (DEFAULT_ABI == ABI_ELFv2 && crtl->calls_eh_return)
23375 int i, cr_off = info->ehcr_offset;
23376 rtx crsave;
23378 /* ??? We might get better performance by using multiple mfocrf
23379 instructions. */
23380 crsave = gen_rtx_REG (SImode, 0);
23381 emit_insn (gen_movesi_from_cr (crsave));
23383 for (i = 0; i < 8; i++)
23384 if (!call_used_regs[CR0_REGNO + i])
23386 rtvec p = rtvec_alloc (2);
23387 RTVEC_ELT (p, 0)
23388 = gen_frame_store (crsave, frame_reg_rtx, cr_off + frame_off);
23389 RTVEC_ELT (p, 1)
23390 = gen_rtx_USE (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO + i));
23392 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
23394 RTX_FRAME_RELATED_P (insn) = 1;
23395 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
23396 gen_frame_store (gen_rtx_REG (SImode, CR0_REGNO + i),
23397 sp_reg_rtx, cr_off + sp_off));
23399 cr_off += reg_size;
23403 /* Update stack and set back pointer unless this is V.4,
23404 for which it was done previously. */
23405 if (!WORLD_SAVE_P (info) && info->push_p
23406 && !(DEFAULT_ABI == ABI_V4 || crtl->calls_eh_return))
23408 rtx ptr_reg = NULL;
23409 int ptr_off = 0;
23411 /* If saving altivec regs we need to be able to address all save
23412 locations using a 16-bit offset. */
23413 if ((strategy & SAVE_INLINE_VRS) == 0
23414 || (info->altivec_size != 0
23415 && (info->altivec_save_offset + info->altivec_size - 16
23416 + info->total_size - frame_off) > 32767)
23417 || (info->vrsave_size != 0
23418 && (info->vrsave_save_offset
23419 + info->total_size - frame_off) > 32767))
23421 int sel = SAVRES_SAVE | SAVRES_VR;
23422 unsigned ptr_regno = ptr_regno_for_savres (sel);
23424 if (using_static_chain_p
23425 && ptr_regno == STATIC_CHAIN_REGNUM)
23426 ptr_regno = 12;
23427 if (REGNO (frame_reg_rtx) != ptr_regno)
23428 START_USE (ptr_regno);
23429 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
23430 frame_reg_rtx = ptr_reg;
23431 ptr_off = info->altivec_save_offset + info->altivec_size;
23432 frame_off = -ptr_off;
23434 else if (REGNO (frame_reg_rtx) == 1)
23435 frame_off = info->total_size;
23436 rs6000_emit_allocate_stack (info->total_size, ptr_reg, ptr_off);
23437 sp_off = info->total_size;
23438 if (frame_reg_rtx != sp_reg_rtx)
23439 rs6000_emit_stack_tie (frame_reg_rtx, false);
23442 /* Set frame pointer, if needed. */
23443 if (frame_pointer_needed)
23445 insn = emit_move_insn (gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
23446 sp_reg_rtx);
23447 RTX_FRAME_RELATED_P (insn) = 1;
23450 /* Save AltiVec registers if needed. Save here because the red zone does
23451 not always include AltiVec registers. */
23452 if (!WORLD_SAVE_P (info) && TARGET_ALTIVEC_ABI
23453 && info->altivec_size != 0 && (strategy & SAVE_INLINE_VRS) == 0)
23455 int end_save = info->altivec_save_offset + info->altivec_size;
23456 int ptr_off;
23457 /* Oddly, the vector save/restore functions point r0 at the end
23458 of the save area, then use r11 or r12 to load offsets for
23459 [reg+reg] addressing. */
23460 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
23461 int scratch_regno = ptr_regno_for_savres (SAVRES_SAVE | SAVRES_VR);
23462 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
23464 gcc_checking_assert (scratch_regno == 11 || scratch_regno == 12);
23465 NOT_INUSE (0);
23466 if (end_save + frame_off != 0)
23468 rtx offset = GEN_INT (end_save + frame_off);
23470 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
23472 else
23473 emit_move_insn (ptr_reg, frame_reg_rtx);
23475 ptr_off = -end_save;
23476 insn = rs6000_emit_savres_rtx (info, scratch_reg,
23477 info->altivec_save_offset + ptr_off,
23478 0, V4SImode, SAVRES_SAVE | SAVRES_VR);
23479 rs6000_frame_related (insn, scratch_reg, sp_off - ptr_off,
23480 NULL_RTX, NULL_RTX, NULL_RTX);
23481 if (REGNO (frame_reg_rtx) == REGNO (scratch_reg))
23483 /* The oddity mentioned above clobbered our frame reg. */
23484 emit_move_insn (frame_reg_rtx, ptr_reg);
23485 frame_off = ptr_off;
23488 else if (!WORLD_SAVE_P (info) && TARGET_ALTIVEC_ABI
23489 && info->altivec_size != 0)
23491 int i;
23493 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
23494 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
23496 rtx areg, savereg, mem, split_reg;
23497 int offset;
23499 offset = (info->altivec_save_offset + frame_off
23500 + 16 * (i - info->first_altivec_reg_save));
23502 savereg = gen_rtx_REG (V4SImode, i);
23504 NOT_INUSE (0);
23505 areg = gen_rtx_REG (Pmode, 0);
23506 emit_move_insn (areg, GEN_INT (offset));
23508 /* AltiVec addressing mode is [reg+reg]. */
23509 mem = gen_frame_mem (V4SImode,
23510 gen_rtx_PLUS (Pmode, frame_reg_rtx, areg));
23512 insn = emit_move_insn (mem, savereg);
23514 /* When we split a VSX store into two insns, we need to make
23515 sure the DWARF info knows which register we are storing.
23516 Pass it in to be used on the appropriate note. */
23517 if (!BYTES_BIG_ENDIAN
23518 && GET_CODE (PATTERN (insn)) == SET
23519 && GET_CODE (SET_SRC (PATTERN (insn))) == VEC_SELECT)
23520 split_reg = savereg;
23521 else
23522 split_reg = NULL_RTX;
23524 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
23525 areg, GEN_INT (offset), split_reg);
23529 /* VRSAVE is a bit vector representing which AltiVec registers
23530 are used. The OS uses this to determine which vector
23531 registers to save on a context switch. We need to save
23532 VRSAVE on the stack frame, add whatever AltiVec registers we
23533 used in this function, and do the corresponding magic in the
23534 epilogue. */
23536 if (!WORLD_SAVE_P (info)
23537 && TARGET_ALTIVEC
23538 && TARGET_ALTIVEC_VRSAVE
23539 && info->vrsave_mask != 0)
23541 rtx reg, vrsave;
23542 int offset;
23543 int save_regno;
23545 /* Get VRSAVE onto a GPR. Note that ABI_V4 and ABI_DARWIN might
23546 be using r12 as frame_reg_rtx and r11 as the static chain
23547 pointer for nested functions. */
23548 save_regno = 12;
23549 if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
23550 && !using_static_chain_p)
23551 save_regno = 11;
23552 else if (REGNO (frame_reg_rtx) == 12)
23554 save_regno = 11;
23555 if (using_static_chain_p)
23556 save_regno = 0;
23559 NOT_INUSE (save_regno);
23560 reg = gen_rtx_REG (SImode, save_regno);
23561 vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
23562 if (TARGET_MACHO)
23563 emit_insn (gen_get_vrsave_internal (reg));
23564 else
23565 emit_insn (gen_rtx_SET (VOIDmode, reg, vrsave));
23567 /* Save VRSAVE. */
23568 offset = info->vrsave_save_offset + frame_off;
23569 insn = emit_insn (gen_frame_store (reg, frame_reg_rtx, offset));
23571 /* Include the registers in the mask. */
23572 emit_insn (gen_iorsi3 (reg, reg, GEN_INT ((int) info->vrsave_mask)));
23574 insn = emit_insn (generate_set_vrsave (reg, info, 0));
23577 /* If we are using RS6000_PIC_OFFSET_TABLE_REGNUM, we need to set it up. */
23578 if (!TARGET_SINGLE_PIC_BASE
23579 && ((TARGET_TOC && TARGET_MINIMAL_TOC && get_pool_size () != 0)
23580 || (DEFAULT_ABI == ABI_V4
23581 && (flag_pic == 1 || (flag_pic && TARGET_SECURE_PLT))
23582 && df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))))
23584 /* If emit_load_toc_table will use the link register, we need to save
23585 it. We use R12 for this purpose because emit_load_toc_table
23586 can use register 0. This allows us to use a plain 'blr' to return
23587 from the procedure more often. */
23588 int save_LR_around_toc_setup = (TARGET_ELF
23589 && DEFAULT_ABI == ABI_V4
23590 && flag_pic
23591 && ! info->lr_save_p
23592 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds) > 0);
23593 if (save_LR_around_toc_setup)
23595 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
23596 rtx tmp = gen_rtx_REG (Pmode, 12);
23598 insn = emit_move_insn (tmp, lr);
23599 RTX_FRAME_RELATED_P (insn) = 1;
23601 rs6000_emit_load_toc_table (TRUE);
23603 insn = emit_move_insn (lr, tmp);
23604 add_reg_note (insn, REG_CFA_RESTORE, lr);
23605 RTX_FRAME_RELATED_P (insn) = 1;
23607 else
23608 rs6000_emit_load_toc_table (TRUE);
23611 #if TARGET_MACHO
23612 if (!TARGET_SINGLE_PIC_BASE
23613 && DEFAULT_ABI == ABI_DARWIN
23614 && flag_pic && crtl->uses_pic_offset_table)
23616 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
23617 rtx src = gen_rtx_SYMBOL_REF (Pmode, MACHOPIC_FUNCTION_BASE_NAME);
23619 /* Save and restore LR locally around this call (in R0). */
23620 if (!info->lr_save_p)
23621 emit_move_insn (gen_rtx_REG (Pmode, 0), lr);
23623 emit_insn (gen_load_macho_picbase (src));
23625 emit_move_insn (gen_rtx_REG (Pmode,
23626 RS6000_PIC_OFFSET_TABLE_REGNUM),
23627 lr);
23629 if (!info->lr_save_p)
23630 emit_move_insn (lr, gen_rtx_REG (Pmode, 0));
23632 #endif
23634 /* If we need to, save the TOC register after doing the stack setup.
23635 Do not emit eh frame info for this save. The unwinder wants info,
23636 conceptually attached to instructions in this function, about
23637 register values in the caller of this function. This R2 may have
23638 already been changed from the value in the caller.
23639 We don't attempt to write accurate DWARF EH frame info for R2
23640 because code emitted by gcc for a (non-pointer) function call
23641 doesn't save and restore R2. Instead, R2 is managed out-of-line
23642 by a linker generated plt call stub when the function resides in
23643 a shared library. This behaviour is costly to describe in DWARF,
23644 both in terms of the size of DWARF info and the time taken in the
23645 unwinder to interpret it. R2 changes, apart from the
23646 calls_eh_return case earlier in this function, are handled by
23647 linux-unwind.h frob_update_context. */
23648 if (rs6000_save_toc_in_prologue_p ())
23650 rtx reg = gen_rtx_REG (reg_mode, TOC_REGNUM);
23651 emit_insn (gen_frame_store (reg, sp_reg_rtx, RS6000_TOC_SAVE_SLOT));
23655 /* Write function prologue. */
23657 static void
23658 rs6000_output_function_prologue (FILE *file,
23659 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
23661 rs6000_stack_t *info = rs6000_stack_info ();
23663 if (TARGET_DEBUG_STACK)
23664 debug_stack_info (info);
23666 /* Write .extern for any function we will call to save and restore
23667 fp values. */
23668 if (info->first_fp_reg_save < 64
23669 && !TARGET_MACHO
23670 && !TARGET_ELF)
23672 char *name;
23673 int regno = info->first_fp_reg_save - 32;
23675 if ((info->savres_strategy & SAVE_INLINE_FPRS) == 0)
23677 bool lr = (info->savres_strategy & SAVE_NOINLINE_FPRS_SAVES_LR) != 0;
23678 int sel = SAVRES_SAVE | SAVRES_FPR | (lr ? SAVRES_LR : 0);
23679 name = rs6000_savres_routine_name (info, regno, sel);
23680 fprintf (file, "\t.extern %s\n", name);
23682 if ((info->savres_strategy & REST_INLINE_FPRS) == 0)
23684 bool lr = (info->savres_strategy
23685 & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
23686 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
23687 name = rs6000_savres_routine_name (info, regno, sel);
23688 fprintf (file, "\t.extern %s\n", name);
23692 /* ELFv2 ABI r2 setup code and local entry point. This must follow
23693 immediately after the global entry point label. */
23694 if (DEFAULT_ABI == ABI_ELFv2 && cfun->machine->r2_setup_needed)
23696 const char *name = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
23698 fprintf (file, "0:\taddis 2,12,.TOC.-0b@ha\n");
23699 fprintf (file, "\taddi 2,2,.TOC.-0b@l\n");
23701 fputs ("\t.localentry\t", file);
23702 assemble_name (file, name);
23703 fputs (",.-", file);
23704 assemble_name (file, name);
23705 fputs ("\n", file);
23708 /* Output -mprofile-kernel code. This needs to be done here instead of
23709 in output_function_profile since it must go after the ELFv2 ABI
23710 local entry point. */
23711 if (TARGET_PROFILE_KERNEL && crtl->profile)
23713 gcc_assert (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2);
23714 gcc_assert (!TARGET_32BIT);
23716 asm_fprintf (file, "\tmflr %s\n", reg_names[0]);
23717 asm_fprintf (file, "\tstd %s,16(%s)\n", reg_names[0], reg_names[1]);
23719 /* In the ELFv2 ABI we have no compiler stack word. It must be
23720 the resposibility of _mcount to preserve the static chain
23721 register if required. */
23722 if (DEFAULT_ABI != ABI_ELFv2
23723 && cfun->static_chain_decl != NULL)
23725 asm_fprintf (file, "\tstd %s,24(%s)\n",
23726 reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
23727 fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
23728 asm_fprintf (file, "\tld %s,24(%s)\n",
23729 reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
23731 else
23732 fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
23735 rs6000_pic_labelno++;
23738 /* Non-zero if vmx regs are restored before the frame pop, zero if
23739 we restore after the pop when possible. */
23740 #define ALWAYS_RESTORE_ALTIVEC_BEFORE_POP 0
23742 /* Restoring cr is a two step process: loading a reg from the frame
23743 save, then moving the reg to cr. For ABI_V4 we must let the
23744 unwinder know that the stack location is no longer valid at or
23745 before the stack deallocation, but we can't emit a cfa_restore for
23746 cr at the stack deallocation like we do for other registers.
23747 The trouble is that it is possible for the move to cr to be
23748 scheduled after the stack deallocation. So say exactly where cr
23749 is located on each of the two insns. */
23751 static rtx
23752 load_cr_save (int regno, rtx frame_reg_rtx, int offset, bool exit_func)
23754 rtx mem = gen_frame_mem_offset (SImode, frame_reg_rtx, offset);
23755 rtx reg = gen_rtx_REG (SImode, regno);
23756 rtx insn = emit_move_insn (reg, mem);
23758 if (!exit_func && DEFAULT_ABI == ABI_V4)
23760 rtx cr = gen_rtx_REG (SImode, CR2_REGNO);
23761 rtx set = gen_rtx_SET (VOIDmode, reg, cr);
23763 add_reg_note (insn, REG_CFA_REGISTER, set);
23764 RTX_FRAME_RELATED_P (insn) = 1;
23766 return reg;
23769 /* Reload CR from REG. */
23771 static void
23772 restore_saved_cr (rtx reg, int using_mfcr_multiple, bool exit_func)
23774 int count = 0;
23775 int i;
23777 if (using_mfcr_multiple)
23779 for (i = 0; i < 8; i++)
23780 if (save_reg_p (CR0_REGNO + i))
23781 count++;
23782 gcc_assert (count);
23785 if (using_mfcr_multiple && count > 1)
23787 rtx insn;
23788 rtvec p;
23789 int ndx;
23791 p = rtvec_alloc (count);
23793 ndx = 0;
23794 for (i = 0; i < 8; i++)
23795 if (save_reg_p (CR0_REGNO + i))
23797 rtvec r = rtvec_alloc (2);
23798 RTVEC_ELT (r, 0) = reg;
23799 RTVEC_ELT (r, 1) = GEN_INT (1 << (7-i));
23800 RTVEC_ELT (p, ndx) =
23801 gen_rtx_SET (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO + i),
23802 gen_rtx_UNSPEC (CCmode, r, UNSPEC_MOVESI_TO_CR));
23803 ndx++;
23805 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
23806 gcc_assert (ndx == count);
23808 /* For the ELFv2 ABI we generate a CFA_RESTORE for each
23809 CR field separately. */
23810 if (!exit_func && DEFAULT_ABI == ABI_ELFv2 && flag_shrink_wrap)
23812 for (i = 0; i < 8; i++)
23813 if (save_reg_p (CR0_REGNO + i))
23814 add_reg_note (insn, REG_CFA_RESTORE,
23815 gen_rtx_REG (SImode, CR0_REGNO + i));
23817 RTX_FRAME_RELATED_P (insn) = 1;
23820 else
23821 for (i = 0; i < 8; i++)
23822 if (save_reg_p (CR0_REGNO + i))
23824 rtx insn = emit_insn (gen_movsi_to_cr_one
23825 (gen_rtx_REG (CCmode, CR0_REGNO + i), reg));
23827 /* For the ELFv2 ABI we generate a CFA_RESTORE for each
23828 CR field separately, attached to the insn that in fact
23829 restores this particular CR field. */
23830 if (!exit_func && DEFAULT_ABI == ABI_ELFv2 && flag_shrink_wrap)
23832 add_reg_note (insn, REG_CFA_RESTORE,
23833 gen_rtx_REG (SImode, CR0_REGNO + i));
23835 RTX_FRAME_RELATED_P (insn) = 1;
23839 /* For other ABIs, we just generate a single CFA_RESTORE for CR2. */
23840 if (!exit_func && DEFAULT_ABI != ABI_ELFv2
23841 && (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap))
23843 rtx insn = get_last_insn ();
23844 rtx cr = gen_rtx_REG (SImode, CR2_REGNO);
23846 add_reg_note (insn, REG_CFA_RESTORE, cr);
23847 RTX_FRAME_RELATED_P (insn) = 1;
23851 /* Like cr, the move to lr instruction can be scheduled after the
23852 stack deallocation, but unlike cr, its stack frame save is still
23853 valid. So we only need to emit the cfa_restore on the correct
23854 instruction. */
23856 static void
23857 load_lr_save (int regno, rtx frame_reg_rtx, int offset)
23859 rtx mem = gen_frame_mem_offset (Pmode, frame_reg_rtx, offset);
23860 rtx reg = gen_rtx_REG (Pmode, regno);
23862 emit_move_insn (reg, mem);
23865 static void
23866 restore_saved_lr (int regno, bool exit_func)
23868 rtx reg = gen_rtx_REG (Pmode, regno);
23869 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
23870 rtx insn = emit_move_insn (lr, reg);
23872 if (!exit_func && flag_shrink_wrap)
23874 add_reg_note (insn, REG_CFA_RESTORE, lr);
23875 RTX_FRAME_RELATED_P (insn) = 1;
23879 static rtx
23880 add_crlr_cfa_restore (const rs6000_stack_t *info, rtx cfa_restores)
23882 if (DEFAULT_ABI == ABI_ELFv2)
23884 int i;
23885 for (i = 0; i < 8; i++)
23886 if (save_reg_p (CR0_REGNO + i))
23888 rtx cr = gen_rtx_REG (SImode, CR0_REGNO + i);
23889 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, cr,
23890 cfa_restores);
23893 else if (info->cr_save_p)
23894 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
23895 gen_rtx_REG (SImode, CR2_REGNO),
23896 cfa_restores);
23898 if (info->lr_save_p)
23899 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
23900 gen_rtx_REG (Pmode, LR_REGNO),
23901 cfa_restores);
23902 return cfa_restores;
23905 /* Return true if OFFSET from stack pointer can be clobbered by signals.
23906 V.4 doesn't have any stack cushion, AIX ABIs have 220 or 288 bytes
23907 below stack pointer not cloberred by signals. */
23909 static inline bool
23910 offset_below_red_zone_p (HOST_WIDE_INT offset)
23912 return offset < (DEFAULT_ABI == ABI_V4
23914 : TARGET_32BIT ? -220 : -288);
23917 /* Append CFA_RESTORES to any existing REG_NOTES on the last insn. */
23919 static void
23920 emit_cfa_restores (rtx cfa_restores)
23922 rtx insn = get_last_insn ();
23923 rtx *loc = &REG_NOTES (insn);
23925 while (*loc)
23926 loc = &XEXP (*loc, 1);
23927 *loc = cfa_restores;
23928 RTX_FRAME_RELATED_P (insn) = 1;
23931 /* Emit function epilogue as insns. */
23933 void
23934 rs6000_emit_epilogue (int sibcall)
23936 rs6000_stack_t *info;
23937 int restoring_GPRs_inline;
23938 int restoring_FPRs_inline;
23939 int using_load_multiple;
23940 int using_mtcr_multiple;
23941 int use_backchain_to_restore_sp;
23942 int restore_lr;
23943 int strategy;
23944 HOST_WIDE_INT frame_off = 0;
23945 rtx sp_reg_rtx = gen_rtx_REG (Pmode, 1);
23946 rtx frame_reg_rtx = sp_reg_rtx;
23947 rtx cfa_restores = NULL_RTX;
23948 rtx insn;
23949 rtx cr_save_reg = NULL_RTX;
23950 enum machine_mode reg_mode = Pmode;
23951 int reg_size = TARGET_32BIT ? 4 : 8;
23952 int i;
23953 bool exit_func;
23954 unsigned ptr_regno;
23956 info = rs6000_stack_info ();
23958 if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
23960 reg_mode = V2SImode;
23961 reg_size = 8;
23964 strategy = info->savres_strategy;
23965 using_load_multiple = strategy & SAVRES_MULTIPLE;
23966 restoring_FPRs_inline = sibcall || (strategy & REST_INLINE_FPRS);
23967 restoring_GPRs_inline = sibcall || (strategy & REST_INLINE_GPRS);
23968 using_mtcr_multiple = (rs6000_cpu == PROCESSOR_PPC601
23969 || rs6000_cpu == PROCESSOR_PPC603
23970 || rs6000_cpu == PROCESSOR_PPC750
23971 || optimize_size);
23972 /* Restore via the backchain when we have a large frame, since this
23973 is more efficient than an addis, addi pair. The second condition
23974 here will not trigger at the moment; We don't actually need a
23975 frame pointer for alloca, but the generic parts of the compiler
23976 give us one anyway. */
23977 use_backchain_to_restore_sp = (info->total_size > 32767 - info->lr_save_offset
23978 || (cfun->calls_alloca
23979 && !frame_pointer_needed));
23980 restore_lr = (info->lr_save_p
23981 && (restoring_FPRs_inline
23982 || (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR))
23983 && (restoring_GPRs_inline
23984 || info->first_fp_reg_save < 64));
23986 if (WORLD_SAVE_P (info))
23988 int i, j;
23989 char rname[30];
23990 const char *alloc_rname;
23991 rtvec p;
23993 /* eh_rest_world_r10 will return to the location saved in the LR
23994 stack slot (which is not likely to be our caller.)
23995 Input: R10 -- stack adjustment. Clobbers R0, R11, R12, R7, R8.
23996 rest_world is similar, except any R10 parameter is ignored.
23997 The exception-handling stuff that was here in 2.95 is no
23998 longer necessary. */
24000 p = rtvec_alloc (9
24002 + 32 - info->first_gp_reg_save
24003 + LAST_ALTIVEC_REGNO + 1 - info->first_altivec_reg_save
24004 + 63 + 1 - info->first_fp_reg_save);
24006 strcpy (rname, ((crtl->calls_eh_return) ?
24007 "*eh_rest_world_r10" : "*rest_world"));
24008 alloc_rname = ggc_strdup (rname);
24010 j = 0;
24011 RTVEC_ELT (p, j++) = ret_rtx;
24012 RTVEC_ELT (p, j++) = gen_rtx_USE (VOIDmode,
24013 gen_rtx_REG (Pmode,
24014 LR_REGNO));
24015 RTVEC_ELT (p, j++)
24016 = gen_rtx_USE (VOIDmode, gen_rtx_SYMBOL_REF (Pmode, alloc_rname));
24017 /* The instruction pattern requires a clobber here;
24018 it is shared with the restVEC helper. */
24019 RTVEC_ELT (p, j++)
24020 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 11));
24023 /* CR register traditionally saved as CR2. */
24024 rtx reg = gen_rtx_REG (SImode, CR2_REGNO);
24025 RTVEC_ELT (p, j++)
24026 = gen_frame_load (reg, frame_reg_rtx, info->cr_save_offset);
24027 if (flag_shrink_wrap)
24029 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
24030 gen_rtx_REG (Pmode, LR_REGNO),
24031 cfa_restores);
24032 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
24036 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
24038 rtx reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
24039 RTVEC_ELT (p, j++)
24040 = gen_frame_load (reg,
24041 frame_reg_rtx, info->gp_save_offset + reg_size * i);
24042 if (flag_shrink_wrap)
24043 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
24045 for (i = 0; info->first_altivec_reg_save + i <= LAST_ALTIVEC_REGNO; i++)
24047 rtx reg = gen_rtx_REG (V4SImode, info->first_altivec_reg_save + i);
24048 RTVEC_ELT (p, j++)
24049 = gen_frame_load (reg,
24050 frame_reg_rtx, info->altivec_save_offset + 16 * i);
24051 if (flag_shrink_wrap)
24052 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
24054 for (i = 0; info->first_fp_reg_save + i <= 63; i++)
24056 rtx reg = gen_rtx_REG ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
24057 ? DFmode : SFmode),
24058 info->first_fp_reg_save + i);
24059 RTVEC_ELT (p, j++)
24060 = gen_frame_load (reg, frame_reg_rtx, info->fp_save_offset + 8 * i);
24061 if (flag_shrink_wrap)
24062 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
24064 RTVEC_ELT (p, j++)
24065 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 0));
24066 RTVEC_ELT (p, j++)
24067 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 12));
24068 RTVEC_ELT (p, j++)
24069 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 7));
24070 RTVEC_ELT (p, j++)
24071 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 8));
24072 RTVEC_ELT (p, j++)
24073 = gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, 10));
24074 insn = emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
24076 if (flag_shrink_wrap)
24078 REG_NOTES (insn) = cfa_restores;
24079 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
24080 RTX_FRAME_RELATED_P (insn) = 1;
24082 return;
24085 /* frame_reg_rtx + frame_off points to the top of this stack frame. */
24086 if (info->push_p)
24087 frame_off = info->total_size;
24089 /* Restore AltiVec registers if we must do so before adjusting the
24090 stack. */
24091 if (TARGET_ALTIVEC_ABI
24092 && info->altivec_size != 0
24093 && (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
24094 || (DEFAULT_ABI != ABI_V4
24095 && offset_below_red_zone_p (info->altivec_save_offset))))
24097 int i;
24098 int scratch_regno = ptr_regno_for_savres (SAVRES_VR);
24100 gcc_checking_assert (scratch_regno == 11 || scratch_regno == 12);
24101 if (use_backchain_to_restore_sp)
24103 int frame_regno = 11;
24105 if ((strategy & REST_INLINE_VRS) == 0)
24107 /* Of r11 and r12, select the one not clobbered by an
24108 out-of-line restore function for the frame register. */
24109 frame_regno = 11 + 12 - scratch_regno;
24111 frame_reg_rtx = gen_rtx_REG (Pmode, frame_regno);
24112 emit_move_insn (frame_reg_rtx,
24113 gen_rtx_MEM (Pmode, sp_reg_rtx));
24114 frame_off = 0;
24116 else if (frame_pointer_needed)
24117 frame_reg_rtx = hard_frame_pointer_rtx;
24119 if ((strategy & REST_INLINE_VRS) == 0)
24121 int end_save = info->altivec_save_offset + info->altivec_size;
24122 int ptr_off;
24123 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
24124 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
24126 if (end_save + frame_off != 0)
24128 rtx offset = GEN_INT (end_save + frame_off);
24130 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
24132 else
24133 emit_move_insn (ptr_reg, frame_reg_rtx);
24135 ptr_off = -end_save;
24136 insn = rs6000_emit_savres_rtx (info, scratch_reg,
24137 info->altivec_save_offset + ptr_off,
24138 0, V4SImode, SAVRES_VR);
24140 else
24142 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
24143 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
24145 rtx addr, areg, mem, reg;
24147 areg = gen_rtx_REG (Pmode, 0);
24148 emit_move_insn
24149 (areg, GEN_INT (info->altivec_save_offset
24150 + frame_off
24151 + 16 * (i - info->first_altivec_reg_save)));
24153 /* AltiVec addressing mode is [reg+reg]. */
24154 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
24155 mem = gen_frame_mem (V4SImode, addr);
24157 reg = gen_rtx_REG (V4SImode, i);
24158 emit_move_insn (reg, mem);
24162 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
24163 if (((strategy & REST_INLINE_VRS) == 0
24164 || (info->vrsave_mask & ALTIVEC_REG_BIT (i)) != 0)
24165 && (flag_shrink_wrap
24166 || (offset_below_red_zone_p
24167 (info->altivec_save_offset
24168 + 16 * (i - info->first_altivec_reg_save)))))
24170 rtx reg = gen_rtx_REG (V4SImode, i);
24171 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
24175 /* Restore VRSAVE if we must do so before adjusting the stack. */
24176 if (TARGET_ALTIVEC
24177 && TARGET_ALTIVEC_VRSAVE
24178 && info->vrsave_mask != 0
24179 && (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
24180 || (DEFAULT_ABI != ABI_V4
24181 && offset_below_red_zone_p (info->vrsave_save_offset))))
24183 rtx reg;
24185 if (frame_reg_rtx == sp_reg_rtx)
24187 if (use_backchain_to_restore_sp)
24189 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
24190 emit_move_insn (frame_reg_rtx,
24191 gen_rtx_MEM (Pmode, sp_reg_rtx));
24192 frame_off = 0;
24194 else if (frame_pointer_needed)
24195 frame_reg_rtx = hard_frame_pointer_rtx;
24198 reg = gen_rtx_REG (SImode, 12);
24199 emit_insn (gen_frame_load (reg, frame_reg_rtx,
24200 info->vrsave_save_offset + frame_off));
24202 emit_insn (generate_set_vrsave (reg, info, 1));
24205 insn = NULL_RTX;
24206 /* If we have a large stack frame, restore the old stack pointer
24207 using the backchain. */
24208 if (use_backchain_to_restore_sp)
24210 if (frame_reg_rtx == sp_reg_rtx)
24212 /* Under V.4, don't reset the stack pointer until after we're done
24213 loading the saved registers. */
24214 if (DEFAULT_ABI == ABI_V4)
24215 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
24217 insn = emit_move_insn (frame_reg_rtx,
24218 gen_rtx_MEM (Pmode, sp_reg_rtx));
24219 frame_off = 0;
24221 else if (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
24222 && DEFAULT_ABI == ABI_V4)
24223 /* frame_reg_rtx has been set up by the altivec restore. */
24225 else
24227 insn = emit_move_insn (sp_reg_rtx, frame_reg_rtx);
24228 frame_reg_rtx = sp_reg_rtx;
24231 /* If we have a frame pointer, we can restore the old stack pointer
24232 from it. */
24233 else if (frame_pointer_needed)
24235 frame_reg_rtx = sp_reg_rtx;
24236 if (DEFAULT_ABI == ABI_V4)
24237 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
24238 /* Prevent reordering memory accesses against stack pointer restore. */
24239 else if (cfun->calls_alloca
24240 || offset_below_red_zone_p (-info->total_size))
24241 rs6000_emit_stack_tie (frame_reg_rtx, true);
24243 insn = emit_insn (gen_add3_insn (frame_reg_rtx, hard_frame_pointer_rtx,
24244 GEN_INT (info->total_size)));
24245 frame_off = 0;
24247 else if (info->push_p
24248 && DEFAULT_ABI != ABI_V4
24249 && !crtl->calls_eh_return)
24251 /* Prevent reordering memory accesses against stack pointer restore. */
24252 if (cfun->calls_alloca
24253 || offset_below_red_zone_p (-info->total_size))
24254 rs6000_emit_stack_tie (frame_reg_rtx, false);
24255 insn = emit_insn (gen_add3_insn (sp_reg_rtx, sp_reg_rtx,
24256 GEN_INT (info->total_size)));
24257 frame_off = 0;
24259 if (insn && frame_reg_rtx == sp_reg_rtx)
24261 if (cfa_restores)
24263 REG_NOTES (insn) = cfa_restores;
24264 cfa_restores = NULL_RTX;
24266 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
24267 RTX_FRAME_RELATED_P (insn) = 1;
24270 /* Restore AltiVec registers if we have not done so already. */
24271 if (!ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
24272 && TARGET_ALTIVEC_ABI
24273 && info->altivec_size != 0
24274 && (DEFAULT_ABI == ABI_V4
24275 || !offset_below_red_zone_p (info->altivec_save_offset)))
24277 int i;
24279 if ((strategy & REST_INLINE_VRS) == 0)
24281 int end_save = info->altivec_save_offset + info->altivec_size;
24282 int ptr_off;
24283 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
24284 int scratch_regno = ptr_regno_for_savres (SAVRES_VR);
24285 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
24287 if (end_save + frame_off != 0)
24289 rtx offset = GEN_INT (end_save + frame_off);
24291 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
24293 else
24294 emit_move_insn (ptr_reg, frame_reg_rtx);
24296 ptr_off = -end_save;
24297 insn = rs6000_emit_savres_rtx (info, scratch_reg,
24298 info->altivec_save_offset + ptr_off,
24299 0, V4SImode, SAVRES_VR);
24300 if (REGNO (frame_reg_rtx) == REGNO (scratch_reg))
24302 /* Frame reg was clobbered by out-of-line save. Restore it
24303 from ptr_reg, and if we are calling out-of-line gpr or
24304 fpr restore set up the correct pointer and offset. */
24305 unsigned newptr_regno = 1;
24306 if (!restoring_GPRs_inline)
24308 bool lr = info->gp_save_offset + info->gp_size == 0;
24309 int sel = SAVRES_GPR | (lr ? SAVRES_LR : 0);
24310 newptr_regno = ptr_regno_for_savres (sel);
24311 end_save = info->gp_save_offset + info->gp_size;
24313 else if (!restoring_FPRs_inline)
24315 bool lr = !(strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR);
24316 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
24317 newptr_regno = ptr_regno_for_savres (sel);
24318 end_save = info->gp_save_offset + info->gp_size;
24321 if (newptr_regno != 1 && REGNO (frame_reg_rtx) != newptr_regno)
24322 frame_reg_rtx = gen_rtx_REG (Pmode, newptr_regno);
24324 if (end_save + ptr_off != 0)
24326 rtx offset = GEN_INT (end_save + ptr_off);
24328 frame_off = -end_save;
24329 emit_insn (gen_add3_insn (frame_reg_rtx, ptr_reg, offset));
24331 else
24333 frame_off = ptr_off;
24334 emit_move_insn (frame_reg_rtx, ptr_reg);
24338 else
24340 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
24341 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
24343 rtx addr, areg, mem, reg;
24345 areg = gen_rtx_REG (Pmode, 0);
24346 emit_move_insn
24347 (areg, GEN_INT (info->altivec_save_offset
24348 + frame_off
24349 + 16 * (i - info->first_altivec_reg_save)));
24351 /* AltiVec addressing mode is [reg+reg]. */
24352 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
24353 mem = gen_frame_mem (V4SImode, addr);
24355 reg = gen_rtx_REG (V4SImode, i);
24356 emit_move_insn (reg, mem);
24360 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
24361 if (((strategy & REST_INLINE_VRS) == 0
24362 || (info->vrsave_mask & ALTIVEC_REG_BIT (i)) != 0)
24363 && (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap))
24365 rtx reg = gen_rtx_REG (V4SImode, i);
24366 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
24370 /* Restore VRSAVE if we have not done so already. */
24371 if (!ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
24372 && TARGET_ALTIVEC
24373 && TARGET_ALTIVEC_VRSAVE
24374 && info->vrsave_mask != 0
24375 && (DEFAULT_ABI == ABI_V4
24376 || !offset_below_red_zone_p (info->vrsave_save_offset)))
24378 rtx reg;
24380 reg = gen_rtx_REG (SImode, 12);
24381 emit_insn (gen_frame_load (reg, frame_reg_rtx,
24382 info->vrsave_save_offset + frame_off));
24384 emit_insn (generate_set_vrsave (reg, info, 1));
24387 /* If we exit by an out-of-line restore function on ABI_V4 then that
24388 function will deallocate the stack, so we don't need to worry
24389 about the unwinder restoring cr from an invalid stack frame
24390 location. */
24391 exit_func = (!restoring_FPRs_inline
24392 || (!restoring_GPRs_inline
24393 && info->first_fp_reg_save == 64));
24395 /* In the ELFv2 ABI we need to restore all call-saved CR fields from
24396 *separate* slots if the routine calls __builtin_eh_return, so
24397 that they can be independently restored by the unwinder. */
24398 if (DEFAULT_ABI == ABI_ELFv2 && crtl->calls_eh_return)
24400 int i, cr_off = info->ehcr_offset;
24402 for (i = 0; i < 8; i++)
24403 if (!call_used_regs[CR0_REGNO + i])
24405 rtx reg = gen_rtx_REG (SImode, 0);
24406 emit_insn (gen_frame_load (reg, frame_reg_rtx,
24407 cr_off + frame_off));
24409 insn = emit_insn (gen_movsi_to_cr_one
24410 (gen_rtx_REG (CCmode, CR0_REGNO + i), reg));
24412 if (!exit_func && flag_shrink_wrap)
24414 add_reg_note (insn, REG_CFA_RESTORE,
24415 gen_rtx_REG (SImode, CR0_REGNO + i));
24417 RTX_FRAME_RELATED_P (insn) = 1;
24420 cr_off += reg_size;
24424 /* Get the old lr if we saved it. If we are restoring registers
24425 out-of-line, then the out-of-line routines can do this for us. */
24426 if (restore_lr && restoring_GPRs_inline)
24427 load_lr_save (0, frame_reg_rtx, info->lr_save_offset + frame_off);
24429 /* Get the old cr if we saved it. */
24430 if (info->cr_save_p)
24432 unsigned cr_save_regno = 12;
24434 if (!restoring_GPRs_inline)
24436 /* Ensure we don't use the register used by the out-of-line
24437 gpr register restore below. */
24438 bool lr = info->gp_save_offset + info->gp_size == 0;
24439 int sel = SAVRES_GPR | (lr ? SAVRES_LR : 0);
24440 int gpr_ptr_regno = ptr_regno_for_savres (sel);
24442 if (gpr_ptr_regno == 12)
24443 cr_save_regno = 11;
24444 gcc_checking_assert (REGNO (frame_reg_rtx) != cr_save_regno);
24446 else if (REGNO (frame_reg_rtx) == 12)
24447 cr_save_regno = 11;
24449 cr_save_reg = load_cr_save (cr_save_regno, frame_reg_rtx,
24450 info->cr_save_offset + frame_off,
24451 exit_func);
24454 /* Set LR here to try to overlap restores below. */
24455 if (restore_lr && restoring_GPRs_inline)
24456 restore_saved_lr (0, exit_func);
24458 /* Load exception handler data registers, if needed. */
24459 if (crtl->calls_eh_return)
24461 unsigned int i, regno;
24463 if (TARGET_AIX)
24465 rtx reg = gen_rtx_REG (reg_mode, 2);
24466 emit_insn (gen_frame_load (reg, frame_reg_rtx,
24467 frame_off + RS6000_TOC_SAVE_SLOT));
24470 for (i = 0; ; ++i)
24472 rtx mem;
24474 regno = EH_RETURN_DATA_REGNO (i);
24475 if (regno == INVALID_REGNUM)
24476 break;
24478 /* Note: possible use of r0 here to address SPE regs. */
24479 mem = gen_frame_mem_offset (reg_mode, frame_reg_rtx,
24480 info->ehrd_offset + frame_off
24481 + reg_size * (int) i);
24483 emit_move_insn (gen_rtx_REG (reg_mode, regno), mem);
24487 /* Restore GPRs. This is done as a PARALLEL if we are using
24488 the load-multiple instructions. */
24489 if (TARGET_SPE_ABI
24490 && info->spe_64bit_regs_used
24491 && info->first_gp_reg_save != 32)
24493 /* Determine whether we can address all of the registers that need
24494 to be saved with an offset from frame_reg_rtx that fits in
24495 the small const field for SPE memory instructions. */
24496 int spe_regs_addressable
24497 = (SPE_CONST_OFFSET_OK (info->spe_gp_save_offset + frame_off
24498 + reg_size * (32 - info->first_gp_reg_save - 1))
24499 && restoring_GPRs_inline);
24501 if (!spe_regs_addressable)
24503 int ool_adjust = 0;
24504 rtx old_frame_reg_rtx = frame_reg_rtx;
24505 /* Make r11 point to the start of the SPE save area. We worried about
24506 not clobbering it when we were saving registers in the prologue.
24507 There's no need to worry here because the static chain is passed
24508 anew to every function. */
24510 if (!restoring_GPRs_inline)
24511 ool_adjust = 8 * (info->first_gp_reg_save - FIRST_SAVED_GP_REGNO);
24512 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
24513 emit_insn (gen_addsi3 (frame_reg_rtx, old_frame_reg_rtx,
24514 GEN_INT (info->spe_gp_save_offset
24515 + frame_off
24516 - ool_adjust)));
24517 /* Keep the invariant that frame_reg_rtx + frame_off points
24518 at the top of the stack frame. */
24519 frame_off = -info->spe_gp_save_offset + ool_adjust;
24522 if (restoring_GPRs_inline)
24524 HOST_WIDE_INT spe_offset = info->spe_gp_save_offset + frame_off;
24526 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
24527 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
24529 rtx offset, addr, mem, reg;
24531 /* We're doing all this to ensure that the immediate offset
24532 fits into the immediate field of 'evldd'. */
24533 gcc_assert (SPE_CONST_OFFSET_OK (spe_offset + reg_size * i));
24535 offset = GEN_INT (spe_offset + reg_size * i);
24536 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, offset);
24537 mem = gen_rtx_MEM (V2SImode, addr);
24538 reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
24540 emit_move_insn (reg, mem);
24543 else
24544 rs6000_emit_savres_rtx (info, frame_reg_rtx,
24545 info->spe_gp_save_offset + frame_off,
24546 info->lr_save_offset + frame_off,
24547 reg_mode,
24548 SAVRES_GPR | SAVRES_LR);
24550 else if (!restoring_GPRs_inline)
24552 /* We are jumping to an out-of-line function. */
24553 rtx ptr_reg;
24554 int end_save = info->gp_save_offset + info->gp_size;
24555 bool can_use_exit = end_save == 0;
24556 int sel = SAVRES_GPR | (can_use_exit ? SAVRES_LR : 0);
24557 int ptr_off;
24559 /* Emit stack reset code if we need it. */
24560 ptr_regno = ptr_regno_for_savres (sel);
24561 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
24562 if (can_use_exit)
24563 rs6000_emit_stack_reset (info, frame_reg_rtx, frame_off, ptr_regno);
24564 else if (end_save + frame_off != 0)
24565 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx,
24566 GEN_INT (end_save + frame_off)));
24567 else if (REGNO (frame_reg_rtx) != ptr_regno)
24568 emit_move_insn (ptr_reg, frame_reg_rtx);
24569 if (REGNO (frame_reg_rtx) == ptr_regno)
24570 frame_off = -end_save;
24572 if (can_use_exit && info->cr_save_p)
24573 restore_saved_cr (cr_save_reg, using_mtcr_multiple, true);
24575 ptr_off = -end_save;
24576 rs6000_emit_savres_rtx (info, ptr_reg,
24577 info->gp_save_offset + ptr_off,
24578 info->lr_save_offset + ptr_off,
24579 reg_mode, sel);
24581 else if (using_load_multiple)
24583 rtvec p;
24584 p = rtvec_alloc (32 - info->first_gp_reg_save);
24585 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
24586 RTVEC_ELT (p, i)
24587 = gen_frame_load (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
24588 frame_reg_rtx,
24589 info->gp_save_offset + frame_off + reg_size * i);
24590 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
24592 else
24594 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
24595 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
24596 emit_insn (gen_frame_load
24597 (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
24598 frame_reg_rtx,
24599 info->gp_save_offset + frame_off + reg_size * i));
24602 if (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
24604 /* If the frame pointer was used then we can't delay emitting
24605 a REG_CFA_DEF_CFA note. This must happen on the insn that
24606 restores the frame pointer, r31. We may have already emitted
24607 a REG_CFA_DEF_CFA note, but that's OK; A duplicate is
24608 discarded by dwarf2cfi.c/dwarf2out.c, and in any case would
24609 be harmless if emitted. */
24610 if (frame_pointer_needed)
24612 insn = get_last_insn ();
24613 add_reg_note (insn, REG_CFA_DEF_CFA,
24614 plus_constant (Pmode, frame_reg_rtx, frame_off));
24615 RTX_FRAME_RELATED_P (insn) = 1;
24618 /* Set up cfa_restores. We always need these when
24619 shrink-wrapping. If not shrink-wrapping then we only need
24620 the cfa_restore when the stack location is no longer valid.
24621 The cfa_restores must be emitted on or before the insn that
24622 invalidates the stack, and of course must not be emitted
24623 before the insn that actually does the restore. The latter
24624 is why it is a bad idea to emit the cfa_restores as a group
24625 on the last instruction here that actually does a restore:
24626 That insn may be reordered with respect to others doing
24627 restores. */
24628 if (flag_shrink_wrap
24629 && !restoring_GPRs_inline
24630 && info->first_fp_reg_save == 64)
24631 cfa_restores = add_crlr_cfa_restore (info, cfa_restores);
24633 for (i = info->first_gp_reg_save; i < 32; i++)
24634 if (!restoring_GPRs_inline
24635 || using_load_multiple
24636 || rs6000_reg_live_or_pic_offset_p (i))
24638 rtx reg = gen_rtx_REG (reg_mode, i);
24640 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
24644 if (!restoring_GPRs_inline
24645 && info->first_fp_reg_save == 64)
24647 /* We are jumping to an out-of-line function. */
24648 if (cfa_restores)
24649 emit_cfa_restores (cfa_restores);
24650 return;
24653 if (restore_lr && !restoring_GPRs_inline)
24655 load_lr_save (0, frame_reg_rtx, info->lr_save_offset + frame_off);
24656 restore_saved_lr (0, exit_func);
24659 /* Restore fpr's if we need to do it without calling a function. */
24660 if (restoring_FPRs_inline)
24661 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
24662 if (save_reg_p (info->first_fp_reg_save + i))
24664 rtx reg = gen_rtx_REG ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
24665 ? DFmode : SFmode),
24666 info->first_fp_reg_save + i);
24667 emit_insn (gen_frame_load (reg, frame_reg_rtx,
24668 info->fp_save_offset + frame_off + 8 * i));
24669 if (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
24670 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
24673 /* If we saved cr, restore it here. Just those that were used. */
24674 if (info->cr_save_p)
24675 restore_saved_cr (cr_save_reg, using_mtcr_multiple, exit_func);
24677 /* If this is V.4, unwind the stack pointer after all of the loads
24678 have been done, or set up r11 if we are restoring fp out of line. */
24679 ptr_regno = 1;
24680 if (!restoring_FPRs_inline)
24682 bool lr = (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
24683 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
24684 ptr_regno = ptr_regno_for_savres (sel);
24687 insn = rs6000_emit_stack_reset (info, frame_reg_rtx, frame_off, ptr_regno);
24688 if (REGNO (frame_reg_rtx) == ptr_regno)
24689 frame_off = 0;
24691 if (insn && restoring_FPRs_inline)
24693 if (cfa_restores)
24695 REG_NOTES (insn) = cfa_restores;
24696 cfa_restores = NULL_RTX;
24698 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
24699 RTX_FRAME_RELATED_P (insn) = 1;
24702 if (crtl->calls_eh_return)
24704 rtx sa = EH_RETURN_STACKADJ_RTX;
24705 emit_insn (gen_add3_insn (sp_reg_rtx, sp_reg_rtx, sa));
24708 if (!sibcall)
24710 rtvec p;
24711 bool lr = (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
24712 if (! restoring_FPRs_inline)
24714 p = rtvec_alloc (4 + 64 - info->first_fp_reg_save);
24715 RTVEC_ELT (p, 0) = ret_rtx;
24717 else
24719 if (cfa_restores)
24721 /* We can't hang the cfa_restores off a simple return,
24722 since the shrink-wrap code sometimes uses an existing
24723 return. This means there might be a path from
24724 pre-prologue code to this return, and dwarf2cfi code
24725 wants the eh_frame unwinder state to be the same on
24726 all paths to any point. So we need to emit the
24727 cfa_restores before the return. For -m64 we really
24728 don't need epilogue cfa_restores at all, except for
24729 this irritating dwarf2cfi with shrink-wrap
24730 requirement; The stack red-zone means eh_frame info
24731 from the prologue telling the unwinder to restore
24732 from the stack is perfectly good right to the end of
24733 the function. */
24734 emit_insn (gen_blockage ());
24735 emit_cfa_restores (cfa_restores);
24736 cfa_restores = NULL_RTX;
24738 p = rtvec_alloc (2);
24739 RTVEC_ELT (p, 0) = simple_return_rtx;
24742 RTVEC_ELT (p, 1) = ((restoring_FPRs_inline || !lr)
24743 ? gen_rtx_USE (VOIDmode,
24744 gen_rtx_REG (Pmode, LR_REGNO))
24745 : gen_rtx_CLOBBER (VOIDmode,
24746 gen_rtx_REG (Pmode, LR_REGNO)));
24748 /* If we have to restore more than two FP registers, branch to the
24749 restore function. It will return to our caller. */
24750 if (! restoring_FPRs_inline)
24752 int i;
24753 int reg;
24754 rtx sym;
24756 if (flag_shrink_wrap)
24757 cfa_restores = add_crlr_cfa_restore (info, cfa_restores);
24759 sym = rs6000_savres_routine_sym (info,
24760 SAVRES_FPR | (lr ? SAVRES_LR : 0));
24761 RTVEC_ELT (p, 2) = gen_rtx_USE (VOIDmode, sym);
24762 reg = (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)? 1 : 11;
24763 RTVEC_ELT (p, 3) = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, reg));
24765 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
24767 rtx reg = gen_rtx_REG (DFmode, info->first_fp_reg_save + i);
24769 RTVEC_ELT (p, i + 4)
24770 = gen_frame_load (reg, sp_reg_rtx, info->fp_save_offset + 8 * i);
24771 if (flag_shrink_wrap)
24772 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg,
24773 cfa_restores);
24777 emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
24780 if (cfa_restores)
24782 if (sibcall)
24783 /* Ensure the cfa_restores are hung off an insn that won't
24784 be reordered above other restores. */
24785 emit_insn (gen_blockage ());
24787 emit_cfa_restores (cfa_restores);
24791 /* Write function epilogue. */
24793 static void
24794 rs6000_output_function_epilogue (FILE *file,
24795 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
24797 #if TARGET_MACHO
24798 macho_branch_islands ();
24799 /* Mach-O doesn't support labels at the end of objects, so if
24800 it looks like we might want one, insert a NOP. */
24802 rtx insn = get_last_insn ();
24803 rtx deleted_debug_label = NULL_RTX;
24804 while (insn
24805 && NOTE_P (insn)
24806 && NOTE_KIND (insn) != NOTE_INSN_DELETED_LABEL)
24808 /* Don't insert a nop for NOTE_INSN_DELETED_DEBUG_LABEL
24809 notes only, instead set their CODE_LABEL_NUMBER to -1,
24810 otherwise there would be code generation differences
24811 in between -g and -g0. */
24812 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
24813 deleted_debug_label = insn;
24814 insn = PREV_INSN (insn);
24816 if (insn
24817 && (LABEL_P (insn)
24818 || (NOTE_P (insn)
24819 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL)))
24820 fputs ("\tnop\n", file);
24821 else if (deleted_debug_label)
24822 for (insn = deleted_debug_label; insn; insn = NEXT_INSN (insn))
24823 if (NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
24824 CODE_LABEL_NUMBER (insn) = -1;
24826 #endif
24828 /* Output a traceback table here. See /usr/include/sys/debug.h for info
24829 on its format.
24831 We don't output a traceback table if -finhibit-size-directive was
24832 used. The documentation for -finhibit-size-directive reads
24833 ``don't output a @code{.size} assembler directive, or anything
24834 else that would cause trouble if the function is split in the
24835 middle, and the two halves are placed at locations far apart in
24836 memory.'' The traceback table has this property, since it
24837 includes the offset from the start of the function to the
24838 traceback table itself.
24840 System V.4 Powerpc's (and the embedded ABI derived from it) use a
24841 different traceback table. */
24842 if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
24843 && ! flag_inhibit_size_directive
24844 && rs6000_traceback != traceback_none && !cfun->is_thunk)
24846 const char *fname = NULL;
24847 const char *language_string = lang_hooks.name;
24848 int fixed_parms = 0, float_parms = 0, parm_info = 0;
24849 int i;
24850 int optional_tbtab;
24851 rs6000_stack_t *info = rs6000_stack_info ();
24853 if (rs6000_traceback == traceback_full)
24854 optional_tbtab = 1;
24855 else if (rs6000_traceback == traceback_part)
24856 optional_tbtab = 0;
24857 else
24858 optional_tbtab = !optimize_size && !TARGET_ELF;
24860 if (optional_tbtab)
24862 fname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
24863 while (*fname == '.') /* V.4 encodes . in the name */
24864 fname++;
24866 /* Need label immediately before tbtab, so we can compute
24867 its offset from the function start. */
24868 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
24869 ASM_OUTPUT_LABEL (file, fname);
24872 /* The .tbtab pseudo-op can only be used for the first eight
24873 expressions, since it can't handle the possibly variable
24874 length fields that follow. However, if you omit the optional
24875 fields, the assembler outputs zeros for all optional fields
24876 anyways, giving each variable length field is minimum length
24877 (as defined in sys/debug.h). Thus we can not use the .tbtab
24878 pseudo-op at all. */
24880 /* An all-zero word flags the start of the tbtab, for debuggers
24881 that have to find it by searching forward from the entry
24882 point or from the current pc. */
24883 fputs ("\t.long 0\n", file);
24885 /* Tbtab format type. Use format type 0. */
24886 fputs ("\t.byte 0,", file);
24888 /* Language type. Unfortunately, there does not seem to be any
24889 official way to discover the language being compiled, so we
24890 use language_string.
24891 C is 0. Fortran is 1. Pascal is 2. Ada is 3. C++ is 9.
24892 Java is 13. Objective-C is 14. Objective-C++ isn't assigned
24893 a number, so for now use 9. LTO and Go aren't assigned numbers
24894 either, so for now use 0. */
24895 if (! strcmp (language_string, "GNU C")
24896 || ! strcmp (language_string, "GNU GIMPLE")
24897 || ! strcmp (language_string, "GNU Go"))
24898 i = 0;
24899 else if (! strcmp (language_string, "GNU F77")
24900 || ! strcmp (language_string, "GNU Fortran"))
24901 i = 1;
24902 else if (! strcmp (language_string, "GNU Pascal"))
24903 i = 2;
24904 else if (! strcmp (language_string, "GNU Ada"))
24905 i = 3;
24906 else if (! strcmp (language_string, "GNU C++")
24907 || ! strcmp (language_string, "GNU Objective-C++"))
24908 i = 9;
24909 else if (! strcmp (language_string, "GNU Java"))
24910 i = 13;
24911 else if (! strcmp (language_string, "GNU Objective-C"))
24912 i = 14;
24913 else
24914 gcc_unreachable ();
24915 fprintf (file, "%d,", i);
24917 /* 8 single bit fields: global linkage (not set for C extern linkage,
24918 apparently a PL/I convention?), out-of-line epilogue/prologue, offset
24919 from start of procedure stored in tbtab, internal function, function
24920 has controlled storage, function has no toc, function uses fp,
24921 function logs/aborts fp operations. */
24922 /* Assume that fp operations are used if any fp reg must be saved. */
24923 fprintf (file, "%d,",
24924 (optional_tbtab << 5) | ((info->first_fp_reg_save != 64) << 1));
24926 /* 6 bitfields: function is interrupt handler, name present in
24927 proc table, function calls alloca, on condition directives
24928 (controls stack walks, 3 bits), saves condition reg, saves
24929 link reg. */
24930 /* The `function calls alloca' bit seems to be set whenever reg 31 is
24931 set up as a frame pointer, even when there is no alloca call. */
24932 fprintf (file, "%d,",
24933 ((optional_tbtab << 6)
24934 | ((optional_tbtab & frame_pointer_needed) << 5)
24935 | (info->cr_save_p << 1)
24936 | (info->lr_save_p)));
24938 /* 3 bitfields: saves backchain, fixup code, number of fpr saved
24939 (6 bits). */
24940 fprintf (file, "%d,",
24941 (info->push_p << 7) | (64 - info->first_fp_reg_save));
24943 /* 2 bitfields: spare bits (2 bits), number of gpr saved (6 bits). */
24944 fprintf (file, "%d,", (32 - first_reg_to_save ()));
24946 if (optional_tbtab)
24948 /* Compute the parameter info from the function decl argument
24949 list. */
24950 tree decl;
24951 int next_parm_info_bit = 31;
24953 for (decl = DECL_ARGUMENTS (current_function_decl);
24954 decl; decl = DECL_CHAIN (decl))
24956 rtx parameter = DECL_INCOMING_RTL (decl);
24957 enum machine_mode mode = GET_MODE (parameter);
24959 if (GET_CODE (parameter) == REG)
24961 if (SCALAR_FLOAT_MODE_P (mode))
24963 int bits;
24965 float_parms++;
24967 switch (mode)
24969 case SFmode:
24970 case SDmode:
24971 bits = 0x2;
24972 break;
24974 case DFmode:
24975 case DDmode:
24976 case TFmode:
24977 case TDmode:
24978 bits = 0x3;
24979 break;
24981 default:
24982 gcc_unreachable ();
24985 /* If only one bit will fit, don't or in this entry. */
24986 if (next_parm_info_bit > 0)
24987 parm_info |= (bits << (next_parm_info_bit - 1));
24988 next_parm_info_bit -= 2;
24990 else
24992 fixed_parms += ((GET_MODE_SIZE (mode)
24993 + (UNITS_PER_WORD - 1))
24994 / UNITS_PER_WORD);
24995 next_parm_info_bit -= 1;
25001 /* Number of fixed point parameters. */
25002 /* This is actually the number of words of fixed point parameters; thus
25003 an 8 byte struct counts as 2; and thus the maximum value is 8. */
25004 fprintf (file, "%d,", fixed_parms);
25006 /* 2 bitfields: number of floating point parameters (7 bits), parameters
25007 all on stack. */
25008 /* This is actually the number of fp registers that hold parameters;
25009 and thus the maximum value is 13. */
25010 /* Set parameters on stack bit if parameters are not in their original
25011 registers, regardless of whether they are on the stack? Xlc
25012 seems to set the bit when not optimizing. */
25013 fprintf (file, "%d\n", ((float_parms << 1) | (! optimize)));
25015 if (! optional_tbtab)
25016 return;
25018 /* Optional fields follow. Some are variable length. */
25020 /* Parameter types, left adjusted bit fields: 0 fixed, 10 single float,
25021 11 double float. */
25022 /* There is an entry for each parameter in a register, in the order that
25023 they occur in the parameter list. Any intervening arguments on the
25024 stack are ignored. If the list overflows a long (max possible length
25025 34 bits) then completely leave off all elements that don't fit. */
25026 /* Only emit this long if there was at least one parameter. */
25027 if (fixed_parms || float_parms)
25028 fprintf (file, "\t.long %d\n", parm_info);
25030 /* Offset from start of code to tb table. */
25031 fputs ("\t.long ", file);
25032 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
25033 RS6000_OUTPUT_BASENAME (file, fname);
25034 putc ('-', file);
25035 rs6000_output_function_entry (file, fname);
25036 putc ('\n', file);
25038 /* Interrupt handler mask. */
25039 /* Omit this long, since we never set the interrupt handler bit
25040 above. */
25042 /* Number of CTL (controlled storage) anchors. */
25043 /* Omit this long, since the has_ctl bit is never set above. */
25045 /* Displacement into stack of each CTL anchor. */
25046 /* Omit this list of longs, because there are no CTL anchors. */
25048 /* Length of function name. */
25049 if (*fname == '*')
25050 ++fname;
25051 fprintf (file, "\t.short %d\n", (int) strlen (fname));
25053 /* Function name. */
25054 assemble_string (fname, strlen (fname));
25056 /* Register for alloca automatic storage; this is always reg 31.
25057 Only emit this if the alloca bit was set above. */
25058 if (frame_pointer_needed)
25059 fputs ("\t.byte 31\n", file);
25061 fputs ("\t.align 2\n", file);
25065 /* A C compound statement that outputs the assembler code for a thunk
25066 function, used to implement C++ virtual function calls with
25067 multiple inheritance. The thunk acts as a wrapper around a virtual
25068 function, adjusting the implicit object parameter before handing
25069 control off to the real function.
25071 First, emit code to add the integer DELTA to the location that
25072 contains the incoming first argument. Assume that this argument
25073 contains a pointer, and is the one used to pass the `this' pointer
25074 in C++. This is the incoming argument *before* the function
25075 prologue, e.g. `%o0' on a sparc. The addition must preserve the
25076 values of all other incoming arguments.
25078 After the addition, emit code to jump to FUNCTION, which is a
25079 `FUNCTION_DECL'. This is a direct pure jump, not a call, and does
25080 not touch the return address. Hence returning from FUNCTION will
25081 return to whoever called the current `thunk'.
25083 The effect must be as if FUNCTION had been called directly with the
25084 adjusted first argument. This macro is responsible for emitting
25085 all of the code for a thunk function; output_function_prologue()
25086 and output_function_epilogue() are not invoked.
25088 The THUNK_FNDECL is redundant. (DELTA and FUNCTION have already
25089 been extracted from it.) It might possibly be useful on some
25090 targets, but probably not.
25092 If you do not define this macro, the target-independent code in the
25093 C++ frontend will generate a less efficient heavyweight thunk that
25094 calls FUNCTION instead of jumping to it. The generic approach does
25095 not support varargs. */
25097 static void
25098 rs6000_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
25099 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
25100 tree function)
25102 rtx this_rtx, insn, funexp;
25104 reload_completed = 1;
25105 epilogue_completed = 1;
25107 /* Mark the end of the (empty) prologue. */
25108 emit_note (NOTE_INSN_PROLOGUE_END);
25110 /* Find the "this" pointer. If the function returns a structure,
25111 the structure return pointer is in r3. */
25112 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
25113 this_rtx = gen_rtx_REG (Pmode, 4);
25114 else
25115 this_rtx = gen_rtx_REG (Pmode, 3);
25117 /* Apply the constant offset, if required. */
25118 if (delta)
25119 emit_insn (gen_add3_insn (this_rtx, this_rtx, GEN_INT (delta)));
25121 /* Apply the offset from the vtable, if required. */
25122 if (vcall_offset)
25124 rtx vcall_offset_rtx = GEN_INT (vcall_offset);
25125 rtx tmp = gen_rtx_REG (Pmode, 12);
25127 emit_move_insn (tmp, gen_rtx_MEM (Pmode, this_rtx));
25128 if (((unsigned HOST_WIDE_INT) vcall_offset) + 0x8000 >= 0x10000)
25130 emit_insn (gen_add3_insn (tmp, tmp, vcall_offset_rtx));
25131 emit_move_insn (tmp, gen_rtx_MEM (Pmode, tmp));
25133 else
25135 rtx loc = gen_rtx_PLUS (Pmode, tmp, vcall_offset_rtx);
25137 emit_move_insn (tmp, gen_rtx_MEM (Pmode, loc));
25139 emit_insn (gen_add3_insn (this_rtx, this_rtx, tmp));
25142 /* Generate a tail call to the target function. */
25143 if (!TREE_USED (function))
25145 assemble_external (function);
25146 TREE_USED (function) = 1;
25148 funexp = XEXP (DECL_RTL (function), 0);
25149 funexp = gen_rtx_MEM (FUNCTION_MODE, funexp);
25151 #if TARGET_MACHO
25152 if (MACHOPIC_INDIRECT)
25153 funexp = machopic_indirect_call_target (funexp);
25154 #endif
25156 /* gen_sibcall expects reload to convert scratch pseudo to LR so we must
25157 generate sibcall RTL explicitly. */
25158 insn = emit_call_insn (
25159 gen_rtx_PARALLEL (VOIDmode,
25160 gen_rtvec (4,
25161 gen_rtx_CALL (VOIDmode,
25162 funexp, const0_rtx),
25163 gen_rtx_USE (VOIDmode, const0_rtx),
25164 gen_rtx_USE (VOIDmode,
25165 gen_rtx_REG (SImode,
25166 LR_REGNO)),
25167 simple_return_rtx)));
25168 SIBLING_CALL_P (insn) = 1;
25169 emit_barrier ();
25171 /* Ensure we have a global entry point for the thunk. ??? We could
25172 avoid that if the target routine doesn't need a global entry point,
25173 but we do not know whether this is the case at this point. */
25174 if (DEFAULT_ABI == ABI_ELFv2)
25175 cfun->machine->r2_setup_needed = true;
25177 /* Run just enough of rest_of_compilation to get the insns emitted.
25178 There's not really enough bulk here to make other passes such as
25179 instruction scheduling worth while. Note that use_thunk calls
25180 assemble_start_function and assemble_end_function. */
25181 insn = get_insns ();
25182 shorten_branches (insn);
25183 final_start_function (insn, file, 1);
25184 final (insn, file, 1);
25185 final_end_function ();
25187 reload_completed = 0;
25188 epilogue_completed = 0;
25191 /* A quick summary of the various types of 'constant-pool tables'
25192 under PowerPC:
25194 Target Flags Name One table per
25195 AIX (none) AIX TOC object file
25196 AIX -mfull-toc AIX TOC object file
25197 AIX -mminimal-toc AIX minimal TOC translation unit
25198 SVR4/EABI (none) SVR4 SDATA object file
25199 SVR4/EABI -fpic SVR4 pic object file
25200 SVR4/EABI -fPIC SVR4 PIC translation unit
25201 SVR4/EABI -mrelocatable EABI TOC function
25202 SVR4/EABI -maix AIX TOC object file
25203 SVR4/EABI -maix -mminimal-toc
25204 AIX minimal TOC translation unit
25206 Name Reg. Set by entries contains:
25207 made by addrs? fp? sum?
25209 AIX TOC 2 crt0 as Y option option
25210 AIX minimal TOC 30 prolog gcc Y Y option
25211 SVR4 SDATA 13 crt0 gcc N Y N
25212 SVR4 pic 30 prolog ld Y not yet N
25213 SVR4 PIC 30 prolog gcc Y option option
25214 EABI TOC 30 prolog gcc Y option option
25218 /* Hash functions for the hash table. */
25220 static unsigned
25221 rs6000_hash_constant (rtx k)
25223 enum rtx_code code = GET_CODE (k);
25224 enum machine_mode mode = GET_MODE (k);
25225 unsigned result = (code << 3) ^ mode;
25226 const char *format;
25227 int flen, fidx;
25229 format = GET_RTX_FORMAT (code);
25230 flen = strlen (format);
25231 fidx = 0;
25233 switch (code)
25235 case LABEL_REF:
25236 return result * 1231 + (unsigned) INSN_UID (XEXP (k, 0));
25238 case CONST_WIDE_INT:
25240 int i;
25241 flen = CONST_WIDE_INT_NUNITS (k);
25242 for (i = 0; i < flen; i++)
25243 result = result * 613 + CONST_WIDE_INT_ELT (k, i);
25244 return result;
25247 case CONST_DOUBLE:
25248 if (mode != VOIDmode)
25249 return real_hash (CONST_DOUBLE_REAL_VALUE (k)) * result;
25250 flen = 2;
25251 break;
25253 case CODE_LABEL:
25254 fidx = 3;
25255 break;
25257 default:
25258 break;
25261 for (; fidx < flen; fidx++)
25262 switch (format[fidx])
25264 case 's':
25266 unsigned i, len;
25267 const char *str = XSTR (k, fidx);
25268 len = strlen (str);
25269 result = result * 613 + len;
25270 for (i = 0; i < len; i++)
25271 result = result * 613 + (unsigned) str[i];
25272 break;
25274 case 'u':
25275 case 'e':
25276 result = result * 1231 + rs6000_hash_constant (XEXP (k, fidx));
25277 break;
25278 case 'i':
25279 case 'n':
25280 result = result * 613 + (unsigned) XINT (k, fidx);
25281 break;
25282 case 'w':
25283 if (sizeof (unsigned) >= sizeof (HOST_WIDE_INT))
25284 result = result * 613 + (unsigned) XWINT (k, fidx);
25285 else
25287 size_t i;
25288 for (i = 0; i < sizeof (HOST_WIDE_INT) / sizeof (unsigned); i++)
25289 result = result * 613 + (unsigned) (XWINT (k, fidx)
25290 >> CHAR_BIT * i);
25292 break;
25293 case '0':
25294 break;
25295 default:
25296 gcc_unreachable ();
25299 return result;
25302 static unsigned
25303 toc_hash_function (const void *hash_entry)
25305 const struct toc_hash_struct *thc =
25306 (const struct toc_hash_struct *) hash_entry;
25307 return rs6000_hash_constant (thc->key) ^ thc->key_mode;
25310 /* Compare H1 and H2 for equivalence. */
25312 static int
25313 toc_hash_eq (const void *h1, const void *h2)
25315 rtx r1 = ((const struct toc_hash_struct *) h1)->key;
25316 rtx r2 = ((const struct toc_hash_struct *) h2)->key;
25318 if (((const struct toc_hash_struct *) h1)->key_mode
25319 != ((const struct toc_hash_struct *) h2)->key_mode)
25320 return 0;
25322 return rtx_equal_p (r1, r2);
25325 /* These are the names given by the C++ front-end to vtables, and
25326 vtable-like objects. Ideally, this logic should not be here;
25327 instead, there should be some programmatic way of inquiring as
25328 to whether or not an object is a vtable. */
25330 #define VTABLE_NAME_P(NAME) \
25331 (strncmp ("_vt.", name, strlen ("_vt.")) == 0 \
25332 || strncmp ("_ZTV", name, strlen ("_ZTV")) == 0 \
25333 || strncmp ("_ZTT", name, strlen ("_ZTT")) == 0 \
25334 || strncmp ("_ZTI", name, strlen ("_ZTI")) == 0 \
25335 || strncmp ("_ZTC", name, strlen ("_ZTC")) == 0)
25337 #ifdef NO_DOLLAR_IN_LABEL
25338 /* Return a GGC-allocated character string translating dollar signs in
25339 input NAME to underscores. Used by XCOFF ASM_OUTPUT_LABELREF. */
25341 const char *
25342 rs6000_xcoff_strip_dollar (const char *name)
25344 char *strip, *p;
25345 const char *q;
25346 size_t len;
25348 q = (const char *) strchr (name, '$');
25350 if (q == 0 || q == name)
25351 return name;
25353 len = strlen (name);
25354 strip = XALLOCAVEC (char, len + 1);
25355 strcpy (strip, name);
25356 p = strip + (q - name);
25357 while (p)
25359 *p = '_';
25360 p = strchr (p + 1, '$');
25363 return ggc_alloc_string (strip, len);
25365 #endif
25367 void
25368 rs6000_output_symbol_ref (FILE *file, rtx x)
25370 /* Currently C++ toc references to vtables can be emitted before it
25371 is decided whether the vtable is public or private. If this is
25372 the case, then the linker will eventually complain that there is
25373 a reference to an unknown section. Thus, for vtables only,
25374 we emit the TOC reference to reference the symbol and not the
25375 section. */
25376 const char *name = XSTR (x, 0);
25378 if (VTABLE_NAME_P (name))
25380 RS6000_OUTPUT_BASENAME (file, name);
25382 else
25383 assemble_name (file, name);
25386 /* Output a TOC entry. We derive the entry name from what is being
25387 written. */
25389 void
25390 output_toc (FILE *file, rtx x, int labelno, enum machine_mode mode)
25392 char buf[256];
25393 const char *name = buf;
25394 rtx base = x;
25395 HOST_WIDE_INT offset = 0;
25397 gcc_assert (!TARGET_NO_TOC);
25399 /* When the linker won't eliminate them, don't output duplicate
25400 TOC entries (this happens on AIX if there is any kind of TOC,
25401 and on SVR4 under -fPIC or -mrelocatable). Don't do this for
25402 CODE_LABELs. */
25403 if (TARGET_TOC && GET_CODE (x) != LABEL_REF)
25405 struct toc_hash_struct *h;
25406 void * * found;
25408 /* Create toc_hash_table. This can't be done at TARGET_OPTION_OVERRIDE
25409 time because GGC is not initialized at that point. */
25410 if (toc_hash_table == NULL)
25411 toc_hash_table = htab_create_ggc (1021, toc_hash_function,
25412 toc_hash_eq, NULL);
25414 h = ggc_alloc_toc_hash_struct ();
25415 h->key = x;
25416 h->key_mode = mode;
25417 h->labelno = labelno;
25419 found = htab_find_slot (toc_hash_table, h, INSERT);
25420 if (*found == NULL)
25421 *found = h;
25422 else /* This is indeed a duplicate.
25423 Set this label equal to that label. */
25425 fputs ("\t.set ", file);
25426 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
25427 fprintf (file, "%d,", labelno);
25428 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
25429 fprintf (file, "%d\n", ((*(const struct toc_hash_struct **)
25430 found)->labelno));
25432 #ifdef HAVE_AS_TLS
25433 if (TARGET_XCOFF && GET_CODE (x) == SYMBOL_REF
25434 && (SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_GLOBAL_DYNAMIC
25435 || SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC))
25437 fputs ("\t.set ", file);
25438 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LCM");
25439 fprintf (file, "%d,", labelno);
25440 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LCM");
25441 fprintf (file, "%d\n", ((*(const struct toc_hash_struct **)
25442 found)->labelno));
25444 #endif
25445 return;
25449 /* If we're going to put a double constant in the TOC, make sure it's
25450 aligned properly when strict alignment is on. */
25451 if ((CONST_DOUBLE_P (x) || CONST_WIDE_INT_P (x))
25452 && STRICT_ALIGNMENT
25453 && GET_MODE_BITSIZE (mode) >= 64
25454 && ! (TARGET_NO_FP_IN_TOC && ! TARGET_MINIMAL_TOC)) {
25455 ASM_OUTPUT_ALIGN (file, 3);
25458 (*targetm.asm_out.internal_label) (file, "LC", labelno);
25460 /* Handle FP constants specially. Note that if we have a minimal
25461 TOC, things we put here aren't actually in the TOC, so we can allow
25462 FP constants. */
25463 if (GET_CODE (x) == CONST_DOUBLE &&
25464 (GET_MODE (x) == TFmode || GET_MODE (x) == TDmode))
25466 REAL_VALUE_TYPE rv;
25467 long k[4];
25469 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
25470 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
25471 REAL_VALUE_TO_TARGET_DECIMAL128 (rv, k);
25472 else
25473 REAL_VALUE_TO_TARGET_LONG_DOUBLE (rv, k);
25475 if (TARGET_64BIT)
25477 if (TARGET_ELF || TARGET_MINIMAL_TOC)
25478 fputs (DOUBLE_INT_ASM_OP, file);
25479 else
25480 fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
25481 k[0] & 0xffffffff, k[1] & 0xffffffff,
25482 k[2] & 0xffffffff, k[3] & 0xffffffff);
25483 fprintf (file, "0x%lx%08lx,0x%lx%08lx\n",
25484 k[WORDS_BIG_ENDIAN ? 0 : 1] & 0xffffffff,
25485 k[WORDS_BIG_ENDIAN ? 1 : 0] & 0xffffffff,
25486 k[WORDS_BIG_ENDIAN ? 2 : 3] & 0xffffffff,
25487 k[WORDS_BIG_ENDIAN ? 3 : 2] & 0xffffffff);
25488 return;
25490 else
25492 if (TARGET_ELF || TARGET_MINIMAL_TOC)
25493 fputs ("\t.long ", file);
25494 else
25495 fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
25496 k[0] & 0xffffffff, k[1] & 0xffffffff,
25497 k[2] & 0xffffffff, k[3] & 0xffffffff);
25498 fprintf (file, "0x%lx,0x%lx,0x%lx,0x%lx\n",
25499 k[0] & 0xffffffff, k[1] & 0xffffffff,
25500 k[2] & 0xffffffff, k[3] & 0xffffffff);
25501 return;
25504 else if (GET_CODE (x) == CONST_DOUBLE &&
25505 (GET_MODE (x) == DFmode || GET_MODE (x) == DDmode))
25507 REAL_VALUE_TYPE rv;
25508 long k[2];
25510 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
25512 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
25513 REAL_VALUE_TO_TARGET_DECIMAL64 (rv, k);
25514 else
25515 REAL_VALUE_TO_TARGET_DOUBLE (rv, k);
25517 if (TARGET_64BIT)
25519 if (TARGET_ELF || TARGET_MINIMAL_TOC)
25520 fputs (DOUBLE_INT_ASM_OP, file);
25521 else
25522 fprintf (file, "\t.tc FD_%lx_%lx[TC],",
25523 k[0] & 0xffffffff, k[1] & 0xffffffff);
25524 fprintf (file, "0x%lx%08lx\n",
25525 k[WORDS_BIG_ENDIAN ? 0 : 1] & 0xffffffff,
25526 k[WORDS_BIG_ENDIAN ? 1 : 0] & 0xffffffff);
25527 return;
25529 else
25531 if (TARGET_ELF || TARGET_MINIMAL_TOC)
25532 fputs ("\t.long ", file);
25533 else
25534 fprintf (file, "\t.tc FD_%lx_%lx[TC],",
25535 k[0] & 0xffffffff, k[1] & 0xffffffff);
25536 fprintf (file, "0x%lx,0x%lx\n",
25537 k[0] & 0xffffffff, k[1] & 0xffffffff);
25538 return;
25541 else if (GET_CODE (x) == CONST_DOUBLE &&
25542 (GET_MODE (x) == SFmode || GET_MODE (x) == SDmode))
25544 REAL_VALUE_TYPE rv;
25545 long l;
25547 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
25548 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
25549 REAL_VALUE_TO_TARGET_DECIMAL32 (rv, l);
25550 else
25551 REAL_VALUE_TO_TARGET_SINGLE (rv, l);
25553 if (TARGET_64BIT)
25555 if (TARGET_ELF || TARGET_MINIMAL_TOC)
25556 fputs (DOUBLE_INT_ASM_OP, file);
25557 else
25558 fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
25559 if (WORDS_BIG_ENDIAN)
25560 fprintf (file, "0x%lx00000000\n", l & 0xffffffff);
25561 else
25562 fprintf (file, "0x%lx\n", l & 0xffffffff);
25563 return;
25565 else
25567 if (TARGET_ELF || TARGET_MINIMAL_TOC)
25568 fputs ("\t.long ", file);
25569 else
25570 fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
25571 fprintf (file, "0x%lx\n", l & 0xffffffff);
25572 return;
25575 else if (GET_MODE (x) == VOIDmode && GET_CODE (x) == CONST_INT)
25577 unsigned HOST_WIDE_INT low;
25578 HOST_WIDE_INT high;
25580 low = INTVAL (x) & 0xffffffff;
25581 high = (HOST_WIDE_INT) INTVAL (x) >> 32;
25583 /* TOC entries are always Pmode-sized, so when big-endian
25584 smaller integer constants in the TOC need to be padded.
25585 (This is still a win over putting the constants in
25586 a separate constant pool, because then we'd have
25587 to have both a TOC entry _and_ the actual constant.)
25589 For a 32-bit target, CONST_INT values are loaded and shifted
25590 entirely within `low' and can be stored in one TOC entry. */
25592 /* It would be easy to make this work, but it doesn't now. */
25593 gcc_assert (!TARGET_64BIT || POINTER_SIZE >= GET_MODE_BITSIZE (mode));
25595 if (WORDS_BIG_ENDIAN && POINTER_SIZE > GET_MODE_BITSIZE (mode))
25597 low |= high << 32;
25598 low <<= POINTER_SIZE - GET_MODE_BITSIZE (mode);
25599 high = (HOST_WIDE_INT) low >> 32;
25600 low &= 0xffffffff;
25603 if (TARGET_64BIT)
25605 if (TARGET_ELF || TARGET_MINIMAL_TOC)
25606 fputs (DOUBLE_INT_ASM_OP, file);
25607 else
25608 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
25609 (long) high & 0xffffffff, (long) low & 0xffffffff);
25610 fprintf (file, "0x%lx%08lx\n",
25611 (long) high & 0xffffffff, (long) low & 0xffffffff);
25612 return;
25614 else
25616 if (POINTER_SIZE < GET_MODE_BITSIZE (mode))
25618 if (TARGET_ELF || TARGET_MINIMAL_TOC)
25619 fputs ("\t.long ", file);
25620 else
25621 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
25622 (long) high & 0xffffffff, (long) low & 0xffffffff);
25623 fprintf (file, "0x%lx,0x%lx\n",
25624 (long) high & 0xffffffff, (long) low & 0xffffffff);
25626 else
25628 if (TARGET_ELF || TARGET_MINIMAL_TOC)
25629 fputs ("\t.long ", file);
25630 else
25631 fprintf (file, "\t.tc IS_%lx[TC],", (long) low & 0xffffffff);
25632 fprintf (file, "0x%lx\n", (long) low & 0xffffffff);
25634 return;
25638 if (GET_CODE (x) == CONST)
25640 gcc_assert (GET_CODE (XEXP (x, 0)) == PLUS
25641 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT);
25643 base = XEXP (XEXP (x, 0), 0);
25644 offset = INTVAL (XEXP (XEXP (x, 0), 1));
25647 switch (GET_CODE (base))
25649 case SYMBOL_REF:
25650 name = XSTR (base, 0);
25651 break;
25653 case LABEL_REF:
25654 ASM_GENERATE_INTERNAL_LABEL (buf, "L",
25655 CODE_LABEL_NUMBER (XEXP (base, 0)));
25656 break;
25658 case CODE_LABEL:
25659 ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (base));
25660 break;
25662 default:
25663 gcc_unreachable ();
25666 if (TARGET_ELF || TARGET_MINIMAL_TOC)
25667 fputs (TARGET_32BIT ? "\t.long " : DOUBLE_INT_ASM_OP, file);
25668 else
25670 fputs ("\t.tc ", file);
25671 RS6000_OUTPUT_BASENAME (file, name);
25673 if (offset < 0)
25674 fprintf (file, ".N" HOST_WIDE_INT_PRINT_UNSIGNED, - offset);
25675 else if (offset)
25676 fprintf (file, ".P" HOST_WIDE_INT_PRINT_UNSIGNED, offset);
25678 /* Mark large TOC symbols on AIX with [TE] so they are mapped
25679 after other TOC symbols, reducing overflow of small TOC access
25680 to [TC] symbols. */
25681 fputs (TARGET_XCOFF && TARGET_CMODEL != CMODEL_SMALL
25682 ? "[TE]," : "[TC],", file);
25685 /* Currently C++ toc references to vtables can be emitted before it
25686 is decided whether the vtable is public or private. If this is
25687 the case, then the linker will eventually complain that there is
25688 a TOC reference to an unknown section. Thus, for vtables only,
25689 we emit the TOC reference to reference the symbol and not the
25690 section. */
25691 if (VTABLE_NAME_P (name))
25693 RS6000_OUTPUT_BASENAME (file, name);
25694 if (offset < 0)
25695 fprintf (file, HOST_WIDE_INT_PRINT_DEC, offset);
25696 else if (offset > 0)
25697 fprintf (file, "+" HOST_WIDE_INT_PRINT_DEC, offset);
25699 else
25700 output_addr_const (file, x);
25702 #if HAVE_AS_TLS
25703 if (TARGET_XCOFF && GET_CODE (base) == SYMBOL_REF
25704 && SYMBOL_REF_TLS_MODEL (base) != 0)
25706 if (SYMBOL_REF_TLS_MODEL (base) == TLS_MODEL_LOCAL_EXEC)
25707 fputs ("@le", file);
25708 else if (SYMBOL_REF_TLS_MODEL (base) == TLS_MODEL_INITIAL_EXEC)
25709 fputs ("@ie", file);
25710 /* Use global-dynamic for local-dynamic. */
25711 else if (SYMBOL_REF_TLS_MODEL (base) == TLS_MODEL_GLOBAL_DYNAMIC
25712 || SYMBOL_REF_TLS_MODEL (base) == TLS_MODEL_LOCAL_DYNAMIC)
25714 putc ('\n', file);
25715 (*targetm.asm_out.internal_label) (file, "LCM", labelno);
25716 fputs ("\t.tc .", file);
25717 RS6000_OUTPUT_BASENAME (file, name);
25718 fputs ("[TC],", file);
25719 output_addr_const (file, x);
25720 fputs ("@m", file);
25723 #endif
25725 putc ('\n', file);
25728 /* Output an assembler pseudo-op to write an ASCII string of N characters
25729 starting at P to FILE.
25731 On the RS/6000, we have to do this using the .byte operation and
25732 write out special characters outside the quoted string.
25733 Also, the assembler is broken; very long strings are truncated,
25734 so we must artificially break them up early. */
25736 void
25737 output_ascii (FILE *file, const char *p, int n)
25739 char c;
25740 int i, count_string;
25741 const char *for_string = "\t.byte \"";
25742 const char *for_decimal = "\t.byte ";
25743 const char *to_close = NULL;
25745 count_string = 0;
25746 for (i = 0; i < n; i++)
25748 c = *p++;
25749 if (c >= ' ' && c < 0177)
25751 if (for_string)
25752 fputs (for_string, file);
25753 putc (c, file);
25755 /* Write two quotes to get one. */
25756 if (c == '"')
25758 putc (c, file);
25759 ++count_string;
25762 for_string = NULL;
25763 for_decimal = "\"\n\t.byte ";
25764 to_close = "\"\n";
25765 ++count_string;
25767 if (count_string >= 512)
25769 fputs (to_close, file);
25771 for_string = "\t.byte \"";
25772 for_decimal = "\t.byte ";
25773 to_close = NULL;
25774 count_string = 0;
25777 else
25779 if (for_decimal)
25780 fputs (for_decimal, file);
25781 fprintf (file, "%d", c);
25783 for_string = "\n\t.byte \"";
25784 for_decimal = ", ";
25785 to_close = "\n";
25786 count_string = 0;
25790 /* Now close the string if we have written one. Then end the line. */
25791 if (to_close)
25792 fputs (to_close, file);
25795 /* Generate a unique section name for FILENAME for a section type
25796 represented by SECTION_DESC. Output goes into BUF.
25798 SECTION_DESC can be any string, as long as it is different for each
25799 possible section type.
25801 We name the section in the same manner as xlc. The name begins with an
25802 underscore followed by the filename (after stripping any leading directory
25803 names) with the last period replaced by the string SECTION_DESC. If
25804 FILENAME does not contain a period, SECTION_DESC is appended to the end of
25805 the name. */
25807 void
25808 rs6000_gen_section_name (char **buf, const char *filename,
25809 const char *section_desc)
25811 const char *q, *after_last_slash, *last_period = 0;
25812 char *p;
25813 int len;
25815 after_last_slash = filename;
25816 for (q = filename; *q; q++)
25818 if (*q == '/')
25819 after_last_slash = q + 1;
25820 else if (*q == '.')
25821 last_period = q;
25824 len = strlen (after_last_slash) + strlen (section_desc) + 2;
25825 *buf = (char *) xmalloc (len);
25827 p = *buf;
25828 *p++ = '_';
25830 for (q = after_last_slash; *q; q++)
25832 if (q == last_period)
25834 strcpy (p, section_desc);
25835 p += strlen (section_desc);
25836 break;
25839 else if (ISALNUM (*q))
25840 *p++ = *q;
25843 if (last_period == 0)
25844 strcpy (p, section_desc);
25845 else
25846 *p = '\0';
25849 /* Emit profile function. */
25851 void
25852 output_profile_hook (int labelno ATTRIBUTE_UNUSED)
25854 /* Non-standard profiling for kernels, which just saves LR then calls
25855 _mcount without worrying about arg saves. The idea is to change
25856 the function prologue as little as possible as it isn't easy to
25857 account for arg save/restore code added just for _mcount. */
25858 if (TARGET_PROFILE_KERNEL)
25859 return;
25861 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
25863 #ifndef NO_PROFILE_COUNTERS
25864 # define NO_PROFILE_COUNTERS 0
25865 #endif
25866 if (NO_PROFILE_COUNTERS)
25867 emit_library_call (init_one_libfunc (RS6000_MCOUNT),
25868 LCT_NORMAL, VOIDmode, 0);
25869 else
25871 char buf[30];
25872 const char *label_name;
25873 rtx fun;
25875 ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
25876 label_name = ggc_strdup ((*targetm.strip_name_encoding) (buf));
25877 fun = gen_rtx_SYMBOL_REF (Pmode, label_name);
25879 emit_library_call (init_one_libfunc (RS6000_MCOUNT),
25880 LCT_NORMAL, VOIDmode, 1, fun, Pmode);
25883 else if (DEFAULT_ABI == ABI_DARWIN)
25885 const char *mcount_name = RS6000_MCOUNT;
25886 int caller_addr_regno = LR_REGNO;
25888 /* Be conservative and always set this, at least for now. */
25889 crtl->uses_pic_offset_table = 1;
25891 #if TARGET_MACHO
25892 /* For PIC code, set up a stub and collect the caller's address
25893 from r0, which is where the prologue puts it. */
25894 if (MACHOPIC_INDIRECT
25895 && crtl->uses_pic_offset_table)
25896 caller_addr_regno = 0;
25897 #endif
25898 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, mcount_name),
25899 LCT_NORMAL, VOIDmode, 1,
25900 gen_rtx_REG (Pmode, caller_addr_regno), Pmode);
25904 /* Write function profiler code. */
25906 void
25907 output_function_profiler (FILE *file, int labelno)
25909 char buf[100];
25911 switch (DEFAULT_ABI)
25913 default:
25914 gcc_unreachable ();
25916 case ABI_V4:
25917 if (!TARGET_32BIT)
25919 warning (0, "no profiling of 64-bit code for this ABI");
25920 return;
25922 ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
25923 fprintf (file, "\tmflr %s\n", reg_names[0]);
25924 if (NO_PROFILE_COUNTERS)
25926 asm_fprintf (file, "\tstw %s,4(%s)\n",
25927 reg_names[0], reg_names[1]);
25929 else if (TARGET_SECURE_PLT && flag_pic)
25931 if (TARGET_LINK_STACK)
25933 char name[32];
25934 get_ppc476_thunk_name (name);
25935 asm_fprintf (file, "\tbl %s\n", name);
25937 else
25938 asm_fprintf (file, "\tbcl 20,31,1f\n1:\n");
25939 asm_fprintf (file, "\tstw %s,4(%s)\n",
25940 reg_names[0], reg_names[1]);
25941 asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
25942 asm_fprintf (file, "\taddis %s,%s,",
25943 reg_names[12], reg_names[12]);
25944 assemble_name (file, buf);
25945 asm_fprintf (file, "-1b@ha\n\tla %s,", reg_names[0]);
25946 assemble_name (file, buf);
25947 asm_fprintf (file, "-1b@l(%s)\n", reg_names[12]);
25949 else if (flag_pic == 1)
25951 fputs ("\tbl _GLOBAL_OFFSET_TABLE_@local-4\n", file);
25952 asm_fprintf (file, "\tstw %s,4(%s)\n",
25953 reg_names[0], reg_names[1]);
25954 asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
25955 asm_fprintf (file, "\tlwz %s,", reg_names[0]);
25956 assemble_name (file, buf);
25957 asm_fprintf (file, "@got(%s)\n", reg_names[12]);
25959 else if (flag_pic > 1)
25961 asm_fprintf (file, "\tstw %s,4(%s)\n",
25962 reg_names[0], reg_names[1]);
25963 /* Now, we need to get the address of the label. */
25964 if (TARGET_LINK_STACK)
25966 char name[32];
25967 get_ppc476_thunk_name (name);
25968 asm_fprintf (file, "\tbl %s\n\tb 1f\n\t.long ", name);
25969 assemble_name (file, buf);
25970 fputs ("-.\n1:", file);
25971 asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
25972 asm_fprintf (file, "\taddi %s,%s,4\n",
25973 reg_names[11], reg_names[11]);
25975 else
25977 fputs ("\tbcl 20,31,1f\n\t.long ", file);
25978 assemble_name (file, buf);
25979 fputs ("-.\n1:", file);
25980 asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
25982 asm_fprintf (file, "\tlwz %s,0(%s)\n",
25983 reg_names[0], reg_names[11]);
25984 asm_fprintf (file, "\tadd %s,%s,%s\n",
25985 reg_names[0], reg_names[0], reg_names[11]);
25987 else
25989 asm_fprintf (file, "\tlis %s,", reg_names[12]);
25990 assemble_name (file, buf);
25991 fputs ("@ha\n", file);
25992 asm_fprintf (file, "\tstw %s,4(%s)\n",
25993 reg_names[0], reg_names[1]);
25994 asm_fprintf (file, "\tla %s,", reg_names[0]);
25995 assemble_name (file, buf);
25996 asm_fprintf (file, "@l(%s)\n", reg_names[12]);
25999 /* ABI_V4 saves the static chain reg with ASM_OUTPUT_REG_PUSH. */
26000 fprintf (file, "\tbl %s%s\n",
26001 RS6000_MCOUNT, flag_pic ? "@plt" : "");
26002 break;
26004 case ABI_AIX:
26005 case ABI_ELFv2:
26006 case ABI_DARWIN:
26007 /* Don't do anything, done in output_profile_hook (). */
26008 break;
26014 /* The following variable value is the last issued insn. */
26016 static rtx last_scheduled_insn;
26018 /* The following variable helps to balance issuing of load and
26019 store instructions */
26021 static int load_store_pendulum;
26023 /* Power4 load update and store update instructions are cracked into a
26024 load or store and an integer insn which are executed in the same cycle.
26025 Branches have their own dispatch slot which does not count against the
26026 GCC issue rate, but it changes the program flow so there are no other
26027 instructions to issue in this cycle. */
26029 static int
26030 rs6000_variable_issue_1 (rtx insn, int more)
26032 last_scheduled_insn = insn;
26033 if (GET_CODE (PATTERN (insn)) == USE
26034 || GET_CODE (PATTERN (insn)) == CLOBBER)
26036 cached_can_issue_more = more;
26037 return cached_can_issue_more;
26040 if (insn_terminates_group_p (insn, current_group))
26042 cached_can_issue_more = 0;
26043 return cached_can_issue_more;
26046 /* If no reservation, but reach here */
26047 if (recog_memoized (insn) < 0)
26048 return more;
26050 if (rs6000_sched_groups)
26052 if (is_microcoded_insn (insn))
26053 cached_can_issue_more = 0;
26054 else if (is_cracked_insn (insn))
26055 cached_can_issue_more = more > 2 ? more - 2 : 0;
26056 else
26057 cached_can_issue_more = more - 1;
26059 return cached_can_issue_more;
26062 if (rs6000_cpu_attr == CPU_CELL && is_nonpipeline_insn (insn))
26063 return 0;
26065 cached_can_issue_more = more - 1;
26066 return cached_can_issue_more;
26069 static int
26070 rs6000_variable_issue (FILE *stream, int verbose, rtx insn, int more)
26072 int r = rs6000_variable_issue_1 (insn, more);
26073 if (verbose)
26074 fprintf (stream, "// rs6000_variable_issue (more = %d) = %d\n", more, r);
26075 return r;
26078 /* Adjust the cost of a scheduling dependency. Return the new cost of
26079 a dependency LINK or INSN on DEP_INSN. COST is the current cost. */
26081 static int
26082 rs6000_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
26084 enum attr_type attr_type;
26086 if (! recog_memoized (insn))
26087 return 0;
26089 switch (REG_NOTE_KIND (link))
26091 case REG_DEP_TRUE:
26093 /* Data dependency; DEP_INSN writes a register that INSN reads
26094 some cycles later. */
26096 /* Separate a load from a narrower, dependent store. */
26097 if (rs6000_sched_groups
26098 && GET_CODE (PATTERN (insn)) == SET
26099 && GET_CODE (PATTERN (dep_insn)) == SET
26100 && GET_CODE (XEXP (PATTERN (insn), 1)) == MEM
26101 && GET_CODE (XEXP (PATTERN (dep_insn), 0)) == MEM
26102 && (GET_MODE_SIZE (GET_MODE (XEXP (PATTERN (insn), 1)))
26103 > GET_MODE_SIZE (GET_MODE (XEXP (PATTERN (dep_insn), 0)))))
26104 return cost + 14;
26106 attr_type = get_attr_type (insn);
26108 switch (attr_type)
26110 case TYPE_JMPREG:
26111 /* Tell the first scheduling pass about the latency between
26112 a mtctr and bctr (and mtlr and br/blr). The first
26113 scheduling pass will not know about this latency since
26114 the mtctr instruction, which has the latency associated
26115 to it, will be generated by reload. */
26116 return 4;
26117 case TYPE_BRANCH:
26118 /* Leave some extra cycles between a compare and its
26119 dependent branch, to inhibit expensive mispredicts. */
26120 if ((rs6000_cpu_attr == CPU_PPC603
26121 || rs6000_cpu_attr == CPU_PPC604
26122 || rs6000_cpu_attr == CPU_PPC604E
26123 || rs6000_cpu_attr == CPU_PPC620
26124 || rs6000_cpu_attr == CPU_PPC630
26125 || rs6000_cpu_attr == CPU_PPC750
26126 || rs6000_cpu_attr == CPU_PPC7400
26127 || rs6000_cpu_attr == CPU_PPC7450
26128 || rs6000_cpu_attr == CPU_PPCE5500
26129 || rs6000_cpu_attr == CPU_PPCE6500
26130 || rs6000_cpu_attr == CPU_POWER4
26131 || rs6000_cpu_attr == CPU_POWER5
26132 || rs6000_cpu_attr == CPU_POWER7
26133 || rs6000_cpu_attr == CPU_POWER8
26134 || rs6000_cpu_attr == CPU_CELL)
26135 && recog_memoized (dep_insn)
26136 && (INSN_CODE (dep_insn) >= 0))
26138 switch (get_attr_type (dep_insn))
26140 case TYPE_CMP:
26141 case TYPE_COMPARE:
26142 case TYPE_DELAYED_COMPARE:
26143 case TYPE_IMUL_COMPARE:
26144 case TYPE_LMUL_COMPARE:
26145 case TYPE_FPCOMPARE:
26146 case TYPE_CR_LOGICAL:
26147 case TYPE_DELAYED_CR:
26148 return cost + 2;
26149 default:
26150 break;
26152 break;
26154 case TYPE_STORE:
26155 case TYPE_FPSTORE:
26156 if ((rs6000_cpu == PROCESSOR_POWER6)
26157 && recog_memoized (dep_insn)
26158 && (INSN_CODE (dep_insn) >= 0))
26161 if (GET_CODE (PATTERN (insn)) != SET)
26162 /* If this happens, we have to extend this to schedule
26163 optimally. Return default for now. */
26164 return cost;
26166 /* Adjust the cost for the case where the value written
26167 by a fixed point operation is used as the address
26168 gen value on a store. */
26169 switch (get_attr_type (dep_insn))
26171 case TYPE_LOAD:
26172 case TYPE_CNTLZ:
26174 if (! store_data_bypass_p (dep_insn, insn))
26175 return get_attr_sign_extend (dep_insn)
26176 == SIGN_EXTEND_YES ? 6 : 4;
26177 break;
26179 case TYPE_VAR_SHIFT_ROTATE:
26180 case TYPE_VAR_DELAYED_COMPARE:
26182 if (! store_data_bypass_p (dep_insn, insn))
26183 return 6;
26184 break;
26186 case TYPE_INTEGER:
26187 case TYPE_COMPARE:
26188 case TYPE_FAST_COMPARE:
26189 case TYPE_EXTS:
26190 case TYPE_SHIFT:
26191 case TYPE_INSERT_WORD:
26192 case TYPE_INSERT_DWORD:
26194 if (! store_data_bypass_p (dep_insn, insn))
26195 return 3;
26196 break;
26198 case TYPE_STORE:
26199 case TYPE_FPLOAD:
26200 case TYPE_FPSTORE:
26202 if (get_attr_update (dep_insn) == UPDATE_YES
26203 && ! store_data_bypass_p (dep_insn, insn))
26204 return 3;
26205 break;
26207 case TYPE_IMUL:
26208 case TYPE_IMUL2:
26209 case TYPE_IMUL3:
26210 case TYPE_LMUL:
26211 case TYPE_IMUL_COMPARE:
26212 case TYPE_LMUL_COMPARE:
26214 if (! store_data_bypass_p (dep_insn, insn))
26215 return 17;
26216 break;
26218 case TYPE_IDIV:
26220 if (! store_data_bypass_p (dep_insn, insn))
26221 return 45;
26222 break;
26224 case TYPE_LDIV:
26226 if (! store_data_bypass_p (dep_insn, insn))
26227 return 57;
26228 break;
26230 default:
26231 break;
26234 break;
26236 case TYPE_LOAD:
26237 if ((rs6000_cpu == PROCESSOR_POWER6)
26238 && recog_memoized (dep_insn)
26239 && (INSN_CODE (dep_insn) >= 0))
26242 /* Adjust the cost for the case where the value written
26243 by a fixed point instruction is used within the address
26244 gen portion of a subsequent load(u)(x) */
26245 switch (get_attr_type (dep_insn))
26247 case TYPE_LOAD:
26248 case TYPE_CNTLZ:
26250 if (set_to_load_agen (dep_insn, insn))
26251 return get_attr_sign_extend (dep_insn)
26252 == SIGN_EXTEND_YES ? 6 : 4;
26253 break;
26255 case TYPE_VAR_SHIFT_ROTATE:
26256 case TYPE_VAR_DELAYED_COMPARE:
26258 if (set_to_load_agen (dep_insn, insn))
26259 return 6;
26260 break;
26262 case TYPE_INTEGER:
26263 case TYPE_COMPARE:
26264 case TYPE_FAST_COMPARE:
26265 case TYPE_EXTS:
26266 case TYPE_SHIFT:
26267 case TYPE_INSERT_WORD:
26268 case TYPE_INSERT_DWORD:
26270 if (set_to_load_agen (dep_insn, insn))
26271 return 3;
26272 break;
26274 case TYPE_STORE:
26275 case TYPE_FPLOAD:
26276 case TYPE_FPSTORE:
26278 if (get_attr_update (dep_insn) == UPDATE_YES
26279 && set_to_load_agen (dep_insn, insn))
26280 return 3;
26281 break;
26283 case TYPE_IMUL:
26284 case TYPE_IMUL2:
26285 case TYPE_IMUL3:
26286 case TYPE_LMUL:
26287 case TYPE_IMUL_COMPARE:
26288 case TYPE_LMUL_COMPARE:
26290 if (set_to_load_agen (dep_insn, insn))
26291 return 17;
26292 break;
26294 case TYPE_IDIV:
26296 if (set_to_load_agen (dep_insn, insn))
26297 return 45;
26298 break;
26300 case TYPE_LDIV:
26302 if (set_to_load_agen (dep_insn, insn))
26303 return 57;
26304 break;
26306 default:
26307 break;
26310 break;
26312 case TYPE_FPLOAD:
26313 if ((rs6000_cpu == PROCESSOR_POWER6)
26314 && get_attr_update (insn) == UPDATE_NO
26315 && recog_memoized (dep_insn)
26316 && (INSN_CODE (dep_insn) >= 0)
26317 && (get_attr_type (dep_insn) == TYPE_MFFGPR))
26318 return 2;
26320 default:
26321 break;
26324 /* Fall out to return default cost. */
26326 break;
26328 case REG_DEP_OUTPUT:
26329 /* Output dependency; DEP_INSN writes a register that INSN writes some
26330 cycles later. */
26331 if ((rs6000_cpu == PROCESSOR_POWER6)
26332 && recog_memoized (dep_insn)
26333 && (INSN_CODE (dep_insn) >= 0))
26335 attr_type = get_attr_type (insn);
26337 switch (attr_type)
26339 case TYPE_FP:
26340 if (get_attr_type (dep_insn) == TYPE_FP)
26341 return 1;
26342 break;
26343 case TYPE_FPLOAD:
26344 if (get_attr_update (insn) == UPDATE_NO
26345 && get_attr_type (dep_insn) == TYPE_MFFGPR)
26346 return 2;
26347 break;
26348 default:
26349 break;
26352 case REG_DEP_ANTI:
26353 /* Anti dependency; DEP_INSN reads a register that INSN writes some
26354 cycles later. */
26355 return 0;
26357 default:
26358 gcc_unreachable ();
26361 return cost;
26364 /* Debug version of rs6000_adjust_cost. */
26366 static int
26367 rs6000_debug_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
26369 int ret = rs6000_adjust_cost (insn, link, dep_insn, cost);
26371 if (ret != cost)
26373 const char *dep;
26375 switch (REG_NOTE_KIND (link))
26377 default: dep = "unknown depencency"; break;
26378 case REG_DEP_TRUE: dep = "data dependency"; break;
26379 case REG_DEP_OUTPUT: dep = "output dependency"; break;
26380 case REG_DEP_ANTI: dep = "anti depencency"; break;
26383 fprintf (stderr,
26384 "\nrs6000_adjust_cost, final cost = %d, orig cost = %d, "
26385 "%s, insn:\n", ret, cost, dep);
26387 debug_rtx (insn);
26390 return ret;
26393 /* The function returns a true if INSN is microcoded.
26394 Return false otherwise. */
26396 static bool
26397 is_microcoded_insn (rtx insn)
26399 if (!insn || !NONDEBUG_INSN_P (insn)
26400 || GET_CODE (PATTERN (insn)) == USE
26401 || GET_CODE (PATTERN (insn)) == CLOBBER)
26402 return false;
26404 if (rs6000_cpu_attr == CPU_CELL)
26405 return get_attr_cell_micro (insn) == CELL_MICRO_ALWAYS;
26407 if (rs6000_sched_groups
26408 && (rs6000_cpu == PROCESSOR_POWER4 || rs6000_cpu == PROCESSOR_POWER5))
26410 enum attr_type type = get_attr_type (insn);
26411 if ((type == TYPE_LOAD
26412 && get_attr_update (insn) == UPDATE_YES
26413 && get_attr_sign_extend (insn) == SIGN_EXTEND_YES)
26414 || ((type == TYPE_LOAD || type == TYPE_STORE)
26415 && get_attr_update (insn) == UPDATE_YES
26416 && get_attr_indexed (insn) == INDEXED_YES)
26417 || type == TYPE_MFCR)
26418 return true;
26421 return false;
26424 /* The function returns true if INSN is cracked into 2 instructions
26425 by the processor (and therefore occupies 2 issue slots). */
26427 static bool
26428 is_cracked_insn (rtx insn)
26430 if (!insn || !NONDEBUG_INSN_P (insn)
26431 || GET_CODE (PATTERN (insn)) == USE
26432 || GET_CODE (PATTERN (insn)) == CLOBBER)
26433 return false;
26435 if (rs6000_sched_groups
26436 && (rs6000_cpu == PROCESSOR_POWER4 || rs6000_cpu == PROCESSOR_POWER5))
26438 enum attr_type type = get_attr_type (insn);
26439 if ((type == TYPE_LOAD
26440 && get_attr_sign_extend (insn) == SIGN_EXTEND_YES
26441 && get_attr_update (insn) == UPDATE_NO)
26442 || (type == TYPE_LOAD
26443 && get_attr_sign_extend (insn) == SIGN_EXTEND_NO
26444 && get_attr_update (insn) == UPDATE_YES
26445 && get_attr_indexed (insn) == INDEXED_NO)
26446 || (type == TYPE_STORE
26447 && get_attr_update (insn) == UPDATE_YES
26448 && get_attr_indexed (insn) == INDEXED_NO)
26449 || ((type == TYPE_FPLOAD || type == TYPE_FPSTORE)
26450 && get_attr_update (insn) == UPDATE_YES)
26451 || type == TYPE_DELAYED_CR
26452 || type == TYPE_COMPARE || type == TYPE_DELAYED_COMPARE
26453 || type == TYPE_IMUL_COMPARE || type == TYPE_LMUL_COMPARE
26454 || type == TYPE_IDIV || type == TYPE_LDIV
26455 || type == TYPE_INSERT_WORD)
26456 return true;
26459 return false;
26462 /* The function returns true if INSN can be issued only from
26463 the branch slot. */
26465 static bool
26466 is_branch_slot_insn (rtx insn)
26468 if (!insn || !NONDEBUG_INSN_P (insn)
26469 || GET_CODE (PATTERN (insn)) == USE
26470 || GET_CODE (PATTERN (insn)) == CLOBBER)
26471 return false;
26473 if (rs6000_sched_groups)
26475 enum attr_type type = get_attr_type (insn);
26476 if (type == TYPE_BRANCH || type == TYPE_JMPREG)
26477 return true;
26478 return false;
26481 return false;
26484 /* The function returns true if out_inst sets a value that is
26485 used in the address generation computation of in_insn */
26486 static bool
26487 set_to_load_agen (rtx out_insn, rtx in_insn)
26489 rtx out_set, in_set;
26491 /* For performance reasons, only handle the simple case where
26492 both loads are a single_set. */
26493 out_set = single_set (out_insn);
26494 if (out_set)
26496 in_set = single_set (in_insn);
26497 if (in_set)
26498 return reg_mentioned_p (SET_DEST (out_set), SET_SRC (in_set));
26501 return false;
26504 /* Try to determine base/offset/size parts of the given MEM.
26505 Return true if successful, false if all the values couldn't
26506 be determined.
26508 This function only looks for REG or REG+CONST address forms.
26509 REG+REG address form will return false. */
26511 static bool
26512 get_memref_parts (rtx mem, rtx *base, HOST_WIDE_INT *offset,
26513 HOST_WIDE_INT *size)
26515 rtx addr_rtx;
26516 if MEM_SIZE_KNOWN_P (mem)
26517 *size = MEM_SIZE (mem);
26518 else
26519 return false;
26521 if (GET_CODE (XEXP (mem, 0)) == PRE_MODIFY)
26522 addr_rtx = XEXP (XEXP (mem, 0), 1);
26523 else
26524 addr_rtx = (XEXP (mem, 0));
26526 if (GET_CODE (addr_rtx) == REG)
26528 *base = addr_rtx;
26529 *offset = 0;
26531 else if (GET_CODE (addr_rtx) == PLUS
26532 && CONST_INT_P (XEXP (addr_rtx, 1)))
26534 *base = XEXP (addr_rtx, 0);
26535 *offset = INTVAL (XEXP (addr_rtx, 1));
26537 else
26538 return false;
26540 return true;
26543 /* The function returns true if the target storage location of
26544 mem1 is adjacent to the target storage location of mem2 */
26545 /* Return 1 if memory locations are adjacent. */
26547 static bool
26548 adjacent_mem_locations (rtx mem1, rtx mem2)
26550 rtx reg1, reg2;
26551 HOST_WIDE_INT off1, size1, off2, size2;
26553 if (get_memref_parts (mem1, &reg1, &off1, &size1)
26554 && get_memref_parts (mem2, &reg2, &off2, &size2))
26555 return ((REGNO (reg1) == REGNO (reg2))
26556 && ((off1 + size1 == off2)
26557 || (off2 + size2 == off1)));
26559 return false;
26562 /* This function returns true if it can be determined that the two MEM
26563 locations overlap by at least 1 byte based on base reg/offset/size. */
26565 static bool
26566 mem_locations_overlap (rtx mem1, rtx mem2)
26568 rtx reg1, reg2;
26569 HOST_WIDE_INT off1, size1, off2, size2;
26571 if (get_memref_parts (mem1, &reg1, &off1, &size1)
26572 && get_memref_parts (mem2, &reg2, &off2, &size2))
26573 return ((REGNO (reg1) == REGNO (reg2))
26574 && (((off1 <= off2) && (off1 + size1 > off2))
26575 || ((off2 <= off1) && (off2 + size2 > off1))));
26577 return false;
26580 /* A C statement (sans semicolon) to update the integer scheduling
26581 priority INSN_PRIORITY (INSN). Increase the priority to execute the
26582 INSN earlier, reduce the priority to execute INSN later. Do not
26583 define this macro if you do not need to adjust the scheduling
26584 priorities of insns. */
26586 static int
26587 rs6000_adjust_priority (rtx insn ATTRIBUTE_UNUSED, int priority)
26589 rtx load_mem, str_mem;
26590 /* On machines (like the 750) which have asymmetric integer units,
26591 where one integer unit can do multiply and divides and the other
26592 can't, reduce the priority of multiply/divide so it is scheduled
26593 before other integer operations. */
26595 #if 0
26596 if (! INSN_P (insn))
26597 return priority;
26599 if (GET_CODE (PATTERN (insn)) == USE)
26600 return priority;
26602 switch (rs6000_cpu_attr) {
26603 case CPU_PPC750:
26604 switch (get_attr_type (insn))
26606 default:
26607 break;
26609 case TYPE_IMUL:
26610 case TYPE_IDIV:
26611 fprintf (stderr, "priority was %#x (%d) before adjustment\n",
26612 priority, priority);
26613 if (priority >= 0 && priority < 0x01000000)
26614 priority >>= 3;
26615 break;
26618 #endif
26620 if (insn_must_be_first_in_group (insn)
26621 && reload_completed
26622 && current_sched_info->sched_max_insns_priority
26623 && rs6000_sched_restricted_insns_priority)
26626 /* Prioritize insns that can be dispatched only in the first
26627 dispatch slot. */
26628 if (rs6000_sched_restricted_insns_priority == 1)
26629 /* Attach highest priority to insn. This means that in
26630 haifa-sched.c:ready_sort(), dispatch-slot restriction considerations
26631 precede 'priority' (critical path) considerations. */
26632 return current_sched_info->sched_max_insns_priority;
26633 else if (rs6000_sched_restricted_insns_priority == 2)
26634 /* Increase priority of insn by a minimal amount. This means that in
26635 haifa-sched.c:ready_sort(), only 'priority' (critical path)
26636 considerations precede dispatch-slot restriction considerations. */
26637 return (priority + 1);
26640 if (rs6000_cpu == PROCESSOR_POWER6
26641 && ((load_store_pendulum == -2 && is_load_insn (insn, &load_mem))
26642 || (load_store_pendulum == 2 && is_store_insn (insn, &str_mem))))
26643 /* Attach highest priority to insn if the scheduler has just issued two
26644 stores and this instruction is a load, or two loads and this instruction
26645 is a store. Power6 wants loads and stores scheduled alternately
26646 when possible */
26647 return current_sched_info->sched_max_insns_priority;
26649 return priority;
26652 /* Return true if the instruction is nonpipelined on the Cell. */
26653 static bool
26654 is_nonpipeline_insn (rtx insn)
26656 enum attr_type type;
26657 if (!insn || !NONDEBUG_INSN_P (insn)
26658 || GET_CODE (PATTERN (insn)) == USE
26659 || GET_CODE (PATTERN (insn)) == CLOBBER)
26660 return false;
26662 type = get_attr_type (insn);
26663 if (type == TYPE_IMUL
26664 || type == TYPE_IMUL2
26665 || type == TYPE_IMUL3
26666 || type == TYPE_LMUL
26667 || type == TYPE_IDIV
26668 || type == TYPE_LDIV
26669 || type == TYPE_SDIV
26670 || type == TYPE_DDIV
26671 || type == TYPE_SSQRT
26672 || type == TYPE_DSQRT
26673 || type == TYPE_MFCR
26674 || type == TYPE_MFCRF
26675 || type == TYPE_MFJMPR)
26677 return true;
26679 return false;
26683 /* Return how many instructions the machine can issue per cycle. */
26685 static int
26686 rs6000_issue_rate (void)
26688 /* Unless scheduling for register pressure, use issue rate of 1 for
26689 first scheduling pass to decrease degradation. */
26690 if (!reload_completed && !flag_sched_pressure)
26691 return 1;
26693 switch (rs6000_cpu_attr) {
26694 case CPU_RS64A:
26695 case CPU_PPC601: /* ? */
26696 case CPU_PPC7450:
26697 return 3;
26698 case CPU_PPC440:
26699 case CPU_PPC603:
26700 case CPU_PPC750:
26701 case CPU_PPC7400:
26702 case CPU_PPC8540:
26703 case CPU_PPC8548:
26704 case CPU_CELL:
26705 case CPU_PPCE300C2:
26706 case CPU_PPCE300C3:
26707 case CPU_PPCE500MC:
26708 case CPU_PPCE500MC64:
26709 case CPU_PPCE5500:
26710 case CPU_PPCE6500:
26711 case CPU_TITAN:
26712 return 2;
26713 case CPU_PPC476:
26714 case CPU_PPC604:
26715 case CPU_PPC604E:
26716 case CPU_PPC620:
26717 case CPU_PPC630:
26718 return 4;
26719 case CPU_POWER4:
26720 case CPU_POWER5:
26721 case CPU_POWER6:
26722 case CPU_POWER7:
26723 return 5;
26724 case CPU_POWER8:
26725 return 7;
26726 default:
26727 return 1;
26731 /* Return how many instructions to look ahead for better insn
26732 scheduling. */
26734 static int
26735 rs6000_use_sched_lookahead (void)
26737 switch (rs6000_cpu_attr)
26739 case CPU_PPC8540:
26740 case CPU_PPC8548:
26741 return 4;
26743 case CPU_CELL:
26744 return (reload_completed ? 8 : 0);
26746 default:
26747 return 0;
26751 /* We are choosing insn from the ready queue. Return nonzero if INSN can be chosen. */
26752 static int
26753 rs6000_use_sched_lookahead_guard (rtx insn)
26755 if (rs6000_cpu_attr != CPU_CELL)
26756 return 1;
26758 if (insn == NULL_RTX || !INSN_P (insn))
26759 abort ();
26761 if (!reload_completed
26762 || is_nonpipeline_insn (insn)
26763 || is_microcoded_insn (insn))
26764 return 0;
26766 return 1;
26769 /* Determine if PAT refers to memory. If so, set MEM_REF to the MEM rtx
26770 and return true. */
26772 static bool
26773 find_mem_ref (rtx pat, rtx *mem_ref)
26775 const char * fmt;
26776 int i, j;
26778 /* stack_tie does not produce any real memory traffic. */
26779 if (tie_operand (pat, VOIDmode))
26780 return false;
26782 if (GET_CODE (pat) == MEM)
26784 *mem_ref = pat;
26785 return true;
26788 /* Recursively process the pattern. */
26789 fmt = GET_RTX_FORMAT (GET_CODE (pat));
26791 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
26793 if (fmt[i] == 'e')
26795 if (find_mem_ref (XEXP (pat, i), mem_ref))
26796 return true;
26798 else if (fmt[i] == 'E')
26799 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
26801 if (find_mem_ref (XVECEXP (pat, i, j), mem_ref))
26802 return true;
26806 return false;
26809 /* Determine if PAT is a PATTERN of a load insn. */
26811 static bool
26812 is_load_insn1 (rtx pat, rtx *load_mem)
26814 if (!pat || pat == NULL_RTX)
26815 return false;
26817 if (GET_CODE (pat) == SET)
26818 return find_mem_ref (SET_SRC (pat), load_mem);
26820 if (GET_CODE (pat) == PARALLEL)
26822 int i;
26824 for (i = 0; i < XVECLEN (pat, 0); i++)
26825 if (is_load_insn1 (XVECEXP (pat, 0, i), load_mem))
26826 return true;
26829 return false;
26832 /* Determine if INSN loads from memory. */
26834 static bool
26835 is_load_insn (rtx insn, rtx *load_mem)
26837 if (!insn || !INSN_P (insn))
26838 return false;
26840 if (CALL_P (insn))
26841 return false;
26843 return is_load_insn1 (PATTERN (insn), load_mem);
26846 /* Determine if PAT is a PATTERN of a store insn. */
26848 static bool
26849 is_store_insn1 (rtx pat, rtx *str_mem)
26851 if (!pat || pat == NULL_RTX)
26852 return false;
26854 if (GET_CODE (pat) == SET)
26855 return find_mem_ref (SET_DEST (pat), str_mem);
26857 if (GET_CODE (pat) == PARALLEL)
26859 int i;
26861 for (i = 0; i < XVECLEN (pat, 0); i++)
26862 if (is_store_insn1 (XVECEXP (pat, 0, i), str_mem))
26863 return true;
26866 return false;
26869 /* Determine if INSN stores to memory. */
26871 static bool
26872 is_store_insn (rtx insn, rtx *str_mem)
26874 if (!insn || !INSN_P (insn))
26875 return false;
26877 return is_store_insn1 (PATTERN (insn), str_mem);
26880 /* Returns whether the dependence between INSN and NEXT is considered
26881 costly by the given target. */
26883 static bool
26884 rs6000_is_costly_dependence (dep_t dep, int cost, int distance)
26886 rtx insn;
26887 rtx next;
26888 rtx load_mem, str_mem;
26890 /* If the flag is not enabled - no dependence is considered costly;
26891 allow all dependent insns in the same group.
26892 This is the most aggressive option. */
26893 if (rs6000_sched_costly_dep == no_dep_costly)
26894 return false;
26896 /* If the flag is set to 1 - a dependence is always considered costly;
26897 do not allow dependent instructions in the same group.
26898 This is the most conservative option. */
26899 if (rs6000_sched_costly_dep == all_deps_costly)
26900 return true;
26902 insn = DEP_PRO (dep);
26903 next = DEP_CON (dep);
26905 if (rs6000_sched_costly_dep == store_to_load_dep_costly
26906 && is_load_insn (next, &load_mem)
26907 && is_store_insn (insn, &str_mem))
26908 /* Prevent load after store in the same group. */
26909 return true;
26911 if (rs6000_sched_costly_dep == true_store_to_load_dep_costly
26912 && is_load_insn (next, &load_mem)
26913 && is_store_insn (insn, &str_mem)
26914 && DEP_TYPE (dep) == REG_DEP_TRUE
26915 && mem_locations_overlap(str_mem, load_mem))
26916 /* Prevent load after store in the same group if it is a true
26917 dependence. */
26918 return true;
26920 /* The flag is set to X; dependences with latency >= X are considered costly,
26921 and will not be scheduled in the same group. */
26922 if (rs6000_sched_costly_dep <= max_dep_latency
26923 && ((cost - distance) >= (int)rs6000_sched_costly_dep))
26924 return true;
26926 return false;
26929 /* Return the next insn after INSN that is found before TAIL is reached,
26930 skipping any "non-active" insns - insns that will not actually occupy
26931 an issue slot. Return NULL_RTX if such an insn is not found. */
26933 static rtx
26934 get_next_active_insn (rtx insn, rtx tail)
26936 if (insn == NULL_RTX || insn == tail)
26937 return NULL_RTX;
26939 while (1)
26941 insn = NEXT_INSN (insn);
26942 if (insn == NULL_RTX || insn == tail)
26943 return NULL_RTX;
26945 if (CALL_P (insn)
26946 || JUMP_P (insn) || JUMP_TABLE_DATA_P (insn)
26947 || (NONJUMP_INSN_P (insn)
26948 && GET_CODE (PATTERN (insn)) != USE
26949 && GET_CODE (PATTERN (insn)) != CLOBBER
26950 && INSN_CODE (insn) != CODE_FOR_stack_tie))
26951 break;
26953 return insn;
26956 /* We are about to begin issuing insns for this clock cycle. */
26958 static int
26959 rs6000_sched_reorder (FILE *dump ATTRIBUTE_UNUSED, int sched_verbose,
26960 rtx *ready ATTRIBUTE_UNUSED,
26961 int *pn_ready ATTRIBUTE_UNUSED,
26962 int clock_var ATTRIBUTE_UNUSED)
26964 int n_ready = *pn_ready;
26966 if (sched_verbose)
26967 fprintf (dump, "// rs6000_sched_reorder :\n");
26969 /* Reorder the ready list, if the second to last ready insn
26970 is a nonepipeline insn. */
26971 if (rs6000_cpu_attr == CPU_CELL && n_ready > 1)
26973 if (is_nonpipeline_insn (ready[n_ready - 1])
26974 && (recog_memoized (ready[n_ready - 2]) > 0))
26975 /* Simply swap first two insns. */
26977 rtx tmp = ready[n_ready - 1];
26978 ready[n_ready - 1] = ready[n_ready - 2];
26979 ready[n_ready - 2] = tmp;
26983 if (rs6000_cpu == PROCESSOR_POWER6)
26984 load_store_pendulum = 0;
26986 return rs6000_issue_rate ();
26989 /* Like rs6000_sched_reorder, but called after issuing each insn. */
26991 static int
26992 rs6000_sched_reorder2 (FILE *dump, int sched_verbose, rtx *ready,
26993 int *pn_ready, int clock_var ATTRIBUTE_UNUSED)
26995 if (sched_verbose)
26996 fprintf (dump, "// rs6000_sched_reorder2 :\n");
26998 /* For Power6, we need to handle some special cases to try and keep the
26999 store queue from overflowing and triggering expensive flushes.
27001 This code monitors how load and store instructions are being issued
27002 and skews the ready list one way or the other to increase the likelihood
27003 that a desired instruction is issued at the proper time.
27005 A couple of things are done. First, we maintain a "load_store_pendulum"
27006 to track the current state of load/store issue.
27008 - If the pendulum is at zero, then no loads or stores have been
27009 issued in the current cycle so we do nothing.
27011 - If the pendulum is 1, then a single load has been issued in this
27012 cycle and we attempt to locate another load in the ready list to
27013 issue with it.
27015 - If the pendulum is -2, then two stores have already been
27016 issued in this cycle, so we increase the priority of the first load
27017 in the ready list to increase it's likelihood of being chosen first
27018 in the next cycle.
27020 - If the pendulum is -1, then a single store has been issued in this
27021 cycle and we attempt to locate another store in the ready list to
27022 issue with it, preferring a store to an adjacent memory location to
27023 facilitate store pairing in the store queue.
27025 - If the pendulum is 2, then two loads have already been
27026 issued in this cycle, so we increase the priority of the first store
27027 in the ready list to increase it's likelihood of being chosen first
27028 in the next cycle.
27030 - If the pendulum < -2 or > 2, then do nothing.
27032 Note: This code covers the most common scenarios. There exist non
27033 load/store instructions which make use of the LSU and which
27034 would need to be accounted for to strictly model the behavior
27035 of the machine. Those instructions are currently unaccounted
27036 for to help minimize compile time overhead of this code.
27038 if (rs6000_cpu == PROCESSOR_POWER6 && last_scheduled_insn)
27040 int pos;
27041 int i;
27042 rtx tmp, load_mem, str_mem;
27044 if (is_store_insn (last_scheduled_insn, &str_mem))
27045 /* Issuing a store, swing the load_store_pendulum to the left */
27046 load_store_pendulum--;
27047 else if (is_load_insn (last_scheduled_insn, &load_mem))
27048 /* Issuing a load, swing the load_store_pendulum to the right */
27049 load_store_pendulum++;
27050 else
27051 return cached_can_issue_more;
27053 /* If the pendulum is balanced, or there is only one instruction on
27054 the ready list, then all is well, so return. */
27055 if ((load_store_pendulum == 0) || (*pn_ready <= 1))
27056 return cached_can_issue_more;
27058 if (load_store_pendulum == 1)
27060 /* A load has been issued in this cycle. Scan the ready list
27061 for another load to issue with it */
27062 pos = *pn_ready-1;
27064 while (pos >= 0)
27066 if (is_load_insn (ready[pos], &load_mem))
27068 /* Found a load. Move it to the head of the ready list,
27069 and adjust it's priority so that it is more likely to
27070 stay there */
27071 tmp = ready[pos];
27072 for (i=pos; i<*pn_ready-1; i++)
27073 ready[i] = ready[i + 1];
27074 ready[*pn_ready-1] = tmp;
27076 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
27077 INSN_PRIORITY (tmp)++;
27078 break;
27080 pos--;
27083 else if (load_store_pendulum == -2)
27085 /* Two stores have been issued in this cycle. Increase the
27086 priority of the first load in the ready list to favor it for
27087 issuing in the next cycle. */
27088 pos = *pn_ready-1;
27090 while (pos >= 0)
27092 if (is_load_insn (ready[pos], &load_mem)
27093 && !sel_sched_p ()
27094 && INSN_PRIORITY_KNOWN (ready[pos]))
27096 INSN_PRIORITY (ready[pos])++;
27098 /* Adjust the pendulum to account for the fact that a load
27099 was found and increased in priority. This is to prevent
27100 increasing the priority of multiple loads */
27101 load_store_pendulum--;
27103 break;
27105 pos--;
27108 else if (load_store_pendulum == -1)
27110 /* A store has been issued in this cycle. Scan the ready list for
27111 another store to issue with it, preferring a store to an adjacent
27112 memory location */
27113 int first_store_pos = -1;
27115 pos = *pn_ready-1;
27117 while (pos >= 0)
27119 if (is_store_insn (ready[pos], &str_mem))
27121 rtx str_mem2;
27122 /* Maintain the index of the first store found on the
27123 list */
27124 if (first_store_pos == -1)
27125 first_store_pos = pos;
27127 if (is_store_insn (last_scheduled_insn, &str_mem2)
27128 && adjacent_mem_locations (str_mem, str_mem2))
27130 /* Found an adjacent store. Move it to the head of the
27131 ready list, and adjust it's priority so that it is
27132 more likely to stay there */
27133 tmp = ready[pos];
27134 for (i=pos; i<*pn_ready-1; i++)
27135 ready[i] = ready[i + 1];
27136 ready[*pn_ready-1] = tmp;
27138 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
27139 INSN_PRIORITY (tmp)++;
27141 first_store_pos = -1;
27143 break;
27146 pos--;
27149 if (first_store_pos >= 0)
27151 /* An adjacent store wasn't found, but a non-adjacent store was,
27152 so move the non-adjacent store to the front of the ready
27153 list, and adjust its priority so that it is more likely to
27154 stay there. */
27155 tmp = ready[first_store_pos];
27156 for (i=first_store_pos; i<*pn_ready-1; i++)
27157 ready[i] = ready[i + 1];
27158 ready[*pn_ready-1] = tmp;
27159 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
27160 INSN_PRIORITY (tmp)++;
27163 else if (load_store_pendulum == 2)
27165 /* Two loads have been issued in this cycle. Increase the priority
27166 of the first store in the ready list to favor it for issuing in
27167 the next cycle. */
27168 pos = *pn_ready-1;
27170 while (pos >= 0)
27172 if (is_store_insn (ready[pos], &str_mem)
27173 && !sel_sched_p ()
27174 && INSN_PRIORITY_KNOWN (ready[pos]))
27176 INSN_PRIORITY (ready[pos])++;
27178 /* Adjust the pendulum to account for the fact that a store
27179 was found and increased in priority. This is to prevent
27180 increasing the priority of multiple stores */
27181 load_store_pendulum++;
27183 break;
27185 pos--;
27190 return cached_can_issue_more;
27193 /* Return whether the presence of INSN causes a dispatch group termination
27194 of group WHICH_GROUP.
27196 If WHICH_GROUP == current_group, this function will return true if INSN
27197 causes the termination of the current group (i.e, the dispatch group to
27198 which INSN belongs). This means that INSN will be the last insn in the
27199 group it belongs to.
27201 If WHICH_GROUP == previous_group, this function will return true if INSN
27202 causes the termination of the previous group (i.e, the dispatch group that
27203 precedes the group to which INSN belongs). This means that INSN will be
27204 the first insn in the group it belongs to). */
27206 static bool
27207 insn_terminates_group_p (rtx insn, enum group_termination which_group)
27209 bool first, last;
27211 if (! insn)
27212 return false;
27214 first = insn_must_be_first_in_group (insn);
27215 last = insn_must_be_last_in_group (insn);
27217 if (first && last)
27218 return true;
27220 if (which_group == current_group)
27221 return last;
27222 else if (which_group == previous_group)
27223 return first;
27225 return false;
27229 static bool
27230 insn_must_be_first_in_group (rtx insn)
27232 enum attr_type type;
27234 if (!insn
27235 || NOTE_P (insn)
27236 || DEBUG_INSN_P (insn)
27237 || GET_CODE (PATTERN (insn)) == USE
27238 || GET_CODE (PATTERN (insn)) == CLOBBER)
27239 return false;
27241 switch (rs6000_cpu)
27243 case PROCESSOR_POWER5:
27244 if (is_cracked_insn (insn))
27245 return true;
27246 case PROCESSOR_POWER4:
27247 if (is_microcoded_insn (insn))
27248 return true;
27250 if (!rs6000_sched_groups)
27251 return false;
27253 type = get_attr_type (insn);
27255 switch (type)
27257 case TYPE_MFCR:
27258 case TYPE_MFCRF:
27259 case TYPE_MTCR:
27260 case TYPE_DELAYED_CR:
27261 case TYPE_CR_LOGICAL:
27262 case TYPE_MTJMPR:
27263 case TYPE_MFJMPR:
27264 case TYPE_IDIV:
27265 case TYPE_LDIV:
27266 case TYPE_LOAD_L:
27267 case TYPE_STORE_C:
27268 case TYPE_ISYNC:
27269 case TYPE_SYNC:
27270 return true;
27271 default:
27272 break;
27274 break;
27275 case PROCESSOR_POWER6:
27276 type = get_attr_type (insn);
27278 switch (type)
27280 case TYPE_INSERT_DWORD:
27281 case TYPE_EXTS:
27282 case TYPE_CNTLZ:
27283 case TYPE_SHIFT:
27284 case TYPE_VAR_SHIFT_ROTATE:
27285 case TYPE_TRAP:
27286 case TYPE_IMUL:
27287 case TYPE_IMUL2:
27288 case TYPE_IMUL3:
27289 case TYPE_LMUL:
27290 case TYPE_IDIV:
27291 case TYPE_INSERT_WORD:
27292 case TYPE_DELAYED_COMPARE:
27293 case TYPE_IMUL_COMPARE:
27294 case TYPE_LMUL_COMPARE:
27295 case TYPE_FPCOMPARE:
27296 case TYPE_MFCR:
27297 case TYPE_MTCR:
27298 case TYPE_MFJMPR:
27299 case TYPE_MTJMPR:
27300 case TYPE_ISYNC:
27301 case TYPE_SYNC:
27302 case TYPE_LOAD_L:
27303 case TYPE_STORE_C:
27304 return true;
27305 case TYPE_LOAD:
27306 case TYPE_STORE:
27307 case TYPE_FPLOAD:
27308 case TYPE_FPSTORE:
27309 if (get_attr_update (insn) == UPDATE_YES)
27310 return true;
27311 else
27312 break;
27313 default:
27314 break;
27316 break;
27317 case PROCESSOR_POWER7:
27318 type = get_attr_type (insn);
27320 switch (type)
27322 case TYPE_CR_LOGICAL:
27323 case TYPE_MFCR:
27324 case TYPE_MFCRF:
27325 case TYPE_MTCR:
27326 case TYPE_IDIV:
27327 case TYPE_LDIV:
27328 case TYPE_COMPARE:
27329 case TYPE_DELAYED_COMPARE:
27330 case TYPE_VAR_DELAYED_COMPARE:
27331 case TYPE_ISYNC:
27332 case TYPE_LOAD_L:
27333 case TYPE_STORE_C:
27334 case TYPE_MFJMPR:
27335 case TYPE_MTJMPR:
27336 return true;
27337 case TYPE_LOAD:
27338 if (get_attr_sign_extend (insn) == SIGN_EXTEND_YES
27339 || get_attr_update (insn) == UPDATE_YES)
27340 return true;
27341 else
27342 break;
27343 case TYPE_STORE:
27344 case TYPE_FPLOAD:
27345 case TYPE_FPSTORE:
27346 if (get_attr_update (insn) == UPDATE_YES)
27347 return true;
27348 else
27349 break;
27350 default:
27351 break;
27353 break;
27354 case PROCESSOR_POWER8:
27355 type = get_attr_type (insn);
27357 switch (type)
27359 case TYPE_CR_LOGICAL:
27360 case TYPE_DELAYED_CR:
27361 case TYPE_MFCR:
27362 case TYPE_MFCRF:
27363 case TYPE_MTCR:
27364 case TYPE_COMPARE:
27365 case TYPE_DELAYED_COMPARE:
27366 case TYPE_VAR_DELAYED_COMPARE:
27367 case TYPE_IMUL_COMPARE:
27368 case TYPE_LMUL_COMPARE:
27369 case TYPE_SYNC:
27370 case TYPE_ISYNC:
27371 case TYPE_LOAD_L:
27372 case TYPE_STORE_C:
27373 case TYPE_VECSTORE:
27374 case TYPE_MFJMPR:
27375 case TYPE_MTJMPR:
27376 return true;
27377 case TYPE_LOAD:
27378 if (get_attr_sign_extend (insn) == SIGN_EXTEND_YES
27379 || get_attr_update (insn) == UPDATE_YES)
27380 return true;
27381 else
27382 break;
27383 case TYPE_STORE:
27384 if (get_attr_update (insn) == UPDATE_YES
27385 && get_attr_indexed (insn) == INDEXED_YES)
27386 return true;
27387 else
27388 break;
27389 default:
27390 break;
27392 break;
27393 default:
27394 break;
27397 return false;
27400 static bool
27401 insn_must_be_last_in_group (rtx insn)
27403 enum attr_type type;
27405 if (!insn
27406 || NOTE_P (insn)
27407 || DEBUG_INSN_P (insn)
27408 || GET_CODE (PATTERN (insn)) == USE
27409 || GET_CODE (PATTERN (insn)) == CLOBBER)
27410 return false;
27412 switch (rs6000_cpu) {
27413 case PROCESSOR_POWER4:
27414 case PROCESSOR_POWER5:
27415 if (is_microcoded_insn (insn))
27416 return true;
27418 if (is_branch_slot_insn (insn))
27419 return true;
27421 break;
27422 case PROCESSOR_POWER6:
27423 type = get_attr_type (insn);
27425 switch (type)
27427 case TYPE_EXTS:
27428 case TYPE_CNTLZ:
27429 case TYPE_SHIFT:
27430 case TYPE_VAR_SHIFT_ROTATE:
27431 case TYPE_TRAP:
27432 case TYPE_IMUL:
27433 case TYPE_IMUL2:
27434 case TYPE_IMUL3:
27435 case TYPE_LMUL:
27436 case TYPE_IDIV:
27437 case TYPE_DELAYED_COMPARE:
27438 case TYPE_IMUL_COMPARE:
27439 case TYPE_LMUL_COMPARE:
27440 case TYPE_FPCOMPARE:
27441 case TYPE_MFCR:
27442 case TYPE_MTCR:
27443 case TYPE_MFJMPR:
27444 case TYPE_MTJMPR:
27445 case TYPE_ISYNC:
27446 case TYPE_SYNC:
27447 case TYPE_LOAD_L:
27448 case TYPE_STORE_C:
27449 return true;
27450 default:
27451 break;
27453 break;
27454 case PROCESSOR_POWER7:
27455 type = get_attr_type (insn);
27457 switch (type)
27459 case TYPE_ISYNC:
27460 case TYPE_SYNC:
27461 case TYPE_LOAD_L:
27462 case TYPE_STORE_C:
27463 return true;
27464 case TYPE_LOAD:
27465 if (get_attr_sign_extend (insn) == SIGN_EXTEND_YES
27466 && get_attr_update (insn) == UPDATE_YES)
27467 return true;
27468 else
27469 break;
27470 case TYPE_STORE:
27471 if (get_attr_update (insn) == UPDATE_YES
27472 && get_attr_indexed (insn) == INDEXED_YES)
27473 return true;
27474 else
27475 break;
27476 default:
27477 break;
27479 break;
27480 case PROCESSOR_POWER8:
27481 type = get_attr_type (insn);
27483 switch (type)
27485 case TYPE_MFCR:
27486 case TYPE_MTCR:
27487 case TYPE_ISYNC:
27488 case TYPE_SYNC:
27489 case TYPE_LOAD_L:
27490 case TYPE_STORE_C:
27491 return true;
27492 case TYPE_LOAD:
27493 if (get_attr_sign_extend (insn) == SIGN_EXTEND_YES
27494 && get_attr_update (insn) == UPDATE_YES)
27495 return true;
27496 else
27497 break;
27498 case TYPE_STORE:
27499 if (get_attr_update (insn) == UPDATE_YES
27500 && get_attr_indexed (insn) == INDEXED_YES)
27501 return true;
27502 else
27503 break;
27504 default:
27505 break;
27507 break;
27508 default:
27509 break;
27512 return false;
27515 /* Return true if it is recommended to keep NEXT_INSN "far" (in a separate
27516 dispatch group) from the insns in GROUP_INSNS. Return false otherwise. */
27518 static bool
27519 is_costly_group (rtx *group_insns, rtx next_insn)
27521 int i;
27522 int issue_rate = rs6000_issue_rate ();
27524 for (i = 0; i < issue_rate; i++)
27526 sd_iterator_def sd_it;
27527 dep_t dep;
27528 rtx insn = group_insns[i];
27530 if (!insn)
27531 continue;
27533 FOR_EACH_DEP (insn, SD_LIST_RES_FORW, sd_it, dep)
27535 rtx next = DEP_CON (dep);
27537 if (next == next_insn
27538 && rs6000_is_costly_dependence (dep, dep_cost (dep), 0))
27539 return true;
27543 return false;
27546 /* Utility of the function redefine_groups.
27547 Check if it is too costly to schedule NEXT_INSN together with GROUP_INSNS
27548 in the same dispatch group. If so, insert nops before NEXT_INSN, in order
27549 to keep it "far" (in a separate group) from GROUP_INSNS, following
27550 one of the following schemes, depending on the value of the flag
27551 -minsert_sched_nops = X:
27552 (1) X == sched_finish_regroup_exact: insert exactly as many nops as needed
27553 in order to force NEXT_INSN into a separate group.
27554 (2) X < sched_finish_regroup_exact: insert exactly X nops.
27555 GROUP_END, CAN_ISSUE_MORE and GROUP_COUNT record the state after nop
27556 insertion (has a group just ended, how many vacant issue slots remain in the
27557 last group, and how many dispatch groups were encountered so far). */
27559 static int
27560 force_new_group (int sched_verbose, FILE *dump, rtx *group_insns,
27561 rtx next_insn, bool *group_end, int can_issue_more,
27562 int *group_count)
27564 rtx nop;
27565 bool force;
27566 int issue_rate = rs6000_issue_rate ();
27567 bool end = *group_end;
27568 int i;
27570 if (next_insn == NULL_RTX || DEBUG_INSN_P (next_insn))
27571 return can_issue_more;
27573 if (rs6000_sched_insert_nops > sched_finish_regroup_exact)
27574 return can_issue_more;
27576 force = is_costly_group (group_insns, next_insn);
27577 if (!force)
27578 return can_issue_more;
27580 if (sched_verbose > 6)
27581 fprintf (dump,"force: group count = %d, can_issue_more = %d\n",
27582 *group_count ,can_issue_more);
27584 if (rs6000_sched_insert_nops == sched_finish_regroup_exact)
27586 if (*group_end)
27587 can_issue_more = 0;
27589 /* Since only a branch can be issued in the last issue_slot, it is
27590 sufficient to insert 'can_issue_more - 1' nops if next_insn is not
27591 a branch. If next_insn is a branch, we insert 'can_issue_more' nops;
27592 in this case the last nop will start a new group and the branch
27593 will be forced to the new group. */
27594 if (can_issue_more && !is_branch_slot_insn (next_insn))
27595 can_issue_more--;
27597 /* Do we have a special group ending nop? */
27598 if (rs6000_cpu_attr == CPU_POWER6 || rs6000_cpu_attr == CPU_POWER7
27599 || rs6000_cpu_attr == CPU_POWER8)
27601 nop = gen_group_ending_nop ();
27602 emit_insn_before (nop, next_insn);
27603 can_issue_more = 0;
27605 else
27606 while (can_issue_more > 0)
27608 nop = gen_nop ();
27609 emit_insn_before (nop, next_insn);
27610 can_issue_more--;
27613 *group_end = true;
27614 return 0;
27617 if (rs6000_sched_insert_nops < sched_finish_regroup_exact)
27619 int n_nops = rs6000_sched_insert_nops;
27621 /* Nops can't be issued from the branch slot, so the effective
27622 issue_rate for nops is 'issue_rate - 1'. */
27623 if (can_issue_more == 0)
27624 can_issue_more = issue_rate;
27625 can_issue_more--;
27626 if (can_issue_more == 0)
27628 can_issue_more = issue_rate - 1;
27629 (*group_count)++;
27630 end = true;
27631 for (i = 0; i < issue_rate; i++)
27633 group_insns[i] = 0;
27637 while (n_nops > 0)
27639 nop = gen_nop ();
27640 emit_insn_before (nop, next_insn);
27641 if (can_issue_more == issue_rate - 1) /* new group begins */
27642 end = false;
27643 can_issue_more--;
27644 if (can_issue_more == 0)
27646 can_issue_more = issue_rate - 1;
27647 (*group_count)++;
27648 end = true;
27649 for (i = 0; i < issue_rate; i++)
27651 group_insns[i] = 0;
27654 n_nops--;
27657 /* Scale back relative to 'issue_rate' (instead of 'issue_rate - 1'). */
27658 can_issue_more++;
27660 /* Is next_insn going to start a new group? */
27661 *group_end
27662 = (end
27663 || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
27664 || (can_issue_more <= 2 && is_cracked_insn (next_insn))
27665 || (can_issue_more < issue_rate &&
27666 insn_terminates_group_p (next_insn, previous_group)));
27667 if (*group_end && end)
27668 (*group_count)--;
27670 if (sched_verbose > 6)
27671 fprintf (dump, "done force: group count = %d, can_issue_more = %d\n",
27672 *group_count, can_issue_more);
27673 return can_issue_more;
27676 return can_issue_more;
27679 /* This function tries to synch the dispatch groups that the compiler "sees"
27680 with the dispatch groups that the processor dispatcher is expected to
27681 form in practice. It tries to achieve this synchronization by forcing the
27682 estimated processor grouping on the compiler (as opposed to the function
27683 'pad_goups' which tries to force the scheduler's grouping on the processor).
27685 The function scans the insn sequence between PREV_HEAD_INSN and TAIL and
27686 examines the (estimated) dispatch groups that will be formed by the processor
27687 dispatcher. It marks these group boundaries to reflect the estimated
27688 processor grouping, overriding the grouping that the scheduler had marked.
27689 Depending on the value of the flag '-minsert-sched-nops' this function can
27690 force certain insns into separate groups or force a certain distance between
27691 them by inserting nops, for example, if there exists a "costly dependence"
27692 between the insns.
27694 The function estimates the group boundaries that the processor will form as
27695 follows: It keeps track of how many vacant issue slots are available after
27696 each insn. A subsequent insn will start a new group if one of the following
27697 4 cases applies:
27698 - no more vacant issue slots remain in the current dispatch group.
27699 - only the last issue slot, which is the branch slot, is vacant, but the next
27700 insn is not a branch.
27701 - only the last 2 or less issue slots, including the branch slot, are vacant,
27702 which means that a cracked insn (which occupies two issue slots) can't be
27703 issued in this group.
27704 - less than 'issue_rate' slots are vacant, and the next insn always needs to
27705 start a new group. */
27707 static int
27708 redefine_groups (FILE *dump, int sched_verbose, rtx prev_head_insn, rtx tail)
27710 rtx insn, next_insn;
27711 int issue_rate;
27712 int can_issue_more;
27713 int slot, i;
27714 bool group_end;
27715 int group_count = 0;
27716 rtx *group_insns;
27718 /* Initialize. */
27719 issue_rate = rs6000_issue_rate ();
27720 group_insns = XALLOCAVEC (rtx, issue_rate);
27721 for (i = 0; i < issue_rate; i++)
27723 group_insns[i] = 0;
27725 can_issue_more = issue_rate;
27726 slot = 0;
27727 insn = get_next_active_insn (prev_head_insn, tail);
27728 group_end = false;
27730 while (insn != NULL_RTX)
27732 slot = (issue_rate - can_issue_more);
27733 group_insns[slot] = insn;
27734 can_issue_more =
27735 rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
27736 if (insn_terminates_group_p (insn, current_group))
27737 can_issue_more = 0;
27739 next_insn = get_next_active_insn (insn, tail);
27740 if (next_insn == NULL_RTX)
27741 return group_count + 1;
27743 /* Is next_insn going to start a new group? */
27744 group_end
27745 = (can_issue_more == 0
27746 || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
27747 || (can_issue_more <= 2 && is_cracked_insn (next_insn))
27748 || (can_issue_more < issue_rate &&
27749 insn_terminates_group_p (next_insn, previous_group)));
27751 can_issue_more = force_new_group (sched_verbose, dump, group_insns,
27752 next_insn, &group_end, can_issue_more,
27753 &group_count);
27755 if (group_end)
27757 group_count++;
27758 can_issue_more = 0;
27759 for (i = 0; i < issue_rate; i++)
27761 group_insns[i] = 0;
27765 if (GET_MODE (next_insn) == TImode && can_issue_more)
27766 PUT_MODE (next_insn, VOIDmode);
27767 else if (!can_issue_more && GET_MODE (next_insn) != TImode)
27768 PUT_MODE (next_insn, TImode);
27770 insn = next_insn;
27771 if (can_issue_more == 0)
27772 can_issue_more = issue_rate;
27773 } /* while */
27775 return group_count;
27778 /* Scan the insn sequence between PREV_HEAD_INSN and TAIL and examine the
27779 dispatch group boundaries that the scheduler had marked. Pad with nops
27780 any dispatch groups which have vacant issue slots, in order to force the
27781 scheduler's grouping on the processor dispatcher. The function
27782 returns the number of dispatch groups found. */
27784 static int
27785 pad_groups (FILE *dump, int sched_verbose, rtx prev_head_insn, rtx tail)
27787 rtx insn, next_insn;
27788 rtx nop;
27789 int issue_rate;
27790 int can_issue_more;
27791 int group_end;
27792 int group_count = 0;
27794 /* Initialize issue_rate. */
27795 issue_rate = rs6000_issue_rate ();
27796 can_issue_more = issue_rate;
27798 insn = get_next_active_insn (prev_head_insn, tail);
27799 next_insn = get_next_active_insn (insn, tail);
27801 while (insn != NULL_RTX)
27803 can_issue_more =
27804 rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
27806 group_end = (next_insn == NULL_RTX || GET_MODE (next_insn) == TImode);
27808 if (next_insn == NULL_RTX)
27809 break;
27811 if (group_end)
27813 /* If the scheduler had marked group termination at this location
27814 (between insn and next_insn), and neither insn nor next_insn will
27815 force group termination, pad the group with nops to force group
27816 termination. */
27817 if (can_issue_more
27818 && (rs6000_sched_insert_nops == sched_finish_pad_groups)
27819 && !insn_terminates_group_p (insn, current_group)
27820 && !insn_terminates_group_p (next_insn, previous_group))
27822 if (!is_branch_slot_insn (next_insn))
27823 can_issue_more--;
27825 while (can_issue_more)
27827 nop = gen_nop ();
27828 emit_insn_before (nop, next_insn);
27829 can_issue_more--;
27833 can_issue_more = issue_rate;
27834 group_count++;
27837 insn = next_insn;
27838 next_insn = get_next_active_insn (insn, tail);
27841 return group_count;
27844 /* We're beginning a new block. Initialize data structures as necessary. */
27846 static void
27847 rs6000_sched_init (FILE *dump ATTRIBUTE_UNUSED,
27848 int sched_verbose ATTRIBUTE_UNUSED,
27849 int max_ready ATTRIBUTE_UNUSED)
27851 last_scheduled_insn = NULL_RTX;
27852 load_store_pendulum = 0;
27855 /* The following function is called at the end of scheduling BB.
27856 After reload, it inserts nops at insn group bundling. */
27858 static void
27859 rs6000_sched_finish (FILE *dump, int sched_verbose)
27861 int n_groups;
27863 if (sched_verbose)
27864 fprintf (dump, "=== Finishing schedule.\n");
27866 if (reload_completed && rs6000_sched_groups)
27868 /* Do not run sched_finish hook when selective scheduling enabled. */
27869 if (sel_sched_p ())
27870 return;
27872 if (rs6000_sched_insert_nops == sched_finish_none)
27873 return;
27875 if (rs6000_sched_insert_nops == sched_finish_pad_groups)
27876 n_groups = pad_groups (dump, sched_verbose,
27877 current_sched_info->prev_head,
27878 current_sched_info->next_tail);
27879 else
27880 n_groups = redefine_groups (dump, sched_verbose,
27881 current_sched_info->prev_head,
27882 current_sched_info->next_tail);
27884 if (sched_verbose >= 6)
27886 fprintf (dump, "ngroups = %d\n", n_groups);
27887 print_rtl (dump, current_sched_info->prev_head);
27888 fprintf (dump, "Done finish_sched\n");
27893 struct _rs6000_sched_context
27895 short cached_can_issue_more;
27896 rtx last_scheduled_insn;
27897 int load_store_pendulum;
27900 typedef struct _rs6000_sched_context rs6000_sched_context_def;
27901 typedef rs6000_sched_context_def *rs6000_sched_context_t;
27903 /* Allocate store for new scheduling context. */
27904 static void *
27905 rs6000_alloc_sched_context (void)
27907 return xmalloc (sizeof (rs6000_sched_context_def));
27910 /* If CLEAN_P is true then initializes _SC with clean data,
27911 and from the global context otherwise. */
27912 static void
27913 rs6000_init_sched_context (void *_sc, bool clean_p)
27915 rs6000_sched_context_t sc = (rs6000_sched_context_t) _sc;
27917 if (clean_p)
27919 sc->cached_can_issue_more = 0;
27920 sc->last_scheduled_insn = NULL_RTX;
27921 sc->load_store_pendulum = 0;
27923 else
27925 sc->cached_can_issue_more = cached_can_issue_more;
27926 sc->last_scheduled_insn = last_scheduled_insn;
27927 sc->load_store_pendulum = load_store_pendulum;
27931 /* Sets the global scheduling context to the one pointed to by _SC. */
27932 static void
27933 rs6000_set_sched_context (void *_sc)
27935 rs6000_sched_context_t sc = (rs6000_sched_context_t) _sc;
27937 gcc_assert (sc != NULL);
27939 cached_can_issue_more = sc->cached_can_issue_more;
27940 last_scheduled_insn = sc->last_scheduled_insn;
27941 load_store_pendulum = sc->load_store_pendulum;
27944 /* Free _SC. */
27945 static void
27946 rs6000_free_sched_context (void *_sc)
27948 gcc_assert (_sc != NULL);
27950 free (_sc);
27954 /* Length in units of the trampoline for entering a nested function. */
27957 rs6000_trampoline_size (void)
27959 int ret = 0;
27961 switch (DEFAULT_ABI)
27963 default:
27964 gcc_unreachable ();
27966 case ABI_AIX:
27967 ret = (TARGET_32BIT) ? 12 : 24;
27968 break;
27970 case ABI_ELFv2:
27971 gcc_assert (!TARGET_32BIT);
27972 ret = 32;
27973 break;
27975 case ABI_DARWIN:
27976 case ABI_V4:
27977 ret = (TARGET_32BIT) ? 40 : 48;
27978 break;
27981 return ret;
27984 /* Emit RTL insns to initialize the variable parts of a trampoline.
27985 FNADDR is an RTX for the address of the function's pure code.
27986 CXT is an RTX for the static chain value for the function. */
27988 static void
27989 rs6000_trampoline_init (rtx m_tramp, tree fndecl, rtx cxt)
27991 int regsize = (TARGET_32BIT) ? 4 : 8;
27992 rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
27993 rtx ctx_reg = force_reg (Pmode, cxt);
27994 rtx addr = force_reg (Pmode, XEXP (m_tramp, 0));
27996 switch (DEFAULT_ABI)
27998 default:
27999 gcc_unreachable ();
28001 /* Under AIX, just build the 3 word function descriptor */
28002 case ABI_AIX:
28004 rtx fnmem, fn_reg, toc_reg;
28006 if (!TARGET_POINTERS_TO_NESTED_FUNCTIONS)
28007 error ("You cannot take the address of a nested function if you use "
28008 "the -mno-pointers-to-nested-functions option.");
28010 fnmem = gen_const_mem (Pmode, force_reg (Pmode, fnaddr));
28011 fn_reg = gen_reg_rtx (Pmode);
28012 toc_reg = gen_reg_rtx (Pmode);
28014 /* Macro to shorten the code expansions below. */
28015 # define MEM_PLUS(MEM, OFFSET) adjust_address (MEM, Pmode, OFFSET)
28017 m_tramp = replace_equiv_address (m_tramp, addr);
28019 emit_move_insn (fn_reg, MEM_PLUS (fnmem, 0));
28020 emit_move_insn (toc_reg, MEM_PLUS (fnmem, regsize));
28021 emit_move_insn (MEM_PLUS (m_tramp, 0), fn_reg);
28022 emit_move_insn (MEM_PLUS (m_tramp, regsize), toc_reg);
28023 emit_move_insn (MEM_PLUS (m_tramp, 2*regsize), ctx_reg);
28025 # undef MEM_PLUS
28027 break;
28029 /* Under V.4/eabi/darwin, __trampoline_setup does the real work. */
28030 case ABI_ELFv2:
28031 case ABI_DARWIN:
28032 case ABI_V4:
28033 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__trampoline_setup"),
28034 LCT_NORMAL, VOIDmode, 4,
28035 addr, Pmode,
28036 GEN_INT (rs6000_trampoline_size ()), SImode,
28037 fnaddr, Pmode,
28038 ctx_reg, Pmode);
28039 break;
28044 /* Returns TRUE iff the target attribute indicated by ATTR_ID takes a plain
28045 identifier as an argument, so the front end shouldn't look it up. */
28047 static bool
28048 rs6000_attribute_takes_identifier_p (const_tree attr_id)
28050 return is_attribute_p ("altivec", attr_id);
28053 /* Handle the "altivec" attribute. The attribute may have
28054 arguments as follows:
28056 __attribute__((altivec(vector__)))
28057 __attribute__((altivec(pixel__))) (always followed by 'unsigned short')
28058 __attribute__((altivec(bool__))) (always followed by 'unsigned')
28060 and may appear more than once (e.g., 'vector bool char') in a
28061 given declaration. */
28063 static tree
28064 rs6000_handle_altivec_attribute (tree *node,
28065 tree name ATTRIBUTE_UNUSED,
28066 tree args,
28067 int flags ATTRIBUTE_UNUSED,
28068 bool *no_add_attrs)
28070 tree type = *node, result = NULL_TREE;
28071 enum machine_mode mode;
28072 int unsigned_p;
28073 char altivec_type
28074 = ((args && TREE_CODE (args) == TREE_LIST && TREE_VALUE (args)
28075 && TREE_CODE (TREE_VALUE (args)) == IDENTIFIER_NODE)
28076 ? *IDENTIFIER_POINTER (TREE_VALUE (args))
28077 : '?');
28079 while (POINTER_TYPE_P (type)
28080 || TREE_CODE (type) == FUNCTION_TYPE
28081 || TREE_CODE (type) == METHOD_TYPE
28082 || TREE_CODE (type) == ARRAY_TYPE)
28083 type = TREE_TYPE (type);
28085 mode = TYPE_MODE (type);
28087 /* Check for invalid AltiVec type qualifiers. */
28088 if (type == long_double_type_node)
28089 error ("use of %<long double%> in AltiVec types is invalid");
28090 else if (type == boolean_type_node)
28091 error ("use of boolean types in AltiVec types is invalid");
28092 else if (TREE_CODE (type) == COMPLEX_TYPE)
28093 error ("use of %<complex%> in AltiVec types is invalid");
28094 else if (DECIMAL_FLOAT_MODE_P (mode))
28095 error ("use of decimal floating point types in AltiVec types is invalid");
28096 else if (!TARGET_VSX)
28098 if (type == long_unsigned_type_node || type == long_integer_type_node)
28100 if (TARGET_64BIT)
28101 error ("use of %<long%> in AltiVec types is invalid for "
28102 "64-bit code without -mvsx");
28103 else if (rs6000_warn_altivec_long)
28104 warning (0, "use of %<long%> in AltiVec types is deprecated; "
28105 "use %<int%>");
28107 else if (type == long_long_unsigned_type_node
28108 || type == long_long_integer_type_node)
28109 error ("use of %<long long%> in AltiVec types is invalid without "
28110 "-mvsx");
28111 else if (type == double_type_node)
28112 error ("use of %<double%> in AltiVec types is invalid without -mvsx");
28115 switch (altivec_type)
28117 case 'v':
28118 unsigned_p = TYPE_UNSIGNED (type);
28119 switch (mode)
28121 case TImode:
28122 result = (unsigned_p ? unsigned_V1TI_type_node : V1TI_type_node);
28123 break;
28124 case DImode:
28125 result = (unsigned_p ? unsigned_V2DI_type_node : V2DI_type_node);
28126 break;
28127 case SImode:
28128 result = (unsigned_p ? unsigned_V4SI_type_node : V4SI_type_node);
28129 break;
28130 case HImode:
28131 result = (unsigned_p ? unsigned_V8HI_type_node : V8HI_type_node);
28132 break;
28133 case QImode:
28134 result = (unsigned_p ? unsigned_V16QI_type_node : V16QI_type_node);
28135 break;
28136 case SFmode: result = V4SF_type_node; break;
28137 case DFmode: result = V2DF_type_node; break;
28138 /* If the user says 'vector int bool', we may be handed the 'bool'
28139 attribute _before_ the 'vector' attribute, and so select the
28140 proper type in the 'b' case below. */
28141 case V4SImode: case V8HImode: case V16QImode: case V4SFmode:
28142 case V2DImode: case V2DFmode:
28143 result = type;
28144 default: break;
28146 break;
28147 case 'b':
28148 switch (mode)
28150 case DImode: case V2DImode: result = bool_V2DI_type_node; break;
28151 case SImode: case V4SImode: result = bool_V4SI_type_node; break;
28152 case HImode: case V8HImode: result = bool_V8HI_type_node; break;
28153 case QImode: case V16QImode: result = bool_V16QI_type_node;
28154 default: break;
28156 break;
28157 case 'p':
28158 switch (mode)
28160 case V8HImode: result = pixel_V8HI_type_node;
28161 default: break;
28163 default: break;
28166 /* Propagate qualifiers attached to the element type
28167 onto the vector type. */
28168 if (result && result != type && TYPE_QUALS (type))
28169 result = build_qualified_type (result, TYPE_QUALS (type));
28171 *no_add_attrs = true; /* No need to hang on to the attribute. */
28173 if (result)
28174 *node = lang_hooks.types.reconstruct_complex_type (*node, result);
28176 return NULL_TREE;
28179 /* AltiVec defines four built-in scalar types that serve as vector
28180 elements; we must teach the compiler how to mangle them. */
28182 static const char *
28183 rs6000_mangle_type (const_tree type)
28185 type = TYPE_MAIN_VARIANT (type);
28187 if (TREE_CODE (type) != VOID_TYPE && TREE_CODE (type) != BOOLEAN_TYPE
28188 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
28189 return NULL;
28191 if (type == bool_char_type_node) return "U6__boolc";
28192 if (type == bool_short_type_node) return "U6__bools";
28193 if (type == pixel_type_node) return "u7__pixel";
28194 if (type == bool_int_type_node) return "U6__booli";
28195 if (type == bool_long_type_node) return "U6__booll";
28197 /* Mangle IBM extended float long double as `g' (__float128) on
28198 powerpc*-linux where long-double-64 previously was the default. */
28199 if (TYPE_MAIN_VARIANT (type) == long_double_type_node
28200 && TARGET_ELF
28201 && TARGET_LONG_DOUBLE_128
28202 && !TARGET_IEEEQUAD)
28203 return "g";
28205 /* For all other types, use normal C++ mangling. */
28206 return NULL;
28209 /* Handle a "longcall" or "shortcall" attribute; arguments as in
28210 struct attribute_spec.handler. */
28212 static tree
28213 rs6000_handle_longcall_attribute (tree *node, tree name,
28214 tree args ATTRIBUTE_UNUSED,
28215 int flags ATTRIBUTE_UNUSED,
28216 bool *no_add_attrs)
28218 if (TREE_CODE (*node) != FUNCTION_TYPE
28219 && TREE_CODE (*node) != FIELD_DECL
28220 && TREE_CODE (*node) != TYPE_DECL)
28222 warning (OPT_Wattributes, "%qE attribute only applies to functions",
28223 name);
28224 *no_add_attrs = true;
28227 return NULL_TREE;
28230 /* Set longcall attributes on all functions declared when
28231 rs6000_default_long_calls is true. */
28232 static void
28233 rs6000_set_default_type_attributes (tree type)
28235 if (rs6000_default_long_calls
28236 && (TREE_CODE (type) == FUNCTION_TYPE
28237 || TREE_CODE (type) == METHOD_TYPE))
28238 TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("longcall"),
28239 NULL_TREE,
28240 TYPE_ATTRIBUTES (type));
28242 #if TARGET_MACHO
28243 darwin_set_default_type_attributes (type);
28244 #endif
28247 /* Return a reference suitable for calling a function with the
28248 longcall attribute. */
28251 rs6000_longcall_ref (rtx call_ref)
28253 const char *call_name;
28254 tree node;
28256 if (GET_CODE (call_ref) != SYMBOL_REF)
28257 return call_ref;
28259 /* System V adds '.' to the internal name, so skip them. */
28260 call_name = XSTR (call_ref, 0);
28261 if (*call_name == '.')
28263 while (*call_name == '.')
28264 call_name++;
28266 node = get_identifier (call_name);
28267 call_ref = gen_rtx_SYMBOL_REF (VOIDmode, IDENTIFIER_POINTER (node));
28270 return force_reg (Pmode, call_ref);
28273 #ifndef TARGET_USE_MS_BITFIELD_LAYOUT
28274 #define TARGET_USE_MS_BITFIELD_LAYOUT 0
28275 #endif
28277 /* Handle a "ms_struct" or "gcc_struct" attribute; arguments as in
28278 struct attribute_spec.handler. */
28279 static tree
28280 rs6000_handle_struct_attribute (tree *node, tree name,
28281 tree args ATTRIBUTE_UNUSED,
28282 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
28284 tree *type = NULL;
28285 if (DECL_P (*node))
28287 if (TREE_CODE (*node) == TYPE_DECL)
28288 type = &TREE_TYPE (*node);
28290 else
28291 type = node;
28293 if (!(type && (TREE_CODE (*type) == RECORD_TYPE
28294 || TREE_CODE (*type) == UNION_TYPE)))
28296 warning (OPT_Wattributes, "%qE attribute ignored", name);
28297 *no_add_attrs = true;
28300 else if ((is_attribute_p ("ms_struct", name)
28301 && lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (*type)))
28302 || ((is_attribute_p ("gcc_struct", name)
28303 && lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (*type)))))
28305 warning (OPT_Wattributes, "%qE incompatible attribute ignored",
28306 name);
28307 *no_add_attrs = true;
28310 return NULL_TREE;
28313 static bool
28314 rs6000_ms_bitfield_layout_p (const_tree record_type)
28316 return (TARGET_USE_MS_BITFIELD_LAYOUT &&
28317 !lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (record_type)))
28318 || lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (record_type));
28321 #ifdef USING_ELFOS_H
28323 /* A get_unnamed_section callback, used for switching to toc_section. */
28325 static void
28326 rs6000_elf_output_toc_section_asm_op (const void *data ATTRIBUTE_UNUSED)
28328 if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
28329 && TARGET_MINIMAL_TOC
28330 && !TARGET_RELOCATABLE)
28332 if (!toc_initialized)
28334 toc_initialized = 1;
28335 fprintf (asm_out_file, "%s\n", TOC_SECTION_ASM_OP);
28336 (*targetm.asm_out.internal_label) (asm_out_file, "LCTOC", 0);
28337 fprintf (asm_out_file, "\t.tc ");
28338 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1[TC],");
28339 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
28340 fprintf (asm_out_file, "\n");
28342 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
28343 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
28344 fprintf (asm_out_file, " = .+32768\n");
28346 else
28347 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
28349 else if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
28350 && !TARGET_RELOCATABLE)
28351 fprintf (asm_out_file, "%s\n", TOC_SECTION_ASM_OP);
28352 else
28354 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
28355 if (!toc_initialized)
28357 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
28358 fprintf (asm_out_file, " = .+32768\n");
28359 toc_initialized = 1;
28364 /* Implement TARGET_ASM_INIT_SECTIONS. */
28366 static void
28367 rs6000_elf_asm_init_sections (void)
28369 toc_section
28370 = get_unnamed_section (0, rs6000_elf_output_toc_section_asm_op, NULL);
28372 sdata2_section
28373 = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
28374 SDATA2_SECTION_ASM_OP);
28377 /* Implement TARGET_SELECT_RTX_SECTION. */
28379 static section *
28380 rs6000_elf_select_rtx_section (enum machine_mode mode, rtx x,
28381 unsigned HOST_WIDE_INT align)
28383 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
28384 return toc_section;
28385 else
28386 return default_elf_select_rtx_section (mode, x, align);
28389 /* For a SYMBOL_REF, set generic flags and then perform some
28390 target-specific processing.
28392 When the AIX ABI is requested on a non-AIX system, replace the
28393 function name with the real name (with a leading .) rather than the
28394 function descriptor name. This saves a lot of overriding code to
28395 read the prefixes. */
28397 static void rs6000_elf_encode_section_info (tree, rtx, int) ATTRIBUTE_UNUSED;
28398 static void
28399 rs6000_elf_encode_section_info (tree decl, rtx rtl, int first)
28401 default_encode_section_info (decl, rtl, first);
28403 if (first
28404 && TREE_CODE (decl) == FUNCTION_DECL
28405 && !TARGET_AIX
28406 && DEFAULT_ABI == ABI_AIX)
28408 rtx sym_ref = XEXP (rtl, 0);
28409 size_t len = strlen (XSTR (sym_ref, 0));
28410 char *str = XALLOCAVEC (char, len + 2);
28411 str[0] = '.';
28412 memcpy (str + 1, XSTR (sym_ref, 0), len + 1);
28413 XSTR (sym_ref, 0) = ggc_alloc_string (str, len + 1);
28417 static inline bool
28418 compare_section_name (const char *section, const char *templ)
28420 int len;
28422 len = strlen (templ);
28423 return (strncmp (section, templ, len) == 0
28424 && (section[len] == 0 || section[len] == '.'));
28427 bool
28428 rs6000_elf_in_small_data_p (const_tree decl)
28430 if (rs6000_sdata == SDATA_NONE)
28431 return false;
28433 /* We want to merge strings, so we never consider them small data. */
28434 if (TREE_CODE (decl) == STRING_CST)
28435 return false;
28437 /* Functions are never in the small data area. */
28438 if (TREE_CODE (decl) == FUNCTION_DECL)
28439 return false;
28441 if (TREE_CODE (decl) == VAR_DECL && DECL_SECTION_NAME (decl))
28443 const char *section = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
28444 if (compare_section_name (section, ".sdata")
28445 || compare_section_name (section, ".sdata2")
28446 || compare_section_name (section, ".gnu.linkonce.s")
28447 || compare_section_name (section, ".sbss")
28448 || compare_section_name (section, ".sbss2")
28449 || compare_section_name (section, ".gnu.linkonce.sb")
28450 || strcmp (section, ".PPC.EMB.sdata0") == 0
28451 || strcmp (section, ".PPC.EMB.sbss0") == 0)
28452 return true;
28454 else
28456 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (decl));
28458 if (size > 0
28459 && size <= g_switch_value
28460 /* If it's not public, and we're not going to reference it there,
28461 there's no need to put it in the small data section. */
28462 && (rs6000_sdata != SDATA_DATA || TREE_PUBLIC (decl)))
28463 return true;
28466 return false;
28469 #endif /* USING_ELFOS_H */
28471 /* Implement TARGET_USE_BLOCKS_FOR_CONSTANT_P. */
28473 static bool
28474 rs6000_use_blocks_for_constant_p (enum machine_mode mode, const_rtx x)
28476 return !ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode);
28479 /* Do not place thread-local symbols refs in the object blocks. */
28481 static bool
28482 rs6000_use_blocks_for_decl_p (const_tree decl)
28484 return !DECL_THREAD_LOCAL_P (decl);
28487 /* Return a REG that occurs in ADDR with coefficient 1.
28488 ADDR can be effectively incremented by incrementing REG.
28490 r0 is special and we must not select it as an address
28491 register by this routine since our caller will try to
28492 increment the returned register via an "la" instruction. */
28495 find_addr_reg (rtx addr)
28497 while (GET_CODE (addr) == PLUS)
28499 if (GET_CODE (XEXP (addr, 0)) == REG
28500 && REGNO (XEXP (addr, 0)) != 0)
28501 addr = XEXP (addr, 0);
28502 else if (GET_CODE (XEXP (addr, 1)) == REG
28503 && REGNO (XEXP (addr, 1)) != 0)
28504 addr = XEXP (addr, 1);
28505 else if (CONSTANT_P (XEXP (addr, 0)))
28506 addr = XEXP (addr, 1);
28507 else if (CONSTANT_P (XEXP (addr, 1)))
28508 addr = XEXP (addr, 0);
28509 else
28510 gcc_unreachable ();
28512 gcc_assert (GET_CODE (addr) == REG && REGNO (addr) != 0);
28513 return addr;
28516 void
28517 rs6000_fatal_bad_address (rtx op)
28519 fatal_insn ("bad address", op);
28522 #if TARGET_MACHO
28524 typedef struct branch_island_d {
28525 tree function_name;
28526 tree label_name;
28527 int line_number;
28528 } branch_island;
28531 static vec<branch_island, va_gc> *branch_islands;
28533 /* Remember to generate a branch island for far calls to the given
28534 function. */
28536 static void
28537 add_compiler_branch_island (tree label_name, tree function_name,
28538 int line_number)
28540 branch_island bi = {function_name, label_name, line_number};
28541 vec_safe_push (branch_islands, bi);
28544 /* Generate far-jump branch islands for everything recorded in
28545 branch_islands. Invoked immediately after the last instruction of
28546 the epilogue has been emitted; the branch islands must be appended
28547 to, and contiguous with, the function body. Mach-O stubs are
28548 generated in machopic_output_stub(). */
28550 static void
28551 macho_branch_islands (void)
28553 char tmp_buf[512];
28555 while (!vec_safe_is_empty (branch_islands))
28557 branch_island *bi = &branch_islands->last ();
28558 const char *label = IDENTIFIER_POINTER (bi->label_name);
28559 const char *name = IDENTIFIER_POINTER (bi->function_name);
28560 char name_buf[512];
28561 /* Cheap copy of the details from the Darwin ASM_OUTPUT_LABELREF(). */
28562 if (name[0] == '*' || name[0] == '&')
28563 strcpy (name_buf, name+1);
28564 else
28566 name_buf[0] = '_';
28567 strcpy (name_buf+1, name);
28569 strcpy (tmp_buf, "\n");
28570 strcat (tmp_buf, label);
28571 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
28572 if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
28573 dbxout_stabd (N_SLINE, bi->line_number);
28574 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
28575 if (flag_pic)
28577 if (TARGET_LINK_STACK)
28579 char name[32];
28580 get_ppc476_thunk_name (name);
28581 strcat (tmp_buf, ":\n\tmflr r0\n\tbl ");
28582 strcat (tmp_buf, name);
28583 strcat (tmp_buf, "\n");
28584 strcat (tmp_buf, label);
28585 strcat (tmp_buf, "_pic:\n\tmflr r11\n");
28587 else
28589 strcat (tmp_buf, ":\n\tmflr r0\n\tbcl 20,31,");
28590 strcat (tmp_buf, label);
28591 strcat (tmp_buf, "_pic\n");
28592 strcat (tmp_buf, label);
28593 strcat (tmp_buf, "_pic:\n\tmflr r11\n");
28596 strcat (tmp_buf, "\taddis r11,r11,ha16(");
28597 strcat (tmp_buf, name_buf);
28598 strcat (tmp_buf, " - ");
28599 strcat (tmp_buf, label);
28600 strcat (tmp_buf, "_pic)\n");
28602 strcat (tmp_buf, "\tmtlr r0\n");
28604 strcat (tmp_buf, "\taddi r12,r11,lo16(");
28605 strcat (tmp_buf, name_buf);
28606 strcat (tmp_buf, " - ");
28607 strcat (tmp_buf, label);
28608 strcat (tmp_buf, "_pic)\n");
28610 strcat (tmp_buf, "\tmtctr r12\n\tbctr\n");
28612 else
28614 strcat (tmp_buf, ":\nlis r12,hi16(");
28615 strcat (tmp_buf, name_buf);
28616 strcat (tmp_buf, ")\n\tori r12,r12,lo16(");
28617 strcat (tmp_buf, name_buf);
28618 strcat (tmp_buf, ")\n\tmtctr r12\n\tbctr");
28620 output_asm_insn (tmp_buf, 0);
28621 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
28622 if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
28623 dbxout_stabd (N_SLINE, bi->line_number);
28624 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
28625 branch_islands->pop ();
28629 /* NO_PREVIOUS_DEF checks in the link list whether the function name is
28630 already there or not. */
28632 static int
28633 no_previous_def (tree function_name)
28635 branch_island *bi;
28636 unsigned ix;
28638 FOR_EACH_VEC_SAFE_ELT (branch_islands, ix, bi)
28639 if (function_name == bi->function_name)
28640 return 0;
28641 return 1;
28644 /* GET_PREV_LABEL gets the label name from the previous definition of
28645 the function. */
28647 static tree
28648 get_prev_label (tree function_name)
28650 branch_island *bi;
28651 unsigned ix;
28653 FOR_EACH_VEC_SAFE_ELT (branch_islands, ix, bi)
28654 if (function_name == bi->function_name)
28655 return bi->label_name;
28656 return NULL_TREE;
28659 /* INSN is either a function call or a millicode call. It may have an
28660 unconditional jump in its delay slot.
28662 CALL_DEST is the routine we are calling. */
28664 char *
28665 output_call (rtx insn, rtx *operands, int dest_operand_number,
28666 int cookie_operand_number)
28668 static char buf[256];
28669 if (darwin_emit_branch_islands
28670 && GET_CODE (operands[dest_operand_number]) == SYMBOL_REF
28671 && (INTVAL (operands[cookie_operand_number]) & CALL_LONG))
28673 tree labelname;
28674 tree funname = get_identifier (XSTR (operands[dest_operand_number], 0));
28676 if (no_previous_def (funname))
28678 rtx label_rtx = gen_label_rtx ();
28679 char *label_buf, temp_buf[256];
28680 ASM_GENERATE_INTERNAL_LABEL (temp_buf, "L",
28681 CODE_LABEL_NUMBER (label_rtx));
28682 label_buf = temp_buf[0] == '*' ? temp_buf + 1 : temp_buf;
28683 labelname = get_identifier (label_buf);
28684 add_compiler_branch_island (labelname, funname, insn_line (insn));
28686 else
28687 labelname = get_prev_label (funname);
28689 /* "jbsr foo, L42" is Mach-O for "Link as 'bl foo' if a 'bl'
28690 instruction will reach 'foo', otherwise link as 'bl L42'".
28691 "L42" should be a 'branch island', that will do a far jump to
28692 'foo'. Branch islands are generated in
28693 macho_branch_islands(). */
28694 sprintf (buf, "jbsr %%z%d,%.246s",
28695 dest_operand_number, IDENTIFIER_POINTER (labelname));
28697 else
28698 sprintf (buf, "bl %%z%d", dest_operand_number);
28699 return buf;
28702 /* Generate PIC and indirect symbol stubs. */
28704 void
28705 machopic_output_stub (FILE *file, const char *symb, const char *stub)
28707 unsigned int length;
28708 char *symbol_name, *lazy_ptr_name;
28709 char *local_label_0;
28710 static int label = 0;
28712 /* Lose our funky encoding stuff so it doesn't contaminate the stub. */
28713 symb = (*targetm.strip_name_encoding) (symb);
28716 length = strlen (symb);
28717 symbol_name = XALLOCAVEC (char, length + 32);
28718 GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
28720 lazy_ptr_name = XALLOCAVEC (char, length + 32);
28721 GEN_LAZY_PTR_NAME_FOR_SYMBOL (lazy_ptr_name, symb, length);
28723 if (flag_pic == 2)
28724 switch_to_section (darwin_sections[machopic_picsymbol_stub1_section]);
28725 else
28726 switch_to_section (darwin_sections[machopic_symbol_stub1_section]);
28728 if (flag_pic == 2)
28730 fprintf (file, "\t.align 5\n");
28732 fprintf (file, "%s:\n", stub);
28733 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
28735 label++;
28736 local_label_0 = XALLOCAVEC (char, sizeof ("\"L00000000000$spb\""));
28737 sprintf (local_label_0, "\"L%011d$spb\"", label);
28739 fprintf (file, "\tmflr r0\n");
28740 if (TARGET_LINK_STACK)
28742 char name[32];
28743 get_ppc476_thunk_name (name);
28744 fprintf (file, "\tbl %s\n", name);
28745 fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
28747 else
28749 fprintf (file, "\tbcl 20,31,%s\n", local_label_0);
28750 fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
28752 fprintf (file, "\taddis r11,r11,ha16(%s-%s)\n",
28753 lazy_ptr_name, local_label_0);
28754 fprintf (file, "\tmtlr r0\n");
28755 fprintf (file, "\t%s r12,lo16(%s-%s)(r11)\n",
28756 (TARGET_64BIT ? "ldu" : "lwzu"),
28757 lazy_ptr_name, local_label_0);
28758 fprintf (file, "\tmtctr r12\n");
28759 fprintf (file, "\tbctr\n");
28761 else
28763 fprintf (file, "\t.align 4\n");
28765 fprintf (file, "%s:\n", stub);
28766 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
28768 fprintf (file, "\tlis r11,ha16(%s)\n", lazy_ptr_name);
28769 fprintf (file, "\t%s r12,lo16(%s)(r11)\n",
28770 (TARGET_64BIT ? "ldu" : "lwzu"),
28771 lazy_ptr_name);
28772 fprintf (file, "\tmtctr r12\n");
28773 fprintf (file, "\tbctr\n");
28776 switch_to_section (darwin_sections[machopic_lazy_symbol_ptr_section]);
28777 fprintf (file, "%s:\n", lazy_ptr_name);
28778 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
28779 fprintf (file, "%sdyld_stub_binding_helper\n",
28780 (TARGET_64BIT ? DOUBLE_INT_ASM_OP : "\t.long\t"));
28783 /* Legitimize PIC addresses. If the address is already
28784 position-independent, we return ORIG. Newly generated
28785 position-independent addresses go into a reg. This is REG if non
28786 zero, otherwise we allocate register(s) as necessary. */
28788 #define SMALL_INT(X) ((UINTVAL (X) + 0x8000) < 0x10000)
28791 rs6000_machopic_legitimize_pic_address (rtx orig, enum machine_mode mode,
28792 rtx reg)
28794 rtx base, offset;
28796 if (reg == NULL && ! reload_in_progress && ! reload_completed)
28797 reg = gen_reg_rtx (Pmode);
28799 if (GET_CODE (orig) == CONST)
28801 rtx reg_temp;
28803 if (GET_CODE (XEXP (orig, 0)) == PLUS
28804 && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
28805 return orig;
28807 gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
28809 /* Use a different reg for the intermediate value, as
28810 it will be marked UNCHANGING. */
28811 reg_temp = !can_create_pseudo_p () ? reg : gen_reg_rtx (Pmode);
28812 base = rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 0),
28813 Pmode, reg_temp);
28814 offset =
28815 rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 1),
28816 Pmode, reg);
28818 if (GET_CODE (offset) == CONST_INT)
28820 if (SMALL_INT (offset))
28821 return plus_constant (Pmode, base, INTVAL (offset));
28822 else if (! reload_in_progress && ! reload_completed)
28823 offset = force_reg (Pmode, offset);
28824 else
28826 rtx mem = force_const_mem (Pmode, orig);
28827 return machopic_legitimize_pic_address (mem, Pmode, reg);
28830 return gen_rtx_PLUS (Pmode, base, offset);
28833 /* Fall back on generic machopic code. */
28834 return machopic_legitimize_pic_address (orig, mode, reg);
28837 /* Output a .machine directive for the Darwin assembler, and call
28838 the generic start_file routine. */
28840 static void
28841 rs6000_darwin_file_start (void)
28843 static const struct
28845 const char *arg;
28846 const char *name;
28847 HOST_WIDE_INT if_set;
28848 } mapping[] = {
28849 { "ppc64", "ppc64", MASK_64BIT },
28850 { "970", "ppc970", MASK_PPC_GPOPT | MASK_MFCRF | MASK_POWERPC64 },
28851 { "power4", "ppc970", 0 },
28852 { "G5", "ppc970", 0 },
28853 { "7450", "ppc7450", 0 },
28854 { "7400", "ppc7400", MASK_ALTIVEC },
28855 { "G4", "ppc7400", 0 },
28856 { "750", "ppc750", 0 },
28857 { "740", "ppc750", 0 },
28858 { "G3", "ppc750", 0 },
28859 { "604e", "ppc604e", 0 },
28860 { "604", "ppc604", 0 },
28861 { "603e", "ppc603", 0 },
28862 { "603", "ppc603", 0 },
28863 { "601", "ppc601", 0 },
28864 { NULL, "ppc", 0 } };
28865 const char *cpu_id = "";
28866 size_t i;
28868 rs6000_file_start ();
28869 darwin_file_start ();
28871 /* Determine the argument to -mcpu=. Default to G3 if not specified. */
28873 if (rs6000_default_cpu != 0 && rs6000_default_cpu[0] != '\0')
28874 cpu_id = rs6000_default_cpu;
28876 if (global_options_set.x_rs6000_cpu_index)
28877 cpu_id = processor_target_table[rs6000_cpu_index].name;
28879 /* Look through the mapping array. Pick the first name that either
28880 matches the argument, has a bit set in IF_SET that is also set
28881 in the target flags, or has a NULL name. */
28883 i = 0;
28884 while (mapping[i].arg != NULL
28885 && strcmp (mapping[i].arg, cpu_id) != 0
28886 && (mapping[i].if_set & rs6000_isa_flags) == 0)
28887 i++;
28889 fprintf (asm_out_file, "\t.machine %s\n", mapping[i].name);
28892 #endif /* TARGET_MACHO */
28894 #if TARGET_ELF
28895 static int
28896 rs6000_elf_reloc_rw_mask (void)
28898 if (flag_pic)
28899 return 3;
28900 else if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
28901 return 2;
28902 else
28903 return 0;
28906 /* Record an element in the table of global constructors. SYMBOL is
28907 a SYMBOL_REF of the function to be called; PRIORITY is a number
28908 between 0 and MAX_INIT_PRIORITY.
28910 This differs from default_named_section_asm_out_constructor in
28911 that we have special handling for -mrelocatable. */
28913 static void rs6000_elf_asm_out_constructor (rtx, int) ATTRIBUTE_UNUSED;
28914 static void
28915 rs6000_elf_asm_out_constructor (rtx symbol, int priority)
28917 const char *section = ".ctors";
28918 char buf[16];
28920 if (priority != DEFAULT_INIT_PRIORITY)
28922 sprintf (buf, ".ctors.%.5u",
28923 /* Invert the numbering so the linker puts us in the proper
28924 order; constructors are run from right to left, and the
28925 linker sorts in increasing order. */
28926 MAX_INIT_PRIORITY - priority);
28927 section = buf;
28930 switch_to_section (get_section (section, SECTION_WRITE, NULL));
28931 assemble_align (POINTER_SIZE);
28933 if (TARGET_RELOCATABLE)
28935 fputs ("\t.long (", asm_out_file);
28936 output_addr_const (asm_out_file, symbol);
28937 fputs (")@fixup\n", asm_out_file);
28939 else
28940 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
28943 static void rs6000_elf_asm_out_destructor (rtx, int) ATTRIBUTE_UNUSED;
28944 static void
28945 rs6000_elf_asm_out_destructor (rtx symbol, int priority)
28947 const char *section = ".dtors";
28948 char buf[16];
28950 if (priority != DEFAULT_INIT_PRIORITY)
28952 sprintf (buf, ".dtors.%.5u",
28953 /* Invert the numbering so the linker puts us in the proper
28954 order; constructors are run from right to left, and the
28955 linker sorts in increasing order. */
28956 MAX_INIT_PRIORITY - priority);
28957 section = buf;
28960 switch_to_section (get_section (section, SECTION_WRITE, NULL));
28961 assemble_align (POINTER_SIZE);
28963 if (TARGET_RELOCATABLE)
28965 fputs ("\t.long (", asm_out_file);
28966 output_addr_const (asm_out_file, symbol);
28967 fputs (")@fixup\n", asm_out_file);
28969 else
28970 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
28973 void
28974 rs6000_elf_declare_function_name (FILE *file, const char *name, tree decl)
28976 if (TARGET_64BIT && DEFAULT_ABI != ABI_ELFv2)
28978 fputs ("\t.section\t\".opd\",\"aw\"\n\t.align 3\n", file);
28979 ASM_OUTPUT_LABEL (file, name);
28980 fputs (DOUBLE_INT_ASM_OP, file);
28981 rs6000_output_function_entry (file, name);
28982 fputs (",.TOC.@tocbase,0\n\t.previous\n", file);
28983 if (DOT_SYMBOLS)
28985 fputs ("\t.size\t", file);
28986 assemble_name (file, name);
28987 fputs (",24\n\t.type\t.", file);
28988 assemble_name (file, name);
28989 fputs (",@function\n", file);
28990 if (TREE_PUBLIC (decl) && ! DECL_WEAK (decl))
28992 fputs ("\t.globl\t.", file);
28993 assemble_name (file, name);
28994 putc ('\n', file);
28997 else
28998 ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
28999 ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
29000 rs6000_output_function_entry (file, name);
29001 fputs (":\n", file);
29002 return;
29005 if (TARGET_RELOCATABLE
29006 && !TARGET_SECURE_PLT
29007 && (get_pool_size () != 0 || crtl->profile)
29008 && uses_TOC ())
29010 char buf[256];
29012 (*targetm.asm_out.internal_label) (file, "LCL", rs6000_pic_labelno);
29014 ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
29015 fprintf (file, "\t.long ");
29016 assemble_name (file, buf);
29017 putc ('-', file);
29018 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
29019 assemble_name (file, buf);
29020 putc ('\n', file);
29023 ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
29024 ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
29026 if (DEFAULT_ABI == ABI_AIX)
29028 const char *desc_name, *orig_name;
29030 orig_name = (*targetm.strip_name_encoding) (name);
29031 desc_name = orig_name;
29032 while (*desc_name == '.')
29033 desc_name++;
29035 if (TREE_PUBLIC (decl))
29036 fprintf (file, "\t.globl %s\n", desc_name);
29038 fprintf (file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
29039 fprintf (file, "%s:\n", desc_name);
29040 fprintf (file, "\t.long %s\n", orig_name);
29041 fputs ("\t.long _GLOBAL_OFFSET_TABLE_\n", file);
29042 fputs ("\t.long 0\n", file);
29043 fprintf (file, "\t.previous\n");
29045 ASM_OUTPUT_LABEL (file, name);
29048 static void rs6000_elf_file_end (void) ATTRIBUTE_UNUSED;
29049 static void
29050 rs6000_elf_file_end (void)
29052 #ifdef HAVE_AS_GNU_ATTRIBUTE
29053 if (TARGET_32BIT && DEFAULT_ABI == ABI_V4)
29055 if (rs6000_passes_float)
29056 fprintf (asm_out_file, "\t.gnu_attribute 4, %d\n",
29057 ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT) ? 1
29058 : (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_SINGLE_FLOAT) ? 3
29059 : 2));
29060 if (rs6000_passes_vector)
29061 fprintf (asm_out_file, "\t.gnu_attribute 8, %d\n",
29062 (TARGET_ALTIVEC_ABI ? 2
29063 : TARGET_SPE_ABI ? 3
29064 : 1));
29065 if (rs6000_returns_struct)
29066 fprintf (asm_out_file, "\t.gnu_attribute 12, %d\n",
29067 aix_struct_return ? 2 : 1);
29069 #endif
29070 #if defined (POWERPC_LINUX) || defined (POWERPC_FREEBSD)
29071 if (TARGET_32BIT || DEFAULT_ABI == ABI_ELFv2)
29072 file_end_indicate_exec_stack ();
29073 #endif
29075 #endif
29077 #if TARGET_XCOFF
29078 static void
29079 rs6000_xcoff_asm_output_anchor (rtx symbol)
29081 char buffer[100];
29083 sprintf (buffer, "$ + " HOST_WIDE_INT_PRINT_DEC,
29084 SYMBOL_REF_BLOCK_OFFSET (symbol));
29085 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
29088 static void
29089 rs6000_xcoff_asm_globalize_label (FILE *stream, const char *name)
29091 fputs (GLOBAL_ASM_OP, stream);
29092 RS6000_OUTPUT_BASENAME (stream, name);
29093 putc ('\n', stream);
29096 /* A get_unnamed_decl callback, used for read-only sections. PTR
29097 points to the section string variable. */
29099 static void
29100 rs6000_xcoff_output_readonly_section_asm_op (const void *directive)
29102 fprintf (asm_out_file, "\t.csect %s[RO],%s\n",
29103 *(const char *const *) directive,
29104 XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
29107 /* Likewise for read-write sections. */
29109 static void
29110 rs6000_xcoff_output_readwrite_section_asm_op (const void *directive)
29112 fprintf (asm_out_file, "\t.csect %s[RW],%s\n",
29113 *(const char *const *) directive,
29114 XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
29117 static void
29118 rs6000_xcoff_output_tls_section_asm_op (const void *directive)
29120 fprintf (asm_out_file, "\t.csect %s[TL],%s\n",
29121 *(const char *const *) directive,
29122 XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
29125 /* A get_unnamed_section callback, used for switching to toc_section. */
29127 static void
29128 rs6000_xcoff_output_toc_section_asm_op (const void *data ATTRIBUTE_UNUSED)
29130 if (TARGET_MINIMAL_TOC)
29132 /* toc_section is always selected at least once from
29133 rs6000_xcoff_file_start, so this is guaranteed to
29134 always be defined once and only once in each file. */
29135 if (!toc_initialized)
29137 fputs ("\t.toc\nLCTOC..1:\n", asm_out_file);
29138 fputs ("\t.tc toc_table[TC],toc_table[RW]\n", asm_out_file);
29139 toc_initialized = 1;
29141 fprintf (asm_out_file, "\t.csect toc_table[RW]%s\n",
29142 (TARGET_32BIT ? "" : ",3"));
29144 else
29145 fputs ("\t.toc\n", asm_out_file);
29148 /* Implement TARGET_ASM_INIT_SECTIONS. */
29150 static void
29151 rs6000_xcoff_asm_init_sections (void)
29153 read_only_data_section
29154 = get_unnamed_section (0, rs6000_xcoff_output_readonly_section_asm_op,
29155 &xcoff_read_only_section_name);
29157 private_data_section
29158 = get_unnamed_section (SECTION_WRITE,
29159 rs6000_xcoff_output_readwrite_section_asm_op,
29160 &xcoff_private_data_section_name);
29162 tls_data_section
29163 = get_unnamed_section (SECTION_TLS,
29164 rs6000_xcoff_output_tls_section_asm_op,
29165 &xcoff_tls_data_section_name);
29167 tls_private_data_section
29168 = get_unnamed_section (SECTION_TLS,
29169 rs6000_xcoff_output_tls_section_asm_op,
29170 &xcoff_private_data_section_name);
29172 read_only_private_data_section
29173 = get_unnamed_section (0, rs6000_xcoff_output_readonly_section_asm_op,
29174 &xcoff_private_data_section_name);
29176 toc_section
29177 = get_unnamed_section (0, rs6000_xcoff_output_toc_section_asm_op, NULL);
29179 readonly_data_section = read_only_data_section;
29180 exception_section = data_section;
29183 static int
29184 rs6000_xcoff_reloc_rw_mask (void)
29186 return 3;
29189 static void
29190 rs6000_xcoff_asm_named_section (const char *name, unsigned int flags,
29191 tree decl ATTRIBUTE_UNUSED)
29193 int smclass;
29194 static const char * const suffix[4] = { "PR", "RO", "RW", "TL" };
29196 if (flags & SECTION_CODE)
29197 smclass = 0;
29198 else if (flags & SECTION_TLS)
29199 smclass = 3;
29200 else if (flags & SECTION_WRITE)
29201 smclass = 2;
29202 else
29203 smclass = 1;
29205 fprintf (asm_out_file, "\t.csect %s%s[%s],%u\n",
29206 (flags & SECTION_CODE) ? "." : "",
29207 name, suffix[smclass], flags & SECTION_ENTSIZE);
29210 #define IN_NAMED_SECTION(DECL) \
29211 ((TREE_CODE (DECL) == FUNCTION_DECL || TREE_CODE (DECL) == VAR_DECL) \
29212 && DECL_SECTION_NAME (DECL) != NULL_TREE)
29214 static section *
29215 rs6000_xcoff_select_section (tree decl, int reloc,
29216 unsigned HOST_WIDE_INT align)
29218 /* Place variables with alignment stricter than BIGGEST_ALIGNMENT into
29219 named section. */
29220 if (align > BIGGEST_ALIGNMENT)
29222 resolve_unique_section (decl, reloc, true);
29223 if (IN_NAMED_SECTION (decl))
29224 return get_named_section (decl, NULL, reloc);
29227 if (decl_readonly_section (decl, reloc))
29229 if (TREE_PUBLIC (decl))
29230 return read_only_data_section;
29231 else
29232 return read_only_private_data_section;
29234 else
29236 #if HAVE_AS_TLS
29237 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
29239 if (TREE_PUBLIC (decl))
29240 return tls_data_section;
29241 else if (bss_initializer_p (decl))
29243 /* Convert to COMMON to emit in BSS. */
29244 DECL_COMMON (decl) = 1;
29245 return tls_comm_section;
29247 else
29248 return tls_private_data_section;
29250 else
29251 #endif
29252 if (TREE_PUBLIC (decl))
29253 return data_section;
29254 else
29255 return private_data_section;
29259 static void
29260 rs6000_xcoff_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED)
29262 const char *name;
29264 /* Use select_section for private data and uninitialized data with
29265 alignment <= BIGGEST_ALIGNMENT. */
29266 if (!TREE_PUBLIC (decl)
29267 || DECL_COMMON (decl)
29268 || (DECL_INITIAL (decl) == NULL_TREE
29269 && DECL_ALIGN (decl) <= BIGGEST_ALIGNMENT)
29270 || DECL_INITIAL (decl) == error_mark_node
29271 || (flag_zero_initialized_in_bss
29272 && initializer_zerop (DECL_INITIAL (decl))))
29273 return;
29275 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
29276 name = (*targetm.strip_name_encoding) (name);
29277 DECL_SECTION_NAME (decl) = build_string (strlen (name), name);
29280 /* Select section for constant in constant pool.
29282 On RS/6000, all constants are in the private read-only data area.
29283 However, if this is being placed in the TOC it must be output as a
29284 toc entry. */
29286 static section *
29287 rs6000_xcoff_select_rtx_section (enum machine_mode mode, rtx x,
29288 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
29290 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
29291 return toc_section;
29292 else
29293 return read_only_private_data_section;
29296 /* Remove any trailing [DS] or the like from the symbol name. */
29298 static const char *
29299 rs6000_xcoff_strip_name_encoding (const char *name)
29301 size_t len;
29302 if (*name == '*')
29303 name++;
29304 len = strlen (name);
29305 if (name[len - 1] == ']')
29306 return ggc_alloc_string (name, len - 4);
29307 else
29308 return name;
29311 /* Section attributes. AIX is always PIC. */
29313 static unsigned int
29314 rs6000_xcoff_section_type_flags (tree decl, const char *name, int reloc)
29316 unsigned int align;
29317 unsigned int flags = default_section_type_flags (decl, name, reloc);
29319 /* Align to at least UNIT size. */
29320 if ((flags & SECTION_CODE) != 0 || !decl || !DECL_P (decl))
29321 align = MIN_UNITS_PER_WORD;
29322 else
29323 /* Increase alignment of large objects if not already stricter. */
29324 align = MAX ((DECL_ALIGN (decl) / BITS_PER_UNIT),
29325 int_size_in_bytes (TREE_TYPE (decl)) > MIN_UNITS_PER_WORD
29326 ? UNITS_PER_FP_WORD : MIN_UNITS_PER_WORD);
29328 return flags | (exact_log2 (align) & SECTION_ENTSIZE);
29331 /* Output at beginning of assembler file.
29333 Initialize the section names for the RS/6000 at this point.
29335 Specify filename, including full path, to assembler.
29337 We want to go into the TOC section so at least one .toc will be emitted.
29338 Also, in order to output proper .bs/.es pairs, we need at least one static
29339 [RW] section emitted.
29341 Finally, declare mcount when profiling to make the assembler happy. */
29343 static void
29344 rs6000_xcoff_file_start (void)
29346 rs6000_gen_section_name (&xcoff_bss_section_name,
29347 main_input_filename, ".bss_");
29348 rs6000_gen_section_name (&xcoff_private_data_section_name,
29349 main_input_filename, ".rw_");
29350 rs6000_gen_section_name (&xcoff_read_only_section_name,
29351 main_input_filename, ".ro_");
29352 rs6000_gen_section_name (&xcoff_tls_data_section_name,
29353 main_input_filename, ".tls_");
29354 rs6000_gen_section_name (&xcoff_tbss_section_name,
29355 main_input_filename, ".tbss_[UL]");
29357 fputs ("\t.file\t", asm_out_file);
29358 output_quoted_string (asm_out_file, main_input_filename);
29359 fputc ('\n', asm_out_file);
29360 if (write_symbols != NO_DEBUG)
29361 switch_to_section (private_data_section);
29362 switch_to_section (text_section);
29363 if (profile_flag)
29364 fprintf (asm_out_file, "\t.extern %s\n", RS6000_MCOUNT);
29365 rs6000_file_start ();
29368 /* Output at end of assembler file.
29369 On the RS/6000, referencing data should automatically pull in text. */
29371 static void
29372 rs6000_xcoff_file_end (void)
29374 switch_to_section (text_section);
29375 fputs ("_section_.text:\n", asm_out_file);
29376 switch_to_section (data_section);
29377 fputs (TARGET_32BIT
29378 ? "\t.long _section_.text\n" : "\t.llong _section_.text\n",
29379 asm_out_file);
29382 #ifdef HAVE_AS_TLS
29383 static void
29384 rs6000_xcoff_encode_section_info (tree decl, rtx rtl, int first)
29386 rtx symbol;
29387 int flags;
29389 default_encode_section_info (decl, rtl, first);
29391 /* Careful not to prod global register variables. */
29392 if (!MEM_P (rtl))
29393 return;
29394 symbol = XEXP (rtl, 0);
29395 if (GET_CODE (symbol) != SYMBOL_REF)
29396 return;
29398 flags = SYMBOL_REF_FLAGS (symbol);
29400 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
29401 flags &= ~SYMBOL_FLAG_HAS_BLOCK_INFO;
29403 SYMBOL_REF_FLAGS (symbol) = flags;
29405 #endif /* HAVE_AS_TLS */
29406 #endif /* TARGET_XCOFF */
29408 /* Compute a (partial) cost for rtx X. Return true if the complete
29409 cost has been computed, and false if subexpressions should be
29410 scanned. In either case, *TOTAL contains the cost result. */
29412 static bool
29413 rs6000_rtx_costs (rtx x, int code, int outer_code, int opno ATTRIBUTE_UNUSED,
29414 int *total, bool speed)
29416 enum machine_mode mode = GET_MODE (x);
29418 switch (code)
29420 /* On the RS/6000, if it is valid in the insn, it is free. */
29421 case CONST_INT:
29422 if (((outer_code == SET
29423 || outer_code == PLUS
29424 || outer_code == MINUS)
29425 && (satisfies_constraint_I (x)
29426 || satisfies_constraint_L (x)))
29427 || (outer_code == AND
29428 && (satisfies_constraint_K (x)
29429 || (mode == SImode
29430 ? satisfies_constraint_L (x)
29431 : satisfies_constraint_J (x))
29432 || mask_operand (x, mode)
29433 || (mode == DImode
29434 && mask64_operand (x, DImode))))
29435 || ((outer_code == IOR || outer_code == XOR)
29436 && (satisfies_constraint_K (x)
29437 || (mode == SImode
29438 ? satisfies_constraint_L (x)
29439 : satisfies_constraint_J (x))))
29440 || outer_code == ASHIFT
29441 || outer_code == ASHIFTRT
29442 || outer_code == LSHIFTRT
29443 || outer_code == ROTATE
29444 || outer_code == ROTATERT
29445 || outer_code == ZERO_EXTRACT
29446 || (outer_code == MULT
29447 && satisfies_constraint_I (x))
29448 || ((outer_code == DIV || outer_code == UDIV
29449 || outer_code == MOD || outer_code == UMOD)
29450 && exact_log2 (INTVAL (x)) >= 0)
29451 || (outer_code == COMPARE
29452 && (satisfies_constraint_I (x)
29453 || satisfies_constraint_K (x)))
29454 || ((outer_code == EQ || outer_code == NE)
29455 && (satisfies_constraint_I (x)
29456 || satisfies_constraint_K (x)
29457 || (mode == SImode
29458 ? satisfies_constraint_L (x)
29459 : satisfies_constraint_J (x))))
29460 || (outer_code == GTU
29461 && satisfies_constraint_I (x))
29462 || (outer_code == LTU
29463 && satisfies_constraint_P (x)))
29465 *total = 0;
29466 return true;
29468 else if ((outer_code == PLUS
29469 && reg_or_add_cint_operand (x, VOIDmode))
29470 || (outer_code == MINUS
29471 && reg_or_sub_cint_operand (x, VOIDmode))
29472 || ((outer_code == SET
29473 || outer_code == IOR
29474 || outer_code == XOR)
29475 && (INTVAL (x)
29476 & ~ (unsigned HOST_WIDE_INT) 0xffffffff) == 0))
29478 *total = COSTS_N_INSNS (1);
29479 return true;
29481 /* FALLTHRU */
29483 case CONST_DOUBLE:
29484 case CONST_WIDE_INT:
29485 case CONST:
29486 case HIGH:
29487 case SYMBOL_REF:
29488 case MEM:
29489 /* When optimizing for size, MEM should be slightly more expensive
29490 than generating address, e.g., (plus (reg) (const)).
29491 L1 cache latency is about two instructions. */
29492 *total = !speed ? COSTS_N_INSNS (1) + 1 : COSTS_N_INSNS (2);
29493 return true;
29495 case LABEL_REF:
29496 *total = 0;
29497 return true;
29499 case PLUS:
29500 case MINUS:
29501 if (FLOAT_MODE_P (mode))
29502 *total = rs6000_cost->fp;
29503 else
29504 *total = COSTS_N_INSNS (1);
29505 return false;
29507 case MULT:
29508 if (GET_CODE (XEXP (x, 1)) == CONST_INT
29509 && satisfies_constraint_I (XEXP (x, 1)))
29511 if (INTVAL (XEXP (x, 1)) >= -256
29512 && INTVAL (XEXP (x, 1)) <= 255)
29513 *total = rs6000_cost->mulsi_const9;
29514 else
29515 *total = rs6000_cost->mulsi_const;
29517 else if (mode == SFmode)
29518 *total = rs6000_cost->fp;
29519 else if (FLOAT_MODE_P (mode))
29520 *total = rs6000_cost->dmul;
29521 else if (mode == DImode)
29522 *total = rs6000_cost->muldi;
29523 else
29524 *total = rs6000_cost->mulsi;
29525 return false;
29527 case FMA:
29528 if (mode == SFmode)
29529 *total = rs6000_cost->fp;
29530 else
29531 *total = rs6000_cost->dmul;
29532 break;
29534 case DIV:
29535 case MOD:
29536 if (FLOAT_MODE_P (mode))
29538 *total = mode == DFmode ? rs6000_cost->ddiv
29539 : rs6000_cost->sdiv;
29540 return false;
29542 /* FALLTHRU */
29544 case UDIV:
29545 case UMOD:
29546 if (GET_CODE (XEXP (x, 1)) == CONST_INT
29547 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0)
29549 if (code == DIV || code == MOD)
29550 /* Shift, addze */
29551 *total = COSTS_N_INSNS (2);
29552 else
29553 /* Shift */
29554 *total = COSTS_N_INSNS (1);
29556 else
29558 if (GET_MODE (XEXP (x, 1)) == DImode)
29559 *total = rs6000_cost->divdi;
29560 else
29561 *total = rs6000_cost->divsi;
29563 /* Add in shift and subtract for MOD. */
29564 if (code == MOD || code == UMOD)
29565 *total += COSTS_N_INSNS (2);
29566 return false;
29568 case CTZ:
29569 case FFS:
29570 *total = COSTS_N_INSNS (4);
29571 return false;
29573 case POPCOUNT:
29574 *total = COSTS_N_INSNS (TARGET_POPCNTD ? 1 : 6);
29575 return false;
29577 case PARITY:
29578 *total = COSTS_N_INSNS (TARGET_CMPB ? 2 : 6);
29579 return false;
29581 case NOT:
29582 if (outer_code == AND || outer_code == IOR || outer_code == XOR)
29584 *total = 0;
29585 return false;
29587 /* FALLTHRU */
29589 case AND:
29590 case CLZ:
29591 case IOR:
29592 case XOR:
29593 case ZERO_EXTRACT:
29594 *total = COSTS_N_INSNS (1);
29595 return false;
29597 case ASHIFT:
29598 case ASHIFTRT:
29599 case LSHIFTRT:
29600 case ROTATE:
29601 case ROTATERT:
29602 /* Handle mul_highpart. */
29603 if (outer_code == TRUNCATE
29604 && GET_CODE (XEXP (x, 0)) == MULT)
29606 if (mode == DImode)
29607 *total = rs6000_cost->muldi;
29608 else
29609 *total = rs6000_cost->mulsi;
29610 return true;
29612 else if (outer_code == AND)
29613 *total = 0;
29614 else
29615 *total = COSTS_N_INSNS (1);
29616 return false;
29618 case SIGN_EXTEND:
29619 case ZERO_EXTEND:
29620 if (GET_CODE (XEXP (x, 0)) == MEM)
29621 *total = 0;
29622 else
29623 *total = COSTS_N_INSNS (1);
29624 return false;
29626 case COMPARE:
29627 case NEG:
29628 case ABS:
29629 if (!FLOAT_MODE_P (mode))
29631 *total = COSTS_N_INSNS (1);
29632 return false;
29634 /* FALLTHRU */
29636 case FLOAT:
29637 case UNSIGNED_FLOAT:
29638 case FIX:
29639 case UNSIGNED_FIX:
29640 case FLOAT_TRUNCATE:
29641 *total = rs6000_cost->fp;
29642 return false;
29644 case FLOAT_EXTEND:
29645 if (mode == DFmode)
29646 *total = 0;
29647 else
29648 *total = rs6000_cost->fp;
29649 return false;
29651 case UNSPEC:
29652 switch (XINT (x, 1))
29654 case UNSPEC_FRSP:
29655 *total = rs6000_cost->fp;
29656 return true;
29658 default:
29659 break;
29661 break;
29663 case CALL:
29664 case IF_THEN_ELSE:
29665 if (!speed)
29667 *total = COSTS_N_INSNS (1);
29668 return true;
29670 else if (FLOAT_MODE_P (mode)
29671 && TARGET_PPC_GFXOPT && TARGET_HARD_FLOAT && TARGET_FPRS)
29673 *total = rs6000_cost->fp;
29674 return false;
29676 break;
29678 case EQ:
29679 case GTU:
29680 case LTU:
29681 /* Carry bit requires mode == Pmode.
29682 NEG or PLUS already counted so only add one. */
29683 if (mode == Pmode
29684 && (outer_code == NEG || outer_code == PLUS))
29686 *total = COSTS_N_INSNS (1);
29687 return true;
29689 if (outer_code == SET)
29691 if (XEXP (x, 1) == const0_rtx)
29693 if (TARGET_ISEL && !TARGET_MFCRF)
29694 *total = COSTS_N_INSNS (8);
29695 else
29696 *total = COSTS_N_INSNS (2);
29697 return true;
29699 else if (mode == Pmode)
29701 *total = COSTS_N_INSNS (3);
29702 return false;
29705 /* FALLTHRU */
29707 case GT:
29708 case LT:
29709 case UNORDERED:
29710 if (outer_code == SET && (XEXP (x, 1) == const0_rtx))
29712 if (TARGET_ISEL && !TARGET_MFCRF)
29713 *total = COSTS_N_INSNS (8);
29714 else
29715 *total = COSTS_N_INSNS (2);
29716 return true;
29718 /* CC COMPARE. */
29719 if (outer_code == COMPARE)
29721 *total = 0;
29722 return true;
29724 break;
29726 default:
29727 break;
29730 return false;
29733 /* Debug form of r6000_rtx_costs that is selected if -mdebug=cost. */
29735 static bool
29736 rs6000_debug_rtx_costs (rtx x, int code, int outer_code, int opno, int *total,
29737 bool speed)
29739 bool ret = rs6000_rtx_costs (x, code, outer_code, opno, total, speed);
29741 fprintf (stderr,
29742 "\nrs6000_rtx_costs, return = %s, code = %s, outer_code = %s, "
29743 "opno = %d, total = %d, speed = %s, x:\n",
29744 ret ? "complete" : "scan inner",
29745 GET_RTX_NAME (code),
29746 GET_RTX_NAME (outer_code),
29747 opno,
29748 *total,
29749 speed ? "true" : "false");
29751 debug_rtx (x);
29753 return ret;
29756 /* Debug form of ADDRESS_COST that is selected if -mdebug=cost. */
29758 static int
29759 rs6000_debug_address_cost (rtx x, enum machine_mode mode,
29760 addr_space_t as, bool speed)
29762 int ret = TARGET_ADDRESS_COST (x, mode, as, speed);
29764 fprintf (stderr, "\nrs6000_address_cost, return = %d, speed = %s, x:\n",
29765 ret, speed ? "true" : "false");
29766 debug_rtx (x);
29768 return ret;
29772 /* A C expression returning the cost of moving data from a register of class
29773 CLASS1 to one of CLASS2. */
29775 static int
29776 rs6000_register_move_cost (enum machine_mode mode,
29777 reg_class_t from, reg_class_t to)
29779 int ret;
29781 if (TARGET_DEBUG_COST)
29782 dbg_cost_ctrl++;
29784 /* Moves from/to GENERAL_REGS. */
29785 if (reg_classes_intersect_p (to, GENERAL_REGS)
29786 || reg_classes_intersect_p (from, GENERAL_REGS))
29788 reg_class_t rclass = from;
29790 if (! reg_classes_intersect_p (to, GENERAL_REGS))
29791 rclass = to;
29793 if (rclass == FLOAT_REGS || rclass == ALTIVEC_REGS || rclass == VSX_REGS)
29794 ret = (rs6000_memory_move_cost (mode, rclass, false)
29795 + rs6000_memory_move_cost (mode, GENERAL_REGS, false));
29797 /* It's more expensive to move CR_REGS than CR0_REGS because of the
29798 shift. */
29799 else if (rclass == CR_REGS)
29800 ret = 4;
29802 /* For those processors that have slow LR/CTR moves, make them more
29803 expensive than memory in order to bias spills to memory .*/
29804 else if ((rs6000_cpu == PROCESSOR_POWER6
29805 || rs6000_cpu == PROCESSOR_POWER7
29806 || rs6000_cpu == PROCESSOR_POWER8)
29807 && reg_classes_intersect_p (rclass, LINK_OR_CTR_REGS))
29808 ret = 6 * hard_regno_nregs[0][mode];
29810 else
29811 /* A move will cost one instruction per GPR moved. */
29812 ret = 2 * hard_regno_nregs[0][mode];
29815 /* If we have VSX, we can easily move between FPR or Altivec registers. */
29816 else if (VECTOR_MEM_VSX_P (mode)
29817 && reg_classes_intersect_p (to, VSX_REGS)
29818 && reg_classes_intersect_p (from, VSX_REGS))
29819 ret = 2 * hard_regno_nregs[32][mode];
29821 /* Moving between two similar registers is just one instruction. */
29822 else if (reg_classes_intersect_p (to, from))
29823 ret = (mode == TFmode || mode == TDmode) ? 4 : 2;
29825 /* Everything else has to go through GENERAL_REGS. */
29826 else
29827 ret = (rs6000_register_move_cost (mode, GENERAL_REGS, to)
29828 + rs6000_register_move_cost (mode, from, GENERAL_REGS));
29830 if (TARGET_DEBUG_COST)
29832 if (dbg_cost_ctrl == 1)
29833 fprintf (stderr,
29834 "rs6000_register_move_cost:, ret=%d, mode=%s, from=%s, to=%s\n",
29835 ret, GET_MODE_NAME (mode), reg_class_names[from],
29836 reg_class_names[to]);
29837 dbg_cost_ctrl--;
29840 return ret;
29843 /* A C expressions returning the cost of moving data of MODE from a register to
29844 or from memory. */
29846 static int
29847 rs6000_memory_move_cost (enum machine_mode mode, reg_class_t rclass,
29848 bool in ATTRIBUTE_UNUSED)
29850 int ret;
29852 if (TARGET_DEBUG_COST)
29853 dbg_cost_ctrl++;
29855 if (reg_classes_intersect_p (rclass, GENERAL_REGS))
29856 ret = 4 * hard_regno_nregs[0][mode];
29857 else if ((reg_classes_intersect_p (rclass, FLOAT_REGS)
29858 || reg_classes_intersect_p (rclass, VSX_REGS)))
29859 ret = 4 * hard_regno_nregs[32][mode];
29860 else if (reg_classes_intersect_p (rclass, ALTIVEC_REGS))
29861 ret = 4 * hard_regno_nregs[FIRST_ALTIVEC_REGNO][mode];
29862 else
29863 ret = 4 + rs6000_register_move_cost (mode, rclass, GENERAL_REGS);
29865 if (TARGET_DEBUG_COST)
29867 if (dbg_cost_ctrl == 1)
29868 fprintf (stderr,
29869 "rs6000_memory_move_cost: ret=%d, mode=%s, rclass=%s, in=%d\n",
29870 ret, GET_MODE_NAME (mode), reg_class_names[rclass], in);
29871 dbg_cost_ctrl--;
29874 return ret;
29877 /* Returns a code for a target-specific builtin that implements
29878 reciprocal of the function, or NULL_TREE if not available. */
29880 static tree
29881 rs6000_builtin_reciprocal (unsigned int fn, bool md_fn,
29882 bool sqrt ATTRIBUTE_UNUSED)
29884 if (optimize_insn_for_size_p ())
29885 return NULL_TREE;
29887 if (md_fn)
29888 switch (fn)
29890 case VSX_BUILTIN_XVSQRTDP:
29891 if (!RS6000_RECIP_AUTO_RSQRTE_P (V2DFmode))
29892 return NULL_TREE;
29894 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_2DF];
29896 case VSX_BUILTIN_XVSQRTSP:
29897 if (!RS6000_RECIP_AUTO_RSQRTE_P (V4SFmode))
29898 return NULL_TREE;
29900 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_4SF];
29902 default:
29903 return NULL_TREE;
29906 else
29907 switch (fn)
29909 case BUILT_IN_SQRT:
29910 if (!RS6000_RECIP_AUTO_RSQRTE_P (DFmode))
29911 return NULL_TREE;
29913 return rs6000_builtin_decls[RS6000_BUILTIN_RSQRT];
29915 case BUILT_IN_SQRTF:
29916 if (!RS6000_RECIP_AUTO_RSQRTE_P (SFmode))
29917 return NULL_TREE;
29919 return rs6000_builtin_decls[RS6000_BUILTIN_RSQRTF];
29921 default:
29922 return NULL_TREE;
29926 /* Load up a constant. If the mode is a vector mode, splat the value across
29927 all of the vector elements. */
29929 static rtx
29930 rs6000_load_constant_and_splat (enum machine_mode mode, REAL_VALUE_TYPE dconst)
29932 rtx reg;
29934 if (mode == SFmode || mode == DFmode)
29936 rtx d = CONST_DOUBLE_FROM_REAL_VALUE (dconst, mode);
29937 reg = force_reg (mode, d);
29939 else if (mode == V4SFmode)
29941 rtx d = CONST_DOUBLE_FROM_REAL_VALUE (dconst, SFmode);
29942 rtvec v = gen_rtvec (4, d, d, d, d);
29943 reg = gen_reg_rtx (mode);
29944 rs6000_expand_vector_init (reg, gen_rtx_PARALLEL (mode, v));
29946 else if (mode == V2DFmode)
29948 rtx d = CONST_DOUBLE_FROM_REAL_VALUE (dconst, DFmode);
29949 rtvec v = gen_rtvec (2, d, d);
29950 reg = gen_reg_rtx (mode);
29951 rs6000_expand_vector_init (reg, gen_rtx_PARALLEL (mode, v));
29953 else
29954 gcc_unreachable ();
29956 return reg;
29959 /* Generate an FMA instruction. */
29961 static void
29962 rs6000_emit_madd (rtx target, rtx m1, rtx m2, rtx a)
29964 enum machine_mode mode = GET_MODE (target);
29965 rtx dst;
29967 dst = expand_ternary_op (mode, fma_optab, m1, m2, a, target, 0);
29968 gcc_assert (dst != NULL);
29970 if (dst != target)
29971 emit_move_insn (target, dst);
29974 /* Generate a FMSUB instruction: dst = fma(m1, m2, -a). */
29976 static void
29977 rs6000_emit_msub (rtx target, rtx m1, rtx m2, rtx a)
29979 enum machine_mode mode = GET_MODE (target);
29980 rtx dst;
29982 /* Altivec does not support fms directly;
29983 generate in terms of fma in that case. */
29984 if (optab_handler (fms_optab, mode) != CODE_FOR_nothing)
29985 dst = expand_ternary_op (mode, fms_optab, m1, m2, a, target, 0);
29986 else
29988 a = expand_unop (mode, neg_optab, a, NULL_RTX, 0);
29989 dst = expand_ternary_op (mode, fma_optab, m1, m2, a, target, 0);
29991 gcc_assert (dst != NULL);
29993 if (dst != target)
29994 emit_move_insn (target, dst);
29997 /* Generate a FNMSUB instruction: dst = -fma(m1, m2, -a). */
29999 static void
30000 rs6000_emit_nmsub (rtx dst, rtx m1, rtx m2, rtx a)
30002 enum machine_mode mode = GET_MODE (dst);
30003 rtx r;
30005 /* This is a tad more complicated, since the fnma_optab is for
30006 a different expression: fma(-m1, m2, a), which is the same
30007 thing except in the case of signed zeros.
30009 Fortunately we know that if FMA is supported that FNMSUB is
30010 also supported in the ISA. Just expand it directly. */
30012 gcc_assert (optab_handler (fma_optab, mode) != CODE_FOR_nothing);
30014 r = gen_rtx_NEG (mode, a);
30015 r = gen_rtx_FMA (mode, m1, m2, r);
30016 r = gen_rtx_NEG (mode, r);
30017 emit_insn (gen_rtx_SET (VOIDmode, dst, r));
30020 /* Newton-Raphson approximation of floating point divide DST = N/D. If NOTE_P,
30021 add a reg_note saying that this was a division. Support both scalar and
30022 vector divide. Assumes no trapping math and finite arguments. */
30024 void
30025 rs6000_emit_swdiv (rtx dst, rtx n, rtx d, bool note_p)
30027 enum machine_mode mode = GET_MODE (dst);
30028 rtx one, x0, e0, x1, xprev, eprev, xnext, enext, u, v;
30029 int i;
30031 /* Low precision estimates guarantee 5 bits of accuracy. High
30032 precision estimates guarantee 14 bits of accuracy. SFmode
30033 requires 23 bits of accuracy. DFmode requires 52 bits of
30034 accuracy. Each pass at least doubles the accuracy, leading
30035 to the following. */
30036 int passes = (TARGET_RECIP_PRECISION) ? 1 : 3;
30037 if (mode == DFmode || mode == V2DFmode)
30038 passes++;
30040 enum insn_code code = optab_handler (smul_optab, mode);
30041 insn_gen_fn gen_mul = GEN_FCN (code);
30043 gcc_assert (code != CODE_FOR_nothing);
30045 one = rs6000_load_constant_and_splat (mode, dconst1);
30047 /* x0 = 1./d estimate */
30048 x0 = gen_reg_rtx (mode);
30049 emit_insn (gen_rtx_SET (VOIDmode, x0,
30050 gen_rtx_UNSPEC (mode, gen_rtvec (1, d),
30051 UNSPEC_FRES)));
30053 /* Each iteration but the last calculates x_(i+1) = x_i * (2 - d * x_i). */
30054 if (passes > 1) {
30056 /* e0 = 1. - d * x0 */
30057 e0 = gen_reg_rtx (mode);
30058 rs6000_emit_nmsub (e0, d, x0, one);
30060 /* x1 = x0 + e0 * x0 */
30061 x1 = gen_reg_rtx (mode);
30062 rs6000_emit_madd (x1, e0, x0, x0);
30064 for (i = 0, xprev = x1, eprev = e0; i < passes - 2;
30065 ++i, xprev = xnext, eprev = enext) {
30067 /* enext = eprev * eprev */
30068 enext = gen_reg_rtx (mode);
30069 emit_insn (gen_mul (enext, eprev, eprev));
30071 /* xnext = xprev + enext * xprev */
30072 xnext = gen_reg_rtx (mode);
30073 rs6000_emit_madd (xnext, enext, xprev, xprev);
30076 } else
30077 xprev = x0;
30079 /* The last iteration calculates x_(i+1) = n * x_i * (2 - d * x_i). */
30081 /* u = n * xprev */
30082 u = gen_reg_rtx (mode);
30083 emit_insn (gen_mul (u, n, xprev));
30085 /* v = n - (d * u) */
30086 v = gen_reg_rtx (mode);
30087 rs6000_emit_nmsub (v, d, u, n);
30089 /* dst = (v * xprev) + u */
30090 rs6000_emit_madd (dst, v, xprev, u);
30092 if (note_p)
30093 add_reg_note (get_last_insn (), REG_EQUAL, gen_rtx_DIV (mode, n, d));
30096 /* Newton-Raphson approximation of single/double-precision floating point
30097 rsqrt. Assumes no trapping math and finite arguments. */
30099 void
30100 rs6000_emit_swrsqrt (rtx dst, rtx src)
30102 enum machine_mode mode = GET_MODE (src);
30103 rtx x0 = gen_reg_rtx (mode);
30104 rtx y = gen_reg_rtx (mode);
30106 /* Low precision estimates guarantee 5 bits of accuracy. High
30107 precision estimates guarantee 14 bits of accuracy. SFmode
30108 requires 23 bits of accuracy. DFmode requires 52 bits of
30109 accuracy. Each pass at least doubles the accuracy, leading
30110 to the following. */
30111 int passes = (TARGET_RECIP_PRECISION) ? 1 : 3;
30112 if (mode == DFmode || mode == V2DFmode)
30113 passes++;
30115 REAL_VALUE_TYPE dconst3_2;
30116 int i;
30117 rtx halfthree;
30118 enum insn_code code = optab_handler (smul_optab, mode);
30119 insn_gen_fn gen_mul = GEN_FCN (code);
30121 gcc_assert (code != CODE_FOR_nothing);
30123 /* Load up the constant 1.5 either as a scalar, or as a vector. */
30124 real_from_integer (&dconst3_2, VOIDmode, 3, SIGNED);
30125 SET_REAL_EXP (&dconst3_2, REAL_EXP (&dconst3_2) - 1);
30127 halfthree = rs6000_load_constant_and_splat (mode, dconst3_2);
30129 /* x0 = rsqrt estimate */
30130 emit_insn (gen_rtx_SET (VOIDmode, x0,
30131 gen_rtx_UNSPEC (mode, gen_rtvec (1, src),
30132 UNSPEC_RSQRT)));
30134 /* y = 0.5 * src = 1.5 * src - src -> fewer constants */
30135 rs6000_emit_msub (y, src, halfthree, src);
30137 for (i = 0; i < passes; i++)
30139 rtx x1 = gen_reg_rtx (mode);
30140 rtx u = gen_reg_rtx (mode);
30141 rtx v = gen_reg_rtx (mode);
30143 /* x1 = x0 * (1.5 - y * (x0 * x0)) */
30144 emit_insn (gen_mul (u, x0, x0));
30145 rs6000_emit_nmsub (v, y, u, halfthree);
30146 emit_insn (gen_mul (x1, x0, v));
30147 x0 = x1;
30150 emit_move_insn (dst, x0);
30151 return;
30154 /* Emit popcount intrinsic on TARGET_POPCNTB (Power5) and TARGET_POPCNTD
30155 (Power7) targets. DST is the target, and SRC is the argument operand. */
30157 void
30158 rs6000_emit_popcount (rtx dst, rtx src)
30160 enum machine_mode mode = GET_MODE (dst);
30161 rtx tmp1, tmp2;
30163 /* Use the PPC ISA 2.06 popcnt{w,d} instruction if we can. */
30164 if (TARGET_POPCNTD)
30166 if (mode == SImode)
30167 emit_insn (gen_popcntdsi2 (dst, src));
30168 else
30169 emit_insn (gen_popcntddi2 (dst, src));
30170 return;
30173 tmp1 = gen_reg_rtx (mode);
30175 if (mode == SImode)
30177 emit_insn (gen_popcntbsi2 (tmp1, src));
30178 tmp2 = expand_mult (SImode, tmp1, GEN_INT (0x01010101),
30179 NULL_RTX, 0);
30180 tmp2 = force_reg (SImode, tmp2);
30181 emit_insn (gen_lshrsi3 (dst, tmp2, GEN_INT (24)));
30183 else
30185 emit_insn (gen_popcntbdi2 (tmp1, src));
30186 tmp2 = expand_mult (DImode, tmp1,
30187 GEN_INT ((HOST_WIDE_INT)
30188 0x01010101 << 32 | 0x01010101),
30189 NULL_RTX, 0);
30190 tmp2 = force_reg (DImode, tmp2);
30191 emit_insn (gen_lshrdi3 (dst, tmp2, GEN_INT (56)));
30196 /* Emit parity intrinsic on TARGET_POPCNTB targets. DST is the
30197 target, and SRC is the argument operand. */
30199 void
30200 rs6000_emit_parity (rtx dst, rtx src)
30202 enum machine_mode mode = GET_MODE (dst);
30203 rtx tmp;
30205 tmp = gen_reg_rtx (mode);
30207 /* Use the PPC ISA 2.05 prtyw/prtyd instruction if we can. */
30208 if (TARGET_CMPB)
30210 if (mode == SImode)
30212 emit_insn (gen_popcntbsi2 (tmp, src));
30213 emit_insn (gen_paritysi2_cmpb (dst, tmp));
30215 else
30217 emit_insn (gen_popcntbdi2 (tmp, src));
30218 emit_insn (gen_paritydi2_cmpb (dst, tmp));
30220 return;
30223 if (mode == SImode)
30225 /* Is mult+shift >= shift+xor+shift+xor? */
30226 if (rs6000_cost->mulsi_const >= COSTS_N_INSNS (3))
30228 rtx tmp1, tmp2, tmp3, tmp4;
30230 tmp1 = gen_reg_rtx (SImode);
30231 emit_insn (gen_popcntbsi2 (tmp1, src));
30233 tmp2 = gen_reg_rtx (SImode);
30234 emit_insn (gen_lshrsi3 (tmp2, tmp1, GEN_INT (16)));
30235 tmp3 = gen_reg_rtx (SImode);
30236 emit_insn (gen_xorsi3 (tmp3, tmp1, tmp2));
30238 tmp4 = gen_reg_rtx (SImode);
30239 emit_insn (gen_lshrsi3 (tmp4, tmp3, GEN_INT (8)));
30240 emit_insn (gen_xorsi3 (tmp, tmp3, tmp4));
30242 else
30243 rs6000_emit_popcount (tmp, src);
30244 emit_insn (gen_andsi3 (dst, tmp, const1_rtx));
30246 else
30248 /* Is mult+shift >= shift+xor+shift+xor+shift+xor? */
30249 if (rs6000_cost->muldi >= COSTS_N_INSNS (5))
30251 rtx tmp1, tmp2, tmp3, tmp4, tmp5, tmp6;
30253 tmp1 = gen_reg_rtx (DImode);
30254 emit_insn (gen_popcntbdi2 (tmp1, src));
30256 tmp2 = gen_reg_rtx (DImode);
30257 emit_insn (gen_lshrdi3 (tmp2, tmp1, GEN_INT (32)));
30258 tmp3 = gen_reg_rtx (DImode);
30259 emit_insn (gen_xordi3 (tmp3, tmp1, tmp2));
30261 tmp4 = gen_reg_rtx (DImode);
30262 emit_insn (gen_lshrdi3 (tmp4, tmp3, GEN_INT (16)));
30263 tmp5 = gen_reg_rtx (DImode);
30264 emit_insn (gen_xordi3 (tmp5, tmp3, tmp4));
30266 tmp6 = gen_reg_rtx (DImode);
30267 emit_insn (gen_lshrdi3 (tmp6, tmp5, GEN_INT (8)));
30268 emit_insn (gen_xordi3 (tmp, tmp5, tmp6));
30270 else
30271 rs6000_emit_popcount (tmp, src);
30272 emit_insn (gen_anddi3 (dst, tmp, const1_rtx));
30276 /* Expand an Altivec constant permutation for little endian mode.
30277 There are two issues: First, the two input operands must be
30278 swapped so that together they form a double-wide array in LE
30279 order. Second, the vperm instruction has surprising behavior
30280 in LE mode: it interprets the elements of the source vectors
30281 in BE mode ("left to right") and interprets the elements of
30282 the destination vector in LE mode ("right to left"). To
30283 correct for this, we must subtract each element of the permute
30284 control vector from 31.
30286 For example, suppose we want to concatenate vr10 = {0, 1, 2, 3}
30287 with vr11 = {4, 5, 6, 7} and extract {0, 2, 4, 6} using a vperm.
30288 We place {0,1,2,3,8,9,10,11,16,17,18,19,24,25,26,27} in vr12 to
30289 serve as the permute control vector. Then, in BE mode,
30291 vperm 9,10,11,12
30293 places the desired result in vr9. However, in LE mode the
30294 vector contents will be
30296 vr10 = 00000003 00000002 00000001 00000000
30297 vr11 = 00000007 00000006 00000005 00000004
30299 The result of the vperm using the same permute control vector is
30301 vr9 = 05000000 07000000 01000000 03000000
30303 That is, the leftmost 4 bytes of vr10 are interpreted as the
30304 source for the rightmost 4 bytes of vr9, and so on.
30306 If we change the permute control vector to
30308 vr12 = {31,20,29,28,23,22,21,20,15,14,13,12,7,6,5,4}
30310 and issue
30312 vperm 9,11,10,12
30314 we get the desired
30316 vr9 = 00000006 00000004 00000002 00000000. */
30318 void
30319 altivec_expand_vec_perm_const_le (rtx operands[4])
30321 unsigned int i;
30322 rtx perm[16];
30323 rtx constv, unspec;
30324 rtx target = operands[0];
30325 rtx op0 = operands[1];
30326 rtx op1 = operands[2];
30327 rtx sel = operands[3];
30329 /* Unpack and adjust the constant selector. */
30330 for (i = 0; i < 16; ++i)
30332 rtx e = XVECEXP (sel, 0, i);
30333 unsigned int elt = 31 - (INTVAL (e) & 31);
30334 perm[i] = GEN_INT (elt);
30337 /* Expand to a permute, swapping the inputs and using the
30338 adjusted selector. */
30339 if (!REG_P (op0))
30340 op0 = force_reg (V16QImode, op0);
30341 if (!REG_P (op1))
30342 op1 = force_reg (V16QImode, op1);
30344 constv = gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, perm));
30345 constv = force_reg (V16QImode, constv);
30346 unspec = gen_rtx_UNSPEC (V16QImode, gen_rtvec (3, op1, op0, constv),
30347 UNSPEC_VPERM);
30348 if (!REG_P (target))
30350 rtx tmp = gen_reg_rtx (V16QImode);
30351 emit_move_insn (tmp, unspec);
30352 unspec = tmp;
30355 emit_move_insn (target, unspec);
30358 /* Similarly to altivec_expand_vec_perm_const_le, we must adjust the
30359 permute control vector. But here it's not a constant, so we must
30360 generate a vector NAND or NOR to do the adjustment. */
30362 void
30363 altivec_expand_vec_perm_le (rtx operands[4])
30365 rtx notx, iorx, unspec;
30366 rtx target = operands[0];
30367 rtx op0 = operands[1];
30368 rtx op1 = operands[2];
30369 rtx sel = operands[3];
30370 rtx tmp = target;
30371 rtx norreg = gen_reg_rtx (V16QImode);
30372 enum machine_mode mode = GET_MODE (target);
30374 /* Get everything in regs so the pattern matches. */
30375 if (!REG_P (op0))
30376 op0 = force_reg (mode, op0);
30377 if (!REG_P (op1))
30378 op1 = force_reg (mode, op1);
30379 if (!REG_P (sel))
30380 sel = force_reg (V16QImode, sel);
30381 if (!REG_P (target))
30382 tmp = gen_reg_rtx (mode);
30384 /* Invert the selector with a VNAND if available, else a VNOR.
30385 The VNAND is preferred for future fusion opportunities. */
30386 notx = gen_rtx_NOT (V16QImode, sel);
30387 iorx = (TARGET_P8_VECTOR
30388 ? gen_rtx_IOR (V16QImode, notx, notx)
30389 : gen_rtx_AND (V16QImode, notx, notx));
30390 emit_insn (gen_rtx_SET (VOIDmode, norreg, iorx));
30392 /* Permute with operands reversed and adjusted selector. */
30393 unspec = gen_rtx_UNSPEC (mode, gen_rtvec (3, op1, op0, norreg),
30394 UNSPEC_VPERM);
30396 /* Copy into target, possibly by way of a register. */
30397 if (!REG_P (target))
30399 emit_move_insn (tmp, unspec);
30400 unspec = tmp;
30403 emit_move_insn (target, unspec);
30406 /* Expand an Altivec constant permutation. Return true if we match
30407 an efficient implementation; false to fall back to VPERM. */
30409 bool
30410 altivec_expand_vec_perm_const (rtx operands[4])
30412 struct altivec_perm_insn {
30413 HOST_WIDE_INT mask;
30414 enum insn_code impl;
30415 unsigned char perm[16];
30417 static const struct altivec_perm_insn patterns[] = {
30418 { OPTION_MASK_ALTIVEC, CODE_FOR_altivec_vpkuhum_direct,
30419 { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } },
30420 { OPTION_MASK_ALTIVEC, CODE_FOR_altivec_vpkuwum_direct,
30421 { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } },
30422 { OPTION_MASK_ALTIVEC,
30423 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrghb_direct
30424 : CODE_FOR_altivec_vmrglb_direct),
30425 { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 } },
30426 { OPTION_MASK_ALTIVEC,
30427 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrghh_direct
30428 : CODE_FOR_altivec_vmrglh_direct),
30429 { 0, 1, 16, 17, 2, 3, 18, 19, 4, 5, 20, 21, 6, 7, 22, 23 } },
30430 { OPTION_MASK_ALTIVEC,
30431 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrghw_direct
30432 : CODE_FOR_altivec_vmrglw_direct),
30433 { 0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23 } },
30434 { OPTION_MASK_ALTIVEC,
30435 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrglb_direct
30436 : CODE_FOR_altivec_vmrghb_direct),
30437 { 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } },
30438 { OPTION_MASK_ALTIVEC,
30439 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrglh_direct
30440 : CODE_FOR_altivec_vmrghh_direct),
30441 { 8, 9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } },
30442 { OPTION_MASK_ALTIVEC,
30443 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrglw_direct
30444 : CODE_FOR_altivec_vmrghw_direct),
30445 { 8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } },
30446 { OPTION_MASK_P8_VECTOR, CODE_FOR_p8_vmrgew,
30447 { 0, 1, 2, 3, 16, 17, 18, 19, 8, 9, 10, 11, 24, 25, 26, 27 } },
30448 { OPTION_MASK_P8_VECTOR, CODE_FOR_p8_vmrgow,
30449 { 4, 5, 6, 7, 20, 21, 22, 23, 12, 13, 14, 15, 28, 29, 30, 31 } }
30452 unsigned int i, j, elt, which;
30453 unsigned char perm[16];
30454 rtx target, op0, op1, sel, x;
30455 bool one_vec;
30457 target = operands[0];
30458 op0 = operands[1];
30459 op1 = operands[2];
30460 sel = operands[3];
30462 /* Unpack the constant selector. */
30463 for (i = which = 0; i < 16; ++i)
30465 rtx e = XVECEXP (sel, 0, i);
30466 elt = INTVAL (e) & 31;
30467 which |= (elt < 16 ? 1 : 2);
30468 perm[i] = elt;
30471 /* Simplify the constant selector based on operands. */
30472 switch (which)
30474 default:
30475 gcc_unreachable ();
30477 case 3:
30478 one_vec = false;
30479 if (!rtx_equal_p (op0, op1))
30480 break;
30481 /* FALLTHRU */
30483 case 2:
30484 for (i = 0; i < 16; ++i)
30485 perm[i] &= 15;
30486 op0 = op1;
30487 one_vec = true;
30488 break;
30490 case 1:
30491 op1 = op0;
30492 one_vec = true;
30493 break;
30496 /* Look for splat patterns. */
30497 if (one_vec)
30499 elt = perm[0];
30501 for (i = 0; i < 16; ++i)
30502 if (perm[i] != elt)
30503 break;
30504 if (i == 16)
30506 if (!BYTES_BIG_ENDIAN)
30507 elt = 15 - elt;
30508 emit_insn (gen_altivec_vspltb_direct (target, op0, GEN_INT (elt)));
30509 return true;
30512 if (elt % 2 == 0)
30514 for (i = 0; i < 16; i += 2)
30515 if (perm[i] != elt || perm[i + 1] != elt + 1)
30516 break;
30517 if (i == 16)
30519 int field = BYTES_BIG_ENDIAN ? elt / 2 : 7 - elt / 2;
30520 x = gen_reg_rtx (V8HImode);
30521 emit_insn (gen_altivec_vsplth_direct (x, gen_lowpart (V8HImode, op0),
30522 GEN_INT (field)));
30523 emit_move_insn (target, gen_lowpart (V16QImode, x));
30524 return true;
30528 if (elt % 4 == 0)
30530 for (i = 0; i < 16; i += 4)
30531 if (perm[i] != elt
30532 || perm[i + 1] != elt + 1
30533 || perm[i + 2] != elt + 2
30534 || perm[i + 3] != elt + 3)
30535 break;
30536 if (i == 16)
30538 int field = BYTES_BIG_ENDIAN ? elt / 4 : 3 - elt / 4;
30539 x = gen_reg_rtx (V4SImode);
30540 emit_insn (gen_altivec_vspltw_direct (x, gen_lowpart (V4SImode, op0),
30541 GEN_INT (field)));
30542 emit_move_insn (target, gen_lowpart (V16QImode, x));
30543 return true;
30548 /* Look for merge and pack patterns. */
30549 for (j = 0; j < ARRAY_SIZE (patterns); ++j)
30551 bool swapped;
30553 if ((patterns[j].mask & rs6000_isa_flags) == 0)
30554 continue;
30556 elt = patterns[j].perm[0];
30557 if (perm[0] == elt)
30558 swapped = false;
30559 else if (perm[0] == elt + 16)
30560 swapped = true;
30561 else
30562 continue;
30563 for (i = 1; i < 16; ++i)
30565 elt = patterns[j].perm[i];
30566 if (swapped)
30567 elt = (elt >= 16 ? elt - 16 : elt + 16);
30568 else if (one_vec && elt >= 16)
30569 elt -= 16;
30570 if (perm[i] != elt)
30571 break;
30573 if (i == 16)
30575 enum insn_code icode = patterns[j].impl;
30576 enum machine_mode omode = insn_data[icode].operand[0].mode;
30577 enum machine_mode imode = insn_data[icode].operand[1].mode;
30579 /* For little-endian, don't use vpkuwum and vpkuhum if the
30580 underlying vector type is not V4SI and V8HI, respectively.
30581 For example, using vpkuwum with a V8HI picks up the even
30582 halfwords (BE numbering) when the even halfwords (LE
30583 numbering) are what we need. */
30584 if (!BYTES_BIG_ENDIAN
30585 && icode == CODE_FOR_altivec_vpkuwum_direct
30586 && ((GET_CODE (op0) == REG
30587 && GET_MODE (op0) != V4SImode)
30588 || (GET_CODE (op0) == SUBREG
30589 && GET_MODE (XEXP (op0, 0)) != V4SImode)))
30590 continue;
30591 if (!BYTES_BIG_ENDIAN
30592 && icode == CODE_FOR_altivec_vpkuhum_direct
30593 && ((GET_CODE (op0) == REG
30594 && GET_MODE (op0) != V8HImode)
30595 || (GET_CODE (op0) == SUBREG
30596 && GET_MODE (XEXP (op0, 0)) != V8HImode)))
30597 continue;
30599 /* For little-endian, the two input operands must be swapped
30600 (or swapped back) to ensure proper right-to-left numbering
30601 from 0 to 2N-1. */
30602 if (swapped ^ !BYTES_BIG_ENDIAN)
30603 x = op0, op0 = op1, op1 = x;
30604 if (imode != V16QImode)
30606 op0 = gen_lowpart (imode, op0);
30607 op1 = gen_lowpart (imode, op1);
30609 if (omode == V16QImode)
30610 x = target;
30611 else
30612 x = gen_reg_rtx (omode);
30613 emit_insn (GEN_FCN (icode) (x, op0, op1));
30614 if (omode != V16QImode)
30615 emit_move_insn (target, gen_lowpart (V16QImode, x));
30616 return true;
30620 if (!BYTES_BIG_ENDIAN)
30622 altivec_expand_vec_perm_const_le (operands);
30623 return true;
30626 return false;
30629 /* Expand a Paired Single, VSX Permute Doubleword, or SPE constant permutation.
30630 Return true if we match an efficient implementation. */
30632 static bool
30633 rs6000_expand_vec_perm_const_1 (rtx target, rtx op0, rtx op1,
30634 unsigned char perm0, unsigned char perm1)
30636 rtx x;
30638 /* If both selectors come from the same operand, fold to single op. */
30639 if ((perm0 & 2) == (perm1 & 2))
30641 if (perm0 & 2)
30642 op0 = op1;
30643 else
30644 op1 = op0;
30646 /* If both operands are equal, fold to simpler permutation. */
30647 if (rtx_equal_p (op0, op1))
30649 perm0 = perm0 & 1;
30650 perm1 = (perm1 & 1) + 2;
30652 /* If the first selector comes from the second operand, swap. */
30653 else if (perm0 & 2)
30655 if (perm1 & 2)
30656 return false;
30657 perm0 -= 2;
30658 perm1 += 2;
30659 x = op0, op0 = op1, op1 = x;
30661 /* If the second selector does not come from the second operand, fail. */
30662 else if ((perm1 & 2) == 0)
30663 return false;
30665 /* Success! */
30666 if (target != NULL)
30668 enum machine_mode vmode, dmode;
30669 rtvec v;
30671 vmode = GET_MODE (target);
30672 gcc_assert (GET_MODE_NUNITS (vmode) == 2);
30673 dmode = mode_for_vector (GET_MODE_INNER (vmode), 4);
30674 x = gen_rtx_VEC_CONCAT (dmode, op0, op1);
30675 v = gen_rtvec (2, GEN_INT (perm0), GEN_INT (perm1));
30676 x = gen_rtx_VEC_SELECT (vmode, x, gen_rtx_PARALLEL (VOIDmode, v));
30677 emit_insn (gen_rtx_SET (VOIDmode, target, x));
30679 return true;
30682 bool
30683 rs6000_expand_vec_perm_const (rtx operands[4])
30685 rtx target, op0, op1, sel;
30686 unsigned char perm0, perm1;
30688 target = operands[0];
30689 op0 = operands[1];
30690 op1 = operands[2];
30691 sel = operands[3];
30693 /* Unpack the constant selector. */
30694 perm0 = INTVAL (XVECEXP (sel, 0, 0)) & 3;
30695 perm1 = INTVAL (XVECEXP (sel, 0, 1)) & 3;
30697 return rs6000_expand_vec_perm_const_1 (target, op0, op1, perm0, perm1);
30700 /* Test whether a constant permutation is supported. */
30702 static bool
30703 rs6000_vectorize_vec_perm_const_ok (enum machine_mode vmode,
30704 const unsigned char *sel)
30706 /* AltiVec (and thus VSX) can handle arbitrary permutations. */
30707 if (TARGET_ALTIVEC)
30708 return true;
30710 /* Check for ps_merge* or evmerge* insns. */
30711 if ((TARGET_PAIRED_FLOAT && vmode == V2SFmode)
30712 || (TARGET_SPE && vmode == V2SImode))
30714 rtx op0 = gen_raw_REG (vmode, LAST_VIRTUAL_REGISTER + 1);
30715 rtx op1 = gen_raw_REG (vmode, LAST_VIRTUAL_REGISTER + 2);
30716 return rs6000_expand_vec_perm_const_1 (NULL, op0, op1, sel[0], sel[1]);
30719 return false;
30722 /* A subroutine for rs6000_expand_extract_even & rs6000_expand_interleave. */
30724 static void
30725 rs6000_do_expand_vec_perm (rtx target, rtx op0, rtx op1,
30726 enum machine_mode vmode, unsigned nelt, rtx perm[])
30728 enum machine_mode imode;
30729 rtx x;
30731 imode = vmode;
30732 if (GET_MODE_CLASS (vmode) != MODE_VECTOR_INT)
30734 imode = GET_MODE_INNER (vmode);
30735 imode = mode_for_size (GET_MODE_BITSIZE (imode), MODE_INT, 0);
30736 imode = mode_for_vector (imode, nelt);
30739 x = gen_rtx_CONST_VECTOR (imode, gen_rtvec_v (nelt, perm));
30740 x = expand_vec_perm (vmode, op0, op1, x, target);
30741 if (x != target)
30742 emit_move_insn (target, x);
30745 /* Expand an extract even operation. */
30747 void
30748 rs6000_expand_extract_even (rtx target, rtx op0, rtx op1)
30750 enum machine_mode vmode = GET_MODE (target);
30751 unsigned i, nelt = GET_MODE_NUNITS (vmode);
30752 rtx perm[16];
30754 for (i = 0; i < nelt; i++)
30755 perm[i] = GEN_INT (i * 2);
30757 rs6000_do_expand_vec_perm (target, op0, op1, vmode, nelt, perm);
30760 /* Expand a vector interleave operation. */
30762 void
30763 rs6000_expand_interleave (rtx target, rtx op0, rtx op1, bool highp)
30765 enum machine_mode vmode = GET_MODE (target);
30766 unsigned i, high, nelt = GET_MODE_NUNITS (vmode);
30767 rtx perm[16];
30769 high = (highp ? 0 : nelt / 2);
30770 for (i = 0; i < nelt / 2; i++)
30772 perm[i * 2] = GEN_INT (i + high);
30773 perm[i * 2 + 1] = GEN_INT (i + nelt + high);
30776 rs6000_do_expand_vec_perm (target, op0, op1, vmode, nelt, perm);
30779 /* Return an RTX representing where to find the function value of a
30780 function returning MODE. */
30781 static rtx
30782 rs6000_complex_function_value (enum machine_mode mode)
30784 unsigned int regno;
30785 rtx r1, r2;
30786 enum machine_mode inner = GET_MODE_INNER (mode);
30787 unsigned int inner_bytes = GET_MODE_SIZE (inner);
30789 if (FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
30790 regno = FP_ARG_RETURN;
30791 else
30793 regno = GP_ARG_RETURN;
30795 /* 32-bit is OK since it'll go in r3/r4. */
30796 if (TARGET_32BIT && inner_bytes >= 4)
30797 return gen_rtx_REG (mode, regno);
30800 if (inner_bytes >= 8)
30801 return gen_rtx_REG (mode, regno);
30803 r1 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno),
30804 const0_rtx);
30805 r2 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno + 1),
30806 GEN_INT (inner_bytes));
30807 return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
30810 /* Target hook for TARGET_FUNCTION_VALUE.
30812 On the SPE, both FPs and vectors are returned in r3.
30814 On RS/6000 an integer value is in r3 and a floating-point value is in
30815 fp1, unless -msoft-float. */
30817 static rtx
30818 rs6000_function_value (const_tree valtype,
30819 const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
30820 bool outgoing ATTRIBUTE_UNUSED)
30822 enum machine_mode mode;
30823 unsigned int regno;
30824 enum machine_mode elt_mode;
30825 int n_elts;
30827 /* Special handling for structs in darwin64. */
30828 if (TARGET_MACHO
30829 && rs6000_darwin64_struct_check_p (TYPE_MODE (valtype), valtype))
30831 CUMULATIVE_ARGS valcum;
30832 rtx valret;
30834 valcum.words = 0;
30835 valcum.fregno = FP_ARG_MIN_REG;
30836 valcum.vregno = ALTIVEC_ARG_MIN_REG;
30837 /* Do a trial code generation as if this were going to be passed as
30838 an argument; if any part goes in memory, we return NULL. */
30839 valret = rs6000_darwin64_record_arg (&valcum, valtype, true, /* retval= */ true);
30840 if (valret)
30841 return valret;
30842 /* Otherwise fall through to standard ABI rules. */
30845 /* The ELFv2 ABI returns homogeneous VFP aggregates in registers. */
30846 if (rs6000_discover_homogeneous_aggregate (TYPE_MODE (valtype), valtype,
30847 &elt_mode, &n_elts))
30849 int first_reg, n_regs, i;
30850 rtx par;
30852 if (SCALAR_FLOAT_MODE_P (elt_mode))
30854 /* _Decimal128 must use even/odd register pairs. */
30855 first_reg = (elt_mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
30856 n_regs = (GET_MODE_SIZE (elt_mode) + 7) >> 3;
30858 else
30860 first_reg = ALTIVEC_ARG_RETURN;
30861 n_regs = 1;
30864 par = gen_rtx_PARALLEL (TYPE_MODE (valtype), rtvec_alloc (n_elts));
30865 for (i = 0; i < n_elts; i++)
30867 rtx r = gen_rtx_REG (elt_mode, first_reg + i * n_regs);
30868 rtx off = GEN_INT (i * GET_MODE_SIZE (elt_mode));
30869 XVECEXP (par, 0, i) = gen_rtx_EXPR_LIST (VOIDmode, r, off);
30872 return par;
30875 if (TARGET_32BIT && TARGET_POWERPC64 && TYPE_MODE (valtype) == DImode)
30877 /* Long long return value need be split in -mpowerpc64, 32bit ABI. */
30878 return gen_rtx_PARALLEL (DImode,
30879 gen_rtvec (2,
30880 gen_rtx_EXPR_LIST (VOIDmode,
30881 gen_rtx_REG (SImode, GP_ARG_RETURN),
30882 const0_rtx),
30883 gen_rtx_EXPR_LIST (VOIDmode,
30884 gen_rtx_REG (SImode,
30885 GP_ARG_RETURN + 1),
30886 GEN_INT (4))));
30888 if (TARGET_32BIT && TARGET_POWERPC64 && TYPE_MODE (valtype) == DCmode)
30890 return gen_rtx_PARALLEL (DCmode,
30891 gen_rtvec (4,
30892 gen_rtx_EXPR_LIST (VOIDmode,
30893 gen_rtx_REG (SImode, GP_ARG_RETURN),
30894 const0_rtx),
30895 gen_rtx_EXPR_LIST (VOIDmode,
30896 gen_rtx_REG (SImode,
30897 GP_ARG_RETURN + 1),
30898 GEN_INT (4)),
30899 gen_rtx_EXPR_LIST (VOIDmode,
30900 gen_rtx_REG (SImode,
30901 GP_ARG_RETURN + 2),
30902 GEN_INT (8)),
30903 gen_rtx_EXPR_LIST (VOIDmode,
30904 gen_rtx_REG (SImode,
30905 GP_ARG_RETURN + 3),
30906 GEN_INT (12))));
30909 mode = TYPE_MODE (valtype);
30910 if ((INTEGRAL_TYPE_P (valtype) && GET_MODE_BITSIZE (mode) < BITS_PER_WORD)
30911 || POINTER_TYPE_P (valtype))
30912 mode = TARGET_32BIT ? SImode : DImode;
30914 if (DECIMAL_FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
30915 /* _Decimal128 must use an even/odd register pair. */
30916 regno = (mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
30917 else if (SCALAR_FLOAT_TYPE_P (valtype) && TARGET_HARD_FLOAT && TARGET_FPRS
30918 && ((TARGET_SINGLE_FLOAT && (mode == SFmode)) || TARGET_DOUBLE_FLOAT))
30919 regno = FP_ARG_RETURN;
30920 else if (TREE_CODE (valtype) == COMPLEX_TYPE
30921 && targetm.calls.split_complex_arg)
30922 return rs6000_complex_function_value (mode);
30923 /* VSX is a superset of Altivec and adds V2DImode/V2DFmode. Since the same
30924 return register is used in both cases, and we won't see V2DImode/V2DFmode
30925 for pure altivec, combine the two cases. */
30926 else if (TREE_CODE (valtype) == VECTOR_TYPE
30927 && TARGET_ALTIVEC && TARGET_ALTIVEC_ABI
30928 && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
30929 regno = ALTIVEC_ARG_RETURN;
30930 else if (TARGET_E500_DOUBLE && TARGET_HARD_FLOAT
30931 && (mode == DFmode || mode == DCmode
30932 || mode == TFmode || mode == TCmode))
30933 return spe_build_register_parallel (mode, GP_ARG_RETURN);
30934 else
30935 regno = GP_ARG_RETURN;
30937 return gen_rtx_REG (mode, regno);
30940 /* Define how to find the value returned by a library function
30941 assuming the value has mode MODE. */
30943 rs6000_libcall_value (enum machine_mode mode)
30945 unsigned int regno;
30947 if (TARGET_32BIT && TARGET_POWERPC64 && mode == DImode)
30949 /* Long long return value need be split in -mpowerpc64, 32bit ABI. */
30950 return gen_rtx_PARALLEL (DImode,
30951 gen_rtvec (2,
30952 gen_rtx_EXPR_LIST (VOIDmode,
30953 gen_rtx_REG (SImode, GP_ARG_RETURN),
30954 const0_rtx),
30955 gen_rtx_EXPR_LIST (VOIDmode,
30956 gen_rtx_REG (SImode,
30957 GP_ARG_RETURN + 1),
30958 GEN_INT (4))));
30961 if (DECIMAL_FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
30962 /* _Decimal128 must use an even/odd register pair. */
30963 regno = (mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
30964 else if (SCALAR_FLOAT_MODE_P (mode)
30965 && TARGET_HARD_FLOAT && TARGET_FPRS
30966 && ((TARGET_SINGLE_FLOAT && mode == SFmode) || TARGET_DOUBLE_FLOAT))
30967 regno = FP_ARG_RETURN;
30968 /* VSX is a superset of Altivec and adds V2DImode/V2DFmode. Since the same
30969 return register is used in both cases, and we won't see V2DImode/V2DFmode
30970 for pure altivec, combine the two cases. */
30971 else if (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
30972 && TARGET_ALTIVEC && TARGET_ALTIVEC_ABI)
30973 regno = ALTIVEC_ARG_RETURN;
30974 else if (COMPLEX_MODE_P (mode) && targetm.calls.split_complex_arg)
30975 return rs6000_complex_function_value (mode);
30976 else if (TARGET_E500_DOUBLE && TARGET_HARD_FLOAT
30977 && (mode == DFmode || mode == DCmode
30978 || mode == TFmode || mode == TCmode))
30979 return spe_build_register_parallel (mode, GP_ARG_RETURN);
30980 else
30981 regno = GP_ARG_RETURN;
30983 return gen_rtx_REG (mode, regno);
30987 /* Return true if we use LRA instead of reload pass. */
30988 static bool
30989 rs6000_lra_p (void)
30991 return rs6000_lra_flag;
30994 /* Given FROM and TO register numbers, say whether this elimination is allowed.
30995 Frame pointer elimination is automatically handled.
30997 For the RS/6000, if frame pointer elimination is being done, we would like
30998 to convert ap into fp, not sp.
31000 We need r30 if -mminimal-toc was specified, and there are constant pool
31001 references. */
31003 static bool
31004 rs6000_can_eliminate (const int from, const int to)
31006 return (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM
31007 ? ! frame_pointer_needed
31008 : from == RS6000_PIC_OFFSET_TABLE_REGNUM
31009 ? ! TARGET_MINIMAL_TOC || TARGET_NO_TOC || get_pool_size () == 0
31010 : true);
31013 /* Define the offset between two registers, FROM to be eliminated and its
31014 replacement TO, at the start of a routine. */
31015 HOST_WIDE_INT
31016 rs6000_initial_elimination_offset (int from, int to)
31018 rs6000_stack_t *info = rs6000_stack_info ();
31019 HOST_WIDE_INT offset;
31021 if (from == HARD_FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
31022 offset = info->push_p ? 0 : -info->total_size;
31023 else if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
31025 offset = info->push_p ? 0 : -info->total_size;
31026 if (FRAME_GROWS_DOWNWARD)
31027 offset += info->fixed_size + info->vars_size + info->parm_size;
31029 else if (from == FRAME_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
31030 offset = FRAME_GROWS_DOWNWARD
31031 ? info->fixed_size + info->vars_size + info->parm_size
31032 : 0;
31033 else if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
31034 offset = info->total_size;
31035 else if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
31036 offset = info->push_p ? info->total_size : 0;
31037 else if (from == RS6000_PIC_OFFSET_TABLE_REGNUM)
31038 offset = 0;
31039 else
31040 gcc_unreachable ();
31042 return offset;
31045 static rtx
31046 rs6000_dwarf_register_span (rtx reg)
31048 rtx parts[8];
31049 int i, words;
31050 unsigned regno = REGNO (reg);
31051 enum machine_mode mode = GET_MODE (reg);
31053 if (TARGET_SPE
31054 && regno < 32
31055 && (SPE_VECTOR_MODE (GET_MODE (reg))
31056 || (TARGET_E500_DOUBLE && FLOAT_MODE_P (mode)
31057 && mode != SFmode && mode != SDmode && mode != SCmode)))
31059 else
31060 return NULL_RTX;
31062 regno = REGNO (reg);
31064 /* The duality of the SPE register size wreaks all kinds of havoc.
31065 This is a way of distinguishing r0 in 32-bits from r0 in
31066 64-bits. */
31067 words = (GET_MODE_SIZE (mode) + UNITS_PER_FP_WORD - 1) / UNITS_PER_FP_WORD;
31068 gcc_assert (words <= 4);
31069 for (i = 0; i < words; i++, regno++)
31071 if (BYTES_BIG_ENDIAN)
31073 parts[2 * i] = gen_rtx_REG (SImode, regno + 1200);
31074 parts[2 * i + 1] = gen_rtx_REG (SImode, regno);
31076 else
31078 parts[2 * i] = gen_rtx_REG (SImode, regno);
31079 parts[2 * i + 1] = gen_rtx_REG (SImode, regno + 1200);
31083 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (words * 2, parts));
31086 /* Fill in sizes for SPE register high parts in table used by unwinder. */
31088 static void
31089 rs6000_init_dwarf_reg_sizes_extra (tree address)
31091 if (TARGET_SPE)
31093 int i;
31094 enum machine_mode mode = TYPE_MODE (char_type_node);
31095 rtx addr = expand_expr (address, NULL_RTX, VOIDmode, EXPAND_NORMAL);
31096 rtx mem = gen_rtx_MEM (BLKmode, addr);
31097 rtx value = gen_int_mode (4, mode);
31099 for (i = 1201; i < 1232; i++)
31101 int column = DWARF_REG_TO_UNWIND_COLUMN (i);
31102 HOST_WIDE_INT offset
31103 = DWARF_FRAME_REGNUM (column) * GET_MODE_SIZE (mode);
31105 emit_move_insn (adjust_address (mem, mode, offset), value);
31109 if (TARGET_MACHO && ! TARGET_ALTIVEC)
31111 int i;
31112 enum machine_mode mode = TYPE_MODE (char_type_node);
31113 rtx addr = expand_expr (address, NULL_RTX, VOIDmode, EXPAND_NORMAL);
31114 rtx mem = gen_rtx_MEM (BLKmode, addr);
31115 rtx value = gen_int_mode (16, mode);
31117 /* On Darwin, libgcc may be built to run on both G3 and G4/5.
31118 The unwinder still needs to know the size of Altivec registers. */
31120 for (i = FIRST_ALTIVEC_REGNO; i < LAST_ALTIVEC_REGNO+1; i++)
31122 int column = DWARF_REG_TO_UNWIND_COLUMN (i);
31123 HOST_WIDE_INT offset
31124 = DWARF_FRAME_REGNUM (column) * GET_MODE_SIZE (mode);
31126 emit_move_insn (adjust_address (mem, mode, offset), value);
31131 /* Map internal gcc register numbers to DWARF2 register numbers. */
31133 unsigned int
31134 rs6000_dbx_register_number (unsigned int regno)
31136 if (regno <= 63 || write_symbols != DWARF2_DEBUG)
31137 return regno;
31138 if (regno == LR_REGNO)
31139 return 108;
31140 if (regno == CTR_REGNO)
31141 return 109;
31142 if (CR_REGNO_P (regno))
31143 return regno - CR0_REGNO + 86;
31144 if (regno == CA_REGNO)
31145 return 101; /* XER */
31146 if (ALTIVEC_REGNO_P (regno))
31147 return regno - FIRST_ALTIVEC_REGNO + 1124;
31148 if (regno == VRSAVE_REGNO)
31149 return 356;
31150 if (regno == VSCR_REGNO)
31151 return 67;
31152 if (regno == SPE_ACC_REGNO)
31153 return 99;
31154 if (regno == SPEFSCR_REGNO)
31155 return 612;
31156 /* SPE high reg number. We get these values of regno from
31157 rs6000_dwarf_register_span. */
31158 gcc_assert (regno >= 1200 && regno < 1232);
31159 return regno;
31162 /* target hook eh_return_filter_mode */
31163 static enum machine_mode
31164 rs6000_eh_return_filter_mode (void)
31166 return TARGET_32BIT ? SImode : word_mode;
31169 /* Target hook for scalar_mode_supported_p. */
31170 static bool
31171 rs6000_scalar_mode_supported_p (enum machine_mode mode)
31173 if (DECIMAL_FLOAT_MODE_P (mode))
31174 return default_decimal_float_supported_p ();
31175 else
31176 return default_scalar_mode_supported_p (mode);
31179 /* Target hook for vector_mode_supported_p. */
31180 static bool
31181 rs6000_vector_mode_supported_p (enum machine_mode mode)
31184 if (TARGET_PAIRED_FLOAT && PAIRED_VECTOR_MODE (mode))
31185 return true;
31187 if (TARGET_SPE && SPE_VECTOR_MODE (mode))
31188 return true;
31190 else if (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode))
31191 return true;
31193 else
31194 return false;
31197 /* Target hook for invalid_arg_for_unprototyped_fn. */
31198 static const char *
31199 invalid_arg_for_unprototyped_fn (const_tree typelist, const_tree funcdecl, const_tree val)
31201 return (!rs6000_darwin64_abi
31202 && typelist == 0
31203 && TREE_CODE (TREE_TYPE (val)) == VECTOR_TYPE
31204 && (funcdecl == NULL_TREE
31205 || (TREE_CODE (funcdecl) == FUNCTION_DECL
31206 && DECL_BUILT_IN_CLASS (funcdecl) != BUILT_IN_MD)))
31207 ? N_("AltiVec argument passed to unprototyped function")
31208 : NULL;
31211 /* For TARGET_SECURE_PLT 32-bit PIC code we can save PIC register
31212 setup by using __stack_chk_fail_local hidden function instead of
31213 calling __stack_chk_fail directly. Otherwise it is better to call
31214 __stack_chk_fail directly. */
31216 static tree ATTRIBUTE_UNUSED
31217 rs6000_stack_protect_fail (void)
31219 return (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
31220 ? default_hidden_stack_protect_fail ()
31221 : default_external_stack_protect_fail ();
31224 void
31225 rs6000_final_prescan_insn (rtx insn, rtx *operand ATTRIBUTE_UNUSED,
31226 int num_operands ATTRIBUTE_UNUSED)
31228 if (rs6000_warn_cell_microcode)
31230 const char *temp;
31231 int insn_code_number = recog_memoized (insn);
31232 location_t location = INSN_LOCATION (insn);
31234 /* Punt on insns we cannot recognize. */
31235 if (insn_code_number < 0)
31236 return;
31238 temp = get_insn_template (insn_code_number, insn);
31240 if (get_attr_cell_micro (insn) == CELL_MICRO_ALWAYS)
31241 warning_at (location, OPT_mwarn_cell_microcode,
31242 "emitting microcode insn %s\t[%s] #%d",
31243 temp, insn_data[INSN_CODE (insn)].name, INSN_UID (insn));
31244 else if (get_attr_cell_micro (insn) == CELL_MICRO_CONDITIONAL)
31245 warning_at (location, OPT_mwarn_cell_microcode,
31246 "emitting conditional microcode insn %s\t[%s] #%d",
31247 temp, insn_data[INSN_CODE (insn)].name, INSN_UID (insn));
31251 /* Implement the TARGET_ASAN_SHADOW_OFFSET hook. */
31253 #if TARGET_ELF
31254 static unsigned HOST_WIDE_INT
31255 rs6000_asan_shadow_offset (void)
31257 return (unsigned HOST_WIDE_INT) 1 << (TARGET_64BIT ? 41 : 29);
31259 #endif
31261 /* Mask options that we want to support inside of attribute((target)) and
31262 #pragma GCC target operations. Note, we do not include things like
31263 64/32-bit, endianess, hard/soft floating point, etc. that would have
31264 different calling sequences. */
31266 struct rs6000_opt_mask {
31267 const char *name; /* option name */
31268 HOST_WIDE_INT mask; /* mask to set */
31269 bool invert; /* invert sense of mask */
31270 bool valid_target; /* option is a target option */
31273 static struct rs6000_opt_mask const rs6000_opt_masks[] =
31275 { "altivec", OPTION_MASK_ALTIVEC, false, true },
31276 { "cmpb", OPTION_MASK_CMPB, false, true },
31277 { "crypto", OPTION_MASK_CRYPTO, false, true },
31278 { "direct-move", OPTION_MASK_DIRECT_MOVE, false, true },
31279 { "dlmzb", OPTION_MASK_DLMZB, false, true },
31280 { "fprnd", OPTION_MASK_FPRND, false, true },
31281 { "hard-dfp", OPTION_MASK_DFP, false, true },
31282 { "htm", OPTION_MASK_HTM, false, true },
31283 { "isel", OPTION_MASK_ISEL, false, true },
31284 { "mfcrf", OPTION_MASK_MFCRF, false, true },
31285 { "mfpgpr", OPTION_MASK_MFPGPR, false, true },
31286 { "mulhw", OPTION_MASK_MULHW, false, true },
31287 { "multiple", OPTION_MASK_MULTIPLE, false, true },
31288 { "popcntb", OPTION_MASK_POPCNTB, false, true },
31289 { "popcntd", OPTION_MASK_POPCNTD, false, true },
31290 { "power8-fusion", OPTION_MASK_P8_FUSION, false, true },
31291 { "power8-fusion-sign", OPTION_MASK_P8_FUSION_SIGN, false, true },
31292 { "power8-vector", OPTION_MASK_P8_VECTOR, false, true },
31293 { "powerpc-gfxopt", OPTION_MASK_PPC_GFXOPT, false, true },
31294 { "powerpc-gpopt", OPTION_MASK_PPC_GPOPT, false, true },
31295 { "quad-memory", OPTION_MASK_QUAD_MEMORY, false, true },
31296 { "quad-memory-atomic", OPTION_MASK_QUAD_MEMORY_ATOMIC, false, true },
31297 { "recip-precision", OPTION_MASK_RECIP_PRECISION, false, true },
31298 { "string", OPTION_MASK_STRING, false, true },
31299 { "update", OPTION_MASK_NO_UPDATE, true , true },
31300 { "upper-regs-df", OPTION_MASK_UPPER_REGS_DF, false, false },
31301 { "upper-regs-sf", OPTION_MASK_UPPER_REGS_SF, false, false },
31302 { "vsx", OPTION_MASK_VSX, false, true },
31303 { "vsx-timode", OPTION_MASK_VSX_TIMODE, false, true },
31304 #ifdef OPTION_MASK_64BIT
31305 #if TARGET_AIX_OS
31306 { "aix64", OPTION_MASK_64BIT, false, false },
31307 { "aix32", OPTION_MASK_64BIT, true, false },
31308 #else
31309 { "64", OPTION_MASK_64BIT, false, false },
31310 { "32", OPTION_MASK_64BIT, true, false },
31311 #endif
31312 #endif
31313 #ifdef OPTION_MASK_EABI
31314 { "eabi", OPTION_MASK_EABI, false, false },
31315 #endif
31316 #ifdef OPTION_MASK_LITTLE_ENDIAN
31317 { "little", OPTION_MASK_LITTLE_ENDIAN, false, false },
31318 { "big", OPTION_MASK_LITTLE_ENDIAN, true, false },
31319 #endif
31320 #ifdef OPTION_MASK_RELOCATABLE
31321 { "relocatable", OPTION_MASK_RELOCATABLE, false, false },
31322 #endif
31323 #ifdef OPTION_MASK_STRICT_ALIGN
31324 { "strict-align", OPTION_MASK_STRICT_ALIGN, false, false },
31325 #endif
31326 { "soft-float", OPTION_MASK_SOFT_FLOAT, false, false },
31327 { "string", OPTION_MASK_STRING, false, false },
31330 /* Builtin mask mapping for printing the flags. */
31331 static struct rs6000_opt_mask const rs6000_builtin_mask_names[] =
31333 { "altivec", RS6000_BTM_ALTIVEC, false, false },
31334 { "vsx", RS6000_BTM_VSX, false, false },
31335 { "spe", RS6000_BTM_SPE, false, false },
31336 { "paired", RS6000_BTM_PAIRED, false, false },
31337 { "fre", RS6000_BTM_FRE, false, false },
31338 { "fres", RS6000_BTM_FRES, false, false },
31339 { "frsqrte", RS6000_BTM_FRSQRTE, false, false },
31340 { "frsqrtes", RS6000_BTM_FRSQRTES, false, false },
31341 { "popcntd", RS6000_BTM_POPCNTD, false, false },
31342 { "cell", RS6000_BTM_CELL, false, false },
31343 { "power8-vector", RS6000_BTM_P8_VECTOR, false, false },
31344 { "crypto", RS6000_BTM_CRYPTO, false, false },
31345 { "htm", RS6000_BTM_HTM, false, false },
31346 { "hard-dfp", RS6000_BTM_DFP, false, false },
31347 { "hard-float", RS6000_BTM_HARD_FLOAT, false, false },
31350 /* Option variables that we want to support inside attribute((target)) and
31351 #pragma GCC target operations. */
31353 struct rs6000_opt_var {
31354 const char *name; /* option name */
31355 size_t global_offset; /* offset of the option in global_options. */
31356 size_t target_offset; /* offset of the option in target optiosn. */
31359 static struct rs6000_opt_var const rs6000_opt_vars[] =
31361 { "friz",
31362 offsetof (struct gcc_options, x_TARGET_FRIZ),
31363 offsetof (struct cl_target_option, x_TARGET_FRIZ), },
31364 { "avoid-indexed-addresses",
31365 offsetof (struct gcc_options, x_TARGET_AVOID_XFORM),
31366 offsetof (struct cl_target_option, x_TARGET_AVOID_XFORM) },
31367 { "paired",
31368 offsetof (struct gcc_options, x_rs6000_paired_float),
31369 offsetof (struct cl_target_option, x_rs6000_paired_float), },
31370 { "longcall",
31371 offsetof (struct gcc_options, x_rs6000_default_long_calls),
31372 offsetof (struct cl_target_option, x_rs6000_default_long_calls), },
31375 /* Inner function to handle attribute((target("..."))) and #pragma GCC target
31376 parsing. Return true if there were no errors. */
31378 static bool
31379 rs6000_inner_target_options (tree args, bool attr_p)
31381 bool ret = true;
31383 if (args == NULL_TREE)
31386 else if (TREE_CODE (args) == STRING_CST)
31388 char *p = ASTRDUP (TREE_STRING_POINTER (args));
31389 char *q;
31391 while ((q = strtok (p, ",")) != NULL)
31393 bool error_p = false;
31394 bool not_valid_p = false;
31395 const char *cpu_opt = NULL;
31397 p = NULL;
31398 if (strncmp (q, "cpu=", 4) == 0)
31400 int cpu_index = rs6000_cpu_name_lookup (q+4);
31401 if (cpu_index >= 0)
31402 rs6000_cpu_index = cpu_index;
31403 else
31405 error_p = true;
31406 cpu_opt = q+4;
31409 else if (strncmp (q, "tune=", 5) == 0)
31411 int tune_index = rs6000_cpu_name_lookup (q+5);
31412 if (tune_index >= 0)
31413 rs6000_tune_index = tune_index;
31414 else
31416 error_p = true;
31417 cpu_opt = q+5;
31420 else
31422 size_t i;
31423 bool invert = false;
31424 char *r = q;
31426 error_p = true;
31427 if (strncmp (r, "no-", 3) == 0)
31429 invert = true;
31430 r += 3;
31433 for (i = 0; i < ARRAY_SIZE (rs6000_opt_masks); i++)
31434 if (strcmp (r, rs6000_opt_masks[i].name) == 0)
31436 HOST_WIDE_INT mask = rs6000_opt_masks[i].mask;
31438 if (!rs6000_opt_masks[i].valid_target)
31439 not_valid_p = true;
31440 else
31442 error_p = false;
31443 rs6000_isa_flags_explicit |= mask;
31445 /* VSX needs altivec, so -mvsx automagically sets
31446 altivec. */
31447 if (mask == OPTION_MASK_VSX && !invert)
31448 mask |= OPTION_MASK_ALTIVEC;
31450 if (rs6000_opt_masks[i].invert)
31451 invert = !invert;
31453 if (invert)
31454 rs6000_isa_flags &= ~mask;
31455 else
31456 rs6000_isa_flags |= mask;
31458 break;
31461 if (error_p && !not_valid_p)
31463 for (i = 0; i < ARRAY_SIZE (rs6000_opt_vars); i++)
31464 if (strcmp (r, rs6000_opt_vars[i].name) == 0)
31466 size_t j = rs6000_opt_vars[i].global_offset;
31467 *((int *) ((char *)&global_options + j)) = !invert;
31468 error_p = false;
31469 break;
31474 if (error_p)
31476 const char *eprefix, *esuffix;
31478 ret = false;
31479 if (attr_p)
31481 eprefix = "__attribute__((__target__(";
31482 esuffix = ")))";
31484 else
31486 eprefix = "#pragma GCC target ";
31487 esuffix = "";
31490 if (cpu_opt)
31491 error ("invalid cpu \"%s\" for %s\"%s\"%s", cpu_opt, eprefix,
31492 q, esuffix);
31493 else if (not_valid_p)
31494 error ("%s\"%s\"%s is not allowed", eprefix, q, esuffix);
31495 else
31496 error ("%s\"%s\"%s is invalid", eprefix, q, esuffix);
31501 else if (TREE_CODE (args) == TREE_LIST)
31505 tree value = TREE_VALUE (args);
31506 if (value)
31508 bool ret2 = rs6000_inner_target_options (value, attr_p);
31509 if (!ret2)
31510 ret = false;
31512 args = TREE_CHAIN (args);
31514 while (args != NULL_TREE);
31517 else
31518 gcc_unreachable ();
31520 return ret;
31523 /* Print out the target options as a list for -mdebug=target. */
31525 static void
31526 rs6000_debug_target_options (tree args, const char *prefix)
31528 if (args == NULL_TREE)
31529 fprintf (stderr, "%s<NULL>", prefix);
31531 else if (TREE_CODE (args) == STRING_CST)
31533 char *p = ASTRDUP (TREE_STRING_POINTER (args));
31534 char *q;
31536 while ((q = strtok (p, ",")) != NULL)
31538 p = NULL;
31539 fprintf (stderr, "%s\"%s\"", prefix, q);
31540 prefix = ", ";
31544 else if (TREE_CODE (args) == TREE_LIST)
31548 tree value = TREE_VALUE (args);
31549 if (value)
31551 rs6000_debug_target_options (value, prefix);
31552 prefix = ", ";
31554 args = TREE_CHAIN (args);
31556 while (args != NULL_TREE);
31559 else
31560 gcc_unreachable ();
31562 return;
31566 /* Hook to validate attribute((target("..."))). */
31568 static bool
31569 rs6000_valid_attribute_p (tree fndecl,
31570 tree ARG_UNUSED (name),
31571 tree args,
31572 int flags)
31574 struct cl_target_option cur_target;
31575 bool ret;
31576 tree old_optimize = build_optimization_node (&global_options);
31577 tree new_target, new_optimize;
31578 tree func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
31580 gcc_assert ((fndecl != NULL_TREE) && (args != NULL_TREE));
31582 if (TARGET_DEBUG_TARGET)
31584 tree tname = DECL_NAME (fndecl);
31585 fprintf (stderr, "\n==================== rs6000_valid_attribute_p:\n");
31586 if (tname)
31587 fprintf (stderr, "function: %.*s\n",
31588 (int) IDENTIFIER_LENGTH (tname),
31589 IDENTIFIER_POINTER (tname));
31590 else
31591 fprintf (stderr, "function: unknown\n");
31593 fprintf (stderr, "args:");
31594 rs6000_debug_target_options (args, " ");
31595 fprintf (stderr, "\n");
31597 if (flags)
31598 fprintf (stderr, "flags: 0x%x\n", flags);
31600 fprintf (stderr, "--------------------\n");
31603 old_optimize = build_optimization_node (&global_options);
31604 func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
31606 /* If the function changed the optimization levels as well as setting target
31607 options, start with the optimizations specified. */
31608 if (func_optimize && func_optimize != old_optimize)
31609 cl_optimization_restore (&global_options,
31610 TREE_OPTIMIZATION (func_optimize));
31612 /* The target attributes may also change some optimization flags, so update
31613 the optimization options if necessary. */
31614 cl_target_option_save (&cur_target, &global_options);
31615 rs6000_cpu_index = rs6000_tune_index = -1;
31616 ret = rs6000_inner_target_options (args, true);
31618 /* Set up any additional state. */
31619 if (ret)
31621 ret = rs6000_option_override_internal (false);
31622 new_target = build_target_option_node (&global_options);
31624 else
31625 new_target = NULL;
31627 new_optimize = build_optimization_node (&global_options);
31629 if (!new_target)
31630 ret = false;
31632 else if (fndecl)
31634 DECL_FUNCTION_SPECIFIC_TARGET (fndecl) = new_target;
31636 if (old_optimize != new_optimize)
31637 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl) = new_optimize;
31640 cl_target_option_restore (&global_options, &cur_target);
31642 if (old_optimize != new_optimize)
31643 cl_optimization_restore (&global_options,
31644 TREE_OPTIMIZATION (old_optimize));
31646 return ret;
31650 /* Hook to validate the current #pragma GCC target and set the state, and
31651 update the macros based on what was changed. If ARGS is NULL, then
31652 POP_TARGET is used to reset the options. */
31654 bool
31655 rs6000_pragma_target_parse (tree args, tree pop_target)
31657 tree prev_tree = build_target_option_node (&global_options);
31658 tree cur_tree;
31659 struct cl_target_option *prev_opt, *cur_opt;
31660 HOST_WIDE_INT prev_flags, cur_flags, diff_flags;
31661 HOST_WIDE_INT prev_bumask, cur_bumask, diff_bumask;
31663 if (TARGET_DEBUG_TARGET)
31665 fprintf (stderr, "\n==================== rs6000_pragma_target_parse\n");
31666 fprintf (stderr, "args:");
31667 rs6000_debug_target_options (args, " ");
31668 fprintf (stderr, "\n");
31670 if (pop_target)
31672 fprintf (stderr, "pop_target:\n");
31673 debug_tree (pop_target);
31675 else
31676 fprintf (stderr, "pop_target: <NULL>\n");
31678 fprintf (stderr, "--------------------\n");
31681 if (! args)
31683 cur_tree = ((pop_target)
31684 ? pop_target
31685 : target_option_default_node);
31686 cl_target_option_restore (&global_options,
31687 TREE_TARGET_OPTION (cur_tree));
31689 else
31691 rs6000_cpu_index = rs6000_tune_index = -1;
31692 if (!rs6000_inner_target_options (args, false)
31693 || !rs6000_option_override_internal (false)
31694 || (cur_tree = build_target_option_node (&global_options))
31695 == NULL_TREE)
31697 if (TARGET_DEBUG_BUILTIN || TARGET_DEBUG_TARGET)
31698 fprintf (stderr, "invalid pragma\n");
31700 return false;
31704 target_option_current_node = cur_tree;
31706 /* If we have the preprocessor linked in (i.e. C or C++ languages), possibly
31707 change the macros that are defined. */
31708 if (rs6000_target_modify_macros_ptr)
31710 prev_opt = TREE_TARGET_OPTION (prev_tree);
31711 prev_bumask = prev_opt->x_rs6000_builtin_mask;
31712 prev_flags = prev_opt->x_rs6000_isa_flags;
31714 cur_opt = TREE_TARGET_OPTION (cur_tree);
31715 cur_flags = cur_opt->x_rs6000_isa_flags;
31716 cur_bumask = cur_opt->x_rs6000_builtin_mask;
31718 diff_bumask = (prev_bumask ^ cur_bumask);
31719 diff_flags = (prev_flags ^ cur_flags);
31721 if ((diff_flags != 0) || (diff_bumask != 0))
31723 /* Delete old macros. */
31724 rs6000_target_modify_macros_ptr (false,
31725 prev_flags & diff_flags,
31726 prev_bumask & diff_bumask);
31728 /* Define new macros. */
31729 rs6000_target_modify_macros_ptr (true,
31730 cur_flags & diff_flags,
31731 cur_bumask & diff_bumask);
31735 return true;
31739 /* Remember the last target of rs6000_set_current_function. */
31740 static GTY(()) tree rs6000_previous_fndecl;
31742 /* Establish appropriate back-end context for processing the function
31743 FNDECL. The argument might be NULL to indicate processing at top
31744 level, outside of any function scope. */
31745 static void
31746 rs6000_set_current_function (tree fndecl)
31748 tree old_tree = (rs6000_previous_fndecl
31749 ? DECL_FUNCTION_SPECIFIC_TARGET (rs6000_previous_fndecl)
31750 : NULL_TREE);
31752 tree new_tree = (fndecl
31753 ? DECL_FUNCTION_SPECIFIC_TARGET (fndecl)
31754 : NULL_TREE);
31756 if (TARGET_DEBUG_TARGET)
31758 bool print_final = false;
31759 fprintf (stderr, "\n==================== rs6000_set_current_function");
31761 if (fndecl)
31762 fprintf (stderr, ", fndecl %s (%p)",
31763 (DECL_NAME (fndecl)
31764 ? IDENTIFIER_POINTER (DECL_NAME (fndecl))
31765 : "<unknown>"), (void *)fndecl);
31767 if (rs6000_previous_fndecl)
31768 fprintf (stderr, ", prev_fndecl (%p)", (void *)rs6000_previous_fndecl);
31770 fprintf (stderr, "\n");
31771 if (new_tree)
31773 fprintf (stderr, "\nnew fndecl target specific options:\n");
31774 debug_tree (new_tree);
31775 print_final = true;
31778 if (old_tree)
31780 fprintf (stderr, "\nold fndecl target specific options:\n");
31781 debug_tree (old_tree);
31782 print_final = true;
31785 if (print_final)
31786 fprintf (stderr, "--------------------\n");
31789 /* Only change the context if the function changes. This hook is called
31790 several times in the course of compiling a function, and we don't want to
31791 slow things down too much or call target_reinit when it isn't safe. */
31792 if (fndecl && fndecl != rs6000_previous_fndecl)
31794 rs6000_previous_fndecl = fndecl;
31795 if (old_tree == new_tree)
31798 else if (new_tree)
31800 cl_target_option_restore (&global_options,
31801 TREE_TARGET_OPTION (new_tree));
31802 if (TREE_TARGET_GLOBALS (new_tree))
31803 restore_target_globals (TREE_TARGET_GLOBALS (new_tree));
31804 else
31805 TREE_TARGET_GLOBALS (new_tree)
31806 = save_target_globals_default_opts ();
31809 else if (old_tree)
31811 new_tree = target_option_current_node;
31812 cl_target_option_restore (&global_options,
31813 TREE_TARGET_OPTION (new_tree));
31814 if (TREE_TARGET_GLOBALS (new_tree))
31815 restore_target_globals (TREE_TARGET_GLOBALS (new_tree));
31816 else if (new_tree == target_option_default_node)
31817 restore_target_globals (&default_target_globals);
31818 else
31819 TREE_TARGET_GLOBALS (new_tree)
31820 = save_target_globals_default_opts ();
31826 /* Save the current options */
31828 static void
31829 rs6000_function_specific_save (struct cl_target_option *ptr,
31830 struct gcc_options *opts)
31832 ptr->x_rs6000_isa_flags = opts->x_rs6000_isa_flags;
31833 ptr->x_rs6000_isa_flags_explicit = opts->x_rs6000_isa_flags_explicit;
31836 /* Restore the current options */
31838 static void
31839 rs6000_function_specific_restore (struct gcc_options *opts,
31840 struct cl_target_option *ptr)
31843 opts->x_rs6000_isa_flags = ptr->x_rs6000_isa_flags;
31844 opts->x_rs6000_isa_flags_explicit = ptr->x_rs6000_isa_flags_explicit;
31845 (void) rs6000_option_override_internal (false);
31848 /* Print the current options */
31850 static void
31851 rs6000_function_specific_print (FILE *file, int indent,
31852 struct cl_target_option *ptr)
31854 rs6000_print_isa_options (file, indent, "Isa options set",
31855 ptr->x_rs6000_isa_flags);
31857 rs6000_print_isa_options (file, indent, "Isa options explicit",
31858 ptr->x_rs6000_isa_flags_explicit);
31861 /* Helper function to print the current isa or misc options on a line. */
31863 static void
31864 rs6000_print_options_internal (FILE *file,
31865 int indent,
31866 const char *string,
31867 HOST_WIDE_INT flags,
31868 const char *prefix,
31869 const struct rs6000_opt_mask *opts,
31870 size_t num_elements)
31872 size_t i;
31873 size_t start_column = 0;
31874 size_t cur_column;
31875 size_t max_column = 76;
31876 const char *comma = "";
31878 if (indent)
31879 start_column += fprintf (file, "%*s", indent, "");
31881 if (!flags)
31883 fprintf (stderr, DEBUG_FMT_S, string, "<none>");
31884 return;
31887 start_column += fprintf (stderr, DEBUG_FMT_WX, string, flags);
31889 /* Print the various mask options. */
31890 cur_column = start_column;
31891 for (i = 0; i < num_elements; i++)
31893 if ((flags & opts[i].mask) != 0)
31895 const char *no_str = rs6000_opt_masks[i].invert ? "no-" : "";
31896 size_t len = (strlen (comma)
31897 + strlen (prefix)
31898 + strlen (no_str)
31899 + strlen (rs6000_opt_masks[i].name));
31901 cur_column += len;
31902 if (cur_column > max_column)
31904 fprintf (stderr, ", \\\n%*s", (int)start_column, "");
31905 cur_column = start_column + len;
31906 comma = "";
31909 fprintf (file, "%s%s%s%s", comma, prefix, no_str,
31910 rs6000_opt_masks[i].name);
31911 flags &= ~ opts[i].mask;
31912 comma = ", ";
31916 fputs ("\n", file);
31919 /* Helper function to print the current isa options on a line. */
31921 static void
31922 rs6000_print_isa_options (FILE *file, int indent, const char *string,
31923 HOST_WIDE_INT flags)
31925 rs6000_print_options_internal (file, indent, string, flags, "-m",
31926 &rs6000_opt_masks[0],
31927 ARRAY_SIZE (rs6000_opt_masks));
31930 static void
31931 rs6000_print_builtin_options (FILE *file, int indent, const char *string,
31932 HOST_WIDE_INT flags)
31934 rs6000_print_options_internal (file, indent, string, flags, "",
31935 &rs6000_builtin_mask_names[0],
31936 ARRAY_SIZE (rs6000_builtin_mask_names));
31940 /* Hook to determine if one function can safely inline another. */
31942 static bool
31943 rs6000_can_inline_p (tree caller, tree callee)
31945 bool ret = false;
31946 tree caller_tree = DECL_FUNCTION_SPECIFIC_TARGET (caller);
31947 tree callee_tree = DECL_FUNCTION_SPECIFIC_TARGET (callee);
31949 /* If callee has no option attributes, then it is ok to inline. */
31950 if (!callee_tree)
31951 ret = true;
31953 /* If caller has no option attributes, but callee does then it is not ok to
31954 inline. */
31955 else if (!caller_tree)
31956 ret = false;
31958 else
31960 struct cl_target_option *caller_opts = TREE_TARGET_OPTION (caller_tree);
31961 struct cl_target_option *callee_opts = TREE_TARGET_OPTION (callee_tree);
31963 /* Callee's options should a subset of the caller's, i.e. a vsx function
31964 can inline an altivec function but a non-vsx function can't inline a
31965 vsx function. */
31966 if ((caller_opts->x_rs6000_isa_flags & callee_opts->x_rs6000_isa_flags)
31967 == callee_opts->x_rs6000_isa_flags)
31968 ret = true;
31971 if (TARGET_DEBUG_TARGET)
31972 fprintf (stderr, "rs6000_can_inline_p:, caller %s, callee %s, %s inline\n",
31973 (DECL_NAME (caller)
31974 ? IDENTIFIER_POINTER (DECL_NAME (caller))
31975 : "<unknown>"),
31976 (DECL_NAME (callee)
31977 ? IDENTIFIER_POINTER (DECL_NAME (callee))
31978 : "<unknown>"),
31979 (ret ? "can" : "cannot"));
31981 return ret;
31984 /* Allocate a stack temp and fixup the address so it meets the particular
31985 memory requirements (either offetable or REG+REG addressing). */
31988 rs6000_allocate_stack_temp (enum machine_mode mode,
31989 bool offsettable_p,
31990 bool reg_reg_p)
31992 rtx stack = assign_stack_temp (mode, GET_MODE_SIZE (mode));
31993 rtx addr = XEXP (stack, 0);
31994 int strict_p = (reload_in_progress || reload_completed);
31996 if (!legitimate_indirect_address_p (addr, strict_p))
31998 if (offsettable_p
31999 && !rs6000_legitimate_offset_address_p (mode, addr, strict_p, true))
32000 stack = replace_equiv_address (stack, copy_addr_to_reg (addr));
32002 else if (reg_reg_p && !legitimate_indexed_address_p (addr, strict_p))
32003 stack = replace_equiv_address (stack, copy_addr_to_reg (addr));
32006 return stack;
32009 /* Given a memory reference, if it is not a reg or reg+reg addressing, convert
32010 to such a form to deal with memory reference instructions like STFIWX that
32011 only take reg+reg addressing. */
32014 rs6000_address_for_fpconvert (rtx x)
32016 int strict_p = (reload_in_progress || reload_completed);
32017 rtx addr;
32019 gcc_assert (MEM_P (x));
32020 addr = XEXP (x, 0);
32021 if (! legitimate_indirect_address_p (addr, strict_p)
32022 && ! legitimate_indexed_address_p (addr, strict_p))
32024 if (GET_CODE (addr) == PRE_INC || GET_CODE (addr) == PRE_DEC)
32026 rtx reg = XEXP (addr, 0);
32027 HOST_WIDE_INT size = GET_MODE_SIZE (GET_MODE (x));
32028 rtx size_rtx = GEN_INT ((GET_CODE (addr) == PRE_DEC) ? -size : size);
32029 gcc_assert (REG_P (reg));
32030 emit_insn (gen_add3_insn (reg, reg, size_rtx));
32031 addr = reg;
32033 else if (GET_CODE (addr) == PRE_MODIFY)
32035 rtx reg = XEXP (addr, 0);
32036 rtx expr = XEXP (addr, 1);
32037 gcc_assert (REG_P (reg));
32038 gcc_assert (GET_CODE (expr) == PLUS);
32039 emit_insn (gen_add3_insn (reg, XEXP (expr, 0), XEXP (expr, 1)));
32040 addr = reg;
32043 x = replace_equiv_address (x, copy_addr_to_reg (addr));
32046 return x;
32049 /* Given a memory reference, if it is not in the form for altivec memory
32050 reference instructions (i.e. reg or reg+reg addressing with AND of -16),
32051 convert to the altivec format. */
32054 rs6000_address_for_altivec (rtx x)
32056 gcc_assert (MEM_P (x));
32057 if (!altivec_indexed_or_indirect_operand (x, GET_MODE (x)))
32059 rtx addr = XEXP (x, 0);
32060 int strict_p = (reload_in_progress || reload_completed);
32062 if (!legitimate_indexed_address_p (addr, strict_p)
32063 && !legitimate_indirect_address_p (addr, strict_p))
32064 addr = copy_to_mode_reg (Pmode, addr);
32066 addr = gen_rtx_AND (Pmode, addr, GEN_INT (-16));
32067 x = change_address (x, GET_MODE (x), addr);
32070 return x;
32073 /* Implement TARGET_LEGITIMATE_CONSTANT_P.
32075 On the RS/6000, all integer constants are acceptable, most won't be valid
32076 for particular insns, though. Only easy FP constants are acceptable. */
32078 static bool
32079 rs6000_legitimate_constant_p (enum machine_mode mode, rtx x)
32081 if (TARGET_ELF && rs6000_tls_referenced_p (x))
32082 return false;
32084 return ((GET_CODE (x) != CONST_DOUBLE && GET_CODE (x) != CONST_VECTOR)
32085 || GET_MODE (x) == VOIDmode
32086 || (TARGET_POWERPC64 && mode == DImode)
32087 || easy_fp_constant (x, mode)
32088 || easy_vector_constant (x, mode));
32093 /* Expand code to perform a call under the AIX or ELFv2 ABI. */
32095 void
32096 rs6000_call_aix (rtx value, rtx func_desc, rtx flag, rtx cookie)
32098 rtx toc_reg = gen_rtx_REG (Pmode, TOC_REGNUM);
32099 rtx toc_load = NULL_RTX;
32100 rtx toc_restore = NULL_RTX;
32101 rtx func_addr;
32102 rtx abi_reg = NULL_RTX;
32103 rtx call[4];
32104 int n_call;
32105 rtx insn;
32107 /* Handle longcall attributes. */
32108 if (INTVAL (cookie) & CALL_LONG)
32109 func_desc = rs6000_longcall_ref (func_desc);
32111 /* Handle indirect calls. */
32112 if (GET_CODE (func_desc) != SYMBOL_REF
32113 || (DEFAULT_ABI == ABI_AIX && !SYMBOL_REF_FUNCTION_P (func_desc)))
32115 /* Save the TOC into its reserved slot before the call,
32116 and prepare to restore it after the call. */
32117 rtx stack_ptr = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
32118 rtx stack_toc_offset = GEN_INT (RS6000_TOC_SAVE_SLOT);
32119 rtx stack_toc_mem = gen_frame_mem (Pmode,
32120 gen_rtx_PLUS (Pmode, stack_ptr,
32121 stack_toc_offset));
32122 toc_restore = gen_rtx_SET (VOIDmode, toc_reg, stack_toc_mem);
32124 /* Can we optimize saving the TOC in the prologue or
32125 do we need to do it at every call? */
32126 if (TARGET_SAVE_TOC_INDIRECT && !cfun->calls_alloca)
32127 cfun->machine->save_toc_in_prologue = true;
32128 else
32130 MEM_VOLATILE_P (stack_toc_mem) = 1;
32131 emit_move_insn (stack_toc_mem, toc_reg);
32134 if (DEFAULT_ABI == ABI_ELFv2)
32136 /* A function pointer in the ELFv2 ABI is just a plain address, but
32137 the ABI requires it to be loaded into r12 before the call. */
32138 func_addr = gen_rtx_REG (Pmode, 12);
32139 emit_move_insn (func_addr, func_desc);
32140 abi_reg = func_addr;
32142 else
32144 /* A function pointer under AIX is a pointer to a data area whose
32145 first word contains the actual address of the function, whose
32146 second word contains a pointer to its TOC, and whose third word
32147 contains a value to place in the static chain register (r11).
32148 Note that if we load the static chain, our "trampoline" need
32149 not have any executable code. */
32151 /* Load up address of the actual function. */
32152 func_desc = force_reg (Pmode, func_desc);
32153 func_addr = gen_reg_rtx (Pmode);
32154 emit_move_insn (func_addr, gen_rtx_MEM (Pmode, func_desc));
32156 /* Prepare to load the TOC of the called function. Note that the
32157 TOC load must happen immediately before the actual call so
32158 that unwinding the TOC registers works correctly. See the
32159 comment in frob_update_context. */
32160 rtx func_toc_offset = GEN_INT (GET_MODE_SIZE (Pmode));
32161 rtx func_toc_mem = gen_rtx_MEM (Pmode,
32162 gen_rtx_PLUS (Pmode, func_desc,
32163 func_toc_offset));
32164 toc_load = gen_rtx_USE (VOIDmode, func_toc_mem);
32166 /* If we have a static chain, load it up. */
32167 if (TARGET_POINTERS_TO_NESTED_FUNCTIONS)
32169 rtx sc_reg = gen_rtx_REG (Pmode, STATIC_CHAIN_REGNUM);
32170 rtx func_sc_offset = GEN_INT (2 * GET_MODE_SIZE (Pmode));
32171 rtx func_sc_mem = gen_rtx_MEM (Pmode,
32172 gen_rtx_PLUS (Pmode, func_desc,
32173 func_sc_offset));
32174 emit_move_insn (sc_reg, func_sc_mem);
32175 abi_reg = sc_reg;
32179 else
32181 /* Direct calls use the TOC: for local calls, the callee will
32182 assume the TOC register is set; for non-local calls, the
32183 PLT stub needs the TOC register. */
32184 abi_reg = toc_reg;
32185 func_addr = func_desc;
32188 /* Create the call. */
32189 call[0] = gen_rtx_CALL (VOIDmode, gen_rtx_MEM (SImode, func_addr), flag);
32190 if (value != NULL_RTX)
32191 call[0] = gen_rtx_SET (VOIDmode, value, call[0]);
32192 n_call = 1;
32194 if (toc_load)
32195 call[n_call++] = toc_load;
32196 if (toc_restore)
32197 call[n_call++] = toc_restore;
32199 call[n_call++] = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, LR_REGNO));
32201 insn = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (n_call, call));
32202 insn = emit_call_insn (insn);
32204 /* Mention all registers defined by the ABI to hold information
32205 as uses in CALL_INSN_FUNCTION_USAGE. */
32206 if (abi_reg)
32207 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), abi_reg);
32210 /* Expand code to perform a sibling call under the AIX or ELFv2 ABI. */
32212 void
32213 rs6000_sibcall_aix (rtx value, rtx func_desc, rtx flag, rtx cookie)
32215 rtx call[2];
32216 rtx insn;
32218 gcc_assert (INTVAL (cookie) == 0);
32220 /* Create the call. */
32221 call[0] = gen_rtx_CALL (VOIDmode, gen_rtx_MEM (SImode, func_desc), flag);
32222 if (value != NULL_RTX)
32223 call[0] = gen_rtx_SET (VOIDmode, value, call[0]);
32225 call[1] = simple_return_rtx;
32227 insn = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (2, call));
32228 insn = emit_call_insn (insn);
32230 /* Note use of the TOC register. */
32231 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), gen_rtx_REG (Pmode, TOC_REGNUM));
32232 /* We need to also mark a use of the link register since the function we
32233 sibling-call to will use it to return to our caller. */
32234 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), gen_rtx_REG (Pmode, LR_REGNO));
32237 /* Return whether we need to always update the saved TOC pointer when we update
32238 the stack pointer. */
32240 static bool
32241 rs6000_save_toc_in_prologue_p (void)
32243 return (cfun && cfun->machine && cfun->machine->save_toc_in_prologue);
32246 #ifdef HAVE_GAS_HIDDEN
32247 # define USE_HIDDEN_LINKONCE 1
32248 #else
32249 # define USE_HIDDEN_LINKONCE 0
32250 #endif
32252 /* Fills in the label name that should be used for a 476 link stack thunk. */
32254 void
32255 get_ppc476_thunk_name (char name[32])
32257 gcc_assert (TARGET_LINK_STACK);
32259 if (USE_HIDDEN_LINKONCE)
32260 sprintf (name, "__ppc476.get_thunk");
32261 else
32262 ASM_GENERATE_INTERNAL_LABEL (name, "LPPC476_", 0);
32265 /* This function emits the simple thunk routine that is used to preserve
32266 the link stack on the 476 cpu. */
32268 static void rs6000_code_end (void) ATTRIBUTE_UNUSED;
32269 static void
32270 rs6000_code_end (void)
32272 char name[32];
32273 tree decl;
32275 if (!TARGET_LINK_STACK)
32276 return;
32278 get_ppc476_thunk_name (name);
32280 decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL, get_identifier (name),
32281 build_function_type_list (void_type_node, NULL_TREE));
32282 DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
32283 NULL_TREE, void_type_node);
32284 TREE_PUBLIC (decl) = 1;
32285 TREE_STATIC (decl) = 1;
32287 #if RS6000_WEAK
32288 if (USE_HIDDEN_LINKONCE)
32290 DECL_COMDAT_GROUP (decl) = DECL_ASSEMBLER_NAME (decl);
32291 targetm.asm_out.unique_section (decl, 0);
32292 switch_to_section (get_named_section (decl, NULL, 0));
32293 DECL_WEAK (decl) = 1;
32294 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
32295 targetm.asm_out.globalize_label (asm_out_file, name);
32296 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
32297 ASM_DECLARE_FUNCTION_NAME (asm_out_file, name, decl);
32299 else
32300 #endif
32302 switch_to_section (text_section);
32303 ASM_OUTPUT_LABEL (asm_out_file, name);
32306 DECL_INITIAL (decl) = make_node (BLOCK);
32307 current_function_decl = decl;
32308 init_function_start (decl);
32309 first_function_block_is_cold = false;
32310 /* Make sure unwind info is emitted for the thunk if needed. */
32311 final_start_function (emit_barrier (), asm_out_file, 1);
32313 fputs ("\tblr\n", asm_out_file);
32315 final_end_function ();
32316 init_insn_lengths ();
32317 free_after_compilation (cfun);
32318 set_cfun (NULL);
32319 current_function_decl = NULL;
32322 /* Add r30 to hard reg set if the prologue sets it up and it is not
32323 pic_offset_table_rtx. */
32325 static void
32326 rs6000_set_up_by_prologue (struct hard_reg_set_container *set)
32328 if (!TARGET_SINGLE_PIC_BASE
32329 && TARGET_TOC
32330 && TARGET_MINIMAL_TOC
32331 && get_pool_size () != 0)
32332 add_to_hard_reg_set (&set->set, Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
32336 /* Helper function for rs6000_split_logical to emit a logical instruction after
32337 spliting the operation to single GPR registers.
32339 DEST is the destination register.
32340 OP1 and OP2 are the input source registers.
32341 CODE is the base operation (AND, IOR, XOR, NOT).
32342 MODE is the machine mode.
32343 If COMPLEMENT_FINAL_P is true, wrap the whole operation with NOT.
32344 If COMPLEMENT_OP1_P is true, wrap operand1 with NOT.
32345 If COMPLEMENT_OP2_P is true, wrap operand2 with NOT.
32346 CLOBBER_REG is either NULL or a scratch register of type CC to allow
32347 formation of the AND instructions. */
32349 static void
32350 rs6000_split_logical_inner (rtx dest,
32351 rtx op1,
32352 rtx op2,
32353 enum rtx_code code,
32354 enum machine_mode mode,
32355 bool complement_final_p,
32356 bool complement_op1_p,
32357 bool complement_op2_p,
32358 rtx clobber_reg)
32360 rtx bool_rtx;
32361 rtx set_rtx;
32363 /* Optimize AND of 0/0xffffffff and IOR/XOR of 0. */
32364 if (op2 && GET_CODE (op2) == CONST_INT
32365 && (mode == SImode || (mode == DImode && TARGET_POWERPC64))
32366 && !complement_final_p && !complement_op1_p && !complement_op2_p)
32368 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
32369 HOST_WIDE_INT value = INTVAL (op2) & mask;
32371 /* Optimize AND of 0 to just set 0. Optimize AND of -1 to be a move. */
32372 if (code == AND)
32374 if (value == 0)
32376 emit_insn (gen_rtx_SET (VOIDmode, dest, const0_rtx));
32377 return;
32380 else if (value == mask)
32382 if (!rtx_equal_p (dest, op1))
32383 emit_insn (gen_rtx_SET (VOIDmode, dest, op1));
32384 return;
32388 /* Optimize IOR/XOR of 0 to be a simple move. Split large operations
32389 into separate ORI/ORIS or XORI/XORIS instrucitons. */
32390 else if (code == IOR || code == XOR)
32392 if (value == 0)
32394 if (!rtx_equal_p (dest, op1))
32395 emit_insn (gen_rtx_SET (VOIDmode, dest, op1));
32396 return;
32401 if (complement_op1_p)
32402 op1 = gen_rtx_NOT (mode, op1);
32404 if (complement_op2_p)
32405 op2 = gen_rtx_NOT (mode, op2);
32407 bool_rtx = ((code == NOT)
32408 ? gen_rtx_NOT (mode, op1)
32409 : gen_rtx_fmt_ee (code, mode, op1, op2));
32411 if (complement_final_p)
32412 bool_rtx = gen_rtx_NOT (mode, bool_rtx);
32414 set_rtx = gen_rtx_SET (VOIDmode, dest, bool_rtx);
32416 /* Is this AND with an explicit clobber? */
32417 if (clobber_reg)
32419 rtx clobber = gen_rtx_CLOBBER (VOIDmode, clobber_reg);
32420 set_rtx = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set_rtx, clobber));
32423 emit_insn (set_rtx);
32424 return;
32427 /* Split a DImode AND/IOR/XOR with a constant on a 32-bit system. These
32428 operations are split immediately during RTL generation to allow for more
32429 optimizations of the AND/IOR/XOR.
32431 OPERANDS is an array containing the destination and two input operands.
32432 CODE is the base operation (AND, IOR, XOR, NOT).
32433 MODE is the machine mode.
32434 If COMPLEMENT_FINAL_P is true, wrap the whole operation with NOT.
32435 If COMPLEMENT_OP1_P is true, wrap operand1 with NOT.
32436 If COMPLEMENT_OP2_P is true, wrap operand2 with NOT.
32437 CLOBBER_REG is either NULL or a scratch register of type CC to allow
32438 formation of the AND instructions. */
32440 static void
32441 rs6000_split_logical_di (rtx operands[3],
32442 enum rtx_code code,
32443 bool complement_final_p,
32444 bool complement_op1_p,
32445 bool complement_op2_p,
32446 rtx clobber_reg)
32448 const HOST_WIDE_INT lower_32bits = HOST_WIDE_INT_C(0xffffffff);
32449 const HOST_WIDE_INT upper_32bits = ~ lower_32bits;
32450 const HOST_WIDE_INT sign_bit = HOST_WIDE_INT_C(0x80000000);
32451 enum hi_lo { hi = 0, lo = 1 };
32452 rtx op0_hi_lo[2], op1_hi_lo[2], op2_hi_lo[2];
32453 size_t i;
32455 op0_hi_lo[hi] = gen_highpart (SImode, operands[0]);
32456 op1_hi_lo[hi] = gen_highpart (SImode, operands[1]);
32457 op0_hi_lo[lo] = gen_lowpart (SImode, operands[0]);
32458 op1_hi_lo[lo] = gen_lowpart (SImode, operands[1]);
32460 if (code == NOT)
32461 op2_hi_lo[hi] = op2_hi_lo[lo] = NULL_RTX;
32462 else
32464 if (GET_CODE (operands[2]) != CONST_INT)
32466 op2_hi_lo[hi] = gen_highpart_mode (SImode, DImode, operands[2]);
32467 op2_hi_lo[lo] = gen_lowpart (SImode, operands[2]);
32469 else
32471 HOST_WIDE_INT value = INTVAL (operands[2]);
32472 HOST_WIDE_INT value_hi_lo[2];
32474 gcc_assert (!complement_final_p);
32475 gcc_assert (!complement_op1_p);
32476 gcc_assert (!complement_op2_p);
32478 value_hi_lo[hi] = value >> 32;
32479 value_hi_lo[lo] = value & lower_32bits;
32481 for (i = 0; i < 2; i++)
32483 HOST_WIDE_INT sub_value = value_hi_lo[i];
32485 if (sub_value & sign_bit)
32486 sub_value |= upper_32bits;
32488 op2_hi_lo[i] = GEN_INT (sub_value);
32490 /* If this is an AND instruction, check to see if we need to load
32491 the value in a register. */
32492 if (code == AND && sub_value != -1 && sub_value != 0
32493 && !and_operand (op2_hi_lo[i], SImode))
32494 op2_hi_lo[i] = force_reg (SImode, op2_hi_lo[i]);
32499 for (i = 0; i < 2; i++)
32501 /* Split large IOR/XOR operations. */
32502 if ((code == IOR || code == XOR)
32503 && GET_CODE (op2_hi_lo[i]) == CONST_INT
32504 && !complement_final_p
32505 && !complement_op1_p
32506 && !complement_op2_p
32507 && clobber_reg == NULL_RTX
32508 && !logical_const_operand (op2_hi_lo[i], SImode))
32510 HOST_WIDE_INT value = INTVAL (op2_hi_lo[i]);
32511 HOST_WIDE_INT hi_16bits = value & HOST_WIDE_INT_C(0xffff0000);
32512 HOST_WIDE_INT lo_16bits = value & HOST_WIDE_INT_C(0x0000ffff);
32513 rtx tmp = gen_reg_rtx (SImode);
32515 /* Make sure the constant is sign extended. */
32516 if ((hi_16bits & sign_bit) != 0)
32517 hi_16bits |= upper_32bits;
32519 rs6000_split_logical_inner (tmp, op1_hi_lo[i], GEN_INT (hi_16bits),
32520 code, SImode, false, false, false,
32521 NULL_RTX);
32523 rs6000_split_logical_inner (op0_hi_lo[i], tmp, GEN_INT (lo_16bits),
32524 code, SImode, false, false, false,
32525 NULL_RTX);
32527 else
32528 rs6000_split_logical_inner (op0_hi_lo[i], op1_hi_lo[i], op2_hi_lo[i],
32529 code, SImode, complement_final_p,
32530 complement_op1_p, complement_op2_p,
32531 clobber_reg);
32534 return;
32537 /* Split the insns that make up boolean operations operating on multiple GPR
32538 registers. The boolean MD patterns ensure that the inputs either are
32539 exactly the same as the output registers, or there is no overlap.
32541 OPERANDS is an array containing the destination and two input operands.
32542 CODE is the base operation (AND, IOR, XOR, NOT).
32543 MODE is the machine mode.
32544 If COMPLEMENT_FINAL_P is true, wrap the whole operation with NOT.
32545 If COMPLEMENT_OP1_P is true, wrap operand1 with NOT.
32546 If COMPLEMENT_OP2_P is true, wrap operand2 with NOT.
32547 CLOBBER_REG is either NULL or a scratch register of type CC to allow
32548 formation of the AND instructions. */
32550 void
32551 rs6000_split_logical (rtx operands[3],
32552 enum rtx_code code,
32553 bool complement_final_p,
32554 bool complement_op1_p,
32555 bool complement_op2_p,
32556 rtx clobber_reg)
32558 enum machine_mode mode = GET_MODE (operands[0]);
32559 enum machine_mode sub_mode;
32560 rtx op0, op1, op2;
32561 int sub_size, regno0, regno1, nregs, i;
32563 /* If this is DImode, use the specialized version that can run before
32564 register allocation. */
32565 if (mode == DImode && !TARGET_POWERPC64)
32567 rs6000_split_logical_di (operands, code, complement_final_p,
32568 complement_op1_p, complement_op2_p,
32569 clobber_reg);
32570 return;
32573 op0 = operands[0];
32574 op1 = operands[1];
32575 op2 = (code == NOT) ? NULL_RTX : operands[2];
32576 sub_mode = (TARGET_POWERPC64) ? DImode : SImode;
32577 sub_size = GET_MODE_SIZE (sub_mode);
32578 regno0 = REGNO (op0);
32579 regno1 = REGNO (op1);
32581 gcc_assert (reload_completed);
32582 gcc_assert (IN_RANGE (regno0, FIRST_GPR_REGNO, LAST_GPR_REGNO));
32583 gcc_assert (IN_RANGE (regno1, FIRST_GPR_REGNO, LAST_GPR_REGNO));
32585 nregs = rs6000_hard_regno_nregs[(int)mode][regno0];
32586 gcc_assert (nregs > 1);
32588 if (op2 && REG_P (op2))
32589 gcc_assert (IN_RANGE (REGNO (op2), FIRST_GPR_REGNO, LAST_GPR_REGNO));
32591 for (i = 0; i < nregs; i++)
32593 int offset = i * sub_size;
32594 rtx sub_op0 = simplify_subreg (sub_mode, op0, mode, offset);
32595 rtx sub_op1 = simplify_subreg (sub_mode, op1, mode, offset);
32596 rtx sub_op2 = ((code == NOT)
32597 ? NULL_RTX
32598 : simplify_subreg (sub_mode, op2, mode, offset));
32600 rs6000_split_logical_inner (sub_op0, sub_op1, sub_op2, code, sub_mode,
32601 complement_final_p, complement_op1_p,
32602 complement_op2_p, clobber_reg);
32605 return;
32609 /* Return true if the peephole2 can combine a load involving a combination of
32610 an addis instruction and a load with an offset that can be fused together on
32611 a power8.
32613 The operands are:
32614 operands[0] register set with addis
32615 operands[1] value set via addis
32616 operands[2] target register being loaded
32617 operands[3] D-form memory reference using operands[0].
32619 In addition, we are passed a boolean that is true if this is a peephole2,
32620 and we can use see if the addis_reg is dead after the insn and can be
32621 replaced by the target register. */
32623 bool
32624 fusion_gpr_load_p (rtx *operands, bool peep2_p)
32626 rtx addis_reg = operands[0];
32627 rtx addis_value = operands[1];
32628 rtx target = operands[2];
32629 rtx mem = operands[3];
32630 rtx addr;
32631 rtx base_reg;
32633 /* Validate arguments. */
32634 if (!base_reg_operand (addis_reg, GET_MODE (addis_reg)))
32635 return false;
32637 if (!base_reg_operand (target, GET_MODE (target)))
32638 return false;
32640 if (!fusion_gpr_addis (addis_value, GET_MODE (addis_value)))
32641 return false;
32643 if (!fusion_gpr_mem_load (mem, GET_MODE (mem)))
32644 return false;
32646 /* Allow sign/zero extension. */
32647 if (GET_CODE (mem) == ZERO_EXTEND
32648 || (GET_CODE (mem) == SIGN_EXTEND && TARGET_P8_FUSION_SIGN))
32649 mem = XEXP (mem, 0);
32651 if (!MEM_P (mem))
32652 return false;
32654 addr = XEXP (mem, 0); /* either PLUS or LO_SUM. */
32655 if (GET_CODE (addr) != PLUS && GET_CODE (addr) != LO_SUM)
32656 return false;
32658 /* Validate that the register used to load the high value is either the
32659 register being loaded, or we can safely replace its use in a peephole2.
32661 If this is a peephole2, we assume that there are 2 instructions in the
32662 peephole (addis and load), so we want to check if the target register was
32663 not used in the memory address and the register to hold the addis result
32664 is dead after the peephole. */
32665 if (REGNO (addis_reg) != REGNO (target))
32667 if (!peep2_p)
32668 return false;
32670 if (reg_mentioned_p (target, mem))
32671 return false;
32673 if (!peep2_reg_dead_p (2, addis_reg))
32674 return false;
32676 /* If the target register being loaded is the stack pointer, we must
32677 avoid loading any other value into it, even temporarily. */
32678 if (REG_P (target) && REGNO (target) == STACK_POINTER_REGNUM)
32679 return false;
32682 base_reg = XEXP (addr, 0);
32683 return REGNO (addis_reg) == REGNO (base_reg);
32686 /* During the peephole2 pass, adjust and expand the insns for a load fusion
32687 sequence. We adjust the addis register to use the target register. If the
32688 load sign extends, we adjust the code to do the zero extending load, and an
32689 explicit sign extension later since the fusion only covers zero extending
32690 loads.
32692 The operands are:
32693 operands[0] register set with addis (to be replaced with target)
32694 operands[1] value set via addis
32695 operands[2] target register being loaded
32696 operands[3] D-form memory reference using operands[0]. */
32698 void
32699 expand_fusion_gpr_load (rtx *operands)
32701 rtx addis_value = operands[1];
32702 rtx target = operands[2];
32703 rtx orig_mem = operands[3];
32704 rtx new_addr, new_mem, orig_addr, offset;
32705 enum rtx_code plus_or_lo_sum;
32706 enum machine_mode target_mode = GET_MODE (target);
32707 enum machine_mode extend_mode = target_mode;
32708 enum machine_mode ptr_mode = Pmode;
32709 enum rtx_code extend = UNKNOWN;
32710 rtx addis_reg = ((ptr_mode == target_mode)
32711 ? target
32712 : simplify_subreg (ptr_mode, target, target_mode, 0));
32714 if (GET_CODE (orig_mem) == ZERO_EXTEND
32715 || (TARGET_P8_FUSION_SIGN && GET_CODE (orig_mem) == SIGN_EXTEND))
32717 extend = GET_CODE (orig_mem);
32718 orig_mem = XEXP (orig_mem, 0);
32719 target_mode = GET_MODE (orig_mem);
32722 gcc_assert (MEM_P (orig_mem));
32724 orig_addr = XEXP (orig_mem, 0);
32725 plus_or_lo_sum = GET_CODE (orig_addr);
32726 gcc_assert (plus_or_lo_sum == PLUS || plus_or_lo_sum == LO_SUM);
32728 offset = XEXP (orig_addr, 1);
32729 new_addr = gen_rtx_fmt_ee (plus_or_lo_sum, ptr_mode, addis_reg, offset);
32730 new_mem = change_address (orig_mem, target_mode, new_addr);
32732 if (extend != UNKNOWN)
32733 new_mem = gen_rtx_fmt_e (ZERO_EXTEND, extend_mode, new_mem);
32735 emit_insn (gen_rtx_SET (VOIDmode, addis_reg, addis_value));
32736 emit_insn (gen_rtx_SET (VOIDmode, target, new_mem));
32738 if (extend == SIGN_EXTEND)
32740 int sub_off = ((BYTES_BIG_ENDIAN)
32741 ? GET_MODE_SIZE (extend_mode) - GET_MODE_SIZE (target_mode)
32742 : 0);
32743 rtx sign_reg
32744 = simplify_subreg (target_mode, target, extend_mode, sub_off);
32746 emit_insn (gen_rtx_SET (VOIDmode, target,
32747 gen_rtx_SIGN_EXTEND (extend_mode, sign_reg)));
32750 return;
32753 /* Return a string to fuse an addis instruction with a gpr load to the same
32754 register that we loaded up the addis instruction. The code is complicated,
32755 so we call output_asm_insn directly, and just return "".
32757 The operands are:
32758 operands[0] register set with addis (must be same reg as target).
32759 operands[1] value set via addis
32760 operands[2] target register being loaded
32761 operands[3] D-form memory reference using operands[0]. */
32763 const char *
32764 emit_fusion_gpr_load (rtx *operands)
32766 rtx addis_reg = operands[0];
32767 rtx addis_value = operands[1];
32768 rtx target = operands[2];
32769 rtx mem = operands[3];
32770 rtx fuse_ops[10];
32771 rtx addr;
32772 rtx load_offset;
32773 const char *addis_str = NULL;
32774 const char *load_str = NULL;
32775 const char *extend_insn = NULL;
32776 const char *mode_name = NULL;
32777 char insn_template[80];
32778 enum machine_mode mode;
32779 const char *comment_str = ASM_COMMENT_START;
32780 bool sign_p = false;
32782 gcc_assert (REG_P (addis_reg) && REG_P (target));
32783 gcc_assert (REGNO (addis_reg) == REGNO (target));
32785 if (*comment_str == ' ')
32786 comment_str++;
32788 /* Allow sign/zero extension. */
32789 if (GET_CODE (mem) == ZERO_EXTEND)
32790 mem = XEXP (mem, 0);
32792 else if (GET_CODE (mem) == SIGN_EXTEND && TARGET_P8_FUSION_SIGN)
32794 sign_p = true;
32795 mem = XEXP (mem, 0);
32798 gcc_assert (MEM_P (mem));
32799 addr = XEXP (mem, 0);
32800 if (GET_CODE (addr) != PLUS && GET_CODE (addr) != LO_SUM)
32801 gcc_unreachable ();
32803 load_offset = XEXP (addr, 1);
32805 /* Now emit the load instruction to the same register. */
32806 mode = GET_MODE (mem);
32807 switch (mode)
32809 case QImode:
32810 mode_name = "char";
32811 load_str = "lbz";
32812 extend_insn = "extsb %0,%0";
32813 break;
32815 case HImode:
32816 mode_name = "short";
32817 load_str = "lhz";
32818 extend_insn = "extsh %0,%0";
32819 break;
32821 case SImode:
32822 mode_name = "int";
32823 load_str = "lwz";
32824 extend_insn = "extsw %0,%0";
32825 break;
32827 case DImode:
32828 if (TARGET_POWERPC64)
32830 mode_name = "long";
32831 load_str = "ld";
32833 else
32834 gcc_unreachable ();
32835 break;
32837 default:
32838 gcc_unreachable ();
32841 /* Emit the addis instruction. */
32842 fuse_ops[0] = target;
32843 if (satisfies_constraint_L (addis_value))
32845 fuse_ops[1] = addis_value;
32846 addis_str = "lis %0,%v1";
32849 else if (GET_CODE (addis_value) == PLUS)
32851 rtx op0 = XEXP (addis_value, 0);
32852 rtx op1 = XEXP (addis_value, 1);
32854 if (REG_P (op0) && CONST_INT_P (op1)
32855 && satisfies_constraint_L (op1))
32857 fuse_ops[1] = op0;
32858 fuse_ops[2] = op1;
32859 addis_str = "addis %0,%1,%v2";
32863 else if (GET_CODE (addis_value) == HIGH)
32865 rtx value = XEXP (addis_value, 0);
32866 if (GET_CODE (value) == UNSPEC && XINT (value, 1) == UNSPEC_TOCREL)
32868 fuse_ops[1] = XVECEXP (value, 0, 0); /* symbol ref. */
32869 fuse_ops[2] = XVECEXP (value, 0, 1); /* TOC register. */
32870 if (TARGET_ELF)
32871 addis_str = "addis %0,%2,%1@toc@ha";
32873 else if (TARGET_XCOFF)
32874 addis_str = "addis %0,%1@u(%2)";
32876 else
32877 gcc_unreachable ();
32880 else if (GET_CODE (value) == PLUS)
32882 rtx op0 = XEXP (value, 0);
32883 rtx op1 = XEXP (value, 1);
32885 if (GET_CODE (op0) == UNSPEC
32886 && XINT (op0, 1) == UNSPEC_TOCREL
32887 && CONST_INT_P (op1))
32889 fuse_ops[1] = XVECEXP (op0, 0, 0); /* symbol ref. */
32890 fuse_ops[2] = XVECEXP (op0, 0, 1); /* TOC register. */
32891 fuse_ops[3] = op1;
32892 if (TARGET_ELF)
32893 addis_str = "addis %0,%2,%1+%3@toc@ha";
32895 else if (TARGET_XCOFF)
32896 addis_str = "addis %0,%1+%3@u(%2)";
32898 else
32899 gcc_unreachable ();
32903 else if (satisfies_constraint_L (value))
32905 fuse_ops[1] = value;
32906 addis_str = "lis %0,%v1";
32909 else if (TARGET_ELF && !TARGET_POWERPC64 && CONSTANT_P (value))
32911 fuse_ops[1] = value;
32912 addis_str = "lis %0,%1@ha";
32916 if (!addis_str)
32917 fatal_insn ("Could not generate addis value for fusion", addis_value);
32919 sprintf (insn_template, "%s\t\t%s gpr load fusion, type %s", addis_str,
32920 comment_str, mode_name);
32921 output_asm_insn (insn_template, fuse_ops);
32923 /* Emit the D-form load instruction. */
32924 if (CONST_INT_P (load_offset) && satisfies_constraint_I (load_offset))
32926 sprintf (insn_template, "%s %%0,%%1(%%0)", load_str);
32927 fuse_ops[1] = load_offset;
32928 output_asm_insn (insn_template, fuse_ops);
32931 else if (GET_CODE (load_offset) == UNSPEC
32932 && XINT (load_offset, 1) == UNSPEC_TOCREL)
32934 if (TARGET_ELF)
32935 sprintf (insn_template, "%s %%0,%%1@toc@l(%%0)", load_str);
32937 else if (TARGET_XCOFF)
32938 sprintf (insn_template, "%s %%0,%%1@l(%%0)", load_str);
32940 else
32941 gcc_unreachable ();
32943 fuse_ops[1] = XVECEXP (load_offset, 0, 0);
32944 output_asm_insn (insn_template, fuse_ops);
32947 else if (GET_CODE (load_offset) == PLUS
32948 && GET_CODE (XEXP (load_offset, 0)) == UNSPEC
32949 && XINT (XEXP (load_offset, 0), 1) == UNSPEC_TOCREL
32950 && CONST_INT_P (XEXP (load_offset, 1)))
32952 rtx tocrel_unspec = XEXP (load_offset, 0);
32953 if (TARGET_ELF)
32954 sprintf (insn_template, "%s %%0,%%1+%%2@toc@l(%%0)", load_str);
32956 else if (TARGET_XCOFF)
32957 sprintf (insn_template, "%s %%0,%%1+%%2@l(%%0)", load_str);
32959 else
32960 gcc_unreachable ();
32962 fuse_ops[1] = XVECEXP (tocrel_unspec, 0, 0);
32963 fuse_ops[2] = XEXP (load_offset, 1);
32964 output_asm_insn (insn_template, fuse_ops);
32967 else if (TARGET_ELF && !TARGET_POWERPC64 && CONSTANT_P (load_offset))
32969 sprintf (insn_template, "%s %%0,%%1@l(%%0)", load_str);
32971 fuse_ops[1] = load_offset;
32972 output_asm_insn (insn_template, fuse_ops);
32975 else
32976 fatal_insn ("Unable to generate load offset for fusion", load_offset);
32978 /* Handle sign extension. The peephole2 pass generates this as a separate
32979 insn, but we handle it just in case it got reattached. */
32980 if (sign_p)
32982 gcc_assert (extend_insn != NULL);
32983 output_asm_insn (extend_insn, fuse_ops);
32986 return "";
32990 struct gcc_target targetm = TARGET_INITIALIZER;
32992 #include "gt-rs6000.h"