Merged revisions 209304,209307,209332,209338-209339,209343,209346,209351,209354,20936...
[official-gcc.git] / gcc-4_9 / gcc / config / rs6000 / rs6000.c
blob6dcf440b7a5511553a8c4a1e7136385c03f68bef
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 /* SDmode is special in that we want to access it only via REG+REG
2351 addressing on power7 and above, since we want to use the LFIWZX and
2352 STFIWZX instructions to load it. */
2353 bool indexed_only_p = (m == SDmode && TARGET_NO_SDMODE_STACK);
2355 any_addr_mask = 0;
2356 for (rc = FIRST_RELOAD_REG_CLASS; rc <= LAST_RELOAD_REG_CLASS; rc++)
2358 addr_mask = 0;
2359 reg = reload_reg_map[rc].reg;
2361 /* Can mode values go in the GPR/FPR/Altivec registers? */
2362 if (reg >= 0 && rs6000_hard_regno_mode_ok_p[m][reg])
2364 nregs = rs6000_hard_regno_nregs[m][reg];
2365 addr_mask |= RELOAD_REG_VALID;
2367 /* Indicate if the mode takes more than 1 physical register. If
2368 it takes a single register, indicate it can do REG+REG
2369 addressing. */
2370 if (nregs > 1 || m == BLKmode)
2371 addr_mask |= RELOAD_REG_MULTIPLE;
2372 else
2373 addr_mask |= RELOAD_REG_INDEXED;
2375 /* Figure out if we can do PRE_INC, PRE_DEC, or PRE_MODIFY
2376 addressing. Restrict addressing on SPE for 64-bit types
2377 because of the SUBREG hackery used to address 64-bit floats in
2378 '32-bit' GPRs. To simplify secondary reload, don't allow
2379 update forms on scalar floating point types that can go in the
2380 upper registers. */
2382 if (TARGET_UPDATE
2383 && (rc == RELOAD_REG_GPR || rc == RELOAD_REG_FPR)
2384 && GET_MODE_SIZE (m) <= 8
2385 && !VECTOR_MODE_P (m)
2386 && !COMPLEX_MODE_P (m)
2387 && !indexed_only_p
2388 && !(TARGET_E500_DOUBLE && GET_MODE_SIZE (m) == 8)
2389 && !(m == DFmode && TARGET_UPPER_REGS_DF)
2390 && !(m == SFmode && TARGET_UPPER_REGS_SF))
2392 addr_mask |= RELOAD_REG_PRE_INCDEC;
2394 /* PRE_MODIFY is more restricted than PRE_INC/PRE_DEC in that
2395 we don't allow PRE_MODIFY for some multi-register
2396 operations. */
2397 switch (m)
2399 default:
2400 addr_mask |= RELOAD_REG_PRE_MODIFY;
2401 break;
2403 case DImode:
2404 if (TARGET_POWERPC64)
2405 addr_mask |= RELOAD_REG_PRE_MODIFY;
2406 break;
2408 case DFmode:
2409 case DDmode:
2410 if (TARGET_DF_INSN)
2411 addr_mask |= RELOAD_REG_PRE_MODIFY;
2412 break;
2417 /* GPR and FPR registers can do REG+OFFSET addressing, except
2418 possibly for SDmode. */
2419 if ((addr_mask != 0) && !indexed_only_p
2420 && (rc == RELOAD_REG_GPR || rc == RELOAD_REG_FPR))
2421 addr_mask |= RELOAD_REG_OFFSET;
2423 reg_addr[m].addr_mask[rc] = addr_mask;
2424 any_addr_mask |= addr_mask;
2427 reg_addr[m].addr_mask[RELOAD_REG_ANY] = any_addr_mask;
2432 /* Initialize the various global tables that are based on register size. */
2433 static void
2434 rs6000_init_hard_regno_mode_ok (bool global_init_p)
2436 ssize_t r, m, c;
2437 int align64;
2438 int align32;
2440 /* Precalculate REGNO_REG_CLASS. */
2441 rs6000_regno_regclass[0] = GENERAL_REGS;
2442 for (r = 1; r < 32; ++r)
2443 rs6000_regno_regclass[r] = BASE_REGS;
2445 for (r = 32; r < 64; ++r)
2446 rs6000_regno_regclass[r] = FLOAT_REGS;
2448 for (r = 64; r < FIRST_PSEUDO_REGISTER; ++r)
2449 rs6000_regno_regclass[r] = NO_REGS;
2451 for (r = FIRST_ALTIVEC_REGNO; r <= LAST_ALTIVEC_REGNO; ++r)
2452 rs6000_regno_regclass[r] = ALTIVEC_REGS;
2454 rs6000_regno_regclass[CR0_REGNO] = CR0_REGS;
2455 for (r = CR1_REGNO; r <= CR7_REGNO; ++r)
2456 rs6000_regno_regclass[r] = CR_REGS;
2458 rs6000_regno_regclass[LR_REGNO] = LINK_REGS;
2459 rs6000_regno_regclass[CTR_REGNO] = CTR_REGS;
2460 rs6000_regno_regclass[CA_REGNO] = CA_REGS;
2461 rs6000_regno_regclass[VRSAVE_REGNO] = VRSAVE_REGS;
2462 rs6000_regno_regclass[VSCR_REGNO] = VRSAVE_REGS;
2463 rs6000_regno_regclass[SPE_ACC_REGNO] = SPE_ACC_REGS;
2464 rs6000_regno_regclass[SPEFSCR_REGNO] = SPEFSCR_REGS;
2465 rs6000_regno_regclass[TFHAR_REGNO] = SPR_REGS;
2466 rs6000_regno_regclass[TFIAR_REGNO] = SPR_REGS;
2467 rs6000_regno_regclass[TEXASR_REGNO] = SPR_REGS;
2468 rs6000_regno_regclass[ARG_POINTER_REGNUM] = BASE_REGS;
2469 rs6000_regno_regclass[FRAME_POINTER_REGNUM] = BASE_REGS;
2471 /* Precalculate register class to simpler reload register class. We don't
2472 need all of the register classes that are combinations of different
2473 classes, just the simple ones that have constraint letters. */
2474 for (c = 0; c < N_REG_CLASSES; c++)
2475 reg_class_to_reg_type[c] = NO_REG_TYPE;
2477 reg_class_to_reg_type[(int)GENERAL_REGS] = GPR_REG_TYPE;
2478 reg_class_to_reg_type[(int)BASE_REGS] = GPR_REG_TYPE;
2479 reg_class_to_reg_type[(int)VSX_REGS] = VSX_REG_TYPE;
2480 reg_class_to_reg_type[(int)VRSAVE_REGS] = SPR_REG_TYPE;
2481 reg_class_to_reg_type[(int)VSCR_REGS] = SPR_REG_TYPE;
2482 reg_class_to_reg_type[(int)LINK_REGS] = SPR_REG_TYPE;
2483 reg_class_to_reg_type[(int)CTR_REGS] = SPR_REG_TYPE;
2484 reg_class_to_reg_type[(int)LINK_OR_CTR_REGS] = SPR_REG_TYPE;
2485 reg_class_to_reg_type[(int)CR_REGS] = CR_REG_TYPE;
2486 reg_class_to_reg_type[(int)CR0_REGS] = CR_REG_TYPE;
2487 reg_class_to_reg_type[(int)SPE_ACC_REGS] = SPE_ACC_TYPE;
2488 reg_class_to_reg_type[(int)SPEFSCR_REGS] = SPEFSCR_REG_TYPE;
2490 if (TARGET_VSX)
2492 reg_class_to_reg_type[(int)FLOAT_REGS] = VSX_REG_TYPE;
2493 reg_class_to_reg_type[(int)ALTIVEC_REGS] = VSX_REG_TYPE;
2495 else
2497 reg_class_to_reg_type[(int)FLOAT_REGS] = FPR_REG_TYPE;
2498 reg_class_to_reg_type[(int)ALTIVEC_REGS] = ALTIVEC_REG_TYPE;
2501 /* Precalculate the valid memory formats as well as the vector information,
2502 this must be set up before the rs6000_hard_regno_nregs_internal calls
2503 below. */
2504 gcc_assert ((int)VECTOR_NONE == 0);
2505 memset ((void *) &rs6000_vector_unit[0], '\0', sizeof (rs6000_vector_unit));
2506 memset ((void *) &rs6000_vector_mem[0], '\0', sizeof (rs6000_vector_unit));
2508 gcc_assert ((int)CODE_FOR_nothing == 0);
2509 memset ((void *) &reg_addr[0], '\0', sizeof (reg_addr));
2511 gcc_assert ((int)NO_REGS == 0);
2512 memset ((void *) &rs6000_constraints[0], '\0', sizeof (rs6000_constraints));
2514 /* The VSX hardware allows native alignment for vectors, but control whether the compiler
2515 believes it can use native alignment or still uses 128-bit alignment. */
2516 if (TARGET_VSX && !TARGET_VSX_ALIGN_128)
2518 align64 = 64;
2519 align32 = 32;
2521 else
2523 align64 = 128;
2524 align32 = 128;
2527 /* V2DF mode, VSX only. */
2528 if (TARGET_VSX)
2530 rs6000_vector_unit[V2DFmode] = VECTOR_VSX;
2531 rs6000_vector_mem[V2DFmode] = VECTOR_VSX;
2532 rs6000_vector_align[V2DFmode] = align64;
2535 /* V4SF mode, either VSX or Altivec. */
2536 if (TARGET_VSX)
2538 rs6000_vector_unit[V4SFmode] = VECTOR_VSX;
2539 rs6000_vector_mem[V4SFmode] = VECTOR_VSX;
2540 rs6000_vector_align[V4SFmode] = align32;
2542 else if (TARGET_ALTIVEC)
2544 rs6000_vector_unit[V4SFmode] = VECTOR_ALTIVEC;
2545 rs6000_vector_mem[V4SFmode] = VECTOR_ALTIVEC;
2546 rs6000_vector_align[V4SFmode] = align32;
2549 /* V16QImode, V8HImode, V4SImode are Altivec only, but possibly do VSX loads
2550 and stores. */
2551 if (TARGET_ALTIVEC)
2553 rs6000_vector_unit[V4SImode] = VECTOR_ALTIVEC;
2554 rs6000_vector_unit[V8HImode] = VECTOR_ALTIVEC;
2555 rs6000_vector_unit[V16QImode] = VECTOR_ALTIVEC;
2556 rs6000_vector_align[V4SImode] = align32;
2557 rs6000_vector_align[V8HImode] = align32;
2558 rs6000_vector_align[V16QImode] = align32;
2560 if (TARGET_VSX)
2562 rs6000_vector_mem[V4SImode] = VECTOR_VSX;
2563 rs6000_vector_mem[V8HImode] = VECTOR_VSX;
2564 rs6000_vector_mem[V16QImode] = VECTOR_VSX;
2566 else
2568 rs6000_vector_mem[V4SImode] = VECTOR_ALTIVEC;
2569 rs6000_vector_mem[V8HImode] = VECTOR_ALTIVEC;
2570 rs6000_vector_mem[V16QImode] = VECTOR_ALTIVEC;
2574 /* V2DImode, full mode depends on ISA 2.07 vector mode. Allow under VSX to
2575 do insert/splat/extract. Altivec doesn't have 64-bit integer support. */
2576 if (TARGET_VSX)
2578 rs6000_vector_mem[V2DImode] = VECTOR_VSX;
2579 rs6000_vector_unit[V2DImode]
2580 = (TARGET_P8_VECTOR) ? VECTOR_P8_VECTOR : VECTOR_NONE;
2581 rs6000_vector_align[V2DImode] = align64;
2583 rs6000_vector_mem[V1TImode] = VECTOR_VSX;
2584 rs6000_vector_unit[V1TImode]
2585 = (TARGET_P8_VECTOR) ? VECTOR_P8_VECTOR : VECTOR_NONE;
2586 rs6000_vector_align[V1TImode] = 128;
2589 /* DFmode, see if we want to use the VSX unit. */
2590 if (TARGET_VSX && TARGET_VSX_SCALAR_DOUBLE)
2592 rs6000_vector_unit[DFmode] = VECTOR_VSX;
2593 rs6000_vector_mem[DFmode]
2594 = (TARGET_UPPER_REGS_DF ? VECTOR_VSX : VECTOR_NONE);
2595 rs6000_vector_align[DFmode] = align64;
2598 /* Allow TImode in VSX register and set the VSX memory macros. */
2599 if (TARGET_VSX && TARGET_VSX_TIMODE)
2601 rs6000_vector_mem[TImode] = VECTOR_VSX;
2602 rs6000_vector_align[TImode] = align64;
2605 /* TODO add SPE and paired floating point vector support. */
2607 /* Register class constraints for the constraints that depend on compile
2608 switches. When the VSX code was added, different constraints were added
2609 based on the type (DFmode, V2DFmode, V4SFmode). For the vector types, all
2610 of the VSX registers are used. The register classes for scalar floating
2611 point types is set, based on whether we allow that type into the upper
2612 (Altivec) registers. GCC has register classes to target the Altivec
2613 registers for load/store operations, to select using a VSX memory
2614 operation instead of the traditional floating point operation. The
2615 constraints are:
2617 d - Register class to use with traditional DFmode instructions.
2618 f - Register class to use with traditional SFmode instructions.
2619 v - Altivec register.
2620 wa - Any VSX register.
2621 wd - Preferred register class for V2DFmode.
2622 wf - Preferred register class for V4SFmode.
2623 wg - Float register for power6x move insns.
2624 wl - Float register if we can do 32-bit signed int loads.
2625 wm - VSX register for ISA 2.07 direct move operations.
2626 wr - GPR if 64-bit mode is permitted.
2627 ws - Register class to do ISA 2.06 DF operations.
2628 wu - Altivec register for ISA 2.07 VSX SF/SI load/stores.
2629 wv - Altivec register for ISA 2.06 VSX DF/DI load/stores.
2630 wt - VSX register for TImode in VSX registers.
2631 ww - Register class to do SF conversions in with VSX operations.
2632 wx - Float register if we can do 32-bit int stores.
2633 wy - Register class to do ISA 2.07 SF operations.
2634 wz - Float register if we can do 32-bit unsigned int loads. */
2636 if (TARGET_HARD_FLOAT && TARGET_FPRS)
2637 rs6000_constraints[RS6000_CONSTRAINT_f] = FLOAT_REGS;
2639 if (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
2640 rs6000_constraints[RS6000_CONSTRAINT_d] = FLOAT_REGS;
2642 if (TARGET_VSX)
2644 rs6000_constraints[RS6000_CONSTRAINT_wa] = VSX_REGS;
2645 rs6000_constraints[RS6000_CONSTRAINT_wd] = VSX_REGS;
2646 rs6000_constraints[RS6000_CONSTRAINT_wf] = VSX_REGS;
2648 if (TARGET_VSX_TIMODE)
2649 rs6000_constraints[RS6000_CONSTRAINT_wt] = VSX_REGS;
2651 if (TARGET_UPPER_REGS_DF)
2653 rs6000_constraints[RS6000_CONSTRAINT_ws] = VSX_REGS;
2654 rs6000_constraints[RS6000_CONSTRAINT_wv] = ALTIVEC_REGS;
2656 else
2657 rs6000_constraints[RS6000_CONSTRAINT_ws] = FLOAT_REGS;
2660 /* Add conditional constraints based on various options, to allow us to
2661 collapse multiple insn patterns. */
2662 if (TARGET_ALTIVEC)
2663 rs6000_constraints[RS6000_CONSTRAINT_v] = ALTIVEC_REGS;
2665 if (TARGET_MFPGPR)
2666 rs6000_constraints[RS6000_CONSTRAINT_wg] = FLOAT_REGS;
2668 if (TARGET_LFIWAX)
2669 rs6000_constraints[RS6000_CONSTRAINT_wl] = FLOAT_REGS;
2671 if (TARGET_DIRECT_MOVE)
2672 rs6000_constraints[RS6000_CONSTRAINT_wm] = VSX_REGS;
2674 if (TARGET_POWERPC64)
2675 rs6000_constraints[RS6000_CONSTRAINT_wr] = GENERAL_REGS;
2677 if (TARGET_P8_VECTOR && TARGET_UPPER_REGS_SF)
2679 rs6000_constraints[RS6000_CONSTRAINT_wu] = ALTIVEC_REGS;
2680 rs6000_constraints[RS6000_CONSTRAINT_wy] = VSX_REGS;
2681 rs6000_constraints[RS6000_CONSTRAINT_ww] = VSX_REGS;
2683 else if (TARGET_P8_VECTOR)
2685 rs6000_constraints[RS6000_CONSTRAINT_wy] = FLOAT_REGS;
2686 rs6000_constraints[RS6000_CONSTRAINT_ww] = FLOAT_REGS;
2688 else if (TARGET_VSX)
2689 rs6000_constraints[RS6000_CONSTRAINT_ww] = FLOAT_REGS;
2691 if (TARGET_STFIWX)
2692 rs6000_constraints[RS6000_CONSTRAINT_wx] = FLOAT_REGS;
2694 if (TARGET_LFIWZX)
2695 rs6000_constraints[RS6000_CONSTRAINT_wz] = FLOAT_REGS;
2697 /* Set up the reload helper and direct move functions. */
2698 if (TARGET_VSX || TARGET_ALTIVEC)
2700 if (TARGET_64BIT)
2702 reg_addr[V16QImode].reload_store = CODE_FOR_reload_v16qi_di_store;
2703 reg_addr[V16QImode].reload_load = CODE_FOR_reload_v16qi_di_load;
2704 reg_addr[V8HImode].reload_store = CODE_FOR_reload_v8hi_di_store;
2705 reg_addr[V8HImode].reload_load = CODE_FOR_reload_v8hi_di_load;
2706 reg_addr[V4SImode].reload_store = CODE_FOR_reload_v4si_di_store;
2707 reg_addr[V4SImode].reload_load = CODE_FOR_reload_v4si_di_load;
2708 reg_addr[V2DImode].reload_store = CODE_FOR_reload_v2di_di_store;
2709 reg_addr[V2DImode].reload_load = CODE_FOR_reload_v2di_di_load;
2710 reg_addr[V1TImode].reload_store = CODE_FOR_reload_v1ti_di_store;
2711 reg_addr[V1TImode].reload_load = CODE_FOR_reload_v1ti_di_load;
2712 reg_addr[V4SFmode].reload_store = CODE_FOR_reload_v4sf_di_store;
2713 reg_addr[V4SFmode].reload_load = CODE_FOR_reload_v4sf_di_load;
2714 reg_addr[V2DFmode].reload_store = CODE_FOR_reload_v2df_di_store;
2715 reg_addr[V2DFmode].reload_load = CODE_FOR_reload_v2df_di_load;
2716 if (TARGET_VSX && TARGET_UPPER_REGS_DF)
2718 reg_addr[DFmode].reload_store = CODE_FOR_reload_df_di_store;
2719 reg_addr[DFmode].reload_load = CODE_FOR_reload_df_di_load;
2720 reg_addr[DDmode].reload_store = CODE_FOR_reload_dd_di_store;
2721 reg_addr[DDmode].reload_load = CODE_FOR_reload_dd_di_load;
2723 if (TARGET_P8_VECTOR)
2725 reg_addr[SFmode].reload_store = CODE_FOR_reload_sf_di_store;
2726 reg_addr[SFmode].reload_load = CODE_FOR_reload_sf_di_load;
2727 reg_addr[SDmode].reload_store = CODE_FOR_reload_sd_di_store;
2728 reg_addr[SDmode].reload_load = CODE_FOR_reload_sd_di_load;
2730 if (TARGET_VSX_TIMODE)
2732 reg_addr[TImode].reload_store = CODE_FOR_reload_ti_di_store;
2733 reg_addr[TImode].reload_load = CODE_FOR_reload_ti_di_load;
2735 if (TARGET_DIRECT_MOVE)
2737 if (TARGET_POWERPC64)
2739 reg_addr[TImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxti;
2740 reg_addr[V1TImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv1ti;
2741 reg_addr[V2DFmode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv2df;
2742 reg_addr[V2DImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv2di;
2743 reg_addr[V4SFmode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv4sf;
2744 reg_addr[V4SImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv4si;
2745 reg_addr[V8HImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv8hi;
2746 reg_addr[V16QImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv16qi;
2747 reg_addr[SFmode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxsf;
2749 reg_addr[TImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprti;
2750 reg_addr[V1TImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv1ti;
2751 reg_addr[V2DFmode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv2df;
2752 reg_addr[V2DImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv2di;
2753 reg_addr[V4SFmode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv4sf;
2754 reg_addr[V4SImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv4si;
2755 reg_addr[V8HImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv8hi;
2756 reg_addr[V16QImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv16qi;
2757 reg_addr[SFmode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprsf;
2759 else
2761 reg_addr[DImode].reload_fpr_gpr = CODE_FOR_reload_fpr_from_gprdi;
2762 reg_addr[DDmode].reload_fpr_gpr = CODE_FOR_reload_fpr_from_gprdd;
2763 reg_addr[DFmode].reload_fpr_gpr = CODE_FOR_reload_fpr_from_gprdf;
2767 else
2769 reg_addr[V16QImode].reload_store = CODE_FOR_reload_v16qi_si_store;
2770 reg_addr[V16QImode].reload_load = CODE_FOR_reload_v16qi_si_load;
2771 reg_addr[V8HImode].reload_store = CODE_FOR_reload_v8hi_si_store;
2772 reg_addr[V8HImode].reload_load = CODE_FOR_reload_v8hi_si_load;
2773 reg_addr[V4SImode].reload_store = CODE_FOR_reload_v4si_si_store;
2774 reg_addr[V4SImode].reload_load = CODE_FOR_reload_v4si_si_load;
2775 reg_addr[V2DImode].reload_store = CODE_FOR_reload_v2di_si_store;
2776 reg_addr[V2DImode].reload_load = CODE_FOR_reload_v2di_si_load;
2777 reg_addr[V1TImode].reload_store = CODE_FOR_reload_v1ti_si_store;
2778 reg_addr[V1TImode].reload_load = CODE_FOR_reload_v1ti_si_load;
2779 reg_addr[V4SFmode].reload_store = CODE_FOR_reload_v4sf_si_store;
2780 reg_addr[V4SFmode].reload_load = CODE_FOR_reload_v4sf_si_load;
2781 reg_addr[V2DFmode].reload_store = CODE_FOR_reload_v2df_si_store;
2782 reg_addr[V2DFmode].reload_load = CODE_FOR_reload_v2df_si_load;
2783 if (TARGET_VSX && TARGET_UPPER_REGS_DF)
2785 reg_addr[DFmode].reload_store = CODE_FOR_reload_df_si_store;
2786 reg_addr[DFmode].reload_load = CODE_FOR_reload_df_si_load;
2787 reg_addr[DDmode].reload_store = CODE_FOR_reload_dd_si_store;
2788 reg_addr[DDmode].reload_load = CODE_FOR_reload_dd_si_load;
2790 if (TARGET_P8_VECTOR)
2792 reg_addr[SFmode].reload_store = CODE_FOR_reload_sf_si_store;
2793 reg_addr[SFmode].reload_load = CODE_FOR_reload_sf_si_load;
2794 reg_addr[SDmode].reload_store = CODE_FOR_reload_sd_si_store;
2795 reg_addr[SDmode].reload_load = CODE_FOR_reload_sd_si_load;
2797 if (TARGET_VSX_TIMODE)
2799 reg_addr[TImode].reload_store = CODE_FOR_reload_ti_si_store;
2800 reg_addr[TImode].reload_load = CODE_FOR_reload_ti_si_load;
2805 /* Precalculate HARD_REGNO_NREGS. */
2806 for (r = 0; r < FIRST_PSEUDO_REGISTER; ++r)
2807 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2808 rs6000_hard_regno_nregs[m][r]
2809 = rs6000_hard_regno_nregs_internal (r, (enum machine_mode)m);
2811 /* Precalculate HARD_REGNO_MODE_OK. */
2812 for (r = 0; r < FIRST_PSEUDO_REGISTER; ++r)
2813 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2814 if (rs6000_hard_regno_mode_ok (r, (enum machine_mode)m))
2815 rs6000_hard_regno_mode_ok_p[m][r] = true;
2817 /* Precalculate CLASS_MAX_NREGS sizes. */
2818 for (c = 0; c < LIM_REG_CLASSES; ++c)
2820 int reg_size;
2822 if (TARGET_VSX && VSX_REG_CLASS_P (c))
2823 reg_size = UNITS_PER_VSX_WORD;
2825 else if (c == ALTIVEC_REGS)
2826 reg_size = UNITS_PER_ALTIVEC_WORD;
2828 else if (c == FLOAT_REGS)
2829 reg_size = UNITS_PER_FP_WORD;
2831 else
2832 reg_size = UNITS_PER_WORD;
2834 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2836 int reg_size2 = reg_size;
2838 /* TFmode/TDmode always takes 2 registers, even in VSX. */
2839 if (TARGET_VSX && VSX_REG_CLASS_P (c)
2840 && (m == TDmode || m == TFmode))
2841 reg_size2 = UNITS_PER_FP_WORD;
2843 rs6000_class_max_nregs[m][c]
2844 = (GET_MODE_SIZE (m) + reg_size2 - 1) / reg_size2;
2848 if (TARGET_E500_DOUBLE)
2849 rs6000_class_max_nregs[DFmode][GENERAL_REGS] = 1;
2851 /* Calculate which modes to automatically generate code to use a the
2852 reciprocal divide and square root instructions. In the future, possibly
2853 automatically generate the instructions even if the user did not specify
2854 -mrecip. The older machines double precision reciprocal sqrt estimate is
2855 not accurate enough. */
2856 memset (rs6000_recip_bits, 0, sizeof (rs6000_recip_bits));
2857 if (TARGET_FRES)
2858 rs6000_recip_bits[SFmode] = RS6000_RECIP_MASK_HAVE_RE;
2859 if (TARGET_FRE)
2860 rs6000_recip_bits[DFmode] = RS6000_RECIP_MASK_HAVE_RE;
2861 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode))
2862 rs6000_recip_bits[V4SFmode] = RS6000_RECIP_MASK_HAVE_RE;
2863 if (VECTOR_UNIT_VSX_P (V2DFmode))
2864 rs6000_recip_bits[V2DFmode] = RS6000_RECIP_MASK_HAVE_RE;
2866 if (TARGET_FRSQRTES)
2867 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2868 if (TARGET_FRSQRTE)
2869 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2870 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode))
2871 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2872 if (VECTOR_UNIT_VSX_P (V2DFmode))
2873 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2875 if (rs6000_recip_control)
2877 if (!flag_finite_math_only)
2878 warning (0, "-mrecip requires -ffinite-math or -ffast-math");
2879 if (flag_trapping_math)
2880 warning (0, "-mrecip requires -fno-trapping-math or -ffast-math");
2881 if (!flag_reciprocal_math)
2882 warning (0, "-mrecip requires -freciprocal-math or -ffast-math");
2883 if (flag_finite_math_only && !flag_trapping_math && flag_reciprocal_math)
2885 if (RS6000_RECIP_HAVE_RE_P (SFmode)
2886 && (rs6000_recip_control & RECIP_SF_DIV) != 0)
2887 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2889 if (RS6000_RECIP_HAVE_RE_P (DFmode)
2890 && (rs6000_recip_control & RECIP_DF_DIV) != 0)
2891 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2893 if (RS6000_RECIP_HAVE_RE_P (V4SFmode)
2894 && (rs6000_recip_control & RECIP_V4SF_DIV) != 0)
2895 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2897 if (RS6000_RECIP_HAVE_RE_P (V2DFmode)
2898 && (rs6000_recip_control & RECIP_V2DF_DIV) != 0)
2899 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2901 if (RS6000_RECIP_HAVE_RSQRTE_P (SFmode)
2902 && (rs6000_recip_control & RECIP_SF_RSQRT) != 0)
2903 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2905 if (RS6000_RECIP_HAVE_RSQRTE_P (DFmode)
2906 && (rs6000_recip_control & RECIP_DF_RSQRT) != 0)
2907 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2909 if (RS6000_RECIP_HAVE_RSQRTE_P (V4SFmode)
2910 && (rs6000_recip_control & RECIP_V4SF_RSQRT) != 0)
2911 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2913 if (RS6000_RECIP_HAVE_RSQRTE_P (V2DFmode)
2914 && (rs6000_recip_control & RECIP_V2DF_RSQRT) != 0)
2915 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2919 /* Update the addr mask bits in reg_addr to help secondary reload and go if
2920 legitimate address support to figure out the appropriate addressing to
2921 use. */
2922 rs6000_setup_reg_addr_masks ();
2924 if (global_init_p || TARGET_DEBUG_TARGET)
2926 if (TARGET_DEBUG_REG)
2927 rs6000_debug_reg_global ();
2929 if (TARGET_DEBUG_COST || TARGET_DEBUG_REG)
2930 fprintf (stderr,
2931 "SImode variable mult cost = %d\n"
2932 "SImode constant mult cost = %d\n"
2933 "SImode short constant mult cost = %d\n"
2934 "DImode multipliciation cost = %d\n"
2935 "SImode division cost = %d\n"
2936 "DImode division cost = %d\n"
2937 "Simple fp operation cost = %d\n"
2938 "DFmode multiplication cost = %d\n"
2939 "SFmode division cost = %d\n"
2940 "DFmode division cost = %d\n"
2941 "cache line size = %d\n"
2942 "l1 cache size = %d\n"
2943 "l2 cache size = %d\n"
2944 "simultaneous prefetches = %d\n"
2945 "\n",
2946 rs6000_cost->mulsi,
2947 rs6000_cost->mulsi_const,
2948 rs6000_cost->mulsi_const9,
2949 rs6000_cost->muldi,
2950 rs6000_cost->divsi,
2951 rs6000_cost->divdi,
2952 rs6000_cost->fp,
2953 rs6000_cost->dmul,
2954 rs6000_cost->sdiv,
2955 rs6000_cost->ddiv,
2956 rs6000_cost->cache_line_size,
2957 rs6000_cost->l1_cache_size,
2958 rs6000_cost->l2_cache_size,
2959 rs6000_cost->simultaneous_prefetches);
2963 #if TARGET_MACHO
2964 /* The Darwin version of SUBTARGET_OVERRIDE_OPTIONS. */
2966 static void
2967 darwin_rs6000_override_options (void)
2969 /* The Darwin ABI always includes AltiVec, can't be (validly) turned
2970 off. */
2971 rs6000_altivec_abi = 1;
2972 TARGET_ALTIVEC_VRSAVE = 1;
2973 rs6000_current_abi = ABI_DARWIN;
2975 if (DEFAULT_ABI == ABI_DARWIN
2976 && TARGET_64BIT)
2977 darwin_one_byte_bool = 1;
2979 if (TARGET_64BIT && ! TARGET_POWERPC64)
2981 rs6000_isa_flags |= OPTION_MASK_POWERPC64;
2982 warning (0, "-m64 requires PowerPC64 architecture, enabling");
2984 if (flag_mkernel)
2986 rs6000_default_long_calls = 1;
2987 rs6000_isa_flags |= OPTION_MASK_SOFT_FLOAT;
2990 /* Make -m64 imply -maltivec. Darwin's 64-bit ABI includes
2991 Altivec. */
2992 if (!flag_mkernel && !flag_apple_kext
2993 && TARGET_64BIT
2994 && ! (rs6000_isa_flags_explicit & OPTION_MASK_ALTIVEC))
2995 rs6000_isa_flags |= OPTION_MASK_ALTIVEC;
2997 /* Unless the user (not the configurer) has explicitly overridden
2998 it with -mcpu=G3 or -mno-altivec, then 10.5+ targets default to
2999 G4 unless targeting the kernel. */
3000 if (!flag_mkernel
3001 && !flag_apple_kext
3002 && strverscmp (darwin_macosx_version_min, "10.5") >= 0
3003 && ! (rs6000_isa_flags_explicit & OPTION_MASK_ALTIVEC)
3004 && ! global_options_set.x_rs6000_cpu_index)
3006 rs6000_isa_flags |= OPTION_MASK_ALTIVEC;
3009 #endif
3011 /* If not otherwise specified by a target, make 'long double' equivalent to
3012 'double'. */
3014 #ifndef RS6000_DEFAULT_LONG_DOUBLE_SIZE
3015 #define RS6000_DEFAULT_LONG_DOUBLE_SIZE 64
3016 #endif
3018 /* Return the builtin mask of the various options used that could affect which
3019 builtins were used. In the past we used target_flags, but we've run out of
3020 bits, and some options like SPE and PAIRED are no longer in
3021 target_flags. */
3023 HOST_WIDE_INT
3024 rs6000_builtin_mask_calculate (void)
3026 return (((TARGET_ALTIVEC) ? RS6000_BTM_ALTIVEC : 0)
3027 | ((TARGET_VSX) ? RS6000_BTM_VSX : 0)
3028 | ((TARGET_SPE) ? RS6000_BTM_SPE : 0)
3029 | ((TARGET_PAIRED_FLOAT) ? RS6000_BTM_PAIRED : 0)
3030 | ((TARGET_FRE) ? RS6000_BTM_FRE : 0)
3031 | ((TARGET_FRES) ? RS6000_BTM_FRES : 0)
3032 | ((TARGET_FRSQRTE) ? RS6000_BTM_FRSQRTE : 0)
3033 | ((TARGET_FRSQRTES) ? RS6000_BTM_FRSQRTES : 0)
3034 | ((TARGET_POPCNTD) ? RS6000_BTM_POPCNTD : 0)
3035 | ((rs6000_cpu == PROCESSOR_CELL) ? RS6000_BTM_CELL : 0)
3036 | ((TARGET_P8_VECTOR) ? RS6000_BTM_P8_VECTOR : 0)
3037 | ((TARGET_CRYPTO) ? RS6000_BTM_CRYPTO : 0)
3038 | ((TARGET_HTM) ? RS6000_BTM_HTM : 0));
3041 /* Override command line options. Mostly we process the processor type and
3042 sometimes adjust other TARGET_ options. */
3044 static bool
3045 rs6000_option_override_internal (bool global_init_p)
3047 bool ret = true;
3048 bool have_cpu = false;
3050 /* The default cpu requested at configure time, if any. */
3051 const char *implicit_cpu = OPTION_TARGET_CPU_DEFAULT;
3053 HOST_WIDE_INT set_masks;
3054 int cpu_index;
3055 int tune_index;
3056 struct cl_target_option *main_target_opt
3057 = ((global_init_p || target_option_default_node == NULL)
3058 ? NULL : TREE_TARGET_OPTION (target_option_default_node));
3060 /* Remember the explicit arguments. */
3061 if (global_init_p)
3062 rs6000_isa_flags_explicit = global_options_set.x_rs6000_isa_flags;
3064 /* On 64-bit Darwin, power alignment is ABI-incompatible with some C
3065 library functions, so warn about it. The flag may be useful for
3066 performance studies from time to time though, so don't disable it
3067 entirely. */
3068 if (global_options_set.x_rs6000_alignment_flags
3069 && rs6000_alignment_flags == MASK_ALIGN_POWER
3070 && DEFAULT_ABI == ABI_DARWIN
3071 && TARGET_64BIT)
3072 warning (0, "-malign-power is not supported for 64-bit Darwin;"
3073 " it is incompatible with the installed C and C++ libraries");
3075 /* Numerous experiment shows that IRA based loop pressure
3076 calculation works better for RTL loop invariant motion on targets
3077 with enough (>= 32) registers. It is an expensive optimization.
3078 So it is on only for peak performance. */
3079 if (optimize >= 3 && global_init_p
3080 && !global_options_set.x_flag_ira_loop_pressure)
3081 flag_ira_loop_pressure = 1;
3083 /* Set the pointer size. */
3084 if (TARGET_64BIT)
3086 rs6000_pmode = (int)DImode;
3087 rs6000_pointer_size = 64;
3089 else
3091 rs6000_pmode = (int)SImode;
3092 rs6000_pointer_size = 32;
3095 /* Some OSs don't support saving the high part of 64-bit registers on context
3096 switch. Other OSs don't support saving Altivec registers. On those OSs,
3097 we don't touch the OPTION_MASK_POWERPC64 or OPTION_MASK_ALTIVEC settings;
3098 if the user wants either, the user must explicitly specify them and we
3099 won't interfere with the user's specification. */
3101 set_masks = POWERPC_MASKS;
3102 #ifdef OS_MISSING_POWERPC64
3103 if (OS_MISSING_POWERPC64)
3104 set_masks &= ~OPTION_MASK_POWERPC64;
3105 #endif
3106 #ifdef OS_MISSING_ALTIVEC
3107 if (OS_MISSING_ALTIVEC)
3108 set_masks &= ~(OPTION_MASK_ALTIVEC | OPTION_MASK_VSX);
3109 #endif
3111 /* Don't override by the processor default if given explicitly. */
3112 set_masks &= ~rs6000_isa_flags_explicit;
3114 /* Process the -mcpu=<xxx> and -mtune=<xxx> argument. If the user changed
3115 the cpu in a target attribute or pragma, but did not specify a tuning
3116 option, use the cpu for the tuning option rather than the option specified
3117 with -mtune on the command line. Process a '--with-cpu' configuration
3118 request as an implicit --cpu. */
3119 if (rs6000_cpu_index >= 0)
3121 cpu_index = rs6000_cpu_index;
3122 have_cpu = true;
3124 else if (main_target_opt != NULL && main_target_opt->x_rs6000_cpu_index >= 0)
3126 rs6000_cpu_index = cpu_index = main_target_opt->x_rs6000_cpu_index;
3127 have_cpu = true;
3129 else if (implicit_cpu)
3131 rs6000_cpu_index = cpu_index = rs6000_cpu_name_lookup (implicit_cpu);
3132 have_cpu = true;
3134 else
3136 const char *default_cpu = (TARGET_POWERPC64 ? "powerpc64" : "powerpc");
3137 rs6000_cpu_index = cpu_index = rs6000_cpu_name_lookup (default_cpu);
3138 have_cpu = false;
3141 gcc_assert (cpu_index >= 0);
3143 /* If we have a cpu, either through an explicit -mcpu=<xxx> or if the
3144 compiler was configured with --with-cpu=<xxx>, replace all of the ISA bits
3145 with those from the cpu, except for options that were explicitly set. If
3146 we don't have a cpu, do not override the target bits set in
3147 TARGET_DEFAULT. */
3148 if (have_cpu)
3150 rs6000_isa_flags &= ~set_masks;
3151 rs6000_isa_flags |= (processor_target_table[cpu_index].target_enable
3152 & set_masks);
3154 else
3155 rs6000_isa_flags |= (processor_target_table[cpu_index].target_enable
3156 & ~rs6000_isa_flags_explicit);
3158 /* If no -mcpu=<xxx>, inherit any default options that were cleared via
3159 POWERPC_MASKS. Originally, TARGET_DEFAULT was used to initialize
3160 target_flags via the TARGET_DEFAULT_TARGET_FLAGS hook. When we switched
3161 to using rs6000_isa_flags, we need to do the initialization here. */
3162 if (!have_cpu)
3163 rs6000_isa_flags |= (TARGET_DEFAULT & ~rs6000_isa_flags_explicit);
3165 if (rs6000_tune_index >= 0)
3166 tune_index = rs6000_tune_index;
3167 else if (have_cpu)
3168 rs6000_tune_index = tune_index = cpu_index;
3169 else
3171 size_t i;
3172 enum processor_type tune_proc
3173 = (TARGET_POWERPC64 ? PROCESSOR_DEFAULT64 : PROCESSOR_DEFAULT);
3175 tune_index = -1;
3176 for (i = 0; i < ARRAY_SIZE (processor_target_table); i++)
3177 if (processor_target_table[i].processor == tune_proc)
3179 rs6000_tune_index = tune_index = i;
3180 break;
3184 gcc_assert (tune_index >= 0);
3185 rs6000_cpu = processor_target_table[tune_index].processor;
3187 /* Pick defaults for SPE related control flags. Do this early to make sure
3188 that the TARGET_ macros are representative ASAP. */
3190 int spe_capable_cpu =
3191 (rs6000_cpu == PROCESSOR_PPC8540
3192 || rs6000_cpu == PROCESSOR_PPC8548);
3194 if (!global_options_set.x_rs6000_spe_abi)
3195 rs6000_spe_abi = spe_capable_cpu;
3197 if (!global_options_set.x_rs6000_spe)
3198 rs6000_spe = spe_capable_cpu;
3200 if (!global_options_set.x_rs6000_float_gprs)
3201 rs6000_float_gprs =
3202 (rs6000_cpu == PROCESSOR_PPC8540 ? 1
3203 : rs6000_cpu == PROCESSOR_PPC8548 ? 2
3204 : 0);
3207 if (global_options_set.x_rs6000_spe_abi
3208 && rs6000_spe_abi
3209 && !TARGET_SPE_ABI)
3210 error ("not configured for SPE ABI");
3212 if (global_options_set.x_rs6000_spe
3213 && rs6000_spe
3214 && !TARGET_SPE)
3215 error ("not configured for SPE instruction set");
3217 if (main_target_opt != NULL
3218 && ((main_target_opt->x_rs6000_spe_abi != rs6000_spe_abi)
3219 || (main_target_opt->x_rs6000_spe != rs6000_spe)
3220 || (main_target_opt->x_rs6000_float_gprs != rs6000_float_gprs)))
3221 error ("target attribute or pragma changes SPE ABI");
3223 if (rs6000_cpu == PROCESSOR_PPCE300C2 || rs6000_cpu == PROCESSOR_PPCE300C3
3224 || rs6000_cpu == PROCESSOR_PPCE500MC || rs6000_cpu == PROCESSOR_PPCE500MC64
3225 || rs6000_cpu == PROCESSOR_PPCE5500)
3227 if (TARGET_ALTIVEC)
3228 error ("AltiVec not supported in this target");
3229 if (TARGET_SPE)
3230 error ("SPE not supported in this target");
3232 if (rs6000_cpu == PROCESSOR_PPCE6500)
3234 if (TARGET_SPE)
3235 error ("SPE not supported in this target");
3238 /* Disable Cell microcode if we are optimizing for the Cell
3239 and not optimizing for size. */
3240 if (rs6000_gen_cell_microcode == -1)
3241 rs6000_gen_cell_microcode = !(rs6000_cpu == PROCESSOR_CELL
3242 && !optimize_size);
3244 /* If we are optimizing big endian systems for space and it's OK to
3245 use instructions that would be microcoded on the Cell, use the
3246 load/store multiple and string instructions. */
3247 if (BYTES_BIG_ENDIAN && optimize_size && rs6000_gen_cell_microcode)
3248 rs6000_isa_flags |= ~rs6000_isa_flags_explicit & (OPTION_MASK_MULTIPLE
3249 | OPTION_MASK_STRING);
3251 /* Don't allow -mmultiple or -mstring on little endian systems
3252 unless the cpu is a 750, because the hardware doesn't support the
3253 instructions used in little endian mode, and causes an alignment
3254 trap. The 750 does not cause an alignment trap (except when the
3255 target is unaligned). */
3257 if (!BYTES_BIG_ENDIAN && rs6000_cpu != PROCESSOR_PPC750)
3259 if (TARGET_MULTIPLE)
3261 rs6000_isa_flags &= ~OPTION_MASK_MULTIPLE;
3262 if ((rs6000_isa_flags_explicit & OPTION_MASK_MULTIPLE) != 0)
3263 warning (0, "-mmultiple is not supported on little endian systems");
3266 if (TARGET_STRING)
3268 rs6000_isa_flags &= ~OPTION_MASK_STRING;
3269 if ((rs6000_isa_flags_explicit & OPTION_MASK_STRING) != 0)
3270 warning (0, "-mstring is not supported on little endian systems");
3274 /* If little-endian, default to -mstrict-align on older processors.
3275 Testing for htm matches power8 and later. */
3276 if (!BYTES_BIG_ENDIAN
3277 && !(processor_target_table[tune_index].target_enable & OPTION_MASK_HTM))
3278 rs6000_isa_flags |= ~rs6000_isa_flags_explicit & OPTION_MASK_STRICT_ALIGN;
3280 /* -maltivec={le,be} implies -maltivec. */
3281 if (rs6000_altivec_element_order != 0)
3282 rs6000_isa_flags |= OPTION_MASK_ALTIVEC;
3284 /* Disallow -maltivec=le in big endian mode for now. This is not
3285 known to be useful for anyone. */
3286 if (BYTES_BIG_ENDIAN && rs6000_altivec_element_order == 1)
3288 warning (0, N_("-maltivec=le not allowed for big-endian targets"));
3289 rs6000_altivec_element_order = 0;
3292 /* Add some warnings for VSX. */
3293 if (TARGET_VSX)
3295 const char *msg = NULL;
3296 if (!TARGET_HARD_FLOAT || !TARGET_FPRS
3297 || !TARGET_SINGLE_FLOAT || !TARGET_DOUBLE_FLOAT)
3299 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX)
3300 msg = N_("-mvsx requires hardware floating point");
3301 else
3303 rs6000_isa_flags &= ~ OPTION_MASK_VSX;
3304 rs6000_isa_flags_explicit |= OPTION_MASK_VSX;
3307 else if (TARGET_PAIRED_FLOAT)
3308 msg = N_("-mvsx and -mpaired are incompatible");
3309 else if (TARGET_AVOID_XFORM > 0)
3310 msg = N_("-mvsx needs indexed addressing");
3311 else if (!TARGET_ALTIVEC && (rs6000_isa_flags_explicit
3312 & OPTION_MASK_ALTIVEC))
3314 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX)
3315 msg = N_("-mvsx and -mno-altivec are incompatible");
3316 else
3317 msg = N_("-mno-altivec disables vsx");
3320 if (msg)
3322 warning (0, msg);
3323 rs6000_isa_flags &= ~ OPTION_MASK_VSX;
3324 rs6000_isa_flags_explicit |= OPTION_MASK_VSX;
3328 /* If hard-float/altivec/vsx were explicitly turned off then don't allow
3329 the -mcpu setting to enable options that conflict. */
3330 if ((!TARGET_HARD_FLOAT || !TARGET_ALTIVEC || !TARGET_VSX)
3331 && (rs6000_isa_flags_explicit & (OPTION_MASK_SOFT_FLOAT
3332 | OPTION_MASK_ALTIVEC
3333 | OPTION_MASK_VSX)) != 0)
3334 rs6000_isa_flags &= ~((OPTION_MASK_P8_VECTOR | OPTION_MASK_CRYPTO
3335 | OPTION_MASK_DIRECT_MOVE)
3336 & ~rs6000_isa_flags_explicit);
3338 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
3339 rs6000_print_isa_options (stderr, 0, "before defaults", rs6000_isa_flags);
3341 /* For the newer switches (vsx, dfp, etc.) set some of the older options,
3342 unless the user explicitly used the -mno-<option> to disable the code. */
3343 if (TARGET_P8_VECTOR || TARGET_DIRECT_MOVE || TARGET_CRYPTO)
3344 rs6000_isa_flags |= (ISA_2_7_MASKS_SERVER & ~rs6000_isa_flags_explicit);
3345 else if (TARGET_VSX)
3346 rs6000_isa_flags |= (ISA_2_6_MASKS_SERVER & ~rs6000_isa_flags_explicit);
3347 else if (TARGET_POPCNTD)
3348 rs6000_isa_flags |= (ISA_2_6_MASKS_EMBEDDED & ~rs6000_isa_flags_explicit);
3349 else if (TARGET_DFP)
3350 rs6000_isa_flags |= (ISA_2_5_MASKS_SERVER & ~rs6000_isa_flags_explicit);
3351 else if (TARGET_CMPB)
3352 rs6000_isa_flags |= (ISA_2_5_MASKS_EMBEDDED & ~rs6000_isa_flags_explicit);
3353 else if (TARGET_FPRND)
3354 rs6000_isa_flags |= (ISA_2_4_MASKS & ~rs6000_isa_flags_explicit);
3355 else if (TARGET_POPCNTB)
3356 rs6000_isa_flags |= (ISA_2_2_MASKS & ~rs6000_isa_flags_explicit);
3357 else if (TARGET_ALTIVEC)
3358 rs6000_isa_flags |= (OPTION_MASK_PPC_GFXOPT & ~rs6000_isa_flags_explicit);
3360 if (TARGET_CRYPTO && !TARGET_ALTIVEC)
3362 if (rs6000_isa_flags_explicit & OPTION_MASK_CRYPTO)
3363 error ("-mcrypto requires -maltivec");
3364 rs6000_isa_flags &= ~OPTION_MASK_CRYPTO;
3367 if (TARGET_DIRECT_MOVE && !TARGET_VSX)
3369 if (rs6000_isa_flags_explicit & OPTION_MASK_DIRECT_MOVE)
3370 error ("-mdirect-move requires -mvsx");
3371 rs6000_isa_flags &= ~OPTION_MASK_DIRECT_MOVE;
3374 if (TARGET_P8_VECTOR && !TARGET_ALTIVEC)
3376 if (rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR)
3377 error ("-mpower8-vector requires -maltivec");
3378 rs6000_isa_flags &= ~OPTION_MASK_P8_VECTOR;
3381 if (TARGET_P8_VECTOR && !TARGET_VSX)
3383 if (rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR)
3384 error ("-mpower8-vector requires -mvsx");
3385 rs6000_isa_flags &= ~OPTION_MASK_P8_VECTOR;
3388 if (TARGET_VSX_TIMODE && !TARGET_VSX)
3390 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX_TIMODE)
3391 error ("-mvsx-timode requires -mvsx");
3392 rs6000_isa_flags &= ~OPTION_MASK_VSX_TIMODE;
3395 /* The quad memory instructions only works in 64-bit mode. In 32-bit mode,
3396 silently turn off quad memory mode. */
3397 if ((TARGET_QUAD_MEMORY || TARGET_QUAD_MEMORY_ATOMIC) && !TARGET_POWERPC64)
3399 if ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY) != 0)
3400 warning (0, N_("-mquad-memory requires 64-bit mode"));
3402 if ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY_ATOMIC) != 0)
3403 warning (0, N_("-mquad-memory-atomic requires 64-bit mode"));
3405 rs6000_isa_flags &= ~(OPTION_MASK_QUAD_MEMORY
3406 | OPTION_MASK_QUAD_MEMORY_ATOMIC);
3409 /* Non-atomic quad memory load/store are disabled for little endian, since
3410 the words are reversed, but atomic operations can still be done by
3411 swapping the words. */
3412 if (TARGET_QUAD_MEMORY && !WORDS_BIG_ENDIAN)
3414 if ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY) != 0)
3415 warning (0, N_("-mquad-memory is not available in little endian mode"));
3417 rs6000_isa_flags &= ~OPTION_MASK_QUAD_MEMORY;
3420 /* Assume if the user asked for normal quad memory instructions, they want
3421 the atomic versions as well, unless they explicity told us not to use quad
3422 word atomic instructions. */
3423 if (TARGET_QUAD_MEMORY
3424 && !TARGET_QUAD_MEMORY_ATOMIC
3425 && ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY_ATOMIC) == 0))
3426 rs6000_isa_flags |= OPTION_MASK_QUAD_MEMORY_ATOMIC;
3428 /* Enable power8 fusion if we are tuning for power8, even if we aren't
3429 generating power8 instructions. */
3430 if (!(rs6000_isa_flags_explicit & OPTION_MASK_P8_FUSION))
3431 rs6000_isa_flags |= (processor_target_table[tune_index].target_enable
3432 & OPTION_MASK_P8_FUSION);
3434 /* Power8 does not fuse sign extended loads with the addis. If we are
3435 optimizing at high levels for speed, convert a sign extended load into a
3436 zero extending load, and an explicit sign extension. */
3437 if (TARGET_P8_FUSION
3438 && !(rs6000_isa_flags_explicit & OPTION_MASK_P8_FUSION_SIGN)
3439 && optimize_function_for_speed_p (cfun)
3440 && optimize >= 3)
3441 rs6000_isa_flags |= OPTION_MASK_P8_FUSION_SIGN;
3443 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
3444 rs6000_print_isa_options (stderr, 0, "after defaults", rs6000_isa_flags);
3446 /* E500mc does "better" if we inline more aggressively. Respect the
3447 user's opinion, though. */
3448 if (rs6000_block_move_inline_limit == 0
3449 && (rs6000_cpu == PROCESSOR_PPCE500MC
3450 || rs6000_cpu == PROCESSOR_PPCE500MC64
3451 || rs6000_cpu == PROCESSOR_PPCE5500
3452 || rs6000_cpu == PROCESSOR_PPCE6500))
3453 rs6000_block_move_inline_limit = 128;
3455 /* store_one_arg depends on expand_block_move to handle at least the
3456 size of reg_parm_stack_space. */
3457 if (rs6000_block_move_inline_limit < (TARGET_POWERPC64 ? 64 : 32))
3458 rs6000_block_move_inline_limit = (TARGET_POWERPC64 ? 64 : 32);
3460 if (global_init_p)
3462 /* If the appropriate debug option is enabled, replace the target hooks
3463 with debug versions that call the real version and then prints
3464 debugging information. */
3465 if (TARGET_DEBUG_COST)
3467 targetm.rtx_costs = rs6000_debug_rtx_costs;
3468 targetm.address_cost = rs6000_debug_address_cost;
3469 targetm.sched.adjust_cost = rs6000_debug_adjust_cost;
3472 if (TARGET_DEBUG_ADDR)
3474 targetm.legitimate_address_p = rs6000_debug_legitimate_address_p;
3475 targetm.legitimize_address = rs6000_debug_legitimize_address;
3476 rs6000_secondary_reload_class_ptr
3477 = rs6000_debug_secondary_reload_class;
3478 rs6000_secondary_memory_needed_ptr
3479 = rs6000_debug_secondary_memory_needed;
3480 rs6000_cannot_change_mode_class_ptr
3481 = rs6000_debug_cannot_change_mode_class;
3482 rs6000_preferred_reload_class_ptr
3483 = rs6000_debug_preferred_reload_class;
3484 rs6000_legitimize_reload_address_ptr
3485 = rs6000_debug_legitimize_reload_address;
3486 rs6000_mode_dependent_address_ptr
3487 = rs6000_debug_mode_dependent_address;
3490 if (rs6000_veclibabi_name)
3492 if (strcmp (rs6000_veclibabi_name, "mass") == 0)
3493 rs6000_veclib_handler = rs6000_builtin_vectorized_libmass;
3494 else
3496 error ("unknown vectorization library ABI type (%s) for "
3497 "-mveclibabi= switch", rs6000_veclibabi_name);
3498 ret = false;
3503 if (!global_options_set.x_rs6000_long_double_type_size)
3505 if (main_target_opt != NULL
3506 && (main_target_opt->x_rs6000_long_double_type_size
3507 != RS6000_DEFAULT_LONG_DOUBLE_SIZE))
3508 error ("target attribute or pragma changes long double size");
3509 else
3510 rs6000_long_double_type_size = RS6000_DEFAULT_LONG_DOUBLE_SIZE;
3513 #if !defined (POWERPC_LINUX) && !defined (POWERPC_FREEBSD)
3514 if (!global_options_set.x_rs6000_ieeequad)
3515 rs6000_ieeequad = 1;
3516 #endif
3518 /* Disable VSX and Altivec silently if the user switched cpus to power7 in a
3519 target attribute or pragma which automatically enables both options,
3520 unless the altivec ABI was set. This is set by default for 64-bit, but
3521 not for 32-bit. */
3522 if (main_target_opt != NULL && !main_target_opt->x_rs6000_altivec_abi)
3523 rs6000_isa_flags &= ~((OPTION_MASK_VSX | OPTION_MASK_ALTIVEC)
3524 & ~rs6000_isa_flags_explicit);
3526 /* Enable Altivec ABI for AIX -maltivec. */
3527 if (TARGET_XCOFF && (TARGET_ALTIVEC || TARGET_VSX))
3529 if (main_target_opt != NULL && !main_target_opt->x_rs6000_altivec_abi)
3530 error ("target attribute or pragma changes AltiVec ABI");
3531 else
3532 rs6000_altivec_abi = 1;
3535 /* The AltiVec ABI is the default for PowerPC-64 GNU/Linux. For
3536 PowerPC-32 GNU/Linux, -maltivec implies the AltiVec ABI. It can
3537 be explicitly overridden in either case. */
3538 if (TARGET_ELF)
3540 if (!global_options_set.x_rs6000_altivec_abi
3541 && (TARGET_64BIT || TARGET_ALTIVEC || TARGET_VSX))
3543 if (main_target_opt != NULL &&
3544 !main_target_opt->x_rs6000_altivec_abi)
3545 error ("target attribute or pragma changes AltiVec ABI");
3546 else
3547 rs6000_altivec_abi = 1;
3551 /* Set the Darwin64 ABI as default for 64-bit Darwin.
3552 So far, the only darwin64 targets are also MACH-O. */
3553 if (TARGET_MACHO
3554 && DEFAULT_ABI == ABI_DARWIN
3555 && TARGET_64BIT)
3557 if (main_target_opt != NULL && !main_target_opt->x_rs6000_darwin64_abi)
3558 error ("target attribute or pragma changes darwin64 ABI");
3559 else
3561 rs6000_darwin64_abi = 1;
3562 /* Default to natural alignment, for better performance. */
3563 rs6000_alignment_flags = MASK_ALIGN_NATURAL;
3567 /* Place FP constants in the constant pool instead of TOC
3568 if section anchors enabled. */
3569 if (flag_section_anchors
3570 && !global_options_set.x_TARGET_NO_FP_IN_TOC)
3571 TARGET_NO_FP_IN_TOC = 1;
3573 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
3574 rs6000_print_isa_options (stderr, 0, "before subtarget", rs6000_isa_flags);
3576 #ifdef SUBTARGET_OVERRIDE_OPTIONS
3577 SUBTARGET_OVERRIDE_OPTIONS;
3578 #endif
3579 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
3580 SUBSUBTARGET_OVERRIDE_OPTIONS;
3581 #endif
3582 #ifdef SUB3TARGET_OVERRIDE_OPTIONS
3583 SUB3TARGET_OVERRIDE_OPTIONS;
3584 #endif
3586 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
3587 rs6000_print_isa_options (stderr, 0, "after subtarget", rs6000_isa_flags);
3589 /* For the E500 family of cores, reset the single/double FP flags to let us
3590 check that they remain constant across attributes or pragmas. Also,
3591 clear a possible request for string instructions, not supported and which
3592 we might have silently queried above for -Os.
3594 For other families, clear ISEL in case it was set implicitly.
3597 switch (rs6000_cpu)
3599 case PROCESSOR_PPC8540:
3600 case PROCESSOR_PPC8548:
3601 case PROCESSOR_PPCE500MC:
3602 case PROCESSOR_PPCE500MC64:
3603 case PROCESSOR_PPCE5500:
3604 case PROCESSOR_PPCE6500:
3606 rs6000_single_float = TARGET_E500_SINGLE || TARGET_E500_DOUBLE;
3607 rs6000_double_float = TARGET_E500_DOUBLE;
3609 rs6000_isa_flags &= ~OPTION_MASK_STRING;
3611 break;
3613 default:
3615 if (have_cpu && !(rs6000_isa_flags_explicit & OPTION_MASK_ISEL))
3616 rs6000_isa_flags &= ~OPTION_MASK_ISEL;
3618 break;
3621 if (main_target_opt)
3623 if (main_target_opt->x_rs6000_single_float != rs6000_single_float)
3624 error ("target attribute or pragma changes single precision floating "
3625 "point");
3626 if (main_target_opt->x_rs6000_double_float != rs6000_double_float)
3627 error ("target attribute or pragma changes double precision floating "
3628 "point");
3631 /* Detect invalid option combinations with E500. */
3632 CHECK_E500_OPTIONS;
3634 rs6000_always_hint = (rs6000_cpu != PROCESSOR_POWER4
3635 && rs6000_cpu != PROCESSOR_POWER5
3636 && rs6000_cpu != PROCESSOR_POWER6
3637 && rs6000_cpu != PROCESSOR_POWER7
3638 && rs6000_cpu != PROCESSOR_POWER8
3639 && rs6000_cpu != PROCESSOR_PPCA2
3640 && rs6000_cpu != PROCESSOR_CELL
3641 && rs6000_cpu != PROCESSOR_PPC476);
3642 rs6000_sched_groups = (rs6000_cpu == PROCESSOR_POWER4
3643 || rs6000_cpu == PROCESSOR_POWER5
3644 || rs6000_cpu == PROCESSOR_POWER7
3645 || rs6000_cpu == PROCESSOR_POWER8);
3646 rs6000_align_branch_targets = (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_PPCE500MC
3652 || rs6000_cpu == PROCESSOR_PPCE500MC64
3653 || rs6000_cpu == PROCESSOR_PPCE5500
3654 || rs6000_cpu == PROCESSOR_PPCE6500);
3656 /* Allow debug switches to override the above settings. These are set to -1
3657 in rs6000.opt to indicate the user hasn't directly set the switch. */
3658 if (TARGET_ALWAYS_HINT >= 0)
3659 rs6000_always_hint = TARGET_ALWAYS_HINT;
3661 if (TARGET_SCHED_GROUPS >= 0)
3662 rs6000_sched_groups = TARGET_SCHED_GROUPS;
3664 if (TARGET_ALIGN_BRANCH_TARGETS >= 0)
3665 rs6000_align_branch_targets = TARGET_ALIGN_BRANCH_TARGETS;
3667 rs6000_sched_restricted_insns_priority
3668 = (rs6000_sched_groups ? 1 : 0);
3670 /* Handle -msched-costly-dep option. */
3671 rs6000_sched_costly_dep
3672 = (rs6000_sched_groups ? true_store_to_load_dep_costly : no_dep_costly);
3674 if (rs6000_sched_costly_dep_str)
3676 if (! strcmp (rs6000_sched_costly_dep_str, "no"))
3677 rs6000_sched_costly_dep = no_dep_costly;
3678 else if (! strcmp (rs6000_sched_costly_dep_str, "all"))
3679 rs6000_sched_costly_dep = all_deps_costly;
3680 else if (! strcmp (rs6000_sched_costly_dep_str, "true_store_to_load"))
3681 rs6000_sched_costly_dep = true_store_to_load_dep_costly;
3682 else if (! strcmp (rs6000_sched_costly_dep_str, "store_to_load"))
3683 rs6000_sched_costly_dep = store_to_load_dep_costly;
3684 else
3685 rs6000_sched_costly_dep = ((enum rs6000_dependence_cost)
3686 atoi (rs6000_sched_costly_dep_str));
3689 /* Handle -minsert-sched-nops option. */
3690 rs6000_sched_insert_nops
3691 = (rs6000_sched_groups ? sched_finish_regroup_exact : sched_finish_none);
3693 if (rs6000_sched_insert_nops_str)
3695 if (! strcmp (rs6000_sched_insert_nops_str, "no"))
3696 rs6000_sched_insert_nops = sched_finish_none;
3697 else if (! strcmp (rs6000_sched_insert_nops_str, "pad"))
3698 rs6000_sched_insert_nops = sched_finish_pad_groups;
3699 else if (! strcmp (rs6000_sched_insert_nops_str, "regroup_exact"))
3700 rs6000_sched_insert_nops = sched_finish_regroup_exact;
3701 else
3702 rs6000_sched_insert_nops = ((enum rs6000_nop_insertion)
3703 atoi (rs6000_sched_insert_nops_str));
3706 if (global_init_p)
3708 #ifdef TARGET_REGNAMES
3709 /* If the user desires alternate register names, copy in the
3710 alternate names now. */
3711 if (TARGET_REGNAMES)
3712 memcpy (rs6000_reg_names, alt_reg_names, sizeof (rs6000_reg_names));
3713 #endif
3715 /* Set aix_struct_return last, after the ABI is determined.
3716 If -maix-struct-return or -msvr4-struct-return was explicitly
3717 used, don't override with the ABI default. */
3718 if (!global_options_set.x_aix_struct_return)
3719 aix_struct_return = (DEFAULT_ABI != ABI_V4 || DRAFT_V4_STRUCT_RET);
3721 #if 0
3722 /* IBM XL compiler defaults to unsigned bitfields. */
3723 if (TARGET_XL_COMPAT)
3724 flag_signed_bitfields = 0;
3725 #endif
3727 if (TARGET_LONG_DOUBLE_128 && !TARGET_IEEEQUAD)
3728 REAL_MODE_FORMAT (TFmode) = &ibm_extended_format;
3730 if (TARGET_TOC)
3731 ASM_GENERATE_INTERNAL_LABEL (toc_label_name, "LCTOC", 1);
3733 /* We can only guarantee the availability of DI pseudo-ops when
3734 assembling for 64-bit targets. */
3735 if (!TARGET_64BIT)
3737 targetm.asm_out.aligned_op.di = NULL;
3738 targetm.asm_out.unaligned_op.di = NULL;
3742 /* Set branch target alignment, if not optimizing for size. */
3743 if (!optimize_size)
3745 /* Cell wants to be aligned 8byte for dual issue. Titan wants to be
3746 aligned 8byte to avoid misprediction by the branch predictor. */
3747 if (rs6000_cpu == PROCESSOR_TITAN
3748 || rs6000_cpu == PROCESSOR_CELL)
3750 if (align_functions <= 0)
3751 align_functions = 8;
3752 if (align_jumps <= 0)
3753 align_jumps = 8;
3754 if (align_loops <= 0)
3755 align_loops = 8;
3757 if (rs6000_align_branch_targets)
3759 if (align_functions <= 0)
3760 align_functions = 16;
3761 if (align_jumps <= 0)
3762 align_jumps = 16;
3763 if (align_loops <= 0)
3765 can_override_loop_align = 1;
3766 align_loops = 16;
3769 if (align_jumps_max_skip <= 0)
3770 align_jumps_max_skip = 15;
3771 if (align_loops_max_skip <= 0)
3772 align_loops_max_skip = 15;
3775 /* Arrange to save and restore machine status around nested functions. */
3776 init_machine_status = rs6000_init_machine_status;
3778 /* We should always be splitting complex arguments, but we can't break
3779 Linux and Darwin ABIs at the moment. For now, only AIX is fixed. */
3780 if (DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN)
3781 targetm.calls.split_complex_arg = NULL;
3784 /* Initialize rs6000_cost with the appropriate target costs. */
3785 if (optimize_size)
3786 rs6000_cost = TARGET_POWERPC64 ? &size64_cost : &size32_cost;
3787 else
3788 switch (rs6000_cpu)
3790 case PROCESSOR_RS64A:
3791 rs6000_cost = &rs64a_cost;
3792 break;
3794 case PROCESSOR_MPCCORE:
3795 rs6000_cost = &mpccore_cost;
3796 break;
3798 case PROCESSOR_PPC403:
3799 rs6000_cost = &ppc403_cost;
3800 break;
3802 case PROCESSOR_PPC405:
3803 rs6000_cost = &ppc405_cost;
3804 break;
3806 case PROCESSOR_PPC440:
3807 rs6000_cost = &ppc440_cost;
3808 break;
3810 case PROCESSOR_PPC476:
3811 rs6000_cost = &ppc476_cost;
3812 break;
3814 case PROCESSOR_PPC601:
3815 rs6000_cost = &ppc601_cost;
3816 break;
3818 case PROCESSOR_PPC603:
3819 rs6000_cost = &ppc603_cost;
3820 break;
3822 case PROCESSOR_PPC604:
3823 rs6000_cost = &ppc604_cost;
3824 break;
3826 case PROCESSOR_PPC604e:
3827 rs6000_cost = &ppc604e_cost;
3828 break;
3830 case PROCESSOR_PPC620:
3831 rs6000_cost = &ppc620_cost;
3832 break;
3834 case PROCESSOR_PPC630:
3835 rs6000_cost = &ppc630_cost;
3836 break;
3838 case PROCESSOR_CELL:
3839 rs6000_cost = &ppccell_cost;
3840 break;
3842 case PROCESSOR_PPC750:
3843 case PROCESSOR_PPC7400:
3844 rs6000_cost = &ppc750_cost;
3845 break;
3847 case PROCESSOR_PPC7450:
3848 rs6000_cost = &ppc7450_cost;
3849 break;
3851 case PROCESSOR_PPC8540:
3852 case PROCESSOR_PPC8548:
3853 rs6000_cost = &ppc8540_cost;
3854 break;
3856 case PROCESSOR_PPCE300C2:
3857 case PROCESSOR_PPCE300C3:
3858 rs6000_cost = &ppce300c2c3_cost;
3859 break;
3861 case PROCESSOR_PPCE500MC:
3862 rs6000_cost = &ppce500mc_cost;
3863 break;
3865 case PROCESSOR_PPCE500MC64:
3866 rs6000_cost = &ppce500mc64_cost;
3867 break;
3869 case PROCESSOR_PPCE5500:
3870 rs6000_cost = &ppce5500_cost;
3871 break;
3873 case PROCESSOR_PPCE6500:
3874 rs6000_cost = &ppce6500_cost;
3875 break;
3877 case PROCESSOR_TITAN:
3878 rs6000_cost = &titan_cost;
3879 break;
3881 case PROCESSOR_POWER4:
3882 case PROCESSOR_POWER5:
3883 rs6000_cost = &power4_cost;
3884 break;
3886 case PROCESSOR_POWER6:
3887 rs6000_cost = &power6_cost;
3888 break;
3890 case PROCESSOR_POWER7:
3891 rs6000_cost = &power7_cost;
3892 break;
3894 case PROCESSOR_POWER8:
3895 rs6000_cost = &power8_cost;
3896 break;
3898 case PROCESSOR_PPCA2:
3899 rs6000_cost = &ppca2_cost;
3900 break;
3902 default:
3903 gcc_unreachable ();
3906 if (global_init_p)
3908 maybe_set_param_value (PARAM_SIMULTANEOUS_PREFETCHES,
3909 rs6000_cost->simultaneous_prefetches,
3910 global_options.x_param_values,
3911 global_options_set.x_param_values);
3912 maybe_set_param_value (PARAM_L1_CACHE_SIZE, rs6000_cost->l1_cache_size,
3913 global_options.x_param_values,
3914 global_options_set.x_param_values);
3915 maybe_set_param_value (PARAM_L1_CACHE_LINE_SIZE,
3916 rs6000_cost->cache_line_size,
3917 global_options.x_param_values,
3918 global_options_set.x_param_values);
3919 maybe_set_param_value (PARAM_L2_CACHE_SIZE, rs6000_cost->l2_cache_size,
3920 global_options.x_param_values,
3921 global_options_set.x_param_values);
3923 /* Increase loop peeling limits based on performance analysis. */
3924 maybe_set_param_value (PARAM_MAX_PEELED_INSNS, 400,
3925 global_options.x_param_values,
3926 global_options_set.x_param_values);
3927 maybe_set_param_value (PARAM_MAX_COMPLETELY_PEELED_INSNS, 400,
3928 global_options.x_param_values,
3929 global_options_set.x_param_values);
3931 /* If using typedef char *va_list, signal that
3932 __builtin_va_start (&ap, 0) can be optimized to
3933 ap = __builtin_next_arg (0). */
3934 if (DEFAULT_ABI != ABI_V4)
3935 targetm.expand_builtin_va_start = NULL;
3938 /* Set up single/double float flags.
3939 If TARGET_HARD_FLOAT is set, but neither single or double is set,
3940 then set both flags. */
3941 if (TARGET_HARD_FLOAT && TARGET_FPRS
3942 && rs6000_single_float == 0 && rs6000_double_float == 0)
3943 rs6000_single_float = rs6000_double_float = 1;
3945 /* If not explicitly specified via option, decide whether to generate indexed
3946 load/store instructions. */
3947 if (TARGET_AVOID_XFORM == -1)
3948 /* Avoid indexed addressing when targeting Power6 in order to avoid the
3949 DERAT mispredict penalty. However the LVE and STVE altivec instructions
3950 need indexed accesses and the type used is the scalar type of the element
3951 being loaded or stored. */
3952 TARGET_AVOID_XFORM = (rs6000_cpu == PROCESSOR_POWER6 && TARGET_CMPB
3953 && !TARGET_ALTIVEC);
3955 /* Set the -mrecip options. */
3956 if (rs6000_recip_name)
3958 char *p = ASTRDUP (rs6000_recip_name);
3959 char *q;
3960 unsigned int mask, i;
3961 bool invert;
3963 while ((q = strtok (p, ",")) != NULL)
3965 p = NULL;
3966 if (*q == '!')
3968 invert = true;
3969 q++;
3971 else
3972 invert = false;
3974 if (!strcmp (q, "default"))
3975 mask = ((TARGET_RECIP_PRECISION)
3976 ? RECIP_HIGH_PRECISION : RECIP_LOW_PRECISION);
3977 else
3979 for (i = 0; i < ARRAY_SIZE (recip_options); i++)
3980 if (!strcmp (q, recip_options[i].string))
3982 mask = recip_options[i].mask;
3983 break;
3986 if (i == ARRAY_SIZE (recip_options))
3988 error ("unknown option for -mrecip=%s", q);
3989 invert = false;
3990 mask = 0;
3991 ret = false;
3995 if (invert)
3996 rs6000_recip_control &= ~mask;
3997 else
3998 rs6000_recip_control |= mask;
4002 /* Set the builtin mask of the various options used that could affect which
4003 builtins were used. In the past we used target_flags, but we've run out
4004 of bits, and some options like SPE and PAIRED are no longer in
4005 target_flags. */
4006 rs6000_builtin_mask = rs6000_builtin_mask_calculate ();
4007 if (TARGET_DEBUG_BUILTIN || TARGET_DEBUG_TARGET)
4009 fprintf (stderr,
4010 "new builtin mask = " HOST_WIDE_INT_PRINT_HEX ", ",
4011 rs6000_builtin_mask);
4012 rs6000_print_builtin_options (stderr, 0, NULL, rs6000_builtin_mask);
4015 /* Initialize all of the registers. */
4016 rs6000_init_hard_regno_mode_ok (global_init_p);
4018 /* Save the initial options in case the user does function specific options */
4019 if (global_init_p)
4020 target_option_default_node = target_option_current_node
4021 = build_target_option_node (&global_options);
4023 /* If not explicitly specified via option, decide whether to generate the
4024 extra blr's required to preserve the link stack on some cpus (eg, 476). */
4025 if (TARGET_LINK_STACK == -1)
4026 SET_TARGET_LINK_STACK (rs6000_cpu == PROCESSOR_PPC476 && flag_pic);
4028 return ret;
4031 /* Implement TARGET_OPTION_OVERRIDE. On the RS/6000 this is used to
4032 define the target cpu type. */
4034 static void
4035 rs6000_option_override (void)
4037 (void) rs6000_option_override_internal (true);
4041 /* Implement targetm.vectorize.builtin_mask_for_load. */
4042 static tree
4043 rs6000_builtin_mask_for_load (void)
4045 if (TARGET_ALTIVEC || TARGET_VSX)
4046 return altivec_builtin_mask_for_load;
4047 else
4048 return 0;
4051 /* Implement LOOP_ALIGN. */
4053 rs6000_loop_align (rtx label)
4055 basic_block bb;
4056 int ninsns;
4058 /* Don't override loop alignment if -falign-loops was specified. */
4059 if (!can_override_loop_align)
4060 return align_loops_log;
4062 bb = BLOCK_FOR_INSN (label);
4063 ninsns = num_loop_insns(bb->loop_father);
4065 /* Align small loops to 32 bytes to fit in an icache sector, otherwise return default. */
4066 if (ninsns > 4 && ninsns <= 8
4067 && (rs6000_cpu == PROCESSOR_POWER4
4068 || rs6000_cpu == PROCESSOR_POWER5
4069 || rs6000_cpu == PROCESSOR_POWER6
4070 || rs6000_cpu == PROCESSOR_POWER7
4071 || rs6000_cpu == PROCESSOR_POWER8))
4072 return 5;
4073 else
4074 return align_loops_log;
4077 /* Implement TARGET_LOOP_ALIGN_MAX_SKIP. */
4078 static int
4079 rs6000_loop_align_max_skip (rtx label)
4081 return (1 << rs6000_loop_align (label)) - 1;
4084 /* Return true iff, data reference of TYPE can reach vector alignment (16)
4085 after applying N number of iterations. This routine does not determine
4086 how may iterations are required to reach desired alignment. */
4088 static bool
4089 rs6000_vector_alignment_reachable (const_tree type ATTRIBUTE_UNUSED, bool is_packed)
4091 if (is_packed)
4092 return false;
4094 if (TARGET_32BIT)
4096 if (rs6000_alignment_flags == MASK_ALIGN_NATURAL)
4097 return true;
4099 if (rs6000_alignment_flags == MASK_ALIGN_POWER)
4100 return true;
4102 return false;
4104 else
4106 if (TARGET_MACHO)
4107 return false;
4109 /* Assuming that all other types are naturally aligned. CHECKME! */
4110 return true;
4114 /* Return true if the vector misalignment factor is supported by the
4115 target. */
4116 static bool
4117 rs6000_builtin_support_vector_misalignment (enum machine_mode mode,
4118 const_tree type,
4119 int misalignment,
4120 bool is_packed)
4122 if (TARGET_VSX)
4124 /* Return if movmisalign pattern is not supported for this mode. */
4125 if (optab_handler (movmisalign_optab, mode) == CODE_FOR_nothing)
4126 return false;
4128 if (misalignment == -1)
4130 /* Misalignment factor is unknown at compile time but we know
4131 it's word aligned. */
4132 if (rs6000_vector_alignment_reachable (type, is_packed))
4134 int element_size = TREE_INT_CST_LOW (TYPE_SIZE (type));
4136 if (element_size == 64 || element_size == 32)
4137 return true;
4140 return false;
4143 /* VSX supports word-aligned vector. */
4144 if (misalignment % 4 == 0)
4145 return true;
4147 return false;
4150 /* Implement targetm.vectorize.builtin_vectorization_cost. */
4151 static int
4152 rs6000_builtin_vectorization_cost (enum vect_cost_for_stmt type_of_cost,
4153 tree vectype, int misalign)
4155 unsigned elements;
4156 tree elem_type;
4158 switch (type_of_cost)
4160 case scalar_stmt:
4161 case scalar_load:
4162 case scalar_store:
4163 case vector_stmt:
4164 case vector_load:
4165 case vector_store:
4166 case vec_to_scalar:
4167 case scalar_to_vec:
4168 case cond_branch_not_taken:
4169 return 1;
4171 case vec_perm:
4172 if (TARGET_VSX)
4173 return 3;
4174 else
4175 return 1;
4177 case vec_promote_demote:
4178 if (TARGET_VSX)
4179 return 4;
4180 else
4181 return 1;
4183 case cond_branch_taken:
4184 return 3;
4186 case unaligned_load:
4187 if (TARGET_VSX && TARGET_ALLOW_MOVMISALIGN)
4189 elements = TYPE_VECTOR_SUBPARTS (vectype);
4190 if (elements == 2)
4191 /* Double word aligned. */
4192 return 2;
4194 if (elements == 4)
4196 switch (misalign)
4198 case 8:
4199 /* Double word aligned. */
4200 return 2;
4202 case -1:
4203 /* Unknown misalignment. */
4204 case 4:
4205 case 12:
4206 /* Word aligned. */
4207 return 22;
4209 default:
4210 gcc_unreachable ();
4215 if (TARGET_ALTIVEC)
4216 /* Misaligned loads are not supported. */
4217 gcc_unreachable ();
4219 return 2;
4221 case unaligned_store:
4222 if (TARGET_VSX && TARGET_ALLOW_MOVMISALIGN)
4224 elements = TYPE_VECTOR_SUBPARTS (vectype);
4225 if (elements == 2)
4226 /* Double word aligned. */
4227 return 2;
4229 if (elements == 4)
4231 switch (misalign)
4233 case 8:
4234 /* Double word aligned. */
4235 return 2;
4237 case -1:
4238 /* Unknown misalignment. */
4239 case 4:
4240 case 12:
4241 /* Word aligned. */
4242 return 23;
4244 default:
4245 gcc_unreachable ();
4250 if (TARGET_ALTIVEC)
4251 /* Misaligned stores are not supported. */
4252 gcc_unreachable ();
4254 return 2;
4256 case vec_construct:
4257 elements = TYPE_VECTOR_SUBPARTS (vectype);
4258 elem_type = TREE_TYPE (vectype);
4259 /* 32-bit vectors loaded into registers are stored as double
4260 precision, so we need n/2 converts in addition to the usual
4261 n/2 merges to construct a vector of short floats from them. */
4262 if (SCALAR_FLOAT_TYPE_P (elem_type)
4263 && TYPE_PRECISION (elem_type) == 32)
4264 return elements + 1;
4265 else
4266 return elements / 2 + 1;
4268 default:
4269 gcc_unreachable ();
4273 /* Implement targetm.vectorize.preferred_simd_mode. */
4275 static enum machine_mode
4276 rs6000_preferred_simd_mode (enum machine_mode mode)
4278 if (TARGET_VSX)
4279 switch (mode)
4281 case DFmode:
4282 return V2DFmode;
4283 default:;
4285 if (TARGET_ALTIVEC || TARGET_VSX)
4286 switch (mode)
4288 case SFmode:
4289 return V4SFmode;
4290 case TImode:
4291 return V1TImode;
4292 case DImode:
4293 return V2DImode;
4294 case SImode:
4295 return V4SImode;
4296 case HImode:
4297 return V8HImode;
4298 case QImode:
4299 return V16QImode;
4300 default:;
4302 if (TARGET_SPE)
4303 switch (mode)
4305 case SFmode:
4306 return V2SFmode;
4307 case SImode:
4308 return V2SImode;
4309 default:;
4311 if (TARGET_PAIRED_FLOAT
4312 && mode == SFmode)
4313 return V2SFmode;
4314 return word_mode;
4317 typedef struct _rs6000_cost_data
4319 struct loop *loop_info;
4320 unsigned cost[3];
4321 } rs6000_cost_data;
4323 /* Test for likely overcommitment of vector hardware resources. If a
4324 loop iteration is relatively large, and too large a percentage of
4325 instructions in the loop are vectorized, the cost model may not
4326 adequately reflect delays from unavailable vector resources.
4327 Penalize the loop body cost for this case. */
4329 static void
4330 rs6000_density_test (rs6000_cost_data *data)
4332 const int DENSITY_PCT_THRESHOLD = 85;
4333 const int DENSITY_SIZE_THRESHOLD = 70;
4334 const int DENSITY_PENALTY = 10;
4335 struct loop *loop = data->loop_info;
4336 basic_block *bbs = get_loop_body (loop);
4337 int nbbs = loop->num_nodes;
4338 int vec_cost = data->cost[vect_body], not_vec_cost = 0;
4339 int i, density_pct;
4341 for (i = 0; i < nbbs; i++)
4343 basic_block bb = bbs[i];
4344 gimple_stmt_iterator gsi;
4346 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4348 gimple stmt = gsi_stmt (gsi);
4349 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4351 if (!STMT_VINFO_RELEVANT_P (stmt_info)
4352 && !STMT_VINFO_IN_PATTERN_P (stmt_info))
4353 not_vec_cost++;
4357 free (bbs);
4358 density_pct = (vec_cost * 100) / (vec_cost + not_vec_cost);
4360 if (density_pct > DENSITY_PCT_THRESHOLD
4361 && vec_cost + not_vec_cost > DENSITY_SIZE_THRESHOLD)
4363 data->cost[vect_body] = vec_cost * (100 + DENSITY_PENALTY) / 100;
4364 if (dump_enabled_p ())
4365 dump_printf_loc (MSG_NOTE, vect_location,
4366 "density %d%%, cost %d exceeds threshold, penalizing "
4367 "loop body cost by %d%%", density_pct,
4368 vec_cost + not_vec_cost, DENSITY_PENALTY);
4372 /* Implement targetm.vectorize.init_cost. */
4374 static void *
4375 rs6000_init_cost (struct loop *loop_info)
4377 rs6000_cost_data *data = XNEW (struct _rs6000_cost_data);
4378 data->loop_info = loop_info;
4379 data->cost[vect_prologue] = 0;
4380 data->cost[vect_body] = 0;
4381 data->cost[vect_epilogue] = 0;
4382 return data;
4385 /* Implement targetm.vectorize.add_stmt_cost. */
4387 static unsigned
4388 rs6000_add_stmt_cost (void *data, int count, enum vect_cost_for_stmt kind,
4389 struct _stmt_vec_info *stmt_info, int misalign,
4390 enum vect_cost_model_location where)
4392 rs6000_cost_data *cost_data = (rs6000_cost_data*) data;
4393 unsigned retval = 0;
4395 if (flag_vect_cost_model)
4397 tree vectype = stmt_info ? stmt_vectype (stmt_info) : NULL_TREE;
4398 int stmt_cost = rs6000_builtin_vectorization_cost (kind, vectype,
4399 misalign);
4400 /* Statements in an inner loop relative to the loop being
4401 vectorized are weighted more heavily. The value here is
4402 arbitrary and could potentially be improved with analysis. */
4403 if (where == vect_body && stmt_info && stmt_in_inner_loop_p (stmt_info))
4404 count *= 50; /* FIXME. */
4406 retval = (unsigned) (count * stmt_cost);
4407 cost_data->cost[where] += retval;
4410 return retval;
4413 /* Implement targetm.vectorize.finish_cost. */
4415 static void
4416 rs6000_finish_cost (void *data, unsigned *prologue_cost,
4417 unsigned *body_cost, unsigned *epilogue_cost)
4419 rs6000_cost_data *cost_data = (rs6000_cost_data*) data;
4421 if (cost_data->loop_info)
4422 rs6000_density_test (cost_data);
4424 *prologue_cost = cost_data->cost[vect_prologue];
4425 *body_cost = cost_data->cost[vect_body];
4426 *epilogue_cost = cost_data->cost[vect_epilogue];
4429 /* Implement targetm.vectorize.destroy_cost_data. */
4431 static void
4432 rs6000_destroy_cost_data (void *data)
4434 free (data);
4437 /* Handler for the Mathematical Acceleration Subsystem (mass) interface to a
4438 library with vectorized intrinsics. */
4440 static tree
4441 rs6000_builtin_vectorized_libmass (tree fndecl, tree type_out, tree type_in)
4443 char name[32];
4444 const char *suffix = NULL;
4445 tree fntype, new_fndecl, bdecl = NULL_TREE;
4446 int n_args = 1;
4447 const char *bname;
4448 enum machine_mode el_mode, in_mode;
4449 int n, in_n;
4451 /* Libmass is suitable for unsafe math only as it does not correctly support
4452 parts of IEEE with the required precision such as denormals. Only support
4453 it if we have VSX to use the simd d2 or f4 functions.
4454 XXX: Add variable length support. */
4455 if (!flag_unsafe_math_optimizations || !TARGET_VSX)
4456 return NULL_TREE;
4458 el_mode = TYPE_MODE (TREE_TYPE (type_out));
4459 n = TYPE_VECTOR_SUBPARTS (type_out);
4460 in_mode = TYPE_MODE (TREE_TYPE (type_in));
4461 in_n = TYPE_VECTOR_SUBPARTS (type_in);
4462 if (el_mode != in_mode
4463 || n != in_n)
4464 return NULL_TREE;
4466 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
4468 enum built_in_function fn = DECL_FUNCTION_CODE (fndecl);
4469 switch (fn)
4471 case BUILT_IN_ATAN2:
4472 case BUILT_IN_HYPOT:
4473 case BUILT_IN_POW:
4474 n_args = 2;
4475 /* fall through */
4477 case BUILT_IN_ACOS:
4478 case BUILT_IN_ACOSH:
4479 case BUILT_IN_ASIN:
4480 case BUILT_IN_ASINH:
4481 case BUILT_IN_ATAN:
4482 case BUILT_IN_ATANH:
4483 case BUILT_IN_CBRT:
4484 case BUILT_IN_COS:
4485 case BUILT_IN_COSH:
4486 case BUILT_IN_ERF:
4487 case BUILT_IN_ERFC:
4488 case BUILT_IN_EXP2:
4489 case BUILT_IN_EXP:
4490 case BUILT_IN_EXPM1:
4491 case BUILT_IN_LGAMMA:
4492 case BUILT_IN_LOG10:
4493 case BUILT_IN_LOG1P:
4494 case BUILT_IN_LOG2:
4495 case BUILT_IN_LOG:
4496 case BUILT_IN_SIN:
4497 case BUILT_IN_SINH:
4498 case BUILT_IN_SQRT:
4499 case BUILT_IN_TAN:
4500 case BUILT_IN_TANH:
4501 bdecl = builtin_decl_implicit (fn);
4502 suffix = "d2"; /* pow -> powd2 */
4503 if (el_mode != DFmode
4504 || n != 2
4505 || !bdecl)
4506 return NULL_TREE;
4507 break;
4509 case BUILT_IN_ATAN2F:
4510 case BUILT_IN_HYPOTF:
4511 case BUILT_IN_POWF:
4512 n_args = 2;
4513 /* fall through */
4515 case BUILT_IN_ACOSF:
4516 case BUILT_IN_ACOSHF:
4517 case BUILT_IN_ASINF:
4518 case BUILT_IN_ASINHF:
4519 case BUILT_IN_ATANF:
4520 case BUILT_IN_ATANHF:
4521 case BUILT_IN_CBRTF:
4522 case BUILT_IN_COSF:
4523 case BUILT_IN_COSHF:
4524 case BUILT_IN_ERFF:
4525 case BUILT_IN_ERFCF:
4526 case BUILT_IN_EXP2F:
4527 case BUILT_IN_EXPF:
4528 case BUILT_IN_EXPM1F:
4529 case BUILT_IN_LGAMMAF:
4530 case BUILT_IN_LOG10F:
4531 case BUILT_IN_LOG1PF:
4532 case BUILT_IN_LOG2F:
4533 case BUILT_IN_LOGF:
4534 case BUILT_IN_SINF:
4535 case BUILT_IN_SINHF:
4536 case BUILT_IN_SQRTF:
4537 case BUILT_IN_TANF:
4538 case BUILT_IN_TANHF:
4539 bdecl = builtin_decl_implicit (fn);
4540 suffix = "4"; /* powf -> powf4 */
4541 if (el_mode != SFmode
4542 || n != 4
4543 || !bdecl)
4544 return NULL_TREE;
4545 break;
4547 default:
4548 return NULL_TREE;
4551 else
4552 return NULL_TREE;
4554 gcc_assert (suffix != NULL);
4555 bname = IDENTIFIER_POINTER (DECL_NAME (bdecl));
4556 if (!bname)
4557 return NULL_TREE;
4559 strcpy (name, bname + sizeof ("__builtin_") - 1);
4560 strcat (name, suffix);
4562 if (n_args == 1)
4563 fntype = build_function_type_list (type_out, type_in, NULL);
4564 else if (n_args == 2)
4565 fntype = build_function_type_list (type_out, type_in, type_in, NULL);
4566 else
4567 gcc_unreachable ();
4569 /* Build a function declaration for the vectorized function. */
4570 new_fndecl = build_decl (BUILTINS_LOCATION,
4571 FUNCTION_DECL, get_identifier (name), fntype);
4572 TREE_PUBLIC (new_fndecl) = 1;
4573 DECL_EXTERNAL (new_fndecl) = 1;
4574 DECL_IS_NOVOPS (new_fndecl) = 1;
4575 TREE_READONLY (new_fndecl) = 1;
4577 return new_fndecl;
4580 /* Returns a function decl for a vectorized version of the builtin function
4581 with builtin function code FN and the result vector type TYPE, or NULL_TREE
4582 if it is not available. */
4584 static tree
4585 rs6000_builtin_vectorized_function (tree fndecl, tree type_out,
4586 tree type_in)
4588 enum machine_mode in_mode, out_mode;
4589 int in_n, out_n;
4591 if (TARGET_DEBUG_BUILTIN)
4592 fprintf (stderr, "rs6000_builtin_vectorized_function (%s, %s, %s)\n",
4593 IDENTIFIER_POINTER (DECL_NAME (fndecl)),
4594 GET_MODE_NAME (TYPE_MODE (type_out)),
4595 GET_MODE_NAME (TYPE_MODE (type_in)));
4597 if (TREE_CODE (type_out) != VECTOR_TYPE
4598 || TREE_CODE (type_in) != VECTOR_TYPE
4599 || !TARGET_VECTORIZE_BUILTINS)
4600 return NULL_TREE;
4602 out_mode = TYPE_MODE (TREE_TYPE (type_out));
4603 out_n = TYPE_VECTOR_SUBPARTS (type_out);
4604 in_mode = TYPE_MODE (TREE_TYPE (type_in));
4605 in_n = TYPE_VECTOR_SUBPARTS (type_in);
4607 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
4609 enum built_in_function fn = DECL_FUNCTION_CODE (fndecl);
4610 switch (fn)
4612 case BUILT_IN_CLZIMAX:
4613 case BUILT_IN_CLZLL:
4614 case BUILT_IN_CLZL:
4615 case BUILT_IN_CLZ:
4616 if (TARGET_P8_VECTOR && in_mode == out_mode && out_n == in_n)
4618 if (out_mode == QImode && out_n == 16)
4619 return rs6000_builtin_decls[P8V_BUILTIN_VCLZB];
4620 else if (out_mode == HImode && out_n == 8)
4621 return rs6000_builtin_decls[P8V_BUILTIN_VCLZH];
4622 else if (out_mode == SImode && out_n == 4)
4623 return rs6000_builtin_decls[P8V_BUILTIN_VCLZW];
4624 else if (out_mode == DImode && out_n == 2)
4625 return rs6000_builtin_decls[P8V_BUILTIN_VCLZD];
4627 break;
4628 case BUILT_IN_COPYSIGN:
4629 if (VECTOR_UNIT_VSX_P (V2DFmode)
4630 && out_mode == DFmode && out_n == 2
4631 && in_mode == DFmode && in_n == 2)
4632 return rs6000_builtin_decls[VSX_BUILTIN_CPSGNDP];
4633 break;
4634 case BUILT_IN_COPYSIGNF:
4635 if (out_mode != SFmode || out_n != 4
4636 || in_mode != SFmode || in_n != 4)
4637 break;
4638 if (VECTOR_UNIT_VSX_P (V4SFmode))
4639 return rs6000_builtin_decls[VSX_BUILTIN_CPSGNSP];
4640 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
4641 return rs6000_builtin_decls[ALTIVEC_BUILTIN_COPYSIGN_V4SF];
4642 break;
4643 case BUILT_IN_POPCOUNTIMAX:
4644 case BUILT_IN_POPCOUNTLL:
4645 case BUILT_IN_POPCOUNTL:
4646 case BUILT_IN_POPCOUNT:
4647 if (TARGET_P8_VECTOR && in_mode == out_mode && out_n == in_n)
4649 if (out_mode == QImode && out_n == 16)
4650 return rs6000_builtin_decls[P8V_BUILTIN_VPOPCNTB];
4651 else if (out_mode == HImode && out_n == 8)
4652 return rs6000_builtin_decls[P8V_BUILTIN_VPOPCNTH];
4653 else if (out_mode == SImode && out_n == 4)
4654 return rs6000_builtin_decls[P8V_BUILTIN_VPOPCNTW];
4655 else if (out_mode == DImode && out_n == 2)
4656 return rs6000_builtin_decls[P8V_BUILTIN_VPOPCNTD];
4658 break;
4659 case BUILT_IN_SQRT:
4660 if (VECTOR_UNIT_VSX_P (V2DFmode)
4661 && out_mode == DFmode && out_n == 2
4662 && in_mode == DFmode && in_n == 2)
4663 return rs6000_builtin_decls[VSX_BUILTIN_XVSQRTDP];
4664 break;
4665 case BUILT_IN_SQRTF:
4666 if (VECTOR_UNIT_VSX_P (V4SFmode)
4667 && out_mode == SFmode && out_n == 4
4668 && in_mode == SFmode && in_n == 4)
4669 return rs6000_builtin_decls[VSX_BUILTIN_XVSQRTSP];
4670 break;
4671 case BUILT_IN_CEIL:
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_XVRDPIP];
4676 break;
4677 case BUILT_IN_CEILF:
4678 if (out_mode != SFmode || out_n != 4
4679 || in_mode != SFmode || in_n != 4)
4680 break;
4681 if (VECTOR_UNIT_VSX_P (V4SFmode))
4682 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIP];
4683 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
4684 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIP];
4685 break;
4686 case BUILT_IN_FLOOR:
4687 if (VECTOR_UNIT_VSX_P (V2DFmode)
4688 && out_mode == DFmode && out_n == 2
4689 && in_mode == DFmode && in_n == 2)
4690 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIM];
4691 break;
4692 case BUILT_IN_FLOORF:
4693 if (out_mode != SFmode || out_n != 4
4694 || in_mode != SFmode || in_n != 4)
4695 break;
4696 if (VECTOR_UNIT_VSX_P (V4SFmode))
4697 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIM];
4698 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
4699 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIM];
4700 break;
4701 case BUILT_IN_FMA:
4702 if (VECTOR_UNIT_VSX_P (V2DFmode)
4703 && out_mode == DFmode && out_n == 2
4704 && in_mode == DFmode && in_n == 2)
4705 return rs6000_builtin_decls[VSX_BUILTIN_XVMADDDP];
4706 break;
4707 case BUILT_IN_FMAF:
4708 if (VECTOR_UNIT_VSX_P (V4SFmode)
4709 && out_mode == SFmode && out_n == 4
4710 && in_mode == SFmode && in_n == 4)
4711 return rs6000_builtin_decls[VSX_BUILTIN_XVMADDSP];
4712 else if (VECTOR_UNIT_ALTIVEC_P (V4SFmode)
4713 && out_mode == SFmode && out_n == 4
4714 && in_mode == SFmode && in_n == 4)
4715 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VMADDFP];
4716 break;
4717 case BUILT_IN_TRUNC:
4718 if (VECTOR_UNIT_VSX_P (V2DFmode)
4719 && out_mode == DFmode && out_n == 2
4720 && in_mode == DFmode && in_n == 2)
4721 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIZ];
4722 break;
4723 case BUILT_IN_TRUNCF:
4724 if (out_mode != SFmode || out_n != 4
4725 || in_mode != SFmode || in_n != 4)
4726 break;
4727 if (VECTOR_UNIT_VSX_P (V4SFmode))
4728 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIZ];
4729 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
4730 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIZ];
4731 break;
4732 case BUILT_IN_NEARBYINT:
4733 if (VECTOR_UNIT_VSX_P (V2DFmode)
4734 && flag_unsafe_math_optimizations
4735 && out_mode == DFmode && out_n == 2
4736 && in_mode == DFmode && in_n == 2)
4737 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPI];
4738 break;
4739 case BUILT_IN_NEARBYINTF:
4740 if (VECTOR_UNIT_VSX_P (V4SFmode)
4741 && flag_unsafe_math_optimizations
4742 && out_mode == SFmode && out_n == 4
4743 && in_mode == SFmode && in_n == 4)
4744 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPI];
4745 break;
4746 case BUILT_IN_RINT:
4747 if (VECTOR_UNIT_VSX_P (V2DFmode)
4748 && !flag_trapping_math
4749 && out_mode == DFmode && out_n == 2
4750 && in_mode == DFmode && in_n == 2)
4751 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIC];
4752 break;
4753 case BUILT_IN_RINTF:
4754 if (VECTOR_UNIT_VSX_P (V4SFmode)
4755 && !flag_trapping_math
4756 && out_mode == SFmode && out_n == 4
4757 && in_mode == SFmode && in_n == 4)
4758 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIC];
4759 break;
4760 default:
4761 break;
4765 else if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
4767 enum rs6000_builtins fn
4768 = (enum rs6000_builtins)DECL_FUNCTION_CODE (fndecl);
4769 switch (fn)
4771 case RS6000_BUILTIN_RSQRTF:
4772 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode)
4773 && out_mode == SFmode && out_n == 4
4774 && in_mode == SFmode && in_n == 4)
4775 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRSQRTFP];
4776 break;
4777 case RS6000_BUILTIN_RSQRT:
4778 if (VECTOR_UNIT_VSX_P (V2DFmode)
4779 && out_mode == DFmode && out_n == 2
4780 && in_mode == DFmode && in_n == 2)
4781 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_2DF];
4782 break;
4783 case RS6000_BUILTIN_RECIPF:
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_VRECIPFP];
4788 break;
4789 case RS6000_BUILTIN_RECIP:
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_RECIP_V2DF];
4794 break;
4795 default:
4796 break;
4800 /* Generate calls to libmass if appropriate. */
4801 if (rs6000_veclib_handler)
4802 return rs6000_veclib_handler (fndecl, type_out, type_in);
4804 return NULL_TREE;
4807 /* Default CPU string for rs6000*_file_start functions. */
4808 static const char *rs6000_default_cpu;
4810 /* Do anything needed at the start of the asm file. */
4812 static void
4813 rs6000_file_start (void)
4815 char buffer[80];
4816 const char *start = buffer;
4817 FILE *file = asm_out_file;
4819 rs6000_default_cpu = TARGET_CPU_DEFAULT;
4821 default_file_start ();
4823 if (flag_verbose_asm)
4825 sprintf (buffer, "\n%s rs6000/powerpc options:", ASM_COMMENT_START);
4827 if (rs6000_default_cpu != 0 && rs6000_default_cpu[0] != '\0')
4829 fprintf (file, "%s --with-cpu=%s", start, rs6000_default_cpu);
4830 start = "";
4833 if (global_options_set.x_rs6000_cpu_index)
4835 fprintf (file, "%s -mcpu=%s", start,
4836 processor_target_table[rs6000_cpu_index].name);
4837 start = "";
4840 if (global_options_set.x_rs6000_tune_index)
4842 fprintf (file, "%s -mtune=%s", start,
4843 processor_target_table[rs6000_tune_index].name);
4844 start = "";
4847 if (PPC405_ERRATUM77)
4849 fprintf (file, "%s PPC405CR_ERRATUM77", start);
4850 start = "";
4853 #ifdef USING_ELFOS_H
4854 switch (rs6000_sdata)
4856 case SDATA_NONE: fprintf (file, "%s -msdata=none", start); start = ""; break;
4857 case SDATA_DATA: fprintf (file, "%s -msdata=data", start); start = ""; break;
4858 case SDATA_SYSV: fprintf (file, "%s -msdata=sysv", start); start = ""; break;
4859 case SDATA_EABI: fprintf (file, "%s -msdata=eabi", start); start = ""; break;
4862 if (rs6000_sdata && g_switch_value)
4864 fprintf (file, "%s -G %d", start,
4865 g_switch_value);
4866 start = "";
4868 #endif
4870 if (*start == '\0')
4871 putc ('\n', file);
4874 if (DEFAULT_ABI == ABI_ELFv2)
4875 fprintf (file, "\t.abiversion 2\n");
4877 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2
4878 || (TARGET_ELF && flag_pic == 2))
4880 switch_to_section (toc_section);
4881 switch_to_section (text_section);
4886 /* Return nonzero if this function is known to have a null epilogue. */
4889 direct_return (void)
4891 if (reload_completed)
4893 rs6000_stack_t *info = rs6000_stack_info ();
4895 if (info->first_gp_reg_save == 32
4896 && info->first_fp_reg_save == 64
4897 && info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1
4898 && ! info->lr_save_p
4899 && ! info->cr_save_p
4900 && info->vrsave_mask == 0
4901 && ! info->push_p)
4902 return 1;
4905 return 0;
4908 /* Return the number of instructions it takes to form a constant in an
4909 integer register. */
4912 num_insns_constant_wide (HOST_WIDE_INT value)
4914 /* signed constant loadable with addi */
4915 if ((unsigned HOST_WIDE_INT) (value + 0x8000) < 0x10000)
4916 return 1;
4918 /* constant loadable with addis */
4919 else if ((value & 0xffff) == 0
4920 && (value >> 31 == -1 || value >> 31 == 0))
4921 return 1;
4923 else if (TARGET_POWERPC64)
4925 HOST_WIDE_INT low = ((value & 0xffffffff) ^ 0x80000000) - 0x80000000;
4926 HOST_WIDE_INT high = value >> 31;
4928 if (high == 0 || high == -1)
4929 return 2;
4931 high >>= 1;
4933 if (low == 0)
4934 return num_insns_constant_wide (high) + 1;
4935 else if (high == 0)
4936 return num_insns_constant_wide (low) + 1;
4937 else
4938 return (num_insns_constant_wide (high)
4939 + num_insns_constant_wide (low) + 1);
4942 else
4943 return 2;
4947 num_insns_constant (rtx op, enum machine_mode mode)
4949 HOST_WIDE_INT low, high;
4951 switch (GET_CODE (op))
4953 case CONST_INT:
4954 if ((INTVAL (op) >> 31) != 0 && (INTVAL (op) >> 31) != -1
4955 && mask64_operand (op, mode))
4956 return 2;
4957 else
4958 return num_insns_constant_wide (INTVAL (op));
4960 case CONST_DOUBLE:
4961 if (mode == SFmode || mode == SDmode)
4963 long l;
4964 REAL_VALUE_TYPE rv;
4966 REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
4967 if (DECIMAL_FLOAT_MODE_P (mode))
4968 REAL_VALUE_TO_TARGET_DECIMAL32 (rv, l);
4969 else
4970 REAL_VALUE_TO_TARGET_SINGLE (rv, l);
4971 return num_insns_constant_wide ((HOST_WIDE_INT) l);
4974 long l[2];
4975 REAL_VALUE_TYPE rv;
4977 REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
4978 if (DECIMAL_FLOAT_MODE_P (mode))
4979 REAL_VALUE_TO_TARGET_DECIMAL64 (rv, l);
4980 else
4981 REAL_VALUE_TO_TARGET_DOUBLE (rv, l);
4982 high = l[WORDS_BIG_ENDIAN == 0];
4983 low = l[WORDS_BIG_ENDIAN != 0];
4985 if (TARGET_32BIT)
4986 return (num_insns_constant_wide (low)
4987 + num_insns_constant_wide (high));
4988 else
4990 if ((high == 0 && low >= 0)
4991 || (high == -1 && low < 0))
4992 return num_insns_constant_wide (low);
4994 else if (mask64_operand (op, mode))
4995 return 2;
4997 else if (low == 0)
4998 return num_insns_constant_wide (high) + 1;
5000 else
5001 return (num_insns_constant_wide (high)
5002 + num_insns_constant_wide (low) + 1);
5005 default:
5006 gcc_unreachable ();
5010 /* Interpret element ELT of the CONST_VECTOR OP as an integer value.
5011 If the mode of OP is MODE_VECTOR_INT, this simply returns the
5012 corresponding element of the vector, but for V4SFmode and V2SFmode,
5013 the corresponding "float" is interpreted as an SImode integer. */
5015 HOST_WIDE_INT
5016 const_vector_elt_as_int (rtx op, unsigned int elt)
5018 rtx tmp;
5020 /* We can't handle V2DImode and V2DFmode vector constants here yet. */
5021 gcc_assert (GET_MODE (op) != V2DImode
5022 && GET_MODE (op) != V2DFmode);
5024 tmp = CONST_VECTOR_ELT (op, elt);
5025 if (GET_MODE (op) == V4SFmode
5026 || GET_MODE (op) == V2SFmode)
5027 tmp = gen_lowpart (SImode, tmp);
5028 return INTVAL (tmp);
5031 /* Return true if OP can be synthesized with a particular vspltisb, vspltish
5032 or vspltisw instruction. OP is a CONST_VECTOR. Which instruction is used
5033 depends on STEP and COPIES, one of which will be 1. If COPIES > 1,
5034 all items are set to the same value and contain COPIES replicas of the
5035 vsplt's operand; if STEP > 1, one in STEP elements is set to the vsplt's
5036 operand and the others are set to the value of the operand's msb. */
5038 static bool
5039 vspltis_constant (rtx op, unsigned step, unsigned copies)
5041 enum machine_mode mode = GET_MODE (op);
5042 enum machine_mode inner = GET_MODE_INNER (mode);
5044 unsigned i;
5045 unsigned nunits;
5046 unsigned bitsize;
5047 unsigned mask;
5049 HOST_WIDE_INT val;
5050 HOST_WIDE_INT splat_val;
5051 HOST_WIDE_INT msb_val;
5053 if (mode == V2DImode || mode == V2DFmode || mode == V1TImode)
5054 return false;
5056 nunits = GET_MODE_NUNITS (mode);
5057 bitsize = GET_MODE_BITSIZE (inner);
5058 mask = GET_MODE_MASK (inner);
5060 val = const_vector_elt_as_int (op, BYTES_BIG_ENDIAN ? nunits - 1 : 0);
5061 splat_val = val;
5062 msb_val = val >= 0 ? 0 : -1;
5064 /* Construct the value to be splatted, if possible. If not, return 0. */
5065 for (i = 2; i <= copies; i *= 2)
5067 HOST_WIDE_INT small_val;
5068 bitsize /= 2;
5069 small_val = splat_val >> bitsize;
5070 mask >>= bitsize;
5071 if (splat_val != ((small_val << bitsize) | (small_val & mask)))
5072 return false;
5073 splat_val = small_val;
5076 /* Check if SPLAT_VAL can really be the operand of a vspltis[bhw]. */
5077 if (EASY_VECTOR_15 (splat_val))
5080 /* Also check if we can splat, and then add the result to itself. Do so if
5081 the value is positive, of if the splat instruction is using OP's mode;
5082 for splat_val < 0, the splat and the add should use the same mode. */
5083 else if (EASY_VECTOR_15_ADD_SELF (splat_val)
5084 && (splat_val >= 0 || (step == 1 && copies == 1)))
5087 /* Also check if are loading up the most significant bit which can be done by
5088 loading up -1 and shifting the value left by -1. */
5089 else if (EASY_VECTOR_MSB (splat_val, inner))
5092 else
5093 return false;
5095 /* Check if VAL is present in every STEP-th element, and the
5096 other elements are filled with its most significant bit. */
5097 for (i = 1; i < nunits; ++i)
5099 HOST_WIDE_INT desired_val;
5100 unsigned elt = BYTES_BIG_ENDIAN ? nunits - 1 - i : i;
5101 if ((i & (step - 1)) == 0)
5102 desired_val = val;
5103 else
5104 desired_val = msb_val;
5106 if (desired_val != const_vector_elt_as_int (op, elt))
5107 return false;
5110 return true;
5114 /* Return true if OP is of the given MODE and can be synthesized
5115 with a vspltisb, vspltish or vspltisw. */
5117 bool
5118 easy_altivec_constant (rtx op, enum machine_mode mode)
5120 unsigned step, copies;
5122 if (mode == VOIDmode)
5123 mode = GET_MODE (op);
5124 else if (mode != GET_MODE (op))
5125 return false;
5127 /* V2DI/V2DF was added with VSX. Only allow 0 and all 1's as easy
5128 constants. */
5129 if (mode == V2DFmode)
5130 return zero_constant (op, mode);
5132 else if (mode == V2DImode)
5134 /* In case the compiler is built 32-bit, CONST_DOUBLE constants are not
5135 easy. */
5136 if (GET_CODE (CONST_VECTOR_ELT (op, 0)) != CONST_INT
5137 || GET_CODE (CONST_VECTOR_ELT (op, 1)) != CONST_INT)
5138 return false;
5140 if (zero_constant (op, mode))
5141 return true;
5143 if (INTVAL (CONST_VECTOR_ELT (op, 0)) == -1
5144 && INTVAL (CONST_VECTOR_ELT (op, 1)) == -1)
5145 return true;
5147 return false;
5150 /* V1TImode is a special container for TImode. Ignore for now. */
5151 else if (mode == V1TImode)
5152 return false;
5154 /* Start with a vspltisw. */
5155 step = GET_MODE_NUNITS (mode) / 4;
5156 copies = 1;
5158 if (vspltis_constant (op, step, copies))
5159 return true;
5161 /* Then try with a vspltish. */
5162 if (step == 1)
5163 copies <<= 1;
5164 else
5165 step >>= 1;
5167 if (vspltis_constant (op, step, copies))
5168 return true;
5170 /* And finally a vspltisb. */
5171 if (step == 1)
5172 copies <<= 1;
5173 else
5174 step >>= 1;
5176 if (vspltis_constant (op, step, copies))
5177 return true;
5179 return false;
5182 /* Generate a VEC_DUPLICATE representing a vspltis[bhw] instruction whose
5183 result is OP. Abort if it is not possible. */
5186 gen_easy_altivec_constant (rtx op)
5188 enum machine_mode mode = GET_MODE (op);
5189 int nunits = GET_MODE_NUNITS (mode);
5190 rtx val = CONST_VECTOR_ELT (op, BYTES_BIG_ENDIAN ? nunits - 1 : 0);
5191 unsigned step = nunits / 4;
5192 unsigned copies = 1;
5194 /* Start with a vspltisw. */
5195 if (vspltis_constant (op, step, copies))
5196 return gen_rtx_VEC_DUPLICATE (V4SImode, gen_lowpart (SImode, val));
5198 /* Then try with a vspltish. */
5199 if (step == 1)
5200 copies <<= 1;
5201 else
5202 step >>= 1;
5204 if (vspltis_constant (op, step, copies))
5205 return gen_rtx_VEC_DUPLICATE (V8HImode, gen_lowpart (HImode, val));
5207 /* And finally a vspltisb. */
5208 if (step == 1)
5209 copies <<= 1;
5210 else
5211 step >>= 1;
5213 if (vspltis_constant (op, step, copies))
5214 return gen_rtx_VEC_DUPLICATE (V16QImode, gen_lowpart (QImode, val));
5216 gcc_unreachable ();
5219 const char *
5220 output_vec_const_move (rtx *operands)
5222 int cst, cst2;
5223 enum machine_mode mode;
5224 rtx dest, vec;
5226 dest = operands[0];
5227 vec = operands[1];
5228 mode = GET_MODE (dest);
5230 if (TARGET_VSX)
5232 if (zero_constant (vec, mode))
5233 return "xxlxor %x0,%x0,%x0";
5235 if ((mode == V2DImode || mode == V1TImode)
5236 && INTVAL (CONST_VECTOR_ELT (vec, 0)) == -1
5237 && INTVAL (CONST_VECTOR_ELT (vec, 1)) == -1)
5238 return "vspltisw %0,-1";
5241 if (TARGET_ALTIVEC)
5243 rtx splat_vec;
5244 if (zero_constant (vec, mode))
5245 return "vxor %0,%0,%0";
5247 splat_vec = gen_easy_altivec_constant (vec);
5248 gcc_assert (GET_CODE (splat_vec) == VEC_DUPLICATE);
5249 operands[1] = XEXP (splat_vec, 0);
5250 if (!EASY_VECTOR_15 (INTVAL (operands[1])))
5251 return "#";
5253 switch (GET_MODE (splat_vec))
5255 case V4SImode:
5256 return "vspltisw %0,%1";
5258 case V8HImode:
5259 return "vspltish %0,%1";
5261 case V16QImode:
5262 return "vspltisb %0,%1";
5264 default:
5265 gcc_unreachable ();
5269 gcc_assert (TARGET_SPE);
5271 /* Vector constant 0 is handled as a splitter of V2SI, and in the
5272 pattern of V1DI, V4HI, and V2SF.
5274 FIXME: We should probably return # and add post reload
5275 splitters for these, but this way is so easy ;-). */
5276 cst = INTVAL (CONST_VECTOR_ELT (vec, 0));
5277 cst2 = INTVAL (CONST_VECTOR_ELT (vec, 1));
5278 operands[1] = CONST_VECTOR_ELT (vec, 0);
5279 operands[2] = CONST_VECTOR_ELT (vec, 1);
5280 if (cst == cst2)
5281 return "li %0,%1\n\tevmergelo %0,%0,%0";
5282 else
5283 return "li %0,%1\n\tevmergelo %0,%0,%0\n\tli %0,%2";
5286 /* Initialize TARGET of vector PAIRED to VALS. */
5288 void
5289 paired_expand_vector_init (rtx target, rtx vals)
5291 enum machine_mode mode = GET_MODE (target);
5292 int n_elts = GET_MODE_NUNITS (mode);
5293 int n_var = 0;
5294 rtx x, new_rtx, tmp, constant_op, op1, op2;
5295 int i;
5297 for (i = 0; i < n_elts; ++i)
5299 x = XVECEXP (vals, 0, i);
5300 if (!(CONST_INT_P (x)
5301 || GET_CODE (x) == CONST_DOUBLE
5302 || GET_CODE (x) == CONST_FIXED))
5303 ++n_var;
5305 if (n_var == 0)
5307 /* Load from constant pool. */
5308 emit_move_insn (target, gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)));
5309 return;
5312 if (n_var == 2)
5314 /* The vector is initialized only with non-constants. */
5315 new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, XVECEXP (vals, 0, 0),
5316 XVECEXP (vals, 0, 1));
5318 emit_move_insn (target, new_rtx);
5319 return;
5322 /* One field is non-constant and the other one is a constant. Load the
5323 constant from the constant pool and use ps_merge instruction to
5324 construct the whole vector. */
5325 op1 = XVECEXP (vals, 0, 0);
5326 op2 = XVECEXP (vals, 0, 1);
5328 constant_op = (CONSTANT_P (op1)) ? op1 : op2;
5330 tmp = gen_reg_rtx (GET_MODE (constant_op));
5331 emit_move_insn (tmp, constant_op);
5333 if (CONSTANT_P (op1))
5334 new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, tmp, op2);
5335 else
5336 new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, op1, tmp);
5338 emit_move_insn (target, new_rtx);
5341 void
5342 paired_expand_vector_move (rtx operands[])
5344 rtx op0 = operands[0], op1 = operands[1];
5346 emit_move_insn (op0, op1);
5349 /* Emit vector compare for code RCODE. DEST is destination, OP1 and
5350 OP2 are two VEC_COND_EXPR operands, CC_OP0 and CC_OP1 are the two
5351 operands for the relation operation COND. This is a recursive
5352 function. */
5354 static void
5355 paired_emit_vector_compare (enum rtx_code rcode,
5356 rtx dest, rtx op0, rtx op1,
5357 rtx cc_op0, rtx cc_op1)
5359 rtx tmp = gen_reg_rtx (V2SFmode);
5360 rtx tmp1, max, min;
5362 gcc_assert (TARGET_PAIRED_FLOAT);
5363 gcc_assert (GET_MODE (op0) == GET_MODE (op1));
5365 switch (rcode)
5367 case LT:
5368 case LTU:
5369 paired_emit_vector_compare (GE, dest, op1, op0, cc_op0, cc_op1);
5370 return;
5371 case GE:
5372 case GEU:
5373 emit_insn (gen_subv2sf3 (tmp, cc_op0, cc_op1));
5374 emit_insn (gen_selv2sf4 (dest, tmp, op0, op1, CONST0_RTX (SFmode)));
5375 return;
5376 case LE:
5377 case LEU:
5378 paired_emit_vector_compare (GE, dest, op0, op1, cc_op1, cc_op0);
5379 return;
5380 case GT:
5381 paired_emit_vector_compare (LE, dest, op1, op0, cc_op0, cc_op1);
5382 return;
5383 case EQ:
5384 tmp1 = gen_reg_rtx (V2SFmode);
5385 max = gen_reg_rtx (V2SFmode);
5386 min = gen_reg_rtx (V2SFmode);
5387 gen_reg_rtx (V2SFmode);
5389 emit_insn (gen_subv2sf3 (tmp, cc_op0, cc_op1));
5390 emit_insn (gen_selv2sf4
5391 (max, tmp, cc_op0, cc_op1, CONST0_RTX (SFmode)));
5392 emit_insn (gen_subv2sf3 (tmp, cc_op1, cc_op0));
5393 emit_insn (gen_selv2sf4
5394 (min, tmp, cc_op0, cc_op1, CONST0_RTX (SFmode)));
5395 emit_insn (gen_subv2sf3 (tmp1, min, max));
5396 emit_insn (gen_selv2sf4 (dest, tmp1, op0, op1, CONST0_RTX (SFmode)));
5397 return;
5398 case NE:
5399 paired_emit_vector_compare (EQ, dest, op1, op0, cc_op0, cc_op1);
5400 return;
5401 case UNLE:
5402 paired_emit_vector_compare (LE, dest, op1, op0, cc_op0, cc_op1);
5403 return;
5404 case UNLT:
5405 paired_emit_vector_compare (LT, dest, op1, op0, cc_op0, cc_op1);
5406 return;
5407 case UNGE:
5408 paired_emit_vector_compare (GE, dest, op1, op0, cc_op0, cc_op1);
5409 return;
5410 case UNGT:
5411 paired_emit_vector_compare (GT, dest, op1, op0, cc_op0, cc_op1);
5412 return;
5413 default:
5414 gcc_unreachable ();
5417 return;
5420 /* Emit vector conditional expression.
5421 DEST is destination. OP1 and OP2 are two VEC_COND_EXPR operands.
5422 CC_OP0 and CC_OP1 are the two operands for the relation operation COND. */
5425 paired_emit_vector_cond_expr (rtx dest, rtx op1, rtx op2,
5426 rtx cond, rtx cc_op0, rtx cc_op1)
5428 enum rtx_code rcode = GET_CODE (cond);
5430 if (!TARGET_PAIRED_FLOAT)
5431 return 0;
5433 paired_emit_vector_compare (rcode, dest, op1, op2, cc_op0, cc_op1);
5435 return 1;
5438 /* Initialize vector TARGET to VALS. */
5440 void
5441 rs6000_expand_vector_init (rtx target, rtx vals)
5443 enum machine_mode mode = GET_MODE (target);
5444 enum machine_mode inner_mode = GET_MODE_INNER (mode);
5445 int n_elts = GET_MODE_NUNITS (mode);
5446 int n_var = 0, one_var = -1;
5447 bool all_same = true, all_const_zero = true;
5448 rtx x, mem;
5449 int i;
5451 for (i = 0; i < n_elts; ++i)
5453 x = XVECEXP (vals, 0, i);
5454 if (!(CONST_INT_P (x)
5455 || GET_CODE (x) == CONST_DOUBLE
5456 || GET_CODE (x) == CONST_FIXED))
5457 ++n_var, one_var = i;
5458 else if (x != CONST0_RTX (inner_mode))
5459 all_const_zero = false;
5461 if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
5462 all_same = false;
5465 if (n_var == 0)
5467 rtx const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0));
5468 bool int_vector_p = (GET_MODE_CLASS (mode) == MODE_VECTOR_INT);
5469 if ((int_vector_p || TARGET_VSX) && all_const_zero)
5471 /* Zero register. */
5472 emit_insn (gen_rtx_SET (VOIDmode, target,
5473 gen_rtx_XOR (mode, target, target)));
5474 return;
5476 else if (int_vector_p && easy_vector_constant (const_vec, mode))
5478 /* Splat immediate. */
5479 emit_insn (gen_rtx_SET (VOIDmode, target, const_vec));
5480 return;
5482 else
5484 /* Load from constant pool. */
5485 emit_move_insn (target, const_vec);
5486 return;
5490 /* Double word values on VSX can use xxpermdi or lxvdsx. */
5491 if (VECTOR_MEM_VSX_P (mode) && (mode == V2DFmode || mode == V2DImode))
5493 rtx op0 = XVECEXP (vals, 0, 0);
5494 rtx op1 = XVECEXP (vals, 0, 1);
5495 if (all_same)
5497 if (!MEM_P (op0) && !REG_P (op0))
5498 op0 = force_reg (inner_mode, op0);
5499 if (mode == V2DFmode)
5500 emit_insn (gen_vsx_splat_v2df (target, op0));
5501 else
5502 emit_insn (gen_vsx_splat_v2di (target, op0));
5504 else
5506 op0 = force_reg (inner_mode, op0);
5507 op1 = force_reg (inner_mode, op1);
5508 if (mode == V2DFmode)
5509 emit_insn (gen_vsx_concat_v2df (target, op0, op1));
5510 else
5511 emit_insn (gen_vsx_concat_v2di (target, op0, op1));
5513 return;
5516 /* With single precision floating point on VSX, know that internally single
5517 precision is actually represented as a double, and either make 2 V2DF
5518 vectors, and convert these vectors to single precision, or do one
5519 conversion, and splat the result to the other elements. */
5520 if (mode == V4SFmode && VECTOR_MEM_VSX_P (mode))
5522 if (all_same)
5524 rtx freg = gen_reg_rtx (V4SFmode);
5525 rtx sreg = force_reg (SFmode, XVECEXP (vals, 0, 0));
5526 rtx cvt = ((TARGET_XSCVDPSPN)
5527 ? gen_vsx_xscvdpspn_scalar (freg, sreg)
5528 : gen_vsx_xscvdpsp_scalar (freg, sreg));
5530 emit_insn (cvt);
5531 emit_insn (gen_vsx_xxspltw_v4sf_direct (target, freg, const0_rtx));
5533 else
5535 rtx dbl_even = gen_reg_rtx (V2DFmode);
5536 rtx dbl_odd = gen_reg_rtx (V2DFmode);
5537 rtx flt_even = gen_reg_rtx (V4SFmode);
5538 rtx flt_odd = gen_reg_rtx (V4SFmode);
5539 rtx op0 = force_reg (SFmode, XVECEXP (vals, 0, 0));
5540 rtx op1 = force_reg (SFmode, XVECEXP (vals, 0, 1));
5541 rtx op2 = force_reg (SFmode, XVECEXP (vals, 0, 2));
5542 rtx op3 = force_reg (SFmode, XVECEXP (vals, 0, 3));
5544 emit_insn (gen_vsx_concat_v2sf (dbl_even, op0, op1));
5545 emit_insn (gen_vsx_concat_v2sf (dbl_odd, op2, op3));
5546 emit_insn (gen_vsx_xvcvdpsp (flt_even, dbl_even));
5547 emit_insn (gen_vsx_xvcvdpsp (flt_odd, dbl_odd));
5548 rs6000_expand_extract_even (target, flt_even, flt_odd);
5550 return;
5553 /* Store value to stack temp. Load vector element. Splat. However, splat
5554 of 64-bit items is not supported on Altivec. */
5555 if (all_same && GET_MODE_SIZE (inner_mode) <= 4)
5557 mem = assign_stack_temp (mode, GET_MODE_SIZE (inner_mode));
5558 emit_move_insn (adjust_address_nv (mem, inner_mode, 0),
5559 XVECEXP (vals, 0, 0));
5560 x = gen_rtx_UNSPEC (VOIDmode,
5561 gen_rtvec (1, const0_rtx), UNSPEC_LVE);
5562 emit_insn (gen_rtx_PARALLEL (VOIDmode,
5563 gen_rtvec (2,
5564 gen_rtx_SET (VOIDmode,
5565 target, mem),
5566 x)));
5567 x = gen_rtx_VEC_SELECT (inner_mode, target,
5568 gen_rtx_PARALLEL (VOIDmode,
5569 gen_rtvec (1, const0_rtx)));
5570 emit_insn (gen_rtx_SET (VOIDmode, target,
5571 gen_rtx_VEC_DUPLICATE (mode, x)));
5572 return;
5575 /* One field is non-constant. Load constant then overwrite
5576 varying field. */
5577 if (n_var == 1)
5579 rtx copy = copy_rtx (vals);
5581 /* Load constant part of vector, substitute neighboring value for
5582 varying element. */
5583 XVECEXP (copy, 0, one_var) = XVECEXP (vals, 0, (one_var + 1) % n_elts);
5584 rs6000_expand_vector_init (target, copy);
5586 /* Insert variable. */
5587 rs6000_expand_vector_set (target, XVECEXP (vals, 0, one_var), one_var);
5588 return;
5591 /* Construct the vector in memory one field at a time
5592 and load the whole vector. */
5593 mem = assign_stack_temp (mode, GET_MODE_SIZE (mode));
5594 for (i = 0; i < n_elts; i++)
5595 emit_move_insn (adjust_address_nv (mem, inner_mode,
5596 i * GET_MODE_SIZE (inner_mode)),
5597 XVECEXP (vals, 0, i));
5598 emit_move_insn (target, mem);
5601 /* Set field ELT of TARGET to VAL. */
5603 void
5604 rs6000_expand_vector_set (rtx target, rtx val, int elt)
5606 enum machine_mode mode = GET_MODE (target);
5607 enum machine_mode inner_mode = GET_MODE_INNER (mode);
5608 rtx reg = gen_reg_rtx (mode);
5609 rtx mask, mem, x;
5610 int width = GET_MODE_SIZE (inner_mode);
5611 int i;
5613 if (VECTOR_MEM_VSX_P (mode) && (mode == V2DFmode || mode == V2DImode))
5615 rtx (*set_func) (rtx, rtx, rtx, rtx)
5616 = ((mode == V2DFmode) ? gen_vsx_set_v2df : gen_vsx_set_v2di);
5617 emit_insn (set_func (target, target, val, GEN_INT (elt)));
5618 return;
5621 /* Simplify setting single element vectors like V1TImode. */
5622 if (GET_MODE_SIZE (mode) == GET_MODE_SIZE (inner_mode) && elt == 0)
5624 emit_move_insn (target, gen_lowpart (mode, val));
5625 return;
5628 /* Load single variable value. */
5629 mem = assign_stack_temp (mode, GET_MODE_SIZE (inner_mode));
5630 emit_move_insn (adjust_address_nv (mem, inner_mode, 0), val);
5631 x = gen_rtx_UNSPEC (VOIDmode,
5632 gen_rtvec (1, const0_rtx), UNSPEC_LVE);
5633 emit_insn (gen_rtx_PARALLEL (VOIDmode,
5634 gen_rtvec (2,
5635 gen_rtx_SET (VOIDmode,
5636 reg, mem),
5637 x)));
5639 /* Linear sequence. */
5640 mask = gen_rtx_PARALLEL (V16QImode, rtvec_alloc (16));
5641 for (i = 0; i < 16; ++i)
5642 XVECEXP (mask, 0, i) = GEN_INT (i);
5644 /* Set permute mask to insert element into target. */
5645 for (i = 0; i < width; ++i)
5646 XVECEXP (mask, 0, elt*width + i)
5647 = GEN_INT (i + 0x10);
5648 x = gen_rtx_CONST_VECTOR (V16QImode, XVEC (mask, 0));
5650 if (BYTES_BIG_ENDIAN)
5651 x = gen_rtx_UNSPEC (mode,
5652 gen_rtvec (3, target, reg,
5653 force_reg (V16QImode, x)),
5654 UNSPEC_VPERM);
5655 else
5657 /* Invert selector. We prefer to generate VNAND on P8 so
5658 that future fusion opportunities can kick in, but must
5659 generate VNOR elsewhere. */
5660 rtx notx = gen_rtx_NOT (V16QImode, force_reg (V16QImode, x));
5661 rtx iorx = (TARGET_P8_VECTOR
5662 ? gen_rtx_IOR (V16QImode, notx, notx)
5663 : gen_rtx_AND (V16QImode, notx, notx));
5664 rtx tmp = gen_reg_rtx (V16QImode);
5665 emit_insn (gen_rtx_SET (VOIDmode, tmp, iorx));
5667 /* Permute with operands reversed and adjusted selector. */
5668 x = gen_rtx_UNSPEC (mode, gen_rtvec (3, reg, target, tmp),
5669 UNSPEC_VPERM);
5672 emit_insn (gen_rtx_SET (VOIDmode, target, x));
5675 /* Extract field ELT from VEC into TARGET. */
5677 void
5678 rs6000_expand_vector_extract (rtx target, rtx vec, int elt)
5680 enum machine_mode mode = GET_MODE (vec);
5681 enum machine_mode inner_mode = GET_MODE_INNER (mode);
5682 rtx mem;
5684 if (VECTOR_MEM_VSX_P (mode))
5686 switch (mode)
5688 default:
5689 break;
5690 case V1TImode:
5691 gcc_assert (elt == 0 && inner_mode == TImode);
5692 emit_move_insn (target, gen_lowpart (TImode, vec));
5693 break;
5694 case V2DFmode:
5695 emit_insn (gen_vsx_extract_v2df (target, vec, GEN_INT (elt)));
5696 return;
5697 case V2DImode:
5698 emit_insn (gen_vsx_extract_v2di (target, vec, GEN_INT (elt)));
5699 return;
5700 case V4SFmode:
5701 emit_insn (gen_vsx_extract_v4sf (target, vec, GEN_INT (elt)));
5702 return;
5706 /* Allocate mode-sized buffer. */
5707 mem = assign_stack_temp (mode, GET_MODE_SIZE (mode));
5709 emit_move_insn (mem, vec);
5711 /* Add offset to field within buffer matching vector element. */
5712 mem = adjust_address_nv (mem, inner_mode, elt * GET_MODE_SIZE (inner_mode));
5714 emit_move_insn (target, adjust_address_nv (mem, inner_mode, 0));
5717 /* Generates shifts and masks for a pair of rldicl or rldicr insns to
5718 implement ANDing by the mask IN. */
5719 void
5720 build_mask64_2_operands (rtx in, rtx *out)
5722 unsigned HOST_WIDE_INT c, lsb, m1, m2;
5723 int shift;
5725 gcc_assert (GET_CODE (in) == CONST_INT);
5727 c = INTVAL (in);
5728 if (c & 1)
5730 /* Assume c initially something like 0x00fff000000fffff. The idea
5731 is to rotate the word so that the middle ^^^^^^ group of zeros
5732 is at the MS end and can be cleared with an rldicl mask. We then
5733 rotate back and clear off the MS ^^ group of zeros with a
5734 second rldicl. */
5735 c = ~c; /* c == 0xff000ffffff00000 */
5736 lsb = c & -c; /* lsb == 0x0000000000100000 */
5737 m1 = -lsb; /* m1 == 0xfffffffffff00000 */
5738 c = ~c; /* c == 0x00fff000000fffff */
5739 c &= -lsb; /* c == 0x00fff00000000000 */
5740 lsb = c & -c; /* lsb == 0x0000100000000000 */
5741 c = ~c; /* c == 0xff000fffffffffff */
5742 c &= -lsb; /* c == 0xff00000000000000 */
5743 shift = 0;
5744 while ((lsb >>= 1) != 0)
5745 shift++; /* shift == 44 on exit from loop */
5746 m1 <<= 64 - shift; /* m1 == 0xffffff0000000000 */
5747 m1 = ~m1; /* m1 == 0x000000ffffffffff */
5748 m2 = ~c; /* m2 == 0x00ffffffffffffff */
5750 else
5752 /* Assume c initially something like 0xff000f0000000000. The idea
5753 is to rotate the word so that the ^^^ middle group of zeros
5754 is at the LS end and can be cleared with an rldicr mask. We then
5755 rotate back and clear off the LS group of ^^^^^^^^^^ zeros with
5756 a second rldicr. */
5757 lsb = c & -c; /* lsb == 0x0000010000000000 */
5758 m2 = -lsb; /* m2 == 0xffffff0000000000 */
5759 c = ~c; /* c == 0x00fff0ffffffffff */
5760 c &= -lsb; /* c == 0x00fff00000000000 */
5761 lsb = c & -c; /* lsb == 0x0000100000000000 */
5762 c = ~c; /* c == 0xff000fffffffffff */
5763 c &= -lsb; /* c == 0xff00000000000000 */
5764 shift = 0;
5765 while ((lsb >>= 1) != 0)
5766 shift++; /* shift == 44 on exit from loop */
5767 m1 = ~c; /* m1 == 0x00ffffffffffffff */
5768 m1 >>= shift; /* m1 == 0x0000000000000fff */
5769 m1 = ~m1; /* m1 == 0xfffffffffffff000 */
5772 /* Note that when we only have two 0->1 and 1->0 transitions, one of the
5773 masks will be all 1's. We are guaranteed more than one transition. */
5774 out[0] = GEN_INT (64 - shift);
5775 out[1] = GEN_INT (m1);
5776 out[2] = GEN_INT (shift);
5777 out[3] = GEN_INT (m2);
5780 /* Return TRUE if OP is an invalid SUBREG operation on the e500. */
5782 bool
5783 invalid_e500_subreg (rtx op, enum machine_mode mode)
5785 if (TARGET_E500_DOUBLE)
5787 /* Reject (subreg:SI (reg:DF)); likewise with subreg:DI or
5788 subreg:TI and reg:TF. Decimal float modes are like integer
5789 modes (only low part of each register used) for this
5790 purpose. */
5791 if (GET_CODE (op) == SUBREG
5792 && (mode == SImode || mode == DImode || mode == TImode
5793 || mode == DDmode || mode == TDmode || mode == PTImode)
5794 && REG_P (SUBREG_REG (op))
5795 && (GET_MODE (SUBREG_REG (op)) == DFmode
5796 || GET_MODE (SUBREG_REG (op)) == TFmode))
5797 return true;
5799 /* Reject (subreg:DF (reg:DI)); likewise with subreg:TF and
5800 reg:TI. */
5801 if (GET_CODE (op) == SUBREG
5802 && (mode == DFmode || mode == TFmode)
5803 && REG_P (SUBREG_REG (op))
5804 && (GET_MODE (SUBREG_REG (op)) == DImode
5805 || GET_MODE (SUBREG_REG (op)) == TImode
5806 || GET_MODE (SUBREG_REG (op)) == PTImode
5807 || GET_MODE (SUBREG_REG (op)) == DDmode
5808 || GET_MODE (SUBREG_REG (op)) == TDmode))
5809 return true;
5812 if (TARGET_SPE
5813 && GET_CODE (op) == SUBREG
5814 && mode == SImode
5815 && REG_P (SUBREG_REG (op))
5816 && SPE_VECTOR_MODE (GET_MODE (SUBREG_REG (op))))
5817 return true;
5819 return false;
5822 /* Return alignment of TYPE. Existing alignment is ALIGN. HOW
5823 selects whether the alignment is abi mandated, optional, or
5824 both abi and optional alignment. */
5826 unsigned int
5827 rs6000_data_alignment (tree type, unsigned int align, enum data_align how)
5829 if (how != align_opt)
5831 if (TREE_CODE (type) == VECTOR_TYPE)
5833 if ((TARGET_SPE && SPE_VECTOR_MODE (TYPE_MODE (type)))
5834 || (TARGET_PAIRED_FLOAT && PAIRED_VECTOR_MODE (TYPE_MODE (type))))
5836 if (align < 64)
5837 align = 64;
5839 else if (align < 128)
5840 align = 128;
5842 else if (TARGET_E500_DOUBLE
5843 && TREE_CODE (type) == REAL_TYPE
5844 && TYPE_MODE (type) == DFmode)
5846 if (align < 64)
5847 align = 64;
5851 if (how != align_abi)
5853 if (TREE_CODE (type) == ARRAY_TYPE
5854 && TYPE_MODE (TREE_TYPE (type)) == QImode)
5856 if (align < BITS_PER_WORD)
5857 align = BITS_PER_WORD;
5861 return align;
5864 /* AIX increases natural record alignment to doubleword if the first
5865 field is an FP double while the FP fields remain word aligned. */
5867 unsigned int
5868 rs6000_special_round_type_align (tree type, unsigned int computed,
5869 unsigned int specified)
5871 unsigned int align = MAX (computed, specified);
5872 tree field = TYPE_FIELDS (type);
5874 /* Skip all non field decls */
5875 while (field != NULL && TREE_CODE (field) != FIELD_DECL)
5876 field = DECL_CHAIN (field);
5878 if (field != NULL && field != type)
5880 type = TREE_TYPE (field);
5881 while (TREE_CODE (type) == ARRAY_TYPE)
5882 type = TREE_TYPE (type);
5884 if (type != error_mark_node && TYPE_MODE (type) == DFmode)
5885 align = MAX (align, 64);
5888 return align;
5891 /* Darwin increases record alignment to the natural alignment of
5892 the first field. */
5894 unsigned int
5895 darwin_rs6000_special_round_type_align (tree type, unsigned int computed,
5896 unsigned int specified)
5898 unsigned int align = MAX (computed, specified);
5900 if (TYPE_PACKED (type))
5901 return align;
5903 /* Find the first field, looking down into aggregates. */
5904 do {
5905 tree field = TYPE_FIELDS (type);
5906 /* Skip all non field decls */
5907 while (field != NULL && TREE_CODE (field) != FIELD_DECL)
5908 field = DECL_CHAIN (field);
5909 if (! field)
5910 break;
5911 /* A packed field does not contribute any extra alignment. */
5912 if (DECL_PACKED (field))
5913 return align;
5914 type = TREE_TYPE (field);
5915 while (TREE_CODE (type) == ARRAY_TYPE)
5916 type = TREE_TYPE (type);
5917 } while (AGGREGATE_TYPE_P (type));
5919 if (! AGGREGATE_TYPE_P (type) && type != error_mark_node)
5920 align = MAX (align, TYPE_ALIGN (type));
5922 return align;
5925 /* Return 1 for an operand in small memory on V.4/eabi. */
5928 small_data_operand (rtx op ATTRIBUTE_UNUSED,
5929 enum machine_mode mode ATTRIBUTE_UNUSED)
5931 #if TARGET_ELF
5932 rtx sym_ref;
5934 if (rs6000_sdata == SDATA_NONE || rs6000_sdata == SDATA_DATA)
5935 return 0;
5937 if (DEFAULT_ABI != ABI_V4)
5938 return 0;
5940 /* Vector and float memory instructions have a limited offset on the
5941 SPE, so using a vector or float variable directly as an operand is
5942 not useful. */
5943 if (TARGET_SPE
5944 && (SPE_VECTOR_MODE (mode) || FLOAT_MODE_P (mode)))
5945 return 0;
5947 if (GET_CODE (op) == SYMBOL_REF)
5948 sym_ref = op;
5950 else if (GET_CODE (op) != CONST
5951 || GET_CODE (XEXP (op, 0)) != PLUS
5952 || GET_CODE (XEXP (XEXP (op, 0), 0)) != SYMBOL_REF
5953 || GET_CODE (XEXP (XEXP (op, 0), 1)) != CONST_INT)
5954 return 0;
5956 else
5958 rtx sum = XEXP (op, 0);
5959 HOST_WIDE_INT summand;
5961 /* We have to be careful here, because it is the referenced address
5962 that must be 32k from _SDA_BASE_, not just the symbol. */
5963 summand = INTVAL (XEXP (sum, 1));
5964 if (summand < 0 || summand > g_switch_value)
5965 return 0;
5967 sym_ref = XEXP (sum, 0);
5970 return SYMBOL_REF_SMALL_P (sym_ref);
5971 #else
5972 return 0;
5973 #endif
5976 /* Return true if either operand is a general purpose register. */
5978 bool
5979 gpr_or_gpr_p (rtx op0, rtx op1)
5981 return ((REG_P (op0) && INT_REGNO_P (REGNO (op0)))
5982 || (REG_P (op1) && INT_REGNO_P (REGNO (op1))));
5985 /* Return true if this is a move direct operation between GPR registers and
5986 floating point/VSX registers. */
5988 bool
5989 direct_move_p (rtx op0, rtx op1)
5991 int regno0, regno1;
5993 if (!REG_P (op0) || !REG_P (op1))
5994 return false;
5996 if (!TARGET_DIRECT_MOVE && !TARGET_MFPGPR)
5997 return false;
5999 regno0 = REGNO (op0);
6000 regno1 = REGNO (op1);
6001 if (regno0 >= FIRST_PSEUDO_REGISTER || regno1 >= FIRST_PSEUDO_REGISTER)
6002 return false;
6004 if (INT_REGNO_P (regno0))
6005 return (TARGET_DIRECT_MOVE) ? VSX_REGNO_P (regno1) : FP_REGNO_P (regno1);
6007 else if (INT_REGNO_P (regno1))
6009 if (TARGET_MFPGPR && FP_REGNO_P (regno0))
6010 return true;
6012 else if (TARGET_DIRECT_MOVE && VSX_REGNO_P (regno0))
6013 return true;
6016 return false;
6019 /* Return true if this is a load or store quad operation. This function does
6020 not handle the atomic quad memory instructions. */
6022 bool
6023 quad_load_store_p (rtx op0, rtx op1)
6025 bool ret;
6027 if (!TARGET_QUAD_MEMORY)
6028 ret = false;
6030 else if (REG_P (op0) && MEM_P (op1))
6031 ret = (quad_int_reg_operand (op0, GET_MODE (op0))
6032 && quad_memory_operand (op1, GET_MODE (op1))
6033 && !reg_overlap_mentioned_p (op0, op1));
6035 else if (MEM_P (op0) && REG_P (op1))
6036 ret = (quad_memory_operand (op0, GET_MODE (op0))
6037 && quad_int_reg_operand (op1, GET_MODE (op1)));
6039 else
6040 ret = false;
6042 if (TARGET_DEBUG_ADDR)
6044 fprintf (stderr, "\n========== quad_load_store, return %s\n",
6045 ret ? "true" : "false");
6046 debug_rtx (gen_rtx_SET (VOIDmode, op0, op1));
6049 return ret;
6052 /* Given an address, return a constant offset term if one exists. */
6054 static rtx
6055 address_offset (rtx op)
6057 if (GET_CODE (op) == PRE_INC
6058 || GET_CODE (op) == PRE_DEC)
6059 op = XEXP (op, 0);
6060 else if (GET_CODE (op) == PRE_MODIFY
6061 || GET_CODE (op) == LO_SUM)
6062 op = XEXP (op, 1);
6064 if (GET_CODE (op) == CONST)
6065 op = XEXP (op, 0);
6067 if (GET_CODE (op) == PLUS)
6068 op = XEXP (op, 1);
6070 if (CONST_INT_P (op))
6071 return op;
6073 return NULL_RTX;
6076 /* Return true if the MEM operand is a memory operand suitable for use
6077 with a (full width, possibly multiple) gpr load/store. On
6078 powerpc64 this means the offset must be divisible by 4.
6079 Implements 'Y' constraint.
6081 Accept direct, indexed, offset, lo_sum and tocref. Since this is
6082 a constraint function we know the operand has satisfied a suitable
6083 memory predicate. Also accept some odd rtl generated by reload
6084 (see rs6000_legitimize_reload_address for various forms). It is
6085 important that reload rtl be accepted by appropriate constraints
6086 but not by the operand predicate.
6088 Offsetting a lo_sum should not be allowed, except where we know by
6089 alignment that a 32k boundary is not crossed, but see the ???
6090 comment in rs6000_legitimize_reload_address. Note that by
6091 "offsetting" here we mean a further offset to access parts of the
6092 MEM. It's fine to have a lo_sum where the inner address is offset
6093 from a sym, since the same sym+offset will appear in the high part
6094 of the address calculation. */
6096 bool
6097 mem_operand_gpr (rtx op, enum machine_mode mode)
6099 unsigned HOST_WIDE_INT offset;
6100 int extra;
6101 rtx addr = XEXP (op, 0);
6103 op = address_offset (addr);
6104 if (op == NULL_RTX)
6105 return true;
6107 offset = INTVAL (op);
6108 if (TARGET_POWERPC64 && (offset & 3) != 0)
6109 return false;
6111 extra = GET_MODE_SIZE (mode) - UNITS_PER_WORD;
6112 gcc_assert (extra >= 0);
6114 if (GET_CODE (addr) == LO_SUM)
6115 /* For lo_sum addresses, we must allow any offset except one that
6116 causes a wrap, so test only the low 16 bits. */
6117 offset = ((offset & 0xffff) ^ 0x8000) - 0x8000;
6119 return offset + 0x8000 < 0x10000u - extra;
6122 /* Subroutines of rs6000_legitimize_address and rs6000_legitimate_address_p. */
6124 static bool
6125 reg_offset_addressing_ok_p (enum machine_mode mode)
6127 switch (mode)
6129 case V16QImode:
6130 case V8HImode:
6131 case V4SFmode:
6132 case V4SImode:
6133 case V2DFmode:
6134 case V2DImode:
6135 case V1TImode:
6136 case TImode:
6137 /* AltiVec/VSX vector modes. Only reg+reg addressing is valid. While
6138 TImode is not a vector mode, if we want to use the VSX registers to
6139 move it around, we need to restrict ourselves to reg+reg
6140 addressing. */
6141 if (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode))
6142 return false;
6143 break;
6145 case V4HImode:
6146 case V2SImode:
6147 case V1DImode:
6148 case V2SFmode:
6149 /* Paired vector modes. Only reg+reg addressing is valid. */
6150 if (TARGET_PAIRED_FLOAT)
6151 return false;
6152 break;
6154 case SDmode:
6155 /* If we can do direct load/stores of SDmode, restrict it to reg+reg
6156 addressing for the LFIWZX and STFIWX instructions. */
6157 if (TARGET_NO_SDMODE_STACK)
6158 return false;
6159 break;
6161 default:
6162 break;
6165 return true;
6168 static bool
6169 virtual_stack_registers_memory_p (rtx op)
6171 int regnum;
6173 if (GET_CODE (op) == REG)
6174 regnum = REGNO (op);
6176 else if (GET_CODE (op) == PLUS
6177 && GET_CODE (XEXP (op, 0)) == REG
6178 && GET_CODE (XEXP (op, 1)) == CONST_INT)
6179 regnum = REGNO (XEXP (op, 0));
6181 else
6182 return false;
6184 return (regnum >= FIRST_VIRTUAL_REGISTER
6185 && regnum <= LAST_VIRTUAL_POINTER_REGISTER);
6188 /* Return true if a MODE sized memory accesses to OP plus OFFSET
6189 is known to not straddle a 32k boundary. */
6191 static bool
6192 offsettable_ok_by_alignment (rtx op, HOST_WIDE_INT offset,
6193 enum machine_mode mode)
6195 tree decl, type;
6196 unsigned HOST_WIDE_INT dsize, dalign, lsb, mask;
6198 if (GET_CODE (op) != SYMBOL_REF)
6199 return false;
6201 dsize = GET_MODE_SIZE (mode);
6202 decl = SYMBOL_REF_DECL (op);
6203 if (!decl)
6205 if (dsize == 0)
6206 return false;
6208 /* -fsection-anchors loses the original SYMBOL_REF_DECL when
6209 replacing memory addresses with an anchor plus offset. We
6210 could find the decl by rummaging around in the block->objects
6211 VEC for the given offset but that seems like too much work. */
6212 dalign = BITS_PER_UNIT;
6213 if (SYMBOL_REF_HAS_BLOCK_INFO_P (op)
6214 && SYMBOL_REF_ANCHOR_P (op)
6215 && SYMBOL_REF_BLOCK (op) != NULL)
6217 struct object_block *block = SYMBOL_REF_BLOCK (op);
6219 dalign = block->alignment;
6220 offset += SYMBOL_REF_BLOCK_OFFSET (op);
6222 else if (CONSTANT_POOL_ADDRESS_P (op))
6224 /* It would be nice to have get_pool_align().. */
6225 enum machine_mode cmode = get_pool_mode (op);
6227 dalign = GET_MODE_ALIGNMENT (cmode);
6230 else if (DECL_P (decl))
6232 dalign = DECL_ALIGN (decl);
6234 if (dsize == 0)
6236 /* Allow BLKmode when the entire object is known to not
6237 cross a 32k boundary. */
6238 if (!DECL_SIZE_UNIT (decl))
6239 return false;
6241 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (decl)))
6242 return false;
6244 dsize = tree_to_uhwi (DECL_SIZE_UNIT (decl));
6245 if (dsize > 32768)
6246 return false;
6248 return dalign / BITS_PER_UNIT >= dsize;
6251 else
6253 type = TREE_TYPE (decl);
6255 dalign = TYPE_ALIGN (type);
6256 if (CONSTANT_CLASS_P (decl))
6257 dalign = CONSTANT_ALIGNMENT (decl, dalign);
6258 else
6259 dalign = DATA_ALIGNMENT (decl, dalign);
6261 if (dsize == 0)
6263 /* BLKmode, check the entire object. */
6264 if (TREE_CODE (decl) == STRING_CST)
6265 dsize = TREE_STRING_LENGTH (decl);
6266 else if (TYPE_SIZE_UNIT (type)
6267 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
6268 dsize = tree_to_uhwi (TYPE_SIZE_UNIT (type));
6269 else
6270 return false;
6271 if (dsize > 32768)
6272 return false;
6274 return dalign / BITS_PER_UNIT >= dsize;
6278 /* Find how many bits of the alignment we know for this access. */
6279 mask = dalign / BITS_PER_UNIT - 1;
6280 lsb = offset & -offset;
6281 mask &= lsb - 1;
6282 dalign = mask + 1;
6284 return dalign >= dsize;
6287 static bool
6288 constant_pool_expr_p (rtx op)
6290 rtx base, offset;
6292 split_const (op, &base, &offset);
6293 return (GET_CODE (base) == SYMBOL_REF
6294 && CONSTANT_POOL_ADDRESS_P (base)
6295 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (base), Pmode));
6298 static const_rtx tocrel_base, tocrel_offset;
6300 /* Return true if OP is a toc pointer relative address (the output
6301 of create_TOC_reference). If STRICT, do not match high part or
6302 non-split -mcmodel=large/medium toc pointer relative addresses. */
6304 bool
6305 toc_relative_expr_p (const_rtx op, bool strict)
6307 if (!TARGET_TOC)
6308 return false;
6310 if (TARGET_CMODEL != CMODEL_SMALL)
6312 /* Only match the low part. */
6313 if (GET_CODE (op) == LO_SUM
6314 && REG_P (XEXP (op, 0))
6315 && INT_REG_OK_FOR_BASE_P (XEXP (op, 0), strict))
6316 op = XEXP (op, 1);
6317 else if (strict)
6318 return false;
6321 tocrel_base = op;
6322 tocrel_offset = const0_rtx;
6323 if (GET_CODE (op) == PLUS && add_cint_operand (XEXP (op, 1), GET_MODE (op)))
6325 tocrel_base = XEXP (op, 0);
6326 tocrel_offset = XEXP (op, 1);
6329 return (GET_CODE (tocrel_base) == UNSPEC
6330 && XINT (tocrel_base, 1) == UNSPEC_TOCREL);
6333 /* Return true if X is a constant pool address, and also for cmodel=medium
6334 if X is a toc-relative address known to be offsettable within MODE. */
6336 bool
6337 legitimate_constant_pool_address_p (const_rtx x, enum machine_mode mode,
6338 bool strict)
6340 return (toc_relative_expr_p (x, strict)
6341 && (TARGET_CMODEL != CMODEL_MEDIUM
6342 || constant_pool_expr_p (XVECEXP (tocrel_base, 0, 0))
6343 || mode == QImode
6344 || offsettable_ok_by_alignment (XVECEXP (tocrel_base, 0, 0),
6345 INTVAL (tocrel_offset), mode)));
6348 static bool
6349 legitimate_small_data_p (enum machine_mode mode, rtx x)
6351 return (DEFAULT_ABI == ABI_V4
6352 && !flag_pic && !TARGET_TOC
6353 && (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST)
6354 && small_data_operand (x, mode));
6357 /* SPE offset addressing is limited to 5-bits worth of double words. */
6358 #define SPE_CONST_OFFSET_OK(x) (((x) & ~0xf8) == 0)
6360 bool
6361 rs6000_legitimate_offset_address_p (enum machine_mode mode, rtx x,
6362 bool strict, bool worst_case)
6364 unsigned HOST_WIDE_INT offset;
6365 unsigned int extra;
6367 if (GET_CODE (x) != PLUS)
6368 return false;
6369 if (!REG_P (XEXP (x, 0)))
6370 return false;
6371 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
6372 return false;
6373 if (!reg_offset_addressing_ok_p (mode))
6374 return virtual_stack_registers_memory_p (x);
6375 if (legitimate_constant_pool_address_p (x, mode, strict || lra_in_progress))
6376 return true;
6377 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6378 return false;
6380 offset = INTVAL (XEXP (x, 1));
6381 extra = 0;
6382 switch (mode)
6384 case V4HImode:
6385 case V2SImode:
6386 case V1DImode:
6387 case V2SFmode:
6388 /* SPE vector modes. */
6389 return SPE_CONST_OFFSET_OK (offset);
6391 case DFmode:
6392 case DDmode:
6393 case DImode:
6394 /* On e500v2, we may have:
6396 (subreg:DF (mem:DI (plus (reg) (const_int))) 0).
6398 Which gets addressed with evldd instructions. */
6399 if (TARGET_E500_DOUBLE)
6400 return SPE_CONST_OFFSET_OK (offset);
6402 /* If we are using VSX scalar loads, restrict ourselves to reg+reg
6403 addressing. */
6404 if (VECTOR_MEM_VSX_P (mode))
6405 return false;
6407 if (!worst_case)
6408 break;
6409 if (!TARGET_POWERPC64)
6410 extra = 4;
6411 else if (offset & 3)
6412 return false;
6413 break;
6415 case TFmode:
6416 if (TARGET_E500_DOUBLE)
6417 return (SPE_CONST_OFFSET_OK (offset)
6418 && SPE_CONST_OFFSET_OK (offset + 8));
6419 /* fall through */
6421 case TDmode:
6422 case TImode:
6423 case PTImode:
6424 extra = 8;
6425 if (!worst_case)
6426 break;
6427 if (!TARGET_POWERPC64)
6428 extra = 12;
6429 else if (offset & 3)
6430 return false;
6431 break;
6433 default:
6434 break;
6437 offset += 0x8000;
6438 return offset < 0x10000 - extra;
6441 bool
6442 legitimate_indexed_address_p (rtx x, int strict)
6444 rtx op0, op1;
6446 if (GET_CODE (x) != PLUS)
6447 return false;
6449 op0 = XEXP (x, 0);
6450 op1 = XEXP (x, 1);
6452 /* Recognize the rtl generated by reload which we know will later be
6453 replaced with proper base and index regs. */
6454 if (!strict
6455 && reload_in_progress
6456 && (REG_P (op0) || GET_CODE (op0) == PLUS)
6457 && REG_P (op1))
6458 return true;
6460 return (REG_P (op0) && REG_P (op1)
6461 && ((INT_REG_OK_FOR_BASE_P (op0, strict)
6462 && INT_REG_OK_FOR_INDEX_P (op1, strict))
6463 || (INT_REG_OK_FOR_BASE_P (op1, strict)
6464 && INT_REG_OK_FOR_INDEX_P (op0, strict))));
6467 bool
6468 avoiding_indexed_address_p (enum machine_mode mode)
6470 /* Avoid indexed addressing for modes that have non-indexed
6471 load/store instruction forms. */
6472 return (TARGET_AVOID_XFORM && VECTOR_MEM_NONE_P (mode));
6475 bool
6476 legitimate_indirect_address_p (rtx x, int strict)
6478 return GET_CODE (x) == REG && INT_REG_OK_FOR_BASE_P (x, strict);
6481 bool
6482 macho_lo_sum_memory_operand (rtx x, enum machine_mode mode)
6484 if (!TARGET_MACHO || !flag_pic
6485 || mode != SImode || GET_CODE (x) != MEM)
6486 return false;
6487 x = XEXP (x, 0);
6489 if (GET_CODE (x) != LO_SUM)
6490 return false;
6491 if (GET_CODE (XEXP (x, 0)) != REG)
6492 return false;
6493 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), 0))
6494 return false;
6495 x = XEXP (x, 1);
6497 return CONSTANT_P (x);
6500 static bool
6501 legitimate_lo_sum_address_p (enum machine_mode mode, rtx x, int strict)
6503 if (GET_CODE (x) != LO_SUM)
6504 return false;
6505 if (GET_CODE (XEXP (x, 0)) != REG)
6506 return false;
6507 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
6508 return false;
6509 /* Restrict addressing for DI because of our SUBREG hackery. */
6510 if (TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
6511 return false;
6512 x = XEXP (x, 1);
6514 if (TARGET_ELF || TARGET_MACHO)
6516 bool large_toc_ok;
6518 if (DEFAULT_ABI == ABI_V4 && flag_pic)
6519 return false;
6520 /* LRA don't use LEGITIMIZE_RELOAD_ADDRESS as it usually calls
6521 push_reload from reload pass code. LEGITIMIZE_RELOAD_ADDRESS
6522 recognizes some LO_SUM addresses as valid although this
6523 function says opposite. In most cases, LRA through different
6524 transformations can generate correct code for address reloads.
6525 It can not manage only some LO_SUM cases. So we need to add
6526 code analogous to one in rs6000_legitimize_reload_address for
6527 LOW_SUM here saying that some addresses are still valid. */
6528 large_toc_ok = (lra_in_progress && TARGET_CMODEL != CMODEL_SMALL
6529 && small_toc_ref (x, VOIDmode));
6530 if (TARGET_TOC && ! large_toc_ok)
6531 return false;
6532 if (GET_MODE_NUNITS (mode) != 1)
6533 return false;
6534 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
6535 && !(/* ??? Assume floating point reg based on mode? */
6536 TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT
6537 && (mode == DFmode || mode == DDmode)))
6538 return false;
6540 return CONSTANT_P (x) || large_toc_ok;
6543 return false;
6547 /* Try machine-dependent ways of modifying an illegitimate address
6548 to be legitimate. If we find one, return the new, valid address.
6549 This is used from only one place: `memory_address' in explow.c.
6551 OLDX is the address as it was before break_out_memory_refs was
6552 called. In some cases it is useful to look at this to decide what
6553 needs to be done.
6555 It is always safe for this function to do nothing. It exists to
6556 recognize opportunities to optimize the output.
6558 On RS/6000, first check for the sum of a register with a constant
6559 integer that is out of range. If so, generate code to add the
6560 constant with the low-order 16 bits masked to the register and force
6561 this result into another register (this can be done with `cau').
6562 Then generate an address of REG+(CONST&0xffff), allowing for the
6563 possibility of bit 16 being a one.
6565 Then check for the sum of a register and something not constant, try to
6566 load the other things into a register and return the sum. */
6568 static rtx
6569 rs6000_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
6570 enum machine_mode mode)
6572 unsigned int extra;
6574 if (!reg_offset_addressing_ok_p (mode))
6576 if (virtual_stack_registers_memory_p (x))
6577 return x;
6579 /* In theory we should not be seeing addresses of the form reg+0,
6580 but just in case it is generated, optimize it away. */
6581 if (GET_CODE (x) == PLUS && XEXP (x, 1) == const0_rtx)
6582 return force_reg (Pmode, XEXP (x, 0));
6584 /* For TImode with load/store quad, restrict addresses to just a single
6585 pointer, so it works with both GPRs and VSX registers. */
6586 /* Make sure both operands are registers. */
6587 else if (GET_CODE (x) == PLUS
6588 && (mode != TImode || !TARGET_QUAD_MEMORY))
6589 return gen_rtx_PLUS (Pmode,
6590 force_reg (Pmode, XEXP (x, 0)),
6591 force_reg (Pmode, XEXP (x, 1)));
6592 else
6593 return force_reg (Pmode, x);
6595 if (GET_CODE (x) == SYMBOL_REF)
6597 enum tls_model model = SYMBOL_REF_TLS_MODEL (x);
6598 if (model != 0)
6599 return rs6000_legitimize_tls_address (x, model);
6602 extra = 0;
6603 switch (mode)
6605 case TFmode:
6606 case TDmode:
6607 case TImode:
6608 case PTImode:
6609 /* As in legitimate_offset_address_p we do not assume
6610 worst-case. The mode here is just a hint as to the registers
6611 used. A TImode is usually in gprs, but may actually be in
6612 fprs. Leave worst-case scenario for reload to handle via
6613 insn constraints. PTImode is only GPRs. */
6614 extra = 8;
6615 break;
6616 default:
6617 break;
6620 if (GET_CODE (x) == PLUS
6621 && GET_CODE (XEXP (x, 0)) == REG
6622 && GET_CODE (XEXP (x, 1)) == CONST_INT
6623 && ((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 1)) + 0x8000)
6624 >= 0x10000 - extra)
6625 && !(SPE_VECTOR_MODE (mode)
6626 || (TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD)))
6628 HOST_WIDE_INT high_int, low_int;
6629 rtx sum;
6630 low_int = ((INTVAL (XEXP (x, 1)) & 0xffff) ^ 0x8000) - 0x8000;
6631 if (low_int >= 0x8000 - extra)
6632 low_int = 0;
6633 high_int = INTVAL (XEXP (x, 1)) - low_int;
6634 sum = force_operand (gen_rtx_PLUS (Pmode, XEXP (x, 0),
6635 GEN_INT (high_int)), 0);
6636 return plus_constant (Pmode, sum, low_int);
6638 else if (GET_CODE (x) == PLUS
6639 && GET_CODE (XEXP (x, 0)) == REG
6640 && GET_CODE (XEXP (x, 1)) != CONST_INT
6641 && GET_MODE_NUNITS (mode) == 1
6642 && (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
6643 || (/* ??? Assume floating point reg based on mode? */
6644 (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
6645 && (mode == DFmode || mode == DDmode)))
6646 && !avoiding_indexed_address_p (mode))
6648 return gen_rtx_PLUS (Pmode, XEXP (x, 0),
6649 force_reg (Pmode, force_operand (XEXP (x, 1), 0)));
6651 else if (SPE_VECTOR_MODE (mode)
6652 || (TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD))
6654 if (mode == DImode)
6655 return x;
6656 /* We accept [reg + reg] and [reg + OFFSET]. */
6658 if (GET_CODE (x) == PLUS)
6660 rtx op1 = XEXP (x, 0);
6661 rtx op2 = XEXP (x, 1);
6662 rtx y;
6664 op1 = force_reg (Pmode, op1);
6666 if (GET_CODE (op2) != REG
6667 && (GET_CODE (op2) != CONST_INT
6668 || !SPE_CONST_OFFSET_OK (INTVAL (op2))
6669 || (GET_MODE_SIZE (mode) > 8
6670 && !SPE_CONST_OFFSET_OK (INTVAL (op2) + 8))))
6671 op2 = force_reg (Pmode, op2);
6673 /* We can't always do [reg + reg] for these, because [reg +
6674 reg + offset] is not a legitimate addressing mode. */
6675 y = gen_rtx_PLUS (Pmode, op1, op2);
6677 if ((GET_MODE_SIZE (mode) > 8 || mode == DDmode) && REG_P (op2))
6678 return force_reg (Pmode, y);
6679 else
6680 return y;
6683 return force_reg (Pmode, x);
6685 else if ((TARGET_ELF
6686 #if TARGET_MACHO
6687 || !MACHO_DYNAMIC_NO_PIC_P
6688 #endif
6690 && TARGET_32BIT
6691 && TARGET_NO_TOC
6692 && ! flag_pic
6693 && GET_CODE (x) != CONST_INT
6694 && GET_CODE (x) != CONST_DOUBLE
6695 && CONSTANT_P (x)
6696 && GET_MODE_NUNITS (mode) == 1
6697 && (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
6698 || (/* ??? Assume floating point reg based on mode? */
6699 (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
6700 && (mode == DFmode || mode == DDmode))))
6702 rtx reg = gen_reg_rtx (Pmode);
6703 if (TARGET_ELF)
6704 emit_insn (gen_elf_high (reg, x));
6705 else
6706 emit_insn (gen_macho_high (reg, x));
6707 return gen_rtx_LO_SUM (Pmode, reg, x);
6709 else if (TARGET_TOC
6710 && GET_CODE (x) == SYMBOL_REF
6711 && constant_pool_expr_p (x)
6712 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (x), Pmode))
6713 return create_TOC_reference (x, NULL_RTX);
6714 else
6715 return x;
6718 /* Debug version of rs6000_legitimize_address. */
6719 static rtx
6720 rs6000_debug_legitimize_address (rtx x, rtx oldx, enum machine_mode mode)
6722 rtx ret;
6723 rtx insns;
6725 start_sequence ();
6726 ret = rs6000_legitimize_address (x, oldx, mode);
6727 insns = get_insns ();
6728 end_sequence ();
6730 if (ret != x)
6732 fprintf (stderr,
6733 "\nrs6000_legitimize_address: mode %s, old code %s, "
6734 "new code %s, modified\n",
6735 GET_MODE_NAME (mode), GET_RTX_NAME (GET_CODE (x)),
6736 GET_RTX_NAME (GET_CODE (ret)));
6738 fprintf (stderr, "Original address:\n");
6739 debug_rtx (x);
6741 fprintf (stderr, "oldx:\n");
6742 debug_rtx (oldx);
6744 fprintf (stderr, "New address:\n");
6745 debug_rtx (ret);
6747 if (insns)
6749 fprintf (stderr, "Insns added:\n");
6750 debug_rtx_list (insns, 20);
6753 else
6755 fprintf (stderr,
6756 "\nrs6000_legitimize_address: mode %s, code %s, no change:\n",
6757 GET_MODE_NAME (mode), GET_RTX_NAME (GET_CODE (x)));
6759 debug_rtx (x);
6762 if (insns)
6763 emit_insn (insns);
6765 return ret;
6768 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
6769 We need to emit DTP-relative relocations. */
6771 static void rs6000_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
6772 static void
6773 rs6000_output_dwarf_dtprel (FILE *file, int size, rtx x)
6775 switch (size)
6777 case 4:
6778 fputs ("\t.long\t", file);
6779 break;
6780 case 8:
6781 fputs (DOUBLE_INT_ASM_OP, file);
6782 break;
6783 default:
6784 gcc_unreachable ();
6786 output_addr_const (file, x);
6787 fputs ("@dtprel+0x8000", file);
6790 /* In the name of slightly smaller debug output, and to cater to
6791 general assembler lossage, recognize various UNSPEC sequences
6792 and turn them back into a direct symbol reference. */
6794 static rtx
6795 rs6000_delegitimize_address (rtx orig_x)
6797 rtx x, y, offset;
6799 orig_x = delegitimize_mem_from_attrs (orig_x);
6800 x = orig_x;
6801 if (MEM_P (x))
6802 x = XEXP (x, 0);
6804 y = x;
6805 if (TARGET_CMODEL != CMODEL_SMALL
6806 && GET_CODE (y) == LO_SUM)
6807 y = XEXP (y, 1);
6809 offset = NULL_RTX;
6810 if (GET_CODE (y) == PLUS
6811 && GET_MODE (y) == Pmode
6812 && CONST_INT_P (XEXP (y, 1)))
6814 offset = XEXP (y, 1);
6815 y = XEXP (y, 0);
6818 if (GET_CODE (y) == UNSPEC
6819 && XINT (y, 1) == UNSPEC_TOCREL)
6821 #ifdef ENABLE_CHECKING
6822 if (REG_P (XVECEXP (y, 0, 1))
6823 && REGNO (XVECEXP (y, 0, 1)) == TOC_REGISTER)
6825 /* All good. */
6827 else if (GET_CODE (XVECEXP (y, 0, 1)) == DEBUG_EXPR)
6829 /* Weirdness alert. df_note_compute can replace r2 with a
6830 debug_expr when this unspec is in a debug_insn.
6831 Seen in gcc.dg/pr51957-1.c */
6833 else
6835 debug_rtx (orig_x);
6836 abort ();
6838 #endif
6839 y = XVECEXP (y, 0, 0);
6841 #ifdef HAVE_AS_TLS
6842 /* Do not associate thread-local symbols with the original
6843 constant pool symbol. */
6844 if (TARGET_XCOFF
6845 && GET_CODE (y) == SYMBOL_REF
6846 && CONSTANT_POOL_ADDRESS_P (y)
6847 && SYMBOL_REF_TLS_MODEL (get_pool_constant (y)) >= TLS_MODEL_REAL)
6848 return orig_x;
6849 #endif
6851 if (offset != NULL_RTX)
6852 y = gen_rtx_PLUS (Pmode, y, offset);
6853 if (!MEM_P (orig_x))
6854 return y;
6855 else
6856 return replace_equiv_address_nv (orig_x, y);
6859 if (TARGET_MACHO
6860 && GET_CODE (orig_x) == LO_SUM
6861 && GET_CODE (XEXP (orig_x, 1)) == CONST)
6863 y = XEXP (XEXP (orig_x, 1), 0);
6864 if (GET_CODE (y) == UNSPEC
6865 && XINT (y, 1) == UNSPEC_MACHOPIC_OFFSET)
6866 return XVECEXP (y, 0, 0);
6869 return orig_x;
6872 /* Return true if X shouldn't be emitted into the debug info.
6873 The linker doesn't like .toc section references from
6874 .debug_* sections, so reject .toc section symbols. */
6876 static bool
6877 rs6000_const_not_ok_for_debug_p (rtx x)
6879 if (GET_CODE (x) == SYMBOL_REF
6880 && CONSTANT_POOL_ADDRESS_P (x))
6882 rtx c = get_pool_constant (x);
6883 enum machine_mode cmode = get_pool_mode (x);
6884 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (c, cmode))
6885 return true;
6888 return false;
6891 /* Construct the SYMBOL_REF for the tls_get_addr function. */
6893 static GTY(()) rtx rs6000_tls_symbol;
6894 static rtx
6895 rs6000_tls_get_addr (void)
6897 if (!rs6000_tls_symbol)
6898 rs6000_tls_symbol = init_one_libfunc ("__tls_get_addr");
6900 return rs6000_tls_symbol;
6903 /* Construct the SYMBOL_REF for TLS GOT references. */
6905 static GTY(()) rtx rs6000_got_symbol;
6906 static rtx
6907 rs6000_got_sym (void)
6909 if (!rs6000_got_symbol)
6911 rs6000_got_symbol = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
6912 SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_LOCAL;
6913 SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_EXTERNAL;
6916 return rs6000_got_symbol;
6919 /* AIX Thread-Local Address support. */
6921 static rtx
6922 rs6000_legitimize_tls_address_aix (rtx addr, enum tls_model model)
6924 rtx sym, mem, tocref, tlsreg, tmpreg, dest, tlsaddr;
6925 const char *name;
6926 char *tlsname;
6928 name = XSTR (addr, 0);
6929 /* Append TLS CSECT qualifier, unless the symbol already is qualified
6930 or the symbol will be in TLS private data section. */
6931 if (name[strlen (name) - 1] != ']'
6932 && (TREE_PUBLIC (SYMBOL_REF_DECL (addr))
6933 || bss_initializer_p (SYMBOL_REF_DECL (addr))))
6935 tlsname = XALLOCAVEC (char, strlen (name) + 4);
6936 strcpy (tlsname, name);
6937 strcat (tlsname,
6938 bss_initializer_p (SYMBOL_REF_DECL (addr)) ? "[UL]" : "[TL]");
6939 tlsaddr = copy_rtx (addr);
6940 XSTR (tlsaddr, 0) = ggc_strdup (tlsname);
6942 else
6943 tlsaddr = addr;
6945 /* Place addr into TOC constant pool. */
6946 sym = force_const_mem (GET_MODE (tlsaddr), tlsaddr);
6948 /* Output the TOC entry and create the MEM referencing the value. */
6949 if (constant_pool_expr_p (XEXP (sym, 0))
6950 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (XEXP (sym, 0)), Pmode))
6952 tocref = create_TOC_reference (XEXP (sym, 0), NULL_RTX);
6953 mem = gen_const_mem (Pmode, tocref);
6954 set_mem_alias_set (mem, get_TOC_alias_set ());
6956 else
6957 return sym;
6959 /* Use global-dynamic for local-dynamic. */
6960 if (model == TLS_MODEL_GLOBAL_DYNAMIC
6961 || model == TLS_MODEL_LOCAL_DYNAMIC)
6963 /* Create new TOC reference for @m symbol. */
6964 name = XSTR (XVECEXP (XEXP (mem, 0), 0, 0), 0);
6965 tlsname = XALLOCAVEC (char, strlen (name) + 1);
6966 strcpy (tlsname, "*LCM");
6967 strcat (tlsname, name + 3);
6968 rtx modaddr = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (tlsname));
6969 SYMBOL_REF_FLAGS (modaddr) |= SYMBOL_FLAG_LOCAL;
6970 tocref = create_TOC_reference (modaddr, NULL_RTX);
6971 rtx modmem = gen_const_mem (Pmode, tocref);
6972 set_mem_alias_set (modmem, get_TOC_alias_set ());
6974 rtx modreg = gen_reg_rtx (Pmode);
6975 emit_insn (gen_rtx_SET (VOIDmode, modreg, modmem));
6977 tmpreg = gen_reg_rtx (Pmode);
6978 emit_insn (gen_rtx_SET (VOIDmode, tmpreg, mem));
6980 dest = gen_reg_rtx (Pmode);
6981 if (TARGET_32BIT)
6982 emit_insn (gen_tls_get_addrsi (dest, modreg, tmpreg));
6983 else
6984 emit_insn (gen_tls_get_addrdi (dest, modreg, tmpreg));
6985 return dest;
6987 /* Obtain TLS pointer: 32 bit call or 64 bit GPR 13. */
6988 else if (TARGET_32BIT)
6990 tlsreg = gen_reg_rtx (SImode);
6991 emit_insn (gen_tls_get_tpointer (tlsreg));
6993 else
6994 tlsreg = gen_rtx_REG (DImode, 13);
6996 /* Load the TOC value into temporary register. */
6997 tmpreg = gen_reg_rtx (Pmode);
6998 emit_insn (gen_rtx_SET (VOIDmode, tmpreg, mem));
6999 set_unique_reg_note (get_last_insn (), REG_EQUAL,
7000 gen_rtx_MINUS (Pmode, addr, tlsreg));
7002 /* Add TOC symbol value to TLS pointer. */
7003 dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, tmpreg, tlsreg));
7005 return dest;
7008 /* ADDR contains a thread-local SYMBOL_REF. Generate code to compute
7009 this (thread-local) address. */
7011 static rtx
7012 rs6000_legitimize_tls_address (rtx addr, enum tls_model model)
7014 rtx dest, insn;
7016 if (TARGET_XCOFF)
7017 return rs6000_legitimize_tls_address_aix (addr, model);
7019 dest = gen_reg_rtx (Pmode);
7020 if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 16)
7022 rtx tlsreg;
7024 if (TARGET_64BIT)
7026 tlsreg = gen_rtx_REG (Pmode, 13);
7027 insn = gen_tls_tprel_64 (dest, tlsreg, addr);
7029 else
7031 tlsreg = gen_rtx_REG (Pmode, 2);
7032 insn = gen_tls_tprel_32 (dest, tlsreg, addr);
7034 emit_insn (insn);
7036 else if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 32)
7038 rtx tlsreg, tmp;
7040 tmp = gen_reg_rtx (Pmode);
7041 if (TARGET_64BIT)
7043 tlsreg = gen_rtx_REG (Pmode, 13);
7044 insn = gen_tls_tprel_ha_64 (tmp, tlsreg, addr);
7046 else
7048 tlsreg = gen_rtx_REG (Pmode, 2);
7049 insn = gen_tls_tprel_ha_32 (tmp, tlsreg, addr);
7051 emit_insn (insn);
7052 if (TARGET_64BIT)
7053 insn = gen_tls_tprel_lo_64 (dest, tmp, addr);
7054 else
7055 insn = gen_tls_tprel_lo_32 (dest, tmp, addr);
7056 emit_insn (insn);
7058 else
7060 rtx r3, got, tga, tmp1, tmp2, call_insn;
7062 /* We currently use relocations like @got@tlsgd for tls, which
7063 means the linker will handle allocation of tls entries, placing
7064 them in the .got section. So use a pointer to the .got section,
7065 not one to secondary TOC sections used by 64-bit -mminimal-toc,
7066 or to secondary GOT sections used by 32-bit -fPIC. */
7067 if (TARGET_64BIT)
7068 got = gen_rtx_REG (Pmode, 2);
7069 else
7071 if (flag_pic == 1)
7072 got = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
7073 else
7075 rtx gsym = rs6000_got_sym ();
7076 got = gen_reg_rtx (Pmode);
7077 if (flag_pic == 0)
7078 rs6000_emit_move (got, gsym, Pmode);
7079 else
7081 rtx mem, lab, last;
7083 tmp1 = gen_reg_rtx (Pmode);
7084 tmp2 = gen_reg_rtx (Pmode);
7085 mem = gen_const_mem (Pmode, tmp1);
7086 lab = gen_label_rtx ();
7087 emit_insn (gen_load_toc_v4_PIC_1b (gsym, lab));
7088 emit_move_insn (tmp1, gen_rtx_REG (Pmode, LR_REGNO));
7089 if (TARGET_LINK_STACK)
7090 emit_insn (gen_addsi3 (tmp1, tmp1, GEN_INT (4)));
7091 emit_move_insn (tmp2, mem);
7092 last = emit_insn (gen_addsi3 (got, tmp1, tmp2));
7093 set_unique_reg_note (last, REG_EQUAL, gsym);
7098 if (model == TLS_MODEL_GLOBAL_DYNAMIC)
7100 tga = rs6000_tls_get_addr ();
7101 emit_library_call_value (tga, dest, LCT_CONST, Pmode,
7102 1, const0_rtx, Pmode);
7104 r3 = gen_rtx_REG (Pmode, 3);
7105 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
7107 if (TARGET_64BIT)
7108 insn = gen_tls_gd_aix64 (r3, got, addr, tga, const0_rtx);
7109 else
7110 insn = gen_tls_gd_aix32 (r3, got, addr, tga, const0_rtx);
7112 else if (DEFAULT_ABI == ABI_V4)
7113 insn = gen_tls_gd_sysvsi (r3, got, addr, tga, const0_rtx);
7114 else
7115 gcc_unreachable ();
7116 call_insn = last_call_insn ();
7117 PATTERN (call_insn) = insn;
7118 if (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
7119 use_reg (&CALL_INSN_FUNCTION_USAGE (call_insn),
7120 pic_offset_table_rtx);
7122 else if (model == TLS_MODEL_LOCAL_DYNAMIC)
7124 tga = rs6000_tls_get_addr ();
7125 tmp1 = gen_reg_rtx (Pmode);
7126 emit_library_call_value (tga, tmp1, LCT_CONST, Pmode,
7127 1, const0_rtx, Pmode);
7129 r3 = gen_rtx_REG (Pmode, 3);
7130 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
7132 if (TARGET_64BIT)
7133 insn = gen_tls_ld_aix64 (r3, got, tga, const0_rtx);
7134 else
7135 insn = gen_tls_ld_aix32 (r3, got, tga, const0_rtx);
7137 else if (DEFAULT_ABI == ABI_V4)
7138 insn = gen_tls_ld_sysvsi (r3, got, tga, const0_rtx);
7139 else
7140 gcc_unreachable ();
7141 call_insn = last_call_insn ();
7142 PATTERN (call_insn) = insn;
7143 if (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
7144 use_reg (&CALL_INSN_FUNCTION_USAGE (call_insn),
7145 pic_offset_table_rtx);
7147 if (rs6000_tls_size == 16)
7149 if (TARGET_64BIT)
7150 insn = gen_tls_dtprel_64 (dest, tmp1, addr);
7151 else
7152 insn = gen_tls_dtprel_32 (dest, tmp1, addr);
7154 else if (rs6000_tls_size == 32)
7156 tmp2 = gen_reg_rtx (Pmode);
7157 if (TARGET_64BIT)
7158 insn = gen_tls_dtprel_ha_64 (tmp2, tmp1, addr);
7159 else
7160 insn = gen_tls_dtprel_ha_32 (tmp2, tmp1, addr);
7161 emit_insn (insn);
7162 if (TARGET_64BIT)
7163 insn = gen_tls_dtprel_lo_64 (dest, tmp2, addr);
7164 else
7165 insn = gen_tls_dtprel_lo_32 (dest, tmp2, addr);
7167 else
7169 tmp2 = gen_reg_rtx (Pmode);
7170 if (TARGET_64BIT)
7171 insn = gen_tls_got_dtprel_64 (tmp2, got, addr);
7172 else
7173 insn = gen_tls_got_dtprel_32 (tmp2, got, addr);
7174 emit_insn (insn);
7175 insn = gen_rtx_SET (Pmode, dest,
7176 gen_rtx_PLUS (Pmode, tmp2, tmp1));
7178 emit_insn (insn);
7180 else
7182 /* IE, or 64-bit offset LE. */
7183 tmp2 = gen_reg_rtx (Pmode);
7184 if (TARGET_64BIT)
7185 insn = gen_tls_got_tprel_64 (tmp2, got, addr);
7186 else
7187 insn = gen_tls_got_tprel_32 (tmp2, got, addr);
7188 emit_insn (insn);
7189 if (TARGET_64BIT)
7190 insn = gen_tls_tls_64 (dest, tmp2, addr);
7191 else
7192 insn = gen_tls_tls_32 (dest, tmp2, addr);
7193 emit_insn (insn);
7197 return dest;
7200 /* Return 1 if X contains a thread-local symbol. */
7202 static bool
7203 rs6000_tls_referenced_p (rtx x)
7205 if (! TARGET_HAVE_TLS)
7206 return false;
7208 return for_each_rtx (&x, &rs6000_tls_symbol_ref_1, 0);
7211 /* Implement TARGET_CANNOT_FORCE_CONST_MEM. */
7213 static bool
7214 rs6000_cannot_force_const_mem (enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
7216 if (GET_CODE (x) == HIGH
7217 && GET_CODE (XEXP (x, 0)) == UNSPEC)
7218 return true;
7220 /* A TLS symbol in the TOC cannot contain a sum. */
7221 if (GET_CODE (x) == CONST
7222 && GET_CODE (XEXP (x, 0)) == PLUS
7223 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
7224 && SYMBOL_REF_TLS_MODEL (XEXP (XEXP (x, 0), 0)) != 0)
7225 return true;
7227 /* Do not place an ELF TLS symbol in the constant pool. */
7228 return TARGET_ELF && rs6000_tls_referenced_p (x);
7231 /* Return 1 if *X is a thread-local symbol. This is the same as
7232 rs6000_tls_symbol_ref except for the type of the unused argument. */
7234 static int
7235 rs6000_tls_symbol_ref_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
7237 return RS6000_SYMBOL_REF_TLS_P (*x);
7240 /* Return true iff the given SYMBOL_REF refers to a constant pool entry
7241 that we have put in the TOC, or for cmodel=medium, if the SYMBOL_REF
7242 can be addressed relative to the toc pointer. */
7244 static bool
7245 use_toc_relative_ref (rtx sym)
7247 return ((constant_pool_expr_p (sym)
7248 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (sym),
7249 get_pool_mode (sym)))
7250 || (TARGET_CMODEL == CMODEL_MEDIUM
7251 && SYMBOL_REF_LOCAL_P (sym)));
7254 /* Our implementation of LEGITIMIZE_RELOAD_ADDRESS. Returns a value to
7255 replace the input X, or the original X if no replacement is called for.
7256 The output parameter *WIN is 1 if the calling macro should goto WIN,
7257 0 if it should not.
7259 For RS/6000, we wish to handle large displacements off a base
7260 register by splitting the addend across an addiu/addis and the mem insn.
7261 This cuts number of extra insns needed from 3 to 1.
7263 On Darwin, we use this to generate code for floating point constants.
7264 A movsf_low is generated so we wind up with 2 instructions rather than 3.
7265 The Darwin code is inside #if TARGET_MACHO because only then are the
7266 machopic_* functions defined. */
7267 static rtx
7268 rs6000_legitimize_reload_address (rtx x, enum machine_mode mode,
7269 int opnum, int type,
7270 int ind_levels ATTRIBUTE_UNUSED, int *win)
7272 bool reg_offset_p = reg_offset_addressing_ok_p (mode);
7274 /* Nasty hack for vsx_splat_V2DF/V2DI load from mem, which takes a
7275 DFmode/DImode MEM. */
7276 if (reg_offset_p
7277 && opnum == 1
7278 && ((mode == DFmode && recog_data.operand_mode[0] == V2DFmode)
7279 || (mode == DImode && recog_data.operand_mode[0] == V2DImode)))
7280 reg_offset_p = false;
7282 /* We must recognize output that we have already generated ourselves. */
7283 if (GET_CODE (x) == PLUS
7284 && GET_CODE (XEXP (x, 0)) == PLUS
7285 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
7286 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7287 && GET_CODE (XEXP (x, 1)) == CONST_INT)
7289 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7290 BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
7291 opnum, (enum reload_type) type);
7292 *win = 1;
7293 return x;
7296 /* Likewise for (lo_sum (high ...) ...) output we have generated. */
7297 if (GET_CODE (x) == LO_SUM
7298 && GET_CODE (XEXP (x, 0)) == HIGH)
7300 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7301 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
7302 opnum, (enum reload_type) type);
7303 *win = 1;
7304 return x;
7307 #if TARGET_MACHO
7308 if (DEFAULT_ABI == ABI_DARWIN && flag_pic
7309 && GET_CODE (x) == LO_SUM
7310 && GET_CODE (XEXP (x, 0)) == PLUS
7311 && XEXP (XEXP (x, 0), 0) == pic_offset_table_rtx
7312 && GET_CODE (XEXP (XEXP (x, 0), 1)) == HIGH
7313 && XEXP (XEXP (XEXP (x, 0), 1), 0) == XEXP (x, 1)
7314 && machopic_operand_p (XEXP (x, 1)))
7316 /* Result of previous invocation of this function on Darwin
7317 floating point constant. */
7318 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7319 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
7320 opnum, (enum reload_type) type);
7321 *win = 1;
7322 return x;
7324 #endif
7326 if (TARGET_CMODEL != CMODEL_SMALL
7327 && reg_offset_p
7328 && small_toc_ref (x, VOIDmode))
7330 rtx hi = gen_rtx_HIGH (Pmode, copy_rtx (x));
7331 x = gen_rtx_LO_SUM (Pmode, hi, x);
7332 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7333 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
7334 opnum, (enum reload_type) type);
7335 *win = 1;
7336 return x;
7339 if (GET_CODE (x) == PLUS
7340 && GET_CODE (XEXP (x, 0)) == REG
7341 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
7342 && INT_REG_OK_FOR_BASE_P (XEXP (x, 0), 1)
7343 && GET_CODE (XEXP (x, 1)) == CONST_INT
7344 && reg_offset_p
7345 && !SPE_VECTOR_MODE (mode)
7346 && !(TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
7347 && (!VECTOR_MODE_P (mode) || VECTOR_MEM_NONE_P (mode)))
7349 HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
7350 HOST_WIDE_INT low = ((val & 0xffff) ^ 0x8000) - 0x8000;
7351 HOST_WIDE_INT high
7352 = (((val - low) & 0xffffffff) ^ 0x80000000) - 0x80000000;
7354 /* Check for 32-bit overflow. */
7355 if (high + low != val)
7357 *win = 0;
7358 return x;
7361 /* Reload the high part into a base reg; leave the low part
7362 in the mem directly. */
7364 x = gen_rtx_PLUS (GET_MODE (x),
7365 gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0),
7366 GEN_INT (high)),
7367 GEN_INT (low));
7369 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7370 BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
7371 opnum, (enum reload_type) type);
7372 *win = 1;
7373 return x;
7376 if (GET_CODE (x) == SYMBOL_REF
7377 && reg_offset_p
7378 && (!VECTOR_MODE_P (mode) || VECTOR_MEM_NONE_P (mode))
7379 && !SPE_VECTOR_MODE (mode)
7380 #if TARGET_MACHO
7381 && DEFAULT_ABI == ABI_DARWIN
7382 && (flag_pic || MACHO_DYNAMIC_NO_PIC_P)
7383 && machopic_symbol_defined_p (x)
7384 #else
7385 && DEFAULT_ABI == ABI_V4
7386 && !flag_pic
7387 #endif
7388 /* Don't do this for TFmode or TDmode, since the result isn't offsettable.
7389 The same goes for DImode without 64-bit gprs and DFmode and DDmode
7390 without fprs.
7391 ??? Assume floating point reg based on mode? This assumption is
7392 violated by eg. powerpc-linux -m32 compile of gcc.dg/pr28796-2.c
7393 where reload ends up doing a DFmode load of a constant from
7394 mem using two gprs. Unfortunately, at this point reload
7395 hasn't yet selected regs so poking around in reload data
7396 won't help and even if we could figure out the regs reliably,
7397 we'd still want to allow this transformation when the mem is
7398 naturally aligned. Since we say the address is good here, we
7399 can't disable offsets from LO_SUMs in mem_operand_gpr.
7400 FIXME: Allow offset from lo_sum for other modes too, when
7401 mem is sufficiently aligned. */
7402 && mode != TFmode
7403 && mode != TDmode
7404 && (mode != TImode || !TARGET_VSX_TIMODE)
7405 && mode != PTImode
7406 && (mode != DImode || TARGET_POWERPC64)
7407 && ((mode != DFmode && mode != DDmode) || TARGET_POWERPC64
7408 || (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)))
7410 #if TARGET_MACHO
7411 if (flag_pic)
7413 rtx offset = machopic_gen_offset (x);
7414 x = gen_rtx_LO_SUM (GET_MODE (x),
7415 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
7416 gen_rtx_HIGH (Pmode, offset)), offset);
7418 else
7419 #endif
7420 x = gen_rtx_LO_SUM (GET_MODE (x),
7421 gen_rtx_HIGH (Pmode, x), x);
7423 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7424 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
7425 opnum, (enum reload_type) type);
7426 *win = 1;
7427 return x;
7430 /* Reload an offset address wrapped by an AND that represents the
7431 masking of the lower bits. Strip the outer AND and let reload
7432 convert the offset address into an indirect address. For VSX,
7433 force reload to create the address with an AND in a separate
7434 register, because we can't guarantee an altivec register will
7435 be used. */
7436 if (VECTOR_MEM_ALTIVEC_P (mode)
7437 && GET_CODE (x) == AND
7438 && GET_CODE (XEXP (x, 0)) == PLUS
7439 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
7440 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7441 && GET_CODE (XEXP (x, 1)) == CONST_INT
7442 && INTVAL (XEXP (x, 1)) == -16)
7444 x = XEXP (x, 0);
7445 *win = 1;
7446 return x;
7449 if (TARGET_TOC
7450 && reg_offset_p
7451 && GET_CODE (x) == SYMBOL_REF
7452 && use_toc_relative_ref (x))
7454 x = create_TOC_reference (x, NULL_RTX);
7455 if (TARGET_CMODEL != CMODEL_SMALL)
7456 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7457 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
7458 opnum, (enum reload_type) type);
7459 *win = 1;
7460 return x;
7462 *win = 0;
7463 return x;
7466 /* Debug version of rs6000_legitimize_reload_address. */
7467 static rtx
7468 rs6000_debug_legitimize_reload_address (rtx x, enum machine_mode mode,
7469 int opnum, int type,
7470 int ind_levels, int *win)
7472 rtx ret = rs6000_legitimize_reload_address (x, mode, opnum, type,
7473 ind_levels, win);
7474 fprintf (stderr,
7475 "\nrs6000_legitimize_reload_address: mode = %s, opnum = %d, "
7476 "type = %d, ind_levels = %d, win = %d, original addr:\n",
7477 GET_MODE_NAME (mode), opnum, type, ind_levels, *win);
7478 debug_rtx (x);
7480 if (x == ret)
7481 fprintf (stderr, "Same address returned\n");
7482 else if (!ret)
7483 fprintf (stderr, "NULL returned\n");
7484 else
7486 fprintf (stderr, "New address:\n");
7487 debug_rtx (ret);
7490 return ret;
7493 /* TARGET_LEGITIMATE_ADDRESS_P recognizes an RTL expression
7494 that is a valid memory address for an instruction.
7495 The MODE argument is the machine mode for the MEM expression
7496 that wants to use this address.
7498 On the RS/6000, there are four valid address: a SYMBOL_REF that
7499 refers to a constant pool entry of an address (or the sum of it
7500 plus a constant), a short (16-bit signed) constant plus a register,
7501 the sum of two registers, or a register indirect, possibly with an
7502 auto-increment. For DFmode, DDmode and DImode with a constant plus
7503 register, we must ensure that both words are addressable or PowerPC64
7504 with offset word aligned.
7506 For modes spanning multiple registers (DFmode and DDmode in 32-bit GPRs,
7507 32-bit DImode, TImode, TFmode, TDmode), indexed addressing cannot be used
7508 because adjacent memory cells are accessed by adding word-sized offsets
7509 during assembly output. */
7510 static bool
7511 rs6000_legitimate_address_p (enum machine_mode mode, rtx x, bool reg_ok_strict)
7513 bool reg_offset_p = reg_offset_addressing_ok_p (mode);
7515 /* If this is an unaligned stvx/ldvx type address, discard the outer AND. */
7516 if (VECTOR_MEM_ALTIVEC_P (mode)
7517 && GET_CODE (x) == AND
7518 && GET_CODE (XEXP (x, 1)) == CONST_INT
7519 && INTVAL (XEXP (x, 1)) == -16)
7520 x = XEXP (x, 0);
7522 if (TARGET_ELF && RS6000_SYMBOL_REF_TLS_P (x))
7523 return 0;
7524 if (legitimate_indirect_address_p (x, reg_ok_strict))
7525 return 1;
7526 if (TARGET_UPDATE
7527 && (GET_CODE (x) == PRE_INC || GET_CODE (x) == PRE_DEC)
7528 && mode_supports_pre_incdec_p (mode)
7529 && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict))
7530 return 1;
7531 if (virtual_stack_registers_memory_p (x))
7532 return 1;
7533 if (reg_offset_p && legitimate_small_data_p (mode, x))
7534 return 1;
7535 if (reg_offset_p
7536 && legitimate_constant_pool_address_p (x, mode,
7537 reg_ok_strict || lra_in_progress))
7538 return 1;
7539 /* For TImode, if we have load/store quad and TImode in VSX registers, only
7540 allow register indirect addresses. This will allow the values to go in
7541 either GPRs or VSX registers without reloading. The vector types would
7542 tend to go into VSX registers, so we allow REG+REG, while TImode seems
7543 somewhat split, in that some uses are GPR based, and some VSX based. */
7544 if (mode == TImode && TARGET_QUAD_MEMORY && TARGET_VSX_TIMODE)
7545 return 0;
7546 /* If not REG_OK_STRICT (before reload) let pass any stack offset. */
7547 if (! reg_ok_strict
7548 && reg_offset_p
7549 && GET_CODE (x) == PLUS
7550 && GET_CODE (XEXP (x, 0)) == REG
7551 && (XEXP (x, 0) == virtual_stack_vars_rtx
7552 || XEXP (x, 0) == arg_pointer_rtx)
7553 && GET_CODE (XEXP (x, 1)) == CONST_INT)
7554 return 1;
7555 if (rs6000_legitimate_offset_address_p (mode, x, reg_ok_strict, false))
7556 return 1;
7557 if (mode != TFmode
7558 && mode != TDmode
7559 && ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
7560 || TARGET_POWERPC64
7561 || (mode != DFmode && mode != DDmode)
7562 || (TARGET_E500_DOUBLE && mode != DDmode))
7563 && (TARGET_POWERPC64 || mode != DImode)
7564 && (mode != TImode || VECTOR_MEM_VSX_P (TImode))
7565 && mode != PTImode
7566 && !avoiding_indexed_address_p (mode)
7567 && legitimate_indexed_address_p (x, reg_ok_strict))
7568 return 1;
7569 if (TARGET_UPDATE && GET_CODE (x) == PRE_MODIFY
7570 && mode_supports_pre_modify_p (mode)
7571 && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict)
7572 && (rs6000_legitimate_offset_address_p (mode, XEXP (x, 1),
7573 reg_ok_strict, false)
7574 || (!avoiding_indexed_address_p (mode)
7575 && legitimate_indexed_address_p (XEXP (x, 1), reg_ok_strict)))
7576 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
7577 return 1;
7578 if (reg_offset_p && legitimate_lo_sum_address_p (mode, x, reg_ok_strict))
7579 return 1;
7580 return 0;
7583 /* Debug version of rs6000_legitimate_address_p. */
7584 static bool
7585 rs6000_debug_legitimate_address_p (enum machine_mode mode, rtx x,
7586 bool reg_ok_strict)
7588 bool ret = rs6000_legitimate_address_p (mode, x, reg_ok_strict);
7589 fprintf (stderr,
7590 "\nrs6000_legitimate_address_p: return = %s, mode = %s, "
7591 "strict = %d, reload = %s, code = %s\n",
7592 ret ? "true" : "false",
7593 GET_MODE_NAME (mode),
7594 reg_ok_strict,
7595 (reload_completed
7596 ? "after"
7597 : (reload_in_progress ? "progress" : "before")),
7598 GET_RTX_NAME (GET_CODE (x)));
7599 debug_rtx (x);
7601 return ret;
7604 /* Implement TARGET_MODE_DEPENDENT_ADDRESS_P. */
7606 static bool
7607 rs6000_mode_dependent_address_p (const_rtx addr,
7608 addr_space_t as ATTRIBUTE_UNUSED)
7610 return rs6000_mode_dependent_address_ptr (addr);
7613 /* Go to LABEL if ADDR (a legitimate address expression)
7614 has an effect that depends on the machine mode it is used for.
7616 On the RS/6000 this is true of all integral offsets (since AltiVec
7617 and VSX modes don't allow them) or is a pre-increment or decrement.
7619 ??? Except that due to conceptual problems in offsettable_address_p
7620 we can't really report the problems of integral offsets. So leave
7621 this assuming that the adjustable offset must be valid for the
7622 sub-words of a TFmode operand, which is what we had before. */
7624 static bool
7625 rs6000_mode_dependent_address (const_rtx addr)
7627 switch (GET_CODE (addr))
7629 case PLUS:
7630 /* Any offset from virtual_stack_vars_rtx and arg_pointer_rtx
7631 is considered a legitimate address before reload, so there
7632 are no offset restrictions in that case. Note that this
7633 condition is safe in strict mode because any address involving
7634 virtual_stack_vars_rtx or arg_pointer_rtx would already have
7635 been rejected as illegitimate. */
7636 if (XEXP (addr, 0) != virtual_stack_vars_rtx
7637 && XEXP (addr, 0) != arg_pointer_rtx
7638 && GET_CODE (XEXP (addr, 1)) == CONST_INT)
7640 unsigned HOST_WIDE_INT val = INTVAL (XEXP (addr, 1));
7641 return val + 0x8000 >= 0x10000 - (TARGET_POWERPC64 ? 8 : 12);
7643 break;
7645 case LO_SUM:
7646 /* Anything in the constant pool is sufficiently aligned that
7647 all bytes have the same high part address. */
7648 return !legitimate_constant_pool_address_p (addr, QImode, false);
7650 /* Auto-increment cases are now treated generically in recog.c. */
7651 case PRE_MODIFY:
7652 return TARGET_UPDATE;
7654 /* AND is only allowed in Altivec loads. */
7655 case AND:
7656 return true;
7658 default:
7659 break;
7662 return false;
7665 /* Debug version of rs6000_mode_dependent_address. */
7666 static bool
7667 rs6000_debug_mode_dependent_address (const_rtx addr)
7669 bool ret = rs6000_mode_dependent_address (addr);
7671 fprintf (stderr, "\nrs6000_mode_dependent_address: ret = %s\n",
7672 ret ? "true" : "false");
7673 debug_rtx (addr);
7675 return ret;
7678 /* Implement FIND_BASE_TERM. */
7681 rs6000_find_base_term (rtx op)
7683 rtx base;
7685 base = op;
7686 if (GET_CODE (base) == CONST)
7687 base = XEXP (base, 0);
7688 if (GET_CODE (base) == PLUS)
7689 base = XEXP (base, 0);
7690 if (GET_CODE (base) == UNSPEC)
7691 switch (XINT (base, 1))
7693 case UNSPEC_TOCREL:
7694 case UNSPEC_MACHOPIC_OFFSET:
7695 /* OP represents SYM [+ OFFSET] - ANCHOR. SYM is the base term
7696 for aliasing purposes. */
7697 return XVECEXP (base, 0, 0);
7700 return op;
7703 /* More elaborate version of recog's offsettable_memref_p predicate
7704 that works around the ??? note of rs6000_mode_dependent_address.
7705 In particular it accepts
7707 (mem:DI (plus:SI (reg/f:SI 31 31) (const_int 32760 [0x7ff8])))
7709 in 32-bit mode, that the recog predicate rejects. */
7711 static bool
7712 rs6000_offsettable_memref_p (rtx op, enum machine_mode reg_mode)
7714 bool worst_case;
7716 if (!MEM_P (op))
7717 return false;
7719 /* First mimic offsettable_memref_p. */
7720 if (offsettable_address_p (true, GET_MODE (op), XEXP (op, 0)))
7721 return true;
7723 /* offsettable_address_p invokes rs6000_mode_dependent_address, but
7724 the latter predicate knows nothing about the mode of the memory
7725 reference and, therefore, assumes that it is the largest supported
7726 mode (TFmode). As a consequence, legitimate offsettable memory
7727 references are rejected. rs6000_legitimate_offset_address_p contains
7728 the correct logic for the PLUS case of rs6000_mode_dependent_address,
7729 at least with a little bit of help here given that we know the
7730 actual registers used. */
7731 worst_case = ((TARGET_POWERPC64 && GET_MODE_CLASS (reg_mode) == MODE_INT)
7732 || GET_MODE_SIZE (reg_mode) == 4);
7733 return rs6000_legitimate_offset_address_p (GET_MODE (op), XEXP (op, 0),
7734 true, worst_case);
7737 /* Change register usage conditional on target flags. */
7738 static void
7739 rs6000_conditional_register_usage (void)
7741 int i;
7743 if (TARGET_DEBUG_TARGET)
7744 fprintf (stderr, "rs6000_conditional_register_usage called\n");
7746 /* Set MQ register fixed (already call_used) so that it will not be
7747 allocated. */
7748 fixed_regs[64] = 1;
7750 /* 64-bit AIX and Linux reserve GPR13 for thread-private data. */
7751 if (TARGET_64BIT)
7752 fixed_regs[13] = call_used_regs[13]
7753 = call_really_used_regs[13] = 1;
7755 /* Conditionally disable FPRs. */
7756 if (TARGET_SOFT_FLOAT || !TARGET_FPRS)
7757 for (i = 32; i < 64; i++)
7758 fixed_regs[i] = call_used_regs[i]
7759 = call_really_used_regs[i] = 1;
7761 /* The TOC register is not killed across calls in a way that is
7762 visible to the compiler. */
7763 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
7764 call_really_used_regs[2] = 0;
7766 if (DEFAULT_ABI == ABI_V4
7767 && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
7768 && flag_pic == 2)
7769 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
7771 if (DEFAULT_ABI == ABI_V4
7772 && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
7773 && flag_pic == 1)
7774 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
7775 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
7776 = call_really_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
7778 if (DEFAULT_ABI == ABI_DARWIN
7779 && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM)
7780 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
7781 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
7782 = call_really_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
7784 if (TARGET_TOC && TARGET_MINIMAL_TOC)
7785 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
7786 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
7788 if (TARGET_SPE)
7790 global_regs[SPEFSCR_REGNO] = 1;
7791 /* We used to use r14 as FIXED_SCRATCH to address SPE 64-bit
7792 registers in prologues and epilogues. We no longer use r14
7793 for FIXED_SCRATCH, but we're keeping r14 out of the allocation
7794 pool for link-compatibility with older versions of GCC. Once
7795 "old" code has died out, we can return r14 to the allocation
7796 pool. */
7797 fixed_regs[14]
7798 = call_used_regs[14]
7799 = call_really_used_regs[14] = 1;
7802 if (!TARGET_ALTIVEC && !TARGET_VSX)
7804 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
7805 fixed_regs[i] = call_used_regs[i] = call_really_used_regs[i] = 1;
7806 call_really_used_regs[VRSAVE_REGNO] = 1;
7809 if (TARGET_ALTIVEC || TARGET_VSX)
7810 global_regs[VSCR_REGNO] = 1;
7812 if (TARGET_ALTIVEC_ABI)
7814 for (i = FIRST_ALTIVEC_REGNO; i < FIRST_ALTIVEC_REGNO + 20; ++i)
7815 call_used_regs[i] = call_really_used_regs[i] = 1;
7817 /* AIX reserves VR20:31 in non-extended ABI mode. */
7818 if (TARGET_XCOFF)
7819 for (i = FIRST_ALTIVEC_REGNO + 20; i < FIRST_ALTIVEC_REGNO + 32; ++i)
7820 fixed_regs[i] = call_used_regs[i] = call_really_used_regs[i] = 1;
7825 /* Try to output insns to set TARGET equal to the constant C if it can
7826 be done in less than N insns. Do all computations in MODE.
7827 Returns the place where the output has been placed if it can be
7828 done and the insns have been emitted. If it would take more than N
7829 insns, zero is returned and no insns and emitted. */
7832 rs6000_emit_set_const (rtx dest, enum machine_mode mode,
7833 rtx source, int n ATTRIBUTE_UNUSED)
7835 rtx result, insn, set;
7836 HOST_WIDE_INT c0, c1;
7838 switch (mode)
7840 case QImode:
7841 case HImode:
7842 if (dest == NULL)
7843 dest = gen_reg_rtx (mode);
7844 emit_insn (gen_rtx_SET (VOIDmode, dest, source));
7845 return dest;
7847 case SImode:
7848 result = !can_create_pseudo_p () ? dest : gen_reg_rtx (SImode);
7850 emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (result),
7851 GEN_INT (INTVAL (source)
7852 & (~ (HOST_WIDE_INT) 0xffff))));
7853 emit_insn (gen_rtx_SET (VOIDmode, dest,
7854 gen_rtx_IOR (SImode, copy_rtx (result),
7855 GEN_INT (INTVAL (source) & 0xffff))));
7856 result = dest;
7857 break;
7859 case DImode:
7860 switch (GET_CODE (source))
7862 case CONST_INT:
7863 c0 = INTVAL (source);
7864 c1 = -(c0 < 0);
7865 break;
7867 default:
7868 gcc_unreachable ();
7871 result = rs6000_emit_set_long_const (dest, c0, c1);
7872 break;
7874 default:
7875 gcc_unreachable ();
7878 insn = get_last_insn ();
7879 set = single_set (insn);
7880 if (! CONSTANT_P (SET_SRC (set)))
7881 set_unique_reg_note (insn, REG_EQUAL, source);
7883 return result;
7886 /* Having failed to find a 3 insn sequence in rs6000_emit_set_const,
7887 fall back to a straight forward decomposition. We do this to avoid
7888 exponential run times encountered when looking for longer sequences
7889 with rs6000_emit_set_const. */
7890 static rtx
7891 rs6000_emit_set_long_const (rtx dest, HOST_WIDE_INT c1, HOST_WIDE_INT c2)
7893 if (!TARGET_POWERPC64)
7895 rtx operand1, operand2;
7897 operand1 = operand_subword_force (dest, WORDS_BIG_ENDIAN == 0,
7898 DImode);
7899 operand2 = operand_subword_force (copy_rtx (dest), WORDS_BIG_ENDIAN != 0,
7900 DImode);
7901 emit_move_insn (operand1, GEN_INT (c1));
7902 emit_move_insn (operand2, GEN_INT (c2));
7904 else
7906 HOST_WIDE_INT ud1, ud2, ud3, ud4;
7908 ud1 = c1 & 0xffff;
7909 ud2 = (c1 & 0xffff0000) >> 16;
7910 c2 = c1 >> 32;
7911 ud3 = c2 & 0xffff;
7912 ud4 = (c2 & 0xffff0000) >> 16;
7914 if ((ud4 == 0xffff && ud3 == 0xffff && ud2 == 0xffff && (ud1 & 0x8000))
7915 || (ud4 == 0 && ud3 == 0 && ud2 == 0 && ! (ud1 & 0x8000)))
7916 emit_move_insn (dest, GEN_INT ((ud1 ^ 0x8000) - 0x8000));
7918 else if ((ud4 == 0xffff && ud3 == 0xffff && (ud2 & 0x8000))
7919 || (ud4 == 0 && ud3 == 0 && ! (ud2 & 0x8000)))
7921 emit_move_insn (dest, GEN_INT (((ud2 << 16) ^ 0x80000000)
7922 - 0x80000000));
7923 if (ud1 != 0)
7924 emit_move_insn (copy_rtx (dest),
7925 gen_rtx_IOR (DImode, copy_rtx (dest),
7926 GEN_INT (ud1)));
7928 else if (ud3 == 0 && ud4 == 0)
7930 gcc_assert (ud2 & 0x8000);
7931 emit_move_insn (dest, GEN_INT (((ud2 << 16) ^ 0x80000000)
7932 - 0x80000000));
7933 if (ud1 != 0)
7934 emit_move_insn (copy_rtx (dest),
7935 gen_rtx_IOR (DImode, copy_rtx (dest),
7936 GEN_INT (ud1)));
7937 emit_move_insn (copy_rtx (dest),
7938 gen_rtx_ZERO_EXTEND (DImode,
7939 gen_lowpart (SImode,
7940 copy_rtx (dest))));
7942 else if ((ud4 == 0xffff && (ud3 & 0x8000))
7943 || (ud4 == 0 && ! (ud3 & 0x8000)))
7945 emit_move_insn (dest, GEN_INT (((ud3 << 16) ^ 0x80000000)
7946 - 0x80000000));
7947 if (ud2 != 0)
7948 emit_move_insn (copy_rtx (dest),
7949 gen_rtx_IOR (DImode, copy_rtx (dest),
7950 GEN_INT (ud2)));
7951 emit_move_insn (copy_rtx (dest),
7952 gen_rtx_ASHIFT (DImode, copy_rtx (dest),
7953 GEN_INT (16)));
7954 if (ud1 != 0)
7955 emit_move_insn (copy_rtx (dest),
7956 gen_rtx_IOR (DImode, copy_rtx (dest),
7957 GEN_INT (ud1)));
7959 else
7961 emit_move_insn (dest, GEN_INT (((ud4 << 16) ^ 0x80000000)
7962 - 0x80000000));
7963 if (ud3 != 0)
7964 emit_move_insn (copy_rtx (dest),
7965 gen_rtx_IOR (DImode, copy_rtx (dest),
7966 GEN_INT (ud3)));
7968 emit_move_insn (copy_rtx (dest),
7969 gen_rtx_ASHIFT (DImode, copy_rtx (dest),
7970 GEN_INT (32)));
7971 if (ud2 != 0)
7972 emit_move_insn (copy_rtx (dest),
7973 gen_rtx_IOR (DImode, copy_rtx (dest),
7974 GEN_INT (ud2 << 16)));
7975 if (ud1 != 0)
7976 emit_move_insn (copy_rtx (dest),
7977 gen_rtx_IOR (DImode, copy_rtx (dest),
7978 GEN_INT (ud1)));
7981 return dest;
7984 /* Helper for the following. Get rid of [r+r] memory refs
7985 in cases where it won't work (TImode, TFmode, TDmode, PTImode). */
7987 static void
7988 rs6000_eliminate_indexed_memrefs (rtx operands[2])
7990 if (reload_in_progress)
7991 return;
7993 if (GET_CODE (operands[0]) == MEM
7994 && GET_CODE (XEXP (operands[0], 0)) != REG
7995 && ! legitimate_constant_pool_address_p (XEXP (operands[0], 0),
7996 GET_MODE (operands[0]), false))
7997 operands[0]
7998 = replace_equiv_address (operands[0],
7999 copy_addr_to_reg (XEXP (operands[0], 0)));
8001 if (GET_CODE (operands[1]) == MEM
8002 && GET_CODE (XEXP (operands[1], 0)) != REG
8003 && ! legitimate_constant_pool_address_p (XEXP (operands[1], 0),
8004 GET_MODE (operands[1]), false))
8005 operands[1]
8006 = replace_equiv_address (operands[1],
8007 copy_addr_to_reg (XEXP (operands[1], 0)));
8010 /* Generate a vector of constants to permute MODE for a little-endian
8011 storage operation by swapping the two halves of a vector. */
8012 static rtvec
8013 rs6000_const_vec (enum machine_mode mode)
8015 int i, subparts;
8016 rtvec v;
8018 switch (mode)
8020 case V1TImode:
8021 subparts = 1;
8022 break;
8023 case V2DFmode:
8024 case V2DImode:
8025 subparts = 2;
8026 break;
8027 case V4SFmode:
8028 case V4SImode:
8029 subparts = 4;
8030 break;
8031 case V8HImode:
8032 subparts = 8;
8033 break;
8034 case V16QImode:
8035 subparts = 16;
8036 break;
8037 default:
8038 gcc_unreachable();
8041 v = rtvec_alloc (subparts);
8043 for (i = 0; i < subparts / 2; ++i)
8044 RTVEC_ELT (v, i) = gen_rtx_CONST_INT (DImode, i + subparts / 2);
8045 for (i = subparts / 2; i < subparts; ++i)
8046 RTVEC_ELT (v, i) = gen_rtx_CONST_INT (DImode, i - subparts / 2);
8048 return v;
8051 /* Generate a permute rtx that represents an lxvd2x, stxvd2x, or xxpermdi
8052 for a VSX load or store operation. */
8054 rs6000_gen_le_vsx_permute (rtx source, enum machine_mode mode)
8056 rtx par = gen_rtx_PARALLEL (VOIDmode, rs6000_const_vec (mode));
8057 return gen_rtx_VEC_SELECT (mode, source, par);
8060 /* Emit a little-endian load from vector memory location SOURCE to VSX
8061 register DEST in mode MODE. The load is done with two permuting
8062 insn's that represent an lxvd2x and xxpermdi. */
8063 void
8064 rs6000_emit_le_vsx_load (rtx dest, rtx source, enum machine_mode mode)
8066 rtx tmp, permute_mem, permute_reg;
8068 /* Use V2DImode to do swaps of types with 128-bit scalare parts (TImode,
8069 V1TImode). */
8070 if (mode == TImode || mode == V1TImode)
8072 mode = V2DImode;
8073 dest = gen_lowpart (V2DImode, dest);
8074 source = adjust_address (source, V2DImode, 0);
8077 tmp = can_create_pseudo_p () ? gen_reg_rtx_and_attrs (dest) : dest;
8078 permute_mem = rs6000_gen_le_vsx_permute (source, mode);
8079 permute_reg = rs6000_gen_le_vsx_permute (tmp, mode);
8080 emit_insn (gen_rtx_SET (VOIDmode, tmp, permute_mem));
8081 emit_insn (gen_rtx_SET (VOIDmode, dest, permute_reg));
8084 /* Emit a little-endian store to vector memory location DEST from VSX
8085 register SOURCE in mode MODE. The store is done with two permuting
8086 insn's that represent an xxpermdi and an stxvd2x. */
8087 void
8088 rs6000_emit_le_vsx_store (rtx dest, rtx source, enum machine_mode mode)
8090 rtx tmp, permute_src, permute_tmp;
8092 /* Use V2DImode to do swaps of types with 128-bit scalare parts (TImode,
8093 V1TImode). */
8094 if (mode == TImode || mode == V1TImode)
8096 mode = V2DImode;
8097 dest = adjust_address (dest, V2DImode, 0);
8098 source = gen_lowpart (V2DImode, source);
8101 tmp = can_create_pseudo_p () ? gen_reg_rtx_and_attrs (source) : source;
8102 permute_src = rs6000_gen_le_vsx_permute (source, mode);
8103 permute_tmp = rs6000_gen_le_vsx_permute (tmp, mode);
8104 emit_insn (gen_rtx_SET (VOIDmode, tmp, permute_src));
8105 emit_insn (gen_rtx_SET (VOIDmode, dest, permute_tmp));
8108 /* Emit a sequence representing a little-endian VSX load or store,
8109 moving data from SOURCE to DEST in mode MODE. This is done
8110 separately from rs6000_emit_move to ensure it is called only
8111 during expand. LE VSX loads and stores introduced later are
8112 handled with a split. The expand-time RTL generation allows
8113 us to optimize away redundant pairs of register-permutes. */
8114 void
8115 rs6000_emit_le_vsx_move (rtx dest, rtx source, enum machine_mode mode)
8117 gcc_assert (!BYTES_BIG_ENDIAN
8118 && VECTOR_MEM_VSX_P (mode)
8119 && !gpr_or_gpr_p (dest, source)
8120 && (MEM_P (source) ^ MEM_P (dest)));
8122 if (MEM_P (source))
8124 gcc_assert (REG_P (dest) || GET_CODE (dest) == SUBREG);
8125 rs6000_emit_le_vsx_load (dest, source, mode);
8127 else
8129 if (!REG_P (source))
8130 source = force_reg (mode, source);
8131 rs6000_emit_le_vsx_store (dest, source, mode);
8135 /* Emit a move from SOURCE to DEST in mode MODE. */
8136 void
8137 rs6000_emit_move (rtx dest, rtx source, enum machine_mode mode)
8139 rtx operands[2];
8140 operands[0] = dest;
8141 operands[1] = source;
8143 if (TARGET_DEBUG_ADDR)
8145 fprintf (stderr,
8146 "\nrs6000_emit_move: mode = %s, reload_in_progress = %d, "
8147 "reload_completed = %d, can_create_pseudos = %d.\ndest:\n",
8148 GET_MODE_NAME (mode),
8149 reload_in_progress,
8150 reload_completed,
8151 can_create_pseudo_p ());
8152 debug_rtx (dest);
8153 fprintf (stderr, "source:\n");
8154 debug_rtx (source);
8157 /* Sanity checks. Check that we get CONST_DOUBLE only when we should. */
8158 if (GET_CODE (operands[1]) == CONST_DOUBLE
8159 && ! FLOAT_MODE_P (mode)
8160 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
8162 /* FIXME. This should never happen. */
8163 /* Since it seems that it does, do the safe thing and convert
8164 to a CONST_INT. */
8165 operands[1] = gen_int_mode (CONST_DOUBLE_LOW (operands[1]), mode);
8167 gcc_assert (GET_CODE (operands[1]) != CONST_DOUBLE
8168 || FLOAT_MODE_P (mode)
8169 || ((CONST_DOUBLE_HIGH (operands[1]) != 0
8170 || CONST_DOUBLE_LOW (operands[1]) < 0)
8171 && (CONST_DOUBLE_HIGH (operands[1]) != -1
8172 || CONST_DOUBLE_LOW (operands[1]) >= 0)));
8174 /* Check if GCC is setting up a block move that will end up using FP
8175 registers as temporaries. We must make sure this is acceptable. */
8176 if (GET_CODE (operands[0]) == MEM
8177 && GET_CODE (operands[1]) == MEM
8178 && mode == DImode
8179 && (SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[0]))
8180 || SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[1])))
8181 && ! (SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[0]) > 32
8182 ? 32 : MEM_ALIGN (operands[0])))
8183 || SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[1]) > 32
8184 ? 32
8185 : MEM_ALIGN (operands[1]))))
8186 && ! MEM_VOLATILE_P (operands [0])
8187 && ! MEM_VOLATILE_P (operands [1]))
8189 emit_move_insn (adjust_address (operands[0], SImode, 0),
8190 adjust_address (operands[1], SImode, 0));
8191 emit_move_insn (adjust_address (copy_rtx (operands[0]), SImode, 4),
8192 adjust_address (copy_rtx (operands[1]), SImode, 4));
8193 return;
8196 if (can_create_pseudo_p () && GET_CODE (operands[0]) == MEM
8197 && !gpc_reg_operand (operands[1], mode))
8198 operands[1] = force_reg (mode, operands[1]);
8200 /* Recognize the case where operand[1] is a reference to thread-local
8201 data and load its address to a register. */
8202 if (rs6000_tls_referenced_p (operands[1]))
8204 enum tls_model model;
8205 rtx tmp = operands[1];
8206 rtx addend = NULL;
8208 if (GET_CODE (tmp) == CONST && GET_CODE (XEXP (tmp, 0)) == PLUS)
8210 addend = XEXP (XEXP (tmp, 0), 1);
8211 tmp = XEXP (XEXP (tmp, 0), 0);
8214 gcc_assert (GET_CODE (tmp) == SYMBOL_REF);
8215 model = SYMBOL_REF_TLS_MODEL (tmp);
8216 gcc_assert (model != 0);
8218 tmp = rs6000_legitimize_tls_address (tmp, model);
8219 if (addend)
8221 tmp = gen_rtx_PLUS (mode, tmp, addend);
8222 tmp = force_operand (tmp, operands[0]);
8224 operands[1] = tmp;
8227 /* Handle the case where reload calls us with an invalid address. */
8228 if (reload_in_progress && mode == Pmode
8229 && (! general_operand (operands[1], mode)
8230 || ! nonimmediate_operand (operands[0], mode)))
8231 goto emit_set;
8233 /* 128-bit constant floating-point values on Darwin should really be
8234 loaded as two parts. */
8235 if (!TARGET_IEEEQUAD && TARGET_LONG_DOUBLE_128
8236 && mode == TFmode && GET_CODE (operands[1]) == CONST_DOUBLE)
8238 rs6000_emit_move (simplify_gen_subreg (DFmode, operands[0], mode, 0),
8239 simplify_gen_subreg (DFmode, operands[1], mode, 0),
8240 DFmode);
8241 rs6000_emit_move (simplify_gen_subreg (DFmode, operands[0], mode,
8242 GET_MODE_SIZE (DFmode)),
8243 simplify_gen_subreg (DFmode, operands[1], mode,
8244 GET_MODE_SIZE (DFmode)),
8245 DFmode);
8246 return;
8249 if (reload_in_progress && cfun->machine->sdmode_stack_slot != NULL_RTX)
8250 cfun->machine->sdmode_stack_slot =
8251 eliminate_regs (cfun->machine->sdmode_stack_slot, VOIDmode, NULL_RTX);
8254 if (lra_in_progress
8255 && mode == SDmode
8256 && REG_P (operands[0]) && REGNO (operands[0]) >= FIRST_PSEUDO_REGISTER
8257 && reg_preferred_class (REGNO (operands[0])) == NO_REGS
8258 && (REG_P (operands[1])
8259 || (GET_CODE (operands[1]) == SUBREG
8260 && REG_P (SUBREG_REG (operands[1])))))
8262 int regno = REGNO (GET_CODE (operands[1]) == SUBREG
8263 ? SUBREG_REG (operands[1]) : operands[1]);
8264 enum reg_class cl;
8266 if (regno >= FIRST_PSEUDO_REGISTER)
8268 cl = reg_preferred_class (regno);
8269 gcc_assert (cl != NO_REGS);
8270 regno = ira_class_hard_regs[cl][0];
8272 if (FP_REGNO_P (regno))
8274 if (GET_MODE (operands[0]) != DDmode)
8275 operands[0] = gen_rtx_SUBREG (DDmode, operands[0], 0);
8276 emit_insn (gen_movsd_store (operands[0], operands[1]));
8278 else if (INT_REGNO_P (regno))
8279 emit_insn (gen_movsd_hardfloat (operands[0], operands[1]));
8280 else
8281 gcc_unreachable();
8282 return;
8284 if (lra_in_progress
8285 && mode == SDmode
8286 && (REG_P (operands[0])
8287 || (GET_CODE (operands[0]) == SUBREG
8288 && REG_P (SUBREG_REG (operands[0]))))
8289 && REG_P (operands[1]) && REGNO (operands[1]) >= FIRST_PSEUDO_REGISTER
8290 && reg_preferred_class (REGNO (operands[1])) == NO_REGS)
8292 int regno = REGNO (GET_CODE (operands[0]) == SUBREG
8293 ? SUBREG_REG (operands[0]) : operands[0]);
8294 enum reg_class cl;
8296 if (regno >= FIRST_PSEUDO_REGISTER)
8298 cl = reg_preferred_class (regno);
8299 gcc_assert (cl != NO_REGS);
8300 regno = ira_class_hard_regs[cl][0];
8302 if (FP_REGNO_P (regno))
8304 if (GET_MODE (operands[1]) != DDmode)
8305 operands[1] = gen_rtx_SUBREG (DDmode, operands[1], 0);
8306 emit_insn (gen_movsd_load (operands[0], operands[1]));
8308 else if (INT_REGNO_P (regno))
8309 emit_insn (gen_movsd_hardfloat (operands[0], operands[1]));
8310 else
8311 gcc_unreachable();
8312 return;
8315 if (reload_in_progress
8316 && mode == SDmode
8317 && cfun->machine->sdmode_stack_slot != NULL_RTX
8318 && MEM_P (operands[0])
8319 && rtx_equal_p (operands[0], cfun->machine->sdmode_stack_slot)
8320 && REG_P (operands[1]))
8322 if (FP_REGNO_P (REGNO (operands[1])))
8324 rtx mem = adjust_address_nv (operands[0], DDmode, 0);
8325 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
8326 emit_insn (gen_movsd_store (mem, operands[1]));
8328 else if (INT_REGNO_P (REGNO (operands[1])))
8330 rtx mem = operands[0];
8331 if (BYTES_BIG_ENDIAN)
8332 mem = adjust_address_nv (mem, mode, 4);
8333 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
8334 emit_insn (gen_movsd_hardfloat (mem, operands[1]));
8336 else
8337 gcc_unreachable();
8338 return;
8340 if (reload_in_progress
8341 && mode == SDmode
8342 && REG_P (operands[0])
8343 && MEM_P (operands[1])
8344 && cfun->machine->sdmode_stack_slot != NULL_RTX
8345 && rtx_equal_p (operands[1], cfun->machine->sdmode_stack_slot))
8347 if (FP_REGNO_P (REGNO (operands[0])))
8349 rtx mem = adjust_address_nv (operands[1], DDmode, 0);
8350 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
8351 emit_insn (gen_movsd_load (operands[0], mem));
8353 else if (INT_REGNO_P (REGNO (operands[0])))
8355 rtx mem = operands[1];
8356 if (BYTES_BIG_ENDIAN)
8357 mem = adjust_address_nv (mem, mode, 4);
8358 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
8359 emit_insn (gen_movsd_hardfloat (operands[0], mem));
8361 else
8362 gcc_unreachable();
8363 return;
8366 /* FIXME: In the long term, this switch statement should go away
8367 and be replaced by a sequence of tests based on things like
8368 mode == Pmode. */
8369 switch (mode)
8371 case HImode:
8372 case QImode:
8373 if (CONSTANT_P (operands[1])
8374 && GET_CODE (operands[1]) != CONST_INT)
8375 operands[1] = force_const_mem (mode, operands[1]);
8376 break;
8378 case TFmode:
8379 case TDmode:
8380 rs6000_eliminate_indexed_memrefs (operands);
8381 /* fall through */
8383 case DFmode:
8384 case DDmode:
8385 case SFmode:
8386 case SDmode:
8387 if (CONSTANT_P (operands[1])
8388 && ! easy_fp_constant (operands[1], mode))
8389 operands[1] = force_const_mem (mode, operands[1]);
8390 break;
8392 case V16QImode:
8393 case V8HImode:
8394 case V4SFmode:
8395 case V4SImode:
8396 case V4HImode:
8397 case V2SFmode:
8398 case V2SImode:
8399 case V1DImode:
8400 case V2DFmode:
8401 case V2DImode:
8402 case V1TImode:
8403 if (CONSTANT_P (operands[1])
8404 && !easy_vector_constant (operands[1], mode))
8405 operands[1] = force_const_mem (mode, operands[1]);
8406 break;
8408 case SImode:
8409 case DImode:
8410 /* Use default pattern for address of ELF small data */
8411 if (TARGET_ELF
8412 && mode == Pmode
8413 && DEFAULT_ABI == ABI_V4
8414 && (GET_CODE (operands[1]) == SYMBOL_REF
8415 || GET_CODE (operands[1]) == CONST)
8416 && small_data_operand (operands[1], mode))
8418 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
8419 return;
8422 if (DEFAULT_ABI == ABI_V4
8423 && mode == Pmode && mode == SImode
8424 && flag_pic == 1 && got_operand (operands[1], mode))
8426 emit_insn (gen_movsi_got (operands[0], operands[1]));
8427 return;
8430 if ((TARGET_ELF || DEFAULT_ABI == ABI_DARWIN)
8431 && TARGET_NO_TOC
8432 && ! flag_pic
8433 && mode == Pmode
8434 && CONSTANT_P (operands[1])
8435 && GET_CODE (operands[1]) != HIGH
8436 && GET_CODE (operands[1]) != CONST_INT)
8438 rtx target = (!can_create_pseudo_p ()
8439 ? operands[0]
8440 : gen_reg_rtx (mode));
8442 /* If this is a function address on -mcall-aixdesc,
8443 convert it to the address of the descriptor. */
8444 if (DEFAULT_ABI == ABI_AIX
8445 && GET_CODE (operands[1]) == SYMBOL_REF
8446 && XSTR (operands[1], 0)[0] == '.')
8448 const char *name = XSTR (operands[1], 0);
8449 rtx new_ref;
8450 while (*name == '.')
8451 name++;
8452 new_ref = gen_rtx_SYMBOL_REF (Pmode, name);
8453 CONSTANT_POOL_ADDRESS_P (new_ref)
8454 = CONSTANT_POOL_ADDRESS_P (operands[1]);
8455 SYMBOL_REF_FLAGS (new_ref) = SYMBOL_REF_FLAGS (operands[1]);
8456 SYMBOL_REF_USED (new_ref) = SYMBOL_REF_USED (operands[1]);
8457 SYMBOL_REF_DATA (new_ref) = SYMBOL_REF_DATA (operands[1]);
8458 operands[1] = new_ref;
8461 if (DEFAULT_ABI == ABI_DARWIN)
8463 #if TARGET_MACHO
8464 if (MACHO_DYNAMIC_NO_PIC_P)
8466 /* Take care of any required data indirection. */
8467 operands[1] = rs6000_machopic_legitimize_pic_address (
8468 operands[1], mode, operands[0]);
8469 if (operands[0] != operands[1])
8470 emit_insn (gen_rtx_SET (VOIDmode,
8471 operands[0], operands[1]));
8472 return;
8474 #endif
8475 emit_insn (gen_macho_high (target, operands[1]));
8476 emit_insn (gen_macho_low (operands[0], target, operands[1]));
8477 return;
8480 emit_insn (gen_elf_high (target, operands[1]));
8481 emit_insn (gen_elf_low (operands[0], target, operands[1]));
8482 return;
8485 /* If this is a SYMBOL_REF that refers to a constant pool entry,
8486 and we have put it in the TOC, we just need to make a TOC-relative
8487 reference to it. */
8488 if (TARGET_TOC
8489 && GET_CODE (operands[1]) == SYMBOL_REF
8490 && use_toc_relative_ref (operands[1]))
8491 operands[1] = create_TOC_reference (operands[1], operands[0]);
8492 else if (mode == Pmode
8493 && CONSTANT_P (operands[1])
8494 && GET_CODE (operands[1]) != HIGH
8495 && ((GET_CODE (operands[1]) != CONST_INT
8496 && ! easy_fp_constant (operands[1], mode))
8497 || (GET_CODE (operands[1]) == CONST_INT
8498 && (num_insns_constant (operands[1], mode)
8499 > (TARGET_CMODEL != CMODEL_SMALL ? 3 : 2)))
8500 || (GET_CODE (operands[0]) == REG
8501 && FP_REGNO_P (REGNO (operands[0]))))
8502 && !toc_relative_expr_p (operands[1], false)
8503 && (TARGET_CMODEL == CMODEL_SMALL
8504 || can_create_pseudo_p ()
8505 || (REG_P (operands[0])
8506 && INT_REG_OK_FOR_BASE_P (operands[0], true))))
8509 #if TARGET_MACHO
8510 /* Darwin uses a special PIC legitimizer. */
8511 if (DEFAULT_ABI == ABI_DARWIN && MACHOPIC_INDIRECT)
8513 operands[1] =
8514 rs6000_machopic_legitimize_pic_address (operands[1], mode,
8515 operands[0]);
8516 if (operands[0] != operands[1])
8517 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
8518 return;
8520 #endif
8522 /* If we are to limit the number of things we put in the TOC and
8523 this is a symbol plus a constant we can add in one insn,
8524 just put the symbol in the TOC and add the constant. Don't do
8525 this if reload is in progress. */
8526 if (GET_CODE (operands[1]) == CONST
8527 && TARGET_NO_SUM_IN_TOC && ! reload_in_progress
8528 && GET_CODE (XEXP (operands[1], 0)) == PLUS
8529 && add_operand (XEXP (XEXP (operands[1], 0), 1), mode)
8530 && (GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == LABEL_REF
8531 || GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == SYMBOL_REF)
8532 && ! side_effects_p (operands[0]))
8534 rtx sym =
8535 force_const_mem (mode, XEXP (XEXP (operands[1], 0), 0));
8536 rtx other = XEXP (XEXP (operands[1], 0), 1);
8538 sym = force_reg (mode, sym);
8539 emit_insn (gen_add3_insn (operands[0], sym, other));
8540 return;
8543 operands[1] = force_const_mem (mode, operands[1]);
8545 if (TARGET_TOC
8546 && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
8547 && constant_pool_expr_p (XEXP (operands[1], 0))
8548 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (
8549 get_pool_constant (XEXP (operands[1], 0)),
8550 get_pool_mode (XEXP (operands[1], 0))))
8552 rtx tocref = create_TOC_reference (XEXP (operands[1], 0),
8553 operands[0]);
8554 operands[1] = gen_const_mem (mode, tocref);
8555 set_mem_alias_set (operands[1], get_TOC_alias_set ());
8558 break;
8560 case TImode:
8561 if (!VECTOR_MEM_VSX_P (TImode))
8562 rs6000_eliminate_indexed_memrefs (operands);
8563 break;
8565 case PTImode:
8566 rs6000_eliminate_indexed_memrefs (operands);
8567 break;
8569 default:
8570 fatal_insn ("bad move", gen_rtx_SET (VOIDmode, dest, source));
8573 /* Above, we may have called force_const_mem which may have returned
8574 an invalid address. If we can, fix this up; otherwise, reload will
8575 have to deal with it. */
8576 if (GET_CODE (operands[1]) == MEM && ! reload_in_progress)
8577 operands[1] = validize_mem (operands[1]);
8579 emit_set:
8580 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
8583 /* Return true if a structure, union or array containing FIELD should be
8584 accessed using `BLKMODE'.
8586 For the SPE, simd types are V2SI, and gcc can be tempted to put the
8587 entire thing in a DI and use subregs to access the internals.
8588 store_bit_field() will force (subreg:DI (reg:V2SI x))'s to the
8589 back-end. Because a single GPR can hold a V2SI, but not a DI, the
8590 best thing to do is set structs to BLKmode and avoid Severe Tire
8591 Damage.
8593 On e500 v2, DF and DI modes suffer from the same anomaly. DF can
8594 fit into 1, whereas DI still needs two. */
8596 static bool
8597 rs6000_member_type_forces_blk (const_tree field, enum machine_mode mode)
8599 return ((TARGET_SPE && TREE_CODE (TREE_TYPE (field)) == VECTOR_TYPE)
8600 || (TARGET_E500_DOUBLE && mode == DFmode));
8603 /* Nonzero if we can use a floating-point register to pass this arg. */
8604 #define USE_FP_FOR_ARG_P(CUM,MODE) \
8605 (SCALAR_FLOAT_MODE_P (MODE) \
8606 && (CUM)->fregno <= FP_ARG_MAX_REG \
8607 && TARGET_HARD_FLOAT && TARGET_FPRS)
8609 /* Nonzero if we can use an AltiVec register to pass this arg. */
8610 #define USE_ALTIVEC_FOR_ARG_P(CUM,MODE,NAMED) \
8611 (ALTIVEC_OR_VSX_VECTOR_MODE (MODE) \
8612 && (CUM)->vregno <= ALTIVEC_ARG_MAX_REG \
8613 && TARGET_ALTIVEC_ABI \
8614 && (NAMED))
8616 /* Walk down the type tree of TYPE counting consecutive base elements.
8617 If *MODEP is VOIDmode, then set it to the first valid floating point
8618 or vector type. If a non-floating point or vector type is found, or
8619 if a floating point or vector type that doesn't match a non-VOIDmode
8620 *MODEP is found, then return -1, otherwise return the count in the
8621 sub-tree. */
8623 static int
8624 rs6000_aggregate_candidate (const_tree type, enum machine_mode *modep)
8626 enum machine_mode mode;
8627 HOST_WIDE_INT size;
8629 switch (TREE_CODE (type))
8631 case REAL_TYPE:
8632 mode = TYPE_MODE (type);
8633 if (!SCALAR_FLOAT_MODE_P (mode))
8634 return -1;
8636 if (*modep == VOIDmode)
8637 *modep = mode;
8639 if (*modep == mode)
8640 return 1;
8642 break;
8644 case COMPLEX_TYPE:
8645 mode = TYPE_MODE (TREE_TYPE (type));
8646 if (!SCALAR_FLOAT_MODE_P (mode))
8647 return -1;
8649 if (*modep == VOIDmode)
8650 *modep = mode;
8652 if (*modep == mode)
8653 return 2;
8655 break;
8657 case VECTOR_TYPE:
8658 if (!TARGET_ALTIVEC_ABI || !TARGET_ALTIVEC)
8659 return -1;
8661 /* Use V4SImode as representative of all 128-bit vector types. */
8662 size = int_size_in_bytes (type);
8663 switch (size)
8665 case 16:
8666 mode = V4SImode;
8667 break;
8668 default:
8669 return -1;
8672 if (*modep == VOIDmode)
8673 *modep = mode;
8675 /* Vector modes are considered to be opaque: two vectors are
8676 equivalent for the purposes of being homogeneous aggregates
8677 if they are the same size. */
8678 if (*modep == mode)
8679 return 1;
8681 break;
8683 case ARRAY_TYPE:
8685 int count;
8686 tree index = TYPE_DOMAIN (type);
8688 /* Can't handle incomplete types. */
8689 if (!COMPLETE_TYPE_P (type))
8690 return -1;
8692 count = rs6000_aggregate_candidate (TREE_TYPE (type), modep);
8693 if (count == -1
8694 || !index
8695 || !TYPE_MAX_VALUE (index)
8696 || !tree_fits_uhwi_p (TYPE_MAX_VALUE (index))
8697 || !TYPE_MIN_VALUE (index)
8698 || !tree_fits_uhwi_p (TYPE_MIN_VALUE (index))
8699 || count < 0)
8700 return -1;
8702 count *= (1 + tree_to_uhwi (TYPE_MAX_VALUE (index))
8703 - tree_to_uhwi (TYPE_MIN_VALUE (index)));
8705 /* There must be no padding. */
8706 if (!tree_fits_uhwi_p (TYPE_SIZE (type))
8707 || ((HOST_WIDE_INT) tree_to_uhwi (TYPE_SIZE (type))
8708 != count * GET_MODE_BITSIZE (*modep)))
8709 return -1;
8711 return count;
8714 case RECORD_TYPE:
8716 int count = 0;
8717 int sub_count;
8718 tree field;
8720 /* Can't handle incomplete types. */
8721 if (!COMPLETE_TYPE_P (type))
8722 return -1;
8724 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
8726 if (TREE_CODE (field) != FIELD_DECL)
8727 continue;
8729 sub_count = rs6000_aggregate_candidate (TREE_TYPE (field), modep);
8730 if (sub_count < 0)
8731 return -1;
8732 count += sub_count;
8735 /* There must be no padding. */
8736 if (!tree_fits_uhwi_p (TYPE_SIZE (type))
8737 || ((HOST_WIDE_INT) tree_to_uhwi (TYPE_SIZE (type))
8738 != count * GET_MODE_BITSIZE (*modep)))
8739 return -1;
8741 return count;
8744 case UNION_TYPE:
8745 case QUAL_UNION_TYPE:
8747 /* These aren't very interesting except in a degenerate case. */
8748 int count = 0;
8749 int sub_count;
8750 tree field;
8752 /* Can't handle incomplete types. */
8753 if (!COMPLETE_TYPE_P (type))
8754 return -1;
8756 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
8758 if (TREE_CODE (field) != FIELD_DECL)
8759 continue;
8761 sub_count = rs6000_aggregate_candidate (TREE_TYPE (field), modep);
8762 if (sub_count < 0)
8763 return -1;
8764 count = count > sub_count ? count : sub_count;
8767 /* There must be no padding. */
8768 if (!tree_fits_uhwi_p (TYPE_SIZE (type))
8769 || ((HOST_WIDE_INT) tree_to_uhwi (TYPE_SIZE (type))
8770 != count * GET_MODE_BITSIZE (*modep)))
8771 return -1;
8773 return count;
8776 default:
8777 break;
8780 return -1;
8783 /* If an argument, whose type is described by TYPE and MODE, is a homogeneous
8784 float or vector aggregate that shall be passed in FP/vector registers
8785 according to the ELFv2 ABI, return the homogeneous element mode in
8786 *ELT_MODE and the number of elements in *N_ELTS, and return TRUE.
8788 Otherwise, set *ELT_MODE to MODE and *N_ELTS to 1, and return FALSE. */
8790 static bool
8791 rs6000_discover_homogeneous_aggregate (enum machine_mode mode, const_tree type,
8792 enum machine_mode *elt_mode,
8793 int *n_elts)
8795 /* Note that we do not accept complex types at the top level as
8796 homogeneous aggregates; these types are handled via the
8797 targetm.calls.split_complex_arg mechanism. Complex types
8798 can be elements of homogeneous aggregates, however. */
8799 if (DEFAULT_ABI == ABI_ELFv2 && type && AGGREGATE_TYPE_P (type))
8801 enum machine_mode field_mode = VOIDmode;
8802 int field_count = rs6000_aggregate_candidate (type, &field_mode);
8804 if (field_count > 0)
8806 int n_regs = (SCALAR_FLOAT_MODE_P (field_mode)?
8807 (GET_MODE_SIZE (field_mode) + 7) >> 3 : 1);
8809 /* The ELFv2 ABI allows homogeneous aggregates to occupy
8810 up to AGGR_ARG_NUM_REG registers. */
8811 if (field_count * n_regs <= AGGR_ARG_NUM_REG)
8813 if (elt_mode)
8814 *elt_mode = field_mode;
8815 if (n_elts)
8816 *n_elts = field_count;
8817 return true;
8822 if (elt_mode)
8823 *elt_mode = mode;
8824 if (n_elts)
8825 *n_elts = 1;
8826 return false;
8829 /* Return a nonzero value to say to return the function value in
8830 memory, just as large structures are always returned. TYPE will be
8831 the data type of the value, and FNTYPE will be the type of the
8832 function doing the returning, or @code{NULL} for libcalls.
8834 The AIX ABI for the RS/6000 specifies that all structures are
8835 returned in memory. The Darwin ABI does the same.
8837 For the Darwin 64 Bit ABI, a function result can be returned in
8838 registers or in memory, depending on the size of the return data
8839 type. If it is returned in registers, the value occupies the same
8840 registers as it would if it were the first and only function
8841 argument. Otherwise, the function places its result in memory at
8842 the location pointed to by GPR3.
8844 The SVR4 ABI specifies that structures <= 8 bytes are returned in r3/r4,
8845 but a draft put them in memory, and GCC used to implement the draft
8846 instead of the final standard. Therefore, aix_struct_return
8847 controls this instead of DEFAULT_ABI; V.4 targets needing backward
8848 compatibility can change DRAFT_V4_STRUCT_RET to override the
8849 default, and -m switches get the final word. See
8850 rs6000_option_override_internal for more details.
8852 The PPC32 SVR4 ABI uses IEEE double extended for long double, if 128-bit
8853 long double support is enabled. These values are returned in memory.
8855 int_size_in_bytes returns -1 for variable size objects, which go in
8856 memory always. The cast to unsigned makes -1 > 8. */
8858 static bool
8859 rs6000_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
8861 /* For the Darwin64 ABI, test if we can fit the return value in regs. */
8862 if (TARGET_MACHO
8863 && rs6000_darwin64_abi
8864 && TREE_CODE (type) == RECORD_TYPE
8865 && int_size_in_bytes (type) > 0)
8867 CUMULATIVE_ARGS valcum;
8868 rtx valret;
8870 valcum.words = 0;
8871 valcum.fregno = FP_ARG_MIN_REG;
8872 valcum.vregno = ALTIVEC_ARG_MIN_REG;
8873 /* Do a trial code generation as if this were going to be passed
8874 as an argument; if any part goes in memory, we return NULL. */
8875 valret = rs6000_darwin64_record_arg (&valcum, type, true, true);
8876 if (valret)
8877 return false;
8878 /* Otherwise fall through to more conventional ABI rules. */
8881 /* The ELFv2 ABI returns homogeneous VFP aggregates in registers */
8882 if (rs6000_discover_homogeneous_aggregate (TYPE_MODE (type), type,
8883 NULL, NULL))
8884 return false;
8886 /* The ELFv2 ABI returns aggregates up to 16B in registers */
8887 if (DEFAULT_ABI == ABI_ELFv2 && AGGREGATE_TYPE_P (type)
8888 && (unsigned HOST_WIDE_INT) int_size_in_bytes (type) <= 16)
8889 return false;
8891 if (AGGREGATE_TYPE_P (type)
8892 && (aix_struct_return
8893 || (unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 8))
8894 return true;
8896 /* Allow -maltivec -mabi=no-altivec without warning. Altivec vector
8897 modes only exist for GCC vector types if -maltivec. */
8898 if (TARGET_32BIT && !TARGET_ALTIVEC_ABI
8899 && ALTIVEC_VECTOR_MODE (TYPE_MODE (type)))
8900 return false;
8902 /* Return synthetic vectors in memory. */
8903 if (TREE_CODE (type) == VECTOR_TYPE
8904 && int_size_in_bytes (type) > (TARGET_ALTIVEC_ABI ? 16 : 8))
8906 static bool warned_for_return_big_vectors = false;
8907 if (!warned_for_return_big_vectors)
8909 warning (0, "GCC vector returned by reference: "
8910 "non-standard ABI extension with no compatibility guarantee");
8911 warned_for_return_big_vectors = true;
8913 return true;
8916 if (DEFAULT_ABI == ABI_V4 && TARGET_IEEEQUAD && TYPE_MODE (type) == TFmode)
8917 return true;
8919 return false;
8922 /* Specify whether values returned in registers should be at the most
8923 significant end of a register. We want aggregates returned by
8924 value to match the way aggregates are passed to functions. */
8926 static bool
8927 rs6000_return_in_msb (const_tree valtype)
8929 return (DEFAULT_ABI == ABI_ELFv2
8930 && BYTES_BIG_ENDIAN
8931 && AGGREGATE_TYPE_P (valtype)
8932 && FUNCTION_ARG_PADDING (TYPE_MODE (valtype), valtype) == upward);
8935 #ifdef HAVE_AS_GNU_ATTRIBUTE
8936 /* Return TRUE if a call to function FNDECL may be one that
8937 potentially affects the function calling ABI of the object file. */
8939 static bool
8940 call_ABI_of_interest (tree fndecl)
8942 if (cgraph_state == CGRAPH_STATE_EXPANSION)
8944 struct cgraph_node *c_node;
8946 /* Libcalls are always interesting. */
8947 if (fndecl == NULL_TREE)
8948 return true;
8950 /* Any call to an external function is interesting. */
8951 if (DECL_EXTERNAL (fndecl))
8952 return true;
8954 /* Interesting functions that we are emitting in this object file. */
8955 c_node = cgraph_get_node (fndecl);
8956 c_node = cgraph_function_or_thunk_node (c_node, NULL);
8957 return !cgraph_only_called_directly_p (c_node);
8959 return false;
8961 #endif
8963 /* Initialize a variable CUM of type CUMULATIVE_ARGS
8964 for a call to a function whose data type is FNTYPE.
8965 For a library call, FNTYPE is 0 and RETURN_MODE the return value mode.
8967 For incoming args we set the number of arguments in the prototype large
8968 so we never return a PARALLEL. */
8970 void
8971 init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype,
8972 rtx libname ATTRIBUTE_UNUSED, int incoming,
8973 int libcall, int n_named_args,
8974 tree fndecl ATTRIBUTE_UNUSED,
8975 enum machine_mode return_mode ATTRIBUTE_UNUSED)
8977 static CUMULATIVE_ARGS zero_cumulative;
8979 *cum = zero_cumulative;
8980 cum->words = 0;
8981 cum->fregno = FP_ARG_MIN_REG;
8982 cum->vregno = ALTIVEC_ARG_MIN_REG;
8983 cum->prototype = (fntype && prototype_p (fntype));
8984 cum->call_cookie = ((DEFAULT_ABI == ABI_V4 && libcall)
8985 ? CALL_LIBCALL : CALL_NORMAL);
8986 cum->sysv_gregno = GP_ARG_MIN_REG;
8987 cum->stdarg = stdarg_p (fntype);
8989 cum->nargs_prototype = 0;
8990 if (incoming || cum->prototype)
8991 cum->nargs_prototype = n_named_args;
8993 /* Check for a longcall attribute. */
8994 if ((!fntype && rs6000_default_long_calls)
8995 || (fntype
8996 && lookup_attribute ("longcall", TYPE_ATTRIBUTES (fntype))
8997 && !lookup_attribute ("shortcall", TYPE_ATTRIBUTES (fntype))))
8998 cum->call_cookie |= CALL_LONG;
9000 if (TARGET_DEBUG_ARG)
9002 fprintf (stderr, "\ninit_cumulative_args:");
9003 if (fntype)
9005 tree ret_type = TREE_TYPE (fntype);
9006 fprintf (stderr, " ret code = %s,",
9007 get_tree_code_name (TREE_CODE (ret_type)));
9010 if (cum->call_cookie & CALL_LONG)
9011 fprintf (stderr, " longcall,");
9013 fprintf (stderr, " proto = %d, nargs = %d\n",
9014 cum->prototype, cum->nargs_prototype);
9017 #ifdef HAVE_AS_GNU_ATTRIBUTE
9018 if (DEFAULT_ABI == ABI_V4)
9020 cum->escapes = call_ABI_of_interest (fndecl);
9021 if (cum->escapes)
9023 tree return_type;
9025 if (fntype)
9027 return_type = TREE_TYPE (fntype);
9028 return_mode = TYPE_MODE (return_type);
9030 else
9031 return_type = lang_hooks.types.type_for_mode (return_mode, 0);
9033 if (return_type != NULL)
9035 if (TREE_CODE (return_type) == RECORD_TYPE
9036 && TYPE_TRANSPARENT_AGGR (return_type))
9038 return_type = TREE_TYPE (first_field (return_type));
9039 return_mode = TYPE_MODE (return_type);
9041 if (AGGREGATE_TYPE_P (return_type)
9042 && ((unsigned HOST_WIDE_INT) int_size_in_bytes (return_type)
9043 <= 8))
9044 rs6000_returns_struct = true;
9046 if (SCALAR_FLOAT_MODE_P (return_mode))
9047 rs6000_passes_float = true;
9048 else if (ALTIVEC_OR_VSX_VECTOR_MODE (return_mode)
9049 || SPE_VECTOR_MODE (return_mode))
9050 rs6000_passes_vector = true;
9053 #endif
9055 if (fntype
9056 && !TARGET_ALTIVEC
9057 && TARGET_ALTIVEC_ABI
9058 && ALTIVEC_VECTOR_MODE (TYPE_MODE (TREE_TYPE (fntype))))
9060 error ("cannot return value in vector register because"
9061 " altivec instructions are disabled, use -maltivec"
9062 " to enable them");
9066 /* Return true if TYPE must be passed on the stack and not in registers. */
9068 static bool
9069 rs6000_must_pass_in_stack (enum machine_mode mode, const_tree type)
9071 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2 || TARGET_64BIT)
9072 return must_pass_in_stack_var_size (mode, type);
9073 else
9074 return must_pass_in_stack_var_size_or_pad (mode, type);
9077 /* If defined, a C expression which determines whether, and in which
9078 direction, to pad out an argument with extra space. The value
9079 should be of type `enum direction': either `upward' to pad above
9080 the argument, `downward' to pad below, or `none' to inhibit
9081 padding.
9083 For the AIX ABI structs are always stored left shifted in their
9084 argument slot. */
9086 enum direction
9087 function_arg_padding (enum machine_mode mode, const_tree type)
9089 #ifndef AGGREGATE_PADDING_FIXED
9090 #define AGGREGATE_PADDING_FIXED 0
9091 #endif
9092 #ifndef AGGREGATES_PAD_UPWARD_ALWAYS
9093 #define AGGREGATES_PAD_UPWARD_ALWAYS 0
9094 #endif
9096 if (!AGGREGATE_PADDING_FIXED)
9098 /* GCC used to pass structures of the same size as integer types as
9099 if they were in fact integers, ignoring FUNCTION_ARG_PADDING.
9100 i.e. Structures of size 1 or 2 (or 4 when TARGET_64BIT) were
9101 passed padded downward, except that -mstrict-align further
9102 muddied the water in that multi-component structures of 2 and 4
9103 bytes in size were passed padded upward.
9105 The following arranges for best compatibility with previous
9106 versions of gcc, but removes the -mstrict-align dependency. */
9107 if (BYTES_BIG_ENDIAN)
9109 HOST_WIDE_INT size = 0;
9111 if (mode == BLKmode)
9113 if (type && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
9114 size = int_size_in_bytes (type);
9116 else
9117 size = GET_MODE_SIZE (mode);
9119 if (size == 1 || size == 2 || size == 4)
9120 return downward;
9122 return upward;
9125 if (AGGREGATES_PAD_UPWARD_ALWAYS)
9127 if (type != 0 && AGGREGATE_TYPE_P (type))
9128 return upward;
9131 /* Fall back to the default. */
9132 return DEFAULT_FUNCTION_ARG_PADDING (mode, type);
9135 /* If defined, a C expression that gives the alignment boundary, in bits,
9136 of an argument with the specified mode and type. If it is not defined,
9137 PARM_BOUNDARY is used for all arguments.
9139 V.4 wants long longs and doubles to be double word aligned. Just
9140 testing the mode size is a boneheaded way to do this as it means
9141 that other types such as complex int are also double word aligned.
9142 However, we're stuck with this because changing the ABI might break
9143 existing library interfaces.
9145 Doubleword align SPE vectors.
9146 Quadword align Altivec/VSX vectors.
9147 Quadword align large synthetic vector types. */
9149 static unsigned int
9150 rs6000_function_arg_boundary (enum machine_mode mode, const_tree type)
9152 enum machine_mode elt_mode;
9153 int n_elts;
9155 rs6000_discover_homogeneous_aggregate (mode, type, &elt_mode, &n_elts);
9157 if (DEFAULT_ABI == ABI_V4
9158 && (GET_MODE_SIZE (mode) == 8
9159 || (TARGET_HARD_FLOAT
9160 && TARGET_FPRS
9161 && (mode == TFmode || mode == TDmode))))
9162 return 64;
9163 else if (SPE_VECTOR_MODE (mode)
9164 || (type && TREE_CODE (type) == VECTOR_TYPE
9165 && int_size_in_bytes (type) >= 8
9166 && int_size_in_bytes (type) < 16))
9167 return 64;
9168 else if (ALTIVEC_OR_VSX_VECTOR_MODE (elt_mode)
9169 || (type && TREE_CODE (type) == VECTOR_TYPE
9170 && int_size_in_bytes (type) >= 16))
9171 return 128;
9172 else if (((TARGET_MACHO && rs6000_darwin64_abi)
9173 || DEFAULT_ABI == ABI_ELFv2
9174 || (DEFAULT_ABI == ABI_AIX && !rs6000_compat_align_parm))
9175 && mode == BLKmode
9176 && type && TYPE_ALIGN (type) > 64)
9177 return 128;
9178 else
9179 return PARM_BOUNDARY;
9182 /* The offset in words to the start of the parameter save area. */
9184 static unsigned int
9185 rs6000_parm_offset (void)
9187 return (DEFAULT_ABI == ABI_V4 ? 2
9188 : DEFAULT_ABI == ABI_ELFv2 ? 4
9189 : 6);
9192 /* For a function parm of MODE and TYPE, return the starting word in
9193 the parameter area. NWORDS of the parameter area are already used. */
9195 static unsigned int
9196 rs6000_parm_start (enum machine_mode mode, const_tree type,
9197 unsigned int nwords)
9199 unsigned int align;
9201 align = rs6000_function_arg_boundary (mode, type) / PARM_BOUNDARY - 1;
9202 return nwords + (-(rs6000_parm_offset () + nwords) & align);
9205 /* Compute the size (in words) of a function argument. */
9207 static unsigned long
9208 rs6000_arg_size (enum machine_mode mode, const_tree type)
9210 unsigned long size;
9212 if (mode != BLKmode)
9213 size = GET_MODE_SIZE (mode);
9214 else
9215 size = int_size_in_bytes (type);
9217 if (TARGET_32BIT)
9218 return (size + 3) >> 2;
9219 else
9220 return (size + 7) >> 3;
9223 /* Use this to flush pending int fields. */
9225 static void
9226 rs6000_darwin64_record_arg_advance_flush (CUMULATIVE_ARGS *cum,
9227 HOST_WIDE_INT bitpos, int final)
9229 unsigned int startbit, endbit;
9230 int intregs, intoffset;
9231 enum machine_mode mode;
9233 /* Handle the situations where a float is taking up the first half
9234 of the GPR, and the other half is empty (typically due to
9235 alignment restrictions). We can detect this by a 8-byte-aligned
9236 int field, or by seeing that this is the final flush for this
9237 argument. Count the word and continue on. */
9238 if (cum->floats_in_gpr == 1
9239 && (cum->intoffset % 64 == 0
9240 || (cum->intoffset == -1 && final)))
9242 cum->words++;
9243 cum->floats_in_gpr = 0;
9246 if (cum->intoffset == -1)
9247 return;
9249 intoffset = cum->intoffset;
9250 cum->intoffset = -1;
9251 cum->floats_in_gpr = 0;
9253 if (intoffset % BITS_PER_WORD != 0)
9255 mode = mode_for_size (BITS_PER_WORD - intoffset % BITS_PER_WORD,
9256 MODE_INT, 0);
9257 if (mode == BLKmode)
9259 /* We couldn't find an appropriate mode, which happens,
9260 e.g., in packed structs when there are 3 bytes to load.
9261 Back intoffset back to the beginning of the word in this
9262 case. */
9263 intoffset = intoffset & -BITS_PER_WORD;
9267 startbit = intoffset & -BITS_PER_WORD;
9268 endbit = (bitpos + BITS_PER_WORD - 1) & -BITS_PER_WORD;
9269 intregs = (endbit - startbit) / BITS_PER_WORD;
9270 cum->words += intregs;
9271 /* words should be unsigned. */
9272 if ((unsigned)cum->words < (endbit/BITS_PER_WORD))
9274 int pad = (endbit/BITS_PER_WORD) - cum->words;
9275 cum->words += pad;
9279 /* The darwin64 ABI calls for us to recurse down through structs,
9280 looking for elements passed in registers. Unfortunately, we have
9281 to track int register count here also because of misalignments
9282 in powerpc alignment mode. */
9284 static void
9285 rs6000_darwin64_record_arg_advance_recurse (CUMULATIVE_ARGS *cum,
9286 const_tree type,
9287 HOST_WIDE_INT startbitpos)
9289 tree f;
9291 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
9292 if (TREE_CODE (f) == FIELD_DECL)
9294 HOST_WIDE_INT bitpos = startbitpos;
9295 tree ftype = TREE_TYPE (f);
9296 enum machine_mode mode;
9297 if (ftype == error_mark_node)
9298 continue;
9299 mode = TYPE_MODE (ftype);
9301 if (DECL_SIZE (f) != 0
9302 && tree_fits_uhwi_p (bit_position (f)))
9303 bitpos += int_bit_position (f);
9305 /* ??? FIXME: else assume zero offset. */
9307 if (TREE_CODE (ftype) == RECORD_TYPE)
9308 rs6000_darwin64_record_arg_advance_recurse (cum, ftype, bitpos);
9309 else if (USE_FP_FOR_ARG_P (cum, mode))
9311 unsigned n_fpregs = (GET_MODE_SIZE (mode) + 7) >> 3;
9312 rs6000_darwin64_record_arg_advance_flush (cum, bitpos, 0);
9313 cum->fregno += n_fpregs;
9314 /* Single-precision floats present a special problem for
9315 us, because they are smaller than an 8-byte GPR, and so
9316 the structure-packing rules combined with the standard
9317 varargs behavior mean that we want to pack float/float
9318 and float/int combinations into a single register's
9319 space. This is complicated by the arg advance flushing,
9320 which works on arbitrarily large groups of int-type
9321 fields. */
9322 if (mode == SFmode)
9324 if (cum->floats_in_gpr == 1)
9326 /* Two floats in a word; count the word and reset
9327 the float count. */
9328 cum->words++;
9329 cum->floats_in_gpr = 0;
9331 else if (bitpos % 64 == 0)
9333 /* A float at the beginning of an 8-byte word;
9334 count it and put off adjusting cum->words until
9335 we see if a arg advance flush is going to do it
9336 for us. */
9337 cum->floats_in_gpr++;
9339 else
9341 /* The float is at the end of a word, preceded
9342 by integer fields, so the arg advance flush
9343 just above has already set cum->words and
9344 everything is taken care of. */
9347 else
9348 cum->words += n_fpregs;
9350 else if (USE_ALTIVEC_FOR_ARG_P (cum, mode, 1))
9352 rs6000_darwin64_record_arg_advance_flush (cum, bitpos, 0);
9353 cum->vregno++;
9354 cum->words += 2;
9356 else if (cum->intoffset == -1)
9357 cum->intoffset = bitpos;
9361 /* Check for an item that needs to be considered specially under the darwin 64
9362 bit ABI. These are record types where the mode is BLK or the structure is
9363 8 bytes in size. */
9364 static int
9365 rs6000_darwin64_struct_check_p (enum machine_mode mode, const_tree type)
9367 return rs6000_darwin64_abi
9368 && ((mode == BLKmode
9369 && TREE_CODE (type) == RECORD_TYPE
9370 && int_size_in_bytes (type) > 0)
9371 || (type && TREE_CODE (type) == RECORD_TYPE
9372 && int_size_in_bytes (type) == 8)) ? 1 : 0;
9375 /* Update the data in CUM to advance over an argument
9376 of mode MODE and data type TYPE.
9377 (TYPE is null for libcalls where that information may not be available.)
9379 Note that for args passed by reference, function_arg will be called
9380 with MODE and TYPE set to that of the pointer to the arg, not the arg
9381 itself. */
9383 static void
9384 rs6000_function_arg_advance_1 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
9385 const_tree type, bool named, int depth)
9387 enum machine_mode elt_mode;
9388 int n_elts;
9390 rs6000_discover_homogeneous_aggregate (mode, type, &elt_mode, &n_elts);
9392 /* Only tick off an argument if we're not recursing. */
9393 if (depth == 0)
9394 cum->nargs_prototype--;
9396 #ifdef HAVE_AS_GNU_ATTRIBUTE
9397 if (DEFAULT_ABI == ABI_V4
9398 && cum->escapes)
9400 if (SCALAR_FLOAT_MODE_P (mode))
9401 rs6000_passes_float = true;
9402 else if (named && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
9403 rs6000_passes_vector = true;
9404 else if (SPE_VECTOR_MODE (mode)
9405 && !cum->stdarg
9406 && cum->sysv_gregno <= GP_ARG_MAX_REG)
9407 rs6000_passes_vector = true;
9409 #endif
9411 if (TARGET_ALTIVEC_ABI
9412 && (ALTIVEC_OR_VSX_VECTOR_MODE (elt_mode)
9413 || (type && TREE_CODE (type) == VECTOR_TYPE
9414 && int_size_in_bytes (type) == 16)))
9416 bool stack = false;
9418 if (USE_ALTIVEC_FOR_ARG_P (cum, elt_mode, named))
9420 cum->vregno += n_elts;
9422 if (!TARGET_ALTIVEC)
9423 error ("cannot pass argument in vector register because"
9424 " altivec instructions are disabled, use -maltivec"
9425 " to enable them");
9427 /* PowerPC64 Linux and AIX allocate GPRs for a vector argument
9428 even if it is going to be passed in a vector register.
9429 Darwin does the same for variable-argument functions. */
9430 if (((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
9431 && TARGET_64BIT)
9432 || (cum->stdarg && DEFAULT_ABI != ABI_V4))
9433 stack = true;
9435 else
9436 stack = true;
9438 if (stack)
9440 int align;
9442 /* Vector parameters must be 16-byte aligned. In 32-bit
9443 mode this means we need to take into account the offset
9444 to the parameter save area. In 64-bit mode, they just
9445 have to start on an even word, since the parameter save
9446 area is 16-byte aligned. */
9447 if (TARGET_32BIT)
9448 align = -(rs6000_parm_offset () + cum->words) & 3;
9449 else
9450 align = cum->words & 1;
9451 cum->words += align + rs6000_arg_size (mode, type);
9453 if (TARGET_DEBUG_ARG)
9455 fprintf (stderr, "function_adv: words = %2d, align=%d, ",
9456 cum->words, align);
9457 fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s\n",
9458 cum->nargs_prototype, cum->prototype,
9459 GET_MODE_NAME (mode));
9463 else if (TARGET_SPE_ABI && TARGET_SPE && SPE_VECTOR_MODE (mode)
9464 && !cum->stdarg
9465 && cum->sysv_gregno <= GP_ARG_MAX_REG)
9466 cum->sysv_gregno++;
9468 else if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
9470 int size = int_size_in_bytes (type);
9471 /* Variable sized types have size == -1 and are
9472 treated as if consisting entirely of ints.
9473 Pad to 16 byte boundary if needed. */
9474 if (TYPE_ALIGN (type) >= 2 * BITS_PER_WORD
9475 && (cum->words % 2) != 0)
9476 cum->words++;
9477 /* For varargs, we can just go up by the size of the struct. */
9478 if (!named)
9479 cum->words += (size + 7) / 8;
9480 else
9482 /* It is tempting to say int register count just goes up by
9483 sizeof(type)/8, but this is wrong in a case such as
9484 { int; double; int; } [powerpc alignment]. We have to
9485 grovel through the fields for these too. */
9486 cum->intoffset = 0;
9487 cum->floats_in_gpr = 0;
9488 rs6000_darwin64_record_arg_advance_recurse (cum, type, 0);
9489 rs6000_darwin64_record_arg_advance_flush (cum,
9490 size * BITS_PER_UNIT, 1);
9492 if (TARGET_DEBUG_ARG)
9494 fprintf (stderr, "function_adv: words = %2d, align=%d, size=%d",
9495 cum->words, TYPE_ALIGN (type), size);
9496 fprintf (stderr,
9497 "nargs = %4d, proto = %d, mode = %4s (darwin64 abi)\n",
9498 cum->nargs_prototype, cum->prototype,
9499 GET_MODE_NAME (mode));
9502 else if (DEFAULT_ABI == ABI_V4)
9504 if (TARGET_HARD_FLOAT && TARGET_FPRS
9505 && ((TARGET_SINGLE_FLOAT && mode == SFmode)
9506 || (TARGET_DOUBLE_FLOAT && mode == DFmode)
9507 || (mode == TFmode && !TARGET_IEEEQUAD)
9508 || mode == SDmode || mode == DDmode || mode == TDmode))
9510 /* _Decimal128 must use an even/odd register pair. This assumes
9511 that the register number is odd when fregno is odd. */
9512 if (mode == TDmode && (cum->fregno % 2) == 1)
9513 cum->fregno++;
9515 if (cum->fregno + (mode == TFmode || mode == TDmode ? 1 : 0)
9516 <= FP_ARG_V4_MAX_REG)
9517 cum->fregno += (GET_MODE_SIZE (mode) + 7) >> 3;
9518 else
9520 cum->fregno = FP_ARG_V4_MAX_REG + 1;
9521 if (mode == DFmode || mode == TFmode
9522 || mode == DDmode || mode == TDmode)
9523 cum->words += cum->words & 1;
9524 cum->words += rs6000_arg_size (mode, type);
9527 else
9529 int n_words = rs6000_arg_size (mode, type);
9530 int gregno = cum->sysv_gregno;
9532 /* Long long and SPE vectors are put in (r3,r4), (r5,r6),
9533 (r7,r8) or (r9,r10). As does any other 2 word item such
9534 as complex int due to a historical mistake. */
9535 if (n_words == 2)
9536 gregno += (1 - gregno) & 1;
9538 /* Multi-reg args are not split between registers and stack. */
9539 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
9541 /* Long long and SPE vectors are aligned on the stack.
9542 So are other 2 word items such as complex int due to
9543 a historical mistake. */
9544 if (n_words == 2)
9545 cum->words += cum->words & 1;
9546 cum->words += n_words;
9549 /* Note: continuing to accumulate gregno past when we've started
9550 spilling to the stack indicates the fact that we've started
9551 spilling to the stack to expand_builtin_saveregs. */
9552 cum->sysv_gregno = gregno + n_words;
9555 if (TARGET_DEBUG_ARG)
9557 fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
9558 cum->words, cum->fregno);
9559 fprintf (stderr, "gregno = %2d, nargs = %4d, proto = %d, ",
9560 cum->sysv_gregno, cum->nargs_prototype, cum->prototype);
9561 fprintf (stderr, "mode = %4s, named = %d\n",
9562 GET_MODE_NAME (mode), named);
9565 else
9567 int n_words = rs6000_arg_size (mode, type);
9568 int start_words = cum->words;
9569 int align_words = rs6000_parm_start (mode, type, start_words);
9571 cum->words = align_words + n_words;
9573 if (SCALAR_FLOAT_MODE_P (elt_mode)
9574 && TARGET_HARD_FLOAT && TARGET_FPRS)
9576 /* _Decimal128 must be passed in an even/odd float register pair.
9577 This assumes that the register number is odd when fregno is
9578 odd. */
9579 if (elt_mode == TDmode && (cum->fregno % 2) == 1)
9580 cum->fregno++;
9581 cum->fregno += n_elts * ((GET_MODE_SIZE (elt_mode) + 7) >> 3);
9584 if (TARGET_DEBUG_ARG)
9586 fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
9587 cum->words, cum->fregno);
9588 fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s, ",
9589 cum->nargs_prototype, cum->prototype, GET_MODE_NAME (mode));
9590 fprintf (stderr, "named = %d, align = %d, depth = %d\n",
9591 named, align_words - start_words, depth);
9596 static void
9597 rs6000_function_arg_advance (cumulative_args_t cum, enum machine_mode mode,
9598 const_tree type, bool named)
9600 rs6000_function_arg_advance_1 (get_cumulative_args (cum), mode, type, named,
9604 static rtx
9605 spe_build_register_parallel (enum machine_mode mode, int gregno)
9607 rtx r1, r3, r5, r7;
9609 switch (mode)
9611 case DFmode:
9612 r1 = gen_rtx_REG (DImode, gregno);
9613 r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
9614 return gen_rtx_PARALLEL (mode, gen_rtvec (1, r1));
9616 case DCmode:
9617 case TFmode:
9618 r1 = gen_rtx_REG (DImode, gregno);
9619 r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
9620 r3 = gen_rtx_REG (DImode, gregno + 2);
9621 r3 = gen_rtx_EXPR_LIST (VOIDmode, r3, GEN_INT (8));
9622 return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r3));
9624 case TCmode:
9625 r1 = gen_rtx_REG (DImode, gregno);
9626 r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
9627 r3 = gen_rtx_REG (DImode, gregno + 2);
9628 r3 = gen_rtx_EXPR_LIST (VOIDmode, r3, GEN_INT (8));
9629 r5 = gen_rtx_REG (DImode, gregno + 4);
9630 r5 = gen_rtx_EXPR_LIST (VOIDmode, r5, GEN_INT (16));
9631 r7 = gen_rtx_REG (DImode, gregno + 6);
9632 r7 = gen_rtx_EXPR_LIST (VOIDmode, r7, GEN_INT (24));
9633 return gen_rtx_PARALLEL (mode, gen_rtvec (4, r1, r3, r5, r7));
9635 default:
9636 gcc_unreachable ();
9640 /* Determine where to put a SIMD argument on the SPE. */
9641 static rtx
9642 rs6000_spe_function_arg (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
9643 const_tree type)
9645 int gregno = cum->sysv_gregno;
9647 /* On E500 v2, double arithmetic is done on the full 64-bit GPR, but
9648 are passed and returned in a pair of GPRs for ABI compatibility. */
9649 if (TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode
9650 || mode == DCmode || mode == TCmode))
9652 int n_words = rs6000_arg_size (mode, type);
9654 /* Doubles go in an odd/even register pair (r5/r6, etc). */
9655 if (mode == DFmode)
9656 gregno += (1 - gregno) & 1;
9658 /* Multi-reg args are not split between registers and stack. */
9659 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
9660 return NULL_RTX;
9662 return spe_build_register_parallel (mode, gregno);
9664 if (cum->stdarg)
9666 int n_words = rs6000_arg_size (mode, type);
9668 /* SPE vectors are put in odd registers. */
9669 if (n_words == 2 && (gregno & 1) == 0)
9670 gregno += 1;
9672 if (gregno + n_words - 1 <= GP_ARG_MAX_REG)
9674 rtx r1, r2;
9675 enum machine_mode m = SImode;
9677 r1 = gen_rtx_REG (m, gregno);
9678 r1 = gen_rtx_EXPR_LIST (m, r1, const0_rtx);
9679 r2 = gen_rtx_REG (m, gregno + 1);
9680 r2 = gen_rtx_EXPR_LIST (m, r2, GEN_INT (4));
9681 return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
9683 else
9684 return NULL_RTX;
9686 else
9688 if (gregno <= GP_ARG_MAX_REG)
9689 return gen_rtx_REG (mode, gregno);
9690 else
9691 return NULL_RTX;
9695 /* A subroutine of rs6000_darwin64_record_arg. Assign the bits of the
9696 structure between cum->intoffset and bitpos to integer registers. */
9698 static void
9699 rs6000_darwin64_record_arg_flush (CUMULATIVE_ARGS *cum,
9700 HOST_WIDE_INT bitpos, rtx rvec[], int *k)
9702 enum machine_mode mode;
9703 unsigned int regno;
9704 unsigned int startbit, endbit;
9705 int this_regno, intregs, intoffset;
9706 rtx reg;
9708 if (cum->intoffset == -1)
9709 return;
9711 intoffset = cum->intoffset;
9712 cum->intoffset = -1;
9714 /* If this is the trailing part of a word, try to only load that
9715 much into the register. Otherwise load the whole register. Note
9716 that in the latter case we may pick up unwanted bits. It's not a
9717 problem at the moment but may wish to revisit. */
9719 if (intoffset % BITS_PER_WORD != 0)
9721 mode = mode_for_size (BITS_PER_WORD - intoffset % BITS_PER_WORD,
9722 MODE_INT, 0);
9723 if (mode == BLKmode)
9725 /* We couldn't find an appropriate mode, which happens,
9726 e.g., in packed structs when there are 3 bytes to load.
9727 Back intoffset back to the beginning of the word in this
9728 case. */
9729 intoffset = intoffset & -BITS_PER_WORD;
9730 mode = word_mode;
9733 else
9734 mode = word_mode;
9736 startbit = intoffset & -BITS_PER_WORD;
9737 endbit = (bitpos + BITS_PER_WORD - 1) & -BITS_PER_WORD;
9738 intregs = (endbit - startbit) / BITS_PER_WORD;
9739 this_regno = cum->words + intoffset / BITS_PER_WORD;
9741 if (intregs > 0 && intregs > GP_ARG_NUM_REG - this_regno)
9742 cum->use_stack = 1;
9744 intregs = MIN (intregs, GP_ARG_NUM_REG - this_regno);
9745 if (intregs <= 0)
9746 return;
9748 intoffset /= BITS_PER_UNIT;
9751 regno = GP_ARG_MIN_REG + this_regno;
9752 reg = gen_rtx_REG (mode, regno);
9753 rvec[(*k)++] =
9754 gen_rtx_EXPR_LIST (VOIDmode, reg, GEN_INT (intoffset));
9756 this_regno += 1;
9757 intoffset = (intoffset | (UNITS_PER_WORD-1)) + 1;
9758 mode = word_mode;
9759 intregs -= 1;
9761 while (intregs > 0);
9764 /* Recursive workhorse for the following. */
9766 static void
9767 rs6000_darwin64_record_arg_recurse (CUMULATIVE_ARGS *cum, const_tree type,
9768 HOST_WIDE_INT startbitpos, rtx rvec[],
9769 int *k)
9771 tree f;
9773 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
9774 if (TREE_CODE (f) == FIELD_DECL)
9776 HOST_WIDE_INT bitpos = startbitpos;
9777 tree ftype = TREE_TYPE (f);
9778 enum machine_mode mode;
9779 if (ftype == error_mark_node)
9780 continue;
9781 mode = TYPE_MODE (ftype);
9783 if (DECL_SIZE (f) != 0
9784 && tree_fits_uhwi_p (bit_position (f)))
9785 bitpos += int_bit_position (f);
9787 /* ??? FIXME: else assume zero offset. */
9789 if (TREE_CODE (ftype) == RECORD_TYPE)
9790 rs6000_darwin64_record_arg_recurse (cum, ftype, bitpos, rvec, k);
9791 else if (cum->named && USE_FP_FOR_ARG_P (cum, mode))
9793 unsigned n_fpreg = (GET_MODE_SIZE (mode) + 7) >> 3;
9794 #if 0
9795 switch (mode)
9797 case SCmode: mode = SFmode; break;
9798 case DCmode: mode = DFmode; break;
9799 case TCmode: mode = TFmode; break;
9800 default: break;
9802 #endif
9803 rs6000_darwin64_record_arg_flush (cum, bitpos, rvec, k);
9804 if (cum->fregno + n_fpreg > FP_ARG_MAX_REG + 1)
9806 gcc_assert (cum->fregno == FP_ARG_MAX_REG
9807 && (mode == TFmode || mode == TDmode));
9808 /* Long double or _Decimal128 split over regs and memory. */
9809 mode = DECIMAL_FLOAT_MODE_P (mode) ? DDmode : DFmode;
9810 cum->use_stack=1;
9812 rvec[(*k)++]
9813 = gen_rtx_EXPR_LIST (VOIDmode,
9814 gen_rtx_REG (mode, cum->fregno++),
9815 GEN_INT (bitpos / BITS_PER_UNIT));
9816 if (mode == TFmode || mode == TDmode)
9817 cum->fregno++;
9819 else if (cum->named && USE_ALTIVEC_FOR_ARG_P (cum, mode, 1))
9821 rs6000_darwin64_record_arg_flush (cum, bitpos, rvec, k);
9822 rvec[(*k)++]
9823 = gen_rtx_EXPR_LIST (VOIDmode,
9824 gen_rtx_REG (mode, cum->vregno++),
9825 GEN_INT (bitpos / BITS_PER_UNIT));
9827 else if (cum->intoffset == -1)
9828 cum->intoffset = bitpos;
9832 /* For the darwin64 ABI, we want to construct a PARALLEL consisting of
9833 the register(s) to be used for each field and subfield of a struct
9834 being passed by value, along with the offset of where the
9835 register's value may be found in the block. FP fields go in FP
9836 register, vector fields go in vector registers, and everything
9837 else goes in int registers, packed as in memory.
9839 This code is also used for function return values. RETVAL indicates
9840 whether this is the case.
9842 Much of this is taken from the SPARC V9 port, which has a similar
9843 calling convention. */
9845 static rtx
9846 rs6000_darwin64_record_arg (CUMULATIVE_ARGS *orig_cum, const_tree type,
9847 bool named, bool retval)
9849 rtx rvec[FIRST_PSEUDO_REGISTER];
9850 int k = 1, kbase = 1;
9851 HOST_WIDE_INT typesize = int_size_in_bytes (type);
9852 /* This is a copy; modifications are not visible to our caller. */
9853 CUMULATIVE_ARGS copy_cum = *orig_cum;
9854 CUMULATIVE_ARGS *cum = &copy_cum;
9856 /* Pad to 16 byte boundary if needed. */
9857 if (!retval && TYPE_ALIGN (type) >= 2 * BITS_PER_WORD
9858 && (cum->words % 2) != 0)
9859 cum->words++;
9861 cum->intoffset = 0;
9862 cum->use_stack = 0;
9863 cum->named = named;
9865 /* Put entries into rvec[] for individual FP and vector fields, and
9866 for the chunks of memory that go in int regs. Note we start at
9867 element 1; 0 is reserved for an indication of using memory, and
9868 may or may not be filled in below. */
9869 rs6000_darwin64_record_arg_recurse (cum, type, /* startbit pos= */ 0, rvec, &k);
9870 rs6000_darwin64_record_arg_flush (cum, typesize * BITS_PER_UNIT, rvec, &k);
9872 /* If any part of the struct went on the stack put all of it there.
9873 This hack is because the generic code for
9874 FUNCTION_ARG_PARTIAL_NREGS cannot handle cases where the register
9875 parts of the struct are not at the beginning. */
9876 if (cum->use_stack)
9878 if (retval)
9879 return NULL_RTX; /* doesn't go in registers at all */
9880 kbase = 0;
9881 rvec[0] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
9883 if (k > 1 || cum->use_stack)
9884 return gen_rtx_PARALLEL (BLKmode, gen_rtvec_v (k - kbase, &rvec[kbase]));
9885 else
9886 return NULL_RTX;
9889 /* Determine where to place an argument in 64-bit mode with 32-bit ABI. */
9891 static rtx
9892 rs6000_mixed_function_arg (enum machine_mode mode, const_tree type,
9893 int align_words)
9895 int n_units;
9896 int i, k;
9897 rtx rvec[GP_ARG_NUM_REG + 1];
9899 if (align_words >= GP_ARG_NUM_REG)
9900 return NULL_RTX;
9902 n_units = rs6000_arg_size (mode, type);
9904 /* Optimize the simple case where the arg fits in one gpr, except in
9905 the case of BLKmode due to assign_parms assuming that registers are
9906 BITS_PER_WORD wide. */
9907 if (n_units == 0
9908 || (n_units == 1 && mode != BLKmode))
9909 return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
9911 k = 0;
9912 if (align_words + n_units > GP_ARG_NUM_REG)
9913 /* Not all of the arg fits in gprs. Say that it goes in memory too,
9914 using a magic NULL_RTX component.
9915 This is not strictly correct. Only some of the arg belongs in
9916 memory, not all of it. However, the normal scheme using
9917 function_arg_partial_nregs can result in unusual subregs, eg.
9918 (subreg:SI (reg:DF) 4), which are not handled well. The code to
9919 store the whole arg to memory is often more efficient than code
9920 to store pieces, and we know that space is available in the right
9921 place for the whole arg. */
9922 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
9924 i = 0;
9927 rtx r = gen_rtx_REG (SImode, GP_ARG_MIN_REG + align_words);
9928 rtx off = GEN_INT (i++ * 4);
9929 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
9931 while (++align_words < GP_ARG_NUM_REG && --n_units != 0);
9933 return gen_rtx_PARALLEL (mode, gen_rtvec_v (k, rvec));
9936 /* We have an argument of MODE and TYPE that goes into FPRs or VRs,
9937 but must also be copied into the parameter save area starting at
9938 offset ALIGN_WORDS. Fill in RVEC with the elements corresponding
9939 to the GPRs and/or memory. Return the number of elements used. */
9941 static int
9942 rs6000_psave_function_arg (enum machine_mode mode, const_tree type,
9943 int align_words, rtx *rvec)
9945 int k = 0;
9947 if (align_words < GP_ARG_NUM_REG)
9949 int n_words = rs6000_arg_size (mode, type);
9951 if (align_words + n_words > GP_ARG_NUM_REG
9952 || mode == BLKmode
9953 || (TARGET_32BIT && TARGET_POWERPC64))
9955 /* If this is partially on the stack, then we only
9956 include the portion actually in registers here. */
9957 enum machine_mode rmode = TARGET_32BIT ? SImode : DImode;
9958 int i = 0;
9960 if (align_words + n_words > GP_ARG_NUM_REG)
9962 /* Not all of the arg fits in gprs. Say that it goes in memory
9963 too, using a magic NULL_RTX component. Also see comment in
9964 rs6000_mixed_function_arg for why the normal
9965 function_arg_partial_nregs scheme doesn't work in this case. */
9966 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
9971 rtx r = gen_rtx_REG (rmode, GP_ARG_MIN_REG + align_words);
9972 rtx off = GEN_INT (i++ * GET_MODE_SIZE (rmode));
9973 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
9975 while (++align_words < GP_ARG_NUM_REG && --n_words != 0);
9977 else
9979 /* The whole arg fits in gprs. */
9980 rtx r = gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
9981 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, const0_rtx);
9984 else
9986 /* It's entirely in memory. */
9987 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
9990 return k;
9993 /* RVEC is a vector of K components of an argument of mode MODE.
9994 Construct the final function_arg return value from it. */
9996 static rtx
9997 rs6000_finish_function_arg (enum machine_mode mode, rtx *rvec, int k)
9999 gcc_assert (k >= 1);
10001 /* Avoid returning a PARALLEL in the trivial cases. */
10002 if (k == 1)
10004 if (XEXP (rvec[0], 0) == NULL_RTX)
10005 return NULL_RTX;
10007 if (GET_MODE (XEXP (rvec[0], 0)) == mode)
10008 return XEXP (rvec[0], 0);
10011 return gen_rtx_PARALLEL (mode, gen_rtvec_v (k, rvec));
10014 /* Determine where to put an argument to a function.
10015 Value is zero to push the argument on the stack,
10016 or a hard register in which to store the argument.
10018 MODE is the argument's machine mode.
10019 TYPE is the data type of the argument (as a tree).
10020 This is null for libcalls where that information may
10021 not be available.
10022 CUM is a variable of type CUMULATIVE_ARGS which gives info about
10023 the preceding args and about the function being called. It is
10024 not modified in this routine.
10025 NAMED is nonzero if this argument is a named parameter
10026 (otherwise it is an extra parameter matching an ellipsis).
10028 On RS/6000 the first eight words of non-FP are normally in registers
10029 and the rest are pushed. Under AIX, the first 13 FP args are in registers.
10030 Under V.4, the first 8 FP args are in registers.
10032 If this is floating-point and no prototype is specified, we use
10033 both an FP and integer register (or possibly FP reg and stack). Library
10034 functions (when CALL_LIBCALL is set) always have the proper types for args,
10035 so we can pass the FP value just in one register. emit_library_function
10036 doesn't support PARALLEL anyway.
10038 Note that for args passed by reference, function_arg will be called
10039 with MODE and TYPE set to that of the pointer to the arg, not the arg
10040 itself. */
10042 static rtx
10043 rs6000_function_arg (cumulative_args_t cum_v, enum machine_mode mode,
10044 const_tree type, bool named)
10046 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
10047 enum rs6000_abi abi = DEFAULT_ABI;
10048 enum machine_mode elt_mode;
10049 int n_elts;
10051 /* Return a marker to indicate whether CR1 needs to set or clear the
10052 bit that V.4 uses to say fp args were passed in registers.
10053 Assume that we don't need the marker for software floating point,
10054 or compiler generated library calls. */
10055 if (mode == VOIDmode)
10057 if (abi == ABI_V4
10058 && (cum->call_cookie & CALL_LIBCALL) == 0
10059 && (cum->stdarg
10060 || (cum->nargs_prototype < 0
10061 && (cum->prototype || TARGET_NO_PROTOTYPE))))
10063 /* For the SPE, we need to crxor CR6 always. */
10064 if (TARGET_SPE_ABI)
10065 return GEN_INT (cum->call_cookie | CALL_V4_SET_FP_ARGS);
10066 else if (TARGET_HARD_FLOAT && TARGET_FPRS)
10067 return GEN_INT (cum->call_cookie
10068 | ((cum->fregno == FP_ARG_MIN_REG)
10069 ? CALL_V4_SET_FP_ARGS
10070 : CALL_V4_CLEAR_FP_ARGS));
10073 return GEN_INT (cum->call_cookie & ~CALL_LIBCALL);
10076 rs6000_discover_homogeneous_aggregate (mode, type, &elt_mode, &n_elts);
10078 if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
10080 rtx rslt = rs6000_darwin64_record_arg (cum, type, named, /*retval= */false);
10081 if (rslt != NULL_RTX)
10082 return rslt;
10083 /* Else fall through to usual handling. */
10086 if (USE_ALTIVEC_FOR_ARG_P (cum, elt_mode, named))
10088 rtx rvec[GP_ARG_NUM_REG + AGGR_ARG_NUM_REG + 1];
10089 rtx r, off;
10090 int i, k = 0;
10092 /* Do we also need to pass this argument in the parameter
10093 save area? */
10094 if (TARGET_64BIT && ! cum->prototype)
10096 int align_words = (cum->words + 1) & ~1;
10097 k = rs6000_psave_function_arg (mode, type, align_words, rvec);
10100 /* Describe where this argument goes in the vector registers. */
10101 for (i = 0; i < n_elts && cum->vregno + i <= ALTIVEC_ARG_MAX_REG; i++)
10103 r = gen_rtx_REG (elt_mode, cum->vregno + i);
10104 off = GEN_INT (i * GET_MODE_SIZE (elt_mode));
10105 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
10108 return rs6000_finish_function_arg (mode, rvec, k);
10110 else if (TARGET_ALTIVEC_ABI
10111 && (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
10112 || (type && TREE_CODE (type) == VECTOR_TYPE
10113 && int_size_in_bytes (type) == 16)))
10115 if (named || abi == ABI_V4)
10116 return NULL_RTX;
10117 else
10119 /* Vector parameters to varargs functions under AIX or Darwin
10120 get passed in memory and possibly also in GPRs. */
10121 int align, align_words, n_words;
10122 enum machine_mode part_mode;
10124 /* Vector parameters must be 16-byte aligned. In 32-bit
10125 mode this means we need to take into account the offset
10126 to the parameter save area. In 64-bit mode, they just
10127 have to start on an even word, since the parameter save
10128 area is 16-byte aligned. */
10129 if (TARGET_32BIT)
10130 align = -(rs6000_parm_offset () + cum->words) & 3;
10131 else
10132 align = cum->words & 1;
10133 align_words = cum->words + align;
10135 /* Out of registers? Memory, then. */
10136 if (align_words >= GP_ARG_NUM_REG)
10137 return NULL_RTX;
10139 if (TARGET_32BIT && TARGET_POWERPC64)
10140 return rs6000_mixed_function_arg (mode, type, align_words);
10142 /* The vector value goes in GPRs. Only the part of the
10143 value in GPRs is reported here. */
10144 part_mode = mode;
10145 n_words = rs6000_arg_size (mode, type);
10146 if (align_words + n_words > GP_ARG_NUM_REG)
10147 /* Fortunately, there are only two possibilities, the value
10148 is either wholly in GPRs or half in GPRs and half not. */
10149 part_mode = DImode;
10151 return gen_rtx_REG (part_mode, GP_ARG_MIN_REG + align_words);
10154 else if (TARGET_SPE_ABI && TARGET_SPE
10155 && (SPE_VECTOR_MODE (mode)
10156 || (TARGET_E500_DOUBLE && (mode == DFmode
10157 || mode == DCmode
10158 || mode == TFmode
10159 || mode == TCmode))))
10160 return rs6000_spe_function_arg (cum, mode, type);
10162 else if (abi == ABI_V4)
10164 if (TARGET_HARD_FLOAT && TARGET_FPRS
10165 && ((TARGET_SINGLE_FLOAT && mode == SFmode)
10166 || (TARGET_DOUBLE_FLOAT && mode == DFmode)
10167 || (mode == TFmode && !TARGET_IEEEQUAD)
10168 || mode == SDmode || mode == DDmode || mode == TDmode))
10170 /* _Decimal128 must use an even/odd register pair. This assumes
10171 that the register number is odd when fregno is odd. */
10172 if (mode == TDmode && (cum->fregno % 2) == 1)
10173 cum->fregno++;
10175 if (cum->fregno + (mode == TFmode || mode == TDmode ? 1 : 0)
10176 <= FP_ARG_V4_MAX_REG)
10177 return gen_rtx_REG (mode, cum->fregno);
10178 else
10179 return NULL_RTX;
10181 else
10183 int n_words = rs6000_arg_size (mode, type);
10184 int gregno = cum->sysv_gregno;
10186 /* Long long and SPE vectors are put in (r3,r4), (r5,r6),
10187 (r7,r8) or (r9,r10). As does any other 2 word item such
10188 as complex int due to a historical mistake. */
10189 if (n_words == 2)
10190 gregno += (1 - gregno) & 1;
10192 /* Multi-reg args are not split between registers and stack. */
10193 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
10194 return NULL_RTX;
10196 if (TARGET_32BIT && TARGET_POWERPC64)
10197 return rs6000_mixed_function_arg (mode, type,
10198 gregno - GP_ARG_MIN_REG);
10199 return gen_rtx_REG (mode, gregno);
10202 else
10204 int align_words = rs6000_parm_start (mode, type, cum->words);
10206 /* _Decimal128 must be passed in an even/odd float register pair.
10207 This assumes that the register number is odd when fregno is odd. */
10208 if (elt_mode == TDmode && (cum->fregno % 2) == 1)
10209 cum->fregno++;
10211 if (USE_FP_FOR_ARG_P (cum, elt_mode))
10213 rtx rvec[GP_ARG_NUM_REG + AGGR_ARG_NUM_REG + 1];
10214 rtx r, off;
10215 int i, k = 0;
10216 unsigned long n_fpreg = (GET_MODE_SIZE (elt_mode) + 7) >> 3;
10218 /* Do we also need to pass this argument in the parameter
10219 save area? */
10220 if (type && (cum->nargs_prototype <= 0
10221 || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
10222 && TARGET_XL_COMPAT
10223 && align_words >= GP_ARG_NUM_REG)))
10224 k = rs6000_psave_function_arg (mode, type, align_words, rvec);
10226 /* Describe where this argument goes in the fprs. */
10227 for (i = 0; i < n_elts
10228 && cum->fregno + i * n_fpreg <= FP_ARG_MAX_REG; i++)
10230 /* Check if the argument is split over registers and memory.
10231 This can only ever happen for long double or _Decimal128;
10232 complex types are handled via split_complex_arg. */
10233 enum machine_mode fmode = elt_mode;
10234 if (cum->fregno + (i + 1) * n_fpreg > FP_ARG_MAX_REG + 1)
10236 gcc_assert (fmode == TFmode || fmode == TDmode);
10237 fmode = DECIMAL_FLOAT_MODE_P (fmode) ? DDmode : DFmode;
10240 r = gen_rtx_REG (fmode, cum->fregno + i * n_fpreg);
10241 off = GEN_INT (i * GET_MODE_SIZE (elt_mode));
10242 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
10245 return rs6000_finish_function_arg (mode, rvec, k);
10247 else if (align_words < GP_ARG_NUM_REG)
10249 if (TARGET_32BIT && TARGET_POWERPC64)
10250 return rs6000_mixed_function_arg (mode, type, align_words);
10252 return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
10254 else
10255 return NULL_RTX;
10259 /* For an arg passed partly in registers and partly in memory, this is
10260 the number of bytes passed in registers. For args passed entirely in
10261 registers or entirely in memory, zero. When an arg is described by a
10262 PARALLEL, perhaps using more than one register type, this function
10263 returns the number of bytes used by the first element of the PARALLEL. */
10265 static int
10266 rs6000_arg_partial_bytes (cumulative_args_t cum_v, enum machine_mode mode,
10267 tree type, bool named)
10269 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
10270 bool passed_in_gprs = true;
10271 int ret = 0;
10272 int align_words;
10273 enum machine_mode elt_mode;
10274 int n_elts;
10276 rs6000_discover_homogeneous_aggregate (mode, type, &elt_mode, &n_elts);
10278 if (DEFAULT_ABI == ABI_V4)
10279 return 0;
10281 if (USE_ALTIVEC_FOR_ARG_P (cum, elt_mode, named))
10283 /* If we are passing this arg in the fixed parameter save area
10284 (gprs or memory) as well as VRs, we do not use the partial
10285 bytes mechanism; instead, rs6000_function_arg will return a
10286 PARALLEL including a memory element as necessary. */
10287 if (TARGET_64BIT && ! cum->prototype)
10288 return 0;
10290 /* Otherwise, we pass in VRs only. Check for partial copies. */
10291 passed_in_gprs = false;
10292 if (cum->vregno + n_elts > ALTIVEC_ARG_MAX_REG + 1)
10293 ret = (ALTIVEC_ARG_MAX_REG + 1 - cum->vregno) * 16;
10296 /* In this complicated case we just disable the partial_nregs code. */
10297 if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
10298 return 0;
10300 align_words = rs6000_parm_start (mode, type, cum->words);
10302 if (USE_FP_FOR_ARG_P (cum, elt_mode))
10304 unsigned long n_fpreg = (GET_MODE_SIZE (elt_mode) + 7) >> 3;
10306 /* If we are passing this arg in the fixed parameter save area
10307 (gprs or memory) as well as FPRs, we do not use the partial
10308 bytes mechanism; instead, rs6000_function_arg will return a
10309 PARALLEL including a memory element as necessary. */
10310 if (type
10311 && (cum->nargs_prototype <= 0
10312 || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
10313 && TARGET_XL_COMPAT
10314 && align_words >= GP_ARG_NUM_REG)))
10315 return 0;
10317 /* Otherwise, we pass in FPRs only. Check for partial copies. */
10318 passed_in_gprs = false;
10319 if (cum->fregno + n_elts * n_fpreg > FP_ARG_MAX_REG + 1)
10320 ret = ((FP_ARG_MAX_REG + 1 - cum->fregno)
10321 * MIN (8, GET_MODE_SIZE (elt_mode)));
10324 if (passed_in_gprs
10325 && align_words < GP_ARG_NUM_REG
10326 && GP_ARG_NUM_REG < align_words + rs6000_arg_size (mode, type))
10327 ret = (GP_ARG_NUM_REG - align_words) * (TARGET_32BIT ? 4 : 8);
10329 if (ret != 0 && TARGET_DEBUG_ARG)
10330 fprintf (stderr, "rs6000_arg_partial_bytes: %d\n", ret);
10332 return ret;
10335 /* A C expression that indicates when an argument must be passed by
10336 reference. If nonzero for an argument, a copy of that argument is
10337 made in memory and a pointer to the argument is passed instead of
10338 the argument itself. The pointer is passed in whatever way is
10339 appropriate for passing a pointer to that type.
10341 Under V.4, aggregates and long double are passed by reference.
10343 As an extension to all 32-bit ABIs, AltiVec vectors are passed by
10344 reference unless the AltiVec vector extension ABI is in force.
10346 As an extension to all ABIs, variable sized types are passed by
10347 reference. */
10349 static bool
10350 rs6000_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED,
10351 enum machine_mode mode, const_tree type,
10352 bool named ATTRIBUTE_UNUSED)
10354 if (DEFAULT_ABI == ABI_V4 && TARGET_IEEEQUAD && mode == TFmode)
10356 if (TARGET_DEBUG_ARG)
10357 fprintf (stderr, "function_arg_pass_by_reference: V4 long double\n");
10358 return 1;
10361 if (!type)
10362 return 0;
10364 if (DEFAULT_ABI == ABI_V4 && AGGREGATE_TYPE_P (type))
10366 if (TARGET_DEBUG_ARG)
10367 fprintf (stderr, "function_arg_pass_by_reference: V4 aggregate\n");
10368 return 1;
10371 if (int_size_in_bytes (type) < 0)
10373 if (TARGET_DEBUG_ARG)
10374 fprintf (stderr, "function_arg_pass_by_reference: variable size\n");
10375 return 1;
10378 /* Allow -maltivec -mabi=no-altivec without warning. Altivec vector
10379 modes only exist for GCC vector types if -maltivec. */
10380 if (TARGET_32BIT && !TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
10382 if (TARGET_DEBUG_ARG)
10383 fprintf (stderr, "function_arg_pass_by_reference: AltiVec\n");
10384 return 1;
10387 /* Pass synthetic vectors in memory. */
10388 if (TREE_CODE (type) == VECTOR_TYPE
10389 && int_size_in_bytes (type) > (TARGET_ALTIVEC_ABI ? 16 : 8))
10391 static bool warned_for_pass_big_vectors = false;
10392 if (TARGET_DEBUG_ARG)
10393 fprintf (stderr, "function_arg_pass_by_reference: synthetic vector\n");
10394 if (!warned_for_pass_big_vectors)
10396 warning (0, "GCC vector passed by reference: "
10397 "non-standard ABI extension with no compatibility guarantee");
10398 warned_for_pass_big_vectors = true;
10400 return 1;
10403 return 0;
10406 /* Process parameter of type TYPE after ARGS_SO_FAR parameters were
10407 already processes. Return true if the parameter must be passed
10408 (fully or partially) on the stack. */
10410 static bool
10411 rs6000_parm_needs_stack (cumulative_args_t args_so_far, tree type)
10413 enum machine_mode mode;
10414 int unsignedp;
10415 rtx entry_parm;
10417 /* Catch errors. */
10418 if (type == NULL || type == error_mark_node)
10419 return true;
10421 /* Handle types with no storage requirement. */
10422 if (TYPE_MODE (type) == VOIDmode)
10423 return false;
10425 /* Handle complex types. */
10426 if (TREE_CODE (type) == COMPLEX_TYPE)
10427 return (rs6000_parm_needs_stack (args_so_far, TREE_TYPE (type))
10428 || rs6000_parm_needs_stack (args_so_far, TREE_TYPE (type)));
10430 /* Handle transparent aggregates. */
10431 if ((TREE_CODE (type) == UNION_TYPE || TREE_CODE (type) == RECORD_TYPE)
10432 && TYPE_TRANSPARENT_AGGR (type))
10433 type = TREE_TYPE (first_field (type));
10435 /* See if this arg was passed by invisible reference. */
10436 if (pass_by_reference (get_cumulative_args (args_so_far),
10437 TYPE_MODE (type), type, true))
10438 type = build_pointer_type (type);
10440 /* Find mode as it is passed by the ABI. */
10441 unsignedp = TYPE_UNSIGNED (type);
10442 mode = promote_mode (type, TYPE_MODE (type), &unsignedp);
10444 /* If we must pass in stack, we need a stack. */
10445 if (rs6000_must_pass_in_stack (mode, type))
10446 return true;
10448 /* If there is no incoming register, we need a stack. */
10449 entry_parm = rs6000_function_arg (args_so_far, mode, type, true);
10450 if (entry_parm == NULL)
10451 return true;
10453 /* Likewise if we need to pass both in registers and on the stack. */
10454 if (GET_CODE (entry_parm) == PARALLEL
10455 && XEXP (XVECEXP (entry_parm, 0, 0), 0) == NULL_RTX)
10456 return true;
10458 /* Also true if we're partially in registers and partially not. */
10459 if (rs6000_arg_partial_bytes (args_so_far, mode, type, true) != 0)
10460 return true;
10462 /* Update info on where next arg arrives in registers. */
10463 rs6000_function_arg_advance (args_so_far, mode, type, true);
10464 return false;
10467 /* Return true if FUN has no prototype, has a variable argument
10468 list, or passes any parameter in memory. */
10470 static bool
10471 rs6000_function_parms_need_stack (tree fun)
10473 function_args_iterator args_iter;
10474 tree arg_type;
10475 CUMULATIVE_ARGS args_so_far_v;
10476 cumulative_args_t args_so_far;
10478 if (!fun)
10479 /* Must be a libcall, all of which only use reg parms. */
10480 return false;
10481 if (!TYPE_P (fun))
10482 fun = TREE_TYPE (fun);
10484 /* Varargs functions need the parameter save area. */
10485 if (!prototype_p (fun) || stdarg_p (fun))
10486 return true;
10488 INIT_CUMULATIVE_INCOMING_ARGS (args_so_far_v, fun, NULL_RTX);
10489 args_so_far = pack_cumulative_args (&args_so_far_v);
10491 if (aggregate_value_p (TREE_TYPE (fun), fun))
10493 tree type = build_pointer_type (TREE_TYPE (fun));
10494 rs6000_parm_needs_stack (args_so_far, type);
10497 FOREACH_FUNCTION_ARGS (fun, arg_type, args_iter)
10498 if (rs6000_parm_needs_stack (args_so_far, arg_type))
10499 return true;
10501 return false;
10504 /* Return the size of the REG_PARM_STACK_SPACE are for FUN. This is
10505 usually a constant depending on the ABI. However, in the ELFv2 ABI
10506 the register parameter area is optional when calling a function that
10507 has a prototype is scope, has no variable argument list, and passes
10508 all parameters in registers. */
10511 rs6000_reg_parm_stack_space (tree fun)
10513 int reg_parm_stack_space;
10515 switch (DEFAULT_ABI)
10517 default:
10518 reg_parm_stack_space = 0;
10519 break;
10521 case ABI_AIX:
10522 case ABI_DARWIN:
10523 reg_parm_stack_space = TARGET_64BIT ? 64 : 32;
10524 break;
10526 case ABI_ELFv2:
10527 /* ??? Recomputing this every time is a bit expensive. Is there
10528 a place to cache this information? */
10529 if (rs6000_function_parms_need_stack (fun))
10530 reg_parm_stack_space = TARGET_64BIT ? 64 : 32;
10531 else
10532 reg_parm_stack_space = 0;
10533 break;
10536 return reg_parm_stack_space;
10539 static void
10540 rs6000_move_block_from_reg (int regno, rtx x, int nregs)
10542 int i;
10543 enum machine_mode reg_mode = TARGET_32BIT ? SImode : DImode;
10545 if (nregs == 0)
10546 return;
10548 for (i = 0; i < nregs; i++)
10550 rtx tem = adjust_address_nv (x, reg_mode, i * GET_MODE_SIZE (reg_mode));
10551 if (reload_completed)
10553 if (! strict_memory_address_p (reg_mode, XEXP (tem, 0)))
10554 tem = NULL_RTX;
10555 else
10556 tem = simplify_gen_subreg (reg_mode, x, BLKmode,
10557 i * GET_MODE_SIZE (reg_mode));
10559 else
10560 tem = replace_equiv_address (tem, XEXP (tem, 0));
10562 gcc_assert (tem);
10564 emit_move_insn (tem, gen_rtx_REG (reg_mode, regno + i));
10568 /* Perform any needed actions needed for a function that is receiving a
10569 variable number of arguments.
10571 CUM is as above.
10573 MODE and TYPE are the mode and type of the current parameter.
10575 PRETEND_SIZE is a variable that should be set to the amount of stack
10576 that must be pushed by the prolog to pretend that our caller pushed
10579 Normally, this macro will push all remaining incoming registers on the
10580 stack and set PRETEND_SIZE to the length of the registers pushed. */
10582 static void
10583 setup_incoming_varargs (cumulative_args_t cum, enum machine_mode mode,
10584 tree type, int *pretend_size ATTRIBUTE_UNUSED,
10585 int no_rtl)
10587 CUMULATIVE_ARGS next_cum;
10588 int reg_size = TARGET_32BIT ? 4 : 8;
10589 rtx save_area = NULL_RTX, mem;
10590 int first_reg_offset;
10591 alias_set_type set;
10593 /* Skip the last named argument. */
10594 next_cum = *get_cumulative_args (cum);
10595 rs6000_function_arg_advance_1 (&next_cum, mode, type, true, 0);
10597 if (DEFAULT_ABI == ABI_V4)
10599 first_reg_offset = next_cum.sysv_gregno - GP_ARG_MIN_REG;
10601 if (! no_rtl)
10603 int gpr_reg_num = 0, gpr_size = 0, fpr_size = 0;
10604 HOST_WIDE_INT offset = 0;
10606 /* Try to optimize the size of the varargs save area.
10607 The ABI requires that ap.reg_save_area is doubleword
10608 aligned, but we don't need to allocate space for all
10609 the bytes, only those to which we actually will save
10610 anything. */
10611 if (cfun->va_list_gpr_size && first_reg_offset < GP_ARG_NUM_REG)
10612 gpr_reg_num = GP_ARG_NUM_REG - first_reg_offset;
10613 if (TARGET_HARD_FLOAT && TARGET_FPRS
10614 && next_cum.fregno <= FP_ARG_V4_MAX_REG
10615 && cfun->va_list_fpr_size)
10617 if (gpr_reg_num)
10618 fpr_size = (next_cum.fregno - FP_ARG_MIN_REG)
10619 * UNITS_PER_FP_WORD;
10620 if (cfun->va_list_fpr_size
10621 < FP_ARG_V4_MAX_REG + 1 - next_cum.fregno)
10622 fpr_size += cfun->va_list_fpr_size * UNITS_PER_FP_WORD;
10623 else
10624 fpr_size += (FP_ARG_V4_MAX_REG + 1 - next_cum.fregno)
10625 * UNITS_PER_FP_WORD;
10627 if (gpr_reg_num)
10629 offset = -((first_reg_offset * reg_size) & ~7);
10630 if (!fpr_size && gpr_reg_num > cfun->va_list_gpr_size)
10632 gpr_reg_num = cfun->va_list_gpr_size;
10633 if (reg_size == 4 && (first_reg_offset & 1))
10634 gpr_reg_num++;
10636 gpr_size = (gpr_reg_num * reg_size + 7) & ~7;
10638 else if (fpr_size)
10639 offset = - (int) (next_cum.fregno - FP_ARG_MIN_REG)
10640 * UNITS_PER_FP_WORD
10641 - (int) (GP_ARG_NUM_REG * reg_size);
10643 if (gpr_size + fpr_size)
10645 rtx reg_save_area
10646 = assign_stack_local (BLKmode, gpr_size + fpr_size, 64);
10647 gcc_assert (GET_CODE (reg_save_area) == MEM);
10648 reg_save_area = XEXP (reg_save_area, 0);
10649 if (GET_CODE (reg_save_area) == PLUS)
10651 gcc_assert (XEXP (reg_save_area, 0)
10652 == virtual_stack_vars_rtx);
10653 gcc_assert (GET_CODE (XEXP (reg_save_area, 1)) == CONST_INT);
10654 offset += INTVAL (XEXP (reg_save_area, 1));
10656 else
10657 gcc_assert (reg_save_area == virtual_stack_vars_rtx);
10660 cfun->machine->varargs_save_offset = offset;
10661 save_area = plus_constant (Pmode, virtual_stack_vars_rtx, offset);
10664 else
10666 first_reg_offset = next_cum.words;
10667 save_area = virtual_incoming_args_rtx;
10669 if (targetm.calls.must_pass_in_stack (mode, type))
10670 first_reg_offset += rs6000_arg_size (TYPE_MODE (type), type);
10673 set = get_varargs_alias_set ();
10674 if (! no_rtl && first_reg_offset < GP_ARG_NUM_REG
10675 && cfun->va_list_gpr_size)
10677 int n_gpr, nregs = GP_ARG_NUM_REG - first_reg_offset;
10679 if (va_list_gpr_counter_field)
10680 /* V4 va_list_gpr_size counts number of registers needed. */
10681 n_gpr = cfun->va_list_gpr_size;
10682 else
10683 /* char * va_list instead counts number of bytes needed. */
10684 n_gpr = (cfun->va_list_gpr_size + reg_size - 1) / reg_size;
10686 if (nregs > n_gpr)
10687 nregs = n_gpr;
10689 mem = gen_rtx_MEM (BLKmode,
10690 plus_constant (Pmode, save_area,
10691 first_reg_offset * reg_size));
10692 MEM_NOTRAP_P (mem) = 1;
10693 set_mem_alias_set (mem, set);
10694 set_mem_align (mem, BITS_PER_WORD);
10696 rs6000_move_block_from_reg (GP_ARG_MIN_REG + first_reg_offset, mem,
10697 nregs);
10700 /* Save FP registers if needed. */
10701 if (DEFAULT_ABI == ABI_V4
10702 && TARGET_HARD_FLOAT && TARGET_FPRS
10703 && ! no_rtl
10704 && next_cum.fregno <= FP_ARG_V4_MAX_REG
10705 && cfun->va_list_fpr_size)
10707 int fregno = next_cum.fregno, nregs;
10708 rtx cr1 = gen_rtx_REG (CCmode, CR1_REGNO);
10709 rtx lab = gen_label_rtx ();
10710 int off = (GP_ARG_NUM_REG * reg_size) + ((fregno - FP_ARG_MIN_REG)
10711 * UNITS_PER_FP_WORD);
10713 emit_jump_insn
10714 (gen_rtx_SET (VOIDmode,
10715 pc_rtx,
10716 gen_rtx_IF_THEN_ELSE (VOIDmode,
10717 gen_rtx_NE (VOIDmode, cr1,
10718 const0_rtx),
10719 gen_rtx_LABEL_REF (VOIDmode, lab),
10720 pc_rtx)));
10722 for (nregs = 0;
10723 fregno <= FP_ARG_V4_MAX_REG && nregs < cfun->va_list_fpr_size;
10724 fregno++, off += UNITS_PER_FP_WORD, nregs++)
10726 mem = gen_rtx_MEM ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
10727 ? DFmode : SFmode,
10728 plus_constant (Pmode, save_area, off));
10729 MEM_NOTRAP_P (mem) = 1;
10730 set_mem_alias_set (mem, set);
10731 set_mem_align (mem, GET_MODE_ALIGNMENT (
10732 (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
10733 ? DFmode : SFmode));
10734 emit_move_insn (mem, gen_rtx_REG (
10735 (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
10736 ? DFmode : SFmode, fregno));
10739 emit_label (lab);
10743 /* Create the va_list data type. */
10745 static tree
10746 rs6000_build_builtin_va_list (void)
10748 tree f_gpr, f_fpr, f_res, f_ovf, f_sav, record, type_decl;
10750 /* For AIX, prefer 'char *' because that's what the system
10751 header files like. */
10752 if (DEFAULT_ABI != ABI_V4)
10753 return build_pointer_type (char_type_node);
10755 record = (*lang_hooks.types.make_type) (RECORD_TYPE);
10756 type_decl = build_decl (BUILTINS_LOCATION, TYPE_DECL,
10757 get_identifier ("__va_list_tag"), record);
10759 f_gpr = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("gpr"),
10760 unsigned_char_type_node);
10761 f_fpr = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("fpr"),
10762 unsigned_char_type_node);
10763 /* Give the two bytes of padding a name, so that -Wpadded won't warn on
10764 every user file. */
10765 f_res = build_decl (BUILTINS_LOCATION, FIELD_DECL,
10766 get_identifier ("reserved"), short_unsigned_type_node);
10767 f_ovf = build_decl (BUILTINS_LOCATION, FIELD_DECL,
10768 get_identifier ("overflow_arg_area"),
10769 ptr_type_node);
10770 f_sav = build_decl (BUILTINS_LOCATION, FIELD_DECL,
10771 get_identifier ("reg_save_area"),
10772 ptr_type_node);
10774 va_list_gpr_counter_field = f_gpr;
10775 va_list_fpr_counter_field = f_fpr;
10777 DECL_FIELD_CONTEXT (f_gpr) = record;
10778 DECL_FIELD_CONTEXT (f_fpr) = record;
10779 DECL_FIELD_CONTEXT (f_res) = record;
10780 DECL_FIELD_CONTEXT (f_ovf) = record;
10781 DECL_FIELD_CONTEXT (f_sav) = record;
10783 TYPE_STUB_DECL (record) = type_decl;
10784 TYPE_NAME (record) = type_decl;
10785 TYPE_FIELDS (record) = f_gpr;
10786 DECL_CHAIN (f_gpr) = f_fpr;
10787 DECL_CHAIN (f_fpr) = f_res;
10788 DECL_CHAIN (f_res) = f_ovf;
10789 DECL_CHAIN (f_ovf) = f_sav;
10791 layout_type (record);
10793 /* The correct type is an array type of one element. */
10794 return build_array_type (record, build_index_type (size_zero_node));
10797 /* Implement va_start. */
10799 static void
10800 rs6000_va_start (tree valist, rtx nextarg)
10802 HOST_WIDE_INT words, n_gpr, n_fpr;
10803 tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
10804 tree gpr, fpr, ovf, sav, t;
10806 /* Only SVR4 needs something special. */
10807 if (DEFAULT_ABI != ABI_V4)
10809 std_expand_builtin_va_start (valist, nextarg);
10810 return;
10813 f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
10814 f_fpr = DECL_CHAIN (f_gpr);
10815 f_res = DECL_CHAIN (f_fpr);
10816 f_ovf = DECL_CHAIN (f_res);
10817 f_sav = DECL_CHAIN (f_ovf);
10819 valist = build_simple_mem_ref (valist);
10820 gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
10821 fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
10822 f_fpr, NULL_TREE);
10823 ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
10824 f_ovf, NULL_TREE);
10825 sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
10826 f_sav, NULL_TREE);
10828 /* Count number of gp and fp argument registers used. */
10829 words = crtl->args.info.words;
10830 n_gpr = MIN (crtl->args.info.sysv_gregno - GP_ARG_MIN_REG,
10831 GP_ARG_NUM_REG);
10832 n_fpr = MIN (crtl->args.info.fregno - FP_ARG_MIN_REG,
10833 FP_ARG_NUM_REG);
10835 if (TARGET_DEBUG_ARG)
10836 fprintf (stderr, "va_start: words = "HOST_WIDE_INT_PRINT_DEC", n_gpr = "
10837 HOST_WIDE_INT_PRINT_DEC", n_fpr = "HOST_WIDE_INT_PRINT_DEC"\n",
10838 words, n_gpr, n_fpr);
10840 if (cfun->va_list_gpr_size)
10842 t = build2 (MODIFY_EXPR, TREE_TYPE (gpr), gpr,
10843 build_int_cst (NULL_TREE, n_gpr));
10844 TREE_SIDE_EFFECTS (t) = 1;
10845 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
10848 if (cfun->va_list_fpr_size)
10850 t = build2 (MODIFY_EXPR, TREE_TYPE (fpr), fpr,
10851 build_int_cst (NULL_TREE, n_fpr));
10852 TREE_SIDE_EFFECTS (t) = 1;
10853 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
10855 #ifdef HAVE_AS_GNU_ATTRIBUTE
10856 if (call_ABI_of_interest (cfun->decl))
10857 rs6000_passes_float = true;
10858 #endif
10861 /* Find the overflow area. */
10862 t = make_tree (TREE_TYPE (ovf), virtual_incoming_args_rtx);
10863 if (words != 0)
10864 t = fold_build_pointer_plus_hwi (t, words * UNITS_PER_WORD);
10865 t = build2 (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
10866 TREE_SIDE_EFFECTS (t) = 1;
10867 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
10869 /* If there were no va_arg invocations, don't set up the register
10870 save area. */
10871 if (!cfun->va_list_gpr_size
10872 && !cfun->va_list_fpr_size
10873 && n_gpr < GP_ARG_NUM_REG
10874 && n_fpr < FP_ARG_V4_MAX_REG)
10875 return;
10877 /* Find the register save area. */
10878 t = make_tree (TREE_TYPE (sav), virtual_stack_vars_rtx);
10879 if (cfun->machine->varargs_save_offset)
10880 t = fold_build_pointer_plus_hwi (t, cfun->machine->varargs_save_offset);
10881 t = build2 (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
10882 TREE_SIDE_EFFECTS (t) = 1;
10883 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
10886 /* Implement va_arg. */
10888 static tree
10889 rs6000_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p,
10890 gimple_seq *post_p)
10892 tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
10893 tree gpr, fpr, ovf, sav, reg, t, u;
10894 int size, rsize, n_reg, sav_ofs, sav_scale;
10895 tree lab_false, lab_over, addr;
10896 int align;
10897 tree ptrtype = build_pointer_type_for_mode (type, ptr_mode, true);
10898 int regalign = 0;
10899 gimple stmt;
10901 if (pass_by_reference (NULL, TYPE_MODE (type), type, false))
10903 t = rs6000_gimplify_va_arg (valist, ptrtype, pre_p, post_p);
10904 return build_va_arg_indirect_ref (t);
10907 /* We need to deal with the fact that the darwin ppc64 ABI is defined by an
10908 earlier version of gcc, with the property that it always applied alignment
10909 adjustments to the va-args (even for zero-sized types). The cheapest way
10910 to deal with this is to replicate the effect of the part of
10911 std_gimplify_va_arg_expr that carries out the align adjust, for the case
10912 of relevance.
10913 We don't need to check for pass-by-reference because of the test above.
10914 We can return a simplifed answer, since we know there's no offset to add. */
10916 if (((TARGET_MACHO
10917 && rs6000_darwin64_abi)
10918 || DEFAULT_ABI == ABI_ELFv2
10919 || (DEFAULT_ABI == ABI_AIX && !rs6000_compat_align_parm))
10920 && integer_zerop (TYPE_SIZE (type)))
10922 unsigned HOST_WIDE_INT align, boundary;
10923 tree valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
10924 align = PARM_BOUNDARY / BITS_PER_UNIT;
10925 boundary = rs6000_function_arg_boundary (TYPE_MODE (type), type);
10926 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
10927 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
10928 boundary /= BITS_PER_UNIT;
10929 if (boundary > align)
10931 tree t ;
10932 /* This updates arg ptr by the amount that would be necessary
10933 to align the zero-sized (but not zero-alignment) item. */
10934 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
10935 fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
10936 gimplify_and_add (t, pre_p);
10938 t = fold_convert (sizetype, valist_tmp);
10939 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
10940 fold_convert (TREE_TYPE (valist),
10941 fold_build2 (BIT_AND_EXPR, sizetype, t,
10942 size_int (-boundary))));
10943 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
10944 gimplify_and_add (t, pre_p);
10946 /* Since it is zero-sized there's no increment for the item itself. */
10947 valist_tmp = fold_convert (build_pointer_type (type), valist_tmp);
10948 return build_va_arg_indirect_ref (valist_tmp);
10951 if (DEFAULT_ABI != ABI_V4)
10953 if (targetm.calls.split_complex_arg && TREE_CODE (type) == COMPLEX_TYPE)
10955 tree elem_type = TREE_TYPE (type);
10956 enum machine_mode elem_mode = TYPE_MODE (elem_type);
10957 int elem_size = GET_MODE_SIZE (elem_mode);
10959 if (elem_size < UNITS_PER_WORD)
10961 tree real_part, imag_part;
10962 gimple_seq post = NULL;
10964 real_part = rs6000_gimplify_va_arg (valist, elem_type, pre_p,
10965 &post);
10966 /* Copy the value into a temporary, lest the formal temporary
10967 be reused out from under us. */
10968 real_part = get_initialized_tmp_var (real_part, pre_p, &post);
10969 gimple_seq_add_seq (pre_p, post);
10971 imag_part = rs6000_gimplify_va_arg (valist, elem_type, pre_p,
10972 post_p);
10974 return build2 (COMPLEX_EXPR, type, real_part, imag_part);
10978 return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
10981 f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
10982 f_fpr = DECL_CHAIN (f_gpr);
10983 f_res = DECL_CHAIN (f_fpr);
10984 f_ovf = DECL_CHAIN (f_res);
10985 f_sav = DECL_CHAIN (f_ovf);
10987 valist = build_va_arg_indirect_ref (valist);
10988 gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
10989 fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
10990 f_fpr, NULL_TREE);
10991 ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
10992 f_ovf, NULL_TREE);
10993 sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
10994 f_sav, NULL_TREE);
10996 size = int_size_in_bytes (type);
10997 rsize = (size + 3) / 4;
10998 align = 1;
11000 if (TARGET_HARD_FLOAT && TARGET_FPRS
11001 && ((TARGET_SINGLE_FLOAT && TYPE_MODE (type) == SFmode)
11002 || (TARGET_DOUBLE_FLOAT
11003 && (TYPE_MODE (type) == DFmode
11004 || TYPE_MODE (type) == TFmode
11005 || TYPE_MODE (type) == SDmode
11006 || TYPE_MODE (type) == DDmode
11007 || TYPE_MODE (type) == TDmode))))
11009 /* FP args go in FP registers, if present. */
11010 reg = fpr;
11011 n_reg = (size + 7) / 8;
11012 sav_ofs = ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? 8 : 4) * 4;
11013 sav_scale = ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? 8 : 4);
11014 if (TYPE_MODE (type) != SFmode && TYPE_MODE (type) != SDmode)
11015 align = 8;
11017 else
11019 /* Otherwise into GP registers. */
11020 reg = gpr;
11021 n_reg = rsize;
11022 sav_ofs = 0;
11023 sav_scale = 4;
11024 if (n_reg == 2)
11025 align = 8;
11028 /* Pull the value out of the saved registers.... */
11030 lab_over = NULL;
11031 addr = create_tmp_var (ptr_type_node, "addr");
11033 /* AltiVec vectors never go in registers when -mabi=altivec. */
11034 if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (TYPE_MODE (type)))
11035 align = 16;
11036 else
11038 lab_false = create_artificial_label (input_location);
11039 lab_over = create_artificial_label (input_location);
11041 /* Long long and SPE vectors are aligned in the registers.
11042 As are any other 2 gpr item such as complex int due to a
11043 historical mistake. */
11044 u = reg;
11045 if (n_reg == 2 && reg == gpr)
11047 regalign = 1;
11048 u = build2 (BIT_AND_EXPR, TREE_TYPE (reg), unshare_expr (reg),
11049 build_int_cst (TREE_TYPE (reg), n_reg - 1));
11050 u = build2 (POSTINCREMENT_EXPR, TREE_TYPE (reg),
11051 unshare_expr (reg), u);
11053 /* _Decimal128 is passed in even/odd fpr pairs; the stored
11054 reg number is 0 for f1, so we want to make it odd. */
11055 else if (reg == fpr && TYPE_MODE (type) == TDmode)
11057 t = build2 (BIT_IOR_EXPR, TREE_TYPE (reg), unshare_expr (reg),
11058 build_int_cst (TREE_TYPE (reg), 1));
11059 u = build2 (MODIFY_EXPR, void_type_node, unshare_expr (reg), t);
11062 t = fold_convert (TREE_TYPE (reg), size_int (8 - n_reg + 1));
11063 t = build2 (GE_EXPR, boolean_type_node, u, t);
11064 u = build1 (GOTO_EXPR, void_type_node, lab_false);
11065 t = build3 (COND_EXPR, void_type_node, t, u, NULL_TREE);
11066 gimplify_and_add (t, pre_p);
11068 t = sav;
11069 if (sav_ofs)
11070 t = fold_build_pointer_plus_hwi (sav, sav_ofs);
11072 u = build2 (POSTINCREMENT_EXPR, TREE_TYPE (reg), unshare_expr (reg),
11073 build_int_cst (TREE_TYPE (reg), n_reg));
11074 u = fold_convert (sizetype, u);
11075 u = build2 (MULT_EXPR, sizetype, u, size_int (sav_scale));
11076 t = fold_build_pointer_plus (t, u);
11078 /* _Decimal32 varargs are located in the second word of the 64-bit
11079 FP register for 32-bit binaries. */
11080 if (!TARGET_POWERPC64
11081 && TARGET_HARD_FLOAT && TARGET_FPRS
11082 && TYPE_MODE (type) == SDmode)
11083 t = fold_build_pointer_plus_hwi (t, size);
11085 gimplify_assign (addr, t, pre_p);
11087 gimple_seq_add_stmt (pre_p, gimple_build_goto (lab_over));
11089 stmt = gimple_build_label (lab_false);
11090 gimple_seq_add_stmt (pre_p, stmt);
11092 if ((n_reg == 2 && !regalign) || n_reg > 2)
11094 /* Ensure that we don't find any more args in regs.
11095 Alignment has taken care of for special cases. */
11096 gimplify_assign (reg, build_int_cst (TREE_TYPE (reg), 8), pre_p);
11100 /* ... otherwise out of the overflow area. */
11102 /* Care for on-stack alignment if needed. */
11103 t = ovf;
11104 if (align != 1)
11106 t = fold_build_pointer_plus_hwi (t, align - 1);
11107 t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t,
11108 build_int_cst (TREE_TYPE (t), -align));
11110 gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
11112 gimplify_assign (unshare_expr (addr), t, pre_p);
11114 t = fold_build_pointer_plus_hwi (t, size);
11115 gimplify_assign (unshare_expr (ovf), t, pre_p);
11117 if (lab_over)
11119 stmt = gimple_build_label (lab_over);
11120 gimple_seq_add_stmt (pre_p, stmt);
11123 if (STRICT_ALIGNMENT
11124 && (TYPE_ALIGN (type)
11125 > (unsigned) BITS_PER_UNIT * (align < 4 ? 4 : align)))
11127 /* The value (of type complex double, for example) may not be
11128 aligned in memory in the saved registers, so copy via a
11129 temporary. (This is the same code as used for SPARC.) */
11130 tree tmp = create_tmp_var (type, "va_arg_tmp");
11131 tree dest_addr = build_fold_addr_expr (tmp);
11133 tree copy = build_call_expr (builtin_decl_implicit (BUILT_IN_MEMCPY),
11134 3, dest_addr, addr, size_int (rsize * 4));
11136 gimplify_and_add (copy, pre_p);
11137 addr = dest_addr;
11140 addr = fold_convert (ptrtype, addr);
11141 return build_va_arg_indirect_ref (addr);
11144 /* Builtins. */
11146 static void
11147 def_builtin (const char *name, tree type, enum rs6000_builtins code)
11149 tree t;
11150 unsigned classify = rs6000_builtin_info[(int)code].attr;
11151 const char *attr_string = "";
11153 gcc_assert (name != NULL);
11154 gcc_assert (IN_RANGE ((int)code, 0, (int)RS6000_BUILTIN_COUNT));
11156 if (rs6000_builtin_decls[(int)code])
11157 fatal_error ("internal error: builtin function %s already processed", name);
11159 rs6000_builtin_decls[(int)code] = t =
11160 add_builtin_function (name, type, (int)code, BUILT_IN_MD, NULL, NULL_TREE);
11162 /* Set any special attributes. */
11163 if ((classify & RS6000_BTC_CONST) != 0)
11165 /* const function, function only depends on the inputs. */
11166 TREE_READONLY (t) = 1;
11167 TREE_NOTHROW (t) = 1;
11168 attr_string = ", pure";
11170 else if ((classify & RS6000_BTC_PURE) != 0)
11172 /* pure function, function can read global memory, but does not set any
11173 external state. */
11174 DECL_PURE_P (t) = 1;
11175 TREE_NOTHROW (t) = 1;
11176 attr_string = ", const";
11178 else if ((classify & RS6000_BTC_FP) != 0)
11180 /* Function is a math function. If rounding mode is on, then treat the
11181 function as not reading global memory, but it can have arbitrary side
11182 effects. If it is off, then assume the function is a const function.
11183 This mimics the ATTR_MATHFN_FPROUNDING attribute in
11184 builtin-attribute.def that is used for the math functions. */
11185 TREE_NOTHROW (t) = 1;
11186 if (flag_rounding_math)
11188 DECL_PURE_P (t) = 1;
11189 DECL_IS_NOVOPS (t) = 1;
11190 attr_string = ", fp, pure";
11192 else
11194 TREE_READONLY (t) = 1;
11195 attr_string = ", fp, const";
11198 else if ((classify & RS6000_BTC_ATTR_MASK) != 0)
11199 gcc_unreachable ();
11201 if (TARGET_DEBUG_BUILTIN)
11202 fprintf (stderr, "rs6000_builtin, code = %4d, %s%s\n",
11203 (int)code, name, attr_string);
11206 /* Simple ternary operations: VECd = foo (VECa, VECb, VECc). */
11208 #undef RS6000_BUILTIN_1
11209 #undef RS6000_BUILTIN_2
11210 #undef RS6000_BUILTIN_3
11211 #undef RS6000_BUILTIN_A
11212 #undef RS6000_BUILTIN_D
11213 #undef RS6000_BUILTIN_E
11214 #undef RS6000_BUILTIN_H
11215 #undef RS6000_BUILTIN_P
11216 #undef RS6000_BUILTIN_Q
11217 #undef RS6000_BUILTIN_S
11218 #undef RS6000_BUILTIN_X
11220 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
11221 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
11222 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE) \
11223 { MASK, ICODE, NAME, ENUM },
11225 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
11226 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
11227 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
11228 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
11229 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
11230 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
11231 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
11232 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11234 static const struct builtin_description bdesc_3arg[] =
11236 #include "rs6000-builtin.def"
11239 /* DST operations: void foo (void *, const int, const char). */
11241 #undef RS6000_BUILTIN_1
11242 #undef RS6000_BUILTIN_2
11243 #undef RS6000_BUILTIN_3
11244 #undef RS6000_BUILTIN_A
11245 #undef RS6000_BUILTIN_D
11246 #undef RS6000_BUILTIN_E
11247 #undef RS6000_BUILTIN_H
11248 #undef RS6000_BUILTIN_P
11249 #undef RS6000_BUILTIN_Q
11250 #undef RS6000_BUILTIN_S
11251 #undef RS6000_BUILTIN_X
11253 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
11254 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
11255 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
11256 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
11257 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE) \
11258 { MASK, ICODE, NAME, ENUM },
11260 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
11261 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
11262 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
11263 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
11264 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
11265 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11267 static const struct builtin_description bdesc_dst[] =
11269 #include "rs6000-builtin.def"
11272 /* Simple binary operations: VECc = foo (VECa, VECb). */
11274 #undef RS6000_BUILTIN_1
11275 #undef RS6000_BUILTIN_2
11276 #undef RS6000_BUILTIN_3
11277 #undef RS6000_BUILTIN_A
11278 #undef RS6000_BUILTIN_D
11279 #undef RS6000_BUILTIN_E
11280 #undef RS6000_BUILTIN_H
11281 #undef RS6000_BUILTIN_P
11282 #undef RS6000_BUILTIN_Q
11283 #undef RS6000_BUILTIN_S
11284 #undef RS6000_BUILTIN_X
11286 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
11287 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE) \
11288 { MASK, ICODE, NAME, ENUM },
11290 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
11291 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
11292 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
11293 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
11294 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
11295 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
11296 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
11297 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
11298 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11300 static const struct builtin_description bdesc_2arg[] =
11302 #include "rs6000-builtin.def"
11305 #undef RS6000_BUILTIN_1
11306 #undef RS6000_BUILTIN_2
11307 #undef RS6000_BUILTIN_3
11308 #undef RS6000_BUILTIN_A
11309 #undef RS6000_BUILTIN_D
11310 #undef RS6000_BUILTIN_E
11311 #undef RS6000_BUILTIN_H
11312 #undef RS6000_BUILTIN_P
11313 #undef RS6000_BUILTIN_Q
11314 #undef RS6000_BUILTIN_S
11315 #undef RS6000_BUILTIN_X
11317 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
11318 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
11319 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
11320 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
11321 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
11322 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
11323 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
11324 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE) \
11325 { MASK, ICODE, NAME, ENUM },
11327 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
11328 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
11329 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11331 /* AltiVec predicates. */
11333 static const struct builtin_description bdesc_altivec_preds[] =
11335 #include "rs6000-builtin.def"
11338 /* SPE predicates. */
11339 #undef RS6000_BUILTIN_1
11340 #undef RS6000_BUILTIN_2
11341 #undef RS6000_BUILTIN_3
11342 #undef RS6000_BUILTIN_A
11343 #undef RS6000_BUILTIN_D
11344 #undef RS6000_BUILTIN_E
11345 #undef RS6000_BUILTIN_H
11346 #undef RS6000_BUILTIN_P
11347 #undef RS6000_BUILTIN_Q
11348 #undef RS6000_BUILTIN_S
11349 #undef RS6000_BUILTIN_X
11351 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
11352 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
11353 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
11354 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
11355 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
11356 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
11357 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
11358 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
11359 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
11360 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE) \
11361 { MASK, ICODE, NAME, ENUM },
11363 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11365 static const struct builtin_description bdesc_spe_predicates[] =
11367 #include "rs6000-builtin.def"
11370 /* SPE evsel predicates. */
11371 #undef RS6000_BUILTIN_1
11372 #undef RS6000_BUILTIN_2
11373 #undef RS6000_BUILTIN_3
11374 #undef RS6000_BUILTIN_A
11375 #undef RS6000_BUILTIN_D
11376 #undef RS6000_BUILTIN_E
11377 #undef RS6000_BUILTIN_H
11378 #undef RS6000_BUILTIN_P
11379 #undef RS6000_BUILTIN_Q
11380 #undef RS6000_BUILTIN_S
11381 #undef RS6000_BUILTIN_X
11383 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
11384 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
11385 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
11386 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
11387 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
11388 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE) \
11389 { MASK, ICODE, NAME, ENUM },
11391 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
11392 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
11393 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
11394 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
11395 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11397 static const struct builtin_description bdesc_spe_evsel[] =
11399 #include "rs6000-builtin.def"
11402 /* PAIRED predicates. */
11403 #undef RS6000_BUILTIN_1
11404 #undef RS6000_BUILTIN_2
11405 #undef RS6000_BUILTIN_3
11406 #undef RS6000_BUILTIN_A
11407 #undef RS6000_BUILTIN_D
11408 #undef RS6000_BUILTIN_E
11409 #undef RS6000_BUILTIN_H
11410 #undef RS6000_BUILTIN_P
11411 #undef RS6000_BUILTIN_Q
11412 #undef RS6000_BUILTIN_S
11413 #undef RS6000_BUILTIN_X
11415 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
11416 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
11417 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
11418 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
11419 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
11420 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
11421 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
11422 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
11423 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE) \
11424 { MASK, ICODE, NAME, ENUM },
11426 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
11427 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11429 static const struct builtin_description bdesc_paired_preds[] =
11431 #include "rs6000-builtin.def"
11434 /* ABS* operations. */
11436 #undef RS6000_BUILTIN_1
11437 #undef RS6000_BUILTIN_2
11438 #undef RS6000_BUILTIN_3
11439 #undef RS6000_BUILTIN_A
11440 #undef RS6000_BUILTIN_D
11441 #undef RS6000_BUILTIN_E
11442 #undef RS6000_BUILTIN_H
11443 #undef RS6000_BUILTIN_P
11444 #undef RS6000_BUILTIN_Q
11445 #undef RS6000_BUILTIN_S
11446 #undef RS6000_BUILTIN_X
11448 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
11449 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
11450 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
11451 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE) \
11452 { MASK, ICODE, NAME, ENUM },
11454 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
11455 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
11456 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
11457 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
11458 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
11459 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
11460 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11462 static const struct builtin_description bdesc_abs[] =
11464 #include "rs6000-builtin.def"
11467 /* Simple unary operations: VECb = foo (unsigned literal) or VECb =
11468 foo (VECa). */
11470 #undef RS6000_BUILTIN_1
11471 #undef RS6000_BUILTIN_2
11472 #undef RS6000_BUILTIN_3
11473 #undef RS6000_BUILTIN_A
11474 #undef RS6000_BUILTIN_D
11475 #undef RS6000_BUILTIN_E
11476 #undef RS6000_BUILTIN_H
11477 #undef RS6000_BUILTIN_P
11478 #undef RS6000_BUILTIN_Q
11479 #undef RS6000_BUILTIN_S
11480 #undef RS6000_BUILTIN_X
11482 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE) \
11483 { MASK, ICODE, NAME, ENUM },
11485 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
11486 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
11487 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
11488 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
11489 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
11490 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
11491 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
11492 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
11493 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
11494 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11496 static const struct builtin_description bdesc_1arg[] =
11498 #include "rs6000-builtin.def"
11501 /* HTM builtins. */
11502 #undef RS6000_BUILTIN_1
11503 #undef RS6000_BUILTIN_2
11504 #undef RS6000_BUILTIN_3
11505 #undef RS6000_BUILTIN_A
11506 #undef RS6000_BUILTIN_D
11507 #undef RS6000_BUILTIN_E
11508 #undef RS6000_BUILTIN_H
11509 #undef RS6000_BUILTIN_P
11510 #undef RS6000_BUILTIN_Q
11511 #undef RS6000_BUILTIN_S
11512 #undef RS6000_BUILTIN_X
11514 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
11515 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
11516 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
11517 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
11518 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
11519 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
11520 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE) \
11521 { MASK, ICODE, NAME, ENUM },
11523 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
11524 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
11525 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
11526 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
11528 static const struct builtin_description bdesc_htm[] =
11530 #include "rs6000-builtin.def"
11533 #undef RS6000_BUILTIN_1
11534 #undef RS6000_BUILTIN_2
11535 #undef RS6000_BUILTIN_3
11536 #undef RS6000_BUILTIN_A
11537 #undef RS6000_BUILTIN_D
11538 #undef RS6000_BUILTIN_E
11539 #undef RS6000_BUILTIN_H
11540 #undef RS6000_BUILTIN_P
11541 #undef RS6000_BUILTIN_Q
11542 #undef RS6000_BUILTIN_S
11544 /* Return true if a builtin function is overloaded. */
11545 bool
11546 rs6000_overloaded_builtin_p (enum rs6000_builtins fncode)
11548 return (rs6000_builtin_info[(int)fncode].attr & RS6000_BTC_OVERLOADED) != 0;
11551 /* Expand an expression EXP that calls a builtin without arguments. */
11552 static rtx
11553 rs6000_expand_zeroop_builtin (enum insn_code icode, rtx target)
11555 rtx pat;
11556 enum machine_mode tmode = insn_data[icode].operand[0].mode;
11558 if (icode == CODE_FOR_nothing)
11559 /* Builtin not supported on this processor. */
11560 return 0;
11562 if (target == 0
11563 || GET_MODE (target) != tmode
11564 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11565 target = gen_reg_rtx (tmode);
11567 pat = GEN_FCN (icode) (target);
11568 if (! pat)
11569 return 0;
11570 emit_insn (pat);
11572 return target;
11576 static rtx
11577 rs6000_expand_mtfsf_builtin (enum insn_code icode, tree exp)
11579 rtx pat;
11580 tree arg0 = CALL_EXPR_ARG (exp, 0);
11581 tree arg1 = CALL_EXPR_ARG (exp, 1);
11582 rtx op0 = expand_normal (arg0);
11583 rtx op1 = expand_normal (arg1);
11584 enum machine_mode mode0 = insn_data[icode].operand[0].mode;
11585 enum machine_mode mode1 = insn_data[icode].operand[1].mode;
11587 if (icode == CODE_FOR_nothing)
11588 /* Builtin not supported on this processor. */
11589 return 0;
11591 /* If we got invalid arguments bail out before generating bad rtl. */
11592 if (arg0 == error_mark_node || arg1 == error_mark_node)
11593 return const0_rtx;
11595 if (GET_CODE (op0) != CONST_INT
11596 || INTVAL (op0) > 255
11597 || INTVAL (op0) < 0)
11599 error ("argument 1 must be an 8-bit field value");
11600 return const0_rtx;
11603 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
11604 op0 = copy_to_mode_reg (mode0, op0);
11606 if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
11607 op1 = copy_to_mode_reg (mode1, op1);
11609 pat = GEN_FCN (icode) (op0, op1);
11610 if (! pat)
11611 return const0_rtx;
11612 emit_insn (pat);
11614 return NULL_RTX;
11618 static rtx
11619 rs6000_expand_unop_builtin (enum insn_code icode, tree exp, rtx target)
11621 rtx pat;
11622 tree arg0 = CALL_EXPR_ARG (exp, 0);
11623 rtx op0 = expand_normal (arg0);
11624 enum machine_mode tmode = insn_data[icode].operand[0].mode;
11625 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11627 if (icode == CODE_FOR_nothing)
11628 /* Builtin not supported on this processor. */
11629 return 0;
11631 /* If we got invalid arguments bail out before generating bad rtl. */
11632 if (arg0 == error_mark_node)
11633 return const0_rtx;
11635 if (icode == CODE_FOR_altivec_vspltisb
11636 || icode == CODE_FOR_altivec_vspltish
11637 || icode == CODE_FOR_altivec_vspltisw
11638 || icode == CODE_FOR_spe_evsplatfi
11639 || icode == CODE_FOR_spe_evsplati)
11641 /* Only allow 5-bit *signed* literals. */
11642 if (GET_CODE (op0) != CONST_INT
11643 || INTVAL (op0) > 15
11644 || INTVAL (op0) < -16)
11646 error ("argument 1 must be a 5-bit signed literal");
11647 return const0_rtx;
11651 if (target == 0
11652 || GET_MODE (target) != tmode
11653 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11654 target = gen_reg_rtx (tmode);
11656 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11657 op0 = copy_to_mode_reg (mode0, op0);
11659 pat = GEN_FCN (icode) (target, op0);
11660 if (! pat)
11661 return 0;
11662 emit_insn (pat);
11664 return target;
11667 static rtx
11668 altivec_expand_abs_builtin (enum insn_code icode, tree exp, rtx target)
11670 rtx pat, scratch1, scratch2;
11671 tree arg0 = CALL_EXPR_ARG (exp, 0);
11672 rtx op0 = expand_normal (arg0);
11673 enum machine_mode tmode = insn_data[icode].operand[0].mode;
11674 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11676 /* If we have invalid arguments, bail out before generating bad rtl. */
11677 if (arg0 == error_mark_node)
11678 return const0_rtx;
11680 if (target == 0
11681 || GET_MODE (target) != tmode
11682 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11683 target = gen_reg_rtx (tmode);
11685 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11686 op0 = copy_to_mode_reg (mode0, op0);
11688 scratch1 = gen_reg_rtx (mode0);
11689 scratch2 = gen_reg_rtx (mode0);
11691 pat = GEN_FCN (icode) (target, op0, scratch1, scratch2);
11692 if (! pat)
11693 return 0;
11694 emit_insn (pat);
11696 return target;
11699 static rtx
11700 rs6000_expand_binop_builtin (enum insn_code icode, tree exp, rtx target)
11702 rtx pat;
11703 tree arg0 = CALL_EXPR_ARG (exp, 0);
11704 tree arg1 = CALL_EXPR_ARG (exp, 1);
11705 rtx op0 = expand_normal (arg0);
11706 rtx op1 = expand_normal (arg1);
11707 enum machine_mode tmode = insn_data[icode].operand[0].mode;
11708 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11709 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
11711 if (icode == CODE_FOR_nothing)
11712 /* Builtin not supported on this processor. */
11713 return 0;
11715 /* If we got invalid arguments bail out before generating bad rtl. */
11716 if (arg0 == error_mark_node || arg1 == error_mark_node)
11717 return const0_rtx;
11719 if (icode == CODE_FOR_altivec_vcfux
11720 || icode == CODE_FOR_altivec_vcfsx
11721 || icode == CODE_FOR_altivec_vctsxs
11722 || icode == CODE_FOR_altivec_vctuxs
11723 || icode == CODE_FOR_altivec_vspltb
11724 || icode == CODE_FOR_altivec_vsplth
11725 || icode == CODE_FOR_altivec_vspltw
11726 || icode == CODE_FOR_spe_evaddiw
11727 || icode == CODE_FOR_spe_evldd
11728 || icode == CODE_FOR_spe_evldh
11729 || icode == CODE_FOR_spe_evldw
11730 || icode == CODE_FOR_spe_evlhhesplat
11731 || icode == CODE_FOR_spe_evlhhossplat
11732 || icode == CODE_FOR_spe_evlhhousplat
11733 || icode == CODE_FOR_spe_evlwhe
11734 || icode == CODE_FOR_spe_evlwhos
11735 || icode == CODE_FOR_spe_evlwhou
11736 || icode == CODE_FOR_spe_evlwhsplat
11737 || icode == CODE_FOR_spe_evlwwsplat
11738 || icode == CODE_FOR_spe_evrlwi
11739 || icode == CODE_FOR_spe_evslwi
11740 || icode == CODE_FOR_spe_evsrwis
11741 || icode == CODE_FOR_spe_evsubifw
11742 || icode == CODE_FOR_spe_evsrwiu)
11744 /* Only allow 5-bit unsigned literals. */
11745 STRIP_NOPS (arg1);
11746 if (TREE_CODE (arg1) != INTEGER_CST
11747 || TREE_INT_CST_LOW (arg1) & ~0x1f)
11749 error ("argument 2 must be a 5-bit unsigned literal");
11750 return const0_rtx;
11754 if (target == 0
11755 || GET_MODE (target) != tmode
11756 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11757 target = gen_reg_rtx (tmode);
11759 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11760 op0 = copy_to_mode_reg (mode0, op0);
11761 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
11762 op1 = copy_to_mode_reg (mode1, op1);
11764 pat = GEN_FCN (icode) (target, op0, op1);
11765 if (! pat)
11766 return 0;
11767 emit_insn (pat);
11769 return target;
11772 static rtx
11773 altivec_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
11775 rtx pat, scratch;
11776 tree cr6_form = CALL_EXPR_ARG (exp, 0);
11777 tree arg0 = CALL_EXPR_ARG (exp, 1);
11778 tree arg1 = CALL_EXPR_ARG (exp, 2);
11779 rtx op0 = expand_normal (arg0);
11780 rtx op1 = expand_normal (arg1);
11781 enum machine_mode tmode = SImode;
11782 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11783 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
11784 int cr6_form_int;
11786 if (TREE_CODE (cr6_form) != INTEGER_CST)
11788 error ("argument 1 of __builtin_altivec_predicate must be a constant");
11789 return const0_rtx;
11791 else
11792 cr6_form_int = TREE_INT_CST_LOW (cr6_form);
11794 gcc_assert (mode0 == mode1);
11796 /* If we have invalid arguments, bail out before generating bad rtl. */
11797 if (arg0 == error_mark_node || arg1 == error_mark_node)
11798 return const0_rtx;
11800 if (target == 0
11801 || GET_MODE (target) != tmode
11802 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11803 target = gen_reg_rtx (tmode);
11805 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11806 op0 = copy_to_mode_reg (mode0, op0);
11807 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
11808 op1 = copy_to_mode_reg (mode1, op1);
11810 scratch = gen_reg_rtx (mode0);
11812 pat = GEN_FCN (icode) (scratch, op0, op1);
11813 if (! pat)
11814 return 0;
11815 emit_insn (pat);
11817 /* The vec_any* and vec_all* predicates use the same opcodes for two
11818 different operations, but the bits in CR6 will be different
11819 depending on what information we want. So we have to play tricks
11820 with CR6 to get the right bits out.
11822 If you think this is disgusting, look at the specs for the
11823 AltiVec predicates. */
11825 switch (cr6_form_int)
11827 case 0:
11828 emit_insn (gen_cr6_test_for_zero (target));
11829 break;
11830 case 1:
11831 emit_insn (gen_cr6_test_for_zero_reverse (target));
11832 break;
11833 case 2:
11834 emit_insn (gen_cr6_test_for_lt (target));
11835 break;
11836 case 3:
11837 emit_insn (gen_cr6_test_for_lt_reverse (target));
11838 break;
11839 default:
11840 error ("argument 1 of __builtin_altivec_predicate is out of range");
11841 break;
11844 return target;
11847 static rtx
11848 paired_expand_lv_builtin (enum insn_code icode, tree exp, rtx target)
11850 rtx pat, addr;
11851 tree arg0 = CALL_EXPR_ARG (exp, 0);
11852 tree arg1 = CALL_EXPR_ARG (exp, 1);
11853 enum machine_mode tmode = insn_data[icode].operand[0].mode;
11854 enum machine_mode mode0 = Pmode;
11855 enum machine_mode mode1 = Pmode;
11856 rtx op0 = expand_normal (arg0);
11857 rtx op1 = expand_normal (arg1);
11859 if (icode == CODE_FOR_nothing)
11860 /* Builtin not supported on this processor. */
11861 return 0;
11863 /* If we got invalid arguments bail out before generating bad rtl. */
11864 if (arg0 == error_mark_node || arg1 == error_mark_node)
11865 return const0_rtx;
11867 if (target == 0
11868 || GET_MODE (target) != tmode
11869 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11870 target = gen_reg_rtx (tmode);
11872 op1 = copy_to_mode_reg (mode1, op1);
11874 if (op0 == const0_rtx)
11876 addr = gen_rtx_MEM (tmode, op1);
11878 else
11880 op0 = copy_to_mode_reg (mode0, op0);
11881 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op0, op1));
11884 pat = GEN_FCN (icode) (target, addr);
11886 if (! pat)
11887 return 0;
11888 emit_insn (pat);
11890 return target;
11893 /* Return a constant vector for use as a little-endian permute control vector
11894 to reverse the order of elements of the given vector mode. */
11895 static rtx
11896 swap_selector_for_mode (enum machine_mode mode)
11898 /* These are little endian vectors, so their elements are reversed
11899 from what you would normally expect for a permute control vector. */
11900 unsigned int swap2[16] = {7,6,5,4,3,2,1,0,15,14,13,12,11,10,9,8};
11901 unsigned int swap4[16] = {3,2,1,0,7,6,5,4,11,10,9,8,15,14,13,12};
11902 unsigned int swap8[16] = {1,0,3,2,5,4,7,6,9,8,11,10,13,12,15,14};
11903 unsigned int swap16[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
11904 unsigned int *swaparray, i;
11905 rtx perm[16];
11907 switch (mode)
11909 case V2DFmode:
11910 case V2DImode:
11911 swaparray = swap2;
11912 break;
11913 case V4SFmode:
11914 case V4SImode:
11915 swaparray = swap4;
11916 break;
11917 case V8HImode:
11918 swaparray = swap8;
11919 break;
11920 case V16QImode:
11921 swaparray = swap16;
11922 break;
11923 default:
11924 gcc_unreachable ();
11927 for (i = 0; i < 16; ++i)
11928 perm[i] = GEN_INT (swaparray[i]);
11930 return force_reg (V16QImode, gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, perm)));
11933 /* Generate code for an "lvx", "lvxl", or "lve*x" built-in for a little endian target
11934 with -maltivec=be specified. Issue the load followed by an element-reversing
11935 permute. */
11936 void
11937 altivec_expand_lvx_be (rtx op0, rtx op1, enum machine_mode mode, unsigned unspec)
11939 rtx tmp = gen_reg_rtx (mode);
11940 rtx load = gen_rtx_SET (VOIDmode, tmp, op1);
11941 rtx lvx = gen_rtx_UNSPEC (mode, gen_rtvec (1, const0_rtx), unspec);
11942 rtx par = gen_rtx_PARALLEL (mode, gen_rtvec (2, load, lvx));
11943 rtx sel = swap_selector_for_mode (mode);
11944 rtx vperm = gen_rtx_UNSPEC (mode, gen_rtvec (3, tmp, tmp, sel), UNSPEC_VPERM);
11946 gcc_assert (REG_P (op0));
11947 emit_insn (par);
11948 emit_insn (gen_rtx_SET (VOIDmode, op0, vperm));
11951 /* Generate code for a "stvx" or "stvxl" built-in for a little endian target
11952 with -maltivec=be specified. Issue the store preceded by an element-reversing
11953 permute. */
11954 void
11955 altivec_expand_stvx_be (rtx op0, rtx op1, enum machine_mode mode, unsigned unspec)
11957 rtx tmp = gen_reg_rtx (mode);
11958 rtx store = gen_rtx_SET (VOIDmode, op0, tmp);
11959 rtx stvx = gen_rtx_UNSPEC (mode, gen_rtvec (1, const0_rtx), unspec);
11960 rtx par = gen_rtx_PARALLEL (mode, gen_rtvec (2, store, stvx));
11961 rtx sel = swap_selector_for_mode (mode);
11962 rtx vperm;
11964 gcc_assert (REG_P (op1));
11965 vperm = gen_rtx_UNSPEC (mode, gen_rtvec (3, op1, op1, sel), UNSPEC_VPERM);
11966 emit_insn (gen_rtx_SET (VOIDmode, tmp, vperm));
11967 emit_insn (par);
11970 /* Generate code for a "stve*x" built-in for a little endian target with -maltivec=be
11971 specified. Issue the store preceded by an element-reversing permute. */
11972 void
11973 altivec_expand_stvex_be (rtx op0, rtx op1, enum machine_mode mode, unsigned unspec)
11975 enum machine_mode inner_mode = GET_MODE_INNER (mode);
11976 rtx tmp = gen_reg_rtx (mode);
11977 rtx stvx = gen_rtx_UNSPEC (inner_mode, gen_rtvec (1, tmp), unspec);
11978 rtx sel = swap_selector_for_mode (mode);
11979 rtx vperm;
11981 gcc_assert (REG_P (op1));
11982 vperm = gen_rtx_UNSPEC (mode, gen_rtvec (3, op1, op1, sel), UNSPEC_VPERM);
11983 emit_insn (gen_rtx_SET (VOIDmode, tmp, vperm));
11984 emit_insn (gen_rtx_SET (VOIDmode, op0, stvx));
11987 static rtx
11988 altivec_expand_lv_builtin (enum insn_code icode, tree exp, rtx target, bool blk)
11990 rtx pat, addr;
11991 tree arg0 = CALL_EXPR_ARG (exp, 0);
11992 tree arg1 = CALL_EXPR_ARG (exp, 1);
11993 enum machine_mode tmode = insn_data[icode].operand[0].mode;
11994 enum machine_mode mode0 = Pmode;
11995 enum machine_mode mode1 = Pmode;
11996 rtx op0 = expand_normal (arg0);
11997 rtx op1 = expand_normal (arg1);
11999 if (icode == CODE_FOR_nothing)
12000 /* Builtin not supported on this processor. */
12001 return 0;
12003 /* If we got invalid arguments bail out before generating bad rtl. */
12004 if (arg0 == error_mark_node || arg1 == error_mark_node)
12005 return const0_rtx;
12007 if (target == 0
12008 || GET_MODE (target) != tmode
12009 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12010 target = gen_reg_rtx (tmode);
12012 op1 = copy_to_mode_reg (mode1, op1);
12014 if (op0 == const0_rtx)
12016 addr = gen_rtx_MEM (blk ? BLKmode : tmode, op1);
12018 else
12020 op0 = copy_to_mode_reg (mode0, op0);
12021 addr = gen_rtx_MEM (blk ? BLKmode : tmode, gen_rtx_PLUS (Pmode, op0, op1));
12024 pat = GEN_FCN (icode) (target, addr);
12026 if (! pat)
12027 return 0;
12028 emit_insn (pat);
12030 return target;
12033 static rtx
12034 spe_expand_stv_builtin (enum insn_code icode, tree exp)
12036 tree arg0 = CALL_EXPR_ARG (exp, 0);
12037 tree arg1 = CALL_EXPR_ARG (exp, 1);
12038 tree arg2 = CALL_EXPR_ARG (exp, 2);
12039 rtx op0 = expand_normal (arg0);
12040 rtx op1 = expand_normal (arg1);
12041 rtx op2 = expand_normal (arg2);
12042 rtx pat;
12043 enum machine_mode mode0 = insn_data[icode].operand[0].mode;
12044 enum machine_mode mode1 = insn_data[icode].operand[1].mode;
12045 enum machine_mode mode2 = insn_data[icode].operand[2].mode;
12047 /* Invalid arguments. Bail before doing anything stoopid! */
12048 if (arg0 == error_mark_node
12049 || arg1 == error_mark_node
12050 || arg2 == error_mark_node)
12051 return const0_rtx;
12053 if (! (*insn_data[icode].operand[2].predicate) (op0, mode2))
12054 op0 = copy_to_mode_reg (mode2, op0);
12055 if (! (*insn_data[icode].operand[0].predicate) (op1, mode0))
12056 op1 = copy_to_mode_reg (mode0, op1);
12057 if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
12058 op2 = copy_to_mode_reg (mode1, op2);
12060 pat = GEN_FCN (icode) (op1, op2, op0);
12061 if (pat)
12062 emit_insn (pat);
12063 return NULL_RTX;
12066 static rtx
12067 paired_expand_stv_builtin (enum insn_code icode, tree exp)
12069 tree arg0 = CALL_EXPR_ARG (exp, 0);
12070 tree arg1 = CALL_EXPR_ARG (exp, 1);
12071 tree arg2 = CALL_EXPR_ARG (exp, 2);
12072 rtx op0 = expand_normal (arg0);
12073 rtx op1 = expand_normal (arg1);
12074 rtx op2 = expand_normal (arg2);
12075 rtx pat, addr;
12076 enum machine_mode tmode = insn_data[icode].operand[0].mode;
12077 enum machine_mode mode1 = Pmode;
12078 enum machine_mode mode2 = Pmode;
12080 /* Invalid arguments. Bail before doing anything stoopid! */
12081 if (arg0 == error_mark_node
12082 || arg1 == error_mark_node
12083 || arg2 == error_mark_node)
12084 return const0_rtx;
12086 if (! (*insn_data[icode].operand[1].predicate) (op0, tmode))
12087 op0 = copy_to_mode_reg (tmode, op0);
12089 op2 = copy_to_mode_reg (mode2, op2);
12091 if (op1 == const0_rtx)
12093 addr = gen_rtx_MEM (tmode, op2);
12095 else
12097 op1 = copy_to_mode_reg (mode1, op1);
12098 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op1, op2));
12101 pat = GEN_FCN (icode) (addr, op0);
12102 if (pat)
12103 emit_insn (pat);
12104 return NULL_RTX;
12107 static rtx
12108 altivec_expand_stv_builtin (enum insn_code icode, tree exp)
12110 tree arg0 = CALL_EXPR_ARG (exp, 0);
12111 tree arg1 = CALL_EXPR_ARG (exp, 1);
12112 tree arg2 = CALL_EXPR_ARG (exp, 2);
12113 rtx op0 = expand_normal (arg0);
12114 rtx op1 = expand_normal (arg1);
12115 rtx op2 = expand_normal (arg2);
12116 rtx pat, addr;
12117 enum machine_mode tmode = insn_data[icode].operand[0].mode;
12118 enum machine_mode smode = insn_data[icode].operand[1].mode;
12119 enum machine_mode mode1 = Pmode;
12120 enum machine_mode mode2 = Pmode;
12122 /* Invalid arguments. Bail before doing anything stoopid! */
12123 if (arg0 == error_mark_node
12124 || arg1 == error_mark_node
12125 || arg2 == error_mark_node)
12126 return const0_rtx;
12128 if (! (*insn_data[icode].operand[1].predicate) (op0, smode))
12129 op0 = copy_to_mode_reg (smode, op0);
12131 op2 = copy_to_mode_reg (mode2, op2);
12133 if (op1 == const0_rtx)
12135 addr = gen_rtx_MEM (tmode, op2);
12137 else
12139 op1 = copy_to_mode_reg (mode1, op1);
12140 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op1, op2));
12143 pat = GEN_FCN (icode) (addr, op0);
12144 if (pat)
12145 emit_insn (pat);
12146 return NULL_RTX;
12149 /* Return the appropriate SPR number associated with the given builtin. */
12150 static inline HOST_WIDE_INT
12151 htm_spr_num (enum rs6000_builtins code)
12153 if (code == HTM_BUILTIN_GET_TFHAR
12154 || code == HTM_BUILTIN_SET_TFHAR)
12155 return TFHAR_SPR;
12156 else if (code == HTM_BUILTIN_GET_TFIAR
12157 || code == HTM_BUILTIN_SET_TFIAR)
12158 return TFIAR_SPR;
12159 else if (code == HTM_BUILTIN_GET_TEXASR
12160 || code == HTM_BUILTIN_SET_TEXASR)
12161 return TEXASR_SPR;
12162 gcc_assert (code == HTM_BUILTIN_GET_TEXASRU
12163 || code == HTM_BUILTIN_SET_TEXASRU);
12164 return TEXASRU_SPR;
12167 /* Return the appropriate SPR regno associated with the given builtin. */
12168 static inline HOST_WIDE_INT
12169 htm_spr_regno (enum rs6000_builtins code)
12171 if (code == HTM_BUILTIN_GET_TFHAR
12172 || code == HTM_BUILTIN_SET_TFHAR)
12173 return TFHAR_REGNO;
12174 else if (code == HTM_BUILTIN_GET_TFIAR
12175 || code == HTM_BUILTIN_SET_TFIAR)
12176 return TFIAR_REGNO;
12177 gcc_assert (code == HTM_BUILTIN_GET_TEXASR
12178 || code == HTM_BUILTIN_SET_TEXASR
12179 || code == HTM_BUILTIN_GET_TEXASRU
12180 || code == HTM_BUILTIN_SET_TEXASRU);
12181 return TEXASR_REGNO;
12184 /* Return the correct ICODE value depending on whether we are
12185 setting or reading the HTM SPRs. */
12186 static inline enum insn_code
12187 rs6000_htm_spr_icode (bool nonvoid)
12189 if (nonvoid)
12190 return (TARGET_64BIT) ? CODE_FOR_htm_mfspr_di : CODE_FOR_htm_mfspr_si;
12191 else
12192 return (TARGET_64BIT) ? CODE_FOR_htm_mtspr_di : CODE_FOR_htm_mtspr_si;
12195 /* Expand the HTM builtin in EXP and store the result in TARGET.
12196 Store true in *EXPANDEDP if we found a builtin to expand. */
12197 static rtx
12198 htm_expand_builtin (tree exp, rtx target, bool * expandedp)
12200 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
12201 bool nonvoid = TREE_TYPE (TREE_TYPE (fndecl)) != void_type_node;
12202 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
12203 const struct builtin_description *d;
12204 size_t i;
12206 *expandedp = false;
12208 /* Expand the HTM builtins. */
12209 d = bdesc_htm;
12210 for (i = 0; i < ARRAY_SIZE (bdesc_htm); i++, d++)
12211 if (d->code == fcode)
12213 rtx op[MAX_HTM_OPERANDS], pat;
12214 int nopnds = 0;
12215 tree arg;
12216 call_expr_arg_iterator iter;
12217 unsigned attr = rs6000_builtin_info[fcode].attr;
12218 enum insn_code icode = d->icode;
12220 if (attr & RS6000_BTC_SPR)
12221 icode = rs6000_htm_spr_icode (nonvoid);
12223 if (nonvoid)
12225 enum machine_mode tmode = insn_data[icode].operand[0].mode;
12226 if (!target
12227 || GET_MODE (target) != tmode
12228 || !(*insn_data[icode].operand[0].predicate) (target, tmode))
12229 target = gen_reg_rtx (tmode);
12230 op[nopnds++] = target;
12233 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
12235 const struct insn_operand_data *insn_op;
12237 if (arg == error_mark_node || nopnds >= MAX_HTM_OPERANDS)
12238 return NULL_RTX;
12240 insn_op = &insn_data[icode].operand[nopnds];
12242 op[nopnds] = expand_normal (arg);
12244 if (!(*insn_op->predicate) (op[nopnds], insn_op->mode))
12246 if (!strcmp (insn_op->constraint, "n"))
12248 int arg_num = (nonvoid) ? nopnds : nopnds + 1;
12249 if (!CONST_INT_P (op[nopnds]))
12250 error ("argument %d must be an unsigned literal", arg_num);
12251 else
12252 error ("argument %d is an unsigned literal that is "
12253 "out of range", arg_num);
12254 return const0_rtx;
12256 op[nopnds] = copy_to_mode_reg (insn_op->mode, op[nopnds]);
12259 nopnds++;
12262 /* Handle the builtins for extended mnemonics. These accept
12263 no arguments, but map to builtins that take arguments. */
12264 switch (fcode)
12266 case HTM_BUILTIN_TENDALL: /* Alias for: tend. 1 */
12267 case HTM_BUILTIN_TRESUME: /* Alias for: tsr. 1 */
12268 op[nopnds++] = GEN_INT (1);
12269 #ifdef ENABLE_CHECKING
12270 attr |= RS6000_BTC_UNARY;
12271 #endif
12272 break;
12273 case HTM_BUILTIN_TSUSPEND: /* Alias for: tsr. 0 */
12274 op[nopnds++] = GEN_INT (0);
12275 #ifdef ENABLE_CHECKING
12276 attr |= RS6000_BTC_UNARY;
12277 #endif
12278 break;
12279 default:
12280 break;
12283 /* If this builtin accesses SPRs, then pass in the appropriate
12284 SPR number and SPR regno as the last two operands. */
12285 if (attr & RS6000_BTC_SPR)
12287 op[nopnds++] = gen_rtx_CONST_INT (Pmode, htm_spr_num (fcode));
12288 op[nopnds++] = gen_rtx_REG (Pmode, htm_spr_regno (fcode));
12291 #ifdef ENABLE_CHECKING
12292 int expected_nopnds = 0;
12293 if ((attr & RS6000_BTC_TYPE_MASK) == RS6000_BTC_UNARY)
12294 expected_nopnds = 1;
12295 else if ((attr & RS6000_BTC_TYPE_MASK) == RS6000_BTC_BINARY)
12296 expected_nopnds = 2;
12297 else if ((attr & RS6000_BTC_TYPE_MASK) == RS6000_BTC_TERNARY)
12298 expected_nopnds = 3;
12299 if (!(attr & RS6000_BTC_VOID))
12300 expected_nopnds += 1;
12301 if (attr & RS6000_BTC_SPR)
12302 expected_nopnds += 2;
12304 gcc_assert (nopnds == expected_nopnds && nopnds <= MAX_HTM_OPERANDS);
12305 #endif
12307 switch (nopnds)
12309 case 1:
12310 pat = GEN_FCN (icode) (op[0]);
12311 break;
12312 case 2:
12313 pat = GEN_FCN (icode) (op[0], op[1]);
12314 break;
12315 case 3:
12316 pat = GEN_FCN (icode) (op[0], op[1], op[2]);
12317 break;
12318 case 4:
12319 pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3]);
12320 break;
12321 default:
12322 gcc_unreachable ();
12324 if (!pat)
12325 return NULL_RTX;
12326 emit_insn (pat);
12328 *expandedp = true;
12329 if (nonvoid)
12330 return target;
12331 return const0_rtx;
12334 return NULL_RTX;
12337 static rtx
12338 rs6000_expand_ternop_builtin (enum insn_code icode, tree exp, rtx target)
12340 rtx pat;
12341 tree arg0 = CALL_EXPR_ARG (exp, 0);
12342 tree arg1 = CALL_EXPR_ARG (exp, 1);
12343 tree arg2 = CALL_EXPR_ARG (exp, 2);
12344 rtx op0 = expand_normal (arg0);
12345 rtx op1 = expand_normal (arg1);
12346 rtx op2 = expand_normal (arg2);
12347 enum machine_mode tmode = insn_data[icode].operand[0].mode;
12348 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12349 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
12350 enum machine_mode mode2 = insn_data[icode].operand[3].mode;
12352 if (icode == CODE_FOR_nothing)
12353 /* Builtin not supported on this processor. */
12354 return 0;
12356 /* If we got invalid arguments bail out before generating bad rtl. */
12357 if (arg0 == error_mark_node
12358 || arg1 == error_mark_node
12359 || arg2 == error_mark_node)
12360 return const0_rtx;
12362 /* Check and prepare argument depending on the instruction code.
12364 Note that a switch statement instead of the sequence of tests
12365 would be incorrect as many of the CODE_FOR values could be
12366 CODE_FOR_nothing and that would yield multiple alternatives
12367 with identical values. We'd never reach here at runtime in
12368 this case. */
12369 if (icode == CODE_FOR_altivec_vsldoi_v4sf
12370 || icode == CODE_FOR_altivec_vsldoi_v4si
12371 || icode == CODE_FOR_altivec_vsldoi_v8hi
12372 || icode == CODE_FOR_altivec_vsldoi_v16qi)
12374 /* Only allow 4-bit unsigned literals. */
12375 STRIP_NOPS (arg2);
12376 if (TREE_CODE (arg2) != INTEGER_CST
12377 || TREE_INT_CST_LOW (arg2) & ~0xf)
12379 error ("argument 3 must be a 4-bit unsigned literal");
12380 return const0_rtx;
12383 else if (icode == CODE_FOR_vsx_xxpermdi_v2df
12384 || icode == CODE_FOR_vsx_xxpermdi_v2di
12385 || icode == CODE_FOR_vsx_xxsldwi_v16qi
12386 || icode == CODE_FOR_vsx_xxsldwi_v8hi
12387 || icode == CODE_FOR_vsx_xxsldwi_v4si
12388 || icode == CODE_FOR_vsx_xxsldwi_v4sf
12389 || icode == CODE_FOR_vsx_xxsldwi_v2di
12390 || icode == CODE_FOR_vsx_xxsldwi_v2df)
12392 /* Only allow 2-bit unsigned literals. */
12393 STRIP_NOPS (arg2);
12394 if (TREE_CODE (arg2) != INTEGER_CST
12395 || TREE_INT_CST_LOW (arg2) & ~0x3)
12397 error ("argument 3 must be a 2-bit unsigned literal");
12398 return const0_rtx;
12401 else if (icode == CODE_FOR_vsx_set_v2df
12402 || icode == CODE_FOR_vsx_set_v2di)
12404 /* Only allow 1-bit unsigned literals. */
12405 STRIP_NOPS (arg2);
12406 if (TREE_CODE (arg2) != INTEGER_CST
12407 || TREE_INT_CST_LOW (arg2) & ~0x1)
12409 error ("argument 3 must be a 1-bit unsigned literal");
12410 return const0_rtx;
12413 else if (icode == CODE_FOR_crypto_vshasigmaw
12414 || icode == CODE_FOR_crypto_vshasigmad)
12416 /* Check whether the 2nd and 3rd arguments are integer constants and in
12417 range and prepare arguments. */
12418 STRIP_NOPS (arg1);
12419 if (TREE_CODE (arg1) != INTEGER_CST
12420 || !IN_RANGE (TREE_INT_CST_LOW (arg1), 0, 1))
12422 error ("argument 2 must be 0 or 1");
12423 return const0_rtx;
12426 STRIP_NOPS (arg2);
12427 if (TREE_CODE (arg2) != INTEGER_CST
12428 || !IN_RANGE (TREE_INT_CST_LOW (arg2), 0, 15))
12430 error ("argument 3 must be in the range 0..15");
12431 return const0_rtx;
12435 if (target == 0
12436 || GET_MODE (target) != tmode
12437 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12438 target = gen_reg_rtx (tmode);
12440 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12441 op0 = copy_to_mode_reg (mode0, op0);
12442 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12443 op1 = copy_to_mode_reg (mode1, op1);
12444 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12445 op2 = copy_to_mode_reg (mode2, op2);
12447 if (TARGET_PAIRED_FLOAT && icode == CODE_FOR_selv2sf4)
12448 pat = GEN_FCN (icode) (target, op0, op1, op2, CONST0_RTX (SFmode));
12449 else
12450 pat = GEN_FCN (icode) (target, op0, op1, op2);
12451 if (! pat)
12452 return 0;
12453 emit_insn (pat);
12455 return target;
12458 /* Expand the lvx builtins. */
12459 static rtx
12460 altivec_expand_ld_builtin (tree exp, rtx target, bool *expandedp)
12462 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
12463 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
12464 tree arg0;
12465 enum machine_mode tmode, mode0;
12466 rtx pat, op0;
12467 enum insn_code icode;
12469 switch (fcode)
12471 case ALTIVEC_BUILTIN_LD_INTERNAL_16qi:
12472 icode = CODE_FOR_vector_altivec_load_v16qi;
12473 break;
12474 case ALTIVEC_BUILTIN_LD_INTERNAL_8hi:
12475 icode = CODE_FOR_vector_altivec_load_v8hi;
12476 break;
12477 case ALTIVEC_BUILTIN_LD_INTERNAL_4si:
12478 icode = CODE_FOR_vector_altivec_load_v4si;
12479 break;
12480 case ALTIVEC_BUILTIN_LD_INTERNAL_4sf:
12481 icode = CODE_FOR_vector_altivec_load_v4sf;
12482 break;
12483 case ALTIVEC_BUILTIN_LD_INTERNAL_2df:
12484 icode = CODE_FOR_vector_altivec_load_v2df;
12485 break;
12486 case ALTIVEC_BUILTIN_LD_INTERNAL_2di:
12487 icode = CODE_FOR_vector_altivec_load_v2di;
12488 case ALTIVEC_BUILTIN_LD_INTERNAL_1ti:
12489 icode = CODE_FOR_vector_altivec_load_v1ti;
12490 break;
12491 default:
12492 *expandedp = false;
12493 return NULL_RTX;
12496 *expandedp = true;
12498 arg0 = CALL_EXPR_ARG (exp, 0);
12499 op0 = expand_normal (arg0);
12500 tmode = insn_data[icode].operand[0].mode;
12501 mode0 = insn_data[icode].operand[1].mode;
12503 if (target == 0
12504 || GET_MODE (target) != tmode
12505 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12506 target = gen_reg_rtx (tmode);
12508 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12509 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
12511 pat = GEN_FCN (icode) (target, op0);
12512 if (! pat)
12513 return 0;
12514 emit_insn (pat);
12515 return target;
12518 /* Expand the stvx builtins. */
12519 static rtx
12520 altivec_expand_st_builtin (tree exp, rtx target ATTRIBUTE_UNUSED,
12521 bool *expandedp)
12523 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
12524 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
12525 tree arg0, arg1;
12526 enum machine_mode mode0, mode1;
12527 rtx pat, op0, op1;
12528 enum insn_code icode;
12530 switch (fcode)
12532 case ALTIVEC_BUILTIN_ST_INTERNAL_16qi:
12533 icode = CODE_FOR_vector_altivec_store_v16qi;
12534 break;
12535 case ALTIVEC_BUILTIN_ST_INTERNAL_8hi:
12536 icode = CODE_FOR_vector_altivec_store_v8hi;
12537 break;
12538 case ALTIVEC_BUILTIN_ST_INTERNAL_4si:
12539 icode = CODE_FOR_vector_altivec_store_v4si;
12540 break;
12541 case ALTIVEC_BUILTIN_ST_INTERNAL_4sf:
12542 icode = CODE_FOR_vector_altivec_store_v4sf;
12543 break;
12544 case ALTIVEC_BUILTIN_ST_INTERNAL_2df:
12545 icode = CODE_FOR_vector_altivec_store_v2df;
12546 break;
12547 case ALTIVEC_BUILTIN_ST_INTERNAL_2di:
12548 icode = CODE_FOR_vector_altivec_store_v2di;
12549 case ALTIVEC_BUILTIN_ST_INTERNAL_1ti:
12550 icode = CODE_FOR_vector_altivec_store_v1ti;
12551 break;
12552 default:
12553 *expandedp = false;
12554 return NULL_RTX;
12557 arg0 = CALL_EXPR_ARG (exp, 0);
12558 arg1 = CALL_EXPR_ARG (exp, 1);
12559 op0 = expand_normal (arg0);
12560 op1 = expand_normal (arg1);
12561 mode0 = insn_data[icode].operand[0].mode;
12562 mode1 = insn_data[icode].operand[1].mode;
12564 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
12565 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
12566 if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
12567 op1 = copy_to_mode_reg (mode1, op1);
12569 pat = GEN_FCN (icode) (op0, op1);
12570 if (pat)
12571 emit_insn (pat);
12573 *expandedp = true;
12574 return NULL_RTX;
12577 /* Expand the dst builtins. */
12578 static rtx
12579 altivec_expand_dst_builtin (tree exp, rtx target ATTRIBUTE_UNUSED,
12580 bool *expandedp)
12582 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
12583 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
12584 tree arg0, arg1, arg2;
12585 enum machine_mode mode0, mode1;
12586 rtx pat, op0, op1, op2;
12587 const struct builtin_description *d;
12588 size_t i;
12590 *expandedp = false;
12592 /* Handle DST variants. */
12593 d = bdesc_dst;
12594 for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
12595 if (d->code == fcode)
12597 arg0 = CALL_EXPR_ARG (exp, 0);
12598 arg1 = CALL_EXPR_ARG (exp, 1);
12599 arg2 = CALL_EXPR_ARG (exp, 2);
12600 op0 = expand_normal (arg0);
12601 op1 = expand_normal (arg1);
12602 op2 = expand_normal (arg2);
12603 mode0 = insn_data[d->icode].operand[0].mode;
12604 mode1 = insn_data[d->icode].operand[1].mode;
12606 /* Invalid arguments, bail out before generating bad rtl. */
12607 if (arg0 == error_mark_node
12608 || arg1 == error_mark_node
12609 || arg2 == error_mark_node)
12610 return const0_rtx;
12612 *expandedp = true;
12613 STRIP_NOPS (arg2);
12614 if (TREE_CODE (arg2) != INTEGER_CST
12615 || TREE_INT_CST_LOW (arg2) & ~0x3)
12617 error ("argument to %qs must be a 2-bit unsigned literal", d->name);
12618 return const0_rtx;
12621 if (! (*insn_data[d->icode].operand[0].predicate) (op0, mode0))
12622 op0 = copy_to_mode_reg (Pmode, op0);
12623 if (! (*insn_data[d->icode].operand[1].predicate) (op1, mode1))
12624 op1 = copy_to_mode_reg (mode1, op1);
12626 pat = GEN_FCN (d->icode) (op0, op1, op2);
12627 if (pat != 0)
12628 emit_insn (pat);
12630 return NULL_RTX;
12633 return NULL_RTX;
12636 /* Expand vec_init builtin. */
12637 static rtx
12638 altivec_expand_vec_init_builtin (tree type, tree exp, rtx target)
12640 enum machine_mode tmode = TYPE_MODE (type);
12641 enum machine_mode inner_mode = GET_MODE_INNER (tmode);
12642 int i, n_elt = GET_MODE_NUNITS (tmode);
12644 gcc_assert (VECTOR_MODE_P (tmode));
12645 gcc_assert (n_elt == call_expr_nargs (exp));
12647 if (!target || !register_operand (target, tmode))
12648 target = gen_reg_rtx (tmode);
12650 /* If we have a vector compromised of a single element, such as V1TImode, do
12651 the initialization directly. */
12652 if (n_elt == 1 && GET_MODE_SIZE (tmode) == GET_MODE_SIZE (inner_mode))
12654 rtx x = expand_normal (CALL_EXPR_ARG (exp, 0));
12655 emit_move_insn (target, gen_lowpart (tmode, x));
12657 else
12659 rtvec v = rtvec_alloc (n_elt);
12661 for (i = 0; i < n_elt; ++i)
12663 rtx x = expand_normal (CALL_EXPR_ARG (exp, i));
12664 RTVEC_ELT (v, i) = gen_lowpart (inner_mode, x);
12667 rs6000_expand_vector_init (target, gen_rtx_PARALLEL (tmode, v));
12670 return target;
12673 /* Return the integer constant in ARG. Constrain it to be in the range
12674 of the subparts of VEC_TYPE; issue an error if not. */
12676 static int
12677 get_element_number (tree vec_type, tree arg)
12679 unsigned HOST_WIDE_INT elt, max = TYPE_VECTOR_SUBPARTS (vec_type) - 1;
12681 if (!tree_fits_uhwi_p (arg)
12682 || (elt = tree_to_uhwi (arg), elt > max))
12684 error ("selector must be an integer constant in the range 0..%wi", max);
12685 return 0;
12688 return elt;
12691 /* Expand vec_set builtin. */
12692 static rtx
12693 altivec_expand_vec_set_builtin (tree exp)
12695 enum machine_mode tmode, mode1;
12696 tree arg0, arg1, arg2;
12697 int elt;
12698 rtx op0, op1;
12700 arg0 = CALL_EXPR_ARG (exp, 0);
12701 arg1 = CALL_EXPR_ARG (exp, 1);
12702 arg2 = CALL_EXPR_ARG (exp, 2);
12704 tmode = TYPE_MODE (TREE_TYPE (arg0));
12705 mode1 = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
12706 gcc_assert (VECTOR_MODE_P (tmode));
12708 op0 = expand_expr (arg0, NULL_RTX, tmode, EXPAND_NORMAL);
12709 op1 = expand_expr (arg1, NULL_RTX, mode1, EXPAND_NORMAL);
12710 elt = get_element_number (TREE_TYPE (arg0), arg2);
12712 if (GET_MODE (op1) != mode1 && GET_MODE (op1) != VOIDmode)
12713 op1 = convert_modes (mode1, GET_MODE (op1), op1, true);
12715 op0 = force_reg (tmode, op0);
12716 op1 = force_reg (mode1, op1);
12718 rs6000_expand_vector_set (op0, op1, elt);
12720 return op0;
12723 /* Expand vec_ext builtin. */
12724 static rtx
12725 altivec_expand_vec_ext_builtin (tree exp, rtx target)
12727 enum machine_mode tmode, mode0;
12728 tree arg0, arg1;
12729 int elt;
12730 rtx op0;
12732 arg0 = CALL_EXPR_ARG (exp, 0);
12733 arg1 = CALL_EXPR_ARG (exp, 1);
12735 op0 = expand_normal (arg0);
12736 elt = get_element_number (TREE_TYPE (arg0), arg1);
12738 tmode = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
12739 mode0 = TYPE_MODE (TREE_TYPE (arg0));
12740 gcc_assert (VECTOR_MODE_P (mode0));
12742 op0 = force_reg (mode0, op0);
12744 if (optimize || !target || !register_operand (target, tmode))
12745 target = gen_reg_rtx (tmode);
12747 rs6000_expand_vector_extract (target, op0, elt);
12749 return target;
12752 /* Expand the builtin in EXP and store the result in TARGET. Store
12753 true in *EXPANDEDP if we found a builtin to expand. */
12754 static rtx
12755 altivec_expand_builtin (tree exp, rtx target, bool *expandedp)
12757 const struct builtin_description *d;
12758 size_t i;
12759 enum insn_code icode;
12760 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
12761 tree arg0;
12762 rtx op0, pat;
12763 enum machine_mode tmode, mode0;
12764 enum rs6000_builtins fcode
12765 = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
12767 if (rs6000_overloaded_builtin_p (fcode))
12769 *expandedp = true;
12770 error ("unresolved overload for Altivec builtin %qF", fndecl);
12772 /* Given it is invalid, just generate a normal call. */
12773 return expand_call (exp, target, false);
12776 target = altivec_expand_ld_builtin (exp, target, expandedp);
12777 if (*expandedp)
12778 return target;
12780 target = altivec_expand_st_builtin (exp, target, expandedp);
12781 if (*expandedp)
12782 return target;
12784 target = altivec_expand_dst_builtin (exp, target, expandedp);
12785 if (*expandedp)
12786 return target;
12788 *expandedp = true;
12790 switch (fcode)
12792 case ALTIVEC_BUILTIN_STVX_V2DF:
12793 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v2df, exp);
12794 case ALTIVEC_BUILTIN_STVX_V2DI:
12795 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v2di, exp);
12796 case ALTIVEC_BUILTIN_STVX_V4SF:
12797 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v4sf, exp);
12798 case ALTIVEC_BUILTIN_STVX:
12799 case ALTIVEC_BUILTIN_STVX_V4SI:
12800 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v4si, exp);
12801 case ALTIVEC_BUILTIN_STVX_V8HI:
12802 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v8hi, exp);
12803 case ALTIVEC_BUILTIN_STVX_V16QI:
12804 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v16qi, exp);
12805 case ALTIVEC_BUILTIN_STVEBX:
12806 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvebx, exp);
12807 case ALTIVEC_BUILTIN_STVEHX:
12808 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvehx, exp);
12809 case ALTIVEC_BUILTIN_STVEWX:
12810 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvewx, exp);
12811 case ALTIVEC_BUILTIN_STVXL_V2DF:
12812 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v2df, exp);
12813 case ALTIVEC_BUILTIN_STVXL_V2DI:
12814 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v2di, exp);
12815 case ALTIVEC_BUILTIN_STVXL_V4SF:
12816 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v4sf, exp);
12817 case ALTIVEC_BUILTIN_STVXL:
12818 case ALTIVEC_BUILTIN_STVXL_V4SI:
12819 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v4si, exp);
12820 case ALTIVEC_BUILTIN_STVXL_V8HI:
12821 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v8hi, exp);
12822 case ALTIVEC_BUILTIN_STVXL_V16QI:
12823 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl_v16qi, exp);
12825 case ALTIVEC_BUILTIN_STVLX:
12826 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvlx, exp);
12827 case ALTIVEC_BUILTIN_STVLXL:
12828 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvlxl, exp);
12829 case ALTIVEC_BUILTIN_STVRX:
12830 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvrx, exp);
12831 case ALTIVEC_BUILTIN_STVRXL:
12832 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvrxl, exp);
12834 case VSX_BUILTIN_STXVD2X_V1TI:
12835 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v1ti, exp);
12836 case VSX_BUILTIN_STXVD2X_V2DF:
12837 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v2df, exp);
12838 case VSX_BUILTIN_STXVD2X_V2DI:
12839 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v2di, exp);
12840 case VSX_BUILTIN_STXVW4X_V4SF:
12841 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v4sf, exp);
12842 case VSX_BUILTIN_STXVW4X_V4SI:
12843 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v4si, exp);
12844 case VSX_BUILTIN_STXVW4X_V8HI:
12845 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v8hi, exp);
12846 case VSX_BUILTIN_STXVW4X_V16QI:
12847 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v16qi, exp);
12849 case ALTIVEC_BUILTIN_MFVSCR:
12850 icode = CODE_FOR_altivec_mfvscr;
12851 tmode = insn_data[icode].operand[0].mode;
12853 if (target == 0
12854 || GET_MODE (target) != tmode
12855 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12856 target = gen_reg_rtx (tmode);
12858 pat = GEN_FCN (icode) (target);
12859 if (! pat)
12860 return 0;
12861 emit_insn (pat);
12862 return target;
12864 case ALTIVEC_BUILTIN_MTVSCR:
12865 icode = CODE_FOR_altivec_mtvscr;
12866 arg0 = CALL_EXPR_ARG (exp, 0);
12867 op0 = expand_normal (arg0);
12868 mode0 = insn_data[icode].operand[0].mode;
12870 /* If we got invalid arguments bail out before generating bad rtl. */
12871 if (arg0 == error_mark_node)
12872 return const0_rtx;
12874 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
12875 op0 = copy_to_mode_reg (mode0, op0);
12877 pat = GEN_FCN (icode) (op0);
12878 if (pat)
12879 emit_insn (pat);
12880 return NULL_RTX;
12882 case ALTIVEC_BUILTIN_DSSALL:
12883 emit_insn (gen_altivec_dssall ());
12884 return NULL_RTX;
12886 case ALTIVEC_BUILTIN_DSS:
12887 icode = CODE_FOR_altivec_dss;
12888 arg0 = CALL_EXPR_ARG (exp, 0);
12889 STRIP_NOPS (arg0);
12890 op0 = expand_normal (arg0);
12891 mode0 = insn_data[icode].operand[0].mode;
12893 /* If we got invalid arguments bail out before generating bad rtl. */
12894 if (arg0 == error_mark_node)
12895 return const0_rtx;
12897 if (TREE_CODE (arg0) != INTEGER_CST
12898 || TREE_INT_CST_LOW (arg0) & ~0x3)
12900 error ("argument to dss must be a 2-bit unsigned literal");
12901 return const0_rtx;
12904 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
12905 op0 = copy_to_mode_reg (mode0, op0);
12907 emit_insn (gen_altivec_dss (op0));
12908 return NULL_RTX;
12910 case ALTIVEC_BUILTIN_VEC_INIT_V4SI:
12911 case ALTIVEC_BUILTIN_VEC_INIT_V8HI:
12912 case ALTIVEC_BUILTIN_VEC_INIT_V16QI:
12913 case ALTIVEC_BUILTIN_VEC_INIT_V4SF:
12914 case VSX_BUILTIN_VEC_INIT_V2DF:
12915 case VSX_BUILTIN_VEC_INIT_V2DI:
12916 case VSX_BUILTIN_VEC_INIT_V1TI:
12917 return altivec_expand_vec_init_builtin (TREE_TYPE (exp), exp, target);
12919 case ALTIVEC_BUILTIN_VEC_SET_V4SI:
12920 case ALTIVEC_BUILTIN_VEC_SET_V8HI:
12921 case ALTIVEC_BUILTIN_VEC_SET_V16QI:
12922 case ALTIVEC_BUILTIN_VEC_SET_V4SF:
12923 case VSX_BUILTIN_VEC_SET_V2DF:
12924 case VSX_BUILTIN_VEC_SET_V2DI:
12925 case VSX_BUILTIN_VEC_SET_V1TI:
12926 return altivec_expand_vec_set_builtin (exp);
12928 case ALTIVEC_BUILTIN_VEC_EXT_V4SI:
12929 case ALTIVEC_BUILTIN_VEC_EXT_V8HI:
12930 case ALTIVEC_BUILTIN_VEC_EXT_V16QI:
12931 case ALTIVEC_BUILTIN_VEC_EXT_V4SF:
12932 case VSX_BUILTIN_VEC_EXT_V2DF:
12933 case VSX_BUILTIN_VEC_EXT_V2DI:
12934 case VSX_BUILTIN_VEC_EXT_V1TI:
12935 return altivec_expand_vec_ext_builtin (exp, target);
12937 default:
12938 break;
12939 /* Fall through. */
12942 /* Expand abs* operations. */
12943 d = bdesc_abs;
12944 for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
12945 if (d->code == fcode)
12946 return altivec_expand_abs_builtin (d->icode, exp, target);
12948 /* Expand the AltiVec predicates. */
12949 d = bdesc_altivec_preds;
12950 for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, d++)
12951 if (d->code == fcode)
12952 return altivec_expand_predicate_builtin (d->icode, exp, target);
12954 /* LV* are funky. We initialized them differently. */
12955 switch (fcode)
12957 case ALTIVEC_BUILTIN_LVSL:
12958 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsl,
12959 exp, target, false);
12960 case ALTIVEC_BUILTIN_LVSR:
12961 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsr,
12962 exp, target, false);
12963 case ALTIVEC_BUILTIN_LVEBX:
12964 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvebx,
12965 exp, target, false);
12966 case ALTIVEC_BUILTIN_LVEHX:
12967 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvehx,
12968 exp, target, false);
12969 case ALTIVEC_BUILTIN_LVEWX:
12970 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvewx,
12971 exp, target, false);
12972 case ALTIVEC_BUILTIN_LVXL_V2DF:
12973 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v2df,
12974 exp, target, false);
12975 case ALTIVEC_BUILTIN_LVXL_V2DI:
12976 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v2di,
12977 exp, target, false);
12978 case ALTIVEC_BUILTIN_LVXL_V4SF:
12979 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v4sf,
12980 exp, target, false);
12981 case ALTIVEC_BUILTIN_LVXL:
12982 case ALTIVEC_BUILTIN_LVXL_V4SI:
12983 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v4si,
12984 exp, target, false);
12985 case ALTIVEC_BUILTIN_LVXL_V8HI:
12986 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v8hi,
12987 exp, target, false);
12988 case ALTIVEC_BUILTIN_LVXL_V16QI:
12989 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl_v16qi,
12990 exp, target, false);
12991 case ALTIVEC_BUILTIN_LVX_V2DF:
12992 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v2df,
12993 exp, target, false);
12994 case ALTIVEC_BUILTIN_LVX_V2DI:
12995 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v2di,
12996 exp, target, false);
12997 case ALTIVEC_BUILTIN_LVX_V4SF:
12998 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v4sf,
12999 exp, target, false);
13000 case ALTIVEC_BUILTIN_LVX:
13001 case ALTIVEC_BUILTIN_LVX_V4SI:
13002 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v4si,
13003 exp, target, false);
13004 case ALTIVEC_BUILTIN_LVX_V8HI:
13005 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v8hi,
13006 exp, target, false);
13007 case ALTIVEC_BUILTIN_LVX_V16QI:
13008 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v16qi,
13009 exp, target, false);
13010 case ALTIVEC_BUILTIN_LVLX:
13011 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvlx,
13012 exp, target, true);
13013 case ALTIVEC_BUILTIN_LVLXL:
13014 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvlxl,
13015 exp, target, true);
13016 case ALTIVEC_BUILTIN_LVRX:
13017 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvrx,
13018 exp, target, true);
13019 case ALTIVEC_BUILTIN_LVRXL:
13020 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvrxl,
13021 exp, target, true);
13022 case VSX_BUILTIN_LXVD2X_V1TI:
13023 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v1ti,
13024 exp, target, false);
13025 case VSX_BUILTIN_LXVD2X_V2DF:
13026 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v2df,
13027 exp, target, false);
13028 case VSX_BUILTIN_LXVD2X_V2DI:
13029 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v2di,
13030 exp, target, false);
13031 case VSX_BUILTIN_LXVW4X_V4SF:
13032 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v4sf,
13033 exp, target, false);
13034 case VSX_BUILTIN_LXVW4X_V4SI:
13035 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v4si,
13036 exp, target, false);
13037 case VSX_BUILTIN_LXVW4X_V8HI:
13038 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v8hi,
13039 exp, target, false);
13040 case VSX_BUILTIN_LXVW4X_V16QI:
13041 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v16qi,
13042 exp, target, false);
13043 break;
13044 default:
13045 break;
13046 /* Fall through. */
13049 *expandedp = false;
13050 return NULL_RTX;
13053 /* Expand the builtin in EXP and store the result in TARGET. Store
13054 true in *EXPANDEDP if we found a builtin to expand. */
13055 static rtx
13056 paired_expand_builtin (tree exp, rtx target, bool * expandedp)
13058 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
13059 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
13060 const struct builtin_description *d;
13061 size_t i;
13063 *expandedp = true;
13065 switch (fcode)
13067 case PAIRED_BUILTIN_STX:
13068 return paired_expand_stv_builtin (CODE_FOR_paired_stx, exp);
13069 case PAIRED_BUILTIN_LX:
13070 return paired_expand_lv_builtin (CODE_FOR_paired_lx, exp, target);
13071 default:
13072 break;
13073 /* Fall through. */
13076 /* Expand the paired predicates. */
13077 d = bdesc_paired_preds;
13078 for (i = 0; i < ARRAY_SIZE (bdesc_paired_preds); i++, d++)
13079 if (d->code == fcode)
13080 return paired_expand_predicate_builtin (d->icode, exp, target);
13082 *expandedp = false;
13083 return NULL_RTX;
13086 /* Binops that need to be initialized manually, but can be expanded
13087 automagically by rs6000_expand_binop_builtin. */
13088 static const struct builtin_description bdesc_2arg_spe[] =
13090 { RS6000_BTM_SPE, CODE_FOR_spe_evlddx, "__builtin_spe_evlddx", SPE_BUILTIN_EVLDDX },
13091 { RS6000_BTM_SPE, CODE_FOR_spe_evldwx, "__builtin_spe_evldwx", SPE_BUILTIN_EVLDWX },
13092 { RS6000_BTM_SPE, CODE_FOR_spe_evldhx, "__builtin_spe_evldhx", SPE_BUILTIN_EVLDHX },
13093 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhex, "__builtin_spe_evlwhex", SPE_BUILTIN_EVLWHEX },
13094 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhoux, "__builtin_spe_evlwhoux", SPE_BUILTIN_EVLWHOUX },
13095 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhosx, "__builtin_spe_evlwhosx", SPE_BUILTIN_EVLWHOSX },
13096 { RS6000_BTM_SPE, CODE_FOR_spe_evlwwsplatx, "__builtin_spe_evlwwsplatx", SPE_BUILTIN_EVLWWSPLATX },
13097 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhsplatx, "__builtin_spe_evlwhsplatx", SPE_BUILTIN_EVLWHSPLATX },
13098 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhesplatx, "__builtin_spe_evlhhesplatx", SPE_BUILTIN_EVLHHESPLATX },
13099 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhousplatx, "__builtin_spe_evlhhousplatx", SPE_BUILTIN_EVLHHOUSPLATX },
13100 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhossplatx, "__builtin_spe_evlhhossplatx", SPE_BUILTIN_EVLHHOSSPLATX },
13101 { RS6000_BTM_SPE, CODE_FOR_spe_evldd, "__builtin_spe_evldd", SPE_BUILTIN_EVLDD },
13102 { RS6000_BTM_SPE, CODE_FOR_spe_evldw, "__builtin_spe_evldw", SPE_BUILTIN_EVLDW },
13103 { RS6000_BTM_SPE, CODE_FOR_spe_evldh, "__builtin_spe_evldh", SPE_BUILTIN_EVLDH },
13104 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhe, "__builtin_spe_evlwhe", SPE_BUILTIN_EVLWHE },
13105 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhou, "__builtin_spe_evlwhou", SPE_BUILTIN_EVLWHOU },
13106 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhos, "__builtin_spe_evlwhos", SPE_BUILTIN_EVLWHOS },
13107 { RS6000_BTM_SPE, CODE_FOR_spe_evlwwsplat, "__builtin_spe_evlwwsplat", SPE_BUILTIN_EVLWWSPLAT },
13108 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhsplat, "__builtin_spe_evlwhsplat", SPE_BUILTIN_EVLWHSPLAT },
13109 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhesplat, "__builtin_spe_evlhhesplat", SPE_BUILTIN_EVLHHESPLAT },
13110 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhousplat, "__builtin_spe_evlhhousplat", SPE_BUILTIN_EVLHHOUSPLAT },
13111 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhossplat, "__builtin_spe_evlhhossplat", SPE_BUILTIN_EVLHHOSSPLAT }
13114 /* Expand the builtin in EXP and store the result in TARGET. Store
13115 true in *EXPANDEDP if we found a builtin to expand.
13117 This expands the SPE builtins that are not simple unary and binary
13118 operations. */
13119 static rtx
13120 spe_expand_builtin (tree exp, rtx target, bool *expandedp)
13122 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
13123 tree arg1, arg0;
13124 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
13125 enum insn_code icode;
13126 enum machine_mode tmode, mode0;
13127 rtx pat, op0;
13128 const struct builtin_description *d;
13129 size_t i;
13131 *expandedp = true;
13133 /* Syntax check for a 5-bit unsigned immediate. */
13134 switch (fcode)
13136 case SPE_BUILTIN_EVSTDD:
13137 case SPE_BUILTIN_EVSTDH:
13138 case SPE_BUILTIN_EVSTDW:
13139 case SPE_BUILTIN_EVSTWHE:
13140 case SPE_BUILTIN_EVSTWHO:
13141 case SPE_BUILTIN_EVSTWWE:
13142 case SPE_BUILTIN_EVSTWWO:
13143 arg1 = CALL_EXPR_ARG (exp, 2);
13144 if (TREE_CODE (arg1) != INTEGER_CST
13145 || TREE_INT_CST_LOW (arg1) & ~0x1f)
13147 error ("argument 2 must be a 5-bit unsigned literal");
13148 return const0_rtx;
13150 break;
13151 default:
13152 break;
13155 /* The evsplat*i instructions are not quite generic. */
13156 switch (fcode)
13158 case SPE_BUILTIN_EVSPLATFI:
13159 return rs6000_expand_unop_builtin (CODE_FOR_spe_evsplatfi,
13160 exp, target);
13161 case SPE_BUILTIN_EVSPLATI:
13162 return rs6000_expand_unop_builtin (CODE_FOR_spe_evsplati,
13163 exp, target);
13164 default:
13165 break;
13168 d = bdesc_2arg_spe;
13169 for (i = 0; i < ARRAY_SIZE (bdesc_2arg_spe); ++i, ++d)
13170 if (d->code == fcode)
13171 return rs6000_expand_binop_builtin (d->icode, exp, target);
13173 d = bdesc_spe_predicates;
13174 for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, ++d)
13175 if (d->code == fcode)
13176 return spe_expand_predicate_builtin (d->icode, exp, target);
13178 d = bdesc_spe_evsel;
13179 for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, ++d)
13180 if (d->code == fcode)
13181 return spe_expand_evsel_builtin (d->icode, exp, target);
13183 switch (fcode)
13185 case SPE_BUILTIN_EVSTDDX:
13186 return spe_expand_stv_builtin (CODE_FOR_spe_evstddx, exp);
13187 case SPE_BUILTIN_EVSTDHX:
13188 return spe_expand_stv_builtin (CODE_FOR_spe_evstdhx, exp);
13189 case SPE_BUILTIN_EVSTDWX:
13190 return spe_expand_stv_builtin (CODE_FOR_spe_evstdwx, exp);
13191 case SPE_BUILTIN_EVSTWHEX:
13192 return spe_expand_stv_builtin (CODE_FOR_spe_evstwhex, exp);
13193 case SPE_BUILTIN_EVSTWHOX:
13194 return spe_expand_stv_builtin (CODE_FOR_spe_evstwhox, exp);
13195 case SPE_BUILTIN_EVSTWWEX:
13196 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwex, exp);
13197 case SPE_BUILTIN_EVSTWWOX:
13198 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwox, exp);
13199 case SPE_BUILTIN_EVSTDD:
13200 return spe_expand_stv_builtin (CODE_FOR_spe_evstdd, exp);
13201 case SPE_BUILTIN_EVSTDH:
13202 return spe_expand_stv_builtin (CODE_FOR_spe_evstdh, exp);
13203 case SPE_BUILTIN_EVSTDW:
13204 return spe_expand_stv_builtin (CODE_FOR_spe_evstdw, exp);
13205 case SPE_BUILTIN_EVSTWHE:
13206 return spe_expand_stv_builtin (CODE_FOR_spe_evstwhe, exp);
13207 case SPE_BUILTIN_EVSTWHO:
13208 return spe_expand_stv_builtin (CODE_FOR_spe_evstwho, exp);
13209 case SPE_BUILTIN_EVSTWWE:
13210 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwe, exp);
13211 case SPE_BUILTIN_EVSTWWO:
13212 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwo, exp);
13213 case SPE_BUILTIN_MFSPEFSCR:
13214 icode = CODE_FOR_spe_mfspefscr;
13215 tmode = insn_data[icode].operand[0].mode;
13217 if (target == 0
13218 || GET_MODE (target) != tmode
13219 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13220 target = gen_reg_rtx (tmode);
13222 pat = GEN_FCN (icode) (target);
13223 if (! pat)
13224 return 0;
13225 emit_insn (pat);
13226 return target;
13227 case SPE_BUILTIN_MTSPEFSCR:
13228 icode = CODE_FOR_spe_mtspefscr;
13229 arg0 = CALL_EXPR_ARG (exp, 0);
13230 op0 = expand_normal (arg0);
13231 mode0 = insn_data[icode].operand[0].mode;
13233 if (arg0 == error_mark_node)
13234 return const0_rtx;
13236 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
13237 op0 = copy_to_mode_reg (mode0, op0);
13239 pat = GEN_FCN (icode) (op0);
13240 if (pat)
13241 emit_insn (pat);
13242 return NULL_RTX;
13243 default:
13244 break;
13247 *expandedp = false;
13248 return NULL_RTX;
13251 static rtx
13252 paired_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
13254 rtx pat, scratch, tmp;
13255 tree form = CALL_EXPR_ARG (exp, 0);
13256 tree arg0 = CALL_EXPR_ARG (exp, 1);
13257 tree arg1 = CALL_EXPR_ARG (exp, 2);
13258 rtx op0 = expand_normal (arg0);
13259 rtx op1 = expand_normal (arg1);
13260 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
13261 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
13262 int form_int;
13263 enum rtx_code code;
13265 if (TREE_CODE (form) != INTEGER_CST)
13267 error ("argument 1 of __builtin_paired_predicate must be a constant");
13268 return const0_rtx;
13270 else
13271 form_int = TREE_INT_CST_LOW (form);
13273 gcc_assert (mode0 == mode1);
13275 if (arg0 == error_mark_node || arg1 == error_mark_node)
13276 return const0_rtx;
13278 if (target == 0
13279 || GET_MODE (target) != SImode
13280 || !(*insn_data[icode].operand[0].predicate) (target, SImode))
13281 target = gen_reg_rtx (SImode);
13282 if (!(*insn_data[icode].operand[1].predicate) (op0, mode0))
13283 op0 = copy_to_mode_reg (mode0, op0);
13284 if (!(*insn_data[icode].operand[2].predicate) (op1, mode1))
13285 op1 = copy_to_mode_reg (mode1, op1);
13287 scratch = gen_reg_rtx (CCFPmode);
13289 pat = GEN_FCN (icode) (scratch, op0, op1);
13290 if (!pat)
13291 return const0_rtx;
13293 emit_insn (pat);
13295 switch (form_int)
13297 /* LT bit. */
13298 case 0:
13299 code = LT;
13300 break;
13301 /* GT bit. */
13302 case 1:
13303 code = GT;
13304 break;
13305 /* EQ bit. */
13306 case 2:
13307 code = EQ;
13308 break;
13309 /* UN bit. */
13310 case 3:
13311 emit_insn (gen_move_from_CR_ov_bit (target, scratch));
13312 return target;
13313 default:
13314 error ("argument 1 of __builtin_paired_predicate is out of range");
13315 return const0_rtx;
13318 tmp = gen_rtx_fmt_ee (code, SImode, scratch, const0_rtx);
13319 emit_move_insn (target, tmp);
13320 return target;
13323 static rtx
13324 spe_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
13326 rtx pat, scratch, tmp;
13327 tree form = CALL_EXPR_ARG (exp, 0);
13328 tree arg0 = CALL_EXPR_ARG (exp, 1);
13329 tree arg1 = CALL_EXPR_ARG (exp, 2);
13330 rtx op0 = expand_normal (arg0);
13331 rtx op1 = expand_normal (arg1);
13332 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
13333 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
13334 int form_int;
13335 enum rtx_code code;
13337 if (TREE_CODE (form) != INTEGER_CST)
13339 error ("argument 1 of __builtin_spe_predicate must be a constant");
13340 return const0_rtx;
13342 else
13343 form_int = TREE_INT_CST_LOW (form);
13345 gcc_assert (mode0 == mode1);
13347 if (arg0 == error_mark_node || arg1 == error_mark_node)
13348 return const0_rtx;
13350 if (target == 0
13351 || GET_MODE (target) != SImode
13352 || ! (*insn_data[icode].operand[0].predicate) (target, SImode))
13353 target = gen_reg_rtx (SImode);
13355 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13356 op0 = copy_to_mode_reg (mode0, op0);
13357 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
13358 op1 = copy_to_mode_reg (mode1, op1);
13360 scratch = gen_reg_rtx (CCmode);
13362 pat = GEN_FCN (icode) (scratch, op0, op1);
13363 if (! pat)
13364 return const0_rtx;
13365 emit_insn (pat);
13367 /* There are 4 variants for each predicate: _any_, _all_, _upper_,
13368 _lower_. We use one compare, but look in different bits of the
13369 CR for each variant.
13371 There are 2 elements in each SPE simd type (upper/lower). The CR
13372 bits are set as follows:
13374 BIT0 | BIT 1 | BIT 2 | BIT 3
13375 U | L | (U | L) | (U & L)
13377 So, for an "all" relationship, BIT 3 would be set.
13378 For an "any" relationship, BIT 2 would be set. Etc.
13380 Following traditional nomenclature, these bits map to:
13382 BIT0 | BIT 1 | BIT 2 | BIT 3
13383 LT | GT | EQ | OV
13385 Later, we will generate rtl to look in the LT/EQ/EQ/OV bits.
13388 switch (form_int)
13390 /* All variant. OV bit. */
13391 case 0:
13392 /* We need to get to the OV bit, which is the ORDERED bit. We
13393 could generate (ordered:SI (reg:CC xx) (const_int 0)), but
13394 that's ugly and will make validate_condition_mode die.
13395 So let's just use another pattern. */
13396 emit_insn (gen_move_from_CR_ov_bit (target, scratch));
13397 return target;
13398 /* Any variant. EQ bit. */
13399 case 1:
13400 code = EQ;
13401 break;
13402 /* Upper variant. LT bit. */
13403 case 2:
13404 code = LT;
13405 break;
13406 /* Lower variant. GT bit. */
13407 case 3:
13408 code = GT;
13409 break;
13410 default:
13411 error ("argument 1 of __builtin_spe_predicate is out of range");
13412 return const0_rtx;
13415 tmp = gen_rtx_fmt_ee (code, SImode, scratch, const0_rtx);
13416 emit_move_insn (target, tmp);
13418 return target;
13421 /* The evsel builtins look like this:
13423 e = __builtin_spe_evsel_OP (a, b, c, d);
13425 and work like this:
13427 e[upper] = a[upper] *OP* b[upper] ? c[upper] : d[upper];
13428 e[lower] = a[lower] *OP* b[lower] ? c[lower] : d[lower];
13431 static rtx
13432 spe_expand_evsel_builtin (enum insn_code icode, tree exp, rtx target)
13434 rtx pat, scratch;
13435 tree arg0 = CALL_EXPR_ARG (exp, 0);
13436 tree arg1 = CALL_EXPR_ARG (exp, 1);
13437 tree arg2 = CALL_EXPR_ARG (exp, 2);
13438 tree arg3 = CALL_EXPR_ARG (exp, 3);
13439 rtx op0 = expand_normal (arg0);
13440 rtx op1 = expand_normal (arg1);
13441 rtx op2 = expand_normal (arg2);
13442 rtx op3 = expand_normal (arg3);
13443 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
13444 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
13446 gcc_assert (mode0 == mode1);
13448 if (arg0 == error_mark_node || arg1 == error_mark_node
13449 || arg2 == error_mark_node || arg3 == error_mark_node)
13450 return const0_rtx;
13452 if (target == 0
13453 || GET_MODE (target) != mode0
13454 || ! (*insn_data[icode].operand[0].predicate) (target, mode0))
13455 target = gen_reg_rtx (mode0);
13457 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13458 op0 = copy_to_mode_reg (mode0, op0);
13459 if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
13460 op1 = copy_to_mode_reg (mode0, op1);
13461 if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
13462 op2 = copy_to_mode_reg (mode0, op2);
13463 if (! (*insn_data[icode].operand[1].predicate) (op3, mode1))
13464 op3 = copy_to_mode_reg (mode0, op3);
13466 /* Generate the compare. */
13467 scratch = gen_reg_rtx (CCmode);
13468 pat = GEN_FCN (icode) (scratch, op0, op1);
13469 if (! pat)
13470 return const0_rtx;
13471 emit_insn (pat);
13473 if (mode0 == V2SImode)
13474 emit_insn (gen_spe_evsel (target, op2, op3, scratch));
13475 else
13476 emit_insn (gen_spe_evsel_fs (target, op2, op3, scratch));
13478 return target;
13481 /* Raise an error message for a builtin function that is called without the
13482 appropriate target options being set. */
13484 static void
13485 rs6000_invalid_builtin (enum rs6000_builtins fncode)
13487 size_t uns_fncode = (size_t)fncode;
13488 const char *name = rs6000_builtin_info[uns_fncode].name;
13489 HOST_WIDE_INT fnmask = rs6000_builtin_info[uns_fncode].mask;
13491 gcc_assert (name != NULL);
13492 if ((fnmask & RS6000_BTM_CELL) != 0)
13493 error ("Builtin function %s is only valid for the cell processor", name);
13494 else if ((fnmask & RS6000_BTM_VSX) != 0)
13495 error ("Builtin function %s requires the -mvsx option", name);
13496 else if ((fnmask & RS6000_BTM_HTM) != 0)
13497 error ("Builtin function %s requires the -mhtm option", name);
13498 else if ((fnmask & RS6000_BTM_ALTIVEC) != 0)
13499 error ("Builtin function %s requires the -maltivec option", name);
13500 else if ((fnmask & RS6000_BTM_PAIRED) != 0)
13501 error ("Builtin function %s requires the -mpaired option", name);
13502 else if ((fnmask & RS6000_BTM_SPE) != 0)
13503 error ("Builtin function %s requires the -mspe option", name);
13504 else
13505 error ("Builtin function %s is not supported with the current options",
13506 name);
13509 /* Expand an expression EXP that calls a built-in function,
13510 with result going to TARGET if that's convenient
13511 (and in mode MODE if that's convenient).
13512 SUBTARGET may be used as the target for computing one of EXP's operands.
13513 IGNORE is nonzero if the value is to be ignored. */
13515 static rtx
13516 rs6000_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
13517 enum machine_mode mode ATTRIBUTE_UNUSED,
13518 int ignore ATTRIBUTE_UNUSED)
13520 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
13521 enum rs6000_builtins fcode
13522 = (enum rs6000_builtins)DECL_FUNCTION_CODE (fndecl);
13523 size_t uns_fcode = (size_t)fcode;
13524 const struct builtin_description *d;
13525 size_t i;
13526 rtx ret;
13527 bool success;
13528 HOST_WIDE_INT mask = rs6000_builtin_info[uns_fcode].mask;
13529 bool func_valid_p = ((rs6000_builtin_mask & mask) == mask);
13531 if (TARGET_DEBUG_BUILTIN)
13533 enum insn_code icode = rs6000_builtin_info[uns_fcode].icode;
13534 const char *name1 = rs6000_builtin_info[uns_fcode].name;
13535 const char *name2 = ((icode != CODE_FOR_nothing)
13536 ? get_insn_name ((int)icode)
13537 : "nothing");
13538 const char *name3;
13540 switch (rs6000_builtin_info[uns_fcode].attr & RS6000_BTC_TYPE_MASK)
13542 default: name3 = "unknown"; break;
13543 case RS6000_BTC_SPECIAL: name3 = "special"; break;
13544 case RS6000_BTC_UNARY: name3 = "unary"; break;
13545 case RS6000_BTC_BINARY: name3 = "binary"; break;
13546 case RS6000_BTC_TERNARY: name3 = "ternary"; break;
13547 case RS6000_BTC_PREDICATE: name3 = "predicate"; break;
13548 case RS6000_BTC_ABS: name3 = "abs"; break;
13549 case RS6000_BTC_EVSEL: name3 = "evsel"; break;
13550 case RS6000_BTC_DST: name3 = "dst"; break;
13554 fprintf (stderr,
13555 "rs6000_expand_builtin, %s (%d), insn = %s (%d), type=%s%s\n",
13556 (name1) ? name1 : "---", fcode,
13557 (name2) ? name2 : "---", (int)icode,
13558 name3,
13559 func_valid_p ? "" : ", not valid");
13562 if (!func_valid_p)
13564 rs6000_invalid_builtin (fcode);
13566 /* Given it is invalid, just generate a normal call. */
13567 return expand_call (exp, target, ignore);
13570 switch (fcode)
13572 case RS6000_BUILTIN_RECIP:
13573 return rs6000_expand_binop_builtin (CODE_FOR_recipdf3, exp, target);
13575 case RS6000_BUILTIN_RECIPF:
13576 return rs6000_expand_binop_builtin (CODE_FOR_recipsf3, exp, target);
13578 case RS6000_BUILTIN_RSQRTF:
13579 return rs6000_expand_unop_builtin (CODE_FOR_rsqrtsf2, exp, target);
13581 case RS6000_BUILTIN_RSQRT:
13582 return rs6000_expand_unop_builtin (CODE_FOR_rsqrtdf2, exp, target);
13584 case POWER7_BUILTIN_BPERMD:
13585 return rs6000_expand_binop_builtin (((TARGET_64BIT)
13586 ? CODE_FOR_bpermd_di
13587 : CODE_FOR_bpermd_si), exp, target);
13589 case RS6000_BUILTIN_GET_TB:
13590 return rs6000_expand_zeroop_builtin (CODE_FOR_rs6000_get_timebase,
13591 target);
13593 case RS6000_BUILTIN_MFTB:
13594 return rs6000_expand_zeroop_builtin (((TARGET_64BIT)
13595 ? CODE_FOR_rs6000_mftb_di
13596 : CODE_FOR_rs6000_mftb_si),
13597 target);
13599 case RS6000_BUILTIN_MFFS:
13600 return rs6000_expand_zeroop_builtin (CODE_FOR_rs6000_mffs, target);
13602 case RS6000_BUILTIN_MTFSF:
13603 return rs6000_expand_mtfsf_builtin (CODE_FOR_rs6000_mtfsf, exp);
13605 case ALTIVEC_BUILTIN_MASK_FOR_LOAD:
13606 case ALTIVEC_BUILTIN_MASK_FOR_STORE:
13608 int icode = (BYTES_BIG_ENDIAN ? (int) CODE_FOR_altivec_lvsr
13609 : (int) CODE_FOR_altivec_lvsl);
13610 enum machine_mode tmode = insn_data[icode].operand[0].mode;
13611 enum machine_mode mode = insn_data[icode].operand[1].mode;
13612 tree arg;
13613 rtx op, addr, pat;
13615 gcc_assert (TARGET_ALTIVEC);
13617 arg = CALL_EXPR_ARG (exp, 0);
13618 gcc_assert (POINTER_TYPE_P (TREE_TYPE (arg)));
13619 op = expand_expr (arg, NULL_RTX, Pmode, EXPAND_NORMAL);
13620 addr = memory_address (mode, op);
13621 if (fcode == ALTIVEC_BUILTIN_MASK_FOR_STORE)
13622 op = addr;
13623 else
13625 /* For the load case need to negate the address. */
13626 op = gen_reg_rtx (GET_MODE (addr));
13627 emit_insn (gen_rtx_SET (VOIDmode, op,
13628 gen_rtx_NEG (GET_MODE (addr), addr)));
13630 op = gen_rtx_MEM (mode, op);
13632 if (target == 0
13633 || GET_MODE (target) != tmode
13634 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13635 target = gen_reg_rtx (tmode);
13637 /*pat = gen_altivec_lvsr (target, op);*/
13638 pat = GEN_FCN (icode) (target, op);
13639 if (!pat)
13640 return 0;
13641 emit_insn (pat);
13643 return target;
13646 case ALTIVEC_BUILTIN_VCFUX:
13647 case ALTIVEC_BUILTIN_VCFSX:
13648 case ALTIVEC_BUILTIN_VCTUXS:
13649 case ALTIVEC_BUILTIN_VCTSXS:
13650 /* FIXME: There's got to be a nicer way to handle this case than
13651 constructing a new CALL_EXPR. */
13652 if (call_expr_nargs (exp) == 1)
13654 exp = build_call_nary (TREE_TYPE (exp), CALL_EXPR_FN (exp),
13655 2, CALL_EXPR_ARG (exp, 0), integer_zero_node);
13657 break;
13659 default:
13660 break;
13663 if (TARGET_ALTIVEC)
13665 ret = altivec_expand_builtin (exp, target, &success);
13667 if (success)
13668 return ret;
13670 if (TARGET_SPE)
13672 ret = spe_expand_builtin (exp, target, &success);
13674 if (success)
13675 return ret;
13677 if (TARGET_PAIRED_FLOAT)
13679 ret = paired_expand_builtin (exp, target, &success);
13681 if (success)
13682 return ret;
13684 if (TARGET_HTM)
13686 ret = htm_expand_builtin (exp, target, &success);
13688 if (success)
13689 return ret;
13692 gcc_assert (TARGET_ALTIVEC || TARGET_VSX || TARGET_SPE || TARGET_PAIRED_FLOAT);
13694 /* Handle simple unary operations. */
13695 d = bdesc_1arg;
13696 for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
13697 if (d->code == fcode)
13698 return rs6000_expand_unop_builtin (d->icode, exp, target);
13700 /* Handle simple binary operations. */
13701 d = bdesc_2arg;
13702 for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
13703 if (d->code == fcode)
13704 return rs6000_expand_binop_builtin (d->icode, exp, target);
13706 /* Handle simple ternary operations. */
13707 d = bdesc_3arg;
13708 for (i = 0; i < ARRAY_SIZE (bdesc_3arg); i++, d++)
13709 if (d->code == fcode)
13710 return rs6000_expand_ternop_builtin (d->icode, exp, target);
13712 gcc_unreachable ();
13715 static void
13716 rs6000_init_builtins (void)
13718 tree tdecl;
13719 tree ftype;
13720 enum machine_mode mode;
13722 if (TARGET_DEBUG_BUILTIN)
13723 fprintf (stderr, "rs6000_init_builtins%s%s%s%s\n",
13724 (TARGET_PAIRED_FLOAT) ? ", paired" : "",
13725 (TARGET_SPE) ? ", spe" : "",
13726 (TARGET_ALTIVEC) ? ", altivec" : "",
13727 (TARGET_VSX) ? ", vsx" : "");
13729 V2SI_type_node = build_vector_type (intSI_type_node, 2);
13730 V2SF_type_node = build_vector_type (float_type_node, 2);
13731 V2DI_type_node = build_vector_type (intDI_type_node, 2);
13732 V2DF_type_node = build_vector_type (double_type_node, 2);
13733 V4HI_type_node = build_vector_type (intHI_type_node, 4);
13734 V4SI_type_node = build_vector_type (intSI_type_node, 4);
13735 V4SF_type_node = build_vector_type (float_type_node, 4);
13736 V8HI_type_node = build_vector_type (intHI_type_node, 8);
13737 V16QI_type_node = build_vector_type (intQI_type_node, 16);
13739 unsigned_V16QI_type_node = build_vector_type (unsigned_intQI_type_node, 16);
13740 unsigned_V8HI_type_node = build_vector_type (unsigned_intHI_type_node, 8);
13741 unsigned_V4SI_type_node = build_vector_type (unsigned_intSI_type_node, 4);
13742 unsigned_V2DI_type_node = build_vector_type (unsigned_intDI_type_node, 2);
13744 opaque_V2SF_type_node = build_opaque_vector_type (float_type_node, 2);
13745 opaque_V2SI_type_node = build_opaque_vector_type (intSI_type_node, 2);
13746 opaque_p_V2SI_type_node = build_pointer_type (opaque_V2SI_type_node);
13747 opaque_V4SI_type_node = build_opaque_vector_type (intSI_type_node, 4);
13749 /* We use V1TI mode as a special container to hold __int128_t items that
13750 must live in VSX registers. */
13751 if (intTI_type_node)
13753 V1TI_type_node = build_vector_type (intTI_type_node, 1);
13754 unsigned_V1TI_type_node = build_vector_type (unsigned_intTI_type_node, 1);
13757 /* The 'vector bool ...' types must be kept distinct from 'vector unsigned ...'
13758 types, especially in C++ land. Similarly, 'vector pixel' is distinct from
13759 'vector unsigned short'. */
13761 bool_char_type_node = build_distinct_type_copy (unsigned_intQI_type_node);
13762 bool_short_type_node = build_distinct_type_copy (unsigned_intHI_type_node);
13763 bool_int_type_node = build_distinct_type_copy (unsigned_intSI_type_node);
13764 bool_long_type_node = build_distinct_type_copy (unsigned_intDI_type_node);
13765 pixel_type_node = build_distinct_type_copy (unsigned_intHI_type_node);
13767 long_integer_type_internal_node = long_integer_type_node;
13768 long_unsigned_type_internal_node = long_unsigned_type_node;
13769 long_long_integer_type_internal_node = long_long_integer_type_node;
13770 long_long_unsigned_type_internal_node = long_long_unsigned_type_node;
13771 intQI_type_internal_node = intQI_type_node;
13772 uintQI_type_internal_node = unsigned_intQI_type_node;
13773 intHI_type_internal_node = intHI_type_node;
13774 uintHI_type_internal_node = unsigned_intHI_type_node;
13775 intSI_type_internal_node = intSI_type_node;
13776 uintSI_type_internal_node = unsigned_intSI_type_node;
13777 intDI_type_internal_node = intDI_type_node;
13778 uintDI_type_internal_node = unsigned_intDI_type_node;
13779 intTI_type_internal_node = intTI_type_node;
13780 uintTI_type_internal_node = unsigned_intTI_type_node;
13781 float_type_internal_node = float_type_node;
13782 double_type_internal_node = double_type_node;
13783 void_type_internal_node = void_type_node;
13785 /* Initialize the modes for builtin_function_type, mapping a machine mode to
13786 tree type node. */
13787 builtin_mode_to_type[QImode][0] = integer_type_node;
13788 builtin_mode_to_type[HImode][0] = integer_type_node;
13789 builtin_mode_to_type[SImode][0] = intSI_type_node;
13790 builtin_mode_to_type[SImode][1] = unsigned_intSI_type_node;
13791 builtin_mode_to_type[DImode][0] = intDI_type_node;
13792 builtin_mode_to_type[DImode][1] = unsigned_intDI_type_node;
13793 builtin_mode_to_type[TImode][0] = intTI_type_node;
13794 builtin_mode_to_type[TImode][1] = unsigned_intTI_type_node;
13795 builtin_mode_to_type[SFmode][0] = float_type_node;
13796 builtin_mode_to_type[DFmode][0] = double_type_node;
13797 builtin_mode_to_type[V1TImode][0] = V1TI_type_node;
13798 builtin_mode_to_type[V1TImode][1] = unsigned_V1TI_type_node;
13799 builtin_mode_to_type[V2SImode][0] = V2SI_type_node;
13800 builtin_mode_to_type[V2SFmode][0] = V2SF_type_node;
13801 builtin_mode_to_type[V2DImode][0] = V2DI_type_node;
13802 builtin_mode_to_type[V2DImode][1] = unsigned_V2DI_type_node;
13803 builtin_mode_to_type[V2DFmode][0] = V2DF_type_node;
13804 builtin_mode_to_type[V4HImode][0] = V4HI_type_node;
13805 builtin_mode_to_type[V4SImode][0] = V4SI_type_node;
13806 builtin_mode_to_type[V4SImode][1] = unsigned_V4SI_type_node;
13807 builtin_mode_to_type[V4SFmode][0] = V4SF_type_node;
13808 builtin_mode_to_type[V8HImode][0] = V8HI_type_node;
13809 builtin_mode_to_type[V8HImode][1] = unsigned_V8HI_type_node;
13810 builtin_mode_to_type[V16QImode][0] = V16QI_type_node;
13811 builtin_mode_to_type[V16QImode][1] = unsigned_V16QI_type_node;
13813 tdecl = add_builtin_type ("__bool char", bool_char_type_node);
13814 TYPE_NAME (bool_char_type_node) = tdecl;
13816 tdecl = add_builtin_type ("__bool short", bool_short_type_node);
13817 TYPE_NAME (bool_short_type_node) = tdecl;
13819 tdecl = add_builtin_type ("__bool int", bool_int_type_node);
13820 TYPE_NAME (bool_int_type_node) = tdecl;
13822 tdecl = add_builtin_type ("__pixel", pixel_type_node);
13823 TYPE_NAME (pixel_type_node) = tdecl;
13825 bool_V16QI_type_node = build_vector_type (bool_char_type_node, 16);
13826 bool_V8HI_type_node = build_vector_type (bool_short_type_node, 8);
13827 bool_V4SI_type_node = build_vector_type (bool_int_type_node, 4);
13828 bool_V2DI_type_node = build_vector_type (bool_long_type_node, 2);
13829 pixel_V8HI_type_node = build_vector_type (pixel_type_node, 8);
13831 tdecl = add_builtin_type ("__vector unsigned char", unsigned_V16QI_type_node);
13832 TYPE_NAME (unsigned_V16QI_type_node) = tdecl;
13834 tdecl = add_builtin_type ("__vector signed char", V16QI_type_node);
13835 TYPE_NAME (V16QI_type_node) = tdecl;
13837 tdecl = add_builtin_type ("__vector __bool char", bool_V16QI_type_node);
13838 TYPE_NAME ( bool_V16QI_type_node) = tdecl;
13840 tdecl = add_builtin_type ("__vector unsigned short", unsigned_V8HI_type_node);
13841 TYPE_NAME (unsigned_V8HI_type_node) = tdecl;
13843 tdecl = add_builtin_type ("__vector signed short", V8HI_type_node);
13844 TYPE_NAME (V8HI_type_node) = tdecl;
13846 tdecl = add_builtin_type ("__vector __bool short", bool_V8HI_type_node);
13847 TYPE_NAME (bool_V8HI_type_node) = tdecl;
13849 tdecl = add_builtin_type ("__vector unsigned int", unsigned_V4SI_type_node);
13850 TYPE_NAME (unsigned_V4SI_type_node) = tdecl;
13852 tdecl = add_builtin_type ("__vector signed int", V4SI_type_node);
13853 TYPE_NAME (V4SI_type_node) = tdecl;
13855 tdecl = add_builtin_type ("__vector __bool int", bool_V4SI_type_node);
13856 TYPE_NAME (bool_V4SI_type_node) = tdecl;
13858 tdecl = add_builtin_type ("__vector float", V4SF_type_node);
13859 TYPE_NAME (V4SF_type_node) = tdecl;
13861 tdecl = add_builtin_type ("__vector __pixel", pixel_V8HI_type_node);
13862 TYPE_NAME (pixel_V8HI_type_node) = tdecl;
13864 tdecl = add_builtin_type ("__vector double", V2DF_type_node);
13865 TYPE_NAME (V2DF_type_node) = tdecl;
13867 if (TARGET_POWERPC64)
13869 tdecl = add_builtin_type ("__vector long", V2DI_type_node);
13870 TYPE_NAME (V2DI_type_node) = tdecl;
13872 tdecl = add_builtin_type ("__vector unsigned long",
13873 unsigned_V2DI_type_node);
13874 TYPE_NAME (unsigned_V2DI_type_node) = tdecl;
13876 tdecl = add_builtin_type ("__vector __bool long", bool_V2DI_type_node);
13877 TYPE_NAME (bool_V2DI_type_node) = tdecl;
13879 else
13881 tdecl = add_builtin_type ("__vector long long", V2DI_type_node);
13882 TYPE_NAME (V2DI_type_node) = tdecl;
13884 tdecl = add_builtin_type ("__vector unsigned long long",
13885 unsigned_V2DI_type_node);
13886 TYPE_NAME (unsigned_V2DI_type_node) = tdecl;
13888 tdecl = add_builtin_type ("__vector __bool long long",
13889 bool_V2DI_type_node);
13890 TYPE_NAME (bool_V2DI_type_node) = tdecl;
13893 if (V1TI_type_node)
13895 tdecl = add_builtin_type ("__vector __int128", V1TI_type_node);
13896 TYPE_NAME (V1TI_type_node) = tdecl;
13898 tdecl = add_builtin_type ("__vector unsigned __int128",
13899 unsigned_V1TI_type_node);
13900 TYPE_NAME (unsigned_V1TI_type_node) = tdecl;
13903 /* Paired and SPE builtins are only available if you build a compiler with
13904 the appropriate options, so only create those builtins with the
13905 appropriate compiler option. Create Altivec and VSX builtins on machines
13906 with at least the general purpose extensions (970 and newer) to allow the
13907 use of the target attribute. */
13908 if (TARGET_PAIRED_FLOAT)
13909 paired_init_builtins ();
13910 if (TARGET_SPE)
13911 spe_init_builtins ();
13912 if (TARGET_EXTRA_BUILTINS)
13913 altivec_init_builtins ();
13914 if (TARGET_HTM)
13915 htm_init_builtins ();
13917 if (TARGET_EXTRA_BUILTINS || TARGET_SPE || TARGET_PAIRED_FLOAT)
13918 rs6000_common_init_builtins ();
13920 ftype = builtin_function_type (DFmode, DFmode, DFmode, VOIDmode,
13921 RS6000_BUILTIN_RECIP, "__builtin_recipdiv");
13922 def_builtin ("__builtin_recipdiv", ftype, RS6000_BUILTIN_RECIP);
13924 ftype = builtin_function_type (SFmode, SFmode, SFmode, VOIDmode,
13925 RS6000_BUILTIN_RECIPF, "__builtin_recipdivf");
13926 def_builtin ("__builtin_recipdivf", ftype, RS6000_BUILTIN_RECIPF);
13928 ftype = builtin_function_type (DFmode, DFmode, VOIDmode, VOIDmode,
13929 RS6000_BUILTIN_RSQRT, "__builtin_rsqrt");
13930 def_builtin ("__builtin_rsqrt", ftype, RS6000_BUILTIN_RSQRT);
13932 ftype = builtin_function_type (SFmode, SFmode, VOIDmode, VOIDmode,
13933 RS6000_BUILTIN_RSQRTF, "__builtin_rsqrtf");
13934 def_builtin ("__builtin_rsqrtf", ftype, RS6000_BUILTIN_RSQRTF);
13936 mode = (TARGET_64BIT) ? DImode : SImode;
13937 ftype = builtin_function_type (mode, mode, mode, VOIDmode,
13938 POWER7_BUILTIN_BPERMD, "__builtin_bpermd");
13939 def_builtin ("__builtin_bpermd", ftype, POWER7_BUILTIN_BPERMD);
13941 ftype = build_function_type_list (unsigned_intDI_type_node,
13942 NULL_TREE);
13943 def_builtin ("__builtin_ppc_get_timebase", ftype, RS6000_BUILTIN_GET_TB);
13945 if (TARGET_64BIT)
13946 ftype = build_function_type_list (unsigned_intDI_type_node,
13947 NULL_TREE);
13948 else
13949 ftype = build_function_type_list (unsigned_intSI_type_node,
13950 NULL_TREE);
13951 def_builtin ("__builtin_ppc_mftb", ftype, RS6000_BUILTIN_MFTB);
13953 ftype = build_function_type_list (double_type_node, NULL_TREE);
13954 def_builtin ("__builtin_mffs", ftype, RS6000_BUILTIN_MFFS);
13956 ftype = build_function_type_list (void_type_node,
13957 intSI_type_node, double_type_node,
13958 NULL_TREE);
13959 def_builtin ("__builtin_mtfsf", ftype, RS6000_BUILTIN_MTFSF);
13961 #if TARGET_XCOFF
13962 /* AIX libm provides clog as __clog. */
13963 if ((tdecl = builtin_decl_explicit (BUILT_IN_CLOG)) != NULL_TREE)
13964 set_user_assembler_name (tdecl, "__clog");
13965 #endif
13967 #ifdef SUBTARGET_INIT_BUILTINS
13968 SUBTARGET_INIT_BUILTINS;
13969 #endif
13972 /* Returns the rs6000 builtin decl for CODE. */
13974 static tree
13975 rs6000_builtin_decl (unsigned code, bool initialize_p ATTRIBUTE_UNUSED)
13977 HOST_WIDE_INT fnmask;
13979 if (code >= RS6000_BUILTIN_COUNT)
13980 return error_mark_node;
13982 fnmask = rs6000_builtin_info[code].mask;
13983 if ((fnmask & rs6000_builtin_mask) != fnmask)
13985 rs6000_invalid_builtin ((enum rs6000_builtins)code);
13986 return error_mark_node;
13989 return rs6000_builtin_decls[code];
13992 static void
13993 spe_init_builtins (void)
13995 tree puint_type_node = build_pointer_type (unsigned_type_node);
13996 tree pushort_type_node = build_pointer_type (short_unsigned_type_node);
13997 const struct builtin_description *d;
13998 size_t i;
14000 tree v2si_ftype_4_v2si
14001 = build_function_type_list (opaque_V2SI_type_node,
14002 opaque_V2SI_type_node,
14003 opaque_V2SI_type_node,
14004 opaque_V2SI_type_node,
14005 opaque_V2SI_type_node,
14006 NULL_TREE);
14008 tree v2sf_ftype_4_v2sf
14009 = build_function_type_list (opaque_V2SF_type_node,
14010 opaque_V2SF_type_node,
14011 opaque_V2SF_type_node,
14012 opaque_V2SF_type_node,
14013 opaque_V2SF_type_node,
14014 NULL_TREE);
14016 tree int_ftype_int_v2si_v2si
14017 = build_function_type_list (integer_type_node,
14018 integer_type_node,
14019 opaque_V2SI_type_node,
14020 opaque_V2SI_type_node,
14021 NULL_TREE);
14023 tree int_ftype_int_v2sf_v2sf
14024 = build_function_type_list (integer_type_node,
14025 integer_type_node,
14026 opaque_V2SF_type_node,
14027 opaque_V2SF_type_node,
14028 NULL_TREE);
14030 tree void_ftype_v2si_puint_int
14031 = build_function_type_list (void_type_node,
14032 opaque_V2SI_type_node,
14033 puint_type_node,
14034 integer_type_node,
14035 NULL_TREE);
14037 tree void_ftype_v2si_puint_char
14038 = build_function_type_list (void_type_node,
14039 opaque_V2SI_type_node,
14040 puint_type_node,
14041 char_type_node,
14042 NULL_TREE);
14044 tree void_ftype_v2si_pv2si_int
14045 = build_function_type_list (void_type_node,
14046 opaque_V2SI_type_node,
14047 opaque_p_V2SI_type_node,
14048 integer_type_node,
14049 NULL_TREE);
14051 tree void_ftype_v2si_pv2si_char
14052 = build_function_type_list (void_type_node,
14053 opaque_V2SI_type_node,
14054 opaque_p_V2SI_type_node,
14055 char_type_node,
14056 NULL_TREE);
14058 tree void_ftype_int
14059 = build_function_type_list (void_type_node, integer_type_node, NULL_TREE);
14061 tree int_ftype_void
14062 = build_function_type_list (integer_type_node, NULL_TREE);
14064 tree v2si_ftype_pv2si_int
14065 = build_function_type_list (opaque_V2SI_type_node,
14066 opaque_p_V2SI_type_node,
14067 integer_type_node,
14068 NULL_TREE);
14070 tree v2si_ftype_puint_int
14071 = build_function_type_list (opaque_V2SI_type_node,
14072 puint_type_node,
14073 integer_type_node,
14074 NULL_TREE);
14076 tree v2si_ftype_pushort_int
14077 = build_function_type_list (opaque_V2SI_type_node,
14078 pushort_type_node,
14079 integer_type_node,
14080 NULL_TREE);
14082 tree v2si_ftype_signed_char
14083 = build_function_type_list (opaque_V2SI_type_node,
14084 signed_char_type_node,
14085 NULL_TREE);
14087 add_builtin_type ("__ev64_opaque__", opaque_V2SI_type_node);
14089 /* Initialize irregular SPE builtins. */
14091 def_builtin ("__builtin_spe_mtspefscr", void_ftype_int, SPE_BUILTIN_MTSPEFSCR);
14092 def_builtin ("__builtin_spe_mfspefscr", int_ftype_void, SPE_BUILTIN_MFSPEFSCR);
14093 def_builtin ("__builtin_spe_evstddx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDDX);
14094 def_builtin ("__builtin_spe_evstdhx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDHX);
14095 def_builtin ("__builtin_spe_evstdwx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDWX);
14096 def_builtin ("__builtin_spe_evstwhex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHEX);
14097 def_builtin ("__builtin_spe_evstwhox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHOX);
14098 def_builtin ("__builtin_spe_evstwwex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWEX);
14099 def_builtin ("__builtin_spe_evstwwox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWOX);
14100 def_builtin ("__builtin_spe_evstdd", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDD);
14101 def_builtin ("__builtin_spe_evstdh", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDH);
14102 def_builtin ("__builtin_spe_evstdw", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDW);
14103 def_builtin ("__builtin_spe_evstwhe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHE);
14104 def_builtin ("__builtin_spe_evstwho", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHO);
14105 def_builtin ("__builtin_spe_evstwwe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWE);
14106 def_builtin ("__builtin_spe_evstwwo", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWO);
14107 def_builtin ("__builtin_spe_evsplatfi", v2si_ftype_signed_char, SPE_BUILTIN_EVSPLATFI);
14108 def_builtin ("__builtin_spe_evsplati", v2si_ftype_signed_char, SPE_BUILTIN_EVSPLATI);
14110 /* Loads. */
14111 def_builtin ("__builtin_spe_evlddx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDDX);
14112 def_builtin ("__builtin_spe_evldwx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDWX);
14113 def_builtin ("__builtin_spe_evldhx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDHX);
14114 def_builtin ("__builtin_spe_evlwhex", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHEX);
14115 def_builtin ("__builtin_spe_evlwhoux", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOUX);
14116 def_builtin ("__builtin_spe_evlwhosx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOSX);
14117 def_builtin ("__builtin_spe_evlwwsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLATX);
14118 def_builtin ("__builtin_spe_evlwhsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLATX);
14119 def_builtin ("__builtin_spe_evlhhesplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLATX);
14120 def_builtin ("__builtin_spe_evlhhousplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLATX);
14121 def_builtin ("__builtin_spe_evlhhossplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLATX);
14122 def_builtin ("__builtin_spe_evldd", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDD);
14123 def_builtin ("__builtin_spe_evldw", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDW);
14124 def_builtin ("__builtin_spe_evldh", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDH);
14125 def_builtin ("__builtin_spe_evlhhesplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLAT);
14126 def_builtin ("__builtin_spe_evlhhossplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLAT);
14127 def_builtin ("__builtin_spe_evlhhousplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLAT);
14128 def_builtin ("__builtin_spe_evlwhe", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHE);
14129 def_builtin ("__builtin_spe_evlwhos", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOS);
14130 def_builtin ("__builtin_spe_evlwhou", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOU);
14131 def_builtin ("__builtin_spe_evlwhsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLAT);
14132 def_builtin ("__builtin_spe_evlwwsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLAT);
14134 /* Predicates. */
14135 d = bdesc_spe_predicates;
14136 for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, d++)
14138 tree type;
14140 switch (insn_data[d->icode].operand[1].mode)
14142 case V2SImode:
14143 type = int_ftype_int_v2si_v2si;
14144 break;
14145 case V2SFmode:
14146 type = int_ftype_int_v2sf_v2sf;
14147 break;
14148 default:
14149 gcc_unreachable ();
14152 def_builtin (d->name, type, d->code);
14155 /* Evsel predicates. */
14156 d = bdesc_spe_evsel;
14157 for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, d++)
14159 tree type;
14161 switch (insn_data[d->icode].operand[1].mode)
14163 case V2SImode:
14164 type = v2si_ftype_4_v2si;
14165 break;
14166 case V2SFmode:
14167 type = v2sf_ftype_4_v2sf;
14168 break;
14169 default:
14170 gcc_unreachable ();
14173 def_builtin (d->name, type, d->code);
14177 static void
14178 paired_init_builtins (void)
14180 const struct builtin_description *d;
14181 size_t i;
14183 tree int_ftype_int_v2sf_v2sf
14184 = build_function_type_list (integer_type_node,
14185 integer_type_node,
14186 V2SF_type_node,
14187 V2SF_type_node,
14188 NULL_TREE);
14189 tree pcfloat_type_node =
14190 build_pointer_type (build_qualified_type
14191 (float_type_node, TYPE_QUAL_CONST));
14193 tree v2sf_ftype_long_pcfloat = build_function_type_list (V2SF_type_node,
14194 long_integer_type_node,
14195 pcfloat_type_node,
14196 NULL_TREE);
14197 tree void_ftype_v2sf_long_pcfloat =
14198 build_function_type_list (void_type_node,
14199 V2SF_type_node,
14200 long_integer_type_node,
14201 pcfloat_type_node,
14202 NULL_TREE);
14205 def_builtin ("__builtin_paired_lx", v2sf_ftype_long_pcfloat,
14206 PAIRED_BUILTIN_LX);
14209 def_builtin ("__builtin_paired_stx", void_ftype_v2sf_long_pcfloat,
14210 PAIRED_BUILTIN_STX);
14212 /* Predicates. */
14213 d = bdesc_paired_preds;
14214 for (i = 0; i < ARRAY_SIZE (bdesc_paired_preds); ++i, d++)
14216 tree type;
14218 if (TARGET_DEBUG_BUILTIN)
14219 fprintf (stderr, "paired pred #%d, insn = %s [%d], mode = %s\n",
14220 (int)i, get_insn_name (d->icode), (int)d->icode,
14221 GET_MODE_NAME (insn_data[d->icode].operand[1].mode));
14223 switch (insn_data[d->icode].operand[1].mode)
14225 case V2SFmode:
14226 type = int_ftype_int_v2sf_v2sf;
14227 break;
14228 default:
14229 gcc_unreachable ();
14232 def_builtin (d->name, type, d->code);
14236 static void
14237 altivec_init_builtins (void)
14239 const struct builtin_description *d;
14240 size_t i;
14241 tree ftype;
14242 tree decl;
14244 tree pvoid_type_node = build_pointer_type (void_type_node);
14246 tree pcvoid_type_node
14247 = build_pointer_type (build_qualified_type (void_type_node,
14248 TYPE_QUAL_CONST));
14250 tree int_ftype_opaque
14251 = build_function_type_list (integer_type_node,
14252 opaque_V4SI_type_node, NULL_TREE);
14253 tree opaque_ftype_opaque
14254 = build_function_type_list (integer_type_node, NULL_TREE);
14255 tree opaque_ftype_opaque_int
14256 = build_function_type_list (opaque_V4SI_type_node,
14257 opaque_V4SI_type_node, integer_type_node, NULL_TREE);
14258 tree opaque_ftype_opaque_opaque_int
14259 = build_function_type_list (opaque_V4SI_type_node,
14260 opaque_V4SI_type_node, opaque_V4SI_type_node,
14261 integer_type_node, NULL_TREE);
14262 tree int_ftype_int_opaque_opaque
14263 = build_function_type_list (integer_type_node,
14264 integer_type_node, opaque_V4SI_type_node,
14265 opaque_V4SI_type_node, NULL_TREE);
14266 tree int_ftype_int_v4si_v4si
14267 = build_function_type_list (integer_type_node,
14268 integer_type_node, V4SI_type_node,
14269 V4SI_type_node, NULL_TREE);
14270 tree int_ftype_int_v2di_v2di
14271 = build_function_type_list (integer_type_node,
14272 integer_type_node, V2DI_type_node,
14273 V2DI_type_node, NULL_TREE);
14274 tree void_ftype_v4si
14275 = build_function_type_list (void_type_node, V4SI_type_node, NULL_TREE);
14276 tree v8hi_ftype_void
14277 = build_function_type_list (V8HI_type_node, NULL_TREE);
14278 tree void_ftype_void
14279 = build_function_type_list (void_type_node, NULL_TREE);
14280 tree void_ftype_int
14281 = build_function_type_list (void_type_node, integer_type_node, NULL_TREE);
14283 tree opaque_ftype_long_pcvoid
14284 = build_function_type_list (opaque_V4SI_type_node,
14285 long_integer_type_node, pcvoid_type_node,
14286 NULL_TREE);
14287 tree v16qi_ftype_long_pcvoid
14288 = build_function_type_list (V16QI_type_node,
14289 long_integer_type_node, pcvoid_type_node,
14290 NULL_TREE);
14291 tree v8hi_ftype_long_pcvoid
14292 = build_function_type_list (V8HI_type_node,
14293 long_integer_type_node, pcvoid_type_node,
14294 NULL_TREE);
14295 tree v4si_ftype_long_pcvoid
14296 = build_function_type_list (V4SI_type_node,
14297 long_integer_type_node, pcvoid_type_node,
14298 NULL_TREE);
14299 tree v4sf_ftype_long_pcvoid
14300 = build_function_type_list (V4SF_type_node,
14301 long_integer_type_node, pcvoid_type_node,
14302 NULL_TREE);
14303 tree v2df_ftype_long_pcvoid
14304 = build_function_type_list (V2DF_type_node,
14305 long_integer_type_node, pcvoid_type_node,
14306 NULL_TREE);
14307 tree v2di_ftype_long_pcvoid
14308 = build_function_type_list (V2DI_type_node,
14309 long_integer_type_node, pcvoid_type_node,
14310 NULL_TREE);
14312 tree void_ftype_opaque_long_pvoid
14313 = build_function_type_list (void_type_node,
14314 opaque_V4SI_type_node, long_integer_type_node,
14315 pvoid_type_node, NULL_TREE);
14316 tree void_ftype_v4si_long_pvoid
14317 = build_function_type_list (void_type_node,
14318 V4SI_type_node, long_integer_type_node,
14319 pvoid_type_node, NULL_TREE);
14320 tree void_ftype_v16qi_long_pvoid
14321 = build_function_type_list (void_type_node,
14322 V16QI_type_node, long_integer_type_node,
14323 pvoid_type_node, NULL_TREE);
14324 tree void_ftype_v8hi_long_pvoid
14325 = build_function_type_list (void_type_node,
14326 V8HI_type_node, long_integer_type_node,
14327 pvoid_type_node, NULL_TREE);
14328 tree void_ftype_v4sf_long_pvoid
14329 = build_function_type_list (void_type_node,
14330 V4SF_type_node, long_integer_type_node,
14331 pvoid_type_node, NULL_TREE);
14332 tree void_ftype_v2df_long_pvoid
14333 = build_function_type_list (void_type_node,
14334 V2DF_type_node, long_integer_type_node,
14335 pvoid_type_node, NULL_TREE);
14336 tree void_ftype_v2di_long_pvoid
14337 = build_function_type_list (void_type_node,
14338 V2DI_type_node, long_integer_type_node,
14339 pvoid_type_node, NULL_TREE);
14340 tree int_ftype_int_v8hi_v8hi
14341 = build_function_type_list (integer_type_node,
14342 integer_type_node, V8HI_type_node,
14343 V8HI_type_node, NULL_TREE);
14344 tree int_ftype_int_v16qi_v16qi
14345 = build_function_type_list (integer_type_node,
14346 integer_type_node, V16QI_type_node,
14347 V16QI_type_node, NULL_TREE);
14348 tree int_ftype_int_v4sf_v4sf
14349 = build_function_type_list (integer_type_node,
14350 integer_type_node, V4SF_type_node,
14351 V4SF_type_node, NULL_TREE);
14352 tree int_ftype_int_v2df_v2df
14353 = build_function_type_list (integer_type_node,
14354 integer_type_node, V2DF_type_node,
14355 V2DF_type_node, NULL_TREE);
14356 tree v2di_ftype_v2di
14357 = build_function_type_list (V2DI_type_node, V2DI_type_node, NULL_TREE);
14358 tree v4si_ftype_v4si
14359 = build_function_type_list (V4SI_type_node, V4SI_type_node, NULL_TREE);
14360 tree v8hi_ftype_v8hi
14361 = build_function_type_list (V8HI_type_node, V8HI_type_node, NULL_TREE);
14362 tree v16qi_ftype_v16qi
14363 = build_function_type_list (V16QI_type_node, V16QI_type_node, NULL_TREE);
14364 tree v4sf_ftype_v4sf
14365 = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
14366 tree v2df_ftype_v2df
14367 = build_function_type_list (V2DF_type_node, V2DF_type_node, NULL_TREE);
14368 tree void_ftype_pcvoid_int_int
14369 = build_function_type_list (void_type_node,
14370 pcvoid_type_node, integer_type_node,
14371 integer_type_node, NULL_TREE);
14373 def_builtin ("__builtin_altivec_mtvscr", void_ftype_v4si, ALTIVEC_BUILTIN_MTVSCR);
14374 def_builtin ("__builtin_altivec_mfvscr", v8hi_ftype_void, ALTIVEC_BUILTIN_MFVSCR);
14375 def_builtin ("__builtin_altivec_dssall", void_ftype_void, ALTIVEC_BUILTIN_DSSALL);
14376 def_builtin ("__builtin_altivec_dss", void_ftype_int, ALTIVEC_BUILTIN_DSS);
14377 def_builtin ("__builtin_altivec_lvsl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSL);
14378 def_builtin ("__builtin_altivec_lvsr", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSR);
14379 def_builtin ("__builtin_altivec_lvebx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEBX);
14380 def_builtin ("__builtin_altivec_lvehx", v8hi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEHX);
14381 def_builtin ("__builtin_altivec_lvewx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEWX);
14382 def_builtin ("__builtin_altivec_lvxl", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVXL);
14383 def_builtin ("__builtin_altivec_lvxl_v2df", v2df_ftype_long_pcvoid,
14384 ALTIVEC_BUILTIN_LVXL_V2DF);
14385 def_builtin ("__builtin_altivec_lvxl_v2di", v2di_ftype_long_pcvoid,
14386 ALTIVEC_BUILTIN_LVXL_V2DI);
14387 def_builtin ("__builtin_altivec_lvxl_v4sf", v4sf_ftype_long_pcvoid,
14388 ALTIVEC_BUILTIN_LVXL_V4SF);
14389 def_builtin ("__builtin_altivec_lvxl_v4si", v4si_ftype_long_pcvoid,
14390 ALTIVEC_BUILTIN_LVXL_V4SI);
14391 def_builtin ("__builtin_altivec_lvxl_v8hi", v8hi_ftype_long_pcvoid,
14392 ALTIVEC_BUILTIN_LVXL_V8HI);
14393 def_builtin ("__builtin_altivec_lvxl_v16qi", v16qi_ftype_long_pcvoid,
14394 ALTIVEC_BUILTIN_LVXL_V16QI);
14395 def_builtin ("__builtin_altivec_lvx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVX);
14396 def_builtin ("__builtin_altivec_lvx_v2df", v2df_ftype_long_pcvoid,
14397 ALTIVEC_BUILTIN_LVX_V2DF);
14398 def_builtin ("__builtin_altivec_lvx_v2di", v2di_ftype_long_pcvoid,
14399 ALTIVEC_BUILTIN_LVX_V2DI);
14400 def_builtin ("__builtin_altivec_lvx_v4sf", v4sf_ftype_long_pcvoid,
14401 ALTIVEC_BUILTIN_LVX_V4SF);
14402 def_builtin ("__builtin_altivec_lvx_v4si", v4si_ftype_long_pcvoid,
14403 ALTIVEC_BUILTIN_LVX_V4SI);
14404 def_builtin ("__builtin_altivec_lvx_v8hi", v8hi_ftype_long_pcvoid,
14405 ALTIVEC_BUILTIN_LVX_V8HI);
14406 def_builtin ("__builtin_altivec_lvx_v16qi", v16qi_ftype_long_pcvoid,
14407 ALTIVEC_BUILTIN_LVX_V16QI);
14408 def_builtin ("__builtin_altivec_stvx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVX);
14409 def_builtin ("__builtin_altivec_stvx_v2df", void_ftype_v2df_long_pvoid,
14410 ALTIVEC_BUILTIN_STVX_V2DF);
14411 def_builtin ("__builtin_altivec_stvx_v2di", void_ftype_v2di_long_pvoid,
14412 ALTIVEC_BUILTIN_STVX_V2DI);
14413 def_builtin ("__builtin_altivec_stvx_v4sf", void_ftype_v4sf_long_pvoid,
14414 ALTIVEC_BUILTIN_STVX_V4SF);
14415 def_builtin ("__builtin_altivec_stvx_v4si", void_ftype_v4si_long_pvoid,
14416 ALTIVEC_BUILTIN_STVX_V4SI);
14417 def_builtin ("__builtin_altivec_stvx_v8hi", void_ftype_v8hi_long_pvoid,
14418 ALTIVEC_BUILTIN_STVX_V8HI);
14419 def_builtin ("__builtin_altivec_stvx_v16qi", void_ftype_v16qi_long_pvoid,
14420 ALTIVEC_BUILTIN_STVX_V16QI);
14421 def_builtin ("__builtin_altivec_stvewx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVEWX);
14422 def_builtin ("__builtin_altivec_stvxl", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVXL);
14423 def_builtin ("__builtin_altivec_stvxl_v2df", void_ftype_v2df_long_pvoid,
14424 ALTIVEC_BUILTIN_STVXL_V2DF);
14425 def_builtin ("__builtin_altivec_stvxl_v2di", void_ftype_v2di_long_pvoid,
14426 ALTIVEC_BUILTIN_STVXL_V2DI);
14427 def_builtin ("__builtin_altivec_stvxl_v4sf", void_ftype_v4sf_long_pvoid,
14428 ALTIVEC_BUILTIN_STVXL_V4SF);
14429 def_builtin ("__builtin_altivec_stvxl_v4si", void_ftype_v4si_long_pvoid,
14430 ALTIVEC_BUILTIN_STVXL_V4SI);
14431 def_builtin ("__builtin_altivec_stvxl_v8hi", void_ftype_v8hi_long_pvoid,
14432 ALTIVEC_BUILTIN_STVXL_V8HI);
14433 def_builtin ("__builtin_altivec_stvxl_v16qi", void_ftype_v16qi_long_pvoid,
14434 ALTIVEC_BUILTIN_STVXL_V16QI);
14435 def_builtin ("__builtin_altivec_stvebx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVEBX);
14436 def_builtin ("__builtin_altivec_stvehx", void_ftype_v8hi_long_pvoid, ALTIVEC_BUILTIN_STVEHX);
14437 def_builtin ("__builtin_vec_ld", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LD);
14438 def_builtin ("__builtin_vec_lde", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LDE);
14439 def_builtin ("__builtin_vec_ldl", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LDL);
14440 def_builtin ("__builtin_vec_lvsl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVSL);
14441 def_builtin ("__builtin_vec_lvsr", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVSR);
14442 def_builtin ("__builtin_vec_lvebx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEBX);
14443 def_builtin ("__builtin_vec_lvehx", v8hi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEHX);
14444 def_builtin ("__builtin_vec_lvewx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEWX);
14445 def_builtin ("__builtin_vec_st", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_ST);
14446 def_builtin ("__builtin_vec_ste", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STE);
14447 def_builtin ("__builtin_vec_stl", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STL);
14448 def_builtin ("__builtin_vec_stvewx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEWX);
14449 def_builtin ("__builtin_vec_stvebx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEBX);
14450 def_builtin ("__builtin_vec_stvehx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEHX);
14452 def_builtin ("__builtin_vsx_lxvd2x_v2df", v2df_ftype_long_pcvoid,
14453 VSX_BUILTIN_LXVD2X_V2DF);
14454 def_builtin ("__builtin_vsx_lxvd2x_v2di", v2di_ftype_long_pcvoid,
14455 VSX_BUILTIN_LXVD2X_V2DI);
14456 def_builtin ("__builtin_vsx_lxvw4x_v4sf", v4sf_ftype_long_pcvoid,
14457 VSX_BUILTIN_LXVW4X_V4SF);
14458 def_builtin ("__builtin_vsx_lxvw4x_v4si", v4si_ftype_long_pcvoid,
14459 VSX_BUILTIN_LXVW4X_V4SI);
14460 def_builtin ("__builtin_vsx_lxvw4x_v8hi", v8hi_ftype_long_pcvoid,
14461 VSX_BUILTIN_LXVW4X_V8HI);
14462 def_builtin ("__builtin_vsx_lxvw4x_v16qi", v16qi_ftype_long_pcvoid,
14463 VSX_BUILTIN_LXVW4X_V16QI);
14464 def_builtin ("__builtin_vsx_stxvd2x_v2df", void_ftype_v2df_long_pvoid,
14465 VSX_BUILTIN_STXVD2X_V2DF);
14466 def_builtin ("__builtin_vsx_stxvd2x_v2di", void_ftype_v2di_long_pvoid,
14467 VSX_BUILTIN_STXVD2X_V2DI);
14468 def_builtin ("__builtin_vsx_stxvw4x_v4sf", void_ftype_v4sf_long_pvoid,
14469 VSX_BUILTIN_STXVW4X_V4SF);
14470 def_builtin ("__builtin_vsx_stxvw4x_v4si", void_ftype_v4si_long_pvoid,
14471 VSX_BUILTIN_STXVW4X_V4SI);
14472 def_builtin ("__builtin_vsx_stxvw4x_v8hi", void_ftype_v8hi_long_pvoid,
14473 VSX_BUILTIN_STXVW4X_V8HI);
14474 def_builtin ("__builtin_vsx_stxvw4x_v16qi", void_ftype_v16qi_long_pvoid,
14475 VSX_BUILTIN_STXVW4X_V16QI);
14476 def_builtin ("__builtin_vec_vsx_ld", opaque_ftype_long_pcvoid,
14477 VSX_BUILTIN_VEC_LD);
14478 def_builtin ("__builtin_vec_vsx_st", void_ftype_opaque_long_pvoid,
14479 VSX_BUILTIN_VEC_ST);
14481 def_builtin ("__builtin_vec_step", int_ftype_opaque, ALTIVEC_BUILTIN_VEC_STEP);
14482 def_builtin ("__builtin_vec_splats", opaque_ftype_opaque, ALTIVEC_BUILTIN_VEC_SPLATS);
14483 def_builtin ("__builtin_vec_promote", opaque_ftype_opaque, ALTIVEC_BUILTIN_VEC_PROMOTE);
14485 def_builtin ("__builtin_vec_sld", opaque_ftype_opaque_opaque_int, ALTIVEC_BUILTIN_VEC_SLD);
14486 def_builtin ("__builtin_vec_splat", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_SPLAT);
14487 def_builtin ("__builtin_vec_extract", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_EXTRACT);
14488 def_builtin ("__builtin_vec_insert", opaque_ftype_opaque_opaque_int, ALTIVEC_BUILTIN_VEC_INSERT);
14489 def_builtin ("__builtin_vec_vspltw", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTW);
14490 def_builtin ("__builtin_vec_vsplth", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTH);
14491 def_builtin ("__builtin_vec_vspltb", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTB);
14492 def_builtin ("__builtin_vec_ctf", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTF);
14493 def_builtin ("__builtin_vec_vcfsx", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VCFSX);
14494 def_builtin ("__builtin_vec_vcfux", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VCFUX);
14495 def_builtin ("__builtin_vec_cts", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTS);
14496 def_builtin ("__builtin_vec_ctu", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTU);
14498 /* Cell builtins. */
14499 def_builtin ("__builtin_altivec_lvlx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVLX);
14500 def_builtin ("__builtin_altivec_lvlxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVLXL);
14501 def_builtin ("__builtin_altivec_lvrx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVRX);
14502 def_builtin ("__builtin_altivec_lvrxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVRXL);
14504 def_builtin ("__builtin_vec_lvlx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVLX);
14505 def_builtin ("__builtin_vec_lvlxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVLXL);
14506 def_builtin ("__builtin_vec_lvrx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVRX);
14507 def_builtin ("__builtin_vec_lvrxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVRXL);
14509 def_builtin ("__builtin_altivec_stvlx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVLX);
14510 def_builtin ("__builtin_altivec_stvlxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVLXL);
14511 def_builtin ("__builtin_altivec_stvrx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVRX);
14512 def_builtin ("__builtin_altivec_stvrxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVRXL);
14514 def_builtin ("__builtin_vec_stvlx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVLX);
14515 def_builtin ("__builtin_vec_stvlxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVLXL);
14516 def_builtin ("__builtin_vec_stvrx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVRX);
14517 def_builtin ("__builtin_vec_stvrxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVRXL);
14519 /* Add the DST variants. */
14520 d = bdesc_dst;
14521 for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
14522 def_builtin (d->name, void_ftype_pcvoid_int_int, d->code);
14524 /* Initialize the predicates. */
14525 d = bdesc_altivec_preds;
14526 for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, d++)
14528 enum machine_mode mode1;
14529 tree type;
14531 if (rs6000_overloaded_builtin_p (d->code))
14532 mode1 = VOIDmode;
14533 else
14534 mode1 = insn_data[d->icode].operand[1].mode;
14536 switch (mode1)
14538 case VOIDmode:
14539 type = int_ftype_int_opaque_opaque;
14540 break;
14541 case V2DImode:
14542 type = int_ftype_int_v2di_v2di;
14543 break;
14544 case V4SImode:
14545 type = int_ftype_int_v4si_v4si;
14546 break;
14547 case V8HImode:
14548 type = int_ftype_int_v8hi_v8hi;
14549 break;
14550 case V16QImode:
14551 type = int_ftype_int_v16qi_v16qi;
14552 break;
14553 case V4SFmode:
14554 type = int_ftype_int_v4sf_v4sf;
14555 break;
14556 case V2DFmode:
14557 type = int_ftype_int_v2df_v2df;
14558 break;
14559 default:
14560 gcc_unreachable ();
14563 def_builtin (d->name, type, d->code);
14566 /* Initialize the abs* operators. */
14567 d = bdesc_abs;
14568 for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
14570 enum machine_mode mode0;
14571 tree type;
14573 mode0 = insn_data[d->icode].operand[0].mode;
14575 switch (mode0)
14577 case V2DImode:
14578 type = v2di_ftype_v2di;
14579 break;
14580 case V4SImode:
14581 type = v4si_ftype_v4si;
14582 break;
14583 case V8HImode:
14584 type = v8hi_ftype_v8hi;
14585 break;
14586 case V16QImode:
14587 type = v16qi_ftype_v16qi;
14588 break;
14589 case V4SFmode:
14590 type = v4sf_ftype_v4sf;
14591 break;
14592 case V2DFmode:
14593 type = v2df_ftype_v2df;
14594 break;
14595 default:
14596 gcc_unreachable ();
14599 def_builtin (d->name, type, d->code);
14602 /* Initialize target builtin that implements
14603 targetm.vectorize.builtin_mask_for_load. */
14605 decl = add_builtin_function ("__builtin_altivec_mask_for_load",
14606 v16qi_ftype_long_pcvoid,
14607 ALTIVEC_BUILTIN_MASK_FOR_LOAD,
14608 BUILT_IN_MD, NULL, NULL_TREE);
14609 TREE_READONLY (decl) = 1;
14610 /* Record the decl. Will be used by rs6000_builtin_mask_for_load. */
14611 altivec_builtin_mask_for_load = decl;
14613 /* Access to the vec_init patterns. */
14614 ftype = build_function_type_list (V4SI_type_node, integer_type_node,
14615 integer_type_node, integer_type_node,
14616 integer_type_node, NULL_TREE);
14617 def_builtin ("__builtin_vec_init_v4si", ftype, ALTIVEC_BUILTIN_VEC_INIT_V4SI);
14619 ftype = build_function_type_list (V8HI_type_node, short_integer_type_node,
14620 short_integer_type_node,
14621 short_integer_type_node,
14622 short_integer_type_node,
14623 short_integer_type_node,
14624 short_integer_type_node,
14625 short_integer_type_node,
14626 short_integer_type_node, NULL_TREE);
14627 def_builtin ("__builtin_vec_init_v8hi", ftype, ALTIVEC_BUILTIN_VEC_INIT_V8HI);
14629 ftype = build_function_type_list (V16QI_type_node, char_type_node,
14630 char_type_node, char_type_node,
14631 char_type_node, char_type_node,
14632 char_type_node, char_type_node,
14633 char_type_node, char_type_node,
14634 char_type_node, char_type_node,
14635 char_type_node, char_type_node,
14636 char_type_node, char_type_node,
14637 char_type_node, NULL_TREE);
14638 def_builtin ("__builtin_vec_init_v16qi", ftype,
14639 ALTIVEC_BUILTIN_VEC_INIT_V16QI);
14641 ftype = build_function_type_list (V4SF_type_node, float_type_node,
14642 float_type_node, float_type_node,
14643 float_type_node, NULL_TREE);
14644 def_builtin ("__builtin_vec_init_v4sf", ftype, ALTIVEC_BUILTIN_VEC_INIT_V4SF);
14646 /* VSX builtins. */
14647 ftype = build_function_type_list (V2DF_type_node, double_type_node,
14648 double_type_node, NULL_TREE);
14649 def_builtin ("__builtin_vec_init_v2df", ftype, VSX_BUILTIN_VEC_INIT_V2DF);
14651 ftype = build_function_type_list (V2DI_type_node, intDI_type_node,
14652 intDI_type_node, NULL_TREE);
14653 def_builtin ("__builtin_vec_init_v2di", ftype, VSX_BUILTIN_VEC_INIT_V2DI);
14655 /* Access to the vec_set patterns. */
14656 ftype = build_function_type_list (V4SI_type_node, V4SI_type_node,
14657 intSI_type_node,
14658 integer_type_node, NULL_TREE);
14659 def_builtin ("__builtin_vec_set_v4si", ftype, ALTIVEC_BUILTIN_VEC_SET_V4SI);
14661 ftype = build_function_type_list (V8HI_type_node, V8HI_type_node,
14662 intHI_type_node,
14663 integer_type_node, NULL_TREE);
14664 def_builtin ("__builtin_vec_set_v8hi", ftype, ALTIVEC_BUILTIN_VEC_SET_V8HI);
14666 ftype = build_function_type_list (V16QI_type_node, V16QI_type_node,
14667 intQI_type_node,
14668 integer_type_node, NULL_TREE);
14669 def_builtin ("__builtin_vec_set_v16qi", ftype, ALTIVEC_BUILTIN_VEC_SET_V16QI);
14671 ftype = build_function_type_list (V4SF_type_node, V4SF_type_node,
14672 float_type_node,
14673 integer_type_node, NULL_TREE);
14674 def_builtin ("__builtin_vec_set_v4sf", ftype, ALTIVEC_BUILTIN_VEC_SET_V4SF);
14676 ftype = build_function_type_list (V2DF_type_node, V2DF_type_node,
14677 double_type_node,
14678 integer_type_node, NULL_TREE);
14679 def_builtin ("__builtin_vec_set_v2df", ftype, VSX_BUILTIN_VEC_SET_V2DF);
14681 ftype = build_function_type_list (V2DI_type_node, V2DI_type_node,
14682 intDI_type_node,
14683 integer_type_node, NULL_TREE);
14684 def_builtin ("__builtin_vec_set_v2di", ftype, VSX_BUILTIN_VEC_SET_V2DI);
14686 /* Access to the vec_extract patterns. */
14687 ftype = build_function_type_list (intSI_type_node, V4SI_type_node,
14688 integer_type_node, NULL_TREE);
14689 def_builtin ("__builtin_vec_ext_v4si", ftype, ALTIVEC_BUILTIN_VEC_EXT_V4SI);
14691 ftype = build_function_type_list (intHI_type_node, V8HI_type_node,
14692 integer_type_node, NULL_TREE);
14693 def_builtin ("__builtin_vec_ext_v8hi", ftype, ALTIVEC_BUILTIN_VEC_EXT_V8HI);
14695 ftype = build_function_type_list (intQI_type_node, V16QI_type_node,
14696 integer_type_node, NULL_TREE);
14697 def_builtin ("__builtin_vec_ext_v16qi", ftype, ALTIVEC_BUILTIN_VEC_EXT_V16QI);
14699 ftype = build_function_type_list (float_type_node, V4SF_type_node,
14700 integer_type_node, NULL_TREE);
14701 def_builtin ("__builtin_vec_ext_v4sf", ftype, ALTIVEC_BUILTIN_VEC_EXT_V4SF);
14703 ftype = build_function_type_list (double_type_node, V2DF_type_node,
14704 integer_type_node, NULL_TREE);
14705 def_builtin ("__builtin_vec_ext_v2df", ftype, VSX_BUILTIN_VEC_EXT_V2DF);
14707 ftype = build_function_type_list (intDI_type_node, V2DI_type_node,
14708 integer_type_node, NULL_TREE);
14709 def_builtin ("__builtin_vec_ext_v2di", ftype, VSX_BUILTIN_VEC_EXT_V2DI);
14712 if (V1TI_type_node)
14714 tree v1ti_ftype_long_pcvoid
14715 = build_function_type_list (V1TI_type_node,
14716 long_integer_type_node, pcvoid_type_node,
14717 NULL_TREE);
14718 tree void_ftype_v1ti_long_pvoid
14719 = build_function_type_list (void_type_node,
14720 V1TI_type_node, long_integer_type_node,
14721 pvoid_type_node, NULL_TREE);
14722 def_builtin ("__builtin_vsx_lxvd2x_v1ti", v1ti_ftype_long_pcvoid,
14723 VSX_BUILTIN_LXVD2X_V1TI);
14724 def_builtin ("__builtin_vsx_stxvd2x_v1ti", void_ftype_v1ti_long_pvoid,
14725 VSX_BUILTIN_STXVD2X_V1TI);
14726 ftype = build_function_type_list (V1TI_type_node, intTI_type_node,
14727 NULL_TREE, NULL_TREE);
14728 def_builtin ("__builtin_vec_init_v1ti", ftype, VSX_BUILTIN_VEC_INIT_V1TI);
14729 ftype = build_function_type_list (V1TI_type_node, V1TI_type_node,
14730 intTI_type_node,
14731 integer_type_node, NULL_TREE);
14732 def_builtin ("__builtin_vec_set_v1ti", ftype, VSX_BUILTIN_VEC_SET_V1TI);
14733 ftype = build_function_type_list (intTI_type_node, V1TI_type_node,
14734 integer_type_node, NULL_TREE);
14735 def_builtin ("__builtin_vec_ext_v1ti", ftype, VSX_BUILTIN_VEC_EXT_V1TI);
14740 static void
14741 htm_init_builtins (void)
14743 HOST_WIDE_INT builtin_mask = rs6000_builtin_mask;
14744 const struct builtin_description *d;
14745 size_t i;
14747 d = bdesc_htm;
14748 for (i = 0; i < ARRAY_SIZE (bdesc_htm); i++, d++)
14750 tree op[MAX_HTM_OPERANDS], type;
14751 HOST_WIDE_INT mask = d->mask;
14752 unsigned attr = rs6000_builtin_info[d->code].attr;
14753 bool void_func = (attr & RS6000_BTC_VOID);
14754 int attr_args = (attr & RS6000_BTC_TYPE_MASK);
14755 int nopnds = 0;
14756 tree argtype = (attr & RS6000_BTC_SPR) ? long_unsigned_type_node
14757 : unsigned_type_node;
14759 if ((mask & builtin_mask) != mask)
14761 if (TARGET_DEBUG_BUILTIN)
14762 fprintf (stderr, "htm_builtin, skip binary %s\n", d->name);
14763 continue;
14766 if (d->name == 0)
14768 if (TARGET_DEBUG_BUILTIN)
14769 fprintf (stderr, "htm_builtin, bdesc_htm[%ld] no name\n",
14770 (long unsigned) i);
14771 continue;
14774 op[nopnds++] = (void_func) ? void_type_node : argtype;
14776 if (attr_args == RS6000_BTC_UNARY)
14777 op[nopnds++] = argtype;
14778 else if (attr_args == RS6000_BTC_BINARY)
14780 op[nopnds++] = argtype;
14781 op[nopnds++] = argtype;
14783 else if (attr_args == RS6000_BTC_TERNARY)
14785 op[nopnds++] = argtype;
14786 op[nopnds++] = argtype;
14787 op[nopnds++] = argtype;
14790 switch (nopnds)
14792 case 1:
14793 type = build_function_type_list (op[0], NULL_TREE);
14794 break;
14795 case 2:
14796 type = build_function_type_list (op[0], op[1], NULL_TREE);
14797 break;
14798 case 3:
14799 type = build_function_type_list (op[0], op[1], op[2], NULL_TREE);
14800 break;
14801 case 4:
14802 type = build_function_type_list (op[0], op[1], op[2], op[3],
14803 NULL_TREE);
14804 break;
14805 default:
14806 gcc_unreachable ();
14809 def_builtin (d->name, type, d->code);
14813 /* Hash function for builtin functions with up to 3 arguments and a return
14814 type. */
14815 static unsigned
14816 builtin_hash_function (const void *hash_entry)
14818 unsigned ret = 0;
14819 int i;
14820 const struct builtin_hash_struct *bh =
14821 (const struct builtin_hash_struct *) hash_entry;
14823 for (i = 0; i < 4; i++)
14825 ret = (ret * (unsigned)MAX_MACHINE_MODE) + ((unsigned)bh->mode[i]);
14826 ret = (ret * 2) + bh->uns_p[i];
14829 return ret;
14832 /* Compare builtin hash entries H1 and H2 for equivalence. */
14833 static int
14834 builtin_hash_eq (const void *h1, const void *h2)
14836 const struct builtin_hash_struct *p1 = (const struct builtin_hash_struct *) h1;
14837 const struct builtin_hash_struct *p2 = (const struct builtin_hash_struct *) h2;
14839 return ((p1->mode[0] == p2->mode[0])
14840 && (p1->mode[1] == p2->mode[1])
14841 && (p1->mode[2] == p2->mode[2])
14842 && (p1->mode[3] == p2->mode[3])
14843 && (p1->uns_p[0] == p2->uns_p[0])
14844 && (p1->uns_p[1] == p2->uns_p[1])
14845 && (p1->uns_p[2] == p2->uns_p[2])
14846 && (p1->uns_p[3] == p2->uns_p[3]));
14849 /* Map types for builtin functions with an explicit return type and up to 3
14850 arguments. Functions with fewer than 3 arguments use VOIDmode as the type
14851 of the argument. */
14852 static tree
14853 builtin_function_type (enum machine_mode mode_ret, enum machine_mode mode_arg0,
14854 enum machine_mode mode_arg1, enum machine_mode mode_arg2,
14855 enum rs6000_builtins builtin, const char *name)
14857 struct builtin_hash_struct h;
14858 struct builtin_hash_struct *h2;
14859 void **found;
14860 int num_args = 3;
14861 int i;
14862 tree ret_type = NULL_TREE;
14863 tree arg_type[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
14865 /* Create builtin_hash_table. */
14866 if (builtin_hash_table == NULL)
14867 builtin_hash_table = htab_create_ggc (1500, builtin_hash_function,
14868 builtin_hash_eq, NULL);
14870 h.type = NULL_TREE;
14871 h.mode[0] = mode_ret;
14872 h.mode[1] = mode_arg0;
14873 h.mode[2] = mode_arg1;
14874 h.mode[3] = mode_arg2;
14875 h.uns_p[0] = 0;
14876 h.uns_p[1] = 0;
14877 h.uns_p[2] = 0;
14878 h.uns_p[3] = 0;
14880 /* If the builtin is a type that produces unsigned results or takes unsigned
14881 arguments, and it is returned as a decl for the vectorizer (such as
14882 widening multiplies, permute), make sure the arguments and return value
14883 are type correct. */
14884 switch (builtin)
14886 /* unsigned 1 argument functions. */
14887 case CRYPTO_BUILTIN_VSBOX:
14888 case P8V_BUILTIN_VGBBD:
14889 h.uns_p[0] = 1;
14890 h.uns_p[1] = 1;
14891 break;
14893 /* unsigned 2 argument functions. */
14894 case ALTIVEC_BUILTIN_VMULEUB_UNS:
14895 case ALTIVEC_BUILTIN_VMULEUH_UNS:
14896 case ALTIVEC_BUILTIN_VMULOUB_UNS:
14897 case ALTIVEC_BUILTIN_VMULOUH_UNS:
14898 case CRYPTO_BUILTIN_VCIPHER:
14899 case CRYPTO_BUILTIN_VCIPHERLAST:
14900 case CRYPTO_BUILTIN_VNCIPHER:
14901 case CRYPTO_BUILTIN_VNCIPHERLAST:
14902 case CRYPTO_BUILTIN_VPMSUMB:
14903 case CRYPTO_BUILTIN_VPMSUMH:
14904 case CRYPTO_BUILTIN_VPMSUMW:
14905 case CRYPTO_BUILTIN_VPMSUMD:
14906 case CRYPTO_BUILTIN_VPMSUM:
14907 h.uns_p[0] = 1;
14908 h.uns_p[1] = 1;
14909 h.uns_p[2] = 1;
14910 break;
14912 /* unsigned 3 argument functions. */
14913 case ALTIVEC_BUILTIN_VPERM_16QI_UNS:
14914 case ALTIVEC_BUILTIN_VPERM_8HI_UNS:
14915 case ALTIVEC_BUILTIN_VPERM_4SI_UNS:
14916 case ALTIVEC_BUILTIN_VPERM_2DI_UNS:
14917 case ALTIVEC_BUILTIN_VSEL_16QI_UNS:
14918 case ALTIVEC_BUILTIN_VSEL_8HI_UNS:
14919 case ALTIVEC_BUILTIN_VSEL_4SI_UNS:
14920 case ALTIVEC_BUILTIN_VSEL_2DI_UNS:
14921 case VSX_BUILTIN_VPERM_16QI_UNS:
14922 case VSX_BUILTIN_VPERM_8HI_UNS:
14923 case VSX_BUILTIN_VPERM_4SI_UNS:
14924 case VSX_BUILTIN_VPERM_2DI_UNS:
14925 case VSX_BUILTIN_XXSEL_16QI_UNS:
14926 case VSX_BUILTIN_XXSEL_8HI_UNS:
14927 case VSX_BUILTIN_XXSEL_4SI_UNS:
14928 case VSX_BUILTIN_XXSEL_2DI_UNS:
14929 case CRYPTO_BUILTIN_VPERMXOR:
14930 case CRYPTO_BUILTIN_VPERMXOR_V2DI:
14931 case CRYPTO_BUILTIN_VPERMXOR_V4SI:
14932 case CRYPTO_BUILTIN_VPERMXOR_V8HI:
14933 case CRYPTO_BUILTIN_VPERMXOR_V16QI:
14934 case CRYPTO_BUILTIN_VSHASIGMAW:
14935 case CRYPTO_BUILTIN_VSHASIGMAD:
14936 case CRYPTO_BUILTIN_VSHASIGMA:
14937 h.uns_p[0] = 1;
14938 h.uns_p[1] = 1;
14939 h.uns_p[2] = 1;
14940 h.uns_p[3] = 1;
14941 break;
14943 /* signed permute functions with unsigned char mask. */
14944 case ALTIVEC_BUILTIN_VPERM_16QI:
14945 case ALTIVEC_BUILTIN_VPERM_8HI:
14946 case ALTIVEC_BUILTIN_VPERM_4SI:
14947 case ALTIVEC_BUILTIN_VPERM_4SF:
14948 case ALTIVEC_BUILTIN_VPERM_2DI:
14949 case ALTIVEC_BUILTIN_VPERM_2DF:
14950 case VSX_BUILTIN_VPERM_16QI:
14951 case VSX_BUILTIN_VPERM_8HI:
14952 case VSX_BUILTIN_VPERM_4SI:
14953 case VSX_BUILTIN_VPERM_4SF:
14954 case VSX_BUILTIN_VPERM_2DI:
14955 case VSX_BUILTIN_VPERM_2DF:
14956 h.uns_p[3] = 1;
14957 break;
14959 /* unsigned args, signed return. */
14960 case VSX_BUILTIN_XVCVUXDDP_UNS:
14961 case ALTIVEC_BUILTIN_UNSFLOAT_V4SI_V4SF:
14962 h.uns_p[1] = 1;
14963 break;
14965 /* signed args, unsigned return. */
14966 case VSX_BUILTIN_XVCVDPUXDS_UNS:
14967 case ALTIVEC_BUILTIN_FIXUNS_V4SF_V4SI:
14968 h.uns_p[0] = 1;
14969 break;
14971 default:
14972 break;
14975 /* Figure out how many args are present. */
14976 while (num_args > 0 && h.mode[num_args] == VOIDmode)
14977 num_args--;
14979 if (num_args == 0)
14980 fatal_error ("internal error: builtin function %s had no type", name);
14982 ret_type = builtin_mode_to_type[h.mode[0]][h.uns_p[0]];
14983 if (!ret_type && h.uns_p[0])
14984 ret_type = builtin_mode_to_type[h.mode[0]][0];
14986 if (!ret_type)
14987 fatal_error ("internal error: builtin function %s had an unexpected "
14988 "return type %s", name, GET_MODE_NAME (h.mode[0]));
14990 for (i = 0; i < (int) ARRAY_SIZE (arg_type); i++)
14991 arg_type[i] = NULL_TREE;
14993 for (i = 0; i < num_args; i++)
14995 int m = (int) h.mode[i+1];
14996 int uns_p = h.uns_p[i+1];
14998 arg_type[i] = builtin_mode_to_type[m][uns_p];
14999 if (!arg_type[i] && uns_p)
15000 arg_type[i] = builtin_mode_to_type[m][0];
15002 if (!arg_type[i])
15003 fatal_error ("internal error: builtin function %s, argument %d "
15004 "had unexpected argument type %s", name, i,
15005 GET_MODE_NAME (m));
15008 found = htab_find_slot (builtin_hash_table, &h, INSERT);
15009 if (*found == NULL)
15011 h2 = ggc_alloc_builtin_hash_struct ();
15012 *h2 = h;
15013 *found = (void *)h2;
15015 h2->type = build_function_type_list (ret_type, arg_type[0], arg_type[1],
15016 arg_type[2], NULL_TREE);
15019 return ((struct builtin_hash_struct *)(*found))->type;
15022 static void
15023 rs6000_common_init_builtins (void)
15025 const struct builtin_description *d;
15026 size_t i;
15028 tree opaque_ftype_opaque = NULL_TREE;
15029 tree opaque_ftype_opaque_opaque = NULL_TREE;
15030 tree opaque_ftype_opaque_opaque_opaque = NULL_TREE;
15031 tree v2si_ftype_qi = NULL_TREE;
15032 tree v2si_ftype_v2si_qi = NULL_TREE;
15033 tree v2si_ftype_int_qi = NULL_TREE;
15034 HOST_WIDE_INT builtin_mask = rs6000_builtin_mask;
15036 if (!TARGET_PAIRED_FLOAT)
15038 builtin_mode_to_type[V2SImode][0] = opaque_V2SI_type_node;
15039 builtin_mode_to_type[V2SFmode][0] = opaque_V2SF_type_node;
15042 /* Paired and SPE builtins are only available if you build a compiler with
15043 the appropriate options, so only create those builtins with the
15044 appropriate compiler option. Create Altivec and VSX builtins on machines
15045 with at least the general purpose extensions (970 and newer) to allow the
15046 use of the target attribute.. */
15048 if (TARGET_EXTRA_BUILTINS)
15049 builtin_mask |= RS6000_BTM_COMMON;
15051 /* Add the ternary operators. */
15052 d = bdesc_3arg;
15053 for (i = 0; i < ARRAY_SIZE (bdesc_3arg); i++, d++)
15055 tree type;
15056 HOST_WIDE_INT mask = d->mask;
15058 if ((mask & builtin_mask) != mask)
15060 if (TARGET_DEBUG_BUILTIN)
15061 fprintf (stderr, "rs6000_builtin, skip ternary %s\n", d->name);
15062 continue;
15065 if (rs6000_overloaded_builtin_p (d->code))
15067 if (! (type = opaque_ftype_opaque_opaque_opaque))
15068 type = opaque_ftype_opaque_opaque_opaque
15069 = build_function_type_list (opaque_V4SI_type_node,
15070 opaque_V4SI_type_node,
15071 opaque_V4SI_type_node,
15072 opaque_V4SI_type_node,
15073 NULL_TREE);
15075 else
15077 enum insn_code icode = d->icode;
15078 if (d->name == 0)
15080 if (TARGET_DEBUG_BUILTIN)
15081 fprintf (stderr, "rs6000_builtin, bdesc_3arg[%ld] no name\n",
15082 (long unsigned)i);
15084 continue;
15087 if (icode == CODE_FOR_nothing)
15089 if (TARGET_DEBUG_BUILTIN)
15090 fprintf (stderr, "rs6000_builtin, skip ternary %s (no code)\n",
15091 d->name);
15093 continue;
15096 type = builtin_function_type (insn_data[icode].operand[0].mode,
15097 insn_data[icode].operand[1].mode,
15098 insn_data[icode].operand[2].mode,
15099 insn_data[icode].operand[3].mode,
15100 d->code, d->name);
15103 def_builtin (d->name, type, d->code);
15106 /* Add the binary operators. */
15107 d = bdesc_2arg;
15108 for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
15110 enum machine_mode mode0, mode1, mode2;
15111 tree type;
15112 HOST_WIDE_INT mask = d->mask;
15114 if ((mask & builtin_mask) != mask)
15116 if (TARGET_DEBUG_BUILTIN)
15117 fprintf (stderr, "rs6000_builtin, skip binary %s\n", d->name);
15118 continue;
15121 if (rs6000_overloaded_builtin_p (d->code))
15123 if (! (type = opaque_ftype_opaque_opaque))
15124 type = opaque_ftype_opaque_opaque
15125 = build_function_type_list (opaque_V4SI_type_node,
15126 opaque_V4SI_type_node,
15127 opaque_V4SI_type_node,
15128 NULL_TREE);
15130 else
15132 enum insn_code icode = d->icode;
15133 if (d->name == 0)
15135 if (TARGET_DEBUG_BUILTIN)
15136 fprintf (stderr, "rs6000_builtin, bdesc_2arg[%ld] no name\n",
15137 (long unsigned)i);
15139 continue;
15142 if (icode == CODE_FOR_nothing)
15144 if (TARGET_DEBUG_BUILTIN)
15145 fprintf (stderr, "rs6000_builtin, skip binary %s (no code)\n",
15146 d->name);
15148 continue;
15151 mode0 = insn_data[icode].operand[0].mode;
15152 mode1 = insn_data[icode].operand[1].mode;
15153 mode2 = insn_data[icode].operand[2].mode;
15155 if (mode0 == V2SImode && mode1 == V2SImode && mode2 == QImode)
15157 if (! (type = v2si_ftype_v2si_qi))
15158 type = v2si_ftype_v2si_qi
15159 = build_function_type_list (opaque_V2SI_type_node,
15160 opaque_V2SI_type_node,
15161 char_type_node,
15162 NULL_TREE);
15165 else if (mode0 == V2SImode && GET_MODE_CLASS (mode1) == MODE_INT
15166 && mode2 == QImode)
15168 if (! (type = v2si_ftype_int_qi))
15169 type = v2si_ftype_int_qi
15170 = build_function_type_list (opaque_V2SI_type_node,
15171 integer_type_node,
15172 char_type_node,
15173 NULL_TREE);
15176 else
15177 type = builtin_function_type (mode0, mode1, mode2, VOIDmode,
15178 d->code, d->name);
15181 def_builtin (d->name, type, d->code);
15184 /* Add the simple unary operators. */
15185 d = bdesc_1arg;
15186 for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
15188 enum machine_mode mode0, mode1;
15189 tree type;
15190 HOST_WIDE_INT mask = d->mask;
15192 if ((mask & builtin_mask) != mask)
15194 if (TARGET_DEBUG_BUILTIN)
15195 fprintf (stderr, "rs6000_builtin, skip unary %s\n", d->name);
15196 continue;
15199 if (rs6000_overloaded_builtin_p (d->code))
15201 if (! (type = opaque_ftype_opaque))
15202 type = opaque_ftype_opaque
15203 = build_function_type_list (opaque_V4SI_type_node,
15204 opaque_V4SI_type_node,
15205 NULL_TREE);
15207 else
15209 enum insn_code icode = d->icode;
15210 if (d->name == 0)
15212 if (TARGET_DEBUG_BUILTIN)
15213 fprintf (stderr, "rs6000_builtin, bdesc_1arg[%ld] no name\n",
15214 (long unsigned)i);
15216 continue;
15219 if (icode == CODE_FOR_nothing)
15221 if (TARGET_DEBUG_BUILTIN)
15222 fprintf (stderr, "rs6000_builtin, skip unary %s (no code)\n",
15223 d->name);
15225 continue;
15228 mode0 = insn_data[icode].operand[0].mode;
15229 mode1 = insn_data[icode].operand[1].mode;
15231 if (mode0 == V2SImode && mode1 == QImode)
15233 if (! (type = v2si_ftype_qi))
15234 type = v2si_ftype_qi
15235 = build_function_type_list (opaque_V2SI_type_node,
15236 char_type_node,
15237 NULL_TREE);
15240 else
15241 type = builtin_function_type (mode0, mode1, VOIDmode, VOIDmode,
15242 d->code, d->name);
15245 def_builtin (d->name, type, d->code);
15249 static void
15250 rs6000_init_libfuncs (void)
15252 if (!TARGET_IEEEQUAD)
15253 /* AIX/Darwin/64-bit Linux quad floating point routines. */
15254 if (!TARGET_XL_COMPAT)
15256 set_optab_libfunc (add_optab, TFmode, "__gcc_qadd");
15257 set_optab_libfunc (sub_optab, TFmode, "__gcc_qsub");
15258 set_optab_libfunc (smul_optab, TFmode, "__gcc_qmul");
15259 set_optab_libfunc (sdiv_optab, TFmode, "__gcc_qdiv");
15261 if (!(TARGET_HARD_FLOAT && (TARGET_FPRS || TARGET_E500_DOUBLE)))
15263 set_optab_libfunc (neg_optab, TFmode, "__gcc_qneg");
15264 set_optab_libfunc (eq_optab, TFmode, "__gcc_qeq");
15265 set_optab_libfunc (ne_optab, TFmode, "__gcc_qne");
15266 set_optab_libfunc (gt_optab, TFmode, "__gcc_qgt");
15267 set_optab_libfunc (ge_optab, TFmode, "__gcc_qge");
15268 set_optab_libfunc (lt_optab, TFmode, "__gcc_qlt");
15269 set_optab_libfunc (le_optab, TFmode, "__gcc_qle");
15271 set_conv_libfunc (sext_optab, TFmode, SFmode, "__gcc_stoq");
15272 set_conv_libfunc (sext_optab, TFmode, DFmode, "__gcc_dtoq");
15273 set_conv_libfunc (trunc_optab, SFmode, TFmode, "__gcc_qtos");
15274 set_conv_libfunc (trunc_optab, DFmode, TFmode, "__gcc_qtod");
15275 set_conv_libfunc (sfix_optab, SImode, TFmode, "__gcc_qtoi");
15276 set_conv_libfunc (ufix_optab, SImode, TFmode, "__gcc_qtou");
15277 set_conv_libfunc (sfloat_optab, TFmode, SImode, "__gcc_itoq");
15278 set_conv_libfunc (ufloat_optab, TFmode, SImode, "__gcc_utoq");
15281 if (!(TARGET_HARD_FLOAT && TARGET_FPRS))
15282 set_optab_libfunc (unord_optab, TFmode, "__gcc_qunord");
15284 else
15286 set_optab_libfunc (add_optab, TFmode, "_xlqadd");
15287 set_optab_libfunc (sub_optab, TFmode, "_xlqsub");
15288 set_optab_libfunc (smul_optab, TFmode, "_xlqmul");
15289 set_optab_libfunc (sdiv_optab, TFmode, "_xlqdiv");
15291 else
15293 /* 32-bit SVR4 quad floating point routines. */
15295 set_optab_libfunc (add_optab, TFmode, "_q_add");
15296 set_optab_libfunc (sub_optab, TFmode, "_q_sub");
15297 set_optab_libfunc (neg_optab, TFmode, "_q_neg");
15298 set_optab_libfunc (smul_optab, TFmode, "_q_mul");
15299 set_optab_libfunc (sdiv_optab, TFmode, "_q_div");
15300 if (TARGET_PPC_GPOPT)
15301 set_optab_libfunc (sqrt_optab, TFmode, "_q_sqrt");
15303 set_optab_libfunc (eq_optab, TFmode, "_q_feq");
15304 set_optab_libfunc (ne_optab, TFmode, "_q_fne");
15305 set_optab_libfunc (gt_optab, TFmode, "_q_fgt");
15306 set_optab_libfunc (ge_optab, TFmode, "_q_fge");
15307 set_optab_libfunc (lt_optab, TFmode, "_q_flt");
15308 set_optab_libfunc (le_optab, TFmode, "_q_fle");
15310 set_conv_libfunc (sext_optab, TFmode, SFmode, "_q_stoq");
15311 set_conv_libfunc (sext_optab, TFmode, DFmode, "_q_dtoq");
15312 set_conv_libfunc (trunc_optab, SFmode, TFmode, "_q_qtos");
15313 set_conv_libfunc (trunc_optab, DFmode, TFmode, "_q_qtod");
15314 set_conv_libfunc (sfix_optab, SImode, TFmode, "_q_qtoi");
15315 set_conv_libfunc (ufix_optab, SImode, TFmode, "_q_qtou");
15316 set_conv_libfunc (sfloat_optab, TFmode, SImode, "_q_itoq");
15317 set_conv_libfunc (ufloat_optab, TFmode, SImode, "_q_utoq");
15322 /* Expand a block clear operation, and return 1 if successful. Return 0
15323 if we should let the compiler generate normal code.
15325 operands[0] is the destination
15326 operands[1] is the length
15327 operands[3] is the alignment */
15330 expand_block_clear (rtx operands[])
15332 rtx orig_dest = operands[0];
15333 rtx bytes_rtx = operands[1];
15334 rtx align_rtx = operands[3];
15335 bool constp = (GET_CODE (bytes_rtx) == CONST_INT);
15336 HOST_WIDE_INT align;
15337 HOST_WIDE_INT bytes;
15338 int offset;
15339 int clear_bytes;
15340 int clear_step;
15342 /* If this is not a fixed size move, just call memcpy */
15343 if (! constp)
15344 return 0;
15346 /* This must be a fixed size alignment */
15347 gcc_assert (GET_CODE (align_rtx) == CONST_INT);
15348 align = INTVAL (align_rtx) * BITS_PER_UNIT;
15350 /* Anything to clear? */
15351 bytes = INTVAL (bytes_rtx);
15352 if (bytes <= 0)
15353 return 1;
15355 /* Use the builtin memset after a point, to avoid huge code bloat.
15356 When optimize_size, avoid any significant code bloat; calling
15357 memset is about 4 instructions, so allow for one instruction to
15358 load zero and three to do clearing. */
15359 if (TARGET_ALTIVEC && align >= 128)
15360 clear_step = 16;
15361 else if (TARGET_POWERPC64 && align >= 32)
15362 clear_step = 8;
15363 else if (TARGET_SPE && align >= 64)
15364 clear_step = 8;
15365 else
15366 clear_step = 4;
15368 if (optimize_size && bytes > 3 * clear_step)
15369 return 0;
15370 if (! optimize_size && bytes > 8 * clear_step)
15371 return 0;
15373 for (offset = 0; bytes > 0; offset += clear_bytes, bytes -= clear_bytes)
15375 enum machine_mode mode = BLKmode;
15376 rtx dest;
15378 if (bytes >= 16 && TARGET_ALTIVEC && align >= 128)
15380 clear_bytes = 16;
15381 mode = V4SImode;
15383 else if (bytes >= 8 && TARGET_SPE && align >= 64)
15385 clear_bytes = 8;
15386 mode = V2SImode;
15388 else if (bytes >= 8 && TARGET_POWERPC64
15389 /* 64-bit loads and stores require word-aligned
15390 displacements. */
15391 && (align >= 64 || (!STRICT_ALIGNMENT && align >= 32)))
15393 clear_bytes = 8;
15394 mode = DImode;
15396 else if (bytes >= 4 && (align >= 32 || !STRICT_ALIGNMENT))
15397 { /* move 4 bytes */
15398 clear_bytes = 4;
15399 mode = SImode;
15401 else if (bytes >= 2 && (align >= 16 || !STRICT_ALIGNMENT))
15402 { /* move 2 bytes */
15403 clear_bytes = 2;
15404 mode = HImode;
15406 else /* move 1 byte at a time */
15408 clear_bytes = 1;
15409 mode = QImode;
15412 dest = adjust_address (orig_dest, mode, offset);
15414 emit_move_insn (dest, CONST0_RTX (mode));
15417 return 1;
15421 /* Expand a block move operation, and return 1 if successful. Return 0
15422 if we should let the compiler generate normal code.
15424 operands[0] is the destination
15425 operands[1] is the source
15426 operands[2] is the length
15427 operands[3] is the alignment */
15429 #define MAX_MOVE_REG 4
15432 expand_block_move (rtx operands[])
15434 rtx orig_dest = operands[0];
15435 rtx orig_src = operands[1];
15436 rtx bytes_rtx = operands[2];
15437 rtx align_rtx = operands[3];
15438 int constp = (GET_CODE (bytes_rtx) == CONST_INT);
15439 int align;
15440 int bytes;
15441 int offset;
15442 int move_bytes;
15443 rtx stores[MAX_MOVE_REG];
15444 int num_reg = 0;
15446 /* If this is not a fixed size move, just call memcpy */
15447 if (! constp)
15448 return 0;
15450 /* This must be a fixed size alignment */
15451 gcc_assert (GET_CODE (align_rtx) == CONST_INT);
15452 align = INTVAL (align_rtx) * BITS_PER_UNIT;
15454 /* Anything to move? */
15455 bytes = INTVAL (bytes_rtx);
15456 if (bytes <= 0)
15457 return 1;
15459 if (bytes > rs6000_block_move_inline_limit)
15460 return 0;
15462 for (offset = 0; bytes > 0; offset += move_bytes, bytes -= move_bytes)
15464 union {
15465 rtx (*movmemsi) (rtx, rtx, rtx, rtx);
15466 rtx (*mov) (rtx, rtx);
15467 } gen_func;
15468 enum machine_mode mode = BLKmode;
15469 rtx src, dest;
15471 /* Altivec first, since it will be faster than a string move
15472 when it applies, and usually not significantly larger. */
15473 if (TARGET_ALTIVEC && bytes >= 16 && align >= 128)
15475 move_bytes = 16;
15476 mode = V4SImode;
15477 gen_func.mov = gen_movv4si;
15479 else if (TARGET_SPE && bytes >= 8 && align >= 64)
15481 move_bytes = 8;
15482 mode = V2SImode;
15483 gen_func.mov = gen_movv2si;
15485 else if (TARGET_STRING
15486 && bytes > 24 /* move up to 32 bytes at a time */
15487 && ! fixed_regs[5]
15488 && ! fixed_regs[6]
15489 && ! fixed_regs[7]
15490 && ! fixed_regs[8]
15491 && ! fixed_regs[9]
15492 && ! fixed_regs[10]
15493 && ! fixed_regs[11]
15494 && ! fixed_regs[12])
15496 move_bytes = (bytes > 32) ? 32 : bytes;
15497 gen_func.movmemsi = gen_movmemsi_8reg;
15499 else if (TARGET_STRING
15500 && bytes > 16 /* move up to 24 bytes at a time */
15501 && ! fixed_regs[5]
15502 && ! fixed_regs[6]
15503 && ! fixed_regs[7]
15504 && ! fixed_regs[8]
15505 && ! fixed_regs[9]
15506 && ! fixed_regs[10])
15508 move_bytes = (bytes > 24) ? 24 : bytes;
15509 gen_func.movmemsi = gen_movmemsi_6reg;
15511 else if (TARGET_STRING
15512 && bytes > 8 /* move up to 16 bytes at a time */
15513 && ! fixed_regs[5]
15514 && ! fixed_regs[6]
15515 && ! fixed_regs[7]
15516 && ! fixed_regs[8])
15518 move_bytes = (bytes > 16) ? 16 : bytes;
15519 gen_func.movmemsi = gen_movmemsi_4reg;
15521 else if (bytes >= 8 && TARGET_POWERPC64
15522 /* 64-bit loads and stores require word-aligned
15523 displacements. */
15524 && (align >= 64 || (!STRICT_ALIGNMENT && align >= 32)))
15526 move_bytes = 8;
15527 mode = DImode;
15528 gen_func.mov = gen_movdi;
15530 else if (TARGET_STRING && bytes > 4 && !TARGET_POWERPC64)
15531 { /* move up to 8 bytes at a time */
15532 move_bytes = (bytes > 8) ? 8 : bytes;
15533 gen_func.movmemsi = gen_movmemsi_2reg;
15535 else if (bytes >= 4 && (align >= 32 || !STRICT_ALIGNMENT))
15536 { /* move 4 bytes */
15537 move_bytes = 4;
15538 mode = SImode;
15539 gen_func.mov = gen_movsi;
15541 else if (bytes >= 2 && (align >= 16 || !STRICT_ALIGNMENT))
15542 { /* move 2 bytes */
15543 move_bytes = 2;
15544 mode = HImode;
15545 gen_func.mov = gen_movhi;
15547 else if (TARGET_STRING && bytes > 1)
15548 { /* move up to 4 bytes at a time */
15549 move_bytes = (bytes > 4) ? 4 : bytes;
15550 gen_func.movmemsi = gen_movmemsi_1reg;
15552 else /* move 1 byte at a time */
15554 move_bytes = 1;
15555 mode = QImode;
15556 gen_func.mov = gen_movqi;
15559 src = adjust_address (orig_src, mode, offset);
15560 dest = adjust_address (orig_dest, mode, offset);
15562 if (mode != BLKmode)
15564 rtx tmp_reg = gen_reg_rtx (mode);
15566 emit_insn ((*gen_func.mov) (tmp_reg, src));
15567 stores[num_reg++] = (*gen_func.mov) (dest, tmp_reg);
15570 if (mode == BLKmode || num_reg >= MAX_MOVE_REG || bytes == move_bytes)
15572 int i;
15573 for (i = 0; i < num_reg; i++)
15574 emit_insn (stores[i]);
15575 num_reg = 0;
15578 if (mode == BLKmode)
15580 /* Move the address into scratch registers. The movmemsi
15581 patterns require zero offset. */
15582 if (!REG_P (XEXP (src, 0)))
15584 rtx src_reg = copy_addr_to_reg (XEXP (src, 0));
15585 src = replace_equiv_address (src, src_reg);
15587 set_mem_size (src, move_bytes);
15589 if (!REG_P (XEXP (dest, 0)))
15591 rtx dest_reg = copy_addr_to_reg (XEXP (dest, 0));
15592 dest = replace_equiv_address (dest, dest_reg);
15594 set_mem_size (dest, move_bytes);
15596 emit_insn ((*gen_func.movmemsi) (dest, src,
15597 GEN_INT (move_bytes & 31),
15598 align_rtx));
15602 return 1;
15606 /* Return a string to perform a load_multiple operation.
15607 operands[0] is the vector.
15608 operands[1] is the source address.
15609 operands[2] is the first destination register. */
15611 const char *
15612 rs6000_output_load_multiple (rtx operands[3])
15614 /* We have to handle the case where the pseudo used to contain the address
15615 is assigned to one of the output registers. */
15616 int i, j;
15617 int words = XVECLEN (operands[0], 0);
15618 rtx xop[10];
15620 if (XVECLEN (operands[0], 0) == 1)
15621 return "lwz %2,0(%1)";
15623 for (i = 0; i < words; i++)
15624 if (refers_to_regno_p (REGNO (operands[2]) + i,
15625 REGNO (operands[2]) + i + 1, operands[1], 0))
15627 if (i == words-1)
15629 xop[0] = GEN_INT (4 * (words-1));
15630 xop[1] = operands[1];
15631 xop[2] = operands[2];
15632 output_asm_insn ("lswi %2,%1,%0\n\tlwz %1,%0(%1)", xop);
15633 return "";
15635 else if (i == 0)
15637 xop[0] = GEN_INT (4 * (words-1));
15638 xop[1] = operands[1];
15639 xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
15640 output_asm_insn ("addi %1,%1,4\n\tlswi %2,%1,%0\n\tlwz %1,-4(%1)", xop);
15641 return "";
15643 else
15645 for (j = 0; j < words; j++)
15646 if (j != i)
15648 xop[0] = GEN_INT (j * 4);
15649 xop[1] = operands[1];
15650 xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + j);
15651 output_asm_insn ("lwz %2,%0(%1)", xop);
15653 xop[0] = GEN_INT (i * 4);
15654 xop[1] = operands[1];
15655 output_asm_insn ("lwz %1,%0(%1)", xop);
15656 return "";
15660 return "lswi %2,%1,%N0";
15664 /* A validation routine: say whether CODE, a condition code, and MODE
15665 match. The other alternatives either don't make sense or should
15666 never be generated. */
15668 void
15669 validate_condition_mode (enum rtx_code code, enum machine_mode mode)
15671 gcc_assert ((GET_RTX_CLASS (code) == RTX_COMPARE
15672 || GET_RTX_CLASS (code) == RTX_COMM_COMPARE)
15673 && GET_MODE_CLASS (mode) == MODE_CC);
15675 /* These don't make sense. */
15676 gcc_assert ((code != GT && code != LT && code != GE && code != LE)
15677 || mode != CCUNSmode);
15679 gcc_assert ((code != GTU && code != LTU && code != GEU && code != LEU)
15680 || mode == CCUNSmode);
15682 gcc_assert (mode == CCFPmode
15683 || (code != ORDERED && code != UNORDERED
15684 && code != UNEQ && code != LTGT
15685 && code != UNGT && code != UNLT
15686 && code != UNGE && code != UNLE));
15688 /* These should never be generated except for
15689 flag_finite_math_only. */
15690 gcc_assert (mode != CCFPmode
15691 || flag_finite_math_only
15692 || (code != LE && code != GE
15693 && code != UNEQ && code != LTGT
15694 && code != UNGT && code != UNLT));
15696 /* These are invalid; the information is not there. */
15697 gcc_assert (mode != CCEQmode || code == EQ || code == NE);
15701 /* Return 1 if ANDOP is a mask that has no bits on that are not in the
15702 mask required to convert the result of a rotate insn into a shift
15703 left insn of SHIFTOP bits. Both are known to be SImode CONST_INT. */
15706 includes_lshift_p (rtx shiftop, rtx andop)
15708 unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
15710 shift_mask <<= INTVAL (shiftop);
15712 return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
15715 /* Similar, but for right shift. */
15718 includes_rshift_p (rtx shiftop, rtx andop)
15720 unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
15722 shift_mask >>= INTVAL (shiftop);
15724 return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
15727 /* Return 1 if ANDOP is a mask suitable for use with an rldic insn
15728 to perform a left shift. It must have exactly SHIFTOP least
15729 significant 0's, then one or more 1's, then zero or more 0's. */
15732 includes_rldic_lshift_p (rtx shiftop, rtx andop)
15734 if (GET_CODE (andop) == CONST_INT)
15736 HOST_WIDE_INT c, lsb, shift_mask;
15738 c = INTVAL (andop);
15739 if (c == 0 || c == ~0)
15740 return 0;
15742 shift_mask = ~0;
15743 shift_mask <<= INTVAL (shiftop);
15745 /* Find the least significant one bit. */
15746 lsb = c & -c;
15748 /* It must coincide with the LSB of the shift mask. */
15749 if (-lsb != shift_mask)
15750 return 0;
15752 /* Invert to look for the next transition (if any). */
15753 c = ~c;
15755 /* Remove the low group of ones (originally low group of zeros). */
15756 c &= -lsb;
15758 /* Again find the lsb, and check we have all 1's above. */
15759 lsb = c & -c;
15760 return c == -lsb;
15762 else
15763 return 0;
15766 /* Return 1 if ANDOP is a mask suitable for use with an rldicr insn
15767 to perform a left shift. It must have SHIFTOP or more least
15768 significant 0's, with the remainder of the word 1's. */
15771 includes_rldicr_lshift_p (rtx shiftop, rtx andop)
15773 if (GET_CODE (andop) == CONST_INT)
15775 HOST_WIDE_INT c, lsb, shift_mask;
15777 shift_mask = ~0;
15778 shift_mask <<= INTVAL (shiftop);
15779 c = INTVAL (andop);
15781 /* Find the least significant one bit. */
15782 lsb = c & -c;
15784 /* It must be covered by the shift mask.
15785 This test also rejects c == 0. */
15786 if ((lsb & shift_mask) == 0)
15787 return 0;
15789 /* Check we have all 1's above the transition, and reject all 1's. */
15790 return c == -lsb && lsb != 1;
15792 else
15793 return 0;
15796 /* Return 1 if operands will generate a valid arguments to rlwimi
15797 instruction for insert with right shift in 64-bit mode. The mask may
15798 not start on the first bit or stop on the last bit because wrap-around
15799 effects of instruction do not correspond to semantics of RTL insn. */
15802 insvdi_rshift_rlwimi_p (rtx sizeop, rtx startop, rtx shiftop)
15804 if (INTVAL (startop) > 32
15805 && INTVAL (startop) < 64
15806 && INTVAL (sizeop) > 1
15807 && INTVAL (sizeop) + INTVAL (startop) < 64
15808 && INTVAL (shiftop) > 0
15809 && INTVAL (sizeop) + INTVAL (shiftop) < 32
15810 && (64 - (INTVAL (shiftop) & 63)) >= INTVAL (sizeop))
15811 return 1;
15813 return 0;
15816 /* Return 1 if REGNO (reg1) == REGNO (reg2) - 1 making them candidates
15817 for lfq and stfq insns iff the registers are hard registers. */
15820 registers_ok_for_quad_peep (rtx reg1, rtx reg2)
15822 /* We might have been passed a SUBREG. */
15823 if (GET_CODE (reg1) != REG || GET_CODE (reg2) != REG)
15824 return 0;
15826 /* We might have been passed non floating point registers. */
15827 if (!FP_REGNO_P (REGNO (reg1))
15828 || !FP_REGNO_P (REGNO (reg2)))
15829 return 0;
15831 return (REGNO (reg1) == REGNO (reg2) - 1);
15834 /* Return 1 if addr1 and addr2 are suitable for lfq or stfq insn.
15835 addr1 and addr2 must be in consecutive memory locations
15836 (addr2 == addr1 + 8). */
15839 mems_ok_for_quad_peep (rtx mem1, rtx mem2)
15841 rtx addr1, addr2;
15842 unsigned int reg1, reg2;
15843 int offset1, offset2;
15845 /* The mems cannot be volatile. */
15846 if (MEM_VOLATILE_P (mem1) || MEM_VOLATILE_P (mem2))
15847 return 0;
15849 addr1 = XEXP (mem1, 0);
15850 addr2 = XEXP (mem2, 0);
15852 /* Extract an offset (if used) from the first addr. */
15853 if (GET_CODE (addr1) == PLUS)
15855 /* If not a REG, return zero. */
15856 if (GET_CODE (XEXP (addr1, 0)) != REG)
15857 return 0;
15858 else
15860 reg1 = REGNO (XEXP (addr1, 0));
15861 /* The offset must be constant! */
15862 if (GET_CODE (XEXP (addr1, 1)) != CONST_INT)
15863 return 0;
15864 offset1 = INTVAL (XEXP (addr1, 1));
15867 else if (GET_CODE (addr1) != REG)
15868 return 0;
15869 else
15871 reg1 = REGNO (addr1);
15872 /* This was a simple (mem (reg)) expression. Offset is 0. */
15873 offset1 = 0;
15876 /* And now for the second addr. */
15877 if (GET_CODE (addr2) == PLUS)
15879 /* If not a REG, return zero. */
15880 if (GET_CODE (XEXP (addr2, 0)) != REG)
15881 return 0;
15882 else
15884 reg2 = REGNO (XEXP (addr2, 0));
15885 /* The offset must be constant. */
15886 if (GET_CODE (XEXP (addr2, 1)) != CONST_INT)
15887 return 0;
15888 offset2 = INTVAL (XEXP (addr2, 1));
15891 else if (GET_CODE (addr2) != REG)
15892 return 0;
15893 else
15895 reg2 = REGNO (addr2);
15896 /* This was a simple (mem (reg)) expression. Offset is 0. */
15897 offset2 = 0;
15900 /* Both of these must have the same base register. */
15901 if (reg1 != reg2)
15902 return 0;
15904 /* The offset for the second addr must be 8 more than the first addr. */
15905 if (offset2 != offset1 + 8)
15906 return 0;
15908 /* All the tests passed. addr1 and addr2 are valid for lfq or stfq
15909 instructions. */
15910 return 1;
15915 rs6000_secondary_memory_needed_rtx (enum machine_mode mode)
15917 static bool eliminated = false;
15918 rtx ret;
15920 if (mode != SDmode || TARGET_NO_SDMODE_STACK)
15921 ret = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
15922 else
15924 rtx mem = cfun->machine->sdmode_stack_slot;
15925 gcc_assert (mem != NULL_RTX);
15927 if (!eliminated)
15929 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
15930 cfun->machine->sdmode_stack_slot = mem;
15931 eliminated = true;
15933 ret = mem;
15936 if (TARGET_DEBUG_ADDR)
15938 fprintf (stderr, "\nrs6000_secondary_memory_needed_rtx, mode %s, rtx:\n",
15939 GET_MODE_NAME (mode));
15940 if (!ret)
15941 fprintf (stderr, "\tNULL_RTX\n");
15942 else
15943 debug_rtx (ret);
15946 return ret;
15949 /* Return the mode to be used for memory when a secondary memory
15950 location is needed. For SDmode values we need to use DDmode, in
15951 all other cases we can use the same mode. */
15952 enum machine_mode
15953 rs6000_secondary_memory_needed_mode (enum machine_mode mode)
15955 if (lra_in_progress && mode == SDmode)
15956 return DDmode;
15957 return mode;
15960 static tree
15961 rs6000_check_sdmode (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
15963 /* Don't walk into types. */
15964 if (*tp == NULL_TREE || *tp == error_mark_node || TYPE_P (*tp))
15966 *walk_subtrees = 0;
15967 return NULL_TREE;
15970 switch (TREE_CODE (*tp))
15972 case VAR_DECL:
15973 case PARM_DECL:
15974 case FIELD_DECL:
15975 case RESULT_DECL:
15976 case SSA_NAME:
15977 case REAL_CST:
15978 case MEM_REF:
15979 case VIEW_CONVERT_EXPR:
15980 if (TYPE_MODE (TREE_TYPE (*tp)) == SDmode)
15981 return *tp;
15982 break;
15983 default:
15984 break;
15987 return NULL_TREE;
15990 /* Classify a register type. Because the FMRGOW/FMRGEW instructions only work
15991 on traditional floating point registers, and the VMRGOW/VMRGEW instructions
15992 only work on the traditional altivec registers, note if an altivec register
15993 was chosen. */
15995 static enum rs6000_reg_type
15996 register_to_reg_type (rtx reg, bool *is_altivec)
15998 HOST_WIDE_INT regno;
15999 enum reg_class rclass;
16001 if (GET_CODE (reg) == SUBREG)
16002 reg = SUBREG_REG (reg);
16004 if (!REG_P (reg))
16005 return NO_REG_TYPE;
16007 regno = REGNO (reg);
16008 if (regno >= FIRST_PSEUDO_REGISTER)
16010 if (!lra_in_progress && !reload_in_progress && !reload_completed)
16011 return PSEUDO_REG_TYPE;
16013 regno = true_regnum (reg);
16014 if (regno < 0 || regno >= FIRST_PSEUDO_REGISTER)
16015 return PSEUDO_REG_TYPE;
16018 gcc_assert (regno >= 0);
16020 if (is_altivec && ALTIVEC_REGNO_P (regno))
16021 *is_altivec = true;
16023 rclass = rs6000_regno_regclass[regno];
16024 return reg_class_to_reg_type[(int)rclass];
16027 /* Helper function for rs6000_secondary_reload to return true if a move to a
16028 different register classe is really a simple move. */
16030 static bool
16031 rs6000_secondary_reload_simple_move (enum rs6000_reg_type to_type,
16032 enum rs6000_reg_type from_type,
16033 enum machine_mode mode)
16035 int size;
16037 /* Add support for various direct moves available. In this function, we only
16038 look at cases where we don't need any extra registers, and one or more
16039 simple move insns are issued. At present, 32-bit integers are not allowed
16040 in FPR/VSX registers. Single precision binary floating is not a simple
16041 move because we need to convert to the single precision memory layout.
16042 The 4-byte SDmode can be moved. */
16043 size = GET_MODE_SIZE (mode);
16044 if (TARGET_DIRECT_MOVE
16045 && ((mode == SDmode) || (TARGET_POWERPC64 && size == 8))
16046 && ((to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
16047 || (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)))
16048 return true;
16050 else if (TARGET_MFPGPR && TARGET_POWERPC64 && size == 8
16051 && ((to_type == GPR_REG_TYPE && from_type == FPR_REG_TYPE)
16052 || (to_type == FPR_REG_TYPE && from_type == GPR_REG_TYPE)))
16053 return true;
16055 else if ((size == 4 || (TARGET_POWERPC64 && size == 8))
16056 && ((to_type == GPR_REG_TYPE && from_type == SPR_REG_TYPE)
16057 || (to_type == SPR_REG_TYPE && from_type == GPR_REG_TYPE)))
16058 return true;
16060 return false;
16063 /* Power8 helper function for rs6000_secondary_reload, handle all of the
16064 special direct moves that involve allocating an extra register, return the
16065 insn code of the helper function if there is such a function or
16066 CODE_FOR_nothing if not. */
16068 static bool
16069 rs6000_secondary_reload_direct_move (enum rs6000_reg_type to_type,
16070 enum rs6000_reg_type from_type,
16071 enum machine_mode mode,
16072 secondary_reload_info *sri,
16073 bool altivec_p)
16075 bool ret = false;
16076 enum insn_code icode = CODE_FOR_nothing;
16077 int cost = 0;
16078 int size = GET_MODE_SIZE (mode);
16080 if (TARGET_POWERPC64)
16082 if (size == 16)
16084 /* Handle moving 128-bit values from GPRs to VSX point registers on
16085 power8 when running in 64-bit mode using XXPERMDI to glue the two
16086 64-bit values back together. */
16087 if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)
16089 cost = 3; /* 2 mtvsrd's, 1 xxpermdi. */
16090 icode = reg_addr[mode].reload_vsx_gpr;
16093 /* Handle moving 128-bit values from VSX point registers to GPRs on
16094 power8 when running in 64-bit mode using XXPERMDI to get access to the
16095 bottom 64-bit value. */
16096 else if (to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
16098 cost = 3; /* 2 mfvsrd's, 1 xxpermdi. */
16099 icode = reg_addr[mode].reload_gpr_vsx;
16103 else if (mode == SFmode)
16105 if (to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
16107 cost = 3; /* xscvdpspn, mfvsrd, and. */
16108 icode = reg_addr[mode].reload_gpr_vsx;
16111 else if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)
16113 cost = 2; /* mtvsrz, xscvspdpn. */
16114 icode = reg_addr[mode].reload_vsx_gpr;
16119 if (TARGET_POWERPC64 && size == 16)
16121 /* Handle moving 128-bit values from GPRs to VSX point registers on
16122 power8 when running in 64-bit mode using XXPERMDI to glue the two
16123 64-bit values back together. */
16124 if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)
16126 cost = 3; /* 2 mtvsrd's, 1 xxpermdi. */
16127 icode = reg_addr[mode].reload_vsx_gpr;
16130 /* Handle moving 128-bit values from VSX point registers to GPRs on
16131 power8 when running in 64-bit mode using XXPERMDI to get access to the
16132 bottom 64-bit value. */
16133 else if (to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
16135 cost = 3; /* 2 mfvsrd's, 1 xxpermdi. */
16136 icode = reg_addr[mode].reload_gpr_vsx;
16140 else if (!TARGET_POWERPC64 && size == 8)
16142 /* Handle moving 64-bit values from GPRs to floating point registers on
16143 power8 when running in 32-bit mode using FMRGOW to glue the two 32-bit
16144 values back together. Altivec register classes must be handled
16145 specially since a different instruction is used, and the secondary
16146 reload support requires a single instruction class in the scratch
16147 register constraint. However, right now TFmode is not allowed in
16148 Altivec registers, so the pattern will never match. */
16149 if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE && !altivec_p)
16151 cost = 3; /* 2 mtvsrwz's, 1 fmrgow. */
16152 icode = reg_addr[mode].reload_fpr_gpr;
16156 if (icode != CODE_FOR_nothing)
16158 ret = true;
16159 if (sri)
16161 sri->icode = icode;
16162 sri->extra_cost = cost;
16166 return ret;
16169 /* Return whether a move between two register classes can be done either
16170 directly (simple move) or via a pattern that uses a single extra temporary
16171 (using power8's direct move in this case. */
16173 static bool
16174 rs6000_secondary_reload_move (enum rs6000_reg_type to_type,
16175 enum rs6000_reg_type from_type,
16176 enum machine_mode mode,
16177 secondary_reload_info *sri,
16178 bool altivec_p)
16180 /* Fall back to load/store reloads if either type is not a register. */
16181 if (to_type == NO_REG_TYPE || from_type == NO_REG_TYPE)
16182 return false;
16184 /* If we haven't allocated registers yet, assume the move can be done for the
16185 standard register types. */
16186 if ((to_type == PSEUDO_REG_TYPE && from_type == PSEUDO_REG_TYPE)
16187 || (to_type == PSEUDO_REG_TYPE && IS_STD_REG_TYPE (from_type))
16188 || (from_type == PSEUDO_REG_TYPE && IS_STD_REG_TYPE (to_type)))
16189 return true;
16191 /* Moves to the same set of registers is a simple move for non-specialized
16192 registers. */
16193 if (to_type == from_type && IS_STD_REG_TYPE (to_type))
16194 return true;
16196 /* Check whether a simple move can be done directly. */
16197 if (rs6000_secondary_reload_simple_move (to_type, from_type, mode))
16199 if (sri)
16201 sri->icode = CODE_FOR_nothing;
16202 sri->extra_cost = 0;
16204 return true;
16207 /* Now check if we can do it in a few steps. */
16208 return rs6000_secondary_reload_direct_move (to_type, from_type, mode, sri,
16209 altivec_p);
16212 /* Inform reload about cases where moving X with a mode MODE to a register in
16213 RCLASS requires an extra scratch or immediate register. Return the class
16214 needed for the immediate register.
16216 For VSX and Altivec, we may need a register to convert sp+offset into
16217 reg+sp.
16219 For misaligned 64-bit gpr loads and stores we need a register to
16220 convert an offset address to indirect. */
16222 static reg_class_t
16223 rs6000_secondary_reload (bool in_p,
16224 rtx x,
16225 reg_class_t rclass_i,
16226 enum machine_mode mode,
16227 secondary_reload_info *sri)
16229 enum reg_class rclass = (enum reg_class) rclass_i;
16230 reg_class_t ret = ALL_REGS;
16231 enum insn_code icode;
16232 bool default_p = false;
16234 sri->icode = CODE_FOR_nothing;
16235 icode = ((in_p)
16236 ? reg_addr[mode].reload_load
16237 : reg_addr[mode].reload_store);
16239 if (REG_P (x) || register_operand (x, mode))
16241 enum rs6000_reg_type to_type = reg_class_to_reg_type[(int)rclass];
16242 bool altivec_p = (rclass == ALTIVEC_REGS);
16243 enum rs6000_reg_type from_type = register_to_reg_type (x, &altivec_p);
16245 if (!in_p)
16247 enum rs6000_reg_type exchange = to_type;
16248 to_type = from_type;
16249 from_type = exchange;
16252 /* Can we do a direct move of some sort? */
16253 if (rs6000_secondary_reload_move (to_type, from_type, mode, sri,
16254 altivec_p))
16256 icode = (enum insn_code)sri->icode;
16257 default_p = false;
16258 ret = NO_REGS;
16262 /* Handle vector moves with reload helper functions. */
16263 if (ret == ALL_REGS && icode != CODE_FOR_nothing)
16265 ret = NO_REGS;
16266 sri->icode = CODE_FOR_nothing;
16267 sri->extra_cost = 0;
16269 if (GET_CODE (x) == MEM)
16271 rtx addr = XEXP (x, 0);
16273 /* Loads to and stores from gprs can do reg+offset, and wouldn't need
16274 an extra register in that case, but it would need an extra
16275 register if the addressing is reg+reg or (reg+reg)&(-16). Special
16276 case load/store quad. */
16277 if (rclass == GENERAL_REGS || rclass == BASE_REGS)
16279 if (TARGET_POWERPC64 && TARGET_QUAD_MEMORY
16280 && GET_MODE_SIZE (mode) == 16
16281 && quad_memory_operand (x, mode))
16283 sri->icode = icode;
16284 sri->extra_cost = 2;
16287 else if (!legitimate_indirect_address_p (addr, false)
16288 && !rs6000_legitimate_offset_address_p (PTImode, addr,
16289 false, true))
16291 sri->icode = icode;
16292 /* account for splitting the loads, and converting the
16293 address from reg+reg to reg. */
16294 sri->extra_cost = (((TARGET_64BIT) ? 3 : 5)
16295 + ((GET_CODE (addr) == AND) ? 1 : 0));
16298 /* Allow scalar loads to/from the traditional floating point
16299 registers, even if VSX memory is set. */
16300 else if ((rclass == FLOAT_REGS || rclass == NO_REGS)
16301 && (GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8)
16302 && (legitimate_indirect_address_p (addr, false)
16303 || legitimate_indirect_address_p (addr, false)
16304 || rs6000_legitimate_offset_address_p (mode, addr,
16305 false, true)))
16308 /* Loads to and stores from vector registers can only do reg+reg
16309 addressing. Altivec registers can also do (reg+reg)&(-16). Allow
16310 scalar modes loading up the traditional floating point registers
16311 to use offset addresses. */
16312 else if (rclass == VSX_REGS || rclass == ALTIVEC_REGS
16313 || rclass == FLOAT_REGS || rclass == NO_REGS)
16315 if (!VECTOR_MEM_ALTIVEC_P (mode)
16316 && GET_CODE (addr) == AND
16317 && GET_CODE (XEXP (addr, 1)) == CONST_INT
16318 && INTVAL (XEXP (addr, 1)) == -16
16319 && (legitimate_indirect_address_p (XEXP (addr, 0), false)
16320 || legitimate_indexed_address_p (XEXP (addr, 0), false)))
16322 sri->icode = icode;
16323 sri->extra_cost = ((GET_CODE (XEXP (addr, 0)) == PLUS)
16324 ? 2 : 1);
16326 else if (!legitimate_indirect_address_p (addr, false)
16327 && (rclass == NO_REGS
16328 || !legitimate_indexed_address_p (addr, false)))
16330 sri->icode = icode;
16331 sri->extra_cost = 1;
16333 else
16334 icode = CODE_FOR_nothing;
16336 /* Any other loads, including to pseudo registers which haven't been
16337 assigned to a register yet, default to require a scratch
16338 register. */
16339 else
16341 sri->icode = icode;
16342 sri->extra_cost = 2;
16345 else if (REG_P (x))
16347 int regno = true_regnum (x);
16349 icode = CODE_FOR_nothing;
16350 if (regno < 0 || regno >= FIRST_PSEUDO_REGISTER)
16351 default_p = true;
16352 else
16354 enum reg_class xclass = REGNO_REG_CLASS (regno);
16355 enum rs6000_reg_type rtype1 = reg_class_to_reg_type[(int)rclass];
16356 enum rs6000_reg_type rtype2 = reg_class_to_reg_type[(int)xclass];
16358 /* If memory is needed, use default_secondary_reload to create the
16359 stack slot. */
16360 if (rtype1 != rtype2 || !IS_STD_REG_TYPE (rtype1))
16361 default_p = true;
16362 else
16363 ret = NO_REGS;
16366 else
16367 default_p = true;
16369 else if (TARGET_POWERPC64
16370 && reg_class_to_reg_type[(int)rclass] == GPR_REG_TYPE
16371 && MEM_P (x)
16372 && GET_MODE_SIZE (GET_MODE (x)) >= UNITS_PER_WORD)
16374 rtx addr = XEXP (x, 0);
16375 rtx off = address_offset (addr);
16377 if (off != NULL_RTX)
16379 unsigned int extra = GET_MODE_SIZE (GET_MODE (x)) - UNITS_PER_WORD;
16380 unsigned HOST_WIDE_INT offset = INTVAL (off);
16382 /* We need a secondary reload when our legitimate_address_p
16383 says the address is good (as otherwise the entire address
16384 will be reloaded), and the offset is not a multiple of
16385 four or we have an address wrap. Address wrap will only
16386 occur for LO_SUMs since legitimate_offset_address_p
16387 rejects addresses for 16-byte mems that will wrap. */
16388 if (GET_CODE (addr) == LO_SUM
16389 ? (1 /* legitimate_address_p allows any offset for lo_sum */
16390 && ((offset & 3) != 0
16391 || ((offset & 0xffff) ^ 0x8000) >= 0x10000 - extra))
16392 : (offset + 0x8000 < 0x10000 - extra /* legitimate_address_p */
16393 && (offset & 3) != 0))
16395 if (in_p)
16396 sri->icode = CODE_FOR_reload_di_load;
16397 else
16398 sri->icode = CODE_FOR_reload_di_store;
16399 sri->extra_cost = 2;
16400 ret = NO_REGS;
16402 else
16403 default_p = true;
16405 else
16406 default_p = true;
16408 else if (!TARGET_POWERPC64
16409 && reg_class_to_reg_type[(int)rclass] == GPR_REG_TYPE
16410 && MEM_P (x)
16411 && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
16413 rtx addr = XEXP (x, 0);
16414 rtx off = address_offset (addr);
16416 if (off != NULL_RTX)
16418 unsigned int extra = GET_MODE_SIZE (GET_MODE (x)) - UNITS_PER_WORD;
16419 unsigned HOST_WIDE_INT offset = INTVAL (off);
16421 /* We need a secondary reload when our legitimate_address_p
16422 says the address is good (as otherwise the entire address
16423 will be reloaded), and we have a wrap.
16425 legitimate_lo_sum_address_p allows LO_SUM addresses to
16426 have any offset so test for wrap in the low 16 bits.
16428 legitimate_offset_address_p checks for the range
16429 [-0x8000,0x7fff] for mode size of 8 and [-0x8000,0x7ff7]
16430 for mode size of 16. We wrap at [0x7ffc,0x7fff] and
16431 [0x7ff4,0x7fff] respectively, so test for the
16432 intersection of these ranges, [0x7ffc,0x7fff] and
16433 [0x7ff4,0x7ff7] respectively.
16435 Note that the address we see here may have been
16436 manipulated by legitimize_reload_address. */
16437 if (GET_CODE (addr) == LO_SUM
16438 ? ((offset & 0xffff) ^ 0x8000) >= 0x10000 - extra
16439 : offset - (0x8000 - extra) < UNITS_PER_WORD)
16441 if (in_p)
16442 sri->icode = CODE_FOR_reload_si_load;
16443 else
16444 sri->icode = CODE_FOR_reload_si_store;
16445 sri->extra_cost = 2;
16446 ret = NO_REGS;
16448 else
16449 default_p = true;
16451 else
16452 default_p = true;
16454 else
16455 default_p = true;
16457 if (default_p)
16458 ret = default_secondary_reload (in_p, x, rclass, mode, sri);
16460 gcc_assert (ret != ALL_REGS);
16462 if (TARGET_DEBUG_ADDR)
16464 fprintf (stderr,
16465 "\nrs6000_secondary_reload, return %s, in_p = %s, rclass = %s, "
16466 "mode = %s",
16467 reg_class_names[ret],
16468 in_p ? "true" : "false",
16469 reg_class_names[rclass],
16470 GET_MODE_NAME (mode));
16472 if (default_p)
16473 fprintf (stderr, ", default secondary reload");
16475 if (sri->icode != CODE_FOR_nothing)
16476 fprintf (stderr, ", reload func = %s, extra cost = %d\n",
16477 insn_data[sri->icode].name, sri->extra_cost);
16478 else
16479 fprintf (stderr, "\n");
16481 debug_rtx (x);
16484 return ret;
16487 /* Better tracing for rs6000_secondary_reload_inner. */
16489 static void
16490 rs6000_secondary_reload_trace (int line, rtx reg, rtx mem, rtx scratch,
16491 bool store_p)
16493 rtx set, clobber;
16495 gcc_assert (reg != NULL_RTX && mem != NULL_RTX && scratch != NULL_RTX);
16497 fprintf (stderr, "rs6000_secondary_reload_inner:%d, type = %s\n", line,
16498 store_p ? "store" : "load");
16500 if (store_p)
16501 set = gen_rtx_SET (VOIDmode, mem, reg);
16502 else
16503 set = gen_rtx_SET (VOIDmode, reg, mem);
16505 clobber = gen_rtx_CLOBBER (VOIDmode, scratch);
16506 debug_rtx (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set, clobber)));
16509 static void
16510 rs6000_secondary_reload_fail (int line, rtx reg, rtx mem, rtx scratch,
16511 bool store_p)
16513 rs6000_secondary_reload_trace (line, reg, mem, scratch, store_p);
16514 gcc_unreachable ();
16517 /* Fixup reload addresses for Altivec or VSX loads/stores to change SP+offset
16518 to SP+reg addressing. */
16520 void
16521 rs6000_secondary_reload_inner (rtx reg, rtx mem, rtx scratch, bool store_p)
16523 int regno = true_regnum (reg);
16524 enum machine_mode mode = GET_MODE (reg);
16525 enum reg_class rclass;
16526 rtx addr;
16527 rtx and_op2 = NULL_RTX;
16528 rtx addr_op1;
16529 rtx addr_op2;
16530 rtx scratch_or_premodify = scratch;
16531 rtx and_rtx;
16532 rtx cc_clobber;
16534 if (TARGET_DEBUG_ADDR)
16535 rs6000_secondary_reload_trace (__LINE__, reg, mem, scratch, store_p);
16537 if (regno < 0 || regno >= FIRST_PSEUDO_REGISTER)
16538 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
16540 if (GET_CODE (mem) != MEM)
16541 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
16543 rclass = REGNO_REG_CLASS (regno);
16544 addr = find_replacement (&XEXP (mem, 0));
16546 switch (rclass)
16548 /* GPRs can handle reg + small constant, all other addresses need to use
16549 the scratch register. */
16550 case GENERAL_REGS:
16551 case BASE_REGS:
16552 if (GET_CODE (addr) == AND)
16554 and_op2 = XEXP (addr, 1);
16555 addr = find_replacement (&XEXP (addr, 0));
16558 if (GET_CODE (addr) == PRE_MODIFY)
16560 scratch_or_premodify = find_replacement (&XEXP (addr, 0));
16561 if (!REG_P (scratch_or_premodify))
16562 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
16564 addr = find_replacement (&XEXP (addr, 1));
16565 if (GET_CODE (addr) != PLUS)
16566 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
16569 if (GET_CODE (addr) == PLUS
16570 && (and_op2 != NULL_RTX
16571 || !rs6000_legitimate_offset_address_p (PTImode, addr,
16572 false, true)))
16574 /* find_replacement already recurses into both operands of
16575 PLUS so we don't need to call it here. */
16576 addr_op1 = XEXP (addr, 0);
16577 addr_op2 = XEXP (addr, 1);
16578 if (!legitimate_indirect_address_p (addr_op1, false))
16579 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
16581 if (!REG_P (addr_op2)
16582 && (GET_CODE (addr_op2) != CONST_INT
16583 || !satisfies_constraint_I (addr_op2)))
16585 if (TARGET_DEBUG_ADDR)
16587 fprintf (stderr,
16588 "\nMove plus addr to register %s, mode = %s: ",
16589 rs6000_reg_names[REGNO (scratch)],
16590 GET_MODE_NAME (mode));
16591 debug_rtx (addr_op2);
16593 rs6000_emit_move (scratch, addr_op2, Pmode);
16594 addr_op2 = scratch;
16597 emit_insn (gen_rtx_SET (VOIDmode,
16598 scratch_or_premodify,
16599 gen_rtx_PLUS (Pmode,
16600 addr_op1,
16601 addr_op2)));
16603 addr = scratch_or_premodify;
16604 scratch_or_premodify = scratch;
16606 else if (!legitimate_indirect_address_p (addr, false)
16607 && !rs6000_legitimate_offset_address_p (PTImode, addr,
16608 false, true))
16610 if (TARGET_DEBUG_ADDR)
16612 fprintf (stderr, "\nMove addr to register %s, mode = %s: ",
16613 rs6000_reg_names[REGNO (scratch_or_premodify)],
16614 GET_MODE_NAME (mode));
16615 debug_rtx (addr);
16617 rs6000_emit_move (scratch_or_premodify, addr, Pmode);
16618 addr = scratch_or_premodify;
16619 scratch_or_premodify = scratch;
16621 break;
16623 /* Float registers can do offset+reg addressing for scalar types. */
16624 case FLOAT_REGS:
16625 if (legitimate_indirect_address_p (addr, false) /* reg */
16626 || legitimate_indexed_address_p (addr, false) /* reg+reg */
16627 || ((GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8)
16628 && and_op2 == NULL_RTX
16629 && scratch_or_premodify == scratch
16630 && rs6000_legitimate_offset_address_p (mode, addr, false, false)))
16631 break;
16633 /* If this isn't a legacy floating point load/store, fall through to the
16634 VSX defaults. */
16636 /* VSX/Altivec registers can only handle reg+reg addressing. Move other
16637 addresses into a scratch register. */
16638 case VSX_REGS:
16639 case ALTIVEC_REGS:
16641 /* With float regs, we need to handle the AND ourselves, since we can't
16642 use the Altivec instruction with an implicit AND -16. Allow scalar
16643 loads to float registers to use reg+offset even if VSX. */
16644 if (GET_CODE (addr) == AND
16645 && (rclass != ALTIVEC_REGS || GET_MODE_SIZE (mode) != 16
16646 || GET_CODE (XEXP (addr, 1)) != CONST_INT
16647 || INTVAL (XEXP (addr, 1)) != -16
16648 || !VECTOR_MEM_ALTIVEC_P (mode)))
16650 and_op2 = XEXP (addr, 1);
16651 addr = find_replacement (&XEXP (addr, 0));
16654 /* If we aren't using a VSX load, save the PRE_MODIFY register and use it
16655 as the address later. */
16656 if (GET_CODE (addr) == PRE_MODIFY
16657 && ((ALTIVEC_OR_VSX_VECTOR_MODE (mode)
16658 && (rclass != FLOAT_REGS
16659 || (GET_MODE_SIZE (mode) != 4 && GET_MODE_SIZE (mode) != 8)))
16660 || and_op2 != NULL_RTX
16661 || !legitimate_indexed_address_p (XEXP (addr, 1), false)))
16663 scratch_or_premodify = find_replacement (&XEXP (addr, 0));
16664 if (!legitimate_indirect_address_p (scratch_or_premodify, false))
16665 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
16667 addr = find_replacement (&XEXP (addr, 1));
16668 if (GET_CODE (addr) != PLUS)
16669 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
16672 if (legitimate_indirect_address_p (addr, false) /* reg */
16673 || legitimate_indexed_address_p (addr, false) /* reg+reg */
16674 || (GET_CODE (addr) == AND /* Altivec memory */
16675 && rclass == ALTIVEC_REGS
16676 && GET_CODE (XEXP (addr, 1)) == CONST_INT
16677 && INTVAL (XEXP (addr, 1)) == -16
16678 && (legitimate_indirect_address_p (XEXP (addr, 0), false)
16679 || legitimate_indexed_address_p (XEXP (addr, 0), false))))
16682 else if (GET_CODE (addr) == PLUS)
16684 addr_op1 = XEXP (addr, 0);
16685 addr_op2 = XEXP (addr, 1);
16686 if (!REG_P (addr_op1))
16687 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
16689 if (TARGET_DEBUG_ADDR)
16691 fprintf (stderr, "\nMove plus addr to register %s, mode = %s: ",
16692 rs6000_reg_names[REGNO (scratch)], GET_MODE_NAME (mode));
16693 debug_rtx (addr_op2);
16695 rs6000_emit_move (scratch, addr_op2, Pmode);
16696 emit_insn (gen_rtx_SET (VOIDmode,
16697 scratch_or_premodify,
16698 gen_rtx_PLUS (Pmode,
16699 addr_op1,
16700 scratch)));
16701 addr = scratch_or_premodify;
16702 scratch_or_premodify = scratch;
16705 else if (GET_CODE (addr) == SYMBOL_REF || GET_CODE (addr) == CONST
16706 || GET_CODE (addr) == CONST_INT || GET_CODE (addr) == LO_SUM
16707 || REG_P (addr))
16709 if (TARGET_DEBUG_ADDR)
16711 fprintf (stderr, "\nMove addr to register %s, mode = %s: ",
16712 rs6000_reg_names[REGNO (scratch_or_premodify)],
16713 GET_MODE_NAME (mode));
16714 debug_rtx (addr);
16717 rs6000_emit_move (scratch_or_premodify, addr, Pmode);
16718 addr = scratch_or_premodify;
16719 scratch_or_premodify = scratch;
16722 else
16723 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
16725 break;
16727 default:
16728 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
16731 /* If the original address involved a pre-modify that we couldn't use the VSX
16732 memory instruction with update, and we haven't taken care of already,
16733 store the address in the pre-modify register and use that as the
16734 address. */
16735 if (scratch_or_premodify != scratch && scratch_or_premodify != addr)
16737 emit_insn (gen_rtx_SET (VOIDmode, scratch_or_premodify, addr));
16738 addr = scratch_or_premodify;
16741 /* If the original address involved an AND -16 and we couldn't use an ALTIVEC
16742 memory instruction, recreate the AND now, including the clobber which is
16743 generated by the general ANDSI3/ANDDI3 patterns for the
16744 andi. instruction. */
16745 if (and_op2 != NULL_RTX)
16747 if (! legitimate_indirect_address_p (addr, false))
16749 emit_insn (gen_rtx_SET (VOIDmode, scratch, addr));
16750 addr = scratch;
16753 if (TARGET_DEBUG_ADDR)
16755 fprintf (stderr, "\nAnd addr to register %s, mode = %s: ",
16756 rs6000_reg_names[REGNO (scratch)], GET_MODE_NAME (mode));
16757 debug_rtx (and_op2);
16760 and_rtx = gen_rtx_SET (VOIDmode,
16761 scratch,
16762 gen_rtx_AND (Pmode,
16763 addr,
16764 and_op2));
16766 cc_clobber = gen_rtx_CLOBBER (CCmode, gen_rtx_SCRATCH (CCmode));
16767 emit_insn (gen_rtx_PARALLEL (VOIDmode,
16768 gen_rtvec (2, and_rtx, cc_clobber)));
16769 addr = scratch;
16772 /* Adjust the address if it changed. */
16773 if (addr != XEXP (mem, 0))
16775 mem = replace_equiv_address_nv (mem, addr);
16776 if (TARGET_DEBUG_ADDR)
16777 fprintf (stderr, "\nrs6000_secondary_reload_inner, mem adjusted.\n");
16780 /* Now create the move. */
16781 if (store_p)
16782 emit_insn (gen_rtx_SET (VOIDmode, mem, reg));
16783 else
16784 emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
16786 return;
16789 /* Convert reloads involving 64-bit gprs and misaligned offset
16790 addressing, or multiple 32-bit gprs and offsets that are too large,
16791 to use indirect addressing. */
16793 void
16794 rs6000_secondary_reload_gpr (rtx reg, rtx mem, rtx scratch, bool store_p)
16796 int regno = true_regnum (reg);
16797 enum reg_class rclass;
16798 rtx addr;
16799 rtx scratch_or_premodify = scratch;
16801 if (TARGET_DEBUG_ADDR)
16803 fprintf (stderr, "\nrs6000_secondary_reload_gpr, type = %s\n",
16804 store_p ? "store" : "load");
16805 fprintf (stderr, "reg:\n");
16806 debug_rtx (reg);
16807 fprintf (stderr, "mem:\n");
16808 debug_rtx (mem);
16809 fprintf (stderr, "scratch:\n");
16810 debug_rtx (scratch);
16813 gcc_assert (regno >= 0 && regno < FIRST_PSEUDO_REGISTER);
16814 gcc_assert (GET_CODE (mem) == MEM);
16815 rclass = REGNO_REG_CLASS (regno);
16816 gcc_assert (rclass == GENERAL_REGS || rclass == BASE_REGS);
16817 addr = XEXP (mem, 0);
16819 if (GET_CODE (addr) == PRE_MODIFY)
16821 scratch_or_premodify = XEXP (addr, 0);
16822 gcc_assert (REG_P (scratch_or_premodify));
16823 addr = XEXP (addr, 1);
16825 gcc_assert (GET_CODE (addr) == PLUS || GET_CODE (addr) == LO_SUM);
16827 rs6000_emit_move (scratch_or_premodify, addr, Pmode);
16829 mem = replace_equiv_address_nv (mem, scratch_or_premodify);
16831 /* Now create the move. */
16832 if (store_p)
16833 emit_insn (gen_rtx_SET (VOIDmode, mem, reg));
16834 else
16835 emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
16837 return;
16840 /* Allocate a 64-bit stack slot to be used for copying SDmode values through if
16841 this function has any SDmode references. If we are on a power7 or later, we
16842 don't need the 64-bit stack slot since the LFIWZX and STIFWX instructions
16843 can load/store the value. */
16845 static void
16846 rs6000_alloc_sdmode_stack_slot (void)
16848 tree t;
16849 basic_block bb;
16850 gimple_stmt_iterator gsi;
16852 gcc_assert (cfun->machine->sdmode_stack_slot == NULL_RTX);
16853 /* We use a different approach for dealing with the secondary
16854 memory in LRA. */
16855 if (ira_use_lra_p)
16856 return;
16858 if (TARGET_NO_SDMODE_STACK)
16859 return;
16861 FOR_EACH_BB_FN (bb, cfun)
16862 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
16864 tree ret = walk_gimple_op (gsi_stmt (gsi), rs6000_check_sdmode, NULL);
16865 if (ret)
16867 rtx stack = assign_stack_local (DDmode, GET_MODE_SIZE (DDmode), 0);
16868 cfun->machine->sdmode_stack_slot = adjust_address_nv (stack,
16869 SDmode, 0);
16870 return;
16874 /* Check for any SDmode parameters of the function. */
16875 for (t = DECL_ARGUMENTS (cfun->decl); t; t = DECL_CHAIN (t))
16877 if (TREE_TYPE (t) == error_mark_node)
16878 continue;
16880 if (TYPE_MODE (TREE_TYPE (t)) == SDmode
16881 || TYPE_MODE (DECL_ARG_TYPE (t)) == SDmode)
16883 rtx stack = assign_stack_local (DDmode, GET_MODE_SIZE (DDmode), 0);
16884 cfun->machine->sdmode_stack_slot = adjust_address_nv (stack,
16885 SDmode, 0);
16886 return;
16891 static void
16892 rs6000_instantiate_decls (void)
16894 if (cfun->machine->sdmode_stack_slot != NULL_RTX)
16895 instantiate_decl_rtl (cfun->machine->sdmode_stack_slot);
16898 /* Given an rtx X being reloaded into a reg required to be
16899 in class CLASS, return the class of reg to actually use.
16900 In general this is just CLASS; but on some machines
16901 in some cases it is preferable to use a more restrictive class.
16903 On the RS/6000, we have to return NO_REGS when we want to reload a
16904 floating-point CONST_DOUBLE to force it to be copied to memory.
16906 We also don't want to reload integer values into floating-point
16907 registers if we can at all help it. In fact, this can
16908 cause reload to die, if it tries to generate a reload of CTR
16909 into a FP register and discovers it doesn't have the memory location
16910 required.
16912 ??? Would it be a good idea to have reload do the converse, that is
16913 try to reload floating modes into FP registers if possible?
16916 static enum reg_class
16917 rs6000_preferred_reload_class (rtx x, enum reg_class rclass)
16919 enum machine_mode mode = GET_MODE (x);
16921 if (TARGET_VSX && x == CONST0_RTX (mode) && VSX_REG_CLASS_P (rclass))
16922 return rclass;
16924 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (mode)
16925 && (rclass == ALTIVEC_REGS || rclass == VSX_REGS)
16926 && easy_vector_constant (x, mode))
16927 return ALTIVEC_REGS;
16929 if ((CONSTANT_P (x) || GET_CODE (x) == PLUS))
16931 if (reg_class_subset_p (GENERAL_REGS, rclass))
16932 return GENERAL_REGS;
16933 if (reg_class_subset_p (BASE_REGS, rclass))
16934 return BASE_REGS;
16935 return NO_REGS;
16938 if (GET_MODE_CLASS (mode) == MODE_INT && rclass == NON_SPECIAL_REGS)
16939 return GENERAL_REGS;
16941 /* For VSX, prefer the traditional registers for 64-bit values because we can
16942 use the non-VSX loads. Prefer the Altivec registers if Altivec is
16943 handling the vector operations (i.e. V16QI, V8HI, and V4SI), or if we
16944 prefer Altivec loads.. */
16945 if (rclass == VSX_REGS)
16947 if (GET_MODE_SIZE (mode) <= 8)
16948 return FLOAT_REGS;
16950 if (VECTOR_UNIT_ALTIVEC_P (mode) || VECTOR_MEM_ALTIVEC_P (mode)
16951 || mode == V1TImode)
16952 return ALTIVEC_REGS;
16954 return rclass;
16957 return rclass;
16960 /* Debug version of rs6000_preferred_reload_class. */
16961 static enum reg_class
16962 rs6000_debug_preferred_reload_class (rtx x, enum reg_class rclass)
16964 enum reg_class ret = rs6000_preferred_reload_class (x, rclass);
16966 fprintf (stderr,
16967 "\nrs6000_preferred_reload_class, return %s, rclass = %s, "
16968 "mode = %s, x:\n",
16969 reg_class_names[ret], reg_class_names[rclass],
16970 GET_MODE_NAME (GET_MODE (x)));
16971 debug_rtx (x);
16973 return ret;
16976 /* If we are copying between FP or AltiVec registers and anything else, we need
16977 a memory location. The exception is when we are targeting ppc64 and the
16978 move to/from fpr to gpr instructions are available. Also, under VSX, you
16979 can copy vector registers from the FP register set to the Altivec register
16980 set and vice versa. */
16982 static bool
16983 rs6000_secondary_memory_needed (enum reg_class from_class,
16984 enum reg_class to_class,
16985 enum machine_mode mode)
16987 enum rs6000_reg_type from_type, to_type;
16988 bool altivec_p = ((from_class == ALTIVEC_REGS)
16989 || (to_class == ALTIVEC_REGS));
16991 /* If a simple/direct move is available, we don't need secondary memory */
16992 from_type = reg_class_to_reg_type[(int)from_class];
16993 to_type = reg_class_to_reg_type[(int)to_class];
16995 if (rs6000_secondary_reload_move (to_type, from_type, mode,
16996 (secondary_reload_info *)0, altivec_p))
16997 return false;
16999 /* If we have a floating point or vector register class, we need to use
17000 memory to transfer the data. */
17001 if (IS_FP_VECT_REG_TYPE (from_type) || IS_FP_VECT_REG_TYPE (to_type))
17002 return true;
17004 return false;
17007 /* Debug version of rs6000_secondary_memory_needed. */
17008 static bool
17009 rs6000_debug_secondary_memory_needed (enum reg_class from_class,
17010 enum reg_class to_class,
17011 enum machine_mode mode)
17013 bool ret = rs6000_secondary_memory_needed (from_class, to_class, mode);
17015 fprintf (stderr,
17016 "rs6000_secondary_memory_needed, return: %s, from_class = %s, "
17017 "to_class = %s, mode = %s\n",
17018 ret ? "true" : "false",
17019 reg_class_names[from_class],
17020 reg_class_names[to_class],
17021 GET_MODE_NAME (mode));
17023 return ret;
17026 /* Return the register class of a scratch register needed to copy IN into
17027 or out of a register in RCLASS in MODE. If it can be done directly,
17028 NO_REGS is returned. */
17030 static enum reg_class
17031 rs6000_secondary_reload_class (enum reg_class rclass, enum machine_mode mode,
17032 rtx in)
17034 int regno;
17036 if (TARGET_ELF || (DEFAULT_ABI == ABI_DARWIN
17037 #if TARGET_MACHO
17038 && MACHOPIC_INDIRECT
17039 #endif
17042 /* We cannot copy a symbolic operand directly into anything
17043 other than BASE_REGS for TARGET_ELF. So indicate that a
17044 register from BASE_REGS is needed as an intermediate
17045 register.
17047 On Darwin, pic addresses require a load from memory, which
17048 needs a base register. */
17049 if (rclass != BASE_REGS
17050 && (GET_CODE (in) == SYMBOL_REF
17051 || GET_CODE (in) == HIGH
17052 || GET_CODE (in) == LABEL_REF
17053 || GET_CODE (in) == CONST))
17054 return BASE_REGS;
17057 if (GET_CODE (in) == REG)
17059 regno = REGNO (in);
17060 if (regno >= FIRST_PSEUDO_REGISTER)
17062 regno = true_regnum (in);
17063 if (regno >= FIRST_PSEUDO_REGISTER)
17064 regno = -1;
17067 else if (GET_CODE (in) == SUBREG)
17069 regno = true_regnum (in);
17070 if (regno >= FIRST_PSEUDO_REGISTER)
17071 regno = -1;
17073 else
17074 regno = -1;
17076 /* We can place anything into GENERAL_REGS and can put GENERAL_REGS
17077 into anything. */
17078 if (rclass == GENERAL_REGS || rclass == BASE_REGS
17079 || (regno >= 0 && INT_REGNO_P (regno)))
17080 return NO_REGS;
17082 /* Constants, memory, and FP registers can go into FP registers. */
17083 if ((regno == -1 || FP_REGNO_P (regno))
17084 && (rclass == FLOAT_REGS || rclass == NON_SPECIAL_REGS))
17085 return (mode != SDmode || lra_in_progress) ? NO_REGS : GENERAL_REGS;
17087 /* Memory, and FP/altivec registers can go into fp/altivec registers under
17088 VSX. However, for scalar variables, use the traditional floating point
17089 registers so that we can use offset+register addressing. */
17090 if (TARGET_VSX
17091 && (regno == -1 || VSX_REGNO_P (regno))
17092 && VSX_REG_CLASS_P (rclass))
17094 if (GET_MODE_SIZE (mode) < 16)
17095 return FLOAT_REGS;
17097 return NO_REGS;
17100 /* Memory, and AltiVec registers can go into AltiVec registers. */
17101 if ((regno == -1 || ALTIVEC_REGNO_P (regno))
17102 && rclass == ALTIVEC_REGS)
17103 return NO_REGS;
17105 /* We can copy among the CR registers. */
17106 if ((rclass == CR_REGS || rclass == CR0_REGS)
17107 && regno >= 0 && CR_REGNO_P (regno))
17108 return NO_REGS;
17110 /* Otherwise, we need GENERAL_REGS. */
17111 return GENERAL_REGS;
17114 /* Debug version of rs6000_secondary_reload_class. */
17115 static enum reg_class
17116 rs6000_debug_secondary_reload_class (enum reg_class rclass,
17117 enum machine_mode mode, rtx in)
17119 enum reg_class ret = rs6000_secondary_reload_class (rclass, mode, in);
17120 fprintf (stderr,
17121 "\nrs6000_secondary_reload_class, return %s, rclass = %s, "
17122 "mode = %s, input rtx:\n",
17123 reg_class_names[ret], reg_class_names[rclass],
17124 GET_MODE_NAME (mode));
17125 debug_rtx (in);
17127 return ret;
17130 /* Return nonzero if for CLASS a mode change from FROM to TO is invalid. */
17132 static bool
17133 rs6000_cannot_change_mode_class (enum machine_mode from,
17134 enum machine_mode to,
17135 enum reg_class rclass)
17137 unsigned from_size = GET_MODE_SIZE (from);
17138 unsigned to_size = GET_MODE_SIZE (to);
17140 if (from_size != to_size)
17142 enum reg_class xclass = (TARGET_VSX) ? VSX_REGS : FLOAT_REGS;
17144 if (reg_classes_intersect_p (xclass, rclass))
17146 unsigned to_nregs = hard_regno_nregs[FIRST_FPR_REGNO][to];
17147 unsigned from_nregs = hard_regno_nregs[FIRST_FPR_REGNO][from];
17149 /* Don't allow 64-bit types to overlap with 128-bit types that take a
17150 single register under VSX because the scalar part of the register
17151 is in the upper 64-bits, and not the lower 64-bits. Types like
17152 TFmode/TDmode that take 2 scalar register can overlap. 128-bit
17153 IEEE floating point can't overlap, and neither can small
17154 values. */
17156 if (TARGET_IEEEQUAD && (to == TFmode || from == TFmode))
17157 return true;
17159 /* TDmode in floating-mode registers must always go into a register
17160 pair with the most significant word in the even-numbered register
17161 to match ISA requirements. In little-endian mode, this does not
17162 match subreg numbering, so we cannot allow subregs. */
17163 if (!BYTES_BIG_ENDIAN && (to == TDmode || from == TDmode))
17164 return true;
17166 if (from_size < 8 || to_size < 8)
17167 return true;
17169 if (from_size == 8 && (8 * to_nregs) != to_size)
17170 return true;
17172 if (to_size == 8 && (8 * from_nregs) != from_size)
17173 return true;
17175 return false;
17177 else
17178 return false;
17181 if (TARGET_E500_DOUBLE
17182 && ((((to) == DFmode) + ((from) == DFmode)) == 1
17183 || (((to) == TFmode) + ((from) == TFmode)) == 1
17184 || (((to) == DDmode) + ((from) == DDmode)) == 1
17185 || (((to) == TDmode) + ((from) == TDmode)) == 1
17186 || (((to) == DImode) + ((from) == DImode)) == 1))
17187 return true;
17189 /* Since the VSX register set includes traditional floating point registers
17190 and altivec registers, just check for the size being different instead of
17191 trying to check whether the modes are vector modes. Otherwise it won't
17192 allow say DF and DI to change classes. For types like TFmode and TDmode
17193 that take 2 64-bit registers, rather than a single 128-bit register, don't
17194 allow subregs of those types to other 128 bit types. */
17195 if (TARGET_VSX && VSX_REG_CLASS_P (rclass))
17197 unsigned num_regs = (from_size + 15) / 16;
17198 if (hard_regno_nregs[FIRST_FPR_REGNO][to] > num_regs
17199 || hard_regno_nregs[FIRST_FPR_REGNO][from] > num_regs)
17200 return true;
17202 return (from_size != 8 && from_size != 16);
17205 if (TARGET_ALTIVEC && rclass == ALTIVEC_REGS
17206 && (ALTIVEC_VECTOR_MODE (from) + ALTIVEC_VECTOR_MODE (to)) == 1)
17207 return true;
17209 if (TARGET_SPE && (SPE_VECTOR_MODE (from) + SPE_VECTOR_MODE (to)) == 1
17210 && reg_classes_intersect_p (GENERAL_REGS, rclass))
17211 return true;
17213 return false;
17216 /* Debug version of rs6000_cannot_change_mode_class. */
17217 static bool
17218 rs6000_debug_cannot_change_mode_class (enum machine_mode from,
17219 enum machine_mode to,
17220 enum reg_class rclass)
17222 bool ret = rs6000_cannot_change_mode_class (from, to, rclass);
17224 fprintf (stderr,
17225 "rs6000_cannot_change_mode_class, return %s, from = %s, "
17226 "to = %s, rclass = %s\n",
17227 ret ? "true" : "false",
17228 GET_MODE_NAME (from), GET_MODE_NAME (to),
17229 reg_class_names[rclass]);
17231 return ret;
17234 /* Return a string to do a move operation of 128 bits of data. */
17236 const char *
17237 rs6000_output_move_128bit (rtx operands[])
17239 rtx dest = operands[0];
17240 rtx src = operands[1];
17241 enum machine_mode mode = GET_MODE (dest);
17242 int dest_regno;
17243 int src_regno;
17244 bool dest_gpr_p, dest_fp_p, dest_vmx_p, dest_vsx_p;
17245 bool src_gpr_p, src_fp_p, src_vmx_p, src_vsx_p;
17247 if (REG_P (dest))
17249 dest_regno = REGNO (dest);
17250 dest_gpr_p = INT_REGNO_P (dest_regno);
17251 dest_fp_p = FP_REGNO_P (dest_regno);
17252 dest_vmx_p = ALTIVEC_REGNO_P (dest_regno);
17253 dest_vsx_p = dest_fp_p | dest_vmx_p;
17255 else
17257 dest_regno = -1;
17258 dest_gpr_p = dest_fp_p = dest_vmx_p = dest_vsx_p = false;
17261 if (REG_P (src))
17263 src_regno = REGNO (src);
17264 src_gpr_p = INT_REGNO_P (src_regno);
17265 src_fp_p = FP_REGNO_P (src_regno);
17266 src_vmx_p = ALTIVEC_REGNO_P (src_regno);
17267 src_vsx_p = src_fp_p | src_vmx_p;
17269 else
17271 src_regno = -1;
17272 src_gpr_p = src_fp_p = src_vmx_p = src_vsx_p = false;
17275 /* Register moves. */
17276 if (dest_regno >= 0 && src_regno >= 0)
17278 if (dest_gpr_p)
17280 if (src_gpr_p)
17281 return "#";
17283 else if (TARGET_VSX && TARGET_DIRECT_MOVE && src_vsx_p)
17284 return "#";
17287 else if (TARGET_VSX && dest_vsx_p)
17289 if (src_vsx_p)
17290 return "xxlor %x0,%x1,%x1";
17292 else if (TARGET_DIRECT_MOVE && src_gpr_p)
17293 return "#";
17296 else if (TARGET_ALTIVEC && dest_vmx_p && src_vmx_p)
17297 return "vor %0,%1,%1";
17299 else if (dest_fp_p && src_fp_p)
17300 return "#";
17303 /* Loads. */
17304 else if (dest_regno >= 0 && MEM_P (src))
17306 if (dest_gpr_p)
17308 if (TARGET_QUAD_MEMORY && quad_load_store_p (dest, src))
17309 return "lq %0,%1";
17310 else
17311 return "#";
17314 else if (TARGET_ALTIVEC && dest_vmx_p
17315 && altivec_indexed_or_indirect_operand (src, mode))
17316 return "lvx %0,%y1";
17318 else if (TARGET_VSX && dest_vsx_p)
17320 if (mode == V16QImode || mode == V8HImode || mode == V4SImode)
17321 return "lxvw4x %x0,%y1";
17322 else
17323 return "lxvd2x %x0,%y1";
17326 else if (TARGET_ALTIVEC && dest_vmx_p)
17327 return "lvx %0,%y1";
17329 else if (dest_fp_p)
17330 return "#";
17333 /* Stores. */
17334 else if (src_regno >= 0 && MEM_P (dest))
17336 if (src_gpr_p)
17338 if (TARGET_QUAD_MEMORY && quad_load_store_p (dest, src))
17339 return "stq %1,%0";
17340 else
17341 return "#";
17344 else if (TARGET_ALTIVEC && src_vmx_p
17345 && altivec_indexed_or_indirect_operand (src, mode))
17346 return "stvx %1,%y0";
17348 else if (TARGET_VSX && src_vsx_p)
17350 if (mode == V16QImode || mode == V8HImode || mode == V4SImode)
17351 return "stxvw4x %x1,%y0";
17352 else
17353 return "stxvd2x %x1,%y0";
17356 else if (TARGET_ALTIVEC && src_vmx_p)
17357 return "stvx %1,%y0";
17359 else if (src_fp_p)
17360 return "#";
17363 /* Constants. */
17364 else if (dest_regno >= 0
17365 && (GET_CODE (src) == CONST_INT
17366 || GET_CODE (src) == CONST_DOUBLE
17367 || GET_CODE (src) == CONST_VECTOR))
17369 if (dest_gpr_p)
17370 return "#";
17372 else if (TARGET_VSX && dest_vsx_p && zero_constant (src, mode))
17373 return "xxlxor %x0,%x0,%x0";
17375 else if (TARGET_ALTIVEC && dest_vmx_p)
17376 return output_vec_const_move (operands);
17379 if (TARGET_DEBUG_ADDR)
17381 fprintf (stderr, "\n===== Bad 128 bit move:\n");
17382 debug_rtx (gen_rtx_SET (VOIDmode, dest, src));
17385 gcc_unreachable ();
17388 /* Validate a 128-bit move. */
17389 bool
17390 rs6000_move_128bit_ok_p (rtx operands[])
17392 enum machine_mode mode = GET_MODE (operands[0]);
17393 return (gpc_reg_operand (operands[0], mode)
17394 || gpc_reg_operand (operands[1], mode));
17397 /* Return true if a 128-bit move needs to be split. */
17398 bool
17399 rs6000_split_128bit_ok_p (rtx operands[])
17401 if (!reload_completed)
17402 return false;
17404 if (!gpr_or_gpr_p (operands[0], operands[1]))
17405 return false;
17407 if (quad_load_store_p (operands[0], operands[1]))
17408 return false;
17410 return true;
17414 /* Given a comparison operation, return the bit number in CCR to test. We
17415 know this is a valid comparison.
17417 SCC_P is 1 if this is for an scc. That means that %D will have been
17418 used instead of %C, so the bits will be in different places.
17420 Return -1 if OP isn't a valid comparison for some reason. */
17423 ccr_bit (rtx op, int scc_p)
17425 enum rtx_code code = GET_CODE (op);
17426 enum machine_mode cc_mode;
17427 int cc_regnum;
17428 int base_bit;
17429 rtx reg;
17431 if (!COMPARISON_P (op))
17432 return -1;
17434 reg = XEXP (op, 0);
17436 gcc_assert (GET_CODE (reg) == REG && CR_REGNO_P (REGNO (reg)));
17438 cc_mode = GET_MODE (reg);
17439 cc_regnum = REGNO (reg);
17440 base_bit = 4 * (cc_regnum - CR0_REGNO);
17442 validate_condition_mode (code, cc_mode);
17444 /* When generating a sCOND operation, only positive conditions are
17445 allowed. */
17446 gcc_assert (!scc_p
17447 || code == EQ || code == GT || code == LT || code == UNORDERED
17448 || code == GTU || code == LTU);
17450 switch (code)
17452 case NE:
17453 return scc_p ? base_bit + 3 : base_bit + 2;
17454 case EQ:
17455 return base_bit + 2;
17456 case GT: case GTU: case UNLE:
17457 return base_bit + 1;
17458 case LT: case LTU: case UNGE:
17459 return base_bit;
17460 case ORDERED: case UNORDERED:
17461 return base_bit + 3;
17463 case GE: case GEU:
17464 /* If scc, we will have done a cror to put the bit in the
17465 unordered position. So test that bit. For integer, this is ! LT
17466 unless this is an scc insn. */
17467 return scc_p ? base_bit + 3 : base_bit;
17469 case LE: case LEU:
17470 return scc_p ? base_bit + 3 : base_bit + 1;
17472 default:
17473 gcc_unreachable ();
17477 /* Return the GOT register. */
17480 rs6000_got_register (rtx value ATTRIBUTE_UNUSED)
17482 /* The second flow pass currently (June 1999) can't update
17483 regs_ever_live without disturbing other parts of the compiler, so
17484 update it here to make the prolog/epilogue code happy. */
17485 if (!can_create_pseudo_p ()
17486 && !df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))
17487 df_set_regs_ever_live (RS6000_PIC_OFFSET_TABLE_REGNUM, true);
17489 crtl->uses_pic_offset_table = 1;
17491 return pic_offset_table_rtx;
17494 static rs6000_stack_t stack_info;
17496 /* Function to init struct machine_function.
17497 This will be called, via a pointer variable,
17498 from push_function_context. */
17500 static struct machine_function *
17501 rs6000_init_machine_status (void)
17503 stack_info.reload_completed = 0;
17504 return ggc_alloc_cleared_machine_function ();
17507 #define INT_P(X) (GET_CODE (X) == CONST_INT && GET_MODE (X) == VOIDmode)
17510 extract_MB (rtx op)
17512 int i;
17513 unsigned long val = INTVAL (op);
17515 /* If the high bit is zero, the value is the first 1 bit we find
17516 from the left. */
17517 if ((val & 0x80000000) == 0)
17519 gcc_assert (val & 0xffffffff);
17521 i = 1;
17522 while (((val <<= 1) & 0x80000000) == 0)
17523 ++i;
17524 return i;
17527 /* If the high bit is set and the low bit is not, or the mask is all
17528 1's, the value is zero. */
17529 if ((val & 1) == 0 || (val & 0xffffffff) == 0xffffffff)
17530 return 0;
17532 /* Otherwise we have a wrap-around mask. Look for the first 0 bit
17533 from the right. */
17534 i = 31;
17535 while (((val >>= 1) & 1) != 0)
17536 --i;
17538 return i;
17542 extract_ME (rtx op)
17544 int i;
17545 unsigned long val = INTVAL (op);
17547 /* If the low bit is zero, the value is the first 1 bit we find from
17548 the right. */
17549 if ((val & 1) == 0)
17551 gcc_assert (val & 0xffffffff);
17553 i = 30;
17554 while (((val >>= 1) & 1) == 0)
17555 --i;
17557 return i;
17560 /* If the low bit is set and the high bit is not, or the mask is all
17561 1's, the value is 31. */
17562 if ((val & 0x80000000) == 0 || (val & 0xffffffff) == 0xffffffff)
17563 return 31;
17565 /* Otherwise we have a wrap-around mask. Look for the first 0 bit
17566 from the left. */
17567 i = 0;
17568 while (((val <<= 1) & 0x80000000) != 0)
17569 ++i;
17571 return i;
17574 /* Locate some local-dynamic symbol still in use by this function
17575 so that we can print its name in some tls_ld pattern. */
17577 static const char *
17578 rs6000_get_some_local_dynamic_name (void)
17580 rtx insn;
17582 if (cfun->machine->some_ld_name)
17583 return cfun->machine->some_ld_name;
17585 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
17586 if (INSN_P (insn)
17587 && for_each_rtx (&PATTERN (insn),
17588 rs6000_get_some_local_dynamic_name_1, 0))
17589 return cfun->machine->some_ld_name;
17591 gcc_unreachable ();
17594 /* Helper function for rs6000_get_some_local_dynamic_name. */
17596 static int
17597 rs6000_get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
17599 rtx x = *px;
17601 if (GET_CODE (x) == SYMBOL_REF)
17603 const char *str = XSTR (x, 0);
17604 if (SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC)
17606 cfun->machine->some_ld_name = str;
17607 return 1;
17611 return 0;
17614 /* Write out a function code label. */
17616 void
17617 rs6000_output_function_entry (FILE *file, const char *fname)
17619 if (fname[0] != '.')
17621 switch (DEFAULT_ABI)
17623 default:
17624 gcc_unreachable ();
17626 case ABI_AIX:
17627 if (DOT_SYMBOLS)
17628 putc ('.', file);
17629 else
17630 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "L.");
17631 break;
17633 case ABI_ELFv2:
17634 case ABI_V4:
17635 case ABI_DARWIN:
17636 break;
17640 RS6000_OUTPUT_BASENAME (file, fname);
17643 /* Print an operand. Recognize special options, documented below. */
17645 #if TARGET_ELF
17646 #define SMALL_DATA_RELOC ((rs6000_sdata == SDATA_EABI) ? "sda21" : "sdarel")
17647 #define SMALL_DATA_REG ((rs6000_sdata == SDATA_EABI) ? 0 : 13)
17648 #else
17649 #define SMALL_DATA_RELOC "sda21"
17650 #define SMALL_DATA_REG 0
17651 #endif
17653 void
17654 print_operand (FILE *file, rtx x, int code)
17656 int i;
17657 unsigned HOST_WIDE_INT uval;
17659 switch (code)
17661 /* %a is output_address. */
17663 case 'b':
17664 /* If constant, low-order 16 bits of constant, unsigned.
17665 Otherwise, write normally. */
17666 if (INT_P (x))
17667 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0xffff);
17668 else
17669 print_operand (file, x, 0);
17670 return;
17672 case 'B':
17673 /* If the low-order bit is zero, write 'r'; otherwise, write 'l'
17674 for 64-bit mask direction. */
17675 putc (((INTVAL (x) & 1) == 0 ? 'r' : 'l'), file);
17676 return;
17678 /* %c is output_addr_const if a CONSTANT_ADDRESS_P, otherwise
17679 output_operand. */
17681 case 'D':
17682 /* Like 'J' but get to the GT bit only. */
17683 gcc_assert (REG_P (x));
17685 /* Bit 1 is GT bit. */
17686 i = 4 * (REGNO (x) - CR0_REGNO) + 1;
17688 /* Add one for shift count in rlinm for scc. */
17689 fprintf (file, "%d", i + 1);
17690 return;
17692 case 'E':
17693 /* X is a CR register. Print the number of the EQ bit of the CR */
17694 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
17695 output_operand_lossage ("invalid %%E value");
17696 else
17697 fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO) + 2);
17698 return;
17700 case 'f':
17701 /* X is a CR register. Print the shift count needed to move it
17702 to the high-order four bits. */
17703 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
17704 output_operand_lossage ("invalid %%f value");
17705 else
17706 fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO));
17707 return;
17709 case 'F':
17710 /* Similar, but print the count for the rotate in the opposite
17711 direction. */
17712 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
17713 output_operand_lossage ("invalid %%F value");
17714 else
17715 fprintf (file, "%d", 32 - 4 * (REGNO (x) - CR0_REGNO));
17716 return;
17718 case 'G':
17719 /* X is a constant integer. If it is negative, print "m",
17720 otherwise print "z". This is to make an aze or ame insn. */
17721 if (GET_CODE (x) != CONST_INT)
17722 output_operand_lossage ("invalid %%G value");
17723 else if (INTVAL (x) >= 0)
17724 putc ('z', file);
17725 else
17726 putc ('m', file);
17727 return;
17729 case 'h':
17730 /* If constant, output low-order five bits. Otherwise, write
17731 normally. */
17732 if (INT_P (x))
17733 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 31);
17734 else
17735 print_operand (file, x, 0);
17736 return;
17738 case 'H':
17739 /* If constant, output low-order six bits. Otherwise, write
17740 normally. */
17741 if (INT_P (x))
17742 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 63);
17743 else
17744 print_operand (file, x, 0);
17745 return;
17747 case 'I':
17748 /* Print `i' if this is a constant, else nothing. */
17749 if (INT_P (x))
17750 putc ('i', file);
17751 return;
17753 case 'j':
17754 /* Write the bit number in CCR for jump. */
17755 i = ccr_bit (x, 0);
17756 if (i == -1)
17757 output_operand_lossage ("invalid %%j code");
17758 else
17759 fprintf (file, "%d", i);
17760 return;
17762 case 'J':
17763 /* Similar, but add one for shift count in rlinm for scc and pass
17764 scc flag to `ccr_bit'. */
17765 i = ccr_bit (x, 1);
17766 if (i == -1)
17767 output_operand_lossage ("invalid %%J code");
17768 else
17769 /* If we want bit 31, write a shift count of zero, not 32. */
17770 fprintf (file, "%d", i == 31 ? 0 : i + 1);
17771 return;
17773 case 'k':
17774 /* X must be a constant. Write the 1's complement of the
17775 constant. */
17776 if (! INT_P (x))
17777 output_operand_lossage ("invalid %%k value");
17778 else
17779 fprintf (file, HOST_WIDE_INT_PRINT_DEC, ~ INTVAL (x));
17780 return;
17782 case 'K':
17783 /* X must be a symbolic constant on ELF. Write an
17784 expression suitable for an 'addi' that adds in the low 16
17785 bits of the MEM. */
17786 if (GET_CODE (x) == CONST)
17788 if (GET_CODE (XEXP (x, 0)) != PLUS
17789 || (GET_CODE (XEXP (XEXP (x, 0), 0)) != SYMBOL_REF
17790 && GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
17791 || GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
17792 output_operand_lossage ("invalid %%K value");
17794 print_operand_address (file, x);
17795 fputs ("@l", file);
17796 return;
17798 /* %l is output_asm_label. */
17800 case 'L':
17801 /* Write second word of DImode or DFmode reference. Works on register
17802 or non-indexed memory only. */
17803 if (REG_P (x))
17804 fputs (reg_names[REGNO (x) + 1], file);
17805 else if (MEM_P (x))
17807 /* Handle possible auto-increment. Since it is pre-increment and
17808 we have already done it, we can just use an offset of word. */
17809 if (GET_CODE (XEXP (x, 0)) == PRE_INC
17810 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
17811 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0),
17812 UNITS_PER_WORD));
17813 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
17814 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0),
17815 UNITS_PER_WORD));
17816 else
17817 output_address (XEXP (adjust_address_nv (x, SImode,
17818 UNITS_PER_WORD),
17819 0));
17821 if (small_data_operand (x, GET_MODE (x)))
17822 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
17823 reg_names[SMALL_DATA_REG]);
17825 return;
17827 case 'm':
17828 /* MB value for a mask operand. */
17829 if (! mask_operand (x, SImode))
17830 output_operand_lossage ("invalid %%m value");
17832 fprintf (file, "%d", extract_MB (x));
17833 return;
17835 case 'M':
17836 /* ME value for a mask operand. */
17837 if (! mask_operand (x, SImode))
17838 output_operand_lossage ("invalid %%M value");
17840 fprintf (file, "%d", extract_ME (x));
17841 return;
17843 /* %n outputs the negative of its operand. */
17845 case 'N':
17846 /* Write the number of elements in the vector times 4. */
17847 if (GET_CODE (x) != PARALLEL)
17848 output_operand_lossage ("invalid %%N value");
17849 else
17850 fprintf (file, "%d", XVECLEN (x, 0) * 4);
17851 return;
17853 case 'O':
17854 /* Similar, but subtract 1 first. */
17855 if (GET_CODE (x) != PARALLEL)
17856 output_operand_lossage ("invalid %%O value");
17857 else
17858 fprintf (file, "%d", (XVECLEN (x, 0) - 1) * 4);
17859 return;
17861 case 'p':
17862 /* X is a CONST_INT that is a power of two. Output the logarithm. */
17863 if (! INT_P (x)
17864 || INTVAL (x) < 0
17865 || (i = exact_log2 (INTVAL (x))) < 0)
17866 output_operand_lossage ("invalid %%p value");
17867 else
17868 fprintf (file, "%d", i);
17869 return;
17871 case 'P':
17872 /* The operand must be an indirect memory reference. The result
17873 is the register name. */
17874 if (GET_CODE (x) != MEM || GET_CODE (XEXP (x, 0)) != REG
17875 || REGNO (XEXP (x, 0)) >= 32)
17876 output_operand_lossage ("invalid %%P value");
17877 else
17878 fputs (reg_names[REGNO (XEXP (x, 0))], file);
17879 return;
17881 case 'q':
17882 /* This outputs the logical code corresponding to a boolean
17883 expression. The expression may have one or both operands
17884 negated (if one, only the first one). For condition register
17885 logical operations, it will also treat the negated
17886 CR codes as NOTs, but not handle NOTs of them. */
17888 const char *const *t = 0;
17889 const char *s;
17890 enum rtx_code code = GET_CODE (x);
17891 static const char * const tbl[3][3] = {
17892 { "and", "andc", "nor" },
17893 { "or", "orc", "nand" },
17894 { "xor", "eqv", "xor" } };
17896 if (code == AND)
17897 t = tbl[0];
17898 else if (code == IOR)
17899 t = tbl[1];
17900 else if (code == XOR)
17901 t = tbl[2];
17902 else
17903 output_operand_lossage ("invalid %%q value");
17905 if (GET_CODE (XEXP (x, 0)) != NOT)
17906 s = t[0];
17907 else
17909 if (GET_CODE (XEXP (x, 1)) == NOT)
17910 s = t[2];
17911 else
17912 s = t[1];
17915 fputs (s, file);
17917 return;
17919 case 'Q':
17920 if (! TARGET_MFCRF)
17921 return;
17922 fputc (',', file);
17923 /* FALLTHRU */
17925 case 'R':
17926 /* X is a CR register. Print the mask for `mtcrf'. */
17927 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
17928 output_operand_lossage ("invalid %%R value");
17929 else
17930 fprintf (file, "%d", 128 >> (REGNO (x) - CR0_REGNO));
17931 return;
17933 case 's':
17934 /* Low 5 bits of 32 - value */
17935 if (! INT_P (x))
17936 output_operand_lossage ("invalid %%s value");
17937 else
17938 fprintf (file, HOST_WIDE_INT_PRINT_DEC, (32 - INTVAL (x)) & 31);
17939 return;
17941 case 'S':
17942 /* PowerPC64 mask position. All 0's is excluded.
17943 CONST_INT 32-bit mask is considered sign-extended so any
17944 transition must occur within the CONST_INT, not on the boundary. */
17945 if (! mask64_operand (x, DImode))
17946 output_operand_lossage ("invalid %%S value");
17948 uval = INTVAL (x);
17950 if (uval & 1) /* Clear Left */
17952 #if HOST_BITS_PER_WIDE_INT > 64
17953 uval &= ((unsigned HOST_WIDE_INT) 1 << 64) - 1;
17954 #endif
17955 i = 64;
17957 else /* Clear Right */
17959 uval = ~uval;
17960 #if HOST_BITS_PER_WIDE_INT > 64
17961 uval &= ((unsigned HOST_WIDE_INT) 1 << 64) - 1;
17962 #endif
17963 i = 63;
17965 while (uval != 0)
17966 --i, uval >>= 1;
17967 gcc_assert (i >= 0);
17968 fprintf (file, "%d", i);
17969 return;
17971 case 't':
17972 /* Like 'J' but get to the OVERFLOW/UNORDERED bit. */
17973 gcc_assert (REG_P (x) && GET_MODE (x) == CCmode);
17975 /* Bit 3 is OV bit. */
17976 i = 4 * (REGNO (x) - CR0_REGNO) + 3;
17978 /* If we want bit 31, write a shift count of zero, not 32. */
17979 fprintf (file, "%d", i == 31 ? 0 : i + 1);
17980 return;
17982 case 'T':
17983 /* Print the symbolic name of a branch target register. */
17984 if (GET_CODE (x) != REG || (REGNO (x) != LR_REGNO
17985 && REGNO (x) != CTR_REGNO))
17986 output_operand_lossage ("invalid %%T value");
17987 else if (REGNO (x) == LR_REGNO)
17988 fputs ("lr", file);
17989 else
17990 fputs ("ctr", file);
17991 return;
17993 case 'u':
17994 /* High-order 16 bits of constant for use in unsigned operand. */
17995 if (! INT_P (x))
17996 output_operand_lossage ("invalid %%u value");
17997 else
17998 fprintf (file, HOST_WIDE_INT_PRINT_HEX,
17999 (INTVAL (x) >> 16) & 0xffff);
18000 return;
18002 case 'v':
18003 /* High-order 16 bits of constant for use in signed operand. */
18004 if (! INT_P (x))
18005 output_operand_lossage ("invalid %%v value");
18006 else
18007 fprintf (file, HOST_WIDE_INT_PRINT_HEX,
18008 (INTVAL (x) >> 16) & 0xffff);
18009 return;
18011 case 'U':
18012 /* Print `u' if this has an auto-increment or auto-decrement. */
18013 if (MEM_P (x)
18014 && (GET_CODE (XEXP (x, 0)) == PRE_INC
18015 || GET_CODE (XEXP (x, 0)) == PRE_DEC
18016 || GET_CODE (XEXP (x, 0)) == PRE_MODIFY))
18017 putc ('u', file);
18018 return;
18020 case 'V':
18021 /* Print the trap code for this operand. */
18022 switch (GET_CODE (x))
18024 case EQ:
18025 fputs ("eq", file); /* 4 */
18026 break;
18027 case NE:
18028 fputs ("ne", file); /* 24 */
18029 break;
18030 case LT:
18031 fputs ("lt", file); /* 16 */
18032 break;
18033 case LE:
18034 fputs ("le", file); /* 20 */
18035 break;
18036 case GT:
18037 fputs ("gt", file); /* 8 */
18038 break;
18039 case GE:
18040 fputs ("ge", file); /* 12 */
18041 break;
18042 case LTU:
18043 fputs ("llt", file); /* 2 */
18044 break;
18045 case LEU:
18046 fputs ("lle", file); /* 6 */
18047 break;
18048 case GTU:
18049 fputs ("lgt", file); /* 1 */
18050 break;
18051 case GEU:
18052 fputs ("lge", file); /* 5 */
18053 break;
18054 default:
18055 gcc_unreachable ();
18057 break;
18059 case 'w':
18060 /* If constant, low-order 16 bits of constant, signed. Otherwise, write
18061 normally. */
18062 if (INT_P (x))
18063 fprintf (file, HOST_WIDE_INT_PRINT_DEC,
18064 ((INTVAL (x) & 0xffff) ^ 0x8000) - 0x8000);
18065 else
18066 print_operand (file, x, 0);
18067 return;
18069 case 'W':
18070 /* MB value for a PowerPC64 rldic operand. */
18071 i = clz_hwi (INTVAL (x));
18073 fprintf (file, "%d", i);
18074 return;
18076 case 'x':
18077 /* X is a FPR or Altivec register used in a VSX context. */
18078 if (GET_CODE (x) != REG || !VSX_REGNO_P (REGNO (x)))
18079 output_operand_lossage ("invalid %%x value");
18080 else
18082 int reg = REGNO (x);
18083 int vsx_reg = (FP_REGNO_P (reg)
18084 ? reg - 32
18085 : reg - FIRST_ALTIVEC_REGNO + 32);
18087 #ifdef TARGET_REGNAMES
18088 if (TARGET_REGNAMES)
18089 fprintf (file, "%%vs%d", vsx_reg);
18090 else
18091 #endif
18092 fprintf (file, "%d", vsx_reg);
18094 return;
18096 case 'X':
18097 if (MEM_P (x)
18098 && (legitimate_indexed_address_p (XEXP (x, 0), 0)
18099 || (GET_CODE (XEXP (x, 0)) == PRE_MODIFY
18100 && legitimate_indexed_address_p (XEXP (XEXP (x, 0), 1), 0))))
18101 putc ('x', file);
18102 return;
18104 case 'Y':
18105 /* Like 'L', for third word of TImode/PTImode */
18106 if (REG_P (x))
18107 fputs (reg_names[REGNO (x) + 2], file);
18108 else if (MEM_P (x))
18110 if (GET_CODE (XEXP (x, 0)) == PRE_INC
18111 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
18112 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 8));
18113 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
18114 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 8));
18115 else
18116 output_address (XEXP (adjust_address_nv (x, SImode, 8), 0));
18117 if (small_data_operand (x, GET_MODE (x)))
18118 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
18119 reg_names[SMALL_DATA_REG]);
18121 return;
18123 case 'z':
18124 /* X is a SYMBOL_REF. Write out the name preceded by a
18125 period and without any trailing data in brackets. Used for function
18126 names. If we are configured for System V (or the embedded ABI) on
18127 the PowerPC, do not emit the period, since those systems do not use
18128 TOCs and the like. */
18129 gcc_assert (GET_CODE (x) == SYMBOL_REF);
18131 /* For macho, check to see if we need a stub. */
18132 if (TARGET_MACHO)
18134 const char *name = XSTR (x, 0);
18135 #if TARGET_MACHO
18136 if (darwin_emit_branch_islands
18137 && MACHOPIC_INDIRECT
18138 && machopic_classify_symbol (x) == MACHOPIC_UNDEFINED_FUNCTION)
18139 name = machopic_indirection_name (x, /*stub_p=*/true);
18140 #endif
18141 assemble_name (file, name);
18143 else if (!DOT_SYMBOLS)
18144 assemble_name (file, XSTR (x, 0));
18145 else
18146 rs6000_output_function_entry (file, XSTR (x, 0));
18147 return;
18149 case 'Z':
18150 /* Like 'L', for last word of TImode/PTImode. */
18151 if (REG_P (x))
18152 fputs (reg_names[REGNO (x) + 3], file);
18153 else if (MEM_P (x))
18155 if (GET_CODE (XEXP (x, 0)) == PRE_INC
18156 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
18157 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 12));
18158 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
18159 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 12));
18160 else
18161 output_address (XEXP (adjust_address_nv (x, SImode, 12), 0));
18162 if (small_data_operand (x, GET_MODE (x)))
18163 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
18164 reg_names[SMALL_DATA_REG]);
18166 return;
18168 /* Print AltiVec or SPE memory operand. */
18169 case 'y':
18171 rtx tmp;
18173 gcc_assert (MEM_P (x));
18175 tmp = XEXP (x, 0);
18177 /* Ugly hack because %y is overloaded. */
18178 if ((TARGET_SPE || TARGET_E500_DOUBLE)
18179 && (GET_MODE_SIZE (GET_MODE (x)) == 8
18180 || GET_MODE (x) == TFmode
18181 || GET_MODE (x) == TImode
18182 || GET_MODE (x) == PTImode))
18184 /* Handle [reg]. */
18185 if (REG_P (tmp))
18187 fprintf (file, "0(%s)", reg_names[REGNO (tmp)]);
18188 break;
18190 /* Handle [reg+UIMM]. */
18191 else if (GET_CODE (tmp) == PLUS &&
18192 GET_CODE (XEXP (tmp, 1)) == CONST_INT)
18194 int x;
18196 gcc_assert (REG_P (XEXP (tmp, 0)));
18198 x = INTVAL (XEXP (tmp, 1));
18199 fprintf (file, "%d(%s)", x, reg_names[REGNO (XEXP (tmp, 0))]);
18200 break;
18203 /* Fall through. Must be [reg+reg]. */
18205 if (VECTOR_MEM_ALTIVEC_P (GET_MODE (x))
18206 && GET_CODE (tmp) == AND
18207 && GET_CODE (XEXP (tmp, 1)) == CONST_INT
18208 && INTVAL (XEXP (tmp, 1)) == -16)
18209 tmp = XEXP (tmp, 0);
18210 else if (VECTOR_MEM_VSX_P (GET_MODE (x))
18211 && GET_CODE (tmp) == PRE_MODIFY)
18212 tmp = XEXP (tmp, 1);
18213 if (REG_P (tmp))
18214 fprintf (file, "0,%s", reg_names[REGNO (tmp)]);
18215 else
18217 if (!GET_CODE (tmp) == PLUS
18218 || !REG_P (XEXP (tmp, 0))
18219 || !REG_P (XEXP (tmp, 1)))
18221 output_operand_lossage ("invalid %%y value, try using the 'Z' constraint");
18222 break;
18225 if (REGNO (XEXP (tmp, 0)) == 0)
18226 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 1)) ],
18227 reg_names[ REGNO (XEXP (tmp, 0)) ]);
18228 else
18229 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 0)) ],
18230 reg_names[ REGNO (XEXP (tmp, 1)) ]);
18232 break;
18235 case 0:
18236 if (REG_P (x))
18237 fprintf (file, "%s", reg_names[REGNO (x)]);
18238 else if (MEM_P (x))
18240 /* We need to handle PRE_INC and PRE_DEC here, since we need to
18241 know the width from the mode. */
18242 if (GET_CODE (XEXP (x, 0)) == PRE_INC)
18243 fprintf (file, "%d(%s)", GET_MODE_SIZE (GET_MODE (x)),
18244 reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
18245 else if (GET_CODE (XEXP (x, 0)) == PRE_DEC)
18246 fprintf (file, "%d(%s)", - GET_MODE_SIZE (GET_MODE (x)),
18247 reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
18248 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
18249 output_address (XEXP (XEXP (x, 0), 1));
18250 else
18251 output_address (XEXP (x, 0));
18253 else
18255 if (toc_relative_expr_p (x, false))
18256 /* This hack along with a corresponding hack in
18257 rs6000_output_addr_const_extra arranges to output addends
18258 where the assembler expects to find them. eg.
18259 (plus (unspec [(symbol_ref ("x")) (reg 2)] tocrel) 4)
18260 without this hack would be output as "x@toc+4". We
18261 want "x+4@toc". */
18262 output_addr_const (file, CONST_CAST_RTX (tocrel_base));
18263 else
18264 output_addr_const (file, x);
18266 return;
18268 case '&':
18269 assemble_name (file, rs6000_get_some_local_dynamic_name ());
18270 return;
18272 default:
18273 output_operand_lossage ("invalid %%xn code");
18277 /* Print the address of an operand. */
18279 void
18280 print_operand_address (FILE *file, rtx x)
18282 if (REG_P (x))
18283 fprintf (file, "0(%s)", reg_names[ REGNO (x) ]);
18284 else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST
18285 || GET_CODE (x) == LABEL_REF)
18287 output_addr_const (file, x);
18288 if (small_data_operand (x, GET_MODE (x)))
18289 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
18290 reg_names[SMALL_DATA_REG]);
18291 else
18292 gcc_assert (!TARGET_TOC);
18294 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
18295 && REG_P (XEXP (x, 1)))
18297 if (REGNO (XEXP (x, 0)) == 0)
18298 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 1)) ],
18299 reg_names[ REGNO (XEXP (x, 0)) ]);
18300 else
18301 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 0)) ],
18302 reg_names[ REGNO (XEXP (x, 1)) ]);
18304 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
18305 && GET_CODE (XEXP (x, 1)) == CONST_INT)
18306 fprintf (file, HOST_WIDE_INT_PRINT_DEC "(%s)",
18307 INTVAL (XEXP (x, 1)), reg_names[ REGNO (XEXP (x, 0)) ]);
18308 #if TARGET_MACHO
18309 else if (GET_CODE (x) == LO_SUM && REG_P (XEXP (x, 0))
18310 && CONSTANT_P (XEXP (x, 1)))
18312 fprintf (file, "lo16(");
18313 output_addr_const (file, XEXP (x, 1));
18314 fprintf (file, ")(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
18316 #endif
18317 #if TARGET_ELF
18318 else if (GET_CODE (x) == LO_SUM && REG_P (XEXP (x, 0))
18319 && CONSTANT_P (XEXP (x, 1)))
18321 output_addr_const (file, XEXP (x, 1));
18322 fprintf (file, "@l(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
18324 #endif
18325 else if (toc_relative_expr_p (x, false))
18327 /* This hack along with a corresponding hack in
18328 rs6000_output_addr_const_extra arranges to output addends
18329 where the assembler expects to find them. eg.
18330 (lo_sum (reg 9)
18331 . (plus (unspec [(symbol_ref ("x")) (reg 2)] tocrel) 8))
18332 without this hack would be output as "x@toc+8@l(9)". We
18333 want "x+8@toc@l(9)". */
18334 output_addr_const (file, CONST_CAST_RTX (tocrel_base));
18335 if (GET_CODE (x) == LO_SUM)
18336 fprintf (file, "@l(%s)", reg_names[REGNO (XEXP (x, 0))]);
18337 else
18338 fprintf (file, "(%s)", reg_names[REGNO (XVECEXP (tocrel_base, 0, 1))]);
18340 else
18341 gcc_unreachable ();
18344 /* Implement TARGET_OUTPUT_ADDR_CONST_EXTRA. */
18346 static bool
18347 rs6000_output_addr_const_extra (FILE *file, rtx x)
18349 if (GET_CODE (x) == UNSPEC)
18350 switch (XINT (x, 1))
18352 case UNSPEC_TOCREL:
18353 gcc_checking_assert (GET_CODE (XVECEXP (x, 0, 0)) == SYMBOL_REF
18354 && REG_P (XVECEXP (x, 0, 1))
18355 && REGNO (XVECEXP (x, 0, 1)) == TOC_REGISTER);
18356 output_addr_const (file, XVECEXP (x, 0, 0));
18357 if (x == tocrel_base && tocrel_offset != const0_rtx)
18359 if (INTVAL (tocrel_offset) >= 0)
18360 fprintf (file, "+");
18361 output_addr_const (file, CONST_CAST_RTX (tocrel_offset));
18363 if (!TARGET_AIX || (TARGET_ELF && TARGET_MINIMAL_TOC))
18365 putc ('-', file);
18366 assemble_name (file, toc_label_name);
18368 else if (TARGET_ELF)
18369 fputs ("@toc", file);
18370 return true;
18372 #if TARGET_MACHO
18373 case UNSPEC_MACHOPIC_OFFSET:
18374 output_addr_const (file, XVECEXP (x, 0, 0));
18375 putc ('-', file);
18376 machopic_output_function_base_name (file);
18377 return true;
18378 #endif
18380 return false;
18383 /* Target hook for assembling integer objects. The PowerPC version has
18384 to handle fixup entries for relocatable code if RELOCATABLE_NEEDS_FIXUP
18385 is defined. It also needs to handle DI-mode objects on 64-bit
18386 targets. */
18388 static bool
18389 rs6000_assemble_integer (rtx x, unsigned int size, int aligned_p)
18391 #ifdef RELOCATABLE_NEEDS_FIXUP
18392 /* Special handling for SI values. */
18393 if (RELOCATABLE_NEEDS_FIXUP && size == 4 && aligned_p)
18395 static int recurse = 0;
18397 /* For -mrelocatable, we mark all addresses that need to be fixed up in
18398 the .fixup section. Since the TOC section is already relocated, we
18399 don't need to mark it here. We used to skip the text section, but it
18400 should never be valid for relocated addresses to be placed in the text
18401 section. */
18402 if (TARGET_RELOCATABLE
18403 && in_section != toc_section
18404 && !recurse
18405 && GET_CODE (x) != CONST_INT
18406 && GET_CODE (x) != CONST_DOUBLE
18407 && CONSTANT_P (x))
18409 char buf[256];
18411 recurse = 1;
18412 ASM_GENERATE_INTERNAL_LABEL (buf, "LCP", fixuplabelno);
18413 fixuplabelno++;
18414 ASM_OUTPUT_LABEL (asm_out_file, buf);
18415 fprintf (asm_out_file, "\t.long\t(");
18416 output_addr_const (asm_out_file, x);
18417 fprintf (asm_out_file, ")@fixup\n");
18418 fprintf (asm_out_file, "\t.section\t\".fixup\",\"aw\"\n");
18419 ASM_OUTPUT_ALIGN (asm_out_file, 2);
18420 fprintf (asm_out_file, "\t.long\t");
18421 assemble_name (asm_out_file, buf);
18422 fprintf (asm_out_file, "\n\t.previous\n");
18423 recurse = 0;
18424 return true;
18426 /* Remove initial .'s to turn a -mcall-aixdesc function
18427 address into the address of the descriptor, not the function
18428 itself. */
18429 else if (GET_CODE (x) == SYMBOL_REF
18430 && XSTR (x, 0)[0] == '.'
18431 && DEFAULT_ABI == ABI_AIX)
18433 const char *name = XSTR (x, 0);
18434 while (*name == '.')
18435 name++;
18437 fprintf (asm_out_file, "\t.long\t%s\n", name);
18438 return true;
18441 #endif /* RELOCATABLE_NEEDS_FIXUP */
18442 return default_assemble_integer (x, size, aligned_p);
18445 #if defined (HAVE_GAS_HIDDEN) && !TARGET_MACHO
18446 /* Emit an assembler directive to set symbol visibility for DECL to
18447 VISIBILITY_TYPE. */
18449 static void
18450 rs6000_assemble_visibility (tree decl, int vis)
18452 if (TARGET_XCOFF)
18453 return;
18455 /* Functions need to have their entry point symbol visibility set as
18456 well as their descriptor symbol visibility. */
18457 if (DEFAULT_ABI == ABI_AIX
18458 && DOT_SYMBOLS
18459 && TREE_CODE (decl) == FUNCTION_DECL)
18461 static const char * const visibility_types[] = {
18462 NULL, "internal", "hidden", "protected"
18465 const char *name, *type;
18467 name = ((* targetm.strip_name_encoding)
18468 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
18469 type = visibility_types[vis];
18471 fprintf (asm_out_file, "\t.%s\t%s\n", type, name);
18472 fprintf (asm_out_file, "\t.%s\t.%s\n", type, name);
18474 else
18475 default_assemble_visibility (decl, vis);
18477 #endif
18479 enum rtx_code
18480 rs6000_reverse_condition (enum machine_mode mode, enum rtx_code code)
18482 /* Reversal of FP compares takes care -- an ordered compare
18483 becomes an unordered compare and vice versa. */
18484 if (mode == CCFPmode
18485 && (!flag_finite_math_only
18486 || code == UNLT || code == UNLE || code == UNGT || code == UNGE
18487 || code == UNEQ || code == LTGT))
18488 return reverse_condition_maybe_unordered (code);
18489 else
18490 return reverse_condition (code);
18493 /* Generate a compare for CODE. Return a brand-new rtx that
18494 represents the result of the compare. */
18496 static rtx
18497 rs6000_generate_compare (rtx cmp, enum machine_mode mode)
18499 enum machine_mode comp_mode;
18500 rtx compare_result;
18501 enum rtx_code code = GET_CODE (cmp);
18502 rtx op0 = XEXP (cmp, 0);
18503 rtx op1 = XEXP (cmp, 1);
18505 if (FLOAT_MODE_P (mode))
18506 comp_mode = CCFPmode;
18507 else if (code == GTU || code == LTU
18508 || code == GEU || code == LEU)
18509 comp_mode = CCUNSmode;
18510 else if ((code == EQ || code == NE)
18511 && unsigned_reg_p (op0)
18512 && (unsigned_reg_p (op1)
18513 || (CONST_INT_P (op1) && INTVAL (op1) != 0)))
18514 /* These are unsigned values, perhaps there will be a later
18515 ordering compare that can be shared with this one. */
18516 comp_mode = CCUNSmode;
18517 else
18518 comp_mode = CCmode;
18520 /* If we have an unsigned compare, make sure we don't have a signed value as
18521 an immediate. */
18522 if (comp_mode == CCUNSmode && GET_CODE (op1) == CONST_INT
18523 && INTVAL (op1) < 0)
18525 op0 = copy_rtx_if_shared (op0);
18526 op1 = force_reg (GET_MODE (op0), op1);
18527 cmp = gen_rtx_fmt_ee (code, GET_MODE (cmp), op0, op1);
18530 /* First, the compare. */
18531 compare_result = gen_reg_rtx (comp_mode);
18533 /* E500 FP compare instructions on the GPRs. Yuck! */
18534 if ((!TARGET_FPRS && TARGET_HARD_FLOAT)
18535 && FLOAT_MODE_P (mode))
18537 rtx cmp, or_result, compare_result2;
18538 enum machine_mode op_mode = GET_MODE (op0);
18539 bool reverse_p;
18541 if (op_mode == VOIDmode)
18542 op_mode = GET_MODE (op1);
18544 /* First reverse the condition codes that aren't directly supported. */
18545 switch (code)
18547 case NE:
18548 case UNLT:
18549 case UNLE:
18550 case UNGT:
18551 case UNGE:
18552 code = reverse_condition_maybe_unordered (code);
18553 reverse_p = true;
18554 break;
18556 case EQ:
18557 case LT:
18558 case LE:
18559 case GT:
18560 case GE:
18561 reverse_p = false;
18562 break;
18564 default:
18565 gcc_unreachable ();
18568 /* The E500 FP compare instructions toggle the GT bit (CR bit 1) only.
18569 This explains the following mess. */
18571 switch (code)
18573 case EQ:
18574 switch (op_mode)
18576 case SFmode:
18577 cmp = (flag_finite_math_only && !flag_trapping_math)
18578 ? gen_tstsfeq_gpr (compare_result, op0, op1)
18579 : gen_cmpsfeq_gpr (compare_result, op0, op1);
18580 break;
18582 case DFmode:
18583 cmp = (flag_finite_math_only && !flag_trapping_math)
18584 ? gen_tstdfeq_gpr (compare_result, op0, op1)
18585 : gen_cmpdfeq_gpr (compare_result, op0, op1);
18586 break;
18588 case TFmode:
18589 cmp = (flag_finite_math_only && !flag_trapping_math)
18590 ? gen_tsttfeq_gpr (compare_result, op0, op1)
18591 : gen_cmptfeq_gpr (compare_result, op0, op1);
18592 break;
18594 default:
18595 gcc_unreachable ();
18597 break;
18599 case GT:
18600 case GE:
18601 switch (op_mode)
18603 case SFmode:
18604 cmp = (flag_finite_math_only && !flag_trapping_math)
18605 ? gen_tstsfgt_gpr (compare_result, op0, op1)
18606 : gen_cmpsfgt_gpr (compare_result, op0, op1);
18607 break;
18609 case DFmode:
18610 cmp = (flag_finite_math_only && !flag_trapping_math)
18611 ? gen_tstdfgt_gpr (compare_result, op0, op1)
18612 : gen_cmpdfgt_gpr (compare_result, op0, op1);
18613 break;
18615 case TFmode:
18616 cmp = (flag_finite_math_only && !flag_trapping_math)
18617 ? gen_tsttfgt_gpr (compare_result, op0, op1)
18618 : gen_cmptfgt_gpr (compare_result, op0, op1);
18619 break;
18621 default:
18622 gcc_unreachable ();
18624 break;
18626 case LT:
18627 case LE:
18628 switch (op_mode)
18630 case SFmode:
18631 cmp = (flag_finite_math_only && !flag_trapping_math)
18632 ? gen_tstsflt_gpr (compare_result, op0, op1)
18633 : gen_cmpsflt_gpr (compare_result, op0, op1);
18634 break;
18636 case DFmode:
18637 cmp = (flag_finite_math_only && !flag_trapping_math)
18638 ? gen_tstdflt_gpr (compare_result, op0, op1)
18639 : gen_cmpdflt_gpr (compare_result, op0, op1);
18640 break;
18642 case TFmode:
18643 cmp = (flag_finite_math_only && !flag_trapping_math)
18644 ? gen_tsttflt_gpr (compare_result, op0, op1)
18645 : gen_cmptflt_gpr (compare_result, op0, op1);
18646 break;
18648 default:
18649 gcc_unreachable ();
18651 break;
18653 default:
18654 gcc_unreachable ();
18657 /* Synthesize LE and GE from LT/GT || EQ. */
18658 if (code == LE || code == GE)
18660 emit_insn (cmp);
18662 compare_result2 = gen_reg_rtx (CCFPmode);
18664 /* Do the EQ. */
18665 switch (op_mode)
18667 case SFmode:
18668 cmp = (flag_finite_math_only && !flag_trapping_math)
18669 ? gen_tstsfeq_gpr (compare_result2, op0, op1)
18670 : gen_cmpsfeq_gpr (compare_result2, op0, op1);
18671 break;
18673 case DFmode:
18674 cmp = (flag_finite_math_only && !flag_trapping_math)
18675 ? gen_tstdfeq_gpr (compare_result2, op0, op1)
18676 : gen_cmpdfeq_gpr (compare_result2, op0, op1);
18677 break;
18679 case TFmode:
18680 cmp = (flag_finite_math_only && !flag_trapping_math)
18681 ? gen_tsttfeq_gpr (compare_result2, op0, op1)
18682 : gen_cmptfeq_gpr (compare_result2, op0, op1);
18683 break;
18685 default:
18686 gcc_unreachable ();
18689 emit_insn (cmp);
18691 /* OR them together. */
18692 or_result = gen_reg_rtx (CCFPmode);
18693 cmp = gen_e500_cr_ior_compare (or_result, compare_result,
18694 compare_result2);
18695 compare_result = or_result;
18698 code = reverse_p ? NE : EQ;
18700 emit_insn (cmp);
18702 else
18704 /* Generate XLC-compatible TFmode compare as PARALLEL with extra
18705 CLOBBERs to match cmptf_internal2 pattern. */
18706 if (comp_mode == CCFPmode && TARGET_XL_COMPAT
18707 && GET_MODE (op0) == TFmode
18708 && !TARGET_IEEEQUAD
18709 && TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_LONG_DOUBLE_128)
18710 emit_insn (gen_rtx_PARALLEL (VOIDmode,
18711 gen_rtvec (10,
18712 gen_rtx_SET (VOIDmode,
18713 compare_result,
18714 gen_rtx_COMPARE (comp_mode, op0, op1)),
18715 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
18716 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
18717 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
18718 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
18719 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
18720 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
18721 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
18722 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
18723 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (Pmode)))));
18724 else if (GET_CODE (op1) == UNSPEC
18725 && XINT (op1, 1) == UNSPEC_SP_TEST)
18727 rtx op1b = XVECEXP (op1, 0, 0);
18728 comp_mode = CCEQmode;
18729 compare_result = gen_reg_rtx (CCEQmode);
18730 if (TARGET_64BIT)
18731 emit_insn (gen_stack_protect_testdi (compare_result, op0, op1b));
18732 else
18733 emit_insn (gen_stack_protect_testsi (compare_result, op0, op1b));
18735 else
18736 emit_insn (gen_rtx_SET (VOIDmode, compare_result,
18737 gen_rtx_COMPARE (comp_mode, op0, op1)));
18740 /* Some kinds of FP comparisons need an OR operation;
18741 under flag_finite_math_only we don't bother. */
18742 if (FLOAT_MODE_P (mode)
18743 && !flag_finite_math_only
18744 && !(TARGET_HARD_FLOAT && !TARGET_FPRS)
18745 && (code == LE || code == GE
18746 || code == UNEQ || code == LTGT
18747 || code == UNGT || code == UNLT))
18749 enum rtx_code or1, or2;
18750 rtx or1_rtx, or2_rtx, compare2_rtx;
18751 rtx or_result = gen_reg_rtx (CCEQmode);
18753 switch (code)
18755 case LE: or1 = LT; or2 = EQ; break;
18756 case GE: or1 = GT; or2 = EQ; break;
18757 case UNEQ: or1 = UNORDERED; or2 = EQ; break;
18758 case LTGT: or1 = LT; or2 = GT; break;
18759 case UNGT: or1 = UNORDERED; or2 = GT; break;
18760 case UNLT: or1 = UNORDERED; or2 = LT; break;
18761 default: gcc_unreachable ();
18763 validate_condition_mode (or1, comp_mode);
18764 validate_condition_mode (or2, comp_mode);
18765 or1_rtx = gen_rtx_fmt_ee (or1, SImode, compare_result, const0_rtx);
18766 or2_rtx = gen_rtx_fmt_ee (or2, SImode, compare_result, const0_rtx);
18767 compare2_rtx = gen_rtx_COMPARE (CCEQmode,
18768 gen_rtx_IOR (SImode, or1_rtx, or2_rtx),
18769 const_true_rtx);
18770 emit_insn (gen_rtx_SET (VOIDmode, or_result, compare2_rtx));
18772 compare_result = or_result;
18773 code = EQ;
18776 validate_condition_mode (code, GET_MODE (compare_result));
18778 return gen_rtx_fmt_ee (code, VOIDmode, compare_result, const0_rtx);
18782 /* Emit the RTL for an sISEL pattern. */
18784 void
18785 rs6000_emit_sISEL (enum machine_mode mode ATTRIBUTE_UNUSED, rtx operands[])
18787 rs6000_emit_int_cmove (operands[0], operands[1], const1_rtx, const0_rtx);
18790 void
18791 rs6000_emit_sCOND (enum machine_mode mode, rtx operands[])
18793 rtx condition_rtx;
18794 enum machine_mode op_mode;
18795 enum rtx_code cond_code;
18796 rtx result = operands[0];
18798 if (TARGET_ISEL && (mode == SImode || mode == DImode))
18800 rs6000_emit_sISEL (mode, operands);
18801 return;
18804 condition_rtx = rs6000_generate_compare (operands[1], mode);
18805 cond_code = GET_CODE (condition_rtx);
18807 if (FLOAT_MODE_P (mode)
18808 && !TARGET_FPRS && TARGET_HARD_FLOAT)
18810 rtx t;
18812 PUT_MODE (condition_rtx, SImode);
18813 t = XEXP (condition_rtx, 0);
18815 gcc_assert (cond_code == NE || cond_code == EQ);
18817 if (cond_code == NE)
18818 emit_insn (gen_e500_flip_gt_bit (t, t));
18820 emit_insn (gen_move_from_CR_gt_bit (result, t));
18821 return;
18824 if (cond_code == NE
18825 || cond_code == GE || cond_code == LE
18826 || cond_code == GEU || cond_code == LEU
18827 || cond_code == ORDERED || cond_code == UNGE || cond_code == UNLE)
18829 rtx not_result = gen_reg_rtx (CCEQmode);
18830 rtx not_op, rev_cond_rtx;
18831 enum machine_mode cc_mode;
18833 cc_mode = GET_MODE (XEXP (condition_rtx, 0));
18835 rev_cond_rtx = gen_rtx_fmt_ee (rs6000_reverse_condition (cc_mode, cond_code),
18836 SImode, XEXP (condition_rtx, 0), const0_rtx);
18837 not_op = gen_rtx_COMPARE (CCEQmode, rev_cond_rtx, const0_rtx);
18838 emit_insn (gen_rtx_SET (VOIDmode, not_result, not_op));
18839 condition_rtx = gen_rtx_EQ (VOIDmode, not_result, const0_rtx);
18842 op_mode = GET_MODE (XEXP (operands[1], 0));
18843 if (op_mode == VOIDmode)
18844 op_mode = GET_MODE (XEXP (operands[1], 1));
18846 if (TARGET_POWERPC64 && (op_mode == DImode || FLOAT_MODE_P (mode)))
18848 PUT_MODE (condition_rtx, DImode);
18849 convert_move (result, condition_rtx, 0);
18851 else
18853 PUT_MODE (condition_rtx, SImode);
18854 emit_insn (gen_rtx_SET (VOIDmode, result, condition_rtx));
18858 /* Emit a branch of kind CODE to location LOC. */
18860 void
18861 rs6000_emit_cbranch (enum machine_mode mode, rtx operands[])
18863 rtx condition_rtx, loc_ref;
18865 condition_rtx = rs6000_generate_compare (operands[0], mode);
18866 loc_ref = gen_rtx_LABEL_REF (VOIDmode, operands[3]);
18867 emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx,
18868 gen_rtx_IF_THEN_ELSE (VOIDmode, condition_rtx,
18869 loc_ref, pc_rtx)));
18872 /* Return the string to output a conditional branch to LABEL, which is
18873 the operand template of the label, or NULL if the branch is really a
18874 conditional return.
18876 OP is the conditional expression. XEXP (OP, 0) is assumed to be a
18877 condition code register and its mode specifies what kind of
18878 comparison we made.
18880 REVERSED is nonzero if we should reverse the sense of the comparison.
18882 INSN is the insn. */
18884 char *
18885 output_cbranch (rtx op, const char *label, int reversed, rtx insn)
18887 static char string[64];
18888 enum rtx_code code = GET_CODE (op);
18889 rtx cc_reg = XEXP (op, 0);
18890 enum machine_mode mode = GET_MODE (cc_reg);
18891 int cc_regno = REGNO (cc_reg) - CR0_REGNO;
18892 int need_longbranch = label != NULL && get_attr_length (insn) == 8;
18893 int really_reversed = reversed ^ need_longbranch;
18894 char *s = string;
18895 const char *ccode;
18896 const char *pred;
18897 rtx note;
18899 validate_condition_mode (code, mode);
18901 /* Work out which way this really branches. We could use
18902 reverse_condition_maybe_unordered here always but this
18903 makes the resulting assembler clearer. */
18904 if (really_reversed)
18906 /* Reversal of FP compares takes care -- an ordered compare
18907 becomes an unordered compare and vice versa. */
18908 if (mode == CCFPmode)
18909 code = reverse_condition_maybe_unordered (code);
18910 else
18911 code = reverse_condition (code);
18914 if ((!TARGET_FPRS && TARGET_HARD_FLOAT) && mode == CCFPmode)
18916 /* The efscmp/tst* instructions twiddle bit 2, which maps nicely
18917 to the GT bit. */
18918 switch (code)
18920 case EQ:
18921 /* Opposite of GT. */
18922 code = GT;
18923 break;
18925 case NE:
18926 code = UNLE;
18927 break;
18929 default:
18930 gcc_unreachable ();
18934 switch (code)
18936 /* Not all of these are actually distinct opcodes, but
18937 we distinguish them for clarity of the resulting assembler. */
18938 case NE: case LTGT:
18939 ccode = "ne"; break;
18940 case EQ: case UNEQ:
18941 ccode = "eq"; break;
18942 case GE: case GEU:
18943 ccode = "ge"; break;
18944 case GT: case GTU: case UNGT:
18945 ccode = "gt"; break;
18946 case LE: case LEU:
18947 ccode = "le"; break;
18948 case LT: case LTU: case UNLT:
18949 ccode = "lt"; break;
18950 case UNORDERED: ccode = "un"; break;
18951 case ORDERED: ccode = "nu"; break;
18952 case UNGE: ccode = "nl"; break;
18953 case UNLE: ccode = "ng"; break;
18954 default:
18955 gcc_unreachable ();
18958 /* Maybe we have a guess as to how likely the branch is. */
18959 pred = "";
18960 note = find_reg_note (insn, REG_BR_PROB, NULL_RTX);
18961 if (note != NULL_RTX)
18963 /* PROB is the difference from 50%. */
18964 int prob = XINT (note, 0) - REG_BR_PROB_BASE / 2;
18966 /* Only hint for highly probable/improbable branches on newer
18967 cpus as static prediction overrides processor dynamic
18968 prediction. For older cpus we may as well always hint, but
18969 assume not taken for branches that are very close to 50% as a
18970 mispredicted taken branch is more expensive than a
18971 mispredicted not-taken branch. */
18972 if (rs6000_always_hint
18973 || (abs (prob) > REG_BR_PROB_BASE / 100 * 48
18974 && br_prob_note_reliable_p (note)))
18976 if (abs (prob) > REG_BR_PROB_BASE / 20
18977 && ((prob > 0) ^ need_longbranch))
18978 pred = "+";
18979 else
18980 pred = "-";
18984 if (label == NULL)
18985 s += sprintf (s, "b%slr%s ", ccode, pred);
18986 else
18987 s += sprintf (s, "b%s%s ", ccode, pred);
18989 /* We need to escape any '%' characters in the reg_names string.
18990 Assume they'd only be the first character.... */
18991 if (reg_names[cc_regno + CR0_REGNO][0] == '%')
18992 *s++ = '%';
18993 s += sprintf (s, "%s", reg_names[cc_regno + CR0_REGNO]);
18995 if (label != NULL)
18997 /* If the branch distance was too far, we may have to use an
18998 unconditional branch to go the distance. */
18999 if (need_longbranch)
19000 s += sprintf (s, ",$+8\n\tb %s", label);
19001 else
19002 s += sprintf (s, ",%s", label);
19005 return string;
19008 /* Return the string to flip the GT bit on a CR. */
19009 char *
19010 output_e500_flip_gt_bit (rtx dst, rtx src)
19012 static char string[64];
19013 int a, b;
19015 gcc_assert (GET_CODE (dst) == REG && CR_REGNO_P (REGNO (dst))
19016 && GET_CODE (src) == REG && CR_REGNO_P (REGNO (src)));
19018 /* GT bit. */
19019 a = 4 * (REGNO (dst) - CR0_REGNO) + 1;
19020 b = 4 * (REGNO (src) - CR0_REGNO) + 1;
19022 sprintf (string, "crnot %d,%d", a, b);
19023 return string;
19026 /* Return insn for VSX or Altivec comparisons. */
19028 static rtx
19029 rs6000_emit_vector_compare_inner (enum rtx_code code, rtx op0, rtx op1)
19031 rtx mask;
19032 enum machine_mode mode = GET_MODE (op0);
19034 switch (code)
19036 default:
19037 break;
19039 case GE:
19040 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
19041 return NULL_RTX;
19043 case EQ:
19044 case GT:
19045 case GTU:
19046 case ORDERED:
19047 case UNORDERED:
19048 case UNEQ:
19049 case LTGT:
19050 mask = gen_reg_rtx (mode);
19051 emit_insn (gen_rtx_SET (VOIDmode,
19052 mask,
19053 gen_rtx_fmt_ee (code, mode, op0, op1)));
19054 return mask;
19057 return NULL_RTX;
19060 /* Emit vector compare for operands OP0 and OP1 using code RCODE.
19061 DMODE is expected destination mode. This is a recursive function. */
19063 static rtx
19064 rs6000_emit_vector_compare (enum rtx_code rcode,
19065 rtx op0, rtx op1,
19066 enum machine_mode dmode)
19068 rtx mask;
19069 bool swap_operands = false;
19070 bool try_again = false;
19072 gcc_assert (VECTOR_UNIT_ALTIVEC_OR_VSX_P (dmode));
19073 gcc_assert (GET_MODE (op0) == GET_MODE (op1));
19075 /* See if the comparison works as is. */
19076 mask = rs6000_emit_vector_compare_inner (rcode, op0, op1);
19077 if (mask)
19078 return mask;
19080 switch (rcode)
19082 case LT:
19083 rcode = GT;
19084 swap_operands = true;
19085 try_again = true;
19086 break;
19087 case LTU:
19088 rcode = GTU;
19089 swap_operands = true;
19090 try_again = true;
19091 break;
19092 case NE:
19093 case UNLE:
19094 case UNLT:
19095 case UNGE:
19096 case UNGT:
19097 /* Invert condition and try again.
19098 e.g., A != B becomes ~(A==B). */
19100 enum rtx_code rev_code;
19101 enum insn_code nor_code;
19102 rtx mask2;
19104 rev_code = reverse_condition_maybe_unordered (rcode);
19105 if (rev_code == UNKNOWN)
19106 return NULL_RTX;
19108 nor_code = optab_handler (one_cmpl_optab, dmode);
19109 if (nor_code == CODE_FOR_nothing)
19110 return NULL_RTX;
19112 mask2 = rs6000_emit_vector_compare (rev_code, op0, op1, dmode);
19113 if (!mask2)
19114 return NULL_RTX;
19116 mask = gen_reg_rtx (dmode);
19117 emit_insn (GEN_FCN (nor_code) (mask, mask2));
19118 return mask;
19120 break;
19121 case GE:
19122 case GEU:
19123 case LE:
19124 case LEU:
19125 /* Try GT/GTU/LT/LTU OR EQ */
19127 rtx c_rtx, eq_rtx;
19128 enum insn_code ior_code;
19129 enum rtx_code new_code;
19131 switch (rcode)
19133 case GE:
19134 new_code = GT;
19135 break;
19137 case GEU:
19138 new_code = GTU;
19139 break;
19141 case LE:
19142 new_code = LT;
19143 break;
19145 case LEU:
19146 new_code = LTU;
19147 break;
19149 default:
19150 gcc_unreachable ();
19153 ior_code = optab_handler (ior_optab, dmode);
19154 if (ior_code == CODE_FOR_nothing)
19155 return NULL_RTX;
19157 c_rtx = rs6000_emit_vector_compare (new_code, op0, op1, dmode);
19158 if (!c_rtx)
19159 return NULL_RTX;
19161 eq_rtx = rs6000_emit_vector_compare (EQ, op0, op1, dmode);
19162 if (!eq_rtx)
19163 return NULL_RTX;
19165 mask = gen_reg_rtx (dmode);
19166 emit_insn (GEN_FCN (ior_code) (mask, c_rtx, eq_rtx));
19167 return mask;
19169 break;
19170 default:
19171 return NULL_RTX;
19174 if (try_again)
19176 if (swap_operands)
19178 rtx tmp;
19179 tmp = op0;
19180 op0 = op1;
19181 op1 = tmp;
19184 mask = rs6000_emit_vector_compare_inner (rcode, op0, op1);
19185 if (mask)
19186 return mask;
19189 /* You only get two chances. */
19190 return NULL_RTX;
19193 /* Emit vector conditional expression. DEST is destination. OP_TRUE and
19194 OP_FALSE are two VEC_COND_EXPR operands. CC_OP0 and CC_OP1 are the two
19195 operands for the relation operation COND. */
19198 rs6000_emit_vector_cond_expr (rtx dest, rtx op_true, rtx op_false,
19199 rtx cond, rtx cc_op0, rtx cc_op1)
19201 enum machine_mode dest_mode = GET_MODE (dest);
19202 enum machine_mode mask_mode = GET_MODE (cc_op0);
19203 enum rtx_code rcode = GET_CODE (cond);
19204 enum machine_mode cc_mode = CCmode;
19205 rtx mask;
19206 rtx cond2;
19207 rtx tmp;
19208 bool invert_move = false;
19210 if (VECTOR_UNIT_NONE_P (dest_mode))
19211 return 0;
19213 gcc_assert (GET_MODE_SIZE (dest_mode) == GET_MODE_SIZE (mask_mode)
19214 && GET_MODE_NUNITS (dest_mode) == GET_MODE_NUNITS (mask_mode));
19216 switch (rcode)
19218 /* Swap operands if we can, and fall back to doing the operation as
19219 specified, and doing a NOR to invert the test. */
19220 case NE:
19221 case UNLE:
19222 case UNLT:
19223 case UNGE:
19224 case UNGT:
19225 /* Invert condition and try again.
19226 e.g., A = (B != C) ? D : E becomes A = (B == C) ? E : D. */
19227 invert_move = true;
19228 rcode = reverse_condition_maybe_unordered (rcode);
19229 if (rcode == UNKNOWN)
19230 return 0;
19231 break;
19233 /* Mark unsigned tests with CCUNSmode. */
19234 case GTU:
19235 case GEU:
19236 case LTU:
19237 case LEU:
19238 cc_mode = CCUNSmode;
19239 break;
19241 default:
19242 break;
19245 /* Get the vector mask for the given relational operations. */
19246 mask = rs6000_emit_vector_compare (rcode, cc_op0, cc_op1, mask_mode);
19248 if (!mask)
19249 return 0;
19251 if (invert_move)
19253 tmp = op_true;
19254 op_true = op_false;
19255 op_false = tmp;
19258 cond2 = gen_rtx_fmt_ee (NE, cc_mode, gen_lowpart (dest_mode, mask),
19259 CONST0_RTX (dest_mode));
19260 emit_insn (gen_rtx_SET (VOIDmode,
19261 dest,
19262 gen_rtx_IF_THEN_ELSE (dest_mode,
19263 cond2,
19264 op_true,
19265 op_false)));
19266 return 1;
19269 /* Emit a conditional move: move TRUE_COND to DEST if OP of the
19270 operands of the last comparison is nonzero/true, FALSE_COND if it
19271 is zero/false. Return 0 if the hardware has no such operation. */
19274 rs6000_emit_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
19276 enum rtx_code code = GET_CODE (op);
19277 rtx op0 = XEXP (op, 0);
19278 rtx op1 = XEXP (op, 1);
19279 REAL_VALUE_TYPE c1;
19280 enum machine_mode compare_mode = GET_MODE (op0);
19281 enum machine_mode result_mode = GET_MODE (dest);
19282 rtx temp;
19283 bool is_against_zero;
19285 /* These modes should always match. */
19286 if (GET_MODE (op1) != compare_mode
19287 /* In the isel case however, we can use a compare immediate, so
19288 op1 may be a small constant. */
19289 && (!TARGET_ISEL || !short_cint_operand (op1, VOIDmode)))
19290 return 0;
19291 if (GET_MODE (true_cond) != result_mode)
19292 return 0;
19293 if (GET_MODE (false_cond) != result_mode)
19294 return 0;
19296 /* Don't allow using floating point comparisons for integer results for
19297 now. */
19298 if (FLOAT_MODE_P (compare_mode) && !FLOAT_MODE_P (result_mode))
19299 return 0;
19301 /* First, work out if the hardware can do this at all, or
19302 if it's too slow.... */
19303 if (!FLOAT_MODE_P (compare_mode))
19305 if (TARGET_ISEL)
19306 return rs6000_emit_int_cmove (dest, op, true_cond, false_cond);
19307 return 0;
19309 else if (TARGET_HARD_FLOAT && !TARGET_FPRS
19310 && SCALAR_FLOAT_MODE_P (compare_mode))
19311 return 0;
19313 is_against_zero = op1 == CONST0_RTX (compare_mode);
19315 /* A floating-point subtract might overflow, underflow, or produce
19316 an inexact result, thus changing the floating-point flags, so it
19317 can't be generated if we care about that. It's safe if one side
19318 of the construct is zero, since then no subtract will be
19319 generated. */
19320 if (SCALAR_FLOAT_MODE_P (compare_mode)
19321 && flag_trapping_math && ! is_against_zero)
19322 return 0;
19324 /* Eliminate half of the comparisons by switching operands, this
19325 makes the remaining code simpler. */
19326 if (code == UNLT || code == UNGT || code == UNORDERED || code == NE
19327 || code == LTGT || code == LT || code == UNLE)
19329 code = reverse_condition_maybe_unordered (code);
19330 temp = true_cond;
19331 true_cond = false_cond;
19332 false_cond = temp;
19335 /* UNEQ and LTGT take four instructions for a comparison with zero,
19336 it'll probably be faster to use a branch here too. */
19337 if (code == UNEQ && HONOR_NANS (compare_mode))
19338 return 0;
19340 if (GET_CODE (op1) == CONST_DOUBLE)
19341 REAL_VALUE_FROM_CONST_DOUBLE (c1, op1);
19343 /* We're going to try to implement comparisons by performing
19344 a subtract, then comparing against zero. Unfortunately,
19345 Inf - Inf is NaN which is not zero, and so if we don't
19346 know that the operand is finite and the comparison
19347 would treat EQ different to UNORDERED, we can't do it. */
19348 if (HONOR_INFINITIES (compare_mode)
19349 && code != GT && code != UNGE
19350 && (GET_CODE (op1) != CONST_DOUBLE || real_isinf (&c1))
19351 /* Constructs of the form (a OP b ? a : b) are safe. */
19352 && ((! rtx_equal_p (op0, false_cond) && ! rtx_equal_p (op1, false_cond))
19353 || (! rtx_equal_p (op0, true_cond)
19354 && ! rtx_equal_p (op1, true_cond))))
19355 return 0;
19357 /* At this point we know we can use fsel. */
19359 /* Reduce the comparison to a comparison against zero. */
19360 if (! is_against_zero)
19362 temp = gen_reg_rtx (compare_mode);
19363 emit_insn (gen_rtx_SET (VOIDmode, temp,
19364 gen_rtx_MINUS (compare_mode, op0, op1)));
19365 op0 = temp;
19366 op1 = CONST0_RTX (compare_mode);
19369 /* If we don't care about NaNs we can reduce some of the comparisons
19370 down to faster ones. */
19371 if (! HONOR_NANS (compare_mode))
19372 switch (code)
19374 case GT:
19375 code = LE;
19376 temp = true_cond;
19377 true_cond = false_cond;
19378 false_cond = temp;
19379 break;
19380 case UNGE:
19381 code = GE;
19382 break;
19383 case UNEQ:
19384 code = EQ;
19385 break;
19386 default:
19387 break;
19390 /* Now, reduce everything down to a GE. */
19391 switch (code)
19393 case GE:
19394 break;
19396 case LE:
19397 temp = gen_reg_rtx (compare_mode);
19398 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
19399 op0 = temp;
19400 break;
19402 case ORDERED:
19403 temp = gen_reg_rtx (compare_mode);
19404 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_ABS (compare_mode, op0)));
19405 op0 = temp;
19406 break;
19408 case EQ:
19409 temp = gen_reg_rtx (compare_mode);
19410 emit_insn (gen_rtx_SET (VOIDmode, temp,
19411 gen_rtx_NEG (compare_mode,
19412 gen_rtx_ABS (compare_mode, op0))));
19413 op0 = temp;
19414 break;
19416 case UNGE:
19417 /* a UNGE 0 <-> (a GE 0 || -a UNLT 0) */
19418 temp = gen_reg_rtx (result_mode);
19419 emit_insn (gen_rtx_SET (VOIDmode, temp,
19420 gen_rtx_IF_THEN_ELSE (result_mode,
19421 gen_rtx_GE (VOIDmode,
19422 op0, op1),
19423 true_cond, false_cond)));
19424 false_cond = true_cond;
19425 true_cond = temp;
19427 temp = gen_reg_rtx (compare_mode);
19428 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
19429 op0 = temp;
19430 break;
19432 case GT:
19433 /* a GT 0 <-> (a GE 0 && -a UNLT 0) */
19434 temp = gen_reg_rtx (result_mode);
19435 emit_insn (gen_rtx_SET (VOIDmode, temp,
19436 gen_rtx_IF_THEN_ELSE (result_mode,
19437 gen_rtx_GE (VOIDmode,
19438 op0, op1),
19439 true_cond, false_cond)));
19440 true_cond = false_cond;
19441 false_cond = temp;
19443 temp = gen_reg_rtx (compare_mode);
19444 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
19445 op0 = temp;
19446 break;
19448 default:
19449 gcc_unreachable ();
19452 emit_insn (gen_rtx_SET (VOIDmode, dest,
19453 gen_rtx_IF_THEN_ELSE (result_mode,
19454 gen_rtx_GE (VOIDmode,
19455 op0, op1),
19456 true_cond, false_cond)));
19457 return 1;
19460 /* Same as above, but for ints (isel). */
19462 static int
19463 rs6000_emit_int_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
19465 rtx condition_rtx, cr;
19466 enum machine_mode mode = GET_MODE (dest);
19467 enum rtx_code cond_code;
19468 rtx (*isel_func) (rtx, rtx, rtx, rtx, rtx);
19469 bool signedp;
19471 if (mode != SImode && (!TARGET_POWERPC64 || mode != DImode))
19472 return 0;
19474 /* We still have to do the compare, because isel doesn't do a
19475 compare, it just looks at the CRx bits set by a previous compare
19476 instruction. */
19477 condition_rtx = rs6000_generate_compare (op, mode);
19478 cond_code = GET_CODE (condition_rtx);
19479 cr = XEXP (condition_rtx, 0);
19480 signedp = GET_MODE (cr) == CCmode;
19482 isel_func = (mode == SImode
19483 ? (signedp ? gen_isel_signed_si : gen_isel_unsigned_si)
19484 : (signedp ? gen_isel_signed_di : gen_isel_unsigned_di));
19486 switch (cond_code)
19488 case LT: case GT: case LTU: case GTU: case EQ:
19489 /* isel handles these directly. */
19490 break;
19492 default:
19493 /* We need to swap the sense of the comparison. */
19495 rtx t = true_cond;
19496 true_cond = false_cond;
19497 false_cond = t;
19498 PUT_CODE (condition_rtx, reverse_condition (cond_code));
19500 break;
19503 false_cond = force_reg (mode, false_cond);
19504 if (true_cond != const0_rtx)
19505 true_cond = force_reg (mode, true_cond);
19507 emit_insn (isel_func (dest, condition_rtx, true_cond, false_cond, cr));
19509 return 1;
19512 const char *
19513 output_isel (rtx *operands)
19515 enum rtx_code code;
19517 code = GET_CODE (operands[1]);
19519 if (code == GE || code == GEU || code == LE || code == LEU || code == NE)
19521 gcc_assert (GET_CODE (operands[2]) == REG
19522 && GET_CODE (operands[3]) == REG);
19523 PUT_CODE (operands[1], reverse_condition (code));
19524 return "isel %0,%3,%2,%j1";
19527 return "isel %0,%2,%3,%j1";
19530 void
19531 rs6000_emit_minmax (rtx dest, enum rtx_code code, rtx op0, rtx op1)
19533 enum machine_mode mode = GET_MODE (op0);
19534 enum rtx_code c;
19535 rtx target;
19537 /* VSX/altivec have direct min/max insns. */
19538 if ((code == SMAX || code == SMIN)
19539 && (VECTOR_UNIT_ALTIVEC_OR_VSX_P (mode)
19540 || (mode == SFmode && VECTOR_UNIT_VSX_P (DFmode))))
19542 emit_insn (gen_rtx_SET (VOIDmode,
19543 dest,
19544 gen_rtx_fmt_ee (code, mode, op0, op1)));
19545 return;
19548 if (code == SMAX || code == SMIN)
19549 c = GE;
19550 else
19551 c = GEU;
19553 if (code == SMAX || code == UMAX)
19554 target = emit_conditional_move (dest, c, op0, op1, mode,
19555 op0, op1, mode, 0);
19556 else
19557 target = emit_conditional_move (dest, c, op0, op1, mode,
19558 op1, op0, mode, 0);
19559 gcc_assert (target);
19560 if (target != dest)
19561 emit_move_insn (dest, target);
19564 /* A subroutine of the atomic operation splitters. Jump to LABEL if
19565 COND is true. Mark the jump as unlikely to be taken. */
19567 static void
19568 emit_unlikely_jump (rtx cond, rtx label)
19570 int very_unlikely = REG_BR_PROB_BASE / 100 - 1;
19571 rtx x;
19573 x = gen_rtx_IF_THEN_ELSE (VOIDmode, cond, label, pc_rtx);
19574 x = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, x));
19575 add_int_reg_note (x, REG_BR_PROB, very_unlikely);
19578 /* A subroutine of the atomic operation splitters. Emit a load-locked
19579 instruction in MODE. For QI/HImode, possibly use a pattern than includes
19580 the zero_extend operation. */
19582 static void
19583 emit_load_locked (enum machine_mode mode, rtx reg, rtx mem)
19585 rtx (*fn) (rtx, rtx) = NULL;
19587 switch (mode)
19589 case QImode:
19590 fn = gen_load_lockedqi;
19591 break;
19592 case HImode:
19593 fn = gen_load_lockedhi;
19594 break;
19595 case SImode:
19596 if (GET_MODE (mem) == QImode)
19597 fn = gen_load_lockedqi_si;
19598 else if (GET_MODE (mem) == HImode)
19599 fn = gen_load_lockedhi_si;
19600 else
19601 fn = gen_load_lockedsi;
19602 break;
19603 case DImode:
19604 fn = gen_load_lockeddi;
19605 break;
19606 case TImode:
19607 fn = gen_load_lockedti;
19608 break;
19609 default:
19610 gcc_unreachable ();
19612 emit_insn (fn (reg, mem));
19615 /* A subroutine of the atomic operation splitters. Emit a store-conditional
19616 instruction in MODE. */
19618 static void
19619 emit_store_conditional (enum machine_mode mode, rtx res, rtx mem, rtx val)
19621 rtx (*fn) (rtx, rtx, rtx) = NULL;
19623 switch (mode)
19625 case QImode:
19626 fn = gen_store_conditionalqi;
19627 break;
19628 case HImode:
19629 fn = gen_store_conditionalhi;
19630 break;
19631 case SImode:
19632 fn = gen_store_conditionalsi;
19633 break;
19634 case DImode:
19635 fn = gen_store_conditionaldi;
19636 break;
19637 case TImode:
19638 fn = gen_store_conditionalti;
19639 break;
19640 default:
19641 gcc_unreachable ();
19644 /* Emit sync before stwcx. to address PPC405 Erratum. */
19645 if (PPC405_ERRATUM77)
19646 emit_insn (gen_hwsync ());
19648 emit_insn (fn (res, mem, val));
19651 /* Expand barriers before and after a load_locked/store_cond sequence. */
19653 static rtx
19654 rs6000_pre_atomic_barrier (rtx mem, enum memmodel model)
19656 rtx addr = XEXP (mem, 0);
19657 int strict_p = (reload_in_progress || reload_completed);
19659 if (!legitimate_indirect_address_p (addr, strict_p)
19660 && !legitimate_indexed_address_p (addr, strict_p))
19662 addr = force_reg (Pmode, addr);
19663 mem = replace_equiv_address_nv (mem, addr);
19666 switch (model)
19668 case MEMMODEL_RELAXED:
19669 case MEMMODEL_CONSUME:
19670 case MEMMODEL_ACQUIRE:
19671 break;
19672 case MEMMODEL_RELEASE:
19673 case MEMMODEL_ACQ_REL:
19674 emit_insn (gen_lwsync ());
19675 break;
19676 case MEMMODEL_SEQ_CST:
19677 emit_insn (gen_hwsync ());
19678 break;
19679 default:
19680 gcc_unreachable ();
19682 return mem;
19685 static void
19686 rs6000_post_atomic_barrier (enum memmodel model)
19688 switch (model)
19690 case MEMMODEL_RELAXED:
19691 case MEMMODEL_CONSUME:
19692 case MEMMODEL_RELEASE:
19693 break;
19694 case MEMMODEL_ACQUIRE:
19695 case MEMMODEL_ACQ_REL:
19696 case MEMMODEL_SEQ_CST:
19697 emit_insn (gen_isync ());
19698 break;
19699 default:
19700 gcc_unreachable ();
19704 /* A subroutine of the various atomic expanders. For sub-word operations,
19705 we must adjust things to operate on SImode. Given the original MEM,
19706 return a new aligned memory. Also build and return the quantities by
19707 which to shift and mask. */
19709 static rtx
19710 rs6000_adjust_atomic_subword (rtx orig_mem, rtx *pshift, rtx *pmask)
19712 rtx addr, align, shift, mask, mem;
19713 HOST_WIDE_INT shift_mask;
19714 enum machine_mode mode = GET_MODE (orig_mem);
19716 /* For smaller modes, we have to implement this via SImode. */
19717 shift_mask = (mode == QImode ? 0x18 : 0x10);
19719 addr = XEXP (orig_mem, 0);
19720 addr = force_reg (GET_MODE (addr), addr);
19722 /* Aligned memory containing subword. Generate a new memory. We
19723 do not want any of the existing MEM_ATTR data, as we're now
19724 accessing memory outside the original object. */
19725 align = expand_simple_binop (Pmode, AND, addr, GEN_INT (-4),
19726 NULL_RTX, 1, OPTAB_LIB_WIDEN);
19727 mem = gen_rtx_MEM (SImode, align);
19728 MEM_VOLATILE_P (mem) = MEM_VOLATILE_P (orig_mem);
19729 if (MEM_ALIAS_SET (orig_mem) == ALIAS_SET_MEMORY_BARRIER)
19730 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
19732 /* Shift amount for subword relative to aligned word. */
19733 shift = gen_reg_rtx (SImode);
19734 addr = gen_lowpart (SImode, addr);
19735 emit_insn (gen_rlwinm (shift, addr, GEN_INT (3), GEN_INT (shift_mask)));
19736 if (WORDS_BIG_ENDIAN)
19737 shift = expand_simple_binop (SImode, XOR, shift, GEN_INT (shift_mask),
19738 shift, 1, OPTAB_LIB_WIDEN);
19739 *pshift = shift;
19741 /* Mask for insertion. */
19742 mask = expand_simple_binop (SImode, ASHIFT, GEN_INT (GET_MODE_MASK (mode)),
19743 shift, NULL_RTX, 1, OPTAB_LIB_WIDEN);
19744 *pmask = mask;
19746 return mem;
19749 /* A subroutine of the various atomic expanders. For sub-word operands,
19750 combine OLDVAL and NEWVAL via MASK. Returns a new pseduo. */
19752 static rtx
19753 rs6000_mask_atomic_subword (rtx oldval, rtx newval, rtx mask)
19755 rtx x;
19757 x = gen_reg_rtx (SImode);
19758 emit_insn (gen_rtx_SET (VOIDmode, x,
19759 gen_rtx_AND (SImode,
19760 gen_rtx_NOT (SImode, mask),
19761 oldval)));
19763 x = expand_simple_binop (SImode, IOR, newval, x, x, 1, OPTAB_LIB_WIDEN);
19765 return x;
19768 /* A subroutine of the various atomic expanders. For sub-word operands,
19769 extract WIDE to NARROW via SHIFT. */
19771 static void
19772 rs6000_finish_atomic_subword (rtx narrow, rtx wide, rtx shift)
19774 wide = expand_simple_binop (SImode, LSHIFTRT, wide, shift,
19775 wide, 1, OPTAB_LIB_WIDEN);
19776 emit_move_insn (narrow, gen_lowpart (GET_MODE (narrow), wide));
19779 /* Expand an atomic compare and swap operation. */
19781 void
19782 rs6000_expand_atomic_compare_and_swap (rtx operands[])
19784 rtx boolval, retval, mem, oldval, newval, cond;
19785 rtx label1, label2, x, mask, shift;
19786 enum machine_mode mode, orig_mode;
19787 enum memmodel mod_s, mod_f;
19788 bool is_weak;
19790 boolval = operands[0];
19791 retval = operands[1];
19792 mem = operands[2];
19793 oldval = operands[3];
19794 newval = operands[4];
19795 is_weak = (INTVAL (operands[5]) != 0);
19796 mod_s = (enum memmodel) INTVAL (operands[6]);
19797 mod_f = (enum memmodel) INTVAL (operands[7]);
19798 orig_mode = mode = GET_MODE (mem);
19800 mask = shift = NULL_RTX;
19801 if (mode == QImode || mode == HImode)
19803 /* Before power8, we didn't have access to lbarx/lharx, so generate a
19804 lwarx and shift/mask operations. With power8, we need to do the
19805 comparison in SImode, but the store is still done in QI/HImode. */
19806 oldval = convert_modes (SImode, mode, oldval, 1);
19808 if (!TARGET_SYNC_HI_QI)
19810 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
19812 /* Shift and mask OLDVAL into position with the word. */
19813 oldval = expand_simple_binop (SImode, ASHIFT, oldval, shift,
19814 NULL_RTX, 1, OPTAB_LIB_WIDEN);
19816 /* Shift and mask NEWVAL into position within the word. */
19817 newval = convert_modes (SImode, mode, newval, 1);
19818 newval = expand_simple_binop (SImode, ASHIFT, newval, shift,
19819 NULL_RTX, 1, OPTAB_LIB_WIDEN);
19822 /* Prepare to adjust the return value. */
19823 retval = gen_reg_rtx (SImode);
19824 mode = SImode;
19826 else if (reg_overlap_mentioned_p (retval, oldval))
19827 oldval = copy_to_reg (oldval);
19829 mem = rs6000_pre_atomic_barrier (mem, mod_s);
19831 label1 = NULL_RTX;
19832 if (!is_weak)
19834 label1 = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
19835 emit_label (XEXP (label1, 0));
19837 label2 = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
19839 emit_load_locked (mode, retval, mem);
19841 x = retval;
19842 if (mask)
19844 x = expand_simple_binop (SImode, AND, retval, mask,
19845 NULL_RTX, 1, OPTAB_LIB_WIDEN);
19848 cond = gen_reg_rtx (CCmode);
19849 /* If we have TImode, synthesize a comparison. */
19850 if (mode != TImode)
19851 x = gen_rtx_COMPARE (CCmode, x, oldval);
19852 else
19854 rtx xor1_result = gen_reg_rtx (DImode);
19855 rtx xor2_result = gen_reg_rtx (DImode);
19856 rtx or_result = gen_reg_rtx (DImode);
19857 rtx new_word0 = simplify_gen_subreg (DImode, x, TImode, 0);
19858 rtx new_word1 = simplify_gen_subreg (DImode, x, TImode, 8);
19859 rtx old_word0 = simplify_gen_subreg (DImode, oldval, TImode, 0);
19860 rtx old_word1 = simplify_gen_subreg (DImode, oldval, TImode, 8);
19862 emit_insn (gen_xordi3 (xor1_result, new_word0, old_word0));
19863 emit_insn (gen_xordi3 (xor2_result, new_word1, old_word1));
19864 emit_insn (gen_iordi3 (or_result, xor1_result, xor2_result));
19865 x = gen_rtx_COMPARE (CCmode, or_result, const0_rtx);
19868 emit_insn (gen_rtx_SET (VOIDmode, cond, x));
19870 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
19871 emit_unlikely_jump (x, label2);
19873 x = newval;
19874 if (mask)
19875 x = rs6000_mask_atomic_subword (retval, newval, mask);
19877 emit_store_conditional (orig_mode, cond, mem, x);
19879 if (!is_weak)
19881 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
19882 emit_unlikely_jump (x, label1);
19885 if (mod_f != MEMMODEL_RELAXED)
19886 emit_label (XEXP (label2, 0));
19888 rs6000_post_atomic_barrier (mod_s);
19890 if (mod_f == MEMMODEL_RELAXED)
19891 emit_label (XEXP (label2, 0));
19893 if (shift)
19894 rs6000_finish_atomic_subword (operands[1], retval, shift);
19895 else if (mode != GET_MODE (operands[1]))
19896 convert_move (operands[1], retval, 1);
19898 /* In all cases, CR0 contains EQ on success, and NE on failure. */
19899 x = gen_rtx_EQ (SImode, cond, const0_rtx);
19900 emit_insn (gen_rtx_SET (VOIDmode, boolval, x));
19903 /* Expand an atomic exchange operation. */
19905 void
19906 rs6000_expand_atomic_exchange (rtx operands[])
19908 rtx retval, mem, val, cond;
19909 enum machine_mode mode;
19910 enum memmodel model;
19911 rtx label, x, mask, shift;
19913 retval = operands[0];
19914 mem = operands[1];
19915 val = operands[2];
19916 model = (enum memmodel) INTVAL (operands[3]);
19917 mode = GET_MODE (mem);
19919 mask = shift = NULL_RTX;
19920 if (!TARGET_SYNC_HI_QI && (mode == QImode || mode == HImode))
19922 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
19924 /* Shift and mask VAL into position with the word. */
19925 val = convert_modes (SImode, mode, val, 1);
19926 val = expand_simple_binop (SImode, ASHIFT, val, shift,
19927 NULL_RTX, 1, OPTAB_LIB_WIDEN);
19929 /* Prepare to adjust the return value. */
19930 retval = gen_reg_rtx (SImode);
19931 mode = SImode;
19934 mem = rs6000_pre_atomic_barrier (mem, model);
19936 label = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
19937 emit_label (XEXP (label, 0));
19939 emit_load_locked (mode, retval, mem);
19941 x = val;
19942 if (mask)
19943 x = rs6000_mask_atomic_subword (retval, val, mask);
19945 cond = gen_reg_rtx (CCmode);
19946 emit_store_conditional (mode, cond, mem, x);
19948 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
19949 emit_unlikely_jump (x, label);
19951 rs6000_post_atomic_barrier (model);
19953 if (shift)
19954 rs6000_finish_atomic_subword (operands[0], retval, shift);
19957 /* Expand an atomic fetch-and-operate pattern. CODE is the binary operation
19958 to perform. MEM is the memory on which to operate. VAL is the second
19959 operand of the binary operator. BEFORE and AFTER are optional locations to
19960 return the value of MEM either before of after the operation. MODEL_RTX
19961 is a CONST_INT containing the memory model to use. */
19963 void
19964 rs6000_expand_atomic_op (enum rtx_code code, rtx mem, rtx val,
19965 rtx orig_before, rtx orig_after, rtx model_rtx)
19967 enum memmodel model = (enum memmodel) INTVAL (model_rtx);
19968 enum machine_mode mode = GET_MODE (mem);
19969 enum machine_mode store_mode = mode;
19970 rtx label, x, cond, mask, shift;
19971 rtx before = orig_before, after = orig_after;
19973 mask = shift = NULL_RTX;
19974 /* On power8, we want to use SImode for the operation. On previous systems,
19975 use the operation in a subword and shift/mask to get the proper byte or
19976 halfword. */
19977 if (mode == QImode || mode == HImode)
19979 if (TARGET_SYNC_HI_QI)
19981 val = convert_modes (SImode, mode, val, 1);
19983 /* Prepare to adjust the return value. */
19984 before = gen_reg_rtx (SImode);
19985 if (after)
19986 after = gen_reg_rtx (SImode);
19987 mode = SImode;
19989 else
19991 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
19993 /* Shift and mask VAL into position with the word. */
19994 val = convert_modes (SImode, mode, val, 1);
19995 val = expand_simple_binop (SImode, ASHIFT, val, shift,
19996 NULL_RTX, 1, OPTAB_LIB_WIDEN);
19998 switch (code)
20000 case IOR:
20001 case XOR:
20002 /* We've already zero-extended VAL. That is sufficient to
20003 make certain that it does not affect other bits. */
20004 mask = NULL;
20005 break;
20007 case AND:
20008 /* If we make certain that all of the other bits in VAL are
20009 set, that will be sufficient to not affect other bits. */
20010 x = gen_rtx_NOT (SImode, mask);
20011 x = gen_rtx_IOR (SImode, x, val);
20012 emit_insn (gen_rtx_SET (VOIDmode, val, x));
20013 mask = NULL;
20014 break;
20016 case NOT:
20017 case PLUS:
20018 case MINUS:
20019 /* These will all affect bits outside the field and need
20020 adjustment via MASK within the loop. */
20021 break;
20023 default:
20024 gcc_unreachable ();
20027 /* Prepare to adjust the return value. */
20028 before = gen_reg_rtx (SImode);
20029 if (after)
20030 after = gen_reg_rtx (SImode);
20031 store_mode = mode = SImode;
20035 mem = rs6000_pre_atomic_barrier (mem, model);
20037 label = gen_label_rtx ();
20038 emit_label (label);
20039 label = gen_rtx_LABEL_REF (VOIDmode, label);
20041 if (before == NULL_RTX)
20042 before = gen_reg_rtx (mode);
20044 emit_load_locked (mode, before, mem);
20046 if (code == NOT)
20048 x = expand_simple_binop (mode, AND, before, val,
20049 NULL_RTX, 1, OPTAB_LIB_WIDEN);
20050 after = expand_simple_unop (mode, NOT, x, after, 1);
20052 else
20054 after = expand_simple_binop (mode, code, before, val,
20055 after, 1, OPTAB_LIB_WIDEN);
20058 x = after;
20059 if (mask)
20061 x = expand_simple_binop (SImode, AND, after, mask,
20062 NULL_RTX, 1, OPTAB_LIB_WIDEN);
20063 x = rs6000_mask_atomic_subword (before, x, mask);
20065 else if (store_mode != mode)
20066 x = convert_modes (store_mode, mode, x, 1);
20068 cond = gen_reg_rtx (CCmode);
20069 emit_store_conditional (store_mode, cond, mem, x);
20071 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
20072 emit_unlikely_jump (x, label);
20074 rs6000_post_atomic_barrier (model);
20076 if (shift)
20078 /* QImode/HImode on machines without lbarx/lharx where we do a lwarx and
20079 then do the calcuations in a SImode register. */
20080 if (orig_before)
20081 rs6000_finish_atomic_subword (orig_before, before, shift);
20082 if (orig_after)
20083 rs6000_finish_atomic_subword (orig_after, after, shift);
20085 else if (store_mode != mode)
20087 /* QImode/HImode on machines with lbarx/lharx where we do the native
20088 operation and then do the calcuations in a SImode register. */
20089 if (orig_before)
20090 convert_move (orig_before, before, 1);
20091 if (orig_after)
20092 convert_move (orig_after, after, 1);
20094 else if (orig_after && after != orig_after)
20095 emit_move_insn (orig_after, after);
20098 /* Emit instructions to move SRC to DST. Called by splitters for
20099 multi-register moves. It will emit at most one instruction for
20100 each register that is accessed; that is, it won't emit li/lis pairs
20101 (or equivalent for 64-bit code). One of SRC or DST must be a hard
20102 register. */
20104 void
20105 rs6000_split_multireg_move (rtx dst, rtx src)
20107 /* The register number of the first register being moved. */
20108 int reg;
20109 /* The mode that is to be moved. */
20110 enum machine_mode mode;
20111 /* The mode that the move is being done in, and its size. */
20112 enum machine_mode reg_mode;
20113 int reg_mode_size;
20114 /* The number of registers that will be moved. */
20115 int nregs;
20117 reg = REG_P (dst) ? REGNO (dst) : REGNO (src);
20118 mode = GET_MODE (dst);
20119 nregs = hard_regno_nregs[reg][mode];
20120 if (FP_REGNO_P (reg))
20121 reg_mode = DECIMAL_FLOAT_MODE_P (mode) ? DDmode :
20122 ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? DFmode : SFmode);
20123 else if (ALTIVEC_REGNO_P (reg))
20124 reg_mode = V16QImode;
20125 else if (TARGET_E500_DOUBLE && mode == TFmode)
20126 reg_mode = DFmode;
20127 else
20128 reg_mode = word_mode;
20129 reg_mode_size = GET_MODE_SIZE (reg_mode);
20131 gcc_assert (reg_mode_size * nregs == GET_MODE_SIZE (mode));
20133 /* TDmode residing in FP registers is special, since the ISA requires that
20134 the lower-numbered word of a register pair is always the most significant
20135 word, even in little-endian mode. This does not match the usual subreg
20136 semantics, so we cannnot use simplify_gen_subreg in those cases. Access
20137 the appropriate constituent registers "by hand" in little-endian mode.
20139 Note we do not need to check for destructive overlap here since TDmode
20140 can only reside in even/odd register pairs. */
20141 if (FP_REGNO_P (reg) && DECIMAL_FLOAT_MODE_P (mode) && !BYTES_BIG_ENDIAN)
20143 rtx p_src, p_dst;
20144 int i;
20146 for (i = 0; i < nregs; i++)
20148 if (REG_P (src) && FP_REGNO_P (REGNO (src)))
20149 p_src = gen_rtx_REG (reg_mode, REGNO (src) + nregs - 1 - i);
20150 else
20151 p_src = simplify_gen_subreg (reg_mode, src, mode,
20152 i * reg_mode_size);
20154 if (REG_P (dst) && FP_REGNO_P (REGNO (dst)))
20155 p_dst = gen_rtx_REG (reg_mode, REGNO (dst) + nregs - 1 - i);
20156 else
20157 p_dst = simplify_gen_subreg (reg_mode, dst, mode,
20158 i * reg_mode_size);
20160 emit_insn (gen_rtx_SET (VOIDmode, p_dst, p_src));
20163 return;
20166 if (REG_P (src) && REG_P (dst) && (REGNO (src) < REGNO (dst)))
20168 /* Move register range backwards, if we might have destructive
20169 overlap. */
20170 int i;
20171 for (i = nregs - 1; i >= 0; i--)
20172 emit_insn (gen_rtx_SET (VOIDmode,
20173 simplify_gen_subreg (reg_mode, dst, mode,
20174 i * reg_mode_size),
20175 simplify_gen_subreg (reg_mode, src, mode,
20176 i * reg_mode_size)));
20178 else
20180 int i;
20181 int j = -1;
20182 bool used_update = false;
20183 rtx restore_basereg = NULL_RTX;
20185 if (MEM_P (src) && INT_REGNO_P (reg))
20187 rtx breg;
20189 if (GET_CODE (XEXP (src, 0)) == PRE_INC
20190 || GET_CODE (XEXP (src, 0)) == PRE_DEC)
20192 rtx delta_rtx;
20193 breg = XEXP (XEXP (src, 0), 0);
20194 delta_rtx = (GET_CODE (XEXP (src, 0)) == PRE_INC
20195 ? GEN_INT (GET_MODE_SIZE (GET_MODE (src)))
20196 : GEN_INT (-GET_MODE_SIZE (GET_MODE (src))));
20197 emit_insn (gen_add3_insn (breg, breg, delta_rtx));
20198 src = replace_equiv_address (src, breg);
20200 else if (! rs6000_offsettable_memref_p (src, reg_mode))
20202 if (GET_CODE (XEXP (src, 0)) == PRE_MODIFY)
20204 rtx basereg = XEXP (XEXP (src, 0), 0);
20205 if (TARGET_UPDATE)
20207 rtx ndst = simplify_gen_subreg (reg_mode, dst, mode, 0);
20208 emit_insn (gen_rtx_SET (VOIDmode, ndst,
20209 gen_rtx_MEM (reg_mode, XEXP (src, 0))));
20210 used_update = true;
20212 else
20213 emit_insn (gen_rtx_SET (VOIDmode, basereg,
20214 XEXP (XEXP (src, 0), 1)));
20215 src = replace_equiv_address (src, basereg);
20217 else
20219 rtx basereg = gen_rtx_REG (Pmode, reg);
20220 emit_insn (gen_rtx_SET (VOIDmode, basereg, XEXP (src, 0)));
20221 src = replace_equiv_address (src, basereg);
20225 breg = XEXP (src, 0);
20226 if (GET_CODE (breg) == PLUS || GET_CODE (breg) == LO_SUM)
20227 breg = XEXP (breg, 0);
20229 /* If the base register we are using to address memory is
20230 also a destination reg, then change that register last. */
20231 if (REG_P (breg)
20232 && REGNO (breg) >= REGNO (dst)
20233 && REGNO (breg) < REGNO (dst) + nregs)
20234 j = REGNO (breg) - REGNO (dst);
20236 else if (MEM_P (dst) && INT_REGNO_P (reg))
20238 rtx breg;
20240 if (GET_CODE (XEXP (dst, 0)) == PRE_INC
20241 || GET_CODE (XEXP (dst, 0)) == PRE_DEC)
20243 rtx delta_rtx;
20244 breg = XEXP (XEXP (dst, 0), 0);
20245 delta_rtx = (GET_CODE (XEXP (dst, 0)) == PRE_INC
20246 ? GEN_INT (GET_MODE_SIZE (GET_MODE (dst)))
20247 : GEN_INT (-GET_MODE_SIZE (GET_MODE (dst))));
20249 /* We have to update the breg before doing the store.
20250 Use store with update, if available. */
20252 if (TARGET_UPDATE)
20254 rtx nsrc = simplify_gen_subreg (reg_mode, src, mode, 0);
20255 emit_insn (TARGET_32BIT
20256 ? (TARGET_POWERPC64
20257 ? gen_movdi_si_update (breg, breg, delta_rtx, nsrc)
20258 : gen_movsi_update (breg, breg, delta_rtx, nsrc))
20259 : gen_movdi_di_update (breg, breg, delta_rtx, nsrc));
20260 used_update = true;
20262 else
20263 emit_insn (gen_add3_insn (breg, breg, delta_rtx));
20264 dst = replace_equiv_address (dst, breg);
20266 else if (!rs6000_offsettable_memref_p (dst, reg_mode)
20267 && GET_CODE (XEXP (dst, 0)) != LO_SUM)
20269 if (GET_CODE (XEXP (dst, 0)) == PRE_MODIFY)
20271 rtx basereg = XEXP (XEXP (dst, 0), 0);
20272 if (TARGET_UPDATE)
20274 rtx nsrc = simplify_gen_subreg (reg_mode, src, mode, 0);
20275 emit_insn (gen_rtx_SET (VOIDmode,
20276 gen_rtx_MEM (reg_mode, XEXP (dst, 0)), nsrc));
20277 used_update = true;
20279 else
20280 emit_insn (gen_rtx_SET (VOIDmode, basereg,
20281 XEXP (XEXP (dst, 0), 1)));
20282 dst = replace_equiv_address (dst, basereg);
20284 else
20286 rtx basereg = XEXP (XEXP (dst, 0), 0);
20287 rtx offsetreg = XEXP (XEXP (dst, 0), 1);
20288 gcc_assert (GET_CODE (XEXP (dst, 0)) == PLUS
20289 && REG_P (basereg)
20290 && REG_P (offsetreg)
20291 && REGNO (basereg) != REGNO (offsetreg));
20292 if (REGNO (basereg) == 0)
20294 rtx tmp = offsetreg;
20295 offsetreg = basereg;
20296 basereg = tmp;
20298 emit_insn (gen_add3_insn (basereg, basereg, offsetreg));
20299 restore_basereg = gen_sub3_insn (basereg, basereg, offsetreg);
20300 dst = replace_equiv_address (dst, basereg);
20303 else if (GET_CODE (XEXP (dst, 0)) != LO_SUM)
20304 gcc_assert (rs6000_offsettable_memref_p (dst, reg_mode));
20307 for (i = 0; i < nregs; i++)
20309 /* Calculate index to next subword. */
20310 ++j;
20311 if (j == nregs)
20312 j = 0;
20314 /* If compiler already emitted move of first word by
20315 store with update, no need to do anything. */
20316 if (j == 0 && used_update)
20317 continue;
20319 emit_insn (gen_rtx_SET (VOIDmode,
20320 simplify_gen_subreg (reg_mode, dst, mode,
20321 j * reg_mode_size),
20322 simplify_gen_subreg (reg_mode, src, mode,
20323 j * reg_mode_size)));
20325 if (restore_basereg != NULL_RTX)
20326 emit_insn (restore_basereg);
20331 /* This page contains routines that are used to determine what the
20332 function prologue and epilogue code will do and write them out. */
20334 static inline bool
20335 save_reg_p (int r)
20337 return !call_used_regs[r] && df_regs_ever_live_p (r);
20340 /* Return the first fixed-point register that is required to be
20341 saved. 32 if none. */
20344 first_reg_to_save (void)
20346 int first_reg;
20348 /* Find lowest numbered live register. */
20349 for (first_reg = 13; first_reg <= 31; first_reg++)
20350 if (save_reg_p (first_reg))
20351 break;
20353 if (first_reg > RS6000_PIC_OFFSET_TABLE_REGNUM
20354 && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
20355 || (DEFAULT_ABI == ABI_DARWIN && flag_pic)
20356 || (TARGET_TOC && TARGET_MINIMAL_TOC))
20357 && df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))
20358 first_reg = RS6000_PIC_OFFSET_TABLE_REGNUM;
20360 #if TARGET_MACHO
20361 if (flag_pic
20362 && crtl->uses_pic_offset_table
20363 && first_reg > RS6000_PIC_OFFSET_TABLE_REGNUM)
20364 return RS6000_PIC_OFFSET_TABLE_REGNUM;
20365 #endif
20367 return first_reg;
20370 /* Similar, for FP regs. */
20373 first_fp_reg_to_save (void)
20375 int first_reg;
20377 /* Find lowest numbered live register. */
20378 for (first_reg = 14 + 32; first_reg <= 63; first_reg++)
20379 if (save_reg_p (first_reg))
20380 break;
20382 return first_reg;
20385 /* Similar, for AltiVec regs. */
20387 static int
20388 first_altivec_reg_to_save (void)
20390 int i;
20392 /* Stack frame remains as is unless we are in AltiVec ABI. */
20393 if (! TARGET_ALTIVEC_ABI)
20394 return LAST_ALTIVEC_REGNO + 1;
20396 /* On Darwin, the unwind routines are compiled without
20397 TARGET_ALTIVEC, and use save_world to save/restore the
20398 altivec registers when necessary. */
20399 if (DEFAULT_ABI == ABI_DARWIN && crtl->calls_eh_return
20400 && ! TARGET_ALTIVEC)
20401 return FIRST_ALTIVEC_REGNO + 20;
20403 /* Find lowest numbered live register. */
20404 for (i = FIRST_ALTIVEC_REGNO + 20; i <= LAST_ALTIVEC_REGNO; ++i)
20405 if (save_reg_p (i))
20406 break;
20408 return i;
20411 /* Return a 32-bit mask of the AltiVec registers we need to set in
20412 VRSAVE. Bit n of the return value is 1 if Vn is live. The MSB in
20413 the 32-bit word is 0. */
20415 static unsigned int
20416 compute_vrsave_mask (void)
20418 unsigned int i, mask = 0;
20420 /* On Darwin, the unwind routines are compiled without
20421 TARGET_ALTIVEC, and use save_world to save/restore the
20422 call-saved altivec registers when necessary. */
20423 if (DEFAULT_ABI == ABI_DARWIN && crtl->calls_eh_return
20424 && ! TARGET_ALTIVEC)
20425 mask |= 0xFFF;
20427 /* First, find out if we use _any_ altivec registers. */
20428 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
20429 if (df_regs_ever_live_p (i))
20430 mask |= ALTIVEC_REG_BIT (i);
20432 if (mask == 0)
20433 return mask;
20435 /* Next, remove the argument registers from the set. These must
20436 be in the VRSAVE mask set by the caller, so we don't need to add
20437 them in again. More importantly, the mask we compute here is
20438 used to generate CLOBBERs in the set_vrsave insn, and we do not
20439 wish the argument registers to die. */
20440 for (i = crtl->args.info.vregno - 1; i >= ALTIVEC_ARG_MIN_REG; --i)
20441 mask &= ~ALTIVEC_REG_BIT (i);
20443 /* Similarly, remove the return value from the set. */
20445 bool yes = false;
20446 diddle_return_value (is_altivec_return_reg, &yes);
20447 if (yes)
20448 mask &= ~ALTIVEC_REG_BIT (ALTIVEC_ARG_RETURN);
20451 return mask;
20454 /* For a very restricted set of circumstances, we can cut down the
20455 size of prologues/epilogues by calling our own save/restore-the-world
20456 routines. */
20458 static void
20459 compute_save_world_info (rs6000_stack_t *info_ptr)
20461 info_ptr->world_save_p = 1;
20462 info_ptr->world_save_p
20463 = (WORLD_SAVE_P (info_ptr)
20464 && DEFAULT_ABI == ABI_DARWIN
20465 && !cfun->has_nonlocal_label
20466 && info_ptr->first_fp_reg_save == FIRST_SAVED_FP_REGNO
20467 && info_ptr->first_gp_reg_save == FIRST_SAVED_GP_REGNO
20468 && info_ptr->first_altivec_reg_save == FIRST_SAVED_ALTIVEC_REGNO
20469 && info_ptr->cr_save_p);
20471 /* This will not work in conjunction with sibcalls. Make sure there
20472 are none. (This check is expensive, but seldom executed.) */
20473 if (WORLD_SAVE_P (info_ptr))
20475 rtx insn;
20476 for (insn = get_last_insn_anywhere (); insn; insn = PREV_INSN (insn))
20477 if (CALL_P (insn) && SIBLING_CALL_P (insn))
20479 info_ptr->world_save_p = 0;
20480 break;
20484 if (WORLD_SAVE_P (info_ptr))
20486 /* Even if we're not touching VRsave, make sure there's room on the
20487 stack for it, if it looks like we're calling SAVE_WORLD, which
20488 will attempt to save it. */
20489 info_ptr->vrsave_size = 4;
20491 /* If we are going to save the world, we need to save the link register too. */
20492 info_ptr->lr_save_p = 1;
20494 /* "Save" the VRsave register too if we're saving the world. */
20495 if (info_ptr->vrsave_mask == 0)
20496 info_ptr->vrsave_mask = compute_vrsave_mask ();
20498 /* Because the Darwin register save/restore routines only handle
20499 F14 .. F31 and V20 .. V31 as per the ABI, perform a consistency
20500 check. */
20501 gcc_assert (info_ptr->first_fp_reg_save >= FIRST_SAVED_FP_REGNO
20502 && (info_ptr->first_altivec_reg_save
20503 >= FIRST_SAVED_ALTIVEC_REGNO));
20505 return;
20509 static void
20510 is_altivec_return_reg (rtx reg, void *xyes)
20512 bool *yes = (bool *) xyes;
20513 if (REGNO (reg) == ALTIVEC_ARG_RETURN)
20514 *yes = true;
20518 /* Look for user-defined global regs in the range FIRST to LAST-1.
20519 We should not restore these, and so cannot use lmw or out-of-line
20520 restore functions if there are any. We also can't save them
20521 (well, emit frame notes for them), because frame unwinding during
20522 exception handling will restore saved registers. */
20524 static bool
20525 global_regs_p (unsigned first, unsigned last)
20527 while (first < last)
20528 if (global_regs[first++])
20529 return true;
20530 return false;
20533 /* Determine the strategy for savings/restoring registers. */
20535 enum {
20536 SAVRES_MULTIPLE = 0x1,
20537 SAVE_INLINE_FPRS = 0x2,
20538 SAVE_INLINE_GPRS = 0x4,
20539 REST_INLINE_FPRS = 0x8,
20540 REST_INLINE_GPRS = 0x10,
20541 SAVE_NOINLINE_GPRS_SAVES_LR = 0x20,
20542 SAVE_NOINLINE_FPRS_SAVES_LR = 0x40,
20543 REST_NOINLINE_FPRS_DOESNT_RESTORE_LR = 0x80,
20544 SAVE_INLINE_VRS = 0x100,
20545 REST_INLINE_VRS = 0x200
20548 static int
20549 rs6000_savres_strategy (rs6000_stack_t *info,
20550 bool using_static_chain_p)
20552 int strategy = 0;
20553 bool lr_save_p;
20555 if (TARGET_MULTIPLE
20556 && !TARGET_POWERPC64
20557 && !(TARGET_SPE_ABI && info->spe_64bit_regs_used)
20558 && info->first_gp_reg_save < 31
20559 && !global_regs_p (info->first_gp_reg_save, 32))
20560 strategy |= SAVRES_MULTIPLE;
20562 if (crtl->calls_eh_return
20563 || cfun->machine->ra_need_lr)
20564 strategy |= (SAVE_INLINE_FPRS | REST_INLINE_FPRS
20565 | SAVE_INLINE_GPRS | REST_INLINE_GPRS
20566 | SAVE_INLINE_VRS | REST_INLINE_VRS);
20568 if (info->first_fp_reg_save == 64
20569 /* The out-of-line FP routines use double-precision stores;
20570 we can't use those routines if we don't have such stores. */
20571 || (TARGET_HARD_FLOAT && !TARGET_DOUBLE_FLOAT)
20572 || global_regs_p (info->first_fp_reg_save, 64))
20573 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
20575 if (info->first_gp_reg_save == 32
20576 || (!(strategy & SAVRES_MULTIPLE)
20577 && global_regs_p (info->first_gp_reg_save, 32)))
20578 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
20580 if (info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1
20581 || global_regs_p (info->first_altivec_reg_save, LAST_ALTIVEC_REGNO + 1))
20582 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
20584 /* Define cutoff for using out-of-line functions to save registers. */
20585 if (DEFAULT_ABI == ABI_V4 || TARGET_ELF)
20587 if (!optimize_size)
20589 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
20590 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
20591 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
20593 else
20595 /* Prefer out-of-line restore if it will exit. */
20596 if (info->first_fp_reg_save > 61)
20597 strategy |= SAVE_INLINE_FPRS;
20598 if (info->first_gp_reg_save > 29)
20600 if (info->first_fp_reg_save == 64)
20601 strategy |= SAVE_INLINE_GPRS;
20602 else
20603 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
20605 if (info->first_altivec_reg_save == LAST_ALTIVEC_REGNO)
20606 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
20609 else if (DEFAULT_ABI == ABI_DARWIN)
20611 if (info->first_fp_reg_save > 60)
20612 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
20613 if (info->first_gp_reg_save > 29)
20614 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
20615 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
20617 else
20619 gcc_checking_assert (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2);
20620 if (info->first_fp_reg_save > 61)
20621 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
20622 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
20623 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
20626 /* Don't bother to try to save things out-of-line if r11 is occupied
20627 by the static chain. It would require too much fiddling and the
20628 static chain is rarely used anyway. FPRs are saved w.r.t the stack
20629 pointer on Darwin, and AIX uses r1 or r12. */
20630 if (using_static_chain_p
20631 && (DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN))
20632 strategy |= ((DEFAULT_ABI == ABI_DARWIN ? 0 : SAVE_INLINE_FPRS)
20633 | SAVE_INLINE_GPRS
20634 | SAVE_INLINE_VRS | REST_INLINE_VRS);
20636 /* We can only use the out-of-line routines to restore if we've
20637 saved all the registers from first_fp_reg_save in the prologue.
20638 Otherwise, we risk loading garbage. */
20639 if ((strategy & (SAVE_INLINE_FPRS | REST_INLINE_FPRS)) == SAVE_INLINE_FPRS)
20641 int i;
20643 for (i = info->first_fp_reg_save; i < 64; i++)
20644 if (!save_reg_p (i))
20646 strategy |= REST_INLINE_FPRS;
20647 break;
20651 /* If we are going to use store multiple, then don't even bother
20652 with the out-of-line routines, since the store-multiple
20653 instruction will always be smaller. */
20654 if ((strategy & SAVRES_MULTIPLE))
20655 strategy |= SAVE_INLINE_GPRS;
20657 /* info->lr_save_p isn't yet set if the only reason lr needs to be
20658 saved is an out-of-line save or restore. Set up the value for
20659 the next test (excluding out-of-line gpr restore). */
20660 lr_save_p = (info->lr_save_p
20661 || !(strategy & SAVE_INLINE_GPRS)
20662 || !(strategy & SAVE_INLINE_FPRS)
20663 || !(strategy & SAVE_INLINE_VRS)
20664 || !(strategy & REST_INLINE_FPRS)
20665 || !(strategy & REST_INLINE_VRS));
20667 /* The situation is more complicated with load multiple. We'd
20668 prefer to use the out-of-line routines for restores, since the
20669 "exit" out-of-line routines can handle the restore of LR and the
20670 frame teardown. However if doesn't make sense to use the
20671 out-of-line routine if that is the only reason we'd need to save
20672 LR, and we can't use the "exit" out-of-line gpr restore if we
20673 have saved some fprs; In those cases it is advantageous to use
20674 load multiple when available. */
20675 if ((strategy & SAVRES_MULTIPLE)
20676 && (!lr_save_p
20677 || info->first_fp_reg_save != 64))
20678 strategy |= REST_INLINE_GPRS;
20680 /* Saving CR interferes with the exit routines used on the SPE, so
20681 just punt here. */
20682 if (TARGET_SPE_ABI
20683 && info->spe_64bit_regs_used
20684 && info->cr_save_p)
20685 strategy |= REST_INLINE_GPRS;
20687 /* We can only use load multiple or the out-of-line routines to
20688 restore if we've used store multiple or out-of-line routines
20689 in the prologue, i.e. if we've saved all the registers from
20690 first_gp_reg_save. Otherwise, we risk loading garbage. */
20691 if ((strategy & (SAVE_INLINE_GPRS | REST_INLINE_GPRS | SAVRES_MULTIPLE))
20692 == SAVE_INLINE_GPRS)
20694 int i;
20696 for (i = info->first_gp_reg_save; i < 32; i++)
20697 if (!save_reg_p (i))
20699 strategy |= REST_INLINE_GPRS;
20700 break;
20704 if (TARGET_ELF && TARGET_64BIT)
20706 if (!(strategy & SAVE_INLINE_FPRS))
20707 strategy |= SAVE_NOINLINE_FPRS_SAVES_LR;
20708 else if (!(strategy & SAVE_INLINE_GPRS)
20709 && info->first_fp_reg_save == 64)
20710 strategy |= SAVE_NOINLINE_GPRS_SAVES_LR;
20712 else if (TARGET_AIX && !(strategy & REST_INLINE_FPRS))
20713 strategy |= REST_NOINLINE_FPRS_DOESNT_RESTORE_LR;
20715 if (TARGET_MACHO && !(strategy & SAVE_INLINE_FPRS))
20716 strategy |= SAVE_NOINLINE_FPRS_SAVES_LR;
20718 return strategy;
20721 /* Calculate the stack information for the current function. This is
20722 complicated by having two separate calling sequences, the AIX calling
20723 sequence and the V.4 calling sequence.
20725 AIX (and Darwin/Mac OS X) stack frames look like:
20726 32-bit 64-bit
20727 SP----> +---------------------------------------+
20728 | back chain to caller | 0 0
20729 +---------------------------------------+
20730 | saved CR | 4 8 (8-11)
20731 +---------------------------------------+
20732 | saved LR | 8 16
20733 +---------------------------------------+
20734 | reserved for compilers | 12 24
20735 +---------------------------------------+
20736 | reserved for binders | 16 32
20737 +---------------------------------------+
20738 | saved TOC pointer | 20 40
20739 +---------------------------------------+
20740 | Parameter save area (P) | 24 48
20741 +---------------------------------------+
20742 | Alloca space (A) | 24+P etc.
20743 +---------------------------------------+
20744 | Local variable space (L) | 24+P+A
20745 +---------------------------------------+
20746 | Float/int conversion temporary (X) | 24+P+A+L
20747 +---------------------------------------+
20748 | Save area for AltiVec registers (W) | 24+P+A+L+X
20749 +---------------------------------------+
20750 | AltiVec alignment padding (Y) | 24+P+A+L+X+W
20751 +---------------------------------------+
20752 | Save area for VRSAVE register (Z) | 24+P+A+L+X+W+Y
20753 +---------------------------------------+
20754 | Save area for GP registers (G) | 24+P+A+X+L+X+W+Y+Z
20755 +---------------------------------------+
20756 | Save area for FP registers (F) | 24+P+A+X+L+X+W+Y+Z+G
20757 +---------------------------------------+
20758 old SP->| back chain to caller's caller |
20759 +---------------------------------------+
20761 The required alignment for AIX configurations is two words (i.e., 8
20762 or 16 bytes).
20764 The ELFv2 ABI is a variant of the AIX ABI. Stack frames look like:
20766 SP----> +---------------------------------------+
20767 | Back chain to caller | 0
20768 +---------------------------------------+
20769 | Save area for CR | 8
20770 +---------------------------------------+
20771 | Saved LR | 16
20772 +---------------------------------------+
20773 | Saved TOC pointer | 24
20774 +---------------------------------------+
20775 | Parameter save area (P) | 32
20776 +---------------------------------------+
20777 | Alloca space (A) | 32+P
20778 +---------------------------------------+
20779 | Local variable space (L) | 32+P+A
20780 +---------------------------------------+
20781 | Save area for AltiVec registers (W) | 32+P+A+L
20782 +---------------------------------------+
20783 | AltiVec alignment padding (Y) | 32+P+A+L+W
20784 +---------------------------------------+
20785 | Save area for GP registers (G) | 32+P+A+L+W+Y
20786 +---------------------------------------+
20787 | Save area for FP registers (F) | 32+P+A+L+W+Y+G
20788 +---------------------------------------+
20789 old SP->| back chain to caller's caller | 32+P+A+L+W+Y+G+F
20790 +---------------------------------------+
20793 V.4 stack frames look like:
20795 SP----> +---------------------------------------+
20796 | back chain to caller | 0
20797 +---------------------------------------+
20798 | caller's saved LR | 4
20799 +---------------------------------------+
20800 | Parameter save area (P) | 8
20801 +---------------------------------------+
20802 | Alloca space (A) | 8+P
20803 +---------------------------------------+
20804 | Varargs save area (V) | 8+P+A
20805 +---------------------------------------+
20806 | Local variable space (L) | 8+P+A+V
20807 +---------------------------------------+
20808 | Float/int conversion temporary (X) | 8+P+A+V+L
20809 +---------------------------------------+
20810 | Save area for AltiVec registers (W) | 8+P+A+V+L+X
20811 +---------------------------------------+
20812 | AltiVec alignment padding (Y) | 8+P+A+V+L+X+W
20813 +---------------------------------------+
20814 | Save area for VRSAVE register (Z) | 8+P+A+V+L+X+W+Y
20815 +---------------------------------------+
20816 | SPE: area for 64-bit GP registers |
20817 +---------------------------------------+
20818 | SPE alignment padding |
20819 +---------------------------------------+
20820 | saved CR (C) | 8+P+A+V+L+X+W+Y+Z
20821 +---------------------------------------+
20822 | Save area for GP registers (G) | 8+P+A+V+L+X+W+Y+Z+C
20823 +---------------------------------------+
20824 | Save area for FP registers (F) | 8+P+A+V+L+X+W+Y+Z+C+G
20825 +---------------------------------------+
20826 old SP->| back chain to caller's caller |
20827 +---------------------------------------+
20829 The required alignment for V.4 is 16 bytes, or 8 bytes if -meabi is
20830 given. (But note below and in sysv4.h that we require only 8 and
20831 may round up the size of our stack frame anyways. The historical
20832 reason is early versions of powerpc-linux which didn't properly
20833 align the stack at program startup. A happy side-effect is that
20834 -mno-eabi libraries can be used with -meabi programs.)
20836 The EABI configuration defaults to the V.4 layout. However,
20837 the stack alignment requirements may differ. If -mno-eabi is not
20838 given, the required stack alignment is 8 bytes; if -mno-eabi is
20839 given, the required alignment is 16 bytes. (But see V.4 comment
20840 above.) */
20842 #ifndef ABI_STACK_BOUNDARY
20843 #define ABI_STACK_BOUNDARY STACK_BOUNDARY
20844 #endif
20846 static rs6000_stack_t *
20847 rs6000_stack_info (void)
20849 rs6000_stack_t *info_ptr = &stack_info;
20850 int reg_size = TARGET_32BIT ? 4 : 8;
20851 int ehrd_size;
20852 int ehcr_size;
20853 int save_align;
20854 int first_gp;
20855 HOST_WIDE_INT non_fixed_size;
20856 bool using_static_chain_p;
20858 if (reload_completed && info_ptr->reload_completed)
20859 return info_ptr;
20861 memset (info_ptr, 0, sizeof (*info_ptr));
20862 info_ptr->reload_completed = reload_completed;
20864 if (TARGET_SPE)
20866 /* Cache value so we don't rescan instruction chain over and over. */
20867 if (cfun->machine->insn_chain_scanned_p == 0)
20868 cfun->machine->insn_chain_scanned_p
20869 = spe_func_has_64bit_regs_p () + 1;
20870 info_ptr->spe_64bit_regs_used = cfun->machine->insn_chain_scanned_p - 1;
20873 /* Select which calling sequence. */
20874 info_ptr->abi = DEFAULT_ABI;
20876 /* Calculate which registers need to be saved & save area size. */
20877 info_ptr->first_gp_reg_save = first_reg_to_save ();
20878 /* Assume that we will have to save RS6000_PIC_OFFSET_TABLE_REGNUM,
20879 even if it currently looks like we won't. Reload may need it to
20880 get at a constant; if so, it will have already created a constant
20881 pool entry for it. */
20882 if (((TARGET_TOC && TARGET_MINIMAL_TOC)
20883 || (flag_pic == 1 && DEFAULT_ABI == ABI_V4)
20884 || (flag_pic && DEFAULT_ABI == ABI_DARWIN))
20885 && crtl->uses_const_pool
20886 && info_ptr->first_gp_reg_save > RS6000_PIC_OFFSET_TABLE_REGNUM)
20887 first_gp = RS6000_PIC_OFFSET_TABLE_REGNUM;
20888 else
20889 first_gp = info_ptr->first_gp_reg_save;
20891 info_ptr->gp_size = reg_size * (32 - first_gp);
20893 /* For the SPE, we have an additional upper 32-bits on each GPR.
20894 Ideally we should save the entire 64-bits only when the upper
20895 half is used in SIMD instructions. Since we only record
20896 registers live (not the size they are used in), this proves
20897 difficult because we'd have to traverse the instruction chain at
20898 the right time, taking reload into account. This is a real pain,
20899 so we opt to save the GPRs in 64-bits always if but one register
20900 gets used in 64-bits. Otherwise, all the registers in the frame
20901 get saved in 32-bits.
20903 So... since when we save all GPRs (except the SP) in 64-bits, the
20904 traditional GP save area will be empty. */
20905 if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
20906 info_ptr->gp_size = 0;
20908 info_ptr->first_fp_reg_save = first_fp_reg_to_save ();
20909 info_ptr->fp_size = 8 * (64 - info_ptr->first_fp_reg_save);
20911 info_ptr->first_altivec_reg_save = first_altivec_reg_to_save ();
20912 info_ptr->altivec_size = 16 * (LAST_ALTIVEC_REGNO + 1
20913 - info_ptr->first_altivec_reg_save);
20915 /* Does this function call anything? */
20916 info_ptr->calls_p = (! crtl->is_leaf
20917 || cfun->machine->ra_needs_full_frame);
20919 /* Determine if we need to save the condition code registers. */
20920 if (df_regs_ever_live_p (CR2_REGNO)
20921 || df_regs_ever_live_p (CR3_REGNO)
20922 || df_regs_ever_live_p (CR4_REGNO))
20924 info_ptr->cr_save_p = 1;
20925 if (DEFAULT_ABI == ABI_V4)
20926 info_ptr->cr_size = reg_size;
20929 /* If the current function calls __builtin_eh_return, then we need
20930 to allocate stack space for registers that will hold data for
20931 the exception handler. */
20932 if (crtl->calls_eh_return)
20934 unsigned int i;
20935 for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; ++i)
20936 continue;
20938 /* SPE saves EH registers in 64-bits. */
20939 ehrd_size = i * (TARGET_SPE_ABI
20940 && info_ptr->spe_64bit_regs_used != 0
20941 ? UNITS_PER_SPE_WORD : UNITS_PER_WORD);
20943 else
20944 ehrd_size = 0;
20946 /* In the ELFv2 ABI, we also need to allocate space for separate
20947 CR field save areas if the function calls __builtin_eh_return. */
20948 if (DEFAULT_ABI == ABI_ELFv2 && crtl->calls_eh_return)
20950 /* This hard-codes that we have three call-saved CR fields. */
20951 ehcr_size = 3 * reg_size;
20952 /* We do *not* use the regular CR save mechanism. */
20953 info_ptr->cr_save_p = 0;
20955 else
20956 ehcr_size = 0;
20958 /* Determine various sizes. */
20959 info_ptr->reg_size = reg_size;
20960 info_ptr->fixed_size = RS6000_SAVE_AREA;
20961 info_ptr->vars_size = RS6000_ALIGN (get_frame_size (), 8);
20962 info_ptr->parm_size = RS6000_ALIGN (crtl->outgoing_args_size,
20963 TARGET_ALTIVEC ? 16 : 8);
20964 if (FRAME_GROWS_DOWNWARD)
20965 info_ptr->vars_size
20966 += RS6000_ALIGN (info_ptr->fixed_size + info_ptr->vars_size
20967 + info_ptr->parm_size,
20968 ABI_STACK_BOUNDARY / BITS_PER_UNIT)
20969 - (info_ptr->fixed_size + info_ptr->vars_size
20970 + info_ptr->parm_size);
20972 if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
20973 info_ptr->spe_gp_size = 8 * (32 - first_gp);
20974 else
20975 info_ptr->spe_gp_size = 0;
20977 if (TARGET_ALTIVEC_ABI)
20978 info_ptr->vrsave_mask = compute_vrsave_mask ();
20979 else
20980 info_ptr->vrsave_mask = 0;
20982 if (TARGET_ALTIVEC_VRSAVE && info_ptr->vrsave_mask)
20983 info_ptr->vrsave_size = 4;
20984 else
20985 info_ptr->vrsave_size = 0;
20987 compute_save_world_info (info_ptr);
20989 /* Calculate the offsets. */
20990 switch (DEFAULT_ABI)
20992 case ABI_NONE:
20993 default:
20994 gcc_unreachable ();
20996 case ABI_AIX:
20997 case ABI_ELFv2:
20998 case ABI_DARWIN:
20999 info_ptr->fp_save_offset = - info_ptr->fp_size;
21000 info_ptr->gp_save_offset = info_ptr->fp_save_offset - info_ptr->gp_size;
21002 if (TARGET_ALTIVEC_ABI)
21004 info_ptr->vrsave_save_offset
21005 = info_ptr->gp_save_offset - info_ptr->vrsave_size;
21007 /* Align stack so vector save area is on a quadword boundary.
21008 The padding goes above the vectors. */
21009 if (info_ptr->altivec_size != 0)
21010 info_ptr->altivec_padding_size
21011 = info_ptr->vrsave_save_offset & 0xF;
21012 else
21013 info_ptr->altivec_padding_size = 0;
21015 info_ptr->altivec_save_offset
21016 = info_ptr->vrsave_save_offset
21017 - info_ptr->altivec_padding_size
21018 - info_ptr->altivec_size;
21019 gcc_assert (info_ptr->altivec_size == 0
21020 || info_ptr->altivec_save_offset % 16 == 0);
21022 /* Adjust for AltiVec case. */
21023 info_ptr->ehrd_offset = info_ptr->altivec_save_offset - ehrd_size;
21025 else
21026 info_ptr->ehrd_offset = info_ptr->gp_save_offset - ehrd_size;
21028 info_ptr->ehcr_offset = info_ptr->ehrd_offset - ehcr_size;
21029 info_ptr->cr_save_offset = reg_size; /* first word when 64-bit. */
21030 info_ptr->lr_save_offset = 2*reg_size;
21031 break;
21033 case ABI_V4:
21034 info_ptr->fp_save_offset = - info_ptr->fp_size;
21035 info_ptr->gp_save_offset = info_ptr->fp_save_offset - info_ptr->gp_size;
21036 info_ptr->cr_save_offset = info_ptr->gp_save_offset - info_ptr->cr_size;
21038 if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
21040 /* Align stack so SPE GPR save area is aligned on a
21041 double-word boundary. */
21042 if (info_ptr->spe_gp_size != 0 && info_ptr->cr_save_offset != 0)
21043 info_ptr->spe_padding_size
21044 = 8 - (-info_ptr->cr_save_offset % 8);
21045 else
21046 info_ptr->spe_padding_size = 0;
21048 info_ptr->spe_gp_save_offset
21049 = info_ptr->cr_save_offset
21050 - info_ptr->spe_padding_size
21051 - info_ptr->spe_gp_size;
21053 /* Adjust for SPE case. */
21054 info_ptr->ehrd_offset = info_ptr->spe_gp_save_offset;
21056 else if (TARGET_ALTIVEC_ABI)
21058 info_ptr->vrsave_save_offset
21059 = info_ptr->cr_save_offset - info_ptr->vrsave_size;
21061 /* Align stack so vector save area is on a quadword boundary. */
21062 if (info_ptr->altivec_size != 0)
21063 info_ptr->altivec_padding_size
21064 = 16 - (-info_ptr->vrsave_save_offset % 16);
21065 else
21066 info_ptr->altivec_padding_size = 0;
21068 info_ptr->altivec_save_offset
21069 = info_ptr->vrsave_save_offset
21070 - info_ptr->altivec_padding_size
21071 - info_ptr->altivec_size;
21073 /* Adjust for AltiVec case. */
21074 info_ptr->ehrd_offset = info_ptr->altivec_save_offset;
21076 else
21077 info_ptr->ehrd_offset = info_ptr->cr_save_offset;
21078 info_ptr->ehrd_offset -= ehrd_size;
21079 info_ptr->lr_save_offset = reg_size;
21080 break;
21083 save_align = (TARGET_ALTIVEC_ABI || DEFAULT_ABI == ABI_DARWIN) ? 16 : 8;
21084 info_ptr->save_size = RS6000_ALIGN (info_ptr->fp_size
21085 + info_ptr->gp_size
21086 + info_ptr->altivec_size
21087 + info_ptr->altivec_padding_size
21088 + info_ptr->spe_gp_size
21089 + info_ptr->spe_padding_size
21090 + ehrd_size
21091 + ehcr_size
21092 + info_ptr->cr_size
21093 + info_ptr->vrsave_size,
21094 save_align);
21096 non_fixed_size = (info_ptr->vars_size
21097 + info_ptr->parm_size
21098 + info_ptr->save_size);
21100 info_ptr->total_size = RS6000_ALIGN (non_fixed_size + info_ptr->fixed_size,
21101 ABI_STACK_BOUNDARY / BITS_PER_UNIT);
21103 /* Determine if we need to save the link register. */
21104 if (info_ptr->calls_p
21105 || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
21106 && crtl->profile
21107 && !TARGET_PROFILE_KERNEL)
21108 || (DEFAULT_ABI == ABI_V4 && cfun->calls_alloca)
21109 #ifdef TARGET_RELOCATABLE
21110 || (TARGET_RELOCATABLE && (get_pool_size () != 0))
21111 #endif
21112 || rs6000_ra_ever_killed ())
21113 info_ptr->lr_save_p = 1;
21115 using_static_chain_p = (cfun->static_chain_decl != NULL_TREE
21116 && df_regs_ever_live_p (STATIC_CHAIN_REGNUM)
21117 && call_used_regs[STATIC_CHAIN_REGNUM]);
21118 info_ptr->savres_strategy = rs6000_savres_strategy (info_ptr,
21119 using_static_chain_p);
21121 if (!(info_ptr->savres_strategy & SAVE_INLINE_GPRS)
21122 || !(info_ptr->savres_strategy & SAVE_INLINE_FPRS)
21123 || !(info_ptr->savres_strategy & SAVE_INLINE_VRS)
21124 || !(info_ptr->savres_strategy & REST_INLINE_GPRS)
21125 || !(info_ptr->savres_strategy & REST_INLINE_FPRS)
21126 || !(info_ptr->savres_strategy & REST_INLINE_VRS))
21127 info_ptr->lr_save_p = 1;
21129 if (info_ptr->lr_save_p)
21130 df_set_regs_ever_live (LR_REGNO, true);
21132 /* Determine if we need to allocate any stack frame:
21134 For AIX we need to push the stack if a frame pointer is needed
21135 (because the stack might be dynamically adjusted), if we are
21136 debugging, if we make calls, or if the sum of fp_save, gp_save,
21137 and local variables are more than the space needed to save all
21138 non-volatile registers: 32-bit: 18*8 + 19*4 = 220 or 64-bit: 18*8
21139 + 18*8 = 288 (GPR13 reserved).
21141 For V.4 we don't have the stack cushion that AIX uses, but assume
21142 that the debugger can handle stackless frames. */
21144 if (info_ptr->calls_p)
21145 info_ptr->push_p = 1;
21147 else if (DEFAULT_ABI == ABI_V4)
21148 info_ptr->push_p = non_fixed_size != 0;
21150 else if (frame_pointer_needed)
21151 info_ptr->push_p = 1;
21153 else if (TARGET_XCOFF && write_symbols != NO_DEBUG)
21154 info_ptr->push_p = 1;
21156 else
21157 info_ptr->push_p = non_fixed_size > (TARGET_32BIT ? 220 : 288);
21159 /* Zero offsets if we're not saving those registers. */
21160 if (info_ptr->fp_size == 0)
21161 info_ptr->fp_save_offset = 0;
21163 if (info_ptr->gp_size == 0)
21164 info_ptr->gp_save_offset = 0;
21166 if (! TARGET_ALTIVEC_ABI || info_ptr->altivec_size == 0)
21167 info_ptr->altivec_save_offset = 0;
21169 /* Zero VRSAVE offset if not saved and restored. */
21170 if (! TARGET_ALTIVEC_VRSAVE || info_ptr->vrsave_mask == 0)
21171 info_ptr->vrsave_save_offset = 0;
21173 if (! TARGET_SPE_ABI
21174 || info_ptr->spe_64bit_regs_used == 0
21175 || info_ptr->spe_gp_size == 0)
21176 info_ptr->spe_gp_save_offset = 0;
21178 if (! info_ptr->lr_save_p)
21179 info_ptr->lr_save_offset = 0;
21181 if (! info_ptr->cr_save_p)
21182 info_ptr->cr_save_offset = 0;
21184 return info_ptr;
21187 /* Return true if the current function uses any GPRs in 64-bit SIMD
21188 mode. */
21190 static bool
21191 spe_func_has_64bit_regs_p (void)
21193 rtx insns, insn;
21195 /* Functions that save and restore all the call-saved registers will
21196 need to save/restore the registers in 64-bits. */
21197 if (crtl->calls_eh_return
21198 || cfun->calls_setjmp
21199 || crtl->has_nonlocal_goto)
21200 return true;
21202 insns = get_insns ();
21204 for (insn = NEXT_INSN (insns); insn != NULL_RTX; insn = NEXT_INSN (insn))
21206 if (INSN_P (insn))
21208 rtx i;
21210 /* FIXME: This should be implemented with attributes...
21212 (set_attr "spe64" "true")....then,
21213 if (get_spe64(insn)) return true;
21215 It's the only reliable way to do the stuff below. */
21217 i = PATTERN (insn);
21218 if (GET_CODE (i) == SET)
21220 enum machine_mode mode = GET_MODE (SET_SRC (i));
21222 if (SPE_VECTOR_MODE (mode))
21223 return true;
21224 if (TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode))
21225 return true;
21230 return false;
21233 static void
21234 debug_stack_info (rs6000_stack_t *info)
21236 const char *abi_string;
21238 if (! info)
21239 info = rs6000_stack_info ();
21241 fprintf (stderr, "\nStack information for function %s:\n",
21242 ((current_function_decl && DECL_NAME (current_function_decl))
21243 ? IDENTIFIER_POINTER (DECL_NAME (current_function_decl))
21244 : "<unknown>"));
21246 switch (info->abi)
21248 default: abi_string = "Unknown"; break;
21249 case ABI_NONE: abi_string = "NONE"; break;
21250 case ABI_AIX: abi_string = "AIX"; break;
21251 case ABI_ELFv2: abi_string = "ELFv2"; break;
21252 case ABI_DARWIN: abi_string = "Darwin"; break;
21253 case ABI_V4: abi_string = "V.4"; break;
21256 fprintf (stderr, "\tABI = %5s\n", abi_string);
21258 if (TARGET_ALTIVEC_ABI)
21259 fprintf (stderr, "\tALTIVEC ABI extensions enabled.\n");
21261 if (TARGET_SPE_ABI)
21262 fprintf (stderr, "\tSPE ABI extensions enabled.\n");
21264 if (info->first_gp_reg_save != 32)
21265 fprintf (stderr, "\tfirst_gp_reg_save = %5d\n", info->first_gp_reg_save);
21267 if (info->first_fp_reg_save != 64)
21268 fprintf (stderr, "\tfirst_fp_reg_save = %5d\n", info->first_fp_reg_save);
21270 if (info->first_altivec_reg_save <= LAST_ALTIVEC_REGNO)
21271 fprintf (stderr, "\tfirst_altivec_reg_save = %5d\n",
21272 info->first_altivec_reg_save);
21274 if (info->lr_save_p)
21275 fprintf (stderr, "\tlr_save_p = %5d\n", info->lr_save_p);
21277 if (info->cr_save_p)
21278 fprintf (stderr, "\tcr_save_p = %5d\n", info->cr_save_p);
21280 if (info->vrsave_mask)
21281 fprintf (stderr, "\tvrsave_mask = 0x%x\n", info->vrsave_mask);
21283 if (info->push_p)
21284 fprintf (stderr, "\tpush_p = %5d\n", info->push_p);
21286 if (info->calls_p)
21287 fprintf (stderr, "\tcalls_p = %5d\n", info->calls_p);
21289 if (info->gp_save_offset)
21290 fprintf (stderr, "\tgp_save_offset = %5d\n", info->gp_save_offset);
21292 if (info->fp_save_offset)
21293 fprintf (stderr, "\tfp_save_offset = %5d\n", info->fp_save_offset);
21295 if (info->altivec_save_offset)
21296 fprintf (stderr, "\taltivec_save_offset = %5d\n",
21297 info->altivec_save_offset);
21299 if (info->spe_gp_save_offset)
21300 fprintf (stderr, "\tspe_gp_save_offset = %5d\n",
21301 info->spe_gp_save_offset);
21303 if (info->vrsave_save_offset)
21304 fprintf (stderr, "\tvrsave_save_offset = %5d\n",
21305 info->vrsave_save_offset);
21307 if (info->lr_save_offset)
21308 fprintf (stderr, "\tlr_save_offset = %5d\n", info->lr_save_offset);
21310 if (info->cr_save_offset)
21311 fprintf (stderr, "\tcr_save_offset = %5d\n", info->cr_save_offset);
21313 if (info->varargs_save_offset)
21314 fprintf (stderr, "\tvarargs_save_offset = %5d\n", info->varargs_save_offset);
21316 if (info->total_size)
21317 fprintf (stderr, "\ttotal_size = "HOST_WIDE_INT_PRINT_DEC"\n",
21318 info->total_size);
21320 if (info->vars_size)
21321 fprintf (stderr, "\tvars_size = "HOST_WIDE_INT_PRINT_DEC"\n",
21322 info->vars_size);
21324 if (info->parm_size)
21325 fprintf (stderr, "\tparm_size = %5d\n", info->parm_size);
21327 if (info->fixed_size)
21328 fprintf (stderr, "\tfixed_size = %5d\n", info->fixed_size);
21330 if (info->gp_size)
21331 fprintf (stderr, "\tgp_size = %5d\n", info->gp_size);
21333 if (info->spe_gp_size)
21334 fprintf (stderr, "\tspe_gp_size = %5d\n", info->spe_gp_size);
21336 if (info->fp_size)
21337 fprintf (stderr, "\tfp_size = %5d\n", info->fp_size);
21339 if (info->altivec_size)
21340 fprintf (stderr, "\taltivec_size = %5d\n", info->altivec_size);
21342 if (info->vrsave_size)
21343 fprintf (stderr, "\tvrsave_size = %5d\n", info->vrsave_size);
21345 if (info->altivec_padding_size)
21346 fprintf (stderr, "\taltivec_padding_size= %5d\n",
21347 info->altivec_padding_size);
21349 if (info->spe_padding_size)
21350 fprintf (stderr, "\tspe_padding_size = %5d\n",
21351 info->spe_padding_size);
21353 if (info->cr_size)
21354 fprintf (stderr, "\tcr_size = %5d\n", info->cr_size);
21356 if (info->save_size)
21357 fprintf (stderr, "\tsave_size = %5d\n", info->save_size);
21359 if (info->reg_size != 4)
21360 fprintf (stderr, "\treg_size = %5d\n", info->reg_size);
21362 fprintf (stderr, "\tsave-strategy = %04x\n", info->savres_strategy);
21364 fprintf (stderr, "\n");
21368 rs6000_return_addr (int count, rtx frame)
21370 /* Currently we don't optimize very well between prolog and body
21371 code and for PIC code the code can be actually quite bad, so
21372 don't try to be too clever here. */
21373 if (count != 0
21374 || ((DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN) && flag_pic))
21376 cfun->machine->ra_needs_full_frame = 1;
21378 return
21379 gen_rtx_MEM
21380 (Pmode,
21381 memory_address
21382 (Pmode,
21383 plus_constant (Pmode,
21384 copy_to_reg
21385 (gen_rtx_MEM (Pmode,
21386 memory_address (Pmode, frame))),
21387 RETURN_ADDRESS_OFFSET)));
21390 cfun->machine->ra_need_lr = 1;
21391 return get_hard_reg_initial_val (Pmode, LR_REGNO);
21394 /* Say whether a function is a candidate for sibcall handling or not. */
21396 static bool
21397 rs6000_function_ok_for_sibcall (tree decl, tree exp)
21399 tree fntype;
21401 if (decl)
21402 fntype = TREE_TYPE (decl);
21403 else
21404 fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (exp)));
21406 /* We can't do it if the called function has more vector parameters
21407 than the current function; there's nowhere to put the VRsave code. */
21408 if (TARGET_ALTIVEC_ABI
21409 && TARGET_ALTIVEC_VRSAVE
21410 && !(decl && decl == current_function_decl))
21412 function_args_iterator args_iter;
21413 tree type;
21414 int nvreg = 0;
21416 /* Functions with vector parameters are required to have a
21417 prototype, so the argument type info must be available
21418 here. */
21419 FOREACH_FUNCTION_ARGS(fntype, type, args_iter)
21420 if (TREE_CODE (type) == VECTOR_TYPE
21421 && ALTIVEC_OR_VSX_VECTOR_MODE (TYPE_MODE (type)))
21422 nvreg++;
21424 FOREACH_FUNCTION_ARGS(TREE_TYPE (current_function_decl), type, args_iter)
21425 if (TREE_CODE (type) == VECTOR_TYPE
21426 && ALTIVEC_OR_VSX_VECTOR_MODE (TYPE_MODE (type)))
21427 nvreg--;
21429 if (nvreg > 0)
21430 return false;
21433 /* Under the AIX or ELFv2 ABIs we can't allow calls to non-local
21434 functions, because the callee may have a different TOC pointer to
21435 the caller and there's no way to ensure we restore the TOC when
21436 we return. With the secure-plt SYSV ABI we can't make non-local
21437 calls when -fpic/PIC because the plt call stubs use r30. */
21438 if (DEFAULT_ABI == ABI_DARWIN
21439 || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
21440 && decl
21441 && !DECL_EXTERNAL (decl)
21442 && (*targetm.binds_local_p) (decl))
21443 || (DEFAULT_ABI == ABI_V4
21444 && (!TARGET_SECURE_PLT
21445 || !flag_pic
21446 || (decl
21447 && (*targetm.binds_local_p) (decl)))))
21449 tree attr_list = TYPE_ATTRIBUTES (fntype);
21451 if (!lookup_attribute ("longcall", attr_list)
21452 || lookup_attribute ("shortcall", attr_list))
21453 return true;
21456 return false;
21459 static int
21460 rs6000_ra_ever_killed (void)
21462 rtx top;
21463 rtx reg;
21464 rtx insn;
21466 if (cfun->is_thunk)
21467 return 0;
21469 if (cfun->machine->lr_save_state)
21470 return cfun->machine->lr_save_state - 1;
21472 /* regs_ever_live has LR marked as used if any sibcalls are present,
21473 but this should not force saving and restoring in the
21474 pro/epilogue. Likewise, reg_set_between_p thinks a sibcall
21475 clobbers LR, so that is inappropriate. */
21477 /* Also, the prologue can generate a store into LR that
21478 doesn't really count, like this:
21480 move LR->R0
21481 bcl to set PIC register
21482 move LR->R31
21483 move R0->LR
21485 When we're called from the epilogue, we need to avoid counting
21486 this as a store. */
21488 push_topmost_sequence ();
21489 top = get_insns ();
21490 pop_topmost_sequence ();
21491 reg = gen_rtx_REG (Pmode, LR_REGNO);
21493 for (insn = NEXT_INSN (top); insn != NULL_RTX; insn = NEXT_INSN (insn))
21495 if (INSN_P (insn))
21497 if (CALL_P (insn))
21499 if (!SIBLING_CALL_P (insn))
21500 return 1;
21502 else if (find_regno_note (insn, REG_INC, LR_REGNO))
21503 return 1;
21504 else if (set_of (reg, insn) != NULL_RTX
21505 && !prologue_epilogue_contains (insn))
21506 return 1;
21509 return 0;
21512 /* Emit instructions needed to load the TOC register.
21513 This is only needed when TARGET_TOC, TARGET_MINIMAL_TOC, and there is
21514 a constant pool; or for SVR4 -fpic. */
21516 void
21517 rs6000_emit_load_toc_table (int fromprolog)
21519 rtx dest;
21520 dest = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
21522 if (TARGET_ELF && TARGET_SECURE_PLT && DEFAULT_ABI == ABI_V4 && flag_pic)
21524 char buf[30];
21525 rtx lab, tmp1, tmp2, got;
21527 lab = gen_label_rtx ();
21528 ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (lab));
21529 lab = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
21530 if (flag_pic == 2)
21531 got = gen_rtx_SYMBOL_REF (Pmode, toc_label_name);
21532 else
21533 got = rs6000_got_sym ();
21534 tmp1 = tmp2 = dest;
21535 if (!fromprolog)
21537 tmp1 = gen_reg_rtx (Pmode);
21538 tmp2 = gen_reg_rtx (Pmode);
21540 emit_insn (gen_load_toc_v4_PIC_1 (lab));
21541 emit_move_insn (tmp1, gen_rtx_REG (Pmode, LR_REGNO));
21542 emit_insn (gen_load_toc_v4_PIC_3b (tmp2, tmp1, got, lab));
21543 emit_insn (gen_load_toc_v4_PIC_3c (dest, tmp2, got, lab));
21545 else if (TARGET_ELF && DEFAULT_ABI == ABI_V4 && flag_pic == 1)
21547 emit_insn (gen_load_toc_v4_pic_si ());
21548 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
21550 else if (TARGET_ELF && DEFAULT_ABI == ABI_V4 && flag_pic == 2)
21552 char buf[30];
21553 rtx temp0 = (fromprolog
21554 ? gen_rtx_REG (Pmode, 0)
21555 : gen_reg_rtx (Pmode));
21557 if (fromprolog)
21559 rtx symF, symL;
21561 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
21562 symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
21564 ASM_GENERATE_INTERNAL_LABEL (buf, "LCL", rs6000_pic_labelno);
21565 symL = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
21567 emit_insn (gen_load_toc_v4_PIC_1 (symF));
21568 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
21569 emit_insn (gen_load_toc_v4_PIC_2 (temp0, dest, symL, symF));
21571 else
21573 rtx tocsym, lab;
21575 tocsym = gen_rtx_SYMBOL_REF (Pmode, toc_label_name);
21576 lab = gen_label_rtx ();
21577 emit_insn (gen_load_toc_v4_PIC_1b (tocsym, lab));
21578 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
21579 if (TARGET_LINK_STACK)
21580 emit_insn (gen_addsi3 (dest, dest, GEN_INT (4)));
21581 emit_move_insn (temp0, gen_rtx_MEM (Pmode, dest));
21583 emit_insn (gen_addsi3 (dest, temp0, dest));
21585 else if (TARGET_ELF && !TARGET_AIX && flag_pic == 0 && TARGET_MINIMAL_TOC)
21587 /* This is for AIX code running in non-PIC ELF32. */
21588 char buf[30];
21589 rtx realsym;
21590 ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
21591 realsym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
21593 emit_insn (gen_elf_high (dest, realsym));
21594 emit_insn (gen_elf_low (dest, dest, realsym));
21596 else
21598 gcc_assert (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2);
21600 if (TARGET_32BIT)
21601 emit_insn (gen_load_toc_aix_si (dest));
21602 else
21603 emit_insn (gen_load_toc_aix_di (dest));
21607 /* Emit instructions to restore the link register after determining where
21608 its value has been stored. */
21610 void
21611 rs6000_emit_eh_reg_restore (rtx source, rtx scratch)
21613 rs6000_stack_t *info = rs6000_stack_info ();
21614 rtx operands[2];
21616 operands[0] = source;
21617 operands[1] = scratch;
21619 if (info->lr_save_p)
21621 rtx frame_rtx = stack_pointer_rtx;
21622 HOST_WIDE_INT sp_offset = 0;
21623 rtx tmp;
21625 if (frame_pointer_needed
21626 || cfun->calls_alloca
21627 || info->total_size > 32767)
21629 tmp = gen_frame_mem (Pmode, frame_rtx);
21630 emit_move_insn (operands[1], tmp);
21631 frame_rtx = operands[1];
21633 else if (info->push_p)
21634 sp_offset = info->total_size;
21636 tmp = plus_constant (Pmode, frame_rtx,
21637 info->lr_save_offset + sp_offset);
21638 tmp = gen_frame_mem (Pmode, tmp);
21639 emit_move_insn (tmp, operands[0]);
21641 else
21642 emit_move_insn (gen_rtx_REG (Pmode, LR_REGNO), operands[0]);
21644 /* Freeze lr_save_p. We've just emitted rtl that depends on the
21645 state of lr_save_p so any change from here on would be a bug. In
21646 particular, stop rs6000_ra_ever_killed from considering the SET
21647 of lr we may have added just above. */
21648 cfun->machine->lr_save_state = info->lr_save_p + 1;
21651 static GTY(()) alias_set_type set = -1;
21653 alias_set_type
21654 get_TOC_alias_set (void)
21656 if (set == -1)
21657 set = new_alias_set ();
21658 return set;
21661 /* This returns nonzero if the current function uses the TOC. This is
21662 determined by the presence of (use (unspec ... UNSPEC_TOC)), which
21663 is generated by the ABI_V4 load_toc_* patterns. */
21664 #if TARGET_ELF
21665 static int
21666 uses_TOC (void)
21668 rtx insn;
21670 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
21671 if (INSN_P (insn))
21673 rtx pat = PATTERN (insn);
21674 int i;
21676 if (GET_CODE (pat) == PARALLEL)
21677 for (i = 0; i < XVECLEN (pat, 0); i++)
21679 rtx sub = XVECEXP (pat, 0, i);
21680 if (GET_CODE (sub) == USE)
21682 sub = XEXP (sub, 0);
21683 if (GET_CODE (sub) == UNSPEC
21684 && XINT (sub, 1) == UNSPEC_TOC)
21685 return 1;
21689 return 0;
21691 #endif
21694 create_TOC_reference (rtx symbol, rtx largetoc_reg)
21696 rtx tocrel, tocreg, hi;
21698 if (TARGET_DEBUG_ADDR)
21700 if (GET_CODE (symbol) == SYMBOL_REF)
21701 fprintf (stderr, "\ncreate_TOC_reference, (symbol_ref %s)\n",
21702 XSTR (symbol, 0));
21703 else
21705 fprintf (stderr, "\ncreate_TOC_reference, code %s:\n",
21706 GET_RTX_NAME (GET_CODE (symbol)));
21707 debug_rtx (symbol);
21711 if (!can_create_pseudo_p ())
21712 df_set_regs_ever_live (TOC_REGISTER, true);
21714 tocreg = gen_rtx_REG (Pmode, TOC_REGISTER);
21715 tocrel = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, symbol, tocreg), UNSPEC_TOCREL);
21716 if (TARGET_CMODEL == CMODEL_SMALL || can_create_pseudo_p ())
21717 return tocrel;
21719 hi = gen_rtx_HIGH (Pmode, copy_rtx (tocrel));
21720 if (largetoc_reg != NULL)
21722 emit_move_insn (largetoc_reg, hi);
21723 hi = largetoc_reg;
21725 return gen_rtx_LO_SUM (Pmode, hi, tocrel);
21728 /* Issue assembly directives that create a reference to the given DWARF
21729 FRAME_TABLE_LABEL from the current function section. */
21730 void
21731 rs6000_aix_asm_output_dwarf_table_ref (char * frame_table_label)
21733 fprintf (asm_out_file, "\t.ref %s\n",
21734 (* targetm.strip_name_encoding) (frame_table_label));
21737 /* This ties together stack memory (MEM with an alias set of frame_alias_set)
21738 and the change to the stack pointer. */
21740 static void
21741 rs6000_emit_stack_tie (rtx fp, bool hard_frame_needed)
21743 rtvec p;
21744 int i;
21745 rtx regs[3];
21747 i = 0;
21748 regs[i++] = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
21749 if (hard_frame_needed)
21750 regs[i++] = gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM);
21751 if (!(REGNO (fp) == STACK_POINTER_REGNUM
21752 || (hard_frame_needed
21753 && REGNO (fp) == HARD_FRAME_POINTER_REGNUM)))
21754 regs[i++] = fp;
21756 p = rtvec_alloc (i);
21757 while (--i >= 0)
21759 rtx mem = gen_frame_mem (BLKmode, regs[i]);
21760 RTVEC_ELT (p, i) = gen_rtx_SET (VOIDmode, mem, const0_rtx);
21763 emit_insn (gen_stack_tie (gen_rtx_PARALLEL (VOIDmode, p)));
21766 /* Emit the correct code for allocating stack space, as insns.
21767 If COPY_REG, make sure a copy of the old frame is left there.
21768 The generated code may use hard register 0 as a temporary. */
21770 static void
21771 rs6000_emit_allocate_stack (HOST_WIDE_INT size, rtx copy_reg, int copy_off)
21773 rtx insn;
21774 rtx stack_reg = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
21775 rtx tmp_reg = gen_rtx_REG (Pmode, 0);
21776 rtx todec = gen_int_mode (-size, Pmode);
21777 rtx par, set, mem;
21779 if (INTVAL (todec) != -size)
21781 warning (0, "stack frame too large");
21782 emit_insn (gen_trap ());
21783 return;
21786 if (crtl->limit_stack)
21788 if (REG_P (stack_limit_rtx)
21789 && REGNO (stack_limit_rtx) > 1
21790 && REGNO (stack_limit_rtx) <= 31)
21792 emit_insn (gen_add3_insn (tmp_reg, stack_limit_rtx, GEN_INT (size)));
21793 emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
21794 const0_rtx));
21796 else if (GET_CODE (stack_limit_rtx) == SYMBOL_REF
21797 && TARGET_32BIT
21798 && DEFAULT_ABI == ABI_V4)
21800 rtx toload = gen_rtx_CONST (VOIDmode,
21801 gen_rtx_PLUS (Pmode,
21802 stack_limit_rtx,
21803 GEN_INT (size)));
21805 emit_insn (gen_elf_high (tmp_reg, toload));
21806 emit_insn (gen_elf_low (tmp_reg, tmp_reg, toload));
21807 emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
21808 const0_rtx));
21810 else
21811 warning (0, "stack limit expression is not supported");
21814 if (copy_reg)
21816 if (copy_off != 0)
21817 emit_insn (gen_add3_insn (copy_reg, stack_reg, GEN_INT (copy_off)));
21818 else
21819 emit_move_insn (copy_reg, stack_reg);
21822 if (size > 32767)
21824 /* Need a note here so that try_split doesn't get confused. */
21825 if (get_last_insn () == NULL_RTX)
21826 emit_note (NOTE_INSN_DELETED);
21827 insn = emit_move_insn (tmp_reg, todec);
21828 try_split (PATTERN (insn), insn, 0);
21829 todec = tmp_reg;
21832 insn = emit_insn (TARGET_32BIT
21833 ? gen_movsi_update_stack (stack_reg, stack_reg,
21834 todec, stack_reg)
21835 : gen_movdi_di_update_stack (stack_reg, stack_reg,
21836 todec, stack_reg));
21837 /* Since we didn't use gen_frame_mem to generate the MEM, grab
21838 it now and set the alias set/attributes. The above gen_*_update
21839 calls will generate a PARALLEL with the MEM set being the first
21840 operation. */
21841 par = PATTERN (insn);
21842 gcc_assert (GET_CODE (par) == PARALLEL);
21843 set = XVECEXP (par, 0, 0);
21844 gcc_assert (GET_CODE (set) == SET);
21845 mem = SET_DEST (set);
21846 gcc_assert (MEM_P (mem));
21847 MEM_NOTRAP_P (mem) = 1;
21848 set_mem_alias_set (mem, get_frame_alias_set ());
21850 RTX_FRAME_RELATED_P (insn) = 1;
21851 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
21852 gen_rtx_SET (VOIDmode, stack_reg,
21853 gen_rtx_PLUS (Pmode, stack_reg,
21854 GEN_INT (-size))));
21857 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
21859 #if PROBE_INTERVAL > 32768
21860 #error Cannot use indexed addressing mode for stack probing
21861 #endif
21863 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
21864 inclusive. These are offsets from the current stack pointer. */
21866 static void
21867 rs6000_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
21869 /* See if we have a constant small number of probes to generate. If so,
21870 that's the easy case. */
21871 if (first + size <= 32768)
21873 HOST_WIDE_INT i;
21875 /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
21876 it exceeds SIZE. If only one probe is needed, this will not
21877 generate any code. Then probe at FIRST + SIZE. */
21878 for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
21879 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
21880 -(first + i)));
21882 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
21883 -(first + size)));
21886 /* Otherwise, do the same as above, but in a loop. Note that we must be
21887 extra careful with variables wrapping around because we might be at
21888 the very top (or the very bottom) of the address space and we have
21889 to be able to handle this case properly; in particular, we use an
21890 equality test for the loop condition. */
21891 else
21893 HOST_WIDE_INT rounded_size;
21894 rtx r12 = gen_rtx_REG (Pmode, 12);
21895 rtx r0 = gen_rtx_REG (Pmode, 0);
21897 /* Sanity check for the addressing mode we're going to use. */
21898 gcc_assert (first <= 32768);
21900 /* Step 1: round SIZE to the previous multiple of the interval. */
21902 rounded_size = size & -PROBE_INTERVAL;
21905 /* Step 2: compute initial and final value of the loop counter. */
21907 /* TEST_ADDR = SP + FIRST. */
21908 emit_insn (gen_rtx_SET (VOIDmode, r12,
21909 plus_constant (Pmode, stack_pointer_rtx,
21910 -first)));
21912 /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE. */
21913 if (rounded_size > 32768)
21915 emit_move_insn (r0, GEN_INT (-rounded_size));
21916 emit_insn (gen_rtx_SET (VOIDmode, r0,
21917 gen_rtx_PLUS (Pmode, r12, r0)));
21919 else
21920 emit_insn (gen_rtx_SET (VOIDmode, r0,
21921 plus_constant (Pmode, r12, -rounded_size)));
21924 /* Step 3: the loop
21926 while (TEST_ADDR != LAST_ADDR)
21928 TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
21929 probe at TEST_ADDR
21932 probes at FIRST + N * PROBE_INTERVAL for values of N from 1
21933 until it is equal to ROUNDED_SIZE. */
21935 if (TARGET_64BIT)
21936 emit_insn (gen_probe_stack_rangedi (r12, r12, r0));
21937 else
21938 emit_insn (gen_probe_stack_rangesi (r12, r12, r0));
21941 /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
21942 that SIZE is equal to ROUNDED_SIZE. */
21944 if (size != rounded_size)
21945 emit_stack_probe (plus_constant (Pmode, r12, rounded_size - size));
21949 /* Probe a range of stack addresses from REG1 to REG2 inclusive. These are
21950 absolute addresses. */
21952 const char *
21953 output_probe_stack_range (rtx reg1, rtx reg2)
21955 static int labelno = 0;
21956 char loop_lab[32], end_lab[32];
21957 rtx xops[2];
21959 ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno);
21960 ASM_GENERATE_INTERNAL_LABEL (end_lab, "LPSRE", labelno++);
21962 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
21964 /* Jump to END_LAB if TEST_ADDR == LAST_ADDR. */
21965 xops[0] = reg1;
21966 xops[1] = reg2;
21967 if (TARGET_64BIT)
21968 output_asm_insn ("cmpd 0,%0,%1", xops);
21969 else
21970 output_asm_insn ("cmpw 0,%0,%1", xops);
21972 fputs ("\tbeq 0,", asm_out_file);
21973 assemble_name_raw (asm_out_file, end_lab);
21974 fputc ('\n', asm_out_file);
21976 /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL. */
21977 xops[1] = GEN_INT (-PROBE_INTERVAL);
21978 output_asm_insn ("addi %0,%0,%1", xops);
21980 /* Probe at TEST_ADDR and branch. */
21981 xops[1] = gen_rtx_REG (Pmode, 0);
21982 output_asm_insn ("stw %1,0(%0)", xops);
21983 fprintf (asm_out_file, "\tb ");
21984 assemble_name_raw (asm_out_file, loop_lab);
21985 fputc ('\n', asm_out_file);
21987 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, end_lab);
21989 return "";
21992 /* Add to 'insn' a note which is PATTERN (INSN) but with REG replaced
21993 with (plus:P (reg 1) VAL), and with REG2 replaced with RREG if REG2
21994 is not NULL. It would be nice if dwarf2out_frame_debug_expr could
21995 deduce these equivalences by itself so it wasn't necessary to hold
21996 its hand so much. Don't be tempted to always supply d2_f_d_e with
21997 the actual cfa register, ie. r31 when we are using a hard frame
21998 pointer. That fails when saving regs off r1, and sched moves the
21999 r31 setup past the reg saves. */
22001 static rtx
22002 rs6000_frame_related (rtx insn, rtx reg, HOST_WIDE_INT val,
22003 rtx reg2, rtx rreg, rtx split_reg)
22005 rtx real, temp;
22007 if (REGNO (reg) == STACK_POINTER_REGNUM && reg2 == NULL_RTX)
22009 /* No need for any replacement. Just set RTX_FRAME_RELATED_P. */
22010 int i;
22012 gcc_checking_assert (val == 0);
22013 real = PATTERN (insn);
22014 if (GET_CODE (real) == PARALLEL)
22015 for (i = 0; i < XVECLEN (real, 0); i++)
22016 if (GET_CODE (XVECEXP (real, 0, i)) == SET)
22018 rtx set = XVECEXP (real, 0, i);
22020 RTX_FRAME_RELATED_P (set) = 1;
22022 RTX_FRAME_RELATED_P (insn) = 1;
22023 return insn;
22026 /* copy_rtx will not make unique copies of registers, so we need to
22027 ensure we don't have unwanted sharing here. */
22028 if (reg == reg2)
22029 reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
22031 if (reg == rreg)
22032 reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
22034 real = copy_rtx (PATTERN (insn));
22036 if (reg2 != NULL_RTX)
22037 real = replace_rtx (real, reg2, rreg);
22039 if (REGNO (reg) == STACK_POINTER_REGNUM)
22040 gcc_checking_assert (val == 0);
22041 else
22042 real = replace_rtx (real, reg,
22043 gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode,
22044 STACK_POINTER_REGNUM),
22045 GEN_INT (val)));
22047 /* We expect that 'real' is either a SET or a PARALLEL containing
22048 SETs (and possibly other stuff). In a PARALLEL, all the SETs
22049 are important so they all have to be marked RTX_FRAME_RELATED_P. */
22051 if (GET_CODE (real) == SET)
22053 rtx set = real;
22055 temp = simplify_rtx (SET_SRC (set));
22056 if (temp)
22057 SET_SRC (set) = temp;
22058 temp = simplify_rtx (SET_DEST (set));
22059 if (temp)
22060 SET_DEST (set) = temp;
22061 if (GET_CODE (SET_DEST (set)) == MEM)
22063 temp = simplify_rtx (XEXP (SET_DEST (set), 0));
22064 if (temp)
22065 XEXP (SET_DEST (set), 0) = temp;
22068 else
22070 int i;
22072 gcc_assert (GET_CODE (real) == PARALLEL);
22073 for (i = 0; i < XVECLEN (real, 0); i++)
22074 if (GET_CODE (XVECEXP (real, 0, i)) == SET)
22076 rtx set = XVECEXP (real, 0, i);
22078 temp = simplify_rtx (SET_SRC (set));
22079 if (temp)
22080 SET_SRC (set) = temp;
22081 temp = simplify_rtx (SET_DEST (set));
22082 if (temp)
22083 SET_DEST (set) = temp;
22084 if (GET_CODE (SET_DEST (set)) == MEM)
22086 temp = simplify_rtx (XEXP (SET_DEST (set), 0));
22087 if (temp)
22088 XEXP (SET_DEST (set), 0) = temp;
22090 RTX_FRAME_RELATED_P (set) = 1;
22094 /* If a store insn has been split into multiple insns, the
22095 true source register is given by split_reg. */
22096 if (split_reg != NULL_RTX)
22097 real = gen_rtx_SET (VOIDmode, SET_DEST (real), split_reg);
22099 RTX_FRAME_RELATED_P (insn) = 1;
22100 add_reg_note (insn, REG_FRAME_RELATED_EXPR, real);
22102 return insn;
22105 /* Returns an insn that has a vrsave set operation with the
22106 appropriate CLOBBERs. */
22108 static rtx
22109 generate_set_vrsave (rtx reg, rs6000_stack_t *info, int epiloguep)
22111 int nclobs, i;
22112 rtx insn, clobs[TOTAL_ALTIVEC_REGS + 1];
22113 rtx vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
22115 clobs[0]
22116 = gen_rtx_SET (VOIDmode,
22117 vrsave,
22118 gen_rtx_UNSPEC_VOLATILE (SImode,
22119 gen_rtvec (2, reg, vrsave),
22120 UNSPECV_SET_VRSAVE));
22122 nclobs = 1;
22124 /* We need to clobber the registers in the mask so the scheduler
22125 does not move sets to VRSAVE before sets of AltiVec registers.
22127 However, if the function receives nonlocal gotos, reload will set
22128 all call saved registers live. We will end up with:
22130 (set (reg 999) (mem))
22131 (parallel [ (set (reg vrsave) (unspec blah))
22132 (clobber (reg 999))])
22134 The clobber will cause the store into reg 999 to be dead, and
22135 flow will attempt to delete an epilogue insn. In this case, we
22136 need an unspec use/set of the register. */
22138 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
22139 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
22141 if (!epiloguep || call_used_regs [i])
22142 clobs[nclobs++] = gen_rtx_CLOBBER (VOIDmode,
22143 gen_rtx_REG (V4SImode, i));
22144 else
22146 rtx reg = gen_rtx_REG (V4SImode, i);
22148 clobs[nclobs++]
22149 = gen_rtx_SET (VOIDmode,
22150 reg,
22151 gen_rtx_UNSPEC (V4SImode,
22152 gen_rtvec (1, reg), 27));
22156 insn = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nclobs));
22158 for (i = 0; i < nclobs; ++i)
22159 XVECEXP (insn, 0, i) = clobs[i];
22161 return insn;
22164 static rtx
22165 gen_frame_set (rtx reg, rtx frame_reg, int offset, bool store)
22167 rtx addr, mem;
22169 addr = gen_rtx_PLUS (Pmode, frame_reg, GEN_INT (offset));
22170 mem = gen_frame_mem (GET_MODE (reg), addr);
22171 return gen_rtx_SET (VOIDmode, store ? mem : reg, store ? reg : mem);
22174 static rtx
22175 gen_frame_load (rtx reg, rtx frame_reg, int offset)
22177 return gen_frame_set (reg, frame_reg, offset, false);
22180 static rtx
22181 gen_frame_store (rtx reg, rtx frame_reg, int offset)
22183 return gen_frame_set (reg, frame_reg, offset, true);
22186 /* Save a register into the frame, and emit RTX_FRAME_RELATED_P notes.
22187 Save REGNO into [FRAME_REG + OFFSET] in mode MODE. */
22189 static rtx
22190 emit_frame_save (rtx frame_reg, enum machine_mode mode,
22191 unsigned int regno, int offset, HOST_WIDE_INT frame_reg_to_sp)
22193 rtx reg, insn;
22195 /* Some cases that need register indexed addressing. */
22196 gcc_checking_assert (!((TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
22197 || (TARGET_VSX && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
22198 || (TARGET_E500_DOUBLE && mode == DFmode)
22199 || (TARGET_SPE_ABI
22200 && SPE_VECTOR_MODE (mode)
22201 && !SPE_CONST_OFFSET_OK (offset))));
22203 reg = gen_rtx_REG (mode, regno);
22204 insn = emit_insn (gen_frame_store (reg, frame_reg, offset));
22205 return rs6000_frame_related (insn, frame_reg, frame_reg_to_sp,
22206 NULL_RTX, NULL_RTX, NULL_RTX);
22209 /* Emit an offset memory reference suitable for a frame store, while
22210 converting to a valid addressing mode. */
22212 static rtx
22213 gen_frame_mem_offset (enum machine_mode mode, rtx reg, int offset)
22215 rtx int_rtx, offset_rtx;
22217 int_rtx = GEN_INT (offset);
22219 if ((TARGET_SPE_ABI && SPE_VECTOR_MODE (mode) && !SPE_CONST_OFFSET_OK (offset))
22220 || (TARGET_E500_DOUBLE && mode == DFmode))
22222 offset_rtx = gen_rtx_REG (Pmode, FIXED_SCRATCH);
22223 emit_move_insn (offset_rtx, int_rtx);
22225 else
22226 offset_rtx = int_rtx;
22228 return gen_frame_mem (mode, gen_rtx_PLUS (Pmode, reg, offset_rtx));
22231 #ifndef TARGET_FIX_AND_CONTINUE
22232 #define TARGET_FIX_AND_CONTINUE 0
22233 #endif
22235 /* It's really GPR 13 or 14, FPR 14 and VR 20. We need the smallest. */
22236 #define FIRST_SAVRES_REGISTER FIRST_SAVED_GP_REGNO
22237 #define LAST_SAVRES_REGISTER 31
22238 #define N_SAVRES_REGISTERS (LAST_SAVRES_REGISTER - FIRST_SAVRES_REGISTER + 1)
22240 enum {
22241 SAVRES_LR = 0x1,
22242 SAVRES_SAVE = 0x2,
22243 SAVRES_REG = 0x0c,
22244 SAVRES_GPR = 0,
22245 SAVRES_FPR = 4,
22246 SAVRES_VR = 8
22249 static GTY(()) rtx savres_routine_syms[N_SAVRES_REGISTERS][12];
22251 /* Temporary holding space for an out-of-line register save/restore
22252 routine name. */
22253 static char savres_routine_name[30];
22255 /* Return the name for an out-of-line register save/restore routine.
22256 We are saving/restoring GPRs if GPR is true. */
22258 static char *
22259 rs6000_savres_routine_name (rs6000_stack_t *info, int regno, int sel)
22261 const char *prefix = "";
22262 const char *suffix = "";
22264 /* Different targets are supposed to define
22265 {SAVE,RESTORE}_FP_{PREFIX,SUFFIX} with the idea that the needed
22266 routine name could be defined with:
22268 sprintf (name, "%s%d%s", SAVE_FP_PREFIX, regno, SAVE_FP_SUFFIX)
22270 This is a nice idea in practice, but in reality, things are
22271 complicated in several ways:
22273 - ELF targets have save/restore routines for GPRs.
22275 - SPE targets use different prefixes for 32/64-bit registers, and
22276 neither of them fit neatly in the FOO_{PREFIX,SUFFIX} regimen.
22278 - PPC64 ELF targets have routines for save/restore of GPRs that
22279 differ in what they do with the link register, so having a set
22280 prefix doesn't work. (We only use one of the save routines at
22281 the moment, though.)
22283 - PPC32 elf targets have "exit" versions of the restore routines
22284 that restore the link register and can save some extra space.
22285 These require an extra suffix. (There are also "tail" versions
22286 of the restore routines and "GOT" versions of the save routines,
22287 but we don't generate those at present. Same problems apply,
22288 though.)
22290 We deal with all this by synthesizing our own prefix/suffix and
22291 using that for the simple sprintf call shown above. */
22292 if (TARGET_SPE)
22294 /* No floating point saves on the SPE. */
22295 gcc_assert ((sel & SAVRES_REG) == SAVRES_GPR);
22297 if ((sel & SAVRES_SAVE))
22298 prefix = info->spe_64bit_regs_used ? "_save64gpr_" : "_save32gpr_";
22299 else
22300 prefix = info->spe_64bit_regs_used ? "_rest64gpr_" : "_rest32gpr_";
22302 if ((sel & SAVRES_LR))
22303 suffix = "_x";
22305 else if (DEFAULT_ABI == ABI_V4)
22307 if (TARGET_64BIT)
22308 goto aix_names;
22310 if ((sel & SAVRES_REG) == SAVRES_GPR)
22311 prefix = (sel & SAVRES_SAVE) ? "_savegpr_" : "_restgpr_";
22312 else if ((sel & SAVRES_REG) == SAVRES_FPR)
22313 prefix = (sel & SAVRES_SAVE) ? "_savefpr_" : "_restfpr_";
22314 else if ((sel & SAVRES_REG) == SAVRES_VR)
22315 prefix = (sel & SAVRES_SAVE) ? "_savevr_" : "_restvr_";
22316 else
22317 abort ();
22319 if ((sel & SAVRES_LR))
22320 suffix = "_x";
22322 else if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
22324 #if !defined (POWERPC_LINUX) && !defined (POWERPC_FREEBSD)
22325 /* No out-of-line save/restore routines for GPRs on AIX. */
22326 gcc_assert (!TARGET_AIX || (sel & SAVRES_REG) != SAVRES_GPR);
22327 #endif
22329 aix_names:
22330 if ((sel & SAVRES_REG) == SAVRES_GPR)
22331 prefix = ((sel & SAVRES_SAVE)
22332 ? ((sel & SAVRES_LR) ? "_savegpr0_" : "_savegpr1_")
22333 : ((sel & SAVRES_LR) ? "_restgpr0_" : "_restgpr1_"));
22334 else if ((sel & SAVRES_REG) == SAVRES_FPR)
22336 #if defined (POWERPC_LINUX) || defined (POWERPC_FREEBSD)
22337 if ((sel & SAVRES_LR))
22338 prefix = ((sel & SAVRES_SAVE) ? "_savefpr_" : "_restfpr_");
22339 else
22340 #endif
22342 prefix = (sel & SAVRES_SAVE) ? SAVE_FP_PREFIX : RESTORE_FP_PREFIX;
22343 suffix = (sel & SAVRES_SAVE) ? SAVE_FP_SUFFIX : RESTORE_FP_SUFFIX;
22346 else if ((sel & SAVRES_REG) == SAVRES_VR)
22347 prefix = (sel & SAVRES_SAVE) ? "_savevr_" : "_restvr_";
22348 else
22349 abort ();
22352 if (DEFAULT_ABI == ABI_DARWIN)
22354 /* The Darwin approach is (slightly) different, in order to be
22355 compatible with code generated by the system toolchain. There is a
22356 single symbol for the start of save sequence, and the code here
22357 embeds an offset into that code on the basis of the first register
22358 to be saved. */
22359 prefix = (sel & SAVRES_SAVE) ? "save" : "rest" ;
22360 if ((sel & SAVRES_REG) == SAVRES_GPR)
22361 sprintf (savres_routine_name, "*%sGPR%s%s%.0d ; %s r%d-r31", prefix,
22362 ((sel & SAVRES_LR) ? "x" : ""), (regno == 13 ? "" : "+"),
22363 (regno - 13) * 4, prefix, regno);
22364 else if ((sel & SAVRES_REG) == SAVRES_FPR)
22365 sprintf (savres_routine_name, "*%sFP%s%.0d ; %s f%d-f31", prefix,
22366 (regno == 14 ? "" : "+"), (regno - 14) * 4, prefix, regno);
22367 else if ((sel & SAVRES_REG) == SAVRES_VR)
22368 sprintf (savres_routine_name, "*%sVEC%s%.0d ; %s v%d-v31", prefix,
22369 (regno == 20 ? "" : "+"), (regno - 20) * 8, prefix, regno);
22370 else
22371 abort ();
22373 else
22374 sprintf (savres_routine_name, "%s%d%s", prefix, regno, suffix);
22376 return savres_routine_name;
22379 /* Return an RTL SYMBOL_REF for an out-of-line register save/restore routine.
22380 We are saving/restoring GPRs if GPR is true. */
22382 static rtx
22383 rs6000_savres_routine_sym (rs6000_stack_t *info, int sel)
22385 int regno = ((sel & SAVRES_REG) == SAVRES_GPR
22386 ? info->first_gp_reg_save
22387 : (sel & SAVRES_REG) == SAVRES_FPR
22388 ? info->first_fp_reg_save - 32
22389 : (sel & SAVRES_REG) == SAVRES_VR
22390 ? info->first_altivec_reg_save - FIRST_ALTIVEC_REGNO
22391 : -1);
22392 rtx sym;
22393 int select = sel;
22395 /* On the SPE, we never have any FPRs, but we do have 32/64-bit
22396 versions of the gpr routines. */
22397 if (TARGET_SPE_ABI && (sel & SAVRES_REG) == SAVRES_GPR
22398 && info->spe_64bit_regs_used)
22399 select ^= SAVRES_FPR ^ SAVRES_GPR;
22401 /* Don't generate bogus routine names. */
22402 gcc_assert (FIRST_SAVRES_REGISTER <= regno
22403 && regno <= LAST_SAVRES_REGISTER
22404 && select >= 0 && select <= 12);
22406 sym = savres_routine_syms[regno-FIRST_SAVRES_REGISTER][select];
22408 if (sym == NULL)
22410 char *name;
22412 name = rs6000_savres_routine_name (info, regno, sel);
22414 sym = savres_routine_syms[regno-FIRST_SAVRES_REGISTER][select]
22415 = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
22416 SYMBOL_REF_FLAGS (sym) |= SYMBOL_FLAG_FUNCTION;
22419 return sym;
22422 /* Emit a sequence of insns, including a stack tie if needed, for
22423 resetting the stack pointer. If UPDT_REGNO is not 1, then don't
22424 reset the stack pointer, but move the base of the frame into
22425 reg UPDT_REGNO for use by out-of-line register restore routines. */
22427 static rtx
22428 rs6000_emit_stack_reset (rs6000_stack_t *info,
22429 rtx frame_reg_rtx, HOST_WIDE_INT frame_off,
22430 unsigned updt_regno)
22432 rtx updt_reg_rtx;
22434 /* This blockage is needed so that sched doesn't decide to move
22435 the sp change before the register restores. */
22436 if (DEFAULT_ABI == ABI_V4
22437 || (TARGET_SPE_ABI
22438 && info->spe_64bit_regs_used != 0
22439 && info->first_gp_reg_save != 32))
22440 rs6000_emit_stack_tie (frame_reg_rtx, frame_pointer_needed);
22442 /* If we are restoring registers out-of-line, we will be using the
22443 "exit" variants of the restore routines, which will reset the
22444 stack for us. But we do need to point updt_reg into the
22445 right place for those routines. */
22446 updt_reg_rtx = gen_rtx_REG (Pmode, updt_regno);
22448 if (frame_off != 0)
22449 return emit_insn (gen_add3_insn (updt_reg_rtx,
22450 frame_reg_rtx, GEN_INT (frame_off)));
22451 else if (REGNO (frame_reg_rtx) != updt_regno)
22452 return emit_move_insn (updt_reg_rtx, frame_reg_rtx);
22454 return NULL_RTX;
22457 /* Return the register number used as a pointer by out-of-line
22458 save/restore functions. */
22460 static inline unsigned
22461 ptr_regno_for_savres (int sel)
22463 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
22464 return (sel & SAVRES_REG) == SAVRES_FPR || (sel & SAVRES_LR) ? 1 : 12;
22465 return DEFAULT_ABI == ABI_DARWIN && (sel & SAVRES_REG) == SAVRES_FPR ? 1 : 11;
22468 /* Construct a parallel rtx describing the effect of a call to an
22469 out-of-line register save/restore routine, and emit the insn
22470 or jump_insn as appropriate. */
22472 static rtx
22473 rs6000_emit_savres_rtx (rs6000_stack_t *info,
22474 rtx frame_reg_rtx, int save_area_offset, int lr_offset,
22475 enum machine_mode reg_mode, int sel)
22477 int i;
22478 int offset, start_reg, end_reg, n_regs, use_reg;
22479 int reg_size = GET_MODE_SIZE (reg_mode);
22480 rtx sym;
22481 rtvec p;
22482 rtx par, insn;
22484 offset = 0;
22485 start_reg = ((sel & SAVRES_REG) == SAVRES_GPR
22486 ? info->first_gp_reg_save
22487 : (sel & SAVRES_REG) == SAVRES_FPR
22488 ? info->first_fp_reg_save
22489 : (sel & SAVRES_REG) == SAVRES_VR
22490 ? info->first_altivec_reg_save
22491 : -1);
22492 end_reg = ((sel & SAVRES_REG) == SAVRES_GPR
22493 ? 32
22494 : (sel & SAVRES_REG) == SAVRES_FPR
22495 ? 64
22496 : (sel & SAVRES_REG) == SAVRES_VR
22497 ? LAST_ALTIVEC_REGNO + 1
22498 : -1);
22499 n_regs = end_reg - start_reg;
22500 p = rtvec_alloc (3 + ((sel & SAVRES_LR) ? 1 : 0)
22501 + ((sel & SAVRES_REG) == SAVRES_VR ? 1 : 0)
22502 + n_regs);
22504 if (!(sel & SAVRES_SAVE) && (sel & SAVRES_LR))
22505 RTVEC_ELT (p, offset++) = ret_rtx;
22507 RTVEC_ELT (p, offset++)
22508 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, LR_REGNO));
22510 sym = rs6000_savres_routine_sym (info, sel);
22511 RTVEC_ELT (p, offset++) = gen_rtx_USE (VOIDmode, sym);
22513 use_reg = ptr_regno_for_savres (sel);
22514 if ((sel & SAVRES_REG) == SAVRES_VR)
22516 /* Vector regs are saved/restored using [reg+reg] addressing. */
22517 RTVEC_ELT (p, offset++)
22518 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, use_reg));
22519 RTVEC_ELT (p, offset++)
22520 = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, 0));
22522 else
22523 RTVEC_ELT (p, offset++)
22524 = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, use_reg));
22526 for (i = 0; i < end_reg - start_reg; i++)
22527 RTVEC_ELT (p, i + offset)
22528 = gen_frame_set (gen_rtx_REG (reg_mode, start_reg + i),
22529 frame_reg_rtx, save_area_offset + reg_size * i,
22530 (sel & SAVRES_SAVE) != 0);
22532 if ((sel & SAVRES_SAVE) && (sel & SAVRES_LR))
22533 RTVEC_ELT (p, i + offset)
22534 = gen_frame_store (gen_rtx_REG (Pmode, 0), frame_reg_rtx, lr_offset);
22536 par = gen_rtx_PARALLEL (VOIDmode, p);
22538 if (!(sel & SAVRES_SAVE) && (sel & SAVRES_LR))
22540 insn = emit_jump_insn (par);
22541 JUMP_LABEL (insn) = ret_rtx;
22543 else
22544 insn = emit_insn (par);
22545 return insn;
22548 /* Emit code to store CR fields that need to be saved into REG. */
22550 static void
22551 rs6000_emit_move_from_cr (rtx reg)
22553 /* Only the ELFv2 ABI allows storing only selected fields. */
22554 if (DEFAULT_ABI == ABI_ELFv2 && TARGET_MFCRF)
22556 int i, cr_reg[8], count = 0;
22558 /* Collect CR fields that must be saved. */
22559 for (i = 0; i < 8; i++)
22560 if (save_reg_p (CR0_REGNO + i))
22561 cr_reg[count++] = i;
22563 /* If it's just a single one, use mfcrf. */
22564 if (count == 1)
22566 rtvec p = rtvec_alloc (1);
22567 rtvec r = rtvec_alloc (2);
22568 RTVEC_ELT (r, 0) = gen_rtx_REG (CCmode, CR0_REGNO + cr_reg[0]);
22569 RTVEC_ELT (r, 1) = GEN_INT (1 << (7 - cr_reg[0]));
22570 RTVEC_ELT (p, 0)
22571 = gen_rtx_SET (VOIDmode, reg,
22572 gen_rtx_UNSPEC (SImode, r, UNSPEC_MOVESI_FROM_CR));
22574 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
22575 return;
22578 /* ??? It might be better to handle count == 2 / 3 cases here
22579 as well, using logical operations to combine the values. */
22582 emit_insn (gen_movesi_from_cr (reg));
22585 /* Determine whether the gp REG is really used. */
22587 static bool
22588 rs6000_reg_live_or_pic_offset_p (int reg)
22590 /* If the function calls eh_return, claim used all the registers that would
22591 be checked for liveness otherwise. This is required for the PIC offset
22592 register with -mminimal-toc on AIX, as it is advertised as "fixed" for
22593 register allocation purposes in this case. */
22595 return (((crtl->calls_eh_return || df_regs_ever_live_p (reg))
22596 && (!call_used_regs[reg]
22597 || (reg == RS6000_PIC_OFFSET_TABLE_REGNUM
22598 && !TARGET_SINGLE_PIC_BASE
22599 && TARGET_TOC && TARGET_MINIMAL_TOC)))
22600 || (reg == RS6000_PIC_OFFSET_TABLE_REGNUM
22601 && !TARGET_SINGLE_PIC_BASE
22602 && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
22603 || (DEFAULT_ABI == ABI_DARWIN && flag_pic))));
22606 /* Emit function prologue as insns. */
22608 void
22609 rs6000_emit_prologue (void)
22611 rs6000_stack_t *info = rs6000_stack_info ();
22612 enum machine_mode reg_mode = Pmode;
22613 int reg_size = TARGET_32BIT ? 4 : 8;
22614 rtx sp_reg_rtx = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
22615 rtx frame_reg_rtx = sp_reg_rtx;
22616 unsigned int cr_save_regno;
22617 rtx cr_save_rtx = NULL_RTX;
22618 rtx insn;
22619 int strategy;
22620 int using_static_chain_p = (cfun->static_chain_decl != NULL_TREE
22621 && df_regs_ever_live_p (STATIC_CHAIN_REGNUM)
22622 && call_used_regs[STATIC_CHAIN_REGNUM]);
22623 /* Offset to top of frame for frame_reg and sp respectively. */
22624 HOST_WIDE_INT frame_off = 0;
22625 HOST_WIDE_INT sp_off = 0;
22627 #ifdef ENABLE_CHECKING
22628 /* Track and check usage of r0, r11, r12. */
22629 int reg_inuse = using_static_chain_p ? 1 << 11 : 0;
22630 #define START_USE(R) do \
22632 gcc_assert ((reg_inuse & (1 << (R))) == 0); \
22633 reg_inuse |= 1 << (R); \
22634 } while (0)
22635 #define END_USE(R) do \
22637 gcc_assert ((reg_inuse & (1 << (R))) != 0); \
22638 reg_inuse &= ~(1 << (R)); \
22639 } while (0)
22640 #define NOT_INUSE(R) do \
22642 gcc_assert ((reg_inuse & (1 << (R))) == 0); \
22643 } while (0)
22644 #else
22645 #define START_USE(R) do {} while (0)
22646 #define END_USE(R) do {} while (0)
22647 #define NOT_INUSE(R) do {} while (0)
22648 #endif
22650 if (DEFAULT_ABI == ABI_ELFv2)
22652 cfun->machine->r2_setup_needed = df_regs_ever_live_p (TOC_REGNUM);
22654 /* With -mminimal-toc we may generate an extra use of r2 below. */
22655 if (!TARGET_SINGLE_PIC_BASE
22656 && TARGET_TOC && TARGET_MINIMAL_TOC && get_pool_size () != 0)
22657 cfun->machine->r2_setup_needed = true;
22661 if (flag_stack_usage_info)
22662 current_function_static_stack_size = info->total_size;
22664 if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
22666 HOST_WIDE_INT size = info->total_size;
22668 if (crtl->is_leaf && !cfun->calls_alloca)
22670 if (size > PROBE_INTERVAL && size > STACK_CHECK_PROTECT)
22671 rs6000_emit_probe_stack_range (STACK_CHECK_PROTECT,
22672 size - STACK_CHECK_PROTECT);
22674 else if (size > 0)
22675 rs6000_emit_probe_stack_range (STACK_CHECK_PROTECT, size);
22678 if (TARGET_FIX_AND_CONTINUE)
22680 /* gdb on darwin arranges to forward a function from the old
22681 address by modifying the first 5 instructions of the function
22682 to branch to the overriding function. This is necessary to
22683 permit function pointers that point to the old function to
22684 actually forward to the new function. */
22685 emit_insn (gen_nop ());
22686 emit_insn (gen_nop ());
22687 emit_insn (gen_nop ());
22688 emit_insn (gen_nop ());
22689 emit_insn (gen_nop ());
22692 if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
22694 reg_mode = V2SImode;
22695 reg_size = 8;
22698 /* Handle world saves specially here. */
22699 if (WORLD_SAVE_P (info))
22701 int i, j, sz;
22702 rtx treg;
22703 rtvec p;
22704 rtx reg0;
22706 /* save_world expects lr in r0. */
22707 reg0 = gen_rtx_REG (Pmode, 0);
22708 if (info->lr_save_p)
22710 insn = emit_move_insn (reg0,
22711 gen_rtx_REG (Pmode, LR_REGNO));
22712 RTX_FRAME_RELATED_P (insn) = 1;
22715 /* The SAVE_WORLD and RESTORE_WORLD routines make a number of
22716 assumptions about the offsets of various bits of the stack
22717 frame. */
22718 gcc_assert (info->gp_save_offset == -220
22719 && info->fp_save_offset == -144
22720 && info->lr_save_offset == 8
22721 && info->cr_save_offset == 4
22722 && info->push_p
22723 && info->lr_save_p
22724 && (!crtl->calls_eh_return
22725 || info->ehrd_offset == -432)
22726 && info->vrsave_save_offset == -224
22727 && info->altivec_save_offset == -416);
22729 treg = gen_rtx_REG (SImode, 11);
22730 emit_move_insn (treg, GEN_INT (-info->total_size));
22732 /* SAVE_WORLD takes the caller's LR in R0 and the frame size
22733 in R11. It also clobbers R12, so beware! */
22735 /* Preserve CR2 for save_world prologues */
22736 sz = 5;
22737 sz += 32 - info->first_gp_reg_save;
22738 sz += 64 - info->first_fp_reg_save;
22739 sz += LAST_ALTIVEC_REGNO - info->first_altivec_reg_save + 1;
22740 p = rtvec_alloc (sz);
22741 j = 0;
22742 RTVEC_ELT (p, j++) = gen_rtx_CLOBBER (VOIDmode,
22743 gen_rtx_REG (SImode,
22744 LR_REGNO));
22745 RTVEC_ELT (p, j++) = gen_rtx_USE (VOIDmode,
22746 gen_rtx_SYMBOL_REF (Pmode,
22747 "*save_world"));
22748 /* We do floats first so that the instruction pattern matches
22749 properly. */
22750 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
22751 RTVEC_ELT (p, j++)
22752 = gen_frame_store (gen_rtx_REG (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
22753 ? DFmode : SFmode,
22754 info->first_fp_reg_save + i),
22755 frame_reg_rtx,
22756 info->fp_save_offset + frame_off + 8 * i);
22757 for (i = 0; info->first_altivec_reg_save + i <= LAST_ALTIVEC_REGNO; i++)
22758 RTVEC_ELT (p, j++)
22759 = gen_frame_store (gen_rtx_REG (V4SImode,
22760 info->first_altivec_reg_save + i),
22761 frame_reg_rtx,
22762 info->altivec_save_offset + frame_off + 16 * i);
22763 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
22764 RTVEC_ELT (p, j++)
22765 = gen_frame_store (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
22766 frame_reg_rtx,
22767 info->gp_save_offset + frame_off + reg_size * i);
22769 /* CR register traditionally saved as CR2. */
22770 RTVEC_ELT (p, j++)
22771 = gen_frame_store (gen_rtx_REG (SImode, CR2_REGNO),
22772 frame_reg_rtx, info->cr_save_offset + frame_off);
22773 /* Explain about use of R0. */
22774 if (info->lr_save_p)
22775 RTVEC_ELT (p, j++)
22776 = gen_frame_store (reg0,
22777 frame_reg_rtx, info->lr_save_offset + frame_off);
22778 /* Explain what happens to the stack pointer. */
22780 rtx newval = gen_rtx_PLUS (Pmode, sp_reg_rtx, treg);
22781 RTVEC_ELT (p, j++) = gen_rtx_SET (VOIDmode, sp_reg_rtx, newval);
22784 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
22785 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
22786 treg, GEN_INT (-info->total_size), NULL_RTX);
22787 sp_off = frame_off = info->total_size;
22790 strategy = info->savres_strategy;
22792 /* For V.4, update stack before we do any saving and set back pointer. */
22793 if (! WORLD_SAVE_P (info)
22794 && info->push_p
22795 && (DEFAULT_ABI == ABI_V4
22796 || crtl->calls_eh_return))
22798 bool need_r11 = (TARGET_SPE
22799 ? (!(strategy & SAVE_INLINE_GPRS)
22800 && info->spe_64bit_regs_used == 0)
22801 : (!(strategy & SAVE_INLINE_FPRS)
22802 || !(strategy & SAVE_INLINE_GPRS)
22803 || !(strategy & SAVE_INLINE_VRS)));
22804 int ptr_regno = -1;
22805 rtx ptr_reg = NULL_RTX;
22806 int ptr_off = 0;
22808 if (info->total_size < 32767)
22809 frame_off = info->total_size;
22810 else if (need_r11)
22811 ptr_regno = 11;
22812 else if (info->cr_save_p
22813 || info->lr_save_p
22814 || info->first_fp_reg_save < 64
22815 || info->first_gp_reg_save < 32
22816 || info->altivec_size != 0
22817 || info->vrsave_mask != 0
22818 || crtl->calls_eh_return)
22819 ptr_regno = 12;
22820 else
22822 /* The prologue won't be saving any regs so there is no need
22823 to set up a frame register to access any frame save area.
22824 We also won't be using frame_off anywhere below, but set
22825 the correct value anyway to protect against future
22826 changes to this function. */
22827 frame_off = info->total_size;
22829 if (ptr_regno != -1)
22831 /* Set up the frame offset to that needed by the first
22832 out-of-line save function. */
22833 START_USE (ptr_regno);
22834 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
22835 frame_reg_rtx = ptr_reg;
22836 if (!(strategy & SAVE_INLINE_FPRS) && info->fp_size != 0)
22837 gcc_checking_assert (info->fp_save_offset + info->fp_size == 0);
22838 else if (!(strategy & SAVE_INLINE_GPRS) && info->first_gp_reg_save < 32)
22839 ptr_off = info->gp_save_offset + info->gp_size;
22840 else if (!(strategy & SAVE_INLINE_VRS) && info->altivec_size != 0)
22841 ptr_off = info->altivec_save_offset + info->altivec_size;
22842 frame_off = -ptr_off;
22844 rs6000_emit_allocate_stack (info->total_size, ptr_reg, ptr_off);
22845 sp_off = info->total_size;
22846 if (frame_reg_rtx != sp_reg_rtx)
22847 rs6000_emit_stack_tie (frame_reg_rtx, false);
22850 /* If we use the link register, get it into r0. */
22851 if (!WORLD_SAVE_P (info) && info->lr_save_p)
22853 rtx addr, reg, mem;
22855 reg = gen_rtx_REG (Pmode, 0);
22856 START_USE (0);
22857 insn = emit_move_insn (reg, gen_rtx_REG (Pmode, LR_REGNO));
22858 RTX_FRAME_RELATED_P (insn) = 1;
22860 if (!(strategy & (SAVE_NOINLINE_GPRS_SAVES_LR
22861 | SAVE_NOINLINE_FPRS_SAVES_LR)))
22863 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
22864 GEN_INT (info->lr_save_offset + frame_off));
22865 mem = gen_rtx_MEM (Pmode, addr);
22866 /* This should not be of rs6000_sr_alias_set, because of
22867 __builtin_return_address. */
22869 insn = emit_move_insn (mem, reg);
22870 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
22871 NULL_RTX, NULL_RTX, NULL_RTX);
22872 END_USE (0);
22876 /* If we need to save CR, put it into r12 or r11. Choose r12 except when
22877 r12 will be needed by out-of-line gpr restore. */
22878 cr_save_regno = ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
22879 && !(strategy & (SAVE_INLINE_GPRS
22880 | SAVE_NOINLINE_GPRS_SAVES_LR))
22881 ? 11 : 12);
22882 if (!WORLD_SAVE_P (info)
22883 && info->cr_save_p
22884 && REGNO (frame_reg_rtx) != cr_save_regno
22885 && !(using_static_chain_p && cr_save_regno == 11))
22887 cr_save_rtx = gen_rtx_REG (SImode, cr_save_regno);
22888 START_USE (cr_save_regno);
22889 rs6000_emit_move_from_cr (cr_save_rtx);
22892 /* Do any required saving of fpr's. If only one or two to save, do
22893 it ourselves. Otherwise, call function. */
22894 if (!WORLD_SAVE_P (info) && (strategy & SAVE_INLINE_FPRS))
22896 int i;
22897 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
22898 if (save_reg_p (info->first_fp_reg_save + i))
22899 emit_frame_save (frame_reg_rtx,
22900 (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
22901 ? DFmode : SFmode),
22902 info->first_fp_reg_save + i,
22903 info->fp_save_offset + frame_off + 8 * i,
22904 sp_off - frame_off);
22906 else if (!WORLD_SAVE_P (info) && info->first_fp_reg_save != 64)
22908 bool lr = (strategy & SAVE_NOINLINE_FPRS_SAVES_LR) != 0;
22909 int sel = SAVRES_SAVE | SAVRES_FPR | (lr ? SAVRES_LR : 0);
22910 unsigned ptr_regno = ptr_regno_for_savres (sel);
22911 rtx ptr_reg = frame_reg_rtx;
22913 if (REGNO (frame_reg_rtx) == ptr_regno)
22914 gcc_checking_assert (frame_off == 0);
22915 else
22917 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
22918 NOT_INUSE (ptr_regno);
22919 emit_insn (gen_add3_insn (ptr_reg,
22920 frame_reg_rtx, GEN_INT (frame_off)));
22922 insn = rs6000_emit_savres_rtx (info, ptr_reg,
22923 info->fp_save_offset,
22924 info->lr_save_offset,
22925 DFmode, sel);
22926 rs6000_frame_related (insn, ptr_reg, sp_off,
22927 NULL_RTX, NULL_RTX, NULL_RTX);
22928 if (lr)
22929 END_USE (0);
22932 /* Save GPRs. This is done as a PARALLEL if we are using
22933 the store-multiple instructions. */
22934 if (!WORLD_SAVE_P (info)
22935 && TARGET_SPE_ABI
22936 && info->spe_64bit_regs_used != 0
22937 && info->first_gp_reg_save != 32)
22939 int i;
22940 rtx spe_save_area_ptr;
22941 HOST_WIDE_INT save_off;
22942 int ool_adjust = 0;
22944 /* Determine whether we can address all of the registers that need
22945 to be saved with an offset from frame_reg_rtx that fits in
22946 the small const field for SPE memory instructions. */
22947 int spe_regs_addressable
22948 = (SPE_CONST_OFFSET_OK (info->spe_gp_save_offset + frame_off
22949 + reg_size * (32 - info->first_gp_reg_save - 1))
22950 && (strategy & SAVE_INLINE_GPRS));
22952 if (spe_regs_addressable)
22954 spe_save_area_ptr = frame_reg_rtx;
22955 save_off = frame_off;
22957 else
22959 /* Make r11 point to the start of the SPE save area. We need
22960 to be careful here if r11 is holding the static chain. If
22961 it is, then temporarily save it in r0. */
22962 HOST_WIDE_INT offset;
22964 if (!(strategy & SAVE_INLINE_GPRS))
22965 ool_adjust = 8 * (info->first_gp_reg_save - FIRST_SAVED_GP_REGNO);
22966 offset = info->spe_gp_save_offset + frame_off - ool_adjust;
22967 spe_save_area_ptr = gen_rtx_REG (Pmode, 11);
22968 save_off = frame_off - offset;
22970 if (using_static_chain_p)
22972 rtx r0 = gen_rtx_REG (Pmode, 0);
22974 START_USE (0);
22975 gcc_assert (info->first_gp_reg_save > 11);
22977 emit_move_insn (r0, spe_save_area_ptr);
22979 else if (REGNO (frame_reg_rtx) != 11)
22980 START_USE (11);
22982 emit_insn (gen_addsi3 (spe_save_area_ptr,
22983 frame_reg_rtx, GEN_INT (offset)));
22984 if (!using_static_chain_p && REGNO (frame_reg_rtx) == 11)
22985 frame_off = -info->spe_gp_save_offset + ool_adjust;
22988 if ((strategy & SAVE_INLINE_GPRS))
22990 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
22991 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
22992 emit_frame_save (spe_save_area_ptr, reg_mode,
22993 info->first_gp_reg_save + i,
22994 (info->spe_gp_save_offset + save_off
22995 + reg_size * i),
22996 sp_off - save_off);
22998 else
23000 insn = rs6000_emit_savres_rtx (info, spe_save_area_ptr,
23001 info->spe_gp_save_offset + save_off,
23002 0, reg_mode,
23003 SAVRES_SAVE | SAVRES_GPR);
23005 rs6000_frame_related (insn, spe_save_area_ptr, sp_off - save_off,
23006 NULL_RTX, NULL_RTX, NULL_RTX);
23009 /* Move the static chain pointer back. */
23010 if (!spe_regs_addressable)
23012 if (using_static_chain_p)
23014 emit_move_insn (spe_save_area_ptr, gen_rtx_REG (Pmode, 0));
23015 END_USE (0);
23017 else if (REGNO (frame_reg_rtx) != 11)
23018 END_USE (11);
23021 else if (!WORLD_SAVE_P (info) && !(strategy & SAVE_INLINE_GPRS))
23023 bool lr = (strategy & SAVE_NOINLINE_GPRS_SAVES_LR) != 0;
23024 int sel = SAVRES_SAVE | SAVRES_GPR | (lr ? SAVRES_LR : 0);
23025 unsigned ptr_regno = ptr_regno_for_savres (sel);
23026 rtx ptr_reg = frame_reg_rtx;
23027 bool ptr_set_up = REGNO (ptr_reg) == ptr_regno;
23028 int end_save = info->gp_save_offset + info->gp_size;
23029 int ptr_off;
23031 if (!ptr_set_up)
23032 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
23034 /* Need to adjust r11 (r12) if we saved any FPRs. */
23035 if (end_save + frame_off != 0)
23037 rtx offset = GEN_INT (end_save + frame_off);
23039 if (ptr_set_up)
23040 frame_off = -end_save;
23041 else
23042 NOT_INUSE (ptr_regno);
23043 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
23045 else if (!ptr_set_up)
23047 NOT_INUSE (ptr_regno);
23048 emit_move_insn (ptr_reg, frame_reg_rtx);
23050 ptr_off = -end_save;
23051 insn = rs6000_emit_savres_rtx (info, ptr_reg,
23052 info->gp_save_offset + ptr_off,
23053 info->lr_save_offset + ptr_off,
23054 reg_mode, sel);
23055 rs6000_frame_related (insn, ptr_reg, sp_off - ptr_off,
23056 NULL_RTX, NULL_RTX, NULL_RTX);
23057 if (lr)
23058 END_USE (0);
23060 else if (!WORLD_SAVE_P (info) && (strategy & SAVRES_MULTIPLE))
23062 rtvec p;
23063 int i;
23064 p = rtvec_alloc (32 - info->first_gp_reg_save);
23065 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
23066 RTVEC_ELT (p, i)
23067 = gen_frame_store (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
23068 frame_reg_rtx,
23069 info->gp_save_offset + frame_off + reg_size * i);
23070 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
23071 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
23072 NULL_RTX, NULL_RTX, NULL_RTX);
23074 else if (!WORLD_SAVE_P (info))
23076 int i;
23077 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
23078 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
23079 emit_frame_save (frame_reg_rtx, reg_mode,
23080 info->first_gp_reg_save + i,
23081 info->gp_save_offset + frame_off + reg_size * i,
23082 sp_off - frame_off);
23085 if (crtl->calls_eh_return)
23087 unsigned int i;
23088 rtvec p;
23090 for (i = 0; ; ++i)
23092 unsigned int regno = EH_RETURN_DATA_REGNO (i);
23093 if (regno == INVALID_REGNUM)
23094 break;
23097 p = rtvec_alloc (i);
23099 for (i = 0; ; ++i)
23101 unsigned int regno = EH_RETURN_DATA_REGNO (i);
23102 if (regno == INVALID_REGNUM)
23103 break;
23105 insn
23106 = gen_frame_store (gen_rtx_REG (reg_mode, regno),
23107 sp_reg_rtx,
23108 info->ehrd_offset + sp_off + reg_size * (int) i);
23109 RTVEC_ELT (p, i) = insn;
23110 RTX_FRAME_RELATED_P (insn) = 1;
23113 insn = emit_insn (gen_blockage ());
23114 RTX_FRAME_RELATED_P (insn) = 1;
23115 add_reg_note (insn, REG_FRAME_RELATED_EXPR, gen_rtx_PARALLEL (VOIDmode, p));
23118 /* In AIX ABI we need to make sure r2 is really saved. */
23119 if (TARGET_AIX && crtl->calls_eh_return)
23121 rtx tmp_reg, tmp_reg_si, hi, lo, compare_result, toc_save_done, jump;
23122 rtx save_insn, join_insn, note;
23123 long toc_restore_insn;
23125 tmp_reg = gen_rtx_REG (Pmode, 11);
23126 tmp_reg_si = gen_rtx_REG (SImode, 11);
23127 if (using_static_chain_p)
23129 START_USE (0);
23130 emit_move_insn (gen_rtx_REG (Pmode, 0), tmp_reg);
23132 else
23133 START_USE (11);
23134 emit_move_insn (tmp_reg, gen_rtx_REG (Pmode, LR_REGNO));
23135 /* Peek at instruction to which this function returns. If it's
23136 restoring r2, then we know we've already saved r2. We can't
23137 unconditionally save r2 because the value we have will already
23138 be updated if we arrived at this function via a plt call or
23139 toc adjusting stub. */
23140 emit_move_insn (tmp_reg_si, gen_rtx_MEM (SImode, tmp_reg));
23141 toc_restore_insn = ((TARGET_32BIT ? 0x80410000 : 0xE8410000)
23142 + RS6000_TOC_SAVE_SLOT);
23143 hi = gen_int_mode (toc_restore_insn & ~0xffff, SImode);
23144 emit_insn (gen_xorsi3 (tmp_reg_si, tmp_reg_si, hi));
23145 compare_result = gen_rtx_REG (CCUNSmode, CR0_REGNO);
23146 validate_condition_mode (EQ, CCUNSmode);
23147 lo = gen_int_mode (toc_restore_insn & 0xffff, SImode);
23148 emit_insn (gen_rtx_SET (VOIDmode, compare_result,
23149 gen_rtx_COMPARE (CCUNSmode, tmp_reg_si, lo)));
23150 toc_save_done = gen_label_rtx ();
23151 jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
23152 gen_rtx_EQ (VOIDmode, compare_result,
23153 const0_rtx),
23154 gen_rtx_LABEL_REF (VOIDmode, toc_save_done),
23155 pc_rtx);
23156 jump = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, jump));
23157 JUMP_LABEL (jump) = toc_save_done;
23158 LABEL_NUSES (toc_save_done) += 1;
23160 save_insn = emit_frame_save (frame_reg_rtx, reg_mode,
23161 TOC_REGNUM, frame_off + RS6000_TOC_SAVE_SLOT,
23162 sp_off - frame_off);
23164 emit_label (toc_save_done);
23166 /* ??? If we leave SAVE_INSN as marked as saving R2, then we'll
23167 have a CFG that has different saves along different paths.
23168 Move the note to a dummy blockage insn, which describes that
23169 R2 is unconditionally saved after the label. */
23170 /* ??? An alternate representation might be a special insn pattern
23171 containing both the branch and the store. That might let the
23172 code that minimizes the number of DW_CFA_advance opcodes better
23173 freedom in placing the annotations. */
23174 note = find_reg_note (save_insn, REG_FRAME_RELATED_EXPR, NULL);
23175 if (note)
23176 remove_note (save_insn, note);
23177 else
23178 note = alloc_reg_note (REG_FRAME_RELATED_EXPR,
23179 copy_rtx (PATTERN (save_insn)), NULL_RTX);
23180 RTX_FRAME_RELATED_P (save_insn) = 0;
23182 join_insn = emit_insn (gen_blockage ());
23183 REG_NOTES (join_insn) = note;
23184 RTX_FRAME_RELATED_P (join_insn) = 1;
23186 if (using_static_chain_p)
23188 emit_move_insn (tmp_reg, gen_rtx_REG (Pmode, 0));
23189 END_USE (0);
23191 else
23192 END_USE (11);
23195 /* Save CR if we use any that must be preserved. */
23196 if (!WORLD_SAVE_P (info) && info->cr_save_p)
23198 rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
23199 GEN_INT (info->cr_save_offset + frame_off));
23200 rtx mem = gen_frame_mem (SImode, addr);
23202 /* If we didn't copy cr before, do so now using r0. */
23203 if (cr_save_rtx == NULL_RTX)
23205 START_USE (0);
23206 cr_save_rtx = gen_rtx_REG (SImode, 0);
23207 rs6000_emit_move_from_cr (cr_save_rtx);
23210 /* Saving CR requires a two-instruction sequence: one instruction
23211 to move the CR to a general-purpose register, and a second
23212 instruction that stores the GPR to memory.
23214 We do not emit any DWARF CFI records for the first of these,
23215 because we cannot properly represent the fact that CR is saved in
23216 a register. One reason is that we cannot express that multiple
23217 CR fields are saved; another reason is that on 64-bit, the size
23218 of the CR register in DWARF (4 bytes) differs from the size of
23219 a general-purpose register.
23221 This means if any intervening instruction were to clobber one of
23222 the call-saved CR fields, we'd have incorrect CFI. To prevent
23223 this from happening, we mark the store to memory as a use of
23224 those CR fields, which prevents any such instruction from being
23225 scheduled in between the two instructions. */
23226 rtx crsave_v[9];
23227 int n_crsave = 0;
23228 int i;
23230 crsave_v[n_crsave++] = gen_rtx_SET (VOIDmode, mem, cr_save_rtx);
23231 for (i = 0; i < 8; i++)
23232 if (save_reg_p (CR0_REGNO + i))
23233 crsave_v[n_crsave++]
23234 = gen_rtx_USE (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO + i));
23236 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode,
23237 gen_rtvec_v (n_crsave, crsave_v)));
23238 END_USE (REGNO (cr_save_rtx));
23240 /* Now, there's no way that dwarf2out_frame_debug_expr is going to
23241 understand '(unspec:SI [(reg:CC 68) ...] UNSPEC_MOVESI_FROM_CR)',
23242 so we need to construct a frame expression manually. */
23243 RTX_FRAME_RELATED_P (insn) = 1;
23245 /* Update address to be stack-pointer relative, like
23246 rs6000_frame_related would do. */
23247 addr = gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode, STACK_POINTER_REGNUM),
23248 GEN_INT (info->cr_save_offset + sp_off));
23249 mem = gen_frame_mem (SImode, addr);
23251 if (DEFAULT_ABI == ABI_ELFv2)
23253 /* In the ELFv2 ABI we generate separate CFI records for each
23254 CR field that was actually saved. They all point to the
23255 same 32-bit stack slot. */
23256 rtx crframe[8];
23257 int n_crframe = 0;
23259 for (i = 0; i < 8; i++)
23260 if (save_reg_p (CR0_REGNO + i))
23262 crframe[n_crframe]
23263 = gen_rtx_SET (VOIDmode, mem,
23264 gen_rtx_REG (SImode, CR0_REGNO + i));
23266 RTX_FRAME_RELATED_P (crframe[n_crframe]) = 1;
23267 n_crframe++;
23270 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
23271 gen_rtx_PARALLEL (VOIDmode,
23272 gen_rtvec_v (n_crframe, crframe)));
23274 else
23276 /* In other ABIs, by convention, we use a single CR regnum to
23277 represent the fact that all call-saved CR fields are saved.
23278 We use CR2_REGNO to be compatible with gcc-2.95 on Linux. */
23279 rtx set = gen_rtx_SET (VOIDmode, mem,
23280 gen_rtx_REG (SImode, CR2_REGNO));
23281 add_reg_note (insn, REG_FRAME_RELATED_EXPR, set);
23285 /* In the ELFv2 ABI we need to save all call-saved CR fields into
23286 *separate* slots if the routine calls __builtin_eh_return, so
23287 that they can be independently restored by the unwinder. */
23288 if (DEFAULT_ABI == ABI_ELFv2 && crtl->calls_eh_return)
23290 int i, cr_off = info->ehcr_offset;
23291 rtx crsave;
23293 /* ??? We might get better performance by using multiple mfocrf
23294 instructions. */
23295 crsave = gen_rtx_REG (SImode, 0);
23296 emit_insn (gen_movesi_from_cr (crsave));
23298 for (i = 0; i < 8; i++)
23299 if (!call_used_regs[CR0_REGNO + i])
23301 rtvec p = rtvec_alloc (2);
23302 RTVEC_ELT (p, 0)
23303 = gen_frame_store (crsave, frame_reg_rtx, cr_off + frame_off);
23304 RTVEC_ELT (p, 1)
23305 = gen_rtx_USE (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO + i));
23307 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
23309 RTX_FRAME_RELATED_P (insn) = 1;
23310 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
23311 gen_frame_store (gen_rtx_REG (SImode, CR0_REGNO + i),
23312 sp_reg_rtx, cr_off + sp_off));
23314 cr_off += reg_size;
23318 /* Update stack and set back pointer unless this is V.4,
23319 for which it was done previously. */
23320 if (!WORLD_SAVE_P (info) && info->push_p
23321 && !(DEFAULT_ABI == ABI_V4 || crtl->calls_eh_return))
23323 rtx ptr_reg = NULL;
23324 int ptr_off = 0;
23326 /* If saving altivec regs we need to be able to address all save
23327 locations using a 16-bit offset. */
23328 if ((strategy & SAVE_INLINE_VRS) == 0
23329 || (info->altivec_size != 0
23330 && (info->altivec_save_offset + info->altivec_size - 16
23331 + info->total_size - frame_off) > 32767)
23332 || (info->vrsave_size != 0
23333 && (info->vrsave_save_offset
23334 + info->total_size - frame_off) > 32767))
23336 int sel = SAVRES_SAVE | SAVRES_VR;
23337 unsigned ptr_regno = ptr_regno_for_savres (sel);
23339 if (using_static_chain_p
23340 && ptr_regno == STATIC_CHAIN_REGNUM)
23341 ptr_regno = 12;
23342 if (REGNO (frame_reg_rtx) != ptr_regno)
23343 START_USE (ptr_regno);
23344 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
23345 frame_reg_rtx = ptr_reg;
23346 ptr_off = info->altivec_save_offset + info->altivec_size;
23347 frame_off = -ptr_off;
23349 else if (REGNO (frame_reg_rtx) == 1)
23350 frame_off = info->total_size;
23351 rs6000_emit_allocate_stack (info->total_size, ptr_reg, ptr_off);
23352 sp_off = info->total_size;
23353 if (frame_reg_rtx != sp_reg_rtx)
23354 rs6000_emit_stack_tie (frame_reg_rtx, false);
23357 /* Set frame pointer, if needed. */
23358 if (frame_pointer_needed)
23360 insn = emit_move_insn (gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
23361 sp_reg_rtx);
23362 RTX_FRAME_RELATED_P (insn) = 1;
23365 /* Save AltiVec registers if needed. Save here because the red zone does
23366 not always include AltiVec registers. */
23367 if (!WORLD_SAVE_P (info) && TARGET_ALTIVEC_ABI
23368 && info->altivec_size != 0 && (strategy & SAVE_INLINE_VRS) == 0)
23370 int end_save = info->altivec_save_offset + info->altivec_size;
23371 int ptr_off;
23372 /* Oddly, the vector save/restore functions point r0 at the end
23373 of the save area, then use r11 or r12 to load offsets for
23374 [reg+reg] addressing. */
23375 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
23376 int scratch_regno = ptr_regno_for_savres (SAVRES_SAVE | SAVRES_VR);
23377 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
23379 gcc_checking_assert (scratch_regno == 11 || scratch_regno == 12);
23380 NOT_INUSE (0);
23381 if (end_save + frame_off != 0)
23383 rtx offset = GEN_INT (end_save + frame_off);
23385 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
23387 else
23388 emit_move_insn (ptr_reg, frame_reg_rtx);
23390 ptr_off = -end_save;
23391 insn = rs6000_emit_savres_rtx (info, scratch_reg,
23392 info->altivec_save_offset + ptr_off,
23393 0, V4SImode, SAVRES_SAVE | SAVRES_VR);
23394 rs6000_frame_related (insn, scratch_reg, sp_off - ptr_off,
23395 NULL_RTX, NULL_RTX, NULL_RTX);
23396 if (REGNO (frame_reg_rtx) == REGNO (scratch_reg))
23398 /* The oddity mentioned above clobbered our frame reg. */
23399 emit_move_insn (frame_reg_rtx, ptr_reg);
23400 frame_off = ptr_off;
23403 else if (!WORLD_SAVE_P (info) && TARGET_ALTIVEC_ABI
23404 && info->altivec_size != 0)
23406 int i;
23408 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
23409 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
23411 rtx areg, savereg, mem, split_reg;
23412 int offset;
23414 offset = (info->altivec_save_offset + frame_off
23415 + 16 * (i - info->first_altivec_reg_save));
23417 savereg = gen_rtx_REG (V4SImode, i);
23419 NOT_INUSE (0);
23420 areg = gen_rtx_REG (Pmode, 0);
23421 emit_move_insn (areg, GEN_INT (offset));
23423 /* AltiVec addressing mode is [reg+reg]. */
23424 mem = gen_frame_mem (V4SImode,
23425 gen_rtx_PLUS (Pmode, frame_reg_rtx, areg));
23427 insn = emit_move_insn (mem, savereg);
23429 /* When we split a VSX store into two insns, we need to make
23430 sure the DWARF info knows which register we are storing.
23431 Pass it in to be used on the appropriate note. */
23432 if (!BYTES_BIG_ENDIAN
23433 && GET_CODE (PATTERN (insn)) == SET
23434 && GET_CODE (SET_SRC (PATTERN (insn))) == VEC_SELECT)
23435 split_reg = savereg;
23436 else
23437 split_reg = NULL_RTX;
23439 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
23440 areg, GEN_INT (offset), split_reg);
23444 /* VRSAVE is a bit vector representing which AltiVec registers
23445 are used. The OS uses this to determine which vector
23446 registers to save on a context switch. We need to save
23447 VRSAVE on the stack frame, add whatever AltiVec registers we
23448 used in this function, and do the corresponding magic in the
23449 epilogue. */
23451 if (!WORLD_SAVE_P (info)
23452 && TARGET_ALTIVEC
23453 && TARGET_ALTIVEC_VRSAVE
23454 && info->vrsave_mask != 0)
23456 rtx reg, vrsave;
23457 int offset;
23458 int save_regno;
23460 /* Get VRSAVE onto a GPR. Note that ABI_V4 and ABI_DARWIN might
23461 be using r12 as frame_reg_rtx and r11 as the static chain
23462 pointer for nested functions. */
23463 save_regno = 12;
23464 if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
23465 && !using_static_chain_p)
23466 save_regno = 11;
23467 else if (REGNO (frame_reg_rtx) == 12)
23469 save_regno = 11;
23470 if (using_static_chain_p)
23471 save_regno = 0;
23474 NOT_INUSE (save_regno);
23475 reg = gen_rtx_REG (SImode, save_regno);
23476 vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
23477 if (TARGET_MACHO)
23478 emit_insn (gen_get_vrsave_internal (reg));
23479 else
23480 emit_insn (gen_rtx_SET (VOIDmode, reg, vrsave));
23482 /* Save VRSAVE. */
23483 offset = info->vrsave_save_offset + frame_off;
23484 insn = emit_insn (gen_frame_store (reg, frame_reg_rtx, offset));
23486 /* Include the registers in the mask. */
23487 emit_insn (gen_iorsi3 (reg, reg, GEN_INT ((int) info->vrsave_mask)));
23489 insn = emit_insn (generate_set_vrsave (reg, info, 0));
23492 /* If we are using RS6000_PIC_OFFSET_TABLE_REGNUM, we need to set it up. */
23493 if (!TARGET_SINGLE_PIC_BASE
23494 && ((TARGET_TOC && TARGET_MINIMAL_TOC && get_pool_size () != 0)
23495 || (DEFAULT_ABI == ABI_V4
23496 && (flag_pic == 1 || (flag_pic && TARGET_SECURE_PLT))
23497 && df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))))
23499 /* If emit_load_toc_table will use the link register, we need to save
23500 it. We use R12 for this purpose because emit_load_toc_table
23501 can use register 0. This allows us to use a plain 'blr' to return
23502 from the procedure more often. */
23503 int save_LR_around_toc_setup = (TARGET_ELF
23504 && DEFAULT_ABI == ABI_V4
23505 && flag_pic
23506 && ! info->lr_save_p
23507 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds) > 0);
23508 if (save_LR_around_toc_setup)
23510 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
23511 rtx tmp = gen_rtx_REG (Pmode, 12);
23513 insn = emit_move_insn (tmp, lr);
23514 RTX_FRAME_RELATED_P (insn) = 1;
23516 rs6000_emit_load_toc_table (TRUE);
23518 insn = emit_move_insn (lr, tmp);
23519 add_reg_note (insn, REG_CFA_RESTORE, lr);
23520 RTX_FRAME_RELATED_P (insn) = 1;
23522 else
23523 rs6000_emit_load_toc_table (TRUE);
23526 #if TARGET_MACHO
23527 if (!TARGET_SINGLE_PIC_BASE
23528 && DEFAULT_ABI == ABI_DARWIN
23529 && flag_pic && crtl->uses_pic_offset_table)
23531 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
23532 rtx src = gen_rtx_SYMBOL_REF (Pmode, MACHOPIC_FUNCTION_BASE_NAME);
23534 /* Save and restore LR locally around this call (in R0). */
23535 if (!info->lr_save_p)
23536 emit_move_insn (gen_rtx_REG (Pmode, 0), lr);
23538 emit_insn (gen_load_macho_picbase (src));
23540 emit_move_insn (gen_rtx_REG (Pmode,
23541 RS6000_PIC_OFFSET_TABLE_REGNUM),
23542 lr);
23544 if (!info->lr_save_p)
23545 emit_move_insn (lr, gen_rtx_REG (Pmode, 0));
23547 #endif
23549 /* If we need to, save the TOC register after doing the stack setup.
23550 Do not emit eh frame info for this save. The unwinder wants info,
23551 conceptually attached to instructions in this function, about
23552 register values in the caller of this function. This R2 may have
23553 already been changed from the value in the caller.
23554 We don't attempt to write accurate DWARF EH frame info for R2
23555 because code emitted by gcc for a (non-pointer) function call
23556 doesn't save and restore R2. Instead, R2 is managed out-of-line
23557 by a linker generated plt call stub when the function resides in
23558 a shared library. This behaviour is costly to describe in DWARF,
23559 both in terms of the size of DWARF info and the time taken in the
23560 unwinder to interpret it. R2 changes, apart from the
23561 calls_eh_return case earlier in this function, are handled by
23562 linux-unwind.h frob_update_context. */
23563 if (rs6000_save_toc_in_prologue_p ())
23565 rtx reg = gen_rtx_REG (reg_mode, TOC_REGNUM);
23566 emit_insn (gen_frame_store (reg, sp_reg_rtx, RS6000_TOC_SAVE_SLOT));
23570 /* Write function prologue. */
23572 static void
23573 rs6000_output_function_prologue (FILE *file,
23574 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
23576 rs6000_stack_t *info = rs6000_stack_info ();
23578 if (TARGET_DEBUG_STACK)
23579 debug_stack_info (info);
23581 /* Write .extern for any function we will call to save and restore
23582 fp values. */
23583 if (info->first_fp_reg_save < 64
23584 && !TARGET_MACHO
23585 && !TARGET_ELF)
23587 char *name;
23588 int regno = info->first_fp_reg_save - 32;
23590 if ((info->savres_strategy & SAVE_INLINE_FPRS) == 0)
23592 bool lr = (info->savres_strategy & SAVE_NOINLINE_FPRS_SAVES_LR) != 0;
23593 int sel = SAVRES_SAVE | SAVRES_FPR | (lr ? SAVRES_LR : 0);
23594 name = rs6000_savres_routine_name (info, regno, sel);
23595 fprintf (file, "\t.extern %s\n", name);
23597 if ((info->savres_strategy & REST_INLINE_FPRS) == 0)
23599 bool lr = (info->savres_strategy
23600 & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
23601 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
23602 name = rs6000_savres_routine_name (info, regno, sel);
23603 fprintf (file, "\t.extern %s\n", name);
23607 /* ELFv2 ABI r2 setup code and local entry point. This must follow
23608 immediately after the global entry point label. */
23609 if (DEFAULT_ABI == ABI_ELFv2 && cfun->machine->r2_setup_needed)
23611 const char *name = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
23613 fprintf (file, "0:\taddis 2,12,.TOC.-0b@ha\n");
23614 fprintf (file, "\taddi 2,2,.TOC.-0b@l\n");
23616 fputs ("\t.localentry\t", file);
23617 assemble_name (file, name);
23618 fputs (",.-", file);
23619 assemble_name (file, name);
23620 fputs ("\n", file);
23623 /* Output -mprofile-kernel code. This needs to be done here instead of
23624 in output_function_profile since it must go after the ELFv2 ABI
23625 local entry point. */
23626 if (TARGET_PROFILE_KERNEL && crtl->profile)
23628 gcc_assert (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2);
23629 gcc_assert (!TARGET_32BIT);
23631 asm_fprintf (file, "\tmflr %s\n", reg_names[0]);
23632 asm_fprintf (file, "\tstd %s,16(%s)\n", reg_names[0], reg_names[1]);
23634 /* In the ELFv2 ABI we have no compiler stack word. It must be
23635 the resposibility of _mcount to preserve the static chain
23636 register if required. */
23637 if (DEFAULT_ABI != ABI_ELFv2
23638 && cfun->static_chain_decl != NULL)
23640 asm_fprintf (file, "\tstd %s,24(%s)\n",
23641 reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
23642 fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
23643 asm_fprintf (file, "\tld %s,24(%s)\n",
23644 reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
23646 else
23647 fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
23650 rs6000_pic_labelno++;
23653 /* Non-zero if vmx regs are restored before the frame pop, zero if
23654 we restore after the pop when possible. */
23655 #define ALWAYS_RESTORE_ALTIVEC_BEFORE_POP 0
23657 /* Restoring cr is a two step process: loading a reg from the frame
23658 save, then moving the reg to cr. For ABI_V4 we must let the
23659 unwinder know that the stack location is no longer valid at or
23660 before the stack deallocation, but we can't emit a cfa_restore for
23661 cr at the stack deallocation like we do for other registers.
23662 The trouble is that it is possible for the move to cr to be
23663 scheduled after the stack deallocation. So say exactly where cr
23664 is located on each of the two insns. */
23666 static rtx
23667 load_cr_save (int regno, rtx frame_reg_rtx, int offset, bool exit_func)
23669 rtx mem = gen_frame_mem_offset (SImode, frame_reg_rtx, offset);
23670 rtx reg = gen_rtx_REG (SImode, regno);
23671 rtx insn = emit_move_insn (reg, mem);
23673 if (!exit_func && DEFAULT_ABI == ABI_V4)
23675 rtx cr = gen_rtx_REG (SImode, CR2_REGNO);
23676 rtx set = gen_rtx_SET (VOIDmode, reg, cr);
23678 add_reg_note (insn, REG_CFA_REGISTER, set);
23679 RTX_FRAME_RELATED_P (insn) = 1;
23681 return reg;
23684 /* Reload CR from REG. */
23686 static void
23687 restore_saved_cr (rtx reg, int using_mfcr_multiple, bool exit_func)
23689 int count = 0;
23690 int i;
23692 if (using_mfcr_multiple)
23694 for (i = 0; i < 8; i++)
23695 if (save_reg_p (CR0_REGNO + i))
23696 count++;
23697 gcc_assert (count);
23700 if (using_mfcr_multiple && count > 1)
23702 rtx insn;
23703 rtvec p;
23704 int ndx;
23706 p = rtvec_alloc (count);
23708 ndx = 0;
23709 for (i = 0; i < 8; i++)
23710 if (save_reg_p (CR0_REGNO + i))
23712 rtvec r = rtvec_alloc (2);
23713 RTVEC_ELT (r, 0) = reg;
23714 RTVEC_ELT (r, 1) = GEN_INT (1 << (7-i));
23715 RTVEC_ELT (p, ndx) =
23716 gen_rtx_SET (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO + i),
23717 gen_rtx_UNSPEC (CCmode, r, UNSPEC_MOVESI_TO_CR));
23718 ndx++;
23720 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
23721 gcc_assert (ndx == count);
23723 /* For the ELFv2 ABI we generate a CFA_RESTORE for each
23724 CR field separately. */
23725 if (!exit_func && DEFAULT_ABI == ABI_ELFv2 && flag_shrink_wrap)
23727 for (i = 0; i < 8; i++)
23728 if (save_reg_p (CR0_REGNO + i))
23729 add_reg_note (insn, REG_CFA_RESTORE,
23730 gen_rtx_REG (SImode, CR0_REGNO + i));
23732 RTX_FRAME_RELATED_P (insn) = 1;
23735 else
23736 for (i = 0; i < 8; i++)
23737 if (save_reg_p (CR0_REGNO + i))
23739 rtx insn = emit_insn (gen_movsi_to_cr_one
23740 (gen_rtx_REG (CCmode, CR0_REGNO + i), reg));
23742 /* For the ELFv2 ABI we generate a CFA_RESTORE for each
23743 CR field separately, attached to the insn that in fact
23744 restores this particular CR field. */
23745 if (!exit_func && DEFAULT_ABI == ABI_ELFv2 && flag_shrink_wrap)
23747 add_reg_note (insn, REG_CFA_RESTORE,
23748 gen_rtx_REG (SImode, CR0_REGNO + i));
23750 RTX_FRAME_RELATED_P (insn) = 1;
23754 /* For other ABIs, we just generate a single CFA_RESTORE for CR2. */
23755 if (!exit_func && DEFAULT_ABI != ABI_ELFv2
23756 && (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap))
23758 rtx insn = get_last_insn ();
23759 rtx cr = gen_rtx_REG (SImode, CR2_REGNO);
23761 add_reg_note (insn, REG_CFA_RESTORE, cr);
23762 RTX_FRAME_RELATED_P (insn) = 1;
23766 /* Like cr, the move to lr instruction can be scheduled after the
23767 stack deallocation, but unlike cr, its stack frame save is still
23768 valid. So we only need to emit the cfa_restore on the correct
23769 instruction. */
23771 static void
23772 load_lr_save (int regno, rtx frame_reg_rtx, int offset)
23774 rtx mem = gen_frame_mem_offset (Pmode, frame_reg_rtx, offset);
23775 rtx reg = gen_rtx_REG (Pmode, regno);
23777 emit_move_insn (reg, mem);
23780 static void
23781 restore_saved_lr (int regno, bool exit_func)
23783 rtx reg = gen_rtx_REG (Pmode, regno);
23784 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
23785 rtx insn = emit_move_insn (lr, reg);
23787 if (!exit_func && flag_shrink_wrap)
23789 add_reg_note (insn, REG_CFA_RESTORE, lr);
23790 RTX_FRAME_RELATED_P (insn) = 1;
23794 static rtx
23795 add_crlr_cfa_restore (const rs6000_stack_t *info, rtx cfa_restores)
23797 if (DEFAULT_ABI == ABI_ELFv2)
23799 int i;
23800 for (i = 0; i < 8; i++)
23801 if (save_reg_p (CR0_REGNO + i))
23803 rtx cr = gen_rtx_REG (SImode, CR0_REGNO + i);
23804 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, cr,
23805 cfa_restores);
23808 else if (info->cr_save_p)
23809 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
23810 gen_rtx_REG (SImode, CR2_REGNO),
23811 cfa_restores);
23813 if (info->lr_save_p)
23814 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
23815 gen_rtx_REG (Pmode, LR_REGNO),
23816 cfa_restores);
23817 return cfa_restores;
23820 /* Return true if OFFSET from stack pointer can be clobbered by signals.
23821 V.4 doesn't have any stack cushion, AIX ABIs have 220 or 288 bytes
23822 below stack pointer not cloberred by signals. */
23824 static inline bool
23825 offset_below_red_zone_p (HOST_WIDE_INT offset)
23827 return offset < (DEFAULT_ABI == ABI_V4
23829 : TARGET_32BIT ? -220 : -288);
23832 /* Append CFA_RESTORES to any existing REG_NOTES on the last insn. */
23834 static void
23835 emit_cfa_restores (rtx cfa_restores)
23837 rtx insn = get_last_insn ();
23838 rtx *loc = &REG_NOTES (insn);
23840 while (*loc)
23841 loc = &XEXP (*loc, 1);
23842 *loc = cfa_restores;
23843 RTX_FRAME_RELATED_P (insn) = 1;
23846 /* Emit function epilogue as insns. */
23848 void
23849 rs6000_emit_epilogue (int sibcall)
23851 rs6000_stack_t *info;
23852 int restoring_GPRs_inline;
23853 int restoring_FPRs_inline;
23854 int using_load_multiple;
23855 int using_mtcr_multiple;
23856 int use_backchain_to_restore_sp;
23857 int restore_lr;
23858 int strategy;
23859 HOST_WIDE_INT frame_off = 0;
23860 rtx sp_reg_rtx = gen_rtx_REG (Pmode, 1);
23861 rtx frame_reg_rtx = sp_reg_rtx;
23862 rtx cfa_restores = NULL_RTX;
23863 rtx insn;
23864 rtx cr_save_reg = NULL_RTX;
23865 enum machine_mode reg_mode = Pmode;
23866 int reg_size = TARGET_32BIT ? 4 : 8;
23867 int i;
23868 bool exit_func;
23869 unsigned ptr_regno;
23871 info = rs6000_stack_info ();
23873 if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
23875 reg_mode = V2SImode;
23876 reg_size = 8;
23879 strategy = info->savres_strategy;
23880 using_load_multiple = strategy & SAVRES_MULTIPLE;
23881 restoring_FPRs_inline = sibcall || (strategy & REST_INLINE_FPRS);
23882 restoring_GPRs_inline = sibcall || (strategy & REST_INLINE_GPRS);
23883 using_mtcr_multiple = (rs6000_cpu == PROCESSOR_PPC601
23884 || rs6000_cpu == PROCESSOR_PPC603
23885 || rs6000_cpu == PROCESSOR_PPC750
23886 || optimize_size);
23887 /* Restore via the backchain when we have a large frame, since this
23888 is more efficient than an addis, addi pair. The second condition
23889 here will not trigger at the moment; We don't actually need a
23890 frame pointer for alloca, but the generic parts of the compiler
23891 give us one anyway. */
23892 use_backchain_to_restore_sp = (info->total_size > 32767 - info->lr_save_offset
23893 || (cfun->calls_alloca
23894 && !frame_pointer_needed));
23895 restore_lr = (info->lr_save_p
23896 && (restoring_FPRs_inline
23897 || (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR))
23898 && (restoring_GPRs_inline
23899 || info->first_fp_reg_save < 64));
23901 if (WORLD_SAVE_P (info))
23903 int i, j;
23904 char rname[30];
23905 const char *alloc_rname;
23906 rtvec p;
23908 /* eh_rest_world_r10 will return to the location saved in the LR
23909 stack slot (which is not likely to be our caller.)
23910 Input: R10 -- stack adjustment. Clobbers R0, R11, R12, R7, R8.
23911 rest_world is similar, except any R10 parameter is ignored.
23912 The exception-handling stuff that was here in 2.95 is no
23913 longer necessary. */
23915 p = rtvec_alloc (9
23917 + 32 - info->first_gp_reg_save
23918 + LAST_ALTIVEC_REGNO + 1 - info->first_altivec_reg_save
23919 + 63 + 1 - info->first_fp_reg_save);
23921 strcpy (rname, ((crtl->calls_eh_return) ?
23922 "*eh_rest_world_r10" : "*rest_world"));
23923 alloc_rname = ggc_strdup (rname);
23925 j = 0;
23926 RTVEC_ELT (p, j++) = ret_rtx;
23927 RTVEC_ELT (p, j++) = gen_rtx_USE (VOIDmode,
23928 gen_rtx_REG (Pmode,
23929 LR_REGNO));
23930 RTVEC_ELT (p, j++)
23931 = gen_rtx_USE (VOIDmode, gen_rtx_SYMBOL_REF (Pmode, alloc_rname));
23932 /* The instruction pattern requires a clobber here;
23933 it is shared with the restVEC helper. */
23934 RTVEC_ELT (p, j++)
23935 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 11));
23938 /* CR register traditionally saved as CR2. */
23939 rtx reg = gen_rtx_REG (SImode, CR2_REGNO);
23940 RTVEC_ELT (p, j++)
23941 = gen_frame_load (reg, frame_reg_rtx, info->cr_save_offset);
23942 if (flag_shrink_wrap)
23944 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
23945 gen_rtx_REG (Pmode, LR_REGNO),
23946 cfa_restores);
23947 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
23951 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
23953 rtx reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
23954 RTVEC_ELT (p, j++)
23955 = gen_frame_load (reg,
23956 frame_reg_rtx, info->gp_save_offset + reg_size * i);
23957 if (flag_shrink_wrap)
23958 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
23960 for (i = 0; info->first_altivec_reg_save + i <= LAST_ALTIVEC_REGNO; i++)
23962 rtx reg = gen_rtx_REG (V4SImode, info->first_altivec_reg_save + i);
23963 RTVEC_ELT (p, j++)
23964 = gen_frame_load (reg,
23965 frame_reg_rtx, info->altivec_save_offset + 16 * i);
23966 if (flag_shrink_wrap)
23967 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
23969 for (i = 0; info->first_fp_reg_save + i <= 63; i++)
23971 rtx reg = gen_rtx_REG ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
23972 ? DFmode : SFmode),
23973 info->first_fp_reg_save + i);
23974 RTVEC_ELT (p, j++)
23975 = gen_frame_load (reg, frame_reg_rtx, info->fp_save_offset + 8 * i);
23976 if (flag_shrink_wrap)
23977 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
23979 RTVEC_ELT (p, j++)
23980 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 0));
23981 RTVEC_ELT (p, j++)
23982 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 12));
23983 RTVEC_ELT (p, j++)
23984 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 7));
23985 RTVEC_ELT (p, j++)
23986 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 8));
23987 RTVEC_ELT (p, j++)
23988 = gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, 10));
23989 insn = emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
23991 if (flag_shrink_wrap)
23993 REG_NOTES (insn) = cfa_restores;
23994 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
23995 RTX_FRAME_RELATED_P (insn) = 1;
23997 return;
24000 /* frame_reg_rtx + frame_off points to the top of this stack frame. */
24001 if (info->push_p)
24002 frame_off = info->total_size;
24004 /* Restore AltiVec registers if we must do so before adjusting the
24005 stack. */
24006 if (TARGET_ALTIVEC_ABI
24007 && info->altivec_size != 0
24008 && (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
24009 || (DEFAULT_ABI != ABI_V4
24010 && offset_below_red_zone_p (info->altivec_save_offset))))
24012 int i;
24013 int scratch_regno = ptr_regno_for_savres (SAVRES_VR);
24015 gcc_checking_assert (scratch_regno == 11 || scratch_regno == 12);
24016 if (use_backchain_to_restore_sp)
24018 int frame_regno = 11;
24020 if ((strategy & REST_INLINE_VRS) == 0)
24022 /* Of r11 and r12, select the one not clobbered by an
24023 out-of-line restore function for the frame register. */
24024 frame_regno = 11 + 12 - scratch_regno;
24026 frame_reg_rtx = gen_rtx_REG (Pmode, frame_regno);
24027 emit_move_insn (frame_reg_rtx,
24028 gen_rtx_MEM (Pmode, sp_reg_rtx));
24029 frame_off = 0;
24031 else if (frame_pointer_needed)
24032 frame_reg_rtx = hard_frame_pointer_rtx;
24034 if ((strategy & REST_INLINE_VRS) == 0)
24036 int end_save = info->altivec_save_offset + info->altivec_size;
24037 int ptr_off;
24038 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
24039 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
24041 if (end_save + frame_off != 0)
24043 rtx offset = GEN_INT (end_save + frame_off);
24045 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
24047 else
24048 emit_move_insn (ptr_reg, frame_reg_rtx);
24050 ptr_off = -end_save;
24051 insn = rs6000_emit_savres_rtx (info, scratch_reg,
24052 info->altivec_save_offset + ptr_off,
24053 0, V4SImode, SAVRES_VR);
24055 else
24057 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
24058 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
24060 rtx addr, areg, mem, reg;
24062 areg = gen_rtx_REG (Pmode, 0);
24063 emit_move_insn
24064 (areg, GEN_INT (info->altivec_save_offset
24065 + frame_off
24066 + 16 * (i - info->first_altivec_reg_save)));
24068 /* AltiVec addressing mode is [reg+reg]. */
24069 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
24070 mem = gen_frame_mem (V4SImode, addr);
24072 reg = gen_rtx_REG (V4SImode, i);
24073 emit_move_insn (reg, mem);
24077 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
24078 if (((strategy & REST_INLINE_VRS) == 0
24079 || (info->vrsave_mask & ALTIVEC_REG_BIT (i)) != 0)
24080 && (flag_shrink_wrap
24081 || (offset_below_red_zone_p
24082 (info->altivec_save_offset
24083 + 16 * (i - info->first_altivec_reg_save)))))
24085 rtx reg = gen_rtx_REG (V4SImode, i);
24086 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
24090 /* Restore VRSAVE if we must do so before adjusting the stack. */
24091 if (TARGET_ALTIVEC
24092 && TARGET_ALTIVEC_VRSAVE
24093 && info->vrsave_mask != 0
24094 && (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
24095 || (DEFAULT_ABI != ABI_V4
24096 && offset_below_red_zone_p (info->vrsave_save_offset))))
24098 rtx reg;
24100 if (frame_reg_rtx == sp_reg_rtx)
24102 if (use_backchain_to_restore_sp)
24104 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
24105 emit_move_insn (frame_reg_rtx,
24106 gen_rtx_MEM (Pmode, sp_reg_rtx));
24107 frame_off = 0;
24109 else if (frame_pointer_needed)
24110 frame_reg_rtx = hard_frame_pointer_rtx;
24113 reg = gen_rtx_REG (SImode, 12);
24114 emit_insn (gen_frame_load (reg, frame_reg_rtx,
24115 info->vrsave_save_offset + frame_off));
24117 emit_insn (generate_set_vrsave (reg, info, 1));
24120 insn = NULL_RTX;
24121 /* If we have a large stack frame, restore the old stack pointer
24122 using the backchain. */
24123 if (use_backchain_to_restore_sp)
24125 if (frame_reg_rtx == sp_reg_rtx)
24127 /* Under V.4, don't reset the stack pointer until after we're done
24128 loading the saved registers. */
24129 if (DEFAULT_ABI == ABI_V4)
24130 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
24132 insn = emit_move_insn (frame_reg_rtx,
24133 gen_rtx_MEM (Pmode, sp_reg_rtx));
24134 frame_off = 0;
24136 else if (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
24137 && DEFAULT_ABI == ABI_V4)
24138 /* frame_reg_rtx has been set up by the altivec restore. */
24140 else
24142 insn = emit_move_insn (sp_reg_rtx, frame_reg_rtx);
24143 frame_reg_rtx = sp_reg_rtx;
24146 /* If we have a frame pointer, we can restore the old stack pointer
24147 from it. */
24148 else if (frame_pointer_needed)
24150 frame_reg_rtx = sp_reg_rtx;
24151 if (DEFAULT_ABI == ABI_V4)
24152 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
24153 /* Prevent reordering memory accesses against stack pointer restore. */
24154 else if (cfun->calls_alloca
24155 || offset_below_red_zone_p (-info->total_size))
24156 rs6000_emit_stack_tie (frame_reg_rtx, true);
24158 insn = emit_insn (gen_add3_insn (frame_reg_rtx, hard_frame_pointer_rtx,
24159 GEN_INT (info->total_size)));
24160 frame_off = 0;
24162 else if (info->push_p
24163 && DEFAULT_ABI != ABI_V4
24164 && !crtl->calls_eh_return)
24166 /* Prevent reordering memory accesses against stack pointer restore. */
24167 if (cfun->calls_alloca
24168 || offset_below_red_zone_p (-info->total_size))
24169 rs6000_emit_stack_tie (frame_reg_rtx, false);
24170 insn = emit_insn (gen_add3_insn (sp_reg_rtx, sp_reg_rtx,
24171 GEN_INT (info->total_size)));
24172 frame_off = 0;
24174 if (insn && frame_reg_rtx == sp_reg_rtx)
24176 if (cfa_restores)
24178 REG_NOTES (insn) = cfa_restores;
24179 cfa_restores = NULL_RTX;
24181 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
24182 RTX_FRAME_RELATED_P (insn) = 1;
24185 /* Restore AltiVec registers if we have not done so already. */
24186 if (!ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
24187 && TARGET_ALTIVEC_ABI
24188 && info->altivec_size != 0
24189 && (DEFAULT_ABI == ABI_V4
24190 || !offset_below_red_zone_p (info->altivec_save_offset)))
24192 int i;
24194 if ((strategy & REST_INLINE_VRS) == 0)
24196 int end_save = info->altivec_save_offset + info->altivec_size;
24197 int ptr_off;
24198 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
24199 int scratch_regno = ptr_regno_for_savres (SAVRES_VR);
24200 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
24202 if (end_save + frame_off != 0)
24204 rtx offset = GEN_INT (end_save + frame_off);
24206 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
24208 else
24209 emit_move_insn (ptr_reg, frame_reg_rtx);
24211 ptr_off = -end_save;
24212 insn = rs6000_emit_savres_rtx (info, scratch_reg,
24213 info->altivec_save_offset + ptr_off,
24214 0, V4SImode, SAVRES_VR);
24215 if (REGNO (frame_reg_rtx) == REGNO (scratch_reg))
24217 /* Frame reg was clobbered by out-of-line save. Restore it
24218 from ptr_reg, and if we are calling out-of-line gpr or
24219 fpr restore set up the correct pointer and offset. */
24220 unsigned newptr_regno = 1;
24221 if (!restoring_GPRs_inline)
24223 bool lr = info->gp_save_offset + info->gp_size == 0;
24224 int sel = SAVRES_GPR | (lr ? SAVRES_LR : 0);
24225 newptr_regno = ptr_regno_for_savres (sel);
24226 end_save = info->gp_save_offset + info->gp_size;
24228 else if (!restoring_FPRs_inline)
24230 bool lr = !(strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR);
24231 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
24232 newptr_regno = ptr_regno_for_savres (sel);
24233 end_save = info->gp_save_offset + info->gp_size;
24236 if (newptr_regno != 1 && REGNO (frame_reg_rtx) != newptr_regno)
24237 frame_reg_rtx = gen_rtx_REG (Pmode, newptr_regno);
24239 if (end_save + ptr_off != 0)
24241 rtx offset = GEN_INT (end_save + ptr_off);
24243 frame_off = -end_save;
24244 emit_insn (gen_add3_insn (frame_reg_rtx, ptr_reg, offset));
24246 else
24248 frame_off = ptr_off;
24249 emit_move_insn (frame_reg_rtx, ptr_reg);
24253 else
24255 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
24256 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
24258 rtx addr, areg, mem, reg;
24260 areg = gen_rtx_REG (Pmode, 0);
24261 emit_move_insn
24262 (areg, GEN_INT (info->altivec_save_offset
24263 + frame_off
24264 + 16 * (i - info->first_altivec_reg_save)));
24266 /* AltiVec addressing mode is [reg+reg]. */
24267 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
24268 mem = gen_frame_mem (V4SImode, addr);
24270 reg = gen_rtx_REG (V4SImode, i);
24271 emit_move_insn (reg, mem);
24275 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
24276 if (((strategy & REST_INLINE_VRS) == 0
24277 || (info->vrsave_mask & ALTIVEC_REG_BIT (i)) != 0)
24278 && (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap))
24280 rtx reg = gen_rtx_REG (V4SImode, i);
24281 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
24285 /* Restore VRSAVE if we have not done so already. */
24286 if (!ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
24287 && TARGET_ALTIVEC
24288 && TARGET_ALTIVEC_VRSAVE
24289 && info->vrsave_mask != 0
24290 && (DEFAULT_ABI == ABI_V4
24291 || !offset_below_red_zone_p (info->vrsave_save_offset)))
24293 rtx reg;
24295 reg = gen_rtx_REG (SImode, 12);
24296 emit_insn (gen_frame_load (reg, frame_reg_rtx,
24297 info->vrsave_save_offset + frame_off));
24299 emit_insn (generate_set_vrsave (reg, info, 1));
24302 /* If we exit by an out-of-line restore function on ABI_V4 then that
24303 function will deallocate the stack, so we don't need to worry
24304 about the unwinder restoring cr from an invalid stack frame
24305 location. */
24306 exit_func = (!restoring_FPRs_inline
24307 || (!restoring_GPRs_inline
24308 && info->first_fp_reg_save == 64));
24310 /* In the ELFv2 ABI we need to restore all call-saved CR fields from
24311 *separate* slots if the routine calls __builtin_eh_return, so
24312 that they can be independently restored by the unwinder. */
24313 if (DEFAULT_ABI == ABI_ELFv2 && crtl->calls_eh_return)
24315 int i, cr_off = info->ehcr_offset;
24317 for (i = 0; i < 8; i++)
24318 if (!call_used_regs[CR0_REGNO + i])
24320 rtx reg = gen_rtx_REG (SImode, 0);
24321 emit_insn (gen_frame_load (reg, frame_reg_rtx,
24322 cr_off + frame_off));
24324 insn = emit_insn (gen_movsi_to_cr_one
24325 (gen_rtx_REG (CCmode, CR0_REGNO + i), reg));
24327 if (!exit_func && flag_shrink_wrap)
24329 add_reg_note (insn, REG_CFA_RESTORE,
24330 gen_rtx_REG (SImode, CR0_REGNO + i));
24332 RTX_FRAME_RELATED_P (insn) = 1;
24335 cr_off += reg_size;
24339 /* Get the old lr if we saved it. If we are restoring registers
24340 out-of-line, then the out-of-line routines can do this for us. */
24341 if (restore_lr && restoring_GPRs_inline)
24342 load_lr_save (0, frame_reg_rtx, info->lr_save_offset + frame_off);
24344 /* Get the old cr if we saved it. */
24345 if (info->cr_save_p)
24347 unsigned cr_save_regno = 12;
24349 if (!restoring_GPRs_inline)
24351 /* Ensure we don't use the register used by the out-of-line
24352 gpr register restore below. */
24353 bool lr = info->gp_save_offset + info->gp_size == 0;
24354 int sel = SAVRES_GPR | (lr ? SAVRES_LR : 0);
24355 int gpr_ptr_regno = ptr_regno_for_savres (sel);
24357 if (gpr_ptr_regno == 12)
24358 cr_save_regno = 11;
24359 gcc_checking_assert (REGNO (frame_reg_rtx) != cr_save_regno);
24361 else if (REGNO (frame_reg_rtx) == 12)
24362 cr_save_regno = 11;
24364 cr_save_reg = load_cr_save (cr_save_regno, frame_reg_rtx,
24365 info->cr_save_offset + frame_off,
24366 exit_func);
24369 /* Set LR here to try to overlap restores below. */
24370 if (restore_lr && restoring_GPRs_inline)
24371 restore_saved_lr (0, exit_func);
24373 /* Load exception handler data registers, if needed. */
24374 if (crtl->calls_eh_return)
24376 unsigned int i, regno;
24378 if (TARGET_AIX)
24380 rtx reg = gen_rtx_REG (reg_mode, 2);
24381 emit_insn (gen_frame_load (reg, frame_reg_rtx,
24382 frame_off + RS6000_TOC_SAVE_SLOT));
24385 for (i = 0; ; ++i)
24387 rtx mem;
24389 regno = EH_RETURN_DATA_REGNO (i);
24390 if (regno == INVALID_REGNUM)
24391 break;
24393 /* Note: possible use of r0 here to address SPE regs. */
24394 mem = gen_frame_mem_offset (reg_mode, frame_reg_rtx,
24395 info->ehrd_offset + frame_off
24396 + reg_size * (int) i);
24398 emit_move_insn (gen_rtx_REG (reg_mode, regno), mem);
24402 /* Restore GPRs. This is done as a PARALLEL if we are using
24403 the load-multiple instructions. */
24404 if (TARGET_SPE_ABI
24405 && info->spe_64bit_regs_used
24406 && info->first_gp_reg_save != 32)
24408 /* Determine whether we can address all of the registers that need
24409 to be saved with an offset from frame_reg_rtx that fits in
24410 the small const field for SPE memory instructions. */
24411 int spe_regs_addressable
24412 = (SPE_CONST_OFFSET_OK (info->spe_gp_save_offset + frame_off
24413 + reg_size * (32 - info->first_gp_reg_save - 1))
24414 && restoring_GPRs_inline);
24416 if (!spe_regs_addressable)
24418 int ool_adjust = 0;
24419 rtx old_frame_reg_rtx = frame_reg_rtx;
24420 /* Make r11 point to the start of the SPE save area. We worried about
24421 not clobbering it when we were saving registers in the prologue.
24422 There's no need to worry here because the static chain is passed
24423 anew to every function. */
24425 if (!restoring_GPRs_inline)
24426 ool_adjust = 8 * (info->first_gp_reg_save - FIRST_SAVED_GP_REGNO);
24427 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
24428 emit_insn (gen_addsi3 (frame_reg_rtx, old_frame_reg_rtx,
24429 GEN_INT (info->spe_gp_save_offset
24430 + frame_off
24431 - ool_adjust)));
24432 /* Keep the invariant that frame_reg_rtx + frame_off points
24433 at the top of the stack frame. */
24434 frame_off = -info->spe_gp_save_offset + ool_adjust;
24437 if (restoring_GPRs_inline)
24439 HOST_WIDE_INT spe_offset = info->spe_gp_save_offset + frame_off;
24441 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
24442 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
24444 rtx offset, addr, mem, reg;
24446 /* We're doing all this to ensure that the immediate offset
24447 fits into the immediate field of 'evldd'. */
24448 gcc_assert (SPE_CONST_OFFSET_OK (spe_offset + reg_size * i));
24450 offset = GEN_INT (spe_offset + reg_size * i);
24451 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, offset);
24452 mem = gen_rtx_MEM (V2SImode, addr);
24453 reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
24455 emit_move_insn (reg, mem);
24458 else
24459 rs6000_emit_savres_rtx (info, frame_reg_rtx,
24460 info->spe_gp_save_offset + frame_off,
24461 info->lr_save_offset + frame_off,
24462 reg_mode,
24463 SAVRES_GPR | SAVRES_LR);
24465 else if (!restoring_GPRs_inline)
24467 /* We are jumping to an out-of-line function. */
24468 rtx ptr_reg;
24469 int end_save = info->gp_save_offset + info->gp_size;
24470 bool can_use_exit = end_save == 0;
24471 int sel = SAVRES_GPR | (can_use_exit ? SAVRES_LR : 0);
24472 int ptr_off;
24474 /* Emit stack reset code if we need it. */
24475 ptr_regno = ptr_regno_for_savres (sel);
24476 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
24477 if (can_use_exit)
24478 rs6000_emit_stack_reset (info, frame_reg_rtx, frame_off, ptr_regno);
24479 else if (end_save + frame_off != 0)
24480 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx,
24481 GEN_INT (end_save + frame_off)));
24482 else if (REGNO (frame_reg_rtx) != ptr_regno)
24483 emit_move_insn (ptr_reg, frame_reg_rtx);
24484 if (REGNO (frame_reg_rtx) == ptr_regno)
24485 frame_off = -end_save;
24487 if (can_use_exit && info->cr_save_p)
24488 restore_saved_cr (cr_save_reg, using_mtcr_multiple, true);
24490 ptr_off = -end_save;
24491 rs6000_emit_savres_rtx (info, ptr_reg,
24492 info->gp_save_offset + ptr_off,
24493 info->lr_save_offset + ptr_off,
24494 reg_mode, sel);
24496 else if (using_load_multiple)
24498 rtvec p;
24499 p = rtvec_alloc (32 - info->first_gp_reg_save);
24500 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
24501 RTVEC_ELT (p, i)
24502 = gen_frame_load (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
24503 frame_reg_rtx,
24504 info->gp_save_offset + frame_off + reg_size * i);
24505 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
24507 else
24509 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
24510 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
24511 emit_insn (gen_frame_load
24512 (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
24513 frame_reg_rtx,
24514 info->gp_save_offset + frame_off + reg_size * i));
24517 if (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
24519 /* If the frame pointer was used then we can't delay emitting
24520 a REG_CFA_DEF_CFA note. This must happen on the insn that
24521 restores the frame pointer, r31. We may have already emitted
24522 a REG_CFA_DEF_CFA note, but that's OK; A duplicate is
24523 discarded by dwarf2cfi.c/dwarf2out.c, and in any case would
24524 be harmless if emitted. */
24525 if (frame_pointer_needed)
24527 insn = get_last_insn ();
24528 add_reg_note (insn, REG_CFA_DEF_CFA,
24529 plus_constant (Pmode, frame_reg_rtx, frame_off));
24530 RTX_FRAME_RELATED_P (insn) = 1;
24533 /* Set up cfa_restores. We always need these when
24534 shrink-wrapping. If not shrink-wrapping then we only need
24535 the cfa_restore when the stack location is no longer valid.
24536 The cfa_restores must be emitted on or before the insn that
24537 invalidates the stack, and of course must not be emitted
24538 before the insn that actually does the restore. The latter
24539 is why it is a bad idea to emit the cfa_restores as a group
24540 on the last instruction here that actually does a restore:
24541 That insn may be reordered with respect to others doing
24542 restores. */
24543 if (flag_shrink_wrap
24544 && !restoring_GPRs_inline
24545 && info->first_fp_reg_save == 64)
24546 cfa_restores = add_crlr_cfa_restore (info, cfa_restores);
24548 for (i = info->first_gp_reg_save; i < 32; i++)
24549 if (!restoring_GPRs_inline
24550 || using_load_multiple
24551 || rs6000_reg_live_or_pic_offset_p (i))
24553 rtx reg = gen_rtx_REG (reg_mode, i);
24555 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
24559 if (!restoring_GPRs_inline
24560 && info->first_fp_reg_save == 64)
24562 /* We are jumping to an out-of-line function. */
24563 if (cfa_restores)
24564 emit_cfa_restores (cfa_restores);
24565 return;
24568 if (restore_lr && !restoring_GPRs_inline)
24570 load_lr_save (0, frame_reg_rtx, info->lr_save_offset + frame_off);
24571 restore_saved_lr (0, exit_func);
24574 /* Restore fpr's if we need to do it without calling a function. */
24575 if (restoring_FPRs_inline)
24576 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
24577 if (save_reg_p (info->first_fp_reg_save + i))
24579 rtx reg = gen_rtx_REG ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
24580 ? DFmode : SFmode),
24581 info->first_fp_reg_save + i);
24582 emit_insn (gen_frame_load (reg, frame_reg_rtx,
24583 info->fp_save_offset + frame_off + 8 * i));
24584 if (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
24585 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
24588 /* If we saved cr, restore it here. Just those that were used. */
24589 if (info->cr_save_p)
24590 restore_saved_cr (cr_save_reg, using_mtcr_multiple, exit_func);
24592 /* If this is V.4, unwind the stack pointer after all of the loads
24593 have been done, or set up r11 if we are restoring fp out of line. */
24594 ptr_regno = 1;
24595 if (!restoring_FPRs_inline)
24597 bool lr = (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
24598 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
24599 ptr_regno = ptr_regno_for_savres (sel);
24602 insn = rs6000_emit_stack_reset (info, frame_reg_rtx, frame_off, ptr_regno);
24603 if (REGNO (frame_reg_rtx) == ptr_regno)
24604 frame_off = 0;
24606 if (insn && restoring_FPRs_inline)
24608 if (cfa_restores)
24610 REG_NOTES (insn) = cfa_restores;
24611 cfa_restores = NULL_RTX;
24613 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
24614 RTX_FRAME_RELATED_P (insn) = 1;
24617 if (crtl->calls_eh_return)
24619 rtx sa = EH_RETURN_STACKADJ_RTX;
24620 emit_insn (gen_add3_insn (sp_reg_rtx, sp_reg_rtx, sa));
24623 if (!sibcall)
24625 rtvec p;
24626 bool lr = (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
24627 if (! restoring_FPRs_inline)
24629 p = rtvec_alloc (4 + 64 - info->first_fp_reg_save);
24630 RTVEC_ELT (p, 0) = ret_rtx;
24632 else
24634 if (cfa_restores)
24636 /* We can't hang the cfa_restores off a simple return,
24637 since the shrink-wrap code sometimes uses an existing
24638 return. This means there might be a path from
24639 pre-prologue code to this return, and dwarf2cfi code
24640 wants the eh_frame unwinder state to be the same on
24641 all paths to any point. So we need to emit the
24642 cfa_restores before the return. For -m64 we really
24643 don't need epilogue cfa_restores at all, except for
24644 this irritating dwarf2cfi with shrink-wrap
24645 requirement; The stack red-zone means eh_frame info
24646 from the prologue telling the unwinder to restore
24647 from the stack is perfectly good right to the end of
24648 the function. */
24649 emit_insn (gen_blockage ());
24650 emit_cfa_restores (cfa_restores);
24651 cfa_restores = NULL_RTX;
24653 p = rtvec_alloc (2);
24654 RTVEC_ELT (p, 0) = simple_return_rtx;
24657 RTVEC_ELT (p, 1) = ((restoring_FPRs_inline || !lr)
24658 ? gen_rtx_USE (VOIDmode,
24659 gen_rtx_REG (Pmode, LR_REGNO))
24660 : gen_rtx_CLOBBER (VOIDmode,
24661 gen_rtx_REG (Pmode, LR_REGNO)));
24663 /* If we have to restore more than two FP registers, branch to the
24664 restore function. It will return to our caller. */
24665 if (! restoring_FPRs_inline)
24667 int i;
24668 int reg;
24669 rtx sym;
24671 if (flag_shrink_wrap)
24672 cfa_restores = add_crlr_cfa_restore (info, cfa_restores);
24674 sym = rs6000_savres_routine_sym (info,
24675 SAVRES_FPR | (lr ? SAVRES_LR : 0));
24676 RTVEC_ELT (p, 2) = gen_rtx_USE (VOIDmode, sym);
24677 reg = (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)? 1 : 11;
24678 RTVEC_ELT (p, 3) = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, reg));
24680 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
24682 rtx reg = gen_rtx_REG (DFmode, info->first_fp_reg_save + i);
24684 RTVEC_ELT (p, i + 4)
24685 = gen_frame_load (reg, sp_reg_rtx, info->fp_save_offset + 8 * i);
24686 if (flag_shrink_wrap)
24687 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg,
24688 cfa_restores);
24692 emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
24695 if (cfa_restores)
24697 if (sibcall)
24698 /* Ensure the cfa_restores are hung off an insn that won't
24699 be reordered above other restores. */
24700 emit_insn (gen_blockage ());
24702 emit_cfa_restores (cfa_restores);
24706 /* Write function epilogue. */
24708 static void
24709 rs6000_output_function_epilogue (FILE *file,
24710 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
24712 #if TARGET_MACHO
24713 macho_branch_islands ();
24714 /* Mach-O doesn't support labels at the end of objects, so if
24715 it looks like we might want one, insert a NOP. */
24717 rtx insn = get_last_insn ();
24718 rtx deleted_debug_label = NULL_RTX;
24719 while (insn
24720 && NOTE_P (insn)
24721 && NOTE_KIND (insn) != NOTE_INSN_DELETED_LABEL)
24723 /* Don't insert a nop for NOTE_INSN_DELETED_DEBUG_LABEL
24724 notes only, instead set their CODE_LABEL_NUMBER to -1,
24725 otherwise there would be code generation differences
24726 in between -g and -g0. */
24727 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
24728 deleted_debug_label = insn;
24729 insn = PREV_INSN (insn);
24731 if (insn
24732 && (LABEL_P (insn)
24733 || (NOTE_P (insn)
24734 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL)))
24735 fputs ("\tnop\n", file);
24736 else if (deleted_debug_label)
24737 for (insn = deleted_debug_label; insn; insn = NEXT_INSN (insn))
24738 if (NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
24739 CODE_LABEL_NUMBER (insn) = -1;
24741 #endif
24743 /* Output a traceback table here. See /usr/include/sys/debug.h for info
24744 on its format.
24746 We don't output a traceback table if -finhibit-size-directive was
24747 used. The documentation for -finhibit-size-directive reads
24748 ``don't output a @code{.size} assembler directive, or anything
24749 else that would cause trouble if the function is split in the
24750 middle, and the two halves are placed at locations far apart in
24751 memory.'' The traceback table has this property, since it
24752 includes the offset from the start of the function to the
24753 traceback table itself.
24755 System V.4 Powerpc's (and the embedded ABI derived from it) use a
24756 different traceback table. */
24757 if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
24758 && ! flag_inhibit_size_directive
24759 && rs6000_traceback != traceback_none && !cfun->is_thunk)
24761 const char *fname = NULL;
24762 const char *language_string = lang_hooks.name;
24763 int fixed_parms = 0, float_parms = 0, parm_info = 0;
24764 int i;
24765 int optional_tbtab;
24766 rs6000_stack_t *info = rs6000_stack_info ();
24768 if (rs6000_traceback == traceback_full)
24769 optional_tbtab = 1;
24770 else if (rs6000_traceback == traceback_part)
24771 optional_tbtab = 0;
24772 else
24773 optional_tbtab = !optimize_size && !TARGET_ELF;
24775 if (optional_tbtab)
24777 fname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
24778 while (*fname == '.') /* V.4 encodes . in the name */
24779 fname++;
24781 /* Need label immediately before tbtab, so we can compute
24782 its offset from the function start. */
24783 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
24784 ASM_OUTPUT_LABEL (file, fname);
24787 /* The .tbtab pseudo-op can only be used for the first eight
24788 expressions, since it can't handle the possibly variable
24789 length fields that follow. However, if you omit the optional
24790 fields, the assembler outputs zeros for all optional fields
24791 anyways, giving each variable length field is minimum length
24792 (as defined in sys/debug.h). Thus we can not use the .tbtab
24793 pseudo-op at all. */
24795 /* An all-zero word flags the start of the tbtab, for debuggers
24796 that have to find it by searching forward from the entry
24797 point or from the current pc. */
24798 fputs ("\t.long 0\n", file);
24800 /* Tbtab format type. Use format type 0. */
24801 fputs ("\t.byte 0,", file);
24803 /* Language type. Unfortunately, there does not seem to be any
24804 official way to discover the language being compiled, so we
24805 use language_string.
24806 C is 0. Fortran is 1. Pascal is 2. Ada is 3. C++ is 9.
24807 Java is 13. Objective-C is 14. Objective-C++ isn't assigned
24808 a number, so for now use 9. LTO and Go aren't assigned numbers
24809 either, so for now use 0. */
24810 if (! strcmp (language_string, "GNU C")
24811 || ! strcmp (language_string, "GNU GIMPLE")
24812 || ! strcmp (language_string, "GNU Go"))
24813 i = 0;
24814 else if (! strcmp (language_string, "GNU F77")
24815 || ! strcmp (language_string, "GNU Fortran"))
24816 i = 1;
24817 else if (! strcmp (language_string, "GNU Pascal"))
24818 i = 2;
24819 else if (! strcmp (language_string, "GNU Ada"))
24820 i = 3;
24821 else if (! strcmp (language_string, "GNU C++")
24822 || ! strcmp (language_string, "GNU Objective-C++"))
24823 i = 9;
24824 else if (! strcmp (language_string, "GNU Java"))
24825 i = 13;
24826 else if (! strcmp (language_string, "GNU Objective-C"))
24827 i = 14;
24828 else
24829 gcc_unreachable ();
24830 fprintf (file, "%d,", i);
24832 /* 8 single bit fields: global linkage (not set for C extern linkage,
24833 apparently a PL/I convention?), out-of-line epilogue/prologue, offset
24834 from start of procedure stored in tbtab, internal function, function
24835 has controlled storage, function has no toc, function uses fp,
24836 function logs/aborts fp operations. */
24837 /* Assume that fp operations are used if any fp reg must be saved. */
24838 fprintf (file, "%d,",
24839 (optional_tbtab << 5) | ((info->first_fp_reg_save != 64) << 1));
24841 /* 6 bitfields: function is interrupt handler, name present in
24842 proc table, function calls alloca, on condition directives
24843 (controls stack walks, 3 bits), saves condition reg, saves
24844 link reg. */
24845 /* The `function calls alloca' bit seems to be set whenever reg 31 is
24846 set up as a frame pointer, even when there is no alloca call. */
24847 fprintf (file, "%d,",
24848 ((optional_tbtab << 6)
24849 | ((optional_tbtab & frame_pointer_needed) << 5)
24850 | (info->cr_save_p << 1)
24851 | (info->lr_save_p)));
24853 /* 3 bitfields: saves backchain, fixup code, number of fpr saved
24854 (6 bits). */
24855 fprintf (file, "%d,",
24856 (info->push_p << 7) | (64 - info->first_fp_reg_save));
24858 /* 2 bitfields: spare bits (2 bits), number of gpr saved (6 bits). */
24859 fprintf (file, "%d,", (32 - first_reg_to_save ()));
24861 if (optional_tbtab)
24863 /* Compute the parameter info from the function decl argument
24864 list. */
24865 tree decl;
24866 int next_parm_info_bit = 31;
24868 for (decl = DECL_ARGUMENTS (current_function_decl);
24869 decl; decl = DECL_CHAIN (decl))
24871 rtx parameter = DECL_INCOMING_RTL (decl);
24872 enum machine_mode mode = GET_MODE (parameter);
24874 if (GET_CODE (parameter) == REG)
24876 if (SCALAR_FLOAT_MODE_P (mode))
24878 int bits;
24880 float_parms++;
24882 switch (mode)
24884 case SFmode:
24885 case SDmode:
24886 bits = 0x2;
24887 break;
24889 case DFmode:
24890 case DDmode:
24891 case TFmode:
24892 case TDmode:
24893 bits = 0x3;
24894 break;
24896 default:
24897 gcc_unreachable ();
24900 /* If only one bit will fit, don't or in this entry. */
24901 if (next_parm_info_bit > 0)
24902 parm_info |= (bits << (next_parm_info_bit - 1));
24903 next_parm_info_bit -= 2;
24905 else
24907 fixed_parms += ((GET_MODE_SIZE (mode)
24908 + (UNITS_PER_WORD - 1))
24909 / UNITS_PER_WORD);
24910 next_parm_info_bit -= 1;
24916 /* Number of fixed point parameters. */
24917 /* This is actually the number of words of fixed point parameters; thus
24918 an 8 byte struct counts as 2; and thus the maximum value is 8. */
24919 fprintf (file, "%d,", fixed_parms);
24921 /* 2 bitfields: number of floating point parameters (7 bits), parameters
24922 all on stack. */
24923 /* This is actually the number of fp registers that hold parameters;
24924 and thus the maximum value is 13. */
24925 /* Set parameters on stack bit if parameters are not in their original
24926 registers, regardless of whether they are on the stack? Xlc
24927 seems to set the bit when not optimizing. */
24928 fprintf (file, "%d\n", ((float_parms << 1) | (! optimize)));
24930 if (! optional_tbtab)
24931 return;
24933 /* Optional fields follow. Some are variable length. */
24935 /* Parameter types, left adjusted bit fields: 0 fixed, 10 single float,
24936 11 double float. */
24937 /* There is an entry for each parameter in a register, in the order that
24938 they occur in the parameter list. Any intervening arguments on the
24939 stack are ignored. If the list overflows a long (max possible length
24940 34 bits) then completely leave off all elements that don't fit. */
24941 /* Only emit this long if there was at least one parameter. */
24942 if (fixed_parms || float_parms)
24943 fprintf (file, "\t.long %d\n", parm_info);
24945 /* Offset from start of code to tb table. */
24946 fputs ("\t.long ", file);
24947 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
24948 RS6000_OUTPUT_BASENAME (file, fname);
24949 putc ('-', file);
24950 rs6000_output_function_entry (file, fname);
24951 putc ('\n', file);
24953 /* Interrupt handler mask. */
24954 /* Omit this long, since we never set the interrupt handler bit
24955 above. */
24957 /* Number of CTL (controlled storage) anchors. */
24958 /* Omit this long, since the has_ctl bit is never set above. */
24960 /* Displacement into stack of each CTL anchor. */
24961 /* Omit this list of longs, because there are no CTL anchors. */
24963 /* Length of function name. */
24964 if (*fname == '*')
24965 ++fname;
24966 fprintf (file, "\t.short %d\n", (int) strlen (fname));
24968 /* Function name. */
24969 assemble_string (fname, strlen (fname));
24971 /* Register for alloca automatic storage; this is always reg 31.
24972 Only emit this if the alloca bit was set above. */
24973 if (frame_pointer_needed)
24974 fputs ("\t.byte 31\n", file);
24976 fputs ("\t.align 2\n", file);
24980 /* A C compound statement that outputs the assembler code for a thunk
24981 function, used to implement C++ virtual function calls with
24982 multiple inheritance. The thunk acts as a wrapper around a virtual
24983 function, adjusting the implicit object parameter before handing
24984 control off to the real function.
24986 First, emit code to add the integer DELTA to the location that
24987 contains the incoming first argument. Assume that this argument
24988 contains a pointer, and is the one used to pass the `this' pointer
24989 in C++. This is the incoming argument *before* the function
24990 prologue, e.g. `%o0' on a sparc. The addition must preserve the
24991 values of all other incoming arguments.
24993 After the addition, emit code to jump to FUNCTION, which is a
24994 `FUNCTION_DECL'. This is a direct pure jump, not a call, and does
24995 not touch the return address. Hence returning from FUNCTION will
24996 return to whoever called the current `thunk'.
24998 The effect must be as if FUNCTION had been called directly with the
24999 adjusted first argument. This macro is responsible for emitting
25000 all of the code for a thunk function; output_function_prologue()
25001 and output_function_epilogue() are not invoked.
25003 The THUNK_FNDECL is redundant. (DELTA and FUNCTION have already
25004 been extracted from it.) It might possibly be useful on some
25005 targets, but probably not.
25007 If you do not define this macro, the target-independent code in the
25008 C++ frontend will generate a less efficient heavyweight thunk that
25009 calls FUNCTION instead of jumping to it. The generic approach does
25010 not support varargs. */
25012 static void
25013 rs6000_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
25014 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
25015 tree function)
25017 rtx this_rtx, insn, funexp;
25019 reload_completed = 1;
25020 epilogue_completed = 1;
25022 /* Mark the end of the (empty) prologue. */
25023 emit_note (NOTE_INSN_PROLOGUE_END);
25025 /* Find the "this" pointer. If the function returns a structure,
25026 the structure return pointer is in r3. */
25027 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
25028 this_rtx = gen_rtx_REG (Pmode, 4);
25029 else
25030 this_rtx = gen_rtx_REG (Pmode, 3);
25032 /* Apply the constant offset, if required. */
25033 if (delta)
25034 emit_insn (gen_add3_insn (this_rtx, this_rtx, GEN_INT (delta)));
25036 /* Apply the offset from the vtable, if required. */
25037 if (vcall_offset)
25039 rtx vcall_offset_rtx = GEN_INT (vcall_offset);
25040 rtx tmp = gen_rtx_REG (Pmode, 12);
25042 emit_move_insn (tmp, gen_rtx_MEM (Pmode, this_rtx));
25043 if (((unsigned HOST_WIDE_INT) vcall_offset) + 0x8000 >= 0x10000)
25045 emit_insn (gen_add3_insn (tmp, tmp, vcall_offset_rtx));
25046 emit_move_insn (tmp, gen_rtx_MEM (Pmode, tmp));
25048 else
25050 rtx loc = gen_rtx_PLUS (Pmode, tmp, vcall_offset_rtx);
25052 emit_move_insn (tmp, gen_rtx_MEM (Pmode, loc));
25054 emit_insn (gen_add3_insn (this_rtx, this_rtx, tmp));
25057 /* Generate a tail call to the target function. */
25058 if (!TREE_USED (function))
25060 assemble_external (function);
25061 TREE_USED (function) = 1;
25063 funexp = XEXP (DECL_RTL (function), 0);
25064 funexp = gen_rtx_MEM (FUNCTION_MODE, funexp);
25066 #if TARGET_MACHO
25067 if (MACHOPIC_INDIRECT)
25068 funexp = machopic_indirect_call_target (funexp);
25069 #endif
25071 /* gen_sibcall expects reload to convert scratch pseudo to LR so we must
25072 generate sibcall RTL explicitly. */
25073 insn = emit_call_insn (
25074 gen_rtx_PARALLEL (VOIDmode,
25075 gen_rtvec (4,
25076 gen_rtx_CALL (VOIDmode,
25077 funexp, const0_rtx),
25078 gen_rtx_USE (VOIDmode, const0_rtx),
25079 gen_rtx_USE (VOIDmode,
25080 gen_rtx_REG (SImode,
25081 LR_REGNO)),
25082 simple_return_rtx)));
25083 SIBLING_CALL_P (insn) = 1;
25084 emit_barrier ();
25086 /* Ensure we have a global entry point for the thunk. ??? We could
25087 avoid that if the target routine doesn't need a global entry point,
25088 but we do not know whether this is the case at this point. */
25089 if (DEFAULT_ABI == ABI_ELFv2)
25090 cfun->machine->r2_setup_needed = true;
25092 /* Run just enough of rest_of_compilation to get the insns emitted.
25093 There's not really enough bulk here to make other passes such as
25094 instruction scheduling worth while. Note that use_thunk calls
25095 assemble_start_function and assemble_end_function. */
25096 insn = get_insns ();
25097 shorten_branches (insn);
25098 final_start_function (insn, file, 1);
25099 final (insn, file, 1);
25100 final_end_function ();
25102 reload_completed = 0;
25103 epilogue_completed = 0;
25106 /* A quick summary of the various types of 'constant-pool tables'
25107 under PowerPC:
25109 Target Flags Name One table per
25110 AIX (none) AIX TOC object file
25111 AIX -mfull-toc AIX TOC object file
25112 AIX -mminimal-toc AIX minimal TOC translation unit
25113 SVR4/EABI (none) SVR4 SDATA object file
25114 SVR4/EABI -fpic SVR4 pic object file
25115 SVR4/EABI -fPIC SVR4 PIC translation unit
25116 SVR4/EABI -mrelocatable EABI TOC function
25117 SVR4/EABI -maix AIX TOC object file
25118 SVR4/EABI -maix -mminimal-toc
25119 AIX minimal TOC translation unit
25121 Name Reg. Set by entries contains:
25122 made by addrs? fp? sum?
25124 AIX TOC 2 crt0 as Y option option
25125 AIX minimal TOC 30 prolog gcc Y Y option
25126 SVR4 SDATA 13 crt0 gcc N Y N
25127 SVR4 pic 30 prolog ld Y not yet N
25128 SVR4 PIC 30 prolog gcc Y option option
25129 EABI TOC 30 prolog gcc Y option option
25133 /* Hash functions for the hash table. */
25135 static unsigned
25136 rs6000_hash_constant (rtx k)
25138 enum rtx_code code = GET_CODE (k);
25139 enum machine_mode mode = GET_MODE (k);
25140 unsigned result = (code << 3) ^ mode;
25141 const char *format;
25142 int flen, fidx;
25144 format = GET_RTX_FORMAT (code);
25145 flen = strlen (format);
25146 fidx = 0;
25148 switch (code)
25150 case LABEL_REF:
25151 return result * 1231 + (unsigned) INSN_UID (XEXP (k, 0));
25153 case CONST_DOUBLE:
25154 if (mode != VOIDmode)
25155 return real_hash (CONST_DOUBLE_REAL_VALUE (k)) * result;
25156 flen = 2;
25157 break;
25159 case CODE_LABEL:
25160 fidx = 3;
25161 break;
25163 default:
25164 break;
25167 for (; fidx < flen; fidx++)
25168 switch (format[fidx])
25170 case 's':
25172 unsigned i, len;
25173 const char *str = XSTR (k, fidx);
25174 len = strlen (str);
25175 result = result * 613 + len;
25176 for (i = 0; i < len; i++)
25177 result = result * 613 + (unsigned) str[i];
25178 break;
25180 case 'u':
25181 case 'e':
25182 result = result * 1231 + rs6000_hash_constant (XEXP (k, fidx));
25183 break;
25184 case 'i':
25185 case 'n':
25186 result = result * 613 + (unsigned) XINT (k, fidx);
25187 break;
25188 case 'w':
25189 if (sizeof (unsigned) >= sizeof (HOST_WIDE_INT))
25190 result = result * 613 + (unsigned) XWINT (k, fidx);
25191 else
25193 size_t i;
25194 for (i = 0; i < sizeof (HOST_WIDE_INT) / sizeof (unsigned); i++)
25195 result = result * 613 + (unsigned) (XWINT (k, fidx)
25196 >> CHAR_BIT * i);
25198 break;
25199 case '0':
25200 break;
25201 default:
25202 gcc_unreachable ();
25205 return result;
25208 static unsigned
25209 toc_hash_function (const void *hash_entry)
25211 const struct toc_hash_struct *thc =
25212 (const struct toc_hash_struct *) hash_entry;
25213 return rs6000_hash_constant (thc->key) ^ thc->key_mode;
25216 /* Compare H1 and H2 for equivalence. */
25218 static int
25219 toc_hash_eq (const void *h1, const void *h2)
25221 rtx r1 = ((const struct toc_hash_struct *) h1)->key;
25222 rtx r2 = ((const struct toc_hash_struct *) h2)->key;
25224 if (((const struct toc_hash_struct *) h1)->key_mode
25225 != ((const struct toc_hash_struct *) h2)->key_mode)
25226 return 0;
25228 return rtx_equal_p (r1, r2);
25231 /* These are the names given by the C++ front-end to vtables, and
25232 vtable-like objects. Ideally, this logic should not be here;
25233 instead, there should be some programmatic way of inquiring as
25234 to whether or not an object is a vtable. */
25236 #define VTABLE_NAME_P(NAME) \
25237 (strncmp ("_vt.", name, strlen ("_vt.")) == 0 \
25238 || strncmp ("_ZTV", name, strlen ("_ZTV")) == 0 \
25239 || strncmp ("_ZTT", name, strlen ("_ZTT")) == 0 \
25240 || strncmp ("_ZTI", name, strlen ("_ZTI")) == 0 \
25241 || strncmp ("_ZTC", name, strlen ("_ZTC")) == 0)
25243 #ifdef NO_DOLLAR_IN_LABEL
25244 /* Return a GGC-allocated character string translating dollar signs in
25245 input NAME to underscores. Used by XCOFF ASM_OUTPUT_LABELREF. */
25247 const char *
25248 rs6000_xcoff_strip_dollar (const char *name)
25250 char *strip, *p;
25251 const char *q;
25252 size_t len;
25254 q = (const char *) strchr (name, '$');
25256 if (q == 0 || q == name)
25257 return name;
25259 len = strlen (name);
25260 strip = XALLOCAVEC (char, len + 1);
25261 strcpy (strip, name);
25262 p = strip + (q - name);
25263 while (p)
25265 *p = '_';
25266 p = strchr (p + 1, '$');
25269 return ggc_alloc_string (strip, len);
25271 #endif
25273 void
25274 rs6000_output_symbol_ref (FILE *file, rtx x)
25276 /* Currently C++ toc references to vtables can be emitted before it
25277 is decided whether the vtable is public or private. If this is
25278 the case, then the linker will eventually complain that there is
25279 a reference to an unknown section. Thus, for vtables only,
25280 we emit the TOC reference to reference the symbol and not the
25281 section. */
25282 const char *name = XSTR (x, 0);
25284 if (VTABLE_NAME_P (name))
25286 RS6000_OUTPUT_BASENAME (file, name);
25288 else
25289 assemble_name (file, name);
25292 /* Output a TOC entry. We derive the entry name from what is being
25293 written. */
25295 void
25296 output_toc (FILE *file, rtx x, int labelno, enum machine_mode mode)
25298 char buf[256];
25299 const char *name = buf;
25300 rtx base = x;
25301 HOST_WIDE_INT offset = 0;
25303 gcc_assert (!TARGET_NO_TOC);
25305 /* When the linker won't eliminate them, don't output duplicate
25306 TOC entries (this happens on AIX if there is any kind of TOC,
25307 and on SVR4 under -fPIC or -mrelocatable). Don't do this for
25308 CODE_LABELs. */
25309 if (TARGET_TOC && GET_CODE (x) != LABEL_REF)
25311 struct toc_hash_struct *h;
25312 void * * found;
25314 /* Create toc_hash_table. This can't be done at TARGET_OPTION_OVERRIDE
25315 time because GGC is not initialized at that point. */
25316 if (toc_hash_table == NULL)
25317 toc_hash_table = htab_create_ggc (1021, toc_hash_function,
25318 toc_hash_eq, NULL);
25320 h = ggc_alloc_toc_hash_struct ();
25321 h->key = x;
25322 h->key_mode = mode;
25323 h->labelno = labelno;
25325 found = htab_find_slot (toc_hash_table, h, INSERT);
25326 if (*found == NULL)
25327 *found = h;
25328 else /* This is indeed a duplicate.
25329 Set this label equal to that label. */
25331 fputs ("\t.set ", file);
25332 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
25333 fprintf (file, "%d,", labelno);
25334 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
25335 fprintf (file, "%d\n", ((*(const struct toc_hash_struct **)
25336 found)->labelno));
25338 #ifdef HAVE_AS_TLS
25339 if (TARGET_XCOFF && GET_CODE (x) == SYMBOL_REF
25340 && (SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_GLOBAL_DYNAMIC
25341 || SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC))
25343 fputs ("\t.set ", file);
25344 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LCM");
25345 fprintf (file, "%d,", labelno);
25346 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LCM");
25347 fprintf (file, "%d\n", ((*(const struct toc_hash_struct **)
25348 found)->labelno));
25350 #endif
25351 return;
25355 /* If we're going to put a double constant in the TOC, make sure it's
25356 aligned properly when strict alignment is on. */
25357 if (GET_CODE (x) == CONST_DOUBLE
25358 && STRICT_ALIGNMENT
25359 && GET_MODE_BITSIZE (mode) >= 64
25360 && ! (TARGET_NO_FP_IN_TOC && ! TARGET_MINIMAL_TOC)) {
25361 ASM_OUTPUT_ALIGN (file, 3);
25364 (*targetm.asm_out.internal_label) (file, "LC", labelno);
25366 /* Handle FP constants specially. Note that if we have a minimal
25367 TOC, things we put here aren't actually in the TOC, so we can allow
25368 FP constants. */
25369 if (GET_CODE (x) == CONST_DOUBLE &&
25370 (GET_MODE (x) == TFmode || GET_MODE (x) == TDmode))
25372 REAL_VALUE_TYPE rv;
25373 long k[4];
25375 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
25376 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
25377 REAL_VALUE_TO_TARGET_DECIMAL128 (rv, k);
25378 else
25379 REAL_VALUE_TO_TARGET_LONG_DOUBLE (rv, k);
25381 if (TARGET_64BIT)
25383 if (TARGET_ELF || TARGET_MINIMAL_TOC)
25384 fputs (DOUBLE_INT_ASM_OP, file);
25385 else
25386 fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
25387 k[0] & 0xffffffff, k[1] & 0xffffffff,
25388 k[2] & 0xffffffff, k[3] & 0xffffffff);
25389 fprintf (file, "0x%lx%08lx,0x%lx%08lx\n",
25390 k[WORDS_BIG_ENDIAN ? 0 : 1] & 0xffffffff,
25391 k[WORDS_BIG_ENDIAN ? 1 : 0] & 0xffffffff,
25392 k[WORDS_BIG_ENDIAN ? 2 : 3] & 0xffffffff,
25393 k[WORDS_BIG_ENDIAN ? 3 : 2] & 0xffffffff);
25394 return;
25396 else
25398 if (TARGET_ELF || TARGET_MINIMAL_TOC)
25399 fputs ("\t.long ", file);
25400 else
25401 fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
25402 k[0] & 0xffffffff, k[1] & 0xffffffff,
25403 k[2] & 0xffffffff, k[3] & 0xffffffff);
25404 fprintf (file, "0x%lx,0x%lx,0x%lx,0x%lx\n",
25405 k[0] & 0xffffffff, k[1] & 0xffffffff,
25406 k[2] & 0xffffffff, k[3] & 0xffffffff);
25407 return;
25410 else if (GET_CODE (x) == CONST_DOUBLE &&
25411 (GET_MODE (x) == DFmode || GET_MODE (x) == DDmode))
25413 REAL_VALUE_TYPE rv;
25414 long k[2];
25416 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
25418 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
25419 REAL_VALUE_TO_TARGET_DECIMAL64 (rv, k);
25420 else
25421 REAL_VALUE_TO_TARGET_DOUBLE (rv, k);
25423 if (TARGET_64BIT)
25425 if (TARGET_ELF || TARGET_MINIMAL_TOC)
25426 fputs (DOUBLE_INT_ASM_OP, file);
25427 else
25428 fprintf (file, "\t.tc FD_%lx_%lx[TC],",
25429 k[0] & 0xffffffff, k[1] & 0xffffffff);
25430 fprintf (file, "0x%lx%08lx\n",
25431 k[WORDS_BIG_ENDIAN ? 0 : 1] & 0xffffffff,
25432 k[WORDS_BIG_ENDIAN ? 1 : 0] & 0xffffffff);
25433 return;
25435 else
25437 if (TARGET_ELF || TARGET_MINIMAL_TOC)
25438 fputs ("\t.long ", file);
25439 else
25440 fprintf (file, "\t.tc FD_%lx_%lx[TC],",
25441 k[0] & 0xffffffff, k[1] & 0xffffffff);
25442 fprintf (file, "0x%lx,0x%lx\n",
25443 k[0] & 0xffffffff, k[1] & 0xffffffff);
25444 return;
25447 else if (GET_CODE (x) == CONST_DOUBLE &&
25448 (GET_MODE (x) == SFmode || GET_MODE (x) == SDmode))
25450 REAL_VALUE_TYPE rv;
25451 long l;
25453 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
25454 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
25455 REAL_VALUE_TO_TARGET_DECIMAL32 (rv, l);
25456 else
25457 REAL_VALUE_TO_TARGET_SINGLE (rv, l);
25459 if (TARGET_64BIT)
25461 if (TARGET_ELF || TARGET_MINIMAL_TOC)
25462 fputs (DOUBLE_INT_ASM_OP, file);
25463 else
25464 fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
25465 if (WORDS_BIG_ENDIAN)
25466 fprintf (file, "0x%lx00000000\n", l & 0xffffffff);
25467 else
25468 fprintf (file, "0x%lx\n", l & 0xffffffff);
25469 return;
25471 else
25473 if (TARGET_ELF || TARGET_MINIMAL_TOC)
25474 fputs ("\t.long ", file);
25475 else
25476 fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
25477 fprintf (file, "0x%lx\n", l & 0xffffffff);
25478 return;
25481 else if (GET_MODE (x) == VOIDmode && GET_CODE (x) == CONST_INT)
25483 unsigned HOST_WIDE_INT low;
25484 HOST_WIDE_INT high;
25486 low = INTVAL (x) & 0xffffffff;
25487 high = (HOST_WIDE_INT) INTVAL (x) >> 32;
25489 /* TOC entries are always Pmode-sized, so when big-endian
25490 smaller integer constants in the TOC need to be padded.
25491 (This is still a win over putting the constants in
25492 a separate constant pool, because then we'd have
25493 to have both a TOC entry _and_ the actual constant.)
25495 For a 32-bit target, CONST_INT values are loaded and shifted
25496 entirely within `low' and can be stored in one TOC entry. */
25498 /* It would be easy to make this work, but it doesn't now. */
25499 gcc_assert (!TARGET_64BIT || POINTER_SIZE >= GET_MODE_BITSIZE (mode));
25501 if (WORDS_BIG_ENDIAN && POINTER_SIZE > GET_MODE_BITSIZE (mode))
25503 low |= high << 32;
25504 low <<= POINTER_SIZE - GET_MODE_BITSIZE (mode);
25505 high = (HOST_WIDE_INT) low >> 32;
25506 low &= 0xffffffff;
25509 if (TARGET_64BIT)
25511 if (TARGET_ELF || TARGET_MINIMAL_TOC)
25512 fputs (DOUBLE_INT_ASM_OP, file);
25513 else
25514 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
25515 (long) high & 0xffffffff, (long) low & 0xffffffff);
25516 fprintf (file, "0x%lx%08lx\n",
25517 (long) high & 0xffffffff, (long) low & 0xffffffff);
25518 return;
25520 else
25522 if (POINTER_SIZE < GET_MODE_BITSIZE (mode))
25524 if (TARGET_ELF || TARGET_MINIMAL_TOC)
25525 fputs ("\t.long ", file);
25526 else
25527 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
25528 (long) high & 0xffffffff, (long) low & 0xffffffff);
25529 fprintf (file, "0x%lx,0x%lx\n",
25530 (long) high & 0xffffffff, (long) low & 0xffffffff);
25532 else
25534 if (TARGET_ELF || TARGET_MINIMAL_TOC)
25535 fputs ("\t.long ", file);
25536 else
25537 fprintf (file, "\t.tc IS_%lx[TC],", (long) low & 0xffffffff);
25538 fprintf (file, "0x%lx\n", (long) low & 0xffffffff);
25540 return;
25544 if (GET_CODE (x) == CONST)
25546 gcc_assert (GET_CODE (XEXP (x, 0)) == PLUS
25547 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT);
25549 base = XEXP (XEXP (x, 0), 0);
25550 offset = INTVAL (XEXP (XEXP (x, 0), 1));
25553 switch (GET_CODE (base))
25555 case SYMBOL_REF:
25556 name = XSTR (base, 0);
25557 break;
25559 case LABEL_REF:
25560 ASM_GENERATE_INTERNAL_LABEL (buf, "L",
25561 CODE_LABEL_NUMBER (XEXP (base, 0)));
25562 break;
25564 case CODE_LABEL:
25565 ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (base));
25566 break;
25568 default:
25569 gcc_unreachable ();
25572 if (TARGET_ELF || TARGET_MINIMAL_TOC)
25573 fputs (TARGET_32BIT ? "\t.long " : DOUBLE_INT_ASM_OP, file);
25574 else
25576 fputs ("\t.tc ", file);
25577 RS6000_OUTPUT_BASENAME (file, name);
25579 if (offset < 0)
25580 fprintf (file, ".N" HOST_WIDE_INT_PRINT_UNSIGNED, - offset);
25581 else if (offset)
25582 fprintf (file, ".P" HOST_WIDE_INT_PRINT_UNSIGNED, offset);
25584 /* Mark large TOC symbols on AIX with [TE] so they are mapped
25585 after other TOC symbols, reducing overflow of small TOC access
25586 to [TC] symbols. */
25587 fputs (TARGET_XCOFF && TARGET_CMODEL != CMODEL_SMALL
25588 ? "[TE]," : "[TC],", file);
25591 /* Currently C++ toc references to vtables can be emitted before it
25592 is decided whether the vtable is public or private. If this is
25593 the case, then the linker will eventually complain that there is
25594 a TOC reference to an unknown section. Thus, for vtables only,
25595 we emit the TOC reference to reference the symbol and not the
25596 section. */
25597 if (VTABLE_NAME_P (name))
25599 RS6000_OUTPUT_BASENAME (file, name);
25600 if (offset < 0)
25601 fprintf (file, HOST_WIDE_INT_PRINT_DEC, offset);
25602 else if (offset > 0)
25603 fprintf (file, "+" HOST_WIDE_INT_PRINT_DEC, offset);
25605 else
25606 output_addr_const (file, x);
25608 #if HAVE_AS_TLS
25609 if (TARGET_XCOFF && GET_CODE (base) == SYMBOL_REF
25610 && SYMBOL_REF_TLS_MODEL (base) != 0)
25612 if (SYMBOL_REF_TLS_MODEL (base) == TLS_MODEL_LOCAL_EXEC)
25613 fputs ("@le", file);
25614 else if (SYMBOL_REF_TLS_MODEL (base) == TLS_MODEL_INITIAL_EXEC)
25615 fputs ("@ie", file);
25616 /* Use global-dynamic for local-dynamic. */
25617 else if (SYMBOL_REF_TLS_MODEL (base) == TLS_MODEL_GLOBAL_DYNAMIC
25618 || SYMBOL_REF_TLS_MODEL (base) == TLS_MODEL_LOCAL_DYNAMIC)
25620 putc ('\n', file);
25621 (*targetm.asm_out.internal_label) (file, "LCM", labelno);
25622 fputs ("\t.tc .", file);
25623 RS6000_OUTPUT_BASENAME (file, name);
25624 fputs ("[TC],", file);
25625 output_addr_const (file, x);
25626 fputs ("@m", file);
25629 #endif
25631 putc ('\n', file);
25634 /* Output an assembler pseudo-op to write an ASCII string of N characters
25635 starting at P to FILE.
25637 On the RS/6000, we have to do this using the .byte operation and
25638 write out special characters outside the quoted string.
25639 Also, the assembler is broken; very long strings are truncated,
25640 so we must artificially break them up early. */
25642 void
25643 output_ascii (FILE *file, const char *p, int n)
25645 char c;
25646 int i, count_string;
25647 const char *for_string = "\t.byte \"";
25648 const char *for_decimal = "\t.byte ";
25649 const char *to_close = NULL;
25651 count_string = 0;
25652 for (i = 0; i < n; i++)
25654 c = *p++;
25655 if (c >= ' ' && c < 0177)
25657 if (for_string)
25658 fputs (for_string, file);
25659 putc (c, file);
25661 /* Write two quotes to get one. */
25662 if (c == '"')
25664 putc (c, file);
25665 ++count_string;
25668 for_string = NULL;
25669 for_decimal = "\"\n\t.byte ";
25670 to_close = "\"\n";
25671 ++count_string;
25673 if (count_string >= 512)
25675 fputs (to_close, file);
25677 for_string = "\t.byte \"";
25678 for_decimal = "\t.byte ";
25679 to_close = NULL;
25680 count_string = 0;
25683 else
25685 if (for_decimal)
25686 fputs (for_decimal, file);
25687 fprintf (file, "%d", c);
25689 for_string = "\n\t.byte \"";
25690 for_decimal = ", ";
25691 to_close = "\n";
25692 count_string = 0;
25696 /* Now close the string if we have written one. Then end the line. */
25697 if (to_close)
25698 fputs (to_close, file);
25701 /* Generate a unique section name for FILENAME for a section type
25702 represented by SECTION_DESC. Output goes into BUF.
25704 SECTION_DESC can be any string, as long as it is different for each
25705 possible section type.
25707 We name the section in the same manner as xlc. The name begins with an
25708 underscore followed by the filename (after stripping any leading directory
25709 names) with the last period replaced by the string SECTION_DESC. If
25710 FILENAME does not contain a period, SECTION_DESC is appended to the end of
25711 the name. */
25713 void
25714 rs6000_gen_section_name (char **buf, const char *filename,
25715 const char *section_desc)
25717 const char *q, *after_last_slash, *last_period = 0;
25718 char *p;
25719 int len;
25721 after_last_slash = filename;
25722 for (q = filename; *q; q++)
25724 if (*q == '/')
25725 after_last_slash = q + 1;
25726 else if (*q == '.')
25727 last_period = q;
25730 len = strlen (after_last_slash) + strlen (section_desc) + 2;
25731 *buf = (char *) xmalloc (len);
25733 p = *buf;
25734 *p++ = '_';
25736 for (q = after_last_slash; *q; q++)
25738 if (q == last_period)
25740 strcpy (p, section_desc);
25741 p += strlen (section_desc);
25742 break;
25745 else if (ISALNUM (*q))
25746 *p++ = *q;
25749 if (last_period == 0)
25750 strcpy (p, section_desc);
25751 else
25752 *p = '\0';
25755 /* Emit profile function. */
25757 void
25758 output_profile_hook (int labelno ATTRIBUTE_UNUSED)
25760 /* Non-standard profiling for kernels, which just saves LR then calls
25761 _mcount without worrying about arg saves. The idea is to change
25762 the function prologue as little as possible as it isn't easy to
25763 account for arg save/restore code added just for _mcount. */
25764 if (TARGET_PROFILE_KERNEL)
25765 return;
25767 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
25769 #ifndef NO_PROFILE_COUNTERS
25770 # define NO_PROFILE_COUNTERS 0
25771 #endif
25772 if (NO_PROFILE_COUNTERS)
25773 emit_library_call (init_one_libfunc (RS6000_MCOUNT),
25774 LCT_NORMAL, VOIDmode, 0);
25775 else
25777 char buf[30];
25778 const char *label_name;
25779 rtx fun;
25781 ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
25782 label_name = ggc_strdup ((*targetm.strip_name_encoding) (buf));
25783 fun = gen_rtx_SYMBOL_REF (Pmode, label_name);
25785 emit_library_call (init_one_libfunc (RS6000_MCOUNT),
25786 LCT_NORMAL, VOIDmode, 1, fun, Pmode);
25789 else if (DEFAULT_ABI == ABI_DARWIN)
25791 const char *mcount_name = RS6000_MCOUNT;
25792 int caller_addr_regno = LR_REGNO;
25794 /* Be conservative and always set this, at least for now. */
25795 crtl->uses_pic_offset_table = 1;
25797 #if TARGET_MACHO
25798 /* For PIC code, set up a stub and collect the caller's address
25799 from r0, which is where the prologue puts it. */
25800 if (MACHOPIC_INDIRECT
25801 && crtl->uses_pic_offset_table)
25802 caller_addr_regno = 0;
25803 #endif
25804 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, mcount_name),
25805 LCT_NORMAL, VOIDmode, 1,
25806 gen_rtx_REG (Pmode, caller_addr_regno), Pmode);
25810 /* Write function profiler code. */
25812 void
25813 output_function_profiler (FILE *file, int labelno)
25815 char buf[100];
25817 switch (DEFAULT_ABI)
25819 default:
25820 gcc_unreachable ();
25822 case ABI_V4:
25823 if (!TARGET_32BIT)
25825 warning (0, "no profiling of 64-bit code for this ABI");
25826 return;
25828 ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
25829 fprintf (file, "\tmflr %s\n", reg_names[0]);
25830 if (NO_PROFILE_COUNTERS)
25832 asm_fprintf (file, "\tstw %s,4(%s)\n",
25833 reg_names[0], reg_names[1]);
25835 else if (TARGET_SECURE_PLT && flag_pic)
25837 if (TARGET_LINK_STACK)
25839 char name[32];
25840 get_ppc476_thunk_name (name);
25841 asm_fprintf (file, "\tbl %s\n", name);
25843 else
25844 asm_fprintf (file, "\tbcl 20,31,1f\n1:\n");
25845 asm_fprintf (file, "\tstw %s,4(%s)\n",
25846 reg_names[0], reg_names[1]);
25847 asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
25848 asm_fprintf (file, "\taddis %s,%s,",
25849 reg_names[12], reg_names[12]);
25850 assemble_name (file, buf);
25851 asm_fprintf (file, "-1b@ha\n\tla %s,", reg_names[0]);
25852 assemble_name (file, buf);
25853 asm_fprintf (file, "-1b@l(%s)\n", reg_names[12]);
25855 else if (flag_pic == 1)
25857 fputs ("\tbl _GLOBAL_OFFSET_TABLE_@local-4\n", file);
25858 asm_fprintf (file, "\tstw %s,4(%s)\n",
25859 reg_names[0], reg_names[1]);
25860 asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
25861 asm_fprintf (file, "\tlwz %s,", reg_names[0]);
25862 assemble_name (file, buf);
25863 asm_fprintf (file, "@got(%s)\n", reg_names[12]);
25865 else if (flag_pic > 1)
25867 asm_fprintf (file, "\tstw %s,4(%s)\n",
25868 reg_names[0], reg_names[1]);
25869 /* Now, we need to get the address of the label. */
25870 if (TARGET_LINK_STACK)
25872 char name[32];
25873 get_ppc476_thunk_name (name);
25874 asm_fprintf (file, "\tbl %s\n\tb 1f\n\t.long ", name);
25875 assemble_name (file, buf);
25876 fputs ("-.\n1:", file);
25877 asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
25878 asm_fprintf (file, "\taddi %s,%s,4\n",
25879 reg_names[11], reg_names[11]);
25881 else
25883 fputs ("\tbcl 20,31,1f\n\t.long ", file);
25884 assemble_name (file, buf);
25885 fputs ("-.\n1:", file);
25886 asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
25888 asm_fprintf (file, "\tlwz %s,0(%s)\n",
25889 reg_names[0], reg_names[11]);
25890 asm_fprintf (file, "\tadd %s,%s,%s\n",
25891 reg_names[0], reg_names[0], reg_names[11]);
25893 else
25895 asm_fprintf (file, "\tlis %s,", reg_names[12]);
25896 assemble_name (file, buf);
25897 fputs ("@ha\n", file);
25898 asm_fprintf (file, "\tstw %s,4(%s)\n",
25899 reg_names[0], reg_names[1]);
25900 asm_fprintf (file, "\tla %s,", reg_names[0]);
25901 assemble_name (file, buf);
25902 asm_fprintf (file, "@l(%s)\n", reg_names[12]);
25905 /* ABI_V4 saves the static chain reg with ASM_OUTPUT_REG_PUSH. */
25906 fprintf (file, "\tbl %s%s\n",
25907 RS6000_MCOUNT, flag_pic ? "@plt" : "");
25908 break;
25910 case ABI_AIX:
25911 case ABI_ELFv2:
25912 case ABI_DARWIN:
25913 /* Don't do anything, done in output_profile_hook (). */
25914 break;
25920 /* The following variable value is the last issued insn. */
25922 static rtx last_scheduled_insn;
25924 /* The following variable helps to balance issuing of load and
25925 store instructions */
25927 static int load_store_pendulum;
25929 /* Power4 load update and store update instructions are cracked into a
25930 load or store and an integer insn which are executed in the same cycle.
25931 Branches have their own dispatch slot which does not count against the
25932 GCC issue rate, but it changes the program flow so there are no other
25933 instructions to issue in this cycle. */
25935 static int
25936 rs6000_variable_issue_1 (rtx insn, int more)
25938 last_scheduled_insn = insn;
25939 if (GET_CODE (PATTERN (insn)) == USE
25940 || GET_CODE (PATTERN (insn)) == CLOBBER)
25942 cached_can_issue_more = more;
25943 return cached_can_issue_more;
25946 if (insn_terminates_group_p (insn, current_group))
25948 cached_can_issue_more = 0;
25949 return cached_can_issue_more;
25952 /* If no reservation, but reach here */
25953 if (recog_memoized (insn) < 0)
25954 return more;
25956 if (rs6000_sched_groups)
25958 if (is_microcoded_insn (insn))
25959 cached_can_issue_more = 0;
25960 else if (is_cracked_insn (insn))
25961 cached_can_issue_more = more > 2 ? more - 2 : 0;
25962 else
25963 cached_can_issue_more = more - 1;
25965 return cached_can_issue_more;
25968 if (rs6000_cpu_attr == CPU_CELL && is_nonpipeline_insn (insn))
25969 return 0;
25971 cached_can_issue_more = more - 1;
25972 return cached_can_issue_more;
25975 static int
25976 rs6000_variable_issue (FILE *stream, int verbose, rtx insn, int more)
25978 int r = rs6000_variable_issue_1 (insn, more);
25979 if (verbose)
25980 fprintf (stream, "// rs6000_variable_issue (more = %d) = %d\n", more, r);
25981 return r;
25984 /* Adjust the cost of a scheduling dependency. Return the new cost of
25985 a dependency LINK or INSN on DEP_INSN. COST is the current cost. */
25987 static int
25988 rs6000_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
25990 enum attr_type attr_type;
25992 if (! recog_memoized (insn))
25993 return 0;
25995 switch (REG_NOTE_KIND (link))
25997 case REG_DEP_TRUE:
25999 /* Data dependency; DEP_INSN writes a register that INSN reads
26000 some cycles later. */
26002 /* Separate a load from a narrower, dependent store. */
26003 if (rs6000_sched_groups
26004 && GET_CODE (PATTERN (insn)) == SET
26005 && GET_CODE (PATTERN (dep_insn)) == SET
26006 && GET_CODE (XEXP (PATTERN (insn), 1)) == MEM
26007 && GET_CODE (XEXP (PATTERN (dep_insn), 0)) == MEM
26008 && (GET_MODE_SIZE (GET_MODE (XEXP (PATTERN (insn), 1)))
26009 > GET_MODE_SIZE (GET_MODE (XEXP (PATTERN (dep_insn), 0)))))
26010 return cost + 14;
26012 attr_type = get_attr_type (insn);
26014 switch (attr_type)
26016 case TYPE_JMPREG:
26017 /* Tell the first scheduling pass about the latency between
26018 a mtctr and bctr (and mtlr and br/blr). The first
26019 scheduling pass will not know about this latency since
26020 the mtctr instruction, which has the latency associated
26021 to it, will be generated by reload. */
26022 return 4;
26023 case TYPE_BRANCH:
26024 /* Leave some extra cycles between a compare and its
26025 dependent branch, to inhibit expensive mispredicts. */
26026 if ((rs6000_cpu_attr == CPU_PPC603
26027 || rs6000_cpu_attr == CPU_PPC604
26028 || rs6000_cpu_attr == CPU_PPC604E
26029 || rs6000_cpu_attr == CPU_PPC620
26030 || rs6000_cpu_attr == CPU_PPC630
26031 || rs6000_cpu_attr == CPU_PPC750
26032 || rs6000_cpu_attr == CPU_PPC7400
26033 || rs6000_cpu_attr == CPU_PPC7450
26034 || rs6000_cpu_attr == CPU_PPCE5500
26035 || rs6000_cpu_attr == CPU_PPCE6500
26036 || rs6000_cpu_attr == CPU_POWER4
26037 || rs6000_cpu_attr == CPU_POWER5
26038 || rs6000_cpu_attr == CPU_POWER7
26039 || rs6000_cpu_attr == CPU_POWER8
26040 || rs6000_cpu_attr == CPU_CELL)
26041 && recog_memoized (dep_insn)
26042 && (INSN_CODE (dep_insn) >= 0))
26044 switch (get_attr_type (dep_insn))
26046 case TYPE_CMP:
26047 case TYPE_COMPARE:
26048 case TYPE_DELAYED_COMPARE:
26049 case TYPE_IMUL_COMPARE:
26050 case TYPE_LMUL_COMPARE:
26051 case TYPE_FPCOMPARE:
26052 case TYPE_CR_LOGICAL:
26053 case TYPE_DELAYED_CR:
26054 return cost + 2;
26055 default:
26056 break;
26058 break;
26060 case TYPE_STORE:
26061 case TYPE_STORE_U:
26062 case TYPE_STORE_UX:
26063 case TYPE_FPSTORE:
26064 case TYPE_FPSTORE_U:
26065 case TYPE_FPSTORE_UX:
26066 if ((rs6000_cpu == PROCESSOR_POWER6)
26067 && recog_memoized (dep_insn)
26068 && (INSN_CODE (dep_insn) >= 0))
26071 if (GET_CODE (PATTERN (insn)) != SET)
26072 /* If this happens, we have to extend this to schedule
26073 optimally. Return default for now. */
26074 return cost;
26076 /* Adjust the cost for the case where the value written
26077 by a fixed point operation is used as the address
26078 gen value on a store. */
26079 switch (get_attr_type (dep_insn))
26081 case TYPE_LOAD:
26082 case TYPE_LOAD_U:
26083 case TYPE_LOAD_UX:
26084 case TYPE_CNTLZ:
26086 if (! store_data_bypass_p (dep_insn, insn))
26087 return 4;
26088 break;
26090 case TYPE_LOAD_EXT:
26091 case TYPE_LOAD_EXT_U:
26092 case TYPE_LOAD_EXT_UX:
26093 case TYPE_VAR_SHIFT_ROTATE:
26094 case TYPE_VAR_DELAYED_COMPARE:
26096 if (! store_data_bypass_p (dep_insn, insn))
26097 return 6;
26098 break;
26100 case TYPE_INTEGER:
26101 case TYPE_COMPARE:
26102 case TYPE_FAST_COMPARE:
26103 case TYPE_EXTS:
26104 case TYPE_SHIFT:
26105 case TYPE_INSERT_WORD:
26106 case TYPE_INSERT_DWORD:
26107 case TYPE_FPLOAD_U:
26108 case TYPE_FPLOAD_UX:
26109 case TYPE_STORE_U:
26110 case TYPE_STORE_UX:
26111 case TYPE_FPSTORE_U:
26112 case TYPE_FPSTORE_UX:
26114 if (! store_data_bypass_p (dep_insn, insn))
26115 return 3;
26116 break;
26118 case TYPE_IMUL:
26119 case TYPE_IMUL2:
26120 case TYPE_IMUL3:
26121 case TYPE_LMUL:
26122 case TYPE_IMUL_COMPARE:
26123 case TYPE_LMUL_COMPARE:
26125 if (! store_data_bypass_p (dep_insn, insn))
26126 return 17;
26127 break;
26129 case TYPE_IDIV:
26131 if (! store_data_bypass_p (dep_insn, insn))
26132 return 45;
26133 break;
26135 case TYPE_LDIV:
26137 if (! store_data_bypass_p (dep_insn, insn))
26138 return 57;
26139 break;
26141 default:
26142 break;
26145 break;
26147 case TYPE_LOAD:
26148 case TYPE_LOAD_U:
26149 case TYPE_LOAD_UX:
26150 case TYPE_LOAD_EXT:
26151 case TYPE_LOAD_EXT_U:
26152 case TYPE_LOAD_EXT_UX:
26153 if ((rs6000_cpu == PROCESSOR_POWER6)
26154 && recog_memoized (dep_insn)
26155 && (INSN_CODE (dep_insn) >= 0))
26158 /* Adjust the cost for the case where the value written
26159 by a fixed point instruction is used within the address
26160 gen portion of a subsequent load(u)(x) */
26161 switch (get_attr_type (dep_insn))
26163 case TYPE_LOAD:
26164 case TYPE_LOAD_U:
26165 case TYPE_LOAD_UX:
26166 case TYPE_CNTLZ:
26168 if (set_to_load_agen (dep_insn, insn))
26169 return 4;
26170 break;
26172 case TYPE_LOAD_EXT:
26173 case TYPE_LOAD_EXT_U:
26174 case TYPE_LOAD_EXT_UX:
26175 case TYPE_VAR_SHIFT_ROTATE:
26176 case TYPE_VAR_DELAYED_COMPARE:
26178 if (set_to_load_agen (dep_insn, insn))
26179 return 6;
26180 break;
26182 case TYPE_INTEGER:
26183 case TYPE_COMPARE:
26184 case TYPE_FAST_COMPARE:
26185 case TYPE_EXTS:
26186 case TYPE_SHIFT:
26187 case TYPE_INSERT_WORD:
26188 case TYPE_INSERT_DWORD:
26189 case TYPE_FPLOAD_U:
26190 case TYPE_FPLOAD_UX:
26191 case TYPE_STORE_U:
26192 case TYPE_STORE_UX:
26193 case TYPE_FPSTORE_U:
26194 case TYPE_FPSTORE_UX:
26196 if (set_to_load_agen (dep_insn, insn))
26197 return 3;
26198 break;
26200 case TYPE_IMUL:
26201 case TYPE_IMUL2:
26202 case TYPE_IMUL3:
26203 case TYPE_LMUL:
26204 case TYPE_IMUL_COMPARE:
26205 case TYPE_LMUL_COMPARE:
26207 if (set_to_load_agen (dep_insn, insn))
26208 return 17;
26209 break;
26211 case TYPE_IDIV:
26213 if (set_to_load_agen (dep_insn, insn))
26214 return 45;
26215 break;
26217 case TYPE_LDIV:
26219 if (set_to_load_agen (dep_insn, insn))
26220 return 57;
26221 break;
26223 default:
26224 break;
26227 break;
26229 case TYPE_FPLOAD:
26230 if ((rs6000_cpu == PROCESSOR_POWER6)
26231 && recog_memoized (dep_insn)
26232 && (INSN_CODE (dep_insn) >= 0)
26233 && (get_attr_type (dep_insn) == TYPE_MFFGPR))
26234 return 2;
26236 default:
26237 break;
26240 /* Fall out to return default cost. */
26242 break;
26244 case REG_DEP_OUTPUT:
26245 /* Output dependency; DEP_INSN writes a register that INSN writes some
26246 cycles later. */
26247 if ((rs6000_cpu == PROCESSOR_POWER6)
26248 && recog_memoized (dep_insn)
26249 && (INSN_CODE (dep_insn) >= 0))
26251 attr_type = get_attr_type (insn);
26253 switch (attr_type)
26255 case TYPE_FP:
26256 if (get_attr_type (dep_insn) == TYPE_FP)
26257 return 1;
26258 break;
26259 case TYPE_FPLOAD:
26260 if (get_attr_type (dep_insn) == TYPE_MFFGPR)
26261 return 2;
26262 break;
26263 default:
26264 break;
26267 case REG_DEP_ANTI:
26268 /* Anti dependency; DEP_INSN reads a register that INSN writes some
26269 cycles later. */
26270 return 0;
26272 default:
26273 gcc_unreachable ();
26276 return cost;
26279 /* Debug version of rs6000_adjust_cost. */
26281 static int
26282 rs6000_debug_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
26284 int ret = rs6000_adjust_cost (insn, link, dep_insn, cost);
26286 if (ret != cost)
26288 const char *dep;
26290 switch (REG_NOTE_KIND (link))
26292 default: dep = "unknown depencency"; break;
26293 case REG_DEP_TRUE: dep = "data dependency"; break;
26294 case REG_DEP_OUTPUT: dep = "output dependency"; break;
26295 case REG_DEP_ANTI: dep = "anti depencency"; break;
26298 fprintf (stderr,
26299 "\nrs6000_adjust_cost, final cost = %d, orig cost = %d, "
26300 "%s, insn:\n", ret, cost, dep);
26302 debug_rtx (insn);
26305 return ret;
26308 /* The function returns a true if INSN is microcoded.
26309 Return false otherwise. */
26311 static bool
26312 is_microcoded_insn (rtx insn)
26314 if (!insn || !NONDEBUG_INSN_P (insn)
26315 || GET_CODE (PATTERN (insn)) == USE
26316 || GET_CODE (PATTERN (insn)) == CLOBBER)
26317 return false;
26319 if (rs6000_cpu_attr == CPU_CELL)
26320 return get_attr_cell_micro (insn) == CELL_MICRO_ALWAYS;
26322 if (rs6000_sched_groups
26323 && (rs6000_cpu == PROCESSOR_POWER4 || rs6000_cpu == PROCESSOR_POWER5))
26325 enum attr_type type = get_attr_type (insn);
26326 if (type == TYPE_LOAD_EXT_U
26327 || type == TYPE_LOAD_EXT_UX
26328 || type == TYPE_LOAD_UX
26329 || type == TYPE_STORE_UX
26330 || type == TYPE_MFCR)
26331 return true;
26334 return false;
26337 /* The function returns true if INSN is cracked into 2 instructions
26338 by the processor (and therefore occupies 2 issue slots). */
26340 static bool
26341 is_cracked_insn (rtx insn)
26343 if (!insn || !NONDEBUG_INSN_P (insn)
26344 || GET_CODE (PATTERN (insn)) == USE
26345 || GET_CODE (PATTERN (insn)) == CLOBBER)
26346 return false;
26348 if (rs6000_sched_groups
26349 && (rs6000_cpu == PROCESSOR_POWER4 || rs6000_cpu == PROCESSOR_POWER5))
26351 enum attr_type type = get_attr_type (insn);
26352 if (type == TYPE_LOAD_U || type == TYPE_STORE_U
26353 || type == TYPE_FPLOAD_U || type == TYPE_FPSTORE_U
26354 || type == TYPE_FPLOAD_UX || type == TYPE_FPSTORE_UX
26355 || type == TYPE_LOAD_EXT || type == TYPE_DELAYED_CR
26356 || type == TYPE_COMPARE || type == TYPE_DELAYED_COMPARE
26357 || type == TYPE_IMUL_COMPARE || type == TYPE_LMUL_COMPARE
26358 || type == TYPE_IDIV || type == TYPE_LDIV
26359 || type == TYPE_INSERT_WORD)
26360 return true;
26363 return false;
26366 /* The function returns true if INSN can be issued only from
26367 the branch slot. */
26369 static bool
26370 is_branch_slot_insn (rtx insn)
26372 if (!insn || !NONDEBUG_INSN_P (insn)
26373 || GET_CODE (PATTERN (insn)) == USE
26374 || GET_CODE (PATTERN (insn)) == CLOBBER)
26375 return false;
26377 if (rs6000_sched_groups)
26379 enum attr_type type = get_attr_type (insn);
26380 if (type == TYPE_BRANCH || type == TYPE_JMPREG)
26381 return true;
26382 return false;
26385 return false;
26388 /* The function returns true if out_inst sets a value that is
26389 used in the address generation computation of in_insn */
26390 static bool
26391 set_to_load_agen (rtx out_insn, rtx in_insn)
26393 rtx out_set, in_set;
26395 /* For performance reasons, only handle the simple case where
26396 both loads are a single_set. */
26397 out_set = single_set (out_insn);
26398 if (out_set)
26400 in_set = single_set (in_insn);
26401 if (in_set)
26402 return reg_mentioned_p (SET_DEST (out_set), SET_SRC (in_set));
26405 return false;
26408 /* Try to determine base/offset/size parts of the given MEM.
26409 Return true if successful, false if all the values couldn't
26410 be determined.
26412 This function only looks for REG or REG+CONST address forms.
26413 REG+REG address form will return false. */
26415 static bool
26416 get_memref_parts (rtx mem, rtx *base, HOST_WIDE_INT *offset,
26417 HOST_WIDE_INT *size)
26419 rtx addr_rtx;
26420 if MEM_SIZE_KNOWN_P (mem)
26421 *size = MEM_SIZE (mem);
26422 else
26423 return false;
26425 if (GET_CODE (XEXP (mem, 0)) == PRE_MODIFY)
26426 addr_rtx = XEXP (XEXP (mem, 0), 1);
26427 else
26428 addr_rtx = (XEXP (mem, 0));
26430 if (GET_CODE (addr_rtx) == REG)
26432 *base = addr_rtx;
26433 *offset = 0;
26435 else if (GET_CODE (addr_rtx) == PLUS
26436 && CONST_INT_P (XEXP (addr_rtx, 1)))
26438 *base = XEXP (addr_rtx, 0);
26439 *offset = INTVAL (XEXP (addr_rtx, 1));
26441 else
26442 return false;
26444 return true;
26447 /* The function returns true if the target storage location of
26448 mem1 is adjacent to the target storage location of mem2 */
26449 /* Return 1 if memory locations are adjacent. */
26451 static bool
26452 adjacent_mem_locations (rtx mem1, rtx mem2)
26454 rtx reg1, reg2;
26455 HOST_WIDE_INT off1, size1, off2, size2;
26457 if (get_memref_parts (mem1, &reg1, &off1, &size1)
26458 && get_memref_parts (mem2, &reg2, &off2, &size2))
26459 return ((REGNO (reg1) == REGNO (reg2))
26460 && ((off1 + size1 == off2)
26461 || (off2 + size2 == off1)));
26463 return false;
26466 /* This function returns true if it can be determined that the two MEM
26467 locations overlap by at least 1 byte based on base reg/offset/size. */
26469 static bool
26470 mem_locations_overlap (rtx mem1, rtx mem2)
26472 rtx reg1, reg2;
26473 HOST_WIDE_INT off1, size1, off2, size2;
26475 if (get_memref_parts (mem1, &reg1, &off1, &size1)
26476 && get_memref_parts (mem2, &reg2, &off2, &size2))
26477 return ((REGNO (reg1) == REGNO (reg2))
26478 && (((off1 <= off2) && (off1 + size1 > off2))
26479 || ((off2 <= off1) && (off2 + size2 > off1))));
26481 return false;
26484 /* A C statement (sans semicolon) to update the integer scheduling
26485 priority INSN_PRIORITY (INSN). Increase the priority to execute the
26486 INSN earlier, reduce the priority to execute INSN later. Do not
26487 define this macro if you do not need to adjust the scheduling
26488 priorities of insns. */
26490 static int
26491 rs6000_adjust_priority (rtx insn ATTRIBUTE_UNUSED, int priority)
26493 rtx load_mem, str_mem;
26494 /* On machines (like the 750) which have asymmetric integer units,
26495 where one integer unit can do multiply and divides and the other
26496 can't, reduce the priority of multiply/divide so it is scheduled
26497 before other integer operations. */
26499 #if 0
26500 if (! INSN_P (insn))
26501 return priority;
26503 if (GET_CODE (PATTERN (insn)) == USE)
26504 return priority;
26506 switch (rs6000_cpu_attr) {
26507 case CPU_PPC750:
26508 switch (get_attr_type (insn))
26510 default:
26511 break;
26513 case TYPE_IMUL:
26514 case TYPE_IDIV:
26515 fprintf (stderr, "priority was %#x (%d) before adjustment\n",
26516 priority, priority);
26517 if (priority >= 0 && priority < 0x01000000)
26518 priority >>= 3;
26519 break;
26522 #endif
26524 if (insn_must_be_first_in_group (insn)
26525 && reload_completed
26526 && current_sched_info->sched_max_insns_priority
26527 && rs6000_sched_restricted_insns_priority)
26530 /* Prioritize insns that can be dispatched only in the first
26531 dispatch slot. */
26532 if (rs6000_sched_restricted_insns_priority == 1)
26533 /* Attach highest priority to insn. This means that in
26534 haifa-sched.c:ready_sort(), dispatch-slot restriction considerations
26535 precede 'priority' (critical path) considerations. */
26536 return current_sched_info->sched_max_insns_priority;
26537 else if (rs6000_sched_restricted_insns_priority == 2)
26538 /* Increase priority of insn by a minimal amount. This means that in
26539 haifa-sched.c:ready_sort(), only 'priority' (critical path)
26540 considerations precede dispatch-slot restriction considerations. */
26541 return (priority + 1);
26544 if (rs6000_cpu == PROCESSOR_POWER6
26545 && ((load_store_pendulum == -2 && is_load_insn (insn, &load_mem))
26546 || (load_store_pendulum == 2 && is_store_insn (insn, &str_mem))))
26547 /* Attach highest priority to insn if the scheduler has just issued two
26548 stores and this instruction is a load, or two loads and this instruction
26549 is a store. Power6 wants loads and stores scheduled alternately
26550 when possible */
26551 return current_sched_info->sched_max_insns_priority;
26553 return priority;
26556 /* Return true if the instruction is nonpipelined on the Cell. */
26557 static bool
26558 is_nonpipeline_insn (rtx insn)
26560 enum attr_type type;
26561 if (!insn || !NONDEBUG_INSN_P (insn)
26562 || GET_CODE (PATTERN (insn)) == USE
26563 || GET_CODE (PATTERN (insn)) == CLOBBER)
26564 return false;
26566 type = get_attr_type (insn);
26567 if (type == TYPE_IMUL
26568 || type == TYPE_IMUL2
26569 || type == TYPE_IMUL3
26570 || type == TYPE_LMUL
26571 || type == TYPE_IDIV
26572 || type == TYPE_LDIV
26573 || type == TYPE_SDIV
26574 || type == TYPE_DDIV
26575 || type == TYPE_SSQRT
26576 || type == TYPE_DSQRT
26577 || type == TYPE_MFCR
26578 || type == TYPE_MFCRF
26579 || type == TYPE_MFJMPR)
26581 return true;
26583 return false;
26587 /* Return how many instructions the machine can issue per cycle. */
26589 static int
26590 rs6000_issue_rate (void)
26592 /* Unless scheduling for register pressure, use issue rate of 1 for
26593 first scheduling pass to decrease degradation. */
26594 if (!reload_completed && !flag_sched_pressure)
26595 return 1;
26597 switch (rs6000_cpu_attr) {
26598 case CPU_RS64A:
26599 case CPU_PPC601: /* ? */
26600 case CPU_PPC7450:
26601 return 3;
26602 case CPU_PPC440:
26603 case CPU_PPC603:
26604 case CPU_PPC750:
26605 case CPU_PPC7400:
26606 case CPU_PPC8540:
26607 case CPU_PPC8548:
26608 case CPU_CELL:
26609 case CPU_PPCE300C2:
26610 case CPU_PPCE300C3:
26611 case CPU_PPCE500MC:
26612 case CPU_PPCE500MC64:
26613 case CPU_PPCE5500:
26614 case CPU_PPCE6500:
26615 case CPU_TITAN:
26616 return 2;
26617 case CPU_PPC476:
26618 case CPU_PPC604:
26619 case CPU_PPC604E:
26620 case CPU_PPC620:
26621 case CPU_PPC630:
26622 return 4;
26623 case CPU_POWER4:
26624 case CPU_POWER5:
26625 case CPU_POWER6:
26626 case CPU_POWER7:
26627 return 5;
26628 case CPU_POWER8:
26629 return 7;
26630 default:
26631 return 1;
26635 /* Return how many instructions to look ahead for better insn
26636 scheduling. */
26638 static int
26639 rs6000_use_sched_lookahead (void)
26641 switch (rs6000_cpu_attr)
26643 case CPU_PPC8540:
26644 case CPU_PPC8548:
26645 return 4;
26647 case CPU_CELL:
26648 return (reload_completed ? 8 : 0);
26650 default:
26651 return 0;
26655 /* We are choosing insn from the ready queue. Return nonzero if INSN can be chosen. */
26656 static int
26657 rs6000_use_sched_lookahead_guard (rtx insn)
26659 if (rs6000_cpu_attr != CPU_CELL)
26660 return 1;
26662 if (insn == NULL_RTX || !INSN_P (insn))
26663 abort ();
26665 if (!reload_completed
26666 || is_nonpipeline_insn (insn)
26667 || is_microcoded_insn (insn))
26668 return 0;
26670 return 1;
26673 /* Determine if PAT refers to memory. If so, set MEM_REF to the MEM rtx
26674 and return true. */
26676 static bool
26677 find_mem_ref (rtx pat, rtx *mem_ref)
26679 const char * fmt;
26680 int i, j;
26682 /* stack_tie does not produce any real memory traffic. */
26683 if (tie_operand (pat, VOIDmode))
26684 return false;
26686 if (GET_CODE (pat) == MEM)
26688 *mem_ref = pat;
26689 return true;
26692 /* Recursively process the pattern. */
26693 fmt = GET_RTX_FORMAT (GET_CODE (pat));
26695 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
26697 if (fmt[i] == 'e')
26699 if (find_mem_ref (XEXP (pat, i), mem_ref))
26700 return true;
26702 else if (fmt[i] == 'E')
26703 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
26705 if (find_mem_ref (XVECEXP (pat, i, j), mem_ref))
26706 return true;
26710 return false;
26713 /* Determine if PAT is a PATTERN of a load insn. */
26715 static bool
26716 is_load_insn1 (rtx pat, rtx *load_mem)
26718 if (!pat || pat == NULL_RTX)
26719 return false;
26721 if (GET_CODE (pat) == SET)
26722 return find_mem_ref (SET_SRC (pat), load_mem);
26724 if (GET_CODE (pat) == PARALLEL)
26726 int i;
26728 for (i = 0; i < XVECLEN (pat, 0); i++)
26729 if (is_load_insn1 (XVECEXP (pat, 0, i), load_mem))
26730 return true;
26733 return false;
26736 /* Determine if INSN loads from memory. */
26738 static bool
26739 is_load_insn (rtx insn, rtx *load_mem)
26741 if (!insn || !INSN_P (insn))
26742 return false;
26744 if (CALL_P (insn))
26745 return false;
26747 return is_load_insn1 (PATTERN (insn), load_mem);
26750 /* Determine if PAT is a PATTERN of a store insn. */
26752 static bool
26753 is_store_insn1 (rtx pat, rtx *str_mem)
26755 if (!pat || pat == NULL_RTX)
26756 return false;
26758 if (GET_CODE (pat) == SET)
26759 return find_mem_ref (SET_DEST (pat), str_mem);
26761 if (GET_CODE (pat) == PARALLEL)
26763 int i;
26765 for (i = 0; i < XVECLEN (pat, 0); i++)
26766 if (is_store_insn1 (XVECEXP (pat, 0, i), str_mem))
26767 return true;
26770 return false;
26773 /* Determine if INSN stores to memory. */
26775 static bool
26776 is_store_insn (rtx insn, rtx *str_mem)
26778 if (!insn || !INSN_P (insn))
26779 return false;
26781 return is_store_insn1 (PATTERN (insn), str_mem);
26784 /* Returns whether the dependence between INSN and NEXT is considered
26785 costly by the given target. */
26787 static bool
26788 rs6000_is_costly_dependence (dep_t dep, int cost, int distance)
26790 rtx insn;
26791 rtx next;
26792 rtx load_mem, str_mem;
26794 /* If the flag is not enabled - no dependence is considered costly;
26795 allow all dependent insns in the same group.
26796 This is the most aggressive option. */
26797 if (rs6000_sched_costly_dep == no_dep_costly)
26798 return false;
26800 /* If the flag is set to 1 - a dependence is always considered costly;
26801 do not allow dependent instructions in the same group.
26802 This is the most conservative option. */
26803 if (rs6000_sched_costly_dep == all_deps_costly)
26804 return true;
26806 insn = DEP_PRO (dep);
26807 next = DEP_CON (dep);
26809 if (rs6000_sched_costly_dep == store_to_load_dep_costly
26810 && is_load_insn (next, &load_mem)
26811 && is_store_insn (insn, &str_mem))
26812 /* Prevent load after store in the same group. */
26813 return true;
26815 if (rs6000_sched_costly_dep == true_store_to_load_dep_costly
26816 && is_load_insn (next, &load_mem)
26817 && is_store_insn (insn, &str_mem)
26818 && DEP_TYPE (dep) == REG_DEP_TRUE
26819 && mem_locations_overlap(str_mem, load_mem))
26820 /* Prevent load after store in the same group if it is a true
26821 dependence. */
26822 return true;
26824 /* The flag is set to X; dependences with latency >= X are considered costly,
26825 and will not be scheduled in the same group. */
26826 if (rs6000_sched_costly_dep <= max_dep_latency
26827 && ((cost - distance) >= (int)rs6000_sched_costly_dep))
26828 return true;
26830 return false;
26833 /* Return the next insn after INSN that is found before TAIL is reached,
26834 skipping any "non-active" insns - insns that will not actually occupy
26835 an issue slot. Return NULL_RTX if such an insn is not found. */
26837 static rtx
26838 get_next_active_insn (rtx insn, rtx tail)
26840 if (insn == NULL_RTX || insn == tail)
26841 return NULL_RTX;
26843 while (1)
26845 insn = NEXT_INSN (insn);
26846 if (insn == NULL_RTX || insn == tail)
26847 return NULL_RTX;
26849 if (CALL_P (insn)
26850 || JUMP_P (insn) || JUMP_TABLE_DATA_P (insn)
26851 || (NONJUMP_INSN_P (insn)
26852 && GET_CODE (PATTERN (insn)) != USE
26853 && GET_CODE (PATTERN (insn)) != CLOBBER
26854 && INSN_CODE (insn) != CODE_FOR_stack_tie))
26855 break;
26857 return insn;
26860 /* We are about to begin issuing insns for this clock cycle. */
26862 static int
26863 rs6000_sched_reorder (FILE *dump ATTRIBUTE_UNUSED, int sched_verbose,
26864 rtx *ready ATTRIBUTE_UNUSED,
26865 int *pn_ready ATTRIBUTE_UNUSED,
26866 int clock_var ATTRIBUTE_UNUSED)
26868 int n_ready = *pn_ready;
26870 if (sched_verbose)
26871 fprintf (dump, "// rs6000_sched_reorder :\n");
26873 /* Reorder the ready list, if the second to last ready insn
26874 is a nonepipeline insn. */
26875 if (rs6000_cpu_attr == CPU_CELL && n_ready > 1)
26877 if (is_nonpipeline_insn (ready[n_ready - 1])
26878 && (recog_memoized (ready[n_ready - 2]) > 0))
26879 /* Simply swap first two insns. */
26881 rtx tmp = ready[n_ready - 1];
26882 ready[n_ready - 1] = ready[n_ready - 2];
26883 ready[n_ready - 2] = tmp;
26887 if (rs6000_cpu == PROCESSOR_POWER6)
26888 load_store_pendulum = 0;
26890 return rs6000_issue_rate ();
26893 /* Like rs6000_sched_reorder, but called after issuing each insn. */
26895 static int
26896 rs6000_sched_reorder2 (FILE *dump, int sched_verbose, rtx *ready,
26897 int *pn_ready, int clock_var ATTRIBUTE_UNUSED)
26899 if (sched_verbose)
26900 fprintf (dump, "// rs6000_sched_reorder2 :\n");
26902 /* For Power6, we need to handle some special cases to try and keep the
26903 store queue from overflowing and triggering expensive flushes.
26905 This code monitors how load and store instructions are being issued
26906 and skews the ready list one way or the other to increase the likelihood
26907 that a desired instruction is issued at the proper time.
26909 A couple of things are done. First, we maintain a "load_store_pendulum"
26910 to track the current state of load/store issue.
26912 - If the pendulum is at zero, then no loads or stores have been
26913 issued in the current cycle so we do nothing.
26915 - If the pendulum is 1, then a single load has been issued in this
26916 cycle and we attempt to locate another load in the ready list to
26917 issue with it.
26919 - If the pendulum is -2, then two stores have already been
26920 issued in this cycle, so we increase the priority of the first load
26921 in the ready list to increase it's likelihood of being chosen first
26922 in the next cycle.
26924 - If the pendulum is -1, then a single store has been issued in this
26925 cycle and we attempt to locate another store in the ready list to
26926 issue with it, preferring a store to an adjacent memory location to
26927 facilitate store pairing in the store queue.
26929 - If the pendulum is 2, then two loads have already been
26930 issued in this cycle, so we increase the priority of the first store
26931 in the ready list to increase it's likelihood of being chosen first
26932 in the next cycle.
26934 - If the pendulum < -2 or > 2, then do nothing.
26936 Note: This code covers the most common scenarios. There exist non
26937 load/store instructions which make use of the LSU and which
26938 would need to be accounted for to strictly model the behavior
26939 of the machine. Those instructions are currently unaccounted
26940 for to help minimize compile time overhead of this code.
26942 if (rs6000_cpu == PROCESSOR_POWER6 && last_scheduled_insn)
26944 int pos;
26945 int i;
26946 rtx tmp, load_mem, str_mem;
26948 if (is_store_insn (last_scheduled_insn, &str_mem))
26949 /* Issuing a store, swing the load_store_pendulum to the left */
26950 load_store_pendulum--;
26951 else if (is_load_insn (last_scheduled_insn, &load_mem))
26952 /* Issuing a load, swing the load_store_pendulum to the right */
26953 load_store_pendulum++;
26954 else
26955 return cached_can_issue_more;
26957 /* If the pendulum is balanced, or there is only one instruction on
26958 the ready list, then all is well, so return. */
26959 if ((load_store_pendulum == 0) || (*pn_ready <= 1))
26960 return cached_can_issue_more;
26962 if (load_store_pendulum == 1)
26964 /* A load has been issued in this cycle. Scan the ready list
26965 for another load to issue with it */
26966 pos = *pn_ready-1;
26968 while (pos >= 0)
26970 if (is_load_insn (ready[pos], &load_mem))
26972 /* Found a load. Move it to the head of the ready list,
26973 and adjust it's priority so that it is more likely to
26974 stay there */
26975 tmp = ready[pos];
26976 for (i=pos; i<*pn_ready-1; i++)
26977 ready[i] = ready[i + 1];
26978 ready[*pn_ready-1] = tmp;
26980 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
26981 INSN_PRIORITY (tmp)++;
26982 break;
26984 pos--;
26987 else if (load_store_pendulum == -2)
26989 /* Two stores have been issued in this cycle. Increase the
26990 priority of the first load in the ready list to favor it for
26991 issuing in the next cycle. */
26992 pos = *pn_ready-1;
26994 while (pos >= 0)
26996 if (is_load_insn (ready[pos], &load_mem)
26997 && !sel_sched_p ()
26998 && INSN_PRIORITY_KNOWN (ready[pos]))
27000 INSN_PRIORITY (ready[pos])++;
27002 /* Adjust the pendulum to account for the fact that a load
27003 was found and increased in priority. This is to prevent
27004 increasing the priority of multiple loads */
27005 load_store_pendulum--;
27007 break;
27009 pos--;
27012 else if (load_store_pendulum == -1)
27014 /* A store has been issued in this cycle. Scan the ready list for
27015 another store to issue with it, preferring a store to an adjacent
27016 memory location */
27017 int first_store_pos = -1;
27019 pos = *pn_ready-1;
27021 while (pos >= 0)
27023 if (is_store_insn (ready[pos], &str_mem))
27025 rtx str_mem2;
27026 /* Maintain the index of the first store found on the
27027 list */
27028 if (first_store_pos == -1)
27029 first_store_pos = pos;
27031 if (is_store_insn (last_scheduled_insn, &str_mem2)
27032 && adjacent_mem_locations (str_mem, str_mem2))
27034 /* Found an adjacent store. Move it to the head of the
27035 ready list, and adjust it's priority so that it is
27036 more likely to stay there */
27037 tmp = ready[pos];
27038 for (i=pos; i<*pn_ready-1; i++)
27039 ready[i] = ready[i + 1];
27040 ready[*pn_ready-1] = tmp;
27042 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
27043 INSN_PRIORITY (tmp)++;
27045 first_store_pos = -1;
27047 break;
27050 pos--;
27053 if (first_store_pos >= 0)
27055 /* An adjacent store wasn't found, but a non-adjacent store was,
27056 so move the non-adjacent store to the front of the ready
27057 list, and adjust its priority so that it is more likely to
27058 stay there. */
27059 tmp = ready[first_store_pos];
27060 for (i=first_store_pos; i<*pn_ready-1; i++)
27061 ready[i] = ready[i + 1];
27062 ready[*pn_ready-1] = tmp;
27063 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
27064 INSN_PRIORITY (tmp)++;
27067 else if (load_store_pendulum == 2)
27069 /* Two loads have been issued in this cycle. Increase the priority
27070 of the first store in the ready list to favor it for issuing in
27071 the next cycle. */
27072 pos = *pn_ready-1;
27074 while (pos >= 0)
27076 if (is_store_insn (ready[pos], &str_mem)
27077 && !sel_sched_p ()
27078 && INSN_PRIORITY_KNOWN (ready[pos]))
27080 INSN_PRIORITY (ready[pos])++;
27082 /* Adjust the pendulum to account for the fact that a store
27083 was found and increased in priority. This is to prevent
27084 increasing the priority of multiple stores */
27085 load_store_pendulum++;
27087 break;
27089 pos--;
27094 return cached_can_issue_more;
27097 /* Return whether the presence of INSN causes a dispatch group termination
27098 of group WHICH_GROUP.
27100 If WHICH_GROUP == current_group, this function will return true if INSN
27101 causes the termination of the current group (i.e, the dispatch group to
27102 which INSN belongs). This means that INSN will be the last insn in the
27103 group it belongs to.
27105 If WHICH_GROUP == previous_group, this function will return true if INSN
27106 causes the termination of the previous group (i.e, the dispatch group that
27107 precedes the group to which INSN belongs). This means that INSN will be
27108 the first insn in the group it belongs to). */
27110 static bool
27111 insn_terminates_group_p (rtx insn, enum group_termination which_group)
27113 bool first, last;
27115 if (! insn)
27116 return false;
27118 first = insn_must_be_first_in_group (insn);
27119 last = insn_must_be_last_in_group (insn);
27121 if (first && last)
27122 return true;
27124 if (which_group == current_group)
27125 return last;
27126 else if (which_group == previous_group)
27127 return first;
27129 return false;
27133 static bool
27134 insn_must_be_first_in_group (rtx insn)
27136 enum attr_type type;
27138 if (!insn
27139 || NOTE_P (insn)
27140 || DEBUG_INSN_P (insn)
27141 || GET_CODE (PATTERN (insn)) == USE
27142 || GET_CODE (PATTERN (insn)) == CLOBBER)
27143 return false;
27145 switch (rs6000_cpu)
27147 case PROCESSOR_POWER5:
27148 if (is_cracked_insn (insn))
27149 return true;
27150 case PROCESSOR_POWER4:
27151 if (is_microcoded_insn (insn))
27152 return true;
27154 if (!rs6000_sched_groups)
27155 return false;
27157 type = get_attr_type (insn);
27159 switch (type)
27161 case TYPE_MFCR:
27162 case TYPE_MFCRF:
27163 case TYPE_MTCR:
27164 case TYPE_DELAYED_CR:
27165 case TYPE_CR_LOGICAL:
27166 case TYPE_MTJMPR:
27167 case TYPE_MFJMPR:
27168 case TYPE_IDIV:
27169 case TYPE_LDIV:
27170 case TYPE_LOAD_L:
27171 case TYPE_STORE_C:
27172 case TYPE_ISYNC:
27173 case TYPE_SYNC:
27174 return true;
27175 default:
27176 break;
27178 break;
27179 case PROCESSOR_POWER6:
27180 type = get_attr_type (insn);
27182 switch (type)
27184 case TYPE_INSERT_DWORD:
27185 case TYPE_EXTS:
27186 case TYPE_CNTLZ:
27187 case TYPE_SHIFT:
27188 case TYPE_VAR_SHIFT_ROTATE:
27189 case TYPE_TRAP:
27190 case TYPE_IMUL:
27191 case TYPE_IMUL2:
27192 case TYPE_IMUL3:
27193 case TYPE_LMUL:
27194 case TYPE_IDIV:
27195 case TYPE_INSERT_WORD:
27196 case TYPE_DELAYED_COMPARE:
27197 case TYPE_IMUL_COMPARE:
27198 case TYPE_LMUL_COMPARE:
27199 case TYPE_FPCOMPARE:
27200 case TYPE_MFCR:
27201 case TYPE_MTCR:
27202 case TYPE_MFJMPR:
27203 case TYPE_MTJMPR:
27204 case TYPE_ISYNC:
27205 case TYPE_SYNC:
27206 case TYPE_LOAD_L:
27207 case TYPE_STORE_C:
27208 case TYPE_LOAD_U:
27209 case TYPE_LOAD_UX:
27210 case TYPE_LOAD_EXT_UX:
27211 case TYPE_STORE_U:
27212 case TYPE_STORE_UX:
27213 case TYPE_FPLOAD_U:
27214 case TYPE_FPLOAD_UX:
27215 case TYPE_FPSTORE_U:
27216 case TYPE_FPSTORE_UX:
27217 return true;
27218 default:
27219 break;
27221 break;
27222 case PROCESSOR_POWER7:
27223 type = get_attr_type (insn);
27225 switch (type)
27227 case TYPE_CR_LOGICAL:
27228 case TYPE_MFCR:
27229 case TYPE_MFCRF:
27230 case TYPE_MTCR:
27231 case TYPE_IDIV:
27232 case TYPE_LDIV:
27233 case TYPE_COMPARE:
27234 case TYPE_DELAYED_COMPARE:
27235 case TYPE_VAR_DELAYED_COMPARE:
27236 case TYPE_ISYNC:
27237 case TYPE_LOAD_L:
27238 case TYPE_STORE_C:
27239 case TYPE_LOAD_U:
27240 case TYPE_LOAD_UX:
27241 case TYPE_LOAD_EXT:
27242 case TYPE_LOAD_EXT_U:
27243 case TYPE_LOAD_EXT_UX:
27244 case TYPE_STORE_U:
27245 case TYPE_STORE_UX:
27246 case TYPE_FPLOAD_U:
27247 case TYPE_FPLOAD_UX:
27248 case TYPE_FPSTORE_U:
27249 case TYPE_FPSTORE_UX:
27250 case TYPE_MFJMPR:
27251 case TYPE_MTJMPR:
27252 return true;
27253 default:
27254 break;
27256 break;
27257 case PROCESSOR_POWER8:
27258 type = get_attr_type (insn);
27260 switch (type)
27262 case TYPE_CR_LOGICAL:
27263 case TYPE_DELAYED_CR:
27264 case TYPE_MFCR:
27265 case TYPE_MFCRF:
27266 case TYPE_MTCR:
27267 case TYPE_COMPARE:
27268 case TYPE_DELAYED_COMPARE:
27269 case TYPE_VAR_DELAYED_COMPARE:
27270 case TYPE_IMUL_COMPARE:
27271 case TYPE_LMUL_COMPARE:
27272 case TYPE_SYNC:
27273 case TYPE_ISYNC:
27274 case TYPE_LOAD_L:
27275 case TYPE_STORE_C:
27276 case TYPE_LOAD_U:
27277 case TYPE_LOAD_UX:
27278 case TYPE_LOAD_EXT:
27279 case TYPE_LOAD_EXT_U:
27280 case TYPE_LOAD_EXT_UX:
27281 case TYPE_STORE_UX:
27282 case TYPE_VECSTORE:
27283 case TYPE_MFJMPR:
27284 case TYPE_MTJMPR:
27285 return true;
27286 default:
27287 break;
27289 break;
27290 default:
27291 break;
27294 return false;
27297 static bool
27298 insn_must_be_last_in_group (rtx insn)
27300 enum attr_type type;
27302 if (!insn
27303 || NOTE_P (insn)
27304 || DEBUG_INSN_P (insn)
27305 || GET_CODE (PATTERN (insn)) == USE
27306 || GET_CODE (PATTERN (insn)) == CLOBBER)
27307 return false;
27309 switch (rs6000_cpu) {
27310 case PROCESSOR_POWER4:
27311 case PROCESSOR_POWER5:
27312 if (is_microcoded_insn (insn))
27313 return true;
27315 if (is_branch_slot_insn (insn))
27316 return true;
27318 break;
27319 case PROCESSOR_POWER6:
27320 type = get_attr_type (insn);
27322 switch (type)
27324 case TYPE_EXTS:
27325 case TYPE_CNTLZ:
27326 case TYPE_SHIFT:
27327 case TYPE_VAR_SHIFT_ROTATE:
27328 case TYPE_TRAP:
27329 case TYPE_IMUL:
27330 case TYPE_IMUL2:
27331 case TYPE_IMUL3:
27332 case TYPE_LMUL:
27333 case TYPE_IDIV:
27334 case TYPE_DELAYED_COMPARE:
27335 case TYPE_IMUL_COMPARE:
27336 case TYPE_LMUL_COMPARE:
27337 case TYPE_FPCOMPARE:
27338 case TYPE_MFCR:
27339 case TYPE_MTCR:
27340 case TYPE_MFJMPR:
27341 case TYPE_MTJMPR:
27342 case TYPE_ISYNC:
27343 case TYPE_SYNC:
27344 case TYPE_LOAD_L:
27345 case TYPE_STORE_C:
27346 return true;
27347 default:
27348 break;
27350 break;
27351 case PROCESSOR_POWER7:
27352 type = get_attr_type (insn);
27354 switch (type)
27356 case TYPE_ISYNC:
27357 case TYPE_SYNC:
27358 case TYPE_LOAD_L:
27359 case TYPE_STORE_C:
27360 case TYPE_LOAD_EXT_U:
27361 case TYPE_LOAD_EXT_UX:
27362 case TYPE_STORE_UX:
27363 return true;
27364 default:
27365 break;
27367 break;
27368 case PROCESSOR_POWER8:
27369 type = get_attr_type (insn);
27371 switch (type)
27373 case TYPE_MFCR:
27374 case TYPE_MTCR:
27375 case TYPE_ISYNC:
27376 case TYPE_SYNC:
27377 case TYPE_LOAD_L:
27378 case TYPE_STORE_C:
27379 case TYPE_LOAD_EXT_U:
27380 case TYPE_LOAD_EXT_UX:
27381 case TYPE_STORE_UX:
27382 return true;
27383 default:
27384 break;
27386 break;
27387 default:
27388 break;
27391 return false;
27394 /* Return true if it is recommended to keep NEXT_INSN "far" (in a separate
27395 dispatch group) from the insns in GROUP_INSNS. Return false otherwise. */
27397 static bool
27398 is_costly_group (rtx *group_insns, rtx next_insn)
27400 int i;
27401 int issue_rate = rs6000_issue_rate ();
27403 for (i = 0; i < issue_rate; i++)
27405 sd_iterator_def sd_it;
27406 dep_t dep;
27407 rtx insn = group_insns[i];
27409 if (!insn)
27410 continue;
27412 FOR_EACH_DEP (insn, SD_LIST_RES_FORW, sd_it, dep)
27414 rtx next = DEP_CON (dep);
27416 if (next == next_insn
27417 && rs6000_is_costly_dependence (dep, dep_cost (dep), 0))
27418 return true;
27422 return false;
27425 /* Utility of the function redefine_groups.
27426 Check if it is too costly to schedule NEXT_INSN together with GROUP_INSNS
27427 in the same dispatch group. If so, insert nops before NEXT_INSN, in order
27428 to keep it "far" (in a separate group) from GROUP_INSNS, following
27429 one of the following schemes, depending on the value of the flag
27430 -minsert_sched_nops = X:
27431 (1) X == sched_finish_regroup_exact: insert exactly as many nops as needed
27432 in order to force NEXT_INSN into a separate group.
27433 (2) X < sched_finish_regroup_exact: insert exactly X nops.
27434 GROUP_END, CAN_ISSUE_MORE and GROUP_COUNT record the state after nop
27435 insertion (has a group just ended, how many vacant issue slots remain in the
27436 last group, and how many dispatch groups were encountered so far). */
27438 static int
27439 force_new_group (int sched_verbose, FILE *dump, rtx *group_insns,
27440 rtx next_insn, bool *group_end, int can_issue_more,
27441 int *group_count)
27443 rtx nop;
27444 bool force;
27445 int issue_rate = rs6000_issue_rate ();
27446 bool end = *group_end;
27447 int i;
27449 if (next_insn == NULL_RTX || DEBUG_INSN_P (next_insn))
27450 return can_issue_more;
27452 if (rs6000_sched_insert_nops > sched_finish_regroup_exact)
27453 return can_issue_more;
27455 force = is_costly_group (group_insns, next_insn);
27456 if (!force)
27457 return can_issue_more;
27459 if (sched_verbose > 6)
27460 fprintf (dump,"force: group count = %d, can_issue_more = %d\n",
27461 *group_count ,can_issue_more);
27463 if (rs6000_sched_insert_nops == sched_finish_regroup_exact)
27465 if (*group_end)
27466 can_issue_more = 0;
27468 /* Since only a branch can be issued in the last issue_slot, it is
27469 sufficient to insert 'can_issue_more - 1' nops if next_insn is not
27470 a branch. If next_insn is a branch, we insert 'can_issue_more' nops;
27471 in this case the last nop will start a new group and the branch
27472 will be forced to the new group. */
27473 if (can_issue_more && !is_branch_slot_insn (next_insn))
27474 can_issue_more--;
27476 /* Do we have a special group ending nop? */
27477 if (rs6000_cpu_attr == CPU_POWER6 || rs6000_cpu_attr == CPU_POWER7
27478 || rs6000_cpu_attr == CPU_POWER8)
27480 nop = gen_group_ending_nop ();
27481 emit_insn_before (nop, next_insn);
27482 can_issue_more = 0;
27484 else
27485 while (can_issue_more > 0)
27487 nop = gen_nop ();
27488 emit_insn_before (nop, next_insn);
27489 can_issue_more--;
27492 *group_end = true;
27493 return 0;
27496 if (rs6000_sched_insert_nops < sched_finish_regroup_exact)
27498 int n_nops = rs6000_sched_insert_nops;
27500 /* Nops can't be issued from the branch slot, so the effective
27501 issue_rate for nops is 'issue_rate - 1'. */
27502 if (can_issue_more == 0)
27503 can_issue_more = issue_rate;
27504 can_issue_more--;
27505 if (can_issue_more == 0)
27507 can_issue_more = issue_rate - 1;
27508 (*group_count)++;
27509 end = true;
27510 for (i = 0; i < issue_rate; i++)
27512 group_insns[i] = 0;
27516 while (n_nops > 0)
27518 nop = gen_nop ();
27519 emit_insn_before (nop, next_insn);
27520 if (can_issue_more == issue_rate - 1) /* new group begins */
27521 end = false;
27522 can_issue_more--;
27523 if (can_issue_more == 0)
27525 can_issue_more = issue_rate - 1;
27526 (*group_count)++;
27527 end = true;
27528 for (i = 0; i < issue_rate; i++)
27530 group_insns[i] = 0;
27533 n_nops--;
27536 /* Scale back relative to 'issue_rate' (instead of 'issue_rate - 1'). */
27537 can_issue_more++;
27539 /* Is next_insn going to start a new group? */
27540 *group_end
27541 = (end
27542 || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
27543 || (can_issue_more <= 2 && is_cracked_insn (next_insn))
27544 || (can_issue_more < issue_rate &&
27545 insn_terminates_group_p (next_insn, previous_group)));
27546 if (*group_end && end)
27547 (*group_count)--;
27549 if (sched_verbose > 6)
27550 fprintf (dump, "done force: group count = %d, can_issue_more = %d\n",
27551 *group_count, can_issue_more);
27552 return can_issue_more;
27555 return can_issue_more;
27558 /* This function tries to synch the dispatch groups that the compiler "sees"
27559 with the dispatch groups that the processor dispatcher is expected to
27560 form in practice. It tries to achieve this synchronization by forcing the
27561 estimated processor grouping on the compiler (as opposed to the function
27562 'pad_goups' which tries to force the scheduler's grouping on the processor).
27564 The function scans the insn sequence between PREV_HEAD_INSN and TAIL and
27565 examines the (estimated) dispatch groups that will be formed by the processor
27566 dispatcher. It marks these group boundaries to reflect the estimated
27567 processor grouping, overriding the grouping that the scheduler had marked.
27568 Depending on the value of the flag '-minsert-sched-nops' this function can
27569 force certain insns into separate groups or force a certain distance between
27570 them by inserting nops, for example, if there exists a "costly dependence"
27571 between the insns.
27573 The function estimates the group boundaries that the processor will form as
27574 follows: It keeps track of how many vacant issue slots are available after
27575 each insn. A subsequent insn will start a new group if one of the following
27576 4 cases applies:
27577 - no more vacant issue slots remain in the current dispatch group.
27578 - only the last issue slot, which is the branch slot, is vacant, but the next
27579 insn is not a branch.
27580 - only the last 2 or less issue slots, including the branch slot, are vacant,
27581 which means that a cracked insn (which occupies two issue slots) can't be
27582 issued in this group.
27583 - less than 'issue_rate' slots are vacant, and the next insn always needs to
27584 start a new group. */
27586 static int
27587 redefine_groups (FILE *dump, int sched_verbose, rtx prev_head_insn, rtx tail)
27589 rtx insn, next_insn;
27590 int issue_rate;
27591 int can_issue_more;
27592 int slot, i;
27593 bool group_end;
27594 int group_count = 0;
27595 rtx *group_insns;
27597 /* Initialize. */
27598 issue_rate = rs6000_issue_rate ();
27599 group_insns = XALLOCAVEC (rtx, issue_rate);
27600 for (i = 0; i < issue_rate; i++)
27602 group_insns[i] = 0;
27604 can_issue_more = issue_rate;
27605 slot = 0;
27606 insn = get_next_active_insn (prev_head_insn, tail);
27607 group_end = false;
27609 while (insn != NULL_RTX)
27611 slot = (issue_rate - can_issue_more);
27612 group_insns[slot] = insn;
27613 can_issue_more =
27614 rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
27615 if (insn_terminates_group_p (insn, current_group))
27616 can_issue_more = 0;
27618 next_insn = get_next_active_insn (insn, tail);
27619 if (next_insn == NULL_RTX)
27620 return group_count + 1;
27622 /* Is next_insn going to start a new group? */
27623 group_end
27624 = (can_issue_more == 0
27625 || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
27626 || (can_issue_more <= 2 && is_cracked_insn (next_insn))
27627 || (can_issue_more < issue_rate &&
27628 insn_terminates_group_p (next_insn, previous_group)));
27630 can_issue_more = force_new_group (sched_verbose, dump, group_insns,
27631 next_insn, &group_end, can_issue_more,
27632 &group_count);
27634 if (group_end)
27636 group_count++;
27637 can_issue_more = 0;
27638 for (i = 0; i < issue_rate; i++)
27640 group_insns[i] = 0;
27644 if (GET_MODE (next_insn) == TImode && can_issue_more)
27645 PUT_MODE (next_insn, VOIDmode);
27646 else if (!can_issue_more && GET_MODE (next_insn) != TImode)
27647 PUT_MODE (next_insn, TImode);
27649 insn = next_insn;
27650 if (can_issue_more == 0)
27651 can_issue_more = issue_rate;
27652 } /* while */
27654 return group_count;
27657 /* Scan the insn sequence between PREV_HEAD_INSN and TAIL and examine the
27658 dispatch group boundaries that the scheduler had marked. Pad with nops
27659 any dispatch groups which have vacant issue slots, in order to force the
27660 scheduler's grouping on the processor dispatcher. The function
27661 returns the number of dispatch groups found. */
27663 static int
27664 pad_groups (FILE *dump, int sched_verbose, rtx prev_head_insn, rtx tail)
27666 rtx insn, next_insn;
27667 rtx nop;
27668 int issue_rate;
27669 int can_issue_more;
27670 int group_end;
27671 int group_count = 0;
27673 /* Initialize issue_rate. */
27674 issue_rate = rs6000_issue_rate ();
27675 can_issue_more = issue_rate;
27677 insn = get_next_active_insn (prev_head_insn, tail);
27678 next_insn = get_next_active_insn (insn, tail);
27680 while (insn != NULL_RTX)
27682 can_issue_more =
27683 rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
27685 group_end = (next_insn == NULL_RTX || GET_MODE (next_insn) == TImode);
27687 if (next_insn == NULL_RTX)
27688 break;
27690 if (group_end)
27692 /* If the scheduler had marked group termination at this location
27693 (between insn and next_insn), and neither insn nor next_insn will
27694 force group termination, pad the group with nops to force group
27695 termination. */
27696 if (can_issue_more
27697 && (rs6000_sched_insert_nops == sched_finish_pad_groups)
27698 && !insn_terminates_group_p (insn, current_group)
27699 && !insn_terminates_group_p (next_insn, previous_group))
27701 if (!is_branch_slot_insn (next_insn))
27702 can_issue_more--;
27704 while (can_issue_more)
27706 nop = gen_nop ();
27707 emit_insn_before (nop, next_insn);
27708 can_issue_more--;
27712 can_issue_more = issue_rate;
27713 group_count++;
27716 insn = next_insn;
27717 next_insn = get_next_active_insn (insn, tail);
27720 return group_count;
27723 /* We're beginning a new block. Initialize data structures as necessary. */
27725 static void
27726 rs6000_sched_init (FILE *dump ATTRIBUTE_UNUSED,
27727 int sched_verbose ATTRIBUTE_UNUSED,
27728 int max_ready ATTRIBUTE_UNUSED)
27730 last_scheduled_insn = NULL_RTX;
27731 load_store_pendulum = 0;
27734 /* The following function is called at the end of scheduling BB.
27735 After reload, it inserts nops at insn group bundling. */
27737 static void
27738 rs6000_sched_finish (FILE *dump, int sched_verbose)
27740 int n_groups;
27742 if (sched_verbose)
27743 fprintf (dump, "=== Finishing schedule.\n");
27745 if (reload_completed && rs6000_sched_groups)
27747 /* Do not run sched_finish hook when selective scheduling enabled. */
27748 if (sel_sched_p ())
27749 return;
27751 if (rs6000_sched_insert_nops == sched_finish_none)
27752 return;
27754 if (rs6000_sched_insert_nops == sched_finish_pad_groups)
27755 n_groups = pad_groups (dump, sched_verbose,
27756 current_sched_info->prev_head,
27757 current_sched_info->next_tail);
27758 else
27759 n_groups = redefine_groups (dump, sched_verbose,
27760 current_sched_info->prev_head,
27761 current_sched_info->next_tail);
27763 if (sched_verbose >= 6)
27765 fprintf (dump, "ngroups = %d\n", n_groups);
27766 print_rtl (dump, current_sched_info->prev_head);
27767 fprintf (dump, "Done finish_sched\n");
27772 struct _rs6000_sched_context
27774 short cached_can_issue_more;
27775 rtx last_scheduled_insn;
27776 int load_store_pendulum;
27779 typedef struct _rs6000_sched_context rs6000_sched_context_def;
27780 typedef rs6000_sched_context_def *rs6000_sched_context_t;
27782 /* Allocate store for new scheduling context. */
27783 static void *
27784 rs6000_alloc_sched_context (void)
27786 return xmalloc (sizeof (rs6000_sched_context_def));
27789 /* If CLEAN_P is true then initializes _SC with clean data,
27790 and from the global context otherwise. */
27791 static void
27792 rs6000_init_sched_context (void *_sc, bool clean_p)
27794 rs6000_sched_context_t sc = (rs6000_sched_context_t) _sc;
27796 if (clean_p)
27798 sc->cached_can_issue_more = 0;
27799 sc->last_scheduled_insn = NULL_RTX;
27800 sc->load_store_pendulum = 0;
27802 else
27804 sc->cached_can_issue_more = cached_can_issue_more;
27805 sc->last_scheduled_insn = last_scheduled_insn;
27806 sc->load_store_pendulum = load_store_pendulum;
27810 /* Sets the global scheduling context to the one pointed to by _SC. */
27811 static void
27812 rs6000_set_sched_context (void *_sc)
27814 rs6000_sched_context_t sc = (rs6000_sched_context_t) _sc;
27816 gcc_assert (sc != NULL);
27818 cached_can_issue_more = sc->cached_can_issue_more;
27819 last_scheduled_insn = sc->last_scheduled_insn;
27820 load_store_pendulum = sc->load_store_pendulum;
27823 /* Free _SC. */
27824 static void
27825 rs6000_free_sched_context (void *_sc)
27827 gcc_assert (_sc != NULL);
27829 free (_sc);
27833 /* Length in units of the trampoline for entering a nested function. */
27836 rs6000_trampoline_size (void)
27838 int ret = 0;
27840 switch (DEFAULT_ABI)
27842 default:
27843 gcc_unreachable ();
27845 case ABI_AIX:
27846 ret = (TARGET_32BIT) ? 12 : 24;
27847 break;
27849 case ABI_ELFv2:
27850 gcc_assert (!TARGET_32BIT);
27851 ret = 32;
27852 break;
27854 case ABI_DARWIN:
27855 case ABI_V4:
27856 ret = (TARGET_32BIT) ? 40 : 48;
27857 break;
27860 return ret;
27863 /* Emit RTL insns to initialize the variable parts of a trampoline.
27864 FNADDR is an RTX for the address of the function's pure code.
27865 CXT is an RTX for the static chain value for the function. */
27867 static void
27868 rs6000_trampoline_init (rtx m_tramp, tree fndecl, rtx cxt)
27870 int regsize = (TARGET_32BIT) ? 4 : 8;
27871 rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
27872 rtx ctx_reg = force_reg (Pmode, cxt);
27873 rtx addr = force_reg (Pmode, XEXP (m_tramp, 0));
27875 switch (DEFAULT_ABI)
27877 default:
27878 gcc_unreachable ();
27880 /* Under AIX, just build the 3 word function descriptor */
27881 case ABI_AIX:
27883 rtx fnmem, fn_reg, toc_reg;
27885 if (!TARGET_POINTERS_TO_NESTED_FUNCTIONS)
27886 error ("You cannot take the address of a nested function if you use "
27887 "the -mno-pointers-to-nested-functions option.");
27889 fnmem = gen_const_mem (Pmode, force_reg (Pmode, fnaddr));
27890 fn_reg = gen_reg_rtx (Pmode);
27891 toc_reg = gen_reg_rtx (Pmode);
27893 /* Macro to shorten the code expansions below. */
27894 # define MEM_PLUS(MEM, OFFSET) adjust_address (MEM, Pmode, OFFSET)
27896 m_tramp = replace_equiv_address (m_tramp, addr);
27898 emit_move_insn (fn_reg, MEM_PLUS (fnmem, 0));
27899 emit_move_insn (toc_reg, MEM_PLUS (fnmem, regsize));
27900 emit_move_insn (MEM_PLUS (m_tramp, 0), fn_reg);
27901 emit_move_insn (MEM_PLUS (m_tramp, regsize), toc_reg);
27902 emit_move_insn (MEM_PLUS (m_tramp, 2*regsize), ctx_reg);
27904 # undef MEM_PLUS
27906 break;
27908 /* Under V.4/eabi/darwin, __trampoline_setup does the real work. */
27909 case ABI_ELFv2:
27910 case ABI_DARWIN:
27911 case ABI_V4:
27912 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__trampoline_setup"),
27913 LCT_NORMAL, VOIDmode, 4,
27914 addr, Pmode,
27915 GEN_INT (rs6000_trampoline_size ()), SImode,
27916 fnaddr, Pmode,
27917 ctx_reg, Pmode);
27918 break;
27923 /* Returns TRUE iff the target attribute indicated by ATTR_ID takes a plain
27924 identifier as an argument, so the front end shouldn't look it up. */
27926 static bool
27927 rs6000_attribute_takes_identifier_p (const_tree attr_id)
27929 return is_attribute_p ("altivec", attr_id);
27932 /* Handle the "altivec" attribute. The attribute may have
27933 arguments as follows:
27935 __attribute__((altivec(vector__)))
27936 __attribute__((altivec(pixel__))) (always followed by 'unsigned short')
27937 __attribute__((altivec(bool__))) (always followed by 'unsigned')
27939 and may appear more than once (e.g., 'vector bool char') in a
27940 given declaration. */
27942 static tree
27943 rs6000_handle_altivec_attribute (tree *node,
27944 tree name ATTRIBUTE_UNUSED,
27945 tree args,
27946 int flags ATTRIBUTE_UNUSED,
27947 bool *no_add_attrs)
27949 tree type = *node, result = NULL_TREE;
27950 enum machine_mode mode;
27951 int unsigned_p;
27952 char altivec_type
27953 = ((args && TREE_CODE (args) == TREE_LIST && TREE_VALUE (args)
27954 && TREE_CODE (TREE_VALUE (args)) == IDENTIFIER_NODE)
27955 ? *IDENTIFIER_POINTER (TREE_VALUE (args))
27956 : '?');
27958 while (POINTER_TYPE_P (type)
27959 || TREE_CODE (type) == FUNCTION_TYPE
27960 || TREE_CODE (type) == METHOD_TYPE
27961 || TREE_CODE (type) == ARRAY_TYPE)
27962 type = TREE_TYPE (type);
27964 mode = TYPE_MODE (type);
27966 /* Check for invalid AltiVec type qualifiers. */
27967 if (type == long_double_type_node)
27968 error ("use of %<long double%> in AltiVec types is invalid");
27969 else if (type == boolean_type_node)
27970 error ("use of boolean types in AltiVec types is invalid");
27971 else if (TREE_CODE (type) == COMPLEX_TYPE)
27972 error ("use of %<complex%> in AltiVec types is invalid");
27973 else if (DECIMAL_FLOAT_MODE_P (mode))
27974 error ("use of decimal floating point types in AltiVec types is invalid");
27975 else if (!TARGET_VSX)
27977 if (type == long_unsigned_type_node || type == long_integer_type_node)
27979 if (TARGET_64BIT)
27980 error ("use of %<long%> in AltiVec types is invalid for "
27981 "64-bit code without -mvsx");
27982 else if (rs6000_warn_altivec_long)
27983 warning (0, "use of %<long%> in AltiVec types is deprecated; "
27984 "use %<int%>");
27986 else if (type == long_long_unsigned_type_node
27987 || type == long_long_integer_type_node)
27988 error ("use of %<long long%> in AltiVec types is invalid without "
27989 "-mvsx");
27990 else if (type == double_type_node)
27991 error ("use of %<double%> in AltiVec types is invalid without -mvsx");
27994 switch (altivec_type)
27996 case 'v':
27997 unsigned_p = TYPE_UNSIGNED (type);
27998 switch (mode)
28000 case TImode:
28001 result = (unsigned_p ? unsigned_V1TI_type_node : V1TI_type_node);
28002 break;
28003 case DImode:
28004 result = (unsigned_p ? unsigned_V2DI_type_node : V2DI_type_node);
28005 break;
28006 case SImode:
28007 result = (unsigned_p ? unsigned_V4SI_type_node : V4SI_type_node);
28008 break;
28009 case HImode:
28010 result = (unsigned_p ? unsigned_V8HI_type_node : V8HI_type_node);
28011 break;
28012 case QImode:
28013 result = (unsigned_p ? unsigned_V16QI_type_node : V16QI_type_node);
28014 break;
28015 case SFmode: result = V4SF_type_node; break;
28016 case DFmode: result = V2DF_type_node; break;
28017 /* If the user says 'vector int bool', we may be handed the 'bool'
28018 attribute _before_ the 'vector' attribute, and so select the
28019 proper type in the 'b' case below. */
28020 case V4SImode: case V8HImode: case V16QImode: case V4SFmode:
28021 case V2DImode: case V2DFmode:
28022 result = type;
28023 default: break;
28025 break;
28026 case 'b':
28027 switch (mode)
28029 case DImode: case V2DImode: result = bool_V2DI_type_node; break;
28030 case SImode: case V4SImode: result = bool_V4SI_type_node; break;
28031 case HImode: case V8HImode: result = bool_V8HI_type_node; break;
28032 case QImode: case V16QImode: result = bool_V16QI_type_node;
28033 default: break;
28035 break;
28036 case 'p':
28037 switch (mode)
28039 case V8HImode: result = pixel_V8HI_type_node;
28040 default: break;
28042 default: break;
28045 /* Propagate qualifiers attached to the element type
28046 onto the vector type. */
28047 if (result && result != type && TYPE_QUALS (type))
28048 result = build_qualified_type (result, TYPE_QUALS (type));
28050 *no_add_attrs = true; /* No need to hang on to the attribute. */
28052 if (result)
28053 *node = lang_hooks.types.reconstruct_complex_type (*node, result);
28055 return NULL_TREE;
28058 /* AltiVec defines four built-in scalar types that serve as vector
28059 elements; we must teach the compiler how to mangle them. */
28061 static const char *
28062 rs6000_mangle_type (const_tree type)
28064 type = TYPE_MAIN_VARIANT (type);
28066 if (TREE_CODE (type) != VOID_TYPE && TREE_CODE (type) != BOOLEAN_TYPE
28067 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
28068 return NULL;
28070 if (type == bool_char_type_node) return "U6__boolc";
28071 if (type == bool_short_type_node) return "U6__bools";
28072 if (type == pixel_type_node) return "u7__pixel";
28073 if (type == bool_int_type_node) return "U6__booli";
28074 if (type == bool_long_type_node) return "U6__booll";
28076 /* Mangle IBM extended float long double as `g' (__float128) on
28077 powerpc*-linux where long-double-64 previously was the default. */
28078 if (TYPE_MAIN_VARIANT (type) == long_double_type_node
28079 && TARGET_ELF
28080 && TARGET_LONG_DOUBLE_128
28081 && !TARGET_IEEEQUAD)
28082 return "g";
28084 /* For all other types, use normal C++ mangling. */
28085 return NULL;
28088 /* Handle a "longcall" or "shortcall" attribute; arguments as in
28089 struct attribute_spec.handler. */
28091 static tree
28092 rs6000_handle_longcall_attribute (tree *node, tree name,
28093 tree args ATTRIBUTE_UNUSED,
28094 int flags ATTRIBUTE_UNUSED,
28095 bool *no_add_attrs)
28097 if (TREE_CODE (*node) != FUNCTION_TYPE
28098 && TREE_CODE (*node) != FIELD_DECL
28099 && TREE_CODE (*node) != TYPE_DECL)
28101 warning (OPT_Wattributes, "%qE attribute only applies to functions",
28102 name);
28103 *no_add_attrs = true;
28106 return NULL_TREE;
28109 /* Set longcall attributes on all functions declared when
28110 rs6000_default_long_calls is true. */
28111 static void
28112 rs6000_set_default_type_attributes (tree type)
28114 if (rs6000_default_long_calls
28115 && (TREE_CODE (type) == FUNCTION_TYPE
28116 || TREE_CODE (type) == METHOD_TYPE))
28117 TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("longcall"),
28118 NULL_TREE,
28119 TYPE_ATTRIBUTES (type));
28121 #if TARGET_MACHO
28122 darwin_set_default_type_attributes (type);
28123 #endif
28126 /* Return a reference suitable for calling a function with the
28127 longcall attribute. */
28130 rs6000_longcall_ref (rtx call_ref)
28132 const char *call_name;
28133 tree node;
28135 if (GET_CODE (call_ref) != SYMBOL_REF)
28136 return call_ref;
28138 /* System V adds '.' to the internal name, so skip them. */
28139 call_name = XSTR (call_ref, 0);
28140 if (*call_name == '.')
28142 while (*call_name == '.')
28143 call_name++;
28145 node = get_identifier (call_name);
28146 call_ref = gen_rtx_SYMBOL_REF (VOIDmode, IDENTIFIER_POINTER (node));
28149 return force_reg (Pmode, call_ref);
28152 #ifndef TARGET_USE_MS_BITFIELD_LAYOUT
28153 #define TARGET_USE_MS_BITFIELD_LAYOUT 0
28154 #endif
28156 /* Handle a "ms_struct" or "gcc_struct" attribute; arguments as in
28157 struct attribute_spec.handler. */
28158 static tree
28159 rs6000_handle_struct_attribute (tree *node, tree name,
28160 tree args ATTRIBUTE_UNUSED,
28161 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
28163 tree *type = NULL;
28164 if (DECL_P (*node))
28166 if (TREE_CODE (*node) == TYPE_DECL)
28167 type = &TREE_TYPE (*node);
28169 else
28170 type = node;
28172 if (!(type && (TREE_CODE (*type) == RECORD_TYPE
28173 || TREE_CODE (*type) == UNION_TYPE)))
28175 warning (OPT_Wattributes, "%qE attribute ignored", name);
28176 *no_add_attrs = true;
28179 else if ((is_attribute_p ("ms_struct", name)
28180 && lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (*type)))
28181 || ((is_attribute_p ("gcc_struct", name)
28182 && lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (*type)))))
28184 warning (OPT_Wattributes, "%qE incompatible attribute ignored",
28185 name);
28186 *no_add_attrs = true;
28189 return NULL_TREE;
28192 static bool
28193 rs6000_ms_bitfield_layout_p (const_tree record_type)
28195 return (TARGET_USE_MS_BITFIELD_LAYOUT &&
28196 !lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (record_type)))
28197 || lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (record_type));
28200 #ifdef USING_ELFOS_H
28202 /* A get_unnamed_section callback, used for switching to toc_section. */
28204 static void
28205 rs6000_elf_output_toc_section_asm_op (const void *data ATTRIBUTE_UNUSED)
28207 if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
28208 && TARGET_MINIMAL_TOC
28209 && !TARGET_RELOCATABLE)
28211 if (!toc_initialized)
28213 toc_initialized = 1;
28214 fprintf (asm_out_file, "%s\n", TOC_SECTION_ASM_OP);
28215 (*targetm.asm_out.internal_label) (asm_out_file, "LCTOC", 0);
28216 fprintf (asm_out_file, "\t.tc ");
28217 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1[TC],");
28218 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
28219 fprintf (asm_out_file, "\n");
28221 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
28222 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
28223 fprintf (asm_out_file, " = .+32768\n");
28225 else
28226 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
28228 else if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
28229 && !TARGET_RELOCATABLE)
28230 fprintf (asm_out_file, "%s\n", TOC_SECTION_ASM_OP);
28231 else
28233 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
28234 if (!toc_initialized)
28236 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
28237 fprintf (asm_out_file, " = .+32768\n");
28238 toc_initialized = 1;
28243 /* Implement TARGET_ASM_INIT_SECTIONS. */
28245 static void
28246 rs6000_elf_asm_init_sections (void)
28248 toc_section
28249 = get_unnamed_section (0, rs6000_elf_output_toc_section_asm_op, NULL);
28251 sdata2_section
28252 = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
28253 SDATA2_SECTION_ASM_OP);
28256 /* Implement TARGET_SELECT_RTX_SECTION. */
28258 static section *
28259 rs6000_elf_select_rtx_section (enum machine_mode mode, rtx x,
28260 unsigned HOST_WIDE_INT align)
28262 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
28263 return toc_section;
28264 else
28265 return default_elf_select_rtx_section (mode, x, align);
28268 /* For a SYMBOL_REF, set generic flags and then perform some
28269 target-specific processing.
28271 When the AIX ABI is requested on a non-AIX system, replace the
28272 function name with the real name (with a leading .) rather than the
28273 function descriptor name. This saves a lot of overriding code to
28274 read the prefixes. */
28276 static void rs6000_elf_encode_section_info (tree, rtx, int) ATTRIBUTE_UNUSED;
28277 static void
28278 rs6000_elf_encode_section_info (tree decl, rtx rtl, int first)
28280 default_encode_section_info (decl, rtl, first);
28282 if (first
28283 && TREE_CODE (decl) == FUNCTION_DECL
28284 && !TARGET_AIX
28285 && DEFAULT_ABI == ABI_AIX)
28287 rtx sym_ref = XEXP (rtl, 0);
28288 size_t len = strlen (XSTR (sym_ref, 0));
28289 char *str = XALLOCAVEC (char, len + 2);
28290 str[0] = '.';
28291 memcpy (str + 1, XSTR (sym_ref, 0), len + 1);
28292 XSTR (sym_ref, 0) = ggc_alloc_string (str, len + 1);
28296 static inline bool
28297 compare_section_name (const char *section, const char *templ)
28299 int len;
28301 len = strlen (templ);
28302 return (strncmp (section, templ, len) == 0
28303 && (section[len] == 0 || section[len] == '.'));
28306 bool
28307 rs6000_elf_in_small_data_p (const_tree decl)
28309 if (rs6000_sdata == SDATA_NONE)
28310 return false;
28312 /* We want to merge strings, so we never consider them small data. */
28313 if (TREE_CODE (decl) == STRING_CST)
28314 return false;
28316 /* Functions are never in the small data area. */
28317 if (TREE_CODE (decl) == FUNCTION_DECL)
28318 return false;
28320 if (TREE_CODE (decl) == VAR_DECL && DECL_SECTION_NAME (decl))
28322 const char *section = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
28323 if (compare_section_name (section, ".sdata")
28324 || compare_section_name (section, ".sdata2")
28325 || compare_section_name (section, ".gnu.linkonce.s")
28326 || compare_section_name (section, ".sbss")
28327 || compare_section_name (section, ".sbss2")
28328 || compare_section_name (section, ".gnu.linkonce.sb")
28329 || strcmp (section, ".PPC.EMB.sdata0") == 0
28330 || strcmp (section, ".PPC.EMB.sbss0") == 0)
28331 return true;
28333 else
28335 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (decl));
28337 if (size > 0
28338 && size <= g_switch_value
28339 /* If it's not public, and we're not going to reference it there,
28340 there's no need to put it in the small data section. */
28341 && (rs6000_sdata != SDATA_DATA || TREE_PUBLIC (decl)))
28342 return true;
28345 return false;
28348 #endif /* USING_ELFOS_H */
28350 /* Implement TARGET_USE_BLOCKS_FOR_CONSTANT_P. */
28352 static bool
28353 rs6000_use_blocks_for_constant_p (enum machine_mode mode, const_rtx x)
28355 return !ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode);
28358 /* Do not place thread-local symbols refs in the object blocks. */
28360 static bool
28361 rs6000_use_blocks_for_decl_p (const_tree decl)
28363 return !DECL_THREAD_LOCAL_P (decl);
28366 /* Return a REG that occurs in ADDR with coefficient 1.
28367 ADDR can be effectively incremented by incrementing REG.
28369 r0 is special and we must not select it as an address
28370 register by this routine since our caller will try to
28371 increment the returned register via an "la" instruction. */
28374 find_addr_reg (rtx addr)
28376 while (GET_CODE (addr) == PLUS)
28378 if (GET_CODE (XEXP (addr, 0)) == REG
28379 && REGNO (XEXP (addr, 0)) != 0)
28380 addr = XEXP (addr, 0);
28381 else if (GET_CODE (XEXP (addr, 1)) == REG
28382 && REGNO (XEXP (addr, 1)) != 0)
28383 addr = XEXP (addr, 1);
28384 else if (CONSTANT_P (XEXP (addr, 0)))
28385 addr = XEXP (addr, 1);
28386 else if (CONSTANT_P (XEXP (addr, 1)))
28387 addr = XEXP (addr, 0);
28388 else
28389 gcc_unreachable ();
28391 gcc_assert (GET_CODE (addr) == REG && REGNO (addr) != 0);
28392 return addr;
28395 void
28396 rs6000_fatal_bad_address (rtx op)
28398 fatal_insn ("bad address", op);
28401 #if TARGET_MACHO
28403 typedef struct branch_island_d {
28404 tree function_name;
28405 tree label_name;
28406 int line_number;
28407 } branch_island;
28410 static vec<branch_island, va_gc> *branch_islands;
28412 /* Remember to generate a branch island for far calls to the given
28413 function. */
28415 static void
28416 add_compiler_branch_island (tree label_name, tree function_name,
28417 int line_number)
28419 branch_island bi = {function_name, label_name, line_number};
28420 vec_safe_push (branch_islands, bi);
28423 /* Generate far-jump branch islands for everything recorded in
28424 branch_islands. Invoked immediately after the last instruction of
28425 the epilogue has been emitted; the branch islands must be appended
28426 to, and contiguous with, the function body. Mach-O stubs are
28427 generated in machopic_output_stub(). */
28429 static void
28430 macho_branch_islands (void)
28432 char tmp_buf[512];
28434 while (!vec_safe_is_empty (branch_islands))
28436 branch_island *bi = &branch_islands->last ();
28437 const char *label = IDENTIFIER_POINTER (bi->label_name);
28438 const char *name = IDENTIFIER_POINTER (bi->function_name);
28439 char name_buf[512];
28440 /* Cheap copy of the details from the Darwin ASM_OUTPUT_LABELREF(). */
28441 if (name[0] == '*' || name[0] == '&')
28442 strcpy (name_buf, name+1);
28443 else
28445 name_buf[0] = '_';
28446 strcpy (name_buf+1, name);
28448 strcpy (tmp_buf, "\n");
28449 strcat (tmp_buf, label);
28450 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
28451 if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
28452 dbxout_stabd (N_SLINE, bi->line_number);
28453 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
28454 if (flag_pic)
28456 if (TARGET_LINK_STACK)
28458 char name[32];
28459 get_ppc476_thunk_name (name);
28460 strcat (tmp_buf, ":\n\tmflr r0\n\tbl ");
28461 strcat (tmp_buf, name);
28462 strcat (tmp_buf, "\n");
28463 strcat (tmp_buf, label);
28464 strcat (tmp_buf, "_pic:\n\tmflr r11\n");
28466 else
28468 strcat (tmp_buf, ":\n\tmflr r0\n\tbcl 20,31,");
28469 strcat (tmp_buf, label);
28470 strcat (tmp_buf, "_pic\n");
28471 strcat (tmp_buf, label);
28472 strcat (tmp_buf, "_pic:\n\tmflr r11\n");
28475 strcat (tmp_buf, "\taddis r11,r11,ha16(");
28476 strcat (tmp_buf, name_buf);
28477 strcat (tmp_buf, " - ");
28478 strcat (tmp_buf, label);
28479 strcat (tmp_buf, "_pic)\n");
28481 strcat (tmp_buf, "\tmtlr r0\n");
28483 strcat (tmp_buf, "\taddi r12,r11,lo16(");
28484 strcat (tmp_buf, name_buf);
28485 strcat (tmp_buf, " - ");
28486 strcat (tmp_buf, label);
28487 strcat (tmp_buf, "_pic)\n");
28489 strcat (tmp_buf, "\tmtctr r12\n\tbctr\n");
28491 else
28493 strcat (tmp_buf, ":\nlis r12,hi16(");
28494 strcat (tmp_buf, name_buf);
28495 strcat (tmp_buf, ")\n\tori r12,r12,lo16(");
28496 strcat (tmp_buf, name_buf);
28497 strcat (tmp_buf, ")\n\tmtctr r12\n\tbctr");
28499 output_asm_insn (tmp_buf, 0);
28500 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
28501 if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
28502 dbxout_stabd (N_SLINE, bi->line_number);
28503 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
28504 branch_islands->pop ();
28508 /* NO_PREVIOUS_DEF checks in the link list whether the function name is
28509 already there or not. */
28511 static int
28512 no_previous_def (tree function_name)
28514 branch_island *bi;
28515 unsigned ix;
28517 FOR_EACH_VEC_SAFE_ELT (branch_islands, ix, bi)
28518 if (function_name == bi->function_name)
28519 return 0;
28520 return 1;
28523 /* GET_PREV_LABEL gets the label name from the previous definition of
28524 the function. */
28526 static tree
28527 get_prev_label (tree function_name)
28529 branch_island *bi;
28530 unsigned ix;
28532 FOR_EACH_VEC_SAFE_ELT (branch_islands, ix, bi)
28533 if (function_name == bi->function_name)
28534 return bi->label_name;
28535 return NULL_TREE;
28538 /* INSN is either a function call or a millicode call. It may have an
28539 unconditional jump in its delay slot.
28541 CALL_DEST is the routine we are calling. */
28543 char *
28544 output_call (rtx insn, rtx *operands, int dest_operand_number,
28545 int cookie_operand_number)
28547 static char buf[256];
28548 if (darwin_emit_branch_islands
28549 && GET_CODE (operands[dest_operand_number]) == SYMBOL_REF
28550 && (INTVAL (operands[cookie_operand_number]) & CALL_LONG))
28552 tree labelname;
28553 tree funname = get_identifier (XSTR (operands[dest_operand_number], 0));
28555 if (no_previous_def (funname))
28557 rtx label_rtx = gen_label_rtx ();
28558 char *label_buf, temp_buf[256];
28559 ASM_GENERATE_INTERNAL_LABEL (temp_buf, "L",
28560 CODE_LABEL_NUMBER (label_rtx));
28561 label_buf = temp_buf[0] == '*' ? temp_buf + 1 : temp_buf;
28562 labelname = get_identifier (label_buf);
28563 add_compiler_branch_island (labelname, funname, insn_line (insn));
28565 else
28566 labelname = get_prev_label (funname);
28568 /* "jbsr foo, L42" is Mach-O for "Link as 'bl foo' if a 'bl'
28569 instruction will reach 'foo', otherwise link as 'bl L42'".
28570 "L42" should be a 'branch island', that will do a far jump to
28571 'foo'. Branch islands are generated in
28572 macho_branch_islands(). */
28573 sprintf (buf, "jbsr %%z%d,%.246s",
28574 dest_operand_number, IDENTIFIER_POINTER (labelname));
28576 else
28577 sprintf (buf, "bl %%z%d", dest_operand_number);
28578 return buf;
28581 /* Generate PIC and indirect symbol stubs. */
28583 void
28584 machopic_output_stub (FILE *file, const char *symb, const char *stub)
28586 unsigned int length;
28587 char *symbol_name, *lazy_ptr_name;
28588 char *local_label_0;
28589 static int label = 0;
28591 /* Lose our funky encoding stuff so it doesn't contaminate the stub. */
28592 symb = (*targetm.strip_name_encoding) (symb);
28595 length = strlen (symb);
28596 symbol_name = XALLOCAVEC (char, length + 32);
28597 GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
28599 lazy_ptr_name = XALLOCAVEC (char, length + 32);
28600 GEN_LAZY_PTR_NAME_FOR_SYMBOL (lazy_ptr_name, symb, length);
28602 if (flag_pic == 2)
28603 switch_to_section (darwin_sections[machopic_picsymbol_stub1_section]);
28604 else
28605 switch_to_section (darwin_sections[machopic_symbol_stub1_section]);
28607 if (flag_pic == 2)
28609 fprintf (file, "\t.align 5\n");
28611 fprintf (file, "%s:\n", stub);
28612 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
28614 label++;
28615 local_label_0 = XALLOCAVEC (char, sizeof ("\"L00000000000$spb\""));
28616 sprintf (local_label_0, "\"L%011d$spb\"", label);
28618 fprintf (file, "\tmflr r0\n");
28619 if (TARGET_LINK_STACK)
28621 char name[32];
28622 get_ppc476_thunk_name (name);
28623 fprintf (file, "\tbl %s\n", name);
28624 fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
28626 else
28628 fprintf (file, "\tbcl 20,31,%s\n", local_label_0);
28629 fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
28631 fprintf (file, "\taddis r11,r11,ha16(%s-%s)\n",
28632 lazy_ptr_name, local_label_0);
28633 fprintf (file, "\tmtlr r0\n");
28634 fprintf (file, "\t%s r12,lo16(%s-%s)(r11)\n",
28635 (TARGET_64BIT ? "ldu" : "lwzu"),
28636 lazy_ptr_name, local_label_0);
28637 fprintf (file, "\tmtctr r12\n");
28638 fprintf (file, "\tbctr\n");
28640 else
28642 fprintf (file, "\t.align 4\n");
28644 fprintf (file, "%s:\n", stub);
28645 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
28647 fprintf (file, "\tlis r11,ha16(%s)\n", lazy_ptr_name);
28648 fprintf (file, "\t%s r12,lo16(%s)(r11)\n",
28649 (TARGET_64BIT ? "ldu" : "lwzu"),
28650 lazy_ptr_name);
28651 fprintf (file, "\tmtctr r12\n");
28652 fprintf (file, "\tbctr\n");
28655 switch_to_section (darwin_sections[machopic_lazy_symbol_ptr_section]);
28656 fprintf (file, "%s:\n", lazy_ptr_name);
28657 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
28658 fprintf (file, "%sdyld_stub_binding_helper\n",
28659 (TARGET_64BIT ? DOUBLE_INT_ASM_OP : "\t.long\t"));
28662 /* Legitimize PIC addresses. If the address is already
28663 position-independent, we return ORIG. Newly generated
28664 position-independent addresses go into a reg. This is REG if non
28665 zero, otherwise we allocate register(s) as necessary. */
28667 #define SMALL_INT(X) ((UINTVAL (X) + 0x8000) < 0x10000)
28670 rs6000_machopic_legitimize_pic_address (rtx orig, enum machine_mode mode,
28671 rtx reg)
28673 rtx base, offset;
28675 if (reg == NULL && ! reload_in_progress && ! reload_completed)
28676 reg = gen_reg_rtx (Pmode);
28678 if (GET_CODE (orig) == CONST)
28680 rtx reg_temp;
28682 if (GET_CODE (XEXP (orig, 0)) == PLUS
28683 && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
28684 return orig;
28686 gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
28688 /* Use a different reg for the intermediate value, as
28689 it will be marked UNCHANGING. */
28690 reg_temp = !can_create_pseudo_p () ? reg : gen_reg_rtx (Pmode);
28691 base = rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 0),
28692 Pmode, reg_temp);
28693 offset =
28694 rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 1),
28695 Pmode, reg);
28697 if (GET_CODE (offset) == CONST_INT)
28699 if (SMALL_INT (offset))
28700 return plus_constant (Pmode, base, INTVAL (offset));
28701 else if (! reload_in_progress && ! reload_completed)
28702 offset = force_reg (Pmode, offset);
28703 else
28705 rtx mem = force_const_mem (Pmode, orig);
28706 return machopic_legitimize_pic_address (mem, Pmode, reg);
28709 return gen_rtx_PLUS (Pmode, base, offset);
28712 /* Fall back on generic machopic code. */
28713 return machopic_legitimize_pic_address (orig, mode, reg);
28716 /* Output a .machine directive for the Darwin assembler, and call
28717 the generic start_file routine. */
28719 static void
28720 rs6000_darwin_file_start (void)
28722 static const struct
28724 const char *arg;
28725 const char *name;
28726 HOST_WIDE_INT if_set;
28727 } mapping[] = {
28728 { "ppc64", "ppc64", MASK_64BIT },
28729 { "970", "ppc970", MASK_PPC_GPOPT | MASK_MFCRF | MASK_POWERPC64 },
28730 { "power4", "ppc970", 0 },
28731 { "G5", "ppc970", 0 },
28732 { "7450", "ppc7450", 0 },
28733 { "7400", "ppc7400", MASK_ALTIVEC },
28734 { "G4", "ppc7400", 0 },
28735 { "750", "ppc750", 0 },
28736 { "740", "ppc750", 0 },
28737 { "G3", "ppc750", 0 },
28738 { "604e", "ppc604e", 0 },
28739 { "604", "ppc604", 0 },
28740 { "603e", "ppc603", 0 },
28741 { "603", "ppc603", 0 },
28742 { "601", "ppc601", 0 },
28743 { NULL, "ppc", 0 } };
28744 const char *cpu_id = "";
28745 size_t i;
28747 rs6000_file_start ();
28748 darwin_file_start ();
28750 /* Determine the argument to -mcpu=. Default to G3 if not specified. */
28752 if (rs6000_default_cpu != 0 && rs6000_default_cpu[0] != '\0')
28753 cpu_id = rs6000_default_cpu;
28755 if (global_options_set.x_rs6000_cpu_index)
28756 cpu_id = processor_target_table[rs6000_cpu_index].name;
28758 /* Look through the mapping array. Pick the first name that either
28759 matches the argument, has a bit set in IF_SET that is also set
28760 in the target flags, or has a NULL name. */
28762 i = 0;
28763 while (mapping[i].arg != NULL
28764 && strcmp (mapping[i].arg, cpu_id) != 0
28765 && (mapping[i].if_set & rs6000_isa_flags) == 0)
28766 i++;
28768 fprintf (asm_out_file, "\t.machine %s\n", mapping[i].name);
28771 #endif /* TARGET_MACHO */
28773 #if TARGET_ELF
28774 static int
28775 rs6000_elf_reloc_rw_mask (void)
28777 if (flag_pic)
28778 return 3;
28779 else if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
28780 return 2;
28781 else
28782 return 0;
28785 /* Record an element in the table of global constructors. SYMBOL is
28786 a SYMBOL_REF of the function to be called; PRIORITY is a number
28787 between 0 and MAX_INIT_PRIORITY.
28789 This differs from default_named_section_asm_out_constructor in
28790 that we have special handling for -mrelocatable. */
28792 static void rs6000_elf_asm_out_constructor (rtx, int) ATTRIBUTE_UNUSED;
28793 static void
28794 rs6000_elf_asm_out_constructor (rtx symbol, int priority)
28796 const char *section = ".ctors";
28797 char buf[16];
28799 if (priority != DEFAULT_INIT_PRIORITY)
28801 sprintf (buf, ".ctors.%.5u",
28802 /* Invert the numbering so the linker puts us in the proper
28803 order; constructors are run from right to left, and the
28804 linker sorts in increasing order. */
28805 MAX_INIT_PRIORITY - priority);
28806 section = buf;
28809 switch_to_section (get_section (section, SECTION_WRITE, NULL));
28810 assemble_align (POINTER_SIZE);
28812 if (TARGET_RELOCATABLE)
28814 fputs ("\t.long (", asm_out_file);
28815 output_addr_const (asm_out_file, symbol);
28816 fputs (")@fixup\n", asm_out_file);
28818 else
28819 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
28822 static void rs6000_elf_asm_out_destructor (rtx, int) ATTRIBUTE_UNUSED;
28823 static void
28824 rs6000_elf_asm_out_destructor (rtx symbol, int priority)
28826 const char *section = ".dtors";
28827 char buf[16];
28829 if (priority != DEFAULT_INIT_PRIORITY)
28831 sprintf (buf, ".dtors.%.5u",
28832 /* Invert the numbering so the linker puts us in the proper
28833 order; constructors are run from right to left, and the
28834 linker sorts in increasing order. */
28835 MAX_INIT_PRIORITY - priority);
28836 section = buf;
28839 switch_to_section (get_section (section, SECTION_WRITE, NULL));
28840 assemble_align (POINTER_SIZE);
28842 if (TARGET_RELOCATABLE)
28844 fputs ("\t.long (", asm_out_file);
28845 output_addr_const (asm_out_file, symbol);
28846 fputs (")@fixup\n", asm_out_file);
28848 else
28849 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
28852 void
28853 rs6000_elf_declare_function_name (FILE *file, const char *name, tree decl)
28855 if (TARGET_64BIT && DEFAULT_ABI != ABI_ELFv2)
28857 fputs ("\t.section\t\".opd\",\"aw\"\n\t.align 3\n", file);
28858 ASM_OUTPUT_LABEL (file, name);
28859 fputs (DOUBLE_INT_ASM_OP, file);
28860 rs6000_output_function_entry (file, name);
28861 fputs (",.TOC.@tocbase,0\n\t.previous\n", file);
28862 if (DOT_SYMBOLS)
28864 fputs ("\t.size\t", file);
28865 assemble_name (file, name);
28866 fputs (",24\n\t.type\t.", file);
28867 assemble_name (file, name);
28868 fputs (",@function\n", file);
28869 if (TREE_PUBLIC (decl) && ! DECL_WEAK (decl))
28871 fputs ("\t.globl\t.", file);
28872 assemble_name (file, name);
28873 putc ('\n', file);
28876 else
28877 ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
28878 ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
28879 rs6000_output_function_entry (file, name);
28880 fputs (":\n", file);
28881 return;
28884 if (TARGET_RELOCATABLE
28885 && !TARGET_SECURE_PLT
28886 && (get_pool_size () != 0 || crtl->profile)
28887 && uses_TOC ())
28889 char buf[256];
28891 (*targetm.asm_out.internal_label) (file, "LCL", rs6000_pic_labelno);
28893 ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
28894 fprintf (file, "\t.long ");
28895 assemble_name (file, buf);
28896 putc ('-', file);
28897 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
28898 assemble_name (file, buf);
28899 putc ('\n', file);
28902 ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
28903 ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
28905 if (DEFAULT_ABI == ABI_AIX)
28907 const char *desc_name, *orig_name;
28909 orig_name = (*targetm.strip_name_encoding) (name);
28910 desc_name = orig_name;
28911 while (*desc_name == '.')
28912 desc_name++;
28914 if (TREE_PUBLIC (decl))
28915 fprintf (file, "\t.globl %s\n", desc_name);
28917 fprintf (file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
28918 fprintf (file, "%s:\n", desc_name);
28919 fprintf (file, "\t.long %s\n", orig_name);
28920 fputs ("\t.long _GLOBAL_OFFSET_TABLE_\n", file);
28921 fputs ("\t.long 0\n", file);
28922 fprintf (file, "\t.previous\n");
28924 ASM_OUTPUT_LABEL (file, name);
28927 static void rs6000_elf_file_end (void) ATTRIBUTE_UNUSED;
28928 static void
28929 rs6000_elf_file_end (void)
28931 #ifdef HAVE_AS_GNU_ATTRIBUTE
28932 if (TARGET_32BIT && DEFAULT_ABI == ABI_V4)
28934 if (rs6000_passes_float)
28935 fprintf (asm_out_file, "\t.gnu_attribute 4, %d\n",
28936 ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT) ? 1
28937 : (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_SINGLE_FLOAT) ? 3
28938 : 2));
28939 if (rs6000_passes_vector)
28940 fprintf (asm_out_file, "\t.gnu_attribute 8, %d\n",
28941 (TARGET_ALTIVEC_ABI ? 2
28942 : TARGET_SPE_ABI ? 3
28943 : 1));
28944 if (rs6000_returns_struct)
28945 fprintf (asm_out_file, "\t.gnu_attribute 12, %d\n",
28946 aix_struct_return ? 2 : 1);
28948 #endif
28949 #if defined (POWERPC_LINUX) || defined (POWERPC_FREEBSD)
28950 if (TARGET_32BIT || DEFAULT_ABI == ABI_ELFv2)
28951 file_end_indicate_exec_stack ();
28952 #endif
28954 #endif
28956 #if TARGET_XCOFF
28957 static void
28958 rs6000_xcoff_asm_output_anchor (rtx symbol)
28960 char buffer[100];
28962 sprintf (buffer, "$ + " HOST_WIDE_INT_PRINT_DEC,
28963 SYMBOL_REF_BLOCK_OFFSET (symbol));
28964 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
28967 static void
28968 rs6000_xcoff_asm_globalize_label (FILE *stream, const char *name)
28970 fputs (GLOBAL_ASM_OP, stream);
28971 RS6000_OUTPUT_BASENAME (stream, name);
28972 putc ('\n', stream);
28975 /* A get_unnamed_decl callback, used for read-only sections. PTR
28976 points to the section string variable. */
28978 static void
28979 rs6000_xcoff_output_readonly_section_asm_op (const void *directive)
28981 fprintf (asm_out_file, "\t.csect %s[RO],%s\n",
28982 *(const char *const *) directive,
28983 XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
28986 /* Likewise for read-write sections. */
28988 static void
28989 rs6000_xcoff_output_readwrite_section_asm_op (const void *directive)
28991 fprintf (asm_out_file, "\t.csect %s[RW],%s\n",
28992 *(const char *const *) directive,
28993 XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
28996 static void
28997 rs6000_xcoff_output_tls_section_asm_op (const void *directive)
28999 fprintf (asm_out_file, "\t.csect %s[TL],%s\n",
29000 *(const char *const *) directive,
29001 XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
29004 /* A get_unnamed_section callback, used for switching to toc_section. */
29006 static void
29007 rs6000_xcoff_output_toc_section_asm_op (const void *data ATTRIBUTE_UNUSED)
29009 if (TARGET_MINIMAL_TOC)
29011 /* toc_section is always selected at least once from
29012 rs6000_xcoff_file_start, so this is guaranteed to
29013 always be defined once and only once in each file. */
29014 if (!toc_initialized)
29016 fputs ("\t.toc\nLCTOC..1:\n", asm_out_file);
29017 fputs ("\t.tc toc_table[TC],toc_table[RW]\n", asm_out_file);
29018 toc_initialized = 1;
29020 fprintf (asm_out_file, "\t.csect toc_table[RW]%s\n",
29021 (TARGET_32BIT ? "" : ",3"));
29023 else
29024 fputs ("\t.toc\n", asm_out_file);
29027 /* Implement TARGET_ASM_INIT_SECTIONS. */
29029 static void
29030 rs6000_xcoff_asm_init_sections (void)
29032 read_only_data_section
29033 = get_unnamed_section (0, rs6000_xcoff_output_readonly_section_asm_op,
29034 &xcoff_read_only_section_name);
29036 private_data_section
29037 = get_unnamed_section (SECTION_WRITE,
29038 rs6000_xcoff_output_readwrite_section_asm_op,
29039 &xcoff_private_data_section_name);
29041 tls_data_section
29042 = get_unnamed_section (SECTION_TLS,
29043 rs6000_xcoff_output_tls_section_asm_op,
29044 &xcoff_tls_data_section_name);
29046 tls_private_data_section
29047 = get_unnamed_section (SECTION_TLS,
29048 rs6000_xcoff_output_tls_section_asm_op,
29049 &xcoff_private_data_section_name);
29051 read_only_private_data_section
29052 = get_unnamed_section (0, rs6000_xcoff_output_readonly_section_asm_op,
29053 &xcoff_private_data_section_name);
29055 toc_section
29056 = get_unnamed_section (0, rs6000_xcoff_output_toc_section_asm_op, NULL);
29058 readonly_data_section = read_only_data_section;
29059 exception_section = data_section;
29062 static int
29063 rs6000_xcoff_reloc_rw_mask (void)
29065 return 3;
29068 static void
29069 rs6000_xcoff_asm_named_section (const char *name, unsigned int flags,
29070 tree decl ATTRIBUTE_UNUSED)
29072 int smclass;
29073 static const char * const suffix[4] = { "PR", "RO", "RW", "TL" };
29075 if (flags & SECTION_CODE)
29076 smclass = 0;
29077 else if (flags & SECTION_TLS)
29078 smclass = 3;
29079 else if (flags & SECTION_WRITE)
29080 smclass = 2;
29081 else
29082 smclass = 1;
29084 fprintf (asm_out_file, "\t.csect %s%s[%s],%u\n",
29085 (flags & SECTION_CODE) ? "." : "",
29086 name, suffix[smclass], flags & SECTION_ENTSIZE);
29089 #define IN_NAMED_SECTION(DECL) \
29090 ((TREE_CODE (DECL) == FUNCTION_DECL || TREE_CODE (DECL) == VAR_DECL) \
29091 && DECL_SECTION_NAME (DECL) != NULL_TREE)
29093 static section *
29094 rs6000_xcoff_select_section (tree decl, int reloc,
29095 unsigned HOST_WIDE_INT align)
29097 /* Place variables with alignment stricter than BIGGEST_ALIGNMENT into
29098 named section. */
29099 if (align > BIGGEST_ALIGNMENT)
29101 resolve_unique_section (decl, reloc, true);
29102 if (IN_NAMED_SECTION (decl))
29103 return get_named_section (decl, NULL, reloc);
29106 if (decl_readonly_section (decl, reloc))
29108 if (TREE_PUBLIC (decl))
29109 return read_only_data_section;
29110 else
29111 return read_only_private_data_section;
29113 else
29115 #if HAVE_AS_TLS
29116 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
29118 if (TREE_PUBLIC (decl))
29119 return tls_data_section;
29120 else if (bss_initializer_p (decl))
29122 /* Convert to COMMON to emit in BSS. */
29123 DECL_COMMON (decl) = 1;
29124 return tls_comm_section;
29126 else
29127 return tls_private_data_section;
29129 else
29130 #endif
29131 if (TREE_PUBLIC (decl))
29132 return data_section;
29133 else
29134 return private_data_section;
29138 static void
29139 rs6000_xcoff_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED)
29141 const char *name;
29143 /* Use select_section for private data and uninitialized data with
29144 alignment <= BIGGEST_ALIGNMENT. */
29145 if (!TREE_PUBLIC (decl)
29146 || DECL_COMMON (decl)
29147 || (DECL_INITIAL (decl) == NULL_TREE
29148 && DECL_ALIGN (decl) <= BIGGEST_ALIGNMENT)
29149 || DECL_INITIAL (decl) == error_mark_node
29150 || (flag_zero_initialized_in_bss
29151 && initializer_zerop (DECL_INITIAL (decl))))
29152 return;
29154 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
29155 name = (*targetm.strip_name_encoding) (name);
29156 DECL_SECTION_NAME (decl) = build_string (strlen (name), name);
29159 /* Select section for constant in constant pool.
29161 On RS/6000, all constants are in the private read-only data area.
29162 However, if this is being placed in the TOC it must be output as a
29163 toc entry. */
29165 static section *
29166 rs6000_xcoff_select_rtx_section (enum machine_mode mode, rtx x,
29167 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
29169 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
29170 return toc_section;
29171 else
29172 return read_only_private_data_section;
29175 /* Remove any trailing [DS] or the like from the symbol name. */
29177 static const char *
29178 rs6000_xcoff_strip_name_encoding (const char *name)
29180 size_t len;
29181 if (*name == '*')
29182 name++;
29183 len = strlen (name);
29184 if (name[len - 1] == ']')
29185 return ggc_alloc_string (name, len - 4);
29186 else
29187 return name;
29190 /* Section attributes. AIX is always PIC. */
29192 static unsigned int
29193 rs6000_xcoff_section_type_flags (tree decl, const char *name, int reloc)
29195 unsigned int align;
29196 unsigned int flags = default_section_type_flags (decl, name, reloc);
29198 /* Align to at least UNIT size. */
29199 if ((flags & SECTION_CODE) != 0 || !decl || !DECL_P (decl))
29200 align = MIN_UNITS_PER_WORD;
29201 else
29202 /* Increase alignment of large objects if not already stricter. */
29203 align = MAX ((DECL_ALIGN (decl) / BITS_PER_UNIT),
29204 int_size_in_bytes (TREE_TYPE (decl)) > MIN_UNITS_PER_WORD
29205 ? UNITS_PER_FP_WORD : MIN_UNITS_PER_WORD);
29207 return flags | (exact_log2 (align) & SECTION_ENTSIZE);
29210 /* Output at beginning of assembler file.
29212 Initialize the section names for the RS/6000 at this point.
29214 Specify filename, including full path, to assembler.
29216 We want to go into the TOC section so at least one .toc will be emitted.
29217 Also, in order to output proper .bs/.es pairs, we need at least one static
29218 [RW] section emitted.
29220 Finally, declare mcount when profiling to make the assembler happy. */
29222 static void
29223 rs6000_xcoff_file_start (void)
29225 rs6000_gen_section_name (&xcoff_bss_section_name,
29226 main_input_filename, ".bss_");
29227 rs6000_gen_section_name (&xcoff_private_data_section_name,
29228 main_input_filename, ".rw_");
29229 rs6000_gen_section_name (&xcoff_read_only_section_name,
29230 main_input_filename, ".ro_");
29231 rs6000_gen_section_name (&xcoff_tls_data_section_name,
29232 main_input_filename, ".tls_");
29233 rs6000_gen_section_name (&xcoff_tbss_section_name,
29234 main_input_filename, ".tbss_[UL]");
29236 fputs ("\t.file\t", asm_out_file);
29237 output_quoted_string (asm_out_file, main_input_filename);
29238 fputc ('\n', asm_out_file);
29239 if (write_symbols != NO_DEBUG)
29240 switch_to_section (private_data_section);
29241 switch_to_section (text_section);
29242 if (profile_flag)
29243 fprintf (asm_out_file, "\t.extern %s\n", RS6000_MCOUNT);
29244 rs6000_file_start ();
29247 /* Output at end of assembler file.
29248 On the RS/6000, referencing data should automatically pull in text. */
29250 static void
29251 rs6000_xcoff_file_end (void)
29253 switch_to_section (text_section);
29254 fputs ("_section_.text:\n", asm_out_file);
29255 switch_to_section (data_section);
29256 fputs (TARGET_32BIT
29257 ? "\t.long _section_.text\n" : "\t.llong _section_.text\n",
29258 asm_out_file);
29261 #ifdef HAVE_AS_TLS
29262 static void
29263 rs6000_xcoff_encode_section_info (tree decl, rtx rtl, int first)
29265 rtx symbol;
29266 int flags;
29268 default_encode_section_info (decl, rtl, first);
29270 /* Careful not to prod global register variables. */
29271 if (!MEM_P (rtl))
29272 return;
29273 symbol = XEXP (rtl, 0);
29274 if (GET_CODE (symbol) != SYMBOL_REF)
29275 return;
29277 flags = SYMBOL_REF_FLAGS (symbol);
29279 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
29280 flags &= ~SYMBOL_FLAG_HAS_BLOCK_INFO;
29282 SYMBOL_REF_FLAGS (symbol) = flags;
29284 #endif /* HAVE_AS_TLS */
29285 #endif /* TARGET_XCOFF */
29287 /* Compute a (partial) cost for rtx X. Return true if the complete
29288 cost has been computed, and false if subexpressions should be
29289 scanned. In either case, *TOTAL contains the cost result. */
29291 static bool
29292 rs6000_rtx_costs (rtx x, int code, int outer_code, int opno ATTRIBUTE_UNUSED,
29293 int *total, bool speed)
29295 enum machine_mode mode = GET_MODE (x);
29297 switch (code)
29299 /* On the RS/6000, if it is valid in the insn, it is free. */
29300 case CONST_INT:
29301 if (((outer_code == SET
29302 || outer_code == PLUS
29303 || outer_code == MINUS)
29304 && (satisfies_constraint_I (x)
29305 || satisfies_constraint_L (x)))
29306 || (outer_code == AND
29307 && (satisfies_constraint_K (x)
29308 || (mode == SImode
29309 ? satisfies_constraint_L (x)
29310 : satisfies_constraint_J (x))
29311 || mask_operand (x, mode)
29312 || (mode == DImode
29313 && mask64_operand (x, DImode))))
29314 || ((outer_code == IOR || outer_code == XOR)
29315 && (satisfies_constraint_K (x)
29316 || (mode == SImode
29317 ? satisfies_constraint_L (x)
29318 : satisfies_constraint_J (x))))
29319 || outer_code == ASHIFT
29320 || outer_code == ASHIFTRT
29321 || outer_code == LSHIFTRT
29322 || outer_code == ROTATE
29323 || outer_code == ROTATERT
29324 || outer_code == ZERO_EXTRACT
29325 || (outer_code == MULT
29326 && satisfies_constraint_I (x))
29327 || ((outer_code == DIV || outer_code == UDIV
29328 || outer_code == MOD || outer_code == UMOD)
29329 && exact_log2 (INTVAL (x)) >= 0)
29330 || (outer_code == COMPARE
29331 && (satisfies_constraint_I (x)
29332 || satisfies_constraint_K (x)))
29333 || ((outer_code == EQ || outer_code == NE)
29334 && (satisfies_constraint_I (x)
29335 || satisfies_constraint_K (x)
29336 || (mode == SImode
29337 ? satisfies_constraint_L (x)
29338 : satisfies_constraint_J (x))))
29339 || (outer_code == GTU
29340 && satisfies_constraint_I (x))
29341 || (outer_code == LTU
29342 && satisfies_constraint_P (x)))
29344 *total = 0;
29345 return true;
29347 else if ((outer_code == PLUS
29348 && reg_or_add_cint_operand (x, VOIDmode))
29349 || (outer_code == MINUS
29350 && reg_or_sub_cint_operand (x, VOIDmode))
29351 || ((outer_code == SET
29352 || outer_code == IOR
29353 || outer_code == XOR)
29354 && (INTVAL (x)
29355 & ~ (unsigned HOST_WIDE_INT) 0xffffffff) == 0))
29357 *total = COSTS_N_INSNS (1);
29358 return true;
29360 /* FALLTHRU */
29362 case CONST_DOUBLE:
29363 case CONST:
29364 case HIGH:
29365 case SYMBOL_REF:
29366 case MEM:
29367 /* When optimizing for size, MEM should be slightly more expensive
29368 than generating address, e.g., (plus (reg) (const)).
29369 L1 cache latency is about two instructions. */
29370 *total = !speed ? COSTS_N_INSNS (1) + 1 : COSTS_N_INSNS (2);
29371 return true;
29373 case LABEL_REF:
29374 *total = 0;
29375 return true;
29377 case PLUS:
29378 case MINUS:
29379 if (FLOAT_MODE_P (mode))
29380 *total = rs6000_cost->fp;
29381 else
29382 *total = COSTS_N_INSNS (1);
29383 return false;
29385 case MULT:
29386 if (GET_CODE (XEXP (x, 1)) == CONST_INT
29387 && satisfies_constraint_I (XEXP (x, 1)))
29389 if (INTVAL (XEXP (x, 1)) >= -256
29390 && INTVAL (XEXP (x, 1)) <= 255)
29391 *total = rs6000_cost->mulsi_const9;
29392 else
29393 *total = rs6000_cost->mulsi_const;
29395 else if (mode == SFmode)
29396 *total = rs6000_cost->fp;
29397 else if (FLOAT_MODE_P (mode))
29398 *total = rs6000_cost->dmul;
29399 else if (mode == DImode)
29400 *total = rs6000_cost->muldi;
29401 else
29402 *total = rs6000_cost->mulsi;
29403 return false;
29405 case FMA:
29406 if (mode == SFmode)
29407 *total = rs6000_cost->fp;
29408 else
29409 *total = rs6000_cost->dmul;
29410 break;
29412 case DIV:
29413 case MOD:
29414 if (FLOAT_MODE_P (mode))
29416 *total = mode == DFmode ? rs6000_cost->ddiv
29417 : rs6000_cost->sdiv;
29418 return false;
29420 /* FALLTHRU */
29422 case UDIV:
29423 case UMOD:
29424 if (GET_CODE (XEXP (x, 1)) == CONST_INT
29425 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0)
29427 if (code == DIV || code == MOD)
29428 /* Shift, addze */
29429 *total = COSTS_N_INSNS (2);
29430 else
29431 /* Shift */
29432 *total = COSTS_N_INSNS (1);
29434 else
29436 if (GET_MODE (XEXP (x, 1)) == DImode)
29437 *total = rs6000_cost->divdi;
29438 else
29439 *total = rs6000_cost->divsi;
29441 /* Add in shift and subtract for MOD. */
29442 if (code == MOD || code == UMOD)
29443 *total += COSTS_N_INSNS (2);
29444 return false;
29446 case CTZ:
29447 case FFS:
29448 *total = COSTS_N_INSNS (4);
29449 return false;
29451 case POPCOUNT:
29452 *total = COSTS_N_INSNS (TARGET_POPCNTD ? 1 : 6);
29453 return false;
29455 case PARITY:
29456 *total = COSTS_N_INSNS (TARGET_CMPB ? 2 : 6);
29457 return false;
29459 case NOT:
29460 if (outer_code == AND || outer_code == IOR || outer_code == XOR)
29462 *total = 0;
29463 return false;
29465 /* FALLTHRU */
29467 case AND:
29468 case CLZ:
29469 case IOR:
29470 case XOR:
29471 case ZERO_EXTRACT:
29472 *total = COSTS_N_INSNS (1);
29473 return false;
29475 case ASHIFT:
29476 case ASHIFTRT:
29477 case LSHIFTRT:
29478 case ROTATE:
29479 case ROTATERT:
29480 /* Handle mul_highpart. */
29481 if (outer_code == TRUNCATE
29482 && GET_CODE (XEXP (x, 0)) == MULT)
29484 if (mode == DImode)
29485 *total = rs6000_cost->muldi;
29486 else
29487 *total = rs6000_cost->mulsi;
29488 return true;
29490 else if (outer_code == AND)
29491 *total = 0;
29492 else
29493 *total = COSTS_N_INSNS (1);
29494 return false;
29496 case SIGN_EXTEND:
29497 case ZERO_EXTEND:
29498 if (GET_CODE (XEXP (x, 0)) == MEM)
29499 *total = 0;
29500 else
29501 *total = COSTS_N_INSNS (1);
29502 return false;
29504 case COMPARE:
29505 case NEG:
29506 case ABS:
29507 if (!FLOAT_MODE_P (mode))
29509 *total = COSTS_N_INSNS (1);
29510 return false;
29512 /* FALLTHRU */
29514 case FLOAT:
29515 case UNSIGNED_FLOAT:
29516 case FIX:
29517 case UNSIGNED_FIX:
29518 case FLOAT_TRUNCATE:
29519 *total = rs6000_cost->fp;
29520 return false;
29522 case FLOAT_EXTEND:
29523 if (mode == DFmode)
29524 *total = 0;
29525 else
29526 *total = rs6000_cost->fp;
29527 return false;
29529 case UNSPEC:
29530 switch (XINT (x, 1))
29532 case UNSPEC_FRSP:
29533 *total = rs6000_cost->fp;
29534 return true;
29536 default:
29537 break;
29539 break;
29541 case CALL:
29542 case IF_THEN_ELSE:
29543 if (!speed)
29545 *total = COSTS_N_INSNS (1);
29546 return true;
29548 else if (FLOAT_MODE_P (mode)
29549 && TARGET_PPC_GFXOPT && TARGET_HARD_FLOAT && TARGET_FPRS)
29551 *total = rs6000_cost->fp;
29552 return false;
29554 break;
29556 case EQ:
29557 case GTU:
29558 case LTU:
29559 /* Carry bit requires mode == Pmode.
29560 NEG or PLUS already counted so only add one. */
29561 if (mode == Pmode
29562 && (outer_code == NEG || outer_code == PLUS))
29564 *total = COSTS_N_INSNS (1);
29565 return true;
29567 if (outer_code == SET)
29569 if (XEXP (x, 1) == const0_rtx)
29571 if (TARGET_ISEL && !TARGET_MFCRF)
29572 *total = COSTS_N_INSNS (8);
29573 else
29574 *total = COSTS_N_INSNS (2);
29575 return true;
29577 else if (mode == Pmode)
29579 *total = COSTS_N_INSNS (3);
29580 return false;
29583 /* FALLTHRU */
29585 case GT:
29586 case LT:
29587 case UNORDERED:
29588 if (outer_code == SET && (XEXP (x, 1) == const0_rtx))
29590 if (TARGET_ISEL && !TARGET_MFCRF)
29591 *total = COSTS_N_INSNS (8);
29592 else
29593 *total = COSTS_N_INSNS (2);
29594 return true;
29596 /* CC COMPARE. */
29597 if (outer_code == COMPARE)
29599 *total = 0;
29600 return true;
29602 break;
29604 default:
29605 break;
29608 return false;
29611 /* Debug form of r6000_rtx_costs that is selected if -mdebug=cost. */
29613 static bool
29614 rs6000_debug_rtx_costs (rtx x, int code, int outer_code, int opno, int *total,
29615 bool speed)
29617 bool ret = rs6000_rtx_costs (x, code, outer_code, opno, total, speed);
29619 fprintf (stderr,
29620 "\nrs6000_rtx_costs, return = %s, code = %s, outer_code = %s, "
29621 "opno = %d, total = %d, speed = %s, x:\n",
29622 ret ? "complete" : "scan inner",
29623 GET_RTX_NAME (code),
29624 GET_RTX_NAME (outer_code),
29625 opno,
29626 *total,
29627 speed ? "true" : "false");
29629 debug_rtx (x);
29631 return ret;
29634 /* Debug form of ADDRESS_COST that is selected if -mdebug=cost. */
29636 static int
29637 rs6000_debug_address_cost (rtx x, enum machine_mode mode,
29638 addr_space_t as, bool speed)
29640 int ret = TARGET_ADDRESS_COST (x, mode, as, speed);
29642 fprintf (stderr, "\nrs6000_address_cost, return = %d, speed = %s, x:\n",
29643 ret, speed ? "true" : "false");
29644 debug_rtx (x);
29646 return ret;
29650 /* A C expression returning the cost of moving data from a register of class
29651 CLASS1 to one of CLASS2. */
29653 static int
29654 rs6000_register_move_cost (enum machine_mode mode,
29655 reg_class_t from, reg_class_t to)
29657 int ret;
29659 if (TARGET_DEBUG_COST)
29660 dbg_cost_ctrl++;
29662 /* Moves from/to GENERAL_REGS. */
29663 if (reg_classes_intersect_p (to, GENERAL_REGS)
29664 || reg_classes_intersect_p (from, GENERAL_REGS))
29666 reg_class_t rclass = from;
29668 if (! reg_classes_intersect_p (to, GENERAL_REGS))
29669 rclass = to;
29671 if (rclass == FLOAT_REGS || rclass == ALTIVEC_REGS || rclass == VSX_REGS)
29672 ret = (rs6000_memory_move_cost (mode, rclass, false)
29673 + rs6000_memory_move_cost (mode, GENERAL_REGS, false));
29675 /* It's more expensive to move CR_REGS than CR0_REGS because of the
29676 shift. */
29677 else if (rclass == CR_REGS)
29678 ret = 4;
29680 /* For those processors that have slow LR/CTR moves, make them more
29681 expensive than memory in order to bias spills to memory .*/
29682 else if ((rs6000_cpu == PROCESSOR_POWER6
29683 || rs6000_cpu == PROCESSOR_POWER7
29684 || rs6000_cpu == PROCESSOR_POWER8)
29685 && reg_classes_intersect_p (rclass, LINK_OR_CTR_REGS))
29686 ret = 6 * hard_regno_nregs[0][mode];
29688 else
29689 /* A move will cost one instruction per GPR moved. */
29690 ret = 2 * hard_regno_nregs[0][mode];
29693 /* If we have VSX, we can easily move between FPR or Altivec registers. */
29694 else if (VECTOR_MEM_VSX_P (mode)
29695 && reg_classes_intersect_p (to, VSX_REGS)
29696 && reg_classes_intersect_p (from, VSX_REGS))
29697 ret = 2 * hard_regno_nregs[32][mode];
29699 /* Moving between two similar registers is just one instruction. */
29700 else if (reg_classes_intersect_p (to, from))
29701 ret = (mode == TFmode || mode == TDmode) ? 4 : 2;
29703 /* Everything else has to go through GENERAL_REGS. */
29704 else
29705 ret = (rs6000_register_move_cost (mode, GENERAL_REGS, to)
29706 + rs6000_register_move_cost (mode, from, GENERAL_REGS));
29708 if (TARGET_DEBUG_COST)
29710 if (dbg_cost_ctrl == 1)
29711 fprintf (stderr,
29712 "rs6000_register_move_cost:, ret=%d, mode=%s, from=%s, to=%s\n",
29713 ret, GET_MODE_NAME (mode), reg_class_names[from],
29714 reg_class_names[to]);
29715 dbg_cost_ctrl--;
29718 return ret;
29721 /* A C expressions returning the cost of moving data of MODE from a register to
29722 or from memory. */
29724 static int
29725 rs6000_memory_move_cost (enum machine_mode mode, reg_class_t rclass,
29726 bool in ATTRIBUTE_UNUSED)
29728 int ret;
29730 if (TARGET_DEBUG_COST)
29731 dbg_cost_ctrl++;
29733 if (reg_classes_intersect_p (rclass, GENERAL_REGS))
29734 ret = 4 * hard_regno_nregs[0][mode];
29735 else if ((reg_classes_intersect_p (rclass, FLOAT_REGS)
29736 || reg_classes_intersect_p (rclass, VSX_REGS)))
29737 ret = 4 * hard_regno_nregs[32][mode];
29738 else if (reg_classes_intersect_p (rclass, ALTIVEC_REGS))
29739 ret = 4 * hard_regno_nregs[FIRST_ALTIVEC_REGNO][mode];
29740 else
29741 ret = 4 + rs6000_register_move_cost (mode, rclass, GENERAL_REGS);
29743 if (TARGET_DEBUG_COST)
29745 if (dbg_cost_ctrl == 1)
29746 fprintf (stderr,
29747 "rs6000_memory_move_cost: ret=%d, mode=%s, rclass=%s, in=%d\n",
29748 ret, GET_MODE_NAME (mode), reg_class_names[rclass], in);
29749 dbg_cost_ctrl--;
29752 return ret;
29755 /* Returns a code for a target-specific builtin that implements
29756 reciprocal of the function, or NULL_TREE if not available. */
29758 static tree
29759 rs6000_builtin_reciprocal (unsigned int fn, bool md_fn,
29760 bool sqrt ATTRIBUTE_UNUSED)
29762 if (optimize_insn_for_size_p ())
29763 return NULL_TREE;
29765 if (md_fn)
29766 switch (fn)
29768 case VSX_BUILTIN_XVSQRTDP:
29769 if (!RS6000_RECIP_AUTO_RSQRTE_P (V2DFmode))
29770 return NULL_TREE;
29772 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_2DF];
29774 case VSX_BUILTIN_XVSQRTSP:
29775 if (!RS6000_RECIP_AUTO_RSQRTE_P (V4SFmode))
29776 return NULL_TREE;
29778 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_4SF];
29780 default:
29781 return NULL_TREE;
29784 else
29785 switch (fn)
29787 case BUILT_IN_SQRT:
29788 if (!RS6000_RECIP_AUTO_RSQRTE_P (DFmode))
29789 return NULL_TREE;
29791 return rs6000_builtin_decls[RS6000_BUILTIN_RSQRT];
29793 case BUILT_IN_SQRTF:
29794 if (!RS6000_RECIP_AUTO_RSQRTE_P (SFmode))
29795 return NULL_TREE;
29797 return rs6000_builtin_decls[RS6000_BUILTIN_RSQRTF];
29799 default:
29800 return NULL_TREE;
29804 /* Load up a constant. If the mode is a vector mode, splat the value across
29805 all of the vector elements. */
29807 static rtx
29808 rs6000_load_constant_and_splat (enum machine_mode mode, REAL_VALUE_TYPE dconst)
29810 rtx reg;
29812 if (mode == SFmode || mode == DFmode)
29814 rtx d = CONST_DOUBLE_FROM_REAL_VALUE (dconst, mode);
29815 reg = force_reg (mode, d);
29817 else if (mode == V4SFmode)
29819 rtx d = CONST_DOUBLE_FROM_REAL_VALUE (dconst, SFmode);
29820 rtvec v = gen_rtvec (4, d, d, d, d);
29821 reg = gen_reg_rtx (mode);
29822 rs6000_expand_vector_init (reg, gen_rtx_PARALLEL (mode, v));
29824 else if (mode == V2DFmode)
29826 rtx d = CONST_DOUBLE_FROM_REAL_VALUE (dconst, DFmode);
29827 rtvec v = gen_rtvec (2, d, d);
29828 reg = gen_reg_rtx (mode);
29829 rs6000_expand_vector_init (reg, gen_rtx_PARALLEL (mode, v));
29831 else
29832 gcc_unreachable ();
29834 return reg;
29837 /* Generate an FMA instruction. */
29839 static void
29840 rs6000_emit_madd (rtx target, rtx m1, rtx m2, rtx a)
29842 enum machine_mode mode = GET_MODE (target);
29843 rtx dst;
29845 dst = expand_ternary_op (mode, fma_optab, m1, m2, a, target, 0);
29846 gcc_assert (dst != NULL);
29848 if (dst != target)
29849 emit_move_insn (target, dst);
29852 /* Generate a FMSUB instruction: dst = fma(m1, m2, -a). */
29854 static void
29855 rs6000_emit_msub (rtx target, rtx m1, rtx m2, rtx a)
29857 enum machine_mode mode = GET_MODE (target);
29858 rtx dst;
29860 /* Altivec does not support fms directly;
29861 generate in terms of fma in that case. */
29862 if (optab_handler (fms_optab, mode) != CODE_FOR_nothing)
29863 dst = expand_ternary_op (mode, fms_optab, m1, m2, a, target, 0);
29864 else
29866 a = expand_unop (mode, neg_optab, a, NULL_RTX, 0);
29867 dst = expand_ternary_op (mode, fma_optab, m1, m2, a, target, 0);
29869 gcc_assert (dst != NULL);
29871 if (dst != target)
29872 emit_move_insn (target, dst);
29875 /* Generate a FNMSUB instruction: dst = -fma(m1, m2, -a). */
29877 static void
29878 rs6000_emit_nmsub (rtx dst, rtx m1, rtx m2, rtx a)
29880 enum machine_mode mode = GET_MODE (dst);
29881 rtx r;
29883 /* This is a tad more complicated, since the fnma_optab is for
29884 a different expression: fma(-m1, m2, a), which is the same
29885 thing except in the case of signed zeros.
29887 Fortunately we know that if FMA is supported that FNMSUB is
29888 also supported in the ISA. Just expand it directly. */
29890 gcc_assert (optab_handler (fma_optab, mode) != CODE_FOR_nothing);
29892 r = gen_rtx_NEG (mode, a);
29893 r = gen_rtx_FMA (mode, m1, m2, r);
29894 r = gen_rtx_NEG (mode, r);
29895 emit_insn (gen_rtx_SET (VOIDmode, dst, r));
29898 /* Newton-Raphson approximation of floating point divide DST = N/D. If NOTE_P,
29899 add a reg_note saying that this was a division. Support both scalar and
29900 vector divide. Assumes no trapping math and finite arguments. */
29902 void
29903 rs6000_emit_swdiv (rtx dst, rtx n, rtx d, bool note_p)
29905 enum machine_mode mode = GET_MODE (dst);
29906 rtx one, x0, e0, x1, xprev, eprev, xnext, enext, u, v;
29907 int i;
29909 /* Low precision estimates guarantee 5 bits of accuracy. High
29910 precision estimates guarantee 14 bits of accuracy. SFmode
29911 requires 23 bits of accuracy. DFmode requires 52 bits of
29912 accuracy. Each pass at least doubles the accuracy, leading
29913 to the following. */
29914 int passes = (TARGET_RECIP_PRECISION) ? 1 : 3;
29915 if (mode == DFmode || mode == V2DFmode)
29916 passes++;
29918 enum insn_code code = optab_handler (smul_optab, mode);
29919 insn_gen_fn gen_mul = GEN_FCN (code);
29921 gcc_assert (code != CODE_FOR_nothing);
29923 one = rs6000_load_constant_and_splat (mode, dconst1);
29925 /* x0 = 1./d estimate */
29926 x0 = gen_reg_rtx (mode);
29927 emit_insn (gen_rtx_SET (VOIDmode, x0,
29928 gen_rtx_UNSPEC (mode, gen_rtvec (1, d),
29929 UNSPEC_FRES)));
29931 /* Each iteration but the last calculates x_(i+1) = x_i * (2 - d * x_i). */
29932 if (passes > 1) {
29934 /* e0 = 1. - d * x0 */
29935 e0 = gen_reg_rtx (mode);
29936 rs6000_emit_nmsub (e0, d, x0, one);
29938 /* x1 = x0 + e0 * x0 */
29939 x1 = gen_reg_rtx (mode);
29940 rs6000_emit_madd (x1, e0, x0, x0);
29942 for (i = 0, xprev = x1, eprev = e0; i < passes - 2;
29943 ++i, xprev = xnext, eprev = enext) {
29945 /* enext = eprev * eprev */
29946 enext = gen_reg_rtx (mode);
29947 emit_insn (gen_mul (enext, eprev, eprev));
29949 /* xnext = xprev + enext * xprev */
29950 xnext = gen_reg_rtx (mode);
29951 rs6000_emit_madd (xnext, enext, xprev, xprev);
29954 } else
29955 xprev = x0;
29957 /* The last iteration calculates x_(i+1) = n * x_i * (2 - d * x_i). */
29959 /* u = n * xprev */
29960 u = gen_reg_rtx (mode);
29961 emit_insn (gen_mul (u, n, xprev));
29963 /* v = n - (d * u) */
29964 v = gen_reg_rtx (mode);
29965 rs6000_emit_nmsub (v, d, u, n);
29967 /* dst = (v * xprev) + u */
29968 rs6000_emit_madd (dst, v, xprev, u);
29970 if (note_p)
29971 add_reg_note (get_last_insn (), REG_EQUAL, gen_rtx_DIV (mode, n, d));
29974 /* Newton-Raphson approximation of single/double-precision floating point
29975 rsqrt. Assumes no trapping math and finite arguments. */
29977 void
29978 rs6000_emit_swrsqrt (rtx dst, rtx src)
29980 enum machine_mode mode = GET_MODE (src);
29981 rtx x0 = gen_reg_rtx (mode);
29982 rtx y = gen_reg_rtx (mode);
29984 /* Low precision estimates guarantee 5 bits of accuracy. High
29985 precision estimates guarantee 14 bits of accuracy. SFmode
29986 requires 23 bits of accuracy. DFmode requires 52 bits of
29987 accuracy. Each pass at least doubles the accuracy, leading
29988 to the following. */
29989 int passes = (TARGET_RECIP_PRECISION) ? 1 : 3;
29990 if (mode == DFmode || mode == V2DFmode)
29991 passes++;
29993 REAL_VALUE_TYPE dconst3_2;
29994 int i;
29995 rtx halfthree;
29996 enum insn_code code = optab_handler (smul_optab, mode);
29997 insn_gen_fn gen_mul = GEN_FCN (code);
29999 gcc_assert (code != CODE_FOR_nothing);
30001 /* Load up the constant 1.5 either as a scalar, or as a vector. */
30002 real_from_integer (&dconst3_2, VOIDmode, 3, 0, 0);
30003 SET_REAL_EXP (&dconst3_2, REAL_EXP (&dconst3_2) - 1);
30005 halfthree = rs6000_load_constant_and_splat (mode, dconst3_2);
30007 /* x0 = rsqrt estimate */
30008 emit_insn (gen_rtx_SET (VOIDmode, x0,
30009 gen_rtx_UNSPEC (mode, gen_rtvec (1, src),
30010 UNSPEC_RSQRT)));
30012 /* y = 0.5 * src = 1.5 * src - src -> fewer constants */
30013 rs6000_emit_msub (y, src, halfthree, src);
30015 for (i = 0; i < passes; i++)
30017 rtx x1 = gen_reg_rtx (mode);
30018 rtx u = gen_reg_rtx (mode);
30019 rtx v = gen_reg_rtx (mode);
30021 /* x1 = x0 * (1.5 - y * (x0 * x0)) */
30022 emit_insn (gen_mul (u, x0, x0));
30023 rs6000_emit_nmsub (v, y, u, halfthree);
30024 emit_insn (gen_mul (x1, x0, v));
30025 x0 = x1;
30028 emit_move_insn (dst, x0);
30029 return;
30032 /* Emit popcount intrinsic on TARGET_POPCNTB (Power5) and TARGET_POPCNTD
30033 (Power7) targets. DST is the target, and SRC is the argument operand. */
30035 void
30036 rs6000_emit_popcount (rtx dst, rtx src)
30038 enum machine_mode mode = GET_MODE (dst);
30039 rtx tmp1, tmp2;
30041 /* Use the PPC ISA 2.06 popcnt{w,d} instruction if we can. */
30042 if (TARGET_POPCNTD)
30044 if (mode == SImode)
30045 emit_insn (gen_popcntdsi2 (dst, src));
30046 else
30047 emit_insn (gen_popcntddi2 (dst, src));
30048 return;
30051 tmp1 = gen_reg_rtx (mode);
30053 if (mode == SImode)
30055 emit_insn (gen_popcntbsi2 (tmp1, src));
30056 tmp2 = expand_mult (SImode, tmp1, GEN_INT (0x01010101),
30057 NULL_RTX, 0);
30058 tmp2 = force_reg (SImode, tmp2);
30059 emit_insn (gen_lshrsi3 (dst, tmp2, GEN_INT (24)));
30061 else
30063 emit_insn (gen_popcntbdi2 (tmp1, src));
30064 tmp2 = expand_mult (DImode, tmp1,
30065 GEN_INT ((HOST_WIDE_INT)
30066 0x01010101 << 32 | 0x01010101),
30067 NULL_RTX, 0);
30068 tmp2 = force_reg (DImode, tmp2);
30069 emit_insn (gen_lshrdi3 (dst, tmp2, GEN_INT (56)));
30074 /* Emit parity intrinsic on TARGET_POPCNTB targets. DST is the
30075 target, and SRC is the argument operand. */
30077 void
30078 rs6000_emit_parity (rtx dst, rtx src)
30080 enum machine_mode mode = GET_MODE (dst);
30081 rtx tmp;
30083 tmp = gen_reg_rtx (mode);
30085 /* Use the PPC ISA 2.05 prtyw/prtyd instruction if we can. */
30086 if (TARGET_CMPB)
30088 if (mode == SImode)
30090 emit_insn (gen_popcntbsi2 (tmp, src));
30091 emit_insn (gen_paritysi2_cmpb (dst, tmp));
30093 else
30095 emit_insn (gen_popcntbdi2 (tmp, src));
30096 emit_insn (gen_paritydi2_cmpb (dst, tmp));
30098 return;
30101 if (mode == SImode)
30103 /* Is mult+shift >= shift+xor+shift+xor? */
30104 if (rs6000_cost->mulsi_const >= COSTS_N_INSNS (3))
30106 rtx tmp1, tmp2, tmp3, tmp4;
30108 tmp1 = gen_reg_rtx (SImode);
30109 emit_insn (gen_popcntbsi2 (tmp1, src));
30111 tmp2 = gen_reg_rtx (SImode);
30112 emit_insn (gen_lshrsi3 (tmp2, tmp1, GEN_INT (16)));
30113 tmp3 = gen_reg_rtx (SImode);
30114 emit_insn (gen_xorsi3 (tmp3, tmp1, tmp2));
30116 tmp4 = gen_reg_rtx (SImode);
30117 emit_insn (gen_lshrsi3 (tmp4, tmp3, GEN_INT (8)));
30118 emit_insn (gen_xorsi3 (tmp, tmp3, tmp4));
30120 else
30121 rs6000_emit_popcount (tmp, src);
30122 emit_insn (gen_andsi3 (dst, tmp, const1_rtx));
30124 else
30126 /* Is mult+shift >= shift+xor+shift+xor+shift+xor? */
30127 if (rs6000_cost->muldi >= COSTS_N_INSNS (5))
30129 rtx tmp1, tmp2, tmp3, tmp4, tmp5, tmp6;
30131 tmp1 = gen_reg_rtx (DImode);
30132 emit_insn (gen_popcntbdi2 (tmp1, src));
30134 tmp2 = gen_reg_rtx (DImode);
30135 emit_insn (gen_lshrdi3 (tmp2, tmp1, GEN_INT (32)));
30136 tmp3 = gen_reg_rtx (DImode);
30137 emit_insn (gen_xordi3 (tmp3, tmp1, tmp2));
30139 tmp4 = gen_reg_rtx (DImode);
30140 emit_insn (gen_lshrdi3 (tmp4, tmp3, GEN_INT (16)));
30141 tmp5 = gen_reg_rtx (DImode);
30142 emit_insn (gen_xordi3 (tmp5, tmp3, tmp4));
30144 tmp6 = gen_reg_rtx (DImode);
30145 emit_insn (gen_lshrdi3 (tmp6, tmp5, GEN_INT (8)));
30146 emit_insn (gen_xordi3 (tmp, tmp5, tmp6));
30148 else
30149 rs6000_emit_popcount (tmp, src);
30150 emit_insn (gen_anddi3 (dst, tmp, const1_rtx));
30154 /* Expand an Altivec constant permutation for little endian mode.
30155 There are two issues: First, the two input operands must be
30156 swapped so that together they form a double-wide array in LE
30157 order. Second, the vperm instruction has surprising behavior
30158 in LE mode: it interprets the elements of the source vectors
30159 in BE mode ("left to right") and interprets the elements of
30160 the destination vector in LE mode ("right to left"). To
30161 correct for this, we must subtract each element of the permute
30162 control vector from 31.
30164 For example, suppose we want to concatenate vr10 = {0, 1, 2, 3}
30165 with vr11 = {4, 5, 6, 7} and extract {0, 2, 4, 6} using a vperm.
30166 We place {0,1,2,3,8,9,10,11,16,17,18,19,24,25,26,27} in vr12 to
30167 serve as the permute control vector. Then, in BE mode,
30169 vperm 9,10,11,12
30171 places the desired result in vr9. However, in LE mode the
30172 vector contents will be
30174 vr10 = 00000003 00000002 00000001 00000000
30175 vr11 = 00000007 00000006 00000005 00000004
30177 The result of the vperm using the same permute control vector is
30179 vr9 = 05000000 07000000 01000000 03000000
30181 That is, the leftmost 4 bytes of vr10 are interpreted as the
30182 source for the rightmost 4 bytes of vr9, and so on.
30184 If we change the permute control vector to
30186 vr12 = {31,20,29,28,23,22,21,20,15,14,13,12,7,6,5,4}
30188 and issue
30190 vperm 9,11,10,12
30192 we get the desired
30194 vr9 = 00000006 00000004 00000002 00000000. */
30196 void
30197 altivec_expand_vec_perm_const_le (rtx operands[4])
30199 unsigned int i;
30200 rtx perm[16];
30201 rtx constv, unspec;
30202 rtx target = operands[0];
30203 rtx op0 = operands[1];
30204 rtx op1 = operands[2];
30205 rtx sel = operands[3];
30207 /* Unpack and adjust the constant selector. */
30208 for (i = 0; i < 16; ++i)
30210 rtx e = XVECEXP (sel, 0, i);
30211 unsigned int elt = 31 - (INTVAL (e) & 31);
30212 perm[i] = GEN_INT (elt);
30215 /* Expand to a permute, swapping the inputs and using the
30216 adjusted selector. */
30217 if (!REG_P (op0))
30218 op0 = force_reg (V16QImode, op0);
30219 if (!REG_P (op1))
30220 op1 = force_reg (V16QImode, op1);
30222 constv = gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, perm));
30223 constv = force_reg (V16QImode, constv);
30224 unspec = gen_rtx_UNSPEC (V16QImode, gen_rtvec (3, op1, op0, constv),
30225 UNSPEC_VPERM);
30226 if (!REG_P (target))
30228 rtx tmp = gen_reg_rtx (V16QImode);
30229 emit_move_insn (tmp, unspec);
30230 unspec = tmp;
30233 emit_move_insn (target, unspec);
30236 /* Similarly to altivec_expand_vec_perm_const_le, we must adjust the
30237 permute control vector. But here it's not a constant, so we must
30238 generate a vector NAND or NOR to do the adjustment. */
30240 void
30241 altivec_expand_vec_perm_le (rtx operands[4])
30243 rtx notx, iorx, unspec;
30244 rtx target = operands[0];
30245 rtx op0 = operands[1];
30246 rtx op1 = operands[2];
30247 rtx sel = operands[3];
30248 rtx tmp = target;
30249 rtx norreg = gen_reg_rtx (V16QImode);
30250 enum machine_mode mode = GET_MODE (target);
30252 /* Get everything in regs so the pattern matches. */
30253 if (!REG_P (op0))
30254 op0 = force_reg (mode, op0);
30255 if (!REG_P (op1))
30256 op1 = force_reg (mode, op1);
30257 if (!REG_P (sel))
30258 sel = force_reg (V16QImode, sel);
30259 if (!REG_P (target))
30260 tmp = gen_reg_rtx (mode);
30262 /* Invert the selector with a VNAND if available, else a VNOR.
30263 The VNAND is preferred for future fusion opportunities. */
30264 notx = gen_rtx_NOT (V16QImode, sel);
30265 iorx = (TARGET_P8_VECTOR
30266 ? gen_rtx_IOR (V16QImode, notx, notx)
30267 : gen_rtx_AND (V16QImode, notx, notx));
30268 emit_insn (gen_rtx_SET (VOIDmode, norreg, iorx));
30270 /* Permute with operands reversed and adjusted selector. */
30271 unspec = gen_rtx_UNSPEC (mode, gen_rtvec (3, op1, op0, norreg),
30272 UNSPEC_VPERM);
30274 /* Copy into target, possibly by way of a register. */
30275 if (!REG_P (target))
30277 emit_move_insn (tmp, unspec);
30278 unspec = tmp;
30281 emit_move_insn (target, unspec);
30284 /* Expand an Altivec constant permutation. Return true if we match
30285 an efficient implementation; false to fall back to VPERM. */
30287 bool
30288 altivec_expand_vec_perm_const (rtx operands[4])
30290 struct altivec_perm_insn {
30291 HOST_WIDE_INT mask;
30292 enum insn_code impl;
30293 unsigned char perm[16];
30295 static const struct altivec_perm_insn patterns[] = {
30296 { OPTION_MASK_ALTIVEC, CODE_FOR_altivec_vpkuhum_direct,
30297 { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } },
30298 { OPTION_MASK_ALTIVEC, CODE_FOR_altivec_vpkuwum_direct,
30299 { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } },
30300 { OPTION_MASK_ALTIVEC,
30301 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrghb_direct
30302 : CODE_FOR_altivec_vmrglb_direct),
30303 { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 } },
30304 { OPTION_MASK_ALTIVEC,
30305 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrghh_direct
30306 : CODE_FOR_altivec_vmrglh_direct),
30307 { 0, 1, 16, 17, 2, 3, 18, 19, 4, 5, 20, 21, 6, 7, 22, 23 } },
30308 { OPTION_MASK_ALTIVEC,
30309 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrghw_direct
30310 : CODE_FOR_altivec_vmrglw_direct),
30311 { 0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23 } },
30312 { OPTION_MASK_ALTIVEC,
30313 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrglb_direct
30314 : CODE_FOR_altivec_vmrghb_direct),
30315 { 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } },
30316 { OPTION_MASK_ALTIVEC,
30317 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrglh_direct
30318 : CODE_FOR_altivec_vmrghh_direct),
30319 { 8, 9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } },
30320 { OPTION_MASK_ALTIVEC,
30321 (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrglw_direct
30322 : CODE_FOR_altivec_vmrghw_direct),
30323 { 8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } },
30324 { OPTION_MASK_P8_VECTOR, CODE_FOR_p8_vmrgew,
30325 { 0, 1, 2, 3, 16, 17, 18, 19, 8, 9, 10, 11, 24, 25, 26, 27 } },
30326 { OPTION_MASK_P8_VECTOR, CODE_FOR_p8_vmrgow,
30327 { 4, 5, 6, 7, 20, 21, 22, 23, 12, 13, 14, 15, 28, 29, 30, 31 } }
30330 unsigned int i, j, elt, which;
30331 unsigned char perm[16];
30332 rtx target, op0, op1, sel, x;
30333 bool one_vec;
30335 target = operands[0];
30336 op0 = operands[1];
30337 op1 = operands[2];
30338 sel = operands[3];
30340 /* Unpack the constant selector. */
30341 for (i = which = 0; i < 16; ++i)
30343 rtx e = XVECEXP (sel, 0, i);
30344 elt = INTVAL (e) & 31;
30345 which |= (elt < 16 ? 1 : 2);
30346 perm[i] = elt;
30349 /* Simplify the constant selector based on operands. */
30350 switch (which)
30352 default:
30353 gcc_unreachable ();
30355 case 3:
30356 one_vec = false;
30357 if (!rtx_equal_p (op0, op1))
30358 break;
30359 /* FALLTHRU */
30361 case 2:
30362 for (i = 0; i < 16; ++i)
30363 perm[i] &= 15;
30364 op0 = op1;
30365 one_vec = true;
30366 break;
30368 case 1:
30369 op1 = op0;
30370 one_vec = true;
30371 break;
30374 /* Look for splat patterns. */
30375 if (one_vec)
30377 elt = perm[0];
30379 for (i = 0; i < 16; ++i)
30380 if (perm[i] != elt)
30381 break;
30382 if (i == 16)
30384 if (!BYTES_BIG_ENDIAN)
30385 elt = 15 - elt;
30386 emit_insn (gen_altivec_vspltb_direct (target, op0, GEN_INT (elt)));
30387 return true;
30390 if (elt % 2 == 0)
30392 for (i = 0; i < 16; i += 2)
30393 if (perm[i] != elt || perm[i + 1] != elt + 1)
30394 break;
30395 if (i == 16)
30397 int field = BYTES_BIG_ENDIAN ? elt / 2 : 7 - elt / 2;
30398 x = gen_reg_rtx (V8HImode);
30399 emit_insn (gen_altivec_vsplth_direct (x, gen_lowpart (V8HImode, op0),
30400 GEN_INT (field)));
30401 emit_move_insn (target, gen_lowpart (V16QImode, x));
30402 return true;
30406 if (elt % 4 == 0)
30408 for (i = 0; i < 16; i += 4)
30409 if (perm[i] != elt
30410 || perm[i + 1] != elt + 1
30411 || perm[i + 2] != elt + 2
30412 || perm[i + 3] != elt + 3)
30413 break;
30414 if (i == 16)
30416 int field = BYTES_BIG_ENDIAN ? elt / 4 : 3 - elt / 4;
30417 x = gen_reg_rtx (V4SImode);
30418 emit_insn (gen_altivec_vspltw_direct (x, gen_lowpart (V4SImode, op0),
30419 GEN_INT (field)));
30420 emit_move_insn (target, gen_lowpart (V16QImode, x));
30421 return true;
30426 /* Look for merge and pack patterns. */
30427 for (j = 0; j < ARRAY_SIZE (patterns); ++j)
30429 bool swapped;
30431 if ((patterns[j].mask & rs6000_isa_flags) == 0)
30432 continue;
30434 elt = patterns[j].perm[0];
30435 if (perm[0] == elt)
30436 swapped = false;
30437 else if (perm[0] == elt + 16)
30438 swapped = true;
30439 else
30440 continue;
30441 for (i = 1; i < 16; ++i)
30443 elt = patterns[j].perm[i];
30444 if (swapped)
30445 elt = (elt >= 16 ? elt - 16 : elt + 16);
30446 else if (one_vec && elt >= 16)
30447 elt -= 16;
30448 if (perm[i] != elt)
30449 break;
30451 if (i == 16)
30453 enum insn_code icode = patterns[j].impl;
30454 enum machine_mode omode = insn_data[icode].operand[0].mode;
30455 enum machine_mode imode = insn_data[icode].operand[1].mode;
30457 /* For little-endian, don't use vpkuwum and vpkuhum if the
30458 underlying vector type is not V4SI and V8HI, respectively.
30459 For example, using vpkuwum with a V8HI picks up the even
30460 halfwords (BE numbering) when the even halfwords (LE
30461 numbering) are what we need. */
30462 if (!BYTES_BIG_ENDIAN
30463 && icode == CODE_FOR_altivec_vpkuwum_direct
30464 && ((GET_CODE (op0) == REG
30465 && GET_MODE (op0) != V4SImode)
30466 || (GET_CODE (op0) == SUBREG
30467 && GET_MODE (XEXP (op0, 0)) != V4SImode)))
30468 continue;
30469 if (!BYTES_BIG_ENDIAN
30470 && icode == CODE_FOR_altivec_vpkuhum_direct
30471 && ((GET_CODE (op0) == REG
30472 && GET_MODE (op0) != V8HImode)
30473 || (GET_CODE (op0) == SUBREG
30474 && GET_MODE (XEXP (op0, 0)) != V8HImode)))
30475 continue;
30477 /* For little-endian, the two input operands must be swapped
30478 (or swapped back) to ensure proper right-to-left numbering
30479 from 0 to 2N-1. */
30480 if (swapped ^ !BYTES_BIG_ENDIAN)
30481 x = op0, op0 = op1, op1 = x;
30482 if (imode != V16QImode)
30484 op0 = gen_lowpart (imode, op0);
30485 op1 = gen_lowpart (imode, op1);
30487 if (omode == V16QImode)
30488 x = target;
30489 else
30490 x = gen_reg_rtx (omode);
30491 emit_insn (GEN_FCN (icode) (x, op0, op1));
30492 if (omode != V16QImode)
30493 emit_move_insn (target, gen_lowpart (V16QImode, x));
30494 return true;
30498 if (!BYTES_BIG_ENDIAN)
30500 altivec_expand_vec_perm_const_le (operands);
30501 return true;
30504 return false;
30507 /* Expand a Paired Single, VSX Permute Doubleword, or SPE constant permutation.
30508 Return true if we match an efficient implementation. */
30510 static bool
30511 rs6000_expand_vec_perm_const_1 (rtx target, rtx op0, rtx op1,
30512 unsigned char perm0, unsigned char perm1)
30514 rtx x;
30516 /* If both selectors come from the same operand, fold to single op. */
30517 if ((perm0 & 2) == (perm1 & 2))
30519 if (perm0 & 2)
30520 op0 = op1;
30521 else
30522 op1 = op0;
30524 /* If both operands are equal, fold to simpler permutation. */
30525 if (rtx_equal_p (op0, op1))
30527 perm0 = perm0 & 1;
30528 perm1 = (perm1 & 1) + 2;
30530 /* If the first selector comes from the second operand, swap. */
30531 else if (perm0 & 2)
30533 if (perm1 & 2)
30534 return false;
30535 perm0 -= 2;
30536 perm1 += 2;
30537 x = op0, op0 = op1, op1 = x;
30539 /* If the second selector does not come from the second operand, fail. */
30540 else if ((perm1 & 2) == 0)
30541 return false;
30543 /* Success! */
30544 if (target != NULL)
30546 enum machine_mode vmode, dmode;
30547 rtvec v;
30549 vmode = GET_MODE (target);
30550 gcc_assert (GET_MODE_NUNITS (vmode) == 2);
30551 dmode = mode_for_vector (GET_MODE_INNER (vmode), 4);
30552 x = gen_rtx_VEC_CONCAT (dmode, op0, op1);
30553 v = gen_rtvec (2, GEN_INT (perm0), GEN_INT (perm1));
30554 x = gen_rtx_VEC_SELECT (vmode, x, gen_rtx_PARALLEL (VOIDmode, v));
30555 emit_insn (gen_rtx_SET (VOIDmode, target, x));
30557 return true;
30560 bool
30561 rs6000_expand_vec_perm_const (rtx operands[4])
30563 rtx target, op0, op1, sel;
30564 unsigned char perm0, perm1;
30566 target = operands[0];
30567 op0 = operands[1];
30568 op1 = operands[2];
30569 sel = operands[3];
30571 /* Unpack the constant selector. */
30572 perm0 = INTVAL (XVECEXP (sel, 0, 0)) & 3;
30573 perm1 = INTVAL (XVECEXP (sel, 0, 1)) & 3;
30575 return rs6000_expand_vec_perm_const_1 (target, op0, op1, perm0, perm1);
30578 /* Test whether a constant permutation is supported. */
30580 static bool
30581 rs6000_vectorize_vec_perm_const_ok (enum machine_mode vmode,
30582 const unsigned char *sel)
30584 /* AltiVec (and thus VSX) can handle arbitrary permutations. */
30585 if (TARGET_ALTIVEC)
30586 return true;
30588 /* Check for ps_merge* or evmerge* insns. */
30589 if ((TARGET_PAIRED_FLOAT && vmode == V2SFmode)
30590 || (TARGET_SPE && vmode == V2SImode))
30592 rtx op0 = gen_raw_REG (vmode, LAST_VIRTUAL_REGISTER + 1);
30593 rtx op1 = gen_raw_REG (vmode, LAST_VIRTUAL_REGISTER + 2);
30594 return rs6000_expand_vec_perm_const_1 (NULL, op0, op1, sel[0], sel[1]);
30597 return false;
30600 /* A subroutine for rs6000_expand_extract_even & rs6000_expand_interleave. */
30602 static void
30603 rs6000_do_expand_vec_perm (rtx target, rtx op0, rtx op1,
30604 enum machine_mode vmode, unsigned nelt, rtx perm[])
30606 enum machine_mode imode;
30607 rtx x;
30609 imode = vmode;
30610 if (GET_MODE_CLASS (vmode) != MODE_VECTOR_INT)
30612 imode = GET_MODE_INNER (vmode);
30613 imode = mode_for_size (GET_MODE_BITSIZE (imode), MODE_INT, 0);
30614 imode = mode_for_vector (imode, nelt);
30617 x = gen_rtx_CONST_VECTOR (imode, gen_rtvec_v (nelt, perm));
30618 x = expand_vec_perm (vmode, op0, op1, x, target);
30619 if (x != target)
30620 emit_move_insn (target, x);
30623 /* Expand an extract even operation. */
30625 void
30626 rs6000_expand_extract_even (rtx target, rtx op0, rtx op1)
30628 enum machine_mode vmode = GET_MODE (target);
30629 unsigned i, nelt = GET_MODE_NUNITS (vmode);
30630 rtx perm[16];
30632 for (i = 0; i < nelt; i++)
30633 perm[i] = GEN_INT (i * 2);
30635 rs6000_do_expand_vec_perm (target, op0, op1, vmode, nelt, perm);
30638 /* Expand a vector interleave operation. */
30640 void
30641 rs6000_expand_interleave (rtx target, rtx op0, rtx op1, bool highp)
30643 enum machine_mode vmode = GET_MODE (target);
30644 unsigned i, high, nelt = GET_MODE_NUNITS (vmode);
30645 rtx perm[16];
30647 high = (highp ? 0 : nelt / 2);
30648 for (i = 0; i < nelt / 2; i++)
30650 perm[i * 2] = GEN_INT (i + high);
30651 perm[i * 2 + 1] = GEN_INT (i + nelt + high);
30654 rs6000_do_expand_vec_perm (target, op0, op1, vmode, nelt, perm);
30657 /* Return an RTX representing where to find the function value of a
30658 function returning MODE. */
30659 static rtx
30660 rs6000_complex_function_value (enum machine_mode mode)
30662 unsigned int regno;
30663 rtx r1, r2;
30664 enum machine_mode inner = GET_MODE_INNER (mode);
30665 unsigned int inner_bytes = GET_MODE_SIZE (inner);
30667 if (FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
30668 regno = FP_ARG_RETURN;
30669 else
30671 regno = GP_ARG_RETURN;
30673 /* 32-bit is OK since it'll go in r3/r4. */
30674 if (TARGET_32BIT && inner_bytes >= 4)
30675 return gen_rtx_REG (mode, regno);
30678 if (inner_bytes >= 8)
30679 return gen_rtx_REG (mode, regno);
30681 r1 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno),
30682 const0_rtx);
30683 r2 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno + 1),
30684 GEN_INT (inner_bytes));
30685 return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
30688 /* Target hook for TARGET_FUNCTION_VALUE.
30690 On the SPE, both FPs and vectors are returned in r3.
30692 On RS/6000 an integer value is in r3 and a floating-point value is in
30693 fp1, unless -msoft-float. */
30695 static rtx
30696 rs6000_function_value (const_tree valtype,
30697 const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
30698 bool outgoing ATTRIBUTE_UNUSED)
30700 enum machine_mode mode;
30701 unsigned int regno;
30702 enum machine_mode elt_mode;
30703 int n_elts;
30705 /* Special handling for structs in darwin64. */
30706 if (TARGET_MACHO
30707 && rs6000_darwin64_struct_check_p (TYPE_MODE (valtype), valtype))
30709 CUMULATIVE_ARGS valcum;
30710 rtx valret;
30712 valcum.words = 0;
30713 valcum.fregno = FP_ARG_MIN_REG;
30714 valcum.vregno = ALTIVEC_ARG_MIN_REG;
30715 /* Do a trial code generation as if this were going to be passed as
30716 an argument; if any part goes in memory, we return NULL. */
30717 valret = rs6000_darwin64_record_arg (&valcum, valtype, true, /* retval= */ true);
30718 if (valret)
30719 return valret;
30720 /* Otherwise fall through to standard ABI rules. */
30723 /* The ELFv2 ABI returns homogeneous VFP aggregates in registers. */
30724 if (rs6000_discover_homogeneous_aggregate (TYPE_MODE (valtype), valtype,
30725 &elt_mode, &n_elts))
30727 int first_reg, n_regs, i;
30728 rtx par;
30730 if (SCALAR_FLOAT_MODE_P (elt_mode))
30732 /* _Decimal128 must use even/odd register pairs. */
30733 first_reg = (elt_mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
30734 n_regs = (GET_MODE_SIZE (elt_mode) + 7) >> 3;
30736 else
30738 first_reg = ALTIVEC_ARG_RETURN;
30739 n_regs = 1;
30742 par = gen_rtx_PARALLEL (TYPE_MODE (valtype), rtvec_alloc (n_elts));
30743 for (i = 0; i < n_elts; i++)
30745 rtx r = gen_rtx_REG (elt_mode, first_reg + i * n_regs);
30746 rtx off = GEN_INT (i * GET_MODE_SIZE (elt_mode));
30747 XVECEXP (par, 0, i) = gen_rtx_EXPR_LIST (VOIDmode, r, off);
30750 return par;
30753 if (TARGET_32BIT && TARGET_POWERPC64 && TYPE_MODE (valtype) == DImode)
30755 /* Long long return value need be split in -mpowerpc64, 32bit ABI. */
30756 return gen_rtx_PARALLEL (DImode,
30757 gen_rtvec (2,
30758 gen_rtx_EXPR_LIST (VOIDmode,
30759 gen_rtx_REG (SImode, GP_ARG_RETURN),
30760 const0_rtx),
30761 gen_rtx_EXPR_LIST (VOIDmode,
30762 gen_rtx_REG (SImode,
30763 GP_ARG_RETURN + 1),
30764 GEN_INT (4))));
30766 if (TARGET_32BIT && TARGET_POWERPC64 && TYPE_MODE (valtype) == DCmode)
30768 return gen_rtx_PARALLEL (DCmode,
30769 gen_rtvec (4,
30770 gen_rtx_EXPR_LIST (VOIDmode,
30771 gen_rtx_REG (SImode, GP_ARG_RETURN),
30772 const0_rtx),
30773 gen_rtx_EXPR_LIST (VOIDmode,
30774 gen_rtx_REG (SImode,
30775 GP_ARG_RETURN + 1),
30776 GEN_INT (4)),
30777 gen_rtx_EXPR_LIST (VOIDmode,
30778 gen_rtx_REG (SImode,
30779 GP_ARG_RETURN + 2),
30780 GEN_INT (8)),
30781 gen_rtx_EXPR_LIST (VOIDmode,
30782 gen_rtx_REG (SImode,
30783 GP_ARG_RETURN + 3),
30784 GEN_INT (12))));
30787 mode = TYPE_MODE (valtype);
30788 if ((INTEGRAL_TYPE_P (valtype) && GET_MODE_BITSIZE (mode) < BITS_PER_WORD)
30789 || POINTER_TYPE_P (valtype))
30790 mode = TARGET_32BIT ? SImode : DImode;
30792 if (DECIMAL_FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
30793 /* _Decimal128 must use an even/odd register pair. */
30794 regno = (mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
30795 else if (SCALAR_FLOAT_TYPE_P (valtype) && TARGET_HARD_FLOAT && TARGET_FPRS
30796 && ((TARGET_SINGLE_FLOAT && (mode == SFmode)) || TARGET_DOUBLE_FLOAT))
30797 regno = FP_ARG_RETURN;
30798 else if (TREE_CODE (valtype) == COMPLEX_TYPE
30799 && targetm.calls.split_complex_arg)
30800 return rs6000_complex_function_value (mode);
30801 /* VSX is a superset of Altivec and adds V2DImode/V2DFmode. Since the same
30802 return register is used in both cases, and we won't see V2DImode/V2DFmode
30803 for pure altivec, combine the two cases. */
30804 else if (TREE_CODE (valtype) == VECTOR_TYPE
30805 && TARGET_ALTIVEC && TARGET_ALTIVEC_ABI
30806 && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
30807 regno = ALTIVEC_ARG_RETURN;
30808 else if (TARGET_E500_DOUBLE && TARGET_HARD_FLOAT
30809 && (mode == DFmode || mode == DCmode
30810 || mode == TFmode || mode == TCmode))
30811 return spe_build_register_parallel (mode, GP_ARG_RETURN);
30812 else
30813 regno = GP_ARG_RETURN;
30815 return gen_rtx_REG (mode, regno);
30818 /* Define how to find the value returned by a library function
30819 assuming the value has mode MODE. */
30821 rs6000_libcall_value (enum machine_mode mode)
30823 unsigned int regno;
30825 if (TARGET_32BIT && TARGET_POWERPC64 && mode == DImode)
30827 /* Long long return value need be split in -mpowerpc64, 32bit ABI. */
30828 return gen_rtx_PARALLEL (DImode,
30829 gen_rtvec (2,
30830 gen_rtx_EXPR_LIST (VOIDmode,
30831 gen_rtx_REG (SImode, GP_ARG_RETURN),
30832 const0_rtx),
30833 gen_rtx_EXPR_LIST (VOIDmode,
30834 gen_rtx_REG (SImode,
30835 GP_ARG_RETURN + 1),
30836 GEN_INT (4))));
30839 if (DECIMAL_FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
30840 /* _Decimal128 must use an even/odd register pair. */
30841 regno = (mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
30842 else if (SCALAR_FLOAT_MODE_P (mode)
30843 && TARGET_HARD_FLOAT && TARGET_FPRS
30844 && ((TARGET_SINGLE_FLOAT && mode == SFmode) || TARGET_DOUBLE_FLOAT))
30845 regno = FP_ARG_RETURN;
30846 /* VSX is a superset of Altivec and adds V2DImode/V2DFmode. Since the same
30847 return register is used in both cases, and we won't see V2DImode/V2DFmode
30848 for pure altivec, combine the two cases. */
30849 else if (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
30850 && TARGET_ALTIVEC && TARGET_ALTIVEC_ABI)
30851 regno = ALTIVEC_ARG_RETURN;
30852 else if (COMPLEX_MODE_P (mode) && targetm.calls.split_complex_arg)
30853 return rs6000_complex_function_value (mode);
30854 else if (TARGET_E500_DOUBLE && TARGET_HARD_FLOAT
30855 && (mode == DFmode || mode == DCmode
30856 || mode == TFmode || mode == TCmode))
30857 return spe_build_register_parallel (mode, GP_ARG_RETURN);
30858 else
30859 regno = GP_ARG_RETURN;
30861 return gen_rtx_REG (mode, regno);
30865 /* Return true if we use LRA instead of reload pass. */
30866 static bool
30867 rs6000_lra_p (void)
30869 return rs6000_lra_flag;
30872 /* Given FROM and TO register numbers, say whether this elimination is allowed.
30873 Frame pointer elimination is automatically handled.
30875 For the RS/6000, if frame pointer elimination is being done, we would like
30876 to convert ap into fp, not sp.
30878 We need r30 if -mminimal-toc was specified, and there are constant pool
30879 references. */
30881 static bool
30882 rs6000_can_eliminate (const int from, const int to)
30884 return (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM
30885 ? ! frame_pointer_needed
30886 : from == RS6000_PIC_OFFSET_TABLE_REGNUM
30887 ? ! TARGET_MINIMAL_TOC || TARGET_NO_TOC || get_pool_size () == 0
30888 : true);
30891 /* Define the offset between two registers, FROM to be eliminated and its
30892 replacement TO, at the start of a routine. */
30893 HOST_WIDE_INT
30894 rs6000_initial_elimination_offset (int from, int to)
30896 rs6000_stack_t *info = rs6000_stack_info ();
30897 HOST_WIDE_INT offset;
30899 if (from == HARD_FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
30900 offset = info->push_p ? 0 : -info->total_size;
30901 else if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
30903 offset = info->push_p ? 0 : -info->total_size;
30904 if (FRAME_GROWS_DOWNWARD)
30905 offset += info->fixed_size + info->vars_size + info->parm_size;
30907 else if (from == FRAME_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
30908 offset = FRAME_GROWS_DOWNWARD
30909 ? info->fixed_size + info->vars_size + info->parm_size
30910 : 0;
30911 else if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
30912 offset = info->total_size;
30913 else if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
30914 offset = info->push_p ? info->total_size : 0;
30915 else if (from == RS6000_PIC_OFFSET_TABLE_REGNUM)
30916 offset = 0;
30917 else
30918 gcc_unreachable ();
30920 return offset;
30923 static rtx
30924 rs6000_dwarf_register_span (rtx reg)
30926 rtx parts[8];
30927 int i, words;
30928 unsigned regno = REGNO (reg);
30929 enum machine_mode mode = GET_MODE (reg);
30931 if (TARGET_SPE
30932 && regno < 32
30933 && (SPE_VECTOR_MODE (GET_MODE (reg))
30934 || (TARGET_E500_DOUBLE && FLOAT_MODE_P (mode)
30935 && mode != SFmode && mode != SDmode && mode != SCmode)))
30937 else
30938 return NULL_RTX;
30940 regno = REGNO (reg);
30942 /* The duality of the SPE register size wreaks all kinds of havoc.
30943 This is a way of distinguishing r0 in 32-bits from r0 in
30944 64-bits. */
30945 words = (GET_MODE_SIZE (mode) + UNITS_PER_FP_WORD - 1) / UNITS_PER_FP_WORD;
30946 gcc_assert (words <= 4);
30947 for (i = 0; i < words; i++, regno++)
30949 if (BYTES_BIG_ENDIAN)
30951 parts[2 * i] = gen_rtx_REG (SImode, regno + 1200);
30952 parts[2 * i + 1] = gen_rtx_REG (SImode, regno);
30954 else
30956 parts[2 * i] = gen_rtx_REG (SImode, regno);
30957 parts[2 * i + 1] = gen_rtx_REG (SImode, regno + 1200);
30961 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (words * 2, parts));
30964 /* Fill in sizes for SPE register high parts in table used by unwinder. */
30966 static void
30967 rs6000_init_dwarf_reg_sizes_extra (tree address)
30969 if (TARGET_SPE)
30971 int i;
30972 enum machine_mode mode = TYPE_MODE (char_type_node);
30973 rtx addr = expand_expr (address, NULL_RTX, VOIDmode, EXPAND_NORMAL);
30974 rtx mem = gen_rtx_MEM (BLKmode, addr);
30975 rtx value = gen_int_mode (4, mode);
30977 for (i = 1201; i < 1232; i++)
30979 int column = DWARF_REG_TO_UNWIND_COLUMN (i);
30980 HOST_WIDE_INT offset
30981 = DWARF_FRAME_REGNUM (column) * GET_MODE_SIZE (mode);
30983 emit_move_insn (adjust_address (mem, mode, offset), value);
30987 if (TARGET_MACHO && ! TARGET_ALTIVEC)
30989 int i;
30990 enum machine_mode mode = TYPE_MODE (char_type_node);
30991 rtx addr = expand_expr (address, NULL_RTX, VOIDmode, EXPAND_NORMAL);
30992 rtx mem = gen_rtx_MEM (BLKmode, addr);
30993 rtx value = gen_int_mode (16, mode);
30995 /* On Darwin, libgcc may be built to run on both G3 and G4/5.
30996 The unwinder still needs to know the size of Altivec registers. */
30998 for (i = FIRST_ALTIVEC_REGNO; i < LAST_ALTIVEC_REGNO+1; i++)
31000 int column = DWARF_REG_TO_UNWIND_COLUMN (i);
31001 HOST_WIDE_INT offset
31002 = DWARF_FRAME_REGNUM (column) * GET_MODE_SIZE (mode);
31004 emit_move_insn (adjust_address (mem, mode, offset), value);
31009 /* Map internal gcc register numbers to DWARF2 register numbers. */
31011 unsigned int
31012 rs6000_dbx_register_number (unsigned int regno)
31014 if (regno <= 63 || write_symbols != DWARF2_DEBUG)
31015 return regno;
31016 if (regno == LR_REGNO)
31017 return 108;
31018 if (regno == CTR_REGNO)
31019 return 109;
31020 if (CR_REGNO_P (regno))
31021 return regno - CR0_REGNO + 86;
31022 if (regno == CA_REGNO)
31023 return 101; /* XER */
31024 if (ALTIVEC_REGNO_P (regno))
31025 return regno - FIRST_ALTIVEC_REGNO + 1124;
31026 if (regno == VRSAVE_REGNO)
31027 return 356;
31028 if (regno == VSCR_REGNO)
31029 return 67;
31030 if (regno == SPE_ACC_REGNO)
31031 return 99;
31032 if (regno == SPEFSCR_REGNO)
31033 return 612;
31034 /* SPE high reg number. We get these values of regno from
31035 rs6000_dwarf_register_span. */
31036 gcc_assert (regno >= 1200 && regno < 1232);
31037 return regno;
31040 /* target hook eh_return_filter_mode */
31041 static enum machine_mode
31042 rs6000_eh_return_filter_mode (void)
31044 return TARGET_32BIT ? SImode : word_mode;
31047 /* Target hook for scalar_mode_supported_p. */
31048 static bool
31049 rs6000_scalar_mode_supported_p (enum machine_mode mode)
31051 if (DECIMAL_FLOAT_MODE_P (mode))
31052 return default_decimal_float_supported_p ();
31053 else
31054 return default_scalar_mode_supported_p (mode);
31057 /* Target hook for vector_mode_supported_p. */
31058 static bool
31059 rs6000_vector_mode_supported_p (enum machine_mode mode)
31062 if (TARGET_PAIRED_FLOAT && PAIRED_VECTOR_MODE (mode))
31063 return true;
31065 if (TARGET_SPE && SPE_VECTOR_MODE (mode))
31066 return true;
31068 else if (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode))
31069 return true;
31071 else
31072 return false;
31075 /* Target hook for invalid_arg_for_unprototyped_fn. */
31076 static const char *
31077 invalid_arg_for_unprototyped_fn (const_tree typelist, const_tree funcdecl, const_tree val)
31079 return (!rs6000_darwin64_abi
31080 && typelist == 0
31081 && TREE_CODE (TREE_TYPE (val)) == VECTOR_TYPE
31082 && (funcdecl == NULL_TREE
31083 || (TREE_CODE (funcdecl) == FUNCTION_DECL
31084 && DECL_BUILT_IN_CLASS (funcdecl) != BUILT_IN_MD)))
31085 ? N_("AltiVec argument passed to unprototyped function")
31086 : NULL;
31089 /* For TARGET_SECURE_PLT 32-bit PIC code we can save PIC register
31090 setup by using __stack_chk_fail_local hidden function instead of
31091 calling __stack_chk_fail directly. Otherwise it is better to call
31092 __stack_chk_fail directly. */
31094 static tree ATTRIBUTE_UNUSED
31095 rs6000_stack_protect_fail (void)
31097 return (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
31098 ? default_hidden_stack_protect_fail ()
31099 : default_external_stack_protect_fail ();
31102 void
31103 rs6000_final_prescan_insn (rtx insn, rtx *operand ATTRIBUTE_UNUSED,
31104 int num_operands ATTRIBUTE_UNUSED)
31106 if (rs6000_warn_cell_microcode)
31108 const char *temp;
31109 int insn_code_number = recog_memoized (insn);
31110 location_t location = INSN_LOCATION (insn);
31112 /* Punt on insns we cannot recognize. */
31113 if (insn_code_number < 0)
31114 return;
31116 temp = get_insn_template (insn_code_number, insn);
31118 if (get_attr_cell_micro (insn) == CELL_MICRO_ALWAYS)
31119 warning_at (location, OPT_mwarn_cell_microcode,
31120 "emitting microcode insn %s\t[%s] #%d",
31121 temp, insn_data[INSN_CODE (insn)].name, INSN_UID (insn));
31122 else if (get_attr_cell_micro (insn) == CELL_MICRO_CONDITIONAL)
31123 warning_at (location, OPT_mwarn_cell_microcode,
31124 "emitting conditional microcode insn %s\t[%s] #%d",
31125 temp, insn_data[INSN_CODE (insn)].name, INSN_UID (insn));
31129 /* Implement the TARGET_ASAN_SHADOW_OFFSET hook. */
31131 #if TARGET_ELF
31132 static unsigned HOST_WIDE_INT
31133 rs6000_asan_shadow_offset (void)
31135 return (unsigned HOST_WIDE_INT) 1 << (TARGET_64BIT ? 41 : 29);
31137 #endif
31139 /* Mask options that we want to support inside of attribute((target)) and
31140 #pragma GCC target operations. Note, we do not include things like
31141 64/32-bit, endianess, hard/soft floating point, etc. that would have
31142 different calling sequences. */
31144 struct rs6000_opt_mask {
31145 const char *name; /* option name */
31146 HOST_WIDE_INT mask; /* mask to set */
31147 bool invert; /* invert sense of mask */
31148 bool valid_target; /* option is a target option */
31151 static struct rs6000_opt_mask const rs6000_opt_masks[] =
31153 { "altivec", OPTION_MASK_ALTIVEC, false, true },
31154 { "cmpb", OPTION_MASK_CMPB, false, true },
31155 { "crypto", OPTION_MASK_CRYPTO, false, true },
31156 { "direct-move", OPTION_MASK_DIRECT_MOVE, false, true },
31157 { "dlmzb", OPTION_MASK_DLMZB, false, true },
31158 { "fprnd", OPTION_MASK_FPRND, false, true },
31159 { "hard-dfp", OPTION_MASK_DFP, false, true },
31160 { "htm", OPTION_MASK_HTM, false, true },
31161 { "isel", OPTION_MASK_ISEL, false, true },
31162 { "mfcrf", OPTION_MASK_MFCRF, false, true },
31163 { "mfpgpr", OPTION_MASK_MFPGPR, false, true },
31164 { "mulhw", OPTION_MASK_MULHW, false, true },
31165 { "multiple", OPTION_MASK_MULTIPLE, false, true },
31166 { "popcntb", OPTION_MASK_POPCNTB, false, true },
31167 { "popcntd", OPTION_MASK_POPCNTD, false, true },
31168 { "power8-fusion", OPTION_MASK_P8_FUSION, false, true },
31169 { "power8-fusion-sign", OPTION_MASK_P8_FUSION_SIGN, false, true },
31170 { "power8-vector", OPTION_MASK_P8_VECTOR, false, true },
31171 { "powerpc-gfxopt", OPTION_MASK_PPC_GFXOPT, false, true },
31172 { "powerpc-gpopt", OPTION_MASK_PPC_GPOPT, false, true },
31173 { "quad-memory", OPTION_MASK_QUAD_MEMORY, false, true },
31174 { "quad-memory-atomic", OPTION_MASK_QUAD_MEMORY_ATOMIC, false, true },
31175 { "recip-precision", OPTION_MASK_RECIP_PRECISION, false, true },
31176 { "string", OPTION_MASK_STRING, false, true },
31177 { "update", OPTION_MASK_NO_UPDATE, true , true },
31178 { "upper-regs-df", OPTION_MASK_UPPER_REGS_DF, false, false },
31179 { "upper-regs-sf", OPTION_MASK_UPPER_REGS_SF, false, false },
31180 { "vsx", OPTION_MASK_VSX, false, true },
31181 { "vsx-timode", OPTION_MASK_VSX_TIMODE, false, true },
31182 #ifdef OPTION_MASK_64BIT
31183 #if TARGET_AIX_OS
31184 { "aix64", OPTION_MASK_64BIT, false, false },
31185 { "aix32", OPTION_MASK_64BIT, true, false },
31186 #else
31187 { "64", OPTION_MASK_64BIT, false, false },
31188 { "32", OPTION_MASK_64BIT, true, false },
31189 #endif
31190 #endif
31191 #ifdef OPTION_MASK_EABI
31192 { "eabi", OPTION_MASK_EABI, false, false },
31193 #endif
31194 #ifdef OPTION_MASK_LITTLE_ENDIAN
31195 { "little", OPTION_MASK_LITTLE_ENDIAN, false, false },
31196 { "big", OPTION_MASK_LITTLE_ENDIAN, true, false },
31197 #endif
31198 #ifdef OPTION_MASK_RELOCATABLE
31199 { "relocatable", OPTION_MASK_RELOCATABLE, false, false },
31200 #endif
31201 #ifdef OPTION_MASK_STRICT_ALIGN
31202 { "strict-align", OPTION_MASK_STRICT_ALIGN, false, false },
31203 #endif
31204 { "soft-float", OPTION_MASK_SOFT_FLOAT, false, false },
31205 { "string", OPTION_MASK_STRING, false, false },
31208 /* Builtin mask mapping for printing the flags. */
31209 static struct rs6000_opt_mask const rs6000_builtin_mask_names[] =
31211 { "altivec", RS6000_BTM_ALTIVEC, false, false },
31212 { "vsx", RS6000_BTM_VSX, false, false },
31213 { "spe", RS6000_BTM_SPE, false, false },
31214 { "paired", RS6000_BTM_PAIRED, false, false },
31215 { "fre", RS6000_BTM_FRE, false, false },
31216 { "fres", RS6000_BTM_FRES, false, false },
31217 { "frsqrte", RS6000_BTM_FRSQRTE, false, false },
31218 { "frsqrtes", RS6000_BTM_FRSQRTES, false, false },
31219 { "popcntd", RS6000_BTM_POPCNTD, false, false },
31220 { "cell", RS6000_BTM_CELL, false, false },
31221 { "power8-vector", RS6000_BTM_P8_VECTOR, false, false },
31222 { "crypto", RS6000_BTM_CRYPTO, false, false },
31223 { "htm", RS6000_BTM_HTM, false, false },
31226 /* Option variables that we want to support inside attribute((target)) and
31227 #pragma GCC target operations. */
31229 struct rs6000_opt_var {
31230 const char *name; /* option name */
31231 size_t global_offset; /* offset of the option in global_options. */
31232 size_t target_offset; /* offset of the option in target optiosn. */
31235 static struct rs6000_opt_var const rs6000_opt_vars[] =
31237 { "friz",
31238 offsetof (struct gcc_options, x_TARGET_FRIZ),
31239 offsetof (struct cl_target_option, x_TARGET_FRIZ), },
31240 { "avoid-indexed-addresses",
31241 offsetof (struct gcc_options, x_TARGET_AVOID_XFORM),
31242 offsetof (struct cl_target_option, x_TARGET_AVOID_XFORM) },
31243 { "paired",
31244 offsetof (struct gcc_options, x_rs6000_paired_float),
31245 offsetof (struct cl_target_option, x_rs6000_paired_float), },
31246 { "longcall",
31247 offsetof (struct gcc_options, x_rs6000_default_long_calls),
31248 offsetof (struct cl_target_option, x_rs6000_default_long_calls), },
31251 /* Inner function to handle attribute((target("..."))) and #pragma GCC target
31252 parsing. Return true if there were no errors. */
31254 static bool
31255 rs6000_inner_target_options (tree args, bool attr_p)
31257 bool ret = true;
31259 if (args == NULL_TREE)
31262 else if (TREE_CODE (args) == STRING_CST)
31264 char *p = ASTRDUP (TREE_STRING_POINTER (args));
31265 char *q;
31267 while ((q = strtok (p, ",")) != NULL)
31269 bool error_p = false;
31270 bool not_valid_p = false;
31271 const char *cpu_opt = NULL;
31273 p = NULL;
31274 if (strncmp (q, "cpu=", 4) == 0)
31276 int cpu_index = rs6000_cpu_name_lookup (q+4);
31277 if (cpu_index >= 0)
31278 rs6000_cpu_index = cpu_index;
31279 else
31281 error_p = true;
31282 cpu_opt = q+4;
31285 else if (strncmp (q, "tune=", 5) == 0)
31287 int tune_index = rs6000_cpu_name_lookup (q+5);
31288 if (tune_index >= 0)
31289 rs6000_tune_index = tune_index;
31290 else
31292 error_p = true;
31293 cpu_opt = q+5;
31296 else
31298 size_t i;
31299 bool invert = false;
31300 char *r = q;
31302 error_p = true;
31303 if (strncmp (r, "no-", 3) == 0)
31305 invert = true;
31306 r += 3;
31309 for (i = 0; i < ARRAY_SIZE (rs6000_opt_masks); i++)
31310 if (strcmp (r, rs6000_opt_masks[i].name) == 0)
31312 HOST_WIDE_INT mask = rs6000_opt_masks[i].mask;
31314 if (!rs6000_opt_masks[i].valid_target)
31315 not_valid_p = true;
31316 else
31318 error_p = false;
31319 rs6000_isa_flags_explicit |= mask;
31321 /* VSX needs altivec, so -mvsx automagically sets
31322 altivec. */
31323 if (mask == OPTION_MASK_VSX && !invert)
31324 mask |= OPTION_MASK_ALTIVEC;
31326 if (rs6000_opt_masks[i].invert)
31327 invert = !invert;
31329 if (invert)
31330 rs6000_isa_flags &= ~mask;
31331 else
31332 rs6000_isa_flags |= mask;
31334 break;
31337 if (error_p && !not_valid_p)
31339 for (i = 0; i < ARRAY_SIZE (rs6000_opt_vars); i++)
31340 if (strcmp (r, rs6000_opt_vars[i].name) == 0)
31342 size_t j = rs6000_opt_vars[i].global_offset;
31343 *((int *) ((char *)&global_options + j)) = !invert;
31344 error_p = false;
31345 break;
31350 if (error_p)
31352 const char *eprefix, *esuffix;
31354 ret = false;
31355 if (attr_p)
31357 eprefix = "__attribute__((__target__(";
31358 esuffix = ")))";
31360 else
31362 eprefix = "#pragma GCC target ";
31363 esuffix = "";
31366 if (cpu_opt)
31367 error ("invalid cpu \"%s\" for %s\"%s\"%s", cpu_opt, eprefix,
31368 q, esuffix);
31369 else if (not_valid_p)
31370 error ("%s\"%s\"%s is not allowed", eprefix, q, esuffix);
31371 else
31372 error ("%s\"%s\"%s is invalid", eprefix, q, esuffix);
31377 else if (TREE_CODE (args) == TREE_LIST)
31381 tree value = TREE_VALUE (args);
31382 if (value)
31384 bool ret2 = rs6000_inner_target_options (value, attr_p);
31385 if (!ret2)
31386 ret = false;
31388 args = TREE_CHAIN (args);
31390 while (args != NULL_TREE);
31393 else
31394 gcc_unreachable ();
31396 return ret;
31399 /* Print out the target options as a list for -mdebug=target. */
31401 static void
31402 rs6000_debug_target_options (tree args, const char *prefix)
31404 if (args == NULL_TREE)
31405 fprintf (stderr, "%s<NULL>", prefix);
31407 else if (TREE_CODE (args) == STRING_CST)
31409 char *p = ASTRDUP (TREE_STRING_POINTER (args));
31410 char *q;
31412 while ((q = strtok (p, ",")) != NULL)
31414 p = NULL;
31415 fprintf (stderr, "%s\"%s\"", prefix, q);
31416 prefix = ", ";
31420 else if (TREE_CODE (args) == TREE_LIST)
31424 tree value = TREE_VALUE (args);
31425 if (value)
31427 rs6000_debug_target_options (value, prefix);
31428 prefix = ", ";
31430 args = TREE_CHAIN (args);
31432 while (args != NULL_TREE);
31435 else
31436 gcc_unreachable ();
31438 return;
31442 /* Hook to validate attribute((target("..."))). */
31444 static bool
31445 rs6000_valid_attribute_p (tree fndecl,
31446 tree ARG_UNUSED (name),
31447 tree args,
31448 int flags)
31450 struct cl_target_option cur_target;
31451 bool ret;
31452 tree old_optimize = build_optimization_node (&global_options);
31453 tree new_target, new_optimize;
31454 tree func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
31456 gcc_assert ((fndecl != NULL_TREE) && (args != NULL_TREE));
31458 if (TARGET_DEBUG_TARGET)
31460 tree tname = DECL_NAME (fndecl);
31461 fprintf (stderr, "\n==================== rs6000_valid_attribute_p:\n");
31462 if (tname)
31463 fprintf (stderr, "function: %.*s\n",
31464 (int) IDENTIFIER_LENGTH (tname),
31465 IDENTIFIER_POINTER (tname));
31466 else
31467 fprintf (stderr, "function: unknown\n");
31469 fprintf (stderr, "args:");
31470 rs6000_debug_target_options (args, " ");
31471 fprintf (stderr, "\n");
31473 if (flags)
31474 fprintf (stderr, "flags: 0x%x\n", flags);
31476 fprintf (stderr, "--------------------\n");
31479 old_optimize = build_optimization_node (&global_options);
31480 func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
31482 /* If the function changed the optimization levels as well as setting target
31483 options, start with the optimizations specified. */
31484 if (func_optimize && func_optimize != old_optimize)
31485 cl_optimization_restore (&global_options,
31486 TREE_OPTIMIZATION (func_optimize));
31488 /* The target attributes may also change some optimization flags, so update
31489 the optimization options if necessary. */
31490 cl_target_option_save (&cur_target, &global_options);
31491 rs6000_cpu_index = rs6000_tune_index = -1;
31492 ret = rs6000_inner_target_options (args, true);
31494 /* Set up any additional state. */
31495 if (ret)
31497 ret = rs6000_option_override_internal (false);
31498 new_target = build_target_option_node (&global_options);
31500 else
31501 new_target = NULL;
31503 new_optimize = build_optimization_node (&global_options);
31505 if (!new_target)
31506 ret = false;
31508 else if (fndecl)
31510 DECL_FUNCTION_SPECIFIC_TARGET (fndecl) = new_target;
31512 if (old_optimize != new_optimize)
31513 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl) = new_optimize;
31516 cl_target_option_restore (&global_options, &cur_target);
31518 if (old_optimize != new_optimize)
31519 cl_optimization_restore (&global_options,
31520 TREE_OPTIMIZATION (old_optimize));
31522 return ret;
31526 /* Hook to validate the current #pragma GCC target and set the state, and
31527 update the macros based on what was changed. If ARGS is NULL, then
31528 POP_TARGET is used to reset the options. */
31530 bool
31531 rs6000_pragma_target_parse (tree args, tree pop_target)
31533 tree prev_tree = build_target_option_node (&global_options);
31534 tree cur_tree;
31535 struct cl_target_option *prev_opt, *cur_opt;
31536 HOST_WIDE_INT prev_flags, cur_flags, diff_flags;
31537 HOST_WIDE_INT prev_bumask, cur_bumask, diff_bumask;
31539 if (TARGET_DEBUG_TARGET)
31541 fprintf (stderr, "\n==================== rs6000_pragma_target_parse\n");
31542 fprintf (stderr, "args:");
31543 rs6000_debug_target_options (args, " ");
31544 fprintf (stderr, "\n");
31546 if (pop_target)
31548 fprintf (stderr, "pop_target:\n");
31549 debug_tree (pop_target);
31551 else
31552 fprintf (stderr, "pop_target: <NULL>\n");
31554 fprintf (stderr, "--------------------\n");
31557 if (! args)
31559 cur_tree = ((pop_target)
31560 ? pop_target
31561 : target_option_default_node);
31562 cl_target_option_restore (&global_options,
31563 TREE_TARGET_OPTION (cur_tree));
31565 else
31567 rs6000_cpu_index = rs6000_tune_index = -1;
31568 if (!rs6000_inner_target_options (args, false)
31569 || !rs6000_option_override_internal (false)
31570 || (cur_tree = build_target_option_node (&global_options))
31571 == NULL_TREE)
31573 if (TARGET_DEBUG_BUILTIN || TARGET_DEBUG_TARGET)
31574 fprintf (stderr, "invalid pragma\n");
31576 return false;
31580 target_option_current_node = cur_tree;
31582 /* If we have the preprocessor linked in (i.e. C or C++ languages), possibly
31583 change the macros that are defined. */
31584 if (rs6000_target_modify_macros_ptr)
31586 prev_opt = TREE_TARGET_OPTION (prev_tree);
31587 prev_bumask = prev_opt->x_rs6000_builtin_mask;
31588 prev_flags = prev_opt->x_rs6000_isa_flags;
31590 cur_opt = TREE_TARGET_OPTION (cur_tree);
31591 cur_flags = cur_opt->x_rs6000_isa_flags;
31592 cur_bumask = cur_opt->x_rs6000_builtin_mask;
31594 diff_bumask = (prev_bumask ^ cur_bumask);
31595 diff_flags = (prev_flags ^ cur_flags);
31597 if ((diff_flags != 0) || (diff_bumask != 0))
31599 /* Delete old macros. */
31600 rs6000_target_modify_macros_ptr (false,
31601 prev_flags & diff_flags,
31602 prev_bumask & diff_bumask);
31604 /* Define new macros. */
31605 rs6000_target_modify_macros_ptr (true,
31606 cur_flags & diff_flags,
31607 cur_bumask & diff_bumask);
31611 return true;
31615 /* Remember the last target of rs6000_set_current_function. */
31616 static GTY(()) tree rs6000_previous_fndecl;
31618 /* Establish appropriate back-end context for processing the function
31619 FNDECL. The argument might be NULL to indicate processing at top
31620 level, outside of any function scope. */
31621 static void
31622 rs6000_set_current_function (tree fndecl)
31624 tree old_tree = (rs6000_previous_fndecl
31625 ? DECL_FUNCTION_SPECIFIC_TARGET (rs6000_previous_fndecl)
31626 : NULL_TREE);
31628 tree new_tree = (fndecl
31629 ? DECL_FUNCTION_SPECIFIC_TARGET (fndecl)
31630 : NULL_TREE);
31632 if (TARGET_DEBUG_TARGET)
31634 bool print_final = false;
31635 fprintf (stderr, "\n==================== rs6000_set_current_function");
31637 if (fndecl)
31638 fprintf (stderr, ", fndecl %s (%p)",
31639 (DECL_NAME (fndecl)
31640 ? IDENTIFIER_POINTER (DECL_NAME (fndecl))
31641 : "<unknown>"), (void *)fndecl);
31643 if (rs6000_previous_fndecl)
31644 fprintf (stderr, ", prev_fndecl (%p)", (void *)rs6000_previous_fndecl);
31646 fprintf (stderr, "\n");
31647 if (new_tree)
31649 fprintf (stderr, "\nnew fndecl target specific options:\n");
31650 debug_tree (new_tree);
31651 print_final = true;
31654 if (old_tree)
31656 fprintf (stderr, "\nold fndecl target specific options:\n");
31657 debug_tree (old_tree);
31658 print_final = true;
31661 if (print_final)
31662 fprintf (stderr, "--------------------\n");
31665 /* Only change the context if the function changes. This hook is called
31666 several times in the course of compiling a function, and we don't want to
31667 slow things down too much or call target_reinit when it isn't safe. */
31668 if (fndecl && fndecl != rs6000_previous_fndecl)
31670 rs6000_previous_fndecl = fndecl;
31671 if (old_tree == new_tree)
31674 else if (new_tree)
31676 cl_target_option_restore (&global_options,
31677 TREE_TARGET_OPTION (new_tree));
31678 if (TREE_TARGET_GLOBALS (new_tree))
31679 restore_target_globals (TREE_TARGET_GLOBALS (new_tree));
31680 else
31681 TREE_TARGET_GLOBALS (new_tree)
31682 = save_target_globals_default_opts ();
31685 else if (old_tree)
31687 new_tree = target_option_current_node;
31688 cl_target_option_restore (&global_options,
31689 TREE_TARGET_OPTION (new_tree));
31690 if (TREE_TARGET_GLOBALS (new_tree))
31691 restore_target_globals (TREE_TARGET_GLOBALS (new_tree));
31692 else if (new_tree == target_option_default_node)
31693 restore_target_globals (&default_target_globals);
31694 else
31695 TREE_TARGET_GLOBALS (new_tree)
31696 = save_target_globals_default_opts ();
31702 /* Save the current options */
31704 static void
31705 rs6000_function_specific_save (struct cl_target_option *ptr,
31706 struct gcc_options *opts)
31708 ptr->x_rs6000_isa_flags = opts->x_rs6000_isa_flags;
31709 ptr->x_rs6000_isa_flags_explicit = opts->x_rs6000_isa_flags_explicit;
31712 /* Restore the current options */
31714 static void
31715 rs6000_function_specific_restore (struct gcc_options *opts,
31716 struct cl_target_option *ptr)
31719 opts->x_rs6000_isa_flags = ptr->x_rs6000_isa_flags;
31720 opts->x_rs6000_isa_flags_explicit = ptr->x_rs6000_isa_flags_explicit;
31721 (void) rs6000_option_override_internal (false);
31724 /* Print the current options */
31726 static void
31727 rs6000_function_specific_print (FILE *file, int indent,
31728 struct cl_target_option *ptr)
31730 rs6000_print_isa_options (file, indent, "Isa options set",
31731 ptr->x_rs6000_isa_flags);
31733 rs6000_print_isa_options (file, indent, "Isa options explicit",
31734 ptr->x_rs6000_isa_flags_explicit);
31737 /* Helper function to print the current isa or misc options on a line. */
31739 static void
31740 rs6000_print_options_internal (FILE *file,
31741 int indent,
31742 const char *string,
31743 HOST_WIDE_INT flags,
31744 const char *prefix,
31745 const struct rs6000_opt_mask *opts,
31746 size_t num_elements)
31748 size_t i;
31749 size_t start_column = 0;
31750 size_t cur_column;
31751 size_t max_column = 76;
31752 const char *comma = "";
31754 if (indent)
31755 start_column += fprintf (file, "%*s", indent, "");
31757 if (!flags)
31759 fprintf (stderr, DEBUG_FMT_S, string, "<none>");
31760 return;
31763 start_column += fprintf (stderr, DEBUG_FMT_WX, string, flags);
31765 /* Print the various mask options. */
31766 cur_column = start_column;
31767 for (i = 0; i < num_elements; i++)
31769 if ((flags & opts[i].mask) != 0)
31771 const char *no_str = rs6000_opt_masks[i].invert ? "no-" : "";
31772 size_t len = (strlen (comma)
31773 + strlen (prefix)
31774 + strlen (no_str)
31775 + strlen (rs6000_opt_masks[i].name));
31777 cur_column += len;
31778 if (cur_column > max_column)
31780 fprintf (stderr, ", \\\n%*s", (int)start_column, "");
31781 cur_column = start_column + len;
31782 comma = "";
31785 fprintf (file, "%s%s%s%s", comma, prefix, no_str,
31786 rs6000_opt_masks[i].name);
31787 flags &= ~ opts[i].mask;
31788 comma = ", ";
31792 fputs ("\n", file);
31795 /* Helper function to print the current isa options on a line. */
31797 static void
31798 rs6000_print_isa_options (FILE *file, int indent, const char *string,
31799 HOST_WIDE_INT flags)
31801 rs6000_print_options_internal (file, indent, string, flags, "-m",
31802 &rs6000_opt_masks[0],
31803 ARRAY_SIZE (rs6000_opt_masks));
31806 static void
31807 rs6000_print_builtin_options (FILE *file, int indent, const char *string,
31808 HOST_WIDE_INT flags)
31810 rs6000_print_options_internal (file, indent, string, flags, "",
31811 &rs6000_builtin_mask_names[0],
31812 ARRAY_SIZE (rs6000_builtin_mask_names));
31816 /* Hook to determine if one function can safely inline another. */
31818 static bool
31819 rs6000_can_inline_p (tree caller, tree callee)
31821 bool ret = false;
31822 tree caller_tree = DECL_FUNCTION_SPECIFIC_TARGET (caller);
31823 tree callee_tree = DECL_FUNCTION_SPECIFIC_TARGET (callee);
31825 /* If callee has no option attributes, then it is ok to inline. */
31826 if (!callee_tree)
31827 ret = true;
31829 /* If caller has no option attributes, but callee does then it is not ok to
31830 inline. */
31831 else if (!caller_tree)
31832 ret = false;
31834 else
31836 struct cl_target_option *caller_opts = TREE_TARGET_OPTION (caller_tree);
31837 struct cl_target_option *callee_opts = TREE_TARGET_OPTION (callee_tree);
31839 /* Callee's options should a subset of the caller's, i.e. a vsx function
31840 can inline an altivec function but a non-vsx function can't inline a
31841 vsx function. */
31842 if ((caller_opts->x_rs6000_isa_flags & callee_opts->x_rs6000_isa_flags)
31843 == callee_opts->x_rs6000_isa_flags)
31844 ret = true;
31847 if (TARGET_DEBUG_TARGET)
31848 fprintf (stderr, "rs6000_can_inline_p:, caller %s, callee %s, %s inline\n",
31849 (DECL_NAME (caller)
31850 ? IDENTIFIER_POINTER (DECL_NAME (caller))
31851 : "<unknown>"),
31852 (DECL_NAME (callee)
31853 ? IDENTIFIER_POINTER (DECL_NAME (callee))
31854 : "<unknown>"),
31855 (ret ? "can" : "cannot"));
31857 return ret;
31860 /* Allocate a stack temp and fixup the address so it meets the particular
31861 memory requirements (either offetable or REG+REG addressing). */
31864 rs6000_allocate_stack_temp (enum machine_mode mode,
31865 bool offsettable_p,
31866 bool reg_reg_p)
31868 rtx stack = assign_stack_temp (mode, GET_MODE_SIZE (mode));
31869 rtx addr = XEXP (stack, 0);
31870 int strict_p = (reload_in_progress || reload_completed);
31872 if (!legitimate_indirect_address_p (addr, strict_p))
31874 if (offsettable_p
31875 && !rs6000_legitimate_offset_address_p (mode, addr, strict_p, true))
31876 stack = replace_equiv_address (stack, copy_addr_to_reg (addr));
31878 else if (reg_reg_p && !legitimate_indexed_address_p (addr, strict_p))
31879 stack = replace_equiv_address (stack, copy_addr_to_reg (addr));
31882 return stack;
31885 /* Given a memory reference, if it is not a reg or reg+reg addressing, convert
31886 to such a form to deal with memory reference instructions like STFIWX that
31887 only take reg+reg addressing. */
31890 rs6000_address_for_fpconvert (rtx x)
31892 int strict_p = (reload_in_progress || reload_completed);
31893 rtx addr;
31895 gcc_assert (MEM_P (x));
31896 addr = XEXP (x, 0);
31897 if (! legitimate_indirect_address_p (addr, strict_p)
31898 && ! legitimate_indexed_address_p (addr, strict_p))
31900 if (GET_CODE (addr) == PRE_INC || GET_CODE (addr) == PRE_DEC)
31902 rtx reg = XEXP (addr, 0);
31903 HOST_WIDE_INT size = GET_MODE_SIZE (GET_MODE (x));
31904 rtx size_rtx = GEN_INT ((GET_CODE (addr) == PRE_DEC) ? -size : size);
31905 gcc_assert (REG_P (reg));
31906 emit_insn (gen_add3_insn (reg, reg, size_rtx));
31907 addr = reg;
31909 else if (GET_CODE (addr) == PRE_MODIFY)
31911 rtx reg = XEXP (addr, 0);
31912 rtx expr = XEXP (addr, 1);
31913 gcc_assert (REG_P (reg));
31914 gcc_assert (GET_CODE (expr) == PLUS);
31915 emit_insn (gen_add3_insn (reg, XEXP (expr, 0), XEXP (expr, 1)));
31916 addr = reg;
31919 x = replace_equiv_address (x, copy_addr_to_reg (addr));
31922 return x;
31925 /* Given a memory reference, if it is not in the form for altivec memory
31926 reference instructions (i.e. reg or reg+reg addressing with AND of -16),
31927 convert to the altivec format. */
31930 rs6000_address_for_altivec (rtx x)
31932 gcc_assert (MEM_P (x));
31933 if (!altivec_indexed_or_indirect_operand (x, GET_MODE (x)))
31935 rtx addr = XEXP (x, 0);
31936 int strict_p = (reload_in_progress || reload_completed);
31938 if (!legitimate_indexed_address_p (addr, strict_p)
31939 && !legitimate_indirect_address_p (addr, strict_p))
31940 addr = copy_to_mode_reg (Pmode, addr);
31942 addr = gen_rtx_AND (Pmode, addr, GEN_INT (-16));
31943 x = change_address (x, GET_MODE (x), addr);
31946 return x;
31949 /* Implement TARGET_LEGITIMATE_CONSTANT_P.
31951 On the RS/6000, all integer constants are acceptable, most won't be valid
31952 for particular insns, though. Only easy FP constants are acceptable. */
31954 static bool
31955 rs6000_legitimate_constant_p (enum machine_mode mode, rtx x)
31957 if (TARGET_ELF && rs6000_tls_referenced_p (x))
31958 return false;
31960 return ((GET_CODE (x) != CONST_DOUBLE && GET_CODE (x) != CONST_VECTOR)
31961 || GET_MODE (x) == VOIDmode
31962 || (TARGET_POWERPC64 && mode == DImode)
31963 || easy_fp_constant (x, mode)
31964 || easy_vector_constant (x, mode));
31969 /* Expand code to perform a call under the AIX or ELFv2 ABI. */
31971 void
31972 rs6000_call_aix (rtx value, rtx func_desc, rtx flag, rtx cookie)
31974 rtx toc_reg = gen_rtx_REG (Pmode, TOC_REGNUM);
31975 rtx toc_load = NULL_RTX;
31976 rtx toc_restore = NULL_RTX;
31977 rtx func_addr;
31978 rtx abi_reg = NULL_RTX;
31979 rtx call[4];
31980 int n_call;
31981 rtx insn;
31983 /* Handle longcall attributes. */
31984 if (INTVAL (cookie) & CALL_LONG)
31985 func_desc = rs6000_longcall_ref (func_desc);
31987 /* Handle indirect calls. */
31988 if (GET_CODE (func_desc) != SYMBOL_REF
31989 || (DEFAULT_ABI == ABI_AIX && !SYMBOL_REF_FUNCTION_P (func_desc)))
31991 /* Save the TOC into its reserved slot before the call,
31992 and prepare to restore it after the call. */
31993 rtx stack_ptr = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
31994 rtx stack_toc_offset = GEN_INT (RS6000_TOC_SAVE_SLOT);
31995 rtx stack_toc_mem = gen_frame_mem (Pmode,
31996 gen_rtx_PLUS (Pmode, stack_ptr,
31997 stack_toc_offset));
31998 toc_restore = gen_rtx_SET (VOIDmode, toc_reg, stack_toc_mem);
32000 /* Can we optimize saving the TOC in the prologue or
32001 do we need to do it at every call? */
32002 if (TARGET_SAVE_TOC_INDIRECT && !cfun->calls_alloca)
32003 cfun->machine->save_toc_in_prologue = true;
32004 else
32006 MEM_VOLATILE_P (stack_toc_mem) = 1;
32007 emit_move_insn (stack_toc_mem, toc_reg);
32010 if (DEFAULT_ABI == ABI_ELFv2)
32012 /* A function pointer in the ELFv2 ABI is just a plain address, but
32013 the ABI requires it to be loaded into r12 before the call. */
32014 func_addr = gen_rtx_REG (Pmode, 12);
32015 emit_move_insn (func_addr, func_desc);
32016 abi_reg = func_addr;
32018 else
32020 /* A function pointer under AIX is a pointer to a data area whose
32021 first word contains the actual address of the function, whose
32022 second word contains a pointer to its TOC, and whose third word
32023 contains a value to place in the static chain register (r11).
32024 Note that if we load the static chain, our "trampoline" need
32025 not have any executable code. */
32027 /* Load up address of the actual function. */
32028 func_desc = force_reg (Pmode, func_desc);
32029 func_addr = gen_reg_rtx (Pmode);
32030 emit_move_insn (func_addr, gen_rtx_MEM (Pmode, func_desc));
32032 /* Prepare to load the TOC of the called function. Note that the
32033 TOC load must happen immediately before the actual call so
32034 that unwinding the TOC registers works correctly. See the
32035 comment in frob_update_context. */
32036 rtx func_toc_offset = GEN_INT (GET_MODE_SIZE (Pmode));
32037 rtx func_toc_mem = gen_rtx_MEM (Pmode,
32038 gen_rtx_PLUS (Pmode, func_desc,
32039 func_toc_offset));
32040 toc_load = gen_rtx_USE (VOIDmode, func_toc_mem);
32042 /* If we have a static chain, load it up. */
32043 if (TARGET_POINTERS_TO_NESTED_FUNCTIONS)
32045 rtx sc_reg = gen_rtx_REG (Pmode, STATIC_CHAIN_REGNUM);
32046 rtx func_sc_offset = GEN_INT (2 * GET_MODE_SIZE (Pmode));
32047 rtx func_sc_mem = gen_rtx_MEM (Pmode,
32048 gen_rtx_PLUS (Pmode, func_desc,
32049 func_sc_offset));
32050 emit_move_insn (sc_reg, func_sc_mem);
32051 abi_reg = sc_reg;
32055 else
32057 /* Direct calls use the TOC: for local calls, the callee will
32058 assume the TOC register is set; for non-local calls, the
32059 PLT stub needs the TOC register. */
32060 abi_reg = toc_reg;
32061 func_addr = func_desc;
32064 /* Create the call. */
32065 call[0] = gen_rtx_CALL (VOIDmode, gen_rtx_MEM (SImode, func_addr), flag);
32066 if (value != NULL_RTX)
32067 call[0] = gen_rtx_SET (VOIDmode, value, call[0]);
32068 n_call = 1;
32070 if (toc_load)
32071 call[n_call++] = toc_load;
32072 if (toc_restore)
32073 call[n_call++] = toc_restore;
32075 call[n_call++] = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, LR_REGNO));
32077 insn = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (n_call, call));
32078 insn = emit_call_insn (insn);
32080 /* Mention all registers defined by the ABI to hold information
32081 as uses in CALL_INSN_FUNCTION_USAGE. */
32082 if (abi_reg)
32083 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), abi_reg);
32086 /* Expand code to perform a sibling call under the AIX or ELFv2 ABI. */
32088 void
32089 rs6000_sibcall_aix (rtx value, rtx func_desc, rtx flag, rtx cookie)
32091 rtx call[2];
32092 rtx insn;
32094 gcc_assert (INTVAL (cookie) == 0);
32096 /* Create the call. */
32097 call[0] = gen_rtx_CALL (VOIDmode, gen_rtx_MEM (SImode, func_desc), flag);
32098 if (value != NULL_RTX)
32099 call[0] = gen_rtx_SET (VOIDmode, value, call[0]);
32101 call[1] = simple_return_rtx;
32103 insn = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (2, call));
32104 insn = emit_call_insn (insn);
32106 /* Note use of the TOC register. */
32107 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), gen_rtx_REG (Pmode, TOC_REGNUM));
32108 /* We need to also mark a use of the link register since the function we
32109 sibling-call to will use it to return to our caller. */
32110 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), gen_rtx_REG (Pmode, LR_REGNO));
32113 /* Return whether we need to always update the saved TOC pointer when we update
32114 the stack pointer. */
32116 static bool
32117 rs6000_save_toc_in_prologue_p (void)
32119 return (cfun && cfun->machine && cfun->machine->save_toc_in_prologue);
32122 #ifdef HAVE_GAS_HIDDEN
32123 # define USE_HIDDEN_LINKONCE 1
32124 #else
32125 # define USE_HIDDEN_LINKONCE 0
32126 #endif
32128 /* Fills in the label name that should be used for a 476 link stack thunk. */
32130 void
32131 get_ppc476_thunk_name (char name[32])
32133 gcc_assert (TARGET_LINK_STACK);
32135 if (USE_HIDDEN_LINKONCE)
32136 sprintf (name, "__ppc476.get_thunk");
32137 else
32138 ASM_GENERATE_INTERNAL_LABEL (name, "LPPC476_", 0);
32141 /* This function emits the simple thunk routine that is used to preserve
32142 the link stack on the 476 cpu. */
32144 static void rs6000_code_end (void) ATTRIBUTE_UNUSED;
32145 static void
32146 rs6000_code_end (void)
32148 char name[32];
32149 tree decl;
32151 if (!TARGET_LINK_STACK)
32152 return;
32154 get_ppc476_thunk_name (name);
32156 decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL, get_identifier (name),
32157 build_function_type_list (void_type_node, NULL_TREE));
32158 DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
32159 NULL_TREE, void_type_node);
32160 TREE_PUBLIC (decl) = 1;
32161 TREE_STATIC (decl) = 1;
32163 #if RS6000_WEAK
32164 if (USE_HIDDEN_LINKONCE)
32166 DECL_COMDAT_GROUP (decl) = DECL_ASSEMBLER_NAME (decl);
32167 targetm.asm_out.unique_section (decl, 0);
32168 switch_to_section (get_named_section (decl, NULL, 0));
32169 DECL_WEAK (decl) = 1;
32170 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
32171 targetm.asm_out.globalize_label (asm_out_file, name);
32172 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
32173 ASM_DECLARE_FUNCTION_NAME (asm_out_file, name, decl);
32175 else
32176 #endif
32178 switch_to_section (text_section);
32179 ASM_OUTPUT_LABEL (asm_out_file, name);
32182 DECL_INITIAL (decl) = make_node (BLOCK);
32183 current_function_decl = decl;
32184 init_function_start (decl);
32185 first_function_block_is_cold = false;
32186 /* Make sure unwind info is emitted for the thunk if needed. */
32187 final_start_function (emit_barrier (), asm_out_file, 1);
32189 fputs ("\tblr\n", asm_out_file);
32191 final_end_function ();
32192 init_insn_lengths ();
32193 free_after_compilation (cfun);
32194 set_cfun (NULL);
32195 current_function_decl = NULL;
32198 /* Add r30 to hard reg set if the prologue sets it up and it is not
32199 pic_offset_table_rtx. */
32201 static void
32202 rs6000_set_up_by_prologue (struct hard_reg_set_container *set)
32204 if (!TARGET_SINGLE_PIC_BASE
32205 && TARGET_TOC
32206 && TARGET_MINIMAL_TOC
32207 && get_pool_size () != 0)
32208 add_to_hard_reg_set (&set->set, Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
32212 /* Helper function for rs6000_split_logical to emit a logical instruction after
32213 spliting the operation to single GPR registers.
32215 DEST is the destination register.
32216 OP1 and OP2 are the input source registers.
32217 CODE is the base operation (AND, IOR, XOR, NOT).
32218 MODE is the machine mode.
32219 If COMPLEMENT_FINAL_P is true, wrap the whole operation with NOT.
32220 If COMPLEMENT_OP1_P is true, wrap operand1 with NOT.
32221 If COMPLEMENT_OP2_P is true, wrap operand2 with NOT.
32222 CLOBBER_REG is either NULL or a scratch register of type CC to allow
32223 formation of the AND instructions. */
32225 static void
32226 rs6000_split_logical_inner (rtx dest,
32227 rtx op1,
32228 rtx op2,
32229 enum rtx_code code,
32230 enum machine_mode mode,
32231 bool complement_final_p,
32232 bool complement_op1_p,
32233 bool complement_op2_p,
32234 rtx clobber_reg)
32236 rtx bool_rtx;
32237 rtx set_rtx;
32239 /* Optimize AND of 0/0xffffffff and IOR/XOR of 0. */
32240 if (op2 && GET_CODE (op2) == CONST_INT
32241 && (mode == SImode || (mode == DImode && TARGET_POWERPC64))
32242 && !complement_final_p && !complement_op1_p && !complement_op2_p)
32244 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
32245 HOST_WIDE_INT value = INTVAL (op2) & mask;
32247 /* Optimize AND of 0 to just set 0. Optimize AND of -1 to be a move. */
32248 if (code == AND)
32250 if (value == 0)
32252 emit_insn (gen_rtx_SET (VOIDmode, dest, const0_rtx));
32253 return;
32256 else if (value == mask)
32258 if (!rtx_equal_p (dest, op1))
32259 emit_insn (gen_rtx_SET (VOIDmode, dest, op1));
32260 return;
32264 /* Optimize IOR/XOR of 0 to be a simple move. Split large operations
32265 into separate ORI/ORIS or XORI/XORIS instrucitons. */
32266 else if (code == IOR || code == XOR)
32268 if (value == 0)
32270 if (!rtx_equal_p (dest, op1))
32271 emit_insn (gen_rtx_SET (VOIDmode, dest, op1));
32272 return;
32277 if (complement_op1_p)
32278 op1 = gen_rtx_NOT (mode, op1);
32280 if (complement_op2_p)
32281 op2 = gen_rtx_NOT (mode, op2);
32283 bool_rtx = ((code == NOT)
32284 ? gen_rtx_NOT (mode, op1)
32285 : gen_rtx_fmt_ee (code, mode, op1, op2));
32287 if (complement_final_p)
32288 bool_rtx = gen_rtx_NOT (mode, bool_rtx);
32290 set_rtx = gen_rtx_SET (VOIDmode, dest, bool_rtx);
32292 /* Is this AND with an explicit clobber? */
32293 if (clobber_reg)
32295 rtx clobber = gen_rtx_CLOBBER (VOIDmode, clobber_reg);
32296 set_rtx = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set_rtx, clobber));
32299 emit_insn (set_rtx);
32300 return;
32303 /* Split a DImode AND/IOR/XOR with a constant on a 32-bit system. These
32304 operations are split immediately during RTL generation to allow for more
32305 optimizations of the AND/IOR/XOR.
32307 OPERANDS is an array containing the destination and two input operands.
32308 CODE is the base operation (AND, IOR, XOR, NOT).
32309 MODE is the machine mode.
32310 If COMPLEMENT_FINAL_P is true, wrap the whole operation with NOT.
32311 If COMPLEMENT_OP1_P is true, wrap operand1 with NOT.
32312 If COMPLEMENT_OP2_P is true, wrap operand2 with NOT.
32313 CLOBBER_REG is either NULL or a scratch register of type CC to allow
32314 formation of the AND instructions. */
32316 static void
32317 rs6000_split_logical_di (rtx operands[3],
32318 enum rtx_code code,
32319 bool complement_final_p,
32320 bool complement_op1_p,
32321 bool complement_op2_p,
32322 rtx clobber_reg)
32324 const HOST_WIDE_INT lower_32bits = HOST_WIDE_INT_C(0xffffffff);
32325 const HOST_WIDE_INT upper_32bits = ~ lower_32bits;
32326 const HOST_WIDE_INT sign_bit = HOST_WIDE_INT_C(0x80000000);
32327 enum hi_lo { hi = 0, lo = 1 };
32328 rtx op0_hi_lo[2], op1_hi_lo[2], op2_hi_lo[2];
32329 size_t i;
32331 op0_hi_lo[hi] = gen_highpart (SImode, operands[0]);
32332 op1_hi_lo[hi] = gen_highpart (SImode, operands[1]);
32333 op0_hi_lo[lo] = gen_lowpart (SImode, operands[0]);
32334 op1_hi_lo[lo] = gen_lowpart (SImode, operands[1]);
32336 if (code == NOT)
32337 op2_hi_lo[hi] = op2_hi_lo[lo] = NULL_RTX;
32338 else
32340 if (GET_CODE (operands[2]) != CONST_INT)
32342 op2_hi_lo[hi] = gen_highpart_mode (SImode, DImode, operands[2]);
32343 op2_hi_lo[lo] = gen_lowpart (SImode, operands[2]);
32345 else
32347 HOST_WIDE_INT value = INTVAL (operands[2]);
32348 HOST_WIDE_INT value_hi_lo[2];
32350 gcc_assert (!complement_final_p);
32351 gcc_assert (!complement_op1_p);
32352 gcc_assert (!complement_op2_p);
32354 value_hi_lo[hi] = value >> 32;
32355 value_hi_lo[lo] = value & lower_32bits;
32357 for (i = 0; i < 2; i++)
32359 HOST_WIDE_INT sub_value = value_hi_lo[i];
32361 if (sub_value & sign_bit)
32362 sub_value |= upper_32bits;
32364 op2_hi_lo[i] = GEN_INT (sub_value);
32366 /* If this is an AND instruction, check to see if we need to load
32367 the value in a register. */
32368 if (code == AND && sub_value != -1 && sub_value != 0
32369 && !and_operand (op2_hi_lo[i], SImode))
32370 op2_hi_lo[i] = force_reg (SImode, op2_hi_lo[i]);
32375 for (i = 0; i < 2; i++)
32377 /* Split large IOR/XOR operations. */
32378 if ((code == IOR || code == XOR)
32379 && GET_CODE (op2_hi_lo[i]) == CONST_INT
32380 && !complement_final_p
32381 && !complement_op1_p
32382 && !complement_op2_p
32383 && clobber_reg == NULL_RTX
32384 && !logical_const_operand (op2_hi_lo[i], SImode))
32386 HOST_WIDE_INT value = INTVAL (op2_hi_lo[i]);
32387 HOST_WIDE_INT hi_16bits = value & HOST_WIDE_INT_C(0xffff0000);
32388 HOST_WIDE_INT lo_16bits = value & HOST_WIDE_INT_C(0x0000ffff);
32389 rtx tmp = gen_reg_rtx (SImode);
32391 /* Make sure the constant is sign extended. */
32392 if ((hi_16bits & sign_bit) != 0)
32393 hi_16bits |= upper_32bits;
32395 rs6000_split_logical_inner (tmp, op1_hi_lo[i], GEN_INT (hi_16bits),
32396 code, SImode, false, false, false,
32397 NULL_RTX);
32399 rs6000_split_logical_inner (op0_hi_lo[i], tmp, GEN_INT (lo_16bits),
32400 code, SImode, false, false, false,
32401 NULL_RTX);
32403 else
32404 rs6000_split_logical_inner (op0_hi_lo[i], op1_hi_lo[i], op2_hi_lo[i],
32405 code, SImode, complement_final_p,
32406 complement_op1_p, complement_op2_p,
32407 clobber_reg);
32410 return;
32413 /* Split the insns that make up boolean operations operating on multiple GPR
32414 registers. The boolean MD patterns ensure that the inputs either are
32415 exactly the same as the output registers, or there is no overlap.
32417 OPERANDS is an array containing the destination and two input operands.
32418 CODE is the base operation (AND, IOR, XOR, NOT).
32419 MODE is the machine mode.
32420 If COMPLEMENT_FINAL_P is true, wrap the whole operation with NOT.
32421 If COMPLEMENT_OP1_P is true, wrap operand1 with NOT.
32422 If COMPLEMENT_OP2_P is true, wrap operand2 with NOT.
32423 CLOBBER_REG is either NULL or a scratch register of type CC to allow
32424 formation of the AND instructions. */
32426 void
32427 rs6000_split_logical (rtx operands[3],
32428 enum rtx_code code,
32429 bool complement_final_p,
32430 bool complement_op1_p,
32431 bool complement_op2_p,
32432 rtx clobber_reg)
32434 enum machine_mode mode = GET_MODE (operands[0]);
32435 enum machine_mode sub_mode;
32436 rtx op0, op1, op2;
32437 int sub_size, regno0, regno1, nregs, i;
32439 /* If this is DImode, use the specialized version that can run before
32440 register allocation. */
32441 if (mode == DImode && !TARGET_POWERPC64)
32443 rs6000_split_logical_di (operands, code, complement_final_p,
32444 complement_op1_p, complement_op2_p,
32445 clobber_reg);
32446 return;
32449 op0 = operands[0];
32450 op1 = operands[1];
32451 op2 = (code == NOT) ? NULL_RTX : operands[2];
32452 sub_mode = (TARGET_POWERPC64) ? DImode : SImode;
32453 sub_size = GET_MODE_SIZE (sub_mode);
32454 regno0 = REGNO (op0);
32455 regno1 = REGNO (op1);
32457 gcc_assert (reload_completed);
32458 gcc_assert (IN_RANGE (regno0, FIRST_GPR_REGNO, LAST_GPR_REGNO));
32459 gcc_assert (IN_RANGE (regno1, FIRST_GPR_REGNO, LAST_GPR_REGNO));
32461 nregs = rs6000_hard_regno_nregs[(int)mode][regno0];
32462 gcc_assert (nregs > 1);
32464 if (op2 && REG_P (op2))
32465 gcc_assert (IN_RANGE (REGNO (op2), FIRST_GPR_REGNO, LAST_GPR_REGNO));
32467 for (i = 0; i < nregs; i++)
32469 int offset = i * sub_size;
32470 rtx sub_op0 = simplify_subreg (sub_mode, op0, mode, offset);
32471 rtx sub_op1 = simplify_subreg (sub_mode, op1, mode, offset);
32472 rtx sub_op2 = ((code == NOT)
32473 ? NULL_RTX
32474 : simplify_subreg (sub_mode, op2, mode, offset));
32476 rs6000_split_logical_inner (sub_op0, sub_op1, sub_op2, code, sub_mode,
32477 complement_final_p, complement_op1_p,
32478 complement_op2_p, clobber_reg);
32481 return;
32485 /* Return true if the peephole2 can combine a load involving a combination of
32486 an addis instruction and a load with an offset that can be fused together on
32487 a power8.
32489 The operands are:
32490 operands[0] register set with addis
32491 operands[1] value set via addis
32492 operands[2] target register being loaded
32493 operands[3] D-form memory reference using operands[0].
32495 In addition, we are passed a boolean that is true if this is a peephole2,
32496 and we can use see if the addis_reg is dead after the insn and can be
32497 replaced by the target register. */
32499 bool
32500 fusion_gpr_load_p (rtx *operands, bool peep2_p)
32502 rtx addis_reg = operands[0];
32503 rtx addis_value = operands[1];
32504 rtx target = operands[2];
32505 rtx mem = operands[3];
32506 rtx addr;
32507 rtx base_reg;
32509 /* Validate arguments. */
32510 if (!base_reg_operand (addis_reg, GET_MODE (addis_reg)))
32511 return false;
32513 if (!base_reg_operand (target, GET_MODE (target)))
32514 return false;
32516 if (!fusion_gpr_addis (addis_value, GET_MODE (addis_value)))
32517 return false;
32519 if (!fusion_gpr_mem_load (mem, GET_MODE (mem)))
32520 return false;
32522 /* Allow sign/zero extension. */
32523 if (GET_CODE (mem) == ZERO_EXTEND
32524 || (GET_CODE (mem) == SIGN_EXTEND && TARGET_P8_FUSION_SIGN))
32525 mem = XEXP (mem, 0);
32527 if (!MEM_P (mem))
32528 return false;
32530 addr = XEXP (mem, 0); /* either PLUS or LO_SUM. */
32531 if (GET_CODE (addr) != PLUS && GET_CODE (addr) != LO_SUM)
32532 return false;
32534 /* Validate that the register used to load the high value is either the
32535 register being loaded, or we can safely replace its use in a peephole2.
32537 If this is a peephole2, we assume that there are 2 instructions in the
32538 peephole (addis and load), so we want to check if the target register was
32539 not used in the memory address and the register to hold the addis result
32540 is dead after the peephole. */
32541 if (REGNO (addis_reg) != REGNO (target))
32543 if (!peep2_p)
32544 return false;
32546 if (reg_mentioned_p (target, mem))
32547 return false;
32549 if (!peep2_reg_dead_p (2, addis_reg))
32550 return false;
32552 /* If the target register being loaded is the stack pointer, we must
32553 avoid loading any other value into it, even temporarily. */
32554 if (REG_P (target) && REGNO (target) == STACK_POINTER_REGNUM)
32555 return false;
32558 base_reg = XEXP (addr, 0);
32559 return REGNO (addis_reg) == REGNO (base_reg);
32562 /* During the peephole2 pass, adjust and expand the insns for a load fusion
32563 sequence. We adjust the addis register to use the target register. If the
32564 load sign extends, we adjust the code to do the zero extending load, and an
32565 explicit sign extension later since the fusion only covers zero extending
32566 loads.
32568 The operands are:
32569 operands[0] register set with addis (to be replaced with target)
32570 operands[1] value set via addis
32571 operands[2] target register being loaded
32572 operands[3] D-form memory reference using operands[0]. */
32574 void
32575 expand_fusion_gpr_load (rtx *operands)
32577 rtx addis_value = operands[1];
32578 rtx target = operands[2];
32579 rtx orig_mem = operands[3];
32580 rtx new_addr, new_mem, orig_addr, offset;
32581 enum rtx_code plus_or_lo_sum;
32582 enum machine_mode target_mode = GET_MODE (target);
32583 enum machine_mode extend_mode = target_mode;
32584 enum machine_mode ptr_mode = Pmode;
32585 enum rtx_code extend = UNKNOWN;
32586 rtx addis_reg = ((ptr_mode == target_mode)
32587 ? target
32588 : simplify_subreg (ptr_mode, target, target_mode, 0));
32590 if (GET_CODE (orig_mem) == ZERO_EXTEND
32591 || (TARGET_P8_FUSION_SIGN && GET_CODE (orig_mem) == SIGN_EXTEND))
32593 extend = GET_CODE (orig_mem);
32594 orig_mem = XEXP (orig_mem, 0);
32595 target_mode = GET_MODE (orig_mem);
32598 gcc_assert (MEM_P (orig_mem));
32600 orig_addr = XEXP (orig_mem, 0);
32601 plus_or_lo_sum = GET_CODE (orig_addr);
32602 gcc_assert (plus_or_lo_sum == PLUS || plus_or_lo_sum == LO_SUM);
32604 offset = XEXP (orig_addr, 1);
32605 new_addr = gen_rtx_fmt_ee (plus_or_lo_sum, ptr_mode, addis_reg, offset);
32606 new_mem = change_address (orig_mem, target_mode, new_addr);
32608 if (extend != UNKNOWN)
32609 new_mem = gen_rtx_fmt_e (ZERO_EXTEND, extend_mode, new_mem);
32611 emit_insn (gen_rtx_SET (VOIDmode, addis_reg, addis_value));
32612 emit_insn (gen_rtx_SET (VOIDmode, target, new_mem));
32614 if (extend == SIGN_EXTEND)
32616 int sub_off = ((BYTES_BIG_ENDIAN)
32617 ? GET_MODE_SIZE (extend_mode) - GET_MODE_SIZE (target_mode)
32618 : 0);
32619 rtx sign_reg
32620 = simplify_subreg (target_mode, target, extend_mode, sub_off);
32622 emit_insn (gen_rtx_SET (VOIDmode, target,
32623 gen_rtx_SIGN_EXTEND (extend_mode, sign_reg)));
32626 return;
32629 /* Return a string to fuse an addis instruction with a gpr load to the same
32630 register that we loaded up the addis instruction. The code is complicated,
32631 so we call output_asm_insn directly, and just return "".
32633 The operands are:
32634 operands[0] register set with addis (must be same reg as target).
32635 operands[1] value set via addis
32636 operands[2] target register being loaded
32637 operands[3] D-form memory reference using operands[0]. */
32639 const char *
32640 emit_fusion_gpr_load (rtx *operands)
32642 rtx addis_reg = operands[0];
32643 rtx addis_value = operands[1];
32644 rtx target = operands[2];
32645 rtx mem = operands[3];
32646 rtx fuse_ops[10];
32647 rtx addr;
32648 rtx load_offset;
32649 const char *addis_str = NULL;
32650 const char *load_str = NULL;
32651 const char *extend_insn = NULL;
32652 const char *mode_name = NULL;
32653 char insn_template[80];
32654 enum machine_mode mode;
32655 const char *comment_str = ASM_COMMENT_START;
32656 bool sign_p = false;
32658 gcc_assert (REG_P (addis_reg) && REG_P (target));
32659 gcc_assert (REGNO (addis_reg) == REGNO (target));
32661 if (*comment_str == ' ')
32662 comment_str++;
32664 /* Allow sign/zero extension. */
32665 if (GET_CODE (mem) == ZERO_EXTEND)
32666 mem = XEXP (mem, 0);
32668 else if (GET_CODE (mem) == SIGN_EXTEND && TARGET_P8_FUSION_SIGN)
32670 sign_p = true;
32671 mem = XEXP (mem, 0);
32674 gcc_assert (MEM_P (mem));
32675 addr = XEXP (mem, 0);
32676 if (GET_CODE (addr) != PLUS && GET_CODE (addr) != LO_SUM)
32677 gcc_unreachable ();
32679 load_offset = XEXP (addr, 1);
32681 /* Now emit the load instruction to the same register. */
32682 mode = GET_MODE (mem);
32683 switch (mode)
32685 case QImode:
32686 mode_name = "char";
32687 load_str = "lbz";
32688 extend_insn = "extsb %0,%0";
32689 break;
32691 case HImode:
32692 mode_name = "short";
32693 load_str = "lhz";
32694 extend_insn = "extsh %0,%0";
32695 break;
32697 case SImode:
32698 mode_name = "int";
32699 load_str = "lwz";
32700 extend_insn = "extsw %0,%0";
32701 break;
32703 case DImode:
32704 if (TARGET_POWERPC64)
32706 mode_name = "long";
32707 load_str = "ld";
32709 else
32710 gcc_unreachable ();
32711 break;
32713 default:
32714 gcc_unreachable ();
32717 /* Emit the addis instruction. */
32718 fuse_ops[0] = target;
32719 if (satisfies_constraint_L (addis_value))
32721 fuse_ops[1] = addis_value;
32722 addis_str = "lis %0,%v1";
32725 else if (GET_CODE (addis_value) == PLUS)
32727 rtx op0 = XEXP (addis_value, 0);
32728 rtx op1 = XEXP (addis_value, 1);
32730 if (REG_P (op0) && CONST_INT_P (op1)
32731 && satisfies_constraint_L (op1))
32733 fuse_ops[1] = op0;
32734 fuse_ops[2] = op1;
32735 addis_str = "addis %0,%1,%v2";
32739 else if (GET_CODE (addis_value) == HIGH)
32741 rtx value = XEXP (addis_value, 0);
32742 if (GET_CODE (value) == UNSPEC && XINT (value, 1) == UNSPEC_TOCREL)
32744 fuse_ops[1] = XVECEXP (value, 0, 0); /* symbol ref. */
32745 fuse_ops[2] = XVECEXP (value, 0, 1); /* TOC register. */
32746 if (TARGET_ELF)
32747 addis_str = "addis %0,%2,%1@toc@ha";
32749 else if (TARGET_XCOFF)
32750 addis_str = "addis %0,%1@u(%2)";
32752 else
32753 gcc_unreachable ();
32756 else if (GET_CODE (value) == PLUS)
32758 rtx op0 = XEXP (value, 0);
32759 rtx op1 = XEXP (value, 1);
32761 if (GET_CODE (op0) == UNSPEC
32762 && XINT (op0, 1) == UNSPEC_TOCREL
32763 && CONST_INT_P (op1))
32765 fuse_ops[1] = XVECEXP (op0, 0, 0); /* symbol ref. */
32766 fuse_ops[2] = XVECEXP (op0, 0, 1); /* TOC register. */
32767 fuse_ops[3] = op1;
32768 if (TARGET_ELF)
32769 addis_str = "addis %0,%2,%1+%3@toc@ha";
32771 else if (TARGET_XCOFF)
32772 addis_str = "addis %0,%1+%3@u(%2)";
32774 else
32775 gcc_unreachable ();
32779 else if (satisfies_constraint_L (value))
32781 fuse_ops[1] = value;
32782 addis_str = "lis %0,%v1";
32785 else if (TARGET_ELF && !TARGET_POWERPC64 && CONSTANT_P (value))
32787 fuse_ops[1] = value;
32788 addis_str = "lis %0,%1@ha";
32792 if (!addis_str)
32793 fatal_insn ("Could not generate addis value for fusion", addis_value);
32795 sprintf (insn_template, "%s\t\t%s gpr load fusion, type %s", addis_str,
32796 comment_str, mode_name);
32797 output_asm_insn (insn_template, fuse_ops);
32799 /* Emit the D-form load instruction. */
32800 if (CONST_INT_P (load_offset) && satisfies_constraint_I (load_offset))
32802 sprintf (insn_template, "%s %%0,%%1(%%0)", load_str);
32803 fuse_ops[1] = load_offset;
32804 output_asm_insn (insn_template, fuse_ops);
32807 else if (GET_CODE (load_offset) == UNSPEC
32808 && XINT (load_offset, 1) == UNSPEC_TOCREL)
32810 if (TARGET_ELF)
32811 sprintf (insn_template, "%s %%0,%%1@toc@l(%%0)", load_str);
32813 else if (TARGET_XCOFF)
32814 sprintf (insn_template, "%s %%0,%%1@l(%%0)", load_str);
32816 else
32817 gcc_unreachable ();
32819 fuse_ops[1] = XVECEXP (load_offset, 0, 0);
32820 output_asm_insn (insn_template, fuse_ops);
32823 else if (GET_CODE (load_offset) == PLUS
32824 && GET_CODE (XEXP (load_offset, 0)) == UNSPEC
32825 && XINT (XEXP (load_offset, 0), 1) == UNSPEC_TOCREL
32826 && CONST_INT_P (XEXP (load_offset, 1)))
32828 rtx tocrel_unspec = XEXP (load_offset, 0);
32829 if (TARGET_ELF)
32830 sprintf (insn_template, "%s %%0,%%1+%%2@toc@l(%%0)", load_str);
32832 else if (TARGET_XCOFF)
32833 sprintf (insn_template, "%s %%0,%%1+%%2@l(%%0)", load_str);
32835 else
32836 gcc_unreachable ();
32838 fuse_ops[1] = XVECEXP (tocrel_unspec, 0, 0);
32839 fuse_ops[2] = XEXP (load_offset, 1);
32840 output_asm_insn (insn_template, fuse_ops);
32843 else if (TARGET_ELF && !TARGET_POWERPC64 && CONSTANT_P (load_offset))
32845 sprintf (insn_template, "%s %%0,%%1@l(%%0)", load_str);
32847 fuse_ops[1] = load_offset;
32848 output_asm_insn (insn_template, fuse_ops);
32851 else
32852 fatal_insn ("Unable to generate load offset for fusion", load_offset);
32854 /* Handle sign extension. The peephole2 pass generates this as a separate
32855 insn, but we handle it just in case it got reattached. */
32856 if (sign_p)
32858 gcc_assert (extend_insn != NULL);
32859 output_asm_insn (extend_insn, fuse_ops);
32862 return "";
32866 struct gcc_target targetm = TARGET_INITIALIZER;
32868 #include "gt-rs6000.h"