* config/rs6000/rs6000.c (htm_expand_builtin) <case 0>: Remove.
[official-gcc.git] / gcc / config / rs6000 / rs6000.c
blobbb484c2c83c7ff9a5b021c47378096ad2ef7415a
1 /* Subroutines used for code generation on IBM RS/6000.
2 Copyright (C) 1991-2013 Free Software Foundation, Inc.
3 Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published
9 by the Free Software Foundation; either version 3, or (at your
10 option) any later version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "regs.h"
27 #include "hard-reg-set.h"
28 #include "insn-config.h"
29 #include "conditions.h"
30 #include "insn-attr.h"
31 #include "flags.h"
32 #include "recog.h"
33 #include "obstack.h"
34 #include "tree.h"
35 #include "expr.h"
36 #include "optabs.h"
37 #include "except.h"
38 #include "function.h"
39 #include "output.h"
40 #include "dbxout.h"
41 #include "basic-block.h"
42 #include "diagnostic-core.h"
43 #include "toplev.h"
44 #include "ggc.h"
45 #include "hashtab.h"
46 #include "tm_p.h"
47 #include "target.h"
48 #include "target-def.h"
49 #include "common/common-target.h"
50 #include "langhooks.h"
51 #include "reload.h"
52 #include "cfgloop.h"
53 #include "sched-int.h"
54 #include "gimple.h"
55 #include "tree-flow.h"
56 #include "intl.h"
57 #include "params.h"
58 #include "tm-constrs.h"
59 #include "opts.h"
60 #include "tree-vectorizer.h"
61 #include "dumpfile.h"
62 #if TARGET_XCOFF
63 #include "xcoffout.h" /* get declarations of xcoff_*_section_name */
64 #endif
65 #if TARGET_MACHO
66 #include "gstab.h" /* for N_SLINE */
67 #endif
69 #ifndef TARGET_NO_PROTOTYPE
70 #define TARGET_NO_PROTOTYPE 0
71 #endif
73 #define min(A,B) ((A) < (B) ? (A) : (B))
74 #define max(A,B) ((A) > (B) ? (A) : (B))
76 /* Structure used to define the rs6000 stack */
77 typedef struct rs6000_stack {
78 int reload_completed; /* stack info won't change from here on */
79 int first_gp_reg_save; /* first callee saved GP register used */
80 int first_fp_reg_save; /* first callee saved FP register used */
81 int first_altivec_reg_save; /* first callee saved AltiVec register used */
82 int lr_save_p; /* true if the link reg needs to be saved */
83 int cr_save_p; /* true if the CR reg needs to be saved */
84 unsigned int vrsave_mask; /* mask of vec registers to save */
85 int push_p; /* true if we need to allocate stack space */
86 int calls_p; /* true if the function makes any calls */
87 int world_save_p; /* true if we're saving *everything*:
88 r13-r31, cr, f14-f31, vrsave, v20-v31 */
89 enum rs6000_abi abi; /* which ABI to use */
90 int gp_save_offset; /* offset to save GP regs from initial SP */
91 int fp_save_offset; /* offset to save FP regs from initial SP */
92 int altivec_save_offset; /* offset to save AltiVec regs from initial SP */
93 int lr_save_offset; /* offset to save LR from initial SP */
94 int cr_save_offset; /* offset to save CR from initial SP */
95 int vrsave_save_offset; /* offset to save VRSAVE from initial SP */
96 int spe_gp_save_offset; /* offset to save spe 64-bit gprs */
97 int varargs_save_offset; /* offset to save the varargs registers */
98 int ehrd_offset; /* offset to EH return data */
99 int reg_size; /* register size (4 or 8) */
100 HOST_WIDE_INT vars_size; /* variable save area size */
101 int parm_size; /* outgoing parameter size */
102 int save_size; /* save area size */
103 int fixed_size; /* fixed size of stack frame */
104 int gp_size; /* size of saved GP registers */
105 int fp_size; /* size of saved FP registers */
106 int altivec_size; /* size of saved AltiVec registers */
107 int cr_size; /* size to hold CR if not in save_size */
108 int vrsave_size; /* size to hold VRSAVE if not in save_size */
109 int altivec_padding_size; /* size of altivec alignment padding if
110 not in save_size */
111 int spe_gp_size; /* size of 64-bit GPR save size for SPE */
112 int spe_padding_size;
113 HOST_WIDE_INT total_size; /* total bytes allocated for stack */
114 int spe_64bit_regs_used;
115 int savres_strategy;
116 } rs6000_stack_t;
118 /* A C structure for machine-specific, per-function data.
119 This is added to the cfun structure. */
120 typedef struct GTY(()) machine_function
122 /* Some local-dynamic symbol. */
123 const char *some_ld_name;
124 /* Whether the instruction chain has been scanned already. */
125 int insn_chain_scanned_p;
126 /* Flags if __builtin_return_address (n) with n >= 1 was used. */
127 int ra_needs_full_frame;
128 /* Flags if __builtin_return_address (0) was used. */
129 int ra_need_lr;
130 /* Cache lr_save_p after expansion of builtin_eh_return. */
131 int lr_save_state;
132 /* Whether we need to save the TOC to the reserved stack location in the
133 function prologue. */
134 bool save_toc_in_prologue;
135 /* Offset from virtual_stack_vars_rtx to the start of the ABI_V4
136 varargs save area. */
137 HOST_WIDE_INT varargs_save_offset;
138 /* Temporary stack slot to use for SDmode copies. This slot is
139 64-bits wide and is allocated early enough so that the offset
140 does not overflow the 16-bit load/store offset field. */
141 rtx sdmode_stack_slot;
142 } machine_function;
144 /* Support targetm.vectorize.builtin_mask_for_load. */
145 static GTY(()) tree altivec_builtin_mask_for_load;
147 /* Set to nonzero once AIX common-mode calls have been defined. */
148 static GTY(()) int common_mode_defined;
150 /* Label number of label created for -mrelocatable, to call to so we can
151 get the address of the GOT section */
152 static int rs6000_pic_labelno;
154 #ifdef USING_ELFOS_H
155 /* Counter for labels which are to be placed in .fixup. */
156 int fixuplabelno = 0;
157 #endif
159 /* Whether to use variant of AIX ABI for PowerPC64 Linux. */
160 int dot_symbols;
162 /* Specify the machine mode that pointers have. After generation of rtl, the
163 compiler makes no further distinction between pointers and any other objects
164 of this machine mode. The type is unsigned since not all things that
165 include rs6000.h also include machmode.h. */
166 unsigned rs6000_pmode;
168 /* Width in bits of a pointer. */
169 unsigned rs6000_pointer_size;
171 #ifdef HAVE_AS_GNU_ATTRIBUTE
172 /* Flag whether floating point values have been passed/returned. */
173 static bool rs6000_passes_float;
174 /* Flag whether vector values have been passed/returned. */
175 static bool rs6000_passes_vector;
176 /* Flag whether small (<= 8 byte) structures have been returned. */
177 static bool rs6000_returns_struct;
178 #endif
180 /* Value is TRUE if register/mode pair is acceptable. */
181 bool rs6000_hard_regno_mode_ok_p[NUM_MACHINE_MODES][FIRST_PSEUDO_REGISTER];
183 /* Maximum number of registers needed for a given register class and mode. */
184 unsigned char rs6000_class_max_nregs[NUM_MACHINE_MODES][LIM_REG_CLASSES];
186 /* How many registers are needed for a given register and mode. */
187 unsigned char rs6000_hard_regno_nregs[NUM_MACHINE_MODES][FIRST_PSEUDO_REGISTER];
189 /* Map register number to register class. */
190 enum reg_class rs6000_regno_regclass[FIRST_PSEUDO_REGISTER];
192 /* Reload functions based on the type and the vector unit. */
193 static enum insn_code rs6000_vector_reload[NUM_MACHINE_MODES][2];
195 static int dbg_cost_ctrl;
197 /* Built in types. */
198 tree rs6000_builtin_types[RS6000_BTI_MAX];
199 tree rs6000_builtin_decls[RS6000_BUILTIN_COUNT];
201 /* Flag to say the TOC is initialized */
202 int toc_initialized;
203 char toc_label_name[10];
205 /* Cached value of rs6000_variable_issue. This is cached in
206 rs6000_variable_issue hook and returned from rs6000_sched_reorder2. */
207 static short cached_can_issue_more;
209 static GTY(()) section *read_only_data_section;
210 static GTY(()) section *private_data_section;
211 static GTY(()) section *tls_data_section;
212 static GTY(()) section *tls_private_data_section;
213 static GTY(()) section *read_only_private_data_section;
214 static GTY(()) section *sdata2_section;
215 static GTY(()) section *toc_section;
217 struct builtin_description
219 const HOST_WIDE_INT mask;
220 const enum insn_code icode;
221 const char *const name;
222 const enum rs6000_builtins code;
225 /* Describe the vector unit used for modes. */
226 enum rs6000_vector rs6000_vector_unit[NUM_MACHINE_MODES];
227 enum rs6000_vector rs6000_vector_mem[NUM_MACHINE_MODES];
229 /* Register classes for various constraints that are based on the target
230 switches. */
231 enum reg_class rs6000_constraints[RS6000_CONSTRAINT_MAX];
233 /* Describe the alignment of a vector. */
234 int rs6000_vector_align[NUM_MACHINE_MODES];
236 /* Map selected modes to types for builtins. */
237 static GTY(()) tree builtin_mode_to_type[MAX_MACHINE_MODE][2];
239 /* What modes to automatically generate reciprocal divide estimate (fre) and
240 reciprocal sqrt (frsqrte) for. */
241 unsigned char rs6000_recip_bits[MAX_MACHINE_MODE];
243 /* Masks to determine which reciprocal esitmate instructions to generate
244 automatically. */
245 enum rs6000_recip_mask {
246 RECIP_SF_DIV = 0x001, /* Use divide estimate */
247 RECIP_DF_DIV = 0x002,
248 RECIP_V4SF_DIV = 0x004,
249 RECIP_V2DF_DIV = 0x008,
251 RECIP_SF_RSQRT = 0x010, /* Use reciprocal sqrt estimate. */
252 RECIP_DF_RSQRT = 0x020,
253 RECIP_V4SF_RSQRT = 0x040,
254 RECIP_V2DF_RSQRT = 0x080,
256 /* Various combination of flags for -mrecip=xxx. */
257 RECIP_NONE = 0,
258 RECIP_ALL = (RECIP_SF_DIV | RECIP_DF_DIV | RECIP_V4SF_DIV
259 | RECIP_V2DF_DIV | RECIP_SF_RSQRT | RECIP_DF_RSQRT
260 | RECIP_V4SF_RSQRT | RECIP_V2DF_RSQRT),
262 RECIP_HIGH_PRECISION = RECIP_ALL,
264 /* On low precision machines like the power5, don't enable double precision
265 reciprocal square root estimate, since it isn't accurate enough. */
266 RECIP_LOW_PRECISION = (RECIP_ALL & ~(RECIP_DF_RSQRT | RECIP_V2DF_RSQRT))
269 /* -mrecip options. */
270 static struct
272 const char *string; /* option name */
273 unsigned int mask; /* mask bits to set */
274 } recip_options[] = {
275 { "all", RECIP_ALL },
276 { "none", RECIP_NONE },
277 { "div", (RECIP_SF_DIV | RECIP_DF_DIV | RECIP_V4SF_DIV
278 | RECIP_V2DF_DIV) },
279 { "divf", (RECIP_SF_DIV | RECIP_V4SF_DIV) },
280 { "divd", (RECIP_DF_DIV | RECIP_V2DF_DIV) },
281 { "rsqrt", (RECIP_SF_RSQRT | RECIP_DF_RSQRT | RECIP_V4SF_RSQRT
282 | RECIP_V2DF_RSQRT) },
283 { "rsqrtf", (RECIP_SF_RSQRT | RECIP_V4SF_RSQRT) },
284 { "rsqrtd", (RECIP_DF_RSQRT | RECIP_V2DF_RSQRT) },
287 /* 2 argument gen function typedef. */
288 typedef rtx (*gen_2arg_fn_t) (rtx, rtx, rtx);
290 /* Pointer to function (in rs6000-c.c) that can define or undefine target
291 macros that have changed. Languages that don't support the preprocessor
292 don't link in rs6000-c.c, so we can't call it directly. */
293 void (*rs6000_target_modify_macros_ptr) (bool, HOST_WIDE_INT, HOST_WIDE_INT);
295 /* Simplfy register classes into simpler classifications. We assume
296 GPR_REG_TYPE - FPR_REG_TYPE are ordered so that we can use a simple range
297 check for standard register classes (gpr/floating/altivec/vsx) and
298 floating/vector classes (float/altivec/vsx). */
300 enum rs6000_reg_type {
301 NO_REG_TYPE,
302 PSEUDO_REG_TYPE,
303 GPR_REG_TYPE,
304 VSX_REG_TYPE,
305 ALTIVEC_REG_TYPE,
306 FPR_REG_TYPE,
307 SPR_REG_TYPE,
308 CR_REG_TYPE,
309 SPE_ACC_TYPE,
310 SPEFSCR_REG_TYPE
313 /* Map register class to register type. */
314 static enum rs6000_reg_type reg_class_to_reg_type[N_REG_CLASSES];
316 /* First/last register type for the 'normal' register types (i.e. general
317 purpose, floating point, altivec, and VSX registers). */
318 #define IS_STD_REG_TYPE(RTYPE) IN_RANGE(RTYPE, GPR_REG_TYPE, FPR_REG_TYPE)
320 #define IS_FP_VECT_REG_TYPE(RTYPE) IN_RANGE(RTYPE, VSX_REG_TYPE, FPR_REG_TYPE)
322 /* Direct moves to/from vsx/gpr registers that need an additional register to
323 do the move. */
324 static enum insn_code reload_fpr_gpr[NUM_MACHINE_MODES];
325 static enum insn_code reload_gpr_vsx[NUM_MACHINE_MODES];
326 static enum insn_code reload_vsx_gpr[NUM_MACHINE_MODES];
329 /* Target cpu costs. */
331 struct processor_costs {
332 const int mulsi; /* cost of SImode multiplication. */
333 const int mulsi_const; /* cost of SImode multiplication by constant. */
334 const int mulsi_const9; /* cost of SImode mult by short constant. */
335 const int muldi; /* cost of DImode multiplication. */
336 const int divsi; /* cost of SImode division. */
337 const int divdi; /* cost of DImode division. */
338 const int fp; /* cost of simple SFmode and DFmode insns. */
339 const int dmul; /* cost of DFmode multiplication (and fmadd). */
340 const int sdiv; /* cost of SFmode division (fdivs). */
341 const int ddiv; /* cost of DFmode division (fdiv). */
342 const int cache_line_size; /* cache line size in bytes. */
343 const int l1_cache_size; /* size of l1 cache, in kilobytes. */
344 const int l2_cache_size; /* size of l2 cache, in kilobytes. */
345 const int simultaneous_prefetches; /* number of parallel prefetch
346 operations. */
349 const struct processor_costs *rs6000_cost;
351 /* Processor costs (relative to an add) */
353 /* Instruction size costs on 32bit processors. */
354 static const
355 struct processor_costs size32_cost = {
356 COSTS_N_INSNS (1), /* mulsi */
357 COSTS_N_INSNS (1), /* mulsi_const */
358 COSTS_N_INSNS (1), /* mulsi_const9 */
359 COSTS_N_INSNS (1), /* muldi */
360 COSTS_N_INSNS (1), /* divsi */
361 COSTS_N_INSNS (1), /* divdi */
362 COSTS_N_INSNS (1), /* fp */
363 COSTS_N_INSNS (1), /* dmul */
364 COSTS_N_INSNS (1), /* sdiv */
365 COSTS_N_INSNS (1), /* ddiv */
372 /* Instruction size costs on 64bit processors. */
373 static const
374 struct processor_costs size64_cost = {
375 COSTS_N_INSNS (1), /* mulsi */
376 COSTS_N_INSNS (1), /* mulsi_const */
377 COSTS_N_INSNS (1), /* mulsi_const9 */
378 COSTS_N_INSNS (1), /* muldi */
379 COSTS_N_INSNS (1), /* divsi */
380 COSTS_N_INSNS (1), /* divdi */
381 COSTS_N_INSNS (1), /* fp */
382 COSTS_N_INSNS (1), /* dmul */
383 COSTS_N_INSNS (1), /* sdiv */
384 COSTS_N_INSNS (1), /* ddiv */
385 128,
391 /* Instruction costs on RS64A processors. */
392 static const
393 struct processor_costs rs64a_cost = {
394 COSTS_N_INSNS (20), /* mulsi */
395 COSTS_N_INSNS (12), /* mulsi_const */
396 COSTS_N_INSNS (8), /* mulsi_const9 */
397 COSTS_N_INSNS (34), /* muldi */
398 COSTS_N_INSNS (65), /* divsi */
399 COSTS_N_INSNS (67), /* divdi */
400 COSTS_N_INSNS (4), /* fp */
401 COSTS_N_INSNS (4), /* dmul */
402 COSTS_N_INSNS (31), /* sdiv */
403 COSTS_N_INSNS (31), /* ddiv */
404 128, /* cache line size */
405 128, /* l1 cache */
406 2048, /* l2 cache */
407 1, /* streams */
410 /* Instruction costs on MPCCORE processors. */
411 static const
412 struct processor_costs mpccore_cost = {
413 COSTS_N_INSNS (2), /* mulsi */
414 COSTS_N_INSNS (2), /* mulsi_const */
415 COSTS_N_INSNS (2), /* mulsi_const9 */
416 COSTS_N_INSNS (2), /* muldi */
417 COSTS_N_INSNS (6), /* divsi */
418 COSTS_N_INSNS (6), /* divdi */
419 COSTS_N_INSNS (4), /* fp */
420 COSTS_N_INSNS (5), /* dmul */
421 COSTS_N_INSNS (10), /* sdiv */
422 COSTS_N_INSNS (17), /* ddiv */
423 32, /* cache line size */
424 4, /* l1 cache */
425 16, /* l2 cache */
426 1, /* streams */
429 /* Instruction costs on PPC403 processors. */
430 static const
431 struct processor_costs ppc403_cost = {
432 COSTS_N_INSNS (4), /* mulsi */
433 COSTS_N_INSNS (4), /* mulsi_const */
434 COSTS_N_INSNS (4), /* mulsi_const9 */
435 COSTS_N_INSNS (4), /* muldi */
436 COSTS_N_INSNS (33), /* divsi */
437 COSTS_N_INSNS (33), /* divdi */
438 COSTS_N_INSNS (11), /* fp */
439 COSTS_N_INSNS (11), /* dmul */
440 COSTS_N_INSNS (11), /* sdiv */
441 COSTS_N_INSNS (11), /* ddiv */
442 32, /* cache line size */
443 4, /* l1 cache */
444 16, /* l2 cache */
445 1, /* streams */
448 /* Instruction costs on PPC405 processors. */
449 static const
450 struct processor_costs ppc405_cost = {
451 COSTS_N_INSNS (5), /* mulsi */
452 COSTS_N_INSNS (4), /* mulsi_const */
453 COSTS_N_INSNS (3), /* mulsi_const9 */
454 COSTS_N_INSNS (5), /* muldi */
455 COSTS_N_INSNS (35), /* divsi */
456 COSTS_N_INSNS (35), /* divdi */
457 COSTS_N_INSNS (11), /* fp */
458 COSTS_N_INSNS (11), /* dmul */
459 COSTS_N_INSNS (11), /* sdiv */
460 COSTS_N_INSNS (11), /* ddiv */
461 32, /* cache line size */
462 16, /* l1 cache */
463 128, /* l2 cache */
464 1, /* streams */
467 /* Instruction costs on PPC440 processors. */
468 static const
469 struct processor_costs ppc440_cost = {
470 COSTS_N_INSNS (3), /* mulsi */
471 COSTS_N_INSNS (2), /* mulsi_const */
472 COSTS_N_INSNS (2), /* mulsi_const9 */
473 COSTS_N_INSNS (3), /* muldi */
474 COSTS_N_INSNS (34), /* divsi */
475 COSTS_N_INSNS (34), /* divdi */
476 COSTS_N_INSNS (5), /* fp */
477 COSTS_N_INSNS (5), /* dmul */
478 COSTS_N_INSNS (19), /* sdiv */
479 COSTS_N_INSNS (33), /* ddiv */
480 32, /* cache line size */
481 32, /* l1 cache */
482 256, /* l2 cache */
483 1, /* streams */
486 /* Instruction costs on PPC476 processors. */
487 static const
488 struct processor_costs ppc476_cost = {
489 COSTS_N_INSNS (4), /* mulsi */
490 COSTS_N_INSNS (4), /* mulsi_const */
491 COSTS_N_INSNS (4), /* mulsi_const9 */
492 COSTS_N_INSNS (4), /* muldi */
493 COSTS_N_INSNS (11), /* divsi */
494 COSTS_N_INSNS (11), /* divdi */
495 COSTS_N_INSNS (6), /* fp */
496 COSTS_N_INSNS (6), /* dmul */
497 COSTS_N_INSNS (19), /* sdiv */
498 COSTS_N_INSNS (33), /* ddiv */
499 32, /* l1 cache line size */
500 32, /* l1 cache */
501 512, /* l2 cache */
502 1, /* streams */
505 /* Instruction costs on PPC601 processors. */
506 static const
507 struct processor_costs ppc601_cost = {
508 COSTS_N_INSNS (5), /* mulsi */
509 COSTS_N_INSNS (5), /* mulsi_const */
510 COSTS_N_INSNS (5), /* mulsi_const9 */
511 COSTS_N_INSNS (5), /* muldi */
512 COSTS_N_INSNS (36), /* divsi */
513 COSTS_N_INSNS (36), /* divdi */
514 COSTS_N_INSNS (4), /* fp */
515 COSTS_N_INSNS (5), /* dmul */
516 COSTS_N_INSNS (17), /* sdiv */
517 COSTS_N_INSNS (31), /* ddiv */
518 32, /* cache line size */
519 32, /* l1 cache */
520 256, /* l2 cache */
521 1, /* streams */
524 /* Instruction costs on PPC603 processors. */
525 static const
526 struct processor_costs ppc603_cost = {
527 COSTS_N_INSNS (5), /* mulsi */
528 COSTS_N_INSNS (3), /* mulsi_const */
529 COSTS_N_INSNS (2), /* mulsi_const9 */
530 COSTS_N_INSNS (5), /* muldi */
531 COSTS_N_INSNS (37), /* divsi */
532 COSTS_N_INSNS (37), /* divdi */
533 COSTS_N_INSNS (3), /* fp */
534 COSTS_N_INSNS (4), /* dmul */
535 COSTS_N_INSNS (18), /* sdiv */
536 COSTS_N_INSNS (33), /* ddiv */
537 32, /* cache line size */
538 8, /* l1 cache */
539 64, /* l2 cache */
540 1, /* streams */
543 /* Instruction costs on PPC604 processors. */
544 static const
545 struct processor_costs ppc604_cost = {
546 COSTS_N_INSNS (4), /* mulsi */
547 COSTS_N_INSNS (4), /* mulsi_const */
548 COSTS_N_INSNS (4), /* mulsi_const9 */
549 COSTS_N_INSNS (4), /* muldi */
550 COSTS_N_INSNS (20), /* divsi */
551 COSTS_N_INSNS (20), /* divdi */
552 COSTS_N_INSNS (3), /* fp */
553 COSTS_N_INSNS (3), /* dmul */
554 COSTS_N_INSNS (18), /* sdiv */
555 COSTS_N_INSNS (32), /* ddiv */
556 32, /* cache line size */
557 16, /* l1 cache */
558 512, /* l2 cache */
559 1, /* streams */
562 /* Instruction costs on PPC604e processors. */
563 static const
564 struct processor_costs ppc604e_cost = {
565 COSTS_N_INSNS (2), /* mulsi */
566 COSTS_N_INSNS (2), /* mulsi_const */
567 COSTS_N_INSNS (2), /* mulsi_const9 */
568 COSTS_N_INSNS (2), /* muldi */
569 COSTS_N_INSNS (20), /* divsi */
570 COSTS_N_INSNS (20), /* divdi */
571 COSTS_N_INSNS (3), /* fp */
572 COSTS_N_INSNS (3), /* dmul */
573 COSTS_N_INSNS (18), /* sdiv */
574 COSTS_N_INSNS (32), /* ddiv */
575 32, /* cache line size */
576 32, /* l1 cache */
577 1024, /* l2 cache */
578 1, /* streams */
581 /* Instruction costs on PPC620 processors. */
582 static const
583 struct processor_costs ppc620_cost = {
584 COSTS_N_INSNS (5), /* mulsi */
585 COSTS_N_INSNS (4), /* mulsi_const */
586 COSTS_N_INSNS (3), /* mulsi_const9 */
587 COSTS_N_INSNS (7), /* muldi */
588 COSTS_N_INSNS (21), /* divsi */
589 COSTS_N_INSNS (37), /* divdi */
590 COSTS_N_INSNS (3), /* fp */
591 COSTS_N_INSNS (3), /* dmul */
592 COSTS_N_INSNS (18), /* sdiv */
593 COSTS_N_INSNS (32), /* ddiv */
594 128, /* cache line size */
595 32, /* l1 cache */
596 1024, /* l2 cache */
597 1, /* streams */
600 /* Instruction costs on PPC630 processors. */
601 static const
602 struct processor_costs ppc630_cost = {
603 COSTS_N_INSNS (5), /* mulsi */
604 COSTS_N_INSNS (4), /* mulsi_const */
605 COSTS_N_INSNS (3), /* mulsi_const9 */
606 COSTS_N_INSNS (7), /* muldi */
607 COSTS_N_INSNS (21), /* divsi */
608 COSTS_N_INSNS (37), /* divdi */
609 COSTS_N_INSNS (3), /* fp */
610 COSTS_N_INSNS (3), /* dmul */
611 COSTS_N_INSNS (17), /* sdiv */
612 COSTS_N_INSNS (21), /* ddiv */
613 128, /* cache line size */
614 64, /* l1 cache */
615 1024, /* l2 cache */
616 1, /* streams */
619 /* Instruction costs on Cell processor. */
620 /* COSTS_N_INSNS (1) ~ one add. */
621 static const
622 struct processor_costs ppccell_cost = {
623 COSTS_N_INSNS (9/2)+2, /* mulsi */
624 COSTS_N_INSNS (6/2), /* mulsi_const */
625 COSTS_N_INSNS (6/2), /* mulsi_const9 */
626 COSTS_N_INSNS (15/2)+2, /* muldi */
627 COSTS_N_INSNS (38/2), /* divsi */
628 COSTS_N_INSNS (70/2), /* divdi */
629 COSTS_N_INSNS (10/2), /* fp */
630 COSTS_N_INSNS (10/2), /* dmul */
631 COSTS_N_INSNS (74/2), /* sdiv */
632 COSTS_N_INSNS (74/2), /* ddiv */
633 128, /* cache line size */
634 32, /* l1 cache */
635 512, /* l2 cache */
636 6, /* streams */
639 /* Instruction costs on PPC750 and PPC7400 processors. */
640 static const
641 struct processor_costs ppc750_cost = {
642 COSTS_N_INSNS (5), /* mulsi */
643 COSTS_N_INSNS (3), /* mulsi_const */
644 COSTS_N_INSNS (2), /* mulsi_const9 */
645 COSTS_N_INSNS (5), /* muldi */
646 COSTS_N_INSNS (17), /* divsi */
647 COSTS_N_INSNS (17), /* divdi */
648 COSTS_N_INSNS (3), /* fp */
649 COSTS_N_INSNS (3), /* dmul */
650 COSTS_N_INSNS (17), /* sdiv */
651 COSTS_N_INSNS (31), /* ddiv */
652 32, /* cache line size */
653 32, /* l1 cache */
654 512, /* l2 cache */
655 1, /* streams */
658 /* Instruction costs on PPC7450 processors. */
659 static const
660 struct processor_costs ppc7450_cost = {
661 COSTS_N_INSNS (4), /* mulsi */
662 COSTS_N_INSNS (3), /* mulsi_const */
663 COSTS_N_INSNS (3), /* mulsi_const9 */
664 COSTS_N_INSNS (4), /* muldi */
665 COSTS_N_INSNS (23), /* divsi */
666 COSTS_N_INSNS (23), /* divdi */
667 COSTS_N_INSNS (5), /* fp */
668 COSTS_N_INSNS (5), /* dmul */
669 COSTS_N_INSNS (21), /* sdiv */
670 COSTS_N_INSNS (35), /* ddiv */
671 32, /* cache line size */
672 32, /* l1 cache */
673 1024, /* l2 cache */
674 1, /* streams */
677 /* Instruction costs on PPC8540 processors. */
678 static const
679 struct processor_costs ppc8540_cost = {
680 COSTS_N_INSNS (4), /* mulsi */
681 COSTS_N_INSNS (4), /* mulsi_const */
682 COSTS_N_INSNS (4), /* mulsi_const9 */
683 COSTS_N_INSNS (4), /* muldi */
684 COSTS_N_INSNS (19), /* divsi */
685 COSTS_N_INSNS (19), /* divdi */
686 COSTS_N_INSNS (4), /* fp */
687 COSTS_N_INSNS (4), /* dmul */
688 COSTS_N_INSNS (29), /* sdiv */
689 COSTS_N_INSNS (29), /* ddiv */
690 32, /* cache line size */
691 32, /* l1 cache */
692 256, /* l2 cache */
693 1, /* prefetch streams /*/
696 /* Instruction costs on E300C2 and E300C3 cores. */
697 static const
698 struct processor_costs ppce300c2c3_cost = {
699 COSTS_N_INSNS (4), /* mulsi */
700 COSTS_N_INSNS (4), /* mulsi_const */
701 COSTS_N_INSNS (4), /* mulsi_const9 */
702 COSTS_N_INSNS (4), /* muldi */
703 COSTS_N_INSNS (19), /* divsi */
704 COSTS_N_INSNS (19), /* divdi */
705 COSTS_N_INSNS (3), /* fp */
706 COSTS_N_INSNS (4), /* dmul */
707 COSTS_N_INSNS (18), /* sdiv */
708 COSTS_N_INSNS (33), /* ddiv */
710 16, /* l1 cache */
711 16, /* l2 cache */
712 1, /* prefetch streams /*/
715 /* Instruction costs on PPCE500MC processors. */
716 static const
717 struct processor_costs ppce500mc_cost = {
718 COSTS_N_INSNS (4), /* mulsi */
719 COSTS_N_INSNS (4), /* mulsi_const */
720 COSTS_N_INSNS (4), /* mulsi_const9 */
721 COSTS_N_INSNS (4), /* muldi */
722 COSTS_N_INSNS (14), /* divsi */
723 COSTS_N_INSNS (14), /* divdi */
724 COSTS_N_INSNS (8), /* fp */
725 COSTS_N_INSNS (10), /* dmul */
726 COSTS_N_INSNS (36), /* sdiv */
727 COSTS_N_INSNS (66), /* ddiv */
728 64, /* cache line size */
729 32, /* l1 cache */
730 128, /* l2 cache */
731 1, /* prefetch streams /*/
734 /* Instruction costs on PPCE500MC64 processors. */
735 static const
736 struct processor_costs ppce500mc64_cost = {
737 COSTS_N_INSNS (4), /* mulsi */
738 COSTS_N_INSNS (4), /* mulsi_const */
739 COSTS_N_INSNS (4), /* mulsi_const9 */
740 COSTS_N_INSNS (4), /* muldi */
741 COSTS_N_INSNS (14), /* divsi */
742 COSTS_N_INSNS (14), /* divdi */
743 COSTS_N_INSNS (4), /* fp */
744 COSTS_N_INSNS (10), /* dmul */
745 COSTS_N_INSNS (36), /* sdiv */
746 COSTS_N_INSNS (66), /* ddiv */
747 64, /* cache line size */
748 32, /* l1 cache */
749 128, /* l2 cache */
750 1, /* prefetch streams /*/
753 /* Instruction costs on PPCE5500 processors. */
754 static const
755 struct processor_costs ppce5500_cost = {
756 COSTS_N_INSNS (5), /* mulsi */
757 COSTS_N_INSNS (5), /* mulsi_const */
758 COSTS_N_INSNS (4), /* mulsi_const9 */
759 COSTS_N_INSNS (5), /* muldi */
760 COSTS_N_INSNS (14), /* divsi */
761 COSTS_N_INSNS (14), /* divdi */
762 COSTS_N_INSNS (7), /* fp */
763 COSTS_N_INSNS (10), /* dmul */
764 COSTS_N_INSNS (36), /* sdiv */
765 COSTS_N_INSNS (66), /* ddiv */
766 64, /* cache line size */
767 32, /* l1 cache */
768 128, /* l2 cache */
769 1, /* prefetch streams /*/
772 /* Instruction costs on PPCE6500 processors. */
773 static const
774 struct processor_costs ppce6500_cost = {
775 COSTS_N_INSNS (5), /* mulsi */
776 COSTS_N_INSNS (5), /* mulsi_const */
777 COSTS_N_INSNS (4), /* mulsi_const9 */
778 COSTS_N_INSNS (5), /* muldi */
779 COSTS_N_INSNS (14), /* divsi */
780 COSTS_N_INSNS (14), /* divdi */
781 COSTS_N_INSNS (7), /* fp */
782 COSTS_N_INSNS (10), /* dmul */
783 COSTS_N_INSNS (36), /* sdiv */
784 COSTS_N_INSNS (66), /* ddiv */
785 64, /* cache line size */
786 32, /* l1 cache */
787 128, /* l2 cache */
788 1, /* prefetch streams /*/
791 /* Instruction costs on AppliedMicro Titan processors. */
792 static const
793 struct processor_costs titan_cost = {
794 COSTS_N_INSNS (5), /* mulsi */
795 COSTS_N_INSNS (5), /* mulsi_const */
796 COSTS_N_INSNS (5), /* mulsi_const9 */
797 COSTS_N_INSNS (5), /* muldi */
798 COSTS_N_INSNS (18), /* divsi */
799 COSTS_N_INSNS (18), /* divdi */
800 COSTS_N_INSNS (10), /* fp */
801 COSTS_N_INSNS (10), /* dmul */
802 COSTS_N_INSNS (46), /* sdiv */
803 COSTS_N_INSNS (72), /* ddiv */
804 32, /* cache line size */
805 32, /* l1 cache */
806 512, /* l2 cache */
807 1, /* prefetch streams /*/
810 /* Instruction costs on POWER4 and POWER5 processors. */
811 static const
812 struct processor_costs power4_cost = {
813 COSTS_N_INSNS (3), /* mulsi */
814 COSTS_N_INSNS (2), /* mulsi_const */
815 COSTS_N_INSNS (2), /* mulsi_const9 */
816 COSTS_N_INSNS (4), /* muldi */
817 COSTS_N_INSNS (18), /* divsi */
818 COSTS_N_INSNS (34), /* divdi */
819 COSTS_N_INSNS (3), /* fp */
820 COSTS_N_INSNS (3), /* dmul */
821 COSTS_N_INSNS (17), /* sdiv */
822 COSTS_N_INSNS (17), /* ddiv */
823 128, /* cache line size */
824 32, /* l1 cache */
825 1024, /* l2 cache */
826 8, /* prefetch streams /*/
829 /* Instruction costs on POWER6 processors. */
830 static const
831 struct processor_costs power6_cost = {
832 COSTS_N_INSNS (8), /* mulsi */
833 COSTS_N_INSNS (8), /* mulsi_const */
834 COSTS_N_INSNS (8), /* mulsi_const9 */
835 COSTS_N_INSNS (8), /* muldi */
836 COSTS_N_INSNS (22), /* divsi */
837 COSTS_N_INSNS (28), /* divdi */
838 COSTS_N_INSNS (3), /* fp */
839 COSTS_N_INSNS (3), /* dmul */
840 COSTS_N_INSNS (13), /* sdiv */
841 COSTS_N_INSNS (16), /* ddiv */
842 128, /* cache line size */
843 64, /* l1 cache */
844 2048, /* l2 cache */
845 16, /* prefetch streams */
848 /* Instruction costs on POWER7 processors. */
849 static const
850 struct processor_costs power7_cost = {
851 COSTS_N_INSNS (2), /* mulsi */
852 COSTS_N_INSNS (2), /* mulsi_const */
853 COSTS_N_INSNS (2), /* mulsi_const9 */
854 COSTS_N_INSNS (2), /* muldi */
855 COSTS_N_INSNS (18), /* divsi */
856 COSTS_N_INSNS (34), /* divdi */
857 COSTS_N_INSNS (3), /* fp */
858 COSTS_N_INSNS (3), /* dmul */
859 COSTS_N_INSNS (13), /* sdiv */
860 COSTS_N_INSNS (16), /* ddiv */
861 128, /* cache line size */
862 32, /* l1 cache */
863 256, /* l2 cache */
864 12, /* prefetch streams */
867 /* Instruction costs on POWER8 processors. */
868 static const
869 struct processor_costs power8_cost = {
870 COSTS_N_INSNS (3), /* mulsi */
871 COSTS_N_INSNS (3), /* mulsi_const */
872 COSTS_N_INSNS (3), /* mulsi_const9 */
873 COSTS_N_INSNS (3), /* muldi */
874 COSTS_N_INSNS (19), /* divsi */
875 COSTS_N_INSNS (35), /* divdi */
876 COSTS_N_INSNS (3), /* fp */
877 COSTS_N_INSNS (3), /* dmul */
878 COSTS_N_INSNS (14), /* sdiv */
879 COSTS_N_INSNS (17), /* ddiv */
880 128, /* cache line size */
881 32, /* l1 cache */
882 256, /* l2 cache */
883 12, /* prefetch streams */
886 /* Instruction costs on POWER A2 processors. */
887 static const
888 struct processor_costs ppca2_cost = {
889 COSTS_N_INSNS (16), /* mulsi */
890 COSTS_N_INSNS (16), /* mulsi_const */
891 COSTS_N_INSNS (16), /* mulsi_const9 */
892 COSTS_N_INSNS (16), /* muldi */
893 COSTS_N_INSNS (22), /* divsi */
894 COSTS_N_INSNS (28), /* divdi */
895 COSTS_N_INSNS (3), /* fp */
896 COSTS_N_INSNS (3), /* dmul */
897 COSTS_N_INSNS (59), /* sdiv */
898 COSTS_N_INSNS (72), /* ddiv */
900 16, /* l1 cache */
901 2048, /* l2 cache */
902 16, /* prefetch streams */
906 /* Table that classifies rs6000 builtin functions (pure, const, etc.). */
907 #undef RS6000_BUILTIN_1
908 #undef RS6000_BUILTIN_2
909 #undef RS6000_BUILTIN_3
910 #undef RS6000_BUILTIN_A
911 #undef RS6000_BUILTIN_D
912 #undef RS6000_BUILTIN_E
913 #undef RS6000_BUILTIN_H
914 #undef RS6000_BUILTIN_P
915 #undef RS6000_BUILTIN_Q
916 #undef RS6000_BUILTIN_S
917 #undef RS6000_BUILTIN_X
919 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE) \
920 { NAME, ICODE, MASK, ATTR },
922 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE) \
923 { NAME, ICODE, MASK, ATTR },
925 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE) \
926 { NAME, ICODE, MASK, ATTR },
928 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE) \
929 { NAME, ICODE, MASK, ATTR },
931 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE) \
932 { NAME, ICODE, MASK, ATTR },
934 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE) \
935 { NAME, ICODE, MASK, ATTR },
937 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE) \
938 { NAME, ICODE, MASK, ATTR },
940 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE) \
941 { NAME, ICODE, MASK, ATTR },
943 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE) \
944 { NAME, ICODE, MASK, ATTR },
946 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE) \
947 { NAME, ICODE, MASK, ATTR },
949 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE) \
950 { NAME, ICODE, MASK, ATTR },
952 struct rs6000_builtin_info_type {
953 const char *name;
954 const enum insn_code icode;
955 const HOST_WIDE_INT mask;
956 const unsigned attr;
959 static const struct rs6000_builtin_info_type rs6000_builtin_info[] =
961 #include "rs6000-builtin.def"
964 #undef RS6000_BUILTIN_1
965 #undef RS6000_BUILTIN_2
966 #undef RS6000_BUILTIN_3
967 #undef RS6000_BUILTIN_A
968 #undef RS6000_BUILTIN_D
969 #undef RS6000_BUILTIN_E
970 #undef RS6000_BUILTIN_H
971 #undef RS6000_BUILTIN_P
972 #undef RS6000_BUILTIN_Q
973 #undef RS6000_BUILTIN_S
974 #undef RS6000_BUILTIN_X
976 /* Support for -mveclibabi=<xxx> to control which vector library to use. */
977 static tree (*rs6000_veclib_handler) (tree, tree, tree);
980 static bool rs6000_debug_legitimate_address_p (enum machine_mode, rtx, bool);
981 static bool spe_func_has_64bit_regs_p (void);
982 static struct machine_function * rs6000_init_machine_status (void);
983 static int rs6000_ra_ever_killed (void);
984 static tree rs6000_handle_longcall_attribute (tree *, tree, tree, int, bool *);
985 static tree rs6000_handle_altivec_attribute (tree *, tree, tree, int, bool *);
986 static tree rs6000_handle_struct_attribute (tree *, tree, tree, int, bool *);
987 static tree rs6000_builtin_vectorized_libmass (tree, tree, tree);
988 static rtx rs6000_emit_set_long_const (rtx, HOST_WIDE_INT, HOST_WIDE_INT);
989 static int rs6000_memory_move_cost (enum machine_mode, reg_class_t, bool);
990 static bool rs6000_debug_rtx_costs (rtx, int, int, int, int *, bool);
991 static int rs6000_debug_address_cost (rtx, enum machine_mode, addr_space_t,
992 bool);
993 static int rs6000_debug_adjust_cost (rtx, rtx, rtx, int);
994 static bool is_microcoded_insn (rtx);
995 static bool is_nonpipeline_insn (rtx);
996 static bool is_cracked_insn (rtx);
997 static bool is_load_insn (rtx, rtx *);
998 static bool is_store_insn (rtx, rtx *);
999 static bool set_to_load_agen (rtx,rtx);
1000 static bool insn_terminates_group_p (rtx , enum group_termination);
1001 static bool insn_must_be_first_in_group (rtx);
1002 static bool insn_must_be_last_in_group (rtx);
1003 static void altivec_init_builtins (void);
1004 static tree builtin_function_type (enum machine_mode, enum machine_mode,
1005 enum machine_mode, enum machine_mode,
1006 enum rs6000_builtins, const char *name);
1007 static void rs6000_common_init_builtins (void);
1008 static void paired_init_builtins (void);
1009 static rtx paired_expand_predicate_builtin (enum insn_code, tree, rtx);
1010 static void spe_init_builtins (void);
1011 static void htm_init_builtins (void);
1012 static rtx spe_expand_predicate_builtin (enum insn_code, tree, rtx);
1013 static rtx spe_expand_evsel_builtin (enum insn_code, tree, rtx);
1014 static int rs6000_emit_int_cmove (rtx, rtx, rtx, rtx);
1015 static rs6000_stack_t *rs6000_stack_info (void);
1016 static void is_altivec_return_reg (rtx, void *);
1017 int easy_vector_constant (rtx, enum machine_mode);
1018 static rtx rs6000_debug_legitimize_address (rtx, rtx, enum machine_mode);
1019 static rtx rs6000_legitimize_tls_address (rtx, enum tls_model);
1020 static int rs6000_tls_symbol_ref_1 (rtx *, void *);
1021 static int rs6000_get_some_local_dynamic_name_1 (rtx *, void *);
1022 static rtx rs6000_darwin64_record_arg (CUMULATIVE_ARGS *, const_tree,
1023 bool, bool);
1024 #if TARGET_MACHO
1025 static void macho_branch_islands (void);
1026 #endif
1027 static rtx rs6000_legitimize_reload_address (rtx, enum machine_mode, int, int,
1028 int, int *);
1029 static rtx rs6000_debug_legitimize_reload_address (rtx, enum machine_mode, int,
1030 int, int, int *);
1031 static bool rs6000_mode_dependent_address (const_rtx);
1032 static bool rs6000_debug_mode_dependent_address (const_rtx);
1033 static enum reg_class rs6000_secondary_reload_class (enum reg_class,
1034 enum machine_mode, rtx);
1035 static enum reg_class rs6000_debug_secondary_reload_class (enum reg_class,
1036 enum machine_mode,
1037 rtx);
1038 static enum reg_class rs6000_preferred_reload_class (rtx, enum reg_class);
1039 static enum reg_class rs6000_debug_preferred_reload_class (rtx,
1040 enum reg_class);
1041 static bool rs6000_secondary_memory_needed (enum reg_class, enum reg_class,
1042 enum machine_mode);
1043 static bool rs6000_debug_secondary_memory_needed (enum reg_class,
1044 enum reg_class,
1045 enum machine_mode);
1046 static bool rs6000_cannot_change_mode_class (enum machine_mode,
1047 enum machine_mode,
1048 enum reg_class);
1049 static bool rs6000_debug_cannot_change_mode_class (enum machine_mode,
1050 enum machine_mode,
1051 enum reg_class);
1052 static bool rs6000_save_toc_in_prologue_p (void);
1054 rtx (*rs6000_legitimize_reload_address_ptr) (rtx, enum machine_mode, int, int,
1055 int, int *)
1056 = rs6000_legitimize_reload_address;
1058 static bool (*rs6000_mode_dependent_address_ptr) (const_rtx)
1059 = rs6000_mode_dependent_address;
1061 enum reg_class (*rs6000_secondary_reload_class_ptr) (enum reg_class,
1062 enum machine_mode, rtx)
1063 = rs6000_secondary_reload_class;
1065 enum reg_class (*rs6000_preferred_reload_class_ptr) (rtx, enum reg_class)
1066 = rs6000_preferred_reload_class;
1068 bool (*rs6000_secondary_memory_needed_ptr) (enum reg_class, enum reg_class,
1069 enum machine_mode)
1070 = rs6000_secondary_memory_needed;
1072 bool (*rs6000_cannot_change_mode_class_ptr) (enum machine_mode,
1073 enum machine_mode,
1074 enum reg_class)
1075 = rs6000_cannot_change_mode_class;
1077 const int INSN_NOT_AVAILABLE = -1;
1079 static void rs6000_print_isa_options (FILE *, int, const char *,
1080 HOST_WIDE_INT);
1081 static void rs6000_print_builtin_options (FILE *, int, const char *,
1082 HOST_WIDE_INT);
1084 static enum rs6000_reg_type register_to_reg_type (rtx, bool *);
1085 static bool rs6000_secondary_reload_move (enum rs6000_reg_type,
1086 enum rs6000_reg_type,
1087 enum machine_mode,
1088 secondary_reload_info *,
1089 bool);
1091 /* Hash table stuff for keeping track of TOC entries. */
1093 struct GTY(()) toc_hash_struct
1095 /* `key' will satisfy CONSTANT_P; in fact, it will satisfy
1096 ASM_OUTPUT_SPECIAL_POOL_ENTRY_P. */
1097 rtx key;
1098 enum machine_mode key_mode;
1099 int labelno;
1102 static GTY ((param_is (struct toc_hash_struct))) htab_t toc_hash_table;
1104 /* Hash table to keep track of the argument types for builtin functions. */
1106 struct GTY(()) builtin_hash_struct
1108 tree type;
1109 enum machine_mode mode[4]; /* return value + 3 arguments. */
1110 unsigned char uns_p[4]; /* and whether the types are unsigned. */
1113 static GTY ((param_is (struct builtin_hash_struct))) htab_t builtin_hash_table;
1116 /* Default register names. */
1117 char rs6000_reg_names[][8] =
1119 "0", "1", "2", "3", "4", "5", "6", "7",
1120 "8", "9", "10", "11", "12", "13", "14", "15",
1121 "16", "17", "18", "19", "20", "21", "22", "23",
1122 "24", "25", "26", "27", "28", "29", "30", "31",
1123 "0", "1", "2", "3", "4", "5", "6", "7",
1124 "8", "9", "10", "11", "12", "13", "14", "15",
1125 "16", "17", "18", "19", "20", "21", "22", "23",
1126 "24", "25", "26", "27", "28", "29", "30", "31",
1127 "mq", "lr", "ctr","ap",
1128 "0", "1", "2", "3", "4", "5", "6", "7",
1129 "ca",
1130 /* AltiVec registers. */
1131 "0", "1", "2", "3", "4", "5", "6", "7",
1132 "8", "9", "10", "11", "12", "13", "14", "15",
1133 "16", "17", "18", "19", "20", "21", "22", "23",
1134 "24", "25", "26", "27", "28", "29", "30", "31",
1135 "vrsave", "vscr",
1136 /* SPE registers. */
1137 "spe_acc", "spefscr",
1138 /* Soft frame pointer. */
1139 "sfp",
1140 /* HTM SPR registers. */
1141 "tfhar", "tfiar", "texasr"
1144 #ifdef TARGET_REGNAMES
1145 static const char alt_reg_names[][8] =
1147 "%r0", "%r1", "%r2", "%r3", "%r4", "%r5", "%r6", "%r7",
1148 "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15",
1149 "%r16", "%r17", "%r18", "%r19", "%r20", "%r21", "%r22", "%r23",
1150 "%r24", "%r25", "%r26", "%r27", "%r28", "%r29", "%r30", "%r31",
1151 "%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7",
1152 "%f8", "%f9", "%f10", "%f11", "%f12", "%f13", "%f14", "%f15",
1153 "%f16", "%f17", "%f18", "%f19", "%f20", "%f21", "%f22", "%f23",
1154 "%f24", "%f25", "%f26", "%f27", "%f28", "%f29", "%f30", "%f31",
1155 "mq", "lr", "ctr", "ap",
1156 "%cr0", "%cr1", "%cr2", "%cr3", "%cr4", "%cr5", "%cr6", "%cr7",
1157 "ca",
1158 /* AltiVec registers. */
1159 "%v0", "%v1", "%v2", "%v3", "%v4", "%v5", "%v6", "%v7",
1160 "%v8", "%v9", "%v10", "%v11", "%v12", "%v13", "%v14", "%v15",
1161 "%v16", "%v17", "%v18", "%v19", "%v20", "%v21", "%v22", "%v23",
1162 "%v24", "%v25", "%v26", "%v27", "%v28", "%v29", "%v30", "%v31",
1163 "vrsave", "vscr",
1164 /* SPE registers. */
1165 "spe_acc", "spefscr",
1166 /* Soft frame pointer. */
1167 "sfp",
1168 /* HTM SPR registers. */
1169 "tfhar", "tfiar", "texasr"
1171 #endif
1173 /* Table of valid machine attributes. */
1175 static const struct attribute_spec rs6000_attribute_table[] =
1177 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
1178 affects_type_identity } */
1179 { "altivec", 1, 1, false, true, false, rs6000_handle_altivec_attribute,
1180 false },
1181 { "longcall", 0, 0, false, true, true, rs6000_handle_longcall_attribute,
1182 false },
1183 { "shortcall", 0, 0, false, true, true, rs6000_handle_longcall_attribute,
1184 false },
1185 { "ms_struct", 0, 0, false, false, false, rs6000_handle_struct_attribute,
1186 false },
1187 { "gcc_struct", 0, 0, false, false, false, rs6000_handle_struct_attribute,
1188 false },
1189 #ifdef SUBTARGET_ATTRIBUTE_TABLE
1190 SUBTARGET_ATTRIBUTE_TABLE,
1191 #endif
1192 { NULL, 0, 0, false, false, false, NULL, false }
1195 #ifndef TARGET_PROFILE_KERNEL
1196 #define TARGET_PROFILE_KERNEL 0
1197 #endif
1199 /* The VRSAVE bitmask puts bit %v0 as the most significant bit. */
1200 #define ALTIVEC_REG_BIT(REGNO) (0x80000000 >> ((REGNO) - FIRST_ALTIVEC_REGNO))
1202 /* Initialize the GCC target structure. */
1203 #undef TARGET_ATTRIBUTE_TABLE
1204 #define TARGET_ATTRIBUTE_TABLE rs6000_attribute_table
1205 #undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
1206 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES rs6000_set_default_type_attributes
1207 #undef TARGET_ATTRIBUTE_TAKES_IDENTIFIER_P
1208 #define TARGET_ATTRIBUTE_TAKES_IDENTIFIER_P rs6000_attribute_takes_identifier_p
1210 #undef TARGET_ASM_ALIGNED_DI_OP
1211 #define TARGET_ASM_ALIGNED_DI_OP DOUBLE_INT_ASM_OP
1213 /* Default unaligned ops are only provided for ELF. Find the ops needed
1214 for non-ELF systems. */
1215 #ifndef OBJECT_FORMAT_ELF
1216 #if TARGET_XCOFF
1217 /* For XCOFF. rs6000_assemble_integer will handle unaligned DIs on
1218 64-bit targets. */
1219 #undef TARGET_ASM_UNALIGNED_HI_OP
1220 #define TARGET_ASM_UNALIGNED_HI_OP "\t.vbyte\t2,"
1221 #undef TARGET_ASM_UNALIGNED_SI_OP
1222 #define TARGET_ASM_UNALIGNED_SI_OP "\t.vbyte\t4,"
1223 #undef TARGET_ASM_UNALIGNED_DI_OP
1224 #define TARGET_ASM_UNALIGNED_DI_OP "\t.vbyte\t8,"
1225 #else
1226 /* For Darwin. */
1227 #undef TARGET_ASM_UNALIGNED_HI_OP
1228 #define TARGET_ASM_UNALIGNED_HI_OP "\t.short\t"
1229 #undef TARGET_ASM_UNALIGNED_SI_OP
1230 #define TARGET_ASM_UNALIGNED_SI_OP "\t.long\t"
1231 #undef TARGET_ASM_UNALIGNED_DI_OP
1232 #define TARGET_ASM_UNALIGNED_DI_OP "\t.quad\t"
1233 #undef TARGET_ASM_ALIGNED_DI_OP
1234 #define TARGET_ASM_ALIGNED_DI_OP "\t.quad\t"
1235 #endif
1236 #endif
1238 /* This hook deals with fixups for relocatable code and DI-mode objects
1239 in 64-bit code. */
1240 #undef TARGET_ASM_INTEGER
1241 #define TARGET_ASM_INTEGER rs6000_assemble_integer
1243 #if defined (HAVE_GAS_HIDDEN) && !TARGET_MACHO
1244 #undef TARGET_ASM_ASSEMBLE_VISIBILITY
1245 #define TARGET_ASM_ASSEMBLE_VISIBILITY rs6000_assemble_visibility
1246 #endif
1248 #undef TARGET_SET_UP_BY_PROLOGUE
1249 #define TARGET_SET_UP_BY_PROLOGUE rs6000_set_up_by_prologue
1251 #undef TARGET_HAVE_TLS
1252 #define TARGET_HAVE_TLS HAVE_AS_TLS
1254 #undef TARGET_CANNOT_FORCE_CONST_MEM
1255 #define TARGET_CANNOT_FORCE_CONST_MEM rs6000_cannot_force_const_mem
1257 #undef TARGET_DELEGITIMIZE_ADDRESS
1258 #define TARGET_DELEGITIMIZE_ADDRESS rs6000_delegitimize_address
1260 #undef TARGET_CONST_NOT_OK_FOR_DEBUG_P
1261 #define TARGET_CONST_NOT_OK_FOR_DEBUG_P rs6000_const_not_ok_for_debug_p
1263 #undef TARGET_ASM_FUNCTION_PROLOGUE
1264 #define TARGET_ASM_FUNCTION_PROLOGUE rs6000_output_function_prologue
1265 #undef TARGET_ASM_FUNCTION_EPILOGUE
1266 #define TARGET_ASM_FUNCTION_EPILOGUE rs6000_output_function_epilogue
1268 #undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
1269 #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA rs6000_output_addr_const_extra
1271 #undef TARGET_LEGITIMIZE_ADDRESS
1272 #define TARGET_LEGITIMIZE_ADDRESS rs6000_legitimize_address
1274 #undef TARGET_SCHED_VARIABLE_ISSUE
1275 #define TARGET_SCHED_VARIABLE_ISSUE rs6000_variable_issue
1277 #undef TARGET_SCHED_ISSUE_RATE
1278 #define TARGET_SCHED_ISSUE_RATE rs6000_issue_rate
1279 #undef TARGET_SCHED_ADJUST_COST
1280 #define TARGET_SCHED_ADJUST_COST rs6000_adjust_cost
1281 #undef TARGET_SCHED_ADJUST_PRIORITY
1282 #define TARGET_SCHED_ADJUST_PRIORITY rs6000_adjust_priority
1283 #undef TARGET_SCHED_IS_COSTLY_DEPENDENCE
1284 #define TARGET_SCHED_IS_COSTLY_DEPENDENCE rs6000_is_costly_dependence
1285 #undef TARGET_SCHED_INIT
1286 #define TARGET_SCHED_INIT rs6000_sched_init
1287 #undef TARGET_SCHED_FINISH
1288 #define TARGET_SCHED_FINISH rs6000_sched_finish
1289 #undef TARGET_SCHED_REORDER
1290 #define TARGET_SCHED_REORDER rs6000_sched_reorder
1291 #undef TARGET_SCHED_REORDER2
1292 #define TARGET_SCHED_REORDER2 rs6000_sched_reorder2
1294 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
1295 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD rs6000_use_sched_lookahead
1297 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD
1298 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD rs6000_use_sched_lookahead_guard
1300 #undef TARGET_SCHED_ALLOC_SCHED_CONTEXT
1301 #define TARGET_SCHED_ALLOC_SCHED_CONTEXT rs6000_alloc_sched_context
1302 #undef TARGET_SCHED_INIT_SCHED_CONTEXT
1303 #define TARGET_SCHED_INIT_SCHED_CONTEXT rs6000_init_sched_context
1304 #undef TARGET_SCHED_SET_SCHED_CONTEXT
1305 #define TARGET_SCHED_SET_SCHED_CONTEXT rs6000_set_sched_context
1306 #undef TARGET_SCHED_FREE_SCHED_CONTEXT
1307 #define TARGET_SCHED_FREE_SCHED_CONTEXT rs6000_free_sched_context
1309 #undef TARGET_VECTORIZE_BUILTIN_MASK_FOR_LOAD
1310 #define TARGET_VECTORIZE_BUILTIN_MASK_FOR_LOAD rs6000_builtin_mask_for_load
1311 #undef TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT
1312 #define TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT \
1313 rs6000_builtin_support_vector_misalignment
1314 #undef TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE
1315 #define TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE rs6000_vector_alignment_reachable
1316 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST
1317 #define TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST \
1318 rs6000_builtin_vectorization_cost
1319 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
1320 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE \
1321 rs6000_preferred_simd_mode
1322 #undef TARGET_VECTORIZE_INIT_COST
1323 #define TARGET_VECTORIZE_INIT_COST rs6000_init_cost
1324 #undef TARGET_VECTORIZE_ADD_STMT_COST
1325 #define TARGET_VECTORIZE_ADD_STMT_COST rs6000_add_stmt_cost
1326 #undef TARGET_VECTORIZE_FINISH_COST
1327 #define TARGET_VECTORIZE_FINISH_COST rs6000_finish_cost
1328 #undef TARGET_VECTORIZE_DESTROY_COST_DATA
1329 #define TARGET_VECTORIZE_DESTROY_COST_DATA rs6000_destroy_cost_data
1331 #undef TARGET_INIT_BUILTINS
1332 #define TARGET_INIT_BUILTINS rs6000_init_builtins
1333 #undef TARGET_BUILTIN_DECL
1334 #define TARGET_BUILTIN_DECL rs6000_builtin_decl
1336 #undef TARGET_EXPAND_BUILTIN
1337 #define TARGET_EXPAND_BUILTIN rs6000_expand_builtin
1339 #undef TARGET_MANGLE_TYPE
1340 #define TARGET_MANGLE_TYPE rs6000_mangle_type
1342 #undef TARGET_INIT_LIBFUNCS
1343 #define TARGET_INIT_LIBFUNCS rs6000_init_libfuncs
1345 #if TARGET_MACHO
1346 #undef TARGET_BINDS_LOCAL_P
1347 #define TARGET_BINDS_LOCAL_P darwin_binds_local_p
1348 #endif
1350 #undef TARGET_MS_BITFIELD_LAYOUT_P
1351 #define TARGET_MS_BITFIELD_LAYOUT_P rs6000_ms_bitfield_layout_p
1353 #undef TARGET_ASM_OUTPUT_MI_THUNK
1354 #define TARGET_ASM_OUTPUT_MI_THUNK rs6000_output_mi_thunk
1356 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
1357 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
1359 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
1360 #define TARGET_FUNCTION_OK_FOR_SIBCALL rs6000_function_ok_for_sibcall
1362 #undef TARGET_REGISTER_MOVE_COST
1363 #define TARGET_REGISTER_MOVE_COST rs6000_register_move_cost
1364 #undef TARGET_MEMORY_MOVE_COST
1365 #define TARGET_MEMORY_MOVE_COST rs6000_memory_move_cost
1366 #undef TARGET_RTX_COSTS
1367 #define TARGET_RTX_COSTS rs6000_rtx_costs
1368 #undef TARGET_ADDRESS_COST
1369 #define TARGET_ADDRESS_COST hook_int_rtx_mode_as_bool_0
1371 #undef TARGET_DWARF_REGISTER_SPAN
1372 #define TARGET_DWARF_REGISTER_SPAN rs6000_dwarf_register_span
1374 #undef TARGET_INIT_DWARF_REG_SIZES_EXTRA
1375 #define TARGET_INIT_DWARF_REG_SIZES_EXTRA rs6000_init_dwarf_reg_sizes_extra
1377 #undef TARGET_MEMBER_TYPE_FORCES_BLK
1378 #define TARGET_MEMBER_TYPE_FORCES_BLK rs6000_member_type_forces_blk
1380 /* On rs6000, function arguments are promoted, as are function return
1381 values. */
1382 #undef TARGET_PROMOTE_FUNCTION_MODE
1383 #define TARGET_PROMOTE_FUNCTION_MODE default_promote_function_mode_always_promote
1385 #undef TARGET_RETURN_IN_MEMORY
1386 #define TARGET_RETURN_IN_MEMORY rs6000_return_in_memory
1388 #undef TARGET_SETUP_INCOMING_VARARGS
1389 #define TARGET_SETUP_INCOMING_VARARGS setup_incoming_varargs
1391 /* Always strict argument naming on rs6000. */
1392 #undef TARGET_STRICT_ARGUMENT_NAMING
1393 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
1394 #undef TARGET_PRETEND_OUTGOING_VARARGS_NAMED
1395 #define TARGET_PRETEND_OUTGOING_VARARGS_NAMED hook_bool_CUMULATIVE_ARGS_true
1396 #undef TARGET_SPLIT_COMPLEX_ARG
1397 #define TARGET_SPLIT_COMPLEX_ARG hook_bool_const_tree_true
1398 #undef TARGET_MUST_PASS_IN_STACK
1399 #define TARGET_MUST_PASS_IN_STACK rs6000_must_pass_in_stack
1400 #undef TARGET_PASS_BY_REFERENCE
1401 #define TARGET_PASS_BY_REFERENCE rs6000_pass_by_reference
1402 #undef TARGET_ARG_PARTIAL_BYTES
1403 #define TARGET_ARG_PARTIAL_BYTES rs6000_arg_partial_bytes
1404 #undef TARGET_FUNCTION_ARG_ADVANCE
1405 #define TARGET_FUNCTION_ARG_ADVANCE rs6000_function_arg_advance
1406 #undef TARGET_FUNCTION_ARG
1407 #define TARGET_FUNCTION_ARG rs6000_function_arg
1408 #undef TARGET_FUNCTION_ARG_BOUNDARY
1409 #define TARGET_FUNCTION_ARG_BOUNDARY rs6000_function_arg_boundary
1411 #undef TARGET_BUILD_BUILTIN_VA_LIST
1412 #define TARGET_BUILD_BUILTIN_VA_LIST rs6000_build_builtin_va_list
1414 #undef TARGET_EXPAND_BUILTIN_VA_START
1415 #define TARGET_EXPAND_BUILTIN_VA_START rs6000_va_start
1417 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
1418 #define TARGET_GIMPLIFY_VA_ARG_EXPR rs6000_gimplify_va_arg
1420 #undef TARGET_EH_RETURN_FILTER_MODE
1421 #define TARGET_EH_RETURN_FILTER_MODE rs6000_eh_return_filter_mode
1423 #undef TARGET_SCALAR_MODE_SUPPORTED_P
1424 #define TARGET_SCALAR_MODE_SUPPORTED_P rs6000_scalar_mode_supported_p
1426 #undef TARGET_VECTOR_MODE_SUPPORTED_P
1427 #define TARGET_VECTOR_MODE_SUPPORTED_P rs6000_vector_mode_supported_p
1429 #undef TARGET_INVALID_ARG_FOR_UNPROTOTYPED_FN
1430 #define TARGET_INVALID_ARG_FOR_UNPROTOTYPED_FN invalid_arg_for_unprototyped_fn
1432 #undef TARGET_ASM_LOOP_ALIGN_MAX_SKIP
1433 #define TARGET_ASM_LOOP_ALIGN_MAX_SKIP rs6000_loop_align_max_skip
1435 #undef TARGET_OPTION_OVERRIDE
1436 #define TARGET_OPTION_OVERRIDE rs6000_option_override
1438 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION
1439 #define TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION \
1440 rs6000_builtin_vectorized_function
1442 #if !TARGET_MACHO
1443 #undef TARGET_STACK_PROTECT_FAIL
1444 #define TARGET_STACK_PROTECT_FAIL rs6000_stack_protect_fail
1445 #endif
1447 /* MPC604EUM 3.5.2 Weak Consistency between Multiple Processors
1448 The PowerPC architecture requires only weak consistency among
1449 processors--that is, memory accesses between processors need not be
1450 sequentially consistent and memory accesses among processors can occur
1451 in any order. The ability to order memory accesses weakly provides
1452 opportunities for more efficient use of the system bus. Unless a
1453 dependency exists, the 604e allows read operations to precede store
1454 operations. */
1455 #undef TARGET_RELAXED_ORDERING
1456 #define TARGET_RELAXED_ORDERING true
1458 #ifdef HAVE_AS_TLS
1459 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
1460 #define TARGET_ASM_OUTPUT_DWARF_DTPREL rs6000_output_dwarf_dtprel
1461 #endif
1463 /* Use a 32-bit anchor range. This leads to sequences like:
1465 addis tmp,anchor,high
1466 add dest,tmp,low
1468 where tmp itself acts as an anchor, and can be shared between
1469 accesses to the same 64k page. */
1470 #undef TARGET_MIN_ANCHOR_OFFSET
1471 #define TARGET_MIN_ANCHOR_OFFSET -0x7fffffff - 1
1472 #undef TARGET_MAX_ANCHOR_OFFSET
1473 #define TARGET_MAX_ANCHOR_OFFSET 0x7fffffff
1474 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
1475 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P rs6000_use_blocks_for_constant_p
1476 #undef TARGET_USE_BLOCKS_FOR_DECL_P
1477 #define TARGET_USE_BLOCKS_FOR_DECL_P rs6000_use_blocks_for_decl_p
1479 #undef TARGET_BUILTIN_RECIPROCAL
1480 #define TARGET_BUILTIN_RECIPROCAL rs6000_builtin_reciprocal
1482 #undef TARGET_EXPAND_TO_RTL_HOOK
1483 #define TARGET_EXPAND_TO_RTL_HOOK rs6000_alloc_sdmode_stack_slot
1485 #undef TARGET_INSTANTIATE_DECLS
1486 #define TARGET_INSTANTIATE_DECLS rs6000_instantiate_decls
1488 #undef TARGET_SECONDARY_RELOAD
1489 #define TARGET_SECONDARY_RELOAD rs6000_secondary_reload
1491 #undef TARGET_LEGITIMATE_ADDRESS_P
1492 #define TARGET_LEGITIMATE_ADDRESS_P rs6000_legitimate_address_p
1494 #undef TARGET_MODE_DEPENDENT_ADDRESS_P
1495 #define TARGET_MODE_DEPENDENT_ADDRESS_P rs6000_mode_dependent_address_p
1497 #undef TARGET_CAN_ELIMINATE
1498 #define TARGET_CAN_ELIMINATE rs6000_can_eliminate
1500 #undef TARGET_CONDITIONAL_REGISTER_USAGE
1501 #define TARGET_CONDITIONAL_REGISTER_USAGE rs6000_conditional_register_usage
1503 #undef TARGET_TRAMPOLINE_INIT
1504 #define TARGET_TRAMPOLINE_INIT rs6000_trampoline_init
1506 #undef TARGET_FUNCTION_VALUE
1507 #define TARGET_FUNCTION_VALUE rs6000_function_value
1509 #undef TARGET_OPTION_VALID_ATTRIBUTE_P
1510 #define TARGET_OPTION_VALID_ATTRIBUTE_P rs6000_valid_attribute_p
1512 #undef TARGET_OPTION_SAVE
1513 #define TARGET_OPTION_SAVE rs6000_function_specific_save
1515 #undef TARGET_OPTION_RESTORE
1516 #define TARGET_OPTION_RESTORE rs6000_function_specific_restore
1518 #undef TARGET_OPTION_PRINT
1519 #define TARGET_OPTION_PRINT rs6000_function_specific_print
1521 #undef TARGET_CAN_INLINE_P
1522 #define TARGET_CAN_INLINE_P rs6000_can_inline_p
1524 #undef TARGET_SET_CURRENT_FUNCTION
1525 #define TARGET_SET_CURRENT_FUNCTION rs6000_set_current_function
1527 #undef TARGET_LEGITIMATE_CONSTANT_P
1528 #define TARGET_LEGITIMATE_CONSTANT_P rs6000_legitimate_constant_p
1530 #undef TARGET_VECTORIZE_VEC_PERM_CONST_OK
1531 #define TARGET_VECTORIZE_VEC_PERM_CONST_OK rs6000_vectorize_vec_perm_const_ok
1534 /* Processor table. */
1535 struct rs6000_ptt
1537 const char *const name; /* Canonical processor name. */
1538 const enum processor_type processor; /* Processor type enum value. */
1539 const HOST_WIDE_INT target_enable; /* Target flags to enable. */
1542 static struct rs6000_ptt const processor_target_table[] =
1544 #define RS6000_CPU(NAME, CPU, FLAGS) { NAME, CPU, FLAGS },
1545 #include "rs6000-cpus.def"
1546 #undef RS6000_CPU
1549 /* Look up a processor name for -mcpu=xxx and -mtune=xxx. Return -1 if the
1550 name is invalid. */
1552 static int
1553 rs6000_cpu_name_lookup (const char *name)
1555 size_t i;
1557 if (name != NULL)
1559 for (i = 0; i < ARRAY_SIZE (processor_target_table); i++)
1560 if (! strcmp (name, processor_target_table[i].name))
1561 return (int)i;
1564 return -1;
1568 /* Return number of consecutive hard regs needed starting at reg REGNO
1569 to hold something of mode MODE.
1570 This is ordinarily the length in words of a value of mode MODE
1571 but can be less for certain modes in special long registers.
1573 For the SPE, GPRs are 64 bits but only 32 bits are visible in
1574 scalar instructions. The upper 32 bits are only available to the
1575 SIMD instructions.
1577 POWER and PowerPC GPRs hold 32 bits worth;
1578 PowerPC64 GPRs and FPRs point register holds 64 bits worth. */
1580 static int
1581 rs6000_hard_regno_nregs_internal (int regno, enum machine_mode mode)
1583 unsigned HOST_WIDE_INT reg_size;
1585 /* TF/TD modes are special in that they always take 2 registers. */
1586 if (FP_REGNO_P (regno))
1587 reg_size = ((VECTOR_MEM_VSX_P (mode) && mode != TDmode && mode != TFmode)
1588 ? UNITS_PER_VSX_WORD
1589 : UNITS_PER_FP_WORD);
1591 else if (SPE_SIMD_REGNO_P (regno) && TARGET_SPE && SPE_VECTOR_MODE (mode))
1592 reg_size = UNITS_PER_SPE_WORD;
1594 else if (ALTIVEC_REGNO_P (regno))
1595 reg_size = UNITS_PER_ALTIVEC_WORD;
1597 /* The value returned for SCmode in the E500 double case is 2 for
1598 ABI compatibility; storing an SCmode value in a single register
1599 would require function_arg and rs6000_spe_function_arg to handle
1600 SCmode so as to pass the value correctly in a pair of
1601 registers. */
1602 else if (TARGET_E500_DOUBLE && FLOAT_MODE_P (mode) && mode != SCmode
1603 && !DECIMAL_FLOAT_MODE_P (mode))
1604 reg_size = UNITS_PER_FP_WORD;
1606 else
1607 reg_size = UNITS_PER_WORD;
1609 return (GET_MODE_SIZE (mode) + reg_size - 1) / reg_size;
1612 /* Value is 1 if hard register REGNO can hold a value of machine-mode
1613 MODE. */
1614 static int
1615 rs6000_hard_regno_mode_ok (int regno, enum machine_mode mode)
1617 int last_regno = regno + rs6000_hard_regno_nregs[mode][regno] - 1;
1619 /* PTImode can only go in GPRs. Quad word memory operations require even/odd
1620 register combinations, and use PTImode where we need to deal with quad
1621 word memory operations. Don't allow quad words in the argument or frame
1622 pointer registers, just registers 0..31. */
1623 if (mode == PTImode)
1624 return (IN_RANGE (regno, FIRST_GPR_REGNO, LAST_GPR_REGNO)
1625 && IN_RANGE (last_regno, FIRST_GPR_REGNO, LAST_GPR_REGNO)
1626 && ((regno & 1) == 0));
1628 /* VSX registers that overlap the FPR registers are larger than for non-VSX
1629 implementations. Don't allow an item to be split between a FP register
1630 and an Altivec register. */
1631 if (VECTOR_MEM_VSX_P (mode))
1633 if (FP_REGNO_P (regno))
1634 return FP_REGNO_P (last_regno);
1636 if (ALTIVEC_REGNO_P (regno))
1637 return ALTIVEC_REGNO_P (last_regno);
1640 /* Allow TImode in all VSX registers if the user asked for it. */
1641 if (mode == TImode && TARGET_VSX_TIMODE && VSX_REGNO_P (regno))
1642 return 1;
1644 /* The GPRs can hold any mode, but values bigger than one register
1645 cannot go past R31. */
1646 if (INT_REGNO_P (regno))
1647 return INT_REGNO_P (last_regno);
1649 /* The float registers (except for VSX vector modes) can only hold floating
1650 modes and DImode. */
1651 if (FP_REGNO_P (regno))
1653 if (SCALAR_FLOAT_MODE_P (mode)
1654 && (mode != TDmode || (regno % 2) == 0)
1655 && FP_REGNO_P (last_regno))
1656 return 1;
1658 if (GET_MODE_CLASS (mode) == MODE_INT
1659 && GET_MODE_SIZE (mode) == UNITS_PER_FP_WORD)
1660 return 1;
1662 if (PAIRED_SIMD_REGNO_P (regno) && TARGET_PAIRED_FLOAT
1663 && PAIRED_VECTOR_MODE (mode))
1664 return 1;
1666 return 0;
1669 /* The CR register can only hold CC modes. */
1670 if (CR_REGNO_P (regno))
1671 return GET_MODE_CLASS (mode) == MODE_CC;
1673 if (CA_REGNO_P (regno))
1674 return mode == BImode;
1676 /* AltiVec only in AldyVec registers. */
1677 if (ALTIVEC_REGNO_P (regno))
1678 return VECTOR_MEM_ALTIVEC_OR_VSX_P (mode);
1680 /* ...but GPRs can hold SIMD data on the SPE in one register. */
1681 if (SPE_SIMD_REGNO_P (regno) && TARGET_SPE && SPE_VECTOR_MODE (mode))
1682 return 1;
1684 /* We cannot put non-VSX TImode or PTImode anywhere except general register
1685 and it must be able to fit within the register set. */
1687 return GET_MODE_SIZE (mode) <= UNITS_PER_WORD;
1690 /* Print interesting facts about registers. */
1691 static void
1692 rs6000_debug_reg_print (int first_regno, int last_regno, const char *reg_name)
1694 int r, m;
1696 for (r = first_regno; r <= last_regno; ++r)
1698 const char *comma = "";
1699 int len;
1701 if (first_regno == last_regno)
1702 fprintf (stderr, "%s:\t", reg_name);
1703 else
1704 fprintf (stderr, "%s%d:\t", reg_name, r - first_regno);
1706 len = 8;
1707 for (m = 0; m < NUM_MACHINE_MODES; ++m)
1708 if (rs6000_hard_regno_mode_ok_p[m][r] && rs6000_hard_regno_nregs[m][r])
1710 if (len > 70)
1712 fprintf (stderr, ",\n\t");
1713 len = 8;
1714 comma = "";
1717 if (rs6000_hard_regno_nregs[m][r] > 1)
1718 len += fprintf (stderr, "%s%s/%d", comma, GET_MODE_NAME (m),
1719 rs6000_hard_regno_nregs[m][r]);
1720 else
1721 len += fprintf (stderr, "%s%s", comma, GET_MODE_NAME (m));
1723 comma = ", ";
1726 if (call_used_regs[r])
1728 if (len > 70)
1730 fprintf (stderr, ",\n\t");
1731 len = 8;
1732 comma = "";
1735 len += fprintf (stderr, "%s%s", comma, "call-used");
1736 comma = ", ";
1739 if (fixed_regs[r])
1741 if (len > 70)
1743 fprintf (stderr, ",\n\t");
1744 len = 8;
1745 comma = "";
1748 len += fprintf (stderr, "%s%s", comma, "fixed");
1749 comma = ", ";
1752 if (len > 70)
1754 fprintf (stderr, ",\n\t");
1755 comma = "";
1758 len += fprintf (stderr, "%sreg-class = %s", comma,
1759 reg_class_names[(int)rs6000_regno_regclass[r]]);
1760 comma = ", ";
1762 if (len > 70)
1764 fprintf (stderr, ",\n\t");
1765 comma = "";
1768 fprintf (stderr, "%sregno = %d\n", comma, r);
1772 #define DEBUG_FMT_ID "%-32s= "
1773 #define DEBUG_FMT_D DEBUG_FMT_ID "%d\n"
1774 #define DEBUG_FMT_WX DEBUG_FMT_ID "%#.12" HOST_WIDE_INT_PRINT "x: "
1775 #define DEBUG_FMT_S DEBUG_FMT_ID "%s\n"
1777 /* Print various interesting information with -mdebug=reg. */
1778 static void
1779 rs6000_debug_reg_global (void)
1781 static const char *const tf[2] = { "false", "true" };
1782 const char *nl = (const char *)0;
1783 int m;
1784 size_t m1, m2, v;
1785 char costly_num[20];
1786 char nop_num[20];
1787 char flags_buffer[40];
1788 const char *costly_str;
1789 const char *nop_str;
1790 const char *trace_str;
1791 const char *abi_str;
1792 const char *cmodel_str;
1793 struct cl_target_option cl_opts;
1795 /* Map enum rs6000_vector to string. */
1796 static const char *rs6000_debug_vector_unit[] = {
1797 "none",
1798 "altivec",
1799 "vsx",
1800 "p8_vector",
1801 "paired",
1802 "spe",
1803 "other"
1806 /* Modes we want tieable information on. */
1807 static const enum machine_mode print_tieable_modes[] = {
1808 QImode,
1809 HImode,
1810 SImode,
1811 DImode,
1812 TImode,
1813 PTImode,
1814 SFmode,
1815 DFmode,
1816 TFmode,
1817 SDmode,
1818 DDmode,
1819 TDmode,
1820 V8QImode,
1821 V4HImode,
1822 V2SImode,
1823 V16QImode,
1824 V8HImode,
1825 V4SImode,
1826 V2DImode,
1827 V32QImode,
1828 V16HImode,
1829 V8SImode,
1830 V4DImode,
1831 V2SFmode,
1832 V4SFmode,
1833 V2DFmode,
1834 V8SFmode,
1835 V4DFmode,
1836 CCmode,
1837 CCUNSmode,
1838 CCEQmode,
1841 /* Virtual regs we are interested in. */
1842 const static struct {
1843 int regno; /* register number. */
1844 const char *name; /* register name. */
1845 } virtual_regs[] = {
1846 { STACK_POINTER_REGNUM, "stack pointer:" },
1847 { TOC_REGNUM, "toc: " },
1848 { STATIC_CHAIN_REGNUM, "static chain: " },
1849 { RS6000_PIC_OFFSET_TABLE_REGNUM, "pic offset: " },
1850 { HARD_FRAME_POINTER_REGNUM, "hard frame: " },
1851 { ARG_POINTER_REGNUM, "arg pointer: " },
1852 { FRAME_POINTER_REGNUM, "frame pointer:" },
1853 { FIRST_PSEUDO_REGISTER, "first pseudo: " },
1854 { FIRST_VIRTUAL_REGISTER, "first virtual:" },
1855 { VIRTUAL_INCOMING_ARGS_REGNUM, "incoming_args:" },
1856 { VIRTUAL_STACK_VARS_REGNUM, "stack_vars: " },
1857 { VIRTUAL_STACK_DYNAMIC_REGNUM, "stack_dynamic:" },
1858 { VIRTUAL_OUTGOING_ARGS_REGNUM, "outgoing_args:" },
1859 { VIRTUAL_CFA_REGNUM, "cfa (frame): " },
1860 { VIRTUAL_PREFERRED_STACK_BOUNDARY_REGNUM, "stack boundry:" },
1861 { LAST_VIRTUAL_REGISTER, "last virtual: " },
1864 fputs ("\nHard register information:\n", stderr);
1865 rs6000_debug_reg_print (FIRST_GPR_REGNO, LAST_GPR_REGNO, "gr");
1866 rs6000_debug_reg_print (FIRST_FPR_REGNO, LAST_FPR_REGNO, "fp");
1867 rs6000_debug_reg_print (FIRST_ALTIVEC_REGNO,
1868 LAST_ALTIVEC_REGNO,
1869 "vs");
1870 rs6000_debug_reg_print (LR_REGNO, LR_REGNO, "lr");
1871 rs6000_debug_reg_print (CTR_REGNO, CTR_REGNO, "ctr");
1872 rs6000_debug_reg_print (CR0_REGNO, CR7_REGNO, "cr");
1873 rs6000_debug_reg_print (CA_REGNO, CA_REGNO, "ca");
1874 rs6000_debug_reg_print (VRSAVE_REGNO, VRSAVE_REGNO, "vrsave");
1875 rs6000_debug_reg_print (VSCR_REGNO, VSCR_REGNO, "vscr");
1876 rs6000_debug_reg_print (SPE_ACC_REGNO, SPE_ACC_REGNO, "spe_a");
1877 rs6000_debug_reg_print (SPEFSCR_REGNO, SPEFSCR_REGNO, "spe_f");
1879 fputs ("\nVirtual/stack/frame registers:\n", stderr);
1880 for (v = 0; v < ARRAY_SIZE (virtual_regs); v++)
1881 fprintf (stderr, "%s regno = %3d\n", virtual_regs[v].name, virtual_regs[v].regno);
1883 fprintf (stderr,
1884 "\n"
1885 "d reg_class = %s\n"
1886 "f reg_class = %s\n"
1887 "v reg_class = %s\n"
1888 "wa reg_class = %s\n"
1889 "wd reg_class = %s\n"
1890 "wf reg_class = %s\n"
1891 "wg reg_class = %s\n"
1892 "wl reg_class = %s\n"
1893 "wm reg_class = %s\n"
1894 "wr reg_class = %s\n"
1895 "ws reg_class = %s\n"
1896 "wt reg_class = %s\n"
1897 "wv reg_class = %s\n"
1898 "wx reg_class = %s\n"
1899 "wz reg_class = %s\n"
1900 "\n",
1901 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_d]],
1902 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_f]],
1903 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_v]],
1904 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wa]],
1905 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wd]],
1906 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wf]],
1907 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wg]],
1908 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wl]],
1909 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wm]],
1910 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wr]],
1911 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_ws]],
1912 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wt]],
1913 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wv]],
1914 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wx]],
1915 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wz]]);
1917 for (m = 0; m < NUM_MACHINE_MODES; ++m)
1918 if (rs6000_vector_unit[m] || rs6000_vector_mem[m]
1919 || (rs6000_vector_reload[m][0] != CODE_FOR_nothing)
1920 || (rs6000_vector_reload[m][1] != CODE_FOR_nothing))
1922 nl = "\n";
1923 fprintf (stderr,
1924 "Vector mode: %-5s arithmetic: %-10s move: %-10s "
1925 "reload-out: %c reload-in: %c\n",
1926 GET_MODE_NAME (m),
1927 rs6000_debug_vector_unit[ rs6000_vector_unit[m] ],
1928 rs6000_debug_vector_unit[ rs6000_vector_mem[m] ],
1929 (rs6000_vector_reload[m][0] != CODE_FOR_nothing) ? 'y' : 'n',
1930 (rs6000_vector_reload[m][1] != CODE_FOR_nothing) ? 'y' : 'n');
1933 if (nl)
1934 fputs (nl, stderr);
1936 for (m1 = 0; m1 < ARRAY_SIZE (print_tieable_modes); m1++)
1938 enum machine_mode mode1 = print_tieable_modes[m1];
1939 bool first_time = true;
1941 nl = (const char *)0;
1942 for (m2 = 0; m2 < ARRAY_SIZE (print_tieable_modes); m2++)
1944 enum machine_mode mode2 = print_tieable_modes[m2];
1945 if (mode1 != mode2 && MODES_TIEABLE_P (mode1, mode2))
1947 if (first_time)
1949 fprintf (stderr, "Tieable modes %s:", GET_MODE_NAME (mode1));
1950 nl = "\n";
1951 first_time = false;
1954 fprintf (stderr, " %s", GET_MODE_NAME (mode2));
1958 if (!first_time)
1959 fputs ("\n", stderr);
1962 if (nl)
1963 fputs (nl, stderr);
1965 if (rs6000_recip_control)
1967 fprintf (stderr, "\nReciprocal mask = 0x%x\n", rs6000_recip_control);
1969 for (m = 0; m < NUM_MACHINE_MODES; ++m)
1970 if (rs6000_recip_bits[m])
1972 fprintf (stderr,
1973 "Reciprocal estimate mode: %-5s divide: %s rsqrt: %s\n",
1974 GET_MODE_NAME (m),
1975 (RS6000_RECIP_AUTO_RE_P (m)
1976 ? "auto"
1977 : (RS6000_RECIP_HAVE_RE_P (m) ? "have" : "none")),
1978 (RS6000_RECIP_AUTO_RSQRTE_P (m)
1979 ? "auto"
1980 : (RS6000_RECIP_HAVE_RSQRTE_P (m) ? "have" : "none")));
1983 fputs ("\n", stderr);
1986 if (rs6000_cpu_index >= 0)
1988 const char *name = processor_target_table[rs6000_cpu_index].name;
1989 HOST_WIDE_INT flags
1990 = processor_target_table[rs6000_cpu_index].target_enable;
1992 sprintf (flags_buffer, "-mcpu=%s flags", name);
1993 rs6000_print_isa_options (stderr, 0, flags_buffer, flags);
1995 else
1996 fprintf (stderr, DEBUG_FMT_S, "cpu", "<none>");
1998 if (rs6000_tune_index >= 0)
2000 const char *name = processor_target_table[rs6000_tune_index].name;
2001 HOST_WIDE_INT flags
2002 = processor_target_table[rs6000_tune_index].target_enable;
2004 sprintf (flags_buffer, "-mtune=%s flags", name);
2005 rs6000_print_isa_options (stderr, 0, flags_buffer, flags);
2007 else
2008 fprintf (stderr, DEBUG_FMT_S, "tune", "<none>");
2010 cl_target_option_save (&cl_opts, &global_options);
2011 rs6000_print_isa_options (stderr, 0, "rs6000_isa_flags",
2012 rs6000_isa_flags);
2014 rs6000_print_isa_options (stderr, 0, "rs6000_isa_flags_explicit",
2015 rs6000_isa_flags_explicit);
2017 rs6000_print_builtin_options (stderr, 0, "rs6000_builtin_mask",
2018 rs6000_builtin_mask);
2020 rs6000_print_isa_options (stderr, 0, "TARGET_DEFAULT", TARGET_DEFAULT);
2022 fprintf (stderr, DEBUG_FMT_S, "--with-cpu default",
2023 OPTION_TARGET_CPU_DEFAULT ? OPTION_TARGET_CPU_DEFAULT : "<none>");
2025 switch (rs6000_sched_costly_dep)
2027 case max_dep_latency:
2028 costly_str = "max_dep_latency";
2029 break;
2031 case no_dep_costly:
2032 costly_str = "no_dep_costly";
2033 break;
2035 case all_deps_costly:
2036 costly_str = "all_deps_costly";
2037 break;
2039 case true_store_to_load_dep_costly:
2040 costly_str = "true_store_to_load_dep_costly";
2041 break;
2043 case store_to_load_dep_costly:
2044 costly_str = "store_to_load_dep_costly";
2045 break;
2047 default:
2048 costly_str = costly_num;
2049 sprintf (costly_num, "%d", (int)rs6000_sched_costly_dep);
2050 break;
2053 fprintf (stderr, DEBUG_FMT_S, "sched_costly_dep", costly_str);
2055 switch (rs6000_sched_insert_nops)
2057 case sched_finish_regroup_exact:
2058 nop_str = "sched_finish_regroup_exact";
2059 break;
2061 case sched_finish_pad_groups:
2062 nop_str = "sched_finish_pad_groups";
2063 break;
2065 case sched_finish_none:
2066 nop_str = "sched_finish_none";
2067 break;
2069 default:
2070 nop_str = nop_num;
2071 sprintf (nop_num, "%d", (int)rs6000_sched_insert_nops);
2072 break;
2075 fprintf (stderr, DEBUG_FMT_S, "sched_insert_nops", nop_str);
2077 switch (rs6000_sdata)
2079 default:
2080 case SDATA_NONE:
2081 break;
2083 case SDATA_DATA:
2084 fprintf (stderr, DEBUG_FMT_S, "sdata", "data");
2085 break;
2087 case SDATA_SYSV:
2088 fprintf (stderr, DEBUG_FMT_S, "sdata", "sysv");
2089 break;
2091 case SDATA_EABI:
2092 fprintf (stderr, DEBUG_FMT_S, "sdata", "eabi");
2093 break;
2097 switch (rs6000_traceback)
2099 case traceback_default: trace_str = "default"; break;
2100 case traceback_none: trace_str = "none"; break;
2101 case traceback_part: trace_str = "part"; break;
2102 case traceback_full: trace_str = "full"; break;
2103 default: trace_str = "unknown"; break;
2106 fprintf (stderr, DEBUG_FMT_S, "traceback", trace_str);
2108 switch (rs6000_current_cmodel)
2110 case CMODEL_SMALL: cmodel_str = "small"; break;
2111 case CMODEL_MEDIUM: cmodel_str = "medium"; break;
2112 case CMODEL_LARGE: cmodel_str = "large"; break;
2113 default: cmodel_str = "unknown"; break;
2116 fprintf (stderr, DEBUG_FMT_S, "cmodel", cmodel_str);
2118 switch (rs6000_current_abi)
2120 case ABI_NONE: abi_str = "none"; break;
2121 case ABI_AIX: abi_str = "aix"; break;
2122 case ABI_V4: abi_str = "V4"; break;
2123 case ABI_DARWIN: abi_str = "darwin"; break;
2124 default: abi_str = "unknown"; break;
2127 fprintf (stderr, DEBUG_FMT_S, "abi", abi_str);
2129 if (rs6000_altivec_abi)
2130 fprintf (stderr, DEBUG_FMT_S, "altivec_abi", "true");
2132 if (rs6000_spe_abi)
2133 fprintf (stderr, DEBUG_FMT_S, "spe_abi", "true");
2135 if (rs6000_darwin64_abi)
2136 fprintf (stderr, DEBUG_FMT_S, "darwin64_abi", "true");
2138 if (rs6000_float_gprs)
2139 fprintf (stderr, DEBUG_FMT_S, "float_gprs", "true");
2141 if (TARGET_LINK_STACK)
2142 fprintf (stderr, DEBUG_FMT_S, "link_stack", "true");
2144 if (targetm.lra_p ())
2145 fprintf (stderr, DEBUG_FMT_S, "lra", "true");
2147 if (TARGET_P8_FUSION)
2148 fprintf (stderr, DEBUG_FMT_S, "p8 fusion",
2149 (TARGET_P8_FUSION_SIGN) ? "zero+sign" : "zero");
2151 fprintf (stderr, DEBUG_FMT_S, "plt-format",
2152 TARGET_SECURE_PLT ? "secure" : "bss");
2153 fprintf (stderr, DEBUG_FMT_S, "struct-return",
2154 aix_struct_return ? "aix" : "sysv");
2155 fprintf (stderr, DEBUG_FMT_S, "always_hint", tf[!!rs6000_always_hint]);
2156 fprintf (stderr, DEBUG_FMT_S, "sched_groups", tf[!!rs6000_sched_groups]);
2157 fprintf (stderr, DEBUG_FMT_S, "align_branch",
2158 tf[!!rs6000_align_branch_targets]);
2159 fprintf (stderr, DEBUG_FMT_D, "tls_size", rs6000_tls_size);
2160 fprintf (stderr, DEBUG_FMT_D, "long_double_size",
2161 rs6000_long_double_type_size);
2162 fprintf (stderr, DEBUG_FMT_D, "sched_restricted_insns_priority",
2163 (int)rs6000_sched_restricted_insns_priority);
2164 fprintf (stderr, DEBUG_FMT_D, "Number of standard builtins",
2165 (int)END_BUILTINS);
2166 fprintf (stderr, DEBUG_FMT_D, "Number of rs6000 builtins",
2167 (int)RS6000_BUILTIN_COUNT);
2170 /* Initialize the various global tables that are based on register size. */
2171 static void
2172 rs6000_init_hard_regno_mode_ok (bool global_init_p)
2174 int r, m, c;
2175 int align64;
2176 int align32;
2178 /* Precalculate REGNO_REG_CLASS. */
2179 rs6000_regno_regclass[0] = GENERAL_REGS;
2180 for (r = 1; r < 32; ++r)
2181 rs6000_regno_regclass[r] = BASE_REGS;
2183 for (r = 32; r < 64; ++r)
2184 rs6000_regno_regclass[r] = FLOAT_REGS;
2186 for (r = 64; r < FIRST_PSEUDO_REGISTER; ++r)
2187 rs6000_regno_regclass[r] = NO_REGS;
2189 for (r = FIRST_ALTIVEC_REGNO; r <= LAST_ALTIVEC_REGNO; ++r)
2190 rs6000_regno_regclass[r] = ALTIVEC_REGS;
2192 rs6000_regno_regclass[CR0_REGNO] = CR0_REGS;
2193 for (r = CR1_REGNO; r <= CR7_REGNO; ++r)
2194 rs6000_regno_regclass[r] = CR_REGS;
2196 rs6000_regno_regclass[LR_REGNO] = LINK_REGS;
2197 rs6000_regno_regclass[CTR_REGNO] = CTR_REGS;
2198 rs6000_regno_regclass[CA_REGNO] = CA_REGS;
2199 rs6000_regno_regclass[VRSAVE_REGNO] = VRSAVE_REGS;
2200 rs6000_regno_regclass[VSCR_REGNO] = VRSAVE_REGS;
2201 rs6000_regno_regclass[SPE_ACC_REGNO] = SPE_ACC_REGS;
2202 rs6000_regno_regclass[SPEFSCR_REGNO] = SPEFSCR_REGS;
2203 rs6000_regno_regclass[TFHAR_REGNO] = SPR_REGS;
2204 rs6000_regno_regclass[TFIAR_REGNO] = SPR_REGS;
2205 rs6000_regno_regclass[TEXASR_REGNO] = SPR_REGS;
2206 rs6000_regno_regclass[ARG_POINTER_REGNUM] = BASE_REGS;
2207 rs6000_regno_regclass[FRAME_POINTER_REGNUM] = BASE_REGS;
2209 /* Precalculate register class to simpler reload register class. We don't
2210 need all of the register classes that are combinations of different
2211 classes, just the simple ones that have constraint letters. */
2212 for (c = 0; c < N_REG_CLASSES; c++)
2213 reg_class_to_reg_type[c] = NO_REG_TYPE;
2215 reg_class_to_reg_type[(int)GENERAL_REGS] = GPR_REG_TYPE;
2216 reg_class_to_reg_type[(int)BASE_REGS] = GPR_REG_TYPE;
2217 reg_class_to_reg_type[(int)VSX_REGS] = VSX_REG_TYPE;
2218 reg_class_to_reg_type[(int)VRSAVE_REGS] = SPR_REG_TYPE;
2219 reg_class_to_reg_type[(int)VSCR_REGS] = SPR_REG_TYPE;
2220 reg_class_to_reg_type[(int)LINK_REGS] = SPR_REG_TYPE;
2221 reg_class_to_reg_type[(int)CTR_REGS] = SPR_REG_TYPE;
2222 reg_class_to_reg_type[(int)LINK_OR_CTR_REGS] = SPR_REG_TYPE;
2223 reg_class_to_reg_type[(int)CR_REGS] = CR_REG_TYPE;
2224 reg_class_to_reg_type[(int)CR0_REGS] = CR_REG_TYPE;
2225 reg_class_to_reg_type[(int)SPE_ACC_REGS] = SPE_ACC_TYPE;
2226 reg_class_to_reg_type[(int)SPEFSCR_REGS] = SPEFSCR_REG_TYPE;
2228 if (TARGET_VSX)
2230 reg_class_to_reg_type[(int)FLOAT_REGS] = VSX_REG_TYPE;
2231 reg_class_to_reg_type[(int)ALTIVEC_REGS] = VSX_REG_TYPE;
2233 else
2235 reg_class_to_reg_type[(int)FLOAT_REGS] = FPR_REG_TYPE;
2236 reg_class_to_reg_type[(int)ALTIVEC_REGS] = ALTIVEC_REG_TYPE;
2239 /* Precalculate vector information, this must be set up before the
2240 rs6000_hard_regno_nregs_internal below. */
2241 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2243 rs6000_vector_unit[m] = rs6000_vector_mem[m] = VECTOR_NONE;
2244 rs6000_vector_reload[m][0] = CODE_FOR_nothing;
2245 rs6000_vector_reload[m][1] = CODE_FOR_nothing;
2248 for (c = 0; c < (int)(int)RS6000_CONSTRAINT_MAX; c++)
2249 rs6000_constraints[c] = NO_REGS;
2251 /* The VSX hardware allows native alignment for vectors, but control whether the compiler
2252 believes it can use native alignment or still uses 128-bit alignment. */
2253 if (TARGET_VSX && !TARGET_VSX_ALIGN_128)
2255 align64 = 64;
2256 align32 = 32;
2258 else
2260 align64 = 128;
2261 align32 = 128;
2264 /* V2DF mode, VSX only. */
2265 if (TARGET_VSX)
2267 rs6000_vector_unit[V2DFmode] = VECTOR_VSX;
2268 rs6000_vector_mem[V2DFmode] = VECTOR_VSX;
2269 rs6000_vector_align[V2DFmode] = align64;
2272 /* V4SF mode, either VSX or Altivec. */
2273 if (TARGET_VSX)
2275 rs6000_vector_unit[V4SFmode] = VECTOR_VSX;
2276 rs6000_vector_mem[V4SFmode] = VECTOR_VSX;
2277 rs6000_vector_align[V4SFmode] = align32;
2279 else if (TARGET_ALTIVEC)
2281 rs6000_vector_unit[V4SFmode] = VECTOR_ALTIVEC;
2282 rs6000_vector_mem[V4SFmode] = VECTOR_ALTIVEC;
2283 rs6000_vector_align[V4SFmode] = align32;
2286 /* V16QImode, V8HImode, V4SImode are Altivec only, but possibly do VSX loads
2287 and stores. */
2288 if (TARGET_ALTIVEC)
2290 rs6000_vector_unit[V4SImode] = VECTOR_ALTIVEC;
2291 rs6000_vector_unit[V8HImode] = VECTOR_ALTIVEC;
2292 rs6000_vector_unit[V16QImode] = VECTOR_ALTIVEC;
2293 rs6000_vector_align[V4SImode] = align32;
2294 rs6000_vector_align[V8HImode] = align32;
2295 rs6000_vector_align[V16QImode] = align32;
2297 if (TARGET_VSX)
2299 rs6000_vector_mem[V4SImode] = VECTOR_VSX;
2300 rs6000_vector_mem[V8HImode] = VECTOR_VSX;
2301 rs6000_vector_mem[V16QImode] = VECTOR_VSX;
2303 else
2305 rs6000_vector_mem[V4SImode] = VECTOR_ALTIVEC;
2306 rs6000_vector_mem[V8HImode] = VECTOR_ALTIVEC;
2307 rs6000_vector_mem[V16QImode] = VECTOR_ALTIVEC;
2311 /* V2DImode, full mode depends on ISA 2.07 vector mode. Allow under VSX to
2312 do insert/splat/extract. Altivec doesn't have 64-bit integer support. */
2313 if (TARGET_VSX)
2315 rs6000_vector_mem[V2DImode] = VECTOR_VSX;
2316 rs6000_vector_unit[V2DImode]
2317 = (TARGET_P8_VECTOR) ? VECTOR_P8_VECTOR : VECTOR_NONE;
2318 rs6000_vector_align[V2DImode] = align64;
2321 /* DFmode, see if we want to use the VSX unit. */
2322 if (TARGET_VSX && TARGET_VSX_SCALAR_DOUBLE)
2324 rs6000_vector_unit[DFmode] = VECTOR_VSX;
2325 rs6000_vector_mem[DFmode]
2326 = (TARGET_VSX_SCALAR_MEMORY ? VECTOR_VSX : VECTOR_NONE);
2327 rs6000_vector_align[DFmode] = align64;
2330 /* Allow TImode in VSX register and set the VSX memory macros. */
2331 if (TARGET_VSX && TARGET_VSX_TIMODE)
2333 rs6000_vector_mem[TImode] = VECTOR_VSX;
2334 rs6000_vector_align[TImode] = align64;
2337 /* TODO add SPE and paired floating point vector support. */
2339 /* Register class constraints for the constraints that depend on compile
2340 switches. */
2341 if (TARGET_HARD_FLOAT && TARGET_FPRS)
2342 rs6000_constraints[RS6000_CONSTRAINT_f] = FLOAT_REGS;
2344 if (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
2345 rs6000_constraints[RS6000_CONSTRAINT_d] = FLOAT_REGS;
2347 if (TARGET_VSX)
2349 /* At present, we just use VSX_REGS, but we have different constraints
2350 based on the use, in case we want to fine tune the default register
2351 class used. wa = any VSX register, wf = register class to use for
2352 V4SF, wd = register class to use for V2DF, and ws = register classs to
2353 use for DF scalars. */
2354 rs6000_constraints[RS6000_CONSTRAINT_wa] = VSX_REGS;
2355 rs6000_constraints[RS6000_CONSTRAINT_wf] = VSX_REGS;
2356 rs6000_constraints[RS6000_CONSTRAINT_wd] = VSX_REGS;
2357 rs6000_constraints[RS6000_CONSTRAINT_ws] = (TARGET_VSX_SCALAR_MEMORY
2358 ? VSX_REGS
2359 : FLOAT_REGS);
2360 if (TARGET_VSX_TIMODE)
2361 rs6000_constraints[RS6000_CONSTRAINT_wt] = VSX_REGS;
2364 /* Add conditional constraints based on various options, to allow us to
2365 collapse multiple insn patterns. */
2366 if (TARGET_ALTIVEC)
2367 rs6000_constraints[RS6000_CONSTRAINT_v] = ALTIVEC_REGS;
2369 if (TARGET_MFPGPR)
2370 rs6000_constraints[RS6000_CONSTRAINT_wg] = FLOAT_REGS;
2372 if (TARGET_LFIWAX)
2373 rs6000_constraints[RS6000_CONSTRAINT_wl] = FLOAT_REGS;
2375 if (TARGET_DIRECT_MOVE)
2376 rs6000_constraints[RS6000_CONSTRAINT_wm] = VSX_REGS;
2378 if (TARGET_POWERPC64)
2379 rs6000_constraints[RS6000_CONSTRAINT_wr] = GENERAL_REGS;
2381 if (TARGET_P8_VECTOR)
2382 rs6000_constraints[RS6000_CONSTRAINT_wv] = ALTIVEC_REGS;
2384 if (TARGET_STFIWX)
2385 rs6000_constraints[RS6000_CONSTRAINT_wx] = FLOAT_REGS;
2387 if (TARGET_LFIWZX)
2388 rs6000_constraints[RS6000_CONSTRAINT_wz] = FLOAT_REGS;
2390 /* Setup the direct move combinations. */
2391 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2393 reload_fpr_gpr[m] = CODE_FOR_nothing;
2394 reload_gpr_vsx[m] = CODE_FOR_nothing;
2395 reload_vsx_gpr[m] = CODE_FOR_nothing;
2398 /* Set up the reload helper and direct move functions. */
2399 if (TARGET_VSX || TARGET_ALTIVEC)
2401 if (TARGET_64BIT)
2403 rs6000_vector_reload[V16QImode][0] = CODE_FOR_reload_v16qi_di_store;
2404 rs6000_vector_reload[V16QImode][1] = CODE_FOR_reload_v16qi_di_load;
2405 rs6000_vector_reload[V8HImode][0] = CODE_FOR_reload_v8hi_di_store;
2406 rs6000_vector_reload[V8HImode][1] = CODE_FOR_reload_v8hi_di_load;
2407 rs6000_vector_reload[V4SImode][0] = CODE_FOR_reload_v4si_di_store;
2408 rs6000_vector_reload[V4SImode][1] = CODE_FOR_reload_v4si_di_load;
2409 rs6000_vector_reload[V2DImode][0] = CODE_FOR_reload_v2di_di_store;
2410 rs6000_vector_reload[V2DImode][1] = CODE_FOR_reload_v2di_di_load;
2411 rs6000_vector_reload[V4SFmode][0] = CODE_FOR_reload_v4sf_di_store;
2412 rs6000_vector_reload[V4SFmode][1] = CODE_FOR_reload_v4sf_di_load;
2413 rs6000_vector_reload[V2DFmode][0] = CODE_FOR_reload_v2df_di_store;
2414 rs6000_vector_reload[V2DFmode][1] = CODE_FOR_reload_v2df_di_load;
2415 if (TARGET_VSX && TARGET_VSX_SCALAR_MEMORY)
2417 rs6000_vector_reload[DFmode][0] = CODE_FOR_reload_df_di_store;
2418 rs6000_vector_reload[DFmode][1] = CODE_FOR_reload_df_di_load;
2419 rs6000_vector_reload[DDmode][0] = CODE_FOR_reload_dd_di_store;
2420 rs6000_vector_reload[DDmode][1] = CODE_FOR_reload_dd_di_load;
2422 if (TARGET_P8_VECTOR)
2424 rs6000_vector_reload[SFmode][0] = CODE_FOR_reload_sf_di_store;
2425 rs6000_vector_reload[SFmode][1] = CODE_FOR_reload_sf_di_load;
2426 rs6000_vector_reload[SDmode][0] = CODE_FOR_reload_sd_di_store;
2427 rs6000_vector_reload[SDmode][1] = CODE_FOR_reload_sd_di_load;
2429 if (TARGET_VSX_TIMODE)
2431 rs6000_vector_reload[TImode][0] = CODE_FOR_reload_ti_di_store;
2432 rs6000_vector_reload[TImode][1] = CODE_FOR_reload_ti_di_load;
2434 if (TARGET_DIRECT_MOVE)
2436 if (TARGET_POWERPC64)
2438 reload_gpr_vsx[TImode] = CODE_FOR_reload_gpr_from_vsxti;
2439 reload_gpr_vsx[V2DFmode] = CODE_FOR_reload_gpr_from_vsxv2df;
2440 reload_gpr_vsx[V2DImode] = CODE_FOR_reload_gpr_from_vsxv2di;
2441 reload_gpr_vsx[V4SFmode] = CODE_FOR_reload_gpr_from_vsxv4sf;
2442 reload_gpr_vsx[V4SImode] = CODE_FOR_reload_gpr_from_vsxv4si;
2443 reload_gpr_vsx[V8HImode] = CODE_FOR_reload_gpr_from_vsxv8hi;
2444 reload_gpr_vsx[V16QImode] = CODE_FOR_reload_gpr_from_vsxv16qi;
2445 reload_gpr_vsx[SFmode] = CODE_FOR_reload_gpr_from_vsxsf;
2447 reload_vsx_gpr[TImode] = CODE_FOR_reload_vsx_from_gprti;
2448 reload_vsx_gpr[V2DFmode] = CODE_FOR_reload_vsx_from_gprv2df;
2449 reload_vsx_gpr[V2DImode] = CODE_FOR_reload_vsx_from_gprv2di;
2450 reload_vsx_gpr[V4SFmode] = CODE_FOR_reload_vsx_from_gprv4sf;
2451 reload_vsx_gpr[V4SImode] = CODE_FOR_reload_vsx_from_gprv4si;
2452 reload_vsx_gpr[V8HImode] = CODE_FOR_reload_vsx_from_gprv8hi;
2453 reload_vsx_gpr[V16QImode] = CODE_FOR_reload_vsx_from_gprv16qi;
2454 reload_vsx_gpr[SFmode] = CODE_FOR_reload_vsx_from_gprsf;
2456 else
2458 reload_fpr_gpr[DImode] = CODE_FOR_reload_fpr_from_gprdi;
2459 reload_fpr_gpr[DDmode] = CODE_FOR_reload_fpr_from_gprdd;
2460 reload_fpr_gpr[DFmode] = CODE_FOR_reload_fpr_from_gprdf;
2464 else
2466 rs6000_vector_reload[V16QImode][0] = CODE_FOR_reload_v16qi_si_store;
2467 rs6000_vector_reload[V16QImode][1] = CODE_FOR_reload_v16qi_si_load;
2468 rs6000_vector_reload[V8HImode][0] = CODE_FOR_reload_v8hi_si_store;
2469 rs6000_vector_reload[V8HImode][1] = CODE_FOR_reload_v8hi_si_load;
2470 rs6000_vector_reload[V4SImode][0] = CODE_FOR_reload_v4si_si_store;
2471 rs6000_vector_reload[V4SImode][1] = CODE_FOR_reload_v4si_si_load;
2472 rs6000_vector_reload[V2DImode][0] = CODE_FOR_reload_v2di_si_store;
2473 rs6000_vector_reload[V2DImode][1] = CODE_FOR_reload_v2di_si_load;
2474 rs6000_vector_reload[V4SFmode][0] = CODE_FOR_reload_v4sf_si_store;
2475 rs6000_vector_reload[V4SFmode][1] = CODE_FOR_reload_v4sf_si_load;
2476 rs6000_vector_reload[V2DFmode][0] = CODE_FOR_reload_v2df_si_store;
2477 rs6000_vector_reload[V2DFmode][1] = CODE_FOR_reload_v2df_si_load;
2478 if (TARGET_VSX && TARGET_VSX_SCALAR_MEMORY)
2480 rs6000_vector_reload[DFmode][0] = CODE_FOR_reload_df_si_store;
2481 rs6000_vector_reload[DFmode][1] = CODE_FOR_reload_df_si_load;
2482 rs6000_vector_reload[DDmode][0] = CODE_FOR_reload_dd_si_store;
2483 rs6000_vector_reload[DDmode][1] = CODE_FOR_reload_dd_si_load;
2485 if (TARGET_P8_VECTOR)
2487 rs6000_vector_reload[SFmode][0] = CODE_FOR_reload_sf_si_store;
2488 rs6000_vector_reload[SFmode][1] = CODE_FOR_reload_sf_si_load;
2489 rs6000_vector_reload[SDmode][0] = CODE_FOR_reload_sd_si_store;
2490 rs6000_vector_reload[SDmode][1] = CODE_FOR_reload_sd_si_load;
2492 if (TARGET_VSX_TIMODE)
2494 rs6000_vector_reload[TImode][0] = CODE_FOR_reload_ti_si_store;
2495 rs6000_vector_reload[TImode][1] = CODE_FOR_reload_ti_si_load;
2500 /* Precalculate HARD_REGNO_NREGS. */
2501 for (r = 0; r < FIRST_PSEUDO_REGISTER; ++r)
2502 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2503 rs6000_hard_regno_nregs[m][r]
2504 = rs6000_hard_regno_nregs_internal (r, (enum machine_mode)m);
2506 /* Precalculate HARD_REGNO_MODE_OK. */
2507 for (r = 0; r < FIRST_PSEUDO_REGISTER; ++r)
2508 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2509 if (rs6000_hard_regno_mode_ok (r, (enum machine_mode)m))
2510 rs6000_hard_regno_mode_ok_p[m][r] = true;
2512 /* Precalculate CLASS_MAX_NREGS sizes. */
2513 for (c = 0; c < LIM_REG_CLASSES; ++c)
2515 int reg_size;
2517 if (TARGET_VSX && VSX_REG_CLASS_P (c))
2518 reg_size = UNITS_PER_VSX_WORD;
2520 else if (c == ALTIVEC_REGS)
2521 reg_size = UNITS_PER_ALTIVEC_WORD;
2523 else if (c == FLOAT_REGS)
2524 reg_size = UNITS_PER_FP_WORD;
2526 else
2527 reg_size = UNITS_PER_WORD;
2529 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2531 int reg_size2 = reg_size;
2533 /* TFmode/TDmode always takes 2 registers, even in VSX. */
2534 if (TARGET_VSX && VSX_REG_CLASS_P (c)
2535 && (m == TDmode || m == TFmode))
2536 reg_size2 = UNITS_PER_FP_WORD;
2538 rs6000_class_max_nregs[m][c]
2539 = (GET_MODE_SIZE (m) + reg_size2 - 1) / reg_size2;
2543 if (TARGET_E500_DOUBLE)
2544 rs6000_class_max_nregs[DFmode][GENERAL_REGS] = 1;
2546 /* Calculate which modes to automatically generate code to use a the
2547 reciprocal divide and square root instructions. In the future, possibly
2548 automatically generate the instructions even if the user did not specify
2549 -mrecip. The older machines double precision reciprocal sqrt estimate is
2550 not accurate enough. */
2551 memset (rs6000_recip_bits, 0, sizeof (rs6000_recip_bits));
2552 if (TARGET_FRES)
2553 rs6000_recip_bits[SFmode] = RS6000_RECIP_MASK_HAVE_RE;
2554 if (TARGET_FRE)
2555 rs6000_recip_bits[DFmode] = RS6000_RECIP_MASK_HAVE_RE;
2556 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode))
2557 rs6000_recip_bits[V4SFmode] = RS6000_RECIP_MASK_HAVE_RE;
2558 if (VECTOR_UNIT_VSX_P (V2DFmode))
2559 rs6000_recip_bits[V2DFmode] = RS6000_RECIP_MASK_HAVE_RE;
2561 if (TARGET_FRSQRTES)
2562 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2563 if (TARGET_FRSQRTE)
2564 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2565 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode))
2566 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2567 if (VECTOR_UNIT_VSX_P (V2DFmode))
2568 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2570 if (rs6000_recip_control)
2572 if (!flag_finite_math_only)
2573 warning (0, "-mrecip requires -ffinite-math or -ffast-math");
2574 if (flag_trapping_math)
2575 warning (0, "-mrecip requires -fno-trapping-math or -ffast-math");
2576 if (!flag_reciprocal_math)
2577 warning (0, "-mrecip requires -freciprocal-math or -ffast-math");
2578 if (flag_finite_math_only && !flag_trapping_math && flag_reciprocal_math)
2580 if (RS6000_RECIP_HAVE_RE_P (SFmode)
2581 && (rs6000_recip_control & RECIP_SF_DIV) != 0)
2582 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2584 if (RS6000_RECIP_HAVE_RE_P (DFmode)
2585 && (rs6000_recip_control & RECIP_DF_DIV) != 0)
2586 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2588 if (RS6000_RECIP_HAVE_RE_P (V4SFmode)
2589 && (rs6000_recip_control & RECIP_V4SF_DIV) != 0)
2590 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2592 if (RS6000_RECIP_HAVE_RE_P (V2DFmode)
2593 && (rs6000_recip_control & RECIP_V2DF_DIV) != 0)
2594 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2596 if (RS6000_RECIP_HAVE_RSQRTE_P (SFmode)
2597 && (rs6000_recip_control & RECIP_SF_RSQRT) != 0)
2598 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2600 if (RS6000_RECIP_HAVE_RSQRTE_P (DFmode)
2601 && (rs6000_recip_control & RECIP_DF_RSQRT) != 0)
2602 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2604 if (RS6000_RECIP_HAVE_RSQRTE_P (V4SFmode)
2605 && (rs6000_recip_control & RECIP_V4SF_RSQRT) != 0)
2606 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2608 if (RS6000_RECIP_HAVE_RSQRTE_P (V2DFmode)
2609 && (rs6000_recip_control & RECIP_V2DF_RSQRT) != 0)
2610 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2614 if (global_init_p || TARGET_DEBUG_TARGET)
2616 if (TARGET_DEBUG_REG)
2617 rs6000_debug_reg_global ();
2619 if (TARGET_DEBUG_COST || TARGET_DEBUG_REG)
2620 fprintf (stderr,
2621 "SImode variable mult cost = %d\n"
2622 "SImode constant mult cost = %d\n"
2623 "SImode short constant mult cost = %d\n"
2624 "DImode multipliciation cost = %d\n"
2625 "SImode division cost = %d\n"
2626 "DImode division cost = %d\n"
2627 "Simple fp operation cost = %d\n"
2628 "DFmode multiplication cost = %d\n"
2629 "SFmode division cost = %d\n"
2630 "DFmode division cost = %d\n"
2631 "cache line size = %d\n"
2632 "l1 cache size = %d\n"
2633 "l2 cache size = %d\n"
2634 "simultaneous prefetches = %d\n"
2635 "\n",
2636 rs6000_cost->mulsi,
2637 rs6000_cost->mulsi_const,
2638 rs6000_cost->mulsi_const9,
2639 rs6000_cost->muldi,
2640 rs6000_cost->divsi,
2641 rs6000_cost->divdi,
2642 rs6000_cost->fp,
2643 rs6000_cost->dmul,
2644 rs6000_cost->sdiv,
2645 rs6000_cost->ddiv,
2646 rs6000_cost->cache_line_size,
2647 rs6000_cost->l1_cache_size,
2648 rs6000_cost->l2_cache_size,
2649 rs6000_cost->simultaneous_prefetches);
2653 #if TARGET_MACHO
2654 /* The Darwin version of SUBTARGET_OVERRIDE_OPTIONS. */
2656 static void
2657 darwin_rs6000_override_options (void)
2659 /* The Darwin ABI always includes AltiVec, can't be (validly) turned
2660 off. */
2661 rs6000_altivec_abi = 1;
2662 TARGET_ALTIVEC_VRSAVE = 1;
2663 rs6000_current_abi = ABI_DARWIN;
2665 if (DEFAULT_ABI == ABI_DARWIN
2666 && TARGET_64BIT)
2667 darwin_one_byte_bool = 1;
2669 if (TARGET_64BIT && ! TARGET_POWERPC64)
2671 rs6000_isa_flags |= OPTION_MASK_POWERPC64;
2672 warning (0, "-m64 requires PowerPC64 architecture, enabling");
2674 if (flag_mkernel)
2676 rs6000_default_long_calls = 1;
2677 rs6000_isa_flags |= OPTION_MASK_SOFT_FLOAT;
2680 /* Make -m64 imply -maltivec. Darwin's 64-bit ABI includes
2681 Altivec. */
2682 if (!flag_mkernel && !flag_apple_kext
2683 && TARGET_64BIT
2684 && ! (rs6000_isa_flags_explicit & OPTION_MASK_ALTIVEC))
2685 rs6000_isa_flags |= OPTION_MASK_ALTIVEC;
2687 /* Unless the user (not the configurer) has explicitly overridden
2688 it with -mcpu=G3 or -mno-altivec, then 10.5+ targets default to
2689 G4 unless targeting the kernel. */
2690 if (!flag_mkernel
2691 && !flag_apple_kext
2692 && strverscmp (darwin_macosx_version_min, "10.5") >= 0
2693 && ! (rs6000_isa_flags_explicit & OPTION_MASK_ALTIVEC)
2694 && ! global_options_set.x_rs6000_cpu_index)
2696 rs6000_isa_flags |= OPTION_MASK_ALTIVEC;
2699 #endif
2701 /* If not otherwise specified by a target, make 'long double' equivalent to
2702 'double'. */
2704 #ifndef RS6000_DEFAULT_LONG_DOUBLE_SIZE
2705 #define RS6000_DEFAULT_LONG_DOUBLE_SIZE 64
2706 #endif
2708 /* Return the builtin mask of the various options used that could affect which
2709 builtins were used. In the past we used target_flags, but we've run out of
2710 bits, and some options like SPE and PAIRED are no longer in
2711 target_flags. */
2713 HOST_WIDE_INT
2714 rs6000_builtin_mask_calculate (void)
2716 return (((TARGET_ALTIVEC) ? RS6000_BTM_ALTIVEC : 0)
2717 | ((TARGET_VSX) ? RS6000_BTM_VSX : 0)
2718 | ((TARGET_SPE) ? RS6000_BTM_SPE : 0)
2719 | ((TARGET_PAIRED_FLOAT) ? RS6000_BTM_PAIRED : 0)
2720 | ((TARGET_FRE) ? RS6000_BTM_FRE : 0)
2721 | ((TARGET_FRES) ? RS6000_BTM_FRES : 0)
2722 | ((TARGET_FRSQRTE) ? RS6000_BTM_FRSQRTE : 0)
2723 | ((TARGET_FRSQRTES) ? RS6000_BTM_FRSQRTES : 0)
2724 | ((TARGET_POPCNTD) ? RS6000_BTM_POPCNTD : 0)
2725 | ((rs6000_cpu == PROCESSOR_CELL) ? RS6000_BTM_CELL : 0)
2726 | ((TARGET_P8_VECTOR) ? RS6000_BTM_P8_VECTOR : 0)
2727 | ((TARGET_CRYPTO) ? RS6000_BTM_CRYPTO : 0)
2728 | ((TARGET_HTM) ? RS6000_BTM_HTM : 0));
2731 /* Override command line options. Mostly we process the processor type and
2732 sometimes adjust other TARGET_ options. */
2734 static bool
2735 rs6000_option_override_internal (bool global_init_p)
2737 bool ret = true;
2738 bool have_cpu = false;
2740 /* The default cpu requested at configure time, if any. */
2741 const char *implicit_cpu = OPTION_TARGET_CPU_DEFAULT;
2743 HOST_WIDE_INT set_masks;
2744 int cpu_index;
2745 int tune_index;
2746 struct cl_target_option *main_target_opt
2747 = ((global_init_p || target_option_default_node == NULL)
2748 ? NULL : TREE_TARGET_OPTION (target_option_default_node));
2750 /* On 64-bit Darwin, power alignment is ABI-incompatible with some C
2751 library functions, so warn about it. The flag may be useful for
2752 performance studies from time to time though, so don't disable it
2753 entirely. */
2754 if (global_options_set.x_rs6000_alignment_flags
2755 && rs6000_alignment_flags == MASK_ALIGN_POWER
2756 && DEFAULT_ABI == ABI_DARWIN
2757 && TARGET_64BIT)
2758 warning (0, "-malign-power is not supported for 64-bit Darwin;"
2759 " it is incompatible with the installed C and C++ libraries");
2761 /* Numerous experiment shows that IRA based loop pressure
2762 calculation works better for RTL loop invariant motion on targets
2763 with enough (>= 32) registers. It is an expensive optimization.
2764 So it is on only for peak performance. */
2765 if (optimize >= 3 && global_init_p)
2766 flag_ira_loop_pressure = 1;
2768 /* Set the pointer size. */
2769 if (TARGET_64BIT)
2771 rs6000_pmode = (int)DImode;
2772 rs6000_pointer_size = 64;
2774 else
2776 rs6000_pmode = (int)SImode;
2777 rs6000_pointer_size = 32;
2780 /* Some OSs don't support saving the high part of 64-bit registers on context
2781 switch. Other OSs don't support saving Altivec registers. On those OSs,
2782 we don't touch the OPTION_MASK_POWERPC64 or OPTION_MASK_ALTIVEC settings;
2783 if the user wants either, the user must explicitly specify them and we
2784 won't interfere with the user's specification. */
2786 set_masks = POWERPC_MASKS;
2787 #ifdef OS_MISSING_POWERPC64
2788 if (OS_MISSING_POWERPC64)
2789 set_masks &= ~OPTION_MASK_POWERPC64;
2790 #endif
2791 #ifdef OS_MISSING_ALTIVEC
2792 if (OS_MISSING_ALTIVEC)
2793 set_masks &= ~(OPTION_MASK_ALTIVEC | OPTION_MASK_VSX);
2794 #endif
2796 /* Don't override by the processor default if given explicitly. */
2797 set_masks &= ~rs6000_isa_flags_explicit;
2799 /* Process the -mcpu=<xxx> and -mtune=<xxx> argument. If the user changed
2800 the cpu in a target attribute or pragma, but did not specify a tuning
2801 option, use the cpu for the tuning option rather than the option specified
2802 with -mtune on the command line. Process a '--with-cpu' configuration
2803 request as an implicit --cpu. */
2804 if (rs6000_cpu_index >= 0)
2806 cpu_index = rs6000_cpu_index;
2807 have_cpu = true;
2809 else if (main_target_opt != NULL && main_target_opt->x_rs6000_cpu_index >= 0)
2811 rs6000_cpu_index = cpu_index = main_target_opt->x_rs6000_cpu_index;
2812 have_cpu = true;
2814 else if (implicit_cpu)
2816 rs6000_cpu_index = cpu_index = rs6000_cpu_name_lookup (implicit_cpu);
2817 have_cpu = true;
2819 else
2821 const char *default_cpu = (TARGET_POWERPC64 ? "powerpc64" : "powerpc");
2822 rs6000_cpu_index = cpu_index = rs6000_cpu_name_lookup (default_cpu);
2823 have_cpu = false;
2826 gcc_assert (cpu_index >= 0);
2828 /* If we have a cpu, either through an explicit -mcpu=<xxx> or if the
2829 compiler was configured with --with-cpu=<xxx>, replace all of the ISA bits
2830 with those from the cpu, except for options that were explicitly set. If
2831 we don't have a cpu, do not override the target bits set in
2832 TARGET_DEFAULT. */
2833 if (have_cpu)
2835 rs6000_isa_flags &= ~set_masks;
2836 rs6000_isa_flags |= (processor_target_table[cpu_index].target_enable
2837 & set_masks);
2839 else
2840 rs6000_isa_flags |= (processor_target_table[cpu_index].target_enable
2841 & ~rs6000_isa_flags_explicit);
2843 /* If no -mcpu=<xxx>, inherit any default options that were cleared via
2844 POWERPC_MASKS. Originally, TARGET_DEFAULT was used to initialize
2845 target_flags via the TARGET_DEFAULT_TARGET_FLAGS hook. When we switched
2846 to using rs6000_isa_flags, we need to do the initialization here. */
2847 if (!have_cpu)
2848 rs6000_isa_flags |= (TARGET_DEFAULT & ~rs6000_isa_flags_explicit);
2850 if (rs6000_tune_index >= 0)
2851 tune_index = rs6000_tune_index;
2852 else if (have_cpu)
2853 rs6000_tune_index = tune_index = cpu_index;
2854 else
2856 size_t i;
2857 enum processor_type tune_proc
2858 = (TARGET_POWERPC64 ? PROCESSOR_DEFAULT64 : PROCESSOR_DEFAULT);
2860 tune_index = -1;
2861 for (i = 0; i < ARRAY_SIZE (processor_target_table); i++)
2862 if (processor_target_table[i].processor == tune_proc)
2864 rs6000_tune_index = tune_index = i;
2865 break;
2869 gcc_assert (tune_index >= 0);
2870 rs6000_cpu = processor_target_table[tune_index].processor;
2872 /* Pick defaults for SPE related control flags. Do this early to make sure
2873 that the TARGET_ macros are representative ASAP. */
2875 int spe_capable_cpu =
2876 (rs6000_cpu == PROCESSOR_PPC8540
2877 || rs6000_cpu == PROCESSOR_PPC8548);
2879 if (!global_options_set.x_rs6000_spe_abi)
2880 rs6000_spe_abi = spe_capable_cpu;
2882 if (!global_options_set.x_rs6000_spe)
2883 rs6000_spe = spe_capable_cpu;
2885 if (!global_options_set.x_rs6000_float_gprs)
2886 rs6000_float_gprs =
2887 (rs6000_cpu == PROCESSOR_PPC8540 ? 1
2888 : rs6000_cpu == PROCESSOR_PPC8548 ? 2
2889 : 0);
2892 if (global_options_set.x_rs6000_spe_abi
2893 && rs6000_spe_abi
2894 && !TARGET_SPE_ABI)
2895 error ("not configured for SPE ABI");
2897 if (global_options_set.x_rs6000_spe
2898 && rs6000_spe
2899 && !TARGET_SPE)
2900 error ("not configured for SPE instruction set");
2902 if (main_target_opt != NULL
2903 && ((main_target_opt->x_rs6000_spe_abi != rs6000_spe_abi)
2904 || (main_target_opt->x_rs6000_spe != rs6000_spe)
2905 || (main_target_opt->x_rs6000_float_gprs != rs6000_float_gprs)))
2906 error ("target attribute or pragma changes SPE ABI");
2908 if (rs6000_cpu == PROCESSOR_PPCE300C2 || rs6000_cpu == PROCESSOR_PPCE300C3
2909 || rs6000_cpu == PROCESSOR_PPCE500MC || rs6000_cpu == PROCESSOR_PPCE500MC64
2910 || rs6000_cpu == PROCESSOR_PPCE5500)
2912 if (TARGET_ALTIVEC)
2913 error ("AltiVec not supported in this target");
2914 if (TARGET_SPE)
2915 error ("SPE not supported in this target");
2917 if (rs6000_cpu == PROCESSOR_PPCE6500)
2919 if (TARGET_SPE)
2920 error ("SPE not supported in this target");
2923 /* Disable Cell microcode if we are optimizing for the Cell
2924 and not optimizing for size. */
2925 if (rs6000_gen_cell_microcode == -1)
2926 rs6000_gen_cell_microcode = !(rs6000_cpu == PROCESSOR_CELL
2927 && !optimize_size);
2929 /* If we are optimizing big endian systems for space and it's OK to
2930 use instructions that would be microcoded on the Cell, use the
2931 load/store multiple and string instructions. */
2932 if (BYTES_BIG_ENDIAN && optimize_size && rs6000_gen_cell_microcode)
2933 rs6000_isa_flags |= ~rs6000_isa_flags_explicit & (OPTION_MASK_MULTIPLE
2934 | OPTION_MASK_STRING);
2936 /* Don't allow -mmultiple or -mstring on little endian systems
2937 unless the cpu is a 750, because the hardware doesn't support the
2938 instructions used in little endian mode, and causes an alignment
2939 trap. The 750 does not cause an alignment trap (except when the
2940 target is unaligned). */
2942 if (!BYTES_BIG_ENDIAN && rs6000_cpu != PROCESSOR_PPC750)
2944 if (TARGET_MULTIPLE)
2946 rs6000_isa_flags &= ~OPTION_MASK_MULTIPLE;
2947 if ((rs6000_isa_flags_explicit & OPTION_MASK_MULTIPLE) != 0)
2948 warning (0, "-mmultiple is not supported on little endian systems");
2951 if (TARGET_STRING)
2953 rs6000_isa_flags &= ~OPTION_MASK_STRING;
2954 if ((rs6000_isa_flags_explicit & OPTION_MASK_STRING) != 0)
2955 warning (0, "-mstring is not supported on little endian systems");
2959 /* Add some warnings for VSX. */
2960 if (TARGET_VSX)
2962 const char *msg = NULL;
2963 if (!TARGET_HARD_FLOAT || !TARGET_FPRS
2964 || !TARGET_SINGLE_FLOAT || !TARGET_DOUBLE_FLOAT)
2966 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX)
2967 msg = N_("-mvsx requires hardware floating point");
2968 else
2970 rs6000_isa_flags &= ~ OPTION_MASK_VSX;
2971 rs6000_isa_flags_explicit |= OPTION_MASK_VSX;
2974 else if (TARGET_PAIRED_FLOAT)
2975 msg = N_("-mvsx and -mpaired are incompatible");
2976 /* The hardware will allow VSX and little endian, but until we make sure
2977 things like vector select, etc. work don't allow VSX on little endian
2978 systems at this point. */
2979 else if (!BYTES_BIG_ENDIAN)
2980 msg = N_("-mvsx used with little endian code");
2981 else if (TARGET_AVOID_XFORM > 0)
2982 msg = N_("-mvsx needs indexed addressing");
2983 else if (!TARGET_ALTIVEC && (rs6000_isa_flags_explicit
2984 & OPTION_MASK_ALTIVEC))
2986 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX)
2987 msg = N_("-mvsx and -mno-altivec are incompatible");
2988 else
2989 msg = N_("-mno-altivec disables vsx");
2992 if (msg)
2994 warning (0, msg);
2995 rs6000_isa_flags &= ~ OPTION_MASK_VSX;
2996 rs6000_isa_flags_explicit |= OPTION_MASK_VSX;
3000 /* If hard-float/altivec/vsx were explicitly turned off then don't allow
3001 the -mcpu setting to enable options that conflict. */
3002 if ((!TARGET_HARD_FLOAT || !TARGET_ALTIVEC || !TARGET_VSX)
3003 && (rs6000_isa_flags_explicit & (OPTION_MASK_SOFT_FLOAT
3004 | OPTION_MASK_ALTIVEC
3005 | OPTION_MASK_VSX)) != 0)
3006 rs6000_isa_flags &= ~((OPTION_MASK_P8_VECTOR | OPTION_MASK_CRYPTO
3007 | OPTION_MASK_DIRECT_MOVE)
3008 & ~rs6000_isa_flags_explicit);
3010 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
3011 rs6000_print_isa_options (stderr, 0, "before defaults", rs6000_isa_flags);
3013 /* For the newer switches (vsx, dfp, etc.) set some of the older options,
3014 unless the user explicitly used the -mno-<option> to disable the code. */
3015 if (TARGET_P8_VECTOR || TARGET_DIRECT_MOVE || TARGET_CRYPTO)
3016 rs6000_isa_flags |= (ISA_2_7_MASKS_SERVER & ~rs6000_isa_flags_explicit);
3017 else if (TARGET_VSX)
3018 rs6000_isa_flags |= (ISA_2_6_MASKS_SERVER & ~rs6000_isa_flags_explicit);
3019 else if (TARGET_POPCNTD)
3020 rs6000_isa_flags |= (ISA_2_6_MASKS_EMBEDDED & ~rs6000_isa_flags_explicit);
3021 else if (TARGET_DFP)
3022 rs6000_isa_flags |= (ISA_2_5_MASKS_SERVER & ~rs6000_isa_flags_explicit);
3023 else if (TARGET_CMPB)
3024 rs6000_isa_flags |= (ISA_2_5_MASKS_EMBEDDED & ~rs6000_isa_flags_explicit);
3025 else if (TARGET_FPRND)
3026 rs6000_isa_flags |= (ISA_2_4_MASKS & ~rs6000_isa_flags_explicit);
3027 else if (TARGET_POPCNTB)
3028 rs6000_isa_flags |= (ISA_2_2_MASKS & ~rs6000_isa_flags_explicit);
3029 else if (TARGET_ALTIVEC)
3030 rs6000_isa_flags |= (OPTION_MASK_PPC_GFXOPT & ~rs6000_isa_flags_explicit);
3032 if (TARGET_CRYPTO && !TARGET_ALTIVEC)
3034 if (rs6000_isa_flags_explicit & OPTION_MASK_CRYPTO)
3035 error ("-mcrypto requires -maltivec");
3036 rs6000_isa_flags &= ~OPTION_MASK_CRYPTO;
3039 if (TARGET_DIRECT_MOVE && !TARGET_VSX)
3041 if (rs6000_isa_flags_explicit & OPTION_MASK_DIRECT_MOVE)
3042 error ("-mdirect-move requires -mvsx");
3043 rs6000_isa_flags &= ~OPTION_MASK_DIRECT_MOVE;
3046 if (TARGET_P8_VECTOR && !TARGET_ALTIVEC)
3048 if (rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR)
3049 error ("-mpower8-vector requires -maltivec");
3050 rs6000_isa_flags &= ~OPTION_MASK_P8_VECTOR;
3053 if (TARGET_P8_VECTOR && !TARGET_VSX)
3055 if (rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR)
3056 error ("-mpower8-vector requires -mvsx");
3057 rs6000_isa_flags &= ~OPTION_MASK_P8_VECTOR;
3060 if (TARGET_VSX_TIMODE && !TARGET_VSX)
3062 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX_TIMODE)
3063 error ("-mvsx-timode requires -mvsx");
3064 rs6000_isa_flags &= ~OPTION_MASK_VSX_TIMODE;
3067 /* The quad memory instructions only works in 64-bit mode. In 32-bit mode,
3068 silently turn off quad memory mode. */
3069 if (TARGET_QUAD_MEMORY && !TARGET_POWERPC64)
3071 if ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY) != 0)
3072 warning (0, N_("-mquad-memory requires 64-bit mode"));
3074 rs6000_isa_flags &= ~OPTION_MASK_QUAD_MEMORY;
3077 /* Enable power8 fusion if we are tuning for power8, even if we aren't
3078 generating power8 instructions. */
3079 if (!(rs6000_isa_flags_explicit & OPTION_MASK_P8_FUSION))
3080 rs6000_isa_flags |= (processor_target_table[tune_index].target_enable
3081 & OPTION_MASK_P8_FUSION);
3083 /* Power8 does not fuse sign extended loads with the addis. If we are
3084 optimizing at high levels for speed, convert a sign extended load into a
3085 zero extending load, and an explicit sign extension. */
3086 if (TARGET_P8_FUSION
3087 && !(rs6000_isa_flags_explicit & OPTION_MASK_P8_FUSION_SIGN)
3088 && optimize_function_for_speed_p (cfun)
3089 && optimize >= 3)
3090 rs6000_isa_flags |= OPTION_MASK_P8_FUSION_SIGN;
3092 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
3093 rs6000_print_isa_options (stderr, 0, "after defaults", rs6000_isa_flags);
3095 /* E500mc does "better" if we inline more aggressively. Respect the
3096 user's opinion, though. */
3097 if (rs6000_block_move_inline_limit == 0
3098 && (rs6000_cpu == PROCESSOR_PPCE500MC
3099 || rs6000_cpu == PROCESSOR_PPCE500MC64
3100 || rs6000_cpu == PROCESSOR_PPCE5500
3101 || rs6000_cpu == PROCESSOR_PPCE6500))
3102 rs6000_block_move_inline_limit = 128;
3104 /* store_one_arg depends on expand_block_move to handle at least the
3105 size of reg_parm_stack_space. */
3106 if (rs6000_block_move_inline_limit < (TARGET_POWERPC64 ? 64 : 32))
3107 rs6000_block_move_inline_limit = (TARGET_POWERPC64 ? 64 : 32);
3109 if (global_init_p)
3111 /* If the appropriate debug option is enabled, replace the target hooks
3112 with debug versions that call the real version and then prints
3113 debugging information. */
3114 if (TARGET_DEBUG_COST)
3116 targetm.rtx_costs = rs6000_debug_rtx_costs;
3117 targetm.address_cost = rs6000_debug_address_cost;
3118 targetm.sched.adjust_cost = rs6000_debug_adjust_cost;
3121 if (TARGET_DEBUG_ADDR)
3123 targetm.legitimate_address_p = rs6000_debug_legitimate_address_p;
3124 targetm.legitimize_address = rs6000_debug_legitimize_address;
3125 rs6000_secondary_reload_class_ptr
3126 = rs6000_debug_secondary_reload_class;
3127 rs6000_secondary_memory_needed_ptr
3128 = rs6000_debug_secondary_memory_needed;
3129 rs6000_cannot_change_mode_class_ptr
3130 = rs6000_debug_cannot_change_mode_class;
3131 rs6000_preferred_reload_class_ptr
3132 = rs6000_debug_preferred_reload_class;
3133 rs6000_legitimize_reload_address_ptr
3134 = rs6000_debug_legitimize_reload_address;
3135 rs6000_mode_dependent_address_ptr
3136 = rs6000_debug_mode_dependent_address;
3139 if (rs6000_veclibabi_name)
3141 if (strcmp (rs6000_veclibabi_name, "mass") == 0)
3142 rs6000_veclib_handler = rs6000_builtin_vectorized_libmass;
3143 else
3145 error ("unknown vectorization library ABI type (%s) for "
3146 "-mveclibabi= switch", rs6000_veclibabi_name);
3147 ret = false;
3152 if (!global_options_set.x_rs6000_long_double_type_size)
3154 if (main_target_opt != NULL
3155 && (main_target_opt->x_rs6000_long_double_type_size
3156 != RS6000_DEFAULT_LONG_DOUBLE_SIZE))
3157 error ("target attribute or pragma changes long double size");
3158 else
3159 rs6000_long_double_type_size = RS6000_DEFAULT_LONG_DOUBLE_SIZE;
3162 #if !defined (POWERPC_LINUX) && !defined (POWERPC_FREEBSD)
3163 if (!global_options_set.x_rs6000_ieeequad)
3164 rs6000_ieeequad = 1;
3165 #endif
3167 /* Disable VSX and Altivec silently if the user switched cpus to power7 in a
3168 target attribute or pragma which automatically enables both options,
3169 unless the altivec ABI was set. This is set by default for 64-bit, but
3170 not for 32-bit. */
3171 if (main_target_opt != NULL && !main_target_opt->x_rs6000_altivec_abi)
3172 rs6000_isa_flags &= ~((OPTION_MASK_VSX | OPTION_MASK_ALTIVEC)
3173 & ~rs6000_isa_flags_explicit);
3175 /* Enable Altivec ABI for AIX -maltivec. */
3176 if (TARGET_XCOFF && (TARGET_ALTIVEC || TARGET_VSX))
3178 if (main_target_opt != NULL && !main_target_opt->x_rs6000_altivec_abi)
3179 error ("target attribute or pragma changes AltiVec ABI");
3180 else
3181 rs6000_altivec_abi = 1;
3184 /* The AltiVec ABI is the default for PowerPC-64 GNU/Linux. For
3185 PowerPC-32 GNU/Linux, -maltivec implies the AltiVec ABI. It can
3186 be explicitly overridden in either case. */
3187 if (TARGET_ELF)
3189 if (!global_options_set.x_rs6000_altivec_abi
3190 && (TARGET_64BIT || TARGET_ALTIVEC || TARGET_VSX))
3192 if (main_target_opt != NULL &&
3193 !main_target_opt->x_rs6000_altivec_abi)
3194 error ("target attribute or pragma changes AltiVec ABI");
3195 else
3196 rs6000_altivec_abi = 1;
3200 /* Set the Darwin64 ABI as default for 64-bit Darwin.
3201 So far, the only darwin64 targets are also MACH-O. */
3202 if (TARGET_MACHO
3203 && DEFAULT_ABI == ABI_DARWIN
3204 && TARGET_64BIT)
3206 if (main_target_opt != NULL && !main_target_opt->x_rs6000_darwin64_abi)
3207 error ("target attribute or pragma changes darwin64 ABI");
3208 else
3210 rs6000_darwin64_abi = 1;
3211 /* Default to natural alignment, for better performance. */
3212 rs6000_alignment_flags = MASK_ALIGN_NATURAL;
3216 /* Place FP constants in the constant pool instead of TOC
3217 if section anchors enabled. */
3218 if (flag_section_anchors
3219 && !global_options_set.x_TARGET_NO_FP_IN_TOC)
3220 TARGET_NO_FP_IN_TOC = 1;
3222 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
3223 rs6000_print_isa_options (stderr, 0, "before subtarget", rs6000_isa_flags);
3225 #ifdef SUBTARGET_OVERRIDE_OPTIONS
3226 SUBTARGET_OVERRIDE_OPTIONS;
3227 #endif
3228 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
3229 SUBSUBTARGET_OVERRIDE_OPTIONS;
3230 #endif
3231 #ifdef SUB3TARGET_OVERRIDE_OPTIONS
3232 SUB3TARGET_OVERRIDE_OPTIONS;
3233 #endif
3235 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
3236 rs6000_print_isa_options (stderr, 0, "after subtarget", rs6000_isa_flags);
3238 /* For the E500 family of cores, reset the single/double FP flags to let us
3239 check that they remain constant across attributes or pragmas. Also,
3240 clear a possible request for string instructions, not supported and which
3241 we might have silently queried above for -Os.
3243 For other families, clear ISEL in case it was set implicitly.
3246 switch (rs6000_cpu)
3248 case PROCESSOR_PPC8540:
3249 case PROCESSOR_PPC8548:
3250 case PROCESSOR_PPCE500MC:
3251 case PROCESSOR_PPCE500MC64:
3252 case PROCESSOR_PPCE5500:
3253 case PROCESSOR_PPCE6500:
3255 rs6000_single_float = TARGET_E500_SINGLE || TARGET_E500_DOUBLE;
3256 rs6000_double_float = TARGET_E500_DOUBLE;
3258 rs6000_isa_flags &= ~OPTION_MASK_STRING;
3260 break;
3262 default:
3264 if (have_cpu && !(rs6000_isa_flags_explicit & OPTION_MASK_ISEL))
3265 rs6000_isa_flags &= ~OPTION_MASK_ISEL;
3267 break;
3270 if (main_target_opt)
3272 if (main_target_opt->x_rs6000_single_float != rs6000_single_float)
3273 error ("target attribute or pragma changes single precision floating "
3274 "point");
3275 if (main_target_opt->x_rs6000_double_float != rs6000_double_float)
3276 error ("target attribute or pragma changes double precision floating "
3277 "point");
3280 /* Detect invalid option combinations with E500. */
3281 CHECK_E500_OPTIONS;
3283 rs6000_always_hint = (rs6000_cpu != PROCESSOR_POWER4
3284 && rs6000_cpu != PROCESSOR_POWER5
3285 && rs6000_cpu != PROCESSOR_POWER6
3286 && rs6000_cpu != PROCESSOR_POWER7
3287 && rs6000_cpu != PROCESSOR_POWER8
3288 && rs6000_cpu != PROCESSOR_PPCA2
3289 && rs6000_cpu != PROCESSOR_CELL
3290 && rs6000_cpu != PROCESSOR_PPC476);
3291 rs6000_sched_groups = (rs6000_cpu == PROCESSOR_POWER4
3292 || rs6000_cpu == PROCESSOR_POWER5
3293 || rs6000_cpu == PROCESSOR_POWER7
3294 || rs6000_cpu == PROCESSOR_POWER8);
3295 rs6000_align_branch_targets = (rs6000_cpu == PROCESSOR_POWER4
3296 || rs6000_cpu == PROCESSOR_POWER5
3297 || rs6000_cpu == PROCESSOR_POWER6
3298 || rs6000_cpu == PROCESSOR_POWER7
3299 || rs6000_cpu == PROCESSOR_POWER8
3300 || rs6000_cpu == PROCESSOR_PPCE500MC
3301 || rs6000_cpu == PROCESSOR_PPCE500MC64
3302 || rs6000_cpu == PROCESSOR_PPCE5500
3303 || rs6000_cpu == PROCESSOR_PPCE6500);
3305 /* Allow debug switches to override the above settings. These are set to -1
3306 in rs6000.opt to indicate the user hasn't directly set the switch. */
3307 if (TARGET_ALWAYS_HINT >= 0)
3308 rs6000_always_hint = TARGET_ALWAYS_HINT;
3310 if (TARGET_SCHED_GROUPS >= 0)
3311 rs6000_sched_groups = TARGET_SCHED_GROUPS;
3313 if (TARGET_ALIGN_BRANCH_TARGETS >= 0)
3314 rs6000_align_branch_targets = TARGET_ALIGN_BRANCH_TARGETS;
3316 rs6000_sched_restricted_insns_priority
3317 = (rs6000_sched_groups ? 1 : 0);
3319 /* Handle -msched-costly-dep option. */
3320 rs6000_sched_costly_dep
3321 = (rs6000_sched_groups ? true_store_to_load_dep_costly : no_dep_costly);
3323 if (rs6000_sched_costly_dep_str)
3325 if (! strcmp (rs6000_sched_costly_dep_str, "no"))
3326 rs6000_sched_costly_dep = no_dep_costly;
3327 else if (! strcmp (rs6000_sched_costly_dep_str, "all"))
3328 rs6000_sched_costly_dep = all_deps_costly;
3329 else if (! strcmp (rs6000_sched_costly_dep_str, "true_store_to_load"))
3330 rs6000_sched_costly_dep = true_store_to_load_dep_costly;
3331 else if (! strcmp (rs6000_sched_costly_dep_str, "store_to_load"))
3332 rs6000_sched_costly_dep = store_to_load_dep_costly;
3333 else
3334 rs6000_sched_costly_dep = ((enum rs6000_dependence_cost)
3335 atoi (rs6000_sched_costly_dep_str));
3338 /* Handle -minsert-sched-nops option. */
3339 rs6000_sched_insert_nops
3340 = (rs6000_sched_groups ? sched_finish_regroup_exact : sched_finish_none);
3342 if (rs6000_sched_insert_nops_str)
3344 if (! strcmp (rs6000_sched_insert_nops_str, "no"))
3345 rs6000_sched_insert_nops = sched_finish_none;
3346 else if (! strcmp (rs6000_sched_insert_nops_str, "pad"))
3347 rs6000_sched_insert_nops = sched_finish_pad_groups;
3348 else if (! strcmp (rs6000_sched_insert_nops_str, "regroup_exact"))
3349 rs6000_sched_insert_nops = sched_finish_regroup_exact;
3350 else
3351 rs6000_sched_insert_nops = ((enum rs6000_nop_insertion)
3352 atoi (rs6000_sched_insert_nops_str));
3355 if (global_init_p)
3357 #ifdef TARGET_REGNAMES
3358 /* If the user desires alternate register names, copy in the
3359 alternate names now. */
3360 if (TARGET_REGNAMES)
3361 memcpy (rs6000_reg_names, alt_reg_names, sizeof (rs6000_reg_names));
3362 #endif
3364 /* Set aix_struct_return last, after the ABI is determined.
3365 If -maix-struct-return or -msvr4-struct-return was explicitly
3366 used, don't override with the ABI default. */
3367 if (!global_options_set.x_aix_struct_return)
3368 aix_struct_return = (DEFAULT_ABI != ABI_V4 || DRAFT_V4_STRUCT_RET);
3370 #if 0
3371 /* IBM XL compiler defaults to unsigned bitfields. */
3372 if (TARGET_XL_COMPAT)
3373 flag_signed_bitfields = 0;
3374 #endif
3376 if (TARGET_LONG_DOUBLE_128 && !TARGET_IEEEQUAD)
3377 REAL_MODE_FORMAT (TFmode) = &ibm_extended_format;
3379 if (TARGET_TOC)
3380 ASM_GENERATE_INTERNAL_LABEL (toc_label_name, "LCTOC", 1);
3382 /* We can only guarantee the availability of DI pseudo-ops when
3383 assembling for 64-bit targets. */
3384 if (!TARGET_64BIT)
3386 targetm.asm_out.aligned_op.di = NULL;
3387 targetm.asm_out.unaligned_op.di = NULL;
3391 /* Set branch target alignment, if not optimizing for size. */
3392 if (!optimize_size)
3394 /* Cell wants to be aligned 8byte for dual issue. Titan wants to be
3395 aligned 8byte to avoid misprediction by the branch predictor. */
3396 if (rs6000_cpu == PROCESSOR_TITAN
3397 || rs6000_cpu == PROCESSOR_CELL)
3399 if (align_functions <= 0)
3400 align_functions = 8;
3401 if (align_jumps <= 0)
3402 align_jumps = 8;
3403 if (align_loops <= 0)
3404 align_loops = 8;
3406 if (rs6000_align_branch_targets)
3408 if (align_functions <= 0)
3409 align_functions = 16;
3410 if (align_jumps <= 0)
3411 align_jumps = 16;
3412 if (align_loops <= 0)
3414 can_override_loop_align = 1;
3415 align_loops = 16;
3418 if (align_jumps_max_skip <= 0)
3419 align_jumps_max_skip = 15;
3420 if (align_loops_max_skip <= 0)
3421 align_loops_max_skip = 15;
3424 /* Arrange to save and restore machine status around nested functions. */
3425 init_machine_status = rs6000_init_machine_status;
3427 /* We should always be splitting complex arguments, but we can't break
3428 Linux and Darwin ABIs at the moment. For now, only AIX is fixed. */
3429 if (DEFAULT_ABI != ABI_AIX)
3430 targetm.calls.split_complex_arg = NULL;
3433 /* Initialize rs6000_cost with the appropriate target costs. */
3434 if (optimize_size)
3435 rs6000_cost = TARGET_POWERPC64 ? &size64_cost : &size32_cost;
3436 else
3437 switch (rs6000_cpu)
3439 case PROCESSOR_RS64A:
3440 rs6000_cost = &rs64a_cost;
3441 break;
3443 case PROCESSOR_MPCCORE:
3444 rs6000_cost = &mpccore_cost;
3445 break;
3447 case PROCESSOR_PPC403:
3448 rs6000_cost = &ppc403_cost;
3449 break;
3451 case PROCESSOR_PPC405:
3452 rs6000_cost = &ppc405_cost;
3453 break;
3455 case PROCESSOR_PPC440:
3456 rs6000_cost = &ppc440_cost;
3457 break;
3459 case PROCESSOR_PPC476:
3460 rs6000_cost = &ppc476_cost;
3461 break;
3463 case PROCESSOR_PPC601:
3464 rs6000_cost = &ppc601_cost;
3465 break;
3467 case PROCESSOR_PPC603:
3468 rs6000_cost = &ppc603_cost;
3469 break;
3471 case PROCESSOR_PPC604:
3472 rs6000_cost = &ppc604_cost;
3473 break;
3475 case PROCESSOR_PPC604e:
3476 rs6000_cost = &ppc604e_cost;
3477 break;
3479 case PROCESSOR_PPC620:
3480 rs6000_cost = &ppc620_cost;
3481 break;
3483 case PROCESSOR_PPC630:
3484 rs6000_cost = &ppc630_cost;
3485 break;
3487 case PROCESSOR_CELL:
3488 rs6000_cost = &ppccell_cost;
3489 break;
3491 case PROCESSOR_PPC750:
3492 case PROCESSOR_PPC7400:
3493 rs6000_cost = &ppc750_cost;
3494 break;
3496 case PROCESSOR_PPC7450:
3497 rs6000_cost = &ppc7450_cost;
3498 break;
3500 case PROCESSOR_PPC8540:
3501 case PROCESSOR_PPC8548:
3502 rs6000_cost = &ppc8540_cost;
3503 break;
3505 case PROCESSOR_PPCE300C2:
3506 case PROCESSOR_PPCE300C3:
3507 rs6000_cost = &ppce300c2c3_cost;
3508 break;
3510 case PROCESSOR_PPCE500MC:
3511 rs6000_cost = &ppce500mc_cost;
3512 break;
3514 case PROCESSOR_PPCE500MC64:
3515 rs6000_cost = &ppce500mc64_cost;
3516 break;
3518 case PROCESSOR_PPCE5500:
3519 rs6000_cost = &ppce5500_cost;
3520 break;
3522 case PROCESSOR_PPCE6500:
3523 rs6000_cost = &ppce6500_cost;
3524 break;
3526 case PROCESSOR_TITAN:
3527 rs6000_cost = &titan_cost;
3528 break;
3530 case PROCESSOR_POWER4:
3531 case PROCESSOR_POWER5:
3532 rs6000_cost = &power4_cost;
3533 break;
3535 case PROCESSOR_POWER6:
3536 rs6000_cost = &power6_cost;
3537 break;
3539 case PROCESSOR_POWER7:
3540 rs6000_cost = &power7_cost;
3541 break;
3543 case PROCESSOR_POWER8:
3544 rs6000_cost = &power8_cost;
3545 break;
3547 case PROCESSOR_PPCA2:
3548 rs6000_cost = &ppca2_cost;
3549 break;
3551 default:
3552 gcc_unreachable ();
3555 if (global_init_p)
3557 maybe_set_param_value (PARAM_SIMULTANEOUS_PREFETCHES,
3558 rs6000_cost->simultaneous_prefetches,
3559 global_options.x_param_values,
3560 global_options_set.x_param_values);
3561 maybe_set_param_value (PARAM_L1_CACHE_SIZE, rs6000_cost->l1_cache_size,
3562 global_options.x_param_values,
3563 global_options_set.x_param_values);
3564 maybe_set_param_value (PARAM_L1_CACHE_LINE_SIZE,
3565 rs6000_cost->cache_line_size,
3566 global_options.x_param_values,
3567 global_options_set.x_param_values);
3568 maybe_set_param_value (PARAM_L2_CACHE_SIZE, rs6000_cost->l2_cache_size,
3569 global_options.x_param_values,
3570 global_options_set.x_param_values);
3572 /* Increase loop peeling limits based on performance analysis. */
3573 maybe_set_param_value (PARAM_MAX_PEELED_INSNS, 400,
3574 global_options.x_param_values,
3575 global_options_set.x_param_values);
3576 maybe_set_param_value (PARAM_MAX_COMPLETELY_PEELED_INSNS, 400,
3577 global_options.x_param_values,
3578 global_options_set.x_param_values);
3580 /* If using typedef char *va_list, signal that
3581 __builtin_va_start (&ap, 0) can be optimized to
3582 ap = __builtin_next_arg (0). */
3583 if (DEFAULT_ABI != ABI_V4)
3584 targetm.expand_builtin_va_start = NULL;
3587 /* Set up single/double float flags.
3588 If TARGET_HARD_FLOAT is set, but neither single or double is set,
3589 then set both flags. */
3590 if (TARGET_HARD_FLOAT && TARGET_FPRS
3591 && rs6000_single_float == 0 && rs6000_double_float == 0)
3592 rs6000_single_float = rs6000_double_float = 1;
3594 /* If not explicitly specified via option, decide whether to generate indexed
3595 load/store instructions. */
3596 if (TARGET_AVOID_XFORM == -1)
3597 /* Avoid indexed addressing when targeting Power6 in order to avoid the
3598 DERAT mispredict penalty. However the LVE and STVE altivec instructions
3599 need indexed accesses and the type used is the scalar type of the element
3600 being loaded or stored. */
3601 TARGET_AVOID_XFORM = (rs6000_cpu == PROCESSOR_POWER6 && TARGET_CMPB
3602 && !TARGET_ALTIVEC);
3604 /* Set the -mrecip options. */
3605 if (rs6000_recip_name)
3607 char *p = ASTRDUP (rs6000_recip_name);
3608 char *q;
3609 unsigned int mask, i;
3610 bool invert;
3612 while ((q = strtok (p, ",")) != NULL)
3614 p = NULL;
3615 if (*q == '!')
3617 invert = true;
3618 q++;
3620 else
3621 invert = false;
3623 if (!strcmp (q, "default"))
3624 mask = ((TARGET_RECIP_PRECISION)
3625 ? RECIP_HIGH_PRECISION : RECIP_LOW_PRECISION);
3626 else
3628 for (i = 0; i < ARRAY_SIZE (recip_options); i++)
3629 if (!strcmp (q, recip_options[i].string))
3631 mask = recip_options[i].mask;
3632 break;
3635 if (i == ARRAY_SIZE (recip_options))
3637 error ("unknown option for -mrecip=%s", q);
3638 invert = false;
3639 mask = 0;
3640 ret = false;
3644 if (invert)
3645 rs6000_recip_control &= ~mask;
3646 else
3647 rs6000_recip_control |= mask;
3651 /* Set the builtin mask of the various options used that could affect which
3652 builtins were used. In the past we used target_flags, but we've run out
3653 of bits, and some options like SPE and PAIRED are no longer in
3654 target_flags. */
3655 rs6000_builtin_mask = rs6000_builtin_mask_calculate ();
3656 if (TARGET_DEBUG_BUILTIN || TARGET_DEBUG_TARGET)
3658 fprintf (stderr,
3659 "new builtin mask = " HOST_WIDE_INT_PRINT_HEX ", ",
3660 rs6000_builtin_mask);
3661 rs6000_print_builtin_options (stderr, 0, NULL, rs6000_builtin_mask);
3664 /* Initialize all of the registers. */
3665 rs6000_init_hard_regno_mode_ok (global_init_p);
3667 /* Save the initial options in case the user does function specific options */
3668 if (global_init_p)
3669 target_option_default_node = target_option_current_node
3670 = build_target_option_node ();
3672 /* If not explicitly specified via option, decide whether to generate the
3673 extra blr's required to preserve the link stack on some cpus (eg, 476). */
3674 if (TARGET_LINK_STACK == -1)
3675 SET_TARGET_LINK_STACK (rs6000_cpu == PROCESSOR_PPC476 && flag_pic);
3677 return ret;
3680 /* Implement TARGET_OPTION_OVERRIDE. On the RS/6000 this is used to
3681 define the target cpu type. */
3683 static void
3684 rs6000_option_override (void)
3686 (void) rs6000_option_override_internal (true);
3690 /* Implement targetm.vectorize.builtin_mask_for_load. */
3691 static tree
3692 rs6000_builtin_mask_for_load (void)
3694 if (TARGET_ALTIVEC || TARGET_VSX)
3695 return altivec_builtin_mask_for_load;
3696 else
3697 return 0;
3700 /* Implement LOOP_ALIGN. */
3702 rs6000_loop_align (rtx label)
3704 basic_block bb;
3705 int ninsns;
3707 /* Don't override loop alignment if -falign-loops was specified. */
3708 if (!can_override_loop_align)
3709 return align_loops_log;
3711 bb = BLOCK_FOR_INSN (label);
3712 ninsns = num_loop_insns(bb->loop_father);
3714 /* Align small loops to 32 bytes to fit in an icache sector, otherwise return default. */
3715 if (ninsns > 4 && ninsns <= 8
3716 && (rs6000_cpu == PROCESSOR_POWER4
3717 || rs6000_cpu == PROCESSOR_POWER5
3718 || rs6000_cpu == PROCESSOR_POWER6
3719 || rs6000_cpu == PROCESSOR_POWER7
3720 || rs6000_cpu == PROCESSOR_POWER8))
3721 return 5;
3722 else
3723 return align_loops_log;
3726 /* Implement TARGET_LOOP_ALIGN_MAX_SKIP. */
3727 static int
3728 rs6000_loop_align_max_skip (rtx label)
3730 return (1 << rs6000_loop_align (label)) - 1;
3733 /* Return true iff, data reference of TYPE can reach vector alignment (16)
3734 after applying N number of iterations. This routine does not determine
3735 how may iterations are required to reach desired alignment. */
3737 static bool
3738 rs6000_vector_alignment_reachable (const_tree type ATTRIBUTE_UNUSED, bool is_packed)
3740 if (is_packed)
3741 return false;
3743 if (TARGET_32BIT)
3745 if (rs6000_alignment_flags == MASK_ALIGN_NATURAL)
3746 return true;
3748 if (rs6000_alignment_flags == MASK_ALIGN_POWER)
3749 return true;
3751 return false;
3753 else
3755 if (TARGET_MACHO)
3756 return false;
3758 /* Assuming that all other types are naturally aligned. CHECKME! */
3759 return true;
3763 /* Return true if the vector misalignment factor is supported by the
3764 target. */
3765 static bool
3766 rs6000_builtin_support_vector_misalignment (enum machine_mode mode,
3767 const_tree type,
3768 int misalignment,
3769 bool is_packed)
3771 if (TARGET_VSX)
3773 /* Return if movmisalign pattern is not supported for this mode. */
3774 if (optab_handler (movmisalign_optab, mode) == CODE_FOR_nothing)
3775 return false;
3777 if (misalignment == -1)
3779 /* Misalignment factor is unknown at compile time but we know
3780 it's word aligned. */
3781 if (rs6000_vector_alignment_reachable (type, is_packed))
3783 int element_size = TREE_INT_CST_LOW (TYPE_SIZE (type));
3785 if (element_size == 64 || element_size == 32)
3786 return true;
3789 return false;
3792 /* VSX supports word-aligned vector. */
3793 if (misalignment % 4 == 0)
3794 return true;
3796 return false;
3799 /* Implement targetm.vectorize.builtin_vectorization_cost. */
3800 static int
3801 rs6000_builtin_vectorization_cost (enum vect_cost_for_stmt type_of_cost,
3802 tree vectype, int misalign)
3804 unsigned elements;
3805 tree elem_type;
3807 switch (type_of_cost)
3809 case scalar_stmt:
3810 case scalar_load:
3811 case scalar_store:
3812 case vector_stmt:
3813 case vector_load:
3814 case vector_store:
3815 case vec_to_scalar:
3816 case scalar_to_vec:
3817 case cond_branch_not_taken:
3818 return 1;
3820 case vec_perm:
3821 if (TARGET_VSX)
3822 return 3;
3823 else
3824 return 1;
3826 case vec_promote_demote:
3827 if (TARGET_VSX)
3828 return 4;
3829 else
3830 return 1;
3832 case cond_branch_taken:
3833 return 3;
3835 case unaligned_load:
3836 if (TARGET_VSX && TARGET_ALLOW_MOVMISALIGN)
3838 elements = TYPE_VECTOR_SUBPARTS (vectype);
3839 if (elements == 2)
3840 /* Double word aligned. */
3841 return 2;
3843 if (elements == 4)
3845 switch (misalign)
3847 case 8:
3848 /* Double word aligned. */
3849 return 2;
3851 case -1:
3852 /* Unknown misalignment. */
3853 case 4:
3854 case 12:
3855 /* Word aligned. */
3856 return 22;
3858 default:
3859 gcc_unreachable ();
3864 if (TARGET_ALTIVEC)
3865 /* Misaligned loads are not supported. */
3866 gcc_unreachable ();
3868 return 2;
3870 case unaligned_store:
3871 if (TARGET_VSX && TARGET_ALLOW_MOVMISALIGN)
3873 elements = TYPE_VECTOR_SUBPARTS (vectype);
3874 if (elements == 2)
3875 /* Double word aligned. */
3876 return 2;
3878 if (elements == 4)
3880 switch (misalign)
3882 case 8:
3883 /* Double word aligned. */
3884 return 2;
3886 case -1:
3887 /* Unknown misalignment. */
3888 case 4:
3889 case 12:
3890 /* Word aligned. */
3891 return 23;
3893 default:
3894 gcc_unreachable ();
3899 if (TARGET_ALTIVEC)
3900 /* Misaligned stores are not supported. */
3901 gcc_unreachable ();
3903 return 2;
3905 case vec_construct:
3906 elements = TYPE_VECTOR_SUBPARTS (vectype);
3907 elem_type = TREE_TYPE (vectype);
3908 /* 32-bit vectors loaded into registers are stored as double
3909 precision, so we need n/2 converts in addition to the usual
3910 n/2 merges to construct a vector of short floats from them. */
3911 if (SCALAR_FLOAT_TYPE_P (elem_type)
3912 && TYPE_PRECISION (elem_type) == 32)
3913 return elements + 1;
3914 else
3915 return elements / 2 + 1;
3917 default:
3918 gcc_unreachable ();
3922 /* Implement targetm.vectorize.preferred_simd_mode. */
3924 static enum machine_mode
3925 rs6000_preferred_simd_mode (enum machine_mode mode)
3927 if (TARGET_VSX)
3928 switch (mode)
3930 case DFmode:
3931 return V2DFmode;
3932 default:;
3934 if (TARGET_ALTIVEC || TARGET_VSX)
3935 switch (mode)
3937 case SFmode:
3938 return V4SFmode;
3939 case DImode:
3940 return V2DImode;
3941 case SImode:
3942 return V4SImode;
3943 case HImode:
3944 return V8HImode;
3945 case QImode:
3946 return V16QImode;
3947 default:;
3949 if (TARGET_SPE)
3950 switch (mode)
3952 case SFmode:
3953 return V2SFmode;
3954 case SImode:
3955 return V2SImode;
3956 default:;
3958 if (TARGET_PAIRED_FLOAT
3959 && mode == SFmode)
3960 return V2SFmode;
3961 return word_mode;
3964 typedef struct _rs6000_cost_data
3966 struct loop *loop_info;
3967 unsigned cost[3];
3968 } rs6000_cost_data;
3970 /* Test for likely overcommitment of vector hardware resources. If a
3971 loop iteration is relatively large, and too large a percentage of
3972 instructions in the loop are vectorized, the cost model may not
3973 adequately reflect delays from unavailable vector resources.
3974 Penalize the loop body cost for this case. */
3976 static void
3977 rs6000_density_test (rs6000_cost_data *data)
3979 const int DENSITY_PCT_THRESHOLD = 85;
3980 const int DENSITY_SIZE_THRESHOLD = 70;
3981 const int DENSITY_PENALTY = 10;
3982 struct loop *loop = data->loop_info;
3983 basic_block *bbs = get_loop_body (loop);
3984 int nbbs = loop->num_nodes;
3985 int vec_cost = data->cost[vect_body], not_vec_cost = 0;
3986 int i, density_pct;
3988 for (i = 0; i < nbbs; i++)
3990 basic_block bb = bbs[i];
3991 gimple_stmt_iterator gsi;
3993 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3995 gimple stmt = gsi_stmt (gsi);
3996 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3998 if (!STMT_VINFO_RELEVANT_P (stmt_info)
3999 && !STMT_VINFO_IN_PATTERN_P (stmt_info))
4000 not_vec_cost++;
4004 free (bbs);
4005 density_pct = (vec_cost * 100) / (vec_cost + not_vec_cost);
4007 if (density_pct > DENSITY_PCT_THRESHOLD
4008 && vec_cost + not_vec_cost > DENSITY_SIZE_THRESHOLD)
4010 data->cost[vect_body] = vec_cost * (100 + DENSITY_PENALTY) / 100;
4011 if (dump_enabled_p ())
4012 dump_printf_loc (MSG_NOTE, vect_location,
4013 "density %d%%, cost %d exceeds threshold, penalizing "
4014 "loop body cost by %d%%", density_pct,
4015 vec_cost + not_vec_cost, DENSITY_PENALTY);
4019 /* Implement targetm.vectorize.init_cost. */
4021 static void *
4022 rs6000_init_cost (struct loop *loop_info)
4024 rs6000_cost_data *data = XNEW (struct _rs6000_cost_data);
4025 data->loop_info = loop_info;
4026 data->cost[vect_prologue] = 0;
4027 data->cost[vect_body] = 0;
4028 data->cost[vect_epilogue] = 0;
4029 return data;
4032 /* Implement targetm.vectorize.add_stmt_cost. */
4034 static unsigned
4035 rs6000_add_stmt_cost (void *data, int count, enum vect_cost_for_stmt kind,
4036 struct _stmt_vec_info *stmt_info, int misalign,
4037 enum vect_cost_model_location where)
4039 rs6000_cost_data *cost_data = (rs6000_cost_data*) data;
4040 unsigned retval = 0;
4042 if (flag_vect_cost_model)
4044 tree vectype = stmt_info ? stmt_vectype (stmt_info) : NULL_TREE;
4045 int stmt_cost = rs6000_builtin_vectorization_cost (kind, vectype,
4046 misalign);
4047 /* Statements in an inner loop relative to the loop being
4048 vectorized are weighted more heavily. The value here is
4049 arbitrary and could potentially be improved with analysis. */
4050 if (where == vect_body && stmt_info && stmt_in_inner_loop_p (stmt_info))
4051 count *= 50; /* FIXME. */
4053 retval = (unsigned) (count * stmt_cost);
4054 cost_data->cost[where] += retval;
4057 return retval;
4060 /* Implement targetm.vectorize.finish_cost. */
4062 static void
4063 rs6000_finish_cost (void *data, unsigned *prologue_cost,
4064 unsigned *body_cost, unsigned *epilogue_cost)
4066 rs6000_cost_data *cost_data = (rs6000_cost_data*) data;
4068 if (cost_data->loop_info)
4069 rs6000_density_test (cost_data);
4071 *prologue_cost = cost_data->cost[vect_prologue];
4072 *body_cost = cost_data->cost[vect_body];
4073 *epilogue_cost = cost_data->cost[vect_epilogue];
4076 /* Implement targetm.vectorize.destroy_cost_data. */
4078 static void
4079 rs6000_destroy_cost_data (void *data)
4081 free (data);
4084 /* Handler for the Mathematical Acceleration Subsystem (mass) interface to a
4085 library with vectorized intrinsics. */
4087 static tree
4088 rs6000_builtin_vectorized_libmass (tree fndecl, tree type_out, tree type_in)
4090 char name[32];
4091 const char *suffix = NULL;
4092 tree fntype, new_fndecl, bdecl = NULL_TREE;
4093 int n_args = 1;
4094 const char *bname;
4095 enum machine_mode el_mode, in_mode;
4096 int n, in_n;
4098 /* Libmass is suitable for unsafe math only as it does not correctly support
4099 parts of IEEE with the required precision such as denormals. Only support
4100 it if we have VSX to use the simd d2 or f4 functions.
4101 XXX: Add variable length support. */
4102 if (!flag_unsafe_math_optimizations || !TARGET_VSX)
4103 return NULL_TREE;
4105 el_mode = TYPE_MODE (TREE_TYPE (type_out));
4106 n = TYPE_VECTOR_SUBPARTS (type_out);
4107 in_mode = TYPE_MODE (TREE_TYPE (type_in));
4108 in_n = TYPE_VECTOR_SUBPARTS (type_in);
4109 if (el_mode != in_mode
4110 || n != in_n)
4111 return NULL_TREE;
4113 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
4115 enum built_in_function fn = DECL_FUNCTION_CODE (fndecl);
4116 switch (fn)
4118 case BUILT_IN_ATAN2:
4119 case BUILT_IN_HYPOT:
4120 case BUILT_IN_POW:
4121 n_args = 2;
4122 /* fall through */
4124 case BUILT_IN_ACOS:
4125 case BUILT_IN_ACOSH:
4126 case BUILT_IN_ASIN:
4127 case BUILT_IN_ASINH:
4128 case BUILT_IN_ATAN:
4129 case BUILT_IN_ATANH:
4130 case BUILT_IN_CBRT:
4131 case BUILT_IN_COS:
4132 case BUILT_IN_COSH:
4133 case BUILT_IN_ERF:
4134 case BUILT_IN_ERFC:
4135 case BUILT_IN_EXP2:
4136 case BUILT_IN_EXP:
4137 case BUILT_IN_EXPM1:
4138 case BUILT_IN_LGAMMA:
4139 case BUILT_IN_LOG10:
4140 case BUILT_IN_LOG1P:
4141 case BUILT_IN_LOG2:
4142 case BUILT_IN_LOG:
4143 case BUILT_IN_SIN:
4144 case BUILT_IN_SINH:
4145 case BUILT_IN_SQRT:
4146 case BUILT_IN_TAN:
4147 case BUILT_IN_TANH:
4148 bdecl = builtin_decl_implicit (fn);
4149 suffix = "d2"; /* pow -> powd2 */
4150 if (el_mode != DFmode
4151 || n != 2
4152 || !bdecl)
4153 return NULL_TREE;
4154 break;
4156 case BUILT_IN_ATAN2F:
4157 case BUILT_IN_HYPOTF:
4158 case BUILT_IN_POWF:
4159 n_args = 2;
4160 /* fall through */
4162 case BUILT_IN_ACOSF:
4163 case BUILT_IN_ACOSHF:
4164 case BUILT_IN_ASINF:
4165 case BUILT_IN_ASINHF:
4166 case BUILT_IN_ATANF:
4167 case BUILT_IN_ATANHF:
4168 case BUILT_IN_CBRTF:
4169 case BUILT_IN_COSF:
4170 case BUILT_IN_COSHF:
4171 case BUILT_IN_ERFF:
4172 case BUILT_IN_ERFCF:
4173 case BUILT_IN_EXP2F:
4174 case BUILT_IN_EXPF:
4175 case BUILT_IN_EXPM1F:
4176 case BUILT_IN_LGAMMAF:
4177 case BUILT_IN_LOG10F:
4178 case BUILT_IN_LOG1PF:
4179 case BUILT_IN_LOG2F:
4180 case BUILT_IN_LOGF:
4181 case BUILT_IN_SINF:
4182 case BUILT_IN_SINHF:
4183 case BUILT_IN_SQRTF:
4184 case BUILT_IN_TANF:
4185 case BUILT_IN_TANHF:
4186 bdecl = builtin_decl_implicit (fn);
4187 suffix = "4"; /* powf -> powf4 */
4188 if (el_mode != SFmode
4189 || n != 4
4190 || !bdecl)
4191 return NULL_TREE;
4192 break;
4194 default:
4195 return NULL_TREE;
4198 else
4199 return NULL_TREE;
4201 gcc_assert (suffix != NULL);
4202 bname = IDENTIFIER_POINTER (DECL_NAME (bdecl));
4203 if (!bname)
4204 return NULL_TREE;
4206 strcpy (name, bname + sizeof ("__builtin_") - 1);
4207 strcat (name, suffix);
4209 if (n_args == 1)
4210 fntype = build_function_type_list (type_out, type_in, NULL);
4211 else if (n_args == 2)
4212 fntype = build_function_type_list (type_out, type_in, type_in, NULL);
4213 else
4214 gcc_unreachable ();
4216 /* Build a function declaration for the vectorized function. */
4217 new_fndecl = build_decl (BUILTINS_LOCATION,
4218 FUNCTION_DECL, get_identifier (name), fntype);
4219 TREE_PUBLIC (new_fndecl) = 1;
4220 DECL_EXTERNAL (new_fndecl) = 1;
4221 DECL_IS_NOVOPS (new_fndecl) = 1;
4222 TREE_READONLY (new_fndecl) = 1;
4224 return new_fndecl;
4227 /* Returns a function decl for a vectorized version of the builtin function
4228 with builtin function code FN and the result vector type TYPE, or NULL_TREE
4229 if it is not available. */
4231 static tree
4232 rs6000_builtin_vectorized_function (tree fndecl, tree type_out,
4233 tree type_in)
4235 enum machine_mode in_mode, out_mode;
4236 int in_n, out_n;
4238 if (TARGET_DEBUG_BUILTIN)
4239 fprintf (stderr, "rs6000_builtin_vectorized_function (%s, %s, %s)\n",
4240 IDENTIFIER_POINTER (DECL_NAME (fndecl)),
4241 GET_MODE_NAME (TYPE_MODE (type_out)),
4242 GET_MODE_NAME (TYPE_MODE (type_in)));
4244 if (TREE_CODE (type_out) != VECTOR_TYPE
4245 || TREE_CODE (type_in) != VECTOR_TYPE
4246 || !TARGET_VECTORIZE_BUILTINS)
4247 return NULL_TREE;
4249 out_mode = TYPE_MODE (TREE_TYPE (type_out));
4250 out_n = TYPE_VECTOR_SUBPARTS (type_out);
4251 in_mode = TYPE_MODE (TREE_TYPE (type_in));
4252 in_n = TYPE_VECTOR_SUBPARTS (type_in);
4254 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
4256 enum built_in_function fn = DECL_FUNCTION_CODE (fndecl);
4257 switch (fn)
4259 case BUILT_IN_CLZIMAX:
4260 case BUILT_IN_CLZLL:
4261 case BUILT_IN_CLZL:
4262 case BUILT_IN_CLZ:
4263 if (TARGET_P8_VECTOR && in_mode == out_mode && out_n == in_n)
4265 if (out_mode == QImode && out_n == 16)
4266 return rs6000_builtin_decls[P8V_BUILTIN_VCLZB];
4267 else if (out_mode == HImode && out_n == 8)
4268 return rs6000_builtin_decls[P8V_BUILTIN_VCLZH];
4269 else if (out_mode == SImode && out_n == 4)
4270 return rs6000_builtin_decls[P8V_BUILTIN_VCLZW];
4271 else if (out_mode == DImode && out_n == 2)
4272 return rs6000_builtin_decls[P8V_BUILTIN_VCLZD];
4274 break;
4275 case BUILT_IN_COPYSIGN:
4276 if (VECTOR_UNIT_VSX_P (V2DFmode)
4277 && out_mode == DFmode && out_n == 2
4278 && in_mode == DFmode && in_n == 2)
4279 return rs6000_builtin_decls[VSX_BUILTIN_CPSGNDP];
4280 break;
4281 case BUILT_IN_COPYSIGNF:
4282 if (out_mode != SFmode || out_n != 4
4283 || in_mode != SFmode || in_n != 4)
4284 break;
4285 if (VECTOR_UNIT_VSX_P (V4SFmode))
4286 return rs6000_builtin_decls[VSX_BUILTIN_CPSGNSP];
4287 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
4288 return rs6000_builtin_decls[ALTIVEC_BUILTIN_COPYSIGN_V4SF];
4289 break;
4290 case BUILT_IN_POPCOUNTIMAX:
4291 case BUILT_IN_POPCOUNTLL:
4292 case BUILT_IN_POPCOUNTL:
4293 case BUILT_IN_POPCOUNT:
4294 if (TARGET_P8_VECTOR && in_mode == out_mode && out_n == in_n)
4296 if (out_mode == QImode && out_n == 16)
4297 return rs6000_builtin_decls[P8V_BUILTIN_VPOPCNTB];
4298 else if (out_mode == HImode && out_n == 8)
4299 return rs6000_builtin_decls[P8V_BUILTIN_VPOPCNTH];
4300 else if (out_mode == SImode && out_n == 4)
4301 return rs6000_builtin_decls[P8V_BUILTIN_VPOPCNTW];
4302 else if (out_mode == DImode && out_n == 2)
4303 return rs6000_builtin_decls[P8V_BUILTIN_VPOPCNTD];
4305 break;
4306 case BUILT_IN_SQRT:
4307 if (VECTOR_UNIT_VSX_P (V2DFmode)
4308 && out_mode == DFmode && out_n == 2
4309 && in_mode == DFmode && in_n == 2)
4310 return rs6000_builtin_decls[VSX_BUILTIN_XVSQRTDP];
4311 break;
4312 case BUILT_IN_SQRTF:
4313 if (VECTOR_UNIT_VSX_P (V4SFmode)
4314 && out_mode == SFmode && out_n == 4
4315 && in_mode == SFmode && in_n == 4)
4316 return rs6000_builtin_decls[VSX_BUILTIN_XVSQRTSP];
4317 break;
4318 case BUILT_IN_CEIL:
4319 if (VECTOR_UNIT_VSX_P (V2DFmode)
4320 && out_mode == DFmode && out_n == 2
4321 && in_mode == DFmode && in_n == 2)
4322 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIP];
4323 break;
4324 case BUILT_IN_CEILF:
4325 if (out_mode != SFmode || out_n != 4
4326 || in_mode != SFmode || in_n != 4)
4327 break;
4328 if (VECTOR_UNIT_VSX_P (V4SFmode))
4329 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIP];
4330 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
4331 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIP];
4332 break;
4333 case BUILT_IN_FLOOR:
4334 if (VECTOR_UNIT_VSX_P (V2DFmode)
4335 && out_mode == DFmode && out_n == 2
4336 && in_mode == DFmode && in_n == 2)
4337 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIM];
4338 break;
4339 case BUILT_IN_FLOORF:
4340 if (out_mode != SFmode || out_n != 4
4341 || in_mode != SFmode || in_n != 4)
4342 break;
4343 if (VECTOR_UNIT_VSX_P (V4SFmode))
4344 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIM];
4345 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
4346 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIM];
4347 break;
4348 case BUILT_IN_FMA:
4349 if (VECTOR_UNIT_VSX_P (V2DFmode)
4350 && out_mode == DFmode && out_n == 2
4351 && in_mode == DFmode && in_n == 2)
4352 return rs6000_builtin_decls[VSX_BUILTIN_XVMADDDP];
4353 break;
4354 case BUILT_IN_FMAF:
4355 if (VECTOR_UNIT_VSX_P (V4SFmode)
4356 && out_mode == SFmode && out_n == 4
4357 && in_mode == SFmode && in_n == 4)
4358 return rs6000_builtin_decls[VSX_BUILTIN_XVMADDSP];
4359 else if (VECTOR_UNIT_ALTIVEC_P (V4SFmode)
4360 && out_mode == SFmode && out_n == 4
4361 && in_mode == SFmode && in_n == 4)
4362 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VMADDFP];
4363 break;
4364 case BUILT_IN_TRUNC:
4365 if (VECTOR_UNIT_VSX_P (V2DFmode)
4366 && out_mode == DFmode && out_n == 2
4367 && in_mode == DFmode && in_n == 2)
4368 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIZ];
4369 break;
4370 case BUILT_IN_TRUNCF:
4371 if (out_mode != SFmode || out_n != 4
4372 || in_mode != SFmode || in_n != 4)
4373 break;
4374 if (VECTOR_UNIT_VSX_P (V4SFmode))
4375 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIZ];
4376 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
4377 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIZ];
4378 break;
4379 case BUILT_IN_NEARBYINT:
4380 if (VECTOR_UNIT_VSX_P (V2DFmode)
4381 && flag_unsafe_math_optimizations
4382 && out_mode == DFmode && out_n == 2
4383 && in_mode == DFmode && in_n == 2)
4384 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPI];
4385 break;
4386 case BUILT_IN_NEARBYINTF:
4387 if (VECTOR_UNIT_VSX_P (V4SFmode)
4388 && flag_unsafe_math_optimizations
4389 && out_mode == SFmode && out_n == 4
4390 && in_mode == SFmode && in_n == 4)
4391 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPI];
4392 break;
4393 case BUILT_IN_RINT:
4394 if (VECTOR_UNIT_VSX_P (V2DFmode)
4395 && !flag_trapping_math
4396 && out_mode == DFmode && out_n == 2
4397 && in_mode == DFmode && in_n == 2)
4398 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIC];
4399 break;
4400 case BUILT_IN_RINTF:
4401 if (VECTOR_UNIT_VSX_P (V4SFmode)
4402 && !flag_trapping_math
4403 && out_mode == SFmode && out_n == 4
4404 && in_mode == SFmode && in_n == 4)
4405 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIC];
4406 break;
4407 default:
4408 break;
4412 else if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
4414 enum rs6000_builtins fn
4415 = (enum rs6000_builtins)DECL_FUNCTION_CODE (fndecl);
4416 switch (fn)
4418 case RS6000_BUILTIN_RSQRTF:
4419 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode)
4420 && out_mode == SFmode && out_n == 4
4421 && in_mode == SFmode && in_n == 4)
4422 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRSQRTFP];
4423 break;
4424 case RS6000_BUILTIN_RSQRT:
4425 if (VECTOR_UNIT_VSX_P (V2DFmode)
4426 && out_mode == DFmode && out_n == 2
4427 && in_mode == DFmode && in_n == 2)
4428 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_2DF];
4429 break;
4430 case RS6000_BUILTIN_RECIPF:
4431 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode)
4432 && out_mode == SFmode && out_n == 4
4433 && in_mode == SFmode && in_n == 4)
4434 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRECIPFP];
4435 break;
4436 case RS6000_BUILTIN_RECIP:
4437 if (VECTOR_UNIT_VSX_P (V2DFmode)
4438 && out_mode == DFmode && out_n == 2
4439 && in_mode == DFmode && in_n == 2)
4440 return rs6000_builtin_decls[VSX_BUILTIN_RECIP_V2DF];
4441 break;
4442 default:
4443 break;
4447 /* Generate calls to libmass if appropriate. */
4448 if (rs6000_veclib_handler)
4449 return rs6000_veclib_handler (fndecl, type_out, type_in);
4451 return NULL_TREE;
4454 /* Default CPU string for rs6000*_file_start functions. */
4455 static const char *rs6000_default_cpu;
4457 /* Do anything needed at the start of the asm file. */
4459 static void
4460 rs6000_file_start (void)
4462 char buffer[80];
4463 const char *start = buffer;
4464 FILE *file = asm_out_file;
4466 rs6000_default_cpu = TARGET_CPU_DEFAULT;
4468 default_file_start ();
4470 if (flag_verbose_asm)
4472 sprintf (buffer, "\n%s rs6000/powerpc options:", ASM_COMMENT_START);
4474 if (rs6000_default_cpu != 0 && rs6000_default_cpu[0] != '\0')
4476 fprintf (file, "%s --with-cpu=%s", start, rs6000_default_cpu);
4477 start = "";
4480 if (global_options_set.x_rs6000_cpu_index)
4482 fprintf (file, "%s -mcpu=%s", start,
4483 processor_target_table[rs6000_cpu_index].name);
4484 start = "";
4487 if (global_options_set.x_rs6000_tune_index)
4489 fprintf (file, "%s -mtune=%s", start,
4490 processor_target_table[rs6000_tune_index].name);
4491 start = "";
4494 if (PPC405_ERRATUM77)
4496 fprintf (file, "%s PPC405CR_ERRATUM77", start);
4497 start = "";
4500 #ifdef USING_ELFOS_H
4501 switch (rs6000_sdata)
4503 case SDATA_NONE: fprintf (file, "%s -msdata=none", start); start = ""; break;
4504 case SDATA_DATA: fprintf (file, "%s -msdata=data", start); start = ""; break;
4505 case SDATA_SYSV: fprintf (file, "%s -msdata=sysv", start); start = ""; break;
4506 case SDATA_EABI: fprintf (file, "%s -msdata=eabi", start); start = ""; break;
4509 if (rs6000_sdata && g_switch_value)
4511 fprintf (file, "%s -G %d", start,
4512 g_switch_value);
4513 start = "";
4515 #endif
4517 if (*start == '\0')
4518 putc ('\n', file);
4521 if (DEFAULT_ABI == ABI_AIX || (TARGET_ELF && flag_pic == 2))
4523 switch_to_section (toc_section);
4524 switch_to_section (text_section);
4529 /* Return nonzero if this function is known to have a null epilogue. */
4532 direct_return (void)
4534 if (reload_completed)
4536 rs6000_stack_t *info = rs6000_stack_info ();
4538 if (info->first_gp_reg_save == 32
4539 && info->first_fp_reg_save == 64
4540 && info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1
4541 && ! info->lr_save_p
4542 && ! info->cr_save_p
4543 && info->vrsave_mask == 0
4544 && ! info->push_p)
4545 return 1;
4548 return 0;
4551 /* Return the number of instructions it takes to form a constant in an
4552 integer register. */
4555 num_insns_constant_wide (HOST_WIDE_INT value)
4557 /* signed constant loadable with addi */
4558 if ((unsigned HOST_WIDE_INT) (value + 0x8000) < 0x10000)
4559 return 1;
4561 /* constant loadable with addis */
4562 else if ((value & 0xffff) == 0
4563 && (value >> 31 == -1 || value >> 31 == 0))
4564 return 1;
4566 else if (TARGET_POWERPC64)
4568 HOST_WIDE_INT low = ((value & 0xffffffff) ^ 0x80000000) - 0x80000000;
4569 HOST_WIDE_INT high = value >> 31;
4571 if (high == 0 || high == -1)
4572 return 2;
4574 high >>= 1;
4576 if (low == 0)
4577 return num_insns_constant_wide (high) + 1;
4578 else if (high == 0)
4579 return num_insns_constant_wide (low) + 1;
4580 else
4581 return (num_insns_constant_wide (high)
4582 + num_insns_constant_wide (low) + 1);
4585 else
4586 return 2;
4590 num_insns_constant (rtx op, enum machine_mode mode)
4592 HOST_WIDE_INT low, high;
4594 switch (GET_CODE (op))
4596 case CONST_INT:
4597 if ((INTVAL (op) >> 31) != 0 && (INTVAL (op) >> 31) != -1
4598 && mask64_operand (op, mode))
4599 return 2;
4600 else
4601 return num_insns_constant_wide (INTVAL (op));
4603 case CONST_DOUBLE:
4604 if (mode == SFmode || mode == SDmode)
4606 long l;
4607 REAL_VALUE_TYPE rv;
4609 REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
4610 if (DECIMAL_FLOAT_MODE_P (mode))
4611 REAL_VALUE_TO_TARGET_DECIMAL32 (rv, l);
4612 else
4613 REAL_VALUE_TO_TARGET_SINGLE (rv, l);
4614 return num_insns_constant_wide ((HOST_WIDE_INT) l);
4617 long l[2];
4618 REAL_VALUE_TYPE rv;
4620 REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
4621 if (DECIMAL_FLOAT_MODE_P (mode))
4622 REAL_VALUE_TO_TARGET_DECIMAL64 (rv, l);
4623 else
4624 REAL_VALUE_TO_TARGET_DOUBLE (rv, l);
4625 high = l[WORDS_BIG_ENDIAN == 0];
4626 low = l[WORDS_BIG_ENDIAN != 0];
4628 if (TARGET_32BIT)
4629 return (num_insns_constant_wide (low)
4630 + num_insns_constant_wide (high));
4631 else
4633 if ((high == 0 && low >= 0)
4634 || (high == -1 && low < 0))
4635 return num_insns_constant_wide (low);
4637 else if (mask64_operand (op, mode))
4638 return 2;
4640 else if (low == 0)
4641 return num_insns_constant_wide (high) + 1;
4643 else
4644 return (num_insns_constant_wide (high)
4645 + num_insns_constant_wide (low) + 1);
4648 default:
4649 gcc_unreachable ();
4653 /* Interpret element ELT of the CONST_VECTOR OP as an integer value.
4654 If the mode of OP is MODE_VECTOR_INT, this simply returns the
4655 corresponding element of the vector, but for V4SFmode and V2SFmode,
4656 the corresponding "float" is interpreted as an SImode integer. */
4658 HOST_WIDE_INT
4659 const_vector_elt_as_int (rtx op, unsigned int elt)
4661 rtx tmp;
4663 /* We can't handle V2DImode and V2DFmode vector constants here yet. */
4664 gcc_assert (GET_MODE (op) != V2DImode
4665 && GET_MODE (op) != V2DFmode);
4667 tmp = CONST_VECTOR_ELT (op, elt);
4668 if (GET_MODE (op) == V4SFmode
4669 || GET_MODE (op) == V2SFmode)
4670 tmp = gen_lowpart (SImode, tmp);
4671 return INTVAL (tmp);
4674 /* Return true if OP can be synthesized with a particular vspltisb, vspltish
4675 or vspltisw instruction. OP is a CONST_VECTOR. Which instruction is used
4676 depends on STEP and COPIES, one of which will be 1. If COPIES > 1,
4677 all items are set to the same value and contain COPIES replicas of the
4678 vsplt's operand; if STEP > 1, one in STEP elements is set to the vsplt's
4679 operand and the others are set to the value of the operand's msb. */
4681 static bool
4682 vspltis_constant (rtx op, unsigned step, unsigned copies)
4684 enum machine_mode mode = GET_MODE (op);
4685 enum machine_mode inner = GET_MODE_INNER (mode);
4687 unsigned i;
4688 unsigned nunits;
4689 unsigned bitsize;
4690 unsigned mask;
4692 HOST_WIDE_INT val;
4693 HOST_WIDE_INT splat_val;
4694 HOST_WIDE_INT msb_val;
4696 if (mode == V2DImode || mode == V2DFmode)
4697 return false;
4699 nunits = GET_MODE_NUNITS (mode);
4700 bitsize = GET_MODE_BITSIZE (inner);
4701 mask = GET_MODE_MASK (inner);
4703 val = const_vector_elt_as_int (op, BYTES_BIG_ENDIAN ? nunits - 1 : 0);
4704 splat_val = val;
4705 msb_val = val > 0 ? 0 : -1;
4707 /* Construct the value to be splatted, if possible. If not, return 0. */
4708 for (i = 2; i <= copies; i *= 2)
4710 HOST_WIDE_INT small_val;
4711 bitsize /= 2;
4712 small_val = splat_val >> bitsize;
4713 mask >>= bitsize;
4714 if (splat_val != ((small_val << bitsize) | (small_val & mask)))
4715 return false;
4716 splat_val = small_val;
4719 /* Check if SPLAT_VAL can really be the operand of a vspltis[bhw]. */
4720 if (EASY_VECTOR_15 (splat_val))
4723 /* Also check if we can splat, and then add the result to itself. Do so if
4724 the value is positive, of if the splat instruction is using OP's mode;
4725 for splat_val < 0, the splat and the add should use the same mode. */
4726 else if (EASY_VECTOR_15_ADD_SELF (splat_val)
4727 && (splat_val >= 0 || (step == 1 && copies == 1)))
4730 /* Also check if are loading up the most significant bit which can be done by
4731 loading up -1 and shifting the value left by -1. */
4732 else if (EASY_VECTOR_MSB (splat_val, inner))
4735 else
4736 return false;
4738 /* Check if VAL is present in every STEP-th element, and the
4739 other elements are filled with its most significant bit. */
4740 for (i = 0; i < nunits - 1; ++i)
4742 HOST_WIDE_INT desired_val;
4743 if (((BYTES_BIG_ENDIAN ? i + 1 : i) & (step - 1)) == 0)
4744 desired_val = val;
4745 else
4746 desired_val = msb_val;
4748 if (desired_val != const_vector_elt_as_int (op, i))
4749 return false;
4752 return true;
4756 /* Return true if OP is of the given MODE and can be synthesized
4757 with a vspltisb, vspltish or vspltisw. */
4759 bool
4760 easy_altivec_constant (rtx op, enum machine_mode mode)
4762 unsigned step, copies;
4764 if (mode == VOIDmode)
4765 mode = GET_MODE (op);
4766 else if (mode != GET_MODE (op))
4767 return false;
4769 /* V2DI/V2DF was added with VSX. Only allow 0 and all 1's as easy
4770 constants. */
4771 if (mode == V2DFmode)
4772 return zero_constant (op, mode);
4774 if (mode == V2DImode)
4776 /* In case the compiler is built 32-bit, CONST_DOUBLE constants are not
4777 easy. */
4778 if (GET_CODE (CONST_VECTOR_ELT (op, 0)) != CONST_INT
4779 || GET_CODE (CONST_VECTOR_ELT (op, 1)) != CONST_INT)
4780 return false;
4782 if (zero_constant (op, mode))
4783 return true;
4785 if (INTVAL (CONST_VECTOR_ELT (op, 0)) == -1
4786 && INTVAL (CONST_VECTOR_ELT (op, 1)) == -1)
4787 return true;
4789 return false;
4792 /* Start with a vspltisw. */
4793 step = GET_MODE_NUNITS (mode) / 4;
4794 copies = 1;
4796 if (vspltis_constant (op, step, copies))
4797 return true;
4799 /* Then try with a vspltish. */
4800 if (step == 1)
4801 copies <<= 1;
4802 else
4803 step >>= 1;
4805 if (vspltis_constant (op, step, copies))
4806 return true;
4808 /* And finally a vspltisb. */
4809 if (step == 1)
4810 copies <<= 1;
4811 else
4812 step >>= 1;
4814 if (vspltis_constant (op, step, copies))
4815 return true;
4817 return false;
4820 /* Generate a VEC_DUPLICATE representing a vspltis[bhw] instruction whose
4821 result is OP. Abort if it is not possible. */
4824 gen_easy_altivec_constant (rtx op)
4826 enum machine_mode mode = GET_MODE (op);
4827 int nunits = GET_MODE_NUNITS (mode);
4828 rtx val = CONST_VECTOR_ELT (op, BYTES_BIG_ENDIAN ? nunits - 1 : 0);
4829 unsigned step = nunits / 4;
4830 unsigned copies = 1;
4832 /* Start with a vspltisw. */
4833 if (vspltis_constant (op, step, copies))
4834 return gen_rtx_VEC_DUPLICATE (V4SImode, gen_lowpart (SImode, val));
4836 /* Then try with a vspltish. */
4837 if (step == 1)
4838 copies <<= 1;
4839 else
4840 step >>= 1;
4842 if (vspltis_constant (op, step, copies))
4843 return gen_rtx_VEC_DUPLICATE (V8HImode, gen_lowpart (HImode, val));
4845 /* And finally a vspltisb. */
4846 if (step == 1)
4847 copies <<= 1;
4848 else
4849 step >>= 1;
4851 if (vspltis_constant (op, step, copies))
4852 return gen_rtx_VEC_DUPLICATE (V16QImode, gen_lowpart (QImode, val));
4854 gcc_unreachable ();
4857 const char *
4858 output_vec_const_move (rtx *operands)
4860 int cst, cst2;
4861 enum machine_mode mode;
4862 rtx dest, vec;
4864 dest = operands[0];
4865 vec = operands[1];
4866 mode = GET_MODE (dest);
4868 if (TARGET_VSX)
4870 if (zero_constant (vec, mode))
4871 return "xxlxor %x0,%x0,%x0";
4873 if (mode == V2DImode
4874 && INTVAL (CONST_VECTOR_ELT (vec, 0)) == -1
4875 && INTVAL (CONST_VECTOR_ELT (vec, 1)) == -1)
4876 return "vspltisw %0,-1";
4879 if (TARGET_ALTIVEC)
4881 rtx splat_vec;
4882 if (zero_constant (vec, mode))
4883 return "vxor %0,%0,%0";
4885 splat_vec = gen_easy_altivec_constant (vec);
4886 gcc_assert (GET_CODE (splat_vec) == VEC_DUPLICATE);
4887 operands[1] = XEXP (splat_vec, 0);
4888 if (!EASY_VECTOR_15 (INTVAL (operands[1])))
4889 return "#";
4891 switch (GET_MODE (splat_vec))
4893 case V4SImode:
4894 return "vspltisw %0,%1";
4896 case V8HImode:
4897 return "vspltish %0,%1";
4899 case V16QImode:
4900 return "vspltisb %0,%1";
4902 default:
4903 gcc_unreachable ();
4907 gcc_assert (TARGET_SPE);
4909 /* Vector constant 0 is handled as a splitter of V2SI, and in the
4910 pattern of V1DI, V4HI, and V2SF.
4912 FIXME: We should probably return # and add post reload
4913 splitters for these, but this way is so easy ;-). */
4914 cst = INTVAL (CONST_VECTOR_ELT (vec, 0));
4915 cst2 = INTVAL (CONST_VECTOR_ELT (vec, 1));
4916 operands[1] = CONST_VECTOR_ELT (vec, 0);
4917 operands[2] = CONST_VECTOR_ELT (vec, 1);
4918 if (cst == cst2)
4919 return "li %0,%1\n\tevmergelo %0,%0,%0";
4920 else
4921 return "li %0,%1\n\tevmergelo %0,%0,%0\n\tli %0,%2";
4924 /* Initialize TARGET of vector PAIRED to VALS. */
4926 void
4927 paired_expand_vector_init (rtx target, rtx vals)
4929 enum machine_mode mode = GET_MODE (target);
4930 int n_elts = GET_MODE_NUNITS (mode);
4931 int n_var = 0;
4932 rtx x, new_rtx, tmp, constant_op, op1, op2;
4933 int i;
4935 for (i = 0; i < n_elts; ++i)
4937 x = XVECEXP (vals, 0, i);
4938 if (!(CONST_INT_P (x)
4939 || GET_CODE (x) == CONST_DOUBLE
4940 || GET_CODE (x) == CONST_FIXED))
4941 ++n_var;
4943 if (n_var == 0)
4945 /* Load from constant pool. */
4946 emit_move_insn (target, gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)));
4947 return;
4950 if (n_var == 2)
4952 /* The vector is initialized only with non-constants. */
4953 new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, XVECEXP (vals, 0, 0),
4954 XVECEXP (vals, 0, 1));
4956 emit_move_insn (target, new_rtx);
4957 return;
4960 /* One field is non-constant and the other one is a constant. Load the
4961 constant from the constant pool and use ps_merge instruction to
4962 construct the whole vector. */
4963 op1 = XVECEXP (vals, 0, 0);
4964 op2 = XVECEXP (vals, 0, 1);
4966 constant_op = (CONSTANT_P (op1)) ? op1 : op2;
4968 tmp = gen_reg_rtx (GET_MODE (constant_op));
4969 emit_move_insn (tmp, constant_op);
4971 if (CONSTANT_P (op1))
4972 new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, tmp, op2);
4973 else
4974 new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, op1, tmp);
4976 emit_move_insn (target, new_rtx);
4979 void
4980 paired_expand_vector_move (rtx operands[])
4982 rtx op0 = operands[0], op1 = operands[1];
4984 emit_move_insn (op0, op1);
4987 /* Emit vector compare for code RCODE. DEST is destination, OP1 and
4988 OP2 are two VEC_COND_EXPR operands, CC_OP0 and CC_OP1 are the two
4989 operands for the relation operation COND. This is a recursive
4990 function. */
4992 static void
4993 paired_emit_vector_compare (enum rtx_code rcode,
4994 rtx dest, rtx op0, rtx op1,
4995 rtx cc_op0, rtx cc_op1)
4997 rtx tmp = gen_reg_rtx (V2SFmode);
4998 rtx tmp1, max, min;
5000 gcc_assert (TARGET_PAIRED_FLOAT);
5001 gcc_assert (GET_MODE (op0) == GET_MODE (op1));
5003 switch (rcode)
5005 case LT:
5006 case LTU:
5007 paired_emit_vector_compare (GE, dest, op1, op0, cc_op0, cc_op1);
5008 return;
5009 case GE:
5010 case GEU:
5011 emit_insn (gen_subv2sf3 (tmp, cc_op0, cc_op1));
5012 emit_insn (gen_selv2sf4 (dest, tmp, op0, op1, CONST0_RTX (SFmode)));
5013 return;
5014 case LE:
5015 case LEU:
5016 paired_emit_vector_compare (GE, dest, op0, op1, cc_op1, cc_op0);
5017 return;
5018 case GT:
5019 paired_emit_vector_compare (LE, dest, op1, op0, cc_op0, cc_op1);
5020 return;
5021 case EQ:
5022 tmp1 = gen_reg_rtx (V2SFmode);
5023 max = gen_reg_rtx (V2SFmode);
5024 min = gen_reg_rtx (V2SFmode);
5025 gen_reg_rtx (V2SFmode);
5027 emit_insn (gen_subv2sf3 (tmp, cc_op0, cc_op1));
5028 emit_insn (gen_selv2sf4
5029 (max, tmp, cc_op0, cc_op1, CONST0_RTX (SFmode)));
5030 emit_insn (gen_subv2sf3 (tmp, cc_op1, cc_op0));
5031 emit_insn (gen_selv2sf4
5032 (min, tmp, cc_op0, cc_op1, CONST0_RTX (SFmode)));
5033 emit_insn (gen_subv2sf3 (tmp1, min, max));
5034 emit_insn (gen_selv2sf4 (dest, tmp1, op0, op1, CONST0_RTX (SFmode)));
5035 return;
5036 case NE:
5037 paired_emit_vector_compare (EQ, dest, op1, op0, cc_op0, cc_op1);
5038 return;
5039 case UNLE:
5040 paired_emit_vector_compare (LE, dest, op1, op0, cc_op0, cc_op1);
5041 return;
5042 case UNLT:
5043 paired_emit_vector_compare (LT, dest, op1, op0, cc_op0, cc_op1);
5044 return;
5045 case UNGE:
5046 paired_emit_vector_compare (GE, dest, op1, op0, cc_op0, cc_op1);
5047 return;
5048 case UNGT:
5049 paired_emit_vector_compare (GT, dest, op1, op0, cc_op0, cc_op1);
5050 return;
5051 default:
5052 gcc_unreachable ();
5055 return;
5058 /* Emit vector conditional expression.
5059 DEST is destination. OP1 and OP2 are two VEC_COND_EXPR operands.
5060 CC_OP0 and CC_OP1 are the two operands for the relation operation COND. */
5063 paired_emit_vector_cond_expr (rtx dest, rtx op1, rtx op2,
5064 rtx cond, rtx cc_op0, rtx cc_op1)
5066 enum rtx_code rcode = GET_CODE (cond);
5068 if (!TARGET_PAIRED_FLOAT)
5069 return 0;
5071 paired_emit_vector_compare (rcode, dest, op1, op2, cc_op0, cc_op1);
5073 return 1;
5076 /* Initialize vector TARGET to VALS. */
5078 void
5079 rs6000_expand_vector_init (rtx target, rtx vals)
5081 enum machine_mode mode = GET_MODE (target);
5082 enum machine_mode inner_mode = GET_MODE_INNER (mode);
5083 int n_elts = GET_MODE_NUNITS (mode);
5084 int n_var = 0, one_var = -1;
5085 bool all_same = true, all_const_zero = true;
5086 rtx x, mem;
5087 int i;
5089 for (i = 0; i < n_elts; ++i)
5091 x = XVECEXP (vals, 0, i);
5092 if (!(CONST_INT_P (x)
5093 || GET_CODE (x) == CONST_DOUBLE
5094 || GET_CODE (x) == CONST_FIXED))
5095 ++n_var, one_var = i;
5096 else if (x != CONST0_RTX (inner_mode))
5097 all_const_zero = false;
5099 if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
5100 all_same = false;
5103 if (n_var == 0)
5105 rtx const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0));
5106 bool int_vector_p = (GET_MODE_CLASS (mode) == MODE_VECTOR_INT);
5107 if ((int_vector_p || TARGET_VSX) && all_const_zero)
5109 /* Zero register. */
5110 emit_insn (gen_rtx_SET (VOIDmode, target,
5111 gen_rtx_XOR (mode, target, target)));
5112 return;
5114 else if (int_vector_p && easy_vector_constant (const_vec, mode))
5116 /* Splat immediate. */
5117 emit_insn (gen_rtx_SET (VOIDmode, target, const_vec));
5118 return;
5120 else
5122 /* Load from constant pool. */
5123 emit_move_insn (target, const_vec);
5124 return;
5128 /* Double word values on VSX can use xxpermdi or lxvdsx. */
5129 if (VECTOR_MEM_VSX_P (mode) && (mode == V2DFmode || mode == V2DImode))
5131 rtx op0 = XVECEXP (vals, 0, 0);
5132 rtx op1 = XVECEXP (vals, 0, 1);
5133 if (all_same)
5135 if (!MEM_P (op0) && !REG_P (op0))
5136 op0 = force_reg (inner_mode, op0);
5137 if (mode == V2DFmode)
5138 emit_insn (gen_vsx_splat_v2df (target, op0));
5139 else
5140 emit_insn (gen_vsx_splat_v2di (target, op0));
5142 else
5144 op0 = force_reg (inner_mode, op0);
5145 op1 = force_reg (inner_mode, op1);
5146 if (mode == V2DFmode)
5147 emit_insn (gen_vsx_concat_v2df (target, op0, op1));
5148 else
5149 emit_insn (gen_vsx_concat_v2di (target, op0, op1));
5151 return;
5154 /* With single precision floating point on VSX, know that internally single
5155 precision is actually represented as a double, and either make 2 V2DF
5156 vectors, and convert these vectors to single precision, or do one
5157 conversion, and splat the result to the other elements. */
5158 if (mode == V4SFmode && VECTOR_MEM_VSX_P (mode))
5160 if (all_same)
5162 rtx freg = gen_reg_rtx (V4SFmode);
5163 rtx sreg = force_reg (SFmode, XVECEXP (vals, 0, 0));
5164 rtx cvt = ((TARGET_XSCVDPSPN)
5165 ? gen_vsx_xscvdpspn_scalar (freg, sreg)
5166 : gen_vsx_xscvdpsp_scalar (freg, sreg));
5168 emit_insn (cvt);
5169 emit_insn (gen_vsx_xxspltw_v4sf (target, freg, const0_rtx));
5171 else
5173 rtx dbl_even = gen_reg_rtx (V2DFmode);
5174 rtx dbl_odd = gen_reg_rtx (V2DFmode);
5175 rtx flt_even = gen_reg_rtx (V4SFmode);
5176 rtx flt_odd = gen_reg_rtx (V4SFmode);
5177 rtx op0 = force_reg (SFmode, XVECEXP (vals, 0, 0));
5178 rtx op1 = force_reg (SFmode, XVECEXP (vals, 0, 1));
5179 rtx op2 = force_reg (SFmode, XVECEXP (vals, 0, 2));
5180 rtx op3 = force_reg (SFmode, XVECEXP (vals, 0, 3));
5182 emit_insn (gen_vsx_concat_v2sf (dbl_even, op0, op1));
5183 emit_insn (gen_vsx_concat_v2sf (dbl_odd, op2, op3));
5184 emit_insn (gen_vsx_xvcvdpsp (flt_even, dbl_even));
5185 emit_insn (gen_vsx_xvcvdpsp (flt_odd, dbl_odd));
5186 rs6000_expand_extract_even (target, flt_even, flt_odd);
5188 return;
5191 /* Store value to stack temp. Load vector element. Splat. However, splat
5192 of 64-bit items is not supported on Altivec. */
5193 if (all_same && GET_MODE_SIZE (inner_mode) <= 4)
5195 rtx field;
5196 mem = assign_stack_temp (mode, GET_MODE_SIZE (inner_mode));
5197 emit_move_insn (adjust_address_nv (mem, inner_mode, 0),
5198 XVECEXP (vals, 0, 0));
5199 x = gen_rtx_UNSPEC (VOIDmode,
5200 gen_rtvec (1, const0_rtx), UNSPEC_LVE);
5201 emit_insn (gen_rtx_PARALLEL (VOIDmode,
5202 gen_rtvec (2,
5203 gen_rtx_SET (VOIDmode,
5204 target, mem),
5205 x)));
5206 field = (BYTES_BIG_ENDIAN ? const0_rtx
5207 : GEN_INT (GET_MODE_NUNITS (mode) - 1));
5208 x = gen_rtx_VEC_SELECT (inner_mode, target,
5209 gen_rtx_PARALLEL (VOIDmode,
5210 gen_rtvec (1, field)));
5211 emit_insn (gen_rtx_SET (VOIDmode, target,
5212 gen_rtx_VEC_DUPLICATE (mode, x)));
5213 return;
5216 /* One field is non-constant. Load constant then overwrite
5217 varying field. */
5218 if (n_var == 1)
5220 rtx copy = copy_rtx (vals);
5222 /* Load constant part of vector, substitute neighboring value for
5223 varying element. */
5224 XVECEXP (copy, 0, one_var) = XVECEXP (vals, 0, (one_var + 1) % n_elts);
5225 rs6000_expand_vector_init (target, copy);
5227 /* Insert variable. */
5228 rs6000_expand_vector_set (target, XVECEXP (vals, 0, one_var), one_var);
5229 return;
5232 /* Construct the vector in memory one field at a time
5233 and load the whole vector. */
5234 mem = assign_stack_temp (mode, GET_MODE_SIZE (mode));
5235 for (i = 0; i < n_elts; i++)
5236 emit_move_insn (adjust_address_nv (mem, inner_mode,
5237 i * GET_MODE_SIZE (inner_mode)),
5238 XVECEXP (vals, 0, i));
5239 emit_move_insn (target, mem);
5242 /* Set field ELT of TARGET to VAL. */
5244 void
5245 rs6000_expand_vector_set (rtx target, rtx val, int elt)
5247 enum machine_mode mode = GET_MODE (target);
5248 enum machine_mode inner_mode = GET_MODE_INNER (mode);
5249 rtx reg = gen_reg_rtx (mode);
5250 rtx mask, mem, x;
5251 int width = GET_MODE_SIZE (inner_mode);
5252 int i;
5254 if (VECTOR_MEM_VSX_P (mode) && (mode == V2DFmode || mode == V2DImode))
5256 rtx (*set_func) (rtx, rtx, rtx, rtx)
5257 = ((mode == V2DFmode) ? gen_vsx_set_v2df : gen_vsx_set_v2di);
5258 emit_insn (set_func (target, target, val, GEN_INT (elt)));
5259 return;
5262 /* Load single variable value. */
5263 mem = assign_stack_temp (mode, GET_MODE_SIZE (inner_mode));
5264 emit_move_insn (adjust_address_nv (mem, inner_mode, 0), val);
5265 x = gen_rtx_UNSPEC (VOIDmode,
5266 gen_rtvec (1, const0_rtx), UNSPEC_LVE);
5267 emit_insn (gen_rtx_PARALLEL (VOIDmode,
5268 gen_rtvec (2,
5269 gen_rtx_SET (VOIDmode,
5270 reg, mem),
5271 x)));
5273 /* Linear sequence. */
5274 mask = gen_rtx_PARALLEL (V16QImode, rtvec_alloc (16));
5275 for (i = 0; i < 16; ++i)
5276 XVECEXP (mask, 0, i) = GEN_INT (i);
5278 /* Set permute mask to insert element into target. */
5279 for (i = 0; i < width; ++i)
5280 XVECEXP (mask, 0, elt*width + i)
5281 = GEN_INT (i + 0x10);
5282 x = gen_rtx_CONST_VECTOR (V16QImode, XVEC (mask, 0));
5283 x = gen_rtx_UNSPEC (mode,
5284 gen_rtvec (3, target, reg,
5285 force_reg (V16QImode, x)),
5286 UNSPEC_VPERM);
5287 emit_insn (gen_rtx_SET (VOIDmode, target, x));
5290 /* Extract field ELT from VEC into TARGET. */
5292 void
5293 rs6000_expand_vector_extract (rtx target, rtx vec, int elt)
5295 enum machine_mode mode = GET_MODE (vec);
5296 enum machine_mode inner_mode = GET_MODE_INNER (mode);
5297 rtx mem;
5299 if (VECTOR_MEM_VSX_P (mode))
5301 switch (mode)
5303 default:
5304 break;
5305 case V2DFmode:
5306 emit_insn (gen_vsx_extract_v2df (target, vec, GEN_INT (elt)));
5307 return;
5308 case V2DImode:
5309 emit_insn (gen_vsx_extract_v2di (target, vec, GEN_INT (elt)));
5310 return;
5311 case V4SFmode:
5312 emit_insn (gen_vsx_extract_v4sf (target, vec, GEN_INT (elt)));
5313 return;
5317 /* Allocate mode-sized buffer. */
5318 mem = assign_stack_temp (mode, GET_MODE_SIZE (mode));
5320 emit_move_insn (mem, vec);
5322 /* Add offset to field within buffer matching vector element. */
5323 mem = adjust_address_nv (mem, inner_mode, elt * GET_MODE_SIZE (inner_mode));
5325 emit_move_insn (target, adjust_address_nv (mem, inner_mode, 0));
5328 /* Generates shifts and masks for a pair of rldicl or rldicr insns to
5329 implement ANDing by the mask IN. */
5330 void
5331 build_mask64_2_operands (rtx in, rtx *out)
5333 unsigned HOST_WIDE_INT c, lsb, m1, m2;
5334 int shift;
5336 gcc_assert (GET_CODE (in) == CONST_INT);
5338 c = INTVAL (in);
5339 if (c & 1)
5341 /* Assume c initially something like 0x00fff000000fffff. The idea
5342 is to rotate the word so that the middle ^^^^^^ group of zeros
5343 is at the MS end and can be cleared with an rldicl mask. We then
5344 rotate back and clear off the MS ^^ group of zeros with a
5345 second rldicl. */
5346 c = ~c; /* c == 0xff000ffffff00000 */
5347 lsb = c & -c; /* lsb == 0x0000000000100000 */
5348 m1 = -lsb; /* m1 == 0xfffffffffff00000 */
5349 c = ~c; /* c == 0x00fff000000fffff */
5350 c &= -lsb; /* c == 0x00fff00000000000 */
5351 lsb = c & -c; /* lsb == 0x0000100000000000 */
5352 c = ~c; /* c == 0xff000fffffffffff */
5353 c &= -lsb; /* c == 0xff00000000000000 */
5354 shift = 0;
5355 while ((lsb >>= 1) != 0)
5356 shift++; /* shift == 44 on exit from loop */
5357 m1 <<= 64 - shift; /* m1 == 0xffffff0000000000 */
5358 m1 = ~m1; /* m1 == 0x000000ffffffffff */
5359 m2 = ~c; /* m2 == 0x00ffffffffffffff */
5361 else
5363 /* Assume c initially something like 0xff000f0000000000. The idea
5364 is to rotate the word so that the ^^^ middle group of zeros
5365 is at the LS end and can be cleared with an rldicr mask. We then
5366 rotate back and clear off the LS group of ^^^^^^^^^^ zeros with
5367 a second rldicr. */
5368 lsb = c & -c; /* lsb == 0x0000010000000000 */
5369 m2 = -lsb; /* m2 == 0xffffff0000000000 */
5370 c = ~c; /* c == 0x00fff0ffffffffff */
5371 c &= -lsb; /* c == 0x00fff00000000000 */
5372 lsb = c & -c; /* lsb == 0x0000100000000000 */
5373 c = ~c; /* c == 0xff000fffffffffff */
5374 c &= -lsb; /* c == 0xff00000000000000 */
5375 shift = 0;
5376 while ((lsb >>= 1) != 0)
5377 shift++; /* shift == 44 on exit from loop */
5378 m1 = ~c; /* m1 == 0x00ffffffffffffff */
5379 m1 >>= shift; /* m1 == 0x0000000000000fff */
5380 m1 = ~m1; /* m1 == 0xfffffffffffff000 */
5383 /* Note that when we only have two 0->1 and 1->0 transitions, one of the
5384 masks will be all 1's. We are guaranteed more than one transition. */
5385 out[0] = GEN_INT (64 - shift);
5386 out[1] = GEN_INT (m1);
5387 out[2] = GEN_INT (shift);
5388 out[3] = GEN_INT (m2);
5391 /* Return TRUE if OP is an invalid SUBREG operation on the e500. */
5393 bool
5394 invalid_e500_subreg (rtx op, enum machine_mode mode)
5396 if (TARGET_E500_DOUBLE)
5398 /* Reject (subreg:SI (reg:DF)); likewise with subreg:DI or
5399 subreg:TI and reg:TF. Decimal float modes are like integer
5400 modes (only low part of each register used) for this
5401 purpose. */
5402 if (GET_CODE (op) == SUBREG
5403 && (mode == SImode || mode == DImode || mode == TImode
5404 || mode == DDmode || mode == TDmode || mode == PTImode)
5405 && REG_P (SUBREG_REG (op))
5406 && (GET_MODE (SUBREG_REG (op)) == DFmode
5407 || GET_MODE (SUBREG_REG (op)) == TFmode))
5408 return true;
5410 /* Reject (subreg:DF (reg:DI)); likewise with subreg:TF and
5411 reg:TI. */
5412 if (GET_CODE (op) == SUBREG
5413 && (mode == DFmode || mode == TFmode)
5414 && REG_P (SUBREG_REG (op))
5415 && (GET_MODE (SUBREG_REG (op)) == DImode
5416 || GET_MODE (SUBREG_REG (op)) == TImode
5417 || GET_MODE (SUBREG_REG (op)) == PTImode
5418 || GET_MODE (SUBREG_REG (op)) == DDmode
5419 || GET_MODE (SUBREG_REG (op)) == TDmode))
5420 return true;
5423 if (TARGET_SPE
5424 && GET_CODE (op) == SUBREG
5425 && mode == SImode
5426 && REG_P (SUBREG_REG (op))
5427 && SPE_VECTOR_MODE (GET_MODE (SUBREG_REG (op))))
5428 return true;
5430 return false;
5433 /* Return alignment of TYPE. Existing alignment is ALIGN. HOW
5434 selects whether the alignment is abi mandated, optional, or
5435 both abi and optional alignment. */
5437 unsigned int
5438 rs6000_data_alignment (tree type, unsigned int align, enum data_align how)
5440 if (how != align_opt)
5442 if (TREE_CODE (type) == VECTOR_TYPE)
5444 if ((TARGET_SPE && SPE_VECTOR_MODE (TYPE_MODE (type)))
5445 || (TARGET_PAIRED_FLOAT && PAIRED_VECTOR_MODE (TYPE_MODE (type))))
5447 if (align < 64)
5448 align = 64;
5450 else if (align < 128)
5451 align = 128;
5453 else if (TARGET_E500_DOUBLE
5454 && TREE_CODE (type) == REAL_TYPE
5455 && TYPE_MODE (type) == DFmode)
5457 if (align < 64)
5458 align = 64;
5462 if (how != align_abi)
5464 if (TREE_CODE (type) == ARRAY_TYPE
5465 && TYPE_MODE (TREE_TYPE (type)) == QImode)
5467 if (align < BITS_PER_WORD)
5468 align = BITS_PER_WORD;
5472 return align;
5475 /* AIX increases natural record alignment to doubleword if the first
5476 field is an FP double while the FP fields remain word aligned. */
5478 unsigned int
5479 rs6000_special_round_type_align (tree type, unsigned int computed,
5480 unsigned int specified)
5482 unsigned int align = MAX (computed, specified);
5483 tree field = TYPE_FIELDS (type);
5485 /* Skip all non field decls */
5486 while (field != NULL && TREE_CODE (field) != FIELD_DECL)
5487 field = DECL_CHAIN (field);
5489 if (field != NULL && field != type)
5491 type = TREE_TYPE (field);
5492 while (TREE_CODE (type) == ARRAY_TYPE)
5493 type = TREE_TYPE (type);
5495 if (type != error_mark_node && TYPE_MODE (type) == DFmode)
5496 align = MAX (align, 64);
5499 return align;
5502 /* Darwin increases record alignment to the natural alignment of
5503 the first field. */
5505 unsigned int
5506 darwin_rs6000_special_round_type_align (tree type, unsigned int computed,
5507 unsigned int specified)
5509 unsigned int align = MAX (computed, specified);
5511 if (TYPE_PACKED (type))
5512 return align;
5514 /* Find the first field, looking down into aggregates. */
5515 do {
5516 tree field = TYPE_FIELDS (type);
5517 /* Skip all non field decls */
5518 while (field != NULL && TREE_CODE (field) != FIELD_DECL)
5519 field = DECL_CHAIN (field);
5520 if (! field)
5521 break;
5522 /* A packed field does not contribute any extra alignment. */
5523 if (DECL_PACKED (field))
5524 return align;
5525 type = TREE_TYPE (field);
5526 while (TREE_CODE (type) == ARRAY_TYPE)
5527 type = TREE_TYPE (type);
5528 } while (AGGREGATE_TYPE_P (type));
5530 if (! AGGREGATE_TYPE_P (type) && type != error_mark_node)
5531 align = MAX (align, TYPE_ALIGN (type));
5533 return align;
5536 /* Return 1 for an operand in small memory on V.4/eabi. */
5539 small_data_operand (rtx op ATTRIBUTE_UNUSED,
5540 enum machine_mode mode ATTRIBUTE_UNUSED)
5542 #if TARGET_ELF
5543 rtx sym_ref;
5545 if (rs6000_sdata == SDATA_NONE || rs6000_sdata == SDATA_DATA)
5546 return 0;
5548 if (DEFAULT_ABI != ABI_V4)
5549 return 0;
5551 /* Vector and float memory instructions have a limited offset on the
5552 SPE, so using a vector or float variable directly as an operand is
5553 not useful. */
5554 if (TARGET_SPE
5555 && (SPE_VECTOR_MODE (mode) || FLOAT_MODE_P (mode)))
5556 return 0;
5558 if (GET_CODE (op) == SYMBOL_REF)
5559 sym_ref = op;
5561 else if (GET_CODE (op) != CONST
5562 || GET_CODE (XEXP (op, 0)) != PLUS
5563 || GET_CODE (XEXP (XEXP (op, 0), 0)) != SYMBOL_REF
5564 || GET_CODE (XEXP (XEXP (op, 0), 1)) != CONST_INT)
5565 return 0;
5567 else
5569 rtx sum = XEXP (op, 0);
5570 HOST_WIDE_INT summand;
5572 /* We have to be careful here, because it is the referenced address
5573 that must be 32k from _SDA_BASE_, not just the symbol. */
5574 summand = INTVAL (XEXP (sum, 1));
5575 if (summand < 0 || summand > g_switch_value)
5576 return 0;
5578 sym_ref = XEXP (sum, 0);
5581 return SYMBOL_REF_SMALL_P (sym_ref);
5582 #else
5583 return 0;
5584 #endif
5587 /* Return true if either operand is a general purpose register. */
5589 bool
5590 gpr_or_gpr_p (rtx op0, rtx op1)
5592 return ((REG_P (op0) && INT_REGNO_P (REGNO (op0)))
5593 || (REG_P (op1) && INT_REGNO_P (REGNO (op1))));
5596 /* Return true if this is a move direct operation between GPR registers and
5597 floating point/VSX registers. */
5599 bool
5600 direct_move_p (rtx op0, rtx op1)
5602 int regno0, regno1;
5604 if (!REG_P (op0) || !REG_P (op1))
5605 return false;
5607 if (!TARGET_DIRECT_MOVE && !TARGET_MFPGPR)
5608 return false;
5610 regno0 = REGNO (op0);
5611 regno1 = REGNO (op1);
5612 if (regno0 >= FIRST_PSEUDO_REGISTER || regno1 >= FIRST_PSEUDO_REGISTER)
5613 return false;
5615 if (INT_REGNO_P (regno0))
5616 return (TARGET_DIRECT_MOVE) ? VSX_REGNO_P (regno1) : FP_REGNO_P (regno1);
5618 else if (INT_REGNO_P (regno1))
5620 if (TARGET_MFPGPR && FP_REGNO_P (regno0))
5621 return true;
5623 else if (TARGET_DIRECT_MOVE && VSX_REGNO_P (regno0))
5624 return true;
5627 return false;
5630 /* Return true if this is a load or store quad operation. */
5632 bool
5633 quad_load_store_p (rtx op0, rtx op1)
5635 bool ret;
5637 if (!TARGET_QUAD_MEMORY)
5638 ret = false;
5640 else if (REG_P (op0) && MEM_P (op1))
5641 ret = (quad_int_reg_operand (op0, GET_MODE (op0))
5642 && quad_memory_operand (op1, GET_MODE (op1))
5643 && !reg_overlap_mentioned_p (op0, op1));
5645 else if (MEM_P (op0) && REG_P (op1))
5646 ret = (quad_memory_operand (op0, GET_MODE (op0))
5647 && quad_int_reg_operand (op1, GET_MODE (op1)));
5649 else
5650 ret = false;
5652 if (TARGET_DEBUG_ADDR)
5654 fprintf (stderr, "\n========== quad_load_store, return %s\n",
5655 ret ? "true" : "false");
5656 debug_rtx (gen_rtx_SET (VOIDmode, op0, op1));
5659 return ret;
5662 /* Given an address, return a constant offset term if one exists. */
5664 static rtx
5665 address_offset (rtx op)
5667 if (GET_CODE (op) == PRE_INC
5668 || GET_CODE (op) == PRE_DEC)
5669 op = XEXP (op, 0);
5670 else if (GET_CODE (op) == PRE_MODIFY
5671 || GET_CODE (op) == LO_SUM)
5672 op = XEXP (op, 1);
5674 if (GET_CODE (op) == CONST)
5675 op = XEXP (op, 0);
5677 if (GET_CODE (op) == PLUS)
5678 op = XEXP (op, 1);
5680 if (CONST_INT_P (op))
5681 return op;
5683 return NULL_RTX;
5686 /* Return true if the MEM operand is a memory operand suitable for use
5687 with a (full width, possibly multiple) gpr load/store. On
5688 powerpc64 this means the offset must be divisible by 4.
5689 Implements 'Y' constraint.
5691 Accept direct, indexed, offset, lo_sum and tocref. Since this is
5692 a constraint function we know the operand has satisfied a suitable
5693 memory predicate. Also accept some odd rtl generated by reload
5694 (see rs6000_legitimize_reload_address for various forms). It is
5695 important that reload rtl be accepted by appropriate constraints
5696 but not by the operand predicate.
5698 Offsetting a lo_sum should not be allowed, except where we know by
5699 alignment that a 32k boundary is not crossed, but see the ???
5700 comment in rs6000_legitimize_reload_address. Note that by
5701 "offsetting" here we mean a further offset to access parts of the
5702 MEM. It's fine to have a lo_sum where the inner address is offset
5703 from a sym, since the same sym+offset will appear in the high part
5704 of the address calculation. */
5706 bool
5707 mem_operand_gpr (rtx op, enum machine_mode mode)
5709 unsigned HOST_WIDE_INT offset;
5710 int extra;
5711 rtx addr = XEXP (op, 0);
5713 op = address_offset (addr);
5714 if (op == NULL_RTX)
5715 return true;
5717 offset = INTVAL (op);
5718 if (TARGET_POWERPC64 && (offset & 3) != 0)
5719 return false;
5721 extra = GET_MODE_SIZE (mode) - UNITS_PER_WORD;
5722 gcc_assert (extra >= 0);
5724 if (GET_CODE (addr) == LO_SUM)
5725 /* For lo_sum addresses, we must allow any offset except one that
5726 causes a wrap, so test only the low 16 bits. */
5727 offset = ((offset & 0xffff) ^ 0x8000) - 0x8000;
5729 return offset + 0x8000 < 0x10000u - extra;
5732 /* Subroutines of rs6000_legitimize_address and rs6000_legitimate_address_p. */
5734 static bool
5735 reg_offset_addressing_ok_p (enum machine_mode mode)
5737 switch (mode)
5739 case V16QImode:
5740 case V8HImode:
5741 case V4SFmode:
5742 case V4SImode:
5743 case V2DFmode:
5744 case V2DImode:
5745 case TImode:
5746 /* AltiVec/VSX vector modes. Only reg+reg addressing is valid. While
5747 TImode is not a vector mode, if we want to use the VSX registers to
5748 move it around, we need to restrict ourselves to reg+reg
5749 addressing. */
5750 if (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode))
5751 return false;
5752 break;
5754 case V4HImode:
5755 case V2SImode:
5756 case V1DImode:
5757 case V2SFmode:
5758 /* Paired vector modes. Only reg+reg addressing is valid. */
5759 if (TARGET_PAIRED_FLOAT)
5760 return false;
5761 break;
5763 case SDmode:
5764 /* If we can do direct load/stores of SDmode, restrict it to reg+reg
5765 addressing for the LFIWZX and STFIWX instructions. */
5766 if (TARGET_NO_SDMODE_STACK)
5767 return false;
5768 break;
5770 default:
5771 break;
5774 return true;
5777 static bool
5778 virtual_stack_registers_memory_p (rtx op)
5780 int regnum;
5782 if (GET_CODE (op) == REG)
5783 regnum = REGNO (op);
5785 else if (GET_CODE (op) == PLUS
5786 && GET_CODE (XEXP (op, 0)) == REG
5787 && GET_CODE (XEXP (op, 1)) == CONST_INT)
5788 regnum = REGNO (XEXP (op, 0));
5790 else
5791 return false;
5793 return (regnum >= FIRST_VIRTUAL_REGISTER
5794 && regnum <= LAST_VIRTUAL_POINTER_REGISTER);
5797 /* Return true if a MODE sized memory accesses to OP plus OFFSET
5798 is known to not straddle a 32k boundary. */
5800 static bool
5801 offsettable_ok_by_alignment (rtx op, HOST_WIDE_INT offset,
5802 enum machine_mode mode)
5804 tree decl, type;
5805 unsigned HOST_WIDE_INT dsize, dalign, lsb, mask;
5807 if (GET_CODE (op) != SYMBOL_REF)
5808 return false;
5810 dsize = GET_MODE_SIZE (mode);
5811 decl = SYMBOL_REF_DECL (op);
5812 if (!decl)
5814 if (dsize == 0)
5815 return false;
5817 /* -fsection-anchors loses the original SYMBOL_REF_DECL when
5818 replacing memory addresses with an anchor plus offset. We
5819 could find the decl by rummaging around in the block->objects
5820 VEC for the given offset but that seems like too much work. */
5821 dalign = BITS_PER_UNIT;
5822 if (SYMBOL_REF_HAS_BLOCK_INFO_P (op)
5823 && SYMBOL_REF_ANCHOR_P (op)
5824 && SYMBOL_REF_BLOCK (op) != NULL)
5826 struct object_block *block = SYMBOL_REF_BLOCK (op);
5828 dalign = block->alignment;
5829 offset += SYMBOL_REF_BLOCK_OFFSET (op);
5831 else if (CONSTANT_POOL_ADDRESS_P (op))
5833 /* It would be nice to have get_pool_align().. */
5834 enum machine_mode cmode = get_pool_mode (op);
5836 dalign = GET_MODE_ALIGNMENT (cmode);
5839 else if (DECL_P (decl))
5841 dalign = DECL_ALIGN (decl);
5843 if (dsize == 0)
5845 /* Allow BLKmode when the entire object is known to not
5846 cross a 32k boundary. */
5847 if (!DECL_SIZE_UNIT (decl))
5848 return false;
5850 if (!host_integerp (DECL_SIZE_UNIT (decl), 1))
5851 return false;
5853 dsize = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
5854 if (dsize > 32768)
5855 return false;
5857 return dalign / BITS_PER_UNIT >= dsize;
5860 else
5862 type = TREE_TYPE (decl);
5864 dalign = TYPE_ALIGN (type);
5865 if (CONSTANT_CLASS_P (decl))
5866 dalign = CONSTANT_ALIGNMENT (decl, dalign);
5867 else
5868 dalign = DATA_ALIGNMENT (decl, dalign);
5870 if (dsize == 0)
5872 /* BLKmode, check the entire object. */
5873 if (TREE_CODE (decl) == STRING_CST)
5874 dsize = TREE_STRING_LENGTH (decl);
5875 else if (TYPE_SIZE_UNIT (type)
5876 && host_integerp (TYPE_SIZE_UNIT (type), 1))
5877 dsize = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
5878 else
5879 return false;
5880 if (dsize > 32768)
5881 return false;
5883 return dalign / BITS_PER_UNIT >= dsize;
5887 /* Find how many bits of the alignment we know for this access. */
5888 mask = dalign / BITS_PER_UNIT - 1;
5889 lsb = offset & -offset;
5890 mask &= lsb - 1;
5891 dalign = mask + 1;
5893 return dalign >= dsize;
5896 static bool
5897 constant_pool_expr_p (rtx op)
5899 rtx base, offset;
5901 split_const (op, &base, &offset);
5902 return (GET_CODE (base) == SYMBOL_REF
5903 && CONSTANT_POOL_ADDRESS_P (base)
5904 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (base), Pmode));
5907 static const_rtx tocrel_base, tocrel_offset;
5909 /* Return true if OP is a toc pointer relative address (the output
5910 of create_TOC_reference). If STRICT, do not match high part or
5911 non-split -mcmodel=large/medium toc pointer relative addresses. */
5913 bool
5914 toc_relative_expr_p (const_rtx op, bool strict)
5916 if (!TARGET_TOC)
5917 return false;
5919 if (TARGET_CMODEL != CMODEL_SMALL)
5921 /* Only match the low part. */
5922 if (GET_CODE (op) == LO_SUM
5923 && REG_P (XEXP (op, 0))
5924 && INT_REG_OK_FOR_BASE_P (XEXP (op, 0), strict))
5925 op = XEXP (op, 1);
5926 else if (strict)
5927 return false;
5930 tocrel_base = op;
5931 tocrel_offset = const0_rtx;
5932 if (GET_CODE (op) == PLUS && CONST_INT_P (XEXP (op, 1)))
5934 tocrel_base = XEXP (op, 0);
5935 tocrel_offset = XEXP (op, 1);
5938 return (GET_CODE (tocrel_base) == UNSPEC
5939 && XINT (tocrel_base, 1) == UNSPEC_TOCREL);
5942 /* Return true if X is a constant pool address, and also for cmodel=medium
5943 if X is a toc-relative address known to be offsettable within MODE. */
5945 bool
5946 legitimate_constant_pool_address_p (const_rtx x, enum machine_mode mode,
5947 bool strict)
5949 return (toc_relative_expr_p (x, strict)
5950 && (TARGET_CMODEL != CMODEL_MEDIUM
5951 || constant_pool_expr_p (XVECEXP (tocrel_base, 0, 0))
5952 || mode == QImode
5953 || offsettable_ok_by_alignment (XVECEXP (tocrel_base, 0, 0),
5954 INTVAL (tocrel_offset), mode)));
5957 static bool
5958 legitimate_small_data_p (enum machine_mode mode, rtx x)
5960 return (DEFAULT_ABI == ABI_V4
5961 && !flag_pic && !TARGET_TOC
5962 && (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST)
5963 && small_data_operand (x, mode));
5966 /* SPE offset addressing is limited to 5-bits worth of double words. */
5967 #define SPE_CONST_OFFSET_OK(x) (((x) & ~0xf8) == 0)
5969 bool
5970 rs6000_legitimate_offset_address_p (enum machine_mode mode, rtx x,
5971 bool strict, bool worst_case)
5973 unsigned HOST_WIDE_INT offset;
5974 unsigned int extra;
5976 if (GET_CODE (x) != PLUS)
5977 return false;
5978 if (!REG_P (XEXP (x, 0)))
5979 return false;
5980 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
5981 return false;
5982 if (!reg_offset_addressing_ok_p (mode))
5983 return virtual_stack_registers_memory_p (x);
5984 if (legitimate_constant_pool_address_p (x, mode, strict))
5985 return true;
5986 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
5987 return false;
5989 offset = INTVAL (XEXP (x, 1));
5990 extra = 0;
5991 switch (mode)
5993 case V4HImode:
5994 case V2SImode:
5995 case V1DImode:
5996 case V2SFmode:
5997 /* SPE vector modes. */
5998 return SPE_CONST_OFFSET_OK (offset);
6000 case DFmode:
6001 case DDmode:
6002 case DImode:
6003 /* On e500v2, we may have:
6005 (subreg:DF (mem:DI (plus (reg) (const_int))) 0).
6007 Which gets addressed with evldd instructions. */
6008 if (TARGET_E500_DOUBLE)
6009 return SPE_CONST_OFFSET_OK (offset);
6011 /* If we are using VSX scalar loads, restrict ourselves to reg+reg
6012 addressing. */
6013 if (VECTOR_MEM_VSX_P (mode))
6014 return false;
6016 if (!worst_case)
6017 break;
6018 if (!TARGET_POWERPC64)
6019 extra = 4;
6020 else if (offset & 3)
6021 return false;
6022 break;
6024 case TFmode:
6025 case TDmode:
6026 case TImode:
6027 case PTImode:
6028 if (TARGET_E500_DOUBLE)
6029 return (SPE_CONST_OFFSET_OK (offset)
6030 && SPE_CONST_OFFSET_OK (offset + 8));
6032 extra = 8;
6033 if (!worst_case)
6034 break;
6035 if (!TARGET_POWERPC64)
6036 extra = 12;
6037 else if (offset & 3)
6038 return false;
6039 break;
6041 default:
6042 break;
6045 offset += 0x8000;
6046 return offset < 0x10000 - extra;
6049 bool
6050 legitimate_indexed_address_p (rtx x, int strict)
6052 rtx op0, op1;
6054 if (GET_CODE (x) != PLUS)
6055 return false;
6057 op0 = XEXP (x, 0);
6058 op1 = XEXP (x, 1);
6060 /* Recognize the rtl generated by reload which we know will later be
6061 replaced with proper base and index regs. */
6062 if (!strict
6063 && reload_in_progress
6064 && (REG_P (op0) || GET_CODE (op0) == PLUS)
6065 && REG_P (op1))
6066 return true;
6068 return (REG_P (op0) && REG_P (op1)
6069 && ((INT_REG_OK_FOR_BASE_P (op0, strict)
6070 && INT_REG_OK_FOR_INDEX_P (op1, strict))
6071 || (INT_REG_OK_FOR_BASE_P (op1, strict)
6072 && INT_REG_OK_FOR_INDEX_P (op0, strict))));
6075 bool
6076 avoiding_indexed_address_p (enum machine_mode mode)
6078 /* Avoid indexed addressing for modes that have non-indexed
6079 load/store instruction forms. */
6080 return (TARGET_AVOID_XFORM && VECTOR_MEM_NONE_P (mode));
6083 bool
6084 legitimate_indirect_address_p (rtx x, int strict)
6086 return GET_CODE (x) == REG && INT_REG_OK_FOR_BASE_P (x, strict);
6089 bool
6090 macho_lo_sum_memory_operand (rtx x, enum machine_mode mode)
6092 if (!TARGET_MACHO || !flag_pic
6093 || mode != SImode || GET_CODE (x) != MEM)
6094 return false;
6095 x = XEXP (x, 0);
6097 if (GET_CODE (x) != LO_SUM)
6098 return false;
6099 if (GET_CODE (XEXP (x, 0)) != REG)
6100 return false;
6101 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), 0))
6102 return false;
6103 x = XEXP (x, 1);
6105 return CONSTANT_P (x);
6108 static bool
6109 legitimate_lo_sum_address_p (enum machine_mode mode, rtx x, int strict)
6111 if (GET_CODE (x) != LO_SUM)
6112 return false;
6113 if (GET_CODE (XEXP (x, 0)) != REG)
6114 return false;
6115 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
6116 return false;
6117 /* Restrict addressing for DI because of our SUBREG hackery. */
6118 if (TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
6119 return false;
6120 x = XEXP (x, 1);
6122 if (TARGET_ELF || TARGET_MACHO)
6124 if (DEFAULT_ABI != ABI_AIX && DEFAULT_ABI != ABI_DARWIN && flag_pic)
6125 return false;
6126 if (TARGET_TOC)
6127 return false;
6128 if (GET_MODE_NUNITS (mode) != 1)
6129 return false;
6130 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
6131 && !(/* ??? Assume floating point reg based on mode? */
6132 TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT
6133 && (mode == DFmode || mode == DDmode)))
6134 return false;
6136 return CONSTANT_P (x);
6139 return false;
6143 /* Try machine-dependent ways of modifying an illegitimate address
6144 to be legitimate. If we find one, return the new, valid address.
6145 This is used from only one place: `memory_address' in explow.c.
6147 OLDX is the address as it was before break_out_memory_refs was
6148 called. In some cases it is useful to look at this to decide what
6149 needs to be done.
6151 It is always safe for this function to do nothing. It exists to
6152 recognize opportunities to optimize the output.
6154 On RS/6000, first check for the sum of a register with a constant
6155 integer that is out of range. If so, generate code to add the
6156 constant with the low-order 16 bits masked to the register and force
6157 this result into another register (this can be done with `cau').
6158 Then generate an address of REG+(CONST&0xffff), allowing for the
6159 possibility of bit 16 being a one.
6161 Then check for the sum of a register and something not constant, try to
6162 load the other things into a register and return the sum. */
6164 static rtx
6165 rs6000_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
6166 enum machine_mode mode)
6168 unsigned int extra;
6170 if (!reg_offset_addressing_ok_p (mode))
6172 if (virtual_stack_registers_memory_p (x))
6173 return x;
6175 /* In theory we should not be seeing addresses of the form reg+0,
6176 but just in case it is generated, optimize it away. */
6177 if (GET_CODE (x) == PLUS && XEXP (x, 1) == const0_rtx)
6178 return force_reg (Pmode, XEXP (x, 0));
6180 /* For TImode with load/store quad, restrict addresses to just a single
6181 pointer, so it works with both GPRs and VSX registers. */
6182 /* Make sure both operands are registers. */
6183 else if (GET_CODE (x) == PLUS
6184 && (mode != TImode || !TARGET_QUAD_MEMORY))
6185 return gen_rtx_PLUS (Pmode,
6186 force_reg (Pmode, XEXP (x, 0)),
6187 force_reg (Pmode, XEXP (x, 1)));
6188 else
6189 return force_reg (Pmode, x);
6191 if (GET_CODE (x) == SYMBOL_REF)
6193 enum tls_model model = SYMBOL_REF_TLS_MODEL (x);
6194 if (model != 0)
6195 return rs6000_legitimize_tls_address (x, model);
6198 extra = 0;
6199 switch (mode)
6201 case TFmode:
6202 case TDmode:
6203 case TImode:
6204 case PTImode:
6205 /* As in legitimate_offset_address_p we do not assume
6206 worst-case. The mode here is just a hint as to the registers
6207 used. A TImode is usually in gprs, but may actually be in
6208 fprs. Leave worst-case scenario for reload to handle via
6209 insn constraints. PTImode is only GPRs. */
6210 extra = 8;
6211 break;
6212 default:
6213 break;
6216 if (GET_CODE (x) == PLUS
6217 && GET_CODE (XEXP (x, 0)) == REG
6218 && GET_CODE (XEXP (x, 1)) == CONST_INT
6219 && ((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 1)) + 0x8000)
6220 >= 0x10000 - extra)
6221 && !(SPE_VECTOR_MODE (mode)
6222 || (TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD)))
6224 HOST_WIDE_INT high_int, low_int;
6225 rtx sum;
6226 low_int = ((INTVAL (XEXP (x, 1)) & 0xffff) ^ 0x8000) - 0x8000;
6227 if (low_int >= 0x8000 - extra)
6228 low_int = 0;
6229 high_int = INTVAL (XEXP (x, 1)) - low_int;
6230 sum = force_operand (gen_rtx_PLUS (Pmode, XEXP (x, 0),
6231 GEN_INT (high_int)), 0);
6232 return plus_constant (Pmode, sum, low_int);
6234 else if (GET_CODE (x) == PLUS
6235 && GET_CODE (XEXP (x, 0)) == REG
6236 && GET_CODE (XEXP (x, 1)) != CONST_INT
6237 && GET_MODE_NUNITS (mode) == 1
6238 && (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
6239 || (/* ??? Assume floating point reg based on mode? */
6240 (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
6241 && (mode == DFmode || mode == DDmode)))
6242 && !avoiding_indexed_address_p (mode))
6244 return gen_rtx_PLUS (Pmode, XEXP (x, 0),
6245 force_reg (Pmode, force_operand (XEXP (x, 1), 0)));
6247 else if (SPE_VECTOR_MODE (mode)
6248 || (TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD))
6250 if (mode == DImode)
6251 return x;
6252 /* We accept [reg + reg] and [reg + OFFSET]. */
6254 if (GET_CODE (x) == PLUS)
6256 rtx op1 = XEXP (x, 0);
6257 rtx op2 = XEXP (x, 1);
6258 rtx y;
6260 op1 = force_reg (Pmode, op1);
6262 if (GET_CODE (op2) != REG
6263 && (GET_CODE (op2) != CONST_INT
6264 || !SPE_CONST_OFFSET_OK (INTVAL (op2))
6265 || (GET_MODE_SIZE (mode) > 8
6266 && !SPE_CONST_OFFSET_OK (INTVAL (op2) + 8))))
6267 op2 = force_reg (Pmode, op2);
6269 /* We can't always do [reg + reg] for these, because [reg +
6270 reg + offset] is not a legitimate addressing mode. */
6271 y = gen_rtx_PLUS (Pmode, op1, op2);
6273 if ((GET_MODE_SIZE (mode) > 8 || mode == DDmode) && REG_P (op2))
6274 return force_reg (Pmode, y);
6275 else
6276 return y;
6279 return force_reg (Pmode, x);
6281 else if ((TARGET_ELF
6282 #if TARGET_MACHO
6283 || !MACHO_DYNAMIC_NO_PIC_P
6284 #endif
6286 && TARGET_32BIT
6287 && TARGET_NO_TOC
6288 && ! flag_pic
6289 && GET_CODE (x) != CONST_INT
6290 && GET_CODE (x) != CONST_DOUBLE
6291 && CONSTANT_P (x)
6292 && GET_MODE_NUNITS (mode) == 1
6293 && (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
6294 || (/* ??? Assume floating point reg based on mode? */
6295 (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
6296 && (mode == DFmode || mode == DDmode))))
6298 rtx reg = gen_reg_rtx (Pmode);
6299 if (TARGET_ELF)
6300 emit_insn (gen_elf_high (reg, x));
6301 else
6302 emit_insn (gen_macho_high (reg, x));
6303 return gen_rtx_LO_SUM (Pmode, reg, x);
6305 else if (TARGET_TOC
6306 && GET_CODE (x) == SYMBOL_REF
6307 && constant_pool_expr_p (x)
6308 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (x), Pmode))
6309 return create_TOC_reference (x, NULL_RTX);
6310 else
6311 return x;
6314 /* Debug version of rs6000_legitimize_address. */
6315 static rtx
6316 rs6000_debug_legitimize_address (rtx x, rtx oldx, enum machine_mode mode)
6318 rtx ret;
6319 rtx insns;
6321 start_sequence ();
6322 ret = rs6000_legitimize_address (x, oldx, mode);
6323 insns = get_insns ();
6324 end_sequence ();
6326 if (ret != x)
6328 fprintf (stderr,
6329 "\nrs6000_legitimize_address: mode %s, old code %s, "
6330 "new code %s, modified\n",
6331 GET_MODE_NAME (mode), GET_RTX_NAME (GET_CODE (x)),
6332 GET_RTX_NAME (GET_CODE (ret)));
6334 fprintf (stderr, "Original address:\n");
6335 debug_rtx (x);
6337 fprintf (stderr, "oldx:\n");
6338 debug_rtx (oldx);
6340 fprintf (stderr, "New address:\n");
6341 debug_rtx (ret);
6343 if (insns)
6345 fprintf (stderr, "Insns added:\n");
6346 debug_rtx_list (insns, 20);
6349 else
6351 fprintf (stderr,
6352 "\nrs6000_legitimize_address: mode %s, code %s, no change:\n",
6353 GET_MODE_NAME (mode), GET_RTX_NAME (GET_CODE (x)));
6355 debug_rtx (x);
6358 if (insns)
6359 emit_insn (insns);
6361 return ret;
6364 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
6365 We need to emit DTP-relative relocations. */
6367 static void rs6000_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
6368 static void
6369 rs6000_output_dwarf_dtprel (FILE *file, int size, rtx x)
6371 switch (size)
6373 case 4:
6374 fputs ("\t.long\t", file);
6375 break;
6376 case 8:
6377 fputs (DOUBLE_INT_ASM_OP, file);
6378 break;
6379 default:
6380 gcc_unreachable ();
6382 output_addr_const (file, x);
6383 fputs ("@dtprel+0x8000", file);
6386 /* In the name of slightly smaller debug output, and to cater to
6387 general assembler lossage, recognize various UNSPEC sequences
6388 and turn them back into a direct symbol reference. */
6390 static rtx
6391 rs6000_delegitimize_address (rtx orig_x)
6393 rtx x, y, offset;
6395 orig_x = delegitimize_mem_from_attrs (orig_x);
6396 x = orig_x;
6397 if (MEM_P (x))
6398 x = XEXP (x, 0);
6400 y = x;
6401 if (TARGET_CMODEL != CMODEL_SMALL
6402 && GET_CODE (y) == LO_SUM)
6403 y = XEXP (y, 1);
6405 offset = NULL_RTX;
6406 if (GET_CODE (y) == PLUS
6407 && GET_MODE (y) == Pmode
6408 && CONST_INT_P (XEXP (y, 1)))
6410 offset = XEXP (y, 1);
6411 y = XEXP (y, 0);
6414 if (GET_CODE (y) == UNSPEC
6415 && XINT (y, 1) == UNSPEC_TOCREL)
6417 #ifdef ENABLE_CHECKING
6418 if (REG_P (XVECEXP (y, 0, 1))
6419 && REGNO (XVECEXP (y, 0, 1)) == TOC_REGISTER)
6421 /* All good. */
6423 else if (GET_CODE (XVECEXP (y, 0, 1)) == DEBUG_EXPR)
6425 /* Weirdness alert. df_note_compute can replace r2 with a
6426 debug_expr when this unspec is in a debug_insn.
6427 Seen in gcc.dg/pr51957-1.c */
6429 else
6431 debug_rtx (orig_x);
6432 abort ();
6434 #endif
6435 y = XVECEXP (y, 0, 0);
6437 #ifdef HAVE_AS_TLS
6438 /* Do not associate thread-local symbols with the original
6439 constant pool symbol. */
6440 if (TARGET_XCOFF
6441 && GET_CODE (y) == SYMBOL_REF
6442 && CONSTANT_POOL_ADDRESS_P (y)
6443 && SYMBOL_REF_TLS_MODEL (get_pool_constant (y)) >= TLS_MODEL_REAL)
6444 return orig_x;
6445 #endif
6447 if (offset != NULL_RTX)
6448 y = gen_rtx_PLUS (Pmode, y, offset);
6449 if (!MEM_P (orig_x))
6450 return y;
6451 else
6452 return replace_equiv_address_nv (orig_x, y);
6455 if (TARGET_MACHO
6456 && GET_CODE (orig_x) == LO_SUM
6457 && GET_CODE (XEXP (orig_x, 1)) == CONST)
6459 y = XEXP (XEXP (orig_x, 1), 0);
6460 if (GET_CODE (y) == UNSPEC
6461 && XINT (y, 1) == UNSPEC_MACHOPIC_OFFSET)
6462 return XVECEXP (y, 0, 0);
6465 return orig_x;
6468 /* Return true if X shouldn't be emitted into the debug info.
6469 The linker doesn't like .toc section references from
6470 .debug_* sections, so reject .toc section symbols. */
6472 static bool
6473 rs6000_const_not_ok_for_debug_p (rtx x)
6475 if (GET_CODE (x) == SYMBOL_REF
6476 && CONSTANT_POOL_ADDRESS_P (x))
6478 rtx c = get_pool_constant (x);
6479 enum machine_mode cmode = get_pool_mode (x);
6480 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (c, cmode))
6481 return true;
6484 return false;
6487 /* Construct the SYMBOL_REF for the tls_get_addr function. */
6489 static GTY(()) rtx rs6000_tls_symbol;
6490 static rtx
6491 rs6000_tls_get_addr (void)
6493 if (!rs6000_tls_symbol)
6494 rs6000_tls_symbol = init_one_libfunc ("__tls_get_addr");
6496 return rs6000_tls_symbol;
6499 /* Construct the SYMBOL_REF for TLS GOT references. */
6501 static GTY(()) rtx rs6000_got_symbol;
6502 static rtx
6503 rs6000_got_sym (void)
6505 if (!rs6000_got_symbol)
6507 rs6000_got_symbol = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
6508 SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_LOCAL;
6509 SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_EXTERNAL;
6512 return rs6000_got_symbol;
6515 /* AIX Thread-Local Address support. */
6517 static rtx
6518 rs6000_legitimize_tls_address_aix (rtx addr, enum tls_model model)
6520 rtx sym, mem, tocref, tlsreg, tmpreg, dest, tlsaddr;
6521 const char *name;
6522 char *tlsname;
6524 name = XSTR (addr, 0);
6525 /* Append TLS CSECT qualifier, unless the symbol already is qualified
6526 or the symbol will be in TLS private data section. */
6527 if (name[strlen (name) - 1] != ']'
6528 && (TREE_PUBLIC (SYMBOL_REF_DECL (addr))
6529 || bss_initializer_p (SYMBOL_REF_DECL (addr))))
6531 tlsname = XALLOCAVEC (char, strlen (name) + 4);
6532 strcpy (tlsname, name);
6533 strcat (tlsname,
6534 bss_initializer_p (SYMBOL_REF_DECL (addr)) ? "[UL]" : "[TL]");
6535 tlsaddr = copy_rtx (addr);
6536 XSTR (tlsaddr, 0) = ggc_strdup (tlsname);
6538 else
6539 tlsaddr = addr;
6541 /* Place addr into TOC constant pool. */
6542 sym = force_const_mem (GET_MODE (tlsaddr), tlsaddr);
6544 /* Output the TOC entry and create the MEM referencing the value. */
6545 if (constant_pool_expr_p (XEXP (sym, 0))
6546 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (XEXP (sym, 0)), Pmode))
6548 tocref = create_TOC_reference (XEXP (sym, 0), NULL_RTX);
6549 mem = gen_const_mem (Pmode, tocref);
6550 set_mem_alias_set (mem, get_TOC_alias_set ());
6552 else
6553 return sym;
6555 /* Use global-dynamic for local-dynamic. */
6556 if (model == TLS_MODEL_GLOBAL_DYNAMIC
6557 || model == TLS_MODEL_LOCAL_DYNAMIC)
6559 /* Create new TOC reference for @m symbol. */
6560 name = XSTR (XVECEXP (XEXP (mem, 0), 0, 0), 0);
6561 tlsname = XALLOCAVEC (char, strlen (name) + 1);
6562 strcpy (tlsname, "*LCM");
6563 strcat (tlsname, name + 3);
6564 rtx modaddr = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (tlsname));
6565 SYMBOL_REF_FLAGS (modaddr) |= SYMBOL_FLAG_LOCAL;
6566 tocref = create_TOC_reference (modaddr, NULL_RTX);
6567 rtx modmem = gen_const_mem (Pmode, tocref);
6568 set_mem_alias_set (modmem, get_TOC_alias_set ());
6570 rtx modreg = gen_reg_rtx (Pmode);
6571 emit_insn (gen_rtx_SET (VOIDmode, modreg, modmem));
6573 tmpreg = gen_reg_rtx (Pmode);
6574 emit_insn (gen_rtx_SET (VOIDmode, tmpreg, mem));
6576 dest = gen_reg_rtx (Pmode);
6577 if (TARGET_32BIT)
6578 emit_insn (gen_tls_get_addrsi (dest, modreg, tmpreg));
6579 else
6580 emit_insn (gen_tls_get_addrdi (dest, modreg, tmpreg));
6581 return dest;
6583 /* Obtain TLS pointer: 32 bit call or 64 bit GPR 13. */
6584 else if (TARGET_32BIT)
6586 tlsreg = gen_reg_rtx (SImode);
6587 emit_insn (gen_tls_get_tpointer (tlsreg));
6589 else
6590 tlsreg = gen_rtx_REG (DImode, 13);
6592 /* Load the TOC value into temporary register. */
6593 tmpreg = gen_reg_rtx (Pmode);
6594 emit_insn (gen_rtx_SET (VOIDmode, tmpreg, mem));
6595 set_unique_reg_note (get_last_insn (), REG_EQUAL,
6596 gen_rtx_MINUS (Pmode, addr, tlsreg));
6598 /* Add TOC symbol value to TLS pointer. */
6599 dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, tmpreg, tlsreg));
6601 return dest;
6604 /* ADDR contains a thread-local SYMBOL_REF. Generate code to compute
6605 this (thread-local) address. */
6607 static rtx
6608 rs6000_legitimize_tls_address (rtx addr, enum tls_model model)
6610 rtx dest, insn;
6612 if (TARGET_XCOFF)
6613 return rs6000_legitimize_tls_address_aix (addr, model);
6615 dest = gen_reg_rtx (Pmode);
6616 if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 16)
6618 rtx tlsreg;
6620 if (TARGET_64BIT)
6622 tlsreg = gen_rtx_REG (Pmode, 13);
6623 insn = gen_tls_tprel_64 (dest, tlsreg, addr);
6625 else
6627 tlsreg = gen_rtx_REG (Pmode, 2);
6628 insn = gen_tls_tprel_32 (dest, tlsreg, addr);
6630 emit_insn (insn);
6632 else if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 32)
6634 rtx tlsreg, tmp;
6636 tmp = gen_reg_rtx (Pmode);
6637 if (TARGET_64BIT)
6639 tlsreg = gen_rtx_REG (Pmode, 13);
6640 insn = gen_tls_tprel_ha_64 (tmp, tlsreg, addr);
6642 else
6644 tlsreg = gen_rtx_REG (Pmode, 2);
6645 insn = gen_tls_tprel_ha_32 (tmp, tlsreg, addr);
6647 emit_insn (insn);
6648 if (TARGET_64BIT)
6649 insn = gen_tls_tprel_lo_64 (dest, tmp, addr);
6650 else
6651 insn = gen_tls_tprel_lo_32 (dest, tmp, addr);
6652 emit_insn (insn);
6654 else
6656 rtx r3, got, tga, tmp1, tmp2, call_insn;
6658 /* We currently use relocations like @got@tlsgd for tls, which
6659 means the linker will handle allocation of tls entries, placing
6660 them in the .got section. So use a pointer to the .got section,
6661 not one to secondary TOC sections used by 64-bit -mminimal-toc,
6662 or to secondary GOT sections used by 32-bit -fPIC. */
6663 if (TARGET_64BIT)
6664 got = gen_rtx_REG (Pmode, 2);
6665 else
6667 if (flag_pic == 1)
6668 got = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
6669 else
6671 rtx gsym = rs6000_got_sym ();
6672 got = gen_reg_rtx (Pmode);
6673 if (flag_pic == 0)
6674 rs6000_emit_move (got, gsym, Pmode);
6675 else
6677 rtx mem, lab, last;
6679 tmp1 = gen_reg_rtx (Pmode);
6680 tmp2 = gen_reg_rtx (Pmode);
6681 mem = gen_const_mem (Pmode, tmp1);
6682 lab = gen_label_rtx ();
6683 emit_insn (gen_load_toc_v4_PIC_1b (gsym, lab));
6684 emit_move_insn (tmp1, gen_rtx_REG (Pmode, LR_REGNO));
6685 if (TARGET_LINK_STACK)
6686 emit_insn (gen_addsi3 (tmp1, tmp1, GEN_INT (4)));
6687 emit_move_insn (tmp2, mem);
6688 last = emit_insn (gen_addsi3 (got, tmp1, tmp2));
6689 set_unique_reg_note (last, REG_EQUAL, gsym);
6694 if (model == TLS_MODEL_GLOBAL_DYNAMIC)
6696 tga = rs6000_tls_get_addr ();
6697 emit_library_call_value (tga, dest, LCT_CONST, Pmode,
6698 1, const0_rtx, Pmode);
6700 r3 = gen_rtx_REG (Pmode, 3);
6701 if (DEFAULT_ABI == ABI_AIX && TARGET_64BIT)
6702 insn = gen_tls_gd_aix64 (r3, got, addr, tga, const0_rtx);
6703 else if (DEFAULT_ABI == ABI_AIX && !TARGET_64BIT)
6704 insn = gen_tls_gd_aix32 (r3, got, addr, tga, const0_rtx);
6705 else if (DEFAULT_ABI == ABI_V4)
6706 insn = gen_tls_gd_sysvsi (r3, got, addr, tga, const0_rtx);
6707 else
6708 gcc_unreachable ();
6709 call_insn = last_call_insn ();
6710 PATTERN (call_insn) = insn;
6711 if (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
6712 use_reg (&CALL_INSN_FUNCTION_USAGE (call_insn),
6713 pic_offset_table_rtx);
6715 else if (model == TLS_MODEL_LOCAL_DYNAMIC)
6717 tga = rs6000_tls_get_addr ();
6718 tmp1 = gen_reg_rtx (Pmode);
6719 emit_library_call_value (tga, tmp1, LCT_CONST, Pmode,
6720 1, const0_rtx, Pmode);
6722 r3 = gen_rtx_REG (Pmode, 3);
6723 if (DEFAULT_ABI == ABI_AIX && TARGET_64BIT)
6724 insn = gen_tls_ld_aix64 (r3, got, tga, const0_rtx);
6725 else if (DEFAULT_ABI == ABI_AIX && !TARGET_64BIT)
6726 insn = gen_tls_ld_aix32 (r3, got, tga, const0_rtx);
6727 else if (DEFAULT_ABI == ABI_V4)
6728 insn = gen_tls_ld_sysvsi (r3, got, tga, const0_rtx);
6729 else
6730 gcc_unreachable ();
6731 call_insn = last_call_insn ();
6732 PATTERN (call_insn) = insn;
6733 if (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
6734 use_reg (&CALL_INSN_FUNCTION_USAGE (call_insn),
6735 pic_offset_table_rtx);
6737 if (rs6000_tls_size == 16)
6739 if (TARGET_64BIT)
6740 insn = gen_tls_dtprel_64 (dest, tmp1, addr);
6741 else
6742 insn = gen_tls_dtprel_32 (dest, tmp1, addr);
6744 else if (rs6000_tls_size == 32)
6746 tmp2 = gen_reg_rtx (Pmode);
6747 if (TARGET_64BIT)
6748 insn = gen_tls_dtprel_ha_64 (tmp2, tmp1, addr);
6749 else
6750 insn = gen_tls_dtprel_ha_32 (tmp2, tmp1, addr);
6751 emit_insn (insn);
6752 if (TARGET_64BIT)
6753 insn = gen_tls_dtprel_lo_64 (dest, tmp2, addr);
6754 else
6755 insn = gen_tls_dtprel_lo_32 (dest, tmp2, addr);
6757 else
6759 tmp2 = gen_reg_rtx (Pmode);
6760 if (TARGET_64BIT)
6761 insn = gen_tls_got_dtprel_64 (tmp2, got, addr);
6762 else
6763 insn = gen_tls_got_dtprel_32 (tmp2, got, addr);
6764 emit_insn (insn);
6765 insn = gen_rtx_SET (Pmode, dest,
6766 gen_rtx_PLUS (Pmode, tmp2, tmp1));
6768 emit_insn (insn);
6770 else
6772 /* IE, or 64-bit offset LE. */
6773 tmp2 = gen_reg_rtx (Pmode);
6774 if (TARGET_64BIT)
6775 insn = gen_tls_got_tprel_64 (tmp2, got, addr);
6776 else
6777 insn = gen_tls_got_tprel_32 (tmp2, got, addr);
6778 emit_insn (insn);
6779 if (TARGET_64BIT)
6780 insn = gen_tls_tls_64 (dest, tmp2, addr);
6781 else
6782 insn = gen_tls_tls_32 (dest, tmp2, addr);
6783 emit_insn (insn);
6787 return dest;
6790 /* Return 1 if X contains a thread-local symbol. */
6792 static bool
6793 rs6000_tls_referenced_p (rtx x)
6795 if (! TARGET_HAVE_TLS)
6796 return false;
6798 return for_each_rtx (&x, &rs6000_tls_symbol_ref_1, 0);
6801 /* Implement TARGET_CANNOT_FORCE_CONST_MEM. */
6803 static bool
6804 rs6000_cannot_force_const_mem (enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
6806 if (GET_CODE (x) == HIGH
6807 && GET_CODE (XEXP (x, 0)) == UNSPEC)
6808 return true;
6810 /* A TLS symbol in the TOC cannot contain a sum. */
6811 if (GET_CODE (x) == CONST
6812 && GET_CODE (XEXP (x, 0)) == PLUS
6813 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
6814 && SYMBOL_REF_TLS_MODEL (XEXP (XEXP (x, 0), 0)) != 0)
6815 return true;
6817 /* Do not place an ELF TLS symbol in the constant pool. */
6818 return TARGET_ELF && rs6000_tls_referenced_p (x);
6821 /* Return 1 if *X is a thread-local symbol. This is the same as
6822 rs6000_tls_symbol_ref except for the type of the unused argument. */
6824 static int
6825 rs6000_tls_symbol_ref_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
6827 return RS6000_SYMBOL_REF_TLS_P (*x);
6830 /* Return true iff the given SYMBOL_REF refers to a constant pool entry
6831 that we have put in the TOC, or for cmodel=medium, if the SYMBOL_REF
6832 can be addressed relative to the toc pointer. */
6834 static bool
6835 use_toc_relative_ref (rtx sym)
6837 return ((constant_pool_expr_p (sym)
6838 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (sym),
6839 get_pool_mode (sym)))
6840 || (TARGET_CMODEL == CMODEL_MEDIUM
6841 && SYMBOL_REF_LOCAL_P (sym)));
6844 /* Our implementation of LEGITIMIZE_RELOAD_ADDRESS. Returns a value to
6845 replace the input X, or the original X if no replacement is called for.
6846 The output parameter *WIN is 1 if the calling macro should goto WIN,
6847 0 if it should not.
6849 For RS/6000, we wish to handle large displacements off a base
6850 register by splitting the addend across an addiu/addis and the mem insn.
6851 This cuts number of extra insns needed from 3 to 1.
6853 On Darwin, we use this to generate code for floating point constants.
6854 A movsf_low is generated so we wind up with 2 instructions rather than 3.
6855 The Darwin code is inside #if TARGET_MACHO because only then are the
6856 machopic_* functions defined. */
6857 static rtx
6858 rs6000_legitimize_reload_address (rtx x, enum machine_mode mode,
6859 int opnum, int type,
6860 int ind_levels ATTRIBUTE_UNUSED, int *win)
6862 bool reg_offset_p = reg_offset_addressing_ok_p (mode);
6864 /* Nasty hack for vsx_splat_V2DF/V2DI load from mem, which takes a
6865 DFmode/DImode MEM. */
6866 if (reg_offset_p
6867 && opnum == 1
6868 && ((mode == DFmode && recog_data.operand_mode[0] == V2DFmode)
6869 || (mode == DImode && recog_data.operand_mode[0] == V2DImode)))
6870 reg_offset_p = false;
6872 /* We must recognize output that we have already generated ourselves. */
6873 if (GET_CODE (x) == PLUS
6874 && GET_CODE (XEXP (x, 0)) == PLUS
6875 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
6876 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6877 && GET_CODE (XEXP (x, 1)) == CONST_INT)
6879 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6880 BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
6881 opnum, (enum reload_type) type);
6882 *win = 1;
6883 return x;
6886 /* Likewise for (lo_sum (high ...) ...) output we have generated. */
6887 if (GET_CODE (x) == LO_SUM
6888 && GET_CODE (XEXP (x, 0)) == HIGH)
6890 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6891 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
6892 opnum, (enum reload_type) type);
6893 *win = 1;
6894 return x;
6897 #if TARGET_MACHO
6898 if (DEFAULT_ABI == ABI_DARWIN && flag_pic
6899 && GET_CODE (x) == LO_SUM
6900 && GET_CODE (XEXP (x, 0)) == PLUS
6901 && XEXP (XEXP (x, 0), 0) == pic_offset_table_rtx
6902 && GET_CODE (XEXP (XEXP (x, 0), 1)) == HIGH
6903 && XEXP (XEXP (XEXP (x, 0), 1), 0) == XEXP (x, 1)
6904 && machopic_operand_p (XEXP (x, 1)))
6906 /* Result of previous invocation of this function on Darwin
6907 floating point constant. */
6908 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6909 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
6910 opnum, (enum reload_type) type);
6911 *win = 1;
6912 return x;
6914 #endif
6916 if (TARGET_CMODEL != CMODEL_SMALL
6917 && reg_offset_p
6918 && small_toc_ref (x, VOIDmode))
6920 rtx hi = gen_rtx_HIGH (Pmode, copy_rtx (x));
6921 x = gen_rtx_LO_SUM (Pmode, hi, x);
6922 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6923 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
6924 opnum, (enum reload_type) type);
6925 *win = 1;
6926 return x;
6929 if (GET_CODE (x) == PLUS
6930 && GET_CODE (XEXP (x, 0)) == REG
6931 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
6932 && INT_REG_OK_FOR_BASE_P (XEXP (x, 0), 1)
6933 && GET_CODE (XEXP (x, 1)) == CONST_INT
6934 && reg_offset_p
6935 && !SPE_VECTOR_MODE (mode)
6936 && !(TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode
6937 || mode == DDmode || mode == TDmode
6938 || mode == DImode))
6939 && (!VECTOR_MODE_P (mode) || VECTOR_MEM_NONE_P (mode)))
6941 HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
6942 HOST_WIDE_INT low = ((val & 0xffff) ^ 0x8000) - 0x8000;
6943 HOST_WIDE_INT high
6944 = (((val - low) & 0xffffffff) ^ 0x80000000) - 0x80000000;
6946 /* Check for 32-bit overflow. */
6947 if (high + low != val)
6949 *win = 0;
6950 return x;
6953 /* Reload the high part into a base reg; leave the low part
6954 in the mem directly. */
6956 x = gen_rtx_PLUS (GET_MODE (x),
6957 gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0),
6958 GEN_INT (high)),
6959 GEN_INT (low));
6961 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6962 BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
6963 opnum, (enum reload_type) type);
6964 *win = 1;
6965 return x;
6968 if (GET_CODE (x) == SYMBOL_REF
6969 && reg_offset_p
6970 && (!VECTOR_MODE_P (mode) || VECTOR_MEM_NONE_P (mode))
6971 && !SPE_VECTOR_MODE (mode)
6972 #if TARGET_MACHO
6973 && DEFAULT_ABI == ABI_DARWIN
6974 && (flag_pic || MACHO_DYNAMIC_NO_PIC_P)
6975 && machopic_symbol_defined_p (x)
6976 #else
6977 && DEFAULT_ABI == ABI_V4
6978 && !flag_pic
6979 #endif
6980 /* Don't do this for TFmode or TDmode, since the result isn't offsettable.
6981 The same goes for DImode without 64-bit gprs and DFmode and DDmode
6982 without fprs.
6983 ??? Assume floating point reg based on mode? This assumption is
6984 violated by eg. powerpc-linux -m32 compile of gcc.dg/pr28796-2.c
6985 where reload ends up doing a DFmode load of a constant from
6986 mem using two gprs. Unfortunately, at this point reload
6987 hasn't yet selected regs so poking around in reload data
6988 won't help and even if we could figure out the regs reliably,
6989 we'd still want to allow this transformation when the mem is
6990 naturally aligned. Since we say the address is good here, we
6991 can't disable offsets from LO_SUMs in mem_operand_gpr.
6992 FIXME: Allow offset from lo_sum for other modes too, when
6993 mem is sufficiently aligned. */
6994 && mode != TFmode
6995 && mode != TDmode
6996 && (mode != TImode || !TARGET_VSX_TIMODE)
6997 && mode != PTImode
6998 && (mode != DImode || TARGET_POWERPC64)
6999 && ((mode != DFmode && mode != DDmode) || TARGET_POWERPC64
7000 || (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)))
7002 #if TARGET_MACHO
7003 if (flag_pic)
7005 rtx offset = machopic_gen_offset (x);
7006 x = gen_rtx_LO_SUM (GET_MODE (x),
7007 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
7008 gen_rtx_HIGH (Pmode, offset)), offset);
7010 else
7011 #endif
7012 x = gen_rtx_LO_SUM (GET_MODE (x),
7013 gen_rtx_HIGH (Pmode, x), x);
7015 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7016 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
7017 opnum, (enum reload_type) type);
7018 *win = 1;
7019 return x;
7022 /* Reload an offset address wrapped by an AND that represents the
7023 masking of the lower bits. Strip the outer AND and let reload
7024 convert the offset address into an indirect address. For VSX,
7025 force reload to create the address with an AND in a separate
7026 register, because we can't guarantee an altivec register will
7027 be used. */
7028 if (VECTOR_MEM_ALTIVEC_P (mode)
7029 && GET_CODE (x) == AND
7030 && GET_CODE (XEXP (x, 0)) == PLUS
7031 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
7032 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7033 && GET_CODE (XEXP (x, 1)) == CONST_INT
7034 && INTVAL (XEXP (x, 1)) == -16)
7036 x = XEXP (x, 0);
7037 *win = 1;
7038 return x;
7041 if (TARGET_TOC
7042 && reg_offset_p
7043 && GET_CODE (x) == SYMBOL_REF
7044 && use_toc_relative_ref (x))
7046 x = create_TOC_reference (x, NULL_RTX);
7047 if (TARGET_CMODEL != CMODEL_SMALL)
7048 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7049 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
7050 opnum, (enum reload_type) type);
7051 *win = 1;
7052 return x;
7054 *win = 0;
7055 return x;
7058 /* Debug version of rs6000_legitimize_reload_address. */
7059 static rtx
7060 rs6000_debug_legitimize_reload_address (rtx x, enum machine_mode mode,
7061 int opnum, int type,
7062 int ind_levels, int *win)
7064 rtx ret = rs6000_legitimize_reload_address (x, mode, opnum, type,
7065 ind_levels, win);
7066 fprintf (stderr,
7067 "\nrs6000_legitimize_reload_address: mode = %s, opnum = %d, "
7068 "type = %d, ind_levels = %d, win = %d, original addr:\n",
7069 GET_MODE_NAME (mode), opnum, type, ind_levels, *win);
7070 debug_rtx (x);
7072 if (x == ret)
7073 fprintf (stderr, "Same address returned\n");
7074 else if (!ret)
7075 fprintf (stderr, "NULL returned\n");
7076 else
7078 fprintf (stderr, "New address:\n");
7079 debug_rtx (ret);
7082 return ret;
7085 /* TARGET_LEGITIMATE_ADDRESS_P recognizes an RTL expression
7086 that is a valid memory address for an instruction.
7087 The MODE argument is the machine mode for the MEM expression
7088 that wants to use this address.
7090 On the RS/6000, there are four valid address: a SYMBOL_REF that
7091 refers to a constant pool entry of an address (or the sum of it
7092 plus a constant), a short (16-bit signed) constant plus a register,
7093 the sum of two registers, or a register indirect, possibly with an
7094 auto-increment. For DFmode, DDmode and DImode with a constant plus
7095 register, we must ensure that both words are addressable or PowerPC64
7096 with offset word aligned.
7098 For modes spanning multiple registers (DFmode and DDmode in 32-bit GPRs,
7099 32-bit DImode, TImode, TFmode, TDmode), indexed addressing cannot be used
7100 because adjacent memory cells are accessed by adding word-sized offsets
7101 during assembly output. */
7102 static bool
7103 rs6000_legitimate_address_p (enum machine_mode mode, rtx x, bool reg_ok_strict)
7105 bool reg_offset_p = reg_offset_addressing_ok_p (mode);
7107 /* If this is an unaligned stvx/ldvx type address, discard the outer AND. */
7108 if (VECTOR_MEM_ALTIVEC_P (mode)
7109 && GET_CODE (x) == AND
7110 && GET_CODE (XEXP (x, 1)) == CONST_INT
7111 && INTVAL (XEXP (x, 1)) == -16)
7112 x = XEXP (x, 0);
7114 if (TARGET_ELF && RS6000_SYMBOL_REF_TLS_P (x))
7115 return 0;
7116 if (legitimate_indirect_address_p (x, reg_ok_strict))
7117 return 1;
7118 if ((GET_CODE (x) == PRE_INC || GET_CODE (x) == PRE_DEC)
7119 && !ALTIVEC_OR_VSX_VECTOR_MODE (mode)
7120 && !SPE_VECTOR_MODE (mode)
7121 && mode != TFmode
7122 && mode != TDmode
7123 && mode != TImode
7124 && mode != PTImode
7125 /* Restrict addressing for DI because of our SUBREG hackery. */
7126 && !(TARGET_E500_DOUBLE
7127 && (mode == DFmode || mode == DDmode || mode == DImode))
7128 && TARGET_UPDATE
7129 && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict))
7130 return 1;
7131 if (virtual_stack_registers_memory_p (x))
7132 return 1;
7133 if (reg_offset_p && legitimate_small_data_p (mode, x))
7134 return 1;
7135 if (reg_offset_p
7136 && legitimate_constant_pool_address_p (x, mode, reg_ok_strict))
7137 return 1;
7138 /* For TImode, if we have load/store quad, only allow register indirect
7139 addresses. This will allow the values to go in either GPRs or VSX
7140 registers without reloading. The vector types would tend to go into VSX
7141 registers, so we allow REG+REG, while TImode seems somewhat split, in that
7142 some uses are GPR based, and some VSX based. */
7143 if (mode == TImode && TARGET_QUAD_MEMORY)
7144 return 0;
7145 /* If not REG_OK_STRICT (before reload) let pass any stack offset. */
7146 if (! reg_ok_strict
7147 && reg_offset_p
7148 && GET_CODE (x) == PLUS
7149 && GET_CODE (XEXP (x, 0)) == REG
7150 && (XEXP (x, 0) == virtual_stack_vars_rtx
7151 || XEXP (x, 0) == arg_pointer_rtx)
7152 && GET_CODE (XEXP (x, 1)) == CONST_INT)
7153 return 1;
7154 if (rs6000_legitimate_offset_address_p (mode, x, reg_ok_strict, false))
7155 return 1;
7156 if (mode != TFmode
7157 && mode != TDmode
7158 && ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
7159 || TARGET_POWERPC64
7160 || (mode != DFmode && mode != DDmode)
7161 || (TARGET_E500_DOUBLE && mode != DDmode))
7162 && (TARGET_POWERPC64 || mode != DImode)
7163 && (mode != TImode || VECTOR_MEM_VSX_P (TImode))
7164 && mode != PTImode
7165 && !avoiding_indexed_address_p (mode)
7166 && legitimate_indexed_address_p (x, reg_ok_strict))
7167 return 1;
7168 if (GET_CODE (x) == PRE_MODIFY
7169 && mode != TImode
7170 && mode != PTImode
7171 && mode != TFmode
7172 && mode != TDmode
7173 && ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
7174 || TARGET_POWERPC64
7175 || ((mode != DFmode && mode != DDmode) || TARGET_E500_DOUBLE))
7176 && (TARGET_POWERPC64 || mode != DImode)
7177 && !ALTIVEC_OR_VSX_VECTOR_MODE (mode)
7178 && !SPE_VECTOR_MODE (mode)
7179 /* Restrict addressing for DI because of our SUBREG hackery. */
7180 && !(TARGET_E500_DOUBLE
7181 && (mode == DFmode || mode == DDmode || mode == DImode))
7182 && TARGET_UPDATE
7183 && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict)
7184 && (rs6000_legitimate_offset_address_p (mode, XEXP (x, 1),
7185 reg_ok_strict, false)
7186 || (!avoiding_indexed_address_p (mode)
7187 && legitimate_indexed_address_p (XEXP (x, 1), reg_ok_strict)))
7188 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
7189 return 1;
7190 if (reg_offset_p && legitimate_lo_sum_address_p (mode, x, reg_ok_strict))
7191 return 1;
7192 return 0;
7195 /* Debug version of rs6000_legitimate_address_p. */
7196 static bool
7197 rs6000_debug_legitimate_address_p (enum machine_mode mode, rtx x,
7198 bool reg_ok_strict)
7200 bool ret = rs6000_legitimate_address_p (mode, x, reg_ok_strict);
7201 fprintf (stderr,
7202 "\nrs6000_legitimate_address_p: return = %s, mode = %s, "
7203 "strict = %d, code = %s\n",
7204 ret ? "true" : "false",
7205 GET_MODE_NAME (mode),
7206 reg_ok_strict,
7207 GET_RTX_NAME (GET_CODE (x)));
7208 debug_rtx (x);
7210 return ret;
7213 /* Implement TARGET_MODE_DEPENDENT_ADDRESS_P. */
7215 static bool
7216 rs6000_mode_dependent_address_p (const_rtx addr,
7217 addr_space_t as ATTRIBUTE_UNUSED)
7219 return rs6000_mode_dependent_address_ptr (addr);
7222 /* Go to LABEL if ADDR (a legitimate address expression)
7223 has an effect that depends on the machine mode it is used for.
7225 On the RS/6000 this is true of all integral offsets (since AltiVec
7226 and VSX modes don't allow them) or is a pre-increment or decrement.
7228 ??? Except that due to conceptual problems in offsettable_address_p
7229 we can't really report the problems of integral offsets. So leave
7230 this assuming that the adjustable offset must be valid for the
7231 sub-words of a TFmode operand, which is what we had before. */
7233 static bool
7234 rs6000_mode_dependent_address (const_rtx addr)
7236 switch (GET_CODE (addr))
7238 case PLUS:
7239 /* Any offset from virtual_stack_vars_rtx and arg_pointer_rtx
7240 is considered a legitimate address before reload, so there
7241 are no offset restrictions in that case. Note that this
7242 condition is safe in strict mode because any address involving
7243 virtual_stack_vars_rtx or arg_pointer_rtx would already have
7244 been rejected as illegitimate. */
7245 if (XEXP (addr, 0) != virtual_stack_vars_rtx
7246 && XEXP (addr, 0) != arg_pointer_rtx
7247 && GET_CODE (XEXP (addr, 1)) == CONST_INT)
7249 unsigned HOST_WIDE_INT val = INTVAL (XEXP (addr, 1));
7250 return val + 0x8000 >= 0x10000 - (TARGET_POWERPC64 ? 8 : 12);
7252 break;
7254 case LO_SUM:
7255 /* Anything in the constant pool is sufficiently aligned that
7256 all bytes have the same high part address. */
7257 return !legitimate_constant_pool_address_p (addr, QImode, false);
7259 /* Auto-increment cases are now treated generically in recog.c. */
7260 case PRE_MODIFY:
7261 return TARGET_UPDATE;
7263 /* AND is only allowed in Altivec loads. */
7264 case AND:
7265 return true;
7267 default:
7268 break;
7271 return false;
7274 /* Debug version of rs6000_mode_dependent_address. */
7275 static bool
7276 rs6000_debug_mode_dependent_address (const_rtx addr)
7278 bool ret = rs6000_mode_dependent_address (addr);
7280 fprintf (stderr, "\nrs6000_mode_dependent_address: ret = %s\n",
7281 ret ? "true" : "false");
7282 debug_rtx (addr);
7284 return ret;
7287 /* Implement FIND_BASE_TERM. */
7290 rs6000_find_base_term (rtx op)
7292 rtx base;
7294 base = op;
7295 if (GET_CODE (base) == CONST)
7296 base = XEXP (base, 0);
7297 if (GET_CODE (base) == PLUS)
7298 base = XEXP (base, 0);
7299 if (GET_CODE (base) == UNSPEC)
7300 switch (XINT (base, 1))
7302 case UNSPEC_TOCREL:
7303 case UNSPEC_MACHOPIC_OFFSET:
7304 /* OP represents SYM [+ OFFSET] - ANCHOR. SYM is the base term
7305 for aliasing purposes. */
7306 return XVECEXP (base, 0, 0);
7309 return op;
7312 /* More elaborate version of recog's offsettable_memref_p predicate
7313 that works around the ??? note of rs6000_mode_dependent_address.
7314 In particular it accepts
7316 (mem:DI (plus:SI (reg/f:SI 31 31) (const_int 32760 [0x7ff8])))
7318 in 32-bit mode, that the recog predicate rejects. */
7320 static bool
7321 rs6000_offsettable_memref_p (rtx op, enum machine_mode reg_mode)
7323 bool worst_case;
7325 if (!MEM_P (op))
7326 return false;
7328 /* First mimic offsettable_memref_p. */
7329 if (offsettable_address_p (true, GET_MODE (op), XEXP (op, 0)))
7330 return true;
7332 /* offsettable_address_p invokes rs6000_mode_dependent_address, but
7333 the latter predicate knows nothing about the mode of the memory
7334 reference and, therefore, assumes that it is the largest supported
7335 mode (TFmode). As a consequence, legitimate offsettable memory
7336 references are rejected. rs6000_legitimate_offset_address_p contains
7337 the correct logic for the PLUS case of rs6000_mode_dependent_address,
7338 at least with a little bit of help here given that we know the
7339 actual registers used. */
7340 worst_case = ((TARGET_POWERPC64 && GET_MODE_CLASS (reg_mode) == MODE_INT)
7341 || GET_MODE_SIZE (reg_mode) == 4);
7342 return rs6000_legitimate_offset_address_p (GET_MODE (op), XEXP (op, 0),
7343 true, worst_case);
7346 /* Change register usage conditional on target flags. */
7347 static void
7348 rs6000_conditional_register_usage (void)
7350 int i;
7352 if (TARGET_DEBUG_TARGET)
7353 fprintf (stderr, "rs6000_conditional_register_usage called\n");
7355 /* Set MQ register fixed (already call_used) so that it will not be
7356 allocated. */
7357 fixed_regs[64] = 1;
7359 /* 64-bit AIX and Linux reserve GPR13 for thread-private data. */
7360 if (TARGET_64BIT)
7361 fixed_regs[13] = call_used_regs[13]
7362 = call_really_used_regs[13] = 1;
7364 /* Conditionally disable FPRs. */
7365 if (TARGET_SOFT_FLOAT || !TARGET_FPRS)
7366 for (i = 32; i < 64; i++)
7367 fixed_regs[i] = call_used_regs[i]
7368 = call_really_used_regs[i] = 1;
7370 /* The TOC register is not killed across calls in a way that is
7371 visible to the compiler. */
7372 if (DEFAULT_ABI == ABI_AIX)
7373 call_really_used_regs[2] = 0;
7375 if (DEFAULT_ABI == ABI_V4
7376 && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
7377 && flag_pic == 2)
7378 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
7380 if (DEFAULT_ABI == ABI_V4
7381 && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
7382 && flag_pic == 1)
7383 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
7384 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
7385 = call_really_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
7387 if (DEFAULT_ABI == ABI_DARWIN
7388 && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM)
7389 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
7390 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
7391 = call_really_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
7393 if (TARGET_TOC && TARGET_MINIMAL_TOC)
7394 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
7395 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
7397 if (TARGET_SPE)
7399 global_regs[SPEFSCR_REGNO] = 1;
7400 /* We used to use r14 as FIXED_SCRATCH to address SPE 64-bit
7401 registers in prologues and epilogues. We no longer use r14
7402 for FIXED_SCRATCH, but we're keeping r14 out of the allocation
7403 pool for link-compatibility with older versions of GCC. Once
7404 "old" code has died out, we can return r14 to the allocation
7405 pool. */
7406 fixed_regs[14]
7407 = call_used_regs[14]
7408 = call_really_used_regs[14] = 1;
7411 if (!TARGET_ALTIVEC && !TARGET_VSX)
7413 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
7414 fixed_regs[i] = call_used_regs[i] = call_really_used_regs[i] = 1;
7415 call_really_used_regs[VRSAVE_REGNO] = 1;
7418 if (TARGET_ALTIVEC || TARGET_VSX)
7419 global_regs[VSCR_REGNO] = 1;
7421 if (TARGET_ALTIVEC_ABI)
7423 for (i = FIRST_ALTIVEC_REGNO; i < FIRST_ALTIVEC_REGNO + 20; ++i)
7424 call_used_regs[i] = call_really_used_regs[i] = 1;
7426 /* AIX reserves VR20:31 in non-extended ABI mode. */
7427 if (TARGET_XCOFF)
7428 for (i = FIRST_ALTIVEC_REGNO + 20; i < FIRST_ALTIVEC_REGNO + 32; ++i)
7429 fixed_regs[i] = call_used_regs[i] = call_really_used_regs[i] = 1;
7433 /* Try to output insns to set TARGET equal to the constant C if it can
7434 be done in less than N insns. Do all computations in MODE.
7435 Returns the place where the output has been placed if it can be
7436 done and the insns have been emitted. If it would take more than N
7437 insns, zero is returned and no insns and emitted. */
7440 rs6000_emit_set_const (rtx dest, enum machine_mode mode,
7441 rtx source, int n ATTRIBUTE_UNUSED)
7443 rtx result, insn, set;
7444 HOST_WIDE_INT c0, c1;
7446 switch (mode)
7448 case QImode:
7449 case HImode:
7450 if (dest == NULL)
7451 dest = gen_reg_rtx (mode);
7452 emit_insn (gen_rtx_SET (VOIDmode, dest, source));
7453 return dest;
7455 case SImode:
7456 result = !can_create_pseudo_p () ? dest : gen_reg_rtx (SImode);
7458 emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (result),
7459 GEN_INT (INTVAL (source)
7460 & (~ (HOST_WIDE_INT) 0xffff))));
7461 emit_insn (gen_rtx_SET (VOIDmode, dest,
7462 gen_rtx_IOR (SImode, copy_rtx (result),
7463 GEN_INT (INTVAL (source) & 0xffff))));
7464 result = dest;
7465 break;
7467 case DImode:
7468 switch (GET_CODE (source))
7470 case CONST_INT:
7471 c0 = INTVAL (source);
7472 c1 = -(c0 < 0);
7473 break;
7475 default:
7476 gcc_unreachable ();
7479 result = rs6000_emit_set_long_const (dest, c0, c1);
7480 break;
7482 default:
7483 gcc_unreachable ();
7486 insn = get_last_insn ();
7487 set = single_set (insn);
7488 if (! CONSTANT_P (SET_SRC (set)))
7489 set_unique_reg_note (insn, REG_EQUAL, source);
7491 return result;
7494 /* Having failed to find a 3 insn sequence in rs6000_emit_set_const,
7495 fall back to a straight forward decomposition. We do this to avoid
7496 exponential run times encountered when looking for longer sequences
7497 with rs6000_emit_set_const. */
7498 static rtx
7499 rs6000_emit_set_long_const (rtx dest, HOST_WIDE_INT c1, HOST_WIDE_INT c2)
7501 if (!TARGET_POWERPC64)
7503 rtx operand1, operand2;
7505 operand1 = operand_subword_force (dest, WORDS_BIG_ENDIAN == 0,
7506 DImode);
7507 operand2 = operand_subword_force (copy_rtx (dest), WORDS_BIG_ENDIAN != 0,
7508 DImode);
7509 emit_move_insn (operand1, GEN_INT (c1));
7510 emit_move_insn (operand2, GEN_INT (c2));
7512 else
7514 HOST_WIDE_INT ud1, ud2, ud3, ud4;
7516 ud1 = c1 & 0xffff;
7517 ud2 = (c1 & 0xffff0000) >> 16;
7518 c2 = c1 >> 32;
7519 ud3 = c2 & 0xffff;
7520 ud4 = (c2 & 0xffff0000) >> 16;
7522 if ((ud4 == 0xffff && ud3 == 0xffff && ud2 == 0xffff && (ud1 & 0x8000))
7523 || (ud4 == 0 && ud3 == 0 && ud2 == 0 && ! (ud1 & 0x8000)))
7524 emit_move_insn (dest, GEN_INT ((ud1 ^ 0x8000) - 0x8000));
7526 else if ((ud4 == 0xffff && ud3 == 0xffff && (ud2 & 0x8000))
7527 || (ud4 == 0 && ud3 == 0 && ! (ud2 & 0x8000)))
7529 emit_move_insn (dest, GEN_INT (((ud2 << 16) ^ 0x80000000)
7530 - 0x80000000));
7531 if (ud1 != 0)
7532 emit_move_insn (copy_rtx (dest),
7533 gen_rtx_IOR (DImode, copy_rtx (dest),
7534 GEN_INT (ud1)));
7536 else if (ud3 == 0 && ud4 == 0)
7538 gcc_assert (ud2 & 0x8000);
7539 emit_move_insn (dest, GEN_INT (((ud2 << 16) ^ 0x80000000)
7540 - 0x80000000));
7541 if (ud1 != 0)
7542 emit_move_insn (copy_rtx (dest),
7543 gen_rtx_IOR (DImode, copy_rtx (dest),
7544 GEN_INT (ud1)));
7545 emit_move_insn (copy_rtx (dest),
7546 gen_rtx_ZERO_EXTEND (DImode,
7547 gen_lowpart (SImode,
7548 copy_rtx (dest))));
7550 else if ((ud4 == 0xffff && (ud3 & 0x8000))
7551 || (ud4 == 0 && ! (ud3 & 0x8000)))
7553 emit_move_insn (dest, GEN_INT (((ud3 << 16) ^ 0x80000000)
7554 - 0x80000000));
7555 if (ud2 != 0)
7556 emit_move_insn (copy_rtx (dest),
7557 gen_rtx_IOR (DImode, copy_rtx (dest),
7558 GEN_INT (ud2)));
7559 emit_move_insn (copy_rtx (dest),
7560 gen_rtx_ASHIFT (DImode, copy_rtx (dest),
7561 GEN_INT (16)));
7562 if (ud1 != 0)
7563 emit_move_insn (copy_rtx (dest),
7564 gen_rtx_IOR (DImode, copy_rtx (dest),
7565 GEN_INT (ud1)));
7567 else
7569 emit_move_insn (dest, GEN_INT (((ud4 << 16) ^ 0x80000000)
7570 - 0x80000000));
7571 if (ud3 != 0)
7572 emit_move_insn (copy_rtx (dest),
7573 gen_rtx_IOR (DImode, copy_rtx (dest),
7574 GEN_INT (ud3)));
7576 emit_move_insn (copy_rtx (dest),
7577 gen_rtx_ASHIFT (DImode, copy_rtx (dest),
7578 GEN_INT (32)));
7579 if (ud2 != 0)
7580 emit_move_insn (copy_rtx (dest),
7581 gen_rtx_IOR (DImode, copy_rtx (dest),
7582 GEN_INT (ud2 << 16)));
7583 if (ud1 != 0)
7584 emit_move_insn (copy_rtx (dest),
7585 gen_rtx_IOR (DImode, copy_rtx (dest),
7586 GEN_INT (ud1)));
7589 return dest;
7592 /* Helper for the following. Get rid of [r+r] memory refs
7593 in cases where it won't work (TImode, TFmode, TDmode, PTImode). */
7595 static void
7596 rs6000_eliminate_indexed_memrefs (rtx operands[2])
7598 if (reload_in_progress)
7599 return;
7601 if (GET_CODE (operands[0]) == MEM
7602 && GET_CODE (XEXP (operands[0], 0)) != REG
7603 && ! legitimate_constant_pool_address_p (XEXP (operands[0], 0),
7604 GET_MODE (operands[0]), false))
7605 operands[0]
7606 = replace_equiv_address (operands[0],
7607 copy_addr_to_reg (XEXP (operands[0], 0)));
7609 if (GET_CODE (operands[1]) == MEM
7610 && GET_CODE (XEXP (operands[1], 0)) != REG
7611 && ! legitimate_constant_pool_address_p (XEXP (operands[1], 0),
7612 GET_MODE (operands[1]), false))
7613 operands[1]
7614 = replace_equiv_address (operands[1],
7615 copy_addr_to_reg (XEXP (operands[1], 0)));
7618 /* Emit a move from SOURCE to DEST in mode MODE. */
7619 void
7620 rs6000_emit_move (rtx dest, rtx source, enum machine_mode mode)
7622 rtx operands[2];
7623 operands[0] = dest;
7624 operands[1] = source;
7626 if (TARGET_DEBUG_ADDR)
7628 fprintf (stderr,
7629 "\nrs6000_emit_move: mode = %s, reload_in_progress = %d, "
7630 "reload_completed = %d, can_create_pseudos = %d.\ndest:\n",
7631 GET_MODE_NAME (mode),
7632 reload_in_progress,
7633 reload_completed,
7634 can_create_pseudo_p ());
7635 debug_rtx (dest);
7636 fprintf (stderr, "source:\n");
7637 debug_rtx (source);
7640 /* Sanity checks. Check that we get CONST_DOUBLE only when we should. */
7641 if (GET_CODE (operands[1]) == CONST_DOUBLE
7642 && ! FLOAT_MODE_P (mode)
7643 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
7645 /* FIXME. This should never happen. */
7646 /* Since it seems that it does, do the safe thing and convert
7647 to a CONST_INT. */
7648 operands[1] = gen_int_mode (CONST_DOUBLE_LOW (operands[1]), mode);
7650 gcc_assert (GET_CODE (operands[1]) != CONST_DOUBLE
7651 || FLOAT_MODE_P (mode)
7652 || ((CONST_DOUBLE_HIGH (operands[1]) != 0
7653 || CONST_DOUBLE_LOW (operands[1]) < 0)
7654 && (CONST_DOUBLE_HIGH (operands[1]) != -1
7655 || CONST_DOUBLE_LOW (operands[1]) >= 0)));
7657 /* Check if GCC is setting up a block move that will end up using FP
7658 registers as temporaries. We must make sure this is acceptable. */
7659 if (GET_CODE (operands[0]) == MEM
7660 && GET_CODE (operands[1]) == MEM
7661 && mode == DImode
7662 && (SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[0]))
7663 || SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[1])))
7664 && ! (SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[0]) > 32
7665 ? 32 : MEM_ALIGN (operands[0])))
7666 || SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[1]) > 32
7667 ? 32
7668 : MEM_ALIGN (operands[1]))))
7669 && ! MEM_VOLATILE_P (operands [0])
7670 && ! MEM_VOLATILE_P (operands [1]))
7672 emit_move_insn (adjust_address (operands[0], SImode, 0),
7673 adjust_address (operands[1], SImode, 0));
7674 emit_move_insn (adjust_address (copy_rtx (operands[0]), SImode, 4),
7675 adjust_address (copy_rtx (operands[1]), SImode, 4));
7676 return;
7679 if (can_create_pseudo_p () && GET_CODE (operands[0]) == MEM
7680 && !gpc_reg_operand (operands[1], mode))
7681 operands[1] = force_reg (mode, operands[1]);
7683 /* Recognize the case where operand[1] is a reference to thread-local
7684 data and load its address to a register. */
7685 if (rs6000_tls_referenced_p (operands[1]))
7687 enum tls_model model;
7688 rtx tmp = operands[1];
7689 rtx addend = NULL;
7691 if (GET_CODE (tmp) == CONST && GET_CODE (XEXP (tmp, 0)) == PLUS)
7693 addend = XEXP (XEXP (tmp, 0), 1);
7694 tmp = XEXP (XEXP (tmp, 0), 0);
7697 gcc_assert (GET_CODE (tmp) == SYMBOL_REF);
7698 model = SYMBOL_REF_TLS_MODEL (tmp);
7699 gcc_assert (model != 0);
7701 tmp = rs6000_legitimize_tls_address (tmp, model);
7702 if (addend)
7704 tmp = gen_rtx_PLUS (mode, tmp, addend);
7705 tmp = force_operand (tmp, operands[0]);
7707 operands[1] = tmp;
7710 /* Handle the case where reload calls us with an invalid address. */
7711 if (reload_in_progress && mode == Pmode
7712 && (! general_operand (operands[1], mode)
7713 || ! nonimmediate_operand (operands[0], mode)))
7714 goto emit_set;
7716 /* 128-bit constant floating-point values on Darwin should really be
7717 loaded as two parts. */
7718 if (!TARGET_IEEEQUAD && TARGET_LONG_DOUBLE_128
7719 && mode == TFmode && GET_CODE (operands[1]) == CONST_DOUBLE)
7721 rs6000_emit_move (simplify_gen_subreg (DFmode, operands[0], mode, 0),
7722 simplify_gen_subreg (DFmode, operands[1], mode, 0),
7723 DFmode);
7724 rs6000_emit_move (simplify_gen_subreg (DFmode, operands[0], mode,
7725 GET_MODE_SIZE (DFmode)),
7726 simplify_gen_subreg (DFmode, operands[1], mode,
7727 GET_MODE_SIZE (DFmode)),
7728 DFmode);
7729 return;
7732 if (reload_in_progress && cfun->machine->sdmode_stack_slot != NULL_RTX)
7733 cfun->machine->sdmode_stack_slot =
7734 eliminate_regs (cfun->machine->sdmode_stack_slot, VOIDmode, NULL_RTX);
7736 if (reload_in_progress
7737 && mode == SDmode
7738 && cfun->machine->sdmode_stack_slot != NULL_RTX
7739 && MEM_P (operands[0])
7740 && rtx_equal_p (operands[0], cfun->machine->sdmode_stack_slot)
7741 && REG_P (operands[1]))
7743 if (FP_REGNO_P (REGNO (operands[1])))
7745 rtx mem = adjust_address_nv (operands[0], DDmode, 0);
7746 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
7747 emit_insn (gen_movsd_store (mem, operands[1]));
7749 else if (INT_REGNO_P (REGNO (operands[1])))
7751 rtx mem = adjust_address_nv (operands[0], mode, 4);
7752 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
7753 emit_insn (gen_movsd_hardfloat (mem, operands[1]));
7755 else
7756 gcc_unreachable();
7757 return;
7759 if (reload_in_progress
7760 && mode == SDmode
7761 && REG_P (operands[0])
7762 && MEM_P (operands[1])
7763 && cfun->machine->sdmode_stack_slot != NULL_RTX
7764 && rtx_equal_p (operands[1], cfun->machine->sdmode_stack_slot))
7766 if (FP_REGNO_P (REGNO (operands[0])))
7768 rtx mem = adjust_address_nv (operands[1], DDmode, 0);
7769 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
7770 emit_insn (gen_movsd_load (operands[0], mem));
7772 else if (INT_REGNO_P (REGNO (operands[0])))
7774 rtx mem = adjust_address_nv (operands[1], mode, 4);
7775 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
7776 emit_insn (gen_movsd_hardfloat (operands[0], mem));
7778 else
7779 gcc_unreachable();
7780 return;
7783 /* FIXME: In the long term, this switch statement should go away
7784 and be replaced by a sequence of tests based on things like
7785 mode == Pmode. */
7786 switch (mode)
7788 case HImode:
7789 case QImode:
7790 if (CONSTANT_P (operands[1])
7791 && GET_CODE (operands[1]) != CONST_INT)
7792 operands[1] = force_const_mem (mode, operands[1]);
7793 break;
7795 case TFmode:
7796 case TDmode:
7797 rs6000_eliminate_indexed_memrefs (operands);
7798 /* fall through */
7800 case DFmode:
7801 case DDmode:
7802 case SFmode:
7803 case SDmode:
7804 if (CONSTANT_P (operands[1])
7805 && ! easy_fp_constant (operands[1], mode))
7806 operands[1] = force_const_mem (mode, operands[1]);
7807 break;
7809 case V16QImode:
7810 case V8HImode:
7811 case V4SFmode:
7812 case V4SImode:
7813 case V4HImode:
7814 case V2SFmode:
7815 case V2SImode:
7816 case V1DImode:
7817 case V2DFmode:
7818 case V2DImode:
7819 if (CONSTANT_P (operands[1])
7820 && !easy_vector_constant (operands[1], mode))
7821 operands[1] = force_const_mem (mode, operands[1]);
7822 break;
7824 case SImode:
7825 case DImode:
7826 /* Use default pattern for address of ELF small data */
7827 if (TARGET_ELF
7828 && mode == Pmode
7829 && DEFAULT_ABI == ABI_V4
7830 && (GET_CODE (operands[1]) == SYMBOL_REF
7831 || GET_CODE (operands[1]) == CONST)
7832 && small_data_operand (operands[1], mode))
7834 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
7835 return;
7838 if (DEFAULT_ABI == ABI_V4
7839 && mode == Pmode && mode == SImode
7840 && flag_pic == 1 && got_operand (operands[1], mode))
7842 emit_insn (gen_movsi_got (operands[0], operands[1]));
7843 return;
7846 if ((TARGET_ELF || DEFAULT_ABI == ABI_DARWIN)
7847 && TARGET_NO_TOC
7848 && ! flag_pic
7849 && mode == Pmode
7850 && CONSTANT_P (operands[1])
7851 && GET_CODE (operands[1]) != HIGH
7852 && GET_CODE (operands[1]) != CONST_INT)
7854 rtx target = (!can_create_pseudo_p ()
7855 ? operands[0]
7856 : gen_reg_rtx (mode));
7858 /* If this is a function address on -mcall-aixdesc,
7859 convert it to the address of the descriptor. */
7860 if (DEFAULT_ABI == ABI_AIX
7861 && GET_CODE (operands[1]) == SYMBOL_REF
7862 && XSTR (operands[1], 0)[0] == '.')
7864 const char *name = XSTR (operands[1], 0);
7865 rtx new_ref;
7866 while (*name == '.')
7867 name++;
7868 new_ref = gen_rtx_SYMBOL_REF (Pmode, name);
7869 CONSTANT_POOL_ADDRESS_P (new_ref)
7870 = CONSTANT_POOL_ADDRESS_P (operands[1]);
7871 SYMBOL_REF_FLAGS (new_ref) = SYMBOL_REF_FLAGS (operands[1]);
7872 SYMBOL_REF_USED (new_ref) = SYMBOL_REF_USED (operands[1]);
7873 SYMBOL_REF_DATA (new_ref) = SYMBOL_REF_DATA (operands[1]);
7874 operands[1] = new_ref;
7877 if (DEFAULT_ABI == ABI_DARWIN)
7879 #if TARGET_MACHO
7880 if (MACHO_DYNAMIC_NO_PIC_P)
7882 /* Take care of any required data indirection. */
7883 operands[1] = rs6000_machopic_legitimize_pic_address (
7884 operands[1], mode, operands[0]);
7885 if (operands[0] != operands[1])
7886 emit_insn (gen_rtx_SET (VOIDmode,
7887 operands[0], operands[1]));
7888 return;
7890 #endif
7891 emit_insn (gen_macho_high (target, operands[1]));
7892 emit_insn (gen_macho_low (operands[0], target, operands[1]));
7893 return;
7896 emit_insn (gen_elf_high (target, operands[1]));
7897 emit_insn (gen_elf_low (operands[0], target, operands[1]));
7898 return;
7901 /* If this is a SYMBOL_REF that refers to a constant pool entry,
7902 and we have put it in the TOC, we just need to make a TOC-relative
7903 reference to it. */
7904 if (TARGET_TOC
7905 && GET_CODE (operands[1]) == SYMBOL_REF
7906 && use_toc_relative_ref (operands[1]))
7907 operands[1] = create_TOC_reference (operands[1], operands[0]);
7908 else if (mode == Pmode
7909 && CONSTANT_P (operands[1])
7910 && GET_CODE (operands[1]) != HIGH
7911 && ((GET_CODE (operands[1]) != CONST_INT
7912 && ! easy_fp_constant (operands[1], mode))
7913 || (GET_CODE (operands[1]) == CONST_INT
7914 && (num_insns_constant (operands[1], mode)
7915 > (TARGET_CMODEL != CMODEL_SMALL ? 3 : 2)))
7916 || (GET_CODE (operands[0]) == REG
7917 && FP_REGNO_P (REGNO (operands[0]))))
7918 && !toc_relative_expr_p (operands[1], false)
7919 && (TARGET_CMODEL == CMODEL_SMALL
7920 || can_create_pseudo_p ()
7921 || (REG_P (operands[0])
7922 && INT_REG_OK_FOR_BASE_P (operands[0], true))))
7925 #if TARGET_MACHO
7926 /* Darwin uses a special PIC legitimizer. */
7927 if (DEFAULT_ABI == ABI_DARWIN && MACHOPIC_INDIRECT)
7929 operands[1] =
7930 rs6000_machopic_legitimize_pic_address (operands[1], mode,
7931 operands[0]);
7932 if (operands[0] != operands[1])
7933 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
7934 return;
7936 #endif
7938 /* If we are to limit the number of things we put in the TOC and
7939 this is a symbol plus a constant we can add in one insn,
7940 just put the symbol in the TOC and add the constant. Don't do
7941 this if reload is in progress. */
7942 if (GET_CODE (operands[1]) == CONST
7943 && TARGET_NO_SUM_IN_TOC && ! reload_in_progress
7944 && GET_CODE (XEXP (operands[1], 0)) == PLUS
7945 && add_operand (XEXP (XEXP (operands[1], 0), 1), mode)
7946 && (GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == LABEL_REF
7947 || GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == SYMBOL_REF)
7948 && ! side_effects_p (operands[0]))
7950 rtx sym =
7951 force_const_mem (mode, XEXP (XEXP (operands[1], 0), 0));
7952 rtx other = XEXP (XEXP (operands[1], 0), 1);
7954 sym = force_reg (mode, sym);
7955 emit_insn (gen_add3_insn (operands[0], sym, other));
7956 return;
7959 operands[1] = force_const_mem (mode, operands[1]);
7961 if (TARGET_TOC
7962 && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
7963 && constant_pool_expr_p (XEXP (operands[1], 0))
7964 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (
7965 get_pool_constant (XEXP (operands[1], 0)),
7966 get_pool_mode (XEXP (operands[1], 0))))
7968 rtx tocref = create_TOC_reference (XEXP (operands[1], 0),
7969 operands[0]);
7970 operands[1] = gen_const_mem (mode, tocref);
7971 set_mem_alias_set (operands[1], get_TOC_alias_set ());
7974 break;
7976 case TImode:
7977 if (!VECTOR_MEM_VSX_P (TImode))
7978 rs6000_eliminate_indexed_memrefs (operands);
7979 break;
7981 case PTImode:
7982 rs6000_eliminate_indexed_memrefs (operands);
7983 break;
7985 default:
7986 fatal_insn ("bad move", gen_rtx_SET (VOIDmode, dest, source));
7989 /* Above, we may have called force_const_mem which may have returned
7990 an invalid address. If we can, fix this up; otherwise, reload will
7991 have to deal with it. */
7992 if (GET_CODE (operands[1]) == MEM && ! reload_in_progress)
7993 operands[1] = validize_mem (operands[1]);
7995 emit_set:
7996 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
7999 /* Return true if a structure, union or array containing FIELD should be
8000 accessed using `BLKMODE'.
8002 For the SPE, simd types are V2SI, and gcc can be tempted to put the
8003 entire thing in a DI and use subregs to access the internals.
8004 store_bit_field() will force (subreg:DI (reg:V2SI x))'s to the
8005 back-end. Because a single GPR can hold a V2SI, but not a DI, the
8006 best thing to do is set structs to BLKmode and avoid Severe Tire
8007 Damage.
8009 On e500 v2, DF and DI modes suffer from the same anomaly. DF can
8010 fit into 1, whereas DI still needs two. */
8012 static bool
8013 rs6000_member_type_forces_blk (const_tree field, enum machine_mode mode)
8015 return ((TARGET_SPE && TREE_CODE (TREE_TYPE (field)) == VECTOR_TYPE)
8016 || (TARGET_E500_DOUBLE && mode == DFmode));
8019 /* Nonzero if we can use a floating-point register to pass this arg. */
8020 #define USE_FP_FOR_ARG_P(CUM,MODE,TYPE) \
8021 (SCALAR_FLOAT_MODE_P (MODE) \
8022 && (CUM)->fregno <= FP_ARG_MAX_REG \
8023 && TARGET_HARD_FLOAT && TARGET_FPRS)
8025 /* Nonzero if we can use an AltiVec register to pass this arg. */
8026 #define USE_ALTIVEC_FOR_ARG_P(CUM,MODE,TYPE,NAMED) \
8027 (ALTIVEC_OR_VSX_VECTOR_MODE (MODE) \
8028 && (CUM)->vregno <= ALTIVEC_ARG_MAX_REG \
8029 && TARGET_ALTIVEC_ABI \
8030 && (NAMED))
8032 /* Return a nonzero value to say to return the function value in
8033 memory, just as large structures are always returned. TYPE will be
8034 the data type of the value, and FNTYPE will be the type of the
8035 function doing the returning, or @code{NULL} for libcalls.
8037 The AIX ABI for the RS/6000 specifies that all structures are
8038 returned in memory. The Darwin ABI does the same.
8040 For the Darwin 64 Bit ABI, a function result can be returned in
8041 registers or in memory, depending on the size of the return data
8042 type. If it is returned in registers, the value occupies the same
8043 registers as it would if it were the first and only function
8044 argument. Otherwise, the function places its result in memory at
8045 the location pointed to by GPR3.
8047 The SVR4 ABI specifies that structures <= 8 bytes are returned in r3/r4,
8048 but a draft put them in memory, and GCC used to implement the draft
8049 instead of the final standard. Therefore, aix_struct_return
8050 controls this instead of DEFAULT_ABI; V.4 targets needing backward
8051 compatibility can change DRAFT_V4_STRUCT_RET to override the
8052 default, and -m switches get the final word. See
8053 rs6000_option_override_internal for more details.
8055 The PPC32 SVR4 ABI uses IEEE double extended for long double, if 128-bit
8056 long double support is enabled. These values are returned in memory.
8058 int_size_in_bytes returns -1 for variable size objects, which go in
8059 memory always. The cast to unsigned makes -1 > 8. */
8061 static bool
8062 rs6000_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
8064 /* For the Darwin64 ABI, test if we can fit the return value in regs. */
8065 if (TARGET_MACHO
8066 && rs6000_darwin64_abi
8067 && TREE_CODE (type) == RECORD_TYPE
8068 && int_size_in_bytes (type) > 0)
8070 CUMULATIVE_ARGS valcum;
8071 rtx valret;
8073 valcum.words = 0;
8074 valcum.fregno = FP_ARG_MIN_REG;
8075 valcum.vregno = ALTIVEC_ARG_MIN_REG;
8076 /* Do a trial code generation as if this were going to be passed
8077 as an argument; if any part goes in memory, we return NULL. */
8078 valret = rs6000_darwin64_record_arg (&valcum, type, true, true);
8079 if (valret)
8080 return false;
8081 /* Otherwise fall through to more conventional ABI rules. */
8084 if (AGGREGATE_TYPE_P (type)
8085 && (aix_struct_return
8086 || (unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 8))
8087 return true;
8089 /* Allow -maltivec -mabi=no-altivec without warning. Altivec vector
8090 modes only exist for GCC vector types if -maltivec. */
8091 if (TARGET_32BIT && !TARGET_ALTIVEC_ABI
8092 && ALTIVEC_VECTOR_MODE (TYPE_MODE (type)))
8093 return false;
8095 /* Return synthetic vectors in memory. */
8096 if (TREE_CODE (type) == VECTOR_TYPE
8097 && int_size_in_bytes (type) > (TARGET_ALTIVEC_ABI ? 16 : 8))
8099 static bool warned_for_return_big_vectors = false;
8100 if (!warned_for_return_big_vectors)
8102 warning (0, "GCC vector returned by reference: "
8103 "non-standard ABI extension with no compatibility guarantee");
8104 warned_for_return_big_vectors = true;
8106 return true;
8109 if (DEFAULT_ABI == ABI_V4 && TARGET_IEEEQUAD && TYPE_MODE (type) == TFmode)
8110 return true;
8112 return false;
8115 #ifdef HAVE_AS_GNU_ATTRIBUTE
8116 /* Return TRUE if a call to function FNDECL may be one that
8117 potentially affects the function calling ABI of the object file. */
8119 static bool
8120 call_ABI_of_interest (tree fndecl)
8122 if (cgraph_state == CGRAPH_STATE_EXPANSION)
8124 struct cgraph_node *c_node;
8126 /* Libcalls are always interesting. */
8127 if (fndecl == NULL_TREE)
8128 return true;
8130 /* Any call to an external function is interesting. */
8131 if (DECL_EXTERNAL (fndecl))
8132 return true;
8134 /* Interesting functions that we are emitting in this object file. */
8135 c_node = cgraph_get_node (fndecl);
8136 c_node = cgraph_function_or_thunk_node (c_node, NULL);
8137 return !cgraph_only_called_directly_p (c_node);
8139 return false;
8141 #endif
8143 /* Initialize a variable CUM of type CUMULATIVE_ARGS
8144 for a call to a function whose data type is FNTYPE.
8145 For a library call, FNTYPE is 0 and RETURN_MODE the return value mode.
8147 For incoming args we set the number of arguments in the prototype large
8148 so we never return a PARALLEL. */
8150 void
8151 init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype,
8152 rtx libname ATTRIBUTE_UNUSED, int incoming,
8153 int libcall, int n_named_args,
8154 tree fndecl ATTRIBUTE_UNUSED,
8155 enum machine_mode return_mode ATTRIBUTE_UNUSED)
8157 static CUMULATIVE_ARGS zero_cumulative;
8159 *cum = zero_cumulative;
8160 cum->words = 0;
8161 cum->fregno = FP_ARG_MIN_REG;
8162 cum->vregno = ALTIVEC_ARG_MIN_REG;
8163 cum->prototype = (fntype && prototype_p (fntype));
8164 cum->call_cookie = ((DEFAULT_ABI == ABI_V4 && libcall)
8165 ? CALL_LIBCALL : CALL_NORMAL);
8166 cum->sysv_gregno = GP_ARG_MIN_REG;
8167 cum->stdarg = stdarg_p (fntype);
8169 cum->nargs_prototype = 0;
8170 if (incoming || cum->prototype)
8171 cum->nargs_prototype = n_named_args;
8173 /* Check for a longcall attribute. */
8174 if ((!fntype && rs6000_default_long_calls)
8175 || (fntype
8176 && lookup_attribute ("longcall", TYPE_ATTRIBUTES (fntype))
8177 && !lookup_attribute ("shortcall", TYPE_ATTRIBUTES (fntype))))
8178 cum->call_cookie |= CALL_LONG;
8180 if (TARGET_DEBUG_ARG)
8182 fprintf (stderr, "\ninit_cumulative_args:");
8183 if (fntype)
8185 tree ret_type = TREE_TYPE (fntype);
8186 fprintf (stderr, " ret code = %s,",
8187 tree_code_name[ (int)TREE_CODE (ret_type) ]);
8190 if (cum->call_cookie & CALL_LONG)
8191 fprintf (stderr, " longcall,");
8193 fprintf (stderr, " proto = %d, nargs = %d\n",
8194 cum->prototype, cum->nargs_prototype);
8197 #ifdef HAVE_AS_GNU_ATTRIBUTE
8198 if (DEFAULT_ABI == ABI_V4)
8200 cum->escapes = call_ABI_of_interest (fndecl);
8201 if (cum->escapes)
8203 tree return_type;
8205 if (fntype)
8207 return_type = TREE_TYPE (fntype);
8208 return_mode = TYPE_MODE (return_type);
8210 else
8211 return_type = lang_hooks.types.type_for_mode (return_mode, 0);
8213 if (return_type != NULL)
8215 if (TREE_CODE (return_type) == RECORD_TYPE
8216 && TYPE_TRANSPARENT_AGGR (return_type))
8218 return_type = TREE_TYPE (first_field (return_type));
8219 return_mode = TYPE_MODE (return_type);
8221 if (AGGREGATE_TYPE_P (return_type)
8222 && ((unsigned HOST_WIDE_INT) int_size_in_bytes (return_type)
8223 <= 8))
8224 rs6000_returns_struct = true;
8226 if (SCALAR_FLOAT_MODE_P (return_mode))
8227 rs6000_passes_float = true;
8228 else if (ALTIVEC_OR_VSX_VECTOR_MODE (return_mode)
8229 || SPE_VECTOR_MODE (return_mode))
8230 rs6000_passes_vector = true;
8233 #endif
8235 if (fntype
8236 && !TARGET_ALTIVEC
8237 && TARGET_ALTIVEC_ABI
8238 && ALTIVEC_VECTOR_MODE (TYPE_MODE (TREE_TYPE (fntype))))
8240 error ("cannot return value in vector register because"
8241 " altivec instructions are disabled, use -maltivec"
8242 " to enable them");
8246 /* Return true if TYPE must be passed on the stack and not in registers. */
8248 static bool
8249 rs6000_must_pass_in_stack (enum machine_mode mode, const_tree type)
8251 if (DEFAULT_ABI == ABI_AIX || TARGET_64BIT)
8252 return must_pass_in_stack_var_size (mode, type);
8253 else
8254 return must_pass_in_stack_var_size_or_pad (mode, type);
8257 /* If defined, a C expression which determines whether, and in which
8258 direction, to pad out an argument with extra space. The value
8259 should be of type `enum direction': either `upward' to pad above
8260 the argument, `downward' to pad below, or `none' to inhibit
8261 padding.
8263 For the AIX ABI structs are always stored left shifted in their
8264 argument slot. */
8266 enum direction
8267 function_arg_padding (enum machine_mode mode, const_tree type)
8269 #ifndef AGGREGATE_PADDING_FIXED
8270 #define AGGREGATE_PADDING_FIXED 0
8271 #endif
8272 #ifndef AGGREGATES_PAD_UPWARD_ALWAYS
8273 #define AGGREGATES_PAD_UPWARD_ALWAYS 0
8274 #endif
8276 if (!AGGREGATE_PADDING_FIXED)
8278 /* GCC used to pass structures of the same size as integer types as
8279 if they were in fact integers, ignoring FUNCTION_ARG_PADDING.
8280 i.e. Structures of size 1 or 2 (or 4 when TARGET_64BIT) were
8281 passed padded downward, except that -mstrict-align further
8282 muddied the water in that multi-component structures of 2 and 4
8283 bytes in size were passed padded upward.
8285 The following arranges for best compatibility with previous
8286 versions of gcc, but removes the -mstrict-align dependency. */
8287 if (BYTES_BIG_ENDIAN)
8289 HOST_WIDE_INT size = 0;
8291 if (mode == BLKmode)
8293 if (type && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
8294 size = int_size_in_bytes (type);
8296 else
8297 size = GET_MODE_SIZE (mode);
8299 if (size == 1 || size == 2 || size == 4)
8300 return downward;
8302 return upward;
8305 if (AGGREGATES_PAD_UPWARD_ALWAYS)
8307 if (type != 0 && AGGREGATE_TYPE_P (type))
8308 return upward;
8311 /* Fall back to the default. */
8312 return DEFAULT_FUNCTION_ARG_PADDING (mode, type);
8315 /* If defined, a C expression that gives the alignment boundary, in bits,
8316 of an argument with the specified mode and type. If it is not defined,
8317 PARM_BOUNDARY is used for all arguments.
8319 V.4 wants long longs and doubles to be double word aligned. Just
8320 testing the mode size is a boneheaded way to do this as it means
8321 that other types such as complex int are also double word aligned.
8322 However, we're stuck with this because changing the ABI might break
8323 existing library interfaces.
8325 Doubleword align SPE vectors.
8326 Quadword align Altivec/VSX vectors.
8327 Quadword align large synthetic vector types. */
8329 static unsigned int
8330 rs6000_function_arg_boundary (enum machine_mode mode, const_tree type)
8332 if (DEFAULT_ABI == ABI_V4
8333 && (GET_MODE_SIZE (mode) == 8
8334 || (TARGET_HARD_FLOAT
8335 && TARGET_FPRS
8336 && (mode == TFmode || mode == TDmode))))
8337 return 64;
8338 else if (SPE_VECTOR_MODE (mode)
8339 || (type && TREE_CODE (type) == VECTOR_TYPE
8340 && int_size_in_bytes (type) >= 8
8341 && int_size_in_bytes (type) < 16))
8342 return 64;
8343 else if (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
8344 || (type && TREE_CODE (type) == VECTOR_TYPE
8345 && int_size_in_bytes (type) >= 16))
8346 return 128;
8347 else if (TARGET_MACHO
8348 && rs6000_darwin64_abi
8349 && mode == BLKmode
8350 && type && TYPE_ALIGN (type) > 64)
8351 return 128;
8352 else
8353 return PARM_BOUNDARY;
8356 /* For a function parm of MODE and TYPE, return the starting word in
8357 the parameter area. NWORDS of the parameter area are already used. */
8359 static unsigned int
8360 rs6000_parm_start (enum machine_mode mode, const_tree type,
8361 unsigned int nwords)
8363 unsigned int align;
8364 unsigned int parm_offset;
8366 align = rs6000_function_arg_boundary (mode, type) / PARM_BOUNDARY - 1;
8367 parm_offset = DEFAULT_ABI == ABI_V4 ? 2 : 6;
8368 return nwords + (-(parm_offset + nwords) & align);
8371 /* Compute the size (in words) of a function argument. */
8373 static unsigned long
8374 rs6000_arg_size (enum machine_mode mode, const_tree type)
8376 unsigned long size;
8378 if (mode != BLKmode)
8379 size = GET_MODE_SIZE (mode);
8380 else
8381 size = int_size_in_bytes (type);
8383 if (TARGET_32BIT)
8384 return (size + 3) >> 2;
8385 else
8386 return (size + 7) >> 3;
8389 /* Use this to flush pending int fields. */
8391 static void
8392 rs6000_darwin64_record_arg_advance_flush (CUMULATIVE_ARGS *cum,
8393 HOST_WIDE_INT bitpos, int final)
8395 unsigned int startbit, endbit;
8396 int intregs, intoffset;
8397 enum machine_mode mode;
8399 /* Handle the situations where a float is taking up the first half
8400 of the GPR, and the other half is empty (typically due to
8401 alignment restrictions). We can detect this by a 8-byte-aligned
8402 int field, or by seeing that this is the final flush for this
8403 argument. Count the word and continue on. */
8404 if (cum->floats_in_gpr == 1
8405 && (cum->intoffset % 64 == 0
8406 || (cum->intoffset == -1 && final)))
8408 cum->words++;
8409 cum->floats_in_gpr = 0;
8412 if (cum->intoffset == -1)
8413 return;
8415 intoffset = cum->intoffset;
8416 cum->intoffset = -1;
8417 cum->floats_in_gpr = 0;
8419 if (intoffset % BITS_PER_WORD != 0)
8421 mode = mode_for_size (BITS_PER_WORD - intoffset % BITS_PER_WORD,
8422 MODE_INT, 0);
8423 if (mode == BLKmode)
8425 /* We couldn't find an appropriate mode, which happens,
8426 e.g., in packed structs when there are 3 bytes to load.
8427 Back intoffset back to the beginning of the word in this
8428 case. */
8429 intoffset = intoffset & -BITS_PER_WORD;
8433 startbit = intoffset & -BITS_PER_WORD;
8434 endbit = (bitpos + BITS_PER_WORD - 1) & -BITS_PER_WORD;
8435 intregs = (endbit - startbit) / BITS_PER_WORD;
8436 cum->words += intregs;
8437 /* words should be unsigned. */
8438 if ((unsigned)cum->words < (endbit/BITS_PER_WORD))
8440 int pad = (endbit/BITS_PER_WORD) - cum->words;
8441 cum->words += pad;
8445 /* The darwin64 ABI calls for us to recurse down through structs,
8446 looking for elements passed in registers. Unfortunately, we have
8447 to track int register count here also because of misalignments
8448 in powerpc alignment mode. */
8450 static void
8451 rs6000_darwin64_record_arg_advance_recurse (CUMULATIVE_ARGS *cum,
8452 const_tree type,
8453 HOST_WIDE_INT startbitpos)
8455 tree f;
8457 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
8458 if (TREE_CODE (f) == FIELD_DECL)
8460 HOST_WIDE_INT bitpos = startbitpos;
8461 tree ftype = TREE_TYPE (f);
8462 enum machine_mode mode;
8463 if (ftype == error_mark_node)
8464 continue;
8465 mode = TYPE_MODE (ftype);
8467 if (DECL_SIZE (f) != 0
8468 && host_integerp (bit_position (f), 1))
8469 bitpos += int_bit_position (f);
8471 /* ??? FIXME: else assume zero offset. */
8473 if (TREE_CODE (ftype) == RECORD_TYPE)
8474 rs6000_darwin64_record_arg_advance_recurse (cum, ftype, bitpos);
8475 else if (USE_FP_FOR_ARG_P (cum, mode, ftype))
8477 unsigned n_fpregs = (GET_MODE_SIZE (mode) + 7) >> 3;
8478 rs6000_darwin64_record_arg_advance_flush (cum, bitpos, 0);
8479 cum->fregno += n_fpregs;
8480 /* Single-precision floats present a special problem for
8481 us, because they are smaller than an 8-byte GPR, and so
8482 the structure-packing rules combined with the standard
8483 varargs behavior mean that we want to pack float/float
8484 and float/int combinations into a single register's
8485 space. This is complicated by the arg advance flushing,
8486 which works on arbitrarily large groups of int-type
8487 fields. */
8488 if (mode == SFmode)
8490 if (cum->floats_in_gpr == 1)
8492 /* Two floats in a word; count the word and reset
8493 the float count. */
8494 cum->words++;
8495 cum->floats_in_gpr = 0;
8497 else if (bitpos % 64 == 0)
8499 /* A float at the beginning of an 8-byte word;
8500 count it and put off adjusting cum->words until
8501 we see if a arg advance flush is going to do it
8502 for us. */
8503 cum->floats_in_gpr++;
8505 else
8507 /* The float is at the end of a word, preceded
8508 by integer fields, so the arg advance flush
8509 just above has already set cum->words and
8510 everything is taken care of. */
8513 else
8514 cum->words += n_fpregs;
8516 else if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, 1))
8518 rs6000_darwin64_record_arg_advance_flush (cum, bitpos, 0);
8519 cum->vregno++;
8520 cum->words += 2;
8522 else if (cum->intoffset == -1)
8523 cum->intoffset = bitpos;
8527 /* Check for an item that needs to be considered specially under the darwin 64
8528 bit ABI. These are record types where the mode is BLK or the structure is
8529 8 bytes in size. */
8530 static int
8531 rs6000_darwin64_struct_check_p (enum machine_mode mode, const_tree type)
8533 return rs6000_darwin64_abi
8534 && ((mode == BLKmode
8535 && TREE_CODE (type) == RECORD_TYPE
8536 && int_size_in_bytes (type) > 0)
8537 || (type && TREE_CODE (type) == RECORD_TYPE
8538 && int_size_in_bytes (type) == 8)) ? 1 : 0;
8541 /* Update the data in CUM to advance over an argument
8542 of mode MODE and data type TYPE.
8543 (TYPE is null for libcalls where that information may not be available.)
8545 Note that for args passed by reference, function_arg will be called
8546 with MODE and TYPE set to that of the pointer to the arg, not the arg
8547 itself. */
8549 static void
8550 rs6000_function_arg_advance_1 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
8551 const_tree type, bool named, int depth)
8553 /* Only tick off an argument if we're not recursing. */
8554 if (depth == 0)
8555 cum->nargs_prototype--;
8557 #ifdef HAVE_AS_GNU_ATTRIBUTE
8558 if (DEFAULT_ABI == ABI_V4
8559 && cum->escapes)
8561 if (SCALAR_FLOAT_MODE_P (mode))
8562 rs6000_passes_float = true;
8563 else if (named && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
8564 rs6000_passes_vector = true;
8565 else if (SPE_VECTOR_MODE (mode)
8566 && !cum->stdarg
8567 && cum->sysv_gregno <= GP_ARG_MAX_REG)
8568 rs6000_passes_vector = true;
8570 #endif
8572 if (TARGET_ALTIVEC_ABI
8573 && (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
8574 || (type && TREE_CODE (type) == VECTOR_TYPE
8575 && int_size_in_bytes (type) == 16)))
8577 bool stack = false;
8579 if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, named))
8581 cum->vregno++;
8582 if (!TARGET_ALTIVEC)
8583 error ("cannot pass argument in vector register because"
8584 " altivec instructions are disabled, use -maltivec"
8585 " to enable them");
8587 /* PowerPC64 Linux and AIX allocate GPRs for a vector argument
8588 even if it is going to be passed in a vector register.
8589 Darwin does the same for variable-argument functions. */
8590 if ((DEFAULT_ABI == ABI_AIX && TARGET_64BIT)
8591 || (cum->stdarg && DEFAULT_ABI != ABI_V4))
8592 stack = true;
8594 else
8595 stack = true;
8597 if (stack)
8599 int align;
8601 /* Vector parameters must be 16-byte aligned. This places
8602 them at 2 mod 4 in terms of words in 32-bit mode, since
8603 the parameter save area starts at offset 24 from the
8604 stack. In 64-bit mode, they just have to start on an
8605 even word, since the parameter save area is 16-byte
8606 aligned. Space for GPRs is reserved even if the argument
8607 will be passed in memory. */
8608 if (TARGET_32BIT)
8609 align = (2 - cum->words) & 3;
8610 else
8611 align = cum->words & 1;
8612 cum->words += align + rs6000_arg_size (mode, type);
8614 if (TARGET_DEBUG_ARG)
8616 fprintf (stderr, "function_adv: words = %2d, align=%d, ",
8617 cum->words, align);
8618 fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s\n",
8619 cum->nargs_prototype, cum->prototype,
8620 GET_MODE_NAME (mode));
8624 else if (TARGET_SPE_ABI && TARGET_SPE && SPE_VECTOR_MODE (mode)
8625 && !cum->stdarg
8626 && cum->sysv_gregno <= GP_ARG_MAX_REG)
8627 cum->sysv_gregno++;
8629 else if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
8631 int size = int_size_in_bytes (type);
8632 /* Variable sized types have size == -1 and are
8633 treated as if consisting entirely of ints.
8634 Pad to 16 byte boundary if needed. */
8635 if (TYPE_ALIGN (type) >= 2 * BITS_PER_WORD
8636 && (cum->words % 2) != 0)
8637 cum->words++;
8638 /* For varargs, we can just go up by the size of the struct. */
8639 if (!named)
8640 cum->words += (size + 7) / 8;
8641 else
8643 /* It is tempting to say int register count just goes up by
8644 sizeof(type)/8, but this is wrong in a case such as
8645 { int; double; int; } [powerpc alignment]. We have to
8646 grovel through the fields for these too. */
8647 cum->intoffset = 0;
8648 cum->floats_in_gpr = 0;
8649 rs6000_darwin64_record_arg_advance_recurse (cum, type, 0);
8650 rs6000_darwin64_record_arg_advance_flush (cum,
8651 size * BITS_PER_UNIT, 1);
8653 if (TARGET_DEBUG_ARG)
8655 fprintf (stderr, "function_adv: words = %2d, align=%d, size=%d",
8656 cum->words, TYPE_ALIGN (type), size);
8657 fprintf (stderr,
8658 "nargs = %4d, proto = %d, mode = %4s (darwin64 abi)\n",
8659 cum->nargs_prototype, cum->prototype,
8660 GET_MODE_NAME (mode));
8663 else if (DEFAULT_ABI == ABI_V4)
8665 if (TARGET_HARD_FLOAT && TARGET_FPRS
8666 && ((TARGET_SINGLE_FLOAT && mode == SFmode)
8667 || (TARGET_DOUBLE_FLOAT && mode == DFmode)
8668 || (mode == TFmode && !TARGET_IEEEQUAD)
8669 || mode == SDmode || mode == DDmode || mode == TDmode))
8671 /* _Decimal128 must use an even/odd register pair. This assumes
8672 that the register number is odd when fregno is odd. */
8673 if (mode == TDmode && (cum->fregno % 2) == 1)
8674 cum->fregno++;
8676 if (cum->fregno + (mode == TFmode || mode == TDmode ? 1 : 0)
8677 <= FP_ARG_V4_MAX_REG)
8678 cum->fregno += (GET_MODE_SIZE (mode) + 7) >> 3;
8679 else
8681 cum->fregno = FP_ARG_V4_MAX_REG + 1;
8682 if (mode == DFmode || mode == TFmode
8683 || mode == DDmode || mode == TDmode)
8684 cum->words += cum->words & 1;
8685 cum->words += rs6000_arg_size (mode, type);
8688 else
8690 int n_words = rs6000_arg_size (mode, type);
8691 int gregno = cum->sysv_gregno;
8693 /* Long long and SPE vectors are put in (r3,r4), (r5,r6),
8694 (r7,r8) or (r9,r10). As does any other 2 word item such
8695 as complex int due to a historical mistake. */
8696 if (n_words == 2)
8697 gregno += (1 - gregno) & 1;
8699 /* Multi-reg args are not split between registers and stack. */
8700 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
8702 /* Long long and SPE vectors are aligned on the stack.
8703 So are other 2 word items such as complex int due to
8704 a historical mistake. */
8705 if (n_words == 2)
8706 cum->words += cum->words & 1;
8707 cum->words += n_words;
8710 /* Note: continuing to accumulate gregno past when we've started
8711 spilling to the stack indicates the fact that we've started
8712 spilling to the stack to expand_builtin_saveregs. */
8713 cum->sysv_gregno = gregno + n_words;
8716 if (TARGET_DEBUG_ARG)
8718 fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
8719 cum->words, cum->fregno);
8720 fprintf (stderr, "gregno = %2d, nargs = %4d, proto = %d, ",
8721 cum->sysv_gregno, cum->nargs_prototype, cum->prototype);
8722 fprintf (stderr, "mode = %4s, named = %d\n",
8723 GET_MODE_NAME (mode), named);
8726 else
8728 int n_words = rs6000_arg_size (mode, type);
8729 int start_words = cum->words;
8730 int align_words = rs6000_parm_start (mode, type, start_words);
8732 cum->words = align_words + n_words;
8734 if (SCALAR_FLOAT_MODE_P (mode)
8735 && TARGET_HARD_FLOAT && TARGET_FPRS)
8737 /* _Decimal128 must be passed in an even/odd float register pair.
8738 This assumes that the register number is odd when fregno is
8739 odd. */
8740 if (mode == TDmode && (cum->fregno % 2) == 1)
8741 cum->fregno++;
8742 cum->fregno += (GET_MODE_SIZE (mode) + 7) >> 3;
8745 if (TARGET_DEBUG_ARG)
8747 fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
8748 cum->words, cum->fregno);
8749 fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s, ",
8750 cum->nargs_prototype, cum->prototype, GET_MODE_NAME (mode));
8751 fprintf (stderr, "named = %d, align = %d, depth = %d\n",
8752 named, align_words - start_words, depth);
8757 static void
8758 rs6000_function_arg_advance (cumulative_args_t cum, enum machine_mode mode,
8759 const_tree type, bool named)
8761 rs6000_function_arg_advance_1 (get_cumulative_args (cum), mode, type, named,
8765 static rtx
8766 spe_build_register_parallel (enum machine_mode mode, int gregno)
8768 rtx r1, r3, r5, r7;
8770 switch (mode)
8772 case DFmode:
8773 r1 = gen_rtx_REG (DImode, gregno);
8774 r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
8775 return gen_rtx_PARALLEL (mode, gen_rtvec (1, r1));
8777 case DCmode:
8778 case TFmode:
8779 r1 = gen_rtx_REG (DImode, gregno);
8780 r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
8781 r3 = gen_rtx_REG (DImode, gregno + 2);
8782 r3 = gen_rtx_EXPR_LIST (VOIDmode, r3, GEN_INT (8));
8783 return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r3));
8785 case TCmode:
8786 r1 = gen_rtx_REG (DImode, gregno);
8787 r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
8788 r3 = gen_rtx_REG (DImode, gregno + 2);
8789 r3 = gen_rtx_EXPR_LIST (VOIDmode, r3, GEN_INT (8));
8790 r5 = gen_rtx_REG (DImode, gregno + 4);
8791 r5 = gen_rtx_EXPR_LIST (VOIDmode, r5, GEN_INT (16));
8792 r7 = gen_rtx_REG (DImode, gregno + 6);
8793 r7 = gen_rtx_EXPR_LIST (VOIDmode, r7, GEN_INT (24));
8794 return gen_rtx_PARALLEL (mode, gen_rtvec (4, r1, r3, r5, r7));
8796 default:
8797 gcc_unreachable ();
8801 /* Determine where to put a SIMD argument on the SPE. */
8802 static rtx
8803 rs6000_spe_function_arg (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
8804 const_tree type)
8806 int gregno = cum->sysv_gregno;
8808 /* On E500 v2, double arithmetic is done on the full 64-bit GPR, but
8809 are passed and returned in a pair of GPRs for ABI compatibility. */
8810 if (TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode
8811 || mode == DCmode || mode == TCmode))
8813 int n_words = rs6000_arg_size (mode, type);
8815 /* Doubles go in an odd/even register pair (r5/r6, etc). */
8816 if (mode == DFmode)
8817 gregno += (1 - gregno) & 1;
8819 /* Multi-reg args are not split between registers and stack. */
8820 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
8821 return NULL_RTX;
8823 return spe_build_register_parallel (mode, gregno);
8825 if (cum->stdarg)
8827 int n_words = rs6000_arg_size (mode, type);
8829 /* SPE vectors are put in odd registers. */
8830 if (n_words == 2 && (gregno & 1) == 0)
8831 gregno += 1;
8833 if (gregno + n_words - 1 <= GP_ARG_MAX_REG)
8835 rtx r1, r2;
8836 enum machine_mode m = SImode;
8838 r1 = gen_rtx_REG (m, gregno);
8839 r1 = gen_rtx_EXPR_LIST (m, r1, const0_rtx);
8840 r2 = gen_rtx_REG (m, gregno + 1);
8841 r2 = gen_rtx_EXPR_LIST (m, r2, GEN_INT (4));
8842 return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
8844 else
8845 return NULL_RTX;
8847 else
8849 if (gregno <= GP_ARG_MAX_REG)
8850 return gen_rtx_REG (mode, gregno);
8851 else
8852 return NULL_RTX;
8856 /* A subroutine of rs6000_darwin64_record_arg. Assign the bits of the
8857 structure between cum->intoffset and bitpos to integer registers. */
8859 static void
8860 rs6000_darwin64_record_arg_flush (CUMULATIVE_ARGS *cum,
8861 HOST_WIDE_INT bitpos, rtx rvec[], int *k)
8863 enum machine_mode mode;
8864 unsigned int regno;
8865 unsigned int startbit, endbit;
8866 int this_regno, intregs, intoffset;
8867 rtx reg;
8869 if (cum->intoffset == -1)
8870 return;
8872 intoffset = cum->intoffset;
8873 cum->intoffset = -1;
8875 /* If this is the trailing part of a word, try to only load that
8876 much into the register. Otherwise load the whole register. Note
8877 that in the latter case we may pick up unwanted bits. It's not a
8878 problem at the moment but may wish to revisit. */
8880 if (intoffset % BITS_PER_WORD != 0)
8882 mode = mode_for_size (BITS_PER_WORD - intoffset % BITS_PER_WORD,
8883 MODE_INT, 0);
8884 if (mode == BLKmode)
8886 /* We couldn't find an appropriate mode, which happens,
8887 e.g., in packed structs when there are 3 bytes to load.
8888 Back intoffset back to the beginning of the word in this
8889 case. */
8890 intoffset = intoffset & -BITS_PER_WORD;
8891 mode = word_mode;
8894 else
8895 mode = word_mode;
8897 startbit = intoffset & -BITS_PER_WORD;
8898 endbit = (bitpos + BITS_PER_WORD - 1) & -BITS_PER_WORD;
8899 intregs = (endbit - startbit) / BITS_PER_WORD;
8900 this_regno = cum->words + intoffset / BITS_PER_WORD;
8902 if (intregs > 0 && intregs > GP_ARG_NUM_REG - this_regno)
8903 cum->use_stack = 1;
8905 intregs = MIN (intregs, GP_ARG_NUM_REG - this_regno);
8906 if (intregs <= 0)
8907 return;
8909 intoffset /= BITS_PER_UNIT;
8912 regno = GP_ARG_MIN_REG + this_regno;
8913 reg = gen_rtx_REG (mode, regno);
8914 rvec[(*k)++] =
8915 gen_rtx_EXPR_LIST (VOIDmode, reg, GEN_INT (intoffset));
8917 this_regno += 1;
8918 intoffset = (intoffset | (UNITS_PER_WORD-1)) + 1;
8919 mode = word_mode;
8920 intregs -= 1;
8922 while (intregs > 0);
8925 /* Recursive workhorse for the following. */
8927 static void
8928 rs6000_darwin64_record_arg_recurse (CUMULATIVE_ARGS *cum, const_tree type,
8929 HOST_WIDE_INT startbitpos, rtx rvec[],
8930 int *k)
8932 tree f;
8934 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
8935 if (TREE_CODE (f) == FIELD_DECL)
8937 HOST_WIDE_INT bitpos = startbitpos;
8938 tree ftype = TREE_TYPE (f);
8939 enum machine_mode mode;
8940 if (ftype == error_mark_node)
8941 continue;
8942 mode = TYPE_MODE (ftype);
8944 if (DECL_SIZE (f) != 0
8945 && host_integerp (bit_position (f), 1))
8946 bitpos += int_bit_position (f);
8948 /* ??? FIXME: else assume zero offset. */
8950 if (TREE_CODE (ftype) == RECORD_TYPE)
8951 rs6000_darwin64_record_arg_recurse (cum, ftype, bitpos, rvec, k);
8952 else if (cum->named && USE_FP_FOR_ARG_P (cum, mode, ftype))
8954 unsigned n_fpreg = (GET_MODE_SIZE (mode) + 7) >> 3;
8955 #if 0
8956 switch (mode)
8958 case SCmode: mode = SFmode; break;
8959 case DCmode: mode = DFmode; break;
8960 case TCmode: mode = TFmode; break;
8961 default: break;
8963 #endif
8964 rs6000_darwin64_record_arg_flush (cum, bitpos, rvec, k);
8965 if (cum->fregno + n_fpreg > FP_ARG_MAX_REG + 1)
8967 gcc_assert (cum->fregno == FP_ARG_MAX_REG
8968 && (mode == TFmode || mode == TDmode));
8969 /* Long double or _Decimal128 split over regs and memory. */
8970 mode = DECIMAL_FLOAT_MODE_P (mode) ? DDmode : DFmode;
8971 cum->use_stack=1;
8973 rvec[(*k)++]
8974 = gen_rtx_EXPR_LIST (VOIDmode,
8975 gen_rtx_REG (mode, cum->fregno++),
8976 GEN_INT (bitpos / BITS_PER_UNIT));
8977 if (mode == TFmode || mode == TDmode)
8978 cum->fregno++;
8980 else if (cum->named && USE_ALTIVEC_FOR_ARG_P (cum, mode, ftype, 1))
8982 rs6000_darwin64_record_arg_flush (cum, bitpos, rvec, k);
8983 rvec[(*k)++]
8984 = gen_rtx_EXPR_LIST (VOIDmode,
8985 gen_rtx_REG (mode, cum->vregno++),
8986 GEN_INT (bitpos / BITS_PER_UNIT));
8988 else if (cum->intoffset == -1)
8989 cum->intoffset = bitpos;
8993 /* For the darwin64 ABI, we want to construct a PARALLEL consisting of
8994 the register(s) to be used for each field and subfield of a struct
8995 being passed by value, along with the offset of where the
8996 register's value may be found in the block. FP fields go in FP
8997 register, vector fields go in vector registers, and everything
8998 else goes in int registers, packed as in memory.
9000 This code is also used for function return values. RETVAL indicates
9001 whether this is the case.
9003 Much of this is taken from the SPARC V9 port, which has a similar
9004 calling convention. */
9006 static rtx
9007 rs6000_darwin64_record_arg (CUMULATIVE_ARGS *orig_cum, const_tree type,
9008 bool named, bool retval)
9010 rtx rvec[FIRST_PSEUDO_REGISTER];
9011 int k = 1, kbase = 1;
9012 HOST_WIDE_INT typesize = int_size_in_bytes (type);
9013 /* This is a copy; modifications are not visible to our caller. */
9014 CUMULATIVE_ARGS copy_cum = *orig_cum;
9015 CUMULATIVE_ARGS *cum = &copy_cum;
9017 /* Pad to 16 byte boundary if needed. */
9018 if (!retval && TYPE_ALIGN (type) >= 2 * BITS_PER_WORD
9019 && (cum->words % 2) != 0)
9020 cum->words++;
9022 cum->intoffset = 0;
9023 cum->use_stack = 0;
9024 cum->named = named;
9026 /* Put entries into rvec[] for individual FP and vector fields, and
9027 for the chunks of memory that go in int regs. Note we start at
9028 element 1; 0 is reserved for an indication of using memory, and
9029 may or may not be filled in below. */
9030 rs6000_darwin64_record_arg_recurse (cum, type, /* startbit pos= */ 0, rvec, &k);
9031 rs6000_darwin64_record_arg_flush (cum, typesize * BITS_PER_UNIT, rvec, &k);
9033 /* If any part of the struct went on the stack put all of it there.
9034 This hack is because the generic code for
9035 FUNCTION_ARG_PARTIAL_NREGS cannot handle cases where the register
9036 parts of the struct are not at the beginning. */
9037 if (cum->use_stack)
9039 if (retval)
9040 return NULL_RTX; /* doesn't go in registers at all */
9041 kbase = 0;
9042 rvec[0] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
9044 if (k > 1 || cum->use_stack)
9045 return gen_rtx_PARALLEL (BLKmode, gen_rtvec_v (k - kbase, &rvec[kbase]));
9046 else
9047 return NULL_RTX;
9050 /* Determine where to place an argument in 64-bit mode with 32-bit ABI. */
9052 static rtx
9053 rs6000_mixed_function_arg (enum machine_mode mode, const_tree type,
9054 int align_words)
9056 int n_units;
9057 int i, k;
9058 rtx rvec[GP_ARG_NUM_REG + 1];
9060 if (align_words >= GP_ARG_NUM_REG)
9061 return NULL_RTX;
9063 n_units = rs6000_arg_size (mode, type);
9065 /* Optimize the simple case where the arg fits in one gpr, except in
9066 the case of BLKmode due to assign_parms assuming that registers are
9067 BITS_PER_WORD wide. */
9068 if (n_units == 0
9069 || (n_units == 1 && mode != BLKmode))
9070 return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
9072 k = 0;
9073 if (align_words + n_units > GP_ARG_NUM_REG)
9074 /* Not all of the arg fits in gprs. Say that it goes in memory too,
9075 using a magic NULL_RTX component.
9076 This is not strictly correct. Only some of the arg belongs in
9077 memory, not all of it. However, the normal scheme using
9078 function_arg_partial_nregs can result in unusual subregs, eg.
9079 (subreg:SI (reg:DF) 4), which are not handled well. The code to
9080 store the whole arg to memory is often more efficient than code
9081 to store pieces, and we know that space is available in the right
9082 place for the whole arg. */
9083 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
9085 i = 0;
9088 rtx r = gen_rtx_REG (SImode, GP_ARG_MIN_REG + align_words);
9089 rtx off = GEN_INT (i++ * 4);
9090 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
9092 while (++align_words < GP_ARG_NUM_REG && --n_units != 0);
9094 return gen_rtx_PARALLEL (mode, gen_rtvec_v (k, rvec));
9097 /* Determine where to put an argument to a function.
9098 Value is zero to push the argument on the stack,
9099 or a hard register in which to store the argument.
9101 MODE is the argument's machine mode.
9102 TYPE is the data type of the argument (as a tree).
9103 This is null for libcalls where that information may
9104 not be available.
9105 CUM is a variable of type CUMULATIVE_ARGS which gives info about
9106 the preceding args and about the function being called. It is
9107 not modified in this routine.
9108 NAMED is nonzero if this argument is a named parameter
9109 (otherwise it is an extra parameter matching an ellipsis).
9111 On RS/6000 the first eight words of non-FP are normally in registers
9112 and the rest are pushed. Under AIX, the first 13 FP args are in registers.
9113 Under V.4, the first 8 FP args are in registers.
9115 If this is floating-point and no prototype is specified, we use
9116 both an FP and integer register (or possibly FP reg and stack). Library
9117 functions (when CALL_LIBCALL is set) always have the proper types for args,
9118 so we can pass the FP value just in one register. emit_library_function
9119 doesn't support PARALLEL anyway.
9121 Note that for args passed by reference, function_arg will be called
9122 with MODE and TYPE set to that of the pointer to the arg, not the arg
9123 itself. */
9125 static rtx
9126 rs6000_function_arg (cumulative_args_t cum_v, enum machine_mode mode,
9127 const_tree type, bool named)
9129 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
9130 enum rs6000_abi abi = DEFAULT_ABI;
9132 /* Return a marker to indicate whether CR1 needs to set or clear the
9133 bit that V.4 uses to say fp args were passed in registers.
9134 Assume that we don't need the marker for software floating point,
9135 or compiler generated library calls. */
9136 if (mode == VOIDmode)
9138 if (abi == ABI_V4
9139 && (cum->call_cookie & CALL_LIBCALL) == 0
9140 && (cum->stdarg
9141 || (cum->nargs_prototype < 0
9142 && (cum->prototype || TARGET_NO_PROTOTYPE))))
9144 /* For the SPE, we need to crxor CR6 always. */
9145 if (TARGET_SPE_ABI)
9146 return GEN_INT (cum->call_cookie | CALL_V4_SET_FP_ARGS);
9147 else if (TARGET_HARD_FLOAT && TARGET_FPRS)
9148 return GEN_INT (cum->call_cookie
9149 | ((cum->fregno == FP_ARG_MIN_REG)
9150 ? CALL_V4_SET_FP_ARGS
9151 : CALL_V4_CLEAR_FP_ARGS));
9154 return GEN_INT (cum->call_cookie & ~CALL_LIBCALL);
9157 if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
9159 rtx rslt = rs6000_darwin64_record_arg (cum, type, named, /*retval= */false);
9160 if (rslt != NULL_RTX)
9161 return rslt;
9162 /* Else fall through to usual handling. */
9165 if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, named))
9166 if (TARGET_64BIT && ! cum->prototype)
9168 /* Vector parameters get passed in vector register
9169 and also in GPRs or memory, in absence of prototype. */
9170 int align_words;
9171 rtx slot;
9172 align_words = (cum->words + 1) & ~1;
9174 if (align_words >= GP_ARG_NUM_REG)
9176 slot = NULL_RTX;
9178 else
9180 slot = gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
9182 return gen_rtx_PARALLEL (mode,
9183 gen_rtvec (2,
9184 gen_rtx_EXPR_LIST (VOIDmode,
9185 slot, const0_rtx),
9186 gen_rtx_EXPR_LIST (VOIDmode,
9187 gen_rtx_REG (mode, cum->vregno),
9188 const0_rtx)));
9190 else
9191 return gen_rtx_REG (mode, cum->vregno);
9192 else if (TARGET_ALTIVEC_ABI
9193 && (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
9194 || (type && TREE_CODE (type) == VECTOR_TYPE
9195 && int_size_in_bytes (type) == 16)))
9197 if (named || abi == ABI_V4)
9198 return NULL_RTX;
9199 else
9201 /* Vector parameters to varargs functions under AIX or Darwin
9202 get passed in memory and possibly also in GPRs. */
9203 int align, align_words, n_words;
9204 enum machine_mode part_mode;
9206 /* Vector parameters must be 16-byte aligned. This places them at
9207 2 mod 4 in terms of words in 32-bit mode, since the parameter
9208 save area starts at offset 24 from the stack. In 64-bit mode,
9209 they just have to start on an even word, since the parameter
9210 save area is 16-byte aligned. */
9211 if (TARGET_32BIT)
9212 align = (2 - cum->words) & 3;
9213 else
9214 align = cum->words & 1;
9215 align_words = cum->words + align;
9217 /* Out of registers? Memory, then. */
9218 if (align_words >= GP_ARG_NUM_REG)
9219 return NULL_RTX;
9221 if (TARGET_32BIT && TARGET_POWERPC64)
9222 return rs6000_mixed_function_arg (mode, type, align_words);
9224 /* The vector value goes in GPRs. Only the part of the
9225 value in GPRs is reported here. */
9226 part_mode = mode;
9227 n_words = rs6000_arg_size (mode, type);
9228 if (align_words + n_words > GP_ARG_NUM_REG)
9229 /* Fortunately, there are only two possibilities, the value
9230 is either wholly in GPRs or half in GPRs and half not. */
9231 part_mode = DImode;
9233 return gen_rtx_REG (part_mode, GP_ARG_MIN_REG + align_words);
9236 else if (TARGET_SPE_ABI && TARGET_SPE
9237 && (SPE_VECTOR_MODE (mode)
9238 || (TARGET_E500_DOUBLE && (mode == DFmode
9239 || mode == DCmode
9240 || mode == TFmode
9241 || mode == TCmode))))
9242 return rs6000_spe_function_arg (cum, mode, type);
9244 else if (abi == ABI_V4)
9246 if (TARGET_HARD_FLOAT && TARGET_FPRS
9247 && ((TARGET_SINGLE_FLOAT && mode == SFmode)
9248 || (TARGET_DOUBLE_FLOAT && mode == DFmode)
9249 || (mode == TFmode && !TARGET_IEEEQUAD)
9250 || mode == SDmode || mode == DDmode || mode == TDmode))
9252 /* _Decimal128 must use an even/odd register pair. This assumes
9253 that the register number is odd when fregno is odd. */
9254 if (mode == TDmode && (cum->fregno % 2) == 1)
9255 cum->fregno++;
9257 if (cum->fregno + (mode == TFmode || mode == TDmode ? 1 : 0)
9258 <= FP_ARG_V4_MAX_REG)
9259 return gen_rtx_REG (mode, cum->fregno);
9260 else
9261 return NULL_RTX;
9263 else
9265 int n_words = rs6000_arg_size (mode, type);
9266 int gregno = cum->sysv_gregno;
9268 /* Long long and SPE vectors are put in (r3,r4), (r5,r6),
9269 (r7,r8) or (r9,r10). As does any other 2 word item such
9270 as complex int due to a historical mistake. */
9271 if (n_words == 2)
9272 gregno += (1 - gregno) & 1;
9274 /* Multi-reg args are not split between registers and stack. */
9275 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
9276 return NULL_RTX;
9278 if (TARGET_32BIT && TARGET_POWERPC64)
9279 return rs6000_mixed_function_arg (mode, type,
9280 gregno - GP_ARG_MIN_REG);
9281 return gen_rtx_REG (mode, gregno);
9284 else
9286 int align_words = rs6000_parm_start (mode, type, cum->words);
9288 /* _Decimal128 must be passed in an even/odd float register pair.
9289 This assumes that the register number is odd when fregno is odd. */
9290 if (mode == TDmode && (cum->fregno % 2) == 1)
9291 cum->fregno++;
9293 if (USE_FP_FOR_ARG_P (cum, mode, type))
9295 rtx rvec[GP_ARG_NUM_REG + 1];
9296 rtx r;
9297 int k;
9298 bool needs_psave;
9299 enum machine_mode fmode = mode;
9300 unsigned long n_fpreg = (GET_MODE_SIZE (mode) + 7) >> 3;
9302 if (cum->fregno + n_fpreg > FP_ARG_MAX_REG + 1)
9304 /* Currently, we only ever need one reg here because complex
9305 doubles are split. */
9306 gcc_assert (cum->fregno == FP_ARG_MAX_REG
9307 && (fmode == TFmode || fmode == TDmode));
9309 /* Long double or _Decimal128 split over regs and memory. */
9310 fmode = DECIMAL_FLOAT_MODE_P (fmode) ? DDmode : DFmode;
9313 /* Do we also need to pass this arg in the parameter save
9314 area? */
9315 needs_psave = (type
9316 && (cum->nargs_prototype <= 0
9317 || (DEFAULT_ABI == ABI_AIX
9318 && TARGET_XL_COMPAT
9319 && align_words >= GP_ARG_NUM_REG)));
9321 if (!needs_psave && mode == fmode)
9322 return gen_rtx_REG (fmode, cum->fregno);
9324 k = 0;
9325 if (needs_psave)
9327 /* Describe the part that goes in gprs or the stack.
9328 This piece must come first, before the fprs. */
9329 if (align_words < GP_ARG_NUM_REG)
9331 unsigned long n_words = rs6000_arg_size (mode, type);
9333 if (align_words + n_words > GP_ARG_NUM_REG
9334 || (TARGET_32BIT && TARGET_POWERPC64))
9336 /* If this is partially on the stack, then we only
9337 include the portion actually in registers here. */
9338 enum machine_mode rmode = TARGET_32BIT ? SImode : DImode;
9339 rtx off;
9340 int i = 0;
9341 if (align_words + n_words > GP_ARG_NUM_REG)
9342 /* Not all of the arg fits in gprs. Say that it
9343 goes in memory too, using a magic NULL_RTX
9344 component. Also see comment in
9345 rs6000_mixed_function_arg for why the normal
9346 function_arg_partial_nregs scheme doesn't work
9347 in this case. */
9348 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX,
9349 const0_rtx);
9352 r = gen_rtx_REG (rmode,
9353 GP_ARG_MIN_REG + align_words);
9354 off = GEN_INT (i++ * GET_MODE_SIZE (rmode));
9355 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
9357 while (++align_words < GP_ARG_NUM_REG && --n_words != 0);
9359 else
9361 /* The whole arg fits in gprs. */
9362 r = gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
9363 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, const0_rtx);
9366 else
9367 /* It's entirely in memory. */
9368 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
9371 /* Describe where this piece goes in the fprs. */
9372 r = gen_rtx_REG (fmode, cum->fregno);
9373 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, const0_rtx);
9375 return gen_rtx_PARALLEL (mode, gen_rtvec_v (k, rvec));
9377 else if (align_words < GP_ARG_NUM_REG)
9379 if (TARGET_32BIT && TARGET_POWERPC64)
9380 return rs6000_mixed_function_arg (mode, type, align_words);
9382 if (mode == BLKmode)
9383 mode = Pmode;
9385 return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
9387 else
9388 return NULL_RTX;
9392 /* For an arg passed partly in registers and partly in memory, this is
9393 the number of bytes passed in registers. For args passed entirely in
9394 registers or entirely in memory, zero. When an arg is described by a
9395 PARALLEL, perhaps using more than one register type, this function
9396 returns the number of bytes used by the first element of the PARALLEL. */
9398 static int
9399 rs6000_arg_partial_bytes (cumulative_args_t cum_v, enum machine_mode mode,
9400 tree type, bool named)
9402 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
9403 int ret = 0;
9404 int align_words;
9406 if (DEFAULT_ABI == ABI_V4)
9407 return 0;
9409 if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, named)
9410 && cum->nargs_prototype >= 0)
9411 return 0;
9413 /* In this complicated case we just disable the partial_nregs code. */
9414 if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
9415 return 0;
9417 align_words = rs6000_parm_start (mode, type, cum->words);
9419 if (USE_FP_FOR_ARG_P (cum, mode, type))
9421 /* If we are passing this arg in the fixed parameter save area
9422 (gprs or memory) as well as fprs, then this function should
9423 return the number of partial bytes passed in the parameter
9424 save area rather than partial bytes passed in fprs. */
9425 if (type
9426 && (cum->nargs_prototype <= 0
9427 || (DEFAULT_ABI == ABI_AIX
9428 && TARGET_XL_COMPAT
9429 && align_words >= GP_ARG_NUM_REG)))
9430 return 0;
9431 else if (cum->fregno + ((GET_MODE_SIZE (mode) + 7) >> 3)
9432 > FP_ARG_MAX_REG + 1)
9433 ret = (FP_ARG_MAX_REG + 1 - cum->fregno) * 8;
9434 else if (cum->nargs_prototype >= 0)
9435 return 0;
9438 if (align_words < GP_ARG_NUM_REG
9439 && GP_ARG_NUM_REG < align_words + rs6000_arg_size (mode, type))
9440 ret = (GP_ARG_NUM_REG - align_words) * (TARGET_32BIT ? 4 : 8);
9442 if (ret != 0 && TARGET_DEBUG_ARG)
9443 fprintf (stderr, "rs6000_arg_partial_bytes: %d\n", ret);
9445 return ret;
9448 /* A C expression that indicates when an argument must be passed by
9449 reference. If nonzero for an argument, a copy of that argument is
9450 made in memory and a pointer to the argument is passed instead of
9451 the argument itself. The pointer is passed in whatever way is
9452 appropriate for passing a pointer to that type.
9454 Under V.4, aggregates and long double are passed by reference.
9456 As an extension to all 32-bit ABIs, AltiVec vectors are passed by
9457 reference unless the AltiVec vector extension ABI is in force.
9459 As an extension to all ABIs, variable sized types are passed by
9460 reference. */
9462 static bool
9463 rs6000_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED,
9464 enum machine_mode mode, const_tree type,
9465 bool named ATTRIBUTE_UNUSED)
9467 if (DEFAULT_ABI == ABI_V4 && TARGET_IEEEQUAD && mode == TFmode)
9469 if (TARGET_DEBUG_ARG)
9470 fprintf (stderr, "function_arg_pass_by_reference: V4 long double\n");
9471 return 1;
9474 if (!type)
9475 return 0;
9477 if (DEFAULT_ABI == ABI_V4 && AGGREGATE_TYPE_P (type))
9479 if (TARGET_DEBUG_ARG)
9480 fprintf (stderr, "function_arg_pass_by_reference: V4 aggregate\n");
9481 return 1;
9484 if (int_size_in_bytes (type) < 0)
9486 if (TARGET_DEBUG_ARG)
9487 fprintf (stderr, "function_arg_pass_by_reference: variable size\n");
9488 return 1;
9491 /* Allow -maltivec -mabi=no-altivec without warning. Altivec vector
9492 modes only exist for GCC vector types if -maltivec. */
9493 if (TARGET_32BIT && !TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
9495 if (TARGET_DEBUG_ARG)
9496 fprintf (stderr, "function_arg_pass_by_reference: AltiVec\n");
9497 return 1;
9500 /* Pass synthetic vectors in memory. */
9501 if (TREE_CODE (type) == VECTOR_TYPE
9502 && int_size_in_bytes (type) > (TARGET_ALTIVEC_ABI ? 16 : 8))
9504 static bool warned_for_pass_big_vectors = false;
9505 if (TARGET_DEBUG_ARG)
9506 fprintf (stderr, "function_arg_pass_by_reference: synthetic vector\n");
9507 if (!warned_for_pass_big_vectors)
9509 warning (0, "GCC vector passed by reference: "
9510 "non-standard ABI extension with no compatibility guarantee");
9511 warned_for_pass_big_vectors = true;
9513 return 1;
9516 return 0;
9519 static void
9520 rs6000_move_block_from_reg (int regno, rtx x, int nregs)
9522 int i;
9523 enum machine_mode reg_mode = TARGET_32BIT ? SImode : DImode;
9525 if (nregs == 0)
9526 return;
9528 for (i = 0; i < nregs; i++)
9530 rtx tem = adjust_address_nv (x, reg_mode, i * GET_MODE_SIZE (reg_mode));
9531 if (reload_completed)
9533 if (! strict_memory_address_p (reg_mode, XEXP (tem, 0)))
9534 tem = NULL_RTX;
9535 else
9536 tem = simplify_gen_subreg (reg_mode, x, BLKmode,
9537 i * GET_MODE_SIZE (reg_mode));
9539 else
9540 tem = replace_equiv_address (tem, XEXP (tem, 0));
9542 gcc_assert (tem);
9544 emit_move_insn (tem, gen_rtx_REG (reg_mode, regno + i));
9548 /* Perform any needed actions needed for a function that is receiving a
9549 variable number of arguments.
9551 CUM is as above.
9553 MODE and TYPE are the mode and type of the current parameter.
9555 PRETEND_SIZE is a variable that should be set to the amount of stack
9556 that must be pushed by the prolog to pretend that our caller pushed
9559 Normally, this macro will push all remaining incoming registers on the
9560 stack and set PRETEND_SIZE to the length of the registers pushed. */
9562 static void
9563 setup_incoming_varargs (cumulative_args_t cum, enum machine_mode mode,
9564 tree type, int *pretend_size ATTRIBUTE_UNUSED,
9565 int no_rtl)
9567 CUMULATIVE_ARGS next_cum;
9568 int reg_size = TARGET_32BIT ? 4 : 8;
9569 rtx save_area = NULL_RTX, mem;
9570 int first_reg_offset;
9571 alias_set_type set;
9573 /* Skip the last named argument. */
9574 next_cum = *get_cumulative_args (cum);
9575 rs6000_function_arg_advance_1 (&next_cum, mode, type, true, 0);
9577 if (DEFAULT_ABI == ABI_V4)
9579 first_reg_offset = next_cum.sysv_gregno - GP_ARG_MIN_REG;
9581 if (! no_rtl)
9583 int gpr_reg_num = 0, gpr_size = 0, fpr_size = 0;
9584 HOST_WIDE_INT offset = 0;
9586 /* Try to optimize the size of the varargs save area.
9587 The ABI requires that ap.reg_save_area is doubleword
9588 aligned, but we don't need to allocate space for all
9589 the bytes, only those to which we actually will save
9590 anything. */
9591 if (cfun->va_list_gpr_size && first_reg_offset < GP_ARG_NUM_REG)
9592 gpr_reg_num = GP_ARG_NUM_REG - first_reg_offset;
9593 if (TARGET_HARD_FLOAT && TARGET_FPRS
9594 && next_cum.fregno <= FP_ARG_V4_MAX_REG
9595 && cfun->va_list_fpr_size)
9597 if (gpr_reg_num)
9598 fpr_size = (next_cum.fregno - FP_ARG_MIN_REG)
9599 * UNITS_PER_FP_WORD;
9600 if (cfun->va_list_fpr_size
9601 < FP_ARG_V4_MAX_REG + 1 - next_cum.fregno)
9602 fpr_size += cfun->va_list_fpr_size * UNITS_PER_FP_WORD;
9603 else
9604 fpr_size += (FP_ARG_V4_MAX_REG + 1 - next_cum.fregno)
9605 * UNITS_PER_FP_WORD;
9607 if (gpr_reg_num)
9609 offset = -((first_reg_offset * reg_size) & ~7);
9610 if (!fpr_size && gpr_reg_num > cfun->va_list_gpr_size)
9612 gpr_reg_num = cfun->va_list_gpr_size;
9613 if (reg_size == 4 && (first_reg_offset & 1))
9614 gpr_reg_num++;
9616 gpr_size = (gpr_reg_num * reg_size + 7) & ~7;
9618 else if (fpr_size)
9619 offset = - (int) (next_cum.fregno - FP_ARG_MIN_REG)
9620 * UNITS_PER_FP_WORD
9621 - (int) (GP_ARG_NUM_REG * reg_size);
9623 if (gpr_size + fpr_size)
9625 rtx reg_save_area
9626 = assign_stack_local (BLKmode, gpr_size + fpr_size, 64);
9627 gcc_assert (GET_CODE (reg_save_area) == MEM);
9628 reg_save_area = XEXP (reg_save_area, 0);
9629 if (GET_CODE (reg_save_area) == PLUS)
9631 gcc_assert (XEXP (reg_save_area, 0)
9632 == virtual_stack_vars_rtx);
9633 gcc_assert (GET_CODE (XEXP (reg_save_area, 1)) == CONST_INT);
9634 offset += INTVAL (XEXP (reg_save_area, 1));
9636 else
9637 gcc_assert (reg_save_area == virtual_stack_vars_rtx);
9640 cfun->machine->varargs_save_offset = offset;
9641 save_area = plus_constant (Pmode, virtual_stack_vars_rtx, offset);
9644 else
9646 first_reg_offset = next_cum.words;
9647 save_area = virtual_incoming_args_rtx;
9649 if (targetm.calls.must_pass_in_stack (mode, type))
9650 first_reg_offset += rs6000_arg_size (TYPE_MODE (type), type);
9653 set = get_varargs_alias_set ();
9654 if (! no_rtl && first_reg_offset < GP_ARG_NUM_REG
9655 && cfun->va_list_gpr_size)
9657 int n_gpr, nregs = GP_ARG_NUM_REG - first_reg_offset;
9659 if (va_list_gpr_counter_field)
9660 /* V4 va_list_gpr_size counts number of registers needed. */
9661 n_gpr = cfun->va_list_gpr_size;
9662 else
9663 /* char * va_list instead counts number of bytes needed. */
9664 n_gpr = (cfun->va_list_gpr_size + reg_size - 1) / reg_size;
9666 if (nregs > n_gpr)
9667 nregs = n_gpr;
9669 mem = gen_rtx_MEM (BLKmode,
9670 plus_constant (Pmode, save_area,
9671 first_reg_offset * reg_size));
9672 MEM_NOTRAP_P (mem) = 1;
9673 set_mem_alias_set (mem, set);
9674 set_mem_align (mem, BITS_PER_WORD);
9676 rs6000_move_block_from_reg (GP_ARG_MIN_REG + first_reg_offset, mem,
9677 nregs);
9680 /* Save FP registers if needed. */
9681 if (DEFAULT_ABI == ABI_V4
9682 && TARGET_HARD_FLOAT && TARGET_FPRS
9683 && ! no_rtl
9684 && next_cum.fregno <= FP_ARG_V4_MAX_REG
9685 && cfun->va_list_fpr_size)
9687 int fregno = next_cum.fregno, nregs;
9688 rtx cr1 = gen_rtx_REG (CCmode, CR1_REGNO);
9689 rtx lab = gen_label_rtx ();
9690 int off = (GP_ARG_NUM_REG * reg_size) + ((fregno - FP_ARG_MIN_REG)
9691 * UNITS_PER_FP_WORD);
9693 emit_jump_insn
9694 (gen_rtx_SET (VOIDmode,
9695 pc_rtx,
9696 gen_rtx_IF_THEN_ELSE (VOIDmode,
9697 gen_rtx_NE (VOIDmode, cr1,
9698 const0_rtx),
9699 gen_rtx_LABEL_REF (VOIDmode, lab),
9700 pc_rtx)));
9702 for (nregs = 0;
9703 fregno <= FP_ARG_V4_MAX_REG && nregs < cfun->va_list_fpr_size;
9704 fregno++, off += UNITS_PER_FP_WORD, nregs++)
9706 mem = gen_rtx_MEM ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
9707 ? DFmode : SFmode,
9708 plus_constant (Pmode, save_area, off));
9709 MEM_NOTRAP_P (mem) = 1;
9710 set_mem_alias_set (mem, set);
9711 set_mem_align (mem, GET_MODE_ALIGNMENT (
9712 (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
9713 ? DFmode : SFmode));
9714 emit_move_insn (mem, gen_rtx_REG (
9715 (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
9716 ? DFmode : SFmode, fregno));
9719 emit_label (lab);
9723 /* Create the va_list data type. */
9725 static tree
9726 rs6000_build_builtin_va_list (void)
9728 tree f_gpr, f_fpr, f_res, f_ovf, f_sav, record, type_decl;
9730 /* For AIX, prefer 'char *' because that's what the system
9731 header files like. */
9732 if (DEFAULT_ABI != ABI_V4)
9733 return build_pointer_type (char_type_node);
9735 record = (*lang_hooks.types.make_type) (RECORD_TYPE);
9736 type_decl = build_decl (BUILTINS_LOCATION, TYPE_DECL,
9737 get_identifier ("__va_list_tag"), record);
9739 f_gpr = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("gpr"),
9740 unsigned_char_type_node);
9741 f_fpr = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("fpr"),
9742 unsigned_char_type_node);
9743 /* Give the two bytes of padding a name, so that -Wpadded won't warn on
9744 every user file. */
9745 f_res = build_decl (BUILTINS_LOCATION, FIELD_DECL,
9746 get_identifier ("reserved"), short_unsigned_type_node);
9747 f_ovf = build_decl (BUILTINS_LOCATION, FIELD_DECL,
9748 get_identifier ("overflow_arg_area"),
9749 ptr_type_node);
9750 f_sav = build_decl (BUILTINS_LOCATION, FIELD_DECL,
9751 get_identifier ("reg_save_area"),
9752 ptr_type_node);
9754 va_list_gpr_counter_field = f_gpr;
9755 va_list_fpr_counter_field = f_fpr;
9757 DECL_FIELD_CONTEXT (f_gpr) = record;
9758 DECL_FIELD_CONTEXT (f_fpr) = record;
9759 DECL_FIELD_CONTEXT (f_res) = record;
9760 DECL_FIELD_CONTEXT (f_ovf) = record;
9761 DECL_FIELD_CONTEXT (f_sav) = record;
9763 TYPE_STUB_DECL (record) = type_decl;
9764 TYPE_NAME (record) = type_decl;
9765 TYPE_FIELDS (record) = f_gpr;
9766 DECL_CHAIN (f_gpr) = f_fpr;
9767 DECL_CHAIN (f_fpr) = f_res;
9768 DECL_CHAIN (f_res) = f_ovf;
9769 DECL_CHAIN (f_ovf) = f_sav;
9771 layout_type (record);
9773 /* The correct type is an array type of one element. */
9774 return build_array_type (record, build_index_type (size_zero_node));
9777 /* Implement va_start. */
9779 static void
9780 rs6000_va_start (tree valist, rtx nextarg)
9782 HOST_WIDE_INT words, n_gpr, n_fpr;
9783 tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
9784 tree gpr, fpr, ovf, sav, t;
9786 /* Only SVR4 needs something special. */
9787 if (DEFAULT_ABI != ABI_V4)
9789 std_expand_builtin_va_start (valist, nextarg);
9790 return;
9793 f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
9794 f_fpr = DECL_CHAIN (f_gpr);
9795 f_res = DECL_CHAIN (f_fpr);
9796 f_ovf = DECL_CHAIN (f_res);
9797 f_sav = DECL_CHAIN (f_ovf);
9799 valist = build_simple_mem_ref (valist);
9800 gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
9801 fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
9802 f_fpr, NULL_TREE);
9803 ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
9804 f_ovf, NULL_TREE);
9805 sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
9806 f_sav, NULL_TREE);
9808 /* Count number of gp and fp argument registers used. */
9809 words = crtl->args.info.words;
9810 n_gpr = MIN (crtl->args.info.sysv_gregno - GP_ARG_MIN_REG,
9811 GP_ARG_NUM_REG);
9812 n_fpr = MIN (crtl->args.info.fregno - FP_ARG_MIN_REG,
9813 FP_ARG_NUM_REG);
9815 if (TARGET_DEBUG_ARG)
9816 fprintf (stderr, "va_start: words = "HOST_WIDE_INT_PRINT_DEC", n_gpr = "
9817 HOST_WIDE_INT_PRINT_DEC", n_fpr = "HOST_WIDE_INT_PRINT_DEC"\n",
9818 words, n_gpr, n_fpr);
9820 if (cfun->va_list_gpr_size)
9822 t = build2 (MODIFY_EXPR, TREE_TYPE (gpr), gpr,
9823 build_int_cst (NULL_TREE, n_gpr));
9824 TREE_SIDE_EFFECTS (t) = 1;
9825 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
9828 if (cfun->va_list_fpr_size)
9830 t = build2 (MODIFY_EXPR, TREE_TYPE (fpr), fpr,
9831 build_int_cst (NULL_TREE, n_fpr));
9832 TREE_SIDE_EFFECTS (t) = 1;
9833 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
9835 #ifdef HAVE_AS_GNU_ATTRIBUTE
9836 if (call_ABI_of_interest (cfun->decl))
9837 rs6000_passes_float = true;
9838 #endif
9841 /* Find the overflow area. */
9842 t = make_tree (TREE_TYPE (ovf), virtual_incoming_args_rtx);
9843 if (words != 0)
9844 t = fold_build_pointer_plus_hwi (t, words * UNITS_PER_WORD);
9845 t = build2 (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
9846 TREE_SIDE_EFFECTS (t) = 1;
9847 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
9849 /* If there were no va_arg invocations, don't set up the register
9850 save area. */
9851 if (!cfun->va_list_gpr_size
9852 && !cfun->va_list_fpr_size
9853 && n_gpr < GP_ARG_NUM_REG
9854 && n_fpr < FP_ARG_V4_MAX_REG)
9855 return;
9857 /* Find the register save area. */
9858 t = make_tree (TREE_TYPE (sav), virtual_stack_vars_rtx);
9859 if (cfun->machine->varargs_save_offset)
9860 t = fold_build_pointer_plus_hwi (t, cfun->machine->varargs_save_offset);
9861 t = build2 (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
9862 TREE_SIDE_EFFECTS (t) = 1;
9863 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
9866 /* Implement va_arg. */
9868 static tree
9869 rs6000_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p,
9870 gimple_seq *post_p)
9872 tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
9873 tree gpr, fpr, ovf, sav, reg, t, u;
9874 int size, rsize, n_reg, sav_ofs, sav_scale;
9875 tree lab_false, lab_over, addr;
9876 int align;
9877 tree ptrtype = build_pointer_type_for_mode (type, ptr_mode, true);
9878 int regalign = 0;
9879 gimple stmt;
9881 if (pass_by_reference (NULL, TYPE_MODE (type), type, false))
9883 t = rs6000_gimplify_va_arg (valist, ptrtype, pre_p, post_p);
9884 return build_va_arg_indirect_ref (t);
9887 /* We need to deal with the fact that the darwin ppc64 ABI is defined by an
9888 earlier version of gcc, with the property that it always applied alignment
9889 adjustments to the va-args (even for zero-sized types). The cheapest way
9890 to deal with this is to replicate the effect of the part of
9891 std_gimplify_va_arg_expr that carries out the align adjust, for the case
9892 of relevance.
9893 We don't need to check for pass-by-reference because of the test above.
9894 We can return a simplifed answer, since we know there's no offset to add. */
9896 if (TARGET_MACHO
9897 && rs6000_darwin64_abi
9898 && integer_zerop (TYPE_SIZE (type)))
9900 unsigned HOST_WIDE_INT align, boundary;
9901 tree valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
9902 align = PARM_BOUNDARY / BITS_PER_UNIT;
9903 boundary = rs6000_function_arg_boundary (TYPE_MODE (type), type);
9904 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
9905 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
9906 boundary /= BITS_PER_UNIT;
9907 if (boundary > align)
9909 tree t ;
9910 /* This updates arg ptr by the amount that would be necessary
9911 to align the zero-sized (but not zero-alignment) item. */
9912 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
9913 fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
9914 gimplify_and_add (t, pre_p);
9916 t = fold_convert (sizetype, valist_tmp);
9917 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
9918 fold_convert (TREE_TYPE (valist),
9919 fold_build2 (BIT_AND_EXPR, sizetype, t,
9920 size_int (-boundary))));
9921 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
9922 gimplify_and_add (t, pre_p);
9924 /* Since it is zero-sized there's no increment for the item itself. */
9925 valist_tmp = fold_convert (build_pointer_type (type), valist_tmp);
9926 return build_va_arg_indirect_ref (valist_tmp);
9929 if (DEFAULT_ABI != ABI_V4)
9931 if (targetm.calls.split_complex_arg && TREE_CODE (type) == COMPLEX_TYPE)
9933 tree elem_type = TREE_TYPE (type);
9934 enum machine_mode elem_mode = TYPE_MODE (elem_type);
9935 int elem_size = GET_MODE_SIZE (elem_mode);
9937 if (elem_size < UNITS_PER_WORD)
9939 tree real_part, imag_part;
9940 gimple_seq post = NULL;
9942 real_part = rs6000_gimplify_va_arg (valist, elem_type, pre_p,
9943 &post);
9944 /* Copy the value into a temporary, lest the formal temporary
9945 be reused out from under us. */
9946 real_part = get_initialized_tmp_var (real_part, pre_p, &post);
9947 gimple_seq_add_seq (pre_p, post);
9949 imag_part = rs6000_gimplify_va_arg (valist, elem_type, pre_p,
9950 post_p);
9952 return build2 (COMPLEX_EXPR, type, real_part, imag_part);
9956 return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
9959 f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
9960 f_fpr = DECL_CHAIN (f_gpr);
9961 f_res = DECL_CHAIN (f_fpr);
9962 f_ovf = DECL_CHAIN (f_res);
9963 f_sav = DECL_CHAIN (f_ovf);
9965 valist = build_va_arg_indirect_ref (valist);
9966 gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
9967 fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
9968 f_fpr, NULL_TREE);
9969 ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
9970 f_ovf, NULL_TREE);
9971 sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
9972 f_sav, NULL_TREE);
9974 size = int_size_in_bytes (type);
9975 rsize = (size + 3) / 4;
9976 align = 1;
9978 if (TARGET_HARD_FLOAT && TARGET_FPRS
9979 && ((TARGET_SINGLE_FLOAT && TYPE_MODE (type) == SFmode)
9980 || (TARGET_DOUBLE_FLOAT
9981 && (TYPE_MODE (type) == DFmode
9982 || TYPE_MODE (type) == TFmode
9983 || TYPE_MODE (type) == SDmode
9984 || TYPE_MODE (type) == DDmode
9985 || TYPE_MODE (type) == TDmode))))
9987 /* FP args go in FP registers, if present. */
9988 reg = fpr;
9989 n_reg = (size + 7) / 8;
9990 sav_ofs = ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? 8 : 4) * 4;
9991 sav_scale = ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? 8 : 4);
9992 if (TYPE_MODE (type) != SFmode && TYPE_MODE (type) != SDmode)
9993 align = 8;
9995 else
9997 /* Otherwise into GP registers. */
9998 reg = gpr;
9999 n_reg = rsize;
10000 sav_ofs = 0;
10001 sav_scale = 4;
10002 if (n_reg == 2)
10003 align = 8;
10006 /* Pull the value out of the saved registers.... */
10008 lab_over = NULL;
10009 addr = create_tmp_var (ptr_type_node, "addr");
10011 /* AltiVec vectors never go in registers when -mabi=altivec. */
10012 if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (TYPE_MODE (type)))
10013 align = 16;
10014 else
10016 lab_false = create_artificial_label (input_location);
10017 lab_over = create_artificial_label (input_location);
10019 /* Long long and SPE vectors are aligned in the registers.
10020 As are any other 2 gpr item such as complex int due to a
10021 historical mistake. */
10022 u = reg;
10023 if (n_reg == 2 && reg == gpr)
10025 regalign = 1;
10026 u = build2 (BIT_AND_EXPR, TREE_TYPE (reg), unshare_expr (reg),
10027 build_int_cst (TREE_TYPE (reg), n_reg - 1));
10028 u = build2 (POSTINCREMENT_EXPR, TREE_TYPE (reg),
10029 unshare_expr (reg), u);
10031 /* _Decimal128 is passed in even/odd fpr pairs; the stored
10032 reg number is 0 for f1, so we want to make it odd. */
10033 else if (reg == fpr && TYPE_MODE (type) == TDmode)
10035 t = build2 (BIT_IOR_EXPR, TREE_TYPE (reg), unshare_expr (reg),
10036 build_int_cst (TREE_TYPE (reg), 1));
10037 u = build2 (MODIFY_EXPR, void_type_node, unshare_expr (reg), t);
10040 t = fold_convert (TREE_TYPE (reg), size_int (8 - n_reg + 1));
10041 t = build2 (GE_EXPR, boolean_type_node, u, t);
10042 u = build1 (GOTO_EXPR, void_type_node, lab_false);
10043 t = build3 (COND_EXPR, void_type_node, t, u, NULL_TREE);
10044 gimplify_and_add (t, pre_p);
10046 t = sav;
10047 if (sav_ofs)
10048 t = fold_build_pointer_plus_hwi (sav, sav_ofs);
10050 u = build2 (POSTINCREMENT_EXPR, TREE_TYPE (reg), unshare_expr (reg),
10051 build_int_cst (TREE_TYPE (reg), n_reg));
10052 u = fold_convert (sizetype, u);
10053 u = build2 (MULT_EXPR, sizetype, u, size_int (sav_scale));
10054 t = fold_build_pointer_plus (t, u);
10056 /* _Decimal32 varargs are located in the second word of the 64-bit
10057 FP register for 32-bit binaries. */
10058 if (!TARGET_POWERPC64
10059 && TARGET_HARD_FLOAT && TARGET_FPRS
10060 && TYPE_MODE (type) == SDmode)
10061 t = fold_build_pointer_plus_hwi (t, size);
10063 gimplify_assign (addr, t, pre_p);
10065 gimple_seq_add_stmt (pre_p, gimple_build_goto (lab_over));
10067 stmt = gimple_build_label (lab_false);
10068 gimple_seq_add_stmt (pre_p, stmt);
10070 if ((n_reg == 2 && !regalign) || n_reg > 2)
10072 /* Ensure that we don't find any more args in regs.
10073 Alignment has taken care of for special cases. */
10074 gimplify_assign (reg, build_int_cst (TREE_TYPE (reg), 8), pre_p);
10078 /* ... otherwise out of the overflow area. */
10080 /* Care for on-stack alignment if needed. */
10081 t = ovf;
10082 if (align != 1)
10084 t = fold_build_pointer_plus_hwi (t, align - 1);
10085 t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t,
10086 build_int_cst (TREE_TYPE (t), -align));
10088 gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
10090 gimplify_assign (unshare_expr (addr), t, pre_p);
10092 t = fold_build_pointer_plus_hwi (t, size);
10093 gimplify_assign (unshare_expr (ovf), t, pre_p);
10095 if (lab_over)
10097 stmt = gimple_build_label (lab_over);
10098 gimple_seq_add_stmt (pre_p, stmt);
10101 if (STRICT_ALIGNMENT
10102 && (TYPE_ALIGN (type)
10103 > (unsigned) BITS_PER_UNIT * (align < 4 ? 4 : align)))
10105 /* The value (of type complex double, for example) may not be
10106 aligned in memory in the saved registers, so copy via a
10107 temporary. (This is the same code as used for SPARC.) */
10108 tree tmp = create_tmp_var (type, "va_arg_tmp");
10109 tree dest_addr = build_fold_addr_expr (tmp);
10111 tree copy = build_call_expr (builtin_decl_implicit (BUILT_IN_MEMCPY),
10112 3, dest_addr, addr, size_int (rsize * 4));
10114 gimplify_and_add (copy, pre_p);
10115 addr = dest_addr;
10118 addr = fold_convert (ptrtype, addr);
10119 return build_va_arg_indirect_ref (addr);
10122 /* Builtins. */
10124 static void
10125 def_builtin (const char *name, tree type, enum rs6000_builtins code)
10127 tree t;
10128 unsigned classify = rs6000_builtin_info[(int)code].attr;
10129 const char *attr_string = "";
10131 gcc_assert (name != NULL);
10132 gcc_assert (IN_RANGE ((int)code, 0, (int)RS6000_BUILTIN_COUNT));
10134 if (rs6000_builtin_decls[(int)code])
10135 fatal_error ("internal error: builtin function %s already processed", name);
10137 rs6000_builtin_decls[(int)code] = t =
10138 add_builtin_function (name, type, (int)code, BUILT_IN_MD, NULL, NULL_TREE);
10140 /* Set any special attributes. */
10141 if ((classify & RS6000_BTC_CONST) != 0)
10143 /* const function, function only depends on the inputs. */
10144 TREE_READONLY (t) = 1;
10145 TREE_NOTHROW (t) = 1;
10146 attr_string = ", pure";
10148 else if ((classify & RS6000_BTC_PURE) != 0)
10150 /* pure function, function can read global memory, but does not set any
10151 external state. */
10152 DECL_PURE_P (t) = 1;
10153 TREE_NOTHROW (t) = 1;
10154 attr_string = ", const";
10156 else if ((classify & RS6000_BTC_FP) != 0)
10158 /* Function is a math function. If rounding mode is on, then treat the
10159 function as not reading global memory, but it can have arbitrary side
10160 effects. If it is off, then assume the function is a const function.
10161 This mimics the ATTR_MATHFN_FPROUNDING attribute in
10162 builtin-attribute.def that is used for the math functions. */
10163 TREE_NOTHROW (t) = 1;
10164 if (flag_rounding_math)
10166 DECL_PURE_P (t) = 1;
10167 DECL_IS_NOVOPS (t) = 1;
10168 attr_string = ", fp, pure";
10170 else
10172 TREE_READONLY (t) = 1;
10173 attr_string = ", fp, const";
10176 else if ((classify & RS6000_BTC_ATTR_MASK) != 0)
10177 gcc_unreachable ();
10179 if (TARGET_DEBUG_BUILTIN)
10180 fprintf (stderr, "rs6000_builtin, code = %4d, %s%s\n",
10181 (int)code, name, attr_string);
10184 /* Simple ternary operations: VECd = foo (VECa, VECb, VECc). */
10186 #undef RS6000_BUILTIN_1
10187 #undef RS6000_BUILTIN_2
10188 #undef RS6000_BUILTIN_3
10189 #undef RS6000_BUILTIN_A
10190 #undef RS6000_BUILTIN_D
10191 #undef RS6000_BUILTIN_E
10192 #undef RS6000_BUILTIN_H
10193 #undef RS6000_BUILTIN_P
10194 #undef RS6000_BUILTIN_Q
10195 #undef RS6000_BUILTIN_S
10196 #undef RS6000_BUILTIN_X
10198 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
10199 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
10200 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE) \
10201 { MASK, ICODE, NAME, ENUM },
10203 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
10204 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
10205 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
10206 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
10207 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
10208 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
10209 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
10210 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
10212 static const struct builtin_description bdesc_3arg[] =
10214 #include "rs6000-builtin.def"
10217 /* DST operations: void foo (void *, const int, const char). */
10219 #undef RS6000_BUILTIN_1
10220 #undef RS6000_BUILTIN_2
10221 #undef RS6000_BUILTIN_3
10222 #undef RS6000_BUILTIN_A
10223 #undef RS6000_BUILTIN_D
10224 #undef RS6000_BUILTIN_E
10225 #undef RS6000_BUILTIN_H
10226 #undef RS6000_BUILTIN_P
10227 #undef RS6000_BUILTIN_Q
10228 #undef RS6000_BUILTIN_S
10229 #undef RS6000_BUILTIN_X
10231 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
10232 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
10233 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
10234 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
10235 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE) \
10236 { MASK, ICODE, NAME, ENUM },
10238 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
10239 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
10240 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
10241 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
10242 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
10243 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
10245 static const struct builtin_description bdesc_dst[] =
10247 #include "rs6000-builtin.def"
10250 /* Simple binary operations: VECc = foo (VECa, VECb). */
10252 #undef RS6000_BUILTIN_1
10253 #undef RS6000_BUILTIN_2
10254 #undef RS6000_BUILTIN_3
10255 #undef RS6000_BUILTIN_A
10256 #undef RS6000_BUILTIN_D
10257 #undef RS6000_BUILTIN_E
10258 #undef RS6000_BUILTIN_H
10259 #undef RS6000_BUILTIN_P
10260 #undef RS6000_BUILTIN_Q
10261 #undef RS6000_BUILTIN_S
10262 #undef RS6000_BUILTIN_X
10264 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
10265 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE) \
10266 { MASK, ICODE, NAME, ENUM },
10268 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
10269 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
10270 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
10271 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
10272 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
10273 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
10274 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
10275 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
10276 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
10278 static const struct builtin_description bdesc_2arg[] =
10280 #include "rs6000-builtin.def"
10283 #undef RS6000_BUILTIN_1
10284 #undef RS6000_BUILTIN_2
10285 #undef RS6000_BUILTIN_3
10286 #undef RS6000_BUILTIN_A
10287 #undef RS6000_BUILTIN_D
10288 #undef RS6000_BUILTIN_E
10289 #undef RS6000_BUILTIN_H
10290 #undef RS6000_BUILTIN_P
10291 #undef RS6000_BUILTIN_Q
10292 #undef RS6000_BUILTIN_S
10293 #undef RS6000_BUILTIN_X
10295 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
10296 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
10297 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
10298 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
10299 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
10300 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
10301 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
10302 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE) \
10303 { MASK, ICODE, NAME, ENUM },
10305 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
10306 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
10307 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
10309 /* AltiVec predicates. */
10311 static const struct builtin_description bdesc_altivec_preds[] =
10313 #include "rs6000-builtin.def"
10316 /* SPE predicates. */
10317 #undef RS6000_BUILTIN_1
10318 #undef RS6000_BUILTIN_2
10319 #undef RS6000_BUILTIN_3
10320 #undef RS6000_BUILTIN_A
10321 #undef RS6000_BUILTIN_D
10322 #undef RS6000_BUILTIN_E
10323 #undef RS6000_BUILTIN_H
10324 #undef RS6000_BUILTIN_P
10325 #undef RS6000_BUILTIN_Q
10326 #undef RS6000_BUILTIN_S
10327 #undef RS6000_BUILTIN_X
10329 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
10330 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
10331 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
10332 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
10333 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
10334 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
10335 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
10336 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
10337 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
10338 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE) \
10339 { MASK, ICODE, NAME, ENUM },
10341 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
10343 static const struct builtin_description bdesc_spe_predicates[] =
10345 #include "rs6000-builtin.def"
10348 /* SPE evsel predicates. */
10349 #undef RS6000_BUILTIN_1
10350 #undef RS6000_BUILTIN_2
10351 #undef RS6000_BUILTIN_3
10352 #undef RS6000_BUILTIN_A
10353 #undef RS6000_BUILTIN_D
10354 #undef RS6000_BUILTIN_E
10355 #undef RS6000_BUILTIN_H
10356 #undef RS6000_BUILTIN_P
10357 #undef RS6000_BUILTIN_Q
10358 #undef RS6000_BUILTIN_S
10359 #undef RS6000_BUILTIN_X
10361 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
10362 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
10363 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
10364 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
10365 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
10366 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE) \
10367 { MASK, ICODE, NAME, ENUM },
10369 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
10370 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
10371 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
10372 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
10373 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
10375 static const struct builtin_description bdesc_spe_evsel[] =
10377 #include "rs6000-builtin.def"
10380 /* PAIRED predicates. */
10381 #undef RS6000_BUILTIN_1
10382 #undef RS6000_BUILTIN_2
10383 #undef RS6000_BUILTIN_3
10384 #undef RS6000_BUILTIN_A
10385 #undef RS6000_BUILTIN_D
10386 #undef RS6000_BUILTIN_E
10387 #undef RS6000_BUILTIN_H
10388 #undef RS6000_BUILTIN_P
10389 #undef RS6000_BUILTIN_Q
10390 #undef RS6000_BUILTIN_S
10391 #undef RS6000_BUILTIN_X
10393 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
10394 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
10395 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
10396 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
10397 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
10398 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
10399 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
10400 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
10401 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE) \
10402 { MASK, ICODE, NAME, ENUM },
10404 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
10405 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
10407 static const struct builtin_description bdesc_paired_preds[] =
10409 #include "rs6000-builtin.def"
10412 /* ABS* operations. */
10414 #undef RS6000_BUILTIN_1
10415 #undef RS6000_BUILTIN_2
10416 #undef RS6000_BUILTIN_3
10417 #undef RS6000_BUILTIN_A
10418 #undef RS6000_BUILTIN_D
10419 #undef RS6000_BUILTIN_E
10420 #undef RS6000_BUILTIN_H
10421 #undef RS6000_BUILTIN_P
10422 #undef RS6000_BUILTIN_Q
10423 #undef RS6000_BUILTIN_S
10424 #undef RS6000_BUILTIN_X
10426 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
10427 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
10428 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
10429 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE) \
10430 { MASK, ICODE, NAME, ENUM },
10432 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
10433 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
10434 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
10435 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
10436 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
10437 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
10438 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
10440 static const struct builtin_description bdesc_abs[] =
10442 #include "rs6000-builtin.def"
10445 /* Simple unary operations: VECb = foo (unsigned literal) or VECb =
10446 foo (VECa). */
10448 #undef RS6000_BUILTIN_1
10449 #undef RS6000_BUILTIN_2
10450 #undef RS6000_BUILTIN_3
10451 #undef RS6000_BUILTIN_A
10452 #undef RS6000_BUILTIN_D
10453 #undef RS6000_BUILTIN_E
10454 #undef RS6000_BUILTIN_H
10455 #undef RS6000_BUILTIN_P
10456 #undef RS6000_BUILTIN_Q
10457 #undef RS6000_BUILTIN_S
10458 #undef RS6000_BUILTIN_X
10460 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE) \
10461 { MASK, ICODE, NAME, ENUM },
10463 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
10464 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
10465 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
10466 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
10467 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
10468 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
10469 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
10470 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
10471 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
10472 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
10474 static const struct builtin_description bdesc_1arg[] =
10476 #include "rs6000-builtin.def"
10479 /* HTM builtins. */
10480 #undef RS6000_BUILTIN_1
10481 #undef RS6000_BUILTIN_2
10482 #undef RS6000_BUILTIN_3
10483 #undef RS6000_BUILTIN_A
10484 #undef RS6000_BUILTIN_D
10485 #undef RS6000_BUILTIN_E
10486 #undef RS6000_BUILTIN_H
10487 #undef RS6000_BUILTIN_P
10488 #undef RS6000_BUILTIN_Q
10489 #undef RS6000_BUILTIN_S
10490 #undef RS6000_BUILTIN_X
10492 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
10493 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
10494 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
10495 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
10496 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
10497 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
10498 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE) \
10499 { MASK, ICODE, NAME, ENUM },
10501 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
10502 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
10503 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
10504 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
10506 static const struct builtin_description bdesc_htm[] =
10508 #include "rs6000-builtin.def"
10511 #undef RS6000_BUILTIN_1
10512 #undef RS6000_BUILTIN_2
10513 #undef RS6000_BUILTIN_3
10514 #undef RS6000_BUILTIN_A
10515 #undef RS6000_BUILTIN_D
10516 #undef RS6000_BUILTIN_E
10517 #undef RS6000_BUILTIN_H
10518 #undef RS6000_BUILTIN_P
10519 #undef RS6000_BUILTIN_Q
10520 #undef RS6000_BUILTIN_S
10522 /* Return true if a builtin function is overloaded. */
10523 bool
10524 rs6000_overloaded_builtin_p (enum rs6000_builtins fncode)
10526 return (rs6000_builtin_info[(int)fncode].attr & RS6000_BTC_OVERLOADED) != 0;
10529 /* Expand an expression EXP that calls a builtin without arguments. */
10530 static rtx
10531 rs6000_expand_zeroop_builtin (enum insn_code icode, rtx target)
10533 rtx pat;
10534 enum machine_mode tmode = insn_data[icode].operand[0].mode;
10536 if (icode == CODE_FOR_nothing)
10537 /* Builtin not supported on this processor. */
10538 return 0;
10540 if (target == 0
10541 || GET_MODE (target) != tmode
10542 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10543 target = gen_reg_rtx (tmode);
10545 pat = GEN_FCN (icode) (target);
10546 if (! pat)
10547 return 0;
10548 emit_insn (pat);
10550 return target;
10554 static rtx
10555 rs6000_expand_unop_builtin (enum insn_code icode, tree exp, rtx target)
10557 rtx pat;
10558 tree arg0 = CALL_EXPR_ARG (exp, 0);
10559 rtx op0 = expand_normal (arg0);
10560 enum machine_mode tmode = insn_data[icode].operand[0].mode;
10561 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
10563 if (icode == CODE_FOR_nothing)
10564 /* Builtin not supported on this processor. */
10565 return 0;
10567 /* If we got invalid arguments bail out before generating bad rtl. */
10568 if (arg0 == error_mark_node)
10569 return const0_rtx;
10571 if (icode == CODE_FOR_altivec_vspltisb
10572 || icode == CODE_FOR_altivec_vspltish
10573 || icode == CODE_FOR_altivec_vspltisw
10574 || icode == CODE_FOR_spe_evsplatfi
10575 || icode == CODE_FOR_spe_evsplati)
10577 /* Only allow 5-bit *signed* literals. */
10578 if (GET_CODE (op0) != CONST_INT
10579 || INTVAL (op0) > 15
10580 || INTVAL (op0) < -16)
10582 error ("argument 1 must be a 5-bit signed literal");
10583 return const0_rtx;
10587 if (target == 0
10588 || GET_MODE (target) != tmode
10589 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10590 target = gen_reg_rtx (tmode);
10592 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
10593 op0 = copy_to_mode_reg (mode0, op0);
10595 pat = GEN_FCN (icode) (target, op0);
10596 if (! pat)
10597 return 0;
10598 emit_insn (pat);
10600 return target;
10603 static rtx
10604 altivec_expand_abs_builtin (enum insn_code icode, tree exp, rtx target)
10606 rtx pat, scratch1, scratch2;
10607 tree arg0 = CALL_EXPR_ARG (exp, 0);
10608 rtx op0 = expand_normal (arg0);
10609 enum machine_mode tmode = insn_data[icode].operand[0].mode;
10610 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
10612 /* If we have invalid arguments, bail out before generating bad rtl. */
10613 if (arg0 == error_mark_node)
10614 return const0_rtx;
10616 if (target == 0
10617 || GET_MODE (target) != tmode
10618 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10619 target = gen_reg_rtx (tmode);
10621 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
10622 op0 = copy_to_mode_reg (mode0, op0);
10624 scratch1 = gen_reg_rtx (mode0);
10625 scratch2 = gen_reg_rtx (mode0);
10627 pat = GEN_FCN (icode) (target, op0, scratch1, scratch2);
10628 if (! pat)
10629 return 0;
10630 emit_insn (pat);
10632 return target;
10635 static rtx
10636 rs6000_expand_binop_builtin (enum insn_code icode, tree exp, rtx target)
10638 rtx pat;
10639 tree arg0 = CALL_EXPR_ARG (exp, 0);
10640 tree arg1 = CALL_EXPR_ARG (exp, 1);
10641 rtx op0 = expand_normal (arg0);
10642 rtx op1 = expand_normal (arg1);
10643 enum machine_mode tmode = insn_data[icode].operand[0].mode;
10644 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
10645 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
10647 if (icode == CODE_FOR_nothing)
10648 /* Builtin not supported on this processor. */
10649 return 0;
10651 /* If we got invalid arguments bail out before generating bad rtl. */
10652 if (arg0 == error_mark_node || arg1 == error_mark_node)
10653 return const0_rtx;
10655 if (icode == CODE_FOR_altivec_vcfux
10656 || icode == CODE_FOR_altivec_vcfsx
10657 || icode == CODE_FOR_altivec_vctsxs
10658 || icode == CODE_FOR_altivec_vctuxs
10659 || icode == CODE_FOR_altivec_vspltb
10660 || icode == CODE_FOR_altivec_vsplth
10661 || icode == CODE_FOR_altivec_vspltw
10662 || icode == CODE_FOR_spe_evaddiw
10663 || icode == CODE_FOR_spe_evldd
10664 || icode == CODE_FOR_spe_evldh
10665 || icode == CODE_FOR_spe_evldw
10666 || icode == CODE_FOR_spe_evlhhesplat
10667 || icode == CODE_FOR_spe_evlhhossplat
10668 || icode == CODE_FOR_spe_evlhhousplat
10669 || icode == CODE_FOR_spe_evlwhe
10670 || icode == CODE_FOR_spe_evlwhos
10671 || icode == CODE_FOR_spe_evlwhou
10672 || icode == CODE_FOR_spe_evlwhsplat
10673 || icode == CODE_FOR_spe_evlwwsplat
10674 || icode == CODE_FOR_spe_evrlwi
10675 || icode == CODE_FOR_spe_evslwi
10676 || icode == CODE_FOR_spe_evsrwis
10677 || icode == CODE_FOR_spe_evsubifw
10678 || icode == CODE_FOR_spe_evsrwiu)
10680 /* Only allow 5-bit unsigned literals. */
10681 STRIP_NOPS (arg1);
10682 if (TREE_CODE (arg1) != INTEGER_CST
10683 || TREE_INT_CST_LOW (arg1) & ~0x1f)
10685 error ("argument 2 must be a 5-bit unsigned literal");
10686 return const0_rtx;
10690 if (target == 0
10691 || GET_MODE (target) != tmode
10692 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10693 target = gen_reg_rtx (tmode);
10695 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
10696 op0 = copy_to_mode_reg (mode0, op0);
10697 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
10698 op1 = copy_to_mode_reg (mode1, op1);
10700 pat = GEN_FCN (icode) (target, op0, op1);
10701 if (! pat)
10702 return 0;
10703 emit_insn (pat);
10705 return target;
10708 static rtx
10709 altivec_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
10711 rtx pat, scratch;
10712 tree cr6_form = CALL_EXPR_ARG (exp, 0);
10713 tree arg0 = CALL_EXPR_ARG (exp, 1);
10714 tree arg1 = CALL_EXPR_ARG (exp, 2);
10715 rtx op0 = expand_normal (arg0);
10716 rtx op1 = expand_normal (arg1);
10717 enum machine_mode tmode = SImode;
10718 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
10719 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
10720 int cr6_form_int;
10722 if (TREE_CODE (cr6_form) != INTEGER_CST)
10724 error ("argument 1 of __builtin_altivec_predicate must be a constant");
10725 return const0_rtx;
10727 else
10728 cr6_form_int = TREE_INT_CST_LOW (cr6_form);
10730 gcc_assert (mode0 == mode1);
10732 /* If we have invalid arguments, bail out before generating bad rtl. */
10733 if (arg0 == error_mark_node || arg1 == error_mark_node)
10734 return const0_rtx;
10736 if (target == 0
10737 || GET_MODE (target) != tmode
10738 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10739 target = gen_reg_rtx (tmode);
10741 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
10742 op0 = copy_to_mode_reg (mode0, op0);
10743 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
10744 op1 = copy_to_mode_reg (mode1, op1);
10746 scratch = gen_reg_rtx (mode0);
10748 pat = GEN_FCN (icode) (scratch, op0, op1);
10749 if (! pat)
10750 return 0;
10751 emit_insn (pat);
10753 /* The vec_any* and vec_all* predicates use the same opcodes for two
10754 different operations, but the bits in CR6 will be different
10755 depending on what information we want. So we have to play tricks
10756 with CR6 to get the right bits out.
10758 If you think this is disgusting, look at the specs for the
10759 AltiVec predicates. */
10761 switch (cr6_form_int)
10763 case 0:
10764 emit_insn (gen_cr6_test_for_zero (target));
10765 break;
10766 case 1:
10767 emit_insn (gen_cr6_test_for_zero_reverse (target));
10768 break;
10769 case 2:
10770 emit_insn (gen_cr6_test_for_lt (target));
10771 break;
10772 case 3:
10773 emit_insn (gen_cr6_test_for_lt_reverse (target));
10774 break;
10775 default:
10776 error ("argument 1 of __builtin_altivec_predicate is out of range");
10777 break;
10780 return target;
10783 static rtx
10784 paired_expand_lv_builtin (enum insn_code icode, tree exp, rtx target)
10786 rtx pat, addr;
10787 tree arg0 = CALL_EXPR_ARG (exp, 0);
10788 tree arg1 = CALL_EXPR_ARG (exp, 1);
10789 enum machine_mode tmode = insn_data[icode].operand[0].mode;
10790 enum machine_mode mode0 = Pmode;
10791 enum machine_mode mode1 = Pmode;
10792 rtx op0 = expand_normal (arg0);
10793 rtx op1 = expand_normal (arg1);
10795 if (icode == CODE_FOR_nothing)
10796 /* Builtin not supported on this processor. */
10797 return 0;
10799 /* If we got invalid arguments bail out before generating bad rtl. */
10800 if (arg0 == error_mark_node || arg1 == error_mark_node)
10801 return const0_rtx;
10803 if (target == 0
10804 || GET_MODE (target) != tmode
10805 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10806 target = gen_reg_rtx (tmode);
10808 op1 = copy_to_mode_reg (mode1, op1);
10810 if (op0 == const0_rtx)
10812 addr = gen_rtx_MEM (tmode, op1);
10814 else
10816 op0 = copy_to_mode_reg (mode0, op0);
10817 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op0, op1));
10820 pat = GEN_FCN (icode) (target, addr);
10822 if (! pat)
10823 return 0;
10824 emit_insn (pat);
10826 return target;
10829 static rtx
10830 altivec_expand_lv_builtin (enum insn_code icode, tree exp, rtx target, bool blk)
10832 rtx pat, addr;
10833 tree arg0 = CALL_EXPR_ARG (exp, 0);
10834 tree arg1 = CALL_EXPR_ARG (exp, 1);
10835 enum machine_mode tmode = insn_data[icode].operand[0].mode;
10836 enum machine_mode mode0 = Pmode;
10837 enum machine_mode mode1 = Pmode;
10838 rtx op0 = expand_normal (arg0);
10839 rtx op1 = expand_normal (arg1);
10841 if (icode == CODE_FOR_nothing)
10842 /* Builtin not supported on this processor. */
10843 return 0;
10845 /* If we got invalid arguments bail out before generating bad rtl. */
10846 if (arg0 == error_mark_node || arg1 == error_mark_node)
10847 return const0_rtx;
10849 if (target == 0
10850 || GET_MODE (target) != tmode
10851 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10852 target = gen_reg_rtx (tmode);
10854 op1 = copy_to_mode_reg (mode1, op1);
10856 if (op0 == const0_rtx)
10858 addr = gen_rtx_MEM (blk ? BLKmode : tmode, op1);
10860 else
10862 op0 = copy_to_mode_reg (mode0, op0);
10863 addr = gen_rtx_MEM (blk ? BLKmode : tmode, gen_rtx_PLUS (Pmode, op0, op1));
10866 pat = GEN_FCN (icode) (target, addr);
10868 if (! pat)
10869 return 0;
10870 emit_insn (pat);
10872 return target;
10875 static rtx
10876 spe_expand_stv_builtin (enum insn_code icode, tree exp)
10878 tree arg0 = CALL_EXPR_ARG (exp, 0);
10879 tree arg1 = CALL_EXPR_ARG (exp, 1);
10880 tree arg2 = CALL_EXPR_ARG (exp, 2);
10881 rtx op0 = expand_normal (arg0);
10882 rtx op1 = expand_normal (arg1);
10883 rtx op2 = expand_normal (arg2);
10884 rtx pat;
10885 enum machine_mode mode0 = insn_data[icode].operand[0].mode;
10886 enum machine_mode mode1 = insn_data[icode].operand[1].mode;
10887 enum machine_mode mode2 = insn_data[icode].operand[2].mode;
10889 /* Invalid arguments. Bail before doing anything stoopid! */
10890 if (arg0 == error_mark_node
10891 || arg1 == error_mark_node
10892 || arg2 == error_mark_node)
10893 return const0_rtx;
10895 if (! (*insn_data[icode].operand[2].predicate) (op0, mode2))
10896 op0 = copy_to_mode_reg (mode2, op0);
10897 if (! (*insn_data[icode].operand[0].predicate) (op1, mode0))
10898 op1 = copy_to_mode_reg (mode0, op1);
10899 if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
10900 op2 = copy_to_mode_reg (mode1, op2);
10902 pat = GEN_FCN (icode) (op1, op2, op0);
10903 if (pat)
10904 emit_insn (pat);
10905 return NULL_RTX;
10908 static rtx
10909 paired_expand_stv_builtin (enum insn_code icode, tree exp)
10911 tree arg0 = CALL_EXPR_ARG (exp, 0);
10912 tree arg1 = CALL_EXPR_ARG (exp, 1);
10913 tree arg2 = CALL_EXPR_ARG (exp, 2);
10914 rtx op0 = expand_normal (arg0);
10915 rtx op1 = expand_normal (arg1);
10916 rtx op2 = expand_normal (arg2);
10917 rtx pat, addr;
10918 enum machine_mode tmode = insn_data[icode].operand[0].mode;
10919 enum machine_mode mode1 = Pmode;
10920 enum machine_mode mode2 = Pmode;
10922 /* Invalid arguments. Bail before doing anything stoopid! */
10923 if (arg0 == error_mark_node
10924 || arg1 == error_mark_node
10925 || arg2 == error_mark_node)
10926 return const0_rtx;
10928 if (! (*insn_data[icode].operand[1].predicate) (op0, tmode))
10929 op0 = copy_to_mode_reg (tmode, op0);
10931 op2 = copy_to_mode_reg (mode2, op2);
10933 if (op1 == const0_rtx)
10935 addr = gen_rtx_MEM (tmode, op2);
10937 else
10939 op1 = copy_to_mode_reg (mode1, op1);
10940 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op1, op2));
10943 pat = GEN_FCN (icode) (addr, op0);
10944 if (pat)
10945 emit_insn (pat);
10946 return NULL_RTX;
10949 static rtx
10950 altivec_expand_stv_builtin (enum insn_code icode, tree exp)
10952 tree arg0 = CALL_EXPR_ARG (exp, 0);
10953 tree arg1 = CALL_EXPR_ARG (exp, 1);
10954 tree arg2 = CALL_EXPR_ARG (exp, 2);
10955 rtx op0 = expand_normal (arg0);
10956 rtx op1 = expand_normal (arg1);
10957 rtx op2 = expand_normal (arg2);
10958 rtx pat, addr;
10959 enum machine_mode tmode = insn_data[icode].operand[0].mode;
10960 enum machine_mode smode = insn_data[icode].operand[1].mode;
10961 enum machine_mode mode1 = Pmode;
10962 enum machine_mode mode2 = Pmode;
10964 /* Invalid arguments. Bail before doing anything stoopid! */
10965 if (arg0 == error_mark_node
10966 || arg1 == error_mark_node
10967 || arg2 == error_mark_node)
10968 return const0_rtx;
10970 if (! (*insn_data[icode].operand[1].predicate) (op0, smode))
10971 op0 = copy_to_mode_reg (smode, op0);
10973 op2 = copy_to_mode_reg (mode2, op2);
10975 if (op1 == const0_rtx)
10977 addr = gen_rtx_MEM (tmode, op2);
10979 else
10981 op1 = copy_to_mode_reg (mode1, op1);
10982 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op1, op2));
10985 pat = GEN_FCN (icode) (addr, op0);
10986 if (pat)
10987 emit_insn (pat);
10988 return NULL_RTX;
10991 /* Return the appropriate SPR number associated with the given builtin. */
10992 static inline HOST_WIDE_INT
10993 htm_spr_num (enum rs6000_builtins code)
10995 if (code == HTM_BUILTIN_GET_TFHAR
10996 || code == HTM_BUILTIN_SET_TFHAR)
10997 return TFHAR_SPR;
10998 else if (code == HTM_BUILTIN_GET_TFIAR
10999 || code == HTM_BUILTIN_SET_TFIAR)
11000 return TFIAR_SPR;
11001 else if (code == HTM_BUILTIN_GET_TEXASR
11002 || code == HTM_BUILTIN_SET_TEXASR)
11003 return TEXASR_SPR;
11004 gcc_assert (code == HTM_BUILTIN_GET_TEXASRU
11005 || code == HTM_BUILTIN_SET_TEXASRU);
11006 return TEXASRU_SPR;
11009 /* Return the appropriate SPR regno associated with the given builtin. */
11010 static inline HOST_WIDE_INT
11011 htm_spr_regno (enum rs6000_builtins code)
11013 if (code == HTM_BUILTIN_GET_TFHAR
11014 || code == HTM_BUILTIN_SET_TFHAR)
11015 return TFHAR_REGNO;
11016 else if (code == HTM_BUILTIN_GET_TFIAR
11017 || code == HTM_BUILTIN_SET_TFIAR)
11018 return TFIAR_REGNO;
11019 gcc_assert (code == HTM_BUILTIN_GET_TEXASR
11020 || code == HTM_BUILTIN_SET_TEXASR
11021 || code == HTM_BUILTIN_GET_TEXASRU
11022 || code == HTM_BUILTIN_SET_TEXASRU);
11023 return TEXASR_REGNO;
11026 /* Return the correct ICODE value depending on whether we are
11027 setting or reading the HTM SPRs. */
11028 static inline enum insn_code
11029 rs6000_htm_spr_icode (bool nonvoid)
11031 if (nonvoid)
11032 return (TARGET_64BIT) ? CODE_FOR_htm_mfspr_di : CODE_FOR_htm_mfspr_si;
11033 else
11034 return (TARGET_64BIT) ? CODE_FOR_htm_mtspr_di : CODE_FOR_htm_mtspr_si;
11037 /* Expand the HTM builtin in EXP and store the result in TARGET.
11038 Store true in *EXPANDEDP if we found a builtin to expand. */
11039 static rtx
11040 htm_expand_builtin (tree exp, rtx target, bool * expandedp)
11042 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
11043 bool nonvoid = TREE_TYPE (TREE_TYPE (fndecl)) != void_type_node;
11044 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
11045 const struct builtin_description *d;
11046 size_t i;
11048 *expandedp = false;
11050 /* Expand the HTM builtins. */
11051 d = bdesc_htm;
11052 for (i = 0; i < ARRAY_SIZE (bdesc_htm); i++, d++)
11053 if (d->code == fcode)
11055 rtx op[MAX_HTM_OPERANDS], pat;
11056 int nopnds = 0;
11057 tree arg;
11058 call_expr_arg_iterator iter;
11059 unsigned attr = rs6000_builtin_info[fcode].attr;
11060 enum insn_code icode = d->icode;
11062 if (attr & RS6000_BTC_SPR)
11063 icode = rs6000_htm_spr_icode (nonvoid);
11065 if (nonvoid)
11067 enum machine_mode tmode = insn_data[icode].operand[0].mode;
11068 if (!target
11069 || GET_MODE (target) != tmode
11070 || !(*insn_data[icode].operand[0].predicate) (target, tmode))
11071 target = gen_reg_rtx (tmode);
11072 op[nopnds++] = target;
11075 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
11077 const struct insn_operand_data *insn_op;
11079 if (arg == error_mark_node || nopnds >= MAX_HTM_OPERANDS)
11080 return NULL_RTX;
11082 insn_op = &insn_data[icode].operand[nopnds];
11084 op[nopnds] = expand_normal (arg);
11086 if (!(*insn_op->predicate) (op[nopnds], insn_op->mode))
11088 if (!strcmp (insn_op->constraint, "n"))
11090 int arg_num = (nonvoid) ? nopnds : nopnds + 1;
11091 if (!CONST_INT_P (op[nopnds]))
11092 error ("argument %d must be an unsigned literal", arg_num);
11093 else
11094 error ("argument %d is an unsigned literal that is "
11095 "out of range", arg_num);
11096 return const0_rtx;
11098 op[nopnds] = copy_to_mode_reg (insn_op->mode, op[nopnds]);
11101 nopnds++;
11104 /* Handle the builtins for extended mnemonics. These accept
11105 no arguments, but map to builtins that take arguments. */
11106 switch (fcode)
11108 case HTM_BUILTIN_TENDALL: /* Alias for: tend. 1 */
11109 case HTM_BUILTIN_TRESUME: /* Alias for: tsr. 1 */
11110 op[nopnds++] = GEN_INT (1);
11111 #ifdef ENABLE_CHECKING
11112 attr |= RS6000_BTC_UNARY;
11113 #endif
11114 break;
11115 case HTM_BUILTIN_TSUSPEND: /* Alias for: tsr. 0 */
11116 op[nopnds++] = GEN_INT (0);
11117 #ifdef ENABLE_CHECKING
11118 attr |= RS6000_BTC_UNARY;
11119 #endif
11120 break;
11121 default:
11122 break;
11125 /* If this builtin accesses SPRs, then pass in the appropriate
11126 SPR number and SPR regno as the last two operands. */
11127 if (attr & RS6000_BTC_SPR)
11129 op[nopnds++] = gen_rtx_CONST_INT (Pmode, htm_spr_num (fcode));
11130 op[nopnds++] = gen_rtx_REG (Pmode, htm_spr_regno (fcode));
11133 #ifdef ENABLE_CHECKING
11134 int expected_nopnds = 0;
11135 if ((attr & RS6000_BTC_TYPE_MASK) == RS6000_BTC_UNARY)
11136 expected_nopnds = 1;
11137 else if ((attr & RS6000_BTC_TYPE_MASK) == RS6000_BTC_BINARY)
11138 expected_nopnds = 2;
11139 else if ((attr & RS6000_BTC_TYPE_MASK) == RS6000_BTC_TERNARY)
11140 expected_nopnds = 3;
11141 if (!(attr & RS6000_BTC_VOID))
11142 expected_nopnds += 1;
11143 if (attr & RS6000_BTC_SPR)
11144 expected_nopnds += 2;
11146 gcc_assert (nopnds == expected_nopnds && nopnds <= MAX_HTM_OPERANDS);
11147 #endif
11149 switch (nopnds)
11151 case 1:
11152 pat = GEN_FCN (icode) (op[0]);
11153 break;
11154 case 2:
11155 pat = GEN_FCN (icode) (op[0], op[1]);
11156 break;
11157 case 3:
11158 pat = GEN_FCN (icode) (op[0], op[1], op[2]);
11159 break;
11160 case 4:
11161 pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3]);
11162 break;
11163 default:
11164 gcc_unreachable ();
11166 if (!pat)
11167 return NULL_RTX;
11168 emit_insn (pat);
11170 *expandedp = true;
11171 if (nonvoid)
11172 return target;
11173 return const0_rtx;
11176 return NULL_RTX;
11179 static rtx
11180 rs6000_expand_ternop_builtin (enum insn_code icode, tree exp, rtx target)
11182 rtx pat;
11183 tree arg0 = CALL_EXPR_ARG (exp, 0);
11184 tree arg1 = CALL_EXPR_ARG (exp, 1);
11185 tree arg2 = CALL_EXPR_ARG (exp, 2);
11186 rtx op0 = expand_normal (arg0);
11187 rtx op1 = expand_normal (arg1);
11188 rtx op2 = expand_normal (arg2);
11189 enum machine_mode tmode = insn_data[icode].operand[0].mode;
11190 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11191 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
11192 enum machine_mode mode2 = insn_data[icode].operand[3].mode;
11194 if (icode == CODE_FOR_nothing)
11195 /* Builtin not supported on this processor. */
11196 return 0;
11198 /* If we got invalid arguments bail out before generating bad rtl. */
11199 if (arg0 == error_mark_node
11200 || arg1 == error_mark_node
11201 || arg2 == error_mark_node)
11202 return const0_rtx;
11204 /* Check and prepare argument depending on the instruction code.
11206 Note that a switch statement instead of the sequence of tests
11207 would be incorrect as many of the CODE_FOR values could be
11208 CODE_FOR_nothing and that would yield multiple alternatives
11209 with identical values. We'd never reach here at runtime in
11210 this case. */
11211 if (icode == CODE_FOR_altivec_vsldoi_v4sf
11212 || icode == CODE_FOR_altivec_vsldoi_v4si
11213 || icode == CODE_FOR_altivec_vsldoi_v8hi
11214 || icode == CODE_FOR_altivec_vsldoi_v16qi)
11216 /* Only allow 4-bit unsigned literals. */
11217 STRIP_NOPS (arg2);
11218 if (TREE_CODE (arg2) != INTEGER_CST
11219 || TREE_INT_CST_LOW (arg2) & ~0xf)
11221 error ("argument 3 must be a 4-bit unsigned literal");
11222 return const0_rtx;
11225 else if (icode == CODE_FOR_vsx_xxpermdi_v2df
11226 || icode == CODE_FOR_vsx_xxpermdi_v2di
11227 || icode == CODE_FOR_vsx_xxsldwi_v16qi
11228 || icode == CODE_FOR_vsx_xxsldwi_v8hi
11229 || icode == CODE_FOR_vsx_xxsldwi_v4si
11230 || icode == CODE_FOR_vsx_xxsldwi_v4sf
11231 || icode == CODE_FOR_vsx_xxsldwi_v2di
11232 || icode == CODE_FOR_vsx_xxsldwi_v2df)
11234 /* Only allow 2-bit unsigned literals. */
11235 STRIP_NOPS (arg2);
11236 if (TREE_CODE (arg2) != INTEGER_CST
11237 || TREE_INT_CST_LOW (arg2) & ~0x3)
11239 error ("argument 3 must be a 2-bit unsigned literal");
11240 return const0_rtx;
11243 else if (icode == CODE_FOR_vsx_set_v2df
11244 || icode == CODE_FOR_vsx_set_v2di)
11246 /* Only allow 1-bit unsigned literals. */
11247 STRIP_NOPS (arg2);
11248 if (TREE_CODE (arg2) != INTEGER_CST
11249 || TREE_INT_CST_LOW (arg2) & ~0x1)
11251 error ("argument 3 must be a 1-bit unsigned literal");
11252 return const0_rtx;
11255 else if (icode == CODE_FOR_crypto_vshasigmaw
11256 || icode == CODE_FOR_crypto_vshasigmad)
11258 /* Check whether the 2nd and 3rd arguments are integer constants and in
11259 range and prepare arguments. */
11260 STRIP_NOPS (arg1);
11261 if (TREE_CODE (arg1) != INTEGER_CST
11262 || !IN_RANGE (TREE_INT_CST_LOW (arg1), 0, 1))
11264 error ("argument 2 must be 0 or 1");
11265 return const0_rtx;
11268 STRIP_NOPS (arg2);
11269 if (TREE_CODE (arg2) != INTEGER_CST
11270 || !IN_RANGE (TREE_INT_CST_LOW (arg2), 0, 15))
11272 error ("argument 3 must be in the range 0..15");
11273 return const0_rtx;
11277 if (target == 0
11278 || GET_MODE (target) != tmode
11279 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11280 target = gen_reg_rtx (tmode);
11282 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11283 op0 = copy_to_mode_reg (mode0, op0);
11284 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
11285 op1 = copy_to_mode_reg (mode1, op1);
11286 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
11287 op2 = copy_to_mode_reg (mode2, op2);
11289 if (TARGET_PAIRED_FLOAT && icode == CODE_FOR_selv2sf4)
11290 pat = GEN_FCN (icode) (target, op0, op1, op2, CONST0_RTX (SFmode));
11291 else
11292 pat = GEN_FCN (icode) (target, op0, op1, op2);
11293 if (! pat)
11294 return 0;
11295 emit_insn (pat);
11297 return target;
11300 /* Expand the lvx builtins. */
11301 static rtx
11302 altivec_expand_ld_builtin (tree exp, rtx target, bool *expandedp)
11304 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
11305 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
11306 tree arg0;
11307 enum machine_mode tmode, mode0;
11308 rtx pat, op0;
11309 enum insn_code icode;
11311 switch (fcode)
11313 case ALTIVEC_BUILTIN_LD_INTERNAL_16qi:
11314 icode = CODE_FOR_vector_altivec_load_v16qi;
11315 break;
11316 case ALTIVEC_BUILTIN_LD_INTERNAL_8hi:
11317 icode = CODE_FOR_vector_altivec_load_v8hi;
11318 break;
11319 case ALTIVEC_BUILTIN_LD_INTERNAL_4si:
11320 icode = CODE_FOR_vector_altivec_load_v4si;
11321 break;
11322 case ALTIVEC_BUILTIN_LD_INTERNAL_4sf:
11323 icode = CODE_FOR_vector_altivec_load_v4sf;
11324 break;
11325 case ALTIVEC_BUILTIN_LD_INTERNAL_2df:
11326 icode = CODE_FOR_vector_altivec_load_v2df;
11327 break;
11328 case ALTIVEC_BUILTIN_LD_INTERNAL_2di:
11329 icode = CODE_FOR_vector_altivec_load_v2di;
11330 break;
11331 default:
11332 *expandedp = false;
11333 return NULL_RTX;
11336 *expandedp = true;
11338 arg0 = CALL_EXPR_ARG (exp, 0);
11339 op0 = expand_normal (arg0);
11340 tmode = insn_data[icode].operand[0].mode;
11341 mode0 = insn_data[icode].operand[1].mode;
11343 if (target == 0
11344 || GET_MODE (target) != tmode
11345 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11346 target = gen_reg_rtx (tmode);
11348 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11349 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
11351 pat = GEN_FCN (icode) (target, op0);
11352 if (! pat)
11353 return 0;
11354 emit_insn (pat);
11355 return target;
11358 /* Expand the stvx builtins. */
11359 static rtx
11360 altivec_expand_st_builtin (tree exp, rtx target ATTRIBUTE_UNUSED,
11361 bool *expandedp)
11363 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
11364 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
11365 tree arg0, arg1;
11366 enum machine_mode mode0, mode1;
11367 rtx pat, op0, op1;
11368 enum insn_code icode;
11370 switch (fcode)
11372 case ALTIVEC_BUILTIN_ST_INTERNAL_16qi:
11373 icode = CODE_FOR_vector_altivec_store_v16qi;
11374 break;
11375 case ALTIVEC_BUILTIN_ST_INTERNAL_8hi:
11376 icode = CODE_FOR_vector_altivec_store_v8hi;
11377 break;
11378 case ALTIVEC_BUILTIN_ST_INTERNAL_4si:
11379 icode = CODE_FOR_vector_altivec_store_v4si;
11380 break;
11381 case ALTIVEC_BUILTIN_ST_INTERNAL_4sf:
11382 icode = CODE_FOR_vector_altivec_store_v4sf;
11383 break;
11384 case ALTIVEC_BUILTIN_ST_INTERNAL_2df:
11385 icode = CODE_FOR_vector_altivec_store_v2df;
11386 break;
11387 case ALTIVEC_BUILTIN_ST_INTERNAL_2di:
11388 icode = CODE_FOR_vector_altivec_store_v2di;
11389 break;
11390 default:
11391 *expandedp = false;
11392 return NULL_RTX;
11395 arg0 = CALL_EXPR_ARG (exp, 0);
11396 arg1 = CALL_EXPR_ARG (exp, 1);
11397 op0 = expand_normal (arg0);
11398 op1 = expand_normal (arg1);
11399 mode0 = insn_data[icode].operand[0].mode;
11400 mode1 = insn_data[icode].operand[1].mode;
11402 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
11403 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
11404 if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
11405 op1 = copy_to_mode_reg (mode1, op1);
11407 pat = GEN_FCN (icode) (op0, op1);
11408 if (pat)
11409 emit_insn (pat);
11411 *expandedp = true;
11412 return NULL_RTX;
11415 /* Expand the dst builtins. */
11416 static rtx
11417 altivec_expand_dst_builtin (tree exp, rtx target ATTRIBUTE_UNUSED,
11418 bool *expandedp)
11420 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
11421 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
11422 tree arg0, arg1, arg2;
11423 enum machine_mode mode0, mode1;
11424 rtx pat, op0, op1, op2;
11425 const struct builtin_description *d;
11426 size_t i;
11428 *expandedp = false;
11430 /* Handle DST variants. */
11431 d = bdesc_dst;
11432 for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
11433 if (d->code == fcode)
11435 arg0 = CALL_EXPR_ARG (exp, 0);
11436 arg1 = CALL_EXPR_ARG (exp, 1);
11437 arg2 = CALL_EXPR_ARG (exp, 2);
11438 op0 = expand_normal (arg0);
11439 op1 = expand_normal (arg1);
11440 op2 = expand_normal (arg2);
11441 mode0 = insn_data[d->icode].operand[0].mode;
11442 mode1 = insn_data[d->icode].operand[1].mode;
11444 /* Invalid arguments, bail out before generating bad rtl. */
11445 if (arg0 == error_mark_node
11446 || arg1 == error_mark_node
11447 || arg2 == error_mark_node)
11448 return const0_rtx;
11450 *expandedp = true;
11451 STRIP_NOPS (arg2);
11452 if (TREE_CODE (arg2) != INTEGER_CST
11453 || TREE_INT_CST_LOW (arg2) & ~0x3)
11455 error ("argument to %qs must be a 2-bit unsigned literal", d->name);
11456 return const0_rtx;
11459 if (! (*insn_data[d->icode].operand[0].predicate) (op0, mode0))
11460 op0 = copy_to_mode_reg (Pmode, op0);
11461 if (! (*insn_data[d->icode].operand[1].predicate) (op1, mode1))
11462 op1 = copy_to_mode_reg (mode1, op1);
11464 pat = GEN_FCN (d->icode) (op0, op1, op2);
11465 if (pat != 0)
11466 emit_insn (pat);
11468 return NULL_RTX;
11471 return NULL_RTX;
11474 /* Expand vec_init builtin. */
11475 static rtx
11476 altivec_expand_vec_init_builtin (tree type, tree exp, rtx target)
11478 enum machine_mode tmode = TYPE_MODE (type);
11479 enum machine_mode inner_mode = GET_MODE_INNER (tmode);
11480 int i, n_elt = GET_MODE_NUNITS (tmode);
11481 rtvec v = rtvec_alloc (n_elt);
11483 gcc_assert (VECTOR_MODE_P (tmode));
11484 gcc_assert (n_elt == call_expr_nargs (exp));
11486 for (i = 0; i < n_elt; ++i)
11488 rtx x = expand_normal (CALL_EXPR_ARG (exp, i));
11489 RTVEC_ELT (v, i) = gen_lowpart (inner_mode, x);
11492 if (!target || !register_operand (target, tmode))
11493 target = gen_reg_rtx (tmode);
11495 rs6000_expand_vector_init (target, gen_rtx_PARALLEL (tmode, v));
11496 return target;
11499 /* Return the integer constant in ARG. Constrain it to be in the range
11500 of the subparts of VEC_TYPE; issue an error if not. */
11502 static int
11503 get_element_number (tree vec_type, tree arg)
11505 unsigned HOST_WIDE_INT elt, max = TYPE_VECTOR_SUBPARTS (vec_type) - 1;
11507 if (!host_integerp (arg, 1)
11508 || (elt = tree_low_cst (arg, 1), elt > max))
11510 error ("selector must be an integer constant in the range 0..%wi", max);
11511 return 0;
11514 return elt;
11517 /* Expand vec_set builtin. */
11518 static rtx
11519 altivec_expand_vec_set_builtin (tree exp)
11521 enum machine_mode tmode, mode1;
11522 tree arg0, arg1, arg2;
11523 int elt;
11524 rtx op0, op1;
11526 arg0 = CALL_EXPR_ARG (exp, 0);
11527 arg1 = CALL_EXPR_ARG (exp, 1);
11528 arg2 = CALL_EXPR_ARG (exp, 2);
11530 tmode = TYPE_MODE (TREE_TYPE (arg0));
11531 mode1 = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
11532 gcc_assert (VECTOR_MODE_P (tmode));
11534 op0 = expand_expr (arg0, NULL_RTX, tmode, EXPAND_NORMAL);
11535 op1 = expand_expr (arg1, NULL_RTX, mode1, EXPAND_NORMAL);
11536 elt = get_element_number (TREE_TYPE (arg0), arg2);
11538 if (GET_MODE (op1) != mode1 && GET_MODE (op1) != VOIDmode)
11539 op1 = convert_modes (mode1, GET_MODE (op1), op1, true);
11541 op0 = force_reg (tmode, op0);
11542 op1 = force_reg (mode1, op1);
11544 rs6000_expand_vector_set (op0, op1, elt);
11546 return op0;
11549 /* Expand vec_ext builtin. */
11550 static rtx
11551 altivec_expand_vec_ext_builtin (tree exp, rtx target)
11553 enum machine_mode tmode, mode0;
11554 tree arg0, arg1;
11555 int elt;
11556 rtx op0;
11558 arg0 = CALL_EXPR_ARG (exp, 0);
11559 arg1 = CALL_EXPR_ARG (exp, 1);
11561 op0 = expand_normal (arg0);
11562 elt = get_element_number (TREE_TYPE (arg0), arg1);
11564 tmode = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
11565 mode0 = TYPE_MODE (TREE_TYPE (arg0));
11566 gcc_assert (VECTOR_MODE_P (mode0));
11568 op0 = force_reg (mode0, op0);
11570 if (optimize || !target || !register_operand (target, tmode))
11571 target = gen_reg_rtx (tmode);
11573 rs6000_expand_vector_extract (target, op0, elt);
11575 return target;
11578 /* Expand the builtin in EXP and store the result in TARGET. Store
11579 true in *EXPANDEDP if we found a builtin to expand. */
11580 static rtx
11581 altivec_expand_builtin (tree exp, rtx target, bool *expandedp)
11583 const struct builtin_description *d;
11584 size_t i;
11585 enum insn_code icode;
11586 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
11587 tree arg0;
11588 rtx op0, pat;
11589 enum machine_mode tmode, mode0;
11590 enum rs6000_builtins fcode
11591 = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
11593 if (rs6000_overloaded_builtin_p (fcode))
11595 *expandedp = true;
11596 error ("unresolved overload for Altivec builtin %qF", fndecl);
11598 /* Given it is invalid, just generate a normal call. */
11599 return expand_call (exp, target, false);
11602 target = altivec_expand_ld_builtin (exp, target, expandedp);
11603 if (*expandedp)
11604 return target;
11606 target = altivec_expand_st_builtin (exp, target, expandedp);
11607 if (*expandedp)
11608 return target;
11610 target = altivec_expand_dst_builtin (exp, target, expandedp);
11611 if (*expandedp)
11612 return target;
11614 *expandedp = true;
11616 switch (fcode)
11618 case ALTIVEC_BUILTIN_STVX:
11619 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v4si, exp);
11620 case ALTIVEC_BUILTIN_STVEBX:
11621 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvebx, exp);
11622 case ALTIVEC_BUILTIN_STVEHX:
11623 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvehx, exp);
11624 case ALTIVEC_BUILTIN_STVEWX:
11625 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvewx, exp);
11626 case ALTIVEC_BUILTIN_STVXL:
11627 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl, exp);
11629 case ALTIVEC_BUILTIN_STVLX:
11630 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvlx, exp);
11631 case ALTIVEC_BUILTIN_STVLXL:
11632 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvlxl, exp);
11633 case ALTIVEC_BUILTIN_STVRX:
11634 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvrx, exp);
11635 case ALTIVEC_BUILTIN_STVRXL:
11636 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvrxl, exp);
11638 case VSX_BUILTIN_STXVD2X_V2DF:
11639 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v2df, exp);
11640 case VSX_BUILTIN_STXVD2X_V2DI:
11641 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v2di, exp);
11642 case VSX_BUILTIN_STXVW4X_V4SF:
11643 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v4sf, exp);
11644 case VSX_BUILTIN_STXVW4X_V4SI:
11645 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v4si, exp);
11646 case VSX_BUILTIN_STXVW4X_V8HI:
11647 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v8hi, exp);
11648 case VSX_BUILTIN_STXVW4X_V16QI:
11649 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v16qi, exp);
11651 case ALTIVEC_BUILTIN_MFVSCR:
11652 icode = CODE_FOR_altivec_mfvscr;
11653 tmode = insn_data[icode].operand[0].mode;
11655 if (target == 0
11656 || GET_MODE (target) != tmode
11657 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11658 target = gen_reg_rtx (tmode);
11660 pat = GEN_FCN (icode) (target);
11661 if (! pat)
11662 return 0;
11663 emit_insn (pat);
11664 return target;
11666 case ALTIVEC_BUILTIN_MTVSCR:
11667 icode = CODE_FOR_altivec_mtvscr;
11668 arg0 = CALL_EXPR_ARG (exp, 0);
11669 op0 = expand_normal (arg0);
11670 mode0 = insn_data[icode].operand[0].mode;
11672 /* If we got invalid arguments bail out before generating bad rtl. */
11673 if (arg0 == error_mark_node)
11674 return const0_rtx;
11676 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
11677 op0 = copy_to_mode_reg (mode0, op0);
11679 pat = GEN_FCN (icode) (op0);
11680 if (pat)
11681 emit_insn (pat);
11682 return NULL_RTX;
11684 case ALTIVEC_BUILTIN_DSSALL:
11685 emit_insn (gen_altivec_dssall ());
11686 return NULL_RTX;
11688 case ALTIVEC_BUILTIN_DSS:
11689 icode = CODE_FOR_altivec_dss;
11690 arg0 = CALL_EXPR_ARG (exp, 0);
11691 STRIP_NOPS (arg0);
11692 op0 = expand_normal (arg0);
11693 mode0 = insn_data[icode].operand[0].mode;
11695 /* If we got invalid arguments bail out before generating bad rtl. */
11696 if (arg0 == error_mark_node)
11697 return const0_rtx;
11699 if (TREE_CODE (arg0) != INTEGER_CST
11700 || TREE_INT_CST_LOW (arg0) & ~0x3)
11702 error ("argument to dss must be a 2-bit unsigned literal");
11703 return const0_rtx;
11706 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
11707 op0 = copy_to_mode_reg (mode0, op0);
11709 emit_insn (gen_altivec_dss (op0));
11710 return NULL_RTX;
11712 case ALTIVEC_BUILTIN_VEC_INIT_V4SI:
11713 case ALTIVEC_BUILTIN_VEC_INIT_V8HI:
11714 case ALTIVEC_BUILTIN_VEC_INIT_V16QI:
11715 case ALTIVEC_BUILTIN_VEC_INIT_V4SF:
11716 case VSX_BUILTIN_VEC_INIT_V2DF:
11717 case VSX_BUILTIN_VEC_INIT_V2DI:
11718 return altivec_expand_vec_init_builtin (TREE_TYPE (exp), exp, target);
11720 case ALTIVEC_BUILTIN_VEC_SET_V4SI:
11721 case ALTIVEC_BUILTIN_VEC_SET_V8HI:
11722 case ALTIVEC_BUILTIN_VEC_SET_V16QI:
11723 case ALTIVEC_BUILTIN_VEC_SET_V4SF:
11724 case VSX_BUILTIN_VEC_SET_V2DF:
11725 case VSX_BUILTIN_VEC_SET_V2DI:
11726 return altivec_expand_vec_set_builtin (exp);
11728 case ALTIVEC_BUILTIN_VEC_EXT_V4SI:
11729 case ALTIVEC_BUILTIN_VEC_EXT_V8HI:
11730 case ALTIVEC_BUILTIN_VEC_EXT_V16QI:
11731 case ALTIVEC_BUILTIN_VEC_EXT_V4SF:
11732 case VSX_BUILTIN_VEC_EXT_V2DF:
11733 case VSX_BUILTIN_VEC_EXT_V2DI:
11734 return altivec_expand_vec_ext_builtin (exp, target);
11736 default:
11737 break;
11738 /* Fall through. */
11741 /* Expand abs* operations. */
11742 d = bdesc_abs;
11743 for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
11744 if (d->code == fcode)
11745 return altivec_expand_abs_builtin (d->icode, exp, target);
11747 /* Expand the AltiVec predicates. */
11748 d = bdesc_altivec_preds;
11749 for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, d++)
11750 if (d->code == fcode)
11751 return altivec_expand_predicate_builtin (d->icode, exp, target);
11753 /* LV* are funky. We initialized them differently. */
11754 switch (fcode)
11756 case ALTIVEC_BUILTIN_LVSL:
11757 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsl,
11758 exp, target, false);
11759 case ALTIVEC_BUILTIN_LVSR:
11760 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsr,
11761 exp, target, false);
11762 case ALTIVEC_BUILTIN_LVEBX:
11763 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvebx,
11764 exp, target, false);
11765 case ALTIVEC_BUILTIN_LVEHX:
11766 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvehx,
11767 exp, target, false);
11768 case ALTIVEC_BUILTIN_LVEWX:
11769 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvewx,
11770 exp, target, false);
11771 case ALTIVEC_BUILTIN_LVXL:
11772 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl,
11773 exp, target, false);
11774 case ALTIVEC_BUILTIN_LVX:
11775 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v4si,
11776 exp, target, false);
11777 case ALTIVEC_BUILTIN_LVLX:
11778 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvlx,
11779 exp, target, true);
11780 case ALTIVEC_BUILTIN_LVLXL:
11781 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvlxl,
11782 exp, target, true);
11783 case ALTIVEC_BUILTIN_LVRX:
11784 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvrx,
11785 exp, target, true);
11786 case ALTIVEC_BUILTIN_LVRXL:
11787 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvrxl,
11788 exp, target, true);
11789 case VSX_BUILTIN_LXVD2X_V2DF:
11790 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v2df,
11791 exp, target, false);
11792 case VSX_BUILTIN_LXVD2X_V2DI:
11793 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v2di,
11794 exp, target, false);
11795 case VSX_BUILTIN_LXVW4X_V4SF:
11796 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v4sf,
11797 exp, target, false);
11798 case VSX_BUILTIN_LXVW4X_V4SI:
11799 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v4si,
11800 exp, target, false);
11801 case VSX_BUILTIN_LXVW4X_V8HI:
11802 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v8hi,
11803 exp, target, false);
11804 case VSX_BUILTIN_LXVW4X_V16QI:
11805 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v16qi,
11806 exp, target, false);
11807 break;
11808 default:
11809 break;
11810 /* Fall through. */
11813 *expandedp = false;
11814 return NULL_RTX;
11817 /* Expand the builtin in EXP and store the result in TARGET. Store
11818 true in *EXPANDEDP if we found a builtin to expand. */
11819 static rtx
11820 paired_expand_builtin (tree exp, rtx target, bool * expandedp)
11822 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
11823 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
11824 const struct builtin_description *d;
11825 size_t i;
11827 *expandedp = true;
11829 switch (fcode)
11831 case PAIRED_BUILTIN_STX:
11832 return paired_expand_stv_builtin (CODE_FOR_paired_stx, exp);
11833 case PAIRED_BUILTIN_LX:
11834 return paired_expand_lv_builtin (CODE_FOR_paired_lx, exp, target);
11835 default:
11836 break;
11837 /* Fall through. */
11840 /* Expand the paired predicates. */
11841 d = bdesc_paired_preds;
11842 for (i = 0; i < ARRAY_SIZE (bdesc_paired_preds); i++, d++)
11843 if (d->code == fcode)
11844 return paired_expand_predicate_builtin (d->icode, exp, target);
11846 *expandedp = false;
11847 return NULL_RTX;
11850 /* Binops that need to be initialized manually, but can be expanded
11851 automagically by rs6000_expand_binop_builtin. */
11852 static const struct builtin_description bdesc_2arg_spe[] =
11854 { RS6000_BTM_SPE, CODE_FOR_spe_evlddx, "__builtin_spe_evlddx", SPE_BUILTIN_EVLDDX },
11855 { RS6000_BTM_SPE, CODE_FOR_spe_evldwx, "__builtin_spe_evldwx", SPE_BUILTIN_EVLDWX },
11856 { RS6000_BTM_SPE, CODE_FOR_spe_evldhx, "__builtin_spe_evldhx", SPE_BUILTIN_EVLDHX },
11857 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhex, "__builtin_spe_evlwhex", SPE_BUILTIN_EVLWHEX },
11858 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhoux, "__builtin_spe_evlwhoux", SPE_BUILTIN_EVLWHOUX },
11859 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhosx, "__builtin_spe_evlwhosx", SPE_BUILTIN_EVLWHOSX },
11860 { RS6000_BTM_SPE, CODE_FOR_spe_evlwwsplatx, "__builtin_spe_evlwwsplatx", SPE_BUILTIN_EVLWWSPLATX },
11861 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhsplatx, "__builtin_spe_evlwhsplatx", SPE_BUILTIN_EVLWHSPLATX },
11862 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhesplatx, "__builtin_spe_evlhhesplatx", SPE_BUILTIN_EVLHHESPLATX },
11863 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhousplatx, "__builtin_spe_evlhhousplatx", SPE_BUILTIN_EVLHHOUSPLATX },
11864 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhossplatx, "__builtin_spe_evlhhossplatx", SPE_BUILTIN_EVLHHOSSPLATX },
11865 { RS6000_BTM_SPE, CODE_FOR_spe_evldd, "__builtin_spe_evldd", SPE_BUILTIN_EVLDD },
11866 { RS6000_BTM_SPE, CODE_FOR_spe_evldw, "__builtin_spe_evldw", SPE_BUILTIN_EVLDW },
11867 { RS6000_BTM_SPE, CODE_FOR_spe_evldh, "__builtin_spe_evldh", SPE_BUILTIN_EVLDH },
11868 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhe, "__builtin_spe_evlwhe", SPE_BUILTIN_EVLWHE },
11869 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhou, "__builtin_spe_evlwhou", SPE_BUILTIN_EVLWHOU },
11870 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhos, "__builtin_spe_evlwhos", SPE_BUILTIN_EVLWHOS },
11871 { RS6000_BTM_SPE, CODE_FOR_spe_evlwwsplat, "__builtin_spe_evlwwsplat", SPE_BUILTIN_EVLWWSPLAT },
11872 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhsplat, "__builtin_spe_evlwhsplat", SPE_BUILTIN_EVLWHSPLAT },
11873 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhesplat, "__builtin_spe_evlhhesplat", SPE_BUILTIN_EVLHHESPLAT },
11874 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhousplat, "__builtin_spe_evlhhousplat", SPE_BUILTIN_EVLHHOUSPLAT },
11875 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhossplat, "__builtin_spe_evlhhossplat", SPE_BUILTIN_EVLHHOSSPLAT }
11878 /* Expand the builtin in EXP and store the result in TARGET. Store
11879 true in *EXPANDEDP if we found a builtin to expand.
11881 This expands the SPE builtins that are not simple unary and binary
11882 operations. */
11883 static rtx
11884 spe_expand_builtin (tree exp, rtx target, bool *expandedp)
11886 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
11887 tree arg1, arg0;
11888 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
11889 enum insn_code icode;
11890 enum machine_mode tmode, mode0;
11891 rtx pat, op0;
11892 const struct builtin_description *d;
11893 size_t i;
11895 *expandedp = true;
11897 /* Syntax check for a 5-bit unsigned immediate. */
11898 switch (fcode)
11900 case SPE_BUILTIN_EVSTDD:
11901 case SPE_BUILTIN_EVSTDH:
11902 case SPE_BUILTIN_EVSTDW:
11903 case SPE_BUILTIN_EVSTWHE:
11904 case SPE_BUILTIN_EVSTWHO:
11905 case SPE_BUILTIN_EVSTWWE:
11906 case SPE_BUILTIN_EVSTWWO:
11907 arg1 = CALL_EXPR_ARG (exp, 2);
11908 if (TREE_CODE (arg1) != INTEGER_CST
11909 || TREE_INT_CST_LOW (arg1) & ~0x1f)
11911 error ("argument 2 must be a 5-bit unsigned literal");
11912 return const0_rtx;
11914 break;
11915 default:
11916 break;
11919 /* The evsplat*i instructions are not quite generic. */
11920 switch (fcode)
11922 case SPE_BUILTIN_EVSPLATFI:
11923 return rs6000_expand_unop_builtin (CODE_FOR_spe_evsplatfi,
11924 exp, target);
11925 case SPE_BUILTIN_EVSPLATI:
11926 return rs6000_expand_unop_builtin (CODE_FOR_spe_evsplati,
11927 exp, target);
11928 default:
11929 break;
11932 d = bdesc_2arg_spe;
11933 for (i = 0; i < ARRAY_SIZE (bdesc_2arg_spe); ++i, ++d)
11934 if (d->code == fcode)
11935 return rs6000_expand_binop_builtin (d->icode, exp, target);
11937 d = bdesc_spe_predicates;
11938 for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, ++d)
11939 if (d->code == fcode)
11940 return spe_expand_predicate_builtin (d->icode, exp, target);
11942 d = bdesc_spe_evsel;
11943 for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, ++d)
11944 if (d->code == fcode)
11945 return spe_expand_evsel_builtin (d->icode, exp, target);
11947 switch (fcode)
11949 case SPE_BUILTIN_EVSTDDX:
11950 return spe_expand_stv_builtin (CODE_FOR_spe_evstddx, exp);
11951 case SPE_BUILTIN_EVSTDHX:
11952 return spe_expand_stv_builtin (CODE_FOR_spe_evstdhx, exp);
11953 case SPE_BUILTIN_EVSTDWX:
11954 return spe_expand_stv_builtin (CODE_FOR_spe_evstdwx, exp);
11955 case SPE_BUILTIN_EVSTWHEX:
11956 return spe_expand_stv_builtin (CODE_FOR_spe_evstwhex, exp);
11957 case SPE_BUILTIN_EVSTWHOX:
11958 return spe_expand_stv_builtin (CODE_FOR_spe_evstwhox, exp);
11959 case SPE_BUILTIN_EVSTWWEX:
11960 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwex, exp);
11961 case SPE_BUILTIN_EVSTWWOX:
11962 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwox, exp);
11963 case SPE_BUILTIN_EVSTDD:
11964 return spe_expand_stv_builtin (CODE_FOR_spe_evstdd, exp);
11965 case SPE_BUILTIN_EVSTDH:
11966 return spe_expand_stv_builtin (CODE_FOR_spe_evstdh, exp);
11967 case SPE_BUILTIN_EVSTDW:
11968 return spe_expand_stv_builtin (CODE_FOR_spe_evstdw, exp);
11969 case SPE_BUILTIN_EVSTWHE:
11970 return spe_expand_stv_builtin (CODE_FOR_spe_evstwhe, exp);
11971 case SPE_BUILTIN_EVSTWHO:
11972 return spe_expand_stv_builtin (CODE_FOR_spe_evstwho, exp);
11973 case SPE_BUILTIN_EVSTWWE:
11974 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwe, exp);
11975 case SPE_BUILTIN_EVSTWWO:
11976 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwo, exp);
11977 case SPE_BUILTIN_MFSPEFSCR:
11978 icode = CODE_FOR_spe_mfspefscr;
11979 tmode = insn_data[icode].operand[0].mode;
11981 if (target == 0
11982 || GET_MODE (target) != tmode
11983 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11984 target = gen_reg_rtx (tmode);
11986 pat = GEN_FCN (icode) (target);
11987 if (! pat)
11988 return 0;
11989 emit_insn (pat);
11990 return target;
11991 case SPE_BUILTIN_MTSPEFSCR:
11992 icode = CODE_FOR_spe_mtspefscr;
11993 arg0 = CALL_EXPR_ARG (exp, 0);
11994 op0 = expand_normal (arg0);
11995 mode0 = insn_data[icode].operand[0].mode;
11997 if (arg0 == error_mark_node)
11998 return const0_rtx;
12000 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
12001 op0 = copy_to_mode_reg (mode0, op0);
12003 pat = GEN_FCN (icode) (op0);
12004 if (pat)
12005 emit_insn (pat);
12006 return NULL_RTX;
12007 default:
12008 break;
12011 *expandedp = false;
12012 return NULL_RTX;
12015 static rtx
12016 paired_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
12018 rtx pat, scratch, tmp;
12019 tree form = CALL_EXPR_ARG (exp, 0);
12020 tree arg0 = CALL_EXPR_ARG (exp, 1);
12021 tree arg1 = CALL_EXPR_ARG (exp, 2);
12022 rtx op0 = expand_normal (arg0);
12023 rtx op1 = expand_normal (arg1);
12024 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12025 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
12026 int form_int;
12027 enum rtx_code code;
12029 if (TREE_CODE (form) != INTEGER_CST)
12031 error ("argument 1 of __builtin_paired_predicate must be a constant");
12032 return const0_rtx;
12034 else
12035 form_int = TREE_INT_CST_LOW (form);
12037 gcc_assert (mode0 == mode1);
12039 if (arg0 == error_mark_node || arg1 == error_mark_node)
12040 return const0_rtx;
12042 if (target == 0
12043 || GET_MODE (target) != SImode
12044 || !(*insn_data[icode].operand[0].predicate) (target, SImode))
12045 target = gen_reg_rtx (SImode);
12046 if (!(*insn_data[icode].operand[1].predicate) (op0, mode0))
12047 op0 = copy_to_mode_reg (mode0, op0);
12048 if (!(*insn_data[icode].operand[2].predicate) (op1, mode1))
12049 op1 = copy_to_mode_reg (mode1, op1);
12051 scratch = gen_reg_rtx (CCFPmode);
12053 pat = GEN_FCN (icode) (scratch, op0, op1);
12054 if (!pat)
12055 return const0_rtx;
12057 emit_insn (pat);
12059 switch (form_int)
12061 /* LT bit. */
12062 case 0:
12063 code = LT;
12064 break;
12065 /* GT bit. */
12066 case 1:
12067 code = GT;
12068 break;
12069 /* EQ bit. */
12070 case 2:
12071 code = EQ;
12072 break;
12073 /* UN bit. */
12074 case 3:
12075 emit_insn (gen_move_from_CR_ov_bit (target, scratch));
12076 return target;
12077 default:
12078 error ("argument 1 of __builtin_paired_predicate is out of range");
12079 return const0_rtx;
12082 tmp = gen_rtx_fmt_ee (code, SImode, scratch, const0_rtx);
12083 emit_move_insn (target, tmp);
12084 return target;
12087 static rtx
12088 spe_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
12090 rtx pat, scratch, tmp;
12091 tree form = CALL_EXPR_ARG (exp, 0);
12092 tree arg0 = CALL_EXPR_ARG (exp, 1);
12093 tree arg1 = CALL_EXPR_ARG (exp, 2);
12094 rtx op0 = expand_normal (arg0);
12095 rtx op1 = expand_normal (arg1);
12096 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12097 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
12098 int form_int;
12099 enum rtx_code code;
12101 if (TREE_CODE (form) != INTEGER_CST)
12103 error ("argument 1 of __builtin_spe_predicate must be a constant");
12104 return const0_rtx;
12106 else
12107 form_int = TREE_INT_CST_LOW (form);
12109 gcc_assert (mode0 == mode1);
12111 if (arg0 == error_mark_node || arg1 == error_mark_node)
12112 return const0_rtx;
12114 if (target == 0
12115 || GET_MODE (target) != SImode
12116 || ! (*insn_data[icode].operand[0].predicate) (target, SImode))
12117 target = gen_reg_rtx (SImode);
12119 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12120 op0 = copy_to_mode_reg (mode0, op0);
12121 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12122 op1 = copy_to_mode_reg (mode1, op1);
12124 scratch = gen_reg_rtx (CCmode);
12126 pat = GEN_FCN (icode) (scratch, op0, op1);
12127 if (! pat)
12128 return const0_rtx;
12129 emit_insn (pat);
12131 /* There are 4 variants for each predicate: _any_, _all_, _upper_,
12132 _lower_. We use one compare, but look in different bits of the
12133 CR for each variant.
12135 There are 2 elements in each SPE simd type (upper/lower). The CR
12136 bits are set as follows:
12138 BIT0 | BIT 1 | BIT 2 | BIT 3
12139 U | L | (U | L) | (U & L)
12141 So, for an "all" relationship, BIT 3 would be set.
12142 For an "any" relationship, BIT 2 would be set. Etc.
12144 Following traditional nomenclature, these bits map to:
12146 BIT0 | BIT 1 | BIT 2 | BIT 3
12147 LT | GT | EQ | OV
12149 Later, we will generate rtl to look in the LT/EQ/EQ/OV bits.
12152 switch (form_int)
12154 /* All variant. OV bit. */
12155 case 0:
12156 /* We need to get to the OV bit, which is the ORDERED bit. We
12157 could generate (ordered:SI (reg:CC xx) (const_int 0)), but
12158 that's ugly and will make validate_condition_mode die.
12159 So let's just use another pattern. */
12160 emit_insn (gen_move_from_CR_ov_bit (target, scratch));
12161 return target;
12162 /* Any variant. EQ bit. */
12163 case 1:
12164 code = EQ;
12165 break;
12166 /* Upper variant. LT bit. */
12167 case 2:
12168 code = LT;
12169 break;
12170 /* Lower variant. GT bit. */
12171 case 3:
12172 code = GT;
12173 break;
12174 default:
12175 error ("argument 1 of __builtin_spe_predicate is out of range");
12176 return const0_rtx;
12179 tmp = gen_rtx_fmt_ee (code, SImode, scratch, const0_rtx);
12180 emit_move_insn (target, tmp);
12182 return target;
12185 /* The evsel builtins look like this:
12187 e = __builtin_spe_evsel_OP (a, b, c, d);
12189 and work like this:
12191 e[upper] = a[upper] *OP* b[upper] ? c[upper] : d[upper];
12192 e[lower] = a[lower] *OP* b[lower] ? c[lower] : d[lower];
12195 static rtx
12196 spe_expand_evsel_builtin (enum insn_code icode, tree exp, rtx target)
12198 rtx pat, scratch;
12199 tree arg0 = CALL_EXPR_ARG (exp, 0);
12200 tree arg1 = CALL_EXPR_ARG (exp, 1);
12201 tree arg2 = CALL_EXPR_ARG (exp, 2);
12202 tree arg3 = CALL_EXPR_ARG (exp, 3);
12203 rtx op0 = expand_normal (arg0);
12204 rtx op1 = expand_normal (arg1);
12205 rtx op2 = expand_normal (arg2);
12206 rtx op3 = expand_normal (arg3);
12207 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12208 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
12210 gcc_assert (mode0 == mode1);
12212 if (arg0 == error_mark_node || arg1 == error_mark_node
12213 || arg2 == error_mark_node || arg3 == error_mark_node)
12214 return const0_rtx;
12216 if (target == 0
12217 || GET_MODE (target) != mode0
12218 || ! (*insn_data[icode].operand[0].predicate) (target, mode0))
12219 target = gen_reg_rtx (mode0);
12221 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12222 op0 = copy_to_mode_reg (mode0, op0);
12223 if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
12224 op1 = copy_to_mode_reg (mode0, op1);
12225 if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
12226 op2 = copy_to_mode_reg (mode0, op2);
12227 if (! (*insn_data[icode].operand[1].predicate) (op3, mode1))
12228 op3 = copy_to_mode_reg (mode0, op3);
12230 /* Generate the compare. */
12231 scratch = gen_reg_rtx (CCmode);
12232 pat = GEN_FCN (icode) (scratch, op0, op1);
12233 if (! pat)
12234 return const0_rtx;
12235 emit_insn (pat);
12237 if (mode0 == V2SImode)
12238 emit_insn (gen_spe_evsel (target, op2, op3, scratch));
12239 else
12240 emit_insn (gen_spe_evsel_fs (target, op2, op3, scratch));
12242 return target;
12245 /* Raise an error message for a builtin function that is called without the
12246 appropriate target options being set. */
12248 static void
12249 rs6000_invalid_builtin (enum rs6000_builtins fncode)
12251 size_t uns_fncode = (size_t)fncode;
12252 const char *name = rs6000_builtin_info[uns_fncode].name;
12253 HOST_WIDE_INT fnmask = rs6000_builtin_info[uns_fncode].mask;
12255 gcc_assert (name != NULL);
12256 if ((fnmask & RS6000_BTM_CELL) != 0)
12257 error ("Builtin function %s is only valid for the cell processor", name);
12258 else if ((fnmask & RS6000_BTM_VSX) != 0)
12259 error ("Builtin function %s requires the -mvsx option", name);
12260 else if ((fnmask & RS6000_BTM_HTM) != 0)
12261 error ("Builtin function %s requires the -mhtm option", name);
12262 else if ((fnmask & RS6000_BTM_ALTIVEC) != 0)
12263 error ("Builtin function %s requires the -maltivec option", name);
12264 else if ((fnmask & RS6000_BTM_PAIRED) != 0)
12265 error ("Builtin function %s requires the -mpaired option", name);
12266 else if ((fnmask & RS6000_BTM_SPE) != 0)
12267 error ("Builtin function %s requires the -mspe option", name);
12268 else
12269 error ("Builtin function %s is not supported with the current options",
12270 name);
12273 /* Expand an expression EXP that calls a built-in function,
12274 with result going to TARGET if that's convenient
12275 (and in mode MODE if that's convenient).
12276 SUBTARGET may be used as the target for computing one of EXP's operands.
12277 IGNORE is nonzero if the value is to be ignored. */
12279 static rtx
12280 rs6000_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
12281 enum machine_mode mode ATTRIBUTE_UNUSED,
12282 int ignore ATTRIBUTE_UNUSED)
12284 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
12285 enum rs6000_builtins fcode
12286 = (enum rs6000_builtins)DECL_FUNCTION_CODE (fndecl);
12287 size_t uns_fcode = (size_t)fcode;
12288 const struct builtin_description *d;
12289 size_t i;
12290 rtx ret;
12291 bool success;
12292 HOST_WIDE_INT mask = rs6000_builtin_info[uns_fcode].mask;
12293 bool func_valid_p = ((rs6000_builtin_mask & mask) == mask);
12295 if (TARGET_DEBUG_BUILTIN)
12297 enum insn_code icode = rs6000_builtin_info[uns_fcode].icode;
12298 const char *name1 = rs6000_builtin_info[uns_fcode].name;
12299 const char *name2 = ((icode != CODE_FOR_nothing)
12300 ? get_insn_name ((int)icode)
12301 : "nothing");
12302 const char *name3;
12304 switch (rs6000_builtin_info[uns_fcode].attr & RS6000_BTC_TYPE_MASK)
12306 default: name3 = "unknown"; break;
12307 case RS6000_BTC_SPECIAL: name3 = "special"; break;
12308 case RS6000_BTC_UNARY: name3 = "unary"; break;
12309 case RS6000_BTC_BINARY: name3 = "binary"; break;
12310 case RS6000_BTC_TERNARY: name3 = "ternary"; break;
12311 case RS6000_BTC_PREDICATE: name3 = "predicate"; break;
12312 case RS6000_BTC_ABS: name3 = "abs"; break;
12313 case RS6000_BTC_EVSEL: name3 = "evsel"; break;
12314 case RS6000_BTC_DST: name3 = "dst"; break;
12318 fprintf (stderr,
12319 "rs6000_expand_builtin, %s (%d), insn = %s (%d), type=%s%s\n",
12320 (name1) ? name1 : "---", fcode,
12321 (name2) ? name2 : "---", (int)icode,
12322 name3,
12323 func_valid_p ? "" : ", not valid");
12326 if (!func_valid_p)
12328 rs6000_invalid_builtin (fcode);
12330 /* Given it is invalid, just generate a normal call. */
12331 return expand_call (exp, target, ignore);
12334 switch (fcode)
12336 case RS6000_BUILTIN_RECIP:
12337 return rs6000_expand_binop_builtin (CODE_FOR_recipdf3, exp, target);
12339 case RS6000_BUILTIN_RECIPF:
12340 return rs6000_expand_binop_builtin (CODE_FOR_recipsf3, exp, target);
12342 case RS6000_BUILTIN_RSQRTF:
12343 return rs6000_expand_unop_builtin (CODE_FOR_rsqrtsf2, exp, target);
12345 case RS6000_BUILTIN_RSQRT:
12346 return rs6000_expand_unop_builtin (CODE_FOR_rsqrtdf2, exp, target);
12348 case POWER7_BUILTIN_BPERMD:
12349 return rs6000_expand_binop_builtin (((TARGET_64BIT)
12350 ? CODE_FOR_bpermd_di
12351 : CODE_FOR_bpermd_si), exp, target);
12353 case RS6000_BUILTIN_GET_TB:
12354 return rs6000_expand_zeroop_builtin (CODE_FOR_rs6000_get_timebase,
12355 target);
12357 case RS6000_BUILTIN_MFTB:
12358 return rs6000_expand_zeroop_builtin (((TARGET_64BIT)
12359 ? CODE_FOR_rs6000_mftb_di
12360 : CODE_FOR_rs6000_mftb_si),
12361 target);
12363 case ALTIVEC_BUILTIN_MASK_FOR_LOAD:
12364 case ALTIVEC_BUILTIN_MASK_FOR_STORE:
12366 int icode = (BYTES_BIG_ENDIAN ? (int) CODE_FOR_altivec_lvsr
12367 : (int) CODE_FOR_altivec_lvsl);
12368 enum machine_mode tmode = insn_data[icode].operand[0].mode;
12369 enum machine_mode mode = insn_data[icode].operand[1].mode;
12370 tree arg;
12371 rtx op, addr, pat;
12373 gcc_assert (TARGET_ALTIVEC);
12375 arg = CALL_EXPR_ARG (exp, 0);
12376 gcc_assert (POINTER_TYPE_P (TREE_TYPE (arg)));
12377 op = expand_expr (arg, NULL_RTX, Pmode, EXPAND_NORMAL);
12378 addr = memory_address (mode, op);
12379 if (fcode == ALTIVEC_BUILTIN_MASK_FOR_STORE)
12380 op = addr;
12381 else
12383 /* For the load case need to negate the address. */
12384 op = gen_reg_rtx (GET_MODE (addr));
12385 emit_insn (gen_rtx_SET (VOIDmode, op,
12386 gen_rtx_NEG (GET_MODE (addr), addr)));
12388 op = gen_rtx_MEM (mode, op);
12390 if (target == 0
12391 || GET_MODE (target) != tmode
12392 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12393 target = gen_reg_rtx (tmode);
12395 /*pat = gen_altivec_lvsr (target, op);*/
12396 pat = GEN_FCN (icode) (target, op);
12397 if (!pat)
12398 return 0;
12399 emit_insn (pat);
12401 return target;
12404 case ALTIVEC_BUILTIN_VCFUX:
12405 case ALTIVEC_BUILTIN_VCFSX:
12406 case ALTIVEC_BUILTIN_VCTUXS:
12407 case ALTIVEC_BUILTIN_VCTSXS:
12408 /* FIXME: There's got to be a nicer way to handle this case than
12409 constructing a new CALL_EXPR. */
12410 if (call_expr_nargs (exp) == 1)
12412 exp = build_call_nary (TREE_TYPE (exp), CALL_EXPR_FN (exp),
12413 2, CALL_EXPR_ARG (exp, 0), integer_zero_node);
12415 break;
12417 default:
12418 break;
12421 if (TARGET_ALTIVEC)
12423 ret = altivec_expand_builtin (exp, target, &success);
12425 if (success)
12426 return ret;
12428 if (TARGET_SPE)
12430 ret = spe_expand_builtin (exp, target, &success);
12432 if (success)
12433 return ret;
12435 if (TARGET_PAIRED_FLOAT)
12437 ret = paired_expand_builtin (exp, target, &success);
12439 if (success)
12440 return ret;
12442 if (TARGET_HTM)
12444 ret = htm_expand_builtin (exp, target, &success);
12446 if (success)
12447 return ret;
12450 gcc_assert (TARGET_ALTIVEC || TARGET_VSX || TARGET_SPE || TARGET_PAIRED_FLOAT);
12452 /* Handle simple unary operations. */
12453 d = bdesc_1arg;
12454 for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
12455 if (d->code == fcode)
12456 return rs6000_expand_unop_builtin (d->icode, exp, target);
12458 /* Handle simple binary operations. */
12459 d = bdesc_2arg;
12460 for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
12461 if (d->code == fcode)
12462 return rs6000_expand_binop_builtin (d->icode, exp, target);
12464 /* Handle simple ternary operations. */
12465 d = bdesc_3arg;
12466 for (i = 0; i < ARRAY_SIZE (bdesc_3arg); i++, d++)
12467 if (d->code == fcode)
12468 return rs6000_expand_ternop_builtin (d->icode, exp, target);
12470 gcc_unreachable ();
12473 static void
12474 rs6000_init_builtins (void)
12476 tree tdecl;
12477 tree ftype;
12478 enum machine_mode mode;
12480 if (TARGET_DEBUG_BUILTIN)
12481 fprintf (stderr, "rs6000_init_builtins%s%s%s%s\n",
12482 (TARGET_PAIRED_FLOAT) ? ", paired" : "",
12483 (TARGET_SPE) ? ", spe" : "",
12484 (TARGET_ALTIVEC) ? ", altivec" : "",
12485 (TARGET_VSX) ? ", vsx" : "");
12487 V2SI_type_node = build_vector_type (intSI_type_node, 2);
12488 V2SF_type_node = build_vector_type (float_type_node, 2);
12489 V2DI_type_node = build_vector_type (intDI_type_node, 2);
12490 V2DF_type_node = build_vector_type (double_type_node, 2);
12491 V4HI_type_node = build_vector_type (intHI_type_node, 4);
12492 V4SI_type_node = build_vector_type (intSI_type_node, 4);
12493 V4SF_type_node = build_vector_type (float_type_node, 4);
12494 V8HI_type_node = build_vector_type (intHI_type_node, 8);
12495 V16QI_type_node = build_vector_type (intQI_type_node, 16);
12497 unsigned_V16QI_type_node = build_vector_type (unsigned_intQI_type_node, 16);
12498 unsigned_V8HI_type_node = build_vector_type (unsigned_intHI_type_node, 8);
12499 unsigned_V4SI_type_node = build_vector_type (unsigned_intSI_type_node, 4);
12500 unsigned_V2DI_type_node = build_vector_type (unsigned_intDI_type_node, 2);
12502 opaque_V2SF_type_node = build_opaque_vector_type (float_type_node, 2);
12503 opaque_V2SI_type_node = build_opaque_vector_type (intSI_type_node, 2);
12504 opaque_p_V2SI_type_node = build_pointer_type (opaque_V2SI_type_node);
12505 opaque_V4SI_type_node = build_opaque_vector_type (intSI_type_node, 4);
12507 /* The 'vector bool ...' types must be kept distinct from 'vector unsigned ...'
12508 types, especially in C++ land. Similarly, 'vector pixel' is distinct from
12509 'vector unsigned short'. */
12511 bool_char_type_node = build_distinct_type_copy (unsigned_intQI_type_node);
12512 bool_short_type_node = build_distinct_type_copy (unsigned_intHI_type_node);
12513 bool_int_type_node = build_distinct_type_copy (unsigned_intSI_type_node);
12514 bool_long_type_node = build_distinct_type_copy (unsigned_intDI_type_node);
12515 pixel_type_node = build_distinct_type_copy (unsigned_intHI_type_node);
12517 long_integer_type_internal_node = long_integer_type_node;
12518 long_unsigned_type_internal_node = long_unsigned_type_node;
12519 long_long_integer_type_internal_node = long_long_integer_type_node;
12520 long_long_unsigned_type_internal_node = long_long_unsigned_type_node;
12521 intQI_type_internal_node = intQI_type_node;
12522 uintQI_type_internal_node = unsigned_intQI_type_node;
12523 intHI_type_internal_node = intHI_type_node;
12524 uintHI_type_internal_node = unsigned_intHI_type_node;
12525 intSI_type_internal_node = intSI_type_node;
12526 uintSI_type_internal_node = unsigned_intSI_type_node;
12527 intDI_type_internal_node = intDI_type_node;
12528 uintDI_type_internal_node = unsigned_intDI_type_node;
12529 float_type_internal_node = float_type_node;
12530 double_type_internal_node = double_type_node;
12531 void_type_internal_node = void_type_node;
12533 /* Initialize the modes for builtin_function_type, mapping a machine mode to
12534 tree type node. */
12535 builtin_mode_to_type[QImode][0] = integer_type_node;
12536 builtin_mode_to_type[HImode][0] = integer_type_node;
12537 builtin_mode_to_type[SImode][0] = intSI_type_node;
12538 builtin_mode_to_type[SImode][1] = unsigned_intSI_type_node;
12539 builtin_mode_to_type[DImode][0] = intDI_type_node;
12540 builtin_mode_to_type[DImode][1] = unsigned_intDI_type_node;
12541 builtin_mode_to_type[SFmode][0] = float_type_node;
12542 builtin_mode_to_type[DFmode][0] = double_type_node;
12543 builtin_mode_to_type[V2SImode][0] = V2SI_type_node;
12544 builtin_mode_to_type[V2SFmode][0] = V2SF_type_node;
12545 builtin_mode_to_type[V2DImode][0] = V2DI_type_node;
12546 builtin_mode_to_type[V2DImode][1] = unsigned_V2DI_type_node;
12547 builtin_mode_to_type[V2DFmode][0] = V2DF_type_node;
12548 builtin_mode_to_type[V4HImode][0] = V4HI_type_node;
12549 builtin_mode_to_type[V4SImode][0] = V4SI_type_node;
12550 builtin_mode_to_type[V4SImode][1] = unsigned_V4SI_type_node;
12551 builtin_mode_to_type[V4SFmode][0] = V4SF_type_node;
12552 builtin_mode_to_type[V8HImode][0] = V8HI_type_node;
12553 builtin_mode_to_type[V8HImode][1] = unsigned_V8HI_type_node;
12554 builtin_mode_to_type[V16QImode][0] = V16QI_type_node;
12555 builtin_mode_to_type[V16QImode][1] = unsigned_V16QI_type_node;
12557 tdecl = add_builtin_type ("__bool char", bool_char_type_node);
12558 TYPE_NAME (bool_char_type_node) = tdecl;
12560 tdecl = add_builtin_type ("__bool short", bool_short_type_node);
12561 TYPE_NAME (bool_short_type_node) = tdecl;
12563 tdecl = add_builtin_type ("__bool int", bool_int_type_node);
12564 TYPE_NAME (bool_int_type_node) = tdecl;
12566 tdecl = add_builtin_type ("__pixel", pixel_type_node);
12567 TYPE_NAME (pixel_type_node) = tdecl;
12569 bool_V16QI_type_node = build_vector_type (bool_char_type_node, 16);
12570 bool_V8HI_type_node = build_vector_type (bool_short_type_node, 8);
12571 bool_V4SI_type_node = build_vector_type (bool_int_type_node, 4);
12572 bool_V2DI_type_node = build_vector_type (bool_long_type_node, 2);
12573 pixel_V8HI_type_node = build_vector_type (pixel_type_node, 8);
12575 tdecl = add_builtin_type ("__vector unsigned char", unsigned_V16QI_type_node);
12576 TYPE_NAME (unsigned_V16QI_type_node) = tdecl;
12578 tdecl = add_builtin_type ("__vector signed char", V16QI_type_node);
12579 TYPE_NAME (V16QI_type_node) = tdecl;
12581 tdecl = add_builtin_type ("__vector __bool char", bool_V16QI_type_node);
12582 TYPE_NAME ( bool_V16QI_type_node) = tdecl;
12584 tdecl = add_builtin_type ("__vector unsigned short", unsigned_V8HI_type_node);
12585 TYPE_NAME (unsigned_V8HI_type_node) = tdecl;
12587 tdecl = add_builtin_type ("__vector signed short", V8HI_type_node);
12588 TYPE_NAME (V8HI_type_node) = tdecl;
12590 tdecl = add_builtin_type ("__vector __bool short", bool_V8HI_type_node);
12591 TYPE_NAME (bool_V8HI_type_node) = tdecl;
12593 tdecl = add_builtin_type ("__vector unsigned int", unsigned_V4SI_type_node);
12594 TYPE_NAME (unsigned_V4SI_type_node) = tdecl;
12596 tdecl = add_builtin_type ("__vector signed int", V4SI_type_node);
12597 TYPE_NAME (V4SI_type_node) = tdecl;
12599 tdecl = add_builtin_type ("__vector __bool int", bool_V4SI_type_node);
12600 TYPE_NAME (bool_V4SI_type_node) = tdecl;
12602 tdecl = add_builtin_type ("__vector float", V4SF_type_node);
12603 TYPE_NAME (V4SF_type_node) = tdecl;
12605 tdecl = add_builtin_type ("__vector __pixel", pixel_V8HI_type_node);
12606 TYPE_NAME (pixel_V8HI_type_node) = tdecl;
12608 tdecl = add_builtin_type ("__vector double", V2DF_type_node);
12609 TYPE_NAME (V2DF_type_node) = tdecl;
12611 tdecl = add_builtin_type ("__vector long", V2DI_type_node);
12612 TYPE_NAME (V2DI_type_node) = tdecl;
12614 tdecl = add_builtin_type ("__vector unsigned long", unsigned_V2DI_type_node);
12615 TYPE_NAME (unsigned_V2DI_type_node) = tdecl;
12617 tdecl = add_builtin_type ("__vector __bool long", bool_V2DI_type_node);
12618 TYPE_NAME (bool_V2DI_type_node) = tdecl;
12620 /* Paired and SPE builtins are only available if you build a compiler with
12621 the appropriate options, so only create those builtins with the
12622 appropriate compiler option. Create Altivec and VSX builtins on machines
12623 with at least the general purpose extensions (970 and newer) to allow the
12624 use of the target attribute. */
12625 if (TARGET_PAIRED_FLOAT)
12626 paired_init_builtins ();
12627 if (TARGET_SPE)
12628 spe_init_builtins ();
12629 if (TARGET_EXTRA_BUILTINS)
12630 altivec_init_builtins ();
12631 if (TARGET_HTM)
12632 htm_init_builtins ();
12634 if (TARGET_EXTRA_BUILTINS || TARGET_SPE || TARGET_PAIRED_FLOAT)
12635 rs6000_common_init_builtins ();
12637 ftype = builtin_function_type (DFmode, DFmode, DFmode, VOIDmode,
12638 RS6000_BUILTIN_RECIP, "__builtin_recipdiv");
12639 def_builtin ("__builtin_recipdiv", ftype, RS6000_BUILTIN_RECIP);
12641 ftype = builtin_function_type (SFmode, SFmode, SFmode, VOIDmode,
12642 RS6000_BUILTIN_RECIPF, "__builtin_recipdivf");
12643 def_builtin ("__builtin_recipdivf", ftype, RS6000_BUILTIN_RECIPF);
12645 ftype = builtin_function_type (DFmode, DFmode, VOIDmode, VOIDmode,
12646 RS6000_BUILTIN_RSQRT, "__builtin_rsqrt");
12647 def_builtin ("__builtin_rsqrt", ftype, RS6000_BUILTIN_RSQRT);
12649 ftype = builtin_function_type (SFmode, SFmode, VOIDmode, VOIDmode,
12650 RS6000_BUILTIN_RSQRTF, "__builtin_rsqrtf");
12651 def_builtin ("__builtin_rsqrtf", ftype, RS6000_BUILTIN_RSQRTF);
12653 mode = (TARGET_64BIT) ? DImode : SImode;
12654 ftype = builtin_function_type (mode, mode, mode, VOIDmode,
12655 POWER7_BUILTIN_BPERMD, "__builtin_bpermd");
12656 def_builtin ("__builtin_bpermd", ftype, POWER7_BUILTIN_BPERMD);
12658 ftype = build_function_type_list (unsigned_intDI_type_node,
12659 NULL_TREE);
12660 def_builtin ("__builtin_ppc_get_timebase", ftype, RS6000_BUILTIN_GET_TB);
12662 if (TARGET_64BIT)
12663 ftype = build_function_type_list (unsigned_intDI_type_node,
12664 NULL_TREE);
12665 else
12666 ftype = build_function_type_list (unsigned_intSI_type_node,
12667 NULL_TREE);
12668 def_builtin ("__builtin_ppc_mftb", ftype, RS6000_BUILTIN_MFTB);
12670 #if TARGET_XCOFF
12671 /* AIX libm provides clog as __clog. */
12672 if ((tdecl = builtin_decl_explicit (BUILT_IN_CLOG)) != NULL_TREE)
12673 set_user_assembler_name (tdecl, "__clog");
12674 #endif
12676 #ifdef SUBTARGET_INIT_BUILTINS
12677 SUBTARGET_INIT_BUILTINS;
12678 #endif
12681 /* Returns the rs6000 builtin decl for CODE. */
12683 static tree
12684 rs6000_builtin_decl (unsigned code, bool initialize_p ATTRIBUTE_UNUSED)
12686 HOST_WIDE_INT fnmask;
12688 if (code >= RS6000_BUILTIN_COUNT)
12689 return error_mark_node;
12691 fnmask = rs6000_builtin_info[code].mask;
12692 if ((fnmask & rs6000_builtin_mask) != fnmask)
12694 rs6000_invalid_builtin ((enum rs6000_builtins)code);
12695 return error_mark_node;
12698 return rs6000_builtin_decls[code];
12701 static void
12702 spe_init_builtins (void)
12704 tree puint_type_node = build_pointer_type (unsigned_type_node);
12705 tree pushort_type_node = build_pointer_type (short_unsigned_type_node);
12706 const struct builtin_description *d;
12707 size_t i;
12709 tree v2si_ftype_4_v2si
12710 = build_function_type_list (opaque_V2SI_type_node,
12711 opaque_V2SI_type_node,
12712 opaque_V2SI_type_node,
12713 opaque_V2SI_type_node,
12714 opaque_V2SI_type_node,
12715 NULL_TREE);
12717 tree v2sf_ftype_4_v2sf
12718 = build_function_type_list (opaque_V2SF_type_node,
12719 opaque_V2SF_type_node,
12720 opaque_V2SF_type_node,
12721 opaque_V2SF_type_node,
12722 opaque_V2SF_type_node,
12723 NULL_TREE);
12725 tree int_ftype_int_v2si_v2si
12726 = build_function_type_list (integer_type_node,
12727 integer_type_node,
12728 opaque_V2SI_type_node,
12729 opaque_V2SI_type_node,
12730 NULL_TREE);
12732 tree int_ftype_int_v2sf_v2sf
12733 = build_function_type_list (integer_type_node,
12734 integer_type_node,
12735 opaque_V2SF_type_node,
12736 opaque_V2SF_type_node,
12737 NULL_TREE);
12739 tree void_ftype_v2si_puint_int
12740 = build_function_type_list (void_type_node,
12741 opaque_V2SI_type_node,
12742 puint_type_node,
12743 integer_type_node,
12744 NULL_TREE);
12746 tree void_ftype_v2si_puint_char
12747 = build_function_type_list (void_type_node,
12748 opaque_V2SI_type_node,
12749 puint_type_node,
12750 char_type_node,
12751 NULL_TREE);
12753 tree void_ftype_v2si_pv2si_int
12754 = build_function_type_list (void_type_node,
12755 opaque_V2SI_type_node,
12756 opaque_p_V2SI_type_node,
12757 integer_type_node,
12758 NULL_TREE);
12760 tree void_ftype_v2si_pv2si_char
12761 = build_function_type_list (void_type_node,
12762 opaque_V2SI_type_node,
12763 opaque_p_V2SI_type_node,
12764 char_type_node,
12765 NULL_TREE);
12767 tree void_ftype_int
12768 = build_function_type_list (void_type_node, integer_type_node, NULL_TREE);
12770 tree int_ftype_void
12771 = build_function_type_list (integer_type_node, NULL_TREE);
12773 tree v2si_ftype_pv2si_int
12774 = build_function_type_list (opaque_V2SI_type_node,
12775 opaque_p_V2SI_type_node,
12776 integer_type_node,
12777 NULL_TREE);
12779 tree v2si_ftype_puint_int
12780 = build_function_type_list (opaque_V2SI_type_node,
12781 puint_type_node,
12782 integer_type_node,
12783 NULL_TREE);
12785 tree v2si_ftype_pushort_int
12786 = build_function_type_list (opaque_V2SI_type_node,
12787 pushort_type_node,
12788 integer_type_node,
12789 NULL_TREE);
12791 tree v2si_ftype_signed_char
12792 = build_function_type_list (opaque_V2SI_type_node,
12793 signed_char_type_node,
12794 NULL_TREE);
12796 add_builtin_type ("__ev64_opaque__", opaque_V2SI_type_node);
12798 /* Initialize irregular SPE builtins. */
12800 def_builtin ("__builtin_spe_mtspefscr", void_ftype_int, SPE_BUILTIN_MTSPEFSCR);
12801 def_builtin ("__builtin_spe_mfspefscr", int_ftype_void, SPE_BUILTIN_MFSPEFSCR);
12802 def_builtin ("__builtin_spe_evstddx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDDX);
12803 def_builtin ("__builtin_spe_evstdhx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDHX);
12804 def_builtin ("__builtin_spe_evstdwx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDWX);
12805 def_builtin ("__builtin_spe_evstwhex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHEX);
12806 def_builtin ("__builtin_spe_evstwhox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHOX);
12807 def_builtin ("__builtin_spe_evstwwex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWEX);
12808 def_builtin ("__builtin_spe_evstwwox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWOX);
12809 def_builtin ("__builtin_spe_evstdd", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDD);
12810 def_builtin ("__builtin_spe_evstdh", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDH);
12811 def_builtin ("__builtin_spe_evstdw", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDW);
12812 def_builtin ("__builtin_spe_evstwhe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHE);
12813 def_builtin ("__builtin_spe_evstwho", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHO);
12814 def_builtin ("__builtin_spe_evstwwe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWE);
12815 def_builtin ("__builtin_spe_evstwwo", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWO);
12816 def_builtin ("__builtin_spe_evsplatfi", v2si_ftype_signed_char, SPE_BUILTIN_EVSPLATFI);
12817 def_builtin ("__builtin_spe_evsplati", v2si_ftype_signed_char, SPE_BUILTIN_EVSPLATI);
12819 /* Loads. */
12820 def_builtin ("__builtin_spe_evlddx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDDX);
12821 def_builtin ("__builtin_spe_evldwx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDWX);
12822 def_builtin ("__builtin_spe_evldhx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDHX);
12823 def_builtin ("__builtin_spe_evlwhex", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHEX);
12824 def_builtin ("__builtin_spe_evlwhoux", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOUX);
12825 def_builtin ("__builtin_spe_evlwhosx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOSX);
12826 def_builtin ("__builtin_spe_evlwwsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLATX);
12827 def_builtin ("__builtin_spe_evlwhsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLATX);
12828 def_builtin ("__builtin_spe_evlhhesplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLATX);
12829 def_builtin ("__builtin_spe_evlhhousplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLATX);
12830 def_builtin ("__builtin_spe_evlhhossplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLATX);
12831 def_builtin ("__builtin_spe_evldd", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDD);
12832 def_builtin ("__builtin_spe_evldw", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDW);
12833 def_builtin ("__builtin_spe_evldh", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDH);
12834 def_builtin ("__builtin_spe_evlhhesplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLAT);
12835 def_builtin ("__builtin_spe_evlhhossplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLAT);
12836 def_builtin ("__builtin_spe_evlhhousplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLAT);
12837 def_builtin ("__builtin_spe_evlwhe", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHE);
12838 def_builtin ("__builtin_spe_evlwhos", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOS);
12839 def_builtin ("__builtin_spe_evlwhou", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOU);
12840 def_builtin ("__builtin_spe_evlwhsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLAT);
12841 def_builtin ("__builtin_spe_evlwwsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLAT);
12843 /* Predicates. */
12844 d = bdesc_spe_predicates;
12845 for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, d++)
12847 tree type;
12849 switch (insn_data[d->icode].operand[1].mode)
12851 case V2SImode:
12852 type = int_ftype_int_v2si_v2si;
12853 break;
12854 case V2SFmode:
12855 type = int_ftype_int_v2sf_v2sf;
12856 break;
12857 default:
12858 gcc_unreachable ();
12861 def_builtin (d->name, type, d->code);
12864 /* Evsel predicates. */
12865 d = bdesc_spe_evsel;
12866 for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, d++)
12868 tree type;
12870 switch (insn_data[d->icode].operand[1].mode)
12872 case V2SImode:
12873 type = v2si_ftype_4_v2si;
12874 break;
12875 case V2SFmode:
12876 type = v2sf_ftype_4_v2sf;
12877 break;
12878 default:
12879 gcc_unreachable ();
12882 def_builtin (d->name, type, d->code);
12886 static void
12887 paired_init_builtins (void)
12889 const struct builtin_description *d;
12890 size_t i;
12892 tree int_ftype_int_v2sf_v2sf
12893 = build_function_type_list (integer_type_node,
12894 integer_type_node,
12895 V2SF_type_node,
12896 V2SF_type_node,
12897 NULL_TREE);
12898 tree pcfloat_type_node =
12899 build_pointer_type (build_qualified_type
12900 (float_type_node, TYPE_QUAL_CONST));
12902 tree v2sf_ftype_long_pcfloat = build_function_type_list (V2SF_type_node,
12903 long_integer_type_node,
12904 pcfloat_type_node,
12905 NULL_TREE);
12906 tree void_ftype_v2sf_long_pcfloat =
12907 build_function_type_list (void_type_node,
12908 V2SF_type_node,
12909 long_integer_type_node,
12910 pcfloat_type_node,
12911 NULL_TREE);
12914 def_builtin ("__builtin_paired_lx", v2sf_ftype_long_pcfloat,
12915 PAIRED_BUILTIN_LX);
12918 def_builtin ("__builtin_paired_stx", void_ftype_v2sf_long_pcfloat,
12919 PAIRED_BUILTIN_STX);
12921 /* Predicates. */
12922 d = bdesc_paired_preds;
12923 for (i = 0; i < ARRAY_SIZE (bdesc_paired_preds); ++i, d++)
12925 tree type;
12927 if (TARGET_DEBUG_BUILTIN)
12928 fprintf (stderr, "paired pred #%d, insn = %s [%d], mode = %s\n",
12929 (int)i, get_insn_name (d->icode), (int)d->icode,
12930 GET_MODE_NAME (insn_data[d->icode].operand[1].mode));
12932 switch (insn_data[d->icode].operand[1].mode)
12934 case V2SFmode:
12935 type = int_ftype_int_v2sf_v2sf;
12936 break;
12937 default:
12938 gcc_unreachable ();
12941 def_builtin (d->name, type, d->code);
12945 static void
12946 altivec_init_builtins (void)
12948 const struct builtin_description *d;
12949 size_t i;
12950 tree ftype;
12951 tree decl;
12953 tree pvoid_type_node = build_pointer_type (void_type_node);
12955 tree pcvoid_type_node
12956 = build_pointer_type (build_qualified_type (void_type_node,
12957 TYPE_QUAL_CONST));
12959 tree int_ftype_opaque
12960 = build_function_type_list (integer_type_node,
12961 opaque_V4SI_type_node, NULL_TREE);
12962 tree opaque_ftype_opaque
12963 = build_function_type_list (integer_type_node, NULL_TREE);
12964 tree opaque_ftype_opaque_int
12965 = build_function_type_list (opaque_V4SI_type_node,
12966 opaque_V4SI_type_node, integer_type_node, NULL_TREE);
12967 tree opaque_ftype_opaque_opaque_int
12968 = build_function_type_list (opaque_V4SI_type_node,
12969 opaque_V4SI_type_node, opaque_V4SI_type_node,
12970 integer_type_node, NULL_TREE);
12971 tree int_ftype_int_opaque_opaque
12972 = build_function_type_list (integer_type_node,
12973 integer_type_node, opaque_V4SI_type_node,
12974 opaque_V4SI_type_node, NULL_TREE);
12975 tree int_ftype_int_v4si_v4si
12976 = build_function_type_list (integer_type_node,
12977 integer_type_node, V4SI_type_node,
12978 V4SI_type_node, NULL_TREE);
12979 tree int_ftype_int_v2di_v2di
12980 = build_function_type_list (integer_type_node,
12981 integer_type_node, V2DI_type_node,
12982 V2DI_type_node, NULL_TREE);
12983 tree void_ftype_v4si
12984 = build_function_type_list (void_type_node, V4SI_type_node, NULL_TREE);
12985 tree v8hi_ftype_void
12986 = build_function_type_list (V8HI_type_node, NULL_TREE);
12987 tree void_ftype_void
12988 = build_function_type_list (void_type_node, NULL_TREE);
12989 tree void_ftype_int
12990 = build_function_type_list (void_type_node, integer_type_node, NULL_TREE);
12992 tree opaque_ftype_long_pcvoid
12993 = build_function_type_list (opaque_V4SI_type_node,
12994 long_integer_type_node, pcvoid_type_node,
12995 NULL_TREE);
12996 tree v16qi_ftype_long_pcvoid
12997 = build_function_type_list (V16QI_type_node,
12998 long_integer_type_node, pcvoid_type_node,
12999 NULL_TREE);
13000 tree v8hi_ftype_long_pcvoid
13001 = build_function_type_list (V8HI_type_node,
13002 long_integer_type_node, pcvoid_type_node,
13003 NULL_TREE);
13004 tree v4si_ftype_long_pcvoid
13005 = build_function_type_list (V4SI_type_node,
13006 long_integer_type_node, pcvoid_type_node,
13007 NULL_TREE);
13008 tree v4sf_ftype_long_pcvoid
13009 = build_function_type_list (V4SF_type_node,
13010 long_integer_type_node, pcvoid_type_node,
13011 NULL_TREE);
13012 tree v2df_ftype_long_pcvoid
13013 = build_function_type_list (V2DF_type_node,
13014 long_integer_type_node, pcvoid_type_node,
13015 NULL_TREE);
13016 tree v2di_ftype_long_pcvoid
13017 = build_function_type_list (V2DI_type_node,
13018 long_integer_type_node, pcvoid_type_node,
13019 NULL_TREE);
13021 tree void_ftype_opaque_long_pvoid
13022 = build_function_type_list (void_type_node,
13023 opaque_V4SI_type_node, long_integer_type_node,
13024 pvoid_type_node, NULL_TREE);
13025 tree void_ftype_v4si_long_pvoid
13026 = build_function_type_list (void_type_node,
13027 V4SI_type_node, long_integer_type_node,
13028 pvoid_type_node, NULL_TREE);
13029 tree void_ftype_v16qi_long_pvoid
13030 = build_function_type_list (void_type_node,
13031 V16QI_type_node, long_integer_type_node,
13032 pvoid_type_node, NULL_TREE);
13033 tree void_ftype_v8hi_long_pvoid
13034 = build_function_type_list (void_type_node,
13035 V8HI_type_node, long_integer_type_node,
13036 pvoid_type_node, NULL_TREE);
13037 tree void_ftype_v4sf_long_pvoid
13038 = build_function_type_list (void_type_node,
13039 V4SF_type_node, long_integer_type_node,
13040 pvoid_type_node, NULL_TREE);
13041 tree void_ftype_v2df_long_pvoid
13042 = build_function_type_list (void_type_node,
13043 V2DF_type_node, long_integer_type_node,
13044 pvoid_type_node, NULL_TREE);
13045 tree void_ftype_v2di_long_pvoid
13046 = build_function_type_list (void_type_node,
13047 V2DI_type_node, long_integer_type_node,
13048 pvoid_type_node, NULL_TREE);
13049 tree int_ftype_int_v8hi_v8hi
13050 = build_function_type_list (integer_type_node,
13051 integer_type_node, V8HI_type_node,
13052 V8HI_type_node, NULL_TREE);
13053 tree int_ftype_int_v16qi_v16qi
13054 = build_function_type_list (integer_type_node,
13055 integer_type_node, V16QI_type_node,
13056 V16QI_type_node, NULL_TREE);
13057 tree int_ftype_int_v4sf_v4sf
13058 = build_function_type_list (integer_type_node,
13059 integer_type_node, V4SF_type_node,
13060 V4SF_type_node, NULL_TREE);
13061 tree int_ftype_int_v2df_v2df
13062 = build_function_type_list (integer_type_node,
13063 integer_type_node, V2DF_type_node,
13064 V2DF_type_node, NULL_TREE);
13065 tree v2di_ftype_v2di
13066 = build_function_type_list (V2DI_type_node, V2DI_type_node, NULL_TREE);
13067 tree v4si_ftype_v4si
13068 = build_function_type_list (V4SI_type_node, V4SI_type_node, NULL_TREE);
13069 tree v8hi_ftype_v8hi
13070 = build_function_type_list (V8HI_type_node, V8HI_type_node, NULL_TREE);
13071 tree v16qi_ftype_v16qi
13072 = build_function_type_list (V16QI_type_node, V16QI_type_node, NULL_TREE);
13073 tree v4sf_ftype_v4sf
13074 = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
13075 tree v2df_ftype_v2df
13076 = build_function_type_list (V2DF_type_node, V2DF_type_node, NULL_TREE);
13077 tree void_ftype_pcvoid_int_int
13078 = build_function_type_list (void_type_node,
13079 pcvoid_type_node, integer_type_node,
13080 integer_type_node, NULL_TREE);
13082 def_builtin ("__builtin_altivec_mtvscr", void_ftype_v4si, ALTIVEC_BUILTIN_MTVSCR);
13083 def_builtin ("__builtin_altivec_mfvscr", v8hi_ftype_void, ALTIVEC_BUILTIN_MFVSCR);
13084 def_builtin ("__builtin_altivec_dssall", void_ftype_void, ALTIVEC_BUILTIN_DSSALL);
13085 def_builtin ("__builtin_altivec_dss", void_ftype_int, ALTIVEC_BUILTIN_DSS);
13086 def_builtin ("__builtin_altivec_lvsl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSL);
13087 def_builtin ("__builtin_altivec_lvsr", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSR);
13088 def_builtin ("__builtin_altivec_lvebx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEBX);
13089 def_builtin ("__builtin_altivec_lvehx", v8hi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEHX);
13090 def_builtin ("__builtin_altivec_lvewx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEWX);
13091 def_builtin ("__builtin_altivec_lvxl", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVXL);
13092 def_builtin ("__builtin_altivec_lvx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVX);
13093 def_builtin ("__builtin_altivec_stvx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVX);
13094 def_builtin ("__builtin_altivec_stvewx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVEWX);
13095 def_builtin ("__builtin_altivec_stvxl", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVXL);
13096 def_builtin ("__builtin_altivec_stvebx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVEBX);
13097 def_builtin ("__builtin_altivec_stvehx", void_ftype_v8hi_long_pvoid, ALTIVEC_BUILTIN_STVEHX);
13098 def_builtin ("__builtin_vec_ld", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LD);
13099 def_builtin ("__builtin_vec_lde", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LDE);
13100 def_builtin ("__builtin_vec_ldl", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LDL);
13101 def_builtin ("__builtin_vec_lvsl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVSL);
13102 def_builtin ("__builtin_vec_lvsr", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVSR);
13103 def_builtin ("__builtin_vec_lvebx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEBX);
13104 def_builtin ("__builtin_vec_lvehx", v8hi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEHX);
13105 def_builtin ("__builtin_vec_lvewx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEWX);
13106 def_builtin ("__builtin_vec_st", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_ST);
13107 def_builtin ("__builtin_vec_ste", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STE);
13108 def_builtin ("__builtin_vec_stl", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STL);
13109 def_builtin ("__builtin_vec_stvewx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEWX);
13110 def_builtin ("__builtin_vec_stvebx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEBX);
13111 def_builtin ("__builtin_vec_stvehx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEHX);
13113 def_builtin ("__builtin_vsx_lxvd2x_v2df", v2df_ftype_long_pcvoid,
13114 VSX_BUILTIN_LXVD2X_V2DF);
13115 def_builtin ("__builtin_vsx_lxvd2x_v2di", v2di_ftype_long_pcvoid,
13116 VSX_BUILTIN_LXVD2X_V2DI);
13117 def_builtin ("__builtin_vsx_lxvw4x_v4sf", v4sf_ftype_long_pcvoid,
13118 VSX_BUILTIN_LXVW4X_V4SF);
13119 def_builtin ("__builtin_vsx_lxvw4x_v4si", v4si_ftype_long_pcvoid,
13120 VSX_BUILTIN_LXVW4X_V4SI);
13121 def_builtin ("__builtin_vsx_lxvw4x_v8hi", v8hi_ftype_long_pcvoid,
13122 VSX_BUILTIN_LXVW4X_V8HI);
13123 def_builtin ("__builtin_vsx_lxvw4x_v16qi", v16qi_ftype_long_pcvoid,
13124 VSX_BUILTIN_LXVW4X_V16QI);
13125 def_builtin ("__builtin_vsx_stxvd2x_v2df", void_ftype_v2df_long_pvoid,
13126 VSX_BUILTIN_STXVD2X_V2DF);
13127 def_builtin ("__builtin_vsx_stxvd2x_v2di", void_ftype_v2di_long_pvoid,
13128 VSX_BUILTIN_STXVD2X_V2DI);
13129 def_builtin ("__builtin_vsx_stxvw4x_v4sf", void_ftype_v4sf_long_pvoid,
13130 VSX_BUILTIN_STXVW4X_V4SF);
13131 def_builtin ("__builtin_vsx_stxvw4x_v4si", void_ftype_v4si_long_pvoid,
13132 VSX_BUILTIN_STXVW4X_V4SI);
13133 def_builtin ("__builtin_vsx_stxvw4x_v8hi", void_ftype_v8hi_long_pvoid,
13134 VSX_BUILTIN_STXVW4X_V8HI);
13135 def_builtin ("__builtin_vsx_stxvw4x_v16qi", void_ftype_v16qi_long_pvoid,
13136 VSX_BUILTIN_STXVW4X_V16QI);
13137 def_builtin ("__builtin_vec_vsx_ld", opaque_ftype_long_pcvoid,
13138 VSX_BUILTIN_VEC_LD);
13139 def_builtin ("__builtin_vec_vsx_st", void_ftype_opaque_long_pvoid,
13140 VSX_BUILTIN_VEC_ST);
13142 def_builtin ("__builtin_vec_step", int_ftype_opaque, ALTIVEC_BUILTIN_VEC_STEP);
13143 def_builtin ("__builtin_vec_splats", opaque_ftype_opaque, ALTIVEC_BUILTIN_VEC_SPLATS);
13144 def_builtin ("__builtin_vec_promote", opaque_ftype_opaque, ALTIVEC_BUILTIN_VEC_PROMOTE);
13146 def_builtin ("__builtin_vec_sld", opaque_ftype_opaque_opaque_int, ALTIVEC_BUILTIN_VEC_SLD);
13147 def_builtin ("__builtin_vec_splat", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_SPLAT);
13148 def_builtin ("__builtin_vec_extract", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_EXTRACT);
13149 def_builtin ("__builtin_vec_insert", opaque_ftype_opaque_opaque_int, ALTIVEC_BUILTIN_VEC_INSERT);
13150 def_builtin ("__builtin_vec_vspltw", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTW);
13151 def_builtin ("__builtin_vec_vsplth", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTH);
13152 def_builtin ("__builtin_vec_vspltb", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTB);
13153 def_builtin ("__builtin_vec_ctf", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTF);
13154 def_builtin ("__builtin_vec_vcfsx", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VCFSX);
13155 def_builtin ("__builtin_vec_vcfux", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VCFUX);
13156 def_builtin ("__builtin_vec_cts", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTS);
13157 def_builtin ("__builtin_vec_ctu", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTU);
13159 /* Cell builtins. */
13160 def_builtin ("__builtin_altivec_lvlx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVLX);
13161 def_builtin ("__builtin_altivec_lvlxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVLXL);
13162 def_builtin ("__builtin_altivec_lvrx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVRX);
13163 def_builtin ("__builtin_altivec_lvrxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVRXL);
13165 def_builtin ("__builtin_vec_lvlx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVLX);
13166 def_builtin ("__builtin_vec_lvlxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVLXL);
13167 def_builtin ("__builtin_vec_lvrx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVRX);
13168 def_builtin ("__builtin_vec_lvrxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVRXL);
13170 def_builtin ("__builtin_altivec_stvlx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVLX);
13171 def_builtin ("__builtin_altivec_stvlxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVLXL);
13172 def_builtin ("__builtin_altivec_stvrx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVRX);
13173 def_builtin ("__builtin_altivec_stvrxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVRXL);
13175 def_builtin ("__builtin_vec_stvlx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVLX);
13176 def_builtin ("__builtin_vec_stvlxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVLXL);
13177 def_builtin ("__builtin_vec_stvrx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVRX);
13178 def_builtin ("__builtin_vec_stvrxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVRXL);
13180 /* Add the DST variants. */
13181 d = bdesc_dst;
13182 for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
13183 def_builtin (d->name, void_ftype_pcvoid_int_int, d->code);
13185 /* Initialize the predicates. */
13186 d = bdesc_altivec_preds;
13187 for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, d++)
13189 enum machine_mode mode1;
13190 tree type;
13192 if (rs6000_overloaded_builtin_p (d->code))
13193 mode1 = VOIDmode;
13194 else
13195 mode1 = insn_data[d->icode].operand[1].mode;
13197 switch (mode1)
13199 case VOIDmode:
13200 type = int_ftype_int_opaque_opaque;
13201 break;
13202 case V2DImode:
13203 type = int_ftype_int_v2di_v2di;
13204 break;
13205 case V4SImode:
13206 type = int_ftype_int_v4si_v4si;
13207 break;
13208 case V8HImode:
13209 type = int_ftype_int_v8hi_v8hi;
13210 break;
13211 case V16QImode:
13212 type = int_ftype_int_v16qi_v16qi;
13213 break;
13214 case V4SFmode:
13215 type = int_ftype_int_v4sf_v4sf;
13216 break;
13217 case V2DFmode:
13218 type = int_ftype_int_v2df_v2df;
13219 break;
13220 default:
13221 gcc_unreachable ();
13224 def_builtin (d->name, type, d->code);
13227 /* Initialize the abs* operators. */
13228 d = bdesc_abs;
13229 for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
13231 enum machine_mode mode0;
13232 tree type;
13234 mode0 = insn_data[d->icode].operand[0].mode;
13236 switch (mode0)
13238 case V2DImode:
13239 type = v2di_ftype_v2di;
13240 break;
13241 case V4SImode:
13242 type = v4si_ftype_v4si;
13243 break;
13244 case V8HImode:
13245 type = v8hi_ftype_v8hi;
13246 break;
13247 case V16QImode:
13248 type = v16qi_ftype_v16qi;
13249 break;
13250 case V4SFmode:
13251 type = v4sf_ftype_v4sf;
13252 break;
13253 case V2DFmode:
13254 type = v2df_ftype_v2df;
13255 break;
13256 default:
13257 gcc_unreachable ();
13260 def_builtin (d->name, type, d->code);
13263 /* Initialize target builtin that implements
13264 targetm.vectorize.builtin_mask_for_load. */
13266 decl = add_builtin_function ("__builtin_altivec_mask_for_load",
13267 v16qi_ftype_long_pcvoid,
13268 ALTIVEC_BUILTIN_MASK_FOR_LOAD,
13269 BUILT_IN_MD, NULL, NULL_TREE);
13270 TREE_READONLY (decl) = 1;
13271 /* Record the decl. Will be used by rs6000_builtin_mask_for_load. */
13272 altivec_builtin_mask_for_load = decl;
13274 /* Access to the vec_init patterns. */
13275 ftype = build_function_type_list (V4SI_type_node, integer_type_node,
13276 integer_type_node, integer_type_node,
13277 integer_type_node, NULL_TREE);
13278 def_builtin ("__builtin_vec_init_v4si", ftype, ALTIVEC_BUILTIN_VEC_INIT_V4SI);
13280 ftype = build_function_type_list (V8HI_type_node, short_integer_type_node,
13281 short_integer_type_node,
13282 short_integer_type_node,
13283 short_integer_type_node,
13284 short_integer_type_node,
13285 short_integer_type_node,
13286 short_integer_type_node,
13287 short_integer_type_node, NULL_TREE);
13288 def_builtin ("__builtin_vec_init_v8hi", ftype, ALTIVEC_BUILTIN_VEC_INIT_V8HI);
13290 ftype = build_function_type_list (V16QI_type_node, char_type_node,
13291 char_type_node, char_type_node,
13292 char_type_node, char_type_node,
13293 char_type_node, char_type_node,
13294 char_type_node, char_type_node,
13295 char_type_node, char_type_node,
13296 char_type_node, char_type_node,
13297 char_type_node, char_type_node,
13298 char_type_node, NULL_TREE);
13299 def_builtin ("__builtin_vec_init_v16qi", ftype,
13300 ALTIVEC_BUILTIN_VEC_INIT_V16QI);
13302 ftype = build_function_type_list (V4SF_type_node, float_type_node,
13303 float_type_node, float_type_node,
13304 float_type_node, NULL_TREE);
13305 def_builtin ("__builtin_vec_init_v4sf", ftype, ALTIVEC_BUILTIN_VEC_INIT_V4SF);
13307 /* VSX builtins. */
13308 ftype = build_function_type_list (V2DF_type_node, double_type_node,
13309 double_type_node, NULL_TREE);
13310 def_builtin ("__builtin_vec_init_v2df", ftype, VSX_BUILTIN_VEC_INIT_V2DF);
13312 ftype = build_function_type_list (V2DI_type_node, intDI_type_node,
13313 intDI_type_node, NULL_TREE);
13314 def_builtin ("__builtin_vec_init_v2di", ftype, VSX_BUILTIN_VEC_INIT_V2DI);
13316 /* Access to the vec_set patterns. */
13317 ftype = build_function_type_list (V4SI_type_node, V4SI_type_node,
13318 intSI_type_node,
13319 integer_type_node, NULL_TREE);
13320 def_builtin ("__builtin_vec_set_v4si", ftype, ALTIVEC_BUILTIN_VEC_SET_V4SI);
13322 ftype = build_function_type_list (V8HI_type_node, V8HI_type_node,
13323 intHI_type_node,
13324 integer_type_node, NULL_TREE);
13325 def_builtin ("__builtin_vec_set_v8hi", ftype, ALTIVEC_BUILTIN_VEC_SET_V8HI);
13327 ftype = build_function_type_list (V16QI_type_node, V16QI_type_node,
13328 intQI_type_node,
13329 integer_type_node, NULL_TREE);
13330 def_builtin ("__builtin_vec_set_v16qi", ftype, ALTIVEC_BUILTIN_VEC_SET_V16QI);
13332 ftype = build_function_type_list (V4SF_type_node, V4SF_type_node,
13333 float_type_node,
13334 integer_type_node, NULL_TREE);
13335 def_builtin ("__builtin_vec_set_v4sf", ftype, ALTIVEC_BUILTIN_VEC_SET_V4SF);
13337 ftype = build_function_type_list (V2DF_type_node, V2DF_type_node,
13338 double_type_node,
13339 integer_type_node, NULL_TREE);
13340 def_builtin ("__builtin_vec_set_v2df", ftype, VSX_BUILTIN_VEC_SET_V2DF);
13342 ftype = build_function_type_list (V2DI_type_node, V2DI_type_node,
13343 intDI_type_node,
13344 integer_type_node, NULL_TREE);
13345 def_builtin ("__builtin_vec_set_v2di", ftype, VSX_BUILTIN_VEC_SET_V2DI);
13347 /* Access to the vec_extract patterns. */
13348 ftype = build_function_type_list (intSI_type_node, V4SI_type_node,
13349 integer_type_node, NULL_TREE);
13350 def_builtin ("__builtin_vec_ext_v4si", ftype, ALTIVEC_BUILTIN_VEC_EXT_V4SI);
13352 ftype = build_function_type_list (intHI_type_node, V8HI_type_node,
13353 integer_type_node, NULL_TREE);
13354 def_builtin ("__builtin_vec_ext_v8hi", ftype, ALTIVEC_BUILTIN_VEC_EXT_V8HI);
13356 ftype = build_function_type_list (intQI_type_node, V16QI_type_node,
13357 integer_type_node, NULL_TREE);
13358 def_builtin ("__builtin_vec_ext_v16qi", ftype, ALTIVEC_BUILTIN_VEC_EXT_V16QI);
13360 ftype = build_function_type_list (float_type_node, V4SF_type_node,
13361 integer_type_node, NULL_TREE);
13362 def_builtin ("__builtin_vec_ext_v4sf", ftype, ALTIVEC_BUILTIN_VEC_EXT_V4SF);
13364 ftype = build_function_type_list (double_type_node, V2DF_type_node,
13365 integer_type_node, NULL_TREE);
13366 def_builtin ("__builtin_vec_ext_v2df", ftype, VSX_BUILTIN_VEC_EXT_V2DF);
13368 ftype = build_function_type_list (intDI_type_node, V2DI_type_node,
13369 integer_type_node, NULL_TREE);
13370 def_builtin ("__builtin_vec_ext_v2di", ftype, VSX_BUILTIN_VEC_EXT_V2DI);
13373 static void
13374 htm_init_builtins (void)
13376 HOST_WIDE_INT builtin_mask = rs6000_builtin_mask;
13377 const struct builtin_description *d;
13378 size_t i;
13380 d = bdesc_htm;
13381 for (i = 0; i < ARRAY_SIZE (bdesc_htm); i++, d++)
13383 tree op[MAX_HTM_OPERANDS], type;
13384 HOST_WIDE_INT mask = d->mask;
13385 unsigned attr = rs6000_builtin_info[d->code].attr;
13386 bool void_func = (attr & RS6000_BTC_VOID);
13387 int attr_args = (attr & RS6000_BTC_TYPE_MASK);
13388 int nopnds = 0;
13389 tree argtype = (attr & RS6000_BTC_SPR) ? long_unsigned_type_node
13390 : unsigned_type_node;
13392 if ((mask & builtin_mask) != mask)
13394 if (TARGET_DEBUG_BUILTIN)
13395 fprintf (stderr, "htm_builtin, skip binary %s\n", d->name);
13396 continue;
13399 if (d->name == 0)
13401 if (TARGET_DEBUG_BUILTIN)
13402 fprintf (stderr, "htm_builtin, bdesc_htm[%ld] no name\n",
13403 (long unsigned) i);
13404 continue;
13407 op[nopnds++] = (void_func) ? void_type_node : argtype;
13409 if (attr_args == RS6000_BTC_UNARY)
13410 op[nopnds++] = argtype;
13411 else if (attr_args == RS6000_BTC_BINARY)
13413 op[nopnds++] = argtype;
13414 op[nopnds++] = argtype;
13416 else if (attr_args == RS6000_BTC_TERNARY)
13418 op[nopnds++] = argtype;
13419 op[nopnds++] = argtype;
13420 op[nopnds++] = argtype;
13423 switch (nopnds)
13425 case 1:
13426 type = build_function_type_list (op[0], NULL_TREE);
13427 break;
13428 case 2:
13429 type = build_function_type_list (op[0], op[1], NULL_TREE);
13430 break;
13431 case 3:
13432 type = build_function_type_list (op[0], op[1], op[2], NULL_TREE);
13433 break;
13434 case 4:
13435 type = build_function_type_list (op[0], op[1], op[2], op[3],
13436 NULL_TREE);
13437 break;
13438 default:
13439 gcc_unreachable ();
13442 def_builtin (d->name, type, d->code);
13446 /* Hash function for builtin functions with up to 3 arguments and a return
13447 type. */
13448 static unsigned
13449 builtin_hash_function (const void *hash_entry)
13451 unsigned ret = 0;
13452 int i;
13453 const struct builtin_hash_struct *bh =
13454 (const struct builtin_hash_struct *) hash_entry;
13456 for (i = 0; i < 4; i++)
13458 ret = (ret * (unsigned)MAX_MACHINE_MODE) + ((unsigned)bh->mode[i]);
13459 ret = (ret * 2) + bh->uns_p[i];
13462 return ret;
13465 /* Compare builtin hash entries H1 and H2 for equivalence. */
13466 static int
13467 builtin_hash_eq (const void *h1, const void *h2)
13469 const struct builtin_hash_struct *p1 = (const struct builtin_hash_struct *) h1;
13470 const struct builtin_hash_struct *p2 = (const struct builtin_hash_struct *) h2;
13472 return ((p1->mode[0] == p2->mode[0])
13473 && (p1->mode[1] == p2->mode[1])
13474 && (p1->mode[2] == p2->mode[2])
13475 && (p1->mode[3] == p2->mode[3])
13476 && (p1->uns_p[0] == p2->uns_p[0])
13477 && (p1->uns_p[1] == p2->uns_p[1])
13478 && (p1->uns_p[2] == p2->uns_p[2])
13479 && (p1->uns_p[3] == p2->uns_p[3]));
13482 /* Map types for builtin functions with an explicit return type and up to 3
13483 arguments. Functions with fewer than 3 arguments use VOIDmode as the type
13484 of the argument. */
13485 static tree
13486 builtin_function_type (enum machine_mode mode_ret, enum machine_mode mode_arg0,
13487 enum machine_mode mode_arg1, enum machine_mode mode_arg2,
13488 enum rs6000_builtins builtin, const char *name)
13490 struct builtin_hash_struct h;
13491 struct builtin_hash_struct *h2;
13492 void **found;
13493 int num_args = 3;
13494 int i;
13495 tree ret_type = NULL_TREE;
13496 tree arg_type[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
13498 /* Create builtin_hash_table. */
13499 if (builtin_hash_table == NULL)
13500 builtin_hash_table = htab_create_ggc (1500, builtin_hash_function,
13501 builtin_hash_eq, NULL);
13503 h.type = NULL_TREE;
13504 h.mode[0] = mode_ret;
13505 h.mode[1] = mode_arg0;
13506 h.mode[2] = mode_arg1;
13507 h.mode[3] = mode_arg2;
13508 h.uns_p[0] = 0;
13509 h.uns_p[1] = 0;
13510 h.uns_p[2] = 0;
13511 h.uns_p[3] = 0;
13513 /* If the builtin is a type that produces unsigned results or takes unsigned
13514 arguments, and it is returned as a decl for the vectorizer (such as
13515 widening multiplies, permute), make sure the arguments and return value
13516 are type correct. */
13517 switch (builtin)
13519 /* unsigned 1 argument functions. */
13520 case CRYPTO_BUILTIN_VSBOX:
13521 case P8V_BUILTIN_VGBBD:
13522 h.uns_p[0] = 1;
13523 h.uns_p[1] = 1;
13524 break;
13526 /* unsigned 2 argument functions. */
13527 case ALTIVEC_BUILTIN_VMULEUB_UNS:
13528 case ALTIVEC_BUILTIN_VMULEUH_UNS:
13529 case ALTIVEC_BUILTIN_VMULOUB_UNS:
13530 case ALTIVEC_BUILTIN_VMULOUH_UNS:
13531 case CRYPTO_BUILTIN_VCIPHER:
13532 case CRYPTO_BUILTIN_VCIPHERLAST:
13533 case CRYPTO_BUILTIN_VNCIPHER:
13534 case CRYPTO_BUILTIN_VNCIPHERLAST:
13535 case CRYPTO_BUILTIN_VPMSUMB:
13536 case CRYPTO_BUILTIN_VPMSUMH:
13537 case CRYPTO_BUILTIN_VPMSUMW:
13538 case CRYPTO_BUILTIN_VPMSUMD:
13539 case CRYPTO_BUILTIN_VPMSUM:
13540 h.uns_p[0] = 1;
13541 h.uns_p[1] = 1;
13542 h.uns_p[2] = 1;
13543 break;
13545 /* unsigned 3 argument functions. */
13546 case ALTIVEC_BUILTIN_VPERM_16QI_UNS:
13547 case ALTIVEC_BUILTIN_VPERM_8HI_UNS:
13548 case ALTIVEC_BUILTIN_VPERM_4SI_UNS:
13549 case ALTIVEC_BUILTIN_VPERM_2DI_UNS:
13550 case ALTIVEC_BUILTIN_VSEL_16QI_UNS:
13551 case ALTIVEC_BUILTIN_VSEL_8HI_UNS:
13552 case ALTIVEC_BUILTIN_VSEL_4SI_UNS:
13553 case ALTIVEC_BUILTIN_VSEL_2DI_UNS:
13554 case VSX_BUILTIN_VPERM_16QI_UNS:
13555 case VSX_BUILTIN_VPERM_8HI_UNS:
13556 case VSX_BUILTIN_VPERM_4SI_UNS:
13557 case VSX_BUILTIN_VPERM_2DI_UNS:
13558 case VSX_BUILTIN_XXSEL_16QI_UNS:
13559 case VSX_BUILTIN_XXSEL_8HI_UNS:
13560 case VSX_BUILTIN_XXSEL_4SI_UNS:
13561 case VSX_BUILTIN_XXSEL_2DI_UNS:
13562 case CRYPTO_BUILTIN_VPERMXOR:
13563 case CRYPTO_BUILTIN_VPERMXOR_V2DI:
13564 case CRYPTO_BUILTIN_VPERMXOR_V4SI:
13565 case CRYPTO_BUILTIN_VPERMXOR_V8HI:
13566 case CRYPTO_BUILTIN_VPERMXOR_V16QI:
13567 case CRYPTO_BUILTIN_VSHASIGMAW:
13568 case CRYPTO_BUILTIN_VSHASIGMAD:
13569 case CRYPTO_BUILTIN_VSHASIGMA:
13570 h.uns_p[0] = 1;
13571 h.uns_p[1] = 1;
13572 h.uns_p[2] = 1;
13573 h.uns_p[3] = 1;
13574 break;
13576 /* signed permute functions with unsigned char mask. */
13577 case ALTIVEC_BUILTIN_VPERM_16QI:
13578 case ALTIVEC_BUILTIN_VPERM_8HI:
13579 case ALTIVEC_BUILTIN_VPERM_4SI:
13580 case ALTIVEC_BUILTIN_VPERM_4SF:
13581 case ALTIVEC_BUILTIN_VPERM_2DI:
13582 case ALTIVEC_BUILTIN_VPERM_2DF:
13583 case VSX_BUILTIN_VPERM_16QI:
13584 case VSX_BUILTIN_VPERM_8HI:
13585 case VSX_BUILTIN_VPERM_4SI:
13586 case VSX_BUILTIN_VPERM_4SF:
13587 case VSX_BUILTIN_VPERM_2DI:
13588 case VSX_BUILTIN_VPERM_2DF:
13589 h.uns_p[3] = 1;
13590 break;
13592 /* unsigned args, signed return. */
13593 case VSX_BUILTIN_XVCVUXDDP_UNS:
13594 case ALTIVEC_BUILTIN_UNSFLOAT_V4SI_V4SF:
13595 h.uns_p[1] = 1;
13596 break;
13598 /* signed args, unsigned return. */
13599 case VSX_BUILTIN_XVCVDPUXDS_UNS:
13600 case ALTIVEC_BUILTIN_FIXUNS_V4SF_V4SI:
13601 h.uns_p[0] = 1;
13602 break;
13604 default:
13605 break;
13608 /* Figure out how many args are present. */
13609 while (num_args > 0 && h.mode[num_args] == VOIDmode)
13610 num_args--;
13612 if (num_args == 0)
13613 fatal_error ("internal error: builtin function %s had no type", name);
13615 ret_type = builtin_mode_to_type[h.mode[0]][h.uns_p[0]];
13616 if (!ret_type && h.uns_p[0])
13617 ret_type = builtin_mode_to_type[h.mode[0]][0];
13619 if (!ret_type)
13620 fatal_error ("internal error: builtin function %s had an unexpected "
13621 "return type %s", name, GET_MODE_NAME (h.mode[0]));
13623 for (i = 0; i < (int) ARRAY_SIZE (arg_type); i++)
13624 arg_type[i] = NULL_TREE;
13626 for (i = 0; i < num_args; i++)
13628 int m = (int) h.mode[i+1];
13629 int uns_p = h.uns_p[i+1];
13631 arg_type[i] = builtin_mode_to_type[m][uns_p];
13632 if (!arg_type[i] && uns_p)
13633 arg_type[i] = builtin_mode_to_type[m][0];
13635 if (!arg_type[i])
13636 fatal_error ("internal error: builtin function %s, argument %d "
13637 "had unexpected argument type %s", name, i,
13638 GET_MODE_NAME (m));
13641 found = htab_find_slot (builtin_hash_table, &h, INSERT);
13642 if (*found == NULL)
13644 h2 = ggc_alloc_builtin_hash_struct ();
13645 *h2 = h;
13646 *found = (void *)h2;
13648 h2->type = build_function_type_list (ret_type, arg_type[0], arg_type[1],
13649 arg_type[2], NULL_TREE);
13652 return ((struct builtin_hash_struct *)(*found))->type;
13655 static void
13656 rs6000_common_init_builtins (void)
13658 const struct builtin_description *d;
13659 size_t i;
13661 tree opaque_ftype_opaque = NULL_TREE;
13662 tree opaque_ftype_opaque_opaque = NULL_TREE;
13663 tree opaque_ftype_opaque_opaque_opaque = NULL_TREE;
13664 tree v2si_ftype_qi = NULL_TREE;
13665 tree v2si_ftype_v2si_qi = NULL_TREE;
13666 tree v2si_ftype_int_qi = NULL_TREE;
13667 HOST_WIDE_INT builtin_mask = rs6000_builtin_mask;
13669 if (!TARGET_PAIRED_FLOAT)
13671 builtin_mode_to_type[V2SImode][0] = opaque_V2SI_type_node;
13672 builtin_mode_to_type[V2SFmode][0] = opaque_V2SF_type_node;
13675 /* Paired and SPE builtins are only available if you build a compiler with
13676 the appropriate options, so only create those builtins with the
13677 appropriate compiler option. Create Altivec and VSX builtins on machines
13678 with at least the general purpose extensions (970 and newer) to allow the
13679 use of the target attribute.. */
13681 if (TARGET_EXTRA_BUILTINS)
13682 builtin_mask |= RS6000_BTM_COMMON;
13684 /* Add the ternary operators. */
13685 d = bdesc_3arg;
13686 for (i = 0; i < ARRAY_SIZE (bdesc_3arg); i++, d++)
13688 tree type;
13689 HOST_WIDE_INT mask = d->mask;
13691 if ((mask & builtin_mask) != mask)
13693 if (TARGET_DEBUG_BUILTIN)
13694 fprintf (stderr, "rs6000_builtin, skip ternary %s\n", d->name);
13695 continue;
13698 if (rs6000_overloaded_builtin_p (d->code))
13700 if (! (type = opaque_ftype_opaque_opaque_opaque))
13701 type = opaque_ftype_opaque_opaque_opaque
13702 = build_function_type_list (opaque_V4SI_type_node,
13703 opaque_V4SI_type_node,
13704 opaque_V4SI_type_node,
13705 opaque_V4SI_type_node,
13706 NULL_TREE);
13708 else
13710 enum insn_code icode = d->icode;
13711 if (d->name == 0)
13713 if (TARGET_DEBUG_BUILTIN)
13714 fprintf (stderr, "rs6000_builtin, bdesc_3arg[%ld] no name\n",
13715 (long unsigned)i);
13717 continue;
13720 if (icode == CODE_FOR_nothing)
13722 if (TARGET_DEBUG_BUILTIN)
13723 fprintf (stderr, "rs6000_builtin, skip ternary %s (no code)\n",
13724 d->name);
13726 continue;
13729 type = builtin_function_type (insn_data[icode].operand[0].mode,
13730 insn_data[icode].operand[1].mode,
13731 insn_data[icode].operand[2].mode,
13732 insn_data[icode].operand[3].mode,
13733 d->code, d->name);
13736 def_builtin (d->name, type, d->code);
13739 /* Add the binary operators. */
13740 d = bdesc_2arg;
13741 for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
13743 enum machine_mode mode0, mode1, mode2;
13744 tree type;
13745 HOST_WIDE_INT mask = d->mask;
13747 if ((mask & builtin_mask) != mask)
13749 if (TARGET_DEBUG_BUILTIN)
13750 fprintf (stderr, "rs6000_builtin, skip binary %s\n", d->name);
13751 continue;
13754 if (rs6000_overloaded_builtin_p (d->code))
13756 if (! (type = opaque_ftype_opaque_opaque))
13757 type = opaque_ftype_opaque_opaque
13758 = build_function_type_list (opaque_V4SI_type_node,
13759 opaque_V4SI_type_node,
13760 opaque_V4SI_type_node,
13761 NULL_TREE);
13763 else
13765 enum insn_code icode = d->icode;
13766 if (d->name == 0)
13768 if (TARGET_DEBUG_BUILTIN)
13769 fprintf (stderr, "rs6000_builtin, bdesc_2arg[%ld] no name\n",
13770 (long unsigned)i);
13772 continue;
13775 if (icode == CODE_FOR_nothing)
13777 if (TARGET_DEBUG_BUILTIN)
13778 fprintf (stderr, "rs6000_builtin, skip binary %s (no code)\n",
13779 d->name);
13781 continue;
13784 mode0 = insn_data[icode].operand[0].mode;
13785 mode1 = insn_data[icode].operand[1].mode;
13786 mode2 = insn_data[icode].operand[2].mode;
13788 if (mode0 == V2SImode && mode1 == V2SImode && mode2 == QImode)
13790 if (! (type = v2si_ftype_v2si_qi))
13791 type = v2si_ftype_v2si_qi
13792 = build_function_type_list (opaque_V2SI_type_node,
13793 opaque_V2SI_type_node,
13794 char_type_node,
13795 NULL_TREE);
13798 else if (mode0 == V2SImode && GET_MODE_CLASS (mode1) == MODE_INT
13799 && mode2 == QImode)
13801 if (! (type = v2si_ftype_int_qi))
13802 type = v2si_ftype_int_qi
13803 = build_function_type_list (opaque_V2SI_type_node,
13804 integer_type_node,
13805 char_type_node,
13806 NULL_TREE);
13809 else
13810 type = builtin_function_type (mode0, mode1, mode2, VOIDmode,
13811 d->code, d->name);
13814 def_builtin (d->name, type, d->code);
13817 /* Add the simple unary operators. */
13818 d = bdesc_1arg;
13819 for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
13821 enum machine_mode mode0, mode1;
13822 tree type;
13823 HOST_WIDE_INT mask = d->mask;
13825 if ((mask & builtin_mask) != mask)
13827 if (TARGET_DEBUG_BUILTIN)
13828 fprintf (stderr, "rs6000_builtin, skip unary %s\n", d->name);
13829 continue;
13832 if (rs6000_overloaded_builtin_p (d->code))
13834 if (! (type = opaque_ftype_opaque))
13835 type = opaque_ftype_opaque
13836 = build_function_type_list (opaque_V4SI_type_node,
13837 opaque_V4SI_type_node,
13838 NULL_TREE);
13840 else
13842 enum insn_code icode = d->icode;
13843 if (d->name == 0)
13845 if (TARGET_DEBUG_BUILTIN)
13846 fprintf (stderr, "rs6000_builtin, bdesc_1arg[%ld] no name\n",
13847 (long unsigned)i);
13849 continue;
13852 if (icode == CODE_FOR_nothing)
13854 if (TARGET_DEBUG_BUILTIN)
13855 fprintf (stderr, "rs6000_builtin, skip unary %s (no code)\n",
13856 d->name);
13858 continue;
13861 mode0 = insn_data[icode].operand[0].mode;
13862 mode1 = insn_data[icode].operand[1].mode;
13864 if (mode0 == V2SImode && mode1 == QImode)
13866 if (! (type = v2si_ftype_qi))
13867 type = v2si_ftype_qi
13868 = build_function_type_list (opaque_V2SI_type_node,
13869 char_type_node,
13870 NULL_TREE);
13873 else
13874 type = builtin_function_type (mode0, mode1, VOIDmode, VOIDmode,
13875 d->code, d->name);
13878 def_builtin (d->name, type, d->code);
13882 static void
13883 rs6000_init_libfuncs (void)
13885 if (!TARGET_IEEEQUAD)
13886 /* AIX/Darwin/64-bit Linux quad floating point routines. */
13887 if (!TARGET_XL_COMPAT)
13889 set_optab_libfunc (add_optab, TFmode, "__gcc_qadd");
13890 set_optab_libfunc (sub_optab, TFmode, "__gcc_qsub");
13891 set_optab_libfunc (smul_optab, TFmode, "__gcc_qmul");
13892 set_optab_libfunc (sdiv_optab, TFmode, "__gcc_qdiv");
13894 if (!(TARGET_HARD_FLOAT && (TARGET_FPRS || TARGET_E500_DOUBLE)))
13896 set_optab_libfunc (neg_optab, TFmode, "__gcc_qneg");
13897 set_optab_libfunc (eq_optab, TFmode, "__gcc_qeq");
13898 set_optab_libfunc (ne_optab, TFmode, "__gcc_qne");
13899 set_optab_libfunc (gt_optab, TFmode, "__gcc_qgt");
13900 set_optab_libfunc (ge_optab, TFmode, "__gcc_qge");
13901 set_optab_libfunc (lt_optab, TFmode, "__gcc_qlt");
13902 set_optab_libfunc (le_optab, TFmode, "__gcc_qle");
13904 set_conv_libfunc (sext_optab, TFmode, SFmode, "__gcc_stoq");
13905 set_conv_libfunc (sext_optab, TFmode, DFmode, "__gcc_dtoq");
13906 set_conv_libfunc (trunc_optab, SFmode, TFmode, "__gcc_qtos");
13907 set_conv_libfunc (trunc_optab, DFmode, TFmode, "__gcc_qtod");
13908 set_conv_libfunc (sfix_optab, SImode, TFmode, "__gcc_qtoi");
13909 set_conv_libfunc (ufix_optab, SImode, TFmode, "__gcc_qtou");
13910 set_conv_libfunc (sfloat_optab, TFmode, SImode, "__gcc_itoq");
13911 set_conv_libfunc (ufloat_optab, TFmode, SImode, "__gcc_utoq");
13914 if (!(TARGET_HARD_FLOAT && TARGET_FPRS))
13915 set_optab_libfunc (unord_optab, TFmode, "__gcc_qunord");
13917 else
13919 set_optab_libfunc (add_optab, TFmode, "_xlqadd");
13920 set_optab_libfunc (sub_optab, TFmode, "_xlqsub");
13921 set_optab_libfunc (smul_optab, TFmode, "_xlqmul");
13922 set_optab_libfunc (sdiv_optab, TFmode, "_xlqdiv");
13924 else
13926 /* 32-bit SVR4 quad floating point routines. */
13928 set_optab_libfunc (add_optab, TFmode, "_q_add");
13929 set_optab_libfunc (sub_optab, TFmode, "_q_sub");
13930 set_optab_libfunc (neg_optab, TFmode, "_q_neg");
13931 set_optab_libfunc (smul_optab, TFmode, "_q_mul");
13932 set_optab_libfunc (sdiv_optab, TFmode, "_q_div");
13933 if (TARGET_PPC_GPOPT)
13934 set_optab_libfunc (sqrt_optab, TFmode, "_q_sqrt");
13936 set_optab_libfunc (eq_optab, TFmode, "_q_feq");
13937 set_optab_libfunc (ne_optab, TFmode, "_q_fne");
13938 set_optab_libfunc (gt_optab, TFmode, "_q_fgt");
13939 set_optab_libfunc (ge_optab, TFmode, "_q_fge");
13940 set_optab_libfunc (lt_optab, TFmode, "_q_flt");
13941 set_optab_libfunc (le_optab, TFmode, "_q_fle");
13943 set_conv_libfunc (sext_optab, TFmode, SFmode, "_q_stoq");
13944 set_conv_libfunc (sext_optab, TFmode, DFmode, "_q_dtoq");
13945 set_conv_libfunc (trunc_optab, SFmode, TFmode, "_q_qtos");
13946 set_conv_libfunc (trunc_optab, DFmode, TFmode, "_q_qtod");
13947 set_conv_libfunc (sfix_optab, SImode, TFmode, "_q_qtoi");
13948 set_conv_libfunc (ufix_optab, SImode, TFmode, "_q_qtou");
13949 set_conv_libfunc (sfloat_optab, TFmode, SImode, "_q_itoq");
13950 set_conv_libfunc (ufloat_optab, TFmode, SImode, "_q_utoq");
13955 /* Expand a block clear operation, and return 1 if successful. Return 0
13956 if we should let the compiler generate normal code.
13958 operands[0] is the destination
13959 operands[1] is the length
13960 operands[3] is the alignment */
13963 expand_block_clear (rtx operands[])
13965 rtx orig_dest = operands[0];
13966 rtx bytes_rtx = operands[1];
13967 rtx align_rtx = operands[3];
13968 bool constp = (GET_CODE (bytes_rtx) == CONST_INT);
13969 HOST_WIDE_INT align;
13970 HOST_WIDE_INT bytes;
13971 int offset;
13972 int clear_bytes;
13973 int clear_step;
13975 /* If this is not a fixed size move, just call memcpy */
13976 if (! constp)
13977 return 0;
13979 /* This must be a fixed size alignment */
13980 gcc_assert (GET_CODE (align_rtx) == CONST_INT);
13981 align = INTVAL (align_rtx) * BITS_PER_UNIT;
13983 /* Anything to clear? */
13984 bytes = INTVAL (bytes_rtx);
13985 if (bytes <= 0)
13986 return 1;
13988 /* Use the builtin memset after a point, to avoid huge code bloat.
13989 When optimize_size, avoid any significant code bloat; calling
13990 memset is about 4 instructions, so allow for one instruction to
13991 load zero and three to do clearing. */
13992 if (TARGET_ALTIVEC && align >= 128)
13993 clear_step = 16;
13994 else if (TARGET_POWERPC64 && align >= 32)
13995 clear_step = 8;
13996 else if (TARGET_SPE && align >= 64)
13997 clear_step = 8;
13998 else
13999 clear_step = 4;
14001 if (optimize_size && bytes > 3 * clear_step)
14002 return 0;
14003 if (! optimize_size && bytes > 8 * clear_step)
14004 return 0;
14006 for (offset = 0; bytes > 0; offset += clear_bytes, bytes -= clear_bytes)
14008 enum machine_mode mode = BLKmode;
14009 rtx dest;
14011 if (bytes >= 16 && TARGET_ALTIVEC && align >= 128)
14013 clear_bytes = 16;
14014 mode = V4SImode;
14016 else if (bytes >= 8 && TARGET_SPE && align >= 64)
14018 clear_bytes = 8;
14019 mode = V2SImode;
14021 else if (bytes >= 8 && TARGET_POWERPC64
14022 /* 64-bit loads and stores require word-aligned
14023 displacements. */
14024 && (align >= 64 || (!STRICT_ALIGNMENT && align >= 32)))
14026 clear_bytes = 8;
14027 mode = DImode;
14029 else if (bytes >= 4 && (align >= 32 || !STRICT_ALIGNMENT))
14030 { /* move 4 bytes */
14031 clear_bytes = 4;
14032 mode = SImode;
14034 else if (bytes >= 2 && (align >= 16 || !STRICT_ALIGNMENT))
14035 { /* move 2 bytes */
14036 clear_bytes = 2;
14037 mode = HImode;
14039 else /* move 1 byte at a time */
14041 clear_bytes = 1;
14042 mode = QImode;
14045 dest = adjust_address (orig_dest, mode, offset);
14047 emit_move_insn (dest, CONST0_RTX (mode));
14050 return 1;
14054 /* Expand a block move operation, and return 1 if successful. Return 0
14055 if we should let the compiler generate normal code.
14057 operands[0] is the destination
14058 operands[1] is the source
14059 operands[2] is the length
14060 operands[3] is the alignment */
14062 #define MAX_MOVE_REG 4
14065 expand_block_move (rtx operands[])
14067 rtx orig_dest = operands[0];
14068 rtx orig_src = operands[1];
14069 rtx bytes_rtx = operands[2];
14070 rtx align_rtx = operands[3];
14071 int constp = (GET_CODE (bytes_rtx) == CONST_INT);
14072 int align;
14073 int bytes;
14074 int offset;
14075 int move_bytes;
14076 rtx stores[MAX_MOVE_REG];
14077 int num_reg = 0;
14079 /* If this is not a fixed size move, just call memcpy */
14080 if (! constp)
14081 return 0;
14083 /* This must be a fixed size alignment */
14084 gcc_assert (GET_CODE (align_rtx) == CONST_INT);
14085 align = INTVAL (align_rtx) * BITS_PER_UNIT;
14087 /* Anything to move? */
14088 bytes = INTVAL (bytes_rtx);
14089 if (bytes <= 0)
14090 return 1;
14092 if (bytes > rs6000_block_move_inline_limit)
14093 return 0;
14095 for (offset = 0; bytes > 0; offset += move_bytes, bytes -= move_bytes)
14097 union {
14098 rtx (*movmemsi) (rtx, rtx, rtx, rtx);
14099 rtx (*mov) (rtx, rtx);
14100 } gen_func;
14101 enum machine_mode mode = BLKmode;
14102 rtx src, dest;
14104 /* Altivec first, since it will be faster than a string move
14105 when it applies, and usually not significantly larger. */
14106 if (TARGET_ALTIVEC && bytes >= 16 && align >= 128)
14108 move_bytes = 16;
14109 mode = V4SImode;
14110 gen_func.mov = gen_movv4si;
14112 else if (TARGET_SPE && bytes >= 8 && align >= 64)
14114 move_bytes = 8;
14115 mode = V2SImode;
14116 gen_func.mov = gen_movv2si;
14118 else if (TARGET_STRING
14119 && bytes > 24 /* move up to 32 bytes at a time */
14120 && ! fixed_regs[5]
14121 && ! fixed_regs[6]
14122 && ! fixed_regs[7]
14123 && ! fixed_regs[8]
14124 && ! fixed_regs[9]
14125 && ! fixed_regs[10]
14126 && ! fixed_regs[11]
14127 && ! fixed_regs[12])
14129 move_bytes = (bytes > 32) ? 32 : bytes;
14130 gen_func.movmemsi = gen_movmemsi_8reg;
14132 else if (TARGET_STRING
14133 && bytes > 16 /* move up to 24 bytes at a time */
14134 && ! fixed_regs[5]
14135 && ! fixed_regs[6]
14136 && ! fixed_regs[7]
14137 && ! fixed_regs[8]
14138 && ! fixed_regs[9]
14139 && ! fixed_regs[10])
14141 move_bytes = (bytes > 24) ? 24 : bytes;
14142 gen_func.movmemsi = gen_movmemsi_6reg;
14144 else if (TARGET_STRING
14145 && bytes > 8 /* move up to 16 bytes at a time */
14146 && ! fixed_regs[5]
14147 && ! fixed_regs[6]
14148 && ! fixed_regs[7]
14149 && ! fixed_regs[8])
14151 move_bytes = (bytes > 16) ? 16 : bytes;
14152 gen_func.movmemsi = gen_movmemsi_4reg;
14154 else if (bytes >= 8 && TARGET_POWERPC64
14155 /* 64-bit loads and stores require word-aligned
14156 displacements. */
14157 && (align >= 64 || (!STRICT_ALIGNMENT && align >= 32)))
14159 move_bytes = 8;
14160 mode = DImode;
14161 gen_func.mov = gen_movdi;
14163 else if (TARGET_STRING && bytes > 4 && !TARGET_POWERPC64)
14164 { /* move up to 8 bytes at a time */
14165 move_bytes = (bytes > 8) ? 8 : bytes;
14166 gen_func.movmemsi = gen_movmemsi_2reg;
14168 else if (bytes >= 4 && (align >= 32 || !STRICT_ALIGNMENT))
14169 { /* move 4 bytes */
14170 move_bytes = 4;
14171 mode = SImode;
14172 gen_func.mov = gen_movsi;
14174 else if (bytes >= 2 && (align >= 16 || !STRICT_ALIGNMENT))
14175 { /* move 2 bytes */
14176 move_bytes = 2;
14177 mode = HImode;
14178 gen_func.mov = gen_movhi;
14180 else if (TARGET_STRING && bytes > 1)
14181 { /* move up to 4 bytes at a time */
14182 move_bytes = (bytes > 4) ? 4 : bytes;
14183 gen_func.movmemsi = gen_movmemsi_1reg;
14185 else /* move 1 byte at a time */
14187 move_bytes = 1;
14188 mode = QImode;
14189 gen_func.mov = gen_movqi;
14192 src = adjust_address (orig_src, mode, offset);
14193 dest = adjust_address (orig_dest, mode, offset);
14195 if (mode != BLKmode)
14197 rtx tmp_reg = gen_reg_rtx (mode);
14199 emit_insn ((*gen_func.mov) (tmp_reg, src));
14200 stores[num_reg++] = (*gen_func.mov) (dest, tmp_reg);
14203 if (mode == BLKmode || num_reg >= MAX_MOVE_REG || bytes == move_bytes)
14205 int i;
14206 for (i = 0; i < num_reg; i++)
14207 emit_insn (stores[i]);
14208 num_reg = 0;
14211 if (mode == BLKmode)
14213 /* Move the address into scratch registers. The movmemsi
14214 patterns require zero offset. */
14215 if (!REG_P (XEXP (src, 0)))
14217 rtx src_reg = copy_addr_to_reg (XEXP (src, 0));
14218 src = replace_equiv_address (src, src_reg);
14220 set_mem_size (src, move_bytes);
14222 if (!REG_P (XEXP (dest, 0)))
14224 rtx dest_reg = copy_addr_to_reg (XEXP (dest, 0));
14225 dest = replace_equiv_address (dest, dest_reg);
14227 set_mem_size (dest, move_bytes);
14229 emit_insn ((*gen_func.movmemsi) (dest, src,
14230 GEN_INT (move_bytes & 31),
14231 align_rtx));
14235 return 1;
14239 /* Return a string to perform a load_multiple operation.
14240 operands[0] is the vector.
14241 operands[1] is the source address.
14242 operands[2] is the first destination register. */
14244 const char *
14245 rs6000_output_load_multiple (rtx operands[3])
14247 /* We have to handle the case where the pseudo used to contain the address
14248 is assigned to one of the output registers. */
14249 int i, j;
14250 int words = XVECLEN (operands[0], 0);
14251 rtx xop[10];
14253 if (XVECLEN (operands[0], 0) == 1)
14254 return "lwz %2,0(%1)";
14256 for (i = 0; i < words; i++)
14257 if (refers_to_regno_p (REGNO (operands[2]) + i,
14258 REGNO (operands[2]) + i + 1, operands[1], 0))
14260 if (i == words-1)
14262 xop[0] = GEN_INT (4 * (words-1));
14263 xop[1] = operands[1];
14264 xop[2] = operands[2];
14265 output_asm_insn ("lswi %2,%1,%0\n\tlwz %1,%0(%1)", xop);
14266 return "";
14268 else if (i == 0)
14270 xop[0] = GEN_INT (4 * (words-1));
14271 xop[1] = operands[1];
14272 xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
14273 output_asm_insn ("addi %1,%1,4\n\tlswi %2,%1,%0\n\tlwz %1,-4(%1)", xop);
14274 return "";
14276 else
14278 for (j = 0; j < words; j++)
14279 if (j != i)
14281 xop[0] = GEN_INT (j * 4);
14282 xop[1] = operands[1];
14283 xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + j);
14284 output_asm_insn ("lwz %2,%0(%1)", xop);
14286 xop[0] = GEN_INT (i * 4);
14287 xop[1] = operands[1];
14288 output_asm_insn ("lwz %1,%0(%1)", xop);
14289 return "";
14293 return "lswi %2,%1,%N0";
14297 /* A validation routine: say whether CODE, a condition code, and MODE
14298 match. The other alternatives either don't make sense or should
14299 never be generated. */
14301 void
14302 validate_condition_mode (enum rtx_code code, enum machine_mode mode)
14304 gcc_assert ((GET_RTX_CLASS (code) == RTX_COMPARE
14305 || GET_RTX_CLASS (code) == RTX_COMM_COMPARE)
14306 && GET_MODE_CLASS (mode) == MODE_CC);
14308 /* These don't make sense. */
14309 gcc_assert ((code != GT && code != LT && code != GE && code != LE)
14310 || mode != CCUNSmode);
14312 gcc_assert ((code != GTU && code != LTU && code != GEU && code != LEU)
14313 || mode == CCUNSmode);
14315 gcc_assert (mode == CCFPmode
14316 || (code != ORDERED && code != UNORDERED
14317 && code != UNEQ && code != LTGT
14318 && code != UNGT && code != UNLT
14319 && code != UNGE && code != UNLE));
14321 /* These should never be generated except for
14322 flag_finite_math_only. */
14323 gcc_assert (mode != CCFPmode
14324 || flag_finite_math_only
14325 || (code != LE && code != GE
14326 && code != UNEQ && code != LTGT
14327 && code != UNGT && code != UNLT));
14329 /* These are invalid; the information is not there. */
14330 gcc_assert (mode != CCEQmode || code == EQ || code == NE);
14334 /* Return 1 if ANDOP is a mask that has no bits on that are not in the
14335 mask required to convert the result of a rotate insn into a shift
14336 left insn of SHIFTOP bits. Both are known to be SImode CONST_INT. */
14339 includes_lshift_p (rtx shiftop, rtx andop)
14341 unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
14343 shift_mask <<= INTVAL (shiftop);
14345 return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
14348 /* Similar, but for right shift. */
14351 includes_rshift_p (rtx shiftop, rtx andop)
14353 unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
14355 shift_mask >>= INTVAL (shiftop);
14357 return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
14360 /* Return 1 if ANDOP is a mask suitable for use with an rldic insn
14361 to perform a left shift. It must have exactly SHIFTOP least
14362 significant 0's, then one or more 1's, then zero or more 0's. */
14365 includes_rldic_lshift_p (rtx shiftop, rtx andop)
14367 if (GET_CODE (andop) == CONST_INT)
14369 HOST_WIDE_INT c, lsb, shift_mask;
14371 c = INTVAL (andop);
14372 if (c == 0 || c == ~0)
14373 return 0;
14375 shift_mask = ~0;
14376 shift_mask <<= INTVAL (shiftop);
14378 /* Find the least significant one bit. */
14379 lsb = c & -c;
14381 /* It must coincide with the LSB of the shift mask. */
14382 if (-lsb != shift_mask)
14383 return 0;
14385 /* Invert to look for the next transition (if any). */
14386 c = ~c;
14388 /* Remove the low group of ones (originally low group of zeros). */
14389 c &= -lsb;
14391 /* Again find the lsb, and check we have all 1's above. */
14392 lsb = c & -c;
14393 return c == -lsb;
14395 else
14396 return 0;
14399 /* Return 1 if ANDOP is a mask suitable for use with an rldicr insn
14400 to perform a left shift. It must have SHIFTOP or more least
14401 significant 0's, with the remainder of the word 1's. */
14404 includes_rldicr_lshift_p (rtx shiftop, rtx andop)
14406 if (GET_CODE (andop) == CONST_INT)
14408 HOST_WIDE_INT c, lsb, shift_mask;
14410 shift_mask = ~0;
14411 shift_mask <<= INTVAL (shiftop);
14412 c = INTVAL (andop);
14414 /* Find the least significant one bit. */
14415 lsb = c & -c;
14417 /* It must be covered by the shift mask.
14418 This test also rejects c == 0. */
14419 if ((lsb & shift_mask) == 0)
14420 return 0;
14422 /* Check we have all 1's above the transition, and reject all 1's. */
14423 return c == -lsb && lsb != 1;
14425 else
14426 return 0;
14429 /* Return 1 if operands will generate a valid arguments to rlwimi
14430 instruction for insert with right shift in 64-bit mode. The mask may
14431 not start on the first bit or stop on the last bit because wrap-around
14432 effects of instruction do not correspond to semantics of RTL insn. */
14435 insvdi_rshift_rlwimi_p (rtx sizeop, rtx startop, rtx shiftop)
14437 if (INTVAL (startop) > 32
14438 && INTVAL (startop) < 64
14439 && INTVAL (sizeop) > 1
14440 && INTVAL (sizeop) + INTVAL (startop) < 64
14441 && INTVAL (shiftop) > 0
14442 && INTVAL (sizeop) + INTVAL (shiftop) < 32
14443 && (64 - (INTVAL (shiftop) & 63)) >= INTVAL (sizeop))
14444 return 1;
14446 return 0;
14449 /* Return 1 if REGNO (reg1) == REGNO (reg2) - 1 making them candidates
14450 for lfq and stfq insns iff the registers are hard registers. */
14453 registers_ok_for_quad_peep (rtx reg1, rtx reg2)
14455 /* We might have been passed a SUBREG. */
14456 if (GET_CODE (reg1) != REG || GET_CODE (reg2) != REG)
14457 return 0;
14459 /* We might have been passed non floating point registers. */
14460 if (!FP_REGNO_P (REGNO (reg1))
14461 || !FP_REGNO_P (REGNO (reg2)))
14462 return 0;
14464 return (REGNO (reg1) == REGNO (reg2) - 1);
14467 /* Return 1 if addr1 and addr2 are suitable for lfq or stfq insn.
14468 addr1 and addr2 must be in consecutive memory locations
14469 (addr2 == addr1 + 8). */
14472 mems_ok_for_quad_peep (rtx mem1, rtx mem2)
14474 rtx addr1, addr2;
14475 unsigned int reg1, reg2;
14476 int offset1, offset2;
14478 /* The mems cannot be volatile. */
14479 if (MEM_VOLATILE_P (mem1) || MEM_VOLATILE_P (mem2))
14480 return 0;
14482 addr1 = XEXP (mem1, 0);
14483 addr2 = XEXP (mem2, 0);
14485 /* Extract an offset (if used) from the first addr. */
14486 if (GET_CODE (addr1) == PLUS)
14488 /* If not a REG, return zero. */
14489 if (GET_CODE (XEXP (addr1, 0)) != REG)
14490 return 0;
14491 else
14493 reg1 = REGNO (XEXP (addr1, 0));
14494 /* The offset must be constant! */
14495 if (GET_CODE (XEXP (addr1, 1)) != CONST_INT)
14496 return 0;
14497 offset1 = INTVAL (XEXP (addr1, 1));
14500 else if (GET_CODE (addr1) != REG)
14501 return 0;
14502 else
14504 reg1 = REGNO (addr1);
14505 /* This was a simple (mem (reg)) expression. Offset is 0. */
14506 offset1 = 0;
14509 /* And now for the second addr. */
14510 if (GET_CODE (addr2) == PLUS)
14512 /* If not a REG, return zero. */
14513 if (GET_CODE (XEXP (addr2, 0)) != REG)
14514 return 0;
14515 else
14517 reg2 = REGNO (XEXP (addr2, 0));
14518 /* The offset must be constant. */
14519 if (GET_CODE (XEXP (addr2, 1)) != CONST_INT)
14520 return 0;
14521 offset2 = INTVAL (XEXP (addr2, 1));
14524 else if (GET_CODE (addr2) != REG)
14525 return 0;
14526 else
14528 reg2 = REGNO (addr2);
14529 /* This was a simple (mem (reg)) expression. Offset is 0. */
14530 offset2 = 0;
14533 /* Both of these must have the same base register. */
14534 if (reg1 != reg2)
14535 return 0;
14537 /* The offset for the second addr must be 8 more than the first addr. */
14538 if (offset2 != offset1 + 8)
14539 return 0;
14541 /* All the tests passed. addr1 and addr2 are valid for lfq or stfq
14542 instructions. */
14543 return 1;
14548 rs6000_secondary_memory_needed_rtx (enum machine_mode mode)
14550 static bool eliminated = false;
14551 rtx ret;
14553 if (mode != SDmode || TARGET_NO_SDMODE_STACK)
14554 ret = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
14555 else
14557 rtx mem = cfun->machine->sdmode_stack_slot;
14558 gcc_assert (mem != NULL_RTX);
14560 if (!eliminated)
14562 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
14563 cfun->machine->sdmode_stack_slot = mem;
14564 eliminated = true;
14566 ret = mem;
14569 if (TARGET_DEBUG_ADDR)
14571 fprintf (stderr, "\nrs6000_secondary_memory_needed_rtx, mode %s, rtx:\n",
14572 GET_MODE_NAME (mode));
14573 if (!ret)
14574 fprintf (stderr, "\tNULL_RTX\n");
14575 else
14576 debug_rtx (ret);
14579 return ret;
14582 static tree
14583 rs6000_check_sdmode (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
14585 /* Don't walk into types. */
14586 if (*tp == NULL_TREE || *tp == error_mark_node || TYPE_P (*tp))
14588 *walk_subtrees = 0;
14589 return NULL_TREE;
14592 switch (TREE_CODE (*tp))
14594 case VAR_DECL:
14595 case PARM_DECL:
14596 case FIELD_DECL:
14597 case RESULT_DECL:
14598 case SSA_NAME:
14599 case REAL_CST:
14600 case MEM_REF:
14601 case VIEW_CONVERT_EXPR:
14602 if (TYPE_MODE (TREE_TYPE (*tp)) == SDmode)
14603 return *tp;
14604 break;
14605 default:
14606 break;
14609 return NULL_TREE;
14612 /* Classify a register type. Because the FMRGOW/FMRGEW instructions only work
14613 on traditional floating point registers, and the VMRGOW/VMRGEW instructions
14614 only work on the traditional altivec registers, note if an altivec register
14615 was chosen. */
14617 static enum rs6000_reg_type
14618 register_to_reg_type (rtx reg, bool *is_altivec)
14620 HOST_WIDE_INT regno;
14621 enum reg_class rclass;
14623 if (GET_CODE (reg) == SUBREG)
14624 reg = SUBREG_REG (reg);
14626 if (!REG_P (reg))
14627 return NO_REG_TYPE;
14629 regno = REGNO (reg);
14630 if (regno >= FIRST_PSEUDO_REGISTER)
14632 if (!lra_in_progress && !reload_in_progress && !reload_completed)
14633 return PSEUDO_REG_TYPE;
14635 regno = true_regnum (reg);
14636 if (regno < 0 || regno >= FIRST_PSEUDO_REGISTER)
14637 return PSEUDO_REG_TYPE;
14640 gcc_assert (regno >= 0);
14642 if (is_altivec && ALTIVEC_REGNO_P (regno))
14643 *is_altivec = true;
14645 rclass = rs6000_regno_regclass[regno];
14646 return reg_class_to_reg_type[(int)rclass];
14649 /* Helper function for rs6000_secondary_reload to return true if a move to a
14650 different register classe is really a simple move. */
14652 static bool
14653 rs6000_secondary_reload_simple_move (enum rs6000_reg_type to_type,
14654 enum rs6000_reg_type from_type,
14655 enum machine_mode mode)
14657 int size;
14659 /* Add support for various direct moves available. In this function, we only
14660 look at cases where we don't need any extra registers, and one or more
14661 simple move insns are issued. At present, 32-bit integers are not allowed
14662 in FPR/VSX registers. Single precision binary floating is not a simple
14663 move because we need to convert to the single precision memory layout.
14664 The 4-byte SDmode can be moved. */
14665 size = GET_MODE_SIZE (mode);
14666 if (TARGET_DIRECT_MOVE
14667 && ((mode == SDmode) || (TARGET_POWERPC64 && size == 8))
14668 && ((to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
14669 || (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)))
14670 return true;
14672 else if (TARGET_MFPGPR && TARGET_POWERPC64 && size == 8
14673 && ((to_type == GPR_REG_TYPE && from_type == FPR_REG_TYPE)
14674 || (to_type == FPR_REG_TYPE && from_type == GPR_REG_TYPE)))
14675 return true;
14677 else if ((size == 4 || (TARGET_POWERPC64 && size == 8))
14678 && ((to_type == GPR_REG_TYPE && from_type == SPR_REG_TYPE)
14679 || (to_type == SPR_REG_TYPE && from_type == GPR_REG_TYPE)))
14680 return true;
14682 return false;
14685 /* Power8 helper function for rs6000_secondary_reload, handle all of the
14686 special direct moves that involve allocating an extra register, return the
14687 insn code of the helper function if there is such a function or
14688 CODE_FOR_nothing if not. */
14690 static bool
14691 rs6000_secondary_reload_direct_move (enum rs6000_reg_type to_type,
14692 enum rs6000_reg_type from_type,
14693 enum machine_mode mode,
14694 secondary_reload_info *sri,
14695 bool altivec_p)
14697 bool ret = false;
14698 enum insn_code icode = CODE_FOR_nothing;
14699 int cost = 0;
14700 int size = GET_MODE_SIZE (mode);
14702 if (TARGET_POWERPC64)
14704 if (size == 16)
14706 /* Handle moving 128-bit values from GPRs to VSX point registers on
14707 power8 when running in 64-bit mode using XXPERMDI to glue the two
14708 64-bit values back together. */
14709 if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)
14711 cost = 3; /* 2 mtvsrd's, 1 xxpermdi. */
14712 icode = reload_vsx_gpr[(int)mode];
14715 /* Handle moving 128-bit values from VSX point registers to GPRs on
14716 power8 when running in 64-bit mode using XXPERMDI to get access to the
14717 bottom 64-bit value. */
14718 else if (to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
14720 cost = 3; /* 2 mfvsrd's, 1 xxpermdi. */
14721 icode = reload_gpr_vsx[(int)mode];
14725 else if (mode == SFmode)
14727 if (to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
14729 cost = 3; /* xscvdpspn, mfvsrd, and. */
14730 icode = reload_gpr_vsx[(int)mode];
14733 else if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)
14735 cost = 2; /* mtvsrz, xscvspdpn. */
14736 icode = reload_vsx_gpr[(int)mode];
14741 if (TARGET_POWERPC64 && size == 16)
14743 /* Handle moving 128-bit values from GPRs to VSX point registers on
14744 power8 when running in 64-bit mode using XXPERMDI to glue the two
14745 64-bit values back together. */
14746 if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)
14748 cost = 3; /* 2 mtvsrd's, 1 xxpermdi. */
14749 icode = reload_vsx_gpr[(int)mode];
14752 /* Handle moving 128-bit values from VSX point registers to GPRs on
14753 power8 when running in 64-bit mode using XXPERMDI to get access to the
14754 bottom 64-bit value. */
14755 else if (to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
14757 cost = 3; /* 2 mfvsrd's, 1 xxpermdi. */
14758 icode = reload_gpr_vsx[(int)mode];
14762 else if (!TARGET_POWERPC64 && size == 8)
14764 /* Handle moving 64-bit values from GPRs to floating point registers on
14765 power8 when running in 32-bit mode using FMRGOW to glue the two 32-bit
14766 values back together. Altivec register classes must be handled
14767 specially since a different instruction is used, and the secondary
14768 reload support requires a single instruction class in the scratch
14769 register constraint. However, right now TFmode is not allowed in
14770 Altivec registers, so the pattern will never match. */
14771 if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE && !altivec_p)
14773 cost = 3; /* 2 mtvsrwz's, 1 fmrgow. */
14774 icode = reload_fpr_gpr[(int)mode];
14778 if (icode != CODE_FOR_nothing)
14780 ret = true;
14781 if (sri)
14783 sri->icode = icode;
14784 sri->extra_cost = cost;
14788 return ret;
14791 /* Return whether a move between two register classes can be done either
14792 directly (simple move) or via a pattern that uses a single extra temporary
14793 (using power8's direct move in this case. */
14795 static bool
14796 rs6000_secondary_reload_move (enum rs6000_reg_type to_type,
14797 enum rs6000_reg_type from_type,
14798 enum machine_mode mode,
14799 secondary_reload_info *sri,
14800 bool altivec_p)
14802 /* Fall back to load/store reloads if either type is not a register. */
14803 if (to_type == NO_REG_TYPE || from_type == NO_REG_TYPE)
14804 return false;
14806 /* If we haven't allocated registers yet, assume the move can be done for the
14807 standard register types. */
14808 if ((to_type == PSEUDO_REG_TYPE && from_type == PSEUDO_REG_TYPE)
14809 || (to_type == PSEUDO_REG_TYPE && IS_STD_REG_TYPE (from_type))
14810 || (from_type == PSEUDO_REG_TYPE && IS_STD_REG_TYPE (to_type)))
14811 return true;
14813 /* Moves to the same set of registers is a simple move for non-specialized
14814 registers. */
14815 if (to_type == from_type && IS_STD_REG_TYPE (to_type))
14816 return true;
14818 /* Check whether a simple move can be done directly. */
14819 if (rs6000_secondary_reload_simple_move (to_type, from_type, mode))
14821 if (sri)
14823 sri->icode = CODE_FOR_nothing;
14824 sri->extra_cost = 0;
14826 return true;
14829 /* Now check if we can do it in a few steps. */
14830 return rs6000_secondary_reload_direct_move (to_type, from_type, mode, sri,
14831 altivec_p);
14834 /* Inform reload about cases where moving X with a mode MODE to a register in
14835 RCLASS requires an extra scratch or immediate register. Return the class
14836 needed for the immediate register.
14838 For VSX and Altivec, we may need a register to convert sp+offset into
14839 reg+sp.
14841 For misaligned 64-bit gpr loads and stores we need a register to
14842 convert an offset address to indirect. */
14844 static reg_class_t
14845 rs6000_secondary_reload (bool in_p,
14846 rtx x,
14847 reg_class_t rclass_i,
14848 enum machine_mode mode,
14849 secondary_reload_info *sri)
14851 enum reg_class rclass = (enum reg_class) rclass_i;
14852 reg_class_t ret = ALL_REGS;
14853 enum insn_code icode;
14854 bool default_p = false;
14856 sri->icode = CODE_FOR_nothing;
14857 icode = rs6000_vector_reload[mode][in_p != false];
14859 if (REG_P (x) || register_operand (x, mode))
14861 enum rs6000_reg_type to_type = reg_class_to_reg_type[(int)rclass];
14862 bool altivec_p = (rclass == ALTIVEC_REGS);
14863 enum rs6000_reg_type from_type = register_to_reg_type (x, &altivec_p);
14865 if (!in_p)
14867 enum rs6000_reg_type exchange = to_type;
14868 to_type = from_type;
14869 from_type = exchange;
14872 if (rs6000_secondary_reload_move (to_type, from_type, mode, sri,
14873 altivec_p))
14875 icode = (enum insn_code)sri->icode;
14876 default_p = false;
14877 ret = NO_REGS;
14881 /* Handle vector moves with reload helper functions. */
14882 if (ret == ALL_REGS && icode != CODE_FOR_nothing)
14884 ret = NO_REGS;
14885 sri->icode = CODE_FOR_nothing;
14886 sri->extra_cost = 0;
14888 if (GET_CODE (x) == MEM)
14890 rtx addr = XEXP (x, 0);
14892 /* Loads to and stores from gprs can do reg+offset, and wouldn't need
14893 an extra register in that case, but it would need an extra
14894 register if the addressing is reg+reg or (reg+reg)&(-16). Special
14895 case load/store quad. */
14896 if (rclass == GENERAL_REGS || rclass == BASE_REGS)
14898 if (TARGET_POWERPC64 && TARGET_QUAD_MEMORY
14899 && GET_MODE_SIZE (mode) == 16
14900 && quad_memory_operand (x, mode))
14902 sri->icode = icode;
14903 sri->extra_cost = 2;
14906 else if (!legitimate_indirect_address_p (addr, false)
14907 && !rs6000_legitimate_offset_address_p (PTImode, addr,
14908 false, true))
14910 sri->icode = icode;
14911 /* account for splitting the loads, and converting the
14912 address from reg+reg to reg. */
14913 sri->extra_cost = (((TARGET_64BIT) ? 3 : 5)
14914 + ((GET_CODE (addr) == AND) ? 1 : 0));
14917 /* Allow scalar loads to/from the traditional floating point
14918 registers, even if VSX memory is set. */
14919 else if ((rclass == FLOAT_REGS || rclass == NO_REGS)
14920 && (GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8)
14921 && (legitimate_indirect_address_p (addr, false)
14922 || legitimate_indirect_address_p (addr, false)
14923 || rs6000_legitimate_offset_address_p (mode, addr,
14924 false, true)))
14927 /* Loads to and stores from vector registers can only do reg+reg
14928 addressing. Altivec registers can also do (reg+reg)&(-16). Allow
14929 scalar modes loading up the traditional floating point registers
14930 to use offset addresses. */
14931 else if (rclass == VSX_REGS || rclass == ALTIVEC_REGS
14932 || rclass == FLOAT_REGS || rclass == NO_REGS)
14934 if (!VECTOR_MEM_ALTIVEC_P (mode)
14935 && GET_CODE (addr) == AND
14936 && GET_CODE (XEXP (addr, 1)) == CONST_INT
14937 && INTVAL (XEXP (addr, 1)) == -16
14938 && (legitimate_indirect_address_p (XEXP (addr, 0), false)
14939 || legitimate_indexed_address_p (XEXP (addr, 0), false)))
14941 sri->icode = icode;
14942 sri->extra_cost = ((GET_CODE (XEXP (addr, 0)) == PLUS)
14943 ? 2 : 1);
14945 else if (!legitimate_indirect_address_p (addr, false)
14946 && (rclass == NO_REGS
14947 || !legitimate_indexed_address_p (addr, false)))
14949 sri->icode = icode;
14950 sri->extra_cost = 1;
14952 else
14953 icode = CODE_FOR_nothing;
14955 /* Any other loads, including to pseudo registers which haven't been
14956 assigned to a register yet, default to require a scratch
14957 register. */
14958 else
14960 sri->icode = icode;
14961 sri->extra_cost = 2;
14964 else if (REG_P (x))
14966 int regno = true_regnum (x);
14968 icode = CODE_FOR_nothing;
14969 if (regno < 0 || regno >= FIRST_PSEUDO_REGISTER)
14970 default_p = true;
14971 else
14973 enum reg_class xclass = REGNO_REG_CLASS (regno);
14974 enum rs6000_reg_type rtype1 = reg_class_to_reg_type[(int)rclass];
14975 enum rs6000_reg_type rtype2 = reg_class_to_reg_type[(int)xclass];
14977 /* If memory is needed, use default_secondary_reload to create the
14978 stack slot. */
14979 if (rtype1 != rtype2 || !IS_STD_REG_TYPE (rtype1))
14980 default_p = true;
14981 else
14982 ret = NO_REGS;
14985 else
14986 default_p = true;
14988 else if (TARGET_POWERPC64
14989 && reg_class_to_reg_type[(int)rclass] == GPR_REG_TYPE
14990 && MEM_P (x)
14991 && GET_MODE_SIZE (GET_MODE (x)) >= UNITS_PER_WORD)
14993 rtx addr = XEXP (x, 0);
14994 rtx off = address_offset (addr);
14996 if (off != NULL_RTX)
14998 unsigned int extra = GET_MODE_SIZE (GET_MODE (x)) - UNITS_PER_WORD;
14999 unsigned HOST_WIDE_INT offset = INTVAL (off);
15001 /* We need a secondary reload when our legitimate_address_p
15002 says the address is good (as otherwise the entire address
15003 will be reloaded), and the offset is not a multiple of
15004 four or we have an address wrap. Address wrap will only
15005 occur for LO_SUMs since legitimate_offset_address_p
15006 rejects addresses for 16-byte mems that will wrap. */
15007 if (GET_CODE (addr) == LO_SUM
15008 ? (1 /* legitimate_address_p allows any offset for lo_sum */
15009 && ((offset & 3) != 0
15010 || ((offset & 0xffff) ^ 0x8000) >= 0x10000 - extra))
15011 : (offset + 0x8000 < 0x10000 - extra /* legitimate_address_p */
15012 && (offset & 3) != 0))
15014 if (in_p)
15015 sri->icode = CODE_FOR_reload_di_load;
15016 else
15017 sri->icode = CODE_FOR_reload_di_store;
15018 sri->extra_cost = 2;
15019 ret = NO_REGS;
15021 else
15022 default_p = true;
15024 else
15025 default_p = true;
15027 else if (!TARGET_POWERPC64
15028 && reg_class_to_reg_type[(int)rclass] == GPR_REG_TYPE
15029 && MEM_P (x)
15030 && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
15032 rtx addr = XEXP (x, 0);
15033 rtx off = address_offset (addr);
15035 if (off != NULL_RTX)
15037 unsigned int extra = GET_MODE_SIZE (GET_MODE (x)) - UNITS_PER_WORD;
15038 unsigned HOST_WIDE_INT offset = INTVAL (off);
15040 /* We need a secondary reload when our legitimate_address_p
15041 says the address is good (as otherwise the entire address
15042 will be reloaded), and we have a wrap.
15044 legitimate_lo_sum_address_p allows LO_SUM addresses to
15045 have any offset so test for wrap in the low 16 bits.
15047 legitimate_offset_address_p checks for the range
15048 [-0x8000,0x7fff] for mode size of 8 and [-0x8000,0x7ff7]
15049 for mode size of 16. We wrap at [0x7ffc,0x7fff] and
15050 [0x7ff4,0x7fff] respectively, so test for the
15051 intersection of these ranges, [0x7ffc,0x7fff] and
15052 [0x7ff4,0x7ff7] respectively.
15054 Note that the address we see here may have been
15055 manipulated by legitimize_reload_address. */
15056 if (GET_CODE (addr) == LO_SUM
15057 ? ((offset & 0xffff) ^ 0x8000) >= 0x10000 - extra
15058 : offset - (0x8000 - extra) < UNITS_PER_WORD)
15060 if (in_p)
15061 sri->icode = CODE_FOR_reload_si_load;
15062 else
15063 sri->icode = CODE_FOR_reload_si_store;
15064 sri->extra_cost = 2;
15065 ret = NO_REGS;
15067 else
15068 default_p = true;
15070 else
15071 default_p = true;
15073 else
15074 default_p = true;
15076 if (default_p)
15077 ret = default_secondary_reload (in_p, x, rclass, mode, sri);
15079 gcc_assert (ret != ALL_REGS);
15081 if (TARGET_DEBUG_ADDR)
15083 fprintf (stderr,
15084 "\nrs6000_secondary_reload, return %s, in_p = %s, rclass = %s, "
15085 "mode = %s",
15086 reg_class_names[ret],
15087 in_p ? "true" : "false",
15088 reg_class_names[rclass],
15089 GET_MODE_NAME (mode));
15091 if (default_p)
15092 fprintf (stderr, ", default secondary reload");
15094 if (sri->icode != CODE_FOR_nothing)
15095 fprintf (stderr, ", reload func = %s, extra cost = %d\n",
15096 insn_data[sri->icode].name, sri->extra_cost);
15097 else
15098 fprintf (stderr, "\n");
15100 debug_rtx (x);
15103 return ret;
15106 /* Better tracing for rs6000_secondary_reload_inner. */
15108 static void
15109 rs6000_secondary_reload_trace (int line, rtx reg, rtx mem, rtx scratch,
15110 bool store_p)
15112 rtx set, clobber;
15114 gcc_assert (reg != NULL_RTX && mem != NULL_RTX && scratch != NULL_RTX);
15116 fprintf (stderr, "rs6000_secondary_reload_inner:%d, type = %s\n", line,
15117 store_p ? "store" : "load");
15119 if (store_p)
15120 set = gen_rtx_SET (VOIDmode, mem, reg);
15121 else
15122 set = gen_rtx_SET (VOIDmode, reg, mem);
15124 clobber = gen_rtx_CLOBBER (VOIDmode, scratch);
15125 debug_rtx (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set, clobber)));
15128 static void
15129 rs6000_secondary_reload_fail (int line, rtx reg, rtx mem, rtx scratch,
15130 bool store_p)
15132 rs6000_secondary_reload_trace (line, reg, mem, scratch, store_p);
15133 gcc_unreachable ();
15136 /* Fixup reload addresses for Altivec or VSX loads/stores to change SP+offset
15137 to SP+reg addressing. */
15139 void
15140 rs6000_secondary_reload_inner (rtx reg, rtx mem, rtx scratch, bool store_p)
15142 int regno = true_regnum (reg);
15143 enum machine_mode mode = GET_MODE (reg);
15144 enum reg_class rclass;
15145 rtx addr;
15146 rtx and_op2 = NULL_RTX;
15147 rtx addr_op1;
15148 rtx addr_op2;
15149 rtx scratch_or_premodify = scratch;
15150 rtx and_rtx;
15151 rtx cc_clobber;
15153 if (TARGET_DEBUG_ADDR)
15154 rs6000_secondary_reload_trace (__LINE__, reg, mem, scratch, store_p);
15156 if (regno < 0 || regno >= FIRST_PSEUDO_REGISTER)
15157 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
15159 if (GET_CODE (mem) != MEM)
15160 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
15162 rclass = REGNO_REG_CLASS (regno);
15163 addr = XEXP (mem, 0);
15165 switch (rclass)
15167 /* GPRs can handle reg + small constant, all other addresses need to use
15168 the scratch register. */
15169 case GENERAL_REGS:
15170 case BASE_REGS:
15171 if (GET_CODE (addr) == AND)
15173 and_op2 = XEXP (addr, 1);
15174 addr = XEXP (addr, 0);
15177 if (GET_CODE (addr) == PRE_MODIFY)
15179 scratch_or_premodify = XEXP (addr, 0);
15180 if (!REG_P (scratch_or_premodify))
15181 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
15183 if (GET_CODE (XEXP (addr, 1)) != PLUS)
15184 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
15186 addr = XEXP (addr, 1);
15189 if (GET_CODE (addr) == PLUS
15190 && (and_op2 != NULL_RTX
15191 || !rs6000_legitimate_offset_address_p (PTImode, addr,
15192 false, true)))
15194 addr_op1 = XEXP (addr, 0);
15195 addr_op2 = XEXP (addr, 1);
15196 if (!legitimate_indirect_address_p (addr_op1, false))
15197 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
15199 if (!REG_P (addr_op2)
15200 && (GET_CODE (addr_op2) != CONST_INT
15201 || !satisfies_constraint_I (addr_op2)))
15203 if (TARGET_DEBUG_ADDR)
15205 fprintf (stderr,
15206 "\nMove plus addr to register %s, mode = %s: ",
15207 rs6000_reg_names[REGNO (scratch)],
15208 GET_MODE_NAME (mode));
15209 debug_rtx (addr_op2);
15211 rs6000_emit_move (scratch, addr_op2, Pmode);
15212 addr_op2 = scratch;
15215 emit_insn (gen_rtx_SET (VOIDmode,
15216 scratch_or_premodify,
15217 gen_rtx_PLUS (Pmode,
15218 addr_op1,
15219 addr_op2)));
15221 addr = scratch_or_premodify;
15222 scratch_or_premodify = scratch;
15224 else if (!legitimate_indirect_address_p (addr, false)
15225 && !rs6000_legitimate_offset_address_p (PTImode, addr,
15226 false, true))
15228 if (TARGET_DEBUG_ADDR)
15230 fprintf (stderr, "\nMove addr to register %s, mode = %s: ",
15231 rs6000_reg_names[REGNO (scratch_or_premodify)],
15232 GET_MODE_NAME (mode));
15233 debug_rtx (addr);
15235 rs6000_emit_move (scratch_or_premodify, addr, Pmode);
15236 addr = scratch_or_premodify;
15237 scratch_or_premodify = scratch;
15239 break;
15241 /* Float registers can do offset+reg addressing for scalar types. */
15242 case FLOAT_REGS:
15243 if (legitimate_indirect_address_p (addr, false) /* reg */
15244 || legitimate_indexed_address_p (addr, false) /* reg+reg */
15245 || ((GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8)
15246 && and_op2 == NULL_RTX
15247 && scratch_or_premodify == scratch
15248 && rs6000_legitimate_offset_address_p (mode, addr, false, false)))
15249 break;
15251 /* If this isn't a legacy floating point load/store, fall through to the
15252 VSX defaults. */
15254 /* VSX/Altivec registers can only handle reg+reg addressing. Move other
15255 addresses into a scratch register. */
15256 case VSX_REGS:
15257 case ALTIVEC_REGS:
15259 /* With float regs, we need to handle the AND ourselves, since we can't
15260 use the Altivec instruction with an implicit AND -16. Allow scalar
15261 loads to float registers to use reg+offset even if VSX. */
15262 if (GET_CODE (addr) == AND
15263 && (rclass != ALTIVEC_REGS || GET_MODE_SIZE (mode) != 16
15264 || GET_CODE (XEXP (addr, 1)) != CONST_INT
15265 || INTVAL (XEXP (addr, 1)) != -16
15266 || !VECTOR_MEM_ALTIVEC_P (mode)))
15268 and_op2 = XEXP (addr, 1);
15269 addr = XEXP (addr, 0);
15272 /* If we aren't using a VSX load, save the PRE_MODIFY register and use it
15273 as the address later. */
15274 if (GET_CODE (addr) == PRE_MODIFY
15275 && ((ALTIVEC_OR_VSX_VECTOR_MODE (mode)
15276 && (rclass != FLOAT_REGS
15277 || (GET_MODE_SIZE (mode) != 4 && GET_MODE_SIZE (mode) != 8)))
15278 || and_op2 != NULL_RTX
15279 || !legitimate_indexed_address_p (XEXP (addr, 1), false)))
15281 scratch_or_premodify = XEXP (addr, 0);
15282 if (!legitimate_indirect_address_p (scratch_or_premodify, false))
15283 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
15285 if (GET_CODE (XEXP (addr, 1)) != PLUS)
15286 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
15288 addr = XEXP (addr, 1);
15291 if (legitimate_indirect_address_p (addr, false) /* reg */
15292 || legitimate_indexed_address_p (addr, false) /* reg+reg */
15293 || (GET_CODE (addr) == AND /* Altivec memory */
15294 && rclass == ALTIVEC_REGS
15295 && GET_CODE (XEXP (addr, 1)) == CONST_INT
15296 && INTVAL (XEXP (addr, 1)) == -16
15297 && (legitimate_indirect_address_p (XEXP (addr, 0), false)
15298 || legitimate_indexed_address_p (XEXP (addr, 0), false))))
15301 else if (GET_CODE (addr) == PLUS)
15303 addr_op1 = XEXP (addr, 0);
15304 addr_op2 = XEXP (addr, 1);
15305 if (!REG_P (addr_op1))
15306 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
15308 if (TARGET_DEBUG_ADDR)
15310 fprintf (stderr, "\nMove plus addr to register %s, mode = %s: ",
15311 rs6000_reg_names[REGNO (scratch)], GET_MODE_NAME (mode));
15312 debug_rtx (addr_op2);
15314 rs6000_emit_move (scratch, addr_op2, Pmode);
15315 emit_insn (gen_rtx_SET (VOIDmode,
15316 scratch_or_premodify,
15317 gen_rtx_PLUS (Pmode,
15318 addr_op1,
15319 scratch)));
15320 addr = scratch_or_premodify;
15321 scratch_or_premodify = scratch;
15324 else if (GET_CODE (addr) == SYMBOL_REF || GET_CODE (addr) == CONST
15325 || GET_CODE (addr) == CONST_INT || GET_CODE (addr) == LO_SUM
15326 || REG_P (addr))
15328 if (TARGET_DEBUG_ADDR)
15330 fprintf (stderr, "\nMove addr to register %s, mode = %s: ",
15331 rs6000_reg_names[REGNO (scratch_or_premodify)],
15332 GET_MODE_NAME (mode));
15333 debug_rtx (addr);
15336 rs6000_emit_move (scratch_or_premodify, addr, Pmode);
15337 addr = scratch_or_premodify;
15338 scratch_or_premodify = scratch;
15341 else
15342 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
15344 break;
15346 default:
15347 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
15350 /* If the original address involved a pre-modify that we couldn't use the VSX
15351 memory instruction with update, and we haven't taken care of already,
15352 store the address in the pre-modify register and use that as the
15353 address. */
15354 if (scratch_or_premodify != scratch && scratch_or_premodify != addr)
15356 emit_insn (gen_rtx_SET (VOIDmode, scratch_or_premodify, addr));
15357 addr = scratch_or_premodify;
15360 /* If the original address involved an AND -16 and we couldn't use an ALTIVEC
15361 memory instruction, recreate the AND now, including the clobber which is
15362 generated by the general ANDSI3/ANDDI3 patterns for the
15363 andi. instruction. */
15364 if (and_op2 != NULL_RTX)
15366 if (! legitimate_indirect_address_p (addr, false))
15368 emit_insn (gen_rtx_SET (VOIDmode, scratch, addr));
15369 addr = scratch;
15372 if (TARGET_DEBUG_ADDR)
15374 fprintf (stderr, "\nAnd addr to register %s, mode = %s: ",
15375 rs6000_reg_names[REGNO (scratch)], GET_MODE_NAME (mode));
15376 debug_rtx (and_op2);
15379 and_rtx = gen_rtx_SET (VOIDmode,
15380 scratch,
15381 gen_rtx_AND (Pmode,
15382 addr,
15383 and_op2));
15385 cc_clobber = gen_rtx_CLOBBER (CCmode, gen_rtx_SCRATCH (CCmode));
15386 emit_insn (gen_rtx_PARALLEL (VOIDmode,
15387 gen_rtvec (2, and_rtx, cc_clobber)));
15388 addr = scratch;
15391 /* Adjust the address if it changed. */
15392 if (addr != XEXP (mem, 0))
15394 mem = replace_equiv_address_nv (mem, addr);
15395 if (TARGET_DEBUG_ADDR)
15396 fprintf (stderr, "\nrs6000_secondary_reload_inner, mem adjusted.\n");
15399 /* Now create the move. */
15400 if (store_p)
15401 emit_insn (gen_rtx_SET (VOIDmode, mem, reg));
15402 else
15403 emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
15405 return;
15408 /* Convert reloads involving 64-bit gprs and misaligned offset
15409 addressing, or multiple 32-bit gprs and offsets that are too large,
15410 to use indirect addressing. */
15412 void
15413 rs6000_secondary_reload_gpr (rtx reg, rtx mem, rtx scratch, bool store_p)
15415 int regno = true_regnum (reg);
15416 enum reg_class rclass;
15417 rtx addr;
15418 rtx scratch_or_premodify = scratch;
15420 if (TARGET_DEBUG_ADDR)
15422 fprintf (stderr, "\nrs6000_secondary_reload_gpr, type = %s\n",
15423 store_p ? "store" : "load");
15424 fprintf (stderr, "reg:\n");
15425 debug_rtx (reg);
15426 fprintf (stderr, "mem:\n");
15427 debug_rtx (mem);
15428 fprintf (stderr, "scratch:\n");
15429 debug_rtx (scratch);
15432 gcc_assert (regno >= 0 && regno < FIRST_PSEUDO_REGISTER);
15433 gcc_assert (GET_CODE (mem) == MEM);
15434 rclass = REGNO_REG_CLASS (regno);
15435 gcc_assert (rclass == GENERAL_REGS || rclass == BASE_REGS);
15436 addr = XEXP (mem, 0);
15438 if (GET_CODE (addr) == PRE_MODIFY)
15440 scratch_or_premodify = XEXP (addr, 0);
15441 gcc_assert (REG_P (scratch_or_premodify));
15442 addr = XEXP (addr, 1);
15444 gcc_assert (GET_CODE (addr) == PLUS || GET_CODE (addr) == LO_SUM);
15446 rs6000_emit_move (scratch_or_premodify, addr, Pmode);
15448 mem = replace_equiv_address_nv (mem, scratch_or_premodify);
15450 /* Now create the move. */
15451 if (store_p)
15452 emit_insn (gen_rtx_SET (VOIDmode, mem, reg));
15453 else
15454 emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
15456 return;
15459 /* Allocate a 64-bit stack slot to be used for copying SDmode values through if
15460 this function has any SDmode references. If we are on a power7 or later, we
15461 don't need the 64-bit stack slot since the LFIWZX and STIFWX instructions
15462 can load/store the value. */
15464 static void
15465 rs6000_alloc_sdmode_stack_slot (void)
15467 tree t;
15468 basic_block bb;
15469 gimple_stmt_iterator gsi;
15471 gcc_assert (cfun->machine->sdmode_stack_slot == NULL_RTX);
15473 if (TARGET_NO_SDMODE_STACK)
15474 return;
15476 FOR_EACH_BB (bb)
15477 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
15479 tree ret = walk_gimple_op (gsi_stmt (gsi), rs6000_check_sdmode, NULL);
15480 if (ret)
15482 rtx stack = assign_stack_local (DDmode, GET_MODE_SIZE (DDmode), 0);
15483 cfun->machine->sdmode_stack_slot = adjust_address_nv (stack,
15484 SDmode, 0);
15485 return;
15489 /* Check for any SDmode parameters of the function. */
15490 for (t = DECL_ARGUMENTS (cfun->decl); t; t = DECL_CHAIN (t))
15492 if (TREE_TYPE (t) == error_mark_node)
15493 continue;
15495 if (TYPE_MODE (TREE_TYPE (t)) == SDmode
15496 || TYPE_MODE (DECL_ARG_TYPE (t)) == SDmode)
15498 rtx stack = assign_stack_local (DDmode, GET_MODE_SIZE (DDmode), 0);
15499 cfun->machine->sdmode_stack_slot = adjust_address_nv (stack,
15500 SDmode, 0);
15501 return;
15506 static void
15507 rs6000_instantiate_decls (void)
15509 if (cfun->machine->sdmode_stack_slot != NULL_RTX)
15510 instantiate_decl_rtl (cfun->machine->sdmode_stack_slot);
15513 /* Given an rtx X being reloaded into a reg required to be
15514 in class CLASS, return the class of reg to actually use.
15515 In general this is just CLASS; but on some machines
15516 in some cases it is preferable to use a more restrictive class.
15518 On the RS/6000, we have to return NO_REGS when we want to reload a
15519 floating-point CONST_DOUBLE to force it to be copied to memory.
15521 We also don't want to reload integer values into floating-point
15522 registers if we can at all help it. In fact, this can
15523 cause reload to die, if it tries to generate a reload of CTR
15524 into a FP register and discovers it doesn't have the memory location
15525 required.
15527 ??? Would it be a good idea to have reload do the converse, that is
15528 try to reload floating modes into FP registers if possible?
15531 static enum reg_class
15532 rs6000_preferred_reload_class (rtx x, enum reg_class rclass)
15534 enum machine_mode mode = GET_MODE (x);
15536 if (TARGET_VSX && x == CONST0_RTX (mode) && VSX_REG_CLASS_P (rclass))
15537 return rclass;
15539 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (mode)
15540 && (rclass == ALTIVEC_REGS || rclass == VSX_REGS)
15541 && easy_vector_constant (x, mode))
15542 return ALTIVEC_REGS;
15544 if (CONSTANT_P (x) && reg_classes_intersect_p (rclass, FLOAT_REGS))
15545 return NO_REGS;
15547 if (GET_MODE_CLASS (mode) == MODE_INT && rclass == NON_SPECIAL_REGS)
15548 return GENERAL_REGS;
15550 /* For VSX, prefer the traditional registers for 64-bit values because we can
15551 use the non-VSX loads. Prefer the Altivec registers if Altivec is
15552 handling the vector operations (i.e. V16QI, V8HI, and V4SI), or if we
15553 prefer Altivec loads.. */
15554 if (rclass == VSX_REGS)
15556 if (GET_MODE_SIZE (mode) <= 8)
15557 return FLOAT_REGS;
15559 if (VECTOR_UNIT_ALTIVEC_P (mode) || VECTOR_MEM_ALTIVEC_P (mode))
15560 return ALTIVEC_REGS;
15562 return rclass;
15565 return rclass;
15568 /* Debug version of rs6000_preferred_reload_class. */
15569 static enum reg_class
15570 rs6000_debug_preferred_reload_class (rtx x, enum reg_class rclass)
15572 enum reg_class ret = rs6000_preferred_reload_class (x, rclass);
15574 fprintf (stderr,
15575 "\nrs6000_preferred_reload_class, return %s, rclass = %s, "
15576 "mode = %s, x:\n",
15577 reg_class_names[ret], reg_class_names[rclass],
15578 GET_MODE_NAME (GET_MODE (x)));
15579 debug_rtx (x);
15581 return ret;
15584 /* If we are copying between FP or AltiVec registers and anything else, we need
15585 a memory location. The exception is when we are targeting ppc64 and the
15586 move to/from fpr to gpr instructions are available. Also, under VSX, you
15587 can copy vector registers from the FP register set to the Altivec register
15588 set and vice versa. */
15590 static bool
15591 rs6000_secondary_memory_needed (enum reg_class from_class,
15592 enum reg_class to_class,
15593 enum machine_mode mode)
15595 enum rs6000_reg_type from_type, to_type;
15596 bool altivec_p = ((from_class == ALTIVEC_REGS)
15597 || (to_class == ALTIVEC_REGS));
15599 /* If a simple/direct move is available, we don't need secondary memory */
15600 from_type = reg_class_to_reg_type[(int)from_class];
15601 to_type = reg_class_to_reg_type[(int)to_class];
15603 if (rs6000_secondary_reload_move (to_type, from_type, mode,
15604 (secondary_reload_info *)0, altivec_p))
15605 return false;
15607 /* If we have a floating point or vector register class, we need to use
15608 memory to transfer the data. */
15609 if (IS_FP_VECT_REG_TYPE (from_type) || IS_FP_VECT_REG_TYPE (to_type))
15610 return true;
15612 return false;
15615 /* Debug version of rs6000_secondary_memory_needed. */
15616 static bool
15617 rs6000_debug_secondary_memory_needed (enum reg_class from_class,
15618 enum reg_class to_class,
15619 enum machine_mode mode)
15621 bool ret = rs6000_secondary_memory_needed (from_class, to_class, mode);
15623 fprintf (stderr,
15624 "rs6000_secondary_memory_needed, return: %s, from_class = %s, "
15625 "to_class = %s, mode = %s\n",
15626 ret ? "true" : "false",
15627 reg_class_names[from_class],
15628 reg_class_names[to_class],
15629 GET_MODE_NAME (mode));
15631 return ret;
15634 /* Return the register class of a scratch register needed to copy IN into
15635 or out of a register in RCLASS in MODE. If it can be done directly,
15636 NO_REGS is returned. */
15638 static enum reg_class
15639 rs6000_secondary_reload_class (enum reg_class rclass, enum machine_mode mode,
15640 rtx in)
15642 int regno;
15644 if (TARGET_ELF || (DEFAULT_ABI == ABI_DARWIN
15645 #if TARGET_MACHO
15646 && MACHOPIC_INDIRECT
15647 #endif
15650 /* We cannot copy a symbolic operand directly into anything
15651 other than BASE_REGS for TARGET_ELF. So indicate that a
15652 register from BASE_REGS is needed as an intermediate
15653 register.
15655 On Darwin, pic addresses require a load from memory, which
15656 needs a base register. */
15657 if (rclass != BASE_REGS
15658 && (GET_CODE (in) == SYMBOL_REF
15659 || GET_CODE (in) == HIGH
15660 || GET_CODE (in) == LABEL_REF
15661 || GET_CODE (in) == CONST))
15662 return BASE_REGS;
15665 if (GET_CODE (in) == REG)
15667 regno = REGNO (in);
15668 if (regno >= FIRST_PSEUDO_REGISTER)
15670 regno = true_regnum (in);
15671 if (regno >= FIRST_PSEUDO_REGISTER)
15672 regno = -1;
15675 else if (GET_CODE (in) == SUBREG)
15677 regno = true_regnum (in);
15678 if (regno >= FIRST_PSEUDO_REGISTER)
15679 regno = -1;
15681 else
15682 regno = -1;
15684 /* We can place anything into GENERAL_REGS and can put GENERAL_REGS
15685 into anything. */
15686 if (rclass == GENERAL_REGS || rclass == BASE_REGS
15687 || (regno >= 0 && INT_REGNO_P (regno)))
15688 return NO_REGS;
15690 /* Constants, memory, and FP registers can go into FP registers. */
15691 if ((regno == -1 || FP_REGNO_P (regno))
15692 && (rclass == FLOAT_REGS || rclass == NON_SPECIAL_REGS))
15693 return (mode != SDmode) ? NO_REGS : GENERAL_REGS;
15695 /* Memory, and FP/altivec registers can go into fp/altivec registers under
15696 VSX. However, for scalar variables, use the traditional floating point
15697 registers so that we can use offset+register addressing. */
15698 if (TARGET_VSX
15699 && (regno == -1 || VSX_REGNO_P (regno))
15700 && VSX_REG_CLASS_P (rclass))
15702 if (GET_MODE_SIZE (mode) < 16)
15703 return FLOAT_REGS;
15705 return NO_REGS;
15708 /* Memory, and AltiVec registers can go into AltiVec registers. */
15709 if ((regno == -1 || ALTIVEC_REGNO_P (regno))
15710 && rclass == ALTIVEC_REGS)
15711 return NO_REGS;
15713 /* We can copy among the CR registers. */
15714 if ((rclass == CR_REGS || rclass == CR0_REGS)
15715 && regno >= 0 && CR_REGNO_P (regno))
15716 return NO_REGS;
15718 /* Otherwise, we need GENERAL_REGS. */
15719 return GENERAL_REGS;
15722 /* Debug version of rs6000_secondary_reload_class. */
15723 static enum reg_class
15724 rs6000_debug_secondary_reload_class (enum reg_class rclass,
15725 enum machine_mode mode, rtx in)
15727 enum reg_class ret = rs6000_secondary_reload_class (rclass, mode, in);
15728 fprintf (stderr,
15729 "\nrs6000_secondary_reload_class, return %s, rclass = %s, "
15730 "mode = %s, input rtx:\n",
15731 reg_class_names[ret], reg_class_names[rclass],
15732 GET_MODE_NAME (mode));
15733 debug_rtx (in);
15735 return ret;
15738 /* Return nonzero if for CLASS a mode change from FROM to TO is invalid. */
15740 static bool
15741 rs6000_cannot_change_mode_class (enum machine_mode from,
15742 enum machine_mode to,
15743 enum reg_class rclass)
15745 unsigned from_size = GET_MODE_SIZE (from);
15746 unsigned to_size = GET_MODE_SIZE (to);
15748 if (from_size != to_size)
15750 enum reg_class xclass = (TARGET_VSX) ? VSX_REGS : FLOAT_REGS;
15752 if (reg_classes_intersect_p (xclass, rclass))
15754 unsigned to_nregs = hard_regno_nregs[FIRST_FPR_REGNO][to];
15755 unsigned from_nregs = hard_regno_nregs[FIRST_FPR_REGNO][from];
15757 /* Don't allow 64-bit types to overlap with 128-bit types that take a
15758 single register under VSX because the scalar part of the register
15759 is in the upper 64-bits, and not the lower 64-bits. Types like
15760 TFmode/TDmode that take 2 scalar register can overlap. 128-bit
15761 IEEE floating point can't overlap, and neither can small
15762 values. */
15764 if (TARGET_IEEEQUAD && (to == TFmode || from == TFmode))
15765 return true;
15767 if (from_size < 8 || to_size < 8)
15768 return true;
15770 if (from_size == 8 && (8 * to_nregs) != to_size)
15771 return true;
15773 if (to_size == 8 && (8 * from_nregs) != from_size)
15774 return true;
15776 return false;
15778 else
15779 return false;
15782 if (TARGET_E500_DOUBLE
15783 && ((((to) == DFmode) + ((from) == DFmode)) == 1
15784 || (((to) == TFmode) + ((from) == TFmode)) == 1
15785 || (((to) == DDmode) + ((from) == DDmode)) == 1
15786 || (((to) == TDmode) + ((from) == TDmode)) == 1
15787 || (((to) == DImode) + ((from) == DImode)) == 1))
15788 return true;
15790 /* Since the VSX register set includes traditional floating point registers
15791 and altivec registers, just check for the size being different instead of
15792 trying to check whether the modes are vector modes. Otherwise it won't
15793 allow say DF and DI to change classes. For types like TFmode and TDmode
15794 that take 2 64-bit registers, rather than a single 128-bit register, don't
15795 allow subregs of those types to other 128 bit types. */
15796 if (TARGET_VSX && VSX_REG_CLASS_P (rclass))
15798 unsigned num_regs = (from_size + 15) / 16;
15799 if (hard_regno_nregs[FIRST_FPR_REGNO][to] > num_regs
15800 || hard_regno_nregs[FIRST_FPR_REGNO][from] > num_regs)
15801 return true;
15803 return (from_size != 8 && from_size != 16);
15806 if (TARGET_ALTIVEC && rclass == ALTIVEC_REGS
15807 && (ALTIVEC_VECTOR_MODE (from) + ALTIVEC_VECTOR_MODE (to)) == 1)
15808 return true;
15810 if (TARGET_SPE && (SPE_VECTOR_MODE (from) + SPE_VECTOR_MODE (to)) == 1
15811 && reg_classes_intersect_p (GENERAL_REGS, rclass))
15812 return true;
15814 return false;
15817 /* Debug version of rs6000_cannot_change_mode_class. */
15818 static bool
15819 rs6000_debug_cannot_change_mode_class (enum machine_mode from,
15820 enum machine_mode to,
15821 enum reg_class rclass)
15823 bool ret = rs6000_cannot_change_mode_class (from, to, rclass);
15825 fprintf (stderr,
15826 "rs6000_cannot_change_mode_class, return %s, from = %s, "
15827 "to = %s, rclass = %s\n",
15828 ret ? "true" : "false",
15829 GET_MODE_NAME (from), GET_MODE_NAME (to),
15830 reg_class_names[rclass]);
15832 return ret;
15835 /* Return a string to do a move operation of 128 bits of data. */
15837 const char *
15838 rs6000_output_move_128bit (rtx operands[])
15840 rtx dest = operands[0];
15841 rtx src = operands[1];
15842 enum machine_mode mode = GET_MODE (dest);
15843 int dest_regno;
15844 int src_regno;
15845 bool dest_gpr_p, dest_fp_p, dest_av_p, dest_vsx_p;
15846 bool src_gpr_p, src_fp_p, src_av_p, src_vsx_p;
15848 if (REG_P (dest))
15850 dest_regno = REGNO (dest);
15851 dest_gpr_p = INT_REGNO_P (dest_regno);
15852 dest_fp_p = FP_REGNO_P (dest_regno);
15853 dest_av_p = ALTIVEC_REGNO_P (dest_regno);
15854 dest_vsx_p = dest_fp_p | dest_av_p;
15856 else
15858 dest_regno = -1;
15859 dest_gpr_p = dest_fp_p = dest_av_p = dest_vsx_p = false;
15862 if (REG_P (src))
15864 src_regno = REGNO (src);
15865 src_gpr_p = INT_REGNO_P (src_regno);
15866 src_fp_p = FP_REGNO_P (src_regno);
15867 src_av_p = ALTIVEC_REGNO_P (src_regno);
15868 src_vsx_p = src_fp_p | src_av_p;
15870 else
15872 src_regno = -1;
15873 src_gpr_p = src_fp_p = src_av_p = src_vsx_p = false;
15876 /* Register moves. */
15877 if (dest_regno >= 0 && src_regno >= 0)
15879 if (dest_gpr_p)
15881 if (src_gpr_p)
15882 return "#";
15884 else if (TARGET_VSX && TARGET_DIRECT_MOVE && src_vsx_p)
15885 return "#";
15888 else if (TARGET_VSX && dest_vsx_p)
15890 if (src_vsx_p)
15891 return "xxlor %x0,%x1,%x1";
15893 else if (TARGET_DIRECT_MOVE && src_gpr_p)
15894 return "#";
15897 else if (TARGET_ALTIVEC && dest_av_p && src_av_p)
15898 return "vor %0,%1,%1";
15900 else if (dest_fp_p && src_fp_p)
15901 return "#";
15904 /* Loads. */
15905 else if (dest_regno >= 0 && MEM_P (src))
15907 if (dest_gpr_p)
15909 if (TARGET_QUAD_MEMORY && (dest_regno & 1) == 0
15910 && quad_memory_operand (src, mode)
15911 && !reg_overlap_mentioned_p (dest, src))
15913 /* lq/stq only has DQ-form, so avoid X-form that %y produces. */
15914 return REG_P (XEXP (src, 0)) ? "lq %0,%1" : "lq %0,%y1";
15916 else
15917 return "#";
15920 else if (TARGET_ALTIVEC && dest_av_p
15921 && altivec_indexed_or_indirect_operand (src, mode))
15922 return "lvx %0,%y1";
15924 else if (TARGET_VSX && dest_vsx_p)
15926 if (mode == V16QImode || mode == V8HImode || mode == V4SImode)
15927 return "lxvw4x %x0,%y1";
15928 else
15929 return "lxvd2x %x0,%y1";
15932 else if (TARGET_ALTIVEC && dest_av_p)
15933 return "lvx %0,%y1";
15935 else if (dest_fp_p)
15936 return "#";
15939 /* Stores. */
15940 else if (src_regno >= 0 && MEM_P (dest))
15942 if (src_gpr_p)
15944 if (TARGET_QUAD_MEMORY && (src_regno & 1) == 0
15945 && quad_memory_operand (dest, mode))
15947 /* lq/stq only has DQ-form, so avoid X-form that %y produces. */
15948 return REG_P (XEXP (dest, 0)) ? "stq %1,%0" : "stq %1,%y0";
15950 else
15951 return "#";
15954 else if (TARGET_ALTIVEC && src_av_p
15955 && altivec_indexed_or_indirect_operand (src, mode))
15956 return "stvx %1,%y0";
15958 else if (TARGET_VSX && src_vsx_p)
15960 if (mode == V16QImode || mode == V8HImode || mode == V4SImode)
15961 return "stxvw4x %x1,%y0";
15962 else
15963 return "stxvd2x %x1,%y0";
15966 else if (TARGET_ALTIVEC && src_av_p)
15967 return "stvx %1,%y0";
15969 else if (src_fp_p)
15970 return "#";
15973 /* Constants. */
15974 else if (dest_regno >= 0
15975 && (GET_CODE (src) == CONST_INT
15976 || GET_CODE (src) == CONST_DOUBLE
15977 || GET_CODE (src) == CONST_VECTOR))
15979 if (dest_gpr_p)
15980 return "#";
15982 else if (TARGET_VSX && dest_vsx_p && zero_constant (src, mode))
15983 return "xxlxor %x0,%x0,%x0";
15985 else if (TARGET_ALTIVEC && dest_av_p)
15986 return output_vec_const_move (operands);
15989 if (TARGET_DEBUG_ADDR)
15991 fprintf (stderr, "\n===== Bad 128 bit move:\n");
15992 debug_rtx (gen_rtx_SET (VOIDmode, dest, src));
15995 gcc_unreachable ();
15999 /* Given a comparison operation, return the bit number in CCR to test. We
16000 know this is a valid comparison.
16002 SCC_P is 1 if this is for an scc. That means that %D will have been
16003 used instead of %C, so the bits will be in different places.
16005 Return -1 if OP isn't a valid comparison for some reason. */
16008 ccr_bit (rtx op, int scc_p)
16010 enum rtx_code code = GET_CODE (op);
16011 enum machine_mode cc_mode;
16012 int cc_regnum;
16013 int base_bit;
16014 rtx reg;
16016 if (!COMPARISON_P (op))
16017 return -1;
16019 reg = XEXP (op, 0);
16021 gcc_assert (GET_CODE (reg) == REG && CR_REGNO_P (REGNO (reg)));
16023 cc_mode = GET_MODE (reg);
16024 cc_regnum = REGNO (reg);
16025 base_bit = 4 * (cc_regnum - CR0_REGNO);
16027 validate_condition_mode (code, cc_mode);
16029 /* When generating a sCOND operation, only positive conditions are
16030 allowed. */
16031 gcc_assert (!scc_p
16032 || code == EQ || code == GT || code == LT || code == UNORDERED
16033 || code == GTU || code == LTU);
16035 switch (code)
16037 case NE:
16038 return scc_p ? base_bit + 3 : base_bit + 2;
16039 case EQ:
16040 return base_bit + 2;
16041 case GT: case GTU: case UNLE:
16042 return base_bit + 1;
16043 case LT: case LTU: case UNGE:
16044 return base_bit;
16045 case ORDERED: case UNORDERED:
16046 return base_bit + 3;
16048 case GE: case GEU:
16049 /* If scc, we will have done a cror to put the bit in the
16050 unordered position. So test that bit. For integer, this is ! LT
16051 unless this is an scc insn. */
16052 return scc_p ? base_bit + 3 : base_bit;
16054 case LE: case LEU:
16055 return scc_p ? base_bit + 3 : base_bit + 1;
16057 default:
16058 gcc_unreachable ();
16062 /* Return the GOT register. */
16065 rs6000_got_register (rtx value ATTRIBUTE_UNUSED)
16067 /* The second flow pass currently (June 1999) can't update
16068 regs_ever_live without disturbing other parts of the compiler, so
16069 update it here to make the prolog/epilogue code happy. */
16070 if (!can_create_pseudo_p ()
16071 && !df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))
16072 df_set_regs_ever_live (RS6000_PIC_OFFSET_TABLE_REGNUM, true);
16074 crtl->uses_pic_offset_table = 1;
16076 return pic_offset_table_rtx;
16079 static rs6000_stack_t stack_info;
16081 /* Function to init struct machine_function.
16082 This will be called, via a pointer variable,
16083 from push_function_context. */
16085 static struct machine_function *
16086 rs6000_init_machine_status (void)
16088 stack_info.reload_completed = 0;
16089 return ggc_alloc_cleared_machine_function ();
16092 #define INT_P(X) (GET_CODE (X) == CONST_INT && GET_MODE (X) == VOIDmode)
16095 extract_MB (rtx op)
16097 int i;
16098 unsigned long val = INTVAL (op);
16100 /* If the high bit is zero, the value is the first 1 bit we find
16101 from the left. */
16102 if ((val & 0x80000000) == 0)
16104 gcc_assert (val & 0xffffffff);
16106 i = 1;
16107 while (((val <<= 1) & 0x80000000) == 0)
16108 ++i;
16109 return i;
16112 /* If the high bit is set and the low bit is not, or the mask is all
16113 1's, the value is zero. */
16114 if ((val & 1) == 0 || (val & 0xffffffff) == 0xffffffff)
16115 return 0;
16117 /* Otherwise we have a wrap-around mask. Look for the first 0 bit
16118 from the right. */
16119 i = 31;
16120 while (((val >>= 1) & 1) != 0)
16121 --i;
16123 return i;
16127 extract_ME (rtx op)
16129 int i;
16130 unsigned long val = INTVAL (op);
16132 /* If the low bit is zero, the value is the first 1 bit we find from
16133 the right. */
16134 if ((val & 1) == 0)
16136 gcc_assert (val & 0xffffffff);
16138 i = 30;
16139 while (((val >>= 1) & 1) == 0)
16140 --i;
16142 return i;
16145 /* If the low bit is set and the high bit is not, or the mask is all
16146 1's, the value is 31. */
16147 if ((val & 0x80000000) == 0 || (val & 0xffffffff) == 0xffffffff)
16148 return 31;
16150 /* Otherwise we have a wrap-around mask. Look for the first 0 bit
16151 from the left. */
16152 i = 0;
16153 while (((val <<= 1) & 0x80000000) != 0)
16154 ++i;
16156 return i;
16159 /* Locate some local-dynamic symbol still in use by this function
16160 so that we can print its name in some tls_ld pattern. */
16162 static const char *
16163 rs6000_get_some_local_dynamic_name (void)
16165 rtx insn;
16167 if (cfun->machine->some_ld_name)
16168 return cfun->machine->some_ld_name;
16170 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
16171 if (INSN_P (insn)
16172 && for_each_rtx (&PATTERN (insn),
16173 rs6000_get_some_local_dynamic_name_1, 0))
16174 return cfun->machine->some_ld_name;
16176 gcc_unreachable ();
16179 /* Helper function for rs6000_get_some_local_dynamic_name. */
16181 static int
16182 rs6000_get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
16184 rtx x = *px;
16186 if (GET_CODE (x) == SYMBOL_REF)
16188 const char *str = XSTR (x, 0);
16189 if (SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC)
16191 cfun->machine->some_ld_name = str;
16192 return 1;
16196 return 0;
16199 /* Write out a function code label. */
16201 void
16202 rs6000_output_function_entry (FILE *file, const char *fname)
16204 if (fname[0] != '.')
16206 switch (DEFAULT_ABI)
16208 default:
16209 gcc_unreachable ();
16211 case ABI_AIX:
16212 if (DOT_SYMBOLS)
16213 putc ('.', file);
16214 else
16215 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "L.");
16216 break;
16218 case ABI_V4:
16219 case ABI_DARWIN:
16220 break;
16224 RS6000_OUTPUT_BASENAME (file, fname);
16227 /* Print an operand. Recognize special options, documented below. */
16229 #if TARGET_ELF
16230 #define SMALL_DATA_RELOC ((rs6000_sdata == SDATA_EABI) ? "sda21" : "sdarel")
16231 #define SMALL_DATA_REG ((rs6000_sdata == SDATA_EABI) ? 0 : 13)
16232 #else
16233 #define SMALL_DATA_RELOC "sda21"
16234 #define SMALL_DATA_REG 0
16235 #endif
16237 void
16238 print_operand (FILE *file, rtx x, int code)
16240 int i;
16241 unsigned HOST_WIDE_INT uval;
16243 switch (code)
16245 /* %a is output_address. */
16247 case 'b':
16248 /* If constant, low-order 16 bits of constant, unsigned.
16249 Otherwise, write normally. */
16250 if (INT_P (x))
16251 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0xffff);
16252 else
16253 print_operand (file, x, 0);
16254 return;
16256 case 'B':
16257 /* If the low-order bit is zero, write 'r'; otherwise, write 'l'
16258 for 64-bit mask direction. */
16259 putc (((INTVAL (x) & 1) == 0 ? 'r' : 'l'), file);
16260 return;
16262 /* %c is output_addr_const if a CONSTANT_ADDRESS_P, otherwise
16263 output_operand. */
16265 case 'D':
16266 /* Like 'J' but get to the GT bit only. */
16267 gcc_assert (REG_P (x));
16269 /* Bit 1 is GT bit. */
16270 i = 4 * (REGNO (x) - CR0_REGNO) + 1;
16272 /* Add one for shift count in rlinm for scc. */
16273 fprintf (file, "%d", i + 1);
16274 return;
16276 case 'E':
16277 /* X is a CR register. Print the number of the EQ bit of the CR */
16278 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
16279 output_operand_lossage ("invalid %%E value");
16280 else
16281 fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO) + 2);
16282 return;
16284 case 'f':
16285 /* X is a CR register. Print the shift count needed to move it
16286 to the high-order four bits. */
16287 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
16288 output_operand_lossage ("invalid %%f value");
16289 else
16290 fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO));
16291 return;
16293 case 'F':
16294 /* Similar, but print the count for the rotate in the opposite
16295 direction. */
16296 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
16297 output_operand_lossage ("invalid %%F value");
16298 else
16299 fprintf (file, "%d", 32 - 4 * (REGNO (x) - CR0_REGNO));
16300 return;
16302 case 'G':
16303 /* X is a constant integer. If it is negative, print "m",
16304 otherwise print "z". This is to make an aze or ame insn. */
16305 if (GET_CODE (x) != CONST_INT)
16306 output_operand_lossage ("invalid %%G value");
16307 else if (INTVAL (x) >= 0)
16308 putc ('z', file);
16309 else
16310 putc ('m', file);
16311 return;
16313 case 'h':
16314 /* If constant, output low-order five bits. Otherwise, write
16315 normally. */
16316 if (INT_P (x))
16317 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 31);
16318 else
16319 print_operand (file, x, 0);
16320 return;
16322 case 'H':
16323 /* If constant, output low-order six bits. Otherwise, write
16324 normally. */
16325 if (INT_P (x))
16326 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 63);
16327 else
16328 print_operand (file, x, 0);
16329 return;
16331 case 'I':
16332 /* Print `i' if this is a constant, else nothing. */
16333 if (INT_P (x))
16334 putc ('i', file);
16335 return;
16337 case 'j':
16338 /* Write the bit number in CCR for jump. */
16339 i = ccr_bit (x, 0);
16340 if (i == -1)
16341 output_operand_lossage ("invalid %%j code");
16342 else
16343 fprintf (file, "%d", i);
16344 return;
16346 case 'J':
16347 /* Similar, but add one for shift count in rlinm for scc and pass
16348 scc flag to `ccr_bit'. */
16349 i = ccr_bit (x, 1);
16350 if (i == -1)
16351 output_operand_lossage ("invalid %%J code");
16352 else
16353 /* If we want bit 31, write a shift count of zero, not 32. */
16354 fprintf (file, "%d", i == 31 ? 0 : i + 1);
16355 return;
16357 case 'k':
16358 /* X must be a constant. Write the 1's complement of the
16359 constant. */
16360 if (! INT_P (x))
16361 output_operand_lossage ("invalid %%k value");
16362 else
16363 fprintf (file, HOST_WIDE_INT_PRINT_DEC, ~ INTVAL (x));
16364 return;
16366 case 'K':
16367 /* X must be a symbolic constant on ELF. Write an
16368 expression suitable for an 'addi' that adds in the low 16
16369 bits of the MEM. */
16370 if (GET_CODE (x) == CONST)
16372 if (GET_CODE (XEXP (x, 0)) != PLUS
16373 || (GET_CODE (XEXP (XEXP (x, 0), 0)) != SYMBOL_REF
16374 && GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
16375 || GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
16376 output_operand_lossage ("invalid %%K value");
16378 print_operand_address (file, x);
16379 fputs ("@l", file);
16380 return;
16382 /* %l is output_asm_label. */
16384 case 'L':
16385 /* Write second word of DImode or DFmode reference. Works on register
16386 or non-indexed memory only. */
16387 if (REG_P (x))
16388 fputs (reg_names[REGNO (x) + 1], file);
16389 else if (MEM_P (x))
16391 /* Handle possible auto-increment. Since it is pre-increment and
16392 we have already done it, we can just use an offset of word. */
16393 if (GET_CODE (XEXP (x, 0)) == PRE_INC
16394 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
16395 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0),
16396 UNITS_PER_WORD));
16397 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
16398 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0),
16399 UNITS_PER_WORD));
16400 else
16401 output_address (XEXP (adjust_address_nv (x, SImode,
16402 UNITS_PER_WORD),
16403 0));
16405 if (small_data_operand (x, GET_MODE (x)))
16406 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
16407 reg_names[SMALL_DATA_REG]);
16409 return;
16411 case 'm':
16412 /* MB value for a mask operand. */
16413 if (! mask_operand (x, SImode))
16414 output_operand_lossage ("invalid %%m value");
16416 fprintf (file, "%d", extract_MB (x));
16417 return;
16419 case 'M':
16420 /* ME value for a mask operand. */
16421 if (! mask_operand (x, SImode))
16422 output_operand_lossage ("invalid %%M value");
16424 fprintf (file, "%d", extract_ME (x));
16425 return;
16427 /* %n outputs the negative of its operand. */
16429 case 'N':
16430 /* Write the number of elements in the vector times 4. */
16431 if (GET_CODE (x) != PARALLEL)
16432 output_operand_lossage ("invalid %%N value");
16433 else
16434 fprintf (file, "%d", XVECLEN (x, 0) * 4);
16435 return;
16437 case 'O':
16438 /* Similar, but subtract 1 first. */
16439 if (GET_CODE (x) != PARALLEL)
16440 output_operand_lossage ("invalid %%O value");
16441 else
16442 fprintf (file, "%d", (XVECLEN (x, 0) - 1) * 4);
16443 return;
16445 case 'p':
16446 /* X is a CONST_INT that is a power of two. Output the logarithm. */
16447 if (! INT_P (x)
16448 || INTVAL (x) < 0
16449 || (i = exact_log2 (INTVAL (x))) < 0)
16450 output_operand_lossage ("invalid %%p value");
16451 else
16452 fprintf (file, "%d", i);
16453 return;
16455 case 'P':
16456 /* The operand must be an indirect memory reference. The result
16457 is the register name. */
16458 if (GET_CODE (x) != MEM || GET_CODE (XEXP (x, 0)) != REG
16459 || REGNO (XEXP (x, 0)) >= 32)
16460 output_operand_lossage ("invalid %%P value");
16461 else
16462 fputs (reg_names[REGNO (XEXP (x, 0))], file);
16463 return;
16465 case 'q':
16466 /* This outputs the logical code corresponding to a boolean
16467 expression. The expression may have one or both operands
16468 negated (if one, only the first one). For condition register
16469 logical operations, it will also treat the negated
16470 CR codes as NOTs, but not handle NOTs of them. */
16472 const char *const *t = 0;
16473 const char *s;
16474 enum rtx_code code = GET_CODE (x);
16475 static const char * const tbl[3][3] = {
16476 { "and", "andc", "nor" },
16477 { "or", "orc", "nand" },
16478 { "xor", "eqv", "xor" } };
16480 if (code == AND)
16481 t = tbl[0];
16482 else if (code == IOR)
16483 t = tbl[1];
16484 else if (code == XOR)
16485 t = tbl[2];
16486 else
16487 output_operand_lossage ("invalid %%q value");
16489 if (GET_CODE (XEXP (x, 0)) != NOT)
16490 s = t[0];
16491 else
16493 if (GET_CODE (XEXP (x, 1)) == NOT)
16494 s = t[2];
16495 else
16496 s = t[1];
16499 fputs (s, file);
16501 return;
16503 case 'Q':
16504 if (! TARGET_MFCRF)
16505 return;
16506 fputc (',', file);
16507 /* FALLTHRU */
16509 case 'R':
16510 /* X is a CR register. Print the mask for `mtcrf'. */
16511 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
16512 output_operand_lossage ("invalid %%R value");
16513 else
16514 fprintf (file, "%d", 128 >> (REGNO (x) - CR0_REGNO));
16515 return;
16517 case 's':
16518 /* Low 5 bits of 32 - value */
16519 if (! INT_P (x))
16520 output_operand_lossage ("invalid %%s value");
16521 else
16522 fprintf (file, HOST_WIDE_INT_PRINT_DEC, (32 - INTVAL (x)) & 31);
16523 return;
16525 case 'S':
16526 /* PowerPC64 mask position. All 0's is excluded.
16527 CONST_INT 32-bit mask is considered sign-extended so any
16528 transition must occur within the CONST_INT, not on the boundary. */
16529 if (! mask64_operand (x, DImode))
16530 output_operand_lossage ("invalid %%S value");
16532 uval = INTVAL (x);
16534 if (uval & 1) /* Clear Left */
16536 #if HOST_BITS_PER_WIDE_INT > 64
16537 uval &= ((unsigned HOST_WIDE_INT) 1 << 64) - 1;
16538 #endif
16539 i = 64;
16541 else /* Clear Right */
16543 uval = ~uval;
16544 #if HOST_BITS_PER_WIDE_INT > 64
16545 uval &= ((unsigned HOST_WIDE_INT) 1 << 64) - 1;
16546 #endif
16547 i = 63;
16549 while (uval != 0)
16550 --i, uval >>= 1;
16551 gcc_assert (i >= 0);
16552 fprintf (file, "%d", i);
16553 return;
16555 case 't':
16556 /* Like 'J' but get to the OVERFLOW/UNORDERED bit. */
16557 gcc_assert (REG_P (x) && GET_MODE (x) == CCmode);
16559 /* Bit 3 is OV bit. */
16560 i = 4 * (REGNO (x) - CR0_REGNO) + 3;
16562 /* If we want bit 31, write a shift count of zero, not 32. */
16563 fprintf (file, "%d", i == 31 ? 0 : i + 1);
16564 return;
16566 case 'T':
16567 /* Print the symbolic name of a branch target register. */
16568 if (GET_CODE (x) != REG || (REGNO (x) != LR_REGNO
16569 && REGNO (x) != CTR_REGNO))
16570 output_operand_lossage ("invalid %%T value");
16571 else if (REGNO (x) == LR_REGNO)
16572 fputs ("lr", file);
16573 else
16574 fputs ("ctr", file);
16575 return;
16577 case 'u':
16578 /* High-order 16 bits of constant for use in unsigned operand. */
16579 if (! INT_P (x))
16580 output_operand_lossage ("invalid %%u value");
16581 else
16582 fprintf (file, HOST_WIDE_INT_PRINT_HEX,
16583 (INTVAL (x) >> 16) & 0xffff);
16584 return;
16586 case 'v':
16587 /* High-order 16 bits of constant for use in signed operand. */
16588 if (! INT_P (x))
16589 output_operand_lossage ("invalid %%v value");
16590 else
16591 fprintf (file, HOST_WIDE_INT_PRINT_HEX,
16592 (INTVAL (x) >> 16) & 0xffff);
16593 return;
16595 case 'U':
16596 /* Print `u' if this has an auto-increment or auto-decrement. */
16597 if (MEM_P (x)
16598 && (GET_CODE (XEXP (x, 0)) == PRE_INC
16599 || GET_CODE (XEXP (x, 0)) == PRE_DEC
16600 || GET_CODE (XEXP (x, 0)) == PRE_MODIFY))
16601 putc ('u', file);
16602 return;
16604 case 'V':
16605 /* Print the trap code for this operand. */
16606 switch (GET_CODE (x))
16608 case EQ:
16609 fputs ("eq", file); /* 4 */
16610 break;
16611 case NE:
16612 fputs ("ne", file); /* 24 */
16613 break;
16614 case LT:
16615 fputs ("lt", file); /* 16 */
16616 break;
16617 case LE:
16618 fputs ("le", file); /* 20 */
16619 break;
16620 case GT:
16621 fputs ("gt", file); /* 8 */
16622 break;
16623 case GE:
16624 fputs ("ge", file); /* 12 */
16625 break;
16626 case LTU:
16627 fputs ("llt", file); /* 2 */
16628 break;
16629 case LEU:
16630 fputs ("lle", file); /* 6 */
16631 break;
16632 case GTU:
16633 fputs ("lgt", file); /* 1 */
16634 break;
16635 case GEU:
16636 fputs ("lge", file); /* 5 */
16637 break;
16638 default:
16639 gcc_unreachable ();
16641 break;
16643 case 'w':
16644 /* If constant, low-order 16 bits of constant, signed. Otherwise, write
16645 normally. */
16646 if (INT_P (x))
16647 fprintf (file, HOST_WIDE_INT_PRINT_DEC,
16648 ((INTVAL (x) & 0xffff) ^ 0x8000) - 0x8000);
16649 else
16650 print_operand (file, x, 0);
16651 return;
16653 case 'W':
16654 /* MB value for a PowerPC64 rldic operand. */
16655 i = clz_hwi (INTVAL (x));
16657 fprintf (file, "%d", i);
16658 return;
16660 case 'x':
16661 /* X is a FPR or Altivec register used in a VSX context. */
16662 if (GET_CODE (x) != REG || !VSX_REGNO_P (REGNO (x)))
16663 output_operand_lossage ("invalid %%x value");
16664 else
16666 int reg = REGNO (x);
16667 int vsx_reg = (FP_REGNO_P (reg)
16668 ? reg - 32
16669 : reg - FIRST_ALTIVEC_REGNO + 32);
16671 #ifdef TARGET_REGNAMES
16672 if (TARGET_REGNAMES)
16673 fprintf (file, "%%vs%d", vsx_reg);
16674 else
16675 #endif
16676 fprintf (file, "%d", vsx_reg);
16678 return;
16680 case 'X':
16681 if (MEM_P (x)
16682 && (legitimate_indexed_address_p (XEXP (x, 0), 0)
16683 || (GET_CODE (XEXP (x, 0)) == PRE_MODIFY
16684 && legitimate_indexed_address_p (XEXP (XEXP (x, 0), 1), 0))))
16685 putc ('x', file);
16686 return;
16688 case 'Y':
16689 /* Like 'L', for third word of TImode/PTImode */
16690 if (REG_P (x))
16691 fputs (reg_names[REGNO (x) + 2], file);
16692 else if (MEM_P (x))
16694 if (GET_CODE (XEXP (x, 0)) == PRE_INC
16695 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
16696 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 8));
16697 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
16698 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 8));
16699 else
16700 output_address (XEXP (adjust_address_nv (x, SImode, 8), 0));
16701 if (small_data_operand (x, GET_MODE (x)))
16702 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
16703 reg_names[SMALL_DATA_REG]);
16705 return;
16707 case 'z':
16708 /* X is a SYMBOL_REF. Write out the name preceded by a
16709 period and without any trailing data in brackets. Used for function
16710 names. If we are configured for System V (or the embedded ABI) on
16711 the PowerPC, do not emit the period, since those systems do not use
16712 TOCs and the like. */
16713 gcc_assert (GET_CODE (x) == SYMBOL_REF);
16715 /* For macho, check to see if we need a stub. */
16716 if (TARGET_MACHO)
16718 const char *name = XSTR (x, 0);
16719 #if TARGET_MACHO
16720 if (darwin_emit_branch_islands
16721 && MACHOPIC_INDIRECT
16722 && machopic_classify_symbol (x) == MACHOPIC_UNDEFINED_FUNCTION)
16723 name = machopic_indirection_name (x, /*stub_p=*/true);
16724 #endif
16725 assemble_name (file, name);
16727 else if (!DOT_SYMBOLS)
16728 assemble_name (file, XSTR (x, 0));
16729 else
16730 rs6000_output_function_entry (file, XSTR (x, 0));
16731 return;
16733 case 'Z':
16734 /* Like 'L', for last word of TImode/PTImode. */
16735 if (REG_P (x))
16736 fputs (reg_names[REGNO (x) + 3], file);
16737 else if (MEM_P (x))
16739 if (GET_CODE (XEXP (x, 0)) == PRE_INC
16740 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
16741 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 12));
16742 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
16743 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 12));
16744 else
16745 output_address (XEXP (adjust_address_nv (x, SImode, 12), 0));
16746 if (small_data_operand (x, GET_MODE (x)))
16747 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
16748 reg_names[SMALL_DATA_REG]);
16750 return;
16752 /* Print AltiVec or SPE memory operand. */
16753 case 'y':
16755 rtx tmp;
16757 gcc_assert (MEM_P (x));
16759 tmp = XEXP (x, 0);
16761 /* Ugly hack because %y is overloaded. */
16762 if ((TARGET_SPE || TARGET_E500_DOUBLE)
16763 && (GET_MODE_SIZE (GET_MODE (x)) == 8
16764 || GET_MODE (x) == TFmode
16765 || GET_MODE (x) == TImode
16766 || GET_MODE (x) == PTImode))
16768 /* Handle [reg]. */
16769 if (REG_P (tmp))
16771 fprintf (file, "0(%s)", reg_names[REGNO (tmp)]);
16772 break;
16774 /* Handle [reg+UIMM]. */
16775 else if (GET_CODE (tmp) == PLUS &&
16776 GET_CODE (XEXP (tmp, 1)) == CONST_INT)
16778 int x;
16780 gcc_assert (REG_P (XEXP (tmp, 0)));
16782 x = INTVAL (XEXP (tmp, 1));
16783 fprintf (file, "%d(%s)", x, reg_names[REGNO (XEXP (tmp, 0))]);
16784 break;
16787 /* Fall through. Must be [reg+reg]. */
16789 if (VECTOR_MEM_ALTIVEC_P (GET_MODE (x))
16790 && GET_CODE (tmp) == AND
16791 && GET_CODE (XEXP (tmp, 1)) == CONST_INT
16792 && INTVAL (XEXP (tmp, 1)) == -16)
16793 tmp = XEXP (tmp, 0);
16794 else if (VECTOR_MEM_VSX_P (GET_MODE (x))
16795 && GET_CODE (tmp) == PRE_MODIFY)
16796 tmp = XEXP (tmp, 1);
16797 if (REG_P (tmp))
16798 fprintf (file, "0,%s", reg_names[REGNO (tmp)]);
16799 else
16801 if (!GET_CODE (tmp) == PLUS
16802 || !REG_P (XEXP (tmp, 0))
16803 || !REG_P (XEXP (tmp, 1)))
16805 output_operand_lossage ("invalid %%y value, try using the 'Z' constraint");
16806 break;
16809 if (REGNO (XEXP (tmp, 0)) == 0)
16810 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 1)) ],
16811 reg_names[ REGNO (XEXP (tmp, 0)) ]);
16812 else
16813 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 0)) ],
16814 reg_names[ REGNO (XEXP (tmp, 1)) ]);
16816 break;
16819 case 0:
16820 if (REG_P (x))
16821 fprintf (file, "%s", reg_names[REGNO (x)]);
16822 else if (MEM_P (x))
16824 /* We need to handle PRE_INC and PRE_DEC here, since we need to
16825 know the width from the mode. */
16826 if (GET_CODE (XEXP (x, 0)) == PRE_INC)
16827 fprintf (file, "%d(%s)", GET_MODE_SIZE (GET_MODE (x)),
16828 reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
16829 else if (GET_CODE (XEXP (x, 0)) == PRE_DEC)
16830 fprintf (file, "%d(%s)", - GET_MODE_SIZE (GET_MODE (x)),
16831 reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
16832 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
16833 output_address (XEXP (XEXP (x, 0), 1));
16834 else
16835 output_address (XEXP (x, 0));
16837 else
16839 if (toc_relative_expr_p (x, false))
16840 /* This hack along with a corresponding hack in
16841 rs6000_output_addr_const_extra arranges to output addends
16842 where the assembler expects to find them. eg.
16843 (plus (unspec [(symbol_ref ("x")) (reg 2)] tocrel) 4)
16844 without this hack would be output as "x@toc+4". We
16845 want "x+4@toc". */
16846 output_addr_const (file, CONST_CAST_RTX (tocrel_base));
16847 else
16848 output_addr_const (file, x);
16850 return;
16852 case '&':
16853 assemble_name (file, rs6000_get_some_local_dynamic_name ());
16854 return;
16856 default:
16857 output_operand_lossage ("invalid %%xn code");
16861 /* Print the address of an operand. */
16863 void
16864 print_operand_address (FILE *file, rtx x)
16866 if (REG_P (x))
16867 fprintf (file, "0(%s)", reg_names[ REGNO (x) ]);
16868 else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST
16869 || GET_CODE (x) == LABEL_REF)
16871 output_addr_const (file, x);
16872 if (small_data_operand (x, GET_MODE (x)))
16873 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
16874 reg_names[SMALL_DATA_REG]);
16875 else
16876 gcc_assert (!TARGET_TOC);
16878 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
16879 && REG_P (XEXP (x, 1)))
16881 if (REGNO (XEXP (x, 0)) == 0)
16882 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 1)) ],
16883 reg_names[ REGNO (XEXP (x, 0)) ]);
16884 else
16885 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 0)) ],
16886 reg_names[ REGNO (XEXP (x, 1)) ]);
16888 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
16889 && GET_CODE (XEXP (x, 1)) == CONST_INT)
16890 fprintf (file, HOST_WIDE_INT_PRINT_DEC "(%s)",
16891 INTVAL (XEXP (x, 1)), reg_names[ REGNO (XEXP (x, 0)) ]);
16892 #if TARGET_MACHO
16893 else if (GET_CODE (x) == LO_SUM && REG_P (XEXP (x, 0))
16894 && CONSTANT_P (XEXP (x, 1)))
16896 fprintf (file, "lo16(");
16897 output_addr_const (file, XEXP (x, 1));
16898 fprintf (file, ")(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
16900 #endif
16901 #if TARGET_ELF
16902 else if (GET_CODE (x) == LO_SUM && REG_P (XEXP (x, 0))
16903 && CONSTANT_P (XEXP (x, 1)))
16905 output_addr_const (file, XEXP (x, 1));
16906 fprintf (file, "@l(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
16908 #endif
16909 else if (toc_relative_expr_p (x, false))
16911 /* This hack along with a corresponding hack in
16912 rs6000_output_addr_const_extra arranges to output addends
16913 where the assembler expects to find them. eg.
16914 (lo_sum (reg 9)
16915 . (plus (unspec [(symbol_ref ("x")) (reg 2)] tocrel) 8))
16916 without this hack would be output as "x@toc+8@l(9)". We
16917 want "x+8@toc@l(9)". */
16918 output_addr_const (file, CONST_CAST_RTX (tocrel_base));
16919 if (GET_CODE (x) == LO_SUM)
16920 fprintf (file, "@l(%s)", reg_names[REGNO (XEXP (x, 0))]);
16921 else
16922 fprintf (file, "(%s)", reg_names[REGNO (XVECEXP (tocrel_base, 0, 1))]);
16924 else
16925 gcc_unreachable ();
16928 /* Implement TARGET_OUTPUT_ADDR_CONST_EXTRA. */
16930 static bool
16931 rs6000_output_addr_const_extra (FILE *file, rtx x)
16933 if (GET_CODE (x) == UNSPEC)
16934 switch (XINT (x, 1))
16936 case UNSPEC_TOCREL:
16937 gcc_checking_assert (GET_CODE (XVECEXP (x, 0, 0)) == SYMBOL_REF
16938 && REG_P (XVECEXP (x, 0, 1))
16939 && REGNO (XVECEXP (x, 0, 1)) == TOC_REGISTER);
16940 output_addr_const (file, XVECEXP (x, 0, 0));
16941 if (x == tocrel_base && tocrel_offset != const0_rtx)
16943 if (INTVAL (tocrel_offset) >= 0)
16944 fprintf (file, "+");
16945 output_addr_const (file, CONST_CAST_RTX (tocrel_offset));
16947 if (!TARGET_AIX || (TARGET_ELF && TARGET_MINIMAL_TOC))
16949 putc ('-', file);
16950 assemble_name (file, toc_label_name);
16952 else if (TARGET_ELF)
16953 fputs ("@toc", file);
16954 return true;
16956 #if TARGET_MACHO
16957 case UNSPEC_MACHOPIC_OFFSET:
16958 output_addr_const (file, XVECEXP (x, 0, 0));
16959 putc ('-', file);
16960 machopic_output_function_base_name (file);
16961 return true;
16962 #endif
16964 return false;
16967 /* Target hook for assembling integer objects. The PowerPC version has
16968 to handle fixup entries for relocatable code if RELOCATABLE_NEEDS_FIXUP
16969 is defined. It also needs to handle DI-mode objects on 64-bit
16970 targets. */
16972 static bool
16973 rs6000_assemble_integer (rtx x, unsigned int size, int aligned_p)
16975 #ifdef RELOCATABLE_NEEDS_FIXUP
16976 /* Special handling for SI values. */
16977 if (RELOCATABLE_NEEDS_FIXUP && size == 4 && aligned_p)
16979 static int recurse = 0;
16981 /* For -mrelocatable, we mark all addresses that need to be fixed up in
16982 the .fixup section. Since the TOC section is already relocated, we
16983 don't need to mark it here. We used to skip the text section, but it
16984 should never be valid for relocated addresses to be placed in the text
16985 section. */
16986 if (TARGET_RELOCATABLE
16987 && in_section != toc_section
16988 && !recurse
16989 && GET_CODE (x) != CONST_INT
16990 && GET_CODE (x) != CONST_DOUBLE
16991 && CONSTANT_P (x))
16993 char buf[256];
16995 recurse = 1;
16996 ASM_GENERATE_INTERNAL_LABEL (buf, "LCP", fixuplabelno);
16997 fixuplabelno++;
16998 ASM_OUTPUT_LABEL (asm_out_file, buf);
16999 fprintf (asm_out_file, "\t.long\t(");
17000 output_addr_const (asm_out_file, x);
17001 fprintf (asm_out_file, ")@fixup\n");
17002 fprintf (asm_out_file, "\t.section\t\".fixup\",\"aw\"\n");
17003 ASM_OUTPUT_ALIGN (asm_out_file, 2);
17004 fprintf (asm_out_file, "\t.long\t");
17005 assemble_name (asm_out_file, buf);
17006 fprintf (asm_out_file, "\n\t.previous\n");
17007 recurse = 0;
17008 return true;
17010 /* Remove initial .'s to turn a -mcall-aixdesc function
17011 address into the address of the descriptor, not the function
17012 itself. */
17013 else if (GET_CODE (x) == SYMBOL_REF
17014 && XSTR (x, 0)[0] == '.'
17015 && DEFAULT_ABI == ABI_AIX)
17017 const char *name = XSTR (x, 0);
17018 while (*name == '.')
17019 name++;
17021 fprintf (asm_out_file, "\t.long\t%s\n", name);
17022 return true;
17025 #endif /* RELOCATABLE_NEEDS_FIXUP */
17026 return default_assemble_integer (x, size, aligned_p);
17029 #if defined (HAVE_GAS_HIDDEN) && !TARGET_MACHO
17030 /* Emit an assembler directive to set symbol visibility for DECL to
17031 VISIBILITY_TYPE. */
17033 static void
17034 rs6000_assemble_visibility (tree decl, int vis)
17036 if (TARGET_XCOFF)
17037 return;
17039 /* Functions need to have their entry point symbol visibility set as
17040 well as their descriptor symbol visibility. */
17041 if (DEFAULT_ABI == ABI_AIX
17042 && DOT_SYMBOLS
17043 && TREE_CODE (decl) == FUNCTION_DECL)
17045 static const char * const visibility_types[] = {
17046 NULL, "internal", "hidden", "protected"
17049 const char *name, *type;
17051 name = ((* targetm.strip_name_encoding)
17052 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
17053 type = visibility_types[vis];
17055 fprintf (asm_out_file, "\t.%s\t%s\n", type, name);
17056 fprintf (asm_out_file, "\t.%s\t.%s\n", type, name);
17058 else
17059 default_assemble_visibility (decl, vis);
17061 #endif
17063 enum rtx_code
17064 rs6000_reverse_condition (enum machine_mode mode, enum rtx_code code)
17066 /* Reversal of FP compares takes care -- an ordered compare
17067 becomes an unordered compare and vice versa. */
17068 if (mode == CCFPmode
17069 && (!flag_finite_math_only
17070 || code == UNLT || code == UNLE || code == UNGT || code == UNGE
17071 || code == UNEQ || code == LTGT))
17072 return reverse_condition_maybe_unordered (code);
17073 else
17074 return reverse_condition (code);
17077 /* Generate a compare for CODE. Return a brand-new rtx that
17078 represents the result of the compare. */
17080 static rtx
17081 rs6000_generate_compare (rtx cmp, enum machine_mode mode)
17083 enum machine_mode comp_mode;
17084 rtx compare_result;
17085 enum rtx_code code = GET_CODE (cmp);
17086 rtx op0 = XEXP (cmp, 0);
17087 rtx op1 = XEXP (cmp, 1);
17089 if (FLOAT_MODE_P (mode))
17090 comp_mode = CCFPmode;
17091 else if (code == GTU || code == LTU
17092 || code == GEU || code == LEU)
17093 comp_mode = CCUNSmode;
17094 else if ((code == EQ || code == NE)
17095 && unsigned_reg_p (op0)
17096 && (unsigned_reg_p (op1)
17097 || (CONST_INT_P (op1) && INTVAL (op1) != 0)))
17098 /* These are unsigned values, perhaps there will be a later
17099 ordering compare that can be shared with this one. */
17100 comp_mode = CCUNSmode;
17101 else
17102 comp_mode = CCmode;
17104 /* If we have an unsigned compare, make sure we don't have a signed value as
17105 an immediate. */
17106 if (comp_mode == CCUNSmode && GET_CODE (op1) == CONST_INT
17107 && INTVAL (op1) < 0)
17109 op0 = copy_rtx_if_shared (op0);
17110 op1 = force_reg (GET_MODE (op0), op1);
17111 cmp = gen_rtx_fmt_ee (code, GET_MODE (cmp), op0, op1);
17114 /* First, the compare. */
17115 compare_result = gen_reg_rtx (comp_mode);
17117 /* E500 FP compare instructions on the GPRs. Yuck! */
17118 if ((!TARGET_FPRS && TARGET_HARD_FLOAT)
17119 && FLOAT_MODE_P (mode))
17121 rtx cmp, or_result, compare_result2;
17122 enum machine_mode op_mode = GET_MODE (op0);
17123 bool reverse_p;
17125 if (op_mode == VOIDmode)
17126 op_mode = GET_MODE (op1);
17128 /* First reverse the condition codes that aren't directly supported. */
17129 switch (code)
17131 case NE:
17132 case UNLT:
17133 case UNLE:
17134 case UNGT:
17135 case UNGE:
17136 code = reverse_condition_maybe_unordered (code);
17137 reverse_p = true;
17138 break;
17140 case EQ:
17141 case LT:
17142 case LE:
17143 case GT:
17144 case GE:
17145 reverse_p = false;
17146 break;
17148 default:
17149 gcc_unreachable ();
17152 /* The E500 FP compare instructions toggle the GT bit (CR bit 1) only.
17153 This explains the following mess. */
17155 switch (code)
17157 case EQ:
17158 switch (op_mode)
17160 case SFmode:
17161 cmp = (flag_finite_math_only && !flag_trapping_math)
17162 ? gen_tstsfeq_gpr (compare_result, op0, op1)
17163 : gen_cmpsfeq_gpr (compare_result, op0, op1);
17164 break;
17166 case DFmode:
17167 cmp = (flag_finite_math_only && !flag_trapping_math)
17168 ? gen_tstdfeq_gpr (compare_result, op0, op1)
17169 : gen_cmpdfeq_gpr (compare_result, op0, op1);
17170 break;
17172 case TFmode:
17173 cmp = (flag_finite_math_only && !flag_trapping_math)
17174 ? gen_tsttfeq_gpr (compare_result, op0, op1)
17175 : gen_cmptfeq_gpr (compare_result, op0, op1);
17176 break;
17178 default:
17179 gcc_unreachable ();
17181 break;
17183 case GT:
17184 case GE:
17185 switch (op_mode)
17187 case SFmode:
17188 cmp = (flag_finite_math_only && !flag_trapping_math)
17189 ? gen_tstsfgt_gpr (compare_result, op0, op1)
17190 : gen_cmpsfgt_gpr (compare_result, op0, op1);
17191 break;
17193 case DFmode:
17194 cmp = (flag_finite_math_only && !flag_trapping_math)
17195 ? gen_tstdfgt_gpr (compare_result, op0, op1)
17196 : gen_cmpdfgt_gpr (compare_result, op0, op1);
17197 break;
17199 case TFmode:
17200 cmp = (flag_finite_math_only && !flag_trapping_math)
17201 ? gen_tsttfgt_gpr (compare_result, op0, op1)
17202 : gen_cmptfgt_gpr (compare_result, op0, op1);
17203 break;
17205 default:
17206 gcc_unreachable ();
17208 break;
17210 case LT:
17211 case LE:
17212 switch (op_mode)
17214 case SFmode:
17215 cmp = (flag_finite_math_only && !flag_trapping_math)
17216 ? gen_tstsflt_gpr (compare_result, op0, op1)
17217 : gen_cmpsflt_gpr (compare_result, op0, op1);
17218 break;
17220 case DFmode:
17221 cmp = (flag_finite_math_only && !flag_trapping_math)
17222 ? gen_tstdflt_gpr (compare_result, op0, op1)
17223 : gen_cmpdflt_gpr (compare_result, op0, op1);
17224 break;
17226 case TFmode:
17227 cmp = (flag_finite_math_only && !flag_trapping_math)
17228 ? gen_tsttflt_gpr (compare_result, op0, op1)
17229 : gen_cmptflt_gpr (compare_result, op0, op1);
17230 break;
17232 default:
17233 gcc_unreachable ();
17235 break;
17237 default:
17238 gcc_unreachable ();
17241 /* Synthesize LE and GE from LT/GT || EQ. */
17242 if (code == LE || code == GE)
17244 emit_insn (cmp);
17246 compare_result2 = gen_reg_rtx (CCFPmode);
17248 /* Do the EQ. */
17249 switch (op_mode)
17251 case SFmode:
17252 cmp = (flag_finite_math_only && !flag_trapping_math)
17253 ? gen_tstsfeq_gpr (compare_result2, op0, op1)
17254 : gen_cmpsfeq_gpr (compare_result2, op0, op1);
17255 break;
17257 case DFmode:
17258 cmp = (flag_finite_math_only && !flag_trapping_math)
17259 ? gen_tstdfeq_gpr (compare_result2, op0, op1)
17260 : gen_cmpdfeq_gpr (compare_result2, op0, op1);
17261 break;
17263 case TFmode:
17264 cmp = (flag_finite_math_only && !flag_trapping_math)
17265 ? gen_tsttfeq_gpr (compare_result2, op0, op1)
17266 : gen_cmptfeq_gpr (compare_result2, op0, op1);
17267 break;
17269 default:
17270 gcc_unreachable ();
17273 emit_insn (cmp);
17275 /* OR them together. */
17276 or_result = gen_reg_rtx (CCFPmode);
17277 cmp = gen_e500_cr_ior_compare (or_result, compare_result,
17278 compare_result2);
17279 compare_result = or_result;
17282 code = reverse_p ? NE : EQ;
17284 emit_insn (cmp);
17286 else
17288 /* Generate XLC-compatible TFmode compare as PARALLEL with extra
17289 CLOBBERs to match cmptf_internal2 pattern. */
17290 if (comp_mode == CCFPmode && TARGET_XL_COMPAT
17291 && GET_MODE (op0) == TFmode
17292 && !TARGET_IEEEQUAD
17293 && TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_LONG_DOUBLE_128)
17294 emit_insn (gen_rtx_PARALLEL (VOIDmode,
17295 gen_rtvec (10,
17296 gen_rtx_SET (VOIDmode,
17297 compare_result,
17298 gen_rtx_COMPARE (comp_mode, op0, op1)),
17299 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
17300 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
17301 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
17302 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
17303 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
17304 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
17305 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
17306 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
17307 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (Pmode)))));
17308 else if (GET_CODE (op1) == UNSPEC
17309 && XINT (op1, 1) == UNSPEC_SP_TEST)
17311 rtx op1b = XVECEXP (op1, 0, 0);
17312 comp_mode = CCEQmode;
17313 compare_result = gen_reg_rtx (CCEQmode);
17314 if (TARGET_64BIT)
17315 emit_insn (gen_stack_protect_testdi (compare_result, op0, op1b));
17316 else
17317 emit_insn (gen_stack_protect_testsi (compare_result, op0, op1b));
17319 else
17320 emit_insn (gen_rtx_SET (VOIDmode, compare_result,
17321 gen_rtx_COMPARE (comp_mode, op0, op1)));
17324 /* Some kinds of FP comparisons need an OR operation;
17325 under flag_finite_math_only we don't bother. */
17326 if (FLOAT_MODE_P (mode)
17327 && !flag_finite_math_only
17328 && !(TARGET_HARD_FLOAT && !TARGET_FPRS)
17329 && (code == LE || code == GE
17330 || code == UNEQ || code == LTGT
17331 || code == UNGT || code == UNLT))
17333 enum rtx_code or1, or2;
17334 rtx or1_rtx, or2_rtx, compare2_rtx;
17335 rtx or_result = gen_reg_rtx (CCEQmode);
17337 switch (code)
17339 case LE: or1 = LT; or2 = EQ; break;
17340 case GE: or1 = GT; or2 = EQ; break;
17341 case UNEQ: or1 = UNORDERED; or2 = EQ; break;
17342 case LTGT: or1 = LT; or2 = GT; break;
17343 case UNGT: or1 = UNORDERED; or2 = GT; break;
17344 case UNLT: or1 = UNORDERED; or2 = LT; break;
17345 default: gcc_unreachable ();
17347 validate_condition_mode (or1, comp_mode);
17348 validate_condition_mode (or2, comp_mode);
17349 or1_rtx = gen_rtx_fmt_ee (or1, SImode, compare_result, const0_rtx);
17350 or2_rtx = gen_rtx_fmt_ee (or2, SImode, compare_result, const0_rtx);
17351 compare2_rtx = gen_rtx_COMPARE (CCEQmode,
17352 gen_rtx_IOR (SImode, or1_rtx, or2_rtx),
17353 const_true_rtx);
17354 emit_insn (gen_rtx_SET (VOIDmode, or_result, compare2_rtx));
17356 compare_result = or_result;
17357 code = EQ;
17360 validate_condition_mode (code, GET_MODE (compare_result));
17362 return gen_rtx_fmt_ee (code, VOIDmode, compare_result, const0_rtx);
17366 /* Emit the RTL for an sISEL pattern. */
17368 void
17369 rs6000_emit_sISEL (enum machine_mode mode ATTRIBUTE_UNUSED, rtx operands[])
17371 rs6000_emit_int_cmove (operands[0], operands[1], const1_rtx, const0_rtx);
17374 void
17375 rs6000_emit_sCOND (enum machine_mode mode, rtx operands[])
17377 rtx condition_rtx;
17378 enum machine_mode op_mode;
17379 enum rtx_code cond_code;
17380 rtx result = operands[0];
17382 if (TARGET_ISEL && (mode == SImode || mode == DImode))
17384 rs6000_emit_sISEL (mode, operands);
17385 return;
17388 condition_rtx = rs6000_generate_compare (operands[1], mode);
17389 cond_code = GET_CODE (condition_rtx);
17391 if (FLOAT_MODE_P (mode)
17392 && !TARGET_FPRS && TARGET_HARD_FLOAT)
17394 rtx t;
17396 PUT_MODE (condition_rtx, SImode);
17397 t = XEXP (condition_rtx, 0);
17399 gcc_assert (cond_code == NE || cond_code == EQ);
17401 if (cond_code == NE)
17402 emit_insn (gen_e500_flip_gt_bit (t, t));
17404 emit_insn (gen_move_from_CR_gt_bit (result, t));
17405 return;
17408 if (cond_code == NE
17409 || cond_code == GE || cond_code == LE
17410 || cond_code == GEU || cond_code == LEU
17411 || cond_code == ORDERED || cond_code == UNGE || cond_code == UNLE)
17413 rtx not_result = gen_reg_rtx (CCEQmode);
17414 rtx not_op, rev_cond_rtx;
17415 enum machine_mode cc_mode;
17417 cc_mode = GET_MODE (XEXP (condition_rtx, 0));
17419 rev_cond_rtx = gen_rtx_fmt_ee (rs6000_reverse_condition (cc_mode, cond_code),
17420 SImode, XEXP (condition_rtx, 0), const0_rtx);
17421 not_op = gen_rtx_COMPARE (CCEQmode, rev_cond_rtx, const0_rtx);
17422 emit_insn (gen_rtx_SET (VOIDmode, not_result, not_op));
17423 condition_rtx = gen_rtx_EQ (VOIDmode, not_result, const0_rtx);
17426 op_mode = GET_MODE (XEXP (operands[1], 0));
17427 if (op_mode == VOIDmode)
17428 op_mode = GET_MODE (XEXP (operands[1], 1));
17430 if (TARGET_POWERPC64 && (op_mode == DImode || FLOAT_MODE_P (mode)))
17432 PUT_MODE (condition_rtx, DImode);
17433 convert_move (result, condition_rtx, 0);
17435 else
17437 PUT_MODE (condition_rtx, SImode);
17438 emit_insn (gen_rtx_SET (VOIDmode, result, condition_rtx));
17442 /* Emit a branch of kind CODE to location LOC. */
17444 void
17445 rs6000_emit_cbranch (enum machine_mode mode, rtx operands[])
17447 rtx condition_rtx, loc_ref;
17449 condition_rtx = rs6000_generate_compare (operands[0], mode);
17450 loc_ref = gen_rtx_LABEL_REF (VOIDmode, operands[3]);
17451 emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx,
17452 gen_rtx_IF_THEN_ELSE (VOIDmode, condition_rtx,
17453 loc_ref, pc_rtx)));
17456 /* Return the string to output a conditional branch to LABEL, which is
17457 the operand template of the label, or NULL if the branch is really a
17458 conditional return.
17460 OP is the conditional expression. XEXP (OP, 0) is assumed to be a
17461 condition code register and its mode specifies what kind of
17462 comparison we made.
17464 REVERSED is nonzero if we should reverse the sense of the comparison.
17466 INSN is the insn. */
17468 char *
17469 output_cbranch (rtx op, const char *label, int reversed, rtx insn)
17471 static char string[64];
17472 enum rtx_code code = GET_CODE (op);
17473 rtx cc_reg = XEXP (op, 0);
17474 enum machine_mode mode = GET_MODE (cc_reg);
17475 int cc_regno = REGNO (cc_reg) - CR0_REGNO;
17476 int need_longbranch = label != NULL && get_attr_length (insn) == 8;
17477 int really_reversed = reversed ^ need_longbranch;
17478 char *s = string;
17479 const char *ccode;
17480 const char *pred;
17481 rtx note;
17483 validate_condition_mode (code, mode);
17485 /* Work out which way this really branches. We could use
17486 reverse_condition_maybe_unordered here always but this
17487 makes the resulting assembler clearer. */
17488 if (really_reversed)
17490 /* Reversal of FP compares takes care -- an ordered compare
17491 becomes an unordered compare and vice versa. */
17492 if (mode == CCFPmode)
17493 code = reverse_condition_maybe_unordered (code);
17494 else
17495 code = reverse_condition (code);
17498 if ((!TARGET_FPRS && TARGET_HARD_FLOAT) && mode == CCFPmode)
17500 /* The efscmp/tst* instructions twiddle bit 2, which maps nicely
17501 to the GT bit. */
17502 switch (code)
17504 case EQ:
17505 /* Opposite of GT. */
17506 code = GT;
17507 break;
17509 case NE:
17510 code = UNLE;
17511 break;
17513 default:
17514 gcc_unreachable ();
17518 switch (code)
17520 /* Not all of these are actually distinct opcodes, but
17521 we distinguish them for clarity of the resulting assembler. */
17522 case NE: case LTGT:
17523 ccode = "ne"; break;
17524 case EQ: case UNEQ:
17525 ccode = "eq"; break;
17526 case GE: case GEU:
17527 ccode = "ge"; break;
17528 case GT: case GTU: case UNGT:
17529 ccode = "gt"; break;
17530 case LE: case LEU:
17531 ccode = "le"; break;
17532 case LT: case LTU: case UNLT:
17533 ccode = "lt"; break;
17534 case UNORDERED: ccode = "un"; break;
17535 case ORDERED: ccode = "nu"; break;
17536 case UNGE: ccode = "nl"; break;
17537 case UNLE: ccode = "ng"; break;
17538 default:
17539 gcc_unreachable ();
17542 /* Maybe we have a guess as to how likely the branch is. */
17543 pred = "";
17544 note = find_reg_note (insn, REG_BR_PROB, NULL_RTX);
17545 if (note != NULL_RTX)
17547 /* PROB is the difference from 50%. */
17548 int prob = INTVAL (XEXP (note, 0)) - REG_BR_PROB_BASE / 2;
17550 /* Only hint for highly probable/improbable branches on newer
17551 cpus as static prediction overrides processor dynamic
17552 prediction. For older cpus we may as well always hint, but
17553 assume not taken for branches that are very close to 50% as a
17554 mispredicted taken branch is more expensive than a
17555 mispredicted not-taken branch. */
17556 if (rs6000_always_hint
17557 || (abs (prob) > REG_BR_PROB_BASE / 100 * 48
17558 && br_prob_note_reliable_p (note)))
17560 if (abs (prob) > REG_BR_PROB_BASE / 20
17561 && ((prob > 0) ^ need_longbranch))
17562 pred = "+";
17563 else
17564 pred = "-";
17568 if (label == NULL)
17569 s += sprintf (s, "b%slr%s ", ccode, pred);
17570 else
17571 s += sprintf (s, "b%s%s ", ccode, pred);
17573 /* We need to escape any '%' characters in the reg_names string.
17574 Assume they'd only be the first character.... */
17575 if (reg_names[cc_regno + CR0_REGNO][0] == '%')
17576 *s++ = '%';
17577 s += sprintf (s, "%s", reg_names[cc_regno + CR0_REGNO]);
17579 if (label != NULL)
17581 /* If the branch distance was too far, we may have to use an
17582 unconditional branch to go the distance. */
17583 if (need_longbranch)
17584 s += sprintf (s, ",$+8\n\tb %s", label);
17585 else
17586 s += sprintf (s, ",%s", label);
17589 return string;
17592 /* Return the string to flip the GT bit on a CR. */
17593 char *
17594 output_e500_flip_gt_bit (rtx dst, rtx src)
17596 static char string[64];
17597 int a, b;
17599 gcc_assert (GET_CODE (dst) == REG && CR_REGNO_P (REGNO (dst))
17600 && GET_CODE (src) == REG && CR_REGNO_P (REGNO (src)));
17602 /* GT bit. */
17603 a = 4 * (REGNO (dst) - CR0_REGNO) + 1;
17604 b = 4 * (REGNO (src) - CR0_REGNO) + 1;
17606 sprintf (string, "crnot %d,%d", a, b);
17607 return string;
17610 /* Return insn for VSX or Altivec comparisons. */
17612 static rtx
17613 rs6000_emit_vector_compare_inner (enum rtx_code code, rtx op0, rtx op1)
17615 rtx mask;
17616 enum machine_mode mode = GET_MODE (op0);
17618 switch (code)
17620 default:
17621 break;
17623 case GE:
17624 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
17625 return NULL_RTX;
17627 case EQ:
17628 case GT:
17629 case GTU:
17630 case ORDERED:
17631 case UNORDERED:
17632 case UNEQ:
17633 case LTGT:
17634 mask = gen_reg_rtx (mode);
17635 emit_insn (gen_rtx_SET (VOIDmode,
17636 mask,
17637 gen_rtx_fmt_ee (code, mode, op0, op1)));
17638 return mask;
17641 return NULL_RTX;
17644 /* Emit vector compare for operands OP0 and OP1 using code RCODE.
17645 DMODE is expected destination mode. This is a recursive function. */
17647 static rtx
17648 rs6000_emit_vector_compare (enum rtx_code rcode,
17649 rtx op0, rtx op1,
17650 enum machine_mode dmode)
17652 rtx mask;
17653 bool swap_operands = false;
17654 bool try_again = false;
17656 gcc_assert (VECTOR_UNIT_ALTIVEC_OR_VSX_P (dmode));
17657 gcc_assert (GET_MODE (op0) == GET_MODE (op1));
17659 /* See if the comparison works as is. */
17660 mask = rs6000_emit_vector_compare_inner (rcode, op0, op1);
17661 if (mask)
17662 return mask;
17664 switch (rcode)
17666 case LT:
17667 rcode = GT;
17668 swap_operands = true;
17669 try_again = true;
17670 break;
17671 case LTU:
17672 rcode = GTU;
17673 swap_operands = true;
17674 try_again = true;
17675 break;
17676 case NE:
17677 case UNLE:
17678 case UNLT:
17679 case UNGE:
17680 case UNGT:
17681 /* Invert condition and try again.
17682 e.g., A != B becomes ~(A==B). */
17684 enum rtx_code rev_code;
17685 enum insn_code nor_code;
17686 rtx mask2;
17688 rev_code = reverse_condition_maybe_unordered (rcode);
17689 if (rev_code == UNKNOWN)
17690 return NULL_RTX;
17692 nor_code = optab_handler (one_cmpl_optab, dmode);
17693 if (nor_code == CODE_FOR_nothing)
17694 return NULL_RTX;
17696 mask2 = rs6000_emit_vector_compare (rev_code, op0, op1, dmode);
17697 if (!mask2)
17698 return NULL_RTX;
17700 mask = gen_reg_rtx (dmode);
17701 emit_insn (GEN_FCN (nor_code) (mask, mask2));
17702 return mask;
17704 break;
17705 case GE:
17706 case GEU:
17707 case LE:
17708 case LEU:
17709 /* Try GT/GTU/LT/LTU OR EQ */
17711 rtx c_rtx, eq_rtx;
17712 enum insn_code ior_code;
17713 enum rtx_code new_code;
17715 switch (rcode)
17717 case GE:
17718 new_code = GT;
17719 break;
17721 case GEU:
17722 new_code = GTU;
17723 break;
17725 case LE:
17726 new_code = LT;
17727 break;
17729 case LEU:
17730 new_code = LTU;
17731 break;
17733 default:
17734 gcc_unreachable ();
17737 ior_code = optab_handler (ior_optab, dmode);
17738 if (ior_code == CODE_FOR_nothing)
17739 return NULL_RTX;
17741 c_rtx = rs6000_emit_vector_compare (new_code, op0, op1, dmode);
17742 if (!c_rtx)
17743 return NULL_RTX;
17745 eq_rtx = rs6000_emit_vector_compare (EQ, op0, op1, dmode);
17746 if (!eq_rtx)
17747 return NULL_RTX;
17749 mask = gen_reg_rtx (dmode);
17750 emit_insn (GEN_FCN (ior_code) (mask, c_rtx, eq_rtx));
17751 return mask;
17753 break;
17754 default:
17755 return NULL_RTX;
17758 if (try_again)
17760 if (swap_operands)
17762 rtx tmp;
17763 tmp = op0;
17764 op0 = op1;
17765 op1 = tmp;
17768 mask = rs6000_emit_vector_compare_inner (rcode, op0, op1);
17769 if (mask)
17770 return mask;
17773 /* You only get two chances. */
17774 return NULL_RTX;
17777 /* Emit vector conditional expression. DEST is destination. OP_TRUE and
17778 OP_FALSE are two VEC_COND_EXPR operands. CC_OP0 and CC_OP1 are the two
17779 operands for the relation operation COND. */
17782 rs6000_emit_vector_cond_expr (rtx dest, rtx op_true, rtx op_false,
17783 rtx cond, rtx cc_op0, rtx cc_op1)
17785 enum machine_mode dest_mode = GET_MODE (dest);
17786 enum machine_mode mask_mode = GET_MODE (cc_op0);
17787 enum rtx_code rcode = GET_CODE (cond);
17788 enum machine_mode cc_mode = CCmode;
17789 rtx mask;
17790 rtx cond2;
17791 rtx tmp;
17792 bool invert_move = false;
17794 if (VECTOR_UNIT_NONE_P (dest_mode))
17795 return 0;
17797 gcc_assert (GET_MODE_SIZE (dest_mode) == GET_MODE_SIZE (mask_mode)
17798 && GET_MODE_NUNITS (dest_mode) == GET_MODE_NUNITS (mask_mode));
17800 switch (rcode)
17802 /* Swap operands if we can, and fall back to doing the operation as
17803 specified, and doing a NOR to invert the test. */
17804 case NE:
17805 case UNLE:
17806 case UNLT:
17807 case UNGE:
17808 case UNGT:
17809 /* Invert condition and try again.
17810 e.g., A = (B != C) ? D : E becomes A = (B == C) ? E : D. */
17811 invert_move = true;
17812 rcode = reverse_condition_maybe_unordered (rcode);
17813 if (rcode == UNKNOWN)
17814 return 0;
17815 break;
17817 /* Mark unsigned tests with CCUNSmode. */
17818 case GTU:
17819 case GEU:
17820 case LTU:
17821 case LEU:
17822 cc_mode = CCUNSmode;
17823 break;
17825 default:
17826 break;
17829 /* Get the vector mask for the given relational operations. */
17830 mask = rs6000_emit_vector_compare (rcode, cc_op0, cc_op1, mask_mode);
17832 if (!mask)
17833 return 0;
17835 if (invert_move)
17837 tmp = op_true;
17838 op_true = op_false;
17839 op_false = tmp;
17842 cond2 = gen_rtx_fmt_ee (NE, cc_mode, gen_lowpart (dest_mode, mask),
17843 CONST0_RTX (dest_mode));
17844 emit_insn (gen_rtx_SET (VOIDmode,
17845 dest,
17846 gen_rtx_IF_THEN_ELSE (dest_mode,
17847 cond2,
17848 op_true,
17849 op_false)));
17850 return 1;
17853 /* Emit a conditional move: move TRUE_COND to DEST if OP of the
17854 operands of the last comparison is nonzero/true, FALSE_COND if it
17855 is zero/false. Return 0 if the hardware has no such operation. */
17858 rs6000_emit_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
17860 enum rtx_code code = GET_CODE (op);
17861 rtx op0 = XEXP (op, 0);
17862 rtx op1 = XEXP (op, 1);
17863 REAL_VALUE_TYPE c1;
17864 enum machine_mode compare_mode = GET_MODE (op0);
17865 enum machine_mode result_mode = GET_MODE (dest);
17866 rtx temp;
17867 bool is_against_zero;
17869 /* These modes should always match. */
17870 if (GET_MODE (op1) != compare_mode
17871 /* In the isel case however, we can use a compare immediate, so
17872 op1 may be a small constant. */
17873 && (!TARGET_ISEL || !short_cint_operand (op1, VOIDmode)))
17874 return 0;
17875 if (GET_MODE (true_cond) != result_mode)
17876 return 0;
17877 if (GET_MODE (false_cond) != result_mode)
17878 return 0;
17880 /* Don't allow using floating point comparisons for integer results for
17881 now. */
17882 if (FLOAT_MODE_P (compare_mode) && !FLOAT_MODE_P (result_mode))
17883 return 0;
17885 /* First, work out if the hardware can do this at all, or
17886 if it's too slow.... */
17887 if (!FLOAT_MODE_P (compare_mode))
17889 if (TARGET_ISEL)
17890 return rs6000_emit_int_cmove (dest, op, true_cond, false_cond);
17891 return 0;
17893 else if (TARGET_HARD_FLOAT && !TARGET_FPRS
17894 && SCALAR_FLOAT_MODE_P (compare_mode))
17895 return 0;
17897 is_against_zero = op1 == CONST0_RTX (compare_mode);
17899 /* A floating-point subtract might overflow, underflow, or produce
17900 an inexact result, thus changing the floating-point flags, so it
17901 can't be generated if we care about that. It's safe if one side
17902 of the construct is zero, since then no subtract will be
17903 generated. */
17904 if (SCALAR_FLOAT_MODE_P (compare_mode)
17905 && flag_trapping_math && ! is_against_zero)
17906 return 0;
17908 /* Eliminate half of the comparisons by switching operands, this
17909 makes the remaining code simpler. */
17910 if (code == UNLT || code == UNGT || code == UNORDERED || code == NE
17911 || code == LTGT || code == LT || code == UNLE)
17913 code = reverse_condition_maybe_unordered (code);
17914 temp = true_cond;
17915 true_cond = false_cond;
17916 false_cond = temp;
17919 /* UNEQ and LTGT take four instructions for a comparison with zero,
17920 it'll probably be faster to use a branch here too. */
17921 if (code == UNEQ && HONOR_NANS (compare_mode))
17922 return 0;
17924 if (GET_CODE (op1) == CONST_DOUBLE)
17925 REAL_VALUE_FROM_CONST_DOUBLE (c1, op1);
17927 /* We're going to try to implement comparisons by performing
17928 a subtract, then comparing against zero. Unfortunately,
17929 Inf - Inf is NaN which is not zero, and so if we don't
17930 know that the operand is finite and the comparison
17931 would treat EQ different to UNORDERED, we can't do it. */
17932 if (HONOR_INFINITIES (compare_mode)
17933 && code != GT && code != UNGE
17934 && (GET_CODE (op1) != CONST_DOUBLE || real_isinf (&c1))
17935 /* Constructs of the form (a OP b ? a : b) are safe. */
17936 && ((! rtx_equal_p (op0, false_cond) && ! rtx_equal_p (op1, false_cond))
17937 || (! rtx_equal_p (op0, true_cond)
17938 && ! rtx_equal_p (op1, true_cond))))
17939 return 0;
17941 /* At this point we know we can use fsel. */
17943 /* Reduce the comparison to a comparison against zero. */
17944 if (! is_against_zero)
17946 temp = gen_reg_rtx (compare_mode);
17947 emit_insn (gen_rtx_SET (VOIDmode, temp,
17948 gen_rtx_MINUS (compare_mode, op0, op1)));
17949 op0 = temp;
17950 op1 = CONST0_RTX (compare_mode);
17953 /* If we don't care about NaNs we can reduce some of the comparisons
17954 down to faster ones. */
17955 if (! HONOR_NANS (compare_mode))
17956 switch (code)
17958 case GT:
17959 code = LE;
17960 temp = true_cond;
17961 true_cond = false_cond;
17962 false_cond = temp;
17963 break;
17964 case UNGE:
17965 code = GE;
17966 break;
17967 case UNEQ:
17968 code = EQ;
17969 break;
17970 default:
17971 break;
17974 /* Now, reduce everything down to a GE. */
17975 switch (code)
17977 case GE:
17978 break;
17980 case LE:
17981 temp = gen_reg_rtx (compare_mode);
17982 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
17983 op0 = temp;
17984 break;
17986 case ORDERED:
17987 temp = gen_reg_rtx (compare_mode);
17988 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_ABS (compare_mode, op0)));
17989 op0 = temp;
17990 break;
17992 case EQ:
17993 temp = gen_reg_rtx (compare_mode);
17994 emit_insn (gen_rtx_SET (VOIDmode, temp,
17995 gen_rtx_NEG (compare_mode,
17996 gen_rtx_ABS (compare_mode, op0))));
17997 op0 = temp;
17998 break;
18000 case UNGE:
18001 /* a UNGE 0 <-> (a GE 0 || -a UNLT 0) */
18002 temp = gen_reg_rtx (result_mode);
18003 emit_insn (gen_rtx_SET (VOIDmode, temp,
18004 gen_rtx_IF_THEN_ELSE (result_mode,
18005 gen_rtx_GE (VOIDmode,
18006 op0, op1),
18007 true_cond, false_cond)));
18008 false_cond = true_cond;
18009 true_cond = temp;
18011 temp = gen_reg_rtx (compare_mode);
18012 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
18013 op0 = temp;
18014 break;
18016 case GT:
18017 /* a GT 0 <-> (a GE 0 && -a UNLT 0) */
18018 temp = gen_reg_rtx (result_mode);
18019 emit_insn (gen_rtx_SET (VOIDmode, temp,
18020 gen_rtx_IF_THEN_ELSE (result_mode,
18021 gen_rtx_GE (VOIDmode,
18022 op0, op1),
18023 true_cond, false_cond)));
18024 true_cond = false_cond;
18025 false_cond = temp;
18027 temp = gen_reg_rtx (compare_mode);
18028 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
18029 op0 = temp;
18030 break;
18032 default:
18033 gcc_unreachable ();
18036 emit_insn (gen_rtx_SET (VOIDmode, dest,
18037 gen_rtx_IF_THEN_ELSE (result_mode,
18038 gen_rtx_GE (VOIDmode,
18039 op0, op1),
18040 true_cond, false_cond)));
18041 return 1;
18044 /* Same as above, but for ints (isel). */
18046 static int
18047 rs6000_emit_int_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
18049 rtx condition_rtx, cr;
18050 enum machine_mode mode = GET_MODE (dest);
18051 enum rtx_code cond_code;
18052 rtx (*isel_func) (rtx, rtx, rtx, rtx, rtx);
18053 bool signedp;
18055 if (mode != SImode && (!TARGET_POWERPC64 || mode != DImode))
18056 return 0;
18058 /* We still have to do the compare, because isel doesn't do a
18059 compare, it just looks at the CRx bits set by a previous compare
18060 instruction. */
18061 condition_rtx = rs6000_generate_compare (op, mode);
18062 cond_code = GET_CODE (condition_rtx);
18063 cr = XEXP (condition_rtx, 0);
18064 signedp = GET_MODE (cr) == CCmode;
18066 isel_func = (mode == SImode
18067 ? (signedp ? gen_isel_signed_si : gen_isel_unsigned_si)
18068 : (signedp ? gen_isel_signed_di : gen_isel_unsigned_di));
18070 switch (cond_code)
18072 case LT: case GT: case LTU: case GTU: case EQ:
18073 /* isel handles these directly. */
18074 break;
18076 default:
18077 /* We need to swap the sense of the comparison. */
18079 rtx t = true_cond;
18080 true_cond = false_cond;
18081 false_cond = t;
18082 PUT_CODE (condition_rtx, reverse_condition (cond_code));
18084 break;
18087 false_cond = force_reg (mode, false_cond);
18088 if (true_cond != const0_rtx)
18089 true_cond = force_reg (mode, true_cond);
18091 emit_insn (isel_func (dest, condition_rtx, true_cond, false_cond, cr));
18093 return 1;
18096 const char *
18097 output_isel (rtx *operands)
18099 enum rtx_code code;
18101 code = GET_CODE (operands[1]);
18103 if (code == GE || code == GEU || code == LE || code == LEU || code == NE)
18105 gcc_assert (GET_CODE (operands[2]) == REG
18106 && GET_CODE (operands[3]) == REG);
18107 PUT_CODE (operands[1], reverse_condition (code));
18108 return "isel %0,%3,%2,%j1";
18111 return "isel %0,%2,%3,%j1";
18114 void
18115 rs6000_emit_minmax (rtx dest, enum rtx_code code, rtx op0, rtx op1)
18117 enum machine_mode mode = GET_MODE (op0);
18118 enum rtx_code c;
18119 rtx target;
18121 /* VSX/altivec have direct min/max insns. */
18122 if ((code == SMAX || code == SMIN)
18123 && (VECTOR_UNIT_ALTIVEC_OR_VSX_P (mode)
18124 || (mode == SFmode && VECTOR_UNIT_VSX_P (DFmode))))
18126 emit_insn (gen_rtx_SET (VOIDmode,
18127 dest,
18128 gen_rtx_fmt_ee (code, mode, op0, op1)));
18129 return;
18132 if (code == SMAX || code == SMIN)
18133 c = GE;
18134 else
18135 c = GEU;
18137 if (code == SMAX || code == UMAX)
18138 target = emit_conditional_move (dest, c, op0, op1, mode,
18139 op0, op1, mode, 0);
18140 else
18141 target = emit_conditional_move (dest, c, op0, op1, mode,
18142 op1, op0, mode, 0);
18143 gcc_assert (target);
18144 if (target != dest)
18145 emit_move_insn (dest, target);
18148 /* A subroutine of the atomic operation splitters. Jump to LABEL if
18149 COND is true. Mark the jump as unlikely to be taken. */
18151 static void
18152 emit_unlikely_jump (rtx cond, rtx label)
18154 rtx very_unlikely = GEN_INT (REG_BR_PROB_BASE / 100 - 1);
18155 rtx x;
18157 x = gen_rtx_IF_THEN_ELSE (VOIDmode, cond, label, pc_rtx);
18158 x = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, x));
18159 add_reg_note (x, REG_BR_PROB, very_unlikely);
18162 /* A subroutine of the atomic operation splitters. Emit a load-locked
18163 instruction in MODE. For QI/HImode, possibly use a pattern than includes
18164 the zero_extend operation. */
18166 static void
18167 emit_load_locked (enum machine_mode mode, rtx reg, rtx mem)
18169 rtx (*fn) (rtx, rtx) = NULL;
18171 switch (mode)
18173 case QImode:
18174 fn = gen_load_lockedqi;
18175 break;
18176 case HImode:
18177 fn = gen_load_lockedhi;
18178 break;
18179 case SImode:
18180 if (GET_MODE (mem) == QImode)
18181 fn = gen_load_lockedqi_si;
18182 else if (GET_MODE (mem) == HImode)
18183 fn = gen_load_lockedhi_si;
18184 else
18185 fn = gen_load_lockedsi;
18186 break;
18187 case DImode:
18188 fn = gen_load_lockeddi;
18189 break;
18190 case TImode:
18191 fn = gen_load_lockedti;
18192 break;
18193 default:
18194 gcc_unreachable ();
18196 emit_insn (fn (reg, mem));
18199 /* A subroutine of the atomic operation splitters. Emit a store-conditional
18200 instruction in MODE. */
18202 static void
18203 emit_store_conditional (enum machine_mode mode, rtx res, rtx mem, rtx val)
18205 rtx (*fn) (rtx, rtx, rtx) = NULL;
18207 switch (mode)
18209 case QImode:
18210 fn = gen_store_conditionalqi;
18211 break;
18212 case HImode:
18213 fn = gen_store_conditionalhi;
18214 break;
18215 case SImode:
18216 fn = gen_store_conditionalsi;
18217 break;
18218 case DImode:
18219 fn = gen_store_conditionaldi;
18220 break;
18221 case TImode:
18222 fn = gen_store_conditionalti;
18223 break;
18224 default:
18225 gcc_unreachable ();
18228 /* Emit sync before stwcx. to address PPC405 Erratum. */
18229 if (PPC405_ERRATUM77)
18230 emit_insn (gen_hwsync ());
18232 emit_insn (fn (res, mem, val));
18235 /* Expand barriers before and after a load_locked/store_cond sequence. */
18237 static rtx
18238 rs6000_pre_atomic_barrier (rtx mem, enum memmodel model)
18240 rtx addr = XEXP (mem, 0);
18241 int strict_p = (reload_in_progress || reload_completed);
18243 if (!legitimate_indirect_address_p (addr, strict_p)
18244 && !legitimate_indexed_address_p (addr, strict_p))
18246 addr = force_reg (Pmode, addr);
18247 mem = replace_equiv_address_nv (mem, addr);
18250 switch (model)
18252 case MEMMODEL_RELAXED:
18253 case MEMMODEL_CONSUME:
18254 case MEMMODEL_ACQUIRE:
18255 break;
18256 case MEMMODEL_RELEASE:
18257 case MEMMODEL_ACQ_REL:
18258 emit_insn (gen_lwsync ());
18259 break;
18260 case MEMMODEL_SEQ_CST:
18261 emit_insn (gen_hwsync ());
18262 break;
18263 default:
18264 gcc_unreachable ();
18266 return mem;
18269 static void
18270 rs6000_post_atomic_barrier (enum memmodel model)
18272 switch (model)
18274 case MEMMODEL_RELAXED:
18275 case MEMMODEL_CONSUME:
18276 case MEMMODEL_RELEASE:
18277 break;
18278 case MEMMODEL_ACQUIRE:
18279 case MEMMODEL_ACQ_REL:
18280 case MEMMODEL_SEQ_CST:
18281 emit_insn (gen_isync ());
18282 break;
18283 default:
18284 gcc_unreachable ();
18288 /* A subroutine of the various atomic expanders. For sub-word operations,
18289 we must adjust things to operate on SImode. Given the original MEM,
18290 return a new aligned memory. Also build and return the quantities by
18291 which to shift and mask. */
18293 static rtx
18294 rs6000_adjust_atomic_subword (rtx orig_mem, rtx *pshift, rtx *pmask)
18296 rtx addr, align, shift, mask, mem;
18297 HOST_WIDE_INT shift_mask;
18298 enum machine_mode mode = GET_MODE (orig_mem);
18300 /* For smaller modes, we have to implement this via SImode. */
18301 shift_mask = (mode == QImode ? 0x18 : 0x10);
18303 addr = XEXP (orig_mem, 0);
18304 addr = force_reg (GET_MODE (addr), addr);
18306 /* Aligned memory containing subword. Generate a new memory. We
18307 do not want any of the existing MEM_ATTR data, as we're now
18308 accessing memory outside the original object. */
18309 align = expand_simple_binop (Pmode, AND, addr, GEN_INT (-4),
18310 NULL_RTX, 1, OPTAB_LIB_WIDEN);
18311 mem = gen_rtx_MEM (SImode, align);
18312 MEM_VOLATILE_P (mem) = MEM_VOLATILE_P (orig_mem);
18313 if (MEM_ALIAS_SET (orig_mem) == ALIAS_SET_MEMORY_BARRIER)
18314 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
18316 /* Shift amount for subword relative to aligned word. */
18317 shift = gen_reg_rtx (SImode);
18318 addr = gen_lowpart (SImode, addr);
18319 emit_insn (gen_rlwinm (shift, addr, GEN_INT (3), GEN_INT (shift_mask)));
18320 if (WORDS_BIG_ENDIAN)
18321 shift = expand_simple_binop (SImode, XOR, shift, GEN_INT (shift_mask),
18322 shift, 1, OPTAB_LIB_WIDEN);
18323 *pshift = shift;
18325 /* Mask for insertion. */
18326 mask = expand_simple_binop (SImode, ASHIFT, GEN_INT (GET_MODE_MASK (mode)),
18327 shift, NULL_RTX, 1, OPTAB_LIB_WIDEN);
18328 *pmask = mask;
18330 return mem;
18333 /* A subroutine of the various atomic expanders. For sub-word operands,
18334 combine OLDVAL and NEWVAL via MASK. Returns a new pseduo. */
18336 static rtx
18337 rs6000_mask_atomic_subword (rtx oldval, rtx newval, rtx mask)
18339 rtx x;
18341 x = gen_reg_rtx (SImode);
18342 emit_insn (gen_rtx_SET (VOIDmode, x,
18343 gen_rtx_AND (SImode,
18344 gen_rtx_NOT (SImode, mask),
18345 oldval)));
18347 x = expand_simple_binop (SImode, IOR, newval, x, x, 1, OPTAB_LIB_WIDEN);
18349 return x;
18352 /* A subroutine of the various atomic expanders. For sub-word operands,
18353 extract WIDE to NARROW via SHIFT. */
18355 static void
18356 rs6000_finish_atomic_subword (rtx narrow, rtx wide, rtx shift)
18358 wide = expand_simple_binop (SImode, LSHIFTRT, wide, shift,
18359 wide, 1, OPTAB_LIB_WIDEN);
18360 emit_move_insn (narrow, gen_lowpart (GET_MODE (narrow), wide));
18363 /* Expand an atomic compare and swap operation. */
18365 void
18366 rs6000_expand_atomic_compare_and_swap (rtx operands[])
18368 rtx boolval, retval, mem, oldval, newval, cond;
18369 rtx label1, label2, x, mask, shift;
18370 enum machine_mode mode, orig_mode;
18371 enum memmodel mod_s, mod_f;
18372 bool is_weak;
18374 boolval = operands[0];
18375 retval = operands[1];
18376 mem = operands[2];
18377 oldval = operands[3];
18378 newval = operands[4];
18379 is_weak = (INTVAL (operands[5]) != 0);
18380 mod_s = (enum memmodel) INTVAL (operands[6]);
18381 mod_f = (enum memmodel) INTVAL (operands[7]);
18382 orig_mode = mode = GET_MODE (mem);
18384 mask = shift = NULL_RTX;
18385 if (mode == QImode || mode == HImode)
18387 /* Before power8, we didn't have access to lbarx/lharx, so generate a
18388 lwarx and shift/mask operations. With power8, we need to do the
18389 comparison in SImode, but the store is still done in QI/HImode. */
18390 oldval = convert_modes (SImode, mode, oldval, 1);
18392 if (!TARGET_SYNC_HI_QI)
18394 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
18396 /* Shift and mask OLDVAL into position with the word. */
18397 oldval = expand_simple_binop (SImode, ASHIFT, oldval, shift,
18398 NULL_RTX, 1, OPTAB_LIB_WIDEN);
18400 /* Shift and mask NEWVAL into position within the word. */
18401 newval = convert_modes (SImode, mode, newval, 1);
18402 newval = expand_simple_binop (SImode, ASHIFT, newval, shift,
18403 NULL_RTX, 1, OPTAB_LIB_WIDEN);
18406 /* Prepare to adjust the return value. */
18407 retval = gen_reg_rtx (SImode);
18408 mode = SImode;
18410 else if (reg_overlap_mentioned_p (retval, oldval))
18411 oldval = copy_to_reg (oldval);
18413 mem = rs6000_pre_atomic_barrier (mem, mod_s);
18415 label1 = NULL_RTX;
18416 if (!is_weak)
18418 label1 = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
18419 emit_label (XEXP (label1, 0));
18421 label2 = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
18423 emit_load_locked (mode, retval, mem);
18425 x = retval;
18426 if (mask)
18428 x = expand_simple_binop (SImode, AND, retval, mask,
18429 NULL_RTX, 1, OPTAB_LIB_WIDEN);
18432 cond = gen_reg_rtx (CCmode);
18433 /* If we have TImode, synthesize a comparison. */
18434 if (mode != TImode)
18435 x = gen_rtx_COMPARE (CCmode, x, oldval);
18436 else
18438 rtx xor1_result = gen_reg_rtx (DImode);
18439 rtx xor2_result = gen_reg_rtx (DImode);
18440 rtx or_result = gen_reg_rtx (DImode);
18441 rtx new_word0 = simplify_gen_subreg (DImode, x, TImode, 0);
18442 rtx new_word1 = simplify_gen_subreg (DImode, x, TImode, 8);
18443 rtx old_word0 = simplify_gen_subreg (DImode, oldval, TImode, 0);
18444 rtx old_word1 = simplify_gen_subreg (DImode, oldval, TImode, 8);
18446 emit_insn (gen_xordi3 (xor1_result, new_word0, old_word0));
18447 emit_insn (gen_xordi3 (xor2_result, new_word1, old_word1));
18448 emit_insn (gen_iordi3 (or_result, xor1_result, xor2_result));
18449 x = gen_rtx_COMPARE (CCmode, or_result, const0_rtx);
18452 emit_insn (gen_rtx_SET (VOIDmode, cond, x));
18454 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
18455 emit_unlikely_jump (x, label2);
18457 x = newval;
18458 if (mask)
18459 x = rs6000_mask_atomic_subword (retval, newval, mask);
18461 emit_store_conditional (orig_mode, cond, mem, x);
18463 if (!is_weak)
18465 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
18466 emit_unlikely_jump (x, label1);
18469 if (mod_f != MEMMODEL_RELAXED)
18470 emit_label (XEXP (label2, 0));
18472 rs6000_post_atomic_barrier (mod_s);
18474 if (mod_f == MEMMODEL_RELAXED)
18475 emit_label (XEXP (label2, 0));
18477 if (shift)
18478 rs6000_finish_atomic_subword (operands[1], retval, shift);
18479 else if (mode != GET_MODE (operands[1]))
18480 convert_move (operands[1], retval, 1);
18482 /* In all cases, CR0 contains EQ on success, and NE on failure. */
18483 x = gen_rtx_EQ (SImode, cond, const0_rtx);
18484 emit_insn (gen_rtx_SET (VOIDmode, boolval, x));
18487 /* Expand an atomic exchange operation. */
18489 void
18490 rs6000_expand_atomic_exchange (rtx operands[])
18492 rtx retval, mem, val, cond;
18493 enum machine_mode mode;
18494 enum memmodel model;
18495 rtx label, x, mask, shift;
18497 retval = operands[0];
18498 mem = operands[1];
18499 val = operands[2];
18500 model = (enum memmodel) INTVAL (operands[3]);
18501 mode = GET_MODE (mem);
18503 mask = shift = NULL_RTX;
18504 if (!TARGET_SYNC_HI_QI && (mode == QImode || mode == HImode))
18506 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
18508 /* Shift and mask VAL into position with the word. */
18509 val = convert_modes (SImode, mode, val, 1);
18510 val = expand_simple_binop (SImode, ASHIFT, val, shift,
18511 NULL_RTX, 1, OPTAB_LIB_WIDEN);
18513 /* Prepare to adjust the return value. */
18514 retval = gen_reg_rtx (SImode);
18515 mode = SImode;
18518 mem = rs6000_pre_atomic_barrier (mem, model);
18520 label = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
18521 emit_label (XEXP (label, 0));
18523 emit_load_locked (mode, retval, mem);
18525 x = val;
18526 if (mask)
18527 x = rs6000_mask_atomic_subword (retval, val, mask);
18529 cond = gen_reg_rtx (CCmode);
18530 emit_store_conditional (mode, cond, mem, x);
18532 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
18533 emit_unlikely_jump (x, label);
18535 rs6000_post_atomic_barrier (model);
18537 if (shift)
18538 rs6000_finish_atomic_subword (operands[0], retval, shift);
18541 /* Expand an atomic fetch-and-operate pattern. CODE is the binary operation
18542 to perform. MEM is the memory on which to operate. VAL is the second
18543 operand of the binary operator. BEFORE and AFTER are optional locations to
18544 return the value of MEM either before of after the operation. MODEL_RTX
18545 is a CONST_INT containing the memory model to use. */
18547 void
18548 rs6000_expand_atomic_op (enum rtx_code code, rtx mem, rtx val,
18549 rtx orig_before, rtx orig_after, rtx model_rtx)
18551 enum memmodel model = (enum memmodel) INTVAL (model_rtx);
18552 enum machine_mode mode = GET_MODE (mem);
18553 enum machine_mode store_mode = mode;
18554 rtx label, x, cond, mask, shift;
18555 rtx before = orig_before, after = orig_after;
18557 mask = shift = NULL_RTX;
18558 /* On power8, we want to use SImode for the operation. On previous systems,
18559 use the operation in a subword and shift/mask to get the proper byte or
18560 halfword. */
18561 if (mode == QImode || mode == HImode)
18563 if (TARGET_SYNC_HI_QI)
18565 val = convert_modes (SImode, mode, val, 1);
18567 /* Prepare to adjust the return value. */
18568 before = gen_reg_rtx (SImode);
18569 if (after)
18570 after = gen_reg_rtx (SImode);
18571 mode = SImode;
18573 else
18575 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
18577 /* Shift and mask VAL into position with the word. */
18578 val = convert_modes (SImode, mode, val, 1);
18579 val = expand_simple_binop (SImode, ASHIFT, val, shift,
18580 NULL_RTX, 1, OPTAB_LIB_WIDEN);
18582 switch (code)
18584 case IOR:
18585 case XOR:
18586 /* We've already zero-extended VAL. That is sufficient to
18587 make certain that it does not affect other bits. */
18588 mask = NULL;
18589 break;
18591 case AND:
18592 /* If we make certain that all of the other bits in VAL are
18593 set, that will be sufficient to not affect other bits. */
18594 x = gen_rtx_NOT (SImode, mask);
18595 x = gen_rtx_IOR (SImode, x, val);
18596 emit_insn (gen_rtx_SET (VOIDmode, val, x));
18597 mask = NULL;
18598 break;
18600 case NOT:
18601 case PLUS:
18602 case MINUS:
18603 /* These will all affect bits outside the field and need
18604 adjustment via MASK within the loop. */
18605 break;
18607 default:
18608 gcc_unreachable ();
18611 /* Prepare to adjust the return value. */
18612 before = gen_reg_rtx (SImode);
18613 if (after)
18614 after = gen_reg_rtx (SImode);
18615 store_mode = mode = SImode;
18619 mem = rs6000_pre_atomic_barrier (mem, model);
18621 label = gen_label_rtx ();
18622 emit_label (label);
18623 label = gen_rtx_LABEL_REF (VOIDmode, label);
18625 if (before == NULL_RTX)
18626 before = gen_reg_rtx (mode);
18628 emit_load_locked (mode, before, mem);
18630 if (code == NOT)
18632 x = expand_simple_binop (mode, AND, before, val,
18633 NULL_RTX, 1, OPTAB_LIB_WIDEN);
18634 after = expand_simple_unop (mode, NOT, x, after, 1);
18636 else
18638 after = expand_simple_binop (mode, code, before, val,
18639 after, 1, OPTAB_LIB_WIDEN);
18642 x = after;
18643 if (mask)
18645 x = expand_simple_binop (SImode, AND, after, mask,
18646 NULL_RTX, 1, OPTAB_LIB_WIDEN);
18647 x = rs6000_mask_atomic_subword (before, x, mask);
18649 else if (store_mode != mode)
18650 x = convert_modes (store_mode, mode, x, 1);
18652 cond = gen_reg_rtx (CCmode);
18653 emit_store_conditional (store_mode, cond, mem, x);
18655 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
18656 emit_unlikely_jump (x, label);
18658 rs6000_post_atomic_barrier (model);
18660 if (shift)
18662 /* QImode/HImode on machines without lbarx/lharx where we do a lwarx and
18663 then do the calcuations in a SImode register. */
18664 if (orig_before)
18665 rs6000_finish_atomic_subword (orig_before, before, shift);
18666 if (orig_after)
18667 rs6000_finish_atomic_subword (orig_after, after, shift);
18669 else if (store_mode != mode)
18671 /* QImode/HImode on machines with lbarx/lharx where we do the native
18672 operation and then do the calcuations in a SImode register. */
18673 if (orig_before)
18674 convert_move (orig_before, before, 1);
18675 if (orig_after)
18676 convert_move (orig_after, after, 1);
18678 else if (orig_after && after != orig_after)
18679 emit_move_insn (orig_after, after);
18682 /* Emit instructions to move SRC to DST. Called by splitters for
18683 multi-register moves. It will emit at most one instruction for
18684 each register that is accessed; that is, it won't emit li/lis pairs
18685 (or equivalent for 64-bit code). One of SRC or DST must be a hard
18686 register. */
18688 void
18689 rs6000_split_multireg_move (rtx dst, rtx src)
18691 /* The register number of the first register being moved. */
18692 int reg;
18693 /* The mode that is to be moved. */
18694 enum machine_mode mode;
18695 /* The mode that the move is being done in, and its size. */
18696 enum machine_mode reg_mode;
18697 int reg_mode_size;
18698 /* The number of registers that will be moved. */
18699 int nregs;
18701 reg = REG_P (dst) ? REGNO (dst) : REGNO (src);
18702 mode = GET_MODE (dst);
18703 nregs = hard_regno_nregs[reg][mode];
18704 if (FP_REGNO_P (reg))
18705 reg_mode = DECIMAL_FLOAT_MODE_P (mode) ? DDmode :
18706 ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? DFmode : SFmode);
18707 else if (ALTIVEC_REGNO_P (reg))
18708 reg_mode = V16QImode;
18709 else if (TARGET_E500_DOUBLE && mode == TFmode)
18710 reg_mode = DFmode;
18711 else
18712 reg_mode = word_mode;
18713 reg_mode_size = GET_MODE_SIZE (reg_mode);
18715 gcc_assert (reg_mode_size * nregs == GET_MODE_SIZE (mode));
18717 if (REG_P (src) && REG_P (dst) && (REGNO (src) < REGNO (dst)))
18719 /* Move register range backwards, if we might have destructive
18720 overlap. */
18721 int i;
18722 for (i = nregs - 1; i >= 0; i--)
18723 emit_insn (gen_rtx_SET (VOIDmode,
18724 simplify_gen_subreg (reg_mode, dst, mode,
18725 i * reg_mode_size),
18726 simplify_gen_subreg (reg_mode, src, mode,
18727 i * reg_mode_size)));
18729 else
18731 int i;
18732 int j = -1;
18733 bool used_update = false;
18734 rtx restore_basereg = NULL_RTX;
18736 if (MEM_P (src) && INT_REGNO_P (reg))
18738 rtx breg;
18740 if (GET_CODE (XEXP (src, 0)) == PRE_INC
18741 || GET_CODE (XEXP (src, 0)) == PRE_DEC)
18743 rtx delta_rtx;
18744 breg = XEXP (XEXP (src, 0), 0);
18745 delta_rtx = (GET_CODE (XEXP (src, 0)) == PRE_INC
18746 ? GEN_INT (GET_MODE_SIZE (GET_MODE (src)))
18747 : GEN_INT (-GET_MODE_SIZE (GET_MODE (src))));
18748 emit_insn (gen_add3_insn (breg, breg, delta_rtx));
18749 src = replace_equiv_address (src, breg);
18751 else if (! rs6000_offsettable_memref_p (src, reg_mode))
18753 if (GET_CODE (XEXP (src, 0)) == PRE_MODIFY)
18755 rtx basereg = XEXP (XEXP (src, 0), 0);
18756 if (TARGET_UPDATE)
18758 rtx ndst = simplify_gen_subreg (reg_mode, dst, mode, 0);
18759 emit_insn (gen_rtx_SET (VOIDmode, ndst,
18760 gen_rtx_MEM (reg_mode, XEXP (src, 0))));
18761 used_update = true;
18763 else
18764 emit_insn (gen_rtx_SET (VOIDmode, basereg,
18765 XEXP (XEXP (src, 0), 1)));
18766 src = replace_equiv_address (src, basereg);
18768 else
18770 rtx basereg = gen_rtx_REG (Pmode, reg);
18771 emit_insn (gen_rtx_SET (VOIDmode, basereg, XEXP (src, 0)));
18772 src = replace_equiv_address (src, basereg);
18776 breg = XEXP (src, 0);
18777 if (GET_CODE (breg) == PLUS || GET_CODE (breg) == LO_SUM)
18778 breg = XEXP (breg, 0);
18780 /* If the base register we are using to address memory is
18781 also a destination reg, then change that register last. */
18782 if (REG_P (breg)
18783 && REGNO (breg) >= REGNO (dst)
18784 && REGNO (breg) < REGNO (dst) + nregs)
18785 j = REGNO (breg) - REGNO (dst);
18787 else if (MEM_P (dst) && INT_REGNO_P (reg))
18789 rtx breg;
18791 if (GET_CODE (XEXP (dst, 0)) == PRE_INC
18792 || GET_CODE (XEXP (dst, 0)) == PRE_DEC)
18794 rtx delta_rtx;
18795 breg = XEXP (XEXP (dst, 0), 0);
18796 delta_rtx = (GET_CODE (XEXP (dst, 0)) == PRE_INC
18797 ? GEN_INT (GET_MODE_SIZE (GET_MODE (dst)))
18798 : GEN_INT (-GET_MODE_SIZE (GET_MODE (dst))));
18800 /* We have to update the breg before doing the store.
18801 Use store with update, if available. */
18803 if (TARGET_UPDATE)
18805 rtx nsrc = simplify_gen_subreg (reg_mode, src, mode, 0);
18806 emit_insn (TARGET_32BIT
18807 ? (TARGET_POWERPC64
18808 ? gen_movdi_si_update (breg, breg, delta_rtx, nsrc)
18809 : gen_movsi_update (breg, breg, delta_rtx, nsrc))
18810 : gen_movdi_di_update (breg, breg, delta_rtx, nsrc));
18811 used_update = true;
18813 else
18814 emit_insn (gen_add3_insn (breg, breg, delta_rtx));
18815 dst = replace_equiv_address (dst, breg);
18817 else if (!rs6000_offsettable_memref_p (dst, reg_mode)
18818 && GET_CODE (XEXP (dst, 0)) != LO_SUM)
18820 if (GET_CODE (XEXP (dst, 0)) == PRE_MODIFY)
18822 rtx basereg = XEXP (XEXP (dst, 0), 0);
18823 if (TARGET_UPDATE)
18825 rtx nsrc = simplify_gen_subreg (reg_mode, src, mode, 0);
18826 emit_insn (gen_rtx_SET (VOIDmode,
18827 gen_rtx_MEM (reg_mode, XEXP (dst, 0)), nsrc));
18828 used_update = true;
18830 else
18831 emit_insn (gen_rtx_SET (VOIDmode, basereg,
18832 XEXP (XEXP (dst, 0), 1)));
18833 dst = replace_equiv_address (dst, basereg);
18835 else
18837 rtx basereg = XEXP (XEXP (dst, 0), 0);
18838 rtx offsetreg = XEXP (XEXP (dst, 0), 1);
18839 gcc_assert (GET_CODE (XEXP (dst, 0)) == PLUS
18840 && REG_P (basereg)
18841 && REG_P (offsetreg)
18842 && REGNO (basereg) != REGNO (offsetreg));
18843 if (REGNO (basereg) == 0)
18845 rtx tmp = offsetreg;
18846 offsetreg = basereg;
18847 basereg = tmp;
18849 emit_insn (gen_add3_insn (basereg, basereg, offsetreg));
18850 restore_basereg = gen_sub3_insn (basereg, basereg, offsetreg);
18851 dst = replace_equiv_address (dst, basereg);
18854 else if (GET_CODE (XEXP (dst, 0)) != LO_SUM)
18855 gcc_assert (rs6000_offsettable_memref_p (dst, reg_mode));
18858 for (i = 0; i < nregs; i++)
18860 /* Calculate index to next subword. */
18861 ++j;
18862 if (j == nregs)
18863 j = 0;
18865 /* If compiler already emitted move of first word by
18866 store with update, no need to do anything. */
18867 if (j == 0 && used_update)
18868 continue;
18870 emit_insn (gen_rtx_SET (VOIDmode,
18871 simplify_gen_subreg (reg_mode, dst, mode,
18872 j * reg_mode_size),
18873 simplify_gen_subreg (reg_mode, src, mode,
18874 j * reg_mode_size)));
18876 if (restore_basereg != NULL_RTX)
18877 emit_insn (restore_basereg);
18882 /* This page contains routines that are used to determine what the
18883 function prologue and epilogue code will do and write them out. */
18885 static inline bool
18886 save_reg_p (int r)
18888 return !call_used_regs[r] && df_regs_ever_live_p (r);
18891 /* Return the first fixed-point register that is required to be
18892 saved. 32 if none. */
18895 first_reg_to_save (void)
18897 int first_reg;
18899 /* Find lowest numbered live register. */
18900 for (first_reg = 13; first_reg <= 31; first_reg++)
18901 if (save_reg_p (first_reg))
18902 break;
18904 if (first_reg > RS6000_PIC_OFFSET_TABLE_REGNUM
18905 && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
18906 || (DEFAULT_ABI == ABI_DARWIN && flag_pic)
18907 || (TARGET_TOC && TARGET_MINIMAL_TOC))
18908 && df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))
18909 first_reg = RS6000_PIC_OFFSET_TABLE_REGNUM;
18911 #if TARGET_MACHO
18912 if (flag_pic
18913 && crtl->uses_pic_offset_table
18914 && first_reg > RS6000_PIC_OFFSET_TABLE_REGNUM)
18915 return RS6000_PIC_OFFSET_TABLE_REGNUM;
18916 #endif
18918 return first_reg;
18921 /* Similar, for FP regs. */
18924 first_fp_reg_to_save (void)
18926 int first_reg;
18928 /* Find lowest numbered live register. */
18929 for (first_reg = 14 + 32; first_reg <= 63; first_reg++)
18930 if (save_reg_p (first_reg))
18931 break;
18933 return first_reg;
18936 /* Similar, for AltiVec regs. */
18938 static int
18939 first_altivec_reg_to_save (void)
18941 int i;
18943 /* Stack frame remains as is unless we are in AltiVec ABI. */
18944 if (! TARGET_ALTIVEC_ABI)
18945 return LAST_ALTIVEC_REGNO + 1;
18947 /* On Darwin, the unwind routines are compiled without
18948 TARGET_ALTIVEC, and use save_world to save/restore the
18949 altivec registers when necessary. */
18950 if (DEFAULT_ABI == ABI_DARWIN && crtl->calls_eh_return
18951 && ! TARGET_ALTIVEC)
18952 return FIRST_ALTIVEC_REGNO + 20;
18954 /* Find lowest numbered live register. */
18955 for (i = FIRST_ALTIVEC_REGNO + 20; i <= LAST_ALTIVEC_REGNO; ++i)
18956 if (save_reg_p (i))
18957 break;
18959 return i;
18962 /* Return a 32-bit mask of the AltiVec registers we need to set in
18963 VRSAVE. Bit n of the return value is 1 if Vn is live. The MSB in
18964 the 32-bit word is 0. */
18966 static unsigned int
18967 compute_vrsave_mask (void)
18969 unsigned int i, mask = 0;
18971 /* On Darwin, the unwind routines are compiled without
18972 TARGET_ALTIVEC, and use save_world to save/restore the
18973 call-saved altivec registers when necessary. */
18974 if (DEFAULT_ABI == ABI_DARWIN && crtl->calls_eh_return
18975 && ! TARGET_ALTIVEC)
18976 mask |= 0xFFF;
18978 /* First, find out if we use _any_ altivec registers. */
18979 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
18980 if (df_regs_ever_live_p (i))
18981 mask |= ALTIVEC_REG_BIT (i);
18983 if (mask == 0)
18984 return mask;
18986 /* Next, remove the argument registers from the set. These must
18987 be in the VRSAVE mask set by the caller, so we don't need to add
18988 them in again. More importantly, the mask we compute here is
18989 used to generate CLOBBERs in the set_vrsave insn, and we do not
18990 wish the argument registers to die. */
18991 for (i = crtl->args.info.vregno - 1; i >= ALTIVEC_ARG_MIN_REG; --i)
18992 mask &= ~ALTIVEC_REG_BIT (i);
18994 /* Similarly, remove the return value from the set. */
18996 bool yes = false;
18997 diddle_return_value (is_altivec_return_reg, &yes);
18998 if (yes)
18999 mask &= ~ALTIVEC_REG_BIT (ALTIVEC_ARG_RETURN);
19002 return mask;
19005 /* For a very restricted set of circumstances, we can cut down the
19006 size of prologues/epilogues by calling our own save/restore-the-world
19007 routines. */
19009 static void
19010 compute_save_world_info (rs6000_stack_t *info_ptr)
19012 info_ptr->world_save_p = 1;
19013 info_ptr->world_save_p
19014 = (WORLD_SAVE_P (info_ptr)
19015 && DEFAULT_ABI == ABI_DARWIN
19016 && !cfun->has_nonlocal_label
19017 && info_ptr->first_fp_reg_save == FIRST_SAVED_FP_REGNO
19018 && info_ptr->first_gp_reg_save == FIRST_SAVED_GP_REGNO
19019 && info_ptr->first_altivec_reg_save == FIRST_SAVED_ALTIVEC_REGNO
19020 && info_ptr->cr_save_p);
19022 /* This will not work in conjunction with sibcalls. Make sure there
19023 are none. (This check is expensive, but seldom executed.) */
19024 if (WORLD_SAVE_P (info_ptr))
19026 rtx insn;
19027 for (insn = get_last_insn_anywhere (); insn; insn = PREV_INSN (insn))
19028 if (CALL_P (insn) && SIBLING_CALL_P (insn))
19030 info_ptr->world_save_p = 0;
19031 break;
19035 if (WORLD_SAVE_P (info_ptr))
19037 /* Even if we're not touching VRsave, make sure there's room on the
19038 stack for it, if it looks like we're calling SAVE_WORLD, which
19039 will attempt to save it. */
19040 info_ptr->vrsave_size = 4;
19042 /* If we are going to save the world, we need to save the link register too. */
19043 info_ptr->lr_save_p = 1;
19045 /* "Save" the VRsave register too if we're saving the world. */
19046 if (info_ptr->vrsave_mask == 0)
19047 info_ptr->vrsave_mask = compute_vrsave_mask ();
19049 /* Because the Darwin register save/restore routines only handle
19050 F14 .. F31 and V20 .. V31 as per the ABI, perform a consistency
19051 check. */
19052 gcc_assert (info_ptr->first_fp_reg_save >= FIRST_SAVED_FP_REGNO
19053 && (info_ptr->first_altivec_reg_save
19054 >= FIRST_SAVED_ALTIVEC_REGNO));
19056 return;
19060 static void
19061 is_altivec_return_reg (rtx reg, void *xyes)
19063 bool *yes = (bool *) xyes;
19064 if (REGNO (reg) == ALTIVEC_ARG_RETURN)
19065 *yes = true;
19069 /* Look for user-defined global regs in the range FIRST to LAST-1.
19070 We should not restore these, and so cannot use lmw or out-of-line
19071 restore functions if there are any. We also can't save them
19072 (well, emit frame notes for them), because frame unwinding during
19073 exception handling will restore saved registers. */
19075 static bool
19076 global_regs_p (unsigned first, unsigned last)
19078 while (first < last)
19079 if (global_regs[first++])
19080 return true;
19081 return false;
19084 /* Determine the strategy for savings/restoring registers. */
19086 enum {
19087 SAVRES_MULTIPLE = 0x1,
19088 SAVE_INLINE_FPRS = 0x2,
19089 SAVE_INLINE_GPRS = 0x4,
19090 REST_INLINE_FPRS = 0x8,
19091 REST_INLINE_GPRS = 0x10,
19092 SAVE_NOINLINE_GPRS_SAVES_LR = 0x20,
19093 SAVE_NOINLINE_FPRS_SAVES_LR = 0x40,
19094 REST_NOINLINE_FPRS_DOESNT_RESTORE_LR = 0x80,
19095 SAVE_INLINE_VRS = 0x100,
19096 REST_INLINE_VRS = 0x200
19099 static int
19100 rs6000_savres_strategy (rs6000_stack_t *info,
19101 bool using_static_chain_p)
19103 int strategy = 0;
19104 bool lr_save_p;
19106 if (TARGET_MULTIPLE
19107 && !TARGET_POWERPC64
19108 && !(TARGET_SPE_ABI && info->spe_64bit_regs_used)
19109 && info->first_gp_reg_save < 31
19110 && !global_regs_p (info->first_gp_reg_save, 32))
19111 strategy |= SAVRES_MULTIPLE;
19113 if (crtl->calls_eh_return
19114 || cfun->machine->ra_need_lr)
19115 strategy |= (SAVE_INLINE_FPRS | REST_INLINE_FPRS
19116 | SAVE_INLINE_GPRS | REST_INLINE_GPRS
19117 | SAVE_INLINE_VRS | REST_INLINE_VRS);
19119 if (info->first_fp_reg_save == 64
19120 /* The out-of-line FP routines use double-precision stores;
19121 we can't use those routines if we don't have such stores. */
19122 || (TARGET_HARD_FLOAT && !TARGET_DOUBLE_FLOAT)
19123 || global_regs_p (info->first_fp_reg_save, 64))
19124 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
19126 if (info->first_gp_reg_save == 32
19127 || (!(strategy & SAVRES_MULTIPLE)
19128 && global_regs_p (info->first_gp_reg_save, 32)))
19129 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
19131 if (info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1
19132 || global_regs_p (info->first_altivec_reg_save, LAST_ALTIVEC_REGNO + 1))
19133 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
19135 /* Define cutoff for using out-of-line functions to save registers. */
19136 if (DEFAULT_ABI == ABI_V4 || TARGET_ELF)
19138 if (!optimize_size)
19140 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
19141 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
19142 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
19144 else
19146 /* Prefer out-of-line restore if it will exit. */
19147 if (info->first_fp_reg_save > 61)
19148 strategy |= SAVE_INLINE_FPRS;
19149 if (info->first_gp_reg_save > 29)
19151 if (info->first_fp_reg_save == 64)
19152 strategy |= SAVE_INLINE_GPRS;
19153 else
19154 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
19156 if (info->first_altivec_reg_save == LAST_ALTIVEC_REGNO)
19157 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
19160 else if (DEFAULT_ABI == ABI_DARWIN)
19162 if (info->first_fp_reg_save > 60)
19163 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
19164 if (info->first_gp_reg_save > 29)
19165 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
19166 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
19168 else
19170 gcc_checking_assert (DEFAULT_ABI == ABI_AIX);
19171 if (info->first_fp_reg_save > 61)
19172 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
19173 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
19174 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
19177 /* Don't bother to try to save things out-of-line if r11 is occupied
19178 by the static chain. It would require too much fiddling and the
19179 static chain is rarely used anyway. FPRs are saved w.r.t the stack
19180 pointer on Darwin, and AIX uses r1 or r12. */
19181 if (using_static_chain_p && DEFAULT_ABI != ABI_AIX)
19182 strategy |= ((DEFAULT_ABI == ABI_DARWIN ? 0 : SAVE_INLINE_FPRS)
19183 | SAVE_INLINE_GPRS
19184 | SAVE_INLINE_VRS | REST_INLINE_VRS);
19186 /* We can only use the out-of-line routines to restore if we've
19187 saved all the registers from first_fp_reg_save in the prologue.
19188 Otherwise, we risk loading garbage. */
19189 if ((strategy & (SAVE_INLINE_FPRS | REST_INLINE_FPRS)) == SAVE_INLINE_FPRS)
19191 int i;
19193 for (i = info->first_fp_reg_save; i < 64; i++)
19194 if (!save_reg_p (i))
19196 strategy |= REST_INLINE_FPRS;
19197 break;
19201 /* If we are going to use store multiple, then don't even bother
19202 with the out-of-line routines, since the store-multiple
19203 instruction will always be smaller. */
19204 if ((strategy & SAVRES_MULTIPLE))
19205 strategy |= SAVE_INLINE_GPRS;
19207 /* info->lr_save_p isn't yet set if the only reason lr needs to be
19208 saved is an out-of-line save or restore. Set up the value for
19209 the next test (excluding out-of-line gpr restore). */
19210 lr_save_p = (info->lr_save_p
19211 || !(strategy & SAVE_INLINE_GPRS)
19212 || !(strategy & SAVE_INLINE_FPRS)
19213 || !(strategy & SAVE_INLINE_VRS)
19214 || !(strategy & REST_INLINE_FPRS)
19215 || !(strategy & REST_INLINE_VRS));
19217 /* The situation is more complicated with load multiple. We'd
19218 prefer to use the out-of-line routines for restores, since the
19219 "exit" out-of-line routines can handle the restore of LR and the
19220 frame teardown. However if doesn't make sense to use the
19221 out-of-line routine if that is the only reason we'd need to save
19222 LR, and we can't use the "exit" out-of-line gpr restore if we
19223 have saved some fprs; In those cases it is advantageous to use
19224 load multiple when available. */
19225 if ((strategy & SAVRES_MULTIPLE)
19226 && (!lr_save_p
19227 || info->first_fp_reg_save != 64))
19228 strategy |= REST_INLINE_GPRS;
19230 /* Saving CR interferes with the exit routines used on the SPE, so
19231 just punt here. */
19232 if (TARGET_SPE_ABI
19233 && info->spe_64bit_regs_used
19234 && info->cr_save_p)
19235 strategy |= REST_INLINE_GPRS;
19237 /* We can only use load multiple or the out-of-line routines to
19238 restore if we've used store multiple or out-of-line routines
19239 in the prologue, i.e. if we've saved all the registers from
19240 first_gp_reg_save. Otherwise, we risk loading garbage. */
19241 if ((strategy & (SAVE_INLINE_GPRS | REST_INLINE_GPRS | SAVRES_MULTIPLE))
19242 == SAVE_INLINE_GPRS)
19244 int i;
19246 for (i = info->first_gp_reg_save; i < 32; i++)
19247 if (!save_reg_p (i))
19249 strategy |= REST_INLINE_GPRS;
19250 break;
19254 if (TARGET_ELF && TARGET_64BIT)
19256 if (!(strategy & SAVE_INLINE_FPRS))
19257 strategy |= SAVE_NOINLINE_FPRS_SAVES_LR;
19258 else if (!(strategy & SAVE_INLINE_GPRS)
19259 && info->first_fp_reg_save == 64)
19260 strategy |= SAVE_NOINLINE_GPRS_SAVES_LR;
19262 else if (TARGET_AIX && !(strategy & REST_INLINE_FPRS))
19263 strategy |= REST_NOINLINE_FPRS_DOESNT_RESTORE_LR;
19265 if (TARGET_MACHO && !(strategy & SAVE_INLINE_FPRS))
19266 strategy |= SAVE_NOINLINE_FPRS_SAVES_LR;
19268 return strategy;
19271 /* Calculate the stack information for the current function. This is
19272 complicated by having two separate calling sequences, the AIX calling
19273 sequence and the V.4 calling sequence.
19275 AIX (and Darwin/Mac OS X) stack frames look like:
19276 32-bit 64-bit
19277 SP----> +---------------------------------------+
19278 | back chain to caller | 0 0
19279 +---------------------------------------+
19280 | saved CR | 4 8 (8-11)
19281 +---------------------------------------+
19282 | saved LR | 8 16
19283 +---------------------------------------+
19284 | reserved for compilers | 12 24
19285 +---------------------------------------+
19286 | reserved for binders | 16 32
19287 +---------------------------------------+
19288 | saved TOC pointer | 20 40
19289 +---------------------------------------+
19290 | Parameter save area (P) | 24 48
19291 +---------------------------------------+
19292 | Alloca space (A) | 24+P etc.
19293 +---------------------------------------+
19294 | Local variable space (L) | 24+P+A
19295 +---------------------------------------+
19296 | Float/int conversion temporary (X) | 24+P+A+L
19297 +---------------------------------------+
19298 | Save area for AltiVec registers (W) | 24+P+A+L+X
19299 +---------------------------------------+
19300 | AltiVec alignment padding (Y) | 24+P+A+L+X+W
19301 +---------------------------------------+
19302 | Save area for VRSAVE register (Z) | 24+P+A+L+X+W+Y
19303 +---------------------------------------+
19304 | Save area for GP registers (G) | 24+P+A+X+L+X+W+Y+Z
19305 +---------------------------------------+
19306 | Save area for FP registers (F) | 24+P+A+X+L+X+W+Y+Z+G
19307 +---------------------------------------+
19308 old SP->| back chain to caller's caller |
19309 +---------------------------------------+
19311 The required alignment for AIX configurations is two words (i.e., 8
19312 or 16 bytes).
19315 V.4 stack frames look like:
19317 SP----> +---------------------------------------+
19318 | back chain to caller | 0
19319 +---------------------------------------+
19320 | caller's saved LR | 4
19321 +---------------------------------------+
19322 | Parameter save area (P) | 8
19323 +---------------------------------------+
19324 | Alloca space (A) | 8+P
19325 +---------------------------------------+
19326 | Varargs save area (V) | 8+P+A
19327 +---------------------------------------+
19328 | Local variable space (L) | 8+P+A+V
19329 +---------------------------------------+
19330 | Float/int conversion temporary (X) | 8+P+A+V+L
19331 +---------------------------------------+
19332 | Save area for AltiVec registers (W) | 8+P+A+V+L+X
19333 +---------------------------------------+
19334 | AltiVec alignment padding (Y) | 8+P+A+V+L+X+W
19335 +---------------------------------------+
19336 | Save area for VRSAVE register (Z) | 8+P+A+V+L+X+W+Y
19337 +---------------------------------------+
19338 | SPE: area for 64-bit GP registers |
19339 +---------------------------------------+
19340 | SPE alignment padding |
19341 +---------------------------------------+
19342 | saved CR (C) | 8+P+A+V+L+X+W+Y+Z
19343 +---------------------------------------+
19344 | Save area for GP registers (G) | 8+P+A+V+L+X+W+Y+Z+C
19345 +---------------------------------------+
19346 | Save area for FP registers (F) | 8+P+A+V+L+X+W+Y+Z+C+G
19347 +---------------------------------------+
19348 old SP->| back chain to caller's caller |
19349 +---------------------------------------+
19351 The required alignment for V.4 is 16 bytes, or 8 bytes if -meabi is
19352 given. (But note below and in sysv4.h that we require only 8 and
19353 may round up the size of our stack frame anyways. The historical
19354 reason is early versions of powerpc-linux which didn't properly
19355 align the stack at program startup. A happy side-effect is that
19356 -mno-eabi libraries can be used with -meabi programs.)
19358 The EABI configuration defaults to the V.4 layout. However,
19359 the stack alignment requirements may differ. If -mno-eabi is not
19360 given, the required stack alignment is 8 bytes; if -mno-eabi is
19361 given, the required alignment is 16 bytes. (But see V.4 comment
19362 above.) */
19364 #ifndef ABI_STACK_BOUNDARY
19365 #define ABI_STACK_BOUNDARY STACK_BOUNDARY
19366 #endif
19368 static rs6000_stack_t *
19369 rs6000_stack_info (void)
19371 rs6000_stack_t *info_ptr = &stack_info;
19372 int reg_size = TARGET_32BIT ? 4 : 8;
19373 int ehrd_size;
19374 int save_align;
19375 int first_gp;
19376 HOST_WIDE_INT non_fixed_size;
19377 bool using_static_chain_p;
19379 if (reload_completed && info_ptr->reload_completed)
19380 return info_ptr;
19382 memset (info_ptr, 0, sizeof (*info_ptr));
19383 info_ptr->reload_completed = reload_completed;
19385 if (TARGET_SPE)
19387 /* Cache value so we don't rescan instruction chain over and over. */
19388 if (cfun->machine->insn_chain_scanned_p == 0)
19389 cfun->machine->insn_chain_scanned_p
19390 = spe_func_has_64bit_regs_p () + 1;
19391 info_ptr->spe_64bit_regs_used = cfun->machine->insn_chain_scanned_p - 1;
19394 /* Select which calling sequence. */
19395 info_ptr->abi = DEFAULT_ABI;
19397 /* Calculate which registers need to be saved & save area size. */
19398 info_ptr->first_gp_reg_save = first_reg_to_save ();
19399 /* Assume that we will have to save RS6000_PIC_OFFSET_TABLE_REGNUM,
19400 even if it currently looks like we won't. Reload may need it to
19401 get at a constant; if so, it will have already created a constant
19402 pool entry for it. */
19403 if (((TARGET_TOC && TARGET_MINIMAL_TOC)
19404 || (flag_pic == 1 && DEFAULT_ABI == ABI_V4)
19405 || (flag_pic && DEFAULT_ABI == ABI_DARWIN))
19406 && crtl->uses_const_pool
19407 && info_ptr->first_gp_reg_save > RS6000_PIC_OFFSET_TABLE_REGNUM)
19408 first_gp = RS6000_PIC_OFFSET_TABLE_REGNUM;
19409 else
19410 first_gp = info_ptr->first_gp_reg_save;
19412 info_ptr->gp_size = reg_size * (32 - first_gp);
19414 /* For the SPE, we have an additional upper 32-bits on each GPR.
19415 Ideally we should save the entire 64-bits only when the upper
19416 half is used in SIMD instructions. Since we only record
19417 registers live (not the size they are used in), this proves
19418 difficult because we'd have to traverse the instruction chain at
19419 the right time, taking reload into account. This is a real pain,
19420 so we opt to save the GPRs in 64-bits always if but one register
19421 gets used in 64-bits. Otherwise, all the registers in the frame
19422 get saved in 32-bits.
19424 So... since when we save all GPRs (except the SP) in 64-bits, the
19425 traditional GP save area will be empty. */
19426 if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
19427 info_ptr->gp_size = 0;
19429 info_ptr->first_fp_reg_save = first_fp_reg_to_save ();
19430 info_ptr->fp_size = 8 * (64 - info_ptr->first_fp_reg_save);
19432 info_ptr->first_altivec_reg_save = first_altivec_reg_to_save ();
19433 info_ptr->altivec_size = 16 * (LAST_ALTIVEC_REGNO + 1
19434 - info_ptr->first_altivec_reg_save);
19436 /* Does this function call anything? */
19437 info_ptr->calls_p = (! crtl->is_leaf
19438 || cfun->machine->ra_needs_full_frame);
19440 /* Determine if we need to save the condition code registers. */
19441 if (df_regs_ever_live_p (CR2_REGNO)
19442 || df_regs_ever_live_p (CR3_REGNO)
19443 || df_regs_ever_live_p (CR4_REGNO))
19445 info_ptr->cr_save_p = 1;
19446 if (DEFAULT_ABI == ABI_V4)
19447 info_ptr->cr_size = reg_size;
19450 /* If the current function calls __builtin_eh_return, then we need
19451 to allocate stack space for registers that will hold data for
19452 the exception handler. */
19453 if (crtl->calls_eh_return)
19455 unsigned int i;
19456 for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; ++i)
19457 continue;
19459 /* SPE saves EH registers in 64-bits. */
19460 ehrd_size = i * (TARGET_SPE_ABI
19461 && info_ptr->spe_64bit_regs_used != 0
19462 ? UNITS_PER_SPE_WORD : UNITS_PER_WORD);
19464 else
19465 ehrd_size = 0;
19467 /* Determine various sizes. */
19468 info_ptr->reg_size = reg_size;
19469 info_ptr->fixed_size = RS6000_SAVE_AREA;
19470 info_ptr->vars_size = RS6000_ALIGN (get_frame_size (), 8);
19471 info_ptr->parm_size = RS6000_ALIGN (crtl->outgoing_args_size,
19472 TARGET_ALTIVEC ? 16 : 8);
19473 if (FRAME_GROWS_DOWNWARD)
19474 info_ptr->vars_size
19475 += RS6000_ALIGN (info_ptr->fixed_size + info_ptr->vars_size
19476 + info_ptr->parm_size,
19477 ABI_STACK_BOUNDARY / BITS_PER_UNIT)
19478 - (info_ptr->fixed_size + info_ptr->vars_size
19479 + info_ptr->parm_size);
19481 if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
19482 info_ptr->spe_gp_size = 8 * (32 - first_gp);
19483 else
19484 info_ptr->spe_gp_size = 0;
19486 if (TARGET_ALTIVEC_ABI)
19487 info_ptr->vrsave_mask = compute_vrsave_mask ();
19488 else
19489 info_ptr->vrsave_mask = 0;
19491 if (TARGET_ALTIVEC_VRSAVE && info_ptr->vrsave_mask)
19492 info_ptr->vrsave_size = 4;
19493 else
19494 info_ptr->vrsave_size = 0;
19496 compute_save_world_info (info_ptr);
19498 /* Calculate the offsets. */
19499 switch (DEFAULT_ABI)
19501 case ABI_NONE:
19502 default:
19503 gcc_unreachable ();
19505 case ABI_AIX:
19506 case ABI_DARWIN:
19507 info_ptr->fp_save_offset = - info_ptr->fp_size;
19508 info_ptr->gp_save_offset = info_ptr->fp_save_offset - info_ptr->gp_size;
19510 if (TARGET_ALTIVEC_ABI)
19512 info_ptr->vrsave_save_offset
19513 = info_ptr->gp_save_offset - info_ptr->vrsave_size;
19515 /* Align stack so vector save area is on a quadword boundary.
19516 The padding goes above the vectors. */
19517 if (info_ptr->altivec_size != 0)
19518 info_ptr->altivec_padding_size
19519 = info_ptr->vrsave_save_offset & 0xF;
19520 else
19521 info_ptr->altivec_padding_size = 0;
19523 info_ptr->altivec_save_offset
19524 = info_ptr->vrsave_save_offset
19525 - info_ptr->altivec_padding_size
19526 - info_ptr->altivec_size;
19527 gcc_assert (info_ptr->altivec_size == 0
19528 || info_ptr->altivec_save_offset % 16 == 0);
19530 /* Adjust for AltiVec case. */
19531 info_ptr->ehrd_offset = info_ptr->altivec_save_offset - ehrd_size;
19533 else
19534 info_ptr->ehrd_offset = info_ptr->gp_save_offset - ehrd_size;
19535 info_ptr->cr_save_offset = reg_size; /* first word when 64-bit. */
19536 info_ptr->lr_save_offset = 2*reg_size;
19537 break;
19539 case ABI_V4:
19540 info_ptr->fp_save_offset = - info_ptr->fp_size;
19541 info_ptr->gp_save_offset = info_ptr->fp_save_offset - info_ptr->gp_size;
19542 info_ptr->cr_save_offset = info_ptr->gp_save_offset - info_ptr->cr_size;
19544 if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
19546 /* Align stack so SPE GPR save area is aligned on a
19547 double-word boundary. */
19548 if (info_ptr->spe_gp_size != 0 && info_ptr->cr_save_offset != 0)
19549 info_ptr->spe_padding_size
19550 = 8 - (-info_ptr->cr_save_offset % 8);
19551 else
19552 info_ptr->spe_padding_size = 0;
19554 info_ptr->spe_gp_save_offset
19555 = info_ptr->cr_save_offset
19556 - info_ptr->spe_padding_size
19557 - info_ptr->spe_gp_size;
19559 /* Adjust for SPE case. */
19560 info_ptr->ehrd_offset = info_ptr->spe_gp_save_offset;
19562 else if (TARGET_ALTIVEC_ABI)
19564 info_ptr->vrsave_save_offset
19565 = info_ptr->cr_save_offset - info_ptr->vrsave_size;
19567 /* Align stack so vector save area is on a quadword boundary. */
19568 if (info_ptr->altivec_size != 0)
19569 info_ptr->altivec_padding_size
19570 = 16 - (-info_ptr->vrsave_save_offset % 16);
19571 else
19572 info_ptr->altivec_padding_size = 0;
19574 info_ptr->altivec_save_offset
19575 = info_ptr->vrsave_save_offset
19576 - info_ptr->altivec_padding_size
19577 - info_ptr->altivec_size;
19579 /* Adjust for AltiVec case. */
19580 info_ptr->ehrd_offset = info_ptr->altivec_save_offset;
19582 else
19583 info_ptr->ehrd_offset = info_ptr->cr_save_offset;
19584 info_ptr->ehrd_offset -= ehrd_size;
19585 info_ptr->lr_save_offset = reg_size;
19586 break;
19589 save_align = (TARGET_ALTIVEC_ABI || DEFAULT_ABI == ABI_DARWIN) ? 16 : 8;
19590 info_ptr->save_size = RS6000_ALIGN (info_ptr->fp_size
19591 + info_ptr->gp_size
19592 + info_ptr->altivec_size
19593 + info_ptr->altivec_padding_size
19594 + info_ptr->spe_gp_size
19595 + info_ptr->spe_padding_size
19596 + ehrd_size
19597 + info_ptr->cr_size
19598 + info_ptr->vrsave_size,
19599 save_align);
19601 non_fixed_size = (info_ptr->vars_size
19602 + info_ptr->parm_size
19603 + info_ptr->save_size);
19605 info_ptr->total_size = RS6000_ALIGN (non_fixed_size + info_ptr->fixed_size,
19606 ABI_STACK_BOUNDARY / BITS_PER_UNIT);
19608 /* Determine if we need to save the link register. */
19609 if (info_ptr->calls_p
19610 || (DEFAULT_ABI == ABI_AIX
19611 && crtl->profile
19612 && !TARGET_PROFILE_KERNEL)
19613 || (DEFAULT_ABI == ABI_V4 && cfun->calls_alloca)
19614 #ifdef TARGET_RELOCATABLE
19615 || (TARGET_RELOCATABLE && (get_pool_size () != 0))
19616 #endif
19617 || rs6000_ra_ever_killed ())
19618 info_ptr->lr_save_p = 1;
19620 using_static_chain_p = (cfun->static_chain_decl != NULL_TREE
19621 && df_regs_ever_live_p (STATIC_CHAIN_REGNUM)
19622 && call_used_regs[STATIC_CHAIN_REGNUM]);
19623 info_ptr->savres_strategy = rs6000_savres_strategy (info_ptr,
19624 using_static_chain_p);
19626 if (!(info_ptr->savres_strategy & SAVE_INLINE_GPRS)
19627 || !(info_ptr->savres_strategy & SAVE_INLINE_FPRS)
19628 || !(info_ptr->savres_strategy & SAVE_INLINE_VRS)
19629 || !(info_ptr->savres_strategy & REST_INLINE_GPRS)
19630 || !(info_ptr->savres_strategy & REST_INLINE_FPRS)
19631 || !(info_ptr->savres_strategy & REST_INLINE_VRS))
19632 info_ptr->lr_save_p = 1;
19634 if (info_ptr->lr_save_p)
19635 df_set_regs_ever_live (LR_REGNO, true);
19637 /* Determine if we need to allocate any stack frame:
19639 For AIX we need to push the stack if a frame pointer is needed
19640 (because the stack might be dynamically adjusted), if we are
19641 debugging, if we make calls, or if the sum of fp_save, gp_save,
19642 and local variables are more than the space needed to save all
19643 non-volatile registers: 32-bit: 18*8 + 19*4 = 220 or 64-bit: 18*8
19644 + 18*8 = 288 (GPR13 reserved).
19646 For V.4 we don't have the stack cushion that AIX uses, but assume
19647 that the debugger can handle stackless frames. */
19649 if (info_ptr->calls_p)
19650 info_ptr->push_p = 1;
19652 else if (DEFAULT_ABI == ABI_V4)
19653 info_ptr->push_p = non_fixed_size != 0;
19655 else if (frame_pointer_needed)
19656 info_ptr->push_p = 1;
19658 else if (TARGET_XCOFF && write_symbols != NO_DEBUG)
19659 info_ptr->push_p = 1;
19661 else
19662 info_ptr->push_p = non_fixed_size > (TARGET_32BIT ? 220 : 288);
19664 /* Zero offsets if we're not saving those registers. */
19665 if (info_ptr->fp_size == 0)
19666 info_ptr->fp_save_offset = 0;
19668 if (info_ptr->gp_size == 0)
19669 info_ptr->gp_save_offset = 0;
19671 if (! TARGET_ALTIVEC_ABI || info_ptr->altivec_size == 0)
19672 info_ptr->altivec_save_offset = 0;
19674 /* Zero VRSAVE offset if not saved and restored. */
19675 if (! TARGET_ALTIVEC_VRSAVE || info_ptr->vrsave_mask == 0)
19676 info_ptr->vrsave_save_offset = 0;
19678 if (! TARGET_SPE_ABI
19679 || info_ptr->spe_64bit_regs_used == 0
19680 || info_ptr->spe_gp_size == 0)
19681 info_ptr->spe_gp_save_offset = 0;
19683 if (! info_ptr->lr_save_p)
19684 info_ptr->lr_save_offset = 0;
19686 if (! info_ptr->cr_save_p)
19687 info_ptr->cr_save_offset = 0;
19689 return info_ptr;
19692 /* Return true if the current function uses any GPRs in 64-bit SIMD
19693 mode. */
19695 static bool
19696 spe_func_has_64bit_regs_p (void)
19698 rtx insns, insn;
19700 /* Functions that save and restore all the call-saved registers will
19701 need to save/restore the registers in 64-bits. */
19702 if (crtl->calls_eh_return
19703 || cfun->calls_setjmp
19704 || crtl->has_nonlocal_goto)
19705 return true;
19707 insns = get_insns ();
19709 for (insn = NEXT_INSN (insns); insn != NULL_RTX; insn = NEXT_INSN (insn))
19711 if (INSN_P (insn))
19713 rtx i;
19715 /* FIXME: This should be implemented with attributes...
19717 (set_attr "spe64" "true")....then,
19718 if (get_spe64(insn)) return true;
19720 It's the only reliable way to do the stuff below. */
19722 i = PATTERN (insn);
19723 if (GET_CODE (i) == SET)
19725 enum machine_mode mode = GET_MODE (SET_SRC (i));
19727 if (SPE_VECTOR_MODE (mode))
19728 return true;
19729 if (TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode))
19730 return true;
19735 return false;
19738 static void
19739 debug_stack_info (rs6000_stack_t *info)
19741 const char *abi_string;
19743 if (! info)
19744 info = rs6000_stack_info ();
19746 fprintf (stderr, "\nStack information for function %s:\n",
19747 ((current_function_decl && DECL_NAME (current_function_decl))
19748 ? IDENTIFIER_POINTER (DECL_NAME (current_function_decl))
19749 : "<unknown>"));
19751 switch (info->abi)
19753 default: abi_string = "Unknown"; break;
19754 case ABI_NONE: abi_string = "NONE"; break;
19755 case ABI_AIX: abi_string = "AIX"; break;
19756 case ABI_DARWIN: abi_string = "Darwin"; break;
19757 case ABI_V4: abi_string = "V.4"; break;
19760 fprintf (stderr, "\tABI = %5s\n", abi_string);
19762 if (TARGET_ALTIVEC_ABI)
19763 fprintf (stderr, "\tALTIVEC ABI extensions enabled.\n");
19765 if (TARGET_SPE_ABI)
19766 fprintf (stderr, "\tSPE ABI extensions enabled.\n");
19768 if (info->first_gp_reg_save != 32)
19769 fprintf (stderr, "\tfirst_gp_reg_save = %5d\n", info->first_gp_reg_save);
19771 if (info->first_fp_reg_save != 64)
19772 fprintf (stderr, "\tfirst_fp_reg_save = %5d\n", info->first_fp_reg_save);
19774 if (info->first_altivec_reg_save <= LAST_ALTIVEC_REGNO)
19775 fprintf (stderr, "\tfirst_altivec_reg_save = %5d\n",
19776 info->first_altivec_reg_save);
19778 if (info->lr_save_p)
19779 fprintf (stderr, "\tlr_save_p = %5d\n", info->lr_save_p);
19781 if (info->cr_save_p)
19782 fprintf (stderr, "\tcr_save_p = %5d\n", info->cr_save_p);
19784 if (info->vrsave_mask)
19785 fprintf (stderr, "\tvrsave_mask = 0x%x\n", info->vrsave_mask);
19787 if (info->push_p)
19788 fprintf (stderr, "\tpush_p = %5d\n", info->push_p);
19790 if (info->calls_p)
19791 fprintf (stderr, "\tcalls_p = %5d\n", info->calls_p);
19793 if (info->gp_save_offset)
19794 fprintf (stderr, "\tgp_save_offset = %5d\n", info->gp_save_offset);
19796 if (info->fp_save_offset)
19797 fprintf (stderr, "\tfp_save_offset = %5d\n", info->fp_save_offset);
19799 if (info->altivec_save_offset)
19800 fprintf (stderr, "\taltivec_save_offset = %5d\n",
19801 info->altivec_save_offset);
19803 if (info->spe_gp_save_offset)
19804 fprintf (stderr, "\tspe_gp_save_offset = %5d\n",
19805 info->spe_gp_save_offset);
19807 if (info->vrsave_save_offset)
19808 fprintf (stderr, "\tvrsave_save_offset = %5d\n",
19809 info->vrsave_save_offset);
19811 if (info->lr_save_offset)
19812 fprintf (stderr, "\tlr_save_offset = %5d\n", info->lr_save_offset);
19814 if (info->cr_save_offset)
19815 fprintf (stderr, "\tcr_save_offset = %5d\n", info->cr_save_offset);
19817 if (info->varargs_save_offset)
19818 fprintf (stderr, "\tvarargs_save_offset = %5d\n", info->varargs_save_offset);
19820 if (info->total_size)
19821 fprintf (stderr, "\ttotal_size = "HOST_WIDE_INT_PRINT_DEC"\n",
19822 info->total_size);
19824 if (info->vars_size)
19825 fprintf (stderr, "\tvars_size = "HOST_WIDE_INT_PRINT_DEC"\n",
19826 info->vars_size);
19828 if (info->parm_size)
19829 fprintf (stderr, "\tparm_size = %5d\n", info->parm_size);
19831 if (info->fixed_size)
19832 fprintf (stderr, "\tfixed_size = %5d\n", info->fixed_size);
19834 if (info->gp_size)
19835 fprintf (stderr, "\tgp_size = %5d\n", info->gp_size);
19837 if (info->spe_gp_size)
19838 fprintf (stderr, "\tspe_gp_size = %5d\n", info->spe_gp_size);
19840 if (info->fp_size)
19841 fprintf (stderr, "\tfp_size = %5d\n", info->fp_size);
19843 if (info->altivec_size)
19844 fprintf (stderr, "\taltivec_size = %5d\n", info->altivec_size);
19846 if (info->vrsave_size)
19847 fprintf (stderr, "\tvrsave_size = %5d\n", info->vrsave_size);
19849 if (info->altivec_padding_size)
19850 fprintf (stderr, "\taltivec_padding_size= %5d\n",
19851 info->altivec_padding_size);
19853 if (info->spe_padding_size)
19854 fprintf (stderr, "\tspe_padding_size = %5d\n",
19855 info->spe_padding_size);
19857 if (info->cr_size)
19858 fprintf (stderr, "\tcr_size = %5d\n", info->cr_size);
19860 if (info->save_size)
19861 fprintf (stderr, "\tsave_size = %5d\n", info->save_size);
19863 if (info->reg_size != 4)
19864 fprintf (stderr, "\treg_size = %5d\n", info->reg_size);
19866 fprintf (stderr, "\tsave-strategy = %04x\n", info->savres_strategy);
19868 fprintf (stderr, "\n");
19872 rs6000_return_addr (int count, rtx frame)
19874 /* Currently we don't optimize very well between prolog and body
19875 code and for PIC code the code can be actually quite bad, so
19876 don't try to be too clever here. */
19877 if (count != 0 || (DEFAULT_ABI != ABI_AIX && flag_pic))
19879 cfun->machine->ra_needs_full_frame = 1;
19881 return
19882 gen_rtx_MEM
19883 (Pmode,
19884 memory_address
19885 (Pmode,
19886 plus_constant (Pmode,
19887 copy_to_reg
19888 (gen_rtx_MEM (Pmode,
19889 memory_address (Pmode, frame))),
19890 RETURN_ADDRESS_OFFSET)));
19893 cfun->machine->ra_need_lr = 1;
19894 return get_hard_reg_initial_val (Pmode, LR_REGNO);
19897 /* Say whether a function is a candidate for sibcall handling or not. */
19899 static bool
19900 rs6000_function_ok_for_sibcall (tree decl, tree exp)
19902 tree fntype;
19904 if (decl)
19905 fntype = TREE_TYPE (decl);
19906 else
19907 fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (exp)));
19909 /* We can't do it if the called function has more vector parameters
19910 than the current function; there's nowhere to put the VRsave code. */
19911 if (TARGET_ALTIVEC_ABI
19912 && TARGET_ALTIVEC_VRSAVE
19913 && !(decl && decl == current_function_decl))
19915 function_args_iterator args_iter;
19916 tree type;
19917 int nvreg = 0;
19919 /* Functions with vector parameters are required to have a
19920 prototype, so the argument type info must be available
19921 here. */
19922 FOREACH_FUNCTION_ARGS(fntype, type, args_iter)
19923 if (TREE_CODE (type) == VECTOR_TYPE
19924 && ALTIVEC_OR_VSX_VECTOR_MODE (TYPE_MODE (type)))
19925 nvreg++;
19927 FOREACH_FUNCTION_ARGS(TREE_TYPE (current_function_decl), type, args_iter)
19928 if (TREE_CODE (type) == VECTOR_TYPE
19929 && ALTIVEC_OR_VSX_VECTOR_MODE (TYPE_MODE (type)))
19930 nvreg--;
19932 if (nvreg > 0)
19933 return false;
19936 /* Under the AIX ABI we can't allow calls to non-local functions,
19937 because the callee may have a different TOC pointer to the
19938 caller and there's no way to ensure we restore the TOC when we
19939 return. With the secure-plt SYSV ABI we can't make non-local
19940 calls when -fpic/PIC because the plt call stubs use r30. */
19941 if (DEFAULT_ABI == ABI_DARWIN
19942 || (DEFAULT_ABI == ABI_AIX
19943 && decl
19944 && !DECL_EXTERNAL (decl)
19945 && (*targetm.binds_local_p) (decl))
19946 || (DEFAULT_ABI == ABI_V4
19947 && (!TARGET_SECURE_PLT
19948 || !flag_pic
19949 || (decl
19950 && (*targetm.binds_local_p) (decl)))))
19952 tree attr_list = TYPE_ATTRIBUTES (fntype);
19954 if (!lookup_attribute ("longcall", attr_list)
19955 || lookup_attribute ("shortcall", attr_list))
19956 return true;
19959 return false;
19962 static int
19963 rs6000_ra_ever_killed (void)
19965 rtx top;
19966 rtx reg;
19967 rtx insn;
19969 if (cfun->is_thunk)
19970 return 0;
19972 if (cfun->machine->lr_save_state)
19973 return cfun->machine->lr_save_state - 1;
19975 /* regs_ever_live has LR marked as used if any sibcalls are present,
19976 but this should not force saving and restoring in the
19977 pro/epilogue. Likewise, reg_set_between_p thinks a sibcall
19978 clobbers LR, so that is inappropriate. */
19980 /* Also, the prologue can generate a store into LR that
19981 doesn't really count, like this:
19983 move LR->R0
19984 bcl to set PIC register
19985 move LR->R31
19986 move R0->LR
19988 When we're called from the epilogue, we need to avoid counting
19989 this as a store. */
19991 push_topmost_sequence ();
19992 top = get_insns ();
19993 pop_topmost_sequence ();
19994 reg = gen_rtx_REG (Pmode, LR_REGNO);
19996 for (insn = NEXT_INSN (top); insn != NULL_RTX; insn = NEXT_INSN (insn))
19998 if (INSN_P (insn))
20000 if (CALL_P (insn))
20002 if (!SIBLING_CALL_P (insn))
20003 return 1;
20005 else if (find_regno_note (insn, REG_INC, LR_REGNO))
20006 return 1;
20007 else if (set_of (reg, insn) != NULL_RTX
20008 && !prologue_epilogue_contains (insn))
20009 return 1;
20012 return 0;
20015 /* Emit instructions needed to load the TOC register.
20016 This is only needed when TARGET_TOC, TARGET_MINIMAL_TOC, and there is
20017 a constant pool; or for SVR4 -fpic. */
20019 void
20020 rs6000_emit_load_toc_table (int fromprolog)
20022 rtx dest;
20023 dest = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
20025 if (TARGET_ELF && TARGET_SECURE_PLT && DEFAULT_ABI != ABI_AIX && flag_pic)
20027 char buf[30];
20028 rtx lab, tmp1, tmp2, got;
20030 lab = gen_label_rtx ();
20031 ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (lab));
20032 lab = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
20033 if (flag_pic == 2)
20034 got = gen_rtx_SYMBOL_REF (Pmode, toc_label_name);
20035 else
20036 got = rs6000_got_sym ();
20037 tmp1 = tmp2 = dest;
20038 if (!fromprolog)
20040 tmp1 = gen_reg_rtx (Pmode);
20041 tmp2 = gen_reg_rtx (Pmode);
20043 emit_insn (gen_load_toc_v4_PIC_1 (lab));
20044 emit_move_insn (tmp1, gen_rtx_REG (Pmode, LR_REGNO));
20045 emit_insn (gen_load_toc_v4_PIC_3b (tmp2, tmp1, got, lab));
20046 emit_insn (gen_load_toc_v4_PIC_3c (dest, tmp2, got, lab));
20048 else if (TARGET_ELF && DEFAULT_ABI == ABI_V4 && flag_pic == 1)
20050 emit_insn (gen_load_toc_v4_pic_si ());
20051 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
20053 else if (TARGET_ELF && DEFAULT_ABI != ABI_AIX && flag_pic == 2)
20055 char buf[30];
20056 rtx temp0 = (fromprolog
20057 ? gen_rtx_REG (Pmode, 0)
20058 : gen_reg_rtx (Pmode));
20060 if (fromprolog)
20062 rtx symF, symL;
20064 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
20065 symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
20067 ASM_GENERATE_INTERNAL_LABEL (buf, "LCL", rs6000_pic_labelno);
20068 symL = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
20070 emit_insn (gen_load_toc_v4_PIC_1 (symF));
20071 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
20072 emit_insn (gen_load_toc_v4_PIC_2 (temp0, dest, symL, symF));
20074 else
20076 rtx tocsym, lab;
20078 tocsym = gen_rtx_SYMBOL_REF (Pmode, toc_label_name);
20079 lab = gen_label_rtx ();
20080 emit_insn (gen_load_toc_v4_PIC_1b (tocsym, lab));
20081 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
20082 if (TARGET_LINK_STACK)
20083 emit_insn (gen_addsi3 (dest, dest, GEN_INT (4)));
20084 emit_move_insn (temp0, gen_rtx_MEM (Pmode, dest));
20086 emit_insn (gen_addsi3 (dest, temp0, dest));
20088 else if (TARGET_ELF && !TARGET_AIX && flag_pic == 0 && TARGET_MINIMAL_TOC)
20090 /* This is for AIX code running in non-PIC ELF32. */
20091 char buf[30];
20092 rtx realsym;
20093 ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
20094 realsym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
20096 emit_insn (gen_elf_high (dest, realsym));
20097 emit_insn (gen_elf_low (dest, dest, realsym));
20099 else
20101 gcc_assert (DEFAULT_ABI == ABI_AIX);
20103 if (TARGET_32BIT)
20104 emit_insn (gen_load_toc_aix_si (dest));
20105 else
20106 emit_insn (gen_load_toc_aix_di (dest));
20110 /* Emit instructions to restore the link register after determining where
20111 its value has been stored. */
20113 void
20114 rs6000_emit_eh_reg_restore (rtx source, rtx scratch)
20116 rs6000_stack_t *info = rs6000_stack_info ();
20117 rtx operands[2];
20119 operands[0] = source;
20120 operands[1] = scratch;
20122 if (info->lr_save_p)
20124 rtx frame_rtx = stack_pointer_rtx;
20125 HOST_WIDE_INT sp_offset = 0;
20126 rtx tmp;
20128 if (frame_pointer_needed
20129 || cfun->calls_alloca
20130 || info->total_size > 32767)
20132 tmp = gen_frame_mem (Pmode, frame_rtx);
20133 emit_move_insn (operands[1], tmp);
20134 frame_rtx = operands[1];
20136 else if (info->push_p)
20137 sp_offset = info->total_size;
20139 tmp = plus_constant (Pmode, frame_rtx,
20140 info->lr_save_offset + sp_offset);
20141 tmp = gen_frame_mem (Pmode, tmp);
20142 emit_move_insn (tmp, operands[0]);
20144 else
20145 emit_move_insn (gen_rtx_REG (Pmode, LR_REGNO), operands[0]);
20147 /* Freeze lr_save_p. We've just emitted rtl that depends on the
20148 state of lr_save_p so any change from here on would be a bug. In
20149 particular, stop rs6000_ra_ever_killed from considering the SET
20150 of lr we may have added just above. */
20151 cfun->machine->lr_save_state = info->lr_save_p + 1;
20154 static GTY(()) alias_set_type set = -1;
20156 alias_set_type
20157 get_TOC_alias_set (void)
20159 if (set == -1)
20160 set = new_alias_set ();
20161 return set;
20164 /* This returns nonzero if the current function uses the TOC. This is
20165 determined by the presence of (use (unspec ... UNSPEC_TOC)), which
20166 is generated by the ABI_V4 load_toc_* patterns. */
20167 #if TARGET_ELF
20168 static int
20169 uses_TOC (void)
20171 rtx insn;
20173 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
20174 if (INSN_P (insn))
20176 rtx pat = PATTERN (insn);
20177 int i;
20179 if (GET_CODE (pat) == PARALLEL)
20180 for (i = 0; i < XVECLEN (pat, 0); i++)
20182 rtx sub = XVECEXP (pat, 0, i);
20183 if (GET_CODE (sub) == USE)
20185 sub = XEXP (sub, 0);
20186 if (GET_CODE (sub) == UNSPEC
20187 && XINT (sub, 1) == UNSPEC_TOC)
20188 return 1;
20192 return 0;
20194 #endif
20197 create_TOC_reference (rtx symbol, rtx largetoc_reg)
20199 rtx tocrel, tocreg, hi;
20201 if (TARGET_DEBUG_ADDR)
20203 if (GET_CODE (symbol) == SYMBOL_REF)
20204 fprintf (stderr, "\ncreate_TOC_reference, (symbol_ref %s)\n",
20205 XSTR (symbol, 0));
20206 else
20208 fprintf (stderr, "\ncreate_TOC_reference, code %s:\n",
20209 GET_RTX_NAME (GET_CODE (symbol)));
20210 debug_rtx (symbol);
20214 if (!can_create_pseudo_p ())
20215 df_set_regs_ever_live (TOC_REGISTER, true);
20217 tocreg = gen_rtx_REG (Pmode, TOC_REGISTER);
20218 tocrel = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, symbol, tocreg), UNSPEC_TOCREL);
20219 if (TARGET_CMODEL == CMODEL_SMALL || can_create_pseudo_p ())
20220 return tocrel;
20222 hi = gen_rtx_HIGH (Pmode, copy_rtx (tocrel));
20223 if (largetoc_reg != NULL)
20225 emit_move_insn (largetoc_reg, hi);
20226 hi = largetoc_reg;
20228 return gen_rtx_LO_SUM (Pmode, hi, tocrel);
20231 /* Issue assembly directives that create a reference to the given DWARF
20232 FRAME_TABLE_LABEL from the current function section. */
20233 void
20234 rs6000_aix_asm_output_dwarf_table_ref (char * frame_table_label)
20236 fprintf (asm_out_file, "\t.ref %s\n",
20237 (* targetm.strip_name_encoding) (frame_table_label));
20240 /* This ties together stack memory (MEM with an alias set of frame_alias_set)
20241 and the change to the stack pointer. */
20243 static void
20244 rs6000_emit_stack_tie (rtx fp, bool hard_frame_needed)
20246 rtvec p;
20247 int i;
20248 rtx regs[3];
20250 i = 0;
20251 regs[i++] = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
20252 if (hard_frame_needed)
20253 regs[i++] = gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM);
20254 if (!(REGNO (fp) == STACK_POINTER_REGNUM
20255 || (hard_frame_needed
20256 && REGNO (fp) == HARD_FRAME_POINTER_REGNUM)))
20257 regs[i++] = fp;
20259 p = rtvec_alloc (i);
20260 while (--i >= 0)
20262 rtx mem = gen_frame_mem (BLKmode, regs[i]);
20263 RTVEC_ELT (p, i) = gen_rtx_SET (VOIDmode, mem, const0_rtx);
20266 emit_insn (gen_stack_tie (gen_rtx_PARALLEL (VOIDmode, p)));
20269 /* Emit the correct code for allocating stack space, as insns.
20270 If COPY_REG, make sure a copy of the old frame is left there.
20271 The generated code may use hard register 0 as a temporary. */
20273 static void
20274 rs6000_emit_allocate_stack (HOST_WIDE_INT size, rtx copy_reg, int copy_off)
20276 rtx insn;
20277 rtx stack_reg = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
20278 rtx tmp_reg = gen_rtx_REG (Pmode, 0);
20279 rtx todec = gen_int_mode (-size, Pmode);
20280 rtx par, set, mem;
20282 if (INTVAL (todec) != -size)
20284 warning (0, "stack frame too large");
20285 emit_insn (gen_trap ());
20286 return;
20289 if (crtl->limit_stack)
20291 if (REG_P (stack_limit_rtx)
20292 && REGNO (stack_limit_rtx) > 1
20293 && REGNO (stack_limit_rtx) <= 31)
20295 emit_insn (gen_add3_insn (tmp_reg, stack_limit_rtx, GEN_INT (size)));
20296 emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
20297 const0_rtx));
20299 else if (GET_CODE (stack_limit_rtx) == SYMBOL_REF
20300 && TARGET_32BIT
20301 && DEFAULT_ABI == ABI_V4)
20303 rtx toload = gen_rtx_CONST (VOIDmode,
20304 gen_rtx_PLUS (Pmode,
20305 stack_limit_rtx,
20306 GEN_INT (size)));
20308 emit_insn (gen_elf_high (tmp_reg, toload));
20309 emit_insn (gen_elf_low (tmp_reg, tmp_reg, toload));
20310 emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
20311 const0_rtx));
20313 else
20314 warning (0, "stack limit expression is not supported");
20317 if (copy_reg)
20319 if (copy_off != 0)
20320 emit_insn (gen_add3_insn (copy_reg, stack_reg, GEN_INT (copy_off)));
20321 else
20322 emit_move_insn (copy_reg, stack_reg);
20325 if (size > 32767)
20327 /* Need a note here so that try_split doesn't get confused. */
20328 if (get_last_insn () == NULL_RTX)
20329 emit_note (NOTE_INSN_DELETED);
20330 insn = emit_move_insn (tmp_reg, todec);
20331 try_split (PATTERN (insn), insn, 0);
20332 todec = tmp_reg;
20335 insn = emit_insn (TARGET_32BIT
20336 ? gen_movsi_update_stack (stack_reg, stack_reg,
20337 todec, stack_reg)
20338 : gen_movdi_di_update_stack (stack_reg, stack_reg,
20339 todec, stack_reg));
20340 /* Since we didn't use gen_frame_mem to generate the MEM, grab
20341 it now and set the alias set/attributes. The above gen_*_update
20342 calls will generate a PARALLEL with the MEM set being the first
20343 operation. */
20344 par = PATTERN (insn);
20345 gcc_assert (GET_CODE (par) == PARALLEL);
20346 set = XVECEXP (par, 0, 0);
20347 gcc_assert (GET_CODE (set) == SET);
20348 mem = SET_DEST (set);
20349 gcc_assert (MEM_P (mem));
20350 MEM_NOTRAP_P (mem) = 1;
20351 set_mem_alias_set (mem, get_frame_alias_set ());
20353 RTX_FRAME_RELATED_P (insn) = 1;
20354 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
20355 gen_rtx_SET (VOIDmode, stack_reg,
20356 gen_rtx_PLUS (Pmode, stack_reg,
20357 GEN_INT (-size))));
20360 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
20362 #if PROBE_INTERVAL > 32768
20363 #error Cannot use indexed addressing mode for stack probing
20364 #endif
20366 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
20367 inclusive. These are offsets from the current stack pointer. */
20369 static void
20370 rs6000_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
20372 /* See if we have a constant small number of probes to generate. If so,
20373 that's the easy case. */
20374 if (first + size <= 32768)
20376 HOST_WIDE_INT i;
20378 /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
20379 it exceeds SIZE. If only one probe is needed, this will not
20380 generate any code. Then probe at FIRST + SIZE. */
20381 for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
20382 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
20383 -(first + i)));
20385 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
20386 -(first + size)));
20389 /* Otherwise, do the same as above, but in a loop. Note that we must be
20390 extra careful with variables wrapping around because we might be at
20391 the very top (or the very bottom) of the address space and we have
20392 to be able to handle this case properly; in particular, we use an
20393 equality test for the loop condition. */
20394 else
20396 HOST_WIDE_INT rounded_size;
20397 rtx r12 = gen_rtx_REG (Pmode, 12);
20398 rtx r0 = gen_rtx_REG (Pmode, 0);
20400 /* Sanity check for the addressing mode we're going to use. */
20401 gcc_assert (first <= 32768);
20403 /* Step 1: round SIZE to the previous multiple of the interval. */
20405 rounded_size = size & -PROBE_INTERVAL;
20408 /* Step 2: compute initial and final value of the loop counter. */
20410 /* TEST_ADDR = SP + FIRST. */
20411 emit_insn (gen_rtx_SET (VOIDmode, r12,
20412 plus_constant (Pmode, stack_pointer_rtx,
20413 -first)));
20415 /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE. */
20416 if (rounded_size > 32768)
20418 emit_move_insn (r0, GEN_INT (-rounded_size));
20419 emit_insn (gen_rtx_SET (VOIDmode, r0,
20420 gen_rtx_PLUS (Pmode, r12, r0)));
20422 else
20423 emit_insn (gen_rtx_SET (VOIDmode, r0,
20424 plus_constant (Pmode, r12, -rounded_size)));
20427 /* Step 3: the loop
20429 while (TEST_ADDR != LAST_ADDR)
20431 TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
20432 probe at TEST_ADDR
20435 probes at FIRST + N * PROBE_INTERVAL for values of N from 1
20436 until it is equal to ROUNDED_SIZE. */
20438 if (TARGET_64BIT)
20439 emit_insn (gen_probe_stack_rangedi (r12, r12, r0));
20440 else
20441 emit_insn (gen_probe_stack_rangesi (r12, r12, r0));
20444 /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
20445 that SIZE is equal to ROUNDED_SIZE. */
20447 if (size != rounded_size)
20448 emit_stack_probe (plus_constant (Pmode, r12, rounded_size - size));
20452 /* Probe a range of stack addresses from REG1 to REG2 inclusive. These are
20453 absolute addresses. */
20455 const char *
20456 output_probe_stack_range (rtx reg1, rtx reg2)
20458 static int labelno = 0;
20459 char loop_lab[32], end_lab[32];
20460 rtx xops[2];
20462 ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno);
20463 ASM_GENERATE_INTERNAL_LABEL (end_lab, "LPSRE", labelno++);
20465 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
20467 /* Jump to END_LAB if TEST_ADDR == LAST_ADDR. */
20468 xops[0] = reg1;
20469 xops[1] = reg2;
20470 if (TARGET_64BIT)
20471 output_asm_insn ("cmpd 0,%0,%1", xops);
20472 else
20473 output_asm_insn ("cmpw 0,%0,%1", xops);
20475 fputs ("\tbeq 0,", asm_out_file);
20476 assemble_name_raw (asm_out_file, end_lab);
20477 fputc ('\n', asm_out_file);
20479 /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL. */
20480 xops[1] = GEN_INT (-PROBE_INTERVAL);
20481 output_asm_insn ("addi %0,%0,%1", xops);
20483 /* Probe at TEST_ADDR and branch. */
20484 xops[1] = gen_rtx_REG (Pmode, 0);
20485 output_asm_insn ("stw %1,0(%0)", xops);
20486 fprintf (asm_out_file, "\tb ");
20487 assemble_name_raw (asm_out_file, loop_lab);
20488 fputc ('\n', asm_out_file);
20490 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, end_lab);
20492 return "";
20495 /* Add to 'insn' a note which is PATTERN (INSN) but with REG replaced
20496 with (plus:P (reg 1) VAL), and with REG2 replaced with RREG if REG2
20497 is not NULL. It would be nice if dwarf2out_frame_debug_expr could
20498 deduce these equivalences by itself so it wasn't necessary to hold
20499 its hand so much. Don't be tempted to always supply d2_f_d_e with
20500 the actual cfa register, ie. r31 when we are using a hard frame
20501 pointer. That fails when saving regs off r1, and sched moves the
20502 r31 setup past the reg saves. */
20504 static rtx
20505 rs6000_frame_related (rtx insn, rtx reg, HOST_WIDE_INT val,
20506 rtx reg2, rtx rreg)
20508 rtx real, temp;
20510 if (REGNO (reg) == STACK_POINTER_REGNUM && reg2 == NULL_RTX)
20512 /* No need for any replacement. Just set RTX_FRAME_RELATED_P. */
20513 int i;
20515 gcc_checking_assert (val == 0);
20516 real = PATTERN (insn);
20517 if (GET_CODE (real) == PARALLEL)
20518 for (i = 0; i < XVECLEN (real, 0); i++)
20519 if (GET_CODE (XVECEXP (real, 0, i)) == SET)
20521 rtx set = XVECEXP (real, 0, i);
20523 RTX_FRAME_RELATED_P (set) = 1;
20525 RTX_FRAME_RELATED_P (insn) = 1;
20526 return insn;
20529 /* copy_rtx will not make unique copies of registers, so we need to
20530 ensure we don't have unwanted sharing here. */
20531 if (reg == reg2)
20532 reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
20534 if (reg == rreg)
20535 reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
20537 real = copy_rtx (PATTERN (insn));
20539 if (reg2 != NULL_RTX)
20540 real = replace_rtx (real, reg2, rreg);
20542 if (REGNO (reg) == STACK_POINTER_REGNUM)
20543 gcc_checking_assert (val == 0);
20544 else
20545 real = replace_rtx (real, reg,
20546 gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode,
20547 STACK_POINTER_REGNUM),
20548 GEN_INT (val)));
20550 /* We expect that 'real' is either a SET or a PARALLEL containing
20551 SETs (and possibly other stuff). In a PARALLEL, all the SETs
20552 are important so they all have to be marked RTX_FRAME_RELATED_P. */
20554 if (GET_CODE (real) == SET)
20556 rtx set = real;
20558 temp = simplify_rtx (SET_SRC (set));
20559 if (temp)
20560 SET_SRC (set) = temp;
20561 temp = simplify_rtx (SET_DEST (set));
20562 if (temp)
20563 SET_DEST (set) = temp;
20564 if (GET_CODE (SET_DEST (set)) == MEM)
20566 temp = simplify_rtx (XEXP (SET_DEST (set), 0));
20567 if (temp)
20568 XEXP (SET_DEST (set), 0) = temp;
20571 else
20573 int i;
20575 gcc_assert (GET_CODE (real) == PARALLEL);
20576 for (i = 0; i < XVECLEN (real, 0); i++)
20577 if (GET_CODE (XVECEXP (real, 0, i)) == SET)
20579 rtx set = XVECEXP (real, 0, i);
20581 temp = simplify_rtx (SET_SRC (set));
20582 if (temp)
20583 SET_SRC (set) = temp;
20584 temp = simplify_rtx (SET_DEST (set));
20585 if (temp)
20586 SET_DEST (set) = temp;
20587 if (GET_CODE (SET_DEST (set)) == MEM)
20589 temp = simplify_rtx (XEXP (SET_DEST (set), 0));
20590 if (temp)
20591 XEXP (SET_DEST (set), 0) = temp;
20593 RTX_FRAME_RELATED_P (set) = 1;
20597 RTX_FRAME_RELATED_P (insn) = 1;
20598 add_reg_note (insn, REG_FRAME_RELATED_EXPR, real);
20600 return insn;
20603 /* Returns an insn that has a vrsave set operation with the
20604 appropriate CLOBBERs. */
20606 static rtx
20607 generate_set_vrsave (rtx reg, rs6000_stack_t *info, int epiloguep)
20609 int nclobs, i;
20610 rtx insn, clobs[TOTAL_ALTIVEC_REGS + 1];
20611 rtx vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
20613 clobs[0]
20614 = gen_rtx_SET (VOIDmode,
20615 vrsave,
20616 gen_rtx_UNSPEC_VOLATILE (SImode,
20617 gen_rtvec (2, reg, vrsave),
20618 UNSPECV_SET_VRSAVE));
20620 nclobs = 1;
20622 /* We need to clobber the registers in the mask so the scheduler
20623 does not move sets to VRSAVE before sets of AltiVec registers.
20625 However, if the function receives nonlocal gotos, reload will set
20626 all call saved registers live. We will end up with:
20628 (set (reg 999) (mem))
20629 (parallel [ (set (reg vrsave) (unspec blah))
20630 (clobber (reg 999))])
20632 The clobber will cause the store into reg 999 to be dead, and
20633 flow will attempt to delete an epilogue insn. In this case, we
20634 need an unspec use/set of the register. */
20636 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
20637 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
20639 if (!epiloguep || call_used_regs [i])
20640 clobs[nclobs++] = gen_rtx_CLOBBER (VOIDmode,
20641 gen_rtx_REG (V4SImode, i));
20642 else
20644 rtx reg = gen_rtx_REG (V4SImode, i);
20646 clobs[nclobs++]
20647 = gen_rtx_SET (VOIDmode,
20648 reg,
20649 gen_rtx_UNSPEC (V4SImode,
20650 gen_rtvec (1, reg), 27));
20654 insn = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nclobs));
20656 for (i = 0; i < nclobs; ++i)
20657 XVECEXP (insn, 0, i) = clobs[i];
20659 return insn;
20662 static rtx
20663 gen_frame_set (rtx reg, rtx frame_reg, int offset, bool store)
20665 rtx addr, mem;
20667 addr = gen_rtx_PLUS (Pmode, frame_reg, GEN_INT (offset));
20668 mem = gen_frame_mem (GET_MODE (reg), addr);
20669 return gen_rtx_SET (VOIDmode, store ? mem : reg, store ? reg : mem);
20672 static rtx
20673 gen_frame_load (rtx reg, rtx frame_reg, int offset)
20675 return gen_frame_set (reg, frame_reg, offset, false);
20678 static rtx
20679 gen_frame_store (rtx reg, rtx frame_reg, int offset)
20681 return gen_frame_set (reg, frame_reg, offset, true);
20684 /* Save a register into the frame, and emit RTX_FRAME_RELATED_P notes.
20685 Save REGNO into [FRAME_REG + OFFSET] in mode MODE. */
20687 static rtx
20688 emit_frame_save (rtx frame_reg, enum machine_mode mode,
20689 unsigned int regno, int offset, HOST_WIDE_INT frame_reg_to_sp)
20691 rtx reg, insn;
20693 /* Some cases that need register indexed addressing. */
20694 gcc_checking_assert (!((TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
20695 || (TARGET_VSX && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
20696 || (TARGET_E500_DOUBLE && mode == DFmode)
20697 || (TARGET_SPE_ABI
20698 && SPE_VECTOR_MODE (mode)
20699 && !SPE_CONST_OFFSET_OK (offset))));
20701 reg = gen_rtx_REG (mode, regno);
20702 insn = emit_insn (gen_frame_store (reg, frame_reg, offset));
20703 return rs6000_frame_related (insn, frame_reg, frame_reg_to_sp,
20704 NULL_RTX, NULL_RTX);
20707 /* Emit an offset memory reference suitable for a frame store, while
20708 converting to a valid addressing mode. */
20710 static rtx
20711 gen_frame_mem_offset (enum machine_mode mode, rtx reg, int offset)
20713 rtx int_rtx, offset_rtx;
20715 int_rtx = GEN_INT (offset);
20717 if ((TARGET_SPE_ABI && SPE_VECTOR_MODE (mode) && !SPE_CONST_OFFSET_OK (offset))
20718 || (TARGET_E500_DOUBLE && mode == DFmode))
20720 offset_rtx = gen_rtx_REG (Pmode, FIXED_SCRATCH);
20721 emit_move_insn (offset_rtx, int_rtx);
20723 else
20724 offset_rtx = int_rtx;
20726 return gen_frame_mem (mode, gen_rtx_PLUS (Pmode, reg, offset_rtx));
20729 #ifndef TARGET_FIX_AND_CONTINUE
20730 #define TARGET_FIX_AND_CONTINUE 0
20731 #endif
20733 /* It's really GPR 13 or 14, FPR 14 and VR 20. We need the smallest. */
20734 #define FIRST_SAVRES_REGISTER FIRST_SAVED_GP_REGNO
20735 #define LAST_SAVRES_REGISTER 31
20736 #define N_SAVRES_REGISTERS (LAST_SAVRES_REGISTER - FIRST_SAVRES_REGISTER + 1)
20738 enum {
20739 SAVRES_LR = 0x1,
20740 SAVRES_SAVE = 0x2,
20741 SAVRES_REG = 0x0c,
20742 SAVRES_GPR = 0,
20743 SAVRES_FPR = 4,
20744 SAVRES_VR = 8
20747 static GTY(()) rtx savres_routine_syms[N_SAVRES_REGISTERS][12];
20749 /* Temporary holding space for an out-of-line register save/restore
20750 routine name. */
20751 static char savres_routine_name[30];
20753 /* Return the name for an out-of-line register save/restore routine.
20754 We are saving/restoring GPRs if GPR is true. */
20756 static char *
20757 rs6000_savres_routine_name (rs6000_stack_t *info, int regno, int sel)
20759 const char *prefix = "";
20760 const char *suffix = "";
20762 /* Different targets are supposed to define
20763 {SAVE,RESTORE}_FP_{PREFIX,SUFFIX} with the idea that the needed
20764 routine name could be defined with:
20766 sprintf (name, "%s%d%s", SAVE_FP_PREFIX, regno, SAVE_FP_SUFFIX)
20768 This is a nice idea in practice, but in reality, things are
20769 complicated in several ways:
20771 - ELF targets have save/restore routines for GPRs.
20773 - SPE targets use different prefixes for 32/64-bit registers, and
20774 neither of them fit neatly in the FOO_{PREFIX,SUFFIX} regimen.
20776 - PPC64 ELF targets have routines for save/restore of GPRs that
20777 differ in what they do with the link register, so having a set
20778 prefix doesn't work. (We only use one of the save routines at
20779 the moment, though.)
20781 - PPC32 elf targets have "exit" versions of the restore routines
20782 that restore the link register and can save some extra space.
20783 These require an extra suffix. (There are also "tail" versions
20784 of the restore routines and "GOT" versions of the save routines,
20785 but we don't generate those at present. Same problems apply,
20786 though.)
20788 We deal with all this by synthesizing our own prefix/suffix and
20789 using that for the simple sprintf call shown above. */
20790 if (TARGET_SPE)
20792 /* No floating point saves on the SPE. */
20793 gcc_assert ((sel & SAVRES_REG) == SAVRES_GPR);
20795 if ((sel & SAVRES_SAVE))
20796 prefix = info->spe_64bit_regs_used ? "_save64gpr_" : "_save32gpr_";
20797 else
20798 prefix = info->spe_64bit_regs_used ? "_rest64gpr_" : "_rest32gpr_";
20800 if ((sel & SAVRES_LR))
20801 suffix = "_x";
20803 else if (DEFAULT_ABI == ABI_V4)
20805 if (TARGET_64BIT)
20806 goto aix_names;
20808 if ((sel & SAVRES_REG) == SAVRES_GPR)
20809 prefix = (sel & SAVRES_SAVE) ? "_savegpr_" : "_restgpr_";
20810 else if ((sel & SAVRES_REG) == SAVRES_FPR)
20811 prefix = (sel & SAVRES_SAVE) ? "_savefpr_" : "_restfpr_";
20812 else if ((sel & SAVRES_REG) == SAVRES_VR)
20813 prefix = (sel & SAVRES_SAVE) ? "_savevr_" : "_restvr_";
20814 else
20815 abort ();
20817 if ((sel & SAVRES_LR))
20818 suffix = "_x";
20820 else if (DEFAULT_ABI == ABI_AIX)
20822 #if !defined (POWERPC_LINUX) && !defined (POWERPC_FREEBSD)
20823 /* No out-of-line save/restore routines for GPRs on AIX. */
20824 gcc_assert (!TARGET_AIX || (sel & SAVRES_REG) != SAVRES_GPR);
20825 #endif
20827 aix_names:
20828 if ((sel & SAVRES_REG) == SAVRES_GPR)
20829 prefix = ((sel & SAVRES_SAVE)
20830 ? ((sel & SAVRES_LR) ? "_savegpr0_" : "_savegpr1_")
20831 : ((sel & SAVRES_LR) ? "_restgpr0_" : "_restgpr1_"));
20832 else if ((sel & SAVRES_REG) == SAVRES_FPR)
20834 #if defined (POWERPC_LINUX) || defined (POWERPC_FREEBSD)
20835 if ((sel & SAVRES_LR))
20836 prefix = ((sel & SAVRES_SAVE) ? "_savefpr_" : "_restfpr_");
20837 else
20838 #endif
20840 prefix = (sel & SAVRES_SAVE) ? SAVE_FP_PREFIX : RESTORE_FP_PREFIX;
20841 suffix = (sel & SAVRES_SAVE) ? SAVE_FP_SUFFIX : RESTORE_FP_SUFFIX;
20844 else if ((sel & SAVRES_REG) == SAVRES_VR)
20845 prefix = (sel & SAVRES_SAVE) ? "_savevr_" : "_restvr_";
20846 else
20847 abort ();
20850 if (DEFAULT_ABI == ABI_DARWIN)
20852 /* The Darwin approach is (slightly) different, in order to be
20853 compatible with code generated by the system toolchain. There is a
20854 single symbol for the start of save sequence, and the code here
20855 embeds an offset into that code on the basis of the first register
20856 to be saved. */
20857 prefix = (sel & SAVRES_SAVE) ? "save" : "rest" ;
20858 if ((sel & SAVRES_REG) == SAVRES_GPR)
20859 sprintf (savres_routine_name, "*%sGPR%s%s%.0d ; %s r%d-r31", prefix,
20860 ((sel & SAVRES_LR) ? "x" : ""), (regno == 13 ? "" : "+"),
20861 (regno - 13) * 4, prefix, regno);
20862 else if ((sel & SAVRES_REG) == SAVRES_FPR)
20863 sprintf (savres_routine_name, "*%sFP%s%.0d ; %s f%d-f31", prefix,
20864 (regno == 14 ? "" : "+"), (regno - 14) * 4, prefix, regno);
20865 else if ((sel & SAVRES_REG) == SAVRES_VR)
20866 sprintf (savres_routine_name, "*%sVEC%s%.0d ; %s v%d-v31", prefix,
20867 (regno == 20 ? "" : "+"), (regno - 20) * 8, prefix, regno);
20868 else
20869 abort ();
20871 else
20872 sprintf (savres_routine_name, "%s%d%s", prefix, regno, suffix);
20874 return savres_routine_name;
20877 /* Return an RTL SYMBOL_REF for an out-of-line register save/restore routine.
20878 We are saving/restoring GPRs if GPR is true. */
20880 static rtx
20881 rs6000_savres_routine_sym (rs6000_stack_t *info, int sel)
20883 int regno = ((sel & SAVRES_REG) == SAVRES_GPR
20884 ? info->first_gp_reg_save
20885 : (sel & SAVRES_REG) == SAVRES_FPR
20886 ? info->first_fp_reg_save - 32
20887 : (sel & SAVRES_REG) == SAVRES_VR
20888 ? info->first_altivec_reg_save - FIRST_ALTIVEC_REGNO
20889 : -1);
20890 rtx sym;
20891 int select = sel;
20893 /* On the SPE, we never have any FPRs, but we do have 32/64-bit
20894 versions of the gpr routines. */
20895 if (TARGET_SPE_ABI && (sel & SAVRES_REG) == SAVRES_GPR
20896 && info->spe_64bit_regs_used)
20897 select ^= SAVRES_FPR ^ SAVRES_GPR;
20899 /* Don't generate bogus routine names. */
20900 gcc_assert (FIRST_SAVRES_REGISTER <= regno
20901 && regno <= LAST_SAVRES_REGISTER
20902 && select >= 0 && select <= 12);
20904 sym = savres_routine_syms[regno-FIRST_SAVRES_REGISTER][select];
20906 if (sym == NULL)
20908 char *name;
20910 name = rs6000_savres_routine_name (info, regno, sel);
20912 sym = savres_routine_syms[regno-FIRST_SAVRES_REGISTER][select]
20913 = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
20914 SYMBOL_REF_FLAGS (sym) |= SYMBOL_FLAG_FUNCTION;
20917 return sym;
20920 /* Emit a sequence of insns, including a stack tie if needed, for
20921 resetting the stack pointer. If UPDT_REGNO is not 1, then don't
20922 reset the stack pointer, but move the base of the frame into
20923 reg UPDT_REGNO for use by out-of-line register restore routines. */
20925 static rtx
20926 rs6000_emit_stack_reset (rs6000_stack_t *info,
20927 rtx frame_reg_rtx, HOST_WIDE_INT frame_off,
20928 unsigned updt_regno)
20930 rtx updt_reg_rtx;
20932 /* This blockage is needed so that sched doesn't decide to move
20933 the sp change before the register restores. */
20934 if (DEFAULT_ABI == ABI_V4
20935 || (TARGET_SPE_ABI
20936 && info->spe_64bit_regs_used != 0
20937 && info->first_gp_reg_save != 32))
20938 rs6000_emit_stack_tie (frame_reg_rtx, frame_pointer_needed);
20940 /* If we are restoring registers out-of-line, we will be using the
20941 "exit" variants of the restore routines, which will reset the
20942 stack for us. But we do need to point updt_reg into the
20943 right place for those routines. */
20944 updt_reg_rtx = gen_rtx_REG (Pmode, updt_regno);
20946 if (frame_off != 0)
20947 return emit_insn (gen_add3_insn (updt_reg_rtx,
20948 frame_reg_rtx, GEN_INT (frame_off)));
20949 else if (REGNO (frame_reg_rtx) != updt_regno)
20950 return emit_move_insn (updt_reg_rtx, frame_reg_rtx);
20952 return NULL_RTX;
20955 /* Return the register number used as a pointer by out-of-line
20956 save/restore functions. */
20958 static inline unsigned
20959 ptr_regno_for_savres (int sel)
20961 if (DEFAULT_ABI == ABI_AIX)
20962 return (sel & SAVRES_REG) == SAVRES_FPR || (sel & SAVRES_LR) ? 1 : 12;
20963 return DEFAULT_ABI == ABI_DARWIN && (sel & SAVRES_REG) == SAVRES_FPR ? 1 : 11;
20966 /* Construct a parallel rtx describing the effect of a call to an
20967 out-of-line register save/restore routine, and emit the insn
20968 or jump_insn as appropriate. */
20970 static rtx
20971 rs6000_emit_savres_rtx (rs6000_stack_t *info,
20972 rtx frame_reg_rtx, int save_area_offset, int lr_offset,
20973 enum machine_mode reg_mode, int sel)
20975 int i;
20976 int offset, start_reg, end_reg, n_regs, use_reg;
20977 int reg_size = GET_MODE_SIZE (reg_mode);
20978 rtx sym;
20979 rtvec p;
20980 rtx par, insn;
20982 offset = 0;
20983 start_reg = ((sel & SAVRES_REG) == SAVRES_GPR
20984 ? info->first_gp_reg_save
20985 : (sel & SAVRES_REG) == SAVRES_FPR
20986 ? info->first_fp_reg_save
20987 : (sel & SAVRES_REG) == SAVRES_VR
20988 ? info->first_altivec_reg_save
20989 : -1);
20990 end_reg = ((sel & SAVRES_REG) == SAVRES_GPR
20991 ? 32
20992 : (sel & SAVRES_REG) == SAVRES_FPR
20993 ? 64
20994 : (sel & SAVRES_REG) == SAVRES_VR
20995 ? LAST_ALTIVEC_REGNO + 1
20996 : -1);
20997 n_regs = end_reg - start_reg;
20998 p = rtvec_alloc (3 + ((sel & SAVRES_LR) ? 1 : 0)
20999 + ((sel & SAVRES_REG) == SAVRES_VR ? 1 : 0)
21000 + n_regs);
21002 if (!(sel & SAVRES_SAVE) && (sel & SAVRES_LR))
21003 RTVEC_ELT (p, offset++) = ret_rtx;
21005 RTVEC_ELT (p, offset++)
21006 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, LR_REGNO));
21008 sym = rs6000_savres_routine_sym (info, sel);
21009 RTVEC_ELT (p, offset++) = gen_rtx_USE (VOIDmode, sym);
21011 use_reg = ptr_regno_for_savres (sel);
21012 if ((sel & SAVRES_REG) == SAVRES_VR)
21014 /* Vector regs are saved/restored using [reg+reg] addressing. */
21015 RTVEC_ELT (p, offset++)
21016 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, use_reg));
21017 RTVEC_ELT (p, offset++)
21018 = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, 0));
21020 else
21021 RTVEC_ELT (p, offset++)
21022 = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, use_reg));
21024 for (i = 0; i < end_reg - start_reg; i++)
21025 RTVEC_ELT (p, i + offset)
21026 = gen_frame_set (gen_rtx_REG (reg_mode, start_reg + i),
21027 frame_reg_rtx, save_area_offset + reg_size * i,
21028 (sel & SAVRES_SAVE) != 0);
21030 if ((sel & SAVRES_SAVE) && (sel & SAVRES_LR))
21031 RTVEC_ELT (p, i + offset)
21032 = gen_frame_store (gen_rtx_REG (Pmode, 0), frame_reg_rtx, lr_offset);
21034 par = gen_rtx_PARALLEL (VOIDmode, p);
21036 if (!(sel & SAVRES_SAVE) && (sel & SAVRES_LR))
21038 insn = emit_jump_insn (par);
21039 JUMP_LABEL (insn) = ret_rtx;
21041 else
21042 insn = emit_insn (par);
21043 return insn;
21046 /* Determine whether the gp REG is really used. */
21048 static bool
21049 rs6000_reg_live_or_pic_offset_p (int reg)
21051 /* If the function calls eh_return, claim used all the registers that would
21052 be checked for liveness otherwise. This is required for the PIC offset
21053 register with -mminimal-toc on AIX, as it is advertised as "fixed" for
21054 register allocation purposes in this case. */
21056 return (((crtl->calls_eh_return || df_regs_ever_live_p (reg))
21057 && (!call_used_regs[reg]
21058 || (reg == RS6000_PIC_OFFSET_TABLE_REGNUM
21059 && !TARGET_SINGLE_PIC_BASE
21060 && TARGET_TOC && TARGET_MINIMAL_TOC)))
21061 || (reg == RS6000_PIC_OFFSET_TABLE_REGNUM
21062 && !TARGET_SINGLE_PIC_BASE
21063 && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
21064 || (DEFAULT_ABI == ABI_DARWIN && flag_pic))));
21067 /* Emit function prologue as insns. */
21069 void
21070 rs6000_emit_prologue (void)
21072 rs6000_stack_t *info = rs6000_stack_info ();
21073 enum machine_mode reg_mode = Pmode;
21074 int reg_size = TARGET_32BIT ? 4 : 8;
21075 rtx sp_reg_rtx = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
21076 rtx frame_reg_rtx = sp_reg_rtx;
21077 unsigned int cr_save_regno;
21078 rtx cr_save_rtx = NULL_RTX;
21079 rtx insn;
21080 int strategy;
21081 int using_static_chain_p = (cfun->static_chain_decl != NULL_TREE
21082 && df_regs_ever_live_p (STATIC_CHAIN_REGNUM)
21083 && call_used_regs[STATIC_CHAIN_REGNUM]);
21084 /* Offset to top of frame for frame_reg and sp respectively. */
21085 HOST_WIDE_INT frame_off = 0;
21086 HOST_WIDE_INT sp_off = 0;
21088 #ifdef ENABLE_CHECKING
21089 /* Track and check usage of r0, r11, r12. */
21090 int reg_inuse = using_static_chain_p ? 1 << 11 : 0;
21091 #define START_USE(R) do \
21093 gcc_assert ((reg_inuse & (1 << (R))) == 0); \
21094 reg_inuse |= 1 << (R); \
21095 } while (0)
21096 #define END_USE(R) do \
21098 gcc_assert ((reg_inuse & (1 << (R))) != 0); \
21099 reg_inuse &= ~(1 << (R)); \
21100 } while (0)
21101 #define NOT_INUSE(R) do \
21103 gcc_assert ((reg_inuse & (1 << (R))) == 0); \
21104 } while (0)
21105 #else
21106 #define START_USE(R) do {} while (0)
21107 #define END_USE(R) do {} while (0)
21108 #define NOT_INUSE(R) do {} while (0)
21109 #endif
21111 if (flag_stack_usage_info)
21112 current_function_static_stack_size = info->total_size;
21114 if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK && info->total_size)
21115 rs6000_emit_probe_stack_range (STACK_CHECK_PROTECT, info->total_size);
21117 if (TARGET_FIX_AND_CONTINUE)
21119 /* gdb on darwin arranges to forward a function from the old
21120 address by modifying the first 5 instructions of the function
21121 to branch to the overriding function. This is necessary to
21122 permit function pointers that point to the old function to
21123 actually forward to the new function. */
21124 emit_insn (gen_nop ());
21125 emit_insn (gen_nop ());
21126 emit_insn (gen_nop ());
21127 emit_insn (gen_nop ());
21128 emit_insn (gen_nop ());
21131 if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
21133 reg_mode = V2SImode;
21134 reg_size = 8;
21137 /* Handle world saves specially here. */
21138 if (WORLD_SAVE_P (info))
21140 int i, j, sz;
21141 rtx treg;
21142 rtvec p;
21143 rtx reg0;
21145 /* save_world expects lr in r0. */
21146 reg0 = gen_rtx_REG (Pmode, 0);
21147 if (info->lr_save_p)
21149 insn = emit_move_insn (reg0,
21150 gen_rtx_REG (Pmode, LR_REGNO));
21151 RTX_FRAME_RELATED_P (insn) = 1;
21154 /* The SAVE_WORLD and RESTORE_WORLD routines make a number of
21155 assumptions about the offsets of various bits of the stack
21156 frame. */
21157 gcc_assert (info->gp_save_offset == -220
21158 && info->fp_save_offset == -144
21159 && info->lr_save_offset == 8
21160 && info->cr_save_offset == 4
21161 && info->push_p
21162 && info->lr_save_p
21163 && (!crtl->calls_eh_return
21164 || info->ehrd_offset == -432)
21165 && info->vrsave_save_offset == -224
21166 && info->altivec_save_offset == -416);
21168 treg = gen_rtx_REG (SImode, 11);
21169 emit_move_insn (treg, GEN_INT (-info->total_size));
21171 /* SAVE_WORLD takes the caller's LR in R0 and the frame size
21172 in R11. It also clobbers R12, so beware! */
21174 /* Preserve CR2 for save_world prologues */
21175 sz = 5;
21176 sz += 32 - info->first_gp_reg_save;
21177 sz += 64 - info->first_fp_reg_save;
21178 sz += LAST_ALTIVEC_REGNO - info->first_altivec_reg_save + 1;
21179 p = rtvec_alloc (sz);
21180 j = 0;
21181 RTVEC_ELT (p, j++) = gen_rtx_CLOBBER (VOIDmode,
21182 gen_rtx_REG (SImode,
21183 LR_REGNO));
21184 RTVEC_ELT (p, j++) = gen_rtx_USE (VOIDmode,
21185 gen_rtx_SYMBOL_REF (Pmode,
21186 "*save_world"));
21187 /* We do floats first so that the instruction pattern matches
21188 properly. */
21189 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
21190 RTVEC_ELT (p, j++)
21191 = gen_frame_store (gen_rtx_REG (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
21192 ? DFmode : SFmode,
21193 info->first_fp_reg_save + i),
21194 frame_reg_rtx,
21195 info->fp_save_offset + frame_off + 8 * i);
21196 for (i = 0; info->first_altivec_reg_save + i <= LAST_ALTIVEC_REGNO; i++)
21197 RTVEC_ELT (p, j++)
21198 = gen_frame_store (gen_rtx_REG (V4SImode,
21199 info->first_altivec_reg_save + i),
21200 frame_reg_rtx,
21201 info->altivec_save_offset + frame_off + 16 * i);
21202 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
21203 RTVEC_ELT (p, j++)
21204 = gen_frame_store (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
21205 frame_reg_rtx,
21206 info->gp_save_offset + frame_off + reg_size * i);
21208 /* CR register traditionally saved as CR2. */
21209 RTVEC_ELT (p, j++)
21210 = gen_frame_store (gen_rtx_REG (SImode, CR2_REGNO),
21211 frame_reg_rtx, info->cr_save_offset + frame_off);
21212 /* Explain about use of R0. */
21213 if (info->lr_save_p)
21214 RTVEC_ELT (p, j++)
21215 = gen_frame_store (reg0,
21216 frame_reg_rtx, info->lr_save_offset + frame_off);
21217 /* Explain what happens to the stack pointer. */
21219 rtx newval = gen_rtx_PLUS (Pmode, sp_reg_rtx, treg);
21220 RTVEC_ELT (p, j++) = gen_rtx_SET (VOIDmode, sp_reg_rtx, newval);
21223 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
21224 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
21225 treg, GEN_INT (-info->total_size));
21226 sp_off = frame_off = info->total_size;
21229 strategy = info->savres_strategy;
21231 /* For V.4, update stack before we do any saving and set back pointer. */
21232 if (! WORLD_SAVE_P (info)
21233 && info->push_p
21234 && (DEFAULT_ABI == ABI_V4
21235 || crtl->calls_eh_return))
21237 bool need_r11 = (TARGET_SPE
21238 ? (!(strategy & SAVE_INLINE_GPRS)
21239 && info->spe_64bit_regs_used == 0)
21240 : (!(strategy & SAVE_INLINE_FPRS)
21241 || !(strategy & SAVE_INLINE_GPRS)
21242 || !(strategy & SAVE_INLINE_VRS)));
21243 int ptr_regno = -1;
21244 rtx ptr_reg = NULL_RTX;
21245 int ptr_off = 0;
21247 if (info->total_size < 32767)
21248 frame_off = info->total_size;
21249 else if (need_r11)
21250 ptr_regno = 11;
21251 else if (info->cr_save_p
21252 || info->lr_save_p
21253 || info->first_fp_reg_save < 64
21254 || info->first_gp_reg_save < 32
21255 || info->altivec_size != 0
21256 || info->vrsave_mask != 0
21257 || crtl->calls_eh_return)
21258 ptr_regno = 12;
21259 else
21261 /* The prologue won't be saving any regs so there is no need
21262 to set up a frame register to access any frame save area.
21263 We also won't be using frame_off anywhere below, but set
21264 the correct value anyway to protect against future
21265 changes to this function. */
21266 frame_off = info->total_size;
21268 if (ptr_regno != -1)
21270 /* Set up the frame offset to that needed by the first
21271 out-of-line save function. */
21272 START_USE (ptr_regno);
21273 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
21274 frame_reg_rtx = ptr_reg;
21275 if (!(strategy & SAVE_INLINE_FPRS) && info->fp_size != 0)
21276 gcc_checking_assert (info->fp_save_offset + info->fp_size == 0);
21277 else if (!(strategy & SAVE_INLINE_GPRS) && info->first_gp_reg_save < 32)
21278 ptr_off = info->gp_save_offset + info->gp_size;
21279 else if (!(strategy & SAVE_INLINE_VRS) && info->altivec_size != 0)
21280 ptr_off = info->altivec_save_offset + info->altivec_size;
21281 frame_off = -ptr_off;
21283 rs6000_emit_allocate_stack (info->total_size, ptr_reg, ptr_off);
21284 sp_off = info->total_size;
21285 if (frame_reg_rtx != sp_reg_rtx)
21286 rs6000_emit_stack_tie (frame_reg_rtx, false);
21289 /* If we use the link register, get it into r0. */
21290 if (!WORLD_SAVE_P (info) && info->lr_save_p)
21292 rtx addr, reg, mem;
21294 reg = gen_rtx_REG (Pmode, 0);
21295 START_USE (0);
21296 insn = emit_move_insn (reg, gen_rtx_REG (Pmode, LR_REGNO));
21297 RTX_FRAME_RELATED_P (insn) = 1;
21299 if (!(strategy & (SAVE_NOINLINE_GPRS_SAVES_LR
21300 | SAVE_NOINLINE_FPRS_SAVES_LR)))
21302 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
21303 GEN_INT (info->lr_save_offset + frame_off));
21304 mem = gen_rtx_MEM (Pmode, addr);
21305 /* This should not be of rs6000_sr_alias_set, because of
21306 __builtin_return_address. */
21308 insn = emit_move_insn (mem, reg);
21309 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
21310 NULL_RTX, NULL_RTX);
21311 END_USE (0);
21315 /* If we need to save CR, put it into r12 or r11. Choose r12 except when
21316 r12 will be needed by out-of-line gpr restore. */
21317 cr_save_regno = (DEFAULT_ABI == ABI_AIX
21318 && !(strategy & (SAVE_INLINE_GPRS
21319 | SAVE_NOINLINE_GPRS_SAVES_LR))
21320 ? 11 : 12);
21321 if (!WORLD_SAVE_P (info)
21322 && info->cr_save_p
21323 && REGNO (frame_reg_rtx) != cr_save_regno
21324 && !(using_static_chain_p && cr_save_regno == 11))
21326 rtx set;
21328 cr_save_rtx = gen_rtx_REG (SImode, cr_save_regno);
21329 START_USE (cr_save_regno);
21330 insn = emit_insn (gen_movesi_from_cr (cr_save_rtx));
21331 RTX_FRAME_RELATED_P (insn) = 1;
21332 /* Now, there's no way that dwarf2out_frame_debug_expr is going
21333 to understand '(unspec:SI [(reg:CC 68) ...] UNSPEC_MOVESI_FROM_CR)'.
21334 But that's OK. All we have to do is specify that _one_ condition
21335 code register is saved in this stack slot. The thrower's epilogue
21336 will then restore all the call-saved registers.
21337 We use CR2_REGNO (70) to be compatible with gcc-2.95 on Linux. */
21338 set = gen_rtx_SET (VOIDmode, cr_save_rtx,
21339 gen_rtx_REG (SImode, CR2_REGNO));
21340 add_reg_note (insn, REG_FRAME_RELATED_EXPR, set);
21343 /* Do any required saving of fpr's. If only one or two to save, do
21344 it ourselves. Otherwise, call function. */
21345 if (!WORLD_SAVE_P (info) && (strategy & SAVE_INLINE_FPRS))
21347 int i;
21348 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
21349 if (save_reg_p (info->first_fp_reg_save + i))
21350 emit_frame_save (frame_reg_rtx,
21351 (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
21352 ? DFmode : SFmode),
21353 info->first_fp_reg_save + i,
21354 info->fp_save_offset + frame_off + 8 * i,
21355 sp_off - frame_off);
21357 else if (!WORLD_SAVE_P (info) && info->first_fp_reg_save != 64)
21359 bool lr = (strategy & SAVE_NOINLINE_FPRS_SAVES_LR) != 0;
21360 int sel = SAVRES_SAVE | SAVRES_FPR | (lr ? SAVRES_LR : 0);
21361 unsigned ptr_regno = ptr_regno_for_savres (sel);
21362 rtx ptr_reg = frame_reg_rtx;
21364 if (REGNO (frame_reg_rtx) == ptr_regno)
21365 gcc_checking_assert (frame_off == 0);
21366 else
21368 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
21369 NOT_INUSE (ptr_regno);
21370 emit_insn (gen_add3_insn (ptr_reg,
21371 frame_reg_rtx, GEN_INT (frame_off)));
21373 insn = rs6000_emit_savres_rtx (info, ptr_reg,
21374 info->fp_save_offset,
21375 info->lr_save_offset,
21376 DFmode, sel);
21377 rs6000_frame_related (insn, ptr_reg, sp_off,
21378 NULL_RTX, NULL_RTX);
21379 if (lr)
21380 END_USE (0);
21383 /* Save GPRs. This is done as a PARALLEL if we are using
21384 the store-multiple instructions. */
21385 if (!WORLD_SAVE_P (info)
21386 && TARGET_SPE_ABI
21387 && info->spe_64bit_regs_used != 0
21388 && info->first_gp_reg_save != 32)
21390 int i;
21391 rtx spe_save_area_ptr;
21392 HOST_WIDE_INT save_off;
21393 int ool_adjust = 0;
21395 /* Determine whether we can address all of the registers that need
21396 to be saved with an offset from frame_reg_rtx that fits in
21397 the small const field for SPE memory instructions. */
21398 int spe_regs_addressable
21399 = (SPE_CONST_OFFSET_OK (info->spe_gp_save_offset + frame_off
21400 + reg_size * (32 - info->first_gp_reg_save - 1))
21401 && (strategy & SAVE_INLINE_GPRS));
21403 if (spe_regs_addressable)
21405 spe_save_area_ptr = frame_reg_rtx;
21406 save_off = frame_off;
21408 else
21410 /* Make r11 point to the start of the SPE save area. We need
21411 to be careful here if r11 is holding the static chain. If
21412 it is, then temporarily save it in r0. */
21413 HOST_WIDE_INT offset;
21415 if (!(strategy & SAVE_INLINE_GPRS))
21416 ool_adjust = 8 * (info->first_gp_reg_save
21417 - (FIRST_SAVRES_REGISTER + 1));
21418 offset = info->spe_gp_save_offset + frame_off - ool_adjust;
21419 spe_save_area_ptr = gen_rtx_REG (Pmode, 11);
21420 save_off = frame_off - offset;
21422 if (using_static_chain_p)
21424 rtx r0 = gen_rtx_REG (Pmode, 0);
21426 START_USE (0);
21427 gcc_assert (info->first_gp_reg_save > 11);
21429 emit_move_insn (r0, spe_save_area_ptr);
21431 else if (REGNO (frame_reg_rtx) != 11)
21432 START_USE (11);
21434 emit_insn (gen_addsi3 (spe_save_area_ptr,
21435 frame_reg_rtx, GEN_INT (offset)));
21436 if (!using_static_chain_p && REGNO (frame_reg_rtx) == 11)
21437 frame_off = -info->spe_gp_save_offset + ool_adjust;
21440 if ((strategy & SAVE_INLINE_GPRS))
21442 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
21443 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
21444 emit_frame_save (spe_save_area_ptr, reg_mode,
21445 info->first_gp_reg_save + i,
21446 (info->spe_gp_save_offset + save_off
21447 + reg_size * i),
21448 sp_off - save_off);
21450 else
21452 insn = rs6000_emit_savres_rtx (info, spe_save_area_ptr,
21453 info->spe_gp_save_offset + save_off,
21454 0, reg_mode,
21455 SAVRES_SAVE | SAVRES_GPR);
21457 rs6000_frame_related (insn, spe_save_area_ptr, sp_off - save_off,
21458 NULL_RTX, NULL_RTX);
21461 /* Move the static chain pointer back. */
21462 if (!spe_regs_addressable)
21464 if (using_static_chain_p)
21466 emit_move_insn (spe_save_area_ptr, gen_rtx_REG (Pmode, 0));
21467 END_USE (0);
21469 else if (REGNO (frame_reg_rtx) != 11)
21470 END_USE (11);
21473 else if (!WORLD_SAVE_P (info) && !(strategy & SAVE_INLINE_GPRS))
21475 bool lr = (strategy & SAVE_NOINLINE_GPRS_SAVES_LR) != 0;
21476 int sel = SAVRES_SAVE | SAVRES_GPR | (lr ? SAVRES_LR : 0);
21477 unsigned ptr_regno = ptr_regno_for_savres (sel);
21478 rtx ptr_reg = frame_reg_rtx;
21479 bool ptr_set_up = REGNO (ptr_reg) == ptr_regno;
21480 int end_save = info->gp_save_offset + info->gp_size;
21481 int ptr_off;
21483 if (!ptr_set_up)
21484 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
21486 /* Need to adjust r11 (r12) if we saved any FPRs. */
21487 if (end_save + frame_off != 0)
21489 rtx offset = GEN_INT (end_save + frame_off);
21491 if (ptr_set_up)
21492 frame_off = -end_save;
21493 else
21494 NOT_INUSE (ptr_regno);
21495 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
21497 else if (!ptr_set_up)
21499 NOT_INUSE (ptr_regno);
21500 emit_move_insn (ptr_reg, frame_reg_rtx);
21502 ptr_off = -end_save;
21503 insn = rs6000_emit_savres_rtx (info, ptr_reg,
21504 info->gp_save_offset + ptr_off,
21505 info->lr_save_offset + ptr_off,
21506 reg_mode, sel);
21507 rs6000_frame_related (insn, ptr_reg, sp_off - ptr_off,
21508 NULL_RTX, NULL_RTX);
21509 if (lr)
21510 END_USE (0);
21512 else if (!WORLD_SAVE_P (info) && (strategy & SAVRES_MULTIPLE))
21514 rtvec p;
21515 int i;
21516 p = rtvec_alloc (32 - info->first_gp_reg_save);
21517 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
21518 RTVEC_ELT (p, i)
21519 = gen_frame_store (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
21520 frame_reg_rtx,
21521 info->gp_save_offset + frame_off + reg_size * i);
21522 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
21523 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
21524 NULL_RTX, NULL_RTX);
21526 else if (!WORLD_SAVE_P (info))
21528 int i;
21529 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
21530 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
21531 emit_frame_save (frame_reg_rtx, reg_mode,
21532 info->first_gp_reg_save + i,
21533 info->gp_save_offset + frame_off + reg_size * i,
21534 sp_off - frame_off);
21537 if (crtl->calls_eh_return)
21539 unsigned int i;
21540 rtvec p;
21542 for (i = 0; ; ++i)
21544 unsigned int regno = EH_RETURN_DATA_REGNO (i);
21545 if (regno == INVALID_REGNUM)
21546 break;
21549 p = rtvec_alloc (i);
21551 for (i = 0; ; ++i)
21553 unsigned int regno = EH_RETURN_DATA_REGNO (i);
21554 if (regno == INVALID_REGNUM)
21555 break;
21557 insn
21558 = gen_frame_store (gen_rtx_REG (reg_mode, regno),
21559 sp_reg_rtx,
21560 info->ehrd_offset + sp_off + reg_size * (int) i);
21561 RTVEC_ELT (p, i) = insn;
21562 RTX_FRAME_RELATED_P (insn) = 1;
21565 insn = emit_insn (gen_blockage ());
21566 RTX_FRAME_RELATED_P (insn) = 1;
21567 add_reg_note (insn, REG_FRAME_RELATED_EXPR, gen_rtx_PARALLEL (VOIDmode, p));
21570 /* In AIX ABI we need to make sure r2 is really saved. */
21571 if (TARGET_AIX && crtl->calls_eh_return)
21573 rtx tmp_reg, tmp_reg_si, hi, lo, compare_result, toc_save_done, jump;
21574 rtx save_insn, join_insn, note;
21575 long toc_restore_insn;
21577 tmp_reg = gen_rtx_REG (Pmode, 11);
21578 tmp_reg_si = gen_rtx_REG (SImode, 11);
21579 if (using_static_chain_p)
21581 START_USE (0);
21582 emit_move_insn (gen_rtx_REG (Pmode, 0), tmp_reg);
21584 else
21585 START_USE (11);
21586 emit_move_insn (tmp_reg, gen_rtx_REG (Pmode, LR_REGNO));
21587 /* Peek at instruction to which this function returns. If it's
21588 restoring r2, then we know we've already saved r2. We can't
21589 unconditionally save r2 because the value we have will already
21590 be updated if we arrived at this function via a plt call or
21591 toc adjusting stub. */
21592 emit_move_insn (tmp_reg_si, gen_rtx_MEM (SImode, tmp_reg));
21593 toc_restore_insn = TARGET_32BIT ? 0x80410014 : 0xE8410028;
21594 hi = gen_int_mode (toc_restore_insn & ~0xffff, SImode);
21595 emit_insn (gen_xorsi3 (tmp_reg_si, tmp_reg_si, hi));
21596 compare_result = gen_rtx_REG (CCUNSmode, CR0_REGNO);
21597 validate_condition_mode (EQ, CCUNSmode);
21598 lo = gen_int_mode (toc_restore_insn & 0xffff, SImode);
21599 emit_insn (gen_rtx_SET (VOIDmode, compare_result,
21600 gen_rtx_COMPARE (CCUNSmode, tmp_reg_si, lo)));
21601 toc_save_done = gen_label_rtx ();
21602 jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
21603 gen_rtx_EQ (VOIDmode, compare_result,
21604 const0_rtx),
21605 gen_rtx_LABEL_REF (VOIDmode, toc_save_done),
21606 pc_rtx);
21607 jump = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, jump));
21608 JUMP_LABEL (jump) = toc_save_done;
21609 LABEL_NUSES (toc_save_done) += 1;
21611 save_insn = emit_frame_save (frame_reg_rtx, reg_mode,
21612 TOC_REGNUM, frame_off + 5 * reg_size,
21613 sp_off - frame_off);
21615 emit_label (toc_save_done);
21617 /* ??? If we leave SAVE_INSN as marked as saving R2, then we'll
21618 have a CFG that has different saves along different paths.
21619 Move the note to a dummy blockage insn, which describes that
21620 R2 is unconditionally saved after the label. */
21621 /* ??? An alternate representation might be a special insn pattern
21622 containing both the branch and the store. That might let the
21623 code that minimizes the number of DW_CFA_advance opcodes better
21624 freedom in placing the annotations. */
21625 note = find_reg_note (save_insn, REG_FRAME_RELATED_EXPR, NULL);
21626 if (note)
21627 remove_note (save_insn, note);
21628 else
21629 note = alloc_reg_note (REG_FRAME_RELATED_EXPR,
21630 copy_rtx (PATTERN (save_insn)), NULL_RTX);
21631 RTX_FRAME_RELATED_P (save_insn) = 0;
21633 join_insn = emit_insn (gen_blockage ());
21634 REG_NOTES (join_insn) = note;
21635 RTX_FRAME_RELATED_P (join_insn) = 1;
21637 if (using_static_chain_p)
21639 emit_move_insn (tmp_reg, gen_rtx_REG (Pmode, 0));
21640 END_USE (0);
21642 else
21643 END_USE (11);
21646 /* Save CR if we use any that must be preserved. */
21647 if (!WORLD_SAVE_P (info) && info->cr_save_p)
21649 rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
21650 GEN_INT (info->cr_save_offset + frame_off));
21651 rtx mem = gen_frame_mem (SImode, addr);
21652 /* See the large comment above about why CR2_REGNO is used. */
21653 rtx magic_eh_cr_reg = gen_rtx_REG (SImode, CR2_REGNO);
21655 /* If we didn't copy cr before, do so now using r0. */
21656 if (cr_save_rtx == NULL_RTX)
21658 rtx set;
21660 START_USE (0);
21661 cr_save_rtx = gen_rtx_REG (SImode, 0);
21662 insn = emit_insn (gen_movesi_from_cr (cr_save_rtx));
21663 RTX_FRAME_RELATED_P (insn) = 1;
21664 set = gen_rtx_SET (VOIDmode, cr_save_rtx, magic_eh_cr_reg);
21665 add_reg_note (insn, REG_FRAME_RELATED_EXPR, set);
21667 insn = emit_move_insn (mem, cr_save_rtx);
21668 END_USE (REGNO (cr_save_rtx));
21670 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
21671 NULL_RTX, NULL_RTX);
21674 /* Update stack and set back pointer unless this is V.4,
21675 for which it was done previously. */
21676 if (!WORLD_SAVE_P (info) && info->push_p
21677 && !(DEFAULT_ABI == ABI_V4 || crtl->calls_eh_return))
21679 rtx ptr_reg = NULL;
21680 int ptr_off = 0;
21682 /* If saving altivec regs we need to be able to address all save
21683 locations using a 16-bit offset. */
21684 if ((strategy & SAVE_INLINE_VRS) == 0
21685 || (info->altivec_size != 0
21686 && (info->altivec_save_offset + info->altivec_size - 16
21687 + info->total_size - frame_off) > 32767)
21688 || (info->vrsave_size != 0
21689 && (info->vrsave_save_offset
21690 + info->total_size - frame_off) > 32767))
21692 int sel = SAVRES_SAVE | SAVRES_VR;
21693 unsigned ptr_regno = ptr_regno_for_savres (sel);
21695 if (using_static_chain_p
21696 && ptr_regno == STATIC_CHAIN_REGNUM)
21697 ptr_regno = 12;
21698 if (REGNO (frame_reg_rtx) != ptr_regno)
21699 START_USE (ptr_regno);
21700 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
21701 frame_reg_rtx = ptr_reg;
21702 ptr_off = info->altivec_save_offset + info->altivec_size;
21703 frame_off = -ptr_off;
21705 else if (REGNO (frame_reg_rtx) == 1)
21706 frame_off = info->total_size;
21707 rs6000_emit_allocate_stack (info->total_size, ptr_reg, ptr_off);
21708 sp_off = info->total_size;
21709 if (frame_reg_rtx != sp_reg_rtx)
21710 rs6000_emit_stack_tie (frame_reg_rtx, false);
21713 /* Set frame pointer, if needed. */
21714 if (frame_pointer_needed)
21716 insn = emit_move_insn (gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
21717 sp_reg_rtx);
21718 RTX_FRAME_RELATED_P (insn) = 1;
21721 /* Save AltiVec registers if needed. Save here because the red zone does
21722 not always include AltiVec registers. */
21723 if (!WORLD_SAVE_P (info) && TARGET_ALTIVEC_ABI
21724 && info->altivec_size != 0 && (strategy & SAVE_INLINE_VRS) == 0)
21726 int end_save = info->altivec_save_offset + info->altivec_size;
21727 int ptr_off;
21728 /* Oddly, the vector save/restore functions point r0 at the end
21729 of the save area, then use r11 or r12 to load offsets for
21730 [reg+reg] addressing. */
21731 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
21732 int scratch_regno = ptr_regno_for_savres (SAVRES_SAVE | SAVRES_VR);
21733 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
21735 gcc_checking_assert (scratch_regno == 11 || scratch_regno == 12);
21736 NOT_INUSE (0);
21737 if (end_save + frame_off != 0)
21739 rtx offset = GEN_INT (end_save + frame_off);
21741 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
21743 else
21744 emit_move_insn (ptr_reg, frame_reg_rtx);
21746 ptr_off = -end_save;
21747 insn = rs6000_emit_savres_rtx (info, scratch_reg,
21748 info->altivec_save_offset + ptr_off,
21749 0, V4SImode, SAVRES_SAVE | SAVRES_VR);
21750 rs6000_frame_related (insn, scratch_reg, sp_off - ptr_off,
21751 NULL_RTX, NULL_RTX);
21752 if (REGNO (frame_reg_rtx) == REGNO (scratch_reg))
21754 /* The oddity mentioned above clobbered our frame reg. */
21755 emit_move_insn (frame_reg_rtx, ptr_reg);
21756 frame_off = ptr_off;
21759 else if (!WORLD_SAVE_P (info) && TARGET_ALTIVEC_ABI
21760 && info->altivec_size != 0)
21762 int i;
21764 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
21765 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
21767 rtx areg, savereg, mem;
21768 int offset;
21770 offset = (info->altivec_save_offset + frame_off
21771 + 16 * (i - info->first_altivec_reg_save));
21773 savereg = gen_rtx_REG (V4SImode, i);
21775 NOT_INUSE (0);
21776 areg = gen_rtx_REG (Pmode, 0);
21777 emit_move_insn (areg, GEN_INT (offset));
21779 /* AltiVec addressing mode is [reg+reg]. */
21780 mem = gen_frame_mem (V4SImode,
21781 gen_rtx_PLUS (Pmode, frame_reg_rtx, areg));
21783 insn = emit_move_insn (mem, savereg);
21785 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
21786 areg, GEN_INT (offset));
21790 /* VRSAVE is a bit vector representing which AltiVec registers
21791 are used. The OS uses this to determine which vector
21792 registers to save on a context switch. We need to save
21793 VRSAVE on the stack frame, add whatever AltiVec registers we
21794 used in this function, and do the corresponding magic in the
21795 epilogue. */
21797 if (!WORLD_SAVE_P (info)
21798 && TARGET_ALTIVEC
21799 && TARGET_ALTIVEC_VRSAVE
21800 && info->vrsave_mask != 0)
21802 rtx reg, vrsave;
21803 int offset;
21804 int save_regno;
21806 /* Get VRSAVE onto a GPR. Note that ABI_V4 and ABI_DARWIN might
21807 be using r12 as frame_reg_rtx and r11 as the static chain
21808 pointer for nested functions. */
21809 save_regno = 12;
21810 if (DEFAULT_ABI == ABI_AIX && !using_static_chain_p)
21811 save_regno = 11;
21812 else if (REGNO (frame_reg_rtx) == 12)
21814 save_regno = 11;
21815 if (using_static_chain_p)
21816 save_regno = 0;
21819 NOT_INUSE (save_regno);
21820 reg = gen_rtx_REG (SImode, save_regno);
21821 vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
21822 if (TARGET_MACHO)
21823 emit_insn (gen_get_vrsave_internal (reg));
21824 else
21825 emit_insn (gen_rtx_SET (VOIDmode, reg, vrsave));
21827 /* Save VRSAVE. */
21828 offset = info->vrsave_save_offset + frame_off;
21829 insn = emit_insn (gen_frame_store (reg, frame_reg_rtx, offset));
21831 /* Include the registers in the mask. */
21832 emit_insn (gen_iorsi3 (reg, reg, GEN_INT ((int) info->vrsave_mask)));
21834 insn = emit_insn (generate_set_vrsave (reg, info, 0));
21837 /* If we are using RS6000_PIC_OFFSET_TABLE_REGNUM, we need to set it up. */
21838 if (!TARGET_SINGLE_PIC_BASE
21839 && ((TARGET_TOC && TARGET_MINIMAL_TOC && get_pool_size () != 0)
21840 || (DEFAULT_ABI == ABI_V4
21841 && (flag_pic == 1 || (flag_pic && TARGET_SECURE_PLT))
21842 && df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))))
21844 /* If emit_load_toc_table will use the link register, we need to save
21845 it. We use R12 for this purpose because emit_load_toc_table
21846 can use register 0. This allows us to use a plain 'blr' to return
21847 from the procedure more often. */
21848 int save_LR_around_toc_setup = (TARGET_ELF
21849 && DEFAULT_ABI != ABI_AIX
21850 && flag_pic
21851 && ! info->lr_save_p
21852 && EDGE_COUNT (EXIT_BLOCK_PTR->preds) > 0);
21853 if (save_LR_around_toc_setup)
21855 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
21856 rtx tmp = gen_rtx_REG (Pmode, 12);
21858 insn = emit_move_insn (tmp, lr);
21859 RTX_FRAME_RELATED_P (insn) = 1;
21861 rs6000_emit_load_toc_table (TRUE);
21863 insn = emit_move_insn (lr, tmp);
21864 add_reg_note (insn, REG_CFA_RESTORE, lr);
21865 RTX_FRAME_RELATED_P (insn) = 1;
21867 else
21868 rs6000_emit_load_toc_table (TRUE);
21871 #if TARGET_MACHO
21872 if (!TARGET_SINGLE_PIC_BASE
21873 && DEFAULT_ABI == ABI_DARWIN
21874 && flag_pic && crtl->uses_pic_offset_table)
21876 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
21877 rtx src = gen_rtx_SYMBOL_REF (Pmode, MACHOPIC_FUNCTION_BASE_NAME);
21879 /* Save and restore LR locally around this call (in R0). */
21880 if (!info->lr_save_p)
21881 emit_move_insn (gen_rtx_REG (Pmode, 0), lr);
21883 emit_insn (gen_load_macho_picbase (src));
21885 emit_move_insn (gen_rtx_REG (Pmode,
21886 RS6000_PIC_OFFSET_TABLE_REGNUM),
21887 lr);
21889 if (!info->lr_save_p)
21890 emit_move_insn (lr, gen_rtx_REG (Pmode, 0));
21892 #endif
21894 /* If we need to, save the TOC register after doing the stack setup.
21895 Do not emit eh frame info for this save. The unwinder wants info,
21896 conceptually attached to instructions in this function, about
21897 register values in the caller of this function. This R2 may have
21898 already been changed from the value in the caller.
21899 We don't attempt to write accurate DWARF EH frame info for R2
21900 because code emitted by gcc for a (non-pointer) function call
21901 doesn't save and restore R2. Instead, R2 is managed out-of-line
21902 by a linker generated plt call stub when the function resides in
21903 a shared library. This behaviour is costly to describe in DWARF,
21904 both in terms of the size of DWARF info and the time taken in the
21905 unwinder to interpret it. R2 changes, apart from the
21906 calls_eh_return case earlier in this function, are handled by
21907 linux-unwind.h frob_update_context. */
21908 if (rs6000_save_toc_in_prologue_p ())
21910 rtx reg = gen_rtx_REG (reg_mode, TOC_REGNUM);
21911 emit_insn (gen_frame_store (reg, sp_reg_rtx, 5 * reg_size));
21915 /* Write function prologue. */
21917 static void
21918 rs6000_output_function_prologue (FILE *file,
21919 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
21921 rs6000_stack_t *info = rs6000_stack_info ();
21923 if (TARGET_DEBUG_STACK)
21924 debug_stack_info (info);
21926 /* Write .extern for any function we will call to save and restore
21927 fp values. */
21928 if (info->first_fp_reg_save < 64
21929 && !TARGET_MACHO
21930 && !TARGET_ELF)
21932 char *name;
21933 int regno = info->first_fp_reg_save - 32;
21935 if ((info->savres_strategy & SAVE_INLINE_FPRS) == 0)
21937 bool lr = (info->savres_strategy & SAVE_NOINLINE_FPRS_SAVES_LR) != 0;
21938 int sel = SAVRES_SAVE | SAVRES_FPR | (lr ? SAVRES_LR : 0);
21939 name = rs6000_savres_routine_name (info, regno, sel);
21940 fprintf (file, "\t.extern %s\n", name);
21942 if ((info->savres_strategy & REST_INLINE_FPRS) == 0)
21944 bool lr = (info->savres_strategy
21945 & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
21946 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
21947 name = rs6000_savres_routine_name (info, regno, sel);
21948 fprintf (file, "\t.extern %s\n", name);
21952 rs6000_pic_labelno++;
21955 /* Non-zero if vmx regs are restored before the frame pop, zero if
21956 we restore after the pop when possible. */
21957 #define ALWAYS_RESTORE_ALTIVEC_BEFORE_POP 0
21959 /* Restoring cr is a two step process: loading a reg from the frame
21960 save, then moving the reg to cr. For ABI_V4 we must let the
21961 unwinder know that the stack location is no longer valid at or
21962 before the stack deallocation, but we can't emit a cfa_restore for
21963 cr at the stack deallocation like we do for other registers.
21964 The trouble is that it is possible for the move to cr to be
21965 scheduled after the stack deallocation. So say exactly where cr
21966 is located on each of the two insns. */
21968 static rtx
21969 load_cr_save (int regno, rtx frame_reg_rtx, int offset, bool exit_func)
21971 rtx mem = gen_frame_mem_offset (SImode, frame_reg_rtx, offset);
21972 rtx reg = gen_rtx_REG (SImode, regno);
21973 rtx insn = emit_move_insn (reg, mem);
21975 if (!exit_func && DEFAULT_ABI == ABI_V4)
21977 rtx cr = gen_rtx_REG (SImode, CR2_REGNO);
21978 rtx set = gen_rtx_SET (VOIDmode, reg, cr);
21980 add_reg_note (insn, REG_CFA_REGISTER, set);
21981 RTX_FRAME_RELATED_P (insn) = 1;
21983 return reg;
21986 /* Reload CR from REG. */
21988 static void
21989 restore_saved_cr (rtx reg, int using_mfcr_multiple, bool exit_func)
21991 int count = 0;
21992 int i;
21994 if (using_mfcr_multiple)
21996 for (i = 0; i < 8; i++)
21997 if (save_reg_p (CR0_REGNO + i))
21998 count++;
21999 gcc_assert (count);
22002 if (using_mfcr_multiple && count > 1)
22004 rtvec p;
22005 int ndx;
22007 p = rtvec_alloc (count);
22009 ndx = 0;
22010 for (i = 0; i < 8; i++)
22011 if (save_reg_p (CR0_REGNO + i))
22013 rtvec r = rtvec_alloc (2);
22014 RTVEC_ELT (r, 0) = reg;
22015 RTVEC_ELT (r, 1) = GEN_INT (1 << (7-i));
22016 RTVEC_ELT (p, ndx) =
22017 gen_rtx_SET (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO + i),
22018 gen_rtx_UNSPEC (CCmode, r, UNSPEC_MOVESI_TO_CR));
22019 ndx++;
22021 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
22022 gcc_assert (ndx == count);
22024 else
22025 for (i = 0; i < 8; i++)
22026 if (save_reg_p (CR0_REGNO + i))
22027 emit_insn (gen_movsi_to_cr_one (gen_rtx_REG (CCmode, CR0_REGNO + i),
22028 reg));
22030 if (!exit_func && (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap))
22032 rtx insn = get_last_insn ();
22033 rtx cr = gen_rtx_REG (SImode, CR2_REGNO);
22035 add_reg_note (insn, REG_CFA_RESTORE, cr);
22036 RTX_FRAME_RELATED_P (insn) = 1;
22040 /* Like cr, the move to lr instruction can be scheduled after the
22041 stack deallocation, but unlike cr, its stack frame save is still
22042 valid. So we only need to emit the cfa_restore on the correct
22043 instruction. */
22045 static void
22046 load_lr_save (int regno, rtx frame_reg_rtx, int offset)
22048 rtx mem = gen_frame_mem_offset (Pmode, frame_reg_rtx, offset);
22049 rtx reg = gen_rtx_REG (Pmode, regno);
22051 emit_move_insn (reg, mem);
22054 static void
22055 restore_saved_lr (int regno, bool exit_func)
22057 rtx reg = gen_rtx_REG (Pmode, regno);
22058 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
22059 rtx insn = emit_move_insn (lr, reg);
22061 if (!exit_func && flag_shrink_wrap)
22063 add_reg_note (insn, REG_CFA_RESTORE, lr);
22064 RTX_FRAME_RELATED_P (insn) = 1;
22068 static rtx
22069 add_crlr_cfa_restore (const rs6000_stack_t *info, rtx cfa_restores)
22071 if (info->cr_save_p)
22072 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
22073 gen_rtx_REG (SImode, CR2_REGNO),
22074 cfa_restores);
22075 if (info->lr_save_p)
22076 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
22077 gen_rtx_REG (Pmode, LR_REGNO),
22078 cfa_restores);
22079 return cfa_restores;
22082 /* Return true if OFFSET from stack pointer can be clobbered by signals.
22083 V.4 doesn't have any stack cushion, AIX ABIs have 220 or 288 bytes
22084 below stack pointer not cloberred by signals. */
22086 static inline bool
22087 offset_below_red_zone_p (HOST_WIDE_INT offset)
22089 return offset < (DEFAULT_ABI == ABI_V4
22091 : TARGET_32BIT ? -220 : -288);
22094 /* Append CFA_RESTORES to any existing REG_NOTES on the last insn. */
22096 static void
22097 emit_cfa_restores (rtx cfa_restores)
22099 rtx insn = get_last_insn ();
22100 rtx *loc = &REG_NOTES (insn);
22102 while (*loc)
22103 loc = &XEXP (*loc, 1);
22104 *loc = cfa_restores;
22105 RTX_FRAME_RELATED_P (insn) = 1;
22108 /* Emit function epilogue as insns. */
22110 void
22111 rs6000_emit_epilogue (int sibcall)
22113 rs6000_stack_t *info;
22114 int restoring_GPRs_inline;
22115 int restoring_FPRs_inline;
22116 int using_load_multiple;
22117 int using_mtcr_multiple;
22118 int use_backchain_to_restore_sp;
22119 int restore_lr;
22120 int strategy;
22121 HOST_WIDE_INT frame_off = 0;
22122 rtx sp_reg_rtx = gen_rtx_REG (Pmode, 1);
22123 rtx frame_reg_rtx = sp_reg_rtx;
22124 rtx cfa_restores = NULL_RTX;
22125 rtx insn;
22126 rtx cr_save_reg = NULL_RTX;
22127 enum machine_mode reg_mode = Pmode;
22128 int reg_size = TARGET_32BIT ? 4 : 8;
22129 int i;
22130 bool exit_func;
22131 unsigned ptr_regno;
22133 info = rs6000_stack_info ();
22135 if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
22137 reg_mode = V2SImode;
22138 reg_size = 8;
22141 strategy = info->savres_strategy;
22142 using_load_multiple = strategy & SAVRES_MULTIPLE;
22143 restoring_FPRs_inline = sibcall || (strategy & REST_INLINE_FPRS);
22144 restoring_GPRs_inline = sibcall || (strategy & REST_INLINE_GPRS);
22145 using_mtcr_multiple = (rs6000_cpu == PROCESSOR_PPC601
22146 || rs6000_cpu == PROCESSOR_PPC603
22147 || rs6000_cpu == PROCESSOR_PPC750
22148 || optimize_size);
22149 /* Restore via the backchain when we have a large frame, since this
22150 is more efficient than an addis, addi pair. The second condition
22151 here will not trigger at the moment; We don't actually need a
22152 frame pointer for alloca, but the generic parts of the compiler
22153 give us one anyway. */
22154 use_backchain_to_restore_sp = (info->total_size > 32767 - info->lr_save_offset
22155 || (cfun->calls_alloca
22156 && !frame_pointer_needed));
22157 restore_lr = (info->lr_save_p
22158 && (restoring_FPRs_inline
22159 || (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR))
22160 && (restoring_GPRs_inline
22161 || info->first_fp_reg_save < 64));
22163 if (WORLD_SAVE_P (info))
22165 int i, j;
22166 char rname[30];
22167 const char *alloc_rname;
22168 rtvec p;
22170 /* eh_rest_world_r10 will return to the location saved in the LR
22171 stack slot (which is not likely to be our caller.)
22172 Input: R10 -- stack adjustment. Clobbers R0, R11, R12, R7, R8.
22173 rest_world is similar, except any R10 parameter is ignored.
22174 The exception-handling stuff that was here in 2.95 is no
22175 longer necessary. */
22177 p = rtvec_alloc (9
22179 + 32 - info->first_gp_reg_save
22180 + LAST_ALTIVEC_REGNO + 1 - info->first_altivec_reg_save
22181 + 63 + 1 - info->first_fp_reg_save);
22183 strcpy (rname, ((crtl->calls_eh_return) ?
22184 "*eh_rest_world_r10" : "*rest_world"));
22185 alloc_rname = ggc_strdup (rname);
22187 j = 0;
22188 RTVEC_ELT (p, j++) = ret_rtx;
22189 RTVEC_ELT (p, j++) = gen_rtx_USE (VOIDmode,
22190 gen_rtx_REG (Pmode,
22191 LR_REGNO));
22192 RTVEC_ELT (p, j++)
22193 = gen_rtx_USE (VOIDmode, gen_rtx_SYMBOL_REF (Pmode, alloc_rname));
22194 /* The instruction pattern requires a clobber here;
22195 it is shared with the restVEC helper. */
22196 RTVEC_ELT (p, j++)
22197 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 11));
22200 /* CR register traditionally saved as CR2. */
22201 rtx reg = gen_rtx_REG (SImode, CR2_REGNO);
22202 RTVEC_ELT (p, j++)
22203 = gen_frame_load (reg, frame_reg_rtx, info->cr_save_offset);
22204 if (flag_shrink_wrap)
22206 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
22207 gen_rtx_REG (Pmode, LR_REGNO),
22208 cfa_restores);
22209 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
22213 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
22215 rtx reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
22216 RTVEC_ELT (p, j++)
22217 = gen_frame_load (reg,
22218 frame_reg_rtx, info->gp_save_offset + reg_size * i);
22219 if (flag_shrink_wrap)
22220 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
22222 for (i = 0; info->first_altivec_reg_save + i <= LAST_ALTIVEC_REGNO; i++)
22224 rtx reg = gen_rtx_REG (V4SImode, info->first_altivec_reg_save + i);
22225 RTVEC_ELT (p, j++)
22226 = gen_frame_load (reg,
22227 frame_reg_rtx, info->altivec_save_offset + 16 * i);
22228 if (flag_shrink_wrap)
22229 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
22231 for (i = 0; info->first_fp_reg_save + i <= 63; i++)
22233 rtx reg = gen_rtx_REG ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
22234 ? DFmode : SFmode),
22235 info->first_fp_reg_save + i);
22236 RTVEC_ELT (p, j++)
22237 = gen_frame_load (reg, frame_reg_rtx, info->fp_save_offset + 8 * i);
22238 if (flag_shrink_wrap)
22239 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
22241 RTVEC_ELT (p, j++)
22242 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 0));
22243 RTVEC_ELT (p, j++)
22244 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 12));
22245 RTVEC_ELT (p, j++)
22246 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 7));
22247 RTVEC_ELT (p, j++)
22248 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 8));
22249 RTVEC_ELT (p, j++)
22250 = gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, 10));
22251 insn = emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
22253 if (flag_shrink_wrap)
22255 REG_NOTES (insn) = cfa_restores;
22256 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
22257 RTX_FRAME_RELATED_P (insn) = 1;
22259 return;
22262 /* frame_reg_rtx + frame_off points to the top of this stack frame. */
22263 if (info->push_p)
22264 frame_off = info->total_size;
22266 /* Restore AltiVec registers if we must do so before adjusting the
22267 stack. */
22268 if (TARGET_ALTIVEC_ABI
22269 && info->altivec_size != 0
22270 && (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
22271 || (DEFAULT_ABI != ABI_V4
22272 && offset_below_red_zone_p (info->altivec_save_offset))))
22274 int i;
22275 int scratch_regno = ptr_regno_for_savres (SAVRES_VR);
22277 gcc_checking_assert (scratch_regno == 11 || scratch_regno == 12);
22278 if (use_backchain_to_restore_sp)
22280 int frame_regno = 11;
22282 if ((strategy & REST_INLINE_VRS) == 0)
22284 /* Of r11 and r12, select the one not clobbered by an
22285 out-of-line restore function for the frame register. */
22286 frame_regno = 11 + 12 - scratch_regno;
22288 frame_reg_rtx = gen_rtx_REG (Pmode, frame_regno);
22289 emit_move_insn (frame_reg_rtx,
22290 gen_rtx_MEM (Pmode, sp_reg_rtx));
22291 frame_off = 0;
22293 else if (frame_pointer_needed)
22294 frame_reg_rtx = hard_frame_pointer_rtx;
22296 if ((strategy & REST_INLINE_VRS) == 0)
22298 int end_save = info->altivec_save_offset + info->altivec_size;
22299 int ptr_off;
22300 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
22301 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
22303 if (end_save + frame_off != 0)
22305 rtx offset = GEN_INT (end_save + frame_off);
22307 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
22309 else
22310 emit_move_insn (ptr_reg, frame_reg_rtx);
22312 ptr_off = -end_save;
22313 insn = rs6000_emit_savres_rtx (info, scratch_reg,
22314 info->altivec_save_offset + ptr_off,
22315 0, V4SImode, SAVRES_VR);
22317 else
22319 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
22320 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
22322 rtx addr, areg, mem, reg;
22324 areg = gen_rtx_REG (Pmode, 0);
22325 emit_move_insn
22326 (areg, GEN_INT (info->altivec_save_offset
22327 + frame_off
22328 + 16 * (i - info->first_altivec_reg_save)));
22330 /* AltiVec addressing mode is [reg+reg]. */
22331 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
22332 mem = gen_frame_mem (V4SImode, addr);
22334 reg = gen_rtx_REG (V4SImode, i);
22335 emit_move_insn (reg, mem);
22339 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
22340 if (((strategy & REST_INLINE_VRS) == 0
22341 || (info->vrsave_mask & ALTIVEC_REG_BIT (i)) != 0)
22342 && (flag_shrink_wrap
22343 || (offset_below_red_zone_p
22344 (info->altivec_save_offset
22345 + 16 * (i - info->first_altivec_reg_save)))))
22347 rtx reg = gen_rtx_REG (V4SImode, i);
22348 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
22352 /* Restore VRSAVE if we must do so before adjusting the stack. */
22353 if (TARGET_ALTIVEC
22354 && TARGET_ALTIVEC_VRSAVE
22355 && info->vrsave_mask != 0
22356 && (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
22357 || (DEFAULT_ABI != ABI_V4
22358 && offset_below_red_zone_p (info->vrsave_save_offset))))
22360 rtx reg;
22362 if (frame_reg_rtx == sp_reg_rtx)
22364 if (use_backchain_to_restore_sp)
22366 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
22367 emit_move_insn (frame_reg_rtx,
22368 gen_rtx_MEM (Pmode, sp_reg_rtx));
22369 frame_off = 0;
22371 else if (frame_pointer_needed)
22372 frame_reg_rtx = hard_frame_pointer_rtx;
22375 reg = gen_rtx_REG (SImode, 12);
22376 emit_insn (gen_frame_load (reg, frame_reg_rtx,
22377 info->vrsave_save_offset + frame_off));
22379 emit_insn (generate_set_vrsave (reg, info, 1));
22382 insn = NULL_RTX;
22383 /* If we have a large stack frame, restore the old stack pointer
22384 using the backchain. */
22385 if (use_backchain_to_restore_sp)
22387 if (frame_reg_rtx == sp_reg_rtx)
22389 /* Under V.4, don't reset the stack pointer until after we're done
22390 loading the saved registers. */
22391 if (DEFAULT_ABI == ABI_V4)
22392 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
22394 insn = emit_move_insn (frame_reg_rtx,
22395 gen_rtx_MEM (Pmode, sp_reg_rtx));
22396 frame_off = 0;
22398 else if (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
22399 && DEFAULT_ABI == ABI_V4)
22400 /* frame_reg_rtx has been set up by the altivec restore. */
22402 else
22404 insn = emit_move_insn (sp_reg_rtx, frame_reg_rtx);
22405 frame_reg_rtx = sp_reg_rtx;
22408 /* If we have a frame pointer, we can restore the old stack pointer
22409 from it. */
22410 else if (frame_pointer_needed)
22412 frame_reg_rtx = sp_reg_rtx;
22413 if (DEFAULT_ABI == ABI_V4)
22414 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
22415 /* Prevent reordering memory accesses against stack pointer restore. */
22416 else if (cfun->calls_alloca
22417 || offset_below_red_zone_p (-info->total_size))
22418 rs6000_emit_stack_tie (frame_reg_rtx, true);
22420 insn = emit_insn (gen_add3_insn (frame_reg_rtx, hard_frame_pointer_rtx,
22421 GEN_INT (info->total_size)));
22422 frame_off = 0;
22424 else if (info->push_p
22425 && DEFAULT_ABI != ABI_V4
22426 && !crtl->calls_eh_return)
22428 /* Prevent reordering memory accesses against stack pointer restore. */
22429 if (cfun->calls_alloca
22430 || offset_below_red_zone_p (-info->total_size))
22431 rs6000_emit_stack_tie (frame_reg_rtx, false);
22432 insn = emit_insn (gen_add3_insn (sp_reg_rtx, sp_reg_rtx,
22433 GEN_INT (info->total_size)));
22434 frame_off = 0;
22436 if (insn && frame_reg_rtx == sp_reg_rtx)
22438 if (cfa_restores)
22440 REG_NOTES (insn) = cfa_restores;
22441 cfa_restores = NULL_RTX;
22443 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
22444 RTX_FRAME_RELATED_P (insn) = 1;
22447 /* Restore AltiVec registers if we have not done so already. */
22448 if (!ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
22449 && TARGET_ALTIVEC_ABI
22450 && info->altivec_size != 0
22451 && (DEFAULT_ABI == ABI_V4
22452 || !offset_below_red_zone_p (info->altivec_save_offset)))
22454 int i;
22456 if ((strategy & REST_INLINE_VRS) == 0)
22458 int end_save = info->altivec_save_offset + info->altivec_size;
22459 int ptr_off;
22460 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
22461 int scratch_regno = ptr_regno_for_savres (SAVRES_VR);
22462 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
22464 if (end_save + frame_off != 0)
22466 rtx offset = GEN_INT (end_save + frame_off);
22468 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
22470 else
22471 emit_move_insn (ptr_reg, frame_reg_rtx);
22473 ptr_off = -end_save;
22474 insn = rs6000_emit_savres_rtx (info, scratch_reg,
22475 info->altivec_save_offset + ptr_off,
22476 0, V4SImode, SAVRES_VR);
22477 if (REGNO (frame_reg_rtx) == REGNO (scratch_reg))
22479 /* Frame reg was clobbered by out-of-line save. Restore it
22480 from ptr_reg, and if we are calling out-of-line gpr or
22481 fpr restore set up the correct pointer and offset. */
22482 unsigned newptr_regno = 1;
22483 if (!restoring_GPRs_inline)
22485 bool lr = info->gp_save_offset + info->gp_size == 0;
22486 int sel = SAVRES_GPR | (lr ? SAVRES_LR : 0);
22487 newptr_regno = ptr_regno_for_savres (sel);
22488 end_save = info->gp_save_offset + info->gp_size;
22490 else if (!restoring_FPRs_inline)
22492 bool lr = !(strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR);
22493 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
22494 newptr_regno = ptr_regno_for_savres (sel);
22495 end_save = info->gp_save_offset + info->gp_size;
22498 if (newptr_regno != 1 && REGNO (frame_reg_rtx) != newptr_regno)
22499 frame_reg_rtx = gen_rtx_REG (Pmode, newptr_regno);
22501 if (end_save + ptr_off != 0)
22503 rtx offset = GEN_INT (end_save + ptr_off);
22505 frame_off = -end_save;
22506 emit_insn (gen_add3_insn (frame_reg_rtx, ptr_reg, offset));
22508 else
22510 frame_off = ptr_off;
22511 emit_move_insn (frame_reg_rtx, ptr_reg);
22515 else
22517 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
22518 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
22520 rtx addr, areg, mem, reg;
22522 areg = gen_rtx_REG (Pmode, 0);
22523 emit_move_insn
22524 (areg, GEN_INT (info->altivec_save_offset
22525 + frame_off
22526 + 16 * (i - info->first_altivec_reg_save)));
22528 /* AltiVec addressing mode is [reg+reg]. */
22529 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
22530 mem = gen_frame_mem (V4SImode, addr);
22532 reg = gen_rtx_REG (V4SImode, i);
22533 emit_move_insn (reg, mem);
22537 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
22538 if (((strategy & REST_INLINE_VRS) == 0
22539 || (info->vrsave_mask & ALTIVEC_REG_BIT (i)) != 0)
22540 && (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap))
22542 rtx reg = gen_rtx_REG (V4SImode, i);
22543 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
22547 /* Restore VRSAVE if we have not done so already. */
22548 if (!ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
22549 && TARGET_ALTIVEC
22550 && TARGET_ALTIVEC_VRSAVE
22551 && info->vrsave_mask != 0
22552 && (DEFAULT_ABI == ABI_V4
22553 || !offset_below_red_zone_p (info->vrsave_save_offset)))
22555 rtx reg;
22557 reg = gen_rtx_REG (SImode, 12);
22558 emit_insn (gen_frame_load (reg, frame_reg_rtx,
22559 info->vrsave_save_offset + frame_off));
22561 emit_insn (generate_set_vrsave (reg, info, 1));
22564 /* If we exit by an out-of-line restore function on ABI_V4 then that
22565 function will deallocate the stack, so we don't need to worry
22566 about the unwinder restoring cr from an invalid stack frame
22567 location. */
22568 exit_func = (!restoring_FPRs_inline
22569 || (!restoring_GPRs_inline
22570 && info->first_fp_reg_save == 64));
22572 /* Get the old lr if we saved it. If we are restoring registers
22573 out-of-line, then the out-of-line routines can do this for us. */
22574 if (restore_lr && restoring_GPRs_inline)
22575 load_lr_save (0, frame_reg_rtx, info->lr_save_offset + frame_off);
22577 /* Get the old cr if we saved it. */
22578 if (info->cr_save_p)
22580 unsigned cr_save_regno = 12;
22582 if (!restoring_GPRs_inline)
22584 /* Ensure we don't use the register used by the out-of-line
22585 gpr register restore below. */
22586 bool lr = info->gp_save_offset + info->gp_size == 0;
22587 int sel = SAVRES_GPR | (lr ? SAVRES_LR : 0);
22588 int gpr_ptr_regno = ptr_regno_for_savres (sel);
22590 if (gpr_ptr_regno == 12)
22591 cr_save_regno = 11;
22592 gcc_checking_assert (REGNO (frame_reg_rtx) != cr_save_regno);
22594 else if (REGNO (frame_reg_rtx) == 12)
22595 cr_save_regno = 11;
22597 cr_save_reg = load_cr_save (cr_save_regno, frame_reg_rtx,
22598 info->cr_save_offset + frame_off,
22599 exit_func);
22602 /* Set LR here to try to overlap restores below. */
22603 if (restore_lr && restoring_GPRs_inline)
22604 restore_saved_lr (0, exit_func);
22606 /* Load exception handler data registers, if needed. */
22607 if (crtl->calls_eh_return)
22609 unsigned int i, regno;
22611 if (TARGET_AIX)
22613 rtx reg = gen_rtx_REG (reg_mode, 2);
22614 emit_insn (gen_frame_load (reg, frame_reg_rtx,
22615 frame_off + 5 * reg_size));
22618 for (i = 0; ; ++i)
22620 rtx mem;
22622 regno = EH_RETURN_DATA_REGNO (i);
22623 if (regno == INVALID_REGNUM)
22624 break;
22626 /* Note: possible use of r0 here to address SPE regs. */
22627 mem = gen_frame_mem_offset (reg_mode, frame_reg_rtx,
22628 info->ehrd_offset + frame_off
22629 + reg_size * (int) i);
22631 emit_move_insn (gen_rtx_REG (reg_mode, regno), mem);
22635 /* Restore GPRs. This is done as a PARALLEL if we are using
22636 the load-multiple instructions. */
22637 if (TARGET_SPE_ABI
22638 && info->spe_64bit_regs_used
22639 && info->first_gp_reg_save != 32)
22641 /* Determine whether we can address all of the registers that need
22642 to be saved with an offset from frame_reg_rtx that fits in
22643 the small const field for SPE memory instructions. */
22644 int spe_regs_addressable
22645 = (SPE_CONST_OFFSET_OK (info->spe_gp_save_offset + frame_off
22646 + reg_size * (32 - info->first_gp_reg_save - 1))
22647 && restoring_GPRs_inline);
22649 if (!spe_regs_addressable)
22651 int ool_adjust = 0;
22652 rtx old_frame_reg_rtx = frame_reg_rtx;
22653 /* Make r11 point to the start of the SPE save area. We worried about
22654 not clobbering it when we were saving registers in the prologue.
22655 There's no need to worry here because the static chain is passed
22656 anew to every function. */
22658 if (!restoring_GPRs_inline)
22659 ool_adjust = 8 * (info->first_gp_reg_save
22660 - (FIRST_SAVRES_REGISTER + 1));
22661 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
22662 emit_insn (gen_addsi3 (frame_reg_rtx, old_frame_reg_rtx,
22663 GEN_INT (info->spe_gp_save_offset
22664 + frame_off
22665 - ool_adjust)));
22666 /* Keep the invariant that frame_reg_rtx + frame_off points
22667 at the top of the stack frame. */
22668 frame_off = -info->spe_gp_save_offset + ool_adjust;
22671 if (restoring_GPRs_inline)
22673 HOST_WIDE_INT spe_offset = info->spe_gp_save_offset + frame_off;
22675 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
22676 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
22678 rtx offset, addr, mem, reg;
22680 /* We're doing all this to ensure that the immediate offset
22681 fits into the immediate field of 'evldd'. */
22682 gcc_assert (SPE_CONST_OFFSET_OK (spe_offset + reg_size * i));
22684 offset = GEN_INT (spe_offset + reg_size * i);
22685 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, offset);
22686 mem = gen_rtx_MEM (V2SImode, addr);
22687 reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
22689 emit_move_insn (reg, mem);
22692 else
22693 rs6000_emit_savres_rtx (info, frame_reg_rtx,
22694 info->spe_gp_save_offset + frame_off,
22695 info->lr_save_offset + frame_off,
22696 reg_mode,
22697 SAVRES_GPR | SAVRES_LR);
22699 else if (!restoring_GPRs_inline)
22701 /* We are jumping to an out-of-line function. */
22702 rtx ptr_reg;
22703 int end_save = info->gp_save_offset + info->gp_size;
22704 bool can_use_exit = end_save == 0;
22705 int sel = SAVRES_GPR | (can_use_exit ? SAVRES_LR : 0);
22706 int ptr_off;
22708 /* Emit stack reset code if we need it. */
22709 ptr_regno = ptr_regno_for_savres (sel);
22710 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
22711 if (can_use_exit)
22712 rs6000_emit_stack_reset (info, frame_reg_rtx, frame_off, ptr_regno);
22713 else if (end_save + frame_off != 0)
22714 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx,
22715 GEN_INT (end_save + frame_off)));
22716 else if (REGNO (frame_reg_rtx) != ptr_regno)
22717 emit_move_insn (ptr_reg, frame_reg_rtx);
22718 if (REGNO (frame_reg_rtx) == ptr_regno)
22719 frame_off = -end_save;
22721 if (can_use_exit && info->cr_save_p)
22722 restore_saved_cr (cr_save_reg, using_mtcr_multiple, true);
22724 ptr_off = -end_save;
22725 rs6000_emit_savres_rtx (info, ptr_reg,
22726 info->gp_save_offset + ptr_off,
22727 info->lr_save_offset + ptr_off,
22728 reg_mode, sel);
22730 else if (using_load_multiple)
22732 rtvec p;
22733 p = rtvec_alloc (32 - info->first_gp_reg_save);
22734 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
22735 RTVEC_ELT (p, i)
22736 = gen_frame_load (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
22737 frame_reg_rtx,
22738 info->gp_save_offset + frame_off + reg_size * i);
22739 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
22741 else
22743 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
22744 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
22745 emit_insn (gen_frame_load
22746 (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
22747 frame_reg_rtx,
22748 info->gp_save_offset + frame_off + reg_size * i));
22751 if (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
22753 /* If the frame pointer was used then we can't delay emitting
22754 a REG_CFA_DEF_CFA note. This must happen on the insn that
22755 restores the frame pointer, r31. We may have already emitted
22756 a REG_CFA_DEF_CFA note, but that's OK; A duplicate is
22757 discarded by dwarf2cfi.c/dwarf2out.c, and in any case would
22758 be harmless if emitted. */
22759 if (frame_pointer_needed)
22761 insn = get_last_insn ();
22762 add_reg_note (insn, REG_CFA_DEF_CFA,
22763 plus_constant (Pmode, frame_reg_rtx, frame_off));
22764 RTX_FRAME_RELATED_P (insn) = 1;
22767 /* Set up cfa_restores. We always need these when
22768 shrink-wrapping. If not shrink-wrapping then we only need
22769 the cfa_restore when the stack location is no longer valid.
22770 The cfa_restores must be emitted on or before the insn that
22771 invalidates the stack, and of course must not be emitted
22772 before the insn that actually does the restore. The latter
22773 is why it is a bad idea to emit the cfa_restores as a group
22774 on the last instruction here that actually does a restore:
22775 That insn may be reordered with respect to others doing
22776 restores. */
22777 if (flag_shrink_wrap
22778 && !restoring_GPRs_inline
22779 && info->first_fp_reg_save == 64)
22780 cfa_restores = add_crlr_cfa_restore (info, cfa_restores);
22782 for (i = info->first_gp_reg_save; i < 32; i++)
22783 if (!restoring_GPRs_inline
22784 || using_load_multiple
22785 || rs6000_reg_live_or_pic_offset_p (i))
22787 rtx reg = gen_rtx_REG (reg_mode, i);
22789 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
22793 if (!restoring_GPRs_inline
22794 && info->first_fp_reg_save == 64)
22796 /* We are jumping to an out-of-line function. */
22797 if (cfa_restores)
22798 emit_cfa_restores (cfa_restores);
22799 return;
22802 if (restore_lr && !restoring_GPRs_inline)
22804 load_lr_save (0, frame_reg_rtx, info->lr_save_offset + frame_off);
22805 restore_saved_lr (0, exit_func);
22808 /* Restore fpr's if we need to do it without calling a function. */
22809 if (restoring_FPRs_inline)
22810 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
22811 if (save_reg_p (info->first_fp_reg_save + i))
22813 rtx reg = gen_rtx_REG ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
22814 ? DFmode : SFmode),
22815 info->first_fp_reg_save + i);
22816 emit_insn (gen_frame_load (reg, frame_reg_rtx,
22817 info->fp_save_offset + frame_off + 8 * i));
22818 if (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
22819 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
22822 /* If we saved cr, restore it here. Just those that were used. */
22823 if (info->cr_save_p)
22824 restore_saved_cr (cr_save_reg, using_mtcr_multiple, exit_func);
22826 /* If this is V.4, unwind the stack pointer after all of the loads
22827 have been done, or set up r11 if we are restoring fp out of line. */
22828 ptr_regno = 1;
22829 if (!restoring_FPRs_inline)
22831 bool lr = (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
22832 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
22833 ptr_regno = ptr_regno_for_savres (sel);
22836 insn = rs6000_emit_stack_reset (info, frame_reg_rtx, frame_off, ptr_regno);
22837 if (REGNO (frame_reg_rtx) == ptr_regno)
22838 frame_off = 0;
22840 if (insn && restoring_FPRs_inline)
22842 if (cfa_restores)
22844 REG_NOTES (insn) = cfa_restores;
22845 cfa_restores = NULL_RTX;
22847 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
22848 RTX_FRAME_RELATED_P (insn) = 1;
22851 if (crtl->calls_eh_return)
22853 rtx sa = EH_RETURN_STACKADJ_RTX;
22854 emit_insn (gen_add3_insn (sp_reg_rtx, sp_reg_rtx, sa));
22857 if (!sibcall)
22859 rtvec p;
22860 bool lr = (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
22861 if (! restoring_FPRs_inline)
22863 p = rtvec_alloc (4 + 64 - info->first_fp_reg_save);
22864 RTVEC_ELT (p, 0) = ret_rtx;
22866 else
22868 if (cfa_restores)
22870 /* We can't hang the cfa_restores off a simple return,
22871 since the shrink-wrap code sometimes uses an existing
22872 return. This means there might be a path from
22873 pre-prologue code to this return, and dwarf2cfi code
22874 wants the eh_frame unwinder state to be the same on
22875 all paths to any point. So we need to emit the
22876 cfa_restores before the return. For -m64 we really
22877 don't need epilogue cfa_restores at all, except for
22878 this irritating dwarf2cfi with shrink-wrap
22879 requirement; The stack red-zone means eh_frame info
22880 from the prologue telling the unwinder to restore
22881 from the stack is perfectly good right to the end of
22882 the function. */
22883 emit_insn (gen_blockage ());
22884 emit_cfa_restores (cfa_restores);
22885 cfa_restores = NULL_RTX;
22887 p = rtvec_alloc (2);
22888 RTVEC_ELT (p, 0) = simple_return_rtx;
22891 RTVEC_ELT (p, 1) = ((restoring_FPRs_inline || !lr)
22892 ? gen_rtx_USE (VOIDmode,
22893 gen_rtx_REG (Pmode, LR_REGNO))
22894 : gen_rtx_CLOBBER (VOIDmode,
22895 gen_rtx_REG (Pmode, LR_REGNO)));
22897 /* If we have to restore more than two FP registers, branch to the
22898 restore function. It will return to our caller. */
22899 if (! restoring_FPRs_inline)
22901 int i;
22902 rtx sym;
22904 if (flag_shrink_wrap)
22905 cfa_restores = add_crlr_cfa_restore (info, cfa_restores);
22907 sym = rs6000_savres_routine_sym (info,
22908 SAVRES_FPR | (lr ? SAVRES_LR : 0));
22909 RTVEC_ELT (p, 2) = gen_rtx_USE (VOIDmode, sym);
22910 RTVEC_ELT (p, 3) = gen_rtx_USE (VOIDmode,
22911 gen_rtx_REG (Pmode,
22912 DEFAULT_ABI == ABI_AIX
22913 ? 1 : 11));
22914 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
22916 rtx reg = gen_rtx_REG (DFmode, info->first_fp_reg_save + i);
22918 RTVEC_ELT (p, i + 4)
22919 = gen_frame_load (reg, sp_reg_rtx, info->fp_save_offset + 8 * i);
22920 if (flag_shrink_wrap)
22921 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg,
22922 cfa_restores);
22926 emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
22929 if (cfa_restores)
22931 if (sibcall)
22932 /* Ensure the cfa_restores are hung off an insn that won't
22933 be reordered above other restores. */
22934 emit_insn (gen_blockage ());
22936 emit_cfa_restores (cfa_restores);
22940 /* Write function epilogue. */
22942 static void
22943 rs6000_output_function_epilogue (FILE *file,
22944 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
22946 #if TARGET_MACHO
22947 macho_branch_islands ();
22948 /* Mach-O doesn't support labels at the end of objects, so if
22949 it looks like we might want one, insert a NOP. */
22951 rtx insn = get_last_insn ();
22952 rtx deleted_debug_label = NULL_RTX;
22953 while (insn
22954 && NOTE_P (insn)
22955 && NOTE_KIND (insn) != NOTE_INSN_DELETED_LABEL)
22957 /* Don't insert a nop for NOTE_INSN_DELETED_DEBUG_LABEL
22958 notes only, instead set their CODE_LABEL_NUMBER to -1,
22959 otherwise there would be code generation differences
22960 in between -g and -g0. */
22961 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
22962 deleted_debug_label = insn;
22963 insn = PREV_INSN (insn);
22965 if (insn
22966 && (LABEL_P (insn)
22967 || (NOTE_P (insn)
22968 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL)))
22969 fputs ("\tnop\n", file);
22970 else if (deleted_debug_label)
22971 for (insn = deleted_debug_label; insn; insn = NEXT_INSN (insn))
22972 if (NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
22973 CODE_LABEL_NUMBER (insn) = -1;
22975 #endif
22977 /* Output a traceback table here. See /usr/include/sys/debug.h for info
22978 on its format.
22980 We don't output a traceback table if -finhibit-size-directive was
22981 used. The documentation for -finhibit-size-directive reads
22982 ``don't output a @code{.size} assembler directive, or anything
22983 else that would cause trouble if the function is split in the
22984 middle, and the two halves are placed at locations far apart in
22985 memory.'' The traceback table has this property, since it
22986 includes the offset from the start of the function to the
22987 traceback table itself.
22989 System V.4 Powerpc's (and the embedded ABI derived from it) use a
22990 different traceback table. */
22991 if (DEFAULT_ABI == ABI_AIX && ! flag_inhibit_size_directive
22992 && rs6000_traceback != traceback_none && !cfun->is_thunk)
22994 const char *fname = NULL;
22995 const char *language_string = lang_hooks.name;
22996 int fixed_parms = 0, float_parms = 0, parm_info = 0;
22997 int i;
22998 int optional_tbtab;
22999 rs6000_stack_t *info = rs6000_stack_info ();
23001 if (rs6000_traceback == traceback_full)
23002 optional_tbtab = 1;
23003 else if (rs6000_traceback == traceback_part)
23004 optional_tbtab = 0;
23005 else
23006 optional_tbtab = !optimize_size && !TARGET_ELF;
23008 if (optional_tbtab)
23010 fname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
23011 while (*fname == '.') /* V.4 encodes . in the name */
23012 fname++;
23014 /* Need label immediately before tbtab, so we can compute
23015 its offset from the function start. */
23016 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
23017 ASM_OUTPUT_LABEL (file, fname);
23020 /* The .tbtab pseudo-op can only be used for the first eight
23021 expressions, since it can't handle the possibly variable
23022 length fields that follow. However, if you omit the optional
23023 fields, the assembler outputs zeros for all optional fields
23024 anyways, giving each variable length field is minimum length
23025 (as defined in sys/debug.h). Thus we can not use the .tbtab
23026 pseudo-op at all. */
23028 /* An all-zero word flags the start of the tbtab, for debuggers
23029 that have to find it by searching forward from the entry
23030 point or from the current pc. */
23031 fputs ("\t.long 0\n", file);
23033 /* Tbtab format type. Use format type 0. */
23034 fputs ("\t.byte 0,", file);
23036 /* Language type. Unfortunately, there does not seem to be any
23037 official way to discover the language being compiled, so we
23038 use language_string.
23039 C is 0. Fortran is 1. Pascal is 2. Ada is 3. C++ is 9.
23040 Java is 13. Objective-C is 14. Objective-C++ isn't assigned
23041 a number, so for now use 9. LTO and Go aren't assigned numbers
23042 either, so for now use 0. */
23043 if (! strcmp (language_string, "GNU C")
23044 || ! strcmp (language_string, "GNU GIMPLE")
23045 || ! strcmp (language_string, "GNU Go"))
23046 i = 0;
23047 else if (! strcmp (language_string, "GNU F77")
23048 || ! strcmp (language_string, "GNU Fortran"))
23049 i = 1;
23050 else if (! strcmp (language_string, "GNU Pascal"))
23051 i = 2;
23052 else if (! strcmp (language_string, "GNU Ada"))
23053 i = 3;
23054 else if (! strcmp (language_string, "GNU C++")
23055 || ! strcmp (language_string, "GNU Objective-C++"))
23056 i = 9;
23057 else if (! strcmp (language_string, "GNU Java"))
23058 i = 13;
23059 else if (! strcmp (language_string, "GNU Objective-C"))
23060 i = 14;
23061 else
23062 gcc_unreachable ();
23063 fprintf (file, "%d,", i);
23065 /* 8 single bit fields: global linkage (not set for C extern linkage,
23066 apparently a PL/I convention?), out-of-line epilogue/prologue, offset
23067 from start of procedure stored in tbtab, internal function, function
23068 has controlled storage, function has no toc, function uses fp,
23069 function logs/aborts fp operations. */
23070 /* Assume that fp operations are used if any fp reg must be saved. */
23071 fprintf (file, "%d,",
23072 (optional_tbtab << 5) | ((info->first_fp_reg_save != 64) << 1));
23074 /* 6 bitfields: function is interrupt handler, name present in
23075 proc table, function calls alloca, on condition directives
23076 (controls stack walks, 3 bits), saves condition reg, saves
23077 link reg. */
23078 /* The `function calls alloca' bit seems to be set whenever reg 31 is
23079 set up as a frame pointer, even when there is no alloca call. */
23080 fprintf (file, "%d,",
23081 ((optional_tbtab << 6)
23082 | ((optional_tbtab & frame_pointer_needed) << 5)
23083 | (info->cr_save_p << 1)
23084 | (info->lr_save_p)));
23086 /* 3 bitfields: saves backchain, fixup code, number of fpr saved
23087 (6 bits). */
23088 fprintf (file, "%d,",
23089 (info->push_p << 7) | (64 - info->first_fp_reg_save));
23091 /* 2 bitfields: spare bits (2 bits), number of gpr saved (6 bits). */
23092 fprintf (file, "%d,", (32 - first_reg_to_save ()));
23094 if (optional_tbtab)
23096 /* Compute the parameter info from the function decl argument
23097 list. */
23098 tree decl;
23099 int next_parm_info_bit = 31;
23101 for (decl = DECL_ARGUMENTS (current_function_decl);
23102 decl; decl = DECL_CHAIN (decl))
23104 rtx parameter = DECL_INCOMING_RTL (decl);
23105 enum machine_mode mode = GET_MODE (parameter);
23107 if (GET_CODE (parameter) == REG)
23109 if (SCALAR_FLOAT_MODE_P (mode))
23111 int bits;
23113 float_parms++;
23115 switch (mode)
23117 case SFmode:
23118 case SDmode:
23119 bits = 0x2;
23120 break;
23122 case DFmode:
23123 case DDmode:
23124 case TFmode:
23125 case TDmode:
23126 bits = 0x3;
23127 break;
23129 default:
23130 gcc_unreachable ();
23133 /* If only one bit will fit, don't or in this entry. */
23134 if (next_parm_info_bit > 0)
23135 parm_info |= (bits << (next_parm_info_bit - 1));
23136 next_parm_info_bit -= 2;
23138 else
23140 fixed_parms += ((GET_MODE_SIZE (mode)
23141 + (UNITS_PER_WORD - 1))
23142 / UNITS_PER_WORD);
23143 next_parm_info_bit -= 1;
23149 /* Number of fixed point parameters. */
23150 /* This is actually the number of words of fixed point parameters; thus
23151 an 8 byte struct counts as 2; and thus the maximum value is 8. */
23152 fprintf (file, "%d,", fixed_parms);
23154 /* 2 bitfields: number of floating point parameters (7 bits), parameters
23155 all on stack. */
23156 /* This is actually the number of fp registers that hold parameters;
23157 and thus the maximum value is 13. */
23158 /* Set parameters on stack bit if parameters are not in their original
23159 registers, regardless of whether they are on the stack? Xlc
23160 seems to set the bit when not optimizing. */
23161 fprintf (file, "%d\n", ((float_parms << 1) | (! optimize)));
23163 if (! optional_tbtab)
23164 return;
23166 /* Optional fields follow. Some are variable length. */
23168 /* Parameter types, left adjusted bit fields: 0 fixed, 10 single float,
23169 11 double float. */
23170 /* There is an entry for each parameter in a register, in the order that
23171 they occur in the parameter list. Any intervening arguments on the
23172 stack are ignored. If the list overflows a long (max possible length
23173 34 bits) then completely leave off all elements that don't fit. */
23174 /* Only emit this long if there was at least one parameter. */
23175 if (fixed_parms || float_parms)
23176 fprintf (file, "\t.long %d\n", parm_info);
23178 /* Offset from start of code to tb table. */
23179 fputs ("\t.long ", file);
23180 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
23181 RS6000_OUTPUT_BASENAME (file, fname);
23182 putc ('-', file);
23183 rs6000_output_function_entry (file, fname);
23184 putc ('\n', file);
23186 /* Interrupt handler mask. */
23187 /* Omit this long, since we never set the interrupt handler bit
23188 above. */
23190 /* Number of CTL (controlled storage) anchors. */
23191 /* Omit this long, since the has_ctl bit is never set above. */
23193 /* Displacement into stack of each CTL anchor. */
23194 /* Omit this list of longs, because there are no CTL anchors. */
23196 /* Length of function name. */
23197 if (*fname == '*')
23198 ++fname;
23199 fprintf (file, "\t.short %d\n", (int) strlen (fname));
23201 /* Function name. */
23202 assemble_string (fname, strlen (fname));
23204 /* Register for alloca automatic storage; this is always reg 31.
23205 Only emit this if the alloca bit was set above. */
23206 if (frame_pointer_needed)
23207 fputs ("\t.byte 31\n", file);
23209 fputs ("\t.align 2\n", file);
23213 /* A C compound statement that outputs the assembler code for a thunk
23214 function, used to implement C++ virtual function calls with
23215 multiple inheritance. The thunk acts as a wrapper around a virtual
23216 function, adjusting the implicit object parameter before handing
23217 control off to the real function.
23219 First, emit code to add the integer DELTA to the location that
23220 contains the incoming first argument. Assume that this argument
23221 contains a pointer, and is the one used to pass the `this' pointer
23222 in C++. This is the incoming argument *before* the function
23223 prologue, e.g. `%o0' on a sparc. The addition must preserve the
23224 values of all other incoming arguments.
23226 After the addition, emit code to jump to FUNCTION, which is a
23227 `FUNCTION_DECL'. This is a direct pure jump, not a call, and does
23228 not touch the return address. Hence returning from FUNCTION will
23229 return to whoever called the current `thunk'.
23231 The effect must be as if FUNCTION had been called directly with the
23232 adjusted first argument. This macro is responsible for emitting
23233 all of the code for a thunk function; output_function_prologue()
23234 and output_function_epilogue() are not invoked.
23236 The THUNK_FNDECL is redundant. (DELTA and FUNCTION have already
23237 been extracted from it.) It might possibly be useful on some
23238 targets, but probably not.
23240 If you do not define this macro, the target-independent code in the
23241 C++ frontend will generate a less efficient heavyweight thunk that
23242 calls FUNCTION instead of jumping to it. The generic approach does
23243 not support varargs. */
23245 static void
23246 rs6000_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
23247 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
23248 tree function)
23250 rtx this_rtx, insn, funexp;
23252 reload_completed = 1;
23253 epilogue_completed = 1;
23255 /* Mark the end of the (empty) prologue. */
23256 emit_note (NOTE_INSN_PROLOGUE_END);
23258 /* Find the "this" pointer. If the function returns a structure,
23259 the structure return pointer is in r3. */
23260 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
23261 this_rtx = gen_rtx_REG (Pmode, 4);
23262 else
23263 this_rtx = gen_rtx_REG (Pmode, 3);
23265 /* Apply the constant offset, if required. */
23266 if (delta)
23267 emit_insn (gen_add3_insn (this_rtx, this_rtx, GEN_INT (delta)));
23269 /* Apply the offset from the vtable, if required. */
23270 if (vcall_offset)
23272 rtx vcall_offset_rtx = GEN_INT (vcall_offset);
23273 rtx tmp = gen_rtx_REG (Pmode, 12);
23275 emit_move_insn (tmp, gen_rtx_MEM (Pmode, this_rtx));
23276 if (((unsigned HOST_WIDE_INT) vcall_offset) + 0x8000 >= 0x10000)
23278 emit_insn (gen_add3_insn (tmp, tmp, vcall_offset_rtx));
23279 emit_move_insn (tmp, gen_rtx_MEM (Pmode, tmp));
23281 else
23283 rtx loc = gen_rtx_PLUS (Pmode, tmp, vcall_offset_rtx);
23285 emit_move_insn (tmp, gen_rtx_MEM (Pmode, loc));
23287 emit_insn (gen_add3_insn (this_rtx, this_rtx, tmp));
23290 /* Generate a tail call to the target function. */
23291 if (!TREE_USED (function))
23293 assemble_external (function);
23294 TREE_USED (function) = 1;
23296 funexp = XEXP (DECL_RTL (function), 0);
23297 funexp = gen_rtx_MEM (FUNCTION_MODE, funexp);
23299 #if TARGET_MACHO
23300 if (MACHOPIC_INDIRECT)
23301 funexp = machopic_indirect_call_target (funexp);
23302 #endif
23304 /* gen_sibcall expects reload to convert scratch pseudo to LR so we must
23305 generate sibcall RTL explicitly. */
23306 insn = emit_call_insn (
23307 gen_rtx_PARALLEL (VOIDmode,
23308 gen_rtvec (4,
23309 gen_rtx_CALL (VOIDmode,
23310 funexp, const0_rtx),
23311 gen_rtx_USE (VOIDmode, const0_rtx),
23312 gen_rtx_USE (VOIDmode,
23313 gen_rtx_REG (SImode,
23314 LR_REGNO)),
23315 simple_return_rtx)));
23316 SIBLING_CALL_P (insn) = 1;
23317 emit_barrier ();
23319 /* Run just enough of rest_of_compilation to get the insns emitted.
23320 There's not really enough bulk here to make other passes such as
23321 instruction scheduling worth while. Note that use_thunk calls
23322 assemble_start_function and assemble_end_function. */
23323 insn = get_insns ();
23324 shorten_branches (insn);
23325 final_start_function (insn, file, 1);
23326 final (insn, file, 1);
23327 final_end_function ();
23329 reload_completed = 0;
23330 epilogue_completed = 0;
23333 /* A quick summary of the various types of 'constant-pool tables'
23334 under PowerPC:
23336 Target Flags Name One table per
23337 AIX (none) AIX TOC object file
23338 AIX -mfull-toc AIX TOC object file
23339 AIX -mminimal-toc AIX minimal TOC translation unit
23340 SVR4/EABI (none) SVR4 SDATA object file
23341 SVR4/EABI -fpic SVR4 pic object file
23342 SVR4/EABI -fPIC SVR4 PIC translation unit
23343 SVR4/EABI -mrelocatable EABI TOC function
23344 SVR4/EABI -maix AIX TOC object file
23345 SVR4/EABI -maix -mminimal-toc
23346 AIX minimal TOC translation unit
23348 Name Reg. Set by entries contains:
23349 made by addrs? fp? sum?
23351 AIX TOC 2 crt0 as Y option option
23352 AIX minimal TOC 30 prolog gcc Y Y option
23353 SVR4 SDATA 13 crt0 gcc N Y N
23354 SVR4 pic 30 prolog ld Y not yet N
23355 SVR4 PIC 30 prolog gcc Y option option
23356 EABI TOC 30 prolog gcc Y option option
23360 /* Hash functions for the hash table. */
23362 static unsigned
23363 rs6000_hash_constant (rtx k)
23365 enum rtx_code code = GET_CODE (k);
23366 enum machine_mode mode = GET_MODE (k);
23367 unsigned result = (code << 3) ^ mode;
23368 const char *format;
23369 int flen, fidx;
23371 format = GET_RTX_FORMAT (code);
23372 flen = strlen (format);
23373 fidx = 0;
23375 switch (code)
23377 case LABEL_REF:
23378 return result * 1231 + (unsigned) INSN_UID (XEXP (k, 0));
23380 case CONST_DOUBLE:
23381 if (mode != VOIDmode)
23382 return real_hash (CONST_DOUBLE_REAL_VALUE (k)) * result;
23383 flen = 2;
23384 break;
23386 case CODE_LABEL:
23387 fidx = 3;
23388 break;
23390 default:
23391 break;
23394 for (; fidx < flen; fidx++)
23395 switch (format[fidx])
23397 case 's':
23399 unsigned i, len;
23400 const char *str = XSTR (k, fidx);
23401 len = strlen (str);
23402 result = result * 613 + len;
23403 for (i = 0; i < len; i++)
23404 result = result * 613 + (unsigned) str[i];
23405 break;
23407 case 'u':
23408 case 'e':
23409 result = result * 1231 + rs6000_hash_constant (XEXP (k, fidx));
23410 break;
23411 case 'i':
23412 case 'n':
23413 result = result * 613 + (unsigned) XINT (k, fidx);
23414 break;
23415 case 'w':
23416 if (sizeof (unsigned) >= sizeof (HOST_WIDE_INT))
23417 result = result * 613 + (unsigned) XWINT (k, fidx);
23418 else
23420 size_t i;
23421 for (i = 0; i < sizeof (HOST_WIDE_INT) / sizeof (unsigned); i++)
23422 result = result * 613 + (unsigned) (XWINT (k, fidx)
23423 >> CHAR_BIT * i);
23425 break;
23426 case '0':
23427 break;
23428 default:
23429 gcc_unreachable ();
23432 return result;
23435 static unsigned
23436 toc_hash_function (const void *hash_entry)
23438 const struct toc_hash_struct *thc =
23439 (const struct toc_hash_struct *) hash_entry;
23440 return rs6000_hash_constant (thc->key) ^ thc->key_mode;
23443 /* Compare H1 and H2 for equivalence. */
23445 static int
23446 toc_hash_eq (const void *h1, const void *h2)
23448 rtx r1 = ((const struct toc_hash_struct *) h1)->key;
23449 rtx r2 = ((const struct toc_hash_struct *) h2)->key;
23451 if (((const struct toc_hash_struct *) h1)->key_mode
23452 != ((const struct toc_hash_struct *) h2)->key_mode)
23453 return 0;
23455 return rtx_equal_p (r1, r2);
23458 /* These are the names given by the C++ front-end to vtables, and
23459 vtable-like objects. Ideally, this logic should not be here;
23460 instead, there should be some programmatic way of inquiring as
23461 to whether or not an object is a vtable. */
23463 #define VTABLE_NAME_P(NAME) \
23464 (strncmp ("_vt.", name, strlen ("_vt.")) == 0 \
23465 || strncmp ("_ZTV", name, strlen ("_ZTV")) == 0 \
23466 || strncmp ("_ZTT", name, strlen ("_ZTT")) == 0 \
23467 || strncmp ("_ZTI", name, strlen ("_ZTI")) == 0 \
23468 || strncmp ("_ZTC", name, strlen ("_ZTC")) == 0)
23470 #ifdef NO_DOLLAR_IN_LABEL
23471 /* Return a GGC-allocated character string translating dollar signs in
23472 input NAME to underscores. Used by XCOFF ASM_OUTPUT_LABELREF. */
23474 const char *
23475 rs6000_xcoff_strip_dollar (const char *name)
23477 char *strip, *p;
23478 const char *q;
23479 size_t len;
23481 q = (const char *) strchr (name, '$');
23483 if (q == 0 || q == name)
23484 return name;
23486 len = strlen (name);
23487 strip = XALLOCAVEC (char, len + 1);
23488 strcpy (strip, name);
23489 p = strip + (q - name);
23490 while (p)
23492 *p = '_';
23493 p = strchr (p + 1, '$');
23496 return ggc_alloc_string (strip, len);
23498 #endif
23500 void
23501 rs6000_output_symbol_ref (FILE *file, rtx x)
23503 /* Currently C++ toc references to vtables can be emitted before it
23504 is decided whether the vtable is public or private. If this is
23505 the case, then the linker will eventually complain that there is
23506 a reference to an unknown section. Thus, for vtables only,
23507 we emit the TOC reference to reference the symbol and not the
23508 section. */
23509 const char *name = XSTR (x, 0);
23511 if (VTABLE_NAME_P (name))
23513 RS6000_OUTPUT_BASENAME (file, name);
23515 else
23516 assemble_name (file, name);
23519 /* Output a TOC entry. We derive the entry name from what is being
23520 written. */
23522 void
23523 output_toc (FILE *file, rtx x, int labelno, enum machine_mode mode)
23525 char buf[256];
23526 const char *name = buf;
23527 rtx base = x;
23528 HOST_WIDE_INT offset = 0;
23530 gcc_assert (!TARGET_NO_TOC);
23532 /* When the linker won't eliminate them, don't output duplicate
23533 TOC entries (this happens on AIX if there is any kind of TOC,
23534 and on SVR4 under -fPIC or -mrelocatable). Don't do this for
23535 CODE_LABELs. */
23536 if (TARGET_TOC && GET_CODE (x) != LABEL_REF)
23538 struct toc_hash_struct *h;
23539 void * * found;
23541 /* Create toc_hash_table. This can't be done at TARGET_OPTION_OVERRIDE
23542 time because GGC is not initialized at that point. */
23543 if (toc_hash_table == NULL)
23544 toc_hash_table = htab_create_ggc (1021, toc_hash_function,
23545 toc_hash_eq, NULL);
23547 h = ggc_alloc_toc_hash_struct ();
23548 h->key = x;
23549 h->key_mode = mode;
23550 h->labelno = labelno;
23552 found = htab_find_slot (toc_hash_table, h, INSERT);
23553 if (*found == NULL)
23554 *found = h;
23555 else /* This is indeed a duplicate.
23556 Set this label equal to that label. */
23558 fputs ("\t.set ", file);
23559 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
23560 fprintf (file, "%d,", labelno);
23561 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
23562 fprintf (file, "%d\n", ((*(const struct toc_hash_struct **)
23563 found)->labelno));
23565 #ifdef HAVE_AS_TLS
23566 if (TARGET_XCOFF && GET_CODE (x) == SYMBOL_REF
23567 && (SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_GLOBAL_DYNAMIC
23568 || SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC))
23570 fputs ("\t.set ", file);
23571 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LCM");
23572 fprintf (file, "%d,", labelno);
23573 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LCM");
23574 fprintf (file, "%d\n", ((*(const struct toc_hash_struct **)
23575 found)->labelno));
23577 #endif
23578 return;
23582 /* If we're going to put a double constant in the TOC, make sure it's
23583 aligned properly when strict alignment is on. */
23584 if (GET_CODE (x) == CONST_DOUBLE
23585 && STRICT_ALIGNMENT
23586 && GET_MODE_BITSIZE (mode) >= 64
23587 && ! (TARGET_NO_FP_IN_TOC && ! TARGET_MINIMAL_TOC)) {
23588 ASM_OUTPUT_ALIGN (file, 3);
23591 (*targetm.asm_out.internal_label) (file, "LC", labelno);
23593 /* Handle FP constants specially. Note that if we have a minimal
23594 TOC, things we put here aren't actually in the TOC, so we can allow
23595 FP constants. */
23596 if (GET_CODE (x) == CONST_DOUBLE &&
23597 (GET_MODE (x) == TFmode || GET_MODE (x) == TDmode))
23599 REAL_VALUE_TYPE rv;
23600 long k[4];
23602 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
23603 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
23604 REAL_VALUE_TO_TARGET_DECIMAL128 (rv, k);
23605 else
23606 REAL_VALUE_TO_TARGET_LONG_DOUBLE (rv, k);
23608 if (TARGET_64BIT)
23610 if (TARGET_ELF || TARGET_MINIMAL_TOC)
23611 fputs (DOUBLE_INT_ASM_OP, file);
23612 else
23613 fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
23614 k[0] & 0xffffffff, k[1] & 0xffffffff,
23615 k[2] & 0xffffffff, k[3] & 0xffffffff);
23616 fprintf (file, "0x%lx%08lx,0x%lx%08lx\n",
23617 k[WORDS_BIG_ENDIAN ? 0 : 1] & 0xffffffff,
23618 k[WORDS_BIG_ENDIAN ? 1 : 0] & 0xffffffff,
23619 k[WORDS_BIG_ENDIAN ? 2 : 3] & 0xffffffff,
23620 k[WORDS_BIG_ENDIAN ? 3 : 2] & 0xffffffff);
23621 return;
23623 else
23625 if (TARGET_ELF || TARGET_MINIMAL_TOC)
23626 fputs ("\t.long ", file);
23627 else
23628 fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
23629 k[0] & 0xffffffff, k[1] & 0xffffffff,
23630 k[2] & 0xffffffff, k[3] & 0xffffffff);
23631 fprintf (file, "0x%lx,0x%lx,0x%lx,0x%lx\n",
23632 k[0] & 0xffffffff, k[1] & 0xffffffff,
23633 k[2] & 0xffffffff, k[3] & 0xffffffff);
23634 return;
23637 else if (GET_CODE (x) == CONST_DOUBLE &&
23638 (GET_MODE (x) == DFmode || GET_MODE (x) == DDmode))
23640 REAL_VALUE_TYPE rv;
23641 long k[2];
23643 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
23645 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
23646 REAL_VALUE_TO_TARGET_DECIMAL64 (rv, k);
23647 else
23648 REAL_VALUE_TO_TARGET_DOUBLE (rv, k);
23650 if (TARGET_64BIT)
23652 if (TARGET_ELF || TARGET_MINIMAL_TOC)
23653 fputs (DOUBLE_INT_ASM_OP, file);
23654 else
23655 fprintf (file, "\t.tc FD_%lx_%lx[TC],",
23656 k[0] & 0xffffffff, k[1] & 0xffffffff);
23657 fprintf (file, "0x%lx%08lx\n",
23658 k[WORDS_BIG_ENDIAN ? 0 : 1] & 0xffffffff,
23659 k[WORDS_BIG_ENDIAN ? 1 : 0] & 0xffffffff);
23660 return;
23662 else
23664 if (TARGET_ELF || TARGET_MINIMAL_TOC)
23665 fputs ("\t.long ", file);
23666 else
23667 fprintf (file, "\t.tc FD_%lx_%lx[TC],",
23668 k[0] & 0xffffffff, k[1] & 0xffffffff);
23669 fprintf (file, "0x%lx,0x%lx\n",
23670 k[0] & 0xffffffff, k[1] & 0xffffffff);
23671 return;
23674 else if (GET_CODE (x) == CONST_DOUBLE &&
23675 (GET_MODE (x) == SFmode || GET_MODE (x) == SDmode))
23677 REAL_VALUE_TYPE rv;
23678 long l;
23680 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
23681 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
23682 REAL_VALUE_TO_TARGET_DECIMAL32 (rv, l);
23683 else
23684 REAL_VALUE_TO_TARGET_SINGLE (rv, l);
23686 if (TARGET_64BIT)
23688 if (TARGET_ELF || TARGET_MINIMAL_TOC)
23689 fputs (DOUBLE_INT_ASM_OP, file);
23690 else
23691 fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
23692 if (WORDS_BIG_ENDIAN)
23693 fprintf (file, "0x%lx00000000\n", l & 0xffffffff);
23694 else
23695 fprintf (file, "0x%lx\n", l & 0xffffffff);
23696 return;
23698 else
23700 if (TARGET_ELF || TARGET_MINIMAL_TOC)
23701 fputs ("\t.long ", file);
23702 else
23703 fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
23704 fprintf (file, "0x%lx\n", l & 0xffffffff);
23705 return;
23708 else if (GET_MODE (x) == VOIDmode && GET_CODE (x) == CONST_INT)
23710 unsigned HOST_WIDE_INT low;
23711 HOST_WIDE_INT high;
23713 low = INTVAL (x) & 0xffffffff;
23714 high = (HOST_WIDE_INT) INTVAL (x) >> 32;
23716 /* TOC entries are always Pmode-sized, so when big-endian
23717 smaller integer constants in the TOC need to be padded.
23718 (This is still a win over putting the constants in
23719 a separate constant pool, because then we'd have
23720 to have both a TOC entry _and_ the actual constant.)
23722 For a 32-bit target, CONST_INT values are loaded and shifted
23723 entirely within `low' and can be stored in one TOC entry. */
23725 /* It would be easy to make this work, but it doesn't now. */
23726 gcc_assert (!TARGET_64BIT || POINTER_SIZE >= GET_MODE_BITSIZE (mode));
23728 if (WORDS_BIG_ENDIAN && POINTER_SIZE > GET_MODE_BITSIZE (mode))
23730 low |= high << 32;
23731 low <<= POINTER_SIZE - GET_MODE_BITSIZE (mode);
23732 high = (HOST_WIDE_INT) low >> 32;
23733 low &= 0xffffffff;
23736 if (TARGET_64BIT)
23738 if (TARGET_ELF || TARGET_MINIMAL_TOC)
23739 fputs (DOUBLE_INT_ASM_OP, file);
23740 else
23741 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
23742 (long) high & 0xffffffff, (long) low & 0xffffffff);
23743 fprintf (file, "0x%lx%08lx\n",
23744 (long) high & 0xffffffff, (long) low & 0xffffffff);
23745 return;
23747 else
23749 if (POINTER_SIZE < GET_MODE_BITSIZE (mode))
23751 if (TARGET_ELF || TARGET_MINIMAL_TOC)
23752 fputs ("\t.long ", file);
23753 else
23754 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
23755 (long) high & 0xffffffff, (long) low & 0xffffffff);
23756 fprintf (file, "0x%lx,0x%lx\n",
23757 (long) high & 0xffffffff, (long) low & 0xffffffff);
23759 else
23761 if (TARGET_ELF || TARGET_MINIMAL_TOC)
23762 fputs ("\t.long ", file);
23763 else
23764 fprintf (file, "\t.tc IS_%lx[TC],", (long) low & 0xffffffff);
23765 fprintf (file, "0x%lx\n", (long) low & 0xffffffff);
23767 return;
23771 if (GET_CODE (x) == CONST)
23773 gcc_assert (GET_CODE (XEXP (x, 0)) == PLUS
23774 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT);
23776 base = XEXP (XEXP (x, 0), 0);
23777 offset = INTVAL (XEXP (XEXP (x, 0), 1));
23780 switch (GET_CODE (base))
23782 case SYMBOL_REF:
23783 name = XSTR (base, 0);
23784 break;
23786 case LABEL_REF:
23787 ASM_GENERATE_INTERNAL_LABEL (buf, "L",
23788 CODE_LABEL_NUMBER (XEXP (base, 0)));
23789 break;
23791 case CODE_LABEL:
23792 ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (base));
23793 break;
23795 default:
23796 gcc_unreachable ();
23799 if (TARGET_ELF || TARGET_MINIMAL_TOC)
23800 fputs (TARGET_32BIT ? "\t.long " : DOUBLE_INT_ASM_OP, file);
23801 else
23803 fputs ("\t.tc ", file);
23804 RS6000_OUTPUT_BASENAME (file, name);
23806 if (offset < 0)
23807 fprintf (file, ".N" HOST_WIDE_INT_PRINT_UNSIGNED, - offset);
23808 else if (offset)
23809 fprintf (file, ".P" HOST_WIDE_INT_PRINT_UNSIGNED, offset);
23811 /* Mark large TOC symbols on AIX with [TE] so they are mapped
23812 after other TOC symbols, reducing overflow of small TOC access
23813 to [TC] symbols. */
23814 fputs (TARGET_XCOFF && TARGET_CMODEL != CMODEL_SMALL
23815 ? "[TE]," : "[TC],", file);
23818 /* Currently C++ toc references to vtables can be emitted before it
23819 is decided whether the vtable is public or private. If this is
23820 the case, then the linker will eventually complain that there is
23821 a TOC reference to an unknown section. Thus, for vtables only,
23822 we emit the TOC reference to reference the symbol and not the
23823 section. */
23824 if (VTABLE_NAME_P (name))
23826 RS6000_OUTPUT_BASENAME (file, name);
23827 if (offset < 0)
23828 fprintf (file, HOST_WIDE_INT_PRINT_DEC, offset);
23829 else if (offset > 0)
23830 fprintf (file, "+" HOST_WIDE_INT_PRINT_DEC, offset);
23832 else
23833 output_addr_const (file, x);
23835 #if HAVE_AS_TLS
23836 if (TARGET_XCOFF && GET_CODE (base) == SYMBOL_REF
23837 && SYMBOL_REF_TLS_MODEL (base) != 0)
23839 if (SYMBOL_REF_TLS_MODEL (base) == TLS_MODEL_LOCAL_EXEC)
23840 fputs ("@le", file);
23841 else if (SYMBOL_REF_TLS_MODEL (base) == TLS_MODEL_INITIAL_EXEC)
23842 fputs ("@ie", file);
23843 /* Use global-dynamic for local-dynamic. */
23844 else if (SYMBOL_REF_TLS_MODEL (base) == TLS_MODEL_GLOBAL_DYNAMIC
23845 || SYMBOL_REF_TLS_MODEL (base) == TLS_MODEL_LOCAL_DYNAMIC)
23847 putc ('\n', file);
23848 (*targetm.asm_out.internal_label) (file, "LCM", labelno);
23849 fputs ("\t.tc .", file);
23850 RS6000_OUTPUT_BASENAME (file, name);
23851 fputs ("[TC],", file);
23852 output_addr_const (file, x);
23853 fputs ("@m", file);
23856 #endif
23858 putc ('\n', file);
23861 /* Output an assembler pseudo-op to write an ASCII string of N characters
23862 starting at P to FILE.
23864 On the RS/6000, we have to do this using the .byte operation and
23865 write out special characters outside the quoted string.
23866 Also, the assembler is broken; very long strings are truncated,
23867 so we must artificially break them up early. */
23869 void
23870 output_ascii (FILE *file, const char *p, int n)
23872 char c;
23873 int i, count_string;
23874 const char *for_string = "\t.byte \"";
23875 const char *for_decimal = "\t.byte ";
23876 const char *to_close = NULL;
23878 count_string = 0;
23879 for (i = 0; i < n; i++)
23881 c = *p++;
23882 if (c >= ' ' && c < 0177)
23884 if (for_string)
23885 fputs (for_string, file);
23886 putc (c, file);
23888 /* Write two quotes to get one. */
23889 if (c == '"')
23891 putc (c, file);
23892 ++count_string;
23895 for_string = NULL;
23896 for_decimal = "\"\n\t.byte ";
23897 to_close = "\"\n";
23898 ++count_string;
23900 if (count_string >= 512)
23902 fputs (to_close, file);
23904 for_string = "\t.byte \"";
23905 for_decimal = "\t.byte ";
23906 to_close = NULL;
23907 count_string = 0;
23910 else
23912 if (for_decimal)
23913 fputs (for_decimal, file);
23914 fprintf (file, "%d", c);
23916 for_string = "\n\t.byte \"";
23917 for_decimal = ", ";
23918 to_close = "\n";
23919 count_string = 0;
23923 /* Now close the string if we have written one. Then end the line. */
23924 if (to_close)
23925 fputs (to_close, file);
23928 /* Generate a unique section name for FILENAME for a section type
23929 represented by SECTION_DESC. Output goes into BUF.
23931 SECTION_DESC can be any string, as long as it is different for each
23932 possible section type.
23934 We name the section in the same manner as xlc. The name begins with an
23935 underscore followed by the filename (after stripping any leading directory
23936 names) with the last period replaced by the string SECTION_DESC. If
23937 FILENAME does not contain a period, SECTION_DESC is appended to the end of
23938 the name. */
23940 void
23941 rs6000_gen_section_name (char **buf, const char *filename,
23942 const char *section_desc)
23944 const char *q, *after_last_slash, *last_period = 0;
23945 char *p;
23946 int len;
23948 after_last_slash = filename;
23949 for (q = filename; *q; q++)
23951 if (*q == '/')
23952 after_last_slash = q + 1;
23953 else if (*q == '.')
23954 last_period = q;
23957 len = strlen (after_last_slash) + strlen (section_desc) + 2;
23958 *buf = (char *) xmalloc (len);
23960 p = *buf;
23961 *p++ = '_';
23963 for (q = after_last_slash; *q; q++)
23965 if (q == last_period)
23967 strcpy (p, section_desc);
23968 p += strlen (section_desc);
23969 break;
23972 else if (ISALNUM (*q))
23973 *p++ = *q;
23976 if (last_period == 0)
23977 strcpy (p, section_desc);
23978 else
23979 *p = '\0';
23982 /* Emit profile function. */
23984 void
23985 output_profile_hook (int labelno ATTRIBUTE_UNUSED)
23987 /* Non-standard profiling for kernels, which just saves LR then calls
23988 _mcount without worrying about arg saves. The idea is to change
23989 the function prologue as little as possible as it isn't easy to
23990 account for arg save/restore code added just for _mcount. */
23991 if (TARGET_PROFILE_KERNEL)
23992 return;
23994 if (DEFAULT_ABI == ABI_AIX)
23996 #ifndef NO_PROFILE_COUNTERS
23997 # define NO_PROFILE_COUNTERS 0
23998 #endif
23999 if (NO_PROFILE_COUNTERS)
24000 emit_library_call (init_one_libfunc (RS6000_MCOUNT),
24001 LCT_NORMAL, VOIDmode, 0);
24002 else
24004 char buf[30];
24005 const char *label_name;
24006 rtx fun;
24008 ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
24009 label_name = ggc_strdup ((*targetm.strip_name_encoding) (buf));
24010 fun = gen_rtx_SYMBOL_REF (Pmode, label_name);
24012 emit_library_call (init_one_libfunc (RS6000_MCOUNT),
24013 LCT_NORMAL, VOIDmode, 1, fun, Pmode);
24016 else if (DEFAULT_ABI == ABI_DARWIN)
24018 const char *mcount_name = RS6000_MCOUNT;
24019 int caller_addr_regno = LR_REGNO;
24021 /* Be conservative and always set this, at least for now. */
24022 crtl->uses_pic_offset_table = 1;
24024 #if TARGET_MACHO
24025 /* For PIC code, set up a stub and collect the caller's address
24026 from r0, which is where the prologue puts it. */
24027 if (MACHOPIC_INDIRECT
24028 && crtl->uses_pic_offset_table)
24029 caller_addr_regno = 0;
24030 #endif
24031 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, mcount_name),
24032 LCT_NORMAL, VOIDmode, 1,
24033 gen_rtx_REG (Pmode, caller_addr_regno), Pmode);
24037 /* Write function profiler code. */
24039 void
24040 output_function_profiler (FILE *file, int labelno)
24042 char buf[100];
24044 switch (DEFAULT_ABI)
24046 default:
24047 gcc_unreachable ();
24049 case ABI_V4:
24050 if (!TARGET_32BIT)
24052 warning (0, "no profiling of 64-bit code for this ABI");
24053 return;
24055 ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
24056 fprintf (file, "\tmflr %s\n", reg_names[0]);
24057 if (NO_PROFILE_COUNTERS)
24059 asm_fprintf (file, "\tstw %s,4(%s)\n",
24060 reg_names[0], reg_names[1]);
24062 else if (TARGET_SECURE_PLT && flag_pic)
24064 if (TARGET_LINK_STACK)
24066 char name[32];
24067 get_ppc476_thunk_name (name);
24068 asm_fprintf (file, "\tbl %s\n", name);
24070 else
24071 asm_fprintf (file, "\tbcl 20,31,1f\n1:\n");
24072 asm_fprintf (file, "\tstw %s,4(%s)\n",
24073 reg_names[0], reg_names[1]);
24074 asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
24075 asm_fprintf (file, "\taddis %s,%s,",
24076 reg_names[12], reg_names[12]);
24077 assemble_name (file, buf);
24078 asm_fprintf (file, "-1b@ha\n\tla %s,", reg_names[0]);
24079 assemble_name (file, buf);
24080 asm_fprintf (file, "-1b@l(%s)\n", reg_names[12]);
24082 else if (flag_pic == 1)
24084 fputs ("\tbl _GLOBAL_OFFSET_TABLE_@local-4\n", file);
24085 asm_fprintf (file, "\tstw %s,4(%s)\n",
24086 reg_names[0], reg_names[1]);
24087 asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
24088 asm_fprintf (file, "\tlwz %s,", reg_names[0]);
24089 assemble_name (file, buf);
24090 asm_fprintf (file, "@got(%s)\n", reg_names[12]);
24092 else if (flag_pic > 1)
24094 asm_fprintf (file, "\tstw %s,4(%s)\n",
24095 reg_names[0], reg_names[1]);
24096 /* Now, we need to get the address of the label. */
24097 if (TARGET_LINK_STACK)
24099 char name[32];
24100 get_ppc476_thunk_name (name);
24101 asm_fprintf (file, "\tbl %s\n\tb 1f\n\t.long ", name);
24102 assemble_name (file, buf);
24103 fputs ("-.\n1:", file);
24104 asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
24105 asm_fprintf (file, "\taddi %s,%s,4\n",
24106 reg_names[11], reg_names[11]);
24108 else
24110 fputs ("\tbcl 20,31,1f\n\t.long ", file);
24111 assemble_name (file, buf);
24112 fputs ("-.\n1:", file);
24113 asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
24115 asm_fprintf (file, "\tlwz %s,0(%s)\n",
24116 reg_names[0], reg_names[11]);
24117 asm_fprintf (file, "\tadd %s,%s,%s\n",
24118 reg_names[0], reg_names[0], reg_names[11]);
24120 else
24122 asm_fprintf (file, "\tlis %s,", reg_names[12]);
24123 assemble_name (file, buf);
24124 fputs ("@ha\n", file);
24125 asm_fprintf (file, "\tstw %s,4(%s)\n",
24126 reg_names[0], reg_names[1]);
24127 asm_fprintf (file, "\tla %s,", reg_names[0]);
24128 assemble_name (file, buf);
24129 asm_fprintf (file, "@l(%s)\n", reg_names[12]);
24132 /* ABI_V4 saves the static chain reg with ASM_OUTPUT_REG_PUSH. */
24133 fprintf (file, "\tbl %s%s\n",
24134 RS6000_MCOUNT, flag_pic ? "@plt" : "");
24135 break;
24137 case ABI_AIX:
24138 case ABI_DARWIN:
24139 if (!TARGET_PROFILE_KERNEL)
24141 /* Don't do anything, done in output_profile_hook (). */
24143 else
24145 gcc_assert (!TARGET_32BIT);
24147 asm_fprintf (file, "\tmflr %s\n", reg_names[0]);
24148 asm_fprintf (file, "\tstd %s,16(%s)\n", reg_names[0], reg_names[1]);
24150 if (cfun->static_chain_decl != NULL)
24152 asm_fprintf (file, "\tstd %s,24(%s)\n",
24153 reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
24154 fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
24155 asm_fprintf (file, "\tld %s,24(%s)\n",
24156 reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
24158 else
24159 fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
24161 break;
24167 /* The following variable value is the last issued insn. */
24169 static rtx last_scheduled_insn;
24171 /* The following variable helps to balance issuing of load and
24172 store instructions */
24174 static int load_store_pendulum;
24176 /* Power4 load update and store update instructions are cracked into a
24177 load or store and an integer insn which are executed in the same cycle.
24178 Branches have their own dispatch slot which does not count against the
24179 GCC issue rate, but it changes the program flow so there are no other
24180 instructions to issue in this cycle. */
24182 static int
24183 rs6000_variable_issue_1 (rtx insn, int more)
24185 last_scheduled_insn = insn;
24186 if (GET_CODE (PATTERN (insn)) == USE
24187 || GET_CODE (PATTERN (insn)) == CLOBBER)
24189 cached_can_issue_more = more;
24190 return cached_can_issue_more;
24193 if (insn_terminates_group_p (insn, current_group))
24195 cached_can_issue_more = 0;
24196 return cached_can_issue_more;
24199 /* If no reservation, but reach here */
24200 if (recog_memoized (insn) < 0)
24201 return more;
24203 if (rs6000_sched_groups)
24205 if (is_microcoded_insn (insn))
24206 cached_can_issue_more = 0;
24207 else if (is_cracked_insn (insn))
24208 cached_can_issue_more = more > 2 ? more - 2 : 0;
24209 else
24210 cached_can_issue_more = more - 1;
24212 return cached_can_issue_more;
24215 if (rs6000_cpu_attr == CPU_CELL && is_nonpipeline_insn (insn))
24216 return 0;
24218 cached_can_issue_more = more - 1;
24219 return cached_can_issue_more;
24222 static int
24223 rs6000_variable_issue (FILE *stream, int verbose, rtx insn, int more)
24225 int r = rs6000_variable_issue_1 (insn, more);
24226 if (verbose)
24227 fprintf (stream, "// rs6000_variable_issue (more = %d) = %d\n", more, r);
24228 return r;
24231 /* Adjust the cost of a scheduling dependency. Return the new cost of
24232 a dependency LINK or INSN on DEP_INSN. COST is the current cost. */
24234 static int
24235 rs6000_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
24237 enum attr_type attr_type;
24239 if (! recog_memoized (insn))
24240 return 0;
24242 switch (REG_NOTE_KIND (link))
24244 case REG_DEP_TRUE:
24246 /* Data dependency; DEP_INSN writes a register that INSN reads
24247 some cycles later. */
24249 /* Separate a load from a narrower, dependent store. */
24250 if (rs6000_sched_groups
24251 && GET_CODE (PATTERN (insn)) == SET
24252 && GET_CODE (PATTERN (dep_insn)) == SET
24253 && GET_CODE (XEXP (PATTERN (insn), 1)) == MEM
24254 && GET_CODE (XEXP (PATTERN (dep_insn), 0)) == MEM
24255 && (GET_MODE_SIZE (GET_MODE (XEXP (PATTERN (insn), 1)))
24256 > GET_MODE_SIZE (GET_MODE (XEXP (PATTERN (dep_insn), 0)))))
24257 return cost + 14;
24259 attr_type = get_attr_type (insn);
24261 switch (attr_type)
24263 case TYPE_JMPREG:
24264 /* Tell the first scheduling pass about the latency between
24265 a mtctr and bctr (and mtlr and br/blr). The first
24266 scheduling pass will not know about this latency since
24267 the mtctr instruction, which has the latency associated
24268 to it, will be generated by reload. */
24269 return 4;
24270 case TYPE_BRANCH:
24271 /* Leave some extra cycles between a compare and its
24272 dependent branch, to inhibit expensive mispredicts. */
24273 if ((rs6000_cpu_attr == CPU_PPC603
24274 || rs6000_cpu_attr == CPU_PPC604
24275 || rs6000_cpu_attr == CPU_PPC604E
24276 || rs6000_cpu_attr == CPU_PPC620
24277 || rs6000_cpu_attr == CPU_PPC630
24278 || rs6000_cpu_attr == CPU_PPC750
24279 || rs6000_cpu_attr == CPU_PPC7400
24280 || rs6000_cpu_attr == CPU_PPC7450
24281 || rs6000_cpu_attr == CPU_PPCE5500
24282 || rs6000_cpu_attr == CPU_PPCE6500
24283 || rs6000_cpu_attr == CPU_POWER4
24284 || rs6000_cpu_attr == CPU_POWER5
24285 || rs6000_cpu_attr == CPU_POWER7
24286 || rs6000_cpu_attr == CPU_POWER8
24287 || rs6000_cpu_attr == CPU_CELL)
24288 && recog_memoized (dep_insn)
24289 && (INSN_CODE (dep_insn) >= 0))
24291 switch (get_attr_type (dep_insn))
24293 case TYPE_CMP:
24294 case TYPE_COMPARE:
24295 case TYPE_DELAYED_COMPARE:
24296 case TYPE_IMUL_COMPARE:
24297 case TYPE_LMUL_COMPARE:
24298 case TYPE_FPCOMPARE:
24299 case TYPE_CR_LOGICAL:
24300 case TYPE_DELAYED_CR:
24301 return cost + 2;
24302 default:
24303 break;
24305 break;
24307 case TYPE_STORE:
24308 case TYPE_STORE_U:
24309 case TYPE_STORE_UX:
24310 case TYPE_FPSTORE:
24311 case TYPE_FPSTORE_U:
24312 case TYPE_FPSTORE_UX:
24313 if ((rs6000_cpu == PROCESSOR_POWER6)
24314 && recog_memoized (dep_insn)
24315 && (INSN_CODE (dep_insn) >= 0))
24318 if (GET_CODE (PATTERN (insn)) != SET)
24319 /* If this happens, we have to extend this to schedule
24320 optimally. Return default for now. */
24321 return cost;
24323 /* Adjust the cost for the case where the value written
24324 by a fixed point operation is used as the address
24325 gen value on a store. */
24326 switch (get_attr_type (dep_insn))
24328 case TYPE_LOAD:
24329 case TYPE_LOAD_U:
24330 case TYPE_LOAD_UX:
24331 case TYPE_CNTLZ:
24333 if (! store_data_bypass_p (dep_insn, insn))
24334 return 4;
24335 break;
24337 case TYPE_LOAD_EXT:
24338 case TYPE_LOAD_EXT_U:
24339 case TYPE_LOAD_EXT_UX:
24340 case TYPE_VAR_SHIFT_ROTATE:
24341 case TYPE_VAR_DELAYED_COMPARE:
24343 if (! store_data_bypass_p (dep_insn, insn))
24344 return 6;
24345 break;
24347 case TYPE_INTEGER:
24348 case TYPE_COMPARE:
24349 case TYPE_FAST_COMPARE:
24350 case TYPE_EXTS:
24351 case TYPE_SHIFT:
24352 case TYPE_INSERT_WORD:
24353 case TYPE_INSERT_DWORD:
24354 case TYPE_FPLOAD_U:
24355 case TYPE_FPLOAD_UX:
24356 case TYPE_STORE_U:
24357 case TYPE_STORE_UX:
24358 case TYPE_FPSTORE_U:
24359 case TYPE_FPSTORE_UX:
24361 if (! store_data_bypass_p (dep_insn, insn))
24362 return 3;
24363 break;
24365 case TYPE_IMUL:
24366 case TYPE_IMUL2:
24367 case TYPE_IMUL3:
24368 case TYPE_LMUL:
24369 case TYPE_IMUL_COMPARE:
24370 case TYPE_LMUL_COMPARE:
24372 if (! store_data_bypass_p (dep_insn, insn))
24373 return 17;
24374 break;
24376 case TYPE_IDIV:
24378 if (! store_data_bypass_p (dep_insn, insn))
24379 return 45;
24380 break;
24382 case TYPE_LDIV:
24384 if (! store_data_bypass_p (dep_insn, insn))
24385 return 57;
24386 break;
24388 default:
24389 break;
24392 break;
24394 case TYPE_LOAD:
24395 case TYPE_LOAD_U:
24396 case TYPE_LOAD_UX:
24397 case TYPE_LOAD_EXT:
24398 case TYPE_LOAD_EXT_U:
24399 case TYPE_LOAD_EXT_UX:
24400 if ((rs6000_cpu == PROCESSOR_POWER6)
24401 && recog_memoized (dep_insn)
24402 && (INSN_CODE (dep_insn) >= 0))
24405 /* Adjust the cost for the case where the value written
24406 by a fixed point instruction is used within the address
24407 gen portion of a subsequent load(u)(x) */
24408 switch (get_attr_type (dep_insn))
24410 case TYPE_LOAD:
24411 case TYPE_LOAD_U:
24412 case TYPE_LOAD_UX:
24413 case TYPE_CNTLZ:
24415 if (set_to_load_agen (dep_insn, insn))
24416 return 4;
24417 break;
24419 case TYPE_LOAD_EXT:
24420 case TYPE_LOAD_EXT_U:
24421 case TYPE_LOAD_EXT_UX:
24422 case TYPE_VAR_SHIFT_ROTATE:
24423 case TYPE_VAR_DELAYED_COMPARE:
24425 if (set_to_load_agen (dep_insn, insn))
24426 return 6;
24427 break;
24429 case TYPE_INTEGER:
24430 case TYPE_COMPARE:
24431 case TYPE_FAST_COMPARE:
24432 case TYPE_EXTS:
24433 case TYPE_SHIFT:
24434 case TYPE_INSERT_WORD:
24435 case TYPE_INSERT_DWORD:
24436 case TYPE_FPLOAD_U:
24437 case TYPE_FPLOAD_UX:
24438 case TYPE_STORE_U:
24439 case TYPE_STORE_UX:
24440 case TYPE_FPSTORE_U:
24441 case TYPE_FPSTORE_UX:
24443 if (set_to_load_agen (dep_insn, insn))
24444 return 3;
24445 break;
24447 case TYPE_IMUL:
24448 case TYPE_IMUL2:
24449 case TYPE_IMUL3:
24450 case TYPE_LMUL:
24451 case TYPE_IMUL_COMPARE:
24452 case TYPE_LMUL_COMPARE:
24454 if (set_to_load_agen (dep_insn, insn))
24455 return 17;
24456 break;
24458 case TYPE_IDIV:
24460 if (set_to_load_agen (dep_insn, insn))
24461 return 45;
24462 break;
24464 case TYPE_LDIV:
24466 if (set_to_load_agen (dep_insn, insn))
24467 return 57;
24468 break;
24470 default:
24471 break;
24474 break;
24476 case TYPE_FPLOAD:
24477 if ((rs6000_cpu == PROCESSOR_POWER6)
24478 && recog_memoized (dep_insn)
24479 && (INSN_CODE (dep_insn) >= 0)
24480 && (get_attr_type (dep_insn) == TYPE_MFFGPR))
24481 return 2;
24483 default:
24484 break;
24487 /* Fall out to return default cost. */
24489 break;
24491 case REG_DEP_OUTPUT:
24492 /* Output dependency; DEP_INSN writes a register that INSN writes some
24493 cycles later. */
24494 if ((rs6000_cpu == PROCESSOR_POWER6)
24495 && recog_memoized (dep_insn)
24496 && (INSN_CODE (dep_insn) >= 0))
24498 attr_type = get_attr_type (insn);
24500 switch (attr_type)
24502 case TYPE_FP:
24503 if (get_attr_type (dep_insn) == TYPE_FP)
24504 return 1;
24505 break;
24506 case TYPE_FPLOAD:
24507 if (get_attr_type (dep_insn) == TYPE_MFFGPR)
24508 return 2;
24509 break;
24510 default:
24511 break;
24514 case REG_DEP_ANTI:
24515 /* Anti dependency; DEP_INSN reads a register that INSN writes some
24516 cycles later. */
24517 return 0;
24519 default:
24520 gcc_unreachable ();
24523 return cost;
24526 /* Debug version of rs6000_adjust_cost. */
24528 static int
24529 rs6000_debug_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
24531 int ret = rs6000_adjust_cost (insn, link, dep_insn, cost);
24533 if (ret != cost)
24535 const char *dep;
24537 switch (REG_NOTE_KIND (link))
24539 default: dep = "unknown depencency"; break;
24540 case REG_DEP_TRUE: dep = "data dependency"; break;
24541 case REG_DEP_OUTPUT: dep = "output dependency"; break;
24542 case REG_DEP_ANTI: dep = "anti depencency"; break;
24545 fprintf (stderr,
24546 "\nrs6000_adjust_cost, final cost = %d, orig cost = %d, "
24547 "%s, insn:\n", ret, cost, dep);
24549 debug_rtx (insn);
24552 return ret;
24555 /* The function returns a true if INSN is microcoded.
24556 Return false otherwise. */
24558 static bool
24559 is_microcoded_insn (rtx insn)
24561 if (!insn || !NONDEBUG_INSN_P (insn)
24562 || GET_CODE (PATTERN (insn)) == USE
24563 || GET_CODE (PATTERN (insn)) == CLOBBER)
24564 return false;
24566 if (rs6000_cpu_attr == CPU_CELL)
24567 return get_attr_cell_micro (insn) == CELL_MICRO_ALWAYS;
24569 if (rs6000_sched_groups
24570 && (rs6000_cpu == PROCESSOR_POWER4 || rs6000_cpu == PROCESSOR_POWER5))
24572 enum attr_type type = get_attr_type (insn);
24573 if (type == TYPE_LOAD_EXT_U
24574 || type == TYPE_LOAD_EXT_UX
24575 || type == TYPE_LOAD_UX
24576 || type == TYPE_STORE_UX
24577 || type == TYPE_MFCR)
24578 return true;
24581 return false;
24584 /* The function returns true if INSN is cracked into 2 instructions
24585 by the processor (and therefore occupies 2 issue slots). */
24587 static bool
24588 is_cracked_insn (rtx insn)
24590 if (!insn || !NONDEBUG_INSN_P (insn)
24591 || GET_CODE (PATTERN (insn)) == USE
24592 || GET_CODE (PATTERN (insn)) == CLOBBER)
24593 return false;
24595 if (rs6000_sched_groups
24596 && (rs6000_cpu == PROCESSOR_POWER4 || rs6000_cpu == PROCESSOR_POWER5))
24598 enum attr_type type = get_attr_type (insn);
24599 if (type == TYPE_LOAD_U || type == TYPE_STORE_U
24600 || type == TYPE_FPLOAD_U || type == TYPE_FPSTORE_U
24601 || type == TYPE_FPLOAD_UX || type == TYPE_FPSTORE_UX
24602 || type == TYPE_LOAD_EXT || type == TYPE_DELAYED_CR
24603 || type == TYPE_COMPARE || type == TYPE_DELAYED_COMPARE
24604 || type == TYPE_IMUL_COMPARE || type == TYPE_LMUL_COMPARE
24605 || type == TYPE_IDIV || type == TYPE_LDIV
24606 || type == TYPE_INSERT_WORD)
24607 return true;
24610 return false;
24613 /* The function returns true if INSN can be issued only from
24614 the branch slot. */
24616 static bool
24617 is_branch_slot_insn (rtx insn)
24619 if (!insn || !NONDEBUG_INSN_P (insn)
24620 || GET_CODE (PATTERN (insn)) == USE
24621 || GET_CODE (PATTERN (insn)) == CLOBBER)
24622 return false;
24624 if (rs6000_sched_groups)
24626 enum attr_type type = get_attr_type (insn);
24627 if (type == TYPE_BRANCH || type == TYPE_JMPREG)
24628 return true;
24629 return false;
24632 return false;
24635 /* The function returns true if out_inst sets a value that is
24636 used in the address generation computation of in_insn */
24637 static bool
24638 set_to_load_agen (rtx out_insn, rtx in_insn)
24640 rtx out_set, in_set;
24642 /* For performance reasons, only handle the simple case where
24643 both loads are a single_set. */
24644 out_set = single_set (out_insn);
24645 if (out_set)
24647 in_set = single_set (in_insn);
24648 if (in_set)
24649 return reg_mentioned_p (SET_DEST (out_set), SET_SRC (in_set));
24652 return false;
24655 /* Try to determine base/offset/size parts of the given MEM.
24656 Return true if successful, false if all the values couldn't
24657 be determined.
24659 This function only looks for REG or REG+CONST address forms.
24660 REG+REG address form will return false. */
24662 static bool
24663 get_memref_parts (rtx mem, rtx *base, HOST_WIDE_INT *offset,
24664 HOST_WIDE_INT *size)
24666 rtx addr_rtx;
24667 if MEM_SIZE_KNOWN_P (mem)
24668 *size = MEM_SIZE (mem);
24669 else
24670 return false;
24672 if (GET_CODE (XEXP (mem, 0)) == PRE_MODIFY)
24673 addr_rtx = XEXP (XEXP (mem, 0), 1);
24674 else
24675 addr_rtx = (XEXP (mem, 0));
24677 if (GET_CODE (addr_rtx) == REG)
24679 *base = addr_rtx;
24680 *offset = 0;
24682 else if (GET_CODE (addr_rtx) == PLUS
24683 && CONST_INT_P (XEXP (addr_rtx, 1)))
24685 *base = XEXP (addr_rtx, 0);
24686 *offset = INTVAL (XEXP (addr_rtx, 1));
24688 else
24689 return false;
24691 return true;
24694 /* The function returns true if the target storage location of
24695 mem1 is adjacent to the target storage location of mem2 */
24696 /* Return 1 if memory locations are adjacent. */
24698 static bool
24699 adjacent_mem_locations (rtx mem1, rtx mem2)
24701 rtx reg1, reg2;
24702 HOST_WIDE_INT off1, size1, off2, size2;
24704 if (get_memref_parts (mem1, &reg1, &off1, &size1)
24705 && get_memref_parts (mem2, &reg2, &off2, &size2))
24706 return ((REGNO (reg1) == REGNO (reg2))
24707 && ((off1 + size1 == off2)
24708 || (off2 + size2 == off1)));
24710 return false;
24713 /* This function returns true if it can be determined that the two MEM
24714 locations overlap by at least 1 byte based on base reg/offset/size. */
24716 static bool
24717 mem_locations_overlap (rtx mem1, rtx mem2)
24719 rtx reg1, reg2;
24720 HOST_WIDE_INT off1, size1, off2, size2;
24722 if (get_memref_parts (mem1, &reg1, &off1, &size1)
24723 && get_memref_parts (mem2, &reg2, &off2, &size2))
24724 return ((REGNO (reg1) == REGNO (reg2))
24725 && (((off1 <= off2) && (off1 + size1 > off2))
24726 || ((off2 <= off1) && (off2 + size2 > off1))));
24728 return false;
24731 /* A C statement (sans semicolon) to update the integer scheduling
24732 priority INSN_PRIORITY (INSN). Increase the priority to execute the
24733 INSN earlier, reduce the priority to execute INSN later. Do not
24734 define this macro if you do not need to adjust the scheduling
24735 priorities of insns. */
24737 static int
24738 rs6000_adjust_priority (rtx insn ATTRIBUTE_UNUSED, int priority)
24740 rtx load_mem, str_mem;
24741 /* On machines (like the 750) which have asymmetric integer units,
24742 where one integer unit can do multiply and divides and the other
24743 can't, reduce the priority of multiply/divide so it is scheduled
24744 before other integer operations. */
24746 #if 0
24747 if (! INSN_P (insn))
24748 return priority;
24750 if (GET_CODE (PATTERN (insn)) == USE)
24751 return priority;
24753 switch (rs6000_cpu_attr) {
24754 case CPU_PPC750:
24755 switch (get_attr_type (insn))
24757 default:
24758 break;
24760 case TYPE_IMUL:
24761 case TYPE_IDIV:
24762 fprintf (stderr, "priority was %#x (%d) before adjustment\n",
24763 priority, priority);
24764 if (priority >= 0 && priority < 0x01000000)
24765 priority >>= 3;
24766 break;
24769 #endif
24771 if (insn_must_be_first_in_group (insn)
24772 && reload_completed
24773 && current_sched_info->sched_max_insns_priority
24774 && rs6000_sched_restricted_insns_priority)
24777 /* Prioritize insns that can be dispatched only in the first
24778 dispatch slot. */
24779 if (rs6000_sched_restricted_insns_priority == 1)
24780 /* Attach highest priority to insn. This means that in
24781 haifa-sched.c:ready_sort(), dispatch-slot restriction considerations
24782 precede 'priority' (critical path) considerations. */
24783 return current_sched_info->sched_max_insns_priority;
24784 else if (rs6000_sched_restricted_insns_priority == 2)
24785 /* Increase priority of insn by a minimal amount. This means that in
24786 haifa-sched.c:ready_sort(), only 'priority' (critical path)
24787 considerations precede dispatch-slot restriction considerations. */
24788 return (priority + 1);
24791 if (rs6000_cpu == PROCESSOR_POWER6
24792 && ((load_store_pendulum == -2 && is_load_insn (insn, &load_mem))
24793 || (load_store_pendulum == 2 && is_store_insn (insn, &str_mem))))
24794 /* Attach highest priority to insn if the scheduler has just issued two
24795 stores and this instruction is a load, or two loads and this instruction
24796 is a store. Power6 wants loads and stores scheduled alternately
24797 when possible */
24798 return current_sched_info->sched_max_insns_priority;
24800 return priority;
24803 /* Return true if the instruction is nonpipelined on the Cell. */
24804 static bool
24805 is_nonpipeline_insn (rtx insn)
24807 enum attr_type type;
24808 if (!insn || !NONDEBUG_INSN_P (insn)
24809 || GET_CODE (PATTERN (insn)) == USE
24810 || GET_CODE (PATTERN (insn)) == CLOBBER)
24811 return false;
24813 type = get_attr_type (insn);
24814 if (type == TYPE_IMUL
24815 || type == TYPE_IMUL2
24816 || type == TYPE_IMUL3
24817 || type == TYPE_LMUL
24818 || type == TYPE_IDIV
24819 || type == TYPE_LDIV
24820 || type == TYPE_SDIV
24821 || type == TYPE_DDIV
24822 || type == TYPE_SSQRT
24823 || type == TYPE_DSQRT
24824 || type == TYPE_MFCR
24825 || type == TYPE_MFCRF
24826 || type == TYPE_MFJMPR)
24828 return true;
24830 return false;
24834 /* Return how many instructions the machine can issue per cycle. */
24836 static int
24837 rs6000_issue_rate (void)
24839 /* Unless scheduling for register pressure, use issue rate of 1 for
24840 first scheduling pass to decrease degradation. */
24841 if (!reload_completed && !flag_sched_pressure)
24842 return 1;
24844 switch (rs6000_cpu_attr) {
24845 case CPU_RS64A:
24846 case CPU_PPC601: /* ? */
24847 case CPU_PPC7450:
24848 return 3;
24849 case CPU_PPC440:
24850 case CPU_PPC603:
24851 case CPU_PPC750:
24852 case CPU_PPC7400:
24853 case CPU_PPC8540:
24854 case CPU_PPC8548:
24855 case CPU_CELL:
24856 case CPU_PPCE300C2:
24857 case CPU_PPCE300C3:
24858 case CPU_PPCE500MC:
24859 case CPU_PPCE500MC64:
24860 case CPU_PPCE5500:
24861 case CPU_PPCE6500:
24862 case CPU_TITAN:
24863 return 2;
24864 case CPU_PPC476:
24865 case CPU_PPC604:
24866 case CPU_PPC604E:
24867 case CPU_PPC620:
24868 case CPU_PPC630:
24869 return 4;
24870 case CPU_POWER4:
24871 case CPU_POWER5:
24872 case CPU_POWER6:
24873 case CPU_POWER7:
24874 return 5;
24875 case CPU_POWER8:
24876 return 7;
24877 default:
24878 return 1;
24882 /* Return how many instructions to look ahead for better insn
24883 scheduling. */
24885 static int
24886 rs6000_use_sched_lookahead (void)
24888 switch (rs6000_cpu_attr)
24890 case CPU_PPC8540:
24891 case CPU_PPC8548:
24892 return 4;
24894 case CPU_CELL:
24895 return (reload_completed ? 8 : 0);
24897 default:
24898 return 0;
24902 /* We are choosing insn from the ready queue. Return nonzero if INSN can be chosen. */
24903 static int
24904 rs6000_use_sched_lookahead_guard (rtx insn)
24906 if (rs6000_cpu_attr != CPU_CELL)
24907 return 1;
24909 if (insn == NULL_RTX || !INSN_P (insn))
24910 abort ();
24912 if (!reload_completed
24913 || is_nonpipeline_insn (insn)
24914 || is_microcoded_insn (insn))
24915 return 0;
24917 return 1;
24920 /* Determine if PAT refers to memory. If so, set MEM_REF to the MEM rtx
24921 and return true. */
24923 static bool
24924 find_mem_ref (rtx pat, rtx *mem_ref)
24926 const char * fmt;
24927 int i, j;
24929 /* stack_tie does not produce any real memory traffic. */
24930 if (tie_operand (pat, VOIDmode))
24931 return false;
24933 if (GET_CODE (pat) == MEM)
24935 *mem_ref = pat;
24936 return true;
24939 /* Recursively process the pattern. */
24940 fmt = GET_RTX_FORMAT (GET_CODE (pat));
24942 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
24944 if (fmt[i] == 'e')
24946 if (find_mem_ref (XEXP (pat, i), mem_ref))
24947 return true;
24949 else if (fmt[i] == 'E')
24950 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
24952 if (find_mem_ref (XVECEXP (pat, i, j), mem_ref))
24953 return true;
24957 return false;
24960 /* Determine if PAT is a PATTERN of a load insn. */
24962 static bool
24963 is_load_insn1 (rtx pat, rtx *load_mem)
24965 if (!pat || pat == NULL_RTX)
24966 return false;
24968 if (GET_CODE (pat) == SET)
24969 return find_mem_ref (SET_SRC (pat), load_mem);
24971 if (GET_CODE (pat) == PARALLEL)
24973 int i;
24975 for (i = 0; i < XVECLEN (pat, 0); i++)
24976 if (is_load_insn1 (XVECEXP (pat, 0, i), load_mem))
24977 return true;
24980 return false;
24983 /* Determine if INSN loads from memory. */
24985 static bool
24986 is_load_insn (rtx insn, rtx *load_mem)
24988 if (!insn || !INSN_P (insn))
24989 return false;
24991 if (CALL_P (insn))
24992 return false;
24994 return is_load_insn1 (PATTERN (insn), load_mem);
24997 /* Determine if PAT is a PATTERN of a store insn. */
24999 static bool
25000 is_store_insn1 (rtx pat, rtx *str_mem)
25002 if (!pat || pat == NULL_RTX)
25003 return false;
25005 if (GET_CODE (pat) == SET)
25006 return find_mem_ref (SET_DEST (pat), str_mem);
25008 if (GET_CODE (pat) == PARALLEL)
25010 int i;
25012 for (i = 0; i < XVECLEN (pat, 0); i++)
25013 if (is_store_insn1 (XVECEXP (pat, 0, i), str_mem))
25014 return true;
25017 return false;
25020 /* Determine if INSN stores to memory. */
25022 static bool
25023 is_store_insn (rtx insn, rtx *str_mem)
25025 if (!insn || !INSN_P (insn))
25026 return false;
25028 return is_store_insn1 (PATTERN (insn), str_mem);
25031 /* Returns whether the dependence between INSN and NEXT is considered
25032 costly by the given target. */
25034 static bool
25035 rs6000_is_costly_dependence (dep_t dep, int cost, int distance)
25037 rtx insn;
25038 rtx next;
25039 rtx load_mem, str_mem;
25041 /* If the flag is not enabled - no dependence is considered costly;
25042 allow all dependent insns in the same group.
25043 This is the most aggressive option. */
25044 if (rs6000_sched_costly_dep == no_dep_costly)
25045 return false;
25047 /* If the flag is set to 1 - a dependence is always considered costly;
25048 do not allow dependent instructions in the same group.
25049 This is the most conservative option. */
25050 if (rs6000_sched_costly_dep == all_deps_costly)
25051 return true;
25053 insn = DEP_PRO (dep);
25054 next = DEP_CON (dep);
25056 if (rs6000_sched_costly_dep == store_to_load_dep_costly
25057 && is_load_insn (next, &load_mem)
25058 && is_store_insn (insn, &str_mem))
25059 /* Prevent load after store in the same group. */
25060 return true;
25062 if (rs6000_sched_costly_dep == true_store_to_load_dep_costly
25063 && is_load_insn (next, &load_mem)
25064 && is_store_insn (insn, &str_mem)
25065 && DEP_TYPE (dep) == REG_DEP_TRUE
25066 && mem_locations_overlap(str_mem, load_mem))
25067 /* Prevent load after store in the same group if it is a true
25068 dependence. */
25069 return true;
25071 /* The flag is set to X; dependences with latency >= X are considered costly,
25072 and will not be scheduled in the same group. */
25073 if (rs6000_sched_costly_dep <= max_dep_latency
25074 && ((cost - distance) >= (int)rs6000_sched_costly_dep))
25075 return true;
25077 return false;
25080 /* Return the next insn after INSN that is found before TAIL is reached,
25081 skipping any "non-active" insns - insns that will not actually occupy
25082 an issue slot. Return NULL_RTX if such an insn is not found. */
25084 static rtx
25085 get_next_active_insn (rtx insn, rtx tail)
25087 if (insn == NULL_RTX || insn == tail)
25088 return NULL_RTX;
25090 while (1)
25092 insn = NEXT_INSN (insn);
25093 if (insn == NULL_RTX || insn == tail)
25094 return NULL_RTX;
25096 if (CALL_P (insn)
25097 || JUMP_P (insn) || JUMP_TABLE_DATA_P (insn)
25098 || (NONJUMP_INSN_P (insn)
25099 && GET_CODE (PATTERN (insn)) != USE
25100 && GET_CODE (PATTERN (insn)) != CLOBBER
25101 && INSN_CODE (insn) != CODE_FOR_stack_tie))
25102 break;
25104 return insn;
25107 /* We are about to begin issuing insns for this clock cycle. */
25109 static int
25110 rs6000_sched_reorder (FILE *dump ATTRIBUTE_UNUSED, int sched_verbose,
25111 rtx *ready ATTRIBUTE_UNUSED,
25112 int *pn_ready ATTRIBUTE_UNUSED,
25113 int clock_var ATTRIBUTE_UNUSED)
25115 int n_ready = *pn_ready;
25117 if (sched_verbose)
25118 fprintf (dump, "// rs6000_sched_reorder :\n");
25120 /* Reorder the ready list, if the second to last ready insn
25121 is a nonepipeline insn. */
25122 if (rs6000_cpu_attr == CPU_CELL && n_ready > 1)
25124 if (is_nonpipeline_insn (ready[n_ready - 1])
25125 && (recog_memoized (ready[n_ready - 2]) > 0))
25126 /* Simply swap first two insns. */
25128 rtx tmp = ready[n_ready - 1];
25129 ready[n_ready - 1] = ready[n_ready - 2];
25130 ready[n_ready - 2] = tmp;
25134 if (rs6000_cpu == PROCESSOR_POWER6)
25135 load_store_pendulum = 0;
25137 return rs6000_issue_rate ();
25140 /* Like rs6000_sched_reorder, but called after issuing each insn. */
25142 static int
25143 rs6000_sched_reorder2 (FILE *dump, int sched_verbose, rtx *ready,
25144 int *pn_ready, int clock_var ATTRIBUTE_UNUSED)
25146 if (sched_verbose)
25147 fprintf (dump, "// rs6000_sched_reorder2 :\n");
25149 /* For Power6, we need to handle some special cases to try and keep the
25150 store queue from overflowing and triggering expensive flushes.
25152 This code monitors how load and store instructions are being issued
25153 and skews the ready list one way or the other to increase the likelihood
25154 that a desired instruction is issued at the proper time.
25156 A couple of things are done. First, we maintain a "load_store_pendulum"
25157 to track the current state of load/store issue.
25159 - If the pendulum is at zero, then no loads or stores have been
25160 issued in the current cycle so we do nothing.
25162 - If the pendulum is 1, then a single load has been issued in this
25163 cycle and we attempt to locate another load in the ready list to
25164 issue with it.
25166 - If the pendulum is -2, then two stores have already been
25167 issued in this cycle, so we increase the priority of the first load
25168 in the ready list to increase it's likelihood of being chosen first
25169 in the next cycle.
25171 - If the pendulum is -1, then a single store has been issued in this
25172 cycle and we attempt to locate another store in the ready list to
25173 issue with it, preferring a store to an adjacent memory location to
25174 facilitate store pairing in the store queue.
25176 - If the pendulum is 2, then two loads have already been
25177 issued in this cycle, so we increase the priority of the first store
25178 in the ready list to increase it's likelihood of being chosen first
25179 in the next cycle.
25181 - If the pendulum < -2 or > 2, then do nothing.
25183 Note: This code covers the most common scenarios. There exist non
25184 load/store instructions which make use of the LSU and which
25185 would need to be accounted for to strictly model the behavior
25186 of the machine. Those instructions are currently unaccounted
25187 for to help minimize compile time overhead of this code.
25189 if (rs6000_cpu == PROCESSOR_POWER6 && last_scheduled_insn)
25191 int pos;
25192 int i;
25193 rtx tmp, load_mem, str_mem;
25195 if (is_store_insn (last_scheduled_insn, &str_mem))
25196 /* Issuing a store, swing the load_store_pendulum to the left */
25197 load_store_pendulum--;
25198 else if (is_load_insn (last_scheduled_insn, &load_mem))
25199 /* Issuing a load, swing the load_store_pendulum to the right */
25200 load_store_pendulum++;
25201 else
25202 return cached_can_issue_more;
25204 /* If the pendulum is balanced, or there is only one instruction on
25205 the ready list, then all is well, so return. */
25206 if ((load_store_pendulum == 0) || (*pn_ready <= 1))
25207 return cached_can_issue_more;
25209 if (load_store_pendulum == 1)
25211 /* A load has been issued in this cycle. Scan the ready list
25212 for another load to issue with it */
25213 pos = *pn_ready-1;
25215 while (pos >= 0)
25217 if (is_load_insn (ready[pos], &load_mem))
25219 /* Found a load. Move it to the head of the ready list,
25220 and adjust it's priority so that it is more likely to
25221 stay there */
25222 tmp = ready[pos];
25223 for (i=pos; i<*pn_ready-1; i++)
25224 ready[i] = ready[i + 1];
25225 ready[*pn_ready-1] = tmp;
25227 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
25228 INSN_PRIORITY (tmp)++;
25229 break;
25231 pos--;
25234 else if (load_store_pendulum == -2)
25236 /* Two stores have been issued in this cycle. Increase the
25237 priority of the first load in the ready list to favor it for
25238 issuing in the next cycle. */
25239 pos = *pn_ready-1;
25241 while (pos >= 0)
25243 if (is_load_insn (ready[pos], &load_mem)
25244 && !sel_sched_p ()
25245 && INSN_PRIORITY_KNOWN (ready[pos]))
25247 INSN_PRIORITY (ready[pos])++;
25249 /* Adjust the pendulum to account for the fact that a load
25250 was found and increased in priority. This is to prevent
25251 increasing the priority of multiple loads */
25252 load_store_pendulum--;
25254 break;
25256 pos--;
25259 else if (load_store_pendulum == -1)
25261 /* A store has been issued in this cycle. Scan the ready list for
25262 another store to issue with it, preferring a store to an adjacent
25263 memory location */
25264 int first_store_pos = -1;
25266 pos = *pn_ready-1;
25268 while (pos >= 0)
25270 if (is_store_insn (ready[pos], &str_mem))
25272 rtx str_mem2;
25273 /* Maintain the index of the first store found on the
25274 list */
25275 if (first_store_pos == -1)
25276 first_store_pos = pos;
25278 if (is_store_insn (last_scheduled_insn, &str_mem2)
25279 && adjacent_mem_locations (str_mem, str_mem2))
25281 /* Found an adjacent store. Move it to the head of the
25282 ready list, and adjust it's priority so that it is
25283 more likely to stay there */
25284 tmp = ready[pos];
25285 for (i=pos; i<*pn_ready-1; i++)
25286 ready[i] = ready[i + 1];
25287 ready[*pn_ready-1] = tmp;
25289 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
25290 INSN_PRIORITY (tmp)++;
25292 first_store_pos = -1;
25294 break;
25297 pos--;
25300 if (first_store_pos >= 0)
25302 /* An adjacent store wasn't found, but a non-adjacent store was,
25303 so move the non-adjacent store to the front of the ready
25304 list, and adjust its priority so that it is more likely to
25305 stay there. */
25306 tmp = ready[first_store_pos];
25307 for (i=first_store_pos; i<*pn_ready-1; i++)
25308 ready[i] = ready[i + 1];
25309 ready[*pn_ready-1] = tmp;
25310 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
25311 INSN_PRIORITY (tmp)++;
25314 else if (load_store_pendulum == 2)
25316 /* Two loads have been issued in this cycle. Increase the priority
25317 of the first store in the ready list to favor it for issuing in
25318 the next cycle. */
25319 pos = *pn_ready-1;
25321 while (pos >= 0)
25323 if (is_store_insn (ready[pos], &str_mem)
25324 && !sel_sched_p ()
25325 && INSN_PRIORITY_KNOWN (ready[pos]))
25327 INSN_PRIORITY (ready[pos])++;
25329 /* Adjust the pendulum to account for the fact that a store
25330 was found and increased in priority. This is to prevent
25331 increasing the priority of multiple stores */
25332 load_store_pendulum++;
25334 break;
25336 pos--;
25341 return cached_can_issue_more;
25344 /* Return whether the presence of INSN causes a dispatch group termination
25345 of group WHICH_GROUP.
25347 If WHICH_GROUP == current_group, this function will return true if INSN
25348 causes the termination of the current group (i.e, the dispatch group to
25349 which INSN belongs). This means that INSN will be the last insn in the
25350 group it belongs to.
25352 If WHICH_GROUP == previous_group, this function will return true if INSN
25353 causes the termination of the previous group (i.e, the dispatch group that
25354 precedes the group to which INSN belongs). This means that INSN will be
25355 the first insn in the group it belongs to). */
25357 static bool
25358 insn_terminates_group_p (rtx insn, enum group_termination which_group)
25360 bool first, last;
25362 if (! insn)
25363 return false;
25365 first = insn_must_be_first_in_group (insn);
25366 last = insn_must_be_last_in_group (insn);
25368 if (first && last)
25369 return true;
25371 if (which_group == current_group)
25372 return last;
25373 else if (which_group == previous_group)
25374 return first;
25376 return false;
25380 static bool
25381 insn_must_be_first_in_group (rtx insn)
25383 enum attr_type type;
25385 if (!insn
25386 || NOTE_P (insn)
25387 || DEBUG_INSN_P (insn)
25388 || GET_CODE (PATTERN (insn)) == USE
25389 || GET_CODE (PATTERN (insn)) == CLOBBER)
25390 return false;
25392 switch (rs6000_cpu)
25394 case PROCESSOR_POWER5:
25395 if (is_cracked_insn (insn))
25396 return true;
25397 case PROCESSOR_POWER4:
25398 if (is_microcoded_insn (insn))
25399 return true;
25401 if (!rs6000_sched_groups)
25402 return false;
25404 type = get_attr_type (insn);
25406 switch (type)
25408 case TYPE_MFCR:
25409 case TYPE_MFCRF:
25410 case TYPE_MTCR:
25411 case TYPE_DELAYED_CR:
25412 case TYPE_CR_LOGICAL:
25413 case TYPE_MTJMPR:
25414 case TYPE_MFJMPR:
25415 case TYPE_IDIV:
25416 case TYPE_LDIV:
25417 case TYPE_LOAD_L:
25418 case TYPE_STORE_C:
25419 case TYPE_ISYNC:
25420 case TYPE_SYNC:
25421 return true;
25422 default:
25423 break;
25425 break;
25426 case PROCESSOR_POWER6:
25427 type = get_attr_type (insn);
25429 switch (type)
25431 case TYPE_INSERT_DWORD:
25432 case TYPE_EXTS:
25433 case TYPE_CNTLZ:
25434 case TYPE_SHIFT:
25435 case TYPE_VAR_SHIFT_ROTATE:
25436 case TYPE_TRAP:
25437 case TYPE_IMUL:
25438 case TYPE_IMUL2:
25439 case TYPE_IMUL3:
25440 case TYPE_LMUL:
25441 case TYPE_IDIV:
25442 case TYPE_INSERT_WORD:
25443 case TYPE_DELAYED_COMPARE:
25444 case TYPE_IMUL_COMPARE:
25445 case TYPE_LMUL_COMPARE:
25446 case TYPE_FPCOMPARE:
25447 case TYPE_MFCR:
25448 case TYPE_MTCR:
25449 case TYPE_MFJMPR:
25450 case TYPE_MTJMPR:
25451 case TYPE_ISYNC:
25452 case TYPE_SYNC:
25453 case TYPE_LOAD_L:
25454 case TYPE_STORE_C:
25455 case TYPE_LOAD_U:
25456 case TYPE_LOAD_UX:
25457 case TYPE_LOAD_EXT_UX:
25458 case TYPE_STORE_U:
25459 case TYPE_STORE_UX:
25460 case TYPE_FPLOAD_U:
25461 case TYPE_FPLOAD_UX:
25462 case TYPE_FPSTORE_U:
25463 case TYPE_FPSTORE_UX:
25464 return true;
25465 default:
25466 break;
25468 break;
25469 case PROCESSOR_POWER7:
25470 type = get_attr_type (insn);
25472 switch (type)
25474 case TYPE_CR_LOGICAL:
25475 case TYPE_MFCR:
25476 case TYPE_MFCRF:
25477 case TYPE_MTCR:
25478 case TYPE_IDIV:
25479 case TYPE_LDIV:
25480 case TYPE_COMPARE:
25481 case TYPE_DELAYED_COMPARE:
25482 case TYPE_VAR_DELAYED_COMPARE:
25483 case TYPE_ISYNC:
25484 case TYPE_LOAD_L:
25485 case TYPE_STORE_C:
25486 case TYPE_LOAD_U:
25487 case TYPE_LOAD_UX:
25488 case TYPE_LOAD_EXT:
25489 case TYPE_LOAD_EXT_U:
25490 case TYPE_LOAD_EXT_UX:
25491 case TYPE_STORE_U:
25492 case TYPE_STORE_UX:
25493 case TYPE_FPLOAD_U:
25494 case TYPE_FPLOAD_UX:
25495 case TYPE_FPSTORE_U:
25496 case TYPE_FPSTORE_UX:
25497 case TYPE_MFJMPR:
25498 case TYPE_MTJMPR:
25499 return true;
25500 default:
25501 break;
25503 break;
25504 case PROCESSOR_POWER8:
25505 type = get_attr_type (insn);
25507 switch (type)
25509 case TYPE_CR_LOGICAL:
25510 case TYPE_DELAYED_CR:
25511 case TYPE_MFCR:
25512 case TYPE_MFCRF:
25513 case TYPE_MTCR:
25514 case TYPE_COMPARE:
25515 case TYPE_DELAYED_COMPARE:
25516 case TYPE_VAR_DELAYED_COMPARE:
25517 case TYPE_IMUL_COMPARE:
25518 case TYPE_LMUL_COMPARE:
25519 case TYPE_SYNC:
25520 case TYPE_ISYNC:
25521 case TYPE_LOAD_L:
25522 case TYPE_STORE_C:
25523 case TYPE_LOAD_U:
25524 case TYPE_LOAD_UX:
25525 case TYPE_LOAD_EXT:
25526 case TYPE_LOAD_EXT_U:
25527 case TYPE_LOAD_EXT_UX:
25528 case TYPE_STORE_UX:
25529 case TYPE_VECSTORE:
25530 case TYPE_MFJMPR:
25531 case TYPE_MTJMPR:
25532 return true;
25533 default:
25534 break;
25536 break;
25537 default:
25538 break;
25541 return false;
25544 static bool
25545 insn_must_be_last_in_group (rtx insn)
25547 enum attr_type type;
25549 if (!insn
25550 || NOTE_P (insn)
25551 || DEBUG_INSN_P (insn)
25552 || GET_CODE (PATTERN (insn)) == USE
25553 || GET_CODE (PATTERN (insn)) == CLOBBER)
25554 return false;
25556 switch (rs6000_cpu) {
25557 case PROCESSOR_POWER4:
25558 case PROCESSOR_POWER5:
25559 if (is_microcoded_insn (insn))
25560 return true;
25562 if (is_branch_slot_insn (insn))
25563 return true;
25565 break;
25566 case PROCESSOR_POWER6:
25567 type = get_attr_type (insn);
25569 switch (type)
25571 case TYPE_EXTS:
25572 case TYPE_CNTLZ:
25573 case TYPE_SHIFT:
25574 case TYPE_VAR_SHIFT_ROTATE:
25575 case TYPE_TRAP:
25576 case TYPE_IMUL:
25577 case TYPE_IMUL2:
25578 case TYPE_IMUL3:
25579 case TYPE_LMUL:
25580 case TYPE_IDIV:
25581 case TYPE_DELAYED_COMPARE:
25582 case TYPE_IMUL_COMPARE:
25583 case TYPE_LMUL_COMPARE:
25584 case TYPE_FPCOMPARE:
25585 case TYPE_MFCR:
25586 case TYPE_MTCR:
25587 case TYPE_MFJMPR:
25588 case TYPE_MTJMPR:
25589 case TYPE_ISYNC:
25590 case TYPE_SYNC:
25591 case TYPE_LOAD_L:
25592 case TYPE_STORE_C:
25593 return true;
25594 default:
25595 break;
25597 break;
25598 case PROCESSOR_POWER7:
25599 type = get_attr_type (insn);
25601 switch (type)
25603 case TYPE_ISYNC:
25604 case TYPE_SYNC:
25605 case TYPE_LOAD_L:
25606 case TYPE_STORE_C:
25607 case TYPE_LOAD_EXT_U:
25608 case TYPE_LOAD_EXT_UX:
25609 case TYPE_STORE_UX:
25610 return true;
25611 default:
25612 break;
25614 break;
25615 case PROCESSOR_POWER8:
25616 type = get_attr_type (insn);
25618 switch (type)
25620 case TYPE_MFCR:
25621 case TYPE_MTCR:
25622 case TYPE_ISYNC:
25623 case TYPE_SYNC:
25624 case TYPE_LOAD_L:
25625 case TYPE_STORE_C:
25626 case TYPE_LOAD_EXT_U:
25627 case TYPE_LOAD_EXT_UX:
25628 case TYPE_STORE_UX:
25629 return true;
25630 default:
25631 break;
25633 break;
25634 default:
25635 break;
25638 return false;
25641 /* Return true if it is recommended to keep NEXT_INSN "far" (in a separate
25642 dispatch group) from the insns in GROUP_INSNS. Return false otherwise. */
25644 static bool
25645 is_costly_group (rtx *group_insns, rtx next_insn)
25647 int i;
25648 int issue_rate = rs6000_issue_rate ();
25650 for (i = 0; i < issue_rate; i++)
25652 sd_iterator_def sd_it;
25653 dep_t dep;
25654 rtx insn = group_insns[i];
25656 if (!insn)
25657 continue;
25659 FOR_EACH_DEP (insn, SD_LIST_RES_FORW, sd_it, dep)
25661 rtx next = DEP_CON (dep);
25663 if (next == next_insn
25664 && rs6000_is_costly_dependence (dep, dep_cost (dep), 0))
25665 return true;
25669 return false;
25672 /* Utility of the function redefine_groups.
25673 Check if it is too costly to schedule NEXT_INSN together with GROUP_INSNS
25674 in the same dispatch group. If so, insert nops before NEXT_INSN, in order
25675 to keep it "far" (in a separate group) from GROUP_INSNS, following
25676 one of the following schemes, depending on the value of the flag
25677 -minsert_sched_nops = X:
25678 (1) X == sched_finish_regroup_exact: insert exactly as many nops as needed
25679 in order to force NEXT_INSN into a separate group.
25680 (2) X < sched_finish_regroup_exact: insert exactly X nops.
25681 GROUP_END, CAN_ISSUE_MORE and GROUP_COUNT record the state after nop
25682 insertion (has a group just ended, how many vacant issue slots remain in the
25683 last group, and how many dispatch groups were encountered so far). */
25685 static int
25686 force_new_group (int sched_verbose, FILE *dump, rtx *group_insns,
25687 rtx next_insn, bool *group_end, int can_issue_more,
25688 int *group_count)
25690 rtx nop;
25691 bool force;
25692 int issue_rate = rs6000_issue_rate ();
25693 bool end = *group_end;
25694 int i;
25696 if (next_insn == NULL_RTX || DEBUG_INSN_P (next_insn))
25697 return can_issue_more;
25699 if (rs6000_sched_insert_nops > sched_finish_regroup_exact)
25700 return can_issue_more;
25702 force = is_costly_group (group_insns, next_insn);
25703 if (!force)
25704 return can_issue_more;
25706 if (sched_verbose > 6)
25707 fprintf (dump,"force: group count = %d, can_issue_more = %d\n",
25708 *group_count ,can_issue_more);
25710 if (rs6000_sched_insert_nops == sched_finish_regroup_exact)
25712 if (*group_end)
25713 can_issue_more = 0;
25715 /* Since only a branch can be issued in the last issue_slot, it is
25716 sufficient to insert 'can_issue_more - 1' nops if next_insn is not
25717 a branch. If next_insn is a branch, we insert 'can_issue_more' nops;
25718 in this case the last nop will start a new group and the branch
25719 will be forced to the new group. */
25720 if (can_issue_more && !is_branch_slot_insn (next_insn))
25721 can_issue_more--;
25723 /* Do we have a special group ending nop? */
25724 if (rs6000_cpu_attr == CPU_POWER6 || rs6000_cpu_attr == CPU_POWER7
25725 || rs6000_cpu_attr == CPU_POWER8)
25727 nop = gen_group_ending_nop ();
25728 emit_insn_before (nop, next_insn);
25729 can_issue_more = 0;
25731 else
25732 while (can_issue_more > 0)
25734 nop = gen_nop ();
25735 emit_insn_before (nop, next_insn);
25736 can_issue_more--;
25739 *group_end = true;
25740 return 0;
25743 if (rs6000_sched_insert_nops < sched_finish_regroup_exact)
25745 int n_nops = rs6000_sched_insert_nops;
25747 /* Nops can't be issued from the branch slot, so the effective
25748 issue_rate for nops is 'issue_rate - 1'. */
25749 if (can_issue_more == 0)
25750 can_issue_more = issue_rate;
25751 can_issue_more--;
25752 if (can_issue_more == 0)
25754 can_issue_more = issue_rate - 1;
25755 (*group_count)++;
25756 end = true;
25757 for (i = 0; i < issue_rate; i++)
25759 group_insns[i] = 0;
25763 while (n_nops > 0)
25765 nop = gen_nop ();
25766 emit_insn_before (nop, next_insn);
25767 if (can_issue_more == issue_rate - 1) /* new group begins */
25768 end = false;
25769 can_issue_more--;
25770 if (can_issue_more == 0)
25772 can_issue_more = issue_rate - 1;
25773 (*group_count)++;
25774 end = true;
25775 for (i = 0; i < issue_rate; i++)
25777 group_insns[i] = 0;
25780 n_nops--;
25783 /* Scale back relative to 'issue_rate' (instead of 'issue_rate - 1'). */
25784 can_issue_more++;
25786 /* Is next_insn going to start a new group? */
25787 *group_end
25788 = (end
25789 || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
25790 || (can_issue_more <= 2 && is_cracked_insn (next_insn))
25791 || (can_issue_more < issue_rate &&
25792 insn_terminates_group_p (next_insn, previous_group)));
25793 if (*group_end && end)
25794 (*group_count)--;
25796 if (sched_verbose > 6)
25797 fprintf (dump, "done force: group count = %d, can_issue_more = %d\n",
25798 *group_count, can_issue_more);
25799 return can_issue_more;
25802 return can_issue_more;
25805 /* This function tries to synch the dispatch groups that the compiler "sees"
25806 with the dispatch groups that the processor dispatcher is expected to
25807 form in practice. It tries to achieve this synchronization by forcing the
25808 estimated processor grouping on the compiler (as opposed to the function
25809 'pad_goups' which tries to force the scheduler's grouping on the processor).
25811 The function scans the insn sequence between PREV_HEAD_INSN and TAIL and
25812 examines the (estimated) dispatch groups that will be formed by the processor
25813 dispatcher. It marks these group boundaries to reflect the estimated
25814 processor grouping, overriding the grouping that the scheduler had marked.
25815 Depending on the value of the flag '-minsert-sched-nops' this function can
25816 force certain insns into separate groups or force a certain distance between
25817 them by inserting nops, for example, if there exists a "costly dependence"
25818 between the insns.
25820 The function estimates the group boundaries that the processor will form as
25821 follows: It keeps track of how many vacant issue slots are available after
25822 each insn. A subsequent insn will start a new group if one of the following
25823 4 cases applies:
25824 - no more vacant issue slots remain in the current dispatch group.
25825 - only the last issue slot, which is the branch slot, is vacant, but the next
25826 insn is not a branch.
25827 - only the last 2 or less issue slots, including the branch slot, are vacant,
25828 which means that a cracked insn (which occupies two issue slots) can't be
25829 issued in this group.
25830 - less than 'issue_rate' slots are vacant, and the next insn always needs to
25831 start a new group. */
25833 static int
25834 redefine_groups (FILE *dump, int sched_verbose, rtx prev_head_insn, rtx tail)
25836 rtx insn, next_insn;
25837 int issue_rate;
25838 int can_issue_more;
25839 int slot, i;
25840 bool group_end;
25841 int group_count = 0;
25842 rtx *group_insns;
25844 /* Initialize. */
25845 issue_rate = rs6000_issue_rate ();
25846 group_insns = XALLOCAVEC (rtx, issue_rate);
25847 for (i = 0; i < issue_rate; i++)
25849 group_insns[i] = 0;
25851 can_issue_more = issue_rate;
25852 slot = 0;
25853 insn = get_next_active_insn (prev_head_insn, tail);
25854 group_end = false;
25856 while (insn != NULL_RTX)
25858 slot = (issue_rate - can_issue_more);
25859 group_insns[slot] = insn;
25860 can_issue_more =
25861 rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
25862 if (insn_terminates_group_p (insn, current_group))
25863 can_issue_more = 0;
25865 next_insn = get_next_active_insn (insn, tail);
25866 if (next_insn == NULL_RTX)
25867 return group_count + 1;
25869 /* Is next_insn going to start a new group? */
25870 group_end
25871 = (can_issue_more == 0
25872 || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
25873 || (can_issue_more <= 2 && is_cracked_insn (next_insn))
25874 || (can_issue_more < issue_rate &&
25875 insn_terminates_group_p (next_insn, previous_group)));
25877 can_issue_more = force_new_group (sched_verbose, dump, group_insns,
25878 next_insn, &group_end, can_issue_more,
25879 &group_count);
25881 if (group_end)
25883 group_count++;
25884 can_issue_more = 0;
25885 for (i = 0; i < issue_rate; i++)
25887 group_insns[i] = 0;
25891 if (GET_MODE (next_insn) == TImode && can_issue_more)
25892 PUT_MODE (next_insn, VOIDmode);
25893 else if (!can_issue_more && GET_MODE (next_insn) != TImode)
25894 PUT_MODE (next_insn, TImode);
25896 insn = next_insn;
25897 if (can_issue_more == 0)
25898 can_issue_more = issue_rate;
25899 } /* while */
25901 return group_count;
25904 /* Scan the insn sequence between PREV_HEAD_INSN and TAIL and examine the
25905 dispatch group boundaries that the scheduler had marked. Pad with nops
25906 any dispatch groups which have vacant issue slots, in order to force the
25907 scheduler's grouping on the processor dispatcher. The function
25908 returns the number of dispatch groups found. */
25910 static int
25911 pad_groups (FILE *dump, int sched_verbose, rtx prev_head_insn, rtx tail)
25913 rtx insn, next_insn;
25914 rtx nop;
25915 int issue_rate;
25916 int can_issue_more;
25917 int group_end;
25918 int group_count = 0;
25920 /* Initialize issue_rate. */
25921 issue_rate = rs6000_issue_rate ();
25922 can_issue_more = issue_rate;
25924 insn = get_next_active_insn (prev_head_insn, tail);
25925 next_insn = get_next_active_insn (insn, tail);
25927 while (insn != NULL_RTX)
25929 can_issue_more =
25930 rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
25932 group_end = (next_insn == NULL_RTX || GET_MODE (next_insn) == TImode);
25934 if (next_insn == NULL_RTX)
25935 break;
25937 if (group_end)
25939 /* If the scheduler had marked group termination at this location
25940 (between insn and next_insn), and neither insn nor next_insn will
25941 force group termination, pad the group with nops to force group
25942 termination. */
25943 if (can_issue_more
25944 && (rs6000_sched_insert_nops == sched_finish_pad_groups)
25945 && !insn_terminates_group_p (insn, current_group)
25946 && !insn_terminates_group_p (next_insn, previous_group))
25948 if (!is_branch_slot_insn (next_insn))
25949 can_issue_more--;
25951 while (can_issue_more)
25953 nop = gen_nop ();
25954 emit_insn_before (nop, next_insn);
25955 can_issue_more--;
25959 can_issue_more = issue_rate;
25960 group_count++;
25963 insn = next_insn;
25964 next_insn = get_next_active_insn (insn, tail);
25967 return group_count;
25970 /* We're beginning a new block. Initialize data structures as necessary. */
25972 static void
25973 rs6000_sched_init (FILE *dump ATTRIBUTE_UNUSED,
25974 int sched_verbose ATTRIBUTE_UNUSED,
25975 int max_ready ATTRIBUTE_UNUSED)
25977 last_scheduled_insn = NULL_RTX;
25978 load_store_pendulum = 0;
25981 /* The following function is called at the end of scheduling BB.
25982 After reload, it inserts nops at insn group bundling. */
25984 static void
25985 rs6000_sched_finish (FILE *dump, int sched_verbose)
25987 int n_groups;
25989 if (sched_verbose)
25990 fprintf (dump, "=== Finishing schedule.\n");
25992 if (reload_completed && rs6000_sched_groups)
25994 /* Do not run sched_finish hook when selective scheduling enabled. */
25995 if (sel_sched_p ())
25996 return;
25998 if (rs6000_sched_insert_nops == sched_finish_none)
25999 return;
26001 if (rs6000_sched_insert_nops == sched_finish_pad_groups)
26002 n_groups = pad_groups (dump, sched_verbose,
26003 current_sched_info->prev_head,
26004 current_sched_info->next_tail);
26005 else
26006 n_groups = redefine_groups (dump, sched_verbose,
26007 current_sched_info->prev_head,
26008 current_sched_info->next_tail);
26010 if (sched_verbose >= 6)
26012 fprintf (dump, "ngroups = %d\n", n_groups);
26013 print_rtl (dump, current_sched_info->prev_head);
26014 fprintf (dump, "Done finish_sched\n");
26019 struct _rs6000_sched_context
26021 short cached_can_issue_more;
26022 rtx last_scheduled_insn;
26023 int load_store_pendulum;
26026 typedef struct _rs6000_sched_context rs6000_sched_context_def;
26027 typedef rs6000_sched_context_def *rs6000_sched_context_t;
26029 /* Allocate store for new scheduling context. */
26030 static void *
26031 rs6000_alloc_sched_context (void)
26033 return xmalloc (sizeof (rs6000_sched_context_def));
26036 /* If CLEAN_P is true then initializes _SC with clean data,
26037 and from the global context otherwise. */
26038 static void
26039 rs6000_init_sched_context (void *_sc, bool clean_p)
26041 rs6000_sched_context_t sc = (rs6000_sched_context_t) _sc;
26043 if (clean_p)
26045 sc->cached_can_issue_more = 0;
26046 sc->last_scheduled_insn = NULL_RTX;
26047 sc->load_store_pendulum = 0;
26049 else
26051 sc->cached_can_issue_more = cached_can_issue_more;
26052 sc->last_scheduled_insn = last_scheduled_insn;
26053 sc->load_store_pendulum = load_store_pendulum;
26057 /* Sets the global scheduling context to the one pointed to by _SC. */
26058 static void
26059 rs6000_set_sched_context (void *_sc)
26061 rs6000_sched_context_t sc = (rs6000_sched_context_t) _sc;
26063 gcc_assert (sc != NULL);
26065 cached_can_issue_more = sc->cached_can_issue_more;
26066 last_scheduled_insn = sc->last_scheduled_insn;
26067 load_store_pendulum = sc->load_store_pendulum;
26070 /* Free _SC. */
26071 static void
26072 rs6000_free_sched_context (void *_sc)
26074 gcc_assert (_sc != NULL);
26076 free (_sc);
26080 /* Length in units of the trampoline for entering a nested function. */
26083 rs6000_trampoline_size (void)
26085 int ret = 0;
26087 switch (DEFAULT_ABI)
26089 default:
26090 gcc_unreachable ();
26092 case ABI_AIX:
26093 ret = (TARGET_32BIT) ? 12 : 24;
26094 break;
26096 case ABI_DARWIN:
26097 case ABI_V4:
26098 ret = (TARGET_32BIT) ? 40 : 48;
26099 break;
26102 return ret;
26105 /* Emit RTL insns to initialize the variable parts of a trampoline.
26106 FNADDR is an RTX for the address of the function's pure code.
26107 CXT is an RTX for the static chain value for the function. */
26109 static void
26110 rs6000_trampoline_init (rtx m_tramp, tree fndecl, rtx cxt)
26112 int regsize = (TARGET_32BIT) ? 4 : 8;
26113 rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
26114 rtx ctx_reg = force_reg (Pmode, cxt);
26115 rtx addr = force_reg (Pmode, XEXP (m_tramp, 0));
26117 switch (DEFAULT_ABI)
26119 default:
26120 gcc_unreachable ();
26122 /* Under AIX, just build the 3 word function descriptor */
26123 case ABI_AIX:
26125 rtx fnmem, fn_reg, toc_reg;
26127 if (!TARGET_POINTERS_TO_NESTED_FUNCTIONS)
26128 error ("You cannot take the address of a nested function if you use "
26129 "the -mno-pointers-to-nested-functions option.");
26131 fnmem = gen_const_mem (Pmode, force_reg (Pmode, fnaddr));
26132 fn_reg = gen_reg_rtx (Pmode);
26133 toc_reg = gen_reg_rtx (Pmode);
26135 /* Macro to shorten the code expansions below. */
26136 # define MEM_PLUS(MEM, OFFSET) adjust_address (MEM, Pmode, OFFSET)
26138 m_tramp = replace_equiv_address (m_tramp, addr);
26140 emit_move_insn (fn_reg, MEM_PLUS (fnmem, 0));
26141 emit_move_insn (toc_reg, MEM_PLUS (fnmem, regsize));
26142 emit_move_insn (MEM_PLUS (m_tramp, 0), fn_reg);
26143 emit_move_insn (MEM_PLUS (m_tramp, regsize), toc_reg);
26144 emit_move_insn (MEM_PLUS (m_tramp, 2*regsize), ctx_reg);
26146 # undef MEM_PLUS
26148 break;
26150 /* Under V.4/eabi/darwin, __trampoline_setup does the real work. */
26151 case ABI_DARWIN:
26152 case ABI_V4:
26153 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__trampoline_setup"),
26154 LCT_NORMAL, VOIDmode, 4,
26155 addr, Pmode,
26156 GEN_INT (rs6000_trampoline_size ()), SImode,
26157 fnaddr, Pmode,
26158 ctx_reg, Pmode);
26159 break;
26164 /* Returns TRUE iff the target attribute indicated by ATTR_ID takes a plain
26165 identifier as an argument, so the front end shouldn't look it up. */
26167 static bool
26168 rs6000_attribute_takes_identifier_p (const_tree attr_id)
26170 return is_attribute_p ("altivec", attr_id);
26173 /* Handle the "altivec" attribute. The attribute may have
26174 arguments as follows:
26176 __attribute__((altivec(vector__)))
26177 __attribute__((altivec(pixel__))) (always followed by 'unsigned short')
26178 __attribute__((altivec(bool__))) (always followed by 'unsigned')
26180 and may appear more than once (e.g., 'vector bool char') in a
26181 given declaration. */
26183 static tree
26184 rs6000_handle_altivec_attribute (tree *node,
26185 tree name ATTRIBUTE_UNUSED,
26186 tree args,
26187 int flags ATTRIBUTE_UNUSED,
26188 bool *no_add_attrs)
26190 tree type = *node, result = NULL_TREE;
26191 enum machine_mode mode;
26192 int unsigned_p;
26193 char altivec_type
26194 = ((args && TREE_CODE (args) == TREE_LIST && TREE_VALUE (args)
26195 && TREE_CODE (TREE_VALUE (args)) == IDENTIFIER_NODE)
26196 ? *IDENTIFIER_POINTER (TREE_VALUE (args))
26197 : '?');
26199 while (POINTER_TYPE_P (type)
26200 || TREE_CODE (type) == FUNCTION_TYPE
26201 || TREE_CODE (type) == METHOD_TYPE
26202 || TREE_CODE (type) == ARRAY_TYPE)
26203 type = TREE_TYPE (type);
26205 mode = TYPE_MODE (type);
26207 /* Check for invalid AltiVec type qualifiers. */
26208 if (type == long_double_type_node)
26209 error ("use of %<long double%> in AltiVec types is invalid");
26210 else if (type == boolean_type_node)
26211 error ("use of boolean types in AltiVec types is invalid");
26212 else if (TREE_CODE (type) == COMPLEX_TYPE)
26213 error ("use of %<complex%> in AltiVec types is invalid");
26214 else if (DECIMAL_FLOAT_MODE_P (mode))
26215 error ("use of decimal floating point types in AltiVec types is invalid");
26216 else if (!TARGET_VSX)
26218 if (type == long_unsigned_type_node || type == long_integer_type_node)
26220 if (TARGET_64BIT)
26221 error ("use of %<long%> in AltiVec types is invalid for "
26222 "64-bit code without -mvsx");
26223 else if (rs6000_warn_altivec_long)
26224 warning (0, "use of %<long%> in AltiVec types is deprecated; "
26225 "use %<int%>");
26227 else if (type == long_long_unsigned_type_node
26228 || type == long_long_integer_type_node)
26229 error ("use of %<long long%> in AltiVec types is invalid without "
26230 "-mvsx");
26231 else if (type == double_type_node)
26232 error ("use of %<double%> in AltiVec types is invalid without -mvsx");
26235 switch (altivec_type)
26237 case 'v':
26238 unsigned_p = TYPE_UNSIGNED (type);
26239 switch (mode)
26241 case DImode:
26242 result = (unsigned_p ? unsigned_V2DI_type_node : V2DI_type_node);
26243 break;
26244 case SImode:
26245 result = (unsigned_p ? unsigned_V4SI_type_node : V4SI_type_node);
26246 break;
26247 case HImode:
26248 result = (unsigned_p ? unsigned_V8HI_type_node : V8HI_type_node);
26249 break;
26250 case QImode:
26251 result = (unsigned_p ? unsigned_V16QI_type_node : V16QI_type_node);
26252 break;
26253 case SFmode: result = V4SF_type_node; break;
26254 case DFmode: result = V2DF_type_node; break;
26255 /* If the user says 'vector int bool', we may be handed the 'bool'
26256 attribute _before_ the 'vector' attribute, and so select the
26257 proper type in the 'b' case below. */
26258 case V4SImode: case V8HImode: case V16QImode: case V4SFmode:
26259 case V2DImode: case V2DFmode:
26260 result = type;
26261 default: break;
26263 break;
26264 case 'b':
26265 switch (mode)
26267 case DImode: case V2DImode: result = bool_V2DI_type_node; break;
26268 case SImode: case V4SImode: result = bool_V4SI_type_node; break;
26269 case HImode: case V8HImode: result = bool_V8HI_type_node; break;
26270 case QImode: case V16QImode: result = bool_V16QI_type_node;
26271 default: break;
26273 break;
26274 case 'p':
26275 switch (mode)
26277 case V8HImode: result = pixel_V8HI_type_node;
26278 default: break;
26280 default: break;
26283 /* Propagate qualifiers attached to the element type
26284 onto the vector type. */
26285 if (result && result != type && TYPE_QUALS (type))
26286 result = build_qualified_type (result, TYPE_QUALS (type));
26288 *no_add_attrs = true; /* No need to hang on to the attribute. */
26290 if (result)
26291 *node = lang_hooks.types.reconstruct_complex_type (*node, result);
26293 return NULL_TREE;
26296 /* AltiVec defines four built-in scalar types that serve as vector
26297 elements; we must teach the compiler how to mangle them. */
26299 static const char *
26300 rs6000_mangle_type (const_tree type)
26302 type = TYPE_MAIN_VARIANT (type);
26304 if (TREE_CODE (type) != VOID_TYPE && TREE_CODE (type) != BOOLEAN_TYPE
26305 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
26306 return NULL;
26308 if (type == bool_char_type_node) return "U6__boolc";
26309 if (type == bool_short_type_node) return "U6__bools";
26310 if (type == pixel_type_node) return "u7__pixel";
26311 if (type == bool_int_type_node) return "U6__booli";
26312 if (type == bool_long_type_node) return "U6__booll";
26314 /* Mangle IBM extended float long double as `g' (__float128) on
26315 powerpc*-linux where long-double-64 previously was the default. */
26316 if (TYPE_MAIN_VARIANT (type) == long_double_type_node
26317 && TARGET_ELF
26318 && TARGET_LONG_DOUBLE_128
26319 && !TARGET_IEEEQUAD)
26320 return "g";
26322 /* For all other types, use normal C++ mangling. */
26323 return NULL;
26326 /* Handle a "longcall" or "shortcall" attribute; arguments as in
26327 struct attribute_spec.handler. */
26329 static tree
26330 rs6000_handle_longcall_attribute (tree *node, tree name,
26331 tree args ATTRIBUTE_UNUSED,
26332 int flags ATTRIBUTE_UNUSED,
26333 bool *no_add_attrs)
26335 if (TREE_CODE (*node) != FUNCTION_TYPE
26336 && TREE_CODE (*node) != FIELD_DECL
26337 && TREE_CODE (*node) != TYPE_DECL)
26339 warning (OPT_Wattributes, "%qE attribute only applies to functions",
26340 name);
26341 *no_add_attrs = true;
26344 return NULL_TREE;
26347 /* Set longcall attributes on all functions declared when
26348 rs6000_default_long_calls is true. */
26349 static void
26350 rs6000_set_default_type_attributes (tree type)
26352 if (rs6000_default_long_calls
26353 && (TREE_CODE (type) == FUNCTION_TYPE
26354 || TREE_CODE (type) == METHOD_TYPE))
26355 TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("longcall"),
26356 NULL_TREE,
26357 TYPE_ATTRIBUTES (type));
26359 #if TARGET_MACHO
26360 darwin_set_default_type_attributes (type);
26361 #endif
26364 /* Return a reference suitable for calling a function with the
26365 longcall attribute. */
26368 rs6000_longcall_ref (rtx call_ref)
26370 const char *call_name;
26371 tree node;
26373 if (GET_CODE (call_ref) != SYMBOL_REF)
26374 return call_ref;
26376 /* System V adds '.' to the internal name, so skip them. */
26377 call_name = XSTR (call_ref, 0);
26378 if (*call_name == '.')
26380 while (*call_name == '.')
26381 call_name++;
26383 node = get_identifier (call_name);
26384 call_ref = gen_rtx_SYMBOL_REF (VOIDmode, IDENTIFIER_POINTER (node));
26387 return force_reg (Pmode, call_ref);
26390 #ifndef TARGET_USE_MS_BITFIELD_LAYOUT
26391 #define TARGET_USE_MS_BITFIELD_LAYOUT 0
26392 #endif
26394 /* Handle a "ms_struct" or "gcc_struct" attribute; arguments as in
26395 struct attribute_spec.handler. */
26396 static tree
26397 rs6000_handle_struct_attribute (tree *node, tree name,
26398 tree args ATTRIBUTE_UNUSED,
26399 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
26401 tree *type = NULL;
26402 if (DECL_P (*node))
26404 if (TREE_CODE (*node) == TYPE_DECL)
26405 type = &TREE_TYPE (*node);
26407 else
26408 type = node;
26410 if (!(type && (TREE_CODE (*type) == RECORD_TYPE
26411 || TREE_CODE (*type) == UNION_TYPE)))
26413 warning (OPT_Wattributes, "%qE attribute ignored", name);
26414 *no_add_attrs = true;
26417 else if ((is_attribute_p ("ms_struct", name)
26418 && lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (*type)))
26419 || ((is_attribute_p ("gcc_struct", name)
26420 && lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (*type)))))
26422 warning (OPT_Wattributes, "%qE incompatible attribute ignored",
26423 name);
26424 *no_add_attrs = true;
26427 return NULL_TREE;
26430 static bool
26431 rs6000_ms_bitfield_layout_p (const_tree record_type)
26433 return (TARGET_USE_MS_BITFIELD_LAYOUT &&
26434 !lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (record_type)))
26435 || lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (record_type));
26438 #ifdef USING_ELFOS_H
26440 /* A get_unnamed_section callback, used for switching to toc_section. */
26442 static void
26443 rs6000_elf_output_toc_section_asm_op (const void *data ATTRIBUTE_UNUSED)
26445 if (DEFAULT_ABI == ABI_AIX
26446 && TARGET_MINIMAL_TOC
26447 && !TARGET_RELOCATABLE)
26449 if (!toc_initialized)
26451 toc_initialized = 1;
26452 fprintf (asm_out_file, "%s\n", TOC_SECTION_ASM_OP);
26453 (*targetm.asm_out.internal_label) (asm_out_file, "LCTOC", 0);
26454 fprintf (asm_out_file, "\t.tc ");
26455 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1[TC],");
26456 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
26457 fprintf (asm_out_file, "\n");
26459 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
26460 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
26461 fprintf (asm_out_file, " = .+32768\n");
26463 else
26464 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
26466 else if (DEFAULT_ABI == ABI_AIX && !TARGET_RELOCATABLE)
26467 fprintf (asm_out_file, "%s\n", TOC_SECTION_ASM_OP);
26468 else
26470 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
26471 if (!toc_initialized)
26473 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
26474 fprintf (asm_out_file, " = .+32768\n");
26475 toc_initialized = 1;
26480 /* Implement TARGET_ASM_INIT_SECTIONS. */
26482 static void
26483 rs6000_elf_asm_init_sections (void)
26485 toc_section
26486 = get_unnamed_section (0, rs6000_elf_output_toc_section_asm_op, NULL);
26488 sdata2_section
26489 = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
26490 SDATA2_SECTION_ASM_OP);
26493 /* Implement TARGET_SELECT_RTX_SECTION. */
26495 static section *
26496 rs6000_elf_select_rtx_section (enum machine_mode mode, rtx x,
26497 unsigned HOST_WIDE_INT align)
26499 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
26500 return toc_section;
26501 else
26502 return default_elf_select_rtx_section (mode, x, align);
26505 /* For a SYMBOL_REF, set generic flags and then perform some
26506 target-specific processing.
26508 When the AIX ABI is requested on a non-AIX system, replace the
26509 function name with the real name (with a leading .) rather than the
26510 function descriptor name. This saves a lot of overriding code to
26511 read the prefixes. */
26513 static void rs6000_elf_encode_section_info (tree, rtx, int) ATTRIBUTE_UNUSED;
26514 static void
26515 rs6000_elf_encode_section_info (tree decl, rtx rtl, int first)
26517 default_encode_section_info (decl, rtl, first);
26519 if (first
26520 && TREE_CODE (decl) == FUNCTION_DECL
26521 && !TARGET_AIX
26522 && DEFAULT_ABI == ABI_AIX)
26524 rtx sym_ref = XEXP (rtl, 0);
26525 size_t len = strlen (XSTR (sym_ref, 0));
26526 char *str = XALLOCAVEC (char, len + 2);
26527 str[0] = '.';
26528 memcpy (str + 1, XSTR (sym_ref, 0), len + 1);
26529 XSTR (sym_ref, 0) = ggc_alloc_string (str, len + 1);
26533 static inline bool
26534 compare_section_name (const char *section, const char *templ)
26536 int len;
26538 len = strlen (templ);
26539 return (strncmp (section, templ, len) == 0
26540 && (section[len] == 0 || section[len] == '.'));
26543 bool
26544 rs6000_elf_in_small_data_p (const_tree decl)
26546 if (rs6000_sdata == SDATA_NONE)
26547 return false;
26549 /* We want to merge strings, so we never consider them small data. */
26550 if (TREE_CODE (decl) == STRING_CST)
26551 return false;
26553 /* Functions are never in the small data area. */
26554 if (TREE_CODE (decl) == FUNCTION_DECL)
26555 return false;
26557 if (TREE_CODE (decl) == VAR_DECL && DECL_SECTION_NAME (decl))
26559 const char *section = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
26560 if (compare_section_name (section, ".sdata")
26561 || compare_section_name (section, ".sdata2")
26562 || compare_section_name (section, ".gnu.linkonce.s")
26563 || compare_section_name (section, ".sbss")
26564 || compare_section_name (section, ".sbss2")
26565 || compare_section_name (section, ".gnu.linkonce.sb")
26566 || strcmp (section, ".PPC.EMB.sdata0") == 0
26567 || strcmp (section, ".PPC.EMB.sbss0") == 0)
26568 return true;
26570 else
26572 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (decl));
26574 if (size > 0
26575 && size <= g_switch_value
26576 /* If it's not public, and we're not going to reference it there,
26577 there's no need to put it in the small data section. */
26578 && (rs6000_sdata != SDATA_DATA || TREE_PUBLIC (decl)))
26579 return true;
26582 return false;
26585 #endif /* USING_ELFOS_H */
26587 /* Implement TARGET_USE_BLOCKS_FOR_CONSTANT_P. */
26589 static bool
26590 rs6000_use_blocks_for_constant_p (enum machine_mode mode, const_rtx x)
26592 return !ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode);
26595 /* Do not place thread-local symbols refs in the object blocks. */
26597 static bool
26598 rs6000_use_blocks_for_decl_p (const_tree decl)
26600 return !DECL_THREAD_LOCAL_P (decl);
26603 /* Return a REG that occurs in ADDR with coefficient 1.
26604 ADDR can be effectively incremented by incrementing REG.
26606 r0 is special and we must not select it as an address
26607 register by this routine since our caller will try to
26608 increment the returned register via an "la" instruction. */
26611 find_addr_reg (rtx addr)
26613 while (GET_CODE (addr) == PLUS)
26615 if (GET_CODE (XEXP (addr, 0)) == REG
26616 && REGNO (XEXP (addr, 0)) != 0)
26617 addr = XEXP (addr, 0);
26618 else if (GET_CODE (XEXP (addr, 1)) == REG
26619 && REGNO (XEXP (addr, 1)) != 0)
26620 addr = XEXP (addr, 1);
26621 else if (CONSTANT_P (XEXP (addr, 0)))
26622 addr = XEXP (addr, 1);
26623 else if (CONSTANT_P (XEXP (addr, 1)))
26624 addr = XEXP (addr, 0);
26625 else
26626 gcc_unreachable ();
26628 gcc_assert (GET_CODE (addr) == REG && REGNO (addr) != 0);
26629 return addr;
26632 void
26633 rs6000_fatal_bad_address (rtx op)
26635 fatal_insn ("bad address", op);
26638 #if TARGET_MACHO
26640 typedef struct branch_island_d {
26641 tree function_name;
26642 tree label_name;
26643 int line_number;
26644 } branch_island;
26647 static vec<branch_island, va_gc> *branch_islands;
26649 /* Remember to generate a branch island for far calls to the given
26650 function. */
26652 static void
26653 add_compiler_branch_island (tree label_name, tree function_name,
26654 int line_number)
26656 branch_island bi = {function_name, label_name, line_number};
26657 vec_safe_push (branch_islands, bi);
26660 /* Generate far-jump branch islands for everything recorded in
26661 branch_islands. Invoked immediately after the last instruction of
26662 the epilogue has been emitted; the branch islands must be appended
26663 to, and contiguous with, the function body. Mach-O stubs are
26664 generated in machopic_output_stub(). */
26666 static void
26667 macho_branch_islands (void)
26669 char tmp_buf[512];
26671 while (!vec_safe_is_empty (branch_islands))
26673 branch_island *bi = &branch_islands->last ();
26674 const char *label = IDENTIFIER_POINTER (bi->label_name);
26675 const char *name = IDENTIFIER_POINTER (bi->function_name);
26676 char name_buf[512];
26677 /* Cheap copy of the details from the Darwin ASM_OUTPUT_LABELREF(). */
26678 if (name[0] == '*' || name[0] == '&')
26679 strcpy (name_buf, name+1);
26680 else
26682 name_buf[0] = '_';
26683 strcpy (name_buf+1, name);
26685 strcpy (tmp_buf, "\n");
26686 strcat (tmp_buf, label);
26687 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
26688 if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
26689 dbxout_stabd (N_SLINE, bi->line_number);
26690 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
26691 if (flag_pic)
26693 if (TARGET_LINK_STACK)
26695 char name[32];
26696 get_ppc476_thunk_name (name);
26697 strcat (tmp_buf, ":\n\tmflr r0\n\tbl ");
26698 strcat (tmp_buf, name);
26699 strcat (tmp_buf, "\n");
26700 strcat (tmp_buf, label);
26701 strcat (tmp_buf, "_pic:\n\tmflr r11\n");
26703 else
26705 strcat (tmp_buf, ":\n\tmflr r0\n\tbcl 20,31,");
26706 strcat (tmp_buf, label);
26707 strcat (tmp_buf, "_pic\n");
26708 strcat (tmp_buf, label);
26709 strcat (tmp_buf, "_pic:\n\tmflr r11\n");
26712 strcat (tmp_buf, "\taddis r11,r11,ha16(");
26713 strcat (tmp_buf, name_buf);
26714 strcat (tmp_buf, " - ");
26715 strcat (tmp_buf, label);
26716 strcat (tmp_buf, "_pic)\n");
26718 strcat (tmp_buf, "\tmtlr r0\n");
26720 strcat (tmp_buf, "\taddi r12,r11,lo16(");
26721 strcat (tmp_buf, name_buf);
26722 strcat (tmp_buf, " - ");
26723 strcat (tmp_buf, label);
26724 strcat (tmp_buf, "_pic)\n");
26726 strcat (tmp_buf, "\tmtctr r12\n\tbctr\n");
26728 else
26730 strcat (tmp_buf, ":\nlis r12,hi16(");
26731 strcat (tmp_buf, name_buf);
26732 strcat (tmp_buf, ")\n\tori r12,r12,lo16(");
26733 strcat (tmp_buf, name_buf);
26734 strcat (tmp_buf, ")\n\tmtctr r12\n\tbctr");
26736 output_asm_insn (tmp_buf, 0);
26737 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
26738 if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
26739 dbxout_stabd (N_SLINE, bi->line_number);
26740 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
26741 branch_islands->pop ();
26745 /* NO_PREVIOUS_DEF checks in the link list whether the function name is
26746 already there or not. */
26748 static int
26749 no_previous_def (tree function_name)
26751 branch_island *bi;
26752 unsigned ix;
26754 FOR_EACH_VEC_SAFE_ELT (branch_islands, ix, bi)
26755 if (function_name == bi->function_name)
26756 return 0;
26757 return 1;
26760 /* GET_PREV_LABEL gets the label name from the previous definition of
26761 the function. */
26763 static tree
26764 get_prev_label (tree function_name)
26766 branch_island *bi;
26767 unsigned ix;
26769 FOR_EACH_VEC_SAFE_ELT (branch_islands, ix, bi)
26770 if (function_name == bi->function_name)
26771 return bi->label_name;
26772 return NULL_TREE;
26775 /* INSN is either a function call or a millicode call. It may have an
26776 unconditional jump in its delay slot.
26778 CALL_DEST is the routine we are calling. */
26780 char *
26781 output_call (rtx insn, rtx *operands, int dest_operand_number,
26782 int cookie_operand_number)
26784 static char buf[256];
26785 if (darwin_emit_branch_islands
26786 && GET_CODE (operands[dest_operand_number]) == SYMBOL_REF
26787 && (INTVAL (operands[cookie_operand_number]) & CALL_LONG))
26789 tree labelname;
26790 tree funname = get_identifier (XSTR (operands[dest_operand_number], 0));
26792 if (no_previous_def (funname))
26794 rtx label_rtx = gen_label_rtx ();
26795 char *label_buf, temp_buf[256];
26796 ASM_GENERATE_INTERNAL_LABEL (temp_buf, "L",
26797 CODE_LABEL_NUMBER (label_rtx));
26798 label_buf = temp_buf[0] == '*' ? temp_buf + 1 : temp_buf;
26799 labelname = get_identifier (label_buf);
26800 add_compiler_branch_island (labelname, funname, insn_line (insn));
26802 else
26803 labelname = get_prev_label (funname);
26805 /* "jbsr foo, L42" is Mach-O for "Link as 'bl foo' if a 'bl'
26806 instruction will reach 'foo', otherwise link as 'bl L42'".
26807 "L42" should be a 'branch island', that will do a far jump to
26808 'foo'. Branch islands are generated in
26809 macho_branch_islands(). */
26810 sprintf (buf, "jbsr %%z%d,%.246s",
26811 dest_operand_number, IDENTIFIER_POINTER (labelname));
26813 else
26814 sprintf (buf, "bl %%z%d", dest_operand_number);
26815 return buf;
26818 /* Generate PIC and indirect symbol stubs. */
26820 void
26821 machopic_output_stub (FILE *file, const char *symb, const char *stub)
26823 unsigned int length;
26824 char *symbol_name, *lazy_ptr_name;
26825 char *local_label_0;
26826 static int label = 0;
26828 /* Lose our funky encoding stuff so it doesn't contaminate the stub. */
26829 symb = (*targetm.strip_name_encoding) (symb);
26832 length = strlen (symb);
26833 symbol_name = XALLOCAVEC (char, length + 32);
26834 GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
26836 lazy_ptr_name = XALLOCAVEC (char, length + 32);
26837 GEN_LAZY_PTR_NAME_FOR_SYMBOL (lazy_ptr_name, symb, length);
26839 if (flag_pic == 2)
26840 switch_to_section (darwin_sections[machopic_picsymbol_stub1_section]);
26841 else
26842 switch_to_section (darwin_sections[machopic_symbol_stub1_section]);
26844 if (flag_pic == 2)
26846 fprintf (file, "\t.align 5\n");
26848 fprintf (file, "%s:\n", stub);
26849 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
26851 label++;
26852 local_label_0 = XALLOCAVEC (char, sizeof ("\"L00000000000$spb\""));
26853 sprintf (local_label_0, "\"L%011d$spb\"", label);
26855 fprintf (file, "\tmflr r0\n");
26856 if (TARGET_LINK_STACK)
26858 char name[32];
26859 get_ppc476_thunk_name (name);
26860 fprintf (file, "\tbl %s\n", name);
26861 fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
26863 else
26865 fprintf (file, "\tbcl 20,31,%s\n", local_label_0);
26866 fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
26868 fprintf (file, "\taddis r11,r11,ha16(%s-%s)\n",
26869 lazy_ptr_name, local_label_0);
26870 fprintf (file, "\tmtlr r0\n");
26871 fprintf (file, "\t%s r12,lo16(%s-%s)(r11)\n",
26872 (TARGET_64BIT ? "ldu" : "lwzu"),
26873 lazy_ptr_name, local_label_0);
26874 fprintf (file, "\tmtctr r12\n");
26875 fprintf (file, "\tbctr\n");
26877 else
26879 fprintf (file, "\t.align 4\n");
26881 fprintf (file, "%s:\n", stub);
26882 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
26884 fprintf (file, "\tlis r11,ha16(%s)\n", lazy_ptr_name);
26885 fprintf (file, "\t%s r12,lo16(%s)(r11)\n",
26886 (TARGET_64BIT ? "ldu" : "lwzu"),
26887 lazy_ptr_name);
26888 fprintf (file, "\tmtctr r12\n");
26889 fprintf (file, "\tbctr\n");
26892 switch_to_section (darwin_sections[machopic_lazy_symbol_ptr_section]);
26893 fprintf (file, "%s:\n", lazy_ptr_name);
26894 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
26895 fprintf (file, "%sdyld_stub_binding_helper\n",
26896 (TARGET_64BIT ? DOUBLE_INT_ASM_OP : "\t.long\t"));
26899 /* Legitimize PIC addresses. If the address is already
26900 position-independent, we return ORIG. Newly generated
26901 position-independent addresses go into a reg. This is REG if non
26902 zero, otherwise we allocate register(s) as necessary. */
26904 #define SMALL_INT(X) ((UINTVAL (X) + 0x8000) < 0x10000)
26907 rs6000_machopic_legitimize_pic_address (rtx orig, enum machine_mode mode,
26908 rtx reg)
26910 rtx base, offset;
26912 if (reg == NULL && ! reload_in_progress && ! reload_completed)
26913 reg = gen_reg_rtx (Pmode);
26915 if (GET_CODE (orig) == CONST)
26917 rtx reg_temp;
26919 if (GET_CODE (XEXP (orig, 0)) == PLUS
26920 && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
26921 return orig;
26923 gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
26925 /* Use a different reg for the intermediate value, as
26926 it will be marked UNCHANGING. */
26927 reg_temp = !can_create_pseudo_p () ? reg : gen_reg_rtx (Pmode);
26928 base = rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 0),
26929 Pmode, reg_temp);
26930 offset =
26931 rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 1),
26932 Pmode, reg);
26934 if (GET_CODE (offset) == CONST_INT)
26936 if (SMALL_INT (offset))
26937 return plus_constant (Pmode, base, INTVAL (offset));
26938 else if (! reload_in_progress && ! reload_completed)
26939 offset = force_reg (Pmode, offset);
26940 else
26942 rtx mem = force_const_mem (Pmode, orig);
26943 return machopic_legitimize_pic_address (mem, Pmode, reg);
26946 return gen_rtx_PLUS (Pmode, base, offset);
26949 /* Fall back on generic machopic code. */
26950 return machopic_legitimize_pic_address (orig, mode, reg);
26953 /* Output a .machine directive for the Darwin assembler, and call
26954 the generic start_file routine. */
26956 static void
26957 rs6000_darwin_file_start (void)
26959 static const struct
26961 const char *arg;
26962 const char *name;
26963 HOST_WIDE_INT if_set;
26964 } mapping[] = {
26965 { "ppc64", "ppc64", MASK_64BIT },
26966 { "970", "ppc970", MASK_PPC_GPOPT | MASK_MFCRF | MASK_POWERPC64 },
26967 { "power4", "ppc970", 0 },
26968 { "G5", "ppc970", 0 },
26969 { "7450", "ppc7450", 0 },
26970 { "7400", "ppc7400", MASK_ALTIVEC },
26971 { "G4", "ppc7400", 0 },
26972 { "750", "ppc750", 0 },
26973 { "740", "ppc750", 0 },
26974 { "G3", "ppc750", 0 },
26975 { "604e", "ppc604e", 0 },
26976 { "604", "ppc604", 0 },
26977 { "603e", "ppc603", 0 },
26978 { "603", "ppc603", 0 },
26979 { "601", "ppc601", 0 },
26980 { NULL, "ppc", 0 } };
26981 const char *cpu_id = "";
26982 size_t i;
26984 rs6000_file_start ();
26985 darwin_file_start ();
26987 /* Determine the argument to -mcpu=. Default to G3 if not specified. */
26989 if (rs6000_default_cpu != 0 && rs6000_default_cpu[0] != '\0')
26990 cpu_id = rs6000_default_cpu;
26992 if (global_options_set.x_rs6000_cpu_index)
26993 cpu_id = processor_target_table[rs6000_cpu_index].name;
26995 /* Look through the mapping array. Pick the first name that either
26996 matches the argument, has a bit set in IF_SET that is also set
26997 in the target flags, or has a NULL name. */
26999 i = 0;
27000 while (mapping[i].arg != NULL
27001 && strcmp (mapping[i].arg, cpu_id) != 0
27002 && (mapping[i].if_set & rs6000_isa_flags) == 0)
27003 i++;
27005 fprintf (asm_out_file, "\t.machine %s\n", mapping[i].name);
27008 #endif /* TARGET_MACHO */
27010 #if TARGET_ELF
27011 static int
27012 rs6000_elf_reloc_rw_mask (void)
27014 if (flag_pic)
27015 return 3;
27016 else if (DEFAULT_ABI == ABI_AIX)
27017 return 2;
27018 else
27019 return 0;
27022 /* Record an element in the table of global constructors. SYMBOL is
27023 a SYMBOL_REF of the function to be called; PRIORITY is a number
27024 between 0 and MAX_INIT_PRIORITY.
27026 This differs from default_named_section_asm_out_constructor in
27027 that we have special handling for -mrelocatable. */
27029 static void rs6000_elf_asm_out_constructor (rtx, int) ATTRIBUTE_UNUSED;
27030 static void
27031 rs6000_elf_asm_out_constructor (rtx symbol, int priority)
27033 const char *section = ".ctors";
27034 char buf[16];
27036 if (priority != DEFAULT_INIT_PRIORITY)
27038 sprintf (buf, ".ctors.%.5u",
27039 /* Invert the numbering so the linker puts us in the proper
27040 order; constructors are run from right to left, and the
27041 linker sorts in increasing order. */
27042 MAX_INIT_PRIORITY - priority);
27043 section = buf;
27046 switch_to_section (get_section (section, SECTION_WRITE, NULL));
27047 assemble_align (POINTER_SIZE);
27049 if (TARGET_RELOCATABLE)
27051 fputs ("\t.long (", asm_out_file);
27052 output_addr_const (asm_out_file, symbol);
27053 fputs (")@fixup\n", asm_out_file);
27055 else
27056 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
27059 static void rs6000_elf_asm_out_destructor (rtx, int) ATTRIBUTE_UNUSED;
27060 static void
27061 rs6000_elf_asm_out_destructor (rtx symbol, int priority)
27063 const char *section = ".dtors";
27064 char buf[16];
27066 if (priority != DEFAULT_INIT_PRIORITY)
27068 sprintf (buf, ".dtors.%.5u",
27069 /* Invert the numbering so the linker puts us in the proper
27070 order; constructors are run from right to left, and the
27071 linker sorts in increasing order. */
27072 MAX_INIT_PRIORITY - priority);
27073 section = buf;
27076 switch_to_section (get_section (section, SECTION_WRITE, NULL));
27077 assemble_align (POINTER_SIZE);
27079 if (TARGET_RELOCATABLE)
27081 fputs ("\t.long (", asm_out_file);
27082 output_addr_const (asm_out_file, symbol);
27083 fputs (")@fixup\n", asm_out_file);
27085 else
27086 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
27089 void
27090 rs6000_elf_declare_function_name (FILE *file, const char *name, tree decl)
27092 if (TARGET_64BIT)
27094 fputs ("\t.section\t\".opd\",\"aw\"\n\t.align 3\n", file);
27095 ASM_OUTPUT_LABEL (file, name);
27096 fputs (DOUBLE_INT_ASM_OP, file);
27097 rs6000_output_function_entry (file, name);
27098 fputs (",.TOC.@tocbase,0\n\t.previous\n", file);
27099 if (DOT_SYMBOLS)
27101 fputs ("\t.size\t", file);
27102 assemble_name (file, name);
27103 fputs (",24\n\t.type\t.", file);
27104 assemble_name (file, name);
27105 fputs (",@function\n", file);
27106 if (TREE_PUBLIC (decl) && ! DECL_WEAK (decl))
27108 fputs ("\t.globl\t.", file);
27109 assemble_name (file, name);
27110 putc ('\n', file);
27113 else
27114 ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
27115 ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
27116 rs6000_output_function_entry (file, name);
27117 fputs (":\n", file);
27118 return;
27121 if (TARGET_RELOCATABLE
27122 && !TARGET_SECURE_PLT
27123 && (get_pool_size () != 0 || crtl->profile)
27124 && uses_TOC ())
27126 char buf[256];
27128 (*targetm.asm_out.internal_label) (file, "LCL", rs6000_pic_labelno);
27130 ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
27131 fprintf (file, "\t.long ");
27132 assemble_name (file, buf);
27133 putc ('-', file);
27134 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
27135 assemble_name (file, buf);
27136 putc ('\n', file);
27139 ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
27140 ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
27142 if (DEFAULT_ABI == ABI_AIX)
27144 const char *desc_name, *orig_name;
27146 orig_name = (*targetm.strip_name_encoding) (name);
27147 desc_name = orig_name;
27148 while (*desc_name == '.')
27149 desc_name++;
27151 if (TREE_PUBLIC (decl))
27152 fprintf (file, "\t.globl %s\n", desc_name);
27154 fprintf (file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
27155 fprintf (file, "%s:\n", desc_name);
27156 fprintf (file, "\t.long %s\n", orig_name);
27157 fputs ("\t.long _GLOBAL_OFFSET_TABLE_\n", file);
27158 if (DEFAULT_ABI == ABI_AIX)
27159 fputs ("\t.long 0\n", file);
27160 fprintf (file, "\t.previous\n");
27162 ASM_OUTPUT_LABEL (file, name);
27165 static void rs6000_elf_file_end (void) ATTRIBUTE_UNUSED;
27166 static void
27167 rs6000_elf_file_end (void)
27169 #ifdef HAVE_AS_GNU_ATTRIBUTE
27170 if (TARGET_32BIT && DEFAULT_ABI == ABI_V4)
27172 if (rs6000_passes_float)
27173 fprintf (asm_out_file, "\t.gnu_attribute 4, %d\n",
27174 ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT) ? 1
27175 : (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_SINGLE_FLOAT) ? 3
27176 : 2));
27177 if (rs6000_passes_vector)
27178 fprintf (asm_out_file, "\t.gnu_attribute 8, %d\n",
27179 (TARGET_ALTIVEC_ABI ? 2
27180 : TARGET_SPE_ABI ? 3
27181 : 1));
27182 if (rs6000_returns_struct)
27183 fprintf (asm_out_file, "\t.gnu_attribute 12, %d\n",
27184 aix_struct_return ? 2 : 1);
27186 #endif
27187 #if defined (POWERPC_LINUX) || defined (POWERPC_FREEBSD)
27188 if (TARGET_32BIT)
27189 file_end_indicate_exec_stack ();
27190 #endif
27192 #endif
27194 #if TARGET_XCOFF
27195 static void
27196 rs6000_xcoff_asm_output_anchor (rtx symbol)
27198 char buffer[100];
27200 sprintf (buffer, "$ + " HOST_WIDE_INT_PRINT_DEC,
27201 SYMBOL_REF_BLOCK_OFFSET (symbol));
27202 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
27205 static void
27206 rs6000_xcoff_asm_globalize_label (FILE *stream, const char *name)
27208 fputs (GLOBAL_ASM_OP, stream);
27209 RS6000_OUTPUT_BASENAME (stream, name);
27210 putc ('\n', stream);
27213 /* A get_unnamed_decl callback, used for read-only sections. PTR
27214 points to the section string variable. */
27216 static void
27217 rs6000_xcoff_output_readonly_section_asm_op (const void *directive)
27219 fprintf (asm_out_file, "\t.csect %s[RO],%s\n",
27220 *(const char *const *) directive,
27221 XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
27224 /* Likewise for read-write sections. */
27226 static void
27227 rs6000_xcoff_output_readwrite_section_asm_op (const void *directive)
27229 fprintf (asm_out_file, "\t.csect %s[RW],%s\n",
27230 *(const char *const *) directive,
27231 XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
27234 static void
27235 rs6000_xcoff_output_tls_section_asm_op (const void *directive)
27237 fprintf (asm_out_file, "\t.csect %s[TL],%s\n",
27238 *(const char *const *) directive,
27239 XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
27242 /* A get_unnamed_section callback, used for switching to toc_section. */
27244 static void
27245 rs6000_xcoff_output_toc_section_asm_op (const void *data ATTRIBUTE_UNUSED)
27247 if (TARGET_MINIMAL_TOC)
27249 /* toc_section is always selected at least once from
27250 rs6000_xcoff_file_start, so this is guaranteed to
27251 always be defined once and only once in each file. */
27252 if (!toc_initialized)
27254 fputs ("\t.toc\nLCTOC..1:\n", asm_out_file);
27255 fputs ("\t.tc toc_table[TC],toc_table[RW]\n", asm_out_file);
27256 toc_initialized = 1;
27258 fprintf (asm_out_file, "\t.csect toc_table[RW]%s\n",
27259 (TARGET_32BIT ? "" : ",3"));
27261 else
27262 fputs ("\t.toc\n", asm_out_file);
27265 /* Implement TARGET_ASM_INIT_SECTIONS. */
27267 static void
27268 rs6000_xcoff_asm_init_sections (void)
27270 read_only_data_section
27271 = get_unnamed_section (0, rs6000_xcoff_output_readonly_section_asm_op,
27272 &xcoff_read_only_section_name);
27274 private_data_section
27275 = get_unnamed_section (SECTION_WRITE,
27276 rs6000_xcoff_output_readwrite_section_asm_op,
27277 &xcoff_private_data_section_name);
27279 tls_data_section
27280 = get_unnamed_section (SECTION_TLS,
27281 rs6000_xcoff_output_tls_section_asm_op,
27282 &xcoff_tls_data_section_name);
27284 tls_private_data_section
27285 = get_unnamed_section (SECTION_TLS,
27286 rs6000_xcoff_output_tls_section_asm_op,
27287 &xcoff_private_data_section_name);
27289 read_only_private_data_section
27290 = get_unnamed_section (0, rs6000_xcoff_output_readonly_section_asm_op,
27291 &xcoff_private_data_section_name);
27293 toc_section
27294 = get_unnamed_section (0, rs6000_xcoff_output_toc_section_asm_op, NULL);
27296 readonly_data_section = read_only_data_section;
27297 exception_section = data_section;
27300 static int
27301 rs6000_xcoff_reloc_rw_mask (void)
27303 return 3;
27306 static void
27307 rs6000_xcoff_asm_named_section (const char *name, unsigned int flags,
27308 tree decl ATTRIBUTE_UNUSED)
27310 int smclass;
27311 static const char * const suffix[4] = { "PR", "RO", "RW", "TL" };
27313 if (flags & SECTION_CODE)
27314 smclass = 0;
27315 else if (flags & SECTION_TLS)
27316 smclass = 3;
27317 else if (flags & SECTION_WRITE)
27318 smclass = 2;
27319 else
27320 smclass = 1;
27322 fprintf (asm_out_file, "\t.csect %s%s[%s],%u\n",
27323 (flags & SECTION_CODE) ? "." : "",
27324 name, suffix[smclass], flags & SECTION_ENTSIZE);
27327 static section *
27328 rs6000_xcoff_select_section (tree decl, int reloc,
27329 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
27331 if (decl_readonly_section (decl, reloc))
27333 if (TREE_PUBLIC (decl))
27334 return read_only_data_section;
27335 else
27336 return read_only_private_data_section;
27338 else
27340 #if HAVE_AS_TLS
27341 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
27343 if (TREE_PUBLIC (decl))
27344 return tls_data_section;
27345 else if (bss_initializer_p (decl))
27347 /* Convert to COMMON to emit in BSS. */
27348 DECL_COMMON (decl) = 1;
27349 return tls_comm_section;
27351 else
27352 return tls_private_data_section;
27354 else
27355 #endif
27356 if (TREE_PUBLIC (decl))
27357 return data_section;
27358 else
27359 return private_data_section;
27363 static void
27364 rs6000_xcoff_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED)
27366 const char *name;
27368 /* Use select_section for private and uninitialized data. */
27369 if (!TREE_PUBLIC (decl)
27370 || DECL_COMMON (decl)
27371 || DECL_INITIAL (decl) == NULL_TREE
27372 || DECL_INITIAL (decl) == error_mark_node
27373 || (flag_zero_initialized_in_bss
27374 && initializer_zerop (DECL_INITIAL (decl))))
27375 return;
27377 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
27378 name = (*targetm.strip_name_encoding) (name);
27379 DECL_SECTION_NAME (decl) = build_string (strlen (name), name);
27382 /* Select section for constant in constant pool.
27384 On RS/6000, all constants are in the private read-only data area.
27385 However, if this is being placed in the TOC it must be output as a
27386 toc entry. */
27388 static section *
27389 rs6000_xcoff_select_rtx_section (enum machine_mode mode, rtx x,
27390 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
27392 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
27393 return toc_section;
27394 else
27395 return read_only_private_data_section;
27398 /* Remove any trailing [DS] or the like from the symbol name. */
27400 static const char *
27401 rs6000_xcoff_strip_name_encoding (const char *name)
27403 size_t len;
27404 if (*name == '*')
27405 name++;
27406 len = strlen (name);
27407 if (name[len - 1] == ']')
27408 return ggc_alloc_string (name, len - 4);
27409 else
27410 return name;
27413 /* Section attributes. AIX is always PIC. */
27415 static unsigned int
27416 rs6000_xcoff_section_type_flags (tree decl, const char *name, int reloc)
27418 unsigned int align;
27419 unsigned int flags = default_section_type_flags (decl, name, reloc);
27421 /* Align to at least UNIT size. */
27422 if ((flags & SECTION_CODE) != 0 || !decl || !DECL_P (decl))
27423 align = MIN_UNITS_PER_WORD;
27424 else
27425 /* Increase alignment of large objects if not already stricter. */
27426 align = MAX ((DECL_ALIGN (decl) / BITS_PER_UNIT),
27427 int_size_in_bytes (TREE_TYPE (decl)) > MIN_UNITS_PER_WORD
27428 ? UNITS_PER_FP_WORD : MIN_UNITS_PER_WORD);
27430 return flags | (exact_log2 (align) & SECTION_ENTSIZE);
27433 /* Output at beginning of assembler file.
27435 Initialize the section names for the RS/6000 at this point.
27437 Specify filename, including full path, to assembler.
27439 We want to go into the TOC section so at least one .toc will be emitted.
27440 Also, in order to output proper .bs/.es pairs, we need at least one static
27441 [RW] section emitted.
27443 Finally, declare mcount when profiling to make the assembler happy. */
27445 static void
27446 rs6000_xcoff_file_start (void)
27448 rs6000_gen_section_name (&xcoff_bss_section_name,
27449 main_input_filename, ".bss_");
27450 rs6000_gen_section_name (&xcoff_private_data_section_name,
27451 main_input_filename, ".rw_");
27452 rs6000_gen_section_name (&xcoff_read_only_section_name,
27453 main_input_filename, ".ro_");
27454 rs6000_gen_section_name (&xcoff_tls_data_section_name,
27455 main_input_filename, ".tls_");
27456 rs6000_gen_section_name (&xcoff_tbss_section_name,
27457 main_input_filename, ".tbss_[UL]");
27459 fputs ("\t.file\t", asm_out_file);
27460 output_quoted_string (asm_out_file, main_input_filename);
27461 fputc ('\n', asm_out_file);
27462 if (write_symbols != NO_DEBUG)
27463 switch_to_section (private_data_section);
27464 switch_to_section (text_section);
27465 if (profile_flag)
27466 fprintf (asm_out_file, "\t.extern %s\n", RS6000_MCOUNT);
27467 rs6000_file_start ();
27470 /* Output at end of assembler file.
27471 On the RS/6000, referencing data should automatically pull in text. */
27473 static void
27474 rs6000_xcoff_file_end (void)
27476 switch_to_section (text_section);
27477 fputs ("_section_.text:\n", asm_out_file);
27478 switch_to_section (data_section);
27479 fputs (TARGET_32BIT
27480 ? "\t.long _section_.text\n" : "\t.llong _section_.text\n",
27481 asm_out_file);
27484 #ifdef HAVE_AS_TLS
27485 static void
27486 rs6000_xcoff_encode_section_info (tree decl, rtx rtl, int first)
27488 rtx symbol;
27489 int flags;
27491 default_encode_section_info (decl, rtl, first);
27493 /* Careful not to prod global register variables. */
27494 if (!MEM_P (rtl))
27495 return;
27496 symbol = XEXP (rtl, 0);
27497 if (GET_CODE (symbol) != SYMBOL_REF)
27498 return;
27500 flags = SYMBOL_REF_FLAGS (symbol);
27502 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
27503 flags &= ~SYMBOL_FLAG_HAS_BLOCK_INFO;
27505 SYMBOL_REF_FLAGS (symbol) = flags;
27507 #endif /* HAVE_AS_TLS */
27508 #endif /* TARGET_XCOFF */
27510 /* Compute a (partial) cost for rtx X. Return true if the complete
27511 cost has been computed, and false if subexpressions should be
27512 scanned. In either case, *TOTAL contains the cost result. */
27514 static bool
27515 rs6000_rtx_costs (rtx x, int code, int outer_code, int opno ATTRIBUTE_UNUSED,
27516 int *total, bool speed)
27518 enum machine_mode mode = GET_MODE (x);
27520 switch (code)
27522 /* On the RS/6000, if it is valid in the insn, it is free. */
27523 case CONST_INT:
27524 if (((outer_code == SET
27525 || outer_code == PLUS
27526 || outer_code == MINUS)
27527 && (satisfies_constraint_I (x)
27528 || satisfies_constraint_L (x)))
27529 || (outer_code == AND
27530 && (satisfies_constraint_K (x)
27531 || (mode == SImode
27532 ? satisfies_constraint_L (x)
27533 : satisfies_constraint_J (x))
27534 || mask_operand (x, mode)
27535 || (mode == DImode
27536 && mask64_operand (x, DImode))))
27537 || ((outer_code == IOR || outer_code == XOR)
27538 && (satisfies_constraint_K (x)
27539 || (mode == SImode
27540 ? satisfies_constraint_L (x)
27541 : satisfies_constraint_J (x))))
27542 || outer_code == ASHIFT
27543 || outer_code == ASHIFTRT
27544 || outer_code == LSHIFTRT
27545 || outer_code == ROTATE
27546 || outer_code == ROTATERT
27547 || outer_code == ZERO_EXTRACT
27548 || (outer_code == MULT
27549 && satisfies_constraint_I (x))
27550 || ((outer_code == DIV || outer_code == UDIV
27551 || outer_code == MOD || outer_code == UMOD)
27552 && exact_log2 (INTVAL (x)) >= 0)
27553 || (outer_code == COMPARE
27554 && (satisfies_constraint_I (x)
27555 || satisfies_constraint_K (x)))
27556 || ((outer_code == EQ || outer_code == NE)
27557 && (satisfies_constraint_I (x)
27558 || satisfies_constraint_K (x)
27559 || (mode == SImode
27560 ? satisfies_constraint_L (x)
27561 : satisfies_constraint_J (x))))
27562 || (outer_code == GTU
27563 && satisfies_constraint_I (x))
27564 || (outer_code == LTU
27565 && satisfies_constraint_P (x)))
27567 *total = 0;
27568 return true;
27570 else if ((outer_code == PLUS
27571 && reg_or_add_cint_operand (x, VOIDmode))
27572 || (outer_code == MINUS
27573 && reg_or_sub_cint_operand (x, VOIDmode))
27574 || ((outer_code == SET
27575 || outer_code == IOR
27576 || outer_code == XOR)
27577 && (INTVAL (x)
27578 & ~ (unsigned HOST_WIDE_INT) 0xffffffff) == 0))
27580 *total = COSTS_N_INSNS (1);
27581 return true;
27583 /* FALLTHRU */
27585 case CONST_DOUBLE:
27586 case CONST:
27587 case HIGH:
27588 case SYMBOL_REF:
27589 case MEM:
27590 /* When optimizing for size, MEM should be slightly more expensive
27591 than generating address, e.g., (plus (reg) (const)).
27592 L1 cache latency is about two instructions. */
27593 *total = !speed ? COSTS_N_INSNS (1) + 1 : COSTS_N_INSNS (2);
27594 return true;
27596 case LABEL_REF:
27597 *total = 0;
27598 return true;
27600 case PLUS:
27601 case MINUS:
27602 if (FLOAT_MODE_P (mode))
27603 *total = rs6000_cost->fp;
27604 else
27605 *total = COSTS_N_INSNS (1);
27606 return false;
27608 case MULT:
27609 if (GET_CODE (XEXP (x, 1)) == CONST_INT
27610 && satisfies_constraint_I (XEXP (x, 1)))
27612 if (INTVAL (XEXP (x, 1)) >= -256
27613 && INTVAL (XEXP (x, 1)) <= 255)
27614 *total = rs6000_cost->mulsi_const9;
27615 else
27616 *total = rs6000_cost->mulsi_const;
27618 else if (mode == SFmode)
27619 *total = rs6000_cost->fp;
27620 else if (FLOAT_MODE_P (mode))
27621 *total = rs6000_cost->dmul;
27622 else if (mode == DImode)
27623 *total = rs6000_cost->muldi;
27624 else
27625 *total = rs6000_cost->mulsi;
27626 return false;
27628 case FMA:
27629 if (mode == SFmode)
27630 *total = rs6000_cost->fp;
27631 else
27632 *total = rs6000_cost->dmul;
27633 break;
27635 case DIV:
27636 case MOD:
27637 if (FLOAT_MODE_P (mode))
27639 *total = mode == DFmode ? rs6000_cost->ddiv
27640 : rs6000_cost->sdiv;
27641 return false;
27643 /* FALLTHRU */
27645 case UDIV:
27646 case UMOD:
27647 if (GET_CODE (XEXP (x, 1)) == CONST_INT
27648 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0)
27650 if (code == DIV || code == MOD)
27651 /* Shift, addze */
27652 *total = COSTS_N_INSNS (2);
27653 else
27654 /* Shift */
27655 *total = COSTS_N_INSNS (1);
27657 else
27659 if (GET_MODE (XEXP (x, 1)) == DImode)
27660 *total = rs6000_cost->divdi;
27661 else
27662 *total = rs6000_cost->divsi;
27664 /* Add in shift and subtract for MOD. */
27665 if (code == MOD || code == UMOD)
27666 *total += COSTS_N_INSNS (2);
27667 return false;
27669 case CTZ:
27670 case FFS:
27671 *total = COSTS_N_INSNS (4);
27672 return false;
27674 case POPCOUNT:
27675 *total = COSTS_N_INSNS (TARGET_POPCNTD ? 1 : 6);
27676 return false;
27678 case PARITY:
27679 *total = COSTS_N_INSNS (TARGET_CMPB ? 2 : 6);
27680 return false;
27682 case NOT:
27683 if (outer_code == AND || outer_code == IOR || outer_code == XOR)
27685 *total = 0;
27686 return false;
27688 /* FALLTHRU */
27690 case AND:
27691 case CLZ:
27692 case IOR:
27693 case XOR:
27694 case ZERO_EXTRACT:
27695 *total = COSTS_N_INSNS (1);
27696 return false;
27698 case ASHIFT:
27699 case ASHIFTRT:
27700 case LSHIFTRT:
27701 case ROTATE:
27702 case ROTATERT:
27703 /* Handle mul_highpart. */
27704 if (outer_code == TRUNCATE
27705 && GET_CODE (XEXP (x, 0)) == MULT)
27707 if (mode == DImode)
27708 *total = rs6000_cost->muldi;
27709 else
27710 *total = rs6000_cost->mulsi;
27711 return true;
27713 else if (outer_code == AND)
27714 *total = 0;
27715 else
27716 *total = COSTS_N_INSNS (1);
27717 return false;
27719 case SIGN_EXTEND:
27720 case ZERO_EXTEND:
27721 if (GET_CODE (XEXP (x, 0)) == MEM)
27722 *total = 0;
27723 else
27724 *total = COSTS_N_INSNS (1);
27725 return false;
27727 case COMPARE:
27728 case NEG:
27729 case ABS:
27730 if (!FLOAT_MODE_P (mode))
27732 *total = COSTS_N_INSNS (1);
27733 return false;
27735 /* FALLTHRU */
27737 case FLOAT:
27738 case UNSIGNED_FLOAT:
27739 case FIX:
27740 case UNSIGNED_FIX:
27741 case FLOAT_TRUNCATE:
27742 *total = rs6000_cost->fp;
27743 return false;
27745 case FLOAT_EXTEND:
27746 if (mode == DFmode)
27747 *total = 0;
27748 else
27749 *total = rs6000_cost->fp;
27750 return false;
27752 case UNSPEC:
27753 switch (XINT (x, 1))
27755 case UNSPEC_FRSP:
27756 *total = rs6000_cost->fp;
27757 return true;
27759 default:
27760 break;
27762 break;
27764 case CALL:
27765 case IF_THEN_ELSE:
27766 if (!speed)
27768 *total = COSTS_N_INSNS (1);
27769 return true;
27771 else if (FLOAT_MODE_P (mode)
27772 && TARGET_PPC_GFXOPT && TARGET_HARD_FLOAT && TARGET_FPRS)
27774 *total = rs6000_cost->fp;
27775 return false;
27777 break;
27779 case EQ:
27780 case GTU:
27781 case LTU:
27782 /* Carry bit requires mode == Pmode.
27783 NEG or PLUS already counted so only add one. */
27784 if (mode == Pmode
27785 && (outer_code == NEG || outer_code == PLUS))
27787 *total = COSTS_N_INSNS (1);
27788 return true;
27790 if (outer_code == SET)
27792 if (XEXP (x, 1) == const0_rtx)
27794 if (TARGET_ISEL && !TARGET_MFCRF)
27795 *total = COSTS_N_INSNS (8);
27796 else
27797 *total = COSTS_N_INSNS (2);
27798 return true;
27800 else if (mode == Pmode)
27802 *total = COSTS_N_INSNS (3);
27803 return false;
27806 /* FALLTHRU */
27808 case GT:
27809 case LT:
27810 case UNORDERED:
27811 if (outer_code == SET && (XEXP (x, 1) == const0_rtx))
27813 if (TARGET_ISEL && !TARGET_MFCRF)
27814 *total = COSTS_N_INSNS (8);
27815 else
27816 *total = COSTS_N_INSNS (2);
27817 return true;
27819 /* CC COMPARE. */
27820 if (outer_code == COMPARE)
27822 *total = 0;
27823 return true;
27825 break;
27827 default:
27828 break;
27831 return false;
27834 /* Debug form of r6000_rtx_costs that is selected if -mdebug=cost. */
27836 static bool
27837 rs6000_debug_rtx_costs (rtx x, int code, int outer_code, int opno, int *total,
27838 bool speed)
27840 bool ret = rs6000_rtx_costs (x, code, outer_code, opno, total, speed);
27842 fprintf (stderr,
27843 "\nrs6000_rtx_costs, return = %s, code = %s, outer_code = %s, "
27844 "opno = %d, total = %d, speed = %s, x:\n",
27845 ret ? "complete" : "scan inner",
27846 GET_RTX_NAME (code),
27847 GET_RTX_NAME (outer_code),
27848 opno,
27849 *total,
27850 speed ? "true" : "false");
27852 debug_rtx (x);
27854 return ret;
27857 /* Debug form of ADDRESS_COST that is selected if -mdebug=cost. */
27859 static int
27860 rs6000_debug_address_cost (rtx x, enum machine_mode mode,
27861 addr_space_t as, bool speed)
27863 int ret = TARGET_ADDRESS_COST (x, mode, as, speed);
27865 fprintf (stderr, "\nrs6000_address_cost, return = %d, speed = %s, x:\n",
27866 ret, speed ? "true" : "false");
27867 debug_rtx (x);
27869 return ret;
27873 /* A C expression returning the cost of moving data from a register of class
27874 CLASS1 to one of CLASS2. */
27876 static int
27877 rs6000_register_move_cost (enum machine_mode mode,
27878 reg_class_t from, reg_class_t to)
27880 int ret;
27882 if (TARGET_DEBUG_COST)
27883 dbg_cost_ctrl++;
27885 /* Moves from/to GENERAL_REGS. */
27886 if (reg_classes_intersect_p (to, GENERAL_REGS)
27887 || reg_classes_intersect_p (from, GENERAL_REGS))
27889 reg_class_t rclass = from;
27891 if (! reg_classes_intersect_p (to, GENERAL_REGS))
27892 rclass = to;
27894 if (rclass == FLOAT_REGS || rclass == ALTIVEC_REGS || rclass == VSX_REGS)
27895 ret = (rs6000_memory_move_cost (mode, rclass, false)
27896 + rs6000_memory_move_cost (mode, GENERAL_REGS, false));
27898 /* It's more expensive to move CR_REGS than CR0_REGS because of the
27899 shift. */
27900 else if (rclass == CR_REGS)
27901 ret = 4;
27903 /* For those processors that have slow LR/CTR moves, make them more
27904 expensive than memory in order to bias spills to memory .*/
27905 else if ((rs6000_cpu == PROCESSOR_POWER6
27906 || rs6000_cpu == PROCESSOR_POWER7
27907 || rs6000_cpu == PROCESSOR_POWER8)
27908 && reg_classes_intersect_p (rclass, LINK_OR_CTR_REGS))
27909 ret = 6 * hard_regno_nregs[0][mode];
27911 else
27912 /* A move will cost one instruction per GPR moved. */
27913 ret = 2 * hard_regno_nregs[0][mode];
27916 /* If we have VSX, we can easily move between FPR or Altivec registers. */
27917 else if (VECTOR_MEM_VSX_P (mode)
27918 && reg_classes_intersect_p (to, VSX_REGS)
27919 && reg_classes_intersect_p (from, VSX_REGS))
27920 ret = 2 * hard_regno_nregs[32][mode];
27922 /* Moving between two similar registers is just one instruction. */
27923 else if (reg_classes_intersect_p (to, from))
27924 ret = (mode == TFmode || mode == TDmode) ? 4 : 2;
27926 /* Everything else has to go through GENERAL_REGS. */
27927 else
27928 ret = (rs6000_register_move_cost (mode, GENERAL_REGS, to)
27929 + rs6000_register_move_cost (mode, from, GENERAL_REGS));
27931 if (TARGET_DEBUG_COST)
27933 if (dbg_cost_ctrl == 1)
27934 fprintf (stderr,
27935 "rs6000_register_move_cost:, ret=%d, mode=%s, from=%s, to=%s\n",
27936 ret, GET_MODE_NAME (mode), reg_class_names[from],
27937 reg_class_names[to]);
27938 dbg_cost_ctrl--;
27941 return ret;
27944 /* A C expressions returning the cost of moving data of MODE from a register to
27945 or from memory. */
27947 static int
27948 rs6000_memory_move_cost (enum machine_mode mode, reg_class_t rclass,
27949 bool in ATTRIBUTE_UNUSED)
27951 int ret;
27953 if (TARGET_DEBUG_COST)
27954 dbg_cost_ctrl++;
27956 if (reg_classes_intersect_p (rclass, GENERAL_REGS))
27957 ret = 4 * hard_regno_nregs[0][mode];
27958 else if ((reg_classes_intersect_p (rclass, FLOAT_REGS)
27959 || reg_classes_intersect_p (rclass, VSX_REGS)))
27960 ret = 4 * hard_regno_nregs[32][mode];
27961 else if (reg_classes_intersect_p (rclass, ALTIVEC_REGS))
27962 ret = 4 * hard_regno_nregs[FIRST_ALTIVEC_REGNO][mode];
27963 else
27964 ret = 4 + rs6000_register_move_cost (mode, rclass, GENERAL_REGS);
27966 if (TARGET_DEBUG_COST)
27968 if (dbg_cost_ctrl == 1)
27969 fprintf (stderr,
27970 "rs6000_memory_move_cost: ret=%d, mode=%s, rclass=%s, in=%d\n",
27971 ret, GET_MODE_NAME (mode), reg_class_names[rclass], in);
27972 dbg_cost_ctrl--;
27975 return ret;
27978 /* Returns a code for a target-specific builtin that implements
27979 reciprocal of the function, or NULL_TREE if not available. */
27981 static tree
27982 rs6000_builtin_reciprocal (unsigned int fn, bool md_fn,
27983 bool sqrt ATTRIBUTE_UNUSED)
27985 if (optimize_insn_for_size_p ())
27986 return NULL_TREE;
27988 if (md_fn)
27989 switch (fn)
27991 case VSX_BUILTIN_XVSQRTDP:
27992 if (!RS6000_RECIP_AUTO_RSQRTE_P (V2DFmode))
27993 return NULL_TREE;
27995 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_2DF];
27997 case VSX_BUILTIN_XVSQRTSP:
27998 if (!RS6000_RECIP_AUTO_RSQRTE_P (V4SFmode))
27999 return NULL_TREE;
28001 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_4SF];
28003 default:
28004 return NULL_TREE;
28007 else
28008 switch (fn)
28010 case BUILT_IN_SQRT:
28011 if (!RS6000_RECIP_AUTO_RSQRTE_P (DFmode))
28012 return NULL_TREE;
28014 return rs6000_builtin_decls[RS6000_BUILTIN_RSQRT];
28016 case BUILT_IN_SQRTF:
28017 if (!RS6000_RECIP_AUTO_RSQRTE_P (SFmode))
28018 return NULL_TREE;
28020 return rs6000_builtin_decls[RS6000_BUILTIN_RSQRTF];
28022 default:
28023 return NULL_TREE;
28027 /* Load up a constant. If the mode is a vector mode, splat the value across
28028 all of the vector elements. */
28030 static rtx
28031 rs6000_load_constant_and_splat (enum machine_mode mode, REAL_VALUE_TYPE dconst)
28033 rtx reg;
28035 if (mode == SFmode || mode == DFmode)
28037 rtx d = CONST_DOUBLE_FROM_REAL_VALUE (dconst, mode);
28038 reg = force_reg (mode, d);
28040 else if (mode == V4SFmode)
28042 rtx d = CONST_DOUBLE_FROM_REAL_VALUE (dconst, SFmode);
28043 rtvec v = gen_rtvec (4, d, d, d, d);
28044 reg = gen_reg_rtx (mode);
28045 rs6000_expand_vector_init (reg, gen_rtx_PARALLEL (mode, v));
28047 else if (mode == V2DFmode)
28049 rtx d = CONST_DOUBLE_FROM_REAL_VALUE (dconst, DFmode);
28050 rtvec v = gen_rtvec (2, d, d);
28051 reg = gen_reg_rtx (mode);
28052 rs6000_expand_vector_init (reg, gen_rtx_PARALLEL (mode, v));
28054 else
28055 gcc_unreachable ();
28057 return reg;
28060 /* Generate an FMA instruction. */
28062 static void
28063 rs6000_emit_madd (rtx target, rtx m1, rtx m2, rtx a)
28065 enum machine_mode mode = GET_MODE (target);
28066 rtx dst;
28068 dst = expand_ternary_op (mode, fma_optab, m1, m2, a, target, 0);
28069 gcc_assert (dst != NULL);
28071 if (dst != target)
28072 emit_move_insn (target, dst);
28075 /* Generate a FMSUB instruction: dst = fma(m1, m2, -a). */
28077 static void
28078 rs6000_emit_msub (rtx target, rtx m1, rtx m2, rtx a)
28080 enum machine_mode mode = GET_MODE (target);
28081 rtx dst;
28083 /* Altivec does not support fms directly;
28084 generate in terms of fma in that case. */
28085 if (optab_handler (fms_optab, mode) != CODE_FOR_nothing)
28086 dst = expand_ternary_op (mode, fms_optab, m1, m2, a, target, 0);
28087 else
28089 a = expand_unop (mode, neg_optab, a, NULL_RTX, 0);
28090 dst = expand_ternary_op (mode, fma_optab, m1, m2, a, target, 0);
28092 gcc_assert (dst != NULL);
28094 if (dst != target)
28095 emit_move_insn (target, dst);
28098 /* Generate a FNMSUB instruction: dst = -fma(m1, m2, -a). */
28100 static void
28101 rs6000_emit_nmsub (rtx dst, rtx m1, rtx m2, rtx a)
28103 enum machine_mode mode = GET_MODE (dst);
28104 rtx r;
28106 /* This is a tad more complicated, since the fnma_optab is for
28107 a different expression: fma(-m1, m2, a), which is the same
28108 thing except in the case of signed zeros.
28110 Fortunately we know that if FMA is supported that FNMSUB is
28111 also supported in the ISA. Just expand it directly. */
28113 gcc_assert (optab_handler (fma_optab, mode) != CODE_FOR_nothing);
28115 r = gen_rtx_NEG (mode, a);
28116 r = gen_rtx_FMA (mode, m1, m2, r);
28117 r = gen_rtx_NEG (mode, r);
28118 emit_insn (gen_rtx_SET (VOIDmode, dst, r));
28121 /* Newton-Raphson approximation of floating point divide DST = N/D. If NOTE_P,
28122 add a reg_note saying that this was a division. Support both scalar and
28123 vector divide. Assumes no trapping math and finite arguments. */
28125 void
28126 rs6000_emit_swdiv (rtx dst, rtx n, rtx d, bool note_p)
28128 enum machine_mode mode = GET_MODE (dst);
28129 rtx one, x0, e0, x1, xprev, eprev, xnext, enext, u, v;
28130 int i;
28132 /* Low precision estimates guarantee 5 bits of accuracy. High
28133 precision estimates guarantee 14 bits of accuracy. SFmode
28134 requires 23 bits of accuracy. DFmode requires 52 bits of
28135 accuracy. Each pass at least doubles the accuracy, leading
28136 to the following. */
28137 int passes = (TARGET_RECIP_PRECISION) ? 1 : 3;
28138 if (mode == DFmode || mode == V2DFmode)
28139 passes++;
28141 enum insn_code code = optab_handler (smul_optab, mode);
28142 gen_2arg_fn_t gen_mul = (gen_2arg_fn_t) GEN_FCN (code);
28144 gcc_assert (code != CODE_FOR_nothing);
28146 one = rs6000_load_constant_and_splat (mode, dconst1);
28148 /* x0 = 1./d estimate */
28149 x0 = gen_reg_rtx (mode);
28150 emit_insn (gen_rtx_SET (VOIDmode, x0,
28151 gen_rtx_UNSPEC (mode, gen_rtvec (1, d),
28152 UNSPEC_FRES)));
28154 /* Each iteration but the last calculates x_(i+1) = x_i * (2 - d * x_i). */
28155 if (passes > 1) {
28157 /* e0 = 1. - d * x0 */
28158 e0 = gen_reg_rtx (mode);
28159 rs6000_emit_nmsub (e0, d, x0, one);
28161 /* x1 = x0 + e0 * x0 */
28162 x1 = gen_reg_rtx (mode);
28163 rs6000_emit_madd (x1, e0, x0, x0);
28165 for (i = 0, xprev = x1, eprev = e0; i < passes - 2;
28166 ++i, xprev = xnext, eprev = enext) {
28168 /* enext = eprev * eprev */
28169 enext = gen_reg_rtx (mode);
28170 emit_insn (gen_mul (enext, eprev, eprev));
28172 /* xnext = xprev + enext * xprev */
28173 xnext = gen_reg_rtx (mode);
28174 rs6000_emit_madd (xnext, enext, xprev, xprev);
28177 } else
28178 xprev = x0;
28180 /* The last iteration calculates x_(i+1) = n * x_i * (2 - d * x_i). */
28182 /* u = n * xprev */
28183 u = gen_reg_rtx (mode);
28184 emit_insn (gen_mul (u, n, xprev));
28186 /* v = n - (d * u) */
28187 v = gen_reg_rtx (mode);
28188 rs6000_emit_nmsub (v, d, u, n);
28190 /* dst = (v * xprev) + u */
28191 rs6000_emit_madd (dst, v, xprev, u);
28193 if (note_p)
28194 add_reg_note (get_last_insn (), REG_EQUAL, gen_rtx_DIV (mode, n, d));
28197 /* Newton-Raphson approximation of single/double-precision floating point
28198 rsqrt. Assumes no trapping math and finite arguments. */
28200 void
28201 rs6000_emit_swrsqrt (rtx dst, rtx src)
28203 enum machine_mode mode = GET_MODE (src);
28204 rtx x0 = gen_reg_rtx (mode);
28205 rtx y = gen_reg_rtx (mode);
28207 /* Low precision estimates guarantee 5 bits of accuracy. High
28208 precision estimates guarantee 14 bits of accuracy. SFmode
28209 requires 23 bits of accuracy. DFmode requires 52 bits of
28210 accuracy. Each pass at least doubles the accuracy, leading
28211 to the following. */
28212 int passes = (TARGET_RECIP_PRECISION) ? 1 : 3;
28213 if (mode == DFmode || mode == V2DFmode)
28214 passes++;
28216 REAL_VALUE_TYPE dconst3_2;
28217 int i;
28218 rtx halfthree;
28219 enum insn_code code = optab_handler (smul_optab, mode);
28220 gen_2arg_fn_t gen_mul = (gen_2arg_fn_t) GEN_FCN (code);
28222 gcc_assert (code != CODE_FOR_nothing);
28224 /* Load up the constant 1.5 either as a scalar, or as a vector. */
28225 real_from_integer (&dconst3_2, VOIDmode, 3, 0, 0);
28226 SET_REAL_EXP (&dconst3_2, REAL_EXP (&dconst3_2) - 1);
28228 halfthree = rs6000_load_constant_and_splat (mode, dconst3_2);
28230 /* x0 = rsqrt estimate */
28231 emit_insn (gen_rtx_SET (VOIDmode, x0,
28232 gen_rtx_UNSPEC (mode, gen_rtvec (1, src),
28233 UNSPEC_RSQRT)));
28235 /* y = 0.5 * src = 1.5 * src - src -> fewer constants */
28236 rs6000_emit_msub (y, src, halfthree, src);
28238 for (i = 0; i < passes; i++)
28240 rtx x1 = gen_reg_rtx (mode);
28241 rtx u = gen_reg_rtx (mode);
28242 rtx v = gen_reg_rtx (mode);
28244 /* x1 = x0 * (1.5 - y * (x0 * x0)) */
28245 emit_insn (gen_mul (u, x0, x0));
28246 rs6000_emit_nmsub (v, y, u, halfthree);
28247 emit_insn (gen_mul (x1, x0, v));
28248 x0 = x1;
28251 emit_move_insn (dst, x0);
28252 return;
28255 /* Emit popcount intrinsic on TARGET_POPCNTB (Power5) and TARGET_POPCNTD
28256 (Power7) targets. DST is the target, and SRC is the argument operand. */
28258 void
28259 rs6000_emit_popcount (rtx dst, rtx src)
28261 enum machine_mode mode = GET_MODE (dst);
28262 rtx tmp1, tmp2;
28264 /* Use the PPC ISA 2.06 popcnt{w,d} instruction if we can. */
28265 if (TARGET_POPCNTD)
28267 if (mode == SImode)
28268 emit_insn (gen_popcntdsi2 (dst, src));
28269 else
28270 emit_insn (gen_popcntddi2 (dst, src));
28271 return;
28274 tmp1 = gen_reg_rtx (mode);
28276 if (mode == SImode)
28278 emit_insn (gen_popcntbsi2 (tmp1, src));
28279 tmp2 = expand_mult (SImode, tmp1, GEN_INT (0x01010101),
28280 NULL_RTX, 0);
28281 tmp2 = force_reg (SImode, tmp2);
28282 emit_insn (gen_lshrsi3 (dst, tmp2, GEN_INT (24)));
28284 else
28286 emit_insn (gen_popcntbdi2 (tmp1, src));
28287 tmp2 = expand_mult (DImode, tmp1,
28288 GEN_INT ((HOST_WIDE_INT)
28289 0x01010101 << 32 | 0x01010101),
28290 NULL_RTX, 0);
28291 tmp2 = force_reg (DImode, tmp2);
28292 emit_insn (gen_lshrdi3 (dst, tmp2, GEN_INT (56)));
28297 /* Emit parity intrinsic on TARGET_POPCNTB targets. DST is the
28298 target, and SRC is the argument operand. */
28300 void
28301 rs6000_emit_parity (rtx dst, rtx src)
28303 enum machine_mode mode = GET_MODE (dst);
28304 rtx tmp;
28306 tmp = gen_reg_rtx (mode);
28308 /* Use the PPC ISA 2.05 prtyw/prtyd instruction if we can. */
28309 if (TARGET_CMPB)
28311 if (mode == SImode)
28313 emit_insn (gen_popcntbsi2 (tmp, src));
28314 emit_insn (gen_paritysi2_cmpb (dst, tmp));
28316 else
28318 emit_insn (gen_popcntbdi2 (tmp, src));
28319 emit_insn (gen_paritydi2_cmpb (dst, tmp));
28321 return;
28324 if (mode == SImode)
28326 /* Is mult+shift >= shift+xor+shift+xor? */
28327 if (rs6000_cost->mulsi_const >= COSTS_N_INSNS (3))
28329 rtx tmp1, tmp2, tmp3, tmp4;
28331 tmp1 = gen_reg_rtx (SImode);
28332 emit_insn (gen_popcntbsi2 (tmp1, src));
28334 tmp2 = gen_reg_rtx (SImode);
28335 emit_insn (gen_lshrsi3 (tmp2, tmp1, GEN_INT (16)));
28336 tmp3 = gen_reg_rtx (SImode);
28337 emit_insn (gen_xorsi3 (tmp3, tmp1, tmp2));
28339 tmp4 = gen_reg_rtx (SImode);
28340 emit_insn (gen_lshrsi3 (tmp4, tmp3, GEN_INT (8)));
28341 emit_insn (gen_xorsi3 (tmp, tmp3, tmp4));
28343 else
28344 rs6000_emit_popcount (tmp, src);
28345 emit_insn (gen_andsi3 (dst, tmp, const1_rtx));
28347 else
28349 /* Is mult+shift >= shift+xor+shift+xor+shift+xor? */
28350 if (rs6000_cost->muldi >= COSTS_N_INSNS (5))
28352 rtx tmp1, tmp2, tmp3, tmp4, tmp5, tmp6;
28354 tmp1 = gen_reg_rtx (DImode);
28355 emit_insn (gen_popcntbdi2 (tmp1, src));
28357 tmp2 = gen_reg_rtx (DImode);
28358 emit_insn (gen_lshrdi3 (tmp2, tmp1, GEN_INT (32)));
28359 tmp3 = gen_reg_rtx (DImode);
28360 emit_insn (gen_xordi3 (tmp3, tmp1, tmp2));
28362 tmp4 = gen_reg_rtx (DImode);
28363 emit_insn (gen_lshrdi3 (tmp4, tmp3, GEN_INT (16)));
28364 tmp5 = gen_reg_rtx (DImode);
28365 emit_insn (gen_xordi3 (tmp5, tmp3, tmp4));
28367 tmp6 = gen_reg_rtx (DImode);
28368 emit_insn (gen_lshrdi3 (tmp6, tmp5, GEN_INT (8)));
28369 emit_insn (gen_xordi3 (tmp, tmp5, tmp6));
28371 else
28372 rs6000_emit_popcount (tmp, src);
28373 emit_insn (gen_anddi3 (dst, tmp, const1_rtx));
28377 /* Expand an Altivec constant permutation. Return true if we match
28378 an efficient implementation; false to fall back to VPERM. */
28380 bool
28381 altivec_expand_vec_perm_const (rtx operands[4])
28383 struct altivec_perm_insn {
28384 HOST_WIDE_INT mask;
28385 enum insn_code impl;
28386 unsigned char perm[16];
28388 static const struct altivec_perm_insn patterns[] = {
28389 { OPTION_MASK_ALTIVEC, CODE_FOR_altivec_vpkuhum,
28390 { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } },
28391 { OPTION_MASK_ALTIVEC, CODE_FOR_altivec_vpkuwum,
28392 { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } },
28393 { OPTION_MASK_ALTIVEC, CODE_FOR_altivec_vmrghb,
28394 { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 } },
28395 { OPTION_MASK_ALTIVEC, CODE_FOR_altivec_vmrghh,
28396 { 0, 1, 16, 17, 2, 3, 18, 19, 4, 5, 20, 21, 6, 7, 22, 23 } },
28397 { OPTION_MASK_ALTIVEC, CODE_FOR_altivec_vmrghw,
28398 { 0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23 } },
28399 { OPTION_MASK_ALTIVEC, CODE_FOR_altivec_vmrglb,
28400 { 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } },
28401 { OPTION_MASK_ALTIVEC, CODE_FOR_altivec_vmrglh,
28402 { 8, 9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } },
28403 { OPTION_MASK_ALTIVEC, CODE_FOR_altivec_vmrglw,
28404 { 8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } },
28405 { OPTION_MASK_P8_VECTOR, CODE_FOR_p8_vmrgew,
28406 { 0, 1, 2, 3, 16, 17, 18, 19, 8, 9, 10, 11, 24, 25, 26, 27 } },
28407 { OPTION_MASK_P8_VECTOR, CODE_FOR_p8_vmrgow,
28408 { 4, 5, 6, 7, 20, 21, 22, 23, 12, 13, 14, 15, 28, 29, 30, 31 } }
28411 unsigned int i, j, elt, which;
28412 unsigned char perm[16];
28413 rtx target, op0, op1, sel, x;
28414 bool one_vec;
28416 target = operands[0];
28417 op0 = operands[1];
28418 op1 = operands[2];
28419 sel = operands[3];
28421 /* Unpack the constant selector. */
28422 for (i = which = 0; i < 16; ++i)
28424 rtx e = XVECEXP (sel, 0, i);
28425 elt = INTVAL (e) & 31;
28426 which |= (elt < 16 ? 1 : 2);
28427 perm[i] = elt;
28430 /* Simplify the constant selector based on operands. */
28431 switch (which)
28433 default:
28434 gcc_unreachable ();
28436 case 3:
28437 one_vec = false;
28438 if (!rtx_equal_p (op0, op1))
28439 break;
28440 /* FALLTHRU */
28442 case 2:
28443 for (i = 0; i < 16; ++i)
28444 perm[i] &= 15;
28445 op0 = op1;
28446 one_vec = true;
28447 break;
28449 case 1:
28450 op1 = op0;
28451 one_vec = true;
28452 break;
28455 /* Look for splat patterns. */
28456 if (one_vec)
28458 elt = perm[0];
28460 for (i = 0; i < 16; ++i)
28461 if (perm[i] != elt)
28462 break;
28463 if (i == 16)
28465 emit_insn (gen_altivec_vspltb (target, op0, GEN_INT (elt)));
28466 return true;
28469 if (elt % 2 == 0)
28471 for (i = 0; i < 16; i += 2)
28472 if (perm[i] != elt || perm[i + 1] != elt + 1)
28473 break;
28474 if (i == 16)
28476 int field = BYTES_BIG_ENDIAN ? elt / 2 : 7 - elt / 2;
28477 x = gen_reg_rtx (V8HImode);
28478 emit_insn (gen_altivec_vsplth (x, gen_lowpart (V8HImode, op0),
28479 GEN_INT (field)));
28480 emit_move_insn (target, gen_lowpart (V16QImode, x));
28481 return true;
28485 if (elt % 4 == 0)
28487 for (i = 0; i < 16; i += 4)
28488 if (perm[i] != elt
28489 || perm[i + 1] != elt + 1
28490 || perm[i + 2] != elt + 2
28491 || perm[i + 3] != elt + 3)
28492 break;
28493 if (i == 16)
28495 int field = BYTES_BIG_ENDIAN ? elt / 4 : 3 - elt / 4;
28496 x = gen_reg_rtx (V4SImode);
28497 emit_insn (gen_altivec_vspltw (x, gen_lowpart (V4SImode, op0),
28498 GEN_INT (field)));
28499 emit_move_insn (target, gen_lowpart (V16QImode, x));
28500 return true;
28505 /* Look for merge and pack patterns. */
28506 for (j = 0; j < ARRAY_SIZE (patterns); ++j)
28508 bool swapped;
28510 if ((patterns[j].mask & rs6000_isa_flags) == 0)
28511 continue;
28513 elt = patterns[j].perm[0];
28514 if (perm[0] == elt)
28515 swapped = false;
28516 else if (perm[0] == elt + 16)
28517 swapped = true;
28518 else
28519 continue;
28520 for (i = 1; i < 16; ++i)
28522 elt = patterns[j].perm[i];
28523 if (swapped)
28524 elt = (elt >= 16 ? elt - 16 : elt + 16);
28525 else if (one_vec && elt >= 16)
28526 elt -= 16;
28527 if (perm[i] != elt)
28528 break;
28530 if (i == 16)
28532 enum insn_code icode = patterns[j].impl;
28533 enum machine_mode omode = insn_data[icode].operand[0].mode;
28534 enum machine_mode imode = insn_data[icode].operand[1].mode;
28536 /* For little-endian, the two input operands must be swapped
28537 (or swapped back) to ensure proper right-to-left numbering
28538 from 0 to 2N-1. */
28539 if (swapped ^ !BYTES_BIG_ENDIAN)
28540 x = op0, op0 = op1, op1 = x;
28541 if (imode != V16QImode)
28543 op0 = gen_lowpart (imode, op0);
28544 op1 = gen_lowpart (imode, op1);
28546 if (omode == V16QImode)
28547 x = target;
28548 else
28549 x = gen_reg_rtx (omode);
28550 emit_insn (GEN_FCN (icode) (x, op0, op1));
28551 if (omode != V16QImode)
28552 emit_move_insn (target, gen_lowpart (V16QImode, x));
28553 return true;
28557 return false;
28560 /* Expand a Paired Single, VSX Permute Doubleword, or SPE constant permutation.
28561 Return true if we match an efficient implementation. */
28563 static bool
28564 rs6000_expand_vec_perm_const_1 (rtx target, rtx op0, rtx op1,
28565 unsigned char perm0, unsigned char perm1)
28567 rtx x;
28569 /* If both selectors come from the same operand, fold to single op. */
28570 if ((perm0 & 2) == (perm1 & 2))
28572 if (perm0 & 2)
28573 op0 = op1;
28574 else
28575 op1 = op0;
28577 /* If both operands are equal, fold to simpler permutation. */
28578 if (rtx_equal_p (op0, op1))
28580 perm0 = perm0 & 1;
28581 perm1 = (perm1 & 1) + 2;
28583 /* If the first selector comes from the second operand, swap. */
28584 else if (perm0 & 2)
28586 if (perm1 & 2)
28587 return false;
28588 perm0 -= 2;
28589 perm1 += 2;
28590 x = op0, op0 = op1, op1 = x;
28592 /* If the second selector does not come from the second operand, fail. */
28593 else if ((perm1 & 2) == 0)
28594 return false;
28596 /* Success! */
28597 if (target != NULL)
28599 enum machine_mode vmode, dmode;
28600 rtvec v;
28602 vmode = GET_MODE (target);
28603 gcc_assert (GET_MODE_NUNITS (vmode) == 2);
28604 dmode = mode_for_vector (GET_MODE_INNER (vmode), 4);
28606 x = gen_rtx_VEC_CONCAT (dmode, op0, op1);
28607 v = gen_rtvec (2, GEN_INT (perm0), GEN_INT (perm1));
28608 x = gen_rtx_VEC_SELECT (vmode, x, gen_rtx_PARALLEL (VOIDmode, v));
28609 emit_insn (gen_rtx_SET (VOIDmode, target, x));
28611 return true;
28614 bool
28615 rs6000_expand_vec_perm_const (rtx operands[4])
28617 rtx target, op0, op1, sel;
28618 unsigned char perm0, perm1;
28620 target = operands[0];
28621 op0 = operands[1];
28622 op1 = operands[2];
28623 sel = operands[3];
28625 /* Unpack the constant selector. */
28626 perm0 = INTVAL (XVECEXP (sel, 0, 0)) & 3;
28627 perm1 = INTVAL (XVECEXP (sel, 0, 1)) & 3;
28629 return rs6000_expand_vec_perm_const_1 (target, op0, op1, perm0, perm1);
28632 /* Test whether a constant permutation is supported. */
28634 static bool
28635 rs6000_vectorize_vec_perm_const_ok (enum machine_mode vmode,
28636 const unsigned char *sel)
28638 /* AltiVec (and thus VSX) can handle arbitrary permutations. */
28639 if (TARGET_ALTIVEC)
28640 return true;
28642 /* Check for ps_merge* or evmerge* insns. */
28643 if ((TARGET_PAIRED_FLOAT && vmode == V2SFmode)
28644 || (TARGET_SPE && vmode == V2SImode))
28646 rtx op0 = gen_raw_REG (vmode, LAST_VIRTUAL_REGISTER + 1);
28647 rtx op1 = gen_raw_REG (vmode, LAST_VIRTUAL_REGISTER + 2);
28648 return rs6000_expand_vec_perm_const_1 (NULL, op0, op1, sel[0], sel[1]);
28651 return false;
28654 /* A subroutine for rs6000_expand_extract_even & rs6000_expand_interleave. */
28656 static void
28657 rs6000_do_expand_vec_perm (rtx target, rtx op0, rtx op1,
28658 enum machine_mode vmode, unsigned nelt, rtx perm[])
28660 enum machine_mode imode;
28661 rtx x;
28663 imode = vmode;
28664 if (GET_MODE_CLASS (vmode) != MODE_VECTOR_INT)
28666 imode = GET_MODE_INNER (vmode);
28667 imode = mode_for_size (GET_MODE_BITSIZE (imode), MODE_INT, 0);
28668 imode = mode_for_vector (imode, nelt);
28671 x = gen_rtx_CONST_VECTOR (imode, gen_rtvec_v (nelt, perm));
28672 x = expand_vec_perm (vmode, op0, op1, x, target);
28673 if (x != target)
28674 emit_move_insn (target, x);
28677 /* Expand an extract even operation. */
28679 void
28680 rs6000_expand_extract_even (rtx target, rtx op0, rtx op1)
28682 enum machine_mode vmode = GET_MODE (target);
28683 unsigned i, nelt = GET_MODE_NUNITS (vmode);
28684 rtx perm[16];
28686 for (i = 0; i < nelt; i++)
28687 perm[i] = GEN_INT (i * 2);
28689 rs6000_do_expand_vec_perm (target, op0, op1, vmode, nelt, perm);
28692 /* Expand a vector interleave operation. */
28694 void
28695 rs6000_expand_interleave (rtx target, rtx op0, rtx op1, bool highp)
28697 enum machine_mode vmode = GET_MODE (target);
28698 unsigned i, high, nelt = GET_MODE_NUNITS (vmode);
28699 rtx perm[16];
28701 high = (highp == BYTES_BIG_ENDIAN ? 0 : nelt / 2);
28702 for (i = 0; i < nelt / 2; i++)
28704 perm[i * 2] = GEN_INT (i + high);
28705 perm[i * 2 + 1] = GEN_INT (i + nelt + high);
28708 rs6000_do_expand_vec_perm (target, op0, op1, vmode, nelt, perm);
28711 /* Return an RTX representing where to find the function value of a
28712 function returning MODE. */
28713 static rtx
28714 rs6000_complex_function_value (enum machine_mode mode)
28716 unsigned int regno;
28717 rtx r1, r2;
28718 enum machine_mode inner = GET_MODE_INNER (mode);
28719 unsigned int inner_bytes = GET_MODE_SIZE (inner);
28721 if (FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
28722 regno = FP_ARG_RETURN;
28723 else
28725 regno = GP_ARG_RETURN;
28727 /* 32-bit is OK since it'll go in r3/r4. */
28728 if (TARGET_32BIT && inner_bytes >= 4)
28729 return gen_rtx_REG (mode, regno);
28732 if (inner_bytes >= 8)
28733 return gen_rtx_REG (mode, regno);
28735 r1 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno),
28736 const0_rtx);
28737 r2 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno + 1),
28738 GEN_INT (inner_bytes));
28739 return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
28742 /* Target hook for TARGET_FUNCTION_VALUE.
28744 On the SPE, both FPs and vectors are returned in r3.
28746 On RS/6000 an integer value is in r3 and a floating-point value is in
28747 fp1, unless -msoft-float. */
28749 static rtx
28750 rs6000_function_value (const_tree valtype,
28751 const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
28752 bool outgoing ATTRIBUTE_UNUSED)
28754 enum machine_mode mode;
28755 unsigned int regno;
28757 /* Special handling for structs in darwin64. */
28758 if (TARGET_MACHO
28759 && rs6000_darwin64_struct_check_p (TYPE_MODE (valtype), valtype))
28761 CUMULATIVE_ARGS valcum;
28762 rtx valret;
28764 valcum.words = 0;
28765 valcum.fregno = FP_ARG_MIN_REG;
28766 valcum.vregno = ALTIVEC_ARG_MIN_REG;
28767 /* Do a trial code generation as if this were going to be passed as
28768 an argument; if any part goes in memory, we return NULL. */
28769 valret = rs6000_darwin64_record_arg (&valcum, valtype, true, /* retval= */ true);
28770 if (valret)
28771 return valret;
28772 /* Otherwise fall through to standard ABI rules. */
28775 if (TARGET_32BIT && TARGET_POWERPC64 && TYPE_MODE (valtype) == DImode)
28777 /* Long long return value need be split in -mpowerpc64, 32bit ABI. */
28778 return gen_rtx_PARALLEL (DImode,
28779 gen_rtvec (2,
28780 gen_rtx_EXPR_LIST (VOIDmode,
28781 gen_rtx_REG (SImode, GP_ARG_RETURN),
28782 const0_rtx),
28783 gen_rtx_EXPR_LIST (VOIDmode,
28784 gen_rtx_REG (SImode,
28785 GP_ARG_RETURN + 1),
28786 GEN_INT (4))));
28788 if (TARGET_32BIT && TARGET_POWERPC64 && TYPE_MODE (valtype) == DCmode)
28790 return gen_rtx_PARALLEL (DCmode,
28791 gen_rtvec (4,
28792 gen_rtx_EXPR_LIST (VOIDmode,
28793 gen_rtx_REG (SImode, GP_ARG_RETURN),
28794 const0_rtx),
28795 gen_rtx_EXPR_LIST (VOIDmode,
28796 gen_rtx_REG (SImode,
28797 GP_ARG_RETURN + 1),
28798 GEN_INT (4)),
28799 gen_rtx_EXPR_LIST (VOIDmode,
28800 gen_rtx_REG (SImode,
28801 GP_ARG_RETURN + 2),
28802 GEN_INT (8)),
28803 gen_rtx_EXPR_LIST (VOIDmode,
28804 gen_rtx_REG (SImode,
28805 GP_ARG_RETURN + 3),
28806 GEN_INT (12))));
28809 mode = TYPE_MODE (valtype);
28810 if ((INTEGRAL_TYPE_P (valtype) && GET_MODE_BITSIZE (mode) < BITS_PER_WORD)
28811 || POINTER_TYPE_P (valtype))
28812 mode = TARGET_32BIT ? SImode : DImode;
28814 if (DECIMAL_FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
28815 /* _Decimal128 must use an even/odd register pair. */
28816 regno = (mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
28817 else if (SCALAR_FLOAT_TYPE_P (valtype) && TARGET_HARD_FLOAT && TARGET_FPRS
28818 && ((TARGET_SINGLE_FLOAT && (mode == SFmode)) || TARGET_DOUBLE_FLOAT))
28819 regno = FP_ARG_RETURN;
28820 else if (TREE_CODE (valtype) == COMPLEX_TYPE
28821 && targetm.calls.split_complex_arg)
28822 return rs6000_complex_function_value (mode);
28823 /* VSX is a superset of Altivec and adds V2DImode/V2DFmode. Since the same
28824 return register is used in both cases, and we won't see V2DImode/V2DFmode
28825 for pure altivec, combine the two cases. */
28826 else if (TREE_CODE (valtype) == VECTOR_TYPE
28827 && TARGET_ALTIVEC && TARGET_ALTIVEC_ABI
28828 && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
28829 regno = ALTIVEC_ARG_RETURN;
28830 else if (TARGET_E500_DOUBLE && TARGET_HARD_FLOAT
28831 && (mode == DFmode || mode == DCmode
28832 || mode == TFmode || mode == TCmode))
28833 return spe_build_register_parallel (mode, GP_ARG_RETURN);
28834 else
28835 regno = GP_ARG_RETURN;
28837 return gen_rtx_REG (mode, regno);
28840 /* Define how to find the value returned by a library function
28841 assuming the value has mode MODE. */
28843 rs6000_libcall_value (enum machine_mode mode)
28845 unsigned int regno;
28847 if (TARGET_32BIT && TARGET_POWERPC64 && mode == DImode)
28849 /* Long long return value need be split in -mpowerpc64, 32bit ABI. */
28850 return gen_rtx_PARALLEL (DImode,
28851 gen_rtvec (2,
28852 gen_rtx_EXPR_LIST (VOIDmode,
28853 gen_rtx_REG (SImode, GP_ARG_RETURN),
28854 const0_rtx),
28855 gen_rtx_EXPR_LIST (VOIDmode,
28856 gen_rtx_REG (SImode,
28857 GP_ARG_RETURN + 1),
28858 GEN_INT (4))));
28861 if (DECIMAL_FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
28862 /* _Decimal128 must use an even/odd register pair. */
28863 regno = (mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
28864 else if (SCALAR_FLOAT_MODE_P (mode)
28865 && TARGET_HARD_FLOAT && TARGET_FPRS
28866 && ((TARGET_SINGLE_FLOAT && mode == SFmode) || TARGET_DOUBLE_FLOAT))
28867 regno = FP_ARG_RETURN;
28868 /* VSX is a superset of Altivec and adds V2DImode/V2DFmode. Since the same
28869 return register is used in both cases, and we won't see V2DImode/V2DFmode
28870 for pure altivec, combine the two cases. */
28871 else if (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
28872 && TARGET_ALTIVEC && TARGET_ALTIVEC_ABI)
28873 regno = ALTIVEC_ARG_RETURN;
28874 else if (COMPLEX_MODE_P (mode) && targetm.calls.split_complex_arg)
28875 return rs6000_complex_function_value (mode);
28876 else if (TARGET_E500_DOUBLE && TARGET_HARD_FLOAT
28877 && (mode == DFmode || mode == DCmode
28878 || mode == TFmode || mode == TCmode))
28879 return spe_build_register_parallel (mode, GP_ARG_RETURN);
28880 else
28881 regno = GP_ARG_RETURN;
28883 return gen_rtx_REG (mode, regno);
28887 /* Given FROM and TO register numbers, say whether this elimination is allowed.
28888 Frame pointer elimination is automatically handled.
28890 For the RS/6000, if frame pointer elimination is being done, we would like
28891 to convert ap into fp, not sp.
28893 We need r30 if -mminimal-toc was specified, and there are constant pool
28894 references. */
28896 static bool
28897 rs6000_can_eliminate (const int from, const int to)
28899 return (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM
28900 ? ! frame_pointer_needed
28901 : from == RS6000_PIC_OFFSET_TABLE_REGNUM
28902 ? ! TARGET_MINIMAL_TOC || TARGET_NO_TOC || get_pool_size () == 0
28903 : true);
28906 /* Define the offset between two registers, FROM to be eliminated and its
28907 replacement TO, at the start of a routine. */
28908 HOST_WIDE_INT
28909 rs6000_initial_elimination_offset (int from, int to)
28911 rs6000_stack_t *info = rs6000_stack_info ();
28912 HOST_WIDE_INT offset;
28914 if (from == HARD_FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
28915 offset = info->push_p ? 0 : -info->total_size;
28916 else if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
28918 offset = info->push_p ? 0 : -info->total_size;
28919 if (FRAME_GROWS_DOWNWARD)
28920 offset += info->fixed_size + info->vars_size + info->parm_size;
28922 else if (from == FRAME_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
28923 offset = FRAME_GROWS_DOWNWARD
28924 ? info->fixed_size + info->vars_size + info->parm_size
28925 : 0;
28926 else if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
28927 offset = info->total_size;
28928 else if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
28929 offset = info->push_p ? info->total_size : 0;
28930 else if (from == RS6000_PIC_OFFSET_TABLE_REGNUM)
28931 offset = 0;
28932 else
28933 gcc_unreachable ();
28935 return offset;
28938 static rtx
28939 rs6000_dwarf_register_span (rtx reg)
28941 rtx parts[8];
28942 int i, words;
28943 unsigned regno = REGNO (reg);
28944 enum machine_mode mode = GET_MODE (reg);
28946 if (TARGET_SPE
28947 && regno < 32
28948 && (SPE_VECTOR_MODE (GET_MODE (reg))
28949 || (TARGET_E500_DOUBLE && FLOAT_MODE_P (mode)
28950 && mode != SFmode && mode != SDmode && mode != SCmode)))
28952 else
28953 return NULL_RTX;
28955 regno = REGNO (reg);
28957 /* The duality of the SPE register size wreaks all kinds of havoc.
28958 This is a way of distinguishing r0 in 32-bits from r0 in
28959 64-bits. */
28960 words = (GET_MODE_SIZE (mode) + UNITS_PER_FP_WORD - 1) / UNITS_PER_FP_WORD;
28961 gcc_assert (words <= 4);
28962 for (i = 0; i < words; i++, regno++)
28964 if (BYTES_BIG_ENDIAN)
28966 parts[2 * i] = gen_rtx_REG (SImode, regno + 1200);
28967 parts[2 * i + 1] = gen_rtx_REG (SImode, regno);
28969 else
28971 parts[2 * i] = gen_rtx_REG (SImode, regno);
28972 parts[2 * i + 1] = gen_rtx_REG (SImode, regno + 1200);
28976 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (words * 2, parts));
28979 /* Fill in sizes for SPE register high parts in table used by unwinder. */
28981 static void
28982 rs6000_init_dwarf_reg_sizes_extra (tree address)
28984 if (TARGET_SPE)
28986 int i;
28987 enum machine_mode mode = TYPE_MODE (char_type_node);
28988 rtx addr = expand_expr (address, NULL_RTX, VOIDmode, EXPAND_NORMAL);
28989 rtx mem = gen_rtx_MEM (BLKmode, addr);
28990 rtx value = gen_int_mode (4, mode);
28992 for (i = 1201; i < 1232; i++)
28994 int column = DWARF_REG_TO_UNWIND_COLUMN (i);
28995 HOST_WIDE_INT offset
28996 = DWARF_FRAME_REGNUM (column) * GET_MODE_SIZE (mode);
28998 emit_move_insn (adjust_address (mem, mode, offset), value);
29003 /* Map internal gcc register numbers to DWARF2 register numbers. */
29005 unsigned int
29006 rs6000_dbx_register_number (unsigned int regno)
29008 if (regno <= 63 || write_symbols != DWARF2_DEBUG)
29009 return regno;
29010 if (regno == LR_REGNO)
29011 return 108;
29012 if (regno == CTR_REGNO)
29013 return 109;
29014 if (CR_REGNO_P (regno))
29015 return regno - CR0_REGNO + 86;
29016 if (regno == CA_REGNO)
29017 return 101; /* XER */
29018 if (ALTIVEC_REGNO_P (regno))
29019 return regno - FIRST_ALTIVEC_REGNO + 1124;
29020 if (regno == VRSAVE_REGNO)
29021 return 356;
29022 if (regno == VSCR_REGNO)
29023 return 67;
29024 if (regno == SPE_ACC_REGNO)
29025 return 99;
29026 if (regno == SPEFSCR_REGNO)
29027 return 612;
29028 /* SPE high reg number. We get these values of regno from
29029 rs6000_dwarf_register_span. */
29030 gcc_assert (regno >= 1200 && regno < 1232);
29031 return regno;
29034 /* target hook eh_return_filter_mode */
29035 static enum machine_mode
29036 rs6000_eh_return_filter_mode (void)
29038 return TARGET_32BIT ? SImode : word_mode;
29041 /* Target hook for scalar_mode_supported_p. */
29042 static bool
29043 rs6000_scalar_mode_supported_p (enum machine_mode mode)
29045 if (DECIMAL_FLOAT_MODE_P (mode))
29046 return default_decimal_float_supported_p ();
29047 else
29048 return default_scalar_mode_supported_p (mode);
29051 /* Target hook for vector_mode_supported_p. */
29052 static bool
29053 rs6000_vector_mode_supported_p (enum machine_mode mode)
29056 if (TARGET_PAIRED_FLOAT && PAIRED_VECTOR_MODE (mode))
29057 return true;
29059 if (TARGET_SPE && SPE_VECTOR_MODE (mode))
29060 return true;
29062 else if (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode))
29063 return true;
29065 else
29066 return false;
29069 /* Target hook for invalid_arg_for_unprototyped_fn. */
29070 static const char *
29071 invalid_arg_for_unprototyped_fn (const_tree typelist, const_tree funcdecl, const_tree val)
29073 return (!rs6000_darwin64_abi
29074 && typelist == 0
29075 && TREE_CODE (TREE_TYPE (val)) == VECTOR_TYPE
29076 && (funcdecl == NULL_TREE
29077 || (TREE_CODE (funcdecl) == FUNCTION_DECL
29078 && DECL_BUILT_IN_CLASS (funcdecl) != BUILT_IN_MD)))
29079 ? N_("AltiVec argument passed to unprototyped function")
29080 : NULL;
29083 /* For TARGET_SECURE_PLT 32-bit PIC code we can save PIC register
29084 setup by using __stack_chk_fail_local hidden function instead of
29085 calling __stack_chk_fail directly. Otherwise it is better to call
29086 __stack_chk_fail directly. */
29088 static tree ATTRIBUTE_UNUSED
29089 rs6000_stack_protect_fail (void)
29091 return (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
29092 ? default_hidden_stack_protect_fail ()
29093 : default_external_stack_protect_fail ();
29096 void
29097 rs6000_final_prescan_insn (rtx insn, rtx *operand ATTRIBUTE_UNUSED,
29098 int num_operands ATTRIBUTE_UNUSED)
29100 if (rs6000_warn_cell_microcode)
29102 const char *temp;
29103 int insn_code_number = recog_memoized (insn);
29104 location_t location = INSN_LOCATION (insn);
29106 /* Punt on insns we cannot recognize. */
29107 if (insn_code_number < 0)
29108 return;
29110 temp = get_insn_template (insn_code_number, insn);
29112 if (get_attr_cell_micro (insn) == CELL_MICRO_ALWAYS)
29113 warning_at (location, OPT_mwarn_cell_microcode,
29114 "emitting microcode insn %s\t[%s] #%d",
29115 temp, insn_data[INSN_CODE (insn)].name, INSN_UID (insn));
29116 else if (get_attr_cell_micro (insn) == CELL_MICRO_CONDITIONAL)
29117 warning_at (location, OPT_mwarn_cell_microcode,
29118 "emitting conditional microcode insn %s\t[%s] #%d",
29119 temp, insn_data[INSN_CODE (insn)].name, INSN_UID (insn));
29123 /* Implement the TARGET_ASAN_SHADOW_OFFSET hook. */
29125 #if TARGET_ELF
29126 static unsigned HOST_WIDE_INT
29127 rs6000_asan_shadow_offset (void)
29129 return (unsigned HOST_WIDE_INT) 1 << (TARGET_64BIT ? 41 : 29);
29131 #endif
29133 /* Mask options that we want to support inside of attribute((target)) and
29134 #pragma GCC target operations. Note, we do not include things like
29135 64/32-bit, endianess, hard/soft floating point, etc. that would have
29136 different calling sequences. */
29138 struct rs6000_opt_mask {
29139 const char *name; /* option name */
29140 HOST_WIDE_INT mask; /* mask to set */
29141 bool invert; /* invert sense of mask */
29142 bool valid_target; /* option is a target option */
29145 static struct rs6000_opt_mask const rs6000_opt_masks[] =
29147 { "altivec", OPTION_MASK_ALTIVEC, false, true },
29148 { "cmpb", OPTION_MASK_CMPB, false, true },
29149 { "crypto", OPTION_MASK_CRYPTO, false, true },
29150 { "direct-move", OPTION_MASK_DIRECT_MOVE, false, true },
29151 { "dlmzb", OPTION_MASK_DLMZB, false, true },
29152 { "fprnd", OPTION_MASK_FPRND, false, true },
29153 { "hard-dfp", OPTION_MASK_DFP, false, true },
29154 { "htm", OPTION_MASK_HTM, false, true },
29155 { "isel", OPTION_MASK_ISEL, false, true },
29156 { "mfcrf", OPTION_MASK_MFCRF, false, true },
29157 { "mfpgpr", OPTION_MASK_MFPGPR, false, true },
29158 { "mulhw", OPTION_MASK_MULHW, false, true },
29159 { "multiple", OPTION_MASK_MULTIPLE, false, true },
29160 { "popcntb", OPTION_MASK_POPCNTB, false, true },
29161 { "popcntd", OPTION_MASK_POPCNTD, false, true },
29162 { "power8-fusion", OPTION_MASK_P8_FUSION, false, true },
29163 { "power8-fusion-sign", OPTION_MASK_P8_FUSION_SIGN, false, true },
29164 { "power8-vector", OPTION_MASK_P8_VECTOR, false, true },
29165 { "powerpc-gfxopt", OPTION_MASK_PPC_GFXOPT, false, true },
29166 { "powerpc-gpopt", OPTION_MASK_PPC_GPOPT, false, true },
29167 { "quad-memory", OPTION_MASK_QUAD_MEMORY, false, true },
29168 { "recip-precision", OPTION_MASK_RECIP_PRECISION, false, true },
29169 { "string", OPTION_MASK_STRING, false, true },
29170 { "update", OPTION_MASK_NO_UPDATE, true , true },
29171 { "vsx", OPTION_MASK_VSX, false, true },
29172 { "vsx-timode", OPTION_MASK_VSX_TIMODE, false, true },
29173 #ifdef OPTION_MASK_64BIT
29174 #if TARGET_AIX_OS
29175 { "aix64", OPTION_MASK_64BIT, false, false },
29176 { "aix32", OPTION_MASK_64BIT, true, false },
29177 #else
29178 { "64", OPTION_MASK_64BIT, false, false },
29179 { "32", OPTION_MASK_64BIT, true, false },
29180 #endif
29181 #endif
29182 #ifdef OPTION_MASK_EABI
29183 { "eabi", OPTION_MASK_EABI, false, false },
29184 #endif
29185 #ifdef OPTION_MASK_LITTLE_ENDIAN
29186 { "little", OPTION_MASK_LITTLE_ENDIAN, false, false },
29187 { "big", OPTION_MASK_LITTLE_ENDIAN, true, false },
29188 #endif
29189 #ifdef OPTION_MASK_RELOCATABLE
29190 { "relocatable", OPTION_MASK_RELOCATABLE, false, false },
29191 #endif
29192 #ifdef OPTION_MASK_STRICT_ALIGN
29193 { "strict-align", OPTION_MASK_STRICT_ALIGN, false, false },
29194 #endif
29195 { "soft-float", OPTION_MASK_SOFT_FLOAT, false, false },
29196 { "string", OPTION_MASK_STRING, false, false },
29199 /* Builtin mask mapping for printing the flags. */
29200 static struct rs6000_opt_mask const rs6000_builtin_mask_names[] =
29202 { "altivec", RS6000_BTM_ALTIVEC, false, false },
29203 { "vsx", RS6000_BTM_VSX, false, false },
29204 { "spe", RS6000_BTM_SPE, false, false },
29205 { "paired", RS6000_BTM_PAIRED, false, false },
29206 { "fre", RS6000_BTM_FRE, false, false },
29207 { "fres", RS6000_BTM_FRES, false, false },
29208 { "frsqrte", RS6000_BTM_FRSQRTE, false, false },
29209 { "frsqrtes", RS6000_BTM_FRSQRTES, false, false },
29210 { "popcntd", RS6000_BTM_POPCNTD, false, false },
29211 { "cell", RS6000_BTM_CELL, false, false },
29212 { "power8-vector", RS6000_BTM_P8_VECTOR, false, false },
29213 { "crypto", RS6000_BTM_CRYPTO, false, false },
29214 { "htm", RS6000_BTM_HTM, false, false },
29217 /* Option variables that we want to support inside attribute((target)) and
29218 #pragma GCC target operations. */
29220 struct rs6000_opt_var {
29221 const char *name; /* option name */
29222 size_t global_offset; /* offset of the option in global_options. */
29223 size_t target_offset; /* offset of the option in target optiosn. */
29226 static struct rs6000_opt_var const rs6000_opt_vars[] =
29228 { "friz",
29229 offsetof (struct gcc_options, x_TARGET_FRIZ),
29230 offsetof (struct cl_target_option, x_TARGET_FRIZ), },
29231 { "avoid-indexed-addresses",
29232 offsetof (struct gcc_options, x_TARGET_AVOID_XFORM),
29233 offsetof (struct cl_target_option, x_TARGET_AVOID_XFORM) },
29234 { "paired",
29235 offsetof (struct gcc_options, x_rs6000_paired_float),
29236 offsetof (struct cl_target_option, x_rs6000_paired_float), },
29237 { "longcall",
29238 offsetof (struct gcc_options, x_rs6000_default_long_calls),
29239 offsetof (struct cl_target_option, x_rs6000_default_long_calls), },
29242 /* Inner function to handle attribute((target("..."))) and #pragma GCC target
29243 parsing. Return true if there were no errors. */
29245 static bool
29246 rs6000_inner_target_options (tree args, bool attr_p)
29248 bool ret = true;
29250 if (args == NULL_TREE)
29253 else if (TREE_CODE (args) == STRING_CST)
29255 char *p = ASTRDUP (TREE_STRING_POINTER (args));
29256 char *q;
29258 while ((q = strtok (p, ",")) != NULL)
29260 bool error_p = false;
29261 bool not_valid_p = false;
29262 const char *cpu_opt = NULL;
29264 p = NULL;
29265 if (strncmp (q, "cpu=", 4) == 0)
29267 int cpu_index = rs6000_cpu_name_lookup (q+4);
29268 if (cpu_index >= 0)
29269 rs6000_cpu_index = cpu_index;
29270 else
29272 error_p = true;
29273 cpu_opt = q+4;
29276 else if (strncmp (q, "tune=", 5) == 0)
29278 int tune_index = rs6000_cpu_name_lookup (q+5);
29279 if (tune_index >= 0)
29280 rs6000_tune_index = tune_index;
29281 else
29283 error_p = true;
29284 cpu_opt = q+5;
29287 else
29289 size_t i;
29290 bool invert = false;
29291 char *r = q;
29293 error_p = true;
29294 if (strncmp (r, "no-", 3) == 0)
29296 invert = true;
29297 r += 3;
29300 for (i = 0; i < ARRAY_SIZE (rs6000_opt_masks); i++)
29301 if (strcmp (r, rs6000_opt_masks[i].name) == 0)
29303 HOST_WIDE_INT mask = rs6000_opt_masks[i].mask;
29305 if (!rs6000_opt_masks[i].valid_target)
29306 not_valid_p = true;
29307 else
29309 error_p = false;
29310 rs6000_isa_flags_explicit |= mask;
29312 /* VSX needs altivec, so -mvsx automagically sets
29313 altivec. */
29314 if (mask == OPTION_MASK_VSX && !invert)
29315 mask |= OPTION_MASK_ALTIVEC;
29317 if (rs6000_opt_masks[i].invert)
29318 invert = !invert;
29320 if (invert)
29321 rs6000_isa_flags &= ~mask;
29322 else
29323 rs6000_isa_flags |= mask;
29325 break;
29328 if (error_p && !not_valid_p)
29330 for (i = 0; i < ARRAY_SIZE (rs6000_opt_vars); i++)
29331 if (strcmp (r, rs6000_opt_vars[i].name) == 0)
29333 size_t j = rs6000_opt_vars[i].global_offset;
29334 *((int *) ((char *)&global_options + j)) = !invert;
29335 error_p = false;
29336 break;
29341 if (error_p)
29343 const char *eprefix, *esuffix;
29345 ret = false;
29346 if (attr_p)
29348 eprefix = "__attribute__((__target__(";
29349 esuffix = ")))";
29351 else
29353 eprefix = "#pragma GCC target ";
29354 esuffix = "";
29357 if (cpu_opt)
29358 error ("invalid cpu \"%s\" for %s\"%s\"%s", cpu_opt, eprefix,
29359 q, esuffix);
29360 else if (not_valid_p)
29361 error ("%s\"%s\"%s is not allowed", eprefix, q, esuffix);
29362 else
29363 error ("%s\"%s\"%s is invalid", eprefix, q, esuffix);
29368 else if (TREE_CODE (args) == TREE_LIST)
29372 tree value = TREE_VALUE (args);
29373 if (value)
29375 bool ret2 = rs6000_inner_target_options (value, attr_p);
29376 if (!ret2)
29377 ret = false;
29379 args = TREE_CHAIN (args);
29381 while (args != NULL_TREE);
29384 else
29385 gcc_unreachable ();
29387 return ret;
29390 /* Print out the target options as a list for -mdebug=target. */
29392 static void
29393 rs6000_debug_target_options (tree args, const char *prefix)
29395 if (args == NULL_TREE)
29396 fprintf (stderr, "%s<NULL>", prefix);
29398 else if (TREE_CODE (args) == STRING_CST)
29400 char *p = ASTRDUP (TREE_STRING_POINTER (args));
29401 char *q;
29403 while ((q = strtok (p, ",")) != NULL)
29405 p = NULL;
29406 fprintf (stderr, "%s\"%s\"", prefix, q);
29407 prefix = ", ";
29411 else if (TREE_CODE (args) == TREE_LIST)
29415 tree value = TREE_VALUE (args);
29416 if (value)
29418 rs6000_debug_target_options (value, prefix);
29419 prefix = ", ";
29421 args = TREE_CHAIN (args);
29423 while (args != NULL_TREE);
29426 else
29427 gcc_unreachable ();
29429 return;
29433 /* Hook to validate attribute((target("..."))). */
29435 static bool
29436 rs6000_valid_attribute_p (tree fndecl,
29437 tree ARG_UNUSED (name),
29438 tree args,
29439 int flags)
29441 struct cl_target_option cur_target;
29442 bool ret;
29443 tree old_optimize = build_optimization_node ();
29444 tree new_target, new_optimize;
29445 tree func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
29447 gcc_assert ((fndecl != NULL_TREE) && (args != NULL_TREE));
29449 if (TARGET_DEBUG_TARGET)
29451 tree tname = DECL_NAME (fndecl);
29452 fprintf (stderr, "\n==================== rs6000_valid_attribute_p:\n");
29453 if (tname)
29454 fprintf (stderr, "function: %.*s\n",
29455 (int) IDENTIFIER_LENGTH (tname),
29456 IDENTIFIER_POINTER (tname));
29457 else
29458 fprintf (stderr, "function: unknown\n");
29460 fprintf (stderr, "args:");
29461 rs6000_debug_target_options (args, " ");
29462 fprintf (stderr, "\n");
29464 if (flags)
29465 fprintf (stderr, "flags: 0x%x\n", flags);
29467 fprintf (stderr, "--------------------\n");
29470 old_optimize = build_optimization_node ();
29471 func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
29473 /* If the function changed the optimization levels as well as setting target
29474 options, start with the optimizations specified. */
29475 if (func_optimize && func_optimize != old_optimize)
29476 cl_optimization_restore (&global_options,
29477 TREE_OPTIMIZATION (func_optimize));
29479 /* The target attributes may also change some optimization flags, so update
29480 the optimization options if necessary. */
29481 cl_target_option_save (&cur_target, &global_options);
29482 rs6000_cpu_index = rs6000_tune_index = -1;
29483 ret = rs6000_inner_target_options (args, true);
29485 /* Set up any additional state. */
29486 if (ret)
29488 ret = rs6000_option_override_internal (false);
29489 new_target = build_target_option_node ();
29491 else
29492 new_target = NULL;
29494 new_optimize = build_optimization_node ();
29496 if (!new_target)
29497 ret = false;
29499 else if (fndecl)
29501 DECL_FUNCTION_SPECIFIC_TARGET (fndecl) = new_target;
29503 if (old_optimize != new_optimize)
29504 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl) = new_optimize;
29507 cl_target_option_restore (&global_options, &cur_target);
29509 if (old_optimize != new_optimize)
29510 cl_optimization_restore (&global_options,
29511 TREE_OPTIMIZATION (old_optimize));
29513 return ret;
29517 /* Hook to validate the current #pragma GCC target and set the state, and
29518 update the macros based on what was changed. If ARGS is NULL, then
29519 POP_TARGET is used to reset the options. */
29521 bool
29522 rs6000_pragma_target_parse (tree args, tree pop_target)
29524 tree prev_tree = build_target_option_node ();
29525 tree cur_tree;
29526 struct cl_target_option *prev_opt, *cur_opt;
29527 HOST_WIDE_INT prev_flags, cur_flags, diff_flags;
29528 HOST_WIDE_INT prev_bumask, cur_bumask, diff_bumask;
29530 if (TARGET_DEBUG_TARGET)
29532 fprintf (stderr, "\n==================== rs6000_pragma_target_parse\n");
29533 fprintf (stderr, "args:");
29534 rs6000_debug_target_options (args, " ");
29535 fprintf (stderr, "\n");
29537 if (pop_target)
29539 fprintf (stderr, "pop_target:\n");
29540 debug_tree (pop_target);
29542 else
29543 fprintf (stderr, "pop_target: <NULL>\n");
29545 fprintf (stderr, "--------------------\n");
29548 if (! args)
29550 cur_tree = ((pop_target)
29551 ? pop_target
29552 : target_option_default_node);
29553 cl_target_option_restore (&global_options,
29554 TREE_TARGET_OPTION (cur_tree));
29556 else
29558 rs6000_cpu_index = rs6000_tune_index = -1;
29559 if (!rs6000_inner_target_options (args, false)
29560 || !rs6000_option_override_internal (false)
29561 || (cur_tree = build_target_option_node ()) == NULL_TREE)
29563 if (TARGET_DEBUG_BUILTIN || TARGET_DEBUG_TARGET)
29564 fprintf (stderr, "invalid pragma\n");
29566 return false;
29570 target_option_current_node = cur_tree;
29572 /* If we have the preprocessor linked in (i.e. C or C++ languages), possibly
29573 change the macros that are defined. */
29574 if (rs6000_target_modify_macros_ptr)
29576 prev_opt = TREE_TARGET_OPTION (prev_tree);
29577 prev_bumask = prev_opt->x_rs6000_builtin_mask;
29578 prev_flags = prev_opt->x_rs6000_isa_flags;
29580 cur_opt = TREE_TARGET_OPTION (cur_tree);
29581 cur_flags = cur_opt->x_rs6000_isa_flags;
29582 cur_bumask = cur_opt->x_rs6000_builtin_mask;
29584 diff_bumask = (prev_bumask ^ cur_bumask);
29585 diff_flags = (prev_flags ^ cur_flags);
29587 if ((diff_flags != 0) || (diff_bumask != 0))
29589 /* Delete old macros. */
29590 rs6000_target_modify_macros_ptr (false,
29591 prev_flags & diff_flags,
29592 prev_bumask & diff_bumask);
29594 /* Define new macros. */
29595 rs6000_target_modify_macros_ptr (true,
29596 cur_flags & diff_flags,
29597 cur_bumask & diff_bumask);
29601 return true;
29605 /* Remember the last target of rs6000_set_current_function. */
29606 static GTY(()) tree rs6000_previous_fndecl;
29608 /* Establish appropriate back-end context for processing the function
29609 FNDECL. The argument might be NULL to indicate processing at top
29610 level, outside of any function scope. */
29611 static void
29612 rs6000_set_current_function (tree fndecl)
29614 tree old_tree = (rs6000_previous_fndecl
29615 ? DECL_FUNCTION_SPECIFIC_TARGET (rs6000_previous_fndecl)
29616 : NULL_TREE);
29618 tree new_tree = (fndecl
29619 ? DECL_FUNCTION_SPECIFIC_TARGET (fndecl)
29620 : NULL_TREE);
29622 if (TARGET_DEBUG_TARGET)
29624 bool print_final = false;
29625 fprintf (stderr, "\n==================== rs6000_set_current_function");
29627 if (fndecl)
29628 fprintf (stderr, ", fndecl %s (%p)",
29629 (DECL_NAME (fndecl)
29630 ? IDENTIFIER_POINTER (DECL_NAME (fndecl))
29631 : "<unknown>"), (void *)fndecl);
29633 if (rs6000_previous_fndecl)
29634 fprintf (stderr, ", prev_fndecl (%p)", (void *)rs6000_previous_fndecl);
29636 fprintf (stderr, "\n");
29637 if (new_tree)
29639 fprintf (stderr, "\nnew fndecl target specific options:\n");
29640 debug_tree (new_tree);
29641 print_final = true;
29644 if (old_tree)
29646 fprintf (stderr, "\nold fndecl target specific options:\n");
29647 debug_tree (old_tree);
29648 print_final = true;
29651 if (print_final)
29652 fprintf (stderr, "--------------------\n");
29655 /* Only change the context if the function changes. This hook is called
29656 several times in the course of compiling a function, and we don't want to
29657 slow things down too much or call target_reinit when it isn't safe. */
29658 if (fndecl && fndecl != rs6000_previous_fndecl)
29660 rs6000_previous_fndecl = fndecl;
29661 if (old_tree == new_tree)
29664 else if (new_tree)
29666 cl_target_option_restore (&global_options,
29667 TREE_TARGET_OPTION (new_tree));
29668 target_reinit ();
29671 else if (old_tree)
29673 struct cl_target_option *def
29674 = TREE_TARGET_OPTION (target_option_current_node);
29676 cl_target_option_restore (&global_options, def);
29677 target_reinit ();
29683 /* Save the current options */
29685 static void
29686 rs6000_function_specific_save (struct cl_target_option *ptr)
29688 ptr->x_rs6000_isa_flags = rs6000_isa_flags;
29689 ptr->x_rs6000_isa_flags_explicit = rs6000_isa_flags_explicit;
29692 /* Restore the current options */
29694 static void
29695 rs6000_function_specific_restore (struct cl_target_option *ptr)
29697 rs6000_isa_flags = ptr->x_rs6000_isa_flags;
29698 rs6000_isa_flags_explicit = ptr->x_rs6000_isa_flags_explicit;
29699 (void) rs6000_option_override_internal (false);
29702 /* Print the current options */
29704 static void
29705 rs6000_function_specific_print (FILE *file, int indent,
29706 struct cl_target_option *ptr)
29708 rs6000_print_isa_options (file, indent, "Isa options set",
29709 ptr->x_rs6000_isa_flags);
29711 rs6000_print_isa_options (file, indent, "Isa options explicit",
29712 ptr->x_rs6000_isa_flags_explicit);
29715 /* Helper function to print the current isa or misc options on a line. */
29717 static void
29718 rs6000_print_options_internal (FILE *file,
29719 int indent,
29720 const char *string,
29721 HOST_WIDE_INT flags,
29722 const char *prefix,
29723 const struct rs6000_opt_mask *opts,
29724 size_t num_elements)
29726 size_t i;
29727 size_t start_column = 0;
29728 size_t cur_column;
29729 size_t max_column = 76;
29730 const char *comma = "";
29731 const char *nl = "\n";
29733 if (indent)
29734 start_column += fprintf (file, "%*s", indent, "");
29736 if (!flags)
29738 fprintf (stderr, DEBUG_FMT_S, string, "<none>");
29739 return;
29742 start_column += fprintf (stderr, DEBUG_FMT_WX, string, flags);
29744 /* Print the various mask options. */
29745 cur_column = start_column;
29746 for (i = 0; i < num_elements; i++)
29748 if ((flags & opts[i].mask) != 0)
29750 const char *no_str = rs6000_opt_masks[i].invert ? "no-" : "";
29751 size_t len = (strlen (comma)
29752 + strlen (prefix)
29753 + strlen (no_str)
29754 + strlen (rs6000_opt_masks[i].name));
29756 cur_column += len;
29757 if (cur_column > max_column)
29759 fprintf (stderr, ", \\\n%*s", (int)start_column, "");
29760 cur_column = start_column + len;
29761 comma = "";
29762 nl = "\n\n";
29765 fprintf (file, "%s%s%s%s", comma, prefix, no_str,
29766 rs6000_opt_masks[i].name);
29767 flags &= ~ opts[i].mask;
29768 comma = ", ";
29772 fputs (nl, file);
29775 /* Helper function to print the current isa options on a line. */
29777 static void
29778 rs6000_print_isa_options (FILE *file, int indent, const char *string,
29779 HOST_WIDE_INT flags)
29781 rs6000_print_options_internal (file, indent, string, flags, "-m",
29782 &rs6000_opt_masks[0],
29783 ARRAY_SIZE (rs6000_opt_masks));
29786 static void
29787 rs6000_print_builtin_options (FILE *file, int indent, const char *string,
29788 HOST_WIDE_INT flags)
29790 rs6000_print_options_internal (file, indent, string, flags, "",
29791 &rs6000_builtin_mask_names[0],
29792 ARRAY_SIZE (rs6000_builtin_mask_names));
29796 /* Hook to determine if one function can safely inline another. */
29798 static bool
29799 rs6000_can_inline_p (tree caller, tree callee)
29801 bool ret = false;
29802 tree caller_tree = DECL_FUNCTION_SPECIFIC_TARGET (caller);
29803 tree callee_tree = DECL_FUNCTION_SPECIFIC_TARGET (callee);
29805 /* If callee has no option attributes, then it is ok to inline. */
29806 if (!callee_tree)
29807 ret = true;
29809 /* If caller has no option attributes, but callee does then it is not ok to
29810 inline. */
29811 else if (!caller_tree)
29812 ret = false;
29814 else
29816 struct cl_target_option *caller_opts = TREE_TARGET_OPTION (caller_tree);
29817 struct cl_target_option *callee_opts = TREE_TARGET_OPTION (callee_tree);
29819 /* Callee's options should a subset of the caller's, i.e. a vsx function
29820 can inline an altivec function but a non-vsx function can't inline a
29821 vsx function. */
29822 if ((caller_opts->x_rs6000_isa_flags & callee_opts->x_rs6000_isa_flags)
29823 == callee_opts->x_rs6000_isa_flags)
29824 ret = true;
29827 if (TARGET_DEBUG_TARGET)
29828 fprintf (stderr, "rs6000_can_inline_p:, caller %s, callee %s, %s inline\n",
29829 (DECL_NAME (caller)
29830 ? IDENTIFIER_POINTER (DECL_NAME (caller))
29831 : "<unknown>"),
29832 (DECL_NAME (callee)
29833 ? IDENTIFIER_POINTER (DECL_NAME (callee))
29834 : "<unknown>"),
29835 (ret ? "can" : "cannot"));
29837 return ret;
29840 /* Allocate a stack temp and fixup the address so it meets the particular
29841 memory requirements (either offetable or REG+REG addressing). */
29844 rs6000_allocate_stack_temp (enum machine_mode mode,
29845 bool offsettable_p,
29846 bool reg_reg_p)
29848 rtx stack = assign_stack_temp (mode, GET_MODE_SIZE (mode));
29849 rtx addr = XEXP (stack, 0);
29850 int strict_p = (reload_in_progress || reload_completed);
29852 if (!legitimate_indirect_address_p (addr, strict_p))
29854 if (offsettable_p
29855 && !rs6000_legitimate_offset_address_p (mode, addr, strict_p, true))
29856 stack = replace_equiv_address (stack, copy_addr_to_reg (addr));
29858 else if (reg_reg_p && !legitimate_indexed_address_p (addr, strict_p))
29859 stack = replace_equiv_address (stack, copy_addr_to_reg (addr));
29862 return stack;
29865 /* Given a memory reference, if it is not a reg or reg+reg addressing, convert
29866 to such a form to deal with memory reference instructions like STFIWX that
29867 only take reg+reg addressing. */
29870 rs6000_address_for_fpconvert (rtx x)
29872 int strict_p = (reload_in_progress || reload_completed);
29873 rtx addr;
29875 gcc_assert (MEM_P (x));
29876 addr = XEXP (x, 0);
29877 if (! legitimate_indirect_address_p (addr, strict_p)
29878 && ! legitimate_indexed_address_p (addr, strict_p))
29880 if (GET_CODE (addr) == PRE_INC || GET_CODE (addr) == PRE_DEC)
29882 rtx reg = XEXP (addr, 0);
29883 HOST_WIDE_INT size = GET_MODE_SIZE (GET_MODE (x));
29884 rtx size_rtx = GEN_INT ((GET_CODE (addr) == PRE_DEC) ? -size : size);
29885 gcc_assert (REG_P (reg));
29886 emit_insn (gen_add3_insn (reg, reg, size_rtx));
29887 addr = reg;
29889 else if (GET_CODE (addr) == PRE_MODIFY)
29891 rtx reg = XEXP (addr, 0);
29892 rtx expr = XEXP (addr, 1);
29893 gcc_assert (REG_P (reg));
29894 gcc_assert (GET_CODE (expr) == PLUS);
29895 emit_insn (gen_add3_insn (reg, XEXP (expr, 0), XEXP (expr, 1)));
29896 addr = reg;
29899 x = replace_equiv_address (x, copy_addr_to_reg (addr));
29902 return x;
29905 /* Given a memory reference, if it is not in the form for altivec memory
29906 reference instructions (i.e. reg or reg+reg addressing with AND of -16),
29907 convert to the altivec format. */
29910 rs6000_address_for_altivec (rtx x)
29912 gcc_assert (MEM_P (x));
29913 if (!altivec_indexed_or_indirect_operand (x, GET_MODE (x)))
29915 rtx addr = XEXP (x, 0);
29916 int strict_p = (reload_in_progress || reload_completed);
29918 if (!legitimate_indexed_address_p (addr, strict_p)
29919 && !legitimate_indirect_address_p (addr, strict_p))
29920 addr = copy_to_mode_reg (Pmode, addr);
29922 addr = gen_rtx_AND (Pmode, addr, GEN_INT (-16));
29923 x = change_address (x, GET_MODE (x), addr);
29926 return x;
29929 /* Implement TARGET_LEGITIMATE_CONSTANT_P.
29931 On the RS/6000, all integer constants are acceptable, most won't be valid
29932 for particular insns, though. Only easy FP constants are acceptable. */
29934 static bool
29935 rs6000_legitimate_constant_p (enum machine_mode mode, rtx x)
29937 if (TARGET_ELF && rs6000_tls_referenced_p (x))
29938 return false;
29940 return ((GET_CODE (x) != CONST_DOUBLE && GET_CODE (x) != CONST_VECTOR)
29941 || GET_MODE (x) == VOIDmode
29942 || (TARGET_POWERPC64 && mode == DImode)
29943 || easy_fp_constant (x, mode)
29944 || easy_vector_constant (x, mode));
29948 /* A function pointer under AIX is a pointer to a data area whose first word
29949 contains the actual address of the function, whose second word contains a
29950 pointer to its TOC, and whose third word contains a value to place in the
29951 static chain register (r11). Note that if we load the static chain, our
29952 "trampoline" need not have any executable code. */
29954 void
29955 rs6000_call_indirect_aix (rtx value, rtx func_desc, rtx flag)
29957 rtx func_addr;
29958 rtx toc_reg;
29959 rtx sc_reg;
29960 rtx stack_ptr;
29961 rtx stack_toc_offset;
29962 rtx stack_toc_mem;
29963 rtx func_toc_offset;
29964 rtx func_toc_mem;
29965 rtx func_sc_offset;
29966 rtx func_sc_mem;
29967 rtx insn;
29968 rtx (*call_func) (rtx, rtx, rtx, rtx);
29969 rtx (*call_value_func) (rtx, rtx, rtx, rtx, rtx);
29971 stack_ptr = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
29972 toc_reg = gen_rtx_REG (Pmode, TOC_REGNUM);
29974 /* Load up address of the actual function. */
29975 func_desc = force_reg (Pmode, func_desc);
29976 func_addr = gen_reg_rtx (Pmode);
29977 emit_move_insn (func_addr, gen_rtx_MEM (Pmode, func_desc));
29979 if (TARGET_32BIT)
29982 stack_toc_offset = GEN_INT (TOC_SAVE_OFFSET_32BIT);
29983 func_toc_offset = GEN_INT (AIX_FUNC_DESC_TOC_32BIT);
29984 func_sc_offset = GEN_INT (AIX_FUNC_DESC_SC_32BIT);
29985 if (TARGET_POINTERS_TO_NESTED_FUNCTIONS)
29987 call_func = gen_call_indirect_aix32bit;
29988 call_value_func = gen_call_value_indirect_aix32bit;
29990 else
29992 call_func = gen_call_indirect_aix32bit_nor11;
29993 call_value_func = gen_call_value_indirect_aix32bit_nor11;
29996 else
29998 stack_toc_offset = GEN_INT (TOC_SAVE_OFFSET_64BIT);
29999 func_toc_offset = GEN_INT (AIX_FUNC_DESC_TOC_64BIT);
30000 func_sc_offset = GEN_INT (AIX_FUNC_DESC_SC_64BIT);
30001 if (TARGET_POINTERS_TO_NESTED_FUNCTIONS)
30003 call_func = gen_call_indirect_aix64bit;
30004 call_value_func = gen_call_value_indirect_aix64bit;
30006 else
30008 call_func = gen_call_indirect_aix64bit_nor11;
30009 call_value_func = gen_call_value_indirect_aix64bit_nor11;
30013 /* Reserved spot to store the TOC. */
30014 stack_toc_mem = gen_frame_mem (Pmode,
30015 gen_rtx_PLUS (Pmode,
30016 stack_ptr,
30017 stack_toc_offset));
30019 gcc_assert (cfun);
30020 gcc_assert (cfun->machine);
30022 /* Can we optimize saving the TOC in the prologue or do we need to do it at
30023 every call? */
30024 if (TARGET_SAVE_TOC_INDIRECT && !cfun->calls_alloca)
30025 cfun->machine->save_toc_in_prologue = true;
30027 else
30029 MEM_VOLATILE_P (stack_toc_mem) = 1;
30030 emit_move_insn (stack_toc_mem, toc_reg);
30033 /* Calculate the address to load the TOC of the called function. We don't
30034 actually load this until the split after reload. */
30035 func_toc_mem = gen_rtx_MEM (Pmode,
30036 gen_rtx_PLUS (Pmode,
30037 func_desc,
30038 func_toc_offset));
30040 /* If we have a static chain, load it up. */
30041 if (TARGET_POINTERS_TO_NESTED_FUNCTIONS)
30043 func_sc_mem = gen_rtx_MEM (Pmode,
30044 gen_rtx_PLUS (Pmode,
30045 func_desc,
30046 func_sc_offset));
30048 sc_reg = gen_rtx_REG (Pmode, STATIC_CHAIN_REGNUM);
30049 emit_move_insn (sc_reg, func_sc_mem);
30052 /* Create the call. */
30053 if (value)
30054 insn = call_value_func (value, func_addr, flag, func_toc_mem,
30055 stack_toc_mem);
30056 else
30057 insn = call_func (func_addr, flag, func_toc_mem, stack_toc_mem);
30059 emit_call_insn (insn);
30062 /* Return whether we need to always update the saved TOC pointer when we update
30063 the stack pointer. */
30065 static bool
30066 rs6000_save_toc_in_prologue_p (void)
30068 return (cfun && cfun->machine && cfun->machine->save_toc_in_prologue);
30071 #ifdef HAVE_GAS_HIDDEN
30072 # define USE_HIDDEN_LINKONCE 1
30073 #else
30074 # define USE_HIDDEN_LINKONCE 0
30075 #endif
30077 /* Fills in the label name that should be used for a 476 link stack thunk. */
30079 void
30080 get_ppc476_thunk_name (char name[32])
30082 gcc_assert (TARGET_LINK_STACK);
30084 if (USE_HIDDEN_LINKONCE)
30085 sprintf (name, "__ppc476.get_thunk");
30086 else
30087 ASM_GENERATE_INTERNAL_LABEL (name, "LPPC476_", 0);
30090 /* This function emits the simple thunk routine that is used to preserve
30091 the link stack on the 476 cpu. */
30093 static void rs6000_code_end (void) ATTRIBUTE_UNUSED;
30094 static void
30095 rs6000_code_end (void)
30097 char name[32];
30098 tree decl;
30100 if (!TARGET_LINK_STACK)
30101 return;
30103 get_ppc476_thunk_name (name);
30105 decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL, get_identifier (name),
30106 build_function_type_list (void_type_node, NULL_TREE));
30107 DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
30108 NULL_TREE, void_type_node);
30109 TREE_PUBLIC (decl) = 1;
30110 TREE_STATIC (decl) = 1;
30112 #if RS6000_WEAK
30113 if (USE_HIDDEN_LINKONCE)
30115 DECL_COMDAT_GROUP (decl) = DECL_ASSEMBLER_NAME (decl);
30116 targetm.asm_out.unique_section (decl, 0);
30117 switch_to_section (get_named_section (decl, NULL, 0));
30118 DECL_WEAK (decl) = 1;
30119 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
30120 targetm.asm_out.globalize_label (asm_out_file, name);
30121 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
30122 ASM_DECLARE_FUNCTION_NAME (asm_out_file, name, decl);
30124 else
30125 #endif
30127 switch_to_section (text_section);
30128 ASM_OUTPUT_LABEL (asm_out_file, name);
30131 DECL_INITIAL (decl) = make_node (BLOCK);
30132 current_function_decl = decl;
30133 init_function_start (decl);
30134 first_function_block_is_cold = false;
30135 /* Make sure unwind info is emitted for the thunk if needed. */
30136 final_start_function (emit_barrier (), asm_out_file, 1);
30138 fputs ("\tblr\n", asm_out_file);
30140 final_end_function ();
30141 init_insn_lengths ();
30142 free_after_compilation (cfun);
30143 set_cfun (NULL);
30144 current_function_decl = NULL;
30147 /* Add r30 to hard reg set if the prologue sets it up and it is not
30148 pic_offset_table_rtx. */
30150 static void
30151 rs6000_set_up_by_prologue (struct hard_reg_set_container *set)
30153 if (!TARGET_SINGLE_PIC_BASE
30154 && TARGET_TOC
30155 && TARGET_MINIMAL_TOC
30156 && get_pool_size () != 0)
30157 add_to_hard_reg_set (&set->set, Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
30161 /* Helper function for rs6000_split_logical to emit a logical instruction after
30162 spliting the operation to single GPR registers.
30164 DEST is the destination register.
30165 OP1 and OP2 are the input source registers.
30166 CODE is the base operation (AND, IOR, XOR, NOT).
30167 MODE is the machine mode.
30168 If COMPLEMENT_FINAL_P is true, wrap the whole operation with NOT.
30169 If COMPLEMENT_OP1_P is true, wrap operand1 with NOT.
30170 If COMPLEMENT_OP2_P is true, wrap operand2 with NOT.
30171 CLOBBER_REG is either NULL or a scratch register of type CC to allow
30172 formation of the AND instructions. */
30174 static void
30175 rs6000_split_logical_inner (rtx dest,
30176 rtx op1,
30177 rtx op2,
30178 enum rtx_code code,
30179 enum machine_mode mode,
30180 bool complement_final_p,
30181 bool complement_op1_p,
30182 bool complement_op2_p,
30183 rtx clobber_reg)
30185 rtx bool_rtx;
30186 rtx set_rtx;
30188 /* Optimize AND of 0/0xffffffff and IOR/XOR of 0. */
30189 if (op2 && GET_CODE (op2) == CONST_INT
30190 && (mode == SImode || (mode == DImode && TARGET_POWERPC64))
30191 && !complement_final_p && !complement_op1_p && !complement_op2_p)
30193 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
30194 HOST_WIDE_INT value = INTVAL (op2) & mask;
30196 /* Optimize AND of 0 to just set 0. Optimize AND of -1 to be a move. */
30197 if (code == AND)
30199 if (value == 0)
30201 emit_insn (gen_rtx_SET (VOIDmode, dest, const0_rtx));
30202 return;
30205 else if (value == mask)
30207 if (!rtx_equal_p (dest, op1))
30208 emit_insn (gen_rtx_SET (VOIDmode, dest, op1));
30209 return;
30213 /* Optimize IOR/XOR of 0 to be a simple move. Split large operations
30214 into separate ORI/ORIS or XORI/XORIS instrucitons. */
30215 else if (code == IOR || code == XOR)
30217 if (value == 0)
30219 if (!rtx_equal_p (dest, op1))
30220 emit_insn (gen_rtx_SET (VOIDmode, dest, op1));
30221 return;
30226 if (complement_op1_p)
30227 op1 = gen_rtx_NOT (mode, op1);
30229 if (complement_op2_p)
30230 op2 = gen_rtx_NOT (mode, op2);
30232 bool_rtx = ((code == NOT)
30233 ? gen_rtx_NOT (mode, op1)
30234 : gen_rtx_fmt_ee (code, mode, op1, op2));
30236 if (complement_final_p)
30237 bool_rtx = gen_rtx_NOT (mode, bool_rtx);
30239 set_rtx = gen_rtx_SET (VOIDmode, dest, bool_rtx);
30241 /* Is this AND with an explicit clobber? */
30242 if (clobber_reg)
30244 rtx clobber = gen_rtx_CLOBBER (VOIDmode, clobber_reg);
30245 set_rtx = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set_rtx, clobber));
30248 emit_insn (set_rtx);
30249 return;
30252 /* Split a DImode AND/IOR/XOR with a constant on a 32-bit system. These
30253 operations are split immediately during RTL generation to allow for more
30254 optimizations of the AND/IOR/XOR.
30256 OPERANDS is an array containing the destination and two input operands.
30257 CODE is the base operation (AND, IOR, XOR, NOT).
30258 MODE is the machine mode.
30259 If COMPLEMENT_FINAL_P is true, wrap the whole operation with NOT.
30260 If COMPLEMENT_OP1_P is true, wrap operand1 with NOT.
30261 If COMPLEMENT_OP2_P is true, wrap operand2 with NOT.
30262 CLOBBER_REG is either NULL or a scratch register of type CC to allow
30263 formation of the AND instructions. */
30265 static void
30266 rs6000_split_logical_di (rtx operands[3],
30267 enum rtx_code code,
30268 bool complement_final_p,
30269 bool complement_op1_p,
30270 bool complement_op2_p,
30271 rtx clobber_reg)
30273 const HOST_WIDE_INT lower_32bits = HOST_WIDE_INT_C(0xffffffff);
30274 const HOST_WIDE_INT upper_32bits = ~ lower_32bits;
30275 const HOST_WIDE_INT sign_bit = HOST_WIDE_INT_C(0x80000000);
30276 enum hi_lo { hi = 0, lo = 1 };
30277 rtx op0_hi_lo[2], op1_hi_lo[2], op2_hi_lo[2];
30278 size_t i;
30280 op0_hi_lo[hi] = gen_highpart (SImode, operands[0]);
30281 op1_hi_lo[hi] = gen_highpart (SImode, operands[1]);
30282 op0_hi_lo[lo] = gen_lowpart (SImode, operands[0]);
30283 op1_hi_lo[lo] = gen_lowpart (SImode, operands[1]);
30285 if (code == NOT)
30286 op2_hi_lo[hi] = op2_hi_lo[lo] = NULL_RTX;
30287 else
30289 if (GET_CODE (operands[2]) != CONST_INT)
30291 op2_hi_lo[hi] = gen_highpart_mode (SImode, DImode, operands[2]);
30292 op2_hi_lo[lo] = gen_lowpart (SImode, operands[2]);
30294 else
30296 HOST_WIDE_INT value = INTVAL (operands[2]);
30297 HOST_WIDE_INT value_hi_lo[2];
30299 gcc_assert (!complement_final_p);
30300 gcc_assert (!complement_op1_p);
30301 gcc_assert (!complement_op2_p);
30303 value_hi_lo[hi] = value >> 32;
30304 value_hi_lo[lo] = value & lower_32bits;
30306 for (i = 0; i < 2; i++)
30308 HOST_WIDE_INT sub_value = value_hi_lo[i];
30310 if (sub_value & sign_bit)
30311 sub_value |= upper_32bits;
30313 op2_hi_lo[i] = GEN_INT (sub_value);
30315 /* If this is an AND instruction, check to see if we need to load
30316 the value in a register. */
30317 if (code == AND && sub_value != -1 && sub_value != 0
30318 && !and_operand (op2_hi_lo[i], SImode))
30319 op2_hi_lo[i] = force_reg (SImode, op2_hi_lo[i]);
30324 for (i = 0; i < 2; i++)
30326 /* Split large IOR/XOR operations. */
30327 if ((code == IOR || code == XOR)
30328 && GET_CODE (op2_hi_lo[i]) == CONST_INT
30329 && !complement_final_p
30330 && !complement_op1_p
30331 && !complement_op2_p
30332 && clobber_reg == NULL_RTX
30333 && !logical_const_operand (op2_hi_lo[i], SImode))
30335 HOST_WIDE_INT value = INTVAL (op2_hi_lo[i]);
30336 HOST_WIDE_INT hi_16bits = value & HOST_WIDE_INT_C(0xffff0000);
30337 HOST_WIDE_INT lo_16bits = value & HOST_WIDE_INT_C(0x0000ffff);
30338 rtx tmp = gen_reg_rtx (SImode);
30340 /* Make sure the constant is sign extended. */
30341 if ((hi_16bits & sign_bit) != 0)
30342 hi_16bits |= upper_32bits;
30344 rs6000_split_logical_inner (tmp, op1_hi_lo[i], GEN_INT (hi_16bits),
30345 code, SImode, false, false, false,
30346 NULL_RTX);
30348 rs6000_split_logical_inner (op0_hi_lo[i], tmp, GEN_INT (lo_16bits),
30349 code, SImode, false, false, false,
30350 NULL_RTX);
30352 else
30353 rs6000_split_logical_inner (op0_hi_lo[i], op1_hi_lo[i], op2_hi_lo[i],
30354 code, SImode, complement_final_p,
30355 complement_op1_p, complement_op2_p,
30356 clobber_reg);
30359 return;
30362 /* Split the insns that make up boolean operations operating on multiple GPR
30363 registers. The boolean MD patterns ensure that the inputs either are
30364 exactly the same as the output registers, or there is no overlap.
30366 OPERANDS is an array containing the destination and two input operands.
30367 CODE is the base operation (AND, IOR, XOR, NOT).
30368 MODE is the machine mode.
30369 If COMPLEMENT_FINAL_P is true, wrap the whole operation with NOT.
30370 If COMPLEMENT_OP1_P is true, wrap operand1 with NOT.
30371 If COMPLEMENT_OP2_P is true, wrap operand2 with NOT.
30372 CLOBBER_REG is either NULL or a scratch register of type CC to allow
30373 formation of the AND instructions. */
30375 void
30376 rs6000_split_logical (rtx operands[3],
30377 enum rtx_code code,
30378 bool complement_final_p,
30379 bool complement_op1_p,
30380 bool complement_op2_p,
30381 rtx clobber_reg)
30383 enum machine_mode mode = GET_MODE (operands[0]);
30384 enum machine_mode sub_mode;
30385 rtx op0, op1, op2;
30386 int sub_size, regno0, regno1, nregs, i;
30388 /* If this is DImode, use the specialized version that can run before
30389 register allocation. */
30390 if (mode == DImode && !TARGET_POWERPC64)
30392 rs6000_split_logical_di (operands, code, complement_final_p,
30393 complement_op1_p, complement_op2_p,
30394 clobber_reg);
30395 return;
30398 op0 = operands[0];
30399 op1 = operands[1];
30400 op2 = (code == NOT) ? NULL_RTX : operands[2];
30401 sub_mode = (TARGET_POWERPC64) ? DImode : SImode;
30402 sub_size = GET_MODE_SIZE (sub_mode);
30403 regno0 = REGNO (op0);
30404 regno1 = REGNO (op1);
30406 gcc_assert (reload_completed);
30407 gcc_assert (IN_RANGE (regno0, FIRST_GPR_REGNO, LAST_GPR_REGNO));
30408 gcc_assert (IN_RANGE (regno1, FIRST_GPR_REGNO, LAST_GPR_REGNO));
30410 nregs = rs6000_hard_regno_nregs[(int)mode][regno0];
30411 gcc_assert (nregs > 1);
30413 if (op2 && REG_P (op2))
30414 gcc_assert (IN_RANGE (REGNO (op2), FIRST_GPR_REGNO, LAST_GPR_REGNO));
30416 for (i = 0; i < nregs; i++)
30418 int offset = i * sub_size;
30419 rtx sub_op0 = simplify_subreg (sub_mode, op0, mode, offset);
30420 rtx sub_op1 = simplify_subreg (sub_mode, op1, mode, offset);
30421 rtx sub_op2 = ((code == NOT)
30422 ? NULL_RTX
30423 : simplify_subreg (sub_mode, op2, mode, offset));
30425 rs6000_split_logical_inner (sub_op0, sub_op1, sub_op2, code, sub_mode,
30426 complement_final_p, complement_op1_p,
30427 complement_op2_p, clobber_reg);
30430 return;
30434 /* Return true if the peephole2 can combine a load involving a combination of
30435 an addis instruction and a load with an offset that can be fused together on
30436 a power8. */
30438 bool
30439 fusion_gpr_load_p (rtx addis_reg, /* reg. to hold high value. */
30440 rtx addis_value, /* high value loaded. */
30441 rtx target, /* reg. that is loaded. */
30442 rtx mem, /* memory to load. */
30443 rtx insn) /* insn for looking up reg notes or
30444 NULL_RTX if this is a peephole2. */
30446 rtx addr;
30447 rtx base_reg;
30449 /* Validate arguments. */
30450 if (!base_reg_operand (addis_reg, GET_MODE (addis_reg)))
30451 return false;
30453 if (!base_reg_operand (target, GET_MODE (target)))
30454 return false;
30456 if (!fusion_gpr_addis (addis_value, GET_MODE (addis_value)))
30457 return false;
30459 if (!fusion_gpr_mem_load (mem, GET_MODE (mem)))
30460 return false;
30462 /* Validate that the register used to load the high value is either the
30463 register being loaded, or we can safely replace its use in a peephole.
30465 If this is a peephole2, we assume that there are 2 instructions in the
30466 peephole (addis and load), so we want to check if the target register was
30467 not used and the register to hold the addis result is dead after the
30468 peephole. */
30469 if (REGNO (addis_reg) != REGNO (target))
30471 if (reg_mentioned_p (target, mem))
30472 return false;
30474 if (insn)
30476 if (!find_reg_note (insn, REG_DEAD, addis_reg))
30477 return false;
30479 else
30481 if (!peep2_reg_dead_p (2, addis_reg))
30482 return false;
30486 /* Validate that the value being loaded in the addis is used in the load. */
30487 addr = XEXP (mem, 0); /* either PLUS or LO_SUM. */
30488 if (GET_CODE (addr) != PLUS && GET_CODE (addr) != LO_SUM)
30489 return false;
30491 base_reg = XEXP (addr, 0);
30492 return REGNO (addis_reg) == REGNO (base_reg);
30495 /* Return a string to fuse an addis instruction with a gpr load to the same
30496 register that we loaded up the addis instruction. The code is complicated,
30497 so we call output_asm_insn directly, and just return "". */
30499 const char *
30500 emit_fusion_gpr_load (rtx addis_reg, rtx addis_value, rtx target, rtx mem)
30502 rtx fuse_ops[10];
30503 rtx addr;
30504 rtx load_offset;
30505 const char *addis_str = NULL;
30506 const char *load_str = NULL;
30507 const char *mode_name = NULL;
30508 char insn_template[80];
30509 enum machine_mode mode = GET_MODE (mem);
30510 const char *comment_str = ASM_COMMENT_START;
30512 if (*comment_str == ' ')
30513 comment_str++;
30515 if (!MEM_P (mem))
30516 gcc_unreachable ();
30518 addr = XEXP (mem, 0);
30519 if (GET_CODE (addr) != PLUS && GET_CODE (addr) != LO_SUM)
30520 gcc_unreachable ();
30522 load_offset = XEXP (addr, 1);
30524 /* Now emit the load instruction to the same register. */
30525 switch (mode)
30527 case QImode:
30528 mode_name = "char";
30529 load_str = "lbz";
30530 break;
30532 case HImode:
30533 mode_name = "short";
30534 load_str = "lhz";
30535 break;
30537 case SImode:
30538 mode_name = "int";
30539 load_str = "lwz";
30540 break;
30542 case DImode:
30543 if (TARGET_POWERPC64)
30545 mode_name = "long";
30546 load_str = "ld";
30548 break;
30550 default:
30551 break;
30554 if (!load_str)
30555 gcc_unreachable ();
30557 /* Emit the addis instruction. */
30558 fuse_ops[0] = target;
30559 fuse_ops[1] = addis_reg;
30560 if (satisfies_constraint_L (addis_value))
30562 fuse_ops[2] = addis_value;
30563 addis_str = "lis %0,%v2";
30566 else if (GET_CODE (addis_value) == PLUS)
30568 rtx op0 = XEXP (addis_value, 0);
30569 rtx op1 = XEXP (addis_value, 1);
30571 if (REG_P (op0) && CONST_INT_P (op1)
30572 && satisfies_constraint_L (op1))
30574 fuse_ops[2] = op0;
30575 fuse_ops[3] = op1;
30576 addis_str = "addis %0,%2,%v3";
30580 else if (GET_CODE (addis_value) == HIGH)
30582 rtx value = XEXP (addis_value, 0);
30583 if (GET_CODE (value) == UNSPEC && XINT (value, 1) == UNSPEC_TOCREL)
30585 fuse_ops[2] = XVECEXP (value, 0, 0); /* symbol ref. */
30586 fuse_ops[3] = XVECEXP (value, 0, 1); /* TOC register. */
30587 if (TARGET_ELF)
30588 addis_str = "addis %0,%3,%2@toc@ha";
30590 else if (TARGET_XCOFF)
30591 addis_str = "addis %0,%2@u(%3)";
30593 else
30594 gcc_unreachable ();
30597 else if (GET_CODE (value) == PLUS)
30599 rtx op0 = XEXP (value, 0);
30600 rtx op1 = XEXP (value, 1);
30602 if (GET_CODE (op0) == UNSPEC
30603 && XINT (op0, 1) == UNSPEC_TOCREL
30604 && CONST_INT_P (op1))
30606 fuse_ops[2] = XVECEXP (op0, 0, 0); /* symbol ref. */
30607 fuse_ops[3] = XVECEXP (op0, 0, 1); /* TOC register. */
30608 fuse_ops[4] = op1;
30609 if (TARGET_ELF)
30610 addis_str = "addis %0,%3,%2+%4@toc@ha";
30612 else if (TARGET_XCOFF)
30613 addis_str = "addis %0,%2+%4@u(%3)";
30615 else
30616 gcc_unreachable ();
30620 else if (satisfies_constraint_L (value))
30622 fuse_ops[2] = value;
30623 addis_str = "lis %0,%v2";
30626 else if (TARGET_ELF && !TARGET_POWERPC64 && CONSTANT_P (value))
30628 fuse_ops[2] = value;
30629 addis_str = "lis %0,%2@ha";
30633 if (!addis_str)
30634 fatal_insn ("Could not generate addis value for fusion", addis_value);
30636 sprintf (insn_template, "%s\t\t%s gpr load fusion, type %s, addis reg %%1",
30637 addis_str, comment_str, mode_name);
30638 output_asm_insn (insn_template, fuse_ops);
30640 if (CONST_INT_P (load_offset) && satisfies_constraint_I (load_offset))
30642 sprintf (insn_template, "%s %%0,%%1(%%0)", load_str);
30643 fuse_ops[1] = load_offset;
30644 output_asm_insn (insn_template, fuse_ops);
30647 else if (GET_CODE (load_offset) == UNSPEC
30648 && XINT (load_offset, 1) == UNSPEC_TOCREL)
30650 if (TARGET_ELF)
30651 sprintf (insn_template, "%s %%0,%%1@toc@l(%%0)", load_str);
30653 else if (TARGET_XCOFF)
30654 sprintf (insn_template, "%s %%0,%%1@l(%%0)", load_str);
30656 else
30657 gcc_unreachable ();
30659 fuse_ops[1] = XVECEXP (load_offset, 0, 0);
30660 output_asm_insn (insn_template, fuse_ops);
30663 else if (GET_CODE (load_offset) == PLUS
30664 && GET_CODE (XEXP (load_offset, 0)) == UNSPEC
30665 && XINT (XEXP (load_offset, 0), 1) == UNSPEC_TOCREL
30666 && CONST_INT_P (XEXP (load_offset, 1)))
30668 rtx tocrel_unspec = XEXP (load_offset, 0);
30669 if (TARGET_ELF)
30670 sprintf (insn_template, "%s %%0,%%1+%%2@toc@l(%%0)", load_str);
30672 else if (TARGET_XCOFF)
30673 sprintf (insn_template, "%s %%0,%%1+%%2@l(%%0)", load_str);
30675 else
30676 gcc_unreachable ();
30678 fuse_ops[1] = XVECEXP (tocrel_unspec, 0, 0);
30679 fuse_ops[2] = XEXP (load_offset, 1);
30680 output_asm_insn (insn_template, fuse_ops);
30683 else if (TARGET_ELF && !TARGET_POWERPC64 && CONSTANT_P (load_offset))
30685 sprintf (insn_template, "%s %%0,%%1@l(%%0)", load_str);
30687 fuse_ops[1] = load_offset;
30688 output_asm_insn (insn_template, fuse_ops);
30691 else
30692 fatal_insn ("Unable to generate load offset for fusion", load_offset);
30694 return "";
30698 struct gcc_target targetm = TARGET_INITIALIZER;
30700 #include "gt-rs6000.h"